blob: a3dbba8caa19f8a77d3031624b7d4f76a0674b7d [file] [log] [blame]
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07001/*
David Woodhousea15a5192009-07-01 18:49:06 +01002 * Copyright © 2006-2009, Intel Corporation.
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07003 *
David Woodhousea15a5192009-07-01 18:49:06 +01004 * 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.
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07007 *
David Woodhousea15a5192009-07-01 18:49:06 +01008 * 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
11 * more details.
12 *
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.
16 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070018 */
19
Kay, Allen M38717942008-09-09 18:37:29 +030020#include <linux/iova.h>
Robin Murphy85b45452015-01-12 17:51:14 +000021#include <linux/slab.h>
22
23static struct kmem_cache *iommu_iova_cache;
24
25int iommu_iova_cache_init(void)
26{
27 int ret = 0;
28
29 iommu_iova_cache = kmem_cache_create("iommu_iova",
30 sizeof(struct iova),
31 0,
32 SLAB_HWCACHE_ALIGN,
33 NULL);
34 if (!iommu_iova_cache) {
35 pr_err("Couldn't create iova cache\n");
36 ret = -ENOMEM;
37 }
38
39 return ret;
40}
41
42void iommu_iova_cache_destroy(void)
43{
44 kmem_cache_destroy(iommu_iova_cache);
45}
46
47struct iova *alloc_iova_mem(void)
48{
49 return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
50}
51
52void free_iova_mem(struct iova *iova)
53{
54 kmem_cache_free(iommu_iova_cache, iova);
55}
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070056
57void
Robin Murphy1b722502015-01-12 17:51:15 +000058init_iova_domain(struct iova_domain *iovad, unsigned long start_pfn,
59 unsigned long pfn_32bit)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070060{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070061 spin_lock_init(&iovad->iova_rbtree_lock);
62 iovad->rbroot = RB_ROOT;
63 iovad->cached32_node = NULL;
Robin Murphy1b722502015-01-12 17:51:15 +000064 iovad->start_pfn = start_pfn;
David Millerf6611972008-02-06 01:36:23 -080065 iovad->dma_32bit_pfn = pfn_32bit;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070066}
67
68static struct rb_node *
69__get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
70{
David Millerf6611972008-02-06 01:36:23 -080071 if ((*limit_pfn != iovad->dma_32bit_pfn) ||
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070072 (iovad->cached32_node == NULL))
73 return rb_last(&iovad->rbroot);
74 else {
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;
79 return prev_node;
80 }
81}
82
83static void
84__cached_rbnode_insert_update(struct iova_domain *iovad,
85 unsigned long limit_pfn, struct iova *new)
86{
David Millerf6611972008-02-06 01:36:23 -080087 if (limit_pfn != iovad->dma_32bit_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070088 return;
89 iovad->cached32_node = &new->node;
90}
91
92static void
93__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
94{
95 struct iova *cached_iova;
96 struct rb_node *curr;
97
98 if (!iovad->cached32_node)
99 return;
100 curr = iovad->cached32_node;
101 cached_iova = container_of(curr, struct iova, node);
102
Chris Wright1c9fc3d2011-05-28 13:15:04 -0500103 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);
106
107 /* only cache if it's below 32bit pfn */
108 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
109 iovad->cached32_node = node;
110 else
111 iovad->cached32_node = NULL;
112 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700113}
114
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700115/* Computes the padding size required, to make the
116 * the start address naturally aligned on its size
117 */
118static int
119iova_get_pad_size(int size, unsigned int limit_pfn)
120{
121 unsigned int pad_size = 0;
122 unsigned int order = ilog2(size);
123
124 if (order)
125 pad_size = (limit_pfn + 1) % (1 << order);
126
127 return pad_size;
128}
129
mark grossddf02882008-03-04 15:22:04 -0800130static 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)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700133{
mark grossddf02882008-03-04 15:22:04 -0800134 struct rb_node *prev, *curr = NULL;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700135 unsigned long flags;
136 unsigned long saved_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700137 unsigned int pad_size = 0;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700138
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);
mark grossddf02882008-03-04 15:22:04 -0800143 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700144 while (curr) {
145 struct iova *curr_iova = container_of(curr, struct iova, node);
mark grossddf02882008-03-04 15:22:04 -0800146
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700147 if (limit_pfn < curr_iova->pfn_lo)
148 goto move_left;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700149 else if (limit_pfn < curr_iova->pfn_hi)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700150 goto adjust_limit_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700151 else {
152 if (size_aligned)
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 */
156 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700157adjust_limit_pfn:
158 limit_pfn = curr_iova->pfn_lo - 1;
159move_left:
mark grossddf02882008-03-04 15:22:04 -0800160 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700161 curr = rb_prev(curr);
162 }
163
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700164 if (!curr) {
165 if (size_aligned)
166 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy1b722502015-01-12 17:51:15 +0000167 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700168 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
169 return -ENOMEM;
170 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700171 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700172
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;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700176
mark grossddf02882008-03-04 15:22:04 -0800177 /* Insert the new_iova into domain rbtree by holding writer lock */
178 /* Add new node and rebalance tree. */
179 {
David Woodhousea15a5192009-07-01 18:49:06 +0100180 struct rb_node **entry, *parent = NULL;
181
182 /* If we have 'prev', it's a valid place to start the
183 insertion. Otherwise, start from the root. */
184 if (prev)
185 entry = &prev;
186 else
187 entry = &iovad->rbroot.rb_node;
188
mark grossddf02882008-03-04 15:22:04 -0800189 /* Figure out where to put new node */
190 while (*entry) {
191 struct iova *this = container_of(*entry,
192 struct iova, node);
193 parent = *entry;
194
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);
199 else
200 BUG(); /* this should not happen */
201 }
202
203 /* Add new node and rebalance tree. */
204 rb_link_node(&new->node, parent, entry);
205 rb_insert_color(&new->node, &iovad->rbroot);
206 }
207 __cached_rbnode_insert_update(iovad, saved_pfn, new);
208
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700209 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
mark grossddf02882008-03-04 15:22:04 -0800210
211
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700212 return 0;
213}
214
215static void
216iova_insert_rbtree(struct rb_root *root, struct iova *iova)
217{
218 struct rb_node **new = &(root->rb_node), *parent = NULL;
219 /* Figure out where to put new node */
220 while (*new) {
221 struct iova *this = container_of(*new, struct iova, node);
222 parent = *new;
223
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);
228 else
229 BUG(); /* this should not happen */
230 }
231 /* Add new node and rebalance tree. */
232 rb_link_node(&iova->node, parent, new);
233 rb_insert_color(&iova->node, root);
234}
235
236/**
237 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900238 * @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
Robin Murphy1b722502015-01-12 17:51:15 +0000242 * 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
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700244 * flag is set then the allocated address iova->pfn_lo will be naturally
245 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700246 */
247struct iova *
248alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700249 unsigned long limit_pfn,
250 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700251{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700252 struct iova *new_iova;
253 int ret;
254
255 new_iova = alloc_iova_mem();
256 if (!new_iova)
257 return NULL;
258
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700259 /* If size aligned is set then round the size to
260 * to next power of two.
261 */
262 if (size_aligned)
263 size = __roundup_pow_of_two(size);
264
mark grossddf02882008-03-04 15:22:04 -0800265 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
266 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700267
268 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700269 free_iova_mem(new_iova);
270 return NULL;
271 }
272
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700273 return new_iova;
274}
275
276/**
277 * find_iova - find's an iova for a given pfn
Masanari Iida07db0402012-07-22 02:21:32 +0900278 * @iovad: - iova domain in question.
279 * @pfn: - page frame number
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700280 * This function finds and returns an iova belonging to the
281 * given doamin which matches the given pfn.
282 */
283struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
284{
285 unsigned long flags;
286 struct rb_node *node;
287
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;
291 while (node) {
292 struct iova *iova = container_of(node, struct iova, node);
293
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()
Masanari Iida07db0402012-07-22 02:21:32 +0900300 * and it is by design that only one thread can possibly
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700301 * reference a particular iova and hence no conflict.
302 */
303 return iova;
304 }
305
306 if (pfn < iova->pfn_lo)
307 node = node->rb_left;
308 else if (pfn > iova->pfn_lo)
309 node = node->rb_right;
310 }
311
312 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
313 return NULL;
314}
315
316/**
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
321 */
322void
323__free_iova(struct iova_domain *iovad, struct iova *iova)
324{
325 unsigned long flags;
326
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);
331 free_iova_mem(iova);
332}
333
334/**
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.
340 */
341void
342free_iova(struct iova_domain *iovad, unsigned long pfn)
343{
344 struct iova *iova = find_iova(iovad, pfn);
345 if (iova)
346 __free_iova(iovad, iova);
347
348}
349
350/**
351 * put_iova_domain - destroys the iova doamin
352 * @iovad: - iova domain in question.
353 * All the iova's in that domain are destroyed.
354 */
355void put_iova_domain(struct iova_domain *iovad)
356{
357 struct rb_node *node;
358 unsigned long flags;
359
360 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
361 node = rb_first(&iovad->rbroot);
362 while (node) {
363 struct iova *iova = container_of(node, struct iova, node);
364 rb_erase(node, &iovad->rbroot);
365 free_iova_mem(iova);
366 node = rb_first(&iovad->rbroot);
367 }
368 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
369}
370
371static int
372__is_range_overlap(struct rb_node *node,
373 unsigned long pfn_lo, unsigned long pfn_hi)
374{
375 struct iova *iova = container_of(node, struct iova, node);
376
377 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
378 return 1;
379 return 0;
380}
381
Jiang Liu75f05562014-02-19 14:07:37 +0800382static inline struct iova *
383alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
384{
385 struct iova *iova;
386
387 iova = alloc_iova_mem();
388 if (iova) {
389 iova->pfn_lo = pfn_lo;
390 iova->pfn_hi = pfn_hi;
391 }
392
393 return iova;
394}
395
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700396static struct iova *
397__insert_new_range(struct iova_domain *iovad,
398 unsigned long pfn_lo, unsigned long pfn_hi)
399{
400 struct iova *iova;
401
Jiang Liu75f05562014-02-19 14:07:37 +0800402 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
403 if (iova)
404 iova_insert_rbtree(&iovad->rbroot, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700405
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700406 return iova;
407}
408
409static void
410__adjust_overlap_range(struct iova *iova,
411 unsigned long *pfn_lo, unsigned long *pfn_hi)
412{
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;
417}
418
419/**
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.
426 */
427struct iova *
428reserve_iova(struct iova_domain *iovad,
429 unsigned long pfn_lo, unsigned long pfn_hi)
430{
431 struct rb_node *node;
432 unsigned long flags;
433 struct iova *iova;
434 unsigned int overlap = 0;
435
David Woodhouse3d39cec2009-07-08 15:23:30 +0100436 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700437 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))
443 goto finish;
444 overlap = 1;
445
446 } else if (overlap)
447 break;
448 }
449
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300450 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700451 * or need to insert remaining non overlap addr range
452 */
453 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
454finish:
455
David Woodhouse3d39cec2009-07-08 15:23:30 +0100456 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700457 return iova;
458}
459
460/**
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
465 * other.
466 */
467void
468copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
469{
470 unsigned long flags;
471 struct rb_node *node;
472
David Woodhouse3d39cec2009-07-08 15:23:30 +0100473 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700474 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);
478 if (!new_iova)
479 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
480 iova->pfn_lo, iova->pfn_lo);
481 }
David Woodhouse3d39cec2009-07-08 15:23:30 +0100482 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700483}
Jiang Liu75f05562014-02-19 14:07:37 +0800484
485struct iova *
486split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
487 unsigned long pfn_lo, unsigned long pfn_hi)
488{
489 unsigned long flags;
490 struct iova *prev = NULL, *next = NULL;
491
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);
495 if (prev == NULL)
496 goto error;
497 }
498 if (iova->pfn_hi > pfn_hi) {
499 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
500 if (next == NULL)
501 goto error;
502 }
503
504 __cached_rbnode_delete_update(iovad, iova);
505 rb_erase(&iova->node, &iovad->rbroot);
506
507 if (prev) {
508 iova_insert_rbtree(&iovad->rbroot, prev);
509 iova->pfn_lo = pfn_lo;
510 }
511 if (next) {
512 iova_insert_rbtree(&iovad->rbroot, next);
513 iova->pfn_hi = pfn_hi;
514 }
515 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
516
517 return iova;
518
519error:
520 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
521 if (prev)
522 free_iova_mem(prev);
523 return NULL;
524}