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