blob: 7ce8e77fe85e5e024c79a8f0fd2b542590187486 [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 *
48 * Private support functions: these are not part of the API and are
49 * liable to change. Drivers must not use these.
50 */
51static inline void __dma_single_cpu_to_dev(const void *kaddr, size_t size,
52 enum dma_data_direction dir)
53{
54 extern void ___dma_single_cpu_to_dev(const void *, size_t,
55 enum dma_data_direction);
56
57 if (!arch_is_coherent())
58 ___dma_single_cpu_to_dev(kaddr, size, dir);
59}
60
61static inline void __dma_single_dev_to_cpu(const void *kaddr, size_t size,
62 enum dma_data_direction dir)
63{
64 extern void ___dma_single_dev_to_cpu(const void *, size_t,
65 enum dma_data_direction);
66
67 if (!arch_is_coherent())
68 ___dma_single_dev_to_cpu(kaddr, size, dir);
69}
70
71static inline void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
72 size_t size, enum dma_data_direction dir)
73{
74 extern void ___dma_page_cpu_to_dev(struct page *, unsigned long,
75 size_t, enum dma_data_direction);
76
77 if (!arch_is_coherent())
78 ___dma_page_cpu_to_dev(page, off, size, dir);
79}
80
81static inline void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
82 size_t size, enum dma_data_direction dir)
83{
84 extern void ___dma_page_dev_to_cpu(struct page *, unsigned long,
85 size_t, enum dma_data_direction);
86
87 if (!arch_is_coherent())
88 ___dma_page_dev_to_cpu(page, off, size, dir);
89}
90
91
92static inline dma_addr_t __dma_map_page(struct device *dev, struct page *page,
93 unsigned long offset, size_t size, enum dma_data_direction dir)
94{
95 __dma_page_cpu_to_dev(page, offset, size, dir);
96 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
97}
98
99static inline void __dma_unmap_page(struct device *dev, dma_addr_t handle,
100 size_t size, enum dma_data_direction dir)
101{
102 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
103 handle & ~PAGE_MASK, size, dir);
104}
105
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100106/**
107 * arm_dma_map_page - map a portion of a page for streaming DMA
108 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
109 * @page: page that buffer resides in
110 * @offset: offset into page for start of buffer
111 * @size: size of buffer to map
112 * @dir: DMA transfer direction
113 *
114 * Ensure that any data held in the cache is appropriately discarded
115 * or written back.
116 *
117 * The device owns this memory once this call has completed. The CPU
118 * can regain ownership by calling dma_unmap_page().
119 */
120static inline dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
121 unsigned long offset, size_t size, enum dma_data_direction dir,
122 struct dma_attrs *attrs)
123{
124 return __dma_map_page(dev, page, offset, size, dir);
125}
126
127/**
128 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
129 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
130 * @handle: DMA address of buffer
131 * @size: size of buffer (same as passed to dma_map_page)
132 * @dir: DMA transfer direction (same as passed to dma_map_page)
133 *
134 * Unmap a page streaming mode DMA translation. The handle and size
135 * must match what was provided in the previous dma_map_page() call.
136 * All other usages are undefined.
137 *
138 * After this call, reads by the CPU to the buffer are guaranteed to see
139 * whatever the device wrote there.
140 */
141static inline void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
142 size_t size, enum dma_data_direction dir,
143 struct dma_attrs *attrs)
144{
145 __dma_unmap_page(dev, handle, size, dir);
146}
147
148static inline void arm_dma_sync_single_for_cpu(struct device *dev,
149 dma_addr_t handle, size_t size, enum dma_data_direction dir)
150{
151 unsigned int offset = handle & (PAGE_SIZE - 1);
152 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100153 __dma_page_dev_to_cpu(page, offset, size, dir);
154}
155
156static inline void arm_dma_sync_single_for_device(struct device *dev,
157 dma_addr_t handle, size_t size, enum dma_data_direction dir)
158{
159 unsigned int offset = handle & (PAGE_SIZE - 1);
160 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100161 __dma_page_cpu_to_dev(page, offset, size, dir);
162}
163
164static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
165
166struct dma_map_ops arm_dma_ops = {
167 .map_page = arm_dma_map_page,
168 .unmap_page = arm_dma_unmap_page,
169 .map_sg = arm_dma_map_sg,
170 .unmap_sg = arm_dma_unmap_sg,
171 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
172 .sync_single_for_device = arm_dma_sync_single_for_device,
173 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
174 .sync_sg_for_device = arm_dma_sync_sg_for_device,
175 .set_dma_mask = arm_dma_set_mask,
176};
177EXPORT_SYMBOL(arm_dma_ops);
178
Catalin Marinasab6494f2009-07-24 12:35:02 +0100179static u64 get_coherent_dma_mask(struct device *dev)
180{
Russell King022ae532011-07-08 21:26:59 +0100181 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Catalin Marinasab6494f2009-07-24 12:35:02 +0100183 if (dev) {
184 mask = dev->coherent_dma_mask;
185
186 /*
187 * Sanity check the DMA mask - it must be non-zero, and
188 * must be able to be satisfied by a DMA allocation.
189 */
190 if (mask == 0) {
191 dev_warn(dev, "coherent DMA mask is unset\n");
192 return 0;
193 }
194
Russell King022ae532011-07-08 21:26:59 +0100195 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100196 dev_warn(dev, "coherent DMA mask %#llx is smaller "
197 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100198 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100199 return 0;
200 }
201 }
202
203 return mask;
204}
205
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100206static void __dma_clear_buffer(struct page *page, size_t size)
207{
208 void *ptr;
209 /*
210 * Ensure that the allocated pages are zeroed, and that any data
211 * lurking in the kernel direct-mapped region is invalidated.
212 */
213 ptr = page_address(page);
214 memset(ptr, 0, size);
215 dmac_flush_range(ptr, ptr + size);
216 outer_flush_range(__pa(ptr), __pa(ptr) + size);
217}
218
Russell King7a9a32a2009-11-19 15:31:07 +0000219/*
220 * Allocate a DMA buffer for 'dev' of size 'size' using the
221 * specified gfp mask. Note that 'size' must be page aligned.
222 */
223static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
224{
225 unsigned long order = get_order(size);
226 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000227
228 page = alloc_pages(gfp, order);
229 if (!page)
230 return NULL;
231
232 /*
233 * Now split the huge page and free the excess pages
234 */
235 split_page(page, order);
236 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
237 __free_page(p);
238
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100239 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000240
241 return page;
242}
243
244/*
245 * Free a DMA buffer. 'size' must be page aligned.
246 */
247static void __dma_free_buffer(struct page *page, size_t size)
248{
249 struct page *e = page + (size >> PAGE_SHIFT);
250
251 while (page < e) {
252 __free_page(page);
253 page++;
254 }
255}
256
Catalin Marinasab6494f2009-07-24 12:35:02 +0100257#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100258
Jon Medhurst99d17172011-08-02 17:28:27 +0100259#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700260#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000263 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100265static pte_t **consistent_pte;
266
Steve Mucklef132c6c2012-06-06 18:30:57 -0700267#define DEFAULT_CONSISTENT_DMA_SIZE (7*SZ_2M)
Jon Medhurst99d17172011-08-02 17:28:27 +0100268
269unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
270
271void __init init_consistent_dma_size(unsigned long size)
272{
273 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
274
275 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
276 BUG_ON(base < VMALLOC_END);
277
278 /* Grow region to accommodate specified size */
279 if (base < consistent_base)
280 consistent_base = base;
281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Russell King13ccf3a2009-11-19 15:07:04 +0000283#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Russell King13ccf3a2009-11-19 15:07:04 +0000285static struct arm_vmregion_head consistent_head = {
286 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .vm_end = CONSISTENT_END,
289};
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#ifdef CONFIG_HUGETLB_PAGE
292#error ARM Coherent DMA allocator does not (yet) support huge TLB
293#endif
294
Russell King88c58f32009-11-19 16:46:02 +0000295/*
296 * Initialise the consistent memory allocation.
297 */
298static int __init consistent_init(void)
299{
300 int ret = 0;
301 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000302 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000303 pmd_t *pmd;
304 pte_t *pte;
305 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100306 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100307 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100308
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200309 if (IS_ENABLED(CONFIG_CMA) && !IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100310 return 0;
311
Jon Medhurst99d17172011-08-02 17:28:27 +0100312 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
313 if (!consistent_pte) {
314 pr_err("%s: no memory\n", __func__);
315 return -ENOMEM;
316 }
317
318 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
319 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000320
321 do {
322 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000323
324 pud = pud_alloc(&init_mm, pgd, base);
325 if (!pud) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100326 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000327 ret = -ENOMEM;
328 break;
329 }
330
331 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000332 if (!pmd) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100333 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000334 ret = -ENOMEM;
335 break;
336 }
337 WARN_ON(!pmd_none(*pmd));
338
339 pte = pte_alloc_kernel(pmd, base);
340 if (!pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100341 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000342 ret = -ENOMEM;
343 break;
344 }
345
346 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100347 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000348 } while (base < CONSISTENT_END);
349
350 return ret;
351}
Russell King88c58f32009-11-19 16:46:02 +0000352core_initcall(consistent_init);
353
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100354static void *__alloc_from_contiguous(struct device *dev, size_t size,
355 pgprot_t prot, struct page **ret_page);
356
357static struct arm_vmregion_head coherent_head = {
358 .vm_lock = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock),
359 .vm_list = LIST_HEAD_INIT(coherent_head.vm_list),
360};
361
362size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8;
363
364static int __init early_coherent_pool(char *p)
365{
366 coherent_pool_size = memparse(p, &p);
367 return 0;
368}
369early_param("coherent_pool", early_coherent_pool);
370
371/*
372 * Initialise the coherent pool for atomic allocations.
373 */
374static int __init coherent_init(void)
375{
376 pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
377 size_t size = coherent_pool_size;
378 struct page *page;
379 void *ptr;
380
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200381 if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100382 return 0;
383
384 ptr = __alloc_from_contiguous(NULL, size, prot, &page);
385 if (ptr) {
386 coherent_head.vm_start = (unsigned long) ptr;
387 coherent_head.vm_end = (unsigned long) ptr + size;
388 printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n",
389 (unsigned)size / 1024);
390 return 0;
391 }
392 printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
393 (unsigned)size / 1024);
394 return -ENOMEM;
395}
396/*
397 * CMA is activated by core_initcall, so we must be called after it.
398 */
399postcore_initcall(coherent_init);
400
401struct dma_contig_early_reserve {
402 phys_addr_t base;
403 unsigned long size;
404};
405
406static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
407
408static int dma_mmu_remap_num __initdata;
409
410void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
411{
412 dma_mmu_remap[dma_mmu_remap_num].base = base;
413 dma_mmu_remap[dma_mmu_remap_num].size = size;
414 dma_mmu_remap_num++;
415}
416
417void __init dma_contiguous_remap(void)
418{
419 int i;
420 for (i = 0; i < dma_mmu_remap_num; i++) {
421 phys_addr_t start = dma_mmu_remap[i].base;
422 phys_addr_t end = start + dma_mmu_remap[i].size;
423 struct map_desc map;
424 unsigned long addr;
425
426 if (end > arm_lowmem_limit)
427 end = arm_lowmem_limit;
428 if (start >= end)
429 return;
430
431 map.pfn = __phys_to_pfn(start);
432 map.virtual = __phys_to_virt(start);
433 map.length = end - start;
434 map.type = MT_MEMORY_DMA_READY;
435
436 /*
437 * Clear previous low-memory mapping
438 */
439 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
440 addr += PGDIR_SIZE)
441 pmd_clear(pmd_off_k(addr));
442
443 iotable_init(&map, 1);
444 }
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static void *
Russell King45cd5292012-01-12 23:08:07 +0000448__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
449 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Russell King13ccf3a2009-11-19 15:07:04 +0000451 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100452 size_t align;
453 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Jon Medhurst99d17172011-08-02 17:28:27 +0100455 if (!consistent_pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100456 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000457 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000458 return NULL;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /*
Russell King5bc23d32010-07-25 08:57:02 +0100462 * Align the virtual region allocation - maximum alignment is
463 * a section size, minimum is a page size. This helps reduce
464 * fragmentation of the DMA space, and also prevents allocations
465 * smaller than a section from crossing a section boundary.
466 */
Russell Kingc947f692010-11-03 16:00:15 +0000467 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100468 if (bit > SECTION_SHIFT)
469 bit = SECTION_SHIFT;
470 align = 1 << bit;
471
472 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * Allocate a virtual address in the consistent mapping region.
474 */
Russell King5bc23d32010-07-25 08:57:02 +0100475 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000476 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000478 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000479 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
480 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Kevin Hilman37134cd2006-01-12 16:12:21 +0000482 pte = consistent_pte[idx] + off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 c->vm_pages = page;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 do {
486 BUG_ON(!pte_none(*pte));
487
Russell Kingad1ae2f2006-12-13 14:34:43 +0000488 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 page++;
490 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000491 off++;
492 if (off >= PTRS_PER_PTE) {
493 off = 0;
494 pte = consistent_pte[++idx];
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } while (size -= PAGE_SIZE);
497
Russell King2be23c42010-09-08 16:27:56 +0100498 dsb();
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return (void *)c->vm_start;
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return NULL;
503}
Russell King695ae0a2009-11-19 16:31:39 +0000504
505static void __dma_free_remap(void *cpu_addr, size_t size)
506{
507 struct arm_vmregion *c;
508 unsigned long addr;
509 pte_t *ptep;
510 int idx;
511 u32 off;
512
513 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
514 if (!c) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100515 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000516 __func__, cpu_addr);
517 dump_stack();
518 return;
519 }
520
521 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100522 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000523 __func__, c->vm_end - c->vm_start, size);
524 dump_stack();
525 size = c->vm_end - c->vm_start;
526 }
527
528 idx = CONSISTENT_PTE_INDEX(c->vm_start);
529 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
530 ptep = consistent_pte[idx] + off;
531 addr = c->vm_start;
532 do {
533 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000534
535 ptep++;
536 addr += PAGE_SIZE;
537 off++;
538 if (off >= PTRS_PER_PTE) {
539 off = 0;
540 ptep = consistent_pte[++idx];
541 }
542
Russell Kingacaac252009-11-20 18:19:52 +0000543 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100544 pr_crit("%s: bad page in kernel page table\n",
545 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000546 } while (size -= PAGE_SIZE);
547
548 flush_tlb_kernel_range(c->vm_start, c->vm_end);
549
550 arm_vmregion_free(&consistent_head, c);
551}
552
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100553static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
554 void *data)
555{
556 struct page *page = virt_to_page(addr);
557 pgprot_t prot = *(pgprot_t *)data;
558
559 set_pte_ext(pte, mk_pte(page, prot), 0);
560 return 0;
561}
562
563static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
564{
565 unsigned long start = (unsigned long) page_address(page);
566 unsigned end = start + size;
567
568 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
569 dsb();
570 flush_tlb_kernel_range(start, end);
571}
572
573static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
574 pgprot_t prot, struct page **ret_page,
575 const void *caller)
576{
577 struct page *page;
578 void *ptr;
579 page = __dma_alloc_buffer(dev, size, gfp);
580 if (!page)
581 return NULL;
582
583 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
584 if (!ptr) {
585 __dma_free_buffer(page, size);
586 return NULL;
587 }
588
589 *ret_page = page;
590 return ptr;
591}
592
593static void *__alloc_from_pool(struct device *dev, size_t size,
594 struct page **ret_page, const void *caller)
595{
596 struct arm_vmregion *c;
597 size_t align;
598
599 if (!coherent_head.vm_start) {
600 printk(KERN_ERR "%s: coherent pool not initialised!\n",
601 __func__);
602 dump_stack();
603 return NULL;
604 }
605
606 /*
607 * Align the region allocation - allocations from pool are rather
608 * small, so align them to their order in pages, minimum is a page
609 * size. This helps reduce fragmentation of the DMA space.
610 */
611 align = PAGE_SIZE << get_order(size);
612 c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller);
613 if (c) {
614 void *ptr = (void *)c->vm_start;
615 struct page *page = virt_to_page(ptr);
616 *ret_page = page;
617 return ptr;
618 }
619 return NULL;
620}
621
622static int __free_from_pool(void *cpu_addr, size_t size)
623{
624 unsigned long start = (unsigned long)cpu_addr;
625 unsigned long end = start + size;
626 struct arm_vmregion *c;
627
628 if (start < coherent_head.vm_start || end > coherent_head.vm_end)
629 return 0;
630
631 c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start);
632
633 if ((c->vm_end - c->vm_start) != size) {
634 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
635 __func__, c->vm_end - c->vm_start, size);
636 dump_stack();
637 size = c->vm_end - c->vm_start;
638 }
639
640 arm_vmregion_free(&coherent_head, c);
641 return 1;
642}
643
644static void *__alloc_from_contiguous(struct device *dev, size_t size,
645 pgprot_t prot, struct page **ret_page)
646{
647 unsigned long order = get_order(size);
648 size_t count = size >> PAGE_SHIFT;
649 struct page *page;
650
651 page = dma_alloc_from_contiguous(dev, count, order);
652 if (!page)
653 return NULL;
654
655 __dma_clear_buffer(page, size);
656 __dma_remap(page, size, prot);
657
658 *ret_page = page;
659 return page_address(page);
660}
661
662static void __free_from_contiguous(struct device *dev, struct page *page,
663 size_t size)
664{
665 __dma_remap(page, size, pgprot_kernel);
666 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
667}
668
669#define nommu() 0
670
Catalin Marinasab6494f2009-07-24 12:35:02 +0100671#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000672
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100673#define nommu() 1
674
675#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
676#define __alloc_from_pool(dev, size, ret_page, c) NULL
677#define __alloc_from_contiguous(dev, size, prot, ret) NULL
678#define __free_from_pool(cpu_addr, size) 0
679#define __free_from_contiguous(dev, page, size) do { } while (0)
680#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000681
682#endif /* CONFIG_MMU */
683
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100684static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
685 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100686{
Russell King04da5692009-11-19 15:54:45 +0000687 struct page *page;
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100688 page = __dma_alloc_buffer(dev, size, gfp);
689 if (!page)
690 return NULL;
691
692 *ret_page = page;
693 return page_address(page);
694}
695
696
697
698static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
699 gfp_t gfp, pgprot_t prot, const void *caller)
700{
701 u64 mask = get_coherent_dma_mask(dev);
702 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000703 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100704
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100705#ifdef CONFIG_DMA_API_DEBUG
706 u64 limit = (mask + 1) & ~mask;
707 if (limit && size >= limit) {
708 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
709 size, mask);
710 return NULL;
711 }
712#endif
713
714 if (!mask)
715 return NULL;
716
717 if (mask < 0xffffffffULL)
718 gfp |= GFP_DMA;
719
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100720 /*
721 * Following is a work-around (a.k.a. hack) to prevent pages
722 * with __GFP_COMP being passed to split_page() which cannot
723 * handle them. The real problem is that this flag probably
724 * should be 0 on ARM as it is not supported on this
725 * platform; see CONFIG_HUGETLBFS.
726 */
727 gfp &= ~(__GFP_COMP);
728
Marek Szyprowski1dc8f002012-02-29 14:45:28 +0100729 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000730 size = PAGE_ALIGN(size);
731
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100732 if (arch_is_coherent() || nommu())
733 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200734 else if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100735 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
736 else if (gfp & GFP_ATOMIC)
737 addr = __alloc_from_pool(dev, size, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000738 else
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100739 addr = __alloc_from_contiguous(dev, size, prot, &page);
Russell King31ebf942009-11-19 21:12:17 +0000740
741 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000742 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000743
744 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100745}
Russell King695ae0a2009-11-19 16:31:39 +0000746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * Allocate DMA-coherent memory space and return both the kernel remapped
749 * virtual and bus address for that space.
750 */
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100751void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle,
752 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400754 void *memory;
755
756 if (dma_alloc_from_coherent(dev, size, handle, &memory))
757 return memory;
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000760 pgprot_dmacoherent(pgprot_kernel),
761 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763EXPORT_SYMBOL(dma_alloc_coherent);
764
765/*
766 * Allocate a writecombining region, in much the same way as
767 * dma_alloc_coherent above.
768 */
769void *
Al Virof9e32142005-10-21 03:20:58 -0400770dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 return __dma_alloc(dev, size, handle, gfp,
Russell King45cd5292012-01-12 23:08:07 +0000773 pgprot_writecombine(pgprot_kernel),
774 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776EXPORT_SYMBOL(dma_alloc_writecombine);
777
778static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
779 void *cpu_addr, dma_addr_t dma_addr, size_t size)
780{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100781 int ret = -ENXIO;
782#ifdef CONFIG_MMU
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100783 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowskif504f8e2012-05-15 19:04:13 +0200784
785 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
786 return ret;
787
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100788 ret = remap_pfn_range(vma, vma->vm_start,
789 pfn + vma->vm_pgoff,
790 vma->vm_end - vma->vm_start,
791 vma->vm_page_prot);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100792#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 return ret;
795}
796
797int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
798 void *cpu_addr, dma_addr_t dma_addr, size_t size)
799{
Russell King26a26d32009-11-20 21:06:43 +0000800 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
802}
803EXPORT_SYMBOL(dma_mmap_coherent);
804
805int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
806 void *cpu_addr, dma_addr_t dma_addr, size_t size)
807{
808 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
809 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
810}
811EXPORT_SYMBOL(dma_mmap_writecombine);
812
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100815 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
817void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
818{
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100819 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000820
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400821 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
822 return;
823
Russell King3e82d012009-11-19 15:38:12 +0000824 size = PAGE_ALIGN(size);
825
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100826 if (arch_is_coherent() || nommu()) {
827 __dma_free_buffer(page, size);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200828 } else if (!IS_ENABLED(CONFIG_CMA)) {
Russell King695ae0a2009-11-19 16:31:39 +0000829 __dma_free_remap(cpu_addr, size);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100830 __dma_free_buffer(page, size);
831 } else {
832 if (__free_from_pool(cpu_addr, size))
833 return;
834 /*
835 * Non-atomic allocations cannot be freed with IRQs disabled
836 */
837 WARN_ON(irqs_disabled());
838 __free_from_contiguous(dev, page, size);
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841EXPORT_SYMBOL(dma_free_coherent);
842
Russell King65af1912009-11-24 17:53:33 +0000843static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000844 size_t size, enum dma_data_direction dir,
845 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000846{
847 /*
848 * A single sg entry may refer to multiple physically contiguous
849 * pages. But we still need to process highmem pages individually.
850 * If highmem is not configured then the bulk of this loop gets
851 * optimized out.
852 */
853 size_t left = size;
854 do {
855 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000856 void *vaddr;
857
858 if (PageHighMem(page)) {
859 if (len + offset > PAGE_SIZE) {
860 if (offset >= PAGE_SIZE) {
861 page += offset / PAGE_SIZE;
862 offset %= PAGE_SIZE;
863 }
864 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000865 }
Russell King93f1d622009-11-24 14:41:01 +0000866 vaddr = kmap_high_get(page);
867 if (vaddr) {
868 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000869 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000870 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100871 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500872 /* unmapped pages might still be cached */
873 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100874 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500875 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000876 }
877 } else {
878 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000879 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000880 }
Russell King65af1912009-11-24 17:53:33 +0000881 offset = 0;
882 page++;
883 left -= len;
884 } while (left);
885}
886
887void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
888 size_t size, enum dma_data_direction dir)
889{
Nicolas Pitre43377452009-03-12 22:52:09 -0400890 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400891
Russell Kinga9c91472009-11-26 16:19:58 +0000892 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400893
Russell King65af1912009-11-24 17:53:33 +0000894 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000895 if (dir == DMA_FROM_DEVICE) {
896 outer_inv_range(paddr, paddr + size);
897 } else {
898 outer_clean_range(paddr, paddr + size);
899 }
900 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400901}
Russell King4ea0d732009-11-24 16:27:17 +0000902
903void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
904 size_t size, enum dma_data_direction dir)
905{
Russell King2ffe2da2009-10-31 16:52:16 +0000906 unsigned long paddr = page_to_phys(page) + off;
907
908 /* FIXME: non-speculating: not required */
909 /* don't bother invalidating if DMA to device */
910 if (dir != DMA_TO_DEVICE)
911 outer_inv_range(paddr, paddr + size);
912
Russell Kinga9c91472009-11-26 16:19:58 +0000913 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100914
915 /*
916 * Mark the D-cache clean for this page to avoid extra flushing.
917 */
918 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
919 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000920}
Nicolas Pitre43377452009-03-12 22:52:09 -0400921
Russell Kingafd1a322008-09-25 16:30:57 +0100922/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100923 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100924 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
925 * @sg: list of buffers
926 * @nents: number of buffers to map
927 * @dir: DMA transfer direction
928 *
929 * Map a set of buffers described by scatterlist in streaming mode for DMA.
930 * This is the scatter-gather version of the dma_map_single interface.
931 * Here the scatter gather list elements are each tagged with the
932 * appropriate dma address and length. They are obtained via
933 * sg_dma_{address,length}.
934 *
935 * Device ownership issues as mentioned for dma_map_single are the same
936 * here.
937 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100938int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
939 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100940{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100941 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100942 struct scatterlist *s;
Russell King01135d92008-09-25 21:05:02 +0100943 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100944
945 for_each_sg(sg, s, nents, i) {
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100946 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
947 s->length, dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100948 if (dma_mapping_error(dev, s->dma_address))
949 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100950 }
Russell Kingafd1a322008-09-25 16:30:57 +0100951 return nents;
Russell King01135d92008-09-25 21:05:02 +0100952
953 bad_mapping:
954 for_each_sg(sg, s, i, j)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100955 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100956 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100957}
Russell Kingafd1a322008-09-25 16:30:57 +0100958
959/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100960 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100961 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
962 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100963 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100964 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
965 *
966 * Unmap a set of streaming mode DMA translations. Again, CPU access
967 * rules concerning calls here are the same as for dma_unmap_single().
968 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100969void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
970 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100971{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100972 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d92008-09-25 21:05:02 +0100973 struct scatterlist *s;
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100974
Russell King01135d92008-09-25 21:05:02 +0100975 int i;
976
977 for_each_sg(sg, s, nents, i)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100978 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100979}
Russell Kingafd1a322008-09-25 16:30:57 +0100980
981/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100982 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100983 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
984 * @sg: list of buffers
985 * @nents: number of buffers to map (returned from dma_map_sg)
986 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
987 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100988void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100989 int nents, enum dma_data_direction dir)
990{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100991 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100992 struct scatterlist *s;
993 int i;
994
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100995 for_each_sg(sg, s, nents, i)
996 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
997 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100998}
Russell Kingafd1a322008-09-25 16:30:57 +0100999
1000/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +01001001 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001002 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1003 * @sg: list of buffers
1004 * @nents: number of buffers to map (returned from dma_map_sg)
1005 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1006 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +01001007void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001008 int nents, enum dma_data_direction dir)
1009{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +01001010 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001011 struct scatterlist *s;
1012 int i;
1013
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +01001014 for_each_sg(sg, s, nents, i)
1015 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1016 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001017}
Russell King24056f52011-01-03 11:29:28 +00001018
Russell King022ae532011-07-08 21:26:59 +01001019/*
1020 * Return whether the given device DMA address mask can be supported
1021 * properly. For example, if your device can only drive the low 24-bits
1022 * during bus mastering, then you would pass 0x00ffffff as the mask
1023 * to this function.
1024 */
1025int dma_supported(struct device *dev, u64 mask)
1026{
1027 if (mask < (u64)arm_dma_limit)
1028 return 0;
1029 return 1;
1030}
1031EXPORT_SYMBOL(dma_supported);
1032
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +01001033static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +01001034{
1035 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1036 return -EIO;
1037
Russell King022ae532011-07-08 21:26:59 +01001038 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +01001039
1040 return 0;
1041}
Russell King022ae532011-07-08 21:26:59 +01001042
Russell King24056f52011-01-03 11:29:28 +00001043#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1044
1045static int __init dma_debug_do_init(void)
1046{
Russell King45cd5292012-01-12 23:08:07 +00001047#ifdef CONFIG_MMU
1048 arm_vmregion_create_proc("dma-mappings", &consistent_head);
1049#endif
Russell King24056f52011-01-03 11:29:28 +00001050 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1051 return 0;
1052}
1053fs_initcall(dma_debug_do_init);