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 using Linux sysfs.
27 extern struct rte_pci_bus rte_pci_bus;
30 pci_get_kernel_driver_by_path(const char *filename, char *dri_name,
37 if (!filename || !dri_name)
40 count = readlink(filename, path, PATH_MAX);
41 if (count >= PATH_MAX)
44 /* For device does not have a driver */
50 name = strrchr(path, '/');
52 strlcpy(dri_name, name + 1, len);
61 rte_pci_map_device(struct rte_pci_device *dev)
65 /* try mapping the NIC resources using VFIO if it exists */
67 case RTE_PCI_KDRV_VFIO:
69 if (pci_vfio_is_enabled())
70 ret = pci_vfio_map_resource(dev);
73 case RTE_PCI_KDRV_IGB_UIO:
74 case RTE_PCI_KDRV_UIO_GENERIC:
75 if (rte_eal_using_phys_addrs()) {
76 /* map resources for devices that use uio */
77 ret = pci_uio_map_resource(dev);
82 " Not managed by a supported kernel driver, skipped\n");
90 /* Unmap pci device */
92 rte_pci_unmap_device(struct rte_pci_device *dev)
94 /* try unmapping the NIC resources using VFIO if it exists */
96 case RTE_PCI_KDRV_VFIO:
98 if (pci_vfio_is_enabled())
99 pci_vfio_unmap_resource(dev);
102 case RTE_PCI_KDRV_IGB_UIO:
103 case RTE_PCI_KDRV_UIO_GENERIC:
104 /* unmap resources for devices that use uio */
105 pci_uio_unmap_resource(dev);
109 " Not managed by a supported kernel driver, skipped\n");
115 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
117 size_t sz = msl->len;
118 void *end_va = RTE_PTR_ADD(msl->base_va, sz);
121 if (*max_va < end_va)
127 pci_find_max_end_va(void)
131 rte_memseg_list_walk(find_max_end_va, &va);
136 /* parse one line of the "resource" sysfs file (note that the 'line'
137 * string is modified)
140 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
141 uint64_t *end_addr, uint64_t *flags)
143 union pci_resource_info {
149 char *ptrs[PCI_RESOURCE_FMT_NVAL];
152 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
154 "%s(): bad resource format\n", __func__);
158 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
159 *end_addr = strtoull(res_info.end_addr, NULL, 16);
160 *flags = strtoull(res_info.flags, NULL, 16);
163 "%s(): bad resource format\n", __func__);
170 /* parse the "resource" sysfs file */
172 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
177 uint64_t phys_addr, end_addr, flags;
179 f = fopen(filename, "r");
181 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
185 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
187 if (fgets(buf, sizeof(buf), f) == NULL) {
189 "%s(): cannot read resource\n", __func__);
192 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
193 &end_addr, &flags) < 0)
196 if (flags & IORESOURCE_MEM) {
197 dev->mem_resource[i].phys_addr = phys_addr;
198 dev->mem_resource[i].len = end_addr - phys_addr + 1;
199 /* not mapped for now */
200 dev->mem_resource[i].addr = NULL;
211 /* Scan one pci sysfs entry, and fill the devices list from it. */
213 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
215 char filename[PATH_MAX];
217 struct rte_pci_device *dev;
218 char driver[PATH_MAX];
221 dev = malloc(sizeof(*dev));
225 memset(dev, 0, sizeof(*dev));
226 dev->device.bus = &rte_pci_bus.bus;
230 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
231 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
235 dev->id.vendor_id = (uint16_t)tmp;
238 snprintf(filename, sizeof(filename), "%s/device", dirname);
239 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
243 dev->id.device_id = (uint16_t)tmp;
245 /* get subsystem_vendor id */
246 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
248 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
252 dev->id.subsystem_vendor_id = (uint16_t)tmp;
254 /* get subsystem_device id */
255 snprintf(filename, sizeof(filename), "%s/subsystem_device",
257 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
261 dev->id.subsystem_device_id = (uint16_t)tmp;
264 snprintf(filename, sizeof(filename), "%s/class",
266 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
270 /* the least 24 bits are valid: class, subclass, program interface */
271 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
275 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
276 if (!access(filename, F_OK) &&
277 eal_parse_sysfs_value(filename, &tmp) == 0)
278 dev->max_vfs = (uint16_t)tmp;
280 /* for non igb_uio driver, need kernel version >= 3.8 */
281 snprintf(filename, sizeof(filename),
282 "%s/sriov_numvfs", dirname);
283 if (!access(filename, F_OK) &&
284 eal_parse_sysfs_value(filename, &tmp) == 0)
285 dev->max_vfs = (uint16_t)tmp;
288 /* get numa node, default to 0 if not present */
289 snprintf(filename, sizeof(filename), "%s/numa_node",
292 if (access(filename, F_OK) != -1) {
293 if (eal_parse_sysfs_value(filename, &tmp) == 0)
294 dev->device.numa_node = tmp;
296 dev->device.numa_node = -1;
298 dev->device.numa_node = 0;
303 /* parse resources */
304 snprintf(filename, sizeof(filename), "%s/resource", dirname);
305 if (pci_parse_sysfs_resource(filename, dev) < 0) {
306 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
312 snprintf(filename, sizeof(filename), "%s/driver", dirname);
313 ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
315 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
321 if (!strcmp(driver, "vfio-pci"))
322 dev->kdrv = RTE_PCI_KDRV_VFIO;
323 else if (!strcmp(driver, "igb_uio"))
324 dev->kdrv = RTE_PCI_KDRV_IGB_UIO;
325 else if (!strcmp(driver, "uio_pci_generic"))
326 dev->kdrv = RTE_PCI_KDRV_UIO_GENERIC;
328 dev->kdrv = RTE_PCI_KDRV_UNKNOWN;
333 /* device is valid, add in list (sorted) */
334 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
335 rte_pci_add_device(dev);
337 struct rte_pci_device *dev2;
340 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
341 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
346 rte_pci_insert_device(dev2, dev);
347 } else { /* already registered */
348 if (!rte_dev_is_probed(&dev2->device)) {
349 dev2->kdrv = dev->kdrv;
350 dev2->max_vfs = dev->max_vfs;
353 memmove(dev2->mem_resource,
355 sizeof(dev->mem_resource));
358 * If device is plugged and driver is
359 * probed already, (This happens when
360 * we call rte_dev_probe which will
361 * scan all device on the bus) we don't
362 * need to do anything here unless...
364 if (dev2->kdrv != dev->kdrv ||
365 dev2->max_vfs != dev->max_vfs ||
366 memcmp(&dev2->id, &dev->id, sizeof(dev2->id)))
368 * This should not happens.
369 * But it is still possible if
370 * we unbind a device from
371 * vfio or uio before hotplug
372 * remove and rebind it with
373 * a different configure.
374 * So we just print out the
377 RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n",
379 else if (dev2->device.devargs !=
380 dev->device.devargs) {
381 rte_devargs_remove(dev2->device.devargs);
390 rte_pci_add_device(dev);
397 * split up a pci address into its constituent parts.
400 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
402 /* first split on ':' */
410 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
413 char *buf_copy = strndup(buf, bufsize);
414 if (buf_copy == NULL)
417 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
420 /* final split is on '.' between devid and function */
421 splitaddr.function = strchr(splitaddr.devid,'.');
422 if (splitaddr.function == NULL)
424 *splitaddr.function++ = '\0';
426 /* now convert to int values */
428 addr->domain = strtoul(splitaddr.domain, NULL, 16);
429 addr->bus = strtoul(splitaddr.bus, NULL, 16);
430 addr->devid = strtoul(splitaddr.devid, NULL, 16);
431 addr->function = strtoul(splitaddr.function, NULL, 10);
435 free(buf_copy); /* free the copy made with strdup */
443 * Scan the content of the PCI bus, and the devices in the devices
451 char dirname[PATH_MAX];
452 struct rte_pci_addr addr;
454 /* for debug purposes, PCI can be disabled */
455 if (!rte_eal_has_pci())
459 if (!pci_vfio_is_enabled())
460 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
463 dir = opendir(rte_pci_get_sysfs_path());
465 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
466 __func__, strerror(errno));
470 while ((e = readdir(dir)) != NULL) {
471 if (e->d_name[0] == '.')
474 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
477 if (rte_pci_ignore_device(&addr))
480 snprintf(dirname, sizeof(dirname), "%s/%s",
481 rte_pci_get_sysfs_path(), e->d_name);
483 if (pci_scan_one(dirname, &addr) < 0)
494 #if defined(RTE_ARCH_X86)
496 pci_device_iommu_support_va(const struct rte_pci_device *dev)
498 #define VTD_CAP_MGAW_SHIFT 16
499 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
500 const struct rte_pci_addr *addr = &dev->addr;
501 char filename[PATH_MAX];
503 uint64_t mgaw, vtd_cap_reg = 0;
505 snprintf(filename, sizeof(filename),
506 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
507 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
510 fp = fopen(filename, "r");
512 /* We don't have an Intel IOMMU, assume VA supported */
516 RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n",
517 __func__, filename, strerror(errno));
521 /* We have an Intel IOMMU */
522 if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
523 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
530 mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
533 * Assuming there is no limitation by now. We can not know at this point
534 * because the memory has not been initialized yet. Setting the dma mask
535 * will force a check once memory initialization is done. We can not do
536 * a fallback to IOVA PA now, but if the dma check fails, the error
537 * message should advice for using '--iova-mode pa' if IOVA VA is the
540 rte_mem_set_dma_mask(mgaw);
543 #elif defined(RTE_ARCH_PPC_64)
545 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
548 * All POWER systems support an IOMMU, but only IOMMUv2 supports
549 * IOVA = VA in DPDK. Check contents of /proc/cpuinfo to find the
552 * Platform | Model | IOMMU | VA? | Comment
553 * ---------+-------+---------+-----+---------------------------------
554 * PowerNV | N/A | IOMMUv2 | Yes | OpenPOWER (Bare Metal)
555 * pSeries | ~qemu | IOMMUv2 | Yes | PowerVM Logical Partition (LPAR)
556 * pSeries | qemu | IOMMUv1 | No | QEMU Virtual Machine
561 char filename[PATH_MAX] = "/proc/cpuinfo";
562 FILE *fp = fopen(filename, "r");
563 bool pseries = false, powernv = false, qemu = false;
567 RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n",
568 __func__, filename, strerror(errno));
572 /* Check the "platform" and "model" fields */
573 while (getline(&line, &len, fp) != -1) {
574 if (strstr(line, "platform") != NULL) {
575 if (strstr(line, "PowerNV") != NULL) {
576 RTE_LOG(DEBUG, EAL, "Running on a PowerNV platform\n");
578 } else if (strstr(line, "pSeries") != NULL) {
579 RTE_LOG(DEBUG, EAL, "Running on a pSeries platform\n");
582 } else if (strstr(line, "model") != NULL) {
583 if (strstr(line, "qemu") != NULL) {
584 RTE_LOG(DEBUG, EAL, "Found qemu emulation\n");
593 if (powernv || (pseries && !qemu))
599 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
606 pci_device_iova_mode(const struct rte_pci_driver *pdrv,
607 const struct rte_pci_device *pdev)
609 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
611 switch (pdev->kdrv) {
612 case RTE_PCI_KDRV_VFIO: {
614 static int is_vfio_noiommu_enabled = -1;
616 if (is_vfio_noiommu_enabled == -1) {
617 if (rte_vfio_noiommu_is_enabled() == 1)
618 is_vfio_noiommu_enabled = 1;
620 is_vfio_noiommu_enabled = 0;
622 if (is_vfio_noiommu_enabled != 0)
623 iova_mode = RTE_IOVA_PA;
624 else if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
625 iova_mode = RTE_IOVA_VA;
630 case RTE_PCI_KDRV_IGB_UIO:
631 case RTE_PCI_KDRV_UIO_GENERIC:
632 iova_mode = RTE_IOVA_PA;
636 if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
637 iova_mode = RTE_IOVA_VA;
643 /* Read PCI config space. */
644 int rte_pci_read_config(const struct rte_pci_device *device,
645 void *buf, size_t len, off_t offset)
647 char devname[RTE_DEV_NAME_MAX_LEN] = "";
648 const struct rte_intr_handle *intr_handle = &device->intr_handle;
650 switch (device->kdrv) {
651 case RTE_PCI_KDRV_IGB_UIO:
652 case RTE_PCI_KDRV_UIO_GENERIC:
653 return pci_uio_read_config(intr_handle, buf, len, offset);
655 case RTE_PCI_KDRV_VFIO:
656 return pci_vfio_read_config(intr_handle, buf, len, offset);
659 rte_pci_device_name(&device->addr, devname,
660 RTE_DEV_NAME_MAX_LEN);
662 "Unknown driver type for %s\n", devname);
667 /* Write PCI config space. */
668 int rte_pci_write_config(const struct rte_pci_device *device,
669 const void *buf, size_t len, off_t offset)
671 char devname[RTE_DEV_NAME_MAX_LEN] = "";
672 const struct rte_intr_handle *intr_handle = &device->intr_handle;
674 switch (device->kdrv) {
675 case RTE_PCI_KDRV_IGB_UIO:
676 case RTE_PCI_KDRV_UIO_GENERIC:
677 return pci_uio_write_config(intr_handle, buf, len, offset);
679 case RTE_PCI_KDRV_VFIO:
680 return pci_vfio_write_config(intr_handle, buf, len, offset);
683 rte_pci_device_name(&device->addr, devname,
684 RTE_DEV_NAME_MAX_LEN);
686 "Unknown driver type for %s\n", devname);
692 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
693 struct rte_pci_ioport *p)
699 case RTE_PCI_KDRV_VFIO:
700 if (pci_vfio_is_enabled())
701 ret = pci_vfio_ioport_map(dev, bar, p);
704 case RTE_PCI_KDRV_IGB_UIO:
705 case RTE_PCI_KDRV_UIO_GENERIC:
706 ret = pci_uio_ioport_map(dev, bar, p);
719 rte_pci_ioport_read(struct rte_pci_ioport *p,
720 void *data, size_t len, off_t offset)
722 switch (p->dev->kdrv) {
724 case RTE_PCI_KDRV_VFIO:
725 pci_vfio_ioport_read(p, data, len, offset);
728 case RTE_PCI_KDRV_IGB_UIO:
729 case RTE_PCI_KDRV_UIO_GENERIC:
730 pci_uio_ioport_read(p, data, len, offset);
738 rte_pci_ioport_write(struct rte_pci_ioport *p,
739 const void *data, size_t len, off_t offset)
741 switch (p->dev->kdrv) {
743 case RTE_PCI_KDRV_VFIO:
744 pci_vfio_ioport_write(p, data, len, offset);
747 case RTE_PCI_KDRV_IGB_UIO:
748 case RTE_PCI_KDRV_UIO_GENERIC:
749 pci_uio_ioport_write(p, data, len, offset);
757 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
761 switch (p->dev->kdrv) {
763 case RTE_PCI_KDRV_VFIO:
764 if (pci_vfio_is_enabled())
765 ret = pci_vfio_ioport_unmap(p);
768 case RTE_PCI_KDRV_IGB_UIO:
769 case RTE_PCI_KDRV_UIO_GENERIC:
770 ret = pci_uio_ioport_unmap(p);