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);
43 return pread(uio_cfg_fd, buf, len, offset);
47 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
48 const void *buf, size_t len, off_t offset)
50 int uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
55 return pwrite(uio_cfg_fd, buf, len, offset);
59 pci_uio_set_bus_master(int dev_fd)
64 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
65 if (ret != sizeof(reg)) {
67 "Cannot read command from PCI config space!\n");
71 /* return if bus mastering is already on */
72 if (reg & PCI_COMMAND_MASTER)
75 reg |= PCI_COMMAND_MASTER;
77 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
78 if (ret != sizeof(reg)) {
80 "Cannot write command to PCI config space!\n");
88 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
91 char filename[PATH_MAX];
93 unsigned major, minor;
96 /* get the name of the sysfs file that contains the major and minor
97 * of the uio device and read its content */
98 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
100 f = fopen(filename, "r");
102 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
107 ret = fscanf(f, "%u:%u", &major, &minor);
109 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
116 /* create the char device "mknod /dev/uioX c major minor" */
117 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
118 dev = makedev(major, minor);
119 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
121 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
122 __func__, strerror(errno));
130 * Return the uioX char device used for a pci device. On success, return
131 * the UIO number and fill dstbuf string with the path of the device in
132 * sysfs. On error, return a negative value. In this case dstbuf is
136 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
137 unsigned int buflen, int create)
139 struct rte_pci_addr *loc = &dev->addr;
143 char dirname[PATH_MAX];
145 /* depending on kernel version, uio can be located in uio/uioX
148 snprintf(dirname, sizeof(dirname),
149 "%s/" PCI_PRI_FMT "/uio", rte_pci_get_sysfs_path(),
150 loc->domain, loc->bus, loc->devid, loc->function);
152 dir = opendir(dirname);
154 /* retry with the parent directory */
155 snprintf(dirname, sizeof(dirname),
156 "%s/" PCI_PRI_FMT, rte_pci_get_sysfs_path(),
157 loc->domain, loc->bus, loc->devid, loc->function);
158 dir = opendir(dirname);
161 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
166 /* take the first file starting with "uio" */
167 while ((e = readdir(dir)) != NULL) {
168 /* format could be uio%d ...*/
169 int shortprefix_len = sizeof("uio") - 1;
170 /* ... or uio:uio%d */
171 int longprefix_len = sizeof("uio:uio") - 1;
174 if (strncmp(e->d_name, "uio", 3) != 0)
177 /* first try uio%d */
179 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
180 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
181 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
185 /* then try uio:uio%d */
187 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
188 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
189 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
195 /* No uio resource found */
199 /* create uio device if we've been asked to */
200 if (rte_eal_create_uio_dev() && create &&
201 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
202 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
208 pci_uio_free_resource(struct rte_pci_device *dev,
209 struct mapped_pci_resource *uio_res)
211 int uio_cfg_fd = rte_intr_dev_fd_get(dev->intr_handle);
215 if (uio_cfg_fd >= 0) {
217 rte_intr_dev_fd_set(dev->intr_handle, -1);
220 if (rte_intr_fd_get(dev->intr_handle) >= 0) {
221 close(rte_intr_fd_get(dev->intr_handle));
222 rte_intr_fd_set(dev->intr_handle, -1);
223 rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UNKNOWN);
228 pci_uio_alloc_resource(struct rte_pci_device *dev,
229 struct mapped_pci_resource **uio_res)
231 char dirname[PATH_MAX];
232 char cfgname[PATH_MAX];
233 char devname[PATH_MAX]; /* contains the /dev/uioX */
234 int uio_num, fd, uio_cfg_fd;
235 struct rte_pci_addr *loc;
239 /* find uio resource */
240 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
242 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
243 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
246 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
248 /* save fd if in primary process */
249 fd = open(devname, O_RDWR);
251 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
252 devname, strerror(errno));
256 if (rte_intr_fd_set(dev->intr_handle, fd))
259 snprintf(cfgname, sizeof(cfgname),
260 "/sys/class/uio/uio%u/device/config", uio_num);
262 uio_cfg_fd = open(cfgname, O_RDWR);
263 if (uio_cfg_fd < 0) {
264 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
265 cfgname, strerror(errno));
269 if (rte_intr_dev_fd_set(dev->intr_handle, uio_cfg_fd))
272 if (dev->kdrv == RTE_PCI_KDRV_IGB_UIO) {
273 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
276 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO_INTX))
279 /* set bus master that is not done by uio_pci_generic */
280 if (pci_uio_set_bus_master(uio_cfg_fd)) {
281 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
286 /* allocate the mapping details for secondary processes*/
287 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
288 if (*uio_res == NULL) {
290 "%s(): cannot store uio mmap details\n", __func__);
294 strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
295 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
300 pci_uio_free_resource(dev, *uio_res);
305 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
306 struct mapped_pci_resource *uio_res, int map_idx)
309 char devname[PATH_MAX];
311 struct rte_pci_addr *loc;
312 struct pci_map *maps;
315 if (dev->driver != NULL)
316 wc_activate = dev->driver->drv_flags & RTE_PCI_DRV_WC_ACTIVATE;
319 maps = uio_res->maps;
321 /* allocate memory to keep path */
322 maps[map_idx].path = rte_malloc(NULL, sizeof(devname), 0);
323 if (maps[map_idx].path == NULL) {
324 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
330 * open resource file, to mmap it
333 /* update devname for mmap */
334 snprintf(devname, sizeof(devname),
335 "%s/" PCI_PRI_FMT "/resource%d_wc",
336 rte_pci_get_sysfs_path(),
337 loc->domain, loc->bus, loc->devid,
338 loc->function, res_idx);
340 fd = open(devname, O_RDWR);
341 if (fd < 0 && errno != ENOENT) {
342 RTE_LOG(INFO, EAL, "%s cannot be mapped. "
343 "Fall-back to non prefetchable mode.\n",
348 if (!wc_activate || fd < 0) {
349 snprintf(devname, sizeof(devname),
350 "%s/" PCI_PRI_FMT "/resource%d",
351 rte_pci_get_sysfs_path(),
352 loc->domain, loc->bus, loc->devid,
353 loc->function, res_idx);
355 /* then try to map resource file */
356 fd = open(devname, O_RDWR);
358 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
359 devname, strerror(errno));
364 /* try mapping somewhere close to the end of hugepages */
365 if (pci_map_addr == NULL)
366 pci_map_addr = pci_find_max_end_va();
368 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
369 (size_t)dev->mem_resource[res_idx].len, 0);
374 pci_map_addr = RTE_PTR_ADD(mapaddr,
375 (size_t)dev->mem_resource[res_idx].len);
377 pci_map_addr = RTE_PTR_ALIGN(pci_map_addr, sysconf(_SC_PAGE_SIZE));
379 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
380 maps[map_idx].size = dev->mem_resource[res_idx].len;
381 maps[map_idx].addr = mapaddr;
382 maps[map_idx].offset = 0;
383 strcpy(maps[map_idx].path, devname);
384 dev->mem_resource[res_idx].addr = mapaddr;
389 rte_free(maps[map_idx].path);
393 #define PIO_MAX 0x10000
395 #if defined(RTE_ARCH_X86)
397 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
398 struct rte_pci_ioport *p)
401 char dirname[PATH_MAX];
402 char filename[PATH_MAX];
404 uint64_t phys_addr, end_addr, flags;
408 /* open and read addresses of the corresponding resource in sysfs */
409 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
410 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
411 dev->addr.devid, dev->addr.function);
412 f = fopen(filename, "r");
414 RTE_LOG(ERR, EAL, "%s(): Cannot open sysfs resource: %s\n",
415 __func__, strerror(errno));
419 for (i = 0; i < bar + 1; i++) {
420 if (fgets(buf, sizeof(buf), f) == NULL) {
421 RTE_LOG(ERR, EAL, "%s(): Cannot read sysfs resource\n", __func__);
425 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
426 &end_addr, &flags) < 0)
429 if (flags & IORESOURCE_IO) {
430 if (rte_eal_iopl_init()) {
431 RTE_LOG(ERR, EAL, "%s(): insufficient ioport permissions for PCI device %s\n",
432 __func__, dev->name);
436 base = (unsigned long)phys_addr;
437 if (base > PIO_MAX) {
438 RTE_LOG(ERR, EAL, "%s(): %08lx too large PIO resource\n", __func__, base);
442 RTE_LOG(DEBUG, EAL, "%s(): PIO BAR %08lx detected\n", __func__, base);
443 } else if (flags & IORESOURCE_MEM) {
444 base = (unsigned long)dev->mem_resource[bar].addr;
445 RTE_LOG(DEBUG, EAL, "%s(): MMIO BAR %08lx detected\n", __func__, base);
447 RTE_LOG(ERR, EAL, "%s(): unknown BAR type\n", __func__);
451 /* FIXME only for primary process ? */
452 if (rte_intr_type_get(dev->intr_handle) ==
453 RTE_INTR_HANDLE_UNKNOWN) {
454 int uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
456 RTE_LOG(ERR, EAL, "cannot open %s: %s\n",
457 dirname, strerror(errno));
461 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
462 fd = open(filename, O_RDWR);
464 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
465 filename, strerror(errno));
468 if (rte_intr_fd_set(dev->intr_handle, fd))
471 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
475 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", base);
488 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
489 struct rte_pci_ioport *p)
493 char filename[PATH_MAX];
494 uint64_t phys_addr, end_addr, flags;
498 /* open and read addresses of the corresponding resource in sysfs */
499 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
500 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
501 dev->addr.devid, dev->addr.function);
502 f = fopen(filename, "r");
504 RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
508 for (i = 0; i < bar + 1; i++) {
509 if (fgets(buf, sizeof(buf), f) == NULL) {
510 RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
514 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
515 &end_addr, &flags) < 0)
517 if ((flags & IORESOURCE_IO) == 0) {
518 RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
521 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
522 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
523 dev->addr.devid, dev->addr.function, bar);
525 /* mmap the pci resource */
526 fd = open(filename, O_RDWR);
528 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
532 addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
535 if (addr == MAP_FAILED) {
536 RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
541 /* strangely, the base address is mmap addr + phys_addr */
542 p->base = (uintptr_t)addr + phys_addr;
543 p->len = end_addr + 1;
544 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
555 #if defined(RTE_ARCH_X86)
557 static inline uint8_t ioread8(void *addr)
561 val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
562 *(volatile uint8_t *)addr :
564 inb_p((unsigned long)addr);
566 inb((unsigned long)addr);
572 static inline uint16_t ioread16(void *addr)
576 val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
577 *(volatile uint16_t *)addr :
579 inw_p((unsigned long)addr);
581 inw((unsigned long)addr);
587 static inline uint32_t ioread32(void *addr)
591 val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
592 *(volatile uint32_t *)addr :
594 inl_p((unsigned long)addr);
596 inl((unsigned long)addr);
602 static inline void iowrite8(uint8_t val, void *addr)
604 (uint64_t)(uintptr_t)addr >= PIO_MAX ?
605 *(volatile uint8_t *)addr = val :
607 outb_p(val, (unsigned long)addr);
609 outb(val, (unsigned long)addr);
613 static inline void iowrite16(uint16_t val, void *addr)
615 (uint64_t)(uintptr_t)addr >= PIO_MAX ?
616 *(volatile uint16_t *)addr = val :
618 outw_p(val, (unsigned long)addr);
620 outw(val, (unsigned long)addr);
624 static inline void iowrite32(uint32_t val, void *addr)
626 (uint64_t)(uintptr_t)addr >= PIO_MAX ?
627 *(volatile uint32_t *)addr = val :
629 outl_p(val, (unsigned long)addr);
631 outl(val, (unsigned long)addr);
635 #else /* !RTE_ARCH_X86 */
637 static inline uint8_t ioread8(void *addr)
639 return *(volatile uint8_t *)addr;
642 static inline uint16_t ioread16(void *addr)
644 return *(volatile uint16_t *)addr;
647 static inline uint32_t ioread32(void *addr)
649 return *(volatile uint32_t *)addr;
652 static inline void iowrite8(uint8_t val, void *addr)
654 *(volatile uint8_t *)addr = val;
657 static inline void iowrite16(uint16_t val, void *addr)
659 *(volatile uint16_t *)addr = val;
662 static inline void iowrite32(uint32_t val, void *addr)
664 *(volatile uint32_t *)addr = val;
667 #endif /* !RTE_ARCH_X86 */
670 pci_uio_ioport_read(struct rte_pci_ioport *p,
671 void *data, size_t len, off_t offset)
675 uintptr_t reg = p->base + offset;
677 for (d = data; len > 0; d += size, reg += size, len -= size) {
680 *(uint32_t *)d = ioread32((void *)reg);
681 } else if (len >= 2) {
683 *(uint16_t *)d = ioread16((void *)reg);
686 *d = ioread8((void *)reg);
692 pci_uio_ioport_write(struct rte_pci_ioport *p,
693 const void *data, size_t len, off_t offset)
697 uintptr_t reg = p->base + offset;
699 for (s = data; len > 0; s += size, reg += size, len -= size) {
702 iowrite32(*(const uint32_t *)s, (void *)reg);
703 } else if (len >= 2) {
705 iowrite16(*(const uint16_t *)s, (void *)reg);
708 iowrite8(*s, (void *)reg);
714 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
716 #if defined(RTE_ARCH_X86)
718 /* FIXME close intr fd ? */
721 return munmap((void *)(uintptr_t)p->base, p->len);