a2bbdfbf4a6ee43f283b3fde2bf755ed9a4b7098
[deb_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                 void *arg __rte_unused)
540 {
541         struct rte_memseg_list *msl;
542         struct rte_memseg *ms;
543         size_t cur_len = 0;
544
545         msl = rte_mem_virt2memseg_list(addr);
546
547         /* for IOVA as VA mode, no need to care for IOVA addresses */
548         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
549                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
550                 if (type == RTE_MEM_EVENT_ALLOC)
551                         vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
552                                         len, 1);
553                 else
554                         vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
555                                         len, 0);
556                 return;
557         }
558
559         /* memsegs are contiguous in memory */
560         ms = rte_mem_virt2memseg(addr, msl);
561         while (cur_len < len) {
562                 if (type == RTE_MEM_EVENT_ALLOC)
563                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
564                                         ms->iova, ms->len, 1);
565                 else
566                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
567                                         ms->iova, ms->len, 0);
568
569                 cur_len += ms->len;
570                 ++ms;
571         }
572 }
573
574 int
575 rte_vfio_clear_group(int vfio_group_fd)
576 {
577         int i;
578         struct rte_mp_msg mp_req, *mp_rep;
579         struct rte_mp_reply mp_reply;
580         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
581         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
582         struct vfio_config *vfio_cfg;
583
584         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
585         if (vfio_cfg == NULL) {
586                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
587                 return -1;
588         }
589
590         if (internal_config.process_type == RTE_PROC_PRIMARY) {
591
592                 i = get_vfio_group_idx(vfio_group_fd);
593                 if (i < 0)
594                         return -1;
595                 vfio_cfg->vfio_groups[i].group_num = -1;
596                 vfio_cfg->vfio_groups[i].fd = -1;
597                 vfio_cfg->vfio_groups[i].devices = 0;
598                 vfio_cfg->vfio_active_groups--;
599                 return 0;
600         }
601
602         p->req = SOCKET_CLR_GROUP;
603         p->group_num = vfio_group_fd;
604         strcpy(mp_req.name, EAL_VFIO_MP);
605         mp_req.len_param = sizeof(*p);
606         mp_req.num_fds = 0;
607
608         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
609             mp_reply.nb_received == 1) {
610                 mp_rep = &mp_reply.msgs[0];
611                 p = (struct vfio_mp_param *)mp_rep->param;
612                 if (p->result == SOCKET_OK) {
613                         free(mp_reply.msgs);
614                         return 0;
615                 } else if (p->result == SOCKET_NO_FD)
616                         RTE_LOG(ERR, EAL, "  BAD VFIO group fd!\n");
617                 else
618                         RTE_LOG(ERR, EAL, "  no such VFIO group fd!\n");
619
620                 free(mp_reply.msgs);
621         }
622
623         return -1;
624 }
625
626 int
627 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
628                 int *vfio_dev_fd, struct vfio_device_info *device_info)
629 {
630         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
631         rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
632         struct vfio_group_status group_status = {
633                         .argsz = sizeof(group_status)
634         };
635         struct vfio_config *vfio_cfg;
636         struct user_mem_maps *user_mem_maps;
637         int vfio_container_fd;
638         int vfio_group_fd;
639         int iommu_group_num;
640         int i, ret;
641
642         /* get group number */
643         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
644         if (ret == 0) {
645                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
646                         dev_addr);
647                 return 1;
648         }
649
650         /* if negative, something failed */
651         if (ret < 0)
652                 return -1;
653
654         /* get the actual group fd */
655         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
656         if (vfio_group_fd < 0)
657                 return -1;
658
659         /* if group_fd == 0, that means the device isn't managed by VFIO */
660         if (vfio_group_fd == 0) {
661                 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
662                                 dev_addr);
663                 return 1;
664         }
665
666         /*
667          * at this point, we know that this group is viable (meaning, all devices
668          * are either bound to VFIO or not bound to anything)
669          */
670
671         /* check if the group is viable */
672         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
673         if (ret) {
674                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
675                                 "error %i (%s)\n", dev_addr, errno, strerror(errno));
676                 close(vfio_group_fd);
677                 rte_vfio_clear_group(vfio_group_fd);
678                 return -1;
679         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
680                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", dev_addr);
681                 close(vfio_group_fd);
682                 rte_vfio_clear_group(vfio_group_fd);
683                 return -1;
684         }
685
686         /* get the vfio_config it belongs to */
687         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
688         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
689         vfio_container_fd = vfio_cfg->vfio_container_fd;
690         user_mem_maps = &vfio_cfg->mem_maps;
691
692         /* check if group does not have a container yet */
693         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
694
695                 /* add group to a container */
696                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
697                                 &vfio_container_fd);
698                 if (ret) {
699                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
700                                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
701                         close(vfio_group_fd);
702                         rte_vfio_clear_group(vfio_group_fd);
703                         return -1;
704                 }
705
706                 /*
707                  * pick an IOMMU type and set up DMA mappings for container
708                  *
709                  * needs to be done only once, only when first group is
710                  * assigned to a container and only in primary process.
711                  * Note this can happen several times with the hotplug
712                  * functionality.
713                  */
714                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
715                                 vfio_cfg->vfio_active_groups == 1 &&
716                                 vfio_group_device_count(vfio_group_fd) == 0) {
717                         const struct vfio_iommu_type *t;
718
719                         /* select an IOMMU type which we will be using */
720                         t = vfio_set_iommu_type(vfio_container_fd);
721                         if (!t) {
722                                 RTE_LOG(ERR, EAL,
723                                         "  %s failed to select IOMMU type\n",
724                                         dev_addr);
725                                 close(vfio_group_fd);
726                                 rte_vfio_clear_group(vfio_group_fd);
727                                 return -1;
728                         }
729                         /* lock memory hotplug before mapping and release it
730                          * after registering callback, to prevent races
731                          */
732                         rte_rwlock_read_lock(mem_lock);
733                         if (vfio_cfg == default_vfio_cfg)
734                                 ret = t->dma_map_func(vfio_container_fd);
735                         else
736                                 ret = 0;
737                         if (ret) {
738                                 RTE_LOG(ERR, EAL,
739                                         "  %s DMA remapping failed, error %i (%s)\n",
740                                         dev_addr, errno, strerror(errno));
741                                 close(vfio_group_fd);
742                                 rte_vfio_clear_group(vfio_group_fd);
743                                 rte_rwlock_read_unlock(mem_lock);
744                                 return -1;
745                         }
746
747                         vfio_cfg->vfio_iommu_type = t;
748
749                         /* re-map all user-mapped segments */
750                         rte_spinlock_recursive_lock(&user_mem_maps->lock);
751
752                         /* this IOMMU type may not support DMA mapping, but
753                          * if we have mappings in the list - that means we have
754                          * previously mapped something successfully, so we can
755                          * be sure that DMA mapping is supported.
756                          */
757                         for (i = 0; i < user_mem_maps->n_maps; i++) {
758                                 struct user_mem_map *map;
759                                 map = &user_mem_maps->maps[i];
760
761                                 ret = t->dma_user_map_func(
762                                                 vfio_container_fd,
763                                                 map->addr, map->iova, map->len,
764                                                 1);
765                                 if (ret) {
766                                         RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
767                                                         "va: 0x%" PRIx64 " "
768                                                         "iova: 0x%" PRIx64 " "
769                                                         "len: 0x%" PRIu64 "\n",
770                                                         map->addr, map->iova,
771                                                         map->len);
772                                         rte_spinlock_recursive_unlock(
773                                                         &user_mem_maps->lock);
774                                         rte_rwlock_read_unlock(mem_lock);
775                                         return -1;
776                                 }
777                         }
778                         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
779
780                         /* register callback for mem events */
781                         if (vfio_cfg == default_vfio_cfg)
782                                 ret = rte_mem_event_callback_register(
783                                         VFIO_MEM_EVENT_CLB_NAME,
784                                         vfio_mem_event_callback, NULL);
785                         else
786                                 ret = 0;
787                         /* unlock memory hotplug */
788                         rte_rwlock_read_unlock(mem_lock);
789
790                         if (ret && rte_errno != ENOTSUP) {
791                                 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
792                                 return -1;
793                         }
794                         if (ret)
795                                 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
796                         else
797                                 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
798                 }
799         }
800
801         /* get a file descriptor for the device */
802         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
803         if (*vfio_dev_fd < 0) {
804                 /* if we cannot get a device fd, this implies a problem with
805                  * the VFIO group or the container not having IOMMU configured.
806                  */
807
808                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
809                                 dev_addr);
810                 close(vfio_group_fd);
811                 rte_vfio_clear_group(vfio_group_fd);
812                 return -1;
813         }
814
815         /* test and setup the device */
816         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
817         if (ret) {
818                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
819                                 "error %i (%s)\n", dev_addr, errno,
820                                 strerror(errno));
821                 close(*vfio_dev_fd);
822                 close(vfio_group_fd);
823                 rte_vfio_clear_group(vfio_group_fd);
824                 return -1;
825         }
826         vfio_group_device_get(vfio_group_fd);
827
828         return 0;
829 }
830
831 int
832 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
833                     int vfio_dev_fd)
834 {
835         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
836         rte_rwlock_t *mem_lock = &mcfg->memory_hotplug_lock;
837         struct vfio_group_status group_status = {
838                         .argsz = sizeof(group_status)
839         };
840         struct vfio_config *vfio_cfg;
841         int vfio_group_fd;
842         int iommu_group_num;
843         int ret;
844
845         /* we don't want any DMA mapping messages to come while we're detaching
846          * VFIO device, because this might be the last device and we might need
847          * to unregister the callback.
848          */
849         rte_rwlock_read_lock(mem_lock);
850
851         /* get group number */
852         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
853         if (ret <= 0) {
854                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver\n",
855                         dev_addr);
856                 /* This is an error at this point. */
857                 ret = -1;
858                 goto out;
859         }
860
861         /* get the actual group fd */
862         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
863         if (vfio_group_fd <= 0) {
864                 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
865                                    dev_addr);
866                 ret = -1;
867                 goto out;
868         }
869
870         /* get the vfio_config it belongs to */
871         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
872         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
873
874         /* At this point we got an active group. Closing it will make the
875          * container detachment. If this is the last active group, VFIO kernel
876          * code will unset the container and the IOMMU mappings.
877          */
878
879         /* Closing a device */
880         if (close(vfio_dev_fd) < 0) {
881                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
882                                    dev_addr);
883                 ret = -1;
884                 goto out;
885         }
886
887         /* An VFIO group can have several devices attached. Just when there is
888          * no devices remaining should the group be closed.
889          */
890         vfio_group_device_put(vfio_group_fd);
891         if (!vfio_group_device_count(vfio_group_fd)) {
892
893                 if (close(vfio_group_fd) < 0) {
894                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
895                                 dev_addr);
896                         ret = -1;
897                         goto out;
898                 }
899
900                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
901                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
902                                            dev_addr);
903                         ret = -1;
904                         goto out;
905                 }
906         }
907
908         /* if there are no active device groups, unregister the callback to
909          * avoid spurious attempts to map/unmap memory from VFIO.
910          */
911         if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0)
912                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
913                                 NULL);
914
915         /* success */
916         ret = 0;
917
918 out:
919         rte_rwlock_read_unlock(mem_lock);
920         return ret;
921 }
922
923 int
924 rte_vfio_enable(const char *modname)
925 {
926         /* initialize group list */
927         int i, j;
928         int vfio_available;
929
930         rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
931
932         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
933                 vfio_cfgs[i].vfio_container_fd = -1;
934                 vfio_cfgs[i].vfio_active_groups = 0;
935                 vfio_cfgs[i].vfio_iommu_type = NULL;
936                 vfio_cfgs[i].mem_maps.lock = lock;
937
938                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
939                         vfio_cfgs[i].vfio_groups[j].fd = -1;
940                         vfio_cfgs[i].vfio_groups[j].group_num = -1;
941                         vfio_cfgs[i].vfio_groups[j].devices = 0;
942                 }
943         }
944
945         /* inform the user that we are probing for VFIO */
946         RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
947
948         /* check if vfio module is loaded */
949         vfio_available = rte_eal_check_module(modname);
950
951         /* return error directly */
952         if (vfio_available == -1) {
953                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
954                 return -1;
955         }
956
957         /* return 0 if VFIO modules not loaded */
958         if (vfio_available == 0) {
959                 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
960                         "skipping VFIO support...\n");
961                 return 0;
962         }
963
964         default_vfio_cfg->vfio_container_fd = rte_vfio_get_container_fd();
965
966         /* check if we have VFIO driver enabled */
967         if (default_vfio_cfg->vfio_container_fd != -1) {
968                 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
969                 default_vfio_cfg->vfio_enabled = 1;
970         } else {
971                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
972         }
973
974         return 0;
975 }
976
977 int
978 rte_vfio_is_enabled(const char *modname)
979 {
980         const int mod_available = rte_eal_check_module(modname) > 0;
981         return default_vfio_cfg->vfio_enabled && mod_available;
982 }
983
984 const struct vfio_iommu_type *
985 vfio_set_iommu_type(int vfio_container_fd)
986 {
987         unsigned idx;
988         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
989                 const struct vfio_iommu_type *t = &iommu_types[idx];
990
991                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
992                                 t->type_id);
993                 if (!ret) {
994                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
995                                         t->type_id, t->name);
996                         return t;
997                 }
998                 /* not an error, there may be more supported IOMMU types */
999                 RTE_LOG(DEBUG, EAL, "  set IOMMU type %d (%s) failed, "
1000                                 "error %i (%s)\n", t->type_id, t->name, errno,
1001                                 strerror(errno));
1002         }
1003         /* if we didn't find a suitable IOMMU type, fail */
1004         return NULL;
1005 }
1006
1007 int
1008 vfio_has_supported_extensions(int vfio_container_fd)
1009 {
1010         int ret;
1011         unsigned idx, n_extensions = 0;
1012         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1013                 const struct vfio_iommu_type *t = &iommu_types[idx];
1014
1015                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1016                                 t->type_id);
1017                 if (ret < 0) {
1018                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
1019                                 "error %i (%s)\n", errno,
1020                                 strerror(errno));
1021                         close(vfio_container_fd);
1022                         return -1;
1023                 } else if (ret == 1) {
1024                         /* we found a supported extension */
1025                         n_extensions++;
1026                 }
1027                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
1028                                 t->type_id, t->name,
1029                                 ret ? "supported" : "not supported");
1030         }
1031
1032         /* if we didn't find any supported IOMMU types, fail */
1033         if (!n_extensions) {
1034                 close(vfio_container_fd);
1035                 return -1;
1036         }
1037
1038         return 0;
1039 }
1040
1041 int
1042 rte_vfio_get_container_fd(void)
1043 {
1044         int ret, vfio_container_fd;
1045         struct rte_mp_msg mp_req, *mp_rep;
1046         struct rte_mp_reply mp_reply;
1047         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1048         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1049
1050
1051         /* if we're in a primary process, try to open the container */
1052         if (internal_config.process_type == RTE_PROC_PRIMARY) {
1053                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1054                 if (vfio_container_fd < 0) {
1055                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
1056                                         "error %i (%s)\n", errno, strerror(errno));
1057                         return -1;
1058                 }
1059
1060                 /* check VFIO API version */
1061                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1062                 if (ret != VFIO_API_VERSION) {
1063                         if (ret < 0)
1064                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
1065                                                 "error %i (%s)\n", errno, strerror(errno));
1066                         else
1067                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
1068                         close(vfio_container_fd);
1069                         return -1;
1070                 }
1071
1072                 ret = vfio_has_supported_extensions(vfio_container_fd);
1073                 if (ret) {
1074                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
1075                                         "extensions found!\n");
1076                         return -1;
1077                 }
1078
1079                 return vfio_container_fd;
1080         }
1081         /*
1082          * if we're in a secondary process, request container fd from the
1083          * primary process via mp channel
1084          */
1085         p->req = SOCKET_REQ_CONTAINER;
1086         strcpy(mp_req.name, EAL_VFIO_MP);
1087         mp_req.len_param = sizeof(*p);
1088         mp_req.num_fds = 0;
1089
1090         vfio_container_fd = -1;
1091         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1092             mp_reply.nb_received == 1) {
1093                 mp_rep = &mp_reply.msgs[0];
1094                 p = (struct vfio_mp_param *)mp_rep->param;
1095                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1096                         free(mp_reply.msgs);
1097                         return mp_rep->fds[0];
1098                 }
1099                 free(mp_reply.msgs);
1100         }
1101
1102         RTE_LOG(ERR, EAL, "  cannot request container fd\n");
1103         return -1;
1104 }
1105
1106 int
1107 rte_vfio_get_group_num(const char *sysfs_base,
1108                 const char *dev_addr, int *iommu_group_num)
1109 {
1110         char linkname[PATH_MAX];
1111         char filename[PATH_MAX];
1112         char *tok[16], *group_tok, *end;
1113         int ret;
1114
1115         memset(linkname, 0, sizeof(linkname));
1116         memset(filename, 0, sizeof(filename));
1117
1118         /* try to find out IOMMU group for this device */
1119         snprintf(linkname, sizeof(linkname),
1120                          "%s/%s/iommu_group", sysfs_base, dev_addr);
1121
1122         ret = readlink(linkname, filename, sizeof(filename));
1123
1124         /* if the link doesn't exist, no VFIO for us */
1125         if (ret < 0)
1126                 return 0;
1127
1128         ret = rte_strsplit(filename, sizeof(filename),
1129                         tok, RTE_DIM(tok), '/');
1130
1131         if (ret <= 0) {
1132                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
1133                 return -1;
1134         }
1135
1136         /* IOMMU group is always the last token */
1137         errno = 0;
1138         group_tok = tok[ret - 1];
1139         end = group_tok;
1140         *iommu_group_num = strtol(group_tok, &end, 10);
1141         if ((end != group_tok && *end != '\0') || errno != 0) {
1142                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", dev_addr);
1143                 return -1;
1144         }
1145
1146         return 1;
1147 }
1148
1149 static int
1150 type1_map(const struct rte_memseg_list *msl __rte_unused,
1151                 const struct rte_memseg *ms, void *arg)
1152 {
1153         int *vfio_container_fd = arg;
1154
1155         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1156                         ms->len, 1);
1157 }
1158
1159 static int
1160 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1161                 uint64_t len, int do_map)
1162 {
1163         struct vfio_iommu_type1_dma_map dma_map;
1164         struct vfio_iommu_type1_dma_unmap dma_unmap;
1165         int ret;
1166
1167         if (do_map != 0) {
1168                 memset(&dma_map, 0, sizeof(dma_map));
1169                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1170                 dma_map.vaddr = vaddr;
1171                 dma_map.size = len;
1172                 dma_map.iova = iova;
1173                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1174                                 VFIO_DMA_MAP_FLAG_WRITE;
1175
1176                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1177                 if (ret) {
1178                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
1179                                 errno, strerror(errno));
1180                                 return -1;
1181                 }
1182         } else {
1183                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1184                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1185                 dma_unmap.size = len;
1186                 dma_unmap.iova = iova;
1187
1188                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1189                                 &dma_unmap);
1190                 if (ret) {
1191                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1192                                         errno, strerror(errno));
1193                         return -1;
1194                 }
1195         }
1196
1197         return 0;
1198 }
1199
1200 static int
1201 vfio_type1_dma_map(int vfio_container_fd)
1202 {
1203         return rte_memseg_walk(type1_map, &vfio_container_fd);
1204 }
1205
1206 static int
1207 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1208                 uint64_t len, int do_map)
1209 {
1210         struct vfio_iommu_type1_dma_map dma_map;
1211         struct vfio_iommu_type1_dma_unmap dma_unmap;
1212         int ret;
1213
1214         if (do_map != 0) {
1215                 memset(&dma_map, 0, sizeof(dma_map));
1216                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1217                 dma_map.vaddr = vaddr;
1218                 dma_map.size = len;
1219                 dma_map.iova = iova;
1220                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1221                                 VFIO_DMA_MAP_FLAG_WRITE;
1222
1223                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1224                 if (ret) {
1225                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
1226                                 errno, strerror(errno));
1227                                 return -1;
1228                 }
1229
1230         } else {
1231                 struct vfio_iommu_spapr_register_memory reg = {
1232                         .argsz = sizeof(reg),
1233                         .flags = 0
1234                 };
1235                 reg.vaddr = (uintptr_t) vaddr;
1236                 reg.size = len;
1237
1238                 ret = ioctl(vfio_container_fd,
1239                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
1240                 if (ret) {
1241                         RTE_LOG(ERR, EAL, "  cannot unregister vaddr for IOMMU, error %i (%s)\n",
1242                                         errno, strerror(errno));
1243                         return -1;
1244                 }
1245
1246                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1247                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1248                 dma_unmap.size = len;
1249                 dma_unmap.iova = iova;
1250
1251                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1252                                 &dma_unmap);
1253                 if (ret) {
1254                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1255                                         errno, strerror(errno));
1256                         return -1;
1257                 }
1258         }
1259
1260         return 0;
1261 }
1262
1263 static int
1264 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1265                 const struct rte_memseg *ms, void *arg)
1266 {
1267         int *vfio_container_fd = arg;
1268
1269         return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1270                         ms->len, 1);
1271 }
1272
1273 struct spapr_walk_param {
1274         uint64_t window_size;
1275         uint64_t hugepage_sz;
1276 };
1277 static int
1278 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1279                 const struct rte_memseg *ms, void *arg)
1280 {
1281         struct spapr_walk_param *param = arg;
1282         uint64_t max = ms->iova + ms->len;
1283
1284         if (max > param->window_size) {
1285                 param->hugepage_sz = ms->hugepage_sz;
1286                 param->window_size = max;
1287         }
1288
1289         return 0;
1290 }
1291
1292 static int
1293 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1294                 struct vfio_iommu_spapr_tce_create *create) {
1295         struct vfio_iommu_spapr_tce_remove remove = {
1296                 .argsz = sizeof(remove),
1297         };
1298         struct vfio_iommu_spapr_tce_info info = {
1299                 .argsz = sizeof(info),
1300         };
1301         int ret;
1302
1303         /* query spapr iommu info */
1304         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1305         if (ret) {
1306                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
1307                                 "error %i (%s)\n", errno, strerror(errno));
1308                 return -1;
1309         }
1310
1311         /* remove default DMA of 32 bit window */
1312         remove.start_addr = info.dma32_window_start;
1313         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1314         if (ret) {
1315                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
1316                                 "error %i (%s)\n", errno, strerror(errno));
1317                 return -1;
1318         }
1319
1320         /* create new DMA window */
1321         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1322         if (ret) {
1323                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
1324                                 "error %i (%s)\n", errno, strerror(errno));
1325                 return -1;
1326         }
1327
1328         if (create->start_addr != 0) {
1329                 RTE_LOG(ERR, EAL, "  DMA window start address != 0\n");
1330                 return -1;
1331         }
1332
1333         return 0;
1334 }
1335
1336 static int
1337 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1338                 uint64_t len, int do_map)
1339 {
1340         struct spapr_walk_param param;
1341         struct vfio_iommu_spapr_tce_create create = {
1342                 .argsz = sizeof(create),
1343         };
1344         struct vfio_config *vfio_cfg;
1345         struct user_mem_maps *user_mem_maps;
1346         int i, ret = 0;
1347
1348         vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1349         if (vfio_cfg == NULL) {
1350                 RTE_LOG(ERR, EAL, "  invalid container fd!\n");
1351                 return -1;
1352         }
1353
1354         user_mem_maps = &vfio_cfg->mem_maps;
1355         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1356
1357         /* check if window size needs to be adjusted */
1358         memset(&param, 0, sizeof(param));
1359
1360         if (memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1361                                 &param) < 0) {
1362                 RTE_LOG(ERR, EAL, "Could not get window size\n");
1363                 ret = -1;
1364                 goto out;
1365         }
1366
1367         /* also check user maps */
1368         for (i = 0; i < user_mem_maps->n_maps; i++) {
1369                 uint64_t max = user_mem_maps->maps[i].iova +
1370                                 user_mem_maps->maps[i].len;
1371                 create.window_size = RTE_MAX(create.window_size, max);
1372         }
1373
1374         /* sPAPR requires window size to be a power of 2 */
1375         create.window_size = rte_align64pow2(param.window_size);
1376         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1377         create.levels = 1;
1378
1379         if (do_map) {
1380                 void *addr;
1381                 /* re-create window and remap the entire memory */
1382                 if (iova > create.window_size) {
1383                         if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1384                                         &create) < 0) {
1385                                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1386                                 ret = -1;
1387                                 goto out;
1388                         }
1389                         if (memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1390                                         &vfio_container_fd) < 0) {
1391                                 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1392                                 ret = -1;
1393                                 goto out;
1394                         }
1395                         /* remap all user maps */
1396                         for (i = 0; i < user_mem_maps->n_maps; i++) {
1397                                 struct user_mem_map *map =
1398                                                 &user_mem_maps->maps[i];
1399                                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1400                                                 map->addr, map->iova, map->len,
1401                                                 1)) {
1402                                         RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1403                                         ret = -1;
1404                                         goto out;
1405                                 }
1406                         }
1407                 }
1408
1409                 /* now that we've remapped all of the memory that was present
1410                  * before, map the segment that we were requested to map.
1411                  *
1412                  * however, if we were called by the callback, the memory we
1413                  * were called with was already in the memseg list, so previous
1414                  * mapping should've mapped that segment already.
1415                  *
1416                  * virt2memseg_list is a relatively cheap check, so use that. if
1417                  * memory is within any memseg list, it's a memseg, so it's
1418                  * already mapped.
1419                  */
1420                 addr = (void *)(uintptr_t)vaddr;
1421                 if (rte_mem_virt2memseg_list(addr) == NULL &&
1422                                 vfio_spapr_dma_do_map(vfio_container_fd,
1423                                         vaddr, iova, len, 1) < 0) {
1424                         RTE_LOG(ERR, EAL, "Could not map segment\n");
1425                         ret = -1;
1426                         goto out;
1427                 }
1428         } else {
1429                 /* for unmap, check if iova within DMA window */
1430                 if (iova > create.window_size) {
1431                         RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1432                         ret = -1;
1433                         goto out;
1434                 }
1435
1436                 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1437         }
1438 out:
1439         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1440         return ret;
1441 }
1442
1443 static int
1444 vfio_spapr_dma_map(int vfio_container_fd)
1445 {
1446         struct vfio_iommu_spapr_tce_create create = {
1447                 .argsz = sizeof(create),
1448         };
1449         struct spapr_walk_param param;
1450
1451         memset(&param, 0, sizeof(param));
1452
1453         /* create DMA window from 0 to max(phys_addr + len) */
1454         rte_memseg_walk(vfio_spapr_window_size_walk, &param);
1455
1456         /* sPAPR requires window size to be a power of 2 */
1457         create.window_size = rte_align64pow2(param.window_size);
1458         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1459         create.levels = 1;
1460
1461         if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1462                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1463                 return -1;
1464         }
1465
1466         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1467         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1468                 return -1;
1469
1470         return 0;
1471 }
1472
1473 static int
1474 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1475 {
1476         /* No-IOMMU mode does not need DMA mapping */
1477         return 0;
1478 }
1479
1480 static int
1481 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1482                          uint64_t __rte_unused vaddr,
1483                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
1484                          int __rte_unused do_map)
1485 {
1486         /* No-IOMMU mode does not need DMA mapping */
1487         return 0;
1488 }
1489
1490 static int
1491 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1492                 uint64_t len, int do_map)
1493 {
1494         const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1495
1496         if (!t) {
1497                 RTE_LOG(ERR, EAL, "  VFIO support not initialized\n");
1498                 rte_errno = ENODEV;
1499                 return -1;
1500         }
1501
1502         if (!t->dma_user_map_func) {
1503                 RTE_LOG(ERR, EAL,
1504                         "  VFIO custom DMA region maping not supported by IOMMU %s\n",
1505                         t->name);
1506                 rte_errno = ENOTSUP;
1507                 return -1;
1508         }
1509
1510         return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1511                         len, do_map);
1512 }
1513
1514 static int
1515 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1516                 uint64_t len)
1517 {
1518         struct user_mem_map *new_map;
1519         struct user_mem_maps *user_mem_maps;
1520         int ret = 0;
1521
1522         user_mem_maps = &vfio_cfg->mem_maps;
1523         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1524         if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1525                 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1526                 rte_errno = ENOMEM;
1527                 ret = -1;
1528                 goto out;
1529         }
1530         /* map the entry */
1531         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1532                 /* technically, this will fail if there are currently no devices
1533                  * plugged in, even if a device were added later, this mapping
1534                  * might have succeeded. however, since we cannot verify if this
1535                  * is a valid mapping without having a device attached, consider
1536                  * this to be unsupported, because we can't just store any old
1537                  * mapping and pollute list of active mappings willy-nilly.
1538                  */
1539                 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1540                 ret = -1;
1541                 goto out;
1542         }
1543         /* create new user mem map entry */
1544         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1545         new_map->addr = vaddr;
1546         new_map->iova = iova;
1547         new_map->len = len;
1548
1549         compact_user_maps(user_mem_maps);
1550 out:
1551         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1552         return ret;
1553 }
1554
1555 static int
1556 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1557                 uint64_t len)
1558 {
1559         struct user_mem_map *map, *new_map = NULL;
1560         struct user_mem_maps *user_mem_maps;
1561         int ret = 0;
1562
1563         user_mem_maps = &vfio_cfg->mem_maps;
1564         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1565
1566         /* find our mapping */
1567         map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1568         if (!map) {
1569                 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1570                 rte_errno = EINVAL;
1571                 ret = -1;
1572                 goto out;
1573         }
1574         if (map->addr != vaddr || map->iova != iova || map->len != len) {
1575                 /* we're partially unmapping a previously mapped region, so we
1576                  * need to split entry into two.
1577                  */
1578                 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1579                         RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1580                         rte_errno = ENOMEM;
1581                         ret = -1;
1582                         goto out;
1583                 }
1584                 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1585         }
1586
1587         /* unmap the entry */
1588         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1589                 /* there may not be any devices plugged in, so unmapping will
1590                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1591                  * stop us from removing the mapping, as the assumption is we
1592                  * won't be needing this memory any more and thus will want to
1593                  * prevent it from being remapped again on hotplug. so, only
1594                  * fail if we indeed failed to unmap (e.g. if the mapping was
1595                  * within our mapped range but had invalid alignment).
1596                  */
1597                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1598                         RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1599                         ret = -1;
1600                         goto out;
1601                 } else {
1602                         RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1603                 }
1604         }
1605         /* remove map from the list of active mappings */
1606         if (new_map != NULL) {
1607                 adjust_map(map, new_map, vaddr, len);
1608
1609                 /* if we've created a new map by splitting, sort everything */
1610                 if (!is_null_map(new_map)) {
1611                         compact_user_maps(user_mem_maps);
1612                 } else {
1613                         /* we've created a new mapping, but it was unused */
1614                         user_mem_maps->n_maps--;
1615                 }
1616         } else {
1617                 memset(map, 0, sizeof(*map));
1618                 compact_user_maps(user_mem_maps);
1619                 user_mem_maps->n_maps--;
1620         }
1621
1622 out:
1623         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1624         return ret;
1625 }
1626
1627 int __rte_experimental
1628 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1629 {
1630         if (len == 0) {
1631                 rte_errno = EINVAL;
1632                 return -1;
1633         }
1634
1635         return container_dma_map(default_vfio_cfg, vaddr, iova, len);
1636 }
1637
1638 int __rte_experimental
1639 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1640 {
1641         if (len == 0) {
1642                 rte_errno = EINVAL;
1643                 return -1;
1644         }
1645
1646         return container_dma_unmap(default_vfio_cfg, vaddr, iova, len);
1647 }
1648
1649 int
1650 rte_vfio_noiommu_is_enabled(void)
1651 {
1652         int fd;
1653         ssize_t cnt;
1654         char c;
1655
1656         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1657         if (fd < 0) {
1658                 if (errno != ENOENT) {
1659                         RTE_LOG(ERR, EAL, "  cannot open vfio noiommu file %i (%s)\n",
1660                                         errno, strerror(errno));
1661                         return -1;
1662                 }
1663                 /*
1664                  * else the file does not exists
1665                  * i.e. noiommu is not enabled
1666                  */
1667                 return 0;
1668         }
1669
1670         cnt = read(fd, &c, 1);
1671         close(fd);
1672         if (cnt != 1) {
1673                 RTE_LOG(ERR, EAL, "  unable to read from vfio noiommu "
1674                                 "file %i (%s)\n", errno, strerror(errno));
1675                 return -1;
1676         }
1677
1678         return c == 'Y';
1679 }
1680
1681 int __rte_experimental
1682 rte_vfio_container_create(void)
1683 {
1684         int i;
1685
1686         /* Find an empty slot to store new vfio config */
1687         for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1688                 if (vfio_cfgs[i].vfio_container_fd == -1)
1689                         break;
1690         }
1691
1692         if (i == VFIO_MAX_CONTAINERS) {
1693                 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1694                 return -1;
1695         }
1696
1697         vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1698         if (vfio_cfgs[i].vfio_container_fd < 0) {
1699                 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1700                 return -1;
1701         }
1702
1703         return vfio_cfgs[i].vfio_container_fd;
1704 }
1705
1706 int __rte_experimental
1707 rte_vfio_container_destroy(int container_fd)
1708 {
1709         struct vfio_config *vfio_cfg;
1710         int i;
1711
1712         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1713         if (vfio_cfg == NULL) {
1714                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1715                 return -1;
1716         }
1717
1718         for (i = 0; i < VFIO_MAX_GROUPS; i++)
1719                 if (vfio_cfg->vfio_groups[i].group_num != -1)
1720                         rte_vfio_container_group_unbind(container_fd,
1721                                 vfio_cfg->vfio_groups[i].group_num);
1722
1723         close(container_fd);
1724         vfio_cfg->vfio_container_fd = -1;
1725         vfio_cfg->vfio_active_groups = 0;
1726         vfio_cfg->vfio_iommu_type = NULL;
1727
1728         return 0;
1729 }
1730
1731 int __rte_experimental
1732 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1733 {
1734         struct vfio_config *vfio_cfg;
1735         struct vfio_group *cur_grp;
1736         int vfio_group_fd;
1737         int i;
1738
1739         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1740         if (vfio_cfg == NULL) {
1741                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1742                 return -1;
1743         }
1744
1745         /* Check room for new group */
1746         if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
1747                 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
1748                 return -1;
1749         }
1750
1751         /* Get an index for the new group */
1752         for (i = 0; i < VFIO_MAX_GROUPS; i++)
1753                 if (vfio_cfg->vfio_groups[i].group_num == -1) {
1754                         cur_grp = &vfio_cfg->vfio_groups[i];
1755                         break;
1756                 }
1757
1758         /* This should not happen */
1759         if (i == VFIO_MAX_GROUPS) {
1760                 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
1761                 return -1;
1762         }
1763
1764         vfio_group_fd = vfio_open_group_fd(iommu_group_num);
1765         if (vfio_group_fd < 0) {
1766                 RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
1767                 return -1;
1768         }
1769         cur_grp->group_num = iommu_group_num;
1770         cur_grp->fd = vfio_group_fd;
1771         cur_grp->devices = 0;
1772         vfio_cfg->vfio_active_groups++;
1773
1774         return vfio_group_fd;
1775 }
1776
1777 int __rte_experimental
1778 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
1779 {
1780         struct vfio_config *vfio_cfg;
1781         struct vfio_group *cur_grp;
1782         int i;
1783
1784         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1785         if (vfio_cfg == NULL) {
1786                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1787                 return -1;
1788         }
1789
1790         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
1791                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
1792                         cur_grp = &vfio_cfg->vfio_groups[i];
1793                         break;
1794                 }
1795         }
1796
1797         /* This should not happen */
1798         if (i == VFIO_MAX_GROUPS) {
1799                 RTE_LOG(ERR, EAL, "Specified group number not found\n");
1800                 return -1;
1801         }
1802
1803         if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
1804                 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
1805                         " iommu_group_num %d\n", iommu_group_num);
1806                 return -1;
1807         }
1808         cur_grp->group_num = -1;
1809         cur_grp->fd = -1;
1810         cur_grp->devices = 0;
1811         vfio_cfg->vfio_active_groups--;
1812
1813         return 0;
1814 }
1815
1816 int __rte_experimental
1817 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
1818                 uint64_t len)
1819 {
1820         struct vfio_config *vfio_cfg;
1821
1822         if (len == 0) {
1823                 rte_errno = EINVAL;
1824                 return -1;
1825         }
1826
1827         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1828         if (vfio_cfg == NULL) {
1829                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1830                 return -1;
1831         }
1832
1833         return container_dma_map(vfio_cfg, vaddr, iova, len);
1834 }
1835
1836 int __rte_experimental
1837 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
1838                 uint64_t len)
1839 {
1840         struct vfio_config *vfio_cfg;
1841
1842         if (len == 0) {
1843                 rte_errno = EINVAL;
1844                 return -1;
1845         }
1846
1847         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1848         if (vfio_cfg == NULL) {
1849                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1850                 return -1;
1851         }
1852
1853         return container_dma_unmap(vfio_cfg, vaddr, iova, len);
1854 }
1855
1856 #else
1857
1858 int __rte_experimental
1859 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1860                   __rte_unused uint64_t len)
1861 {
1862         return -1;
1863 }
1864
1865 int __rte_experimental
1866 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1867                     __rte_unused uint64_t len)
1868 {
1869         return -1;
1870 }
1871
1872 int
1873 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
1874                 __rte_unused const char *dev_addr,
1875                 __rte_unused int *vfio_dev_fd,
1876                 __rte_unused struct vfio_device_info *device_info)
1877 {
1878         return -1;
1879 }
1880
1881 int
1882 rte_vfio_release_device(__rte_unused const char *sysfs_base,
1883                 __rte_unused const char *dev_addr, __rte_unused int fd)
1884 {
1885         return -1;
1886 }
1887
1888 int
1889 rte_vfio_enable(__rte_unused const char *modname)
1890 {
1891         return -1;
1892 }
1893
1894 int
1895 rte_vfio_is_enabled(__rte_unused const char *modname)
1896 {
1897         return -1;
1898 }
1899
1900 int
1901 rte_vfio_noiommu_is_enabled(void)
1902 {
1903         return -1;
1904 }
1905
1906 int
1907 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
1908 {
1909         return -1;
1910 }
1911
1912 int __rte_experimental
1913 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
1914                 __rte_unused const char *dev_addr,
1915                 __rte_unused int *iommu_group_num)
1916 {
1917         return -1;
1918 }
1919
1920 int __rte_experimental
1921 rte_vfio_get_container_fd(void)
1922 {
1923         return -1;
1924 }
1925
1926 int __rte_experimental
1927 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
1928 {
1929         return -1;
1930 }
1931
1932 int __rte_experimental
1933 rte_vfio_container_create(void)
1934 {
1935         return -1;
1936 }
1937
1938 int __rte_experimental
1939 rte_vfio_container_destroy(__rte_unused int container_fd)
1940 {
1941         return -1;
1942 }
1943
1944 int __rte_experimental
1945 rte_vfio_container_group_bind(__rte_unused int container_fd,
1946                 __rte_unused int iommu_group_num)
1947 {
1948         return -1;
1949 }
1950
1951 int __rte_experimental
1952 rte_vfio_container_group_unbind(__rte_unused int container_fd,
1953                 __rte_unused int iommu_group_num)
1954 {
1955         return -1;
1956 }
1957
1958 int __rte_experimental
1959 rte_vfio_container_dma_map(__rte_unused int container_fd,
1960                 __rte_unused uint64_t vaddr,
1961                 __rte_unused uint64_t iova,
1962                 __rte_unused uint64_t len)
1963 {
1964         return -1;
1965 }
1966
1967 int __rte_experimental
1968 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
1969                 __rte_unused uint64_t vaddr,
1970                 __rte_unused uint64_t iova,
1971                 __rte_unused uint64_t len)
1972 {
1973         return -1;
1974 }
1975
1976 #endif /* VFIO_PRESENT */