4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_bus_pci.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_malloc.h>
43 #include <rte_devargs.h>
44 #include <rte_memcpy.h>
46 #include <rte_memory.h>
48 #include "eal_private.h"
49 #include "eal_filesystem.h"
56 * PCI probing under linux
58 * This code is used to simulate a PCI probe by parsing information in sysfs.
59 * When a registered device matches a driver, it is then initialized with
60 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
63 extern struct rte_pci_bus rte_pci_bus;
66 pci_get_kernel_driver_by_path(const char *filename, char *dri_name,
73 if (!filename || !dri_name)
76 count = readlink(filename, path, PATH_MAX);
77 if (count >= PATH_MAX)
80 /* For device does not have a driver */
86 name = strrchr(path, '/');
88 strlcpy(dri_name, name + 1, len);
97 rte_pci_map_device(struct rte_pci_device *dev)
101 /* try mapping the NIC resources using VFIO if it exists */
105 if (pci_vfio_is_enabled())
106 ret = pci_vfio_map_resource(dev);
109 case RTE_KDRV_IGB_UIO:
110 case RTE_KDRV_UIO_GENERIC:
111 if (rte_eal_using_phys_addrs()) {
112 /* map resources for devices that use uio */
113 ret = pci_uio_map_resource(dev);
118 " Not managed by a supported kernel driver, skipped\n");
126 /* Unmap pci device */
128 rte_pci_unmap_device(struct rte_pci_device *dev)
130 /* try unmapping the NIC resources using VFIO if it exists */
134 if (pci_vfio_is_enabled())
135 pci_vfio_unmap_resource(dev);
138 case RTE_KDRV_IGB_UIO:
139 case RTE_KDRV_UIO_GENERIC:
140 /* unmap resources for devices that use uio */
141 pci_uio_unmap_resource(dev);
145 " Not managed by a supported kernel driver, skipped\n");
151 pci_find_max_end_va(void)
153 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
154 const struct rte_memseg *last = seg;
157 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
158 if (seg->addr == NULL)
161 if (seg->addr > last->addr)
165 return RTE_PTR_ADD(last->addr, last->len);
168 /* parse one line of the "resource" sysfs file (note that the 'line'
169 * string is modified)
172 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
173 uint64_t *end_addr, uint64_t *flags)
175 union pci_resource_info {
181 char *ptrs[PCI_RESOURCE_FMT_NVAL];
184 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
186 "%s(): bad resource format\n", __func__);
190 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
191 *end_addr = strtoull(res_info.end_addr, NULL, 16);
192 *flags = strtoull(res_info.flags, NULL, 16);
195 "%s(): bad resource format\n", __func__);
202 /* parse the "resource" sysfs file */
204 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
209 uint64_t phys_addr, end_addr, flags;
211 f = fopen(filename, "r");
213 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
217 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
219 if (fgets(buf, sizeof(buf), f) == NULL) {
221 "%s(): cannot read resource\n", __func__);
224 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
225 &end_addr, &flags) < 0)
228 if (flags & IORESOURCE_MEM) {
229 dev->mem_resource[i].phys_addr = phys_addr;
230 dev->mem_resource[i].len = end_addr - phys_addr + 1;
231 /* not mapped for now */
232 dev->mem_resource[i].addr = NULL;
243 /* Scan one pci sysfs entry, and fill the devices list from it. */
245 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
247 char filename[PATH_MAX];
249 struct rte_pci_device *dev;
250 char driver[PATH_MAX];
253 dev = malloc(sizeof(*dev));
257 memset(dev, 0, sizeof(*dev));
261 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
262 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
266 dev->id.vendor_id = (uint16_t)tmp;
269 snprintf(filename, sizeof(filename), "%s/device", dirname);
270 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
274 dev->id.device_id = (uint16_t)tmp;
276 /* get subsystem_vendor id */
277 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
279 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
283 dev->id.subsystem_vendor_id = (uint16_t)tmp;
285 /* get subsystem_device id */
286 snprintf(filename, sizeof(filename), "%s/subsystem_device",
288 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
292 dev->id.subsystem_device_id = (uint16_t)tmp;
295 snprintf(filename, sizeof(filename), "%s/class",
297 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
301 /* the least 24 bits are valid: class, subclass, program interface */
302 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
306 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
307 if (!access(filename, F_OK) &&
308 eal_parse_sysfs_value(filename, &tmp) == 0)
309 dev->max_vfs = (uint16_t)tmp;
311 /* for non igb_uio driver, need kernel version >= 3.8 */
312 snprintf(filename, sizeof(filename),
313 "%s/sriov_numvfs", dirname);
314 if (!access(filename, F_OK) &&
315 eal_parse_sysfs_value(filename, &tmp) == 0)
316 dev->max_vfs = (uint16_t)tmp;
319 /* get numa node, default to 0 if not present */
320 snprintf(filename, sizeof(filename), "%s/numa_node",
323 if (access(filename, F_OK) != -1) {
324 if (eal_parse_sysfs_value(filename, &tmp) == 0)
325 dev->device.numa_node = tmp;
327 dev->device.numa_node = -1;
329 dev->device.numa_node = 0;
334 /* parse resources */
335 snprintf(filename, sizeof(filename), "%s/resource", dirname);
336 if (pci_parse_sysfs_resource(filename, dev) < 0) {
337 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
343 snprintf(filename, sizeof(filename), "%s/driver", dirname);
344 ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
346 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
352 if (!strcmp(driver, "vfio-pci"))
353 dev->kdrv = RTE_KDRV_VFIO;
354 else if (!strcmp(driver, "igb_uio"))
355 dev->kdrv = RTE_KDRV_IGB_UIO;
356 else if (!strcmp(driver, "uio_pci_generic"))
357 dev->kdrv = RTE_KDRV_UIO_GENERIC;
359 dev->kdrv = RTE_KDRV_UNKNOWN;
361 dev->kdrv = RTE_KDRV_NONE;
363 /* device is valid, add in list (sorted) */
364 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
365 rte_pci_add_device(dev);
367 struct rte_pci_device *dev2;
370 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
371 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
376 rte_pci_insert_device(dev2, dev);
377 } else { /* already registered */
378 dev2->kdrv = dev->kdrv;
379 dev2->max_vfs = dev->max_vfs;
381 memmove(dev2->mem_resource, dev->mem_resource,
382 sizeof(dev->mem_resource));
388 rte_pci_add_device(dev);
395 pci_update_device(const struct rte_pci_addr *addr)
397 char filename[PATH_MAX];
399 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
400 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
403 return pci_scan_one(filename, addr);
407 * split up a pci address into its constituent parts.
410 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
412 /* first split on ':' */
420 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
423 char *buf_copy = strndup(buf, bufsize);
424 if (buf_copy == NULL)
427 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
430 /* final split is on '.' between devid and function */
431 splitaddr.function = strchr(splitaddr.devid,'.');
432 if (splitaddr.function == NULL)
434 *splitaddr.function++ = '\0';
436 /* now convert to int values */
438 addr->domain = strtoul(splitaddr.domain, NULL, 16);
439 addr->bus = strtoul(splitaddr.bus, NULL, 16);
440 addr->devid = strtoul(splitaddr.devid, NULL, 16);
441 addr->function = strtoul(splitaddr.function, NULL, 10);
445 free(buf_copy); /* free the copy made with strdup */
453 * Scan the content of the PCI bus, and the devices in the devices
461 char dirname[PATH_MAX];
462 struct rte_pci_addr addr;
464 /* for debug purposes, PCI can be disabled */
465 if (!rte_eal_has_pci())
469 if (!pci_vfio_is_enabled())
470 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
473 dir = opendir(rte_pci_get_sysfs_path());
475 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
476 __func__, strerror(errno));
480 while ((e = readdir(dir)) != NULL) {
481 if (e->d_name[0] == '.')
484 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
487 snprintf(dirname, sizeof(dirname), "%s/%s",
488 rte_pci_get_sysfs_path(), e->d_name);
490 if (pci_scan_one(dirname, &addr) < 0)
502 * Is pci device bound to any kdrv
505 pci_one_device_is_bound(void)
507 struct rte_pci_device *dev = NULL;
510 FOREACH_DEVICE_ON_PCIBUS(dev) {
511 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
512 dev->kdrv == RTE_KDRV_NONE) {
523 * Any one of the device bound to uio
526 pci_one_device_bound_uio(void)
528 struct rte_pci_device *dev = NULL;
529 struct rte_devargs *devargs;
532 FOREACH_DEVICE_ON_PCIBUS(dev) {
533 devargs = dev->device.devargs;
536 switch (rte_pci_bus.bus.conf.scan_mode) {
537 case RTE_BUS_SCAN_WHITELIST:
538 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
541 case RTE_BUS_SCAN_UNDEFINED:
542 case RTE_BUS_SCAN_BLACKLIST:
543 if (devargs == NULL ||
544 devargs->policy != RTE_DEV_BLACKLISTED)
552 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
553 dev->kdrv == RTE_KDRV_UIO_GENERIC) {
561 * Any one of the device has iova as va
564 pci_one_device_has_iova_va(void)
566 struct rte_pci_device *dev = NULL;
567 struct rte_pci_driver *drv = NULL;
569 FOREACH_DRIVER_ON_PCIBUS(drv) {
570 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
571 FOREACH_DEVICE_ON_PCIBUS(dev) {
572 if (dev->kdrv == RTE_KDRV_VFIO &&
573 rte_pci_match(drv, dev))
581 #if defined(RTE_ARCH_X86)
583 pci_one_device_iommu_support_va(struct rte_pci_device *dev)
585 #define VTD_CAP_MGAW_SHIFT 16
586 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
587 struct rte_pci_addr *addr = &dev->addr;
588 char filename[PATH_MAX];
590 uint64_t mgaw, vtd_cap_reg = 0;
592 snprintf(filename, sizeof(filename),
593 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
594 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
596 if (access(filename, F_OK) == -1) {
597 /* We don't have an Intel IOMMU, assume VA supported*/
601 /* We have an intel IOMMU */
602 fp = fopen(filename, "r");
604 RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
608 if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
609 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
616 mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
618 if (!rte_eal_check_dma_mask(mgaw))
624 #elif defined(RTE_ARCH_PPC_64)
626 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
632 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
639 * All devices IOMMUs support VA as IOVA
642 pci_devices_iommu_support_va(void)
644 struct rte_pci_device *dev = NULL;
645 struct rte_pci_driver *drv = NULL;
646 int iommu_dma_mask_check_done = 0;
648 FOREACH_DRIVER_ON_PCIBUS(drv) {
649 FOREACH_DEVICE_ON_PCIBUS(dev) {
650 if (!rte_pci_match(drv, dev))
652 if (!iommu_dma_mask_check_done) {
653 if (!pci_one_device_iommu_support_va(dev))
655 iommu_dma_mask_check_done = 1;
663 * Get iommu class of PCI devices on the bus.
666 rte_pci_get_iommu_class(void)
669 bool is_vfio_noiommu_enabled = true;
674 is_bound = pci_one_device_is_bound();
678 has_iova_va = pci_one_device_has_iova_va();
679 is_bound_uio = pci_one_device_bound_uio();
680 iommu_no_va = !pci_devices_iommu_support_va();
682 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
686 if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
691 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
692 if (is_vfio_noiommu_enabled)
693 RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
695 RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
697 RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
703 /* Read PCI config space. */
704 int rte_pci_read_config(const struct rte_pci_device *device,
705 void *buf, size_t len, off_t offset)
707 char devname[RTE_DEV_NAME_MAX_LEN] = "";
708 const struct rte_intr_handle *intr_handle = &device->intr_handle;
710 switch (device->kdrv) {
711 case RTE_KDRV_IGB_UIO:
712 case RTE_KDRV_UIO_GENERIC:
713 return pci_uio_read_config(intr_handle, buf, len, offset);
716 return pci_vfio_read_config(intr_handle, buf, len, offset);
719 rte_pci_device_name(&device->addr, devname,
720 RTE_DEV_NAME_MAX_LEN);
722 "Unknown driver type for %s\n", devname);
727 /* Write PCI config space. */
728 int rte_pci_write_config(const struct rte_pci_device *device,
729 const void *buf, size_t len, off_t offset)
731 char devname[RTE_DEV_NAME_MAX_LEN] = "";
732 const struct rte_intr_handle *intr_handle = &device->intr_handle;
734 switch (device->kdrv) {
735 case RTE_KDRV_IGB_UIO:
736 case RTE_KDRV_UIO_GENERIC:
737 return pci_uio_write_config(intr_handle, buf, len, offset);
740 return pci_vfio_write_config(intr_handle, buf, len, offset);
743 rte_pci_device_name(&device->addr, devname,
744 RTE_DEV_NAME_MAX_LEN);
746 "Unknown driver type for %s\n", devname);
751 #if defined(RTE_ARCH_X86)
753 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
754 struct rte_pci_ioport *p)
763 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
764 dev->addr.domain, dev->addr.bus,
765 dev->addr.devid, dev->addr.function);
767 fp = fopen("/proc/ioports", "r");
769 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
773 while (getdelim(&line, &linesz, '\n', fp) > 0) {
778 n = strcspn(ptr, ":");
782 while (*left && isspace(*left))
785 if (!strncmp(left, pci_id, strlen(pci_id))) {
788 while (*ptr && isspace(*ptr))
791 sscanf(ptr, "%04hx-%04hx", &start, &end);
804 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
811 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
812 struct rte_pci_ioport *p)
819 if (pci_vfio_is_enabled())
820 ret = pci_vfio_ioport_map(dev, bar, p);
823 case RTE_KDRV_IGB_UIO:
824 ret = pci_uio_ioport_map(dev, bar, p);
826 case RTE_KDRV_UIO_GENERIC:
827 #if defined(RTE_ARCH_X86)
828 ret = pci_ioport_map(dev, bar, p);
830 ret = pci_uio_ioport_map(dev, bar, p);
834 #if defined(RTE_ARCH_X86)
835 ret = pci_ioport_map(dev, bar, p);
849 rte_pci_ioport_read(struct rte_pci_ioport *p,
850 void *data, size_t len, off_t offset)
852 switch (p->dev->kdrv) {
855 pci_vfio_ioport_read(p, data, len, offset);
858 case RTE_KDRV_IGB_UIO:
859 pci_uio_ioport_read(p, data, len, offset);
861 case RTE_KDRV_UIO_GENERIC:
862 pci_uio_ioport_read(p, data, len, offset);
865 #if defined(RTE_ARCH_X86)
866 pci_uio_ioport_read(p, data, len, offset);
875 rte_pci_ioport_write(struct rte_pci_ioport *p,
876 const void *data, size_t len, off_t offset)
878 switch (p->dev->kdrv) {
881 pci_vfio_ioport_write(p, data, len, offset);
884 case RTE_KDRV_IGB_UIO:
885 pci_uio_ioport_write(p, data, len, offset);
887 case RTE_KDRV_UIO_GENERIC:
888 pci_uio_ioport_write(p, data, len, offset);
891 #if defined(RTE_ARCH_X86)
892 pci_uio_ioport_write(p, data, len, offset);
901 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
905 switch (p->dev->kdrv) {
908 if (pci_vfio_is_enabled())
909 ret = pci_vfio_ioport_unmap(p);
912 case RTE_KDRV_IGB_UIO:
913 ret = pci_uio_ioport_unmap(p);
915 case RTE_KDRV_UIO_GENERIC:
916 #if defined(RTE_ARCH_X86)
919 ret = pci_uio_ioport_unmap(p);
923 #if defined(RTE_ARCH_X86)