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