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/slab.h>
23 static struct kmem_cache *iommu_iova_cache;
25 int iommu_iova_cache_init(void)
29 iommu_iova_cache = kmem_cache_create("iommu_iova",
34 if (!iommu_iova_cache) {
35 pr_err("Couldn't create iova cache\n");
42 void iommu_iova_cache_destroy(void)
44 kmem_cache_destroy(iommu_iova_cache);
47 struct iova *alloc_iova_mem(void)
49 return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
52 void free_iova_mem(struct iova *iova)
54 kmem_cache_free(iommu_iova_cache, iova);
58 init_iova_domain(struct iova_domain *iovad, unsigned long start_pfn,
59 unsigned long pfn_32bit)
61 spin_lock_init(&iovad->iova_rbtree_lock);
62 iovad->rbroot = RB_ROOT;
63 iovad->cached32_node = NULL;
64 iovad->start_pfn = start_pfn;
65 iovad->dma_32bit_pfn = pfn_32bit;
68 static struct rb_node *
69 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
71 if ((*limit_pfn != iovad->dma_32bit_pfn) ||
72 (iovad->cached32_node == NULL))
73 return rb_last(&iovad->rbroot);
75 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
76 struct iova *curr_iova =
77 container_of(iovad->cached32_node, struct iova, node);
78 *limit_pfn = curr_iova->pfn_lo - 1;
84 __cached_rbnode_insert_update(struct iova_domain *iovad,
85 unsigned long limit_pfn, struct iova *new)
87 if (limit_pfn != iovad->dma_32bit_pfn)
89 iovad->cached32_node = &new->node;
93 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
95 struct iova *cached_iova;
98 if (!iovad->cached32_node)
100 curr = iovad->cached32_node;
101 cached_iova = container_of(curr, struct iova, node);
103 if (free->pfn_lo >= cached_iova->pfn_lo) {
104 struct rb_node *node = rb_next(&free->node);
105 struct iova *iova = container_of(node, struct iova, node);
107 /* only cache if it's below 32bit pfn */
108 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
109 iovad->cached32_node = node;
111 iovad->cached32_node = NULL;
115 /* Computes the padding size required, to make the
116 * the start address naturally aligned on its size
119 iova_get_pad_size(int size, unsigned int limit_pfn)
121 unsigned int pad_size = 0;
122 unsigned int order = ilog2(size);
125 pad_size = (limit_pfn + 1) % (1 << order);
130 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
131 unsigned long size, unsigned long limit_pfn,
132 struct iova *new, bool size_aligned)
134 struct rb_node *prev, *curr = NULL;
136 unsigned long saved_pfn;
137 unsigned int pad_size = 0;
139 /* Walk the tree backwards */
140 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
141 saved_pfn = limit_pfn;
142 curr = __get_cached_rbnode(iovad, &limit_pfn);
145 struct iova *curr_iova = container_of(curr, struct iova, node);
147 if (limit_pfn < curr_iova->pfn_lo)
149 else if (limit_pfn < curr_iova->pfn_hi)
150 goto adjust_limit_pfn;
153 pad_size = iova_get_pad_size(size, limit_pfn);
154 if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn)
155 break; /* found a free slot */
158 limit_pfn = curr_iova->pfn_lo - 1;
161 curr = rb_prev(curr);
166 pad_size = iova_get_pad_size(size, limit_pfn);
167 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
168 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
173 /* pfn_lo will point to size aligned address if size_aligned is set */
174 new->pfn_lo = limit_pfn - (size + pad_size) + 1;
175 new->pfn_hi = new->pfn_lo + size - 1;
177 /* Insert the new_iova into domain rbtree by holding writer lock */
178 /* Add new node and rebalance tree. */
180 struct rb_node **entry, *parent = NULL;
182 /* If we have 'prev', it's a valid place to start the
183 insertion. Otherwise, start from the root. */
187 entry = &iovad->rbroot.rb_node;
189 /* Figure out where to put new node */
191 struct iova *this = container_of(*entry,
195 if (new->pfn_lo < this->pfn_lo)
196 entry = &((*entry)->rb_left);
197 else if (new->pfn_lo > this->pfn_lo)
198 entry = &((*entry)->rb_right);
200 BUG(); /* this should not happen */
203 /* Add new node and rebalance tree. */
204 rb_link_node(&new->node, parent, entry);
205 rb_insert_color(&new->node, &iovad->rbroot);
207 __cached_rbnode_insert_update(iovad, saved_pfn, new);
209 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
216 iova_insert_rbtree(struct rb_root *root, struct iova *iova)
218 struct rb_node **new = &(root->rb_node), *parent = NULL;
219 /* Figure out where to put new node */
221 struct iova *this = container_of(*new, struct iova, node);
224 if (iova->pfn_lo < this->pfn_lo)
225 new = &((*new)->rb_left);
226 else if (iova->pfn_lo > this->pfn_lo)
227 new = &((*new)->rb_right);
229 BUG(); /* this should not happen */
231 /* Add new node and rebalance tree. */
232 rb_link_node(&iova->node, parent, new);
233 rb_insert_color(&iova->node, root);
237 * alloc_iova - allocates an iova
238 * @iovad: - iova domain in question
239 * @size: - size of page frames to allocate
240 * @limit_pfn: - max limit address
241 * @size_aligned: - set if size_aligned address range is required
242 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
243 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
244 * flag is set then the allocated address iova->pfn_lo will be naturally
245 * aligned on roundup_power_of_two(size).
248 alloc_iova(struct iova_domain *iovad, unsigned long size,
249 unsigned long limit_pfn,
252 struct iova *new_iova;
255 new_iova = alloc_iova_mem();
259 /* If size aligned is set then round the size to
260 * to next power of two.
263 size = __roundup_pow_of_two(size);
265 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
266 new_iova, size_aligned);
269 free_iova_mem(new_iova);
277 * find_iova - find's an iova for a given pfn
278 * @iovad: - iova domain in question.
279 * @pfn: - page frame number
280 * This function finds and returns an iova belonging to the
281 * given doamin which matches the given pfn.
283 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
286 struct rb_node *node;
288 /* Take the lock so that no other thread is manipulating the rbtree */
289 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
290 node = iovad->rbroot.rb_node;
292 struct iova *iova = container_of(node, struct iova, node);
294 /* If pfn falls within iova's range, return iova */
295 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
296 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
297 /* We are not holding the lock while this iova
298 * is referenced by the caller as the same thread
299 * which called this function also calls __free_iova()
300 * and it is by design that only one thread can possibly
301 * reference a particular iova and hence no conflict.
306 if (pfn < iova->pfn_lo)
307 node = node->rb_left;
308 else if (pfn > iova->pfn_lo)
309 node = node->rb_right;
312 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
317 * __free_iova - frees the given iova
318 * @iovad: iova domain in question.
319 * @iova: iova in question.
320 * Frees the given iova belonging to the giving domain
323 __free_iova(struct iova_domain *iovad, struct iova *iova)
327 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
328 __cached_rbnode_delete_update(iovad, iova);
329 rb_erase(&iova->node, &iovad->rbroot);
330 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
335 * free_iova - finds and frees the iova for a given pfn
336 * @iovad: - iova domain in question.
337 * @pfn: - pfn that is allocated previously
338 * This functions finds an iova for a given pfn and then
339 * frees the iova from that domain.
342 free_iova(struct iova_domain *iovad, unsigned long pfn)
344 struct iova *iova = find_iova(iovad, pfn);
346 __free_iova(iovad, iova);
351 * put_iova_domain - destroys the iova doamin
352 * @iovad: - iova domain in question.
353 * All the iova's in that domain are destroyed.
355 void put_iova_domain(struct iova_domain *iovad)
357 struct rb_node *node;
360 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
361 node = rb_first(&iovad->rbroot);
363 struct iova *iova = container_of(node, struct iova, node);
364 rb_erase(node, &iovad->rbroot);
366 node = rb_first(&iovad->rbroot);
368 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
372 __is_range_overlap(struct rb_node *node,
373 unsigned long pfn_lo, unsigned long pfn_hi)
375 struct iova *iova = container_of(node, struct iova, node);
377 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
382 static inline struct iova *
383 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
387 iova = alloc_iova_mem();
389 iova->pfn_lo = pfn_lo;
390 iova->pfn_hi = pfn_hi;
397 __insert_new_range(struct iova_domain *iovad,
398 unsigned long pfn_lo, unsigned long pfn_hi)
402 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
404 iova_insert_rbtree(&iovad->rbroot, iova);
410 __adjust_overlap_range(struct iova *iova,
411 unsigned long *pfn_lo, unsigned long *pfn_hi)
413 if (*pfn_lo < iova->pfn_lo)
414 iova->pfn_lo = *pfn_lo;
415 if (*pfn_hi > iova->pfn_hi)
416 *pfn_lo = iova->pfn_hi + 1;
420 * reserve_iova - reserves an iova in the given range
421 * @iovad: - iova domain pointer
422 * @pfn_lo: - lower page frame address
423 * @pfn_hi:- higher pfn adderss
424 * This function allocates reserves the address range from pfn_lo to pfn_hi so
425 * that this address is not dished out as part of alloc_iova.
428 reserve_iova(struct iova_domain *iovad,
429 unsigned long pfn_lo, unsigned long pfn_hi)
431 struct rb_node *node;
434 unsigned int overlap = 0;
436 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
437 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
438 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
439 iova = container_of(node, struct iova, node);
440 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
441 if ((pfn_lo >= iova->pfn_lo) &&
442 (pfn_hi <= iova->pfn_hi))
450 /* We are here either because this is the first reserver node
451 * or need to insert remaining non overlap addr range
453 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
456 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
461 * copy_reserved_iova - copies the reserved between domains
462 * @from: - source doamin from where to copy
463 * @to: - destination domin where to copy
464 * This function copies reserved iova's from one doamin to
468 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
471 struct rb_node *node;
473 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
474 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
475 struct iova *iova = container_of(node, struct iova, node);
476 struct iova *new_iova;
477 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
479 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
480 iova->pfn_lo, iova->pfn_lo);
482 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
486 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
487 unsigned long pfn_lo, unsigned long pfn_hi)
490 struct iova *prev = NULL, *next = NULL;
492 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
493 if (iova->pfn_lo < pfn_lo) {
494 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
498 if (iova->pfn_hi > pfn_hi) {
499 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
504 __cached_rbnode_delete_update(iovad, iova);
505 rb_erase(&iova->node, &iovad->rbroot);
508 iova_insert_rbtree(&iovad->rbroot, prev);
509 iova->pfn_lo = pfn_lo;
512 iova_insert_rbtree(&iovad->rbroot, next);
513 iova->pfn_hi = pfn_hi;
515 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
520 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);