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;
270 const struct internal_config *internal_conf =
271 eal_get_internal_configuration();
273 /* if primary, try to open the group */
274 if (internal_conf->process_type == RTE_PROC_PRIMARY) {
275 /* try regular group format */
276 snprintf(filename, sizeof(filename),
277 VFIO_GROUP_FMT, iommu_group_num);
278 vfio_group_fd = open(filename, O_RDWR);
279 if (vfio_group_fd < 0) {
280 /* if file not found, it's not an error */
281 if (errno != ENOENT) {
282 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
287 /* special case: try no-IOMMU path as well */
288 snprintf(filename, sizeof(filename),
289 VFIO_NOIOMMU_GROUP_FMT,
291 vfio_group_fd = open(filename, O_RDWR);
292 if (vfio_group_fd < 0) {
293 if (errno != ENOENT) {
294 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
300 /* noiommu group found */
303 return vfio_group_fd;
305 /* if we're in a secondary process, request group fd from the primary
306 * process via mp channel.
308 p->req = SOCKET_REQ_GROUP;
309 p->group_num = iommu_group_num;
310 strcpy(mp_req.name, EAL_VFIO_MP);
311 mp_req.len_param = sizeof(*p);
315 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
316 mp_reply.nb_received == 1) {
317 mp_rep = &mp_reply.msgs[0];
318 p = (struct vfio_mp_param *)mp_rep->param;
319 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
320 vfio_group_fd = mp_rep->fds[0];
321 } else if (p->result == SOCKET_NO_FD) {
322 RTE_LOG(ERR, EAL, " bad VFIO group fd\n");
328 if (vfio_group_fd < 0)
329 RTE_LOG(ERR, EAL, " cannot request group fd\n");
330 return vfio_group_fd;
333 static struct vfio_config *
334 get_vfio_cfg_by_group_num(int iommu_group_num)
336 struct vfio_config *vfio_cfg;
339 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
340 vfio_cfg = &vfio_cfgs[i];
341 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
342 if (vfio_cfg->vfio_groups[j].group_num ==
352 vfio_get_group_fd(struct vfio_config *vfio_cfg,
357 struct vfio_group *cur_grp;
359 /* check if we already have the group descriptor open */
360 for (i = 0; i < VFIO_MAX_GROUPS; i++)
361 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
362 return vfio_cfg->vfio_groups[i].fd;
364 /* Lets see first if there is room for a new group */
365 if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
366 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
370 /* Now lets get an index for the new group */
371 for (i = 0; i < VFIO_MAX_GROUPS; i++)
372 if (vfio_cfg->vfio_groups[i].group_num == -1) {
373 cur_grp = &vfio_cfg->vfio_groups[i];
377 /* This should not happen */
378 if (i == VFIO_MAX_GROUPS) {
379 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
383 vfio_group_fd = vfio_open_group_fd(iommu_group_num);
384 if (vfio_group_fd <= 0) {
385 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
389 cur_grp->group_num = iommu_group_num;
390 cur_grp->fd = vfio_group_fd;
391 vfio_cfg->vfio_active_groups++;
393 return vfio_group_fd;
396 static struct vfio_config *
397 get_vfio_cfg_by_group_fd(int vfio_group_fd)
399 struct vfio_config *vfio_cfg;
402 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
403 vfio_cfg = &vfio_cfgs[i];
404 for (j = 0; j < VFIO_MAX_GROUPS; j++)
405 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
412 static struct vfio_config *
413 get_vfio_cfg_by_container_fd(int container_fd)
417 if (container_fd == RTE_VFIO_DEFAULT_CONTAINER_FD)
418 return default_vfio_cfg;
420 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
421 if (vfio_cfgs[i].vfio_container_fd == container_fd)
422 return &vfio_cfgs[i];
429 rte_vfio_get_group_fd(int iommu_group_num)
431 struct vfio_config *vfio_cfg;
433 /* get the vfio_config it belongs to */
434 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
435 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
437 return vfio_get_group_fd(vfio_cfg, iommu_group_num);
441 get_vfio_group_idx(int vfio_group_fd)
443 struct vfio_config *vfio_cfg;
446 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
447 vfio_cfg = &vfio_cfgs[i];
448 for (j = 0; j < VFIO_MAX_GROUPS; j++)
449 if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
457 vfio_group_device_get(int vfio_group_fd)
459 struct vfio_config *vfio_cfg;
462 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
463 if (vfio_cfg == NULL) {
464 RTE_LOG(ERR, EAL, " invalid group fd!\n");
468 i = get_vfio_group_idx(vfio_group_fd);
469 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
470 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
472 vfio_cfg->vfio_groups[i].devices++;
476 vfio_group_device_put(int vfio_group_fd)
478 struct vfio_config *vfio_cfg;
481 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
482 if (vfio_cfg == NULL) {
483 RTE_LOG(ERR, EAL, " invalid group fd!\n");
487 i = get_vfio_group_idx(vfio_group_fd);
488 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
489 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
491 vfio_cfg->vfio_groups[i].devices--;
495 vfio_group_device_count(int vfio_group_fd)
497 struct vfio_config *vfio_cfg;
500 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
501 if (vfio_cfg == NULL) {
502 RTE_LOG(ERR, EAL, " invalid group fd!\n");
506 i = get_vfio_group_idx(vfio_group_fd);
507 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
508 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
512 return vfio_cfg->vfio_groups[i].devices;
516 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
517 void *arg __rte_unused)
519 rte_iova_t iova_start, iova_expected;
520 struct rte_memseg_list *msl;
521 struct rte_memseg *ms;
525 msl = rte_mem_virt2memseg_list(addr);
527 /* for IOVA as VA mode, no need to care for IOVA addresses */
528 if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
529 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
530 if (type == RTE_MEM_EVENT_ALLOC)
531 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
534 vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
539 #ifdef RTE_ARCH_PPC_64
540 ms = rte_mem_virt2memseg(addr, msl);
541 while (cur_len < len) {
542 int idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
544 rte_fbarray_set_free(&msl->memseg_arr, idx);
550 /* memsegs are contiguous in memory */
551 ms = rte_mem_virt2memseg(addr, msl);
554 * This memory is not guaranteed to be contiguous, but it still could
555 * be, or it could have some small contiguous chunks. Since the number
556 * of VFIO mappings is limited, and VFIO appears to not concatenate
557 * adjacent mappings, we have to do this ourselves.
559 * So, find contiguous chunks, then map them.
561 va_start = ms->addr_64;
562 iova_start = iova_expected = ms->iova;
563 while (cur_len < len) {
564 bool new_contig_area = ms->iova != iova_expected;
565 bool last_seg = (len - cur_len) == ms->len;
566 bool skip_last = false;
568 /* only do mappings when current contiguous area ends */
569 if (new_contig_area) {
570 if (type == RTE_MEM_EVENT_ALLOC)
571 vfio_dma_mem_map(default_vfio_cfg, va_start,
573 iova_expected - iova_start, 1);
575 vfio_dma_mem_map(default_vfio_cfg, va_start,
577 iova_expected - iova_start, 0);
578 va_start = ms->addr_64;
579 iova_start = ms->iova;
581 /* some memory segments may have invalid IOVA */
582 if (ms->iova == RTE_BAD_IOVA) {
583 RTE_LOG(DEBUG, EAL, "Memory segment at %p has bad IOVA, skipping\n",
587 iova_expected = ms->iova + ms->len;
592 * don't count previous segment, and don't attempt to
593 * dereference a potentially invalid pointer.
595 if (skip_last && !last_seg) {
596 iova_expected = iova_start = ms->iova;
597 va_start = ms->addr_64;
598 } else if (!skip_last && last_seg) {
599 /* this is the last segment and we're not skipping */
600 if (type == RTE_MEM_EVENT_ALLOC)
601 vfio_dma_mem_map(default_vfio_cfg, va_start,
603 iova_expected - iova_start, 1);
605 vfio_dma_mem_map(default_vfio_cfg, va_start,
607 iova_expected - iova_start, 0);
610 #ifdef RTE_ARCH_PPC_64
612 ms = rte_mem_virt2memseg(addr, msl);
613 while (cur_len < len) {
614 int idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
616 rte_fbarray_set_used(&msl->memseg_arr, idx);
624 vfio_sync_default_container(void)
626 struct rte_mp_msg mp_req, *mp_rep;
627 struct rte_mp_reply mp_reply = {0};
628 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
629 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
633 /* cannot be called from primary */
634 if (rte_eal_process_type() != RTE_PROC_SECONDARY)
637 /* default container fd should have been opened in rte_vfio_enable() */
638 if (!default_vfio_cfg->vfio_enabled ||
639 default_vfio_cfg->vfio_container_fd < 0) {
640 RTE_LOG(ERR, EAL, "VFIO support is not initialized\n");
644 /* find default container's IOMMU type */
645 p->req = SOCKET_REQ_IOMMU_TYPE;
646 strcpy(mp_req.name, EAL_VFIO_MP);
647 mp_req.len_param = sizeof(*p);
651 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
652 mp_reply.nb_received == 1) {
653 mp_rep = &mp_reply.msgs[0];
654 p = (struct vfio_mp_param *)mp_rep->param;
655 if (p->result == SOCKET_OK)
656 iommu_type_id = p->iommu_type_id;
659 if (iommu_type_id < 0) {
660 RTE_LOG(ERR, EAL, "Could not get IOMMU type for default container\n");
664 /* we now have an fd for default container, as well as its IOMMU type.
665 * now, set up default VFIO container config to match.
667 for (i = 0; i < RTE_DIM(iommu_types); i++) {
668 const struct vfio_iommu_type *t = &iommu_types[i];
669 if (t->type_id != iommu_type_id)
672 /* we found our IOMMU type */
673 default_vfio_cfg->vfio_iommu_type = t;
677 RTE_LOG(ERR, EAL, "Could not find IOMMU type id (%i)\n",
683 rte_vfio_clear_group(int vfio_group_fd)
686 struct vfio_config *vfio_cfg;
688 vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
689 if (vfio_cfg == NULL) {
690 RTE_LOG(ERR, EAL, " invalid group fd!\n");
694 i = get_vfio_group_idx(vfio_group_fd);
697 vfio_cfg->vfio_groups[i].group_num = -1;
698 vfio_cfg->vfio_groups[i].fd = -1;
699 vfio_cfg->vfio_groups[i].devices = 0;
700 vfio_cfg->vfio_active_groups--;
706 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
707 int *vfio_dev_fd, struct vfio_device_info *device_info)
709 struct vfio_group_status group_status = {
710 .argsz = sizeof(group_status)
712 struct vfio_config *vfio_cfg;
713 struct user_mem_maps *user_mem_maps;
714 int vfio_container_fd;
718 const struct internal_config *internal_conf =
719 eal_get_internal_configuration();
721 /* get group number */
722 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
724 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
729 /* if negative, something failed */
733 /* get the actual group fd */
734 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
735 if (vfio_group_fd < 0)
738 /* if group_fd == 0, that means the device isn't managed by VFIO */
739 if (vfio_group_fd == 0) {
740 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
746 * at this point, we know that this group is viable (meaning, all devices
747 * are either bound to VFIO or not bound to anything)
750 /* check if the group is viable */
751 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
753 RTE_LOG(ERR, EAL, " %s cannot get group status, "
754 "error %i (%s)\n", dev_addr, errno, strerror(errno));
755 close(vfio_group_fd);
756 rte_vfio_clear_group(vfio_group_fd);
758 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
759 RTE_LOG(ERR, EAL, " %s VFIO group is not viable! "
760 "Not all devices in IOMMU group bound to VFIO or unbound\n",
762 close(vfio_group_fd);
763 rte_vfio_clear_group(vfio_group_fd);
767 /* get the vfio_config it belongs to */
768 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
769 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
770 vfio_container_fd = vfio_cfg->vfio_container_fd;
771 user_mem_maps = &vfio_cfg->mem_maps;
773 /* check if group does not have a container yet */
774 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
776 /* add group to a container */
777 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
780 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
781 "error %i (%s)\n", dev_addr, errno, strerror(errno));
782 close(vfio_group_fd);
783 rte_vfio_clear_group(vfio_group_fd);
788 * pick an IOMMU type and set up DMA mappings for container
790 * needs to be done only once, only when first group is
791 * assigned to a container and only in primary process.
792 * Note this can happen several times with the hotplug
795 if (internal_conf->process_type == RTE_PROC_PRIMARY &&
796 vfio_cfg->vfio_active_groups == 1 &&
797 vfio_group_device_count(vfio_group_fd) == 0) {
798 const struct vfio_iommu_type *t;
800 /* select an IOMMU type which we will be using */
801 t = vfio_set_iommu_type(vfio_container_fd);
804 " %s failed to select IOMMU type\n",
806 close(vfio_group_fd);
807 rte_vfio_clear_group(vfio_group_fd);
810 /* lock memory hotplug before mapping and release it
811 * after registering callback, to prevent races
813 rte_mcfg_mem_read_lock();
814 if (vfio_cfg == default_vfio_cfg)
815 ret = t->dma_map_func(vfio_container_fd);
820 " %s DMA remapping failed, error %i (%s)\n",
821 dev_addr, errno, strerror(errno));
822 close(vfio_group_fd);
823 rte_vfio_clear_group(vfio_group_fd);
824 rte_mcfg_mem_read_unlock();
828 vfio_cfg->vfio_iommu_type = t;
830 /* re-map all user-mapped segments */
831 rte_spinlock_recursive_lock(&user_mem_maps->lock);
833 /* this IOMMU type may not support DMA mapping, but
834 * if we have mappings in the list - that means we have
835 * previously mapped something successfully, so we can
836 * be sure that DMA mapping is supported.
838 for (i = 0; i < user_mem_maps->n_maps; i++) {
839 struct user_mem_map *map;
840 map = &user_mem_maps->maps[i];
842 ret = t->dma_user_map_func(
844 map->addr, map->iova, map->len,
847 RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
849 "iova: 0x%" PRIx64 " "
850 "len: 0x%" PRIu64 "\n",
851 map->addr, map->iova,
853 rte_spinlock_recursive_unlock(
854 &user_mem_maps->lock);
855 rte_mcfg_mem_read_unlock();
859 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
861 /* register callback for mem events */
862 if (vfio_cfg == default_vfio_cfg)
863 ret = rte_mem_event_callback_register(
864 VFIO_MEM_EVENT_CLB_NAME,
865 vfio_mem_event_callback, NULL);
868 /* unlock memory hotplug */
869 rte_mcfg_mem_read_unlock();
871 if (ret && rte_errno != ENOTSUP) {
872 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
876 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
878 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
880 } else if (rte_eal_process_type() != RTE_PROC_PRIMARY &&
881 vfio_cfg == default_vfio_cfg &&
882 vfio_cfg->vfio_iommu_type == NULL) {
883 /* if we're not a primary process, we do not set up the VFIO
884 * container because it's already been set up by the primary
885 * process. instead, we simply ask the primary about VFIO type
886 * we are using, and set the VFIO config up appropriately.
888 ret = vfio_sync_default_container();
890 RTE_LOG(ERR, EAL, "Could not sync default VFIO container\n");
891 close(vfio_group_fd);
892 rte_vfio_clear_group(vfio_group_fd);
895 /* we have successfully initialized VFIO, notify user */
896 const struct vfio_iommu_type *t =
897 default_vfio_cfg->vfio_iommu_type;
898 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
899 t->type_id, t->name);
902 /* get a file descriptor for the device */
903 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
904 if (*vfio_dev_fd < 0) {
905 /* if we cannot get a device fd, this implies a problem with
906 * the VFIO group or the container not having IOMMU configured.
909 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
911 close(vfio_group_fd);
912 rte_vfio_clear_group(vfio_group_fd);
916 /* test and setup the device */
917 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
919 RTE_LOG(ERR, EAL, " %s cannot get device info, "
920 "error %i (%s)\n", dev_addr, errno,
923 close(vfio_group_fd);
924 rte_vfio_clear_group(vfio_group_fd);
927 vfio_group_device_get(vfio_group_fd);
933 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
936 struct vfio_config *vfio_cfg;
941 /* we don't want any DMA mapping messages to come while we're detaching
942 * VFIO device, because this might be the last device and we might need
943 * to unregister the callback.
945 rte_mcfg_mem_read_lock();
947 /* get group number */
948 ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
950 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
952 /* This is an error at this point. */
957 /* get the actual group fd */
958 vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
959 if (vfio_group_fd <= 0) {
960 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
966 /* get the vfio_config it belongs to */
967 vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
968 vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
970 /* At this point we got an active group. Closing it will make the
971 * container detachment. If this is the last active group, VFIO kernel
972 * code will unset the container and the IOMMU mappings.
975 /* Closing a device */
976 if (close(vfio_dev_fd) < 0) {
977 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
983 /* An VFIO group can have several devices attached. Just when there is
984 * no devices remaining should the group be closed.
986 vfio_group_device_put(vfio_group_fd);
987 if (!vfio_group_device_count(vfio_group_fd)) {
989 if (close(vfio_group_fd) < 0) {
990 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
996 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
997 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
1004 /* if there are no active device groups, unregister the callback to
1005 * avoid spurious attempts to map/unmap memory from VFIO.
1007 if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0 &&
1008 rte_eal_process_type() != RTE_PROC_SECONDARY)
1009 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
1016 rte_mcfg_mem_read_unlock();
1021 rte_vfio_enable(const char *modname)
1023 /* initialize group list */
1026 const struct internal_config *internal_conf =
1027 eal_get_internal_configuration();
1029 rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
1031 for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
1032 vfio_cfgs[i].vfio_container_fd = -1;
1033 vfio_cfgs[i].vfio_active_groups = 0;
1034 vfio_cfgs[i].vfio_iommu_type = NULL;
1035 vfio_cfgs[i].mem_maps.lock = lock;
1037 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
1038 vfio_cfgs[i].vfio_groups[j].fd = -1;
1039 vfio_cfgs[i].vfio_groups[j].group_num = -1;
1040 vfio_cfgs[i].vfio_groups[j].devices = 0;
1044 /* inform the user that we are probing for VFIO */
1045 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
1047 /* check if vfio module is loaded */
1048 vfio_available = rte_eal_check_module(modname);
1050 /* return error directly */
1051 if (vfio_available == -1) {
1052 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
1056 /* return 0 if VFIO modules not loaded */
1057 if (vfio_available == 0) {
1058 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
1059 "skipping VFIO support...\n");
1063 if (internal_conf->process_type == RTE_PROC_PRIMARY) {
1064 /* open a new container */
1065 default_vfio_cfg->vfio_container_fd =
1066 rte_vfio_get_container_fd();
1068 /* get the default container from the primary process */
1069 default_vfio_cfg->vfio_container_fd =
1070 vfio_get_default_container_fd();
1073 /* check if we have VFIO driver enabled */
1074 if (default_vfio_cfg->vfio_container_fd != -1) {
1075 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
1076 default_vfio_cfg->vfio_enabled = 1;
1078 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
1085 rte_vfio_is_enabled(const char *modname)
1087 const int mod_available = rte_eal_check_module(modname) > 0;
1088 return default_vfio_cfg->vfio_enabled && mod_available;
1092 vfio_get_default_container_fd(void)
1094 struct rte_mp_msg mp_req, *mp_rep;
1095 struct rte_mp_reply mp_reply = {0};
1096 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1097 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1099 const struct internal_config *internal_conf =
1100 eal_get_internal_configuration();
1102 if (default_vfio_cfg->vfio_enabled)
1103 return default_vfio_cfg->vfio_container_fd;
1105 if (internal_conf->process_type == RTE_PROC_PRIMARY) {
1106 /* if we were secondary process we would try requesting
1107 * container fd from the primary, but we're the primary
1108 * process so just exit here
1113 p->req = SOCKET_REQ_DEFAULT_CONTAINER;
1114 strcpy(mp_req.name, EAL_VFIO_MP);
1115 mp_req.len_param = sizeof(*p);
1118 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1119 mp_reply.nb_received == 1) {
1120 mp_rep = &mp_reply.msgs[0];
1121 p = (struct vfio_mp_param *)mp_rep->param;
1122 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1123 container_fd = mp_rep->fds[0];
1124 free(mp_reply.msgs);
1125 return container_fd;
1129 free(mp_reply.msgs);
1130 RTE_LOG(ERR, EAL, " cannot request default container fd\n");
1135 vfio_get_iommu_type(void)
1137 if (default_vfio_cfg->vfio_iommu_type == NULL)
1140 return default_vfio_cfg->vfio_iommu_type->type_id;
1143 const struct vfio_iommu_type *
1144 vfio_set_iommu_type(int vfio_container_fd)
1147 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1148 const struct vfio_iommu_type *t = &iommu_types[idx];
1150 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
1153 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
1154 t->type_id, t->name);
1157 /* not an error, there may be more supported IOMMU types */
1158 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
1159 "error %i (%s)\n", t->type_id, t->name, errno,
1162 /* if we didn't find a suitable IOMMU type, fail */
1167 vfio_has_supported_extensions(int vfio_container_fd)
1170 unsigned idx, n_extensions = 0;
1171 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1172 const struct vfio_iommu_type *t = &iommu_types[idx];
1174 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1177 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
1178 "error %i (%s)\n", errno,
1180 close(vfio_container_fd);
1182 } else if (ret == 1) {
1183 /* we found a supported extension */
1186 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
1187 t->type_id, t->name,
1188 ret ? "supported" : "not supported");
1191 /* if we didn't find any supported IOMMU types, fail */
1192 if (!n_extensions) {
1193 close(vfio_container_fd);
1201 rte_vfio_get_container_fd(void)
1203 int ret, vfio_container_fd;
1204 struct rte_mp_msg mp_req, *mp_rep;
1205 struct rte_mp_reply mp_reply = {0};
1206 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1207 struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1208 const struct internal_config *internal_conf =
1209 eal_get_internal_configuration();
1212 /* if we're in a primary process, try to open the container */
1213 if (internal_conf->process_type == RTE_PROC_PRIMARY) {
1214 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1215 if (vfio_container_fd < 0) {
1216 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
1217 "error %i (%s)\n", errno, strerror(errno));
1221 /* check VFIO API version */
1222 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1223 if (ret != VFIO_API_VERSION) {
1225 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
1226 "error %i (%s)\n", errno, strerror(errno));
1228 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
1229 close(vfio_container_fd);
1233 ret = vfio_has_supported_extensions(vfio_container_fd);
1235 RTE_LOG(ERR, EAL, " no supported IOMMU "
1236 "extensions found!\n");
1240 return vfio_container_fd;
1243 * if we're in a secondary process, request container fd from the
1244 * primary process via mp channel
1246 p->req = SOCKET_REQ_CONTAINER;
1247 strcpy(mp_req.name, EAL_VFIO_MP);
1248 mp_req.len_param = sizeof(*p);
1251 vfio_container_fd = -1;
1252 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1253 mp_reply.nb_received == 1) {
1254 mp_rep = &mp_reply.msgs[0];
1255 p = (struct vfio_mp_param *)mp_rep->param;
1256 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1257 vfio_container_fd = mp_rep->fds[0];
1258 free(mp_reply.msgs);
1259 return vfio_container_fd;
1263 free(mp_reply.msgs);
1264 RTE_LOG(ERR, EAL, " cannot request container fd\n");
1269 rte_vfio_get_group_num(const char *sysfs_base,
1270 const char *dev_addr, int *iommu_group_num)
1272 char linkname[PATH_MAX];
1273 char filename[PATH_MAX];
1274 char *tok[16], *group_tok, *end;
1277 memset(linkname, 0, sizeof(linkname));
1278 memset(filename, 0, sizeof(filename));
1280 /* try to find out IOMMU group for this device */
1281 snprintf(linkname, sizeof(linkname),
1282 "%s/%s/iommu_group", sysfs_base, dev_addr);
1284 ret = readlink(linkname, filename, sizeof(filename));
1286 /* if the link doesn't exist, no VFIO for us */
1290 ret = rte_strsplit(filename, sizeof(filename),
1291 tok, RTE_DIM(tok), '/');
1294 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
1298 /* IOMMU group is always the last token */
1300 group_tok = tok[ret - 1];
1302 *iommu_group_num = strtol(group_tok, &end, 10);
1303 if ((end != group_tok && *end != '\0') || errno != 0) {
1304 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
1312 type1_map_contig(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1313 size_t len, void *arg)
1315 int *vfio_container_fd = arg;
1320 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1325 type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1328 int *vfio_container_fd = arg;
1330 /* skip external memory that isn't a heap */
1331 if (msl->external && !msl->heap)
1334 /* skip any segments with invalid IOVA addresses */
1335 if (ms->iova == RTE_BAD_IOVA)
1338 /* if IOVA mode is VA, we've already mapped the internal segments */
1339 if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
1342 return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1347 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1348 uint64_t len, int do_map)
1350 struct vfio_iommu_type1_dma_map dma_map;
1351 struct vfio_iommu_type1_dma_unmap dma_unmap;
1355 memset(&dma_map, 0, sizeof(dma_map));
1356 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1357 dma_map.vaddr = vaddr;
1359 dma_map.iova = iova;
1360 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1361 VFIO_DMA_MAP_FLAG_WRITE;
1363 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1366 * In case the mapping was already done EEXIST will be
1367 * returned from kernel.
1369 if (errno == EEXIST) {
1371 " Memory segment is already mapped,"
1375 " cannot set up DMA remapping,"
1377 errno, strerror(errno));
1382 memset(&dma_unmap, 0, sizeof(dma_unmap));
1383 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1384 dma_unmap.size = len;
1385 dma_unmap.iova = iova;
1387 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1390 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1391 errno, strerror(errno));
1400 vfio_type1_dma_map(int vfio_container_fd)
1402 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
1403 /* with IOVA as VA mode, we can get away with mapping contiguous
1404 * chunks rather than going page-by-page.
1406 int ret = rte_memseg_contig_walk(type1_map_contig,
1407 &vfio_container_fd);
1410 /* we have to continue the walk because we've skipped the
1411 * external segments during the config walk.
1414 return rte_memseg_walk(type1_map, &vfio_container_fd);
1418 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1419 uint64_t len, int do_map)
1421 struct vfio_iommu_type1_dma_map dma_map;
1422 struct vfio_iommu_type1_dma_unmap dma_unmap;
1424 struct vfio_iommu_spapr_register_memory reg = {
1425 .argsz = sizeof(reg),
1428 reg.vaddr = (uintptr_t) vaddr;
1432 ret = ioctl(vfio_container_fd,
1433 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
1435 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
1436 "error %i (%s)\n", errno, strerror(errno));
1440 memset(&dma_map, 0, sizeof(dma_map));
1441 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1442 dma_map.vaddr = vaddr;
1444 dma_map.iova = iova;
1445 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1446 VFIO_DMA_MAP_FLAG_WRITE;
1448 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1451 * In case the mapping was already done EBUSY will be
1452 * returned from kernel.
1454 if (errno == EBUSY) {
1456 " Memory segment is already mapped,"
1460 " cannot set up DMA remapping,"
1461 " error %i (%s)\n", errno,
1468 memset(&dma_unmap, 0, sizeof(dma_unmap));
1469 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1470 dma_unmap.size = len;
1471 dma_unmap.iova = iova;
1473 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1476 RTE_LOG(ERR, EAL, " cannot clear DMA remapping, error %i (%s)\n",
1477 errno, strerror(errno));
1481 ret = ioctl(vfio_container_fd,
1482 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
1484 RTE_LOG(ERR, EAL, " cannot unregister vaddr for IOMMU, error %i (%s)\n",
1485 errno, strerror(errno));
1494 vfio_spapr_map_walk(const struct rte_memseg_list *msl,
1495 const struct rte_memseg *ms, void *arg)
1497 int *vfio_container_fd = arg;
1499 /* skip external memory that isn't a heap */
1500 if (msl->external && !msl->heap)
1503 /* skip any segments with invalid IOVA addresses */
1504 if (ms->iova == RTE_BAD_IOVA)
1507 return vfio_spapr_dma_do_map(*vfio_container_fd, ms->addr_64, ms->iova,
1512 vfio_spapr_unmap_walk(const struct rte_memseg_list *msl,
1513 const struct rte_memseg *ms, void *arg)
1515 int *vfio_container_fd = arg;
1517 /* skip external memory that isn't a heap */
1518 if (msl->external && !msl->heap)
1521 /* skip any segments with invalid IOVA addresses */
1522 if (ms->iova == RTE_BAD_IOVA)
1525 return vfio_spapr_dma_do_map(*vfio_container_fd, ms->addr_64, ms->iova,
1529 struct spapr_walk_param {
1530 uint64_t window_size;
1531 uint64_t hugepage_sz;
1535 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl,
1536 const struct rte_memseg *ms, void *arg)
1538 struct spapr_walk_param *param = arg;
1539 uint64_t max = ms->iova + ms->len;
1541 /* skip external memory that isn't a heap */
1542 if (msl->external && !msl->heap)
1545 /* skip any segments with invalid IOVA addresses */
1546 if (ms->iova == RTE_BAD_IOVA)
1549 if (max > param->window_size) {
1550 param->hugepage_sz = ms->hugepage_sz;
1551 param->window_size = max;
1558 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1559 struct vfio_iommu_spapr_tce_create *create) {
1560 struct vfio_iommu_spapr_tce_remove remove = {
1561 .argsz = sizeof(remove),
1563 struct vfio_iommu_spapr_tce_info info = {
1564 .argsz = sizeof(info),
1568 /* query spapr iommu info */
1569 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1571 RTE_LOG(ERR, EAL, " cannot get iommu info, "
1572 "error %i (%s)\n", errno, strerror(errno));
1576 /* remove default DMA of 32 bit window */
1577 remove.start_addr = info.dma32_window_start;
1578 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1580 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
1581 "error %i (%s)\n", errno, strerror(errno));
1585 /* create new DMA window */
1586 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1588 #ifdef VFIO_IOMMU_SPAPR_INFO_DDW
1589 /* try possible page_shift and levels for workaround */
1592 for (levels = create->levels + 1;
1593 ret && levels <= info.ddw.levels; levels++) {
1594 create->levels = levels;
1595 ret = ioctl(vfio_container_fd,
1596 VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1600 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
1601 "error %i (%s)\n", errno, strerror(errno));
1606 if (create->start_addr != 0) {
1607 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
1615 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1616 uint64_t len, int do_map)
1618 struct spapr_walk_param param;
1619 struct vfio_iommu_spapr_tce_create create = {
1620 .argsz = sizeof(create),
1622 struct vfio_config *vfio_cfg;
1623 struct user_mem_maps *user_mem_maps;
1626 vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1627 if (vfio_cfg == NULL) {
1628 RTE_LOG(ERR, EAL, " invalid container fd!\n");
1632 user_mem_maps = &vfio_cfg->mem_maps;
1633 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1635 /* check if window size needs to be adjusted */
1636 memset(¶m, 0, sizeof(param));
1638 /* we're inside a callback so use thread-unsafe version */
1639 if (rte_memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1641 RTE_LOG(ERR, EAL, "Could not get window size\n");
1646 /* also check user maps */
1647 for (i = 0; i < user_mem_maps->n_maps; i++) {
1648 uint64_t max = user_mem_maps->maps[i].iova +
1649 user_mem_maps->maps[i].len;
1650 param.window_size = RTE_MAX(param.window_size, max);
1653 /* sPAPR requires window size to be a power of 2 */
1654 create.window_size = rte_align64pow2(param.window_size);
1655 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1659 /* re-create window and remap the entire memory */
1660 if (iova + len > create.window_size) {
1661 /* release all maps before recreating the window */
1662 if (rte_memseg_walk_thread_unsafe(vfio_spapr_unmap_walk,
1663 &vfio_container_fd) < 0) {
1664 RTE_LOG(ERR, EAL, "Could not release DMA maps\n");
1668 /* release all user maps */
1669 for (i = 0; i < user_mem_maps->n_maps; i++) {
1670 struct user_mem_map *map =
1671 &user_mem_maps->maps[i];
1672 if (vfio_spapr_dma_do_map(vfio_container_fd,
1673 map->addr, map->iova, map->len,
1675 RTE_LOG(ERR, EAL, "Could not release user DMA maps\n");
1680 create.window_size = rte_align64pow2(iova + len);
1681 if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1683 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1687 /* we're inside a callback, so use thread-unsafe version
1689 if (rte_memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1690 &vfio_container_fd) < 0) {
1691 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1695 /* remap all user maps */
1696 for (i = 0; i < user_mem_maps->n_maps; i++) {
1697 struct user_mem_map *map =
1698 &user_mem_maps->maps[i];
1699 if (vfio_spapr_dma_do_map(vfio_container_fd,
1700 map->addr, map->iova, map->len,
1702 RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1708 if (vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 1)) {
1709 RTE_LOG(ERR, EAL, "Failed to map DMA\n");
1714 /* for unmap, check if iova within DMA window */
1715 if (iova > create.window_size) {
1716 RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1721 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1724 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1729 vfio_spapr_dma_map(int vfio_container_fd)
1731 struct vfio_iommu_spapr_tce_create create = {
1732 .argsz = sizeof(create),
1734 struct spapr_walk_param param;
1736 memset(¶m, 0, sizeof(param));
1738 /* create DMA window from 0 to max(phys_addr + len) */
1739 rte_memseg_walk(vfio_spapr_window_size_walk, ¶m);
1741 /* sPAPR requires window size to be a power of 2 */
1742 create.window_size = rte_align64pow2(param.window_size);
1743 create.page_shift = __builtin_ctzll(param.hugepage_sz);
1746 if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1747 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1751 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1752 if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1759 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1761 /* No-IOMMU mode does not need DMA mapping */
1766 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1767 uint64_t __rte_unused vaddr,
1768 uint64_t __rte_unused iova, uint64_t __rte_unused len,
1769 int __rte_unused do_map)
1771 /* No-IOMMU mode does not need DMA mapping */
1776 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1777 uint64_t len, int do_map)
1779 const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1782 RTE_LOG(ERR, EAL, " VFIO support not initialized\n");
1787 if (!t->dma_user_map_func) {
1789 " VFIO custom DMA region maping not supported by IOMMU %s\n",
1791 rte_errno = ENOTSUP;
1795 return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1800 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1803 struct user_mem_map *new_map;
1804 struct user_mem_maps *user_mem_maps;
1807 user_mem_maps = &vfio_cfg->mem_maps;
1808 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1809 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1810 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1816 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1817 /* technically, this will fail if there are currently no devices
1818 * plugged in, even if a device were added later, this mapping
1819 * might have succeeded. however, since we cannot verify if this
1820 * is a valid mapping without having a device attached, consider
1821 * this to be unsupported, because we can't just store any old
1822 * mapping and pollute list of active mappings willy-nilly.
1824 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1828 /* create new user mem map entry */
1829 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1830 new_map->addr = vaddr;
1831 new_map->iova = iova;
1834 compact_user_maps(user_mem_maps);
1836 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1841 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1844 struct user_mem_map *map, *new_map = NULL;
1845 struct user_mem_maps *user_mem_maps;
1848 user_mem_maps = &vfio_cfg->mem_maps;
1849 rte_spinlock_recursive_lock(&user_mem_maps->lock);
1851 /* find our mapping */
1852 map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1854 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1859 if (map->addr != vaddr || map->iova != iova || map->len != len) {
1860 /* we're partially unmapping a previously mapped region, so we
1861 * need to split entry into two.
1863 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1864 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1869 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1872 /* unmap the entry */
1873 if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1874 /* there may not be any devices plugged in, so unmapping will
1875 * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1876 * stop us from removing the mapping, as the assumption is we
1877 * won't be needing this memory any more and thus will want to
1878 * prevent it from being remapped again on hotplug. so, only
1879 * fail if we indeed failed to unmap (e.g. if the mapping was
1880 * within our mapped range but had invalid alignment).
1882 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1883 RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1887 RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1890 /* remove map from the list of active mappings */
1891 if (new_map != NULL) {
1892 adjust_map(map, new_map, vaddr, len);
1894 /* if we've created a new map by splitting, sort everything */
1895 if (!is_null_map(new_map)) {
1896 compact_user_maps(user_mem_maps);
1898 /* we've created a new mapping, but it was unused */
1899 user_mem_maps->n_maps--;
1902 memset(map, 0, sizeof(*map));
1903 compact_user_maps(user_mem_maps);
1904 user_mem_maps->n_maps--;
1908 rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1913 rte_vfio_noiommu_is_enabled(void)
1919 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1921 if (errno != ENOENT) {
1922 RTE_LOG(ERR, EAL, " cannot open vfio noiommu file %i (%s)\n",
1923 errno, strerror(errno));
1927 * else the file does not exists
1928 * i.e. noiommu is not enabled
1933 cnt = read(fd, &c, 1);
1936 RTE_LOG(ERR, EAL, " unable to read from vfio noiommu "
1937 "file %i (%s)\n", errno, strerror(errno));
1945 rte_vfio_container_create(void)
1949 /* Find an empty slot to store new vfio config */
1950 for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1951 if (vfio_cfgs[i].vfio_container_fd == -1)
1955 if (i == VFIO_MAX_CONTAINERS) {
1956 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1960 vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1961 if (vfio_cfgs[i].vfio_container_fd < 0) {
1962 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1966 return vfio_cfgs[i].vfio_container_fd;
1970 rte_vfio_container_destroy(int container_fd)
1972 struct vfio_config *vfio_cfg;
1975 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1976 if (vfio_cfg == NULL) {
1977 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1981 for (i = 0; i < VFIO_MAX_GROUPS; i++)
1982 if (vfio_cfg->vfio_groups[i].group_num != -1)
1983 rte_vfio_container_group_unbind(container_fd,
1984 vfio_cfg->vfio_groups[i].group_num);
1986 close(container_fd);
1987 vfio_cfg->vfio_container_fd = -1;
1988 vfio_cfg->vfio_active_groups = 0;
1989 vfio_cfg->vfio_iommu_type = NULL;
1995 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1997 struct vfio_config *vfio_cfg;
1999 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2000 if (vfio_cfg == NULL) {
2001 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2005 return vfio_get_group_fd(vfio_cfg, iommu_group_num);
2009 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
2011 struct vfio_config *vfio_cfg;
2012 struct vfio_group *cur_grp = NULL;
2015 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2016 if (vfio_cfg == NULL) {
2017 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2021 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
2022 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
2023 cur_grp = &vfio_cfg->vfio_groups[i];
2028 /* This should not happen */
2029 if (i == VFIO_MAX_GROUPS || cur_grp == NULL) {
2030 RTE_LOG(ERR, EAL, "Specified group number not found\n");
2034 if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
2035 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
2036 " iommu_group_num %d\n", iommu_group_num);
2039 cur_grp->group_num = -1;
2041 cur_grp->devices = 0;
2042 vfio_cfg->vfio_active_groups--;
2048 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
2051 struct vfio_config *vfio_cfg;
2058 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2059 if (vfio_cfg == NULL) {
2060 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2064 return container_dma_map(vfio_cfg, vaddr, iova, len);
2068 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
2071 struct vfio_config *vfio_cfg;
2078 vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2079 if (vfio_cfg == NULL) {
2080 RTE_LOG(ERR, EAL, "Invalid container fd\n");
2084 return container_dma_unmap(vfio_cfg, vaddr, iova, len);
2090 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
2091 __rte_unused const char *dev_addr,
2092 __rte_unused int *vfio_dev_fd,
2093 __rte_unused struct vfio_device_info *device_info)
2099 rte_vfio_release_device(__rte_unused const char *sysfs_base,
2100 __rte_unused const char *dev_addr, __rte_unused int fd)
2106 rte_vfio_enable(__rte_unused const char *modname)
2112 rte_vfio_is_enabled(__rte_unused const char *modname)
2118 rte_vfio_noiommu_is_enabled(void)
2124 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
2130 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
2131 __rte_unused const char *dev_addr,
2132 __rte_unused int *iommu_group_num)
2138 rte_vfio_get_container_fd(void)
2144 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
2150 rte_vfio_container_create(void)
2156 rte_vfio_container_destroy(__rte_unused int container_fd)
2162 rte_vfio_container_group_bind(__rte_unused int container_fd,
2163 __rte_unused int iommu_group_num)
2169 rte_vfio_container_group_unbind(__rte_unused int container_fd,
2170 __rte_unused int iommu_group_num)
2176 rte_vfio_container_dma_map(__rte_unused int container_fd,
2177 __rte_unused uint64_t vaddr,
2178 __rte_unused uint64_t iova,
2179 __rte_unused uint64_t len)
2185 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
2186 __rte_unused uint64_t vaddr,
2187 __rte_unused uint64_t iova,
2188 __rte_unused uint64_t len)
2193 #endif /* VFIO_PRESENT */