1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <linux/pci_regs.h>
8 #include <sys/eventfd.h>
9 #include <sys/socket.h>
10 #include <sys/ioctl.h>
16 #include <rte_bus_pci.h>
17 #include <rte_eal_paging.h>
18 #include <rte_malloc.h>
22 #include <rte_spinlock.h>
23 #include <rte_tailq.h>
25 #include "eal_filesystem.h"
32 * PCI probing under linux (VFIO version)
34 * This code tries to determine if the PCI device is bound to VFIO driver,
35 * and initialize it (map BARs, set up interrupts) if that's the case.
41 static struct rte_tailq_elem rte_vfio_tailq = {
42 .name = "VFIO_RESOURCE_LIST",
44 EAL_REGISTER_TAILQ(rte_vfio_tailq)
47 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
48 void *buf, size_t len, off_t offs)
50 return pread64(intr_handle->vfio_dev_fd, buf, len,
51 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
55 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
56 const void *buf, size_t len, off_t offs)
58 return pwrite64(intr_handle->vfio_dev_fd, buf, len,
59 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
62 /* get PCI BAR number where MSI-X interrupts are */
64 pci_vfio_get_msix_bar(int fd, struct pci_msix_table *msix_table)
69 uint8_t cap_id, cap_offset;
71 /* read PCI capability pointer from config space */
72 ret = pread64(fd, ®, sizeof(reg),
73 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
75 if (ret != sizeof(reg)) {
77 "Cannot read capability pointer from PCI config space!\n");
81 /* we need first byte */
82 cap_offset = reg & 0xFF;
86 /* read PCI capability ID */
87 ret = pread64(fd, ®, sizeof(reg),
88 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
90 if (ret != sizeof(reg)) {
92 "Cannot read capability ID from PCI config space!\n");
96 /* we need first byte */
99 /* if we haven't reached MSI-X, check next capability */
100 if (cap_id != PCI_CAP_ID_MSIX) {
101 ret = pread64(fd, ®, sizeof(reg),
102 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
104 if (ret != sizeof(reg)) {
106 "Cannot read capability pointer from PCI config space!\n");
110 /* we need second byte */
111 cap_offset = (reg & 0xFF00) >> 8;
115 /* else, read table offset */
117 /* table offset resides in the next 4 bytes */
118 ret = pread64(fd, ®, sizeof(reg),
119 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
121 if (ret != sizeof(reg)) {
123 "Cannot read table offset from PCI config space!\n");
127 ret = pread64(fd, &flags, sizeof(flags),
128 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
130 if (ret != sizeof(flags)) {
132 "Cannot read table flags from PCI config space!\n");
136 msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
137 msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
139 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
147 /* enable PCI bus memory space */
149 pci_vfio_enable_bus_memory(int dev_fd)
154 ret = pread64(dev_fd, &cmd, sizeof(cmd),
155 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
158 if (ret != sizeof(cmd)) {
159 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
163 if (cmd & PCI_COMMAND_MEMORY)
166 cmd |= PCI_COMMAND_MEMORY;
167 ret = pwrite64(dev_fd, &cmd, sizeof(cmd),
168 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
171 if (ret != sizeof(cmd)) {
172 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
179 /* set PCI bus mastering */
181 pci_vfio_set_bus_master(int dev_fd, bool op)
186 ret = pread64(dev_fd, ®, sizeof(reg),
187 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
189 if (ret != sizeof(reg)) {
190 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
195 /* set the master bit */
196 reg |= PCI_COMMAND_MASTER;
198 reg &= ~(PCI_COMMAND_MASTER);
200 ret = pwrite64(dev_fd, ®, sizeof(reg),
201 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
204 if (ret != sizeof(reg)) {
205 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
212 /* set up interrupt support (but not enable interrupts) */
214 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
216 int i, ret, intr_idx;
217 enum rte_intr_mode intr_mode;
219 /* default to invalid index */
220 intr_idx = VFIO_PCI_NUM_IRQS;
222 /* Get default / configured intr_mode */
223 intr_mode = rte_eal_vfio_intr_mode();
225 /* get interrupt type from internal config (MSI-X by default, can be
226 * overridden from the command line
229 case RTE_INTR_MODE_MSIX:
230 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
232 case RTE_INTR_MODE_MSI:
233 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
235 case RTE_INTR_MODE_LEGACY:
236 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
238 /* don't do anything if we want to automatically determine interrupt type */
239 case RTE_INTR_MODE_NONE:
242 RTE_LOG(ERR, EAL, "Unknown default interrupt type!\n");
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) };
251 /* skip interrupt modes we don't want */
252 if (intr_mode != RTE_INTR_MODE_NONE &&
258 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
260 RTE_LOG(ERR, EAL, "Cannot get VFIO IRQ info, error "
261 "%i (%s)\n", errno, strerror(errno));
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 (intr_mode != RTE_INTR_MODE_NONE) {
270 "Interrupt vector does not support eventfd!\n");
276 /* set up an eventfd for interrupts */
277 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
279 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error "
280 "%i (%s)\n", errno, strerror(errno));
284 dev->intr_handle.fd = fd;
285 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
288 case VFIO_PCI_MSIX_IRQ_INDEX:
289 intr_mode = RTE_INTR_MODE_MSIX;
290 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
292 case VFIO_PCI_MSI_IRQ_INDEX:
293 intr_mode = RTE_INTR_MODE_MSI;
294 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
296 case VFIO_PCI_INTX_IRQ_INDEX:
297 intr_mode = RTE_INTR_MODE_LEGACY;
298 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
301 RTE_LOG(ERR, EAL, "Unknown interrupt type!\n");
308 /* if we're here, we haven't found a suitable interrupt vector */
312 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
314 * Spinlock for device hot-unplug failure handling.
315 * If it tries to access bus or device, such as handle sigbus on bus
316 * or handle memory failure for device, just need to use this lock.
317 * It could protect the bus and the device to avoid race condition.
319 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
322 pci_vfio_req_handler(void *param)
326 struct rte_device *device = (struct rte_device *)param;
328 rte_spinlock_lock(&failure_handle_lock);
329 bus = rte_bus_find_by_device(device);
331 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
337 * vfio kernel module request user space to release allocated
338 * resources before device be deleted in kernel, so it can directly
339 * call the vfio bus hot-unplug handler to process it.
341 ret = bus->hot_unplug_handler(device);
344 "Can not handle hot-unplug for device (%s)\n",
347 rte_spinlock_unlock(&failure_handle_lock);
350 /* enable notifier (only enable req now) */
352 pci_vfio_enable_notifier(struct rte_pci_device *dev, int vfio_dev_fd)
357 /* set up an eventfd for req notifier */
358 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
360 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error %i (%s)\n",
361 errno, strerror(errno));
365 dev->vfio_req_intr_handle.fd = fd;
366 dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_VFIO_REQ;
367 dev->vfio_req_intr_handle.vfio_dev_fd = vfio_dev_fd;
369 ret = rte_intr_callback_register(&dev->vfio_req_intr_handle,
370 pci_vfio_req_handler,
371 (void *)&dev->device);
373 RTE_LOG(ERR, EAL, "Fail to register req notifier handler.\n");
377 ret = rte_intr_enable(&dev->vfio_req_intr_handle);
379 RTE_LOG(ERR, EAL, "Fail to enable req notifier.\n");
380 ret = rte_intr_callback_unregister(&dev->vfio_req_intr_handle,
381 pci_vfio_req_handler,
382 (void *)&dev->device);
385 "Fail to unregister req notifier handler.\n");
393 dev->vfio_req_intr_handle.fd = -1;
394 dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
395 dev->vfio_req_intr_handle.vfio_dev_fd = -1;
400 /* disable notifier (only disable req now) */
402 pci_vfio_disable_notifier(struct rte_pci_device *dev)
406 ret = rte_intr_disable(&dev->vfio_req_intr_handle);
408 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
412 ret = rte_intr_callback_unregister_sync(&dev->vfio_req_intr_handle,
413 pci_vfio_req_handler,
414 (void *)&dev->device);
417 "fail to unregister req notifier handler.\n");
421 close(dev->vfio_req_intr_handle.fd);
423 dev->vfio_req_intr_handle.fd = -1;
424 dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
425 dev->vfio_req_intr_handle.vfio_dev_fd = -1;
432 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
437 ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
438 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
439 + PCI_BASE_ADDRESS_0 + bar_index*4);
440 if (ret != sizeof(ioport_bar)) {
441 RTE_LOG(ERR, EAL, "Cannot read command (%x) from config space!\n",
442 PCI_BASE_ADDRESS_0 + bar_index*4);
446 return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
450 pci_rte_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
452 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
453 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
457 if (pci_vfio_enable_bus_memory(vfio_dev_fd)) {
458 RTE_LOG(ERR, EAL, "Cannot enable bus memory!\n");
462 /* set bus mastering for the device */
463 if (pci_vfio_set_bus_master(vfio_dev_fd, true)) {
464 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
469 * Reset the device. If the device is not capable of resetting,
470 * then it updates errno as EINVAL.
472 if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
473 RTE_LOG(ERR, EAL, "Unable to reset device! Error: %d (%s)\n",
474 errno, strerror(errno));
482 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
483 int bar_index, int additional_flags)
490 struct pci_msix_table *msix_table = &vfio_res->msix_table;
491 struct pci_map *bar = &vfio_res->maps[bar_index];
493 if (bar->size == 0) {
494 RTE_LOG(DEBUG, EAL, "Bar size is 0, skip BAR%d\n", bar_index);
498 if (msix_table->bar_index == bar_index) {
500 * VFIO will not let us map the MSI-X table,
501 * but we can map around it.
503 uint32_t table_start = msix_table->offset;
504 uint32_t table_end = table_start + msix_table->size;
505 table_end = RTE_ALIGN(table_end, rte_mem_page_size());
506 table_start = RTE_ALIGN_FLOOR(table_start, rte_mem_page_size());
508 /* If page-aligned start of MSI-X table is less than the
509 * actual MSI-X table start address, reassign to the actual
512 if (table_start < msix_table->offset)
513 table_start = msix_table->offset;
515 if (table_start == 0 && table_end >= bar->size) {
516 /* Cannot map this BAR */
517 RTE_LOG(DEBUG, EAL, "Skipping BAR%d\n", bar_index);
523 memreg[0].offset = bar->offset;
524 memreg[0].size = table_start;
525 if (bar->size < table_end) {
527 * If MSI-X table end is beyond BAR end, don't attempt
528 * to perform second mapping.
530 memreg[1].offset = 0;
533 memreg[1].offset = bar->offset + table_end;
534 memreg[1].size = bar->size - table_end;
538 "Trying to map BAR%d that contains the MSI-X "
539 "table. Trying offsets: "
540 "0x%04" PRIx64 ":0x%04zx, 0x%04" PRIx64 ":0x%04zx\n",
542 memreg[0].offset, memreg[0].size,
543 memreg[1].offset, memreg[1].size);
545 memreg[0].offset = bar->offset;
546 memreg[0].size = bar->size;
549 /* reserve the address using an inaccessible mapping */
550 bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
551 MAP_ANONYMOUS | additional_flags, -1, 0);
552 if (bar_addr != MAP_FAILED) {
553 void *map_addr = NULL;
554 if (memreg[0].size) {
555 /* actual map of first part */
556 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
559 RTE_MAP_FORCE_ADDRESS);
563 * Regarding "memreg[0].size == 0":
564 * If this BAR has MSI-X table, memreg[0].size (the
565 * first part or the part before the table) can
566 * legitimately be 0 for hardware using vector table
567 * offset 0 (i.e. first part does not exist).
569 * When memreg[0].size is 0, "mapping the first part"
570 * never happens, and map_addr is NULL at this
571 * point. So check that mapping has been actually
574 /* if there's a second part, try to map it */
575 if ((map_addr != NULL || memreg[0].size == 0)
576 && memreg[1].offset && memreg[1].size) {
577 void *second_addr = RTE_PTR_ADD(bar_addr,
578 (uintptr_t)(memreg[1].offset -
580 map_addr = pci_map_resource(second_addr,
584 RTE_MAP_FORCE_ADDRESS);
587 if (map_addr == NULL) {
588 munmap(bar_addr, bar->size);
589 bar_addr = MAP_FAILED;
590 RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
596 "Failed to create inaccessible mapping for BAR%d\n",
601 bar->addr = bar_addr;
606 * region info may contain capability headers, so we need to keep reallocating
607 * the memory until we match allocated memory size with argsz.
610 pci_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
613 struct vfio_region_info *ri;
614 size_t argsz = sizeof(*ri);
617 ri = malloc(sizeof(*ri));
620 "Cannot allocate memory for VFIO region info\n");
624 memset(ri, 0, argsz);
628 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ri);
633 if (ri->argsz != argsz) {
634 struct vfio_region_info *tmp;
637 tmp = realloc(ri, argsz);
640 /* realloc failed but the ri is still there */
643 "Cannot reallocate memory for VFIO region info\n");
654 static struct vfio_info_cap_header *
655 pci_vfio_info_cap(struct vfio_region_info *info, int cap)
657 struct vfio_info_cap_header *h;
660 if ((info->flags & RTE_VFIO_INFO_FLAG_CAPS) == 0) {
661 /* VFIO info does not advertise capabilities */
665 offset = VFIO_CAP_OFFSET(info);
666 while (offset != 0) {
667 h = RTE_PTR_ADD(info, offset);
676 pci_vfio_msix_is_mappable(int vfio_dev_fd, int msix_region)
678 struct vfio_region_info *info;
681 ret = pci_vfio_get_region_info(vfio_dev_fd, &info, msix_region);
685 ret = pci_vfio_info_cap(info, RTE_VFIO_CAP_MSIX_MAPPABLE) != NULL;
695 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
697 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
698 char pci_addr[PATH_MAX] = {0};
700 struct rte_pci_addr *loc = &dev->addr;
702 struct mapped_pci_resource *vfio_res = NULL;
703 struct mapped_pci_res_list *vfio_res_list =
704 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
706 struct pci_map *maps;
708 dev->intr_handle.fd = -1;
709 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
710 dev->vfio_req_intr_handle.fd = -1;
713 /* store PCI address string */
714 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
715 loc->domain, loc->bus, loc->devid, loc->function);
717 ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
718 &vfio_dev_fd, &device_info);
722 /* allocate vfio_res and get region info */
723 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
724 if (vfio_res == NULL) {
726 "Cannot store VFIO mmap details\n");
727 goto err_vfio_dev_fd;
729 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
731 /* get number of registers (up to BAR5) */
732 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
733 VFIO_PCI_BAR5_REGION_INDEX + 1);
736 maps = vfio_res->maps;
738 vfio_res->msix_table.bar_index = -1;
739 /* get MSI-X BAR, if any (we have to know where it is because we can't
740 * easily mmap it when using VFIO)
742 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
744 RTE_LOG(ERR, EAL, "%s cannot get MSI-X BAR number!\n",
748 /* if we found our MSI-X BAR region, check if we can mmap it */
749 if (vfio_res->msix_table.bar_index != -1) {
750 int ret = pci_vfio_msix_is_mappable(vfio_dev_fd,
751 vfio_res->msix_table.bar_index);
753 RTE_LOG(ERR, EAL, "Couldn't check if MSI-X BAR is mappable\n");
755 } else if (ret != 0) {
756 /* we can map it, so we don't care where it is */
757 RTE_LOG(DEBUG, EAL, "VFIO reports MSI-X BAR as mappable\n");
758 vfio_res->msix_table.bar_index = -1;
762 for (i = 0; i < vfio_res->nb_maps; i++) {
763 struct vfio_region_info *reg = NULL;
766 ret = pci_vfio_get_region_info(vfio_dev_fd, ®, i);
769 "%s cannot get device region info error "
770 "%i (%s)\n", pci_addr, errno, strerror(errno));
774 /* chk for io port region */
775 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
780 RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
786 /* skip non-mmapable BARs */
787 if ((reg->flags & VFIO_REGION_INFO_FLAG_MMAP) == 0) {
792 /* try mapping somewhere close to the end of hugepages */
793 if (pci_map_addr == NULL)
794 pci_map_addr = pci_find_max_end_va();
796 bar_addr = pci_map_addr;
797 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg->size);
799 pci_map_addr = RTE_PTR_ALIGN(pci_map_addr,
800 sysconf(_SC_PAGE_SIZE));
802 maps[i].addr = bar_addr;
803 maps[i].offset = reg->offset;
804 maps[i].size = reg->size;
805 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
807 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
809 RTE_LOG(ERR, EAL, "%s mapping BAR%i failed: %s\n",
810 pci_addr, i, strerror(errno));
815 dev->mem_resource[i].addr = maps[i].addr;
820 if (pci_rte_vfio_setup_device(dev, vfio_dev_fd) < 0) {
821 RTE_LOG(ERR, EAL, "%s setup device failed\n", pci_addr);
825 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
826 if (pci_vfio_enable_notifier(dev, vfio_dev_fd) != 0) {
827 RTE_LOG(ERR, EAL, "Error setting up notifier!\n");
832 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
838 rte_vfio_release_device(rte_pci_get_sysfs_path(),
839 pci_addr, vfio_dev_fd);
844 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
846 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
847 char pci_addr[PATH_MAX] = {0};
849 struct rte_pci_addr *loc = &dev->addr;
851 struct mapped_pci_resource *vfio_res = NULL;
852 struct mapped_pci_res_list *vfio_res_list =
853 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
855 struct pci_map *maps;
857 dev->intr_handle.fd = -1;
858 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
859 dev->vfio_req_intr_handle.fd = -1;
862 /* store PCI address string */
863 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
864 loc->domain, loc->bus, loc->devid, loc->function);
866 /* if we're in a secondary process, just find our tailq entry */
867 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
868 if (rte_pci_addr_cmp(&vfio_res->pci_addr,
873 /* if we haven't found our tailq entry, something's wrong */
874 if (vfio_res == NULL) {
875 RTE_LOG(ERR, EAL, "%s cannot find TAILQ entry for PCI device!\n",
880 ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
881 &vfio_dev_fd, &device_info);
886 maps = vfio_res->maps;
888 for (i = 0; i < vfio_res->nb_maps; i++) {
889 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
891 RTE_LOG(ERR, EAL, "%s mapping BAR%i failed: %s\n",
892 pci_addr, i, strerror(errno));
893 goto err_vfio_dev_fd;
896 dev->mem_resource[i].addr = maps[i].addr;
899 /* we need save vfio_dev_fd, so it can be used during release */
900 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
901 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
902 dev->vfio_req_intr_handle.vfio_dev_fd = vfio_dev_fd;
907 rte_vfio_release_device(rte_pci_get_sysfs_path(),
908 pci_addr, vfio_dev_fd);
913 * map the PCI resources of a PCI device in virtual memory (VFIO version).
914 * primary and secondary processes follow almost exactly the same path
917 pci_vfio_map_resource(struct rte_pci_device *dev)
919 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
920 return pci_vfio_map_resource_primary(dev);
922 return pci_vfio_map_resource_secondary(dev);
925 static struct mapped_pci_resource *
926 find_and_unmap_vfio_resource(struct mapped_pci_res_list *vfio_res_list,
927 struct rte_pci_device *dev,
928 const char *pci_addr)
930 struct mapped_pci_resource *vfio_res = NULL;
931 struct pci_map *maps;
935 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
936 if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
941 if (vfio_res == NULL)
944 RTE_LOG(INFO, EAL, "Releasing PCI mapped resource for %s\n",
947 maps = vfio_res->maps;
948 for (i = 0; i < vfio_res->nb_maps; i++) {
951 * We do not need to be aware of MSI-X table BAR mappings as
952 * when mapping. Just using current maps array is enough
955 RTE_LOG(INFO, EAL, "Calling pci_unmap_resource for %s at %p\n",
956 pci_addr, maps[i].addr);
957 pci_unmap_resource(maps[i].addr, maps[i].size);
965 pci_vfio_unmap_resource_primary(struct rte_pci_device *dev)
967 char pci_addr[PATH_MAX] = {0};
968 struct rte_pci_addr *loc = &dev->addr;
969 struct mapped_pci_resource *vfio_res = NULL;
970 struct mapped_pci_res_list *vfio_res_list;
973 /* store PCI address string */
974 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
975 loc->domain, loc->bus, loc->devid, loc->function);
977 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
978 ret = pci_vfio_disable_notifier(dev);
980 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
985 if (close(dev->intr_handle.fd) < 0) {
986 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
991 if (pci_vfio_set_bus_master(dev->intr_handle.vfio_dev_fd, false)) {
992 RTE_LOG(ERR, EAL, "%s cannot unset bus mastering for PCI device!\n",
997 ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
998 dev->intr_handle.vfio_dev_fd);
1000 RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
1005 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
1006 vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
1008 /* if we haven't found our tailq entry, something's wrong */
1009 if (vfio_res == NULL) {
1010 RTE_LOG(ERR, EAL, "%s cannot find TAILQ entry for PCI device!\n",
1015 TAILQ_REMOVE(vfio_res_list, vfio_res, next);
1021 pci_vfio_unmap_resource_secondary(struct rte_pci_device *dev)
1023 char pci_addr[PATH_MAX] = {0};
1024 struct rte_pci_addr *loc = &dev->addr;
1025 struct mapped_pci_resource *vfio_res = NULL;
1026 struct mapped_pci_res_list *vfio_res_list;
1029 /* store PCI address string */
1030 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
1031 loc->domain, loc->bus, loc->devid, loc->function);
1033 ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
1034 dev->intr_handle.vfio_dev_fd);
1036 RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
1041 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
1042 vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
1044 /* if we haven't found our tailq entry, something's wrong */
1045 if (vfio_res == NULL) {
1046 RTE_LOG(ERR, EAL, "%s cannot find TAILQ entry for PCI device!\n",
1055 pci_vfio_unmap_resource(struct rte_pci_device *dev)
1057 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1058 return pci_vfio_unmap_resource_primary(dev);
1060 return pci_vfio_unmap_resource_secondary(dev);
1064 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
1065 struct rte_pci_ioport *p)
1067 if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
1068 bar > VFIO_PCI_BAR5_REGION_INDEX) {
1069 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
1074 p->base = VFIO_GET_REGION_ADDR(bar);
1079 pci_vfio_ioport_read(struct rte_pci_ioport *p,
1080 void *data, size_t len, off_t offset)
1082 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1084 if (pread64(intr_handle->vfio_dev_fd, data,
1085 len, p->base + offset) <= 0)
1087 "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
1088 VFIO_GET_REGION_IDX(p->base), (int)offset);
1092 pci_vfio_ioport_write(struct rte_pci_ioport *p,
1093 const void *data, size_t len, off_t offset)
1095 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1097 if (pwrite64(intr_handle->vfio_dev_fd, data,
1098 len, p->base + offset) <= 0)
1100 "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
1101 VFIO_GET_REGION_IDX(p->base), (int)offset);
1105 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1112 pci_vfio_is_enabled(void)
1114 return rte_vfio_is_enabled("vfio_pci");