blob: 65032e60a5d1e1c071500b7c8249d34ed1b65cb2 [file] [log] [blame]
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07001/*
David Woodhousea15a5192009-07-01 18:49:06 +01002 * Copyright © 2006-2009, Intel Corporation.
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07003 *
David Woodhousea15a5192009-07-01 18:49:06 +01004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -07007 *
David Woodhousea15a5192009-07-01 18:49:06 +01008 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070018 */
19
Kay, Allen M38717942008-09-09 18:37:29 +030020#include <linux/iova.h>
Sakari Ailus15bbdec2015-07-13 14:31:30 +030021#include <linux/module.h>
Robin Murphy85b45452015-01-12 17:51:14 +000022#include <linux/slab.h>
Omer Peleg9257b4a2016-04-20 11:34:11 +030023#include <linux/smp.h>
24#include <linux/bitops.h>
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +020025#include <linux/cpu.h>
Omer Peleg9257b4a2016-04-20 11:34:11 +030026
27static bool iova_rcache_insert(struct iova_domain *iovad,
28 unsigned long pfn,
29 unsigned long size);
30static unsigned long iova_rcache_get(struct iova_domain *iovad,
31 unsigned long size,
32 unsigned long limit_pfn);
33static void init_iova_rcaches(struct iova_domain *iovad);
34static void free_iova_rcaches(struct iova_domain *iovad);
Joerg Roedel19282102017-08-10 15:49:44 +020035static void fq_destroy_all_entries(struct iova_domain *iovad);
Joerg Roedel9a005a82017-08-10 16:58:18 +020036static void fq_flush_timeout(unsigned long data);
Robin Murphy85b45452015-01-12 17:51:14 +000037
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070038void
Robin Murphy0fb5fe82015-01-12 17:51:16 +000039init_iova_domain(struct iova_domain *iovad, unsigned long granule,
Zhen Leiaa3ac942017-09-21 16:52:45 +010040 unsigned long start_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070041{
Robin Murphy0fb5fe82015-01-12 17:51:16 +000042 /*
43 * IOVA granularity will normally be equal to the smallest
44 * supported IOMMU page size; both *must* be capable of
45 * representing individual CPU pages exactly.
46 */
47 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
48
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070049 spin_lock_init(&iovad->iova_rbtree_lock);
50 iovad->rbroot = RB_ROOT;
Robin Murphye60aa7b2017-09-21 16:52:44 +010051 iovad->cached_node = NULL;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070052 iovad->cached32_node = NULL;
Robin Murphy0fb5fe82015-01-12 17:51:16 +000053 iovad->granule = granule;
Robin Murphy1b722502015-01-12 17:51:15 +000054 iovad->start_pfn = start_pfn;
Zhen Leiaa3ac942017-09-21 16:52:45 +010055 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
Joerg Roedel42f87e72017-08-10 14:44:28 +020056 iovad->flush_cb = NULL;
57 iovad->fq = NULL;
Omer Peleg9257b4a2016-04-20 11:34:11 +030058 init_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070059}
Sakari Ailus9b417602015-07-13 14:31:29 +030060EXPORT_SYMBOL_GPL(init_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070061
Joerg Roedel42f87e72017-08-10 14:44:28 +020062static void free_iova_flush_queue(struct iova_domain *iovad)
63{
64 if (!iovad->fq)
65 return;
66
Joerg Roedel9a005a82017-08-10 16:58:18 +020067 if (timer_pending(&iovad->fq_timer))
68 del_timer(&iovad->fq_timer);
69
Joerg Roedel19282102017-08-10 15:49:44 +020070 fq_destroy_all_entries(iovad);
Joerg Roedel9a005a82017-08-10 16:58:18 +020071
Joerg Roedel42f87e72017-08-10 14:44:28 +020072 free_percpu(iovad->fq);
73
74 iovad->fq = NULL;
75 iovad->flush_cb = NULL;
76 iovad->entry_dtor = NULL;
77}
78
79int init_iova_flush_queue(struct iova_domain *iovad,
80 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
81{
82 int cpu;
83
Joerg Roedelfb418da2017-08-10 16:14:59 +020084 atomic64_set(&iovad->fq_flush_start_cnt, 0);
85 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
86
Joerg Roedel42f87e72017-08-10 14:44:28 +020087 iovad->fq = alloc_percpu(struct iova_fq);
88 if (!iovad->fq)
89 return -ENOMEM;
90
91 iovad->flush_cb = flush_cb;
92 iovad->entry_dtor = entry_dtor;
93
94 for_each_possible_cpu(cpu) {
95 struct iova_fq *fq;
96
97 fq = per_cpu_ptr(iovad->fq, cpu);
98 fq->head = 0;
99 fq->tail = 0;
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200100
101 spin_lock_init(&fq->lock);
Joerg Roedel42f87e72017-08-10 14:44:28 +0200102 }
103
Joerg Roedel9a005a82017-08-10 16:58:18 +0200104 setup_timer(&iovad->fq_timer, fq_flush_timeout, (unsigned long)iovad);
105 atomic_set(&iovad->fq_timer_on, 0);
106
Joerg Roedel42f87e72017-08-10 14:44:28 +0200107 return 0;
108}
109EXPORT_SYMBOL_GPL(init_iova_flush_queue);
110
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700111static struct rb_node *
112__get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
113{
Robin Murphye60aa7b2017-09-21 16:52:44 +0100114 struct rb_node *cached_node = NULL;
115 struct iova *curr_iova;
116
117 if (*limit_pfn <= iovad->dma_32bit_pfn)
118 cached_node = iovad->cached32_node;
119 if (!cached_node)
120 cached_node = iovad->cached_node;
121 if (!cached_node)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700122 return rb_last(&iovad->rbroot);
Robin Murphye60aa7b2017-09-21 16:52:44 +0100123
124 curr_iova = rb_entry(cached_node, struct iova, node);
125 *limit_pfn = min(*limit_pfn, curr_iova->pfn_lo);
126
127 return rb_prev(cached_node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700128}
129
130static void
Robin Murphye60aa7b2017-09-21 16:52:44 +0100131__cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700132{
Robin Murphye60aa7b2017-09-21 16:52:44 +0100133 if (new->pfn_hi < iovad->dma_32bit_pfn)
134 iovad->cached32_node = &new->node;
135 else
136 iovad->cached_node = &new->node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700137}
138
139static void
140__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
141{
142 struct iova *cached_iova;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700143
Robin Murphye60aa7b2017-09-21 16:52:44 +0100144 cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
145 if (free->pfn_hi < iovad->dma_32bit_pfn &&
146 iovad->cached32_node && free->pfn_lo >= cached_iova->pfn_lo)
147 iovad->cached32_node = rb_next(&free->node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700148
Robin Murphye60aa7b2017-09-21 16:52:44 +0100149 cached_iova = rb_entry(iovad->cached_node, struct iova, node);
150 if (iovad->cached_node && free->pfn_lo >= cached_iova->pfn_lo)
151 iovad->cached_node = rb_next(&free->node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700152}
153
Marek Szyprowskid7517512017-02-24 12:13:37 +0100154/* Insert the iova into domain rbtree by holding writer lock */
155static void
156iova_insert_rbtree(struct rb_root *root, struct iova *iova,
157 struct rb_node *start)
158{
159 struct rb_node **new, *parent = NULL;
160
161 new = (start) ? &start : &(root->rb_node);
162 /* Figure out where to put new node */
163 while (*new) {
164 struct iova *this = rb_entry(*new, struct iova, node);
165
166 parent = *new;
167
168 if (iova->pfn_lo < this->pfn_lo)
169 new = &((*new)->rb_left);
170 else if (iova->pfn_lo > this->pfn_lo)
171 new = &((*new)->rb_right);
172 else {
173 WARN_ON(1); /* this should not happen */
174 return;
175 }
176 }
177 /* Add new node and rebalance tree. */
178 rb_link_node(&iova->node, parent, new);
179 rb_insert_color(&iova->node, root);
180}
181
mark grossddf02882008-03-04 15:22:04 -0800182static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
183 unsigned long size, unsigned long limit_pfn,
184 struct iova *new, bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700185{
mark grossddf02882008-03-04 15:22:04 -0800186 struct rb_node *prev, *curr = NULL;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700187 unsigned long flags;
Robin Murphye60aa7b2017-09-21 16:52:44 +0100188 unsigned long new_pfn;
Zhen Lei086c83a2017-09-21 16:52:43 +0100189 unsigned long align_mask = ~0UL;
190
191 if (size_aligned)
192 align_mask <<= fls_long(size - 1);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700193
194 /* Walk the tree backwards */
195 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700196 curr = __get_cached_rbnode(iovad, &limit_pfn);
mark grossddf02882008-03-04 15:22:04 -0800197 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700198 while (curr) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800199 struct iova *curr_iova = rb_entry(curr, struct iova, node);
mark grossddf02882008-03-04 15:22:04 -0800200
Zhen Lei086c83a2017-09-21 16:52:43 +0100201 if (limit_pfn <= curr_iova->pfn_lo)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700202 goto move_left;
Zhen Lei086c83a2017-09-21 16:52:43 +0100203
204 if (((limit_pfn - size) & align_mask) > curr_iova->pfn_hi)
205 break; /* found a free slot */
206
Robin Murphy757c3702017-05-16 12:26:48 +0100207 limit_pfn = curr_iova->pfn_lo;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700208move_left:
mark grossddf02882008-03-04 15:22:04 -0800209 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700210 curr = rb_prev(curr);
211 }
212
Zhen Lei086c83a2017-09-21 16:52:43 +0100213 new_pfn = (limit_pfn - size) & align_mask;
214 if (limit_pfn < size || new_pfn < iovad->start_pfn) {
215 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
216 return -ENOMEM;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700217 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700218
219 /* pfn_lo will point to size aligned address if size_aligned is set */
Zhen Lei086c83a2017-09-21 16:52:43 +0100220 new->pfn_lo = new_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700221 new->pfn_hi = new->pfn_lo + size - 1;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700222
Marek Szyprowskid7517512017-02-24 12:13:37 +0100223 /* If we have 'prev', it's a valid place to start the insertion. */
224 iova_insert_rbtree(&iovad->rbroot, new, prev);
Robin Murphye60aa7b2017-09-21 16:52:44 +0100225 __cached_rbnode_insert_update(iovad, new);
mark grossddf02882008-03-04 15:22:04 -0800226
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700227 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
mark grossddf02882008-03-04 15:22:04 -0800228
229
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700230 return 0;
231}
232
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300233static struct kmem_cache *iova_cache;
234static unsigned int iova_cache_users;
235static DEFINE_MUTEX(iova_cache_mutex);
236
237struct iova *alloc_iova_mem(void)
238{
239 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
240}
241EXPORT_SYMBOL(alloc_iova_mem);
242
243void free_iova_mem(struct iova *iova)
244{
245 kmem_cache_free(iova_cache, iova);
246}
247EXPORT_SYMBOL(free_iova_mem);
248
249int iova_cache_get(void)
250{
251 mutex_lock(&iova_cache_mutex);
252 if (!iova_cache_users) {
253 iova_cache = kmem_cache_create(
254 "iommu_iova", sizeof(struct iova), 0,
255 SLAB_HWCACHE_ALIGN, NULL);
256 if (!iova_cache) {
257 mutex_unlock(&iova_cache_mutex);
258 printk(KERN_ERR "Couldn't create iova cache\n");
259 return -ENOMEM;
260 }
261 }
262
263 iova_cache_users++;
264 mutex_unlock(&iova_cache_mutex);
265
266 return 0;
267}
Sakari Ailus9b417602015-07-13 14:31:29 +0300268EXPORT_SYMBOL_GPL(iova_cache_get);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300269
270void iova_cache_put(void)
271{
272 mutex_lock(&iova_cache_mutex);
273 if (WARN_ON(!iova_cache_users)) {
274 mutex_unlock(&iova_cache_mutex);
275 return;
276 }
277 iova_cache_users--;
278 if (!iova_cache_users)
279 kmem_cache_destroy(iova_cache);
280 mutex_unlock(&iova_cache_mutex);
281}
Sakari Ailus9b417602015-07-13 14:31:29 +0300282EXPORT_SYMBOL_GPL(iova_cache_put);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300283
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700284/**
285 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900286 * @iovad: - iova domain in question
287 * @size: - size of page frames to allocate
288 * @limit_pfn: - max limit address
289 * @size_aligned: - set if size_aligned address range is required
Robin Murphy1b722502015-01-12 17:51:15 +0000290 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
291 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700292 * flag is set then the allocated address iova->pfn_lo will be naturally
293 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700294 */
295struct iova *
296alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700297 unsigned long limit_pfn,
298 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700299{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700300 struct iova *new_iova;
301 int ret;
302
303 new_iova = alloc_iova_mem();
304 if (!new_iova)
305 return NULL;
306
Robin Murphy757c3702017-05-16 12:26:48 +0100307 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
mark grossddf02882008-03-04 15:22:04 -0800308 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700309
310 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700311 free_iova_mem(new_iova);
312 return NULL;
313 }
314
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700315 return new_iova;
316}
Sakari Ailus9b417602015-07-13 14:31:29 +0300317EXPORT_SYMBOL_GPL(alloc_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700318
Omer Peleg9257b4a2016-04-20 11:34:11 +0300319static struct iova *
320private_find_iova(struct iova_domain *iovad, unsigned long pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700321{
Omer Peleg9257b4a2016-04-20 11:34:11 +0300322 struct rb_node *node = iovad->rbroot.rb_node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700323
Omer Peleg9257b4a2016-04-20 11:34:11 +0300324 assert_spin_locked(&iovad->iova_rbtree_lock);
325
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700326 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800327 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700328
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700329 if (pfn < iova->pfn_lo)
330 node = node->rb_left;
Zhen Lei2070f942017-09-21 16:52:42 +0100331 else if (pfn > iova->pfn_hi)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700332 node = node->rb_right;
Zhen Lei2070f942017-09-21 16:52:42 +0100333 else
334 return iova; /* pfn falls within iova's range */
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700335 }
336
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700337 return NULL;
338}
Omer Peleg9257b4a2016-04-20 11:34:11 +0300339
340static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
341{
342 assert_spin_locked(&iovad->iova_rbtree_lock);
343 __cached_rbnode_delete_update(iovad, iova);
344 rb_erase(&iova->node, &iovad->rbroot);
345 free_iova_mem(iova);
346}
347
348/**
349 * find_iova - finds an iova for a given pfn
350 * @iovad: - iova domain in question.
351 * @pfn: - page frame number
352 * This function finds and returns an iova belonging to the
353 * given doamin which matches the given pfn.
354 */
355struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
356{
357 unsigned long flags;
358 struct iova *iova;
359
360 /* Take the lock so that no other thread is manipulating the rbtree */
361 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
362 iova = private_find_iova(iovad, pfn);
363 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
364 return iova;
365}
Sakari Ailus9b417602015-07-13 14:31:29 +0300366EXPORT_SYMBOL_GPL(find_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700367
368/**
369 * __free_iova - frees the given iova
370 * @iovad: iova domain in question.
371 * @iova: iova in question.
372 * Frees the given iova belonging to the giving domain
373 */
374void
375__free_iova(struct iova_domain *iovad, struct iova *iova)
376{
377 unsigned long flags;
378
379 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300380 private_free_iova(iovad, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700381 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700382}
Sakari Ailus9b417602015-07-13 14:31:29 +0300383EXPORT_SYMBOL_GPL(__free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700384
385/**
386 * free_iova - finds and frees the iova for a given pfn
387 * @iovad: - iova domain in question.
388 * @pfn: - pfn that is allocated previously
389 * This functions finds an iova for a given pfn and then
390 * frees the iova from that domain.
391 */
392void
393free_iova(struct iova_domain *iovad, unsigned long pfn)
394{
395 struct iova *iova = find_iova(iovad, pfn);
Robert Callicotte733cac22015-04-16 23:32:47 -0500396
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700397 if (iova)
398 __free_iova(iovad, iova);
399
400}
Sakari Ailus9b417602015-07-13 14:31:29 +0300401EXPORT_SYMBOL_GPL(free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700402
403/**
Omer Peleg9257b4a2016-04-20 11:34:11 +0300404 * alloc_iova_fast - allocates an iova from rcache
405 * @iovad: - iova domain in question
406 * @size: - size of page frames to allocate
407 * @limit_pfn: - max limit address
408 * This function tries to satisfy an iova allocation from the rcache,
409 * and falls back to regular allocation on failure.
410*/
411unsigned long
412alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
413 unsigned long limit_pfn)
414{
415 bool flushed_rcache = false;
416 unsigned long iova_pfn;
417 struct iova *new_iova;
418
419 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
420 if (iova_pfn)
421 return iova_pfn;
422
423retry:
424 new_iova = alloc_iova(iovad, size, limit_pfn, true);
425 if (!new_iova) {
426 unsigned int cpu;
427
428 if (flushed_rcache)
429 return 0;
430
431 /* Try replenishing IOVAs by flushing rcache. */
432 flushed_rcache = true;
433 for_each_online_cpu(cpu)
434 free_cpu_cached_iovas(cpu, iovad);
435 goto retry;
436 }
437
438 return new_iova->pfn_lo;
439}
440EXPORT_SYMBOL_GPL(alloc_iova_fast);
441
442/**
443 * free_iova_fast - free iova pfn range into rcache
444 * @iovad: - iova domain in question.
445 * @pfn: - pfn that is allocated previously
446 * @size: - # of pages in range
447 * This functions frees an iova range by trying to put it into the rcache,
448 * falling back to regular iova deallocation via free_iova() if this fails.
449 */
450void
451free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
452{
453 if (iova_rcache_insert(iovad, pfn, size))
454 return;
455
456 free_iova(iovad, pfn);
457}
458EXPORT_SYMBOL_GPL(free_iova_fast);
459
Joerg Roedel19282102017-08-10 15:49:44 +0200460#define fq_ring_for_each(i, fq) \
461 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
462
463static inline bool fq_full(struct iova_fq *fq)
464{
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200465 assert_spin_locked(&fq->lock);
Joerg Roedel19282102017-08-10 15:49:44 +0200466 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
467}
468
469static inline unsigned fq_ring_add(struct iova_fq *fq)
470{
471 unsigned idx = fq->tail;
472
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200473 assert_spin_locked(&fq->lock);
474
Joerg Roedel19282102017-08-10 15:49:44 +0200475 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
476
477 return idx;
478}
479
480static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
481{
Joerg Roedelfb418da2017-08-10 16:14:59 +0200482 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200483 unsigned idx;
484
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200485 assert_spin_locked(&fq->lock);
486
Joerg Roedel19282102017-08-10 15:49:44 +0200487 fq_ring_for_each(idx, fq) {
488
Joerg Roedelfb418da2017-08-10 16:14:59 +0200489 if (fq->entries[idx].counter >= counter)
490 break;
491
Joerg Roedel19282102017-08-10 15:49:44 +0200492 if (iovad->entry_dtor)
493 iovad->entry_dtor(fq->entries[idx].data);
494
495 free_iova_fast(iovad,
496 fq->entries[idx].iova_pfn,
497 fq->entries[idx].pages);
Joerg Roedel19282102017-08-10 15:49:44 +0200498
Joerg Roedelfb418da2017-08-10 16:14:59 +0200499 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
500 }
501}
502
503static void iova_domain_flush(struct iova_domain *iovad)
504{
505 atomic64_inc(&iovad->fq_flush_start_cnt);
506 iovad->flush_cb(iovad);
507 atomic64_inc(&iovad->fq_flush_finish_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200508}
509
510static void fq_destroy_all_entries(struct iova_domain *iovad)
511{
512 int cpu;
513
514 /*
515 * This code runs when the iova_domain is being detroyed, so don't
516 * bother to free iovas, just call the entry_dtor on all remaining
517 * entries.
518 */
519 if (!iovad->entry_dtor)
520 return;
521
522 for_each_possible_cpu(cpu) {
523 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
524 int idx;
525
526 fq_ring_for_each(idx, fq)
527 iovad->entry_dtor(fq->entries[idx].data);
528 }
529}
530
Joerg Roedel9a005a82017-08-10 16:58:18 +0200531static void fq_flush_timeout(unsigned long data)
532{
533 struct iova_domain *iovad = (struct iova_domain *)data;
534 int cpu;
535
536 atomic_set(&iovad->fq_timer_on, 0);
537 iova_domain_flush(iovad);
538
539 for_each_possible_cpu(cpu) {
540 unsigned long flags;
541 struct iova_fq *fq;
542
543 fq = per_cpu_ptr(iovad->fq, cpu);
544 spin_lock_irqsave(&fq->lock, flags);
545 fq_ring_free(iovad, fq);
546 spin_unlock_irqrestore(&fq->lock, flags);
547 }
548}
549
Joerg Roedel19282102017-08-10 15:49:44 +0200550void queue_iova(struct iova_domain *iovad,
551 unsigned long pfn, unsigned long pages,
552 unsigned long data)
553{
554 struct iova_fq *fq = get_cpu_ptr(iovad->fq);
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200555 unsigned long flags;
Joerg Roedel19282102017-08-10 15:49:44 +0200556 unsigned idx;
557
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200558 spin_lock_irqsave(&fq->lock, flags);
559
Joerg Roedelfb418da2017-08-10 16:14:59 +0200560 /*
561 * First remove all entries from the flush queue that have already been
562 * flushed out on another CPU. This makes the fq_full() check below less
563 * likely to be true.
564 */
565 fq_ring_free(iovad, fq);
566
Joerg Roedel19282102017-08-10 15:49:44 +0200567 if (fq_full(fq)) {
Joerg Roedelfb418da2017-08-10 16:14:59 +0200568 iova_domain_flush(iovad);
Joerg Roedel19282102017-08-10 15:49:44 +0200569 fq_ring_free(iovad, fq);
570 }
571
572 idx = fq_ring_add(fq);
573
574 fq->entries[idx].iova_pfn = pfn;
575 fq->entries[idx].pages = pages;
576 fq->entries[idx].data = data;
Joerg Roedelfb418da2017-08-10 16:14:59 +0200577 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200578
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200579 spin_unlock_irqrestore(&fq->lock, flags);
Joerg Roedel9a005a82017-08-10 16:58:18 +0200580
581 if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
582 mod_timer(&iovad->fq_timer,
583 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
584
Joerg Roedel19282102017-08-10 15:49:44 +0200585 put_cpu_ptr(iovad->fq);
586}
587EXPORT_SYMBOL_GPL(queue_iova);
588
Omer Peleg9257b4a2016-04-20 11:34:11 +0300589/**
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700590 * put_iova_domain - destroys the iova doamin
591 * @iovad: - iova domain in question.
592 * All the iova's in that domain are destroyed.
593 */
594void put_iova_domain(struct iova_domain *iovad)
595{
596 struct rb_node *node;
597 unsigned long flags;
598
Joerg Roedel42f87e72017-08-10 14:44:28 +0200599 free_iova_flush_queue(iovad);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300600 free_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700601 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
602 node = rb_first(&iovad->rbroot);
603 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800604 struct iova *iova = rb_entry(node, struct iova, node);
Robert Callicotte733cac22015-04-16 23:32:47 -0500605
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700606 rb_erase(node, &iovad->rbroot);
607 free_iova_mem(iova);
608 node = rb_first(&iovad->rbroot);
609 }
610 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
611}
Sakari Ailus9b417602015-07-13 14:31:29 +0300612EXPORT_SYMBOL_GPL(put_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700613
614static int
615__is_range_overlap(struct rb_node *node,
616 unsigned long pfn_lo, unsigned long pfn_hi)
617{
Geliang Tangeba484b2016-12-19 22:46:58 +0800618 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700619
620 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
621 return 1;
622 return 0;
623}
624
Jiang Liu75f05562014-02-19 14:07:37 +0800625static inline struct iova *
626alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
627{
628 struct iova *iova;
629
630 iova = alloc_iova_mem();
631 if (iova) {
632 iova->pfn_lo = pfn_lo;
633 iova->pfn_hi = pfn_hi;
634 }
635
636 return iova;
637}
638
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700639static struct iova *
640__insert_new_range(struct iova_domain *iovad,
641 unsigned long pfn_lo, unsigned long pfn_hi)
642{
643 struct iova *iova;
644
Jiang Liu75f05562014-02-19 14:07:37 +0800645 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
646 if (iova)
Marek Szyprowskid7517512017-02-24 12:13:37 +0100647 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700648
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700649 return iova;
650}
651
652static void
653__adjust_overlap_range(struct iova *iova,
654 unsigned long *pfn_lo, unsigned long *pfn_hi)
655{
656 if (*pfn_lo < iova->pfn_lo)
657 iova->pfn_lo = *pfn_lo;
658 if (*pfn_hi > iova->pfn_hi)
659 *pfn_lo = iova->pfn_hi + 1;
660}
661
662/**
663 * reserve_iova - reserves an iova in the given range
664 * @iovad: - iova domain pointer
665 * @pfn_lo: - lower page frame address
666 * @pfn_hi:- higher pfn adderss
667 * This function allocates reserves the address range from pfn_lo to pfn_hi so
668 * that this address is not dished out as part of alloc_iova.
669 */
670struct iova *
671reserve_iova(struct iova_domain *iovad,
672 unsigned long pfn_lo, unsigned long pfn_hi)
673{
674 struct rb_node *node;
675 unsigned long flags;
676 struct iova *iova;
677 unsigned int overlap = 0;
678
David Woodhouse3d39cec2009-07-08 15:23:30 +0100679 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700680 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
681 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800682 iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700683 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
684 if ((pfn_lo >= iova->pfn_lo) &&
685 (pfn_hi <= iova->pfn_hi))
686 goto finish;
687 overlap = 1;
688
689 } else if (overlap)
690 break;
691 }
692
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300693 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700694 * or need to insert remaining non overlap addr range
695 */
696 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
697finish:
698
David Woodhouse3d39cec2009-07-08 15:23:30 +0100699 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700700 return iova;
701}
Sakari Ailus9b417602015-07-13 14:31:29 +0300702EXPORT_SYMBOL_GPL(reserve_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700703
704/**
705 * copy_reserved_iova - copies the reserved between domains
706 * @from: - source doamin from where to copy
707 * @to: - destination domin where to copy
708 * This function copies reserved iova's from one doamin to
709 * other.
710 */
711void
712copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
713{
714 unsigned long flags;
715 struct rb_node *node;
716
David Woodhouse3d39cec2009-07-08 15:23:30 +0100717 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700718 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800719 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700720 struct iova *new_iova;
Robert Callicotte733cac22015-04-16 23:32:47 -0500721
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700722 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
723 if (!new_iova)
724 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
725 iova->pfn_lo, iova->pfn_lo);
726 }
David Woodhouse3d39cec2009-07-08 15:23:30 +0100727 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700728}
Sakari Ailus9b417602015-07-13 14:31:29 +0300729EXPORT_SYMBOL_GPL(copy_reserved_iova);
Jiang Liu75f05562014-02-19 14:07:37 +0800730
731struct iova *
732split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
733 unsigned long pfn_lo, unsigned long pfn_hi)
734{
735 unsigned long flags;
736 struct iova *prev = NULL, *next = NULL;
737
738 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
739 if (iova->pfn_lo < pfn_lo) {
740 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
741 if (prev == NULL)
742 goto error;
743 }
744 if (iova->pfn_hi > pfn_hi) {
745 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
746 if (next == NULL)
747 goto error;
748 }
749
750 __cached_rbnode_delete_update(iovad, iova);
751 rb_erase(&iova->node, &iovad->rbroot);
752
753 if (prev) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100754 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800755 iova->pfn_lo = pfn_lo;
756 }
757 if (next) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100758 iova_insert_rbtree(&iovad->rbroot, next, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800759 iova->pfn_hi = pfn_hi;
760 }
761 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
762
763 return iova;
764
765error:
766 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
767 if (prev)
768 free_iova_mem(prev);
769 return NULL;
770}
Sakari Ailus15bbdec2015-07-13 14:31:30 +0300771
Omer Peleg9257b4a2016-04-20 11:34:11 +0300772/*
773 * Magazine caches for IOVA ranges. For an introduction to magazines,
774 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
775 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
776 * For simplicity, we use a static magazine size and don't implement the
777 * dynamic size tuning described in the paper.
778 */
779
780#define IOVA_MAG_SIZE 128
781
782struct iova_magazine {
783 unsigned long size;
784 unsigned long pfns[IOVA_MAG_SIZE];
785};
786
787struct iova_cpu_rcache {
788 spinlock_t lock;
789 struct iova_magazine *loaded;
790 struct iova_magazine *prev;
791};
792
793static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
794{
795 return kzalloc(sizeof(struct iova_magazine), flags);
796}
797
798static void iova_magazine_free(struct iova_magazine *mag)
799{
800 kfree(mag);
801}
802
803static void
804iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
805{
806 unsigned long flags;
807 int i;
808
809 if (!mag)
810 return;
811
812 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
813
814 for (i = 0 ; i < mag->size; ++i) {
815 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
816
817 BUG_ON(!iova);
818 private_free_iova(iovad, iova);
819 }
820
821 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
822
823 mag->size = 0;
824}
825
826static bool iova_magazine_full(struct iova_magazine *mag)
827{
828 return (mag && mag->size == IOVA_MAG_SIZE);
829}
830
831static bool iova_magazine_empty(struct iova_magazine *mag)
832{
833 return (!mag || mag->size == 0);
834}
835
836static unsigned long iova_magazine_pop(struct iova_magazine *mag,
837 unsigned long limit_pfn)
838{
839 BUG_ON(iova_magazine_empty(mag));
840
841 if (mag->pfns[mag->size - 1] >= limit_pfn)
842 return 0;
843
844 return mag->pfns[--mag->size];
845}
846
847static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
848{
849 BUG_ON(iova_magazine_full(mag));
850
851 mag->pfns[mag->size++] = pfn;
852}
853
854static void init_iova_rcaches(struct iova_domain *iovad)
855{
856 struct iova_cpu_rcache *cpu_rcache;
857 struct iova_rcache *rcache;
858 unsigned int cpu;
859 int i;
860
861 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
862 rcache = &iovad->rcaches[i];
863 spin_lock_init(&rcache->lock);
864 rcache->depot_size = 0;
865 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
866 if (WARN_ON(!rcache->cpu_rcaches))
867 continue;
868 for_each_possible_cpu(cpu) {
869 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
870 spin_lock_init(&cpu_rcache->lock);
871 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
872 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
873 }
874 }
875}
876
877/*
878 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
879 * return true on success. Can fail if rcache is full and we can't free
880 * space, and free_iova() (our only caller) will then return the IOVA
881 * range to the rbtree instead.
882 */
883static bool __iova_rcache_insert(struct iova_domain *iovad,
884 struct iova_rcache *rcache,
885 unsigned long iova_pfn)
886{
887 struct iova_magazine *mag_to_free = NULL;
888 struct iova_cpu_rcache *cpu_rcache;
889 bool can_insert = false;
890 unsigned long flags;
891
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +0200892 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300893 spin_lock_irqsave(&cpu_rcache->lock, flags);
894
895 if (!iova_magazine_full(cpu_rcache->loaded)) {
896 can_insert = true;
897 } else if (!iova_magazine_full(cpu_rcache->prev)) {
898 swap(cpu_rcache->prev, cpu_rcache->loaded);
899 can_insert = true;
900 } else {
901 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
902
903 if (new_mag) {
904 spin_lock(&rcache->lock);
905 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
906 rcache->depot[rcache->depot_size++] =
907 cpu_rcache->loaded;
908 } else {
909 mag_to_free = cpu_rcache->loaded;
910 }
911 spin_unlock(&rcache->lock);
912
913 cpu_rcache->loaded = new_mag;
914 can_insert = true;
915 }
916 }
917
918 if (can_insert)
919 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
920
921 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
922
923 if (mag_to_free) {
924 iova_magazine_free_pfns(mag_to_free, iovad);
925 iova_magazine_free(mag_to_free);
926 }
927
928 return can_insert;
929}
930
931static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
932 unsigned long size)
933{
934 unsigned int log_size = order_base_2(size);
935
936 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
937 return false;
938
939 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
940}
941
942/*
943 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
944 * satisfy the request, return a matching non-NULL range and remove
945 * it from the 'rcache'.
946 */
947static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
948 unsigned long limit_pfn)
949{
950 struct iova_cpu_rcache *cpu_rcache;
951 unsigned long iova_pfn = 0;
952 bool has_pfn = false;
953 unsigned long flags;
954
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +0200955 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300956 spin_lock_irqsave(&cpu_rcache->lock, flags);
957
958 if (!iova_magazine_empty(cpu_rcache->loaded)) {
959 has_pfn = true;
960 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
961 swap(cpu_rcache->prev, cpu_rcache->loaded);
962 has_pfn = true;
963 } else {
964 spin_lock(&rcache->lock);
965 if (rcache->depot_size > 0) {
966 iova_magazine_free(cpu_rcache->loaded);
967 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
968 has_pfn = true;
969 }
970 spin_unlock(&rcache->lock);
971 }
972
973 if (has_pfn)
974 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
975
976 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
977
978 return iova_pfn;
979}
980
981/*
982 * Try to satisfy IOVA allocation range from rcache. Fail if requested
983 * size is too big or the DMA limit we are given isn't satisfied by the
984 * top element in the magazine.
985 */
986static unsigned long iova_rcache_get(struct iova_domain *iovad,
987 unsigned long size,
988 unsigned long limit_pfn)
989{
990 unsigned int log_size = order_base_2(size);
991
992 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
993 return 0;
994
995 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
996}
997
998/*
999 * Free a cpu's rcache.
1000 */
1001static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
1002 struct iova_rcache *rcache)
1003{
1004 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1005 unsigned long flags;
1006
1007 spin_lock_irqsave(&cpu_rcache->lock, flags);
1008
1009 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1010 iova_magazine_free(cpu_rcache->loaded);
1011
1012 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1013 iova_magazine_free(cpu_rcache->prev);
1014
1015 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1016}
1017
1018/*
1019 * free rcache data structures.
1020 */
1021static void free_iova_rcaches(struct iova_domain *iovad)
1022{
1023 struct iova_rcache *rcache;
1024 unsigned long flags;
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];
1030 for_each_possible_cpu(cpu)
1031 free_cpu_iova_rcache(cpu, iovad, rcache);
1032 spin_lock_irqsave(&rcache->lock, flags);
1033 free_percpu(rcache->cpu_rcaches);
1034 for (j = 0; j < rcache->depot_size; ++j) {
1035 iova_magazine_free_pfns(rcache->depot[j], iovad);
1036 iova_magazine_free(rcache->depot[j]);
1037 }
1038 spin_unlock_irqrestore(&rcache->lock, flags);
1039 }
1040}
1041
1042/*
1043 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1044 */
1045void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1046{
1047 struct iova_cpu_rcache *cpu_rcache;
1048 struct iova_rcache *rcache;
1049 unsigned long flags;
1050 int i;
1051
1052 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1053 rcache = &iovad->rcaches[i];
1054 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1055 spin_lock_irqsave(&cpu_rcache->lock, flags);
1056 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1057 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1058 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1059 }
1060}
1061
Sakari Ailus15bbdec2015-07-13 14:31:30 +03001062MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1063MODULE_LICENSE("GPL");