1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2009, Intel Corporation.
5 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
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>
15 /* The anchor node sits above the top of the usable address space */
16 #define IOVA_ANCHOR ~0UL
18 #define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */
20 static bool iova_rcache_insert(struct iova_domain *iovad,
23 static unsigned long iova_rcache_get(struct iova_domain *iovad,
25 unsigned long limit_pfn);
26 static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
27 static void free_iova_rcaches(struct iova_domain *iovad);
29 unsigned long iova_rcache_range(void)
31 return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1);
34 static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node)
36 struct iova_domain *iovad;
38 iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead);
40 free_cpu_cached_iovas(cpu, iovad);
44 static void free_global_cached_iovas(struct iova_domain *iovad);
46 static struct iova *to_iova(struct rb_node *node)
48 return rb_entry(node, struct iova, node);
52 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
53 unsigned long start_pfn)
56 * IOVA granularity will normally be equal to the smallest
57 * supported IOMMU page size; both *must* be capable of
58 * representing individual CPU pages exactly.
60 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
62 spin_lock_init(&iovad->iova_rbtree_lock);
63 iovad->rbroot = RB_ROOT;
64 iovad->cached_node = &iovad->anchor.node;
65 iovad->cached32_node = &iovad->anchor.node;
66 iovad->granule = granule;
67 iovad->start_pfn = start_pfn;
68 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
69 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
70 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
71 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
72 rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
74 EXPORT_SYMBOL_GPL(init_iova_domain);
76 static struct rb_node *
77 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
79 if (limit_pfn <= iovad->dma_32bit_pfn)
80 return iovad->cached32_node;
82 return iovad->cached_node;
86 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
88 if (new->pfn_hi < iovad->dma_32bit_pfn)
89 iovad->cached32_node = &new->node;
91 iovad->cached_node = &new->node;
95 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
97 struct iova *cached_iova;
99 cached_iova = to_iova(iovad->cached32_node);
100 if (free == cached_iova ||
101 (free->pfn_hi < iovad->dma_32bit_pfn &&
102 free->pfn_lo >= cached_iova->pfn_lo))
103 iovad->cached32_node = rb_next(&free->node);
105 if (free->pfn_lo < iovad->dma_32bit_pfn)
106 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
108 cached_iova = to_iova(iovad->cached_node);
109 if (free->pfn_lo >= cached_iova->pfn_lo)
110 iovad->cached_node = rb_next(&free->node);
113 static struct rb_node *iova_find_limit(struct iova_domain *iovad, unsigned long limit_pfn)
115 struct rb_node *node, *next;
117 * Ideally what we'd like to judge here is whether limit_pfn is close
118 * enough to the highest-allocated IOVA that starting the allocation
119 * walk from the anchor node will be quicker than this initial work to
120 * find an exact starting point (especially if that ends up being the
121 * anchor node anyway). This is an incredibly crude approximation which
122 * only really helps the most likely case, but is at least trivially easy.
124 if (limit_pfn > iovad->dma_32bit_pfn)
125 return &iovad->anchor.node;
127 node = iovad->rbroot.rb_node;
128 while (to_iova(node)->pfn_hi < limit_pfn)
129 node = node->rb_right;
132 while (node->rb_left && to_iova(node->rb_left)->pfn_lo >= limit_pfn)
133 node = node->rb_left;
138 next = node->rb_left;
139 while (next->rb_right) {
140 next = next->rb_right;
141 if (to_iova(next)->pfn_lo >= limit_pfn) {
150 /* Insert the iova into domain rbtree by holding writer lock */
152 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
153 struct rb_node *start)
155 struct rb_node **new, *parent = NULL;
157 new = (start) ? &start : &(root->rb_node);
158 /* Figure out where to put new node */
160 struct iova *this = to_iova(*new);
164 if (iova->pfn_lo < this->pfn_lo)
165 new = &((*new)->rb_left);
166 else if (iova->pfn_lo > this->pfn_lo)
167 new = &((*new)->rb_right);
169 WARN_ON(1); /* this should not happen */
173 /* Add new node and rebalance tree. */
174 rb_link_node(&iova->node, parent, new);
175 rb_insert_color(&iova->node, root);
178 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
179 unsigned long size, unsigned long limit_pfn,
180 struct iova *new, bool size_aligned)
182 struct rb_node *curr, *prev;
183 struct iova *curr_iova;
185 unsigned long new_pfn, retry_pfn;
186 unsigned long align_mask = ~0UL;
187 unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
190 align_mask <<= fls_long(size - 1);
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)
198 curr = __get_cached_rbnode(iovad, limit_pfn);
199 curr_iova = to_iova(curr);
200 retry_pfn = curr_iova->pfn_hi;
204 high_pfn = min(high_pfn, curr_iova->pfn_lo);
205 new_pfn = (high_pfn - size) & align_mask;
207 curr = rb_prev(curr);
208 curr_iova = to_iova(curr);
209 } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
211 if (high_pfn < size || new_pfn < low_pfn) {
212 if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
213 high_pfn = limit_pfn;
214 low_pfn = retry_pfn + 1;
215 curr = iova_find_limit(iovad, limit_pfn);
216 curr_iova = to_iova(curr);
219 iovad->max32_alloc_size = size;
223 /* pfn_lo will point to size aligned address if size_aligned is set */
224 new->pfn_lo = new_pfn;
225 new->pfn_hi = new->pfn_lo + size - 1;
227 /* If we have 'prev', it's a valid place to start the insertion. */
228 iova_insert_rbtree(&iovad->rbroot, new, prev);
229 __cached_rbnode_insert_update(iovad, new);
231 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
235 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
239 static struct kmem_cache *iova_cache;
240 static unsigned int iova_cache_users;
241 static DEFINE_MUTEX(iova_cache_mutex);
243 static struct iova *alloc_iova_mem(void)
245 return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
248 static void free_iova_mem(struct iova *iova)
250 if (iova->pfn_lo != IOVA_ANCHOR)
251 kmem_cache_free(iova_cache, iova);
254 int iova_cache_get(void)
256 mutex_lock(&iova_cache_mutex);
257 if (!iova_cache_users) {
260 ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
263 mutex_unlock(&iova_cache_mutex);
264 pr_err("Couldn't register cpuhp handler\n");
268 iova_cache = kmem_cache_create(
269 "iommu_iova", sizeof(struct iova), 0,
270 SLAB_HWCACHE_ALIGN, NULL);
272 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
273 mutex_unlock(&iova_cache_mutex);
274 pr_err("Couldn't create iova cache\n");
280 mutex_unlock(&iova_cache_mutex);
284 EXPORT_SYMBOL_GPL(iova_cache_get);
286 void iova_cache_put(void)
288 mutex_lock(&iova_cache_mutex);
289 if (WARN_ON(!iova_cache_users)) {
290 mutex_unlock(&iova_cache_mutex);
294 if (!iova_cache_users) {
295 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
296 kmem_cache_destroy(iova_cache);
298 mutex_unlock(&iova_cache_mutex);
300 EXPORT_SYMBOL_GPL(iova_cache_put);
303 * alloc_iova - allocates an iova
304 * @iovad: - iova domain in question
305 * @size: - size of page frames to allocate
306 * @limit_pfn: - max limit address
307 * @size_aligned: - set if size_aligned address range is required
308 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
309 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
310 * flag is set then the allocated address iova->pfn_lo will be naturally
311 * aligned on roundup_power_of_two(size).
314 alloc_iova(struct iova_domain *iovad, unsigned long size,
315 unsigned long limit_pfn,
318 struct iova *new_iova;
321 new_iova = alloc_iova_mem();
325 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
326 new_iova, size_aligned);
329 free_iova_mem(new_iova);
335 EXPORT_SYMBOL_GPL(alloc_iova);
338 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
340 struct rb_node *node = iovad->rbroot.rb_node;
342 assert_spin_locked(&iovad->iova_rbtree_lock);
345 struct iova *iova = to_iova(node);
347 if (pfn < iova->pfn_lo)
348 node = node->rb_left;
349 else if (pfn > iova->pfn_hi)
350 node = node->rb_right;
352 return iova; /* pfn falls within iova's range */
358 static void remove_iova(struct iova_domain *iovad, struct iova *iova)
360 assert_spin_locked(&iovad->iova_rbtree_lock);
361 __cached_rbnode_delete_update(iovad, iova);
362 rb_erase(&iova->node, &iovad->rbroot);
366 * find_iova - finds an iova for a given pfn
367 * @iovad: - iova domain in question.
368 * @pfn: - page frame number
369 * This function finds and returns an iova belonging to the
370 * given domain which matches the given pfn.
372 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
377 /* Take the lock so that no other thread is manipulating the rbtree */
378 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
379 iova = private_find_iova(iovad, pfn);
380 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
383 EXPORT_SYMBOL_GPL(find_iova);
386 * __free_iova - frees the given iova
387 * @iovad: iova domain in question.
388 * @iova: iova in question.
389 * Frees the given iova belonging to the giving domain
392 __free_iova(struct iova_domain *iovad, struct iova *iova)
396 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
397 remove_iova(iovad, iova);
398 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
401 EXPORT_SYMBOL_GPL(__free_iova);
404 * free_iova - finds and frees the iova for a given pfn
405 * @iovad: - iova domain in question.
406 * @pfn: - pfn that is allocated previously
407 * This functions finds an iova for a given pfn and then
408 * frees the iova from that domain.
411 free_iova(struct iova_domain *iovad, unsigned long pfn)
416 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
417 iova = private_find_iova(iovad, pfn);
419 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
422 remove_iova(iovad, iova);
423 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
426 EXPORT_SYMBOL_GPL(free_iova);
429 * alloc_iova_fast - allocates an iova from rcache
430 * @iovad: - iova domain in question
431 * @size: - size of page frames to allocate
432 * @limit_pfn: - max limit address
433 * @flush_rcache: - set to flush rcache on regular allocation failure
434 * This function tries to satisfy an iova allocation from the rcache,
435 * and falls back to regular allocation on failure. If regular allocation
436 * fails too and the flush_rcache flag is set then the rcache will be flushed.
439 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
440 unsigned long limit_pfn, bool flush_rcache)
442 unsigned long iova_pfn;
443 struct iova *new_iova;
446 * Freeing non-power-of-two-sized allocations back into the IOVA caches
447 * will come back to bite us badly, so we have to waste a bit of space
448 * rounding up anything cacheable to make sure that can't happen. The
449 * order of the unadjusted size will still match upon freeing.
451 if (size < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
452 size = roundup_pow_of_two(size);
454 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
459 new_iova = alloc_iova(iovad, size, limit_pfn, true);
466 /* Try replenishing IOVAs by flushing rcache. */
467 flush_rcache = false;
468 for_each_online_cpu(cpu)
469 free_cpu_cached_iovas(cpu, iovad);
470 free_global_cached_iovas(iovad);
474 return new_iova->pfn_lo;
476 EXPORT_SYMBOL_GPL(alloc_iova_fast);
479 * free_iova_fast - free iova pfn range into rcache
480 * @iovad: - iova domain in question.
481 * @pfn: - pfn that is allocated previously
482 * @size: - # of pages in range
483 * This functions frees an iova range by trying to put it into the rcache,
484 * falling back to regular iova deallocation via free_iova() if this fails.
487 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
489 if (iova_rcache_insert(iovad, pfn, size))
492 free_iova(iovad, pfn);
494 EXPORT_SYMBOL_GPL(free_iova_fast);
496 static void iova_domain_free_rcaches(struct iova_domain *iovad)
498 cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
500 free_iova_rcaches(iovad);
504 * put_iova_domain - destroys the iova domain
505 * @iovad: - iova domain in question.
506 * All the iova's in that domain are destroyed.
508 void put_iova_domain(struct iova_domain *iovad)
510 struct iova *iova, *tmp;
513 iova_domain_free_rcaches(iovad);
515 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
518 EXPORT_SYMBOL_GPL(put_iova_domain);
521 __is_range_overlap(struct rb_node *node,
522 unsigned long pfn_lo, unsigned long pfn_hi)
524 struct iova *iova = to_iova(node);
526 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
531 static inline struct iova *
532 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
536 iova = alloc_iova_mem();
538 iova->pfn_lo = pfn_lo;
539 iova->pfn_hi = pfn_hi;
546 __insert_new_range(struct iova_domain *iovad,
547 unsigned long pfn_lo, unsigned long pfn_hi)
551 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
553 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
559 __adjust_overlap_range(struct iova *iova,
560 unsigned long *pfn_lo, unsigned long *pfn_hi)
562 if (*pfn_lo < iova->pfn_lo)
563 iova->pfn_lo = *pfn_lo;
564 if (*pfn_hi > iova->pfn_hi)
565 *pfn_lo = iova->pfn_hi + 1;
569 * reserve_iova - reserves an iova in the given range
570 * @iovad: - iova domain pointer
571 * @pfn_lo: - lower page frame address
572 * @pfn_hi:- higher pfn adderss
573 * This function allocates reserves the address range from pfn_lo to pfn_hi so
574 * that this address is not dished out as part of alloc_iova.
577 reserve_iova(struct iova_domain *iovad,
578 unsigned long pfn_lo, unsigned long pfn_hi)
580 struct rb_node *node;
583 unsigned int overlap = 0;
585 /* Don't allow nonsensical pfns */
586 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
589 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
590 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
591 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
592 iova = to_iova(node);
593 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
594 if ((pfn_lo >= iova->pfn_lo) &&
595 (pfn_hi <= iova->pfn_hi))
603 /* We are here either because this is the first reserver node
604 * or need to insert remaining non overlap addr range
606 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
609 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
612 EXPORT_SYMBOL_GPL(reserve_iova);
615 * Magazine caches for IOVA ranges. For an introduction to magazines,
616 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
617 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
618 * For simplicity, we use a static magazine size and don't implement the
619 * dynamic size tuning described in the paper.
623 * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to
624 * assure size of 'iova_magazine' to be 1024 bytes, so that no memory
627 #define IOVA_MAG_SIZE 127
628 #define MAX_GLOBAL_MAGS 32 /* magazines per bin */
630 struct iova_magazine {
632 unsigned long pfns[IOVA_MAG_SIZE];
635 struct iova_cpu_rcache {
637 struct iova_magazine *loaded;
638 struct iova_magazine *prev;
643 unsigned long depot_size;
644 struct iova_magazine *depot[MAX_GLOBAL_MAGS];
645 struct iova_cpu_rcache __percpu *cpu_rcaches;
648 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
650 return kzalloc(sizeof(struct iova_magazine), flags);
653 static void iova_magazine_free(struct iova_magazine *mag)
659 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
664 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
666 for (i = 0 ; i < mag->size; ++i) {
667 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
672 remove_iova(iovad, iova);
676 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
681 static bool iova_magazine_full(struct iova_magazine *mag)
683 return mag->size == IOVA_MAG_SIZE;
686 static bool iova_magazine_empty(struct iova_magazine *mag)
688 return mag->size == 0;
691 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
692 unsigned long limit_pfn)
697 /* Only fall back to the rbtree if we have no suitable pfns at all */
698 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
702 /* Swap it to pop it */
704 mag->pfns[i] = mag->pfns[--mag->size];
709 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
711 mag->pfns[mag->size++] = pfn;
714 int iova_domain_init_rcaches(struct iova_domain *iovad)
719 iovad->rcaches = kcalloc(IOVA_RANGE_CACHE_MAX_SIZE,
720 sizeof(struct iova_rcache),
725 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
726 struct iova_cpu_rcache *cpu_rcache;
727 struct iova_rcache *rcache;
729 rcache = &iovad->rcaches[i];
730 spin_lock_init(&rcache->lock);
731 rcache->depot_size = 0;
732 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),
734 if (!rcache->cpu_rcaches) {
738 for_each_possible_cpu(cpu) {
739 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
741 spin_lock_init(&cpu_rcache->lock);
742 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
743 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
744 if (!cpu_rcache->loaded || !cpu_rcache->prev) {
751 ret = cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
758 free_iova_rcaches(iovad);
761 EXPORT_SYMBOL_GPL(iova_domain_init_rcaches);
764 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
765 * return true on success. Can fail if rcache is full and we can't free
766 * space, and free_iova() (our only caller) will then return the IOVA
767 * range to the rbtree instead.
769 static bool __iova_rcache_insert(struct iova_domain *iovad,
770 struct iova_rcache *rcache,
771 unsigned long iova_pfn)
773 struct iova_magazine *mag_to_free = NULL;
774 struct iova_cpu_rcache *cpu_rcache;
775 bool can_insert = false;
778 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
779 spin_lock_irqsave(&cpu_rcache->lock, flags);
781 if (!iova_magazine_full(cpu_rcache->loaded)) {
783 } else if (!iova_magazine_full(cpu_rcache->prev)) {
784 swap(cpu_rcache->prev, cpu_rcache->loaded);
787 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
790 spin_lock(&rcache->lock);
791 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
792 rcache->depot[rcache->depot_size++] =
795 mag_to_free = cpu_rcache->loaded;
797 spin_unlock(&rcache->lock);
799 cpu_rcache->loaded = new_mag;
805 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
807 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
810 iova_magazine_free_pfns(mag_to_free, iovad);
811 iova_magazine_free(mag_to_free);
817 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
820 unsigned int log_size = order_base_2(size);
822 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
825 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
829 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
830 * satisfy the request, return a matching non-NULL range and remove
831 * it from the 'rcache'.
833 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
834 unsigned long limit_pfn)
836 struct iova_cpu_rcache *cpu_rcache;
837 unsigned long iova_pfn = 0;
838 bool has_pfn = false;
841 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
842 spin_lock_irqsave(&cpu_rcache->lock, flags);
844 if (!iova_magazine_empty(cpu_rcache->loaded)) {
846 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
847 swap(cpu_rcache->prev, cpu_rcache->loaded);
850 spin_lock(&rcache->lock);
851 if (rcache->depot_size > 0) {
852 iova_magazine_free(cpu_rcache->loaded);
853 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
856 spin_unlock(&rcache->lock);
860 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
862 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
868 * Try to satisfy IOVA allocation range from rcache. Fail if requested
869 * size is too big or the DMA limit we are given isn't satisfied by the
870 * top element in the magazine.
872 static unsigned long iova_rcache_get(struct iova_domain *iovad,
874 unsigned long limit_pfn)
876 unsigned int log_size = order_base_2(size);
878 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
881 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
885 * free rcache data structures.
887 static void free_iova_rcaches(struct iova_domain *iovad)
889 struct iova_rcache *rcache;
890 struct iova_cpu_rcache *cpu_rcache;
894 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
895 rcache = &iovad->rcaches[i];
896 if (!rcache->cpu_rcaches)
898 for_each_possible_cpu(cpu) {
899 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
900 iova_magazine_free(cpu_rcache->loaded);
901 iova_magazine_free(cpu_rcache->prev);
903 free_percpu(rcache->cpu_rcaches);
904 for (j = 0; j < rcache->depot_size; ++j)
905 iova_magazine_free(rcache->depot[j]);
908 kfree(iovad->rcaches);
909 iovad->rcaches = NULL;
913 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
915 static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
917 struct iova_cpu_rcache *cpu_rcache;
918 struct iova_rcache *rcache;
922 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
923 rcache = &iovad->rcaches[i];
924 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
925 spin_lock_irqsave(&cpu_rcache->lock, flags);
926 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
927 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
928 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
933 * free all the IOVA ranges of global cache
935 static void free_global_cached_iovas(struct iova_domain *iovad)
937 struct iova_rcache *rcache;
941 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
942 rcache = &iovad->rcaches[i];
943 spin_lock_irqsave(&rcache->lock, flags);
944 for (j = 0; j < rcache->depot_size; ++j) {
945 iova_magazine_free_pfns(rcache->depot[j], iovad);
946 iova_magazine_free(rcache->depot[j]);
948 rcache->depot_size = 0;
949 spin_unlock_irqrestore(&rcache->lock, flags);
952 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
953 MODULE_LICENSE("GPL");