2 * Copyright © 2006-2009, Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
20 #include <linux/iova.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <linux/bitops.h>
25 #include <linux/cpu.h>
27 static bool iova_rcache_insert(struct iova_domain *iovad,
30 static unsigned long iova_rcache_get(struct iova_domain *iovad,
32 unsigned long limit_pfn);
33 static void init_iova_rcaches(struct iova_domain *iovad);
34 static void free_iova_rcaches(struct iova_domain *iovad);
35 static void fq_destroy_all_entries(struct iova_domain *iovad);
36 static void fq_flush_timeout(unsigned long data);
39 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
40 unsigned long start_pfn)
43 * IOVA granularity will normally be equal to the smallest
44 * supported IOMMU page size; both *must* be capable of
45 * representing individual CPU pages exactly.
47 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
49 spin_lock_init(&iovad->iova_rbtree_lock);
50 iovad->rbroot = RB_ROOT;
51 iovad->cached_node = NULL;
52 iovad->cached32_node = NULL;
53 iovad->granule = granule;
54 iovad->start_pfn = start_pfn;
55 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
56 iovad->flush_cb = NULL;
58 init_iova_rcaches(iovad);
60 EXPORT_SYMBOL_GPL(init_iova_domain);
62 static void free_iova_flush_queue(struct iova_domain *iovad)
67 if (timer_pending(&iovad->fq_timer))
68 del_timer(&iovad->fq_timer);
70 fq_destroy_all_entries(iovad);
72 free_percpu(iovad->fq);
75 iovad->flush_cb = NULL;
76 iovad->entry_dtor = NULL;
79 int init_iova_flush_queue(struct iova_domain *iovad,
80 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
84 atomic64_set(&iovad->fq_flush_start_cnt, 0);
85 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
87 iovad->fq = alloc_percpu(struct iova_fq);
91 iovad->flush_cb = flush_cb;
92 iovad->entry_dtor = entry_dtor;
94 for_each_possible_cpu(cpu) {
97 fq = per_cpu_ptr(iovad->fq, cpu);
101 spin_lock_init(&fq->lock);
104 setup_timer(&iovad->fq_timer, fq_flush_timeout, (unsigned long)iovad);
105 atomic_set(&iovad->fq_timer_on, 0);
109 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
111 static struct rb_node *
112 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
114 struct rb_node *cached_node = NULL;
115 struct iova *curr_iova;
117 if (*limit_pfn <= iovad->dma_32bit_pfn)
118 cached_node = iovad->cached32_node;
120 cached_node = iovad->cached_node;
122 return rb_last(&iovad->rbroot);
124 curr_iova = rb_entry(cached_node, struct iova, node);
125 *limit_pfn = min(*limit_pfn, curr_iova->pfn_lo);
127 return rb_prev(cached_node);
131 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
133 if (new->pfn_hi < iovad->dma_32bit_pfn)
134 iovad->cached32_node = &new->node;
136 iovad->cached_node = &new->node;
140 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
142 struct iova *cached_iova;
144 cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
145 if (free->pfn_hi < iovad->dma_32bit_pfn &&
146 iovad->cached32_node && free->pfn_lo >= cached_iova->pfn_lo)
147 iovad->cached32_node = rb_next(&free->node);
149 cached_iova = rb_entry(iovad->cached_node, struct iova, node);
150 if (iovad->cached_node && free->pfn_lo >= cached_iova->pfn_lo)
151 iovad->cached_node = rb_next(&free->node);
154 /* Insert the iova into domain rbtree by holding writer lock */
156 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
157 struct rb_node *start)
159 struct rb_node **new, *parent = NULL;
161 new = (start) ? &start : &(root->rb_node);
162 /* Figure out where to put new node */
164 struct iova *this = rb_entry(*new, struct iova, node);
168 if (iova->pfn_lo < this->pfn_lo)
169 new = &((*new)->rb_left);
170 else if (iova->pfn_lo > this->pfn_lo)
171 new = &((*new)->rb_right);
173 WARN_ON(1); /* this should not happen */
177 /* Add new node and rebalance tree. */
178 rb_link_node(&iova->node, parent, new);
179 rb_insert_color(&iova->node, root);
182 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
183 unsigned long size, unsigned long limit_pfn,
184 struct iova *new, bool size_aligned)
186 struct rb_node *prev, *curr = NULL;
188 unsigned long new_pfn;
189 unsigned long align_mask = ~0UL;
192 align_mask <<= fls_long(size - 1);
194 /* Walk the tree backwards */
195 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
196 curr = __get_cached_rbnode(iovad, &limit_pfn);
199 struct iova *curr_iova = rb_entry(curr, struct iova, node);
201 if (limit_pfn <= curr_iova->pfn_lo)
204 if (((limit_pfn - size) & align_mask) > curr_iova->pfn_hi)
205 break; /* found a free slot */
207 limit_pfn = curr_iova->pfn_lo;
210 curr = rb_prev(curr);
213 new_pfn = (limit_pfn - size) & align_mask;
214 if (limit_pfn < size || new_pfn < iovad->start_pfn) {
215 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
219 /* pfn_lo will point to size aligned address if size_aligned is set */
220 new->pfn_lo = new_pfn;
221 new->pfn_hi = new->pfn_lo + size - 1;
223 /* If we have 'prev', it's a valid place to start the insertion. */
224 iova_insert_rbtree(&iovad->rbroot, new, prev);
225 __cached_rbnode_insert_update(iovad, new);
227 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
233 static struct kmem_cache *iova_cache;
234 static unsigned int iova_cache_users;
235 static DEFINE_MUTEX(iova_cache_mutex);
237 struct iova *alloc_iova_mem(void)
239 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
241 EXPORT_SYMBOL(alloc_iova_mem);
243 void free_iova_mem(struct iova *iova)
245 kmem_cache_free(iova_cache, iova);
247 EXPORT_SYMBOL(free_iova_mem);
249 int iova_cache_get(void)
251 mutex_lock(&iova_cache_mutex);
252 if (!iova_cache_users) {
253 iova_cache = kmem_cache_create(
254 "iommu_iova", sizeof(struct iova), 0,
255 SLAB_HWCACHE_ALIGN, NULL);
257 mutex_unlock(&iova_cache_mutex);
258 printk(KERN_ERR "Couldn't create iova cache\n");
264 mutex_unlock(&iova_cache_mutex);
268 EXPORT_SYMBOL_GPL(iova_cache_get);
270 void iova_cache_put(void)
272 mutex_lock(&iova_cache_mutex);
273 if (WARN_ON(!iova_cache_users)) {
274 mutex_unlock(&iova_cache_mutex);
278 if (!iova_cache_users)
279 kmem_cache_destroy(iova_cache);
280 mutex_unlock(&iova_cache_mutex);
282 EXPORT_SYMBOL_GPL(iova_cache_put);
285 * alloc_iova - allocates an iova
286 * @iovad: - iova domain in question
287 * @size: - size of page frames to allocate
288 * @limit_pfn: - max limit address
289 * @size_aligned: - set if size_aligned address range is required
290 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
291 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
292 * flag is set then the allocated address iova->pfn_lo will be naturally
293 * aligned on roundup_power_of_two(size).
296 alloc_iova(struct iova_domain *iovad, unsigned long size,
297 unsigned long limit_pfn,
300 struct iova *new_iova;
303 new_iova = alloc_iova_mem();
307 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
308 new_iova, size_aligned);
311 free_iova_mem(new_iova);
317 EXPORT_SYMBOL_GPL(alloc_iova);
320 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
322 struct rb_node *node = iovad->rbroot.rb_node;
324 assert_spin_locked(&iovad->iova_rbtree_lock);
327 struct iova *iova = rb_entry(node, struct iova, node);
329 if (pfn < iova->pfn_lo)
330 node = node->rb_left;
331 else if (pfn > iova->pfn_hi)
332 node = node->rb_right;
334 return iova; /* pfn falls within iova's range */
340 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
342 assert_spin_locked(&iovad->iova_rbtree_lock);
343 __cached_rbnode_delete_update(iovad, iova);
344 rb_erase(&iova->node, &iovad->rbroot);
349 * find_iova - finds an iova for a given pfn
350 * @iovad: - iova domain in question.
351 * @pfn: - page frame number
352 * This function finds and returns an iova belonging to the
353 * given doamin which matches the given pfn.
355 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
360 /* Take the lock so that no other thread is manipulating the rbtree */
361 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
362 iova = private_find_iova(iovad, pfn);
363 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
366 EXPORT_SYMBOL_GPL(find_iova);
369 * __free_iova - frees the given iova
370 * @iovad: iova domain in question.
371 * @iova: iova in question.
372 * Frees the given iova belonging to the giving domain
375 __free_iova(struct iova_domain *iovad, struct iova *iova)
379 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
380 private_free_iova(iovad, iova);
381 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
383 EXPORT_SYMBOL_GPL(__free_iova);
386 * free_iova - finds and frees the iova for a given pfn
387 * @iovad: - iova domain in question.
388 * @pfn: - pfn that is allocated previously
389 * This functions finds an iova for a given pfn and then
390 * frees the iova from that domain.
393 free_iova(struct iova_domain *iovad, unsigned long pfn)
395 struct iova *iova = find_iova(iovad, pfn);
398 __free_iova(iovad, iova);
401 EXPORT_SYMBOL_GPL(free_iova);
404 * alloc_iova_fast - allocates an iova from rcache
405 * @iovad: - iova domain in question
406 * @size: - size of page frames to allocate
407 * @limit_pfn: - max limit address
408 * This function tries to satisfy an iova allocation from the rcache,
409 * and falls back to regular allocation on failure.
412 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
413 unsigned long limit_pfn)
415 bool flushed_rcache = false;
416 unsigned long iova_pfn;
417 struct iova *new_iova;
419 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
424 new_iova = alloc_iova(iovad, size, limit_pfn, true);
431 /* Try replenishing IOVAs by flushing rcache. */
432 flushed_rcache = true;
433 for_each_online_cpu(cpu)
434 free_cpu_cached_iovas(cpu, iovad);
438 return new_iova->pfn_lo;
440 EXPORT_SYMBOL_GPL(alloc_iova_fast);
443 * free_iova_fast - free iova pfn range into rcache
444 * @iovad: - iova domain in question.
445 * @pfn: - pfn that is allocated previously
446 * @size: - # of pages in range
447 * This functions frees an iova range by trying to put it into the rcache,
448 * falling back to regular iova deallocation via free_iova() if this fails.
451 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
453 if (iova_rcache_insert(iovad, pfn, size))
456 free_iova(iovad, pfn);
458 EXPORT_SYMBOL_GPL(free_iova_fast);
460 #define fq_ring_for_each(i, fq) \
461 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
463 static inline bool fq_full(struct iova_fq *fq)
465 assert_spin_locked(&fq->lock);
466 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
469 static inline unsigned fq_ring_add(struct iova_fq *fq)
471 unsigned idx = fq->tail;
473 assert_spin_locked(&fq->lock);
475 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
480 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
482 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
485 assert_spin_locked(&fq->lock);
487 fq_ring_for_each(idx, fq) {
489 if (fq->entries[idx].counter >= counter)
492 if (iovad->entry_dtor)
493 iovad->entry_dtor(fq->entries[idx].data);
495 free_iova_fast(iovad,
496 fq->entries[idx].iova_pfn,
497 fq->entries[idx].pages);
499 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
503 static void iova_domain_flush(struct iova_domain *iovad)
505 atomic64_inc(&iovad->fq_flush_start_cnt);
506 iovad->flush_cb(iovad);
507 atomic64_inc(&iovad->fq_flush_finish_cnt);
510 static void fq_destroy_all_entries(struct iova_domain *iovad)
515 * This code runs when the iova_domain is being detroyed, so don't
516 * bother to free iovas, just call the entry_dtor on all remaining
519 if (!iovad->entry_dtor)
522 for_each_possible_cpu(cpu) {
523 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
526 fq_ring_for_each(idx, fq)
527 iovad->entry_dtor(fq->entries[idx].data);
531 static void fq_flush_timeout(unsigned long data)
533 struct iova_domain *iovad = (struct iova_domain *)data;
536 atomic_set(&iovad->fq_timer_on, 0);
537 iova_domain_flush(iovad);
539 for_each_possible_cpu(cpu) {
543 fq = per_cpu_ptr(iovad->fq, cpu);
544 spin_lock_irqsave(&fq->lock, flags);
545 fq_ring_free(iovad, fq);
546 spin_unlock_irqrestore(&fq->lock, flags);
550 void queue_iova(struct iova_domain *iovad,
551 unsigned long pfn, unsigned long pages,
554 struct iova_fq *fq = get_cpu_ptr(iovad->fq);
558 spin_lock_irqsave(&fq->lock, flags);
561 * First remove all entries from the flush queue that have already been
562 * flushed out on another CPU. This makes the fq_full() check below less
565 fq_ring_free(iovad, fq);
568 iova_domain_flush(iovad);
569 fq_ring_free(iovad, fq);
572 idx = fq_ring_add(fq);
574 fq->entries[idx].iova_pfn = pfn;
575 fq->entries[idx].pages = pages;
576 fq->entries[idx].data = data;
577 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
579 spin_unlock_irqrestore(&fq->lock, flags);
581 if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
582 mod_timer(&iovad->fq_timer,
583 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
585 put_cpu_ptr(iovad->fq);
587 EXPORT_SYMBOL_GPL(queue_iova);
590 * put_iova_domain - destroys the iova doamin
591 * @iovad: - iova domain in question.
592 * All the iova's in that domain are destroyed.
594 void put_iova_domain(struct iova_domain *iovad)
596 struct rb_node *node;
599 free_iova_flush_queue(iovad);
600 free_iova_rcaches(iovad);
601 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
602 node = rb_first(&iovad->rbroot);
604 struct iova *iova = rb_entry(node, struct iova, node);
606 rb_erase(node, &iovad->rbroot);
608 node = rb_first(&iovad->rbroot);
610 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
612 EXPORT_SYMBOL_GPL(put_iova_domain);
615 __is_range_overlap(struct rb_node *node,
616 unsigned long pfn_lo, unsigned long pfn_hi)
618 struct iova *iova = rb_entry(node, struct iova, node);
620 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
625 static inline struct iova *
626 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
630 iova = alloc_iova_mem();
632 iova->pfn_lo = pfn_lo;
633 iova->pfn_hi = pfn_hi;
640 __insert_new_range(struct iova_domain *iovad,
641 unsigned long pfn_lo, unsigned long pfn_hi)
645 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
647 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
653 __adjust_overlap_range(struct iova *iova,
654 unsigned long *pfn_lo, unsigned long *pfn_hi)
656 if (*pfn_lo < iova->pfn_lo)
657 iova->pfn_lo = *pfn_lo;
658 if (*pfn_hi > iova->pfn_hi)
659 *pfn_lo = iova->pfn_hi + 1;
663 * reserve_iova - reserves an iova in the given range
664 * @iovad: - iova domain pointer
665 * @pfn_lo: - lower page frame address
666 * @pfn_hi:- higher pfn adderss
667 * This function allocates reserves the address range from pfn_lo to pfn_hi so
668 * that this address is not dished out as part of alloc_iova.
671 reserve_iova(struct iova_domain *iovad,
672 unsigned long pfn_lo, unsigned long pfn_hi)
674 struct rb_node *node;
677 unsigned int overlap = 0;
679 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
680 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
681 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
682 iova = rb_entry(node, struct iova, node);
683 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
684 if ((pfn_lo >= iova->pfn_lo) &&
685 (pfn_hi <= iova->pfn_hi))
693 /* We are here either because this is the first reserver node
694 * or need to insert remaining non overlap addr range
696 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
699 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
702 EXPORT_SYMBOL_GPL(reserve_iova);
705 * copy_reserved_iova - copies the reserved between domains
706 * @from: - source doamin from where to copy
707 * @to: - destination domin where to copy
708 * This function copies reserved iova's from one doamin to
712 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
715 struct rb_node *node;
717 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
718 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
719 struct iova *iova = rb_entry(node, struct iova, node);
720 struct iova *new_iova;
722 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
724 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
725 iova->pfn_lo, iova->pfn_lo);
727 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
729 EXPORT_SYMBOL_GPL(copy_reserved_iova);
732 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
733 unsigned long pfn_lo, unsigned long pfn_hi)
736 struct iova *prev = NULL, *next = NULL;
738 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
739 if (iova->pfn_lo < pfn_lo) {
740 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
744 if (iova->pfn_hi > pfn_hi) {
745 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
750 __cached_rbnode_delete_update(iovad, iova);
751 rb_erase(&iova->node, &iovad->rbroot);
754 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
755 iova->pfn_lo = pfn_lo;
758 iova_insert_rbtree(&iovad->rbroot, next, NULL);
759 iova->pfn_hi = pfn_hi;
761 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
766 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
773 * Magazine caches for IOVA ranges. For an introduction to magazines,
774 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
775 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
776 * For simplicity, we use a static magazine size and don't implement the
777 * dynamic size tuning described in the paper.
780 #define IOVA_MAG_SIZE 128
782 struct iova_magazine {
784 unsigned long pfns[IOVA_MAG_SIZE];
787 struct iova_cpu_rcache {
789 struct iova_magazine *loaded;
790 struct iova_magazine *prev;
793 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
795 return kzalloc(sizeof(struct iova_magazine), flags);
798 static void iova_magazine_free(struct iova_magazine *mag)
804 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
812 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
814 for (i = 0 ; i < mag->size; ++i) {
815 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
818 private_free_iova(iovad, iova);
821 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
826 static bool iova_magazine_full(struct iova_magazine *mag)
828 return (mag && mag->size == IOVA_MAG_SIZE);
831 static bool iova_magazine_empty(struct iova_magazine *mag)
833 return (!mag || mag->size == 0);
836 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
837 unsigned long limit_pfn)
839 BUG_ON(iova_magazine_empty(mag));
841 if (mag->pfns[mag->size - 1] >= limit_pfn)
844 return mag->pfns[--mag->size];
847 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
849 BUG_ON(iova_magazine_full(mag));
851 mag->pfns[mag->size++] = pfn;
854 static void init_iova_rcaches(struct iova_domain *iovad)
856 struct iova_cpu_rcache *cpu_rcache;
857 struct iova_rcache *rcache;
861 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
862 rcache = &iovad->rcaches[i];
863 spin_lock_init(&rcache->lock);
864 rcache->depot_size = 0;
865 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
866 if (WARN_ON(!rcache->cpu_rcaches))
868 for_each_possible_cpu(cpu) {
869 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
870 spin_lock_init(&cpu_rcache->lock);
871 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
872 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
878 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
879 * return true on success. Can fail if rcache is full and we can't free
880 * space, and free_iova() (our only caller) will then return the IOVA
881 * range to the rbtree instead.
883 static bool __iova_rcache_insert(struct iova_domain *iovad,
884 struct iova_rcache *rcache,
885 unsigned long iova_pfn)
887 struct iova_magazine *mag_to_free = NULL;
888 struct iova_cpu_rcache *cpu_rcache;
889 bool can_insert = false;
892 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
893 spin_lock_irqsave(&cpu_rcache->lock, flags);
895 if (!iova_magazine_full(cpu_rcache->loaded)) {
897 } else if (!iova_magazine_full(cpu_rcache->prev)) {
898 swap(cpu_rcache->prev, cpu_rcache->loaded);
901 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
904 spin_lock(&rcache->lock);
905 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
906 rcache->depot[rcache->depot_size++] =
909 mag_to_free = cpu_rcache->loaded;
911 spin_unlock(&rcache->lock);
913 cpu_rcache->loaded = new_mag;
919 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
921 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
924 iova_magazine_free_pfns(mag_to_free, iovad);
925 iova_magazine_free(mag_to_free);
931 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
934 unsigned int log_size = order_base_2(size);
936 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
939 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
943 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
944 * satisfy the request, return a matching non-NULL range and remove
945 * it from the 'rcache'.
947 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
948 unsigned long limit_pfn)
950 struct iova_cpu_rcache *cpu_rcache;
951 unsigned long iova_pfn = 0;
952 bool has_pfn = false;
955 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
956 spin_lock_irqsave(&cpu_rcache->lock, flags);
958 if (!iova_magazine_empty(cpu_rcache->loaded)) {
960 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
961 swap(cpu_rcache->prev, cpu_rcache->loaded);
964 spin_lock(&rcache->lock);
965 if (rcache->depot_size > 0) {
966 iova_magazine_free(cpu_rcache->loaded);
967 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
970 spin_unlock(&rcache->lock);
974 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
976 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
982 * Try to satisfy IOVA allocation range from rcache. Fail if requested
983 * size is too big or the DMA limit we are given isn't satisfied by the
984 * top element in the magazine.
986 static unsigned long iova_rcache_get(struct iova_domain *iovad,
988 unsigned long limit_pfn)
990 unsigned int log_size = order_base_2(size);
992 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
995 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
999 * Free a cpu's rcache.
1001 static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
1002 struct iova_rcache *rcache)
1004 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1005 unsigned long flags;
1007 spin_lock_irqsave(&cpu_rcache->lock, flags);
1009 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1010 iova_magazine_free(cpu_rcache->loaded);
1012 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1013 iova_magazine_free(cpu_rcache->prev);
1015 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1019 * free rcache data structures.
1021 static void free_iova_rcaches(struct iova_domain *iovad)
1023 struct iova_rcache *rcache;
1024 unsigned long flags;
1028 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1029 rcache = &iovad->rcaches[i];
1030 for_each_possible_cpu(cpu)
1031 free_cpu_iova_rcache(cpu, iovad, rcache);
1032 spin_lock_irqsave(&rcache->lock, flags);
1033 free_percpu(rcache->cpu_rcaches);
1034 for (j = 0; j < rcache->depot_size; ++j) {
1035 iova_magazine_free_pfns(rcache->depot[j], iovad);
1036 iova_magazine_free(rcache->depot[j]);
1038 spin_unlock_irqrestore(&rcache->lock, flags);
1043 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1045 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1047 struct iova_cpu_rcache *cpu_rcache;
1048 struct iova_rcache *rcache;
1049 unsigned long flags;
1052 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1053 rcache = &iovad->rcaches[i];
1054 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1055 spin_lock_irqsave(&cpu_rcache->lock, flags);
1056 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1057 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1058 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1062 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1063 MODULE_LICENSE("GPL");