Linux 3.14-rc4
[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 */
11a5aa32 12#include <linux/bootmem.h>
1da177e4
LT
13#include <linux/module.h>
14#include <linux/mm.h>
5a0e3ad6 15#include <linux/gfp.h>
1da177e4
LT
16#include <linux/errno.h>
17#include <linux/list.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/dma-mapping.h>
c7909509 21#include <linux/dma-contiguous.h>
39af22a7 22#include <linux/highmem.h>
c7909509 23#include <linux/memblock.h>
99d1717d 24#include <linux/slab.h>
4ce63fcd 25#include <linux/iommu.h>
e9da6e99 26#include <linux/io.h>
4ce63fcd 27#include <linux/vmalloc.h>
158e8bfe 28#include <linux/sizes.h>
1da177e4 29
23759dc6 30#include <asm/memory.h>
43377453 31#include <asm/highmem.h>
1da177e4 32#include <asm/cacheflush.h>
1da177e4 33#include <asm/tlbflush.h>
99d1717d 34#include <asm/mach/arch.h>
4ce63fcd 35#include <asm/dma-iommu.h>
c7909509
MS
36#include <asm/mach/map.h>
37#include <asm/system_info.h>
38#include <asm/dma-contiguous.h>
37134cd5 39
022ae537
RK
40#include "mm.h"
41
15237e1f
MS
42/*
43 * The DMA API is built upon the notion of "buffer ownership". A buffer
44 * is either exclusively owned by the CPU (and therefore may be accessed
45 * by it) or exclusively owned by the DMA device. These helper functions
46 * represent the transitions between these two ownership states.
47 *
48 * Note, however, that on later ARMs, this notion does not work due to
49 * speculative prefetches. We model our approach on the assumption that
50 * the CPU does do speculative prefetches, which means we clean caches
51 * before transfers and delay cache invalidation until transfer completion.
52 *
15237e1f 53 */
51fde349 54static void __dma_page_cpu_to_dev(struct page *, unsigned long,
15237e1f 55 size_t, enum dma_data_direction);
51fde349 56static void __dma_page_dev_to_cpu(struct page *, unsigned long,
15237e1f
MS
57 size_t, enum dma_data_direction);
58
2dc6a016
MS
59/**
60 * arm_dma_map_page - map a portion of a page for streaming DMA
61 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
62 * @page: page that buffer resides in
63 * @offset: offset into page for start of buffer
64 * @size: size of buffer to map
65 * @dir: DMA transfer direction
66 *
67 * Ensure that any data held in the cache is appropriately discarded
68 * or written back.
69 *
70 * The device owns this memory once this call has completed. The CPU
71 * can regain ownership by calling dma_unmap_page().
72 */
51fde349 73static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
2dc6a016
MS
74 unsigned long offset, size_t size, enum dma_data_direction dir,
75 struct dma_attrs *attrs)
76{
dd37e940 77 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
51fde349
MS
78 __dma_page_cpu_to_dev(page, offset, size, dir);
79 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
2dc6a016
MS
80}
81
dd37e940
RH
82static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
83 unsigned long offset, size_t size, enum dma_data_direction dir,
84 struct dma_attrs *attrs)
85{
86 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
87}
88
2dc6a016
MS
89/**
90 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
91 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
92 * @handle: DMA address of buffer
93 * @size: size of buffer (same as passed to dma_map_page)
94 * @dir: DMA transfer direction (same as passed to dma_map_page)
95 *
96 * Unmap a page streaming mode DMA translation. The handle and size
97 * must match what was provided in the previous dma_map_page() call.
98 * All other usages are undefined.
99 *
100 * After this call, reads by the CPU to the buffer are guaranteed to see
101 * whatever the device wrote there.
102 */
51fde349 103static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
2dc6a016
MS
104 size_t size, enum dma_data_direction dir,
105 struct dma_attrs *attrs)
106{
dd37e940 107 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
51fde349
MS
108 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
109 handle & ~PAGE_MASK, size, dir);
2dc6a016
MS
110}
111
51fde349 112static void arm_dma_sync_single_for_cpu(struct device *dev,
2dc6a016
MS
113 dma_addr_t handle, size_t size, enum dma_data_direction dir)
114{
115 unsigned int offset = handle & (PAGE_SIZE - 1);
116 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
dd37e940 117 __dma_page_dev_to_cpu(page, offset, size, dir);
2dc6a016
MS
118}
119
51fde349 120static void arm_dma_sync_single_for_device(struct device *dev,
2dc6a016
MS
121 dma_addr_t handle, size_t size, enum dma_data_direction dir)
122{
123 unsigned int offset = handle & (PAGE_SIZE - 1);
124 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
dd37e940 125 __dma_page_cpu_to_dev(page, offset, size, dir);
2dc6a016
MS
126}
127
2dc6a016 128struct dma_map_ops arm_dma_ops = {
f99d6034
MS
129 .alloc = arm_dma_alloc,
130 .free = arm_dma_free,
131 .mmap = arm_dma_mmap,
dc2832e1 132 .get_sgtable = arm_dma_get_sgtable,
2dc6a016
MS
133 .map_page = arm_dma_map_page,
134 .unmap_page = arm_dma_unmap_page,
135 .map_sg = arm_dma_map_sg,
136 .unmap_sg = arm_dma_unmap_sg,
137 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
138 .sync_single_for_device = arm_dma_sync_single_for_device,
139 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
140 .sync_sg_for_device = arm_dma_sync_sg_for_device,
141 .set_dma_mask = arm_dma_set_mask,
142};
143EXPORT_SYMBOL(arm_dma_ops);
144
dd37e940
RH
145static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
146 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
147static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
148 dma_addr_t handle, struct dma_attrs *attrs);
149
150struct dma_map_ops arm_coherent_dma_ops = {
151 .alloc = arm_coherent_dma_alloc,
152 .free = arm_coherent_dma_free,
153 .mmap = arm_dma_mmap,
154 .get_sgtable = arm_dma_get_sgtable,
155 .map_page = arm_coherent_dma_map_page,
156 .map_sg = arm_dma_map_sg,
157 .set_dma_mask = arm_dma_set_mask,
158};
159EXPORT_SYMBOL(arm_coherent_dma_ops);
160
9f28cde0
RK
161static int __dma_supported(struct device *dev, u64 mask, bool warn)
162{
163 unsigned long max_dma_pfn;
164
165 /*
166 * If the mask allows for more memory than we can address,
167 * and we actually have that much memory, then we must
168 * indicate that DMA to this device is not supported.
169 */
170 if (sizeof(mask) != sizeof(dma_addr_t) &&
171 mask > (dma_addr_t)~0 &&
172 dma_to_pfn(dev, ~0) < max_pfn) {
173 if (warn) {
174 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
175 mask);
176 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
177 }
178 return 0;
179 }
180
181 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
182
183 /*
184 * Translate the device's DMA mask to a PFN limit. This
185 * PFN number includes the page which we can DMA to.
186 */
187 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
188 if (warn)
189 dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
190 mask,
191 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
192 max_dma_pfn + 1);
193 return 0;
194 }
195
196 return 1;
197}
198
ab6494f0
CM
199static u64 get_coherent_dma_mask(struct device *dev)
200{
4dcfa600 201 u64 mask = (u64)DMA_BIT_MASK(32);
ab6494f0
CM
202
203 if (dev) {
204 mask = dev->coherent_dma_mask;
205
206 /*
207 * Sanity check the DMA mask - it must be non-zero, and
208 * must be able to be satisfied by a DMA allocation.
209 */
210 if (mask == 0) {
211 dev_warn(dev, "coherent DMA mask is unset\n");
212 return 0;
213 }
214
9f28cde0 215 if (!__dma_supported(dev, mask, true))
ab6494f0 216 return 0;
ab6494f0 217 }
1da177e4 218
ab6494f0
CM
219 return mask;
220}
221
c7909509
MS
222static void __dma_clear_buffer(struct page *page, size_t size)
223{
c7909509
MS
224 /*
225 * Ensure that the allocated pages are zeroed, and that any data
226 * lurking in the kernel direct-mapped region is invalidated.
227 */
9848e48f
MS
228 if (PageHighMem(page)) {
229 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
230 phys_addr_t end = base + size;
231 while (size > 0) {
232 void *ptr = kmap_atomic(page);
233 memset(ptr, 0, PAGE_SIZE);
234 dmac_flush_range(ptr, ptr + PAGE_SIZE);
235 kunmap_atomic(ptr);
236 page++;
237 size -= PAGE_SIZE;
238 }
239 outer_flush_range(base, end);
240 } else {
241 void *ptr = page_address(page);
4ce63fcd
MS
242 memset(ptr, 0, size);
243 dmac_flush_range(ptr, ptr + size);
244 outer_flush_range(__pa(ptr), __pa(ptr) + size);
245 }
c7909509
MS
246}
247
7a9a32a9
RK
248/*
249 * Allocate a DMA buffer for 'dev' of size 'size' using the
250 * specified gfp mask. Note that 'size' must be page aligned.
251 */
252static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
253{
254 unsigned long order = get_order(size);
255 struct page *page, *p, *e;
7a9a32a9
RK
256
257 page = alloc_pages(gfp, order);
258 if (!page)
259 return NULL;
260
261 /*
262 * Now split the huge page and free the excess pages
263 */
264 split_page(page, order);
265 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
266 __free_page(p);
267
c7909509 268 __dma_clear_buffer(page, size);
7a9a32a9
RK
269
270 return page;
271}
272
273/*
274 * Free a DMA buffer. 'size' must be page aligned.
275 */
276static void __dma_free_buffer(struct page *page, size_t size)
277{
278 struct page *e = page + (size >> PAGE_SHIFT);
279
280 while (page < e) {
281 __free_page(page);
282 page++;
283 }
284}
285
ab6494f0 286#ifdef CONFIG_MMU
e9da6e99 287#ifdef CONFIG_HUGETLB_PAGE
1355e2a6 288#warning ARM Coherent DMA allocator does not (yet) support huge TLB
e9da6e99 289#endif
a5e9d38b 290
e9da6e99 291static void *__alloc_from_contiguous(struct device *dev, size_t size,
9848e48f
MS
292 pgprot_t prot, struct page **ret_page,
293 const void *caller);
99d1717d 294
e9da6e99
MS
295static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
296 pgprot_t prot, struct page **ret_page,
297 const void *caller);
99d1717d 298
e9da6e99
MS
299static void *
300__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
301 const void *caller)
99d1717d 302{
e9da6e99
MS
303 struct vm_struct *area;
304 unsigned long addr;
99d1717d 305
e9da6e99
MS
306 /*
307 * DMA allocation can be mapped to user space, so lets
308 * set VM_USERMAP flags too.
309 */
310 area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
311 caller);
312 if (!area)
313 return NULL;
314 addr = (unsigned long)area->addr;
315 area->phys_addr = __pfn_to_phys(page_to_pfn(page));
99d1717d 316
e9da6e99
MS
317 if (ioremap_page_range(addr, addr + size, area->phys_addr, prot)) {
318 vunmap((void *)addr);
319 return NULL;
320 }
321 return (void *)addr;
99d1717d 322}
1da177e4 323
e9da6e99 324static void __dma_free_remap(void *cpu_addr, size_t size)
88c58f3b 325{
e9da6e99
MS
326 unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
327 struct vm_struct *area = find_vm_area(cpu_addr);
328 if (!area || (area->flags & flags) != flags) {
329 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
330 return;
99d1717d 331 }
e9da6e99
MS
332 unmap_kernel_range((unsigned long)cpu_addr, size);
333 vunmap(cpu_addr);
88c58f3b 334}
88c58f3b 335
6e5267aa
MS
336#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
337
e9da6e99
MS
338struct dma_pool {
339 size_t size;
340 spinlock_t lock;
341 unsigned long *bitmap;
342 unsigned long nr_pages;
343 void *vaddr;
6b3fe472 344 struct page **pages;
c7909509
MS
345};
346
e9da6e99 347static struct dma_pool atomic_pool = {
6e5267aa 348 .size = DEFAULT_DMA_COHERENT_POOL_SIZE,
e9da6e99 349};
c7909509
MS
350
351static int __init early_coherent_pool(char *p)
352{
e9da6e99 353 atomic_pool.size = memparse(p, &p);
c7909509
MS
354 return 0;
355}
356early_param("coherent_pool", early_coherent_pool);
357
6e5267aa
MS
358void __init init_dma_coherent_pool_size(unsigned long size)
359{
360 /*
361 * Catch any attempt to set the pool size too late.
362 */
363 BUG_ON(atomic_pool.vaddr);
364
365 /*
366 * Set architecture specific coherent pool size only if
367 * it has not been changed by kernel command line parameter.
368 */
369 if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE)
370 atomic_pool.size = size;
371}
372
c7909509
MS
373/*
374 * Initialise the coherent pool for atomic allocations.
375 */
e9da6e99 376static int __init atomic_pool_init(void)
c7909509 377{
e9da6e99 378 struct dma_pool *pool = &atomic_pool;
71b55663 379 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
9d1400cf 380 gfp_t gfp = GFP_KERNEL | GFP_DMA;
e9da6e99
MS
381 unsigned long nr_pages = pool->size >> PAGE_SHIFT;
382 unsigned long *bitmap;
c7909509 383 struct page *page;
6b3fe472 384 struct page **pages;
c7909509 385 void *ptr;
e9da6e99 386 int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
c7909509 387
e9da6e99
MS
388 bitmap = kzalloc(bitmap_size, GFP_KERNEL);
389 if (!bitmap)
390 goto no_bitmap;
c7909509 391
6b3fe472
HD
392 pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
393 if (!pages)
394 goto no_pages;
395
f825c736 396 if (IS_ENABLED(CONFIG_DMA_CMA))
9848e48f
MS
397 ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page,
398 atomic_pool_init);
e9da6e99 399 else
9d1400cf
MS
400 ptr = __alloc_remap_buffer(NULL, pool->size, gfp, prot, &page,
401 atomic_pool_init);
c7909509 402 if (ptr) {
6b3fe472
HD
403 int i;
404
405 for (i = 0; i < nr_pages; i++)
406 pages[i] = page + i;
407
e9da6e99
MS
408 spin_lock_init(&pool->lock);
409 pool->vaddr = ptr;
6b3fe472 410 pool->pages = pages;
e9da6e99
MS
411 pool->bitmap = bitmap;
412 pool->nr_pages = nr_pages;
413 pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
414 (unsigned)pool->size / 1024);
c7909509
MS
415 return 0;
416 }
ec10665c
SK
417
418 kfree(pages);
6b3fe472 419no_pages:
e9da6e99
MS
420 kfree(bitmap);
421no_bitmap:
422 pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
423 (unsigned)pool->size / 1024);
c7909509
MS
424 return -ENOMEM;
425}
426/*
427 * CMA is activated by core_initcall, so we must be called after it.
428 */
e9da6e99 429postcore_initcall(atomic_pool_init);
c7909509
MS
430
431struct dma_contig_early_reserve {
432 phys_addr_t base;
433 unsigned long size;
434};
435
436static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
437
438static int dma_mmu_remap_num __initdata;
439
440void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
441{
442 dma_mmu_remap[dma_mmu_remap_num].base = base;
443 dma_mmu_remap[dma_mmu_remap_num].size = size;
444 dma_mmu_remap_num++;
445}
446
447void __init dma_contiguous_remap(void)
448{
449 int i;
450 for (i = 0; i < dma_mmu_remap_num; i++) {
451 phys_addr_t start = dma_mmu_remap[i].base;
452 phys_addr_t end = start + dma_mmu_remap[i].size;
453 struct map_desc map;
454 unsigned long addr;
455
456 if (end > arm_lowmem_limit)
457 end = arm_lowmem_limit;
458 if (start >= end)
39f78e70 459 continue;
c7909509
MS
460
461 map.pfn = __phys_to_pfn(start);
462 map.virtual = __phys_to_virt(start);
463 map.length = end - start;
464 map.type = MT_MEMORY_DMA_READY;
465
466 /*
467 * Clear previous low-memory mapping
468 */
469 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
61f6c7a4 470 addr += PMD_SIZE)
c7909509
MS
471 pmd_clear(pmd_off_k(addr));
472
473 iotable_init(&map, 1);
474 }
475}
476
c7909509
MS
477static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
478 void *data)
479{
480 struct page *page = virt_to_page(addr);
481 pgprot_t prot = *(pgprot_t *)data;
482
483 set_pte_ext(pte, mk_pte(page, prot), 0);
484 return 0;
485}
486
487static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
488{
489 unsigned long start = (unsigned long) page_address(page);
490 unsigned end = start + size;
491
492 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
c7909509
MS
493 flush_tlb_kernel_range(start, end);
494}
495
496static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
497 pgprot_t prot, struct page **ret_page,
498 const void *caller)
499{
500 struct page *page;
501 void *ptr;
502 page = __dma_alloc_buffer(dev, size, gfp);
503 if (!page)
504 return NULL;
505
506 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
507 if (!ptr) {
508 __dma_free_buffer(page, size);
509 return NULL;
510 }
511
512 *ret_page = page;
513 return ptr;
514}
515
e9da6e99 516static void *__alloc_from_pool(size_t size, struct page **ret_page)
c7909509 517{
e9da6e99
MS
518 struct dma_pool *pool = &atomic_pool;
519 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
520 unsigned int pageno;
521 unsigned long flags;
522 void *ptr = NULL;
e4ea6918 523 unsigned long align_mask;
c7909509 524
e9da6e99
MS
525 if (!pool->vaddr) {
526 WARN(1, "coherent pool not initialised!\n");
c7909509
MS
527 return NULL;
528 }
529
530 /*
531 * Align the region allocation - allocations from pool are rather
532 * small, so align them to their order in pages, minimum is a page
533 * size. This helps reduce fragmentation of the DMA space.
534 */
e4ea6918 535 align_mask = (1 << get_order(size)) - 1;
e9da6e99
MS
536
537 spin_lock_irqsave(&pool->lock, flags);
538 pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
e4ea6918 539 0, count, align_mask);
e9da6e99
MS
540 if (pageno < pool->nr_pages) {
541 bitmap_set(pool->bitmap, pageno, count);
542 ptr = pool->vaddr + PAGE_SIZE * pageno;
6b3fe472 543 *ret_page = pool->pages[pageno];
fb71285f
MS
544 } else {
545 pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n"
546 "Please increase it with coherent_pool= kernel parameter!\n",
547 (unsigned)pool->size / 1024);
c7909509 548 }
e9da6e99
MS
549 spin_unlock_irqrestore(&pool->lock, flags);
550
551 return ptr;
c7909509
MS
552}
553
21d0a759
HD
554static bool __in_atomic_pool(void *start, size_t size)
555{
556 struct dma_pool *pool = &atomic_pool;
557 void *end = start + size;
558 void *pool_start = pool->vaddr;
559 void *pool_end = pool->vaddr + pool->size;
560
f3d87524 561 if (start < pool_start || start >= pool_end)
21d0a759
HD
562 return false;
563
564 if (end <= pool_end)
565 return true;
566
567 WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n",
568 start, end - 1, pool_start, pool_end - 1);
569
570 return false;
571}
572
e9da6e99 573static int __free_from_pool(void *start, size_t size)
c7909509 574{
e9da6e99
MS
575 struct dma_pool *pool = &atomic_pool;
576 unsigned long pageno, count;
577 unsigned long flags;
c7909509 578
21d0a759 579 if (!__in_atomic_pool(start, size))
c7909509
MS
580 return 0;
581
e9da6e99
MS
582 pageno = (start - pool->vaddr) >> PAGE_SHIFT;
583 count = size >> PAGE_SHIFT;
584
585 spin_lock_irqsave(&pool->lock, flags);
586 bitmap_clear(pool->bitmap, pageno, count);
587 spin_unlock_irqrestore(&pool->lock, flags);
588
c7909509
MS
589 return 1;
590}
591
592static void *__alloc_from_contiguous(struct device *dev, size_t size,
9848e48f
MS
593 pgprot_t prot, struct page **ret_page,
594 const void *caller)
c7909509
MS
595{
596 unsigned long order = get_order(size);
597 size_t count = size >> PAGE_SHIFT;
598 struct page *page;
9848e48f 599 void *ptr;
c7909509
MS
600
601 page = dma_alloc_from_contiguous(dev, count, order);
602 if (!page)
603 return NULL;
604
605 __dma_clear_buffer(page, size);
c7909509 606
9848e48f
MS
607 if (PageHighMem(page)) {
608 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
609 if (!ptr) {
610 dma_release_from_contiguous(dev, page, count);
611 return NULL;
612 }
613 } else {
614 __dma_remap(page, size, prot);
615 ptr = page_address(page);
616 }
c7909509 617 *ret_page = page;
9848e48f 618 return ptr;
c7909509
MS
619}
620
621static void __free_from_contiguous(struct device *dev, struct page *page,
9848e48f 622 void *cpu_addr, size_t size)
c7909509 623{
9848e48f
MS
624 if (PageHighMem(page))
625 __dma_free_remap(cpu_addr, size);
626 else
71b55663 627 __dma_remap(page, size, PAGE_KERNEL);
c7909509
MS
628 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
629}
630
f99d6034
MS
631static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
632{
633 prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
634 pgprot_writecombine(prot) :
635 pgprot_dmacoherent(prot);
636 return prot;
637}
638
c7909509
MS
639#define nommu() 0
640
ab6494f0 641#else /* !CONFIG_MMU */
695ae0af 642
c7909509
MS
643#define nommu() 1
644
f99d6034 645#define __get_dma_pgprot(attrs, prot) __pgprot(0)
c7909509 646#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
e9da6e99 647#define __alloc_from_pool(size, ret_page) NULL
9848e48f 648#define __alloc_from_contiguous(dev, size, prot, ret, c) NULL
c7909509 649#define __free_from_pool(cpu_addr, size) 0
9848e48f 650#define __free_from_contiguous(dev, page, cpu_addr, size) do { } while (0)
c7909509 651#define __dma_free_remap(cpu_addr, size) do { } while (0)
31ebf944
RK
652
653#endif /* CONFIG_MMU */
654
c7909509
MS
655static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
656 struct page **ret_page)
ab6494f0 657{
c7909509
MS
658 struct page *page;
659 page = __dma_alloc_buffer(dev, size, gfp);
660 if (!page)
661 return NULL;
662
663 *ret_page = page;
664 return page_address(page);
665}
666
667
668
669static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
dd37e940 670 gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller)
c7909509
MS
671{
672 u64 mask = get_coherent_dma_mask(dev);
3dd7ea92 673 struct page *page = NULL;
31ebf944 674 void *addr;
ab6494f0 675
c7909509
MS
676#ifdef CONFIG_DMA_API_DEBUG
677 u64 limit = (mask + 1) & ~mask;
678 if (limit && size >= limit) {
679 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
680 size, mask);
681 return NULL;
682 }
683#endif
684
685 if (!mask)
686 return NULL;
687
688 if (mask < 0xffffffffULL)
689 gfp |= GFP_DMA;
690
ea2e7057
SB
691 /*
692 * Following is a work-around (a.k.a. hack) to prevent pages
693 * with __GFP_COMP being passed to split_page() which cannot
694 * handle them. The real problem is that this flag probably
695 * should be 0 on ARM as it is not supported on this
696 * platform; see CONFIG_HUGETLBFS.
697 */
698 gfp &= ~(__GFP_COMP);
699
553ac788 700 *handle = DMA_ERROR_CODE;
04da5694 701 size = PAGE_ALIGN(size);
ab6494f0 702
dd37e940 703 if (is_coherent || nommu())
c7909509 704 addr = __alloc_simple_buffer(dev, size, gfp, &page);
633dc92a 705 else if (!(gfp & __GFP_WAIT))
e9da6e99 706 addr = __alloc_from_pool(size, &page);
f825c736 707 else if (!IS_ENABLED(CONFIG_DMA_CMA))
c7909509 708 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
31ebf944 709 else
9848e48f 710 addr = __alloc_from_contiguous(dev, size, prot, &page, caller);
695ae0af 711
31ebf944 712 if (addr)
9eedd963 713 *handle = pfn_to_dma(dev, page_to_pfn(page));
695ae0af 714
31ebf944
RK
715 return addr;
716}
1da177e4
LT
717
718/*
719 * Allocate DMA-coherent memory space and return both the kernel remapped
720 * virtual and bus address for that space.
721 */
f99d6034
MS
722void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
723 gfp_t gfp, struct dma_attrs *attrs)
1da177e4 724{
0ea1ec71 725 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
1fe53268
DB
726 void *memory;
727
728 if (dma_alloc_from_coherent(dev, size, handle, &memory))
729 return memory;
730
dd37e940
RH
731 return __dma_alloc(dev, size, handle, gfp, prot, false,
732 __builtin_return_address(0));
733}
734
735static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
736 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
737{
0ea1ec71 738 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
dd37e940
RH
739 void *memory;
740
741 if (dma_alloc_from_coherent(dev, size, handle, &memory))
742 return memory;
743
744 return __dma_alloc(dev, size, handle, gfp, prot, true,
45cd5290 745 __builtin_return_address(0));
1da177e4 746}
1da177e4
LT
747
748/*
f99d6034 749 * Create userspace mapping for the DMA-coherent memory.
1da177e4 750 */
f99d6034
MS
751int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
752 void *cpu_addr, dma_addr_t dma_addr, size_t size,
753 struct dma_attrs *attrs)
1da177e4 754{
ab6494f0
CM
755 int ret = -ENXIO;
756#ifdef CONFIG_MMU
50262a4b
MS
757 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
758 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
c7909509 759 unsigned long pfn = dma_to_pfn(dev, dma_addr);
50262a4b
MS
760 unsigned long off = vma->vm_pgoff;
761
f99d6034
MS
762 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
763
47142f07
MS
764 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
765 return ret;
766
50262a4b
MS
767 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
768 ret = remap_pfn_range(vma, vma->vm_start,
769 pfn + off,
770 vma->vm_end - vma->vm_start,
771 vma->vm_page_prot);
772 }
ab6494f0 773#endif /* CONFIG_MMU */
1da177e4
LT
774
775 return ret;
776}
777
1da177e4 778/*
c7909509 779 * Free a buffer as defined by the above mapping.
1da177e4 780 */
dd37e940
RH
781static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
782 dma_addr_t handle, struct dma_attrs *attrs,
783 bool is_coherent)
1da177e4 784{
c7909509 785 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
5edf71ae 786
1fe53268
DB
787 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
788 return;
789
3e82d012
RK
790 size = PAGE_ALIGN(size);
791
dd37e940 792 if (is_coherent || nommu()) {
c7909509 793 __dma_free_buffer(page, size);
d9e0d149
AK
794 } else if (__free_from_pool(cpu_addr, size)) {
795 return;
f825c736 796 } else if (!IS_ENABLED(CONFIG_DMA_CMA)) {
695ae0af 797 __dma_free_remap(cpu_addr, size);
c7909509
MS
798 __dma_free_buffer(page, size);
799 } else {
c7909509
MS
800 /*
801 * Non-atomic allocations cannot be freed with IRQs disabled
802 */
803 WARN_ON(irqs_disabled());
9848e48f 804 __free_from_contiguous(dev, page, cpu_addr, size);
c7909509 805 }
1da177e4 806}
afd1a321 807
dd37e940
RH
808void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
809 dma_addr_t handle, struct dma_attrs *attrs)
810{
811 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
812}
813
814static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
815 dma_addr_t handle, struct dma_attrs *attrs)
816{
817 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
818}
819
dc2832e1
MS
820int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
821 void *cpu_addr, dma_addr_t handle, size_t size,
822 struct dma_attrs *attrs)
823{
824 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
825 int ret;
826
827 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
828 if (unlikely(ret))
829 return ret;
830
831 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
832 return 0;
833}
834
4ea0d737 835static void dma_cache_maint_page(struct page *page, unsigned long offset,
a9c9147e
RK
836 size_t size, enum dma_data_direction dir,
837 void (*op)(const void *, size_t, int))
43377453 838{
15653371
RK
839 unsigned long pfn;
840 size_t left = size;
841
842 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
843 offset %= PAGE_SIZE;
844
43377453
NP
845 /*
846 * A single sg entry may refer to multiple physically contiguous
847 * pages. But we still need to process highmem pages individually.
848 * If highmem is not configured then the bulk of this loop gets
849 * optimized out.
850 */
43377453
NP
851 do {
852 size_t len = left;
93f1d629
RK
853 void *vaddr;
854
15653371
RK
855 page = pfn_to_page(pfn);
856
93f1d629 857 if (PageHighMem(page)) {
15653371 858 if (len + offset > PAGE_SIZE)
93f1d629 859 len = PAGE_SIZE - offset;
dd0f67f4
JK
860
861 if (cache_is_vipt_nonaliasing()) {
39af22a7 862 vaddr = kmap_atomic(page);
7e5a69e8 863 op(vaddr + offset, len, dir);
39af22a7 864 kunmap_atomic(vaddr);
dd0f67f4
JK
865 } else {
866 vaddr = kmap_high_get(page);
867 if (vaddr) {
868 op(vaddr + offset, len, dir);
869 kunmap_high(page);
870 }
43377453 871 }
93f1d629
RK
872 } else {
873 vaddr = page_address(page) + offset;
a9c9147e 874 op(vaddr, len, dir);
43377453 875 }
43377453 876 offset = 0;
15653371 877 pfn++;
43377453
NP
878 left -= len;
879 } while (left);
880}
4ea0d737 881
51fde349
MS
882/*
883 * Make an area consistent for devices.
884 * Note: Drivers should NOT use this function directly, as it will break
885 * platforms with CONFIG_DMABOUNCE.
886 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
887 */
888static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
4ea0d737
RK
889 size_t size, enum dma_data_direction dir)
890{
65af191a 891 unsigned long paddr;
65af191a 892
a9c9147e 893 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
65af191a
RK
894
895 paddr = page_to_phys(page) + off;
2ffe2da3
RK
896 if (dir == DMA_FROM_DEVICE) {
897 outer_inv_range(paddr, paddr + size);
898 } else {
899 outer_clean_range(paddr, paddr + size);
900 }
901 /* FIXME: non-speculating: flush on bidirectional mappings? */
4ea0d737 902}
4ea0d737 903
51fde349 904static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
4ea0d737
RK
905 size_t size, enum dma_data_direction dir)
906{
2ffe2da3
RK
907 unsigned long paddr = page_to_phys(page) + off;
908
909 /* FIXME: non-speculating: not required */
910 /* don't bother invalidating if DMA to device */
911 if (dir != DMA_TO_DEVICE)
912 outer_inv_range(paddr, paddr + size);
913
a9c9147e 914 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
c0177800
CM
915
916 /*
b2a234ed 917 * Mark the D-cache clean for these pages to avoid extra flushing.
c0177800 918 */
b2a234ed
ML
919 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
920 unsigned long pfn;
921 size_t left = size;
922
923 pfn = page_to_pfn(page) + off / PAGE_SIZE;
924 off %= PAGE_SIZE;
925 if (off) {
926 pfn++;
927 left -= PAGE_SIZE - off;
928 }
929 while (left >= PAGE_SIZE) {
930 page = pfn_to_page(pfn++);
931 set_bit(PG_dcache_clean, &page->flags);
932 left -= PAGE_SIZE;
933 }
934 }
4ea0d737 935}
43377453 936
afd1a321 937/**
2a550e73 938 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
afd1a321
RK
939 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
940 * @sg: list of buffers
941 * @nents: number of buffers to map
942 * @dir: DMA transfer direction
943 *
944 * Map a set of buffers described by scatterlist in streaming mode for DMA.
945 * This is the scatter-gather version of the dma_map_single interface.
946 * Here the scatter gather list elements are each tagged with the
947 * appropriate dma address and length. They are obtained via
948 * sg_dma_{address,length}.
949 *
950 * Device ownership issues as mentioned for dma_map_single are the same
951 * here.
952 */
2dc6a016
MS
953int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
954 enum dma_data_direction dir, struct dma_attrs *attrs)
afd1a321 955{
2a550e73 956 struct dma_map_ops *ops = get_dma_ops(dev);
afd1a321 957 struct scatterlist *s;
01135d92 958 int i, j;
afd1a321
RK
959
960 for_each_sg(sg, s, nents, i) {
4ce63fcd
MS
961#ifdef CONFIG_NEED_SG_DMA_LENGTH
962 s->dma_length = s->length;
963#endif
2a550e73
MS
964 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
965 s->length, dir, attrs);
01135d92
RK
966 if (dma_mapping_error(dev, s->dma_address))
967 goto bad_mapping;
afd1a321 968 }
afd1a321 969 return nents;
01135d92
RK
970
971 bad_mapping:
972 for_each_sg(sg, s, i, j)
2a550e73 973 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
01135d92 974 return 0;
afd1a321 975}
afd1a321
RK
976
977/**
2a550e73 978 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
afd1a321
RK
979 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
980 * @sg: list of buffers
0adfca6f 981 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
afd1a321
RK
982 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
983 *
984 * Unmap a set of streaming mode DMA translations. Again, CPU access
985 * rules concerning calls here are the same as for dma_unmap_single().
986 */
2dc6a016
MS
987void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
988 enum dma_data_direction dir, struct dma_attrs *attrs)
afd1a321 989{
2a550e73 990 struct dma_map_ops *ops = get_dma_ops(dev);
01135d92 991 struct scatterlist *s;
01135d92 992
01135d92 993 int i;
24056f52 994
01135d92 995 for_each_sg(sg, s, nents, i)
2a550e73 996 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
afd1a321 997}
afd1a321
RK
998
999/**
2a550e73 1000 * arm_dma_sync_sg_for_cpu
afd1a321
RK
1001 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1002 * @sg: list of buffers
1003 * @nents: number of buffers to map (returned from dma_map_sg)
1004 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1005 */
2dc6a016 1006void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
afd1a321
RK
1007 int nents, enum dma_data_direction dir)
1008{
2a550e73 1009 struct dma_map_ops *ops = get_dma_ops(dev);
afd1a321
RK
1010 struct scatterlist *s;
1011 int i;
1012
2a550e73
MS
1013 for_each_sg(sg, s, nents, i)
1014 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1015 dir);
afd1a321 1016}
afd1a321
RK
1017
1018/**
2a550e73 1019 * arm_dma_sync_sg_for_device
afd1a321
RK
1020 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1021 * @sg: list of buffers
1022 * @nents: number of buffers to map (returned from dma_map_sg)
1023 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1024 */
2dc6a016 1025void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
afd1a321
RK
1026 int nents, enum dma_data_direction dir)
1027{
2a550e73 1028 struct dma_map_ops *ops = get_dma_ops(dev);
afd1a321
RK
1029 struct scatterlist *s;
1030 int i;
1031
2a550e73
MS
1032 for_each_sg(sg, s, nents, i)
1033 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1034 dir);
afd1a321 1035}
24056f52 1036
022ae537
RK
1037/*
1038 * Return whether the given device DMA address mask can be supported
1039 * properly. For example, if your device can only drive the low 24-bits
1040 * during bus mastering, then you would pass 0x00ffffff as the mask
1041 * to this function.
1042 */
1043int dma_supported(struct device *dev, u64 mask)
1044{
9f28cde0 1045 return __dma_supported(dev, mask, false);
022ae537
RK
1046}
1047EXPORT_SYMBOL(dma_supported);
1048
87b54e78 1049int arm_dma_set_mask(struct device *dev, u64 dma_mask)
022ae537
RK
1050{
1051 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1052 return -EIO;
1053
022ae537 1054 *dev->dma_mask = dma_mask;
022ae537
RK
1055
1056 return 0;
1057}
022ae537 1058
24056f52
RK
1059#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1060
1061static int __init dma_debug_do_init(void)
1062{
1063 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1064 return 0;
1065}
1066fs_initcall(dma_debug_do_init);
4ce63fcd
MS
1067
1068#ifdef CONFIG_ARM_DMA_USE_IOMMU
1069
1070/* IOMMU */
1071
1072static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1073 size_t size)
1074{
1075 unsigned int order = get_order(size);
1076 unsigned int align = 0;
1077 unsigned int count, start;
1078 unsigned long flags;
1079
60460abf
SWK
1080 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1081 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1082
4ce63fcd
MS
1083 count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
1084 (1 << mapping->order) - 1) >> mapping->order;
1085
1086 if (order > mapping->order)
1087 align = (1 << (order - mapping->order)) - 1;
1088
1089 spin_lock_irqsave(&mapping->lock, flags);
1090 start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
1091 count, align);
1092 if (start > mapping->bits) {
1093 spin_unlock_irqrestore(&mapping->lock, flags);
1094 return DMA_ERROR_CODE;
1095 }
1096
1097 bitmap_set(mapping->bitmap, start, count);
1098 spin_unlock_irqrestore(&mapping->lock, flags);
1099
1100 return mapping->base + (start << (mapping->order + PAGE_SHIFT));
1101}
1102
1103static inline void __free_iova(struct dma_iommu_mapping *mapping,
1104 dma_addr_t addr, size_t size)
1105{
1106 unsigned int start = (addr - mapping->base) >>
1107 (mapping->order + PAGE_SHIFT);
1108 unsigned int count = ((size >> PAGE_SHIFT) +
1109 (1 << mapping->order) - 1) >> mapping->order;
1110 unsigned long flags;
1111
1112 spin_lock_irqsave(&mapping->lock, flags);
1113 bitmap_clear(mapping->bitmap, start, count);
1114 spin_unlock_irqrestore(&mapping->lock, flags);
1115}
1116
549a17e4
MS
1117static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1118 gfp_t gfp, struct dma_attrs *attrs)
4ce63fcd
MS
1119{
1120 struct page **pages;
1121 int count = size >> PAGE_SHIFT;
1122 int array_size = count * sizeof(struct page *);
1123 int i = 0;
1124
1125 if (array_size <= PAGE_SIZE)
1126 pages = kzalloc(array_size, gfp);
1127 else
1128 pages = vzalloc(array_size);
1129 if (!pages)
1130 return NULL;
1131
549a17e4
MS
1132 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
1133 {
1134 unsigned long order = get_order(size);
1135 struct page *page;
1136
1137 page = dma_alloc_from_contiguous(dev, count, order);
1138 if (!page)
1139 goto error;
1140
1141 __dma_clear_buffer(page, size);
1142
1143 for (i = 0; i < count; i++)
1144 pages[i] = page + i;
1145
1146 return pages;
1147 }
1148
f8669bef
MS
1149 /*
1150 * IOMMU can map any pages, so himem can also be used here
1151 */
1152 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1153
4ce63fcd 1154 while (count) {
593f4735 1155 int j, order = __fls(count);
4ce63fcd 1156
f8669bef 1157 pages[i] = alloc_pages(gfp, order);
4ce63fcd 1158 while (!pages[i] && order)
f8669bef 1159 pages[i] = alloc_pages(gfp, --order);
4ce63fcd
MS
1160 if (!pages[i])
1161 goto error;
1162
5a796eeb 1163 if (order) {
4ce63fcd 1164 split_page(pages[i], order);
5a796eeb
HD
1165 j = 1 << order;
1166 while (--j)
1167 pages[i + j] = pages[i] + j;
1168 }
4ce63fcd
MS
1169
1170 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
1171 i += 1 << order;
1172 count -= 1 << order;
1173 }
1174
1175 return pages;
1176error:
9fa8af91 1177 while (i--)
4ce63fcd
MS
1178 if (pages[i])
1179 __free_pages(pages[i], 0);
46c87852 1180 if (array_size <= PAGE_SIZE)
4ce63fcd
MS
1181 kfree(pages);
1182 else
1183 vfree(pages);
1184 return NULL;
1185}
1186
549a17e4
MS
1187static int __iommu_free_buffer(struct device *dev, struct page **pages,
1188 size_t size, struct dma_attrs *attrs)
4ce63fcd
MS
1189{
1190 int count = size >> PAGE_SHIFT;
1191 int array_size = count * sizeof(struct page *);
1192 int i;
549a17e4
MS
1193
1194 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
1195 dma_release_from_contiguous(dev, pages[0], count);
1196 } else {
1197 for (i = 0; i < count; i++)
1198 if (pages[i])
1199 __free_pages(pages[i], 0);
1200 }
1201
46c87852 1202 if (array_size <= PAGE_SIZE)
4ce63fcd
MS
1203 kfree(pages);
1204 else
1205 vfree(pages);
1206 return 0;
1207}
1208
1209/*
1210 * Create a CPU mapping for a specified pages
1211 */
1212static void *
e9da6e99
MS
1213__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1214 const void *caller)
4ce63fcd 1215{
e9da6e99
MS
1216 unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1217 struct vm_struct *area;
1218 unsigned long p;
4ce63fcd 1219
e9da6e99
MS
1220 area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
1221 caller);
1222 if (!area)
4ce63fcd 1223 return NULL;
4ce63fcd 1224
e9da6e99
MS
1225 area->pages = pages;
1226 area->nr_pages = nr_pages;
1227 p = (unsigned long)area->addr;
4ce63fcd 1228
e9da6e99
MS
1229 for (i = 0; i < nr_pages; i++) {
1230 phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i]));
1231 if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot))
1232 goto err;
1233 p += PAGE_SIZE;
4ce63fcd 1234 }
e9da6e99
MS
1235 return area->addr;
1236err:
1237 unmap_kernel_range((unsigned long)area->addr, size);
1238 vunmap(area->addr);
4ce63fcd
MS
1239 return NULL;
1240}
1241
1242/*
1243 * Create a mapping in device IO address space for specified pages
1244 */
1245static dma_addr_t
1246__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1247{
1248 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1249 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1250 dma_addr_t dma_addr, iova;
1251 int i, ret = DMA_ERROR_CODE;
1252
1253 dma_addr = __alloc_iova(mapping, size);
1254 if (dma_addr == DMA_ERROR_CODE)
1255 return dma_addr;
1256
1257 iova = dma_addr;
1258 for (i = 0; i < count; ) {
1259 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1260 phys_addr_t phys = page_to_phys(pages[i]);
1261 unsigned int len, j;
1262
1263 for (j = i + 1; j < count; j++, next_pfn++)
1264 if (page_to_pfn(pages[j]) != next_pfn)
1265 break;
1266
1267 len = (j - i) << PAGE_SHIFT;
c9b24996
AH
1268 ret = iommu_map(mapping->domain, iova, phys, len,
1269 IOMMU_READ|IOMMU_WRITE);
4ce63fcd
MS
1270 if (ret < 0)
1271 goto fail;
1272 iova += len;
1273 i = j;
1274 }
1275 return dma_addr;
1276fail:
1277 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1278 __free_iova(mapping, dma_addr, size);
1279 return DMA_ERROR_CODE;
1280}
1281
1282static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1283{
1284 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1285
1286 /*
1287 * add optional in-page offset from iova to size and align
1288 * result to page size
1289 */
1290 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1291 iova &= PAGE_MASK;
1292
1293 iommu_unmap(mapping->domain, iova, size);
1294 __free_iova(mapping, iova, size);
1295 return 0;
1296}
1297
665bad7b
HD
1298static struct page **__atomic_get_pages(void *addr)
1299{
1300 struct dma_pool *pool = &atomic_pool;
1301 struct page **pages = pool->pages;
1302 int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
1303
1304 return pages + offs;
1305}
1306
955c757e 1307static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
e9da6e99
MS
1308{
1309 struct vm_struct *area;
1310
665bad7b
HD
1311 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1312 return __atomic_get_pages(cpu_addr);
1313
955c757e
MS
1314 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1315 return cpu_addr;
1316
e9da6e99
MS
1317 area = find_vm_area(cpu_addr);
1318 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1319 return area->pages;
1320 return NULL;
1321}
1322
479ed93a
HD
1323static void *__iommu_alloc_atomic(struct device *dev, size_t size,
1324 dma_addr_t *handle)
1325{
1326 struct page *page;
1327 void *addr;
1328
1329 addr = __alloc_from_pool(size, &page);
1330 if (!addr)
1331 return NULL;
1332
1333 *handle = __iommu_create_mapping(dev, &page, size);
1334 if (*handle == DMA_ERROR_CODE)
1335 goto err_mapping;
1336
1337 return addr;
1338
1339err_mapping:
1340 __free_from_pool(addr, size);
1341 return NULL;
1342}
1343
d5898291 1344static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
479ed93a
HD
1345 dma_addr_t handle, size_t size)
1346{
1347 __iommu_remove_mapping(dev, handle, size);
d5898291 1348 __free_from_pool(cpu_addr, size);
479ed93a
HD
1349}
1350
4ce63fcd
MS
1351static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1352 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1353{
71b55663 1354 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
4ce63fcd
MS
1355 struct page **pages;
1356 void *addr = NULL;
1357
1358 *handle = DMA_ERROR_CODE;
1359 size = PAGE_ALIGN(size);
1360
10c8562f 1361 if (!(gfp & __GFP_WAIT))
479ed93a
HD
1362 return __iommu_alloc_atomic(dev, size, handle);
1363
5b91a98c
RZ
1364 /*
1365 * Following is a work-around (a.k.a. hack) to prevent pages
1366 * with __GFP_COMP being passed to split_page() which cannot
1367 * handle them. The real problem is that this flag probably
1368 * should be 0 on ARM as it is not supported on this
1369 * platform; see CONFIG_HUGETLBFS.
1370 */
1371 gfp &= ~(__GFP_COMP);
1372
549a17e4 1373 pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
4ce63fcd
MS
1374 if (!pages)
1375 return NULL;
1376
1377 *handle = __iommu_create_mapping(dev, pages, size);
1378 if (*handle == DMA_ERROR_CODE)
1379 goto err_buffer;
1380
955c757e
MS
1381 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1382 return pages;
1383
e9da6e99
MS
1384 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1385 __builtin_return_address(0));
4ce63fcd
MS
1386 if (!addr)
1387 goto err_mapping;
1388
1389 return addr;
1390
1391err_mapping:
1392 __iommu_remove_mapping(dev, *handle, size);
1393err_buffer:
549a17e4 1394 __iommu_free_buffer(dev, pages, size, attrs);
4ce63fcd
MS
1395 return NULL;
1396}
1397
1398static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1399 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1400 struct dma_attrs *attrs)
1401{
e9da6e99
MS
1402 unsigned long uaddr = vma->vm_start;
1403 unsigned long usize = vma->vm_end - vma->vm_start;
955c757e 1404 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
4ce63fcd
MS
1405
1406 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
4ce63fcd 1407
e9da6e99
MS
1408 if (!pages)
1409 return -ENXIO;
4ce63fcd 1410
e9da6e99
MS
1411 do {
1412 int ret = vm_insert_page(vma, uaddr, *pages++);
1413 if (ret) {
1414 pr_err("Remapping memory failed: %d\n", ret);
1415 return ret;
1416 }
1417 uaddr += PAGE_SIZE;
1418 usize -= PAGE_SIZE;
1419 } while (usize > 0);
4ce63fcd 1420
4ce63fcd
MS
1421 return 0;
1422}
1423
1424/*
1425 * free a page as defined by the above mapping.
1426 * Must not be called with IRQs disabled.
1427 */
1428void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1429 dma_addr_t handle, struct dma_attrs *attrs)
1430{
836bfa0d 1431 struct page **pages;
4ce63fcd
MS
1432 size = PAGE_ALIGN(size);
1433
836bfa0d
YC
1434 if (__in_atomic_pool(cpu_addr, size)) {
1435 __iommu_free_atomic(dev, cpu_addr, handle, size);
e9da6e99 1436 return;
4ce63fcd 1437 }
e9da6e99 1438
836bfa0d
YC
1439 pages = __iommu_get_pages(cpu_addr, attrs);
1440 if (!pages) {
1441 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
479ed93a
HD
1442 return;
1443 }
1444
955c757e
MS
1445 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
1446 unmap_kernel_range((unsigned long)cpu_addr, size);
1447 vunmap(cpu_addr);
1448 }
e9da6e99
MS
1449
1450 __iommu_remove_mapping(dev, handle, size);
549a17e4 1451 __iommu_free_buffer(dev, pages, size, attrs);
4ce63fcd
MS
1452}
1453
dc2832e1
MS
1454static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1455 void *cpu_addr, dma_addr_t dma_addr,
1456 size_t size, struct dma_attrs *attrs)
1457{
1458 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1459 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1460
1461 if (!pages)
1462 return -ENXIO;
1463
1464 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1465 GFP_KERNEL);
4ce63fcd
MS
1466}
1467
c9b24996
AH
1468static int __dma_direction_to_prot(enum dma_data_direction dir)
1469{
1470 int prot;
1471
1472 switch (dir) {
1473 case DMA_BIDIRECTIONAL:
1474 prot = IOMMU_READ | IOMMU_WRITE;
1475 break;
1476 case DMA_TO_DEVICE:
1477 prot = IOMMU_READ;
1478 break;
1479 case DMA_FROM_DEVICE:
1480 prot = IOMMU_WRITE;
1481 break;
1482 default:
1483 prot = 0;
1484 }
1485
1486 return prot;
1487}
1488
4ce63fcd
MS
1489/*
1490 * Map a part of the scatter-gather list into contiguous io address space
1491 */
1492static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1493 size_t size, dma_addr_t *handle,
0fa478df
RH
1494 enum dma_data_direction dir, struct dma_attrs *attrs,
1495 bool is_coherent)
4ce63fcd
MS
1496{
1497 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1498 dma_addr_t iova, iova_base;
1499 int ret = 0;
1500 unsigned int count;
1501 struct scatterlist *s;
c9b24996 1502 int prot;
4ce63fcd
MS
1503
1504 size = PAGE_ALIGN(size);
1505 *handle = DMA_ERROR_CODE;
1506
1507 iova_base = iova = __alloc_iova(mapping, size);
1508 if (iova == DMA_ERROR_CODE)
1509 return -ENOMEM;
1510
1511 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1512 phys_addr_t phys = page_to_phys(sg_page(s));
1513 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1514
0fa478df
RH
1515 if (!is_coherent &&
1516 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
4ce63fcd
MS
1517 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1518
c9b24996
AH
1519 prot = __dma_direction_to_prot(dir);
1520
1521 ret = iommu_map(mapping->domain, iova, phys, len, prot);
4ce63fcd
MS
1522 if (ret < 0)
1523 goto fail;
1524 count += len >> PAGE_SHIFT;
1525 iova += len;
1526 }
1527 *handle = iova_base;
1528
1529 return 0;
1530fail:
1531 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1532 __free_iova(mapping, iova_base, size);
1533 return ret;
1534}
1535
0fa478df
RH
1536static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1537 enum dma_data_direction dir, struct dma_attrs *attrs,
1538 bool is_coherent)
4ce63fcd
MS
1539{
1540 struct scatterlist *s = sg, *dma = sg, *start = sg;
1541 int i, count = 0;
1542 unsigned int offset = s->offset;
1543 unsigned int size = s->offset + s->length;
1544 unsigned int max = dma_get_max_seg_size(dev);
1545
1546 for (i = 1; i < nents; i++) {
1547 s = sg_next(s);
1548
1549 s->dma_address = DMA_ERROR_CODE;
1550 s->dma_length = 0;
1551
1552 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1553 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
0fa478df 1554 dir, attrs, is_coherent) < 0)
4ce63fcd
MS
1555 goto bad_mapping;
1556
1557 dma->dma_address += offset;
1558 dma->dma_length = size - offset;
1559
1560 size = offset = s->offset;
1561 start = s;
1562 dma = sg_next(dma);
1563 count += 1;
1564 }
1565 size += s->length;
1566 }
0fa478df
RH
1567 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1568 is_coherent) < 0)
4ce63fcd
MS
1569 goto bad_mapping;
1570
1571 dma->dma_address += offset;
1572 dma->dma_length = size - offset;
1573
1574 return count+1;
1575
1576bad_mapping:
1577 for_each_sg(sg, s, count, i)
1578 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1579 return 0;
1580}
1581
1582/**
0fa478df 1583 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
4ce63fcd
MS
1584 * @dev: valid struct device pointer
1585 * @sg: list of buffers
0fa478df
RH
1586 * @nents: number of buffers to map
1587 * @dir: DMA transfer direction
4ce63fcd 1588 *
0fa478df
RH
1589 * Map a set of i/o coherent buffers described by scatterlist in streaming
1590 * mode for DMA. The scatter gather list elements are merged together (if
1591 * possible) and tagged with the appropriate dma address and length. They are
1592 * obtained via sg_dma_{address,length}.
4ce63fcd 1593 */
0fa478df
RH
1594int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1595 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1596{
1597 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1598}
1599
1600/**
1601 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1602 * @dev: valid struct device pointer
1603 * @sg: list of buffers
1604 * @nents: number of buffers to map
1605 * @dir: DMA transfer direction
1606 *
1607 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1608 * The scatter gather list elements are merged together (if possible) and
1609 * tagged with the appropriate dma address and length. They are obtained via
1610 * sg_dma_{address,length}.
1611 */
1612int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1613 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1614{
1615 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1616}
1617
1618static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1619 int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
1620 bool is_coherent)
4ce63fcd
MS
1621{
1622 struct scatterlist *s;
1623 int i;
1624
1625 for_each_sg(sg, s, nents, i) {
1626 if (sg_dma_len(s))
1627 __iommu_remove_mapping(dev, sg_dma_address(s),
1628 sg_dma_len(s));
0fa478df 1629 if (!is_coherent &&
97ef952a 1630 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
4ce63fcd
MS
1631 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1632 s->length, dir);
1633 }
1634}
1635
0fa478df
RH
1636/**
1637 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1638 * @dev: valid struct device pointer
1639 * @sg: list of buffers
1640 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1641 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1642 *
1643 * Unmap a set of streaming mode DMA translations. Again, CPU access
1644 * rules concerning calls here are the same as for dma_unmap_single().
1645 */
1646void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1647 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1648{
1649 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1650}
1651
1652/**
1653 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1654 * @dev: valid struct device pointer
1655 * @sg: list of buffers
1656 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1657 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1658 *
1659 * Unmap a set of streaming mode DMA translations. Again, CPU access
1660 * rules concerning calls here are the same as for dma_unmap_single().
1661 */
1662void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1663 enum dma_data_direction dir, struct dma_attrs *attrs)
1664{
1665 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
1666}
1667
4ce63fcd
MS
1668/**
1669 * arm_iommu_sync_sg_for_cpu
1670 * @dev: valid struct device pointer
1671 * @sg: list of buffers
1672 * @nents: number of buffers to map (returned from dma_map_sg)
1673 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1674 */
1675void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1676 int nents, enum dma_data_direction dir)
1677{
1678 struct scatterlist *s;
1679 int i;
1680
1681 for_each_sg(sg, s, nents, i)
0fa478df 1682 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
4ce63fcd
MS
1683
1684}
1685
1686/**
1687 * arm_iommu_sync_sg_for_device
1688 * @dev: valid struct device pointer
1689 * @sg: list of buffers
1690 * @nents: number of buffers to map (returned from dma_map_sg)
1691 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1692 */
1693void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1694 int nents, enum dma_data_direction dir)
1695{
1696 struct scatterlist *s;
1697 int i;
1698
1699 for_each_sg(sg, s, nents, i)
0fa478df 1700 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
4ce63fcd
MS
1701}
1702
1703
1704/**
0fa478df 1705 * arm_coherent_iommu_map_page
4ce63fcd
MS
1706 * @dev: valid struct device pointer
1707 * @page: page that buffer resides in
1708 * @offset: offset into page for start of buffer
1709 * @size: size of buffer to map
1710 * @dir: DMA transfer direction
1711 *
0fa478df 1712 * Coherent IOMMU aware version of arm_dma_map_page()
4ce63fcd 1713 */
0fa478df 1714static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
4ce63fcd
MS
1715 unsigned long offset, size_t size, enum dma_data_direction dir,
1716 struct dma_attrs *attrs)
1717{
1718 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1719 dma_addr_t dma_addr;
13987d68 1720 int ret, prot, len = PAGE_ALIGN(size + offset);
4ce63fcd 1721
4ce63fcd
MS
1722 dma_addr = __alloc_iova(mapping, len);
1723 if (dma_addr == DMA_ERROR_CODE)
1724 return dma_addr;
1725
c9b24996 1726 prot = __dma_direction_to_prot(dir);
13987d68
WD
1727
1728 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
4ce63fcd
MS
1729 if (ret < 0)
1730 goto fail;
1731
1732 return dma_addr + offset;
1733fail:
1734 __free_iova(mapping, dma_addr, len);
1735 return DMA_ERROR_CODE;
1736}
1737
0fa478df
RH
1738/**
1739 * arm_iommu_map_page
1740 * @dev: valid struct device pointer
1741 * @page: page that buffer resides in
1742 * @offset: offset into page for start of buffer
1743 * @size: size of buffer to map
1744 * @dir: DMA transfer direction
1745 *
1746 * IOMMU aware version of arm_dma_map_page()
1747 */
1748static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1749 unsigned long offset, size_t size, enum dma_data_direction dir,
1750 struct dma_attrs *attrs)
1751{
1752 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1753 __dma_page_cpu_to_dev(page, offset, size, dir);
1754
1755 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1756}
1757
1758/**
1759 * arm_coherent_iommu_unmap_page
1760 * @dev: valid struct device pointer
1761 * @handle: DMA address of buffer
1762 * @size: size of buffer (same as passed to dma_map_page)
1763 * @dir: DMA transfer direction (same as passed to dma_map_page)
1764 *
1765 * Coherent IOMMU aware version of arm_dma_unmap_page()
1766 */
1767static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1768 size_t size, enum dma_data_direction dir,
1769 struct dma_attrs *attrs)
1770{
1771 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1772 dma_addr_t iova = handle & PAGE_MASK;
0fa478df
RH
1773 int offset = handle & ~PAGE_MASK;
1774 int len = PAGE_ALIGN(size + offset);
1775
1776 if (!iova)
1777 return;
1778
1779 iommu_unmap(mapping->domain, iova, len);
1780 __free_iova(mapping, iova, len);
1781}
1782
4ce63fcd
MS
1783/**
1784 * arm_iommu_unmap_page
1785 * @dev: valid struct device pointer
1786 * @handle: DMA address of buffer
1787 * @size: size of buffer (same as passed to dma_map_page)
1788 * @dir: DMA transfer direction (same as passed to dma_map_page)
1789 *
1790 * IOMMU aware version of arm_dma_unmap_page()
1791 */
1792static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1793 size_t size, enum dma_data_direction dir,
1794 struct dma_attrs *attrs)
1795{
1796 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1797 dma_addr_t iova = handle & PAGE_MASK;
1798 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1799 int offset = handle & ~PAGE_MASK;
1800 int len = PAGE_ALIGN(size + offset);
1801
1802 if (!iova)
1803 return;
1804
0fa478df 1805 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
4ce63fcd
MS
1806 __dma_page_dev_to_cpu(page, offset, size, dir);
1807
1808 iommu_unmap(mapping->domain, iova, len);
1809 __free_iova(mapping, iova, len);
1810}
1811
1812static void arm_iommu_sync_single_for_cpu(struct device *dev,
1813 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1814{
1815 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1816 dma_addr_t iova = handle & PAGE_MASK;
1817 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1818 unsigned int offset = handle & ~PAGE_MASK;
1819
1820 if (!iova)
1821 return;
1822
0fa478df 1823 __dma_page_dev_to_cpu(page, offset, size, dir);
4ce63fcd
MS
1824}
1825
1826static void arm_iommu_sync_single_for_device(struct device *dev,
1827 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1828{
1829 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1830 dma_addr_t iova = handle & PAGE_MASK;
1831 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1832 unsigned int offset = handle & ~PAGE_MASK;
1833
1834 if (!iova)
1835 return;
1836
1837 __dma_page_cpu_to_dev(page, offset, size, dir);
1838}
1839
1840struct dma_map_ops iommu_ops = {
1841 .alloc = arm_iommu_alloc_attrs,
1842 .free = arm_iommu_free_attrs,
1843 .mmap = arm_iommu_mmap_attrs,
dc2832e1 1844 .get_sgtable = arm_iommu_get_sgtable,
4ce63fcd
MS
1845
1846 .map_page = arm_iommu_map_page,
1847 .unmap_page = arm_iommu_unmap_page,
1848 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
1849 .sync_single_for_device = arm_iommu_sync_single_for_device,
1850
1851 .map_sg = arm_iommu_map_sg,
1852 .unmap_sg = arm_iommu_unmap_sg,
1853 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
1854 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
d09e1333
HD
1855
1856 .set_dma_mask = arm_dma_set_mask,
4ce63fcd
MS
1857};
1858
0fa478df
RH
1859struct dma_map_ops iommu_coherent_ops = {
1860 .alloc = arm_iommu_alloc_attrs,
1861 .free = arm_iommu_free_attrs,
1862 .mmap = arm_iommu_mmap_attrs,
1863 .get_sgtable = arm_iommu_get_sgtable,
1864
1865 .map_page = arm_coherent_iommu_map_page,
1866 .unmap_page = arm_coherent_iommu_unmap_page,
1867
1868 .map_sg = arm_coherent_iommu_map_sg,
1869 .unmap_sg = arm_coherent_iommu_unmap_sg,
d09e1333
HD
1870
1871 .set_dma_mask = arm_dma_set_mask,
0fa478df
RH
1872};
1873
4ce63fcd
MS
1874/**
1875 * arm_iommu_create_mapping
1876 * @bus: pointer to the bus holding the client device (for IOMMU calls)
1877 * @base: start address of the valid IO address space
1878 * @size: size of the valid IO address space
1879 * @order: accuracy of the IO addresses allocations
1880 *
1881 * Creates a mapping structure which holds information about used/unused
1882 * IO address ranges, which is required to perform memory allocation and
1883 * mapping with IOMMU aware functions.
1884 *
1885 * The client device need to be attached to the mapping with
1886 * arm_iommu_attach_device function.
1887 */
1888struct dma_iommu_mapping *
1889arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
1890 int order)
1891{
1892 unsigned int count = size >> (PAGE_SHIFT + order);
1893 unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
1894 struct dma_iommu_mapping *mapping;
1895 int err = -ENOMEM;
1896
1897 if (!count)
1898 return ERR_PTR(-EINVAL);
1899
1900 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1901 if (!mapping)
1902 goto err;
1903
1904 mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1905 if (!mapping->bitmap)
1906 goto err2;
1907
1908 mapping->base = base;
1909 mapping->bits = BITS_PER_BYTE * bitmap_size;
1910 mapping->order = order;
1911 spin_lock_init(&mapping->lock);
1912
1913 mapping->domain = iommu_domain_alloc(bus);
1914 if (!mapping->domain)
1915 goto err3;
1916
1917 kref_init(&mapping->kref);
1918 return mapping;
1919err3:
1920 kfree(mapping->bitmap);
1921err2:
1922 kfree(mapping);
1923err:
1924 return ERR_PTR(err);
1925}
18177d12 1926EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
4ce63fcd
MS
1927
1928static void release_iommu_mapping(struct kref *kref)
1929{
1930 struct dma_iommu_mapping *mapping =
1931 container_of(kref, struct dma_iommu_mapping, kref);
1932
1933 iommu_domain_free(mapping->domain);
1934 kfree(mapping->bitmap);
1935 kfree(mapping);
1936}
1937
1938void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1939{
1940 if (mapping)
1941 kref_put(&mapping->kref, release_iommu_mapping);
1942}
18177d12 1943EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
4ce63fcd
MS
1944
1945/**
1946 * arm_iommu_attach_device
1947 * @dev: valid struct device pointer
1948 * @mapping: io address space mapping structure (returned from
1949 * arm_iommu_create_mapping)
1950 *
1951 * Attaches specified io address space mapping to the provided device,
1952 * this replaces the dma operations (dma_map_ops pointer) with the
1953 * IOMMU aware version. More than one client might be attached to
1954 * the same io address space mapping.
1955 */
1956int arm_iommu_attach_device(struct device *dev,
1957 struct dma_iommu_mapping *mapping)
1958{
1959 int err;
1960
1961 err = iommu_attach_device(mapping->domain, dev);
1962 if (err)
1963 return err;
1964
1965 kref_get(&mapping->kref);
1966 dev->archdata.mapping = mapping;
1967 set_dma_ops(dev, &iommu_ops);
1968
75c59716 1969 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
4ce63fcd
MS
1970 return 0;
1971}
18177d12 1972EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
4ce63fcd 1973
6fe36758
HD
1974/**
1975 * arm_iommu_detach_device
1976 * @dev: valid struct device pointer
1977 *
1978 * Detaches the provided device from a previously attached map.
1979 * This voids the dma operations (dma_map_ops pointer)
1980 */
1981void arm_iommu_detach_device(struct device *dev)
1982{
1983 struct dma_iommu_mapping *mapping;
1984
1985 mapping = to_dma_iommu_mapping(dev);
1986 if (!mapping) {
1987 dev_warn(dev, "Not attached\n");
1988 return;
1989 }
1990
1991 iommu_detach_device(mapping->domain, dev);
1992 kref_put(&mapping->kref, release_iommu_mapping);
9e4b259d 1993 dev->archdata.mapping = NULL;
6fe36758
HD
1994 set_dma_ops(dev, NULL);
1995
1996 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
1997}
18177d12 1998EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
6fe36758 1999
4ce63fcd 2000#endif