blob: 749d39533e0b6b0792e3ccd7f0ea4af6fd651f20 [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
Joerg Roedelfb418da2017-08-10 16:14:59 +020078 atomic64_set(&iovad->fq_flush_start_cnt, 0);
79 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
80
Joerg Roedel42f87e72017-08-10 14:44:28 +020081 iovad->fq = alloc_percpu(struct iova_fq);
82 if (!iovad->fq)
83 return -ENOMEM;
84
85 iovad->flush_cb = flush_cb;
86 iovad->entry_dtor = entry_dtor;
87
88 for_each_possible_cpu(cpu) {
89 struct iova_fq *fq;
90
91 fq = per_cpu_ptr(iovad->fq, cpu);
92 fq->head = 0;
93 fq->tail = 0;
Joerg Roedel8109c2a2017-08-10 16:31:17 +020094
95 spin_lock_init(&fq->lock);
Joerg Roedel42f87e72017-08-10 14:44:28 +020096 }
97
98 return 0;
99}
100EXPORT_SYMBOL_GPL(init_iova_flush_queue);
101
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700102static struct rb_node *
103__get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
104{
Robin Murphy62280cf2016-11-11 18:35:46 +0000105 if ((*limit_pfn > iovad->dma_32bit_pfn) ||
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700106 (iovad->cached32_node == NULL))
107 return rb_last(&iovad->rbroot);
108 else {
109 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
110 struct iova *curr_iova =
Geliang Tangeba484b2016-12-19 22:46:58 +0800111 rb_entry(iovad->cached32_node, struct iova, node);
Robin Murphy757c3702017-05-16 12:26:48 +0100112 *limit_pfn = curr_iova->pfn_lo;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700113 return prev_node;
114 }
115}
116
117static void
118__cached_rbnode_insert_update(struct iova_domain *iovad,
119 unsigned long limit_pfn, struct iova *new)
120{
David Millerf6611972008-02-06 01:36:23 -0800121 if (limit_pfn != iovad->dma_32bit_pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700122 return;
123 iovad->cached32_node = &new->node;
124}
125
126static void
127__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
128{
129 struct iova *cached_iova;
130 struct rb_node *curr;
131
132 if (!iovad->cached32_node)
133 return;
134 curr = iovad->cached32_node;
Geliang Tangeba484b2016-12-19 22:46:58 +0800135 cached_iova = rb_entry(curr, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700136
Chris Wright1c9fc3d2011-05-28 13:15:04 -0500137 if (free->pfn_lo >= cached_iova->pfn_lo) {
138 struct rb_node *node = rb_next(&free->node);
Geliang Tangeba484b2016-12-19 22:46:58 +0800139 struct iova *iova = rb_entry(node, struct iova, node);
Chris Wright1c9fc3d2011-05-28 13:15:04 -0500140
141 /* only cache if it's below 32bit pfn */
142 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
143 iovad->cached32_node = node;
144 else
145 iovad->cached32_node = NULL;
146 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700147}
148
Marek Szyprowskid7517512017-02-24 12:13:37 +0100149/* Insert the iova into domain rbtree by holding writer lock */
150static void
151iova_insert_rbtree(struct rb_root *root, struct iova *iova,
152 struct rb_node *start)
153{
154 struct rb_node **new, *parent = NULL;
155
156 new = (start) ? &start : &(root->rb_node);
157 /* Figure out where to put new node */
158 while (*new) {
159 struct iova *this = rb_entry(*new, struct iova, node);
160
161 parent = *new;
162
163 if (iova->pfn_lo < this->pfn_lo)
164 new = &((*new)->rb_left);
165 else if (iova->pfn_lo > this->pfn_lo)
166 new = &((*new)->rb_right);
167 else {
168 WARN_ON(1); /* this should not happen */
169 return;
170 }
171 }
172 /* Add new node and rebalance tree. */
173 rb_link_node(&iova->node, parent, new);
174 rb_insert_color(&iova->node, root);
175}
176
Robin Murphy8f6429c2015-07-16 19:40:12 +0100177/*
178 * Computes the padding size required, to make the start address
179 * naturally aligned on the power-of-two order of its size
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700180 */
Robin Murphy8f6429c2015-07-16 19:40:12 +0100181static unsigned int
182iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700183{
Robin Murphy757c3702017-05-16 12:26:48 +0100184 return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700185}
186
mark grossddf02882008-03-04 15:22:04 -0800187static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
188 unsigned long size, unsigned long limit_pfn,
189 struct iova *new, bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700190{
mark grossddf02882008-03-04 15:22:04 -0800191 struct rb_node *prev, *curr = NULL;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700192 unsigned long flags;
193 unsigned long saved_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700194 unsigned int pad_size = 0;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700195
196 /* Walk the tree backwards */
197 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
198 saved_pfn = limit_pfn;
199 curr = __get_cached_rbnode(iovad, &limit_pfn);
mark grossddf02882008-03-04 15:22:04 -0800200 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700201 while (curr) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800202 struct iova *curr_iova = rb_entry(curr, struct iova, node);
mark grossddf02882008-03-04 15:22:04 -0800203
Robin Murphy757c3702017-05-16 12:26:48 +0100204 if (limit_pfn <= curr_iova->pfn_lo) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700205 goto move_left;
Robin Murphy757c3702017-05-16 12:26:48 +0100206 } else if (limit_pfn > curr_iova->pfn_hi) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700207 if (size_aligned)
208 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy757c3702017-05-16 12:26:48 +0100209 if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700210 break; /* found a free slot */
211 }
Robin Murphy757c3702017-05-16 12:26:48 +0100212 limit_pfn = curr_iova->pfn_lo;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700213move_left:
mark grossddf02882008-03-04 15:22:04 -0800214 prev = curr;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700215 curr = rb_prev(curr);
216 }
217
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700218 if (!curr) {
219 if (size_aligned)
220 pad_size = iova_get_pad_size(size, limit_pfn);
Robin Murphy1b722502015-01-12 17:51:15 +0000221 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700222 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
223 return -ENOMEM;
224 }
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700225 }
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700226
227 /* pfn_lo will point to size aligned address if size_aligned is set */
Robin Murphy757c3702017-05-16 12:26:48 +0100228 new->pfn_lo = limit_pfn - (size + pad_size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700229 new->pfn_hi = new->pfn_lo + size - 1;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700230
Marek Szyprowskid7517512017-02-24 12:13:37 +0100231 /* If we have 'prev', it's a valid place to start the insertion. */
232 iova_insert_rbtree(&iovad->rbroot, new, prev);
mark grossddf02882008-03-04 15:22:04 -0800233 __cached_rbnode_insert_update(iovad, saved_pfn, new);
234
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700235 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
mark grossddf02882008-03-04 15:22:04 -0800236
237
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700238 return 0;
239}
240
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300241static struct kmem_cache *iova_cache;
242static unsigned int iova_cache_users;
243static DEFINE_MUTEX(iova_cache_mutex);
244
245struct iova *alloc_iova_mem(void)
246{
247 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
248}
249EXPORT_SYMBOL(alloc_iova_mem);
250
251void free_iova_mem(struct iova *iova)
252{
253 kmem_cache_free(iova_cache, iova);
254}
255EXPORT_SYMBOL(free_iova_mem);
256
257int iova_cache_get(void)
258{
259 mutex_lock(&iova_cache_mutex);
260 if (!iova_cache_users) {
261 iova_cache = kmem_cache_create(
262 "iommu_iova", sizeof(struct iova), 0,
263 SLAB_HWCACHE_ALIGN, NULL);
264 if (!iova_cache) {
265 mutex_unlock(&iova_cache_mutex);
266 printk(KERN_ERR "Couldn't create iova cache\n");
267 return -ENOMEM;
268 }
269 }
270
271 iova_cache_users++;
272 mutex_unlock(&iova_cache_mutex);
273
274 return 0;
275}
Sakari Ailus9b417602015-07-13 14:31:29 +0300276EXPORT_SYMBOL_GPL(iova_cache_get);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300277
278void iova_cache_put(void)
279{
280 mutex_lock(&iova_cache_mutex);
281 if (WARN_ON(!iova_cache_users)) {
282 mutex_unlock(&iova_cache_mutex);
283 return;
284 }
285 iova_cache_users--;
286 if (!iova_cache_users)
287 kmem_cache_destroy(iova_cache);
288 mutex_unlock(&iova_cache_mutex);
289}
Sakari Ailus9b417602015-07-13 14:31:29 +0300290EXPORT_SYMBOL_GPL(iova_cache_put);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +0300291
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700292/**
293 * alloc_iova - allocates an iova
Masanari Iida07db0402012-07-22 02:21:32 +0900294 * @iovad: - iova domain in question
295 * @size: - size of page frames to allocate
296 * @limit_pfn: - max limit address
297 * @size_aligned: - set if size_aligned address range is required
Robin Murphy1b722502015-01-12 17:51:15 +0000298 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
299 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700300 * flag is set then the allocated address iova->pfn_lo will be naturally
301 * aligned on roundup_power_of_two(size).
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700302 */
303struct iova *
304alloc_iova(struct iova_domain *iovad, unsigned long size,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700305 unsigned long limit_pfn,
306 bool size_aligned)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700307{
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700308 struct iova *new_iova;
309 int ret;
310
311 new_iova = alloc_iova_mem();
312 if (!new_iova)
313 return NULL;
314
Robin Murphy757c3702017-05-16 12:26:48 +0100315 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
mark grossddf02882008-03-04 15:22:04 -0800316 new_iova, size_aligned);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700317
318 if (ret) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700319 free_iova_mem(new_iova);
320 return NULL;
321 }
322
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700323 return new_iova;
324}
Sakari Ailus9b417602015-07-13 14:31:29 +0300325EXPORT_SYMBOL_GPL(alloc_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700326
Omer Peleg9257b4a2016-04-20 11:34:11 +0300327static struct iova *
328private_find_iova(struct iova_domain *iovad, unsigned long pfn)
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700329{
Omer Peleg9257b4a2016-04-20 11:34:11 +0300330 struct rb_node *node = iovad->rbroot.rb_node;
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700331
Omer Peleg9257b4a2016-04-20 11:34:11 +0300332 assert_spin_locked(&iovad->iova_rbtree_lock);
333
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700334 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800335 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700336
337 /* If pfn falls within iova's range, return iova */
338 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700339 return iova;
340 }
341
342 if (pfn < iova->pfn_lo)
343 node = node->rb_left;
344 else if (pfn > iova->pfn_lo)
345 node = node->rb_right;
346 }
347
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700348 return NULL;
349}
Omer Peleg9257b4a2016-04-20 11:34:11 +0300350
351static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
352{
353 assert_spin_locked(&iovad->iova_rbtree_lock);
354 __cached_rbnode_delete_update(iovad, iova);
355 rb_erase(&iova->node, &iovad->rbroot);
356 free_iova_mem(iova);
357}
358
359/**
360 * find_iova - finds an iova for a given pfn
361 * @iovad: - iova domain in question.
362 * @pfn: - page frame number
363 * This function finds and returns an iova belonging to the
364 * given doamin which matches the given pfn.
365 */
366struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
367{
368 unsigned long flags;
369 struct iova *iova;
370
371 /* Take the lock so that no other thread is manipulating the rbtree */
372 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
373 iova = private_find_iova(iovad, pfn);
374 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
375 return iova;
376}
Sakari Ailus9b417602015-07-13 14:31:29 +0300377EXPORT_SYMBOL_GPL(find_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700378
379/**
380 * __free_iova - frees the given iova
381 * @iovad: iova domain in question.
382 * @iova: iova in question.
383 * Frees the given iova belonging to the giving domain
384 */
385void
386__free_iova(struct iova_domain *iovad, struct iova *iova)
387{
388 unsigned long flags;
389
390 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300391 private_free_iova(iovad, iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700392 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700393}
Sakari Ailus9b417602015-07-13 14:31:29 +0300394EXPORT_SYMBOL_GPL(__free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700395
396/**
397 * free_iova - finds and frees the iova for a given pfn
398 * @iovad: - iova domain in question.
399 * @pfn: - pfn that is allocated previously
400 * This functions finds an iova for a given pfn and then
401 * frees the iova from that domain.
402 */
403void
404free_iova(struct iova_domain *iovad, unsigned long pfn)
405{
406 struct iova *iova = find_iova(iovad, pfn);
Robert Callicotte733cac22015-04-16 23:32:47 -0500407
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700408 if (iova)
409 __free_iova(iovad, iova);
410
411}
Sakari Ailus9b417602015-07-13 14:31:29 +0300412EXPORT_SYMBOL_GPL(free_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700413
414/**
Omer Peleg9257b4a2016-04-20 11:34:11 +0300415 * alloc_iova_fast - allocates an iova from rcache
416 * @iovad: - iova domain in question
417 * @size: - size of page frames to allocate
418 * @limit_pfn: - max limit address
419 * This function tries to satisfy an iova allocation from the rcache,
420 * and falls back to regular allocation on failure.
421*/
422unsigned long
423alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
424 unsigned long limit_pfn)
425{
426 bool flushed_rcache = false;
427 unsigned long iova_pfn;
428 struct iova *new_iova;
429
430 iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
431 if (iova_pfn)
432 return iova_pfn;
433
434retry:
435 new_iova = alloc_iova(iovad, size, limit_pfn, true);
436 if (!new_iova) {
437 unsigned int cpu;
438
439 if (flushed_rcache)
440 return 0;
441
442 /* Try replenishing IOVAs by flushing rcache. */
443 flushed_rcache = true;
444 for_each_online_cpu(cpu)
445 free_cpu_cached_iovas(cpu, iovad);
446 goto retry;
447 }
448
449 return new_iova->pfn_lo;
450}
451EXPORT_SYMBOL_GPL(alloc_iova_fast);
452
453/**
454 * free_iova_fast - free iova pfn range into rcache
455 * @iovad: - iova domain in question.
456 * @pfn: - pfn that is allocated previously
457 * @size: - # of pages in range
458 * This functions frees an iova range by trying to put it into the rcache,
459 * falling back to regular iova deallocation via free_iova() if this fails.
460 */
461void
462free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
463{
464 if (iova_rcache_insert(iovad, pfn, size))
465 return;
466
467 free_iova(iovad, pfn);
468}
469EXPORT_SYMBOL_GPL(free_iova_fast);
470
Joerg Roedel19282102017-08-10 15:49:44 +0200471#define fq_ring_for_each(i, fq) \
472 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
473
474static inline bool fq_full(struct iova_fq *fq)
475{
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200476 assert_spin_locked(&fq->lock);
Joerg Roedel19282102017-08-10 15:49:44 +0200477 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
478}
479
480static inline unsigned fq_ring_add(struct iova_fq *fq)
481{
482 unsigned idx = fq->tail;
483
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200484 assert_spin_locked(&fq->lock);
485
Joerg Roedel19282102017-08-10 15:49:44 +0200486 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
487
488 return idx;
489}
490
491static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
492{
Joerg Roedelfb418da2017-08-10 16:14:59 +0200493 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200494 unsigned idx;
495
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200496 assert_spin_locked(&fq->lock);
497
Joerg Roedel19282102017-08-10 15:49:44 +0200498 fq_ring_for_each(idx, fq) {
499
Joerg Roedelfb418da2017-08-10 16:14:59 +0200500 if (fq->entries[idx].counter >= counter)
501 break;
502
Joerg Roedel19282102017-08-10 15:49:44 +0200503 if (iovad->entry_dtor)
504 iovad->entry_dtor(fq->entries[idx].data);
505
506 free_iova_fast(iovad,
507 fq->entries[idx].iova_pfn,
508 fq->entries[idx].pages);
Joerg Roedel19282102017-08-10 15:49:44 +0200509
Joerg Roedelfb418da2017-08-10 16:14:59 +0200510 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
511 }
512}
513
514static void iova_domain_flush(struct iova_domain *iovad)
515{
516 atomic64_inc(&iovad->fq_flush_start_cnt);
517 iovad->flush_cb(iovad);
518 atomic64_inc(&iovad->fq_flush_finish_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200519}
520
521static void fq_destroy_all_entries(struct iova_domain *iovad)
522{
523 int cpu;
524
525 /*
526 * This code runs when the iova_domain is being detroyed, so don't
527 * bother to free iovas, just call the entry_dtor on all remaining
528 * entries.
529 */
530 if (!iovad->entry_dtor)
531 return;
532
533 for_each_possible_cpu(cpu) {
534 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
535 int idx;
536
537 fq_ring_for_each(idx, fq)
538 iovad->entry_dtor(fq->entries[idx].data);
539 }
540}
541
542void queue_iova(struct iova_domain *iovad,
543 unsigned long pfn, unsigned long pages,
544 unsigned long data)
545{
546 struct iova_fq *fq = get_cpu_ptr(iovad->fq);
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200547 unsigned long flags;
Joerg Roedel19282102017-08-10 15:49:44 +0200548 unsigned idx;
549
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200550 spin_lock_irqsave(&fq->lock, flags);
551
Joerg Roedelfb418da2017-08-10 16:14:59 +0200552 /*
553 * First remove all entries from the flush queue that have already been
554 * flushed out on another CPU. This makes the fq_full() check below less
555 * likely to be true.
556 */
557 fq_ring_free(iovad, fq);
558
Joerg Roedel19282102017-08-10 15:49:44 +0200559 if (fq_full(fq)) {
Joerg Roedelfb418da2017-08-10 16:14:59 +0200560 iova_domain_flush(iovad);
Joerg Roedel19282102017-08-10 15:49:44 +0200561 fq_ring_free(iovad, fq);
562 }
563
564 idx = fq_ring_add(fq);
565
566 fq->entries[idx].iova_pfn = pfn;
567 fq->entries[idx].pages = pages;
568 fq->entries[idx].data = data;
Joerg Roedelfb418da2017-08-10 16:14:59 +0200569 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
Joerg Roedel19282102017-08-10 15:49:44 +0200570
Joerg Roedel8109c2a2017-08-10 16:31:17 +0200571 spin_unlock_irqrestore(&fq->lock, flags);
Joerg Roedel19282102017-08-10 15:49:44 +0200572 put_cpu_ptr(iovad->fq);
573}
574EXPORT_SYMBOL_GPL(queue_iova);
575
Omer Peleg9257b4a2016-04-20 11:34:11 +0300576/**
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700577 * put_iova_domain - destroys the iova doamin
578 * @iovad: - iova domain in question.
579 * All the iova's in that domain are destroyed.
580 */
581void put_iova_domain(struct iova_domain *iovad)
582{
583 struct rb_node *node;
584 unsigned long flags;
585
Joerg Roedel42f87e72017-08-10 14:44:28 +0200586 free_iova_flush_queue(iovad);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300587 free_iova_rcaches(iovad);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700588 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
589 node = rb_first(&iovad->rbroot);
590 while (node) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800591 struct iova *iova = rb_entry(node, struct iova, node);
Robert Callicotte733cac22015-04-16 23:32:47 -0500592
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700593 rb_erase(node, &iovad->rbroot);
594 free_iova_mem(iova);
595 node = rb_first(&iovad->rbroot);
596 }
597 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
598}
Sakari Ailus9b417602015-07-13 14:31:29 +0300599EXPORT_SYMBOL_GPL(put_iova_domain);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700600
601static int
602__is_range_overlap(struct rb_node *node,
603 unsigned long pfn_lo, unsigned long pfn_hi)
604{
Geliang Tangeba484b2016-12-19 22:46:58 +0800605 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700606
607 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
608 return 1;
609 return 0;
610}
611
Jiang Liu75f05562014-02-19 14:07:37 +0800612static inline struct iova *
613alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
614{
615 struct iova *iova;
616
617 iova = alloc_iova_mem();
618 if (iova) {
619 iova->pfn_lo = pfn_lo;
620 iova->pfn_hi = pfn_hi;
621 }
622
623 return iova;
624}
625
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700626static struct iova *
627__insert_new_range(struct iova_domain *iovad,
628 unsigned long pfn_lo, unsigned long pfn_hi)
629{
630 struct iova *iova;
631
Jiang Liu75f05562014-02-19 14:07:37 +0800632 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
633 if (iova)
Marek Szyprowskid7517512017-02-24 12:13:37 +0100634 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700635
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700636 return iova;
637}
638
639static void
640__adjust_overlap_range(struct iova *iova,
641 unsigned long *pfn_lo, unsigned long *pfn_hi)
642{
643 if (*pfn_lo < iova->pfn_lo)
644 iova->pfn_lo = *pfn_lo;
645 if (*pfn_hi > iova->pfn_hi)
646 *pfn_lo = iova->pfn_hi + 1;
647}
648
649/**
650 * reserve_iova - reserves an iova in the given range
651 * @iovad: - iova domain pointer
652 * @pfn_lo: - lower page frame address
653 * @pfn_hi:- higher pfn adderss
654 * This function allocates reserves the address range from pfn_lo to pfn_hi so
655 * that this address is not dished out as part of alloc_iova.
656 */
657struct iova *
658reserve_iova(struct iova_domain *iovad,
659 unsigned long pfn_lo, unsigned long pfn_hi)
660{
661 struct rb_node *node;
662 unsigned long flags;
663 struct iova *iova;
664 unsigned int overlap = 0;
665
David Woodhouse3d39cec2009-07-08 15:23:30 +0100666 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700667 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
668 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800669 iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700670 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
671 if ((pfn_lo >= iova->pfn_lo) &&
672 (pfn_hi <= iova->pfn_hi))
673 goto finish;
674 overlap = 1;
675
676 } else if (overlap)
677 break;
678 }
679
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300680 /* We are here either because this is the first reserver node
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700681 * or need to insert remaining non overlap addr range
682 */
683 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
684finish:
685
David Woodhouse3d39cec2009-07-08 15:23:30 +0100686 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700687 return iova;
688}
Sakari Ailus9b417602015-07-13 14:31:29 +0300689EXPORT_SYMBOL_GPL(reserve_iova);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700690
691/**
692 * copy_reserved_iova - copies the reserved between domains
693 * @from: - source doamin from where to copy
694 * @to: - destination domin where to copy
695 * This function copies reserved iova's from one doamin to
696 * other.
697 */
698void
699copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
700{
701 unsigned long flags;
702 struct rb_node *node;
703
David Woodhouse3d39cec2009-07-08 15:23:30 +0100704 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700705 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
Geliang Tangeba484b2016-12-19 22:46:58 +0800706 struct iova *iova = rb_entry(node, struct iova, node);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700707 struct iova *new_iova;
Robert Callicotte733cac22015-04-16 23:32:47 -0500708
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700709 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
710 if (!new_iova)
711 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
712 iova->pfn_lo, iova->pfn_lo);
713 }
David Woodhouse3d39cec2009-07-08 15:23:30 +0100714 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
Keshavamurthy, Anil Sf8de50e2007-10-21 16:41:48 -0700715}
Sakari Ailus9b417602015-07-13 14:31:29 +0300716EXPORT_SYMBOL_GPL(copy_reserved_iova);
Jiang Liu75f05562014-02-19 14:07:37 +0800717
718struct iova *
719split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
720 unsigned long pfn_lo, unsigned long pfn_hi)
721{
722 unsigned long flags;
723 struct iova *prev = NULL, *next = NULL;
724
725 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
726 if (iova->pfn_lo < pfn_lo) {
727 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
728 if (prev == NULL)
729 goto error;
730 }
731 if (iova->pfn_hi > pfn_hi) {
732 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
733 if (next == NULL)
734 goto error;
735 }
736
737 __cached_rbnode_delete_update(iovad, iova);
738 rb_erase(&iova->node, &iovad->rbroot);
739
740 if (prev) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100741 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800742 iova->pfn_lo = pfn_lo;
743 }
744 if (next) {
Marek Szyprowskid7517512017-02-24 12:13:37 +0100745 iova_insert_rbtree(&iovad->rbroot, next, NULL);
Jiang Liu75f05562014-02-19 14:07:37 +0800746 iova->pfn_hi = pfn_hi;
747 }
748 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
749
750 return iova;
751
752error:
753 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
754 if (prev)
755 free_iova_mem(prev);
756 return NULL;
757}
Sakari Ailus15bbdec2015-07-13 14:31:30 +0300758
Omer Peleg9257b4a2016-04-20 11:34:11 +0300759/*
760 * Magazine caches for IOVA ranges. For an introduction to magazines,
761 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
762 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
763 * For simplicity, we use a static magazine size and don't implement the
764 * dynamic size tuning described in the paper.
765 */
766
767#define IOVA_MAG_SIZE 128
768
769struct iova_magazine {
770 unsigned long size;
771 unsigned long pfns[IOVA_MAG_SIZE];
772};
773
774struct iova_cpu_rcache {
775 spinlock_t lock;
776 struct iova_magazine *loaded;
777 struct iova_magazine *prev;
778};
779
780static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
781{
782 return kzalloc(sizeof(struct iova_magazine), flags);
783}
784
785static void iova_magazine_free(struct iova_magazine *mag)
786{
787 kfree(mag);
788}
789
790static void
791iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
792{
793 unsigned long flags;
794 int i;
795
796 if (!mag)
797 return;
798
799 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
800
801 for (i = 0 ; i < mag->size; ++i) {
802 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
803
804 BUG_ON(!iova);
805 private_free_iova(iovad, iova);
806 }
807
808 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
809
810 mag->size = 0;
811}
812
813static bool iova_magazine_full(struct iova_magazine *mag)
814{
815 return (mag && mag->size == IOVA_MAG_SIZE);
816}
817
818static bool iova_magazine_empty(struct iova_magazine *mag)
819{
820 return (!mag || mag->size == 0);
821}
822
823static unsigned long iova_magazine_pop(struct iova_magazine *mag,
824 unsigned long limit_pfn)
825{
826 BUG_ON(iova_magazine_empty(mag));
827
828 if (mag->pfns[mag->size - 1] >= limit_pfn)
829 return 0;
830
831 return mag->pfns[--mag->size];
832}
833
834static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
835{
836 BUG_ON(iova_magazine_full(mag));
837
838 mag->pfns[mag->size++] = pfn;
839}
840
841static void init_iova_rcaches(struct iova_domain *iovad)
842{
843 struct iova_cpu_rcache *cpu_rcache;
844 struct iova_rcache *rcache;
845 unsigned int cpu;
846 int i;
847
848 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
849 rcache = &iovad->rcaches[i];
850 spin_lock_init(&rcache->lock);
851 rcache->depot_size = 0;
852 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
853 if (WARN_ON(!rcache->cpu_rcaches))
854 continue;
855 for_each_possible_cpu(cpu) {
856 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
857 spin_lock_init(&cpu_rcache->lock);
858 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
859 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
860 }
861 }
862}
863
864/*
865 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
866 * return true on success. Can fail if rcache is full and we can't free
867 * space, and free_iova() (our only caller) will then return the IOVA
868 * range to the rbtree instead.
869 */
870static bool __iova_rcache_insert(struct iova_domain *iovad,
871 struct iova_rcache *rcache,
872 unsigned long iova_pfn)
873{
874 struct iova_magazine *mag_to_free = NULL;
875 struct iova_cpu_rcache *cpu_rcache;
876 bool can_insert = false;
877 unsigned long flags;
878
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +0200879 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300880 spin_lock_irqsave(&cpu_rcache->lock, flags);
881
882 if (!iova_magazine_full(cpu_rcache->loaded)) {
883 can_insert = true;
884 } else if (!iova_magazine_full(cpu_rcache->prev)) {
885 swap(cpu_rcache->prev, cpu_rcache->loaded);
886 can_insert = true;
887 } else {
888 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
889
890 if (new_mag) {
891 spin_lock(&rcache->lock);
892 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
893 rcache->depot[rcache->depot_size++] =
894 cpu_rcache->loaded;
895 } else {
896 mag_to_free = cpu_rcache->loaded;
897 }
898 spin_unlock(&rcache->lock);
899
900 cpu_rcache->loaded = new_mag;
901 can_insert = true;
902 }
903 }
904
905 if (can_insert)
906 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
907
908 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
909
910 if (mag_to_free) {
911 iova_magazine_free_pfns(mag_to_free, iovad);
912 iova_magazine_free(mag_to_free);
913 }
914
915 return can_insert;
916}
917
918static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
919 unsigned long size)
920{
921 unsigned int log_size = order_base_2(size);
922
923 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
924 return false;
925
926 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
927}
928
929/*
930 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
931 * satisfy the request, return a matching non-NULL range and remove
932 * it from the 'rcache'.
933 */
934static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
935 unsigned long limit_pfn)
936{
937 struct iova_cpu_rcache *cpu_rcache;
938 unsigned long iova_pfn = 0;
939 bool has_pfn = false;
940 unsigned long flags;
941
Sebastian Andrzej Siewioraaffaa82017-06-27 18:16:47 +0200942 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
Omer Peleg9257b4a2016-04-20 11:34:11 +0300943 spin_lock_irqsave(&cpu_rcache->lock, flags);
944
945 if (!iova_magazine_empty(cpu_rcache->loaded)) {
946 has_pfn = true;
947 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
948 swap(cpu_rcache->prev, cpu_rcache->loaded);
949 has_pfn = true;
950 } else {
951 spin_lock(&rcache->lock);
952 if (rcache->depot_size > 0) {
953 iova_magazine_free(cpu_rcache->loaded);
954 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
955 has_pfn = true;
956 }
957 spin_unlock(&rcache->lock);
958 }
959
960 if (has_pfn)
961 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
962
963 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
964
965 return iova_pfn;
966}
967
968/*
969 * Try to satisfy IOVA allocation range from rcache. Fail if requested
970 * size is too big or the DMA limit we are given isn't satisfied by the
971 * top element in the magazine.
972 */
973static unsigned long iova_rcache_get(struct iova_domain *iovad,
974 unsigned long size,
975 unsigned long limit_pfn)
976{
977 unsigned int log_size = order_base_2(size);
978
979 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
980 return 0;
981
982 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
983}
984
985/*
986 * Free a cpu's rcache.
987 */
988static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
989 struct iova_rcache *rcache)
990{
991 struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
992 unsigned long flags;
993
994 spin_lock_irqsave(&cpu_rcache->lock, flags);
995
996 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
997 iova_magazine_free(cpu_rcache->loaded);
998
999 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1000 iova_magazine_free(cpu_rcache->prev);
1001
1002 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1003}
1004
1005/*
1006 * free rcache data structures.
1007 */
1008static void free_iova_rcaches(struct iova_domain *iovad)
1009{
1010 struct iova_rcache *rcache;
1011 unsigned long flags;
1012 unsigned int cpu;
1013 int i, j;
1014
1015 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1016 rcache = &iovad->rcaches[i];
1017 for_each_possible_cpu(cpu)
1018 free_cpu_iova_rcache(cpu, iovad, rcache);
1019 spin_lock_irqsave(&rcache->lock, flags);
1020 free_percpu(rcache->cpu_rcaches);
1021 for (j = 0; j < rcache->depot_size; ++j) {
1022 iova_magazine_free_pfns(rcache->depot[j], iovad);
1023 iova_magazine_free(rcache->depot[j]);
1024 }
1025 spin_unlock_irqrestore(&rcache->lock, flags);
1026 }
1027}
1028
1029/*
1030 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1031 */
1032void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1033{
1034 struct iova_cpu_rcache *cpu_rcache;
1035 struct iova_rcache *rcache;
1036 unsigned long flags;
1037 int i;
1038
1039 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1040 rcache = &iovad->rcaches[i];
1041 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1042 spin_lock_irqsave(&cpu_rcache->lock, flags);
1043 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1044 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1045 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1046 }
1047}
1048
Sakari Ailus15bbdec2015-07-13 14:31:30 +03001049MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1050MODULE_LICENSE("GPL");