GNU Linux-libre 4.19.286-gnu1
[releases.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 /* The anchor node sits above the top of the usable address space */
28 #define IOVA_ANCHOR     ~0UL
29
30 static bool iova_rcache_insert(struct iova_domain *iovad,
31                                unsigned long pfn,
32                                unsigned long size);
33 static unsigned long iova_rcache_get(struct iova_domain *iovad,
34                                      unsigned long size,
35                                      unsigned long limit_pfn);
36 static void init_iova_rcaches(struct iova_domain *iovad);
37 static void free_iova_rcaches(struct iova_domain *iovad);
38 static void fq_destroy_all_entries(struct iova_domain *iovad);
39 static void fq_flush_timeout(struct timer_list *t);
40
41 void
42 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
43         unsigned long start_pfn)
44 {
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
52         spin_lock_init(&iovad->iova_rbtree_lock);
53         iovad->rbroot = RB_ROOT;
54         iovad->cached_node = &iovad->anchor.node;
55         iovad->cached32_node = &iovad->anchor.node;
56         iovad->granule = granule;
57         iovad->start_pfn = start_pfn;
58         iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
59         iovad->flush_cb = NULL;
60         iovad->fq = NULL;
61         iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
62         rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
63         rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
64         init_iova_rcaches(iovad);
65 }
66 EXPORT_SYMBOL_GPL(init_iova_domain);
67
68 bool has_iova_flush_queue(struct iova_domain *iovad)
69 {
70         return !!iovad->fq;
71 }
72
73 static void free_iova_flush_queue(struct iova_domain *iovad)
74 {
75         if (!has_iova_flush_queue(iovad))
76                 return;
77
78         del_timer_sync(&iovad->fq_timer);
79
80         fq_destroy_all_entries(iovad);
81
82         free_percpu(iovad->fq);
83
84         iovad->fq         = NULL;
85         iovad->flush_cb   = NULL;
86         iovad->entry_dtor = NULL;
87 }
88
89 int init_iova_flush_queue(struct iova_domain *iovad,
90                           iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
91 {
92         struct iova_fq __percpu *queue;
93         int cpu;
94
95         atomic64_set(&iovad->fq_flush_start_cnt,  0);
96         atomic64_set(&iovad->fq_flush_finish_cnt, 0);
97
98         queue = alloc_percpu(struct iova_fq);
99         if (!queue)
100                 return -ENOMEM;
101
102         iovad->flush_cb   = flush_cb;
103         iovad->entry_dtor = entry_dtor;
104
105         for_each_possible_cpu(cpu) {
106                 struct iova_fq *fq;
107
108                 fq = per_cpu_ptr(queue, cpu);
109                 fq->head = 0;
110                 fq->tail = 0;
111
112                 spin_lock_init(&fq->lock);
113         }
114
115         smp_wmb();
116
117         iovad->fq = queue;
118
119         timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
120         atomic_set(&iovad->fq_timer_on, 0);
121
122         return 0;
123 }
124 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
125
126 static struct rb_node *
127 __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
128 {
129         if (limit_pfn <= iovad->dma_32bit_pfn)
130                 return iovad->cached32_node;
131
132         return iovad->cached_node;
133 }
134
135 static void
136 __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
137 {
138         if (new->pfn_hi < iovad->dma_32bit_pfn)
139                 iovad->cached32_node = &new->node;
140         else
141                 iovad->cached_node = &new->node;
142 }
143
144 static void
145 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
146 {
147         struct iova *cached_iova;
148
149         cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
150         if (free == cached_iova ||
151             (free->pfn_hi < iovad->dma_32bit_pfn &&
152              free->pfn_lo >= cached_iova->pfn_lo))
153                 iovad->cached32_node = rb_next(&free->node);
154
155         cached_iova = rb_entry(iovad->cached_node, struct iova, node);
156         if (free->pfn_lo >= cached_iova->pfn_lo)
157                 iovad->cached_node = rb_next(&free->node);
158 }
159
160 /* Insert the iova into domain rbtree by holding writer lock */
161 static void
162 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
163                    struct rb_node *start)
164 {
165         struct rb_node **new, *parent = NULL;
166
167         new = (start) ? &start : &(root->rb_node);
168         /* Figure out where to put new node */
169         while (*new) {
170                 struct iova *this = rb_entry(*new, struct iova, node);
171
172                 parent = *new;
173
174                 if (iova->pfn_lo < this->pfn_lo)
175                         new = &((*new)->rb_left);
176                 else if (iova->pfn_lo > this->pfn_lo)
177                         new = &((*new)->rb_right);
178                 else {
179                         WARN_ON(1); /* this should not happen */
180                         return;
181                 }
182         }
183         /* Add new node and rebalance tree. */
184         rb_link_node(&iova->node, parent, new);
185         rb_insert_color(&iova->node, root);
186 }
187
188 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
189                 unsigned long size, unsigned long limit_pfn,
190                         struct iova *new, bool size_aligned)
191 {
192         struct rb_node *curr, *prev;
193         struct iova *curr_iova;
194         unsigned long flags;
195         unsigned long new_pfn;
196         unsigned long align_mask = ~0UL;
197
198         if (size_aligned)
199                 align_mask <<= fls_long(size - 1);
200
201         /* Walk the tree backwards */
202         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
203         curr = __get_cached_rbnode(iovad, limit_pfn);
204         curr_iova = rb_entry(curr, struct iova, node);
205         do {
206                 limit_pfn = min(limit_pfn, curr_iova->pfn_lo);
207                 new_pfn = (limit_pfn - size) & align_mask;
208                 prev = curr;
209                 curr = rb_prev(curr);
210                 curr_iova = rb_entry(curr, struct iova, node);
211         } while (curr && new_pfn <= curr_iova->pfn_hi);
212
213         if (limit_pfn < size || new_pfn < iovad->start_pfn) {
214                 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
215                 return -ENOMEM;
216         }
217
218         /* pfn_lo will point to size aligned address if size_aligned is set */
219         new->pfn_lo = new_pfn;
220         new->pfn_hi = new->pfn_lo + size - 1;
221
222         /* If we have 'prev', it's a valid place to start the insertion. */
223         iova_insert_rbtree(&iovad->rbroot, new, prev);
224         __cached_rbnode_insert_update(iovad, new);
225
226         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
227
228
229         return 0;
230 }
231
232 static struct kmem_cache *iova_cache;
233 static unsigned int iova_cache_users;
234 static DEFINE_MUTEX(iova_cache_mutex);
235
236 struct iova *alloc_iova_mem(void)
237 {
238         return kmem_cache_zalloc(iova_cache, GFP_ATOMIC);
239 }
240 EXPORT_SYMBOL(alloc_iova_mem);
241
242 void free_iova_mem(struct iova *iova)
243 {
244         if (iova->pfn_lo != IOVA_ANCHOR)
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  * @flush_rcache: - set to flush rcache on regular allocation failure
409  * This function tries to satisfy an iova allocation from the rcache,
410  * and falls back to regular allocation on failure. If regular allocation
411  * fails too and the flush_rcache flag is set then the rcache will be flushed.
412 */
413 unsigned long
414 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
415                 unsigned long limit_pfn, bool flush_rcache)
416 {
417         unsigned long iova_pfn;
418         struct iova *new_iova;
419
420         iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
421         if (iova_pfn)
422                 return iova_pfn;
423
424 retry:
425         new_iova = alloc_iova(iovad, size, limit_pfn, true);
426         if (!new_iova) {
427                 unsigned int cpu;
428
429                 if (!flush_rcache)
430                         return 0;
431
432                 /* Try replenishing IOVAs by flushing rcache. */
433                 flush_rcache = false;
434                 for_each_online_cpu(cpu)
435                         free_cpu_cached_iovas(cpu, iovad);
436                 goto retry;
437         }
438
439         return new_iova->pfn_lo;
440 }
441 EXPORT_SYMBOL_GPL(alloc_iova_fast);
442
443 /**
444  * free_iova_fast - free iova pfn range into rcache
445  * @iovad: - iova domain in question.
446  * @pfn: - pfn that is allocated previously
447  * @size: - # of pages in range
448  * This functions frees an iova range by trying to put it into the rcache,
449  * falling back to regular iova deallocation via free_iova() if this fails.
450  */
451 void
452 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
453 {
454         if (iova_rcache_insert(iovad, pfn, size))
455                 return;
456
457         free_iova(iovad, pfn);
458 }
459 EXPORT_SYMBOL_GPL(free_iova_fast);
460
461 #define fq_ring_for_each(i, fq) \
462         for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
463
464 static inline bool fq_full(struct iova_fq *fq)
465 {
466         assert_spin_locked(&fq->lock);
467         return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
468 }
469
470 static inline unsigned fq_ring_add(struct iova_fq *fq)
471 {
472         unsigned idx = fq->tail;
473
474         assert_spin_locked(&fq->lock);
475
476         fq->tail = (idx + 1) % IOVA_FQ_SIZE;
477
478         return idx;
479 }
480
481 static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
482 {
483         u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
484         unsigned idx;
485
486         assert_spin_locked(&fq->lock);
487
488         fq_ring_for_each(idx, fq) {
489
490                 if (fq->entries[idx].counter >= counter)
491                         break;
492
493                 if (iovad->entry_dtor)
494                         iovad->entry_dtor(fq->entries[idx].data);
495
496                 free_iova_fast(iovad,
497                                fq->entries[idx].iova_pfn,
498                                fq->entries[idx].pages);
499
500                 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
501         }
502 }
503
504 static void iova_domain_flush(struct iova_domain *iovad)
505 {
506         atomic64_inc(&iovad->fq_flush_start_cnt);
507         iovad->flush_cb(iovad);
508         atomic64_inc(&iovad->fq_flush_finish_cnt);
509 }
510
511 static void fq_destroy_all_entries(struct iova_domain *iovad)
512 {
513         int cpu;
514
515         /*
516          * This code runs when the iova_domain is being detroyed, so don't
517          * bother to free iovas, just call the entry_dtor on all remaining
518          * entries.
519          */
520         if (!iovad->entry_dtor)
521                 return;
522
523         for_each_possible_cpu(cpu) {
524                 struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
525                 int idx;
526
527                 fq_ring_for_each(idx, fq)
528                         iovad->entry_dtor(fq->entries[idx].data);
529         }
530 }
531
532 static void fq_flush_timeout(struct timer_list *t)
533 {
534         struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
535         int cpu;
536
537         atomic_set(&iovad->fq_timer_on, 0);
538         iova_domain_flush(iovad);
539
540         for_each_possible_cpu(cpu) {
541                 unsigned long flags;
542                 struct iova_fq *fq;
543
544                 fq = per_cpu_ptr(iovad->fq, cpu);
545                 spin_lock_irqsave(&fq->lock, flags);
546                 fq_ring_free(iovad, fq);
547                 spin_unlock_irqrestore(&fq->lock, flags);
548         }
549 }
550
551 void queue_iova(struct iova_domain *iovad,
552                 unsigned long pfn, unsigned long pages,
553                 unsigned long data)
554 {
555         struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
556         unsigned long flags;
557         unsigned idx;
558
559         spin_lock_irqsave(&fq->lock, flags);
560
561         /*
562          * First remove all entries from the flush queue that have already been
563          * flushed out on another CPU. This makes the fq_full() check below less
564          * likely to be true.
565          */
566         fq_ring_free(iovad, fq);
567
568         if (fq_full(fq)) {
569                 iova_domain_flush(iovad);
570                 fq_ring_free(iovad, fq);
571         }
572
573         idx = fq_ring_add(fq);
574
575         fq->entries[idx].iova_pfn = pfn;
576         fq->entries[idx].pages    = pages;
577         fq->entries[idx].data     = data;
578         fq->entries[idx].counter  = atomic64_read(&iovad->fq_flush_start_cnt);
579
580         spin_unlock_irqrestore(&fq->lock, flags);
581
582         /* Avoid false sharing as much as possible. */
583         if (!atomic_read(&iovad->fq_timer_on) &&
584             !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
585                 mod_timer(&iovad->fq_timer,
586                           jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
587 }
588 EXPORT_SYMBOL_GPL(queue_iova);
589
590 /**
591  * put_iova_domain - destroys the iova doamin
592  * @iovad: - iova domain in question.
593  * All the iova's in that domain are destroyed.
594  */
595 void put_iova_domain(struct iova_domain *iovad)
596 {
597         struct iova *iova, *tmp;
598
599         free_iova_flush_queue(iovad);
600         free_iova_rcaches(iovad);
601         rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
602                 free_iova_mem(iova);
603 }
604 EXPORT_SYMBOL_GPL(put_iova_domain);
605
606 static int
607 __is_range_overlap(struct rb_node *node,
608         unsigned long pfn_lo, unsigned long pfn_hi)
609 {
610         struct iova *iova = rb_entry(node, struct iova, node);
611
612         if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
613                 return 1;
614         return 0;
615 }
616
617 static inline struct iova *
618 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
619 {
620         struct iova *iova;
621
622         iova = alloc_iova_mem();
623         if (iova) {
624                 iova->pfn_lo = pfn_lo;
625                 iova->pfn_hi = pfn_hi;
626         }
627
628         return iova;
629 }
630
631 static struct iova *
632 __insert_new_range(struct iova_domain *iovad,
633         unsigned long pfn_lo, unsigned long pfn_hi)
634 {
635         struct iova *iova;
636
637         iova = alloc_and_init_iova(pfn_lo, pfn_hi);
638         if (iova)
639                 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
640
641         return iova;
642 }
643
644 static void
645 __adjust_overlap_range(struct iova *iova,
646         unsigned long *pfn_lo, unsigned long *pfn_hi)
647 {
648         if (*pfn_lo < iova->pfn_lo)
649                 iova->pfn_lo = *pfn_lo;
650         if (*pfn_hi > iova->pfn_hi)
651                 *pfn_lo = iova->pfn_hi + 1;
652 }
653
654 /**
655  * reserve_iova - reserves an iova in the given range
656  * @iovad: - iova domain pointer
657  * @pfn_lo: - lower page frame address
658  * @pfn_hi:- higher pfn adderss
659  * This function allocates reserves the address range from pfn_lo to pfn_hi so
660  * that this address is not dished out as part of alloc_iova.
661  */
662 struct iova *
663 reserve_iova(struct iova_domain *iovad,
664         unsigned long pfn_lo, unsigned long pfn_hi)
665 {
666         struct rb_node *node;
667         unsigned long flags;
668         struct iova *iova;
669         unsigned int overlap = 0;
670
671         /* Don't allow nonsensical pfns */
672         if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
673                 return NULL;
674
675         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
676         for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
677                 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
678                         iova = rb_entry(node, struct iova, node);
679                         __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
680                         if ((pfn_lo >= iova->pfn_lo) &&
681                                 (pfn_hi <= iova->pfn_hi))
682                                 goto finish;
683                         overlap = 1;
684
685                 } else if (overlap)
686                                 break;
687         }
688
689         /* We are here either because this is the first reserver node
690          * or need to insert remaining non overlap addr range
691          */
692         iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
693 finish:
694
695         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
696         return iova;
697 }
698 EXPORT_SYMBOL_GPL(reserve_iova);
699
700 /**
701  * copy_reserved_iova - copies the reserved between domains
702  * @from: - source doamin from where to copy
703  * @to: - destination domin where to copy
704  * This function copies reserved iova's from one doamin to
705  * other.
706  */
707 void
708 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
709 {
710         unsigned long flags;
711         struct rb_node *node;
712
713         spin_lock_irqsave(&from->iova_rbtree_lock, flags);
714         for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
715                 struct iova *iova = rb_entry(node, struct iova, node);
716                 struct iova *new_iova;
717
718                 if (iova->pfn_lo == IOVA_ANCHOR)
719                         continue;
720
721                 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
722                 if (!new_iova)
723                         printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
724                                 iova->pfn_lo, iova->pfn_lo);
725         }
726         spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
727 }
728 EXPORT_SYMBOL_GPL(copy_reserved_iova);
729
730 struct iova *
731 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
732                       unsigned long pfn_lo, unsigned long pfn_hi)
733 {
734         unsigned long flags;
735         struct iova *prev = NULL, *next = NULL;
736
737         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
738         if (iova->pfn_lo < pfn_lo) {
739                 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
740                 if (prev == NULL)
741                         goto error;
742         }
743         if (iova->pfn_hi > pfn_hi) {
744                 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
745                 if (next == NULL)
746                         goto error;
747         }
748
749         __cached_rbnode_delete_update(iovad, iova);
750         rb_erase(&iova->node, &iovad->rbroot);
751
752         if (prev) {
753                 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
754                 iova->pfn_lo = pfn_lo;
755         }
756         if (next) {
757                 iova_insert_rbtree(&iovad->rbroot, next, NULL);
758                 iova->pfn_hi = pfn_hi;
759         }
760         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
761
762         return iova;
763
764 error:
765         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
766         if (prev)
767                 free_iova_mem(prev);
768         return NULL;
769 }
770
771 /*
772  * Magazine caches for IOVA ranges.  For an introduction to magazines,
773  * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
774  * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
775  * For simplicity, we use a static magazine size and don't implement the
776  * dynamic size tuning described in the paper.
777  */
778
779 #define IOVA_MAG_SIZE 128
780
781 struct iova_magazine {
782         unsigned long size;
783         unsigned long pfns[IOVA_MAG_SIZE];
784 };
785
786 struct iova_cpu_rcache {
787         spinlock_t lock;
788         struct iova_magazine *loaded;
789         struct iova_magazine *prev;
790 };
791
792 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
793 {
794         return kzalloc(sizeof(struct iova_magazine), flags);
795 }
796
797 static void iova_magazine_free(struct iova_magazine *mag)
798 {
799         kfree(mag);
800 }
801
802 static void
803 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
804 {
805         unsigned long flags;
806         int i;
807
808         if (!mag)
809                 return;
810
811         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
812
813         for (i = 0 ; i < mag->size; ++i) {
814                 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
815
816                 if (WARN_ON(!iova))
817                         continue;
818
819                 private_free_iova(iovad, iova);
820         }
821
822         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
823
824         mag->size = 0;
825 }
826
827 static bool iova_magazine_full(struct iova_magazine *mag)
828 {
829         return (mag && mag->size == IOVA_MAG_SIZE);
830 }
831
832 static bool iova_magazine_empty(struct iova_magazine *mag)
833 {
834         return (!mag || mag->size == 0);
835 }
836
837 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
838                                        unsigned long limit_pfn)
839 {
840         int i;
841         unsigned long pfn;
842
843         BUG_ON(iova_magazine_empty(mag));
844
845         /* Only fall back to the rbtree if we have no suitable pfns at all */
846         for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
847                 if (i == 0)
848                         return 0;
849
850         /* Swap it to pop it */
851         pfn = mag->pfns[i];
852         mag->pfns[i] = mag->pfns[--mag->size];
853
854         return pfn;
855 }
856
857 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
858 {
859         BUG_ON(iova_magazine_full(mag));
860
861         mag->pfns[mag->size++] = pfn;
862 }
863
864 static void init_iova_rcaches(struct iova_domain *iovad)
865 {
866         struct iova_cpu_rcache *cpu_rcache;
867         struct iova_rcache *rcache;
868         unsigned int cpu;
869         int i;
870
871         for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
872                 rcache = &iovad->rcaches[i];
873                 spin_lock_init(&rcache->lock);
874                 rcache->depot_size = 0;
875                 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
876                 if (WARN_ON(!rcache->cpu_rcaches))
877                         continue;
878                 for_each_possible_cpu(cpu) {
879                         cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
880                         spin_lock_init(&cpu_rcache->lock);
881                         cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
882                         cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
883                 }
884         }
885 }
886
887 /*
888  * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
889  * return true on success.  Can fail if rcache is full and we can't free
890  * space, and free_iova() (our only caller) will then return the IOVA
891  * range to the rbtree instead.
892  */
893 static bool __iova_rcache_insert(struct iova_domain *iovad,
894                                  struct iova_rcache *rcache,
895                                  unsigned long iova_pfn)
896 {
897         struct iova_magazine *mag_to_free = NULL;
898         struct iova_cpu_rcache *cpu_rcache;
899         bool can_insert = false;
900         unsigned long flags;
901
902         cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
903         spin_lock_irqsave(&cpu_rcache->lock, flags);
904
905         if (!iova_magazine_full(cpu_rcache->loaded)) {
906                 can_insert = true;
907         } else if (!iova_magazine_full(cpu_rcache->prev)) {
908                 swap(cpu_rcache->prev, cpu_rcache->loaded);
909                 can_insert = true;
910         } else {
911                 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
912
913                 if (new_mag) {
914                         spin_lock(&rcache->lock);
915                         if (rcache->depot_size < MAX_GLOBAL_MAGS) {
916                                 rcache->depot[rcache->depot_size++] =
917                                                 cpu_rcache->loaded;
918                         } else {
919                                 mag_to_free = cpu_rcache->loaded;
920                         }
921                         spin_unlock(&rcache->lock);
922
923                         cpu_rcache->loaded = new_mag;
924                         can_insert = true;
925                 }
926         }
927
928         if (can_insert)
929                 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
930
931         spin_unlock_irqrestore(&cpu_rcache->lock, flags);
932
933         if (mag_to_free) {
934                 iova_magazine_free_pfns(mag_to_free, iovad);
935                 iova_magazine_free(mag_to_free);
936         }
937
938         return can_insert;
939 }
940
941 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
942                                unsigned long size)
943 {
944         unsigned int log_size = order_base_2(size);
945
946         if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
947                 return false;
948
949         return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
950 }
951
952 /*
953  * Caller wants to allocate a new IOVA range from 'rcache'.  If we can
954  * satisfy the request, return a matching non-NULL range and remove
955  * it from the 'rcache'.
956  */
957 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
958                                        unsigned long limit_pfn)
959 {
960         struct iova_cpu_rcache *cpu_rcache;
961         unsigned long iova_pfn = 0;
962         bool has_pfn = false;
963         unsigned long flags;
964
965         cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
966         spin_lock_irqsave(&cpu_rcache->lock, flags);
967
968         if (!iova_magazine_empty(cpu_rcache->loaded)) {
969                 has_pfn = true;
970         } else if (!iova_magazine_empty(cpu_rcache->prev)) {
971                 swap(cpu_rcache->prev, cpu_rcache->loaded);
972                 has_pfn = true;
973         } else {
974                 spin_lock(&rcache->lock);
975                 if (rcache->depot_size > 0) {
976                         iova_magazine_free(cpu_rcache->loaded);
977                         cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
978                         has_pfn = true;
979                 }
980                 spin_unlock(&rcache->lock);
981         }
982
983         if (has_pfn)
984                 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
985
986         spin_unlock_irqrestore(&cpu_rcache->lock, flags);
987
988         return iova_pfn;
989 }
990
991 /*
992  * Try to satisfy IOVA allocation range from rcache.  Fail if requested
993  * size is too big or the DMA limit we are given isn't satisfied by the
994  * top element in the magazine.
995  */
996 static unsigned long iova_rcache_get(struct iova_domain *iovad,
997                                      unsigned long size,
998                                      unsigned long limit_pfn)
999 {
1000         unsigned int log_size = order_base_2(size);
1001
1002         if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
1003                 return 0;
1004
1005         return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
1006 }
1007
1008 /*
1009  * free rcache data structures.
1010  */
1011 static void free_iova_rcaches(struct iova_domain *iovad)
1012 {
1013         struct iova_rcache *rcache;
1014         struct iova_cpu_rcache *cpu_rcache;
1015         unsigned int cpu;
1016         int i, j;
1017
1018         for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1019                 rcache = &iovad->rcaches[i];
1020                 for_each_possible_cpu(cpu) {
1021                         cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1022                         iova_magazine_free(cpu_rcache->loaded);
1023                         iova_magazine_free(cpu_rcache->prev);
1024                 }
1025                 free_percpu(rcache->cpu_rcaches);
1026                 for (j = 0; j < rcache->depot_size; ++j)
1027                         iova_magazine_free(rcache->depot[j]);
1028         }
1029 }
1030
1031 /*
1032  * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
1033  */
1034 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
1035 {
1036         struct iova_cpu_rcache *cpu_rcache;
1037         struct iova_rcache *rcache;
1038         unsigned long flags;
1039         int i;
1040
1041         for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
1042                 rcache = &iovad->rcaches[i];
1043                 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
1044                 spin_lock_irqsave(&cpu_rcache->lock, flags);
1045                 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
1046                 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
1047                 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
1048         }
1049 }
1050
1051 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
1052 MODULE_LICENSE("GPL");