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