Merge branch 'for-linus/pstore' into for-next/pstore
[linux-2.6-block.git] / drivers / iommu / iova.c
CommitLineData
f8de50eb 1/*
a15a519e 2 * Copyright © 2006-2009, Intel Corporation.
f8de50eb 3 *
a15a519e
DW
4 * 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.
7 *
8 * 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.
f8de50eb 16 *
98bcef56 17 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
f8de50eb
KA
18 */
19
38717946 20#include <linux/iova.h>
15bbdec3 21#include <linux/module.h>
85b45456 22#include <linux/slab.h>
9257b4a2
OP
23#include <linux/smp.h>
24#include <linux/bitops.h>
aaffaa8a 25#include <linux/cpu.h>
9257b4a2 26
bb68b2fb
RM
27/* The anchor node sits above the top of the usable address space */
28#define IOVA_ANCHOR ~0UL
29
9257b4a2
OP
30static bool iova_rcache_insert(struct iova_domain *iovad,
31 unsigned long pfn,
32 unsigned long size);
33static unsigned long iova_rcache_get(struct iova_domain *iovad,
34 unsigned long size,
35 unsigned long limit_pfn);
36static void init_iova_rcaches(struct iova_domain *iovad);
37static void free_iova_rcaches(struct iova_domain *iovad);
19282101 38static void fq_destroy_all_entries(struct iova_domain *iovad);
e99e88a9 39static void fq_flush_timeout(struct timer_list *t);
85b45456 40
f8de50eb 41void
0fb5fe87 42init_iova_domain(struct iova_domain *iovad, unsigned long granule,
aa3ac946 43 unsigned long start_pfn)
f8de50eb 44{
0fb5fe87
RM
45 /*
46 * IOVA granularity will normally be equal to the smallest
47 * supported IOMMU page size; both *must* be capable of
48 * representing individual CPU pages exactly.
49 */
50 BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
51
f8de50eb
KA
52 spin_lock_init(&iovad->iova_rbtree_lock);
53 iovad->rbroot = RB_ROOT;
973f5fbe
RM
54 iovad->cached_node = &iovad->anchor.node;
55 iovad->cached32_node = &iovad->anchor.node;
0fb5fe87 56 iovad->granule = granule;
1b722500 57 iovad->start_pfn = start_pfn;
aa3ac946 58 iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
bee60e94 59 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
42f87e71
JR
60 iovad->flush_cb = NULL;
61 iovad->fq = NULL;
bb68b2fb
RM
62 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
63 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
64 rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
9257b4a2 65 init_iova_rcaches(iovad);
f8de50eb 66}
9b41760b 67EXPORT_SYMBOL_GPL(init_iova_domain);
f8de50eb 68
42f87e71
JR
69static void free_iova_flush_queue(struct iova_domain *iovad)
70{
71 if (!iovad->fq)
72 return;
73
9a005a80
JR
74 if (timer_pending(&iovad->fq_timer))
75 del_timer(&iovad->fq_timer);
76
19282101 77 fq_destroy_all_entries(iovad);
9a005a80 78
42f87e71
JR
79 free_percpu(iovad->fq);
80
81 iovad->fq = NULL;
82 iovad->flush_cb = NULL;
83 iovad->entry_dtor = NULL;
84}
85
86int init_iova_flush_queue(struct iova_domain *iovad,
87 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
88{
89 int cpu;
90
fb418dab
JR
91 atomic64_set(&iovad->fq_flush_start_cnt, 0);
92 atomic64_set(&iovad->fq_flush_finish_cnt, 0);
93
42f87e71
JR
94 iovad->fq = alloc_percpu(struct iova_fq);
95 if (!iovad->fq)
96 return -ENOMEM;
97
98 iovad->flush_cb = flush_cb;
99 iovad->entry_dtor = entry_dtor;
100
101 for_each_possible_cpu(cpu) {
102 struct iova_fq *fq;
103
104 fq = per_cpu_ptr(iovad->fq, cpu);
105 fq->head = 0;
106 fq->tail = 0;
8109c2a2
JR
107
108 spin_lock_init(&fq->lock);
42f87e71
JR
109 }
110
e99e88a9 111 timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
9a005a80
JR
112 atomic_set(&iovad->fq_timer_on, 0);
113
42f87e71
JR
114 return 0;
115}
116EXPORT_SYMBOL_GPL(init_iova_flush_queue);
117
f8de50eb 118static struct rb_node *
973f5fbe 119__get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
f8de50eb 120{
973f5fbe
RM
121 if (limit_pfn <= iovad->dma_32bit_pfn)
122 return iovad->cached32_node;
e60aa7b5 123
973f5fbe 124 return iovad->cached_node;
f8de50eb
KA
125}
126
127static void
e60aa7b5 128__cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
f8de50eb 129{
e60aa7b5
RM
130 if (new->pfn_hi < iovad->dma_32bit_pfn)
131 iovad->cached32_node = &new->node;
132 else
133 iovad->cached_node = &new->node;
f8de50eb
KA
134}
135
136static void
137__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
138{
139 struct iova *cached_iova;
f8de50eb 140
e60aa7b5
RM
141 cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
142 if (free->pfn_hi < iovad->dma_32bit_pfn &&
bee60e94 143 free->pfn_lo >= cached_iova->pfn_lo) {
e60aa7b5 144 iovad->cached32_node = rb_next(&free->node);
bee60e94
GK
145 iovad->max32_alloc_size = iovad->dma_32bit_pfn;
146 }
e60aa7b5
RM
147
148 cached_iova = rb_entry(iovad->cached_node, struct iova, node);
973f5fbe 149 if (free->pfn_lo >= cached_iova->pfn_lo)
e60aa7b5 150 iovad->cached_node = rb_next(&free->node);
f8de50eb
KA
151}
152
d751751a
MS
153/* Insert the iova into domain rbtree by holding writer lock */
154static void
155iova_insert_rbtree(struct rb_root *root, struct iova *iova,
156 struct rb_node *start)
157{
158 struct rb_node **new, *parent = NULL;
159
160 new = (start) ? &start : &(root->rb_node);
161 /* Figure out where to put new node */
162 while (*new) {
163 struct iova *this = rb_entry(*new, struct iova, node);
164
165 parent = *new;
166
167 if (iova->pfn_lo < this->pfn_lo)
168 new = &((*new)->rb_left);
169 else if (iova->pfn_lo > this->pfn_lo)
170 new = &((*new)->rb_right);
171 else {
172 WARN_ON(1); /* this should not happen */
173 return;
174 }
175 }
176 /* Add new node and rebalance tree. */
177 rb_link_node(&iova->node, parent, new);
178 rb_insert_color(&iova->node, root);
179}
180
ddf02886 181static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
182 unsigned long size, unsigned long limit_pfn,
183 struct iova *new, bool size_aligned)
f8de50eb 184{
973f5fbe
RM
185 struct rb_node *curr, *prev;
186 struct iova *curr_iova;
f8de50eb 187 unsigned long flags;
e60aa7b5 188 unsigned long new_pfn;
086c83ac
ZL
189 unsigned long align_mask = ~0UL;
190
191 if (size_aligned)
192 align_mask <<= fls_long(size - 1);
f8de50eb
KA
193
194 /* Walk the tree backwards */
195 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
bee60e94
GK
196 if (limit_pfn <= iovad->dma_32bit_pfn &&
197 size >= iovad->max32_alloc_size)
198 goto iova32_full;
199
973f5fbe
RM
200 curr = __get_cached_rbnode(iovad, limit_pfn);
201 curr_iova = rb_entry(curr, struct iova, node);
202 do {
203 limit_pfn = min(limit_pfn, curr_iova->pfn_lo);
204 new_pfn = (limit_pfn - size) & align_mask;
ddf02886 205 prev = curr;
f8de50eb 206 curr = rb_prev(curr);
973f5fbe
RM
207 curr_iova = rb_entry(curr, struct iova, node);
208 } while (curr && new_pfn <= curr_iova->pfn_hi);
f8de50eb 209
bee60e94
GK
210 if (limit_pfn < size || new_pfn < iovad->start_pfn)
211 goto iova32_full;
f76aec76
KA
212
213 /* pfn_lo will point to size aligned address if size_aligned is set */
086c83ac 214 new->pfn_lo = new_pfn;
f76aec76 215 new->pfn_hi = new->pfn_lo + size - 1;
f8de50eb 216
d751751a
MS
217 /* If we have 'prev', it's a valid place to start the insertion. */
218 iova_insert_rbtree(&iovad->rbroot, new, prev);
e60aa7b5 219 __cached_rbnode_insert_update(iovad, new);
ddf02886 220
f8de50eb
KA
221 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
222 return 0;
bee60e94
GK
223
224iova32_full:
225 iovad->max32_alloc_size = size;
226 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
227 return -ENOMEM;
f8de50eb
KA
228}
229
ae1ff3d6
SA
230static struct kmem_cache *iova_cache;
231static unsigned int iova_cache_users;
232static DEFINE_MUTEX(iova_cache_mutex);
233
234struct iova *alloc_iova_mem(void)
235{
236 return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
237}
238EXPORT_SYMBOL(alloc_iova_mem);
239
240void free_iova_mem(struct iova *iova)
241{
bb68b2fb
RM
242 if (iova->pfn_lo != IOVA_ANCHOR)
243 kmem_cache_free(iova_cache, iova);
ae1ff3d6
SA
244}
245EXPORT_SYMBOL(free_iova_mem);
246
247int iova_cache_get(void)
248{
249 mutex_lock(&iova_cache_mutex);
250 if (!iova_cache_users) {
251 iova_cache = kmem_cache_create(
252 "iommu_iova", sizeof(struct iova), 0,
253 SLAB_HWCACHE_ALIGN, NULL);
254 if (!iova_cache) {
255 mutex_unlock(&iova_cache_mutex);
256 printk(KERN_ERR "Couldn't create iova cache\n");
257 return -ENOMEM;
258 }
259 }
260
261 iova_cache_users++;
262 mutex_unlock(&iova_cache_mutex);
263
264 return 0;
265}
9b41760b 266EXPORT_SYMBOL_GPL(iova_cache_get);
ae1ff3d6
SA
267
268void iova_cache_put(void)
269{
270 mutex_lock(&iova_cache_mutex);
271 if (WARN_ON(!iova_cache_users)) {
272 mutex_unlock(&iova_cache_mutex);
273 return;
274 }
275 iova_cache_users--;
276 if (!iova_cache_users)
277 kmem_cache_destroy(iova_cache);
278 mutex_unlock(&iova_cache_mutex);
279}
9b41760b 280EXPORT_SYMBOL_GPL(iova_cache_put);
ae1ff3d6 281
f8de50eb
KA
282/**
283 * alloc_iova - allocates an iova
07db0409
MI
284 * @iovad: - iova domain in question
285 * @size: - size of page frames to allocate
286 * @limit_pfn: - max limit address
287 * @size_aligned: - set if size_aligned address range is required
1b722500
RM
288 * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
289 * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
f76aec76
KA
290 * flag is set then the allocated address iova->pfn_lo will be naturally
291 * aligned on roundup_power_of_two(size).
f8de50eb
KA
292 */
293struct iova *
294alloc_iova(struct iova_domain *iovad, unsigned long size,
f76aec76
KA
295 unsigned long limit_pfn,
296 bool size_aligned)
f8de50eb 297{
f8de50eb
KA
298 struct iova *new_iova;
299 int ret;
300
301 new_iova = alloc_iova_mem();
302 if (!new_iova)
303 return NULL;
304
757c370f 305 ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
ddf02886 306 new_iova, size_aligned);
f8de50eb
KA
307
308 if (ret) {
f8de50eb
KA
309 free_iova_mem(new_iova);
310 return NULL;
311 }
312
f8de50eb
KA
313 return new_iova;
314}
9b41760b 315EXPORT_SYMBOL_GPL(alloc_iova);
f8de50eb 316
9257b4a2
OP
317static struct iova *
318private_find_iova(struct iova_domain *iovad, unsigned long pfn)
f8de50eb 319{
9257b4a2
OP
320 struct rb_node *node = iovad->rbroot.rb_node;
321
322 assert_spin_locked(&iovad->iova_rbtree_lock);
f8de50eb 323
f8de50eb 324 while (node) {
eba484b5 325 struct iova *iova = rb_entry(node, struct iova, node);
f8de50eb 326
f8de50eb
KA
327 if (pfn < iova->pfn_lo)
328 node = node->rb_left;
2070f940 329 else if (pfn > iova->pfn_hi)
f8de50eb 330 node = node->rb_right;
2070f940
ZL
331 else
332 return iova; /* pfn falls within iova's range */
f8de50eb
KA
333 }
334
f8de50eb
KA
335 return NULL;
336}
9257b4a2
OP
337
338static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
339{
340 assert_spin_locked(&iovad->iova_rbtree_lock);
341 __cached_rbnode_delete_update(iovad, iova);
342 rb_erase(&iova->node, &iovad->rbroot);
343 free_iova_mem(iova);
344}
345
346/**
347 * find_iova - finds an iova for a given pfn
348 * @iovad: - iova domain in question.
349 * @pfn: - page frame number
350 * This function finds and returns an iova belonging to the
351 * given doamin which matches the given pfn.
352 */
353struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
354{
355 unsigned long flags;
356 struct iova *iova;
357
358 /* Take the lock so that no other thread is manipulating the rbtree */
359 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
360 iova = private_find_iova(iovad, pfn);
361 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
362 return iova;
363}
9b41760b 364EXPORT_SYMBOL_GPL(find_iova);
f8de50eb
KA
365
366/**
367 * __free_iova - frees the given iova
368 * @iovad: iova domain in question.
369 * @iova: iova in question.
370 * Frees the given iova belonging to the giving domain
371 */
372void
373__free_iova(struct iova_domain *iovad, struct iova *iova)
374{
375 unsigned long flags;
376
377 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
9257b4a2 378 private_free_iova(iovad, iova);
f8de50eb 379 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
f8de50eb 380}
9b41760b 381EXPORT_SYMBOL_GPL(__free_iova);
f8de50eb
KA
382
383/**
384 * free_iova - finds and frees the iova for a given pfn
385 * @iovad: - iova domain in question.
386 * @pfn: - pfn that is allocated previously
387 * This functions finds an iova for a given pfn and then
388 * frees the iova from that domain.
389 */
390void
391free_iova(struct iova_domain *iovad, unsigned long pfn)
392{
393 struct iova *iova = find_iova(iovad, pfn);
733cac2a 394
f8de50eb
KA
395 if (iova)
396 __free_iova(iovad, iova);
397
398}
9b41760b 399EXPORT_SYMBOL_GPL(free_iova);
f8de50eb 400
9257b4a2
OP
401/**
402 * alloc_iova_fast - allocates an iova from rcache
403 * @iovad: - iova domain in question
404 * @size: - size of page frames to allocate
405 * @limit_pfn: - max limit address
538d5b33 406 * @flush_rcache: - set to flush rcache on regular allocation failure
9257b4a2 407 * This function tries to satisfy an iova allocation from the rcache,
538d5b33
TN
408 * and falls back to regular allocation on failure. If regular allocation
409 * fails too and the flush_rcache flag is set then the rcache will be flushed.
9257b4a2
OP
410*/
411unsigned long
412alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
538d5b33 413 unsigned long limit_pfn, bool flush_rcache)
9257b4a2 414{
9257b4a2
OP
415 unsigned long iova_pfn;
416 struct iova *new_iova;
417
b826ee9a 418 iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
9257b4a2
OP
419 if (iova_pfn)
420 return iova_pfn;
421
422retry:
423 new_iova = alloc_iova(iovad, size, limit_pfn, true);
424 if (!new_iova) {
425 unsigned int cpu;
426
538d5b33 427 if (!flush_rcache)
9257b4a2
OP
428 return 0;
429
430 /* Try replenishing IOVAs by flushing rcache. */
538d5b33 431 flush_rcache = false;
9257b4a2
OP
432 for_each_online_cpu(cpu)
433 free_cpu_cached_iovas(cpu, iovad);
434 goto retry;
435 }
436
437 return new_iova->pfn_lo;
438}
439EXPORT_SYMBOL_GPL(alloc_iova_fast);
440
441/**
442 * free_iova_fast - free iova pfn range into rcache
443 * @iovad: - iova domain in question.
444 * @pfn: - pfn that is allocated previously
445 * @size: - # of pages in range
446 * This functions frees an iova range by trying to put it into the rcache,
447 * falling back to regular iova deallocation via free_iova() if this fails.
448 */
449void
450free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
451{
452 if (iova_rcache_insert(iovad, pfn, size))
453 return;
454
455 free_iova(iovad, pfn);
456}
457EXPORT_SYMBOL_GPL(free_iova_fast);
458
19282101
JR
459#define fq_ring_for_each(i, fq) \
460 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
461
462static inline bool fq_full(struct iova_fq *fq)
463{
8109c2a2 464 assert_spin_locked(&fq->lock);
19282101
JR
465 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
466}
467
468static inline unsigned fq_ring_add(struct iova_fq *fq)
469{
470 unsigned idx = fq->tail;
471
8109c2a2
JR
472 assert_spin_locked(&fq->lock);
473
19282101
JR
474 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
475
476 return idx;
477}
478
479static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
480{
fb418dab 481 u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
19282101
JR
482 unsigned idx;
483
8109c2a2
JR
484 assert_spin_locked(&fq->lock);
485
19282101
JR
486 fq_ring_for_each(idx, fq) {
487
fb418dab
JR
488 if (fq->entries[idx].counter >= counter)
489 break;
490
19282101
JR
491 if (iovad->entry_dtor)
492 iovad->entry_dtor(fq->entries[idx].data);
493
494 free_iova_fast(iovad,
495 fq->entries[idx].iova_pfn,
496 fq->entries[idx].pages);
fb418dab
JR
497
498 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
19282101 499 }
fb418dab 500}
19282101 501
fb418dab
JR
502static void iova_domain_flush(struct iova_domain *iovad)
503{
504 atomic64_inc(&iovad->fq_flush_start_cnt);
505 iovad->flush_cb(iovad);
506 atomic64_inc(&iovad->fq_flush_finish_cnt);
19282101
JR
507}
508
509static void fq_destroy_all_entries(struct iova_domain *iovad)
510{
511 int cpu;
512
513 /*
514 * This code runs when the iova_domain is being detroyed, so don't
515 * bother to free iovas, just call the entry_dtor on all remaining
516 * entries.
517 */
518 if (!iovad->entry_dtor)
519 return;
520
521 for_each_possible_cpu(cpu) {
522 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
523 int idx;
524
525 fq_ring_for_each(idx, fq)
526 iovad->entry_dtor(fq->entries[idx].data);
527 }
528}
529
e99e88a9 530static void fq_flush_timeout(struct timer_list *t)
9a005a80 531{
e99e88a9 532 struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
9a005a80
JR
533 int cpu;
534
535 atomic_set(&iovad->fq_timer_on, 0);
536 iova_domain_flush(iovad);
537
538 for_each_possible_cpu(cpu) {
539 unsigned long flags;
540 struct iova_fq *fq;
541
542 fq = per_cpu_ptr(iovad->fq, cpu);
543 spin_lock_irqsave(&fq->lock, flags);
544 fq_ring_free(iovad, fq);
545 spin_unlock_irqrestore(&fq->lock, flags);
546 }
547}
548
19282101
JR
549void queue_iova(struct iova_domain *iovad,
550 unsigned long pfn, unsigned long pages,
551 unsigned long data)
552{
94e2cc4d 553 struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
8109c2a2 554 unsigned long flags;
19282101
JR
555 unsigned idx;
556
8109c2a2
JR
557 spin_lock_irqsave(&fq->lock, flags);
558
fb418dab
JR
559 /*
560 * First remove all entries from the flush queue that have already been
561 * flushed out on another CPU. This makes the fq_full() check below less
562 * likely to be true.
563 */
564 fq_ring_free(iovad, fq);
565
19282101 566 if (fq_full(fq)) {
fb418dab 567 iova_domain_flush(iovad);
19282101
JR
568 fq_ring_free(iovad, fq);
569 }
570
571 idx = fq_ring_add(fq);
572
573 fq->entries[idx].iova_pfn = pfn;
574 fq->entries[idx].pages = pages;
575 fq->entries[idx].data = data;
fb418dab 576 fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
19282101 577
8109c2a2 578 spin_unlock_irqrestore(&fq->lock, flags);
9a005a80
JR
579
580 if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
581 mod_timer(&iovad->fq_timer,
582 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
19282101
JR
583}
584EXPORT_SYMBOL_GPL(queue_iova);
585
f8de50eb
KA
586/**
587 * put_iova_domain - destroys the iova doamin
588 * @iovad: - iova domain in question.
589 * All the iova's in that domain are destroyed.
590 */
591void put_iova_domain(struct iova_domain *iovad)
592{
7595dc58 593 struct iova *iova, *tmp;
f8de50eb 594
42f87e71 595 free_iova_flush_queue(iovad);
9257b4a2 596 free_iova_rcaches(iovad);
7595dc58 597 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
f8de50eb 598 free_iova_mem(iova);
f8de50eb 599}
9b41760b 600EXPORT_SYMBOL_GPL(put_iova_domain);
f8de50eb
KA
601
602static int
603__is_range_overlap(struct rb_node *node,
604 unsigned long pfn_lo, unsigned long pfn_hi)
605{
eba484b5 606 struct iova *iova = rb_entry(node, struct iova, node);
f8de50eb
KA
607
608 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
609 return 1;
610 return 0;
611}
612
75f05569
JL
613static inline struct iova *
614alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
615{
616 struct iova *iova;
617
618 iova = alloc_iova_mem();
619 if (iova) {
620 iova->pfn_lo = pfn_lo;
621 iova->pfn_hi = pfn_hi;
622 }
623
624 return iova;
625}
626
f8de50eb
KA
627static struct iova *
628__insert_new_range(struct iova_domain *iovad,
629 unsigned long pfn_lo, unsigned long pfn_hi)
630{
631 struct iova *iova;
632
75f05569
JL
633 iova = alloc_and_init_iova(pfn_lo, pfn_hi);
634 if (iova)
d751751a 635 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
f8de50eb 636
f8de50eb
KA
637 return iova;
638}
639
640static void
641__adjust_overlap_range(struct iova *iova,
642 unsigned long *pfn_lo, unsigned long *pfn_hi)
643{
644 if (*pfn_lo < iova->pfn_lo)
645 iova->pfn_lo = *pfn_lo;
646 if (*pfn_hi > iova->pfn_hi)
647 *pfn_lo = iova->pfn_hi + 1;
648}
649
650/**
651 * reserve_iova - reserves an iova in the given range
652 * @iovad: - iova domain pointer
653 * @pfn_lo: - lower page frame address
654 * @pfn_hi:- higher pfn adderss
655 * This function allocates reserves the address range from pfn_lo to pfn_hi so
656 * that this address is not dished out as part of alloc_iova.
657 */
658struct iova *
659reserve_iova(struct iova_domain *iovad,
660 unsigned long pfn_lo, unsigned long pfn_hi)
661{
662 struct rb_node *node;
663 unsigned long flags;
664 struct iova *iova;
665 unsigned int overlap = 0;
666
bb68b2fb
RM
667 /* Don't allow nonsensical pfns */
668 if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
669 return NULL;
670
3d39cecc 671 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
f8de50eb
KA
672 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
673 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
eba484b5 674 iova = rb_entry(node, struct iova, node);
f8de50eb
KA
675 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
676 if ((pfn_lo >= iova->pfn_lo) &&
677 (pfn_hi <= iova->pfn_hi))
678 goto finish;
679 overlap = 1;
680
681 } else if (overlap)
682 break;
683 }
684
25985edc 685 /* We are here either because this is the first reserver node
f8de50eb
KA
686 * or need to insert remaining non overlap addr range
687 */
688 iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
689finish:
690
3d39cecc 691 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
f8de50eb
KA
692 return iova;
693}
9b41760b 694EXPORT_SYMBOL_GPL(reserve_iova);
f8de50eb
KA
695
696/**
697 * copy_reserved_iova - copies the reserved between domains
698 * @from: - source doamin from where to copy
699 * @to: - destination domin where to copy
700 * This function copies reserved iova's from one doamin to
701 * other.
702 */
703void
704copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
705{
706 unsigned long flags;
707 struct rb_node *node;
708
3d39cecc 709 spin_lock_irqsave(&from->iova_rbtree_lock, flags);
f8de50eb 710 for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
eba484b5 711 struct iova *iova = rb_entry(node, struct iova, node);
f8de50eb 712 struct iova *new_iova;
733cac2a 713
abbb8a09
RM
714 if (iova->pfn_lo == IOVA_ANCHOR)
715 continue;
716
f8de50eb
KA
717 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
718 if (!new_iova)
719 printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
720 iova->pfn_lo, iova->pfn_lo);
721 }
3d39cecc 722 spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
f8de50eb 723}
9b41760b 724EXPORT_SYMBOL_GPL(copy_reserved_iova);
75f05569
JL
725
726struct iova *
727split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
728 unsigned long pfn_lo, unsigned long pfn_hi)
729{
730 unsigned long flags;
731 struct iova *prev = NULL, *next = NULL;
732
733 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
734 if (iova->pfn_lo < pfn_lo) {
735 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
736 if (prev == NULL)
737 goto error;
738 }
739 if (iova->pfn_hi > pfn_hi) {
740 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
741 if (next == NULL)
742 goto error;
743 }
744
745 __cached_rbnode_delete_update(iovad, iova);
746 rb_erase(&iova->node, &iovad->rbroot);
747
748 if (prev) {
d751751a 749 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
75f05569
JL
750 iova->pfn_lo = pfn_lo;
751 }
752 if (next) {
d751751a 753 iova_insert_rbtree(&iovad->rbroot, next, NULL);
75f05569
JL
754 iova->pfn_hi = pfn_hi;
755 }
756 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
757
758 return iova;
759
760error:
761 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
762 if (prev)
763 free_iova_mem(prev);
764 return NULL;
765}
15bbdec3 766
9257b4a2
OP
767/*
768 * Magazine caches for IOVA ranges. For an introduction to magazines,
769 * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
770 * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
771 * For simplicity, we use a static magazine size and don't implement the
772 * dynamic size tuning described in the paper.
773 */
774
775#define IOVA_MAG_SIZE 128
776
777struct iova_magazine {
778 unsigned long size;
779 unsigned long pfns[IOVA_MAG_SIZE];
780};
781
782struct iova_cpu_rcache {
783 spinlock_t lock;
784 struct iova_magazine *loaded;
785 struct iova_magazine *prev;
786};
787
788static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
789{
790 return kzalloc(sizeof(struct iova_magazine), flags);
791}
792
793static void iova_magazine_free(struct iova_magazine *mag)
794{
795 kfree(mag);
796}
797
798static void
799iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
800{
801 unsigned long flags;
802 int i;
803
804 if (!mag)
805 return;
806
807 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
808
809 for (i = 0 ; i < mag->size; ++i) {
810 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
811
812 BUG_ON(!iova);
813 private_free_iova(iovad, iova);
814 }
815
816 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
817
818 mag->size = 0;
819}
820
821static bool iova_magazine_full(struct iova_magazine *mag)
822{
823 return (mag && mag->size == IOVA_MAG_SIZE);
824}
825
826static bool iova_magazine_empty(struct iova_magazine *mag)
827{
828 return (!mag || mag->size == 0);
829}
830
831static unsigned long iova_magazine_pop(struct iova_magazine *mag,
832 unsigned long limit_pfn)
833{
e8b19840
RM
834 int i;
835 unsigned long pfn;
836
9257b4a2
OP
837 BUG_ON(iova_magazine_empty(mag));
838
e8b19840
RM
839 /* Only fall back to the rbtree if we have no suitable pfns at all */
840 for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
841 if (i == 0)
842 return 0;
843
844 /* Swap it to pop it */
845 pfn = mag->pfns[i];
846 mag->pfns[i] = mag->pfns[--mag->size];
9257b4a2 847
e8b19840 848 return pfn;
9257b4a2
OP
849}
850
851static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
852{
853 BUG_ON(iova_magazine_full(mag));
854
855 mag->pfns[mag->size++] = pfn;
856}
857
858static void init_iova_rcaches(struct iova_domain *iovad)
859{
860 struct iova_cpu_rcache *cpu_rcache;
861 struct iova_rcache *rcache;
862 unsigned int cpu;
863 int i;
864
865 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
866 rcache = &iovad->rcaches[i];
867 spin_lock_init(&rcache->lock);
868 rcache->depot_size = 0;
869 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
870 if (WARN_ON(!rcache->cpu_rcaches))
871 continue;
872 for_each_possible_cpu(cpu) {
873 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
874 spin_lock_init(&cpu_rcache->lock);
875 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
876 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
877 }
878 }
879}
880
881/*
882 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
883 * return true on success. Can fail if rcache is full and we can't free
884 * space, and free_iova() (our only caller) will then return the IOVA
885 * range to the rbtree instead.
886 */
887static bool __iova_rcache_insert(struct iova_domain *iovad,
888 struct iova_rcache *rcache,
889 unsigned long iova_pfn)
890{
891 struct iova_magazine *mag_to_free = NULL;
892 struct iova_cpu_rcache *cpu_rcache;
893 bool can_insert = false;
894 unsigned long flags;
895
aaffaa8a 896 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
9257b4a2
OP
897 spin_lock_irqsave(&cpu_rcache->lock, flags);
898
899 if (!iova_magazine_full(cpu_rcache->loaded)) {
900 can_insert = true;
901 } else if (!iova_magazine_full(cpu_rcache->prev)) {
902 swap(cpu_rcache->prev, cpu_rcache->loaded);
903 can_insert = true;
904 } else {
905 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
906
907 if (new_mag) {
908 spin_lock(&rcache->lock);
909 if (rcache->depot_size < MAX_GLOBAL_MAGS) {
910 rcache->depot[rcache->depot_size++] =
911 cpu_rcache->loaded;
912 } else {
913 mag_to_free = cpu_rcache->loaded;
914 }
915 spin_unlock(&rcache->lock);
916
917 cpu_rcache->loaded = new_mag;
918 can_insert = true;
919 }
920 }
921
922 if (can_insert)
923 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
924
925 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
926
927 if (mag_to_free) {
928 iova_magazine_free_pfns(mag_to_free, iovad);
929 iova_magazine_free(mag_to_free);
930 }
931
932 return can_insert;
933}
934
935static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
936 unsigned long size)
937{
938 unsigned int log_size = order_base_2(size);
939
940 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
941 return false;
942
943 return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
944}
945
946/*
947 * Caller wants to allocate a new IOVA range from 'rcache'. If we can
948 * satisfy the request, return a matching non-NULL range and remove
949 * it from the 'rcache'.
950 */
951static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
952 unsigned long limit_pfn)
953{
954 struct iova_cpu_rcache *cpu_rcache;
955 unsigned long iova_pfn = 0;
956 bool has_pfn = false;
957 unsigned long flags;
958
aaffaa8a 959 cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
9257b4a2
OP
960 spin_lock_irqsave(&cpu_rcache->lock, flags);
961
962 if (!iova_magazine_empty(cpu_rcache->loaded)) {
963 has_pfn = true;
964 } else if (!iova_magazine_empty(cpu_rcache->prev)) {
965 swap(cpu_rcache->prev, cpu_rcache->loaded);
966 has_pfn = true;
967 } else {
968 spin_lock(&rcache->lock);
969 if (rcache->depot_size > 0) {
970 iova_magazine_free(cpu_rcache->loaded);
971 cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
972 has_pfn = true;
973 }
974 spin_unlock(&rcache->lock);
975 }
976
977 if (has_pfn)
978 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
979
980 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
981
982 return iova_pfn;
983}
984
985/*
986 * Try to satisfy IOVA allocation range from rcache. Fail if requested
987 * size is too big or the DMA limit we are given isn't satisfied by the
988 * top element in the magazine.
989 */
990static unsigned long iova_rcache_get(struct iova_domain *iovad,
991 unsigned long size,
992 unsigned long limit_pfn)
993{
994 unsigned int log_size = order_base_2(size);
995
996 if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
997 return 0;
998
b826ee9a 999 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
9257b4a2
OP
1000}
1001
9257b4a2
OP
1002/*
1003 * free rcache data structures.
1004 */
1005static void free_iova_rcaches(struct iova_domain *iovad)
1006{
1007 struct iova_rcache *rcache;
7595dc58 1008 struct iova_cpu_rcache *cpu_rcache;
9257b4a2
OP
1009 unsigned int cpu;
1010 int i, j;
1011
1012 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1013 rcache = &iovad->rcaches[i];
7595dc58
RM
1014 for_each_possible_cpu(cpu) {
1015 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1016 iova_magazine_free(cpu_rcache->loaded);
1017 iova_magazine_free(cpu_rcache->prev);
1018 }
9257b4a2 1019 free_percpu(rcache->cpu_rcaches);
7595dc58 1020 for (j = 0; j < rcache->depot_size; ++j)
9257b4a2 1021 iova_magazine_free(rcache->depot[j]);
9257b4a2
OP
1022 }
1023}
1024
1025/*
1026 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1027 */
1028void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1029{
1030 struct iova_cpu_rcache *cpu_rcache;
1031 struct iova_rcache *rcache;
1032 unsigned long flags;
1033 int i;
1034
1035 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1036 rcache = &iovad->rcaches[i];
1037 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1038 spin_lock_irqsave(&cpu_rcache->lock, flags);
1039 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1040 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1041 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1042 }
1043}
1044
15bbdec3
SA
1045MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1046MODULE_LICENSE("GPL");