1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 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 /* per-process VFIO config */
26 static struct vfio_config vfio_cfg;
28 static int vfio_type1_dma_map(int);
29 static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
30 static int vfio_spapr_dma_map(int);
31 static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
32 static int vfio_noiommu_dma_map(int);
33 static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
34 static int vfio_dma_mem_map(uint64_t vaddr, uint64_t iova, uint64_t len,
37 /* IOMMU types we support */
38 static const struct vfio_iommu_type iommu_types[] = {
39 /* x86 IOMMU, otherwise known as type 1 */
41 .type_id = RTE_VFIO_TYPE1,
43 .dma_map_func = &vfio_type1_dma_map,
44 .dma_user_map_func = &vfio_type1_dma_mem_map
46 /* ppc64 IOMMU, otherwise known as spapr */
48 .type_id = RTE_VFIO_SPAPR,
50 .dma_map_func = &vfio_spapr_dma_map,
51 .dma_user_map_func = &vfio_spapr_dma_mem_map
55 .type_id = RTE_VFIO_NOIOMMU,
57 .dma_map_func = &vfio_noiommu_dma_map,
58 .dma_user_map_func = &vfio_noiommu_dma_mem_map
62 /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
63 * recreate the mappings for DPDK segments, but we cannot do so for memory that
64 * was registered by the user themselves, so we need to store the user mappings
65 * somewhere, to recreate them later.
67 #define VFIO_MAX_USER_MEM_MAPS 256
74 rte_spinlock_recursive_t lock;
76 struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
78 .lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER
81 /* for sPAPR IOMMU, we will need to walk memseg list, but we cannot use
82 * rte_memseg_walk() because by the time we enter callback we will be holding a
83 * write lock, so regular rte-memseg_walk will deadlock. copying the same
84 * iteration code everywhere is not ideal as well. so, use a lockless copy of
88 memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
90 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
91 int i, ms_idx, ret = 0;
93 for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
94 struct rte_memseg_list *msl = &mcfg->memsegs[i];
95 const struct rte_memseg *ms;
96 struct rte_fbarray *arr;
98 if (msl->memseg_arr.count == 0)
101 arr = &msl->memseg_arr;
103 ms_idx = rte_fbarray_find_next_used(arr, 0);
104 while (ms_idx >= 0) {
105 ms = rte_fbarray_get(arr, ms_idx);
106 ret = func(msl, ms, arg);
111 ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
118 is_null_map(const struct user_mem_map *map)
120 return map->addr == 0 && map->iova == 0 && map->len == 0;
123 /* we may need to merge user mem maps together in case of user mapping/unmapping
124 * chunks of memory, so we'll need a comparator function to sort segments.
127 user_mem_map_cmp(const void *a, const void *b)
129 const struct user_mem_map *umm_a = a;
130 const struct user_mem_map *umm_b = b;
132 /* move null entries to end */
133 if (is_null_map(umm_a))
135 if (is_null_map(umm_b))
138 /* sort by iova first */
139 if (umm_a->iova < umm_b->iova)
141 if (umm_a->iova > umm_b->iova)
144 if (umm_a->addr < umm_b->addr)
146 if (umm_a->addr > umm_b->addr)
149 if (umm_a->len < umm_b->len)
151 if (umm_a->len > umm_b->len)
157 /* adjust user map entry. this may result in shortening of existing map, or in
158 * splitting existing map in two pieces.
161 adjust_map(struct user_mem_map *src, struct user_mem_map *end,
162 uint64_t remove_va_start, uint64_t remove_len)
164 /* if va start is same as start address, we're simply moving start */
165 if (remove_va_start == src->addr) {
166 src->addr += remove_len;
167 src->iova += remove_len;
168 src->len -= remove_len;
169 } else if (remove_va_start + remove_len == src->addr + src->len) {
170 /* we're shrinking mapping from the end */
171 src->len -= remove_len;
173 /* we're blowing a hole in the middle */
174 struct user_mem_map tmp;
175 uint64_t total_len = src->len;
177 /* adjust source segment length */
178 src->len = remove_va_start - src->addr;
180 /* create temporary segment in the middle */
181 tmp.addr = src->addr + src->len;
182 tmp.iova = src->iova + src->len;
183 tmp.len = remove_len;
185 /* populate end segment - this one we will be keeping */
186 end->addr = tmp.addr + tmp.len;
187 end->iova = tmp.iova + tmp.len;
188 end->len = total_len - src->len - tmp.len;
192 /* try merging two maps into one, return 1 if succeeded */
194 merge_map(struct user_mem_map *left, struct user_mem_map *right)
196 if (left->addr + left->len != right->addr)
198 if (left->iova + left->len != right->iova)
201 left->len += right->len;
203 memset(right, 0, sizeof(*right));
208 static struct user_mem_map *
209 find_user_mem_map(uint64_t addr, uint64_t iova, uint64_t len)
211 uint64_t va_end = addr + len;
212 uint64_t iova_end = iova + len;
215 for (i = 0; i < user_mem_maps.n_maps; i++) {
216 struct user_mem_map *map = &user_mem_maps.maps[i];
217 uint64_t map_va_end = map->addr + map->len;
218 uint64_t map_iova_end = map->iova + map->len;
221 if (addr < map->addr || addr >= map_va_end)
223 /* check if IOVA end is within boundaries */
224 if (va_end <= map->addr || va_end >= map_va_end)
228 if (iova < map->iova || iova >= map_iova_end)
230 /* check if IOVA end is within boundaries */
231 if (iova_end <= map->iova || iova_end >= map_iova_end)
234 /* we've found our map */
240 /* this will sort all user maps, and merge/compact any adjacent maps */
242 compact_user_maps(void)
244 int i, n_merged, cur_idx;
246 qsort(user_mem_maps.maps, user_mem_maps.n_maps,
247 sizeof(user_mem_maps.maps[0]), user_mem_map_cmp);
249 /* we'll go over the list backwards when merging */
251 for (i = user_mem_maps.n_maps - 2; i >= 0; i--) {
252 struct user_mem_map *l, *r;
254 l = &user_mem_maps.maps[i];
255 r = &user_mem_maps.maps[i + 1];
257 if (is_null_map(l) || is_null_map(r))
264 /* the entries are still sorted, but now they have holes in them, so
265 * walk through the list and remove the holes
269 for (i = 0; i < user_mem_maps.n_maps; i++) {
270 if (!is_null_map(&user_mem_maps.maps[i])) {
271 struct user_mem_map *src, *dst;
273 src = &user_mem_maps.maps[i];
274 dst = &user_mem_maps.maps[cur_idx++];
277 memcpy(dst, src, sizeof(*src));
278 memset(src, 0, sizeof(*src));
282 user_mem_maps.n_maps = cur_idx;
287 rte_vfio_get_group_fd(int iommu_group_num)
291 char filename[PATH_MAX];
292 struct vfio_group *cur_grp;
294 /* check if we already have the group descriptor open */
295 for (i = 0; i < VFIO_MAX_GROUPS; i++)
296 if (vfio_cfg.vfio_groups[i].group_num == iommu_group_num)
297 return vfio_cfg.vfio_groups[i].fd;
299 /* Lets see first if there is room for a new group */
300 if (vfio_cfg.vfio_active_groups == VFIO_MAX_GROUPS) {
301 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
305 /* Now lets get an index for the new group */
306 for (i = 0; i < VFIO_MAX_GROUPS; i++)
307 if (vfio_cfg.vfio_groups[i].group_num == -1) {
308 cur_grp = &vfio_cfg.vfio_groups[i];
312 /* This should not happen */
313 if (i == VFIO_MAX_GROUPS) {
314 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
317 /* if primary, try to open the group */
318 if (internal_config.process_type == RTE_PROC_PRIMARY) {
319 /* try regular group format */
320 snprintf(filename, sizeof(filename),
321 VFIO_GROUP_FMT, iommu_group_num);
322 vfio_group_fd = open(filename, O_RDWR);
323 if (vfio_group_fd < 0) {
324 /* if file not found, it's not an error */
325 if (errno != ENOENT) {
326 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
331 /* special case: try no-IOMMU path as well */
332 snprintf(filename, sizeof(filename),
333 VFIO_NOIOMMU_GROUP_FMT,
335 vfio_group_fd = open(filename, O_RDWR);
336 if (vfio_group_fd < 0) {
337 if (errno != ENOENT) {
338 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
344 /* noiommu group found */
347 cur_grp->group_num = iommu_group_num;
348 cur_grp->fd = vfio_group_fd;
349 vfio_cfg.vfio_active_groups++;
350 return vfio_group_fd;
352 /* if we're in a secondary process, request group fd from the primary
353 * process via our socket
358 socket_fd = vfio_mp_sync_connect_to_primary();
361 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
364 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
365 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
369 if (vfio_mp_sync_send_request(socket_fd, iommu_group_num) < 0) {
370 RTE_LOG(ERR, EAL, " cannot send group number!\n");
374 ret = vfio_mp_sync_receive_request(socket_fd);
380 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
381 /* if we got the fd, store it and return it */
382 if (vfio_group_fd > 0) {
384 cur_grp->group_num = iommu_group_num;
385 cur_grp->fd = vfio_group_fd;
386 vfio_cfg.vfio_active_groups++;
387 return vfio_group_fd;
389 /* fall-through on error */
391 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
401 get_vfio_group_idx(int vfio_group_fd)
404 for (i = 0; i < VFIO_MAX_GROUPS; i++)
405 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
411 vfio_group_device_get(int vfio_group_fd)
415 i = get_vfio_group_idx(vfio_group_fd);
416 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
417 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
419 vfio_cfg.vfio_groups[i].devices++;
423 vfio_group_device_put(int vfio_group_fd)
427 i = get_vfio_group_idx(vfio_group_fd);
428 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
429 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
431 vfio_cfg.vfio_groups[i].devices--;
435 vfio_group_device_count(int vfio_group_fd)
439 i = get_vfio_group_idx(vfio_group_fd);
440 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
441 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
445 return vfio_cfg.vfio_groups[i].devices;
449 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len)
451 struct rte_memseg_list *msl;
452 struct rte_memseg *ms;
455 msl = rte_mem_virt2memseg_list(addr);
457 /* for IOVA as VA mode, no need to care for IOVA addresses */
458 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
459 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
460 if (type == RTE_MEM_EVENT_ALLOC)
461 vfio_dma_mem_map(vfio_va, vfio_va, len, 1);
463 vfio_dma_mem_map(vfio_va, vfio_va, len, 0);
467 /* memsegs are contiguous in memory */
468 ms = rte_mem_virt2memseg(addr, msl);
469 while (cur_len < len) {
470 if (type == RTE_MEM_EVENT_ALLOC)
471 vfio_dma_mem_map(ms->addr_64, ms->iova, ms->len, 1);
473 vfio_dma_mem_map(ms->addr_64, ms->iova, ms->len, 0);
481 rte_vfio_clear_group(int vfio_group_fd)
486 if (internal_config.process_type == RTE_PROC_PRIMARY) {
488 i = get_vfio_group_idx(vfio_group_fd);
491 vfio_cfg.vfio_groups[i].group_num = -1;
492 vfio_cfg.vfio_groups[i].fd = -1;
493 vfio_cfg.vfio_groups[i].devices = 0;
494 vfio_cfg.vfio_active_groups--;
498 /* This is just for SECONDARY processes */
499 socket_fd = vfio_mp_sync_connect_to_primary();
502 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
506 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
507 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
512 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
513 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
518 ret = vfio_mp_sync_receive_request(socket_fd);
521 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
528 RTE_LOG(ERR, EAL, " Socket error\n");
532 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
539 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
540 int *vfio_dev_fd, struct vfio_device_info *device_info)
542 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
543 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
544 struct vfio_group_status group_status = {
545 .argsz = sizeof(group_status)
551 /* get group number */
552 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
554 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
559 /* if negative, something failed */
563 /* get the actual group fd */
564 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
565 if (vfio_group_fd < 0)
568 /* if group_fd == 0, that means the device isn't managed by VFIO */
569 if (vfio_group_fd == 0) {
570 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
576 * at this point, we know that this group is viable (meaning, all devices
577 * are either bound to VFIO or not bound to anything)
580 /* check if the group is viable */
581 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
583 RTE_LOG(ERR, EAL, " %s cannot get group status, "
584 "error %i (%s)\n", dev_addr, errno, strerror(errno));
585 close(vfio_group_fd);
586 rte_vfio_clear_group(vfio_group_fd);
588 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
589 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
590 close(vfio_group_fd);
591 rte_vfio_clear_group(vfio_group_fd);
595 /* check if group does not have a container yet */
596 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
598 /* add group to a container */
599 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
600 &vfio_cfg.vfio_container_fd);
602 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
603 "error %i (%s)\n", dev_addr, errno, strerror(errno));
604 close(vfio_group_fd);
605 rte_vfio_clear_group(vfio_group_fd);
610 * pick an IOMMU type and set up DMA mappings for container
612 * needs to be done only once, only when first group is
613 * assigned to a container and only in primary process.
614 * Note this can happen several times with the hotplug
617 if (internal_config.process_type == RTE_PROC_PRIMARY &&
618 vfio_cfg.vfio_active_groups == 1) {
619 const struct vfio_iommu_type *t;
621 /* select an IOMMU type which we will be using */
622 t = vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
625 " %s failed to select IOMMU type\n",
627 close(vfio_group_fd);
628 rte_vfio_clear_group(vfio_group_fd);
631 /* lock memory hotplug before mapping and release it
632 * after registering callback, to prevent races
634 rte_rwlock_read_lock(mem_lock);
635 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
638 " %s DMA remapping failed, error %i (%s)\n",
639 dev_addr, errno, strerror(errno));
640 close(vfio_group_fd);
641 rte_vfio_clear_group(vfio_group_fd);
642 rte_rwlock_read_unlock(mem_lock);
646 vfio_cfg.vfio_iommu_type = t;
648 /* re-map all user-mapped segments */
649 rte_spinlock_recursive_lock(&user_mem_maps.lock);
651 /* this IOMMU type may not support DMA mapping, but
652 * if we have mappings in the list - that means we have
653 * previously mapped something successfully, so we can
654 * be sure that DMA mapping is supported.
656 for (i = 0; i < user_mem_maps.n_maps; i++) {
657 struct user_mem_map *map;
658 map = &user_mem_maps.maps[i];
660 ret = t->dma_user_map_func(
661 vfio_cfg.vfio_container_fd,
662 map->addr, map->iova, map->len,
665 RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
667 "iova: 0x%" PRIx64 " "
668 "len: 0x%" PRIu64 "\n",
669 map->addr, map->iova,
671 rte_spinlock_recursive_unlock(
672 &user_mem_maps.lock);
673 rte_rwlock_read_unlock(mem_lock);
677 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
679 /* register callback for mem events */
680 ret = rte_mem_event_callback_register(
681 VFIO_MEM_EVENT_CLB_NAME,
682 vfio_mem_event_callback);
683 /* unlock memory hotplug */
684 rte_rwlock_read_unlock(mem_lock);
686 if (ret && rte_errno != ENOTSUP) {
687 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
691 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
693 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
697 /* get a file descriptor for the device */
698 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
699 if (*vfio_dev_fd < 0) {
700 /* if we cannot get a device fd, this implies a problem with
701 * the VFIO group or the container not having IOMMU configured.
704 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
706 close(vfio_group_fd);
707 rte_vfio_clear_group(vfio_group_fd);
711 /* test and setup the device */
712 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
714 RTE_LOG(ERR, EAL, " %s cannot get device info, "
715 "error %i (%s)\n", dev_addr, errno,
718 close(vfio_group_fd);
719 rte_vfio_clear_group(vfio_group_fd);
722 vfio_group_device_get(vfio_group_fd);
728 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
731 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
732 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
733 struct vfio_group_status group_status = {
734 .argsz = sizeof(group_status)
740 /* we don't want any DMA mapping messages to come while we're detaching
741 * VFIO device, because this might be the last device and we might need
742 * to unregister the callback.
744 rte_rwlock_read_lock(mem_lock);
746 /* get group number */
747 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
749 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
751 /* This is an error at this point. */
756 /* get the actual group fd */
757 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
758 if (vfio_group_fd <= 0) {
759 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
765 /* At this point we got an active group. Closing it will make the
766 * container detachment. If this is the last active group, VFIO kernel
767 * code will unset the container and the IOMMU mappings.
770 /* Closing a device */
771 if (close(vfio_dev_fd) < 0) {
772 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
778 /* An VFIO group can have several devices attached. Just when there is
779 * no devices remaining should the group be closed.
781 vfio_group_device_put(vfio_group_fd);
782 if (!vfio_group_device_count(vfio_group_fd)) {
784 if (close(vfio_group_fd) < 0) {
785 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
791 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
792 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
799 /* if there are no active device groups, unregister the callback to
800 * avoid spurious attempts to map/unmap memory from VFIO.
802 if (vfio_cfg.vfio_active_groups == 0)
803 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME);
809 rte_rwlock_read_unlock(mem_lock);
814 rte_vfio_enable(const char *modname)
816 /* initialize group list */
820 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
821 vfio_cfg.vfio_groups[i].fd = -1;
822 vfio_cfg.vfio_groups[i].group_num = -1;
823 vfio_cfg.vfio_groups[i].devices = 0;
826 /* inform the user that we are probing for VFIO */
827 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
829 /* check if vfio module is loaded */
830 vfio_available = rte_eal_check_module(modname);
832 /* return error directly */
833 if (vfio_available == -1) {
834 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
838 /* return 0 if VFIO modules not loaded */
839 if (vfio_available == 0) {
840 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
841 "skipping VFIO support...\n");
845 vfio_cfg.vfio_container_fd = rte_vfio_get_container_fd();
847 /* check if we have VFIO driver enabled */
848 if (vfio_cfg.vfio_container_fd != -1) {
849 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
850 vfio_cfg.vfio_enabled = 1;
852 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
859 rte_vfio_is_enabled(const char *modname)
861 const int mod_available = rte_eal_check_module(modname) > 0;
862 return vfio_cfg.vfio_enabled && mod_available;
865 const struct vfio_iommu_type *
866 vfio_set_iommu_type(int vfio_container_fd)
869 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
870 const struct vfio_iommu_type *t = &iommu_types[idx];
872 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
875 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
876 t->type_id, t->name);
879 /* not an error, there may be more supported IOMMU types */
880 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
881 "error %i (%s)\n", t->type_id, t->name, errno,
884 /* if we didn't find a suitable IOMMU type, fail */
889 vfio_has_supported_extensions(int vfio_container_fd)
892 unsigned idx, n_extensions = 0;
893 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
894 const struct vfio_iommu_type *t = &iommu_types[idx];
896 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
899 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
900 "error %i (%s)\n", errno,
902 close(vfio_container_fd);
904 } else if (ret == 1) {
905 /* we found a supported extension */
908 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
910 ret ? "supported" : "not supported");
913 /* if we didn't find any supported IOMMU types, fail */
915 close(vfio_container_fd);
923 rte_vfio_get_container_fd(void)
925 int ret, vfio_container_fd;
927 /* if we're in a primary process, try to open the container */
928 if (internal_config.process_type == RTE_PROC_PRIMARY) {
929 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
930 if (vfio_container_fd < 0) {
931 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
932 "error %i (%s)\n", errno, strerror(errno));
936 /* check VFIO API version */
937 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
938 if (ret != VFIO_API_VERSION) {
940 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
941 "error %i (%s)\n", errno, strerror(errno));
943 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
944 close(vfio_container_fd);
948 ret = vfio_has_supported_extensions(vfio_container_fd);
950 RTE_LOG(ERR, EAL, " no supported IOMMU "
951 "extensions found!\n");
955 return vfio_container_fd;
958 * if we're in a secondary process, request container fd from the
959 * primary process via our socket
963 socket_fd = vfio_mp_sync_connect_to_primary();
965 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
968 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
969 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
973 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
974 if (vfio_container_fd < 0) {
975 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
980 return vfio_container_fd;
987 rte_vfio_get_group_num(const char *sysfs_base,
988 const char *dev_addr, int *iommu_group_num)
990 char linkname[PATH_MAX];
991 char filename[PATH_MAX];
992 char *tok[16], *group_tok, *end;
995 memset(linkname, 0, sizeof(linkname));
996 memset(filename, 0, sizeof(filename));
998 /* try to find out IOMMU group for this device */
999 snprintf(linkname, sizeof(linkname),
1000 "%s/%s/iommu_group", sysfs_base, dev_addr);
1002 ret = readlink(linkname, filename, sizeof(filename));
1004 /* if the link doesn't exist, no VFIO for us */
1008 ret = rte_strsplit(filename, sizeof(filename),
1009 tok, RTE_DIM(tok), '/');
1012 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
1016 /* IOMMU group is always the last token */
1018 group_tok = tok[ret - 1];
1020 *iommu_group_num = strtol(group_tok, &end, 10);
1021 if ((end != group_tok && *end != '\0') || errno != 0) {
1022 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
1030 type1_map(const struct rte_memseg_list *msl __rte_unused,
1031 const struct rte_memseg *ms, void *arg)
1033 int *vfio_container_fd = arg;
1035 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1040 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1041 uint64_t len, int do_map)
1043 struct vfio_iommu_type1_dma_map dma_map;
1044 struct vfio_iommu_type1_dma_unmap dma_unmap;
1048 memset(&dma_map, 0, sizeof(dma_map));
1049 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1050 dma_map.vaddr = vaddr;
1052 dma_map.iova = iova;
1053 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1054 VFIO_DMA_MAP_FLAG_WRITE;
1056 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1058 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1059 errno, strerror(errno));
1063 memset(&dma_unmap, 0, sizeof(dma_unmap));
1064 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1065 dma_unmap.size = len;
1066 dma_unmap.iova = iova;
1068 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1071 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1072 errno, strerror(errno));
1081 vfio_type1_dma_map(int vfio_container_fd)
1083 return rte_memseg_walk(type1_map, &vfio_container_fd);
1087 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1088 uint64_t len, int do_map)
1090 struct vfio_iommu_type1_dma_map dma_map;
1091 struct vfio_iommu_type1_dma_unmap dma_unmap;
1095 memset(&dma_map, 0, sizeof(dma_map));
1096 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1097 dma_map.vaddr = vaddr;
1099 dma_map.iova = iova;
1100 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1101 VFIO_DMA_MAP_FLAG_WRITE;
1103 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1105 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1106 errno, strerror(errno));
1111 struct vfio_iommu_spapr_register_memory reg = {
1112 .argsz = sizeof(reg),
1115 reg.vaddr = (uintptr_t) vaddr;
1118 ret = ioctl(vfio_container_fd,
1119 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
1121 RTE_LOG(ERR, EAL, " cannot unregister vaddr for IOMMU, error %i (%s)\n",
1122 errno, strerror(errno));
1126 memset(&dma_unmap, 0, sizeof(dma_unmap));
1127 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1128 dma_unmap.size = len;
1129 dma_unmap.iova = iova;
1131 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1134 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1135 errno, strerror(errno));
1144 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1145 const struct rte_memseg *ms, void *arg)
1147 int *vfio_container_fd = arg;
1149 return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1153 struct spapr_walk_param {
1154 uint64_t window_size;
1155 uint64_t hugepage_sz;
1158 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1159 const struct rte_memseg *ms, void *arg)
1161 struct spapr_walk_param *param = arg;
1162 uint64_t max = ms->iova + ms->len;
1164 if (max > param->window_size) {
1165 param->hugepage_sz = ms->hugepage_sz;
1166 param->window_size = max;
1173 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1174 struct vfio_iommu_spapr_tce_create *create) {
1175 struct vfio_iommu_spapr_tce_remove remove = {
1176 .argsz = sizeof(remove),
1178 struct vfio_iommu_spapr_tce_info info = {
1179 .argsz = sizeof(info),
1183 /* query spapr iommu info */
1184 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1186 RTE_LOG(ERR, EAL, " cannot get iommu info, "
1187 "error %i (%s)\n", errno, strerror(errno));
1191 /* remove default DMA of 32 bit window */
1192 remove.start_addr = info.dma32_window_start;
1193 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1195 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
1196 "error %i (%s)\n", errno, strerror(errno));
1200 /* create new DMA window */
1201 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1203 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
1204 "error %i (%s)\n", errno, strerror(errno));
1208 if (create->start_addr != 0) {
1209 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
1217 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1218 uint64_t len, int do_map)
1220 struct spapr_walk_param param;
1221 struct vfio_iommu_spapr_tce_create create = {
1222 .argsz = sizeof(create),
1226 rte_spinlock_recursive_lock(&user_mem_maps.lock);
1228 /* check if window size needs to be adjusted */
1229 memset(¶m, 0, sizeof(param));
1231 if (memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1233 RTE_LOG(ERR, EAL, "Could not get window size\n");
1238 /* also check user maps */
1239 for (i = 0; i < user_mem_maps.n_maps; i++) {
1240 uint64_t max = user_mem_maps.maps[i].iova +
1241 user_mem_maps.maps[i].len;
1242 create.window_size = RTE_MAX(create.window_size, max);
1245 /* sPAPR requires window size to be a power of 2 */
1246 create.window_size = rte_align64pow2(param.window_size);
1247 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1252 /* re-create window and remap the entire memory */
1253 if (iova > create.window_size) {
1254 if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1256 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1260 if (memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1261 &vfio_container_fd) < 0) {
1262 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1266 /* remap all user maps */
1267 for (i = 0; i < user_mem_maps.n_maps; i++) {
1268 struct user_mem_map *map =
1269 &user_mem_maps.maps[i];
1270 if (vfio_spapr_dma_do_map(vfio_container_fd,
1271 map->addr, map->iova, map->len,
1273 RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1280 /* now that we've remapped all of the memory that was present
1281 * before, map the segment that we were requested to map.
1283 * however, if we were called by the callback, the memory we
1284 * were called with was already in the memseg list, so previous
1285 * mapping should've mapped that segment already.
1287 * virt2memseg_list is a relatively cheap check, so use that. if
1288 * memory is within any memseg list, it's a memseg, so it's
1291 addr = (void *)(uintptr_t)vaddr;
1292 if (rte_mem_virt2memseg_list(addr) == NULL &&
1293 vfio_spapr_dma_do_map(vfio_container_fd,
1294 vaddr, iova, len, 1) < 0) {
1295 RTE_LOG(ERR, EAL, "Could not map segment\n");
1300 /* for unmap, check if iova within DMA window */
1301 if (iova > create.window_size) {
1302 RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1307 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1310 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
1315 vfio_spapr_dma_map(int vfio_container_fd)
1317 struct vfio_iommu_spapr_tce_create create = {
1318 .argsz = sizeof(create),
1320 struct spapr_walk_param param;
1322 memset(¶m, 0, sizeof(param));
1324 /* create DMA window from 0 to max(phys_addr + len) */
1325 rte_memseg_walk(vfio_spapr_window_size_walk, ¶m);
1327 /* sPAPR requires window size to be a power of 2 */
1328 create.window_size = rte_align64pow2(param.window_size);
1329 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1332 if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1333 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1337 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1338 if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1345 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1347 /* No-IOMMU mode does not need DMA mapping */
1352 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1353 uint64_t __rte_unused vaddr,
1354 uint64_t __rte_unused iova, uint64_t __rte_unused len,
1355 int __rte_unused do_map)
1357 /* No-IOMMU mode does not need DMA mapping */
1362 vfio_dma_mem_map(uint64_t vaddr, uint64_t iova, uint64_t len, int do_map)
1364 const struct vfio_iommu_type *t = vfio_cfg.vfio_iommu_type;
1367 RTE_LOG(ERR, EAL, " VFIO support not initialized\n");
1372 if (!t->dma_user_map_func) {
1374 " VFIO custom DMA region maping not supported by IOMMU %s\n",
1376 rte_errno = ENOTSUP;
1380 return t->dma_user_map_func(vfio_cfg.vfio_container_fd, vaddr, iova,
1384 int __rte_experimental
1385 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1387 struct user_mem_map *new_map;
1395 rte_spinlock_recursive_lock(&user_mem_maps.lock);
1396 if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1397 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1403 if (vfio_dma_mem_map(vaddr, iova, len, 1)) {
1404 /* technically, this will fail if there are currently no devices
1405 * plugged in, even if a device were added later, this mapping
1406 * might have succeeded. however, since we cannot verify if this
1407 * is a valid mapping without having a device attached, consider
1408 * this to be unsupported, because we can't just store any old
1409 * mapping and pollute list of active mappings willy-nilly.
1411 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1415 /* create new user mem map entry */
1416 new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1417 new_map->addr = vaddr;
1418 new_map->iova = iova;
1421 compact_user_maps();
1423 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
1427 int __rte_experimental
1428 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1430 struct user_mem_map *map, *new_map = NULL;
1438 rte_spinlock_recursive_lock(&user_mem_maps.lock);
1440 /* find our mapping */
1441 map = find_user_mem_map(vaddr, iova, len);
1443 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1448 if (map->addr != vaddr || map->iova != iova || map->len != len) {
1449 /* we're partially unmapping a previously mapped region, so we
1450 * need to split entry into two.
1452 if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1453 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1458 new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1461 /* unmap the entry */
1462 if (vfio_dma_mem_map(vaddr, iova, len, 0)) {
1463 /* there may not be any devices plugged in, so unmapping will
1464 * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1465 * stop us from removing the mapping, as the assumption is we
1466 * won't be needing this memory any more and thus will want to
1467 * prevent it from being remapped again on hotplug. so, only
1468 * fail if we indeed failed to unmap (e.g. if the mapping was
1469 * within our mapped range but had invalid alignment).
1471 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1472 RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1476 RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1479 /* remove map from the list of active mappings */
1480 if (new_map != NULL) {
1481 adjust_map(map, new_map, vaddr, len);
1483 /* if we've created a new map by splitting, sort everything */
1484 if (!is_null_map(new_map)) {
1485 compact_user_maps();
1487 /* we've created a new mapping, but it was unused */
1488 user_mem_maps.n_maps--;
1491 memset(map, 0, sizeof(*map));
1492 compact_user_maps();
1493 user_mem_maps.n_maps--;
1497 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
1502 rte_vfio_noiommu_is_enabled(void)
1508 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1510 if (errno != ENOENT) {
1511 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
1512 errno, strerror(errno));
1516 * else the file does not exists
1517 * i.e. noiommu is not enabled
1522 cnt = read(fd, &c, 1);
1525 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
1526 "file %i (%s)\n", errno, strerror(errno));
1535 int __rte_experimental
1536 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1537 __rte_unused uint64_t len)
1542 int __rte_experimental
1543 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1544 __rte_unused uint64_t len)