4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <linux/pci_regs.h>
37 #include <sys/eventfd.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
44 #include <rte_tailq.h>
45 #include <rte_eal_memconfig.h>
46 #include <rte_malloc.h>
47 #include <eal_private.h>
49 #include "eal_filesystem.h"
50 #include "eal_pci_init.h"
55 * PCI probing under linux (VFIO version)
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.
60 * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
65 #define PAGE_SIZE (sysconf(_SC_PAGESIZE))
66 #define PAGE_MASK (~(PAGE_SIZE - 1))
68 #define VFIO_DIR "/dev/vfio"
69 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
70 #define VFIO_GROUP_FMT "/dev/vfio/%u"
71 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
73 /* per-process VFIO config */
74 static struct vfio_config vfio_cfg;
76 /* get PCI BAR number where MSI-X interrupts are */
78 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
79 uint32_t *msix_table_size)
84 uint8_t cap_id, cap_offset;
86 /* read PCI capability pointer from config space */
87 ret = pread64(fd, ®, sizeof(reg),
88 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
90 if (ret != sizeof(reg)) {
91 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
96 /* we need first byte */
97 cap_offset = reg & 0xFF;
101 /* read PCI capability ID */
102 ret = pread64(fd, ®, sizeof(reg),
103 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
105 if (ret != sizeof(reg)) {
106 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
111 /* we need first byte */
114 /* if we haven't reached MSI-X, check next capability */
115 if (cap_id != PCI_CAP_ID_MSIX) {
116 ret = pread64(fd, ®, sizeof(reg),
117 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
119 if (ret != sizeof(reg)) {
120 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
125 /* we need second byte */
126 cap_offset = (reg & 0xFF00) >> 8;
130 /* else, read table offset */
132 /* table offset resides in the next 4 bytes */
133 ret = pread64(fd, ®, sizeof(reg),
134 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
136 if (ret != sizeof(reg)) {
137 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
142 ret = pread64(fd, &flags, sizeof(flags),
143 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
145 if (ret != sizeof(flags)) {
146 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
151 *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
152 *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
153 *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
161 /* set PCI bus mastering */
163 pci_vfio_set_bus_master(int dev_fd)
168 ret = pread64(dev_fd, ®, sizeof(reg),
169 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
171 if (ret != sizeof(reg)) {
172 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
176 /* set the master bit */
177 reg |= PCI_COMMAND_MASTER;
179 ret = pwrite64(dev_fd, ®, sizeof(reg),
180 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
183 if (ret != sizeof(reg)) {
184 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
191 /* set up DMA mappings */
193 pci_vfio_setup_dma_maps(int vfio_container_fd)
195 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
198 ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
201 RTE_LOG(ERR, EAL, " cannot set IOMMU type, "
202 "error %i (%s)\n", errno, strerror(errno));
206 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
207 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
208 struct vfio_iommu_type1_dma_map dma_map;
210 if (ms[i].addr == NULL)
213 memset(&dma_map, 0, sizeof(dma_map));
214 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
215 dma_map.vaddr = ms[i].addr_64;
216 dma_map.size = ms[i].len;
217 dma_map.iova = ms[i].phys_addr;
218 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
220 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
223 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
224 "error %i (%s)\n", errno, strerror(errno));
232 /* set up interrupt support (but not enable interrupts) */
234 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
236 int i, ret, intr_idx;
238 /* default to invalid index */
239 intr_idx = VFIO_PCI_NUM_IRQS;
241 /* get interrupt type from internal config (MSI-X by default, can be
242 * overriden from the command line
244 switch (internal_config.vfio_intr_mode) {
245 case RTE_INTR_MODE_MSIX:
246 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
248 case RTE_INTR_MODE_MSI:
249 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
251 case RTE_INTR_MODE_LEGACY:
252 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
254 /* don't do anything if we want to automatically determine interrupt type */
255 case RTE_INTR_MODE_NONE:
258 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
262 /* start from MSI-X interrupt type */
263 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
264 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
267 /* skip interrupt modes we don't want */
268 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
274 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
276 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
277 "error %i (%s)\n", errno, strerror(errno));
281 /* if this vector cannot be used with eventfd, fail if we explicitly
282 * specified interrupt type, otherwise continue */
283 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
284 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
286 " interrupt vector does not support eventfd!\n");
292 /* set up an eventfd for interrupts */
295 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
296 "error %i (%s)\n", errno, strerror(errno));
300 dev->intr_handle.fd = fd;
301 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
304 case VFIO_PCI_MSIX_IRQ_INDEX:
305 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
306 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
308 case VFIO_PCI_MSI_IRQ_INDEX:
309 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
310 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
312 case VFIO_PCI_INTX_IRQ_INDEX:
313 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
314 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
317 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
324 /* if we're here, we haven't found a suitable interrupt vector */
328 /* open container fd or get an existing one */
330 pci_vfio_get_container_fd(void)
332 int ret, vfio_container_fd;
334 /* if we're in a primary process, try to open the container */
335 if (internal_config.process_type == RTE_PROC_PRIMARY) {
336 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
337 if (vfio_container_fd < 0) {
338 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
339 "error %i (%s)\n", errno, strerror(errno));
343 /* check VFIO API version */
344 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
345 if (ret != VFIO_API_VERSION) {
347 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
348 "error %i (%s)\n", errno, strerror(errno));
350 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
351 close(vfio_container_fd);
355 /* check if we support IOMMU type 1 */
356 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
359 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
360 "error %i (%s)\n", errno,
363 RTE_LOG(ERR, EAL, " unsupported IOMMU type "
364 "detected in VFIO\n");
365 close(vfio_container_fd);
369 return vfio_container_fd;
372 * if we're in a secondary process, request container fd from the
373 * primary process via our socket
377 socket_fd = vfio_mp_sync_connect_to_primary();
379 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
382 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
383 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
387 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
388 if (vfio_container_fd < 0) {
389 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
394 return vfio_container_fd;
400 /* open group fd or get an existing one */
402 pci_vfio_get_group_fd(int iommu_group_no)
406 char filename[PATH_MAX];
408 /* check if we already have the group descriptor open */
409 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
410 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
411 return vfio_cfg.vfio_groups[i].fd;
413 /* if primary, try to open the group */
414 if (internal_config.process_type == RTE_PROC_PRIMARY) {
415 snprintf(filename, sizeof(filename),
416 VFIO_GROUP_FMT, iommu_group_no);
417 vfio_group_fd = open(filename, O_RDWR);
418 if (vfio_group_fd < 0) {
419 /* if file not found, it's not an error */
420 if (errno != ENOENT) {
421 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
428 /* if the fd is valid, create a new group for it */
429 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
430 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
433 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
434 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
435 return vfio_group_fd;
437 /* if we're in a secondary process, request group fd from the primary
438 * process via our socket
443 socket_fd = vfio_mp_sync_connect_to_primary();
446 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
449 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
450 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
454 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
455 RTE_LOG(ERR, EAL, " cannot send group number!\n");
459 ret = vfio_mp_sync_receive_request(socket_fd);
465 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
466 /* if we got the fd, return it */
467 if (vfio_group_fd > 0) {
469 return vfio_group_fd;
471 /* fall-through on error */
473 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
481 /* parse IOMMU group number for a PCI device
482 * returns -1 for errors, 0 for non-existent group */
484 pci_vfio_get_group_no(const char *pci_addr)
486 char linkname[PATH_MAX];
487 char filename[PATH_MAX];
488 char *tok[16], *group_tok, *end;
489 int ret, iommu_group_no;
491 memset(linkname, 0, sizeof(linkname));
492 memset(filename, 0, sizeof(filename));
494 /* try to find out IOMMU group for this device */
495 snprintf(linkname, sizeof(linkname),
496 SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
498 ret = readlink(linkname, filename, sizeof(filename));
500 /* if the link doesn't exist, no VFIO for us */
504 ret = rte_strsplit(filename, sizeof(filename),
505 tok, RTE_DIM(tok), '/');
508 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", pci_addr);
512 /* IOMMU group is always the last token */
514 group_tok = tok[ret - 1];
516 iommu_group_no = strtol(group_tok, &end, 10);
517 if ((end != group_tok && *end != '\0') || errno != 0) {
518 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", pci_addr);
522 return iommu_group_no;
526 clear_current_group(void)
528 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
529 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
534 * map the PCI resources of a PCI device in virtual memory (VFIO version).
535 * primary and secondary processes follow almost exactly the same path
538 pci_vfio_map_resource(struct rte_pci_device *dev)
540 struct vfio_group_status group_status = {
541 .argsz = sizeof(group_status)
543 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
544 int vfio_group_fd, vfio_dev_fd;
546 char pci_addr[PATH_MAX] = {0};
547 struct rte_pci_addr *loc = &dev->addr;
548 int i, ret, msix_bar;
549 struct mapped_pci_resource *vfio_res = NULL;
550 struct pci_map *maps;
551 uint32_t msix_table_offset = 0;
552 uint32_t msix_table_size = 0;
554 dev->intr_handle.fd = -1;
555 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
557 /* store PCI address string */
558 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
559 loc->domain, loc->bus, loc->devid, loc->function);
561 /* get group number */
562 iommu_group_no = pci_vfio_get_group_no(pci_addr);
564 /* if 0, group doesn't exist */
565 if (iommu_group_no == 0) {
566 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
570 /* if negative, something failed */
571 else if (iommu_group_no < 0)
574 /* get the actual group fd */
575 vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
576 if (vfio_group_fd < 0)
580 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
581 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
583 /* if group_fd == 0, that means the device isn't managed by VFIO */
584 if (vfio_group_fd == 0) {
585 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
587 /* we store 0 as group fd to distinguish between existing but
588 * unbound VFIO groups, and groups that don't exist at all.
590 vfio_cfg.vfio_group_idx++;
595 * at this point, we know at least one port on this device is bound to VFIO,
596 * so we can proceed to try and set this particular port up
599 /* check if the group is viable */
600 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
602 RTE_LOG(ERR, EAL, " %s cannot get group status, "
603 "error %i (%s)\n", pci_addr, errno, strerror(errno));
604 close(vfio_group_fd);
605 clear_current_group();
607 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
608 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", pci_addr);
609 close(vfio_group_fd);
610 clear_current_group();
615 * at this point, we know that this group is viable (meaning, all devices
616 * are either bound to VFIO or not bound to anything)
619 /* check if group does not have a container yet */
620 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
622 /* add group to a container */
623 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
624 &vfio_cfg.vfio_container_fd);
626 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
627 "error %i (%s)\n", pci_addr, errno, strerror(errno));
628 close(vfio_group_fd);
629 clear_current_group();
633 * at this point we know that this group has been successfully
634 * initialized, so we increment vfio_group_idx to indicate that we can
637 vfio_cfg.vfio_group_idx++;
641 * set up DMA mappings for container
643 * needs to be done only once, only when at least one group is assigned to
644 * a container and only in primary process
646 if (internal_config.process_type == RTE_PROC_PRIMARY &&
647 vfio_cfg.vfio_container_has_dma == 0) {
648 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
650 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
651 "error %i (%s)\n", pci_addr, errno, strerror(errno));
654 vfio_cfg.vfio_container_has_dma = 1;
657 /* get a file descriptor for the device */
658 vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
659 if (vfio_dev_fd < 0) {
660 /* if we cannot get a device fd, this simply means that this
661 * particular port is not bound to VFIO
663 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
668 /* test and setup the device */
669 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
671 RTE_LOG(ERR, EAL, " %s cannot get device info, "
672 "error %i (%s)\n", pci_addr, errno, strerror(errno));
677 /* get MSI-X BAR, if any (we have to know where it is because we can't
678 * easily mmap it when using VFIO) */
680 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
681 &msix_table_offset, &msix_table_size);
683 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n", pci_addr);
688 /* if we're in a primary process, allocate vfio_res and get region info */
689 if (internal_config.process_type == RTE_PROC_PRIMARY) {
690 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
691 if (vfio_res == NULL) {
693 "%s(): cannot store uio mmap details\n", __func__);
697 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
699 /* get number of registers (up to BAR5) */
700 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
701 VFIO_PCI_BAR5_REGION_INDEX + 1);
703 /* if we're in a secondary process, just find our tailq entry */
704 TAILQ_FOREACH(vfio_res, pci_res_list, next) {
705 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
709 /* if we haven't found our tailq entry, something's wrong */
710 if (vfio_res == NULL) {
711 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
719 maps = vfio_res->maps;
721 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
722 struct vfio_region_info reg = { .argsz = sizeof(reg) };
725 uint32_t offset, size;
730 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
733 RTE_LOG(ERR, EAL, " %s cannot get device region info "
734 "error %i (%s)\n", pci_addr, errno, strerror(errno));
736 if (internal_config.process_type == RTE_PROC_PRIMARY)
741 /* skip non-mmapable BARs */
742 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
747 * VFIO will not let us map the MSI-X table,
748 * but we can map around it.
750 uint32_t table_start = msix_table_offset;
751 uint32_t table_end = table_start + msix_table_size;
752 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
753 table_start &= PAGE_MASK;
755 if (table_start == 0 && table_end >= reg.size) {
756 /* Cannot map this BAR */
757 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
760 memreg[0].offset = reg.offset;
761 memreg[0].size = table_start;
762 memreg[1].offset = table_end;
763 memreg[1].size = reg.size - table_end;
766 "Trying to map BAR %d that contains the MSI-X "
767 "table. Trying offsets: "
768 "%04x:%04x, %04x:%04x\n", i,
769 memreg[0].offset, memreg[0].size,
770 memreg[1].offset, memreg[1].size);
773 memreg[0].offset = reg.offset;
774 memreg[0].size = reg.size;
777 /* try to figure out an address */
778 if (internal_config.process_type == RTE_PROC_PRIMARY) {
779 /* try mapping somewhere close to the end of hugepages */
780 if (pci_map_addr == NULL)
781 pci_map_addr = pci_find_max_end_va();
783 bar_addr = pci_map_addr;
784 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
786 bar_addr = maps[i].addr;
789 /* reserve the address using an inaccessible mapping */
790 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
791 MAP_ANONYMOUS, -1, 0);
792 if (bar_addr != MAP_FAILED) {
793 void *map_addr = NULL;
794 if (memreg[0].size) {
795 /* actual map of first part */
796 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
802 /* if there's a second part, try to map it */
803 if (map_addr != MAP_FAILED
804 && memreg[1].offset && memreg[1].size) {
805 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
806 map_addr = pci_map_resource(second_addr,
807 vfio_dev_fd, memreg[1].offset,
812 if (map_addr == MAP_FAILED || !map_addr) {
813 munmap(bar_addr, reg.size);
814 bar_addr = MAP_FAILED;
818 if (bar_addr == MAP_FAILED ||
819 (internal_config.process_type == RTE_PROC_SECONDARY &&
820 bar_addr != maps[i].addr)) {
821 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n", pci_addr, i,
824 if (internal_config.process_type == RTE_PROC_PRIMARY)
829 maps[i].addr = bar_addr;
830 maps[i].offset = reg.offset;
831 maps[i].size = reg.size;
832 dev->mem_resource[i].addr = bar_addr;
835 /* if secondary process, do not set up interrupts */
836 if (internal_config.process_type == RTE_PROC_PRIMARY) {
837 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
838 RTE_LOG(ERR, EAL, " %s error setting up interrupts!\n", pci_addr);
844 /* set bus mastering for the device */
845 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
846 RTE_LOG(ERR, EAL, " %s cannot set up bus mastering!\n", pci_addr);
852 /* Reset the device */
853 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
856 if (internal_config.process_type == RTE_PROC_PRIMARY)
857 TAILQ_INSERT_TAIL(pci_res_list, vfio_res, next);
863 pci_vfio_enable(void)
865 /* initialize group list */
867 int module_vfio_type1;
869 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
870 vfio_cfg.vfio_groups[i].fd = -1;
871 vfio_cfg.vfio_groups[i].group_no = -1;
874 module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
876 /* return error directly */
877 if (module_vfio_type1 == -1) {
878 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
882 /* return 0 if VFIO modules not loaded */
883 if (module_vfio_type1 == 0) {
884 RTE_LOG(INFO, EAL, "VFIO modules not all loaded, "
885 "skip VFIO support...\n");
889 vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
891 /* check if we have VFIO driver enabled */
892 if (vfio_cfg.vfio_container_fd != -1)
893 vfio_cfg.vfio_enabled = 1;
895 RTE_LOG(INFO, EAL, "VFIO support could not be initialized\n");
901 pci_vfio_is_enabled(void)
903 return vfio_cfg.vfio_enabled;