1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
11 #include <rte_errno.h>
13 #include <rte_memory.h>
14 #include <rte_eal_memconfig.h>
17 #include "eal_filesystem.h"
19 #include "eal_private.h"
23 #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
25 /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
26 * recreate the mappings for DPDK segments, but we cannot do so for memory that
27 * was registered by the user themselves, so we need to store the user mappings
28 * somewhere, to recreate them later.
30 #define VFIO_MAX_USER_MEM_MAPS 256
37 struct user_mem_maps {
38 rte_spinlock_recursive_t lock;
40 struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
45 int vfio_container_fd;
46 int vfio_active_groups;
47 const struct vfio_iommu_type *vfio_iommu_type;
48 struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
49 struct user_mem_maps mem_maps;
52 /* per-process VFIO config */
53 static struct vfio_config vfio_cfgs[VFIO_MAX_CONTAINERS];
54 static struct vfio_config *default_vfio_cfg = &vfio_cfgs[0];
56 static int vfio_type1_dma_map(int);
57 static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
58 static int vfio_spapr_dma_map(int);
59 static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
60 static int vfio_noiommu_dma_map(int);
61 static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
62 static int vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr,
63 uint64_t iova, uint64_t len, int do_map);
65 /* IOMMU types we support */
66 static const struct vfio_iommu_type iommu_types[] = {
67 /* x86 IOMMU, otherwise known as type 1 */
69 .type_id = RTE_VFIO_TYPE1,
71 .dma_map_func = &vfio_type1_dma_map,
72 .dma_user_map_func = &vfio_type1_dma_mem_map
74 /* ppc64 IOMMU, otherwise known as spapr */
76 .type_id = RTE_VFIO_SPAPR,
78 .dma_map_func = &vfio_spapr_dma_map,
79 .dma_user_map_func = &vfio_spapr_dma_mem_map
83 .type_id = RTE_VFIO_NOIOMMU,
85 .dma_map_func = &vfio_noiommu_dma_map,
86 .dma_user_map_func = &vfio_noiommu_dma_mem_map
91 is_null_map(const struct user_mem_map *map)
93 return map->addr == 0 && map->iova == 0 && map->len == 0;
96 /* we may need to merge user mem maps together in case of user mapping/unmapping
97 * chunks of memory, so we'll need a comparator function to sort segments.
100 user_mem_map_cmp(const void *a, const void *b)
102 const struct user_mem_map *umm_a = a;
103 const struct user_mem_map *umm_b = b;
105 /* move null entries to end */
106 if (is_null_map(umm_a))
108 if (is_null_map(umm_b))
111 /* sort by iova first */
112 if (umm_a->iova < umm_b->iova)
114 if (umm_a->iova > umm_b->iova)
117 if (umm_a->addr < umm_b->addr)
119 if (umm_a->addr > umm_b->addr)
122 if (umm_a->len < umm_b->len)
124 if (umm_a->len > umm_b->len)
130 /* adjust user map entry. this may result in shortening of existing map, or in
131 * splitting existing map in two pieces.
134 adjust_map(struct user_mem_map *src, struct user_mem_map *end,
135 uint64_t remove_va_start, uint64_t remove_len)
137 /* if va start is same as start address, we're simply moving start */
138 if (remove_va_start == src->addr) {
139 src->addr += remove_len;
140 src->iova += remove_len;
141 src->len -= remove_len;
142 } else if (remove_va_start + remove_len == src->addr + src->len) {
143 /* we're shrinking mapping from the end */
144 src->len -= remove_len;
146 /* we're blowing a hole in the middle */
147 struct user_mem_map tmp;
148 uint64_t total_len = src->len;
150 /* adjust source segment length */
151 src->len = remove_va_start - src->addr;
153 /* create temporary segment in the middle */
154 tmp.addr = src->addr + src->len;
155 tmp.iova = src->iova + src->len;
156 tmp.len = remove_len;
158 /* populate end segment - this one we will be keeping */
159 end->addr = tmp.addr + tmp.len;
160 end->iova = tmp.iova + tmp.len;
161 end->len = total_len - src->len - tmp.len;
165 /* try merging two maps into one, return 1 if succeeded */
167 merge_map(struct user_mem_map *left, struct user_mem_map *right)
169 if (left->addr + left->len != right->addr)
171 if (left->iova + left->len != right->iova)
174 left->len += right->len;
176 memset(right, 0, sizeof(*right));
181 static struct user_mem_map *
182 find_user_mem_map(struct user_mem_maps *user_mem_maps, uint64_t addr,
183 uint64_t iova, uint64_t len)
185 uint64_t va_end = addr + len;
186 uint64_t iova_end = iova + len;
189 for (i = 0; i < user_mem_maps->n_maps; i++) {
190 struct user_mem_map *map = &user_mem_maps->maps[i];
191 uint64_t map_va_end = map->addr + map->len;
192 uint64_t map_iova_end = map->iova + map->len;
195 if (addr < map->addr || addr >= map_va_end)
197 /* check if VA end is within boundaries */
198 if (va_end <= map->addr || va_end > map_va_end)
201 /* check start IOVA */
202 if (iova < map->iova || iova >= map_iova_end)
204 /* check if IOVA end is within boundaries */
205 if (iova_end <= map->iova || iova_end > map_iova_end)
208 /* we've found our map */
214 /* this will sort all user maps, and merge/compact any adjacent maps */
216 compact_user_maps(struct user_mem_maps *user_mem_maps)
218 int i, n_merged, cur_idx;
220 qsort(user_mem_maps->maps, user_mem_maps->n_maps,
221 sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
223 /* we'll go over the list backwards when merging */
225 for (i = user_mem_maps->n_maps - 2; i >= 0; i--) {
226 struct user_mem_map *l, *r;
228 l = &user_mem_maps->maps[i];
229 r = &user_mem_maps->maps[i + 1];
231 if (is_null_map(l) || is_null_map(r))
238 /* the entries are still sorted, but now they have holes in them, so
239 * walk through the list and remove the holes
243 for (i = 0; i < user_mem_maps->n_maps; i++) {
244 if (!is_null_map(&user_mem_maps->maps[i])) {
245 struct user_mem_map *src, *dst;
247 src = &user_mem_maps->maps[i];
248 dst = &user_mem_maps->maps[cur_idx++];
251 memcpy(dst, src, sizeof(*src));
252 memset(src, 0, sizeof(*src));
256 user_mem_maps->n_maps = cur_idx;
261 vfio_open_group_fd(int iommu_group_num)
264 char filename[PATH_MAX];
265 struct rte_mp_msg mp_req, *mp_rep;
266 struct rte_mp_reply mp_reply;
267 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
268 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
270 /* if primary, try to open the group */
271 if (internal_config.process_type == RTE_PROC_PRIMARY) {
272 /* try regular group format */
273 snprintf(filename, sizeof(filename),
274 VFIO_GROUP_FMT, iommu_group_num);
275 vfio_group_fd = open(filename, O_RDWR);
276 if (vfio_group_fd < 0) {
277 /* if file not found, it's not an error */
278 if (errno != ENOENT) {
279 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
284 /* special case: try no-IOMMU path as well */
285 snprintf(filename, sizeof(filename),
286 VFIO_NOIOMMU_GROUP_FMT,
288 vfio_group_fd = open(filename, O_RDWR);
289 if (vfio_group_fd < 0) {
290 if (errno != ENOENT) {
291 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
297 /* noiommu group found */
300 return vfio_group_fd;
302 /* if we're in a secondary process, request group fd from the primary
303 * process via mp channel.
305 p->req = SOCKET_REQ_GROUP;
306 p->group_num = iommu_group_num;
307 strcpy(mp_req.name, EAL_VFIO_MP);
308 mp_req.len_param = sizeof(*p);
312 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
313 mp_reply.nb_received == 1) {
314 mp_rep = &mp_reply.msgs[0];
315 p = (struct vfio_mp_param *)mp_rep->param;
316 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
317 vfio_group_fd = mp_rep->fds[0];
318 } else if (p->result == SOCKET_NO_FD) {
319 RTE_LOG(ERR, EAL, " bad VFIO group fd\n");
325 if (vfio_group_fd < 0)
326 RTE_LOG(ERR, EAL, " cannot request group fd\n");
327 return vfio_group_fd;
330 static struct vfio_config *
331 get_vfio_cfg_by_group_num(int iommu_group_num)
333 struct vfio_config *vfio_cfg;
336 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
337 vfio_cfg = &vfio_cfgs[i];
338 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
339 if (vfio_cfg->vfio_groups[j].group_num ==
348 static struct vfio_config *
349 get_vfio_cfg_by_group_fd(int vfio_group_fd)
351 struct vfio_config *vfio_cfg;
354 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
355 vfio_cfg = &vfio_cfgs[i];
356 for (j = 0; j < VFIO_MAX_GROUPS; j++)
357 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
364 static struct vfio_config *
365 get_vfio_cfg_by_container_fd(int container_fd)
369 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
370 if (vfio_cfgs[i].vfio_container_fd == container_fd)
371 return &vfio_cfgs[i];
378 rte_vfio_get_group_fd(int iommu_group_num)
382 struct vfio_group *cur_grp;
383 struct vfio_config *vfio_cfg;
385 /* get the vfio_config it belongs to */
386 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
387 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
389 /* check if we already have the group descriptor open */
390 for (i = 0; i < VFIO_MAX_GROUPS; i++)
391 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
392 return vfio_cfg->vfio_groups[i].fd;
394 /* Lets see first if there is room for a new group */
395 if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
396 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
400 /* Now lets get an index for the new group */
401 for (i = 0; i < VFIO_MAX_GROUPS; i++)
402 if (vfio_cfg->vfio_groups[i].group_num == -1) {
403 cur_grp = &vfio_cfg->vfio_groups[i];
407 /* This should not happen */
408 if (i == VFIO_MAX_GROUPS) {
409 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
413 vfio_group_fd = vfio_open_group_fd(iommu_group_num);
414 if (vfio_group_fd < 0) {
415 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
419 cur_grp->group_num = iommu_group_num;
420 cur_grp->fd = vfio_group_fd;
421 vfio_cfg->vfio_active_groups++;
423 return vfio_group_fd;
427 get_vfio_group_idx(int vfio_group_fd)
429 struct vfio_config *vfio_cfg;
432 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
433 vfio_cfg = &vfio_cfgs[i];
434 for (j = 0; j < VFIO_MAX_GROUPS; j++)
435 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
443 vfio_group_device_get(int vfio_group_fd)
445 struct vfio_config *vfio_cfg;
448 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
449 if (vfio_cfg == NULL) {
450 RTE_LOG(ERR, EAL, " invalid group fd!\n");
454 i = get_vfio_group_idx(vfio_group_fd);
455 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
456 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
458 vfio_cfg->vfio_groups[i].devices++;
462 vfio_group_device_put(int vfio_group_fd)
464 struct vfio_config *vfio_cfg;
467 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
468 if (vfio_cfg == NULL) {
469 RTE_LOG(ERR, EAL, " invalid group fd!\n");
473 i = get_vfio_group_idx(vfio_group_fd);
474 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
475 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
477 vfio_cfg->vfio_groups[i].devices--;
481 vfio_group_device_count(int vfio_group_fd)
483 struct vfio_config *vfio_cfg;
486 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
487 if (vfio_cfg == NULL) {
488 RTE_LOG(ERR, EAL, " invalid group fd!\n");
492 i = get_vfio_group_idx(vfio_group_fd);
493 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
494 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
498 return vfio_cfg->vfio_groups[i].devices;
502 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
503 void *arg __rte_unused)
505 struct rte_memseg_list *msl;
506 struct rte_memseg *ms;
509 msl = rte_mem_virt2memseg_list(addr);
511 /* for IOVA as VA mode, no need to care for IOVA addresses */
512 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
513 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
514 if (type == RTE_MEM_EVENT_ALLOC)
515 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
518 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
523 /* memsegs are contiguous in memory */
524 ms = rte_mem_virt2memseg(addr, msl);
525 while (cur_len < len) {
526 if (type == RTE_MEM_EVENT_ALLOC)
527 vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
528 ms->iova, ms->len, 1);
530 vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
531 ms->iova, ms->len, 0);
539 rte_vfio_clear_group(int vfio_group_fd)
542 struct vfio_config *vfio_cfg;
544 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
545 if (vfio_cfg == NULL) {
546 RTE_LOG(ERR, EAL, " invalid group fd!\n");
550 i = get_vfio_group_idx(vfio_group_fd);
553 vfio_cfg->vfio_groups[i].group_num = -1;
554 vfio_cfg->vfio_groups[i].fd = -1;
555 vfio_cfg->vfio_groups[i].devices = 0;
556 vfio_cfg->vfio_active_groups--;
562 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
563 int *vfio_dev_fd, struct vfio_device_info *device_info)
565 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
566 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
567 struct vfio_group_status group_status = {
568 .argsz = sizeof(group_status)
570 struct vfio_config *vfio_cfg;
571 struct user_mem_maps *user_mem_maps;
572 int vfio_container_fd;
577 /* get group number */
578 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
580 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
585 /* if negative, something failed */
589 /* get the actual group fd */
590 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
591 if (vfio_group_fd < 0)
594 /* if group_fd == 0, that means the device isn't managed by VFIO */
595 if (vfio_group_fd == 0) {
596 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
602 * at this point, we know that this group is viable (meaning, all devices
603 * are either bound to VFIO or not bound to anything)
606 /* check if the group is viable */
607 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
609 RTE_LOG(ERR, EAL, " %s cannot get group status, "
610 "error %i (%s)\n", dev_addr, errno, strerror(errno));
611 close(vfio_group_fd);
612 rte_vfio_clear_group(vfio_group_fd);
614 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
615 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
616 close(vfio_group_fd);
617 rte_vfio_clear_group(vfio_group_fd);
621 /* get the vfio_config it belongs to */
622 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
623 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
624 vfio_container_fd = vfio_cfg->vfio_container_fd;
625 user_mem_maps = &vfio_cfg->mem_maps;
627 /* check if group does not have a container yet */
628 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
630 /* add group to a container */
631 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
634 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
635 "error %i (%s)\n", dev_addr, errno, strerror(errno));
636 close(vfio_group_fd);
637 rte_vfio_clear_group(vfio_group_fd);
642 * pick an IOMMU type and set up DMA mappings for container
644 * needs to be done only once, only when first group is
645 * assigned to a container and only in primary process.
646 * Note this can happen several times with the hotplug
649 if (internal_config.process_type == RTE_PROC_PRIMARY &&
650 vfio_cfg->vfio_active_groups == 1 &&
651 vfio_group_device_count(vfio_group_fd) == 0) {
652 const struct vfio_iommu_type *t;
654 /* select an IOMMU type which we will be using */
655 t = vfio_set_iommu_type(vfio_container_fd);
658 " %s failed to select IOMMU type\n",
660 close(vfio_group_fd);
661 rte_vfio_clear_group(vfio_group_fd);
664 /* lock memory hotplug before mapping and release it
665 * after registering callback, to prevent races
667 rte_rwlock_read_lock(mem_lock);
668 if (vfio_cfg == default_vfio_cfg)
669 ret = t->dma_map_func(vfio_container_fd);
674 " %s DMA remapping failed, error %i (%s)\n",
675 dev_addr, errno, strerror(errno));
676 close(vfio_group_fd);
677 rte_vfio_clear_group(vfio_group_fd);
678 rte_rwlock_read_unlock(mem_lock);
682 vfio_cfg->vfio_iommu_type = t;
684 /* re-map all user-mapped segments */
685 rte_spinlock_recursive_lock(&user_mem_maps->lock);
687 /* this IOMMU type may not support DMA mapping, but
688 * if we have mappings in the list - that means we have
689 * previously mapped something successfully, so we can
690 * be sure that DMA mapping is supported.
692 for (i = 0; i < user_mem_maps->n_maps; i++) {
693 struct user_mem_map *map;
694 map = &user_mem_maps->maps[i];
696 ret = t->dma_user_map_func(
698 map->addr, map->iova, map->len,
701 RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
703 "iova: 0x%" PRIx64 " "
704 "len: 0x%" PRIu64 "\n",
705 map->addr, map->iova,
707 rte_spinlock_recursive_unlock(
708 &user_mem_maps->lock);
709 rte_rwlock_read_unlock(mem_lock);
713 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
715 /* register callback for mem events */
716 if (vfio_cfg == default_vfio_cfg)
717 ret = rte_mem_event_callback_register(
718 VFIO_MEM_EVENT_CLB_NAME,
719 vfio_mem_event_callback, NULL);
722 /* unlock memory hotplug */
723 rte_rwlock_read_unlock(mem_lock);
725 if (ret && rte_errno != ENOTSUP) {
726 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
730 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
732 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
736 /* get a file descriptor for the device */
737 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
738 if (*vfio_dev_fd < 0) {
739 /* if we cannot get a device fd, this implies a problem with
740 * the VFIO group or the container not having IOMMU configured.
743 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
745 close(vfio_group_fd);
746 rte_vfio_clear_group(vfio_group_fd);
750 /* test and setup the device */
751 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
753 RTE_LOG(ERR, EAL, " %s cannot get device info, "
754 "error %i (%s)\n", dev_addr, errno,
757 close(vfio_group_fd);
758 rte_vfio_clear_group(vfio_group_fd);
761 vfio_group_device_get(vfio_group_fd);
767 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
770 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
771 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
772 struct vfio_group_status group_status = {
773 .argsz = sizeof(group_status)
775 struct vfio_config *vfio_cfg;
780 /* we don't want any DMA mapping messages to come while we're detaching
781 * VFIO device, because this might be the last device and we might need
782 * to unregister the callback.
784 rte_rwlock_read_lock(mem_lock);
786 /* get group number */
787 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
789 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
791 /* This is an error at this point. */
796 /* get the actual group fd */
797 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
798 if (vfio_group_fd <= 0) {
799 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
805 /* get the vfio_config it belongs to */
806 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
807 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
809 /* At this point we got an active group. Closing it will make the
810 * container detachment. If this is the last active group, VFIO kernel
811 * code will unset the container and the IOMMU mappings.
814 /* Closing a device */
815 if (close(vfio_dev_fd) < 0) {
816 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
822 /* An VFIO group can have several devices attached. Just when there is
823 * no devices remaining should the group be closed.
825 vfio_group_device_put(vfio_group_fd);
826 if (!vfio_group_device_count(vfio_group_fd)) {
828 if (close(vfio_group_fd) < 0) {
829 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
835 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
836 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
843 /* if there are no active device groups, unregister the callback to
844 * avoid spurious attempts to map/unmap memory from VFIO.
846 if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0)
847 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
854 rte_rwlock_read_unlock(mem_lock);
859 rte_vfio_enable(const char *modname)
861 /* initialize group list */
865 rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
867 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
868 vfio_cfgs[i].vfio_container_fd = -1;
869 vfio_cfgs[i].vfio_active_groups = 0;
870 vfio_cfgs[i].vfio_iommu_type = NULL;
871 vfio_cfgs[i].mem_maps.lock = lock;
873 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
874 vfio_cfgs[i].vfio_groups[j].fd = -1;
875 vfio_cfgs[i].vfio_groups[j].group_num = -1;
876 vfio_cfgs[i].vfio_groups[j].devices = 0;
880 /* inform the user that we are probing for VFIO */
881 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
883 /* check if vfio module is loaded */
884 vfio_available = rte_eal_check_module(modname);
886 /* return error directly */
887 if (vfio_available == -1) {
888 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
892 /* return 0 if VFIO modules not loaded */
893 if (vfio_available == 0) {
894 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
895 "skipping VFIO support...\n");
899 default_vfio_cfg->vfio_container_fd = rte_vfio_get_container_fd();
901 /* check if we have VFIO driver enabled */
902 if (default_vfio_cfg->vfio_container_fd != -1) {
903 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
904 default_vfio_cfg->vfio_enabled = 1;
906 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
913 rte_vfio_is_enabled(const char *modname)
915 const int mod_available = rte_eal_check_module(modname) > 0;
916 return default_vfio_cfg->vfio_enabled && mod_available;
919 const struct vfio_iommu_type *
920 vfio_set_iommu_type(int vfio_container_fd)
923 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
924 const struct vfio_iommu_type *t = &iommu_types[idx];
926 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
929 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
930 t->type_id, t->name);
933 /* not an error, there may be more supported IOMMU types */
934 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
935 "error %i (%s)\n", t->type_id, t->name, errno,
938 /* if we didn't find a suitable IOMMU type, fail */
943 vfio_has_supported_extensions(int vfio_container_fd)
946 unsigned idx, n_extensions = 0;
947 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
948 const struct vfio_iommu_type *t = &iommu_types[idx];
950 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
953 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
954 "error %i (%s)\n", errno,
956 close(vfio_container_fd);
958 } else if (ret == 1) {
959 /* we found a supported extension */
962 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
964 ret ? "supported" : "not supported");
967 /* if we didn't find any supported IOMMU types, fail */
969 close(vfio_container_fd);
977 rte_vfio_get_container_fd(void)
979 int ret, vfio_container_fd;
980 struct rte_mp_msg mp_req, *mp_rep;
981 struct rte_mp_reply mp_reply;
982 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
983 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
986 /* if we're in a primary process, try to open the container */
987 if (internal_config.process_type == RTE_PROC_PRIMARY) {
988 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
989 if (vfio_container_fd < 0) {
990 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
991 "error %i (%s)\n", errno, strerror(errno));
995 /* check VFIO API version */
996 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
997 if (ret != VFIO_API_VERSION) {
999 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
1000 "error %i (%s)\n", errno, strerror(errno));
1002 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
1003 close(vfio_container_fd);
1007 ret = vfio_has_supported_extensions(vfio_container_fd);
1009 RTE_LOG(ERR, EAL, " no supported IOMMU "
1010 "extensions found!\n");
1014 return vfio_container_fd;
1017 * if we're in a secondary process, request container fd from the
1018 * primary process via mp channel
1020 p->req = SOCKET_REQ_CONTAINER;
1021 strcpy(mp_req.name, EAL_VFIO_MP);
1022 mp_req.len_param = sizeof(*p);
1025 vfio_container_fd = -1;
1026 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1027 mp_reply.nb_received == 1) {
1028 mp_rep = &mp_reply.msgs[0];
1029 p = (struct vfio_mp_param *)mp_rep->param;
1030 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1031 free(mp_reply.msgs);
1032 return mp_rep->fds[0];
1034 free(mp_reply.msgs);
1037 RTE_LOG(ERR, EAL, " cannot request container fd\n");
1042 rte_vfio_get_group_num(const char *sysfs_base,
1043 const char *dev_addr, int *iommu_group_num)
1045 char linkname[PATH_MAX];
1046 char filename[PATH_MAX];
1047 char *tok[16], *group_tok, *end;
1050 memset(linkname, 0, sizeof(linkname));
1051 memset(filename, 0, sizeof(filename));
1053 /* try to find out IOMMU group for this device */
1054 snprintf(linkname, sizeof(linkname),
1055 "%s/%s/iommu_group", sysfs_base, dev_addr);
1057 ret = readlink(linkname, filename, sizeof(filename));
1059 /* if the link doesn't exist, no VFIO for us */
1063 ret = rte_strsplit(filename, sizeof(filename),
1064 tok, RTE_DIM(tok), '/');
1067 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
1071 /* IOMMU group is always the last token */
1073 group_tok = tok[ret - 1];
1075 *iommu_group_num = strtol(group_tok, &end, 10);
1076 if ((end != group_tok && *end != '\0') || errno != 0) {
1077 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
1085 type1_map(const struct rte_memseg_list *msl __rte_unused,
1086 const struct rte_memseg *ms, void *arg)
1088 int *vfio_container_fd = arg;
1090 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1095 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1096 uint64_t len, int do_map)
1098 struct vfio_iommu_type1_dma_map dma_map;
1099 struct vfio_iommu_type1_dma_unmap dma_unmap;
1103 memset(&dma_map, 0, sizeof(dma_map));
1104 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1105 dma_map.vaddr = vaddr;
1107 dma_map.iova = iova;
1108 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1109 VFIO_DMA_MAP_FLAG_WRITE;
1111 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1113 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1114 errno, strerror(errno));
1118 memset(&dma_unmap, 0, sizeof(dma_unmap));
1119 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1120 dma_unmap.size = len;
1121 dma_unmap.iova = iova;
1123 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1126 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1127 errno, strerror(errno));
1136 vfio_type1_dma_map(int vfio_container_fd)
1138 return rte_memseg_walk(type1_map, &vfio_container_fd);
1142 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1143 uint64_t len, int do_map)
1145 struct vfio_iommu_type1_dma_map dma_map;
1146 struct vfio_iommu_type1_dma_unmap dma_unmap;
1150 memset(&dma_map, 0, sizeof(dma_map));
1151 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1152 dma_map.vaddr = vaddr;
1154 dma_map.iova = iova;
1155 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1156 VFIO_DMA_MAP_FLAG_WRITE;
1158 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1160 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1161 errno, strerror(errno));
1166 struct vfio_iommu_spapr_register_memory reg = {
1167 .argsz = sizeof(reg),
1170 reg.vaddr = (uintptr_t) vaddr;
1173 ret = ioctl(vfio_container_fd,
1174 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
1176 RTE_LOG(ERR, EAL, " cannot unregister vaddr for IOMMU, error %i (%s)\n",
1177 errno, strerror(errno));
1181 memset(&dma_unmap, 0, sizeof(dma_unmap));
1182 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1183 dma_unmap.size = len;
1184 dma_unmap.iova = iova;
1186 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1189 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1190 errno, strerror(errno));
1199 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1200 const struct rte_memseg *ms, void *arg)
1202 int *vfio_container_fd = arg;
1204 return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1208 struct spapr_walk_param {
1209 uint64_t window_size;
1210 uint64_t hugepage_sz;
1213 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1214 const struct rte_memseg *ms, void *arg)
1216 struct spapr_walk_param *param = arg;
1217 uint64_t max = ms->iova + ms->len;
1219 if (max > param->window_size) {
1220 param->hugepage_sz = ms->hugepage_sz;
1221 param->window_size = max;
1228 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1229 struct vfio_iommu_spapr_tce_create *create) {
1230 struct vfio_iommu_spapr_tce_remove remove = {
1231 .argsz = sizeof(remove),
1233 struct vfio_iommu_spapr_tce_info info = {
1234 .argsz = sizeof(info),
1238 /* query spapr iommu info */
1239 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1241 RTE_LOG(ERR, EAL, " cannot get iommu info, "
1242 "error %i (%s)\n", errno, strerror(errno));
1246 /* remove default DMA of 32 bit window */
1247 remove.start_addr = info.dma32_window_start;
1248 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1250 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
1251 "error %i (%s)\n", errno, strerror(errno));
1255 /* create new DMA window */
1256 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1258 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
1259 "error %i (%s)\n", errno, strerror(errno));
1263 if (create->start_addr != 0) {
1264 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
1272 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1273 uint64_t len, int do_map)
1275 struct spapr_walk_param param;
1276 struct vfio_iommu_spapr_tce_create create = {
1277 .argsz = sizeof(create),
1279 struct vfio_config *vfio_cfg;
1280 struct user_mem_maps *user_mem_maps;
1283 vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1284 if (vfio_cfg == NULL) {
1285 RTE_LOG(ERR, EAL, " invalid container fd!\n");
1289 user_mem_maps = &vfio_cfg->mem_maps;
1290 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1292 /* check if window size needs to be adjusted */
1293 memset(¶m, 0, sizeof(param));
1295 /* we're inside a callback so use thread-unsafe version */
1296 if (rte_memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1298 RTE_LOG(ERR, EAL, "Could not get window size\n");
1303 /* also check user maps */
1304 for (i = 0; i < user_mem_maps->n_maps; i++) {
1305 uint64_t max = user_mem_maps->maps[i].iova +
1306 user_mem_maps->maps[i].len;
1307 create.window_size = RTE_MAX(create.window_size, max);
1310 /* sPAPR requires window size to be a power of 2 */
1311 create.window_size = rte_align64pow2(param.window_size);
1312 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1317 /* re-create window and remap the entire memory */
1318 if (iova > create.window_size) {
1319 if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1321 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1325 /* we're inside a callback, so use thread-unsafe version
1327 if (rte_memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1328 &vfio_container_fd) < 0) {
1329 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1333 /* remap all user maps */
1334 for (i = 0; i < user_mem_maps->n_maps; i++) {
1335 struct user_mem_map *map =
1336 &user_mem_maps->maps[i];
1337 if (vfio_spapr_dma_do_map(vfio_container_fd,
1338 map->addr, map->iova, map->len,
1340 RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1347 /* now that we've remapped all of the memory that was present
1348 * before, map the segment that we were requested to map.
1350 * however, if we were called by the callback, the memory we
1351 * were called with was already in the memseg list, so previous
1352 * mapping should've mapped that segment already.
1354 * virt2memseg_list is a relatively cheap check, so use that. if
1355 * memory is within any memseg list, it's a memseg, so it's
1358 addr = (void *)(uintptr_t)vaddr;
1359 if (rte_mem_virt2memseg_list(addr) == NULL &&
1360 vfio_spapr_dma_do_map(vfio_container_fd,
1361 vaddr, iova, len, 1) < 0) {
1362 RTE_LOG(ERR, EAL, "Could not map segment\n");
1367 /* for unmap, check if iova within DMA window */
1368 if (iova > create.window_size) {
1369 RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1374 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1377 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1382 vfio_spapr_dma_map(int vfio_container_fd)
1384 struct vfio_iommu_spapr_tce_create create = {
1385 .argsz = sizeof(create),
1387 struct spapr_walk_param param;
1389 memset(¶m, 0, sizeof(param));
1391 /* create DMA window from 0 to max(phys_addr + len) */
1392 rte_memseg_walk(vfio_spapr_window_size_walk, ¶m);
1394 /* sPAPR requires window size to be a power of 2 */
1395 create.window_size = rte_align64pow2(param.window_size);
1396 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1399 if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1400 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1404 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1405 if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1412 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1414 /* No-IOMMU mode does not need DMA mapping */
1419 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1420 uint64_t __rte_unused vaddr,
1421 uint64_t __rte_unused iova, uint64_t __rte_unused len,
1422 int __rte_unused do_map)
1424 /* No-IOMMU mode does not need DMA mapping */
1429 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1430 uint64_t len, int do_map)
1432 const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1435 RTE_LOG(ERR, EAL, " VFIO support not initialized\n");
1440 if (!t->dma_user_map_func) {
1442 " VFIO custom DMA region maping not supported by IOMMU %s\n",
1444 rte_errno = ENOTSUP;
1448 return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1453 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1456 struct user_mem_map *new_map;
1457 struct user_mem_maps *user_mem_maps;
1460 user_mem_maps = &vfio_cfg->mem_maps;
1461 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1462 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1463 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1469 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1470 /* technically, this will fail if there are currently no devices
1471 * plugged in, even if a device were added later, this mapping
1472 * might have succeeded. however, since we cannot verify if this
1473 * is a valid mapping without having a device attached, consider
1474 * this to be unsupported, because we can't just store any old
1475 * mapping and pollute list of active mappings willy-nilly.
1477 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1481 /* create new user mem map entry */
1482 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1483 new_map->addr = vaddr;
1484 new_map->iova = iova;
1487 compact_user_maps(user_mem_maps);
1489 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1494 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1497 struct user_mem_map *map, *new_map = NULL;
1498 struct user_mem_maps *user_mem_maps;
1501 user_mem_maps = &vfio_cfg->mem_maps;
1502 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1504 /* find our mapping */
1505 map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1507 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1512 if (map->addr != vaddr || map->iova != iova || map->len != len) {
1513 /* we're partially unmapping a previously mapped region, so we
1514 * need to split entry into two.
1516 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1517 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1522 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1525 /* unmap the entry */
1526 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1527 /* there may not be any devices plugged in, so unmapping will
1528 * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1529 * stop us from removing the mapping, as the assumption is we
1530 * won't be needing this memory any more and thus will want to
1531 * prevent it from being remapped again on hotplug. so, only
1532 * fail if we indeed failed to unmap (e.g. if the mapping was
1533 * within our mapped range but had invalid alignment).
1535 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1536 RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1540 RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1543 /* remove map from the list of active mappings */
1544 if (new_map != NULL) {
1545 adjust_map(map, new_map, vaddr, len);
1547 /* if we've created a new map by splitting, sort everything */
1548 if (!is_null_map(new_map)) {
1549 compact_user_maps(user_mem_maps);
1551 /* we've created a new mapping, but it was unused */
1552 user_mem_maps->n_maps--;
1555 memset(map, 0, sizeof(*map));
1556 compact_user_maps(user_mem_maps);
1557 user_mem_maps->n_maps--;
1561 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1566 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1573 return container_dma_map(default_vfio_cfg, vaddr, iova, len);
1577 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1584 return container_dma_unmap(default_vfio_cfg, vaddr, iova, len);
1588 rte_vfio_noiommu_is_enabled(void)
1594 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1596 if (errno != ENOENT) {
1597 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
1598 errno, strerror(errno));
1602 * else the file does not exists
1603 * i.e. noiommu is not enabled
1608 cnt = read(fd, &c, 1);
1611 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
1612 "file %i (%s)\n", errno, strerror(errno));
1620 rte_vfio_container_create(void)
1624 /* Find an empty slot to store new vfio config */
1625 for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1626 if (vfio_cfgs[i].vfio_container_fd == -1)
1630 if (i == VFIO_MAX_CONTAINERS) {
1631 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1635 vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1636 if (vfio_cfgs[i].vfio_container_fd < 0) {
1637 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1641 return vfio_cfgs[i].vfio_container_fd;
1644 int __rte_experimental
1645 rte_vfio_container_destroy(int container_fd)
1647 struct vfio_config *vfio_cfg;
1650 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1651 if (vfio_cfg == NULL) {
1652 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1656 for (i = 0; i < VFIO_MAX_GROUPS; i++)
1657 if (vfio_cfg->vfio_groups[i].group_num != -1)
1658 rte_vfio_container_group_unbind(container_fd,
1659 vfio_cfg->vfio_groups[i].group_num);
1661 close(container_fd);
1662 vfio_cfg->vfio_container_fd = -1;
1663 vfio_cfg->vfio_active_groups = 0;
1664 vfio_cfg->vfio_iommu_type = NULL;
1670 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1672 struct vfio_config *vfio_cfg;
1673 struct vfio_group *cur_grp;
1677 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1678 if (vfio_cfg == NULL) {
1679 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1683 /* Check room for new group */
1684 if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
1685 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
1689 /* Get an index for the new group */
1690 for (i = 0; i < VFIO_MAX_GROUPS; i++)
1691 if (vfio_cfg->vfio_groups[i].group_num == -1) {
1692 cur_grp = &vfio_cfg->vfio_groups[i];
1696 /* This should not happen */
1697 if (i == VFIO_MAX_GROUPS) {
1698 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
1702 vfio_group_fd = vfio_open_group_fd(iommu_group_num);
1703 if (vfio_group_fd < 0) {
1704 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
1707 cur_grp->group_num = iommu_group_num;
1708 cur_grp->fd = vfio_group_fd;
1709 cur_grp->devices = 0;
1710 vfio_cfg->vfio_active_groups++;
1712 return vfio_group_fd;
1716 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
1718 struct vfio_config *vfio_cfg;
1719 struct vfio_group *cur_grp = NULL;
1722 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1723 if (vfio_cfg == NULL) {
1724 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1728 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
1729 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
1730 cur_grp = &vfio_cfg->vfio_groups[i];
1735 /* This should not happen */
1736 if (i == VFIO_MAX_GROUPS || cur_grp == NULL) {
1737 RTE_LOG(ERR, EAL, "Specified group number not found\n");
1741 if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
1742 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
1743 " iommu_group_num %d\n", iommu_group_num);
1746 cur_grp->group_num = -1;
1748 cur_grp->devices = 0;
1749 vfio_cfg->vfio_active_groups--;
1755 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
1758 struct vfio_config *vfio_cfg;
1765 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1766 if (vfio_cfg == NULL) {
1767 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1771 return container_dma_map(vfio_cfg, vaddr, iova, len);
1775 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
1778 struct vfio_config *vfio_cfg;
1785 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1786 if (vfio_cfg == NULL) {
1787 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1791 return container_dma_unmap(vfio_cfg, vaddr, iova, len);
1797 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1798 __rte_unused uint64_t len)
1804 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1805 __rte_unused uint64_t len)
1811 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
1812 __rte_unused const char *dev_addr,
1813 __rte_unused int *vfio_dev_fd,
1814 __rte_unused struct vfio_device_info *device_info)
1820 rte_vfio_release_device(__rte_unused const char *sysfs_base,
1821 __rte_unused const char *dev_addr, __rte_unused int fd)
1827 rte_vfio_enable(__rte_unused const char *modname)
1833 rte_vfio_is_enabled(__rte_unused const char *modname)
1839 rte_vfio_noiommu_is_enabled(void)
1845 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
1851 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
1852 __rte_unused const char *dev_addr,
1853 __rte_unused int *iommu_group_num)
1859 rte_vfio_get_container_fd(void)
1865 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
1871 rte_vfio_container_create(void)
1877 rte_vfio_container_destroy(__rte_unused int container_fd)
1883 rte_vfio_container_group_bind(__rte_unused int container_fd,
1884 __rte_unused int iommu_group_num)
1890 rte_vfio_container_group_unbind(__rte_unused int container_fd,
1891 __rte_unused int iommu_group_num)
1897 rte_vfio_container_dma_map(__rte_unused int container_fd,
1898 __rte_unused uint64_t vaddr,
1899 __rte_unused uint64_t iova,
1900 __rte_unused uint64_t len)
1906 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
1907 __rte_unused uint64_t vaddr,
1908 __rte_unused uint64_t iova,
1909 __rte_unused uint64_t len)
1914 #endif /* VFIO_PRESENT */