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