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);
101 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
106 /* unmap a particular resource */
108 pci_unmap_resource(void *requested_addr, size_t size)
110 if (requested_addr == NULL)
113 /* Unmap the PCI memory resource of device */
114 if (rte_mem_unmap(requested_addr, size)) {
115 RTE_LOG(ERR, EAL, "%s(): cannot mem unmap(%p, %#zx): %s\n",
116 __func__, requested_addr, size,
117 rte_strerror(rte_errno));
119 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
123 * Match the PCI Driver and Device using the ID Table
126 rte_pci_match(const struct rte_pci_driver *pci_drv,
127 const struct rte_pci_device *pci_dev)
129 const struct rte_pci_id *id_table;
131 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
133 /* check if device's identifiers match the driver's ones */
134 if (id_table->vendor_id != pci_dev->id.vendor_id &&
135 id_table->vendor_id != PCI_ANY_ID)
137 if (id_table->device_id != pci_dev->id.device_id &&
138 id_table->device_id != PCI_ANY_ID)
140 if (id_table->subsystem_vendor_id !=
141 pci_dev->id.subsystem_vendor_id &&
142 id_table->subsystem_vendor_id != PCI_ANY_ID)
144 if (id_table->subsystem_device_id !=
145 pci_dev->id.subsystem_device_id &&
146 id_table->subsystem_device_id != PCI_ANY_ID)
148 if (id_table->class_id != pci_dev->id.class_id &&
149 id_table->class_id != RTE_CLASS_ANY_ID)
159 * If vendor/device ID match, call the probe() function of the
163 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
164 struct rte_pci_device *dev)
168 struct rte_pci_addr *loc;
170 if ((dr == NULL) || (dev == NULL))
175 /* The device is not blacklisted; Check if driver supports it */
176 if (!rte_pci_match(dr, dev))
177 /* Match of device and driver failed */
180 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
181 loc->domain, loc->bus, loc->devid, loc->function,
182 dev->device.numa_node);
184 /* no initialization when blacklisted, return without error */
185 if (dev->device.devargs != NULL &&
186 dev->device.devargs->policy ==
187 RTE_DEV_BLACKLISTED) {
188 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
193 if (dev->device.numa_node < 0) {
194 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
195 dev->device.numa_node = 0;
198 already_probed = rte_dev_is_probed(&dev->device);
199 if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
200 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
205 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
206 dev->id.device_id, dr->driver.name);
209 * reference driver structure
210 * This needs to be before rte_pci_map_device(), as it enables to use
211 * driver flags for adjusting configuration.
213 if (!already_probed) {
214 enum rte_iova_mode dev_iova_mode;
215 enum rte_iova_mode iova_mode;
217 dev_iova_mode = pci_device_iova_mode(dr, dev);
218 iova_mode = rte_eal_iova_mode();
219 if (dev_iova_mode != RTE_IOVA_DC &&
220 dev_iova_mode != iova_mode) {
221 RTE_LOG(ERR, EAL, " Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
222 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
223 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
230 if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
231 /* map resources for devices that use igb_uio */
232 ret = rte_pci_map_device(dev);
239 RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
240 dr->driver.name, dev->id.vendor_id, dev->id.device_id,
241 loc->domain, loc->bus, loc->devid, loc->function,
242 dev->device.numa_node);
243 /* call the driver probe() function */
244 ret = dr->probe(dr, dev);
246 return ret; /* no rollback if already succeeded earlier */
249 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
250 /* Don't unmap if device is unsupported and
251 * driver needs mapped resources.
254 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
255 rte_pci_unmap_device(dev);
257 dev->device.driver = &dr->driver;
264 * If vendor/device ID match, call the remove() function of the
268 rte_pci_detach_dev(struct rte_pci_device *dev)
270 struct rte_pci_addr *loc;
271 struct rte_pci_driver *dr;
280 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
281 loc->domain, loc->bus, loc->devid,
282 loc->function, dev->device.numa_node);
284 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
285 dev->id.device_id, dr->driver.name);
288 ret = dr->remove(dev);
293 /* clear driver structure */
295 dev->device.driver = NULL;
297 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
298 /* unmap resources for devices that use igb_uio */
299 rte_pci_unmap_device(dev);
305 * If vendor/device ID match, call the probe() function of all
306 * registered driver for the given device. Return < 0 if initialization
307 * failed, return 1 if no driver is found for this device.
310 pci_probe_all_drivers(struct rte_pci_device *dev)
312 struct rte_pci_driver *dr = NULL;
318 FOREACH_DRIVER_ON_PCIBUS(dr) {
319 rc = rte_pci_probe_one_driver(dr, dev);
321 /* negative value is an error */
324 /* positive value means driver doesn't support it */
332 * Scan the content of the PCI bus, and call the probe() function for
333 * all registered drivers that have a matching entry in its id_table
334 * for discovered devices.
339 struct rte_pci_device *dev = NULL;
340 size_t probed = 0, failed = 0;
343 FOREACH_DEVICE_ON_PCIBUS(dev) {
346 ret = pci_probe_all_drivers(dev);
348 if (ret != -EEXIST) {
349 RTE_LOG(ERR, EAL, "Requested device "
350 PCI_PRI_FMT " cannot be used\n",
351 dev->addr.domain, dev->addr.bus,
352 dev->addr.devid, dev->addr.function);
360 return (probed && probed == failed) ? -1 : 0;
363 /* dump one device */
365 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
369 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
370 dev->addr.devid, dev->addr.function);
371 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
374 for (i = 0; i != sizeof(dev->mem_resource) /
375 sizeof(dev->mem_resource[0]); i++) {
376 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
377 dev->mem_resource[i].phys_addr,
378 dev->mem_resource[i].len);
383 /* dump devices on the bus */
385 rte_pci_dump(FILE *f)
387 struct rte_pci_device *dev = NULL;
389 FOREACH_DEVICE_ON_PCIBUS(dev) {
390 pci_dump_one_device(f, dev);
395 pci_parse(const char *name, void *addr)
397 struct rte_pci_addr *out = addr;
398 struct rte_pci_addr pci_addr;
401 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
402 if (parse && addr != NULL)
404 return parse == false;
407 /* register a driver */
409 rte_pci_register(struct rte_pci_driver *driver)
411 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
412 driver->bus = &rte_pci_bus;
415 /* unregister a driver */
417 rte_pci_unregister(struct rte_pci_driver *driver)
419 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
423 /* Add a device to PCI bus */
425 rte_pci_add_device(struct rte_pci_device *pci_dev)
427 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
430 /* Insert a device into a predefined position in PCI bus */
432 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
433 struct rte_pci_device *new_pci_dev)
435 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
438 /* Remove a device from PCI bus */
440 rte_pci_remove_device(struct rte_pci_device *pci_dev)
442 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
445 static struct rte_device *
446 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
449 const struct rte_pci_device *pstart;
450 struct rte_pci_device *pdev;
453 pstart = RTE_DEV_TO_PCI_CONST(start);
454 pdev = TAILQ_NEXT(pstart, next);
456 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
458 while (pdev != NULL) {
459 if (cmp(&pdev->device, data) == 0)
460 return &pdev->device;
461 pdev = TAILQ_NEXT(pdev, next);
467 * find the device which encounter the failure, by iterate over all device on
468 * PCI bus to check if the memory failure address is located in the range
469 * of the BARs of the device.
471 static struct rte_pci_device *
472 pci_find_device_by_addr(const void *failure_addr)
474 struct rte_pci_device *pdev = NULL;
475 uint64_t check_point, start, end, len;
478 check_point = (uint64_t)(uintptr_t)failure_addr;
480 FOREACH_DEVICE_ON_PCIBUS(pdev) {
481 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
482 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
483 len = pdev->mem_resource[i].len;
485 if (check_point >= start && check_point < end) {
486 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
487 PRIx64" belongs to device %s!\n",
488 check_point, pdev->device.name);
497 pci_hot_unplug_handler(struct rte_device *dev)
499 struct rte_pci_device *pdev = NULL;
502 pdev = RTE_DEV_TO_PCI(dev);
506 switch (pdev->kdrv) {
507 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
508 case RTE_PCI_KDRV_VFIO:
510 * vfio kernel module guaranty the pci device would not be
511 * deleted until the user space release the resource, so no
512 * need to remap BARs resource here, just directly notify
513 * the req event to the user space to handle it.
515 rte_dev_event_callback_process(dev->name,
516 RTE_DEV_EVENT_REMOVE);
519 case RTE_PCI_KDRV_IGB_UIO:
520 case RTE_PCI_KDRV_UIO_GENERIC:
521 case RTE_PCI_KDRV_NIC_UIO:
522 /* BARs resource is invalid, remap it to be safe. */
523 ret = pci_uio_remap_resource(pdev);
527 "Not managed by a supported kernel driver, skipped\n");
536 pci_sigbus_handler(const void *failure_addr)
538 struct rte_pci_device *pdev = NULL;
541 pdev = pci_find_device_by_addr(failure_addr);
543 /* It is a generic sigbus error, no bus would handle it. */
546 /* The sigbus error is caused of hot-unplug. */
547 ret = pci_hot_unplug_handler(&pdev->device);
550 "Failed to handle hot-unplug for device %s",
559 pci_plug(struct rte_device *dev)
561 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
565 pci_unplug(struct rte_device *dev)
567 struct rte_pci_device *pdev;
570 pdev = RTE_DEV_TO_PCI(dev);
571 ret = rte_pci_detach_dev(pdev);
573 rte_pci_remove_device(pdev);
574 rte_devargs_remove(dev->devargs);
581 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
583 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
585 if (!pdev || !pdev->driver) {
589 if (pdev->driver->dma_map)
590 return pdev->driver->dma_map(pdev, addr, iova, len);
592 * In case driver don't provides any specific mapping
593 * try fallback to VFIO.
595 if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
596 return rte_vfio_container_dma_map
597 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
604 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
606 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
608 if (!pdev || !pdev->driver) {
612 if (pdev->driver->dma_unmap)
613 return pdev->driver->dma_unmap(pdev, addr, iova, len);
615 * In case driver don't provides any specific mapping
616 * try fallback to VFIO.
618 if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
619 return rte_vfio_container_dma_unmap
620 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
627 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
629 struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
631 switch (rte_pci_bus.bus.conf.scan_mode) {
632 case RTE_BUS_SCAN_WHITELIST:
633 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
636 case RTE_BUS_SCAN_UNDEFINED:
637 case RTE_BUS_SCAN_BLACKLIST:
638 if (devargs == NULL ||
639 devargs->policy != RTE_DEV_BLACKLISTED)
647 rte_pci_get_iommu_class(void)
649 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
650 const struct rte_pci_device *dev;
651 const struct rte_pci_driver *drv;
652 bool devices_want_va = false;
653 bool devices_want_pa = false;
654 int iommu_no_va = -1;
656 FOREACH_DEVICE_ON_PCIBUS(dev) {
658 * We can check this only once, because the IOMMU hardware is
659 * the same for all of them.
661 if (iommu_no_va == -1)
662 iommu_no_va = pci_device_iommu_support_va(dev)
665 if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
666 dev->kdrv == RTE_PCI_KDRV_NONE)
668 FOREACH_DRIVER_ON_PCIBUS(drv) {
669 enum rte_iova_mode dev_iova_mode;
671 if (!rte_pci_match(drv, dev))
674 dev_iova_mode = pci_device_iova_mode(drv, dev);
675 RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
676 PCI_PRI_FMT " wants IOVA as '%s'\n",
678 dev->addr.domain, dev->addr.bus,
679 dev->addr.devid, dev->addr.function,
680 dev_iova_mode == RTE_IOVA_DC ? "DC" :
681 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
682 if (dev_iova_mode == RTE_IOVA_PA)
683 devices_want_pa = true;
684 else if (dev_iova_mode == RTE_IOVA_VA)
685 devices_want_va = true;
688 if (iommu_no_va == 1) {
689 iova_mode = RTE_IOVA_PA;
690 if (devices_want_va) {
691 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
692 RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
694 } else if (devices_want_va && !devices_want_pa) {
695 iova_mode = RTE_IOVA_VA;
696 } else if (devices_want_pa && !devices_want_va) {
697 iova_mode = RTE_IOVA_PA;
699 iova_mode = RTE_IOVA_DC;
700 if (devices_want_va) {
701 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
702 RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
709 rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap)
711 off_t offset = RTE_PCI_CFG_SPACE_SIZE;
715 /* minimum 8 bytes per capability */
716 ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
718 if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
719 RTE_LOG(ERR, EAL, "error in reading extended capabilities\n");
724 * If we have no capabilities, this is indicated by cap ID,
725 * cap version and next pointer all being 0.
731 if (RTE_PCI_EXT_CAP_ID(header) == cap)
734 offset = RTE_PCI_EXT_CAP_NEXT(header);
736 if (offset < RTE_PCI_CFG_SPACE_SIZE)
739 if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
741 "error in reading extended capabilities\n");
751 struct rte_pci_bus rte_pci_bus = {
753 .scan = rte_pci_scan,
755 .find_device = pci_find_device,
757 .unplug = pci_unplug,
759 .dma_map = pci_dma_map,
760 .dma_unmap = pci_dma_unmap,
761 .get_iommu_class = rte_pci_get_iommu_class,
762 .dev_iterate = rte_pci_dev_iterate,
763 .hot_unplug_handler = pci_hot_unplug_handler,
764 .sigbus_handler = pci_sigbus_handler,
766 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
767 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
770 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);