Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2023 Marvell.
3 : : * Copyright 2026 NXP
4 : : */
5 : :
6 : : #include <uapi/linux/vfio.h>
7 : :
8 : : #include <dirent.h>
9 : : #include <inttypes.h>
10 : : #include <stdlib.h>
11 : : #include <string.h>
12 : : #include <sys/ioctl.h>
13 : : #include <sys/mman.h>
14 : : #include <sys/queue.h>
15 : : #include <unistd.h>
16 : :
17 : : #include <bus_driver.h>
18 : : #include <bus_platform_driver.h>
19 : : #include <eal_export.h>
20 : : #include <eal_filesystem.h>
21 : : #include <rte_bus.h>
22 : : #include <rte_devargs.h>
23 : : #include <rte_errno.h>
24 : : #include <rte_log.h>
25 : : #include <rte_memory.h>
26 : : #include <rte_string_fns.h>
27 : : #include <dev_vfio.h>
28 : :
29 : : #include "private.h"
30 : :
31 : : #define PLATFORM_BUS_DEVICES_PATH "/sys/bus/platform/devices"
32 : :
33 : : static struct rte_bus platform_bus;
34 : :
35 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_platform_register)
36 : : void
37 : 305 : rte_platform_register(struct rte_platform_driver *pdrv)
38 : : {
39 : 305 : rte_bus_add_driver(&platform_bus, &pdrv->driver);
40 : 305 : }
41 : :
42 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_platform_unregister)
43 : : void
44 : 0 : rte_platform_unregister(struct rte_platform_driver *pdrv)
45 : : {
46 : 0 : rte_bus_remove_driver(&platform_bus, &pdrv->driver);
47 : 0 : }
48 : :
49 : : static int
50 : 0 : dev_add(const char *dev_name)
51 : : {
52 : : struct rte_platform_device *pdev, *tmp;
53 : : char path[PATH_MAX];
54 : : unsigned long val;
55 : :
56 : 0 : pdev = calloc(1, sizeof(*pdev));
57 [ # # ]: 0 : if (pdev == NULL)
58 : : return -ENOMEM;
59 : :
60 : 0 : rte_strscpy(pdev->name, dev_name, sizeof(pdev->name));
61 : 0 : pdev->device.name = pdev->name;
62 : 0 : pdev->device.devargs = rte_bus_find_devargs(&platform_bus, dev_name);
63 : : snprintf(path, sizeof(path), PLATFORM_BUS_DEVICES_PATH "/%s/numa_node", dev_name);
64 [ # # ]: 0 : pdev->device.numa_node = eal_parse_sysfs_value(path, &val) ? rte_socket_id() : val;
65 : :
66 [ # # ]: 0 : RTE_BUS_FOREACH_DEV(tmp, &platform_bus) {
67 [ # # ]: 0 : if (!strcmp(tmp->name, pdev->name)) {
68 : 0 : PLATFORM_LOG_LINE(INFO, "device %s already added", pdev->name);
69 : :
70 [ # # ]: 0 : if (tmp->device.devargs != pdev->device.devargs)
71 : 0 : rte_devargs_remove(pdev->device.devargs);
72 : :
73 : 0 : free(pdev);
74 : 0 : return -EEXIST;
75 : : }
76 : : }
77 : :
78 : 0 : rte_bus_add_device(&platform_bus, &pdev->device);
79 : :
80 : 0 : PLATFORM_LOG_LINE(INFO, "adding device %s to the list", dev_name);
81 : :
82 : 0 : return 0;
83 : : }
84 : :
85 : : static char *
86 : 2515 : dev_kernel_driver_name(const char *dev_name)
87 : : {
88 : 2515 : char path[PATH_MAX], buf[BUFSIZ] = { };
89 : : char *kdrv;
90 : : int ret;
91 : :
92 : : snprintf(path, sizeof(path), PLATFORM_BUS_DEVICES_PATH "/%s/driver", dev_name);
93 : : /* save space for NUL */
94 : 2515 : ret = readlink(path, buf, sizeof(buf) - 1);
95 [ + + ]: 2515 : if (ret <= 0)
96 : : return NULL;
97 : :
98 : : /* last token is kernel driver name */
99 : 1356 : kdrv = strrchr(buf, '/');
100 [ + - ]: 1356 : if (kdrv != NULL)
101 : 1356 : return strdup(kdrv + 1);
102 : :
103 : : return NULL;
104 : : }
105 : :
106 : : static bool
107 : 2486 : dev_is_bound_vfio_platform(const char *dev_name)
108 : : {
109 : : char *kdrv;
110 : : int ret;
111 : :
112 : 2486 : kdrv = dev_kernel_driver_name(dev_name);
113 [ + + ]: 2486 : if (!kdrv)
114 : : return false;
115 : :
116 : 1356 : ret = strcmp(kdrv, "vfio-platform");
117 : 1356 : free(kdrv);
118 : :
119 : 1356 : return ret == 0;
120 : : }
121 : :
122 : : static int
123 : 229 : platform_bus_scan(void)
124 : : {
125 : : const struct dirent *ent;
126 : : const char *dev_name;
127 : : int ret = 0;
128 : : DIR *dp;
129 : :
130 : 229 : dp = opendir(PLATFORM_BUS_DEVICES_PATH);
131 [ - + ]: 229 : if (dp == NULL) {
132 : 0 : PLATFORM_LOG_LINE(INFO, "failed to open %s", PLATFORM_BUS_DEVICES_PATH);
133 : 0 : return -errno;
134 : : }
135 : :
136 [ + + ]: 3206 : while ((ent = readdir(dp))) {
137 : 2977 : dev_name = ent->d_name;
138 [ + + ]: 2977 : if (dev_name[0] == '.')
139 : 458 : continue;
140 : :
141 [ + + ]: 2519 : if (rte_bus_device_is_ignored(&platform_bus, dev_name))
142 : 33 : continue;
143 : :
144 [ + - ]: 2486 : if (!dev_is_bound_vfio_platform(dev_name))
145 : 2486 : continue;
146 : :
147 : 0 : ret = dev_add(dev_name);
148 [ # # ]: 0 : if (ret)
149 : : break;
150 : : }
151 : :
152 : 229 : closedir(dp);
153 : :
154 : 229 : return ret;
155 : : }
156 : :
157 : : static int
158 : 0 : device_map_resource_offset(struct rte_platform_device *pdev, struct rte_platform_resource *res,
159 : : size_t offset)
160 : : {
161 : 0 : res->mem.addr = mmap(NULL, res->mem.len, PROT_READ | PROT_WRITE, MAP_SHARED, pdev->dev_fd,
162 : : offset);
163 [ # # ]: 0 : if (res->mem.addr == MAP_FAILED)
164 : 0 : return -errno;
165 : :
166 : 0 : PLATFORM_LOG_LINE(DEBUG, "adding resource va = %p len = %"PRIu64" name = %s", res->mem.addr,
167 : : res->mem.len, res->name);
168 : :
169 : 0 : return 0;
170 : : }
171 : :
172 : : static void
173 : 0 : device_unmap_resources(struct rte_platform_device *pdev)
174 : : {
175 : : struct rte_platform_resource *res;
176 : : unsigned int i;
177 : :
178 [ # # ]: 0 : for (i = 0; i < pdev->num_resource; i++) {
179 : 0 : res = &pdev->resource[i];
180 : 0 : munmap(res->mem.addr, res->mem.len);
181 : 0 : free(res->name);
182 : : }
183 : :
184 : 0 : free(pdev->resource);
185 : 0 : pdev->resource = NULL;
186 : 0 : pdev->num_resource = 0;
187 : 0 : }
188 : :
189 : : static int
190 : 0 : read_sysfs_string(const char *path, char *buf, size_t size)
191 : : {
192 : : FILE *f;
193 : : char *p;
194 : :
195 : 0 : f = fopen(path, "r");
196 [ # # ]: 0 : if (f == NULL)
197 : 0 : return -errno;
198 : :
199 [ # # # # ]: 0 : if (fgets(buf, size, f) == NULL) {
200 : 0 : fclose(f);
201 : 0 : return -ENODATA;
202 : : }
203 : :
204 : 0 : fclose(f);
205 : :
206 : 0 : p = strrchr(buf, '\n');
207 [ # # ]: 0 : if (p != NULL)
208 : 0 : *p = '\0';
209 : :
210 : : return 0;
211 : : }
212 : :
213 : : static char *
214 : 0 : of_resource_name(const char *dev_name, int index)
215 : : {
216 : 0 : char path[PATH_MAX], buf[BUFSIZ] = { };
217 : : int num = 0, ret;
218 : : char *name;
219 : :
220 : : snprintf(path, sizeof(path), PLATFORM_BUS_DEVICES_PATH "/%s/of_node/reg-names", dev_name);
221 : 0 : ret = read_sysfs_string(path, buf, sizeof(buf) - 1);
222 [ # # ]: 0 : if (ret)
223 : : return NULL;
224 : :
225 [ # # ]: 0 : for (name = buf; *name != 0; name += strlen(name) + 1) {
226 [ # # ]: 0 : if (num++ != index)
227 : : continue;
228 : 0 : return strdup(name);
229 : : }
230 : :
231 : : return NULL;
232 : : }
233 : :
234 : : /*
235 : : * Check whether any of the NUL-separated device-tree "compatible" strings
236 : : * exposed by the platform device matches the given string.
237 : : */
238 : : static bool
239 : 0 : of_device_is_compatible(const char *dev_name, const char *compat)
240 : : {
241 : 0 : char path[PATH_MAX], buf[BUFSIZ] = { };
242 : : const char *s;
243 : : FILE *f;
244 : : size_t len;
245 : :
246 [ # # ]: 0 : if (compat == NULL)
247 : : return false;
248 : :
249 : : snprintf(path, sizeof(path), PLATFORM_BUS_DEVICES_PATH "/%s/of_node/compatible", dev_name);
250 : 0 : f = fopen(path, "r");
251 [ # # ]: 0 : if (f == NULL)
252 : : return false;
253 : :
254 : : /* Read the raw contents, preserving embedded NULL separators. */
255 : : len = fread(buf, 1, sizeof(buf) - 1, f);
256 : 0 : fclose(f);
257 [ # # ]: 0 : if (len == 0)
258 : : return false;
259 : :
260 : : /* Bound the walk by the read length: some kernels omit the trailing NULL. */
261 [ # # ]: 0 : for (s = buf; s < buf + len; s += strlen(s) + 1) {
262 [ # # ]: 0 : if (!strcmp(s, compat))
263 : : return true;
264 : : }
265 : :
266 : : return false;
267 : : }
268 : :
269 : : static int
270 : 0 : device_map_resources(struct rte_platform_device *pdev, unsigned int num)
271 : : {
272 : : struct rte_platform_resource *res;
273 : : unsigned int i;
274 : : int ret;
275 : :
276 [ # # ]: 0 : if (num == 0) {
277 : 0 : PLATFORM_LOG_LINE(WARNING, "device %s has no resources", pdev->name);
278 : 0 : return 0;
279 : : }
280 : :
281 : 0 : pdev->resource = calloc(num, sizeof(*pdev->resource));
282 [ # # ]: 0 : if (pdev->resource == NULL)
283 : : return -ENOMEM;
284 : :
285 [ # # ]: 0 : for (i = 0; i < num; i++) {
286 : 0 : struct vfio_region_info reg_info = {
287 : : .argsz = sizeof(reg_info),
288 : : .index = i,
289 : : };
290 : :
291 : 0 : ret = ioctl(pdev->dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
292 [ # # ]: 0 : if (ret) {
293 : 0 : PLATFORM_LOG_LINE(ERR, "failed to get region info at %d", i);
294 : 0 : ret = -errno;
295 : 0 : goto out;
296 : : }
297 : :
298 : 0 : res = &pdev->resource[i];
299 : 0 : res->name = of_resource_name(pdev->name, reg_info.index);
300 : 0 : res->mem.len = reg_info.size;
301 : 0 : ret = device_map_resource_offset(pdev, res, reg_info.offset);
302 [ # # ]: 0 : if (ret) {
303 : 0 : PLATFORM_LOG_LINE(ERR, "failed to ioremap resource at %d", i);
304 : 0 : goto out;
305 : : }
306 : :
307 : 0 : pdev->num_resource++;
308 : : }
309 : :
310 : : return 0;
311 : : out:
312 : 0 : device_unmap_resources(pdev);
313 : :
314 : 0 : return ret;
315 : : }
316 : :
317 : : static void
318 : 0 : device_cleanup(struct rte_platform_device *pdev)
319 : : {
320 : 0 : device_unmap_resources(pdev);
321 : 0 : dev_vfio_release_device(PLATFORM_BUS_DEVICES_PATH, pdev->name, pdev->dev_fd);
322 : 0 : }
323 : :
324 : : static int
325 : 0 : device_setup(struct rte_platform_device *pdev)
326 : : {
327 : 0 : struct vfio_device_info dev_info = { .argsz = sizeof(dev_info), };
328 : 0 : const char *name = pdev->name;
329 : : int ret;
330 : :
331 : 0 : ret = dev_vfio_setup_device(PLATFORM_BUS_DEVICES_PATH, name, &pdev->dev_fd);
332 [ # # ]: 0 : if (ret) {
333 : 0 : PLATFORM_LOG_LINE(ERR, "failed to setup %s", name);
334 : 0 : return -ENODEV;
335 : : }
336 : :
337 : 0 : ret = dev_vfio_get_device_info(pdev->dev_fd, &dev_info);
338 [ # # ]: 0 : if (ret) {
339 : 0 : PLATFORM_LOG_LINE(ERR, "failed to get device info for %s", name);
340 : : ret = -ENODEV;
341 : 0 : goto out;
342 : : }
343 : :
344 : : /* This is an extra check to confirm that platform device was initialized
345 : : * by a kernel vfio-platform driver. On kernels that predate vfio-platform
346 : : * driver this flag obviously does not exist. In such scenarios this
347 : : * check needs to be removed otherwise compilation fails.
348 : : *
349 : : * Now, on such old kernels code will never reach here because
350 : : * there is another check much earlier which verifies whether
351 : : * device has been bound to vfio-platform driver.
352 : : */
353 : : #ifdef VFIO_DEVICE_FLAGS_PLATFORM
354 [ # # ]: 0 : if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
355 : 0 : PLATFORM_LOG_LINE(ERR, "device not backed by vfio-platform");
356 : : ret = -ENOTSUP;
357 : 0 : goto out;
358 : : }
359 : : #endif
360 : :
361 : 0 : ret = device_map_resources(pdev, dev_info.num_regions);
362 [ # # ]: 0 : if (ret) {
363 : 0 : PLATFORM_LOG_LINE(ERR, "failed to setup platform resources");
364 : 0 : goto out;
365 : : }
366 : :
367 : : return 0;
368 : 0 : out:
369 : 0 : device_cleanup(pdev);
370 : :
371 : 0 : return ret;
372 : : }
373 : :
374 : : static int
375 : : driver_call_probe(struct rte_platform_driver *pdrv, struct rte_platform_device *pdev)
376 : : {
377 [ # # ]: 0 : if (pdrv->probe != NULL)
378 : 0 : return pdrv->probe(pdev);
379 : :
380 : : return 0;
381 : : }
382 : :
383 : : static int
384 : 0 : driver_probe_device(struct rte_platform_driver *pdrv, struct rte_platform_device *pdev)
385 : : {
386 : : enum rte_iova_mode iova_mode;
387 : : int ret;
388 : :
389 : 0 : iova_mode = rte_eal_iova_mode();
390 [ # # # # ]: 0 : if (pdrv->drv_flags & RTE_PLATFORM_DRV_NEED_IOVA_AS_VA && iova_mode != RTE_IOVA_VA) {
391 : 0 : PLATFORM_LOG_LINE(ERR, "driver %s expects VA IOVA mode but current mode is PA",
392 : : pdrv->driver.name);
393 : 0 : return -EINVAL;
394 : : }
395 : :
396 : 0 : ret = device_setup(pdev);
397 [ # # ]: 0 : if (ret)
398 : : return ret;
399 : :
400 : : ret = driver_call_probe(pdrv, pdev);
401 [ # # ]: 0 : if (ret)
402 : 0 : device_cleanup(pdev);
403 : :
404 : : return ret;
405 : : }
406 : :
407 : : static bool
408 : 29 : platform_bus_match(const struct rte_driver *drv, const struct rte_device *dev)
409 : : {
410 : : const struct rte_platform_driver *pdrv = RTE_BUS_DRIVER(drv, *pdrv);
411 : : const struct rte_platform_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
412 : : bool match = false;
413 : : char *kdrv;
414 : :
415 : 29 : kdrv = dev_kernel_driver_name(pdev->name);
416 [ - + ]: 29 : if (!kdrv)
417 : : return false;
418 : :
419 : : /* match by driver name */
420 [ # # ]: 0 : if (!strcmp(kdrv, pdrv->driver.name)) {
421 : : match = true;
422 : 0 : goto out;
423 : : }
424 : :
425 : : /* match by driver alias */
426 [ # # # # ]: 0 : if (pdrv->driver.alias != NULL && !strcmp(kdrv, pdrv->driver.alias)) {
427 : : match = true;
428 : 0 : goto out;
429 : : }
430 : :
431 : : /* match by device name */
432 [ # # ]: 0 : if (!strcmp(pdev->name, pdrv->driver.name)) {
433 : : match = true;
434 : 0 : goto out;
435 : : }
436 : :
437 : : /*
438 : : * The generic vfio-platform kernel driver name carries no device
439 : : * identity, so fall back to matching the device-tree "compatible"
440 : : * strings against the DPDK driver name or alias.
441 : : */
442 [ # # # # ]: 0 : if (of_device_is_compatible(pdev->name, pdrv->driver.name) ||
443 : 0 : of_device_is_compatible(pdev->name, pdrv->driver.alias))
444 : : match = true;
445 : :
446 : 0 : out:
447 : 0 : free(kdrv);
448 : :
449 : 0 : return match;
450 : : }
451 : :
452 : : static int
453 : 0 : platform_bus_probe_device(struct rte_driver *drv, struct rte_device *dev)
454 : : {
455 [ # # ]: 0 : if (!dev_is_bound_vfio_platform(dev->name))
456 : : return -EPERM;
457 : :
458 : 0 : return driver_probe_device(RTE_BUS_DRIVER(drv, struct rte_platform_driver),
459 : : RTE_BUS_DEVICE(dev, struct rte_platform_device));
460 : : }
461 : :
462 : : static void
463 : 0 : device_release_driver(struct rte_platform_device *pdev)
464 : : {
465 : 0 : const struct rte_platform_driver *pdrv = RTE_BUS_DRIVER(pdev->device.driver, *pdrv);
466 : : int ret;
467 : :
468 [ # # ]: 0 : if (pdrv->remove != NULL) {
469 : 0 : ret = pdrv->remove(pdev);
470 [ # # ]: 0 : if (ret)
471 : 0 : PLATFORM_LOG_LINE(WARNING, "failed to remove %s", pdev->name);
472 : : }
473 : 0 : }
474 : :
475 : : static int
476 : 0 : platform_bus_unplug_device(struct rte_device *dev)
477 : : {
478 : : struct rte_platform_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
479 : :
480 : 0 : device_release_driver(pdev);
481 : 0 : device_cleanup(pdev);
482 : :
483 : 0 : return 0;
484 : : }
485 : :
486 : : static int
487 : 29 : platform_bus_parse(const char *name, void *addr)
488 : : {
489 : 29 : struct rte_platform_device pdev = { };
490 : : struct rte_platform_driver *pdrv;
491 : : const char **out = addr;
492 : :
493 : 29 : rte_strscpy(pdev.name, name, sizeof(pdev.name));
494 : :
495 [ + + ]: 58 : RTE_BUS_FOREACH_DRV(pdrv, &platform_bus) {
496 [ + - ]: 29 : if (platform_bus_match(&pdrv->driver, &pdev.device))
497 : : break;
498 : : }
499 : :
500 [ - + ]: 29 : if (pdrv != NULL && addr != NULL)
501 : 0 : *out = name;
502 : :
503 [ + - ]: 29 : return pdrv != NULL ? 0 : -ENODEV;
504 : : }
505 : :
506 : : static int
507 : 0 : platform_bus_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
508 : : {
509 : : struct rte_platform_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
510 : 0 : const struct rte_platform_driver *pdrv = RTE_BUS_DRIVER(dev->driver, *pdrv);
511 : :
512 [ # # ]: 0 : if (pdrv->dma_map != NULL)
513 : 0 : return pdrv->dma_map(pdev, addr, iova, len);
514 : :
515 : 0 : return dev_vfio_container_dma_map(DEV_VFIO_DEFAULT_CONTAINER_FD, (uint64_t)addr, iova, len);
516 : : }
517 : :
518 : : static int
519 : 0 : platform_bus_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
520 : : {
521 : : struct rte_platform_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
522 : 0 : const struct rte_platform_driver *pdrv = RTE_BUS_DRIVER(dev->driver, *pdrv);
523 : :
524 [ # # ]: 0 : if (pdrv->dma_unmap != NULL)
525 : 0 : return pdrv->dma_unmap(pdev, addr, iova, len);
526 : :
527 : 0 : return dev_vfio_container_dma_unmap(DEV_VFIO_DEFAULT_CONTAINER_FD, (uint64_t)addr, iova,
528 : : len);
529 : : }
530 : :
531 : : static enum rte_iova_mode
532 : 229 : platform_bus_get_iommu_class(void)
533 : : {
534 : : const struct rte_platform_driver *pdrv;
535 : : struct rte_platform_device *pdev;
536 : :
537 [ - + ]: 229 : RTE_BUS_FOREACH_DEV(pdev, &platform_bus) {
538 [ # # ]: 0 : if (!rte_dev_is_probed(&pdev->device))
539 : 0 : continue;
540 : 0 : pdrv = RTE_BUS_DRIVER(pdev->device.driver, *pdrv);
541 [ # # ]: 0 : if (pdrv->drv_flags & RTE_PLATFORM_DRV_NEED_IOVA_AS_VA)
542 : : return RTE_IOVA_VA;
543 : : }
544 : :
545 : : return RTE_IOVA_DC;
546 : : }
547 : :
548 : : static void
549 : 0 : platform_free_device(struct rte_device *dev)
550 : : {
551 : 0 : free(RTE_BUS_DEVICE(dev, struct rte_platform_device));
552 : 0 : }
553 : :
554 : : static struct rte_bus platform_bus = {
555 : : .scan = platform_bus_scan,
556 : : .probe = rte_bus_generic_probe,
557 : : .free_device = platform_free_device,
558 : : .cleanup = rte_bus_generic_cleanup,
559 : : .find_device = rte_bus_generic_find_device,
560 : : .match = platform_bus_match,
561 : : .probe_device = platform_bus_probe_device,
562 : : .unplug_device = platform_bus_unplug_device,
563 : : .parse = platform_bus_parse,
564 : : .dma_map = platform_bus_dma_map,
565 : : .dma_unmap = platform_bus_dma_unmap,
566 : : .get_iommu_class = platform_bus_get_iommu_class,
567 : : .dev_iterate = rte_bus_generic_dev_iterate,
568 : : };
569 : :
570 : 305 : RTE_REGISTER_BUS(platform, platform_bus);
571 [ - + ]: 305 : RTE_LOG_REGISTER_DEFAULT(platform_bus_logtype, NOTICE);
|