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 static bool iova_rcache_insert(struct iova_domain *iovad,
21 static unsigned long iova_rcache_get(struct iova_domain *iovad,
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 static void free_global_cached_iovas(struct iova_domain *iovad);
31 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
32 unsigned long start_pfn)
35 * IOVA granularity will normally be equal to the smallest
36 * supported IOMMU page size; both *must* be capable of
37 * representing individual CPU pages exactly.
39 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
41 spin_lock_init(&iovad->iova_rbtree_lock);
42 iovad->rbroot = RB_ROOT;
43 iovad->cached_node = &iovad->anchor.node;
44 iovad->cached32_node = &iovad->anchor.node;
45 iovad->granule = granule;
46 iovad->start_pfn = start_pfn;
47 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
48 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
49 iovad->flush_cb = NULL;
51 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
52 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
53 rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
54 init_iova_rcaches(iovad);
56 EXPORT_SYMBOL_GPL(init_iova_domain);
58 static bool has_iova_flush_queue(struct iova_domain *iovad)
63 static void free_iova_flush_queue(struct iova_domain *iovad)
65 if (!has_iova_flush_queue(iovad))
68 if (timer_pending(&iovad->fq_timer))
69 del_timer(&iovad->fq_timer);
71 fq_destroy_all_entries(iovad);
73 free_percpu(iovad->fq);
76 iovad->flush_cb = NULL;
77 iovad->entry_dtor = NULL;
80 int init_iova_flush_queue(struct iova_domain *iovad,
81 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
83 struct iova_fq __percpu *queue;
86 atomic64_set(&iovad->fq_flush_start_cnt, 0);
87 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
89 queue = alloc_percpu(struct iova_fq);
93 iovad->flush_cb = flush_cb;
94 iovad->entry_dtor = entry_dtor;
96 for_each_possible_cpu(cpu) {
99 fq = per_cpu_ptr(queue, cpu);
103 spin_lock_init(&fq->lock);
110 timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
111 atomic_set(&iovad->fq_timer_on, 0);
116 static struct rb_node *
117 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
119 if (limit_pfn <= iovad->dma_32bit_pfn)
120 return iovad->cached32_node;
122 return iovad->cached_node;
126 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
128 if (new->pfn_hi < iovad->dma_32bit_pfn)
129 iovad->cached32_node = &new->node;
131 iovad->cached_node = &new->node;
135 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
137 struct iova *cached_iova;
139 cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
140 if (free == cached_iova ||
141 (free->pfn_hi < iovad->dma_32bit_pfn &&
142 free->pfn_lo >= cached_iova->pfn_lo)) {
143 iovad->cached32_node = rb_next(&free->node);
144 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
147 cached_iova = rb_entry(iovad->cached_node, struct iova, node);
148 if (free->pfn_lo >= cached_iova->pfn_lo)
149 iovad->cached_node = rb_next(&free->node);
152 /* Insert the iova into domain rbtree by holding writer lock */
154 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
155 struct rb_node *start)
157 struct rb_node **new, *parent = NULL;
159 new = (start) ? &start : &(root->rb_node);
160 /* Figure out where to put new node */
162 struct iova *this = rb_entry(*new, struct iova, node);
166 if (iova->pfn_lo < this->pfn_lo)
167 new = &((*new)->rb_left);
168 else if (iova->pfn_lo > this->pfn_lo)
169 new = &((*new)->rb_right);
171 WARN_ON(1); /* this should not happen */
175 /* Add new node and rebalance tree. */
176 rb_link_node(&iova->node, parent, new);
177 rb_insert_color(&iova->node, root);
180 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
181 unsigned long size, unsigned long limit_pfn,
182 struct iova *new, bool size_aligned)
184 struct rb_node *curr, *prev;
185 struct iova *curr_iova;
187 unsigned long new_pfn, retry_pfn;
188 unsigned long align_mask = ~0UL;
189 unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
192 align_mask <<= fls_long(size - 1);
194 /* Walk the tree backwards */
195 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
196 if (limit_pfn <= iovad->dma_32bit_pfn &&
197 size >= iovad->max32_alloc_size)
200 curr = __get_cached_rbnode(iovad, limit_pfn);
201 curr_iova = rb_entry(curr, struct iova, node);
202 retry_pfn = curr_iova->pfn_hi + 1;
206 high_pfn = min(high_pfn, curr_iova->pfn_lo);
207 new_pfn = (high_pfn - size) & align_mask;
209 curr = rb_prev(curr);
210 curr_iova = rb_entry(curr, struct iova, node);
211 } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
213 if (high_pfn < size || new_pfn < low_pfn) {
214 if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
215 high_pfn = limit_pfn;
217 curr = &iovad->anchor.node;
218 curr_iova = rb_entry(curr, struct iova, node);
221 iovad->max32_alloc_size = size;
225 /* pfn_lo will point to size aligned address if size_aligned is set */
226 new->pfn_lo = new_pfn;
227 new->pfn_hi = new->pfn_lo + size - 1;
229 /* If we have 'prev', it's a valid place to start the insertion. */
230 iova_insert_rbtree(&iovad->rbroot, new, prev);
231 __cached_rbnode_insert_update(iovad, new);
233 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
237 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
241 static struct kmem_cache *iova_cache;
242 static unsigned int iova_cache_users;
243 static DEFINE_MUTEX(iova_cache_mutex);
245 static struct iova *alloc_iova_mem(void)
247 return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
250 static void free_iova_mem(struct iova *iova)
252 if (iova->pfn_lo != IOVA_ANCHOR)
253 kmem_cache_free(iova_cache, iova);
256 int iova_cache_get(void)
258 mutex_lock(&iova_cache_mutex);
259 if (!iova_cache_users) {
260 iova_cache = kmem_cache_create(
261 "iommu_iova", sizeof(struct iova), 0,
262 SLAB_HWCACHE_ALIGN, NULL);
264 mutex_unlock(&iova_cache_mutex);
265 pr_err("Couldn't create iova cache\n");
271 mutex_unlock(&iova_cache_mutex);
275 EXPORT_SYMBOL_GPL(iova_cache_get);
277 void iova_cache_put(void)
279 mutex_lock(&iova_cache_mutex);
280 if (WARN_ON(!iova_cache_users)) {
281 mutex_unlock(&iova_cache_mutex);
285 if (!iova_cache_users)
286 kmem_cache_destroy(iova_cache);
287 mutex_unlock(&iova_cache_mutex);
289 EXPORT_SYMBOL_GPL(iova_cache_put);
292 * alloc_iova - allocates an iova
293 * @iovad: - iova domain in question
294 * @size: - size of page frames to allocate
295 * @limit_pfn: - max limit address
296 * @size_aligned: - set if size_aligned address range is required
297 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
298 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
299 * flag is set then the allocated address iova->pfn_lo will be naturally
300 * aligned on roundup_power_of_two(size).
303 alloc_iova(struct iova_domain *iovad, unsigned long size,
304 unsigned long limit_pfn,
307 struct iova *new_iova;
310 new_iova = alloc_iova_mem();
314 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
315 new_iova, size_aligned);
318 free_iova_mem(new_iova);
324 EXPORT_SYMBOL_GPL(alloc_iova);
327 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
329 struct rb_node *node = iovad->rbroot.rb_node;
331 assert_spin_locked(&iovad->iova_rbtree_lock);
334 struct iova *iova = rb_entry(node, struct iova, node);
336 if (pfn < iova->pfn_lo)
337 node = node->rb_left;
338 else if (pfn > iova->pfn_hi)
339 node = node->rb_right;
341 return iova; /* pfn falls within iova's range */
347 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
349 assert_spin_locked(&iovad->iova_rbtree_lock);
350 __cached_rbnode_delete_update(iovad, iova);
351 rb_erase(&iova->node, &iovad->rbroot);
356 * find_iova - finds an iova for a given pfn
357 * @iovad: - iova domain in question.
358 * @pfn: - page frame number
359 * This function finds and returns an iova belonging to the
360 * given domain which matches the given pfn.
362 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
367 /* Take the lock so that no other thread is manipulating the rbtree */
368 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
369 iova = private_find_iova(iovad, pfn);
370 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
373 EXPORT_SYMBOL_GPL(find_iova);
376 * __free_iova - frees the given iova
377 * @iovad: iova domain in question.
378 * @iova: iova in question.
379 * Frees the given iova belonging to the giving domain
382 __free_iova(struct iova_domain *iovad, struct iova *iova)
386 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
387 private_free_iova(iovad, iova);
388 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
390 EXPORT_SYMBOL_GPL(__free_iova);
393 * free_iova - finds and frees the iova for a given pfn
394 * @iovad: - iova domain in question.
395 * @pfn: - pfn that is allocated previously
396 * This functions finds an iova for a given pfn and then
397 * frees the iova from that domain.
400 free_iova(struct iova_domain *iovad, unsigned long pfn)
405 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
406 iova = private_find_iova(iovad, pfn);
408 private_free_iova(iovad, iova);
409 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
412 EXPORT_SYMBOL_GPL(free_iova);
415 * alloc_iova_fast - allocates an iova from rcache
416 * @iovad: - iova domain in question
417 * @size: - size of page frames to allocate
418 * @limit_pfn: - max limit address
419 * @flush_rcache: - set to flush rcache on regular allocation failure
420 * This function tries to satisfy an iova allocation from the rcache,
421 * and falls back to regular allocation on failure. If regular allocation
422 * fails too and the flush_rcache flag is set then the rcache will be flushed.
425 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
426 unsigned long limit_pfn, bool flush_rcache)
428 unsigned long iova_pfn;
429 struct iova *new_iova;
431 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
436 new_iova = alloc_iova(iovad, size, limit_pfn, true);
443 /* Try replenishing IOVAs by flushing rcache. */
444 flush_rcache = false;
445 for_each_online_cpu(cpu)
446 free_cpu_cached_iovas(cpu, iovad);
447 free_global_cached_iovas(iovad);
451 return new_iova->pfn_lo;
455 * free_iova_fast - free iova pfn range into rcache
456 * @iovad: - iova domain in question.
457 * @pfn: - pfn that is allocated previously
458 * @size: - # of pages in range
459 * This functions frees an iova range by trying to put it into the rcache,
460 * falling back to regular iova deallocation via free_iova() if this fails.
463 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
465 if (iova_rcache_insert(iovad, pfn, size))
468 free_iova(iovad, pfn);
470 EXPORT_SYMBOL_GPL(free_iova_fast);
472 #define fq_ring_for_each(i, fq) \
473 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
475 static inline bool fq_full(struct iova_fq *fq)
477 assert_spin_locked(&fq->lock);
478 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
481 static inline unsigned fq_ring_add(struct iova_fq *fq)
483 unsigned idx = fq->tail;
485 assert_spin_locked(&fq->lock);
487 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
492 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
494 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
497 assert_spin_locked(&fq->lock);
499 fq_ring_for_each(idx, fq) {
501 if (fq->entries[idx].counter >= counter)
504 if (iovad->entry_dtor)
505 iovad->entry_dtor(fq->entries[idx].data);
507 free_iova_fast(iovad,
508 fq->entries[idx].iova_pfn,
509 fq->entries[idx].pages);
511 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
515 static void iova_domain_flush(struct iova_domain *iovad)
517 atomic64_inc(&iovad->fq_flush_start_cnt);
518 iovad->flush_cb(iovad);
519 atomic64_inc(&iovad->fq_flush_finish_cnt);
522 static void fq_destroy_all_entries(struct iova_domain *iovad)
527 * This code runs when the iova_domain is being detroyed, so don't
528 * bother to free iovas, just call the entry_dtor on all remaining
531 if (!iovad->entry_dtor)
534 for_each_possible_cpu(cpu) {
535 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
538 fq_ring_for_each(idx, fq)
539 iovad->entry_dtor(fq->entries[idx].data);
543 static void fq_flush_timeout(struct timer_list *t)
545 struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
548 atomic_set(&iovad->fq_timer_on, 0);
549 iova_domain_flush(iovad);
551 for_each_possible_cpu(cpu) {
555 fq = per_cpu_ptr(iovad->fq, cpu);
556 spin_lock_irqsave(&fq->lock, flags);
557 fq_ring_free(iovad, fq);
558 spin_unlock_irqrestore(&fq->lock, flags);
562 void queue_iova(struct iova_domain *iovad,
563 unsigned long pfn, unsigned long pages,
566 struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
570 spin_lock_irqsave(&fq->lock, flags);
573 * First remove all entries from the flush queue that have already been
574 * flushed out on another CPU. This makes the fq_full() check below less
577 fq_ring_free(iovad, fq);
580 iova_domain_flush(iovad);
581 fq_ring_free(iovad, fq);
584 idx = fq_ring_add(fq);
586 fq->entries[idx].iova_pfn = pfn;
587 fq->entries[idx].pages = pages;
588 fq->entries[idx].data = data;
589 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
591 spin_unlock_irqrestore(&fq->lock, flags);
593 /* Avoid false sharing as much as possible. */
594 if (!atomic_read(&iovad->fq_timer_on) &&
595 !atomic_xchg(&iovad->fq_timer_on, 1))
596 mod_timer(&iovad->fq_timer,
597 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
601 * put_iova_domain - destroys the iova domain
602 * @iovad: - iova domain in question.
603 * All the iova's in that domain are destroyed.
605 void put_iova_domain(struct iova_domain *iovad)
607 struct iova *iova, *tmp;
609 free_iova_flush_queue(iovad);
610 free_iova_rcaches(iovad);
611 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
614 EXPORT_SYMBOL_GPL(put_iova_domain);
617 __is_range_overlap(struct rb_node *node,
618 unsigned long pfn_lo, unsigned long pfn_hi)
620 struct iova *iova = rb_entry(node, struct iova, node);
622 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
627 static inline struct iova *
628 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
632 iova = alloc_iova_mem();
634 iova->pfn_lo = pfn_lo;
635 iova->pfn_hi = pfn_hi;
642 __insert_new_range(struct iova_domain *iovad,
643 unsigned long pfn_lo, unsigned long pfn_hi)
647 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
649 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
655 __adjust_overlap_range(struct iova *iova,
656 unsigned long *pfn_lo, unsigned long *pfn_hi)
658 if (*pfn_lo < iova->pfn_lo)
659 iova->pfn_lo = *pfn_lo;
660 if (*pfn_hi > iova->pfn_hi)
661 *pfn_lo = iova->pfn_hi + 1;
665 * reserve_iova - reserves an iova in the given range
666 * @iovad: - iova domain pointer
667 * @pfn_lo: - lower page frame address
668 * @pfn_hi:- higher pfn adderss
669 * This function allocates reserves the address range from pfn_lo to pfn_hi so
670 * that this address is not dished out as part of alloc_iova.
673 reserve_iova(struct iova_domain *iovad,
674 unsigned long pfn_lo, unsigned long pfn_hi)
676 struct rb_node *node;
679 unsigned int overlap = 0;
681 /* Don't allow nonsensical pfns */
682 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
685 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
686 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
687 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
688 iova = rb_entry(node, struct iova, node);
689 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
690 if ((pfn_lo >= iova->pfn_lo) &&
691 (pfn_hi <= iova->pfn_hi))
699 /* We are here either because this is the first reserver node
700 * or need to insert remaining non overlap addr range
702 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
705 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
708 EXPORT_SYMBOL_GPL(reserve_iova);
711 * Magazine caches for IOVA ranges. For an introduction to magazines,
712 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
713 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
714 * For simplicity, we use a static magazine size and don't implement the
715 * dynamic size tuning described in the paper.
718 #define IOVA_MAG_SIZE 128
720 struct iova_magazine {
722 unsigned long pfns[IOVA_MAG_SIZE];
725 struct iova_cpu_rcache {
727 struct iova_magazine *loaded;
728 struct iova_magazine *prev;
731 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
733 return kzalloc(sizeof(struct iova_magazine), flags);
736 static void iova_magazine_free(struct iova_magazine *mag)
742 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
750 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
752 for (i = 0 ; i < mag->size; ++i) {
753 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
758 private_free_iova(iovad, iova);
761 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
766 static bool iova_magazine_full(struct iova_magazine *mag)
768 return (mag && mag->size == IOVA_MAG_SIZE);
771 static bool iova_magazine_empty(struct iova_magazine *mag)
773 return (!mag || mag->size == 0);
776 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
777 unsigned long limit_pfn)
782 BUG_ON(iova_magazine_empty(mag));
784 /* Only fall back to the rbtree if we have no suitable pfns at all */
785 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
789 /* Swap it to pop it */
791 mag->pfns[i] = mag->pfns[--mag->size];
796 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
798 BUG_ON(iova_magazine_full(mag));
800 mag->pfns[mag->size++] = pfn;
803 static void init_iova_rcaches(struct iova_domain *iovad)
805 struct iova_cpu_rcache *cpu_rcache;
806 struct iova_rcache *rcache;
810 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
811 rcache = &iovad->rcaches[i];
812 spin_lock_init(&rcache->lock);
813 rcache->depot_size = 0;
814 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
815 if (WARN_ON(!rcache->cpu_rcaches))
817 for_each_possible_cpu(cpu) {
818 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
819 spin_lock_init(&cpu_rcache->lock);
820 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
821 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
827 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
828 * return true on success. Can fail if rcache is full and we can't free
829 * space, and free_iova() (our only caller) will then return the IOVA
830 * range to the rbtree instead.
832 static bool __iova_rcache_insert(struct iova_domain *iovad,
833 struct iova_rcache *rcache,
834 unsigned long iova_pfn)
836 struct iova_magazine *mag_to_free = NULL;
837 struct iova_cpu_rcache *cpu_rcache;
838 bool can_insert = false;
841 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
842 spin_lock_irqsave(&cpu_rcache->lock, flags);
844 if (!iova_magazine_full(cpu_rcache->loaded)) {
846 } else if (!iova_magazine_full(cpu_rcache->prev)) {
847 swap(cpu_rcache->prev, cpu_rcache->loaded);
850 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
853 spin_lock(&rcache->lock);
854 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
855 rcache->depot[rcache->depot_size++] =
858 mag_to_free = cpu_rcache->loaded;
860 spin_unlock(&rcache->lock);
862 cpu_rcache->loaded = new_mag;
868 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
870 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
873 iova_magazine_free_pfns(mag_to_free, iovad);
874 iova_magazine_free(mag_to_free);
880 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
883 unsigned int log_size = order_base_2(size);
885 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
888 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
892 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
893 * satisfy the request, return a matching non-NULL range and remove
894 * it from the 'rcache'.
896 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
897 unsigned long limit_pfn)
899 struct iova_cpu_rcache *cpu_rcache;
900 unsigned long iova_pfn = 0;
901 bool has_pfn = false;
904 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
905 spin_lock_irqsave(&cpu_rcache->lock, flags);
907 if (!iova_magazine_empty(cpu_rcache->loaded)) {
909 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
910 swap(cpu_rcache->prev, cpu_rcache->loaded);
913 spin_lock(&rcache->lock);
914 if (rcache->depot_size > 0) {
915 iova_magazine_free(cpu_rcache->loaded);
916 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
919 spin_unlock(&rcache->lock);
923 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
925 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
931 * Try to satisfy IOVA allocation range from rcache. Fail if requested
932 * size is too big or the DMA limit we are given isn't satisfied by the
933 * top element in the magazine.
935 static unsigned long iova_rcache_get(struct iova_domain *iovad,
937 unsigned long limit_pfn)
939 unsigned int log_size = order_base_2(size);
941 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
944 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
948 * free rcache data structures.
950 static void free_iova_rcaches(struct iova_domain *iovad)
952 struct iova_rcache *rcache;
953 struct iova_cpu_rcache *cpu_rcache;
957 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
958 rcache = &iovad->rcaches[i];
959 for_each_possible_cpu(cpu) {
960 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
961 iova_magazine_free(cpu_rcache->loaded);
962 iova_magazine_free(cpu_rcache->prev);
964 free_percpu(rcache->cpu_rcaches);
965 for (j = 0; j < rcache->depot_size; ++j)
966 iova_magazine_free(rcache->depot[j]);
971 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
973 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
975 struct iova_cpu_rcache *cpu_rcache;
976 struct iova_rcache *rcache;
980 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
981 rcache = &iovad->rcaches[i];
982 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
983 spin_lock_irqsave(&cpu_rcache->lock, flags);
984 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
985 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
986 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
991 * free all the IOVA ranges of global cache
993 static void free_global_cached_iovas(struct iova_domain *iovad)
995 struct iova_rcache *rcache;
999 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1000 rcache = &iovad->rcaches[i];
1001 spin_lock_irqsave(&rcache->lock, flags);
1002 for (j = 0; j < rcache->depot_size; ++j) {
1003 iova_magazine_free_pfns(rcache->depot[j], iovad);
1004 iova_magazine_free(rcache->depot[j]);
1006 rcache->depot_size = 0;
1007 spin_unlock_irqrestore(&rcache->lock, flags);
1010 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1011 MODULE_LICENSE("GPL");