bus/fslmc: refactor scan and probe functions
[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 2016 NXP.
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/mman.h>
44 #include <sys/vfs.h>
45 #include <libgen.h>
46 #include <dirent.h>
47 #include <sys/eventfd.h>
48
49 #include <rte_mbuf.h>
50 #include <rte_ethdev.h>
51 #include <rte_malloc.h>
52 #include <rte_memcpy.h>
53 #include <rte_string_fns.h>
54 #include <rte_cycles.h>
55 #include <rte_kvargs.h>
56 #include <rte_dev.h>
57 #include <rte_bus.h>
58
59 #include "rte_fslmc.h"
60 #include "fslmc_vfio.h"
61
62 #include "portal/dpaa2_hw_pvt.h"
63 #include "portal/dpaa2_hw_dpio.h"
64
65 #define FSLMC_VFIO_LOG(level, fmt, args...) \
66         RTE_LOG(level, EAL, fmt "\n", ##args)
67
68 /** Pathname of FSL-MC devices directory. */
69 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
70
71 #define FSLMC_CONTAINER_MAX_LEN 8 /**< Of the format dprc.XX */
72
73 /* Number of VFIO containers & groups with in */
74 static struct fslmc_vfio_group vfio_group;
75 static struct fslmc_vfio_container vfio_container;
76 static int container_device_fd;
77 static char *g_container;
78 static uint32_t *msi_intr_vaddr;
79 void *(*rte_mcp_ptr_list);
80 static int is_dma_done;
81
82 static struct rte_dpaa2_object_list dpaa2_obj_list =
83         TAILQ_HEAD_INITIALIZER(dpaa2_obj_list);
84
85 /*register a fslmc bus based dpaa2 driver */
86 void
87 rte_fslmc_object_register(struct rte_dpaa2_object *object)
88 {
89         RTE_VERIFY(object);
90
91         TAILQ_INSERT_TAIL(&dpaa2_obj_list, object, next);
92 }
93
94 int
95 fslmc_get_container_group(int *groupid)
96 {
97         int ret;
98         char *container;
99
100         if (!g_container) {
101                 container = getenv("DPRC");
102                 if (container == NULL) {
103                         RTE_LOG(WARNING, EAL, "DPAA2: DPRC not available\n");
104                         return -EINVAL;
105                 }
106
107                 if (strlen(container) >= FSLMC_CONTAINER_MAX_LEN) {
108                         FSLMC_VFIO_LOG(ERR, "Invalid container name: %s\n",
109                                        container);
110                         return -1;
111                 }
112
113                 g_container = strdup(container);
114                 if (!g_container) {
115                         FSLMC_VFIO_LOG(ERR, "Out of memory.");
116                         return -ENOMEM;
117                 }
118         }
119
120         /* get group number */
121         ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, g_container, groupid);
122         if (ret <= 0) {
123                 FSLMC_VFIO_LOG(ERR, "Unable to find %s IOMMU group",
124                                g_container);
125                 return -1;
126         }
127
128         FSLMC_VFIO_LOG(DEBUG, "Container: %s has VFIO iommu group id = %d",
129                        g_container, *groupid);
130
131         return 0;
132 }
133
134 static int
135 vfio_connect_container(void)
136 {
137         int fd, ret;
138
139         if (vfio_container.used) {
140                 FSLMC_VFIO_LOG(DEBUG, "No container available.");
141                 return -1;
142         }
143
144         /* Try connecting to vfio container if already created */
145         if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
146                 &vfio_container.fd)) {
147                 FSLMC_VFIO_LOG(INFO,
148                     "Container pre-exists with FD[0x%x] for this group",
149                     vfio_container.fd);
150                 vfio_group.container = &vfio_container;
151                 return 0;
152         }
153
154         /* Opens main vfio file descriptor which represents the "container" */
155         fd = vfio_get_container_fd();
156         if (fd < 0) {
157                 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
158                 return -errno;
159         }
160
161         /* Check whether support for SMMU type IOMMU present or not */
162         if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
163                 /* Connect group to container */
164                 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
165                 if (ret) {
166                         FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
167                         close(fd);
168                         return -errno;
169                 }
170
171                 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
172                 if (ret) {
173                         FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
174                         close(fd);
175                         return -errno;
176                 }
177         } else {
178                 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
179                 close(fd);
180                 return -EINVAL;
181         }
182
183         vfio_container.used = 1;
184         vfio_container.fd = fd;
185         vfio_container.group = &vfio_group;
186         vfio_group.container = &vfio_container;
187
188         return 0;
189 }
190
191 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
192 {
193         int ret;
194         unsigned long *vaddr = NULL;
195         struct vfio_iommu_type1_dma_map map = {
196                 .argsz = sizeof(map),
197                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
198                 .vaddr = 0x6030000,
199                 .iova = 0x6030000,
200                 .size = 0x1000,
201         };
202
203         vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
204                 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
205         if (vaddr == MAP_FAILED) {
206                 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
207                 return -errno;
208         }
209
210         msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
211         map.vaddr = (unsigned long)vaddr;
212         ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
213         if (ret == 0)
214                 return 0;
215
216         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
217         return -errno;
218 }
219
220 int rte_fslmc_vfio_dmamap(void)
221 {
222         int ret;
223         struct fslmc_vfio_group *group;
224         struct vfio_iommu_type1_dma_map dma_map = {
225                 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
226                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
227         };
228
229         int i;
230         const struct rte_memseg *memseg;
231
232         if (is_dma_done)
233                 return 0;
234
235         memseg = rte_eal_get_physmem_layout();
236         if (memseg == NULL) {
237                 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
238                 return -ENODEV;
239         }
240
241         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
242                 if (memseg[i].addr == NULL && memseg[i].len == 0) {
243                         FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
244                         break;
245                 }
246
247                 dma_map.size = memseg[i].len;
248                 dma_map.vaddr = memseg[i].addr_64;
249 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
250                 dma_map.iova = memseg[i].phys_addr;
251 #else
252                 dma_map.iova = dma_map.vaddr;
253 #endif
254
255                 /* SET DMA MAP for IOMMU */
256                 group = &vfio_group;
257
258                 if (!group->container) {
259                         FSLMC_VFIO_LOG(ERR, "Container is not connected ");
260                         return -1;
261                 }
262
263                 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
264                              dma_map.vaddr);
265                 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
266                 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
267                             &dma_map);
268                 if (ret) {
269                         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
270                                        errno);
271                         return ret;
272                 }
273         }
274
275         /* Verifying that at least single segment is available */
276         if (i <= 0) {
277                 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
278                 return -1;
279         }
280
281         /* TODO - This is a W.A. as VFIO currently does not add the mapping of
282          * the interrupt region to SMMU. This should be removed once the
283          * support is added in the Kernel.
284          */
285         vfio_map_irq_region(group);
286
287         is_dma_done = 1;
288
289         return 0;
290 }
291
292 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
293 {
294         int64_t v_addr = (int64_t)MAP_FAILED;
295         int32_t ret, mc_fd;
296
297         struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
298         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
299
300         /* getting the mcp object's fd*/
301         mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
302         if (mc_fd < 0) {
303                 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
304                                mcp_obj, group->fd);
305                 return v_addr;
306         }
307
308         /* getting device info*/
309         ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
310         if (ret < 0) {
311                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
312                 goto MC_FAILURE;
313         }
314
315         /* getting device region info*/
316         ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
317         if (ret < 0) {
318                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
319                 goto MC_FAILURE;
320         }
321
322         FSLMC_VFIO_LOG(DEBUG, "region offset = %llx  , region size = %llx",
323                        reg_info.offset, reg_info.size);
324
325         v_addr = (uint64_t)mmap(NULL, reg_info.size,
326                 PROT_WRITE | PROT_READ, MAP_SHARED,
327                 mc_fd, reg_info.offset);
328
329 MC_FAILURE:
330         close(mc_fd);
331
332         return v_addr;
333 }
334
335 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
336
337 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle,
338                           uint32_t index)
339 {
340         struct vfio_irq_set *irq_set;
341         char irq_set_buf[IRQ_SET_BUF_LEN];
342         int *fd_ptr, fd, ret;
343
344         /* Prepare vfio_irq_set structure and SET the IRQ in VFIO */
345         /* Give the eventfd to VFIO */
346         fd = eventfd(0, 0);
347         irq_set = (struct vfio_irq_set *)irq_set_buf;
348         irq_set->argsz = sizeof(irq_set_buf);
349         irq_set->count = 1;
350         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
351                          VFIO_IRQ_SET_ACTION_TRIGGER;
352         irq_set->index = index;
353         irq_set->start = 0;
354         fd_ptr = (int *)&irq_set->data;
355         *fd_ptr = fd;
356
357         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
358         if (ret < 0) {
359                 FSLMC_VFIO_LOG(ERR, "Unable to set IRQ in VFIO, ret: %d\n",
360                                ret);
361                 return -1;
362         }
363
364         /* Set the FD and update the flags */
365         intr_handle->fd = fd;
366         return 0;
367 }
368
369 /*
370  * fslmc_process_iodevices for processing only IO (ETH, CRYPTO, and possibly
371  * EVENT) devices.
372  */
373 static int
374 fslmc_process_iodevices(struct rte_dpaa2_device *dev)
375 {
376         int dev_fd;
377         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
378         struct rte_dpaa2_object *object = NULL;
379
380         dev_fd = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD,
381                        dev->device.name);
382         if (dev_fd <= 0) {
383                 FSLMC_VFIO_LOG(ERR, "Unable to obtain device FD for device:%s",
384                                dev->device.name);
385                 return -1;
386         }
387
388         if (ioctl(dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
389                 FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
390                 return -1;
391         }
392
393         switch (dev->dev_type) {
394         case DPAA2_CON:
395         case DPAA2_IO:
396         case DPAA2_CI:
397         case DPAA2_BPOOL:
398                 TAILQ_FOREACH(object, &dpaa2_obj_list, next) {
399                         if (dev->dev_type == object->dev_type)
400                                 object->create(dev_fd, &device_info,
401                                                dev->object_id);
402                         else
403                                 continue;
404                 }
405                 break;
406         default:
407                 break;
408         }
409
410         FSLMC_VFIO_LOG(DEBUG, "Device (%s) abstracted from VFIO",
411                        dev->device.name);
412         return 0;
413 }
414
415 static int
416 fslmc_process_mcp(struct rte_dpaa2_device *dev)
417 {
418         int64_t v_addr;
419         char *dev_name;
420
421         rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
422         if (!rte_mcp_ptr_list) {
423                 FSLMC_VFIO_LOG(ERR, "Out of memory");
424                 return -ENOMEM;
425         }
426
427         dev_name = strdup(dev->device.name);
428         if (!dev_name) {
429                 FSLMC_VFIO_LOG(ERR, "Out of memory.");
430                 free(rte_mcp_ptr_list);
431                 rte_mcp_ptr_list = NULL;
432                 return -ENOMEM;
433         }
434
435         v_addr = vfio_map_mcp_obj(&vfio_group, dev_name);
436         if (v_addr == (int64_t)MAP_FAILED) {
437                 FSLMC_VFIO_LOG(ERR, "Error mapping region  (errno = %d)",
438                                errno);
439                 free(rte_mcp_ptr_list);
440                 rte_mcp_ptr_list = NULL;
441                 return -1;
442         }
443
444         rte_mcp_ptr_list[0] = (void *)v_addr;
445
446         return 0;
447 }
448
449 int
450 fslmc_vfio_process_group(void)
451 {
452         int ret;
453         int found_mportal = 0;
454         struct rte_dpaa2_device *dev, *dev_temp;
455
456         /* Search the MCP as that should be initialized first. */
457         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
458                 if (dev->dev_type == DPAA2_MPORTAL) {
459                         ret = fslmc_process_mcp(dev);
460                         if (ret) {
461                                 FSLMC_VFIO_LOG(DEBUG, "Unable to map Portal.");
462                                 return -1;
463                         }
464                         if (!found_mportal)
465                                 found_mportal = 1;
466
467                         TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
468                         free(dev);
469                         dev = NULL;
470                         /* Ideally there is only a single dpmcp, but in case
471                          * multiple exists, looping on remaining devices.
472                          */
473                 }
474         }
475
476         /* Cannot continue if there is not even a single mportal */
477         if (!found_mportal) {
478                 FSLMC_VFIO_LOG(DEBUG,
479                                "No MC Portal device found. Not continuing.");
480                 return -1;
481         }
482
483         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
484                 if (!dev)
485                         break;
486
487                 switch (dev->dev_type) {
488                 case DPAA2_ETH:
489                 case DPAA2_CRYPTO:
490                         ret = fslmc_process_iodevices(dev);
491                         if (ret) {
492                                 FSLMC_VFIO_LOG(DEBUG,
493                                                "Dev (%s) init failed.",
494                                                dev->device.name);
495                                 return ret;
496                         }
497                         break;
498                 case DPAA2_CON:
499                 case DPAA2_IO:
500                 case DPAA2_CI:
501                 case DPAA2_BPOOL:
502                         /* Call the object creation routine and remove the
503                          * device entry from device list
504                          */
505                         ret = fslmc_process_iodevices(dev);
506                         if (ret) {
507                                 FSLMC_VFIO_LOG(DEBUG,
508                                                "Dev (%s) init failed.",
509                                                dev->device.name);
510                                 return -1;
511                         }
512
513                         /* This device is not required to be in the DPDK
514                          * exposed device list.
515                          */
516                         TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
517                         free(dev);
518                         dev = NULL;
519                         break;
520                 case DPAA2_UNKNOWN:
521                 default:
522                         /* Unknown - ignore */
523                         FSLMC_VFIO_LOG(DEBUG, "Found unknown device (%s).",
524                                        dev->device.name);
525                         TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
526                         free(dev);
527                         dev = NULL;
528                 }
529         }
530
531         return 0;
532 }
533
534 int
535 fslmc_vfio_setup_group(void)
536 {
537         int groupid;
538         int ret;
539         struct vfio_group_status status = { .argsz = sizeof(status) };
540
541         /* if already done once */
542         if (container_device_fd)
543                 return 0;
544
545         ret = fslmc_get_container_group(&groupid);
546         if (ret)
547                 return ret;
548
549         /* In case this group was already opened, continue without any
550          * processing.
551          */
552         if (vfio_group.groupid == groupid) {
553                 FSLMC_VFIO_LOG(ERR, "groupid already exists %d", groupid);
554                 return 0;
555         }
556
557         /* Get the actual group fd */
558         ret = vfio_get_group_fd(groupid);
559         if (ret < 0)
560                 return ret;
561         vfio_group.fd = ret;
562
563         /* Check group viability */
564         ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_STATUS, &status);
565         if (ret) {
566                 FSLMC_VFIO_LOG(ERR, "VFIO error getting group status");
567                 close(vfio_group.fd);
568                 return ret;
569         }
570
571         if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
572                 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
573                 close(vfio_group.fd);
574                 return -EPERM;
575         }
576         /* Since Group is VIABLE, Store the groupid */
577         vfio_group.groupid = groupid;
578
579         /* check if group does not have a container yet */
580         if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
581                 /* Now connect this IOMMU group to given container */
582                 ret = vfio_connect_container();
583                 if (ret) {
584                         FSLMC_VFIO_LOG(ERR,
585                                 "Error connecting container with groupid %d",
586                                 groupid);
587                         close(vfio_group.fd);
588                         return ret;
589                 }
590         }
591
592         /* Get Device information */
593         ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD, g_container);
594         if (ret < 0) {
595                 FSLMC_VFIO_LOG(ERR, "Error getting device %s fd from group %d",
596                                g_container, vfio_group.groupid);
597                 close(vfio_group.fd);
598                 return ret;
599         }
600         container_device_fd = ret;
601         FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
602                        container_device_fd);
603
604         return 0;
605 }