Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 1 | /* |
David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 2 | * Copyright © 2006-2009, Intel Corporation. |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 3 | * |
David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 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. |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 7 | * |
David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 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. |
| 16 | * |
mark gross | 98bcef5 | 2008-02-23 15:23:35 -0800 | [diff] [blame] | 17 | * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 20 | #include <linux/iova.h> |
Robin Murphy | 85b4545 | 2015-01-12 17:51:14 +0000 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 23 | void |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 24 | init_iova_domain(struct iova_domain *iovad, unsigned long granule, |
| 25 | unsigned long start_pfn, unsigned long pfn_32bit) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 26 | { |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 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 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 34 | spin_lock_init(&iovad->iova_rbtree_lock); |
| 35 | iovad->rbroot = RB_ROOT; |
| 36 | iovad->cached32_node = NULL; |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 37 | iovad->granule = granule; |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 38 | iovad->start_pfn = start_pfn; |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 39 | iovad->dma_32bit_pfn = pfn_32bit; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static struct rb_node * |
| 43 | __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn) |
| 44 | { |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 45 | if ((*limit_pfn != iovad->dma_32bit_pfn) || |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | { |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 61 | if (limit_pfn != iovad->dma_32bit_pfn) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
Chris Wright | 1c9fc3d | 2011-05-28 13:15:04 -0500 | [diff] [blame] | 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 | } |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Robin Murphy | 8f6429c | 2015-07-16 19:40:12 +0100 | [diff] [blame] | 89 | /* |
| 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 S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 92 | */ |
Robin Murphy | 8f6429c | 2015-07-16 19:40:12 +0100 | [diff] [blame] | 93 | static unsigned int |
| 94 | iova_get_pad_size(unsigned int size, unsigned int limit_pfn) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 95 | { |
Robin Murphy | 8f6429c | 2015-07-16 19:40:12 +0100 | [diff] [blame] | 96 | return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 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) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 102 | { |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 103 | struct rb_node *prev, *curr = NULL; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 104 | unsigned long flags; |
| 105 | unsigned long saved_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 106 | unsigned int pad_size = 0; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 112 | prev = curr; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 113 | while (curr) { |
| 114 | struct iova *curr_iova = container_of(curr, struct iova, node); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 115 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 116 | if (limit_pfn < curr_iova->pfn_lo) |
| 117 | goto move_left; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 118 | else if (limit_pfn < curr_iova->pfn_hi) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 119 | goto adjust_limit_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 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 | } |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 126 | adjust_limit_pfn: |
| 127 | limit_pfn = curr_iova->pfn_lo - 1; |
| 128 | move_left: |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 129 | prev = curr; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 130 | curr = rb_prev(curr); |
| 131 | } |
| 132 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 133 | if (!curr) { |
| 134 | if (size_aligned) |
| 135 | pad_size = iova_get_pad_size(size, limit_pfn); |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 136 | if ((iovad->start_pfn + size + pad_size) > limit_pfn) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 137 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 138 | return -ENOMEM; |
| 139 | } |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 140 | } |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 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; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 145 | |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 146 | /* Insert the new_iova into domain rbtree by holding writer lock */ |
| 147 | /* Add new node and rebalance tree. */ |
| 148 | { |
David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 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 | |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 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 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 178 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 179 | |
| 180 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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); |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 191 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame^] | 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 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 255 | /** |
| 256 | * alloc_iova - allocates an iova |
Masanari Iida | 07db040 | 2012-07-22 02:21:32 +0900 | [diff] [blame] | 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 |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 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 |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 263 | * flag is set then the allocated address iova->pfn_lo will be naturally |
| 264 | * aligned on roundup_power_of_two(size). |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 265 | */ |
| 266 | struct iova * |
| 267 | alloc_iova(struct iova_domain *iovad, unsigned long size, |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 268 | unsigned long limit_pfn, |
| 269 | bool size_aligned) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 270 | { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 271 | struct iova *new_iova; |
| 272 | int ret; |
| 273 | |
| 274 | new_iova = alloc_iova_mem(); |
| 275 | if (!new_iova) |
| 276 | return NULL; |
| 277 | |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 278 | ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn, |
| 279 | new_iova, size_aligned); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 280 | |
| 281 | if (ret) { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 282 | free_iova_mem(new_iova); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 286 | return new_iova; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * find_iova - find's an iova for a given pfn |
Masanari Iida | 07db040 | 2012-07-22 02:21:32 +0900 | [diff] [blame] | 291 | * @iovad: - iova domain in question. |
| 292 | * @pfn: - page frame number |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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() |
Masanari Iida | 07db040 | 2012-07-22 02:21:32 +0900 | [diff] [blame] | 313 | * and it is by design that only one thread can possibly |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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); |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 358 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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); |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 378 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 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 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 417 | iova = alloc_and_init_iova(pfn_lo, pfn_hi); |
| 418 | if (iova) |
| 419 | iova_insert_rbtree(&iovad->rbroot, iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 420 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 451 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 465 | /* We are here either because this is the first reserver node |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 466 | * or need to insert remaining non overlap addr range |
| 467 | */ |
| 468 | iova = __insert_new_range(iovad, pfn_lo, pfn_hi); |
| 469 | finish: |
| 470 | |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 471 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 488 | spin_lock_irqsave(&from->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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; |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 492 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 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 | } |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 498 | spin_unlock_irqrestore(&from->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 499 | } |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 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 | } |