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 if (timer_pending(&iovad
->fq_timer
))
79 del_timer(&iovad
->fq_timer
);
81 fq_destroy_all_entries(iovad
);
83 free_percpu(iovad
->fq
);
86 iovad
->flush_cb
= NULL
;
87 iovad
->entry_dtor
= NULL
;
90 int init_iova_flush_queue(struct iova_domain
*iovad
,
91 iova_flush_cb flush_cb
, iova_entry_dtor entry_dtor
)
93 struct iova_fq __percpu
*queue
;
96 atomic64_set(&iovad
->fq_flush_start_cnt
, 0);
97 atomic64_set(&iovad
->fq_flush_finish_cnt
, 0);
99 queue
= alloc_percpu(struct iova_fq
);
103 iovad
->flush_cb
= flush_cb
;
104 iovad
->entry_dtor
= entry_dtor
;
106 for_each_possible_cpu(cpu
) {
109 fq
= per_cpu_ptr(queue
, cpu
);
113 spin_lock_init(&fq
->lock
);
120 timer_setup(&iovad
->fq_timer
, fq_flush_timeout
, 0);
121 atomic_set(&iovad
->fq_timer_on
, 0);
125 EXPORT_SYMBOL_GPL(init_iova_flush_queue
);
127 static struct rb_node
*
128 __get_cached_rbnode(struct iova_domain
*iovad
, unsigned long limit_pfn
)
130 if (limit_pfn
<= iovad
->dma_32bit_pfn
)
131 return iovad
->cached32_node
;
133 return iovad
->cached_node
;
137 __cached_rbnode_insert_update(struct iova_domain
*iovad
, struct iova
*new)
139 if (new->pfn_hi
< iovad
->dma_32bit_pfn
)
140 iovad
->cached32_node
= &new->node
;
142 iovad
->cached_node
= &new->node
;
146 __cached_rbnode_delete_update(struct iova_domain
*iovad
, struct iova
*free
)
148 struct iova
*cached_iova
;
150 cached_iova
= rb_entry(iovad
->cached32_node
, struct iova
, node
);
151 if (free
== cached_iova
||
152 (free
->pfn_hi
< iovad
->dma_32bit_pfn
&&
153 free
->pfn_lo
>= cached_iova
->pfn_lo
))
154 iovad
->cached32_node
= rb_next(&free
->node
);
156 cached_iova
= rb_entry(iovad
->cached_node
, struct iova
, node
);
157 if (free
->pfn_lo
>= cached_iova
->pfn_lo
)
158 iovad
->cached_node
= rb_next(&free
->node
);
161 /* Insert the iova into domain rbtree by holding writer lock */
163 iova_insert_rbtree(struct rb_root
*root
, struct iova
*iova
,
164 struct rb_node
*start
)
166 struct rb_node
**new, *parent
= NULL
;
168 new = (start
) ? &start
: &(root
->rb_node
);
169 /* Figure out where to put new node */
171 struct iova
*this = rb_entry(*new, struct iova
, node
);
175 if (iova
->pfn_lo
< this->pfn_lo
)
176 new = &((*new)->rb_left
);
177 else if (iova
->pfn_lo
> this->pfn_lo
)
178 new = &((*new)->rb_right
);
180 WARN_ON(1); /* this should not happen */
184 /* Add new node and rebalance tree. */
185 rb_link_node(&iova
->node
, parent
, new);
186 rb_insert_color(&iova
->node
, root
);
189 static int __alloc_and_insert_iova_range(struct iova_domain
*iovad
,
190 unsigned long size
, unsigned long limit_pfn
,
191 struct iova
*new, bool size_aligned
)
193 struct rb_node
*curr
, *prev
;
194 struct iova
*curr_iova
;
196 unsigned long new_pfn
;
197 unsigned long align_mask
= ~0UL;
200 align_mask
<<= fls_long(size
- 1);
202 /* Walk the tree backwards */
203 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
204 curr
= __get_cached_rbnode(iovad
, limit_pfn
);
205 curr_iova
= rb_entry(curr
, struct iova
, node
);
207 limit_pfn
= min(limit_pfn
, curr_iova
->pfn_lo
);
208 new_pfn
= (limit_pfn
- size
) & align_mask
;
210 curr
= rb_prev(curr
);
211 curr_iova
= rb_entry(curr
, struct iova
, node
);
212 } while (curr
&& new_pfn
<= curr_iova
->pfn_hi
);
214 if (limit_pfn
< size
|| new_pfn
< iovad
->start_pfn
) {
215 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
219 /* pfn_lo will point to size aligned address if size_aligned is set */
220 new->pfn_lo
= new_pfn
;
221 new->pfn_hi
= new->pfn_lo
+ size
- 1;
223 /* If we have 'prev', it's a valid place to start the insertion. */
224 iova_insert_rbtree(&iovad
->rbroot
, new, prev
);
225 __cached_rbnode_insert_update(iovad
, new);
227 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
233 static struct kmem_cache
*iova_cache
;
234 static unsigned int iova_cache_users
;
235 static DEFINE_MUTEX(iova_cache_mutex
);
237 struct iova
*alloc_iova_mem(void)
239 return kmem_cache_zalloc(iova_cache
, GFP_ATOMIC
);
241 EXPORT_SYMBOL(alloc_iova_mem
);
243 void free_iova_mem(struct iova
*iova
)
245 if (iova
->pfn_lo
!= IOVA_ANCHOR
)
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
< iova
->pfn_lo
)
331 node
= node
->rb_left
;
332 else if (pfn
> iova
->pfn_hi
)
333 node
= node
->rb_right
;
335 return iova
; /* pfn falls within iova's range */
341 static void private_free_iova(struct iova_domain
*iovad
, struct iova
*iova
)
343 assert_spin_locked(&iovad
->iova_rbtree_lock
);
344 __cached_rbnode_delete_update(iovad
, iova
);
345 rb_erase(&iova
->node
, &iovad
->rbroot
);
350 * find_iova - finds an iova for a given pfn
351 * @iovad: - iova domain in question.
352 * @pfn: - page frame number
353 * This function finds and returns an iova belonging to the
354 * given doamin which matches the given pfn.
356 struct iova
*find_iova(struct iova_domain
*iovad
, unsigned long pfn
)
361 /* Take the lock so that no other thread is manipulating the rbtree */
362 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
363 iova
= private_find_iova(iovad
, pfn
);
364 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
367 EXPORT_SYMBOL_GPL(find_iova
);
370 * __free_iova - frees the given iova
371 * @iovad: iova domain in question.
372 * @iova: iova in question.
373 * Frees the given iova belonging to the giving domain
376 __free_iova(struct iova_domain
*iovad
, struct iova
*iova
)
380 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
381 private_free_iova(iovad
, iova
);
382 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
384 EXPORT_SYMBOL_GPL(__free_iova
);
387 * free_iova - finds and frees the iova for a given pfn
388 * @iovad: - iova domain in question.
389 * @pfn: - pfn that is allocated previously
390 * This functions finds an iova for a given pfn and then
391 * frees the iova from that domain.
394 free_iova(struct iova_domain
*iovad
, unsigned long pfn
)
396 struct iova
*iova
= find_iova(iovad
, pfn
);
399 __free_iova(iovad
, iova
);
402 EXPORT_SYMBOL_GPL(free_iova
);
405 * alloc_iova_fast - allocates an iova from rcache
406 * @iovad: - iova domain in question
407 * @size: - size of page frames to allocate
408 * @limit_pfn: - max limit address
409 * @flush_rcache: - set to flush rcache on regular allocation failure
410 * This function tries to satisfy an iova allocation from the rcache,
411 * and falls back to regular allocation on failure. If regular allocation
412 * fails too and the flush_rcache flag is set then the rcache will be flushed.
415 alloc_iova_fast(struct iova_domain
*iovad
, unsigned long size
,
416 unsigned long limit_pfn
, bool flush_rcache
)
418 unsigned long iova_pfn
;
419 struct iova
*new_iova
;
421 iova_pfn
= iova_rcache_get(iovad
, size
, limit_pfn
+ 1);
426 new_iova
= alloc_iova(iovad
, size
, limit_pfn
, true);
433 /* Try replenishing IOVAs by flushing rcache. */
434 flush_rcache
= false;
435 for_each_online_cpu(cpu
)
436 free_cpu_cached_iovas(cpu
, iovad
);
440 return new_iova
->pfn_lo
;
442 EXPORT_SYMBOL_GPL(alloc_iova_fast
);
445 * free_iova_fast - free iova pfn range into rcache
446 * @iovad: - iova domain in question.
447 * @pfn: - pfn that is allocated previously
448 * @size: - # of pages in range
449 * This functions frees an iova range by trying to put it into the rcache,
450 * falling back to regular iova deallocation via free_iova() if this fails.
453 free_iova_fast(struct iova_domain
*iovad
, unsigned long pfn
, unsigned long size
)
455 if (iova_rcache_insert(iovad
, pfn
, size
))
458 free_iova(iovad
, pfn
);
460 EXPORT_SYMBOL_GPL(free_iova_fast
);
462 #define fq_ring_for_each(i, fq) \
463 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
465 static inline bool fq_full(struct iova_fq
*fq
)
467 assert_spin_locked(&fq
->lock
);
468 return (((fq
->tail
+ 1) % IOVA_FQ_SIZE
) == fq
->head
);
471 static inline unsigned fq_ring_add(struct iova_fq
*fq
)
473 unsigned idx
= fq
->tail
;
475 assert_spin_locked(&fq
->lock
);
477 fq
->tail
= (idx
+ 1) % IOVA_FQ_SIZE
;
482 static void fq_ring_free(struct iova_domain
*iovad
, struct iova_fq
*fq
)
484 u64 counter
= atomic64_read(&iovad
->fq_flush_finish_cnt
);
487 assert_spin_locked(&fq
->lock
);
489 fq_ring_for_each(idx
, fq
) {
491 if (fq
->entries
[idx
].counter
>= counter
)
494 if (iovad
->entry_dtor
)
495 iovad
->entry_dtor(fq
->entries
[idx
].data
);
497 free_iova_fast(iovad
,
498 fq
->entries
[idx
].iova_pfn
,
499 fq
->entries
[idx
].pages
);
501 fq
->head
= (fq
->head
+ 1) % IOVA_FQ_SIZE
;
505 static void iova_domain_flush(struct iova_domain
*iovad
)
507 atomic64_inc(&iovad
->fq_flush_start_cnt
);
508 iovad
->flush_cb(iovad
);
509 atomic64_inc(&iovad
->fq_flush_finish_cnt
);
512 static void fq_destroy_all_entries(struct iova_domain
*iovad
)
517 * This code runs when the iova_domain is being detroyed, so don't
518 * bother to free iovas, just call the entry_dtor on all remaining
521 if (!iovad
->entry_dtor
)
524 for_each_possible_cpu(cpu
) {
525 struct iova_fq
*fq
= per_cpu_ptr(iovad
->fq
, cpu
);
528 fq_ring_for_each(idx
, fq
)
529 iovad
->entry_dtor(fq
->entries
[idx
].data
);
533 static void fq_flush_timeout(struct timer_list
*t
)
535 struct iova_domain
*iovad
= from_timer(iovad
, t
, fq_timer
);
538 atomic_set(&iovad
->fq_timer_on
, 0);
539 iova_domain_flush(iovad
);
541 for_each_possible_cpu(cpu
) {
545 fq
= per_cpu_ptr(iovad
->fq
, cpu
);
546 spin_lock_irqsave(&fq
->lock
, flags
);
547 fq_ring_free(iovad
, fq
);
548 spin_unlock_irqrestore(&fq
->lock
, flags
);
552 void queue_iova(struct iova_domain
*iovad
,
553 unsigned long pfn
, unsigned long pages
,
556 struct iova_fq
*fq
= raw_cpu_ptr(iovad
->fq
);
560 spin_lock_irqsave(&fq
->lock
, flags
);
563 * First remove all entries from the flush queue that have already been
564 * flushed out on another CPU. This makes the fq_full() check below less
567 fq_ring_free(iovad
, fq
);
570 iova_domain_flush(iovad
);
571 fq_ring_free(iovad
, fq
);
574 idx
= fq_ring_add(fq
);
576 fq
->entries
[idx
].iova_pfn
= pfn
;
577 fq
->entries
[idx
].pages
= pages
;
578 fq
->entries
[idx
].data
= data
;
579 fq
->entries
[idx
].counter
= atomic64_read(&iovad
->fq_flush_start_cnt
);
581 spin_unlock_irqrestore(&fq
->lock
, flags
);
583 /* Avoid false sharing as much as possible. */
584 if (!atomic_read(&iovad
->fq_timer_on
) &&
585 !atomic_cmpxchg(&iovad
->fq_timer_on
, 0, 1))
586 mod_timer(&iovad
->fq_timer
,
587 jiffies
+ msecs_to_jiffies(IOVA_FQ_TIMEOUT
));
589 EXPORT_SYMBOL_GPL(queue_iova
);
592 * put_iova_domain - destroys the iova doamin
593 * @iovad: - iova domain in question.
594 * All the iova's in that domain are destroyed.
596 void put_iova_domain(struct iova_domain
*iovad
)
598 struct iova
*iova
, *tmp
;
600 free_iova_flush_queue(iovad
);
601 free_iova_rcaches(iovad
);
602 rbtree_postorder_for_each_entry_safe(iova
, tmp
, &iovad
->rbroot
, node
)
605 EXPORT_SYMBOL_GPL(put_iova_domain
);
608 __is_range_overlap(struct rb_node
*node
,
609 unsigned long pfn_lo
, unsigned long pfn_hi
)
611 struct iova
*iova
= rb_entry(node
, struct iova
, node
);
613 if ((pfn_lo
<= iova
->pfn_hi
) && (pfn_hi
>= iova
->pfn_lo
))
618 static inline struct iova
*
619 alloc_and_init_iova(unsigned long pfn_lo
, unsigned long pfn_hi
)
623 iova
= alloc_iova_mem();
625 iova
->pfn_lo
= pfn_lo
;
626 iova
->pfn_hi
= pfn_hi
;
633 __insert_new_range(struct iova_domain
*iovad
,
634 unsigned long pfn_lo
, unsigned long pfn_hi
)
638 iova
= alloc_and_init_iova(pfn_lo
, pfn_hi
);
640 iova_insert_rbtree(&iovad
->rbroot
, iova
, NULL
);
646 __adjust_overlap_range(struct iova
*iova
,
647 unsigned long *pfn_lo
, unsigned long *pfn_hi
)
649 if (*pfn_lo
< iova
->pfn_lo
)
650 iova
->pfn_lo
= *pfn_lo
;
651 if (*pfn_hi
> iova
->pfn_hi
)
652 *pfn_lo
= iova
->pfn_hi
+ 1;
656 * reserve_iova - reserves an iova in the given range
657 * @iovad: - iova domain pointer
658 * @pfn_lo: - lower page frame address
659 * @pfn_hi:- higher pfn adderss
660 * This function allocates reserves the address range from pfn_lo to pfn_hi so
661 * that this address is not dished out as part of alloc_iova.
664 reserve_iova(struct iova_domain
*iovad
,
665 unsigned long pfn_lo
, unsigned long pfn_hi
)
667 struct rb_node
*node
;
670 unsigned int overlap
= 0;
672 /* Don't allow nonsensical pfns */
673 if (WARN_ON((pfn_hi
| pfn_lo
) > (ULLONG_MAX
>> iova_shift(iovad
))))
676 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
677 for (node
= rb_first(&iovad
->rbroot
); node
; node
= rb_next(node
)) {
678 if (__is_range_overlap(node
, pfn_lo
, pfn_hi
)) {
679 iova
= rb_entry(node
, struct iova
, node
);
680 __adjust_overlap_range(iova
, &pfn_lo
, &pfn_hi
);
681 if ((pfn_lo
>= iova
->pfn_lo
) &&
682 (pfn_hi
<= iova
->pfn_hi
))
690 /* We are here either because this is the first reserver node
691 * or need to insert remaining non overlap addr range
693 iova
= __insert_new_range(iovad
, pfn_lo
, pfn_hi
);
696 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
699 EXPORT_SYMBOL_GPL(reserve_iova
);
702 * copy_reserved_iova - copies the reserved between domains
703 * @from: - source doamin from where to copy
704 * @to: - destination domin where to copy
705 * This function copies reserved iova's from one doamin to
709 copy_reserved_iova(struct iova_domain
*from
, struct iova_domain
*to
)
712 struct rb_node
*node
;
714 spin_lock_irqsave(&from
->iova_rbtree_lock
, flags
);
715 for (node
= rb_first(&from
->rbroot
); node
; node
= rb_next(node
)) {
716 struct iova
*iova
= rb_entry(node
, struct iova
, node
);
717 struct iova
*new_iova
;
719 if (iova
->pfn_lo
== IOVA_ANCHOR
)
722 new_iova
= reserve_iova(to
, iova
->pfn_lo
, iova
->pfn_hi
);
724 printk(KERN_ERR
"Reserve iova range %lx@%lx failed\n",
725 iova
->pfn_lo
, iova
->pfn_lo
);
727 spin_unlock_irqrestore(&from
->iova_rbtree_lock
, flags
);
729 EXPORT_SYMBOL_GPL(copy_reserved_iova
);
732 split_and_remove_iova(struct iova_domain
*iovad
, struct iova
*iova
,
733 unsigned long pfn_lo
, unsigned long pfn_hi
)
736 struct iova
*prev
= NULL
, *next
= NULL
;
738 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
739 if (iova
->pfn_lo
< pfn_lo
) {
740 prev
= alloc_and_init_iova(iova
->pfn_lo
, pfn_lo
- 1);
744 if (iova
->pfn_hi
> pfn_hi
) {
745 next
= alloc_and_init_iova(pfn_hi
+ 1, iova
->pfn_hi
);
750 __cached_rbnode_delete_update(iovad
, iova
);
751 rb_erase(&iova
->node
, &iovad
->rbroot
);
754 iova_insert_rbtree(&iovad
->rbroot
, prev
, NULL
);
755 iova
->pfn_lo
= pfn_lo
;
758 iova_insert_rbtree(&iovad
->rbroot
, next
, NULL
);
759 iova
->pfn_hi
= pfn_hi
;
761 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
766 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
773 * Magazine caches for IOVA ranges. For an introduction to magazines,
774 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
775 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
776 * For simplicity, we use a static magazine size and don't implement the
777 * dynamic size tuning described in the paper.
780 #define IOVA_MAG_SIZE 128
782 struct iova_magazine
{
784 unsigned long pfns
[IOVA_MAG_SIZE
];
787 struct iova_cpu_rcache
{
789 struct iova_magazine
*loaded
;
790 struct iova_magazine
*prev
;
793 static struct iova_magazine
*iova_magazine_alloc(gfp_t flags
)
795 return kzalloc(sizeof(struct iova_magazine
), flags
);
798 static void iova_magazine_free(struct iova_magazine
*mag
)
804 iova_magazine_free_pfns(struct iova_magazine
*mag
, struct iova_domain
*iovad
)
812 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
814 for (i
= 0 ; i
< mag
->size
; ++i
) {
815 struct iova
*iova
= private_find_iova(iovad
, mag
->pfns
[i
]);
818 private_free_iova(iovad
, iova
);
821 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
826 static bool iova_magazine_full(struct iova_magazine
*mag
)
828 return (mag
&& mag
->size
== IOVA_MAG_SIZE
);
831 static bool iova_magazine_empty(struct iova_magazine
*mag
)
833 return (!mag
|| mag
->size
== 0);
836 static unsigned long iova_magazine_pop(struct iova_magazine
*mag
,
837 unsigned long limit_pfn
)
842 BUG_ON(iova_magazine_empty(mag
));
844 /* Only fall back to the rbtree if we have no suitable pfns at all */
845 for (i
= mag
->size
- 1; mag
->pfns
[i
] > limit_pfn
; i
--)
849 /* Swap it to pop it */
851 mag
->pfns
[i
] = mag
->pfns
[--mag
->size
];
856 static void iova_magazine_push(struct iova_magazine
*mag
, unsigned long pfn
)
858 BUG_ON(iova_magazine_full(mag
));
860 mag
->pfns
[mag
->size
++] = pfn
;
863 static void init_iova_rcaches(struct iova_domain
*iovad
)
865 struct iova_cpu_rcache
*cpu_rcache
;
866 struct iova_rcache
*rcache
;
870 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
871 rcache
= &iovad
->rcaches
[i
];
872 spin_lock_init(&rcache
->lock
);
873 rcache
->depot_size
= 0;
874 rcache
->cpu_rcaches
= __alloc_percpu(sizeof(*cpu_rcache
), cache_line_size());
875 if (WARN_ON(!rcache
->cpu_rcaches
))
877 for_each_possible_cpu(cpu
) {
878 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
879 spin_lock_init(&cpu_rcache
->lock
);
880 cpu_rcache
->loaded
= iova_magazine_alloc(GFP_KERNEL
);
881 cpu_rcache
->prev
= iova_magazine_alloc(GFP_KERNEL
);
887 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
888 * return true on success. Can fail if rcache is full and we can't free
889 * space, and free_iova() (our only caller) will then return the IOVA
890 * range to the rbtree instead.
892 static bool __iova_rcache_insert(struct iova_domain
*iovad
,
893 struct iova_rcache
*rcache
,
894 unsigned long iova_pfn
)
896 struct iova_magazine
*mag_to_free
= NULL
;
897 struct iova_cpu_rcache
*cpu_rcache
;
898 bool can_insert
= false;
901 cpu_rcache
= raw_cpu_ptr(rcache
->cpu_rcaches
);
902 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
904 if (!iova_magazine_full(cpu_rcache
->loaded
)) {
906 } else if (!iova_magazine_full(cpu_rcache
->prev
)) {
907 swap(cpu_rcache
->prev
, cpu_rcache
->loaded
);
910 struct iova_magazine
*new_mag
= iova_magazine_alloc(GFP_ATOMIC
);
913 spin_lock(&rcache
->lock
);
914 if (rcache
->depot_size
< MAX_GLOBAL_MAGS
) {
915 rcache
->depot
[rcache
->depot_size
++] =
918 mag_to_free
= cpu_rcache
->loaded
;
920 spin_unlock(&rcache
->lock
);
922 cpu_rcache
->loaded
= new_mag
;
928 iova_magazine_push(cpu_rcache
->loaded
, iova_pfn
);
930 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
933 iova_magazine_free_pfns(mag_to_free
, iovad
);
934 iova_magazine_free(mag_to_free
);
940 static bool iova_rcache_insert(struct iova_domain
*iovad
, unsigned long pfn
,
943 unsigned int log_size
= order_base_2(size
);
945 if (log_size
>= IOVA_RANGE_CACHE_MAX_SIZE
)
948 return __iova_rcache_insert(iovad
, &iovad
->rcaches
[log_size
], pfn
);
952 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
953 * satisfy the request, return a matching non-NULL range and remove
954 * it from the 'rcache'.
956 static unsigned long __iova_rcache_get(struct iova_rcache
*rcache
,
957 unsigned long limit_pfn
)
959 struct iova_cpu_rcache
*cpu_rcache
;
960 unsigned long iova_pfn
= 0;
961 bool has_pfn
= false;
964 cpu_rcache
= raw_cpu_ptr(rcache
->cpu_rcaches
);
965 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
967 if (!iova_magazine_empty(cpu_rcache
->loaded
)) {
969 } else if (!iova_magazine_empty(cpu_rcache
->prev
)) {
970 swap(cpu_rcache
->prev
, cpu_rcache
->loaded
);
973 spin_lock(&rcache
->lock
);
974 if (rcache
->depot_size
> 0) {
975 iova_magazine_free(cpu_rcache
->loaded
);
976 cpu_rcache
->loaded
= rcache
->depot
[--rcache
->depot_size
];
979 spin_unlock(&rcache
->lock
);
983 iova_pfn
= iova_magazine_pop(cpu_rcache
->loaded
, limit_pfn
);
985 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
991 * Try to satisfy IOVA allocation range from rcache. Fail if requested
992 * size is too big or the DMA limit we are given isn't satisfied by the
993 * top element in the magazine.
995 static unsigned long iova_rcache_get(struct iova_domain
*iovad
,
997 unsigned long limit_pfn
)
999 unsigned int log_size
= order_base_2(size
);
1001 if (log_size
>= IOVA_RANGE_CACHE_MAX_SIZE
)
1004 return __iova_rcache_get(&iovad
->rcaches
[log_size
], limit_pfn
- size
);
1008 * free rcache data structures.
1010 static void free_iova_rcaches(struct iova_domain
*iovad
)
1012 struct iova_rcache
*rcache
;
1013 struct iova_cpu_rcache
*cpu_rcache
;
1017 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
1018 rcache
= &iovad
->rcaches
[i
];
1019 for_each_possible_cpu(cpu
) {
1020 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
1021 iova_magazine_free(cpu_rcache
->loaded
);
1022 iova_magazine_free(cpu_rcache
->prev
);
1024 free_percpu(rcache
->cpu_rcaches
);
1025 for (j
= 0; j
< rcache
->depot_size
; ++j
)
1026 iova_magazine_free(rcache
->depot
[j
]);
1031 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1033 void free_cpu_cached_iovas(unsigned int cpu
, struct iova_domain
*iovad
)
1035 struct iova_cpu_rcache
*cpu_rcache
;
1036 struct iova_rcache
*rcache
;
1037 unsigned long flags
;
1040 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
1041 rcache
= &iovad
->rcaches
[i
];
1042 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
1043 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
1044 iova_magazine_free_pfns(cpu_rcache
->loaded
, iovad
);
1045 iova_magazine_free_pfns(cpu_rcache
->prev
, iovad
);
1046 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
1050 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1051 MODULE_LICENSE("GPL");