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