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_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
26 static void free_iova_rcaches(struct iova_domain *iovad);
27 static void fq_destroy_all_entries(struct iova_domain *iovad);
28 static void fq_flush_timeout(struct timer_list *t);
30 static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node)
32 struct iova_domain *iovad;
34 iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead);
36 free_cpu_cached_iovas(cpu, iovad);
40 static void free_global_cached_iovas(struct iova_domain *iovad);
42 static struct iova *to_iova(struct rb_node *node)
44 return rb_entry(node, struct iova, node);
48 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
49 unsigned long start_pfn)
52 * IOVA granularity will normally be equal to the smallest
53 * supported IOMMU page size; both *must* be capable of
54 * representing individual CPU pages exactly.
56 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
58 spin_lock_init(&iovad->iova_rbtree_lock);
59 iovad->rbroot = RB_ROOT;
60 iovad->cached_node = &iovad->anchor.node;
61 iovad->cached32_node = &iovad->anchor.node;
62 iovad->granule = granule;
63 iovad->start_pfn = start_pfn;
64 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
65 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
66 iovad->flush_cb = NULL;
68 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
69 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
70 rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
71 cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD, &iovad->cpuhp_dead);
72 init_iova_rcaches(iovad);
74 EXPORT_SYMBOL_GPL(init_iova_domain);
76 static bool has_iova_flush_queue(struct iova_domain *iovad)
81 static void free_iova_flush_queue(struct iova_domain *iovad)
83 if (!has_iova_flush_queue(iovad))
86 if (timer_pending(&iovad->fq_timer))
87 del_timer(&iovad->fq_timer);
89 fq_destroy_all_entries(iovad);
91 free_percpu(iovad->fq);
94 iovad->flush_cb = NULL;
95 iovad->entry_dtor = NULL;
98 int init_iova_flush_queue(struct iova_domain *iovad,
99 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
101 struct iova_fq __percpu *queue;
104 atomic64_set(&iovad->fq_flush_start_cnt, 0);
105 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
107 queue = alloc_percpu(struct iova_fq);
111 iovad->flush_cb = flush_cb;
112 iovad->entry_dtor = entry_dtor;
114 for_each_possible_cpu(cpu) {
117 fq = per_cpu_ptr(queue, cpu);
121 spin_lock_init(&fq->lock);
126 timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
127 atomic_set(&iovad->fq_timer_on, 0);
132 static struct rb_node *
133 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
135 if (limit_pfn <= iovad->dma_32bit_pfn)
136 return iovad->cached32_node;
138 return iovad->cached_node;
142 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
144 if (new->pfn_hi < iovad->dma_32bit_pfn)
145 iovad->cached32_node = &new->node;
147 iovad->cached_node = &new->node;
151 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
153 struct iova *cached_iova;
155 cached_iova = to_iova(iovad->cached32_node);
156 if (free == cached_iova ||
157 (free->pfn_hi < iovad->dma_32bit_pfn &&
158 free->pfn_lo >= cached_iova->pfn_lo)) {
159 iovad->cached32_node = rb_next(&free->node);
160 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
163 cached_iova = to_iova(iovad->cached_node);
164 if (free->pfn_lo >= cached_iova->pfn_lo)
165 iovad->cached_node = rb_next(&free->node);
168 static struct rb_node *iova_find_limit(struct iova_domain *iovad, unsigned long limit_pfn)
170 struct rb_node *node, *next;
172 * Ideally what we'd like to judge here is whether limit_pfn is close
173 * enough to the highest-allocated IOVA that starting the allocation
174 * walk from the anchor node will be quicker than this initial work to
175 * find an exact starting point (especially if that ends up being the
176 * anchor node anyway). This is an incredibly crude approximation which
177 * only really helps the most likely case, but is at least trivially easy.
179 if (limit_pfn > iovad->dma_32bit_pfn)
180 return &iovad->anchor.node;
182 node = iovad->rbroot.rb_node;
183 while (to_iova(node)->pfn_hi < limit_pfn)
184 node = node->rb_right;
187 while (node->rb_left && to_iova(node->rb_left)->pfn_lo >= limit_pfn)
188 node = node->rb_left;
193 next = node->rb_left;
194 while (next->rb_right) {
195 next = next->rb_right;
196 if (to_iova(next)->pfn_lo >= limit_pfn) {
205 /* Insert the iova into domain rbtree by holding writer lock */
207 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
208 struct rb_node *start)
210 struct rb_node **new, *parent = NULL;
212 new = (start) ? &start : &(root->rb_node);
213 /* Figure out where to put new node */
215 struct iova *this = to_iova(*new);
219 if (iova->pfn_lo < this->pfn_lo)
220 new = &((*new)->rb_left);
221 else if (iova->pfn_lo > this->pfn_lo)
222 new = &((*new)->rb_right);
224 WARN_ON(1); /* this should not happen */
228 /* Add new node and rebalance tree. */
229 rb_link_node(&iova->node, parent, new);
230 rb_insert_color(&iova->node, root);
233 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
234 unsigned long size, unsigned long limit_pfn,
235 struct iova *new, bool size_aligned)
237 struct rb_node *curr, *prev;
238 struct iova *curr_iova;
240 unsigned long new_pfn, retry_pfn;
241 unsigned long align_mask = ~0UL;
242 unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
245 align_mask <<= fls_long(size - 1);
247 /* Walk the tree backwards */
248 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
249 if (limit_pfn <= iovad->dma_32bit_pfn &&
250 size >= iovad->max32_alloc_size)
253 curr = __get_cached_rbnode(iovad, limit_pfn);
254 curr_iova = to_iova(curr);
255 retry_pfn = curr_iova->pfn_hi + 1;
259 high_pfn = min(high_pfn, curr_iova->pfn_lo);
260 new_pfn = (high_pfn - size) & align_mask;
262 curr = rb_prev(curr);
263 curr_iova = to_iova(curr);
264 } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
266 if (high_pfn < size || new_pfn < low_pfn) {
267 if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
268 high_pfn = limit_pfn;
270 curr = iova_find_limit(iovad, limit_pfn);
271 curr_iova = to_iova(curr);
274 iovad->max32_alloc_size = size;
278 /* pfn_lo will point to size aligned address if size_aligned is set */
279 new->pfn_lo = new_pfn;
280 new->pfn_hi = new->pfn_lo + size - 1;
282 /* If we have 'prev', it's a valid place to start the insertion. */
283 iova_insert_rbtree(&iovad->rbroot, new, prev);
284 __cached_rbnode_insert_update(iovad, new);
286 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
290 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
294 static struct kmem_cache *iova_cache;
295 static unsigned int iova_cache_users;
296 static DEFINE_MUTEX(iova_cache_mutex);
298 static struct iova *alloc_iova_mem(void)
300 return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
303 static void free_iova_mem(struct iova *iova)
305 if (iova->pfn_lo != IOVA_ANCHOR)
306 kmem_cache_free(iova_cache, iova);
309 int iova_cache_get(void)
311 mutex_lock(&iova_cache_mutex);
312 if (!iova_cache_users) {
315 ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
318 mutex_unlock(&iova_cache_mutex);
319 pr_err("Couldn't register cpuhp handler\n");
323 iova_cache = kmem_cache_create(
324 "iommu_iova", sizeof(struct iova), 0,
325 SLAB_HWCACHE_ALIGN, NULL);
327 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
328 mutex_unlock(&iova_cache_mutex);
329 pr_err("Couldn't create iova cache\n");
335 mutex_unlock(&iova_cache_mutex);
339 EXPORT_SYMBOL_GPL(iova_cache_get);
341 void iova_cache_put(void)
343 mutex_lock(&iova_cache_mutex);
344 if (WARN_ON(!iova_cache_users)) {
345 mutex_unlock(&iova_cache_mutex);
349 if (!iova_cache_users) {
350 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
351 kmem_cache_destroy(iova_cache);
353 mutex_unlock(&iova_cache_mutex);
355 EXPORT_SYMBOL_GPL(iova_cache_put);
358 * alloc_iova - allocates an iova
359 * @iovad: - iova domain in question
360 * @size: - size of page frames to allocate
361 * @limit_pfn: - max limit address
362 * @size_aligned: - set if size_aligned address range is required
363 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
364 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
365 * flag is set then the allocated address iova->pfn_lo will be naturally
366 * aligned on roundup_power_of_two(size).
369 alloc_iova(struct iova_domain *iovad, unsigned long size,
370 unsigned long limit_pfn,
373 struct iova *new_iova;
376 new_iova = alloc_iova_mem();
380 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
381 new_iova, size_aligned);
384 free_iova_mem(new_iova);
390 EXPORT_SYMBOL_GPL(alloc_iova);
393 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
395 struct rb_node *node = iovad->rbroot.rb_node;
397 assert_spin_locked(&iovad->iova_rbtree_lock);
400 struct iova *iova = to_iova(node);
402 if (pfn < iova->pfn_lo)
403 node = node->rb_left;
404 else if (pfn > iova->pfn_hi)
405 node = node->rb_right;
407 return iova; /* pfn falls within iova's range */
413 static void remove_iova(struct iova_domain *iovad, struct iova *iova)
415 assert_spin_locked(&iovad->iova_rbtree_lock);
416 __cached_rbnode_delete_update(iovad, iova);
417 rb_erase(&iova->node, &iovad->rbroot);
421 * find_iova - finds an iova for a given pfn
422 * @iovad: - iova domain in question.
423 * @pfn: - page frame number
424 * This function finds and returns an iova belonging to the
425 * given domain which matches the given pfn.
427 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
432 /* Take the lock so that no other thread is manipulating the rbtree */
433 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
434 iova = private_find_iova(iovad, pfn);
435 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
438 EXPORT_SYMBOL_GPL(find_iova);
441 * __free_iova - frees the given iova
442 * @iovad: iova domain in question.
443 * @iova: iova in question.
444 * Frees the given iova belonging to the giving domain
447 __free_iova(struct iova_domain *iovad, struct iova *iova)
451 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
452 remove_iova(iovad, iova);
453 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
456 EXPORT_SYMBOL_GPL(__free_iova);
459 * free_iova - finds and frees the iova for a given pfn
460 * @iovad: - iova domain in question.
461 * @pfn: - pfn that is allocated previously
462 * This functions finds an iova for a given pfn and then
463 * frees the iova from that domain.
466 free_iova(struct iova_domain *iovad, unsigned long pfn)
471 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
472 iova = private_find_iova(iovad, pfn);
474 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
477 remove_iova(iovad, iova);
478 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
481 EXPORT_SYMBOL_GPL(free_iova);
484 * alloc_iova_fast - allocates an iova from rcache
485 * @iovad: - iova domain in question
486 * @size: - size of page frames to allocate
487 * @limit_pfn: - max limit address
488 * @flush_rcache: - set to flush rcache on regular allocation failure
489 * This function tries to satisfy an iova allocation from the rcache,
490 * and falls back to regular allocation on failure. If regular allocation
491 * fails too and the flush_rcache flag is set then the rcache will be flushed.
494 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
495 unsigned long limit_pfn, bool flush_rcache)
497 unsigned long iova_pfn;
498 struct iova *new_iova;
500 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
505 new_iova = alloc_iova(iovad, size, limit_pfn, true);
512 /* Try replenishing IOVAs by flushing rcache. */
513 flush_rcache = false;
514 for_each_online_cpu(cpu)
515 free_cpu_cached_iovas(cpu, iovad);
516 free_global_cached_iovas(iovad);
520 return new_iova->pfn_lo;
522 EXPORT_SYMBOL_GPL(alloc_iova_fast);
525 * free_iova_fast - free iova pfn range into rcache
526 * @iovad: - iova domain in question.
527 * @pfn: - pfn that is allocated previously
528 * @size: - # of pages in range
529 * This functions frees an iova range by trying to put it into the rcache,
530 * falling back to regular iova deallocation via free_iova() if this fails.
533 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
535 if (iova_rcache_insert(iovad, pfn, size))
538 free_iova(iovad, pfn);
540 EXPORT_SYMBOL_GPL(free_iova_fast);
542 #define fq_ring_for_each(i, fq) \
543 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
545 static inline bool fq_full(struct iova_fq *fq)
547 assert_spin_locked(&fq->lock);
548 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
551 static inline unsigned fq_ring_add(struct iova_fq *fq)
553 unsigned idx = fq->tail;
555 assert_spin_locked(&fq->lock);
557 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
562 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
564 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
567 assert_spin_locked(&fq->lock);
569 fq_ring_for_each(idx, fq) {
571 if (fq->entries[idx].counter >= counter)
574 if (iovad->entry_dtor)
575 iovad->entry_dtor(fq->entries[idx].data);
577 free_iova_fast(iovad,
578 fq->entries[idx].iova_pfn,
579 fq->entries[idx].pages);
581 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
585 static void iova_domain_flush(struct iova_domain *iovad)
587 atomic64_inc(&iovad->fq_flush_start_cnt);
588 iovad->flush_cb(iovad);
589 atomic64_inc(&iovad->fq_flush_finish_cnt);
592 static void fq_destroy_all_entries(struct iova_domain *iovad)
597 * This code runs when the iova_domain is being detroyed, so don't
598 * bother to free iovas, just call the entry_dtor on all remaining
601 if (!iovad->entry_dtor)
604 for_each_possible_cpu(cpu) {
605 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
608 fq_ring_for_each(idx, fq)
609 iovad->entry_dtor(fq->entries[idx].data);
613 static void fq_flush_timeout(struct timer_list *t)
615 struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
618 atomic_set(&iovad->fq_timer_on, 0);
619 iova_domain_flush(iovad);
621 for_each_possible_cpu(cpu) {
625 fq = per_cpu_ptr(iovad->fq, cpu);
626 spin_lock_irqsave(&fq->lock, flags);
627 fq_ring_free(iovad, fq);
628 spin_unlock_irqrestore(&fq->lock, flags);
632 void queue_iova(struct iova_domain *iovad,
633 unsigned long pfn, unsigned long pages,
641 * Order against the IOMMU driver's pagetable update from unmapping
642 * @pte, to guarantee that iova_domain_flush() observes that if called
643 * from a different CPU before we release the lock below. Full barrier
644 * so it also pairs with iommu_dma_init_fq() to avoid seeing partially
645 * written fq state here.
649 fq = raw_cpu_ptr(iovad->fq);
650 spin_lock_irqsave(&fq->lock, flags);
653 * First remove all entries from the flush queue that have already been
654 * flushed out on another CPU. This makes the fq_full() check below less
657 fq_ring_free(iovad, fq);
660 iova_domain_flush(iovad);
661 fq_ring_free(iovad, fq);
664 idx = fq_ring_add(fq);
666 fq->entries[idx].iova_pfn = pfn;
667 fq->entries[idx].pages = pages;
668 fq->entries[idx].data = data;
669 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
671 spin_unlock_irqrestore(&fq->lock, flags);
673 /* Avoid false sharing as much as possible. */
674 if (!atomic_read(&iovad->fq_timer_on) &&
675 !atomic_xchg(&iovad->fq_timer_on, 1))
676 mod_timer(&iovad->fq_timer,
677 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
681 * put_iova_domain - destroys the iova domain
682 * @iovad: - iova domain in question.
683 * All the iova's in that domain are destroyed.
685 void put_iova_domain(struct iova_domain *iovad)
687 struct iova *iova, *tmp;
689 cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
692 free_iova_flush_queue(iovad);
693 free_iova_rcaches(iovad);
694 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
697 EXPORT_SYMBOL_GPL(put_iova_domain);
700 __is_range_overlap(struct rb_node *node,
701 unsigned long pfn_lo, unsigned long pfn_hi)
703 struct iova *iova = to_iova(node);
705 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
710 static inline struct iova *
711 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
715 iova = alloc_iova_mem();
717 iova->pfn_lo = pfn_lo;
718 iova->pfn_hi = pfn_hi;
725 __insert_new_range(struct iova_domain *iovad,
726 unsigned long pfn_lo, unsigned long pfn_hi)
730 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
732 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
738 __adjust_overlap_range(struct iova *iova,
739 unsigned long *pfn_lo, unsigned long *pfn_hi)
741 if (*pfn_lo < iova->pfn_lo)
742 iova->pfn_lo = *pfn_lo;
743 if (*pfn_hi > iova->pfn_hi)
744 *pfn_lo = iova->pfn_hi + 1;
748 * reserve_iova - reserves an iova in the given range
749 * @iovad: - iova domain pointer
750 * @pfn_lo: - lower page frame address
751 * @pfn_hi:- higher pfn adderss
752 * This function allocates reserves the address range from pfn_lo to pfn_hi so
753 * that this address is not dished out as part of alloc_iova.
756 reserve_iova(struct iova_domain *iovad,
757 unsigned long pfn_lo, unsigned long pfn_hi)
759 struct rb_node *node;
762 unsigned int overlap = 0;
764 /* Don't allow nonsensical pfns */
765 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
768 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
769 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
770 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
771 iova = to_iova(node);
772 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
773 if ((pfn_lo >= iova->pfn_lo) &&
774 (pfn_hi <= iova->pfn_hi))
782 /* We are here either because this is the first reserver node
783 * or need to insert remaining non overlap addr range
785 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
788 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
791 EXPORT_SYMBOL_GPL(reserve_iova);
794 * Magazine caches for IOVA ranges. For an introduction to magazines,
795 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
796 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
797 * For simplicity, we use a static magazine size and don't implement the
798 * dynamic size tuning described in the paper.
801 #define IOVA_MAG_SIZE 128
803 struct iova_magazine {
805 unsigned long pfns[IOVA_MAG_SIZE];
808 struct iova_cpu_rcache {
810 struct iova_magazine *loaded;
811 struct iova_magazine *prev;
814 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
816 return kzalloc(sizeof(struct iova_magazine), flags);
819 static void iova_magazine_free(struct iova_magazine *mag)
825 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
833 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
835 for (i = 0 ; i < mag->size; ++i) {
836 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
841 remove_iova(iovad, iova);
845 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
850 static bool iova_magazine_full(struct iova_magazine *mag)
852 return (mag && mag->size == IOVA_MAG_SIZE);
855 static bool iova_magazine_empty(struct iova_magazine *mag)
857 return (!mag || mag->size == 0);
860 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
861 unsigned long limit_pfn)
866 BUG_ON(iova_magazine_empty(mag));
868 /* Only fall back to the rbtree if we have no suitable pfns at all */
869 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
873 /* Swap it to pop it */
875 mag->pfns[i] = mag->pfns[--mag->size];
880 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
882 BUG_ON(iova_magazine_full(mag));
884 mag->pfns[mag->size++] = pfn;
887 static void init_iova_rcaches(struct iova_domain *iovad)
889 struct iova_cpu_rcache *cpu_rcache;
890 struct iova_rcache *rcache;
894 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
895 rcache = &iovad->rcaches[i];
896 spin_lock_init(&rcache->lock);
897 rcache->depot_size = 0;
898 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
899 if (WARN_ON(!rcache->cpu_rcaches))
901 for_each_possible_cpu(cpu) {
902 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
903 spin_lock_init(&cpu_rcache->lock);
904 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
905 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
911 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
912 * return true on success. Can fail if rcache is full and we can't free
913 * space, and free_iova() (our only caller) will then return the IOVA
914 * range to the rbtree instead.
916 static bool __iova_rcache_insert(struct iova_domain *iovad,
917 struct iova_rcache *rcache,
918 unsigned long iova_pfn)
920 struct iova_magazine *mag_to_free = NULL;
921 struct iova_cpu_rcache *cpu_rcache;
922 bool can_insert = false;
925 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
926 spin_lock_irqsave(&cpu_rcache->lock, flags);
928 if (!iova_magazine_full(cpu_rcache->loaded)) {
930 } else if (!iova_magazine_full(cpu_rcache->prev)) {
931 swap(cpu_rcache->prev, cpu_rcache->loaded);
934 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
937 spin_lock(&rcache->lock);
938 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
939 rcache->depot[rcache->depot_size++] =
942 mag_to_free = cpu_rcache->loaded;
944 spin_unlock(&rcache->lock);
946 cpu_rcache->loaded = new_mag;
952 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
954 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
957 iova_magazine_free_pfns(mag_to_free, iovad);
958 iova_magazine_free(mag_to_free);
964 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
967 unsigned int log_size = order_base_2(size);
969 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
972 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
976 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
977 * satisfy the request, return a matching non-NULL range and remove
978 * it from the 'rcache'.
980 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
981 unsigned long limit_pfn)
983 struct iova_cpu_rcache *cpu_rcache;
984 unsigned long iova_pfn = 0;
985 bool has_pfn = false;
988 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
989 spin_lock_irqsave(&cpu_rcache->lock, flags);
991 if (!iova_magazine_empty(cpu_rcache->loaded)) {
993 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
994 swap(cpu_rcache->prev, cpu_rcache->loaded);
997 spin_lock(&rcache->lock);
998 if (rcache->depot_size > 0) {
999 iova_magazine_free(cpu_rcache->loaded);
1000 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
1003 spin_unlock(&rcache->lock);
1007 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
1009 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1015 * Try to satisfy IOVA allocation range from rcache. Fail if requested
1016 * size is too big or the DMA limit we are given isn't satisfied by the
1017 * top element in the magazine.
1019 static unsigned long iova_rcache_get(struct iova_domain *iovad,
1021 unsigned long limit_pfn)
1023 unsigned int log_size = order_base_2(size);
1025 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
1028 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
1032 * free rcache data structures.
1034 static void free_iova_rcaches(struct iova_domain *iovad)
1036 struct iova_rcache *rcache;
1037 struct iova_cpu_rcache *cpu_rcache;
1041 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1042 rcache = &iovad->rcaches[i];
1043 for_each_possible_cpu(cpu) {
1044 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1045 iova_magazine_free(cpu_rcache->loaded);
1046 iova_magazine_free(cpu_rcache->prev);
1048 free_percpu(rcache->cpu_rcaches);
1049 for (j = 0; j < rcache->depot_size; ++j)
1050 iova_magazine_free(rcache->depot[j]);
1055 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1057 static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1059 struct iova_cpu_rcache *cpu_rcache;
1060 struct iova_rcache *rcache;
1061 unsigned long flags;
1064 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1065 rcache = &iovad->rcaches[i];
1066 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1067 spin_lock_irqsave(&cpu_rcache->lock, flags);
1068 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1069 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1070 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1075 * free all the IOVA ranges of global cache
1077 static void free_global_cached_iovas(struct iova_domain *iovad)
1079 struct iova_rcache *rcache;
1080 unsigned long flags;
1083 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1084 rcache = &iovad->rcaches[i];
1085 spin_lock_irqsave(&rcache->lock, flags);
1086 for (j = 0; j < rcache->depot_size; ++j) {
1087 iova_magazine_free_pfns(rcache->depot[j], iovad);
1088 iova_magazine_free(rcache->depot[j]);
1090 rcache->depot_size = 0;
1091 spin_unlock_irqrestore(&rcache->lock, flags);
1094 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1095 MODULE_LICENSE("GPL");