blob: 2501866a904c68dc8cbbd4aec0502c19a87e9f02 [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 *
Marek Szyprowski15237e12012-02-10 19:55:20 +010043 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010044static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010045 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +010046static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010047 size_t, enum dma_data_direction);
48
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010049/**
50 * arm_dma_map_page - map a portion of a page for streaming DMA
51 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
52 * @page: page that buffer resides in
53 * @offset: offset into page for start of buffer
54 * @size: size of buffer to map
55 * @dir: DMA transfer direction
56 *
57 * Ensure that any data held in the cache is appropriately discarded
58 * or written back.
59 *
60 * The device owns this memory once this call has completed. The CPU
61 * can regain ownership by calling dma_unmap_page().
62 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010063static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010064 unsigned long offset, size_t size, enum dma_data_direction dir,
65 struct dma_attrs *attrs)
66{
Marek Szyprowski51fde3492012-02-10 19:55:20 +010067 if (!arch_is_coherent())
68 __dma_page_cpu_to_dev(page, offset, size, dir);
69 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010070}
71
72/**
73 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
74 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
75 * @handle: DMA address of buffer
76 * @size: size of buffer (same as passed to dma_map_page)
77 * @dir: DMA transfer direction (same as passed to dma_map_page)
78 *
79 * Unmap a page streaming mode DMA translation. The handle and size
80 * must match what was provided in the previous dma_map_page() call.
81 * All other usages are undefined.
82 *
83 * After this call, reads by the CPU to the buffer are guaranteed to see
84 * whatever the device wrote there.
85 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010086static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010087 size_t size, enum dma_data_direction dir,
88 struct dma_attrs *attrs)
89{
Marek Szyprowski51fde3492012-02-10 19:55:20 +010090 if (!arch_is_coherent())
91 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
92 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010093}
94
Marek Szyprowski51fde3492012-02-10 19:55:20 +010095static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010096 dma_addr_t handle, size_t size, enum dma_data_direction dir)
97{
98 unsigned int offset = handle & (PAGE_SIZE - 1);
99 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100100 if (!arch_is_coherent())
101 __dma_page_dev_to_cpu(page, offset, 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_device(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_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100111}
112
113static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
114
115struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200116 .alloc = arm_dma_alloc,
117 .free = arm_dma_free,
118 .mmap = arm_dma_mmap,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100119 .map_page = arm_dma_map_page,
120 .unmap_page = arm_dma_unmap_page,
121 .map_sg = arm_dma_map_sg,
122 .unmap_sg = arm_dma_unmap_sg,
123 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
124 .sync_single_for_device = arm_dma_sync_single_for_device,
125 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
126 .sync_sg_for_device = arm_dma_sync_sg_for_device,
127 .set_dma_mask = arm_dma_set_mask,
128};
129EXPORT_SYMBOL(arm_dma_ops);
130
Catalin Marinasab6494f2009-07-24 12:35:02 +0100131static u64 get_coherent_dma_mask(struct device *dev)
132{
Russell King022ae532011-07-08 21:26:59 +0100133 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Catalin Marinasab6494f2009-07-24 12:35:02 +0100135 if (dev) {
136 mask = dev->coherent_dma_mask;
137
138 /*
139 * Sanity check the DMA mask - it must be non-zero, and
140 * must be able to be satisfied by a DMA allocation.
141 */
142 if (mask == 0) {
143 dev_warn(dev, "coherent DMA mask is unset\n");
144 return 0;
145 }
146
Russell King022ae532011-07-08 21:26:59 +0100147 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100148 dev_warn(dev, "coherent DMA mask %#llx is smaller "
149 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100150 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100151 return 0;
152 }
153 }
154
155 return mask;
156}
157
Russell King7a9a32a2009-11-19 15:31:07 +0000158/*
159 * Allocate a DMA buffer for 'dev' of size 'size' using the
160 * specified gfp mask. Note that 'size' must be page aligned.
161 */
162static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
163{
164 unsigned long order = get_order(size);
165 struct page *page, *p, *e;
166 void *ptr;
167 u64 mask = get_coherent_dma_mask(dev);
168
169#ifdef CONFIG_DMA_API_DEBUG
170 u64 limit = (mask + 1) & ~mask;
171 if (limit && size >= limit) {
172 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
173 size, mask);
174 return NULL;
175 }
176#endif
177
178 if (!mask)
179 return NULL;
180
181 if (mask < 0xffffffffULL)
182 gfp |= GFP_DMA;
183
184 page = alloc_pages(gfp, order);
185 if (!page)
186 return NULL;
187
188 /*
189 * Now split the huge page and free the excess pages
190 */
191 split_page(page, order);
192 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
193 __free_page(p);
194
195 /*
196 * Ensure that the allocated pages are zeroed, and that any data
197 * lurking in the kernel direct-mapped region is invalidated.
198 */
199 ptr = page_address(page);
200 memset(ptr, 0, size);
201 dmac_flush_range(ptr, ptr + size);
202 outer_flush_range(__pa(ptr), __pa(ptr) + size);
203
204 return page;
205}
206
207/*
208 * Free a DMA buffer. 'size' must be page aligned.
209 */
210static void __dma_free_buffer(struct page *page, size_t size)
211{
212 struct page *e = page + (size >> PAGE_SHIFT);
213
214 while (page < e) {
215 __free_page(page);
216 page++;
217 }
218}
219
Catalin Marinasab6494f2009-07-24 12:35:02 +0100220#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100221
Jon Medhurst99d17172011-08-02 17:28:27 +0100222#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700223#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000226 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100228static pte_t **consistent_pte;
229
Jon Medhurst99d17172011-08-02 17:28:27 +0100230#define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
Jon Medhurst99d17172011-08-02 17:28:27 +0100231
232unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
233
234void __init init_consistent_dma_size(unsigned long size)
235{
236 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
237
238 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
239 BUG_ON(base < VMALLOC_END);
240
241 /* Grow region to accommodate specified size */
242 if (base < consistent_base)
243 consistent_base = base;
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Russell King13ccf3a2009-11-19 15:07:04 +0000246#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Russell King13ccf3a2009-11-19 15:07:04 +0000248static struct arm_vmregion_head consistent_head = {
249 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .vm_end = CONSISTENT_END,
252};
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#ifdef CONFIG_HUGETLB_PAGE
255#error ARM Coherent DMA allocator does not (yet) support huge TLB
256#endif
257
Russell King88c58f32009-11-19 16:46:02 +0000258/*
259 * Initialise the consistent memory allocation.
260 */
261static int __init consistent_init(void)
262{
263 int ret = 0;
264 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000265 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000266 pmd_t *pmd;
267 pte_t *pte;
268 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100269 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100270 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100271
272 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
273 if (!consistent_pte) {
274 pr_err("%s: no memory\n", __func__);
275 return -ENOMEM;
276 }
277
278 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
279 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000280
281 do {
282 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000283
284 pud = pud_alloc(&init_mm, pgd, base);
285 if (!pud) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100286 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000287 ret = -ENOMEM;
288 break;
289 }
290
291 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000292 if (!pmd) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100293 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000294 ret = -ENOMEM;
295 break;
296 }
297 WARN_ON(!pmd_none(*pmd));
298
299 pte = pte_alloc_kernel(pmd, base);
300 if (!pte) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100301 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000302 ret = -ENOMEM;
303 break;
304 }
305
306 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100307 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000308 } while (base < CONSISTENT_END);
309
310 return ret;
311}
312
313core_initcall(consistent_init);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static void *
Russell King45cd5292012-01-12 23:08:07 +0000316__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
317 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Russell King13ccf3a2009-11-19 15:07:04 +0000319 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100320 size_t align;
321 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Jon Medhurst99d17172011-08-02 17:28:27 +0100323 if (!consistent_pte) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100324 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000325 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000326 return NULL;
327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
Russell King5bc23d32010-07-25 08:57:02 +0100330 * Align the virtual region allocation - maximum alignment is
331 * a section size, minimum is a page size. This helps reduce
332 * fragmentation of the DMA space, and also prevents allocations
333 * smaller than a section from crossing a section boundary.
334 */
Russell Kingc947f692010-11-03 16:00:15 +0000335 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100336 if (bit > SECTION_SHIFT)
337 bit = SECTION_SHIFT;
338 align = 1 << bit;
339
340 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * Allocate a virtual address in the consistent mapping region.
342 */
Russell King5bc23d32010-07-25 08:57:02 +0100343 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000344 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000346 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000347 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
348 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Kevin Hilman37134cd2006-01-12 16:12:21 +0000350 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 c->vm_pages = page;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 do {
354 BUG_ON(!pte_none(*pte));
355
Russell Kingad1ae2f2006-12-13 14:34:43 +0000356 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 page++;
358 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000359 off++;
360 if (off >= PTRS_PER_PTE) {
361 off = 0;
362 pte = consistent_pte[++idx];
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } while (size -= PAGE_SIZE);
365
Russell King2be23c42010-09-08 16:27:56 +0100366 dsb();
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return (void *)c->vm_start;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return NULL;
371}
Russell King695ae0a2009-11-19 16:31:39 +0000372
373static void __dma_free_remap(void *cpu_addr, size_t size)
374{
375 struct arm_vmregion *c;
376 unsigned long addr;
377 pte_t *ptep;
378 int idx;
379 u32 off;
380
381 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
382 if (!c) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100383 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000384 __func__, cpu_addr);
385 dump_stack();
386 return;
387 }
388
389 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100390 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000391 __func__, c->vm_end - c->vm_start, size);
392 dump_stack();
393 size = c->vm_end - c->vm_start;
394 }
395
396 idx = CONSISTENT_PTE_INDEX(c->vm_start);
397 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
398 ptep = consistent_pte[idx] + off;
399 addr = c->vm_start;
400 do {
401 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000402
403 ptep++;
404 addr += PAGE_SIZE;
405 off++;
406 if (off >= PTRS_PER_PTE) {
407 off = 0;
408 ptep = consistent_pte[++idx];
409 }
410
Russell Kingacaac252009-11-20 18:19:52 +0000411 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowski6b6f7702012-02-28 10:19:14 +0100412 pr_crit("%s: bad page in kernel page table\n",
413 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000414 } while (size -= PAGE_SIZE);
415
416 flush_tlb_kernel_range(c->vm_start, c->vm_end);
417
418 arm_vmregion_free(&consistent_head, c);
419}
420
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200421static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
422{
423 prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
424 pgprot_writecombine(prot) :
425 pgprot_dmacoherent(prot);
426 return prot;
427}
428
Catalin Marinasab6494f2009-07-24 12:35:02 +0100429#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000430
Russell King45cd5292012-01-12 23:08:07 +0000431#define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page)
Russell King31ebf942009-11-19 21:12:17 +0000432#define __dma_free_remap(addr, size) do { } while (0)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200433#define __get_dma_pgprot(attrs, prot) __pgprot(0)
Russell King31ebf942009-11-19 21:12:17 +0000434
435#endif /* CONFIG_MMU */
436
Catalin Marinasab6494f2009-07-24 12:35:02 +0100437static void *
438__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
Russell King45cd5292012-01-12 23:08:07 +0000439 pgprot_t prot, const void *caller)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100440{
Russell King04da5692009-11-19 15:54:45 +0000441 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000442 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100443
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100444 /*
445 * Following is a work-around (a.k.a. hack) to prevent pages
446 * with __GFP_COMP being passed to split_page() which cannot
447 * handle them. The real problem is that this flag probably
448 * should be 0 on ARM as it is not supported on this
449 * platform; see CONFIG_HUGETLBFS.
450 */
451 gfp &= ~(__GFP_COMP);
452
Marek Szyprowski553ac782012-02-29 14:45:28 +0100453 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000454 size = PAGE_ALIGN(size);
455
456 page = __dma_alloc_buffer(dev, size, gfp);
457 if (!page)
458 return NULL;
459
Russell King31ebf942009-11-19 21:12:17 +0000460 if (!arch_is_coherent())
Russell King45cd5292012-01-12 23:08:07 +0000461 addr = __dma_alloc_remap(page, size, gfp, prot, caller);
Russell King31ebf942009-11-19 21:12:17 +0000462 else
463 addr = page_address(page);
464
465 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000466 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell Kingd8e89b42011-09-22 10:32:25 +0100467 else
468 __dma_free_buffer(page, size);
Russell King31ebf942009-11-19 21:12:17 +0000469
470 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100471}
Russell King695ae0a2009-11-19 16:31:39 +0000472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
474 * Allocate DMA-coherent memory space and return both the kernel remapped
475 * virtual and bus address for that space.
476 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200477void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
478 gfp_t gfp, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200480 pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400481 void *memory;
482
483 if (dma_alloc_from_coherent(dev, size, handle, &memory))
484 return memory;
485
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200486 return __dma_alloc(dev, size, handle, gfp, prot,
Russell King45cd5292012-01-12 23:08:07 +0000487 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/*
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200491 * Create userspace mapping for the DMA-coherent memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200493int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
494 void *cpu_addr, dma_addr_t dma_addr, size_t size,
495 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100497 int ret = -ENXIO;
498#ifdef CONFIG_MMU
Russell King13ccf3a2009-11-19 15:07:04 +0000499 unsigned long user_size, kern_size;
500 struct arm_vmregion *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200502 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
503
Marek Szyprowski47142f02012-05-15 19:04:13 +0200504 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
505 return ret;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
508
Russell King13ccf3a2009-11-19 15:07:04 +0000509 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (c) {
511 unsigned long off = vma->vm_pgoff;
512
513 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
514
515 if (off < kern_size &&
516 user_size <= (kern_size - off)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 ret = remap_pfn_range(vma, vma->vm_start,
518 page_to_pfn(c->vm_pages) + off,
519 user_size << PAGE_SHIFT,
520 vma->vm_page_prot);
521 }
522 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100523#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 return ret;
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/*
529 * free a page as defined by the above mapping.
Russell King5edf71a2005-11-25 15:52:51 +0000530 * Must not be called with IRQs disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200532void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
533 dma_addr_t handle, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Russell King5edf71a2005-11-25 15:52:51 +0000535 WARN_ON(irqs_disabled());
536
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400537 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
538 return;
539
Russell King3e82d012009-11-19 15:38:12 +0000540 size = PAGE_ALIGN(size);
541
Russell King695ae0a2009-11-19 16:31:39 +0000542 if (!arch_is_coherent())
543 __dma_free_remap(cpu_addr, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000544
Russell King9eedd962011-01-03 00:00:17 +0000545 __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Russell King65af1912009-11-24 17:53:33 +0000548static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000549 size_t size, enum dma_data_direction dir,
550 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000551{
552 /*
553 * A single sg entry may refer to multiple physically contiguous
554 * pages. But we still need to process highmem pages individually.
555 * If highmem is not configured then the bulk of this loop gets
556 * optimized out.
557 */
558 size_t left = size;
559 do {
560 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000561 void *vaddr;
562
563 if (PageHighMem(page)) {
564 if (len + offset > PAGE_SIZE) {
565 if (offset >= PAGE_SIZE) {
566 page += offset / PAGE_SIZE;
567 offset %= PAGE_SIZE;
568 }
569 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000570 }
Russell King93f1d622009-11-24 14:41:01 +0000571 vaddr = kmap_high_get(page);
572 if (vaddr) {
573 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000574 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000575 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100576 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500577 /* unmapped pages might still be cached */
578 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100579 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500580 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000581 }
582 } else {
583 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000584 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000585 }
Russell King65af1912009-11-24 17:53:33 +0000586 offset = 0;
587 page++;
588 left -= len;
589 } while (left);
590}
591
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100592/*
593 * Make an area consistent for devices.
594 * Note: Drivers should NOT use this function directly, as it will break
595 * platforms with CONFIG_DMABOUNCE.
596 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
597 */
598static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000599 size_t size, enum dma_data_direction dir)
600{
Nicolas Pitre43377452009-03-12 22:52:09 -0400601 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400602
Russell Kinga9c91472009-11-26 16:19:58 +0000603 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400604
Russell King65af1912009-11-24 17:53:33 +0000605 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000606 if (dir == DMA_FROM_DEVICE) {
607 outer_inv_range(paddr, paddr + size);
608 } else {
609 outer_clean_range(paddr, paddr + size);
610 }
611 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400612}
Russell King4ea0d732009-11-24 16:27:17 +0000613
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100614static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000615 size_t size, enum dma_data_direction dir)
616{
Russell King2ffe2da2009-10-31 16:52:16 +0000617 unsigned long paddr = page_to_phys(page) + off;
618
619 /* FIXME: non-speculating: not required */
620 /* don't bother invalidating if DMA to device */
621 if (dir != DMA_TO_DEVICE)
622 outer_inv_range(paddr, paddr + size);
623
Russell Kinga9c91472009-11-26 16:19:58 +0000624 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100625
626 /*
627 * Mark the D-cache clean for this page to avoid extra flushing.
628 */
629 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
630 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000631}
Nicolas Pitre43377452009-03-12 22:52:09 -0400632
Russell Kingafd1a322008-09-25 16:30:57 +0100633/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100634 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100635 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
636 * @sg: list of buffers
637 * @nents: number of buffers to map
638 * @dir: DMA transfer direction
639 *
640 * Map a set of buffers described by scatterlist in streaming mode for DMA.
641 * This is the scatter-gather version of the dma_map_single interface.
642 * Here the scatter gather list elements are each tagged with the
643 * appropriate dma address and length. They are obtained via
644 * sg_dma_{address,length}.
645 *
646 * Device ownership issues as mentioned for dma_map_single are the same
647 * here.
648 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100649int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
650 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100651{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100652 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100653 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100654 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100655
656 for_each_sg(sg, s, nents, i) {
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100657 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
658 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100659 if (dma_mapping_error(dev, s->dma_address))
660 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100661 }
Russell Kingafd1a322008-09-25 16:30:57 +0100662 return nents;
Russell King01135d922008-09-25 21:05:02 +0100663
664 bad_mapping:
665 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100666 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100667 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100668}
Russell Kingafd1a322008-09-25 16:30:57 +0100669
670/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100671 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100672 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
673 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100674 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100675 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
676 *
677 * Unmap a set of streaming mode DMA translations. Again, CPU access
678 * rules concerning calls here are the same as for dma_unmap_single().
679 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100680void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
681 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100682{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100683 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100684 struct scatterlist *s;
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100685
Russell King01135d922008-09-25 21:05:02 +0100686 int i;
687
688 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100689 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100690}
Russell Kingafd1a322008-09-25 16:30:57 +0100691
692/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100693 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100694 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
695 * @sg: list of buffers
696 * @nents: number of buffers to map (returned from dma_map_sg)
697 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
698 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100699void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100700 int nents, enum dma_data_direction dir)
701{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100702 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100703 struct scatterlist *s;
704 int i;
705
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100706 for_each_sg(sg, s, nents, i)
707 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
708 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100709}
Russell Kingafd1a322008-09-25 16:30:57 +0100710
711/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100712 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100713 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
714 * @sg: list of buffers
715 * @nents: number of buffers to map (returned from dma_map_sg)
716 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
717 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100718void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100719 int nents, enum dma_data_direction dir)
720{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100721 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100722 struct scatterlist *s;
723 int i;
724
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100725 for_each_sg(sg, s, nents, i)
726 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
727 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100728}
Russell King24056f52011-01-03 11:29:28 +0000729
Russell King022ae532011-07-08 21:26:59 +0100730/*
731 * Return whether the given device DMA address mask can be supported
732 * properly. For example, if your device can only drive the low 24-bits
733 * during bus mastering, then you would pass 0x00ffffff as the mask
734 * to this function.
735 */
736int dma_supported(struct device *dev, u64 mask)
737{
738 if (mask < (u64)arm_dma_limit)
739 return 0;
740 return 1;
741}
742EXPORT_SYMBOL(dma_supported);
743
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100744static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +0100745{
746 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
747 return -EIO;
748
Russell King022ae532011-07-08 21:26:59 +0100749 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +0100750
751 return 0;
752}
Russell King022ae532011-07-08 21:26:59 +0100753
Russell King24056f52011-01-03 11:29:28 +0000754#define PREALLOC_DMA_DEBUG_ENTRIES 4096
755
756static int __init dma_debug_do_init(void)
757{
Russell King45cd5292012-01-12 23:08:07 +0000758#ifdef CONFIG_MMU
759 arm_vmregion_create_proc("dma-mappings", &consistent_head);
760#endif
Russell King24056f52011-01-03 11:29:28 +0000761 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
762 return 0;
763}
764fs_initcall(dma_debug_do_init);