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