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"
18 #include "eal_memcfg.h"
20 #include "eal_private.h"
24 #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
26 /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
27 * recreate the mappings for DPDK segments, but we cannot do so for memory that
28 * was registered by the user themselves, so we need to store the user mappings
29 * somewhere, to recreate them later.
31 #define VFIO_MAX_USER_MEM_MAPS 256
38 struct user_mem_maps {
39 rte_spinlock_recursive_t lock;
41 struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
46 int vfio_container_fd;
47 int vfio_active_groups;
48 const struct vfio_iommu_type *vfio_iommu_type;
49 struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
50 struct user_mem_maps mem_maps;
53 /* per-process VFIO config */
54 static struct vfio_config vfio_cfgs[VFIO_MAX_CONTAINERS];
55 static struct vfio_config *default_vfio_cfg = &vfio_cfgs[0];
57 static int vfio_type1_dma_map(int);
58 static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
59 static int vfio_spapr_dma_map(int);
60 static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
61 static int vfio_noiommu_dma_map(int);
62 static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
63 static int vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr,
64 uint64_t iova, uint64_t len, int do_map);
66 /* IOMMU types we support */
67 static const struct vfio_iommu_type iommu_types[] = {
68 /* x86 IOMMU, otherwise known as type 1 */
70 .type_id = RTE_VFIO_TYPE1,
72 .dma_map_func = &vfio_type1_dma_map,
73 .dma_user_map_func = &vfio_type1_dma_mem_map
75 /* ppc64 IOMMU, otherwise known as spapr */
77 .type_id = RTE_VFIO_SPAPR,
79 .dma_map_func = &vfio_spapr_dma_map,
80 .dma_user_map_func = &vfio_spapr_dma_mem_map
84 .type_id = RTE_VFIO_NOIOMMU,
86 .dma_map_func = &vfio_noiommu_dma_map,
87 .dma_user_map_func = &vfio_noiommu_dma_mem_map
92 is_null_map(const struct user_mem_map *map)
94 return map->addr == 0 && map->iova == 0 && map->len == 0;
97 /* we may need to merge user mem maps together in case of user mapping/unmapping
98 * chunks of memory, so we'll need a comparator function to sort segments.
101 user_mem_map_cmp(const void *a, const void *b)
103 const struct user_mem_map *umm_a = a;
104 const struct user_mem_map *umm_b = b;
106 /* move null entries to end */
107 if (is_null_map(umm_a))
109 if (is_null_map(umm_b))
112 /* sort by iova first */
113 if (umm_a->iova < umm_b->iova)
115 if (umm_a->iova > umm_b->iova)
118 if (umm_a->addr < umm_b->addr)
120 if (umm_a->addr > umm_b->addr)
123 if (umm_a->len < umm_b->len)
125 if (umm_a->len > umm_b->len)
131 /* adjust user map entry. this may result in shortening of existing map, or in
132 * splitting existing map in two pieces.
135 adjust_map(struct user_mem_map *src, struct user_mem_map *end,
136 uint64_t remove_va_start, uint64_t remove_len)
138 /* if va start is same as start address, we're simply moving start */
139 if (remove_va_start == src->addr) {
140 src->addr += remove_len;
141 src->iova += remove_len;
142 src->len -= remove_len;
143 } else if (remove_va_start + remove_len == src->addr + src->len) {
144 /* we're shrinking mapping from the end */
145 src->len -= remove_len;
147 /* we're blowing a hole in the middle */
148 struct user_mem_map tmp;
149 uint64_t total_len = src->len;
151 /* adjust source segment length */
152 src->len = remove_va_start - src->addr;
154 /* create temporary segment in the middle */
155 tmp.addr = src->addr + src->len;
156 tmp.iova = src->iova + src->len;
157 tmp.len = remove_len;
159 /* populate end segment - this one we will be keeping */
160 end->addr = tmp.addr + tmp.len;
161 end->iova = tmp.iova + tmp.len;
162 end->len = total_len - src->len - tmp.len;
166 /* try merging two maps into one, return 1 if succeeded */
168 merge_map(struct user_mem_map *left, struct user_mem_map *right)
170 if (left->addr + left->len != right->addr)
172 if (left->iova + left->len != right->iova)
175 left->len += right->len;
177 memset(right, 0, sizeof(*right));
182 static struct user_mem_map *
183 find_user_mem_map(struct user_mem_maps *user_mem_maps, uint64_t addr,
184 uint64_t iova, uint64_t len)
186 uint64_t va_end = addr + len;
187 uint64_t iova_end = iova + len;
190 for (i = 0; i < user_mem_maps->n_maps; i++) {
191 struct user_mem_map *map = &user_mem_maps->maps[i];
192 uint64_t map_va_end = map->addr + map->len;
193 uint64_t map_iova_end = map->iova + map->len;
196 if (addr < map->addr || addr >= map_va_end)
198 /* check if VA end is within boundaries */
199 if (va_end <= map->addr || va_end > map_va_end)
202 /* check start IOVA */
203 if (iova < map->iova || iova >= map_iova_end)
205 /* check if IOVA end is within boundaries */
206 if (iova_end <= map->iova || iova_end > map_iova_end)
209 /* we've found our map */
215 /* this will sort all user maps, and merge/compact any adjacent maps */
217 compact_user_maps(struct user_mem_maps *user_mem_maps)
219 int i, n_merged, cur_idx;
221 qsort(user_mem_maps->maps, user_mem_maps->n_maps,
222 sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
224 /* we'll go over the list backwards when merging */
226 for (i = user_mem_maps->n_maps - 2; i >= 0; i--) {
227 struct user_mem_map *l, *r;
229 l = &user_mem_maps->maps[i];
230 r = &user_mem_maps->maps[i + 1];
232 if (is_null_map(l) || is_null_map(r))
239 /* the entries are still sorted, but now they have holes in them, so
240 * walk through the list and remove the holes
244 for (i = 0; i < user_mem_maps->n_maps; i++) {
245 if (!is_null_map(&user_mem_maps->maps[i])) {
246 struct user_mem_map *src, *dst;
248 src = &user_mem_maps->maps[i];
249 dst = &user_mem_maps->maps[cur_idx++];
252 memcpy(dst, src, sizeof(*src));
253 memset(src, 0, sizeof(*src));
257 user_mem_maps->n_maps = cur_idx;
262 vfio_open_group_fd(int iommu_group_num)
265 char filename[PATH_MAX];
266 struct rte_mp_msg mp_req, *mp_rep;
267 struct rte_mp_reply mp_reply = {0};
268 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
269 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
271 /* if primary, try to open the group */
272 if (internal_config.process_type == RTE_PROC_PRIMARY) {
273 /* try regular group format */
274 snprintf(filename, sizeof(filename),
275 VFIO_GROUP_FMT, iommu_group_num);
276 vfio_group_fd = open(filename, O_RDWR);
277 if (vfio_group_fd < 0) {
278 /* if file not found, it's not an error */
279 if (errno != ENOENT) {
280 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
285 /* special case: try no-IOMMU path as well */
286 snprintf(filename, sizeof(filename),
287 VFIO_NOIOMMU_GROUP_FMT,
289 vfio_group_fd = open(filename, O_RDWR);
290 if (vfio_group_fd < 0) {
291 if (errno != ENOENT) {
292 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
298 /* noiommu group found */
301 return vfio_group_fd;
303 /* if we're in a secondary process, request group fd from the primary
304 * process via mp channel.
306 p->req = SOCKET_REQ_GROUP;
307 p->group_num = iommu_group_num;
308 strcpy(mp_req.name, EAL_VFIO_MP);
309 mp_req.len_param = sizeof(*p);
313 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
314 mp_reply.nb_received == 1) {
315 mp_rep = &mp_reply.msgs[0];
316 p = (struct vfio_mp_param *)mp_rep->param;
317 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
318 vfio_group_fd = mp_rep->fds[0];
319 } else if (p->result == SOCKET_NO_FD) {
320 RTE_LOG(ERR, EAL, " bad VFIO group fd\n");
326 if (vfio_group_fd < 0)
327 RTE_LOG(ERR, EAL, " cannot request group fd\n");
328 return vfio_group_fd;
331 static struct vfio_config *
332 get_vfio_cfg_by_group_num(int iommu_group_num)
334 struct vfio_config *vfio_cfg;
337 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
338 vfio_cfg = &vfio_cfgs[i];
339 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
340 if (vfio_cfg->vfio_groups[j].group_num ==
350 vfio_get_group_fd(struct vfio_config *vfio_cfg,
355 struct vfio_group *cur_grp;
357 /* check if we already have the group descriptor open */
358 for (i = 0; i < VFIO_MAX_GROUPS; i++)
359 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
360 return vfio_cfg->vfio_groups[i].fd;
362 /* Lets see first if there is room for a new group */
363 if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
364 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
368 /* Now lets get an index for the new group */
369 for (i = 0; i < VFIO_MAX_GROUPS; i++)
370 if (vfio_cfg->vfio_groups[i].group_num == -1) {
371 cur_grp = &vfio_cfg->vfio_groups[i];
375 /* This should not happen */
376 if (i == VFIO_MAX_GROUPS) {
377 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
381 vfio_group_fd = vfio_open_group_fd(iommu_group_num);
382 if (vfio_group_fd <= 0) {
383 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
387 cur_grp->group_num = iommu_group_num;
388 cur_grp->fd = vfio_group_fd;
389 vfio_cfg->vfio_active_groups++;
391 return vfio_group_fd;
394 static struct vfio_config *
395 get_vfio_cfg_by_group_fd(int vfio_group_fd)
397 struct vfio_config *vfio_cfg;
400 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
401 vfio_cfg = &vfio_cfgs[i];
402 for (j = 0; j < VFIO_MAX_GROUPS; j++)
403 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
410 static struct vfio_config *
411 get_vfio_cfg_by_container_fd(int container_fd)
415 if (container_fd == RTE_VFIO_DEFAULT_CONTAINER_FD)
416 return default_vfio_cfg;
418 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
419 if (vfio_cfgs[i].vfio_container_fd == container_fd)
420 return &vfio_cfgs[i];
427 rte_vfio_get_group_fd(int iommu_group_num)
429 struct vfio_config *vfio_cfg;
431 /* get the vfio_config it belongs to */
432 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
433 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
435 return vfio_get_group_fd(vfio_cfg, iommu_group_num);
439 get_vfio_group_idx(int vfio_group_fd)
441 struct vfio_config *vfio_cfg;
444 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
445 vfio_cfg = &vfio_cfgs[i];
446 for (j = 0; j < VFIO_MAX_GROUPS; j++)
447 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
455 vfio_group_device_get(int vfio_group_fd)
457 struct vfio_config *vfio_cfg;
460 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
461 if (vfio_cfg == NULL) {
462 RTE_LOG(ERR, EAL, " invalid group fd!\n");
466 i = get_vfio_group_idx(vfio_group_fd);
467 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
468 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
470 vfio_cfg->vfio_groups[i].devices++;
474 vfio_group_device_put(int vfio_group_fd)
476 struct vfio_config *vfio_cfg;
479 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
480 if (vfio_cfg == NULL) {
481 RTE_LOG(ERR, EAL, " invalid group fd!\n");
485 i = get_vfio_group_idx(vfio_group_fd);
486 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
487 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
489 vfio_cfg->vfio_groups[i].devices--;
493 vfio_group_device_count(int vfio_group_fd)
495 struct vfio_config *vfio_cfg;
498 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
499 if (vfio_cfg == NULL) {
500 RTE_LOG(ERR, EAL, " invalid group fd!\n");
504 i = get_vfio_group_idx(vfio_group_fd);
505 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
506 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
510 return vfio_cfg->vfio_groups[i].devices;
514 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
515 void *arg __rte_unused)
517 rte_iova_t iova_start, iova_expected;
518 struct rte_memseg_list *msl;
519 struct rte_memseg *ms;
523 msl = rte_mem_virt2memseg_list(addr);
525 /* for IOVA as VA mode, no need to care for IOVA addresses */
526 if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
527 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
528 if (type == RTE_MEM_EVENT_ALLOC)
529 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
532 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
537 #ifdef RTE_ARCH_PPC_64
538 ms = rte_mem_virt2memseg(addr, msl);
539 while (cur_len < len) {
540 int idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
542 rte_fbarray_set_free(&msl->memseg_arr, idx);
548 /* memsegs are contiguous in memory */
549 ms = rte_mem_virt2memseg(addr, msl);
552 * This memory is not guaranteed to be contiguous, but it still could
553 * be, or it could have some small contiguous chunks. Since the number
554 * of VFIO mappings is limited, and VFIO appears to not concatenate
555 * adjacent mappings, we have to do this ourselves.
557 * So, find contiguous chunks, then map them.
559 va_start = ms->addr_64;
560 iova_start = iova_expected = ms->iova;
561 while (cur_len < len) {
562 bool new_contig_area = ms->iova != iova_expected;
563 bool last_seg = (len - cur_len) == ms->len;
564 bool skip_last = false;
566 /* only do mappings when current contiguous area ends */
567 if (new_contig_area) {
568 if (type == RTE_MEM_EVENT_ALLOC)
569 vfio_dma_mem_map(default_vfio_cfg, va_start,
571 iova_expected - iova_start, 1);
573 vfio_dma_mem_map(default_vfio_cfg, va_start,
575 iova_expected - iova_start, 0);
576 va_start = ms->addr_64;
577 iova_start = ms->iova;
579 /* some memory segments may have invalid IOVA */
580 if (ms->iova == RTE_BAD_IOVA) {
581 RTE_LOG(DEBUG, EAL, "Memory segment at %p has bad IOVA, skipping\n",
585 iova_expected = ms->iova + ms->len;
590 * don't count previous segment, and don't attempt to
591 * dereference a potentially invalid pointer.
593 if (skip_last && !last_seg) {
594 iova_expected = iova_start = ms->iova;
595 va_start = ms->addr_64;
596 } else if (!skip_last && last_seg) {
597 /* this is the last segment and we're not skipping */
598 if (type == RTE_MEM_EVENT_ALLOC)
599 vfio_dma_mem_map(default_vfio_cfg, va_start,
601 iova_expected - iova_start, 1);
603 vfio_dma_mem_map(default_vfio_cfg, va_start,
605 iova_expected - iova_start, 0);
608 #ifdef RTE_ARCH_PPC_64
610 ms = rte_mem_virt2memseg(addr, msl);
611 while (cur_len < len) {
612 int idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
614 rte_fbarray_set_used(&msl->memseg_arr, idx);
622 vfio_sync_default_container(void)
624 struct rte_mp_msg mp_req, *mp_rep;
625 struct rte_mp_reply mp_reply = {0};
626 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
627 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
631 /* cannot be called from primary */
632 if (rte_eal_process_type() != RTE_PROC_SECONDARY)
635 /* default container fd should have been opened in rte_vfio_enable() */
636 if (!default_vfio_cfg->vfio_enabled ||
637 default_vfio_cfg->vfio_container_fd < 0) {
638 RTE_LOG(ERR, EAL, "VFIO support is not initialized\n");
642 /* find default container's IOMMU type */
643 p->req = SOCKET_REQ_IOMMU_TYPE;
644 strcpy(mp_req.name, EAL_VFIO_MP);
645 mp_req.len_param = sizeof(*p);
649 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
650 mp_reply.nb_received == 1) {
651 mp_rep = &mp_reply.msgs[0];
652 p = (struct vfio_mp_param *)mp_rep->param;
653 if (p->result == SOCKET_OK)
654 iommu_type_id = p->iommu_type_id;
657 if (iommu_type_id < 0) {
658 RTE_LOG(ERR, EAL, "Could not get IOMMU type for default container\n");
662 /* we now have an fd for default container, as well as its IOMMU type.
663 * now, set up default VFIO container config to match.
665 for (i = 0; i < RTE_DIM(iommu_types); i++) {
666 const struct vfio_iommu_type *t = &iommu_types[i];
667 if (t->type_id != iommu_type_id)
670 /* we found our IOMMU type */
671 default_vfio_cfg->vfio_iommu_type = t;
675 RTE_LOG(ERR, EAL, "Could not find IOMMU type id (%i)\n",
681 rte_vfio_clear_group(int vfio_group_fd)
684 struct vfio_config *vfio_cfg;
686 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
687 if (vfio_cfg == NULL) {
688 RTE_LOG(ERR, EAL, " invalid group fd!\n");
692 i = get_vfio_group_idx(vfio_group_fd);
695 vfio_cfg->vfio_groups[i].group_num = -1;
696 vfio_cfg->vfio_groups[i].fd = -1;
697 vfio_cfg->vfio_groups[i].devices = 0;
698 vfio_cfg->vfio_active_groups--;
704 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
705 int *vfio_dev_fd, struct vfio_device_info *device_info)
707 struct vfio_group_status group_status = {
708 .argsz = sizeof(group_status)
710 struct vfio_config *vfio_cfg;
711 struct user_mem_maps *user_mem_maps;
712 int vfio_container_fd;
717 /* get group number */
718 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
720 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
725 /* if negative, something failed */
729 /* get the actual group fd */
730 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
731 if (vfio_group_fd < 0)
734 /* if group_fd == 0, that means the device isn't managed by VFIO */
735 if (vfio_group_fd == 0) {
736 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
742 * at this point, we know that this group is viable (meaning, all devices
743 * are either bound to VFIO or not bound to anything)
746 /* check if the group is viable */
747 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
749 RTE_LOG(ERR, EAL, " %s cannot get group status, "
750 "error %i (%s)\n", dev_addr, errno, strerror(errno));
751 close(vfio_group_fd);
752 rte_vfio_clear_group(vfio_group_fd);
754 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
755 RTE_LOG(ERR, EAL, " %s VFIO group is not viable! "
756 "Not all devices in IOMMU group bound to VFIO or unbound\n",
758 close(vfio_group_fd);
759 rte_vfio_clear_group(vfio_group_fd);
763 /* get the vfio_config it belongs to */
764 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
765 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
766 vfio_container_fd = vfio_cfg->vfio_container_fd;
767 user_mem_maps = &vfio_cfg->mem_maps;
769 /* check if group does not have a container yet */
770 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
772 /* add group to a container */
773 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
776 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
777 "error %i (%s)\n", dev_addr, errno, strerror(errno));
778 close(vfio_group_fd);
779 rte_vfio_clear_group(vfio_group_fd);
784 * pick an IOMMU type and set up DMA mappings for container
786 * needs to be done only once, only when first group is
787 * assigned to a container and only in primary process.
788 * Note this can happen several times with the hotplug
791 if (internal_config.process_type == RTE_PROC_PRIMARY &&
792 vfio_cfg->vfio_active_groups == 1 &&
793 vfio_group_device_count(vfio_group_fd) == 0) {
794 const struct vfio_iommu_type *t;
796 /* select an IOMMU type which we will be using */
797 t = vfio_set_iommu_type(vfio_container_fd);
800 " %s failed to select IOMMU type\n",
802 close(vfio_group_fd);
803 rte_vfio_clear_group(vfio_group_fd);
806 /* lock memory hotplug before mapping and release it
807 * after registering callback, to prevent races
809 rte_mcfg_mem_read_lock();
810 if (vfio_cfg == default_vfio_cfg)
811 ret = t->dma_map_func(vfio_container_fd);
816 " %s DMA remapping failed, error %i (%s)\n",
817 dev_addr, errno, strerror(errno));
818 close(vfio_group_fd);
819 rte_vfio_clear_group(vfio_group_fd);
820 rte_mcfg_mem_read_unlock();
824 vfio_cfg->vfio_iommu_type = t;
826 /* re-map all user-mapped segments */
827 rte_spinlock_recursive_lock(&user_mem_maps->lock);
829 /* this IOMMU type may not support DMA mapping, but
830 * if we have mappings in the list - that means we have
831 * previously mapped something successfully, so we can
832 * be sure that DMA mapping is supported.
834 for (i = 0; i < user_mem_maps->n_maps; i++) {
835 struct user_mem_map *map;
836 map = &user_mem_maps->maps[i];
838 ret = t->dma_user_map_func(
840 map->addr, map->iova, map->len,
843 RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
845 "iova: 0x%" PRIx64 " "
846 "len: 0x%" PRIu64 "\n",
847 map->addr, map->iova,
849 rte_spinlock_recursive_unlock(
850 &user_mem_maps->lock);
851 rte_mcfg_mem_read_unlock();
855 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
857 /* register callback for mem events */
858 if (vfio_cfg == default_vfio_cfg)
859 ret = rte_mem_event_callback_register(
860 VFIO_MEM_EVENT_CLB_NAME,
861 vfio_mem_event_callback, NULL);
864 /* unlock memory hotplug */
865 rte_mcfg_mem_read_unlock();
867 if (ret && rte_errno != ENOTSUP) {
868 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
872 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
874 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
876 } else if (rte_eal_process_type() != RTE_PROC_PRIMARY &&
877 vfio_cfg == default_vfio_cfg &&
878 vfio_cfg->vfio_iommu_type == NULL) {
879 /* if we're not a primary process, we do not set up the VFIO
880 * container because it's already been set up by the primary
881 * process. instead, we simply ask the primary about VFIO type
882 * we are using, and set the VFIO config up appropriately.
884 ret = vfio_sync_default_container();
886 RTE_LOG(ERR, EAL, "Could not sync default VFIO container\n");
887 close(vfio_group_fd);
888 rte_vfio_clear_group(vfio_group_fd);
891 /* we have successfully initialized VFIO, notify user */
892 const struct vfio_iommu_type *t =
893 default_vfio_cfg->vfio_iommu_type;
894 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
895 t->type_id, t->name);
898 /* get a file descriptor for the device */
899 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
900 if (*vfio_dev_fd < 0) {
901 /* if we cannot get a device fd, this implies a problem with
902 * the VFIO group or the container not having IOMMU configured.
905 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
907 close(vfio_group_fd);
908 rte_vfio_clear_group(vfio_group_fd);
912 /* test and setup the device */
913 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
915 RTE_LOG(ERR, EAL, " %s cannot get device info, "
916 "error %i (%s)\n", dev_addr, errno,
919 close(vfio_group_fd);
920 rte_vfio_clear_group(vfio_group_fd);
923 vfio_group_device_get(vfio_group_fd);
929 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
932 struct vfio_group_status group_status = {
933 .argsz = sizeof(group_status)
935 struct vfio_config *vfio_cfg;
940 /* we don't want any DMA mapping messages to come while we're detaching
941 * VFIO device, because this might be the last device and we might need
942 * to unregister the callback.
944 rte_mcfg_mem_read_lock();
946 /* get group number */
947 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
949 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
951 /* This is an error at this point. */
956 /* get the actual group fd */
957 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
958 if (vfio_group_fd <= 0) {
959 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
965 /* get the vfio_config it belongs to */
966 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
967 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
969 /* At this point we got an active group. Closing it will make the
970 * container detachment. If this is the last active group, VFIO kernel
971 * code will unset the container and the IOMMU mappings.
974 /* Closing a device */
975 if (close(vfio_dev_fd) < 0) {
976 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
982 /* An VFIO group can have several devices attached. Just when there is
983 * no devices remaining should the group be closed.
985 vfio_group_device_put(vfio_group_fd);
986 if (!vfio_group_device_count(vfio_group_fd)) {
988 if (close(vfio_group_fd) < 0) {
989 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
995 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
996 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
1003 /* if there are no active device groups, unregister the callback to
1004 * avoid spurious attempts to map/unmap memory from VFIO.
1006 if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0 &&
1007 rte_eal_process_type() != RTE_PROC_SECONDARY)
1008 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
1015 rte_mcfg_mem_read_unlock();
1020 rte_vfio_enable(const char *modname)
1022 /* initialize group list */
1026 rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
1028 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
1029 vfio_cfgs[i].vfio_container_fd = -1;
1030 vfio_cfgs[i].vfio_active_groups = 0;
1031 vfio_cfgs[i].vfio_iommu_type = NULL;
1032 vfio_cfgs[i].mem_maps.lock = lock;
1034 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
1035 vfio_cfgs[i].vfio_groups[j].fd = -1;
1036 vfio_cfgs[i].vfio_groups[j].group_num = -1;
1037 vfio_cfgs[i].vfio_groups[j].devices = 0;
1041 /* inform the user that we are probing for VFIO */
1042 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
1044 /* check if vfio module is loaded */
1045 vfio_available = rte_eal_check_module(modname);
1047 /* return error directly */
1048 if (vfio_available == -1) {
1049 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
1053 /* return 0 if VFIO modules not loaded */
1054 if (vfio_available == 0) {
1055 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
1056 "skipping VFIO support...\n");
1060 if (internal_config.process_type == RTE_PROC_PRIMARY) {
1061 /* open a new container */
1062 default_vfio_cfg->vfio_container_fd =
1063 rte_vfio_get_container_fd();
1065 /* get the default container from the primary process */
1066 default_vfio_cfg->vfio_container_fd =
1067 vfio_get_default_container_fd();
1070 /* check if we have VFIO driver enabled */
1071 if (default_vfio_cfg->vfio_container_fd != -1) {
1072 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
1073 default_vfio_cfg->vfio_enabled = 1;
1075 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
1082 rte_vfio_is_enabled(const char *modname)
1084 const int mod_available = rte_eal_check_module(modname) > 0;
1085 return default_vfio_cfg->vfio_enabled && mod_available;
1089 vfio_get_default_container_fd(void)
1091 struct rte_mp_msg mp_req, *mp_rep;
1092 struct rte_mp_reply mp_reply = {0};
1093 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1094 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1097 if (default_vfio_cfg->vfio_enabled)
1098 return default_vfio_cfg->vfio_container_fd;
1100 if (internal_config.process_type == RTE_PROC_PRIMARY) {
1101 /* if we were secondary process we would try requesting
1102 * container fd from the primary, but we're the primary
1103 * process so just exit here
1108 p->req = SOCKET_REQ_DEFAULT_CONTAINER;
1109 strcpy(mp_req.name, EAL_VFIO_MP);
1110 mp_req.len_param = sizeof(*p);
1113 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1114 mp_reply.nb_received == 1) {
1115 mp_rep = &mp_reply.msgs[0];
1116 p = (struct vfio_mp_param *)mp_rep->param;
1117 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1118 container_fd = mp_rep->fds[0];
1119 free(mp_reply.msgs);
1120 return container_fd;
1124 free(mp_reply.msgs);
1125 RTE_LOG(ERR, EAL, " cannot request default container fd\n");
1130 vfio_get_iommu_type(void)
1132 if (default_vfio_cfg->vfio_iommu_type == NULL)
1135 return default_vfio_cfg->vfio_iommu_type->type_id;
1138 const struct vfio_iommu_type *
1139 vfio_set_iommu_type(int vfio_container_fd)
1142 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1143 const struct vfio_iommu_type *t = &iommu_types[idx];
1145 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
1148 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
1149 t->type_id, t->name);
1152 /* not an error, there may be more supported IOMMU types */
1153 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
1154 "error %i (%s)\n", t->type_id, t->name, errno,
1157 /* if we didn't find a suitable IOMMU type, fail */
1162 vfio_has_supported_extensions(int vfio_container_fd)
1165 unsigned idx, n_extensions = 0;
1166 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1167 const struct vfio_iommu_type *t = &iommu_types[idx];
1169 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1172 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
1173 "error %i (%s)\n", errno,
1175 close(vfio_container_fd);
1177 } else if (ret == 1) {
1178 /* we found a supported extension */
1181 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
1182 t->type_id, t->name,
1183 ret ? "supported" : "not supported");
1186 /* if we didn't find any supported IOMMU types, fail */
1187 if (!n_extensions) {
1188 close(vfio_container_fd);
1196 rte_vfio_get_container_fd(void)
1198 int ret, vfio_container_fd;
1199 struct rte_mp_msg mp_req, *mp_rep;
1200 struct rte_mp_reply mp_reply = {0};
1201 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1202 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1205 /* if we're in a primary process, try to open the container */
1206 if (internal_config.process_type == RTE_PROC_PRIMARY) {
1207 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1208 if (vfio_container_fd < 0) {
1209 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
1210 "error %i (%s)\n", errno, strerror(errno));
1214 /* check VFIO API version */
1215 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1216 if (ret != VFIO_API_VERSION) {
1218 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
1219 "error %i (%s)\n", errno, strerror(errno));
1221 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
1222 close(vfio_container_fd);
1226 ret = vfio_has_supported_extensions(vfio_container_fd);
1228 RTE_LOG(ERR, EAL, " no supported IOMMU "
1229 "extensions found!\n");
1233 return vfio_container_fd;
1236 * if we're in a secondary process, request container fd from the
1237 * primary process via mp channel
1239 p->req = SOCKET_REQ_CONTAINER;
1240 strcpy(mp_req.name, EAL_VFIO_MP);
1241 mp_req.len_param = sizeof(*p);
1244 vfio_container_fd = -1;
1245 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1246 mp_reply.nb_received == 1) {
1247 mp_rep = &mp_reply.msgs[0];
1248 p = (struct vfio_mp_param *)mp_rep->param;
1249 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1250 vfio_container_fd = mp_rep->fds[0];
1251 free(mp_reply.msgs);
1252 return vfio_container_fd;
1256 free(mp_reply.msgs);
1257 RTE_LOG(ERR, EAL, " cannot request container fd\n");
1262 rte_vfio_get_group_num(const char *sysfs_base,
1263 const char *dev_addr, int *iommu_group_num)
1265 char linkname[PATH_MAX];
1266 char filename[PATH_MAX];
1267 char *tok[16], *group_tok, *end;
1270 memset(linkname, 0, sizeof(linkname));
1271 memset(filename, 0, sizeof(filename));
1273 /* try to find out IOMMU group for this device */
1274 snprintf(linkname, sizeof(linkname),
1275 "%s/%s/iommu_group", sysfs_base, dev_addr);
1277 ret = readlink(linkname, filename, sizeof(filename));
1279 /* if the link doesn't exist, no VFIO for us */
1283 ret = rte_strsplit(filename, sizeof(filename),
1284 tok, RTE_DIM(tok), '/');
1287 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
1291 /* IOMMU group is always the last token */
1293 group_tok = tok[ret - 1];
1295 *iommu_group_num = strtol(group_tok, &end, 10);
1296 if ((end != group_tok && *end != '\0') || errno != 0) {
1297 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
1305 type1_map_contig(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1306 size_t len, void *arg)
1308 int *vfio_container_fd = arg;
1313 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1318 type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1321 int *vfio_container_fd = arg;
1323 /* skip external memory that isn't a heap */
1324 if (msl->external && !msl->heap)
1327 /* skip any segments with invalid IOVA addresses */
1328 if (ms->iova == RTE_BAD_IOVA)
1331 /* if IOVA mode is VA, we've already mapped the internal segments */
1332 if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
1335 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1340 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1341 uint64_t len, int do_map)
1343 struct vfio_iommu_type1_dma_map dma_map;
1344 struct vfio_iommu_type1_dma_unmap dma_unmap;
1348 memset(&dma_map, 0, sizeof(dma_map));
1349 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1350 dma_map.vaddr = vaddr;
1352 dma_map.iova = iova;
1353 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1354 VFIO_DMA_MAP_FLAG_WRITE;
1356 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1359 * In case the mapping was already done EEXIST will be
1360 * returned from kernel.
1362 if (errno == EEXIST) {
1364 " Memory segment is already mapped,"
1368 " cannot set up DMA remapping,"
1370 errno, strerror(errno));
1375 memset(&dma_unmap, 0, sizeof(dma_unmap));
1376 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1377 dma_unmap.size = len;
1378 dma_unmap.iova = iova;
1380 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1383 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1384 errno, strerror(errno));
1393 vfio_type1_dma_map(int vfio_container_fd)
1395 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
1396 /* with IOVA as VA mode, we can get away with mapping contiguous
1397 * chunks rather than going page-by-page.
1399 int ret = rte_memseg_contig_walk(type1_map_contig,
1400 &vfio_container_fd);
1403 /* we have to continue the walk because we've skipped the
1404 * external segments during the config walk.
1407 return rte_memseg_walk(type1_map, &vfio_container_fd);
1411 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1412 uint64_t len, int do_map)
1414 struct vfio_iommu_type1_dma_map dma_map;
1415 struct vfio_iommu_type1_dma_unmap dma_unmap;
1417 struct vfio_iommu_spapr_register_memory reg = {
1418 .argsz = sizeof(reg),
1421 reg.vaddr = (uintptr_t) vaddr;
1425 ret = ioctl(vfio_container_fd,
1426 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
1428 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
1429 "error %i (%s)\n", errno, strerror(errno));
1433 memset(&dma_map, 0, sizeof(dma_map));
1434 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1435 dma_map.vaddr = vaddr;
1437 dma_map.iova = iova;
1438 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1439 VFIO_DMA_MAP_FLAG_WRITE;
1441 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1444 * In case the mapping was already done EBUSY will be
1445 * returned from kernel.
1447 if (errno == EBUSY) {
1449 " Memory segment is already mapped,"
1453 " cannot set up DMA remapping,"
1454 " error %i (%s)\n", errno,
1461 memset(&dma_unmap, 0, sizeof(dma_unmap));
1462 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1463 dma_unmap.size = len;
1464 dma_unmap.iova = iova;
1466 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1469 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1470 errno, strerror(errno));
1474 ret = ioctl(vfio_container_fd,
1475 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
1477 RTE_LOG(ERR, EAL, " cannot unregister vaddr for IOMMU, error %i (%s)\n",
1478 errno, strerror(errno));
1487 vfio_spapr_map_walk(const struct rte_memseg_list *msl,
1488 const struct rte_memseg *ms, void *arg)
1490 int *vfio_container_fd = arg;
1492 /* skip external memory that isn't a heap */
1493 if (msl->external && !msl->heap)
1496 /* skip any segments with invalid IOVA addresses */
1497 if (ms->iova == RTE_BAD_IOVA)
1500 return vfio_spapr_dma_do_map(*vfio_container_fd, ms->addr_64, ms->iova,
1505 vfio_spapr_unmap_walk(const struct rte_memseg_list *msl,
1506 const struct rte_memseg *ms, void *arg)
1508 int *vfio_container_fd = arg;
1510 /* skip external memory that isn't a heap */
1511 if (msl->external && !msl->heap)
1514 /* skip any segments with invalid IOVA addresses */
1515 if (ms->iova == RTE_BAD_IOVA)
1518 return vfio_spapr_dma_do_map(*vfio_container_fd, ms->addr_64, ms->iova,
1522 struct spapr_walk_param {
1523 uint64_t window_size;
1524 uint64_t hugepage_sz;
1528 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl,
1529 const struct rte_memseg *ms, void *arg)
1531 struct spapr_walk_param *param = arg;
1532 uint64_t max = ms->iova + ms->len;
1534 /* skip external memory that isn't a heap */
1535 if (msl->external && !msl->heap)
1538 /* skip any segments with invalid IOVA addresses */
1539 if (ms->iova == RTE_BAD_IOVA)
1542 if (max > param->window_size) {
1543 param->hugepage_sz = ms->hugepage_sz;
1544 param->window_size = max;
1551 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1552 struct vfio_iommu_spapr_tce_create *create) {
1553 struct vfio_iommu_spapr_tce_remove remove = {
1554 .argsz = sizeof(remove),
1556 struct vfio_iommu_spapr_tce_info info = {
1557 .argsz = sizeof(info),
1561 /* query spapr iommu info */
1562 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1564 RTE_LOG(ERR, EAL, " cannot get iommu info, "
1565 "error %i (%s)\n", errno, strerror(errno));
1569 /* remove default DMA of 32 bit window */
1570 remove.start_addr = info.dma32_window_start;
1571 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1573 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
1574 "error %i (%s)\n", errno, strerror(errno));
1578 /* create new DMA window */
1579 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1581 #ifdef VFIO_IOMMU_SPAPR_INFO_DDW
1582 /* try possible page_shift and levels for workaround */
1585 for (levels = create->levels + 1;
1586 ret && levels <= info.ddw.levels; levels++) {
1587 create->levels = levels;
1588 ret = ioctl(vfio_container_fd,
1589 VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1593 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
1594 "error %i (%s)\n", errno, strerror(errno));
1599 if (create->start_addr != 0) {
1600 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
1608 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1609 uint64_t len, int do_map)
1611 struct spapr_walk_param param;
1612 struct vfio_iommu_spapr_tce_create create = {
1613 .argsz = sizeof(create),
1615 struct vfio_config *vfio_cfg;
1616 struct user_mem_maps *user_mem_maps;
1619 vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1620 if (vfio_cfg == NULL) {
1621 RTE_LOG(ERR, EAL, " invalid container fd!\n");
1625 user_mem_maps = &vfio_cfg->mem_maps;
1626 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1628 /* check if window size needs to be adjusted */
1629 memset(¶m, 0, sizeof(param));
1631 /* we're inside a callback so use thread-unsafe version */
1632 if (rte_memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1634 RTE_LOG(ERR, EAL, "Could not get window size\n");
1639 /* also check user maps */
1640 for (i = 0; i < user_mem_maps->n_maps; i++) {
1641 uint64_t max = user_mem_maps->maps[i].iova +
1642 user_mem_maps->maps[i].len;
1643 param.window_size = RTE_MAX(param.window_size, max);
1646 /* sPAPR requires window size to be a power of 2 */
1647 create.window_size = rte_align64pow2(param.window_size);
1648 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1652 /* re-create window and remap the entire memory */
1653 if (iova + len > create.window_size) {
1654 /* release all maps before recreating the window */
1655 if (rte_memseg_walk_thread_unsafe(vfio_spapr_unmap_walk,
1656 &vfio_container_fd) < 0) {
1657 RTE_LOG(ERR, EAL, "Could not release DMA maps\n");
1661 /* release all user maps */
1662 for (i = 0; i < user_mem_maps->n_maps; i++) {
1663 struct user_mem_map *map =
1664 &user_mem_maps->maps[i];
1665 if (vfio_spapr_dma_do_map(vfio_container_fd,
1666 map->addr, map->iova, map->len,
1668 RTE_LOG(ERR, EAL, "Could not release user DMA maps\n");
1673 create.window_size = rte_align64pow2(iova + len);
1674 if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1676 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1680 /* we're inside a callback, so use thread-unsafe version
1682 if (rte_memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1683 &vfio_container_fd) < 0) {
1684 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1688 /* remap all user maps */
1689 for (i = 0; i < user_mem_maps->n_maps; i++) {
1690 struct user_mem_map *map =
1691 &user_mem_maps->maps[i];
1692 if (vfio_spapr_dma_do_map(vfio_container_fd,
1693 map->addr, map->iova, map->len,
1695 RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1701 if (vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 1)) {
1702 RTE_LOG(ERR, EAL, "Failed to map DMA\n");
1707 /* for unmap, check if iova within DMA window */
1708 if (iova > create.window_size) {
1709 RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1714 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1717 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1722 vfio_spapr_dma_map(int vfio_container_fd)
1724 struct vfio_iommu_spapr_tce_create create = {
1725 .argsz = sizeof(create),
1727 struct spapr_walk_param param;
1729 memset(¶m, 0, sizeof(param));
1731 /* create DMA window from 0 to max(phys_addr + len) */
1732 rte_memseg_walk(vfio_spapr_window_size_walk, ¶m);
1734 /* sPAPR requires window size to be a power of 2 */
1735 create.window_size = rte_align64pow2(param.window_size);
1736 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1739 if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1740 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1744 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1745 if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1752 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1754 /* No-IOMMU mode does not need DMA mapping */
1759 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1760 uint64_t __rte_unused vaddr,
1761 uint64_t __rte_unused iova, uint64_t __rte_unused len,
1762 int __rte_unused do_map)
1764 /* No-IOMMU mode does not need DMA mapping */
1769 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1770 uint64_t len, int do_map)
1772 const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1775 RTE_LOG(ERR, EAL, " VFIO support not initialized\n");
1780 if (!t->dma_user_map_func) {
1782 " VFIO custom DMA region maping not supported by IOMMU %s\n",
1784 rte_errno = ENOTSUP;
1788 return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1793 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1796 struct user_mem_map *new_map;
1797 struct user_mem_maps *user_mem_maps;
1800 user_mem_maps = &vfio_cfg->mem_maps;
1801 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1802 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1803 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1809 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1810 /* technically, this will fail if there are currently no devices
1811 * plugged in, even if a device were added later, this mapping
1812 * might have succeeded. however, since we cannot verify if this
1813 * is a valid mapping without having a device attached, consider
1814 * this to be unsupported, because we can't just store any old
1815 * mapping and pollute list of active mappings willy-nilly.
1817 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1821 /* create new user mem map entry */
1822 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1823 new_map->addr = vaddr;
1824 new_map->iova = iova;
1827 compact_user_maps(user_mem_maps);
1829 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1834 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1837 struct user_mem_map *map, *new_map = NULL;
1838 struct user_mem_maps *user_mem_maps;
1841 user_mem_maps = &vfio_cfg->mem_maps;
1842 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1844 /* find our mapping */
1845 map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1847 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1852 if (map->addr != vaddr || map->iova != iova || map->len != len) {
1853 /* we're partially unmapping a previously mapped region, so we
1854 * need to split entry into two.
1856 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1857 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1862 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1865 /* unmap the entry */
1866 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1867 /* there may not be any devices plugged in, so unmapping will
1868 * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1869 * stop us from removing the mapping, as the assumption is we
1870 * won't be needing this memory any more and thus will want to
1871 * prevent it from being remapped again on hotplug. so, only
1872 * fail if we indeed failed to unmap (e.g. if the mapping was
1873 * within our mapped range but had invalid alignment).
1875 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1876 RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1880 RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1883 /* remove map from the list of active mappings */
1884 if (new_map != NULL) {
1885 adjust_map(map, new_map, vaddr, len);
1887 /* if we've created a new map by splitting, sort everything */
1888 if (!is_null_map(new_map)) {
1889 compact_user_maps(user_mem_maps);
1891 /* we've created a new mapping, but it was unused */
1892 user_mem_maps->n_maps--;
1895 memset(map, 0, sizeof(*map));
1896 compact_user_maps(user_mem_maps);
1897 user_mem_maps->n_maps--;
1901 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1906 rte_vfio_noiommu_is_enabled(void)
1912 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1914 if (errno != ENOENT) {
1915 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
1916 errno, strerror(errno));
1920 * else the file does not exists
1921 * i.e. noiommu is not enabled
1926 cnt = read(fd, &c, 1);
1929 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
1930 "file %i (%s)\n", errno, strerror(errno));
1938 rte_vfio_container_create(void)
1942 /* Find an empty slot to store new vfio config */
1943 for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1944 if (vfio_cfgs[i].vfio_container_fd == -1)
1948 if (i == VFIO_MAX_CONTAINERS) {
1949 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1953 vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1954 if (vfio_cfgs[i].vfio_container_fd < 0) {
1955 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1959 return vfio_cfgs[i].vfio_container_fd;
1963 rte_vfio_container_destroy(int container_fd)
1965 struct vfio_config *vfio_cfg;
1968 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1969 if (vfio_cfg == NULL) {
1970 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1974 for (i = 0; i < VFIO_MAX_GROUPS; i++)
1975 if (vfio_cfg->vfio_groups[i].group_num != -1)
1976 rte_vfio_container_group_unbind(container_fd,
1977 vfio_cfg->vfio_groups[i].group_num);
1979 close(container_fd);
1980 vfio_cfg->vfio_container_fd = -1;
1981 vfio_cfg->vfio_active_groups = 0;
1982 vfio_cfg->vfio_iommu_type = NULL;
1988 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1990 struct vfio_config *vfio_cfg;
1992 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1993 if (vfio_cfg == NULL) {
1994 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1998 return vfio_get_group_fd(vfio_cfg, iommu_group_num);
2002 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
2004 struct vfio_config *vfio_cfg;
2005 struct vfio_group *cur_grp = NULL;
2008 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2009 if (vfio_cfg == NULL) {
2010 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2014 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
2015 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
2016 cur_grp = &vfio_cfg->vfio_groups[i];
2021 /* This should not happen */
2022 if (i == VFIO_MAX_GROUPS || cur_grp == NULL) {
2023 RTE_LOG(ERR, EAL, "Specified group number not found\n");
2027 if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
2028 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
2029 " iommu_group_num %d\n", iommu_group_num);
2032 cur_grp->group_num = -1;
2034 cur_grp->devices = 0;
2035 vfio_cfg->vfio_active_groups--;
2041 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
2044 struct vfio_config *vfio_cfg;
2051 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2052 if (vfio_cfg == NULL) {
2053 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2057 return container_dma_map(vfio_cfg, vaddr, iova, len);
2061 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
2064 struct vfio_config *vfio_cfg;
2071 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2072 if (vfio_cfg == NULL) {
2073 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2077 return container_dma_unmap(vfio_cfg, vaddr, iova, len);
2083 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
2084 __rte_unused const char *dev_addr,
2085 __rte_unused int *vfio_dev_fd,
2086 __rte_unused struct vfio_device_info *device_info)
2092 rte_vfio_release_device(__rte_unused const char *sysfs_base,
2093 __rte_unused const char *dev_addr, __rte_unused int fd)
2099 rte_vfio_enable(__rte_unused const char *modname)
2105 rte_vfio_is_enabled(__rte_unused const char *modname)
2111 rte_vfio_noiommu_is_enabled(void)
2117 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
2123 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
2124 __rte_unused const char *dev_addr,
2125 __rte_unused int *iommu_group_num)
2131 rte_vfio_get_container_fd(void)
2137 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
2143 rte_vfio_container_create(void)
2149 rte_vfio_container_destroy(__rte_unused int container_fd)
2155 rte_vfio_container_group_bind(__rte_unused int container_fd,
2156 __rte_unused int iommu_group_num)
2162 rte_vfio_container_group_unbind(__rte_unused int container_fd,
2163 __rte_unused int iommu_group_num)
2169 rte_vfio_container_dma_map(__rte_unused int container_fd,
2170 __rte_unused uint64_t vaddr,
2171 __rte_unused uint64_t iova,
2172 __rte_unused uint64_t len)
2178 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
2179 __rte_unused uint64_t vaddr,
2180 __rte_unused uint64_t iova,
2181 __rte_unused uint64_t len)
2186 #endif /* VFIO_PRESENT */