blob: e80a4105ac2abe55d365a9a5f03220f97417c9ac [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>
25
26static bool iova_rcache_insert(struct iova_domain *iovad,
27 unsigned long pfn,
28 unsigned long size);
29static unsigned long iova_rcache_get(struct iova_domain *iovad,
30 unsigned long size,
31 unsigned long limit_pfn);
32static void init_iova_rcaches(struct iova_domain *iovad);
33static void free_iova_rcaches(struct iova_domain *iovad);
Robin Murphy85b45452015-01-12 17:51:14 +000034
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070035void
Robin Murphy0fb5fe82015-01-12 17:51:16 +000036init_iova_domain(struct iova_domain *iovad, unsigned long granule,
37 unsigned long start_pfn, unsigned long pfn_32bit)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070038{
Robin Murphy0fb5fe82015-01-12 17:51:16 +000039 /*
40 * IOVA granularity will normally be equal to the smallest
41 * supported IOMMU page size; both *must* be capable of
42 * representing individual CPU pages exactly.
43 */
44 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
45
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070046 spin_lock_init(&iovad->iova_rbtree_lock);
47 iovad->rbroot = RB_ROOT;
48 iovad->cached32_node = NULL;
Robin Murphy0fb5fe82015-01-12 17:51:16 +000049 iovad->granule = granule;
Robin Murphy1b722502015-01-12 17:51:15 +000050 iovad->start_pfn = start_pfn;
David Millerf6611972008-02-06 01:36:23 -080051 iovad->dma_32bit_pfn = pfn_32bit;
Omer Peleg9257b4a2016-04-20 11:34:11 +030052 init_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070053}
Sakari Ailus9b417602015-07-13 14:31:29 +030054EXPORT_SYMBOL_GPL(init_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070055
56static struct rb_node *
57__get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
58{
Robin Murphy62280cf2016-11-11 18:35:46 +000059 if ((*limit_pfn > iovad->dma_32bit_pfn) ||
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070060 (iovad->cached32_node == NULL))
61 return rb_last(&iovad->rbroot);
62 else {
63 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
64 struct iova *curr_iova =
Geliang Tangeba484b2016-12-19 22:46:58 +080065 rb_entry(iovad->cached32_node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070066 *limit_pfn = curr_iova->pfn_lo - 1;
67 return prev_node;
68 }
69}
70
71static void
72__cached_rbnode_insert_update(struct iova_domain *iovad,
73 unsigned long limit_pfn, struct iova *new)
74{
David Millerf6611972008-02-06 01:36:23 -080075 if (limit_pfn != iovad->dma_32bit_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070076 return;
77 iovad->cached32_node = &new->node;
78}
79
80static void
81__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
82{
83 struct iova *cached_iova;
84 struct rb_node *curr;
85
86 if (!iovad->cached32_node)
87 return;
88 curr = iovad->cached32_node;
Geliang Tangeba484b2016-12-19 22:46:58 +080089 cached_iova = rb_entry(curr, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -070090
Chris Wright1c9fc3d2011-05-28 13:15:04 -050091 if (free->pfn_lo >= cached_iova->pfn_lo) {
92 struct rb_node *node = rb_next(&free->node);
Geliang Tangeba484b2016-12-19 22:46:58 +080093 struct iova *iova = rb_entry(node, struct iova, node);
Chris Wright1c9fc3d2011-05-28 13:15:04 -050094
95 /* only cache if it's below 32bit pfn */
96 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
97 iovad->cached32_node = node;
98 else
99 iovad->cached32_node = NULL;
100 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700101}
102
Marek Szyprowskid7517512017-02-24 12:13:37 +0100103/* Insert the iova into domain rbtree by holding writer lock */
104static void
105iova_insert_rbtree(struct rb_root *root, struct iova *iova,
106 struct rb_node *start)
107{
108 struct rb_node **new, *parent = NULL;
109
110 new = (start) ? &start : &(root->rb_node);
111 /* Figure out where to put new node */
112 while (*new) {
113 struct iova *this = rb_entry(*new, struct iova, node);
114
115 parent = *new;
116
117 if (iova->pfn_lo < this->pfn_lo)
118 new = &((*new)->rb_left);
119 else if (iova->pfn_lo > this->pfn_lo)
120 new = &((*new)->rb_right);
121 else {
122 WARN_ON(1); /* this should not happen */
123 return;
124 }
125 }
126 /* Add new node and rebalance tree. */
127 rb_link_node(&iova->node, parent, new);
128 rb_insert_color(&iova->node, root);
129}
130
Robin Murphy8f6429c2015-07-16 19:40:12 +0100131/*
132 * Computes the padding size required, to make the start address
133 * naturally aligned on the power-of-two order of its size
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700134 */
Robin Murphy8f6429c2015-07-16 19:40:12 +0100135static unsigned int
136iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700137{
Robin Murphy8f6429c2015-07-16 19:40:12 +0100138 return (limit_pfn + 1 - size) & (__roundup_pow_of_two(size) - 1);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700139}
140
mark grossddf02882008-03-04 15:22:04 -0800141static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
142 unsigned long size, unsigned long limit_pfn,
143 struct iova *new, bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700144{
mark grossddf02882008-03-04 15:22:04 -0800145 struct rb_node *prev, *curr = NULL;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700146 unsigned long flags;
147 unsigned long saved_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700148 unsigned int pad_size = 0;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700149
150 /* Walk the tree backwards */
151 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
152 saved_pfn = limit_pfn;
153 curr = __get_cached_rbnode(iovad, &limit_pfn);
mark grossddf02882008-03-04 15:22:04 -0800154 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700155 while (curr) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800156 struct iova *curr_iova = rb_entry(curr, struct iova, node);
mark grossddf02882008-03-04 15:22:04 -0800157
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700158 if (limit_pfn < curr_iova->pfn_lo)
159 goto move_left;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700160 else if (limit_pfn < curr_iova->pfn_hi)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700161 goto adjust_limit_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700162 else {
163 if (size_aligned)
164 pad_size = iova_get_pad_size(size, limit_pfn);
165 if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn)
166 break; /* found a free slot */
167 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700168adjust_limit_pfn:
169 limit_pfn = curr_iova->pfn_lo - 1;
170move_left:
mark grossddf02882008-03-04 15:22:04 -0800171 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700172 curr = rb_prev(curr);
173 }
174
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700175 if (!curr) {
176 if (size_aligned)
177 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy1b722502015-01-12 17:51:15 +0000178 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700179 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
180 return -ENOMEM;
181 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700182 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700183
184 /* pfn_lo will point to size aligned address if size_aligned is set */
185 new->pfn_lo = limit_pfn - (size + pad_size) + 1;
186 new->pfn_hi = new->pfn_lo + size - 1;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700187
Marek Szyprowskid7517512017-02-24 12:13:37 +0100188 /* If we have 'prev', it's a valid place to start the insertion. */
189 iova_insert_rbtree(&iovad->rbroot, new, prev);
mark grossddf02882008-03-04 15:22:04 -0800190 __cached_rbnode_insert_update(iovad, saved_pfn, new);
191
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700192 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
mark grossddf02882008-03-04 15:22:04 -0800193
194
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700195 return 0;
196}
197
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300198static struct kmem_cache *iova_cache;
199static unsigned int iova_cache_users;
200static DEFINE_MUTEX(iova_cache_mutex);
201
202struct iova *alloc_iova_mem(void)
203{
204 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
205}
206EXPORT_SYMBOL(alloc_iova_mem);
207
208void free_iova_mem(struct iova *iova)
209{
210 kmem_cache_free(iova_cache, iova);
211}
212EXPORT_SYMBOL(free_iova_mem);
213
214int iova_cache_get(void)
215{
216 mutex_lock(&iova_cache_mutex);
217 if (!iova_cache_users) {
218 iova_cache = kmem_cache_create(
219 "iommu_iova", sizeof(struct iova), 0,
220 SLAB_HWCACHE_ALIGN, NULL);
221 if (!iova_cache) {
222 mutex_unlock(&iova_cache_mutex);
223 printk(KERN_ERR "Couldn't create iova cache\n");
224 return -ENOMEM;
225 }
226 }
227
228 iova_cache_users++;
229 mutex_unlock(&iova_cache_mutex);
230
231 return 0;
232}
Sakari Ailus9b417602015-07-13 14:31:29 +0300233EXPORT_SYMBOL_GPL(iova_cache_get);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300234
235void iova_cache_put(void)
236{
237 mutex_lock(&iova_cache_mutex);
238 if (WARN_ON(!iova_cache_users)) {
239 mutex_unlock(&iova_cache_mutex);
240 return;
241 }
242 iova_cache_users--;
243 if (!iova_cache_users)
244 kmem_cache_destroy(iova_cache);
245 mutex_unlock(&iova_cache_mutex);
246}
Sakari Ailus9b417602015-07-13 14:31:29 +0300247EXPORT_SYMBOL_GPL(iova_cache_put);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300248
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700249/**
250 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900251 * @iovad: - iova domain in question
252 * @size: - size of page frames to allocate
253 * @limit_pfn: - max limit address
254 * @size_aligned: - set if size_aligned address range is required
Robin Murphy1b722502015-01-12 17:51:15 +0000255 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
256 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700257 * flag is set then the allocated address iova->pfn_lo will be naturally
258 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700259 */
260struct iova *
261alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700262 unsigned long limit_pfn,
263 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700264{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700265 struct iova *new_iova;
266 int ret;
267
268 new_iova = alloc_iova_mem();
269 if (!new_iova)
270 return NULL;
271
mark grossddf02882008-03-04 15:22:04 -0800272 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn,
273 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700274
275 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700276 free_iova_mem(new_iova);
277 return NULL;
278 }
279
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700280 return new_iova;
281}
Sakari Ailus9b417602015-07-13 14:31:29 +0300282EXPORT_SYMBOL_GPL(alloc_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700283
Omer Peleg9257b4a2016-04-20 11:34:11 +0300284static struct iova *
285private_find_iova(struct iova_domain *iovad, unsigned long pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700286{
Omer Peleg9257b4a2016-04-20 11:34:11 +0300287 struct rb_node *node = iovad->rbroot.rb_node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700288
Omer Peleg9257b4a2016-04-20 11:34:11 +0300289 assert_spin_locked(&iovad->iova_rbtree_lock);
290
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700291 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800292 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700293
294 /* If pfn falls within iova's range, return iova */
295 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700296 return iova;
297 }
298
299 if (pfn < iova->pfn_lo)
300 node = node->rb_left;
301 else if (pfn > iova->pfn_lo)
302 node = node->rb_right;
303 }
304
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700305 return NULL;
306}
Omer Peleg9257b4a2016-04-20 11:34:11 +0300307
308static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
309{
310 assert_spin_locked(&iovad->iova_rbtree_lock);
311 __cached_rbnode_delete_update(iovad, iova);
312 rb_erase(&iova->node, &iovad->rbroot);
313 free_iova_mem(iova);
314}
315
316/**
317 * find_iova - finds an iova for a given pfn
318 * @iovad: - iova domain in question.
319 * @pfn: - page frame number
320 * This function finds and returns an iova belonging to the
321 * given doamin which matches the given pfn.
322 */
323struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
324{
325 unsigned long flags;
326 struct iova *iova;
327
328 /* Take the lock so that no other thread is manipulating the rbtree */
329 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
330 iova = private_find_iova(iovad, pfn);
331 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
332 return iova;
333}
Sakari Ailus9b417602015-07-13 14:31:29 +0300334EXPORT_SYMBOL_GPL(find_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700335
336/**
337 * __free_iova - frees the given iova
338 * @iovad: iova domain in question.
339 * @iova: iova in question.
340 * Frees the given iova belonging to the giving domain
341 */
342void
343__free_iova(struct iova_domain *iovad, struct iova *iova)
344{
345 unsigned long flags;
346
347 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300348 private_free_iova(iovad, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700349 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700350}
Sakari Ailus9b417602015-07-13 14:31:29 +0300351EXPORT_SYMBOL_GPL(__free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700352
353/**
354 * free_iova - finds and frees the iova for a given pfn
355 * @iovad: - iova domain in question.
356 * @pfn: - pfn that is allocated previously
357 * This functions finds an iova for a given pfn and then
358 * frees the iova from that domain.
359 */
360void
361free_iova(struct iova_domain *iovad, unsigned long pfn)
362{
363 struct iova *iova = find_iova(iovad, pfn);
Robert Callicotte733cac22015-04-16 23:32:47 -0500364
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700365 if (iova)
366 __free_iova(iovad, iova);
367
368}
Sakari Ailus9b417602015-07-13 14:31:29 +0300369EXPORT_SYMBOL_GPL(free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700370
371/**
Omer Peleg9257b4a2016-04-20 11:34:11 +0300372 * alloc_iova_fast - allocates an iova from rcache
373 * @iovad: - iova domain in question
374 * @size: - size of page frames to allocate
375 * @limit_pfn: - max limit address
376 * This function tries to satisfy an iova allocation from the rcache,
377 * and falls back to regular allocation on failure.
378*/
379unsigned long
380alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
381 unsigned long limit_pfn)
382{
383 bool flushed_rcache = false;
384 unsigned long iova_pfn;
385 struct iova *new_iova;
386
387 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
388 if (iova_pfn)
389 return iova_pfn;
390
391retry:
392 new_iova = alloc_iova(iovad, size, limit_pfn, true);
393 if (!new_iova) {
394 unsigned int cpu;
395
396 if (flushed_rcache)
397 return 0;
398
399 /* Try replenishing IOVAs by flushing rcache. */
400 flushed_rcache = true;
Chris Wilson583248e2016-06-01 12:10:08 +0100401 preempt_disable();
Omer Peleg9257b4a2016-04-20 11:34:11 +0300402 for_each_online_cpu(cpu)
403 free_cpu_cached_iovas(cpu, iovad);
Chris Wilson583248e2016-06-01 12:10:08 +0100404 preempt_enable();
Omer Peleg9257b4a2016-04-20 11:34:11 +0300405 goto retry;
406 }
407
408 return new_iova->pfn_lo;
409}
410EXPORT_SYMBOL_GPL(alloc_iova_fast);
411
412/**
413 * free_iova_fast - free iova pfn range into rcache
414 * @iovad: - iova domain in question.
415 * @pfn: - pfn that is allocated previously
416 * @size: - # of pages in range
417 * This functions frees an iova range by trying to put it into the rcache,
418 * falling back to regular iova deallocation via free_iova() if this fails.
419 */
420void
421free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
422{
423 if (iova_rcache_insert(iovad, pfn, size))
424 return;
425
426 free_iova(iovad, pfn);
427}
428EXPORT_SYMBOL_GPL(free_iova_fast);
429
430/**
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700431 * put_iova_domain - destroys the iova doamin
432 * @iovad: - iova domain in question.
433 * All the iova's in that domain are destroyed.
434 */
435void put_iova_domain(struct iova_domain *iovad)
436{
437 struct rb_node *node;
438 unsigned long flags;
439
Omer Peleg9257b4a2016-04-20 11:34:11 +0300440 free_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700441 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
442 node = rb_first(&iovad->rbroot);
443 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800444 struct iova *iova = rb_entry(node, struct iova, node);
Robert Callicotte733cac22015-04-16 23:32:47 -0500445
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700446 rb_erase(node, &iovad->rbroot);
447 free_iova_mem(iova);
448 node = rb_first(&iovad->rbroot);
449 }
450 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
451}
Sakari Ailus9b417602015-07-13 14:31:29 +0300452EXPORT_SYMBOL_GPL(put_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700453
454static int
455__is_range_overlap(struct rb_node *node,
456 unsigned long pfn_lo, unsigned long pfn_hi)
457{
Geliang Tangeba484b2016-12-19 22:46:58 +0800458 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700459
460 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
461 return 1;
462 return 0;
463}
464
Jiang Liu75f05562014-02-19 14:07:37 +0800465static inline struct iova *
466alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
467{
468 struct iova *iova;
469
470 iova = alloc_iova_mem();
471 if (iova) {
472 iova->pfn_lo = pfn_lo;
473 iova->pfn_hi = pfn_hi;
474 }
475
476 return iova;
477}
478
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700479static struct iova *
480__insert_new_range(struct iova_domain *iovad,
481 unsigned long pfn_lo, unsigned long pfn_hi)
482{
483 struct iova *iova;
484
Jiang Liu75f05562014-02-19 14:07:37 +0800485 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
486 if (iova)
Marek Szyprowskid7517512017-02-24 12:13:37 +0100487 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700488
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700489 return iova;
490}
491
492static void
493__adjust_overlap_range(struct iova *iova,
494 unsigned long *pfn_lo, unsigned long *pfn_hi)
495{
496 if (*pfn_lo < iova->pfn_lo)
497 iova->pfn_lo = *pfn_lo;
498 if (*pfn_hi > iova->pfn_hi)
499 *pfn_lo = iova->pfn_hi + 1;
500}
501
502/**
503 * reserve_iova - reserves an iova in the given range
504 * @iovad: - iova domain pointer
505 * @pfn_lo: - lower page frame address
506 * @pfn_hi:- higher pfn adderss
507 * This function allocates reserves the address range from pfn_lo to pfn_hi so
508 * that this address is not dished out as part of alloc_iova.
509 */
510struct iova *
511reserve_iova(struct iova_domain *iovad,
512 unsigned long pfn_lo, unsigned long pfn_hi)
513{
514 struct rb_node *node;
515 unsigned long flags;
516 struct iova *iova;
517 unsigned int overlap = 0;
518
David Woodhouse3d39cec2009-07-08 15:23:30 +0100519 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700520 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
521 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800522 iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700523 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
524 if ((pfn_lo >= iova->pfn_lo) &&
525 (pfn_hi <= iova->pfn_hi))
526 goto finish;
527 overlap = 1;
528
529 } else if (overlap)
530 break;
531 }
532
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300533 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700534 * or need to insert remaining non overlap addr range
535 */
536 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
537finish:
538
David Woodhouse3d39cec2009-07-08 15:23:30 +0100539 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700540 return iova;
541}
Sakari Ailus9b417602015-07-13 14:31:29 +0300542EXPORT_SYMBOL_GPL(reserve_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700543
544/**
545 * copy_reserved_iova - copies the reserved between domains
546 * @from: - source doamin from where to copy
547 * @to: - destination domin where to copy
548 * This function copies reserved iova's from one doamin to
549 * other.
550 */
551void
552copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
553{
554 unsigned long flags;
555 struct rb_node *node;
556
David Woodhouse3d39cec2009-07-08 15:23:30 +0100557 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700558 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800559 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700560 struct iova *new_iova;
Robert Callicotte733cac22015-04-16 23:32:47 -0500561
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700562 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
563 if (!new_iova)
564 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
565 iova->pfn_lo, iova->pfn_lo);
566 }
David Woodhouse3d39cec2009-07-08 15:23:30 +0100567 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700568}
Sakari Ailus9b417602015-07-13 14:31:29 +0300569EXPORT_SYMBOL_GPL(copy_reserved_iova);
Jiang Liu75f05562014-02-19 14:07:37 +0800570
571struct iova *
572split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
573 unsigned long pfn_lo, unsigned long pfn_hi)
574{
575 unsigned long flags;
576 struct iova *prev = NULL, *next = NULL;
577
578 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
579 if (iova->pfn_lo < pfn_lo) {
580 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
581 if (prev == NULL)
582 goto error;
583 }
584 if (iova->pfn_hi > pfn_hi) {
585 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
586 if (next == NULL)
587 goto error;
588 }
589
590 __cached_rbnode_delete_update(iovad, iova);
591 rb_erase(&iova->node, &iovad->rbroot);
592
593 if (prev) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100594 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800595 iova->pfn_lo = pfn_lo;
596 }
597 if (next) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100598 iova_insert_rbtree(&iovad->rbroot, next, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800599 iova->pfn_hi = pfn_hi;
600 }
601 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
602
603 return iova;
604
605error:
606 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
607 if (prev)
608 free_iova_mem(prev);
609 return NULL;
610}
Sakari Ailus15bbdec2015-07-13 14:31:30 +0300611
Omer Peleg9257b4a2016-04-20 11:34:11 +0300612/*
613 * Magazine caches for IOVA ranges. For an introduction to magazines,
614 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
615 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
616 * For simplicity, we use a static magazine size and don't implement the
617 * dynamic size tuning described in the paper.
618 */
619
620#define IOVA_MAG_SIZE 128
621
622struct iova_magazine {
623 unsigned long size;
624 unsigned long pfns[IOVA_MAG_SIZE];
625};
626
627struct iova_cpu_rcache {
628 spinlock_t lock;
629 struct iova_magazine *loaded;
630 struct iova_magazine *prev;
631};
632
633static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
634{
635 return kzalloc(sizeof(struct iova_magazine), flags);
636}
637
638static void iova_magazine_free(struct iova_magazine *mag)
639{
640 kfree(mag);
641}
642
643static void
644iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
645{
646 unsigned long flags;
647 int i;
648
649 if (!mag)
650 return;
651
652 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
653
654 for (i = 0 ; i < mag->size; ++i) {
655 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
656
657 BUG_ON(!iova);
658 private_free_iova(iovad, iova);
659 }
660
661 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
662
663 mag->size = 0;
664}
665
666static bool iova_magazine_full(struct iova_magazine *mag)
667{
668 return (mag && mag->size == IOVA_MAG_SIZE);
669}
670
671static bool iova_magazine_empty(struct iova_magazine *mag)
672{
673 return (!mag || mag->size == 0);
674}
675
676static unsigned long iova_magazine_pop(struct iova_magazine *mag,
677 unsigned long limit_pfn)
678{
679 BUG_ON(iova_magazine_empty(mag));
680
681 if (mag->pfns[mag->size - 1] >= limit_pfn)
682 return 0;
683
684 return mag->pfns[--mag->size];
685}
686
687static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
688{
689 BUG_ON(iova_magazine_full(mag));
690
691 mag->pfns[mag->size++] = pfn;
692}
693
694static void init_iova_rcaches(struct iova_domain *iovad)
695{
696 struct iova_cpu_rcache *cpu_rcache;
697 struct iova_rcache *rcache;
698 unsigned int cpu;
699 int i;
700
701 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
702 rcache = &iovad->rcaches[i];
703 spin_lock_init(&rcache->lock);
704 rcache->depot_size = 0;
705 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
706 if (WARN_ON(!rcache->cpu_rcaches))
707 continue;
708 for_each_possible_cpu(cpu) {
709 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
710 spin_lock_init(&cpu_rcache->lock);
711 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
712 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
713 }
714 }
715}
716
717/*
718 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
719 * return true on success. Can fail if rcache is full and we can't free
720 * space, and free_iova() (our only caller) will then return the IOVA
721 * range to the rbtree instead.
722 */
723static bool __iova_rcache_insert(struct iova_domain *iovad,
724 struct iova_rcache *rcache,
725 unsigned long iova_pfn)
726{
727 struct iova_magazine *mag_to_free = NULL;
728 struct iova_cpu_rcache *cpu_rcache;
729 bool can_insert = false;
730 unsigned long flags;
731
Chris Wilson583248e2016-06-01 12:10:08 +0100732 cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300733 spin_lock_irqsave(&cpu_rcache->lock, flags);
734
735 if (!iova_magazine_full(cpu_rcache->loaded)) {
736 can_insert = true;
737 } else if (!iova_magazine_full(cpu_rcache->prev)) {
738 swap(cpu_rcache->prev, cpu_rcache->loaded);
739 can_insert = true;
740 } else {
741 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
742
743 if (new_mag) {
744 spin_lock(&rcache->lock);
745 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
746 rcache->depot[rcache->depot_size++] =
747 cpu_rcache->loaded;
748 } else {
749 mag_to_free = cpu_rcache->loaded;
750 }
751 spin_unlock(&rcache->lock);
752
753 cpu_rcache->loaded = new_mag;
754 can_insert = true;
755 }
756 }
757
758 if (can_insert)
759 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
760
761 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
Chris Wilson583248e2016-06-01 12:10:08 +0100762 put_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300763
764 if (mag_to_free) {
765 iova_magazine_free_pfns(mag_to_free, iovad);
766 iova_magazine_free(mag_to_free);
767 }
768
769 return can_insert;
770}
771
772static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
773 unsigned long size)
774{
775 unsigned int log_size = order_base_2(size);
776
777 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
778 return false;
779
780 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
781}
782
783/*
784 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
785 * satisfy the request, return a matching non-NULL range and remove
786 * it from the 'rcache'.
787 */
788static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
789 unsigned long limit_pfn)
790{
791 struct iova_cpu_rcache *cpu_rcache;
792 unsigned long iova_pfn = 0;
793 bool has_pfn = false;
794 unsigned long flags;
795
Chris Wilson583248e2016-06-01 12:10:08 +0100796 cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300797 spin_lock_irqsave(&cpu_rcache->lock, flags);
798
799 if (!iova_magazine_empty(cpu_rcache->loaded)) {
800 has_pfn = true;
801 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
802 swap(cpu_rcache->prev, cpu_rcache->loaded);
803 has_pfn = true;
804 } else {
805 spin_lock(&rcache->lock);
806 if (rcache->depot_size > 0) {
807 iova_magazine_free(cpu_rcache->loaded);
808 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
809 has_pfn = true;
810 }
811 spin_unlock(&rcache->lock);
812 }
813
814 if (has_pfn)
815 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
816
817 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
Chris Wilson583248e2016-06-01 12:10:08 +0100818 put_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300819
820 return iova_pfn;
821}
822
823/*
824 * Try to satisfy IOVA allocation range from rcache. Fail if requested
825 * size is too big or the DMA limit we are given isn't satisfied by the
826 * top element in the magazine.
827 */
828static unsigned long iova_rcache_get(struct iova_domain *iovad,
829 unsigned long size,
830 unsigned long limit_pfn)
831{
832 unsigned int log_size = order_base_2(size);
833
834 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
835 return 0;
836
837 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
838}
839
840/*
841 * Free a cpu's rcache.
842 */
843static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
844 struct iova_rcache *rcache)
845{
846 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
847 unsigned long flags;
848
849 spin_lock_irqsave(&cpu_rcache->lock, flags);
850
851 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
852 iova_magazine_free(cpu_rcache->loaded);
853
854 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
855 iova_magazine_free(cpu_rcache->prev);
856
857 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
858}
859
860/*
861 * free rcache data structures.
862 */
863static void free_iova_rcaches(struct iova_domain *iovad)
864{
865 struct iova_rcache *rcache;
866 unsigned long flags;
867 unsigned int cpu;
868 int i, j;
869
870 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
871 rcache = &iovad->rcaches[i];
872 for_each_possible_cpu(cpu)
873 free_cpu_iova_rcache(cpu, iovad, rcache);
874 spin_lock_irqsave(&rcache->lock, flags);
875 free_percpu(rcache->cpu_rcaches);
876 for (j = 0; j < rcache->depot_size; ++j) {
877 iova_magazine_free_pfns(rcache->depot[j], iovad);
878 iova_magazine_free(rcache->depot[j]);
879 }
880 spin_unlock_irqrestore(&rcache->lock, flags);
881 }
882}
883
884/*
885 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
886 */
887void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
888{
889 struct iova_cpu_rcache *cpu_rcache;
890 struct iova_rcache *rcache;
891 unsigned long flags;
892 int i;
893
894 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
895 rcache = &iovad->rcaches[i];
896 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
897 spin_lock_irqsave(&cpu_rcache->lock, flags);
898 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
899 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
900 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
901 }
902}
903
Sakari Ailus15bbdec2015-07-13 14:31:30 +0300904MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
905MODULE_LICENSE("GPL");