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