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>
15 #include <rte_errno.h>
16 #include <rte_interrupts.h>
20 #include <rte_bus_pci.h>
21 #include <rte_per_lcore.h>
22 #include <rte_memory.h>
24 #include <rte_string_fns.h>
25 #include <rte_common.h>
26 #include <rte_devargs.h>
32 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
34 const char *rte_pci_get_sysfs_path(void)
36 const char *path = NULL;
38 #ifdef RTE_EXEC_ENV_LINUX
39 path = getenv("SYSFS_PCI_DEVICES");
41 return SYSFS_PCI_DEVICES;
47 static struct rte_devargs *
48 pci_devargs_lookup(const struct rte_pci_addr *pci_addr)
50 struct rte_devargs *devargs;
51 struct rte_pci_addr addr;
53 RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
54 devargs->bus->parse(devargs->name, &addr);
55 if (!rte_pci_addr_cmp(pci_addr, &addr))
62 pci_name_set(struct rte_pci_device *dev)
64 struct rte_devargs *devargs;
66 /* Each device has its internal, canonical name set. */
67 rte_pci_device_name(&dev->addr,
68 dev->name, sizeof(dev->name));
69 devargs = pci_devargs_lookup(&dev->addr);
70 dev->device.devargs = devargs;
71 /* In blacklist mode, if the device is not blacklisted, no
72 * rte_devargs exists for it.
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;
85 * Match the PCI Driver and Device using the ID Table
88 rte_pci_match(const struct rte_pci_driver *pci_drv,
89 const struct rte_pci_device *pci_dev)
91 const struct rte_pci_id *id_table;
93 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
95 /* check if device's identifiers match the driver's ones */
96 if (id_table->vendor_id != pci_dev->id.vendor_id &&
97 id_table->vendor_id != PCI_ANY_ID)
99 if (id_table->device_id != pci_dev->id.device_id &&
100 id_table->device_id != PCI_ANY_ID)
102 if (id_table->subsystem_vendor_id !=
103 pci_dev->id.subsystem_vendor_id &&
104 id_table->subsystem_vendor_id != PCI_ANY_ID)
106 if (id_table->subsystem_device_id !=
107 pci_dev->id.subsystem_device_id &&
108 id_table->subsystem_device_id != PCI_ANY_ID)
110 if (id_table->class_id != pci_dev->id.class_id &&
111 id_table->class_id != RTE_CLASS_ANY_ID)
121 * If vendor/device ID match, call the probe() function of the
125 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
126 struct rte_pci_device *dev)
130 struct rte_pci_addr *loc;
132 if ((dr == NULL) || (dev == NULL))
137 /* The device is not blacklisted; Check if driver supports it */
138 if (!rte_pci_match(dr, dev))
139 /* Match of device and driver failed */
142 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
143 loc->domain, loc->bus, loc->devid, loc->function,
144 dev->device.numa_node);
146 /* no initialization when blacklisted, return without error */
147 if (dev->device.devargs != NULL &&
148 dev->device.devargs->policy ==
149 RTE_DEV_BLACKLISTED) {
150 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
155 if (dev->device.numa_node < 0) {
156 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
157 dev->device.numa_node = 0;
160 already_probed = rte_dev_is_probed(&dev->device);
161 if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
162 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
167 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
168 dev->id.device_id, dr->driver.name);
171 * reference driver structure
172 * This needs to be before rte_pci_map_device(), as it enables to use
173 * driver flags for adjusting configuration.
175 if (!already_probed) {
176 enum rte_iova_mode dev_iova_mode;
177 enum rte_iova_mode iova_mode;
179 dev_iova_mode = pci_device_iova_mode(dr, dev);
180 iova_mode = rte_eal_iova_mode();
181 if (dev_iova_mode != RTE_IOVA_DC &&
182 dev_iova_mode != iova_mode) {
183 RTE_LOG(ERR, EAL, " Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
184 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
185 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
192 if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
193 /* map resources for devices that use igb_uio */
194 ret = rte_pci_map_device(dev);
201 RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
202 dr->driver.name, dev->id.vendor_id, dev->id.device_id,
203 loc->domain, loc->bus, loc->devid, loc->function,
204 dev->device.numa_node);
205 /* call the driver probe() function */
206 ret = dr->probe(dr, dev);
208 return ret; /* no rollback if already succeeded earlier */
211 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
212 /* Don't unmap if device is unsupported and
213 * driver needs mapped resources.
216 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
217 rte_pci_unmap_device(dev);
219 dev->device.driver = &dr->driver;
226 * If vendor/device ID match, call the remove() function of the
230 rte_pci_detach_dev(struct rte_pci_device *dev)
232 struct rte_pci_addr *loc;
233 struct rte_pci_driver *dr;
242 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
243 loc->domain, loc->bus, loc->devid,
244 loc->function, dev->device.numa_node);
246 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
247 dev->id.device_id, dr->driver.name);
250 ret = dr->remove(dev);
255 /* clear driver structure */
257 dev->device.driver = NULL;
259 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
260 /* unmap resources for devices that use igb_uio */
261 rte_pci_unmap_device(dev);
267 * If vendor/device ID match, call the probe() function of all
268 * registered driver for the given device. Return < 0 if initialization
269 * failed, return 1 if no driver is found for this device.
272 pci_probe_all_drivers(struct rte_pci_device *dev)
274 struct rte_pci_driver *dr = NULL;
280 FOREACH_DRIVER_ON_PCIBUS(dr) {
281 rc = rte_pci_probe_one_driver(dr, dev);
283 /* negative value is an error */
286 /* positive value means driver doesn't support it */
294 * Scan the content of the PCI bus, and call the probe() function for
295 * all registered drivers that have a matching entry in its id_table
296 * for discovered devices.
301 struct rte_pci_device *dev = NULL;
302 size_t probed = 0, failed = 0;
305 FOREACH_DEVICE_ON_PCIBUS(dev) {
308 ret = pci_probe_all_drivers(dev);
310 if (ret != -EEXIST) {
311 RTE_LOG(ERR, EAL, "Requested device "
312 PCI_PRI_FMT " cannot be used\n",
313 dev->addr.domain, dev->addr.bus,
314 dev->addr.devid, dev->addr.function);
322 return (probed && probed == failed) ? -1 : 0;
325 /* dump one device */
327 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
331 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
332 dev->addr.devid, dev->addr.function);
333 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
336 for (i = 0; i != sizeof(dev->mem_resource) /
337 sizeof(dev->mem_resource[0]); i++) {
338 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
339 dev->mem_resource[i].phys_addr,
340 dev->mem_resource[i].len);
345 /* dump devices on the bus */
347 rte_pci_dump(FILE *f)
349 struct rte_pci_device *dev = NULL;
351 FOREACH_DEVICE_ON_PCIBUS(dev) {
352 pci_dump_one_device(f, dev);
357 pci_parse(const char *name, void *addr)
359 struct rte_pci_addr *out = addr;
360 struct rte_pci_addr pci_addr;
363 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
364 if (parse && addr != NULL)
366 return parse == false;
369 /* register a driver */
371 rte_pci_register(struct rte_pci_driver *driver)
373 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
374 driver->bus = &rte_pci_bus;
377 /* unregister a driver */
379 rte_pci_unregister(struct rte_pci_driver *driver)
381 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
385 /* Add a device to PCI bus */
387 rte_pci_add_device(struct rte_pci_device *pci_dev)
389 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
392 /* Insert a device into a predefined position in PCI bus */
394 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
395 struct rte_pci_device *new_pci_dev)
397 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
400 /* Remove a device from PCI bus */
402 rte_pci_remove_device(struct rte_pci_device *pci_dev)
404 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
407 static struct rte_device *
408 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
411 const struct rte_pci_device *pstart;
412 struct rte_pci_device *pdev;
415 pstart = RTE_DEV_TO_PCI_CONST(start);
416 pdev = TAILQ_NEXT(pstart, next);
418 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
420 while (pdev != NULL) {
421 if (cmp(&pdev->device, data) == 0)
422 return &pdev->device;
423 pdev = TAILQ_NEXT(pdev, next);
429 * find the device which encounter the failure, by iterate over all device on
430 * PCI bus to check if the memory failure address is located in the range
431 * of the BARs of the device.
433 static struct rte_pci_device *
434 pci_find_device_by_addr(const void *failure_addr)
436 struct rte_pci_device *pdev = NULL;
437 uint64_t check_point, start, end, len;
440 check_point = (uint64_t)(uintptr_t)failure_addr;
442 FOREACH_DEVICE_ON_PCIBUS(pdev) {
443 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
444 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
445 len = pdev->mem_resource[i].len;
447 if (check_point >= start && check_point < end) {
448 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
449 PRIx64" belongs to device %s!\n",
450 check_point, pdev->device.name);
459 pci_hot_unplug_handler(struct rte_device *dev)
461 struct rte_pci_device *pdev = NULL;
464 pdev = RTE_DEV_TO_PCI(dev);
468 switch (pdev->kdrv) {
469 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
472 * vfio kernel module guaranty the pci device would not be
473 * deleted until the user space release the resource, so no
474 * need to remap BARs resource here, just directly notify
475 * the req event to the user space to handle it.
477 rte_dev_event_callback_process(dev->name,
478 RTE_DEV_EVENT_REMOVE);
481 case RTE_KDRV_IGB_UIO:
482 case RTE_KDRV_UIO_GENERIC:
483 case RTE_KDRV_NIC_UIO:
484 /* BARs resource is invalid, remap it to be safe. */
485 ret = pci_uio_remap_resource(pdev);
489 "Not managed by a supported kernel driver, skipped\n");
498 pci_sigbus_handler(const void *failure_addr)
500 struct rte_pci_device *pdev = NULL;
503 pdev = pci_find_device_by_addr(failure_addr);
505 /* It is a generic sigbus error, no bus would handle it. */
508 /* The sigbus error is caused of hot-unplug. */
509 ret = pci_hot_unplug_handler(&pdev->device);
512 "Failed to handle hot-unplug for device %s",
521 pci_plug(struct rte_device *dev)
523 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
527 pci_unplug(struct rte_device *dev)
529 struct rte_pci_device *pdev;
532 pdev = RTE_DEV_TO_PCI(dev);
533 ret = rte_pci_detach_dev(pdev);
535 rte_pci_remove_device(pdev);
536 rte_devargs_remove(dev->devargs);
543 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
545 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
547 if (!pdev || !pdev->driver) {
551 if (pdev->driver->dma_map)
552 return pdev->driver->dma_map(pdev, addr, iova, len);
554 * In case driver don't provides any specific mapping
555 * try fallback to VFIO.
557 if (pdev->kdrv == RTE_KDRV_VFIO)
558 return rte_vfio_container_dma_map
559 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
566 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
568 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
570 if (!pdev || !pdev->driver) {
574 if (pdev->driver->dma_unmap)
575 return pdev->driver->dma_unmap(pdev, addr, iova, len);
577 * In case driver don't provides any specific mapping
578 * try fallback to VFIO.
580 if (pdev->kdrv == RTE_KDRV_VFIO)
581 return rte_vfio_container_dma_unmap
582 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
589 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
591 struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
593 switch (rte_pci_bus.bus.conf.scan_mode) {
594 case RTE_BUS_SCAN_WHITELIST:
595 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
598 case RTE_BUS_SCAN_UNDEFINED:
599 case RTE_BUS_SCAN_BLACKLIST:
600 if (devargs == NULL ||
601 devargs->policy != RTE_DEV_BLACKLISTED)
609 rte_pci_get_iommu_class(void)
611 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
612 const struct rte_pci_device *dev;
613 const struct rte_pci_driver *drv;
614 bool devices_want_va = false;
615 bool devices_want_pa = false;
616 int iommu_no_va = -1;
618 FOREACH_DEVICE_ON_PCIBUS(dev) {
620 * We can check this only once, because the IOMMU hardware is
621 * the same for all of them.
623 if (iommu_no_va == -1)
624 iommu_no_va = pci_device_iommu_support_va(dev)
627 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
628 dev->kdrv == RTE_KDRV_NONE)
630 FOREACH_DRIVER_ON_PCIBUS(drv) {
631 enum rte_iova_mode dev_iova_mode;
633 if (!rte_pci_match(drv, dev))
636 dev_iova_mode = pci_device_iova_mode(drv, dev);
637 RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
638 PCI_PRI_FMT " wants IOVA as '%s'\n",
640 dev->addr.domain, dev->addr.bus,
641 dev->addr.devid, dev->addr.function,
642 dev_iova_mode == RTE_IOVA_DC ? "DC" :
643 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
644 if (dev_iova_mode == RTE_IOVA_PA)
645 devices_want_pa = true;
646 else if (dev_iova_mode == RTE_IOVA_VA)
647 devices_want_va = true;
650 if (iommu_no_va == 1) {
651 iova_mode = RTE_IOVA_PA;
652 if (devices_want_va) {
653 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
654 RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
656 } else if (devices_want_va && !devices_want_pa) {
657 iova_mode = RTE_IOVA_VA;
658 } else if (devices_want_pa && !devices_want_va) {
659 iova_mode = RTE_IOVA_PA;
661 iova_mode = RTE_IOVA_DC;
662 if (devices_want_va) {
663 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
664 RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
670 struct rte_pci_bus rte_pci_bus = {
672 .scan = rte_pci_scan,
674 .find_device = pci_find_device,
676 .unplug = pci_unplug,
678 .dma_map = pci_dma_map,
679 .dma_unmap = pci_dma_unmap,
680 .get_iommu_class = rte_pci_get_iommu_class,
681 .dev_iterate = rte_pci_dev_iterate,
682 .hot_unplug_handler = pci_hot_unplug_handler,
683 .sigbus_handler = pci_sigbus_handler,
685 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
686 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
689 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);