72ea6c897cfb54a4aafd81762fb0022e7386ca4b
[dpdk.git] / drivers / bus / fslmc / fslmc_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2015-2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Freescale Semiconductor, Inc nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/mman.h>
45 #include <sys/vfs.h>
46 #include <libgen.h>
47 #include <dirent.h>
48 #include <sys/eventfd.h>
49
50 #include <rte_mbuf.h>
51 #include <rte_ethdev.h>
52 #include <rte_malloc.h>
53 #include <rte_memcpy.h>
54 #include <rte_string_fns.h>
55 #include <rte_cycles.h>
56 #include <rte_kvargs.h>
57 #include <rte_dev.h>
58 #include <rte_ethdev.h>
59 #include <rte_bus.h>
60
61 #include "rte_fslmc.h"
62 #include "fslmc_vfio.h"
63
64 #define VFIO_MAX_CONTAINERS     1
65
66 #define FSLMC_VFIO_LOG(level, fmt, args...) \
67         RTE_LOG(level, EAL, "%s(): " fmt "\n", __func__, ##args)
68
69 /** Pathname of FSL-MC devices directory. */
70 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
71
72 /* Number of VFIO containers & groups with in */
73 static struct fslmc_vfio_group vfio_groups[VFIO_MAX_GRP];
74 static struct fslmc_vfio_container vfio_containers[VFIO_MAX_CONTAINERS];
75 static int container_device_fd;
76 void *(*rte_mcp_ptr_list);
77 static uint32_t mcp_id;
78
79 static int vfio_connect_container(struct fslmc_vfio_group *vfio_group)
80 {
81         struct fslmc_vfio_container *container;
82         int i, fd, ret;
83
84         /* Try connecting to vfio container if already created */
85         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
86                 container = &vfio_containers[i];
87                 if (!ioctl(vfio_group->fd, VFIO_GROUP_SET_CONTAINER,
88                            &container->fd)) {
89                         FSLMC_VFIO_LOG(INFO, "Container pre-exists with"
90                                     " FD[0x%x] for this group",
91                                     container->fd);
92                         vfio_group->container = container;
93                         return 0;
94                 }
95         }
96
97         /* Opens main vfio file descriptor which represents the "container" */
98         fd = vfio_get_container_fd();
99         if (fd < 0) {
100                 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
101                 return -errno;
102         }
103
104         /* Check whether support for SMMU type IOMMU present or not */
105         if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
106                 /* Connect group to container */
107                 ret = ioctl(vfio_group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
108                 if (ret) {
109                         FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
110                         close(fd);
111                         return -errno;
112                 }
113
114                 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
115                 if (ret) {
116                         FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
117                         close(fd);
118                         return -errno;
119                 }
120         } else {
121                 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
122                 close(fd);
123                 return -EINVAL;
124         }
125
126         container = NULL;
127         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
128                 if (vfio_containers[i].used)
129                         continue;
130                 FSLMC_VFIO_LOG(DEBUG, "Unused container at index %d", i);
131                 container = &vfio_containers[i];
132         }
133         if (!container) {
134                 FSLMC_VFIO_LOG(ERR, "No free container found");
135                 close(fd);
136                 return -ENOMEM;
137         }
138
139         container->used = 1;
140         container->fd = fd;
141         container->group_list[container->index] = vfio_group;
142         vfio_group->container = container;
143         container->index++;
144         return 0;
145 }
146
147 int vfio_dmamap_mem_region(uint64_t vaddr,
148                            uint64_t iova,
149                            uint64_t size)
150 {
151         struct fslmc_vfio_group *group;
152         struct vfio_iommu_type1_dma_map dma_map = {
153                 .argsz = sizeof(dma_map),
154                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
155         };
156
157         dma_map.vaddr = vaddr;
158         dma_map.size = size;
159         dma_map.iova = iova;
160
161         /* SET DMA MAP for IOMMU */
162         group = &vfio_groups[0];
163         if (ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &dma_map)) {
164                 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA (errno = %d)", errno);
165                 return -1;
166         }
167         return 0;
168 }
169
170 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
171 {
172         int64_t v_addr = (int64_t)MAP_FAILED;
173         int32_t ret, mc_fd;
174
175         struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
176         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
177
178         /* getting the mcp object's fd*/
179         mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
180         if (mc_fd < 0) {
181                 FSLMC_VFIO_LOG(ERR, "error in VFIO get device %s fd from group"
182                             " %d", mcp_obj, group->fd);
183                 return v_addr;
184         }
185
186         /* getting device info*/
187         ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
188         if (ret < 0) {
189                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
190                 goto MC_FAILURE;
191         }
192
193         /* getting device region info*/
194         ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
195         if (ret < 0) {
196                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
197                 goto MC_FAILURE;
198         }
199
200         FSLMC_VFIO_LOG(DEBUG, "region offset = %llx  , region size = %llx",
201                      reg_info.offset, reg_info.size);
202
203         v_addr = (uint64_t)mmap(NULL, reg_info.size,
204                 PROT_WRITE | PROT_READ, MAP_SHARED,
205                 mc_fd, reg_info.offset);
206
207 MC_FAILURE:
208         close(mc_fd);
209
210         return v_addr;
211 }
212
213 static inline int
214 dpaa2_compare_dpaa2_dev(const struct rte_dpaa2_device *dev,
215                          const struct rte_dpaa2_device *dev2)
216 {
217         /*not the same family device */
218         if (dev->dev_type != DPAA2_MC_DPNI_DEVID ||
219                         dev->dev_type != DPAA2_MC_DPSECI_DEVID)
220                 return -1;
221
222         if (dev->object_id == dev2->object_id)
223                 return 0;
224         else
225                 return 1;
226 }
227
228 static void
229 fslmc_bus_add_device(struct rte_dpaa2_device *dev)
230 {
231         struct rte_fslmc_device_list *dev_l;
232
233         dev_l = &rte_fslmc_bus.device_list;
234
235         /* device is valid, add in list (sorted) */
236         if (TAILQ_EMPTY(dev_l)) {
237                 TAILQ_INSERT_TAIL(dev_l, dev, next);
238         } else {
239                 struct rte_dpaa2_device *dev2;
240                 int ret;
241
242                 TAILQ_FOREACH(dev2, dev_l, next) {
243                         ret = dpaa2_compare_dpaa2_dev(dev, dev2);
244                         if (ret <= 0)
245                                 continue;
246
247                         TAILQ_INSERT_BEFORE(dev2, dev, next);
248                         return;
249                 }
250
251                 TAILQ_INSERT_TAIL(dev_l, dev, next);
252         }
253 }
254
255 /* Following function shall fetch total available list of MC devices
256  * from VFIO container & populate private list of devices and other
257  * data structures
258  */
259 int fslmc_vfio_process_group(void)
260 {
261         struct fslmc_vfio_device *vdev;
262         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
263         char *temp_obj, *object_type, *mcp_obj, *dev_name;
264         int32_t object_id, i, dev_fd;
265         DIR *d;
266         struct dirent *dir;
267         char path[PATH_MAX];
268         int64_t v_addr;
269         int ndev_count;
270         struct fslmc_vfio_group *group = &vfio_groups[0];
271         static int process_once;
272
273         /* if already done once */
274         if (process_once) {
275                 FSLMC_VFIO_LOG(DEBUG, "Already scanned once - re-scan "
276                             "not supported");
277                 return 0;
278         }
279         process_once = 0;
280
281         sprintf(path, "/sys/kernel/iommu_groups/%d/devices", group->groupid);
282
283         d = opendir(path);
284         if (!d) {
285                 FSLMC_VFIO_LOG(ERR, "Unable to open directory %s", path);
286                 return -1;
287         }
288
289         /*Counting the number of devices in a group and getting the mcp ID*/
290         ndev_count = 0;
291         mcp_obj = NULL;
292         while ((dir = readdir(d)) != NULL) {
293                 if (dir->d_type == DT_LNK) {
294                         ndev_count++;
295                         if (!strncmp("dpmcp", dir->d_name, 5)) {
296                                 if (mcp_obj)
297                                         free(mcp_obj);
298                                 mcp_obj = malloc(sizeof(dir->d_name));
299                                 if (!mcp_obj) {
300                                         FSLMC_VFIO_LOG(ERR, "mcp obj:Unable to"
301                                                     " allocate memory");
302                                         closedir(d);
303                                         return -ENOMEM;
304                                 }
305                                 strcpy(mcp_obj, dir->d_name);
306                                 temp_obj = strtok(dir->d_name, ".");
307                                 temp_obj = strtok(NULL, ".");
308                                 sscanf(temp_obj, "%d", &mcp_id);
309                         }
310                 }
311         }
312         closedir(d);
313         d = NULL;
314         if (!mcp_obj) {
315                 FSLMC_VFIO_LOG(ERR, "DPAA2 MCP Object not Found");
316                 return -ENODEV;
317         }
318         RTE_LOG(INFO, EAL, "fslmc: DPRC contains = %d devices\n", ndev_count);
319
320         /* Allocate the memory depends upon number of objects in a group*/
321         group->vfio_device = (struct fslmc_vfio_device *)malloc(ndev_count *
322                              sizeof(struct fslmc_vfio_device));
323         if (!(group->vfio_device)) {
324                 FSLMC_VFIO_LOG(ERR, "vfio device: Unable to allocate memory\n");
325                 free(mcp_obj);
326                 return -ENOMEM;
327         }
328
329         /* Allocate memory for MC Portal list */
330         rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
331         if (!rte_mcp_ptr_list) {
332                 FSLMC_VFIO_LOG(ERR, "portal list: Unable to allocate memory!");
333                 free(mcp_obj);
334                 goto FAILURE;
335         }
336
337         v_addr = vfio_map_mcp_obj(group, mcp_obj);
338         free(mcp_obj);
339         if (v_addr == (int64_t)MAP_FAILED) {
340                 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)", errno);
341                 goto FAILURE;
342         }
343
344         FSLMC_VFIO_LOG(DEBUG, "DPAA2 MC has VIR_ADD = %ld", v_addr);
345
346         rte_mcp_ptr_list[0] = (void *)v_addr;
347
348         d = opendir(path);
349         if (!d) {
350                 FSLMC_VFIO_LOG(ERR, "Unable to open %s Directory", path);
351                 goto FAILURE;
352         }
353
354         i = 0;
355         FSLMC_VFIO_LOG(DEBUG, "DPAA2 - Parsing devices:");
356         /* Parsing each object and initiating them*/
357         while ((dir = readdir(d)) != NULL) {
358                 if (dir->d_type != DT_LNK)
359                         continue;
360                 if (!strncmp("dprc", dir->d_name, 4) ||
361                     !strncmp("dpmcp", dir->d_name, 5))
362                         continue;
363                 dev_name = malloc(sizeof(dir->d_name));
364                 if (!dev_name) {
365                         FSLMC_VFIO_LOG(ERR, "name: Unable to allocate memory");
366                         goto FAILURE;
367                 }
368                 strcpy(dev_name, dir->d_name);
369                 object_type = strtok(dir->d_name, ".");
370                 temp_obj = strtok(NULL, ".");
371                 sscanf(temp_obj, "%d", &object_id);
372                 FSLMC_VFIO_LOG(DEBUG, " - %s ", dev_name);
373
374                 /* getting the device fd*/
375                 dev_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, dev_name);
376                 if (dev_fd < 0) {
377                         FSLMC_VFIO_LOG(ERR, "VFIO_GROUP_GET_DEVICE_FD error"
378                                     " Device fd: %s, Group: %d",
379                                     dev_name, group->fd);
380                         free(dev_name);
381                         goto FAILURE;
382                 }
383
384                 free(dev_name);
385                 vdev = &group->vfio_device[group->object_index++];
386                 vdev->fd = dev_fd;
387                 vdev->index = i;
388                 i++;
389                 /* Get Device inofrmation */
390                 if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &device_info)) {
391                         FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
392                         goto FAILURE;
393                 }
394                 if (!strcmp(object_type, "dpni") ||
395                     !strcmp(object_type, "dpseci")) {
396                         struct rte_dpaa2_device *dev;
397
398                         dev = malloc(sizeof(struct rte_dpaa2_device));
399                         if (dev == NULL)
400                                 return -1;
401
402                         memset(dev, 0, sizeof(*dev));
403                         /* store hw_id of dpni/dpseci device */
404                         dev->object_id = object_id;
405                         dev->dev_type = (strcmp(object_type, "dpseci")) ?
406                                 DPAA2_MC_DPNI_DEVID : DPAA2_MC_DPSECI_DEVID;
407
408                         FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added [%s-%d]\n",
409                                       object_type, object_id);
410
411                         fslmc_bus_add_device(dev);
412                 }
413         }
414         closedir(d);
415
416         return 0;
417
418 FAILURE:
419         if (d)
420                 closedir(d);
421         if (rte_mcp_ptr_list) {
422                 free(rte_mcp_ptr_list);
423                 rte_mcp_ptr_list = NULL;
424         }
425
426         free(group->vfio_device);
427         group->vfio_device = NULL;
428         return -1;
429 }
430
431 int fslmc_vfio_setup_group(void)
432 {
433         struct fslmc_vfio_group *group = NULL;
434         int groupid;
435         int ret, i;
436         char *container;
437         struct vfio_group_status status = { .argsz = sizeof(status) };
438
439         /* if already done once */
440         if (container_device_fd)
441                 return 0;
442
443         container = getenv("DPRC");
444
445         if (container == NULL) {
446                 FSLMC_VFIO_LOG(ERR, "VFIO container not set in env DPRC");
447                 return -EOPNOTSUPP;
448         }
449
450         /* get group number */
451         ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, container, &groupid);
452         if (ret == 0) {
453                 RTE_LOG(WARNING, EAL, "%s not managed by VFIO, skipping\n",
454                         container);
455                 return -EOPNOTSUPP;
456         }
457
458         /* if negative, something failed */
459         if (ret < 0)
460                 return ret;
461
462         FSLMC_VFIO_LOG(DEBUG, "VFIO iommu group id = %d", groupid);
463
464         /* Check if group already exists */
465         for (i = 0; i < VFIO_MAX_GRP; i++) {
466                 group = &vfio_groups[i];
467                 if (group->groupid == groupid) {
468                         FSLMC_VFIO_LOG(ERR, "groupid already exists %d",
469                                        groupid);
470                         return 0;
471                 }
472         }
473
474         /* get the actual group fd */
475         ret = vfio_get_group_fd(groupid);
476         if (ret < 0)
477                 return ret;
478         group->fd = ret;
479
480         /*
481          * at this point, we know that this group is viable (meaning,
482          * all devices are either bound to VFIO or not bound to anything)
483          */
484
485         ret = ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status);
486         if (ret) {
487                 FSLMC_VFIO_LOG(ERR, " VFIO error getting group status");
488                 close(group->fd);
489                 return ret;
490         }
491
492         if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
493                 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
494                 close(group->fd);
495                 return -EPERM;
496         }
497         /* Since Group is VIABLE, Store the groupid */
498         group->groupid = groupid;
499
500         /* check if group does not have a container yet */
501         if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
502                 /* Now connect this IOMMU group to given container */
503                 ret = vfio_connect_container(group);
504                 if (ret) {
505                         FSLMC_VFIO_LOG(ERR, "VFIO error connecting container"
506                                        " with groupid %d", groupid);
507                         close(group->fd);
508                         return ret;
509                 }
510         }
511
512         /* Get Device information */
513         ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, container);
514         if (ret < 0) {
515                 FSLMC_VFIO_LOG(ERR, "VFIO error getting device %s fd from"
516                                " group  %d", container, group->groupid);
517                 return ret;
518         }
519         container_device_fd = ret;
520         FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
521                      container_device_fd);
522
523         return 0;
524 }