vfio: avoid enabling while the module is not loaded
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   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 Intel Corporation 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 <string.h>
35 #include <fcntl.h>
36 #include <linux/pci_regs.h>
37 #include <sys/eventfd.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
41
42 #include <rte_log.h>
43 #include <rte_pci.h>
44 #include <rte_tailq.h>
45 #include <rte_eal_memconfig.h>
46 #include <rte_malloc.h>
47 #include <eal_private.h>
48
49 #include "eal_filesystem.h"
50 #include "eal_pci_init.h"
51 #include "eal_vfio.h"
52
53 /**
54  * @file
55  * PCI probing under linux (VFIO version)
56  *
57  * This code tries to determine if the PCI device is bound to VFIO driver,
58  * and initialize it (map BARs, set up interrupts) if that's the case.
59  *
60  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
61  */
62
63 #ifdef VFIO_PRESENT
64
65 #define VFIO_DIR "/dev/vfio"
66 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
67 #define VFIO_GROUP_FMT "/dev/vfio/%u"
68 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
69
70 /* per-process VFIO config */
71 static struct vfio_config vfio_cfg;
72
73 /* get PCI BAR number where MSI-X interrupts are */
74 static int
75 pci_vfio_get_msix_bar(int fd, int *msix_bar)
76 {
77         int ret;
78         uint32_t reg;
79         uint8_t cap_id, cap_offset;
80
81         /* read PCI capability pointer from config space */
82         ret = pread64(fd, &reg, sizeof(reg),
83                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
84                         PCI_CAPABILITY_LIST);
85         if (ret != sizeof(reg)) {
86                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
87                                 "config space!\n");
88                 return -1;
89         }
90
91         /* we need first byte */
92         cap_offset = reg & 0xFF;
93
94         while (cap_offset) {
95
96                 /* read PCI capability ID */
97                 ret = pread64(fd, &reg, sizeof(reg),
98                                 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
99                                 cap_offset);
100                 if (ret != sizeof(reg)) {
101                         RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
102                                         "config space!\n");
103                         return -1;
104                 }
105
106                 /* we need first byte */
107                 cap_id = reg & 0xFF;
108
109                 /* if we haven't reached MSI-X, check next capability */
110                 if (cap_id != PCI_CAP_ID_MSIX) {
111                         ret = pread64(fd, &reg, sizeof(reg),
112                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
113                                         cap_offset);
114                         if (ret != sizeof(reg)) {
115                                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
116                                                 "config space!\n");
117                                 return -1;
118                         }
119
120                         /* we need second byte */
121                         cap_offset = (reg & 0xFF00) >> 8;
122
123                         continue;
124                 }
125                 /* else, read table offset */
126                 else {
127                         /* table offset resides in the next 4 bytes */
128                         ret = pread64(fd, &reg, sizeof(reg),
129                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
130                                         cap_offset + 4);
131                         if (ret != sizeof(reg)) {
132                                 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
133                                                 "space!\n");
134                                 return -1;
135                         }
136
137                         *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
138
139                         return 0;
140                 }
141         }
142         return 0;
143 }
144
145 /* set PCI bus mastering */
146 static int
147 pci_vfio_set_bus_master(int dev_fd)
148 {
149         uint16_t reg;
150         int ret;
151
152         ret = pread64(dev_fd, &reg, sizeof(reg),
153                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
154                         PCI_COMMAND);
155         if (ret != sizeof(reg)) {
156                 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
157                 return -1;
158         }
159
160         /* set the master bit */
161         reg |= PCI_COMMAND_MASTER;
162
163         ret = pwrite64(dev_fd, &reg, sizeof(reg),
164                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
165                         PCI_COMMAND);
166
167         if (ret != sizeof(reg)) {
168                 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
169                 return -1;
170         }
171
172         return 0;
173 }
174
175 /* set up DMA mappings */
176 static int
177 pci_vfio_setup_dma_maps(int vfio_container_fd)
178 {
179         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
180         int i, ret;
181
182         ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
183                         VFIO_TYPE1_IOMMU);
184         if (ret) {
185                 RTE_LOG(ERR, EAL, "  cannot set IOMMU type, "
186                                 "error %i (%s)\n", errno, strerror(errno));
187                 return -1;
188         }
189
190         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
191         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
192                 struct vfio_iommu_type1_dma_map dma_map;
193
194                 if (ms[i].addr == NULL)
195                         break;
196
197                 memset(&dma_map, 0, sizeof(dma_map));
198                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
199                 dma_map.vaddr = ms[i].addr_64;
200                 dma_map.size = ms[i].len;
201                 dma_map.iova = ms[i].phys_addr;
202                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
203
204                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
205
206                 if (ret) {
207                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
208                                         "error %i (%s)\n", errno, strerror(errno));
209                         return -1;
210                 }
211         }
212
213         return 0;
214 }
215
216 /* set up interrupt support (but not enable interrupts) */
217 static int
218 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
219 {
220         int i, ret, intr_idx;
221
222         /* default to invalid index */
223         intr_idx = VFIO_PCI_NUM_IRQS;
224
225         /* get interrupt type from internal config (MSI-X by default, can be
226          * overriden from the command line
227          */
228         switch (internal_config.vfio_intr_mode) {
229         case RTE_INTR_MODE_MSIX:
230                 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
231                 break;
232         case RTE_INTR_MODE_MSI:
233                 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
234                 break;
235         case RTE_INTR_MODE_LEGACY:
236                 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
237                 break;
238         /* don't do anything if we want to automatically determine interrupt type */
239         case RTE_INTR_MODE_NONE:
240                 break;
241         default:
242                 RTE_LOG(ERR, EAL, "  unknown default interrupt type!\n");
243                 return -1;
244         }
245
246         /* start from MSI-X interrupt type */
247         for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
248                 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
249                 int fd = -1;
250
251                 /* skip interrupt modes we don't want */
252                 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
253                                 i != intr_idx)
254                         continue;
255
256                 irq.index = i;
257
258                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
259                 if (ret < 0) {
260                         RTE_LOG(ERR, EAL, "  cannot get IRQ info, "
261                                         "error %i (%s)\n", errno, strerror(errno));
262                         return -1;
263                 }
264
265                 /* if this vector cannot be used with eventfd, fail if we explicitly
266                  * specified interrupt type, otherwise continue */
267                 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
268                         if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
269                                 RTE_LOG(ERR, EAL,
270                                                 "  interrupt vector does not support eventfd!\n");
271                                 return -1;
272                         } else
273                                 continue;
274                 }
275
276                 /* set up an eventfd for interrupts */
277                 fd = eventfd(0, 0);
278                 if (fd < 0) {
279                         RTE_LOG(ERR, EAL, "  cannot set up eventfd, "
280                                         "error %i (%s)\n", errno, strerror(errno));
281                         return -1;
282                 }
283
284                 dev->intr_handle.fd = fd;
285                 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
286
287                 switch (i) {
288                 case VFIO_PCI_MSIX_IRQ_INDEX:
289                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
290                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
291                         break;
292                 case VFIO_PCI_MSI_IRQ_INDEX:
293                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
294                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
295                         break;
296                 case VFIO_PCI_INTX_IRQ_INDEX:
297                         internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
298                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
299                         break;
300                 default:
301                         RTE_LOG(ERR, EAL, "  unknown interrupt type!\n");
302                         return -1;
303                 }
304
305                 return 0;
306         }
307
308         /* if we're here, we haven't found a suitable interrupt vector */
309         return -1;
310 }
311
312 /* open container fd or get an existing one */
313 int
314 pci_vfio_get_container_fd(void)
315 {
316         int ret, vfio_container_fd;
317
318         /* if we're in a primary process, try to open the container */
319         if (internal_config.process_type == RTE_PROC_PRIMARY) {
320                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
321                 if (vfio_container_fd < 0) {
322                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
323                                         "error %i (%s)\n", errno, strerror(errno));
324                         return -1;
325                 }
326
327                 /* check VFIO API version */
328                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
329                 if (ret != VFIO_API_VERSION) {
330                         if (ret < 0)
331                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
332                                                 "error %i (%s)\n", errno, strerror(errno));
333                         else
334                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
335                         close(vfio_container_fd);
336                         return -1;
337                 }
338
339                 /* check if we support IOMMU type 1 */
340                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
341                 if (ret != 1) {
342                         if (ret < 0)
343                                 RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
344                                         "error %i (%s)\n", errno,
345                                         strerror(errno));
346                         else
347                                 RTE_LOG(ERR, EAL, "  unsupported IOMMU type "
348                                         "detected in VFIO\n");
349                         close(vfio_container_fd);
350                         return -1;
351                 }
352
353                 return vfio_container_fd;
354         } else {
355                 /*
356                  * if we're in a secondary process, request container fd from the
357                  * primary process via our socket
358                  */
359                 int socket_fd;
360
361                 socket_fd = vfio_mp_sync_connect_to_primary();
362                 if (socket_fd < 0) {
363                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
364                         return -1;
365                 }
366                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
367                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
368                         close(socket_fd);
369                         return -1;
370                 }
371                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
372                 if (vfio_container_fd < 0) {
373                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
374                         close(socket_fd);
375                         return -1;
376                 }
377                 close(socket_fd);
378                 return vfio_container_fd;
379         }
380
381         return -1;
382 }
383
384 /* open group fd or get an existing one */
385 int
386 pci_vfio_get_group_fd(int iommu_group_no)
387 {
388         int i;
389         int vfio_group_fd;
390         char filename[PATH_MAX];
391
392         /* check if we already have the group descriptor open */
393         for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
394                 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
395                         return vfio_cfg.vfio_groups[i].fd;
396
397         /* if primary, try to open the group */
398         if (internal_config.process_type == RTE_PROC_PRIMARY) {
399                 snprintf(filename, sizeof(filename),
400                                  VFIO_GROUP_FMT, iommu_group_no);
401                 vfio_group_fd = open(filename, O_RDWR);
402                 if (vfio_group_fd < 0) {
403                         /* if file not found, it's not an error */
404                         if (errno != ENOENT) {
405                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
406                                                 strerror(errno));
407                                 return -1;
408                         }
409                         return 0;
410                 }
411
412                 /* if the fd is valid, create a new group for it */
413                 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
414                         RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
415                         return -1;
416                 }
417                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
418                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
419                 return vfio_group_fd;
420         }
421         /* if we're in a secondary process, request group fd from the primary
422          * process via our socket
423          */
424         else {
425                 int socket_fd, ret;
426
427                 socket_fd = vfio_mp_sync_connect_to_primary();
428
429                 if (socket_fd < 0) {
430                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
431                         return -1;
432                 }
433                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
434                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
435                         close(socket_fd);
436                         return -1;
437                 }
438                 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
439                         RTE_LOG(ERR, EAL, "  cannot send group number!\n");
440                         close(socket_fd);
441                         return -1;
442                 }
443                 ret = vfio_mp_sync_receive_request(socket_fd);
444                 switch (ret) {
445                 case SOCKET_NO_FD:
446                         close(socket_fd);
447                         return 0;
448                 case SOCKET_OK:
449                         vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
450                         /* if we got the fd, return it */
451                         if (vfio_group_fd > 0) {
452                                 close(socket_fd);
453                                 return vfio_group_fd;
454                         }
455                         /* fall-through on error */
456                 default:
457                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
458                         close(socket_fd);
459                         return -1;
460                 }
461         }
462         return -1;
463 }
464
465 /* parse IOMMU group number for a PCI device
466  * returns -1 for errors, 0 for non-existent group */
467 static int
468 pci_vfio_get_group_no(const char *pci_addr)
469 {
470         char linkname[PATH_MAX];
471         char filename[PATH_MAX];
472         char *tok[16], *group_tok, *end;
473         int ret, iommu_group_no;
474
475         memset(linkname, 0, sizeof(linkname));
476         memset(filename, 0, sizeof(filename));
477
478         /* try to find out IOMMU group for this device */
479         snprintf(linkname, sizeof(linkname),
480                          SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
481
482         ret = readlink(linkname, filename, sizeof(filename));
483
484         /* if the link doesn't exist, no VFIO for us */
485         if (ret < 0)
486                 return 0;
487
488         ret = rte_strsplit(filename, sizeof(filename),
489                         tok, RTE_DIM(tok), '/');
490
491         if (ret <= 0) {
492                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", pci_addr);
493                 return -1;
494         }
495
496         /* IOMMU group is always the last token */
497         errno = 0;
498         group_tok = tok[ret - 1];
499         end = group_tok;
500         iommu_group_no = strtol(group_tok, &end, 10);
501         if ((end != group_tok && *end != '\0') || errno != 0) {
502                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", pci_addr);
503                 return -1;
504         }
505
506         return iommu_group_no;
507 }
508
509 static void
510 clear_current_group(void)
511 {
512         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
513         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
514 }
515
516
517 /*
518  * map the PCI resources of a PCI device in virtual memory (VFIO version).
519  * primary and secondary processes follow almost exactly the same path
520  */
521 int
522 pci_vfio_map_resource(struct rte_pci_device *dev)
523 {
524         struct vfio_group_status group_status = {
525                         .argsz = sizeof(group_status)
526         };
527         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
528         int vfio_group_fd, vfio_dev_fd;
529         int iommu_group_no;
530         char pci_addr[PATH_MAX] = {0};
531         struct rte_pci_addr *loc = &dev->addr;
532         int i, ret, msix_bar;
533         struct mapped_pci_resource *vfio_res = NULL;
534         struct pci_map *maps;
535
536         dev->intr_handle.fd = -1;
537         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
538
539         /* store PCI address string */
540         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
541                         loc->domain, loc->bus, loc->devid, loc->function);
542
543         /* get group number */
544         iommu_group_no = pci_vfio_get_group_no(pci_addr);
545
546         /* if 0, group doesn't exist */
547         if (iommu_group_no == 0) {
548                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
549                                 pci_addr);
550                 return 1;
551         }
552         /* if negative, something failed */
553         else if (iommu_group_no < 0)
554                 return -1;
555
556         /* get the actual group fd */
557         vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
558         if (vfio_group_fd < 0)
559                 return -1;
560
561         /* store group fd */
562         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
563         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
564
565         /* if group_fd == 0, that means the device isn't managed by VFIO */
566         if (vfio_group_fd == 0) {
567                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
568                                 pci_addr);
569                 /* we store 0 as group fd to distinguish between existing but
570                  * unbound VFIO groups, and groups that don't exist at all.
571                  */
572                 vfio_cfg.vfio_group_idx++;
573                 return 1;
574         }
575
576         /*
577          * at this point, we know at least one port on this device is bound to VFIO,
578          * so we can proceed to try and set this particular port up
579          */
580
581         /* check if the group is viable */
582         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
583         if (ret) {
584                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
585                                 "error %i (%s)\n", pci_addr, errno, strerror(errno));
586                 close(vfio_group_fd);
587                 clear_current_group();
588                 return -1;
589         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
590                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", pci_addr);
591                 close(vfio_group_fd);
592                 clear_current_group();
593                 return -1;
594         }
595
596         /*
597          * at this point, we know that this group is viable (meaning, all devices
598          * are either bound to VFIO or not bound to anything)
599          */
600
601         /* check if group does not have a container yet */
602         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
603
604                 /* add group to a container */
605                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
606                                 &vfio_cfg.vfio_container_fd);
607                 if (ret) {
608                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
609                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
610                         close(vfio_group_fd);
611                         clear_current_group();
612                         return -1;
613                 }
614                 /*
615                  * at this point we know that this group has been successfully
616                  * initialized, so we increment vfio_group_idx to indicate that we can
617                  * add new groups.
618                  */
619                 vfio_cfg.vfio_group_idx++;
620         }
621
622         /*
623          * set up DMA mappings for container
624          *
625          * needs to be done only once, only when at least one group is assigned to
626          * a container and only in primary process
627          */
628         if (internal_config.process_type == RTE_PROC_PRIMARY &&
629                         vfio_cfg.vfio_container_has_dma == 0) {
630                 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
631                 if (ret) {
632                         RTE_LOG(ERR, EAL, "  %s DMA remapping failed, "
633                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
634                         return -1;
635                 }
636                 vfio_cfg.vfio_container_has_dma = 1;
637         }
638
639         /* get a file descriptor for the device */
640         vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
641         if (vfio_dev_fd < 0) {
642                 /* if we cannot get a device fd, this simply means that this
643                  * particular port is not bound to VFIO
644                  */
645                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
646                                 pci_addr);
647                 return 1;
648         }
649
650         /* test and setup the device */
651         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
652         if (ret) {
653                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
654                                 "error %i (%s)\n", pci_addr, errno, strerror(errno));
655                 close(vfio_dev_fd);
656                 return -1;
657         }
658
659         /* get MSI-X BAR, if any (we have to know where it is because we can't
660          * mmap it when using VFIO) */
661         msix_bar = -1;
662         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar);
663         if (ret < 0) {
664                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n", pci_addr);
665                 close(vfio_dev_fd);
666                 return -1;
667         }
668
669         /* if we're in a primary process, allocate vfio_res and get region info */
670         if (internal_config.process_type == RTE_PROC_PRIMARY) {
671                 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
672                 if (vfio_res == NULL) {
673                         RTE_LOG(ERR, EAL,
674                                 "%s(): cannot store uio mmap details\n", __func__);
675                         close(vfio_dev_fd);
676                         return -1;
677                 }
678                 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
679
680                 /* get number of registers (up to BAR5) */
681                 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
682                                 VFIO_PCI_BAR5_REGION_INDEX + 1);
683         } else {
684                 /* if we're in a secondary process, just find our tailq entry */
685                 TAILQ_FOREACH(vfio_res, pci_res_list, next) {
686                         if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
687                                 continue;
688                         break;
689                 }
690                 /* if we haven't found our tailq entry, something's wrong */
691                 if (vfio_res == NULL) {
692                         RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
693                                         pci_addr);
694                         close(vfio_dev_fd);
695                         return -1;
696                 }
697         }
698
699         /* map BARs */
700         maps = vfio_res->maps;
701
702         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
703                 struct vfio_region_info reg = { .argsz = sizeof(reg) };
704                 void *bar_addr;
705
706                 reg.index = i;
707
708                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
709
710                 if (ret) {
711                         RTE_LOG(ERR, EAL, "  %s cannot get device region info "
712                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
713                         close(vfio_dev_fd);
714                         if (internal_config.process_type == RTE_PROC_PRIMARY)
715                                 rte_free(vfio_res);
716                         return -1;
717                 }
718
719                 /* skip non-mmapable BARs */
720                 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
721                         continue;
722
723                 /* skip MSI-X BAR */
724                 if (i == msix_bar)
725                         continue;
726
727                 if (internal_config.process_type == RTE_PROC_PRIMARY) {
728                         /* try mapping somewhere close to the end of hugepages */
729                         if (pci_map_addr == NULL)
730                                 pci_map_addr = pci_find_max_end_va();
731
732                         bar_addr = pci_map_resource(pci_map_addr, vfio_dev_fd, reg.offset,
733                                         reg.size);
734                         pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
735                 } else {
736                         bar_addr = pci_map_resource(maps[i].addr, vfio_dev_fd, reg.offset,
737                                         reg.size);
738                 }
739
740                 if (bar_addr == MAP_FAILED ||
741                                 (internal_config.process_type == RTE_PROC_SECONDARY &&
742                                                 bar_addr != maps[i].addr)) {
743                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n", pci_addr, i,
744                                         strerror(errno));
745                         close(vfio_dev_fd);
746                         if (internal_config.process_type == RTE_PROC_PRIMARY)
747                                 rte_free(vfio_res);
748                         return -1;
749                 }
750
751                 maps[i].addr = bar_addr;
752                 maps[i].offset = reg.offset;
753                 maps[i].size = reg.size;
754                 dev->mem_resource[i].addr = bar_addr;
755         }
756
757         /* if secondary process, do not set up interrupts */
758         if (internal_config.process_type == RTE_PROC_PRIMARY) {
759                 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
760                         RTE_LOG(ERR, EAL, "  %s error setting up interrupts!\n", pci_addr);
761                         close(vfio_dev_fd);
762                         rte_free(vfio_res);
763                         return -1;
764                 }
765
766                 /* set bus mastering for the device */
767                 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
768                         RTE_LOG(ERR, EAL, "  %s cannot set up bus mastering!\n", pci_addr);
769                         close(vfio_dev_fd);
770                         rte_free(vfio_res);
771                         return -1;
772                 }
773
774                 /* Reset the device */
775                 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
776         }
777
778         if (internal_config.process_type == RTE_PROC_PRIMARY)
779                 TAILQ_INSERT_TAIL(pci_res_list, vfio_res, next);
780
781         return 0;
782 }
783
784 int
785 pci_vfio_enable(void)
786 {
787         /* initialize group list */
788         int i;
789         int module_vfio_type1;
790
791         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
792                 vfio_cfg.vfio_groups[i].fd = -1;
793                 vfio_cfg.vfio_groups[i].group_no = -1;
794         }
795
796         module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
797
798         /* return error directly */
799         if (module_vfio_type1 == -1) {
800                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
801                 return -1;
802         }
803
804         /* return 0 if VFIO modules not loaded */
805         if (module_vfio_type1 == 0) {
806                 RTE_LOG(INFO, EAL, "VFIO modules not all loaded, "
807                         "skip VFIO support...\n");
808                 return 0;
809         }
810
811         vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
812
813         /* check if we have VFIO driver enabled */
814         if (vfio_cfg.vfio_container_fd != -1)
815                 vfio_cfg.vfio_enabled = 1;
816         else
817                 RTE_LOG(INFO, EAL, "VFIO support could not be initialized\n");
818
819         return 0;
820 }
821
822 int
823 pci_vfio_is_enabled(void)
824 {
825         return vfio_cfg.vfio_enabled;
826 }
827 #endif