blob: e5c9a7ae6088a0cb9ceaa25fd8c86be68674aa45 [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);
Robin Murphy85b45452015-01-12 17:51:14 +000036
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070037void
Robin Murphy0fb5fe82015-01-12 17:51:16 +000038init_iova_domain(struct iova_domain *iovad, unsigned long granule,
39 unsigned long start_pfn, unsigned long pfn_32bit)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070040{
Robin Murphy0fb5fe82015-01-12 17:51:16 +000041 /*
42 * IOVA granularity will normally be equal to the smallest
43 * supported IOMMU page size; both *must* be capable of
44 * representing individual CPU pages exactly.
45 */
46 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
47
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070048 spin_lock_init(&iovad->iova_rbtree_lock);
49 iovad->rbroot = RB_ROOT;
50 iovad->cached32_node = NULL;
Robin Murphy0fb5fe82015-01-12 17:51:16 +000051 iovad->granule = granule;
Robin Murphy1b722502015-01-12 17:51:15 +000052 iovad->start_pfn = start_pfn;
Robin Murphy757c3702017-05-16 12:26:48 +010053 iovad->dma_32bit_pfn = pfn_32bit + 1;
Joerg Roedel42f87e72017-08-10 14:44:28 +020054 iovad->flush_cb = NULL;
55 iovad->fq = NULL;
Omer Peleg9257b4a2016-04-20 11:34:11 +030056 init_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070057}
Sakari Ailus9b417602015-07-13 14:31:29 +030058EXPORT_SYMBOL_GPL(init_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070059
Joerg Roedel42f87e72017-08-10 14:44:28 +020060static void free_iova_flush_queue(struct iova_domain *iovad)
61{
62 if (!iovad->fq)
63 return;
64
Joerg Roedel19282102017-08-10 15:49:44 +020065 fq_destroy_all_entries(iovad);
Joerg Roedel42f87e72017-08-10 14:44:28 +020066 free_percpu(iovad->fq);
67
68 iovad->fq = NULL;
69 iovad->flush_cb = NULL;
70 iovad->entry_dtor = NULL;
71}
72
73int init_iova_flush_queue(struct iova_domain *iovad,
74 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
75{
76 int cpu;
77
78 iovad->fq = alloc_percpu(struct iova_fq);
79 if (!iovad->fq)
80 return -ENOMEM;
81
82 iovad->flush_cb = flush_cb;
83 iovad->entry_dtor = entry_dtor;
84
85 for_each_possible_cpu(cpu) {
86 struct iova_fq *fq;
87
88 fq = per_cpu_ptr(iovad->fq, cpu);
89 fq->head = 0;
90 fq->tail = 0;
91 }
92
93 return 0;
94}
95EXPORT_SYMBOL_GPL(init_iova_flush_queue);
96
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070097static struct rb_node *
98__get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
99{
Robin Murphy62280cf2016-11-11 18:35:46 +0000100 if ((*limit_pfn > iovad->dma_32bit_pfn) ||
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700101 (iovad->cached32_node == NULL))
102 return rb_last(&iovad->rbroot);
103 else {
104 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
105 struct iova *curr_iova =
Geliang Tangeba484b2016-12-19 22:46:58 +0800106 rb_entry(iovad->cached32_node, struct iova, node);
Robin Murphy757c3702017-05-16 12:26:48 +0100107 *limit_pfn = curr_iova->pfn_lo;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700108 return prev_node;
109 }
110}
111
112static void
113__cached_rbnode_insert_update(struct iova_domain *iovad,
114 unsigned long limit_pfn, struct iova *new)
115{
David Millerf6611972008-02-06 01:36:23 -0800116 if (limit_pfn != iovad->dma_32bit_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700117 return;
118 iovad->cached32_node = &new->node;
119}
120
121static void
122__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
123{
124 struct iova *cached_iova;
125 struct rb_node *curr;
126
127 if (!iovad->cached32_node)
128 return;
129 curr = iovad->cached32_node;
Geliang Tangeba484b2016-12-19 22:46:58 +0800130 cached_iova = rb_entry(curr, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700131
Chris Wright1c9fc3d2011-05-28 13:15:04 -0500132 if (free->pfn_lo >= cached_iova->pfn_lo) {
133 struct rb_node *node = rb_next(&free->node);
Geliang Tangeba484b2016-12-19 22:46:58 +0800134 struct iova *iova = rb_entry(node, struct iova, node);
Chris Wright1c9fc3d2011-05-28 13:15:04 -0500135
136 /* only cache if it's below 32bit pfn */
137 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
138 iovad->cached32_node = node;
139 else
140 iovad->cached32_node = NULL;
141 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700142}
143
Marek Szyprowskid7517512017-02-24 12:13:37 +0100144/* Insert the iova into domain rbtree by holding writer lock */
145static void
146iova_insert_rbtree(struct rb_root *root, struct iova *iova,
147 struct rb_node *start)
148{
149 struct rb_node **new, *parent = NULL;
150
151 new = (start) ? &start : &(root->rb_node);
152 /* Figure out where to put new node */
153 while (*new) {
154 struct iova *this = rb_entry(*new, struct iova, node);
155
156 parent = *new;
157
158 if (iova->pfn_lo < this->pfn_lo)
159 new = &((*new)->rb_left);
160 else if (iova->pfn_lo > this->pfn_lo)
161 new = &((*new)->rb_right);
162 else {
163 WARN_ON(1); /* this should not happen */
164 return;
165 }
166 }
167 /* Add new node and rebalance tree. */
168 rb_link_node(&iova->node, parent, new);
169 rb_insert_color(&iova->node, root);
170}
171
Robin Murphy8f6429c2015-07-16 19:40:12 +0100172/*
173 * Computes the padding size required, to make the start address
174 * naturally aligned on the power-of-two order of its size
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700175 */
Robin Murphy8f6429c2015-07-16 19:40:12 +0100176static unsigned int
177iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700178{
Robin Murphy757c3702017-05-16 12:26:48 +0100179 return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700180}
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;
188 unsigned long saved_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700189 unsigned int pad_size = 0;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700190
191 /* Walk the tree backwards */
192 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
193 saved_pfn = limit_pfn;
194 curr = __get_cached_rbnode(iovad, &limit_pfn);
mark grossddf02882008-03-04 15:22:04 -0800195 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700196 while (curr) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800197 struct iova *curr_iova = rb_entry(curr, struct iova, node);
mark grossddf02882008-03-04 15:22:04 -0800198
Robin Murphy757c3702017-05-16 12:26:48 +0100199 if (limit_pfn <= curr_iova->pfn_lo) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700200 goto move_left;
Robin Murphy757c3702017-05-16 12:26:48 +0100201 } else if (limit_pfn > curr_iova->pfn_hi) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700202 if (size_aligned)
203 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy757c3702017-05-16 12:26:48 +0100204 if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700205 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
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700213 if (!curr) {
214 if (size_aligned)
215 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy1b722502015-01-12 17:51:15 +0000216 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700217 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
218 return -ENOMEM;
219 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700220 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700221
222 /* pfn_lo will point to size aligned address if size_aligned is set */
Robin Murphy757c3702017-05-16 12:26:48 +0100223 new->pfn_lo = limit_pfn - (size + pad_size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700224 new->pfn_hi = new->pfn_lo + size - 1;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700225
Marek Szyprowskid7517512017-02-24 12:13:37 +0100226 /* If we have 'prev', it's a valid place to start the insertion. */
227 iova_insert_rbtree(&iovad->rbroot, new, prev);
mark grossddf02882008-03-04 15:22:04 -0800228 __cached_rbnode_insert_update(iovad, saved_pfn, new);
229
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700230 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
mark grossddf02882008-03-04 15:22:04 -0800231
232
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700233 return 0;
234}
235
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300236static struct kmem_cache *iova_cache;
237static unsigned int iova_cache_users;
238static DEFINE_MUTEX(iova_cache_mutex);
239
240struct iova *alloc_iova_mem(void)
241{
242 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
243}
244EXPORT_SYMBOL(alloc_iova_mem);
245
246void free_iova_mem(struct iova *iova)
247{
248 kmem_cache_free(iova_cache, iova);
249}
250EXPORT_SYMBOL(free_iova_mem);
251
252int iova_cache_get(void)
253{
254 mutex_lock(&iova_cache_mutex);
255 if (!iova_cache_users) {
256 iova_cache = kmem_cache_create(
257 "iommu_iova", sizeof(struct iova), 0,
258 SLAB_HWCACHE_ALIGN, NULL);
259 if (!iova_cache) {
260 mutex_unlock(&iova_cache_mutex);
261 printk(KERN_ERR "Couldn't create iova cache\n");
262 return -ENOMEM;
263 }
264 }
265
266 iova_cache_users++;
267 mutex_unlock(&iova_cache_mutex);
268
269 return 0;
270}
Sakari Ailus9b417602015-07-13 14:31:29 +0300271EXPORT_SYMBOL_GPL(iova_cache_get);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300272
273void iova_cache_put(void)
274{
275 mutex_lock(&iova_cache_mutex);
276 if (WARN_ON(!iova_cache_users)) {
277 mutex_unlock(&iova_cache_mutex);
278 return;
279 }
280 iova_cache_users--;
281 if (!iova_cache_users)
282 kmem_cache_destroy(iova_cache);
283 mutex_unlock(&iova_cache_mutex);
284}
Sakari Ailus9b417602015-07-13 14:31:29 +0300285EXPORT_SYMBOL_GPL(iova_cache_put);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300286
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700287/**
288 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900289 * @iovad: - iova domain in question
290 * @size: - size of page frames to allocate
291 * @limit_pfn: - max limit address
292 * @size_aligned: - set if size_aligned address range is required
Robin Murphy1b722502015-01-12 17:51:15 +0000293 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
294 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700295 * flag is set then the allocated address iova->pfn_lo will be naturally
296 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700297 */
298struct iova *
299alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700300 unsigned long limit_pfn,
301 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700302{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700303 struct iova *new_iova;
304 int ret;
305
306 new_iova = alloc_iova_mem();
307 if (!new_iova)
308 return NULL;
309
Robin Murphy757c3702017-05-16 12:26:48 +0100310 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
mark grossddf02882008-03-04 15:22:04 -0800311 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700312
313 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700314 free_iova_mem(new_iova);
315 return NULL;
316 }
317
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700318 return new_iova;
319}
Sakari Ailus9b417602015-07-13 14:31:29 +0300320EXPORT_SYMBOL_GPL(alloc_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700321
Omer Peleg9257b4a2016-04-20 11:34:11 +0300322static struct iova *
323private_find_iova(struct iova_domain *iovad, unsigned long pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700324{
Omer Peleg9257b4a2016-04-20 11:34:11 +0300325 struct rb_node *node = iovad->rbroot.rb_node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700326
Omer Peleg9257b4a2016-04-20 11:34:11 +0300327 assert_spin_locked(&iovad->iova_rbtree_lock);
328
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700329 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800330 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700331
332 /* If pfn falls within iova's range, return iova */
333 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700334 return iova;
335 }
336
337 if (pfn < iova->pfn_lo)
338 node = node->rb_left;
339 else if (pfn > iova->pfn_lo)
340 node = node->rb_right;
341 }
342
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700343 return NULL;
344}
Omer Peleg9257b4a2016-04-20 11:34:11 +0300345
346static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
347{
348 assert_spin_locked(&iovad->iova_rbtree_lock);
349 __cached_rbnode_delete_update(iovad, iova);
350 rb_erase(&iova->node, &iovad->rbroot);
351 free_iova_mem(iova);
352}
353
354/**
355 * find_iova - finds an iova for a given pfn
356 * @iovad: - iova domain in question.
357 * @pfn: - page frame number
358 * This function finds and returns an iova belonging to the
359 * given doamin which matches the given pfn.
360 */
361struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
362{
363 unsigned long flags;
364 struct iova *iova;
365
366 /* Take the lock so that no other thread is manipulating the rbtree */
367 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
368 iova = private_find_iova(iovad, pfn);
369 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
370 return iova;
371}
Sakari Ailus9b417602015-07-13 14:31:29 +0300372EXPORT_SYMBOL_GPL(find_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700373
374/**
375 * __free_iova - frees the given iova
376 * @iovad: iova domain in question.
377 * @iova: iova in question.
378 * Frees the given iova belonging to the giving domain
379 */
380void
381__free_iova(struct iova_domain *iovad, struct iova *iova)
382{
383 unsigned long flags;
384
385 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300386 private_free_iova(iovad, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700387 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700388}
Sakari Ailus9b417602015-07-13 14:31:29 +0300389EXPORT_SYMBOL_GPL(__free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700390
391/**
392 * free_iova - finds and frees the iova for a given pfn
393 * @iovad: - iova domain in question.
394 * @pfn: - pfn that is allocated previously
395 * This functions finds an iova for a given pfn and then
396 * frees the iova from that domain.
397 */
398void
399free_iova(struct iova_domain *iovad, unsigned long pfn)
400{
401 struct iova *iova = find_iova(iovad, pfn);
Robert Callicotte733cac22015-04-16 23:32:47 -0500402
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700403 if (iova)
404 __free_iova(iovad, iova);
405
406}
Sakari Ailus9b417602015-07-13 14:31:29 +0300407EXPORT_SYMBOL_GPL(free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700408
409/**
Omer Peleg9257b4a2016-04-20 11:34:11 +0300410 * alloc_iova_fast - allocates an iova from rcache
411 * @iovad: - iova domain in question
412 * @size: - size of page frames to allocate
413 * @limit_pfn: - max limit address
414 * This function tries to satisfy an iova allocation from the rcache,
415 * and falls back to regular allocation on failure.
416*/
417unsigned long
418alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
419 unsigned long limit_pfn)
420{
421 bool flushed_rcache = false;
422 unsigned long iova_pfn;
423 struct iova *new_iova;
424
425 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
426 if (iova_pfn)
427 return iova_pfn;
428
429retry:
430 new_iova = alloc_iova(iovad, size, limit_pfn, true);
431 if (!new_iova) {
432 unsigned int cpu;
433
434 if (flushed_rcache)
435 return 0;
436
437 /* Try replenishing IOVAs by flushing rcache. */
438 flushed_rcache = true;
439 for_each_online_cpu(cpu)
440 free_cpu_cached_iovas(cpu, iovad);
441 goto retry;
442 }
443
444 return new_iova->pfn_lo;
445}
446EXPORT_SYMBOL_GPL(alloc_iova_fast);
447
448/**
449 * free_iova_fast - free iova pfn range into rcache
450 * @iovad: - iova domain in question.
451 * @pfn: - pfn that is allocated previously
452 * @size: - # of pages in range
453 * This functions frees an iova range by trying to put it into the rcache,
454 * falling back to regular iova deallocation via free_iova() if this fails.
455 */
456void
457free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
458{
459 if (iova_rcache_insert(iovad, pfn, size))
460 return;
461
462 free_iova(iovad, pfn);
463}
464EXPORT_SYMBOL_GPL(free_iova_fast);
465
Joerg Roedel19282102017-08-10 15:49:44 +0200466#define fq_ring_for_each(i, fq) \
467 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
468
469static inline bool fq_full(struct iova_fq *fq)
470{
471 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
472}
473
474static inline unsigned fq_ring_add(struct iova_fq *fq)
475{
476 unsigned idx = fq->tail;
477
478 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
479
480 return idx;
481}
482
483static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
484{
485 unsigned idx;
486
487 fq_ring_for_each(idx, fq) {
488
489 if (iovad->entry_dtor)
490 iovad->entry_dtor(fq->entries[idx].data);
491
492 free_iova_fast(iovad,
493 fq->entries[idx].iova_pfn,
494 fq->entries[idx].pages);
495 }
496
497 fq->head = 0;
498 fq->tail = 0;
499}
500
501static void fq_destroy_all_entries(struct iova_domain *iovad)
502{
503 int cpu;
504
505 /*
506 * This code runs when the iova_domain is being detroyed, so don't
507 * bother to free iovas, just call the entry_dtor on all remaining
508 * entries.
509 */
510 if (!iovad->entry_dtor)
511 return;
512
513 for_each_possible_cpu(cpu) {
514 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
515 int idx;
516
517 fq_ring_for_each(idx, fq)
518 iovad->entry_dtor(fq->entries[idx].data);
519 }
520}
521
522void queue_iova(struct iova_domain *iovad,
523 unsigned long pfn, unsigned long pages,
524 unsigned long data)
525{
526 struct iova_fq *fq = get_cpu_ptr(iovad->fq);
527 unsigned idx;
528
529 if (fq_full(fq)) {
530 iovad->flush_cb(iovad);
531 fq_ring_free(iovad, fq);
532 }
533
534 idx = fq_ring_add(fq);
535
536 fq->entries[idx].iova_pfn = pfn;
537 fq->entries[idx].pages = pages;
538 fq->entries[idx].data = data;
539
540 put_cpu_ptr(iovad->fq);
541}
542EXPORT_SYMBOL_GPL(queue_iova);
543
Omer Peleg9257b4a2016-04-20 11:34:11 +0300544/**
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700545 * put_iova_domain - destroys the iova doamin
546 * @iovad: - iova domain in question.
547 * All the iova's in that domain are destroyed.
548 */
549void put_iova_domain(struct iova_domain *iovad)
550{
551 struct rb_node *node;
552 unsigned long flags;
553
Joerg Roedel42f87e72017-08-10 14:44:28 +0200554 free_iova_flush_queue(iovad);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300555 free_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700556 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
557 node = rb_first(&iovad->rbroot);
558 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800559 struct iova *iova = rb_entry(node, struct iova, node);
Robert Callicotte733cac22015-04-16 23:32:47 -0500560
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700561 rb_erase(node, &iovad->rbroot);
562 free_iova_mem(iova);
563 node = rb_first(&iovad->rbroot);
564 }
565 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
566}
Sakari Ailus9b417602015-07-13 14:31:29 +0300567EXPORT_SYMBOL_GPL(put_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700568
569static int
570__is_range_overlap(struct rb_node *node,
571 unsigned long pfn_lo, unsigned long pfn_hi)
572{
Geliang Tangeba484b2016-12-19 22:46:58 +0800573 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700574
575 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
576 return 1;
577 return 0;
578}
579
Jiang Liu75f05562014-02-19 14:07:37 +0800580static inline struct iova *
581alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
582{
583 struct iova *iova;
584
585 iova = alloc_iova_mem();
586 if (iova) {
587 iova->pfn_lo = pfn_lo;
588 iova->pfn_hi = pfn_hi;
589 }
590
591 return iova;
592}
593
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700594static struct iova *
595__insert_new_range(struct iova_domain *iovad,
596 unsigned long pfn_lo, unsigned long pfn_hi)
597{
598 struct iova *iova;
599
Jiang Liu75f05562014-02-19 14:07:37 +0800600 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
601 if (iova)
Marek Szyprowskid7517512017-02-24 12:13:37 +0100602 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700603
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700604 return iova;
605}
606
607static void
608__adjust_overlap_range(struct iova *iova,
609 unsigned long *pfn_lo, unsigned long *pfn_hi)
610{
611 if (*pfn_lo < iova->pfn_lo)
612 iova->pfn_lo = *pfn_lo;
613 if (*pfn_hi > iova->pfn_hi)
614 *pfn_lo = iova->pfn_hi + 1;
615}
616
617/**
618 * reserve_iova - reserves an iova in the given range
619 * @iovad: - iova domain pointer
620 * @pfn_lo: - lower page frame address
621 * @pfn_hi:- higher pfn adderss
622 * This function allocates reserves the address range from pfn_lo to pfn_hi so
623 * that this address is not dished out as part of alloc_iova.
624 */
625struct iova *
626reserve_iova(struct iova_domain *iovad,
627 unsigned long pfn_lo, unsigned long pfn_hi)
628{
629 struct rb_node *node;
630 unsigned long flags;
631 struct iova *iova;
632 unsigned int overlap = 0;
633
David Woodhouse3d39cec2009-07-08 15:23:30 +0100634 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700635 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
636 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800637 iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700638 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
639 if ((pfn_lo >= iova->pfn_lo) &&
640 (pfn_hi <= iova->pfn_hi))
641 goto finish;
642 overlap = 1;
643
644 } else if (overlap)
645 break;
646 }
647
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300648 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700649 * or need to insert remaining non overlap addr range
650 */
651 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
652finish:
653
David Woodhouse3d39cec2009-07-08 15:23:30 +0100654 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700655 return iova;
656}
Sakari Ailus9b417602015-07-13 14:31:29 +0300657EXPORT_SYMBOL_GPL(reserve_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700658
659/**
660 * copy_reserved_iova - copies the reserved between domains
661 * @from: - source doamin from where to copy
662 * @to: - destination domin where to copy
663 * This function copies reserved iova's from one doamin to
664 * other.
665 */
666void
667copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
668{
669 unsigned long flags;
670 struct rb_node *node;
671
David Woodhouse3d39cec2009-07-08 15:23:30 +0100672 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700673 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800674 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700675 struct iova *new_iova;
Robert Callicotte733cac22015-04-16 23:32:47 -0500676
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700677 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
678 if (!new_iova)
679 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
680 iova->pfn_lo, iova->pfn_lo);
681 }
David Woodhouse3d39cec2009-07-08 15:23:30 +0100682 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700683}
Sakari Ailus9b417602015-07-13 14:31:29 +0300684EXPORT_SYMBOL_GPL(copy_reserved_iova);
Jiang Liu75f05562014-02-19 14:07:37 +0800685
686struct iova *
687split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
688 unsigned long pfn_lo, unsigned long pfn_hi)
689{
690 unsigned long flags;
691 struct iova *prev = NULL, *next = NULL;
692
693 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
694 if (iova->pfn_lo < pfn_lo) {
695 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
696 if (prev == NULL)
697 goto error;
698 }
699 if (iova->pfn_hi > pfn_hi) {
700 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
701 if (next == NULL)
702 goto error;
703 }
704
705 __cached_rbnode_delete_update(iovad, iova);
706 rb_erase(&iova->node, &iovad->rbroot);
707
708 if (prev) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100709 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800710 iova->pfn_lo = pfn_lo;
711 }
712 if (next) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100713 iova_insert_rbtree(&iovad->rbroot, next, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800714 iova->pfn_hi = pfn_hi;
715 }
716 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
717
718 return iova;
719
720error:
721 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
722 if (prev)
723 free_iova_mem(prev);
724 return NULL;
725}
Sakari Ailus15bbdec2015-07-13 14:31:30 +0300726
Omer Peleg9257b4a2016-04-20 11:34:11 +0300727/*
728 * Magazine caches for IOVA ranges. For an introduction to magazines,
729 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
730 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
731 * For simplicity, we use a static magazine size and don't implement the
732 * dynamic size tuning described in the paper.
733 */
734
735#define IOVA_MAG_SIZE 128
736
737struct iova_magazine {
738 unsigned long size;
739 unsigned long pfns[IOVA_MAG_SIZE];
740};
741
742struct iova_cpu_rcache {
743 spinlock_t lock;
744 struct iova_magazine *loaded;
745 struct iova_magazine *prev;
746};
747
748static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
749{
750 return kzalloc(sizeof(struct iova_magazine), flags);
751}
752
753static void iova_magazine_free(struct iova_magazine *mag)
754{
755 kfree(mag);
756}
757
758static void
759iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
760{
761 unsigned long flags;
762 int i;
763
764 if (!mag)
765 return;
766
767 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
768
769 for (i = 0 ; i < mag->size; ++i) {
770 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
771
772 BUG_ON(!iova);
773 private_free_iova(iovad, iova);
774 }
775
776 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
777
778 mag->size = 0;
779}
780
781static bool iova_magazine_full(struct iova_magazine *mag)
782{
783 return (mag && mag->size == IOVA_MAG_SIZE);
784}
785
786static bool iova_magazine_empty(struct iova_magazine *mag)
787{
788 return (!mag || mag->size == 0);
789}
790
791static unsigned long iova_magazine_pop(struct iova_magazine *mag,
792 unsigned long limit_pfn)
793{
794 BUG_ON(iova_magazine_empty(mag));
795
796 if (mag->pfns[mag->size - 1] >= limit_pfn)
797 return 0;
798
799 return mag->pfns[--mag->size];
800}
801
802static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
803{
804 BUG_ON(iova_magazine_full(mag));
805
806 mag->pfns[mag->size++] = pfn;
807}
808
809static void init_iova_rcaches(struct iova_domain *iovad)
810{
811 struct iova_cpu_rcache *cpu_rcache;
812 struct iova_rcache *rcache;
813 unsigned int cpu;
814 int i;
815
816 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
817 rcache = &iovad->rcaches[i];
818 spin_lock_init(&rcache->lock);
819 rcache->depot_size = 0;
820 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
821 if (WARN_ON(!rcache->cpu_rcaches))
822 continue;
823 for_each_possible_cpu(cpu) {
824 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
825 spin_lock_init(&cpu_rcache->lock);
826 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
827 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
828 }
829 }
830}
831
832/*
833 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
834 * return true on success. Can fail if rcache is full and we can't free
835 * space, and free_iova() (our only caller) will then return the IOVA
836 * range to the rbtree instead.
837 */
838static bool __iova_rcache_insert(struct iova_domain *iovad,
839 struct iova_rcache *rcache,
840 unsigned long iova_pfn)
841{
842 struct iova_magazine *mag_to_free = NULL;
843 struct iova_cpu_rcache *cpu_rcache;
844 bool can_insert = false;
845 unsigned long flags;
846
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +0200847 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300848 spin_lock_irqsave(&cpu_rcache->lock, flags);
849
850 if (!iova_magazine_full(cpu_rcache->loaded)) {
851 can_insert = true;
852 } else if (!iova_magazine_full(cpu_rcache->prev)) {
853 swap(cpu_rcache->prev, cpu_rcache->loaded);
854 can_insert = true;
855 } else {
856 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
857
858 if (new_mag) {
859 spin_lock(&rcache->lock);
860 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
861 rcache->depot[rcache->depot_size++] =
862 cpu_rcache->loaded;
863 } else {
864 mag_to_free = cpu_rcache->loaded;
865 }
866 spin_unlock(&rcache->lock);
867
868 cpu_rcache->loaded = new_mag;
869 can_insert = true;
870 }
871 }
872
873 if (can_insert)
874 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
875
876 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
877
878 if (mag_to_free) {
879 iova_magazine_free_pfns(mag_to_free, iovad);
880 iova_magazine_free(mag_to_free);
881 }
882
883 return can_insert;
884}
885
886static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
887 unsigned long size)
888{
889 unsigned int log_size = order_base_2(size);
890
891 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
892 return false;
893
894 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
895}
896
897/*
898 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
899 * satisfy the request, return a matching non-NULL range and remove
900 * it from the 'rcache'.
901 */
902static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
903 unsigned long limit_pfn)
904{
905 struct iova_cpu_rcache *cpu_rcache;
906 unsigned long iova_pfn = 0;
907 bool has_pfn = false;
908 unsigned long flags;
909
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +0200910 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300911 spin_lock_irqsave(&cpu_rcache->lock, flags);
912
913 if (!iova_magazine_empty(cpu_rcache->loaded)) {
914 has_pfn = true;
915 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
916 swap(cpu_rcache->prev, cpu_rcache->loaded);
917 has_pfn = true;
918 } else {
919 spin_lock(&rcache->lock);
920 if (rcache->depot_size > 0) {
921 iova_magazine_free(cpu_rcache->loaded);
922 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
923 has_pfn = true;
924 }
925 spin_unlock(&rcache->lock);
926 }
927
928 if (has_pfn)
929 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
930
931 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
932
933 return iova_pfn;
934}
935
936/*
937 * Try to satisfy IOVA allocation range from rcache. Fail if requested
938 * size is too big or the DMA limit we are given isn't satisfied by the
939 * top element in the magazine.
940 */
941static unsigned long iova_rcache_get(struct iova_domain *iovad,
942 unsigned long size,
943 unsigned long limit_pfn)
944{
945 unsigned int log_size = order_base_2(size);
946
947 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
948 return 0;
949
950 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
951}
952
953/*
954 * Free a cpu's rcache.
955 */
956static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
957 struct iova_rcache *rcache)
958{
959 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
960 unsigned long flags;
961
962 spin_lock_irqsave(&cpu_rcache->lock, flags);
963
964 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
965 iova_magazine_free(cpu_rcache->loaded);
966
967 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
968 iova_magazine_free(cpu_rcache->prev);
969
970 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
971}
972
973/*
974 * free rcache data structures.
975 */
976static void free_iova_rcaches(struct iova_domain *iovad)
977{
978 struct iova_rcache *rcache;
979 unsigned long flags;
980 unsigned int cpu;
981 int i, j;
982
983 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
984 rcache = &iovad->rcaches[i];
985 for_each_possible_cpu(cpu)
986 free_cpu_iova_rcache(cpu, iovad, rcache);
987 spin_lock_irqsave(&rcache->lock, flags);
988 free_percpu(rcache->cpu_rcaches);
989 for (j = 0; j < rcache->depot_size; ++j) {
990 iova_magazine_free_pfns(rcache->depot[j], iovad);
991 iova_magazine_free(rcache->depot[j]);
992 }
993 spin_unlock_irqrestore(&rcache->lock, flags);
994 }
995}
996
997/*
998 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
999 */
1000void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1001{
1002 struct iova_cpu_rcache *cpu_rcache;
1003 struct iova_rcache *rcache;
1004 unsigned long flags;
1005 int i;
1006
1007 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1008 rcache = &iovad->rcaches[i];
1009 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1010 spin_lock_irqsave(&cpu_rcache->lock, flags);
1011 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1012 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1013 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1014 }
1015}
1016
Sakari Ailus15bbdec2015-07-13 14:31:30 +03001017MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1018MODULE_LICENSE("GPL");