1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_memory.h>
12 #include <rte_eal_memconfig.h>
15 #include "eal_filesystem.h"
17 #include "eal_private.h"
21 /* per-process VFIO config */
22 static struct vfio_config vfio_cfg;
24 static int vfio_type1_dma_map(int);
25 static int vfio_spapr_dma_map(int);
26 static int vfio_noiommu_dma_map(int);
28 /* IOMMU types we support */
29 static const struct vfio_iommu_type iommu_types[] = {
30 /* x86 IOMMU, otherwise known as type 1 */
31 { RTE_VFIO_TYPE1, "Type 1", &vfio_type1_dma_map},
32 /* ppc64 IOMMU, otherwise known as spapr */
33 { RTE_VFIO_SPAPR, "sPAPR", &vfio_spapr_dma_map},
35 { RTE_VFIO_NOIOMMU, "No-IOMMU", &vfio_noiommu_dma_map},
39 vfio_get_group_fd(int iommu_group_no)
43 char filename[PATH_MAX];
44 struct vfio_group *cur_grp;
46 /* check if we already have the group descriptor open */
47 for (i = 0; i < VFIO_MAX_GROUPS; i++)
48 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
49 return vfio_cfg.vfio_groups[i].fd;
51 /* Lets see first if there is room for a new group */
52 if (vfio_cfg.vfio_active_groups == VFIO_MAX_GROUPS) {
53 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
57 /* Now lets get an index for the new group */
58 for (i = 0; i < VFIO_MAX_GROUPS; i++)
59 if (vfio_cfg.vfio_groups[i].group_no == -1) {
60 cur_grp = &vfio_cfg.vfio_groups[i];
64 /* This should not happen */
65 if (i == VFIO_MAX_GROUPS) {
66 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
69 /* if primary, try to open the group */
70 if (internal_config.process_type == RTE_PROC_PRIMARY) {
71 /* try regular group format */
72 snprintf(filename, sizeof(filename),
73 VFIO_GROUP_FMT, iommu_group_no);
74 vfio_group_fd = open(filename, O_RDWR);
75 if (vfio_group_fd < 0) {
76 /* if file not found, it's not an error */
77 if (errno != ENOENT) {
78 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
83 /* special case: try no-IOMMU path as well */
84 snprintf(filename, sizeof(filename),
85 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
86 vfio_group_fd = open(filename, O_RDWR);
87 if (vfio_group_fd < 0) {
88 if (errno != ENOENT) {
89 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
95 /* noiommu group found */
98 cur_grp->group_no = iommu_group_no;
99 cur_grp->fd = vfio_group_fd;
100 vfio_cfg.vfio_active_groups++;
101 return vfio_group_fd;
103 /* if we're in a secondary process, request group fd from the primary
104 * process via our socket
109 socket_fd = vfio_mp_sync_connect_to_primary();
112 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
115 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
116 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
120 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
121 RTE_LOG(ERR, EAL, " cannot send group number!\n");
125 ret = vfio_mp_sync_receive_request(socket_fd);
131 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
132 /* if we got the fd, store it and return it */
133 if (vfio_group_fd > 0) {
135 cur_grp->group_no = iommu_group_no;
136 cur_grp->fd = vfio_group_fd;
137 vfio_cfg.vfio_active_groups++;
138 return vfio_group_fd;
140 /* fall-through on error */
142 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
152 get_vfio_group_idx(int vfio_group_fd)
155 for (i = 0; i < VFIO_MAX_GROUPS; i++)
156 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
162 vfio_group_device_get(int vfio_group_fd)
166 i = get_vfio_group_idx(vfio_group_fd);
167 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
168 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
170 vfio_cfg.vfio_groups[i].devices++;
174 vfio_group_device_put(int vfio_group_fd)
178 i = get_vfio_group_idx(vfio_group_fd);
179 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
180 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
182 vfio_cfg.vfio_groups[i].devices--;
186 vfio_group_device_count(int vfio_group_fd)
190 i = get_vfio_group_idx(vfio_group_fd);
191 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
192 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
196 return vfio_cfg.vfio_groups[i].devices;
200 rte_vfio_clear_group(int vfio_group_fd)
205 if (internal_config.process_type == RTE_PROC_PRIMARY) {
207 i = get_vfio_group_idx(vfio_group_fd);
210 vfio_cfg.vfio_groups[i].group_no = -1;
211 vfio_cfg.vfio_groups[i].fd = -1;
212 vfio_cfg.vfio_groups[i].devices = 0;
213 vfio_cfg.vfio_active_groups--;
217 /* This is just for SECONDARY processes */
218 socket_fd = vfio_mp_sync_connect_to_primary();
221 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
225 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
226 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
231 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
232 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
237 ret = vfio_mp_sync_receive_request(socket_fd);
240 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
247 RTE_LOG(ERR, EAL, " Socket error\n");
251 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
258 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
259 int *vfio_dev_fd, struct vfio_device_info *device_info)
261 struct vfio_group_status group_status = {
262 .argsz = sizeof(group_status)
268 /* get group number */
269 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
271 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
276 /* if negative, something failed */
280 /* get the actual group fd */
281 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
282 if (vfio_group_fd < 0)
285 /* if group_fd == 0, that means the device isn't managed by VFIO */
286 if (vfio_group_fd == 0) {
287 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
293 * at this point, we know that this group is viable (meaning, all devices
294 * are either bound to VFIO or not bound to anything)
297 /* check if the group is viable */
298 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
300 RTE_LOG(ERR, EAL, " %s cannot get group status, "
301 "error %i (%s)\n", dev_addr, errno, strerror(errno));
302 close(vfio_group_fd);
303 rte_vfio_clear_group(vfio_group_fd);
305 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
306 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
307 close(vfio_group_fd);
308 rte_vfio_clear_group(vfio_group_fd);
312 /* check if group does not have a container yet */
313 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
315 /* add group to a container */
316 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
317 &vfio_cfg.vfio_container_fd);
319 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
320 "error %i (%s)\n", dev_addr, errno, strerror(errno));
321 close(vfio_group_fd);
322 rte_vfio_clear_group(vfio_group_fd);
327 * pick an IOMMU type and set up DMA mappings for container
329 * needs to be done only once, only when first group is
330 * assigned to a container and only in primary process.
331 * Note this can happen several times with the hotplug
334 if (internal_config.process_type == RTE_PROC_PRIMARY &&
335 vfio_cfg.vfio_active_groups == 1) {
336 /* select an IOMMU type which we will be using */
337 const struct vfio_iommu_type *t =
338 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
341 " %s failed to select IOMMU type\n",
343 close(vfio_group_fd);
344 rte_vfio_clear_group(vfio_group_fd);
347 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
350 " %s DMA remapping failed, error %i (%s)\n",
351 dev_addr, errno, strerror(errno));
352 close(vfio_group_fd);
353 rte_vfio_clear_group(vfio_group_fd);
359 /* get a file descriptor for the device */
360 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
361 if (*vfio_dev_fd < 0) {
362 /* if we cannot get a device fd, this implies a problem with
363 * the VFIO group or the container not having IOMMU configured.
366 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
368 close(vfio_group_fd);
369 rte_vfio_clear_group(vfio_group_fd);
373 /* test and setup the device */
374 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
376 RTE_LOG(ERR, EAL, " %s cannot get device info, "
377 "error %i (%s)\n", dev_addr, errno,
380 close(vfio_group_fd);
381 rte_vfio_clear_group(vfio_group_fd);
384 vfio_group_device_get(vfio_group_fd);
390 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
393 struct vfio_group_status group_status = {
394 .argsz = sizeof(group_status)
400 /* get group number */
401 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
403 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
405 /* This is an error at this point. */
409 /* get the actual group fd */
410 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
411 if (vfio_group_fd <= 0) {
412 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
417 /* At this point we got an active group. Closing it will make the
418 * container detachment. If this is the last active group, VFIO kernel
419 * code will unset the container and the IOMMU mappings.
422 /* Closing a device */
423 if (close(vfio_dev_fd) < 0) {
424 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
429 /* An VFIO group can have several devices attached. Just when there is
430 * no devices remaining should the group be closed.
432 vfio_group_device_put(vfio_group_fd);
433 if (!vfio_group_device_count(vfio_group_fd)) {
435 if (close(vfio_group_fd) < 0) {
436 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
441 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
442 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
452 rte_vfio_enable(const char *modname)
454 /* initialize group list */
458 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
459 vfio_cfg.vfio_groups[i].fd = -1;
460 vfio_cfg.vfio_groups[i].group_no = -1;
461 vfio_cfg.vfio_groups[i].devices = 0;
464 /* inform the user that we are probing for VFIO */
465 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
467 /* check if vfio module is loaded */
468 vfio_available = rte_eal_check_module(modname);
470 /* return error directly */
471 if (vfio_available == -1) {
472 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
476 /* return 0 if VFIO modules not loaded */
477 if (vfio_available == 0) {
478 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
479 "skipping VFIO support...\n");
483 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
485 /* check if we have VFIO driver enabled */
486 if (vfio_cfg.vfio_container_fd != -1) {
487 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
488 vfio_cfg.vfio_enabled = 1;
490 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
497 rte_vfio_is_enabled(const char *modname)
499 const int mod_available = rte_eal_check_module(modname) > 0;
500 return vfio_cfg.vfio_enabled && mod_available;
503 const struct vfio_iommu_type *
504 vfio_set_iommu_type(int vfio_container_fd)
507 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
508 const struct vfio_iommu_type *t = &iommu_types[idx];
510 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
513 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
514 t->type_id, t->name);
517 /* not an error, there may be more supported IOMMU types */
518 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
519 "error %i (%s)\n", t->type_id, t->name, errno,
522 /* if we didn't find a suitable IOMMU type, fail */
527 vfio_has_supported_extensions(int vfio_container_fd)
530 unsigned idx, n_extensions = 0;
531 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
532 const struct vfio_iommu_type *t = &iommu_types[idx];
534 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
537 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
538 "error %i (%s)\n", errno,
540 close(vfio_container_fd);
542 } else if (ret == 1) {
543 /* we found a supported extension */
546 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
548 ret ? "supported" : "not supported");
551 /* if we didn't find any supported IOMMU types, fail */
553 close(vfio_container_fd);
561 vfio_get_container_fd(void)
563 int ret, vfio_container_fd;
565 /* if we're in a primary process, try to open the container */
566 if (internal_config.process_type == RTE_PROC_PRIMARY) {
567 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
568 if (vfio_container_fd < 0) {
569 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
570 "error %i (%s)\n", errno, strerror(errno));
574 /* check VFIO API version */
575 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
576 if (ret != VFIO_API_VERSION) {
578 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
579 "error %i (%s)\n", errno, strerror(errno));
581 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
582 close(vfio_container_fd);
586 ret = vfio_has_supported_extensions(vfio_container_fd);
588 RTE_LOG(ERR, EAL, " no supported IOMMU "
589 "extensions found!\n");
593 return vfio_container_fd;
596 * if we're in a secondary process, request container fd from the
597 * primary process via our socket
601 socket_fd = vfio_mp_sync_connect_to_primary();
603 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
606 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
607 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
611 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
612 if (vfio_container_fd < 0) {
613 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
618 return vfio_container_fd;
625 vfio_get_group_no(const char *sysfs_base,
626 const char *dev_addr, int *iommu_group_no)
628 char linkname[PATH_MAX];
629 char filename[PATH_MAX];
630 char *tok[16], *group_tok, *end;
633 memset(linkname, 0, sizeof(linkname));
634 memset(filename, 0, sizeof(filename));
636 /* try to find out IOMMU group for this device */
637 snprintf(linkname, sizeof(linkname),
638 "%s/%s/iommu_group", sysfs_base, dev_addr);
640 ret = readlink(linkname, filename, sizeof(filename));
642 /* if the link doesn't exist, no VFIO for us */
646 ret = rte_strsplit(filename, sizeof(filename),
647 tok, RTE_DIM(tok), '/');
650 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
654 /* IOMMU group is always the last token */
656 group_tok = tok[ret - 1];
658 *iommu_group_no = strtol(group_tok, &end, 10);
659 if ((end != group_tok && *end != '\0') || errno != 0) {
660 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
668 vfio_type1_dma_map(int vfio_container_fd)
670 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
673 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
674 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
675 struct vfio_iommu_type1_dma_map dma_map;
677 if (ms[i].addr == NULL)
680 memset(&dma_map, 0, sizeof(dma_map));
681 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
682 dma_map.vaddr = ms[i].addr_64;
683 dma_map.size = ms[i].len;
684 if (rte_eal_iova_mode() == RTE_IOVA_VA)
685 dma_map.iova = dma_map.vaddr;
687 dma_map.iova = ms[i].iova;
688 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
690 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
693 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
694 "error %i (%s)\n", errno,
704 vfio_spapr_dma_map(int vfio_container_fd)
706 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
709 struct vfio_iommu_spapr_register_memory reg = {
710 .argsz = sizeof(reg),
713 struct vfio_iommu_spapr_tce_info info = {
714 .argsz = sizeof(info),
716 struct vfio_iommu_spapr_tce_create create = {
717 .argsz = sizeof(create),
719 struct vfio_iommu_spapr_tce_remove remove = {
720 .argsz = sizeof(remove),
723 /* query spapr iommu info */
724 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
726 RTE_LOG(ERR, EAL, " cannot get iommu info, "
727 "error %i (%s)\n", errno, strerror(errno));
731 /* remove default DMA of 32 bit window */
732 remove.start_addr = info.dma32_window_start;
733 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
735 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
736 "error %i (%s)\n", errno, strerror(errno));
740 /* create DMA window from 0 to max(phys_addr + len) */
741 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
742 if (ms[i].addr == NULL)
745 create.window_size = RTE_MAX(create.window_size,
746 ms[i].iova + ms[i].len);
749 /* sPAPR requires window size to be a power of 2 */
750 create.window_size = rte_align64pow2(create.window_size);
751 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
754 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
756 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
757 "error %i (%s)\n", errno, strerror(errno));
761 if (create.start_addr != 0) {
762 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
766 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
767 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
768 struct vfio_iommu_type1_dma_map dma_map;
770 if (ms[i].addr == NULL)
773 reg.vaddr = (uintptr_t) ms[i].addr;
774 reg.size = ms[i].len;
775 ret = ioctl(vfio_container_fd,
776 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
778 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
779 "error %i (%s)\n", errno, strerror(errno));
783 memset(&dma_map, 0, sizeof(dma_map));
784 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
785 dma_map.vaddr = ms[i].addr_64;
786 dma_map.size = ms[i].len;
787 if (rte_eal_iova_mode() == RTE_IOVA_VA)
788 dma_map.iova = dma_map.vaddr;
790 dma_map.iova = ms[i].iova;
791 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
792 VFIO_DMA_MAP_FLAG_WRITE;
794 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
797 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
798 "error %i (%s)\n", errno, strerror(errno));
808 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
810 /* No-IOMMU mode does not need DMA mapping */
815 rte_vfio_noiommu_is_enabled(void)
821 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
823 if (errno != ENOENT) {
824 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
825 errno, strerror(errno));
829 * else the file does not exists
830 * i.e. noiommu is not enabled
835 cnt = read(fd, &c, 1);
838 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
839 "file %i (%s)\n", errno, strerror(errno));