xref: /spdk/lib/env_dpdk/memory.c (revision 2826ef13ceb40afd57a87c8f0757dfe9c350bbe4)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "env_internal.h"
9 
10 #include <rte_config.h>
11 #include <rte_memory.h>
12 #include <rte_eal_memconfig.h>
13 
14 #include "spdk_internal/assert.h"
15 
16 #include "spdk/assert.h"
17 #include "spdk/likely.h"
18 #include "spdk/queue.h"
19 #include "spdk/util.h"
20 #include "spdk/memory.h"
21 #include "spdk/env_dpdk.h"
22 #include "spdk/log.h"
23 
24 #ifndef __linux__
25 #define VFIO_ENABLED 0
26 #else
27 #include <linux/version.h>
28 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
29 #define VFIO_ENABLED 1
30 #include <linux/vfio.h>
31 #include <rte_vfio.h>
32 
33 struct spdk_vfio_dma_map {
34 	struct vfio_iommu_type1_dma_map map;
35 	TAILQ_ENTRY(spdk_vfio_dma_map) tailq;
36 };
37 
38 struct vfio_cfg {
39 	int fd;
40 	bool enabled;
41 	bool noiommu_enabled;
42 	unsigned device_ref;
43 	TAILQ_HEAD(, spdk_vfio_dma_map) maps;
44 	pthread_mutex_t mutex;
45 };
46 
47 static struct vfio_cfg g_vfio = {
48 	.fd = -1,
49 	.enabled = false,
50 	.noiommu_enabled = false,
51 	.device_ref = 0,
52 	.maps = TAILQ_HEAD_INITIALIZER(g_vfio.maps),
53 	.mutex = PTHREAD_MUTEX_INITIALIZER
54 };
55 
56 #else
57 #define VFIO_ENABLED 0
58 #endif
59 #endif
60 
61 #if DEBUG
62 #define DEBUG_PRINT(...) SPDK_ERRLOG(__VA_ARGS__)
63 #else
64 #define DEBUG_PRINT(...)
65 #endif
66 
67 #define FN_2MB_TO_4KB(fn)	(fn << (SHIFT_2MB - SHIFT_4KB))
68 #define FN_4KB_TO_2MB(fn)	(fn >> (SHIFT_2MB - SHIFT_4KB))
69 
70 #define MAP_256TB_IDX(vfn_2mb)	((vfn_2mb) >> (SHIFT_1GB - SHIFT_2MB))
71 #define MAP_1GB_IDX(vfn_2mb)	((vfn_2mb) & ((1ULL << (SHIFT_1GB - SHIFT_2MB)) - 1))
72 
73 /* Page is registered */
74 #define REG_MAP_REGISTERED	(1ULL << 62)
75 
76 /* A notification region barrier. The 2MB translation entry that's marked
77  * with this flag must be unregistered separately. This allows contiguous
78  * regions to be unregistered in the same chunks they were registered.
79  */
80 #define REG_MAP_NOTIFY_START	(1ULL << 63)
81 
82 /* Translation of a single 2MB page. */
83 struct map_2mb {
84 	uint64_t translation_2mb;
85 };
86 
87 /* Second-level map table indexed by bits [21..29] of the virtual address.
88  * Each entry contains the address translation or error for entries that haven't
89  * been retrieved yet.
90  */
91 struct map_1gb {
92 	struct map_2mb map[1ULL << (SHIFT_1GB - SHIFT_2MB)];
93 };
94 
95 /* Top-level map table indexed by bits [30..47] of the virtual address.
96  * Each entry points to a second-level map table or NULL.
97  */
98 struct map_256tb {
99 	struct map_1gb *map[1ULL << (SHIFT_256TB - SHIFT_1GB)];
100 };
101 
102 /* Page-granularity memory address translation */
103 struct spdk_mem_map {
104 	struct map_256tb map_256tb;
105 	pthread_mutex_t mutex;
106 	uint64_t default_translation;
107 	struct spdk_mem_map_ops ops;
108 	void *cb_ctx;
109 	TAILQ_ENTRY(spdk_mem_map) tailq;
110 };
111 
112 /* Registrations map. The 64 bit translations are bit fields with the
113  * following layout (starting with the low bits):
114  *    0 - 61 : reserved
115  *   62 - 63 : flags
116  */
117 static struct spdk_mem_map *g_mem_reg_map;
118 static TAILQ_HEAD(spdk_mem_map_head, spdk_mem_map) g_spdk_mem_maps =
119 	TAILQ_HEAD_INITIALIZER(g_spdk_mem_maps);
120 static pthread_mutex_t g_spdk_mem_map_mutex = PTHREAD_MUTEX_INITIALIZER;
121 
122 static bool g_legacy_mem;
123 
124 /*
125  * Walk the currently registered memory via the main memory registration map
126  * and call the new map's notify callback for each virtually contiguous region.
127  */
128 static int
129 mem_map_notify_walk(struct spdk_mem_map *map, enum spdk_mem_map_notify_action action)
130 {
131 	size_t idx_256tb;
132 	uint64_t idx_1gb;
133 	uint64_t contig_start = UINT64_MAX;
134 	uint64_t contig_end = UINT64_MAX;
135 	struct map_1gb *map_1gb;
136 	int rc;
137 
138 	if (!g_mem_reg_map) {
139 		return -EINVAL;
140 	}
141 
142 	/* Hold the memory registration map mutex so no new registrations can be added while we are looping. */
143 	pthread_mutex_lock(&g_mem_reg_map->mutex);
144 
145 	for (idx_256tb = 0;
146 	     idx_256tb < sizeof(g_mem_reg_map->map_256tb.map) / sizeof(g_mem_reg_map->map_256tb.map[0]);
147 	     idx_256tb++) {
148 		map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
149 
150 		if (!map_1gb) {
151 			if (contig_start != UINT64_MAX) {
152 				/* End of of a virtually contiguous range */
153 				rc = map->ops.notify_cb(map->cb_ctx, map, action,
154 							(void *)contig_start,
155 							contig_end - contig_start + VALUE_2MB);
156 				/* Don't bother handling unregister failures. It can't be any worse */
157 				if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
158 					goto err_unregister;
159 				}
160 			}
161 			contig_start = UINT64_MAX;
162 			continue;
163 		}
164 
165 		for (idx_1gb = 0; idx_1gb < sizeof(map_1gb->map) / sizeof(map_1gb->map[0]); idx_1gb++) {
166 			if ((map_1gb->map[idx_1gb].translation_2mb & REG_MAP_REGISTERED) &&
167 			    (contig_start == UINT64_MAX ||
168 			     (map_1gb->map[idx_1gb].translation_2mb & REG_MAP_NOTIFY_START) == 0)) {
169 				/* Rebuild the virtual address from the indexes */
170 				uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << SHIFT_2MB);
171 
172 				if (contig_start == UINT64_MAX) {
173 					contig_start = vaddr;
174 				}
175 
176 				contig_end = vaddr;
177 			} else {
178 				if (contig_start != UINT64_MAX) {
179 					/* End of of a virtually contiguous range */
180 					rc = map->ops.notify_cb(map->cb_ctx, map, action,
181 								(void *)contig_start,
182 								contig_end - contig_start + VALUE_2MB);
183 					/* Don't bother handling unregister failures. It can't be any worse */
184 					if (rc != 0 && action == SPDK_MEM_MAP_NOTIFY_REGISTER) {
185 						goto err_unregister;
186 					}
187 
188 					/* This page might be a part of a neighbour region, so process
189 					 * it again. The idx_1gb will be incremented immediately.
190 					 */
191 					idx_1gb--;
192 				}
193 				contig_start = UINT64_MAX;
194 			}
195 		}
196 	}
197 
198 	pthread_mutex_unlock(&g_mem_reg_map->mutex);
199 	return 0;
200 
201 err_unregister:
202 	/* Unwind to the first empty translation so we don't unregister
203 	 * a region that just failed to register.
204 	 */
205 	idx_256tb = MAP_256TB_IDX((contig_start >> SHIFT_2MB) - 1);
206 	idx_1gb = MAP_1GB_IDX((contig_start >> SHIFT_2MB) - 1);
207 	contig_start = UINT64_MAX;
208 	contig_end = UINT64_MAX;
209 
210 	/* Unregister any memory we managed to register before the failure */
211 	for (; idx_256tb < SIZE_MAX; idx_256tb--) {
212 		map_1gb = g_mem_reg_map->map_256tb.map[idx_256tb];
213 
214 		if (!map_1gb) {
215 			if (contig_end != UINT64_MAX) {
216 				/* End of of a virtually contiguous range */
217 				map->ops.notify_cb(map->cb_ctx, map,
218 						   SPDK_MEM_MAP_NOTIFY_UNREGISTER,
219 						   (void *)contig_start,
220 						   contig_end - contig_start + VALUE_2MB);
221 			}
222 			contig_end = UINT64_MAX;
223 			continue;
224 		}
225 
226 		for (; idx_1gb < UINT64_MAX; idx_1gb--) {
227 			/* Rebuild the virtual address from the indexes */
228 			uint64_t vaddr = (idx_256tb << SHIFT_1GB) | (idx_1gb << SHIFT_2MB);
229 			if ((map_1gb->map[idx_1gb].translation_2mb & REG_MAP_REGISTERED) &&
230 			    (contig_end == UINT64_MAX || (map_1gb->map[idx_1gb].translation_2mb & REG_MAP_NOTIFY_START) == 0)) {
231 
232 				if (contig_end == UINT64_MAX) {
233 					contig_end = vaddr;
234 				}
235 				contig_start = vaddr;
236 			} else {
237 				if (contig_end != UINT64_MAX) {
238 					if (map_1gb->map[idx_1gb].translation_2mb & REG_MAP_NOTIFY_START) {
239 						contig_start = vaddr;
240 					}
241 					/* End of of a virtually contiguous range */
242 					map->ops.notify_cb(map->cb_ctx, map,
243 							   SPDK_MEM_MAP_NOTIFY_UNREGISTER,
244 							   (void *)contig_start,
245 							   contig_end - contig_start + VALUE_2MB);
246 				}
247 				contig_end = UINT64_MAX;
248 			}
249 		}
250 		idx_1gb = sizeof(map_1gb->map) / sizeof(map_1gb->map[0]) - 1;
251 	}
252 
253 	pthread_mutex_unlock(&g_mem_reg_map->mutex);
254 	return rc;
255 }
256 
257 struct spdk_mem_map *
258 spdk_mem_map_alloc(uint64_t default_translation, const struct spdk_mem_map_ops *ops, void *cb_ctx)
259 {
260 	struct spdk_mem_map *map;
261 	int rc;
262 	size_t i;
263 
264 	map = calloc(1, sizeof(*map));
265 	if (map == NULL) {
266 		return NULL;
267 	}
268 
269 	if (pthread_mutex_init(&map->mutex, NULL)) {
270 		free(map);
271 		return NULL;
272 	}
273 
274 	map->default_translation = default_translation;
275 	map->cb_ctx = cb_ctx;
276 	if (ops) {
277 		map->ops = *ops;
278 	}
279 
280 	if (ops && ops->notify_cb) {
281 		pthread_mutex_lock(&g_spdk_mem_map_mutex);
282 		rc = mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_REGISTER);
283 		if (rc != 0) {
284 			pthread_mutex_unlock(&g_spdk_mem_map_mutex);
285 			DEBUG_PRINT("Initial mem_map notify failed\n");
286 			pthread_mutex_destroy(&map->mutex);
287 			for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
288 				free(map->map_256tb.map[i]);
289 			}
290 			free(map);
291 			return NULL;
292 		}
293 		TAILQ_INSERT_TAIL(&g_spdk_mem_maps, map, tailq);
294 		pthread_mutex_unlock(&g_spdk_mem_map_mutex);
295 	}
296 
297 	return map;
298 }
299 
300 void
301 spdk_mem_map_free(struct spdk_mem_map **pmap)
302 {
303 	struct spdk_mem_map *map;
304 	size_t i;
305 
306 	if (!pmap) {
307 		return;
308 	}
309 
310 	map = *pmap;
311 
312 	if (!map) {
313 		return;
314 	}
315 
316 	if (map->ops.notify_cb) {
317 		pthread_mutex_lock(&g_spdk_mem_map_mutex);
318 		mem_map_notify_walk(map, SPDK_MEM_MAP_NOTIFY_UNREGISTER);
319 		TAILQ_REMOVE(&g_spdk_mem_maps, map, tailq);
320 		pthread_mutex_unlock(&g_spdk_mem_map_mutex);
321 	}
322 
323 	for (i = 0; i < sizeof(map->map_256tb.map) / sizeof(map->map_256tb.map[0]); i++) {
324 		free(map->map_256tb.map[i]);
325 	}
326 
327 	pthread_mutex_destroy(&map->mutex);
328 
329 	free(map);
330 	*pmap = NULL;
331 }
332 
333 int
334 spdk_mem_register(void *vaddr, size_t len)
335 {
336 	struct spdk_mem_map *map;
337 	int rc;
338 	void *seg_vaddr;
339 	size_t seg_len;
340 	uint64_t reg;
341 
342 	if ((uintptr_t)vaddr & ~MASK_256TB) {
343 		DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
344 		return -EINVAL;
345 	}
346 
347 	if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
348 		DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
349 			    __func__, vaddr, len);
350 		return -EINVAL;
351 	}
352 
353 	if (len == 0) {
354 		return 0;
355 	}
356 
357 	pthread_mutex_lock(&g_spdk_mem_map_mutex);
358 
359 	seg_vaddr = vaddr;
360 	seg_len = len;
361 	while (seg_len > 0) {
362 		reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
363 		if (reg & REG_MAP_REGISTERED) {
364 			pthread_mutex_unlock(&g_spdk_mem_map_mutex);
365 			return -EBUSY;
366 		}
367 		seg_vaddr += VALUE_2MB;
368 		seg_len -= VALUE_2MB;
369 	}
370 
371 	seg_vaddr = vaddr;
372 	seg_len = 0;
373 	while (len > 0) {
374 		spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, VALUE_2MB,
375 					     seg_len == 0 ? REG_MAP_REGISTERED | REG_MAP_NOTIFY_START : REG_MAP_REGISTERED);
376 		seg_len += VALUE_2MB;
377 		vaddr += VALUE_2MB;
378 		len -= VALUE_2MB;
379 	}
380 
381 	TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
382 		rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_REGISTER, seg_vaddr, seg_len);
383 		if (rc != 0) {
384 			pthread_mutex_unlock(&g_spdk_mem_map_mutex);
385 			return rc;
386 		}
387 	}
388 
389 	pthread_mutex_unlock(&g_spdk_mem_map_mutex);
390 	return 0;
391 }
392 
393 int
394 spdk_mem_unregister(void *vaddr, size_t len)
395 {
396 	struct spdk_mem_map *map;
397 	int rc;
398 	void *seg_vaddr;
399 	size_t seg_len;
400 	uint64_t reg, newreg;
401 
402 	if ((uintptr_t)vaddr & ~MASK_256TB) {
403 		DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
404 		return -EINVAL;
405 	}
406 
407 	if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
408 		DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
409 			    __func__, vaddr, len);
410 		return -EINVAL;
411 	}
412 
413 	pthread_mutex_lock(&g_spdk_mem_map_mutex);
414 
415 	/* The first page must be a start of a region. Also check if it's
416 	 * registered to make sure we don't return -ERANGE for non-registered
417 	 * regions.
418 	 */
419 	reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
420 	if ((reg & REG_MAP_REGISTERED) && (reg & REG_MAP_NOTIFY_START) == 0) {
421 		pthread_mutex_unlock(&g_spdk_mem_map_mutex);
422 		return -ERANGE;
423 	}
424 
425 	seg_vaddr = vaddr;
426 	seg_len = len;
427 	while (seg_len > 0) {
428 		reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
429 		if ((reg & REG_MAP_REGISTERED) == 0) {
430 			pthread_mutex_unlock(&g_spdk_mem_map_mutex);
431 			return -EINVAL;
432 		}
433 		seg_vaddr += VALUE_2MB;
434 		seg_len -= VALUE_2MB;
435 	}
436 
437 	newreg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
438 	/* If the next page is registered, it must be a start of a region as well,
439 	 * otherwise we'd be unregistering only a part of a region.
440 	 */
441 	if ((newreg & REG_MAP_NOTIFY_START) == 0 && (newreg & REG_MAP_REGISTERED)) {
442 		pthread_mutex_unlock(&g_spdk_mem_map_mutex);
443 		return -ERANGE;
444 	}
445 	seg_vaddr = vaddr;
446 	seg_len = 0;
447 
448 	while (len > 0) {
449 		reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)vaddr, NULL);
450 		spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, VALUE_2MB, 0);
451 
452 		if (seg_len > 0 && (reg & REG_MAP_NOTIFY_START)) {
453 			TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
454 				rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER, seg_vaddr, seg_len);
455 				if (rc != 0) {
456 					pthread_mutex_unlock(&g_spdk_mem_map_mutex);
457 					return rc;
458 				}
459 			}
460 
461 			seg_vaddr = vaddr;
462 			seg_len = VALUE_2MB;
463 		} else {
464 			seg_len += VALUE_2MB;
465 		}
466 
467 		vaddr += VALUE_2MB;
468 		len -= VALUE_2MB;
469 	}
470 
471 	if (seg_len > 0) {
472 		TAILQ_FOREACH_REVERSE(map, &g_spdk_mem_maps, spdk_mem_map_head, tailq) {
473 			rc = map->ops.notify_cb(map->cb_ctx, map, SPDK_MEM_MAP_NOTIFY_UNREGISTER, seg_vaddr, seg_len);
474 			if (rc != 0) {
475 				pthread_mutex_unlock(&g_spdk_mem_map_mutex);
476 				return rc;
477 			}
478 		}
479 	}
480 
481 	pthread_mutex_unlock(&g_spdk_mem_map_mutex);
482 	return 0;
483 }
484 
485 int
486 spdk_mem_reserve(void *vaddr, size_t len)
487 {
488 	struct spdk_mem_map *map;
489 	void *seg_vaddr;
490 	size_t seg_len;
491 	uint64_t reg;
492 
493 	if ((uintptr_t)vaddr & ~MASK_256TB) {
494 		DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
495 		return -EINVAL;
496 	}
497 
498 	if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
499 		DEBUG_PRINT("invalid %s parameters, vaddr=%p len=%ju\n",
500 			    __func__, vaddr, len);
501 		return -EINVAL;
502 	}
503 
504 	if (len == 0) {
505 		return 0;
506 	}
507 
508 	pthread_mutex_lock(&g_spdk_mem_map_mutex);
509 
510 	/* Check if any part of this range is already registered */
511 	seg_vaddr = vaddr;
512 	seg_len = len;
513 	while (seg_len > 0) {
514 		reg = spdk_mem_map_translate(g_mem_reg_map, (uint64_t)seg_vaddr, NULL);
515 		if (reg & REG_MAP_REGISTERED) {
516 			pthread_mutex_unlock(&g_spdk_mem_map_mutex);
517 			return -EBUSY;
518 		}
519 		seg_vaddr += VALUE_2MB;
520 		seg_len -= VALUE_2MB;
521 	}
522 
523 	/* Simply set the translation to the memory map's default. This allocates the space in the
524 	 * map but does not provide a valid translation. */
525 	spdk_mem_map_set_translation(g_mem_reg_map, (uint64_t)vaddr, len,
526 				     g_mem_reg_map->default_translation);
527 
528 	TAILQ_FOREACH(map, &g_spdk_mem_maps, tailq) {
529 		spdk_mem_map_set_translation(map, (uint64_t)vaddr, len, map->default_translation);
530 	}
531 
532 	pthread_mutex_unlock(&g_spdk_mem_map_mutex);
533 	return 0;
534 }
535 
536 static struct map_1gb *
537 mem_map_get_map_1gb(struct spdk_mem_map *map, uint64_t vfn_2mb)
538 {
539 	struct map_1gb *map_1gb;
540 	uint64_t idx_256tb = MAP_256TB_IDX(vfn_2mb);
541 	size_t i;
542 
543 	if (spdk_unlikely(idx_256tb >= SPDK_COUNTOF(map->map_256tb.map))) {
544 		return NULL;
545 	}
546 
547 	map_1gb = map->map_256tb.map[idx_256tb];
548 
549 	if (!map_1gb) {
550 		pthread_mutex_lock(&map->mutex);
551 
552 		/* Recheck to make sure nobody else got the mutex first. */
553 		map_1gb = map->map_256tb.map[idx_256tb];
554 		if (!map_1gb) {
555 			map_1gb = malloc(sizeof(struct map_1gb));
556 			if (map_1gb) {
557 				/* initialize all entries to default translation */
558 				for (i = 0; i < SPDK_COUNTOF(map_1gb->map); i++) {
559 					map_1gb->map[i].translation_2mb = map->default_translation;
560 				}
561 				map->map_256tb.map[idx_256tb] = map_1gb;
562 			}
563 		}
564 
565 		pthread_mutex_unlock(&map->mutex);
566 
567 		if (!map_1gb) {
568 			DEBUG_PRINT("allocation failed\n");
569 			return NULL;
570 		}
571 	}
572 
573 	return map_1gb;
574 }
575 
576 int
577 spdk_mem_map_set_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size,
578 			     uint64_t translation)
579 {
580 	uint64_t vfn_2mb;
581 	struct map_1gb *map_1gb;
582 	uint64_t idx_1gb;
583 	struct map_2mb *map_2mb;
584 
585 	if ((uintptr_t)vaddr & ~MASK_256TB) {
586 		DEBUG_PRINT("invalid usermode virtual address %" PRIu64 "\n", vaddr);
587 		return -EINVAL;
588 	}
589 
590 	/* For now, only 2 MB-aligned registrations are supported */
591 	if (((uintptr_t)vaddr & MASK_2MB) || (size & MASK_2MB)) {
592 		DEBUG_PRINT("invalid %s parameters, vaddr=%" PRIu64 " len=%" PRIu64 "\n",
593 			    __func__, vaddr, size);
594 		return -EINVAL;
595 	}
596 
597 	vfn_2mb = vaddr >> SHIFT_2MB;
598 
599 	while (size) {
600 		map_1gb = mem_map_get_map_1gb(map, vfn_2mb);
601 		if (!map_1gb) {
602 			DEBUG_PRINT("could not get %p map\n", (void *)vaddr);
603 			return -ENOMEM;
604 		}
605 
606 		idx_1gb = MAP_1GB_IDX(vfn_2mb);
607 		map_2mb = &map_1gb->map[idx_1gb];
608 		map_2mb->translation_2mb = translation;
609 
610 		size -= VALUE_2MB;
611 		vfn_2mb++;
612 	}
613 
614 	return 0;
615 }
616 
617 int
618 spdk_mem_map_clear_translation(struct spdk_mem_map *map, uint64_t vaddr, uint64_t size)
619 {
620 	return spdk_mem_map_set_translation(map, vaddr, size, map->default_translation);
621 }
622 
623 inline uint64_t
624 spdk_mem_map_translate(const struct spdk_mem_map *map, uint64_t vaddr, uint64_t *size)
625 {
626 	const struct map_1gb *map_1gb;
627 	const struct map_2mb *map_2mb;
628 	uint64_t idx_256tb;
629 	uint64_t idx_1gb;
630 	uint64_t vfn_2mb;
631 	uint64_t cur_size;
632 	uint64_t prev_translation;
633 	uint64_t orig_translation;
634 
635 	if (spdk_unlikely(vaddr & ~MASK_256TB)) {
636 		DEBUG_PRINT("invalid usermode virtual address %p\n", (void *)vaddr);
637 		return map->default_translation;
638 	}
639 
640 	vfn_2mb = vaddr >> SHIFT_2MB;
641 	idx_256tb = MAP_256TB_IDX(vfn_2mb);
642 	idx_1gb = MAP_1GB_IDX(vfn_2mb);
643 
644 	map_1gb = map->map_256tb.map[idx_256tb];
645 	if (spdk_unlikely(!map_1gb)) {
646 		return map->default_translation;
647 	}
648 
649 	cur_size = VALUE_2MB - _2MB_OFFSET(vaddr);
650 	map_2mb = &map_1gb->map[idx_1gb];
651 	if (size == NULL || map->ops.are_contiguous == NULL ||
652 	    map_2mb->translation_2mb == map->default_translation) {
653 		if (size != NULL) {
654 			*size = spdk_min(*size, cur_size);
655 		}
656 		return map_2mb->translation_2mb;
657 	}
658 
659 	orig_translation = map_2mb->translation_2mb;
660 	prev_translation = orig_translation;
661 	while (cur_size < *size) {
662 		vfn_2mb++;
663 		idx_256tb = MAP_256TB_IDX(vfn_2mb);
664 		idx_1gb = MAP_1GB_IDX(vfn_2mb);
665 
666 		map_1gb = map->map_256tb.map[idx_256tb];
667 		if (spdk_unlikely(!map_1gb)) {
668 			break;
669 		}
670 
671 		map_2mb = &map_1gb->map[idx_1gb];
672 		if (!map->ops.are_contiguous(prev_translation, map_2mb->translation_2mb)) {
673 			break;
674 		}
675 
676 		cur_size += VALUE_2MB;
677 		prev_translation = map_2mb->translation_2mb;
678 	}
679 
680 	*size = spdk_min(*size, cur_size);
681 	return orig_translation;
682 }
683 
684 static void
685 memory_hotplug_cb(enum rte_mem_event event_type,
686 		  const void *addr, size_t len, void *arg)
687 {
688 	if (event_type == RTE_MEM_EVENT_ALLOC) {
689 		spdk_mem_register((void *)addr, len);
690 
691 		if (!spdk_env_dpdk_external_init()) {
692 			return;
693 		}
694 
695 		/* When the user initialized DPDK separately, we can't
696 		 * be sure that --match-allocations RTE flag was specified.
697 		 * Without this flag, DPDK can free memory in different units
698 		 * than it was allocated. It doesn't work with things like RDMA MRs.
699 		 *
700 		 * For such cases, we mark segments so they aren't freed.
701 		 */
702 		while (len > 0) {
703 			struct rte_memseg *seg;
704 
705 			seg = rte_mem_virt2memseg(addr, NULL);
706 			assert(seg != NULL);
707 			seg->flags |= RTE_MEMSEG_FLAG_DO_NOT_FREE;
708 			addr = (void *)((uintptr_t)addr + seg->hugepage_sz);
709 			len -= seg->hugepage_sz;
710 		}
711 	} else if (event_type == RTE_MEM_EVENT_FREE) {
712 		spdk_mem_unregister((void *)addr, len);
713 	}
714 }
715 
716 static int
717 memory_iter_cb(const struct rte_memseg_list *msl,
718 	       const struct rte_memseg *ms, size_t len, void *arg)
719 {
720 	return spdk_mem_register(ms->addr, len);
721 }
722 
723 int
724 mem_map_init(bool legacy_mem)
725 {
726 	g_legacy_mem = legacy_mem;
727 
728 	g_mem_reg_map = spdk_mem_map_alloc(0, NULL, NULL);
729 	if (g_mem_reg_map == NULL) {
730 		DEBUG_PRINT("memory registration map allocation failed\n");
731 		return -ENOMEM;
732 	}
733 
734 	/*
735 	 * Walk all DPDK memory segments and register them
736 	 * with the main memory map
737 	 */
738 	rte_mem_event_callback_register("spdk", memory_hotplug_cb, NULL);
739 	rte_memseg_contig_walk(memory_iter_cb, NULL);
740 	return 0;
741 }
742 
743 bool
744 spdk_iommu_is_enabled(void)
745 {
746 #if VFIO_ENABLED
747 	return g_vfio.enabled && !g_vfio.noiommu_enabled;
748 #else
749 	return false;
750 #endif
751 }
752 
753 struct spdk_vtophys_pci_device {
754 	struct rte_pci_device *pci_device;
755 	TAILQ_ENTRY(spdk_vtophys_pci_device) tailq;
756 };
757 
758 static pthread_mutex_t g_vtophys_pci_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
759 static TAILQ_HEAD(, spdk_vtophys_pci_device) g_vtophys_pci_devices =
760 	TAILQ_HEAD_INITIALIZER(g_vtophys_pci_devices);
761 
762 static struct spdk_mem_map *g_vtophys_map;
763 static struct spdk_mem_map *g_phys_ref_map;
764 
765 #if VFIO_ENABLED
766 static int
767 vtophys_iommu_map_dma(uint64_t vaddr, uint64_t iova, uint64_t size)
768 {
769 	struct spdk_vfio_dma_map *dma_map;
770 	uint64_t refcount;
771 	int ret;
772 
773 	refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
774 	assert(refcount < UINT64_MAX);
775 	if (refcount > 0) {
776 		spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
777 		return 0;
778 	}
779 
780 	dma_map = calloc(1, sizeof(*dma_map));
781 	if (dma_map == NULL) {
782 		return -ENOMEM;
783 	}
784 
785 	dma_map->map.argsz = sizeof(dma_map->map);
786 	dma_map->map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
787 	dma_map->map.vaddr = vaddr;
788 	dma_map->map.iova = iova;
789 	dma_map->map.size = size;
790 
791 	pthread_mutex_lock(&g_vfio.mutex);
792 	if (g_vfio.device_ref == 0) {
793 		/* VFIO requires at least one device (IOMMU group) to be added to
794 		 * a VFIO container before it is possible to perform any IOMMU
795 		 * operations on that container. This memory will be mapped once
796 		 * the first device (IOMMU group) is hotplugged.
797 		 *
798 		 * Since the vfio container is managed internally by DPDK, it is
799 		 * also possible that some device is already in that container, but
800 		 * it's not managed by SPDK -  e.g. an NIC attached internally
801 		 * inside DPDK. We could map the memory straight away in such
802 		 * scenario, but there's no need to do it. DPDK devices clearly
803 		 * don't need our mappings and hence we defer the mapping
804 		 * unconditionally until the first SPDK-managed device is
805 		 * hotplugged.
806 		 */
807 		goto out_insert;
808 	}
809 
810 	ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
811 	if (ret) {
812 		/* There are cases the vfio container doesn't have IOMMU group, it's safe for this case */
813 		SPDK_NOTICELOG("Cannot set up DMA mapping, error %d, ignored\n", errno);
814 	}
815 
816 out_insert:
817 	TAILQ_INSERT_TAIL(&g_vfio.maps, dma_map, tailq);
818 	pthread_mutex_unlock(&g_vfio.mutex);
819 	spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount + 1);
820 	return 0;
821 }
822 
823 static int
824 vtophys_iommu_unmap_dma(uint64_t iova, uint64_t size)
825 {
826 	struct spdk_vfio_dma_map *dma_map;
827 	uint64_t refcount;
828 	int ret;
829 	struct vfio_iommu_type1_dma_unmap unmap = {};
830 
831 	pthread_mutex_lock(&g_vfio.mutex);
832 	TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
833 		if (dma_map->map.iova == iova) {
834 			break;
835 		}
836 	}
837 
838 	if (dma_map == NULL) {
839 		DEBUG_PRINT("Cannot clear DMA mapping for IOVA %"PRIx64" - it's not mapped\n", iova);
840 		pthread_mutex_unlock(&g_vfio.mutex);
841 		return -ENXIO;
842 	}
843 
844 	refcount = spdk_mem_map_translate(g_phys_ref_map, iova, NULL);
845 	assert(refcount < UINT64_MAX);
846 	if (refcount > 0) {
847 		spdk_mem_map_set_translation(g_phys_ref_map, iova, size, refcount - 1);
848 	}
849 
850 	/* We still have outstanding references, don't clear it. */
851 	if (refcount > 1) {
852 		pthread_mutex_unlock(&g_vfio.mutex);
853 		return 0;
854 	}
855 
856 	/** don't support partial or multiple-page unmap for now */
857 	assert(dma_map->map.size == size);
858 
859 	if (g_vfio.device_ref == 0) {
860 		/* Memory is not mapped anymore, just remove it's references */
861 		goto out_remove;
862 	}
863 
864 	unmap.argsz = sizeof(unmap);
865 	unmap.flags = 0;
866 	unmap.iova = dma_map->map.iova;
867 	unmap.size = dma_map->map.size;
868 	ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
869 	if (ret) {
870 		SPDK_NOTICELOG("Cannot clear DMA mapping, error %d, ignored\n", errno);
871 	}
872 
873 out_remove:
874 	TAILQ_REMOVE(&g_vfio.maps, dma_map, tailq);
875 	pthread_mutex_unlock(&g_vfio.mutex);
876 	free(dma_map);
877 	return 0;
878 }
879 #endif
880 
881 static uint64_t
882 vtophys_get_paddr_memseg(uint64_t vaddr)
883 {
884 	uintptr_t paddr;
885 	struct rte_memseg *seg;
886 
887 	seg = rte_mem_virt2memseg((void *)(uintptr_t)vaddr, NULL);
888 	if (seg != NULL) {
889 		paddr = seg->iova;
890 		if (paddr == RTE_BAD_IOVA) {
891 			return SPDK_VTOPHYS_ERROR;
892 		}
893 		paddr += (vaddr - (uintptr_t)seg->addr);
894 		return paddr;
895 	}
896 
897 	return SPDK_VTOPHYS_ERROR;
898 }
899 
900 /* Try to get the paddr from /proc/self/pagemap */
901 static uint64_t
902 vtophys_get_paddr_pagemap(uint64_t vaddr)
903 {
904 	uintptr_t paddr;
905 
906 	/* Silence static analyzers */
907 	assert(vaddr != 0);
908 	paddr = rte_mem_virt2iova((void *)vaddr);
909 	if (paddr == RTE_BAD_IOVA) {
910 		/*
911 		 * The vaddr may be valid but doesn't have a backing page
912 		 * assigned yet.  Touch the page to ensure a backing page
913 		 * gets assigned, then try to translate again.
914 		 */
915 		rte_atomic64_read((rte_atomic64_t *)vaddr);
916 		paddr = rte_mem_virt2iova((void *)vaddr);
917 	}
918 	if (paddr == RTE_BAD_IOVA) {
919 		/* Unable to get to the physical address. */
920 		return SPDK_VTOPHYS_ERROR;
921 	}
922 
923 	return paddr;
924 }
925 
926 /* Try to get the paddr from pci devices */
927 static uint64_t
928 vtophys_get_paddr_pci(uint64_t vaddr)
929 {
930 	struct spdk_vtophys_pci_device *vtophys_dev;
931 	uintptr_t paddr;
932 	struct rte_pci_device	*dev;
933 
934 	pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
935 	TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
936 		dev = vtophys_dev->pci_device;
937 		paddr = dpdk_pci_device_vtophys(dev, vaddr);
938 		if (paddr != SPDK_VTOPHYS_ERROR) {
939 			pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
940 			return paddr;
941 		}
942 	}
943 	pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
944 
945 	return  SPDK_VTOPHYS_ERROR;
946 }
947 
948 static int
949 vtophys_notify(void *cb_ctx, struct spdk_mem_map *map,
950 	       enum spdk_mem_map_notify_action action,
951 	       void *vaddr, size_t len)
952 {
953 	int rc = 0, pci_phys = 0;
954 	uint64_t paddr;
955 
956 	if ((uintptr_t)vaddr & ~MASK_256TB) {
957 		DEBUG_PRINT("invalid usermode virtual address %p\n", vaddr);
958 		return -EINVAL;
959 	}
960 
961 	if (((uintptr_t)vaddr & MASK_2MB) || (len & MASK_2MB)) {
962 		DEBUG_PRINT("invalid parameters, vaddr=%p len=%ju\n",
963 			    vaddr, len);
964 		return -EINVAL;
965 	}
966 
967 	/* Get the physical address from the DPDK memsegs */
968 	paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
969 
970 	switch (action) {
971 	case SPDK_MEM_MAP_NOTIFY_REGISTER:
972 		if (paddr == SPDK_VTOPHYS_ERROR) {
973 			/* This is not an address that DPDK is managing. */
974 #if VFIO_ENABLED
975 			enum rte_iova_mode iova_mode;
976 
977 			iova_mode = rte_eal_iova_mode();
978 
979 			if (spdk_iommu_is_enabled() && iova_mode == RTE_IOVA_VA) {
980 				/* We'll use the virtual address as the iova to match DPDK. */
981 				paddr = (uint64_t)vaddr;
982 				rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, len);
983 				if (rc) {
984 					return -EFAULT;
985 				}
986 				while (len > 0) {
987 					rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
988 					if (rc != 0) {
989 						return rc;
990 					}
991 					vaddr += VALUE_2MB;
992 					paddr += VALUE_2MB;
993 					len -= VALUE_2MB;
994 				}
995 			} else
996 #endif
997 			{
998 				/* Get the physical address from /proc/self/pagemap. */
999 				paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
1000 				if (paddr == SPDK_VTOPHYS_ERROR) {
1001 					/* Get the physical address from PCI devices */
1002 					paddr = vtophys_get_paddr_pci((uint64_t)vaddr);
1003 					if (paddr == SPDK_VTOPHYS_ERROR) {
1004 						DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1005 						return -EFAULT;
1006 					}
1007 					/* The beginning of this address range points to a PCI resource,
1008 					 * so the rest must point to a PCI resource as well.
1009 					 */
1010 					pci_phys = 1;
1011 				}
1012 
1013 				/* Get paddr for each 2MB chunk in this address range */
1014 				while (len > 0) {
1015 					/* Get the physical address from /proc/self/pagemap. */
1016 					if (pci_phys) {
1017 						paddr = vtophys_get_paddr_pci((uint64_t)vaddr);
1018 					} else {
1019 						paddr = vtophys_get_paddr_pagemap((uint64_t)vaddr);
1020 					}
1021 
1022 					if (paddr == SPDK_VTOPHYS_ERROR) {
1023 						DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1024 						return -EFAULT;
1025 					}
1026 
1027 					/* Since PCI paddr can break the 2MiB physical alignment skip this check for that. */
1028 					if (!pci_phys && (paddr & MASK_2MB)) {
1029 						DEBUG_PRINT("invalid paddr 0x%" PRIx64 " - must be 2MB aligned\n", paddr);
1030 						return -EINVAL;
1031 					}
1032 #if VFIO_ENABLED
1033 					/* If the IOMMU is on, but DPDK is using iova-mode=pa, we want to register this memory
1034 					 * with the IOMMU using the physical address to match. */
1035 					if (spdk_iommu_is_enabled()) {
1036 						rc = vtophys_iommu_map_dma((uint64_t)vaddr, paddr, VALUE_2MB);
1037 						if (rc) {
1038 							DEBUG_PRINT("Unable to assign vaddr %p to paddr 0x%" PRIx64 "\n", vaddr, paddr);
1039 							return -EFAULT;
1040 						}
1041 					}
1042 #endif
1043 
1044 					rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
1045 					if (rc != 0) {
1046 						return rc;
1047 					}
1048 
1049 					vaddr += VALUE_2MB;
1050 					len -= VALUE_2MB;
1051 				}
1052 			}
1053 		} else {
1054 			/* This is an address managed by DPDK. Just setup the translations. */
1055 			while (len > 0) {
1056 				paddr = vtophys_get_paddr_memseg((uint64_t)vaddr);
1057 				if (paddr == SPDK_VTOPHYS_ERROR) {
1058 					DEBUG_PRINT("could not get phys addr for %p\n", vaddr);
1059 					return -EFAULT;
1060 				}
1061 
1062 				rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, VALUE_2MB, paddr);
1063 				if (rc != 0) {
1064 					return rc;
1065 				}
1066 
1067 				vaddr += VALUE_2MB;
1068 				len -= VALUE_2MB;
1069 			}
1070 		}
1071 
1072 		break;
1073 	case SPDK_MEM_MAP_NOTIFY_UNREGISTER:
1074 #if VFIO_ENABLED
1075 		if (paddr == SPDK_VTOPHYS_ERROR) {
1076 			/*
1077 			 * This is not an address that DPDK is managing. If vfio is enabled,
1078 			 * we need to unmap the range from the IOMMU
1079 			 */
1080 			if (spdk_iommu_is_enabled()) {
1081 				uint64_t buffer_len = len;
1082 				uint8_t *va = vaddr;
1083 				enum rte_iova_mode iova_mode;
1084 
1085 				iova_mode = rte_eal_iova_mode();
1086 				/*
1087 				 * In virtual address mode, the region is contiguous and can be done in
1088 				 * one unmap.
1089 				 */
1090 				if (iova_mode == RTE_IOVA_VA) {
1091 					paddr = spdk_mem_map_translate(map, (uint64_t)va, &buffer_len);
1092 					if (buffer_len != len || paddr != (uintptr_t)va) {
1093 						DEBUG_PRINT("Unmapping %p with length %lu failed because "
1094 							    "translation had address 0x%" PRIx64 " and length %lu\n",
1095 							    va, len, paddr, buffer_len);
1096 						return -EINVAL;
1097 					}
1098 					rc = vtophys_iommu_unmap_dma(paddr, len);
1099 					if (rc) {
1100 						DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
1101 						return -EFAULT;
1102 					}
1103 				} else if (iova_mode == RTE_IOVA_PA) {
1104 					/* Get paddr for each 2MB chunk in this address range */
1105 					while (buffer_len > 0) {
1106 						paddr = spdk_mem_map_translate(map, (uint64_t)va, NULL);
1107 
1108 						if (paddr == SPDK_VTOPHYS_ERROR || buffer_len < VALUE_2MB) {
1109 							DEBUG_PRINT("could not get phys addr for %p\n", va);
1110 							return -EFAULT;
1111 						}
1112 
1113 						rc = vtophys_iommu_unmap_dma(paddr, VALUE_2MB);
1114 						if (rc) {
1115 							DEBUG_PRINT("Failed to iommu unmap paddr 0x%" PRIx64 "\n", paddr);
1116 							return -EFAULT;
1117 						}
1118 
1119 						va += VALUE_2MB;
1120 						buffer_len -= VALUE_2MB;
1121 					}
1122 				}
1123 			}
1124 		}
1125 #endif
1126 		while (len > 0) {
1127 			rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, VALUE_2MB);
1128 			if (rc != 0) {
1129 				return rc;
1130 			}
1131 
1132 			vaddr += VALUE_2MB;
1133 			len -= VALUE_2MB;
1134 		}
1135 
1136 		break;
1137 	default:
1138 		SPDK_UNREACHABLE();
1139 	}
1140 
1141 	return rc;
1142 }
1143 
1144 static int
1145 vtophys_check_contiguous_entries(uint64_t paddr1, uint64_t paddr2)
1146 {
1147 	/* This function is always called with paddrs for two subsequent
1148 	 * 2MB chunks in virtual address space, so those chunks will be only
1149 	 * physically contiguous if the physical addresses are 2MB apart
1150 	 * from each other as well.
1151 	 */
1152 	return (paddr2 - paddr1 == VALUE_2MB);
1153 }
1154 
1155 #if VFIO_ENABLED
1156 
1157 static bool
1158 vfio_enabled(void)
1159 {
1160 	return rte_vfio_is_enabled("vfio_pci");
1161 }
1162 
1163 /* Check if IOMMU is enabled on the system */
1164 static bool
1165 has_iommu_groups(void)
1166 {
1167 	int count = 0;
1168 	DIR *dir = opendir("/sys/kernel/iommu_groups");
1169 
1170 	if (dir == NULL) {
1171 		return false;
1172 	}
1173 
1174 	while (count < 3 && readdir(dir) != NULL) {
1175 		count++;
1176 	}
1177 
1178 	closedir(dir);
1179 	/* there will always be ./ and ../ entries */
1180 	return count > 2;
1181 }
1182 
1183 static bool
1184 vfio_noiommu_enabled(void)
1185 {
1186 	return rte_vfio_noiommu_is_enabled();
1187 }
1188 
1189 static void
1190 vtophys_iommu_init(void)
1191 {
1192 	char proc_fd_path[PATH_MAX + 1];
1193 	char link_path[PATH_MAX + 1];
1194 	const char vfio_path[] = "/dev/vfio/vfio";
1195 	DIR *dir;
1196 	struct dirent *d;
1197 
1198 	if (!vfio_enabled()) {
1199 		return;
1200 	}
1201 
1202 	if (vfio_noiommu_enabled()) {
1203 		g_vfio.noiommu_enabled = true;
1204 	} else if (!has_iommu_groups()) {
1205 		return;
1206 	}
1207 
1208 	dir = opendir("/proc/self/fd");
1209 	if (!dir) {
1210 		DEBUG_PRINT("Failed to open /proc/self/fd (%d)\n", errno);
1211 		return;
1212 	}
1213 
1214 	while ((d = readdir(dir)) != NULL) {
1215 		if (d->d_type != DT_LNK) {
1216 			continue;
1217 		}
1218 
1219 		snprintf(proc_fd_path, sizeof(proc_fd_path), "/proc/self/fd/%s", d->d_name);
1220 		if (readlink(proc_fd_path, link_path, sizeof(link_path)) != (sizeof(vfio_path) - 1)) {
1221 			continue;
1222 		}
1223 
1224 		if (memcmp(link_path, vfio_path, sizeof(vfio_path) - 1) == 0) {
1225 			sscanf(d->d_name, "%d", &g_vfio.fd);
1226 			break;
1227 		}
1228 	}
1229 
1230 	closedir(dir);
1231 
1232 	if (g_vfio.fd < 0) {
1233 		DEBUG_PRINT("Failed to discover DPDK VFIO container fd.\n");
1234 		return;
1235 	}
1236 
1237 	g_vfio.enabled = true;
1238 
1239 	return;
1240 }
1241 
1242 #endif
1243 
1244 void
1245 vtophys_pci_device_added(struct rte_pci_device *pci_device)
1246 {
1247 	struct spdk_vtophys_pci_device *vtophys_dev;
1248 
1249 	pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
1250 
1251 	vtophys_dev = calloc(1, sizeof(*vtophys_dev));
1252 	if (vtophys_dev) {
1253 		vtophys_dev->pci_device = pci_device;
1254 		TAILQ_INSERT_TAIL(&g_vtophys_pci_devices, vtophys_dev, tailq);
1255 	} else {
1256 		DEBUG_PRINT("Memory allocation error\n");
1257 	}
1258 	pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
1259 
1260 #if VFIO_ENABLED
1261 	struct spdk_vfio_dma_map *dma_map;
1262 	int ret;
1263 
1264 	if (!g_vfio.enabled) {
1265 		return;
1266 	}
1267 
1268 	pthread_mutex_lock(&g_vfio.mutex);
1269 	g_vfio.device_ref++;
1270 	if (g_vfio.device_ref > 1) {
1271 		pthread_mutex_unlock(&g_vfio.mutex);
1272 		return;
1273 	}
1274 
1275 	/* This is the first SPDK device using DPDK vfio. This means that the first
1276 	 * IOMMU group might have been just been added to the DPDK vfio container.
1277 	 * From this point it is certain that the memory can be mapped now.
1278 	 */
1279 	TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
1280 		ret = ioctl(g_vfio.fd, VFIO_IOMMU_MAP_DMA, &dma_map->map);
1281 		if (ret) {
1282 			DEBUG_PRINT("Cannot update DMA mapping, error %d\n", errno);
1283 			break;
1284 		}
1285 	}
1286 	pthread_mutex_unlock(&g_vfio.mutex);
1287 #endif
1288 }
1289 
1290 void
1291 vtophys_pci_device_removed(struct rte_pci_device *pci_device)
1292 {
1293 	struct spdk_vtophys_pci_device *vtophys_dev;
1294 
1295 	pthread_mutex_lock(&g_vtophys_pci_devices_mutex);
1296 	TAILQ_FOREACH(vtophys_dev, &g_vtophys_pci_devices, tailq) {
1297 		if (vtophys_dev->pci_device == pci_device) {
1298 			TAILQ_REMOVE(&g_vtophys_pci_devices, vtophys_dev, tailq);
1299 			free(vtophys_dev);
1300 			break;
1301 		}
1302 	}
1303 	pthread_mutex_unlock(&g_vtophys_pci_devices_mutex);
1304 
1305 #if VFIO_ENABLED
1306 	struct spdk_vfio_dma_map *dma_map;
1307 	int ret;
1308 
1309 	if (!g_vfio.enabled) {
1310 		return;
1311 	}
1312 
1313 	pthread_mutex_lock(&g_vfio.mutex);
1314 	assert(g_vfio.device_ref > 0);
1315 	g_vfio.device_ref--;
1316 	if (g_vfio.device_ref > 0) {
1317 		pthread_mutex_unlock(&g_vfio.mutex);
1318 		return;
1319 	}
1320 
1321 	/* This is the last SPDK device using DPDK vfio. If DPDK doesn't have
1322 	 * any additional devices using it's vfio container, all the mappings
1323 	 * will be automatically removed by the Linux vfio driver. We unmap
1324 	 * the memory manually to be able to easily re-map it later regardless
1325 	 * of other, external factors.
1326 	 */
1327 	TAILQ_FOREACH(dma_map, &g_vfio.maps, tailq) {
1328 		struct vfio_iommu_type1_dma_unmap unmap = {};
1329 		unmap.argsz = sizeof(unmap);
1330 		unmap.flags = 0;
1331 		unmap.iova = dma_map->map.iova;
1332 		unmap.size = dma_map->map.size;
1333 		ret = ioctl(g_vfio.fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
1334 		if (ret) {
1335 			DEBUG_PRINT("Cannot unmap DMA memory, error %d\n", errno);
1336 			break;
1337 		}
1338 	}
1339 	pthread_mutex_unlock(&g_vfio.mutex);
1340 #endif
1341 }
1342 
1343 int
1344 vtophys_init(void)
1345 {
1346 	const struct spdk_mem_map_ops vtophys_map_ops = {
1347 		.notify_cb = vtophys_notify,
1348 		.are_contiguous = vtophys_check_contiguous_entries,
1349 	};
1350 
1351 	const struct spdk_mem_map_ops phys_ref_map_ops = {
1352 		.notify_cb = NULL,
1353 		.are_contiguous = NULL,
1354 	};
1355 
1356 #if VFIO_ENABLED
1357 	vtophys_iommu_init();
1358 #endif
1359 
1360 	g_phys_ref_map = spdk_mem_map_alloc(0, &phys_ref_map_ops, NULL);
1361 	if (g_phys_ref_map == NULL) {
1362 		DEBUG_PRINT("phys_ref map allocation failed.\n");
1363 		return -ENOMEM;
1364 	}
1365 
1366 	g_vtophys_map = spdk_mem_map_alloc(SPDK_VTOPHYS_ERROR, &vtophys_map_ops, NULL);
1367 	if (g_vtophys_map == NULL) {
1368 		DEBUG_PRINT("vtophys map allocation failed\n");
1369 		spdk_mem_map_free(&g_phys_ref_map);
1370 		return -ENOMEM;
1371 	}
1372 	return 0;
1373 }
1374 
1375 uint64_t
1376 spdk_vtophys(const void *buf, uint64_t *size)
1377 {
1378 	uint64_t vaddr, paddr_2mb;
1379 
1380 	vaddr = (uint64_t)buf;
1381 	paddr_2mb = spdk_mem_map_translate(g_vtophys_map, vaddr, size);
1382 
1383 	/*
1384 	 * SPDK_VTOPHYS_ERROR has all bits set, so if the lookup returned SPDK_VTOPHYS_ERROR,
1385 	 * we will still bitwise-or it with the buf offset below, but the result will still be
1386 	 * SPDK_VTOPHYS_ERROR. However now that we do + rather than | (due to PCI vtophys being
1387 	 * unaligned) we must now check the return value before addition.
1388 	 */
1389 	SPDK_STATIC_ASSERT(SPDK_VTOPHYS_ERROR == UINT64_C(-1), "SPDK_VTOPHYS_ERROR should be all 1s");
1390 	if (paddr_2mb == SPDK_VTOPHYS_ERROR) {
1391 		return SPDK_VTOPHYS_ERROR;
1392 	} else {
1393 		return paddr_2mb + (vaddr & MASK_2MB);
1394 	}
1395 }
1396 
1397 int
1398 spdk_mem_get_fd_and_offset(void *vaddr, uint64_t *offset)
1399 {
1400 	struct rte_memseg *seg;
1401 	int ret, fd;
1402 
1403 	seg = rte_mem_virt2memseg(vaddr, NULL);
1404 	if (!seg) {
1405 		SPDK_ERRLOG("memory %p doesn't exist\n", vaddr);
1406 		return -ENOENT;
1407 	}
1408 
1409 	fd = rte_memseg_get_fd_thread_unsafe(seg);
1410 	if (fd < 0) {
1411 		return fd;
1412 	}
1413 
1414 	ret = rte_memseg_get_fd_offset_thread_unsafe(seg, offset);
1415 	if (ret < 0) {
1416 		return ret;
1417 	}
1418 
1419 	return fd;
1420 }
1421