vfio: add multi container support
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <inttypes.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/ioctl.h>
10
11 #include <rte_errno.h>
12 #include <rte_log.h>
13 #include <rte_memory.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_vfio.h>
16
17 #include "eal_filesystem.h"
18 #include "eal_vfio.h"
19 #include "eal_private.h"
20
21 #ifdef VFIO_PRESENT
22
23 #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
24
25 /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
26  * recreate the mappings for DPDK segments, but we cannot do so for memory that
27  * was registered by the user themselves, so we need to store the user mappings
28  * somewhere, to recreate them later.
29  */
30 #define VFIO_MAX_USER_MEM_MAPS 256
31 struct user_mem_map {
32         uint64_t addr;
33         uint64_t iova;
34         uint64_t len;
35 };
36
37 struct user_mem_maps {
38         rte_spinlock_recursive_t lock;
39         int n_maps;
40         struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
41 };
42
43 struct vfio_config {
44         int vfio_enabled;
45         int vfio_container_fd;
46         int vfio_active_groups;
47         const struct vfio_iommu_type *vfio_iommu_type;
48         struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
49         struct user_mem_maps mem_maps;
50 };
51
52 /* per-process VFIO config */
53 static struct vfio_config vfio_cfgs[VFIO_MAX_CONTAINERS];
54 static struct vfio_config *default_vfio_cfg = &vfio_cfgs[0];
55
56 static int vfio_type1_dma_map(int);
57 static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
58 static int vfio_spapr_dma_map(int);
59 static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
60 static int vfio_noiommu_dma_map(int);
61 static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
62 static int vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr,
63                 uint64_t iova, uint64_t len, int do_map);
64
65 /* IOMMU types we support */
66 static const struct vfio_iommu_type iommu_types[] = {
67         /* x86 IOMMU, otherwise known as type 1 */
68         {
69                 .type_id = RTE_VFIO_TYPE1,
70                 .name = "Type 1",
71                 .dma_map_func = &vfio_type1_dma_map,
72                 .dma_user_map_func = &vfio_type1_dma_mem_map
73         },
74         /* ppc64 IOMMU, otherwise known as spapr */
75         {
76                 .type_id = RTE_VFIO_SPAPR,
77                 .name = "sPAPR",
78                 .dma_map_func = &vfio_spapr_dma_map,
79                 .dma_user_map_func = &vfio_spapr_dma_mem_map
80         },
81         /* IOMMU-less mode */
82         {
83                 .type_id = RTE_VFIO_NOIOMMU,
84                 .name = "No-IOMMU",
85                 .dma_map_func = &vfio_noiommu_dma_map,
86                 .dma_user_map_func = &vfio_noiommu_dma_mem_map
87         },
88 };
89
90 /* for sPAPR IOMMU, we will need to walk memseg list, but we cannot use
91  * rte_memseg_walk() because by the time we enter callback we will be holding a
92  * write lock, so regular rte-memseg_walk will deadlock. copying the same
93  * iteration code everywhere is not ideal as well. so, use a lockless copy of
94  * memseg walk here.
95  */
96 static int
97 memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
98 {
99         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
100         int i, ms_idx, ret = 0;
101
102         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
103                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
104                 const struct rte_memseg *ms;
105                 struct rte_fbarray *arr;
106
107                 if (msl->memseg_arr.count == 0)
108                         continue;
109
110                 arr = &msl->memseg_arr;
111
112                 ms_idx = rte_fbarray_find_next_used(arr, 0);
113                 while (ms_idx >= 0) {
114                         ms = rte_fbarray_get(arr, ms_idx);
115                         ret = func(msl, ms, arg);
116                         if (ret < 0)
117                                 return -1;
118                         if (ret > 0)
119                                 return 1;
120                         ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
121                 }
122         }
123         return 0;
124 }
125
126 static int
127 is_null_map(const struct user_mem_map *map)
128 {
129         return map->addr == 0 && map->iova == 0 && map->len == 0;
130 }
131
132 /* we may need to merge user mem maps together in case of user mapping/unmapping
133  * chunks of memory, so we'll need a comparator function to sort segments.
134  */
135 static int
136 user_mem_map_cmp(const void *a, const void *b)
137 {
138         const struct user_mem_map *umm_a = a;
139         const struct user_mem_map *umm_b = b;
140
141         /* move null entries to end */
142         if (is_null_map(umm_a))
143                 return 1;
144         if (is_null_map(umm_b))
145                 return -1;
146
147         /* sort by iova first */
148         if (umm_a->iova < umm_b->iova)
149                 return -1;
150         if (umm_a->iova > umm_b->iova)
151                 return 1;
152
153         if (umm_a->addr < umm_b->addr)
154                 return -1;
155         if (umm_a->addr > umm_b->addr)
156                 return 1;
157
158         if (umm_a->len < umm_b->len)
159                 return -1;
160         if (umm_a->len > umm_b->len)
161                 return 1;
162
163         return 0;
164 }
165
166 /* adjust user map entry. this may result in shortening of existing map, or in
167  * splitting existing map in two pieces.
168  */
169 static void
170 adjust_map(struct user_mem_map *src, struct user_mem_map *end,
171                 uint64_t remove_va_start, uint64_t remove_len)
172 {
173         /* if va start is same as start address, we're simply moving start */
174         if (remove_va_start == src->addr) {
175                 src->addr += remove_len;
176                 src->iova += remove_len;
177                 src->len -= remove_len;
178         } else if (remove_va_start + remove_len == src->addr + src->len) {
179                 /* we're shrinking mapping from the end */
180                 src->len -= remove_len;
181         } else {
182                 /* we're blowing a hole in the middle */
183                 struct user_mem_map tmp;
184                 uint64_t total_len = src->len;
185
186                 /* adjust source segment length */
187                 src->len = remove_va_start - src->addr;
188
189                 /* create temporary segment in the middle */
190                 tmp.addr = src->addr + src->len;
191                 tmp.iova = src->iova + src->len;
192                 tmp.len = remove_len;
193
194                 /* populate end segment - this one we will be keeping */
195                 end->addr = tmp.addr + tmp.len;
196                 end->iova = tmp.iova + tmp.len;
197                 end->len = total_len - src->len - tmp.len;
198         }
199 }
200
201 /* try merging two maps into one, return 1 if succeeded */
202 static int
203 merge_map(struct user_mem_map *left, struct user_mem_map *right)
204 {
205         if (left->addr + left->len != right->addr)
206                 return 0;
207         if (left->iova + left->len != right->iova)
208                 return 0;
209
210         left->len += right->len;
211
212         memset(right, 0, sizeof(*right));
213
214         return 1;
215 }
216
217 static struct user_mem_map *
218 find_user_mem_map(struct user_mem_maps *user_mem_maps, uint64_t addr,
219                 uint64_t iova, uint64_t len)
220 {
221         uint64_t va_end = addr + len;
222         uint64_t iova_end = iova + len;
223         int i;
224
225         for (i = 0; i < user_mem_maps->n_maps; i++) {
226                 struct user_mem_map *map = &user_mem_maps->maps[i];
227                 uint64_t map_va_end = map->addr + map->len;
228                 uint64_t map_iova_end = map->iova + map->len;
229
230                 /* check start VA */
231                 if (addr < map->addr || addr >= map_va_end)
232                         continue;
233                 /* check if VA end is within boundaries */
234                 if (va_end <= map->addr || va_end > map_va_end)
235                         continue;
236
237                 /* check start IOVA */
238                 if (iova < map->iova || iova >= map_iova_end)
239                         continue;
240                 /* check if IOVA end is within boundaries */
241                 if (iova_end <= map->iova || iova_end > map_iova_end)
242                         continue;
243
244                 /* we've found our map */
245                 return map;
246         }
247         return NULL;
248 }
249
250 /* this will sort all user maps, and merge/compact any adjacent maps */
251 static void
252 compact_user_maps(struct user_mem_maps *user_mem_maps)
253 {
254         int i, n_merged, cur_idx;
255
256         qsort(user_mem_maps->maps, user_mem_maps->n_maps,
257                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
258
259         /* we'll go over the list backwards when merging */
260         n_merged = 0;
261         for (i = user_mem_maps->n_maps - 2; i >= 0; i--) {
262                 struct user_mem_map *l, *r;
263
264                 l = &user_mem_maps->maps[i];
265                 r = &user_mem_maps->maps[i + 1];
266
267                 if (is_null_map(l) || is_null_map(r))
268                         continue;
269
270                 if (merge_map(l, r))
271                         n_merged++;
272         }
273
274         /* the entries are still sorted, but now they have holes in them, so
275          * walk through the list and remove the holes
276          */
277         if (n_merged > 0) {
278                 cur_idx = 0;
279                 for (i = 0; i < user_mem_maps->n_maps; i++) {
280                         if (!is_null_map(&user_mem_maps->maps[i])) {
281                                 struct user_mem_map *src, *dst;
282
283                                 src = &user_mem_maps->maps[i];
284                                 dst = &user_mem_maps->maps[cur_idx++];
285
286                                 if (src != dst) {
287                                         memcpy(dst, src, sizeof(*src));
288                                         memset(src, 0, sizeof(*src));
289                                 }
290                         }
291                 }
292                 user_mem_maps->n_maps = cur_idx;
293         }
294 }
295
296 static int
297 vfio_open_group_fd(int iommu_group_num)
298 {
299         int vfio_group_fd;
300         char filename[PATH_MAX];
301         struct rte_mp_msg mp_req, *mp_rep;
302         struct rte_mp_reply mp_reply;
303         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
304         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
305
306         /* if primary, try to open the group */
307         if (internal_config.process_type == RTE_PROC_PRIMARY) {
308                 /* try regular group format */
309                 snprintf(filename, sizeof(filename),
310                                  VFIO_GROUP_FMT, iommu_group_num);
311                 vfio_group_fd = open(filename, O_RDWR);
312                 if (vfio_group_fd < 0) {
313                         /* if file not found, it's not an error */
314                         if (errno != ENOENT) {
315                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
316                                                 strerror(errno));
317                                 return -1;
318                         }
319
320                         /* special case: try no-IOMMU path as well */
321                         snprintf(filename, sizeof(filename),
322                                         VFIO_NOIOMMU_GROUP_FMT,
323                                         iommu_group_num);
324                         vfio_group_fd = open(filename, O_RDWR);
325                         if (vfio_group_fd < 0) {
326                                 if (errno != ENOENT) {
327                                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
328                                                         strerror(errno));
329                                         return -1;
330                                 }
331                                 return 0;
332                         }
333                         /* noiommu group found */
334                 }
335
336                 return vfio_group_fd;
337         }
338         /* if we're in a secondary process, request group fd from the primary
339          * process via mp channel.
340          */
341         p->req = SOCKET_REQ_GROUP;
342         p->group_num = iommu_group_num;
343         strcpy(mp_req.name, EAL_VFIO_MP);
344         mp_req.len_param = sizeof(*p);
345         mp_req.num_fds = 0;
346
347         vfio_group_fd = -1;
348         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
349             mp_reply.nb_received == 1) {
350                 mp_rep = &mp_reply.msgs[0];
351                 p = (struct vfio_mp_param *)mp_rep->param;
352                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
353                         vfio_group_fd = mp_rep->fds[0];
354                 } else if (p->result == SOCKET_NO_FD) {
355                         RTE_LOG(ERR, EAL, "  bad VFIO group fd\n");
356                         vfio_group_fd = 0;
357                 }
358                 free(mp_reply.msgs);
359         }
360
361         if (vfio_group_fd < 0)
362                 RTE_LOG(ERR, EAL, "  cannot request group fd\n");
363         return vfio_group_fd;
364 }
365
366 static struct vfio_config *
367 get_vfio_cfg_by_group_num(int iommu_group_num)
368 {
369         struct vfio_config *vfio_cfg;
370         int i, j;
371
372         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
373                 vfio_cfg = &vfio_cfgs[i];
374                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
375                         if (vfio_cfg->vfio_groups[j].group_num ==
376                                         iommu_group_num)
377                                 return vfio_cfg;
378                 }
379         }
380
381         return NULL;
382 }
383
384 static struct vfio_config *
385 get_vfio_cfg_by_group_fd(int vfio_group_fd)
386 {
387         struct vfio_config *vfio_cfg;
388         int i, j;
389
390         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
391                 vfio_cfg = &vfio_cfgs[i];
392                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
393                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
394                                 return vfio_cfg;
395         }
396
397         return NULL;
398 }
399
400 static struct vfio_config *
401 get_vfio_cfg_by_container_fd(int container_fd)
402 {
403         int i;
404
405         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
406                 if (vfio_cfgs[i].vfio_container_fd == container_fd)
407                         return &vfio_cfgs[i];
408         }
409
410         return NULL;
411 }
412
413 int
414 rte_vfio_get_group_fd(int iommu_group_num)
415 {
416         int i;
417         int vfio_group_fd;
418         struct vfio_group *cur_grp;
419         struct vfio_config *vfio_cfg;
420
421         /* get the vfio_config it belongs to */
422         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
423         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
424
425         /* check if we already have the group descriptor open */
426         for (i = 0; i < VFIO_MAX_GROUPS; i++)
427                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
428                         return vfio_cfg->vfio_groups[i].fd;
429
430         /* Lets see first if there is room for a new group */
431         if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
432                 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
433                 return -1;
434         }
435
436         /* Now lets get an index for the new group */
437         for (i = 0; i < VFIO_MAX_GROUPS; i++)
438                 if (vfio_cfg->vfio_groups[i].group_num == -1) {
439                         cur_grp = &vfio_cfg->vfio_groups[i];
440                         break;
441                 }
442
443         /* This should not happen */
444         if (i == VFIO_MAX_GROUPS) {
445                 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
446                 return -1;
447         }
448
449         vfio_group_fd = vfio_open_group_fd(iommu_group_num);
450         if (vfio_group_fd < 0) {
451                 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
452                 return -1;
453         }
454
455         cur_grp->group_num = iommu_group_num;
456         cur_grp->fd = vfio_group_fd;
457         vfio_cfg->vfio_active_groups++;
458
459         return vfio_group_fd;
460 }
461
462 static int
463 get_vfio_group_idx(int vfio_group_fd)
464 {
465         struct vfio_config *vfio_cfg;
466         int i, j;
467
468         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
469                 vfio_cfg = &vfio_cfgs[i];
470                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
471                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
472                                 return j;
473         }
474
475         return -1;
476 }
477
478 static void
479 vfio_group_device_get(int vfio_group_fd)
480 {
481         struct vfio_config *vfio_cfg;
482         int i;
483
484         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
485         if (vfio_cfg == NULL) {
486                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
487                 return;
488         }
489
490         i = get_vfio_group_idx(vfio_group_fd);
491         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
492                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
493         else
494                 vfio_cfg->vfio_groups[i].devices++;
495 }
496
497 static void
498 vfio_group_device_put(int vfio_group_fd)
499 {
500         struct vfio_config *vfio_cfg;
501         int i;
502
503         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
504         if (vfio_cfg == NULL) {
505                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
506                 return;
507         }
508
509         i = get_vfio_group_idx(vfio_group_fd);
510         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
511                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
512         else
513                 vfio_cfg->vfio_groups[i].devices--;
514 }
515
516 static int
517 vfio_group_device_count(int vfio_group_fd)
518 {
519         struct vfio_config *vfio_cfg;
520         int i;
521
522         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
523         if (vfio_cfg == NULL) {
524                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
525                 return -1;
526         }
527
528         i = get_vfio_group_idx(vfio_group_fd);
529         if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
530                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
531                 return -1;
532         }
533
534         return vfio_cfg->vfio_groups[i].devices;
535 }
536
537 static void
538 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len)
539 {
540         struct rte_memseg_list *msl;
541         struct rte_memseg *ms;
542         size_t cur_len = 0;
543
544         msl = rte_mem_virt2memseg_list(addr);
545
546         /* for IOVA as VA mode, no need to care for IOVA addresses */
547         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
548                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
549                 if (type == RTE_MEM_EVENT_ALLOC)
550                         vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
551                                         len, 1);
552                 else
553                         vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
554                                         len, 0);
555                 return;
556         }
557
558         /* memsegs are contiguous in memory */
559         ms = rte_mem_virt2memseg(addr, msl);
560         while (cur_len < len) {
561                 if (type == RTE_MEM_EVENT_ALLOC)
562                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
563                                         ms->iova, ms->len, 1);
564                 else
565                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
566                                         ms->iova, ms->len, 0);
567
568                 cur_len += ms->len;
569                 ++ms;
570         }
571 }
572
573 int
574 rte_vfio_clear_group(int vfio_group_fd)
575 {
576         int i;
577         struct rte_mp_msg mp_req, *mp_rep;
578         struct rte_mp_reply mp_reply;
579         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
580         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
581         struct vfio_config *vfio_cfg;
582
583         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
584         if (vfio_cfg == NULL) {
585                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
586                 return -1;
587         }
588
589         if (internal_config.process_type == RTE_PROC_PRIMARY) {
590
591                 i = get_vfio_group_idx(vfio_group_fd);
592                 if (i < 0)
593                         return -1;
594                 vfio_cfg->vfio_groups[i].group_num = -1;
595                 vfio_cfg->vfio_groups[i].fd = -1;
596                 vfio_cfg->vfio_groups[i].devices = 0;
597                 vfio_cfg->vfio_active_groups--;
598                 return 0;
599         }
600
601         p->req = SOCKET_CLR_GROUP;
602         p->group_num = vfio_group_fd;
603         strcpy(mp_req.name, EAL_VFIO_MP);
604         mp_req.len_param = sizeof(*p);
605         mp_req.num_fds = 0;
606
607         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
608             mp_reply.nb_received == 1) {
609                 mp_rep = &mp_reply.msgs[0];
610                 p = (struct vfio_mp_param *)mp_rep->param;
611                 if (p->result == SOCKET_OK) {
612                         free(mp_reply.msgs);
613                         return 0;
614                 } else if (p->result == SOCKET_NO_FD)
615                         RTE_LOG(ERR, EAL, "  BAD VFIO group fd!\n");
616                 else
617                         RTE_LOG(ERR, EAL, "  no such VFIO group fd!\n");
618
619                 free(mp_reply.msgs);
620         }
621
622         return -1;
623 }
624
625 int
626 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
627                 int *vfio_dev_fd, struct vfio_device_info *device_info)
628 {
629         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
630         rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
631         struct vfio_group_status group_status = {
632                         .argsz = sizeof(group_status)
633         };
634         struct vfio_config *vfio_cfg;
635         struct user_mem_maps *user_mem_maps;
636         int vfio_container_fd;
637         int vfio_group_fd;
638         int iommu_group_num;
639         int i, ret;
640
641         /* get group number */
642         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
643         if (ret == 0) {
644                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
645                         dev_addr);
646                 return 1;
647         }
648
649         /* if negative, something failed */
650         if (ret < 0)
651                 return -1;
652
653         /* get the actual group fd */
654         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
655         if (vfio_group_fd < 0)
656                 return -1;
657
658         /* if group_fd == 0, that means the device isn't managed by VFIO */
659         if (vfio_group_fd == 0) {
660                 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
661                                 dev_addr);
662                 return 1;
663         }
664
665         /*
666          * at this point, we know that this group is viable (meaning, all devices
667          * are either bound to VFIO or not bound to anything)
668          */
669
670         /* check if the group is viable */
671         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
672         if (ret) {
673                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
674                                 "error %i (%s)\n", dev_addr, errno, strerror(errno));
675                 close(vfio_group_fd);
676                 rte_vfio_clear_group(vfio_group_fd);
677                 return -1;
678         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
679                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", dev_addr);
680                 close(vfio_group_fd);
681                 rte_vfio_clear_group(vfio_group_fd);
682                 return -1;
683         }
684
685         /* get the vfio_config it belongs to */
686         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
687         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
688         vfio_container_fd = vfio_cfg->vfio_container_fd;
689         user_mem_maps = &vfio_cfg->mem_maps;
690
691         /* check if group does not have a container yet */
692         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
693
694                 /* add group to a container */
695                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
696                                 &vfio_container_fd);
697                 if (ret) {
698                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
699                                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
700                         close(vfio_group_fd);
701                         rte_vfio_clear_group(vfio_group_fd);
702                         return -1;
703                 }
704
705                 /*
706                  * pick an IOMMU type and set up DMA mappings for container
707                  *
708                  * needs to be done only once, only when first group is
709                  * assigned to a container and only in primary process.
710                  * Note this can happen several times with the hotplug
711                  * functionality.
712                  */
713                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
714                                 vfio_cfg->vfio_active_groups == 1 &&
715                                 vfio_group_device_count(vfio_group_fd) == 0) {
716                         const struct vfio_iommu_type *t;
717
718                         /* select an IOMMU type which we will be using */
719                         t = vfio_set_iommu_type(vfio_container_fd);
720                         if (!t) {
721                                 RTE_LOG(ERR, EAL,
722                                         "  %s failed to select IOMMU type\n",
723                                         dev_addr);
724                                 close(vfio_group_fd);
725                                 rte_vfio_clear_group(vfio_group_fd);
726                                 return -1;
727                         }
728                         /* lock memory hotplug before mapping and release it
729                          * after registering callback, to prevent races
730                          */
731                         rte_rwlock_read_lock(mem_lock);
732                         if (vfio_cfg == default_vfio_cfg)
733                                 ret = t->dma_map_func(vfio_container_fd);
734                         else
735                                 ret = 0;
736                         if (ret) {
737                                 RTE_LOG(ERR, EAL,
738                                         "  %s DMA remapping failed, error %i (%s)\n",
739                                         dev_addr, errno, strerror(errno));
740                                 close(vfio_group_fd);
741                                 rte_vfio_clear_group(vfio_group_fd);
742                                 rte_rwlock_read_unlock(mem_lock);
743                                 return -1;
744                         }
745
746                         vfio_cfg->vfio_iommu_type = t;
747
748                         /* re-map all user-mapped segments */
749                         rte_spinlock_recursive_lock(&user_mem_maps->lock);
750
751                         /* this IOMMU type may not support DMA mapping, but
752                          * if we have mappings in the list - that means we have
753                          * previously mapped something successfully, so we can
754                          * be sure that DMA mapping is supported.
755                          */
756                         for (i = 0; i < user_mem_maps->n_maps; i++) {
757                                 struct user_mem_map *map;
758                                 map = &user_mem_maps->maps[i];
759
760                                 ret = t->dma_user_map_func(
761                                                 vfio_container_fd,
762                                                 map->addr, map->iova, map->len,
763                                                 1);
764                                 if (ret) {
765                                         RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
766                                                         "va: 0x%" PRIx64 " "
767                                                         "iova: 0x%" PRIx64 " "
768                                                         "len: 0x%" PRIu64 "\n",
769                                                         map->addr, map->iova,
770                                                         map->len);
771                                         rte_spinlock_recursive_unlock(
772                                                         &user_mem_maps->lock);
773                                         rte_rwlock_read_unlock(mem_lock);
774                                         return -1;
775                                 }
776                         }
777                         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
778
779                         /* register callback for mem events */
780                         if (vfio_cfg == default_vfio_cfg)
781                                 ret = rte_mem_event_callback_register(
782                                         VFIO_MEM_EVENT_CLB_NAME,
783                                         vfio_mem_event_callback);
784                         else
785                                 ret = 0;
786                         /* unlock memory hotplug */
787                         rte_rwlock_read_unlock(mem_lock);
788
789                         if (ret && rte_errno != ENOTSUP) {
790                                 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
791                                 return -1;
792                         }
793                         if (ret)
794                                 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
795                         else
796                                 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
797                 }
798         }
799
800         /* get a file descriptor for the device */
801         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
802         if (*vfio_dev_fd < 0) {
803                 /* if we cannot get a device fd, this implies a problem with
804                  * the VFIO group or the container not having IOMMU configured.
805                  */
806
807                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
808                                 dev_addr);
809                 close(vfio_group_fd);
810                 rte_vfio_clear_group(vfio_group_fd);
811                 return -1;
812         }
813
814         /* test and setup the device */
815         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
816         if (ret) {
817                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
818                                 "error %i (%s)\n", dev_addr, errno,
819                                 strerror(errno));
820                 close(*vfio_dev_fd);
821                 close(vfio_group_fd);
822                 rte_vfio_clear_group(vfio_group_fd);
823                 return -1;
824         }
825         vfio_group_device_get(vfio_group_fd);
826
827         return 0;
828 }
829
830 int
831 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
832                     int vfio_dev_fd)
833 {
834         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
835         rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
836         struct vfio_group_status group_status = {
837                         .argsz = sizeof(group_status)
838         };
839         struct vfio_config *vfio_cfg;
840         int vfio_group_fd;
841         int iommu_group_num;
842         int ret;
843
844         /* we don't want any DMA mapping messages to come while we're detaching
845          * VFIO device, because this might be the last device and we might need
846          * to unregister the callback.
847          */
848         rte_rwlock_read_lock(mem_lock);
849
850         /* get group number */
851         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
852         if (ret <= 0) {
853                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver\n",
854                         dev_addr);
855                 /* This is an error at this point. */
856                 ret = -1;
857                 goto out;
858         }
859
860         /* get the actual group fd */
861         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
862         if (vfio_group_fd <= 0) {
863                 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
864                                    dev_addr);
865                 ret = -1;
866                 goto out;
867         }
868
869         /* get the vfio_config it belongs to */
870         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
871         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
872
873         /* At this point we got an active group. Closing it will make the
874          * container detachment. If this is the last active group, VFIO kernel
875          * code will unset the container and the IOMMU mappings.
876          */
877
878         /* Closing a device */
879         if (close(vfio_dev_fd) < 0) {
880                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
881                                    dev_addr);
882                 ret = -1;
883                 goto out;
884         }
885
886         /* An VFIO group can have several devices attached. Just when there is
887          * no devices remaining should the group be closed.
888          */
889         vfio_group_device_put(vfio_group_fd);
890         if (!vfio_group_device_count(vfio_group_fd)) {
891
892                 if (close(vfio_group_fd) < 0) {
893                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
894                                 dev_addr);
895                         ret = -1;
896                         goto out;
897                 }
898
899                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
900                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
901                                            dev_addr);
902                         ret = -1;
903                         goto out;
904                 }
905         }
906
907         /* if there are no active device groups, unregister the callback to
908          * avoid spurious attempts to map/unmap memory from VFIO.
909          */
910         if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0)
911                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME);
912
913         /* success */
914         ret = 0;
915
916 out:
917         rte_rwlock_read_unlock(mem_lock);
918         return ret;
919 }
920
921 int
922 rte_vfio_enable(const char *modname)
923 {
924         /* initialize group list */
925         int i, j;
926         int vfio_available;
927
928         rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
929
930         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
931                 vfio_cfgs[i].vfio_container_fd = -1;
932                 vfio_cfgs[i].vfio_active_groups = 0;
933                 vfio_cfgs[i].vfio_iommu_type = NULL;
934                 vfio_cfgs[i].mem_maps.lock = lock;
935
936                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
937                         vfio_cfgs[i].vfio_groups[j].fd = -1;
938                         vfio_cfgs[i].vfio_groups[j].group_num = -1;
939                         vfio_cfgs[i].vfio_groups[j].devices = 0;
940                 }
941         }
942
943         /* inform the user that we are probing for VFIO */
944         RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
945
946         /* check if vfio module is loaded */
947         vfio_available = rte_eal_check_module(modname);
948
949         /* return error directly */
950         if (vfio_available == -1) {
951                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
952                 return -1;
953         }
954
955         /* return 0 if VFIO modules not loaded */
956         if (vfio_available == 0) {
957                 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
958                         "skipping VFIO support...\n");
959                 return 0;
960         }
961
962         default_vfio_cfg->vfio_container_fd = rte_vfio_get_container_fd();
963
964         /* check if we have VFIO driver enabled */
965         if (default_vfio_cfg->vfio_container_fd != -1) {
966                 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
967                 default_vfio_cfg->vfio_enabled = 1;
968         } else {
969                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
970         }
971
972         return 0;
973 }
974
975 int
976 rte_vfio_is_enabled(const char *modname)
977 {
978         const int mod_available = rte_eal_check_module(modname) > 0;
979         return default_vfio_cfg->vfio_enabled && mod_available;
980 }
981
982 const struct vfio_iommu_type *
983 vfio_set_iommu_type(int vfio_container_fd)
984 {
985         unsigned idx;
986         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
987                 const struct vfio_iommu_type *t = &iommu_types[idx];
988
989                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
990                                 t->type_id);
991                 if (!ret) {
992                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
993                                         t->type_id, t->name);
994                         return t;
995                 }
996                 /* not an error, there may be more supported IOMMU types */
997                 RTE_LOG(DEBUG, EAL, "  set IOMMU type %d (%s) failed, "
998                                 "error %i (%s)\n", t->type_id, t->name, errno,
999                                 strerror(errno));
1000         }
1001         /* if we didn't find a suitable IOMMU type, fail */
1002         return NULL;
1003 }
1004
1005 int
1006 vfio_has_supported_extensions(int vfio_container_fd)
1007 {
1008         int ret;
1009         unsigned idx, n_extensions = 0;
1010         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1011                 const struct vfio_iommu_type *t = &iommu_types[idx];
1012
1013                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1014                                 t->type_id);
1015                 if (ret < 0) {
1016                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
1017                                 "error %i (%s)\n", errno,
1018                                 strerror(errno));
1019                         close(vfio_container_fd);
1020                         return -1;
1021                 } else if (ret == 1) {
1022                         /* we found a supported extension */
1023                         n_extensions++;
1024                 }
1025                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
1026                                 t->type_id, t->name,
1027                                 ret ? "supported" : "not supported");
1028         }
1029
1030         /* if we didn't find any supported IOMMU types, fail */
1031         if (!n_extensions) {
1032                 close(vfio_container_fd);
1033                 return -1;
1034         }
1035
1036         return 0;
1037 }
1038
1039 int
1040 rte_vfio_get_container_fd(void)
1041 {
1042         int ret, vfio_container_fd;
1043         struct rte_mp_msg mp_req, *mp_rep;
1044         struct rte_mp_reply mp_reply;
1045         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1046         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1047
1048
1049         /* if we're in a primary process, try to open the container */
1050         if (internal_config.process_type == RTE_PROC_PRIMARY) {
1051                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1052                 if (vfio_container_fd < 0) {
1053                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
1054                                         "error %i (%s)\n", errno, strerror(errno));
1055                         return -1;
1056                 }
1057
1058                 /* check VFIO API version */
1059                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1060                 if (ret != VFIO_API_VERSION) {
1061                         if (ret < 0)
1062                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
1063                                                 "error %i (%s)\n", errno, strerror(errno));
1064                         else
1065                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
1066                         close(vfio_container_fd);
1067                         return -1;
1068                 }
1069
1070                 ret = vfio_has_supported_extensions(vfio_container_fd);
1071                 if (ret) {
1072                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
1073                                         "extensions found!\n");
1074                         return -1;
1075                 }
1076
1077                 return vfio_container_fd;
1078         }
1079         /*
1080          * if we're in a secondary process, request container fd from the
1081          * primary process via mp channel
1082          */
1083         p->req = SOCKET_REQ_CONTAINER;
1084         strcpy(mp_req.name, EAL_VFIO_MP);
1085         mp_req.len_param = sizeof(*p);
1086         mp_req.num_fds = 0;
1087
1088         vfio_container_fd = -1;
1089         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1090             mp_reply.nb_received == 1) {
1091                 mp_rep = &mp_reply.msgs[0];
1092                 p = (struct vfio_mp_param *)mp_rep->param;
1093                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1094                         free(mp_reply.msgs);
1095                         return mp_rep->fds[0];
1096                 }
1097                 free(mp_reply.msgs);
1098         }
1099
1100         RTE_LOG(ERR, EAL, "  cannot request container fd\n");
1101         return -1;
1102 }
1103
1104 int
1105 rte_vfio_get_group_num(const char *sysfs_base,
1106                 const char *dev_addr, int *iommu_group_num)
1107 {
1108         char linkname[PATH_MAX];
1109         char filename[PATH_MAX];
1110         char *tok[16], *group_tok, *end;
1111         int ret;
1112
1113         memset(linkname, 0, sizeof(linkname));
1114         memset(filename, 0, sizeof(filename));
1115
1116         /* try to find out IOMMU group for this device */
1117         snprintf(linkname, sizeof(linkname),
1118                          "%s/%s/iommu_group", sysfs_base, dev_addr);
1119
1120         ret = readlink(linkname, filename, sizeof(filename));
1121
1122         /* if the link doesn't exist, no VFIO for us */
1123         if (ret < 0)
1124                 return 0;
1125
1126         ret = rte_strsplit(filename, sizeof(filename),
1127                         tok, RTE_DIM(tok), '/');
1128
1129         if (ret <= 0) {
1130                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
1131                 return -1;
1132         }
1133
1134         /* IOMMU group is always the last token */
1135         errno = 0;
1136         group_tok = tok[ret - 1];
1137         end = group_tok;
1138         *iommu_group_num = strtol(group_tok, &end, 10);
1139         if ((end != group_tok && *end != '\0') || errno != 0) {
1140                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", dev_addr);
1141                 return -1;
1142         }
1143
1144         return 1;
1145 }
1146
1147 static int
1148 type1_map(const struct rte_memseg_list *msl __rte_unused,
1149                 const struct rte_memseg *ms, void *arg)
1150 {
1151         int *vfio_container_fd = arg;
1152
1153         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1154                         ms->len, 1);
1155 }
1156
1157 static int
1158 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1159                 uint64_t len, int do_map)
1160 {
1161         struct vfio_iommu_type1_dma_map dma_map;
1162         struct vfio_iommu_type1_dma_unmap dma_unmap;
1163         int ret;
1164
1165         if (do_map != 0) {
1166                 memset(&dma_map, 0, sizeof(dma_map));
1167                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1168                 dma_map.vaddr = vaddr;
1169                 dma_map.size = len;
1170                 dma_map.iova = iova;
1171                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1172                                 VFIO_DMA_MAP_FLAG_WRITE;
1173
1174                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1175                 if (ret) {
1176                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
1177                                 errno, strerror(errno));
1178                                 return -1;
1179                 }
1180         } else {
1181                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1182                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1183                 dma_unmap.size = len;
1184                 dma_unmap.iova = iova;
1185
1186                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1187                                 &dma_unmap);
1188                 if (ret) {
1189                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1190                                         errno, strerror(errno));
1191                         return -1;
1192                 }
1193         }
1194
1195         return 0;
1196 }
1197
1198 static int
1199 vfio_type1_dma_map(int vfio_container_fd)
1200 {
1201         return rte_memseg_walk(type1_map, &vfio_container_fd);
1202 }
1203
1204 static int
1205 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1206                 uint64_t len, int do_map)
1207 {
1208         struct vfio_iommu_type1_dma_map dma_map;
1209         struct vfio_iommu_type1_dma_unmap dma_unmap;
1210         int ret;
1211
1212         if (do_map != 0) {
1213                 memset(&dma_map, 0, sizeof(dma_map));
1214                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1215                 dma_map.vaddr = vaddr;
1216                 dma_map.size = len;
1217                 dma_map.iova = iova;
1218                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1219                                 VFIO_DMA_MAP_FLAG_WRITE;
1220
1221                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1222                 if (ret) {
1223                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
1224                                 errno, strerror(errno));
1225                                 return -1;
1226                 }
1227
1228         } else {
1229                 struct vfio_iommu_spapr_register_memory reg = {
1230                         .argsz = sizeof(reg),
1231                         .flags = 0
1232                 };
1233                 reg.vaddr = (uintptr_t) vaddr;
1234                 reg.size = len;
1235
1236                 ret = ioctl(vfio_container_fd,
1237                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
1238                 if (ret) {
1239                         RTE_LOG(ERR, EAL, "  cannot unregister vaddr for IOMMU, error %i (%s)\n",
1240                                         errno, strerror(errno));
1241                         return -1;
1242                 }
1243
1244                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1245                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1246                 dma_unmap.size = len;
1247                 dma_unmap.iova = iova;
1248
1249                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1250                                 &dma_unmap);
1251                 if (ret) {
1252                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1253                                         errno, strerror(errno));
1254                         return -1;
1255                 }
1256         }
1257
1258         return 0;
1259 }
1260
1261 static int
1262 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1263                 const struct rte_memseg *ms, void *arg)
1264 {
1265         int *vfio_container_fd = arg;
1266
1267         return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1268                         ms->len, 1);
1269 }
1270
1271 struct spapr_walk_param {
1272         uint64_t window_size;
1273         uint64_t hugepage_sz;
1274 };
1275 static int
1276 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1277                 const struct rte_memseg *ms, void *arg)
1278 {
1279         struct spapr_walk_param *param = arg;
1280         uint64_t max = ms->iova + ms->len;
1281
1282         if (max > param->window_size) {
1283                 param->hugepage_sz = ms->hugepage_sz;
1284                 param->window_size = max;
1285         }
1286
1287         return 0;
1288 }
1289
1290 static int
1291 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1292                 struct vfio_iommu_spapr_tce_create *create) {
1293         struct vfio_iommu_spapr_tce_remove remove = {
1294                 .argsz = sizeof(remove),
1295         };
1296         struct vfio_iommu_spapr_tce_info info = {
1297                 .argsz = sizeof(info),
1298         };
1299         int ret;
1300
1301         /* query spapr iommu info */
1302         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1303         if (ret) {
1304                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
1305                                 "error %i (%s)\n", errno, strerror(errno));
1306                 return -1;
1307         }
1308
1309         /* remove default DMA of 32 bit window */
1310         remove.start_addr = info.dma32_window_start;
1311         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1312         if (ret) {
1313                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
1314                                 "error %i (%s)\n", errno, strerror(errno));
1315                 return -1;
1316         }
1317
1318         /* create new DMA window */
1319         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1320         if (ret) {
1321                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
1322                                 "error %i (%s)\n", errno, strerror(errno));
1323                 return -1;
1324         }
1325
1326         if (create->start_addr != 0) {
1327                 RTE_LOG(ERR, EAL, "  DMA window start address != 0\n");
1328                 return -1;
1329         }
1330
1331         return 0;
1332 }
1333
1334 static int
1335 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1336                 uint64_t len, int do_map)
1337 {
1338         struct spapr_walk_param param;
1339         struct vfio_iommu_spapr_tce_create create = {
1340                 .argsz = sizeof(create),
1341         };
1342         struct vfio_config *vfio_cfg;
1343         struct user_mem_maps *user_mem_maps;
1344         int i, ret = 0;
1345
1346         vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1347         if (vfio_cfg == NULL) {
1348                 RTE_LOG(ERR, EAL, "  invalid container fd!\n");
1349                 return -1;
1350         }
1351
1352         user_mem_maps = &vfio_cfg->mem_maps;
1353         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1354
1355         /* check if window size needs to be adjusted */
1356         memset(&param, 0, sizeof(param));
1357
1358         if (memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1359                                 &param) < 0) {
1360                 RTE_LOG(ERR, EAL, "Could not get window size\n");
1361                 ret = -1;
1362                 goto out;
1363         }
1364
1365         /* also check user maps */
1366         for (i = 0; i < user_mem_maps->n_maps; i++) {
1367                 uint64_t max = user_mem_maps->maps[i].iova +
1368                                 user_mem_maps->maps[i].len;
1369                 create.window_size = RTE_MAX(create.window_size, max);
1370         }
1371
1372         /* sPAPR requires window size to be a power of 2 */
1373         create.window_size = rte_align64pow2(param.window_size);
1374         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1375         create.levels = 1;
1376
1377         if (do_map) {
1378                 void *addr;
1379                 /* re-create window and remap the entire memory */
1380                 if (iova > create.window_size) {
1381                         if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1382                                         &create) < 0) {
1383                                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1384                                 ret = -1;
1385                                 goto out;
1386                         }
1387                         if (memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1388                                         &vfio_container_fd) < 0) {
1389                                 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1390                                 ret = -1;
1391                                 goto out;
1392                         }
1393                         /* remap all user maps */
1394                         for (i = 0; i < user_mem_maps->n_maps; i++) {
1395                                 struct user_mem_map *map =
1396                                                 &user_mem_maps->maps[i];
1397                                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1398                                                 map->addr, map->iova, map->len,
1399                                                 1)) {
1400                                         RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1401                                         ret = -1;
1402                                         goto out;
1403                                 }
1404                         }
1405                 }
1406
1407                 /* now that we've remapped all of the memory that was present
1408                  * before, map the segment that we were requested to map.
1409                  *
1410                  * however, if we were called by the callback, the memory we
1411                  * were called with was already in the memseg list, so previous
1412                  * mapping should've mapped that segment already.
1413                  *
1414                  * virt2memseg_list is a relatively cheap check, so use that. if
1415                  * memory is within any memseg list, it's a memseg, so it's
1416                  * already mapped.
1417                  */
1418                 addr = (void *)(uintptr_t)vaddr;
1419                 if (rte_mem_virt2memseg_list(addr) == NULL &&
1420                                 vfio_spapr_dma_do_map(vfio_container_fd,
1421                                         vaddr, iova, len, 1) < 0) {
1422                         RTE_LOG(ERR, EAL, "Could not map segment\n");
1423                         ret = -1;
1424                         goto out;
1425                 }
1426         } else {
1427                 /* for unmap, check if iova within DMA window */
1428                 if (iova > create.window_size) {
1429                         RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1430                         ret = -1;
1431                         goto out;
1432                 }
1433
1434                 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1435         }
1436 out:
1437         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1438         return ret;
1439 }
1440
1441 static int
1442 vfio_spapr_dma_map(int vfio_container_fd)
1443 {
1444         struct vfio_iommu_spapr_tce_create create = {
1445                 .argsz = sizeof(create),
1446         };
1447         struct spapr_walk_param param;
1448
1449         memset(&param, 0, sizeof(param));
1450
1451         /* create DMA window from 0 to max(phys_addr + len) */
1452         rte_memseg_walk(vfio_spapr_window_size_walk, &param);
1453
1454         /* sPAPR requires window size to be a power of 2 */
1455         create.window_size = rte_align64pow2(param.window_size);
1456         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1457         create.levels = 1;
1458
1459         if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1460                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1461                 return -1;
1462         }
1463
1464         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1465         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1466                 return -1;
1467
1468         return 0;
1469 }
1470
1471 static int
1472 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1473 {
1474         /* No-IOMMU mode does not need DMA mapping */
1475         return 0;
1476 }
1477
1478 static int
1479 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1480                          uint64_t __rte_unused vaddr,
1481                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
1482                          int __rte_unused do_map)
1483 {
1484         /* No-IOMMU mode does not need DMA mapping */
1485         return 0;
1486 }
1487
1488 static int
1489 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1490                 uint64_t len, int do_map)
1491 {
1492         const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1493
1494         if (!t) {
1495                 RTE_LOG(ERR, EAL, "  VFIO support not initialized\n");
1496                 rte_errno = ENODEV;
1497                 return -1;
1498         }
1499
1500         if (!t->dma_user_map_func) {
1501                 RTE_LOG(ERR, EAL,
1502                         "  VFIO custom DMA region maping not supported by IOMMU %s\n",
1503                         t->name);
1504                 rte_errno = ENOTSUP;
1505                 return -1;
1506         }
1507
1508         return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1509                         len, do_map);
1510 }
1511
1512 static int
1513 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1514                 uint64_t len)
1515 {
1516         struct user_mem_map *new_map;
1517         struct user_mem_maps *user_mem_maps;
1518         int ret = 0;
1519
1520         user_mem_maps = &vfio_cfg->mem_maps;
1521         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1522         if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1523                 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1524                 rte_errno = ENOMEM;
1525                 ret = -1;
1526                 goto out;
1527         }
1528         /* map the entry */
1529         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1530                 /* technically, this will fail if there are currently no devices
1531                  * plugged in, even if a device were added later, this mapping
1532                  * might have succeeded. however, since we cannot verify if this
1533                  * is a valid mapping without having a device attached, consider
1534                  * this to be unsupported, because we can't just store any old
1535                  * mapping and pollute list of active mappings willy-nilly.
1536                  */
1537                 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1538                 ret = -1;
1539                 goto out;
1540         }
1541         /* create new user mem map entry */
1542         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1543         new_map->addr = vaddr;
1544         new_map->iova = iova;
1545         new_map->len = len;
1546
1547         compact_user_maps(user_mem_maps);
1548 out:
1549         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1550         return ret;
1551 }
1552
1553 static int
1554 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1555                 uint64_t len)
1556 {
1557         struct user_mem_map *map, *new_map = NULL;
1558         struct user_mem_maps *user_mem_maps;
1559         int ret = 0;
1560
1561         user_mem_maps = &vfio_cfg->mem_maps;
1562         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1563
1564         /* find our mapping */
1565         map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1566         if (!map) {
1567                 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1568                 rte_errno = EINVAL;
1569                 ret = -1;
1570                 goto out;
1571         }
1572         if (map->addr != vaddr || map->iova != iova || map->len != len) {
1573                 /* we're partially unmapping a previously mapped region, so we
1574                  * need to split entry into two.
1575                  */
1576                 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1577                         RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1578                         rte_errno = ENOMEM;
1579                         ret = -1;
1580                         goto out;
1581                 }
1582                 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1583         }
1584
1585         /* unmap the entry */
1586         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1587                 /* there may not be any devices plugged in, so unmapping will
1588                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1589                  * stop us from removing the mapping, as the assumption is we
1590                  * won't be needing this memory any more and thus will want to
1591                  * prevent it from being remapped again on hotplug. so, only
1592                  * fail if we indeed failed to unmap (e.g. if the mapping was
1593                  * within our mapped range but had invalid alignment).
1594                  */
1595                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1596                         RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1597                         ret = -1;
1598                         goto out;
1599                 } else {
1600                         RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1601                 }
1602         }
1603         /* remove map from the list of active mappings */
1604         if (new_map != NULL) {
1605                 adjust_map(map, new_map, vaddr, len);
1606
1607                 /* if we've created a new map by splitting, sort everything */
1608                 if (!is_null_map(new_map)) {
1609                         compact_user_maps(user_mem_maps);
1610                 } else {
1611                         /* we've created a new mapping, but it was unused */
1612                         user_mem_maps->n_maps--;
1613                 }
1614         } else {
1615                 memset(map, 0, sizeof(*map));
1616                 compact_user_maps(user_mem_maps);
1617                 user_mem_maps->n_maps--;
1618         }
1619
1620 out:
1621         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1622         return ret;
1623 }
1624
1625 int __rte_experimental
1626 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1627 {
1628         if (len == 0) {
1629                 rte_errno = EINVAL;
1630                 return -1;
1631         }
1632
1633         return container_dma_map(default_vfio_cfg, vaddr, iova, len);
1634 }
1635
1636 int __rte_experimental
1637 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1638 {
1639         if (len == 0) {
1640                 rte_errno = EINVAL;
1641                 return -1;
1642         }
1643
1644         return container_dma_unmap(default_vfio_cfg, vaddr, iova, len);
1645 }
1646
1647 int
1648 rte_vfio_noiommu_is_enabled(void)
1649 {
1650         int fd;
1651         ssize_t cnt;
1652         char c;
1653
1654         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1655         if (fd < 0) {
1656                 if (errno != ENOENT) {
1657                         RTE_LOG(ERR, EAL, "  cannot open vfio noiommu file %i (%s)\n",
1658                                         errno, strerror(errno));
1659                         return -1;
1660                 }
1661                 /*
1662                  * else the file does not exists
1663                  * i.e. noiommu is not enabled
1664                  */
1665                 return 0;
1666         }
1667
1668         cnt = read(fd, &c, 1);
1669         close(fd);
1670         if (cnt != 1) {
1671                 RTE_LOG(ERR, EAL, "  unable to read from vfio noiommu "
1672                                 "file %i (%s)\n", errno, strerror(errno));
1673                 return -1;
1674         }
1675
1676         return c == 'Y';
1677 }
1678
1679 int __rte_experimental
1680 rte_vfio_container_create(void)
1681 {
1682         int i;
1683
1684         /* Find an empty slot to store new vfio config */
1685         for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1686                 if (vfio_cfgs[i].vfio_container_fd == -1)
1687                         break;
1688         }
1689
1690         if (i == VFIO_MAX_CONTAINERS) {
1691                 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1692                 return -1;
1693         }
1694
1695         vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1696         if (vfio_cfgs[i].vfio_container_fd < 0) {
1697                 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1698                 return -1;
1699         }
1700
1701         return vfio_cfgs[i].vfio_container_fd;
1702 }
1703
1704 int __rte_experimental
1705 rte_vfio_container_destroy(int container_fd)
1706 {
1707         struct vfio_config *vfio_cfg;
1708         int i;
1709
1710         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1711         if (vfio_cfg == NULL) {
1712                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1713                 return -1;
1714         }
1715
1716         for (i = 0; i < VFIO_MAX_GROUPS; i++)
1717                 if (vfio_cfg->vfio_groups[i].group_num != -1)
1718                         rte_vfio_container_group_unbind(container_fd,
1719                                 vfio_cfg->vfio_groups[i].group_num);
1720
1721         close(container_fd);
1722         vfio_cfg->vfio_container_fd = -1;
1723         vfio_cfg->vfio_active_groups = 0;
1724         vfio_cfg->vfio_iommu_type = NULL;
1725
1726         return 0;
1727 }
1728
1729 int __rte_experimental
1730 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1731 {
1732         struct vfio_config *vfio_cfg;
1733         struct vfio_group *cur_grp;
1734         int vfio_group_fd;
1735         int i;
1736
1737         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1738         if (vfio_cfg == NULL) {
1739                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1740                 return -1;
1741         }
1742
1743         /* Check room for new group */
1744         if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
1745                 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
1746                 return -1;
1747         }
1748
1749         /* Get an index for the new group */
1750         for (i = 0; i < VFIO_MAX_GROUPS; i++)
1751                 if (vfio_cfg->vfio_groups[i].group_num == -1) {
1752                         cur_grp = &vfio_cfg->vfio_groups[i];
1753                         break;
1754                 }
1755
1756         /* This should not happen */
1757         if (i == VFIO_MAX_GROUPS) {
1758                 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
1759                 return -1;
1760         }
1761
1762         vfio_group_fd = vfio_open_group_fd(iommu_group_num);
1763         if (vfio_group_fd < 0) {
1764                 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
1765                 return -1;
1766         }
1767         cur_grp->group_num = iommu_group_num;
1768         cur_grp->fd = vfio_group_fd;
1769         cur_grp->devices = 0;
1770         vfio_cfg->vfio_active_groups++;
1771
1772         return vfio_group_fd;
1773 }
1774
1775 int __rte_experimental
1776 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
1777 {
1778         struct vfio_config *vfio_cfg;
1779         struct vfio_group *cur_grp;
1780         int i;
1781
1782         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1783         if (vfio_cfg == NULL) {
1784                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1785                 return -1;
1786         }
1787
1788         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
1789                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
1790                         cur_grp = &vfio_cfg->vfio_groups[i];
1791                         break;
1792                 }
1793         }
1794
1795         /* This should not happen */
1796         if (i == VFIO_MAX_GROUPS) {
1797                 RTE_LOG(ERR, EAL, "Specified group number not found\n");
1798                 return -1;
1799         }
1800
1801         if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
1802                 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
1803                         " iommu_group_num %d\n", iommu_group_num);
1804                 return -1;
1805         }
1806         cur_grp->group_num = -1;
1807         cur_grp->fd = -1;
1808         cur_grp->devices = 0;
1809         vfio_cfg->vfio_active_groups--;
1810
1811         return 0;
1812 }
1813
1814 int __rte_experimental
1815 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
1816                 uint64_t len)
1817 {
1818         struct vfio_config *vfio_cfg;
1819
1820         if (len == 0) {
1821                 rte_errno = EINVAL;
1822                 return -1;
1823         }
1824
1825         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1826         if (vfio_cfg == NULL) {
1827                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1828                 return -1;
1829         }
1830
1831         return container_dma_map(vfio_cfg, vaddr, iova, len);
1832 }
1833
1834 int __rte_experimental
1835 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
1836                 uint64_t len)
1837 {
1838         struct vfio_config *vfio_cfg;
1839
1840         if (len == 0) {
1841                 rte_errno = EINVAL;
1842                 return -1;
1843         }
1844
1845         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1846         if (vfio_cfg == NULL) {
1847                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1848                 return -1;
1849         }
1850
1851         return container_dma_unmap(vfio_cfg, vaddr, iova, len);
1852 }
1853
1854 #else
1855
1856 int __rte_experimental
1857 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1858                   __rte_unused uint64_t len)
1859 {
1860         return -1;
1861 }
1862
1863 int __rte_experimental
1864 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1865                     __rte_unused uint64_t len)
1866 {
1867         return -1;
1868 }
1869
1870 int
1871 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
1872                 __rte_unused const char *dev_addr,
1873                 __rte_unused int *vfio_dev_fd,
1874                 __rte_unused struct vfio_device_info *device_info)
1875 {
1876         return -1;
1877 }
1878
1879 int
1880 rte_vfio_release_device(__rte_unused const char *sysfs_base,
1881                 __rte_unused const char *dev_addr, __rte_unused int fd)
1882 {
1883         return -1;
1884 }
1885
1886 int
1887 rte_vfio_enable(__rte_unused const char *modname)
1888 {
1889         return -1;
1890 }
1891
1892 int
1893 rte_vfio_is_enabled(__rte_unused const char *modname)
1894 {
1895         return -1;
1896 }
1897
1898 int
1899 rte_vfio_noiommu_is_enabled(void)
1900 {
1901         return -1;
1902 }
1903
1904 int
1905 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
1906 {
1907         return -1;
1908 }
1909
1910 int __rte_experimental
1911 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
1912                 __rte_unused const char *dev_addr,
1913                 __rte_unused int *iommu_group_num)
1914 {
1915         return -1;
1916 }
1917
1918 int __rte_experimental
1919 rte_vfio_get_container_fd(void)
1920 {
1921         return -1;
1922 }
1923
1924 int __rte_experimental
1925 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
1926 {
1927         return -1;
1928 }
1929
1930 int __rte_experimental
1931 rte_vfio_container_create(void)
1932 {
1933         return -1;
1934 }
1935
1936 int __rte_experimental
1937 rte_vfio_container_destroy(__rte_unused int container_fd)
1938 {
1939         return -1;
1940 }
1941
1942 int __rte_experimental
1943 rte_vfio_container_group_bind(__rte_unused int container_fd,
1944                 __rte_unused int iommu_group_num)
1945 {
1946         return -1;
1947 }
1948
1949 int __rte_experimental
1950 rte_vfio_container_group_unbind(__rte_unused int container_fd,
1951                 __rte_unused int iommu_group_num)
1952 {
1953         return -1;
1954 }
1955
1956 int __rte_experimental
1957 rte_vfio_container_dma_map(__rte_unused int container_fd,
1958                 __rte_unused uint64_t vaddr,
1959                 __rte_unused uint64_t iova,
1960                 __rte_unused uint64_t len)
1961 {
1962         return -1;
1963 }
1964
1965 int __rte_experimental
1966 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
1967                 __rte_unused uint64_t vaddr,
1968                 __rte_unused uint64_t iova,
1969                 __rte_unused uint64_t len)
1970 {
1971         return -1;
1972 }
1973
1974 #endif /* VFIO_PRESENT */