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 using Linux VFIO.
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 int vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
52 return pread64(vfio_dev_fd, buf, len,
53 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
57 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
58 const void *buf, size_t len, off_t offs)
60 int vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
62 return pwrite64(vfio_dev_fd, buf, len,
63 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
66 /* get PCI BAR number where MSI-X interrupts are */
68 pci_vfio_get_msix_bar(int fd, struct pci_msix_table *msix_table)
73 uint8_t cap_id, cap_offset;
75 /* read PCI capability pointer from config space */
76 ret = pread64(fd, ®, sizeof(reg),
77 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
79 if (ret != sizeof(reg)) {
81 "Cannot read capability pointer from PCI config space!\n");
85 /* we need first byte */
86 cap_offset = reg & 0xFF;
90 /* read PCI capability ID */
91 ret = pread64(fd, ®, sizeof(reg),
92 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
94 if (ret != sizeof(reg)) {
96 "Cannot read capability ID from PCI config space!\n");
100 /* we need first byte */
103 /* if we haven't reached MSI-X, check next capability */
104 if (cap_id != PCI_CAP_ID_MSIX) {
105 ret = pread64(fd, ®, sizeof(reg),
106 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
108 if (ret != sizeof(reg)) {
110 "Cannot read capability pointer from PCI config space!\n");
114 /* we need second byte */
115 cap_offset = (reg & 0xFF00) >> 8;
119 /* else, read table offset */
121 /* table offset resides in the next 4 bytes */
122 ret = pread64(fd, ®, sizeof(reg),
123 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
125 if (ret != sizeof(reg)) {
127 "Cannot read table offset from PCI config space!\n");
131 ret = pread64(fd, &flags, sizeof(flags),
132 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
134 if (ret != sizeof(flags)) {
136 "Cannot read table flags from PCI config space!\n");
140 msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
141 msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
143 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
151 /* enable PCI bus memory space */
153 pci_vfio_enable_bus_memory(int dev_fd)
158 ret = pread64(dev_fd, &cmd, sizeof(cmd),
159 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
162 if (ret != sizeof(cmd)) {
163 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
167 if (cmd & PCI_COMMAND_MEMORY)
170 cmd |= PCI_COMMAND_MEMORY;
171 ret = pwrite64(dev_fd, &cmd, sizeof(cmd),
172 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
175 if (ret != sizeof(cmd)) {
176 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
183 /* set PCI bus mastering */
185 pci_vfio_set_bus_master(int dev_fd, bool op)
190 ret = pread64(dev_fd, ®, sizeof(reg),
191 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
193 if (ret != sizeof(reg)) {
194 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
199 /* set the master bit */
200 reg |= PCI_COMMAND_MASTER;
202 reg &= ~(PCI_COMMAND_MASTER);
204 ret = pwrite64(dev_fd, ®, sizeof(reg),
205 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
208 if (ret != sizeof(reg)) {
209 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
216 /* set up interrupt support (but not enable interrupts) */
218 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
220 int i, ret, intr_idx;
221 enum rte_intr_mode intr_mode;
223 /* default to invalid index */
224 intr_idx = VFIO_PCI_NUM_IRQS;
226 /* Get default / configured intr_mode */
227 intr_mode = rte_eal_vfio_intr_mode();
229 /* get interrupt type from internal config (MSI-X by default, can be
230 * overridden from the command line
233 case RTE_INTR_MODE_MSIX:
234 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
236 case RTE_INTR_MODE_MSI:
237 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
239 case RTE_INTR_MODE_LEGACY:
240 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
242 /* don't do anything if we want to automatically determine interrupt type */
243 case RTE_INTR_MODE_NONE:
246 RTE_LOG(ERR, EAL, "Unknown default interrupt type!\n");
250 /* start from MSI-X interrupt type */
251 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
252 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
255 /* skip interrupt modes we don't want */
256 if (intr_mode != RTE_INTR_MODE_NONE &&
262 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
264 RTE_LOG(ERR, EAL, "Cannot get VFIO IRQ info, error "
265 "%i (%s)\n", errno, strerror(errno));
269 /* if this vector cannot be used with eventfd, fail if we explicitly
270 * specified interrupt type, otherwise continue */
271 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
272 if (intr_mode != RTE_INTR_MODE_NONE) {
274 "Interrupt vector does not support eventfd!\n");
280 /* set up an eventfd for interrupts */
281 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
283 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error "
284 "%i (%s)\n", errno, strerror(errno));
288 if (rte_intr_fd_set(dev->intr_handle, fd))
291 if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
295 case VFIO_PCI_MSIX_IRQ_INDEX:
296 intr_mode = RTE_INTR_MODE_MSIX;
297 rte_intr_type_set(dev->intr_handle,
298 RTE_INTR_HANDLE_VFIO_MSIX);
300 case VFIO_PCI_MSI_IRQ_INDEX:
301 intr_mode = RTE_INTR_MODE_MSI;
302 rte_intr_type_set(dev->intr_handle,
303 RTE_INTR_HANDLE_VFIO_MSI);
305 case VFIO_PCI_INTX_IRQ_INDEX:
306 intr_mode = RTE_INTR_MODE_LEGACY;
307 rte_intr_type_set(dev->intr_handle,
308 RTE_INTR_HANDLE_VFIO_LEGACY);
311 RTE_LOG(ERR, EAL, "Unknown interrupt type!\n");
318 /* if we're here, we haven't found a suitable interrupt vector */
322 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
324 * Spinlock for device hot-unplug failure handling.
325 * If it tries to access bus or device, such as handle sigbus on bus
326 * or handle memory failure for device, just need to use this lock.
327 * It could protect the bus and the device to avoid race condition.
329 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
332 pci_vfio_req_handler(void *param)
336 struct rte_device *device = (struct rte_device *)param;
338 rte_spinlock_lock(&failure_handle_lock);
339 bus = rte_bus_find_by_device(device);
341 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
347 * vfio kernel module request user space to release allocated
348 * resources before device be deleted in kernel, so it can directly
349 * call the vfio bus hot-unplug handler to process it.
351 ret = bus->hot_unplug_handler(device);
354 "Can not handle hot-unplug for device (%s)\n",
357 rte_spinlock_unlock(&failure_handle_lock);
360 /* enable notifier (only enable req now) */
362 pci_vfio_enable_notifier(struct rte_pci_device *dev, int vfio_dev_fd)
367 /* set up an eventfd for req notifier */
368 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
370 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error %i (%s)\n",
371 errno, strerror(errno));
375 if (rte_intr_fd_set(dev->vfio_req_intr_handle, fd))
378 if (rte_intr_type_set(dev->vfio_req_intr_handle, RTE_INTR_HANDLE_VFIO_REQ))
381 if (rte_intr_dev_fd_set(dev->vfio_req_intr_handle, vfio_dev_fd))
384 ret = rte_intr_callback_register(dev->vfio_req_intr_handle,
385 pci_vfio_req_handler,
386 (void *)&dev->device);
388 RTE_LOG(ERR, EAL, "Fail to register req notifier handler.\n");
392 ret = rte_intr_enable(dev->vfio_req_intr_handle);
394 RTE_LOG(ERR, EAL, "Fail to enable req notifier.\n");
395 ret = rte_intr_callback_unregister(dev->vfio_req_intr_handle,
396 pci_vfio_req_handler,
397 (void *)&dev->device);
400 "Fail to unregister req notifier handler.\n");
408 rte_intr_fd_set(dev->vfio_req_intr_handle, -1);
409 rte_intr_type_set(dev->vfio_req_intr_handle, RTE_INTR_HANDLE_UNKNOWN);
410 rte_intr_dev_fd_set(dev->vfio_req_intr_handle, -1);
415 /* disable notifier (only disable req now) */
417 pci_vfio_disable_notifier(struct rte_pci_device *dev)
421 ret = rte_intr_disable(dev->vfio_req_intr_handle);
423 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
427 ret = rte_intr_callback_unregister_sync(dev->vfio_req_intr_handle,
428 pci_vfio_req_handler,
429 (void *)&dev->device);
432 "fail to unregister req notifier handler.\n");
436 close(rte_intr_fd_get(dev->vfio_req_intr_handle));
438 rte_intr_fd_set(dev->vfio_req_intr_handle, -1);
439 rte_intr_type_set(dev->vfio_req_intr_handle, RTE_INTR_HANDLE_UNKNOWN);
440 rte_intr_dev_fd_set(dev->vfio_req_intr_handle, -1);
447 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
452 ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
453 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
454 + PCI_BASE_ADDRESS_0 + bar_index*4);
455 if (ret != sizeof(ioport_bar)) {
456 RTE_LOG(ERR, EAL, "Cannot read command (%x) from config space!\n",
457 PCI_BASE_ADDRESS_0 + bar_index*4);
461 return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
465 pci_rte_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
467 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
468 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
472 if (pci_vfio_enable_bus_memory(vfio_dev_fd)) {
473 RTE_LOG(ERR, EAL, "Cannot enable bus memory!\n");
477 /* set bus mastering for the device */
478 if (pci_vfio_set_bus_master(vfio_dev_fd, true)) {
479 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
484 * Reset the device. If the device is not capable of resetting,
485 * then it updates errno as EINVAL.
487 if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
488 RTE_LOG(ERR, EAL, "Unable to reset device! Error: %d (%s)\n",
489 errno, strerror(errno));
497 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
498 int bar_index, int additional_flags)
505 struct pci_msix_table *msix_table = &vfio_res->msix_table;
506 struct pci_map *bar = &vfio_res->maps[bar_index];
508 if (bar->size == 0) {
509 RTE_LOG(DEBUG, EAL, "Bar size is 0, skip BAR%d\n", bar_index);
513 if (msix_table->bar_index == bar_index) {
515 * VFIO will not let us map the MSI-X table,
516 * but we can map around it.
518 uint32_t table_start = msix_table->offset;
519 uint32_t table_end = table_start + msix_table->size;
520 table_end = RTE_ALIGN(table_end, rte_mem_page_size());
521 table_start = RTE_ALIGN_FLOOR(table_start, rte_mem_page_size());
523 /* If page-aligned start of MSI-X table is less than the
524 * actual MSI-X table start address, reassign to the actual
527 if (table_start < msix_table->offset)
528 table_start = msix_table->offset;
530 if (table_start == 0 && table_end >= bar->size) {
531 /* Cannot map this BAR */
532 RTE_LOG(DEBUG, EAL, "Skipping BAR%d\n", bar_index);
538 memreg[0].offset = bar->offset;
539 memreg[0].size = table_start;
540 if (bar->size < table_end) {
542 * If MSI-X table end is beyond BAR end, don't attempt
543 * to perform second mapping.
545 memreg[1].offset = 0;
548 memreg[1].offset = bar->offset + table_end;
549 memreg[1].size = bar->size - table_end;
553 "Trying to map BAR%d that contains the MSI-X "
554 "table. Trying offsets: "
555 "0x%04" PRIx64 ":0x%04zx, 0x%04" PRIx64 ":0x%04zx\n",
557 memreg[0].offset, memreg[0].size,
558 memreg[1].offset, memreg[1].size);
560 memreg[0].offset = bar->offset;
561 memreg[0].size = bar->size;
564 /* reserve the address using an inaccessible mapping */
565 bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
566 MAP_ANONYMOUS | additional_flags, -1, 0);
567 if (bar_addr != MAP_FAILED) {
568 void *map_addr = NULL;
569 if (memreg[0].size) {
570 /* actual map of first part */
571 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
574 RTE_MAP_FORCE_ADDRESS);
578 * Regarding "memreg[0].size == 0":
579 * If this BAR has MSI-X table, memreg[0].size (the
580 * first part or the part before the table) can
581 * legitimately be 0 for hardware using vector table
582 * offset 0 (i.e. first part does not exist).
584 * When memreg[0].size is 0, "mapping the first part"
585 * never happens, and map_addr is NULL at this
586 * point. So check that mapping has been actually
589 /* if there's a second part, try to map it */
590 if ((map_addr != NULL || memreg[0].size == 0)
591 && memreg[1].offset && memreg[1].size) {
592 void *second_addr = RTE_PTR_ADD(bar_addr,
593 (uintptr_t)(memreg[1].offset -
595 map_addr = pci_map_resource(second_addr,
599 RTE_MAP_FORCE_ADDRESS);
602 if (map_addr == NULL) {
603 munmap(bar_addr, bar->size);
604 bar_addr = MAP_FAILED;
605 RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
611 "Failed to create inaccessible mapping for BAR%d\n",
616 bar->addr = bar_addr;
621 * region info may contain capability headers, so we need to keep reallocating
622 * the memory until we match allocated memory size with argsz.
625 pci_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
628 struct vfio_region_info *ri;
629 size_t argsz = sizeof(*ri);
632 ri = malloc(sizeof(*ri));
635 "Cannot allocate memory for VFIO region info\n");
639 memset(ri, 0, argsz);
643 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ri);
648 if (ri->argsz != argsz) {
649 struct vfio_region_info *tmp;
652 tmp = realloc(ri, argsz);
655 /* realloc failed but the ri is still there */
658 "Cannot reallocate memory for VFIO region info\n");
669 static struct vfio_info_cap_header *
670 pci_vfio_info_cap(struct vfio_region_info *info, int cap)
672 struct vfio_info_cap_header *h;
675 if ((info->flags & RTE_VFIO_INFO_FLAG_CAPS) == 0) {
676 /* VFIO info does not advertise capabilities */
680 offset = VFIO_CAP_OFFSET(info);
681 while (offset != 0) {
682 h = RTE_PTR_ADD(info, offset);
691 pci_vfio_msix_is_mappable(int vfio_dev_fd, int msix_region)
693 struct vfio_region_info *info;
696 ret = pci_vfio_get_region_info(vfio_dev_fd, &info, msix_region);
700 ret = pci_vfio_info_cap(info, RTE_VFIO_CAP_MSIX_MAPPABLE) != NULL;
710 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
712 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
713 char pci_addr[PATH_MAX] = {0};
715 struct rte_pci_addr *loc = &dev->addr;
717 struct mapped_pci_resource *vfio_res = NULL;
718 struct mapped_pci_res_list *vfio_res_list =
719 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
721 struct pci_map *maps;
723 if (rte_intr_fd_set(dev->intr_handle, -1))
726 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
727 if (rte_intr_fd_set(dev->vfio_req_intr_handle, -1))
731 /* store PCI address string */
732 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
733 loc->domain, loc->bus, loc->devid, loc->function);
735 ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
736 &vfio_dev_fd, &device_info);
740 /* allocate vfio_res and get region info */
741 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
742 if (vfio_res == NULL) {
744 "Cannot store VFIO mmap details\n");
745 goto err_vfio_dev_fd;
747 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
749 /* get number of registers (up to BAR5) */
750 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
751 VFIO_PCI_BAR5_REGION_INDEX + 1);
754 maps = vfio_res->maps;
756 vfio_res->msix_table.bar_index = -1;
757 /* get MSI-X BAR, if any (we have to know where it is because we can't
758 * easily mmap it when using VFIO)
760 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
762 RTE_LOG(ERR, EAL, "%s cannot get MSI-X BAR number!\n",
766 /* if we found our MSI-X BAR region, check if we can mmap it */
767 if (vfio_res->msix_table.bar_index != -1) {
768 int ret = pci_vfio_msix_is_mappable(vfio_dev_fd,
769 vfio_res->msix_table.bar_index);
771 RTE_LOG(ERR, EAL, "Couldn't check if MSI-X BAR is mappable\n");
773 } else if (ret != 0) {
774 /* we can map it, so we don't care where it is */
775 RTE_LOG(DEBUG, EAL, "VFIO reports MSI-X BAR as mappable\n");
776 vfio_res->msix_table.bar_index = -1;
780 for (i = 0; i < vfio_res->nb_maps; i++) {
781 struct vfio_region_info *reg = NULL;
784 ret = pci_vfio_get_region_info(vfio_dev_fd, ®, i);
787 "%s cannot get device region info error "
788 "%i (%s)\n", pci_addr, errno, strerror(errno));
792 /* chk for io port region */
793 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
798 RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
804 /* skip non-mmapable BARs */
805 if ((reg->flags & VFIO_REGION_INFO_FLAG_MMAP) == 0) {
810 /* try mapping somewhere close to the end of hugepages */
811 if (pci_map_addr == NULL)
812 pci_map_addr = pci_find_max_end_va();
814 bar_addr = pci_map_addr;
815 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg->size);
817 pci_map_addr = RTE_PTR_ALIGN(pci_map_addr,
818 sysconf(_SC_PAGE_SIZE));
820 maps[i].addr = bar_addr;
821 maps[i].offset = reg->offset;
822 maps[i].size = reg->size;
823 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
825 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
827 RTE_LOG(ERR, EAL, "%s mapping BAR%i failed: %s\n",
828 pci_addr, i, strerror(errno));
833 dev->mem_resource[i].addr = maps[i].addr;
838 if (pci_rte_vfio_setup_device(dev, vfio_dev_fd) < 0) {
839 RTE_LOG(ERR, EAL, "%s setup device failed\n", pci_addr);
843 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
844 if (pci_vfio_enable_notifier(dev, vfio_dev_fd) != 0) {
845 RTE_LOG(ERR, EAL, "Error setting up notifier!\n");
850 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
856 rte_vfio_release_device(rte_pci_get_sysfs_path(),
857 pci_addr, vfio_dev_fd);
862 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
864 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
865 char pci_addr[PATH_MAX] = {0};
867 struct rte_pci_addr *loc = &dev->addr;
869 struct mapped_pci_resource *vfio_res = NULL;
870 struct mapped_pci_res_list *vfio_res_list =
871 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
873 struct pci_map *maps;
875 if (rte_intr_fd_set(dev->intr_handle, -1))
877 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
878 if (rte_intr_fd_set(dev->vfio_req_intr_handle, -1))
882 /* store PCI address string */
883 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
884 loc->domain, loc->bus, loc->devid, loc->function);
886 /* if we're in a secondary process, just find our tailq entry */
887 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
888 if (rte_pci_addr_cmp(&vfio_res->pci_addr,
893 /* if we haven't found our tailq entry, something's wrong */
894 if (vfio_res == NULL) {
895 RTE_LOG(ERR, EAL, "%s cannot find TAILQ entry for PCI device!\n",
900 ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
901 &vfio_dev_fd, &device_info);
906 maps = vfio_res->maps;
908 for (i = 0; i < vfio_res->nb_maps; i++) {
909 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
911 RTE_LOG(ERR, EAL, "%s mapping BAR%i failed: %s\n",
912 pci_addr, i, strerror(errno));
913 goto err_vfio_dev_fd;
916 dev->mem_resource[i].addr = maps[i].addr;
919 /* we need save vfio_dev_fd, so it can be used during release */
920 if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
921 goto err_vfio_dev_fd;
922 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
923 if (rte_intr_dev_fd_set(dev->vfio_req_intr_handle, vfio_dev_fd))
924 goto err_vfio_dev_fd;
929 rte_vfio_release_device(rte_pci_get_sysfs_path(),
930 pci_addr, vfio_dev_fd);
935 * map the PCI resources of a PCI device in virtual memory (VFIO version).
936 * primary and secondary processes follow almost exactly the same path
939 pci_vfio_map_resource(struct rte_pci_device *dev)
941 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
942 return pci_vfio_map_resource_primary(dev);
944 return pci_vfio_map_resource_secondary(dev);
947 static struct mapped_pci_resource *
948 find_and_unmap_vfio_resource(struct mapped_pci_res_list *vfio_res_list,
949 struct rte_pci_device *dev,
950 const char *pci_addr)
952 struct mapped_pci_resource *vfio_res = NULL;
953 struct pci_map *maps;
957 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
958 if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
963 if (vfio_res == NULL)
966 RTE_LOG(INFO, EAL, "Releasing PCI mapped resource for %s\n",
969 maps = vfio_res->maps;
970 for (i = 0; i < vfio_res->nb_maps; i++) {
973 * We do not need to be aware of MSI-X table BAR mappings as
974 * when mapping. Just using current maps array is enough
977 RTE_LOG(INFO, EAL, "Calling pci_unmap_resource for %s at %p\n",
978 pci_addr, maps[i].addr);
979 pci_unmap_resource(maps[i].addr, maps[i].size);
987 pci_vfio_unmap_resource_primary(struct rte_pci_device *dev)
989 char pci_addr[PATH_MAX] = {0};
990 struct rte_pci_addr *loc = &dev->addr;
991 struct mapped_pci_resource *vfio_res = NULL;
992 struct mapped_pci_res_list *vfio_res_list;
993 int ret, vfio_dev_fd;
995 /* store PCI address string */
996 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
997 loc->domain, loc->bus, loc->devid, loc->function);
999 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
1000 ret = pci_vfio_disable_notifier(dev);
1002 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
1007 if (close(rte_intr_fd_get(dev->intr_handle)) < 0) {
1008 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
1013 vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
1014 if (pci_vfio_set_bus_master(vfio_dev_fd, false)) {
1015 RTE_LOG(ERR, EAL, "%s cannot unset bus mastering for PCI device!\n",
1020 ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
1023 RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
1028 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
1029 vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
1031 /* if we haven't found our tailq entry, something's wrong */
1032 if (vfio_res == NULL) {
1033 RTE_LOG(ERR, EAL, "%s cannot find TAILQ entry for PCI device!\n",
1038 TAILQ_REMOVE(vfio_res_list, vfio_res, next);
1044 pci_vfio_unmap_resource_secondary(struct rte_pci_device *dev)
1046 char pci_addr[PATH_MAX] = {0};
1047 struct rte_pci_addr *loc = &dev->addr;
1048 struct mapped_pci_resource *vfio_res = NULL;
1049 struct mapped_pci_res_list *vfio_res_list;
1050 int ret, vfio_dev_fd;
1052 /* store PCI address string */
1053 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
1054 loc->domain, loc->bus, loc->devid, loc->function);
1056 vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
1057 ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
1060 RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
1065 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
1066 vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
1068 /* if we haven't found our tailq entry, something's wrong */
1069 if (vfio_res == NULL) {
1070 RTE_LOG(ERR, EAL, "%s cannot find TAILQ entry for PCI device!\n",
1079 pci_vfio_unmap_resource(struct rte_pci_device *dev)
1081 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1082 return pci_vfio_unmap_resource_primary(dev);
1084 return pci_vfio_unmap_resource_secondary(dev);
1088 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
1089 struct rte_pci_ioport *p)
1091 if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
1092 bar > VFIO_PCI_BAR5_REGION_INDEX) {
1093 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
1098 p->base = VFIO_GET_REGION_ADDR(bar);
1103 pci_vfio_ioport_read(struct rte_pci_ioport *p,
1104 void *data, size_t len, off_t offset)
1106 const struct rte_intr_handle *intr_handle = p->dev->intr_handle;
1107 int vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
1109 if (pread64(vfio_dev_fd, data,
1110 len, p->base + offset) <= 0)
1112 "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
1113 VFIO_GET_REGION_IDX(p->base), (int)offset);
1117 pci_vfio_ioport_write(struct rte_pci_ioport *p,
1118 const void *data, size_t len, off_t offset)
1120 const struct rte_intr_handle *intr_handle = p->dev->intr_handle;
1121 int vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
1123 if (pwrite64(vfio_dev_fd, data,
1124 len, p->base + offset) <= 0)
1126 "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
1127 VFIO_GET_REGION_IDX(p->base), (int)offset);
1131 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1138 pci_vfio_is_enabled(void)
1140 return rte_vfio_is_enabled("vfio_pci");