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>
26 static bool iova_rcache_insert(struct iova_domain
*iovad
,
29 static unsigned long iova_rcache_get(struct iova_domain
*iovad
,
31 unsigned long limit_pfn
);
32 static void init_iova_rcaches(struct iova_domain
*iovad
);
33 static void free_iova_rcaches(struct iova_domain
*iovad
);
36 init_iova_domain(struct iova_domain
*iovad
, unsigned long granule
,
37 unsigned long start_pfn
, unsigned long pfn_32bit
)
40 * IOVA granularity will normally be equal to the smallest
41 * supported IOMMU page size; both *must* be capable of
42 * representing individual CPU pages exactly.
44 BUG_ON((granule
> PAGE_SIZE
) || !is_power_of_2(granule
));
46 spin_lock_init(&iovad
->iova_rbtree_lock
);
47 iovad
->rbroot
= RB_ROOT
;
48 iovad
->cached32_node
= NULL
;
49 iovad
->granule
= granule
;
50 iovad
->start_pfn
= start_pfn
;
51 iovad
->dma_32bit_pfn
= pfn_32bit
;
52 init_iova_rcaches(iovad
);
54 EXPORT_SYMBOL_GPL(init_iova_domain
);
56 static struct rb_node
*
57 __get_cached_rbnode(struct iova_domain
*iovad
, unsigned long *limit_pfn
)
59 if ((*limit_pfn
!= iovad
->dma_32bit_pfn
) ||
60 (iovad
->cached32_node
== NULL
))
61 return rb_last(&iovad
->rbroot
);
63 struct rb_node
*prev_node
= rb_prev(iovad
->cached32_node
);
64 struct iova
*curr_iova
=
65 container_of(iovad
->cached32_node
, struct iova
, node
);
66 *limit_pfn
= curr_iova
->pfn_lo
- 1;
72 __cached_rbnode_insert_update(struct iova_domain
*iovad
,
73 unsigned long limit_pfn
, struct iova
*new)
75 if (limit_pfn
!= iovad
->dma_32bit_pfn
)
77 iovad
->cached32_node
= &new->node
;
81 __cached_rbnode_delete_update(struct iova_domain
*iovad
, struct iova
*free
)
83 struct iova
*cached_iova
;
86 if (!iovad
->cached32_node
)
88 curr
= iovad
->cached32_node
;
89 cached_iova
= container_of(curr
, struct iova
, node
);
91 if (free
->pfn_lo
>= cached_iova
->pfn_lo
) {
92 struct rb_node
*node
= rb_next(&free
->node
);
93 struct iova
*iova
= container_of(node
, struct iova
, node
);
95 /* only cache if it's below 32bit pfn */
96 if (node
&& iova
->pfn_lo
< iovad
->dma_32bit_pfn
)
97 iovad
->cached32_node
= node
;
99 iovad
->cached32_node
= NULL
;
104 * Computes the padding size required, to make the start address
105 * naturally aligned on the power-of-two order of its size
108 iova_get_pad_size(unsigned int size
, unsigned int limit_pfn
)
110 return (limit_pfn
+ 1 - size
) & (__roundup_pow_of_two(size
) - 1);
113 static int __alloc_and_insert_iova_range(struct iova_domain
*iovad
,
114 unsigned long size
, unsigned long limit_pfn
,
115 struct iova
*new, bool size_aligned
)
117 struct rb_node
*prev
, *curr
= NULL
;
119 unsigned long saved_pfn
;
120 unsigned int pad_size
= 0;
122 /* Walk the tree backwards */
123 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
124 saved_pfn
= limit_pfn
;
125 curr
= __get_cached_rbnode(iovad
, &limit_pfn
);
128 struct iova
*curr_iova
= container_of(curr
, struct iova
, node
);
130 if (limit_pfn
< curr_iova
->pfn_lo
)
132 else if (limit_pfn
< curr_iova
->pfn_hi
)
133 goto adjust_limit_pfn
;
136 pad_size
= iova_get_pad_size(size
, limit_pfn
);
137 if ((curr_iova
->pfn_hi
+ size
+ pad_size
) <= limit_pfn
)
138 break; /* found a free slot */
141 limit_pfn
= curr_iova
->pfn_lo
- 1;
144 curr
= rb_prev(curr
);
149 pad_size
= iova_get_pad_size(size
, limit_pfn
);
150 if ((iovad
->start_pfn
+ size
+ pad_size
) > limit_pfn
) {
151 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
156 /* pfn_lo will point to size aligned address if size_aligned is set */
157 new->pfn_lo
= limit_pfn
- (size
+ pad_size
) + 1;
158 new->pfn_hi
= new->pfn_lo
+ size
- 1;
160 /* Insert the new_iova into domain rbtree by holding writer lock */
161 /* Add new node and rebalance tree. */
163 struct rb_node
**entry
, *parent
= NULL
;
165 /* If we have 'prev', it's a valid place to start the
166 insertion. Otherwise, start from the root. */
170 entry
= &iovad
->rbroot
.rb_node
;
172 /* Figure out where to put new node */
174 struct iova
*this = container_of(*entry
,
178 if (new->pfn_lo
< this->pfn_lo
)
179 entry
= &((*entry
)->rb_left
);
180 else if (new->pfn_lo
> this->pfn_lo
)
181 entry
= &((*entry
)->rb_right
);
183 BUG(); /* this should not happen */
186 /* Add new node and rebalance tree. */
187 rb_link_node(&new->node
, parent
, entry
);
188 rb_insert_color(&new->node
, &iovad
->rbroot
);
190 __cached_rbnode_insert_update(iovad
, saved_pfn
, new);
192 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
199 iova_insert_rbtree(struct rb_root
*root
, struct iova
*iova
)
201 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
202 /* Figure out where to put new node */
204 struct iova
*this = container_of(*new, struct iova
, node
);
208 if (iova
->pfn_lo
< this->pfn_lo
)
209 new = &((*new)->rb_left
);
210 else if (iova
->pfn_lo
> this->pfn_lo
)
211 new = &((*new)->rb_right
);
213 BUG(); /* this should not happen */
215 /* Add new node and rebalance tree. */
216 rb_link_node(&iova
->node
, parent
, new);
217 rb_insert_color(&iova
->node
, root
);
220 static struct kmem_cache
*iova_cache
;
221 static unsigned int iova_cache_users
;
222 static DEFINE_MUTEX(iova_cache_mutex
);
224 struct iova
*alloc_iova_mem(void)
226 return kmem_cache_alloc(iova_cache
, GFP_ATOMIC
);
228 EXPORT_SYMBOL(alloc_iova_mem
);
230 void free_iova_mem(struct iova
*iova
)
232 kmem_cache_free(iova_cache
, iova
);
234 EXPORT_SYMBOL(free_iova_mem
);
236 int iova_cache_get(void)
238 mutex_lock(&iova_cache_mutex
);
239 if (!iova_cache_users
) {
240 iova_cache
= kmem_cache_create(
241 "iommu_iova", sizeof(struct iova
), 0,
242 SLAB_HWCACHE_ALIGN
, NULL
);
244 mutex_unlock(&iova_cache_mutex
);
245 printk(KERN_ERR
"Couldn't create iova cache\n");
251 mutex_unlock(&iova_cache_mutex
);
255 EXPORT_SYMBOL_GPL(iova_cache_get
);
257 void iova_cache_put(void)
259 mutex_lock(&iova_cache_mutex
);
260 if (WARN_ON(!iova_cache_users
)) {
261 mutex_unlock(&iova_cache_mutex
);
265 if (!iova_cache_users
)
266 kmem_cache_destroy(iova_cache
);
267 mutex_unlock(&iova_cache_mutex
);
269 EXPORT_SYMBOL_GPL(iova_cache_put
);
272 * alloc_iova - allocates an iova
273 * @iovad: - iova domain in question
274 * @size: - size of page frames to allocate
275 * @limit_pfn: - max limit address
276 * @size_aligned: - set if size_aligned address range is required
277 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
278 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
279 * flag is set then the allocated address iova->pfn_lo will be naturally
280 * aligned on roundup_power_of_two(size).
283 alloc_iova(struct iova_domain
*iovad
, unsigned long size
,
284 unsigned long limit_pfn
,
287 struct iova
*new_iova
;
290 new_iova
= alloc_iova_mem();
294 ret
= __alloc_and_insert_iova_range(iovad
, size
, limit_pfn
,
295 new_iova
, size_aligned
);
298 free_iova_mem(new_iova
);
304 EXPORT_SYMBOL_GPL(alloc_iova
);
307 private_find_iova(struct iova_domain
*iovad
, unsigned long pfn
)
309 struct rb_node
*node
= iovad
->rbroot
.rb_node
;
311 assert_spin_locked(&iovad
->iova_rbtree_lock
);
314 struct iova
*iova
= container_of(node
, struct iova
, node
);
316 /* If pfn falls within iova's range, return iova */
317 if ((pfn
>= iova
->pfn_lo
) && (pfn
<= iova
->pfn_hi
)) {
321 if (pfn
< iova
->pfn_lo
)
322 node
= node
->rb_left
;
323 else if (pfn
> iova
->pfn_lo
)
324 node
= node
->rb_right
;
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 * This function tries to satisfy an iova allocation from the rcache,
399 * and falls back to regular allocation on failure.
402 alloc_iova_fast(struct iova_domain
*iovad
, unsigned long size
,
403 unsigned long limit_pfn
)
405 bool flushed_rcache
= false;
406 unsigned long iova_pfn
;
407 struct iova
*new_iova
;
409 iova_pfn
= iova_rcache_get(iovad
, size
, limit_pfn
);
414 new_iova
= alloc_iova(iovad
, size
, limit_pfn
, true);
421 /* Try replenishing IOVAs by flushing rcache. */
422 flushed_rcache
= true;
423 for_each_online_cpu(cpu
)
424 free_cpu_cached_iovas(cpu
, iovad
);
428 return new_iova
->pfn_lo
;
430 EXPORT_SYMBOL_GPL(alloc_iova_fast
);
433 * free_iova_fast - free iova pfn range into rcache
434 * @iovad: - iova domain in question.
435 * @pfn: - pfn that is allocated previously
436 * @size: - # of pages in range
437 * This functions frees an iova range by trying to put it into the rcache,
438 * falling back to regular iova deallocation via free_iova() if this fails.
441 free_iova_fast(struct iova_domain
*iovad
, unsigned long pfn
, unsigned long size
)
443 if (iova_rcache_insert(iovad
, pfn
, size
))
446 free_iova(iovad
, pfn
);
448 EXPORT_SYMBOL_GPL(free_iova_fast
);
451 * put_iova_domain - destroys the iova doamin
452 * @iovad: - iova domain in question.
453 * All the iova's in that domain are destroyed.
455 void put_iova_domain(struct iova_domain
*iovad
)
457 struct rb_node
*node
;
460 free_iova_rcaches(iovad
);
461 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
462 node
= rb_first(&iovad
->rbroot
);
464 struct iova
*iova
= container_of(node
, struct iova
, node
);
466 rb_erase(node
, &iovad
->rbroot
);
468 node
= rb_first(&iovad
->rbroot
);
470 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
472 EXPORT_SYMBOL_GPL(put_iova_domain
);
475 __is_range_overlap(struct rb_node
*node
,
476 unsigned long pfn_lo
, unsigned long pfn_hi
)
478 struct iova
*iova
= container_of(node
, struct iova
, node
);
480 if ((pfn_lo
<= iova
->pfn_hi
) && (pfn_hi
>= iova
->pfn_lo
))
485 static inline struct iova
*
486 alloc_and_init_iova(unsigned long pfn_lo
, unsigned long pfn_hi
)
490 iova
= alloc_iova_mem();
492 iova
->pfn_lo
= pfn_lo
;
493 iova
->pfn_hi
= pfn_hi
;
500 __insert_new_range(struct iova_domain
*iovad
,
501 unsigned long pfn_lo
, unsigned long pfn_hi
)
505 iova
= alloc_and_init_iova(pfn_lo
, pfn_hi
);
507 iova_insert_rbtree(&iovad
->rbroot
, iova
);
513 __adjust_overlap_range(struct iova
*iova
,
514 unsigned long *pfn_lo
, unsigned long *pfn_hi
)
516 if (*pfn_lo
< iova
->pfn_lo
)
517 iova
->pfn_lo
= *pfn_lo
;
518 if (*pfn_hi
> iova
->pfn_hi
)
519 *pfn_lo
= iova
->pfn_hi
+ 1;
523 * reserve_iova - reserves an iova in the given range
524 * @iovad: - iova domain pointer
525 * @pfn_lo: - lower page frame address
526 * @pfn_hi:- higher pfn adderss
527 * This function allocates reserves the address range from pfn_lo to pfn_hi so
528 * that this address is not dished out as part of alloc_iova.
531 reserve_iova(struct iova_domain
*iovad
,
532 unsigned long pfn_lo
, unsigned long pfn_hi
)
534 struct rb_node
*node
;
537 unsigned int overlap
= 0;
539 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
540 for (node
= rb_first(&iovad
->rbroot
); node
; node
= rb_next(node
)) {
541 if (__is_range_overlap(node
, pfn_lo
, pfn_hi
)) {
542 iova
= container_of(node
, struct iova
, node
);
543 __adjust_overlap_range(iova
, &pfn_lo
, &pfn_hi
);
544 if ((pfn_lo
>= iova
->pfn_lo
) &&
545 (pfn_hi
<= iova
->pfn_hi
))
553 /* We are here either because this is the first reserver node
554 * or need to insert remaining non overlap addr range
556 iova
= __insert_new_range(iovad
, pfn_lo
, pfn_hi
);
559 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
562 EXPORT_SYMBOL_GPL(reserve_iova
);
565 * copy_reserved_iova - copies the reserved between domains
566 * @from: - source doamin from where to copy
567 * @to: - destination domin where to copy
568 * This function copies reserved iova's from one doamin to
572 copy_reserved_iova(struct iova_domain
*from
, struct iova_domain
*to
)
575 struct rb_node
*node
;
577 spin_lock_irqsave(&from
->iova_rbtree_lock
, flags
);
578 for (node
= rb_first(&from
->rbroot
); node
; node
= rb_next(node
)) {
579 struct iova
*iova
= container_of(node
, struct iova
, node
);
580 struct iova
*new_iova
;
582 new_iova
= reserve_iova(to
, iova
->pfn_lo
, iova
->pfn_hi
);
584 printk(KERN_ERR
"Reserve iova range %lx@%lx failed\n",
585 iova
->pfn_lo
, iova
->pfn_lo
);
587 spin_unlock_irqrestore(&from
->iova_rbtree_lock
, flags
);
589 EXPORT_SYMBOL_GPL(copy_reserved_iova
);
592 split_and_remove_iova(struct iova_domain
*iovad
, struct iova
*iova
,
593 unsigned long pfn_lo
, unsigned long pfn_hi
)
596 struct iova
*prev
= NULL
, *next
= NULL
;
598 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
599 if (iova
->pfn_lo
< pfn_lo
) {
600 prev
= alloc_and_init_iova(iova
->pfn_lo
, pfn_lo
- 1);
604 if (iova
->pfn_hi
> pfn_hi
) {
605 next
= alloc_and_init_iova(pfn_hi
+ 1, iova
->pfn_hi
);
610 __cached_rbnode_delete_update(iovad
, iova
);
611 rb_erase(&iova
->node
, &iovad
->rbroot
);
614 iova_insert_rbtree(&iovad
->rbroot
, prev
);
615 iova
->pfn_lo
= pfn_lo
;
618 iova_insert_rbtree(&iovad
->rbroot
, next
);
619 iova
->pfn_hi
= pfn_hi
;
621 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
626 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
633 * Magazine caches for IOVA ranges. For an introduction to magazines,
634 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
635 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
636 * For simplicity, we use a static magazine size and don't implement the
637 * dynamic size tuning described in the paper.
640 #define IOVA_MAG_SIZE 128
642 struct iova_magazine
{
644 unsigned long pfns
[IOVA_MAG_SIZE
];
647 struct iova_cpu_rcache
{
649 struct iova_magazine
*loaded
;
650 struct iova_magazine
*prev
;
653 static struct iova_magazine
*iova_magazine_alloc(gfp_t flags
)
655 return kzalloc(sizeof(struct iova_magazine
), flags
);
658 static void iova_magazine_free(struct iova_magazine
*mag
)
664 iova_magazine_free_pfns(struct iova_magazine
*mag
, struct iova_domain
*iovad
)
672 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
674 for (i
= 0 ; i
< mag
->size
; ++i
) {
675 struct iova
*iova
= private_find_iova(iovad
, mag
->pfns
[i
]);
678 private_free_iova(iovad
, iova
);
681 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
686 static bool iova_magazine_full(struct iova_magazine
*mag
)
688 return (mag
&& mag
->size
== IOVA_MAG_SIZE
);
691 static bool iova_magazine_empty(struct iova_magazine
*mag
)
693 return (!mag
|| mag
->size
== 0);
696 static unsigned long iova_magazine_pop(struct iova_magazine
*mag
,
697 unsigned long limit_pfn
)
699 BUG_ON(iova_magazine_empty(mag
));
701 if (mag
->pfns
[mag
->size
- 1] >= limit_pfn
)
704 return mag
->pfns
[--mag
->size
];
707 static void iova_magazine_push(struct iova_magazine
*mag
, unsigned long pfn
)
709 BUG_ON(iova_magazine_full(mag
));
711 mag
->pfns
[mag
->size
++] = pfn
;
714 static void init_iova_rcaches(struct iova_domain
*iovad
)
716 struct iova_cpu_rcache
*cpu_rcache
;
717 struct iova_rcache
*rcache
;
721 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
722 rcache
= &iovad
->rcaches
[i
];
723 spin_lock_init(&rcache
->lock
);
724 rcache
->depot_size
= 0;
725 rcache
->cpu_rcaches
= __alloc_percpu(sizeof(*cpu_rcache
), cache_line_size());
726 if (WARN_ON(!rcache
->cpu_rcaches
))
728 for_each_possible_cpu(cpu
) {
729 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
730 spin_lock_init(&cpu_rcache
->lock
);
731 cpu_rcache
->loaded
= iova_magazine_alloc(GFP_KERNEL
);
732 cpu_rcache
->prev
= iova_magazine_alloc(GFP_KERNEL
);
738 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
739 * return true on success. Can fail if rcache is full and we can't free
740 * space, and free_iova() (our only caller) will then return the IOVA
741 * range to the rbtree instead.
743 static bool __iova_rcache_insert(struct iova_domain
*iovad
,
744 struct iova_rcache
*rcache
,
745 unsigned long iova_pfn
)
747 struct iova_magazine
*mag_to_free
= NULL
;
748 struct iova_cpu_rcache
*cpu_rcache
;
749 bool can_insert
= false;
752 cpu_rcache
= this_cpu_ptr(rcache
->cpu_rcaches
);
753 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
755 if (!iova_magazine_full(cpu_rcache
->loaded
)) {
757 } else if (!iova_magazine_full(cpu_rcache
->prev
)) {
758 swap(cpu_rcache
->prev
, cpu_rcache
->loaded
);
761 struct iova_magazine
*new_mag
= iova_magazine_alloc(GFP_ATOMIC
);
764 spin_lock(&rcache
->lock
);
765 if (rcache
->depot_size
< MAX_GLOBAL_MAGS
) {
766 rcache
->depot
[rcache
->depot_size
++] =
769 mag_to_free
= cpu_rcache
->loaded
;
771 spin_unlock(&rcache
->lock
);
773 cpu_rcache
->loaded
= new_mag
;
779 iova_magazine_push(cpu_rcache
->loaded
, iova_pfn
);
781 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
784 iova_magazine_free_pfns(mag_to_free
, iovad
);
785 iova_magazine_free(mag_to_free
);
791 static bool iova_rcache_insert(struct iova_domain
*iovad
, unsigned long pfn
,
794 unsigned int log_size
= order_base_2(size
);
796 if (log_size
>= IOVA_RANGE_CACHE_MAX_SIZE
)
799 return __iova_rcache_insert(iovad
, &iovad
->rcaches
[log_size
], pfn
);
803 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
804 * satisfy the request, return a matching non-NULL range and remove
805 * it from the 'rcache'.
807 static unsigned long __iova_rcache_get(struct iova_rcache
*rcache
,
808 unsigned long limit_pfn
)
810 struct iova_cpu_rcache
*cpu_rcache
;
811 unsigned long iova_pfn
= 0;
812 bool has_pfn
= false;
815 cpu_rcache
= this_cpu_ptr(rcache
->cpu_rcaches
);
816 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
818 if (!iova_magazine_empty(cpu_rcache
->loaded
)) {
820 } else if (!iova_magazine_empty(cpu_rcache
->prev
)) {
821 swap(cpu_rcache
->prev
, cpu_rcache
->loaded
);
824 spin_lock(&rcache
->lock
);
825 if (rcache
->depot_size
> 0) {
826 iova_magazine_free(cpu_rcache
->loaded
);
827 cpu_rcache
->loaded
= rcache
->depot
[--rcache
->depot_size
];
830 spin_unlock(&rcache
->lock
);
834 iova_pfn
= iova_magazine_pop(cpu_rcache
->loaded
, limit_pfn
);
836 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
842 * Try to satisfy IOVA allocation range from rcache. Fail if requested
843 * size is too big or the DMA limit we are given isn't satisfied by the
844 * top element in the magazine.
846 static unsigned long iova_rcache_get(struct iova_domain
*iovad
,
848 unsigned long limit_pfn
)
850 unsigned int log_size
= order_base_2(size
);
852 if (log_size
>= IOVA_RANGE_CACHE_MAX_SIZE
)
855 return __iova_rcache_get(&iovad
->rcaches
[log_size
], limit_pfn
);
859 * Free a cpu's rcache.
861 static void free_cpu_iova_rcache(unsigned int cpu
, struct iova_domain
*iovad
,
862 struct iova_rcache
*rcache
)
864 struct iova_cpu_rcache
*cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
867 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
869 iova_magazine_free_pfns(cpu_rcache
->loaded
, iovad
);
870 iova_magazine_free(cpu_rcache
->loaded
);
872 iova_magazine_free_pfns(cpu_rcache
->prev
, iovad
);
873 iova_magazine_free(cpu_rcache
->prev
);
875 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
879 * free rcache data structures.
881 static void free_iova_rcaches(struct iova_domain
*iovad
)
883 struct iova_rcache
*rcache
;
888 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
889 rcache
= &iovad
->rcaches
[i
];
890 for_each_possible_cpu(cpu
)
891 free_cpu_iova_rcache(cpu
, iovad
, rcache
);
892 spin_lock_irqsave(&rcache
->lock
, flags
);
893 free_percpu(rcache
->cpu_rcaches
);
894 for (j
= 0; j
< rcache
->depot_size
; ++j
) {
895 iova_magazine_free_pfns(rcache
->depot
[j
], iovad
);
896 iova_magazine_free(rcache
->depot
[j
]);
898 spin_unlock_irqrestore(&rcache
->lock
, flags
);
903 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
905 void free_cpu_cached_iovas(unsigned int cpu
, struct iova_domain
*iovad
)
907 struct iova_cpu_rcache
*cpu_rcache
;
908 struct iova_rcache
*rcache
;
912 for (i
= 0; i
< IOVA_RANGE_CACHE_MAX_SIZE
; ++i
) {
913 rcache
= &iovad
->rcaches
[i
];
914 cpu_rcache
= per_cpu_ptr(rcache
->cpu_rcaches
, cpu
);
915 spin_lock_irqsave(&cpu_rcache
->lock
, flags
);
916 iova_magazine_free_pfns(cpu_rcache
->loaded
, iovad
);
917 iova_magazine_free_pfns(cpu_rcache
->prev
, iovad
);
918 spin_unlock_irqrestore(&cpu_rcache
->lock
, flags
);
922 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
923 MODULE_LICENSE("GPL");