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 pfn_32bit)
60 spin_lock_init(&iovad->iova_rbtree_lock);
61 iovad->rbroot = RB_ROOT;
62 iovad->cached32_node = NULL;
63 iovad->dma_32bit_pfn = pfn_32bit;
66 static struct rb_node *
67 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
69 if ((*limit_pfn != iovad->dma_32bit_pfn) ||
70 (iovad->cached32_node == NULL))
71 return rb_last(&iovad->rbroot);
73 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
74 struct iova *curr_iova =
75 container_of(iovad->cached32_node, struct iova, node);
76 *limit_pfn = curr_iova->pfn_lo - 1;
82 __cached_rbnode_insert_update(struct iova_domain *iovad,
83 unsigned long limit_pfn, struct iova *new)
85 if (limit_pfn != iovad->dma_32bit_pfn)
87 iovad->cached32_node = &new->node;
91 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
93 struct iova *cached_iova;
96 if (!iovad->cached32_node)
98 curr = iovad->cached32_node;
99 cached_iova = container_of(curr, struct iova, node);
101 if (free->pfn_lo >= cached_iova->pfn_lo) {
102 struct rb_node *node = rb_next(&free->node);
103 struct iova *iova = container_of(node, struct iova, node);
105 /* only cache if it's below 32bit pfn */
106 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
107 iovad->cached32_node = node;
109 iovad->cached32_node = NULL;
113 /* Computes the padding size required, to make the
114 * the start address naturally aligned on its size
117 iova_get_pad_size(int size, unsigned int limit_pfn)
119 unsigned int pad_size = 0;
120 unsigned int order = ilog2(size);
123 pad_size = (limit_pfn + 1) % (1 << order);
128 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
129 unsigned long size, unsigned long limit_pfn,
130 struct iova *new, bool size_aligned)
132 struct rb_node *prev, *curr = NULL;
134 unsigned long saved_pfn;
135 unsigned int pad_size = 0;
137 /* Walk the tree backwards */
138 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
139 saved_pfn = limit_pfn;
140 curr = __get_cached_rbnode(iovad, &limit_pfn);
143 struct iova *curr_iova = container_of(curr, struct iova, node);
145 if (limit_pfn < curr_iova->pfn_lo)
147 else if (limit_pfn < curr_iova->pfn_hi)
148 goto adjust_limit_pfn;
151 pad_size = iova_get_pad_size(size, limit_pfn);
152 if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn)
153 break; /* found a free slot */
156 limit_pfn = curr_iova->pfn_lo - 1;
159 curr = rb_prev(curr);
164 pad_size = iova_get_pad_size(size, limit_pfn);
165 if ((IOVA_START_PFN + size + pad_size) > limit_pfn) {
166 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
171 /* pfn_lo will point to size aligned address if size_aligned is set */
172 new->pfn_lo = limit_pfn - (size + pad_size) + 1;
173 new->pfn_hi = new->pfn_lo + size - 1;
175 /* Insert the new_iova into domain rbtree by holding writer lock */
176 /* Add new node and rebalance tree. */
178 struct rb_node **entry, *parent = NULL;
180 /* If we have 'prev', it's a valid place to start the
181 insertion. Otherwise, start from the root. */
185 entry = &iovad->rbroot.rb_node;
187 /* Figure out where to put new node */
189 struct iova *this = container_of(*entry,
193 if (new->pfn_lo < this->pfn_lo)
194 entry = &((*entry)->rb_left);
195 else if (new->pfn_lo > this->pfn_lo)
196 entry = &((*entry)->rb_right);
198 BUG(); /* this should not happen */
201 /* Add new node and rebalance tree. */
202 rb_link_node(&new->node, parent, entry);
203 rb_insert_color(&new->node, &iovad->rbroot);
205 __cached_rbnode_insert_update(iovad, saved_pfn, new);
207 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
214 iova_insert_rbtree(struct rb_root *root, struct iova *iova)
216 struct rb_node **new = &(root->rb_node), *parent = NULL;
217 /* Figure out where to put new node */
219 struct iova *this = container_of(*new, struct iova, node);
222 if (iova->pfn_lo < this->pfn_lo)
223 new = &((*new)->rb_left);
224 else if (iova->pfn_lo > this->pfn_lo)
225 new = &((*new)->rb_right);
227 BUG(); /* this should not happen */
229 /* Add new node and rebalance tree. */
230 rb_link_node(&iova->node, parent, new);
231 rb_insert_color(&iova->node, root);
235 * alloc_iova - allocates an iova
236 * @iovad: - iova domain in question
237 * @size: - size of page frames to allocate
238 * @limit_pfn: - max limit address
239 * @size_aligned: - set if size_aligned address range is required
240 * This function allocates an iova in the range limit_pfn to IOVA_START_PFN
241 * looking from limit_pfn instead from IOVA_START_PFN. If the size_aligned
242 * flag is set then the allocated address iova->pfn_lo will be naturally
243 * aligned on roundup_power_of_two(size).
246 alloc_iova(struct iova_domain *iovad, unsigned long size,
247 unsigned long limit_pfn,
250 struct iova *new_iova;
253 new_iova = alloc_iova_mem();
257 /* If size aligned is set then round the size to
258 * to next power of two.
261 size = __roundup_pow_of_two(size);
263 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
264 new_iova, size_aligned);
267 free_iova_mem(new_iova);
275 * find_iova - find's an iova for a given pfn
276 * @iovad: - iova domain in question.
277 * @pfn: - page frame number
278 * This function finds and returns an iova belonging to the
279 * given doamin which matches the given pfn.
281 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
284 struct rb_node *node;
286 /* Take the lock so that no other thread is manipulating the rbtree */
287 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
288 node = iovad->rbroot.rb_node;
290 struct iova *iova = container_of(node, struct iova, node);
292 /* If pfn falls within iova's range, return iova */
293 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
294 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
295 /* We are not holding the lock while this iova
296 * is referenced by the caller as the same thread
297 * which called this function also calls __free_iova()
298 * and it is by design that only one thread can possibly
299 * reference a particular iova and hence no conflict.
304 if (pfn < iova->pfn_lo)
305 node = node->rb_left;
306 else if (pfn > iova->pfn_lo)
307 node = node->rb_right;
310 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
315 * __free_iova - frees the given iova
316 * @iovad: iova domain in question.
317 * @iova: iova in question.
318 * Frees the given iova belonging to the giving domain
321 __free_iova(struct iova_domain *iovad, struct iova *iova)
325 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
326 __cached_rbnode_delete_update(iovad, iova);
327 rb_erase(&iova->node, &iovad->rbroot);
328 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
333 * free_iova - finds and frees the iova for a given pfn
334 * @iovad: - iova domain in question.
335 * @pfn: - pfn that is allocated previously
336 * This functions finds an iova for a given pfn and then
337 * frees the iova from that domain.
340 free_iova(struct iova_domain *iovad, unsigned long pfn)
342 struct iova *iova = find_iova(iovad, pfn);
344 __free_iova(iovad, iova);
349 * put_iova_domain - destroys the iova doamin
350 * @iovad: - iova domain in question.
351 * All the iova's in that domain are destroyed.
353 void put_iova_domain(struct iova_domain *iovad)
355 struct rb_node *node;
358 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
359 node = rb_first(&iovad->rbroot);
361 struct iova *iova = container_of(node, struct iova, node);
362 rb_erase(node, &iovad->rbroot);
364 node = rb_first(&iovad->rbroot);
366 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
370 __is_range_overlap(struct rb_node *node,
371 unsigned long pfn_lo, unsigned long pfn_hi)
373 struct iova *iova = container_of(node, struct iova, node);
375 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
380 static inline struct iova *
381 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
385 iova = alloc_iova_mem();
387 iova->pfn_lo = pfn_lo;
388 iova->pfn_hi = pfn_hi;
395 __insert_new_range(struct iova_domain *iovad,
396 unsigned long pfn_lo, unsigned long pfn_hi)
400 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
402 iova_insert_rbtree(&iovad->rbroot, iova);
408 __adjust_overlap_range(struct iova *iova,
409 unsigned long *pfn_lo, unsigned long *pfn_hi)
411 if (*pfn_lo < iova->pfn_lo)
412 iova->pfn_lo = *pfn_lo;
413 if (*pfn_hi > iova->pfn_hi)
414 *pfn_lo = iova->pfn_hi + 1;
418 * reserve_iova - reserves an iova in the given range
419 * @iovad: - iova domain pointer
420 * @pfn_lo: - lower page frame address
421 * @pfn_hi:- higher pfn adderss
422 * This function allocates reserves the address range from pfn_lo to pfn_hi so
423 * that this address is not dished out as part of alloc_iova.
426 reserve_iova(struct iova_domain *iovad,
427 unsigned long pfn_lo, unsigned long pfn_hi)
429 struct rb_node *node;
432 unsigned int overlap = 0;
434 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
435 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
436 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
437 iova = container_of(node, struct iova, node);
438 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
439 if ((pfn_lo >= iova->pfn_lo) &&
440 (pfn_hi <= iova->pfn_hi))
448 /* We are here either because this is the first reserver node
449 * or need to insert remaining non overlap addr range
451 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
454 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
459 * copy_reserved_iova - copies the reserved between domains
460 * @from: - source doamin from where to copy
461 * @to: - destination domin where to copy
462 * This function copies reserved iova's from one doamin to
466 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
469 struct rb_node *node;
471 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
472 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
473 struct iova *iova = container_of(node, struct iova, node);
474 struct iova *new_iova;
475 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
477 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
478 iova->pfn_lo, iova->pfn_lo);
480 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
484 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
485 unsigned long pfn_lo, unsigned long pfn_hi)
488 struct iova *prev = NULL, *next = NULL;
490 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
491 if (iova->pfn_lo < pfn_lo) {
492 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
496 if (iova->pfn_hi > pfn_hi) {
497 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
502 __cached_rbnode_delete_update(iovad, iova);
503 rb_erase(&iova->node, &iovad->rbroot);
506 iova_insert_rbtree(&iovad->rbroot, prev);
507 iova->pfn_lo = pfn_lo;
510 iova_insert_rbtree(&iovad->rbroot, next);
511 iova->pfn_hi = pfn_hi;
513 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
518 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);