blob: c2cdf6500f75dc5a1ab5a688ea305099e8d30057 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0ddbccd2008-09-25 15:59:19 +01002 * linux/arch/arm/mm/dma-mapping.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Marek Szyprowskic7909502011-12-29 13:09:51 +010020#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050021#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010022#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010023#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020024#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020025#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020026#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010027#include <linux/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010029#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040030#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010033#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020034#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010035#include <asm/mach/map.h>
36#include <asm/system_info.h>
37#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Russell King022ae532011-07-08 21:26:59 +010039#include "mm.h"
40
Marek Szyprowski15237e12012-02-10 19:55:20 +010041/*
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 *
Marek Szyprowski15237e12012-02-10 19:55:20 +010052 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010053static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010054 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +010055static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010056 size_t, enum dma_data_direction);
57
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010058/**
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 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010072static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010073 unsigned long offset, size_t size, enum dma_data_direction dir,
74 struct dma_attrs *attrs)
75{
Marek Szyprowski97ef9522012-06-06 14:50:56 +020076 if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +010077 __dma_page_cpu_to_dev(page, offset, size, dir);
78 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010079}
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 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010095static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010096 size_t size, enum dma_data_direction dir,
97 struct dma_attrs *attrs)
98{
Marek Szyprowski97ef9522012-06-06 14:50:56 +020099 if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100100 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
101 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100102}
103
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100104static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100105 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));
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100109 if (!arch_is_coherent())
110 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100111}
112
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100113static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100114 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));
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100118 if (!arch_is_coherent())
119 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100120}
121
122static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
123
124struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200125 .alloc = arm_dma_alloc,
126 .free = arm_dma_free,
127 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200128 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100129 .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
Catalin Marinasab6494f2009-07-24 12:35:02 +0100141static u64 get_coherent_dma_mask(struct device *dev)
142{
Russell King022ae532011-07-08 21:26:59 +0100143 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Catalin Marinasab6494f2009-07-24 12:35:02 +0100145 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
Russell King022ae532011-07-08 21:26:59 +0100157 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100158 dev_warn(dev, "coherent DMA mask %#llx is smaller "
159 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100160 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100161 return 0;
162 }
163 }
164
165 return mask;
166}
167
Marek Szyprowskic7909502011-12-29 13:09:51 +0100168static 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);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200176 if (ptr) {
177 memset(ptr, 0, size);
178 dmac_flush_range(ptr, ptr + size);
179 outer_flush_range(__pa(ptr), __pa(ptr) + size);
180 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100181}
182
Russell King7a9a32a2009-11-19 15:31:07 +0000183/*
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;
Russell King7a9a32a2009-11-19 15:31:07 +0000191
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
Marek Szyprowskic7909502011-12-29 13:09:51 +0100203 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000204
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
Catalin Marinasab6494f2009-07-24 12:35:02 +0100221#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#ifdef CONFIG_HUGETLB_PAGE
223#error ARM Coherent DMA allocator does not (yet) support huge TLB
224#endif
225
Marek Szyprowskic7909502011-12-29 13:09:51 +0100226static void *__alloc_from_contiguous(struct device *dev, size_t size,
227 pgprot_t prot, struct page **ret_page);
228
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200229static 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);
232
233static void *
234__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
235 const void *caller)
236{
237 struct vm_struct *area;
238 unsigned long addr;
239
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));
250
251 if (ioremap_page_range(addr, addr + size, area->phys_addr, prot)) {
252 vunmap((void *)addr);
253 return NULL;
254 }
255 return (void *)addr;
256}
257
258static void __dma_free_remap(void *cpu_addr, size_t size)
259{
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;
265 }
266 unmap_kernel_range((unsigned long)cpu_addr, size);
267 vunmap(cpu_addr);
268}
269
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;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100277};
278
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200279static struct dma_pool atomic_pool = {
280 .size = SZ_256K,
281};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100282
283static int __init early_coherent_pool(char *p)
284{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200285 atomic_pool.size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100286 return 0;
287}
288early_param("coherent_pool", early_coherent_pool);
289
290/*
291 * Initialise the coherent pool for atomic allocations.
292 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200293static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100294{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200295 struct dma_pool *pool = &atomic_pool;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100296 pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200297 unsigned long nr_pages = pool->size >> PAGE_SHIFT;
298 unsigned long *bitmap;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100299 struct page *page;
300 void *ptr;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200301 int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100302
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200303 bitmap = kzalloc(bitmap_size, GFP_KERNEL);
304 if (!bitmap)
305 goto no_bitmap;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100306
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200307 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);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100312 if (ptr) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200313 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);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100320 return 0;
321 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200322 kfree(bitmap);
323no_bitmap:
324 pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
325 (unsigned)pool->size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100326 return -ENOMEM;
327}
328/*
329 * CMA is activated by core_initcall, so we must be called after it.
330 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200331postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100332
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)
361 return;
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);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400372 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100373 pmd_clear(pmd_off_k(addr));
374
375 iotable_init(&map, 1);
376 }
377}
378
Marek Szyprowskic7909502011-12-29 13:09:51 +0100379static 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
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200419static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100420{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200421 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;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100426 size_t align;
427
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200428 if (!pool->vaddr) {
429 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100430 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 */
438 align = PAGE_SIZE << get_order(size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200439
440 spin_lock_irqsave(&pool->lock, flags);
441 pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
442 0, count, (1 << align) - 1);
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;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100447 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200448 spin_unlock_irqrestore(&pool->lock, flags);
449
450 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100451}
452
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200453static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100454{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200455 struct dma_pool *pool = &atomic_pool;
456 unsigned long pageno, count;
457 unsigned long flags;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100458
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200459 if (start < pool->vaddr || start > pool->vaddr + pool->size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100460 return 0;
461
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200462 if (start + size > pool->vaddr + pool->size) {
463 WARN(1, "freeing wrong coherent size from pool\n");
464 return 0;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100465 }
466
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200467 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
Marek Szyprowskic7909502011-12-29 13:09:51 +0100474 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
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200502static 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
Marek Szyprowskic7909502011-12-29 13:09:51 +0100510#define nommu() 0
511
Catalin Marinasab6494f2009-07-24 12:35:02 +0100512#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000513
Marek Szyprowskic7909502011-12-29 13:09:51 +0100514#define nommu() 1
515
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200516#define __get_dma_pgprot(attrs, prot) __pgprot(0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100517#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200518#define __alloc_from_pool(size, ret_page) NULL
Marek Szyprowskic7909502011-12-29 13:09:51 +0100519#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)
Russell King31ebf942009-11-19 21:12:17 +0000523
524#endif /* CONFIG_MMU */
525
Marek Szyprowskic7909502011-12-29 13:09:51 +0100526static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
527 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100528{
Russell King04da5692009-11-19 15:54:45 +0000529 struct page *page;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100530 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);
544 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000545 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100546
Marek Szyprowskic7909502011-12-29 13:09:51 +0100547#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
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100562 /*
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
Marek Szyprowski553ac782012-02-29 14:45:28 +0100571 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000572 size = PAGE_ALIGN(size);
573
Marek Szyprowskic7909502011-12-29 13:09:51 +0100574 if (arch_is_coherent() || nommu())
575 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200576 else if (gfp & GFP_ATOMIC)
577 addr = __alloc_from_pool(size, &page);
Marek Szyprowskif1ae98d2012-05-30 10:48:29 +0200578 else if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100579 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000580 else
Marek Szyprowskic7909502011-12-29 13:09:51 +0100581 addr = __alloc_from_contiguous(dev, size, prot, &page);
Russell King31ebf942009-11-19 21:12:17 +0000582
583 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000584 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000585
586 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100587}
Russell King695ae0a2009-11-19 16:31:39 +0000588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
590 * Allocate DMA-coherent memory space and return both the kernel remapped
591 * virtual and bus address for that space.
592 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200593void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
594 gfp_t gfp, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200596 pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400597 void *memory;
598
599 if (dma_alloc_from_coherent(dev, size, handle, &memory))
600 return memory;
601
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200602 return __dma_alloc(dev, size, handle, gfp, prot,
Russell King45cd5292012-01-12 23:08:07 +0000603 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/*
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200607 * Create userspace mapping for the DMA-coherent memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200609int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100613 int ret = -ENXIO;
614#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200615 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
616 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100617 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200618 unsigned long off = vma->vm_pgoff;
619
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200620 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
621
Marek Szyprowski47142f02012-05-15 19:04:13 +0200622 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
623 return ret;
624
Marek Szyprowski50262a42012-07-30 09:35:26 +0200625 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 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100631#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return ret;
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100637 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200639void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
640 dma_addr_t handle, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100642 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000643
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400644 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
645 return;
646
Russell King3e82d012009-11-19 15:38:12 +0000647 size = PAGE_ALIGN(size);
648
Marek Szyprowskic7909502011-12-29 13:09:51 +0100649 if (arch_is_coherent() || nommu()) {
650 __dma_free_buffer(page, size);
Marek Szyprowskif1ae98d2012-05-30 10:48:29 +0200651 } else if (!IS_ENABLED(CONFIG_CMA)) {
Russell King695ae0a2009-11-19 16:31:39 +0000652 __dma_free_remap(cpu_addr, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100653 __dma_free_buffer(page, size);
654 } else {
655 if (__free_from_pool(cpu_addr, size))
656 return;
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
Russell Kingafd1a322008-09-25 16:30:57 +0100664
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200665int 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
Russell King65af1912009-11-24 17:53:33 +0000680static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000681 size_t size, enum dma_data_direction dir,
682 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000683{
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;
Russell King93f1d622009-11-24 14:41:01 +0000693 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;
Russell King65af1912009-11-24 17:53:33 +0000702 }
Russell King93f1d622009-11-24 14:41:01 +0000703 vaddr = kmap_high_get(page);
704 if (vaddr) {
705 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000706 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000707 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100708 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500709 /* unmapped pages might still be cached */
710 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100711 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500712 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000713 }
714 } else {
715 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000716 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000717 }
Russell King65af1912009-11-24 17:53:33 +0000718 offset = 0;
719 page++;
720 left -= len;
721 } while (left);
722}
723
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100724/*
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,
Russell King65af1912009-11-24 17:53:33 +0000731 size_t size, enum dma_data_direction dir)
732{
Nicolas Pitre43377452009-03-12 22:52:09 -0400733 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400734
Russell Kinga9c91472009-11-26 16:19:58 +0000735 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400736
Russell King65af1912009-11-24 17:53:33 +0000737 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000738 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? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400744}
Russell King4ea0d732009-11-24 16:27:17 +0000745
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100746static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000747 size_t size, enum dma_data_direction dir)
748{
Russell King2ffe2da2009-10-31 16:52:16 +0000749 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
Russell Kinga9c91472009-11-26 16:19:58 +0000756 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100757
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);
Russell King4ea0d732009-11-24 16:27:17 +0000763}
Nicolas Pitre43377452009-03-12 22:52:09 -0400764
Russell Kingafd1a322008-09-25 16:30:57 +0100765/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100766 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100767 * @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 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100781int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
782 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100783{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100784 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100785 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100786 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100787
788 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200789#ifdef CONFIG_NEED_SG_DMA_LENGTH
790 s->dma_length = s->length;
791#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100792 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
793 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100794 if (dma_mapping_error(dev, s->dma_address))
795 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100796 }
Russell Kingafd1a322008-09-25 16:30:57 +0100797 return nents;
Russell King01135d922008-09-25 21:05:02 +0100798
799 bad_mapping:
800 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100801 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100802 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100803}
Russell Kingafd1a322008-09-25 16:30:57 +0100804
805/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100806 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100807 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
808 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100809 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100810 * @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 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100815void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
816 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100817{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100818 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100819 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100820
Russell King01135d922008-09-25 21:05:02 +0100821 int i;
Russell King24056f52011-01-03 11:29:28 +0000822
Russell King01135d922008-09-25 21:05:02 +0100823 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100824 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100825}
Russell Kingafd1a322008-09-25 16:30:57 +0100826
827/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100828 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100829 * @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 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100834void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100835 int nents, enum dma_data_direction dir)
836{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100837 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100838 struct scatterlist *s;
839 int i;
840
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100841 for_each_sg(sg, s, nents, i)
842 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
843 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100844}
Russell Kingafd1a322008-09-25 16:30:57 +0100845
846/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100847 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100848 * @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 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100853void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100854 int nents, enum dma_data_direction dir)
855{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100856 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100857 struct scatterlist *s;
858 int i;
859
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100860 for_each_sg(sg, s, nents, i)
861 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
862 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100863}
Russell King24056f52011-01-03 11:29:28 +0000864
Russell King022ae532011-07-08 21:26:59 +0100865/*
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
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100879static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +0100880{
881 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
882 return -EIO;
883
Russell King022ae532011-07-08 21:26:59 +0100884 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +0100885
886 return 0;
887}
Russell King022ae532011-07-08 21:26:59 +0100888
Russell King24056f52011-01-03 11:29:28 +0000889#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);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200897
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) {
Marek Szyprowski593f4732012-06-21 11:48:11 +0200959 int j, order = __fls(count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200960
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:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +0200980 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200981 if (pages[i])
982 __free_pages(pages[i], 0);
Prathyush K46c87852012-07-16 08:59:55 +0200983 if (array_size <= PAGE_SIZE)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200984 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);
Prathyush K46c87852012-07-16 08:59:55 +0200998 if (array_size <= PAGE_SIZE)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200999 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 *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001009__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1010 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001011{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001012 unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1013 struct vm_struct *area;
1014 unsigned long p;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001015
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001016 area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
1017 caller);
1018 if (!area)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001019 return NULL;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001020
1021 area->pages = pages;
1022 area->nr_pages = nr_pages;
1023 p = (unsigned long)area->addr;
1024
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;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001030 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001031 return area->addr;
1032err:
1033 unmap_kernel_range((unsigned long)area->addr, size);
1034 vunmap(area->addr);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001035 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
Marek Szyprowski955c7572012-05-16 19:38:58 +01001093static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001094{
1095 struct vm_struct *area;
1096
Marek Szyprowski955c7572012-05-16 19:38:58 +01001097 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1098 return cpu_addr;
1099
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001100 area = find_vm_area(cpu_addr);
1101 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1102 return area->pages;
1103 return NULL;
1104}
1105
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001106static 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
Marek Szyprowski955c7572012-05-16 19:38:58 +01001124 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1125 return pages;
1126
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001127 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1128 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001129 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{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001145 unsigned long uaddr = vma->vm_start;
1146 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001147 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001148
1149 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001150
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001151 if (!pages)
1152 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001153
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001154 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);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001163
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001164 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{
Marek Szyprowski955c7572012-05-16 19:38:58 +01001174 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001175 size = PAGE_ALIGN(size);
1176
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001177 if (!pages) {
1178 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1179 return;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001180 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001181
Marek Szyprowski955c7572012-05-16 19:38:58 +01001182 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
1183 unmap_kernel_range((unsigned long)cpu_addr, size);
1184 vunmap(cpu_addr);
1185 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001186
1187 __iommu_remove_mapping(dev, handle, size);
1188 __iommu_free_buffer(dev, pages, size);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001189}
1190
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001191static 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);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001203}
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,
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001210 enum dma_data_direction dir, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001211{
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
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001229 if (!arch_is_coherent() &&
1230 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001231 __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,
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001277 dir, attrs) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001278 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 }
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001290 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001291 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));
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001324 if (!arch_is_coherent() &&
1325 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001326 __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
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001387 if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001388 __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
Marek Szyprowski97ef9522012-06-06 14:50:56 +02001426 if (!arch_is_coherent() && !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001427 __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,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001466 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001467
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