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> |
Sakari Ailus | 15bbdec | 2015-07-13 14:31:30 +0300 | [diff] [blame] | 21 | #include <linux/module.h> |
Robin Murphy | 85b4545 | 2015-01-12 17:51:14 +0000 | [diff] [blame] | 22 | #include <linux/slab.h> |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 23 | #include <linux/smp.h> |
| 24 | #include <linux/bitops.h> |
Sebastian Andrzej Siewior | aaffaa8 | 2017-06-27 18:16:47 +0200 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 26 | |
| 27 | static bool iova_rcache_insert(struct iova_domain *iovad, |
| 28 | unsigned long pfn, |
| 29 | unsigned long size); |
| 30 | static unsigned long iova_rcache_get(struct iova_domain *iovad, |
| 31 | unsigned long size, |
| 32 | unsigned long limit_pfn); |
| 33 | static void init_iova_rcaches(struct iova_domain *iovad); |
| 34 | static void free_iova_rcaches(struct iova_domain *iovad); |
Joerg Roedel | 1928210 | 2017-08-10 15:49:44 +0200 | [diff] [blame^] | 35 | static void fq_destroy_all_entries(struct iova_domain *iovad); |
Robin Murphy | 85b4545 | 2015-01-12 17:51:14 +0000 | [diff] [blame] | 36 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 37 | void |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 38 | init_iova_domain(struct iova_domain *iovad, unsigned long granule, |
| 39 | unsigned long start_pfn, unsigned long pfn_32bit) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 40 | { |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 41 | /* |
| 42 | * IOVA granularity will normally be equal to the smallest |
| 43 | * supported IOMMU page size; both *must* be capable of |
| 44 | * representing individual CPU pages exactly. |
| 45 | */ |
| 46 | BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule)); |
| 47 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 48 | spin_lock_init(&iovad->iova_rbtree_lock); |
| 49 | iovad->rbroot = RB_ROOT; |
| 50 | iovad->cached32_node = NULL; |
Robin Murphy | 0fb5fe8 | 2015-01-12 17:51:16 +0000 | [diff] [blame] | 51 | iovad->granule = granule; |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 52 | iovad->start_pfn = start_pfn; |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 53 | iovad->dma_32bit_pfn = pfn_32bit + 1; |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 54 | iovad->flush_cb = NULL; |
| 55 | iovad->fq = NULL; |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 56 | init_iova_rcaches(iovad); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 57 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 58 | EXPORT_SYMBOL_GPL(init_iova_domain); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 59 | |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 60 | static void free_iova_flush_queue(struct iova_domain *iovad) |
| 61 | { |
| 62 | if (!iovad->fq) |
| 63 | return; |
| 64 | |
Joerg Roedel | 1928210 | 2017-08-10 15:49:44 +0200 | [diff] [blame^] | 65 | fq_destroy_all_entries(iovad); |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 66 | free_percpu(iovad->fq); |
| 67 | |
| 68 | iovad->fq = NULL; |
| 69 | iovad->flush_cb = NULL; |
| 70 | iovad->entry_dtor = NULL; |
| 71 | } |
| 72 | |
| 73 | int init_iova_flush_queue(struct iova_domain *iovad, |
| 74 | iova_flush_cb flush_cb, iova_entry_dtor entry_dtor) |
| 75 | { |
| 76 | int cpu; |
| 77 | |
| 78 | iovad->fq = alloc_percpu(struct iova_fq); |
| 79 | if (!iovad->fq) |
| 80 | return -ENOMEM; |
| 81 | |
| 82 | iovad->flush_cb = flush_cb; |
| 83 | iovad->entry_dtor = entry_dtor; |
| 84 | |
| 85 | for_each_possible_cpu(cpu) { |
| 86 | struct iova_fq *fq; |
| 87 | |
| 88 | fq = per_cpu_ptr(iovad->fq, cpu); |
| 89 | fq->head = 0; |
| 90 | fq->tail = 0; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(init_iova_flush_queue); |
| 96 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 97 | static struct rb_node * |
| 98 | __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn) |
| 99 | { |
Robin Murphy | 62280cf | 2016-11-11 18:35:46 +0000 | [diff] [blame] | 100 | if ((*limit_pfn > iovad->dma_32bit_pfn) || |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 101 | (iovad->cached32_node == NULL)) |
| 102 | return rb_last(&iovad->rbroot); |
| 103 | else { |
| 104 | struct rb_node *prev_node = rb_prev(iovad->cached32_node); |
| 105 | struct iova *curr_iova = |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 106 | rb_entry(iovad->cached32_node, struct iova, node); |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 107 | *limit_pfn = curr_iova->pfn_lo; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 108 | return prev_node; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | __cached_rbnode_insert_update(struct iova_domain *iovad, |
| 114 | unsigned long limit_pfn, struct iova *new) |
| 115 | { |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 116 | if (limit_pfn != iovad->dma_32bit_pfn) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 117 | return; |
| 118 | iovad->cached32_node = &new->node; |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free) |
| 123 | { |
| 124 | struct iova *cached_iova; |
| 125 | struct rb_node *curr; |
| 126 | |
| 127 | if (!iovad->cached32_node) |
| 128 | return; |
| 129 | curr = iovad->cached32_node; |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 130 | cached_iova = rb_entry(curr, struct iova, node); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 131 | |
Chris Wright | 1c9fc3d | 2011-05-28 13:15:04 -0500 | [diff] [blame] | 132 | if (free->pfn_lo >= cached_iova->pfn_lo) { |
| 133 | struct rb_node *node = rb_next(&free->node); |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 134 | struct iova *iova = rb_entry(node, struct iova, node); |
Chris Wright | 1c9fc3d | 2011-05-28 13:15:04 -0500 | [diff] [blame] | 135 | |
| 136 | /* only cache if it's below 32bit pfn */ |
| 137 | if (node && iova->pfn_lo < iovad->dma_32bit_pfn) |
| 138 | iovad->cached32_node = node; |
| 139 | else |
| 140 | iovad->cached32_node = NULL; |
| 141 | } |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Marek Szyprowski | d751751 | 2017-02-24 12:13:37 +0100 | [diff] [blame] | 144 | /* Insert the iova into domain rbtree by holding writer lock */ |
| 145 | static void |
| 146 | iova_insert_rbtree(struct rb_root *root, struct iova *iova, |
| 147 | struct rb_node *start) |
| 148 | { |
| 149 | struct rb_node **new, *parent = NULL; |
| 150 | |
| 151 | new = (start) ? &start : &(root->rb_node); |
| 152 | /* Figure out where to put new node */ |
| 153 | while (*new) { |
| 154 | struct iova *this = rb_entry(*new, struct iova, node); |
| 155 | |
| 156 | parent = *new; |
| 157 | |
| 158 | if (iova->pfn_lo < this->pfn_lo) |
| 159 | new = &((*new)->rb_left); |
| 160 | else if (iova->pfn_lo > this->pfn_lo) |
| 161 | new = &((*new)->rb_right); |
| 162 | else { |
| 163 | WARN_ON(1); /* this should not happen */ |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | /* Add new node and rebalance tree. */ |
| 168 | rb_link_node(&iova->node, parent, new); |
| 169 | rb_insert_color(&iova->node, root); |
| 170 | } |
| 171 | |
Robin Murphy | 8f6429c | 2015-07-16 19:40:12 +0100 | [diff] [blame] | 172 | /* |
| 173 | * Computes the padding size required, to make the start address |
| 174 | * naturally aligned on the power-of-two order of its size |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 175 | */ |
Robin Murphy | 8f6429c | 2015-07-16 19:40:12 +0100 | [diff] [blame] | 176 | static unsigned int |
| 177 | iova_get_pad_size(unsigned int size, unsigned int limit_pfn) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 178 | { |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 179 | return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 180 | } |
| 181 | |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 182 | static int __alloc_and_insert_iova_range(struct iova_domain *iovad, |
| 183 | unsigned long size, unsigned long limit_pfn, |
| 184 | struct iova *new, bool size_aligned) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 185 | { |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 186 | struct rb_node *prev, *curr = NULL; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 187 | unsigned long flags; |
| 188 | unsigned long saved_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 189 | unsigned int pad_size = 0; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 190 | |
| 191 | /* Walk the tree backwards */ |
| 192 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
| 193 | saved_pfn = limit_pfn; |
| 194 | curr = __get_cached_rbnode(iovad, &limit_pfn); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 195 | prev = curr; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 196 | while (curr) { |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 197 | struct iova *curr_iova = rb_entry(curr, struct iova, node); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 198 | |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 199 | if (limit_pfn <= curr_iova->pfn_lo) { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 200 | goto move_left; |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 201 | } else if (limit_pfn > curr_iova->pfn_hi) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 202 | if (size_aligned) |
| 203 | pad_size = iova_get_pad_size(size, limit_pfn); |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 204 | if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 205 | break; /* found a free slot */ |
| 206 | } |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 207 | limit_pfn = curr_iova->pfn_lo; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 208 | move_left: |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 209 | prev = curr; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 210 | curr = rb_prev(curr); |
| 211 | } |
| 212 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 213 | if (!curr) { |
| 214 | if (size_aligned) |
| 215 | pad_size = iova_get_pad_size(size, limit_pfn); |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 216 | if ((iovad->start_pfn + size + pad_size) > limit_pfn) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 217 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 218 | return -ENOMEM; |
| 219 | } |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 220 | } |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 221 | |
| 222 | /* pfn_lo will point to size aligned address if size_aligned is set */ |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 223 | new->pfn_lo = limit_pfn - (size + pad_size); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 224 | new->pfn_hi = new->pfn_lo + size - 1; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 225 | |
Marek Szyprowski | d751751 | 2017-02-24 12:13:37 +0100 | [diff] [blame] | 226 | /* If we have 'prev', it's a valid place to start the insertion. */ |
| 227 | iova_insert_rbtree(&iovad->rbroot, new, prev); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 228 | __cached_rbnode_insert_update(iovad, saved_pfn, new); |
| 229 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 230 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 231 | |
| 232 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 236 | static struct kmem_cache *iova_cache; |
| 237 | static unsigned int iova_cache_users; |
| 238 | static DEFINE_MUTEX(iova_cache_mutex); |
| 239 | |
| 240 | struct iova *alloc_iova_mem(void) |
| 241 | { |
| 242 | return kmem_cache_alloc(iova_cache, GFP_ATOMIC); |
| 243 | } |
| 244 | EXPORT_SYMBOL(alloc_iova_mem); |
| 245 | |
| 246 | void free_iova_mem(struct iova *iova) |
| 247 | { |
| 248 | kmem_cache_free(iova_cache, iova); |
| 249 | } |
| 250 | EXPORT_SYMBOL(free_iova_mem); |
| 251 | |
| 252 | int iova_cache_get(void) |
| 253 | { |
| 254 | mutex_lock(&iova_cache_mutex); |
| 255 | if (!iova_cache_users) { |
| 256 | iova_cache = kmem_cache_create( |
| 257 | "iommu_iova", sizeof(struct iova), 0, |
| 258 | SLAB_HWCACHE_ALIGN, NULL); |
| 259 | if (!iova_cache) { |
| 260 | mutex_unlock(&iova_cache_mutex); |
| 261 | printk(KERN_ERR "Couldn't create iova cache\n"); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | iova_cache_users++; |
| 267 | mutex_unlock(&iova_cache_mutex); |
| 268 | |
| 269 | return 0; |
| 270 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 271 | EXPORT_SYMBOL_GPL(iova_cache_get); |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 272 | |
| 273 | void iova_cache_put(void) |
| 274 | { |
| 275 | mutex_lock(&iova_cache_mutex); |
| 276 | if (WARN_ON(!iova_cache_users)) { |
| 277 | mutex_unlock(&iova_cache_mutex); |
| 278 | return; |
| 279 | } |
| 280 | iova_cache_users--; |
| 281 | if (!iova_cache_users) |
| 282 | kmem_cache_destroy(iova_cache); |
| 283 | mutex_unlock(&iova_cache_mutex); |
| 284 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(iova_cache_put); |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 286 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 287 | /** |
| 288 | * alloc_iova - allocates an iova |
Masanari Iida | 07db040 | 2012-07-22 02:21:32 +0900 | [diff] [blame] | 289 | * @iovad: - iova domain in question |
| 290 | * @size: - size of page frames to allocate |
| 291 | * @limit_pfn: - max limit address |
| 292 | * @size_aligned: - set if size_aligned address range is required |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 293 | * This function allocates an iova in the range iovad->start_pfn to limit_pfn, |
| 294 | * 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] | 295 | * flag is set then the allocated address iova->pfn_lo will be naturally |
| 296 | * aligned on roundup_power_of_two(size). |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 297 | */ |
| 298 | struct iova * |
| 299 | alloc_iova(struct iova_domain *iovad, unsigned long size, |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 300 | unsigned long limit_pfn, |
| 301 | bool size_aligned) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 302 | { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 303 | struct iova *new_iova; |
| 304 | int ret; |
| 305 | |
| 306 | new_iova = alloc_iova_mem(); |
| 307 | if (!new_iova) |
| 308 | return NULL; |
| 309 | |
Robin Murphy | 757c370 | 2017-05-16 12:26:48 +0100 | [diff] [blame] | 310 | ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1, |
mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 311 | new_iova, size_aligned); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 312 | |
| 313 | if (ret) { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 314 | free_iova_mem(new_iova); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 318 | return new_iova; |
| 319 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 320 | EXPORT_SYMBOL_GPL(alloc_iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 321 | |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 322 | static struct iova * |
| 323 | private_find_iova(struct iova_domain *iovad, unsigned long pfn) |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 324 | { |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 325 | struct rb_node *node = iovad->rbroot.rb_node; |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 326 | |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 327 | assert_spin_locked(&iovad->iova_rbtree_lock); |
| 328 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 329 | while (node) { |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 330 | struct iova *iova = rb_entry(node, struct iova, node); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 331 | |
| 332 | /* If pfn falls within iova's range, return iova */ |
| 333 | if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) { |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 334 | return iova; |
| 335 | } |
| 336 | |
| 337 | if (pfn < iova->pfn_lo) |
| 338 | node = node->rb_left; |
| 339 | else if (pfn > iova->pfn_lo) |
| 340 | node = node->rb_right; |
| 341 | } |
| 342 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 345 | |
| 346 | static void private_free_iova(struct iova_domain *iovad, struct iova *iova) |
| 347 | { |
| 348 | assert_spin_locked(&iovad->iova_rbtree_lock); |
| 349 | __cached_rbnode_delete_update(iovad, iova); |
| 350 | rb_erase(&iova->node, &iovad->rbroot); |
| 351 | free_iova_mem(iova); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * find_iova - finds an iova for a given pfn |
| 356 | * @iovad: - iova domain in question. |
| 357 | * @pfn: - page frame number |
| 358 | * This function finds and returns an iova belonging to the |
| 359 | * given doamin which matches the given pfn. |
| 360 | */ |
| 361 | struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn) |
| 362 | { |
| 363 | unsigned long flags; |
| 364 | struct iova *iova; |
| 365 | |
| 366 | /* Take the lock so that no other thread is manipulating the rbtree */ |
| 367 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
| 368 | iova = private_find_iova(iovad, pfn); |
| 369 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 370 | return iova; |
| 371 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 372 | EXPORT_SYMBOL_GPL(find_iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 373 | |
| 374 | /** |
| 375 | * __free_iova - frees the given iova |
| 376 | * @iovad: iova domain in question. |
| 377 | * @iova: iova in question. |
| 378 | * Frees the given iova belonging to the giving domain |
| 379 | */ |
| 380 | void |
| 381 | __free_iova(struct iova_domain *iovad, struct iova *iova) |
| 382 | { |
| 383 | unsigned long flags; |
| 384 | |
| 385 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 386 | private_free_iova(iovad, iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 387 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 388 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 389 | EXPORT_SYMBOL_GPL(__free_iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * free_iova - finds and frees the iova for a given pfn |
| 393 | * @iovad: - iova domain in question. |
| 394 | * @pfn: - pfn that is allocated previously |
| 395 | * This functions finds an iova for a given pfn and then |
| 396 | * frees the iova from that domain. |
| 397 | */ |
| 398 | void |
| 399 | free_iova(struct iova_domain *iovad, unsigned long pfn) |
| 400 | { |
| 401 | struct iova *iova = find_iova(iovad, pfn); |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 402 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 403 | if (iova) |
| 404 | __free_iova(iovad, iova); |
| 405 | |
| 406 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 407 | EXPORT_SYMBOL_GPL(free_iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 408 | |
| 409 | /** |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 410 | * alloc_iova_fast - allocates an iova from rcache |
| 411 | * @iovad: - iova domain in question |
| 412 | * @size: - size of page frames to allocate |
| 413 | * @limit_pfn: - max limit address |
| 414 | * This function tries to satisfy an iova allocation from the rcache, |
| 415 | * and falls back to regular allocation on failure. |
| 416 | */ |
| 417 | unsigned long |
| 418 | alloc_iova_fast(struct iova_domain *iovad, unsigned long size, |
| 419 | unsigned long limit_pfn) |
| 420 | { |
| 421 | bool flushed_rcache = false; |
| 422 | unsigned long iova_pfn; |
| 423 | struct iova *new_iova; |
| 424 | |
| 425 | iova_pfn = iova_rcache_get(iovad, size, limit_pfn); |
| 426 | if (iova_pfn) |
| 427 | return iova_pfn; |
| 428 | |
| 429 | retry: |
| 430 | new_iova = alloc_iova(iovad, size, limit_pfn, true); |
| 431 | if (!new_iova) { |
| 432 | unsigned int cpu; |
| 433 | |
| 434 | if (flushed_rcache) |
| 435 | return 0; |
| 436 | |
| 437 | /* Try replenishing IOVAs by flushing rcache. */ |
| 438 | flushed_rcache = true; |
| 439 | for_each_online_cpu(cpu) |
| 440 | free_cpu_cached_iovas(cpu, iovad); |
| 441 | goto retry; |
| 442 | } |
| 443 | |
| 444 | return new_iova->pfn_lo; |
| 445 | } |
| 446 | EXPORT_SYMBOL_GPL(alloc_iova_fast); |
| 447 | |
| 448 | /** |
| 449 | * free_iova_fast - free iova pfn range into rcache |
| 450 | * @iovad: - iova domain in question. |
| 451 | * @pfn: - pfn that is allocated previously |
| 452 | * @size: - # of pages in range |
| 453 | * This functions frees an iova range by trying to put it into the rcache, |
| 454 | * falling back to regular iova deallocation via free_iova() if this fails. |
| 455 | */ |
| 456 | void |
| 457 | free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size) |
| 458 | { |
| 459 | if (iova_rcache_insert(iovad, pfn, size)) |
| 460 | return; |
| 461 | |
| 462 | free_iova(iovad, pfn); |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(free_iova_fast); |
| 465 | |
Joerg Roedel | 1928210 | 2017-08-10 15:49:44 +0200 | [diff] [blame^] | 466 | #define fq_ring_for_each(i, fq) \ |
| 467 | for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE) |
| 468 | |
| 469 | static inline bool fq_full(struct iova_fq *fq) |
| 470 | { |
| 471 | return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head); |
| 472 | } |
| 473 | |
| 474 | static inline unsigned fq_ring_add(struct iova_fq *fq) |
| 475 | { |
| 476 | unsigned idx = fq->tail; |
| 477 | |
| 478 | fq->tail = (idx + 1) % IOVA_FQ_SIZE; |
| 479 | |
| 480 | return idx; |
| 481 | } |
| 482 | |
| 483 | static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq) |
| 484 | { |
| 485 | unsigned idx; |
| 486 | |
| 487 | fq_ring_for_each(idx, fq) { |
| 488 | |
| 489 | if (iovad->entry_dtor) |
| 490 | iovad->entry_dtor(fq->entries[idx].data); |
| 491 | |
| 492 | free_iova_fast(iovad, |
| 493 | fq->entries[idx].iova_pfn, |
| 494 | fq->entries[idx].pages); |
| 495 | } |
| 496 | |
| 497 | fq->head = 0; |
| 498 | fq->tail = 0; |
| 499 | } |
| 500 | |
| 501 | static void fq_destroy_all_entries(struct iova_domain *iovad) |
| 502 | { |
| 503 | int cpu; |
| 504 | |
| 505 | /* |
| 506 | * This code runs when the iova_domain is being detroyed, so don't |
| 507 | * bother to free iovas, just call the entry_dtor on all remaining |
| 508 | * entries. |
| 509 | */ |
| 510 | if (!iovad->entry_dtor) |
| 511 | return; |
| 512 | |
| 513 | for_each_possible_cpu(cpu) { |
| 514 | struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu); |
| 515 | int idx; |
| 516 | |
| 517 | fq_ring_for_each(idx, fq) |
| 518 | iovad->entry_dtor(fq->entries[idx].data); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void queue_iova(struct iova_domain *iovad, |
| 523 | unsigned long pfn, unsigned long pages, |
| 524 | unsigned long data) |
| 525 | { |
| 526 | struct iova_fq *fq = get_cpu_ptr(iovad->fq); |
| 527 | unsigned idx; |
| 528 | |
| 529 | if (fq_full(fq)) { |
| 530 | iovad->flush_cb(iovad); |
| 531 | fq_ring_free(iovad, fq); |
| 532 | } |
| 533 | |
| 534 | idx = fq_ring_add(fq); |
| 535 | |
| 536 | fq->entries[idx].iova_pfn = pfn; |
| 537 | fq->entries[idx].pages = pages; |
| 538 | fq->entries[idx].data = data; |
| 539 | |
| 540 | put_cpu_ptr(iovad->fq); |
| 541 | } |
| 542 | EXPORT_SYMBOL_GPL(queue_iova); |
| 543 | |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 544 | /** |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 545 | * put_iova_domain - destroys the iova doamin |
| 546 | * @iovad: - iova domain in question. |
| 547 | * All the iova's in that domain are destroyed. |
| 548 | */ |
| 549 | void put_iova_domain(struct iova_domain *iovad) |
| 550 | { |
| 551 | struct rb_node *node; |
| 552 | unsigned long flags; |
| 553 | |
Joerg Roedel | 42f87e7 | 2017-08-10 14:44:28 +0200 | [diff] [blame] | 554 | free_iova_flush_queue(iovad); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 555 | free_iova_rcaches(iovad); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 556 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
| 557 | node = rb_first(&iovad->rbroot); |
| 558 | while (node) { |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 559 | struct iova *iova = rb_entry(node, struct iova, node); |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 560 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 561 | rb_erase(node, &iovad->rbroot); |
| 562 | free_iova_mem(iova); |
| 563 | node = rb_first(&iovad->rbroot); |
| 564 | } |
| 565 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 566 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 567 | EXPORT_SYMBOL_GPL(put_iova_domain); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 568 | |
| 569 | static int |
| 570 | __is_range_overlap(struct rb_node *node, |
| 571 | unsigned long pfn_lo, unsigned long pfn_hi) |
| 572 | { |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 573 | struct iova *iova = rb_entry(node, struct iova, node); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 574 | |
| 575 | if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo)) |
| 576 | return 1; |
| 577 | return 0; |
| 578 | } |
| 579 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 580 | static inline struct iova * |
| 581 | alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi) |
| 582 | { |
| 583 | struct iova *iova; |
| 584 | |
| 585 | iova = alloc_iova_mem(); |
| 586 | if (iova) { |
| 587 | iova->pfn_lo = pfn_lo; |
| 588 | iova->pfn_hi = pfn_hi; |
| 589 | } |
| 590 | |
| 591 | return iova; |
| 592 | } |
| 593 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 594 | static struct iova * |
| 595 | __insert_new_range(struct iova_domain *iovad, |
| 596 | unsigned long pfn_lo, unsigned long pfn_hi) |
| 597 | { |
| 598 | struct iova *iova; |
| 599 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 600 | iova = alloc_and_init_iova(pfn_lo, pfn_hi); |
| 601 | if (iova) |
Marek Szyprowski | d751751 | 2017-02-24 12:13:37 +0100 | [diff] [blame] | 602 | iova_insert_rbtree(&iovad->rbroot, iova, NULL); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 603 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 604 | return iova; |
| 605 | } |
| 606 | |
| 607 | static void |
| 608 | __adjust_overlap_range(struct iova *iova, |
| 609 | unsigned long *pfn_lo, unsigned long *pfn_hi) |
| 610 | { |
| 611 | if (*pfn_lo < iova->pfn_lo) |
| 612 | iova->pfn_lo = *pfn_lo; |
| 613 | if (*pfn_hi > iova->pfn_hi) |
| 614 | *pfn_lo = iova->pfn_hi + 1; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * reserve_iova - reserves an iova in the given range |
| 619 | * @iovad: - iova domain pointer |
| 620 | * @pfn_lo: - lower page frame address |
| 621 | * @pfn_hi:- higher pfn adderss |
| 622 | * This function allocates reserves the address range from pfn_lo to pfn_hi so |
| 623 | * that this address is not dished out as part of alloc_iova. |
| 624 | */ |
| 625 | struct iova * |
| 626 | reserve_iova(struct iova_domain *iovad, |
| 627 | unsigned long pfn_lo, unsigned long pfn_hi) |
| 628 | { |
| 629 | struct rb_node *node; |
| 630 | unsigned long flags; |
| 631 | struct iova *iova; |
| 632 | unsigned int overlap = 0; |
| 633 | |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 634 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 635 | for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) { |
| 636 | if (__is_range_overlap(node, pfn_lo, pfn_hi)) { |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 637 | iova = rb_entry(node, struct iova, node); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 638 | __adjust_overlap_range(iova, &pfn_lo, &pfn_hi); |
| 639 | if ((pfn_lo >= iova->pfn_lo) && |
| 640 | (pfn_hi <= iova->pfn_hi)) |
| 641 | goto finish; |
| 642 | overlap = 1; |
| 643 | |
| 644 | } else if (overlap) |
| 645 | break; |
| 646 | } |
| 647 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 648 | /* We are here either because this is the first reserver node |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 649 | * or need to insert remaining non overlap addr range |
| 650 | */ |
| 651 | iova = __insert_new_range(iovad, pfn_lo, pfn_hi); |
| 652 | finish: |
| 653 | |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 654 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 655 | return iova; |
| 656 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 657 | EXPORT_SYMBOL_GPL(reserve_iova); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 658 | |
| 659 | /** |
| 660 | * copy_reserved_iova - copies the reserved between domains |
| 661 | * @from: - source doamin from where to copy |
| 662 | * @to: - destination domin where to copy |
| 663 | * This function copies reserved iova's from one doamin to |
| 664 | * other. |
| 665 | */ |
| 666 | void |
| 667 | copy_reserved_iova(struct iova_domain *from, struct iova_domain *to) |
| 668 | { |
| 669 | unsigned long flags; |
| 670 | struct rb_node *node; |
| 671 | |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 672 | spin_lock_irqsave(&from->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 673 | for (node = rb_first(&from->rbroot); node; node = rb_next(node)) { |
Geliang Tang | eba484b | 2016-12-19 22:46:58 +0800 | [diff] [blame] | 674 | struct iova *iova = rb_entry(node, struct iova, node); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 675 | struct iova *new_iova; |
Robert Callicotte | 733cac2 | 2015-04-16 23:32:47 -0500 | [diff] [blame] | 676 | |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 677 | new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi); |
| 678 | if (!new_iova) |
| 679 | printk(KERN_ERR "Reserve iova range %lx@%lx failed\n", |
| 680 | iova->pfn_lo, iova->pfn_lo); |
| 681 | } |
David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 682 | spin_unlock_irqrestore(&from->iova_rbtree_lock, flags); |
Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 683 | } |
Sakari Ailus | 9b41760 | 2015-07-13 14:31:29 +0300 | [diff] [blame] | 684 | EXPORT_SYMBOL_GPL(copy_reserved_iova); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 685 | |
| 686 | struct iova * |
| 687 | split_and_remove_iova(struct iova_domain *iovad, struct iova *iova, |
| 688 | unsigned long pfn_lo, unsigned long pfn_hi) |
| 689 | { |
| 690 | unsigned long flags; |
| 691 | struct iova *prev = NULL, *next = NULL; |
| 692 | |
| 693 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
| 694 | if (iova->pfn_lo < pfn_lo) { |
| 695 | prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1); |
| 696 | if (prev == NULL) |
| 697 | goto error; |
| 698 | } |
| 699 | if (iova->pfn_hi > pfn_hi) { |
| 700 | next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi); |
| 701 | if (next == NULL) |
| 702 | goto error; |
| 703 | } |
| 704 | |
| 705 | __cached_rbnode_delete_update(iovad, iova); |
| 706 | rb_erase(&iova->node, &iovad->rbroot); |
| 707 | |
| 708 | if (prev) { |
Marek Szyprowski | d751751 | 2017-02-24 12:13:37 +0100 | [diff] [blame] | 709 | iova_insert_rbtree(&iovad->rbroot, prev, NULL); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 710 | iova->pfn_lo = pfn_lo; |
| 711 | } |
| 712 | if (next) { |
Marek Szyprowski | d751751 | 2017-02-24 12:13:37 +0100 | [diff] [blame] | 713 | iova_insert_rbtree(&iovad->rbroot, next, NULL); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 714 | iova->pfn_hi = pfn_hi; |
| 715 | } |
| 716 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 717 | |
| 718 | return iova; |
| 719 | |
| 720 | error: |
| 721 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 722 | if (prev) |
| 723 | free_iova_mem(prev); |
| 724 | return NULL; |
| 725 | } |
Sakari Ailus | 15bbdec | 2015-07-13 14:31:30 +0300 | [diff] [blame] | 726 | |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 727 | /* |
| 728 | * Magazine caches for IOVA ranges. For an introduction to magazines, |
| 729 | * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab |
| 730 | * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams. |
| 731 | * For simplicity, we use a static magazine size and don't implement the |
| 732 | * dynamic size tuning described in the paper. |
| 733 | */ |
| 734 | |
| 735 | #define IOVA_MAG_SIZE 128 |
| 736 | |
| 737 | struct iova_magazine { |
| 738 | unsigned long size; |
| 739 | unsigned long pfns[IOVA_MAG_SIZE]; |
| 740 | }; |
| 741 | |
| 742 | struct iova_cpu_rcache { |
| 743 | spinlock_t lock; |
| 744 | struct iova_magazine *loaded; |
| 745 | struct iova_magazine *prev; |
| 746 | }; |
| 747 | |
| 748 | static struct iova_magazine *iova_magazine_alloc(gfp_t flags) |
| 749 | { |
| 750 | return kzalloc(sizeof(struct iova_magazine), flags); |
| 751 | } |
| 752 | |
| 753 | static void iova_magazine_free(struct iova_magazine *mag) |
| 754 | { |
| 755 | kfree(mag); |
| 756 | } |
| 757 | |
| 758 | static void |
| 759 | iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad) |
| 760 | { |
| 761 | unsigned long flags; |
| 762 | int i; |
| 763 | |
| 764 | if (!mag) |
| 765 | return; |
| 766 | |
| 767 | spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); |
| 768 | |
| 769 | for (i = 0 ; i < mag->size; ++i) { |
| 770 | struct iova *iova = private_find_iova(iovad, mag->pfns[i]); |
| 771 | |
| 772 | BUG_ON(!iova); |
| 773 | private_free_iova(iovad, iova); |
| 774 | } |
| 775 | |
| 776 | spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); |
| 777 | |
| 778 | mag->size = 0; |
| 779 | } |
| 780 | |
| 781 | static bool iova_magazine_full(struct iova_magazine *mag) |
| 782 | { |
| 783 | return (mag && mag->size == IOVA_MAG_SIZE); |
| 784 | } |
| 785 | |
| 786 | static bool iova_magazine_empty(struct iova_magazine *mag) |
| 787 | { |
| 788 | return (!mag || mag->size == 0); |
| 789 | } |
| 790 | |
| 791 | static unsigned long iova_magazine_pop(struct iova_magazine *mag, |
| 792 | unsigned long limit_pfn) |
| 793 | { |
| 794 | BUG_ON(iova_magazine_empty(mag)); |
| 795 | |
| 796 | if (mag->pfns[mag->size - 1] >= limit_pfn) |
| 797 | return 0; |
| 798 | |
| 799 | return mag->pfns[--mag->size]; |
| 800 | } |
| 801 | |
| 802 | static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn) |
| 803 | { |
| 804 | BUG_ON(iova_magazine_full(mag)); |
| 805 | |
| 806 | mag->pfns[mag->size++] = pfn; |
| 807 | } |
| 808 | |
| 809 | static void init_iova_rcaches(struct iova_domain *iovad) |
| 810 | { |
| 811 | struct iova_cpu_rcache *cpu_rcache; |
| 812 | struct iova_rcache *rcache; |
| 813 | unsigned int cpu; |
| 814 | int i; |
| 815 | |
| 816 | for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { |
| 817 | rcache = &iovad->rcaches[i]; |
| 818 | spin_lock_init(&rcache->lock); |
| 819 | rcache->depot_size = 0; |
| 820 | rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size()); |
| 821 | if (WARN_ON(!rcache->cpu_rcaches)) |
| 822 | continue; |
| 823 | for_each_possible_cpu(cpu) { |
| 824 | cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu); |
| 825 | spin_lock_init(&cpu_rcache->lock); |
| 826 | cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL); |
| 827 | cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL); |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and |
| 834 | * return true on success. Can fail if rcache is full and we can't free |
| 835 | * space, and free_iova() (our only caller) will then return the IOVA |
| 836 | * range to the rbtree instead. |
| 837 | */ |
| 838 | static bool __iova_rcache_insert(struct iova_domain *iovad, |
| 839 | struct iova_rcache *rcache, |
| 840 | unsigned long iova_pfn) |
| 841 | { |
| 842 | struct iova_magazine *mag_to_free = NULL; |
| 843 | struct iova_cpu_rcache *cpu_rcache; |
| 844 | bool can_insert = false; |
| 845 | unsigned long flags; |
| 846 | |
Sebastian Andrzej Siewior | aaffaa8 | 2017-06-27 18:16:47 +0200 | [diff] [blame] | 847 | cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 848 | spin_lock_irqsave(&cpu_rcache->lock, flags); |
| 849 | |
| 850 | if (!iova_magazine_full(cpu_rcache->loaded)) { |
| 851 | can_insert = true; |
| 852 | } else if (!iova_magazine_full(cpu_rcache->prev)) { |
| 853 | swap(cpu_rcache->prev, cpu_rcache->loaded); |
| 854 | can_insert = true; |
| 855 | } else { |
| 856 | struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC); |
| 857 | |
| 858 | if (new_mag) { |
| 859 | spin_lock(&rcache->lock); |
| 860 | if (rcache->depot_size < MAX_GLOBAL_MAGS) { |
| 861 | rcache->depot[rcache->depot_size++] = |
| 862 | cpu_rcache->loaded; |
| 863 | } else { |
| 864 | mag_to_free = cpu_rcache->loaded; |
| 865 | } |
| 866 | spin_unlock(&rcache->lock); |
| 867 | |
| 868 | cpu_rcache->loaded = new_mag; |
| 869 | can_insert = true; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | if (can_insert) |
| 874 | iova_magazine_push(cpu_rcache->loaded, iova_pfn); |
| 875 | |
| 876 | spin_unlock_irqrestore(&cpu_rcache->lock, flags); |
| 877 | |
| 878 | if (mag_to_free) { |
| 879 | iova_magazine_free_pfns(mag_to_free, iovad); |
| 880 | iova_magazine_free(mag_to_free); |
| 881 | } |
| 882 | |
| 883 | return can_insert; |
| 884 | } |
| 885 | |
| 886 | static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn, |
| 887 | unsigned long size) |
| 888 | { |
| 889 | unsigned int log_size = order_base_2(size); |
| 890 | |
| 891 | if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE) |
| 892 | return false; |
| 893 | |
| 894 | return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn); |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Caller wants to allocate a new IOVA range from 'rcache'. If we can |
| 899 | * satisfy the request, return a matching non-NULL range and remove |
| 900 | * it from the 'rcache'. |
| 901 | */ |
| 902 | static unsigned long __iova_rcache_get(struct iova_rcache *rcache, |
| 903 | unsigned long limit_pfn) |
| 904 | { |
| 905 | struct iova_cpu_rcache *cpu_rcache; |
| 906 | unsigned long iova_pfn = 0; |
| 907 | bool has_pfn = false; |
| 908 | unsigned long flags; |
| 909 | |
Sebastian Andrzej Siewior | aaffaa8 | 2017-06-27 18:16:47 +0200 | [diff] [blame] | 910 | cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches); |
Omer Peleg | 9257b4a | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 911 | spin_lock_irqsave(&cpu_rcache->lock, flags); |
| 912 | |
| 913 | if (!iova_magazine_empty(cpu_rcache->loaded)) { |
| 914 | has_pfn = true; |
| 915 | } else if (!iova_magazine_empty(cpu_rcache->prev)) { |
| 916 | swap(cpu_rcache->prev, cpu_rcache->loaded); |
| 917 | has_pfn = true; |
| 918 | } else { |
| 919 | spin_lock(&rcache->lock); |
| 920 | if (rcache->depot_size > 0) { |
| 921 | iova_magazine_free(cpu_rcache->loaded); |
| 922 | cpu_rcache->loaded = rcache->depot[--rcache->depot_size]; |
| 923 | has_pfn = true; |
| 924 | } |
| 925 | spin_unlock(&rcache->lock); |
| 926 | } |
| 927 | |
| 928 | if (has_pfn) |
| 929 | iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn); |
| 930 | |
| 931 | spin_unlock_irqrestore(&cpu_rcache->lock, flags); |
| 932 | |
| 933 | return iova_pfn; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Try to satisfy IOVA allocation range from rcache. Fail if requested |
| 938 | * size is too big or the DMA limit we are given isn't satisfied by the |
| 939 | * top element in the magazine. |
| 940 | */ |
| 941 | static unsigned long iova_rcache_get(struct iova_domain *iovad, |
| 942 | unsigned long size, |
| 943 | unsigned long limit_pfn) |
| 944 | { |
| 945 | unsigned int log_size = order_base_2(size); |
| 946 | |
| 947 | if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE) |
| 948 | return 0; |
| 949 | |
| 950 | return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn); |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Free a cpu's rcache. |
| 955 | */ |
| 956 | static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad, |
| 957 | struct iova_rcache *rcache) |
| 958 | { |
| 959 | struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu); |
| 960 | unsigned long flags; |
| 961 | |
| 962 | spin_lock_irqsave(&cpu_rcache->lock, flags); |
| 963 | |
| 964 | iova_magazine_free_pfns(cpu_rcache->loaded, iovad); |
| 965 | iova_magazine_free(cpu_rcache->loaded); |
| 966 | |
| 967 | iova_magazine_free_pfns(cpu_rcache->prev, iovad); |
| 968 | iova_magazine_free(cpu_rcache->prev); |
| 969 | |
| 970 | spin_unlock_irqrestore(&cpu_rcache->lock, flags); |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * free rcache data structures. |
| 975 | */ |
| 976 | static void free_iova_rcaches(struct iova_domain *iovad) |
| 977 | { |
| 978 | struct iova_rcache *rcache; |
| 979 | unsigned long flags; |
| 980 | unsigned int cpu; |
| 981 | int i, j; |
| 982 | |
| 983 | for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { |
| 984 | rcache = &iovad->rcaches[i]; |
| 985 | for_each_possible_cpu(cpu) |
| 986 | free_cpu_iova_rcache(cpu, iovad, rcache); |
| 987 | spin_lock_irqsave(&rcache->lock, flags); |
| 988 | free_percpu(rcache->cpu_rcaches); |
| 989 | for (j = 0; j < rcache->depot_size; ++j) { |
| 990 | iova_magazine_free_pfns(rcache->depot[j], iovad); |
| 991 | iova_magazine_free(rcache->depot[j]); |
| 992 | } |
| 993 | spin_unlock_irqrestore(&rcache->lock, flags); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * free all the IOVA ranges cached by a cpu (used when cpu is unplugged) |
| 999 | */ |
| 1000 | void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad) |
| 1001 | { |
| 1002 | struct iova_cpu_rcache *cpu_rcache; |
| 1003 | struct iova_rcache *rcache; |
| 1004 | unsigned long flags; |
| 1005 | int i; |
| 1006 | |
| 1007 | for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { |
| 1008 | rcache = &iovad->rcaches[i]; |
| 1009 | cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu); |
| 1010 | spin_lock_irqsave(&cpu_rcache->lock, flags); |
| 1011 | iova_magazine_free_pfns(cpu_rcache->loaded, iovad); |
| 1012 | iova_magazine_free_pfns(cpu_rcache->prev, iovad); |
| 1013 | spin_unlock_irqrestore(&cpu_rcache->lock, flags); |
| 1014 | } |
| 1015 | } |
| 1016 | |
Sakari Ailus | 15bbdec | 2015-07-13 14:31:30 +0300 | [diff] [blame] | 1017 | MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>"); |
| 1018 | MODULE_LICENSE("GPL"); |