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"
51 * PCI probing under linux
53 * This code is used to simulate a PCI probe by parsing information in sysfs.
54 * When a registered device matches a driver, it is then initialized with
55 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
58 extern struct rte_pci_bus rte_pci_bus;
61 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
67 if (!filename || !dri_name)
70 count = readlink(filename, path, PATH_MAX);
71 if (count >= PATH_MAX)
74 /* For device does not have a driver */
80 name = strrchr(path, '/');
82 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
91 rte_eal_pci_map_device(struct rte_pci_device *dev)
95 /* try mapping the NIC resources using VFIO if it exists */
99 if (pci_vfio_is_enabled())
100 ret = pci_vfio_map_resource(dev);
103 case RTE_KDRV_IGB_UIO:
104 case RTE_KDRV_UIO_GENERIC:
105 if (rte_eal_using_phys_addrs()) {
106 /* map resources for devices that use uio */
107 ret = pci_uio_map_resource(dev);
112 " Not managed by a supported kernel driver, skipped\n");
120 /* Unmap pci device */
122 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
124 /* try unmapping the NIC resources using VFIO if it exists */
128 if (pci_vfio_is_enabled())
129 pci_vfio_unmap_resource(dev);
132 case RTE_KDRV_IGB_UIO:
133 case RTE_KDRV_UIO_GENERIC:
134 /* unmap resources for devices that use uio */
135 pci_uio_unmap_resource(dev);
139 " Not managed by a supported kernel driver, skipped\n");
145 pci_find_max_end_va(void)
147 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
148 const struct rte_memseg *last = seg;
151 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
152 if (seg->addr == NULL)
155 if (seg->addr > last->addr)
159 return RTE_PTR_ADD(last->addr, last->len);
162 /* parse one line of the "resource" sysfs file (note that the 'line'
163 * string is modified)
166 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
167 uint64_t *end_addr, uint64_t *flags)
169 union pci_resource_info {
175 char *ptrs[PCI_RESOURCE_FMT_NVAL];
178 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
180 "%s(): bad resource format\n", __func__);
184 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
185 *end_addr = strtoull(res_info.end_addr, NULL, 16);
186 *flags = strtoull(res_info.flags, NULL, 16);
189 "%s(): bad resource format\n", __func__);
196 /* parse the "resource" sysfs file */
198 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
203 uint64_t phys_addr, end_addr, flags;
205 f = fopen(filename, "r");
207 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
211 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
213 if (fgets(buf, sizeof(buf), f) == NULL) {
215 "%s(): cannot read resource\n", __func__);
218 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
219 &end_addr, &flags) < 0)
222 if (flags & IORESOURCE_MEM) {
223 dev->mem_resource[i].phys_addr = phys_addr;
224 dev->mem_resource[i].len = end_addr - phys_addr + 1;
225 /* not mapped for now */
226 dev->mem_resource[i].addr = NULL;
237 /* Scan one pci sysfs entry, and fill the devices list from it. */
239 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
241 char filename[PATH_MAX];
243 struct rte_pci_device *dev;
244 char driver[PATH_MAX];
247 dev = malloc(sizeof(*dev));
251 memset(dev, 0, sizeof(*dev));
255 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
256 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
260 dev->id.vendor_id = (uint16_t)tmp;
263 snprintf(filename, sizeof(filename), "%s/device", dirname);
264 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
268 dev->id.device_id = (uint16_t)tmp;
270 /* get subsystem_vendor id */
271 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
273 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
277 dev->id.subsystem_vendor_id = (uint16_t)tmp;
279 /* get subsystem_device id */
280 snprintf(filename, sizeof(filename), "%s/subsystem_device",
282 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
286 dev->id.subsystem_device_id = (uint16_t)tmp;
289 snprintf(filename, sizeof(filename), "%s/class",
291 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
295 /* the least 24 bits are valid: class, subclass, program interface */
296 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
300 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
301 if (!access(filename, F_OK) &&
302 eal_parse_sysfs_value(filename, &tmp) == 0)
303 dev->max_vfs = (uint16_t)tmp;
305 /* for non igb_uio driver, need kernel version >= 3.8 */
306 snprintf(filename, sizeof(filename),
307 "%s/sriov_numvfs", dirname);
308 if (!access(filename, F_OK) &&
309 eal_parse_sysfs_value(filename, &tmp) == 0)
310 dev->max_vfs = (uint16_t)tmp;
314 snprintf(filename, sizeof(filename), "%s/numa_node",
316 if (access(filename, R_OK) != 0) {
317 /* if no NUMA support, set default to 0 */
318 dev->device.numa_node = 0;
320 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
324 dev->device.numa_node = tmp;
327 /* parse resources */
328 snprintf(filename, sizeof(filename), "%s/resource", dirname);
329 if (pci_parse_sysfs_resource(filename, dev) < 0) {
330 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
336 snprintf(filename, sizeof(filename), "%s/driver", dirname);
337 ret = pci_get_kernel_driver_by_path(filename, driver);
339 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
345 if (!strcmp(driver, "vfio-pci"))
346 dev->kdrv = RTE_KDRV_VFIO;
347 else if (!strcmp(driver, "igb_uio"))
348 dev->kdrv = RTE_KDRV_IGB_UIO;
349 else if (!strcmp(driver, "uio_pci_generic"))
350 dev->kdrv = RTE_KDRV_UIO_GENERIC;
352 dev->kdrv = RTE_KDRV_UNKNOWN;
354 dev->kdrv = RTE_KDRV_NONE;
356 /* device is valid, add in list (sorted) */
357 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
358 rte_eal_pci_add_device(dev);
360 struct rte_pci_device *dev2;
363 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
364 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
369 rte_eal_pci_insert_device(dev2, dev);
370 } else { /* already registered */
371 dev2->kdrv = dev->kdrv;
372 dev2->max_vfs = dev->max_vfs;
373 memmove(dev2->mem_resource, dev->mem_resource,
374 sizeof(dev->mem_resource));
380 rte_eal_pci_add_device(dev);
387 pci_update_device(const struct rte_pci_addr *addr)
389 char filename[PATH_MAX];
391 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
392 pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
395 return pci_scan_one(filename, addr);
399 * split up a pci address into its constituent parts.
402 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
404 /* first split on ':' */
412 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
415 char *buf_copy = strndup(buf, bufsize);
416 if (buf_copy == NULL)
419 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
422 /* final split is on '.' between devid and function */
423 splitaddr.function = strchr(splitaddr.devid,'.');
424 if (splitaddr.function == NULL)
426 *splitaddr.function++ = '\0';
428 /* now convert to int values */
430 addr->domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
431 addr->bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
432 addr->devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
433 addr->function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
437 free(buf_copy); /* free the copy made with strdup */
445 * Scan the content of the PCI bus, and the devices in the devices
449 rte_eal_pci_scan(void)
453 char dirname[PATH_MAX];
454 struct rte_pci_addr addr;
456 /* for debug purposes, PCI can be disabled */
457 if (internal_config.no_pci)
460 dir = opendir(pci_get_sysfs_path());
462 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
463 __func__, strerror(errno));
467 while ((e = readdir(dir)) != NULL) {
468 if (e->d_name[0] == '.')
471 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
474 snprintf(dirname, sizeof(dirname), "%s/%s",
475 pci_get_sysfs_path(), e->d_name);
477 if (pci_scan_one(dirname, &addr) < 0)
488 /* Read PCI config space. */
489 int rte_eal_pci_read_config(const struct rte_pci_device *device,
490 void *buf, size_t len, off_t offset)
492 const struct rte_intr_handle *intr_handle = &device->intr_handle;
494 switch (intr_handle->type) {
495 case RTE_INTR_HANDLE_UIO:
496 case RTE_INTR_HANDLE_UIO_INTX:
497 return pci_uio_read_config(intr_handle, buf, len, offset);
500 case RTE_INTR_HANDLE_VFIO_MSIX:
501 case RTE_INTR_HANDLE_VFIO_MSI:
502 case RTE_INTR_HANDLE_VFIO_LEGACY:
503 return pci_vfio_read_config(intr_handle, buf, len, offset);
507 "Unknown handle type of fd %d\n",
513 /* Write PCI config space. */
514 int rte_eal_pci_write_config(const struct rte_pci_device *device,
515 const void *buf, size_t len, off_t offset)
517 const struct rte_intr_handle *intr_handle = &device->intr_handle;
519 switch (intr_handle->type) {
520 case RTE_INTR_HANDLE_UIO:
521 case RTE_INTR_HANDLE_UIO_INTX:
522 return pci_uio_write_config(intr_handle, buf, len, offset);
525 case RTE_INTR_HANDLE_VFIO_MSIX:
526 case RTE_INTR_HANDLE_VFIO_MSI:
527 case RTE_INTR_HANDLE_VFIO_LEGACY:
528 return pci_vfio_write_config(intr_handle, buf, len, offset);
532 "Unknown handle type of fd %d\n",
538 #if defined(RTE_ARCH_X86)
540 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
541 struct rte_pci_ioport *p)
550 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
551 dev->addr.domain, dev->addr.bus,
552 dev->addr.devid, dev->addr.function);
554 fp = fopen("/proc/ioports", "r");
556 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
560 while (getdelim(&line, &linesz, '\n', fp) > 0) {
565 n = strcspn(ptr, ":");
569 while (*left && isspace(*left))
572 if (!strncmp(left, pci_id, strlen(pci_id))) {
575 while (*ptr && isspace(*ptr))
578 sscanf(ptr, "%04hx-%04hx", &start, &end);
590 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
592 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
599 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
600 struct rte_pci_ioport *p)
607 if (pci_vfio_is_enabled())
608 ret = pci_vfio_ioport_map(dev, bar, p);
611 case RTE_KDRV_IGB_UIO:
612 ret = pci_uio_ioport_map(dev, bar, p);
614 case RTE_KDRV_UIO_GENERIC:
615 #if defined(RTE_ARCH_X86)
616 ret = pci_ioport_map(dev, bar, p);
618 ret = pci_uio_ioport_map(dev, bar, p);
622 #if defined(RTE_ARCH_X86)
623 ret = pci_ioport_map(dev, bar, p);
637 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
638 void *data, size_t len, off_t offset)
640 switch (p->dev->kdrv) {
643 pci_vfio_ioport_read(p, data, len, offset);
646 case RTE_KDRV_IGB_UIO:
647 pci_uio_ioport_read(p, data, len, offset);
649 case RTE_KDRV_UIO_GENERIC:
650 pci_uio_ioport_read(p, data, len, offset);
653 #if defined(RTE_ARCH_X86)
654 pci_uio_ioport_read(p, data, len, offset);
663 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
664 const void *data, size_t len, off_t offset)
666 switch (p->dev->kdrv) {
669 pci_vfio_ioport_write(p, data, len, offset);
672 case RTE_KDRV_IGB_UIO:
673 pci_uio_ioport_write(p, data, len, offset);
675 case RTE_KDRV_UIO_GENERIC:
676 pci_uio_ioport_write(p, data, len, offset);
679 #if defined(RTE_ARCH_X86)
680 pci_uio_ioport_write(p, data, len, offset);
689 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
693 switch (p->dev->kdrv) {
696 if (pci_vfio_is_enabled())
697 ret = pci_vfio_ioport_unmap(p);
700 case RTE_KDRV_IGB_UIO:
701 ret = pci_uio_ioport_unmap(p);
703 case RTE_KDRV_UIO_GENERIC:
704 #if defined(RTE_ARCH_X86)
707 ret = pci_uio_ioport_unmap(p);
711 #if defined(RTE_ARCH_X86)