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