ecdb9cf1a34c734866ff9a384f0e278f3cebeca9
[pandora-kernel.git] / arch / arm / mm / dma-mapping.c
1 /*
2  *  linux/arch/arm/mm/dma-mapping.c
3  *
4  *  Copyright (C) 2000-2004 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  DMA uncached mapping support.
11  */
12 #include <linux/module.h>
13 #include <linux/mm.h>
14 #include <linux/gfp.h>
15 #include <linux/errno.h>
16 #include <linux/list.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/highmem.h>
21 #include <linux/slab.h>
22 #include <linux/iommu.h>
23 #include <linux/vmalloc.h>
24
25 #include <asm/memory.h>
26 #include <asm/highmem.h>
27 #include <asm/cacheflush.h>
28 #include <asm/tlbflush.h>
29 #include <asm/sizes.h>
30 #include <asm/mach/arch.h>
31 #include <asm/dma-iommu.h>
32
33 #include "mm.h"
34
35 /*
36  * The DMA API is built upon the notion of "buffer ownership".  A buffer
37  * is either exclusively owned by the CPU (and therefore may be accessed
38  * by it) or exclusively owned by the DMA device.  These helper functions
39  * represent the transitions between these two ownership states.
40  *
41  * Note, however, that on later ARMs, this notion does not work due to
42  * speculative prefetches.  We model our approach on the assumption that
43  * the CPU does do speculative prefetches, which means we clean caches
44  * before transfers and delay cache invalidation until transfer completion.
45  *
46  */
47 static void __dma_page_cpu_to_dev(struct page *, unsigned long,
48                 size_t, enum dma_data_direction);
49 static void __dma_page_dev_to_cpu(struct page *, unsigned long,
50                 size_t, enum dma_data_direction);
51
52 /**
53  * arm_dma_map_page - map a portion of a page for streaming DMA
54  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
55  * @page: page that buffer resides in
56  * @offset: offset into page for start of buffer
57  * @size: size of buffer to map
58  * @dir: DMA transfer direction
59  *
60  * Ensure that any data held in the cache is appropriately discarded
61  * or written back.
62  *
63  * The device owns this memory once this call has completed.  The CPU
64  * can regain ownership by calling dma_unmap_page().
65  */
66 static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
67              unsigned long offset, size_t size, enum dma_data_direction dir,
68              struct dma_attrs *attrs)
69 {
70         if (!arch_is_coherent())
71                 __dma_page_cpu_to_dev(page, offset, size, dir);
72         return pfn_to_dma(dev, page_to_pfn(page)) + offset;
73 }
74
75 /**
76  * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
77  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
78  * @handle: DMA address of buffer
79  * @size: size of buffer (same as passed to dma_map_page)
80  * @dir: DMA transfer direction (same as passed to dma_map_page)
81  *
82  * Unmap a page streaming mode DMA translation.  The handle and size
83  * must match what was provided in the previous dma_map_page() call.
84  * All other usages are undefined.
85  *
86  * After this call, reads by the CPU to the buffer are guaranteed to see
87  * whatever the device wrote there.
88  */
89 static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
90                 size_t size, enum dma_data_direction dir,
91                 struct dma_attrs *attrs)
92 {
93         if (!arch_is_coherent())
94                 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
95                                       handle & ~PAGE_MASK, size, dir);
96 }
97
98 static void arm_dma_sync_single_for_cpu(struct device *dev,
99                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
100 {
101         unsigned int offset = handle & (PAGE_SIZE - 1);
102         struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
103         if (!arch_is_coherent())
104                 __dma_page_dev_to_cpu(page, offset, size, dir);
105 }
106
107 static void arm_dma_sync_single_for_device(struct device *dev,
108                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
109 {
110         unsigned int offset = handle & (PAGE_SIZE - 1);
111         struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
112         if (!arch_is_coherent())
113                 __dma_page_cpu_to_dev(page, offset, size, dir);
114 }
115
116 static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
117
118 struct dma_map_ops arm_dma_ops = {
119         .alloc                  = arm_dma_alloc,
120         .free                   = arm_dma_free,
121         .mmap                   = arm_dma_mmap,
122         .map_page               = arm_dma_map_page,
123         .unmap_page             = arm_dma_unmap_page,
124         .map_sg                 = arm_dma_map_sg,
125         .unmap_sg               = arm_dma_unmap_sg,
126         .sync_single_for_cpu    = arm_dma_sync_single_for_cpu,
127         .sync_single_for_device = arm_dma_sync_single_for_device,
128         .sync_sg_for_cpu        = arm_dma_sync_sg_for_cpu,
129         .sync_sg_for_device     = arm_dma_sync_sg_for_device,
130         .set_dma_mask           = arm_dma_set_mask,
131 };
132 EXPORT_SYMBOL(arm_dma_ops);
133
134 static u64 get_coherent_dma_mask(struct device *dev)
135 {
136         u64 mask = (u64)arm_dma_limit;
137
138         if (dev) {
139                 mask = dev->coherent_dma_mask;
140
141                 /*
142                  * Sanity check the DMA mask - it must be non-zero, and
143                  * must be able to be satisfied by a DMA allocation.
144                  */
145                 if (mask == 0) {
146                         dev_warn(dev, "coherent DMA mask is unset\n");
147                         return 0;
148                 }
149
150                 if ((~mask) & (u64)arm_dma_limit) {
151                         dev_warn(dev, "coherent DMA mask %#llx is smaller "
152                                  "than system GFP_DMA mask %#llx\n",
153                                  mask, (u64)arm_dma_limit);
154                         return 0;
155                 }
156         }
157
158         return mask;
159 }
160
161 static void __dma_clear_buffer(struct page *page, size_t size)
162 {
163         void *ptr;
164         /*
165          * Ensure that the allocated pages are zeroed, and that any data
166          * lurking in the kernel direct-mapped region is invalidated.
167          */
168         ptr = page_address(page);
169         if (ptr) {
170                 memset(ptr, 0, size);
171                 dmac_flush_range(ptr, ptr + size);
172                 outer_flush_range(__pa(ptr), __pa(ptr) + size);
173         }
174 }
175
176 /*
177  * Allocate a DMA buffer for 'dev' of size 'size' using the
178  * specified gfp mask.  Note that 'size' must be page aligned.
179  */
180 static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
181 {
182         unsigned long order = get_order(size);
183         struct page *page, *p, *e;
184         u64 mask = get_coherent_dma_mask(dev);
185
186 #ifdef CONFIG_DMA_API_DEBUG
187         u64 limit = (mask + 1) & ~mask;
188         if (limit && size >= limit) {
189                 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
190                         size, mask);
191                 return NULL;
192         }
193 #endif
194
195         if (!mask)
196                 return NULL;
197
198         if (mask < 0xffffffffULL)
199                 gfp |= GFP_DMA;
200
201         page = alloc_pages(gfp, order);
202         if (!page)
203                 return NULL;
204
205         /*
206          * Now split the huge page and free the excess pages
207          */
208         split_page(page, order);
209         for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
210                 __free_page(p);
211
212         __dma_clear_buffer(page, size);
213
214         return page;
215 }
216
217 /*
218  * Free a DMA buffer.  'size' must be page aligned.
219  */
220 static void __dma_free_buffer(struct page *page, size_t size)
221 {
222         struct page *e = page + (size >> PAGE_SHIFT);
223
224         while (page < e) {
225                 __free_page(page);
226                 page++;
227         }
228 }
229
230 #ifdef CONFIG_MMU
231
232 #define CONSISTENT_OFFSET(x)    (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
233 #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
234
235 /*
236  * These are the page tables (2MB each) covering uncached, DMA consistent allocations
237  */
238 static pte_t **consistent_pte;
239
240 #define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
241
242 unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
243
244 void __init init_consistent_dma_size(unsigned long size)
245 {
246         unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
247
248         BUG_ON(consistent_pte); /* Check we're called before DMA region init */
249         BUG_ON(base < VMALLOC_END);
250
251         /* Grow region to accommodate specified size  */
252         if (base < consistent_base)
253                 consistent_base = base;
254 }
255
256 #include "vmregion.h"
257
258 static struct arm_vmregion_head consistent_head = {
259         .vm_lock        = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
260         .vm_list        = LIST_HEAD_INIT(consistent_head.vm_list),
261         .vm_end         = CONSISTENT_END,
262 };
263
264 #ifdef CONFIG_HUGETLB_PAGE
265 #warning ARM Coherent DMA allocator does not (yet) support huge TLB
266 #endif
267
268 /*
269  * Initialise the consistent memory allocation.
270  */
271 static int __init consistent_init(void)
272 {
273         int ret = 0;
274         pgd_t *pgd;
275         pud_t *pud;
276         pmd_t *pmd;
277         pte_t *pte;
278         int i = 0;
279         unsigned long base = consistent_base;
280         unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
281
282         consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
283         if (!consistent_pte) {
284                 pr_err("%s: no memory\n", __func__);
285                 return -ENOMEM;
286         }
287
288         pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
289         consistent_head.vm_start = base;
290
291         do {
292                 pgd = pgd_offset(&init_mm, base);
293
294                 pud = pud_alloc(&init_mm, pgd, base);
295                 if (!pud) {
296                         pr_err("%s: no pud tables\n", __func__);
297                         ret = -ENOMEM;
298                         break;
299                 }
300
301                 pmd = pmd_alloc(&init_mm, pud, base);
302                 if (!pmd) {
303                         pr_err("%s: no pmd tables\n", __func__);
304                         ret = -ENOMEM;
305                         break;
306                 }
307                 WARN_ON(!pmd_none(*pmd));
308
309                 pte = pte_alloc_kernel(pmd, base);
310                 if (!pte) {
311                         pr_err("%s: no pte tables\n", __func__);
312                         ret = -ENOMEM;
313                         break;
314                 }
315
316                 consistent_pte[i++] = pte;
317                 base += PMD_SIZE;
318         } while (base < CONSISTENT_END);
319
320         return ret;
321 }
322
323 core_initcall(consistent_init);
324
325 static void *
326 __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
327         const void *caller)
328 {
329         struct arm_vmregion *c;
330         size_t align;
331         int bit;
332
333         if (!consistent_pte) {
334                 pr_err("%s: not initialised\n", __func__);
335                 dump_stack();
336                 return NULL;
337         }
338
339         /*
340          * Align the virtual region allocation - maximum alignment is
341          * a section size, minimum is a page size.  This helps reduce
342          * fragmentation of the DMA space, and also prevents allocations
343          * smaller than a section from crossing a section boundary.
344          */
345         bit = fls(size - 1);
346         if (bit > SECTION_SHIFT)
347                 bit = SECTION_SHIFT;
348         align = 1 << bit;
349
350         /*
351          * Allocate a virtual address in the consistent mapping region.
352          */
353         c = arm_vmregion_alloc(&consistent_head, align, size,
354                             gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
355         if (c) {
356                 pte_t *pte;
357                 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
358                 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
359
360                 pte = consistent_pte[idx] + off;
361                 c->priv = page;
362
363                 do {
364                         BUG_ON(!pte_none(*pte));
365
366                         set_pte_ext(pte, mk_pte(page, prot), 0);
367                         page++;
368                         pte++;
369                         off++;
370                         if (off >= PTRS_PER_PTE) {
371                                 off = 0;
372                                 pte = consistent_pte[++idx];
373                         }
374                 } while (size -= PAGE_SIZE);
375
376                 dsb();
377
378                 return (void *)c->vm_start;
379         }
380         return NULL;
381 }
382
383 static void __dma_free_remap(void *cpu_addr, size_t size)
384 {
385         struct arm_vmregion *c;
386         unsigned long addr;
387         pte_t *ptep;
388         int idx;
389         u32 off;
390
391         c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
392         if (!c) {
393                 pr_err("%s: trying to free invalid coherent area: %p\n",
394                        __func__, cpu_addr);
395                 dump_stack();
396                 return;
397         }
398
399         if ((c->vm_end - c->vm_start) != size) {
400                 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
401                        __func__, c->vm_end - c->vm_start, size);
402                 dump_stack();
403                 size = c->vm_end - c->vm_start;
404         }
405
406         idx = CONSISTENT_PTE_INDEX(c->vm_start);
407         off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
408         ptep = consistent_pte[idx] + off;
409         addr = c->vm_start;
410         do {
411                 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
412
413                 ptep++;
414                 addr += PAGE_SIZE;
415                 off++;
416                 if (off >= PTRS_PER_PTE) {
417                         off = 0;
418                         ptep = consistent_pte[++idx];
419                 }
420
421                 if (pte_none(pte) || !pte_present(pte))
422                         pr_crit("%s: bad page in kernel page table\n",
423                                 __func__);
424         } while (size -= PAGE_SIZE);
425
426         flush_tlb_kernel_range(c->vm_start, c->vm_end);
427
428         arm_vmregion_free(&consistent_head, c);
429 }
430
431 static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
432 {
433         prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
434                             pgprot_writecombine(prot) :
435                             pgprot_dmacoherent(prot);
436         return prot;
437 }
438
439 #else   /* !CONFIG_MMU */
440
441 #define __dma_alloc_remap(page, size, gfp, prot, c)     page_address(page)
442 #define __dma_free_remap(addr, size)                    do { } while (0)
443 #define __get_dma_pgprot(attrs, prot)   __pgprot(0)
444
445 #endif  /* CONFIG_MMU */
446
447 static void *
448 __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
449             pgprot_t prot, const void *caller)
450 {
451         struct page *page;
452         void *addr;
453
454         /*
455          * Following is a work-around (a.k.a. hack) to prevent pages
456          * with __GFP_COMP being passed to split_page() which cannot
457          * handle them.  The real problem is that this flag probably
458          * should be 0 on ARM as it is not supported on this
459          * platform; see CONFIG_HUGETLBFS.
460          */
461         gfp &= ~(__GFP_COMP);
462
463         *handle = DMA_ERROR_CODE;
464         size = PAGE_ALIGN(size);
465
466         page = __dma_alloc_buffer(dev, size, gfp);
467         if (!page)
468                 return NULL;
469
470         if (!arch_is_coherent())
471                 addr = __dma_alloc_remap(page, size, gfp, prot, caller);
472         else
473                 addr = page_address(page);
474
475         if (addr)
476                 *handle = pfn_to_dma(dev, page_to_pfn(page));
477         else
478                 __dma_free_buffer(page, size);
479
480         return addr;
481 }
482
483 /*
484  * Allocate DMA-coherent memory space and return both the kernel remapped
485  * virtual and bus address for that space.
486  */
487 void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
488                     gfp_t gfp, struct dma_attrs *attrs)
489 {
490         pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
491         void *memory;
492
493         if (dma_alloc_from_coherent(dev, size, handle, &memory))
494                 return memory;
495
496         return __dma_alloc(dev, size, handle, gfp, prot,
497                            __builtin_return_address(0));
498 }
499
500 /*
501  * Create userspace mapping for the DMA-coherent memory.
502  */
503 int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
504                  void *cpu_addr, dma_addr_t dma_addr, size_t size,
505                  struct dma_attrs *attrs)
506 {
507         int ret = -ENXIO;
508 #ifdef CONFIG_MMU
509         unsigned long user_size, kern_size;
510         struct arm_vmregion *c;
511
512         vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
513
514         if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
515                 return ret;
516
517         user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
518
519         c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
520         if (c) {
521                 unsigned long off = vma->vm_pgoff;
522                 struct page *pages = c->priv;
523
524                 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
525
526                 if (off < kern_size &&
527                     user_size <= (kern_size - off)) {
528                         ret = remap_pfn_range(vma, vma->vm_start,
529                                               page_to_pfn(pages) + off,
530                                               user_size << PAGE_SHIFT,
531                                               vma->vm_page_prot);
532                 }
533         }
534 #endif  /* CONFIG_MMU */
535
536         return ret;
537 }
538
539 /*
540  * free a page as defined by the above mapping.
541  * Must not be called with IRQs disabled.
542  */
543 void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
544                   dma_addr_t handle, struct dma_attrs *attrs)
545 {
546         WARN_ON(irqs_disabled());
547
548         if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
549                 return;
550
551         size = PAGE_ALIGN(size);
552
553         if (!arch_is_coherent())
554                 __dma_free_remap(cpu_addr, size);
555
556         __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size);
557 }
558
559 static void dma_cache_maint_page(struct page *page, unsigned long offset,
560         size_t size, enum dma_data_direction dir,
561         void (*op)(const void *, size_t, int))
562 {
563         unsigned long pfn;
564         size_t left = size;
565
566         pfn = page_to_pfn(page) + offset / PAGE_SIZE;
567         offset %= PAGE_SIZE;
568
569         /*
570          * A single sg entry may refer to multiple physically contiguous
571          * pages.  But we still need to process highmem pages individually.
572          * If highmem is not configured then the bulk of this loop gets
573          * optimized out.
574          */
575         do {
576                 size_t len = left;
577                 void *vaddr;
578
579                 page = pfn_to_page(pfn);
580
581                 if (PageHighMem(page)) {
582                         if (len + offset > PAGE_SIZE)
583                                 len = PAGE_SIZE - offset;
584                         vaddr = kmap_high_get(page);
585                         if (vaddr) {
586                                 vaddr += offset;
587                                 op(vaddr, len, dir);
588                                 kunmap_high(page);
589                         } else if (cache_is_vipt()) {
590                                 /* unmapped pages might still be cached */
591                                 vaddr = kmap_atomic(page);
592                                 op(vaddr + offset, len, dir);
593                                 kunmap_atomic(vaddr);
594                         }
595                 } else {
596                         vaddr = page_address(page) + offset;
597                         op(vaddr, len, dir);
598                 }
599                 offset = 0;
600                 pfn++;
601                 left -= len;
602         } while (left);
603 }
604
605 /*
606  * Make an area consistent for devices.
607  * Note: Drivers should NOT use this function directly, as it will break
608  * platforms with CONFIG_DMABOUNCE.
609  * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
610  */
611 static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
612         size_t size, enum dma_data_direction dir)
613 {
614         unsigned long paddr;
615
616         dma_cache_maint_page(page, off, size, dir, dmac_map_area);
617
618         paddr = page_to_phys(page) + off;
619         if (dir == DMA_FROM_DEVICE) {
620                 outer_inv_range(paddr, paddr + size);
621         } else {
622                 outer_clean_range(paddr, paddr + size);
623         }
624         /* FIXME: non-speculating: flush on bidirectional mappings? */
625 }
626
627 static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
628         size_t size, enum dma_data_direction dir)
629 {
630         unsigned long paddr = page_to_phys(page) + off;
631
632         /* FIXME: non-speculating: not required */
633         /* don't bother invalidating if DMA to device */
634         if (dir != DMA_TO_DEVICE)
635                 outer_inv_range(paddr, paddr + size);
636
637         dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
638
639         /*
640          * Mark the D-cache clean for this page to avoid extra flushing.
641          */
642         if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
643                 set_bit(PG_dcache_clean, &page->flags);
644 }
645
646 /**
647  * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
648  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
649  * @sg: list of buffers
650  * @nents: number of buffers to map
651  * @dir: DMA transfer direction
652  *
653  * Map a set of buffers described by scatterlist in streaming mode for DMA.
654  * This is the scatter-gather version of the dma_map_single interface.
655  * Here the scatter gather list elements are each tagged with the
656  * appropriate dma address and length.  They are obtained via
657  * sg_dma_{address,length}.
658  *
659  * Device ownership issues as mentioned for dma_map_single are the same
660  * here.
661  */
662 int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
663                 enum dma_data_direction dir, struct dma_attrs *attrs)
664 {
665         struct dma_map_ops *ops = get_dma_ops(dev);
666         struct scatterlist *s;
667         int i, j;
668
669         for_each_sg(sg, s, nents, i) {
670 #ifdef CONFIG_NEED_SG_DMA_LENGTH
671                 s->dma_length = s->length;
672 #endif
673                 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
674                                                 s->length, dir, attrs);
675                 if (dma_mapping_error(dev, s->dma_address))
676                         goto bad_mapping;
677         }
678         return nents;
679
680  bad_mapping:
681         for_each_sg(sg, s, i, j)
682                 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
683         return 0;
684 }
685
686 /**
687  * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
688  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
689  * @sg: list of buffers
690  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
691  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
692  *
693  * Unmap a set of streaming mode DMA translations.  Again, CPU access
694  * rules concerning calls here are the same as for dma_unmap_single().
695  */
696 void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
697                 enum dma_data_direction dir, struct dma_attrs *attrs)
698 {
699         struct dma_map_ops *ops = get_dma_ops(dev);
700         struct scatterlist *s;
701
702         int i;
703
704         for_each_sg(sg, s, nents, i)
705                 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
706 }
707
708 /**
709  * arm_dma_sync_sg_for_cpu
710  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
711  * @sg: list of buffers
712  * @nents: number of buffers to map (returned from dma_map_sg)
713  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
714  */
715 void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
716                         int nents, enum dma_data_direction dir)
717 {
718         struct dma_map_ops *ops = get_dma_ops(dev);
719         struct scatterlist *s;
720         int i;
721
722         for_each_sg(sg, s, nents, i)
723                 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
724                                          dir);
725 }
726
727 /**
728  * arm_dma_sync_sg_for_device
729  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
730  * @sg: list of buffers
731  * @nents: number of buffers to map (returned from dma_map_sg)
732  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
733  */
734 void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
735                         int nents, enum dma_data_direction dir)
736 {
737         struct dma_map_ops *ops = get_dma_ops(dev);
738         struct scatterlist *s;
739         int i;
740
741         for_each_sg(sg, s, nents, i)
742                 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
743                                             dir);
744 }
745
746 /*
747  * Return whether the given device DMA address mask can be supported
748  * properly.  For example, if your device can only drive the low 24-bits
749  * during bus mastering, then you would pass 0x00ffffff as the mask
750  * to this function.
751  */
752 int dma_supported(struct device *dev, u64 mask)
753 {
754         if (mask < (u64)arm_dma_limit)
755                 return 0;
756         return 1;
757 }
758 EXPORT_SYMBOL(dma_supported);
759
760 static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
761 {
762         if (!dev->dma_mask || !dma_supported(dev, dma_mask))
763                 return -EIO;
764
765         *dev->dma_mask = dma_mask;
766
767         return 0;
768 }
769
770 #define PREALLOC_DMA_DEBUG_ENTRIES      4096
771
772 static int __init dma_debug_do_init(void)
773 {
774 #ifdef CONFIG_MMU
775         arm_vmregion_create_proc("dma-mappings", &consistent_head);
776 #endif
777         dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
778         return 0;
779 }
780 fs_initcall(dma_debug_do_init);
781
782 #ifdef CONFIG_ARM_DMA_USE_IOMMU
783
784 /* IOMMU */
785
786 static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
787                                       size_t size)
788 {
789         unsigned int order = get_order(size);
790         unsigned int align = 0;
791         unsigned int count, start;
792         unsigned long flags;
793
794         count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
795                  (1 << mapping->order) - 1) >> mapping->order;
796
797         if (order > mapping->order)
798                 align = (1 << (order - mapping->order)) - 1;
799
800         spin_lock_irqsave(&mapping->lock, flags);
801         start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
802                                            count, align);
803         if (start > mapping->bits) {
804                 spin_unlock_irqrestore(&mapping->lock, flags);
805                 return DMA_ERROR_CODE;
806         }
807
808         bitmap_set(mapping->bitmap, start, count);
809         spin_unlock_irqrestore(&mapping->lock, flags);
810
811         return mapping->base + (start << (mapping->order + PAGE_SHIFT));
812 }
813
814 static inline void __free_iova(struct dma_iommu_mapping *mapping,
815                                dma_addr_t addr, size_t size)
816 {
817         unsigned int start = (addr - mapping->base) >>
818                              (mapping->order + PAGE_SHIFT);
819         unsigned int count = ((size >> PAGE_SHIFT) +
820                               (1 << mapping->order) - 1) >> mapping->order;
821         unsigned long flags;
822
823         spin_lock_irqsave(&mapping->lock, flags);
824         bitmap_clear(mapping->bitmap, start, count);
825         spin_unlock_irqrestore(&mapping->lock, flags);
826 }
827
828 static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
829 {
830         struct page **pages;
831         int count = size >> PAGE_SHIFT;
832         int array_size = count * sizeof(struct page *);
833         int i = 0;
834
835         if (array_size <= PAGE_SIZE)
836                 pages = kzalloc(array_size, gfp);
837         else
838                 pages = vzalloc(array_size);
839         if (!pages)
840                 return NULL;
841
842         while (count) {
843                 int j, order = __ffs(count);
844
845                 pages[i] = alloc_pages(gfp | __GFP_NOWARN, order);
846                 while (!pages[i] && order)
847                         pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order);
848                 if (!pages[i])
849                         goto error;
850
851                 if (order)
852                         split_page(pages[i], order);
853                 j = 1 << order;
854                 while (--j)
855                         pages[i + j] = pages[i] + j;
856
857                 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
858                 i += 1 << order;
859                 count -= 1 << order;
860         }
861
862         return pages;
863 error:
864         while (--i)
865                 if (pages[i])
866                         __free_pages(pages[i], 0);
867         if (array_size < PAGE_SIZE)
868                 kfree(pages);
869         else
870                 vfree(pages);
871         return NULL;
872 }
873
874 static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size)
875 {
876         int count = size >> PAGE_SHIFT;
877         int array_size = count * sizeof(struct page *);
878         int i;
879         for (i = 0; i < count; i++)
880                 if (pages[i])
881                         __free_pages(pages[i], 0);
882         if (array_size < PAGE_SIZE)
883                 kfree(pages);
884         else
885                 vfree(pages);
886         return 0;
887 }
888
889 /*
890  * Create a CPU mapping for a specified pages
891  */
892 static void *
893 __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot)
894 {
895         struct arm_vmregion *c;
896         size_t align;
897         size_t count = size >> PAGE_SHIFT;
898         int bit;
899
900         if (!consistent_pte[0]) {
901                 pr_err("%s: not initialised\n", __func__);
902                 dump_stack();
903                 return NULL;
904         }
905
906         /*
907          * Align the virtual region allocation - maximum alignment is
908          * a section size, minimum is a page size.  This helps reduce
909          * fragmentation of the DMA space, and also prevents allocations
910          * smaller than a section from crossing a section boundary.
911          */
912         bit = fls(size - 1);
913         if (bit > SECTION_SHIFT)
914                 bit = SECTION_SHIFT;
915         align = 1 << bit;
916
917         /*
918          * Allocate a virtual address in the consistent mapping region.
919          */
920         c = arm_vmregion_alloc(&consistent_head, align, size,
921                             gfp & ~(__GFP_DMA | __GFP_HIGHMEM), NULL);
922         if (c) {
923                 pte_t *pte;
924                 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
925                 int i = 0;
926                 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
927
928                 pte = consistent_pte[idx] + off;
929                 c->priv = pages;
930
931                 do {
932                         BUG_ON(!pte_none(*pte));
933
934                         set_pte_ext(pte, mk_pte(pages[i], prot), 0);
935                         pte++;
936                         off++;
937                         i++;
938                         if (off >= PTRS_PER_PTE) {
939                                 off = 0;
940                                 pte = consistent_pte[++idx];
941                         }
942                 } while (i < count);
943
944                 dsb();
945
946                 return (void *)c->vm_start;
947         }
948         return NULL;
949 }
950
951 /*
952  * Create a mapping in device IO address space for specified pages
953  */
954 static dma_addr_t
955 __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
956 {
957         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
958         unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
959         dma_addr_t dma_addr, iova;
960         int i, ret = DMA_ERROR_CODE;
961
962         dma_addr = __alloc_iova(mapping, size);
963         if (dma_addr == DMA_ERROR_CODE)
964                 return dma_addr;
965
966         iova = dma_addr;
967         for (i = 0; i < count; ) {
968                 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
969                 phys_addr_t phys = page_to_phys(pages[i]);
970                 unsigned int len, j;
971
972                 for (j = i + 1; j < count; j++, next_pfn++)
973                         if (page_to_pfn(pages[j]) != next_pfn)
974                                 break;
975
976                 len = (j - i) << PAGE_SHIFT;
977                 ret = iommu_map(mapping->domain, iova, phys, len, 0);
978                 if (ret < 0)
979                         goto fail;
980                 iova += len;
981                 i = j;
982         }
983         return dma_addr;
984 fail:
985         iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
986         __free_iova(mapping, dma_addr, size);
987         return DMA_ERROR_CODE;
988 }
989
990 static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
991 {
992         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
993
994         /*
995          * add optional in-page offset from iova to size and align
996          * result to page size
997          */
998         size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
999         iova &= PAGE_MASK;
1000
1001         iommu_unmap(mapping->domain, iova, size);
1002         __free_iova(mapping, iova, size);
1003         return 0;
1004 }
1005
1006 static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1007             dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1008 {
1009         pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
1010         struct page **pages;
1011         void *addr = NULL;
1012
1013         *handle = DMA_ERROR_CODE;
1014         size = PAGE_ALIGN(size);
1015
1016         pages = __iommu_alloc_buffer(dev, size, gfp);
1017         if (!pages)
1018                 return NULL;
1019
1020         *handle = __iommu_create_mapping(dev, pages, size);
1021         if (*handle == DMA_ERROR_CODE)
1022                 goto err_buffer;
1023
1024         addr = __iommu_alloc_remap(pages, size, gfp, prot);
1025         if (!addr)
1026                 goto err_mapping;
1027
1028         return addr;
1029
1030 err_mapping:
1031         __iommu_remove_mapping(dev, *handle, size);
1032 err_buffer:
1033         __iommu_free_buffer(dev, pages, size);
1034         return NULL;
1035 }
1036
1037 static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1038                     void *cpu_addr, dma_addr_t dma_addr, size_t size,
1039                     struct dma_attrs *attrs)
1040 {
1041         struct arm_vmregion *c;
1042
1043         vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1044         c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
1045
1046         if (c) {
1047                 struct page **pages = c->priv;
1048
1049                 unsigned long uaddr = vma->vm_start;
1050                 unsigned long usize = vma->vm_end - vma->vm_start;
1051                 int i = 0;
1052
1053                 do {
1054                         int ret;
1055
1056                         ret = vm_insert_page(vma, uaddr, pages[i++]);
1057                         if (ret) {
1058                                 pr_err("Remapping memory, error: %d\n", ret);
1059                                 return ret;
1060                         }
1061
1062                         uaddr += PAGE_SIZE;
1063                         usize -= PAGE_SIZE;
1064                 } while (usize > 0);
1065         }
1066         return 0;
1067 }
1068
1069 /*
1070  * free a page as defined by the above mapping.
1071  * Must not be called with IRQs disabled.
1072  */
1073 void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1074                           dma_addr_t handle, struct dma_attrs *attrs)
1075 {
1076         struct arm_vmregion *c;
1077         size = PAGE_ALIGN(size);
1078
1079         c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
1080         if (c) {
1081                 struct page **pages = c->priv;
1082                 __dma_free_remap(cpu_addr, size);
1083                 __iommu_remove_mapping(dev, handle, size);
1084                 __iommu_free_buffer(dev, pages, size);
1085         }
1086 }
1087
1088 /*
1089  * Map a part of the scatter-gather list into contiguous io address space
1090  */
1091 static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1092                           size_t size, dma_addr_t *handle,
1093                           enum dma_data_direction dir)
1094 {
1095         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1096         dma_addr_t iova, iova_base;
1097         int ret = 0;
1098         unsigned int count;
1099         struct scatterlist *s;
1100
1101         size = PAGE_ALIGN(size);
1102         *handle = DMA_ERROR_CODE;
1103
1104         iova_base = iova = __alloc_iova(mapping, size);
1105         if (iova == DMA_ERROR_CODE)
1106                 return -ENOMEM;
1107
1108         for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1109                 phys_addr_t phys = page_to_phys(sg_page(s));
1110                 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1111
1112                 if (!arch_is_coherent())
1113                         __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1114
1115                 ret = iommu_map(mapping->domain, iova, phys, len, 0);
1116                 if (ret < 0)
1117                         goto fail;
1118                 count += len >> PAGE_SHIFT;
1119                 iova += len;
1120         }
1121         *handle = iova_base;
1122
1123         return 0;
1124 fail:
1125         iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1126         __free_iova(mapping, iova_base, size);
1127         return ret;
1128 }
1129
1130 /**
1131  * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1132  * @dev: valid struct device pointer
1133  * @sg: list of buffers
1134  * @nents: number of buffers to map
1135  * @dir: DMA transfer direction
1136  *
1137  * Map a set of buffers described by scatterlist in streaming mode for DMA.
1138  * The scatter gather list elements are merged together (if possible) and
1139  * tagged with the appropriate dma address and length. They are obtained via
1140  * sg_dma_{address,length}.
1141  */
1142 int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1143                      enum dma_data_direction dir, struct dma_attrs *attrs)
1144 {
1145         struct scatterlist *s = sg, *dma = sg, *start = sg;
1146         int i, count = 0;
1147         unsigned int offset = s->offset;
1148         unsigned int size = s->offset + s->length;
1149         unsigned int max = dma_get_max_seg_size(dev);
1150
1151         for (i = 1; i < nents; i++) {
1152                 s = sg_next(s);
1153
1154                 s->dma_address = DMA_ERROR_CODE;
1155                 s->dma_length = 0;
1156
1157                 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1158                         if (__map_sg_chunk(dev, start, size, &dma->dma_address,
1159                             dir) < 0)
1160                                 goto bad_mapping;
1161
1162                         dma->dma_address += offset;
1163                         dma->dma_length = size - offset;
1164
1165                         size = offset = s->offset;
1166                         start = s;
1167                         dma = sg_next(dma);
1168                         count += 1;
1169                 }
1170                 size += s->length;
1171         }
1172         if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir) < 0)
1173                 goto bad_mapping;
1174
1175         dma->dma_address += offset;
1176         dma->dma_length = size - offset;
1177
1178         return count+1;
1179
1180 bad_mapping:
1181         for_each_sg(sg, s, count, i)
1182                 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1183         return 0;
1184 }
1185
1186 /**
1187  * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1188  * @dev: valid struct device pointer
1189  * @sg: list of buffers
1190  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1191  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1192  *
1193  * Unmap a set of streaming mode DMA translations.  Again, CPU access
1194  * rules concerning calls here are the same as for dma_unmap_single().
1195  */
1196 void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1197                         enum dma_data_direction dir, struct dma_attrs *attrs)
1198 {
1199         struct scatterlist *s;
1200         int i;
1201
1202         for_each_sg(sg, s, nents, i) {
1203                 if (sg_dma_len(s))
1204                         __iommu_remove_mapping(dev, sg_dma_address(s),
1205                                                sg_dma_len(s));
1206                 if (!arch_is_coherent())
1207                         __dma_page_dev_to_cpu(sg_page(s), s->offset,
1208                                               s->length, dir);
1209         }
1210 }
1211
1212 /**
1213  * arm_iommu_sync_sg_for_cpu
1214  * @dev: valid struct device pointer
1215  * @sg: list of buffers
1216  * @nents: number of buffers to map (returned from dma_map_sg)
1217  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1218  */
1219 void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1220                         int nents, enum dma_data_direction dir)
1221 {
1222         struct scatterlist *s;
1223         int i;
1224
1225         for_each_sg(sg, s, nents, i)
1226                 if (!arch_is_coherent())
1227                         __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
1228
1229 }
1230
1231 /**
1232  * arm_iommu_sync_sg_for_device
1233  * @dev: valid struct device pointer
1234  * @sg: list of buffers
1235  * @nents: number of buffers to map (returned from dma_map_sg)
1236  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1237  */
1238 void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1239                         int nents, enum dma_data_direction dir)
1240 {
1241         struct scatterlist *s;
1242         int i;
1243
1244         for_each_sg(sg, s, nents, i)
1245                 if (!arch_is_coherent())
1246                         __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1247 }
1248
1249
1250 /**
1251  * arm_iommu_map_page
1252  * @dev: valid struct device pointer
1253  * @page: page that buffer resides in
1254  * @offset: offset into page for start of buffer
1255  * @size: size of buffer to map
1256  * @dir: DMA transfer direction
1257  *
1258  * IOMMU aware version of arm_dma_map_page()
1259  */
1260 static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1261              unsigned long offset, size_t size, enum dma_data_direction dir,
1262              struct dma_attrs *attrs)
1263 {
1264         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1265         dma_addr_t dma_addr;
1266         int ret, len = PAGE_ALIGN(size + offset);
1267
1268         if (!arch_is_coherent())
1269                 __dma_page_cpu_to_dev(page, offset, size, dir);
1270
1271         dma_addr = __alloc_iova(mapping, len);
1272         if (dma_addr == DMA_ERROR_CODE)
1273                 return dma_addr;
1274
1275         ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0);
1276         if (ret < 0)
1277                 goto fail;
1278
1279         return dma_addr + offset;
1280 fail:
1281         __free_iova(mapping, dma_addr, len);
1282         return DMA_ERROR_CODE;
1283 }
1284
1285 /**
1286  * arm_iommu_unmap_page
1287  * @dev: valid struct device pointer
1288  * @handle: DMA address of buffer
1289  * @size: size of buffer (same as passed to dma_map_page)
1290  * @dir: DMA transfer direction (same as passed to dma_map_page)
1291  *
1292  * IOMMU aware version of arm_dma_unmap_page()
1293  */
1294 static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1295                 size_t size, enum dma_data_direction dir,
1296                 struct dma_attrs *attrs)
1297 {
1298         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1299         dma_addr_t iova = handle & PAGE_MASK;
1300         struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1301         int offset = handle & ~PAGE_MASK;
1302         int len = PAGE_ALIGN(size + offset);
1303
1304         if (!iova)
1305                 return;
1306
1307         if (!arch_is_coherent())
1308                 __dma_page_dev_to_cpu(page, offset, size, dir);
1309
1310         iommu_unmap(mapping->domain, iova, len);
1311         __free_iova(mapping, iova, len);
1312 }
1313
1314 static void arm_iommu_sync_single_for_cpu(struct device *dev,
1315                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1316 {
1317         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1318         dma_addr_t iova = handle & PAGE_MASK;
1319         struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1320         unsigned int offset = handle & ~PAGE_MASK;
1321
1322         if (!iova)
1323                 return;
1324
1325         if (!arch_is_coherent())
1326                 __dma_page_dev_to_cpu(page, offset, size, dir);
1327 }
1328
1329 static void arm_iommu_sync_single_for_device(struct device *dev,
1330                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1331 {
1332         struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1333         dma_addr_t iova = handle & PAGE_MASK;
1334         struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1335         unsigned int offset = handle & ~PAGE_MASK;
1336
1337         if (!iova)
1338                 return;
1339
1340         __dma_page_cpu_to_dev(page, offset, size, dir);
1341 }
1342
1343 struct dma_map_ops iommu_ops = {
1344         .alloc          = arm_iommu_alloc_attrs,
1345         .free           = arm_iommu_free_attrs,
1346         .mmap           = arm_iommu_mmap_attrs,
1347
1348         .map_page               = arm_iommu_map_page,
1349         .unmap_page             = arm_iommu_unmap_page,
1350         .sync_single_for_cpu    = arm_iommu_sync_single_for_cpu,
1351         .sync_single_for_device = arm_iommu_sync_single_for_device,
1352
1353         .map_sg                 = arm_iommu_map_sg,
1354         .unmap_sg               = arm_iommu_unmap_sg,
1355         .sync_sg_for_cpu        = arm_iommu_sync_sg_for_cpu,
1356         .sync_sg_for_device     = arm_iommu_sync_sg_for_device,
1357 };
1358
1359 /**
1360  * arm_iommu_create_mapping
1361  * @bus: pointer to the bus holding the client device (for IOMMU calls)
1362  * @base: start address of the valid IO address space
1363  * @size: size of the valid IO address space
1364  * @order: accuracy of the IO addresses allocations
1365  *
1366  * Creates a mapping structure which holds information about used/unused
1367  * IO address ranges, which is required to perform memory allocation and
1368  * mapping with IOMMU aware functions.
1369  *
1370  * The client device need to be attached to the mapping with
1371  * arm_iommu_attach_device function.
1372  */
1373 struct dma_iommu_mapping *
1374 arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
1375                          int order)
1376 {
1377         unsigned int count = size >> (PAGE_SHIFT + order);
1378         unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
1379         struct dma_iommu_mapping *mapping;
1380         int err = -ENOMEM;
1381
1382         if (!count)
1383                 return ERR_PTR(-EINVAL);
1384
1385         mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1386         if (!mapping)
1387                 goto err;
1388
1389         mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1390         if (!mapping->bitmap)
1391                 goto err2;
1392
1393         mapping->base = base;
1394         mapping->bits = BITS_PER_BYTE * bitmap_size;
1395         mapping->order = order;
1396         spin_lock_init(&mapping->lock);
1397
1398         mapping->domain = iommu_domain_alloc(bus);
1399         if (!mapping->domain)
1400                 goto err3;
1401
1402         kref_init(&mapping->kref);
1403         return mapping;
1404 err3:
1405         kfree(mapping->bitmap);
1406 err2:
1407         kfree(mapping);
1408 err:
1409         return ERR_PTR(err);
1410 }
1411
1412 static void release_iommu_mapping(struct kref *kref)
1413 {
1414         struct dma_iommu_mapping *mapping =
1415                 container_of(kref, struct dma_iommu_mapping, kref);
1416
1417         iommu_domain_free(mapping->domain);
1418         kfree(mapping->bitmap);
1419         kfree(mapping);
1420 }
1421
1422 void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1423 {
1424         if (mapping)
1425                 kref_put(&mapping->kref, release_iommu_mapping);
1426 }
1427
1428 /**
1429  * arm_iommu_attach_device
1430  * @dev: valid struct device pointer
1431  * @mapping: io address space mapping structure (returned from
1432  *      arm_iommu_create_mapping)
1433  *
1434  * Attaches specified io address space mapping to the provided device,
1435  * this replaces the dma operations (dma_map_ops pointer) with the
1436  * IOMMU aware version. More than one client might be attached to
1437  * the same io address space mapping.
1438  */
1439 int arm_iommu_attach_device(struct device *dev,
1440                             struct dma_iommu_mapping *mapping)
1441 {
1442         int err;
1443
1444         err = iommu_attach_device(mapping->domain, dev);
1445         if (err)
1446                 return err;
1447
1448         kref_get(&mapping->kref);
1449         dev->archdata.mapping = mapping;
1450         set_dma_ops(dev, &iommu_ops);
1451
1452         pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev));
1453         return 0;
1454 }
1455
1456 #endif