Commit | Line | Data |
---|---|---|
f8de50eb | 1 | /* |
a15a519e | 2 | * Copyright © 2006-2009, Intel Corporation. |
f8de50eb | 3 | * |
a15a519e DW |
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. | |
7 | * | |
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 | |
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. | |
f8de50eb | 16 | * |
98bcef56 | 17 | * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> |
f8de50eb KA |
18 | */ |
19 | ||
38717946 | 20 | #include <linux/iova.h> |
85b45456 RM |
21 | #include <linux/slab.h> |
22 | ||
f8de50eb | 23 | void |
0fb5fe87 RM |
24 | init_iova_domain(struct iova_domain *iovad, unsigned long granule, |
25 | unsigned long start_pfn, unsigned long pfn_32bit) | |
f8de50eb | 26 | { |
0fb5fe87 RM |
27 | /* |
28 | * IOVA granularity will normally be equal to the smallest | |
29 | * supported IOMMU page size; both *must* be capable of | |
30 | * representing individual CPU pages exactly. | |
31 | */ | |
32 | BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule)); | |
33 | ||
f8de50eb KA |
34 | spin_lock_init(&iovad->iova_rbtree_lock); |
35 | iovad->rbroot = RB_ROOT; | |
36 | iovad->cached32_node = NULL; | |
0fb5fe87 | 37 | iovad->granule = granule; |
1b722500 | 38 | iovad->start_pfn = start_pfn; |
f661197e | 39 | iovad->dma_32bit_pfn = pfn_32bit; |
f8de50eb KA |
40 | } |
41 | ||
42 | static struct rb_node * | |
43 | __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn) | |
44 | { | |
f661197e | 45 | if ((*limit_pfn != iovad->dma_32bit_pfn) || |
f8de50eb KA |
46 | (iovad->cached32_node == NULL)) |
47 | return rb_last(&iovad->rbroot); | |
48 | else { | |
49 | struct rb_node *prev_node = rb_prev(iovad->cached32_node); | |
50 | struct iova *curr_iova = | |
51 | container_of(iovad->cached32_node, struct iova, node); | |
52 | *limit_pfn = curr_iova->pfn_lo - 1; | |
53 | return prev_node; | |
54 | } | |
55 | } | |
56 | ||
57 | static void | |
58 | __cached_rbnode_insert_update(struct iova_domain *iovad, | |
59 | unsigned long limit_pfn, struct iova *new) | |
60 | { | |
f661197e | 61 | if (limit_pfn != iovad->dma_32bit_pfn) |
f8de50eb KA |
62 | return; |
63 | iovad->cached32_node = &new->node; | |
64 | } | |
65 | ||
66 | static void | |
67 | __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free) | |
68 | { | |
69 | struct iova *cached_iova; | |
70 | struct rb_node *curr; | |
71 | ||
72 | if (!iovad->cached32_node) | |
73 | return; | |
74 | curr = iovad->cached32_node; | |
75 | cached_iova = container_of(curr, struct iova, node); | |
76 | ||
1c9fc3d1 CW |
77 | if (free->pfn_lo >= cached_iova->pfn_lo) { |
78 | struct rb_node *node = rb_next(&free->node); | |
79 | struct iova *iova = container_of(node, struct iova, node); | |
80 | ||
81 | /* only cache if it's below 32bit pfn */ | |
82 | if (node && iova->pfn_lo < iovad->dma_32bit_pfn) | |
83 | iovad->cached32_node = node; | |
84 | else | |
85 | iovad->cached32_node = NULL; | |
86 | } | |
f8de50eb KA |
87 | } |
88 | ||
8f6429c7 RM |
89 | /* |
90 | * Computes the padding size required, to make the start address | |
91 | * naturally aligned on the power-of-two order of its size | |
f76aec76 | 92 | */ |
8f6429c7 RM |
93 | static unsigned int |
94 | iova_get_pad_size(unsigned int size, unsigned int limit_pfn) | |
f76aec76 | 95 | { |
8f6429c7 | 96 | return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1); |
f76aec76 KA |
97 | } |
98 | ||
ddf02886 | 99 | static int __alloc_and_insert_iova_range(struct iova_domain *iovad, |
100 | unsigned long size, unsigned long limit_pfn, | |
101 | struct iova *new, bool size_aligned) | |
f8de50eb | 102 | { |
ddf02886 | 103 | struct rb_node *prev, *curr = NULL; |
f8de50eb KA |
104 | unsigned long flags; |
105 | unsigned long saved_pfn; | |
f76aec76 | 106 | unsigned int pad_size = 0; |
f8de50eb KA |
107 | |
108 | /* Walk the tree backwards */ | |
109 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | |
110 | saved_pfn = limit_pfn; | |
111 | curr = __get_cached_rbnode(iovad, &limit_pfn); | |
ddf02886 | 112 | prev = curr; |
f8de50eb KA |
113 | while (curr) { |
114 | struct iova *curr_iova = container_of(curr, struct iova, node); | |
ddf02886 | 115 | |
f8de50eb KA |
116 | if (limit_pfn < curr_iova->pfn_lo) |
117 | goto move_left; | |
f76aec76 | 118 | else if (limit_pfn < curr_iova->pfn_hi) |
f8de50eb | 119 | goto adjust_limit_pfn; |
f76aec76 KA |
120 | else { |
121 | if (size_aligned) | |
122 | pad_size = iova_get_pad_size(size, limit_pfn); | |
123 | if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn) | |
124 | break; /* found a free slot */ | |
125 | } | |
f8de50eb KA |
126 | adjust_limit_pfn: |
127 | limit_pfn = curr_iova->pfn_lo - 1; | |
128 | move_left: | |
ddf02886 | 129 | prev = curr; |
f8de50eb KA |
130 | curr = rb_prev(curr); |
131 | } | |
132 | ||
f76aec76 KA |
133 | if (!curr) { |
134 | if (size_aligned) | |
135 | pad_size = iova_get_pad_size(size, limit_pfn); | |
1b722500 | 136 | if ((iovad->start_pfn + size + pad_size) > limit_pfn) { |
f76aec76 KA |
137 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
138 | return -ENOMEM; | |
139 | } | |
f8de50eb | 140 | } |
f76aec76 KA |
141 | |
142 | /* pfn_lo will point to size aligned address if size_aligned is set */ | |
143 | new->pfn_lo = limit_pfn - (size + pad_size) + 1; | |
144 | new->pfn_hi = new->pfn_lo + size - 1; | |
f8de50eb | 145 | |
ddf02886 | 146 | /* Insert the new_iova into domain rbtree by holding writer lock */ |
147 | /* Add new node and rebalance tree. */ | |
148 | { | |
a15a519e DW |
149 | struct rb_node **entry, *parent = NULL; |
150 | ||
151 | /* If we have 'prev', it's a valid place to start the | |
152 | insertion. Otherwise, start from the root. */ | |
153 | if (prev) | |
154 | entry = &prev; | |
155 | else | |
156 | entry = &iovad->rbroot.rb_node; | |
157 | ||
ddf02886 | 158 | /* Figure out where to put new node */ |
159 | while (*entry) { | |
160 | struct iova *this = container_of(*entry, | |
161 | struct iova, node); | |
162 | parent = *entry; | |
163 | ||
164 | if (new->pfn_lo < this->pfn_lo) | |
165 | entry = &((*entry)->rb_left); | |
166 | else if (new->pfn_lo > this->pfn_lo) | |
167 | entry = &((*entry)->rb_right); | |
168 | else | |
169 | BUG(); /* this should not happen */ | |
170 | } | |
171 | ||
172 | /* Add new node and rebalance tree. */ | |
173 | rb_link_node(&new->node, parent, entry); | |
174 | rb_insert_color(&new->node, &iovad->rbroot); | |
175 | } | |
176 | __cached_rbnode_insert_update(iovad, saved_pfn, new); | |
177 | ||
f8de50eb | 178 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
ddf02886 | 179 | |
180 | ||
f8de50eb KA |
181 | return 0; |
182 | } | |
183 | ||
184 | static void | |
185 | iova_insert_rbtree(struct rb_root *root, struct iova *iova) | |
186 | { | |
187 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
188 | /* Figure out where to put new node */ | |
189 | while (*new) { | |
190 | struct iova *this = container_of(*new, struct iova, node); | |
733cac2a | 191 | |
f8de50eb KA |
192 | parent = *new; |
193 | ||
194 | if (iova->pfn_lo < this->pfn_lo) | |
195 | new = &((*new)->rb_left); | |
196 | else if (iova->pfn_lo > this->pfn_lo) | |
197 | new = &((*new)->rb_right); | |
198 | else | |
199 | BUG(); /* this should not happen */ | |
200 | } | |
201 | /* Add new node and rebalance tree. */ | |
202 | rb_link_node(&iova->node, parent, new); | |
203 | rb_insert_color(&iova->node, root); | |
204 | } | |
205 | ||
ae1ff3d6 SA |
206 | static struct kmem_cache *iova_cache; |
207 | static unsigned int iova_cache_users; | |
208 | static DEFINE_MUTEX(iova_cache_mutex); | |
209 | ||
210 | struct iova *alloc_iova_mem(void) | |
211 | { | |
212 | return kmem_cache_alloc(iova_cache, GFP_ATOMIC); | |
213 | } | |
214 | EXPORT_SYMBOL(alloc_iova_mem); | |
215 | ||
216 | void free_iova_mem(struct iova *iova) | |
217 | { | |
218 | kmem_cache_free(iova_cache, iova); | |
219 | } | |
220 | EXPORT_SYMBOL(free_iova_mem); | |
221 | ||
222 | int iova_cache_get(void) | |
223 | { | |
224 | mutex_lock(&iova_cache_mutex); | |
225 | if (!iova_cache_users) { | |
226 | iova_cache = kmem_cache_create( | |
227 | "iommu_iova", sizeof(struct iova), 0, | |
228 | SLAB_HWCACHE_ALIGN, NULL); | |
229 | if (!iova_cache) { | |
230 | mutex_unlock(&iova_cache_mutex); | |
231 | printk(KERN_ERR "Couldn't create iova cache\n"); | |
232 | return -ENOMEM; | |
233 | } | |
234 | } | |
235 | ||
236 | iova_cache_users++; | |
237 | mutex_unlock(&iova_cache_mutex); | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | void iova_cache_put(void) | |
243 | { | |
244 | mutex_lock(&iova_cache_mutex); | |
245 | if (WARN_ON(!iova_cache_users)) { | |
246 | mutex_unlock(&iova_cache_mutex); | |
247 | return; | |
248 | } | |
249 | iova_cache_users--; | |
250 | if (!iova_cache_users) | |
251 | kmem_cache_destroy(iova_cache); | |
252 | mutex_unlock(&iova_cache_mutex); | |
253 | } | |
254 | ||
f8de50eb KA |
255 | /** |
256 | * alloc_iova - allocates an iova | |
07db0409 MI |
257 | * @iovad: - iova domain in question |
258 | * @size: - size of page frames to allocate | |
259 | * @limit_pfn: - max limit address | |
260 | * @size_aligned: - set if size_aligned address range is required | |
1b722500 RM |
261 | * This function allocates an iova in the range iovad->start_pfn to limit_pfn, |
262 | * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned | |
f76aec76 KA |
263 | * flag is set then the allocated address iova->pfn_lo will be naturally |
264 | * aligned on roundup_power_of_two(size). | |
f8de50eb KA |
265 | */ |
266 | struct iova * | |
267 | alloc_iova(struct iova_domain *iovad, unsigned long size, | |
f76aec76 KA |
268 | unsigned long limit_pfn, |
269 | bool size_aligned) | |
f8de50eb | 270 | { |
f8de50eb KA |
271 | struct iova *new_iova; |
272 | int ret; | |
273 | ||
274 | new_iova = alloc_iova_mem(); | |
275 | if (!new_iova) | |
276 | return NULL; | |
277 | ||
ddf02886 | 278 | ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn, |
279 | new_iova, size_aligned); | |
f8de50eb KA |
280 | |
281 | if (ret) { | |
f8de50eb KA |
282 | free_iova_mem(new_iova); |
283 | return NULL; | |
284 | } | |
285 | ||
f8de50eb KA |
286 | return new_iova; |
287 | } | |
288 | ||
289 | /** | |
290 | * find_iova - find's an iova for a given pfn | |
07db0409 MI |
291 | * @iovad: - iova domain in question. |
292 | * @pfn: - page frame number | |
f8de50eb KA |
293 | * This function finds and returns an iova belonging to the |
294 | * given doamin which matches the given pfn. | |
295 | */ | |
296 | struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn) | |
297 | { | |
298 | unsigned long flags; | |
299 | struct rb_node *node; | |
300 | ||
301 | /* Take the lock so that no other thread is manipulating the rbtree */ | |
302 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | |
303 | node = iovad->rbroot.rb_node; | |
304 | while (node) { | |
305 | struct iova *iova = container_of(node, struct iova, node); | |
306 | ||
307 | /* If pfn falls within iova's range, return iova */ | |
308 | if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) { | |
309 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | |
310 | /* We are not holding the lock while this iova | |
311 | * is referenced by the caller as the same thread | |
312 | * which called this function also calls __free_iova() | |
07db0409 | 313 | * and it is by design that only one thread can possibly |
f8de50eb KA |
314 | * reference a particular iova and hence no conflict. |
315 | */ | |
316 | return iova; | |
317 | } | |
318 | ||
319 | if (pfn < iova->pfn_lo) | |
320 | node = node->rb_left; | |
321 | else if (pfn > iova->pfn_lo) | |
322 | node = node->rb_right; | |
323 | } | |
324 | ||
325 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | |
326 | return NULL; | |
327 | } | |
328 | ||
329 | /** | |
330 | * __free_iova - frees the given iova | |
331 | * @iovad: iova domain in question. | |
332 | * @iova: iova in question. | |
333 | * Frees the given iova belonging to the giving domain | |
334 | */ | |
335 | void | |
336 | __free_iova(struct iova_domain *iovad, struct iova *iova) | |
337 | { | |
338 | unsigned long flags; | |
339 | ||
340 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | |
341 | __cached_rbnode_delete_update(iovad, iova); | |
342 | rb_erase(&iova->node, &iovad->rbroot); | |
343 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | |
344 | free_iova_mem(iova); | |
345 | } | |
346 | ||
347 | /** | |
348 | * free_iova - finds and frees the iova for a given pfn | |
349 | * @iovad: - iova domain in question. | |
350 | * @pfn: - pfn that is allocated previously | |
351 | * This functions finds an iova for a given pfn and then | |
352 | * frees the iova from that domain. | |
353 | */ | |
354 | void | |
355 | free_iova(struct iova_domain *iovad, unsigned long pfn) | |
356 | { | |
357 | struct iova *iova = find_iova(iovad, pfn); | |
733cac2a | 358 | |
f8de50eb KA |
359 | if (iova) |
360 | __free_iova(iovad, iova); | |
361 | ||
362 | } | |
363 | ||
364 | /** | |
365 | * put_iova_domain - destroys the iova doamin | |
366 | * @iovad: - iova domain in question. | |
367 | * All the iova's in that domain are destroyed. | |
368 | */ | |
369 | void put_iova_domain(struct iova_domain *iovad) | |
370 | { | |
371 | struct rb_node *node; | |
372 | unsigned long flags; | |
373 | ||
374 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | |
375 | node = rb_first(&iovad->rbroot); | |
376 | while (node) { | |
377 | struct iova *iova = container_of(node, struct iova, node); | |
733cac2a | 378 | |
f8de50eb KA |
379 | rb_erase(node, &iovad->rbroot); |
380 | free_iova_mem(iova); | |
381 | node = rb_first(&iovad->rbroot); | |
382 | } | |
383 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | |
384 | } | |
385 | ||
386 | static int | |
387 | __is_range_overlap(struct rb_node *node, | |
388 | unsigned long pfn_lo, unsigned long pfn_hi) | |
389 | { | |
390 | struct iova *iova = container_of(node, struct iova, node); | |
391 | ||
392 | if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo)) | |
393 | return 1; | |
394 | return 0; | |
395 | } | |
396 | ||
75f05569 JL |
397 | static inline struct iova * |
398 | alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi) | |
399 | { | |
400 | struct iova *iova; | |
401 | ||
402 | iova = alloc_iova_mem(); | |
403 | if (iova) { | |
404 | iova->pfn_lo = pfn_lo; | |
405 | iova->pfn_hi = pfn_hi; | |
406 | } | |
407 | ||
408 | return iova; | |
409 | } | |
410 | ||
f8de50eb KA |
411 | static struct iova * |
412 | __insert_new_range(struct iova_domain *iovad, | |
413 | unsigned long pfn_lo, unsigned long pfn_hi) | |
414 | { | |
415 | struct iova *iova; | |
416 | ||
75f05569 JL |
417 | iova = alloc_and_init_iova(pfn_lo, pfn_hi); |
418 | if (iova) | |
419 | iova_insert_rbtree(&iovad->rbroot, iova); | |
f8de50eb | 420 | |
f8de50eb KA |
421 | return iova; |
422 | } | |
423 | ||
424 | static void | |
425 | __adjust_overlap_range(struct iova *iova, | |
426 | unsigned long *pfn_lo, unsigned long *pfn_hi) | |
427 | { | |
428 | if (*pfn_lo < iova->pfn_lo) | |
429 | iova->pfn_lo = *pfn_lo; | |
430 | if (*pfn_hi > iova->pfn_hi) | |
431 | *pfn_lo = iova->pfn_hi + 1; | |
432 | } | |
433 | ||
434 | /** | |
435 | * reserve_iova - reserves an iova in the given range | |
436 | * @iovad: - iova domain pointer | |
437 | * @pfn_lo: - lower page frame address | |
438 | * @pfn_hi:- higher pfn adderss | |
439 | * This function allocates reserves the address range from pfn_lo to pfn_hi so | |
440 | * that this address is not dished out as part of alloc_iova. | |
441 | */ | |
442 | struct iova * | |
443 | reserve_iova(struct iova_domain *iovad, | |
444 | unsigned long pfn_lo, unsigned long pfn_hi) | |
445 | { | |
446 | struct rb_node *node; | |
447 | unsigned long flags; | |
448 | struct iova *iova; | |
449 | unsigned int overlap = 0; | |
450 | ||
3d39cecc | 451 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
f8de50eb KA |
452 | for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) { |
453 | if (__is_range_overlap(node, pfn_lo, pfn_hi)) { | |
454 | iova = container_of(node, struct iova, node); | |
455 | __adjust_overlap_range(iova, &pfn_lo, &pfn_hi); | |
456 | if ((pfn_lo >= iova->pfn_lo) && | |
457 | (pfn_hi <= iova->pfn_hi)) | |
458 | goto finish; | |
459 | overlap = 1; | |
460 | ||
461 | } else if (overlap) | |
462 | break; | |
463 | } | |
464 | ||
25985edc | 465 | /* We are here either because this is the first reserver node |
f8de50eb KA |
466 | * or need to insert remaining non overlap addr range |
467 | */ | |
468 | iova = __insert_new_range(iovad, pfn_lo, pfn_hi); | |
469 | finish: | |
470 | ||
3d39cecc | 471 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
f8de50eb KA |
472 | return iova; |
473 | } | |
474 | ||
475 | /** | |
476 | * copy_reserved_iova - copies the reserved between domains | |
477 | * @from: - source doamin from where to copy | |
478 | * @to: - destination domin where to copy | |
479 | * This function copies reserved iova's from one doamin to | |
480 | * other. | |
481 | */ | |
482 | void | |
483 | copy_reserved_iova(struct iova_domain *from, struct iova_domain *to) | |
484 | { | |
485 | unsigned long flags; | |
486 | struct rb_node *node; | |
487 | ||
3d39cecc | 488 | spin_lock_irqsave(&from->iova_rbtree_lock, flags); |
f8de50eb KA |
489 | for (node = rb_first(&from->rbroot); node; node = rb_next(node)) { |
490 | struct iova *iova = container_of(node, struct iova, node); | |
491 | struct iova *new_iova; | |
733cac2a | 492 | |
f8de50eb KA |
493 | new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi); |
494 | if (!new_iova) | |
495 | printk(KERN_ERR "Reserve iova range %lx@%lx failed\n", | |
496 | iova->pfn_lo, iova->pfn_lo); | |
497 | } | |
3d39cecc | 498 | spin_unlock_irqrestore(&from->iova_rbtree_lock, flags); |
f8de50eb | 499 | } |
75f05569 JL |
500 | |
501 | struct iova * | |
502 | split_and_remove_iova(struct iova_domain *iovad, struct iova *iova, | |
503 | unsigned long pfn_lo, unsigned long pfn_hi) | |
504 | { | |
505 | unsigned long flags; | |
506 | struct iova *prev = NULL, *next = NULL; | |
507 | ||
508 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | |
509 | if (iova->pfn_lo < pfn_lo) { | |
510 | prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1); | |
511 | if (prev == NULL) | |
512 | goto error; | |
513 | } | |
514 | if (iova->pfn_hi > pfn_hi) { | |
515 | next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi); | |
516 | if (next == NULL) | |
517 | goto error; | |
518 | } | |
519 | ||
520 | __cached_rbnode_delete_update(iovad, iova); | |
521 | rb_erase(&iova->node, &iovad->rbroot); | |
522 | ||
523 | if (prev) { | |
524 | iova_insert_rbtree(&iovad->rbroot, prev); | |
525 | iova->pfn_lo = pfn_lo; | |
526 | } | |
527 | if (next) { | |
528 | iova_insert_rbtree(&iovad->rbroot, next); | |
529 | iova->pfn_hi = pfn_hi; | |
530 | } | |
531 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | |
532 | ||
533 | return iova; | |
534 | ||
535 | error: | |
536 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | |
537 | if (prev) | |
538 | free_iova_mem(prev); | |
539 | return NULL; | |
540 | } |