blob: cf623944ff7d541612ba2137f564cfe2f62943f3 [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 Szyprowskid4398df2011-12-29 13:09:51 +010020#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050021#include <linux/highmem.h>
Marek Szyprowskid4398df2011-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 Szyprowskid4398df2011-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
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010037/**
38 * arm_dma_map_page - map a portion of a page for streaming DMA
39 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
40 * @page: page that buffer resides in
41 * @offset: offset into page for start of buffer
42 * @size: size of buffer to map
43 * @dir: DMA transfer direction
44 *
45 * Ensure that any data held in the cache is appropriately discarded
46 * or written back.
47 *
48 * The device owns this memory once this call has completed. The CPU
49 * can regain ownership by calling dma_unmap_page().
50 */
51static inline dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
52 unsigned long offset, size_t size, enum dma_data_direction dir,
53 struct dma_attrs *attrs)
54{
55 return __dma_map_page(dev, page, offset, size, dir);
56}
57
58/**
59 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
60 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
61 * @handle: DMA address of buffer
62 * @size: size of buffer (same as passed to dma_map_page)
63 * @dir: DMA transfer direction (same as passed to dma_map_page)
64 *
65 * Unmap a page streaming mode DMA translation. The handle and size
66 * must match what was provided in the previous dma_map_page() call.
67 * All other usages are undefined.
68 *
69 * After this call, reads by the CPU to the buffer are guaranteed to see
70 * whatever the device wrote there.
71 */
72static inline void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
73 size_t size, enum dma_data_direction dir,
74 struct dma_attrs *attrs)
75{
76 __dma_unmap_page(dev, handle, size, dir);
77}
78
79static inline void arm_dma_sync_single_for_cpu(struct device *dev,
80 dma_addr_t handle, size_t size, enum dma_data_direction dir)
81{
82 unsigned int offset = handle & (PAGE_SIZE - 1);
83 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
84 if (!dmabounce_sync_for_cpu(dev, handle, size, dir))
85 return;
86
87 __dma_page_dev_to_cpu(page, offset, size, dir);
88}
89
90static inline void arm_dma_sync_single_for_device(struct device *dev,
91 dma_addr_t handle, size_t size, enum dma_data_direction dir)
92{
93 unsigned int offset = handle & (PAGE_SIZE - 1);
94 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
95 if (!dmabounce_sync_for_device(dev, handle, size, dir))
96 return;
97
98 __dma_page_cpu_to_dev(page, offset, size, dir);
99}
100
101static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
102
103struct dma_map_ops arm_dma_ops = {
104 .map_page = arm_dma_map_page,
105 .unmap_page = arm_dma_unmap_page,
106 .map_sg = arm_dma_map_sg,
107 .unmap_sg = arm_dma_unmap_sg,
108 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
109 .sync_single_for_device = arm_dma_sync_single_for_device,
110 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
111 .sync_sg_for_device = arm_dma_sync_sg_for_device,
112 .set_dma_mask = arm_dma_set_mask,
113};
114EXPORT_SYMBOL(arm_dma_ops);
115
Catalin Marinasab6494f2009-07-24 12:35:02 +0100116static u64 get_coherent_dma_mask(struct device *dev)
117{
Russell King022ae532011-07-08 21:26:59 +0100118 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Catalin Marinasab6494f2009-07-24 12:35:02 +0100120 if (dev) {
121 mask = dev->coherent_dma_mask;
122
123 /*
124 * Sanity check the DMA mask - it must be non-zero, and
125 * must be able to be satisfied by a DMA allocation.
126 */
127 if (mask == 0) {
128 dev_warn(dev, "coherent DMA mask is unset\n");
129 return 0;
130 }
131
Russell King022ae532011-07-08 21:26:59 +0100132 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100133 dev_warn(dev, "coherent DMA mask %#llx is smaller "
134 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100135 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100136 return 0;
137 }
138 }
139
140 return mask;
141}
142
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100143static void __dma_clear_buffer(struct page *page, size_t size)
144{
145 void *ptr;
146 /*
147 * Ensure that the allocated pages are zeroed, and that any data
148 * lurking in the kernel direct-mapped region is invalidated.
149 */
150 ptr = page_address(page);
151 memset(ptr, 0, size);
152 dmac_flush_range(ptr, ptr + size);
153 outer_flush_range(__pa(ptr), __pa(ptr) + size);
154}
155
Russell King7a9a32a2009-11-19 15:31:07 +0000156/*
157 * Allocate a DMA buffer for 'dev' of size 'size' using the
158 * specified gfp mask. Note that 'size' must be page aligned.
159 */
160static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
161{
162 unsigned long order = get_order(size);
163 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000164
165 page = alloc_pages(gfp, order);
166 if (!page)
167 return NULL;
168
169 /*
170 * Now split the huge page and free the excess pages
171 */
172 split_page(page, order);
173 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
174 __free_page(p);
175
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100176 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000177
178 return page;
179}
180
181/*
182 * Free a DMA buffer. 'size' must be page aligned.
183 */
184static void __dma_free_buffer(struct page *page, size_t size)
185{
186 struct page *e = page + (size >> PAGE_SHIFT);
187
188 while (page < e) {
189 __free_page(page);
190 page++;
191 }
192}
193
Catalin Marinasab6494f2009-07-24 12:35:02 +0100194#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100195
Jon Medhurst99d17172011-08-02 17:28:27 +0100196#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700197#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000200 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100202static pte_t **consistent_pte;
203
Steve Mucklef132c6c2012-06-06 18:30:57 -0700204#define DEFAULT_CONSISTENT_DMA_SIZE (7*SZ_2M)
Jon Medhurst99d17172011-08-02 17:28:27 +0100205
206unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
207
208void __init init_consistent_dma_size(unsigned long size)
209{
210 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
211
212 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
213 BUG_ON(base < VMALLOC_END);
214
215 /* Grow region to accommodate specified size */
216 if (base < consistent_base)
217 consistent_base = base;
218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Russell King13ccf3a2009-11-19 15:07:04 +0000220#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Russell King13ccf3a2009-11-19 15:07:04 +0000222static struct arm_vmregion_head consistent_head = {
223 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .vm_end = CONSISTENT_END,
226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#ifdef CONFIG_HUGETLB_PAGE
229#error ARM Coherent DMA allocator does not (yet) support huge TLB
230#endif
231
Russell King88c58f32009-11-19 16:46:02 +0000232/*
233 * Initialise the consistent memory allocation.
234 */
235static int __init consistent_init(void)
236{
237 int ret = 0;
238 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000239 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000240 pmd_t *pmd;
241 pte_t *pte;
242 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100243 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100244 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100245
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200246 if (IS_ENABLED(CONFIG_CMA) && !IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100247 return 0;
248
Jon Medhurst99d17172011-08-02 17:28:27 +0100249 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
250 if (!consistent_pte) {
251 pr_err("%s: no memory\n", __func__);
252 return -ENOMEM;
253 }
254
255 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
256 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000257
258 do {
259 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000260
261 pud = pud_alloc(&init_mm, pgd, base);
262 if (!pud) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100263 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000264 ret = -ENOMEM;
265 break;
266 }
267
268 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000269 if (!pmd) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100270 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000271 ret = -ENOMEM;
272 break;
273 }
274 WARN_ON(!pmd_none(*pmd));
275
276 pte = pte_alloc_kernel(pmd, base);
277 if (!pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100278 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000279 ret = -ENOMEM;
280 break;
281 }
282
283 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100284 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000285 } while (base < CONSISTENT_END);
286
287 return ret;
288}
Russell King88c58f32009-11-19 16:46:02 +0000289core_initcall(consistent_init);
290
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100291static void *__alloc_from_contiguous(struct device *dev, size_t size,
292 pgprot_t prot, struct page **ret_page);
293
294static struct arm_vmregion_head coherent_head = {
295 .vm_lock = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock),
296 .vm_list = LIST_HEAD_INIT(coherent_head.vm_list),
297};
298
299size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8;
300
301static int __init early_coherent_pool(char *p)
302{
303 coherent_pool_size = memparse(p, &p);
304 return 0;
305}
306early_param("coherent_pool", early_coherent_pool);
307
308/*
309 * Initialise the coherent pool for atomic allocations.
310 */
311static int __init coherent_init(void)
312{
313 pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
314 size_t size = coherent_pool_size;
315 struct page *page;
316 void *ptr;
317
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200318 if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100319 return 0;
320
321 ptr = __alloc_from_contiguous(NULL, size, prot, &page);
322 if (ptr) {
323 coherent_head.vm_start = (unsigned long) ptr;
324 coherent_head.vm_end = (unsigned long) ptr + size;
325 printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n",
326 (unsigned)size / 1024);
327 return 0;
328 }
329 printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
330 (unsigned)size / 1024);
331 return -ENOMEM;
332}
333/*
334 * CMA is activated by core_initcall, so we must be called after it.
335 */
336postcore_initcall(coherent_init);
337
338struct dma_contig_early_reserve {
339 phys_addr_t base;
340 unsigned long size;
341};
342
343static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
344
345static int dma_mmu_remap_num __initdata;
346
347void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
348{
349 dma_mmu_remap[dma_mmu_remap_num].base = base;
350 dma_mmu_remap[dma_mmu_remap_num].size = size;
351 dma_mmu_remap_num++;
352}
353
354void __init dma_contiguous_remap(void)
355{
356 int i;
357 for (i = 0; i < dma_mmu_remap_num; i++) {
358 phys_addr_t start = dma_mmu_remap[i].base;
359 phys_addr_t end = start + dma_mmu_remap[i].size;
360 struct map_desc map;
361 unsigned long addr;
362
363 if (end > arm_lowmem_limit)
364 end = arm_lowmem_limit;
365 if (start >= end)
366 return;
367
368 map.pfn = __phys_to_pfn(start);
369 map.virtual = __phys_to_virt(start);
370 map.length = end - start;
371 map.type = MT_MEMORY_DMA_READY;
372
373 /*
374 * Clear previous low-memory mapping
375 */
376 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
377 addr += PGDIR_SIZE)
378 pmd_clear(pmd_off_k(addr));
379
380 iotable_init(&map, 1);
381 }
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void *
Russell King45cd5292012-01-12 23:08:07 +0000385__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
386 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Russell King13ccf3a2009-11-19 15:07:04 +0000388 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100389 size_t align;
390 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Jon Medhurst99d17172011-08-02 17:28:27 +0100392 if (!consistent_pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100393 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000394 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000395 return NULL;
396 }
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
Russell King5bc23d32010-07-25 08:57:02 +0100399 * Align the virtual region allocation - maximum alignment is
400 * a section size, minimum is a page size. This helps reduce
401 * fragmentation of the DMA space, and also prevents allocations
402 * smaller than a section from crossing a section boundary.
403 */
Russell Kingc947f692010-11-03 16:00:15 +0000404 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100405 if (bit > SECTION_SHIFT)
406 bit = SECTION_SHIFT;
407 align = 1 << bit;
408
409 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * Allocate a virtual address in the consistent mapping region.
411 */
Russell King5bc23d32010-07-25 08:57:02 +0100412 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000413 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000415 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000416 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
417 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Kevin Hilman37134cd2006-01-12 16:12:21 +0000419 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 c->vm_pages = page;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 do {
423 BUG_ON(!pte_none(*pte));
424
Russell Kingad1ae2f2006-12-13 14:34:43 +0000425 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 page++;
427 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000428 off++;
429 if (off >= PTRS_PER_PTE) {
430 off = 0;
431 pte = consistent_pte[++idx];
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } while (size -= PAGE_SIZE);
434
Russell King2be23c42010-09-08 16:27:56 +0100435 dsb();
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return (void *)c->vm_start;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return NULL;
440}
Russell King695ae0a2009-11-19 16:31:39 +0000441
442static void __dma_free_remap(void *cpu_addr, size_t size)
443{
444 struct arm_vmregion *c;
445 unsigned long addr;
446 pte_t *ptep;
447 int idx;
448 u32 off;
449
450 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
451 if (!c) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100452 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000453 __func__, cpu_addr);
454 dump_stack();
455 return;
456 }
457
458 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100459 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000460 __func__, c->vm_end - c->vm_start, size);
461 dump_stack();
462 size = c->vm_end - c->vm_start;
463 }
464
465 idx = CONSISTENT_PTE_INDEX(c->vm_start);
466 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
467 ptep = consistent_pte[idx] + off;
468 addr = c->vm_start;
469 do {
470 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000471
472 ptep++;
473 addr += PAGE_SIZE;
474 off++;
475 if (off >= PTRS_PER_PTE) {
476 off = 0;
477 ptep = consistent_pte[++idx];
478 }
479
Russell Kingacaac252009-11-20 18:19:52 +0000480 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100481 pr_crit("%s: bad page in kernel page table\n",
482 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000483 } while (size -= PAGE_SIZE);
484
485 flush_tlb_kernel_range(c->vm_start, c->vm_end);
486
487 arm_vmregion_free(&consistent_head, c);
488}
489
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100490static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
491 void *data)
492{
493 struct page *page = virt_to_page(addr);
494 pgprot_t prot = *(pgprot_t *)data;
495
496 set_pte_ext(pte, mk_pte(page, prot), 0);
497 return 0;
498}
499
500static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
501{
502 unsigned long start = (unsigned long) page_address(page);
503 unsigned end = start + size;
504
505 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
506 dsb();
507 flush_tlb_kernel_range(start, end);
508}
509
510static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
511 pgprot_t prot, struct page **ret_page,
512 const void *caller)
513{
514 struct page *page;
515 void *ptr;
516 page = __dma_alloc_buffer(dev, size, gfp);
517 if (!page)
518 return NULL;
519
520 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
521 if (!ptr) {
522 __dma_free_buffer(page, size);
523 return NULL;
524 }
525
526 *ret_page = page;
527 return ptr;
528}
529
530static void *__alloc_from_pool(struct device *dev, size_t size,
531 struct page **ret_page, const void *caller)
532{
533 struct arm_vmregion *c;
534 size_t align;
535
536 if (!coherent_head.vm_start) {
537 printk(KERN_ERR "%s: coherent pool not initialised!\n",
538 __func__);
539 dump_stack();
540 return NULL;
541 }
542
543 /*
544 * Align the region allocation - allocations from pool are rather
545 * small, so align them to their order in pages, minimum is a page
546 * size. This helps reduce fragmentation of the DMA space.
547 */
548 align = PAGE_SIZE << get_order(size);
549 c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller);
550 if (c) {
551 void *ptr = (void *)c->vm_start;
552 struct page *page = virt_to_page(ptr);
553 *ret_page = page;
554 return ptr;
555 }
556 return NULL;
557}
558
559static int __free_from_pool(void *cpu_addr, size_t size)
560{
561 unsigned long start = (unsigned long)cpu_addr;
562 unsigned long end = start + size;
563 struct arm_vmregion *c;
564
565 if (start < coherent_head.vm_start || end > coherent_head.vm_end)
566 return 0;
567
568 c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start);
569
570 if ((c->vm_end - c->vm_start) != size) {
571 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
572 __func__, c->vm_end - c->vm_start, size);
573 dump_stack();
574 size = c->vm_end - c->vm_start;
575 }
576
577 arm_vmregion_free(&coherent_head, c);
578 return 1;
579}
580
581static void *__alloc_from_contiguous(struct device *dev, size_t size,
582 pgprot_t prot, struct page **ret_page)
583{
584 unsigned long order = get_order(size);
585 size_t count = size >> PAGE_SHIFT;
586 struct page *page;
587
588 page = dma_alloc_from_contiguous(dev, count, order);
589 if (!page)
590 return NULL;
591
592 __dma_clear_buffer(page, size);
593 __dma_remap(page, size, prot);
594
595 *ret_page = page;
596 return page_address(page);
597}
598
599static void __free_from_contiguous(struct device *dev, struct page *page,
600 size_t size)
601{
602 __dma_remap(page, size, pgprot_kernel);
603 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
604}
605
606#define nommu() 0
607
Catalin Marinasab6494f2009-07-24 12:35:02 +0100608#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000609
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100610#define nommu() 1
611
612#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
613#define __alloc_from_pool(dev, size, ret_page, c) NULL
614#define __alloc_from_contiguous(dev, size, prot, ret) NULL
615#define __free_from_pool(cpu_addr, size) 0
616#define __free_from_contiguous(dev, page, size) do { } while (0)
617#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000618
619#endif /* CONFIG_MMU */
620
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100621static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
622 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100623{
Russell King04da5692009-11-19 15:54:45 +0000624 struct page *page;
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100625 page = __dma_alloc_buffer(dev, size, gfp);
626 if (!page)
627 return NULL;
628
629 *ret_page = page;
630 return page_address(page);
631}
632
633
634
635static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
636 gfp_t gfp, pgprot_t prot, const void *caller)
637{
638 u64 mask = get_coherent_dma_mask(dev);
639 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000640 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100641
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100642#ifdef CONFIG_DMA_API_DEBUG
643 u64 limit = (mask + 1) & ~mask;
644 if (limit && size >= limit) {
645 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
646 size, mask);
647 return NULL;
648 }
649#endif
650
651 if (!mask)
652 return NULL;
653
654 if (mask < 0xffffffffULL)
655 gfp |= GFP_DMA;
656
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100657 /*
658 * Following is a work-around (a.k.a. hack) to prevent pages
659 * with __GFP_COMP being passed to split_page() which cannot
660 * handle them. The real problem is that this flag probably
661 * should be 0 on ARM as it is not supported on this
662 * platform; see CONFIG_HUGETLBFS.
663 */
664 gfp &= ~(__GFP_COMP);
665
Marek Szyprowski1dc8f002012-02-29 14:45:28 +0100666 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000667 size = PAGE_ALIGN(size);
668
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100669 if (arch_is_coherent() || nommu())
670 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200671 else if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100672 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
673 else if (gfp & GFP_ATOMIC)
674 addr = __alloc_from_pool(dev, size, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000675 else
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100676 addr = __alloc_from_contiguous(dev, size, prot, &page);
Russell King31ebf942009-11-19 21:12:17 +0000677
678 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000679 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000680
681 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100682}
Russell King695ae0a2009-11-19 16:31:39 +0000683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
685 * Allocate DMA-coherent memory space and return both the kernel remapped
686 * virtual and bus address for that space.
687 */
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100688void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle,
689 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400691 void *memory;
692
693 if (dma_alloc_from_coherent(dev, size, handle, &memory))
694 return memory;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000697 pgprot_dmacoherent(pgprot_kernel),
698 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700EXPORT_SYMBOL(dma_alloc_coherent);
701
702/*
703 * Allocate a writecombining region, in much the same way as
704 * dma_alloc_coherent above.
705 */
706void *
Al Virof9e32142005-10-21 03:20:58 -0400707dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000710 pgprot_writecombine(pgprot_kernel),
711 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713EXPORT_SYMBOL(dma_alloc_writecombine);
714
715static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
716 void *cpu_addr, dma_addr_t dma_addr, size_t size)
717{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100718 int ret = -ENXIO;
719#ifdef CONFIG_MMU
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100720 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowskif504f8e2012-05-15 19:04:13 +0200721
722 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
723 return ret;
724
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100725 ret = remap_pfn_range(vma, vma->vm_start,
726 pfn + vma->vm_pgoff,
727 vma->vm_end - vma->vm_start,
728 vma->vm_page_prot);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100729#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 return ret;
732}
733
734int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
735 void *cpu_addr, dma_addr_t dma_addr, size_t size)
736{
Russell King26a26d32009-11-20 21:06:43 +0000737 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
739}
740EXPORT_SYMBOL(dma_mmap_coherent);
741
742int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
743 void *cpu_addr, dma_addr_t dma_addr, size_t size)
744{
745 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
746 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
747}
748EXPORT_SYMBOL(dma_mmap_writecombine);
749
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/*
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100752 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
755{
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100756 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000757
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400758 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
759 return;
760
Russell King3e82d012009-11-19 15:38:12 +0000761 size = PAGE_ALIGN(size);
762
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100763 if (arch_is_coherent() || nommu()) {
764 __dma_free_buffer(page, size);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200765 } else if (!IS_ENABLED(CONFIG_CMA)) {
Russell King695ae0a2009-11-19 16:31:39 +0000766 __dma_free_remap(cpu_addr, size);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100767 __dma_free_buffer(page, size);
768 } else {
769 if (__free_from_pool(cpu_addr, size))
770 return;
771 /*
772 * Non-atomic allocations cannot be freed with IRQs disabled
773 */
774 WARN_ON(irqs_disabled());
775 __free_from_contiguous(dev, page, size);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778EXPORT_SYMBOL(dma_free_coherent);
779
Russell King65af1912009-11-24 17:53:33 +0000780static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000781 size_t size, enum dma_data_direction dir,
782 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000783{
784 /*
785 * A single sg entry may refer to multiple physically contiguous
786 * pages. But we still need to process highmem pages individually.
787 * If highmem is not configured then the bulk of this loop gets
788 * optimized out.
789 */
790 size_t left = size;
791 do {
792 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000793 void *vaddr;
794
795 if (PageHighMem(page)) {
796 if (len + offset > PAGE_SIZE) {
797 if (offset >= PAGE_SIZE) {
798 page += offset / PAGE_SIZE;
799 offset %= PAGE_SIZE;
800 }
801 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000802 }
Russell King93f1d622009-11-24 14:41:01 +0000803 vaddr = kmap_high_get(page);
804 if (vaddr) {
805 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000806 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000807 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100808 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500809 /* unmapped pages might still be cached */
810 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100811 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500812 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000813 }
814 } else {
815 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000816 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000817 }
Russell King65af1912009-11-24 17:53:33 +0000818 offset = 0;
819 page++;
820 left -= len;
821 } while (left);
822}
823
824void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
825 size_t size, enum dma_data_direction dir)
826{
Nicolas Pitre43377452009-03-12 22:52:09 -0400827 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400828
Russell Kinga9c91472009-11-26 16:19:58 +0000829 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400830
Russell King65af1912009-11-24 17:53:33 +0000831 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000832 if (dir == DMA_FROM_DEVICE) {
833 outer_inv_range(paddr, paddr + size);
834 } else {
835 outer_clean_range(paddr, paddr + size);
836 }
837 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400838}
Russell King4ea0d732009-11-24 16:27:17 +0000839EXPORT_SYMBOL(___dma_page_cpu_to_dev);
840
841void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
842 size_t size, enum dma_data_direction dir)
843{
Russell King2ffe2da2009-10-31 16:52:16 +0000844 unsigned long paddr = page_to_phys(page) + off;
845
846 /* FIXME: non-speculating: not required */
847 /* don't bother invalidating if DMA to device */
848 if (dir != DMA_TO_DEVICE)
849 outer_inv_range(paddr, paddr + size);
850
Russell Kinga9c91472009-11-26 16:19:58 +0000851 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100852
853 /*
854 * Mark the D-cache clean for this page to avoid extra flushing.
855 */
856 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
857 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000858}
859EXPORT_SYMBOL(___dma_page_dev_to_cpu);
Nicolas Pitre43377452009-03-12 22:52:09 -0400860
Russell Kingafd1a322008-09-25 16:30:57 +0100861/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100862 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100863 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
864 * @sg: list of buffers
865 * @nents: number of buffers to map
866 * @dir: DMA transfer direction
867 *
868 * Map a set of buffers described by scatterlist in streaming mode for DMA.
869 * This is the scatter-gather version of the dma_map_single interface.
870 * Here the scatter gather list elements are each tagged with the
871 * appropriate dma address and length. They are obtained via
872 * sg_dma_{address,length}.
873 *
874 * Device ownership issues as mentioned for dma_map_single are the same
875 * here.
876 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100877int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
878 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100879{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100880 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100881 struct scatterlist *s;
Russell King01135d92008-09-25 21:05:02 +0100882 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100883
884 for_each_sg(sg, s, nents, i) {
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100885 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
886 s->length, dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100887 if (dma_mapping_error(dev, s->dma_address))
888 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100889 }
Russell Kingafd1a322008-09-25 16:30:57 +0100890 return nents;
Russell King01135d92008-09-25 21:05:02 +0100891
892 bad_mapping:
893 for_each_sg(sg, s, i, j)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100894 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100895 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100896}
Russell Kingafd1a322008-09-25 16:30:57 +0100897
898/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100899 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100900 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
901 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100902 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100903 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
904 *
905 * Unmap a set of streaming mode DMA translations. Again, CPU access
906 * rules concerning calls here are the same as for dma_unmap_single().
907 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100908void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
909 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100910{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100911 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d92008-09-25 21:05:02 +0100912 struct scatterlist *s;
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100913
Russell King01135d92008-09-25 21:05:02 +0100914 int i;
915
916 for_each_sg(sg, s, nents, i)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100917 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100918}
Russell Kingafd1a322008-09-25 16:30:57 +0100919
920/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100921 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100922 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
923 * @sg: list of buffers
924 * @nents: number of buffers to map (returned from dma_map_sg)
925 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
926 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100927void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100928 int nents, enum dma_data_direction dir)
929{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100930 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100931 struct scatterlist *s;
932 int i;
933
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100934 for_each_sg(sg, s, nents, i)
935 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
936 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100937}
Russell Kingafd1a322008-09-25 16:30:57 +0100938
939/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100940 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100941 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
942 * @sg: list of buffers
943 * @nents: number of buffers to map (returned from dma_map_sg)
944 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
945 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100946void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100947 int nents, enum dma_data_direction dir)
948{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100949 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100950 struct scatterlist *s;
951 int i;
952
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100953 for_each_sg(sg, s, nents, i)
954 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
955 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100956}
Russell King24056f52011-01-03 11:29:28 +0000957
Russell King022ae532011-07-08 21:26:59 +0100958/*
959 * Return whether the given device DMA address mask can be supported
960 * properly. For example, if your device can only drive the low 24-bits
961 * during bus mastering, then you would pass 0x00ffffff as the mask
962 * to this function.
963 */
964int dma_supported(struct device *dev, u64 mask)
965{
966 if (mask < (u64)arm_dma_limit)
967 return 0;
968 return 1;
969}
970EXPORT_SYMBOL(dma_supported);
971
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100972static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +0100973{
974 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
975 return -EIO;
976
977#ifndef CONFIG_DMABOUNCE
978 *dev->dma_mask = dma_mask;
979#endif
980
981 return 0;
982}
Russell King022ae532011-07-08 21:26:59 +0100983
Russell King24056f52011-01-03 11:29:28 +0000984#define PREALLOC_DMA_DEBUG_ENTRIES 4096
985
986static int __init dma_debug_do_init(void)
987{
Russell King45cd5292012-01-12 23:08:07 +0000988#ifdef CONFIG_MMU
989 arm_vmregion_create_proc("dma-mappings", &consistent_head);
990#endif
Russell King24056f52011-01-03 11:29:28 +0000991 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
992 return 0;
993}
994fs_initcall(dma_debug_do_init);