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"
29 void *pci_map_addr = NULL;
31 #define OFF_MAX ((uint64_t)(off_t)-1)
34 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
35 void *buf, size_t len, off_t offset)
37 return pread(intr_handle->uio_cfg_fd, buf, len, offset);
41 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
42 const void *buf, size_t len, off_t offset)
44 return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
48 pci_uio_set_bus_master(int dev_fd)
53 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
54 if (ret != sizeof(reg)) {
56 "Cannot read command from PCI config space!\n");
60 /* return if bus mastering is already on */
61 if (reg & PCI_COMMAND_MASTER)
64 reg |= PCI_COMMAND_MASTER;
66 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
67 if (ret != sizeof(reg)) {
69 "Cannot write command to PCI config space!\n");
77 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
80 char filename[PATH_MAX];
82 unsigned major, minor;
85 /* get the name of the sysfs file that contains the major and minor
86 * of the uio device and read its content */
87 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
89 f = fopen(filename, "r");
91 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
96 ret = fscanf(f, "%u:%u", &major, &minor);
98 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
105 /* create the char device "mknod /dev/uioX c major minor" */
106 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
107 dev = makedev(major, minor);
108 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
110 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
111 __func__, strerror(errno));
119 * Return the uioX char device used for a pci device. On success, return
120 * the UIO number and fill dstbuf string with the path of the device in
121 * sysfs. On error, return a negative value. In this case dstbuf is
125 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
126 unsigned int buflen, int create)
128 struct rte_pci_addr *loc = &dev->addr;
132 char dirname[PATH_MAX];
134 /* depending on kernel version, uio can be located in uio/uioX
137 snprintf(dirname, sizeof(dirname),
138 "%s/" PCI_PRI_FMT "/uio", rte_pci_get_sysfs_path(),
139 loc->domain, loc->bus, loc->devid, loc->function);
141 dir = opendir(dirname);
143 /* retry with the parent directory */
144 snprintf(dirname, sizeof(dirname),
145 "%s/" PCI_PRI_FMT, rte_pci_get_sysfs_path(),
146 loc->domain, loc->bus, loc->devid, loc->function);
147 dir = opendir(dirname);
150 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
155 /* take the first file starting with "uio" */
156 while ((e = readdir(dir)) != NULL) {
157 /* format could be uio%d ...*/
158 int shortprefix_len = sizeof("uio") - 1;
159 /* ... or uio:uio%d */
160 int longprefix_len = sizeof("uio:uio") - 1;
163 if (strncmp(e->d_name, "uio", 3) != 0)
166 /* first try uio%d */
168 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
169 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
170 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
174 /* then try uio:uio%d */
176 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
177 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
178 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
184 /* No uio resource found */
188 /* create uio device if we've been asked to */
189 if (rte_eal_create_uio_dev() && create &&
190 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
191 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
197 pci_uio_free_resource(struct rte_pci_device *dev,
198 struct mapped_pci_resource *uio_res)
202 if (dev->intr_handle.uio_cfg_fd >= 0) {
203 close(dev->intr_handle.uio_cfg_fd);
204 dev->intr_handle.uio_cfg_fd = -1;
206 if (dev->intr_handle.fd >= 0) {
207 close(dev->intr_handle.fd);
208 dev->intr_handle.fd = -1;
209 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
214 pci_uio_alloc_resource(struct rte_pci_device *dev,
215 struct mapped_pci_resource **uio_res)
217 char dirname[PATH_MAX];
218 char cfgname[PATH_MAX];
219 char devname[PATH_MAX]; /* contains the /dev/uioX */
221 struct rte_pci_addr *loc;
225 /* find uio resource */
226 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
228 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
229 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
232 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
234 /* save fd if in primary process */
235 dev->intr_handle.fd = open(devname, O_RDWR);
236 if (dev->intr_handle.fd < 0) {
237 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
238 devname, strerror(errno));
242 snprintf(cfgname, sizeof(cfgname),
243 "/sys/class/uio/uio%u/device/config", uio_num);
244 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
245 if (dev->intr_handle.uio_cfg_fd < 0) {
246 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
247 cfgname, strerror(errno));
251 if (dev->kdrv == RTE_KDRV_IGB_UIO)
252 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
254 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
256 /* set bus master that is not done by uio_pci_generic */
257 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
258 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
263 /* allocate the mapping details for secondary processes*/
264 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
265 if (*uio_res == NULL) {
267 "%s(): cannot store uio mmap details\n", __func__);
271 strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
272 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
277 pci_uio_free_resource(dev, *uio_res);
282 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
283 struct mapped_pci_resource *uio_res, int map_idx)
286 char devname[PATH_MAX];
288 struct rte_pci_addr *loc;
289 struct pci_map *maps;
292 if (dev->driver != NULL)
293 wc_activate = dev->driver->drv_flags & RTE_PCI_DRV_WC_ACTIVATE;
296 maps = uio_res->maps;
298 /* allocate memory to keep path */
299 maps[map_idx].path = rte_malloc(NULL, sizeof(devname), 0);
300 if (maps[map_idx].path == NULL) {
301 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
307 * open resource file, to mmap it
310 /* update devname for mmap */
311 snprintf(devname, sizeof(devname),
312 "%s/" PCI_PRI_FMT "/resource%d_wc",
313 rte_pci_get_sysfs_path(),
314 loc->domain, loc->bus, loc->devid,
315 loc->function, res_idx);
317 fd = open(devname, O_RDWR);
318 if (fd < 0 && errno != ENOENT) {
319 RTE_LOG(INFO, EAL, "%s cannot be mapped. "
320 "Fall-back to non prefetchable mode.\n",
325 if (!wc_activate || fd < 0) {
326 snprintf(devname, sizeof(devname),
327 "%s/" PCI_PRI_FMT "/resource%d",
328 rte_pci_get_sysfs_path(),
329 loc->domain, loc->bus, loc->devid,
330 loc->function, res_idx);
332 /* then try to map resource file */
333 fd = open(devname, O_RDWR);
335 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
336 devname, strerror(errno));
341 /* try mapping somewhere close to the end of hugepages */
342 if (pci_map_addr == NULL)
343 pci_map_addr = pci_find_max_end_va();
345 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
346 (size_t)dev->mem_resource[res_idx].len, 0);
348 if (mapaddr == MAP_FAILED)
351 pci_map_addr = RTE_PTR_ADD(mapaddr,
352 (size_t)dev->mem_resource[res_idx].len);
354 pci_map_addr = RTE_PTR_ALIGN(pci_map_addr, sysconf(_SC_PAGE_SIZE));
356 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
357 maps[map_idx].size = dev->mem_resource[res_idx].len;
358 maps[map_idx].addr = mapaddr;
359 maps[map_idx].offset = 0;
360 strcpy(maps[map_idx].path, devname);
361 dev->mem_resource[res_idx].addr = mapaddr;
366 rte_free(maps[map_idx].path);
370 #if defined(RTE_ARCH_X86)
372 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
373 struct rte_pci_ioport *p)
375 char dirname[PATH_MAX];
376 char filename[PATH_MAX];
380 if (rte_eal_iopl_init() != 0) {
381 RTE_LOG(ERR, EAL, "%s(): insufficient ioport permissions for PCI device %s\n",
382 __func__, dev->name);
386 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
390 /* get portio start */
391 snprintf(filename, sizeof(filename),
392 "%s/portio/port%d/start", dirname, bar);
393 if (eal_parse_sysfs_value(filename, &start) < 0) {
394 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
398 /* ensure we don't get anything funny here, read/write will cast to
400 if (start > UINT16_MAX)
403 /* FIXME only for primary process ? */
404 if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
406 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
407 dev->intr_handle.fd = open(filename, O_RDWR);
408 if (dev->intr_handle.fd < 0) {
409 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
410 filename, strerror(errno));
413 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
416 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
424 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
425 struct rte_pci_ioport *p)
429 char filename[PATH_MAX];
430 uint64_t phys_addr, end_addr, flags;
434 /* open and read addresses of the corresponding resource in sysfs */
435 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
436 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
437 dev->addr.devid, dev->addr.function);
438 f = fopen(filename, "r");
440 RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
444 for (i = 0; i < bar + 1; i++) {
445 if (fgets(buf, sizeof(buf), f) == NULL) {
446 RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
450 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
451 &end_addr, &flags) < 0)
453 if ((flags & IORESOURCE_IO) == 0) {
454 RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
457 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
458 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
459 dev->addr.devid, dev->addr.function, bar);
461 /* mmap the pci resource */
462 fd = open(filename, O_RDWR);
464 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
468 addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
471 if (addr == MAP_FAILED) {
472 RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
477 /* strangely, the base address is mmap addr + phys_addr */
478 p->base = (uintptr_t)addr + phys_addr;
479 p->len = end_addr + 1;
480 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
492 pci_uio_ioport_read(struct rte_pci_ioport *p,
493 void *data, size_t len, off_t offset)
497 uintptr_t reg = p->base + offset;
499 for (d = data; len > 0; d += size, reg += size, len -= size) {
502 #if defined(RTE_ARCH_X86)
503 *(uint32_t *)d = inl(reg);
505 *(uint32_t *)d = *(volatile uint32_t *)reg;
507 } else if (len >= 2) {
509 #if defined(RTE_ARCH_X86)
510 *(uint16_t *)d = inw(reg);
512 *(uint16_t *)d = *(volatile uint16_t *)reg;
516 #if defined(RTE_ARCH_X86)
519 *d = *(volatile uint8_t *)reg;
526 pci_uio_ioport_write(struct rte_pci_ioport *p,
527 const void *data, size_t len, off_t offset)
531 uintptr_t reg = p->base + offset;
533 for (s = data; len > 0; s += size, reg += size, len -= size) {
536 #if defined(RTE_ARCH_X86)
537 outl_p(*(const uint32_t *)s, reg);
539 *(volatile uint32_t *)reg = *(const uint32_t *)s;
541 } else if (len >= 2) {
543 #if defined(RTE_ARCH_X86)
544 outw_p(*(const uint16_t *)s, reg);
546 *(volatile uint16_t *)reg = *(const uint16_t *)s;
550 #if defined(RTE_ARCH_X86)
553 *(volatile uint8_t *)reg = *s;
560 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
562 #if defined(RTE_ARCH_X86)
564 /* FIXME close intr fd ? */
567 return munmap((void *)(uintptr_t)p->base, p->len);