65032e60a5d1e1c071500b7c8249d34ed1b65cb2
[linux-2.6-microblaze.git] / drivers / iommu / iova.c
1 /*
2  * Copyright © 2006-2009, Intel Corporation.
3  *
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.
16  *
17  * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
18  */
19
20 #include <linux/iova.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <linux/bitops.h>
25 #include <linux/cpu.h>
26
27 static bool iova_rcache_insert(struct iova_domain *iovad,
28                                unsigned long pfn,
29                                unsigned long size);
30 static unsigned long iova_rcache_get(struct iova_domain *iovad,
31                                      unsigned long size,
32                                      unsigned long limit_pfn);
33 static void init_iova_rcaches(struct iova_domain *iovad);
34 static void free_iova_rcaches(struct iova_domain *iovad);
35 static void fq_destroy_all_entries(struct iova_domain *iovad);
36 static void fq_flush_timeout(unsigned long data);
37
38 void
39 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
40         unsigned long start_pfn)
41 {
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
49         spin_lock_init(&iovad->iova_rbtree_lock);
50         iovad->rbroot = RB_ROOT;
51         iovad->cached_node = NULL;
52         iovad->cached32_node = NULL;
53         iovad->granule = granule;
54         iovad->start_pfn = start_pfn;
55         iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
56         iovad->flush_cb = NULL;
57         iovad->fq = NULL;
58         init_iova_rcaches(iovad);
59 }
60 EXPORT_SYMBOL_GPL(init_iova_domain);
61
62 static void free_iova_flush_queue(struct iova_domain *iovad)
63 {
64         if (!iovad->fq)
65                 return;
66
67         if (timer_pending(&iovad->fq_timer))
68                 del_timer(&iovad->fq_timer);
69
70         fq_destroy_all_entries(iovad);
71
72         free_percpu(iovad->fq);
73
74         iovad->fq         = NULL;
75         iovad->flush_cb   = NULL;
76         iovad->entry_dtor = NULL;
77 }
78
79 int init_iova_flush_queue(struct iova_domain *iovad,
80                           iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
81 {
82         int cpu;
83
84         atomic64_set(&iovad->fq_flush_start_cnt,  0);
85         atomic64_set(&iovad->fq_flush_finish_cnt, 0);
86
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;
100
101                 spin_lock_init(&fq->lock);
102         }
103
104         setup_timer(&iovad->fq_timer, fq_flush_timeout, (unsigned long)iovad);
105         atomic_set(&iovad->fq_timer_on, 0);
106
107         return 0;
108 }
109 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
110
111 static struct rb_node *
112 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
113 {
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)
122                 return rb_last(&iovad->rbroot);
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);
128 }
129
130 static void
131 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
132 {
133         if (new->pfn_hi < iovad->dma_32bit_pfn)
134                 iovad->cached32_node = &new->node;
135         else
136                 iovad->cached_node = &new->node;
137 }
138
139 static void
140 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
141 {
142         struct iova *cached_iova;
143
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);
152 }
153
154 /* Insert the iova into domain rbtree by holding writer lock */
155 static void
156 iova_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
182 static 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)
185 {
186         struct rb_node *prev, *curr = NULL;
187         unsigned long flags;
188         unsigned long new_pfn;
189         unsigned long align_mask = ~0UL;
190
191         if (size_aligned)
192                 align_mask <<= fls_long(size - 1);
193
194         /* Walk the tree backwards */
195         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
196         curr = __get_cached_rbnode(iovad, &limit_pfn);
197         prev = curr;
198         while (curr) {
199                 struct iova *curr_iova = rb_entry(curr, struct iova, node);
200
201                 if (limit_pfn <= curr_iova->pfn_lo)
202                         goto move_left;
203
204                 if (((limit_pfn - size) & align_mask) > curr_iova->pfn_hi)
205                         break;  /* found a free slot */
206
207                 limit_pfn = curr_iova->pfn_lo;
208 move_left:
209                 prev = curr;
210                 curr = rb_prev(curr);
211         }
212
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;
217         }
218
219         /* pfn_lo will point to size aligned address if size_aligned is set */
220         new->pfn_lo = new_pfn;
221         new->pfn_hi = new->pfn_lo + size - 1;
222
223         /* If we have 'prev', it's a valid place to start the insertion. */
224         iova_insert_rbtree(&iovad->rbroot, new, prev);
225         __cached_rbnode_insert_update(iovad, new);
226
227         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
228
229
230         return 0;
231 }
232
233 static struct kmem_cache *iova_cache;
234 static unsigned int iova_cache_users;
235 static DEFINE_MUTEX(iova_cache_mutex);
236
237 struct iova *alloc_iova_mem(void)
238 {
239         return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
240 }
241 EXPORT_SYMBOL(alloc_iova_mem);
242
243 void free_iova_mem(struct iova *iova)
244 {
245         kmem_cache_free(iova_cache, iova);
246 }
247 EXPORT_SYMBOL(free_iova_mem);
248
249 int 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 }
268 EXPORT_SYMBOL_GPL(iova_cache_get);
269
270 void 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 }
282 EXPORT_SYMBOL_GPL(iova_cache_put);
283
284 /**
285  * alloc_iova - allocates an iova
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
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
292  * flag is set then the allocated address iova->pfn_lo will be naturally
293  * aligned on roundup_power_of_two(size).
294  */
295 struct iova *
296 alloc_iova(struct iova_domain *iovad, unsigned long size,
297         unsigned long limit_pfn,
298         bool size_aligned)
299 {
300         struct iova *new_iova;
301         int ret;
302
303         new_iova = alloc_iova_mem();
304         if (!new_iova)
305                 return NULL;
306
307         ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
308                         new_iova, size_aligned);
309
310         if (ret) {
311                 free_iova_mem(new_iova);
312                 return NULL;
313         }
314
315         return new_iova;
316 }
317 EXPORT_SYMBOL_GPL(alloc_iova);
318
319 static struct iova *
320 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
321 {
322         struct rb_node *node = iovad->rbroot.rb_node;
323
324         assert_spin_locked(&iovad->iova_rbtree_lock);
325
326         while (node) {
327                 struct iova *iova = rb_entry(node, struct iova, node);
328
329                 if (pfn < iova->pfn_lo)
330                         node = node->rb_left;
331                 else if (pfn > iova->pfn_hi)
332                         node = node->rb_right;
333                 else
334                         return iova;    /* pfn falls within iova's range */
335         }
336
337         return NULL;
338 }
339
340 static 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  */
355 struct 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 }
366 EXPORT_SYMBOL_GPL(find_iova);
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  */
374 void
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);
380         private_free_iova(iovad, iova);
381         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
382 }
383 EXPORT_SYMBOL_GPL(__free_iova);
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  */
392 void
393 free_iova(struct iova_domain *iovad, unsigned long pfn)
394 {
395         struct iova *iova = find_iova(iovad, pfn);
396
397         if (iova)
398                 __free_iova(iovad, iova);
399
400 }
401 EXPORT_SYMBOL_GPL(free_iova);
402
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 */
411 unsigned long
412 alloc_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
423 retry:
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 }
440 EXPORT_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  */
450 void
451 free_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 }
458 EXPORT_SYMBOL_GPL(free_iova_fast);
459
460 #define fq_ring_for_each(i, fq) \
461         for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
462
463 static inline bool fq_full(struct iova_fq *fq)
464 {
465         assert_spin_locked(&fq->lock);
466         return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
467 }
468
469 static inline unsigned fq_ring_add(struct iova_fq *fq)
470 {
471         unsigned idx = fq->tail;
472
473         assert_spin_locked(&fq->lock);
474
475         fq->tail = (idx + 1) % IOVA_FQ_SIZE;
476
477         return idx;
478 }
479
480 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
481 {
482         u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
483         unsigned idx;
484
485         assert_spin_locked(&fq->lock);
486
487         fq_ring_for_each(idx, fq) {
488
489                 if (fq->entries[idx].counter >= counter)
490                         break;
491
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);
498
499                 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
500         }
501 }
502
503 static 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);
508 }
509
510 static 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
531 static 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
550 void 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);
555         unsigned long flags;
556         unsigned idx;
557
558         spin_lock_irqsave(&fq->lock, flags);
559
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
567         if (fq_full(fq)) {
568                 iova_domain_flush(iovad);
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;
577         fq->entries[idx].counter  = atomic64_read(&iovad->fq_flush_start_cnt);
578
579         spin_unlock_irqrestore(&fq->lock, flags);
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
585         put_cpu_ptr(iovad->fq);
586 }
587 EXPORT_SYMBOL_GPL(queue_iova);
588
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  */
594 void put_iova_domain(struct iova_domain *iovad)
595 {
596         struct rb_node *node;
597         unsigned long flags;
598
599         free_iova_flush_queue(iovad);
600         free_iova_rcaches(iovad);
601         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
602         node = rb_first(&iovad->rbroot);
603         while (node) {
604                 struct iova *iova = rb_entry(node, struct iova, node);
605
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 }
612 EXPORT_SYMBOL_GPL(put_iova_domain);
613
614 static int
615 __is_range_overlap(struct rb_node *node,
616         unsigned long pfn_lo, unsigned long pfn_hi)
617 {
618         struct iova *iova = rb_entry(node, struct iova, node);
619
620         if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
621                 return 1;
622         return 0;
623 }
624
625 static inline struct iova *
626 alloc_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
639 static 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
645         iova = alloc_and_init_iova(pfn_lo, pfn_hi);
646         if (iova)
647                 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
648
649         return iova;
650 }
651
652 static 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  */
670 struct iova *
671 reserve_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
679         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
680         for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
681                 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
682                         iova = rb_entry(node, struct iova, node);
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
693         /* We are here either because this is the first reserver node
694          * or need to insert remaining non overlap addr range
695          */
696         iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
697 finish:
698
699         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
700         return iova;
701 }
702 EXPORT_SYMBOL_GPL(reserve_iova);
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  */
711 void
712 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
713 {
714         unsigned long flags;
715         struct rb_node *node;
716
717         spin_lock_irqsave(&from->iova_rbtree_lock, flags);
718         for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
719                 struct iova *iova = rb_entry(node, struct iova, node);
720                 struct iova *new_iova;
721
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         }
727         spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
728 }
729 EXPORT_SYMBOL_GPL(copy_reserved_iova);
730
731 struct iova *
732 split_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) {
754                 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
755                 iova->pfn_lo = pfn_lo;
756         }
757         if (next) {
758                 iova_insert_rbtree(&iovad->rbroot, next, NULL);
759                 iova->pfn_hi = pfn_hi;
760         }
761         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
762
763         return iova;
764
765 error:
766         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
767         if (prev)
768                 free_iova_mem(prev);
769         return NULL;
770 }
771
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
782 struct iova_magazine {
783         unsigned long size;
784         unsigned long pfns[IOVA_MAG_SIZE];
785 };
786
787 struct iova_cpu_rcache {
788         spinlock_t lock;
789         struct iova_magazine *loaded;
790         struct iova_magazine *prev;
791 };
792
793 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
794 {
795         return kzalloc(sizeof(struct iova_magazine), flags);
796 }
797
798 static void iova_magazine_free(struct iova_magazine *mag)
799 {
800         kfree(mag);
801 }
802
803 static void
804 iova_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
826 static bool iova_magazine_full(struct iova_magazine *mag)
827 {
828         return (mag && mag->size == IOVA_MAG_SIZE);
829 }
830
831 static bool iova_magazine_empty(struct iova_magazine *mag)
832 {
833         return (!mag || mag->size == 0);
834 }
835
836 static 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
847 static 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
854 static 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  */
883 static 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
892         cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
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
931 static 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  */
947 static 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
955         cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
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  */
986 static 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  */
1001 static 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  */
1021 static 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  */
1045 void 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
1062 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1063 MODULE_LICENSE("GPL");