• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright © 2006-2009, Intel Corporation.
4  *
5  * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
6  */
7 
8 #include <linux/iova.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/smp.h>
12 #include <linux/bitops.h>
13 #include <linux/cpu.h>
14 
15 /* The anchor node sits above the top of the usable address space */
16 #define IOVA_ANCHOR	~0UL
17 
18 static bool iova_rcache_insert(struct iova_domain *iovad,
19 			       unsigned long pfn,
20 			       unsigned long size);
21 static unsigned long iova_rcache_get(struct iova_domain *iovad,
22 				     unsigned long size,
23 				     unsigned long limit_pfn);
24 static void init_iova_rcaches(struct iova_domain *iovad);
25 static void free_iova_rcaches(struct iova_domain *iovad);
26 static void fq_destroy_all_entries(struct iova_domain *iovad);
27 static void fq_flush_timeout(struct timer_list *t);
28 
29 void
init_iova_domain(struct iova_domain * iovad,unsigned long granule,unsigned long start_pfn)30 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
31 	unsigned long start_pfn)
32 {
33 	/*
34 	 * IOVA granularity will normally be equal to the smallest
35 	 * supported IOMMU page size; both *must* be capable of
36 	 * representing individual CPU pages exactly.
37 	 */
38 	BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
39 
40 	spin_lock_init(&iovad->iova_rbtree_lock);
41 	iovad->rbroot = RB_ROOT;
42 	iovad->cached_node = &iovad->anchor.node;
43 	iovad->cached32_node = &iovad->anchor.node;
44 	iovad->granule = granule;
45 	iovad->start_pfn = start_pfn;
46 	iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
47 	iovad->max32_alloc_size = iovad->dma_32bit_pfn;
48 	iovad->flush_cb = NULL;
49 	iovad->fq = NULL;
50 	iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
51 	rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
52 	rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
53 	init_iova_rcaches(iovad);
54 }
55 EXPORT_SYMBOL_GPL(init_iova_domain);
56 
has_iova_flush_queue(struct iova_domain * iovad)57 bool has_iova_flush_queue(struct iova_domain *iovad)
58 {
59 	return !!iovad->fq;
60 }
61 
free_iova_flush_queue(struct iova_domain * iovad)62 static void free_iova_flush_queue(struct iova_domain *iovad)
63 {
64 	if (!has_iova_flush_queue(iovad))
65 		return;
66 
67 	del_timer_sync(&iovad->fq_timer);
68 
69 	fq_destroy_all_entries(iovad);
70 
71 	free_percpu(iovad->fq);
72 
73 	iovad->fq         = NULL;
74 	iovad->flush_cb   = NULL;
75 	iovad->entry_dtor = NULL;
76 }
77 
init_iova_flush_queue(struct iova_domain * iovad,iova_flush_cb flush_cb,iova_entry_dtor entry_dtor)78 int init_iova_flush_queue(struct iova_domain *iovad,
79 			  iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
80 {
81 	struct iova_fq __percpu *queue;
82 	int cpu;
83 
84 	atomic64_set(&iovad->fq_flush_start_cnt,  0);
85 	atomic64_set(&iovad->fq_flush_finish_cnt, 0);
86 
87 	queue = alloc_percpu(struct iova_fq);
88 	if (!queue)
89 		return -ENOMEM;
90 
91 	iovad->flush_cb   = flush_cb;
92 	iovad->entry_dtor = entry_dtor;
93 
94 	for_each_possible_cpu(cpu) {
95 		struct iova_fq *fq;
96 
97 		fq = per_cpu_ptr(queue, cpu);
98 		fq->head = 0;
99 		fq->tail = 0;
100 
101 		spin_lock_init(&fq->lock);
102 	}
103 
104 	smp_wmb();
105 
106 	iovad->fq = queue;
107 
108 	timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
109 	atomic_set(&iovad->fq_timer_on, 0);
110 
111 	return 0;
112 }
113 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
114 
115 static struct rb_node *
__get_cached_rbnode(struct iova_domain * iovad,unsigned long limit_pfn)116 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
117 {
118 	if (limit_pfn <= iovad->dma_32bit_pfn)
119 		return iovad->cached32_node;
120 
121 	return iovad->cached_node;
122 }
123 
124 static void
__cached_rbnode_insert_update(struct iova_domain * iovad,struct iova * new)125 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
126 {
127 	if (new->pfn_hi < iovad->dma_32bit_pfn)
128 		iovad->cached32_node = &new->node;
129 	else
130 		iovad->cached_node = &new->node;
131 }
132 
133 static void
__cached_rbnode_delete_update(struct iova_domain * iovad,struct iova * free)134 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
135 {
136 	struct iova *cached_iova;
137 
138 	cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
139 	if (free == cached_iova ||
140 	    (free->pfn_hi < iovad->dma_32bit_pfn &&
141 	     free->pfn_lo >= cached_iova->pfn_lo)) {
142 		iovad->cached32_node = rb_next(&free->node);
143 		iovad->max32_alloc_size = iovad->dma_32bit_pfn;
144 	}
145 
146 	cached_iova = rb_entry(iovad->cached_node, struct iova, node);
147 	if (free->pfn_lo >= cached_iova->pfn_lo)
148 		iovad->cached_node = rb_next(&free->node);
149 }
150 
151 /* Insert the iova into domain rbtree by holding writer lock */
152 static void
iova_insert_rbtree(struct rb_root * root,struct iova * iova,struct rb_node * start)153 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
154 		   struct rb_node *start)
155 {
156 	struct rb_node **new, *parent = NULL;
157 
158 	new = (start) ? &start : &(root->rb_node);
159 	/* Figure out where to put new node */
160 	while (*new) {
161 		struct iova *this = rb_entry(*new, struct iova, node);
162 
163 		parent = *new;
164 
165 		if (iova->pfn_lo < this->pfn_lo)
166 			new = &((*new)->rb_left);
167 		else if (iova->pfn_lo > this->pfn_lo)
168 			new = &((*new)->rb_right);
169 		else {
170 			WARN_ON(1); /* this should not happen */
171 			return;
172 		}
173 	}
174 	/* Add new node and rebalance tree. */
175 	rb_link_node(&iova->node, parent, new);
176 	rb_insert_color(&iova->node, root);
177 }
178 
__alloc_and_insert_iova_range(struct iova_domain * iovad,unsigned long size,unsigned long limit_pfn,struct iova * new,bool size_aligned)179 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
180 		unsigned long size, unsigned long limit_pfn,
181 			struct iova *new, bool size_aligned)
182 {
183 	struct rb_node *curr, *prev;
184 	struct iova *curr_iova;
185 	unsigned long flags;
186 	unsigned long new_pfn;
187 	unsigned long align_mask = ~0UL;
188 
189 	if (size_aligned)
190 		align_mask <<= fls_long(size - 1);
191 
192 	/* Walk the tree backwards */
193 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
194 	if (limit_pfn <= iovad->dma_32bit_pfn &&
195 			size >= iovad->max32_alloc_size)
196 		goto iova32_full;
197 
198 	curr = __get_cached_rbnode(iovad, limit_pfn);
199 	curr_iova = rb_entry(curr, struct iova, node);
200 	do {
201 		limit_pfn = min(limit_pfn, curr_iova->pfn_lo);
202 		new_pfn = (limit_pfn - size) & align_mask;
203 		prev = curr;
204 		curr = rb_prev(curr);
205 		curr_iova = rb_entry(curr, struct iova, node);
206 	} while (curr && new_pfn <= curr_iova->pfn_hi);
207 
208 	if (limit_pfn < size || new_pfn < iovad->start_pfn) {
209 		iovad->max32_alloc_size = size;
210 		goto iova32_full;
211 	}
212 
213 	/* pfn_lo will point to size aligned address if size_aligned is set */
214 	new->pfn_lo = new_pfn;
215 	new->pfn_hi = new->pfn_lo + size - 1;
216 
217 	/* If we have 'prev', it's a valid place to start the insertion. */
218 	iova_insert_rbtree(&iovad->rbroot, new, prev);
219 	__cached_rbnode_insert_update(iovad, new);
220 
221 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
222 	return 0;
223 
224 iova32_full:
225 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
226 	return -ENOMEM;
227 }
228 
229 static struct kmem_cache *iova_cache;
230 static unsigned int iova_cache_users;
231 static DEFINE_MUTEX(iova_cache_mutex);
232 
alloc_iova_mem(void)233 struct iova *alloc_iova_mem(void)
234 {
235 	return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
236 }
237 EXPORT_SYMBOL(alloc_iova_mem);
238 
free_iova_mem(struct iova * iova)239 void free_iova_mem(struct iova *iova)
240 {
241 	if (iova->pfn_lo != IOVA_ANCHOR)
242 		kmem_cache_free(iova_cache, iova);
243 }
244 EXPORT_SYMBOL(free_iova_mem);
245 
iova_cache_get(void)246 int iova_cache_get(void)
247 {
248 	mutex_lock(&iova_cache_mutex);
249 	if (!iova_cache_users) {
250 		iova_cache = kmem_cache_create(
251 			"iommu_iova", sizeof(struct iova), 0,
252 			SLAB_HWCACHE_ALIGN, NULL);
253 		if (!iova_cache) {
254 			mutex_unlock(&iova_cache_mutex);
255 			pr_err("Couldn't create iova cache\n");
256 			return -ENOMEM;
257 		}
258 	}
259 
260 	iova_cache_users++;
261 	mutex_unlock(&iova_cache_mutex);
262 
263 	return 0;
264 }
265 EXPORT_SYMBOL_GPL(iova_cache_get);
266 
iova_cache_put(void)267 void iova_cache_put(void)
268 {
269 	mutex_lock(&iova_cache_mutex);
270 	if (WARN_ON(!iova_cache_users)) {
271 		mutex_unlock(&iova_cache_mutex);
272 		return;
273 	}
274 	iova_cache_users--;
275 	if (!iova_cache_users)
276 		kmem_cache_destroy(iova_cache);
277 	mutex_unlock(&iova_cache_mutex);
278 }
279 EXPORT_SYMBOL_GPL(iova_cache_put);
280 
281 /**
282  * alloc_iova - allocates an iova
283  * @iovad: - iova domain in question
284  * @size: - size of page frames to allocate
285  * @limit_pfn: - max limit address
286  * @size_aligned: - set if size_aligned address range is required
287  * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
288  * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
289  * flag is set then the allocated address iova->pfn_lo will be naturally
290  * aligned on roundup_power_of_two(size).
291  */
292 struct iova *
alloc_iova(struct iova_domain * iovad,unsigned long size,unsigned long limit_pfn,bool size_aligned)293 alloc_iova(struct iova_domain *iovad, unsigned long size,
294 	unsigned long limit_pfn,
295 	bool size_aligned)
296 {
297 	struct iova *new_iova;
298 	int ret;
299 
300 	new_iova = alloc_iova_mem();
301 	if (!new_iova)
302 		return NULL;
303 
304 	ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
305 			new_iova, size_aligned);
306 
307 	if (ret) {
308 		free_iova_mem(new_iova);
309 		return NULL;
310 	}
311 
312 	return new_iova;
313 }
314 EXPORT_SYMBOL_GPL(alloc_iova);
315 
316 static struct iova *
private_find_iova(struct iova_domain * iovad,unsigned long pfn)317 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
318 {
319 	struct rb_node *node = iovad->rbroot.rb_node;
320 
321 	assert_spin_locked(&iovad->iova_rbtree_lock);
322 
323 	while (node) {
324 		struct iova *iova = rb_entry(node, struct iova, node);
325 
326 		if (pfn < iova->pfn_lo)
327 			node = node->rb_left;
328 		else if (pfn > iova->pfn_hi)
329 			node = node->rb_right;
330 		else
331 			return iova;	/* pfn falls within iova's range */
332 	}
333 
334 	return NULL;
335 }
336 
private_free_iova(struct iova_domain * iovad,struct iova * iova)337 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
338 {
339 	assert_spin_locked(&iovad->iova_rbtree_lock);
340 	__cached_rbnode_delete_update(iovad, iova);
341 	rb_erase(&iova->node, &iovad->rbroot);
342 	free_iova_mem(iova);
343 }
344 
345 /**
346  * find_iova - finds an iova for a given pfn
347  * @iovad: - iova domain in question.
348  * @pfn: - page frame number
349  * This function finds and returns an iova belonging to the
350  * given doamin which matches the given pfn.
351  */
find_iova(struct iova_domain * iovad,unsigned long pfn)352 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
353 {
354 	unsigned long flags;
355 	struct iova *iova;
356 
357 	/* Take the lock so that no other thread is manipulating the rbtree */
358 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
359 	iova = private_find_iova(iovad, pfn);
360 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
361 	return iova;
362 }
363 EXPORT_SYMBOL_GPL(find_iova);
364 
365 /**
366  * __free_iova - frees the given iova
367  * @iovad: iova domain in question.
368  * @iova: iova in question.
369  * Frees the given iova belonging to the giving domain
370  */
371 void
__free_iova(struct iova_domain * iovad,struct iova * iova)372 __free_iova(struct iova_domain *iovad, struct iova *iova)
373 {
374 	unsigned long flags;
375 
376 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
377 	private_free_iova(iovad, iova);
378 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
379 }
380 EXPORT_SYMBOL_GPL(__free_iova);
381 
382 /**
383  * free_iova - finds and frees the iova for a given pfn
384  * @iovad: - iova domain in question.
385  * @pfn: - pfn that is allocated previously
386  * This functions finds an iova for a given pfn and then
387  * frees the iova from that domain.
388  */
389 void
free_iova(struct iova_domain * iovad,unsigned long pfn)390 free_iova(struct iova_domain *iovad, unsigned long pfn)
391 {
392 	struct iova *iova = find_iova(iovad, pfn);
393 
394 	if (iova)
395 		__free_iova(iovad, iova);
396 
397 }
398 EXPORT_SYMBOL_GPL(free_iova);
399 
400 /**
401  * alloc_iova_fast - allocates an iova from rcache
402  * @iovad: - iova domain in question
403  * @size: - size of page frames to allocate
404  * @limit_pfn: - max limit address
405  * @flush_rcache: - set to flush rcache on regular allocation failure
406  * This function tries to satisfy an iova allocation from the rcache,
407  * and falls back to regular allocation on failure. If regular allocation
408  * fails too and the flush_rcache flag is set then the rcache will be flushed.
409 */
410 unsigned long
alloc_iova_fast(struct iova_domain * iovad,unsigned long size,unsigned long limit_pfn,bool flush_rcache)411 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
412 		unsigned long limit_pfn, bool flush_rcache)
413 {
414 	unsigned long iova_pfn;
415 	struct iova *new_iova;
416 
417 	iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
418 	if (iova_pfn)
419 		return iova_pfn;
420 
421 retry:
422 	new_iova = alloc_iova(iovad, size, limit_pfn, true);
423 	if (!new_iova) {
424 		unsigned int cpu;
425 
426 		if (!flush_rcache)
427 			return 0;
428 
429 		/* Try replenishing IOVAs by flushing rcache. */
430 		flush_rcache = false;
431 		for_each_online_cpu(cpu)
432 			free_cpu_cached_iovas(cpu, iovad);
433 		goto retry;
434 	}
435 
436 	return new_iova->pfn_lo;
437 }
438 EXPORT_SYMBOL_GPL(alloc_iova_fast);
439 
440 /**
441  * free_iova_fast - free iova pfn range into rcache
442  * @iovad: - iova domain in question.
443  * @pfn: - pfn that is allocated previously
444  * @size: - # of pages in range
445  * This functions frees an iova range by trying to put it into the rcache,
446  * falling back to regular iova deallocation via free_iova() if this fails.
447  */
448 void
free_iova_fast(struct iova_domain * iovad,unsigned long pfn,unsigned long size)449 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
450 {
451 	if (iova_rcache_insert(iovad, pfn, size))
452 		return;
453 
454 	free_iova(iovad, pfn);
455 }
456 EXPORT_SYMBOL_GPL(free_iova_fast);
457 
458 #define fq_ring_for_each(i, fq) \
459 	for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
460 
fq_full(struct iova_fq * fq)461 static inline bool fq_full(struct iova_fq *fq)
462 {
463 	assert_spin_locked(&fq->lock);
464 	return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
465 }
466 
fq_ring_add(struct iova_fq * fq)467 static inline unsigned fq_ring_add(struct iova_fq *fq)
468 {
469 	unsigned idx = fq->tail;
470 
471 	assert_spin_locked(&fq->lock);
472 
473 	fq->tail = (idx + 1) % IOVA_FQ_SIZE;
474 
475 	return idx;
476 }
477 
fq_ring_free(struct iova_domain * iovad,struct iova_fq * fq)478 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
479 {
480 	u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
481 	unsigned idx;
482 
483 	assert_spin_locked(&fq->lock);
484 
485 	fq_ring_for_each(idx, fq) {
486 
487 		if (fq->entries[idx].counter >= counter)
488 			break;
489 
490 		if (iovad->entry_dtor)
491 			iovad->entry_dtor(fq->entries[idx].data);
492 
493 		free_iova_fast(iovad,
494 			       fq->entries[idx].iova_pfn,
495 			       fq->entries[idx].pages);
496 
497 		fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
498 	}
499 }
500 
iova_domain_flush(struct iova_domain * iovad)501 static void iova_domain_flush(struct iova_domain *iovad)
502 {
503 	atomic64_inc(&iovad->fq_flush_start_cnt);
504 	iovad->flush_cb(iovad);
505 	atomic64_inc(&iovad->fq_flush_finish_cnt);
506 }
507 
fq_destroy_all_entries(struct iova_domain * iovad)508 static void fq_destroy_all_entries(struct iova_domain *iovad)
509 {
510 	int cpu;
511 
512 	/*
513 	 * This code runs when the iova_domain is being detroyed, so don't
514 	 * bother to free iovas, just call the entry_dtor on all remaining
515 	 * entries.
516 	 */
517 	if (!iovad->entry_dtor)
518 		return;
519 
520 	for_each_possible_cpu(cpu) {
521 		struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
522 		int idx;
523 
524 		fq_ring_for_each(idx, fq)
525 			iovad->entry_dtor(fq->entries[idx].data);
526 	}
527 }
528 
fq_flush_timeout(struct timer_list * t)529 static void fq_flush_timeout(struct timer_list *t)
530 {
531 	struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
532 	int cpu;
533 
534 	atomic_set(&iovad->fq_timer_on, 0);
535 	iova_domain_flush(iovad);
536 
537 	for_each_possible_cpu(cpu) {
538 		unsigned long flags;
539 		struct iova_fq *fq;
540 
541 		fq = per_cpu_ptr(iovad->fq, cpu);
542 		spin_lock_irqsave(&fq->lock, flags);
543 		fq_ring_free(iovad, fq);
544 		spin_unlock_irqrestore(&fq->lock, flags);
545 	}
546 }
547 
queue_iova(struct iova_domain * iovad,unsigned long pfn,unsigned long pages,unsigned long data)548 void queue_iova(struct iova_domain *iovad,
549 		unsigned long pfn, unsigned long pages,
550 		unsigned long data)
551 {
552 	struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
553 	unsigned long flags;
554 	unsigned idx;
555 
556 	spin_lock_irqsave(&fq->lock, flags);
557 
558 	/*
559 	 * First remove all entries from the flush queue that have already been
560 	 * flushed out on another CPU. This makes the fq_full() check below less
561 	 * likely to be true.
562 	 */
563 	fq_ring_free(iovad, fq);
564 
565 	if (fq_full(fq)) {
566 		iova_domain_flush(iovad);
567 		fq_ring_free(iovad, fq);
568 	}
569 
570 	idx = fq_ring_add(fq);
571 
572 	fq->entries[idx].iova_pfn = pfn;
573 	fq->entries[idx].pages    = pages;
574 	fq->entries[idx].data     = data;
575 	fq->entries[idx].counter  = atomic64_read(&iovad->fq_flush_start_cnt);
576 
577 	spin_unlock_irqrestore(&fq->lock, flags);
578 
579 	/* Avoid false sharing as much as possible. */
580 	if (!atomic_read(&iovad->fq_timer_on) &&
581 	    !atomic_xchg(&iovad->fq_timer_on, 1))
582 		mod_timer(&iovad->fq_timer,
583 			  jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
584 }
585 EXPORT_SYMBOL_GPL(queue_iova);
586 
587 /**
588  * put_iova_domain - destroys the iova doamin
589  * @iovad: - iova domain in question.
590  * All the iova's in that domain are destroyed.
591  */
put_iova_domain(struct iova_domain * iovad)592 void put_iova_domain(struct iova_domain *iovad)
593 {
594 	struct iova *iova, *tmp;
595 
596 	free_iova_flush_queue(iovad);
597 	free_iova_rcaches(iovad);
598 	rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
599 		free_iova_mem(iova);
600 }
601 EXPORT_SYMBOL_GPL(put_iova_domain);
602 
603 static int
__is_range_overlap(struct rb_node * node,unsigned long pfn_lo,unsigned long pfn_hi)604 __is_range_overlap(struct rb_node *node,
605 	unsigned long pfn_lo, unsigned long pfn_hi)
606 {
607 	struct iova *iova = rb_entry(node, struct iova, node);
608 
609 	if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
610 		return 1;
611 	return 0;
612 }
613 
614 static inline struct iova *
alloc_and_init_iova(unsigned long pfn_lo,unsigned long pfn_hi)615 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
616 {
617 	struct iova *iova;
618 
619 	iova = alloc_iova_mem();
620 	if (iova) {
621 		iova->pfn_lo = pfn_lo;
622 		iova->pfn_hi = pfn_hi;
623 	}
624 
625 	return iova;
626 }
627 
628 static struct iova *
__insert_new_range(struct iova_domain * iovad,unsigned long pfn_lo,unsigned long pfn_hi)629 __insert_new_range(struct iova_domain *iovad,
630 	unsigned long pfn_lo, unsigned long pfn_hi)
631 {
632 	struct iova *iova;
633 
634 	iova = alloc_and_init_iova(pfn_lo, pfn_hi);
635 	if (iova)
636 		iova_insert_rbtree(&iovad->rbroot, iova, NULL);
637 
638 	return iova;
639 }
640 
641 static void
__adjust_overlap_range(struct iova * iova,unsigned long * pfn_lo,unsigned long * pfn_hi)642 __adjust_overlap_range(struct iova *iova,
643 	unsigned long *pfn_lo, unsigned long *pfn_hi)
644 {
645 	if (*pfn_lo < iova->pfn_lo)
646 		iova->pfn_lo = *pfn_lo;
647 	if (*pfn_hi > iova->pfn_hi)
648 		*pfn_lo = iova->pfn_hi + 1;
649 }
650 
651 /**
652  * reserve_iova - reserves an iova in the given range
653  * @iovad: - iova domain pointer
654  * @pfn_lo: - lower page frame address
655  * @pfn_hi:- higher pfn adderss
656  * This function allocates reserves the address range from pfn_lo to pfn_hi so
657  * that this address is not dished out as part of alloc_iova.
658  */
659 struct iova *
reserve_iova(struct iova_domain * iovad,unsigned long pfn_lo,unsigned long pfn_hi)660 reserve_iova(struct iova_domain *iovad,
661 	unsigned long pfn_lo, unsigned long pfn_hi)
662 {
663 	struct rb_node *node;
664 	unsigned long flags;
665 	struct iova *iova;
666 	unsigned int overlap = 0;
667 
668 	/* Don't allow nonsensical pfns */
669 	if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
670 		return NULL;
671 
672 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
673 	for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
674 		if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
675 			iova = rb_entry(node, struct iova, node);
676 			__adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
677 			if ((pfn_lo >= iova->pfn_lo) &&
678 				(pfn_hi <= iova->pfn_hi))
679 				goto finish;
680 			overlap = 1;
681 
682 		} else if (overlap)
683 				break;
684 	}
685 
686 	/* We are here either because this is the first reserver node
687 	 * or need to insert remaining non overlap addr range
688 	 */
689 	iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
690 finish:
691 
692 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
693 	return iova;
694 }
695 EXPORT_SYMBOL_GPL(reserve_iova);
696 
697 /**
698  * copy_reserved_iova - copies the reserved between domains
699  * @from: - source doamin from where to copy
700  * @to: - destination domin where to copy
701  * This function copies reserved iova's from one doamin to
702  * other.
703  */
704 void
copy_reserved_iova(struct iova_domain * from,struct iova_domain * to)705 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
706 {
707 	unsigned long flags;
708 	struct rb_node *node;
709 
710 	spin_lock_irqsave(&from->iova_rbtree_lock, flags);
711 	for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
712 		struct iova *iova = rb_entry(node, struct iova, node);
713 		struct iova *new_iova;
714 
715 		if (iova->pfn_lo == IOVA_ANCHOR)
716 			continue;
717 
718 		new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
719 		if (!new_iova)
720 			pr_err("Reserve iova range %lx@%lx failed\n",
721 			       iova->pfn_lo, iova->pfn_lo);
722 	}
723 	spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
724 }
725 EXPORT_SYMBOL_GPL(copy_reserved_iova);
726 
727 struct iova *
split_and_remove_iova(struct iova_domain * iovad,struct iova * iova,unsigned long pfn_lo,unsigned long pfn_hi)728 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
729 		      unsigned long pfn_lo, unsigned long pfn_hi)
730 {
731 	unsigned long flags;
732 	struct iova *prev = NULL, *next = NULL;
733 
734 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
735 	if (iova->pfn_lo < pfn_lo) {
736 		prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
737 		if (prev == NULL)
738 			goto error;
739 	}
740 	if (iova->pfn_hi > pfn_hi) {
741 		next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
742 		if (next == NULL)
743 			goto error;
744 	}
745 
746 	__cached_rbnode_delete_update(iovad, iova);
747 	rb_erase(&iova->node, &iovad->rbroot);
748 
749 	if (prev) {
750 		iova_insert_rbtree(&iovad->rbroot, prev, NULL);
751 		iova->pfn_lo = pfn_lo;
752 	}
753 	if (next) {
754 		iova_insert_rbtree(&iovad->rbroot, next, NULL);
755 		iova->pfn_hi = pfn_hi;
756 	}
757 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
758 
759 	return iova;
760 
761 error:
762 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
763 	if (prev)
764 		free_iova_mem(prev);
765 	return NULL;
766 }
767 
768 /*
769  * Magazine caches for IOVA ranges.  For an introduction to magazines,
770  * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
771  * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
772  * For simplicity, we use a static magazine size and don't implement the
773  * dynamic size tuning described in the paper.
774  */
775 
776 #define IOVA_MAG_SIZE 128
777 
778 struct iova_magazine {
779 	unsigned long size;
780 	unsigned long pfns[IOVA_MAG_SIZE];
781 };
782 
783 struct iova_cpu_rcache {
784 	spinlock_t lock;
785 	struct iova_magazine *loaded;
786 	struct iova_magazine *prev;
787 };
788 
iova_magazine_alloc(gfp_t flags)789 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
790 {
791 	return kzalloc(sizeof(struct iova_magazine), flags);
792 }
793 
iova_magazine_free(struct iova_magazine * mag)794 static void iova_magazine_free(struct iova_magazine *mag)
795 {
796 	kfree(mag);
797 }
798 
799 static void
iova_magazine_free_pfns(struct iova_magazine * mag,struct iova_domain * iovad)800 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
801 {
802 	unsigned long flags;
803 	int i;
804 
805 	if (!mag)
806 		return;
807 
808 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
809 
810 	for (i = 0 ; i < mag->size; ++i) {
811 		struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
812 
813 		if (WARN_ON(!iova))
814 			continue;
815 
816 		private_free_iova(iovad, iova);
817 	}
818 
819 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
820 
821 	mag->size = 0;
822 }
823 
iova_magazine_full(struct iova_magazine * mag)824 static bool iova_magazine_full(struct iova_magazine *mag)
825 {
826 	return (mag && mag->size == IOVA_MAG_SIZE);
827 }
828 
iova_magazine_empty(struct iova_magazine * mag)829 static bool iova_magazine_empty(struct iova_magazine *mag)
830 {
831 	return (!mag || mag->size == 0);
832 }
833 
iova_magazine_pop(struct iova_magazine * mag,unsigned long limit_pfn)834 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
835 				       unsigned long limit_pfn)
836 {
837 	int i;
838 	unsigned long pfn;
839 
840 	BUG_ON(iova_magazine_empty(mag));
841 
842 	/* Only fall back to the rbtree if we have no suitable pfns at all */
843 	for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
844 		if (i == 0)
845 			return 0;
846 
847 	/* Swap it to pop it */
848 	pfn = mag->pfns[i];
849 	mag->pfns[i] = mag->pfns[--mag->size];
850 
851 	return pfn;
852 }
853 
iova_magazine_push(struct iova_magazine * mag,unsigned long pfn)854 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
855 {
856 	BUG_ON(iova_magazine_full(mag));
857 
858 	mag->pfns[mag->size++] = pfn;
859 }
860 
init_iova_rcaches(struct iova_domain * iovad)861 static void init_iova_rcaches(struct iova_domain *iovad)
862 {
863 	struct iova_cpu_rcache *cpu_rcache;
864 	struct iova_rcache *rcache;
865 	unsigned int cpu;
866 	int i;
867 
868 	for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
869 		rcache = &iovad->rcaches[i];
870 		spin_lock_init(&rcache->lock);
871 		rcache->depot_size = 0;
872 		rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
873 		if (WARN_ON(!rcache->cpu_rcaches))
874 			continue;
875 		for_each_possible_cpu(cpu) {
876 			cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
877 			spin_lock_init(&cpu_rcache->lock);
878 			cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
879 			cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
880 		}
881 	}
882 }
883 
884 /*
885  * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
886  * return true on success.  Can fail if rcache is full and we can't free
887  * space, and free_iova() (our only caller) will then return the IOVA
888  * range to the rbtree instead.
889  */
__iova_rcache_insert(struct iova_domain * iovad,struct iova_rcache * rcache,unsigned long iova_pfn)890 static bool __iova_rcache_insert(struct iova_domain *iovad,
891 				 struct iova_rcache *rcache,
892 				 unsigned long iova_pfn)
893 {
894 	struct iova_magazine *mag_to_free = NULL;
895 	struct iova_cpu_rcache *cpu_rcache;
896 	bool can_insert = false;
897 	unsigned long flags;
898 
899 	cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
900 	spin_lock_irqsave(&cpu_rcache->lock, flags);
901 
902 	if (!iova_magazine_full(cpu_rcache->loaded)) {
903 		can_insert = true;
904 	} else if (!iova_magazine_full(cpu_rcache->prev)) {
905 		swap(cpu_rcache->prev, cpu_rcache->loaded);
906 		can_insert = true;
907 	} else {
908 		struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
909 
910 		if (new_mag) {
911 			spin_lock(&rcache->lock);
912 			if (rcache->depot_size < MAX_GLOBAL_MAGS) {
913 				rcache->depot[rcache->depot_size++] =
914 						cpu_rcache->loaded;
915 			} else {
916 				mag_to_free = cpu_rcache->loaded;
917 			}
918 			spin_unlock(&rcache->lock);
919 
920 			cpu_rcache->loaded = new_mag;
921 			can_insert = true;
922 		}
923 	}
924 
925 	if (can_insert)
926 		iova_magazine_push(cpu_rcache->loaded, iova_pfn);
927 
928 	spin_unlock_irqrestore(&cpu_rcache->lock, flags);
929 
930 	if (mag_to_free) {
931 		iova_magazine_free_pfns(mag_to_free, iovad);
932 		iova_magazine_free(mag_to_free);
933 	}
934 
935 	return can_insert;
936 }
937 
iova_rcache_insert(struct iova_domain * iovad,unsigned long pfn,unsigned long size)938 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
939 			       unsigned long size)
940 {
941 	unsigned int log_size = order_base_2(size);
942 
943 	if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
944 		return false;
945 
946 	return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
947 }
948 
949 /*
950  * Caller wants to allocate a new IOVA range from 'rcache'.  If we can
951  * satisfy the request, return a matching non-NULL range and remove
952  * it from the 'rcache'.
953  */
__iova_rcache_get(struct iova_rcache * rcache,unsigned long limit_pfn)954 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
955 				       unsigned long limit_pfn)
956 {
957 	struct iova_cpu_rcache *cpu_rcache;
958 	unsigned long iova_pfn = 0;
959 	bool has_pfn = false;
960 	unsigned long flags;
961 
962 	cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
963 	spin_lock_irqsave(&cpu_rcache->lock, flags);
964 
965 	if (!iova_magazine_empty(cpu_rcache->loaded)) {
966 		has_pfn = true;
967 	} else if (!iova_magazine_empty(cpu_rcache->prev)) {
968 		swap(cpu_rcache->prev, cpu_rcache->loaded);
969 		has_pfn = true;
970 	} else {
971 		spin_lock(&rcache->lock);
972 		if (rcache->depot_size > 0) {
973 			iova_magazine_free(cpu_rcache->loaded);
974 			cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
975 			has_pfn = true;
976 		}
977 		spin_unlock(&rcache->lock);
978 	}
979 
980 	if (has_pfn)
981 		iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
982 
983 	spin_unlock_irqrestore(&cpu_rcache->lock, flags);
984 
985 	return iova_pfn;
986 }
987 
988 /*
989  * Try to satisfy IOVA allocation range from rcache.  Fail if requested
990  * size is too big or the DMA limit we are given isn't satisfied by the
991  * top element in the magazine.
992  */
iova_rcache_get(struct iova_domain * iovad,unsigned long size,unsigned long limit_pfn)993 static unsigned long iova_rcache_get(struct iova_domain *iovad,
994 				     unsigned long size,
995 				     unsigned long limit_pfn)
996 {
997 	unsigned int log_size = order_base_2(size);
998 
999 	if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
1000 		return 0;
1001 
1002 	return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
1003 }
1004 
1005 /*
1006  * free rcache data structures.
1007  */
free_iova_rcaches(struct iova_domain * iovad)1008 static void free_iova_rcaches(struct iova_domain *iovad)
1009 {
1010 	struct iova_rcache *rcache;
1011 	struct iova_cpu_rcache *cpu_rcache;
1012 	unsigned int cpu;
1013 	int i, j;
1014 
1015 	for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1016 		rcache = &iovad->rcaches[i];
1017 		for_each_possible_cpu(cpu) {
1018 			cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1019 			iova_magazine_free(cpu_rcache->loaded);
1020 			iova_magazine_free(cpu_rcache->prev);
1021 		}
1022 		free_percpu(rcache->cpu_rcaches);
1023 		for (j = 0; j < rcache->depot_size; ++j)
1024 			iova_magazine_free(rcache->depot[j]);
1025 	}
1026 }
1027 
1028 /*
1029  * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1030  */
free_cpu_cached_iovas(unsigned int cpu,struct iova_domain * iovad)1031 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1032 {
1033 	struct iova_cpu_rcache *cpu_rcache;
1034 	struct iova_rcache *rcache;
1035 	unsigned long flags;
1036 	int i;
1037 
1038 	for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1039 		rcache = &iovad->rcaches[i];
1040 		cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1041 		spin_lock_irqsave(&cpu_rcache->lock, flags);
1042 		iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1043 		iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1044 		spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1045 	}
1046 }
1047 
1048 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1049 MODULE_LICENSE("GPL");
1050