blob: ed95f7a0fad31f05f6ff6903c008d7911dc04117 [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
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070023void
Robin Murphy0fb5fe82015-01-12 17:51:16 +000024init_iova_domain(struct iova_domain *iovad, unsigned long granule,
25 unsigned long start_pfn, unsigned long pfn_32bit)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070026{
Robin Murphy0fb5fe82015-01-12 17:51:16 +000027 /*
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
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070034 spin_lock_init(&iovad->iova_rbtree_lock);
35 iovad->rbroot = RB_ROOT;
36 iovad->cached32_node = NULL;
Robin Murphy0fb5fe82015-01-12 17:51:16 +000037 iovad->granule = granule;
Robin Murphy1b722502015-01-12 17:51:15 +000038 iovad->start_pfn = start_pfn;
David Millerf6611972008-02-06 01:36:23 -080039 iovad->dma_32bit_pfn = pfn_32bit;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070040}
41
42static struct rb_node *
43__get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
44{
David Millerf6611972008-02-06 01:36:23 -080045 if ((*limit_pfn != iovad->dma_32bit_pfn) ||
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070046 (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
57static void
58__cached_rbnode_insert_update(struct iova_domain *iovad,
59 unsigned long limit_pfn, struct iova *new)
60{
David Millerf6611972008-02-06 01:36:23 -080061 if (limit_pfn != iovad->dma_32bit_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070062 return;
63 iovad->cached32_node = &new->node;
64}
65
66static 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
Chris Wright1c9fc3d2011-05-28 13:15:04 -050077 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 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070087}
88
Robin Murphy8f6429c2015-07-16 19:40:12 +010089/*
90 * Computes the padding size required, to make the start address
91 * naturally aligned on the power-of-two order of its size
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -070092 */
Robin Murphy8f6429c2015-07-16 19:40:12 +010093static unsigned int
94iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -070095{
Robin Murphy8f6429c2015-07-16 19:40:12 +010096 return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -070097}
98
mark grossddf02882008-03-04 15:22:04 -080099static 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)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700102{
mark grossddf02882008-03-04 15:22:04 -0800103 struct rb_node *prev, *curr = NULL;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700104 unsigned long flags;
105 unsigned long saved_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700106 unsigned int pad_size = 0;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700107
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);
mark grossddf02882008-03-04 15:22:04 -0800112 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700113 while (curr) {
114 struct iova *curr_iova = container_of(curr, struct iova, node);
mark grossddf02882008-03-04 15:22:04 -0800115
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700116 if (limit_pfn < curr_iova->pfn_lo)
117 goto move_left;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700118 else if (limit_pfn < curr_iova->pfn_hi)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700119 goto adjust_limit_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700120 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 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700126adjust_limit_pfn:
127 limit_pfn = curr_iova->pfn_lo - 1;
128move_left:
mark grossddf02882008-03-04 15:22:04 -0800129 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700130 curr = rb_prev(curr);
131 }
132
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700133 if (!curr) {
134 if (size_aligned)
135 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy1b722502015-01-12 17:51:15 +0000136 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700137 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
138 return -ENOMEM;
139 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700140 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700141
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;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700145
mark grossddf02882008-03-04 15:22:04 -0800146 /* Insert the new_iova into domain rbtree by holding writer lock */
147 /* Add new node and rebalance tree. */
148 {
David Woodhousea15a5192009-07-01 18:49:06 +0100149 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
mark grossddf02882008-03-04 15:22:04 -0800158 /* 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
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700178 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
mark grossddf02882008-03-04 15:22:04 -0800179
180
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700181 return 0;
182}
183
184static void
185iova_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);
Robert Callicotte733cac22015-04-16 23:32:47 -0500191
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700192 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
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300206static struct kmem_cache *iova_cache;
207static unsigned int iova_cache_users;
208static DEFINE_MUTEX(iova_cache_mutex);
209
210struct iova *alloc_iova_mem(void)
211{
212 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
213}
214EXPORT_SYMBOL(alloc_iova_mem);
215
216void free_iova_mem(struct iova *iova)
217{
218 kmem_cache_free(iova_cache, iova);
219}
220EXPORT_SYMBOL(free_iova_mem);
221
222int 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
242void 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
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700255/**
256 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900257 * @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
Robin Murphy1b722502015-01-12 17:51:15 +0000261 * 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
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700263 * flag is set then the allocated address iova->pfn_lo will be naturally
264 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700265 */
266struct iova *
267alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700268 unsigned long limit_pfn,
269 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700270{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700271 struct iova *new_iova;
272 int ret;
273
274 new_iova = alloc_iova_mem();
275 if (!new_iova)
276 return NULL;
277
mark grossddf02882008-03-04 15:22:04 -0800278 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
279 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700280
281 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700282 free_iova_mem(new_iova);
283 return NULL;
284 }
285
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700286 return new_iova;
287}
288
289/**
290 * find_iova - find's an iova for a given pfn
Masanari Iida07db0402012-07-22 02:21:32 +0900291 * @iovad: - iova domain in question.
292 * @pfn: - page frame number
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700293 * This function finds and returns an iova belonging to the
294 * given doamin which matches the given pfn.
295 */
296struct 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()
Masanari Iida07db0402012-07-22 02:21:32 +0900313 * and it is by design that only one thread can possibly
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700314 * 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 */
335void
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 */
354void
355free_iova(struct iova_domain *iovad, unsigned long pfn)
356{
357 struct iova *iova = find_iova(iovad, pfn);
Robert Callicotte733cac22015-04-16 23:32:47 -0500358
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700359 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 */
369void 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);
Robert Callicotte733cac22015-04-16 23:32:47 -0500378
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700379 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
386static 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
Jiang Liu75f05562014-02-19 14:07:37 +0800397static inline struct iova *
398alloc_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
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700411static 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
Jiang Liu75f05562014-02-19 14:07:37 +0800417 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
418 if (iova)
419 iova_insert_rbtree(&iovad->rbroot, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700420
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700421 return iova;
422}
423
424static 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 */
442struct iova *
443reserve_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
David Woodhouse3d39cec2009-07-08 15:23:30 +0100451 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700452 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300465 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700466 * or need to insert remaining non overlap addr range
467 */
468 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
469finish:
470
David Woodhouse3d39cec2009-07-08 15:23:30 +0100471 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700472 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 */
482void
483copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
484{
485 unsigned long flags;
486 struct rb_node *node;
487
David Woodhouse3d39cec2009-07-08 15:23:30 +0100488 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700489 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;
Robert Callicotte733cac22015-04-16 23:32:47 -0500492
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700493 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 }
David Woodhouse3d39cec2009-07-08 15:23:30 +0100498 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700499}
Jiang Liu75f05562014-02-19 14:07:37 +0800500
501struct iova *
502split_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
535error:
536 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
537 if (prev)
538 free_iova_mem(prev);
539 return NULL;
540}