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
90 /* for sPAPR IOMMU, we will need to walk memseg list, but we cannot use
91 * rte_memseg_walk() because by the time we enter callback we will be holding a
92 * write lock, so regular rte-memseg_walk will deadlock. copying the same
93 * iteration code everywhere is not ideal as well. so, use a lockless copy of
97 memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
99 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
100 int i, ms_idx, ret = 0;
102 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
103 struct rte_memseg_list *msl = &mcfg->memsegs[i];
104 const struct rte_memseg *ms;
105 struct rte_fbarray *arr;
107 if (msl->memseg_arr.count == 0)
110 arr = &msl->memseg_arr;
112 ms_idx = rte_fbarray_find_next_used(arr, 0);
113 while (ms_idx >= 0) {
114 ms = rte_fbarray_get(arr, ms_idx);
115 ret = func(msl, ms, arg);
120 ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
127 is_null_map(const struct user_mem_map *map)
129 return map->addr == 0 && map->iova == 0 && map->len == 0;
132 /* we may need to merge user mem maps together in case of user mapping/unmapping
133 * chunks of memory, so we'll need a comparator function to sort segments.
136 user_mem_map_cmp(const void *a, const void *b)
138 const struct user_mem_map *umm_a = a;
139 const struct user_mem_map *umm_b = b;
141 /* move null entries to end */
142 if (is_null_map(umm_a))
144 if (is_null_map(umm_b))
147 /* sort by iova first */
148 if (umm_a->iova < umm_b->iova)
150 if (umm_a->iova > umm_b->iova)
153 if (umm_a->addr < umm_b->addr)
155 if (umm_a->addr > umm_b->addr)
158 if (umm_a->len < umm_b->len)
160 if (umm_a->len > umm_b->len)
166 /* adjust user map entry. this may result in shortening of existing map, or in
167 * splitting existing map in two pieces.
170 adjust_map(struct user_mem_map *src, struct user_mem_map *end,
171 uint64_t remove_va_start, uint64_t remove_len)
173 /* if va start is same as start address, we're simply moving start */
174 if (remove_va_start == src->addr) {
175 src->addr += remove_len;
176 src->iova += remove_len;
177 src->len -= remove_len;
178 } else if (remove_va_start + remove_len == src->addr + src->len) {
179 /* we're shrinking mapping from the end */
180 src->len -= remove_len;
182 /* we're blowing a hole in the middle */
183 struct user_mem_map tmp;
184 uint64_t total_len = src->len;
186 /* adjust source segment length */
187 src->len = remove_va_start - src->addr;
189 /* create temporary segment in the middle */
190 tmp.addr = src->addr + src->len;
191 tmp.iova = src->iova + src->len;
192 tmp.len = remove_len;
194 /* populate end segment - this one we will be keeping */
195 end->addr = tmp.addr + tmp.len;
196 end->iova = tmp.iova + tmp.len;
197 end->len = total_len - src->len - tmp.len;
201 /* try merging two maps into one, return 1 if succeeded */
203 merge_map(struct user_mem_map *left, struct user_mem_map *right)
205 if (left->addr + left->len != right->addr)
207 if (left->iova + left->len != right->iova)
210 left->len += right->len;
212 memset(right, 0, sizeof(*right));
217 static struct user_mem_map *
218 find_user_mem_map(struct user_mem_maps *user_mem_maps, uint64_t addr,
219 uint64_t iova, uint64_t len)
221 uint64_t va_end = addr + len;
222 uint64_t iova_end = iova + len;
225 for (i = 0; i < user_mem_maps->n_maps; i++) {
226 struct user_mem_map *map = &user_mem_maps->maps[i];
227 uint64_t map_va_end = map->addr + map->len;
228 uint64_t map_iova_end = map->iova + map->len;
231 if (addr < map->addr || addr >= map_va_end)
233 /* check if VA end is within boundaries */
234 if (va_end <= map->addr || va_end > map_va_end)
237 /* check start IOVA */
238 if (iova < map->iova || iova >= map_iova_end)
240 /* check if IOVA end is within boundaries */
241 if (iova_end <= map->iova || iova_end > map_iova_end)
244 /* we've found our map */
250 /* this will sort all user maps, and merge/compact any adjacent maps */
252 compact_user_maps(struct user_mem_maps *user_mem_maps)
254 int i, n_merged, cur_idx;
256 qsort(user_mem_maps->maps, user_mem_maps->n_maps,
257 sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
259 /* we'll go over the list backwards when merging */
261 for (i = user_mem_maps->n_maps - 2; i >= 0; i--) {
262 struct user_mem_map *l, *r;
264 l = &user_mem_maps->maps[i];
265 r = &user_mem_maps->maps[i + 1];
267 if (is_null_map(l) || is_null_map(r))
274 /* the entries are still sorted, but now they have holes in them, so
275 * walk through the list and remove the holes
279 for (i = 0; i < user_mem_maps->n_maps; i++) {
280 if (!is_null_map(&user_mem_maps->maps[i])) {
281 struct user_mem_map *src, *dst;
283 src = &user_mem_maps->maps[i];
284 dst = &user_mem_maps->maps[cur_idx++];
287 memcpy(dst, src, sizeof(*src));
288 memset(src, 0, sizeof(*src));
292 user_mem_maps->n_maps = cur_idx;
297 vfio_open_group_fd(int iommu_group_num)
300 char filename[PATH_MAX];
301 struct rte_mp_msg mp_req, *mp_rep;
302 struct rte_mp_reply mp_reply;
303 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
304 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
306 /* if primary, try to open the group */
307 if (internal_config.process_type == RTE_PROC_PRIMARY) {
308 /* try regular group format */
309 snprintf(filename, sizeof(filename),
310 VFIO_GROUP_FMT, iommu_group_num);
311 vfio_group_fd = open(filename, O_RDWR);
312 if (vfio_group_fd < 0) {
313 /* if file not found, it's not an error */
314 if (errno != ENOENT) {
315 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
320 /* special case: try no-IOMMU path as well */
321 snprintf(filename, sizeof(filename),
322 VFIO_NOIOMMU_GROUP_FMT,
324 vfio_group_fd = open(filename, O_RDWR);
325 if (vfio_group_fd < 0) {
326 if (errno != ENOENT) {
327 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
333 /* noiommu group found */
336 return vfio_group_fd;
338 /* if we're in a secondary process, request group fd from the primary
339 * process via mp channel.
341 p->req = SOCKET_REQ_GROUP;
342 p->group_num = iommu_group_num;
343 strcpy(mp_req.name, EAL_VFIO_MP);
344 mp_req.len_param = sizeof(*p);
348 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
349 mp_reply.nb_received == 1) {
350 mp_rep = &mp_reply.msgs[0];
351 p = (struct vfio_mp_param *)mp_rep->param;
352 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
353 vfio_group_fd = mp_rep->fds[0];
354 } else if (p->result == SOCKET_NO_FD) {
355 RTE_LOG(ERR, EAL, " bad VFIO group fd\n");
361 if (vfio_group_fd < 0)
362 RTE_LOG(ERR, EAL, " cannot request group fd\n");
363 return vfio_group_fd;
366 static struct vfio_config *
367 get_vfio_cfg_by_group_num(int iommu_group_num)
369 struct vfio_config *vfio_cfg;
372 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
373 vfio_cfg = &vfio_cfgs[i];
374 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
375 if (vfio_cfg->vfio_groups[j].group_num ==
384 static struct vfio_config *
385 get_vfio_cfg_by_group_fd(int vfio_group_fd)
387 struct vfio_config *vfio_cfg;
390 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
391 vfio_cfg = &vfio_cfgs[i];
392 for (j = 0; j < VFIO_MAX_GROUPS; j++)
393 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
400 static struct vfio_config *
401 get_vfio_cfg_by_container_fd(int container_fd)
405 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
406 if (vfio_cfgs[i].vfio_container_fd == container_fd)
407 return &vfio_cfgs[i];
414 rte_vfio_get_group_fd(int iommu_group_num)
418 struct vfio_group *cur_grp;
419 struct vfio_config *vfio_cfg;
421 /* get the vfio_config it belongs to */
422 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
423 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
425 /* check if we already have the group descriptor open */
426 for (i = 0; i < VFIO_MAX_GROUPS; i++)
427 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
428 return vfio_cfg->vfio_groups[i].fd;
430 /* Lets see first if there is room for a new group */
431 if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
432 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
436 /* Now lets get an index for the new group */
437 for (i = 0; i < VFIO_MAX_GROUPS; i++)
438 if (vfio_cfg->vfio_groups[i].group_num == -1) {
439 cur_grp = &vfio_cfg->vfio_groups[i];
443 /* This should not happen */
444 if (i == VFIO_MAX_GROUPS) {
445 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
449 vfio_group_fd = vfio_open_group_fd(iommu_group_num);
450 if (vfio_group_fd < 0) {
451 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
455 cur_grp->group_num = iommu_group_num;
456 cur_grp->fd = vfio_group_fd;
457 vfio_cfg->vfio_active_groups++;
459 return vfio_group_fd;
463 get_vfio_group_idx(int vfio_group_fd)
465 struct vfio_config *vfio_cfg;
468 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
469 vfio_cfg = &vfio_cfgs[i];
470 for (j = 0; j < VFIO_MAX_GROUPS; j++)
471 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
479 vfio_group_device_get(int vfio_group_fd)
481 struct vfio_config *vfio_cfg;
484 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
485 if (vfio_cfg == NULL) {
486 RTE_LOG(ERR, EAL, " invalid group fd!\n");
490 i = get_vfio_group_idx(vfio_group_fd);
491 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
492 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
494 vfio_cfg->vfio_groups[i].devices++;
498 vfio_group_device_put(int vfio_group_fd)
500 struct vfio_config *vfio_cfg;
503 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
504 if (vfio_cfg == NULL) {
505 RTE_LOG(ERR, EAL, " invalid group fd!\n");
509 i = get_vfio_group_idx(vfio_group_fd);
510 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
511 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
513 vfio_cfg->vfio_groups[i].devices--;
517 vfio_group_device_count(int vfio_group_fd)
519 struct vfio_config *vfio_cfg;
522 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
523 if (vfio_cfg == NULL) {
524 RTE_LOG(ERR, EAL, " invalid group fd!\n");
528 i = get_vfio_group_idx(vfio_group_fd);
529 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
530 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
534 return vfio_cfg->vfio_groups[i].devices;
538 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len)
540 struct rte_memseg_list *msl;
541 struct rte_memseg *ms;
544 msl = rte_mem_virt2memseg_list(addr);
546 /* for IOVA as VA mode, no need to care for IOVA addresses */
547 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
548 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
549 if (type == RTE_MEM_EVENT_ALLOC)
550 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
553 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
558 /* memsegs are contiguous in memory */
559 ms = rte_mem_virt2memseg(addr, msl);
560 while (cur_len < len) {
561 if (type == RTE_MEM_EVENT_ALLOC)
562 vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
563 ms->iova, ms->len, 1);
565 vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
566 ms->iova, ms->len, 0);
574 rte_vfio_clear_group(int vfio_group_fd)
577 struct rte_mp_msg mp_req, *mp_rep;
578 struct rte_mp_reply mp_reply;
579 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
580 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
581 struct vfio_config *vfio_cfg;
583 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
584 if (vfio_cfg == NULL) {
585 RTE_LOG(ERR, EAL, " invalid group fd!\n");
589 if (internal_config.process_type == RTE_PROC_PRIMARY) {
591 i = get_vfio_group_idx(vfio_group_fd);
594 vfio_cfg->vfio_groups[i].group_num = -1;
595 vfio_cfg->vfio_groups[i].fd = -1;
596 vfio_cfg->vfio_groups[i].devices = 0;
597 vfio_cfg->vfio_active_groups--;
601 p->req = SOCKET_CLR_GROUP;
602 p->group_num = vfio_group_fd;
603 strcpy(mp_req.name, EAL_VFIO_MP);
604 mp_req.len_param = sizeof(*p);
607 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
608 mp_reply.nb_received == 1) {
609 mp_rep = &mp_reply.msgs[0];
610 p = (struct vfio_mp_param *)mp_rep->param;
611 if (p->result == SOCKET_OK) {
614 } else if (p->result == SOCKET_NO_FD)
615 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
617 RTE_LOG(ERR, EAL, " no such VFIO group fd!\n");
626 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
627 int *vfio_dev_fd, struct vfio_device_info *device_info)
629 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
630 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
631 struct vfio_group_status group_status = {
632 .argsz = sizeof(group_status)
634 struct vfio_config *vfio_cfg;
635 struct user_mem_maps *user_mem_maps;
636 int vfio_container_fd;
641 /* get group number */
642 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
644 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
649 /* if negative, something failed */
653 /* get the actual group fd */
654 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
655 if (vfio_group_fd < 0)
658 /* if group_fd == 0, that means the device isn't managed by VFIO */
659 if (vfio_group_fd == 0) {
660 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
666 * at this point, we know that this group is viable (meaning, all devices
667 * are either bound to VFIO or not bound to anything)
670 /* check if the group is viable */
671 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
673 RTE_LOG(ERR, EAL, " %s cannot get group status, "
674 "error %i (%s)\n", dev_addr, errno, strerror(errno));
675 close(vfio_group_fd);
676 rte_vfio_clear_group(vfio_group_fd);
678 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
679 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
680 close(vfio_group_fd);
681 rte_vfio_clear_group(vfio_group_fd);
685 /* get the vfio_config it belongs to */
686 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
687 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
688 vfio_container_fd = vfio_cfg->vfio_container_fd;
689 user_mem_maps = &vfio_cfg->mem_maps;
691 /* check if group does not have a container yet */
692 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
694 /* add group to a container */
695 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
698 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
699 "error %i (%s)\n", dev_addr, errno, strerror(errno));
700 close(vfio_group_fd);
701 rte_vfio_clear_group(vfio_group_fd);
706 * pick an IOMMU type and set up DMA mappings for container
708 * needs to be done only once, only when first group is
709 * assigned to a container and only in primary process.
710 * Note this can happen several times with the hotplug
713 if (internal_config.process_type == RTE_PROC_PRIMARY &&
714 vfio_cfg->vfio_active_groups == 1 &&
715 vfio_group_device_count(vfio_group_fd) == 0) {
716 const struct vfio_iommu_type *t;
718 /* select an IOMMU type which we will be using */
719 t = vfio_set_iommu_type(vfio_container_fd);
722 " %s failed to select IOMMU type\n",
724 close(vfio_group_fd);
725 rte_vfio_clear_group(vfio_group_fd);
728 /* lock memory hotplug before mapping and release it
729 * after registering callback, to prevent races
731 rte_rwlock_read_lock(mem_lock);
732 if (vfio_cfg == default_vfio_cfg)
733 ret = t->dma_map_func(vfio_container_fd);
738 " %s DMA remapping failed, error %i (%s)\n",
739 dev_addr, errno, strerror(errno));
740 close(vfio_group_fd);
741 rte_vfio_clear_group(vfio_group_fd);
742 rte_rwlock_read_unlock(mem_lock);
746 vfio_cfg->vfio_iommu_type = t;
748 /* re-map all user-mapped segments */
749 rte_spinlock_recursive_lock(&user_mem_maps->lock);
751 /* this IOMMU type may not support DMA mapping, but
752 * if we have mappings in the list - that means we have
753 * previously mapped something successfully, so we can
754 * be sure that DMA mapping is supported.
756 for (i = 0; i < user_mem_maps->n_maps; i++) {
757 struct user_mem_map *map;
758 map = &user_mem_maps->maps[i];
760 ret = t->dma_user_map_func(
762 map->addr, map->iova, map->len,
765 RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
767 "iova: 0x%" PRIx64 " "
768 "len: 0x%" PRIu64 "\n",
769 map->addr, map->iova,
771 rte_spinlock_recursive_unlock(
772 &user_mem_maps->lock);
773 rte_rwlock_read_unlock(mem_lock);
777 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
779 /* register callback for mem events */
780 if (vfio_cfg == default_vfio_cfg)
781 ret = rte_mem_event_callback_register(
782 VFIO_MEM_EVENT_CLB_NAME,
783 vfio_mem_event_callback);
786 /* unlock memory hotplug */
787 rte_rwlock_read_unlock(mem_lock);
789 if (ret && rte_errno != ENOTSUP) {
790 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
794 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
796 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
800 /* get a file descriptor for the device */
801 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
802 if (*vfio_dev_fd < 0) {
803 /* if we cannot get a device fd, this implies a problem with
804 * the VFIO group or the container not having IOMMU configured.
807 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
809 close(vfio_group_fd);
810 rte_vfio_clear_group(vfio_group_fd);
814 /* test and setup the device */
815 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
817 RTE_LOG(ERR, EAL, " %s cannot get device info, "
818 "error %i (%s)\n", dev_addr, errno,
821 close(vfio_group_fd);
822 rte_vfio_clear_group(vfio_group_fd);
825 vfio_group_device_get(vfio_group_fd);
831 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
834 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
835 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
836 struct vfio_group_status group_status = {
837 .argsz = sizeof(group_status)
839 struct vfio_config *vfio_cfg;
844 /* we don't want any DMA mapping messages to come while we're detaching
845 * VFIO device, because this might be the last device and we might need
846 * to unregister the callback.
848 rte_rwlock_read_lock(mem_lock);
850 /* get group number */
851 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
853 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
855 /* This is an error at this point. */
860 /* get the actual group fd */
861 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
862 if (vfio_group_fd <= 0) {
863 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
869 /* get the vfio_config it belongs to */
870 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
871 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
873 /* At this point we got an active group. Closing it will make the
874 * container detachment. If this is the last active group, VFIO kernel
875 * code will unset the container and the IOMMU mappings.
878 /* Closing a device */
879 if (close(vfio_dev_fd) < 0) {
880 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
886 /* An VFIO group can have several devices attached. Just when there is
887 * no devices remaining should the group be closed.
889 vfio_group_device_put(vfio_group_fd);
890 if (!vfio_group_device_count(vfio_group_fd)) {
892 if (close(vfio_group_fd) < 0) {
893 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
899 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
900 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
907 /* if there are no active device groups, unregister the callback to
908 * avoid spurious attempts to map/unmap memory from VFIO.
910 if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0)
911 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME);
917 rte_rwlock_read_unlock(mem_lock);
922 rte_vfio_enable(const char *modname)
924 /* initialize group list */
928 rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
930 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
931 vfio_cfgs[i].vfio_container_fd = -1;
932 vfio_cfgs[i].vfio_active_groups = 0;
933 vfio_cfgs[i].vfio_iommu_type = NULL;
934 vfio_cfgs[i].mem_maps.lock = lock;
936 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
937 vfio_cfgs[i].vfio_groups[j].fd = -1;
938 vfio_cfgs[i].vfio_groups[j].group_num = -1;
939 vfio_cfgs[i].vfio_groups[j].devices = 0;
943 /* inform the user that we are probing for VFIO */
944 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
946 /* check if vfio module is loaded */
947 vfio_available = rte_eal_check_module(modname);
949 /* return error directly */
950 if (vfio_available == -1) {
951 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
955 /* return 0 if VFIO modules not loaded */
956 if (vfio_available == 0) {
957 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
958 "skipping VFIO support...\n");
962 default_vfio_cfg->vfio_container_fd = rte_vfio_get_container_fd();
964 /* check if we have VFIO driver enabled */
965 if (default_vfio_cfg->vfio_container_fd != -1) {
966 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
967 default_vfio_cfg->vfio_enabled = 1;
969 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
976 rte_vfio_is_enabled(const char *modname)
978 const int mod_available = rte_eal_check_module(modname) > 0;
979 return default_vfio_cfg->vfio_enabled && mod_available;
982 const struct vfio_iommu_type *
983 vfio_set_iommu_type(int vfio_container_fd)
986 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
987 const struct vfio_iommu_type *t = &iommu_types[idx];
989 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
992 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
993 t->type_id, t->name);
996 /* not an error, there may be more supported IOMMU types */
997 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
998 "error %i (%s)\n", t->type_id, t->name, errno,
1001 /* if we didn't find a suitable IOMMU type, fail */
1006 vfio_has_supported_extensions(int vfio_container_fd)
1009 unsigned idx, n_extensions = 0;
1010 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1011 const struct vfio_iommu_type *t = &iommu_types[idx];
1013 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1016 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
1017 "error %i (%s)\n", errno,
1019 close(vfio_container_fd);
1021 } else if (ret == 1) {
1022 /* we found a supported extension */
1025 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
1026 t->type_id, t->name,
1027 ret ? "supported" : "not supported");
1030 /* if we didn't find any supported IOMMU types, fail */
1031 if (!n_extensions) {
1032 close(vfio_container_fd);
1040 rte_vfio_get_container_fd(void)
1042 int ret, vfio_container_fd;
1043 struct rte_mp_msg mp_req, *mp_rep;
1044 struct rte_mp_reply mp_reply;
1045 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1046 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1049 /* if we're in a primary process, try to open the container */
1050 if (internal_config.process_type == RTE_PROC_PRIMARY) {
1051 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1052 if (vfio_container_fd < 0) {
1053 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
1054 "error %i (%s)\n", errno, strerror(errno));
1058 /* check VFIO API version */
1059 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1060 if (ret != VFIO_API_VERSION) {
1062 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
1063 "error %i (%s)\n", errno, strerror(errno));
1065 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
1066 close(vfio_container_fd);
1070 ret = vfio_has_supported_extensions(vfio_container_fd);
1072 RTE_LOG(ERR, EAL, " no supported IOMMU "
1073 "extensions found!\n");
1077 return vfio_container_fd;
1080 * if we're in a secondary process, request container fd from the
1081 * primary process via mp channel
1083 p->req = SOCKET_REQ_CONTAINER;
1084 strcpy(mp_req.name, EAL_VFIO_MP);
1085 mp_req.len_param = sizeof(*p);
1088 vfio_container_fd = -1;
1089 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1090 mp_reply.nb_received == 1) {
1091 mp_rep = &mp_reply.msgs[0];
1092 p = (struct vfio_mp_param *)mp_rep->param;
1093 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1094 free(mp_reply.msgs);
1095 return mp_rep->fds[0];
1097 free(mp_reply.msgs);
1100 RTE_LOG(ERR, EAL, " cannot request container fd\n");
1105 rte_vfio_get_group_num(const char *sysfs_base,
1106 const char *dev_addr, int *iommu_group_num)
1108 char linkname[PATH_MAX];
1109 char filename[PATH_MAX];
1110 char *tok[16], *group_tok, *end;
1113 memset(linkname, 0, sizeof(linkname));
1114 memset(filename, 0, sizeof(filename));
1116 /* try to find out IOMMU group for this device */
1117 snprintf(linkname, sizeof(linkname),
1118 "%s/%s/iommu_group", sysfs_base, dev_addr);
1120 ret = readlink(linkname, filename, sizeof(filename));
1122 /* if the link doesn't exist, no VFIO for us */
1126 ret = rte_strsplit(filename, sizeof(filename),
1127 tok, RTE_DIM(tok), '/');
1130 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
1134 /* IOMMU group is always the last token */
1136 group_tok = tok[ret - 1];
1138 *iommu_group_num = strtol(group_tok, &end, 10);
1139 if ((end != group_tok && *end != '\0') || errno != 0) {
1140 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
1148 type1_map(const struct rte_memseg_list *msl __rte_unused,
1149 const struct rte_memseg *ms, void *arg)
1151 int *vfio_container_fd = arg;
1153 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1158 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1159 uint64_t len, int do_map)
1161 struct vfio_iommu_type1_dma_map dma_map;
1162 struct vfio_iommu_type1_dma_unmap dma_unmap;
1166 memset(&dma_map, 0, sizeof(dma_map));
1167 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1168 dma_map.vaddr = vaddr;
1170 dma_map.iova = iova;
1171 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1172 VFIO_DMA_MAP_FLAG_WRITE;
1174 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1176 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, 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_type1_dma_map(int vfio_container_fd)
1201 return rte_memseg_walk(type1_map, &vfio_container_fd);
1205 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1206 uint64_t len, int do_map)
1208 struct vfio_iommu_type1_dma_map dma_map;
1209 struct vfio_iommu_type1_dma_unmap dma_unmap;
1213 memset(&dma_map, 0, sizeof(dma_map));
1214 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1215 dma_map.vaddr = vaddr;
1217 dma_map.iova = iova;
1218 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1219 VFIO_DMA_MAP_FLAG_WRITE;
1221 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1223 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1224 errno, strerror(errno));
1229 struct vfio_iommu_spapr_register_memory reg = {
1230 .argsz = sizeof(reg),
1233 reg.vaddr = (uintptr_t) vaddr;
1236 ret = ioctl(vfio_container_fd,
1237 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
1239 RTE_LOG(ERR, EAL, " cannot unregister vaddr for IOMMU, error %i (%s)\n",
1240 errno, strerror(errno));
1244 memset(&dma_unmap, 0, sizeof(dma_unmap));
1245 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1246 dma_unmap.size = len;
1247 dma_unmap.iova = iova;
1249 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1252 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1253 errno, strerror(errno));
1262 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1263 const struct rte_memseg *ms, void *arg)
1265 int *vfio_container_fd = arg;
1267 return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1271 struct spapr_walk_param {
1272 uint64_t window_size;
1273 uint64_t hugepage_sz;
1276 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1277 const struct rte_memseg *ms, void *arg)
1279 struct spapr_walk_param *param = arg;
1280 uint64_t max = ms->iova + ms->len;
1282 if (max > param->window_size) {
1283 param->hugepage_sz = ms->hugepage_sz;
1284 param->window_size = max;
1291 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1292 struct vfio_iommu_spapr_tce_create *create) {
1293 struct vfio_iommu_spapr_tce_remove remove = {
1294 .argsz = sizeof(remove),
1296 struct vfio_iommu_spapr_tce_info info = {
1297 .argsz = sizeof(info),
1301 /* query spapr iommu info */
1302 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1304 RTE_LOG(ERR, EAL, " cannot get iommu info, "
1305 "error %i (%s)\n", errno, strerror(errno));
1309 /* remove default DMA of 32 bit window */
1310 remove.start_addr = info.dma32_window_start;
1311 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1313 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
1314 "error %i (%s)\n", errno, strerror(errno));
1318 /* create new DMA window */
1319 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1321 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
1322 "error %i (%s)\n", errno, strerror(errno));
1326 if (create->start_addr != 0) {
1327 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
1335 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1336 uint64_t len, int do_map)
1338 struct spapr_walk_param param;
1339 struct vfio_iommu_spapr_tce_create create = {
1340 .argsz = sizeof(create),
1342 struct vfio_config *vfio_cfg;
1343 struct user_mem_maps *user_mem_maps;
1346 vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1347 if (vfio_cfg == NULL) {
1348 RTE_LOG(ERR, EAL, " invalid container fd!\n");
1352 user_mem_maps = &vfio_cfg->mem_maps;
1353 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1355 /* check if window size needs to be adjusted */
1356 memset(¶m, 0, sizeof(param));
1358 if (memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1360 RTE_LOG(ERR, EAL, "Could not get window size\n");
1365 /* also check user maps */
1366 for (i = 0; i < user_mem_maps->n_maps; i++) {
1367 uint64_t max = user_mem_maps->maps[i].iova +
1368 user_mem_maps->maps[i].len;
1369 create.window_size = RTE_MAX(create.window_size, max);
1372 /* sPAPR requires window size to be a power of 2 */
1373 create.window_size = rte_align64pow2(param.window_size);
1374 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1379 /* re-create window and remap the entire memory */
1380 if (iova > create.window_size) {
1381 if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1383 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1387 if (memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1388 &vfio_container_fd) < 0) {
1389 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1393 /* remap all user maps */
1394 for (i = 0; i < user_mem_maps->n_maps; i++) {
1395 struct user_mem_map *map =
1396 &user_mem_maps->maps[i];
1397 if (vfio_spapr_dma_do_map(vfio_container_fd,
1398 map->addr, map->iova, map->len,
1400 RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1407 /* now that we've remapped all of the memory that was present
1408 * before, map the segment that we were requested to map.
1410 * however, if we were called by the callback, the memory we
1411 * were called with was already in the memseg list, so previous
1412 * mapping should've mapped that segment already.
1414 * virt2memseg_list is a relatively cheap check, so use that. if
1415 * memory is within any memseg list, it's a memseg, so it's
1418 addr = (void *)(uintptr_t)vaddr;
1419 if (rte_mem_virt2memseg_list(addr) == NULL &&
1420 vfio_spapr_dma_do_map(vfio_container_fd,
1421 vaddr, iova, len, 1) < 0) {
1422 RTE_LOG(ERR, EAL, "Could not map segment\n");
1427 /* for unmap, check if iova within DMA window */
1428 if (iova > create.window_size) {
1429 RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1434 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1437 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1442 vfio_spapr_dma_map(int vfio_container_fd)
1444 struct vfio_iommu_spapr_tce_create create = {
1445 .argsz = sizeof(create),
1447 struct spapr_walk_param param;
1449 memset(¶m, 0, sizeof(param));
1451 /* create DMA window from 0 to max(phys_addr + len) */
1452 rte_memseg_walk(vfio_spapr_window_size_walk, ¶m);
1454 /* sPAPR requires window size to be a power of 2 */
1455 create.window_size = rte_align64pow2(param.window_size);
1456 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1459 if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1460 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1464 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1465 if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1472 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1474 /* No-IOMMU mode does not need DMA mapping */
1479 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1480 uint64_t __rte_unused vaddr,
1481 uint64_t __rte_unused iova, uint64_t __rte_unused len,
1482 int __rte_unused do_map)
1484 /* No-IOMMU mode does not need DMA mapping */
1489 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1490 uint64_t len, int do_map)
1492 const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1495 RTE_LOG(ERR, EAL, " VFIO support not initialized\n");
1500 if (!t->dma_user_map_func) {
1502 " VFIO custom DMA region maping not supported by IOMMU %s\n",
1504 rte_errno = ENOTSUP;
1508 return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1513 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1516 struct user_mem_map *new_map;
1517 struct user_mem_maps *user_mem_maps;
1520 user_mem_maps = &vfio_cfg->mem_maps;
1521 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1522 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1523 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1529 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1530 /* technically, this will fail if there are currently no devices
1531 * plugged in, even if a device were added later, this mapping
1532 * might have succeeded. however, since we cannot verify if this
1533 * is a valid mapping without having a device attached, consider
1534 * this to be unsupported, because we can't just store any old
1535 * mapping and pollute list of active mappings willy-nilly.
1537 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1541 /* create new user mem map entry */
1542 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1543 new_map->addr = vaddr;
1544 new_map->iova = iova;
1547 compact_user_maps(user_mem_maps);
1549 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1554 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1557 struct user_mem_map *map, *new_map = NULL;
1558 struct user_mem_maps *user_mem_maps;
1561 user_mem_maps = &vfio_cfg->mem_maps;
1562 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1564 /* find our mapping */
1565 map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1567 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1572 if (map->addr != vaddr || map->iova != iova || map->len != len) {
1573 /* we're partially unmapping a previously mapped region, so we
1574 * need to split entry into two.
1576 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1577 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1582 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1585 /* unmap the entry */
1586 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1587 /* there may not be any devices plugged in, so unmapping will
1588 * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1589 * stop us from removing the mapping, as the assumption is we
1590 * won't be needing this memory any more and thus will want to
1591 * prevent it from being remapped again on hotplug. so, only
1592 * fail if we indeed failed to unmap (e.g. if the mapping was
1593 * within our mapped range but had invalid alignment).
1595 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1596 RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1600 RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1603 /* remove map from the list of active mappings */
1604 if (new_map != NULL) {
1605 adjust_map(map, new_map, vaddr, len);
1607 /* if we've created a new map by splitting, sort everything */
1608 if (!is_null_map(new_map)) {
1609 compact_user_maps(user_mem_maps);
1611 /* we've created a new mapping, but it was unused */
1612 user_mem_maps->n_maps--;
1615 memset(map, 0, sizeof(*map));
1616 compact_user_maps(user_mem_maps);
1617 user_mem_maps->n_maps--;
1621 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1625 int __rte_experimental
1626 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1633 return container_dma_map(default_vfio_cfg, vaddr, iova, len);
1636 int __rte_experimental
1637 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1644 return container_dma_unmap(default_vfio_cfg, vaddr, iova, len);
1648 rte_vfio_noiommu_is_enabled(void)
1654 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1656 if (errno != ENOENT) {
1657 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
1658 errno, strerror(errno));
1662 * else the file does not exists
1663 * i.e. noiommu is not enabled
1668 cnt = read(fd, &c, 1);
1671 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
1672 "file %i (%s)\n", errno, strerror(errno));
1679 int __rte_experimental
1680 rte_vfio_container_create(void)
1684 /* Find an empty slot to store new vfio config */
1685 for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1686 if (vfio_cfgs[i].vfio_container_fd == -1)
1690 if (i == VFIO_MAX_CONTAINERS) {
1691 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1695 vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1696 if (vfio_cfgs[i].vfio_container_fd < 0) {
1697 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1701 return vfio_cfgs[i].vfio_container_fd;
1704 int __rte_experimental
1705 rte_vfio_container_destroy(int container_fd)
1707 struct vfio_config *vfio_cfg;
1710 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1711 if (vfio_cfg == NULL) {
1712 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1716 for (i = 0; i < VFIO_MAX_GROUPS; i++)
1717 if (vfio_cfg->vfio_groups[i].group_num != -1)
1718 rte_vfio_container_group_unbind(container_fd,
1719 vfio_cfg->vfio_groups[i].group_num);
1721 close(container_fd);
1722 vfio_cfg->vfio_container_fd = -1;
1723 vfio_cfg->vfio_active_groups = 0;
1724 vfio_cfg->vfio_iommu_type = NULL;
1729 int __rte_experimental
1730 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1732 struct vfio_config *vfio_cfg;
1733 struct vfio_group *cur_grp;
1737 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1738 if (vfio_cfg == NULL) {
1739 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1743 /* Check room for new group */
1744 if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
1745 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
1749 /* Get an index for the new group */
1750 for (i = 0; i < VFIO_MAX_GROUPS; i++)
1751 if (vfio_cfg->vfio_groups[i].group_num == -1) {
1752 cur_grp = &vfio_cfg->vfio_groups[i];
1756 /* This should not happen */
1757 if (i == VFIO_MAX_GROUPS) {
1758 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
1762 vfio_group_fd = vfio_open_group_fd(iommu_group_num);
1763 if (vfio_group_fd < 0) {
1764 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
1767 cur_grp->group_num = iommu_group_num;
1768 cur_grp->fd = vfio_group_fd;
1769 cur_grp->devices = 0;
1770 vfio_cfg->vfio_active_groups++;
1772 return vfio_group_fd;
1775 int __rte_experimental
1776 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
1778 struct vfio_config *vfio_cfg;
1779 struct vfio_group *cur_grp;
1782 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1783 if (vfio_cfg == NULL) {
1784 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1788 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
1789 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
1790 cur_grp = &vfio_cfg->vfio_groups[i];
1795 /* This should not happen */
1796 if (i == VFIO_MAX_GROUPS) {
1797 RTE_LOG(ERR, EAL, "Specified group number not found\n");
1801 if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
1802 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
1803 " iommu_group_num %d\n", iommu_group_num);
1806 cur_grp->group_num = -1;
1808 cur_grp->devices = 0;
1809 vfio_cfg->vfio_active_groups--;
1814 int __rte_experimental
1815 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
1818 struct vfio_config *vfio_cfg;
1825 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1826 if (vfio_cfg == NULL) {
1827 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1831 return container_dma_map(vfio_cfg, vaddr, iova, len);
1834 int __rte_experimental
1835 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
1838 struct vfio_config *vfio_cfg;
1845 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1846 if (vfio_cfg == NULL) {
1847 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1851 return container_dma_unmap(vfio_cfg, vaddr, iova, len);
1856 int __rte_experimental
1857 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1858 __rte_unused uint64_t len)
1863 int __rte_experimental
1864 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1865 __rte_unused uint64_t len)
1871 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
1872 __rte_unused const char *dev_addr,
1873 __rte_unused int *vfio_dev_fd,
1874 __rte_unused struct vfio_device_info *device_info)
1880 rte_vfio_release_device(__rte_unused const char *sysfs_base,
1881 __rte_unused const char *dev_addr, __rte_unused int fd)
1887 rte_vfio_enable(__rte_unused const char *modname)
1893 rte_vfio_is_enabled(__rte_unused const char *modname)
1899 rte_vfio_noiommu_is_enabled(void)
1905 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
1910 int __rte_experimental
1911 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
1912 __rte_unused const char *dev_addr,
1913 __rte_unused int *iommu_group_num)
1918 int __rte_experimental
1919 rte_vfio_get_container_fd(void)
1924 int __rte_experimental
1925 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
1930 int __rte_experimental
1931 rte_vfio_container_create(void)
1936 int __rte_experimental
1937 rte_vfio_container_destroy(__rte_unused int container_fd)
1942 int __rte_experimental
1943 rte_vfio_container_group_bind(__rte_unused int container_fd,
1944 __rte_unused int iommu_group_num)
1949 int __rte_experimental
1950 rte_vfio_container_group_unbind(__rte_unused int container_fd,
1951 __rte_unused int iommu_group_num)
1956 int __rte_experimental
1957 rte_vfio_container_dma_map(__rte_unused int container_fd,
1958 __rte_unused uint64_t vaddr,
1959 __rte_unused uint64_t iova,
1960 __rte_unused uint64_t len)
1965 int __rte_experimental
1966 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
1967 __rte_unused uint64_t vaddr,
1968 __rte_unused uint64_t iova,
1969 __rte_unused uint64_t len)
1974 #endif /* VFIO_PRESENT */