b9f6ce02a1e1f6adade21630c9b08db88b98aaac
[linux-2.6-microblaze.git] / drivers / iommu / iova.c
1 /*
2  * Copyright © 2006-2009, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
18  */
19
20 #include <linux/iova.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <linux/bitops.h>
25 #include <linux/cpu.h>
26
27 static bool iova_rcache_insert(struct iova_domain *iovad,
28                                unsigned long pfn,
29                                unsigned long size);
30 static unsigned long iova_rcache_get(struct iova_domain *iovad,
31                                      unsigned long size,
32                                      unsigned long limit_pfn);
33 static void init_iova_rcaches(struct iova_domain *iovad);
34 static void free_iova_rcaches(struct iova_domain *iovad);
35
36 void
37 init_iova_domain(struct iova_domain *iovad, unsigned long granule,
38         unsigned long start_pfn, unsigned long pfn_32bit)
39 {
40         /*
41          * IOVA granularity will normally be equal to the smallest
42          * supported IOMMU page size; both *must* be capable of
43          * representing individual CPU pages exactly.
44          */
45         BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
46
47         spin_lock_init(&iovad->iova_rbtree_lock);
48         iovad->rbroot = RB_ROOT;
49         iovad->cached32_node = NULL;
50         iovad->granule = granule;
51         iovad->start_pfn = start_pfn;
52         iovad->dma_32bit_pfn = pfn_32bit + 1;
53         iovad->flush_cb = NULL;
54         iovad->fq = NULL;
55         init_iova_rcaches(iovad);
56 }
57 EXPORT_SYMBOL_GPL(init_iova_domain);
58
59 static void free_iova_flush_queue(struct iova_domain *iovad)
60 {
61         if (!iovad->fq)
62                 return;
63
64         free_percpu(iovad->fq);
65
66         iovad->fq         = NULL;
67         iovad->flush_cb   = NULL;
68         iovad->entry_dtor = NULL;
69 }
70
71 int init_iova_flush_queue(struct iova_domain *iovad,
72                           iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
73 {
74         int cpu;
75
76         iovad->fq = alloc_percpu(struct iova_fq);
77         if (!iovad->fq)
78                 return -ENOMEM;
79
80         iovad->flush_cb   = flush_cb;
81         iovad->entry_dtor = entry_dtor;
82
83         for_each_possible_cpu(cpu) {
84                 struct iova_fq *fq;
85
86                 fq = per_cpu_ptr(iovad->fq, cpu);
87                 fq->head = 0;
88                 fq->tail = 0;
89         }
90
91         return 0;
92 }
93 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
94
95 static struct rb_node *
96 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn)
97 {
98         if ((*limit_pfn > iovad->dma_32bit_pfn) ||
99                 (iovad->cached32_node == NULL))
100                 return rb_last(&iovad->rbroot);
101         else {
102                 struct rb_node *prev_node = rb_prev(iovad->cached32_node);
103                 struct iova *curr_iova =
104                         rb_entry(iovad->cached32_node, struct iova, node);
105                 *limit_pfn = curr_iova->pfn_lo;
106                 return prev_node;
107         }
108 }
109
110 static void
111 __cached_rbnode_insert_update(struct iova_domain *iovad,
112         unsigned long limit_pfn, struct iova *new)
113 {
114         if (limit_pfn != iovad->dma_32bit_pfn)
115                 return;
116         iovad->cached32_node = &new->node;
117 }
118
119 static void
120 __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
121 {
122         struct iova *cached_iova;
123         struct rb_node *curr;
124
125         if (!iovad->cached32_node)
126                 return;
127         curr = iovad->cached32_node;
128         cached_iova = rb_entry(curr, struct iova, node);
129
130         if (free->pfn_lo >= cached_iova->pfn_lo) {
131                 struct rb_node *node = rb_next(&free->node);
132                 struct iova *iova = rb_entry(node, struct iova, node);
133
134                 /* only cache if it's below 32bit pfn */
135                 if (node && iova->pfn_lo < iovad->dma_32bit_pfn)
136                         iovad->cached32_node = node;
137                 else
138                         iovad->cached32_node = NULL;
139         }
140 }
141
142 /* Insert the iova into domain rbtree by holding writer lock */
143 static void
144 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
145                    struct rb_node *start)
146 {
147         struct rb_node **new, *parent = NULL;
148
149         new = (start) ? &start : &(root->rb_node);
150         /* Figure out where to put new node */
151         while (*new) {
152                 struct iova *this = rb_entry(*new, struct iova, node);
153
154                 parent = *new;
155
156                 if (iova->pfn_lo < this->pfn_lo)
157                         new = &((*new)->rb_left);
158                 else if (iova->pfn_lo > this->pfn_lo)
159                         new = &((*new)->rb_right);
160                 else {
161                         WARN_ON(1); /* this should not happen */
162                         return;
163                 }
164         }
165         /* Add new node and rebalance tree. */
166         rb_link_node(&iova->node, parent, new);
167         rb_insert_color(&iova->node, root);
168 }
169
170 /*
171  * Computes the padding size required, to make the start address
172  * naturally aligned on the power-of-two order of its size
173  */
174 static unsigned int
175 iova_get_pad_size(unsigned int size, unsigned int limit_pfn)
176 {
177         return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1);
178 }
179
180 static 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 *prev, *curr = NULL;
185         unsigned long flags;
186         unsigned long saved_pfn;
187         unsigned int pad_size = 0;
188
189         /* Walk the tree backwards */
190         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
191         saved_pfn = limit_pfn;
192         curr = __get_cached_rbnode(iovad, &limit_pfn);
193         prev = curr;
194         while (curr) {
195                 struct iova *curr_iova = rb_entry(curr, struct iova, node);
196
197                 if (limit_pfn <= curr_iova->pfn_lo) {
198                         goto move_left;
199                 } else if (limit_pfn > curr_iova->pfn_hi) {
200                         if (size_aligned)
201                                 pad_size = iova_get_pad_size(size, limit_pfn);
202                         if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn)
203                                 break;  /* found a free slot */
204                 }
205                 limit_pfn = curr_iova->pfn_lo;
206 move_left:
207                 prev = curr;
208                 curr = rb_prev(curr);
209         }
210
211         if (!curr) {
212                 if (size_aligned)
213                         pad_size = iova_get_pad_size(size, limit_pfn);
214                 if ((iovad->start_pfn + size + pad_size) > limit_pfn) {
215                         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
216                         return -ENOMEM;
217                 }
218         }
219
220         /* pfn_lo will point to size aligned address if size_aligned is set */
221         new->pfn_lo = limit_pfn - (size + pad_size);
222         new->pfn_hi = new->pfn_lo + size - 1;
223
224         /* If we have 'prev', it's a valid place to start the insertion. */
225         iova_insert_rbtree(&iovad->rbroot, new, prev);
226         __cached_rbnode_insert_update(iovad, saved_pfn, new);
227
228         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
229
230
231         return 0;
232 }
233
234 static struct kmem_cache *iova_cache;
235 static unsigned int iova_cache_users;
236 static DEFINE_MUTEX(iova_cache_mutex);
237
238 struct iova *alloc_iova_mem(void)
239 {
240         return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
241 }
242 EXPORT_SYMBOL(alloc_iova_mem);
243
244 void free_iova_mem(struct iova *iova)
245 {
246         kmem_cache_free(iova_cache, iova);
247 }
248 EXPORT_SYMBOL(free_iova_mem);
249
250 int iova_cache_get(void)
251 {
252         mutex_lock(&iova_cache_mutex);
253         if (!iova_cache_users) {
254                 iova_cache = kmem_cache_create(
255                         "iommu_iova", sizeof(struct iova), 0,
256                         SLAB_HWCACHE_ALIGN, NULL);
257                 if (!iova_cache) {
258                         mutex_unlock(&iova_cache_mutex);
259                         printk(KERN_ERR "Couldn't create iova cache\n");
260                         return -ENOMEM;
261                 }
262         }
263
264         iova_cache_users++;
265         mutex_unlock(&iova_cache_mutex);
266
267         return 0;
268 }
269 EXPORT_SYMBOL_GPL(iova_cache_get);
270
271 void iova_cache_put(void)
272 {
273         mutex_lock(&iova_cache_mutex);
274         if (WARN_ON(!iova_cache_users)) {
275                 mutex_unlock(&iova_cache_mutex);
276                 return;
277         }
278         iova_cache_users--;
279         if (!iova_cache_users)
280                 kmem_cache_destroy(iova_cache);
281         mutex_unlock(&iova_cache_mutex);
282 }
283 EXPORT_SYMBOL_GPL(iova_cache_put);
284
285 /**
286  * alloc_iova - allocates an iova
287  * @iovad: - iova domain in question
288  * @size: - size of page frames to allocate
289  * @limit_pfn: - max limit address
290  * @size_aligned: - set if size_aligned address range is required
291  * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
292  * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
293  * flag is set then the allocated address iova->pfn_lo will be naturally
294  * aligned on roundup_power_of_two(size).
295  */
296 struct iova *
297 alloc_iova(struct iova_domain *iovad, unsigned long size,
298         unsigned long limit_pfn,
299         bool size_aligned)
300 {
301         struct iova *new_iova;
302         int ret;
303
304         new_iova = alloc_iova_mem();
305         if (!new_iova)
306                 return NULL;
307
308         ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
309                         new_iova, size_aligned);
310
311         if (ret) {
312                 free_iova_mem(new_iova);
313                 return NULL;
314         }
315
316         return new_iova;
317 }
318 EXPORT_SYMBOL_GPL(alloc_iova);
319
320 static struct iova *
321 private_find_iova(struct iova_domain *iovad, unsigned long pfn)
322 {
323         struct rb_node *node = iovad->rbroot.rb_node;
324
325         assert_spin_locked(&iovad->iova_rbtree_lock);
326
327         while (node) {
328                 struct iova *iova = rb_entry(node, struct iova, node);
329
330                 /* If pfn falls within iova's range, return iova */
331                 if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) {
332                         return iova;
333                 }
334
335                 if (pfn < iova->pfn_lo)
336                         node = node->rb_left;
337                 else if (pfn > iova->pfn_lo)
338                         node = node->rb_right;
339         }
340
341         return NULL;
342 }
343
344 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
345 {
346         assert_spin_locked(&iovad->iova_rbtree_lock);
347         __cached_rbnode_delete_update(iovad, iova);
348         rb_erase(&iova->node, &iovad->rbroot);
349         free_iova_mem(iova);
350 }
351
352 /**
353  * find_iova - finds an iova for a given pfn
354  * @iovad: - iova domain in question.
355  * @pfn: - page frame number
356  * This function finds and returns an iova belonging to the
357  * given doamin which matches the given pfn.
358  */
359 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
360 {
361         unsigned long flags;
362         struct iova *iova;
363
364         /* Take the lock so that no other thread is manipulating the rbtree */
365         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
366         iova = private_find_iova(iovad, pfn);
367         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
368         return iova;
369 }
370 EXPORT_SYMBOL_GPL(find_iova);
371
372 /**
373  * __free_iova - frees the given iova
374  * @iovad: iova domain in question.
375  * @iova: iova in question.
376  * Frees the given iova belonging to the giving domain
377  */
378 void
379 __free_iova(struct iova_domain *iovad, struct iova *iova)
380 {
381         unsigned long flags;
382
383         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
384         private_free_iova(iovad, iova);
385         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
386 }
387 EXPORT_SYMBOL_GPL(__free_iova);
388
389 /**
390  * free_iova - finds and frees the iova for a given pfn
391  * @iovad: - iova domain in question.
392  * @pfn: - pfn that is allocated previously
393  * This functions finds an iova for a given pfn and then
394  * frees the iova from that domain.
395  */
396 void
397 free_iova(struct iova_domain *iovad, unsigned long pfn)
398 {
399         struct iova *iova = find_iova(iovad, pfn);
400
401         if (iova)
402                 __free_iova(iovad, iova);
403
404 }
405 EXPORT_SYMBOL_GPL(free_iova);
406
407 /**
408  * alloc_iova_fast - allocates an iova from rcache
409  * @iovad: - iova domain in question
410  * @size: - size of page frames to allocate
411  * @limit_pfn: - max limit address
412  * This function tries to satisfy an iova allocation from the rcache,
413  * and falls back to regular allocation on failure.
414 */
415 unsigned long
416 alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
417                 unsigned long limit_pfn)
418 {
419         bool flushed_rcache = false;
420         unsigned long iova_pfn;
421         struct iova *new_iova;
422
423         iova_pfn = iova_rcache_get(iovad, size, limit_pfn);
424         if (iova_pfn)
425                 return iova_pfn;
426
427 retry:
428         new_iova = alloc_iova(iovad, size, limit_pfn, true);
429         if (!new_iova) {
430                 unsigned int cpu;
431
432                 if (flushed_rcache)
433                         return 0;
434
435                 /* Try replenishing IOVAs by flushing rcache. */
436                 flushed_rcache = true;
437                 for_each_online_cpu(cpu)
438                         free_cpu_cached_iovas(cpu, iovad);
439                 goto retry;
440         }
441
442         return new_iova->pfn_lo;
443 }
444 EXPORT_SYMBOL_GPL(alloc_iova_fast);
445
446 /**
447  * free_iova_fast - free iova pfn range into rcache
448  * @iovad: - iova domain in question.
449  * @pfn: - pfn that is allocated previously
450  * @size: - # of pages in range
451  * This functions frees an iova range by trying to put it into the rcache,
452  * falling back to regular iova deallocation via free_iova() if this fails.
453  */
454 void
455 free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
456 {
457         if (iova_rcache_insert(iovad, pfn, size))
458                 return;
459
460         free_iova(iovad, pfn);
461 }
462 EXPORT_SYMBOL_GPL(free_iova_fast);
463
464 /**
465  * put_iova_domain - destroys the iova doamin
466  * @iovad: - iova domain in question.
467  * All the iova's in that domain are destroyed.
468  */
469 void put_iova_domain(struct iova_domain *iovad)
470 {
471         struct rb_node *node;
472         unsigned long flags;
473
474         free_iova_flush_queue(iovad);
475         free_iova_rcaches(iovad);
476         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
477         node = rb_first(&iovad->rbroot);
478         while (node) {
479                 struct iova *iova = rb_entry(node, struct iova, node);
480
481                 rb_erase(node, &iovad->rbroot);
482                 free_iova_mem(iova);
483                 node = rb_first(&iovad->rbroot);
484         }
485         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
486 }
487 EXPORT_SYMBOL_GPL(put_iova_domain);
488
489 static int
490 __is_range_overlap(struct rb_node *node,
491         unsigned long pfn_lo, unsigned long pfn_hi)
492 {
493         struct iova *iova = rb_entry(node, struct iova, node);
494
495         if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
496                 return 1;
497         return 0;
498 }
499
500 static inline struct iova *
501 alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
502 {
503         struct iova *iova;
504
505         iova = alloc_iova_mem();
506         if (iova) {
507                 iova->pfn_lo = pfn_lo;
508                 iova->pfn_hi = pfn_hi;
509         }
510
511         return iova;
512 }
513
514 static struct iova *
515 __insert_new_range(struct iova_domain *iovad,
516         unsigned long pfn_lo, unsigned long pfn_hi)
517 {
518         struct iova *iova;
519
520         iova = alloc_and_init_iova(pfn_lo, pfn_hi);
521         if (iova)
522                 iova_insert_rbtree(&iovad->rbroot, iova, NULL);
523
524         return iova;
525 }
526
527 static void
528 __adjust_overlap_range(struct iova *iova,
529         unsigned long *pfn_lo, unsigned long *pfn_hi)
530 {
531         if (*pfn_lo < iova->pfn_lo)
532                 iova->pfn_lo = *pfn_lo;
533         if (*pfn_hi > iova->pfn_hi)
534                 *pfn_lo = iova->pfn_hi + 1;
535 }
536
537 /**
538  * reserve_iova - reserves an iova in the given range
539  * @iovad: - iova domain pointer
540  * @pfn_lo: - lower page frame address
541  * @pfn_hi:- higher pfn adderss
542  * This function allocates reserves the address range from pfn_lo to pfn_hi so
543  * that this address is not dished out as part of alloc_iova.
544  */
545 struct iova *
546 reserve_iova(struct iova_domain *iovad,
547         unsigned long pfn_lo, unsigned long pfn_hi)
548 {
549         struct rb_node *node;
550         unsigned long flags;
551         struct iova *iova;
552         unsigned int overlap = 0;
553
554         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
555         for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
556                 if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
557                         iova = rb_entry(node, struct iova, node);
558                         __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
559                         if ((pfn_lo >= iova->pfn_lo) &&
560                                 (pfn_hi <= iova->pfn_hi))
561                                 goto finish;
562                         overlap = 1;
563
564                 } else if (overlap)
565                                 break;
566         }
567
568         /* We are here either because this is the first reserver node
569          * or need to insert remaining non overlap addr range
570          */
571         iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
572 finish:
573
574         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
575         return iova;
576 }
577 EXPORT_SYMBOL_GPL(reserve_iova);
578
579 /**
580  * copy_reserved_iova - copies the reserved between domains
581  * @from: - source doamin from where to copy
582  * @to: - destination domin where to copy
583  * This function copies reserved iova's from one doamin to
584  * other.
585  */
586 void
587 copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
588 {
589         unsigned long flags;
590         struct rb_node *node;
591
592         spin_lock_irqsave(&from->iova_rbtree_lock, flags);
593         for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
594                 struct iova *iova = rb_entry(node, struct iova, node);
595                 struct iova *new_iova;
596
597                 new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
598                 if (!new_iova)
599                         printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
600                                 iova->pfn_lo, iova->pfn_lo);
601         }
602         spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
603 }
604 EXPORT_SYMBOL_GPL(copy_reserved_iova);
605
606 struct iova *
607 split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
608                       unsigned long pfn_lo, unsigned long pfn_hi)
609 {
610         unsigned long flags;
611         struct iova *prev = NULL, *next = NULL;
612
613         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
614         if (iova->pfn_lo < pfn_lo) {
615                 prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
616                 if (prev == NULL)
617                         goto error;
618         }
619         if (iova->pfn_hi > pfn_hi) {
620                 next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
621                 if (next == NULL)
622                         goto error;
623         }
624
625         __cached_rbnode_delete_update(iovad, iova);
626         rb_erase(&iova->node, &iovad->rbroot);
627
628         if (prev) {
629                 iova_insert_rbtree(&iovad->rbroot, prev, NULL);
630                 iova->pfn_lo = pfn_lo;
631         }
632         if (next) {
633                 iova_insert_rbtree(&iovad->rbroot, next, NULL);
634                 iova->pfn_hi = pfn_hi;
635         }
636         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
637
638         return iova;
639
640 error:
641         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
642         if (prev)
643                 free_iova_mem(prev);
644         return NULL;
645 }
646
647 /*
648  * Magazine caches for IOVA ranges.  For an introduction to magazines,
649  * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
650  * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
651  * For simplicity, we use a static magazine size and don't implement the
652  * dynamic size tuning described in the paper.
653  */
654
655 #define IOVA_MAG_SIZE 128
656
657 struct iova_magazine {
658         unsigned long size;
659         unsigned long pfns[IOVA_MAG_SIZE];
660 };
661
662 struct iova_cpu_rcache {
663         spinlock_t lock;
664         struct iova_magazine *loaded;
665         struct iova_magazine *prev;
666 };
667
668 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
669 {
670         return kzalloc(sizeof(struct iova_magazine), flags);
671 }
672
673 static void iova_magazine_free(struct iova_magazine *mag)
674 {
675         kfree(mag);
676 }
677
678 static void
679 iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
680 {
681         unsigned long flags;
682         int i;
683
684         if (!mag)
685                 return;
686
687         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
688
689         for (i = 0 ; i < mag->size; ++i) {
690                 struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
691
692                 BUG_ON(!iova);
693                 private_free_iova(iovad, iova);
694         }
695
696         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
697
698         mag->size = 0;
699 }
700
701 static bool iova_magazine_full(struct iova_magazine *mag)
702 {
703         return (mag && mag->size == IOVA_MAG_SIZE);
704 }
705
706 static bool iova_magazine_empty(struct iova_magazine *mag)
707 {
708         return (!mag || mag->size == 0);
709 }
710
711 static unsigned long iova_magazine_pop(struct iova_magazine *mag,
712                                        unsigned long limit_pfn)
713 {
714         BUG_ON(iova_magazine_empty(mag));
715
716         if (mag->pfns[mag->size - 1] >= limit_pfn)
717                 return 0;
718
719         return mag->pfns[--mag->size];
720 }
721
722 static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
723 {
724         BUG_ON(iova_magazine_full(mag));
725
726         mag->pfns[mag->size++] = pfn;
727 }
728
729 static void init_iova_rcaches(struct iova_domain *iovad)
730 {
731         struct iova_cpu_rcache *cpu_rcache;
732         struct iova_rcache *rcache;
733         unsigned int cpu;
734         int i;
735
736         for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
737                 rcache = &iovad->rcaches[i];
738                 spin_lock_init(&rcache->lock);
739                 rcache->depot_size = 0;
740                 rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
741                 if (WARN_ON(!rcache->cpu_rcaches))
742                         continue;
743                 for_each_possible_cpu(cpu) {
744                         cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
745                         spin_lock_init(&cpu_rcache->lock);
746                         cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
747                         cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
748                 }
749         }
750 }
751
752 /*
753  * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
754  * return true on success.  Can fail if rcache is full and we can't free
755  * space, and free_iova() (our only caller) will then return the IOVA
756  * range to the rbtree instead.
757  */
758 static bool __iova_rcache_insert(struct iova_domain *iovad,
759                                  struct iova_rcache *rcache,
760                                  unsigned long iova_pfn)
761 {
762         struct iova_magazine *mag_to_free = NULL;
763         struct iova_cpu_rcache *cpu_rcache;
764         bool can_insert = false;
765         unsigned long flags;
766
767         cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
768         spin_lock_irqsave(&cpu_rcache->lock, flags);
769
770         if (!iova_magazine_full(cpu_rcache->loaded)) {
771                 can_insert = true;
772         } else if (!iova_magazine_full(cpu_rcache->prev)) {
773                 swap(cpu_rcache->prev, cpu_rcache->loaded);
774                 can_insert = true;
775         } else {
776                 struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
777
778                 if (new_mag) {
779                         spin_lock(&rcache->lock);
780                         if (rcache->depot_size < MAX_GLOBAL_MAGS) {
781                                 rcache->depot[rcache->depot_size++] =
782                                                 cpu_rcache->loaded;
783                         } else {
784                                 mag_to_free = cpu_rcache->loaded;
785                         }
786                         spin_unlock(&rcache->lock);
787
788                         cpu_rcache->loaded = new_mag;
789                         can_insert = true;
790                 }
791         }
792
793         if (can_insert)
794                 iova_magazine_push(cpu_rcache->loaded, iova_pfn);
795
796         spin_unlock_irqrestore(&cpu_rcache->lock, flags);
797
798         if (mag_to_free) {
799                 iova_magazine_free_pfns(mag_to_free, iovad);
800                 iova_magazine_free(mag_to_free);
801         }
802
803         return can_insert;
804 }
805
806 static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
807                                unsigned long size)
808 {
809         unsigned int log_size = order_base_2(size);
810
811         if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
812                 return false;
813
814         return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
815 }
816
817 /*
818  * Caller wants to allocate a new IOVA range from 'rcache'.  If we can
819  * satisfy the request, return a matching non-NULL range and remove
820  * it from the 'rcache'.
821  */
822 static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
823                                        unsigned long limit_pfn)
824 {
825         struct iova_cpu_rcache *cpu_rcache;
826         unsigned long iova_pfn = 0;
827         bool has_pfn = false;
828         unsigned long flags;
829
830         cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
831         spin_lock_irqsave(&cpu_rcache->lock, flags);
832
833         if (!iova_magazine_empty(cpu_rcache->loaded)) {
834                 has_pfn = true;
835         } else if (!iova_magazine_empty(cpu_rcache->prev)) {
836                 swap(cpu_rcache->prev, cpu_rcache->loaded);
837                 has_pfn = true;
838         } else {
839                 spin_lock(&rcache->lock);
840                 if (rcache->depot_size > 0) {
841                         iova_magazine_free(cpu_rcache->loaded);
842                         cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
843                         has_pfn = true;
844                 }
845                 spin_unlock(&rcache->lock);
846         }
847
848         if (has_pfn)
849                 iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
850
851         spin_unlock_irqrestore(&cpu_rcache->lock, flags);
852
853         return iova_pfn;
854 }
855
856 /*
857  * Try to satisfy IOVA allocation range from rcache.  Fail if requested
858  * size is too big or the DMA limit we are given isn't satisfied by the
859  * top element in the magazine.
860  */
861 static unsigned long iova_rcache_get(struct iova_domain *iovad,
862                                      unsigned long size,
863                                      unsigned long limit_pfn)
864 {
865         unsigned int log_size = order_base_2(size);
866
867         if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
868                 return 0;
869
870         return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn);
871 }
872
873 /*
874  * Free a cpu's rcache.
875  */
876 static void free_cpu_iova_rcache(unsigned int cpu, struct iova_domain *iovad,
877                                  struct iova_rcache *rcache)
878 {
879         struct iova_cpu_rcache *cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
880         unsigned long flags;
881
882         spin_lock_irqsave(&cpu_rcache->lock, flags);
883
884         iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
885         iova_magazine_free(cpu_rcache->loaded);
886
887         iova_magazine_free_pfns(cpu_rcache->prev, iovad);
888         iova_magazine_free(cpu_rcache->prev);
889
890         spin_unlock_irqrestore(&cpu_rcache->lock, flags);
891 }
892
893 /*
894  * free rcache data structures.
895  */
896 static void free_iova_rcaches(struct iova_domain *iovad)
897 {
898         struct iova_rcache *rcache;
899         unsigned long flags;
900         unsigned int cpu;
901         int i, j;
902
903         for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
904                 rcache = &iovad->rcaches[i];
905                 for_each_possible_cpu(cpu)
906                         free_cpu_iova_rcache(cpu, iovad, rcache);
907                 spin_lock_irqsave(&rcache->lock, flags);
908                 free_percpu(rcache->cpu_rcaches);
909                 for (j = 0; j < rcache->depot_size; ++j) {
910                         iova_magazine_free_pfns(rcache->depot[j], iovad);
911                         iova_magazine_free(rcache->depot[j]);
912                 }
913                 spin_unlock_irqrestore(&rcache->lock, flags);
914         }
915 }
916
917 /*
918  * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
919  */
920 void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
921 {
922         struct iova_cpu_rcache *cpu_rcache;
923         struct iova_rcache *rcache;
924         unsigned long flags;
925         int i;
926
927         for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
928                 rcache = &iovad->rcaches[i];
929                 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
930                 spin_lock_irqsave(&cpu_rcache->lock, flags);
931                 iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
932                 iova_magazine_free_pfns(cpu_rcache->prev, iovad);
933                 spin_unlock_irqrestore(&cpu_rcache->lock, flags);
934         }
935 }
936
937 MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
938 MODULE_LICENSE("GPL");