1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_bus_pci.h>
12 #include <rte_malloc.h>
13 #include <rte_devargs.h>
14 #include <rte_memcpy.h>
17 #include "eal_filesystem.h"
24 * PCI probing under linux
26 * This code is used to simulate a PCI probe by parsing information in sysfs.
27 * When a registered device matches a driver, it is then initialized with
28 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
31 extern struct rte_pci_bus rte_pci_bus;
34 pci_get_kernel_driver_by_path(const char *filename, char *dri_name,
41 if (!filename || !dri_name)
44 count = readlink(filename, path, PATH_MAX);
45 if (count >= PATH_MAX)
48 /* For device does not have a driver */
54 name = strrchr(path, '/');
56 strlcpy(dri_name, name + 1, len);
65 rte_pci_map_device(struct rte_pci_device *dev)
69 /* try mapping the NIC resources using VFIO if it exists */
71 case RTE_PCI_KDRV_VFIO:
73 if (pci_vfio_is_enabled())
74 ret = pci_vfio_map_resource(dev);
77 case RTE_PCI_KDRV_IGB_UIO:
78 case RTE_PCI_KDRV_UIO_GENERIC:
79 if (rte_eal_using_phys_addrs()) {
80 /* map resources for devices that use uio */
81 ret = pci_uio_map_resource(dev);
86 " Not managed by a supported kernel driver, skipped\n");
94 /* Unmap pci device */
96 rte_pci_unmap_device(struct rte_pci_device *dev)
98 /* try unmapping the NIC resources using VFIO if it exists */
100 case RTE_PCI_KDRV_VFIO:
102 if (pci_vfio_is_enabled())
103 pci_vfio_unmap_resource(dev);
106 case RTE_PCI_KDRV_IGB_UIO:
107 case RTE_PCI_KDRV_UIO_GENERIC:
108 /* unmap resources for devices that use uio */
109 pci_uio_unmap_resource(dev);
113 " Not managed by a supported kernel driver, skipped\n");
119 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
121 size_t sz = msl->len;
122 void *end_va = RTE_PTR_ADD(msl->base_va, sz);
125 if (*max_va < end_va)
131 pci_find_max_end_va(void)
135 rte_memseg_list_walk(find_max_end_va, &va);
140 /* parse one line of the "resource" sysfs file (note that the 'line'
141 * string is modified)
144 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
145 uint64_t *end_addr, uint64_t *flags)
147 union pci_resource_info {
153 char *ptrs[PCI_RESOURCE_FMT_NVAL];
156 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
158 "%s(): bad resource format\n", __func__);
162 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
163 *end_addr = strtoull(res_info.end_addr, NULL, 16);
164 *flags = strtoull(res_info.flags, NULL, 16);
167 "%s(): bad resource format\n", __func__);
174 /* parse the "resource" sysfs file */
176 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
181 uint64_t phys_addr, end_addr, flags;
183 f = fopen(filename, "r");
185 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
189 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
191 if (fgets(buf, sizeof(buf), f) == NULL) {
193 "%s(): cannot read resource\n", __func__);
196 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
197 &end_addr, &flags) < 0)
200 if (flags & IORESOURCE_MEM) {
201 dev->mem_resource[i].phys_addr = phys_addr;
202 dev->mem_resource[i].len = end_addr - phys_addr + 1;
203 /* not mapped for now */
204 dev->mem_resource[i].addr = NULL;
215 /* Scan one pci sysfs entry, and fill the devices list from it. */
217 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
219 char filename[PATH_MAX];
221 struct rte_pci_device *dev;
222 char driver[PATH_MAX];
225 dev = malloc(sizeof(*dev));
229 memset(dev, 0, sizeof(*dev));
230 dev->device.bus = &rte_pci_bus.bus;
234 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
235 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
239 dev->id.vendor_id = (uint16_t)tmp;
242 snprintf(filename, sizeof(filename), "%s/device", dirname);
243 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
247 dev->id.device_id = (uint16_t)tmp;
249 /* get subsystem_vendor id */
250 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
252 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
256 dev->id.subsystem_vendor_id = (uint16_t)tmp;
258 /* get subsystem_device id */
259 snprintf(filename, sizeof(filename), "%s/subsystem_device",
261 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
265 dev->id.subsystem_device_id = (uint16_t)tmp;
268 snprintf(filename, sizeof(filename), "%s/class",
270 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
274 /* the least 24 bits are valid: class, subclass, program interface */
275 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
279 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
280 if (!access(filename, F_OK) &&
281 eal_parse_sysfs_value(filename, &tmp) == 0)
282 dev->max_vfs = (uint16_t)tmp;
284 /* for non igb_uio driver, need kernel version >= 3.8 */
285 snprintf(filename, sizeof(filename),
286 "%s/sriov_numvfs", dirname);
287 if (!access(filename, F_OK) &&
288 eal_parse_sysfs_value(filename, &tmp) == 0)
289 dev->max_vfs = (uint16_t)tmp;
292 /* get numa node, default to 0 if not present */
293 snprintf(filename, sizeof(filename), "%s/numa_node",
296 if (access(filename, F_OK) != -1) {
297 if (eal_parse_sysfs_value(filename, &tmp) == 0)
298 dev->device.numa_node = tmp;
300 dev->device.numa_node = -1;
302 dev->device.numa_node = 0;
307 /* parse resources */
308 snprintf(filename, sizeof(filename), "%s/resource", dirname);
309 if (pci_parse_sysfs_resource(filename, dev) < 0) {
310 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
316 snprintf(filename, sizeof(filename), "%s/driver", dirname);
317 ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
319 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
325 if (!strcmp(driver, "vfio-pci"))
326 dev->kdrv = RTE_PCI_KDRV_VFIO;
327 else if (!strcmp(driver, "igb_uio"))
328 dev->kdrv = RTE_PCI_KDRV_IGB_UIO;
329 else if (!strcmp(driver, "uio_pci_generic"))
330 dev->kdrv = RTE_PCI_KDRV_UIO_GENERIC;
332 dev->kdrv = RTE_PCI_KDRV_UNKNOWN;
334 dev->kdrv = RTE_PCI_KDRV_NONE;
337 /* device is valid, add in list (sorted) */
338 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
339 rte_pci_add_device(dev);
341 struct rte_pci_device *dev2;
344 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
345 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
350 rte_pci_insert_device(dev2, dev);
351 } else { /* already registered */
352 if (!rte_dev_is_probed(&dev2->device)) {
353 dev2->kdrv = dev->kdrv;
354 dev2->max_vfs = dev->max_vfs;
357 memmove(dev2->mem_resource,
359 sizeof(dev->mem_resource));
362 * If device is plugged and driver is
363 * probed already, (This happens when
364 * we call rte_dev_probe which will
365 * scan all device on the bus) we don't
366 * need to do anything here unless...
368 if (dev2->kdrv != dev->kdrv ||
369 dev2->max_vfs != dev->max_vfs ||
370 memcmp(&dev2->id, &dev->id, sizeof(dev2->id)))
372 * This should not happens.
373 * But it is still possible if
374 * we unbind a device from
375 * vfio or uio before hotplug
376 * remove and rebind it with
377 * a different configure.
378 * So we just print out the
381 RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n",
383 else if (dev2->device.devargs !=
384 dev->device.devargs) {
385 rte_devargs_remove(dev2->device.devargs);
394 rte_pci_add_device(dev);
401 * split up a pci address into its constituent parts.
404 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
406 /* first split on ':' */
414 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
417 char *buf_copy = strndup(buf, bufsize);
418 if (buf_copy == NULL)
421 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
424 /* final split is on '.' between devid and function */
425 splitaddr.function = strchr(splitaddr.devid,'.');
426 if (splitaddr.function == NULL)
428 *splitaddr.function++ = '\0';
430 /* now convert to int values */
432 addr->domain = strtoul(splitaddr.domain, NULL, 16);
433 addr->bus = strtoul(splitaddr.bus, NULL, 16);
434 addr->devid = strtoul(splitaddr.devid, NULL, 16);
435 addr->function = strtoul(splitaddr.function, NULL, 10);
439 free(buf_copy); /* free the copy made with strdup */
447 * Scan the content of the PCI bus, and the devices in the devices
455 char dirname[PATH_MAX];
456 struct rte_pci_addr addr;
458 /* for debug purposes, PCI can be disabled */
459 if (!rte_eal_has_pci())
463 if (!pci_vfio_is_enabled())
464 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
467 dir = opendir(rte_pci_get_sysfs_path());
469 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
470 __func__, strerror(errno));
474 while ((e = readdir(dir)) != NULL) {
475 if (e->d_name[0] == '.')
478 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
481 if (rte_pci_ignore_device(&addr))
484 snprintf(dirname, sizeof(dirname), "%s/%s",
485 rte_pci_get_sysfs_path(), e->d_name);
487 if (pci_scan_one(dirname, &addr) < 0)
498 #if defined(RTE_ARCH_X86)
500 pci_device_iommu_support_va(const struct rte_pci_device *dev)
502 #define VTD_CAP_MGAW_SHIFT 16
503 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
504 const struct rte_pci_addr *addr = &dev->addr;
505 char filename[PATH_MAX];
507 uint64_t mgaw, vtd_cap_reg = 0;
509 snprintf(filename, sizeof(filename),
510 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
511 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
514 fp = fopen(filename, "r");
516 /* We don't have an Intel IOMMU, assume VA supported */
520 RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n",
521 __func__, filename, strerror(errno));
525 /* We have an Intel IOMMU */
526 if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
527 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
534 mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
537 * Assuming there is no limitation by now. We can not know at this point
538 * because the memory has not been initialized yet. Setting the dma mask
539 * will force a check once memory initialization is done. We can not do
540 * a fallback to IOVA PA now, but if the dma check fails, the error
541 * message should advice for using '--iova-mode pa' if IOVA VA is the
544 rte_mem_set_dma_mask(mgaw);
547 #elif defined(RTE_ARCH_PPC_64)
549 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
552 * IOMMU is always present on a PowerNV host (IOMMUv2).
553 * IOMMU is also present in a KVM/QEMU VM (IOMMUv1) but is not
554 * currently supported by DPDK. Test for our current environment
555 * and report VA support as appropriate.
560 char filename[PATH_MAX] = "/proc/cpuinfo";
561 FILE *fp = fopen(filename, "r");
565 RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n",
566 __func__, filename, strerror(errno));
570 /* Check for a PowerNV platform */
571 while (getline(&line, &len, fp) != -1) {
572 if (strstr(line, "platform") != NULL)
575 if (strstr(line, "PowerNV") != NULL) {
576 RTE_LOG(DEBUG, EAL, "Running on a PowerNV system\n");
588 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
595 pci_device_iova_mode(const struct rte_pci_driver *pdrv,
596 const struct rte_pci_device *pdev)
598 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
600 switch (pdev->kdrv) {
601 case RTE_PCI_KDRV_VFIO: {
603 static int is_vfio_noiommu_enabled = -1;
605 if (is_vfio_noiommu_enabled == -1) {
606 if (rte_vfio_noiommu_is_enabled() == 1)
607 is_vfio_noiommu_enabled = 1;
609 is_vfio_noiommu_enabled = 0;
611 if (is_vfio_noiommu_enabled != 0)
612 iova_mode = RTE_IOVA_PA;
613 else if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
614 iova_mode = RTE_IOVA_VA;
619 case RTE_PCI_KDRV_IGB_UIO:
620 case RTE_PCI_KDRV_UIO_GENERIC:
621 iova_mode = RTE_IOVA_PA;
625 if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
626 iova_mode = RTE_IOVA_VA;
632 /* Read PCI config space. */
633 int rte_pci_read_config(const struct rte_pci_device *device,
634 void *buf, size_t len, off_t offset)
636 char devname[RTE_DEV_NAME_MAX_LEN] = "";
637 const struct rte_intr_handle *intr_handle = &device->intr_handle;
639 switch (device->kdrv) {
640 case RTE_PCI_KDRV_IGB_UIO:
641 case RTE_PCI_KDRV_UIO_GENERIC:
642 return pci_uio_read_config(intr_handle, buf, len, offset);
644 case RTE_PCI_KDRV_VFIO:
645 return pci_vfio_read_config(intr_handle, buf, len, offset);
648 rte_pci_device_name(&device->addr, devname,
649 RTE_DEV_NAME_MAX_LEN);
651 "Unknown driver type for %s\n", devname);
656 /* Write PCI config space. */
657 int rte_pci_write_config(const struct rte_pci_device *device,
658 const void *buf, size_t len, off_t offset)
660 char devname[RTE_DEV_NAME_MAX_LEN] = "";
661 const struct rte_intr_handle *intr_handle = &device->intr_handle;
663 switch (device->kdrv) {
664 case RTE_PCI_KDRV_IGB_UIO:
665 case RTE_PCI_KDRV_UIO_GENERIC:
666 return pci_uio_write_config(intr_handle, buf, len, offset);
668 case RTE_PCI_KDRV_VFIO:
669 return pci_vfio_write_config(intr_handle, buf, len, offset);
672 rte_pci_device_name(&device->addr, devname,
673 RTE_DEV_NAME_MAX_LEN);
675 "Unknown driver type for %s\n", devname);
681 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
682 struct rte_pci_ioport *p)
688 case RTE_PCI_KDRV_VFIO:
689 if (pci_vfio_is_enabled())
690 ret = pci_vfio_ioport_map(dev, bar, p);
693 case RTE_PCI_KDRV_IGB_UIO:
694 case RTE_PCI_KDRV_UIO_GENERIC:
695 ret = pci_uio_ioport_map(dev, bar, p);
708 rte_pci_ioport_read(struct rte_pci_ioport *p,
709 void *data, size_t len, off_t offset)
711 switch (p->dev->kdrv) {
713 case RTE_PCI_KDRV_VFIO:
714 pci_vfio_ioport_read(p, data, len, offset);
717 case RTE_PCI_KDRV_IGB_UIO:
718 pci_uio_ioport_read(p, data, len, offset);
720 case RTE_PCI_KDRV_UIO_GENERIC:
721 pci_uio_ioport_read(p, data, len, offset);
729 rte_pci_ioport_write(struct rte_pci_ioport *p,
730 const void *data, size_t len, off_t offset)
732 switch (p->dev->kdrv) {
734 case RTE_PCI_KDRV_VFIO:
735 pci_vfio_ioport_write(p, data, len, offset);
738 case RTE_PCI_KDRV_IGB_UIO:
739 pci_uio_ioport_write(p, data, len, offset);
741 case RTE_PCI_KDRV_UIO_GENERIC:
742 pci_uio_ioport_write(p, data, len, offset);
750 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
754 switch (p->dev->kdrv) {
756 case RTE_PCI_KDRV_VFIO:
757 if (pci_vfio_is_enabled())
758 ret = pci_vfio_ioport_unmap(p);
761 case RTE_PCI_KDRV_IGB_UIO:
762 case RTE_PCI_KDRV_UIO_GENERIC:
763 ret = pci_uio_ioport_unmap(p);