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