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 /* The anchor node sits above the top of the usable address space */
28 #define IOVA_ANCHOR ~0UL
30 static bool iova_rcache_insert(struct iova_domain *iovad,
33 static unsigned long iova_rcache_get(struct iova_domain *iovad,
35 unsigned long limit_pfn);
36 static void init_iova_rcaches(struct iova_domain *iovad);
37 static void free_iova_rcaches(struct iova_domain *iovad);
38 static void fq_destroy_all_entries(struct iova_domain *iovad);
39 static void fq_flush_timeout(struct timer_list *t);
42 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
43 unsigned long start_pfn)
46 * IOVA granularity will normally be equal to the smallest
47 * supported IOMMU page size; both *must* be capable of
48 * representing individual CPU pages exactly.
50 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
52 spin_lock_init(&iovad->iova_rbtree_lock);
53 iovad->rbroot = RB_ROOT;
54 iovad->cached_node = &iovad->anchor.node;
55 iovad->cached32_node = &iovad->anchor.node;
56 iovad->granule = granule;
57 iovad->start_pfn = start_pfn;
58 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
59 iovad->flush_cb = NULL;
61 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
62 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
63 rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
64 init_iova_rcaches(iovad);
66 EXPORT_SYMBOL_GPL(init_iova_domain);
68 bool has_iova_flush_queue(struct iova_domain *iovad)
73 static void free_iova_flush_queue(struct iova_domain *iovad)
75 if (!has_iova_flush_queue(iovad))
78 del_timer_sync(&iovad->fq_timer);
80 fq_destroy_all_entries(iovad);
82 free_percpu(iovad->fq);
85 iovad->flush_cb = NULL;
86 iovad->entry_dtor = NULL;
89 int init_iova_flush_queue(struct iova_domain *iovad,
90 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
92 struct iova_fq __percpu *queue;
95 atomic64_set(&iovad->fq_flush_start_cnt, 0);
96 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
98 queue = alloc_percpu(struct iova_fq);
102 iovad->flush_cb = flush_cb;
103 iovad->entry_dtor = entry_dtor;
105 for_each_possible_cpu(cpu) {
108 fq = per_cpu_ptr(queue, cpu);
112 spin_lock_init(&fq->lock);
119 timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
120 atomic_set(&iovad->fq_timer_on, 0);
124 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
126 static struct rb_node *
127 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
129 if (limit_pfn <= iovad->dma_32bit_pfn)
130 return iovad->cached32_node;
132 return iovad->cached_node;
136 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
138 if (new->pfn_hi < iovad->dma_32bit_pfn)
139 iovad->cached32_node = &new->node;
141 iovad->cached_node = &new->node;
145 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
147 struct iova *cached_iova;
149 cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
150 if (free == cached_iova ||
151 (free->pfn_hi < iovad->dma_32bit_pfn &&
152 free->pfn_lo >= cached_iova->pfn_lo))
153 iovad->cached32_node = rb_next(&free->node);
155 cached_iova = rb_entry(iovad->cached_node, struct iova, node);
156 if (free->pfn_lo >= cached_iova->pfn_lo)
157 iovad->cached_node = rb_next(&free->node);
160 /* Insert the iova into domain rbtree by holding writer lock */
162 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
163 struct rb_node *start)
165 struct rb_node **new, *parent = NULL;
167 new = (start) ? &start : &(root->rb_node);
168 /* Figure out where to put new node */
170 struct iova *this = rb_entry(*new, struct iova, node);
174 if (iova->pfn_lo < this->pfn_lo)
175 new = &((*new)->rb_left);
176 else if (iova->pfn_lo > this->pfn_lo)
177 new = &((*new)->rb_right);
179 WARN_ON(1); /* this should not happen */
183 /* Add new node and rebalance tree. */
184 rb_link_node(&iova->node, parent, new);
185 rb_insert_color(&iova->node, root);
188 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
189 unsigned long size, unsigned long limit_pfn,
190 struct iova *new, bool size_aligned)
192 struct rb_node *curr, *prev;
193 struct iova *curr_iova;
195 unsigned long new_pfn;
196 unsigned long align_mask = ~0UL;
199 align_mask <<= fls_long(size - 1);
201 /* Walk the tree backwards */
202 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
203 curr = __get_cached_rbnode(iovad, limit_pfn);
204 curr_iova = rb_entry(curr, struct iova, node);
206 limit_pfn = min(limit_pfn, curr_iova->pfn_lo);
207 new_pfn = (limit_pfn - size) & align_mask;
209 curr = rb_prev(curr);
210 curr_iova = rb_entry(curr, struct iova, node);
211 } while (curr && new_pfn <= curr_iova->pfn_hi);
213 if (limit_pfn < size || new_pfn < iovad->start_pfn) {
214 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
218 /* pfn_lo will point to size aligned address if size_aligned is set */
219 new->pfn_lo = new_pfn;
220 new->pfn_hi = new->pfn_lo + size - 1;
222 /* If we have 'prev', it's a valid place to start the insertion. */
223 iova_insert_rbtree(&iovad->rbroot, new, prev);
224 __cached_rbnode_insert_update(iovad, new);
226 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
232 static struct kmem_cache *iova_cache;
233 static unsigned int iova_cache_users;
234 static DEFINE_MUTEX(iova_cache_mutex);
236 struct iova *alloc_iova_mem(void)
238 return kmem_cache_zalloc(iova_cache, GFP_ATOMIC);
240 EXPORT_SYMBOL(alloc_iova_mem);
242 void free_iova_mem(struct iova *iova)
244 if (iova->pfn_lo != IOVA_ANCHOR)
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 * @flush_rcache: - set to flush rcache on regular allocation failure
409 * This function tries to satisfy an iova allocation from the rcache,
410 * and falls back to regular allocation on failure. If regular allocation
411 * fails too and the flush_rcache flag is set then the rcache will be flushed.
414 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
415 unsigned long limit_pfn, bool flush_rcache)
417 unsigned long iova_pfn;
418 struct iova *new_iova;
420 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
425 new_iova = alloc_iova(iovad, size, limit_pfn, true);
432 /* Try replenishing IOVAs by flushing rcache. */
433 flush_rcache = false;
434 for_each_online_cpu(cpu)
435 free_cpu_cached_iovas(cpu, iovad);
439 return new_iova->pfn_lo;
441 EXPORT_SYMBOL_GPL(alloc_iova_fast);
444 * free_iova_fast - free iova pfn range into rcache
445 * @iovad: - iova domain in question.
446 * @pfn: - pfn that is allocated previously
447 * @size: - # of pages in range
448 * This functions frees an iova range by trying to put it into the rcache,
449 * falling back to regular iova deallocation via free_iova() if this fails.
452 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
454 if (iova_rcache_insert(iovad, pfn, size))
457 free_iova(iovad, pfn);
459 EXPORT_SYMBOL_GPL(free_iova_fast);
461 #define fq_ring_for_each(i, fq) \
462 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
464 static inline bool fq_full(struct iova_fq *fq)
466 assert_spin_locked(&fq->lock);
467 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
470 static inline unsigned fq_ring_add(struct iova_fq *fq)
472 unsigned idx = fq->tail;
474 assert_spin_locked(&fq->lock);
476 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
481 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
483 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
486 assert_spin_locked(&fq->lock);
488 fq_ring_for_each(idx, fq) {
490 if (fq->entries[idx].counter >= counter)
493 if (iovad->entry_dtor)
494 iovad->entry_dtor(fq->entries[idx].data);
496 free_iova_fast(iovad,
497 fq->entries[idx].iova_pfn,
498 fq->entries[idx].pages);
500 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
504 static void iova_domain_flush(struct iova_domain *iovad)
506 atomic64_inc(&iovad->fq_flush_start_cnt);
507 iovad->flush_cb(iovad);
508 atomic64_inc(&iovad->fq_flush_finish_cnt);
511 static void fq_destroy_all_entries(struct iova_domain *iovad)
516 * This code runs when the iova_domain is being detroyed, so don't
517 * bother to free iovas, just call the entry_dtor on all remaining
520 if (!iovad->entry_dtor)
523 for_each_possible_cpu(cpu) {
524 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
527 fq_ring_for_each(idx, fq)
528 iovad->entry_dtor(fq->entries[idx].data);
532 static void fq_flush_timeout(struct timer_list *t)
534 struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
537 atomic_set(&iovad->fq_timer_on, 0);
538 iova_domain_flush(iovad);
540 for_each_possible_cpu(cpu) {
544 fq = per_cpu_ptr(iovad->fq, cpu);
545 spin_lock_irqsave(&fq->lock, flags);
546 fq_ring_free(iovad, fq);
547 spin_unlock_irqrestore(&fq->lock, flags);
551 void queue_iova(struct iova_domain *iovad,
552 unsigned long pfn, unsigned long pages,
555 struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
559 spin_lock_irqsave(&fq->lock, flags);
562 * First remove all entries from the flush queue that have already been
563 * flushed out on another CPU. This makes the fq_full() check below less
566 fq_ring_free(iovad, fq);
569 iova_domain_flush(iovad);
570 fq_ring_free(iovad, fq);
573 idx = fq_ring_add(fq);
575 fq->entries[idx].iova_pfn = pfn;
576 fq->entries[idx].pages = pages;
577 fq->entries[idx].data = data;
578 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
580 spin_unlock_irqrestore(&fq->lock, flags);
582 /* Avoid false sharing as much as possible. */
583 if (!atomic_read(&iovad->fq_timer_on) &&
584 !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
585 mod_timer(&iovad->fq_timer,
586 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
588 EXPORT_SYMBOL_GPL(queue_iova);
591 * put_iova_domain - destroys the iova doamin
592 * @iovad: - iova domain in question.
593 * All the iova's in that domain are destroyed.
595 void put_iova_domain(struct iova_domain *iovad)
597 struct iova *iova, *tmp;
599 free_iova_flush_queue(iovad);
600 free_iova_rcaches(iovad);
601 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
604 EXPORT_SYMBOL_GPL(put_iova_domain);
607 __is_range_overlap(struct rb_node *node,
608 unsigned long pfn_lo, unsigned long pfn_hi)
610 struct iova *iova = rb_entry(node, struct iova, node);
612 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
617 static inline struct iova *
618 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
622 iova = alloc_iova_mem();
624 iova->pfn_lo = pfn_lo;
625 iova->pfn_hi = pfn_hi;
632 __insert_new_range(struct iova_domain *iovad,
633 unsigned long pfn_lo, unsigned long pfn_hi)
637 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
639 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
645 __adjust_overlap_range(struct iova *iova,
646 unsigned long *pfn_lo, unsigned long *pfn_hi)
648 if (*pfn_lo < iova->pfn_lo)
649 iova->pfn_lo = *pfn_lo;
650 if (*pfn_hi > iova->pfn_hi)
651 *pfn_lo = iova->pfn_hi + 1;
655 * reserve_iova - reserves an iova in the given range
656 * @iovad: - iova domain pointer
657 * @pfn_lo: - lower page frame address
658 * @pfn_hi:- higher pfn adderss
659 * This function allocates reserves the address range from pfn_lo to pfn_hi so
660 * that this address is not dished out as part of alloc_iova.
663 reserve_iova(struct iova_domain *iovad,
664 unsigned long pfn_lo, unsigned long pfn_hi)
666 struct rb_node *node;
669 unsigned int overlap = 0;
671 /* Don't allow nonsensical pfns */
672 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
675 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
676 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
677 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
678 iova = rb_entry(node, struct iova, node);
679 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
680 if ((pfn_lo >= iova->pfn_lo) &&
681 (pfn_hi <= iova->pfn_hi))
689 /* We are here either because this is the first reserver node
690 * or need to insert remaining non overlap addr range
692 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
695 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
698 EXPORT_SYMBOL_GPL(reserve_iova);
701 * copy_reserved_iova - copies the reserved between domains
702 * @from: - source doamin from where to copy
703 * @to: - destination domin where to copy
704 * This function copies reserved iova's from one doamin to
708 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
711 struct rb_node *node;
713 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
714 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
715 struct iova *iova = rb_entry(node, struct iova, node);
716 struct iova *new_iova;
718 if (iova->pfn_lo == IOVA_ANCHOR)
721 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
723 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
724 iova->pfn_lo, iova->pfn_lo);
726 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
728 EXPORT_SYMBOL_GPL(copy_reserved_iova);
731 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
732 unsigned long pfn_lo, unsigned long pfn_hi)
735 struct iova *prev = NULL, *next = NULL;
737 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
738 if (iova->pfn_lo < pfn_lo) {
739 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
743 if (iova->pfn_hi > pfn_hi) {
744 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
749 __cached_rbnode_delete_update(iovad, iova);
750 rb_erase(&iova->node, &iovad->rbroot);
753 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
754 iova->pfn_lo = pfn_lo;
757 iova_insert_rbtree(&iovad->rbroot, next, NULL);
758 iova->pfn_hi = pfn_hi;
760 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
765 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
772 * Magazine caches for IOVA ranges. For an introduction to magazines,
773 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
774 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
775 * For simplicity, we use a static magazine size and don't implement the
776 * dynamic size tuning described in the paper.
779 #define IOVA_MAG_SIZE 128
781 struct iova_magazine {
783 unsigned long pfns[IOVA_MAG_SIZE];
786 struct iova_cpu_rcache {
788 struct iova_magazine *loaded;
789 struct iova_magazine *prev;
792 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
794 return kzalloc(sizeof(struct iova_magazine), flags);
797 static void iova_magazine_free(struct iova_magazine *mag)
803 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
811 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
813 for (i = 0 ; i < mag->size; ++i) {
814 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
819 private_free_iova(iovad, iova);
822 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
827 static bool iova_magazine_full(struct iova_magazine *mag)
829 return (mag && mag->size == IOVA_MAG_SIZE);
832 static bool iova_magazine_empty(struct iova_magazine *mag)
834 return (!mag || mag->size == 0);
837 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
838 unsigned long limit_pfn)
843 BUG_ON(iova_magazine_empty(mag));
845 /* Only fall back to the rbtree if we have no suitable pfns at all */
846 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
850 /* Swap it to pop it */
852 mag->pfns[i] = mag->pfns[--mag->size];
857 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
859 BUG_ON(iova_magazine_full(mag));
861 mag->pfns[mag->size++] = pfn;
864 static void init_iova_rcaches(struct iova_domain *iovad)
866 struct iova_cpu_rcache *cpu_rcache;
867 struct iova_rcache *rcache;
871 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
872 rcache = &iovad->rcaches[i];
873 spin_lock_init(&rcache->lock);
874 rcache->depot_size = 0;
875 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
876 if (WARN_ON(!rcache->cpu_rcaches))
878 for_each_possible_cpu(cpu) {
879 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
880 spin_lock_init(&cpu_rcache->lock);
881 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
882 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
888 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
889 * return true on success. Can fail if rcache is full and we can't free
890 * space, and free_iova() (our only caller) will then return the IOVA
891 * range to the rbtree instead.
893 static bool __iova_rcache_insert(struct iova_domain *iovad,
894 struct iova_rcache *rcache,
895 unsigned long iova_pfn)
897 struct iova_magazine *mag_to_free = NULL;
898 struct iova_cpu_rcache *cpu_rcache;
899 bool can_insert = false;
902 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
903 spin_lock_irqsave(&cpu_rcache->lock, flags);
905 if (!iova_magazine_full(cpu_rcache->loaded)) {
907 } else if (!iova_magazine_full(cpu_rcache->prev)) {
908 swap(cpu_rcache->prev, cpu_rcache->loaded);
911 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
914 spin_lock(&rcache->lock);
915 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
916 rcache->depot[rcache->depot_size++] =
919 mag_to_free = cpu_rcache->loaded;
921 spin_unlock(&rcache->lock);
923 cpu_rcache->loaded = new_mag;
929 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
931 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
934 iova_magazine_free_pfns(mag_to_free, iovad);
935 iova_magazine_free(mag_to_free);
941 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
944 unsigned int log_size = order_base_2(size);
946 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
949 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
953 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
954 * satisfy the request, return a matching non-NULL range and remove
955 * it from the 'rcache'.
957 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
958 unsigned long limit_pfn)
960 struct iova_cpu_rcache *cpu_rcache;
961 unsigned long iova_pfn = 0;
962 bool has_pfn = false;
965 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
966 spin_lock_irqsave(&cpu_rcache->lock, flags);
968 if (!iova_magazine_empty(cpu_rcache->loaded)) {
970 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
971 swap(cpu_rcache->prev, cpu_rcache->loaded);
974 spin_lock(&rcache->lock);
975 if (rcache->depot_size > 0) {
976 iova_magazine_free(cpu_rcache->loaded);
977 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
980 spin_unlock(&rcache->lock);
984 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
986 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
992 * Try to satisfy IOVA allocation range from rcache. Fail if requested
993 * size is too big or the DMA limit we are given isn't satisfied by the
994 * top element in the magazine.
996 static unsigned long iova_rcache_get(struct iova_domain *iovad,
998 unsigned long limit_pfn)
1000 unsigned int log_size = order_base_2(size);
1002 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
1005 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
1009 * free rcache data structures.
1011 static void free_iova_rcaches(struct iova_domain *iovad)
1013 struct iova_rcache *rcache;
1014 struct iova_cpu_rcache *cpu_rcache;
1018 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1019 rcache = &iovad->rcaches[i];
1020 for_each_possible_cpu(cpu) {
1021 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1022 iova_magazine_free(cpu_rcache->loaded);
1023 iova_magazine_free(cpu_rcache->prev);
1025 free_percpu(rcache->cpu_rcaches);
1026 for (j = 0; j < rcache->depot_size; ++j)
1027 iova_magazine_free(rcache->depot[j]);
1032 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1034 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1036 struct iova_cpu_rcache *cpu_rcache;
1037 struct iova_rcache *rcache;
1038 unsigned long flags;
1041 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1042 rcache = &iovad->rcaches[i];
1043 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1044 spin_lock_irqsave(&cpu_rcache->lock, flags);
1045 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1046 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1047 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1051 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1052 MODULE_LICENSE("GPL");