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