1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_bus_pci.h>
12 #include <rte_eal_memconfig.h>
13 #include <rte_malloc.h>
14 #include <rte_devargs.h>
15 #include <rte_memcpy.h>
18 #include "eal_private.h"
19 #include "eal_filesystem.h"
26 * PCI probing under linux
28 * This code is used to simulate a PCI probe by parsing information in sysfs.
29 * When a registered device matches a driver, it is then initialized with
30 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
33 extern struct rte_pci_bus rte_pci_bus;
36 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
42 if (!filename || !dri_name)
45 count = readlink(filename, path, PATH_MAX);
46 if (count >= PATH_MAX)
49 /* For device does not have a driver */
55 name = strrchr(path, '/');
57 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
66 rte_pci_map_device(struct rte_pci_device *dev)
70 /* try mapping the NIC resources using VFIO if it exists */
74 if (pci_vfio_is_enabled())
75 ret = pci_vfio_map_resource(dev);
78 case RTE_KDRV_IGB_UIO:
79 case RTE_KDRV_UIO_GENERIC:
80 if (rte_eal_using_phys_addrs()) {
81 /* map resources for devices that use uio */
82 ret = pci_uio_map_resource(dev);
87 " Not managed by a supported kernel driver, skipped\n");
95 /* Unmap pci device */
97 rte_pci_unmap_device(struct rte_pci_device *dev)
99 /* try unmapping the NIC resources using VFIO if it exists */
103 if (pci_vfio_is_enabled())
104 pci_vfio_unmap_resource(dev);
107 case RTE_KDRV_IGB_UIO:
108 case RTE_KDRV_UIO_GENERIC:
109 /* unmap resources for devices that use uio */
110 pci_uio_unmap_resource(dev);
114 " Not managed by a supported kernel driver, skipped\n");
120 find_max_end_va(const struct rte_memseg *ms, void *arg)
122 void *end_va = RTE_PTR_ADD(ms->addr, ms->len);
125 if (*max_va < end_va)
131 pci_find_max_end_va(void)
135 rte_memseg_walk(find_max_end_va, &va);
139 /* parse one line of the "resource" sysfs file (note that the 'line'
140 * string is modified)
143 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
144 uint64_t *end_addr, uint64_t *flags)
146 union pci_resource_info {
152 char *ptrs[PCI_RESOURCE_FMT_NVAL];
155 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
157 "%s(): bad resource format\n", __func__);
161 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
162 *end_addr = strtoull(res_info.end_addr, NULL, 16);
163 *flags = strtoull(res_info.flags, NULL, 16);
166 "%s(): bad resource format\n", __func__);
173 /* parse the "resource" sysfs file */
175 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
180 uint64_t phys_addr, end_addr, flags;
182 f = fopen(filename, "r");
184 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
188 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
190 if (fgets(buf, sizeof(buf), f) == NULL) {
192 "%s(): cannot read resource\n", __func__);
195 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
196 &end_addr, &flags) < 0)
199 if (flags & IORESOURCE_MEM) {
200 dev->mem_resource[i].phys_addr = phys_addr;
201 dev->mem_resource[i].len = end_addr - phys_addr + 1;
202 /* not mapped for now */
203 dev->mem_resource[i].addr = NULL;
214 /* Scan one pci sysfs entry, and fill the devices list from it. */
216 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
218 char filename[PATH_MAX];
220 struct rte_pci_device *dev;
221 char driver[PATH_MAX];
224 dev = malloc(sizeof(*dev));
228 memset(dev, 0, sizeof(*dev));
232 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
233 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
237 dev->id.vendor_id = (uint16_t)tmp;
240 snprintf(filename, sizeof(filename), "%s/device", dirname);
241 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
245 dev->id.device_id = (uint16_t)tmp;
247 /* get subsystem_vendor id */
248 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
250 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
254 dev->id.subsystem_vendor_id = (uint16_t)tmp;
256 /* get subsystem_device id */
257 snprintf(filename, sizeof(filename), "%s/subsystem_device",
259 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
263 dev->id.subsystem_device_id = (uint16_t)tmp;
266 snprintf(filename, sizeof(filename), "%s/class",
268 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
272 /* the least 24 bits are valid: class, subclass, program interface */
273 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
277 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
278 if (!access(filename, F_OK) &&
279 eal_parse_sysfs_value(filename, &tmp) == 0)
280 dev->max_vfs = (uint16_t)tmp;
282 /* for non igb_uio driver, need kernel version >= 3.8 */
283 snprintf(filename, sizeof(filename),
284 "%s/sriov_numvfs", dirname);
285 if (!access(filename, F_OK) &&
286 eal_parse_sysfs_value(filename, &tmp) == 0)
287 dev->max_vfs = (uint16_t)tmp;
290 /* get numa node, default to 0 if not present */
291 snprintf(filename, sizeof(filename), "%s/numa_node",
294 if (access(filename, F_OK) != -1) {
295 if (eal_parse_sysfs_value(filename, &tmp) == 0)
296 dev->device.numa_node = tmp;
298 dev->device.numa_node = -1;
300 dev->device.numa_node = 0;
305 /* parse resources */
306 snprintf(filename, sizeof(filename), "%s/resource", dirname);
307 if (pci_parse_sysfs_resource(filename, dev) < 0) {
308 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
314 snprintf(filename, sizeof(filename), "%s/driver", dirname);
315 ret = pci_get_kernel_driver_by_path(filename, driver);
317 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
323 if (!strcmp(driver, "vfio-pci"))
324 dev->kdrv = RTE_KDRV_VFIO;
325 else if (!strcmp(driver, "igb_uio"))
326 dev->kdrv = RTE_KDRV_IGB_UIO;
327 else if (!strcmp(driver, "uio_pci_generic"))
328 dev->kdrv = RTE_KDRV_UIO_GENERIC;
330 dev->kdrv = RTE_KDRV_UNKNOWN;
332 dev->kdrv = RTE_KDRV_NONE;
334 /* device is valid, add in list (sorted) */
335 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
336 rte_pci_add_device(dev);
338 struct rte_pci_device *dev2;
341 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
342 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
347 rte_pci_insert_device(dev2, dev);
348 } else { /* already registered */
349 dev2->kdrv = dev->kdrv;
350 dev2->max_vfs = dev->max_vfs;
352 memmove(dev2->mem_resource, dev->mem_resource,
353 sizeof(dev->mem_resource));
359 rte_pci_add_device(dev);
366 pci_update_device(const struct rte_pci_addr *addr)
368 char filename[PATH_MAX];
370 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
371 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
374 return pci_scan_one(filename, addr);
378 * split up a pci address into its constituent parts.
381 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
383 /* first split on ':' */
391 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
394 char *buf_copy = strndup(buf, bufsize);
395 if (buf_copy == NULL)
398 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
401 /* final split is on '.' between devid and function */
402 splitaddr.function = strchr(splitaddr.devid,'.');
403 if (splitaddr.function == NULL)
405 *splitaddr.function++ = '\0';
407 /* now convert to int values */
409 addr->domain = strtoul(splitaddr.domain, NULL, 16);
410 addr->bus = strtoul(splitaddr.bus, NULL, 16);
411 addr->devid = strtoul(splitaddr.devid, NULL, 16);
412 addr->function = strtoul(splitaddr.function, NULL, 10);
416 free(buf_copy); /* free the copy made with strdup */
424 * Scan the content of the PCI bus, and the devices in the devices
432 char dirname[PATH_MAX];
433 struct rte_pci_addr addr;
435 /* for debug purposes, PCI can be disabled */
436 if (!rte_eal_has_pci())
440 if (!pci_vfio_is_enabled())
441 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
444 dir = opendir(rte_pci_get_sysfs_path());
446 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
447 __func__, strerror(errno));
451 while ((e = readdir(dir)) != NULL) {
452 if (e->d_name[0] == '.')
455 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
458 snprintf(dirname, sizeof(dirname), "%s/%s",
459 rte_pci_get_sysfs_path(), e->d_name);
461 if (pci_scan_one(dirname, &addr) < 0)
473 * Is pci device bound to any kdrv
476 pci_one_device_is_bound(void)
478 struct rte_pci_device *dev = NULL;
481 FOREACH_DEVICE_ON_PCIBUS(dev) {
482 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
483 dev->kdrv == RTE_KDRV_NONE) {
494 * Any one of the device bound to uio
497 pci_one_device_bound_uio(void)
499 struct rte_pci_device *dev = NULL;
500 struct rte_devargs *devargs;
503 FOREACH_DEVICE_ON_PCIBUS(dev) {
504 devargs = dev->device.devargs;
507 switch (rte_pci_bus.bus.conf.scan_mode) {
508 case RTE_BUS_SCAN_WHITELIST:
509 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
512 case RTE_BUS_SCAN_UNDEFINED:
513 case RTE_BUS_SCAN_BLACKLIST:
514 if (devargs == NULL ||
515 devargs->policy != RTE_DEV_BLACKLISTED)
523 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
524 dev->kdrv == RTE_KDRV_UIO_GENERIC) {
532 * Any one of the device has iova as va
535 pci_one_device_has_iova_va(void)
537 struct rte_pci_device *dev = NULL;
538 struct rte_pci_driver *drv = NULL;
540 FOREACH_DRIVER_ON_PCIBUS(drv) {
541 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
542 FOREACH_DEVICE_ON_PCIBUS(dev) {
543 if (dev->kdrv == RTE_KDRV_VFIO &&
544 rte_pci_match(drv, dev))
552 #if defined(RTE_ARCH_X86)
554 pci_one_device_iommu_support_va(struct rte_pci_device *dev)
556 #define VTD_CAP_MGAW_SHIFT 16
557 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
558 #define X86_VA_WIDTH 47 /* From Documentation/x86/x86_64/mm.txt */
559 struct rte_pci_addr *addr = &dev->addr;
560 char filename[PATH_MAX];
562 uint64_t mgaw, vtd_cap_reg = 0;
564 snprintf(filename, sizeof(filename),
565 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
566 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
568 if (access(filename, F_OK) == -1) {
569 /* We don't have an Intel IOMMU, assume VA supported*/
573 /* We have an intel IOMMU */
574 fp = fopen(filename, "r");
576 RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
580 if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
581 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
588 mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
589 if (mgaw < X86_VA_WIDTH)
594 #elif defined(RTE_ARCH_PPC_64)
596 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
602 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
609 * All devices IOMMUs support VA as IOVA
612 pci_devices_iommu_support_va(void)
614 struct rte_pci_device *dev = NULL;
615 struct rte_pci_driver *drv = NULL;
617 FOREACH_DRIVER_ON_PCIBUS(drv) {
618 FOREACH_DEVICE_ON_PCIBUS(dev) {
619 if (!rte_pci_match(drv, dev))
621 if (!pci_one_device_iommu_support_va(dev))
629 * Get iommu class of PCI devices on the bus.
632 rte_pci_get_iommu_class(void)
635 bool is_vfio_noiommu_enabled = true;
640 is_bound = pci_one_device_is_bound();
644 has_iova_va = pci_one_device_has_iova_va();
645 is_bound_uio = pci_one_device_bound_uio();
646 iommu_no_va = !pci_devices_iommu_support_va();
648 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
652 if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
657 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
658 if (is_vfio_noiommu_enabled)
659 RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
661 RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
663 RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
669 /* Read PCI config space. */
670 int rte_pci_read_config(const struct rte_pci_device *device,
671 void *buf, size_t len, off_t offset)
673 const struct rte_intr_handle *intr_handle = &device->intr_handle;
675 switch (intr_handle->type) {
676 case RTE_INTR_HANDLE_UIO:
677 case RTE_INTR_HANDLE_UIO_INTX:
678 return pci_uio_read_config(intr_handle, buf, len, offset);
681 case RTE_INTR_HANDLE_VFIO_MSIX:
682 case RTE_INTR_HANDLE_VFIO_MSI:
683 case RTE_INTR_HANDLE_VFIO_LEGACY:
684 return pci_vfio_read_config(intr_handle, buf, len, offset);
688 "Unknown handle type of fd %d\n",
694 /* Write PCI config space. */
695 int rte_pci_write_config(const struct rte_pci_device *device,
696 const void *buf, size_t len, off_t offset)
698 const struct rte_intr_handle *intr_handle = &device->intr_handle;
700 switch (intr_handle->type) {
701 case RTE_INTR_HANDLE_UIO:
702 case RTE_INTR_HANDLE_UIO_INTX:
703 return pci_uio_write_config(intr_handle, buf, len, offset);
706 case RTE_INTR_HANDLE_VFIO_MSIX:
707 case RTE_INTR_HANDLE_VFIO_MSI:
708 case RTE_INTR_HANDLE_VFIO_LEGACY:
709 return pci_vfio_write_config(intr_handle, buf, len, offset);
713 "Unknown handle type of fd %d\n",
719 #if defined(RTE_ARCH_X86)
721 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
722 struct rte_pci_ioport *p)
731 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
732 dev->addr.domain, dev->addr.bus,
733 dev->addr.devid, dev->addr.function);
735 fp = fopen("/proc/ioports", "r");
737 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
741 while (getdelim(&line, &linesz, '\n', fp) > 0) {
746 n = strcspn(ptr, ":");
750 while (*left && isspace(*left))
753 if (!strncmp(left, pci_id, strlen(pci_id))) {
756 while (*ptr && isspace(*ptr))
759 sscanf(ptr, "%04hx-%04hx", &start, &end);
772 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
779 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
780 struct rte_pci_ioport *p)
787 if (pci_vfio_is_enabled())
788 ret = pci_vfio_ioport_map(dev, bar, p);
791 case RTE_KDRV_IGB_UIO:
792 ret = pci_uio_ioport_map(dev, bar, p);
794 case RTE_KDRV_UIO_GENERIC:
795 #if defined(RTE_ARCH_X86)
796 ret = pci_ioport_map(dev, bar, p);
798 ret = pci_uio_ioport_map(dev, bar, p);
802 #if defined(RTE_ARCH_X86)
803 ret = pci_ioport_map(dev, bar, p);
817 rte_pci_ioport_read(struct rte_pci_ioport *p,
818 void *data, size_t len, off_t offset)
820 switch (p->dev->kdrv) {
823 pci_vfio_ioport_read(p, data, len, offset);
826 case RTE_KDRV_IGB_UIO:
827 pci_uio_ioport_read(p, data, len, offset);
829 case RTE_KDRV_UIO_GENERIC:
830 pci_uio_ioport_read(p, data, len, offset);
833 #if defined(RTE_ARCH_X86)
834 pci_uio_ioport_read(p, data, len, offset);
843 rte_pci_ioport_write(struct rte_pci_ioport *p,
844 const void *data, size_t len, off_t offset)
846 switch (p->dev->kdrv) {
849 pci_vfio_ioport_write(p, data, len, offset);
852 case RTE_KDRV_IGB_UIO:
853 pci_uio_ioport_write(p, data, len, offset);
855 case RTE_KDRV_UIO_GENERIC:
856 pci_uio_ioport_write(p, data, len, offset);
859 #if defined(RTE_ARCH_X86)
860 pci_uio_ioport_write(p, data, len, offset);
869 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
873 switch (p->dev->kdrv) {
876 if (pci_vfio_is_enabled())
877 ret = pci_vfio_ioport_unmap(p);
880 case RTE_KDRV_IGB_UIO:
881 ret = pci_uio_ioport_unmap(p);
883 case RTE_KDRV_UIO_GENERIC:
884 #if defined(RTE_ARCH_X86)
887 ret = pci_uio_ioport_unmap(p);
891 #if defined(RTE_ARCH_X86)