blob: c94966891dee238d565183901a68df071a13342f [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 Szyprowski15237e12012-02-10 19:55:20 +010032/*
33 * The DMA API is built upon the notion of "buffer ownership". A buffer
34 * is either exclusively owned by the CPU (and therefore may be accessed
35 * by it) or exclusively owned by the DMA device. These helper functions
36 * represent the transitions between these two ownership states.
37 *
38 * Note, however, that on later ARMs, this notion does not work due to
39 * speculative prefetches. We model our approach on the assumption that
40 * the CPU does do speculative prefetches, which means we clean caches
41 * before transfers and delay cache invalidation until transfer completion.
42 *
43 * Private support functions: these are not part of the API and are
44 * liable to change. Drivers must not use these.
45 */
46static inline void __dma_single_cpu_to_dev(const void *kaddr, size_t size,
47 enum dma_data_direction dir)
48{
49 extern void ___dma_single_cpu_to_dev(const void *, size_t,
50 enum dma_data_direction);
51
52 if (!arch_is_coherent())
53 ___dma_single_cpu_to_dev(kaddr, size, dir);
54}
55
56static inline void __dma_single_dev_to_cpu(const void *kaddr, size_t size,
57 enum dma_data_direction dir)
58{
59 extern void ___dma_single_dev_to_cpu(const void *, size_t,
60 enum dma_data_direction);
61
62 if (!arch_is_coherent())
63 ___dma_single_dev_to_cpu(kaddr, size, dir);
64}
65
66static inline void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
67 size_t size, enum dma_data_direction dir)
68{
69 extern void ___dma_page_cpu_to_dev(struct page *, unsigned long,
70 size_t, enum dma_data_direction);
71
72 if (!arch_is_coherent())
73 ___dma_page_cpu_to_dev(page, off, size, dir);
74}
75
76static inline void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
77 size_t size, enum dma_data_direction dir)
78{
79 extern void ___dma_page_dev_to_cpu(struct page *, unsigned long,
80 size_t, enum dma_data_direction);
81
82 if (!arch_is_coherent())
83 ___dma_page_dev_to_cpu(page, off, size, dir);
84}
85
86
87static inline dma_addr_t __dma_map_page(struct device *dev, struct page *page,
88 unsigned long offset, size_t size, enum dma_data_direction dir)
89{
90 __dma_page_cpu_to_dev(page, offset, size, dir);
91 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
92}
93
94static inline void __dma_unmap_page(struct device *dev, dma_addr_t handle,
95 size_t size, enum dma_data_direction dir)
96{
97 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
98 handle & ~PAGE_MASK, size, dir);
99}
100
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100101/**
102 * arm_dma_map_page - map a portion of a page for streaming DMA
103 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
104 * @page: page that buffer resides in
105 * @offset: offset into page for start of buffer
106 * @size: size of buffer to map
107 * @dir: DMA transfer direction
108 *
109 * Ensure that any data held in the cache is appropriately discarded
110 * or written back.
111 *
112 * The device owns this memory once this call has completed. The CPU
113 * can regain ownership by calling dma_unmap_page().
114 */
115static inline dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
116 unsigned long offset, size_t size, enum dma_data_direction dir,
117 struct dma_attrs *attrs)
118{
119 return __dma_map_page(dev, page, offset, size, dir);
120}
121
122/**
123 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
124 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
125 * @handle: DMA address of buffer
126 * @size: size of buffer (same as passed to dma_map_page)
127 * @dir: DMA transfer direction (same as passed to dma_map_page)
128 *
129 * Unmap a page streaming mode DMA translation. The handle and size
130 * must match what was provided in the previous dma_map_page() call.
131 * All other usages are undefined.
132 *
133 * After this call, reads by the CPU to the buffer are guaranteed to see
134 * whatever the device wrote there.
135 */
136static inline void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
137 size_t size, enum dma_data_direction dir,
138 struct dma_attrs *attrs)
139{
140 __dma_unmap_page(dev, handle, size, dir);
141}
142
143static inline void arm_dma_sync_single_for_cpu(struct device *dev,
144 dma_addr_t handle, size_t size, enum dma_data_direction dir)
145{
146 unsigned int offset = handle & (PAGE_SIZE - 1);
147 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100148 __dma_page_dev_to_cpu(page, offset, size, dir);
149}
150
151static inline void arm_dma_sync_single_for_device(struct device *dev,
152 dma_addr_t handle, size_t size, enum dma_data_direction dir)
153{
154 unsigned int offset = handle & (PAGE_SIZE - 1);
155 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100156 __dma_page_cpu_to_dev(page, offset, size, dir);
157}
158
159static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
160
161struct dma_map_ops arm_dma_ops = {
162 .map_page = arm_dma_map_page,
163 .unmap_page = arm_dma_unmap_page,
164 .map_sg = arm_dma_map_sg,
165 .unmap_sg = arm_dma_unmap_sg,
166 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
167 .sync_single_for_device = arm_dma_sync_single_for_device,
168 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
169 .sync_sg_for_device = arm_dma_sync_sg_for_device,
170 .set_dma_mask = arm_dma_set_mask,
171};
172EXPORT_SYMBOL(arm_dma_ops);
173
Catalin Marinasab6494f2009-07-24 12:35:02 +0100174static u64 get_coherent_dma_mask(struct device *dev)
175{
Russell King022ae532011-07-08 21:26:59 +0100176 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Catalin Marinasab6494f2009-07-24 12:35:02 +0100178 if (dev) {
179 mask = dev->coherent_dma_mask;
180
181 /*
182 * Sanity check the DMA mask - it must be non-zero, and
183 * must be able to be satisfied by a DMA allocation.
184 */
185 if (mask == 0) {
186 dev_warn(dev, "coherent DMA mask is unset\n");
187 return 0;
188 }
189
Russell King022ae532011-07-08 21:26:59 +0100190 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100191 dev_warn(dev, "coherent DMA mask %#llx is smaller "
192 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100193 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100194 return 0;
195 }
196 }
197
198 return mask;
199}
200
Russell King7a9a32a2009-11-19 15:31:07 +0000201/*
202 * Allocate a DMA buffer for 'dev' of size 'size' using the
203 * specified gfp mask. Note that 'size' must be page aligned.
204 */
205static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
206{
207 unsigned long order = get_order(size);
208 struct page *page, *p, *e;
209 void *ptr;
210 u64 mask = get_coherent_dma_mask(dev);
211
212#ifdef CONFIG_DMA_API_DEBUG
213 u64 limit = (mask + 1) & ~mask;
214 if (limit && size >= limit) {
215 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
216 size, mask);
217 return NULL;
218 }
219#endif
220
221 if (!mask)
222 return NULL;
223
224 if (mask < 0xffffffffULL)
225 gfp |= GFP_DMA;
226
227 page = alloc_pages(gfp, order);
228 if (!page)
229 return NULL;
230
231 /*
232 * Now split the huge page and free the excess pages
233 */
234 split_page(page, order);
235 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
236 __free_page(p);
237
238 /*
239 * Ensure that the allocated pages are zeroed, and that any data
240 * lurking in the kernel direct-mapped region is invalidated.
241 */
242 ptr = page_address(page);
243 memset(ptr, 0, size);
244 dmac_flush_range(ptr, ptr + size);
245 outer_flush_range(__pa(ptr), __pa(ptr) + size);
246
247 return page;
248}
249
250/*
251 * Free a DMA buffer. 'size' must be page aligned.
252 */
253static void __dma_free_buffer(struct page *page, size_t size)
254{
255 struct page *e = page + (size >> PAGE_SHIFT);
256
257 while (page < e) {
258 __free_page(page);
259 page++;
260 }
261}
262
Catalin Marinasab6494f2009-07-24 12:35:02 +0100263#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100264
Jon Medhurst99d17172011-08-02 17:28:27 +0100265#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700266#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000269 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100271static pte_t **consistent_pte;
272
Jon Medhurst99d17172011-08-02 17:28:27 +0100273#define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
Jon Medhurst99d17172011-08-02 17:28:27 +0100274
275unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
276
277void __init init_consistent_dma_size(unsigned long size)
278{
279 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
280
281 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
282 BUG_ON(base < VMALLOC_END);
283
284 /* Grow region to accommodate specified size */
285 if (base < consistent_base)
286 consistent_base = base;
287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Russell King13ccf3a2009-11-19 15:07:04 +0000289#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Russell King13ccf3a2009-11-19 15:07:04 +0000291static struct arm_vmregion_head consistent_head = {
292 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .vm_end = CONSISTENT_END,
295};
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#ifdef CONFIG_HUGETLB_PAGE
298#error ARM Coherent DMA allocator does not (yet) support huge TLB
299#endif
300
Russell King88c58f32009-11-19 16:46:02 +0000301/*
302 * Initialise the consistent memory allocation.
303 */
304static int __init consistent_init(void)
305{
306 int ret = 0;
307 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000308 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000309 pmd_t *pmd;
310 pte_t *pte;
311 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100312 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100313 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100314
315 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
316 if (!consistent_pte) {
317 pr_err("%s: no memory\n", __func__);
318 return -ENOMEM;
319 }
320
321 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
322 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000323
324 do {
325 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000326
327 pud = pud_alloc(&init_mm, pgd, base);
328 if (!pud) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100329 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000330 ret = -ENOMEM;
331 break;
332 }
333
334 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000335 if (!pmd) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100336 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000337 ret = -ENOMEM;
338 break;
339 }
340 WARN_ON(!pmd_none(*pmd));
341
342 pte = pte_alloc_kernel(pmd, base);
343 if (!pte) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100344 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000345 ret = -ENOMEM;
346 break;
347 }
348
349 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100350 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000351 } while (base < CONSISTENT_END);
352
353 return ret;
354}
355
356core_initcall(consistent_init);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static void *
Russell King45cd5292012-01-12 23:08:07 +0000359__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
360 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Russell King13ccf3a2009-11-19 15:07:04 +0000362 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100363 size_t align;
364 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jon Medhurst99d17172011-08-02 17:28:27 +0100366 if (!consistent_pte) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100367 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000368 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000369 return NULL;
370 }
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
Russell King5bc23d32010-07-25 08:57:02 +0100373 * Align the virtual region allocation - maximum alignment is
374 * a section size, minimum is a page size. This helps reduce
375 * fragmentation of the DMA space, and also prevents allocations
376 * smaller than a section from crossing a section boundary.
377 */
Russell Kingc947f692010-11-03 16:00:15 +0000378 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100379 if (bit > SECTION_SHIFT)
380 bit = SECTION_SHIFT;
381 align = 1 << bit;
382
383 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * Allocate a virtual address in the consistent mapping region.
385 */
Russell King5bc23d32010-07-25 08:57:02 +0100386 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000387 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000389 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000390 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
391 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Kevin Hilman37134cd2006-01-12 16:12:21 +0000393 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 c->vm_pages = page;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 do {
397 BUG_ON(!pte_none(*pte));
398
Russell Kingad1ae2f2006-12-13 14:34:43 +0000399 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 page++;
401 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000402 off++;
403 if (off >= PTRS_PER_PTE) {
404 off = 0;
405 pte = consistent_pte[++idx];
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 } while (size -= PAGE_SIZE);
408
Russell King2be23c42010-09-08 16:27:56 +0100409 dsb();
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return (void *)c->vm_start;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return NULL;
414}
Russell King695ae0a2009-11-19 16:31:39 +0000415
416static void __dma_free_remap(void *cpu_addr, size_t size)
417{
418 struct arm_vmregion *c;
419 unsigned long addr;
420 pte_t *ptep;
421 int idx;
422 u32 off;
423
424 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
425 if (!c) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100426 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000427 __func__, cpu_addr);
428 dump_stack();
429 return;
430 }
431
432 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100433 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000434 __func__, c->vm_end - c->vm_start, size);
435 dump_stack();
436 size = c->vm_end - c->vm_start;
437 }
438
439 idx = CONSISTENT_PTE_INDEX(c->vm_start);
440 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
441 ptep = consistent_pte[idx] + off;
442 addr = c->vm_start;
443 do {
444 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000445
446 ptep++;
447 addr += PAGE_SIZE;
448 off++;
449 if (off >= PTRS_PER_PTE) {
450 off = 0;
451 ptep = consistent_pte[++idx];
452 }
453
Russell Kingacaac252009-11-20 18:19:52 +0000454 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100455 pr_crit("%s: bad page in kernel page table\n",
456 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000457 } while (size -= PAGE_SIZE);
458
459 flush_tlb_kernel_range(c->vm_start, c->vm_end);
460
461 arm_vmregion_free(&consistent_head, c);
462}
463
Catalin Marinasab6494f2009-07-24 12:35:02 +0100464#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000465
Russell King45cd5292012-01-12 23:08:07 +0000466#define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page)
Russell King31ebf942009-11-19 21:12:17 +0000467#define __dma_free_remap(addr, size) do { } while (0)
468
469#endif /* CONFIG_MMU */
470
Catalin Marinasab6494f2009-07-24 12:35:02 +0100471static void *
472__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
Russell King45cd5292012-01-12 23:08:07 +0000473 pgprot_t prot, const void *caller)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100474{
Russell King04da5692009-11-19 15:54:45 +0000475 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000476 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100477
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100478 /*
479 * Following is a work-around (a.k.a. hack) to prevent pages
480 * with __GFP_COMP being passed to split_page() which cannot
481 * handle them. The real problem is that this flag probably
482 * should be 0 on ARM as it is not supported on this
483 * platform; see CONFIG_HUGETLBFS.
484 */
485 gfp &= ~(__GFP_COMP);
486
Marek Szyprowski553ac782012-02-29 14:45:28 +0100487 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000488 size = PAGE_ALIGN(size);
489
490 page = __dma_alloc_buffer(dev, size, gfp);
491 if (!page)
492 return NULL;
493
Russell King31ebf942009-11-19 21:12:17 +0000494 if (!arch_is_coherent())
Russell King45cd5292012-01-12 23:08:07 +0000495 addr = __dma_alloc_remap(page, size, gfp, prot, caller);
Russell King31ebf942009-11-19 21:12:17 +0000496 else
497 addr = page_address(page);
498
499 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000500 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell Kingd8e89b42011-09-22 10:32:25 +0100501 else
502 __dma_free_buffer(page, size);
Russell King31ebf942009-11-19 21:12:17 +0000503
504 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100505}
Russell King695ae0a2009-11-19 16:31:39 +0000506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
508 * Allocate DMA-coherent memory space and return both the kernel remapped
509 * virtual and bus address for that space.
510 */
511void *
Al Virof9e32142005-10-21 03:20:58 -0400512dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400514 void *memory;
515
516 if (dma_alloc_from_coherent(dev, size, handle, &memory))
517 return memory;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000520 pgprot_dmacoherent(pgprot_kernel),
521 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523EXPORT_SYMBOL(dma_alloc_coherent);
524
525/*
526 * Allocate a writecombining region, in much the same way as
527 * dma_alloc_coherent above.
528 */
529void *
Al Virof9e32142005-10-21 03:20:58 -0400530dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000533 pgprot_writecombine(pgprot_kernel),
534 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536EXPORT_SYMBOL(dma_alloc_writecombine);
537
538static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
539 void *cpu_addr, dma_addr_t dma_addr, size_t size)
540{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100541 int ret = -ENXIO;
542#ifdef CONFIG_MMU
Russell King13ccf3a2009-11-19 15:07:04 +0000543 unsigned long user_size, kern_size;
544 struct arm_vmregion *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Marek Szyprowski47142f02012-05-15 19:04:13 +0200546 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
547 return ret;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
550
Russell King13ccf3a2009-11-19 15:07:04 +0000551 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (c) {
553 unsigned long off = vma->vm_pgoff;
554
555 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
556
557 if (off < kern_size &&
558 user_size <= (kern_size - off)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ret = remap_pfn_range(vma, vma->vm_start,
560 page_to_pfn(c->vm_pages) + off,
561 user_size << PAGE_SHIFT,
562 vma->vm_page_prot);
563 }
564 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100565#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 return ret;
568}
569
570int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
571 void *cpu_addr, dma_addr_t dma_addr, size_t size)
572{
Russell King26a26d32009-11-20 21:06:43 +0000573 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
575}
576EXPORT_SYMBOL(dma_mmap_coherent);
577
578int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
579 void *cpu_addr, dma_addr_t dma_addr, size_t size)
580{
581 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
582 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
583}
584EXPORT_SYMBOL(dma_mmap_writecombine);
585
586/*
587 * free a page as defined by the above mapping.
Russell King5edf71a2005-11-25 15:52:51 +0000588 * Must not be called with IRQs disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
590void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
591{
Russell King5edf71a2005-11-25 15:52:51 +0000592 WARN_ON(irqs_disabled());
593
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400594 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
595 return;
596
Russell King3e82d012009-11-19 15:38:12 +0000597 size = PAGE_ALIGN(size);
598
Russell King695ae0a2009-11-19 16:31:39 +0000599 if (!arch_is_coherent())
600 __dma_free_remap(cpu_addr, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000601
Russell King9eedd962011-01-03 00:00:17 +0000602 __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604EXPORT_SYMBOL(dma_free_coherent);
605
Russell King65af1912009-11-24 17:53:33 +0000606static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000607 size_t size, enum dma_data_direction dir,
608 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000609{
610 /*
611 * A single sg entry may refer to multiple physically contiguous
612 * pages. But we still need to process highmem pages individually.
613 * If highmem is not configured then the bulk of this loop gets
614 * optimized out.
615 */
616 size_t left = size;
617 do {
618 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000619 void *vaddr;
620
621 if (PageHighMem(page)) {
622 if (len + offset > PAGE_SIZE) {
623 if (offset >= PAGE_SIZE) {
624 page += offset / PAGE_SIZE;
625 offset %= PAGE_SIZE;
626 }
627 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000628 }
Russell King93f1d622009-11-24 14:41:01 +0000629 vaddr = kmap_high_get(page);
630 if (vaddr) {
631 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000632 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000633 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100634 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500635 /* unmapped pages might still be cached */
636 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100637 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500638 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000639 }
640 } else {
641 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000642 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000643 }
Russell King65af1912009-11-24 17:53:33 +0000644 offset = 0;
645 page++;
646 left -= len;
647 } while (left);
648}
649
650void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
651 size_t size, enum dma_data_direction dir)
652{
Nicolas Pitre43377452009-03-12 22:52:09 -0400653 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400654
Russell Kinga9c91472009-11-26 16:19:58 +0000655 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400656
Russell King65af1912009-11-24 17:53:33 +0000657 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000658 if (dir == DMA_FROM_DEVICE) {
659 outer_inv_range(paddr, paddr + size);
660 } else {
661 outer_clean_range(paddr, paddr + size);
662 }
663 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400664}
Russell King4ea0d732009-11-24 16:27:17 +0000665
666void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
667 size_t size, enum dma_data_direction dir)
668{
Russell King2ffe2da2009-10-31 16:52:16 +0000669 unsigned long paddr = page_to_phys(page) + off;
670
671 /* FIXME: non-speculating: not required */
672 /* don't bother invalidating if DMA to device */
673 if (dir != DMA_TO_DEVICE)
674 outer_inv_range(paddr, paddr + size);
675
Russell Kinga9c91472009-11-26 16:19:58 +0000676 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100677
678 /*
679 * Mark the D-cache clean for this page to avoid extra flushing.
680 */
681 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
682 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000683}
Nicolas Pitre43377452009-03-12 22:52:09 -0400684
Russell Kingafd1a322008-09-25 16:30:57 +0100685/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100686 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100687 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
688 * @sg: list of buffers
689 * @nents: number of buffers to map
690 * @dir: DMA transfer direction
691 *
692 * Map a set of buffers described by scatterlist in streaming mode for DMA.
693 * This is the scatter-gather version of the dma_map_single interface.
694 * Here the scatter gather list elements are each tagged with the
695 * appropriate dma address and length. They are obtained via
696 * sg_dma_{address,length}.
697 *
698 * Device ownership issues as mentioned for dma_map_single are the same
699 * here.
700 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100701int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
702 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100703{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100704 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100705 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100706 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100707
708 for_each_sg(sg, s, nents, i) {
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100709 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
710 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100711 if (dma_mapping_error(dev, s->dma_address))
712 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100713 }
Russell Kingafd1a322008-09-25 16:30:57 +0100714 return nents;
Russell King01135d922008-09-25 21:05:02 +0100715
716 bad_mapping:
717 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100718 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100719 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100720}
Russell Kingafd1a322008-09-25 16:30:57 +0100721
722/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100723 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100724 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
725 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100726 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100727 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
728 *
729 * Unmap a set of streaming mode DMA translations. Again, CPU access
730 * rules concerning calls here are the same as for dma_unmap_single().
731 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100732void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
733 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100734{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100735 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100736 struct scatterlist *s;
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100737
Russell King01135d922008-09-25 21:05:02 +0100738 int i;
739
740 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100741 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100742}
Russell Kingafd1a322008-09-25 16:30:57 +0100743
744/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100745 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100746 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
747 * @sg: list of buffers
748 * @nents: number of buffers to map (returned from dma_map_sg)
749 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
750 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100751void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100752 int nents, enum dma_data_direction dir)
753{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100754 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100755 struct scatterlist *s;
756 int i;
757
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100758 for_each_sg(sg, s, nents, i)
759 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
760 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100761}
Russell Kingafd1a322008-09-25 16:30:57 +0100762
763/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100764 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100765 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
766 * @sg: list of buffers
767 * @nents: number of buffers to map (returned from dma_map_sg)
768 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
769 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100770void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100771 int nents, enum dma_data_direction dir)
772{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100773 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100774 struct scatterlist *s;
775 int i;
776
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100777 for_each_sg(sg, s, nents, i)
778 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
779 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100780}
Russell King24056f52011-01-03 11:29:28 +0000781
Russell King022ae532011-07-08 21:26:59 +0100782/*
783 * Return whether the given device DMA address mask can be supported
784 * properly. For example, if your device can only drive the low 24-bits
785 * during bus mastering, then you would pass 0x00ffffff as the mask
786 * to this function.
787 */
788int dma_supported(struct device *dev, u64 mask)
789{
790 if (mask < (u64)arm_dma_limit)
791 return 0;
792 return 1;
793}
794EXPORT_SYMBOL(dma_supported);
795
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100796static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +0100797{
798 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
799 return -EIO;
800
Russell King022ae532011-07-08 21:26:59 +0100801 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +0100802
803 return 0;
804}
Russell King022ae532011-07-08 21:26:59 +0100805
Russell King24056f52011-01-03 11:29:28 +0000806#define PREALLOC_DMA_DEBUG_ENTRIES 4096
807
808static int __init dma_debug_do_init(void)
809{
Russell King45cd5292012-01-12 23:08:07 +0000810#ifdef CONFIG_MMU
811 arm_vmregion_create_proc("dma-mappings", &consistent_head);
812#endif
Russell King24056f52011-01-03 11:29:28 +0000813 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
814 return 0;
815}
816fs_initcall(dma_debug_do_init);