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