1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2009, Intel Corporation.
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);
128 timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
129 atomic_set(&iovad->fq_timer_on, 0);
134 static struct rb_node *
135 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
137 if (limit_pfn <= iovad->dma_32bit_pfn)
138 return iovad->cached32_node;
140 return iovad->cached_node;
144 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
146 if (new->pfn_hi < iovad->dma_32bit_pfn)
147 iovad->cached32_node = &new->node;
149 iovad->cached_node = &new->node;
153 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
155 struct iova *cached_iova;
157 cached_iova = to_iova(iovad->cached32_node);
158 if (free == cached_iova ||
159 (free->pfn_hi < iovad->dma_32bit_pfn &&
160 free->pfn_lo >= cached_iova->pfn_lo)) {
161 iovad->cached32_node = rb_next(&free->node);
162 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
165 cached_iova = to_iova(iovad->cached_node);
166 if (free->pfn_lo >= cached_iova->pfn_lo)
167 iovad->cached_node = rb_next(&free->node);
170 static struct rb_node *iova_find_limit(struct iova_domain *iovad, unsigned long limit_pfn)
172 struct rb_node *node, *next;
174 * Ideally what we'd like to judge here is whether limit_pfn is close
175 * enough to the highest-allocated IOVA that starting the allocation
176 * walk from the anchor node will be quicker than this initial work to
177 * find an exact starting point (especially if that ends up being the
178 * anchor node anyway). This is an incredibly crude approximation which
179 * only really helps the most likely case, but is at least trivially easy.
181 if (limit_pfn > iovad->dma_32bit_pfn)
182 return &iovad->anchor.node;
184 node = iovad->rbroot.rb_node;
185 while (to_iova(node)->pfn_hi < limit_pfn)
186 node = node->rb_right;
189 while (node->rb_left && to_iova(node->rb_left)->pfn_lo >= limit_pfn)
190 node = node->rb_left;
195 next = node->rb_left;
196 while (next->rb_right) {
197 next = next->rb_right;
198 if (to_iova(next)->pfn_lo >= limit_pfn) {
207 /* Insert the iova into domain rbtree by holding writer lock */
209 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
210 struct rb_node *start)
212 struct rb_node **new, *parent = NULL;
214 new = (start) ? &start : &(root->rb_node);
215 /* Figure out where to put new node */
217 struct iova *this = to_iova(*new);
221 if (iova->pfn_lo < this->pfn_lo)
222 new = &((*new)->rb_left);
223 else if (iova->pfn_lo > this->pfn_lo)
224 new = &((*new)->rb_right);
226 WARN_ON(1); /* this should not happen */
230 /* Add new node and rebalance tree. */
231 rb_link_node(&iova->node, parent, new);
232 rb_insert_color(&iova->node, root);
235 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
236 unsigned long size, unsigned long limit_pfn,
237 struct iova *new, bool size_aligned)
239 struct rb_node *curr, *prev;
240 struct iova *curr_iova;
242 unsigned long new_pfn, retry_pfn;
243 unsigned long align_mask = ~0UL;
244 unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
247 align_mask <<= fls_long(size - 1);
249 /* Walk the tree backwards */
250 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
251 if (limit_pfn <= iovad->dma_32bit_pfn &&
252 size >= iovad->max32_alloc_size)
255 curr = __get_cached_rbnode(iovad, limit_pfn);
256 curr_iova = to_iova(curr);
257 retry_pfn = curr_iova->pfn_hi + 1;
261 high_pfn = min(high_pfn, curr_iova->pfn_lo);
262 new_pfn = (high_pfn - size) & align_mask;
264 curr = rb_prev(curr);
265 curr_iova = to_iova(curr);
266 } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
268 if (high_pfn < size || new_pfn < low_pfn) {
269 if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
270 high_pfn = limit_pfn;
272 curr = iova_find_limit(iovad, limit_pfn);
273 curr_iova = to_iova(curr);
276 iovad->max32_alloc_size = size;
280 /* pfn_lo will point to size aligned address if size_aligned is set */
281 new->pfn_lo = new_pfn;
282 new->pfn_hi = new->pfn_lo + size - 1;
284 /* If we have 'prev', it's a valid place to start the insertion. */
285 iova_insert_rbtree(&iovad->rbroot, new, prev);
286 __cached_rbnode_insert_update(iovad, new);
288 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
292 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
296 static struct kmem_cache *iova_cache;
297 static unsigned int iova_cache_users;
298 static DEFINE_MUTEX(iova_cache_mutex);
300 static struct iova *alloc_iova_mem(void)
302 return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
305 static void free_iova_mem(struct iova *iova)
307 if (iova->pfn_lo != IOVA_ANCHOR)
308 kmem_cache_free(iova_cache, iova);
311 int iova_cache_get(void)
313 mutex_lock(&iova_cache_mutex);
314 if (!iova_cache_users) {
317 ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
320 mutex_unlock(&iova_cache_mutex);
321 pr_err("Couldn't register cpuhp handler\n");
325 iova_cache = kmem_cache_create(
326 "iommu_iova", sizeof(struct iova), 0,
327 SLAB_HWCACHE_ALIGN, NULL);
329 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
330 mutex_unlock(&iova_cache_mutex);
331 pr_err("Couldn't create iova cache\n");
337 mutex_unlock(&iova_cache_mutex);
341 EXPORT_SYMBOL_GPL(iova_cache_get);
343 void iova_cache_put(void)
345 mutex_lock(&iova_cache_mutex);
346 if (WARN_ON(!iova_cache_users)) {
347 mutex_unlock(&iova_cache_mutex);
351 if (!iova_cache_users) {
352 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
353 kmem_cache_destroy(iova_cache);
355 mutex_unlock(&iova_cache_mutex);
357 EXPORT_SYMBOL_GPL(iova_cache_put);
360 * alloc_iova - allocates an iova
361 * @iovad: - iova domain in question
362 * @size: - size of page frames to allocate
363 * @limit_pfn: - max limit address
364 * @size_aligned: - set if size_aligned address range is required
365 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
366 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
367 * flag is set then the allocated address iova->pfn_lo will be naturally
368 * aligned on roundup_power_of_two(size).
371 alloc_iova(struct iova_domain *iovad, unsigned long size,
372 unsigned long limit_pfn,
375 struct iova *new_iova;
378 new_iova = alloc_iova_mem();
382 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
383 new_iova, size_aligned);
386 free_iova_mem(new_iova);
392 EXPORT_SYMBOL_GPL(alloc_iova);
395 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
397 struct rb_node *node = iovad->rbroot.rb_node;
399 assert_spin_locked(&iovad->iova_rbtree_lock);
402 struct iova *iova = to_iova(node);
404 if (pfn < iova->pfn_lo)
405 node = node->rb_left;
406 else if (pfn > iova->pfn_hi)
407 node = node->rb_right;
409 return iova; /* pfn falls within iova's range */
415 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
417 assert_spin_locked(&iovad->iova_rbtree_lock);
418 __cached_rbnode_delete_update(iovad, iova);
419 rb_erase(&iova->node, &iovad->rbroot);
424 * find_iova - finds an iova for a given pfn
425 * @iovad: - iova domain in question.
426 * @pfn: - page frame number
427 * This function finds and returns an iova belonging to the
428 * given domain which matches the given pfn.
430 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
435 /* Take the lock so that no other thread is manipulating the rbtree */
436 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
437 iova = private_find_iova(iovad, pfn);
438 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
441 EXPORT_SYMBOL_GPL(find_iova);
444 * __free_iova - frees the given iova
445 * @iovad: iova domain in question.
446 * @iova: iova in question.
447 * Frees the given iova belonging to the giving domain
450 __free_iova(struct iova_domain *iovad, struct iova *iova)
454 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
455 private_free_iova(iovad, iova);
456 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
458 EXPORT_SYMBOL_GPL(__free_iova);
461 * free_iova - finds and frees the iova for a given pfn
462 * @iovad: - iova domain in question.
463 * @pfn: - pfn that is allocated previously
464 * This functions finds an iova for a given pfn and then
465 * frees the iova from that domain.
468 free_iova(struct iova_domain *iovad, unsigned long pfn)
473 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
474 iova = private_find_iova(iovad, pfn);
476 private_free_iova(iovad, iova);
477 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
480 EXPORT_SYMBOL_GPL(free_iova);
483 * alloc_iova_fast - allocates an iova from rcache
484 * @iovad: - iova domain in question
485 * @size: - size of page frames to allocate
486 * @limit_pfn: - max limit address
487 * @flush_rcache: - set to flush rcache on regular allocation failure
488 * This function tries to satisfy an iova allocation from the rcache,
489 * and falls back to regular allocation on failure. If regular allocation
490 * fails too and the flush_rcache flag is set then the rcache will be flushed.
493 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
494 unsigned long limit_pfn, bool flush_rcache)
496 unsigned long iova_pfn;
497 struct iova *new_iova;
499 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
504 new_iova = alloc_iova(iovad, size, limit_pfn, true);
511 /* Try replenishing IOVAs by flushing rcache. */
512 flush_rcache = false;
513 for_each_online_cpu(cpu)
514 free_cpu_cached_iovas(cpu, iovad);
515 free_global_cached_iovas(iovad);
519 return new_iova->pfn_lo;
523 * free_iova_fast - free iova pfn range into rcache
524 * @iovad: - iova domain in question.
525 * @pfn: - pfn that is allocated previously
526 * @size: - # of pages in range
527 * This functions frees an iova range by trying to put it into the rcache,
528 * falling back to regular iova deallocation via free_iova() if this fails.
531 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
533 if (iova_rcache_insert(iovad, pfn, size))
536 free_iova(iovad, pfn);
539 #define fq_ring_for_each(i, fq) \
540 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
542 static inline bool fq_full(struct iova_fq *fq)
544 assert_spin_locked(&fq->lock);
545 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
548 static inline unsigned fq_ring_add(struct iova_fq *fq)
550 unsigned idx = fq->tail;
552 assert_spin_locked(&fq->lock);
554 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
559 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
561 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
564 assert_spin_locked(&fq->lock);
566 fq_ring_for_each(idx, fq) {
568 if (fq->entries[idx].counter >= counter)
571 if (iovad->entry_dtor)
572 iovad->entry_dtor(fq->entries[idx].data);
574 free_iova_fast(iovad,
575 fq->entries[idx].iova_pfn,
576 fq->entries[idx].pages);
578 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
582 static void iova_domain_flush(struct iova_domain *iovad)
584 atomic64_inc(&iovad->fq_flush_start_cnt);
585 iovad->flush_cb(iovad);
586 atomic64_inc(&iovad->fq_flush_finish_cnt);
589 static void fq_destroy_all_entries(struct iova_domain *iovad)
594 * This code runs when the iova_domain is being detroyed, so don't
595 * bother to free iovas, just call the entry_dtor on all remaining
598 if (!iovad->entry_dtor)
601 for_each_possible_cpu(cpu) {
602 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
605 fq_ring_for_each(idx, fq)
606 iovad->entry_dtor(fq->entries[idx].data);
610 static void fq_flush_timeout(struct timer_list *t)
612 struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
615 atomic_set(&iovad->fq_timer_on, 0);
616 iova_domain_flush(iovad);
618 for_each_possible_cpu(cpu) {
622 fq = per_cpu_ptr(iovad->fq, cpu);
623 spin_lock_irqsave(&fq->lock, flags);
624 fq_ring_free(iovad, fq);
625 spin_unlock_irqrestore(&fq->lock, flags);
629 void queue_iova(struct iova_domain *iovad,
630 unsigned long pfn, unsigned long pages,
633 struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
637 spin_lock_irqsave(&fq->lock, flags);
640 * First remove all entries from the flush queue that have already been
641 * flushed out on another CPU. This makes the fq_full() check below less
644 fq_ring_free(iovad, fq);
647 iova_domain_flush(iovad);
648 fq_ring_free(iovad, fq);
651 idx = fq_ring_add(fq);
653 fq->entries[idx].iova_pfn = pfn;
654 fq->entries[idx].pages = pages;
655 fq->entries[idx].data = data;
656 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
658 spin_unlock_irqrestore(&fq->lock, flags);
660 /* Avoid false sharing as much as possible. */
661 if (!atomic_read(&iovad->fq_timer_on) &&
662 !atomic_xchg(&iovad->fq_timer_on, 1))
663 mod_timer(&iovad->fq_timer,
664 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
668 * put_iova_domain - destroys the iova domain
669 * @iovad: - iova domain in question.
670 * All the iova's in that domain are destroyed.
672 void put_iova_domain(struct iova_domain *iovad)
674 struct iova *iova, *tmp;
676 cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
679 free_iova_flush_queue(iovad);
680 free_iova_rcaches(iovad);
681 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
684 EXPORT_SYMBOL_GPL(put_iova_domain);
687 __is_range_overlap(struct rb_node *node,
688 unsigned long pfn_lo, unsigned long pfn_hi)
690 struct iova *iova = to_iova(node);
692 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
697 static inline struct iova *
698 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
702 iova = alloc_iova_mem();
704 iova->pfn_lo = pfn_lo;
705 iova->pfn_hi = pfn_hi;
712 __insert_new_range(struct iova_domain *iovad,
713 unsigned long pfn_lo, unsigned long pfn_hi)
717 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
719 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
725 __adjust_overlap_range(struct iova *iova,
726 unsigned long *pfn_lo, unsigned long *pfn_hi)
728 if (*pfn_lo < iova->pfn_lo)
729 iova->pfn_lo = *pfn_lo;
730 if (*pfn_hi > iova->pfn_hi)
731 *pfn_lo = iova->pfn_hi + 1;
735 * reserve_iova - reserves an iova in the given range
736 * @iovad: - iova domain pointer
737 * @pfn_lo: - lower page frame address
738 * @pfn_hi:- higher pfn adderss
739 * This function allocates reserves the address range from pfn_lo to pfn_hi so
740 * that this address is not dished out as part of alloc_iova.
743 reserve_iova(struct iova_domain *iovad,
744 unsigned long pfn_lo, unsigned long pfn_hi)
746 struct rb_node *node;
749 unsigned int overlap = 0;
751 /* Don't allow nonsensical pfns */
752 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
755 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
756 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
757 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
758 iova = to_iova(node);
759 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
760 if ((pfn_lo >= iova->pfn_lo) &&
761 (pfn_hi <= iova->pfn_hi))
769 /* We are here either because this is the first reserver node
770 * or need to insert remaining non overlap addr range
772 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
775 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
778 EXPORT_SYMBOL_GPL(reserve_iova);
781 * Magazine caches for IOVA ranges. For an introduction to magazines,
782 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
783 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
784 * For simplicity, we use a static magazine size and don't implement the
785 * dynamic size tuning described in the paper.
788 #define IOVA_MAG_SIZE 128
790 struct iova_magazine {
792 unsigned long pfns[IOVA_MAG_SIZE];
795 struct iova_cpu_rcache {
797 struct iova_magazine *loaded;
798 struct iova_magazine *prev;
801 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
803 return kzalloc(sizeof(struct iova_magazine), flags);
806 static void iova_magazine_free(struct iova_magazine *mag)
812 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
820 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
822 for (i = 0 ; i < mag->size; ++i) {
823 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
828 private_free_iova(iovad, iova);
831 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
836 static bool iova_magazine_full(struct iova_magazine *mag)
838 return (mag && mag->size == IOVA_MAG_SIZE);
841 static bool iova_magazine_empty(struct iova_magazine *mag)
843 return (!mag || mag->size == 0);
846 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
847 unsigned long limit_pfn)
852 BUG_ON(iova_magazine_empty(mag));
854 /* Only fall back to the rbtree if we have no suitable pfns at all */
855 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
859 /* Swap it to pop it */
861 mag->pfns[i] = mag->pfns[--mag->size];
866 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
868 BUG_ON(iova_magazine_full(mag));
870 mag->pfns[mag->size++] = pfn;
873 static void init_iova_rcaches(struct iova_domain *iovad)
875 struct iova_cpu_rcache *cpu_rcache;
876 struct iova_rcache *rcache;
880 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
881 rcache = &iovad->rcaches[i];
882 spin_lock_init(&rcache->lock);
883 rcache->depot_size = 0;
884 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
885 if (WARN_ON(!rcache->cpu_rcaches))
887 for_each_possible_cpu(cpu) {
888 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
889 spin_lock_init(&cpu_rcache->lock);
890 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
891 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
897 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
898 * return true on success. Can fail if rcache is full and we can't free
899 * space, and free_iova() (our only caller) will then return the IOVA
900 * range to the rbtree instead.
902 static bool __iova_rcache_insert(struct iova_domain *iovad,
903 struct iova_rcache *rcache,
904 unsigned long iova_pfn)
906 struct iova_magazine *mag_to_free = NULL;
907 struct iova_cpu_rcache *cpu_rcache;
908 bool can_insert = false;
911 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
912 spin_lock_irqsave(&cpu_rcache->lock, flags);
914 if (!iova_magazine_full(cpu_rcache->loaded)) {
916 } else if (!iova_magazine_full(cpu_rcache->prev)) {
917 swap(cpu_rcache->prev, cpu_rcache->loaded);
920 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
923 spin_lock(&rcache->lock);
924 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
925 rcache->depot[rcache->depot_size++] =
928 mag_to_free = cpu_rcache->loaded;
930 spin_unlock(&rcache->lock);
932 cpu_rcache->loaded = new_mag;
938 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
940 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
943 iova_magazine_free_pfns(mag_to_free, iovad);
944 iova_magazine_free(mag_to_free);
950 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
953 unsigned int log_size = order_base_2(size);
955 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
958 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
962 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
963 * satisfy the request, return a matching non-NULL range and remove
964 * it from the 'rcache'.
966 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
967 unsigned long limit_pfn)
969 struct iova_cpu_rcache *cpu_rcache;
970 unsigned long iova_pfn = 0;
971 bool has_pfn = false;
974 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
975 spin_lock_irqsave(&cpu_rcache->lock, flags);
977 if (!iova_magazine_empty(cpu_rcache->loaded)) {
979 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
980 swap(cpu_rcache->prev, cpu_rcache->loaded);
983 spin_lock(&rcache->lock);
984 if (rcache->depot_size > 0) {
985 iova_magazine_free(cpu_rcache->loaded);
986 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
989 spin_unlock(&rcache->lock);
993 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
995 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1001 * Try to satisfy IOVA allocation range from rcache. Fail if requested
1002 * size is too big or the DMA limit we are given isn't satisfied by the
1003 * top element in the magazine.
1005 static unsigned long iova_rcache_get(struct iova_domain *iovad,
1007 unsigned long limit_pfn)
1009 unsigned int log_size = order_base_2(size);
1011 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
1014 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
1018 * free rcache data structures.
1020 static void free_iova_rcaches(struct iova_domain *iovad)
1022 struct iova_rcache *rcache;
1023 struct iova_cpu_rcache *cpu_rcache;
1027 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1028 rcache = &iovad->rcaches[i];
1029 for_each_possible_cpu(cpu) {
1030 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1031 iova_magazine_free(cpu_rcache->loaded);
1032 iova_magazine_free(cpu_rcache->prev);
1034 free_percpu(rcache->cpu_rcaches);
1035 for (j = 0; j < rcache->depot_size; ++j)
1036 iova_magazine_free(rcache->depot[j]);
1041 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1043 static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1045 struct iova_cpu_rcache *cpu_rcache;
1046 struct iova_rcache *rcache;
1047 unsigned long flags;
1050 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1051 rcache = &iovad->rcaches[i];
1052 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1053 spin_lock_irqsave(&cpu_rcache->lock, flags);
1054 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1055 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1056 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1061 * free all the IOVA ranges of global cache
1063 static void free_global_cached_iovas(struct iova_domain *iovad)
1065 struct iova_rcache *rcache;
1066 unsigned long flags;
1069 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1070 rcache = &iovad->rcaches[i];
1071 spin_lock_irqsave(&rcache->lock, flags);
1072 for (j = 0; j < rcache->depot_size; ++j) {
1073 iova_magazine_free_pfns(rcache->depot[j], iovad);
1074 iova_magazine_free(rcache->depot[j]);
1076 rcache->depot_size = 0;
1077 spin_unlock_irqrestore(&rcache->lock, flags);
1081 MODULE_LICENSE("GPL");