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>
47 #include "eal_filesystem.h"
48 #include "eal_pci_init.h"
50 #include "eal_private.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 /* per-process VFIO config */
73 static struct vfio_config vfio_cfg;
76 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
77 void *buf, size_t len, off_t offs)
79 return pread64(intr_handle->vfio_dev_fd, buf, len,
80 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
84 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
85 const void *buf, size_t len, off_t offs)
87 return pwrite64(intr_handle->vfio_dev_fd, buf, len,
88 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
91 /* get PCI BAR number where MSI-X interrupts are */
93 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
94 uint32_t *msix_table_size)
99 uint8_t cap_id, cap_offset;
101 /* read PCI capability pointer from config space */
102 ret = pread64(fd, ®, sizeof(reg),
103 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
104 PCI_CAPABILITY_LIST);
105 if (ret != sizeof(reg)) {
106 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
111 /* we need first byte */
112 cap_offset = reg & 0xFF;
116 /* read PCI capability ID */
117 ret = pread64(fd, ®, sizeof(reg),
118 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
120 if (ret != sizeof(reg)) {
121 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
126 /* we need first byte */
129 /* if we haven't reached MSI-X, check next capability */
130 if (cap_id != PCI_CAP_ID_MSIX) {
131 ret = pread64(fd, ®, sizeof(reg),
132 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
134 if (ret != sizeof(reg)) {
135 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
140 /* we need second byte */
141 cap_offset = (reg & 0xFF00) >> 8;
145 /* else, read table offset */
147 /* table offset resides in the next 4 bytes */
148 ret = pread64(fd, ®, sizeof(reg),
149 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
151 if (ret != sizeof(reg)) {
152 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
157 ret = pread64(fd, &flags, sizeof(flags),
158 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
160 if (ret != sizeof(flags)) {
161 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
166 *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
167 *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
168 *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
176 /* set PCI bus mastering */
178 pci_vfio_set_bus_master(int dev_fd)
183 ret = pread64(dev_fd, ®, sizeof(reg),
184 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
186 if (ret != sizeof(reg)) {
187 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
191 /* set the master bit */
192 reg |= PCI_COMMAND_MASTER;
194 ret = pwrite64(dev_fd, ®, sizeof(reg),
195 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
198 if (ret != sizeof(reg)) {
199 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
206 /* set up interrupt support (but not enable interrupts) */
208 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
210 int i, ret, intr_idx;
212 /* default to invalid index */
213 intr_idx = VFIO_PCI_NUM_IRQS;
215 /* get interrupt type from internal config (MSI-X by default, can be
216 * overriden from the command line
218 switch (internal_config.vfio_intr_mode) {
219 case RTE_INTR_MODE_MSIX:
220 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
222 case RTE_INTR_MODE_MSI:
223 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
225 case RTE_INTR_MODE_LEGACY:
226 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
228 /* don't do anything if we want to automatically determine interrupt type */
229 case RTE_INTR_MODE_NONE:
232 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
236 /* start from MSI-X interrupt type */
237 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
238 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
241 /* skip interrupt modes we don't want */
242 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
248 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
250 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
251 "error %i (%s)\n", errno, strerror(errno));
255 /* if this vector cannot be used with eventfd, fail if we explicitly
256 * specified interrupt type, otherwise continue */
257 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
258 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
260 " interrupt vector does not support eventfd!\n");
266 /* set up an eventfd for interrupts */
267 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
269 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
270 "error %i (%s)\n", errno, strerror(errno));
274 dev->intr_handle.fd = fd;
275 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
278 case VFIO_PCI_MSIX_IRQ_INDEX:
279 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
280 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
282 case VFIO_PCI_MSI_IRQ_INDEX:
283 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
284 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
286 case VFIO_PCI_INTX_IRQ_INDEX:
287 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
288 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
291 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
298 /* if we're here, we haven't found a suitable interrupt vector */
302 /* open group fd or get an existing one */
304 pci_vfio_get_group_fd(int iommu_group_no)
308 char filename[PATH_MAX];
310 /* check if we already have the group descriptor open */
311 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
312 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
313 return vfio_cfg.vfio_groups[i].fd;
315 /* if primary, try to open the group */
316 if (internal_config.process_type == RTE_PROC_PRIMARY) {
317 /* try regular group format */
318 snprintf(filename, sizeof(filename),
319 VFIO_GROUP_FMT, iommu_group_no);
320 vfio_group_fd = open(filename, O_RDWR);
321 if (vfio_group_fd < 0) {
322 /* if file not found, it's not an error */
323 if (errno != ENOENT) {
324 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
329 /* special case: try no-IOMMU path as well */
330 snprintf(filename, sizeof(filename),
331 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
332 vfio_group_fd = open(filename, O_RDWR);
333 if (vfio_group_fd < 0) {
334 if (errno != ENOENT) {
335 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
341 /* noiommu group found */
344 /* if the fd is valid, create a new group for it */
345 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
346 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
347 close(vfio_group_fd);
350 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
351 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
352 return vfio_group_fd;
354 /* if we're in a secondary process, request group fd from the primary
355 * process via our socket
360 socket_fd = vfio_mp_sync_connect_to_primary();
363 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
366 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
367 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
371 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
372 RTE_LOG(ERR, EAL, " cannot send group number!\n");
376 ret = vfio_mp_sync_receive_request(socket_fd);
382 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
383 /* if we got the fd, return it */
384 if (vfio_group_fd > 0) {
386 return vfio_group_fd;
388 /* fall-through on error */
390 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
398 /* parse IOMMU group number for a PCI device
399 * returns 1 on success, -1 for errors, 0 for non-existent group
402 pci_vfio_get_group_no(const char *pci_addr, int *iommu_group_no)
404 return vfio_get_group_no(pci_get_sysfs_path(), pci_addr, iommu_group_no);
408 clear_current_group(void)
410 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
411 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
415 * Setup vfio_cfg for the device indentified by its address. It discovers
416 * the configured I/O MMU groups or sets a new one for the device. If a new
417 * groups is assigned, the DMA mapping is performed.
418 * Returns 0 on success, a negative value on failure and a positive value in
419 * case the given device cannot be managed this way.
421 static int pci_vfio_setup_device(const char *pci_addr, int *vfio_dev_fd,
422 struct vfio_device_info *device_info)
424 struct vfio_group_status group_status = {
425 .argsz = sizeof(group_status)
431 /* get group number */
432 ret = pci_vfio_get_group_no(pci_addr, &iommu_group_no);
434 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
439 /* if negative, something failed */
443 /* get the actual group fd */
444 vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
445 if (vfio_group_fd < 0)
449 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
450 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
452 /* if group_fd == 0, that means the device isn't managed by VFIO */
453 if (vfio_group_fd == 0) {
454 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
456 /* we store 0 as group fd to distinguish between existing but
457 * unbound VFIO groups, and groups that don't exist at all.
459 vfio_cfg.vfio_group_idx++;
464 * at this point, we know that this group is viable (meaning, all devices
465 * are either bound to VFIO or not bound to anything)
468 /* check if the group is viable */
469 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
471 RTE_LOG(ERR, EAL, " %s cannot get group status, "
472 "error %i (%s)\n", pci_addr, errno, strerror(errno));
473 close(vfio_group_fd);
474 clear_current_group();
476 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
477 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", pci_addr);
478 close(vfio_group_fd);
479 clear_current_group();
483 /* check if group does not have a container yet */
484 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
486 /* add group to a container */
487 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
488 &vfio_cfg.vfio_container_fd);
490 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
491 "error %i (%s)\n", pci_addr, errno, strerror(errno));
492 close(vfio_group_fd);
493 clear_current_group();
497 * at this point we know that this group has been successfully
498 * initialized, so we increment vfio_group_idx to indicate that we can
501 vfio_cfg.vfio_group_idx++;
505 * pick an IOMMU type and set up DMA mappings for container
507 * needs to be done only once, only when at least one group is assigned to
508 * a container and only in primary process
510 if (internal_config.process_type == RTE_PROC_PRIMARY &&
511 vfio_cfg.vfio_container_has_dma == 0) {
512 /* select an IOMMU type which we will be using */
513 const struct vfio_iommu_type *t =
514 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
516 RTE_LOG(ERR, EAL, " %s failed to select IOMMU type\n", pci_addr);
519 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
521 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
522 "error %i (%s)\n", pci_addr, errno, strerror(errno));
525 vfio_cfg.vfio_container_has_dma = 1;
528 /* get a file descriptor for the device */
529 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
530 if (*vfio_dev_fd < 0) {
531 /* if we cannot get a device fd, this simply means that this
532 * particular port is not bound to VFIO
534 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
539 /* test and setup the device */
540 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
542 RTE_LOG(ERR, EAL, " %s cannot get device info, "
543 "error %i (%s)\n", pci_addr, errno, strerror(errno));
552 * map the PCI resources of a PCI device in virtual memory (VFIO version).
553 * primary and secondary processes follow almost exactly the same path
556 pci_vfio_map_resource(struct rte_pci_device *dev)
558 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
559 char pci_addr[PATH_MAX] = {0};
561 struct rte_pci_addr *loc = &dev->addr;
562 int i, ret, msix_bar;
563 struct mapped_pci_resource *vfio_res = NULL;
564 struct mapped_pci_res_list *vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
566 struct pci_map *maps;
567 uint32_t msix_table_offset = 0;
568 uint32_t msix_table_size = 0;
571 dev->intr_handle.fd = -1;
572 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
574 /* store PCI address string */
575 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
576 loc->domain, loc->bus, loc->devid, loc->function);
578 if ((ret = pci_vfio_setup_device(pci_addr, &vfio_dev_fd, &device_info)))
581 /* get MSI-X BAR, if any (we have to know where it is because we can't
582 * easily mmap it when using VFIO) */
584 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
585 &msix_table_offset, &msix_table_size);
587 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n", pci_addr);
592 /* if we're in a primary process, allocate vfio_res and get region info */
593 if (internal_config.process_type == RTE_PROC_PRIMARY) {
594 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
595 if (vfio_res == NULL) {
597 "%s(): cannot store uio mmap details\n", __func__);
601 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
603 /* get number of registers (up to BAR5) */
604 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
605 VFIO_PCI_BAR5_REGION_INDEX + 1);
607 /* if we're in a secondary process, just find our tailq entry */
608 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
609 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
613 /* if we haven't found our tailq entry, something's wrong */
614 if (vfio_res == NULL) {
615 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
623 maps = vfio_res->maps;
625 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
626 struct vfio_region_info reg = { .argsz = sizeof(reg) };
629 unsigned long offset, size;
634 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
637 RTE_LOG(ERR, EAL, " %s cannot get device region info "
638 "error %i (%s)\n", pci_addr, errno, strerror(errno));
640 if (internal_config.process_type == RTE_PROC_PRIMARY)
645 /* chk for io port region */
646 ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
647 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
648 + PCI_BASE_ADDRESS_0 + i*4);
650 if (ret != sizeof(ioport_bar)) {
652 "Cannot read command (%x) from config space!\n",
653 PCI_BASE_ADDRESS_0 + i*4);
657 if (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) {
659 "Ignore mapping IO port bar(%d) addr: %x\n",
664 /* skip non-mmapable BARs */
665 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
670 * VFIO will not let us map the MSI-X table,
671 * but we can map around it.
673 uint32_t table_start = msix_table_offset;
674 uint32_t table_end = table_start + msix_table_size;
675 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
676 table_start &= PAGE_MASK;
678 if (table_start == 0 && table_end >= reg.size) {
679 /* Cannot map this BAR */
680 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
683 memreg[0].offset = reg.offset;
684 memreg[0].size = table_start;
685 memreg[1].offset = table_end;
686 memreg[1].size = reg.size - table_end;
689 "Trying to map BAR %d that contains the MSI-X "
690 "table. Trying offsets: "
691 "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", i,
692 memreg[0].offset, memreg[0].size,
693 memreg[1].offset, memreg[1].size);
696 memreg[0].offset = reg.offset;
697 memreg[0].size = reg.size;
700 /* try to figure out an address */
701 if (internal_config.process_type == RTE_PROC_PRIMARY) {
702 /* try mapping somewhere close to the end of hugepages */
703 if (pci_map_addr == NULL)
704 pci_map_addr = pci_find_max_end_va();
706 bar_addr = pci_map_addr;
707 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
709 bar_addr = maps[i].addr;
712 /* reserve the address using an inaccessible mapping */
713 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
714 MAP_ANONYMOUS, -1, 0);
715 if (bar_addr != MAP_FAILED) {
716 void *map_addr = NULL;
717 if (memreg[0].size) {
718 /* actual map of first part */
719 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
725 /* if there's a second part, try to map it */
726 if (map_addr != MAP_FAILED
727 && memreg[1].offset && memreg[1].size) {
728 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
729 map_addr = pci_map_resource(second_addr,
730 vfio_dev_fd, memreg[1].offset,
735 if (map_addr == MAP_FAILED || !map_addr) {
736 munmap(bar_addr, reg.size);
737 bar_addr = MAP_FAILED;
741 if (bar_addr == MAP_FAILED ||
742 (internal_config.process_type == RTE_PROC_SECONDARY &&
743 bar_addr != maps[i].addr)) {
744 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n", pci_addr, i,
747 if (internal_config.process_type == RTE_PROC_PRIMARY)
752 maps[i].addr = bar_addr;
753 maps[i].offset = reg.offset;
754 maps[i].size = reg.size;
755 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
756 dev->mem_resource[i].addr = bar_addr;
759 /* if secondary process, do not set up interrupts */
760 if (internal_config.process_type == RTE_PROC_PRIMARY) {
761 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
762 RTE_LOG(ERR, EAL, " %s error setting up interrupts!\n", pci_addr);
768 /* set bus mastering for the device */
769 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
770 RTE_LOG(ERR, EAL, " %s cannot set up bus mastering!\n", pci_addr);
776 /* Reset the device */
777 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
780 if (internal_config.process_type == RTE_PROC_PRIMARY)
781 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
787 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
788 struct rte_pci_ioport *p)
790 if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
791 bar > VFIO_PCI_BAR5_REGION_INDEX) {
792 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
797 p->base = VFIO_GET_REGION_ADDR(bar);
802 pci_vfio_ioport_read(struct rte_pci_ioport *p,
803 void *data, size_t len, off_t offset)
805 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
807 if (pread64(intr_handle->vfio_dev_fd, data,
808 len, p->base + offset) <= 0)
810 "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
811 VFIO_GET_REGION_IDX(p->base), (int)offset);
815 pci_vfio_ioport_write(struct rte_pci_ioport *p,
816 const void *data, size_t len, off_t offset)
818 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
820 if (pwrite64(intr_handle->vfio_dev_fd, data,
821 len, p->base + offset) <= 0)
823 "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
824 VFIO_GET_REGION_IDX(p->base), (int)offset);
828 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
835 pci_vfio_enable(void)
837 /* initialize group list */
841 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
842 vfio_cfg.vfio_groups[i].fd = -1;
843 vfio_cfg.vfio_groups[i].group_no = -1;
846 /* inform the user that we are probing for VFIO */
847 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
849 /* check if vfio-pci module is loaded */
850 vfio_available = rte_eal_check_module("vfio_pci");
852 /* return error directly */
853 if (vfio_available == -1) {
854 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
858 /* return 0 if VFIO modules not loaded */
859 if (vfio_available == 0) {
860 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
861 "skipping VFIO support...\n");
865 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
867 /* check if we have VFIO driver enabled */
868 if (vfio_cfg.vfio_container_fd != -1) {
869 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
870 vfio_cfg.vfio_enabled = 1;
872 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
879 pci_vfio_is_enabled(void)
881 return vfio_cfg.vfio_enabled;