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