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);
37 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
38 unsigned long start_pfn, unsigned long pfn_32bit)
41 * IOVA granularity will normally be equal to the smallest
42 * supported IOMMU page size; both *must* be capable of
43 * representing individual CPU pages exactly.
45 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
47 spin_lock_init(&iovad->iova_rbtree_lock);
48 iovad->rbroot = RB_ROOT;
49 iovad->cached32_node = NULL;
50 iovad->granule = granule;
51 iovad->start_pfn = start_pfn;
52 iovad->dma_32bit_pfn = pfn_32bit + 1;
53 iovad->flush_cb = NULL;
55 init_iova_rcaches(iovad);
57 EXPORT_SYMBOL_GPL(init_iova_domain);
59 static void free_iova_flush_queue(struct iova_domain *iovad)
64 free_percpu(iovad->fq);
67 iovad->flush_cb = NULL;
68 iovad->entry_dtor = NULL;
71 int init_iova_flush_queue(struct iova_domain *iovad,
72 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
76 iovad->fq = alloc_percpu(struct iova_fq);
80 iovad->flush_cb = flush_cb;
81 iovad->entry_dtor = entry_dtor;
83 for_each_possible_cpu(cpu) {
86 fq = per_cpu_ptr(iovad->fq, cpu);
93 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
95 static struct rb_node *
96 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
98 if ((*limit_pfn > iovad->dma_32bit_pfn) ||
99 (iovad->cached32_node == NULL))
100 return rb_last(&iovad->rbroot);
102 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
103 struct iova *curr_iova =
104 rb_entry(iovad->cached32_node, struct iova, node);
105 *limit_pfn = curr_iova->pfn_lo;
111 __cached_rbnode_insert_update(struct iova_domain *iovad,
112 unsigned long limit_pfn, struct iova *new)
114 if (limit_pfn != iovad->dma_32bit_pfn)
116 iovad->cached32_node = &new->node;
120 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
122 struct iova *cached_iova;
123 struct rb_node *curr;
125 if (!iovad->cached32_node)
127 curr = iovad->cached32_node;
128 cached_iova = rb_entry(curr, struct iova, node);
130 if (free->pfn_lo >= cached_iova->pfn_lo) {
131 struct rb_node *node = rb_next(&free->node);
132 struct iova *iova = rb_entry(node, struct iova, node);
134 /* only cache if it's below 32bit pfn */
135 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
136 iovad->cached32_node = node;
138 iovad->cached32_node = NULL;
142 /* Insert the iova into domain rbtree by holding writer lock */
144 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
145 struct rb_node *start)
147 struct rb_node **new, *parent = NULL;
149 new = (start) ? &start : &(root->rb_node);
150 /* Figure out where to put new node */
152 struct iova *this = rb_entry(*new, struct iova, node);
156 if (iova->pfn_lo < this->pfn_lo)
157 new = &((*new)->rb_left);
158 else if (iova->pfn_lo > this->pfn_lo)
159 new = &((*new)->rb_right);
161 WARN_ON(1); /* this should not happen */
165 /* Add new node and rebalance tree. */
166 rb_link_node(&iova->node, parent, new);
167 rb_insert_color(&iova->node, root);
171 * Computes the padding size required, to make the start address
172 * naturally aligned on the power-of-two order of its size
175 iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
177 return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1);
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 *prev, *curr = NULL;
186 unsigned long saved_pfn;
187 unsigned int pad_size = 0;
189 /* Walk the tree backwards */
190 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
191 saved_pfn = limit_pfn;
192 curr = __get_cached_rbnode(iovad, &limit_pfn);
195 struct iova *curr_iova = rb_entry(curr, struct iova, node);
197 if (limit_pfn <= curr_iova->pfn_lo) {
199 } else if (limit_pfn > curr_iova->pfn_hi) {
201 pad_size = iova_get_pad_size(size, limit_pfn);
202 if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn)
203 break; /* found a free slot */
205 limit_pfn = curr_iova->pfn_lo;
208 curr = rb_prev(curr);
213 pad_size = iova_get_pad_size(size, limit_pfn);
214 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
215 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
220 /* pfn_lo will point to size aligned address if size_aligned is set */
221 new->pfn_lo = limit_pfn - (size + pad_size);
222 new->pfn_hi = new->pfn_lo + size - 1;
224 /* If we have 'prev', it's a valid place to start the insertion. */
225 iova_insert_rbtree(&iovad->rbroot, new, prev);
226 __cached_rbnode_insert_update(iovad, saved_pfn, new);
228 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
234 static struct kmem_cache *iova_cache;
235 static unsigned int iova_cache_users;
236 static DEFINE_MUTEX(iova_cache_mutex);
238 struct iova *alloc_iova_mem(void)
240 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
242 EXPORT_SYMBOL(alloc_iova_mem);
244 void free_iova_mem(struct iova *iova)
246 kmem_cache_free(iova_cache, iova);
248 EXPORT_SYMBOL(free_iova_mem);
250 int iova_cache_get(void)
252 mutex_lock(&iova_cache_mutex);
253 if (!iova_cache_users) {
254 iova_cache = kmem_cache_create(
255 "iommu_iova", sizeof(struct iova), 0,
256 SLAB_HWCACHE_ALIGN, NULL);
258 mutex_unlock(&iova_cache_mutex);
259 printk(KERN_ERR "Couldn't create iova cache\n");
265 mutex_unlock(&iova_cache_mutex);
269 EXPORT_SYMBOL_GPL(iova_cache_get);
271 void iova_cache_put(void)
273 mutex_lock(&iova_cache_mutex);
274 if (WARN_ON(!iova_cache_users)) {
275 mutex_unlock(&iova_cache_mutex);
279 if (!iova_cache_users)
280 kmem_cache_destroy(iova_cache);
281 mutex_unlock(&iova_cache_mutex);
283 EXPORT_SYMBOL_GPL(iova_cache_put);
286 * alloc_iova - allocates an iova
287 * @iovad: - iova domain in question
288 * @size: - size of page frames to allocate
289 * @limit_pfn: - max limit address
290 * @size_aligned: - set if size_aligned address range is required
291 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
292 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
293 * flag is set then the allocated address iova->pfn_lo will be naturally
294 * aligned on roundup_power_of_two(size).
297 alloc_iova(struct iova_domain *iovad, unsigned long size,
298 unsigned long limit_pfn,
301 struct iova *new_iova;
304 new_iova = alloc_iova_mem();
308 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
309 new_iova, size_aligned);
312 free_iova_mem(new_iova);
318 EXPORT_SYMBOL_GPL(alloc_iova);
321 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
323 struct rb_node *node = iovad->rbroot.rb_node;
325 assert_spin_locked(&iovad->iova_rbtree_lock);
328 struct iova *iova = rb_entry(node, struct iova, node);
330 /* If pfn falls within iova's range, return iova */
331 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
335 if (pfn < iova->pfn_lo)
336 node = node->rb_left;
337 else if (pfn > iova->pfn_lo)
338 node = node->rb_right;
344 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
346 assert_spin_locked(&iovad->iova_rbtree_lock);
347 __cached_rbnode_delete_update(iovad, iova);
348 rb_erase(&iova->node, &iovad->rbroot);
353 * find_iova - finds an iova for a given pfn
354 * @iovad: - iova domain in question.
355 * @pfn: - page frame number
356 * This function finds and returns an iova belonging to the
357 * given doamin which matches the given pfn.
359 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
364 /* Take the lock so that no other thread is manipulating the rbtree */
365 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
366 iova = private_find_iova(iovad, pfn);
367 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
370 EXPORT_SYMBOL_GPL(find_iova);
373 * __free_iova - frees the given iova
374 * @iovad: iova domain in question.
375 * @iova: iova in question.
376 * Frees the given iova belonging to the giving domain
379 __free_iova(struct iova_domain *iovad, struct iova *iova)
383 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
384 private_free_iova(iovad, iova);
385 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
387 EXPORT_SYMBOL_GPL(__free_iova);
390 * free_iova - finds and frees the iova for a given pfn
391 * @iovad: - iova domain in question.
392 * @pfn: - pfn that is allocated previously
393 * This functions finds an iova for a given pfn and then
394 * frees the iova from that domain.
397 free_iova(struct iova_domain *iovad, unsigned long pfn)
399 struct iova *iova = find_iova(iovad, pfn);
402 __free_iova(iovad, iova);
405 EXPORT_SYMBOL_GPL(free_iova);
408 * alloc_iova_fast - allocates an iova from rcache
409 * @iovad: - iova domain in question
410 * @size: - size of page frames to allocate
411 * @limit_pfn: - max limit address
412 * This function tries to satisfy an iova allocation from the rcache,
413 * and falls back to regular allocation on failure.
416 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
417 unsigned long limit_pfn)
419 bool flushed_rcache = false;
420 unsigned long iova_pfn;
421 struct iova *new_iova;
423 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
428 new_iova = alloc_iova(iovad, size, limit_pfn, true);
435 /* Try replenishing IOVAs by flushing rcache. */
436 flushed_rcache = true;
437 for_each_online_cpu(cpu)
438 free_cpu_cached_iovas(cpu, iovad);
442 return new_iova->pfn_lo;
444 EXPORT_SYMBOL_GPL(alloc_iova_fast);
447 * free_iova_fast - free iova pfn range into rcache
448 * @iovad: - iova domain in question.
449 * @pfn: - pfn that is allocated previously
450 * @size: - # of pages in range
451 * This functions frees an iova range by trying to put it into the rcache,
452 * falling back to regular iova deallocation via free_iova() if this fails.
455 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
457 if (iova_rcache_insert(iovad, pfn, size))
460 free_iova(iovad, pfn);
462 EXPORT_SYMBOL_GPL(free_iova_fast);
465 * put_iova_domain - destroys the iova doamin
466 * @iovad: - iova domain in question.
467 * All the iova's in that domain are destroyed.
469 void put_iova_domain(struct iova_domain *iovad)
471 struct rb_node *node;
474 free_iova_flush_queue(iovad);
475 free_iova_rcaches(iovad);
476 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
477 node = rb_first(&iovad->rbroot);
479 struct iova *iova = rb_entry(node, struct iova, node);
481 rb_erase(node, &iovad->rbroot);
483 node = rb_first(&iovad->rbroot);
485 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
487 EXPORT_SYMBOL_GPL(put_iova_domain);
490 __is_range_overlap(struct rb_node *node,
491 unsigned long pfn_lo, unsigned long pfn_hi)
493 struct iova *iova = rb_entry(node, struct iova, node);
495 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
500 static inline struct iova *
501 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
505 iova = alloc_iova_mem();
507 iova->pfn_lo = pfn_lo;
508 iova->pfn_hi = pfn_hi;
515 __insert_new_range(struct iova_domain *iovad,
516 unsigned long pfn_lo, unsigned long pfn_hi)
520 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
522 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
528 __adjust_overlap_range(struct iova *iova,
529 unsigned long *pfn_lo, unsigned long *pfn_hi)
531 if (*pfn_lo < iova->pfn_lo)
532 iova->pfn_lo = *pfn_lo;
533 if (*pfn_hi > iova->pfn_hi)
534 *pfn_lo = iova->pfn_hi + 1;
538 * reserve_iova - reserves an iova in the given range
539 * @iovad: - iova domain pointer
540 * @pfn_lo: - lower page frame address
541 * @pfn_hi:- higher pfn adderss
542 * This function allocates reserves the address range from pfn_lo to pfn_hi so
543 * that this address is not dished out as part of alloc_iova.
546 reserve_iova(struct iova_domain *iovad,
547 unsigned long pfn_lo, unsigned long pfn_hi)
549 struct rb_node *node;
552 unsigned int overlap = 0;
554 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
555 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
556 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
557 iova = rb_entry(node, struct iova, node);
558 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
559 if ((pfn_lo >= iova->pfn_lo) &&
560 (pfn_hi <= iova->pfn_hi))
568 /* We are here either because this is the first reserver node
569 * or need to insert remaining non overlap addr range
571 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
574 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
577 EXPORT_SYMBOL_GPL(reserve_iova);
580 * copy_reserved_iova - copies the reserved between domains
581 * @from: - source doamin from where to copy
582 * @to: - destination domin where to copy
583 * This function copies reserved iova's from one doamin to
587 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
590 struct rb_node *node;
592 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
593 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
594 struct iova *iova = rb_entry(node, struct iova, node);
595 struct iova *new_iova;
597 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
599 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
600 iova->pfn_lo, iova->pfn_lo);
602 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
604 EXPORT_SYMBOL_GPL(copy_reserved_iova);
607 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
608 unsigned long pfn_lo, unsigned long pfn_hi)
611 struct iova *prev = NULL, *next = NULL;
613 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
614 if (iova->pfn_lo < pfn_lo) {
615 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
619 if (iova->pfn_hi > pfn_hi) {
620 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
625 __cached_rbnode_delete_update(iovad, iova);
626 rb_erase(&iova->node, &iovad->rbroot);
629 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
630 iova->pfn_lo = pfn_lo;
633 iova_insert_rbtree(&iovad->rbroot, next, NULL);
634 iova->pfn_hi = pfn_hi;
636 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
641 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
648 * Magazine caches for IOVA ranges. For an introduction to magazines,
649 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
650 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
651 * For simplicity, we use a static magazine size and don't implement the
652 * dynamic size tuning described in the paper.
655 #define IOVA_MAG_SIZE 128
657 struct iova_magazine {
659 unsigned long pfns[IOVA_MAG_SIZE];
662 struct iova_cpu_rcache {
664 struct iova_magazine *loaded;
665 struct iova_magazine *prev;
668 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
670 return kzalloc(sizeof(struct iova_magazine), flags);
673 static void iova_magazine_free(struct iova_magazine *mag)
679 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
687 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
689 for (i = 0 ; i < mag->size; ++i) {
690 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
693 private_free_iova(iovad, iova);
696 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
701 static bool iova_magazine_full(struct iova_magazine *mag)
703 return (mag && mag->size == IOVA_MAG_SIZE);
706 static bool iova_magazine_empty(struct iova_magazine *mag)
708 return (!mag || mag->size == 0);
711 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
712 unsigned long limit_pfn)
714 BUG_ON(iova_magazine_empty(mag));
716 if (mag->pfns[mag->size - 1] >= limit_pfn)
719 return mag->pfns[--mag->size];
722 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
724 BUG_ON(iova_magazine_full(mag));
726 mag->pfns[mag->size++] = pfn;
729 static void init_iova_rcaches(struct iova_domain *iovad)
731 struct iova_cpu_rcache *cpu_rcache;
732 struct iova_rcache *rcache;
736 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
737 rcache = &iovad->rcaches[i];
738 spin_lock_init(&rcache->lock);
739 rcache->depot_size = 0;
740 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
741 if (WARN_ON(!rcache->cpu_rcaches))
743 for_each_possible_cpu(cpu) {
744 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
745 spin_lock_init(&cpu_rcache->lock);
746 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
747 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
753 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
754 * return true on success. Can fail if rcache is full and we can't free
755 * space, and free_iova() (our only caller) will then return the IOVA
756 * range to the rbtree instead.
758 static bool __iova_rcache_insert(struct iova_domain *iovad,
759 struct iova_rcache *rcache,
760 unsigned long iova_pfn)
762 struct iova_magazine *mag_to_free = NULL;
763 struct iova_cpu_rcache *cpu_rcache;
764 bool can_insert = false;
767 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
768 spin_lock_irqsave(&cpu_rcache->lock, flags);
770 if (!iova_magazine_full(cpu_rcache->loaded)) {
772 } else if (!iova_magazine_full(cpu_rcache->prev)) {
773 swap(cpu_rcache->prev, cpu_rcache->loaded);
776 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
779 spin_lock(&rcache->lock);
780 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
781 rcache->depot[rcache->depot_size++] =
784 mag_to_free = cpu_rcache->loaded;
786 spin_unlock(&rcache->lock);
788 cpu_rcache->loaded = new_mag;
794 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
796 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
799 iova_magazine_free_pfns(mag_to_free, iovad);
800 iova_magazine_free(mag_to_free);
806 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
809 unsigned int log_size = order_base_2(size);
811 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
814 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
818 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
819 * satisfy the request, return a matching non-NULL range and remove
820 * it from the 'rcache'.
822 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
823 unsigned long limit_pfn)
825 struct iova_cpu_rcache *cpu_rcache;
826 unsigned long iova_pfn = 0;
827 bool has_pfn = false;
830 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
831 spin_lock_irqsave(&cpu_rcache->lock, flags);
833 if (!iova_magazine_empty(cpu_rcache->loaded)) {
835 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
836 swap(cpu_rcache->prev, cpu_rcache->loaded);
839 spin_lock(&rcache->lock);
840 if (rcache->depot_size > 0) {
841 iova_magazine_free(cpu_rcache->loaded);
842 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
845 spin_unlock(&rcache->lock);
849 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
851 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
857 * Try to satisfy IOVA allocation range from rcache. Fail if requested
858 * size is too big or the DMA limit we are given isn't satisfied by the
859 * top element in the magazine.
861 static unsigned long iova_rcache_get(struct iova_domain *iovad,
863 unsigned long limit_pfn)
865 unsigned int log_size = order_base_2(size);
867 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
870 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
874 * Free a cpu's rcache.
876 static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
877 struct iova_rcache *rcache)
879 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
882 spin_lock_irqsave(&cpu_rcache->lock, flags);
884 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
885 iova_magazine_free(cpu_rcache->loaded);
887 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
888 iova_magazine_free(cpu_rcache->prev);
890 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
894 * free rcache data structures.
896 static void free_iova_rcaches(struct iova_domain *iovad)
898 struct iova_rcache *rcache;
903 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
904 rcache = &iovad->rcaches[i];
905 for_each_possible_cpu(cpu)
906 free_cpu_iova_rcache(cpu, iovad, rcache);
907 spin_lock_irqsave(&rcache->lock, flags);
908 free_percpu(rcache->cpu_rcaches);
909 for (j = 0; j < rcache->depot_size; ++j) {
910 iova_magazine_free_pfns(rcache->depot[j], iovad);
911 iova_magazine_free(rcache->depot[j]);
913 spin_unlock_irqrestore(&rcache->lock, flags);
918 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
920 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
922 struct iova_cpu_rcache *cpu_rcache;
923 struct iova_rcache *rcache;
927 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
928 rcache = &iovad->rcaches[i];
929 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
930 spin_lock_irqsave(&cpu_rcache->lock, flags);
931 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
932 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
933 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
937 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
938 MODULE_LICENSE("GPL");