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, unsigned long pfn_32bit)
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->cached32_node = NULL;
52 iovad->granule = granule;
53 iovad->start_pfn = start_pfn;
54 iovad->dma_32bit_pfn = pfn_32bit + 1;
55 iovad->flush_cb = NULL;
57 init_iova_rcaches(iovad);
59 EXPORT_SYMBOL_GPL(init_iova_domain);
61 bool has_iova_flush_queue(struct iova_domain *iovad)
66 static void free_iova_flush_queue(struct iova_domain *iovad)
68 if (!has_iova_flush_queue(iovad))
71 del_timer_sync(&iovad->fq_timer);
73 fq_destroy_all_entries(iovad);
75 free_percpu(iovad->fq);
78 iovad->flush_cb = NULL;
79 iovad->entry_dtor = NULL;
82 int init_iova_flush_queue(struct iova_domain *iovad,
83 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
85 struct iova_fq __percpu *queue;
88 atomic64_set(&iovad->fq_flush_start_cnt, 0);
89 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
91 queue = alloc_percpu(struct iova_fq);
95 iovad->flush_cb = flush_cb;
96 iovad->entry_dtor = entry_dtor;
98 for_each_possible_cpu(cpu) {
101 fq = per_cpu_ptr(queue, cpu);
105 spin_lock_init(&fq->lock);
112 setup_timer(&iovad->fq_timer, fq_flush_timeout, (unsigned long)iovad);
113 atomic_set(&iovad->fq_timer_on, 0);
117 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
119 static struct rb_node *
120 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
122 if ((*limit_pfn > iovad->dma_32bit_pfn) ||
123 (iovad->cached32_node == NULL))
124 return rb_last(&iovad->rbroot);
126 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
127 struct iova *curr_iova =
128 rb_entry(iovad->cached32_node, struct iova, node);
129 *limit_pfn = curr_iova->pfn_lo;
135 __cached_rbnode_insert_update(struct iova_domain *iovad,
136 unsigned long limit_pfn, struct iova *new)
138 if (limit_pfn != iovad->dma_32bit_pfn)
140 iovad->cached32_node = &new->node;
144 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
146 struct iova *cached_iova;
147 struct rb_node *curr;
149 if (!iovad->cached32_node)
151 curr = iovad->cached32_node;
152 cached_iova = rb_entry(curr, struct iova, node);
154 if (free->pfn_lo >= cached_iova->pfn_lo) {
155 struct rb_node *node = rb_next(&free->node);
156 struct iova *iova = rb_entry(node, struct iova, node);
158 /* only cache if it's below 32bit pfn */
159 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
160 iovad->cached32_node = node;
162 iovad->cached32_node = NULL;
166 /* Insert the iova into domain rbtree by holding writer lock */
168 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
169 struct rb_node *start)
171 struct rb_node **new, *parent = NULL;
173 new = (start) ? &start : &(root->rb_node);
174 /* Figure out where to put new node */
176 struct iova *this = rb_entry(*new, struct iova, node);
180 if (iova->pfn_lo < this->pfn_lo)
181 new = &((*new)->rb_left);
182 else if (iova->pfn_lo > this->pfn_lo)
183 new = &((*new)->rb_right);
185 WARN_ON(1); /* this should not happen */
189 /* Add new node and rebalance tree. */
190 rb_link_node(&iova->node, parent, new);
191 rb_insert_color(&iova->node, root);
195 * Computes the padding size required, to make the start address
196 * naturally aligned on the power-of-two order of its size
199 iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
201 return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1);
204 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
205 unsigned long size, unsigned long limit_pfn,
206 struct iova *new, bool size_aligned)
208 struct rb_node *prev, *curr = NULL;
210 unsigned long saved_pfn;
211 unsigned int pad_size = 0;
213 /* Walk the tree backwards */
214 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
215 saved_pfn = limit_pfn;
216 curr = __get_cached_rbnode(iovad, &limit_pfn);
219 struct iova *curr_iova = rb_entry(curr, struct iova, node);
221 if (limit_pfn <= curr_iova->pfn_lo) {
223 } else if (limit_pfn > curr_iova->pfn_hi) {
225 pad_size = iova_get_pad_size(size, limit_pfn);
226 if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn)
227 break; /* found a free slot */
229 limit_pfn = curr_iova->pfn_lo;
232 curr = rb_prev(curr);
237 pad_size = iova_get_pad_size(size, limit_pfn);
238 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
239 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
244 /* pfn_lo will point to size aligned address if size_aligned is set */
245 new->pfn_lo = limit_pfn - (size + pad_size);
246 new->pfn_hi = new->pfn_lo + size - 1;
248 /* If we have 'prev', it's a valid place to start the insertion. */
249 iova_insert_rbtree(&iovad->rbroot, new, prev);
250 __cached_rbnode_insert_update(iovad, saved_pfn, new);
252 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
258 static struct kmem_cache *iova_cache;
259 static unsigned int iova_cache_users;
260 static DEFINE_MUTEX(iova_cache_mutex);
262 struct iova *alloc_iova_mem(void)
264 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
266 EXPORT_SYMBOL(alloc_iova_mem);
268 void free_iova_mem(struct iova *iova)
270 kmem_cache_free(iova_cache, iova);
272 EXPORT_SYMBOL(free_iova_mem);
274 int iova_cache_get(void)
276 mutex_lock(&iova_cache_mutex);
277 if (!iova_cache_users) {
278 iova_cache = kmem_cache_create(
279 "iommu_iova", sizeof(struct iova), 0,
280 SLAB_HWCACHE_ALIGN, NULL);
282 mutex_unlock(&iova_cache_mutex);
283 printk(KERN_ERR "Couldn't create iova cache\n");
289 mutex_unlock(&iova_cache_mutex);
293 EXPORT_SYMBOL_GPL(iova_cache_get);
295 void iova_cache_put(void)
297 mutex_lock(&iova_cache_mutex);
298 if (WARN_ON(!iova_cache_users)) {
299 mutex_unlock(&iova_cache_mutex);
303 if (!iova_cache_users)
304 kmem_cache_destroy(iova_cache);
305 mutex_unlock(&iova_cache_mutex);
307 EXPORT_SYMBOL_GPL(iova_cache_put);
310 * alloc_iova - allocates an iova
311 * @iovad: - iova domain in question
312 * @size: - size of page frames to allocate
313 * @limit_pfn: - max limit address
314 * @size_aligned: - set if size_aligned address range is required
315 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
316 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
317 * flag is set then the allocated address iova->pfn_lo will be naturally
318 * aligned on roundup_power_of_two(size).
321 alloc_iova(struct iova_domain *iovad, unsigned long size,
322 unsigned long limit_pfn,
325 struct iova *new_iova;
328 new_iova = alloc_iova_mem();
332 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
333 new_iova, size_aligned);
336 free_iova_mem(new_iova);
342 EXPORT_SYMBOL_GPL(alloc_iova);
345 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
347 struct rb_node *node = iovad->rbroot.rb_node;
349 assert_spin_locked(&iovad->iova_rbtree_lock);
352 struct iova *iova = rb_entry(node, struct iova, node);
354 /* If pfn falls within iova's range, return iova */
355 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
359 if (pfn < iova->pfn_lo)
360 node = node->rb_left;
361 else if (pfn > iova->pfn_lo)
362 node = node->rb_right;
368 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
370 assert_spin_locked(&iovad->iova_rbtree_lock);
371 __cached_rbnode_delete_update(iovad, iova);
372 rb_erase(&iova->node, &iovad->rbroot);
377 * find_iova - finds an iova for a given pfn
378 * @iovad: - iova domain in question.
379 * @pfn: - page frame number
380 * This function finds and returns an iova belonging to the
381 * given doamin which matches the given pfn.
383 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
388 /* Take the lock so that no other thread is manipulating the rbtree */
389 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
390 iova = private_find_iova(iovad, pfn);
391 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
394 EXPORT_SYMBOL_GPL(find_iova);
397 * __free_iova - frees the given iova
398 * @iovad: iova domain in question.
399 * @iova: iova in question.
400 * Frees the given iova belonging to the giving domain
403 __free_iova(struct iova_domain *iovad, struct iova *iova)
407 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
408 private_free_iova(iovad, iova);
409 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
411 EXPORT_SYMBOL_GPL(__free_iova);
414 * free_iova - finds and frees the iova for a given pfn
415 * @iovad: - iova domain in question.
416 * @pfn: - pfn that is allocated previously
417 * This functions finds an iova for a given pfn and then
418 * frees the iova from that domain.
421 free_iova(struct iova_domain *iovad, unsigned long pfn)
423 struct iova *iova = find_iova(iovad, pfn);
426 __free_iova(iovad, iova);
429 EXPORT_SYMBOL_GPL(free_iova);
432 * alloc_iova_fast - allocates an iova from rcache
433 * @iovad: - iova domain in question
434 * @size: - size of page frames to allocate
435 * @limit_pfn: - max limit address
436 * This function tries to satisfy an iova allocation from the rcache,
437 * and falls back to regular allocation on failure.
440 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
441 unsigned long limit_pfn)
443 bool flushed_rcache = false;
444 unsigned long iova_pfn;
445 struct iova *new_iova;
447 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
452 new_iova = alloc_iova(iovad, size, limit_pfn, true);
459 /* Try replenishing IOVAs by flushing rcache. */
460 flushed_rcache = true;
461 for_each_online_cpu(cpu)
462 free_cpu_cached_iovas(cpu, iovad);
466 return new_iova->pfn_lo;
468 EXPORT_SYMBOL_GPL(alloc_iova_fast);
471 * free_iova_fast - free iova pfn range into rcache
472 * @iovad: - iova domain in question.
473 * @pfn: - pfn that is allocated previously
474 * @size: - # of pages in range
475 * This functions frees an iova range by trying to put it into the rcache,
476 * falling back to regular iova deallocation via free_iova() if this fails.
479 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
481 if (iova_rcache_insert(iovad, pfn, size))
484 free_iova(iovad, pfn);
486 EXPORT_SYMBOL_GPL(free_iova_fast);
488 #define fq_ring_for_each(i, fq) \
489 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
491 static inline bool fq_full(struct iova_fq *fq)
493 assert_spin_locked(&fq->lock);
494 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
497 static inline unsigned fq_ring_add(struct iova_fq *fq)
499 unsigned idx = fq->tail;
501 assert_spin_locked(&fq->lock);
503 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
508 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
510 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
513 assert_spin_locked(&fq->lock);
515 fq_ring_for_each(idx, fq) {
517 if (fq->entries[idx].counter >= counter)
520 if (iovad->entry_dtor)
521 iovad->entry_dtor(fq->entries[idx].data);
523 free_iova_fast(iovad,
524 fq->entries[idx].iova_pfn,
525 fq->entries[idx].pages);
527 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
531 static void iova_domain_flush(struct iova_domain *iovad)
533 atomic64_inc(&iovad->fq_flush_start_cnt);
534 iovad->flush_cb(iovad);
535 atomic64_inc(&iovad->fq_flush_finish_cnt);
538 static void fq_destroy_all_entries(struct iova_domain *iovad)
543 * This code runs when the iova_domain is being detroyed, so don't
544 * bother to free iovas, just call the entry_dtor on all remaining
547 if (!iovad->entry_dtor)
550 for_each_possible_cpu(cpu) {
551 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
554 fq_ring_for_each(idx, fq)
555 iovad->entry_dtor(fq->entries[idx].data);
559 static void fq_flush_timeout(unsigned long data)
561 struct iova_domain *iovad = (struct iova_domain *)data;
564 atomic_set(&iovad->fq_timer_on, 0);
565 iova_domain_flush(iovad);
567 for_each_possible_cpu(cpu) {
571 fq = per_cpu_ptr(iovad->fq, cpu);
572 spin_lock_irqsave(&fq->lock, flags);
573 fq_ring_free(iovad, fq);
574 spin_unlock_irqrestore(&fq->lock, flags);
578 void queue_iova(struct iova_domain *iovad,
579 unsigned long pfn, unsigned long pages,
582 struct iova_fq *fq = get_cpu_ptr(iovad->fq);
586 spin_lock_irqsave(&fq->lock, flags);
589 * First remove all entries from the flush queue that have already been
590 * flushed out on another CPU. This makes the fq_full() check below less
593 fq_ring_free(iovad, fq);
596 iova_domain_flush(iovad);
597 fq_ring_free(iovad, fq);
600 idx = fq_ring_add(fq);
602 fq->entries[idx].iova_pfn = pfn;
603 fq->entries[idx].pages = pages;
604 fq->entries[idx].data = data;
605 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
607 spin_unlock_irqrestore(&fq->lock, flags);
609 /* Avoid false sharing as much as possible. */
610 if (!atomic_read(&iovad->fq_timer_on) &&
611 !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
612 mod_timer(&iovad->fq_timer,
613 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
615 put_cpu_ptr(iovad->fq);
617 EXPORT_SYMBOL_GPL(queue_iova);
620 * put_iova_domain - destroys the iova doamin
621 * @iovad: - iova domain in question.
622 * All the iova's in that domain are destroyed.
624 void put_iova_domain(struct iova_domain *iovad)
626 struct rb_node *node;
629 free_iova_flush_queue(iovad);
630 free_iova_rcaches(iovad);
631 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
632 node = rb_first(&iovad->rbroot);
634 struct iova *iova = rb_entry(node, struct iova, node);
636 rb_erase(node, &iovad->rbroot);
638 node = rb_first(&iovad->rbroot);
640 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
642 EXPORT_SYMBOL_GPL(put_iova_domain);
645 __is_range_overlap(struct rb_node *node,
646 unsigned long pfn_lo, unsigned long pfn_hi)
648 struct iova *iova = rb_entry(node, struct iova, node);
650 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
655 static inline struct iova *
656 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
660 iova = alloc_iova_mem();
662 iova->pfn_lo = pfn_lo;
663 iova->pfn_hi = pfn_hi;
670 __insert_new_range(struct iova_domain *iovad,
671 unsigned long pfn_lo, unsigned long pfn_hi)
675 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
677 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
683 __adjust_overlap_range(struct iova *iova,
684 unsigned long *pfn_lo, unsigned long *pfn_hi)
686 if (*pfn_lo < iova->pfn_lo)
687 iova->pfn_lo = *pfn_lo;
688 if (*pfn_hi > iova->pfn_hi)
689 *pfn_lo = iova->pfn_hi + 1;
693 * reserve_iova - reserves an iova in the given range
694 * @iovad: - iova domain pointer
695 * @pfn_lo: - lower page frame address
696 * @pfn_hi:- higher pfn adderss
697 * This function allocates reserves the address range from pfn_lo to pfn_hi so
698 * that this address is not dished out as part of alloc_iova.
701 reserve_iova(struct iova_domain *iovad,
702 unsigned long pfn_lo, unsigned long pfn_hi)
704 struct rb_node *node;
707 unsigned int overlap = 0;
709 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
710 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
711 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
712 iova = rb_entry(node, struct iova, node);
713 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
714 if ((pfn_lo >= iova->pfn_lo) &&
715 (pfn_hi <= iova->pfn_hi))
723 /* We are here either because this is the first reserver node
724 * or need to insert remaining non overlap addr range
726 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
729 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
732 EXPORT_SYMBOL_GPL(reserve_iova);
735 * copy_reserved_iova - copies the reserved between domains
736 * @from: - source doamin from where to copy
737 * @to: - destination domin where to copy
738 * This function copies reserved iova's from one doamin to
742 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
745 struct rb_node *node;
747 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
748 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
749 struct iova *iova = rb_entry(node, struct iova, node);
750 struct iova *new_iova;
752 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
754 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
755 iova->pfn_lo, iova->pfn_lo);
757 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
759 EXPORT_SYMBOL_GPL(copy_reserved_iova);
762 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
763 unsigned long pfn_lo, unsigned long pfn_hi)
766 struct iova *prev = NULL, *next = NULL;
768 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
769 if (iova->pfn_lo < pfn_lo) {
770 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
774 if (iova->pfn_hi > pfn_hi) {
775 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
780 __cached_rbnode_delete_update(iovad, iova);
781 rb_erase(&iova->node, &iovad->rbroot);
784 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
785 iova->pfn_lo = pfn_lo;
788 iova_insert_rbtree(&iovad->rbroot, next, NULL);
789 iova->pfn_hi = pfn_hi;
791 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
796 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
803 * Magazine caches for IOVA ranges. For an introduction to magazines,
804 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
805 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
806 * For simplicity, we use a static magazine size and don't implement the
807 * dynamic size tuning described in the paper.
810 #define IOVA_MAG_SIZE 128
812 struct iova_magazine {
814 unsigned long pfns[IOVA_MAG_SIZE];
817 struct iova_cpu_rcache {
819 struct iova_magazine *loaded;
820 struct iova_magazine *prev;
823 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
825 return kzalloc(sizeof(struct iova_magazine), flags);
828 static void iova_magazine_free(struct iova_magazine *mag)
834 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
842 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
844 for (i = 0 ; i < mag->size; ++i) {
845 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
850 private_free_iova(iovad, iova);
853 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
858 static bool iova_magazine_full(struct iova_magazine *mag)
860 return (mag && mag->size == IOVA_MAG_SIZE);
863 static bool iova_magazine_empty(struct iova_magazine *mag)
865 return (!mag || mag->size == 0);
868 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
869 unsigned long limit_pfn)
871 BUG_ON(iova_magazine_empty(mag));
873 if (mag->pfns[mag->size - 1] >= limit_pfn)
876 return mag->pfns[--mag->size];
879 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
881 BUG_ON(iova_magazine_full(mag));
883 mag->pfns[mag->size++] = pfn;
886 static void init_iova_rcaches(struct iova_domain *iovad)
888 struct iova_cpu_rcache *cpu_rcache;
889 struct iova_rcache *rcache;
893 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
894 rcache = &iovad->rcaches[i];
895 spin_lock_init(&rcache->lock);
896 rcache->depot_size = 0;
897 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
898 if (WARN_ON(!rcache->cpu_rcaches))
900 for_each_possible_cpu(cpu) {
901 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
902 spin_lock_init(&cpu_rcache->lock);
903 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
904 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
910 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
911 * return true on success. Can fail if rcache is full and we can't free
912 * space, and free_iova() (our only caller) will then return the IOVA
913 * range to the rbtree instead.
915 static bool __iova_rcache_insert(struct iova_domain *iovad,
916 struct iova_rcache *rcache,
917 unsigned long iova_pfn)
919 struct iova_magazine *mag_to_free = NULL;
920 struct iova_cpu_rcache *cpu_rcache;
921 bool can_insert = false;
924 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
925 spin_lock_irqsave(&cpu_rcache->lock, flags);
927 if (!iova_magazine_full(cpu_rcache->loaded)) {
929 } else if (!iova_magazine_full(cpu_rcache->prev)) {
930 swap(cpu_rcache->prev, cpu_rcache->loaded);
933 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
936 spin_lock(&rcache->lock);
937 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
938 rcache->depot[rcache->depot_size++] =
941 mag_to_free = cpu_rcache->loaded;
943 spin_unlock(&rcache->lock);
945 cpu_rcache->loaded = new_mag;
951 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
953 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
956 iova_magazine_free_pfns(mag_to_free, iovad);
957 iova_magazine_free(mag_to_free);
963 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
966 unsigned int log_size = order_base_2(size);
968 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
971 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
975 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
976 * satisfy the request, return a matching non-NULL range and remove
977 * it from the 'rcache'.
979 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
980 unsigned long limit_pfn)
982 struct iova_cpu_rcache *cpu_rcache;
983 unsigned long iova_pfn = 0;
984 bool has_pfn = false;
987 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
988 spin_lock_irqsave(&cpu_rcache->lock, flags);
990 if (!iova_magazine_empty(cpu_rcache->loaded)) {
992 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
993 swap(cpu_rcache->prev, cpu_rcache->loaded);
996 spin_lock(&rcache->lock);
997 if (rcache->depot_size > 0) {
998 iova_magazine_free(cpu_rcache->loaded);
999 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
1002 spin_unlock(&rcache->lock);
1006 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
1008 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1014 * Try to satisfy IOVA allocation range from rcache. Fail if requested
1015 * size is too big or the DMA limit we are given isn't satisfied by the
1016 * top element in the magazine.
1018 static unsigned long iova_rcache_get(struct iova_domain *iovad,
1020 unsigned long limit_pfn)
1022 unsigned int log_size = order_base_2(size);
1024 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
1027 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
1031 * Free a cpu's rcache.
1033 static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
1034 struct iova_rcache *rcache)
1036 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1037 unsigned long flags;
1039 spin_lock_irqsave(&cpu_rcache->lock, flags);
1041 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1042 iova_magazine_free(cpu_rcache->loaded);
1044 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1045 iova_magazine_free(cpu_rcache->prev);
1047 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1051 * free rcache data structures.
1053 static void free_iova_rcaches(struct iova_domain *iovad)
1055 struct iova_rcache *rcache;
1056 unsigned long flags;
1060 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1061 rcache = &iovad->rcaches[i];
1062 for_each_possible_cpu(cpu)
1063 free_cpu_iova_rcache(cpu, iovad, rcache);
1064 spin_lock_irqsave(&rcache->lock, flags);
1065 free_percpu(rcache->cpu_rcaches);
1066 for (j = 0; j < rcache->depot_size; ++j) {
1067 iova_magazine_free_pfns(rcache->depot[j], iovad);
1068 iova_magazine_free(rcache->depot[j]);
1070 spin_unlock_irqrestore(&rcache->lock, flags);
1075 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1077 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1079 struct iova_cpu_rcache *cpu_rcache;
1080 struct iova_rcache *rcache;
1081 unsigned long flags;
1084 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1085 rcache = &iovad->rcaches[i];
1086 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1087 spin_lock_irqsave(&cpu_rcache->lock, flags);
1088 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1089 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1090 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1094 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1095 MODULE_LICENSE("GPL");