blob: 9e8bc802ac053daf6b2310a00c5074e9548c9082 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07002/*
David Woodhousea15a5192009-07-01 18:49:06 +01003 * Copyright © 2006-2009, Intel Corporation.
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07004 *
mark gross98bcef52008-02-23 15:23:35 -08005 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07006 */
7
Kay, Allen M38717942008-09-09 18:37:29 +03008#include <linux/iova.h>
Sakari Ailus15bbdec2015-07-13 14:31:30 +03009#include <linux/module.h>
Robin Murphy85b45452015-01-12 17:51:14 +000010#include <linux/slab.h>
Omer Peleg9257b4a2016-04-20 11:34:11 +030011#include <linux/smp.h>
12#include <linux/bitops.h>
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +020013#include <linux/cpu.h>
Omer Peleg9257b4a2016-04-20 11:34:11 +030014
Robin Murphybb68b2f2017-09-21 16:52:46 +010015/* The anchor node sits above the top of the usable address space */
16#define IOVA_ANCHOR ~0UL
17
Omer Peleg9257b4a2016-04-20 11:34:11 +030018static bool iova_rcache_insert(struct iova_domain *iovad,
19 unsigned long pfn,
20 unsigned long size);
21static unsigned long iova_rcache_get(struct iova_domain *iovad,
22 unsigned long size,
23 unsigned long limit_pfn);
24static void init_iova_rcaches(struct iova_domain *iovad);
John Garry149448b2021-03-25 20:30:00 +080025static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
Omer Peleg9257b4a2016-04-20 11:34:11 +030026static void free_iova_rcaches(struct iova_domain *iovad);
Joerg Roedel19282102017-08-10 15:49:44 +020027static void fq_destroy_all_entries(struct iova_domain *iovad);
Kees Cooke99e88a2017-10-16 14:43:17 -070028static void fq_flush_timeout(struct timer_list *t);
John Garryf598a492021-03-25 20:29:58 +080029
30static 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 Jitta6fa35252020-09-30 13:14:24 +053040static void free_global_cached_iovas(struct iova_domain *iovad);
Robin Murphy85b45452015-01-12 17:51:14 +000041
Robin Murphy7ae31ce2021-03-05 16:35:22 +000042static struct iova *to_iova(struct rb_node *node)
43{
44 return rb_entry(node, struct iova, node);
45}
46
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070047void
Robin Murphy0fb5fe82015-01-12 17:51:16 +000048init_iova_domain(struct iova_domain *iovad, unsigned long granule,
Zhen Leiaa3ac942017-09-21 16:52:45 +010049 unsigned long start_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070050{
Robin Murphy0fb5fe82015-01-12 17:51:16 +000051 /*
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 Sf8de50e2007-10-21 16:41:48 -070058 spin_lock_init(&iovad->iova_rbtree_lock);
59 iovad->rbroot = RB_ROOT;
Robin Murphy973f5fb2017-09-21 16:52:47 +010060 iovad->cached_node = &iovad->anchor.node;
61 iovad->cached32_node = &iovad->anchor.node;
Robin Murphy0fb5fe82015-01-12 17:51:16 +000062 iovad->granule = granule;
Robin Murphy1b722502015-01-12 17:51:15 +000063 iovad->start_pfn = start_pfn;
Zhen Leiaa3ac942017-09-21 16:52:45 +010064 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
Ganapatrao Kulkarnibee60e92018-09-05 09:57:36 +053065 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
Joerg Roedel42f87e72017-08-10 14:44:28 +020066 iovad->flush_cb = NULL;
67 iovad->fq = NULL;
Robin Murphybb68b2f2017-09-21 16:52:46 +010068 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 Garryf598a492021-03-25 20:29:58 +080071 cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD, &iovad->cpuhp_dead);
Omer Peleg9257b4a2016-04-20 11:34:11 +030072 init_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070073}
Sakari Ailus9b417602015-07-13 14:31:29 +030074EXPORT_SYMBOL_GPL(init_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070075
John Garry9cc0aaeb2021-01-06 21:35:06 +080076static bool has_iova_flush_queue(struct iova_domain *iovad)
Dmitry Safonoveffa4672019-07-16 22:38:05 +010077{
78 return !!iovad->fq;
79}
80
Joerg Roedel42f87e72017-08-10 14:44:28 +020081static void free_iova_flush_queue(struct iova_domain *iovad)
82{
Dmitry Safonoveffa4672019-07-16 22:38:05 +010083 if (!has_iova_flush_queue(iovad))
Joerg Roedel42f87e72017-08-10 14:44:28 +020084 return;
85
Joerg Roedel9a005a82017-08-10 16:58:18 +020086 if (timer_pending(&iovad->fq_timer))
87 del_timer(&iovad->fq_timer);
88
Joerg Roedel19282102017-08-10 15:49:44 +020089 fq_destroy_all_entries(iovad);
Joerg Roedel9a005a82017-08-10 16:58:18 +020090
Joerg Roedel42f87e72017-08-10 14:44:28 +020091 free_percpu(iovad->fq);
92
93 iovad->fq = NULL;
94 iovad->flush_cb = NULL;
95 iovad->entry_dtor = NULL;
96}
97
98int init_iova_flush_queue(struct iova_domain *iovad,
99 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
100{
Dmitry Safonoveffa4672019-07-16 22:38:05 +0100101 struct iova_fq __percpu *queue;
Joerg Roedel42f87e72017-08-10 14:44:28 +0200102 int cpu;
103
Joerg Roedelfb418da2017-08-10 16:14:59 +0200104 atomic64_set(&iovad->fq_flush_start_cnt, 0);
105 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
106
Dmitry Safonoveffa4672019-07-16 22:38:05 +0100107 queue = alloc_percpu(struct iova_fq);
108 if (!queue)
Joerg Roedel42f87e72017-08-10 14:44:28 +0200109 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 Safonoveffa4672019-07-16 22:38:05 +0100117 fq = per_cpu_ptr(queue, cpu);
Joerg Roedel42f87e72017-08-10 14:44:28 +0200118 fq->head = 0;
119 fq->tail = 0;
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200120
121 spin_lock_init(&fq->lock);
Joerg Roedel42f87e72017-08-10 14:44:28 +0200122 }
123
Dmitry Safonoveffa4672019-07-16 22:38:05 +0100124 iovad->fq = queue;
125
Kees Cooke99e88a2017-10-16 14:43:17 -0700126 timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
Joerg Roedel9a005a82017-08-10 16:58:18 +0200127 atomic_set(&iovad->fq_timer_on, 0);
128
Joerg Roedel42f87e72017-08-10 14:44:28 +0200129 return 0;
130}
Joerg Roedel42f87e72017-08-10 14:44:28 +0200131
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700132static struct rb_node *
Robin Murphy973f5fb2017-09-21 16:52:47 +0100133__get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700134{
Robin Murphy973f5fb2017-09-21 16:52:47 +0100135 if (limit_pfn <= iovad->dma_32bit_pfn)
136 return iovad->cached32_node;
Robin Murphye60aa7b2017-09-21 16:52:44 +0100137
Robin Murphy973f5fb2017-09-21 16:52:47 +0100138 return iovad->cached_node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700139}
140
141static void
Robin Murphye60aa7b2017-09-21 16:52:44 +0100142__cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700143{
Robin Murphye60aa7b2017-09-21 16:52:44 +0100144 if (new->pfn_hi < iovad->dma_32bit_pfn)
145 iovad->cached32_node = &new->node;
146 else
147 iovad->cached_node = &new->node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700148}
149
150static void
151__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
152{
153 struct iova *cached_iova;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700154
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000155 cached_iova = to_iova(iovad->cached32_node);
Chris Wilson9eed17d2019-07-20 19:08:48 +0100156 if (free == cached_iova ||
157 (free->pfn_hi < iovad->dma_32bit_pfn &&
158 free->pfn_lo >= cached_iova->pfn_lo)) {
Robin Murphye60aa7b2017-09-21 16:52:44 +0100159 iovad->cached32_node = rb_next(&free->node);
Ganapatrao Kulkarnibee60e92018-09-05 09:57:36 +0530160 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
161 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700162
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000163 cached_iova = to_iova(iovad->cached_node);
Robin Murphy973f5fb2017-09-21 16:52:47 +0100164 if (free->pfn_lo >= cached_iova->pfn_lo)
Robin Murphye60aa7b2017-09-21 16:52:44 +0100165 iovad->cached_node = rb_next(&free->node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700166}
167
Robin Murphy371d7952021-03-05 16:35:23 +0000168static 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
186search_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 Szyprowskid7517512017-02-24 12:13:37 +0100205/* Insert the iova into domain rbtree by holding writer lock */
206static void
207iova_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 Murphy7ae31ce2021-03-05 16:35:22 +0000215 struct iova *this = to_iova(*new);
Marek Szyprowskid7517512017-02-24 12:13:37 +0100216
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 grossddf02882008-03-04 15:22:04 -0800233static 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 Sf8de50e2007-10-21 16:41:48 -0700236{
Robin Murphy973f5fb2017-09-21 16:52:47 +0100237 struct rb_node *curr, *prev;
238 struct iova *curr_iova;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700239 unsigned long flags;
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530240 unsigned long new_pfn, retry_pfn;
Zhen Lei086c83a2017-09-21 16:52:43 +0100241 unsigned long align_mask = ~0UL;
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530242 unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
Zhen Lei086c83a2017-09-21 16:52:43 +0100243
244 if (size_aligned)
245 align_mask <<= fls_long(size - 1);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700246
247 /* Walk the tree backwards */
248 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Ganapatrao Kulkarnibee60e92018-09-05 09:57:36 +0530249 if (limit_pfn <= iovad->dma_32bit_pfn &&
250 size >= iovad->max32_alloc_size)
251 goto iova32_full;
252
Robin Murphy973f5fb2017-09-21 16:52:47 +0100253 curr = __get_cached_rbnode(iovad, limit_pfn);
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000254 curr_iova = to_iova(curr);
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530255 retry_pfn = curr_iova->pfn_hi + 1;
256
257retry:
Robin Murphy973f5fb2017-09-21 16:52:47 +0100258 do {
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530259 high_pfn = min(high_pfn, curr_iova->pfn_lo);
260 new_pfn = (high_pfn - size) & align_mask;
mark grossddf02882008-03-04 15:22:04 -0800261 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700262 curr = rb_prev(curr);
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000263 curr_iova = to_iova(curr);
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530264 } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700265
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530266 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 Murphy371d7952021-03-05 16:35:23 +0000270 curr = iova_find_limit(iovad, limit_pfn);
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000271 curr_iova = to_iova(curr);
Vijayanand Jitta4e89dce2020-09-30 13:14:23 +0530272 goto retry;
273 }
Robert Richter80ef4462019-03-20 18:57:23 +0000274 iovad->max32_alloc_size = size;
Ganapatrao Kulkarnibee60e92018-09-05 09:57:36 +0530275 goto iova32_full;
Robert Richter80ef4462019-03-20 18:57:23 +0000276 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700277
278 /* pfn_lo will point to size aligned address if size_aligned is set */
Zhen Lei086c83a2017-09-21 16:52:43 +0100279 new->pfn_lo = new_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700280 new->pfn_hi = new->pfn_lo + size - 1;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700281
Marek Szyprowskid7517512017-02-24 12:13:37 +0100282 /* If we have 'prev', it's a valid place to start the insertion. */
283 iova_insert_rbtree(&iovad->rbroot, new, prev);
Robin Murphye60aa7b2017-09-21 16:52:44 +0100284 __cached_rbnode_insert_update(iovad, new);
mark grossddf02882008-03-04 15:22:04 -0800285
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700286 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
287 return 0;
Ganapatrao Kulkarnibee60e92018-09-05 09:57:36 +0530288
289iova32_full:
Ganapatrao Kulkarnibee60e92018-09-05 09:57:36 +0530290 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
291 return -ENOMEM;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700292}
293
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300294static struct kmem_cache *iova_cache;
295static unsigned int iova_cache_users;
296static DEFINE_MUTEX(iova_cache_mutex);
297
John Garry51b70b82020-12-04 02:34:51 +0800298static struct iova *alloc_iova_mem(void)
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300299{
Qian Cai944c9172019-11-22 14:16:54 -0500300 return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300301}
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300302
John Garry176cfc12020-12-04 02:34:52 +0800303static void free_iova_mem(struct iova *iova)
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300304{
Robin Murphybb68b2f2017-09-21 16:52:46 +0100305 if (iova->pfn_lo != IOVA_ANCHOR)
306 kmem_cache_free(iova_cache, iova);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300307}
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300308
309int iova_cache_get(void)
310{
311 mutex_lock(&iova_cache_mutex);
312 if (!iova_cache_users) {
John Garryf598a492021-03-25 20:29:58 +0800313 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 Ailusae1ff3d2015-07-13 14:31:28 +0300323 iova_cache = kmem_cache_create(
324 "iommu_iova", sizeof(struct iova), 0,
325 SLAB_HWCACHE_ALIGN, NULL);
326 if (!iova_cache) {
John Garryf598a492021-03-25 20:29:58 +0800327 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300328 mutex_unlock(&iova_cache_mutex);
Andy Shevchenko3a0ce122020-05-07 19:18:03 +0300329 pr_err("Couldn't create iova cache\n");
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300330 return -ENOMEM;
331 }
332 }
333
334 iova_cache_users++;
335 mutex_unlock(&iova_cache_mutex);
336
337 return 0;
338}
Sakari Ailus9b417602015-07-13 14:31:29 +0300339EXPORT_SYMBOL_GPL(iova_cache_get);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300340
341void 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 Garryf598a492021-03-25 20:29:58 +0800349 if (!iova_cache_users) {
350 cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300351 kmem_cache_destroy(iova_cache);
John Garryf598a492021-03-25 20:29:58 +0800352 }
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300353 mutex_unlock(&iova_cache_mutex);
354}
Sakari Ailus9b417602015-07-13 14:31:29 +0300355EXPORT_SYMBOL_GPL(iova_cache_put);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300356
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700357/**
358 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900359 * @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 Murphy1b722502015-01-12 17:51:15 +0000363 * 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 Sf76aec72007-10-21 16:41:58 -0700365 * flag is set then the allocated address iova->pfn_lo will be naturally
366 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700367 */
368struct iova *
369alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700370 unsigned long limit_pfn,
371 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700372{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700373 struct iova *new_iova;
374 int ret;
375
376 new_iova = alloc_iova_mem();
377 if (!new_iova)
378 return NULL;
379
Robin Murphy757c3702017-05-16 12:26:48 +0100380 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
mark grossddf02882008-03-04 15:22:04 -0800381 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700382
383 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700384 free_iova_mem(new_iova);
385 return NULL;
386 }
387
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700388 return new_iova;
389}
Sakari Ailus9b417602015-07-13 14:31:29 +0300390EXPORT_SYMBOL_GPL(alloc_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700391
Omer Peleg9257b4a2016-04-20 11:34:11 +0300392static struct iova *
393private_find_iova(struct iova_domain *iovad, unsigned long pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700394{
Omer Peleg9257b4a2016-04-20 11:34:11 +0300395 struct rb_node *node = iovad->rbroot.rb_node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700396
Omer Peleg9257b4a2016-04-20 11:34:11 +0300397 assert_spin_locked(&iovad->iova_rbtree_lock);
398
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700399 while (node) {
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000400 struct iova *iova = to_iova(node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700401
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700402 if (pfn < iova->pfn_lo)
403 node = node->rb_left;
Zhen Lei2070f942017-09-21 16:52:42 +0100404 else if (pfn > iova->pfn_hi)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700405 node = node->rb_right;
Zhen Lei2070f942017-09-21 16:52:42 +0100406 else
407 return iova; /* pfn falls within iova's range */
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700408 }
409
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700410 return NULL;
411}
Omer Peleg9257b4a2016-04-20 11:34:11 +0300412
Xiang Chen79787242021-05-10 19:53:02 +0800413static void remove_iova(struct iova_domain *iovad, struct iova *iova)
Omer Peleg9257b4a2016-04-20 11:34:11 +0300414{
415 assert_spin_locked(&iovad->iova_rbtree_lock);
416 __cached_rbnode_delete_update(iovad, iova);
417 rb_erase(&iova->node, &iovad->rbroot);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300418}
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 Garzarella6775ae92020-12-22 17:42:32 +0100425 * given domain which matches the given pfn.
Omer Peleg9257b4a2016-04-20 11:34:11 +0300426 */
427struct 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 Ailus9b417602015-07-13 14:31:29 +0300438EXPORT_SYMBOL_GPL(find_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700439
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 */
446void
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 Chen79787242021-05-10 19:53:02 +0800452 remove_iova(iovad, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700453 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Xiang Chen79787242021-05-10 19:53:02 +0800454 free_iova_mem(iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700455}
Sakari Ailus9b417602015-07-13 14:31:29 +0300456EXPORT_SYMBOL_GPL(__free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700457
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 */
465void
466free_iova(struct iova_domain *iovad, unsigned long pfn)
467{
Cong Wang3a651b32020-11-17 18:25:34 +0800468 unsigned long flags;
469 struct iova *iova;
Robert Callicotte733cac22015-04-16 23:32:47 -0500470
Cong Wang3a651b32020-11-17 18:25:34 +0800471 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
472 iova = private_find_iova(iovad, pfn);
Xiang Chen79787242021-05-10 19:53:02 +0800473 if (!iova) {
474 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
475 return;
476 }
477 remove_iova(iovad, iova);
Cong Wang3a651b32020-11-17 18:25:34 +0800478 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Xiang Chen79787242021-05-10 19:53:02 +0800479 free_iova_mem(iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700480}
Sakari Ailus9b417602015-07-13 14:31:29 +0300481EXPORT_SYMBOL_GPL(free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700482
483/**
Omer Peleg9257b4a2016-04-20 11:34:11 +0300484 * 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 Nowicki538d5b32017-09-20 10:52:02 +0200488 * @flush_rcache: - set to flush rcache on regular allocation failure
Omer Peleg9257b4a2016-04-20 11:34:11 +0300489 * This function tries to satisfy an iova allocation from the rcache,
Tomasz Nowicki538d5b32017-09-20 10:52:02 +0200490 * 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 Peleg9257b4a2016-04-20 11:34:11 +0300492*/
493unsigned long
494alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
Tomasz Nowicki538d5b32017-09-20 10:52:02 +0200495 unsigned long limit_pfn, bool flush_rcache)
Omer Peleg9257b4a2016-04-20 11:34:11 +0300496{
Omer Peleg9257b4a2016-04-20 11:34:11 +0300497 unsigned long iova_pfn;
498 struct iova *new_iova;
499
Robin Murphyb826ee92017-09-19 14:48:40 +0100500 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300501 if (iova_pfn)
502 return iova_pfn;
503
504retry:
505 new_iova = alloc_iova(iovad, size, limit_pfn, true);
506 if (!new_iova) {
507 unsigned int cpu;
508
Tomasz Nowicki538d5b32017-09-20 10:52:02 +0200509 if (!flush_rcache)
Omer Peleg9257b4a2016-04-20 11:34:11 +0300510 return 0;
511
512 /* Try replenishing IOVAs by flushing rcache. */
Tomasz Nowicki538d5b32017-09-20 10:52:02 +0200513 flush_rcache = false;
Omer Peleg9257b4a2016-04-20 11:34:11 +0300514 for_each_online_cpu(cpu)
515 free_cpu_cached_iovas(cpu, iovad);
Vijayanand Jitta6fa35252020-09-30 13:14:24 +0530516 free_global_cached_iovas(iovad);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300517 goto retry;
518 }
519
520 return new_iova->pfn_lo;
521}
Xie Yongjia93a9622021-08-31 18:36:22 +0800522EXPORT_SYMBOL_GPL(alloc_iova_fast);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300523
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 */
532void
533free_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 Yongjia93a9622021-08-31 18:36:22 +0800540EXPORT_SYMBOL_GPL(free_iova_fast);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300541
Joerg Roedel19282102017-08-10 15:49:44 +0200542#define fq_ring_for_each(i, fq) \
543 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
544
545static inline bool fq_full(struct iova_fq *fq)
546{
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200547 assert_spin_locked(&fq->lock);
Joerg Roedel19282102017-08-10 15:49:44 +0200548 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
549}
550
551static inline unsigned fq_ring_add(struct iova_fq *fq)
552{
553 unsigned idx = fq->tail;
554
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200555 assert_spin_locked(&fq->lock);
556
Joerg Roedel19282102017-08-10 15:49:44 +0200557 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
558
559 return idx;
560}
561
562static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
563{
Joerg Roedelfb418da2017-08-10 16:14:59 +0200564 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200565 unsigned idx;
566
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200567 assert_spin_locked(&fq->lock);
568
Joerg Roedel19282102017-08-10 15:49:44 +0200569 fq_ring_for_each(idx, fq) {
570
Joerg Roedelfb418da2017-08-10 16:14:59 +0200571 if (fq->entries[idx].counter >= counter)
572 break;
573
Joerg Roedel19282102017-08-10 15:49:44 +0200574 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 Roedel19282102017-08-10 15:49:44 +0200580
Joerg Roedelfb418da2017-08-10 16:14:59 +0200581 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
582 }
583}
584
585static 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 Roedel19282102017-08-10 15:49:44 +0200590}
591
592static 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 Cooke99e88a2017-10-16 14:43:17 -0700613static void fq_flush_timeout(struct timer_list *t)
Joerg Roedel9a005a82017-08-10 16:58:18 +0200614{
Kees Cooke99e88a2017-10-16 14:43:17 -0700615 struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
Joerg Roedel9a005a82017-08-10 16:58:18 +0200616 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 Roedel19282102017-08-10 15:49:44 +0200632void queue_iova(struct iova_domain *iovad,
633 unsigned long pfn, unsigned long pages,
634 unsigned long data)
635{
Robin Murphy452e69b2021-08-11 13:21:38 +0100636 struct iova_fq *fq;
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200637 unsigned long flags;
Joerg Roedel19282102017-08-10 15:49:44 +0200638 unsigned idx;
639
Robin Murphy7a7c5ba2021-08-11 13:21:28 +0100640 /*
641 * Order against the IOMMU driver's pagetable update from unmapping
642 * @pte, to guarantee that iova_domain_flush() observes that if called
Robin Murphy452e69b2021-08-11 13:21:38 +0100643 * 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 Murphy7a7c5ba2021-08-11 13:21:28 +0100646 */
Robin Murphy452e69b2021-08-11 13:21:38 +0100647 smp_mb();
Robin Murphy7a7c5ba2021-08-11 13:21:28 +0100648
Robin Murphy452e69b2021-08-11 13:21:38 +0100649 fq = raw_cpu_ptr(iovad->fq);
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200650 spin_lock_irqsave(&fq->lock, flags);
651
Joerg Roedelfb418da2017-08-10 16:14:59 +0200652 /*
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 Roedel19282102017-08-10 15:49:44 +0200659 if (fq_full(fq)) {
Joerg Roedelfb418da2017-08-10 16:14:59 +0200660 iova_domain_flush(iovad);
Joerg Roedel19282102017-08-10 15:49:44 +0200661 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 Roedelfb418da2017-08-10 16:14:59 +0200669 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200670
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200671 spin_unlock_irqrestore(&fq->lock, flags);
Joerg Roedel9a005a82017-08-10 16:58:18 +0200672
Eric Dumazet0d873082019-08-28 06:13:38 -0700673 /* Avoid false sharing as much as possible. */
674 if (!atomic_read(&iovad->fq_timer_on) &&
Yuqi Jinba328f82020-08-27 16:43:54 +0800675 !atomic_xchg(&iovad->fq_timer_on, 1))
Joerg Roedel9a005a82017-08-10 16:58:18 +0200676 mod_timer(&iovad->fq_timer,
677 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
Joerg Roedel19282102017-08-10 15:49:44 +0200678}
Joerg Roedel19282102017-08-10 15:49:44 +0200679
Omer Peleg9257b4a2016-04-20 11:34:11 +0300680/**
Stefano Garzarella6775ae92020-12-22 17:42:32 +0100681 * put_iova_domain - destroys the iova domain
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700682 * @iovad: - iova domain in question.
683 * All the iova's in that domain are destroyed.
684 */
685void put_iova_domain(struct iova_domain *iovad)
686{
Robin Murphy7595dc52017-09-19 14:48:39 +0100687 struct iova *iova, *tmp;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700688
John Garryf598a492021-03-25 20:29:58 +0800689 cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
690 &iovad->cpuhp_dead);
691
Joerg Roedel42f87e72017-08-10 14:44:28 +0200692 free_iova_flush_queue(iovad);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300693 free_iova_rcaches(iovad);
Robin Murphy7595dc52017-09-19 14:48:39 +0100694 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700695 free_iova_mem(iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700696}
Sakari Ailus9b417602015-07-13 14:31:29 +0300697EXPORT_SYMBOL_GPL(put_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700698
699static int
700__is_range_overlap(struct rb_node *node,
701 unsigned long pfn_lo, unsigned long pfn_hi)
702{
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000703 struct iova *iova = to_iova(node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700704
705 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
706 return 1;
707 return 0;
708}
709
Jiang Liu75f05562014-02-19 14:07:37 +0800710static inline struct iova *
711alloc_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 Sf8de50e2007-10-21 16:41:48 -0700724static 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 Liu75f05562014-02-19 14:07:37 +0800730 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
731 if (iova)
Marek Szyprowskid7517512017-02-24 12:13:37 +0100732 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700733
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700734 return iova;
735}
736
737static 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 */
755struct iova *
756reserve_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 Murphybb68b2f2017-09-21 16:52:46 +0100764 /* Don't allow nonsensical pfns */
765 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
766 return NULL;
767
David Woodhouse3d39cec2009-07-08 15:23:30 +0100768 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700769 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
770 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
Robin Murphy7ae31ce2021-03-05 16:35:22 +0000771 iova = to_iova(node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700772 __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 Marchi25985ed2011-03-30 22:57:33 -0300782 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700783 * or need to insert remaining non overlap addr range
784 */
785 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
786finish:
787
David Woodhouse3d39cec2009-07-08 15:23:30 +0100788 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700789 return iova;
790}
Sakari Ailus9b417602015-07-13 14:31:29 +0300791EXPORT_SYMBOL_GPL(reserve_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700792
Omer Peleg9257b4a2016-04-20 11:34:11 +0300793/*
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
803struct iova_magazine {
804 unsigned long size;
805 unsigned long pfns[IOVA_MAG_SIZE];
806};
807
808struct iova_cpu_rcache {
809 spinlock_t lock;
810 struct iova_magazine *loaded;
811 struct iova_magazine *prev;
812};
813
814static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
815{
816 return kzalloc(sizeof(struct iova_magazine), flags);
817}
818
819static void iova_magazine_free(struct iova_magazine *mag)
820{
821 kfree(mag);
822}
823
824static void
825iova_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 Murphyd3e3d2b2020-06-02 14:08:18 +0100838 if (WARN_ON(!iova))
839 continue;
840
Xiang Chen79787242021-05-10 19:53:02 +0800841 remove_iova(iovad, iova);
842 free_iova_mem(iova);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300843 }
844
845 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
846
847 mag->size = 0;
848}
849
850static bool iova_magazine_full(struct iova_magazine *mag)
851{
852 return (mag && mag->size == IOVA_MAG_SIZE);
853}
854
855static bool iova_magazine_empty(struct iova_magazine *mag)
856{
857 return (!mag || mag->size == 0);
858}
859
860static unsigned long iova_magazine_pop(struct iova_magazine *mag,
861 unsigned long limit_pfn)
862{
Robin Murphye8b19842017-09-28 11:31:23 +0100863 int i;
864 unsigned long pfn;
865
Omer Peleg9257b4a2016-04-20 11:34:11 +0300866 BUG_ON(iova_magazine_empty(mag));
867
Robin Murphye8b19842017-09-28 11:31:23 +0100868 /* 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 Peleg9257b4a2016-04-20 11:34:11 +0300872
Robin Murphye8b19842017-09-28 11:31:23 +0100873 /* Swap it to pop it */
874 pfn = mag->pfns[i];
875 mag->pfns[i] = mag->pfns[--mag->size];
876
877 return pfn;
Omer Peleg9257b4a2016-04-20 11:34:11 +0300878}
879
880static 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
887static 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 */
916static 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 Siewioraaffaa82017-06-27 18:16:47 +0200925 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300926 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
964static 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 */
980static 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 Siewioraaffaa82017-06-27 18:16:47 +0200988 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300989 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 */
1019static 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 Murphyb826ee92017-09-19 14:48:40 +01001028 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
Omer Peleg9257b4a2016-04-20 11:34:11 +03001029}
1030
1031/*
Omer Peleg9257b4a2016-04-20 11:34:11 +03001032 * free rcache data structures.
1033 */
1034static void free_iova_rcaches(struct iova_domain *iovad)
1035{
1036 struct iova_rcache *rcache;
Robin Murphy7595dc52017-09-19 14:48:39 +01001037 struct iova_cpu_rcache *cpu_rcache;
Omer Peleg9257b4a2016-04-20 11:34:11 +03001038 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 Murphy7595dc52017-09-19 14:48:39 +01001043 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 Peleg9257b4a2016-04-20 11:34:11 +03001047 }
Robin Murphy7595dc52017-09-19 14:48:39 +01001048 free_percpu(rcache->cpu_rcaches);
1049 for (j = 0; j < rcache->depot_size; ++j)
1050 iova_magazine_free(rcache->depot[j]);
Omer Peleg9257b4a2016-04-20 11:34:11 +03001051 }
1052}
1053
1054/*
1055 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1056 */
John Garry149448b2021-03-25 20:30:00 +08001057static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
Omer Peleg9257b4a2016-04-20 11:34:11 +03001058{
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 Jitta6fa35252020-09-30 13:14:24 +05301074/*
1075 * free all the IOVA ranges of global cache
1076 */
1077static 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 Ailus15bbdec2015-07-13 14:31:30 +03001094MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1095MODULE_LICENSE("GPL");