xref: /linux/drivers/iommu/iova.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  * Copyright © 2006-2009, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
18  */
19 
20 #include <linux/iova.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <linux/bitops.h>
25 
26 static bool iova_rcache_insert(struct iova_domain *iovad,
27 			       unsigned long pfn,
28 			       unsigned long size);
29 static unsigned long iova_rcache_get(struct iova_domain *iovad,
30 				     unsigned long size,
31 				     unsigned long limit_pfn);
32 static void init_iova_rcaches(struct iova_domain *iovad);
33 static void free_iova_rcaches(struct iova_domain *iovad);
34 
35 void
36 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
37 	unsigned long start_pfn, unsigned long pfn_32bit)
38 {
39 	/*
40 	 * IOVA granularity will normally be equal to the smallest
41 	 * supported IOMMU page size; both *must* be capable of
42 	 * representing individual CPU pages exactly.
43 	 */
44 	BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
45 
46 	spin_lock_init(&iovad->iova_rbtree_lock);
47 	iovad->rbroot = RB_ROOT;
48 	iovad->cached32_node = NULL;
49 	iovad->granule = granule;
50 	iovad->start_pfn = start_pfn;
51 	iovad->dma_32bit_pfn = pfn_32bit;
52 	init_iova_rcaches(iovad);
53 }
54 EXPORT_SYMBOL_GPL(init_iova_domain);
55 
56 static struct rb_node *
57 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
58 {
59 	if ((*limit_pfn > iovad->dma_32bit_pfn) ||
60 		(iovad->cached32_node == NULL))
61 		return rb_last(&iovad->rbroot);
62 	else {
63 		struct rb_node *prev_node = rb_prev(iovad->cached32_node);
64 		struct iova *curr_iova =
65 			rb_entry(iovad->cached32_node, struct iova, node);
66 		*limit_pfn = curr_iova->pfn_lo - 1;
67 		return prev_node;
68 	}
69 }
70 
71 static void
72 __cached_rbnode_insert_update(struct iova_domain *iovad,
73 	unsigned long limit_pfn, struct iova *new)
74 {
75 	if (limit_pfn != iovad->dma_32bit_pfn)
76 		return;
77 	iovad->cached32_node = &new->node;
78 }
79 
80 static void
81 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
82 {
83 	struct iova *cached_iova;
84 	struct rb_node *curr;
85 
86 	if (!iovad->cached32_node)
87 		return;
88 	curr = iovad->cached32_node;
89 	cached_iova = rb_entry(curr, struct iova, node);
90 
91 	if (free->pfn_lo >= cached_iova->pfn_lo) {
92 		struct rb_node *node = rb_next(&free->node);
93 		struct iova *iova = rb_entry(node, struct iova, node);
94 
95 		/* only cache if it's below 32bit pfn */
96 		if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
97 			iovad->cached32_node = node;
98 		else
99 			iovad->cached32_node = NULL;
100 	}
101 }
102 
103 /* Insert the iova into domain rbtree by holding writer lock */
104 static void
105 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
106 		   struct rb_node *start)
107 {
108 	struct rb_node **new, *parent = NULL;
109 
110 	new = (start) ? &start : &(root->rb_node);
111 	/* Figure out where to put new node */
112 	while (*new) {
113 		struct iova *this = rb_entry(*new, struct iova, node);
114 
115 		parent = *new;
116 
117 		if (iova->pfn_lo < this->pfn_lo)
118 			new = &((*new)->rb_left);
119 		else if (iova->pfn_lo > this->pfn_lo)
120 			new = &((*new)->rb_right);
121 		else {
122 			WARN_ON(1); /* this should not happen */
123 			return;
124 		}
125 	}
126 	/* Add new node and rebalance tree. */
127 	rb_link_node(&iova->node, parent, new);
128 	rb_insert_color(&iova->node, root);
129 }
130 
131 /*
132  * Computes the padding size required, to make the start address
133  * naturally aligned on the power-of-two order of its size
134  */
135 static unsigned int
136 iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
137 {
138 	return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1);
139 }
140 
141 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
142 		unsigned long size, unsigned long limit_pfn,
143 			struct iova *new, bool size_aligned)
144 {
145 	struct rb_node *prev, *curr = NULL;
146 	unsigned long flags;
147 	unsigned long saved_pfn;
148 	unsigned int pad_size = 0;
149 
150 	/* Walk the tree backwards */
151 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
152 	saved_pfn = limit_pfn;
153 	curr = __get_cached_rbnode(iovad, &limit_pfn);
154 	prev = curr;
155 	while (curr) {
156 		struct iova *curr_iova = rb_entry(curr, struct iova, node);
157 
158 		if (limit_pfn < curr_iova->pfn_lo)
159 			goto move_left;
160 		else if (limit_pfn < curr_iova->pfn_hi)
161 			goto adjust_limit_pfn;
162 		else {
163 			if (size_aligned)
164 				pad_size = iova_get_pad_size(size, limit_pfn);
165 			if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn)
166 				break;	/* found a free slot */
167 		}
168 adjust_limit_pfn:
169 		limit_pfn = curr_iova->pfn_lo ? (curr_iova->pfn_lo - 1) : 0;
170 move_left:
171 		prev = curr;
172 		curr = rb_prev(curr);
173 	}
174 
175 	if (!curr) {
176 		if (size_aligned)
177 			pad_size = iova_get_pad_size(size, limit_pfn);
178 		if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
179 			spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
180 			return -ENOMEM;
181 		}
182 	}
183 
184 	/* pfn_lo will point to size aligned address if size_aligned is set */
185 	new->pfn_lo = limit_pfn - (size + pad_size) + 1;
186 	new->pfn_hi = new->pfn_lo + size - 1;
187 
188 	/* If we have 'prev', it's a valid place to start the insertion. */
189 	iova_insert_rbtree(&iovad->rbroot, new, prev);
190 	__cached_rbnode_insert_update(iovad, saved_pfn, new);
191 
192 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
193 
194 
195 	return 0;
196 }
197 
198 static struct kmem_cache *iova_cache;
199 static unsigned int iova_cache_users;
200 static DEFINE_MUTEX(iova_cache_mutex);
201 
202 struct iova *alloc_iova_mem(void)
203 {
204 	return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
205 }
206 EXPORT_SYMBOL(alloc_iova_mem);
207 
208 void free_iova_mem(struct iova *iova)
209 {
210 	kmem_cache_free(iova_cache, iova);
211 }
212 EXPORT_SYMBOL(free_iova_mem);
213 
214 int iova_cache_get(void)
215 {
216 	mutex_lock(&iova_cache_mutex);
217 	if (!iova_cache_users) {
218 		iova_cache = kmem_cache_create(
219 			"iommu_iova", sizeof(struct iova), 0,
220 			SLAB_HWCACHE_ALIGN, NULL);
221 		if (!iova_cache) {
222 			mutex_unlock(&iova_cache_mutex);
223 			printk(KERN_ERR "Couldn't create iova cache\n");
224 			return -ENOMEM;
225 		}
226 	}
227 
228 	iova_cache_users++;
229 	mutex_unlock(&iova_cache_mutex);
230 
231 	return 0;
232 }
233 EXPORT_SYMBOL_GPL(iova_cache_get);
234 
235 void iova_cache_put(void)
236 {
237 	mutex_lock(&iova_cache_mutex);
238 	if (WARN_ON(!iova_cache_users)) {
239 		mutex_unlock(&iova_cache_mutex);
240 		return;
241 	}
242 	iova_cache_users--;
243 	if (!iova_cache_users)
244 		kmem_cache_destroy(iova_cache);
245 	mutex_unlock(&iova_cache_mutex);
246 }
247 EXPORT_SYMBOL_GPL(iova_cache_put);
248 
249 /**
250  * alloc_iova - allocates an iova
251  * @iovad: - iova domain in question
252  * @size: - size of page frames to allocate
253  * @limit_pfn: - max limit address
254  * @size_aligned: - set if size_aligned address range is required
255  * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
256  * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
257  * flag is set then the allocated address iova->pfn_lo will be naturally
258  * aligned on roundup_power_of_two(size).
259  */
260 struct iova *
261 alloc_iova(struct iova_domain *iovad, unsigned long size,
262 	unsigned long limit_pfn,
263 	bool size_aligned)
264 {
265 	struct iova *new_iova;
266 	int ret;
267 
268 	new_iova = alloc_iova_mem();
269 	if (!new_iova)
270 		return NULL;
271 
272 	ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
273 			new_iova, size_aligned);
274 
275 	if (ret) {
276 		free_iova_mem(new_iova);
277 		return NULL;
278 	}
279 
280 	return new_iova;
281 }
282 EXPORT_SYMBOL_GPL(alloc_iova);
283 
284 static struct iova *
285 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
286 {
287 	struct rb_node *node = iovad->rbroot.rb_node;
288 
289 	assert_spin_locked(&iovad->iova_rbtree_lock);
290 
291 	while (node) {
292 		struct iova *iova = rb_entry(node, struct iova, node);
293 
294 		/* If pfn falls within iova's range, return iova */
295 		if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
296 			return iova;
297 		}
298 
299 		if (pfn < iova->pfn_lo)
300 			node = node->rb_left;
301 		else if (pfn > iova->pfn_lo)
302 			node = node->rb_right;
303 	}
304 
305 	return NULL;
306 }
307 
308 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
309 {
310 	assert_spin_locked(&iovad->iova_rbtree_lock);
311 	__cached_rbnode_delete_update(iovad, iova);
312 	rb_erase(&iova->node, &iovad->rbroot);
313 	free_iova_mem(iova);
314 }
315 
316 /**
317  * find_iova - finds an iova for a given pfn
318  * @iovad: - iova domain in question.
319  * @pfn: - page frame number
320  * This function finds and returns an iova belonging to the
321  * given doamin which matches the given pfn.
322  */
323 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
324 {
325 	unsigned long flags;
326 	struct iova *iova;
327 
328 	/* Take the lock so that no other thread is manipulating the rbtree */
329 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
330 	iova = private_find_iova(iovad, pfn);
331 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
332 	return iova;
333 }
334 EXPORT_SYMBOL_GPL(find_iova);
335 
336 /**
337  * __free_iova - frees the given iova
338  * @iovad: iova domain in question.
339  * @iova: iova in question.
340  * Frees the given iova belonging to the giving domain
341  */
342 void
343 __free_iova(struct iova_domain *iovad, struct iova *iova)
344 {
345 	unsigned long flags;
346 
347 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
348 	private_free_iova(iovad, iova);
349 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
350 }
351 EXPORT_SYMBOL_GPL(__free_iova);
352 
353 /**
354  * free_iova - finds and frees the iova for a given pfn
355  * @iovad: - iova domain in question.
356  * @pfn: - pfn that is allocated previously
357  * This functions finds an iova for a given pfn and then
358  * frees the iova from that domain.
359  */
360 void
361 free_iova(struct iova_domain *iovad, unsigned long pfn)
362 {
363 	struct iova *iova = find_iova(iovad, pfn);
364 
365 	if (iova)
366 		__free_iova(iovad, iova);
367 
368 }
369 EXPORT_SYMBOL_GPL(free_iova);
370 
371 /**
372  * alloc_iova_fast - allocates an iova from rcache
373  * @iovad: - iova domain in question
374  * @size: - size of page frames to allocate
375  * @limit_pfn: - max limit address
376  * This function tries to satisfy an iova allocation from the rcache,
377  * and falls back to regular allocation on failure.
378 */
379 unsigned long
380 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
381 		unsigned long limit_pfn)
382 {
383 	bool flushed_rcache = false;
384 	unsigned long iova_pfn;
385 	struct iova *new_iova;
386 
387 	iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
388 	if (iova_pfn)
389 		return iova_pfn;
390 
391 retry:
392 	new_iova = alloc_iova(iovad, size, limit_pfn, true);
393 	if (!new_iova) {
394 		unsigned int cpu;
395 
396 		if (flushed_rcache)
397 			return 0;
398 
399 		/* Try replenishing IOVAs by flushing rcache. */
400 		flushed_rcache = true;
401 		preempt_disable();
402 		for_each_online_cpu(cpu)
403 			free_cpu_cached_iovas(cpu, iovad);
404 		preempt_enable();
405 		goto retry;
406 	}
407 
408 	return new_iova->pfn_lo;
409 }
410 EXPORT_SYMBOL_GPL(alloc_iova_fast);
411 
412 /**
413  * free_iova_fast - free iova pfn range into rcache
414  * @iovad: - iova domain in question.
415  * @pfn: - pfn that is allocated previously
416  * @size: - # of pages in range
417  * This functions frees an iova range by trying to put it into the rcache,
418  * falling back to regular iova deallocation via free_iova() if this fails.
419  */
420 void
421 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
422 {
423 	if (iova_rcache_insert(iovad, pfn, size))
424 		return;
425 
426 	free_iova(iovad, pfn);
427 }
428 EXPORT_SYMBOL_GPL(free_iova_fast);
429 
430 /**
431  * put_iova_domain - destroys the iova doamin
432  * @iovad: - iova domain in question.
433  * All the iova's in that domain are destroyed.
434  */
435 void put_iova_domain(struct iova_domain *iovad)
436 {
437 	struct rb_node *node;
438 	unsigned long flags;
439 
440 	free_iova_rcaches(iovad);
441 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
442 	node = rb_first(&iovad->rbroot);
443 	while (node) {
444 		struct iova *iova = rb_entry(node, struct iova, node);
445 
446 		rb_erase(node, &iovad->rbroot);
447 		free_iova_mem(iova);
448 		node = rb_first(&iovad->rbroot);
449 	}
450 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
451 }
452 EXPORT_SYMBOL_GPL(put_iova_domain);
453 
454 static int
455 __is_range_overlap(struct rb_node *node,
456 	unsigned long pfn_lo, unsigned long pfn_hi)
457 {
458 	struct iova *iova = rb_entry(node, struct iova, node);
459 
460 	if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
461 		return 1;
462 	return 0;
463 }
464 
465 static inline struct iova *
466 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
467 {
468 	struct iova *iova;
469 
470 	iova = alloc_iova_mem();
471 	if (iova) {
472 		iova->pfn_lo = pfn_lo;
473 		iova->pfn_hi = pfn_hi;
474 	}
475 
476 	return iova;
477 }
478 
479 static struct iova *
480 __insert_new_range(struct iova_domain *iovad,
481 	unsigned long pfn_lo, unsigned long pfn_hi)
482 {
483 	struct iova *iova;
484 
485 	iova = alloc_and_init_iova(pfn_lo, pfn_hi);
486 	if (iova)
487 		iova_insert_rbtree(&iovad->rbroot, iova, NULL);
488 
489 	return iova;
490 }
491 
492 static void
493 __adjust_overlap_range(struct iova *iova,
494 	unsigned long *pfn_lo, unsigned long *pfn_hi)
495 {
496 	if (*pfn_lo < iova->pfn_lo)
497 		iova->pfn_lo = *pfn_lo;
498 	if (*pfn_hi > iova->pfn_hi)
499 		*pfn_lo = iova->pfn_hi + 1;
500 }
501 
502 /**
503  * reserve_iova - reserves an iova in the given range
504  * @iovad: - iova domain pointer
505  * @pfn_lo: - lower page frame address
506  * @pfn_hi:- higher pfn adderss
507  * This function allocates reserves the address range from pfn_lo to pfn_hi so
508  * that this address is not dished out as part of alloc_iova.
509  */
510 struct iova *
511 reserve_iova(struct iova_domain *iovad,
512 	unsigned long pfn_lo, unsigned long pfn_hi)
513 {
514 	struct rb_node *node;
515 	unsigned long flags;
516 	struct iova *iova;
517 	unsigned int overlap = 0;
518 
519 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
520 	for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
521 		if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
522 			iova = rb_entry(node, struct iova, node);
523 			__adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
524 			if ((pfn_lo >= iova->pfn_lo) &&
525 				(pfn_hi <= iova->pfn_hi))
526 				goto finish;
527 			overlap = 1;
528 
529 		} else if (overlap)
530 				break;
531 	}
532 
533 	/* We are here either because this is the first reserver node
534 	 * or need to insert remaining non overlap addr range
535 	 */
536 	iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
537 finish:
538 
539 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
540 	return iova;
541 }
542 EXPORT_SYMBOL_GPL(reserve_iova);
543 
544 /**
545  * copy_reserved_iova - copies the reserved between domains
546  * @from: - source doamin from where to copy
547  * @to: - destination domin where to copy
548  * This function copies reserved iova's from one doamin to
549  * other.
550  */
551 void
552 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
553 {
554 	unsigned long flags;
555 	struct rb_node *node;
556 
557 	spin_lock_irqsave(&from->iova_rbtree_lock, flags);
558 	for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
559 		struct iova *iova = rb_entry(node, struct iova, node);
560 		struct iova *new_iova;
561 
562 		new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
563 		if (!new_iova)
564 			printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
565 				iova->pfn_lo, iova->pfn_lo);
566 	}
567 	spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
568 }
569 EXPORT_SYMBOL_GPL(copy_reserved_iova);
570 
571 struct iova *
572 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
573 		      unsigned long pfn_lo, unsigned long pfn_hi)
574 {
575 	unsigned long flags;
576 	struct iova *prev = NULL, *next = NULL;
577 
578 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
579 	if (iova->pfn_lo < pfn_lo) {
580 		prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
581 		if (prev == NULL)
582 			goto error;
583 	}
584 	if (iova->pfn_hi > pfn_hi) {
585 		next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
586 		if (next == NULL)
587 			goto error;
588 	}
589 
590 	__cached_rbnode_delete_update(iovad, iova);
591 	rb_erase(&iova->node, &iovad->rbroot);
592 
593 	if (prev) {
594 		iova_insert_rbtree(&iovad->rbroot, prev, NULL);
595 		iova->pfn_lo = pfn_lo;
596 	}
597 	if (next) {
598 		iova_insert_rbtree(&iovad->rbroot, next, NULL);
599 		iova->pfn_hi = pfn_hi;
600 	}
601 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
602 
603 	return iova;
604 
605 error:
606 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
607 	if (prev)
608 		free_iova_mem(prev);
609 	return NULL;
610 }
611 
612 /*
613  * Magazine caches for IOVA ranges.  For an introduction to magazines,
614  * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
615  * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
616  * For simplicity, we use a static magazine size and don't implement the
617  * dynamic size tuning described in the paper.
618  */
619 
620 #define IOVA_MAG_SIZE 128
621 
622 struct iova_magazine {
623 	unsigned long size;
624 	unsigned long pfns[IOVA_MAG_SIZE];
625 };
626 
627 struct iova_cpu_rcache {
628 	spinlock_t lock;
629 	struct iova_magazine *loaded;
630 	struct iova_magazine *prev;
631 };
632 
633 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
634 {
635 	return kzalloc(sizeof(struct iova_magazine), flags);
636 }
637 
638 static void iova_magazine_free(struct iova_magazine *mag)
639 {
640 	kfree(mag);
641 }
642 
643 static void
644 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
645 {
646 	unsigned long flags;
647 	int i;
648 
649 	if (!mag)
650 		return;
651 
652 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
653 
654 	for (i = 0 ; i < mag->size; ++i) {
655 		struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
656 
657 		BUG_ON(!iova);
658 		private_free_iova(iovad, iova);
659 	}
660 
661 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
662 
663 	mag->size = 0;
664 }
665 
666 static bool iova_magazine_full(struct iova_magazine *mag)
667 {
668 	return (mag && mag->size == IOVA_MAG_SIZE);
669 }
670 
671 static bool iova_magazine_empty(struct iova_magazine *mag)
672 {
673 	return (!mag || mag->size == 0);
674 }
675 
676 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
677 				       unsigned long limit_pfn)
678 {
679 	BUG_ON(iova_magazine_empty(mag));
680 
681 	if (mag->pfns[mag->size - 1] >= limit_pfn)
682 		return 0;
683 
684 	return mag->pfns[--mag->size];
685 }
686 
687 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
688 {
689 	BUG_ON(iova_magazine_full(mag));
690 
691 	mag->pfns[mag->size++] = pfn;
692 }
693 
694 static void init_iova_rcaches(struct iova_domain *iovad)
695 {
696 	struct iova_cpu_rcache *cpu_rcache;
697 	struct iova_rcache *rcache;
698 	unsigned int cpu;
699 	int i;
700 
701 	for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
702 		rcache = &iovad->rcaches[i];
703 		spin_lock_init(&rcache->lock);
704 		rcache->depot_size = 0;
705 		rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
706 		if (WARN_ON(!rcache->cpu_rcaches))
707 			continue;
708 		for_each_possible_cpu(cpu) {
709 			cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
710 			spin_lock_init(&cpu_rcache->lock);
711 			cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
712 			cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
713 		}
714 	}
715 }
716 
717 /*
718  * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
719  * return true on success.  Can fail if rcache is full and we can't free
720  * space, and free_iova() (our only caller) will then return the IOVA
721  * range to the rbtree instead.
722  */
723 static bool __iova_rcache_insert(struct iova_domain *iovad,
724 				 struct iova_rcache *rcache,
725 				 unsigned long iova_pfn)
726 {
727 	struct iova_magazine *mag_to_free = NULL;
728 	struct iova_cpu_rcache *cpu_rcache;
729 	bool can_insert = false;
730 	unsigned long flags;
731 
732 	cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
733 	spin_lock_irqsave(&cpu_rcache->lock, flags);
734 
735 	if (!iova_magazine_full(cpu_rcache->loaded)) {
736 		can_insert = true;
737 	} else if (!iova_magazine_full(cpu_rcache->prev)) {
738 		swap(cpu_rcache->prev, cpu_rcache->loaded);
739 		can_insert = true;
740 	} else {
741 		struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
742 
743 		if (new_mag) {
744 			spin_lock(&rcache->lock);
745 			if (rcache->depot_size < MAX_GLOBAL_MAGS) {
746 				rcache->depot[rcache->depot_size++] =
747 						cpu_rcache->loaded;
748 			} else {
749 				mag_to_free = cpu_rcache->loaded;
750 			}
751 			spin_unlock(&rcache->lock);
752 
753 			cpu_rcache->loaded = new_mag;
754 			can_insert = true;
755 		}
756 	}
757 
758 	if (can_insert)
759 		iova_magazine_push(cpu_rcache->loaded, iova_pfn);
760 
761 	spin_unlock_irqrestore(&cpu_rcache->lock, flags);
762 	put_cpu_ptr(rcache->cpu_rcaches);
763 
764 	if (mag_to_free) {
765 		iova_magazine_free_pfns(mag_to_free, iovad);
766 		iova_magazine_free(mag_to_free);
767 	}
768 
769 	return can_insert;
770 }
771 
772 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
773 			       unsigned long size)
774 {
775 	unsigned int log_size = order_base_2(size);
776 
777 	if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
778 		return false;
779 
780 	return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
781 }
782 
783 /*
784  * Caller wants to allocate a new IOVA range from 'rcache'.  If we can
785  * satisfy the request, return a matching non-NULL range and remove
786  * it from the 'rcache'.
787  */
788 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
789 				       unsigned long limit_pfn)
790 {
791 	struct iova_cpu_rcache *cpu_rcache;
792 	unsigned long iova_pfn = 0;
793 	bool has_pfn = false;
794 	unsigned long flags;
795 
796 	cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
797 	spin_lock_irqsave(&cpu_rcache->lock, flags);
798 
799 	if (!iova_magazine_empty(cpu_rcache->loaded)) {
800 		has_pfn = true;
801 	} else if (!iova_magazine_empty(cpu_rcache->prev)) {
802 		swap(cpu_rcache->prev, cpu_rcache->loaded);
803 		has_pfn = true;
804 	} else {
805 		spin_lock(&rcache->lock);
806 		if (rcache->depot_size > 0) {
807 			iova_magazine_free(cpu_rcache->loaded);
808 			cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
809 			has_pfn = true;
810 		}
811 		spin_unlock(&rcache->lock);
812 	}
813 
814 	if (has_pfn)
815 		iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
816 
817 	spin_unlock_irqrestore(&cpu_rcache->lock, flags);
818 	put_cpu_ptr(rcache->cpu_rcaches);
819 
820 	return iova_pfn;
821 }
822 
823 /*
824  * Try to satisfy IOVA allocation range from rcache.  Fail if requested
825  * size is too big or the DMA limit we are given isn't satisfied by the
826  * top element in the magazine.
827  */
828 static unsigned long iova_rcache_get(struct iova_domain *iovad,
829 				     unsigned long size,
830 				     unsigned long limit_pfn)
831 {
832 	unsigned int log_size = order_base_2(size);
833 
834 	if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
835 		return 0;
836 
837 	return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
838 }
839 
840 /*
841  * Free a cpu's rcache.
842  */
843 static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
844 				 struct iova_rcache *rcache)
845 {
846 	struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
847 	unsigned long flags;
848 
849 	spin_lock_irqsave(&cpu_rcache->lock, flags);
850 
851 	iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
852 	iova_magazine_free(cpu_rcache->loaded);
853 
854 	iova_magazine_free_pfns(cpu_rcache->prev, iovad);
855 	iova_magazine_free(cpu_rcache->prev);
856 
857 	spin_unlock_irqrestore(&cpu_rcache->lock, flags);
858 }
859 
860 /*
861  * free rcache data structures.
862  */
863 static void free_iova_rcaches(struct iova_domain *iovad)
864 {
865 	struct iova_rcache *rcache;
866 	unsigned long flags;
867 	unsigned int cpu;
868 	int i, j;
869 
870 	for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
871 		rcache = &iovad->rcaches[i];
872 		for_each_possible_cpu(cpu)
873 			free_cpu_iova_rcache(cpu, iovad, rcache);
874 		spin_lock_irqsave(&rcache->lock, flags);
875 		free_percpu(rcache->cpu_rcaches);
876 		for (j = 0; j < rcache->depot_size; ++j) {
877 			iova_magazine_free_pfns(rcache->depot[j], iovad);
878 			iova_magazine_free(rcache->depot[j]);
879 		}
880 		spin_unlock_irqrestore(&rcache->lock, flags);
881 	}
882 }
883 
884 /*
885  * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
886  */
887 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
888 {
889 	struct iova_cpu_rcache *cpu_rcache;
890 	struct iova_rcache *rcache;
891 	unsigned long flags;
892 	int i;
893 
894 	for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
895 		rcache = &iovad->rcaches[i];
896 		cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
897 		spin_lock_irqsave(&cpu_rcache->lock, flags);
898 		iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
899 		iova_magazine_free_pfns(cpu_rcache->prev, iovad);
900 		spin_unlock_irqrestore(&cpu_rcache->lock, flags);
901 	}
902 }
903 
904 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
905 MODULE_LICENSE("GPL");
906