1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/sysmacros.h>
13 #include <linux/pci_regs.h>
15 #if defined(RTE_ARCH_X86)
19 #include <rte_string_fns.h>
22 #include <rte_bus_pci.h>
23 #include <rte_common.h>
24 #include <rte_malloc.h>
26 #include "eal_filesystem.h"
30 void *pci_map_addr = NULL;
32 #define OFF_MAX ((uint64_t)(off_t)-1)
35 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
36 void *buf, size_t len, off_t offset)
38 int uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
40 return pread(uio_cfg_fd, buf, len, offset);
44 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
45 const void *buf, size_t len, off_t offset)
47 int uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
49 return pwrite(uio_cfg_fd, buf, len, offset);
53 pci_uio_set_bus_master(int dev_fd)
58 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
59 if (ret != sizeof(reg)) {
61 "Cannot read command from PCI config space!\n");
65 /* return if bus mastering is already on */
66 if (reg & PCI_COMMAND_MASTER)
69 reg |= PCI_COMMAND_MASTER;
71 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
72 if (ret != sizeof(reg)) {
74 "Cannot write command to PCI config space!\n");
82 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
85 char filename[PATH_MAX];
87 unsigned major, minor;
90 /* get the name of the sysfs file that contains the major and minor
91 * of the uio device and read its content */
92 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
94 f = fopen(filename, "r");
96 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
101 ret = fscanf(f, "%u:%u", &major, &minor);
103 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
110 /* create the char device "mknod /dev/uioX c major minor" */
111 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
112 dev = makedev(major, minor);
113 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
115 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
116 __func__, strerror(errno));
124 * Return the uioX char device used for a pci device. On success, return
125 * the UIO number and fill dstbuf string with the path of the device in
126 * sysfs. On error, return a negative value. In this case dstbuf is
130 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
131 unsigned int buflen, int create)
133 struct rte_pci_addr *loc = &dev->addr;
137 char dirname[PATH_MAX];
139 /* depending on kernel version, uio can be located in uio/uioX
142 snprintf(dirname, sizeof(dirname),
143 "%s/" PCI_PRI_FMT "/uio", rte_pci_get_sysfs_path(),
144 loc->domain, loc->bus, loc->devid, loc->function);
146 dir = opendir(dirname);
148 /* retry with the parent directory */
149 snprintf(dirname, sizeof(dirname),
150 "%s/" PCI_PRI_FMT, rte_pci_get_sysfs_path(),
151 loc->domain, loc->bus, loc->devid, loc->function);
152 dir = opendir(dirname);
155 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
160 /* take the first file starting with "uio" */
161 while ((e = readdir(dir)) != NULL) {
162 /* format could be uio%d ...*/
163 int shortprefix_len = sizeof("uio") - 1;
164 /* ... or uio:uio%d */
165 int longprefix_len = sizeof("uio:uio") - 1;
168 if (strncmp(e->d_name, "uio", 3) != 0)
171 /* first try uio%d */
173 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
174 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
175 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
179 /* then try uio:uio%d */
181 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
182 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
183 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
189 /* No uio resource found */
193 /* create uio device if we've been asked to */
194 if (rte_eal_create_uio_dev() && create &&
195 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
196 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
202 pci_uio_free_resource(struct rte_pci_device *dev,
203 struct mapped_pci_resource *uio_res)
205 int uio_cfg_fd = rte_intr_dev_fd_get(dev->intr_handle);
209 if (uio_cfg_fd >= 0) {
211 rte_intr_dev_fd_set(dev->intr_handle, -1);
214 if (rte_intr_fd_get(dev->intr_handle) >= 0) {
215 close(rte_intr_fd_get(dev->intr_handle));
216 rte_intr_fd_set(dev->intr_handle, -1);
217 rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UNKNOWN);
222 pci_uio_alloc_resource(struct rte_pci_device *dev,
223 struct mapped_pci_resource **uio_res)
225 char dirname[PATH_MAX];
226 char cfgname[PATH_MAX];
227 char devname[PATH_MAX]; /* contains the /dev/uioX */
228 int uio_num, fd, uio_cfg_fd;
229 struct rte_pci_addr *loc;
233 /* find uio resource */
234 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
236 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
237 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
240 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
242 /* save fd if in primary process */
243 fd = open(devname, O_RDWR);
245 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
246 devname, strerror(errno));
250 if (rte_intr_fd_set(dev->intr_handle, fd))
253 snprintf(cfgname, sizeof(cfgname),
254 "/sys/class/uio/uio%u/device/config", uio_num);
256 uio_cfg_fd = open(cfgname, O_RDWR);
257 if (uio_cfg_fd < 0) {
258 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
259 cfgname, strerror(errno));
263 if (rte_intr_dev_fd_set(dev->intr_handle, uio_cfg_fd))
266 if (dev->kdrv == RTE_PCI_KDRV_IGB_UIO) {
267 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
270 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO_INTX))
273 /* set bus master that is not done by uio_pci_generic */
274 if (pci_uio_set_bus_master(uio_cfg_fd)) {
275 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
280 /* allocate the mapping details for secondary processes*/
281 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
282 if (*uio_res == NULL) {
284 "%s(): cannot store uio mmap details\n", __func__);
288 strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
289 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
294 pci_uio_free_resource(dev, *uio_res);
299 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
300 struct mapped_pci_resource *uio_res, int map_idx)
303 char devname[PATH_MAX];
305 struct rte_pci_addr *loc;
306 struct pci_map *maps;
309 if (dev->driver != NULL)
310 wc_activate = dev->driver->drv_flags & RTE_PCI_DRV_WC_ACTIVATE;
313 maps = uio_res->maps;
315 /* allocate memory to keep path */
316 maps[map_idx].path = rte_malloc(NULL, sizeof(devname), 0);
317 if (maps[map_idx].path == NULL) {
318 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
324 * open resource file, to mmap it
327 /* update devname for mmap */
328 snprintf(devname, sizeof(devname),
329 "%s/" PCI_PRI_FMT "/resource%d_wc",
330 rte_pci_get_sysfs_path(),
331 loc->domain, loc->bus, loc->devid,
332 loc->function, res_idx);
334 fd = open(devname, O_RDWR);
335 if (fd < 0 && errno != ENOENT) {
336 RTE_LOG(INFO, EAL, "%s cannot be mapped. "
337 "Fall-back to non prefetchable mode.\n",
342 if (!wc_activate || fd < 0) {
343 snprintf(devname, sizeof(devname),
344 "%s/" PCI_PRI_FMT "/resource%d",
345 rte_pci_get_sysfs_path(),
346 loc->domain, loc->bus, loc->devid,
347 loc->function, res_idx);
349 /* then try to map resource file */
350 fd = open(devname, O_RDWR);
352 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
353 devname, strerror(errno));
358 /* try mapping somewhere close to the end of hugepages */
359 if (pci_map_addr == NULL)
360 pci_map_addr = pci_find_max_end_va();
362 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
363 (size_t)dev->mem_resource[res_idx].len, 0);
368 pci_map_addr = RTE_PTR_ADD(mapaddr,
369 (size_t)dev->mem_resource[res_idx].len);
371 pci_map_addr = RTE_PTR_ALIGN(pci_map_addr, sysconf(_SC_PAGE_SIZE));
373 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
374 maps[map_idx].size = dev->mem_resource[res_idx].len;
375 maps[map_idx].addr = mapaddr;
376 maps[map_idx].offset = 0;
377 strcpy(maps[map_idx].path, devname);
378 dev->mem_resource[res_idx].addr = mapaddr;
383 rte_free(maps[map_idx].path);
387 #define PIO_MAX 0x10000
389 #if defined(RTE_ARCH_X86)
391 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
392 struct rte_pci_ioport *p)
395 char dirname[PATH_MAX];
396 char filename[PATH_MAX];
398 uint64_t phys_addr, end_addr, flags;
402 /* open and read addresses of the corresponding resource in sysfs */
403 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
404 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
405 dev->addr.devid, dev->addr.function);
406 f = fopen(filename, "r");
408 RTE_LOG(ERR, EAL, "%s(): Cannot open sysfs resource: %s\n",
409 __func__, strerror(errno));
413 for (i = 0; i < bar + 1; i++) {
414 if (fgets(buf, sizeof(buf), f) == NULL) {
415 RTE_LOG(ERR, EAL, "%s(): Cannot read sysfs resource\n", __func__);
419 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
420 &end_addr, &flags) < 0)
423 if (flags & IORESOURCE_IO) {
424 if (rte_eal_iopl_init()) {
425 RTE_LOG(ERR, EAL, "%s(): insufficient ioport permissions for PCI device %s\n",
426 __func__, dev->name);
430 base = (unsigned long)phys_addr;
431 if (base > PIO_MAX) {
432 RTE_LOG(ERR, EAL, "%s(): %08lx too large PIO resource\n", __func__, base);
436 RTE_LOG(DEBUG, EAL, "%s(): PIO BAR %08lx detected\n", __func__, base);
437 } else if (flags & IORESOURCE_MEM) {
438 base = (unsigned long)dev->mem_resource[bar].addr;
439 RTE_LOG(DEBUG, EAL, "%s(): MMIO BAR %08lx detected\n", __func__, base);
441 RTE_LOG(ERR, EAL, "%s(): unknown BAR type\n", __func__);
445 /* FIXME only for primary process ? */
446 if (rte_intr_type_get(dev->intr_handle) ==
447 RTE_INTR_HANDLE_UNKNOWN) {
448 int uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
450 RTE_LOG(ERR, EAL, "cannot open %s: %s\n",
451 dirname, strerror(errno));
455 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
456 fd = open(filename, O_RDWR);
458 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
459 filename, strerror(errno));
462 if (rte_intr_fd_set(dev->intr_handle, fd))
465 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
469 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", base);
482 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
483 struct rte_pci_ioport *p)
487 char filename[PATH_MAX];
488 uint64_t phys_addr, end_addr, flags;
492 /* open and read addresses of the corresponding resource in sysfs */
493 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
494 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
495 dev->addr.devid, dev->addr.function);
496 f = fopen(filename, "r");
498 RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
502 for (i = 0; i < bar + 1; i++) {
503 if (fgets(buf, sizeof(buf), f) == NULL) {
504 RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
508 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
509 &end_addr, &flags) < 0)
511 if ((flags & IORESOURCE_IO) == 0) {
512 RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
515 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
516 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
517 dev->addr.devid, dev->addr.function, bar);
519 /* mmap the pci resource */
520 fd = open(filename, O_RDWR);
522 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
526 addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
529 if (addr == MAP_FAILED) {
530 RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
535 /* strangely, the base address is mmap addr + phys_addr */
536 p->base = (uintptr_t)addr + phys_addr;
537 p->len = end_addr + 1;
538 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
549 #if defined(RTE_ARCH_X86)
551 static inline uint8_t ioread8(void *addr)
555 val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
556 *(volatile uint8_t *)addr :
558 inb_p((unsigned long)addr);
560 inb((unsigned long)addr);
566 static inline uint16_t ioread16(void *addr)
570 val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
571 *(volatile uint16_t *)addr :
573 inw_p((unsigned long)addr);
575 inw((unsigned long)addr);
581 static inline uint32_t ioread32(void *addr)
585 val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
586 *(volatile uint32_t *)addr :
588 inl_p((unsigned long)addr);
590 inl((unsigned long)addr);
596 static inline void iowrite8(uint8_t val, void *addr)
598 (uint64_t)(uintptr_t)addr >= PIO_MAX ?
599 *(volatile uint8_t *)addr = val :
601 outb_p(val, (unsigned long)addr);
603 outb(val, (unsigned long)addr);
607 static inline void iowrite16(uint16_t val, void *addr)
609 (uint64_t)(uintptr_t)addr >= PIO_MAX ?
610 *(volatile uint16_t *)addr = val :
612 outw_p(val, (unsigned long)addr);
614 outw(val, (unsigned long)addr);
618 static inline void iowrite32(uint32_t val, void *addr)
620 (uint64_t)(uintptr_t)addr >= PIO_MAX ?
621 *(volatile uint32_t *)addr = val :
623 outl_p(val, (unsigned long)addr);
625 outl(val, (unsigned long)addr);
629 #else /* !RTE_ARCH_X86 */
631 static inline uint8_t ioread8(void *addr)
633 return *(volatile uint8_t *)addr;
636 static inline uint16_t ioread16(void *addr)
638 return *(volatile uint16_t *)addr;
641 static inline uint32_t ioread32(void *addr)
643 return *(volatile uint32_t *)addr;
646 static inline void iowrite8(uint8_t val, void *addr)
648 *(volatile uint8_t *)addr = val;
651 static inline void iowrite16(uint16_t val, void *addr)
653 *(volatile uint16_t *)addr = val;
656 static inline void iowrite32(uint32_t val, void *addr)
658 *(volatile uint32_t *)addr = val;
661 #endif /* !RTE_ARCH_X86 */
664 pci_uio_ioport_read(struct rte_pci_ioport *p,
665 void *data, size_t len, off_t offset)
669 uintptr_t reg = p->base + offset;
671 for (d = data; len > 0; d += size, reg += size, len -= size) {
674 *(uint32_t *)d = ioread32((void *)reg);
675 } else if (len >= 2) {
677 *(uint16_t *)d = ioread16((void *)reg);
680 *d = ioread8((void *)reg);
686 pci_uio_ioport_write(struct rte_pci_ioport *p,
687 const void *data, size_t len, off_t offset)
691 uintptr_t reg = p->base + offset;
693 for (s = data; len > 0; s += size, reg += size, len -= size) {
696 iowrite32(*(const uint32_t *)s, (void *)reg);
697 } else if (len >= 2) {
699 iowrite16(*(const uint16_t *)s, (void *)reg);
702 iowrite8(*s, (void *)reg);
708 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
710 #if defined(RTE_ARCH_X86)
712 /* FIXME close intr fd ? */
715 return munmap((void *)(uintptr_t)p->base, p->len);