1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
12 #include <sys/queue.h>
13 #include <rte_errno.h>
14 #include <rte_interrupts.h>
18 #include <rte_bus_pci.h>
19 #include <rte_per_lcore.h>
20 #include <rte_memory.h>
22 #include <rte_eal_paging.h>
23 #include <rte_string_fns.h>
24 #include <rte_common.h>
25 #include <rte_devargs.h>
31 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
33 const char *rte_pci_get_sysfs_path(void)
35 const char *path = NULL;
37 #ifdef RTE_EXEC_ENV_LINUX
38 path = getenv("SYSFS_PCI_DEVICES");
40 return SYSFS_PCI_DEVICES;
46 static struct rte_devargs *
47 pci_devargs_lookup(const struct rte_pci_addr *pci_addr)
49 struct rte_devargs *devargs;
50 struct rte_pci_addr addr;
52 RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
53 devargs->bus->parse(devargs->name, &addr);
54 if (!rte_pci_addr_cmp(pci_addr, &addr))
61 pci_name_set(struct rte_pci_device *dev)
63 struct rte_devargs *devargs;
65 /* Each device has its internal, canonical name set. */
66 rte_pci_device_name(&dev->addr,
67 dev->name, sizeof(dev->name));
68 devargs = pci_devargs_lookup(&dev->addr);
69 dev->device.devargs = devargs;
70 /* In blacklist mode, if the device is not blacklisted, no
71 * rte_devargs exists for it.
74 /* If an rte_devargs exists, the generic rte_device uses the
75 * given name as its name.
77 dev->device.name = dev->device.devargs->name;
79 /* Otherwise, it uses the internal, canonical form. */
80 dev->device.name = dev->name;
83 /* map a particular resource from a file */
85 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
90 /* Map the PCI memory resource of device */
91 mapaddr = rte_mem_map(requested_addr, size,
92 RTE_PROT_READ | RTE_PROT_WRITE,
93 RTE_MAP_SHARED | additional_flags, fd, offset);
94 if (mapaddr == NULL) {
96 "%s(): cannot map resource(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
97 __func__, fd, requested_addr, size,
98 (unsigned long long)offset,
99 rte_strerror(rte_errno), mapaddr);
100 mapaddr = MAP_FAILED; /* API uses mmap error code */
102 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
107 /* unmap a particular resource */
109 pci_unmap_resource(void *requested_addr, size_t size)
111 if (requested_addr == NULL)
114 /* Unmap the PCI memory resource of device */
115 if (rte_mem_unmap(requested_addr, size)) {
116 RTE_LOG(ERR, EAL, "%s(): cannot mem unmap(%p, %#zx): %s\n",
117 __func__, requested_addr, size,
118 rte_strerror(rte_errno));
120 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
124 * Match the PCI Driver and Device using the ID Table
127 rte_pci_match(const struct rte_pci_driver *pci_drv,
128 const struct rte_pci_device *pci_dev)
130 const struct rte_pci_id *id_table;
132 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
134 /* check if device's identifiers match the driver's ones */
135 if (id_table->vendor_id != pci_dev->id.vendor_id &&
136 id_table->vendor_id != PCI_ANY_ID)
138 if (id_table->device_id != pci_dev->id.device_id &&
139 id_table->device_id != PCI_ANY_ID)
141 if (id_table->subsystem_vendor_id !=
142 pci_dev->id.subsystem_vendor_id &&
143 id_table->subsystem_vendor_id != PCI_ANY_ID)
145 if (id_table->subsystem_device_id !=
146 pci_dev->id.subsystem_device_id &&
147 id_table->subsystem_device_id != PCI_ANY_ID)
149 if (id_table->class_id != pci_dev->id.class_id &&
150 id_table->class_id != RTE_CLASS_ANY_ID)
160 * If vendor/device ID match, call the probe() function of the
164 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
165 struct rte_pci_device *dev)
169 struct rte_pci_addr *loc;
171 if ((dr == NULL) || (dev == NULL))
176 /* The device is not blacklisted; Check if driver supports it */
177 if (!rte_pci_match(dr, dev))
178 /* Match of device and driver failed */
181 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
182 loc->domain, loc->bus, loc->devid, loc->function,
183 dev->device.numa_node);
185 /* no initialization when blacklisted, return without error */
186 if (dev->device.devargs != NULL &&
187 dev->device.devargs->policy ==
188 RTE_DEV_BLACKLISTED) {
189 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
194 if (dev->device.numa_node < 0) {
195 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
196 dev->device.numa_node = 0;
199 already_probed = rte_dev_is_probed(&dev->device);
200 if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
201 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
206 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
207 dev->id.device_id, dr->driver.name);
210 * reference driver structure
211 * This needs to be before rte_pci_map_device(), as it enables to use
212 * driver flags for adjusting configuration.
214 if (!already_probed) {
215 enum rte_iova_mode dev_iova_mode;
216 enum rte_iova_mode iova_mode;
218 dev_iova_mode = pci_device_iova_mode(dr, dev);
219 iova_mode = rte_eal_iova_mode();
220 if (dev_iova_mode != RTE_IOVA_DC &&
221 dev_iova_mode != iova_mode) {
222 RTE_LOG(ERR, EAL, " Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
223 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
224 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
231 if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
232 /* map resources for devices that use igb_uio */
233 ret = rte_pci_map_device(dev);
240 RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
241 dr->driver.name, dev->id.vendor_id, dev->id.device_id,
242 loc->domain, loc->bus, loc->devid, loc->function,
243 dev->device.numa_node);
244 /* call the driver probe() function */
245 ret = dr->probe(dr, dev);
247 return ret; /* no rollback if already succeeded earlier */
250 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
251 /* Don't unmap if device is unsupported and
252 * driver needs mapped resources.
255 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
256 rte_pci_unmap_device(dev);
258 dev->device.driver = &dr->driver;
265 * If vendor/device ID match, call the remove() function of the
269 rte_pci_detach_dev(struct rte_pci_device *dev)
271 struct rte_pci_addr *loc;
272 struct rte_pci_driver *dr;
281 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
282 loc->domain, loc->bus, loc->devid,
283 loc->function, dev->device.numa_node);
285 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
286 dev->id.device_id, dr->driver.name);
289 ret = dr->remove(dev);
294 /* clear driver structure */
296 dev->device.driver = NULL;
298 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
299 /* unmap resources for devices that use igb_uio */
300 rte_pci_unmap_device(dev);
306 * If vendor/device ID match, call the probe() function of all
307 * registered driver for the given device. Return < 0 if initialization
308 * failed, return 1 if no driver is found for this device.
311 pci_probe_all_drivers(struct rte_pci_device *dev)
313 struct rte_pci_driver *dr = NULL;
319 FOREACH_DRIVER_ON_PCIBUS(dr) {
320 rc = rte_pci_probe_one_driver(dr, dev);
322 /* negative value is an error */
325 /* positive value means driver doesn't support it */
333 * Scan the content of the PCI bus, and call the probe() function for
334 * all registered drivers that have a matching entry in its id_table
335 * for discovered devices.
340 struct rte_pci_device *dev = NULL;
341 size_t probed = 0, failed = 0;
344 FOREACH_DEVICE_ON_PCIBUS(dev) {
347 ret = pci_probe_all_drivers(dev);
349 if (ret != -EEXIST) {
350 RTE_LOG(ERR, EAL, "Requested device "
351 PCI_PRI_FMT " cannot be used\n",
352 dev->addr.domain, dev->addr.bus,
353 dev->addr.devid, dev->addr.function);
361 return (probed && probed == failed) ? -1 : 0;
364 /* dump one device */
366 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
370 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
371 dev->addr.devid, dev->addr.function);
372 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
375 for (i = 0; i != sizeof(dev->mem_resource) /
376 sizeof(dev->mem_resource[0]); i++) {
377 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
378 dev->mem_resource[i].phys_addr,
379 dev->mem_resource[i].len);
384 /* dump devices on the bus */
386 rte_pci_dump(FILE *f)
388 struct rte_pci_device *dev = NULL;
390 FOREACH_DEVICE_ON_PCIBUS(dev) {
391 pci_dump_one_device(f, dev);
396 pci_parse(const char *name, void *addr)
398 struct rte_pci_addr *out = addr;
399 struct rte_pci_addr pci_addr;
402 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
403 if (parse && addr != NULL)
405 return parse == false;
408 /* register a driver */
410 rte_pci_register(struct rte_pci_driver *driver)
412 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
413 driver->bus = &rte_pci_bus;
416 /* unregister a driver */
418 rte_pci_unregister(struct rte_pci_driver *driver)
420 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
424 /* Add a device to PCI bus */
426 rte_pci_add_device(struct rte_pci_device *pci_dev)
428 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
431 /* Insert a device into a predefined position in PCI bus */
433 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
434 struct rte_pci_device *new_pci_dev)
436 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
439 /* Remove a device from PCI bus */
441 rte_pci_remove_device(struct rte_pci_device *pci_dev)
443 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
446 static struct rte_device *
447 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
450 const struct rte_pci_device *pstart;
451 struct rte_pci_device *pdev;
454 pstart = RTE_DEV_TO_PCI_CONST(start);
455 pdev = TAILQ_NEXT(pstart, next);
457 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
459 while (pdev != NULL) {
460 if (cmp(&pdev->device, data) == 0)
461 return &pdev->device;
462 pdev = TAILQ_NEXT(pdev, next);
468 * find the device which encounter the failure, by iterate over all device on
469 * PCI bus to check if the memory failure address is located in the range
470 * of the BARs of the device.
472 static struct rte_pci_device *
473 pci_find_device_by_addr(const void *failure_addr)
475 struct rte_pci_device *pdev = NULL;
476 uint64_t check_point, start, end, len;
479 check_point = (uint64_t)(uintptr_t)failure_addr;
481 FOREACH_DEVICE_ON_PCIBUS(pdev) {
482 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
483 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
484 len = pdev->mem_resource[i].len;
486 if (check_point >= start && check_point < end) {
487 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
488 PRIx64" belongs to device %s!\n",
489 check_point, pdev->device.name);
498 pci_hot_unplug_handler(struct rte_device *dev)
500 struct rte_pci_device *pdev = NULL;
503 pdev = RTE_DEV_TO_PCI(dev);
507 switch (pdev->kdrv) {
508 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
509 case RTE_PCI_KDRV_VFIO:
511 * vfio kernel module guaranty the pci device would not be
512 * deleted until the user space release the resource, so no
513 * need to remap BARs resource here, just directly notify
514 * the req event to the user space to handle it.
516 rte_dev_event_callback_process(dev->name,
517 RTE_DEV_EVENT_REMOVE);
520 case RTE_PCI_KDRV_IGB_UIO:
521 case RTE_PCI_KDRV_UIO_GENERIC:
522 case RTE_PCI_KDRV_NIC_UIO:
523 /* BARs resource is invalid, remap it to be safe. */
524 ret = pci_uio_remap_resource(pdev);
528 "Not managed by a supported kernel driver, skipped\n");
537 pci_sigbus_handler(const void *failure_addr)
539 struct rte_pci_device *pdev = NULL;
542 pdev = pci_find_device_by_addr(failure_addr);
544 /* It is a generic sigbus error, no bus would handle it. */
547 /* The sigbus error is caused of hot-unplug. */
548 ret = pci_hot_unplug_handler(&pdev->device);
551 "Failed to handle hot-unplug for device %s",
560 pci_plug(struct rte_device *dev)
562 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
566 pci_unplug(struct rte_device *dev)
568 struct rte_pci_device *pdev;
571 pdev = RTE_DEV_TO_PCI(dev);
572 ret = rte_pci_detach_dev(pdev);
574 rte_pci_remove_device(pdev);
575 rte_devargs_remove(dev->devargs);
582 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
584 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
586 if (!pdev || !pdev->driver) {
590 if (pdev->driver->dma_map)
591 return pdev->driver->dma_map(pdev, addr, iova, len);
593 * In case driver don't provides any specific mapping
594 * try fallback to VFIO.
596 if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
597 return rte_vfio_container_dma_map
598 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
605 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
607 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
609 if (!pdev || !pdev->driver) {
613 if (pdev->driver->dma_unmap)
614 return pdev->driver->dma_unmap(pdev, addr, iova, len);
616 * In case driver don't provides any specific mapping
617 * try fallback to VFIO.
619 if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
620 return rte_vfio_container_dma_unmap
621 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
628 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
630 struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
632 switch (rte_pci_bus.bus.conf.scan_mode) {
633 case RTE_BUS_SCAN_WHITELIST:
634 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
637 case RTE_BUS_SCAN_UNDEFINED:
638 case RTE_BUS_SCAN_BLACKLIST:
639 if (devargs == NULL ||
640 devargs->policy != RTE_DEV_BLACKLISTED)
648 rte_pci_get_iommu_class(void)
650 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
651 const struct rte_pci_device *dev;
652 const struct rte_pci_driver *drv;
653 bool devices_want_va = false;
654 bool devices_want_pa = false;
655 int iommu_no_va = -1;
657 FOREACH_DEVICE_ON_PCIBUS(dev) {
659 * We can check this only once, because the IOMMU hardware is
660 * the same for all of them.
662 if (iommu_no_va == -1)
663 iommu_no_va = pci_device_iommu_support_va(dev)
666 if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
667 dev->kdrv == RTE_PCI_KDRV_NONE)
669 FOREACH_DRIVER_ON_PCIBUS(drv) {
670 enum rte_iova_mode dev_iova_mode;
672 if (!rte_pci_match(drv, dev))
675 dev_iova_mode = pci_device_iova_mode(drv, dev);
676 RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
677 PCI_PRI_FMT " wants IOVA as '%s'\n",
679 dev->addr.domain, dev->addr.bus,
680 dev->addr.devid, dev->addr.function,
681 dev_iova_mode == RTE_IOVA_DC ? "DC" :
682 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
683 if (dev_iova_mode == RTE_IOVA_PA)
684 devices_want_pa = true;
685 else if (dev_iova_mode == RTE_IOVA_VA)
686 devices_want_va = true;
689 if (iommu_no_va == 1) {
690 iova_mode = RTE_IOVA_PA;
691 if (devices_want_va) {
692 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
693 RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
695 } else if (devices_want_va && !devices_want_pa) {
696 iova_mode = RTE_IOVA_VA;
697 } else if (devices_want_pa && !devices_want_va) {
698 iova_mode = RTE_IOVA_PA;
700 iova_mode = RTE_IOVA_DC;
701 if (devices_want_va) {
702 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
703 RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
709 struct rte_pci_bus rte_pci_bus = {
711 .scan = rte_pci_scan,
713 .find_device = pci_find_device,
715 .unplug = pci_unplug,
717 .dma_map = pci_dma_map,
718 .dma_unmap = pci_dma_unmap,
719 .get_iommu_class = rte_pci_get_iommu_class,
720 .dev_iterate = rte_pci_dev_iterate,
721 .hot_unplug_handler = pci_hot_unplug_handler,
722 .sigbus_handler = pci_sigbus_handler,
724 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
725 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
728 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);