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 vfio_get_group_fd(int iommu_group_no)
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_no == iommu_group_no)
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_no == -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_no);
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, iommu_group_no);
334 vfio_group_fd = open(filename, O_RDWR);
335 if (vfio_group_fd < 0) {
336 if (errno != ENOENT) {
337 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
343 /* noiommu group found */
346 cur_grp->group_no = iommu_group_no;
347 cur_grp->fd = vfio_group_fd;
348 vfio_cfg.vfio_active_groups++;
349 return vfio_group_fd;
351 /* if we're in a secondary process, request group fd from the primary
352 * process via our socket
357 socket_fd = vfio_mp_sync_connect_to_primary();
360 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
363 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
364 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
368 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
369 RTE_LOG(ERR, EAL, " cannot send group number!\n");
373 ret = vfio_mp_sync_receive_request(socket_fd);
379 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
380 /* if we got the fd, store it and return it */
381 if (vfio_group_fd > 0) {
383 cur_grp->group_no = iommu_group_no;
384 cur_grp->fd = vfio_group_fd;
385 vfio_cfg.vfio_active_groups++;
386 return vfio_group_fd;
388 /* fall-through on error */
390 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
400 get_vfio_group_idx(int vfio_group_fd)
403 for (i = 0; i < VFIO_MAX_GROUPS; i++)
404 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
410 vfio_group_device_get(int vfio_group_fd)
414 i = get_vfio_group_idx(vfio_group_fd);
415 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
416 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
418 vfio_cfg.vfio_groups[i].devices++;
422 vfio_group_device_put(int vfio_group_fd)
426 i = get_vfio_group_idx(vfio_group_fd);
427 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
428 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
430 vfio_cfg.vfio_groups[i].devices--;
434 vfio_group_device_count(int vfio_group_fd)
438 i = get_vfio_group_idx(vfio_group_fd);
439 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
440 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
444 return vfio_cfg.vfio_groups[i].devices;
448 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len)
450 struct rte_memseg_list *msl;
451 struct rte_memseg *ms;
454 msl = rte_mem_virt2memseg_list(addr);
456 /* for IOVA as VA mode, no need to care for IOVA addresses */
457 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
458 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
459 if (type == RTE_MEM_EVENT_ALLOC)
460 vfio_dma_mem_map(vfio_va, vfio_va, len, 1);
462 vfio_dma_mem_map(vfio_va, vfio_va, len, 0);
466 /* memsegs are contiguous in memory */
467 ms = rte_mem_virt2memseg(addr, msl);
468 while (cur_len < len) {
469 if (type == RTE_MEM_EVENT_ALLOC)
470 vfio_dma_mem_map(ms->addr_64, ms->iova, ms->len, 1);
472 vfio_dma_mem_map(ms->addr_64, ms->iova, ms->len, 0);
480 rte_vfio_clear_group(int vfio_group_fd)
485 if (internal_config.process_type == RTE_PROC_PRIMARY) {
487 i = get_vfio_group_idx(vfio_group_fd);
490 vfio_cfg.vfio_groups[i].group_no = -1;
491 vfio_cfg.vfio_groups[i].fd = -1;
492 vfio_cfg.vfio_groups[i].devices = 0;
493 vfio_cfg.vfio_active_groups--;
497 /* This is just for SECONDARY processes */
498 socket_fd = vfio_mp_sync_connect_to_primary();
501 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
505 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
506 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
511 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
512 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
517 ret = vfio_mp_sync_receive_request(socket_fd);
520 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
527 RTE_LOG(ERR, EAL, " Socket error\n");
531 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
538 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
539 int *vfio_dev_fd, struct vfio_device_info *device_info)
541 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
542 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
543 struct vfio_group_status group_status = {
544 .argsz = sizeof(group_status)
550 /* get group number */
551 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
553 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
558 /* if negative, something failed */
562 /* get the actual group fd */
563 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
564 if (vfio_group_fd < 0)
567 /* if group_fd == 0, that means the device isn't managed by VFIO */
568 if (vfio_group_fd == 0) {
569 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
575 * at this point, we know that this group is viable (meaning, all devices
576 * are either bound to VFIO or not bound to anything)
579 /* check if the group is viable */
580 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
582 RTE_LOG(ERR, EAL, " %s cannot get group status, "
583 "error %i (%s)\n", dev_addr, errno, strerror(errno));
584 close(vfio_group_fd);
585 rte_vfio_clear_group(vfio_group_fd);
587 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
588 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
589 close(vfio_group_fd);
590 rte_vfio_clear_group(vfio_group_fd);
594 /* check if group does not have a container yet */
595 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
597 /* add group to a container */
598 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
599 &vfio_cfg.vfio_container_fd);
601 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
602 "error %i (%s)\n", dev_addr, errno, strerror(errno));
603 close(vfio_group_fd);
604 rte_vfio_clear_group(vfio_group_fd);
609 * pick an IOMMU type and set up DMA mappings for container
611 * needs to be done only once, only when first group is
612 * assigned to a container and only in primary process.
613 * Note this can happen several times with the hotplug
616 if (internal_config.process_type == RTE_PROC_PRIMARY &&
617 vfio_cfg.vfio_active_groups == 1) {
618 const struct vfio_iommu_type *t;
620 /* select an IOMMU type which we will be using */
621 t = vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
624 " %s failed to select IOMMU type\n",
626 close(vfio_group_fd);
627 rte_vfio_clear_group(vfio_group_fd);
630 /* lock memory hotplug before mapping and release it
631 * after registering callback, to prevent races
633 rte_rwlock_read_lock(mem_lock);
634 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
637 " %s DMA remapping failed, error %i (%s)\n",
638 dev_addr, errno, strerror(errno));
639 close(vfio_group_fd);
640 rte_vfio_clear_group(vfio_group_fd);
641 rte_rwlock_read_unlock(mem_lock);
645 vfio_cfg.vfio_iommu_type = t;
647 /* re-map all user-mapped segments */
648 rte_spinlock_recursive_lock(&user_mem_maps.lock);
650 /* this IOMMU type may not support DMA mapping, but
651 * if we have mappings in the list - that means we have
652 * previously mapped something successfully, so we can
653 * be sure that DMA mapping is supported.
655 for (i = 0; i < user_mem_maps.n_maps; i++) {
656 struct user_mem_map *map;
657 map = &user_mem_maps.maps[i];
659 ret = t->dma_user_map_func(
660 vfio_cfg.vfio_container_fd,
661 map->addr, map->iova, map->len,
664 RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
666 "iova: 0x%" PRIx64 " "
667 "len: 0x%" PRIu64 "\n",
668 map->addr, map->iova,
670 rte_spinlock_recursive_unlock(
671 &user_mem_maps.lock);
672 rte_rwlock_read_unlock(mem_lock);
676 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
678 /* register callback for mem events */
679 ret = rte_mem_event_callback_register(
680 VFIO_MEM_EVENT_CLB_NAME,
681 vfio_mem_event_callback);
682 /* unlock memory hotplug */
683 rte_rwlock_read_unlock(mem_lock);
685 if (ret && rte_errno != ENOTSUP) {
686 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
690 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
692 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
696 /* get a file descriptor for the device */
697 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
698 if (*vfio_dev_fd < 0) {
699 /* if we cannot get a device fd, this implies a problem with
700 * the VFIO group or the container not having IOMMU configured.
703 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
705 close(vfio_group_fd);
706 rte_vfio_clear_group(vfio_group_fd);
710 /* test and setup the device */
711 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
713 RTE_LOG(ERR, EAL, " %s cannot get device info, "
714 "error %i (%s)\n", dev_addr, errno,
717 close(vfio_group_fd);
718 rte_vfio_clear_group(vfio_group_fd);
721 vfio_group_device_get(vfio_group_fd);
727 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
730 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
731 rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
732 struct vfio_group_status group_status = {
733 .argsz = sizeof(group_status)
739 /* we don't want any DMA mapping messages to come while we're detaching
740 * VFIO device, because this might be the last device and we might need
741 * to unregister the callback.
743 rte_rwlock_read_lock(mem_lock);
745 /* get group number */
746 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
748 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
750 /* This is an error at this point. */
755 /* get the actual group fd */
756 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
757 if (vfio_group_fd <= 0) {
758 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
764 /* At this point we got an active group. Closing it will make the
765 * container detachment. If this is the last active group, VFIO kernel
766 * code will unset the container and the IOMMU mappings.
769 /* Closing a device */
770 if (close(vfio_dev_fd) < 0) {
771 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
777 /* An VFIO group can have several devices attached. Just when there is
778 * no devices remaining should the group be closed.
780 vfio_group_device_put(vfio_group_fd);
781 if (!vfio_group_device_count(vfio_group_fd)) {
783 if (close(vfio_group_fd) < 0) {
784 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
790 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
791 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
798 /* if there are no active device groups, unregister the callback to
799 * avoid spurious attempts to map/unmap memory from VFIO.
801 if (vfio_cfg.vfio_active_groups == 0)
802 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME);
808 rte_rwlock_read_unlock(mem_lock);
813 rte_vfio_enable(const char *modname)
815 /* initialize group list */
819 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
820 vfio_cfg.vfio_groups[i].fd = -1;
821 vfio_cfg.vfio_groups[i].group_no = -1;
822 vfio_cfg.vfio_groups[i].devices = 0;
825 /* inform the user that we are probing for VFIO */
826 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
828 /* check if vfio module is loaded */
829 vfio_available = rte_eal_check_module(modname);
831 /* return error directly */
832 if (vfio_available == -1) {
833 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
837 /* return 0 if VFIO modules not loaded */
838 if (vfio_available == 0) {
839 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
840 "skipping VFIO support...\n");
844 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
846 /* check if we have VFIO driver enabled */
847 if (vfio_cfg.vfio_container_fd != -1) {
848 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
849 vfio_cfg.vfio_enabled = 1;
851 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
858 rte_vfio_is_enabled(const char *modname)
860 const int mod_available = rte_eal_check_module(modname) > 0;
861 return vfio_cfg.vfio_enabled && mod_available;
864 const struct vfio_iommu_type *
865 vfio_set_iommu_type(int vfio_container_fd)
868 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
869 const struct vfio_iommu_type *t = &iommu_types[idx];
871 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
874 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
875 t->type_id, t->name);
878 /* not an error, there may be more supported IOMMU types */
879 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
880 "error %i (%s)\n", t->type_id, t->name, errno,
883 /* if we didn't find a suitable IOMMU type, fail */
888 vfio_has_supported_extensions(int vfio_container_fd)
891 unsigned idx, n_extensions = 0;
892 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
893 const struct vfio_iommu_type *t = &iommu_types[idx];
895 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
898 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
899 "error %i (%s)\n", errno,
901 close(vfio_container_fd);
903 } else if (ret == 1) {
904 /* we found a supported extension */
907 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
909 ret ? "supported" : "not supported");
912 /* if we didn't find any supported IOMMU types, fail */
914 close(vfio_container_fd);
922 vfio_get_container_fd(void)
924 int ret, vfio_container_fd;
926 /* if we're in a primary process, try to open the container */
927 if (internal_config.process_type == RTE_PROC_PRIMARY) {
928 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
929 if (vfio_container_fd < 0) {
930 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
931 "error %i (%s)\n", errno, strerror(errno));
935 /* check VFIO API version */
936 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
937 if (ret != VFIO_API_VERSION) {
939 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
940 "error %i (%s)\n", errno, strerror(errno));
942 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
943 close(vfio_container_fd);
947 ret = vfio_has_supported_extensions(vfio_container_fd);
949 RTE_LOG(ERR, EAL, " no supported IOMMU "
950 "extensions found!\n");
954 return vfio_container_fd;
957 * if we're in a secondary process, request container fd from the
958 * primary process via our socket
962 socket_fd = vfio_mp_sync_connect_to_primary();
964 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
967 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
968 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
972 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
973 if (vfio_container_fd < 0) {
974 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
979 return vfio_container_fd;
986 vfio_get_group_no(const char *sysfs_base,
987 const char *dev_addr, int *iommu_group_no)
989 char linkname[PATH_MAX];
990 char filename[PATH_MAX];
991 char *tok[16], *group_tok, *end;
994 memset(linkname, 0, sizeof(linkname));
995 memset(filename, 0, sizeof(filename));
997 /* try to find out IOMMU group for this device */
998 snprintf(linkname, sizeof(linkname),
999 "%s/%s/iommu_group", sysfs_base, dev_addr);
1001 ret = readlink(linkname, filename, sizeof(filename));
1003 /* if the link doesn't exist, no VFIO for us */
1007 ret = rte_strsplit(filename, sizeof(filename),
1008 tok, RTE_DIM(tok), '/');
1011 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
1015 /* IOMMU group is always the last token */
1017 group_tok = tok[ret - 1];
1019 *iommu_group_no = strtol(group_tok, &end, 10);
1020 if ((end != group_tok && *end != '\0') || errno != 0) {
1021 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
1029 type1_map(const struct rte_memseg_list *msl __rte_unused,
1030 const struct rte_memseg *ms, void *arg)
1032 int *vfio_container_fd = arg;
1034 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1039 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1040 uint64_t len, int do_map)
1042 struct vfio_iommu_type1_dma_map dma_map;
1043 struct vfio_iommu_type1_dma_unmap dma_unmap;
1047 memset(&dma_map, 0, sizeof(dma_map));
1048 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1049 dma_map.vaddr = vaddr;
1051 dma_map.iova = iova;
1052 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1053 VFIO_DMA_MAP_FLAG_WRITE;
1055 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1057 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1058 errno, strerror(errno));
1062 memset(&dma_unmap, 0, sizeof(dma_unmap));
1063 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1064 dma_unmap.size = len;
1065 dma_unmap.iova = iova;
1067 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1070 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1071 errno, strerror(errno));
1080 vfio_type1_dma_map(int vfio_container_fd)
1082 return rte_memseg_walk(type1_map, &vfio_container_fd);
1086 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1087 uint64_t len, int do_map)
1089 struct vfio_iommu_type1_dma_map dma_map;
1090 struct vfio_iommu_type1_dma_unmap dma_unmap;
1094 memset(&dma_map, 0, sizeof(dma_map));
1095 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1096 dma_map.vaddr = vaddr;
1098 dma_map.iova = iova;
1099 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1100 VFIO_DMA_MAP_FLAG_WRITE;
1102 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1104 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, error %i (%s)\n",
1105 errno, strerror(errno));
1110 struct vfio_iommu_spapr_register_memory reg = {
1111 .argsz = sizeof(reg),
1114 reg.vaddr = (uintptr_t) vaddr;
1117 ret = ioctl(vfio_container_fd,
1118 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
1120 RTE_LOG(ERR, EAL, " cannot unregister vaddr for IOMMU, error %i (%s)\n",
1121 errno, strerror(errno));
1125 memset(&dma_unmap, 0, sizeof(dma_unmap));
1126 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1127 dma_unmap.size = len;
1128 dma_unmap.iova = iova;
1130 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1133 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1134 errno, strerror(errno));
1143 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1144 const struct rte_memseg *ms, void *arg)
1146 int *vfio_container_fd = arg;
1148 return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1152 struct spapr_walk_param {
1153 uint64_t window_size;
1154 uint64_t hugepage_sz;
1157 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1158 const struct rte_memseg *ms, void *arg)
1160 struct spapr_walk_param *param = arg;
1161 uint64_t max = ms->iova + ms->len;
1163 if (max > param->window_size) {
1164 param->hugepage_sz = ms->hugepage_sz;
1165 param->window_size = max;
1172 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1173 struct vfio_iommu_spapr_tce_create *create) {
1174 struct vfio_iommu_spapr_tce_remove remove = {
1175 .argsz = sizeof(remove),
1177 struct vfio_iommu_spapr_tce_info info = {
1178 .argsz = sizeof(info),
1182 /* query spapr iommu info */
1183 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1185 RTE_LOG(ERR, EAL, " cannot get iommu info, "
1186 "error %i (%s)\n", errno, strerror(errno));
1190 /* remove default DMA of 32 bit window */
1191 remove.start_addr = info.dma32_window_start;
1192 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1194 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
1195 "error %i (%s)\n", errno, strerror(errno));
1199 /* create new DMA window */
1200 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1202 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
1203 "error %i (%s)\n", errno, strerror(errno));
1207 if (create->start_addr != 0) {
1208 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
1216 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1217 uint64_t len, int do_map)
1219 struct spapr_walk_param param;
1220 struct vfio_iommu_spapr_tce_create create = {
1221 .argsz = sizeof(create),
1225 rte_spinlock_recursive_lock(&user_mem_maps.lock);
1227 /* check if window size needs to be adjusted */
1228 memset(¶m, 0, sizeof(param));
1230 if (memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1232 RTE_LOG(ERR, EAL, "Could not get window size\n");
1237 /* also check user maps */
1238 for (i = 0; i < user_mem_maps.n_maps; i++) {
1239 uint64_t max = user_mem_maps.maps[i].iova +
1240 user_mem_maps.maps[i].len;
1241 create.window_size = RTE_MAX(create.window_size, max);
1244 /* sPAPR requires window size to be a power of 2 */
1245 create.window_size = rte_align64pow2(param.window_size);
1246 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1251 /* re-create window and remap the entire memory */
1252 if (iova > create.window_size) {
1253 if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1255 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1259 if (memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1260 &vfio_container_fd) < 0) {
1261 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1265 /* remap all user maps */
1266 for (i = 0; i < user_mem_maps.n_maps; i++) {
1267 struct user_mem_map *map =
1268 &user_mem_maps.maps[i];
1269 if (vfio_spapr_dma_do_map(vfio_container_fd,
1270 map->addr, map->iova, map->len,
1272 RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1279 /* now that we've remapped all of the memory that was present
1280 * before, map the segment that we were requested to map.
1282 * however, if we were called by the callback, the memory we
1283 * were called with was already in the memseg list, so previous
1284 * mapping should've mapped that segment already.
1286 * virt2memseg_list is a relatively cheap check, so use that. if
1287 * memory is within any memseg list, it's a memseg, so it's
1290 addr = (void *)(uintptr_t)vaddr;
1291 if (rte_mem_virt2memseg_list(addr) == NULL &&
1292 vfio_spapr_dma_do_map(vfio_container_fd,
1293 vaddr, iova, len, 1) < 0) {
1294 RTE_LOG(ERR, EAL, "Could not map segment\n");
1299 /* for unmap, check if iova within DMA window */
1300 if (iova > create.window_size) {
1301 RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1306 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1309 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
1314 vfio_spapr_dma_map(int vfio_container_fd)
1316 struct vfio_iommu_spapr_tce_create create = {
1317 .argsz = sizeof(create),
1319 struct spapr_walk_param param;
1321 memset(¶m, 0, sizeof(param));
1323 /* create DMA window from 0 to max(phys_addr + len) */
1324 rte_memseg_walk(vfio_spapr_window_size_walk, ¶m);
1326 /* sPAPR requires window size to be a power of 2 */
1327 create.window_size = rte_align64pow2(param.window_size);
1328 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1331 if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1332 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1336 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1337 if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1344 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1346 /* No-IOMMU mode does not need DMA mapping */
1351 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1352 uint64_t __rte_unused vaddr,
1353 uint64_t __rte_unused iova, uint64_t __rte_unused len,
1354 int __rte_unused do_map)
1356 /* No-IOMMU mode does not need DMA mapping */
1361 vfio_dma_mem_map(uint64_t vaddr, uint64_t iova, uint64_t len, int do_map)
1363 const struct vfio_iommu_type *t = vfio_cfg.vfio_iommu_type;
1366 RTE_LOG(ERR, EAL, " VFIO support not initialized\n");
1371 if (!t->dma_user_map_func) {
1373 " VFIO custom DMA region maping not supported by IOMMU %s\n",
1375 rte_errno = ENOTSUP;
1379 return t->dma_user_map_func(vfio_cfg.vfio_container_fd, vaddr, iova,
1383 int __rte_experimental
1384 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1386 struct user_mem_map *new_map;
1394 rte_spinlock_recursive_lock(&user_mem_maps.lock);
1395 if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1396 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1402 if (vfio_dma_mem_map(vaddr, iova, len, 1)) {
1403 /* technically, this will fail if there are currently no devices
1404 * plugged in, even if a device were added later, this mapping
1405 * might have succeeded. however, since we cannot verify if this
1406 * is a valid mapping without having a device attached, consider
1407 * this to be unsupported, because we can't just store any old
1408 * mapping and pollute list of active mappings willy-nilly.
1410 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1414 /* create new user mem map entry */
1415 new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1416 new_map->addr = vaddr;
1417 new_map->iova = iova;
1420 compact_user_maps();
1422 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
1426 int __rte_experimental
1427 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1429 struct user_mem_map *map, *new_map = NULL;
1437 rte_spinlock_recursive_lock(&user_mem_maps.lock);
1439 /* find our mapping */
1440 map = find_user_mem_map(vaddr, iova, len);
1442 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1447 if (map->addr != vaddr || map->iova != iova || map->len != len) {
1448 /* we're partially unmapping a previously mapped region, so we
1449 * need to split entry into two.
1451 if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1452 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1457 new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1460 /* unmap the entry */
1461 if (vfio_dma_mem_map(vaddr, iova, len, 0)) {
1462 /* there may not be any devices plugged in, so unmapping will
1463 * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1464 * stop us from removing the mapping, as the assumption is we
1465 * won't be needing this memory any more and thus will want to
1466 * prevent it from being remapped again on hotplug. so, only
1467 * fail if we indeed failed to unmap (e.g. if the mapping was
1468 * within our mapped range but had invalid alignment).
1470 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1471 RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1475 RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1478 /* remove map from the list of active mappings */
1479 if (new_map != NULL) {
1480 adjust_map(map, new_map, vaddr, len);
1482 /* if we've created a new map by splitting, sort everything */
1483 if (!is_null_map(new_map)) {
1484 compact_user_maps();
1486 /* we've created a new mapping, but it was unused */
1487 user_mem_maps.n_maps--;
1490 memset(map, 0, sizeof(*map));
1491 compact_user_maps();
1492 user_mem_maps.n_maps--;
1496 rte_spinlock_recursive_unlock(&user_mem_maps.lock);
1501 rte_vfio_noiommu_is_enabled(void)
1507 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1509 if (errno != ENOENT) {
1510 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
1511 errno, strerror(errno));
1515 * else the file does not exists
1516 * i.e. noiommu is not enabled
1521 cnt = read(fd, &c, 1);
1524 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
1525 "file %i (%s)\n", errno, strerror(errno));
1534 int __rte_experimental
1535 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1536 __rte_unused uint64_t len)
1541 int __rte_experimental
1542 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1543 __rte_unused uint64_t len)