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