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 static struct rte_tailq_elem rte_vfio_tailq = {
68 .name = "VFIO_RESOURCE_LIST",
70 EAL_REGISTER_TAILQ(rte_vfio_tailq)
72 #define VFIO_DIR "/dev/vfio"
73 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
74 #define VFIO_GROUP_FMT "/dev/vfio/%u"
75 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
77 /* per-process VFIO config */
78 static struct vfio_config vfio_cfg;
80 /* get PCI BAR number where MSI-X interrupts are */
82 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
83 uint32_t *msix_table_size)
88 uint8_t cap_id, cap_offset;
90 /* read PCI capability pointer from config space */
91 ret = pread64(fd, ®, sizeof(reg),
92 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
94 if (ret != sizeof(reg)) {
95 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
100 /* we need first byte */
101 cap_offset = reg & 0xFF;
105 /* read PCI capability ID */
106 ret = pread64(fd, ®, sizeof(reg),
107 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
109 if (ret != sizeof(reg)) {
110 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
115 /* we need first byte */
118 /* if we haven't reached MSI-X, check next capability */
119 if (cap_id != PCI_CAP_ID_MSIX) {
120 ret = pread64(fd, ®, sizeof(reg),
121 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
123 if (ret != sizeof(reg)) {
124 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
129 /* we need second byte */
130 cap_offset = (reg & 0xFF00) >> 8;
134 /* else, read table offset */
136 /* table offset resides in the next 4 bytes */
137 ret = pread64(fd, ®, sizeof(reg),
138 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
140 if (ret != sizeof(reg)) {
141 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
146 ret = pread64(fd, &flags, sizeof(flags),
147 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
149 if (ret != sizeof(flags)) {
150 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
155 *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
156 *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
157 *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
165 /* set PCI bus mastering */
167 pci_vfio_set_bus_master(int dev_fd)
172 ret = pread64(dev_fd, ®, sizeof(reg),
173 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
175 if (ret != sizeof(reg)) {
176 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
180 /* set the master bit */
181 reg |= PCI_COMMAND_MASTER;
183 ret = pwrite64(dev_fd, ®, sizeof(reg),
184 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
187 if (ret != sizeof(reg)) {
188 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
195 /* set up DMA mappings */
197 pci_vfio_setup_dma_maps(int vfio_container_fd)
199 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
202 ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
205 RTE_LOG(ERR, EAL, " cannot set IOMMU type, "
206 "error %i (%s)\n", errno, strerror(errno));
210 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
211 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
212 struct vfio_iommu_type1_dma_map dma_map;
214 if (ms[i].addr == NULL)
217 memset(&dma_map, 0, sizeof(dma_map));
218 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
219 dma_map.vaddr = ms[i].addr_64;
220 dma_map.size = ms[i].len;
221 dma_map.iova = ms[i].phys_addr;
222 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
224 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
227 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
228 "error %i (%s)\n", errno, strerror(errno));
236 /* set up interrupt support (but not enable interrupts) */
238 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
240 int i, ret, intr_idx;
242 /* default to invalid index */
243 intr_idx = VFIO_PCI_NUM_IRQS;
245 /* get interrupt type from internal config (MSI-X by default, can be
246 * overriden from the command line
248 switch (internal_config.vfio_intr_mode) {
249 case RTE_INTR_MODE_MSIX:
250 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
252 case RTE_INTR_MODE_MSI:
253 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
255 case RTE_INTR_MODE_LEGACY:
256 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
258 /* don't do anything if we want to automatically determine interrupt type */
259 case RTE_INTR_MODE_NONE:
262 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
266 /* start from MSI-X interrupt type */
267 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
268 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
271 /* skip interrupt modes we don't want */
272 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
278 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
280 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
281 "error %i (%s)\n", errno, strerror(errno));
285 /* if this vector cannot be used with eventfd, fail if we explicitly
286 * specified interrupt type, otherwise continue */
287 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
288 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
290 " interrupt vector does not support eventfd!\n");
296 /* set up an eventfd for interrupts */
297 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
299 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
300 "error %i (%s)\n", errno, strerror(errno));
304 dev->intr_handle.fd = fd;
305 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
308 case VFIO_PCI_MSIX_IRQ_INDEX:
309 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
310 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
312 case VFIO_PCI_MSI_IRQ_INDEX:
313 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
314 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
316 case VFIO_PCI_INTX_IRQ_INDEX:
317 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
318 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
321 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
328 /* if we're here, we haven't found a suitable interrupt vector */
332 /* open container fd or get an existing one */
334 pci_vfio_get_container_fd(void)
336 int ret, vfio_container_fd;
338 /* if we're in a primary process, try to open the container */
339 if (internal_config.process_type == RTE_PROC_PRIMARY) {
340 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
341 if (vfio_container_fd < 0) {
342 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
343 "error %i (%s)\n", errno, strerror(errno));
347 /* check VFIO API version */
348 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
349 if (ret != VFIO_API_VERSION) {
351 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
352 "error %i (%s)\n", errno, strerror(errno));
354 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
355 close(vfio_container_fd);
359 /* check if we support IOMMU type 1 */
360 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
363 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
364 "error %i (%s)\n", errno,
367 RTE_LOG(ERR, EAL, " unsupported IOMMU type "
368 "detected in VFIO\n");
369 close(vfio_container_fd);
373 return vfio_container_fd;
376 * if we're in a secondary process, request container fd from the
377 * primary process via our socket
381 socket_fd = vfio_mp_sync_connect_to_primary();
383 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
386 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
387 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
391 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
392 if (vfio_container_fd < 0) {
393 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
398 return vfio_container_fd;
404 /* open group fd or get an existing one */
406 pci_vfio_get_group_fd(int iommu_group_no)
410 char filename[PATH_MAX];
412 /* check if we already have the group descriptor open */
413 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
414 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
415 return vfio_cfg.vfio_groups[i].fd;
417 /* if primary, try to open the group */
418 if (internal_config.process_type == RTE_PROC_PRIMARY) {
419 snprintf(filename, sizeof(filename),
420 VFIO_GROUP_FMT, iommu_group_no);
421 vfio_group_fd = open(filename, O_RDWR);
422 if (vfio_group_fd < 0) {
423 /* if file not found, it's not an error */
424 if (errno != ENOENT) {
425 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
432 /* if the fd is valid, create a new group for it */
433 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
434 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
437 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
438 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
439 return vfio_group_fd;
441 /* if we're in a secondary process, request group fd from the primary
442 * process via our socket
447 socket_fd = vfio_mp_sync_connect_to_primary();
450 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
453 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
454 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
458 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
459 RTE_LOG(ERR, EAL, " cannot send group number!\n");
463 ret = vfio_mp_sync_receive_request(socket_fd);
469 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
470 /* if we got the fd, return it */
471 if (vfio_group_fd > 0) {
473 return vfio_group_fd;
475 /* fall-through on error */
477 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
485 /* parse IOMMU group number for a PCI device
486 * returns -1 for errors, 0 for non-existent group */
488 pci_vfio_get_group_no(const char *pci_addr)
490 char linkname[PATH_MAX];
491 char filename[PATH_MAX];
492 char *tok[16], *group_tok, *end;
493 int ret, iommu_group_no;
495 memset(linkname, 0, sizeof(linkname));
496 memset(filename, 0, sizeof(filename));
498 /* try to find out IOMMU group for this device */
499 snprintf(linkname, sizeof(linkname),
500 SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
502 ret = readlink(linkname, filename, sizeof(filename));
504 /* if the link doesn't exist, no VFIO for us */
508 ret = rte_strsplit(filename, sizeof(filename),
509 tok, RTE_DIM(tok), '/');
512 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", pci_addr);
516 /* IOMMU group is always the last token */
518 group_tok = tok[ret - 1];
520 iommu_group_no = strtol(group_tok, &end, 10);
521 if ((end != group_tok && *end != '\0') || errno != 0) {
522 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", pci_addr);
526 return iommu_group_no;
530 clear_current_group(void)
532 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
533 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
538 * map the PCI resources of a PCI device in virtual memory (VFIO version).
539 * primary and secondary processes follow almost exactly the same path
542 pci_vfio_map_resource(struct rte_pci_device *dev)
544 struct vfio_group_status group_status = {
545 .argsz = sizeof(group_status)
547 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
548 int vfio_group_fd, vfio_dev_fd;
550 char pci_addr[PATH_MAX] = {0};
551 struct rte_pci_addr *loc = &dev->addr;
552 int i, ret, msix_bar;
553 struct mapped_pci_resource *vfio_res = NULL;
554 struct mapped_pci_res_list *vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
556 struct pci_map *maps;
557 uint32_t msix_table_offset = 0;
558 uint32_t msix_table_size = 0;
560 dev->intr_handle.fd = -1;
561 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
563 /* store PCI address string */
564 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
565 loc->domain, loc->bus, loc->devid, loc->function);
567 /* get group number */
568 iommu_group_no = pci_vfio_get_group_no(pci_addr);
570 /* if 0, group doesn't exist */
571 if (iommu_group_no == 0) {
572 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
576 /* if negative, something failed */
577 else if (iommu_group_no < 0)
580 /* get the actual group fd */
581 vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
582 if (vfio_group_fd < 0)
586 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
587 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
589 /* if group_fd == 0, that means the device isn't managed by VFIO */
590 if (vfio_group_fd == 0) {
591 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
593 /* we store 0 as group fd to distinguish between existing but
594 * unbound VFIO groups, and groups that don't exist at all.
596 vfio_cfg.vfio_group_idx++;
601 * at this point, we know at least one port on this device is bound to VFIO,
602 * so we can proceed to try and set this particular port up
605 /* check if the group is viable */
606 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
608 RTE_LOG(ERR, EAL, " %s cannot get group status, "
609 "error %i (%s)\n", pci_addr, errno, strerror(errno));
610 close(vfio_group_fd);
611 clear_current_group();
613 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
614 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", pci_addr);
615 close(vfio_group_fd);
616 clear_current_group();
621 * at this point, we know that this group is viable (meaning, all devices
622 * are either bound to VFIO or not bound to anything)
625 /* check if group does not have a container yet */
626 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
628 /* add group to a container */
629 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
630 &vfio_cfg.vfio_container_fd);
632 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
633 "error %i (%s)\n", pci_addr, errno, strerror(errno));
634 close(vfio_group_fd);
635 clear_current_group();
639 * at this point we know that this group has been successfully
640 * initialized, so we increment vfio_group_idx to indicate that we can
643 vfio_cfg.vfio_group_idx++;
647 * set up DMA mappings for container
649 * needs to be done only once, only when at least one group is assigned to
650 * a container and only in primary process
652 if (internal_config.process_type == RTE_PROC_PRIMARY &&
653 vfio_cfg.vfio_container_has_dma == 0) {
654 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
656 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
657 "error %i (%s)\n", pci_addr, errno, strerror(errno));
660 vfio_cfg.vfio_container_has_dma = 1;
663 /* get a file descriptor for the device */
664 vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
665 if (vfio_dev_fd < 0) {
666 /* if we cannot get a device fd, this simply means that this
667 * particular port is not bound to VFIO
669 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
674 /* test and setup the device */
675 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
677 RTE_LOG(ERR, EAL, " %s cannot get device info, "
678 "error %i (%s)\n", pci_addr, errno, strerror(errno));
683 /* get MSI-X BAR, if any (we have to know where it is because we can't
684 * easily mmap it when using VFIO) */
686 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
687 &msix_table_offset, &msix_table_size);
689 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n", pci_addr);
694 /* if we're in a primary process, allocate vfio_res and get region info */
695 if (internal_config.process_type == RTE_PROC_PRIMARY) {
696 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
697 if (vfio_res == NULL) {
699 "%s(): cannot store uio mmap details\n", __func__);
703 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
705 /* get number of registers (up to BAR5) */
706 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
707 VFIO_PCI_BAR5_REGION_INDEX + 1);
709 /* if we're in a secondary process, just find our tailq entry */
710 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
711 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
715 /* if we haven't found our tailq entry, something's wrong */
716 if (vfio_res == NULL) {
717 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
725 maps = vfio_res->maps;
727 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
728 struct vfio_region_info reg = { .argsz = sizeof(reg) };
731 uint32_t offset, size;
736 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
739 RTE_LOG(ERR, EAL, " %s cannot get device region info "
740 "error %i (%s)\n", pci_addr, errno, strerror(errno));
742 if (internal_config.process_type == RTE_PROC_PRIMARY)
747 /* skip non-mmapable BARs */
748 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
753 * VFIO will not let us map the MSI-X table,
754 * but we can map around it.
756 uint32_t table_start = msix_table_offset;
757 uint32_t table_end = table_start + msix_table_size;
758 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
759 table_start &= PAGE_MASK;
761 if (table_start == 0 && table_end >= reg.size) {
762 /* Cannot map this BAR */
763 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
766 memreg[0].offset = reg.offset;
767 memreg[0].size = table_start;
768 memreg[1].offset = table_end;
769 memreg[1].size = reg.size - table_end;
772 "Trying to map BAR %d that contains the MSI-X "
773 "table. Trying offsets: "
774 "%04x:%04x, %04x:%04x\n", i,
775 memreg[0].offset, memreg[0].size,
776 memreg[1].offset, memreg[1].size);
779 memreg[0].offset = reg.offset;
780 memreg[0].size = reg.size;
783 /* try to figure out an address */
784 if (internal_config.process_type == RTE_PROC_PRIMARY) {
785 /* try mapping somewhere close to the end of hugepages */
786 if (pci_map_addr == NULL)
787 pci_map_addr = pci_find_max_end_va();
789 bar_addr = pci_map_addr;
790 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
792 bar_addr = maps[i].addr;
795 /* reserve the address using an inaccessible mapping */
796 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
797 MAP_ANONYMOUS, -1, 0);
798 if (bar_addr != MAP_FAILED) {
799 void *map_addr = NULL;
800 if (memreg[0].size) {
801 /* actual map of first part */
802 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
808 /* if there's a second part, try to map it */
809 if (map_addr != MAP_FAILED
810 && memreg[1].offset && memreg[1].size) {
811 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
812 map_addr = pci_map_resource(second_addr,
813 vfio_dev_fd, memreg[1].offset,
818 if (map_addr == MAP_FAILED || !map_addr) {
819 munmap(bar_addr, reg.size);
820 bar_addr = MAP_FAILED;
824 if (bar_addr == MAP_FAILED ||
825 (internal_config.process_type == RTE_PROC_SECONDARY &&
826 bar_addr != maps[i].addr)) {
827 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n", pci_addr, i,
830 if (internal_config.process_type == RTE_PROC_PRIMARY)
835 maps[i].addr = bar_addr;
836 maps[i].offset = reg.offset;
837 maps[i].size = reg.size;
838 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
839 dev->mem_resource[i].addr = bar_addr;
842 /* if secondary process, do not set up interrupts */
843 if (internal_config.process_type == RTE_PROC_PRIMARY) {
844 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
845 RTE_LOG(ERR, EAL, " %s error setting up interrupts!\n", pci_addr);
851 /* set bus mastering for the device */
852 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
853 RTE_LOG(ERR, EAL, " %s cannot set up bus mastering!\n", pci_addr);
859 /* Reset the device */
860 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
863 if (internal_config.process_type == RTE_PROC_PRIMARY)
864 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
870 pci_vfio_enable(void)
872 /* initialize group list */
874 int module_vfio_type1;
876 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
877 vfio_cfg.vfio_groups[i].fd = -1;
878 vfio_cfg.vfio_groups[i].group_no = -1;
881 module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
883 /* return error directly */
884 if (module_vfio_type1 == -1) {
885 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
889 /* return 0 if VFIO modules not loaded */
890 if (module_vfio_type1 == 0) {
891 RTE_LOG(INFO, EAL, "VFIO modules not all loaded, "
892 "skip VFIO support...\n");
896 vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
898 /* check if we have VFIO driver enabled */
899 if (vfio_cfg.vfio_container_fd != -1)
900 vfio_cfg.vfio_enabled = 1;
902 RTE_LOG(INFO, EAL, "VFIO support could not be initialized\n");
908 pci_vfio_is_enabled(void)
910 return vfio_cfg.vfio_enabled;