New upstream version 18.11
[deb_dpdk.git] / drivers / bus / pci / linux / pci_uio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <dirent.h>
9 #include <inttypes.h>
10 #include <sys/stat.h>
11 #include <sys/mman.h>
12 #include <sys/sysmacros.h>
13 #include <linux/pci_regs.h>
14
15 #if defined(RTE_ARCH_X86)
16 #include <sys/io.h>
17 #endif
18
19 #include <rte_log.h>
20 #include <rte_pci.h>
21 #include <rte_bus_pci.h>
22 #include <rte_eal_memconfig.h>
23 #include <rte_common.h>
24 #include <rte_malloc.h>
25
26 #include "eal_filesystem.h"
27 #include "pci_init.h"
28
29 void *pci_map_addr = NULL;
30
31 #define OFF_MAX              ((uint64_t)(off_t)-1)
32
33 int
34 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
35                     void *buf, size_t len, off_t offset)
36 {
37         return pread(intr_handle->uio_cfg_fd, buf, len, offset);
38 }
39
40 int
41 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
42                      const void *buf, size_t len, off_t offset)
43 {
44         return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
45 }
46
47 static int
48 pci_uio_set_bus_master(int dev_fd)
49 {
50         uint16_t reg;
51         int ret;
52
53         ret = pread(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
54         if (ret != sizeof(reg)) {
55                 RTE_LOG(ERR, EAL,
56                         "Cannot read command from PCI config space!\n");
57                 return -1;
58         }
59
60         /* return if bus mastering is already on */
61         if (reg & PCI_COMMAND_MASTER)
62                 return 0;
63
64         reg |= PCI_COMMAND_MASTER;
65
66         ret = pwrite(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
67         if (ret != sizeof(reg)) {
68                 RTE_LOG(ERR, EAL,
69                         "Cannot write command to PCI config space!\n");
70                 return -1;
71         }
72
73         return 0;
74 }
75
76 static int
77 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
78 {
79         FILE *f;
80         char filename[PATH_MAX];
81         int ret;
82         unsigned major, minor;
83         dev_t dev;
84
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);
88
89         f = fopen(filename, "r");
90         if (f == NULL) {
91                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
92                         __func__);
93                 return -1;
94         }
95
96         ret = fscanf(f, "%u:%u", &major, &minor);
97         if (ret != 2) {
98                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
99                         __func__);
100                 fclose(f);
101                 return -1;
102         }
103         fclose(f);
104
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);
109         if (ret != 0) {
110                 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
111                         __func__, strerror(errno));
112                 return -1;
113         }
114
115         return ret;
116 }
117
118 /*
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
122  * invalid.
123  */
124 static int
125 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
126                            unsigned int buflen, int create)
127 {
128         struct rte_pci_addr *loc = &dev->addr;
129         int uio_num = -1;
130         struct dirent *e;
131         DIR *dir;
132         char dirname[PATH_MAX];
133
134         /* depending on kernel version, uio can be located in uio/uioX
135          * or uio:uioX */
136
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);
140
141         dir = opendir(dirname);
142         if (dir == NULL) {
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);
148
149                 if (dir == NULL) {
150                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
151                         return -1;
152                 }
153         }
154
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;
161                 char *endptr;
162
163                 if (strncmp(e->d_name, "uio", 3) != 0)
164                         continue;
165
166                 /* first try uio%d */
167                 errno = 0;
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);
171                         break;
172                 }
173
174                 /* then try uio:uio%d */
175                 errno = 0;
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);
179                         break;
180                 }
181         }
182         closedir(dir);
183
184         /* No uio resource found */
185         if (e == NULL)
186                 return -1;
187
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);
192
193         return uio_num;
194 }
195
196 void
197 pci_uio_free_resource(struct rte_pci_device *dev,
198                 struct mapped_pci_resource *uio_res)
199 {
200         rte_free(uio_res);
201
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;
205         }
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;
210         }
211 }
212
213 int
214 pci_uio_alloc_resource(struct rte_pci_device *dev,
215                 struct mapped_pci_resource **uio_res)
216 {
217         char dirname[PATH_MAX];
218         char cfgname[PATH_MAX];
219         char devname[PATH_MAX]; /* contains the /dev/uioX */
220         int uio_num;
221         struct rte_pci_addr *loc;
222
223         loc = &dev->addr;
224
225         /* find uio resource */
226         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
227         if (uio_num < 0) {
228                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
229                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
230                 return 1;
231         }
232         snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
233
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));
239                 goto error;
240         }
241
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));
248                 goto error;
249         }
250
251         if (dev->kdrv == RTE_KDRV_IGB_UIO)
252                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
253         else {
254                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
255
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");
259                         goto error;
260                 }
261         }
262
263         /* allocate the mapping details for secondary processes*/
264         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
265         if (*uio_res == NULL) {
266                 RTE_LOG(ERR, EAL,
267                         "%s(): cannot store uio mmap details\n", __func__);
268                 goto error;
269         }
270
271         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
272         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
273
274         return 0;
275
276 error:
277         pci_uio_free_resource(dev, *uio_res);
278         return -1;
279 }
280
281 int
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)
284 {
285         int fd = -1;
286         char devname[PATH_MAX];
287         void *mapaddr;
288         struct rte_pci_addr *loc;
289         struct pci_map *maps;
290         int wc_activate = 0;
291
292         if (dev->driver != NULL)
293                 wc_activate = dev->driver->drv_flags & RTE_PCI_DRV_WC_ACTIVATE;
294
295         loc = &dev->addr;
296         maps = uio_res->maps;
297
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",
302                                 strerror(errno));
303                 return -1;
304         }
305
306         /*
307          * open resource file, to mmap it
308          */
309         if (wc_activate) {
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);
316
317                 if (access(devname, R_OK|W_OK) != -1) {
318                         fd = open(devname, O_RDWR);
319                         if (fd < 0)
320                                 RTE_LOG(INFO, EAL, "%s cannot be mapped. "
321                                         "Fall-back to non prefetchable mode.\n",
322                                         devname);
323                 }
324         }
325
326         if (!wc_activate || fd < 0) {
327                 snprintf(devname, sizeof(devname),
328                         "%s/" PCI_PRI_FMT "/resource%d",
329                         rte_pci_get_sysfs_path(),
330                         loc->domain, loc->bus, loc->devid,
331                         loc->function, res_idx);
332
333                 /* then try to map resource file */
334                 fd = open(devname, O_RDWR);
335                 if (fd < 0) {
336                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
337                                 devname, strerror(errno));
338                         goto error;
339                 }
340         }
341
342         /* try mapping somewhere close to the end of hugepages */
343         if (pci_map_addr == NULL)
344                 pci_map_addr = pci_find_max_end_va();
345
346         mapaddr = pci_map_resource(pci_map_addr, fd, 0,
347                         (size_t)dev->mem_resource[res_idx].len, 0);
348         close(fd);
349         if (mapaddr == MAP_FAILED)
350                 goto error;
351
352         pci_map_addr = RTE_PTR_ADD(mapaddr,
353                         (size_t)dev->mem_resource[res_idx].len);
354
355         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
356         maps[map_idx].size = dev->mem_resource[res_idx].len;
357         maps[map_idx].addr = mapaddr;
358         maps[map_idx].offset = 0;
359         strcpy(maps[map_idx].path, devname);
360         dev->mem_resource[res_idx].addr = mapaddr;
361
362         return 0;
363
364 error:
365         rte_free(maps[map_idx].path);
366         return -1;
367 }
368
369 #if defined(RTE_ARCH_X86)
370 int
371 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
372                    struct rte_pci_ioport *p)
373 {
374         char dirname[PATH_MAX];
375         char filename[PATH_MAX];
376         int uio_num;
377         unsigned long start;
378
379         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
380         if (uio_num < 0)
381                 return -1;
382
383         /* get portio start */
384         snprintf(filename, sizeof(filename),
385                  "%s/portio/port%d/start", dirname, bar);
386         if (eal_parse_sysfs_value(filename, &start) < 0) {
387                 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
388                         __func__);
389                 return -1;
390         }
391         /* ensure we don't get anything funny here, read/write will cast to
392          * uin16_t */
393         if (start > UINT16_MAX)
394                 return -1;
395
396         /* FIXME only for primary process ? */
397         if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
398
399                 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
400                 dev->intr_handle.fd = open(filename, O_RDWR);
401                 if (dev->intr_handle.fd < 0) {
402                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
403                                 filename, strerror(errno));
404                         return -1;
405                 }
406                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
407         }
408
409         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
410
411         p->base = start;
412         p->len = 0;
413         return 0;
414 }
415 #else
416 int
417 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
418                    struct rte_pci_ioport *p)
419 {
420         FILE *f;
421         char buf[BUFSIZ];
422         char filename[PATH_MAX];
423         uint64_t phys_addr, end_addr, flags;
424         int fd, i;
425         void *addr;
426
427         /* open and read addresses of the corresponding resource in sysfs */
428         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
429                 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
430                 dev->addr.devid, dev->addr.function);
431         f = fopen(filename, "r");
432         if (f == NULL) {
433                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
434                         strerror(errno));
435                 return -1;
436         }
437         for (i = 0; i < bar + 1; i++) {
438                 if (fgets(buf, sizeof(buf), f) == NULL) {
439                         RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
440                         goto error;
441                 }
442         }
443         if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
444                         &end_addr, &flags) < 0)
445                 goto error;
446         if ((flags & IORESOURCE_IO) == 0) {
447                 RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
448                 goto error;
449         }
450         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
451                 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
452                 dev->addr.devid, dev->addr.function, bar);
453
454         /* mmap the pci resource */
455         fd = open(filename, O_RDWR);
456         if (fd < 0) {
457                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
458                         strerror(errno));
459                 goto error;
460         }
461         addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
462                 MAP_SHARED, fd, 0);
463         close(fd);
464         if (addr == MAP_FAILED) {
465                 RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
466                         strerror(errno));
467                 goto error;
468         }
469
470         /* strangely, the base address is mmap addr + phys_addr */
471         p->base = (uintptr_t)addr + phys_addr;
472         p->len = end_addr + 1;
473         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
474         fclose(f);
475
476         return 0;
477
478 error:
479         fclose(f);
480         return -1;
481 }
482 #endif
483
484 void
485 pci_uio_ioport_read(struct rte_pci_ioport *p,
486                     void *data, size_t len, off_t offset)
487 {
488         uint8_t *d;
489         int size;
490         uintptr_t reg = p->base + offset;
491
492         for (d = data; len > 0; d += size, reg += size, len -= size) {
493                 if (len >= 4) {
494                         size = 4;
495 #if defined(RTE_ARCH_X86)
496                         *(uint32_t *)d = inl(reg);
497 #else
498                         *(uint32_t *)d = *(volatile uint32_t *)reg;
499 #endif
500                 } else if (len >= 2) {
501                         size = 2;
502 #if defined(RTE_ARCH_X86)
503                         *(uint16_t *)d = inw(reg);
504 #else
505                         *(uint16_t *)d = *(volatile uint16_t *)reg;
506 #endif
507                 } else {
508                         size = 1;
509 #if defined(RTE_ARCH_X86)
510                         *d = inb(reg);
511 #else
512                         *d = *(volatile uint8_t *)reg;
513 #endif
514                 }
515         }
516 }
517
518 void
519 pci_uio_ioport_write(struct rte_pci_ioport *p,
520                      const void *data, size_t len, off_t offset)
521 {
522         const uint8_t *s;
523         int size;
524         uintptr_t reg = p->base + offset;
525
526         for (s = data; len > 0; s += size, reg += size, len -= size) {
527                 if (len >= 4) {
528                         size = 4;
529 #if defined(RTE_ARCH_X86)
530                         outl_p(*(const uint32_t *)s, reg);
531 #else
532                         *(volatile uint32_t *)reg = *(const uint32_t *)s;
533 #endif
534                 } else if (len >= 2) {
535                         size = 2;
536 #if defined(RTE_ARCH_X86)
537                         outw_p(*(const uint16_t *)s, reg);
538 #else
539                         *(volatile uint16_t *)reg = *(const uint16_t *)s;
540 #endif
541                 } else {
542                         size = 1;
543 #if defined(RTE_ARCH_X86)
544                         outb_p(*s, reg);
545 #else
546                         *(volatile uint8_t *)reg = *s;
547 #endif
548                 }
549         }
550 }
551
552 int
553 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
554 {
555 #if defined(RTE_ARCH_X86)
556         RTE_SET_USED(p);
557         /* FIXME close intr fd ? */
558         return 0;
559 #else
560         return munmap((void *)(uintptr_t)p->base, p->len);
561 #endif
562 }