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 static void free_iova_flush_queue(struct iova_domain
*iovad
)
73 if (timer_pending(&iovad
->fq_timer
))
74 del_timer(&iovad
->fq_timer
);
76 fq_destroy_all_entries(iovad
);
78 free_percpu(iovad
->fq
);
81 iovad
->flush_cb
= NULL
;
82 iovad
->entry_dtor
= NULL
;
85 int init_iova_flush_queue(struct iova_domain
*iovad
,
86 iova_flush_cb flush_cb
, iova_entry_dtor entry_dtor
)
90 atomic64_set(&iovad
->fq_flush_start_cnt
, 0);
91 atomic64_set(&iovad
->fq_flush_finish_cnt
, 0);
93 iovad
->fq
= alloc_percpu(struct iova_fq
);
97 iovad
->flush_cb
= flush_cb
;
98 iovad
->entry_dtor
= entry_dtor
;
100 for_each_possible_cpu(cpu
) {
103 fq
= per_cpu_ptr(iovad
->fq
, cpu
);
107 spin_lock_init(&fq
->lock
);
110 timer_setup(&iovad
->fq_timer
, fq_flush_timeout
, 0);
111 atomic_set(&iovad
->fq_timer_on
, 0);
115 EXPORT_SYMBOL_GPL(init_iova_flush_queue
);
117 static struct rb_node
*
118 __get_cached_rbnode(struct iova_domain
*iovad
, unsigned long limit_pfn
)
120 if (limit_pfn
<= iovad
->dma_32bit_pfn
)
121 return iovad
->cached32_node
;
123 return iovad
->cached_node
;
127 __cached_rbnode_insert_update(struct iova_domain
*iovad
, struct iova
*new)
129 if (new->pfn_hi
< iovad
->dma_32bit_pfn
)
130 iovad
->cached32_node
= &new->node
;
132 iovad
->cached_node
= &new->node
;
136 __cached_rbnode_delete_update(struct iova_domain
*iovad
, struct iova
*free
)
138 struct iova
*cached_iova
;
140 cached_iova
= rb_entry(iovad
->cached32_node
, struct iova
, node
);
141 if (free
->pfn_hi
< iovad
->dma_32bit_pfn
&&
142 free
->pfn_lo
>= cached_iova
->pfn_lo
)
143 iovad
->cached32_node
= rb_next(&free
->node
);
145 cached_iova
= rb_entry(iovad
->cached_node
, struct iova
, node
);
146 if (free
->pfn_lo
>= cached_iova
->pfn_lo
)
147 iovad
->cached_node
= rb_next(&free
->node
);
150 /* Insert the iova into domain rbtree by holding writer lock */
152 iova_insert_rbtree(struct rb_root
*root
, struct iova
*iova
,
153 struct rb_node
*start
)
155 struct rb_node
**new, *parent
= NULL
;
157 new = (start
) ? &start
: &(root
->rb_node
);
158 /* Figure out where to put new node */
160 struct iova
*this = rb_entry(*new, struct iova
, node
);
164 if (iova
->pfn_lo
< this->pfn_lo
)
165 new = &((*new)->rb_left
);
166 else if (iova
->pfn_lo
> this->pfn_lo
)
167 new = &((*new)->rb_right
);
169 WARN_ON(1); /* this should not happen */
173 /* Add new node and rebalance tree. */
174 rb_link_node(&iova
->node
, parent
, new);
175 rb_insert_color(&iova
->node
, root
);
178 static int __alloc_and_insert_iova_range(struct iova_domain
*iovad
,
179 unsigned long size
, unsigned long limit_pfn
,
180 struct iova
*new, bool size_aligned
)
182 struct rb_node
*curr
, *prev
;
183 struct iova
*curr_iova
;
185 unsigned long new_pfn
;
186 unsigned long align_mask
= ~0UL;
189 align_mask
<<= fls_long(size
- 1);
191 /* Walk the tree backwards */
192 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
193 curr
= __get_cached_rbnode(iovad
, limit_pfn
);
194 curr_iova
= rb_entry(curr
, struct iova
, node
);
196 limit_pfn
= min(limit_pfn
, curr_iova
->pfn_lo
);
197 new_pfn
= (limit_pfn
- size
) & align_mask
;
199 curr
= rb_prev(curr
);
200 curr_iova
= rb_entry(curr
, struct iova
, node
);
201 } while (curr
&& new_pfn
<= curr_iova
->pfn_hi
);
203 if (limit_pfn
< size
|| new_pfn
< iovad
->start_pfn
) {
204 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
208 /* pfn_lo will point to size aligned address if size_aligned is set */
209 new->pfn_lo
= new_pfn
;
210 new->pfn_hi
= new->pfn_lo
+ size
- 1;
212 /* If we have 'prev', it's a valid place to start the insertion. */
213 iova_insert_rbtree(&iovad
->rbroot
, new, prev
);
214 __cached_rbnode_insert_update(iovad
, new);
216 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
222 static struct kmem_cache
*iova_cache
;
223 static unsigned int iova_cache_users
;
224 static DEFINE_MUTEX(iova_cache_mutex
);
226 struct iova
*alloc_iova_mem(void)
228 return kmem_cache_alloc(iova_cache
, GFP_ATOMIC
);
230 EXPORT_SYMBOL(alloc_iova_mem
);
232 void free_iova_mem(struct iova
*iova
)
234 if (iova
->pfn_lo
!= IOVA_ANCHOR
)
235 kmem_cache_free(iova_cache
, iova
);
237 EXPORT_SYMBOL(free_iova_mem
);
239 int iova_cache_get(void)
241 mutex_lock(&iova_cache_mutex
);
242 if (!iova_cache_users
) {
243 iova_cache
= kmem_cache_create(
244 "iommu_iova", sizeof(struct iova
), 0,
245 SLAB_HWCACHE_ALIGN
, NULL
);
247 mutex_unlock(&iova_cache_mutex
);
248 printk(KERN_ERR
"Couldn't create iova cache\n");
254 mutex_unlock(&iova_cache_mutex
);
258 EXPORT_SYMBOL_GPL(iova_cache_get
);
260 void iova_cache_put(void)
262 mutex_lock(&iova_cache_mutex
);
263 if (WARN_ON(!iova_cache_users
)) {
264 mutex_unlock(&iova_cache_mutex
);
268 if (!iova_cache_users
)
269 kmem_cache_destroy(iova_cache
);
270 mutex_unlock(&iova_cache_mutex
);
272 EXPORT_SYMBOL_GPL(iova_cache_put
);
275 * alloc_iova - allocates an iova
276 * @iovad: - iova domain in question
277 * @size: - size of page frames to allocate
278 * @limit_pfn: - max limit address
279 * @size_aligned: - set if size_aligned address range is required
280 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
281 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
282 * flag is set then the allocated address iova->pfn_lo will be naturally
283 * aligned on roundup_power_of_two(size).
286 alloc_iova(struct iova_domain
*iovad
, unsigned long size
,
287 unsigned long limit_pfn
,
290 struct iova
*new_iova
;
293 new_iova
= alloc_iova_mem();
297 ret
= __alloc_and_insert_iova_range(iovad
, size
, limit_pfn
+ 1,
298 new_iova
, size_aligned
);
301 free_iova_mem(new_iova
);
307 EXPORT_SYMBOL_GPL(alloc_iova
);
310 private_find_iova(struct iova_domain
*iovad
, unsigned long pfn
)
312 struct rb_node
*node
= iovad
->rbroot
.rb_node
;
314 assert_spin_locked(&iovad
->iova_rbtree_lock
);
317 struct iova
*iova
= rb_entry(node
, struct iova
, node
);
319 if (pfn
< iova
->pfn_lo
)
320 node
= node
->rb_left
;
321 else if (pfn
> iova
->pfn_hi
)
322 node
= node
->rb_right
;
324 return iova
; /* pfn falls within iova's range */
330 static void private_free_iova(struct iova_domain
*iovad
, struct iova
*iova
)
332 assert_spin_locked(&iovad
->iova_rbtree_lock
);
333 __cached_rbnode_delete_update(iovad
, iova
);
334 rb_erase(&iova
->node
, &iovad
->rbroot
);
339 * find_iova - finds an iova for a given pfn
340 * @iovad: - iova domain in question.
341 * @pfn: - page frame number
342 * This function finds and returns an iova belonging to the
343 * given doamin which matches the given pfn.
345 struct iova
*find_iova(struct iova_domain
*iovad
, unsigned long pfn
)
350 /* Take the lock so that no other thread is manipulating the rbtree */
351 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
352 iova
= private_find_iova(iovad
, pfn
);
353 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
356 EXPORT_SYMBOL_GPL(find_iova
);
359 * __free_iova - frees the given iova
360 * @iovad: iova domain in question.
361 * @iova: iova in question.
362 * Frees the given iova belonging to the giving domain
365 __free_iova(struct iova_domain
*iovad
, struct iova
*iova
)
369 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
370 private_free_iova(iovad
, iova
);
371 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
373 EXPORT_SYMBOL_GPL(__free_iova
);
376 * free_iova - finds and frees the iova for a given pfn
377 * @iovad: - iova domain in question.
378 * @pfn: - pfn that is allocated previously
379 * This functions finds an iova for a given pfn and then
380 * frees the iova from that domain.
383 free_iova(struct iova_domain
*iovad
, unsigned long pfn
)
385 struct iova
*iova
= find_iova(iovad
, pfn
);
388 __free_iova(iovad
, iova
);
391 EXPORT_SYMBOL_GPL(free_iova
);
394 * alloc_iova_fast - allocates an iova from rcache
395 * @iovad: - iova domain in question
396 * @size: - size of page frames to allocate
397 * @limit_pfn: - max limit address
398 * @flush_rcache: - set to flush rcache on regular allocation failure
399 * This function tries to satisfy an iova allocation from the rcache,
400 * and falls back to regular allocation on failure. If regular allocation
401 * fails too and the flush_rcache flag is set then the rcache will be flushed.
404 alloc_iova_fast(struct iova_domain
*iovad
, unsigned long size
,
405 unsigned long limit_pfn
, bool flush_rcache
)
407 unsigned long iova_pfn
;
408 struct iova
*new_iova
;
410 iova_pfn
= iova_rcache_get(iovad
, size
, limit_pfn
+ 1);
415 new_iova
= alloc_iova(iovad
, size
, limit_pfn
, true);
422 /* Try replenishing IOVAs by flushing rcache. */
423 flush_rcache
= false;
424 for_each_online_cpu(cpu
)
425 free_cpu_cached_iovas(cpu
, iovad
);
429 return new_iova
->pfn_lo
;
431 EXPORT_SYMBOL_GPL(alloc_iova_fast
);
434 * free_iova_fast - free iova pfn range into rcache
435 * @iovad: - iova domain in question.
436 * @pfn: - pfn that is allocated previously
437 * @size: - # of pages in range
438 * This functions frees an iova range by trying to put it into the rcache,
439 * falling back to regular iova deallocation via free_iova() if this fails.
442 free_iova_fast(struct iova_domain
*iovad
, unsigned long pfn
, unsigned long size
)
444 if (iova_rcache_insert(iovad
, pfn
, size
))
447 free_iova(iovad
, pfn
);
449 EXPORT_SYMBOL_GPL(free_iova_fast
);
451 #define fq_ring_for_each(i, fq) \
452 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
454 static inline bool fq_full(struct iova_fq
*fq
)
456 assert_spin_locked(&fq
->lock
);
457 return (((fq
->tail
+ 1) % IOVA_FQ_SIZE
) == fq
->head
);
460 static inline unsigned fq_ring_add(struct iova_fq
*fq
)
462 unsigned idx
= fq
->tail
;
464 assert_spin_locked(&fq
->lock
);
466 fq
->tail
= (idx
+ 1) % IOVA_FQ_SIZE
;
471 static void fq_ring_free(struct iova_domain
*iovad
, struct iova_fq
*fq
)
473 u64 counter
= atomic64_read(&iovad
->fq_flush_finish_cnt
);
476 assert_spin_locked(&fq
->lock
);
478 fq_ring_for_each(idx
, fq
) {
480 if (fq
->entries
[idx
].counter
>= counter
)
483 if (iovad
->entry_dtor
)
484 iovad
->entry_dtor(fq
->entries
[idx
].data
);
486 free_iova_fast(iovad
,
487 fq
->entries
[idx
].iova_pfn
,
488 fq
->entries
[idx
].pages
);
490 fq
->head
= (fq
->head
+ 1) % IOVA_FQ_SIZE
;
494 static void iova_domain_flush(struct iova_domain
*iovad
)
496 atomic64_inc(&iovad
->fq_flush_start_cnt
);
497 iovad
->flush_cb(iovad
);
498 atomic64_inc(&iovad
->fq_flush_finish_cnt
);
501 static void fq_destroy_all_entries(struct iova_domain
*iovad
)
506 * This code runs when the iova_domain is being detroyed, so don't
507 * bother to free iovas, just call the entry_dtor on all remaining
510 if (!iovad
->entry_dtor
)
513 for_each_possible_cpu(cpu
) {
514 struct iova_fq
*fq
= per_cpu_ptr(iovad
->fq
, cpu
);
517 fq_ring_for_each(idx
, fq
)
518 iovad
->entry_dtor(fq
->entries
[idx
].data
);
522 static void fq_flush_timeout(struct timer_list
*t
)
524 struct iova_domain
*iovad
= from_timer(iovad
, t
, fq_timer
);
527 atomic_set(&iovad
->fq_timer_on
, 0);
528 iova_domain_flush(iovad
);
530 for_each_possible_cpu(cpu
) {
534 fq
= per_cpu_ptr(iovad
->fq
, cpu
);
535 spin_lock_irqsave(&fq
->lock
, flags
);
536 fq_ring_free(iovad
, fq
);
537 spin_unlock_irqrestore(&fq
->lock
, flags
);
541 void queue_iova(struct iova_domain
*iovad
,
542 unsigned long pfn
, unsigned long pages
,
545 struct iova_fq
*fq
= raw_cpu_ptr(iovad
->fq
);
549 spin_lock_irqsave(&fq
->lock
, flags
);
552 * First remove all entries from the flush queue that have already been
553 * flushed out on another CPU. This makes the fq_full() check below less
556 fq_ring_free(iovad
, fq
);
559 iova_domain_flush(iovad
);
560 fq_ring_free(iovad
, fq
);
563 idx
= fq_ring_add(fq
);
565 fq
->entries
[idx
].iova_pfn
= pfn
;
566 fq
->entries
[idx
].pages
= pages
;
567 fq
->entries
[idx
].data
= data
;
568 fq
->entries
[idx
].counter
= atomic64_read(&iovad
->fq_flush_start_cnt
);
570 spin_unlock_irqrestore(&fq
->lock
, flags
);
572 if (atomic_cmpxchg(&iovad
->fq_timer_on
, 0, 1) == 0)
573 mod_timer(&iovad
->fq_timer
,
574 jiffies
+ msecs_to_jiffies(IOVA_FQ_TIMEOUT
));
576 EXPORT_SYMBOL_GPL(queue_iova
);
579 * put_iova_domain - destroys the iova doamin
580 * @iovad: - iova domain in question.
581 * All the iova's in that domain are destroyed.
583 void put_iova_domain(struct iova_domain
*iovad
)
585 struct iova
*iova
, *tmp
;
587 free_iova_flush_queue(iovad
);
588 free_iova_rcaches(iovad
);
589 rbtree_postorder_for_each_entry_safe(iova
, tmp
, &iovad
->rbroot
, node
)
592 EXPORT_SYMBOL_GPL(put_iova_domain
);
595 __is_range_overlap(struct rb_node
*node
,
596 unsigned long pfn_lo
, unsigned long pfn_hi
)
598 struct iova
*iova
= rb_entry(node
, struct iova
, node
);
600 if ((pfn_lo
<= iova
->pfn_hi
) && (pfn_hi
>= iova
->pfn_lo
))
605 static inline struct iova
*
606 alloc_and_init_iova(unsigned long pfn_lo
, unsigned long pfn_hi
)
610 iova
= alloc_iova_mem();
612 iova
->pfn_lo
= pfn_lo
;
613 iova
->pfn_hi
= pfn_hi
;
620 __insert_new_range(struct iova_domain
*iovad
,
621 unsigned long pfn_lo
, unsigned long pfn_hi
)
625 iova
= alloc_and_init_iova(pfn_lo
, pfn_hi
);
627 iova_insert_rbtree(&iovad
->rbroot
, iova
, NULL
);
633 __adjust_overlap_range(struct iova
*iova
,
634 unsigned long *pfn_lo
, unsigned long *pfn_hi
)
636 if (*pfn_lo
< iova
->pfn_lo
)
637 iova
->pfn_lo
= *pfn_lo
;
638 if (*pfn_hi
> iova
->pfn_hi
)
639 *pfn_lo
= iova
->pfn_hi
+ 1;
643 * reserve_iova - reserves an iova in the given range
644 * @iovad: - iova domain pointer
645 * @pfn_lo: - lower page frame address
646 * @pfn_hi:- higher pfn adderss
647 * This function allocates reserves the address range from pfn_lo to pfn_hi so
648 * that this address is not dished out as part of alloc_iova.
651 reserve_iova(struct iova_domain
*iovad
,
652 unsigned long pfn_lo
, unsigned long pfn_hi
)
654 struct rb_node
*node
;
657 unsigned int overlap
= 0;
659 /* Don't allow nonsensical pfns */
660 if (WARN_ON((pfn_hi
| pfn_lo
) > (ULLONG_MAX
>> iova_shift(iovad
))))
663 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
664 for (node
= rb_first(&iovad
->rbroot
); node
; node
= rb_next(node
)) {
665 if (__is_range_overlap(node
, pfn_lo
, pfn_hi
)) {
666 iova
= rb_entry(node
, struct iova
, node
);
667 __adjust_overlap_range(iova
, &pfn_lo
, &pfn_hi
);
668 if ((pfn_lo
>= iova
->pfn_lo
) &&
669 (pfn_hi
<= iova
->pfn_hi
))
677 /* We are here either because this is the first reserver node
678 * or need to insert remaining non overlap addr range
680 iova
= __insert_new_range(iovad
, pfn_lo
, pfn_hi
);
683 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
686 EXPORT_SYMBOL_GPL(reserve_iova
);
689 * copy_reserved_iova - copies the reserved between domains
690 * @from: - source doamin from where to copy
691 * @to: - destination domin where to copy
692 * This function copies reserved iova's from one doamin to
696 copy_reserved_iova(struct iova_domain
*from
, struct iova_domain
*to
)
699 struct rb_node
*node
;
701 spin_lock_irqsave(&from
->iova_rbtree_lock
, flags
);
702 for (node
= rb_first(&from
->rbroot
); node
; node
= rb_next(node
)) {
703 struct iova
*iova
= rb_entry(node
, struct iova
, node
);
704 struct iova
*new_iova
;
706 if (iova
->pfn_lo
== IOVA_ANCHOR
)
709 new_iova
= reserve_iova(to
, iova
->pfn_lo
, iova
->pfn_hi
);
711 printk(KERN_ERR
"Reserve iova range %lx@%lx failed\n",
712 iova
->pfn_lo
, iova
->pfn_lo
);
714 spin_unlock_irqrestore(&from
->iova_rbtree_lock
, flags
);
716 EXPORT_SYMBOL_GPL(copy_reserved_iova
);
719 split_and_remove_iova(struct iova_domain
*iovad
, struct iova
*iova
,
720 unsigned long pfn_lo
, unsigned long pfn_hi
)
723 struct iova
*prev
= NULL
, *next
= NULL
;
725 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
726 if (iova
->pfn_lo
< pfn_lo
) {
727 prev
= alloc_and_init_iova(iova
->pfn_lo
, pfn_lo
- 1);
731 if (iova
->pfn_hi
> pfn_hi
) {
732 next
= alloc_and_init_iova(pfn_hi
+ 1, iova
->pfn_hi
);
737 __cached_rbnode_delete_update(iovad
, iova
);
738 rb_erase(&iova
->node
, &iovad
->rbroot
);
741 iova_insert_rbtree(&iovad
->rbroot
, prev
, NULL
);
742 iova
->pfn_lo
= pfn_lo
;
745 iova_insert_rbtree(&iovad
->rbroot
, next
, NULL
);
746 iova
->pfn_hi
= pfn_hi
;
748 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
753 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
760 * Magazine caches for IOVA ranges. For an introduction to magazines,
761 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
762 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
763 * For simplicity, we use a static magazine size and don't implement the
764 * dynamic size tuning described in the paper.
767 #define IOVA_MAG_SIZE 128
769 struct iova_magazine
{
771 unsigned long pfns
[IOVA_MAG_SIZE
];
774 struct iova_cpu_rcache
{
776 struct iova_magazine
*loaded
;
777 struct iova_magazine
*prev
;
780 static struct iova_magazine
*iova_magazine_alloc(gfp_t flags
)
782 return kzalloc(sizeof(struct iova_magazine
), flags
);
785 static void iova_magazine_free(struct iova_magazine
*mag
)
791 iova_magazine_free_pfns(struct iova_magazine
*mag
, struct iova_domain
*iovad
)
799 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
801 for (i
= 0 ; i
< mag
->size
; ++i
) {
802 struct iova
*iova
= private_find_iova(iovad
, mag
->pfns
[i
]);
805 private_free_iova(iovad
, iova
);
808 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
813 static bool iova_magazine_full(struct iova_magazine
*mag
)
815 return (mag
&& mag
->size
== IOVA_MAG_SIZE
);
818 static bool iova_magazine_empty(struct iova_magazine
*mag
)
820 return (!mag
|| mag
->size
== 0);
823 static unsigned long iova_magazine_pop(struct iova_magazine
*mag
,
824 unsigned long limit_pfn
)
829 BUG_ON(iova_magazine_empty(mag
));
831 /* Only fall back to the rbtree if we have no suitable pfns at all */
832 for (i
= mag
->size
- 1; mag
->pfns
[i
] > limit_pfn
; i
--)
836 /* Swap it to pop it */
838 mag
->pfns
[i
] = mag
->pfns
[--mag
->size
];
843 static void iova_magazine_push(struct iova_magazine
*mag
, unsigned long pfn
)
845 BUG_ON(iova_magazine_full(mag
));
847 mag
->pfns
[mag
->size
++] = pfn
;
850 static void init_iova_rcaches(struct iova_domain
*iovad
)
852 struct iova_cpu_rcache
*cpu_rcache
;
853 struct iova_rcache
*rcache
;
857 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
858 rcache
= &iovad
->rcaches
[i
];
859 spin_lock_init(&rcache
->lock
);
860 rcache
->depot_size
= 0;
861 rcache
->cpu_rcaches
= __alloc_percpu(sizeof(*cpu_rcache
), cache_line_size());
862 if (WARN_ON(!rcache
->cpu_rcaches
))
864 for_each_possible_cpu(cpu
) {
865 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
866 spin_lock_init(&cpu_rcache
->lock
);
867 cpu_rcache
->loaded
= iova_magazine_alloc(GFP_KERNEL
);
868 cpu_rcache
->prev
= iova_magazine_alloc(GFP_KERNEL
);
874 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
875 * return true on success. Can fail if rcache is full and we can't free
876 * space, and free_iova() (our only caller) will then return the IOVA
877 * range to the rbtree instead.
879 static bool __iova_rcache_insert(struct iova_domain
*iovad
,
880 struct iova_rcache
*rcache
,
881 unsigned long iova_pfn
)
883 struct iova_magazine
*mag_to_free
= NULL
;
884 struct iova_cpu_rcache
*cpu_rcache
;
885 bool can_insert
= false;
888 cpu_rcache
= raw_cpu_ptr(rcache
->cpu_rcaches
);
889 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
891 if (!iova_magazine_full(cpu_rcache
->loaded
)) {
893 } else if (!iova_magazine_full(cpu_rcache
->prev
)) {
894 swap(cpu_rcache
->prev
, cpu_rcache
->loaded
);
897 struct iova_magazine
*new_mag
= iova_magazine_alloc(GFP_ATOMIC
);
900 spin_lock(&rcache
->lock
);
901 if (rcache
->depot_size
< MAX_GLOBAL_MAGS
) {
902 rcache
->depot
[rcache
->depot_size
++] =
905 mag_to_free
= cpu_rcache
->loaded
;
907 spin_unlock(&rcache
->lock
);
909 cpu_rcache
->loaded
= new_mag
;
915 iova_magazine_push(cpu_rcache
->loaded
, iova_pfn
);
917 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
920 iova_magazine_free_pfns(mag_to_free
, iovad
);
921 iova_magazine_free(mag_to_free
);
927 static bool iova_rcache_insert(struct iova_domain
*iovad
, unsigned long pfn
,
930 unsigned int log_size
= order_base_2(size
);
932 if (log_size
>= IOVA_RANGE_CACHE_MAX_SIZE
)
935 return __iova_rcache_insert(iovad
, &iovad
->rcaches
[log_size
], pfn
);
939 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
940 * satisfy the request, return a matching non-NULL range and remove
941 * it from the 'rcache'.
943 static unsigned long __iova_rcache_get(struct iova_rcache
*rcache
,
944 unsigned long limit_pfn
)
946 struct iova_cpu_rcache
*cpu_rcache
;
947 unsigned long iova_pfn
= 0;
948 bool has_pfn
= false;
951 cpu_rcache
= raw_cpu_ptr(rcache
->cpu_rcaches
);
952 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
954 if (!iova_magazine_empty(cpu_rcache
->loaded
)) {
956 } else if (!iova_magazine_empty(cpu_rcache
->prev
)) {
957 swap(cpu_rcache
->prev
, cpu_rcache
->loaded
);
960 spin_lock(&rcache
->lock
);
961 if (rcache
->depot_size
> 0) {
962 iova_magazine_free(cpu_rcache
->loaded
);
963 cpu_rcache
->loaded
= rcache
->depot
[--rcache
->depot_size
];
966 spin_unlock(&rcache
->lock
);
970 iova_pfn
= iova_magazine_pop(cpu_rcache
->loaded
, limit_pfn
);
972 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
978 * Try to satisfy IOVA allocation range from rcache. Fail if requested
979 * size is too big or the DMA limit we are given isn't satisfied by the
980 * top element in the magazine.
982 static unsigned long iova_rcache_get(struct iova_domain
*iovad
,
984 unsigned long limit_pfn
)
986 unsigned int log_size
= order_base_2(size
);
988 if (log_size
>= IOVA_RANGE_CACHE_MAX_SIZE
)
991 return __iova_rcache_get(&iovad
->rcaches
[log_size
], limit_pfn
- size
);
995 * free rcache data structures.
997 static void free_iova_rcaches(struct iova_domain
*iovad
)
999 struct iova_rcache
*rcache
;
1000 struct iova_cpu_rcache
*cpu_rcache
;
1004 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
1005 rcache
= &iovad
->rcaches
[i
];
1006 for_each_possible_cpu(cpu
) {
1007 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
1008 iova_magazine_free(cpu_rcache
->loaded
);
1009 iova_magazine_free(cpu_rcache
->prev
);
1011 free_percpu(rcache
->cpu_rcaches
);
1012 for (j
= 0; j
< rcache
->depot_size
; ++j
)
1013 iova_magazine_free(rcache
->depot
[j
]);
1018 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1020 void free_cpu_cached_iovas(unsigned int cpu
, struct iova_domain
*iovad
)
1022 struct iova_cpu_rcache
*cpu_rcache
;
1023 struct iova_rcache
*rcache
;
1024 unsigned long flags
;
1027 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
1028 rcache
= &iovad
->rcaches
[i
];
1029 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
1030 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
1031 iova_magazine_free_pfns(cpu_rcache
->loaded
, iovad
);
1032 iova_magazine_free_pfns(cpu_rcache
->prev
, iovad
);
1033 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
1037 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1038 MODULE_LICENSE("GPL");