staging: fsl-mc: remove dpmng API files
[linux-2.6/btrfs-unstable.git] / drivers / iommu / iova.c
blob5c88ba70e4e0fe92b282ebf1e8a1d0b2857677d4
1 /*
2 * Copyright © 2006-2009, Intel Corporation.
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.
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.
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.
17 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
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>
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);
35 void
36 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
37 unsigned long start_pfn, unsigned long pfn_32bit)
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.
44 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
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);
54 EXPORT_SYMBOL_GPL(init_iova_domain);
56 static struct rb_node *
57 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
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;
71 static void
72 __cached_rbnode_insert_update(struct iova_domain *iovad,
73 unsigned long limit_pfn, struct iova *new)
75 if (limit_pfn != iovad->dma_32bit_pfn)
76 return;
77 iovad->cached32_node = &new->node;
80 static void
81 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
83 struct iova *cached_iova;
84 struct rb_node *curr;
86 if (!iovad->cached32_node)
87 return;
88 curr = iovad->cached32_node;
89 cached_iova = rb_entry(curr, struct iova, node);
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);
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;
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)
108 struct rb_node **new, *parent = NULL;
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);
115 parent = *new;
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;
126 /* Add new node and rebalance tree. */
127 rb_link_node(&iova->node, parent, new);
128 rb_insert_color(&iova->node, root);
132 * Computes the padding size required, to make the start address
133 * naturally aligned on the power-of-two order of its size
135 static unsigned int
136 iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
138 return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1);
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)
145 struct rb_node *prev, *curr = NULL;
146 unsigned long flags;
147 unsigned long saved_pfn;
148 unsigned int pad_size = 0;
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);
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 */
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);
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;
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;
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);
192 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
195 return 0;
198 static struct kmem_cache *iova_cache;
199 static unsigned int iova_cache_users;
200 static DEFINE_MUTEX(iova_cache_mutex);
202 struct iova *alloc_iova_mem(void)
204 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
206 EXPORT_SYMBOL(alloc_iova_mem);
208 void free_iova_mem(struct iova *iova)
210 kmem_cache_free(iova_cache, iova);
212 EXPORT_SYMBOL(free_iova_mem);
214 int iova_cache_get(void)
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;
228 iova_cache_users++;
229 mutex_unlock(&iova_cache_mutex);
231 return 0;
233 EXPORT_SYMBOL_GPL(iova_cache_get);
235 void iova_cache_put(void)
237 mutex_lock(&iova_cache_mutex);
238 if (WARN_ON(!iova_cache_users)) {
239 mutex_unlock(&iova_cache_mutex);
240 return;
242 iova_cache_users--;
243 if (!iova_cache_users)
244 kmem_cache_destroy(iova_cache);
245 mutex_unlock(&iova_cache_mutex);
247 EXPORT_SYMBOL_GPL(iova_cache_put);
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).
260 struct iova *
261 alloc_iova(struct iova_domain *iovad, unsigned long size,
262 unsigned long limit_pfn,
263 bool size_aligned)
265 struct iova *new_iova;
266 int ret;
268 new_iova = alloc_iova_mem();
269 if (!new_iova)
270 return NULL;
272 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
273 new_iova, size_aligned);
275 if (ret) {
276 free_iova_mem(new_iova);
277 return NULL;
280 return new_iova;
282 EXPORT_SYMBOL_GPL(alloc_iova);
284 static struct iova *
285 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
287 struct rb_node *node = iovad->rbroot.rb_node;
289 assert_spin_locked(&iovad->iova_rbtree_lock);
291 while (node) {
292 struct iova *iova = rb_entry(node, struct iova, node);
294 /* If pfn falls within iova's range, return iova */
295 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
296 return iova;
299 if (pfn < iova->pfn_lo)
300 node = node->rb_left;
301 else if (pfn > iova->pfn_lo)
302 node = node->rb_right;
305 return NULL;
308 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
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);
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.
323 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
325 unsigned long flags;
326 struct iova *iova;
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;
334 EXPORT_SYMBOL_GPL(find_iova);
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
342 void
343 __free_iova(struct iova_domain *iovad, struct iova *iova)
345 unsigned long flags;
347 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
348 private_free_iova(iovad, iova);
349 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
351 EXPORT_SYMBOL_GPL(__free_iova);
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.
360 void
361 free_iova(struct iova_domain *iovad, unsigned long pfn)
363 struct iova *iova = find_iova(iovad, pfn);
365 if (iova)
366 __free_iova(iovad, iova);
369 EXPORT_SYMBOL_GPL(free_iova);
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.
379 unsigned long
380 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
381 unsigned long limit_pfn)
383 bool flushed_rcache = false;
384 unsigned long iova_pfn;
385 struct iova *new_iova;
387 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
388 if (iova_pfn)
389 return iova_pfn;
391 retry:
392 new_iova = alloc_iova(iovad, size, limit_pfn, true);
393 if (!new_iova) {
394 unsigned int cpu;
396 if (flushed_rcache)
397 return 0;
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;
408 return new_iova->pfn_lo;
410 EXPORT_SYMBOL_GPL(alloc_iova_fast);
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.
420 void
421 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
423 if (iova_rcache_insert(iovad, pfn, size))
424 return;
426 free_iova(iovad, pfn);
428 EXPORT_SYMBOL_GPL(free_iova_fast);
431 * put_iova_domain - destroys the iova doamin
432 * @iovad: - iova domain in question.
433 * All the iova's in that domain are destroyed.
435 void put_iova_domain(struct iova_domain *iovad)
437 struct rb_node *node;
438 unsigned long flags;
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);
446 rb_erase(node, &iovad->rbroot);
447 free_iova_mem(iova);
448 node = rb_first(&iovad->rbroot);
450 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
452 EXPORT_SYMBOL_GPL(put_iova_domain);
454 static int
455 __is_range_overlap(struct rb_node *node,
456 unsigned long pfn_lo, unsigned long pfn_hi)
458 struct iova *iova = rb_entry(node, struct iova, node);
460 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
461 return 1;
462 return 0;
465 static inline struct iova *
466 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
468 struct iova *iova;
470 iova = alloc_iova_mem();
471 if (iova) {
472 iova->pfn_lo = pfn_lo;
473 iova->pfn_hi = pfn_hi;
476 return iova;
479 static struct iova *
480 __insert_new_range(struct iova_domain *iovad,
481 unsigned long pfn_lo, unsigned long pfn_hi)
483 struct iova *iova;
485 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
486 if (iova)
487 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
489 return iova;
492 static void
493 __adjust_overlap_range(struct iova *iova,
494 unsigned long *pfn_lo, unsigned long *pfn_hi)
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;
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.
510 struct iova *
511 reserve_iova(struct iova_domain *iovad,
512 unsigned long pfn_lo, unsigned long pfn_hi)
514 struct rb_node *node;
515 unsigned long flags;
516 struct iova *iova;
517 unsigned int overlap = 0;
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;
529 } else if (overlap)
530 break;
533 /* We are here either because this is the first reserver node
534 * or need to insert remaining non overlap addr range
536 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
537 finish:
539 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
540 return iova;
542 EXPORT_SYMBOL_GPL(reserve_iova);
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.
551 void
552 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
554 unsigned long flags;
555 struct rb_node *node;
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;
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);
567 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
569 EXPORT_SYMBOL_GPL(copy_reserved_iova);
571 struct iova *
572 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
573 unsigned long pfn_lo, unsigned long pfn_hi)
575 unsigned long flags;
576 struct iova *prev = NULL, *next = NULL;
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;
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;
590 __cached_rbnode_delete_update(iovad, iova);
591 rb_erase(&iova->node, &iovad->rbroot);
593 if (prev) {
594 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
595 iova->pfn_lo = pfn_lo;
597 if (next) {
598 iova_insert_rbtree(&iovad->rbroot, next, NULL);
599 iova->pfn_hi = pfn_hi;
601 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
603 return iova;
605 error:
606 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
607 if (prev)
608 free_iova_mem(prev);
609 return NULL;
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.
620 #define IOVA_MAG_SIZE 128
622 struct iova_magazine {
623 unsigned long size;
624 unsigned long pfns[IOVA_MAG_SIZE];
627 struct iova_cpu_rcache {
628 spinlock_t lock;
629 struct iova_magazine *loaded;
630 struct iova_magazine *prev;
633 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
635 return kzalloc(sizeof(struct iova_magazine), flags);
638 static void iova_magazine_free(struct iova_magazine *mag)
640 kfree(mag);
643 static void
644 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
646 unsigned long flags;
647 int i;
649 if (!mag)
650 return;
652 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
654 for (i = 0 ; i < mag->size; ++i) {
655 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
657 BUG_ON(!iova);
658 private_free_iova(iovad, iova);
661 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
663 mag->size = 0;
666 static bool iova_magazine_full(struct iova_magazine *mag)
668 return (mag && mag->size == IOVA_MAG_SIZE);
671 static bool iova_magazine_empty(struct iova_magazine *mag)
673 return (!mag || mag->size == 0);
676 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
677 unsigned long limit_pfn)
679 BUG_ON(iova_magazine_empty(mag));
681 if (mag->pfns[mag->size - 1] >= limit_pfn)
682 return 0;
684 return mag->pfns[--mag->size];
687 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
689 BUG_ON(iova_magazine_full(mag));
691 mag->pfns[mag->size++] = pfn;
694 static void init_iova_rcaches(struct iova_domain *iovad)
696 struct iova_cpu_rcache *cpu_rcache;
697 struct iova_rcache *rcache;
698 unsigned int cpu;
699 int i;
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);
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.
723 static bool __iova_rcache_insert(struct iova_domain *iovad,
724 struct iova_rcache *rcache,
725 unsigned long iova_pfn)
727 struct iova_magazine *mag_to_free = NULL;
728 struct iova_cpu_rcache *cpu_rcache;
729 bool can_insert = false;
730 unsigned long flags;
732 cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
733 spin_lock_irqsave(&cpu_rcache->lock, flags);
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);
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;
751 spin_unlock(&rcache->lock);
753 cpu_rcache->loaded = new_mag;
754 can_insert = true;
758 if (can_insert)
759 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
761 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
762 put_cpu_ptr(rcache->cpu_rcaches);
764 if (mag_to_free) {
765 iova_magazine_free_pfns(mag_to_free, iovad);
766 iova_magazine_free(mag_to_free);
769 return can_insert;
772 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
773 unsigned long size)
775 unsigned int log_size = order_base_2(size);
777 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
778 return false;
780 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
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'.
788 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
789 unsigned long limit_pfn)
791 struct iova_cpu_rcache *cpu_rcache;
792 unsigned long iova_pfn = 0;
793 bool has_pfn = false;
794 unsigned long flags;
796 cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
797 spin_lock_irqsave(&cpu_rcache->lock, flags);
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;
811 spin_unlock(&rcache->lock);
814 if (has_pfn)
815 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
817 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
818 put_cpu_ptr(rcache->cpu_rcaches);
820 return iova_pfn;
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.
828 static unsigned long iova_rcache_get(struct iova_domain *iovad,
829 unsigned long size,
830 unsigned long limit_pfn)
832 unsigned int log_size = order_base_2(size);
834 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
835 return 0;
837 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
841 * Free a cpu's rcache.
843 static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
844 struct iova_rcache *rcache)
846 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
847 unsigned long flags;
849 spin_lock_irqsave(&cpu_rcache->lock, flags);
851 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
852 iova_magazine_free(cpu_rcache->loaded);
854 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
855 iova_magazine_free(cpu_rcache->prev);
857 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
861 * free rcache data structures.
863 static void free_iova_rcaches(struct iova_domain *iovad)
865 struct iova_rcache *rcache;
866 unsigned long flags;
867 unsigned int cpu;
868 int i, j;
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]);
880 spin_unlock_irqrestore(&rcache->lock, flags);
885 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
887 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
889 struct iova_cpu_rcache *cpu_rcache;
890 struct iova_rcache *rcache;
891 unsigned long flags;
892 int i;
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);
904 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
905 MODULE_LICENSE("GPL");