blob: 9145919878bd4a8e9a4d0e06ea2e903ba9d5bcb0 [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 Szyprowski8b59d2a2012-02-10 19:55:20 +010037/*
38 * The DMA API is built upon the notion of "buffer ownership". A buffer
39 * is either exclusively owned by the CPU (and therefore may be accessed
40 * by it) or exclusively owned by the DMA device. These helper functions
41 * represent the transitions between these two ownership states.
42 *
43 * Note, however, that on later ARMs, this notion does not work due to
44 * speculative prefetches. We model our approach on the assumption that
45 * the CPU does do speculative prefetches, which means we clean caches
46 * before transfers and delay cache invalidation until transfer completion.
47 *
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010048 */
Marek Szyprowski53e207d2012-02-10 19:55:20 +010049static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010050 size_t, enum dma_data_direction);
Marek Szyprowski53e207d2012-02-10 19:55:20 +010051static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010052 size_t, enum dma_data_direction);
53
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010054/**
55 * arm_dma_map_page - map a portion of a page for streaming DMA
56 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
57 * @page: page that buffer resides in
58 * @offset: offset into page for start of buffer
59 * @size: size of buffer to map
60 * @dir: DMA transfer direction
61 *
62 * Ensure that any data held in the cache is appropriately discarded
63 * or written back.
64 *
65 * The device owns this memory once this call has completed. The CPU
66 * can regain ownership by calling dma_unmap_page().
67 */
Marek Szyprowski53e207d2012-02-10 19:55:20 +010068static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010069 unsigned long offset, size_t size, enum dma_data_direction dir,
70 struct dma_attrs *attrs)
71{
Marek Szyprowski53e207d2012-02-10 19:55:20 +010072 if (!arch_is_coherent())
73 __dma_page_cpu_to_dev(page, offset, size, dir);
74 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010075}
76
77/**
78 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
79 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
80 * @handle: DMA address of buffer
81 * @size: size of buffer (same as passed to dma_map_page)
82 * @dir: DMA transfer direction (same as passed to dma_map_page)
83 *
84 * Unmap a page streaming mode DMA translation. The handle and size
85 * must match what was provided in the previous dma_map_page() call.
86 * All other usages are undefined.
87 *
88 * After this call, reads by the CPU to the buffer are guaranteed to see
89 * whatever the device wrote there.
90 */
Marek Szyprowski53e207d2012-02-10 19:55:20 +010091static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010092 size_t size, enum dma_data_direction dir,
93 struct dma_attrs *attrs)
94{
Marek Szyprowski53e207d2012-02-10 19:55:20 +010095 if (!arch_is_coherent())
96 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
97 handle & ~PAGE_MASK, size, dir);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010098}
99
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100100static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100101 dma_addr_t handle, size_t size, enum dma_data_direction dir)
102{
103 unsigned int offset = handle & (PAGE_SIZE - 1);
104 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100105 if (!arch_is_coherent())
106 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100107}
108
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100109static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100110 dma_addr_t handle, size_t size, enum dma_data_direction dir)
111{
112 unsigned int offset = handle & (PAGE_SIZE - 1);
113 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100114 if (!arch_is_coherent())
115 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100116}
117
118static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
119
120struct dma_map_ops arm_dma_ops = {
121 .map_page = arm_dma_map_page,
122 .unmap_page = arm_dma_unmap_page,
123 .map_sg = arm_dma_map_sg,
124 .unmap_sg = arm_dma_unmap_sg,
125 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
126 .sync_single_for_device = arm_dma_sync_single_for_device,
127 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
128 .sync_sg_for_device = arm_dma_sync_sg_for_device,
129 .set_dma_mask = arm_dma_set_mask,
130};
131EXPORT_SYMBOL(arm_dma_ops);
132
Catalin Marinasab6494f2009-07-24 12:35:02 +0100133static u64 get_coherent_dma_mask(struct device *dev)
134{
Russell King022ae532011-07-08 21:26:59 +0100135 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Catalin Marinasab6494f2009-07-24 12:35:02 +0100137 if (dev) {
138 mask = dev->coherent_dma_mask;
139
140 /*
141 * Sanity check the DMA mask - it must be non-zero, and
142 * must be able to be satisfied by a DMA allocation.
143 */
144 if (mask == 0) {
145 dev_warn(dev, "coherent DMA mask is unset\n");
146 return 0;
147 }
148
Russell King022ae532011-07-08 21:26:59 +0100149 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100150 dev_warn(dev, "coherent DMA mask %#llx is smaller "
151 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100152 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100153 return 0;
154 }
155 }
156
157 return mask;
158}
159
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100160static void __dma_clear_buffer(struct page *page, size_t size)
161{
162 void *ptr;
163 /*
164 * Ensure that the allocated pages are zeroed, and that any data
165 * lurking in the kernel direct-mapped region is invalidated.
166 */
167 ptr = page_address(page);
168 memset(ptr, 0, size);
169 dmac_flush_range(ptr, ptr + size);
170 outer_flush_range(__pa(ptr), __pa(ptr) + size);
171}
172
Russell King7a9a32a2009-11-19 15:31:07 +0000173/*
174 * Allocate a DMA buffer for 'dev' of size 'size' using the
175 * specified gfp mask. Note that 'size' must be page aligned.
176 */
177static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
178{
179 unsigned long order = get_order(size);
180 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000181
182 page = alloc_pages(gfp, order);
183 if (!page)
184 return NULL;
185
186 /*
187 * Now split the huge page and free the excess pages
188 */
189 split_page(page, order);
190 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
191 __free_page(p);
192
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100193 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000194
195 return page;
196}
197
198/*
199 * Free a DMA buffer. 'size' must be page aligned.
200 */
201static void __dma_free_buffer(struct page *page, size_t size)
202{
203 struct page *e = page + (size >> PAGE_SHIFT);
204
205 while (page < e) {
206 __free_page(page);
207 page++;
208 }
209}
210
Catalin Marinasab6494f2009-07-24 12:35:02 +0100211#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100212
Jon Medhurst99d17172011-08-02 17:28:27 +0100213#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700214#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000217 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100219static pte_t **consistent_pte;
220
Steve Mucklef132c6c2012-06-06 18:30:57 -0700221#define DEFAULT_CONSISTENT_DMA_SIZE (7*SZ_2M)
Jon Medhurst99d17172011-08-02 17:28:27 +0100222
223unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
224
225void __init init_consistent_dma_size(unsigned long size)
226{
227 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
228
229 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
230 BUG_ON(base < VMALLOC_END);
231
232 /* Grow region to accommodate specified size */
233 if (base < consistent_base)
234 consistent_base = base;
235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Russell King13ccf3a2009-11-19 15:07:04 +0000237#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Russell King13ccf3a2009-11-19 15:07:04 +0000239static struct arm_vmregion_head consistent_head = {
240 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .vm_end = CONSISTENT_END,
243};
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#ifdef CONFIG_HUGETLB_PAGE
246#error ARM Coherent DMA allocator does not (yet) support huge TLB
247#endif
248
Russell King88c58f32009-11-19 16:46:02 +0000249/*
250 * Initialise the consistent memory allocation.
251 */
252static int __init consistent_init(void)
253{
254 int ret = 0;
255 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000256 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000257 pmd_t *pmd;
258 pte_t *pte;
259 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100260 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100261 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100262
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200263 if (IS_ENABLED(CONFIG_CMA) && !IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100264 return 0;
265
Jon Medhurst99d17172011-08-02 17:28:27 +0100266 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
267 if (!consistent_pte) {
268 pr_err("%s: no memory\n", __func__);
269 return -ENOMEM;
270 }
271
272 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
273 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000274
275 do {
276 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000277
278 pud = pud_alloc(&init_mm, pgd, base);
279 if (!pud) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100280 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000281 ret = -ENOMEM;
282 break;
283 }
284
285 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000286 if (!pmd) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100287 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000288 ret = -ENOMEM;
289 break;
290 }
291 WARN_ON(!pmd_none(*pmd));
292
293 pte = pte_alloc_kernel(pmd, base);
294 if (!pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100295 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000296 ret = -ENOMEM;
297 break;
298 }
299
300 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100301 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000302 } while (base < CONSISTENT_END);
303
304 return ret;
305}
Russell King88c58f32009-11-19 16:46:02 +0000306core_initcall(consistent_init);
307
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100308static void *__alloc_from_contiguous(struct device *dev, size_t size,
309 pgprot_t prot, struct page **ret_page);
310
311static struct arm_vmregion_head coherent_head = {
312 .vm_lock = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock),
313 .vm_list = LIST_HEAD_INIT(coherent_head.vm_list),
314};
315
316size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8;
317
318static int __init early_coherent_pool(char *p)
319{
320 coherent_pool_size = memparse(p, &p);
321 return 0;
322}
323early_param("coherent_pool", early_coherent_pool);
324
325/*
326 * Initialise the coherent pool for atomic allocations.
327 */
328static int __init coherent_init(void)
329{
330 pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
331 size_t size = coherent_pool_size;
332 struct page *page;
333 void *ptr;
334
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200335 if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100336 return 0;
337
338 ptr = __alloc_from_contiguous(NULL, size, prot, &page);
339 if (ptr) {
340 coherent_head.vm_start = (unsigned long) ptr;
341 coherent_head.vm_end = (unsigned long) ptr + size;
342 printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n",
343 (unsigned)size / 1024);
344 return 0;
345 }
346 printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
347 (unsigned)size / 1024);
348 return -ENOMEM;
349}
350/*
351 * CMA is activated by core_initcall, so we must be called after it.
352 */
353postcore_initcall(coherent_init);
354
355struct dma_contig_early_reserve {
356 phys_addr_t base;
357 unsigned long size;
358};
359
360static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
361
362static int dma_mmu_remap_num __initdata;
363
364void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
365{
366 dma_mmu_remap[dma_mmu_remap_num].base = base;
367 dma_mmu_remap[dma_mmu_remap_num].size = size;
368 dma_mmu_remap_num++;
369}
370
371void __init dma_contiguous_remap(void)
372{
373 int i;
374 for (i = 0; i < dma_mmu_remap_num; i++) {
375 phys_addr_t start = dma_mmu_remap[i].base;
376 phys_addr_t end = start + dma_mmu_remap[i].size;
377 struct map_desc map;
378 unsigned long addr;
379
380 if (end > arm_lowmem_limit)
381 end = arm_lowmem_limit;
382 if (start >= end)
383 return;
384
385 map.pfn = __phys_to_pfn(start);
386 map.virtual = __phys_to_virt(start);
387 map.length = end - start;
388 map.type = MT_MEMORY_DMA_READY;
389
390 /*
391 * Clear previous low-memory mapping
392 */
393 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
394 addr += PGDIR_SIZE)
395 pmd_clear(pmd_off_k(addr));
396
397 iotable_init(&map, 1);
398 }
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static void *
Russell King45cd5292012-01-12 23:08:07 +0000402__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
403 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Russell King13ccf3a2009-11-19 15:07:04 +0000405 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100406 size_t align;
407 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jon Medhurst99d17172011-08-02 17:28:27 +0100409 if (!consistent_pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100410 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000411 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000412 return NULL;
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
Russell King5bc23d32010-07-25 08:57:02 +0100416 * Align the virtual region allocation - maximum alignment is
417 * a section size, minimum is a page size. This helps reduce
418 * fragmentation of the DMA space, and also prevents allocations
419 * smaller than a section from crossing a section boundary.
420 */
Russell Kingc947f692010-11-03 16:00:15 +0000421 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100422 if (bit > SECTION_SHIFT)
423 bit = SECTION_SHIFT;
424 align = 1 << bit;
425
426 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * Allocate a virtual address in the consistent mapping region.
428 */
Russell King5bc23d32010-07-25 08:57:02 +0100429 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000430 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000432 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000433 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
434 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Kevin Hilman37134cd2006-01-12 16:12:21 +0000436 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 c->vm_pages = page;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 do {
440 BUG_ON(!pte_none(*pte));
441
Russell Kingad1ae2f2006-12-13 14:34:43 +0000442 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 page++;
444 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000445 off++;
446 if (off >= PTRS_PER_PTE) {
447 off = 0;
448 pte = consistent_pte[++idx];
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } while (size -= PAGE_SIZE);
451
Russell King2be23c42010-09-08 16:27:56 +0100452 dsb();
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return (void *)c->vm_start;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return NULL;
457}
Russell King695ae0a2009-11-19 16:31:39 +0000458
459static void __dma_free_remap(void *cpu_addr, size_t size)
460{
461 struct arm_vmregion *c;
462 unsigned long addr;
463 pte_t *ptep;
464 int idx;
465 u32 off;
466
467 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
468 if (!c) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100469 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000470 __func__, cpu_addr);
471 dump_stack();
472 return;
473 }
474
475 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100476 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000477 __func__, c->vm_end - c->vm_start, size);
478 dump_stack();
479 size = c->vm_end - c->vm_start;
480 }
481
482 idx = CONSISTENT_PTE_INDEX(c->vm_start);
483 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
484 ptep = consistent_pte[idx] + off;
485 addr = c->vm_start;
486 do {
487 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000488
489 ptep++;
490 addr += PAGE_SIZE;
491 off++;
492 if (off >= PTRS_PER_PTE) {
493 off = 0;
494 ptep = consistent_pte[++idx];
495 }
496
Russell Kingacaac252009-11-20 18:19:52 +0000497 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100498 pr_crit("%s: bad page in kernel page table\n",
499 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000500 } while (size -= PAGE_SIZE);
501
502 flush_tlb_kernel_range(c->vm_start, c->vm_end);
503
504 arm_vmregion_free(&consistent_head, c);
505}
506
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100507static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
508 void *data)
509{
510 struct page *page = virt_to_page(addr);
511 pgprot_t prot = *(pgprot_t *)data;
512
513 set_pte_ext(pte, mk_pte(page, prot), 0);
514 return 0;
515}
516
517static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
518{
519 unsigned long start = (unsigned long) page_address(page);
520 unsigned end = start + size;
521
522 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
523 dsb();
524 flush_tlb_kernel_range(start, end);
525}
526
527static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
528 pgprot_t prot, struct page **ret_page,
529 const void *caller)
530{
531 struct page *page;
532 void *ptr;
533 page = __dma_alloc_buffer(dev, size, gfp);
534 if (!page)
535 return NULL;
536
537 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
538 if (!ptr) {
539 __dma_free_buffer(page, size);
540 return NULL;
541 }
542
543 *ret_page = page;
544 return ptr;
545}
546
547static void *__alloc_from_pool(struct device *dev, size_t size,
548 struct page **ret_page, const void *caller)
549{
550 struct arm_vmregion *c;
551 size_t align;
552
553 if (!coherent_head.vm_start) {
554 printk(KERN_ERR "%s: coherent pool not initialised!\n",
555 __func__);
556 dump_stack();
557 return NULL;
558 }
559
560 /*
561 * Align the region allocation - allocations from pool are rather
562 * small, so align them to their order in pages, minimum is a page
563 * size. This helps reduce fragmentation of the DMA space.
564 */
565 align = PAGE_SIZE << get_order(size);
566 c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller);
567 if (c) {
568 void *ptr = (void *)c->vm_start;
569 struct page *page = virt_to_page(ptr);
570 *ret_page = page;
571 return ptr;
572 }
573 return NULL;
574}
575
576static int __free_from_pool(void *cpu_addr, size_t size)
577{
578 unsigned long start = (unsigned long)cpu_addr;
579 unsigned long end = start + size;
580 struct arm_vmregion *c;
581
582 if (start < coherent_head.vm_start || end > coherent_head.vm_end)
583 return 0;
584
585 c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start);
586
587 if ((c->vm_end - c->vm_start) != size) {
588 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
589 __func__, c->vm_end - c->vm_start, size);
590 dump_stack();
591 size = c->vm_end - c->vm_start;
592 }
593
594 arm_vmregion_free(&coherent_head, c);
595 return 1;
596}
597
598static void *__alloc_from_contiguous(struct device *dev, size_t size,
599 pgprot_t prot, struct page **ret_page)
600{
601 unsigned long order = get_order(size);
602 size_t count = size >> PAGE_SHIFT;
603 struct page *page;
604
605 page = dma_alloc_from_contiguous(dev, count, order);
606 if (!page)
607 return NULL;
608
609 __dma_clear_buffer(page, size);
610 __dma_remap(page, size, prot);
611
612 *ret_page = page;
613 return page_address(page);
614}
615
616static void __free_from_contiguous(struct device *dev, struct page *page,
617 size_t size)
618{
619 __dma_remap(page, size, pgprot_kernel);
620 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
621}
622
623#define nommu() 0
624
Catalin Marinasab6494f2009-07-24 12:35:02 +0100625#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000626
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100627#define nommu() 1
628
629#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
630#define __alloc_from_pool(dev, size, ret_page, c) NULL
631#define __alloc_from_contiguous(dev, size, prot, ret) NULL
632#define __free_from_pool(cpu_addr, size) 0
633#define __free_from_contiguous(dev, page, size) do { } while (0)
634#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000635
636#endif /* CONFIG_MMU */
637
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100638static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
639 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100640{
Russell King04da5692009-11-19 15:54:45 +0000641 struct page *page;
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100642 page = __dma_alloc_buffer(dev, size, gfp);
643 if (!page)
644 return NULL;
645
646 *ret_page = page;
647 return page_address(page);
648}
649
650
651
652static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
653 gfp_t gfp, pgprot_t prot, const void *caller)
654{
655 u64 mask = get_coherent_dma_mask(dev);
656 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000657 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100658
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100659#ifdef CONFIG_DMA_API_DEBUG
660 u64 limit = (mask + 1) & ~mask;
661 if (limit && size >= limit) {
662 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
663 size, mask);
664 return NULL;
665 }
666#endif
667
668 if (!mask)
669 return NULL;
670
671 if (mask < 0xffffffffULL)
672 gfp |= GFP_DMA;
673
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100674 /*
675 * Following is a work-around (a.k.a. hack) to prevent pages
676 * with __GFP_COMP being passed to split_page() which cannot
677 * handle them. The real problem is that this flag probably
678 * should be 0 on ARM as it is not supported on this
679 * platform; see CONFIG_HUGETLBFS.
680 */
681 gfp &= ~(__GFP_COMP);
682
Marek Szyprowski1dc8f002012-02-29 14:45:28 +0100683 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000684 size = PAGE_ALIGN(size);
685
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100686 if (arch_is_coherent() || nommu())
687 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200688 else if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100689 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
690 else if (gfp & GFP_ATOMIC)
691 addr = __alloc_from_pool(dev, size, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000692 else
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100693 addr = __alloc_from_contiguous(dev, size, prot, &page);
Russell King31ebf942009-11-19 21:12:17 +0000694
695 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000696 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000697
698 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100699}
Russell King695ae0a2009-11-19 16:31:39 +0000700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
702 * Allocate DMA-coherent memory space and return both the kernel remapped
703 * virtual and bus address for that space.
704 */
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100705void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle,
706 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400708 void *memory;
709
710 if (dma_alloc_from_coherent(dev, size, handle, &memory))
711 return memory;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000714 pgprot_dmacoherent(pgprot_kernel),
715 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717EXPORT_SYMBOL(dma_alloc_coherent);
718
719/*
720 * Allocate a writecombining region, in much the same way as
721 * dma_alloc_coherent above.
722 */
723void *
Al Virof9e32142005-10-21 03:20:58 -0400724dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000727 pgprot_writecombine(pgprot_kernel),
728 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730EXPORT_SYMBOL(dma_alloc_writecombine);
731
732static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
733 void *cpu_addr, dma_addr_t dma_addr, size_t size)
734{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100735 int ret = -ENXIO;
736#ifdef CONFIG_MMU
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100737 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowskif504f8e2012-05-15 19:04:13 +0200738
739 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
740 return ret;
741
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100742 ret = remap_pfn_range(vma, vma->vm_start,
743 pfn + vma->vm_pgoff,
744 vma->vm_end - vma->vm_start,
745 vma->vm_page_prot);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100746#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 return ret;
749}
750
751int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
752 void *cpu_addr, dma_addr_t dma_addr, size_t size)
753{
Russell King26a26d32009-11-20 21:06:43 +0000754 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
756}
757EXPORT_SYMBOL(dma_mmap_coherent);
758
759int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
760 void *cpu_addr, dma_addr_t dma_addr, size_t size)
761{
762 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
763 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
764}
765EXPORT_SYMBOL(dma_mmap_writecombine);
766
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100769 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
771void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
772{
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100773 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000774
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400775 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
776 return;
777
Russell King3e82d012009-11-19 15:38:12 +0000778 size = PAGE_ALIGN(size);
779
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100780 if (arch_is_coherent() || nommu()) {
781 __dma_free_buffer(page, size);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200782 } else if (!IS_ENABLED(CONFIG_CMA)) {
Russell King695ae0a2009-11-19 16:31:39 +0000783 __dma_free_remap(cpu_addr, size);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100784 __dma_free_buffer(page, size);
785 } else {
786 if (__free_from_pool(cpu_addr, size))
787 return;
788 /*
789 * Non-atomic allocations cannot be freed with IRQs disabled
790 */
791 WARN_ON(irqs_disabled());
792 __free_from_contiguous(dev, page, size);
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795EXPORT_SYMBOL(dma_free_coherent);
796
Russell King65af1912009-11-24 17:53:33 +0000797static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000798 size_t size, enum dma_data_direction dir,
799 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000800{
801 /*
802 * A single sg entry may refer to multiple physically contiguous
803 * pages. But we still need to process highmem pages individually.
804 * If highmem is not configured then the bulk of this loop gets
805 * optimized out.
806 */
807 size_t left = size;
808 do {
809 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000810 void *vaddr;
811
812 if (PageHighMem(page)) {
813 if (len + offset > PAGE_SIZE) {
814 if (offset >= PAGE_SIZE) {
815 page += offset / PAGE_SIZE;
816 offset %= PAGE_SIZE;
817 }
818 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000819 }
Russell King93f1d622009-11-24 14:41:01 +0000820 vaddr = kmap_high_get(page);
821 if (vaddr) {
822 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000823 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000824 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100825 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500826 /* unmapped pages might still be cached */
827 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100828 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500829 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000830 }
831 } else {
832 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000833 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000834 }
Russell King65af1912009-11-24 17:53:33 +0000835 offset = 0;
836 page++;
837 left -= len;
838 } while (left);
839}
840
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100841/*
842 * Make an area consistent for devices.
843 * Note: Drivers should NOT use this function directly, as it will break
844 * platforms with CONFIG_DMABOUNCE.
845 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
846 */
847static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000848 size_t size, enum dma_data_direction dir)
849{
Nicolas Pitre43377452009-03-12 22:52:09 -0400850 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400851
Russell Kinga9c91472009-11-26 16:19:58 +0000852 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400853
Russell King65af1912009-11-24 17:53:33 +0000854 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000855 if (dir == DMA_FROM_DEVICE) {
856 outer_inv_range(paddr, paddr + size);
857 } else {
858 outer_clean_range(paddr, paddr + size);
859 }
860 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400861}
Russell King4ea0d732009-11-24 16:27:17 +0000862
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100863static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000864 size_t size, enum dma_data_direction dir)
865{
Russell King2ffe2da2009-10-31 16:52:16 +0000866 unsigned long paddr = page_to_phys(page) + off;
867
868 /* FIXME: non-speculating: not required */
869 /* don't bother invalidating if DMA to device */
870 if (dir != DMA_TO_DEVICE)
871 outer_inv_range(paddr, paddr + size);
872
Russell Kinga9c91472009-11-26 16:19:58 +0000873 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100874
875 /*
876 * Mark the D-cache clean for this page to avoid extra flushing.
877 */
878 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
879 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000880}
Nicolas Pitre43377452009-03-12 22:52:09 -0400881
Russell Kingafd1a322008-09-25 16:30:57 +0100882/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100883 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100884 * @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
887 * @dir: DMA transfer direction
888 *
889 * Map a set of buffers described by scatterlist in streaming mode for DMA.
890 * This is the scatter-gather version of the dma_map_single interface.
891 * Here the scatter gather list elements are each tagged with the
892 * appropriate dma address and length. They are obtained via
893 * sg_dma_{address,length}.
894 *
895 * Device ownership issues as mentioned for dma_map_single are the same
896 * here.
897 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100898int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
899 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100900{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100901 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100902 struct scatterlist *s;
Russell King01135d92008-09-25 21:05:02 +0100903 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100904
905 for_each_sg(sg, s, nents, i) {
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100906 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
907 s->length, dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100908 if (dma_mapping_error(dev, s->dma_address))
909 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100910 }
Russell Kingafd1a322008-09-25 16:30:57 +0100911 return nents;
Russell King01135d92008-09-25 21:05:02 +0100912
913 bad_mapping:
914 for_each_sg(sg, s, i, j)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100915 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100916 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100917}
Russell Kingafd1a322008-09-25 16:30:57 +0100918
919/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100920 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100921 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
922 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100923 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100924 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
925 *
926 * Unmap a set of streaming mode DMA translations. Again, CPU access
927 * rules concerning calls here are the same as for dma_unmap_single().
928 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100929void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
930 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100931{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100932 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d92008-09-25 21:05:02 +0100933 struct scatterlist *s;
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100934
Russell King01135d92008-09-25 21:05:02 +0100935 int i;
936
937 for_each_sg(sg, s, nents, i)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100938 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100939}
Russell Kingafd1a322008-09-25 16:30:57 +0100940
941/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100942 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100943 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
944 * @sg: list of buffers
945 * @nents: number of buffers to map (returned from dma_map_sg)
946 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
947 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100948void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100949 int nents, enum dma_data_direction dir)
950{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100951 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100952 struct scatterlist *s;
953 int i;
954
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100955 for_each_sg(sg, s, nents, i)
956 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
957 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100958}
Russell Kingafd1a322008-09-25 16:30:57 +0100959
960/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100961 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100962 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
963 * @sg: list of buffers
964 * @nents: number of buffers to map (returned from dma_map_sg)
965 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
966 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100967void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100968 int nents, enum dma_data_direction dir)
969{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100970 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100971 struct scatterlist *s;
972 int i;
973
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100974 for_each_sg(sg, s, nents, i)
975 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
976 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100977}
Russell King24056f52011-01-03 11:29:28 +0000978
Russell King022ae532011-07-08 21:26:59 +0100979/*
980 * Return whether the given device DMA address mask can be supported
981 * properly. For example, if your device can only drive the low 24-bits
982 * during bus mastering, then you would pass 0x00ffffff as the mask
983 * to this function.
984 */
985int dma_supported(struct device *dev, u64 mask)
986{
987 if (mask < (u64)arm_dma_limit)
988 return 0;
989 return 1;
990}
991EXPORT_SYMBOL(dma_supported);
992
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100993static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +0100994{
995 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
996 return -EIO;
997
Russell King022ae532011-07-08 21:26:59 +0100998 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +0100999
1000 return 0;
1001}
Russell King022ae532011-07-08 21:26:59 +01001002
Russell King24056f52011-01-03 11:29:28 +00001003#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1004
1005static int __init dma_debug_do_init(void)
1006{
Russell King45cd5292012-01-12 23:08:07 +00001007#ifdef CONFIG_MMU
1008 arm_vmregion_create_proc("dma-mappings", &consistent_head);
1009#endif
Russell King24056f52011-01-03 11:29:28 +00001010 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1011 return 0;
1012}
1013fs_initcall(dma_debug_do_init);