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_eal_memconfig.h>
45 #include <rte_malloc.h>
46 #include <eal_private.h>
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
54 * PCI probing under linux (VFIO version)
56 * This code tries to determine if the PCI device is bound to VFIO driver,
57 * and initialize it (map BARs, set up interrupts) if that's the case.
59 * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
64 #define PAGE_SIZE (sysconf(_SC_PAGESIZE))
65 #define PAGE_MASK (~(PAGE_SIZE - 1))
67 #define VFIO_DIR "/dev/vfio"
68 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
69 #define VFIO_GROUP_FMT "/dev/vfio/%u"
70 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
72 /* per-process VFIO config */
73 static struct vfio_config vfio_cfg;
75 /* get PCI BAR number where MSI-X interrupts are */
77 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
78 uint32_t *msix_table_size)
83 uint8_t cap_id, cap_offset;
85 /* read PCI capability pointer from config space */
86 ret = pread64(fd, ®, sizeof(reg),
87 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
89 if (ret != sizeof(reg)) {
90 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
95 /* we need first byte */
96 cap_offset = reg & 0xFF;
100 /* read PCI capability ID */
101 ret = pread64(fd, ®, sizeof(reg),
102 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
104 if (ret != sizeof(reg)) {
105 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
110 /* we need first byte */
113 /* if we haven't reached MSI-X, check next capability */
114 if (cap_id != PCI_CAP_ID_MSIX) {
115 ret = pread64(fd, ®, sizeof(reg),
116 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
118 if (ret != sizeof(reg)) {
119 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
124 /* we need second byte */
125 cap_offset = (reg & 0xFF00) >> 8;
129 /* else, read table offset */
131 /* table offset resides in the next 4 bytes */
132 ret = pread64(fd, ®, sizeof(reg),
133 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
135 if (ret != sizeof(reg)) {
136 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
141 ret = pread64(fd, &flags, sizeof(flags),
142 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
144 if (ret != sizeof(flags)) {
145 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
150 *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
151 *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
152 *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
160 /* set PCI bus mastering */
162 pci_vfio_set_bus_master(int dev_fd)
167 ret = pread64(dev_fd, ®, sizeof(reg),
168 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
170 if (ret != sizeof(reg)) {
171 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
175 /* set the master bit */
176 reg |= PCI_COMMAND_MASTER;
178 ret = pwrite64(dev_fd, ®, sizeof(reg),
179 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
182 if (ret != sizeof(reg)) {
183 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
190 /* set up DMA mappings */
192 pci_vfio_setup_dma_maps(int vfio_container_fd)
194 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
197 ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
200 RTE_LOG(ERR, EAL, " cannot set IOMMU type, "
201 "error %i (%s)\n", errno, strerror(errno));
205 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
206 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
207 struct vfio_iommu_type1_dma_map dma_map;
209 if (ms[i].addr == NULL)
212 memset(&dma_map, 0, sizeof(dma_map));
213 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
214 dma_map.vaddr = ms[i].addr_64;
215 dma_map.size = ms[i].len;
216 dma_map.iova = ms[i].phys_addr;
217 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
219 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
222 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
223 "error %i (%s)\n", errno, strerror(errno));
231 /* set up interrupt support (but not enable interrupts) */
233 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
235 int i, ret, intr_idx;
237 /* default to invalid index */
238 intr_idx = VFIO_PCI_NUM_IRQS;
240 /* get interrupt type from internal config (MSI-X by default, can be
241 * overriden from the command line
243 switch (internal_config.vfio_intr_mode) {
244 case RTE_INTR_MODE_MSIX:
245 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
247 case RTE_INTR_MODE_MSI:
248 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
250 case RTE_INTR_MODE_LEGACY:
251 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
253 /* don't do anything if we want to automatically determine interrupt type */
254 case RTE_INTR_MODE_NONE:
257 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
261 /* start from MSI-X interrupt type */
262 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
263 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
266 /* skip interrupt modes we don't want */
267 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
273 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
275 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
276 "error %i (%s)\n", errno, strerror(errno));
280 /* if this vector cannot be used with eventfd, fail if we explicitly
281 * specified interrupt type, otherwise continue */
282 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
283 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
285 " interrupt vector does not support eventfd!\n");
291 /* set up an eventfd for interrupts */
294 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
295 "error %i (%s)\n", errno, strerror(errno));
299 dev->intr_handle.fd = fd;
300 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
303 case VFIO_PCI_MSIX_IRQ_INDEX:
304 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
305 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
307 case VFIO_PCI_MSI_IRQ_INDEX:
308 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
309 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
311 case VFIO_PCI_INTX_IRQ_INDEX:
312 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
313 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
316 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
323 /* if we're here, we haven't found a suitable interrupt vector */
327 /* open container fd or get an existing one */
329 pci_vfio_get_container_fd(void)
331 int ret, vfio_container_fd;
333 /* if we're in a primary process, try to open the container */
334 if (internal_config.process_type == RTE_PROC_PRIMARY) {
335 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
336 if (vfio_container_fd < 0) {
337 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
338 "error %i (%s)\n", errno, strerror(errno));
342 /* check VFIO API version */
343 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
344 if (ret != VFIO_API_VERSION) {
346 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
347 "error %i (%s)\n", errno, strerror(errno));
349 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
350 close(vfio_container_fd);
354 /* check if we support IOMMU type 1 */
355 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
358 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
359 "error %i (%s)\n", errno,
362 RTE_LOG(ERR, EAL, " unsupported IOMMU type "
363 "detected in VFIO\n");
364 close(vfio_container_fd);
368 return vfio_container_fd;
371 * if we're in a secondary process, request container fd from the
372 * primary process via our socket
376 socket_fd = vfio_mp_sync_connect_to_primary();
378 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
381 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
382 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
386 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
387 if (vfio_container_fd < 0) {
388 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
393 return vfio_container_fd;
399 /* open group fd or get an existing one */
401 pci_vfio_get_group_fd(int iommu_group_no)
405 char filename[PATH_MAX];
407 /* check if we already have the group descriptor open */
408 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
409 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
410 return vfio_cfg.vfio_groups[i].fd;
412 /* if primary, try to open the group */
413 if (internal_config.process_type == RTE_PROC_PRIMARY) {
414 snprintf(filename, sizeof(filename),
415 VFIO_GROUP_FMT, iommu_group_no);
416 vfio_group_fd = open(filename, O_RDWR);
417 if (vfio_group_fd < 0) {
418 /* if file not found, it's not an error */
419 if (errno != ENOENT) {
420 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
427 /* if the fd is valid, create a new group for it */
428 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
429 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
432 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
433 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
434 return vfio_group_fd;
436 /* if we're in a secondary process, request group fd from the primary
437 * process via our socket
442 socket_fd = vfio_mp_sync_connect_to_primary();
445 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
448 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
449 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
453 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
454 RTE_LOG(ERR, EAL, " cannot send group number!\n");
458 ret = vfio_mp_sync_receive_request(socket_fd);
464 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
465 /* if we got the fd, return it */
466 if (vfio_group_fd > 0) {
468 return vfio_group_fd;
470 /* fall-through on error */
472 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
480 /* parse IOMMU group number for a PCI device
481 * returns -1 for errors, 0 for non-existent group */
483 pci_vfio_get_group_no(const char *pci_addr)
485 char linkname[PATH_MAX];
486 char filename[PATH_MAX];
487 char *tok[16], *group_tok, *end;
488 int ret, iommu_group_no;
490 memset(linkname, 0, sizeof(linkname));
491 memset(filename, 0, sizeof(filename));
493 /* try to find out IOMMU group for this device */
494 snprintf(linkname, sizeof(linkname),
495 SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
497 ret = readlink(linkname, filename, sizeof(filename));
499 /* if the link doesn't exist, no VFIO for us */
503 ret = rte_strsplit(filename, sizeof(filename),
504 tok, RTE_DIM(tok), '/');
507 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", pci_addr);
511 /* IOMMU group is always the last token */
513 group_tok = tok[ret - 1];
515 iommu_group_no = strtol(group_tok, &end, 10);
516 if ((end != group_tok && *end != '\0') || errno != 0) {
517 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", pci_addr);
521 return iommu_group_no;
525 clear_current_group(void)
527 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
528 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
533 * map the PCI resources of a PCI device in virtual memory (VFIO version).
534 * primary and secondary processes follow almost exactly the same path
537 pci_vfio_map_resource(struct rte_pci_device *dev)
539 struct vfio_group_status group_status = {
540 .argsz = sizeof(group_status)
542 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
543 int vfio_group_fd, vfio_dev_fd;
545 char pci_addr[PATH_MAX] = {0};
546 struct rte_pci_addr *loc = &dev->addr;
547 int i, ret, msix_bar;
548 struct mapped_pci_resource *vfio_res = NULL;
549 struct pci_map *maps;
550 uint32_t msix_table_offset = 0;
551 uint32_t msix_table_size = 0;
553 dev->intr_handle.fd = -1;
554 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
556 /* store PCI address string */
557 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
558 loc->domain, loc->bus, loc->devid, loc->function);
560 /* get group number */
561 iommu_group_no = pci_vfio_get_group_no(pci_addr);
563 /* if 0, group doesn't exist */
564 if (iommu_group_no == 0) {
565 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
569 /* if negative, something failed */
570 else if (iommu_group_no < 0)
573 /* get the actual group fd */
574 vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
575 if (vfio_group_fd < 0)
579 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
580 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
582 /* if group_fd == 0, that means the device isn't managed by VFIO */
583 if (vfio_group_fd == 0) {
584 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
586 /* we store 0 as group fd to distinguish between existing but
587 * unbound VFIO groups, and groups that don't exist at all.
589 vfio_cfg.vfio_group_idx++;
594 * at this point, we know at least one port on this device is bound to VFIO,
595 * so we can proceed to try and set this particular port up
598 /* check if the group is viable */
599 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
601 RTE_LOG(ERR, EAL, " %s cannot get group status, "
602 "error %i (%s)\n", pci_addr, errno, strerror(errno));
603 close(vfio_group_fd);
604 clear_current_group();
606 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
607 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", pci_addr);
608 close(vfio_group_fd);
609 clear_current_group();
614 * at this point, we know that this group is viable (meaning, all devices
615 * are either bound to VFIO or not bound to anything)
618 /* check if group does not have a container yet */
619 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
621 /* add group to a container */
622 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
623 &vfio_cfg.vfio_container_fd);
625 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
626 "error %i (%s)\n", pci_addr, errno, strerror(errno));
627 close(vfio_group_fd);
628 clear_current_group();
632 * at this point we know that this group has been successfully
633 * initialized, so we increment vfio_group_idx to indicate that we can
636 vfio_cfg.vfio_group_idx++;
640 * set up DMA mappings for container
642 * needs to be done only once, only when at least one group is assigned to
643 * a container and only in primary process
645 if (internal_config.process_type == RTE_PROC_PRIMARY &&
646 vfio_cfg.vfio_container_has_dma == 0) {
647 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
649 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
650 "error %i (%s)\n", pci_addr, errno, strerror(errno));
653 vfio_cfg.vfio_container_has_dma = 1;
656 /* get a file descriptor for the device */
657 vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
658 if (vfio_dev_fd < 0) {
659 /* if we cannot get a device fd, this simply means that this
660 * particular port is not bound to VFIO
662 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
667 /* test and setup the device */
668 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
670 RTE_LOG(ERR, EAL, " %s cannot get device info, "
671 "error %i (%s)\n", pci_addr, errno, strerror(errno));
676 /* get MSI-X BAR, if any (we have to know where it is because we can't
677 * easily mmap it when using VFIO) */
679 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
680 &msix_table_offset, &msix_table_size);
682 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n", pci_addr);
687 /* if we're in a primary process, allocate vfio_res and get region info */
688 if (internal_config.process_type == RTE_PROC_PRIMARY) {
689 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
690 if (vfio_res == NULL) {
692 "%s(): cannot store uio mmap details\n", __func__);
696 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
698 /* get number of registers (up to BAR5) */
699 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
700 VFIO_PCI_BAR5_REGION_INDEX + 1);
702 /* if we're in a secondary process, just find our tailq entry */
703 TAILQ_FOREACH(vfio_res, pci_res_list, next) {
704 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
708 /* if we haven't found our tailq entry, something's wrong */
709 if (vfio_res == NULL) {
710 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
718 maps = vfio_res->maps;
720 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
721 struct vfio_region_info reg = { .argsz = sizeof(reg) };
724 uint32_t offset, size;
729 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
732 RTE_LOG(ERR, EAL, " %s cannot get device region info "
733 "error %i (%s)\n", pci_addr, errno, strerror(errno));
735 if (internal_config.process_type == RTE_PROC_PRIMARY)
740 /* skip non-mmapable BARs */
741 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
746 * VFIO will not let us map the MSI-X table,
747 * but we can map around it.
749 uint32_t table_start = msix_table_offset;
750 uint32_t table_end = table_start + msix_table_size;
751 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
752 table_start &= PAGE_MASK;
754 if (table_start == 0 && table_end >= reg.size) {
755 /* Cannot map this BAR */
756 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
759 memreg[0].offset = reg.offset;
760 memreg[0].size = table_start;
761 memreg[1].offset = table_end;
762 memreg[1].size = reg.size - table_end;
765 "Trying to map BAR %d that contains the MSI-X "
766 "table. Trying offsets: "
767 "%04x:%04x, %04x:%04x\n", i,
768 memreg[0].offset, memreg[0].size,
769 memreg[1].offset, memreg[1].size);
772 memreg[0].offset = reg.offset;
773 memreg[0].size = reg.size;
776 /* try to figure out an address */
777 if (internal_config.process_type == RTE_PROC_PRIMARY) {
778 /* try mapping somewhere close to the end of hugepages */
779 if (pci_map_addr == NULL)
780 pci_map_addr = pci_find_max_end_va();
782 bar_addr = pci_map_addr;
783 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
785 bar_addr = maps[i].addr;
788 /* reserve the address using an inaccessible mapping */
789 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
790 MAP_ANONYMOUS, -1, 0);
791 if (bar_addr != MAP_FAILED) {
792 void *map_addr = NULL;
793 if (memreg[0].size) {
794 /* actual map of first part */
795 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
801 /* if there's a second part, try to map it */
802 if (map_addr != MAP_FAILED
803 && memreg[1].offset && memreg[1].size) {
804 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
805 map_addr = pci_map_resource(second_addr,
806 vfio_dev_fd, memreg[1].offset,
811 if (map_addr == MAP_FAILED || !map_addr) {
812 munmap(bar_addr, reg.size);
813 bar_addr = MAP_FAILED;
817 if (bar_addr == MAP_FAILED ||
818 (internal_config.process_type == RTE_PROC_SECONDARY &&
819 bar_addr != maps[i].addr)) {
820 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n", pci_addr, i,
823 if (internal_config.process_type == RTE_PROC_PRIMARY)
828 maps[i].addr = bar_addr;
829 maps[i].offset = reg.offset;
830 maps[i].size = reg.size;
831 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
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;