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;
71 /* When using a blocklist, only blocked devices will have
72 * an rte_devargs. Allowed devices won't have one.
75 /* If an rte_devargs exists, the generic rte_device uses the
76 * given name as its name.
78 dev->device.name = dev->device.devargs->name;
80 /* Otherwise, it uses the internal, canonical form. */
81 dev->device.name = dev->name;
84 /* map a particular resource from a file */
86 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
91 /* Map the PCI memory resource of device */
92 mapaddr = rte_mem_map(requested_addr, size,
93 RTE_PROT_READ | RTE_PROT_WRITE,
94 RTE_MAP_SHARED | additional_flags, fd, offset);
95 if (mapaddr == NULL) {
97 "%s(): cannot map resource(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
98 __func__, fd, requested_addr, size,
99 (unsigned long long)offset,
100 rte_strerror(rte_errno), mapaddr);
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 blocked; 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 marked as blocked, return without error */
186 if (dev->device.devargs != NULL &&
187 dev->device.devargs->policy == RTE_DEV_BLOCKED) {
188 RTE_LOG(INFO, EAL, " Device is blocked, not initializing\n");
192 if (dev->device.numa_node < 0) {
193 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
194 dev->device.numa_node = 0;
197 already_probed = rte_dev_is_probed(&dev->device);
198 if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
199 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
204 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
205 dev->id.device_id, dr->driver.name);
208 * reference driver structure
209 * This needs to be before rte_pci_map_device(), as it enables to use
210 * driver flags for adjusting configuration.
212 if (!already_probed) {
213 enum rte_iova_mode dev_iova_mode;
214 enum rte_iova_mode iova_mode;
216 dev_iova_mode = pci_device_iova_mode(dr, dev);
217 iova_mode = rte_eal_iova_mode();
218 if (dev_iova_mode != RTE_IOVA_DC &&
219 dev_iova_mode != iova_mode) {
220 RTE_LOG(ERR, EAL, " Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
221 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
222 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
229 if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
230 /* map resources for devices that use igb_uio */
231 ret = rte_pci_map_device(dev);
238 RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
239 dr->driver.name, dev->id.vendor_id, dev->id.device_id,
240 loc->domain, loc->bus, loc->devid, loc->function,
241 dev->device.numa_node);
242 /* call the driver probe() function */
243 ret = dr->probe(dr, dev);
245 return ret; /* no rollback if already succeeded earlier */
248 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
249 /* Don't unmap if device is unsupported and
250 * driver needs mapped resources.
253 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
254 rte_pci_unmap_device(dev);
256 dev->device.driver = &dr->driver;
263 * If vendor/device ID match, call the remove() function of the
267 rte_pci_detach_dev(struct rte_pci_device *dev)
269 struct rte_pci_addr *loc;
270 struct rte_pci_driver *dr;
279 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
280 loc->domain, loc->bus, loc->devid,
281 loc->function, dev->device.numa_node);
283 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
284 dev->id.device_id, dr->driver.name);
287 ret = dr->remove(dev);
292 /* clear driver structure */
294 dev->device.driver = NULL;
296 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
297 /* unmap resources for devices that use igb_uio */
298 rte_pci_unmap_device(dev);
304 * If vendor/device ID match, call the probe() function of all
305 * registered driver for the given device. Return < 0 if initialization
306 * failed, return 1 if no driver is found for this device.
309 pci_probe_all_drivers(struct rte_pci_device *dev)
311 struct rte_pci_driver *dr = NULL;
317 FOREACH_DRIVER_ON_PCIBUS(dr) {
318 rc = rte_pci_probe_one_driver(dr, dev);
320 /* negative value is an error */
323 /* positive value means driver doesn't support it */
331 * Scan the content of the PCI bus, and call the probe() function for
332 * all registered drivers that have a matching entry in its id_table
333 * for discovered devices.
338 struct rte_pci_device *dev = NULL;
339 size_t probed = 0, failed = 0;
342 FOREACH_DEVICE_ON_PCIBUS(dev) {
345 ret = pci_probe_all_drivers(dev);
347 if (ret != -EEXIST) {
348 RTE_LOG(ERR, EAL, "Requested device "
349 PCI_PRI_FMT " cannot be used\n",
350 dev->addr.domain, dev->addr.bus,
351 dev->addr.devid, dev->addr.function);
359 return (probed && probed == failed) ? -1 : 0;
362 /* dump one device */
364 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
368 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
369 dev->addr.devid, dev->addr.function);
370 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
373 for (i = 0; i != sizeof(dev->mem_resource) /
374 sizeof(dev->mem_resource[0]); i++) {
375 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
376 dev->mem_resource[i].phys_addr,
377 dev->mem_resource[i].len);
382 /* dump devices on the bus */
384 rte_pci_dump(FILE *f)
386 struct rte_pci_device *dev = NULL;
388 FOREACH_DEVICE_ON_PCIBUS(dev) {
389 pci_dump_one_device(f, dev);
394 pci_parse(const char *name, void *addr)
396 struct rte_pci_addr *out = addr;
397 struct rte_pci_addr pci_addr;
400 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
401 if (parse && addr != NULL)
403 return parse == false;
406 /* register a driver */
408 rte_pci_register(struct rte_pci_driver *driver)
410 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
411 driver->bus = &rte_pci_bus;
414 /* unregister a driver */
416 rte_pci_unregister(struct rte_pci_driver *driver)
418 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
422 /* Add a device to PCI bus */
424 rte_pci_add_device(struct rte_pci_device *pci_dev)
426 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
429 /* Insert a device into a predefined position in PCI bus */
431 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
432 struct rte_pci_device *new_pci_dev)
434 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
437 /* Remove a device from PCI bus */
439 rte_pci_remove_device(struct rte_pci_device *pci_dev)
441 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
444 static struct rte_device *
445 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
448 const struct rte_pci_device *pstart;
449 struct rte_pci_device *pdev;
452 pstart = RTE_DEV_TO_PCI_CONST(start);
453 pdev = TAILQ_NEXT(pstart, next);
455 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
457 while (pdev != NULL) {
458 if (cmp(&pdev->device, data) == 0)
459 return &pdev->device;
460 pdev = TAILQ_NEXT(pdev, next);
466 * find the device which encounter the failure, by iterate over all device on
467 * PCI bus to check if the memory failure address is located in the range
468 * of the BARs of the device.
470 static struct rte_pci_device *
471 pci_find_device_by_addr(const void *failure_addr)
473 struct rte_pci_device *pdev = NULL;
474 uint64_t check_point, start, end, len;
477 check_point = (uint64_t)(uintptr_t)failure_addr;
479 FOREACH_DEVICE_ON_PCIBUS(pdev) {
480 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
481 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
482 len = pdev->mem_resource[i].len;
484 if (check_point >= start && check_point < end) {
485 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
486 PRIx64" belongs to device %s!\n",
487 check_point, pdev->device.name);
496 pci_hot_unplug_handler(struct rte_device *dev)
498 struct rte_pci_device *pdev = NULL;
501 pdev = RTE_DEV_TO_PCI(dev);
505 switch (pdev->kdrv) {
506 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
507 case RTE_PCI_KDRV_VFIO:
509 * vfio kernel module guaranty the pci device would not be
510 * deleted until the user space release the resource, so no
511 * need to remap BARs resource here, just directly notify
512 * the req event to the user space to handle it.
514 rte_dev_event_callback_process(dev->name,
515 RTE_DEV_EVENT_REMOVE);
518 case RTE_PCI_KDRV_IGB_UIO:
519 case RTE_PCI_KDRV_UIO_GENERIC:
520 case RTE_PCI_KDRV_NIC_UIO:
521 /* BARs resource is invalid, remap it to be safe. */
522 ret = pci_uio_remap_resource(pdev);
526 "Not managed by a supported kernel driver, skipped\n");
535 pci_sigbus_handler(const void *failure_addr)
537 struct rte_pci_device *pdev = NULL;
540 pdev = pci_find_device_by_addr(failure_addr);
542 /* It is a generic sigbus error, no bus would handle it. */
545 /* The sigbus error is caused of hot-unplug. */
546 ret = pci_hot_unplug_handler(&pdev->device);
549 "Failed to handle hot-unplug for device %s",
558 pci_plug(struct rte_device *dev)
560 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
564 pci_unplug(struct rte_device *dev)
566 struct rte_pci_device *pdev;
569 pdev = RTE_DEV_TO_PCI(dev);
570 ret = rte_pci_detach_dev(pdev);
572 rte_pci_remove_device(pdev);
573 rte_devargs_remove(dev->devargs);
580 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
582 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
584 if (!pdev || !pdev->driver) {
588 if (pdev->driver->dma_map)
589 return pdev->driver->dma_map(pdev, addr, iova, len);
591 * In case driver don't provides any specific mapping
592 * try fallback to VFIO.
594 if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
595 return rte_vfio_container_dma_map
596 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
603 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
605 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
607 if (!pdev || !pdev->driver) {
611 if (pdev->driver->dma_unmap)
612 return pdev->driver->dma_unmap(pdev, addr, iova, len);
614 * In case driver don't provides any specific mapping
615 * try fallback to VFIO.
617 if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
618 return rte_vfio_container_dma_unmap
619 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
626 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
628 struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
630 switch (rte_pci_bus.bus.conf.scan_mode) {
631 case RTE_BUS_SCAN_ALLOWLIST:
632 if (devargs && devargs->policy == RTE_DEV_ALLOWED)
635 case RTE_BUS_SCAN_UNDEFINED:
636 case RTE_BUS_SCAN_BLOCKLIST:
637 if (devargs == NULL || devargs->policy != RTE_DEV_BLOCKED)
645 rte_pci_get_iommu_class(void)
647 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
648 const struct rte_pci_device *dev;
649 const struct rte_pci_driver *drv;
650 bool devices_want_va = false;
651 bool devices_want_pa = false;
652 int iommu_no_va = -1;
654 FOREACH_DEVICE_ON_PCIBUS(dev) {
656 * We can check this only once, because the IOMMU hardware is
657 * the same for all of them.
659 if (iommu_no_va == -1)
660 iommu_no_va = pci_device_iommu_support_va(dev)
663 if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
664 dev->kdrv == RTE_PCI_KDRV_NONE)
666 FOREACH_DRIVER_ON_PCIBUS(drv) {
667 enum rte_iova_mode dev_iova_mode;
669 if (!rte_pci_match(drv, dev))
672 dev_iova_mode = pci_device_iova_mode(drv, dev);
673 RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
674 PCI_PRI_FMT " wants IOVA as '%s'\n",
676 dev->addr.domain, dev->addr.bus,
677 dev->addr.devid, dev->addr.function,
678 dev_iova_mode == RTE_IOVA_DC ? "DC" :
679 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
680 if (dev_iova_mode == RTE_IOVA_PA)
681 devices_want_pa = true;
682 else if (dev_iova_mode == RTE_IOVA_VA)
683 devices_want_va = true;
686 if (iommu_no_va == 1) {
687 iova_mode = RTE_IOVA_PA;
688 if (devices_want_va) {
689 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
690 RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
692 } else if (devices_want_va && !devices_want_pa) {
693 iova_mode = RTE_IOVA_VA;
694 } else if (devices_want_pa && !devices_want_va) {
695 iova_mode = RTE_IOVA_PA;
697 iova_mode = RTE_IOVA_DC;
698 if (devices_want_va) {
699 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
700 RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
707 rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap)
709 off_t offset = RTE_PCI_CFG_SPACE_SIZE;
713 /* minimum 8 bytes per capability */
714 ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
716 if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
717 RTE_LOG(ERR, EAL, "error in reading extended capabilities\n");
722 * If we have no capabilities, this is indicated by cap ID,
723 * cap version and next pointer all being 0.
729 if (RTE_PCI_EXT_CAP_ID(header) == cap)
732 offset = RTE_PCI_EXT_CAP_NEXT(header);
734 if (offset < RTE_PCI_CFG_SPACE_SIZE)
737 if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
739 "error in reading extended capabilities\n");
749 struct rte_pci_bus rte_pci_bus = {
751 .scan = rte_pci_scan,
753 .find_device = pci_find_device,
755 .unplug = pci_unplug,
757 .dma_map = pci_dma_map,
758 .dma_unmap = pci_dma_unmap,
759 .get_iommu_class = rte_pci_get_iommu_class,
760 .dev_iterate = rte_pci_dev_iterate,
761 .hot_unplug_handler = pci_hot_unplug_handler,
762 .sigbus_handler = pci_sigbus_handler,
764 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
765 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
768 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);