blob: 153f5559406a1e44bd5525277953d2e468244c9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0ddbccd2008-09-25 15:59:19 +01002 * linux/arch/arm/mm/dma-mapping.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000-2004 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * DMA uncached mapping support.
11 */
12#include <linux/module.h>
13#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/list.h>
17#include <linux/init.h>
18#include <linux/device.h>
19#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010020#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050021#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010022#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010025#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040026#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/tlbflush.h>
Kevin Hilman37134cd2006-01-12 16:12:21 +000029#include <asm/sizes.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010030#include <asm/mach/arch.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010031#include <asm/mach/map.h>
32#include <asm/system_info.h>
33#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Russell King022ae532011-07-08 21:26:59 +010035#include "mm.h"
36
Catalin Marinasab6494f2009-07-24 12:35:02 +010037static u64 get_coherent_dma_mask(struct device *dev)
38{
Russell King022ae532011-07-08 21:26:59 +010039 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Catalin Marinasab6494f2009-07-24 12:35:02 +010041 if (dev) {
42 mask = dev->coherent_dma_mask;
43
44 /*
45 * Sanity check the DMA mask - it must be non-zero, and
46 * must be able to be satisfied by a DMA allocation.
47 */
48 if (mask == 0) {
49 dev_warn(dev, "coherent DMA mask is unset\n");
50 return 0;
51 }
52
Russell King022ae532011-07-08 21:26:59 +010053 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +010054 dev_warn(dev, "coherent DMA mask %#llx is smaller "
55 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +010056 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +010057 return 0;
58 }
59 }
60
61 return mask;
62}
63
Marek Szyprowskic7909502011-12-29 13:09:51 +010064static void __dma_clear_buffer(struct page *page, size_t size)
65{
66 void *ptr;
67 /*
68 * Ensure that the allocated pages are zeroed, and that any data
69 * lurking in the kernel direct-mapped region is invalidated.
70 */
71 ptr = page_address(page);
72 memset(ptr, 0, size);
73 dmac_flush_range(ptr, ptr + size);
74 outer_flush_range(__pa(ptr), __pa(ptr) + size);
75}
76
Russell King7a9a32a2009-11-19 15:31:07 +000077/*
78 * Allocate a DMA buffer for 'dev' of size 'size' using the
79 * specified gfp mask. Note that 'size' must be page aligned.
80 */
81static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
82{
83 unsigned long order = get_order(size);
84 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +000085
86 page = alloc_pages(gfp, order);
87 if (!page)
88 return NULL;
89
90 /*
91 * Now split the huge page and free the excess pages
92 */
93 split_page(page, order);
94 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
95 __free_page(p);
96
Marek Szyprowskic7909502011-12-29 13:09:51 +010097 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +000098
99 return page;
100}
101
102/*
103 * Free a DMA buffer. 'size' must be page aligned.
104 */
105static void __dma_free_buffer(struct page *page, size_t size)
106{
107 struct page *e = page + (size >> PAGE_SHIFT);
108
109 while (page < e) {
110 __free_page(page);
111 page++;
112 }
113}
114
Catalin Marinasab6494f2009-07-24 12:35:02 +0100115#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100116
Jon Medhurst99d17172011-08-02 17:28:27 +0100117#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700118#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000121 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100123static pte_t **consistent_pte;
124
Jon Medhurst99d17172011-08-02 17:28:27 +0100125#define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M
Jon Medhurst99d17172011-08-02 17:28:27 +0100126
127unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
128
129void __init init_consistent_dma_size(unsigned long size)
130{
131 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
132
133 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
134 BUG_ON(base < VMALLOC_END);
135
136 /* Grow region to accommodate specified size */
137 if (base < consistent_base)
138 consistent_base = base;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Russell King13ccf3a2009-11-19 15:07:04 +0000141#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Russell King13ccf3a2009-11-19 15:07:04 +0000143static struct arm_vmregion_head consistent_head = {
144 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .vm_end = CONSISTENT_END,
147};
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#ifdef CONFIG_HUGETLB_PAGE
150#error ARM Coherent DMA allocator does not (yet) support huge TLB
151#endif
152
Russell King88c58f32009-11-19 16:46:02 +0000153/*
154 * Initialise the consistent memory allocation.
155 */
156static int __init consistent_init(void)
157{
158 int ret = 0;
159 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000160 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000161 pmd_t *pmd;
162 pte_t *pte;
163 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100164 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100165 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100166
Marek Szyprowskic7909502011-12-29 13:09:51 +0100167 if (cpu_architecture() >= CPU_ARCH_ARMv6)
168 return 0;
169
Jon Medhurst99d17172011-08-02 17:28:27 +0100170 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
171 if (!consistent_pte) {
172 pr_err("%s: no memory\n", __func__);
173 return -ENOMEM;
174 }
175
176 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
177 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000178
179 do {
180 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000181
182 pud = pud_alloc(&init_mm, pgd, base);
183 if (!pud) {
184 printk(KERN_ERR "%s: no pud tables\n", __func__);
185 ret = -ENOMEM;
186 break;
187 }
188
189 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000190 if (!pmd) {
191 printk(KERN_ERR "%s: no pmd tables\n", __func__);
192 ret = -ENOMEM;
193 break;
194 }
195 WARN_ON(!pmd_none(*pmd));
196
197 pte = pte_alloc_kernel(pmd, base);
198 if (!pte) {
199 printk(KERN_ERR "%s: no pte tables\n", __func__);
200 ret = -ENOMEM;
201 break;
202 }
203
204 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100205 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000206 } while (base < CONSISTENT_END);
207
208 return ret;
209}
Russell King88c58f32009-11-19 16:46:02 +0000210core_initcall(consistent_init);
211
Marek Szyprowskic7909502011-12-29 13:09:51 +0100212static void *__alloc_from_contiguous(struct device *dev, size_t size,
213 pgprot_t prot, struct page **ret_page);
214
215static struct arm_vmregion_head coherent_head = {
216 .vm_lock = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock),
217 .vm_list = LIST_HEAD_INIT(coherent_head.vm_list),
218};
219
220size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8;
221
222static int __init early_coherent_pool(char *p)
223{
224 coherent_pool_size = memparse(p, &p);
225 return 0;
226}
227early_param("coherent_pool", early_coherent_pool);
228
229/*
230 * Initialise the coherent pool for atomic allocations.
231 */
232static int __init coherent_init(void)
233{
234 pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
235 size_t size = coherent_pool_size;
236 struct page *page;
237 void *ptr;
238
239 if (cpu_architecture() < CPU_ARCH_ARMv6)
240 return 0;
241
242 ptr = __alloc_from_contiguous(NULL, size, prot, &page);
243 if (ptr) {
244 coherent_head.vm_start = (unsigned long) ptr;
245 coherent_head.vm_end = (unsigned long) ptr + size;
246 printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n",
247 (unsigned)size / 1024);
248 return 0;
249 }
250 printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
251 (unsigned)size / 1024);
252 return -ENOMEM;
253}
254/*
255 * CMA is activated by core_initcall, so we must be called after it.
256 */
257postcore_initcall(coherent_init);
258
259struct dma_contig_early_reserve {
260 phys_addr_t base;
261 unsigned long size;
262};
263
264static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
265
266static int dma_mmu_remap_num __initdata;
267
268void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
269{
270 dma_mmu_remap[dma_mmu_remap_num].base = base;
271 dma_mmu_remap[dma_mmu_remap_num].size = size;
272 dma_mmu_remap_num++;
273}
274
275void __init dma_contiguous_remap(void)
276{
277 int i;
278 for (i = 0; i < dma_mmu_remap_num; i++) {
279 phys_addr_t start = dma_mmu_remap[i].base;
280 phys_addr_t end = start + dma_mmu_remap[i].size;
281 struct map_desc map;
282 unsigned long addr;
283
284 if (end > arm_lowmem_limit)
285 end = arm_lowmem_limit;
286 if (start >= end)
287 return;
288
289 map.pfn = __phys_to_pfn(start);
290 map.virtual = __phys_to_virt(start);
291 map.length = end - start;
292 map.type = MT_MEMORY_DMA_READY;
293
294 /*
295 * Clear previous low-memory mapping
296 */
297 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400298 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100299 pmd_clear(pmd_off_k(addr));
300
301 iotable_init(&map, 1);
302 }
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static void *
Russell King45cd5292012-01-12 23:08:07 +0000306__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
307 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Russell King13ccf3a2009-11-19 15:07:04 +0000309 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100310 size_t align;
311 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jon Medhurst99d17172011-08-02 17:28:27 +0100313 if (!consistent_pte) {
Russell Kingebd7a842009-11-19 20:58:31 +0000314 printk(KERN_ERR "%s: not initialised\n", __func__);
315 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000316 return NULL;
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
Russell King5bc23d32010-07-25 08:57:02 +0100320 * Align the virtual region allocation - maximum alignment is
321 * a section size, minimum is a page size. This helps reduce
322 * fragmentation of the DMA space, and also prevents allocations
323 * smaller than a section from crossing a section boundary.
324 */
Russell Kingc947f692010-11-03 16:00:15 +0000325 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100326 if (bit > SECTION_SHIFT)
327 bit = SECTION_SHIFT;
328 align = 1 << bit;
329
330 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * Allocate a virtual address in the consistent mapping region.
332 */
Russell King5bc23d32010-07-25 08:57:02 +0100333 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000334 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000336 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000337 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
338 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Kevin Hilman37134cd2006-01-12 16:12:21 +0000340 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 c->vm_pages = page;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 do {
344 BUG_ON(!pte_none(*pte));
345
Russell Kingad1ae2f2006-12-13 14:34:43 +0000346 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 page++;
348 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000349 off++;
350 if (off >= PTRS_PER_PTE) {
351 off = 0;
352 pte = consistent_pte[++idx];
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 } while (size -= PAGE_SIZE);
355
Russell King2be23c42010-09-08 16:27:56 +0100356 dsb();
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return (void *)c->vm_start;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return NULL;
361}
Russell King695ae0a2009-11-19 16:31:39 +0000362
363static void __dma_free_remap(void *cpu_addr, size_t size)
364{
365 struct arm_vmregion *c;
366 unsigned long addr;
367 pte_t *ptep;
368 int idx;
369 u32 off;
370
371 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
372 if (!c) {
373 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
374 __func__, cpu_addr);
375 dump_stack();
376 return;
377 }
378
379 if ((c->vm_end - c->vm_start) != size) {
380 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
381 __func__, c->vm_end - c->vm_start, size);
382 dump_stack();
383 size = c->vm_end - c->vm_start;
384 }
385
386 idx = CONSISTENT_PTE_INDEX(c->vm_start);
387 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
388 ptep = consistent_pte[idx] + off;
389 addr = c->vm_start;
390 do {
391 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000392
393 ptep++;
394 addr += PAGE_SIZE;
395 off++;
396 if (off >= PTRS_PER_PTE) {
397 off = 0;
398 ptep = consistent_pte[++idx];
399 }
400
Russell Kingacaac252009-11-20 18:19:52 +0000401 if (pte_none(pte) || !pte_present(pte))
402 printk(KERN_CRIT "%s: bad page in kernel page table\n",
403 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000404 } while (size -= PAGE_SIZE);
405
406 flush_tlb_kernel_range(c->vm_start, c->vm_end);
407
408 arm_vmregion_free(&consistent_head, c);
409}
410
Marek Szyprowskic7909502011-12-29 13:09:51 +0100411static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
412 void *data)
413{
414 struct page *page = virt_to_page(addr);
415 pgprot_t prot = *(pgprot_t *)data;
416
417 set_pte_ext(pte, mk_pte(page, prot), 0);
418 return 0;
419}
420
421static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
422{
423 unsigned long start = (unsigned long) page_address(page);
424 unsigned end = start + size;
425
426 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
427 dsb();
428 flush_tlb_kernel_range(start, end);
429}
430
431static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
432 pgprot_t prot, struct page **ret_page,
433 const void *caller)
434{
435 struct page *page;
436 void *ptr;
437 page = __dma_alloc_buffer(dev, size, gfp);
438 if (!page)
439 return NULL;
440
441 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
442 if (!ptr) {
443 __dma_free_buffer(page, size);
444 return NULL;
445 }
446
447 *ret_page = page;
448 return ptr;
449}
450
451static void *__alloc_from_pool(struct device *dev, size_t size,
452 struct page **ret_page, const void *caller)
453{
454 struct arm_vmregion *c;
455 size_t align;
456
457 if (!coherent_head.vm_start) {
458 printk(KERN_ERR "%s: coherent pool not initialised!\n",
459 __func__);
460 dump_stack();
461 return NULL;
462 }
463
464 /*
465 * Align the region allocation - allocations from pool are rather
466 * small, so align them to their order in pages, minimum is a page
467 * size. This helps reduce fragmentation of the DMA space.
468 */
469 align = PAGE_SIZE << get_order(size);
470 c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller);
471 if (c) {
472 void *ptr = (void *)c->vm_start;
473 struct page *page = virt_to_page(ptr);
474 *ret_page = page;
475 return ptr;
476 }
477 return NULL;
478}
479
480static int __free_from_pool(void *cpu_addr, size_t size)
481{
482 unsigned long start = (unsigned long)cpu_addr;
483 unsigned long end = start + size;
484 struct arm_vmregion *c;
485
486 if (start < coherent_head.vm_start || end > coherent_head.vm_end)
487 return 0;
488
489 c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start);
490
491 if ((c->vm_end - c->vm_start) != size) {
492 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
493 __func__, c->vm_end - c->vm_start, size);
494 dump_stack();
495 size = c->vm_end - c->vm_start;
496 }
497
498 arm_vmregion_free(&coherent_head, c);
499 return 1;
500}
501
502static void *__alloc_from_contiguous(struct device *dev, size_t size,
503 pgprot_t prot, struct page **ret_page)
504{
505 unsigned long order = get_order(size);
506 size_t count = size >> PAGE_SHIFT;
507 struct page *page;
508
509 page = dma_alloc_from_contiguous(dev, count, order);
510 if (!page)
511 return NULL;
512
513 __dma_clear_buffer(page, size);
514 __dma_remap(page, size, prot);
515
516 *ret_page = page;
517 return page_address(page);
518}
519
520static void __free_from_contiguous(struct device *dev, struct page *page,
521 size_t size)
522{
523 __dma_remap(page, size, pgprot_kernel);
524 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
525}
526
527#define nommu() 0
528
Catalin Marinasab6494f2009-07-24 12:35:02 +0100529#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000530
Marek Szyprowskic7909502011-12-29 13:09:51 +0100531#define nommu() 1
532
533#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
534#define __alloc_from_pool(dev, size, ret_page, c) NULL
535#define __alloc_from_contiguous(dev, size, prot, ret) NULL
536#define __free_from_pool(cpu_addr, size) 0
537#define __free_from_contiguous(dev, page, size) do { } while (0)
538#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000539
540#endif /* CONFIG_MMU */
541
Marek Szyprowskic7909502011-12-29 13:09:51 +0100542static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
543 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100544{
Russell King04da5692009-11-19 15:54:45 +0000545 struct page *page;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100546 page = __dma_alloc_buffer(dev, size, gfp);
547 if (!page)
548 return NULL;
549
550 *ret_page = page;
551 return page_address(page);
552}
553
554
555
556static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
557 gfp_t gfp, pgprot_t prot, const void *caller)
558{
559 u64 mask = get_coherent_dma_mask(dev);
560 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000561 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100562
Marek Szyprowskic7909502011-12-29 13:09:51 +0100563#ifdef CONFIG_DMA_API_DEBUG
564 u64 limit = (mask + 1) & ~mask;
565 if (limit && size >= limit) {
566 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
567 size, mask);
568 return NULL;
569 }
570#endif
571
572 if (!mask)
573 return NULL;
574
575 if (mask < 0xffffffffULL)
576 gfp |= GFP_DMA;
577
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100578 /*
579 * Following is a work-around (a.k.a. hack) to prevent pages
580 * with __GFP_COMP being passed to split_page() which cannot
581 * handle them. The real problem is that this flag probably
582 * should be 0 on ARM as it is not supported on this
583 * platform; see CONFIG_HUGETLBFS.
584 */
585 gfp &= ~(__GFP_COMP);
586
Catalin Marinasab6494f2009-07-24 12:35:02 +0100587 *handle = ~0;
Russell King04da5692009-11-19 15:54:45 +0000588 size = PAGE_ALIGN(size);
589
Marek Szyprowskic7909502011-12-29 13:09:51 +0100590 if (arch_is_coherent() || nommu())
591 addr = __alloc_simple_buffer(dev, size, gfp, &page);
592 else if (cpu_architecture() < CPU_ARCH_ARMv6)
593 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
594 else if (gfp & GFP_ATOMIC)
595 addr = __alloc_from_pool(dev, size, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000596 else
Marek Szyprowskic7909502011-12-29 13:09:51 +0100597 addr = __alloc_from_contiguous(dev, size, prot, &page);
Russell King31ebf942009-11-19 21:12:17 +0000598
599 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000600 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000601
602 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100603}
Russell King695ae0a2009-11-19 16:31:39 +0000604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * Allocate DMA-coherent memory space and return both the kernel remapped
607 * virtual and bus address for that space.
608 */
Marek Szyprowskic7909502011-12-29 13:09:51 +0100609void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle,
610 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400612 void *memory;
613
614 if (dma_alloc_from_coherent(dev, size, handle, &memory))
615 return memory;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000618 pgprot_dmacoherent(pgprot_kernel),
619 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621EXPORT_SYMBOL(dma_alloc_coherent);
622
623/*
624 * Allocate a writecombining region, in much the same way as
625 * dma_alloc_coherent above.
626 */
627void *
Al Virof9e32142005-10-21 03:20:58 -0400628dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000631 pgprot_writecombine(pgprot_kernel),
632 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634EXPORT_SYMBOL(dma_alloc_writecombine);
635
636static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
637 void *cpu_addr, dma_addr_t dma_addr, size_t size)
638{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100639 int ret = -ENXIO;
640#ifdef CONFIG_MMU
Marek Szyprowskic7909502011-12-29 13:09:51 +0100641 unsigned long pfn = dma_to_pfn(dev, dma_addr);
642 ret = remap_pfn_range(vma, vma->vm_start,
643 pfn + vma->vm_pgoff,
644 vma->vm_end - vma->vm_start,
645 vma->vm_page_prot);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100646#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 return ret;
649}
650
651int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
652 void *cpu_addr, dma_addr_t dma_addr, size_t size)
653{
Russell King26a26d32009-11-20 21:06:43 +0000654 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
656}
657EXPORT_SYMBOL(dma_mmap_coherent);
658
659int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
660 void *cpu_addr, dma_addr_t dma_addr, size_t size)
661{
662 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
663 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
664}
665EXPORT_SYMBOL(dma_mmap_writecombine);
666
Marek Szyprowskic7909502011-12-29 13:09:51 +0100667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100669 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
671void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
672{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100673 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000674
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400675 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
676 return;
677
Russell King3e82d012009-11-19 15:38:12 +0000678 size = PAGE_ALIGN(size);
679
Marek Szyprowskic7909502011-12-29 13:09:51 +0100680 if (arch_is_coherent() || nommu()) {
681 __dma_free_buffer(page, size);
682 } else if (cpu_architecture() < CPU_ARCH_ARMv6) {
Russell King695ae0a2009-11-19 16:31:39 +0000683 __dma_free_remap(cpu_addr, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100684 __dma_free_buffer(page, size);
685 } else {
686 if (__free_from_pool(cpu_addr, size))
687 return;
688 /*
689 * Non-atomic allocations cannot be freed with IRQs disabled
690 */
691 WARN_ON(irqs_disabled());
692 __free_from_contiguous(dev, page, size);
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695EXPORT_SYMBOL(dma_free_coherent);
696
697/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * Make an area consistent for devices.
Dan Williams105ef9a2006-11-21 22:57:23 +0100699 * Note: Drivers should NOT use this function directly, as it will break
700 * platforms with CONFIG_DMABOUNCE.
701 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Russell King4ea0d732009-11-24 16:27:17 +0000703void ___dma_single_cpu_to_dev(const void *kaddr, size_t size,
704 enum dma_data_direction dir)
705{
Russell King2ffe2da2009-10-31 16:52:16 +0000706 unsigned long paddr;
707
Russell Kinga9c91472009-11-26 16:19:58 +0000708 BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1));
709
710 dmac_map_area(kaddr, size, dir);
Russell King2ffe2da2009-10-31 16:52:16 +0000711
712 paddr = __pa(kaddr);
713 if (dir == DMA_FROM_DEVICE) {
714 outer_inv_range(paddr, paddr + size);
715 } else {
716 outer_clean_range(paddr, paddr + size);
717 }
718 /* FIXME: non-speculating: flush on bidirectional mappings? */
Russell King4ea0d732009-11-24 16:27:17 +0000719}
720EXPORT_SYMBOL(___dma_single_cpu_to_dev);
721
722void ___dma_single_dev_to_cpu(const void *kaddr, size_t size,
723 enum dma_data_direction dir)
724{
Russell Kinga9c91472009-11-26 16:19:58 +0000725 BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1));
726
Russell King2ffe2da2009-10-31 16:52:16 +0000727 /* FIXME: non-speculating: not required */
728 /* don't bother invalidating if DMA to device */
729 if (dir != DMA_TO_DEVICE) {
730 unsigned long paddr = __pa(kaddr);
731 outer_inv_range(paddr, paddr + size);
732 }
733
Russell Kinga9c91472009-11-26 16:19:58 +0000734 dmac_unmap_area(kaddr, size, dir);
Russell King4ea0d732009-11-24 16:27:17 +0000735}
736EXPORT_SYMBOL(___dma_single_dev_to_cpu);
Russell Kingafd1a322008-09-25 16:30:57 +0100737
Russell King65af1912009-11-24 17:53:33 +0000738static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000739 size_t size, enum dma_data_direction dir,
740 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000741{
742 /*
743 * A single sg entry may refer to multiple physically contiguous
744 * pages. But we still need to process highmem pages individually.
745 * If highmem is not configured then the bulk of this loop gets
746 * optimized out.
747 */
748 size_t left = size;
749 do {
750 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000751 void *vaddr;
752
753 if (PageHighMem(page)) {
754 if (len + offset > PAGE_SIZE) {
755 if (offset >= PAGE_SIZE) {
756 page += offset / PAGE_SIZE;
757 offset %= PAGE_SIZE;
758 }
759 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000760 }
Russell King93f1d622009-11-24 14:41:01 +0000761 vaddr = kmap_high_get(page);
762 if (vaddr) {
763 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000764 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000765 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100766 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500767 /* unmapped pages might still be cached */
768 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100769 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500770 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000771 }
772 } else {
773 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000774 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000775 }
Russell King65af1912009-11-24 17:53:33 +0000776 offset = 0;
777 page++;
778 left -= len;
779 } while (left);
780}
781
782void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
783 size_t size, enum dma_data_direction dir)
784{
Nicolas Pitre43377452009-03-12 22:52:09 -0400785 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400786
Russell Kinga9c91472009-11-26 16:19:58 +0000787 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400788
Russell King65af1912009-11-24 17:53:33 +0000789 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000790 if (dir == DMA_FROM_DEVICE) {
791 outer_inv_range(paddr, paddr + size);
792 } else {
793 outer_clean_range(paddr, paddr + size);
794 }
795 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400796}
Russell King4ea0d732009-11-24 16:27:17 +0000797EXPORT_SYMBOL(___dma_page_cpu_to_dev);
798
799void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
800 size_t size, enum dma_data_direction dir)
801{
Russell King2ffe2da2009-10-31 16:52:16 +0000802 unsigned long paddr = page_to_phys(page) + off;
803
804 /* FIXME: non-speculating: not required */
805 /* don't bother invalidating if DMA to device */
806 if (dir != DMA_TO_DEVICE)
807 outer_inv_range(paddr, paddr + size);
808
Russell Kinga9c91472009-11-26 16:19:58 +0000809 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100810
811 /*
812 * Mark the D-cache clean for this page to avoid extra flushing.
813 */
814 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
815 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000816}
817EXPORT_SYMBOL(___dma_page_dev_to_cpu);
Nicolas Pitre43377452009-03-12 22:52:09 -0400818
Russell Kingafd1a322008-09-25 16:30:57 +0100819/**
820 * dma_map_sg - map a set of SG buffers for streaming mode DMA
821 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
822 * @sg: list of buffers
823 * @nents: number of buffers to map
824 * @dir: DMA transfer direction
825 *
826 * Map a set of buffers described by scatterlist in streaming mode for DMA.
827 * This is the scatter-gather version of the dma_map_single interface.
828 * Here the scatter gather list elements are each tagged with the
829 * appropriate dma address and length. They are obtained via
830 * sg_dma_{address,length}.
831 *
832 * Device ownership issues as mentioned for dma_map_single are the same
833 * here.
834 */
835int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
836 enum dma_data_direction dir)
837{
838 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100839 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100840
Russell King24056f52011-01-03 11:29:28 +0000841 BUG_ON(!valid_dma_direction(dir));
842
Russell Kingafd1a322008-09-25 16:30:57 +0100843 for_each_sg(sg, s, nents, i) {
Russell King24056f52011-01-03 11:29:28 +0000844 s->dma_address = __dma_map_page(dev, sg_page(s), s->offset,
Russell King01135d922008-09-25 21:05:02 +0100845 s->length, dir);
846 if (dma_mapping_error(dev, s->dma_address))
847 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100848 }
Russell King24056f52011-01-03 11:29:28 +0000849 debug_dma_map_sg(dev, sg, nents, nents, dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100850 return nents;
Russell King01135d922008-09-25 21:05:02 +0100851
852 bad_mapping:
853 for_each_sg(sg, s, i, j)
Russell King24056f52011-01-03 11:29:28 +0000854 __dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
Russell King01135d922008-09-25 21:05:02 +0100855 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100856}
857EXPORT_SYMBOL(dma_map_sg);
858
859/**
860 * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
861 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
862 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100863 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100864 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
865 *
866 * Unmap a set of streaming mode DMA translations. Again, CPU access
867 * rules concerning calls here are the same as for dma_unmap_single().
868 */
869void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
870 enum dma_data_direction dir)
871{
Russell King01135d922008-09-25 21:05:02 +0100872 struct scatterlist *s;
873 int i;
874
Russell King24056f52011-01-03 11:29:28 +0000875 debug_dma_unmap_sg(dev, sg, nents, dir);
876
Russell King01135d922008-09-25 21:05:02 +0100877 for_each_sg(sg, s, nents, i)
Russell King24056f52011-01-03 11:29:28 +0000878 __dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100879}
880EXPORT_SYMBOL(dma_unmap_sg);
881
882/**
883 * dma_sync_sg_for_cpu
884 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
885 * @sg: list of buffers
886 * @nents: number of buffers to map (returned from dma_map_sg)
887 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
888 */
889void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
890 int nents, enum dma_data_direction dir)
891{
892 struct scatterlist *s;
893 int i;
894
895 for_each_sg(sg, s, nents, i) {
Russell King18eabe22009-10-31 16:52:16 +0000896 if (!dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
897 sg_dma_len(s), dir))
898 continue;
899
900 __dma_page_dev_to_cpu(sg_page(s), s->offset,
901 s->length, dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100902 }
Russell King24056f52011-01-03 11:29:28 +0000903
904 debug_dma_sync_sg_for_cpu(dev, sg, nents, dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100905}
906EXPORT_SYMBOL(dma_sync_sg_for_cpu);
907
908/**
909 * dma_sync_sg_for_device
910 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
911 * @sg: list of buffers
912 * @nents: number of buffers to map (returned from dma_map_sg)
913 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
914 */
915void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
916 int nents, enum dma_data_direction dir)
917{
918 struct scatterlist *s;
919 int i;
920
921 for_each_sg(sg, s, nents, i) {
Russell King2638b4d2008-09-25 21:38:41 +0100922 if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
923 sg_dma_len(s), dir))
924 continue;
925
Russell King18eabe22009-10-31 16:52:16 +0000926 __dma_page_cpu_to_dev(sg_page(s), s->offset,
927 s->length, dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100928 }
Russell King24056f52011-01-03 11:29:28 +0000929
930 debug_dma_sync_sg_for_device(dev, sg, nents, dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100931}
932EXPORT_SYMBOL(dma_sync_sg_for_device);
Russell King24056f52011-01-03 11:29:28 +0000933
Russell King022ae532011-07-08 21:26:59 +0100934/*
935 * Return whether the given device DMA address mask can be supported
936 * properly. For example, if your device can only drive the low 24-bits
937 * during bus mastering, then you would pass 0x00ffffff as the mask
938 * to this function.
939 */
940int dma_supported(struct device *dev, u64 mask)
941{
942 if (mask < (u64)arm_dma_limit)
943 return 0;
944 return 1;
945}
946EXPORT_SYMBOL(dma_supported);
947
948int dma_set_mask(struct device *dev, u64 dma_mask)
949{
950 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
951 return -EIO;
952
953#ifndef CONFIG_DMABOUNCE
954 *dev->dma_mask = dma_mask;
955#endif
956
957 return 0;
958}
959EXPORT_SYMBOL(dma_set_mask);
960
Russell King24056f52011-01-03 11:29:28 +0000961#define PREALLOC_DMA_DEBUG_ENTRIES 4096
962
963static int __init dma_debug_do_init(void)
964{
Russell King45cd5292012-01-12 23:08:07 +0000965#ifdef CONFIG_MMU
966 arm_vmregion_create_proc("dma-mappings", &consistent_head);
967#endif
Russell King24056f52011-01-03 11:29:28 +0000968 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
969 return 0;
970}
971fs_initcall(dma_debug_do_init);