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_eal_memconfig.h>
41 #include <rte_malloc.h>
42 #include <rte_devargs.h>
43 #include <rte_memcpy.h>
45 #include "eal_filesystem.h"
46 #include "eal_private.h"
47 #include "eal_pci_init.h"
52 * PCI probing under linux
54 * This code is used to simulate a PCI probe by parsing information in sysfs.
55 * When a registered device matches a driver, it is then initialized with
56 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
59 extern struct rte_pci_bus rte_pci_bus;
62 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
68 if (!filename || !dri_name)
71 count = readlink(filename, path, PATH_MAX);
72 if (count >= PATH_MAX)
75 /* For device does not have a driver */
81 name = strrchr(path, '/');
83 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
92 rte_pci_map_device(struct rte_pci_device *dev)
96 /* try mapping the NIC resources using VFIO if it exists */
100 if (pci_vfio_is_enabled())
101 ret = pci_vfio_map_resource(dev);
104 case RTE_KDRV_IGB_UIO:
105 case RTE_KDRV_UIO_GENERIC:
106 if (rte_eal_using_phys_addrs()) {
107 /* map resources for devices that use uio */
108 ret = pci_uio_map_resource(dev);
113 " Not managed by a supported kernel driver, skipped\n");
121 /* Unmap pci device */
123 rte_pci_unmap_device(struct rte_pci_device *dev)
125 /* try unmapping the NIC resources using VFIO if it exists */
129 if (pci_vfio_is_enabled())
130 pci_vfio_unmap_resource(dev);
133 case RTE_KDRV_IGB_UIO:
134 case RTE_KDRV_UIO_GENERIC:
135 /* unmap resources for devices that use uio */
136 pci_uio_unmap_resource(dev);
140 " Not managed by a supported kernel driver, skipped\n");
146 pci_find_max_end_va(void)
148 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
149 const struct rte_memseg *last = seg;
152 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
153 if (seg->addr == NULL)
156 if (seg->addr > last->addr)
160 return RTE_PTR_ADD(last->addr, last->len);
163 /* parse one line of the "resource" sysfs file (note that the 'line'
164 * string is modified)
167 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
168 uint64_t *end_addr, uint64_t *flags)
170 union pci_resource_info {
176 char *ptrs[PCI_RESOURCE_FMT_NVAL];
179 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
181 "%s(): bad resource format\n", __func__);
185 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
186 *end_addr = strtoull(res_info.end_addr, NULL, 16);
187 *flags = strtoull(res_info.flags, NULL, 16);
190 "%s(): bad resource format\n", __func__);
197 /* parse the "resource" sysfs file */
199 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
204 uint64_t phys_addr, end_addr, flags;
206 f = fopen(filename, "r");
208 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
212 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
214 if (fgets(buf, sizeof(buf), f) == NULL) {
216 "%s(): cannot read resource\n", __func__);
219 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
220 &end_addr, &flags) < 0)
223 if (flags & IORESOURCE_MEM) {
224 dev->mem_resource[i].phys_addr = phys_addr;
225 dev->mem_resource[i].len = end_addr - phys_addr + 1;
226 /* not mapped for now */
227 dev->mem_resource[i].addr = NULL;
238 /* Scan one pci sysfs entry, and fill the devices list from it. */
240 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
242 char filename[PATH_MAX];
244 struct rte_pci_device *dev;
245 char driver[PATH_MAX];
248 dev = malloc(sizeof(*dev));
252 memset(dev, 0, sizeof(*dev));
256 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
257 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
261 dev->id.vendor_id = (uint16_t)tmp;
264 snprintf(filename, sizeof(filename), "%s/device", dirname);
265 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
269 dev->id.device_id = (uint16_t)tmp;
271 /* get subsystem_vendor id */
272 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
274 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
278 dev->id.subsystem_vendor_id = (uint16_t)tmp;
280 /* get subsystem_device id */
281 snprintf(filename, sizeof(filename), "%s/subsystem_device",
283 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
287 dev->id.subsystem_device_id = (uint16_t)tmp;
290 snprintf(filename, sizeof(filename), "%s/class",
292 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
296 /* the least 24 bits are valid: class, subclass, program interface */
297 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
301 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
302 if (!access(filename, F_OK) &&
303 eal_parse_sysfs_value(filename, &tmp) == 0)
304 dev->max_vfs = (uint16_t)tmp;
306 /* for non igb_uio driver, need kernel version >= 3.8 */
307 snprintf(filename, sizeof(filename),
308 "%s/sriov_numvfs", dirname);
309 if (!access(filename, F_OK) &&
310 eal_parse_sysfs_value(filename, &tmp) == 0)
311 dev->max_vfs = (uint16_t)tmp;
314 /* get numa node, default to 0 if not present */
315 snprintf(filename, sizeof(filename), "%s/numa_node",
318 if (access(filename, F_OK) != -1) {
319 if (eal_parse_sysfs_value(filename, &tmp) == 0)
320 dev->device.numa_node = tmp;
322 dev->device.numa_node = -1;
324 dev->device.numa_node = 0;
329 /* parse resources */
330 snprintf(filename, sizeof(filename), "%s/resource", dirname);
331 if (pci_parse_sysfs_resource(filename, dev) < 0) {
332 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
338 snprintf(filename, sizeof(filename), "%s/driver", dirname);
339 ret = pci_get_kernel_driver_by_path(filename, driver);
341 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
347 if (!strcmp(driver, "vfio-pci"))
348 dev->kdrv = RTE_KDRV_VFIO;
349 else if (!strcmp(driver, "igb_uio"))
350 dev->kdrv = RTE_KDRV_IGB_UIO;
351 else if (!strcmp(driver, "uio_pci_generic"))
352 dev->kdrv = RTE_KDRV_UIO_GENERIC;
354 dev->kdrv = RTE_KDRV_UNKNOWN;
356 dev->kdrv = RTE_KDRV_NONE;
358 /* device is valid, add in list (sorted) */
359 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
360 rte_pci_add_device(dev);
362 struct rte_pci_device *dev2;
365 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
366 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
371 rte_pci_insert_device(dev2, dev);
372 } else { /* already registered */
373 dev2->kdrv = dev->kdrv;
374 dev2->max_vfs = dev->max_vfs;
376 memmove(dev2->mem_resource, dev->mem_resource,
377 sizeof(dev->mem_resource));
383 rte_pci_add_device(dev);
390 pci_update_device(const struct rte_pci_addr *addr)
392 char filename[PATH_MAX];
394 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
395 pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
398 return pci_scan_one(filename, addr);
402 * split up a pci address into its constituent parts.
405 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
407 /* first split on ':' */
415 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
418 char *buf_copy = strndup(buf, bufsize);
419 if (buf_copy == NULL)
422 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
425 /* final split is on '.' between devid and function */
426 splitaddr.function = strchr(splitaddr.devid,'.');
427 if (splitaddr.function == NULL)
429 *splitaddr.function++ = '\0';
431 /* now convert to int values */
433 addr->domain = strtoul(splitaddr.domain, NULL, 16);
434 addr->bus = strtoul(splitaddr.bus, NULL, 16);
435 addr->devid = strtoul(splitaddr.devid, NULL, 16);
436 addr->function = strtoul(splitaddr.function, NULL, 10);
440 free(buf_copy); /* free the copy made with strdup */
448 * Scan the content of the PCI bus, and the devices in the devices
456 char dirname[PATH_MAX];
457 struct rte_pci_addr addr;
459 /* for debug purposes, PCI can be disabled */
460 if (internal_config.no_pci)
463 dir = opendir(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 snprintf(dirname, sizeof(dirname), "%s/%s",
478 pci_get_sysfs_path(), e->d_name);
480 if (pci_scan_one(dirname, &addr) < 0)
492 * Is pci device bound to any kdrv
495 pci_one_device_is_bound(void)
497 struct rte_pci_device *dev = NULL;
500 FOREACH_DEVICE_ON_PCIBUS(dev) {
501 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
502 dev->kdrv == RTE_KDRV_NONE) {
513 * Any one of the device bound to uio
516 pci_one_device_bound_uio(void)
518 struct rte_pci_device *dev = NULL;
520 FOREACH_DEVICE_ON_PCIBUS(dev) {
521 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
522 dev->kdrv == RTE_KDRV_UIO_GENERIC) {
530 * Any one of the device has iova as va
533 pci_one_device_has_iova_va(void)
535 struct rte_pci_device *dev = NULL;
536 struct rte_pci_driver *drv = NULL;
538 FOREACH_DRIVER_ON_PCIBUS(drv) {
539 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
540 FOREACH_DEVICE_ON_PCIBUS(dev) {
541 if (dev->kdrv == RTE_KDRV_VFIO &&
542 rte_pci_match(drv, dev))
551 * Get iommu class of PCI devices on the bus.
554 rte_pci_get_iommu_class(void)
557 bool is_vfio_noiommu_enabled = true;
561 is_bound = pci_one_device_is_bound();
565 has_iova_va = pci_one_device_has_iova_va();
566 is_bound_uio = pci_one_device_bound_uio();
568 is_vfio_noiommu_enabled = vfio_noiommu_is_enabled() == true ?
572 if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled)
576 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
577 if (is_vfio_noiommu_enabled)
578 RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
580 RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
586 /* Read PCI config space. */
587 int rte_pci_read_config(const struct rte_pci_device *device,
588 void *buf, size_t len, off_t offset)
590 const struct rte_intr_handle *intr_handle = &device->intr_handle;
592 switch (intr_handle->type) {
593 case RTE_INTR_HANDLE_UIO:
594 case RTE_INTR_HANDLE_UIO_INTX:
595 return pci_uio_read_config(intr_handle, buf, len, offset);
598 case RTE_INTR_HANDLE_VFIO_MSIX:
599 case RTE_INTR_HANDLE_VFIO_MSI:
600 case RTE_INTR_HANDLE_VFIO_LEGACY:
601 return pci_vfio_read_config(intr_handle, buf, len, offset);
605 "Unknown handle type of fd %d\n",
611 /* Write PCI config space. */
612 int rte_pci_write_config(const struct rte_pci_device *device,
613 const void *buf, size_t len, off_t offset)
615 const struct rte_intr_handle *intr_handle = &device->intr_handle;
617 switch (intr_handle->type) {
618 case RTE_INTR_HANDLE_UIO:
619 case RTE_INTR_HANDLE_UIO_INTX:
620 return pci_uio_write_config(intr_handle, buf, len, offset);
623 case RTE_INTR_HANDLE_VFIO_MSIX:
624 case RTE_INTR_HANDLE_VFIO_MSI:
625 case RTE_INTR_HANDLE_VFIO_LEGACY:
626 return pci_vfio_write_config(intr_handle, buf, len, offset);
630 "Unknown handle type of fd %d\n",
636 #if defined(RTE_ARCH_X86)
638 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
639 struct rte_pci_ioport *p)
648 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
649 dev->addr.domain, dev->addr.bus,
650 dev->addr.devid, dev->addr.function);
652 fp = fopen("/proc/ioports", "r");
654 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
658 while (getdelim(&line, &linesz, '\n', fp) > 0) {
663 n = strcspn(ptr, ":");
667 while (*left && isspace(*left))
670 if (!strncmp(left, pci_id, strlen(pci_id))) {
673 while (*ptr && isspace(*ptr))
676 sscanf(ptr, "%04hx-%04hx", &start, &end);
688 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
690 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
697 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
698 struct rte_pci_ioport *p)
705 if (pci_vfio_is_enabled())
706 ret = pci_vfio_ioport_map(dev, bar, p);
709 case RTE_KDRV_IGB_UIO:
710 ret = pci_uio_ioport_map(dev, bar, p);
712 case RTE_KDRV_UIO_GENERIC:
713 #if defined(RTE_ARCH_X86)
714 ret = pci_ioport_map(dev, bar, p);
716 ret = pci_uio_ioport_map(dev, bar, p);
720 #if defined(RTE_ARCH_X86)
721 ret = pci_ioport_map(dev, bar, p);
735 rte_pci_ioport_read(struct rte_pci_ioport *p,
736 void *data, size_t len, off_t offset)
738 switch (p->dev->kdrv) {
741 pci_vfio_ioport_read(p, data, len, offset);
744 case RTE_KDRV_IGB_UIO:
745 pci_uio_ioport_read(p, data, len, offset);
747 case RTE_KDRV_UIO_GENERIC:
748 pci_uio_ioport_read(p, data, len, offset);
751 #if defined(RTE_ARCH_X86)
752 pci_uio_ioport_read(p, data, len, offset);
761 rte_pci_ioport_write(struct rte_pci_ioport *p,
762 const void *data, size_t len, off_t offset)
764 switch (p->dev->kdrv) {
767 pci_vfio_ioport_write(p, data, len, offset);
770 case RTE_KDRV_IGB_UIO:
771 pci_uio_ioport_write(p, data, len, offset);
773 case RTE_KDRV_UIO_GENERIC:
774 pci_uio_ioport_write(p, data, len, offset);
777 #if defined(RTE_ARCH_X86)
778 pci_uio_ioport_write(p, data, len, offset);
787 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
791 switch (p->dev->kdrv) {
794 if (pci_vfio_is_enabled())
795 ret = pci_vfio_ioport_unmap(p);
798 case RTE_KDRV_IGB_UIO:
799 ret = pci_uio_ioport_unmap(p);
801 case RTE_KDRV_UIO_GENERIC:
802 #if defined(RTE_ARCH_X86)
805 ret = pci_uio_ioport_unmap(p);
809 #if defined(RTE_ARCH_X86)