blob: b50fa578df818ee58c381a620afee22003169f48 [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>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050020#include <linux/highmem.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010023#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040024#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/tlbflush.h>
Kevin Hilman37134cd2006-01-12 16:12:21 +000027#include <asm/sizes.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010028#include <asm/mach/arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Russell King022ae532011-07-08 21:26:59 +010030#include "mm.h"
31
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010032/**
33 * arm_dma_map_page - map a portion of a page for streaming DMA
34 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
35 * @page: page that buffer resides in
36 * @offset: offset into page for start of buffer
37 * @size: size of buffer to map
38 * @dir: DMA transfer direction
39 *
40 * Ensure that any data held in the cache is appropriately discarded
41 * or written back.
42 *
43 * The device owns this memory once this call has completed. The CPU
44 * can regain ownership by calling dma_unmap_page().
45 */
46static inline dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
47 unsigned long offset, size_t size, enum dma_data_direction dir,
48 struct dma_attrs *attrs)
49{
50 return __dma_map_page(dev, page, offset, size, dir);
51}
52
53/**
54 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
55 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
56 * @handle: DMA address of buffer
57 * @size: size of buffer (same as passed to dma_map_page)
58 * @dir: DMA transfer direction (same as passed to dma_map_page)
59 *
60 * Unmap a page streaming mode DMA translation. The handle and size
61 * must match what was provided in the previous dma_map_page() call.
62 * All other usages are undefined.
63 *
64 * After this call, reads by the CPU to the buffer are guaranteed to see
65 * whatever the device wrote there.
66 */
67static inline void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
68 size_t size, enum dma_data_direction dir,
69 struct dma_attrs *attrs)
70{
71 __dma_unmap_page(dev, handle, size, dir);
72}
73
74static inline void arm_dma_sync_single_for_cpu(struct device *dev,
75 dma_addr_t handle, size_t size, enum dma_data_direction dir)
76{
77 unsigned int offset = handle & (PAGE_SIZE - 1);
78 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
79 if (!dmabounce_sync_for_cpu(dev, handle, size, dir))
80 return;
81
82 __dma_page_dev_to_cpu(page, offset, size, dir);
83}
84
85static inline void arm_dma_sync_single_for_device(struct device *dev,
86 dma_addr_t handle, size_t size, enum dma_data_direction dir)
87{
88 unsigned int offset = handle & (PAGE_SIZE - 1);
89 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
90 if (!dmabounce_sync_for_device(dev, handle, size, dir))
91 return;
92
93 __dma_page_cpu_to_dev(page, offset, size, dir);
94}
95
96static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
97
98struct dma_map_ops arm_dma_ops = {
99 .map_page = arm_dma_map_page,
100 .unmap_page = arm_dma_unmap_page,
101 .map_sg = arm_dma_map_sg,
102 .unmap_sg = arm_dma_unmap_sg,
103 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
104 .sync_single_for_device = arm_dma_sync_single_for_device,
105 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
106 .sync_sg_for_device = arm_dma_sync_sg_for_device,
107 .set_dma_mask = arm_dma_set_mask,
108};
109EXPORT_SYMBOL(arm_dma_ops);
110
Catalin Marinasab6494f2009-07-24 12:35:02 +0100111static u64 get_coherent_dma_mask(struct device *dev)
112{
Russell King022ae532011-07-08 21:26:59 +0100113 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Catalin Marinasab6494f2009-07-24 12:35:02 +0100115 if (dev) {
116 mask = dev->coherent_dma_mask;
117
118 /*
119 * Sanity check the DMA mask - it must be non-zero, and
120 * must be able to be satisfied by a DMA allocation.
121 */
122 if (mask == 0) {
123 dev_warn(dev, "coherent DMA mask is unset\n");
124 return 0;
125 }
126
Russell King022ae532011-07-08 21:26:59 +0100127 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100128 dev_warn(dev, "coherent DMA mask %#llx is smaller "
129 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100130 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100131 return 0;
132 }
133 }
134
135 return mask;
136}
137
Russell King7a9a32a2009-11-19 15:31:07 +0000138/*
139 * Allocate a DMA buffer for 'dev' of size 'size' using the
140 * specified gfp mask. Note that 'size' must be page aligned.
141 */
142static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
143{
144 unsigned long order = get_order(size);
145 struct page *page, *p, *e;
146 void *ptr;
147 u64 mask = get_coherent_dma_mask(dev);
148
149#ifdef CONFIG_DMA_API_DEBUG
150 u64 limit = (mask + 1) & ~mask;
151 if (limit && size >= limit) {
152 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
153 size, mask);
154 return NULL;
155 }
156#endif
157
158 if (!mask)
159 return NULL;
160
161 if (mask < 0xffffffffULL)
162 gfp |= GFP_DMA;
163
164 page = alloc_pages(gfp, order);
165 if (!page)
166 return NULL;
167
168 /*
169 * Now split the huge page and free the excess pages
170 */
171 split_page(page, order);
172 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
173 __free_page(p);
174
175 /*
176 * Ensure that the allocated pages are zeroed, and that any data
177 * lurking in the kernel direct-mapped region is invalidated.
178 */
179 ptr = page_address(page);
180 memset(ptr, 0, size);
181 dmac_flush_range(ptr, ptr + size);
182 outer_flush_range(__pa(ptr), __pa(ptr) + size);
183
184 return page;
185}
186
187/*
188 * Free a DMA buffer. 'size' must be page aligned.
189 */
190static void __dma_free_buffer(struct page *page, size_t size)
191{
192 struct page *e = page + (size >> PAGE_SHIFT);
193
194 while (page < e) {
195 __free_page(page);
196 page++;
197 }
198}
199
Catalin Marinasab6494f2009-07-24 12:35:02 +0100200#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100201
Jon Medhurst99d17172011-08-02 17:28:27 +0100202#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700203#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000206 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100208static pte_t **consistent_pte;
209
Jon Medhurst99d17172011-08-02 17:28:27 +0100210#define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
Jon Medhurst99d17172011-08-02 17:28:27 +0100211
212unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
213
214void __init init_consistent_dma_size(unsigned long size)
215{
216 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
217
218 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
219 BUG_ON(base < VMALLOC_END);
220
221 /* Grow region to accommodate specified size */
222 if (base < consistent_base)
223 consistent_base = base;
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Russell King13ccf3a2009-11-19 15:07:04 +0000226#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Russell King13ccf3a2009-11-19 15:07:04 +0000228static struct arm_vmregion_head consistent_head = {
229 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .vm_end = CONSISTENT_END,
232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#ifdef CONFIG_HUGETLB_PAGE
235#error ARM Coherent DMA allocator does not (yet) support huge TLB
236#endif
237
Russell King88c58f32009-11-19 16:46:02 +0000238/*
239 * Initialise the consistent memory allocation.
240 */
241static int __init consistent_init(void)
242{
243 int ret = 0;
244 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000245 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000246 pmd_t *pmd;
247 pte_t *pte;
248 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100249 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100250 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100251
252 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
253 if (!consistent_pte) {
254 pr_err("%s: no memory\n", __func__);
255 return -ENOMEM;
256 }
257
258 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
259 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000260
261 do {
262 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000263
264 pud = pud_alloc(&init_mm, pgd, base);
265 if (!pud) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100266 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000267 ret = -ENOMEM;
268 break;
269 }
270
271 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000272 if (!pmd) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100273 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000274 ret = -ENOMEM;
275 break;
276 }
277 WARN_ON(!pmd_none(*pmd));
278
279 pte = pte_alloc_kernel(pmd, base);
280 if (!pte) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100281 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000282 ret = -ENOMEM;
283 break;
284 }
285
286 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100287 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000288 } while (base < CONSISTENT_END);
289
290 return ret;
291}
292
293core_initcall(consistent_init);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static void *
Russell King45cd5292012-01-12 23:08:07 +0000296__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
297 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Russell King13ccf3a2009-11-19 15:07:04 +0000299 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100300 size_t align;
301 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jon Medhurst99d17172011-08-02 17:28:27 +0100303 if (!consistent_pte) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100304 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000305 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000306 return NULL;
307 }
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
Russell King5bc23d32010-07-25 08:57:02 +0100310 * Align the virtual region allocation - maximum alignment is
311 * a section size, minimum is a page size. This helps reduce
312 * fragmentation of the DMA space, and also prevents allocations
313 * smaller than a section from crossing a section boundary.
314 */
Russell Kingc947f692010-11-03 16:00:15 +0000315 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100316 if (bit > SECTION_SHIFT)
317 bit = SECTION_SHIFT;
318 align = 1 << bit;
319
320 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * Allocate a virtual address in the consistent mapping region.
322 */
Russell King5bc23d32010-07-25 08:57:02 +0100323 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000324 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000326 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000327 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
328 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Kevin Hilman37134cd2006-01-12 16:12:21 +0000330 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 c->vm_pages = page;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 do {
334 BUG_ON(!pte_none(*pte));
335
Russell Kingad1ae2f2006-12-13 14:34:43 +0000336 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 page++;
338 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000339 off++;
340 if (off >= PTRS_PER_PTE) {
341 off = 0;
342 pte = consistent_pte[++idx];
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } while (size -= PAGE_SIZE);
345
Russell King2be23c42010-09-08 16:27:56 +0100346 dsb();
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return (void *)c->vm_start;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return NULL;
351}
Russell King695ae0a2009-11-19 16:31:39 +0000352
353static void __dma_free_remap(void *cpu_addr, size_t size)
354{
355 struct arm_vmregion *c;
356 unsigned long addr;
357 pte_t *ptep;
358 int idx;
359 u32 off;
360
361 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
362 if (!c) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100363 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000364 __func__, cpu_addr);
365 dump_stack();
366 return;
367 }
368
369 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100370 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000371 __func__, c->vm_end - c->vm_start, size);
372 dump_stack();
373 size = c->vm_end - c->vm_start;
374 }
375
376 idx = CONSISTENT_PTE_INDEX(c->vm_start);
377 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
378 ptep = consistent_pte[idx] + off;
379 addr = c->vm_start;
380 do {
381 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000382
383 ptep++;
384 addr += PAGE_SIZE;
385 off++;
386 if (off >= PTRS_PER_PTE) {
387 off = 0;
388 ptep = consistent_pte[++idx];
389 }
390
Russell Kingacaac252009-11-20 18:19:52 +0000391 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100392 pr_crit("%s: bad page in kernel page table\n",
393 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000394 } while (size -= PAGE_SIZE);
395
396 flush_tlb_kernel_range(c->vm_start, c->vm_end);
397
398 arm_vmregion_free(&consistent_head, c);
399}
400
Catalin Marinasab6494f2009-07-24 12:35:02 +0100401#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000402
Russell King45cd5292012-01-12 23:08:07 +0000403#define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page)
Russell King31ebf942009-11-19 21:12:17 +0000404#define __dma_free_remap(addr, size) do { } while (0)
405
406#endif /* CONFIG_MMU */
407
Catalin Marinasab6494f2009-07-24 12:35:02 +0100408static void *
409__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
Russell King45cd5292012-01-12 23:08:07 +0000410 pgprot_t prot, const void *caller)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100411{
Russell King04da5692009-11-19 15:54:45 +0000412 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000413 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100414
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100415 /*
416 * Following is a work-around (a.k.a. hack) to prevent pages
417 * with __GFP_COMP being passed to split_page() which cannot
418 * handle them. The real problem is that this flag probably
419 * should be 0 on ARM as it is not supported on this
420 * platform; see CONFIG_HUGETLBFS.
421 */
422 gfp &= ~(__GFP_COMP);
423
Marek Szyprowski553ac782012-02-29 14:45:28 +0100424 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000425 size = PAGE_ALIGN(size);
426
427 page = __dma_alloc_buffer(dev, size, gfp);
428 if (!page)
429 return NULL;
430
Russell King31ebf942009-11-19 21:12:17 +0000431 if (!arch_is_coherent())
Russell King45cd5292012-01-12 23:08:07 +0000432 addr = __dma_alloc_remap(page, size, gfp, prot, caller);
Russell King31ebf942009-11-19 21:12:17 +0000433 else
434 addr = page_address(page);
435
436 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000437 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell Kingd8e89b42011-09-22 10:32:25 +0100438 else
439 __dma_free_buffer(page, size);
Russell King31ebf942009-11-19 21:12:17 +0000440
441 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100442}
Russell King695ae0a2009-11-19 16:31:39 +0000443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * Allocate DMA-coherent memory space and return both the kernel remapped
446 * virtual and bus address for that space.
447 */
448void *
Al Virof9e32142005-10-21 03:20:58 -0400449dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400451 void *memory;
452
453 if (dma_alloc_from_coherent(dev, size, handle, &memory))
454 return memory;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000457 pgprot_dmacoherent(pgprot_kernel),
458 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460EXPORT_SYMBOL(dma_alloc_coherent);
461
462/*
463 * Allocate a writecombining region, in much the same way as
464 * dma_alloc_coherent above.
465 */
466void *
Al Virof9e32142005-10-21 03:20:58 -0400467dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000470 pgprot_writecombine(pgprot_kernel),
471 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473EXPORT_SYMBOL(dma_alloc_writecombine);
474
475static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
476 void *cpu_addr, dma_addr_t dma_addr, size_t size)
477{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100478 int ret = -ENXIO;
479#ifdef CONFIG_MMU
Russell King13ccf3a2009-11-19 15:07:04 +0000480 unsigned long user_size, kern_size;
481 struct arm_vmregion *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Marek Szyprowski47142f02012-05-15 19:04:13 +0200483 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
484 return ret;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
487
Russell King13ccf3a2009-11-19 15:07:04 +0000488 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (c) {
490 unsigned long off = vma->vm_pgoff;
491
492 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
493
494 if (off < kern_size &&
495 user_size <= (kern_size - off)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ret = remap_pfn_range(vma, vma->vm_start,
497 page_to_pfn(c->vm_pages) + off,
498 user_size << PAGE_SHIFT,
499 vma->vm_page_prot);
500 }
501 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100502#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return ret;
505}
506
507int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
508 void *cpu_addr, dma_addr_t dma_addr, size_t size)
509{
Russell King26a26d32009-11-20 21:06:43 +0000510 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
512}
513EXPORT_SYMBOL(dma_mmap_coherent);
514
515int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
516 void *cpu_addr, dma_addr_t dma_addr, size_t size)
517{
518 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
519 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
520}
521EXPORT_SYMBOL(dma_mmap_writecombine);
522
523/*
524 * free a page as defined by the above mapping.
Russell King5edf71a2005-11-25 15:52:51 +0000525 * Must not be called with IRQs disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
527void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
528{
Russell King5edf71a2005-11-25 15:52:51 +0000529 WARN_ON(irqs_disabled());
530
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400531 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
532 return;
533
Russell King3e82d012009-11-19 15:38:12 +0000534 size = PAGE_ALIGN(size);
535
Russell King695ae0a2009-11-19 16:31:39 +0000536 if (!arch_is_coherent())
537 __dma_free_remap(cpu_addr, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000538
Russell King9eedd962011-01-03 00:00:17 +0000539 __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541EXPORT_SYMBOL(dma_free_coherent);
542
Russell King65af1912009-11-24 17:53:33 +0000543static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000544 size_t size, enum dma_data_direction dir,
545 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000546{
547 /*
548 * A single sg entry may refer to multiple physically contiguous
549 * pages. But we still need to process highmem pages individually.
550 * If highmem is not configured then the bulk of this loop gets
551 * optimized out.
552 */
553 size_t left = size;
554 do {
555 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000556 void *vaddr;
557
558 if (PageHighMem(page)) {
559 if (len + offset > PAGE_SIZE) {
560 if (offset >= PAGE_SIZE) {
561 page += offset / PAGE_SIZE;
562 offset %= PAGE_SIZE;
563 }
564 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000565 }
Russell King93f1d622009-11-24 14:41:01 +0000566 vaddr = kmap_high_get(page);
567 if (vaddr) {
568 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000569 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000570 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100571 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500572 /* unmapped pages might still be cached */
573 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100574 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500575 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000576 }
577 } else {
578 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000579 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000580 }
Russell King65af1912009-11-24 17:53:33 +0000581 offset = 0;
582 page++;
583 left -= len;
584 } while (left);
585}
586
587void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
588 size_t size, enum dma_data_direction dir)
589{
Nicolas Pitre43377452009-03-12 22:52:09 -0400590 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400591
Russell Kinga9c91472009-11-26 16:19:58 +0000592 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400593
Russell King65af1912009-11-24 17:53:33 +0000594 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000595 if (dir == DMA_FROM_DEVICE) {
596 outer_inv_range(paddr, paddr + size);
597 } else {
598 outer_clean_range(paddr, paddr + size);
599 }
600 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400601}
Russell King4ea0d732009-11-24 16:27:17 +0000602EXPORT_SYMBOL(___dma_page_cpu_to_dev);
603
604void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
605 size_t size, enum dma_data_direction dir)
606{
Russell King2ffe2da2009-10-31 16:52:16 +0000607 unsigned long paddr = page_to_phys(page) + off;
608
609 /* FIXME: non-speculating: not required */
610 /* don't bother invalidating if DMA to device */
611 if (dir != DMA_TO_DEVICE)
612 outer_inv_range(paddr, paddr + size);
613
Russell Kinga9c91472009-11-26 16:19:58 +0000614 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100615
616 /*
617 * Mark the D-cache clean for this page to avoid extra flushing.
618 */
619 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
620 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000621}
622EXPORT_SYMBOL(___dma_page_dev_to_cpu);
Nicolas Pitre43377452009-03-12 22:52:09 -0400623
Russell Kingafd1a322008-09-25 16:30:57 +0100624/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100625 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100626 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
627 * @sg: list of buffers
628 * @nents: number of buffers to map
629 * @dir: DMA transfer direction
630 *
631 * Map a set of buffers described by scatterlist in streaming mode for DMA.
632 * This is the scatter-gather version of the dma_map_single interface.
633 * Here the scatter gather list elements are each tagged with the
634 * appropriate dma address and length. They are obtained via
635 * sg_dma_{address,length}.
636 *
637 * Device ownership issues as mentioned for dma_map_single are the same
638 * here.
639 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100640int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
641 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100642{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100643 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100644 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100645 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100646
647 for_each_sg(sg, s, nents, i) {
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100648 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
649 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100650 if (dma_mapping_error(dev, s->dma_address))
651 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100652 }
Russell Kingafd1a322008-09-25 16:30:57 +0100653 return nents;
Russell King01135d922008-09-25 21:05:02 +0100654
655 bad_mapping:
656 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100657 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100658 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100659}
Russell Kingafd1a322008-09-25 16:30:57 +0100660
661/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100662 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100663 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
664 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100665 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100666 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
667 *
668 * Unmap a set of streaming mode DMA translations. Again, CPU access
669 * rules concerning calls here are the same as for dma_unmap_single().
670 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100671void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
672 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100673{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100674 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100675 struct scatterlist *s;
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100676
Russell King01135d922008-09-25 21:05:02 +0100677 int i;
678
679 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100680 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100681}
Russell Kingafd1a322008-09-25 16:30:57 +0100682
683/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100684 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100685 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
686 * @sg: list of buffers
687 * @nents: number of buffers to map (returned from dma_map_sg)
688 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
689 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100690void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100691 int nents, enum dma_data_direction dir)
692{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100693 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100694 struct scatterlist *s;
695 int i;
696
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100697 for_each_sg(sg, s, nents, i)
698 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
699 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100700}
Russell Kingafd1a322008-09-25 16:30:57 +0100701
702/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100703 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100704 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
705 * @sg: list of buffers
706 * @nents: number of buffers to map (returned from dma_map_sg)
707 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
708 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100709void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100710 int nents, enum dma_data_direction dir)
711{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100712 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100713 struct scatterlist *s;
714 int i;
715
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100716 for_each_sg(sg, s, nents, i)
717 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
718 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100719}
Russell King24056f52011-01-03 11:29:28 +0000720
Russell King022ae532011-07-08 21:26:59 +0100721/*
722 * Return whether the given device DMA address mask can be supported
723 * properly. For example, if your device can only drive the low 24-bits
724 * during bus mastering, then you would pass 0x00ffffff as the mask
725 * to this function.
726 */
727int dma_supported(struct device *dev, u64 mask)
728{
729 if (mask < (u64)arm_dma_limit)
730 return 0;
731 return 1;
732}
733EXPORT_SYMBOL(dma_supported);
734
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100735static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +0100736{
737 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
738 return -EIO;
739
740#ifndef CONFIG_DMABOUNCE
741 *dev->dma_mask = dma_mask;
742#endif
743
744 return 0;
745}
Russell King022ae532011-07-08 21:26:59 +0100746
Russell King24056f52011-01-03 11:29:28 +0000747#define PREALLOC_DMA_DEBUG_ENTRIES 4096
748
749static int __init dma_debug_do_init(void)
750{
Russell King45cd5292012-01-12 23:08:07 +0000751#ifdef CONFIG_MMU
752 arm_vmregion_create_proc("dma-mappings", &consistent_head);
753#endif
Russell King24056f52011-01-03 11:29:28 +0000754 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
755 return 0;
756}
757fs_initcall(dma_debug_do_init);