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