bus/fslmc: add dpio portal driver
[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 #include "portal/dpaa2_hw_pvt.h"
65 #include "portal/dpaa2_hw_dpio.h"
66
67 #define VFIO_MAX_CONTAINERS     1
68
69 #define FSLMC_VFIO_LOG(level, fmt, args...) \
70         RTE_LOG(level, EAL, "%s(): " fmt "\n", __func__, ##args)
71
72 /** Pathname of FSL-MC devices directory. */
73 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
74
75 /* Number of VFIO containers & groups with in */
76 static struct fslmc_vfio_group vfio_groups[VFIO_MAX_GRP];
77 static struct fslmc_vfio_container vfio_containers[VFIO_MAX_CONTAINERS];
78 static int container_device_fd;
79 void *(*rte_mcp_ptr_list);
80 static uint32_t mcp_id;
81
82 static int vfio_connect_container(struct fslmc_vfio_group *vfio_group)
83 {
84         struct fslmc_vfio_container *container;
85         int i, fd, ret;
86
87         /* Try connecting to vfio container if already created */
88         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
89                 container = &vfio_containers[i];
90                 if (!ioctl(vfio_group->fd, VFIO_GROUP_SET_CONTAINER,
91                            &container->fd)) {
92                         FSLMC_VFIO_LOG(INFO, "Container pre-exists with"
93                                     " FD[0x%x] for this group",
94                                     container->fd);
95                         vfio_group->container = container;
96                         return 0;
97                 }
98         }
99
100         /* Opens main vfio file descriptor which represents the "container" */
101         fd = vfio_get_container_fd();
102         if (fd < 0) {
103                 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
104                 return -errno;
105         }
106
107         /* Check whether support for SMMU type IOMMU present or not */
108         if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
109                 /* Connect group to container */
110                 ret = ioctl(vfio_group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
111                 if (ret) {
112                         FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
113                         close(fd);
114                         return -errno;
115                 }
116
117                 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
118                 if (ret) {
119                         FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
120                         close(fd);
121                         return -errno;
122                 }
123         } else {
124                 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
125                 close(fd);
126                 return -EINVAL;
127         }
128
129         container = NULL;
130         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
131                 if (vfio_containers[i].used)
132                         continue;
133                 FSLMC_VFIO_LOG(DEBUG, "Unused container at index %d", i);
134                 container = &vfio_containers[i];
135         }
136         if (!container) {
137                 FSLMC_VFIO_LOG(ERR, "No free container found");
138                 close(fd);
139                 return -ENOMEM;
140         }
141
142         container->used = 1;
143         container->fd = fd;
144         container->group_list[container->index] = vfio_group;
145         vfio_group->container = container;
146         container->index++;
147         return 0;
148 }
149
150 int vfio_dmamap_mem_region(uint64_t vaddr,
151                            uint64_t iova,
152                            uint64_t size)
153 {
154         struct fslmc_vfio_group *group;
155         struct vfio_iommu_type1_dma_map dma_map = {
156                 .argsz = sizeof(dma_map),
157                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
158         };
159
160         dma_map.vaddr = vaddr;
161         dma_map.size = size;
162         dma_map.iova = iova;
163
164         /* SET DMA MAP for IOMMU */
165         group = &vfio_groups[0];
166         if (ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &dma_map)) {
167                 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA (errno = %d)", errno);
168                 return -1;
169         }
170         return 0;
171 }
172
173 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
174 {
175         int64_t v_addr = (int64_t)MAP_FAILED;
176         int32_t ret, mc_fd;
177
178         struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
179         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
180
181         /* getting the mcp object's fd*/
182         mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
183         if (mc_fd < 0) {
184                 FSLMC_VFIO_LOG(ERR, "error in VFIO get device %s fd from group"
185                             " %d", mcp_obj, group->fd);
186                 return v_addr;
187         }
188
189         /* getting device info*/
190         ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
191         if (ret < 0) {
192                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
193                 goto MC_FAILURE;
194         }
195
196         /* getting device region info*/
197         ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
198         if (ret < 0) {
199                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
200                 goto MC_FAILURE;
201         }
202
203         FSLMC_VFIO_LOG(DEBUG, "region offset = %llx  , region size = %llx",
204                      reg_info.offset, reg_info.size);
205
206         v_addr = (uint64_t)mmap(NULL, reg_info.size,
207                 PROT_WRITE | PROT_READ, MAP_SHARED,
208                 mc_fd, reg_info.offset);
209
210 MC_FAILURE:
211         close(mc_fd);
212
213         return v_addr;
214 }
215
216 static inline int
217 dpaa2_compare_dpaa2_dev(const struct rte_dpaa2_device *dev,
218                          const struct rte_dpaa2_device *dev2)
219 {
220         /*not the same family device */
221         if (dev->dev_type != DPAA2_MC_DPNI_DEVID ||
222                         dev->dev_type != DPAA2_MC_DPSECI_DEVID)
223                 return -1;
224
225         if (dev->object_id == dev2->object_id)
226                 return 0;
227         else
228                 return 1;
229 }
230
231 static void
232 fslmc_bus_add_device(struct rte_dpaa2_device *dev)
233 {
234         struct rte_fslmc_device_list *dev_l;
235
236         dev_l = &rte_fslmc_bus.device_list;
237
238         /* device is valid, add in list (sorted) */
239         if (TAILQ_EMPTY(dev_l)) {
240                 TAILQ_INSERT_TAIL(dev_l, dev, next);
241         } else {
242                 struct rte_dpaa2_device *dev2;
243                 int ret;
244
245                 TAILQ_FOREACH(dev2, dev_l, next) {
246                         ret = dpaa2_compare_dpaa2_dev(dev, dev2);
247                         if (ret <= 0)
248                                 continue;
249
250                         TAILQ_INSERT_BEFORE(dev2, dev, next);
251                         return;
252                 }
253
254                 TAILQ_INSERT_TAIL(dev_l, dev, next);
255         }
256 }
257
258 /* Following function shall fetch total available list of MC devices
259  * from VFIO container & populate private list of devices and other
260  * data structures
261  */
262 int fslmc_vfio_process_group(void)
263 {
264         struct fslmc_vfio_device *vdev;
265         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
266         char *temp_obj, *object_type, *mcp_obj, *dev_name;
267         int32_t object_id, i, dev_fd, ret;
268         DIR *d;
269         struct dirent *dir;
270         char path[PATH_MAX];
271         int64_t v_addr;
272         int ndev_count;
273         int dpio_count = 0;
274         struct fslmc_vfio_group *group = &vfio_groups[0];
275         static int process_once;
276
277         /* if already done once */
278         if (process_once) {
279                 FSLMC_VFIO_LOG(DEBUG, "Already scanned once - re-scan "
280                             "not supported");
281                 return 0;
282         }
283         process_once = 0;
284
285         sprintf(path, "/sys/kernel/iommu_groups/%d/devices", group->groupid);
286
287         d = opendir(path);
288         if (!d) {
289                 FSLMC_VFIO_LOG(ERR, "Unable to open directory %s", path);
290                 return -1;
291         }
292
293         /*Counting the number of devices in a group and getting the mcp ID*/
294         ndev_count = 0;
295         mcp_obj = NULL;
296         while ((dir = readdir(d)) != NULL) {
297                 if (dir->d_type == DT_LNK) {
298                         ndev_count++;
299                         if (!strncmp("dpmcp", dir->d_name, 5)) {
300                                 if (mcp_obj)
301                                         free(mcp_obj);
302                                 mcp_obj = malloc(sizeof(dir->d_name));
303                                 if (!mcp_obj) {
304                                         FSLMC_VFIO_LOG(ERR, "mcp obj:Unable to"
305                                                     " allocate memory");
306                                         closedir(d);
307                                         return -ENOMEM;
308                                 }
309                                 strcpy(mcp_obj, dir->d_name);
310                                 temp_obj = strtok(dir->d_name, ".");
311                                 temp_obj = strtok(NULL, ".");
312                                 sscanf(temp_obj, "%d", &mcp_id);
313                         }
314                 }
315         }
316         closedir(d);
317         d = NULL;
318         if (!mcp_obj) {
319                 FSLMC_VFIO_LOG(ERR, "DPAA2 MCP Object not Found");
320                 return -ENODEV;
321         }
322         RTE_LOG(INFO, EAL, "fslmc: DPRC contains = %d devices\n", ndev_count);
323
324         /* Allocate the memory depends upon number of objects in a group*/
325         group->vfio_device = (struct fslmc_vfio_device *)malloc(ndev_count *
326                              sizeof(struct fslmc_vfio_device));
327         if (!(group->vfio_device)) {
328                 FSLMC_VFIO_LOG(ERR, "vfio device: Unable to allocate memory\n");
329                 free(mcp_obj);
330                 return -ENOMEM;
331         }
332
333         /* Allocate memory for MC Portal list */
334         rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
335         if (!rte_mcp_ptr_list) {
336                 FSLMC_VFIO_LOG(ERR, "portal list: Unable to allocate memory!");
337                 free(mcp_obj);
338                 goto FAILURE;
339         }
340
341         v_addr = vfio_map_mcp_obj(group, mcp_obj);
342         free(mcp_obj);
343         if (v_addr == (int64_t)MAP_FAILED) {
344                 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)", errno);
345                 goto FAILURE;
346         }
347
348         FSLMC_VFIO_LOG(DEBUG, "DPAA2 MC has VIR_ADD = %ld", v_addr);
349
350         rte_mcp_ptr_list[0] = (void *)v_addr;
351
352         d = opendir(path);
353         if (!d) {
354                 FSLMC_VFIO_LOG(ERR, "Unable to open %s Directory", path);
355                 goto FAILURE;
356         }
357
358         i = 0;
359         FSLMC_VFIO_LOG(DEBUG, "DPAA2 - Parsing devices:");
360         /* Parsing each object and initiating them*/
361         while ((dir = readdir(d)) != NULL) {
362                 if (dir->d_type != DT_LNK)
363                         continue;
364                 if (!strncmp("dprc", dir->d_name, 4) ||
365                     !strncmp("dpmcp", dir->d_name, 5))
366                         continue;
367                 dev_name = malloc(sizeof(dir->d_name));
368                 if (!dev_name) {
369                         FSLMC_VFIO_LOG(ERR, "name: Unable to allocate memory");
370                         goto FAILURE;
371                 }
372                 strcpy(dev_name, dir->d_name);
373                 object_type = strtok(dir->d_name, ".");
374                 temp_obj = strtok(NULL, ".");
375                 sscanf(temp_obj, "%d", &object_id);
376                 FSLMC_VFIO_LOG(DEBUG, " - %s ", dev_name);
377
378                 /* getting the device fd*/
379                 dev_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, dev_name);
380                 if (dev_fd < 0) {
381                         FSLMC_VFIO_LOG(ERR, "VFIO_GROUP_GET_DEVICE_FD error"
382                                     " Device fd: %s, Group: %d",
383                                     dev_name, group->fd);
384                         free(dev_name);
385                         goto FAILURE;
386                 }
387
388                 free(dev_name);
389                 vdev = &group->vfio_device[group->object_index++];
390                 vdev->fd = dev_fd;
391                 vdev->index = i;
392                 i++;
393                 /* Get Device inofrmation */
394                 if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &device_info)) {
395                         FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
396                         goto FAILURE;
397                 }
398                 if (!strcmp(object_type, "dpni") ||
399                     !strcmp(object_type, "dpseci")) {
400                         struct rte_dpaa2_device *dev;
401
402                         dev = malloc(sizeof(struct rte_dpaa2_device));
403                         if (dev == NULL)
404                                 return -1;
405
406                         memset(dev, 0, sizeof(*dev));
407                         /* store hw_id of dpni/dpseci device */
408                         dev->object_id = object_id;
409                         dev->dev_type = (strcmp(object_type, "dpseci")) ?
410                                 DPAA2_MC_DPNI_DEVID : DPAA2_MC_DPSECI_DEVID;
411
412                         FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added [%s-%d]\n",
413                                       object_type, object_id);
414
415                         fslmc_bus_add_device(dev);
416                 }
417                 if (!strcmp(object_type, "dpio")) {
418                         ret = dpaa2_create_dpio_device(vdev,
419                                                        &device_info,
420                                                        object_id);
421                         if (!ret)
422                                 dpio_count++;
423                 }
424         }
425         closedir(d);
426
427         ret = dpaa2_affine_qbman_swp();
428         if (ret)
429                 FSLMC_VFIO_LOG(DEBUG, "Error in affining qbman swp %d", ret);
430
431         return 0;
432
433 FAILURE:
434         if (d)
435                 closedir(d);
436         if (rte_mcp_ptr_list) {
437                 free(rte_mcp_ptr_list);
438                 rte_mcp_ptr_list = NULL;
439         }
440
441         free(group->vfio_device);
442         group->vfio_device = NULL;
443         return -1;
444 }
445
446 int fslmc_vfio_setup_group(void)
447 {
448         struct fslmc_vfio_group *group = NULL;
449         int groupid;
450         int ret, i;
451         char *container;
452         struct vfio_group_status status = { .argsz = sizeof(status) };
453
454         /* if already done once */
455         if (container_device_fd)
456                 return 0;
457
458         container = getenv("DPRC");
459
460         if (container == NULL) {
461                 FSLMC_VFIO_LOG(ERR, "VFIO container not set in env DPRC");
462                 return -EOPNOTSUPP;
463         }
464
465         /* get group number */
466         ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, container, &groupid);
467         if (ret == 0) {
468                 RTE_LOG(WARNING, EAL, "%s not managed by VFIO, skipping\n",
469                         container);
470                 return -EOPNOTSUPP;
471         }
472
473         /* if negative, something failed */
474         if (ret < 0)
475                 return ret;
476
477         FSLMC_VFIO_LOG(DEBUG, "VFIO iommu group id = %d", groupid);
478
479         /* Check if group already exists */
480         for (i = 0; i < VFIO_MAX_GRP; i++) {
481                 group = &vfio_groups[i];
482                 if (group->groupid == groupid) {
483                         FSLMC_VFIO_LOG(ERR, "groupid already exists %d",
484                                        groupid);
485                         return 0;
486                 }
487         }
488
489         /* get the actual group fd */
490         ret = vfio_get_group_fd(groupid);
491         if (ret < 0)
492                 return ret;
493         group->fd = ret;
494
495         /*
496          * at this point, we know that this group is viable (meaning,
497          * all devices are either bound to VFIO or not bound to anything)
498          */
499
500         ret = ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status);
501         if (ret) {
502                 FSLMC_VFIO_LOG(ERR, " VFIO error getting group status");
503                 close(group->fd);
504                 return ret;
505         }
506
507         if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
508                 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
509                 close(group->fd);
510                 return -EPERM;
511         }
512         /* Since Group is VIABLE, Store the groupid */
513         group->groupid = groupid;
514
515         /* check if group does not have a container yet */
516         if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
517                 /* Now connect this IOMMU group to given container */
518                 ret = vfio_connect_container(group);
519                 if (ret) {
520                         FSLMC_VFIO_LOG(ERR, "VFIO error connecting container"
521                                        " with groupid %d", groupid);
522                         close(group->fd);
523                         return ret;
524                 }
525         }
526
527         /* Get Device information */
528         ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, container);
529         if (ret < 0) {
530                 FSLMC_VFIO_LOG(ERR, "VFIO error getting device %s fd from"
531                                " group  %d", container, group->groupid);
532                 return ret;
533         }
534         container_device_fd = ret;
535         FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
536                      container_device_fd);
537
538         return 0;
539 }