blob: 41fef0011c41de1606f99d96ab2bd8a87bea35a7 [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>
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +020024#include <linux/iommu.h>
25#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010027#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040028#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/tlbflush.h>
Kevin Hilman37134cd2006-01-12 16:12:21 +000031#include <asm/sizes.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010032#include <asm/mach/arch.h>
Marek Szyprowskid4398df2011-12-29 13:09:51 +010033#include <asm/mach/map.h>
34#include <asm/system_info.h>
35#include <asm/dma-contiguous.h>
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +020036#include <asm/dma-iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Russell King022ae532011-07-08 21:26:59 +010038#include "mm.h"
39
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010040/*
41 * The DMA API is built upon the notion of "buffer ownership". A buffer
42 * is either exclusively owned by the CPU (and therefore may be accessed
43 * by it) or exclusively owned by the DMA device. These helper functions
44 * represent the transitions between these two ownership states.
45 *
46 * Note, however, that on later ARMs, this notion does not work due to
47 * speculative prefetches. We model our approach on the assumption that
48 * the CPU does do speculative prefetches, which means we clean caches
49 * before transfers and delay cache invalidation until transfer completion.
50 *
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010051 */
Marek Szyprowski53e207d2012-02-10 19:55:20 +010052static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010053 size_t, enum dma_data_direction);
Marek Szyprowski53e207d2012-02-10 19:55:20 +010054static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski8b59d2a2012-02-10 19:55:20 +010055 size_t, enum dma_data_direction);
56
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010057/**
58 * arm_dma_map_page - map a portion of a page for streaming DMA
59 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
60 * @page: page that buffer resides in
61 * @offset: offset into page for start of buffer
62 * @size: size of buffer to map
63 * @dir: DMA transfer direction
64 *
65 * Ensure that any data held in the cache is appropriately discarded
66 * or written back.
67 *
68 * The device owns this memory once this call has completed. The CPU
69 * can regain ownership by calling dma_unmap_page().
70 */
Marek Szyprowski53e207d2012-02-10 19:55:20 +010071static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010072 unsigned long offset, size_t size, enum dma_data_direction dir,
73 struct dma_attrs *attrs)
74{
Marek Szyprowski53e207d2012-02-10 19:55:20 +010075 if (!arch_is_coherent())
76 __dma_page_cpu_to_dev(page, offset, size, dir);
77 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010078}
79
80/**
81 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
82 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
83 * @handle: DMA address of buffer
84 * @size: size of buffer (same as passed to dma_map_page)
85 * @dir: DMA transfer direction (same as passed to dma_map_page)
86 *
87 * Unmap a page streaming mode DMA translation. The handle and size
88 * must match what was provided in the previous dma_map_page() call.
89 * All other usages are undefined.
90 *
91 * After this call, reads by the CPU to the buffer are guaranteed to see
92 * whatever the device wrote there.
93 */
Marek Szyprowski53e207d2012-02-10 19:55:20 +010094static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010095 size_t size, enum dma_data_direction dir,
96 struct dma_attrs *attrs)
97{
Marek Szyprowski53e207d2012-02-10 19:55:20 +010098 if (!arch_is_coherent())
99 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
100 handle & ~PAGE_MASK, size, dir);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100101}
102
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100103static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100104 dma_addr_t handle, size_t size, enum dma_data_direction dir)
105{
106 unsigned int offset = handle & (PAGE_SIZE - 1);
107 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100108 if (!arch_is_coherent())
109 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100110}
111
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100112static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100113 dma_addr_t handle, size_t size, enum dma_data_direction dir)
114{
115 unsigned int offset = handle & (PAGE_SIZE - 1);
116 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100117 if (!arch_is_coherent())
118 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100119}
120
121static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
122
123struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200124 .alloc = arm_dma_alloc,
125 .free = arm_dma_free,
126 .mmap = arm_dma_mmap,
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100127 .map_page = arm_dma_map_page,
128 .unmap_page = arm_dma_unmap_page,
129 .map_sg = arm_dma_map_sg,
130 .unmap_sg = arm_dma_unmap_sg,
131 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
132 .sync_single_for_device = arm_dma_sync_single_for_device,
133 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
134 .sync_sg_for_device = arm_dma_sync_sg_for_device,
135 .set_dma_mask = arm_dma_set_mask,
136};
137EXPORT_SYMBOL(arm_dma_ops);
138
Catalin Marinasab6494f2009-07-24 12:35:02 +0100139static u64 get_coherent_dma_mask(struct device *dev)
140{
Russell King022ae532011-07-08 21:26:59 +0100141 u64 mask = (u64)arm_dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Catalin Marinasab6494f2009-07-24 12:35:02 +0100143 if (dev) {
144 mask = dev->coherent_dma_mask;
145
146 /*
147 * Sanity check the DMA mask - it must be non-zero, and
148 * must be able to be satisfied by a DMA allocation.
149 */
150 if (mask == 0) {
151 dev_warn(dev, "coherent DMA mask is unset\n");
152 return 0;
153 }
154
Russell King022ae532011-07-08 21:26:59 +0100155 if ((~mask) & (u64)arm_dma_limit) {
Catalin Marinasab6494f2009-07-24 12:35:02 +0100156 dev_warn(dev, "coherent DMA mask %#llx is smaller "
157 "than system GFP_DMA mask %#llx\n",
Russell King022ae532011-07-08 21:26:59 +0100158 mask, (u64)arm_dma_limit);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100159 return 0;
160 }
161 }
162
163 return mask;
164}
165
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100166static void __dma_clear_buffer(struct page *page, size_t size)
167{
168 void *ptr;
169 /*
170 * Ensure that the allocated pages are zeroed, and that any data
171 * lurking in the kernel direct-mapped region is invalidated.
172 */
173 ptr = page_address(page);
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +0200174 if (ptr) {
175 memset(ptr, 0, size);
176 dmac_flush_range(ptr, ptr + size);
177 outer_flush_range(__pa(ptr), __pa(ptr) + size);
178 }
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100179}
180
Russell King7a9a32a2009-11-19 15:31:07 +0000181/*
182 * Allocate a DMA buffer for 'dev' of size 'size' using the
183 * specified gfp mask. Note that 'size' must be page aligned.
184 */
185static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
186{
187 unsigned long order = get_order(size);
188 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000189
190 page = alloc_pages(gfp, order);
191 if (!page)
192 return NULL;
193
194 /*
195 * Now split the huge page and free the excess pages
196 */
197 split_page(page, order);
198 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
199 __free_page(p);
200
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100201 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000202
203 return page;
204}
205
206/*
207 * Free a DMA buffer. 'size' must be page aligned.
208 */
209static void __dma_free_buffer(struct page *page, size_t size)
210{
211 struct page *e = page + (size >> PAGE_SHIFT);
212
213 while (page < e) {
214 __free_page(page);
215 page++;
216 }
217}
218
Catalin Marinasab6494f2009-07-24 12:35:02 +0100219#ifdef CONFIG_MMU
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100220
Jon Medhurst99d17172011-08-02 17:28:27 +0100221#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT)
Linus Torvalds1fdb24e2011-10-28 12:02:27 -0700222#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT)
Catalin Marinasa5e9d382010-06-21 15:09:06 +0100223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
Kevin Hilman37134cd2006-01-12 16:12:21 +0000225 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Jon Medhurst99d17172011-08-02 17:28:27 +0100227static pte_t **consistent_pte;
228
Steve Mucklef132c6c2012-06-06 18:30:57 -0700229#define DEFAULT_CONSISTENT_DMA_SIZE (7*SZ_2M)
Jon Medhurst99d17172011-08-02 17:28:27 +0100230
Sachin Kamat7af10a32012-06-06 08:03:35 +0200231static unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE;
Jon Medhurst99d17172011-08-02 17:28:27 +0100232
233void __init init_consistent_dma_size(unsigned long size)
234{
235 unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M);
236
237 BUG_ON(consistent_pte); /* Check we're called before DMA region init */
238 BUG_ON(base < VMALLOC_END);
239
240 /* Grow region to accommodate specified size */
241 if (base < consistent_base)
242 consistent_base = base;
243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Russell King13ccf3a2009-11-19 15:07:04 +0000245#include "vmregion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Russell King13ccf3a2009-11-19 15:07:04 +0000247static struct arm_vmregion_head consistent_head = {
248 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 .vm_end = CONSISTENT_END,
251};
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#ifdef CONFIG_HUGETLB_PAGE
254#error ARM Coherent DMA allocator does not (yet) support huge TLB
255#endif
256
Russell King88c58f32009-11-19 16:46:02 +0000257/*
258 * Initialise the consistent memory allocation.
259 */
260static int __init consistent_init(void)
261{
262 int ret = 0;
263 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000264 pud_t *pud;
Russell King88c58f32009-11-19 16:46:02 +0000265 pmd_t *pmd;
266 pte_t *pte;
267 int i = 0;
Jon Medhurst99d17172011-08-02 17:28:27 +0100268 unsigned long base = consistent_base;
Catalin Marinas53cbcbc2011-11-17 13:11:21 +0100269 unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT;
Jon Medhurst99d17172011-08-02 17:28:27 +0100270
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200271 if (IS_ENABLED(CONFIG_CMA) && !IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100272 return 0;
273
Jon Medhurst99d17172011-08-02 17:28:27 +0100274 consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL);
275 if (!consistent_pte) {
276 pr_err("%s: no memory\n", __func__);
277 return -ENOMEM;
278 }
279
280 pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END);
281 consistent_head.vm_start = base;
Russell King88c58f32009-11-19 16:46:02 +0000282
283 do {
284 pgd = pgd_offset(&init_mm, base);
Russell King516295e2010-11-21 16:27:49 +0000285
286 pud = pud_alloc(&init_mm, pgd, base);
287 if (!pud) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100288 pr_err("%s: no pud tables\n", __func__);
Russell King516295e2010-11-21 16:27:49 +0000289 ret = -ENOMEM;
290 break;
291 }
292
293 pmd = pmd_alloc(&init_mm, pud, base);
Russell King88c58f32009-11-19 16:46:02 +0000294 if (!pmd) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100295 pr_err("%s: no pmd tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000296 ret = -ENOMEM;
297 break;
298 }
299 WARN_ON(!pmd_none(*pmd));
300
301 pte = pte_alloc_kernel(pmd, base);
302 if (!pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100303 pr_err("%s: no pte tables\n", __func__);
Russell King88c58f32009-11-19 16:46:02 +0000304 ret = -ENOMEM;
305 break;
306 }
307
308 consistent_pte[i++] = pte;
Catalin Marinase73fc882011-08-23 14:07:23 +0100309 base += PMD_SIZE;
Russell King88c58f32009-11-19 16:46:02 +0000310 } while (base < CONSISTENT_END);
311
312 return ret;
313}
Russell King88c58f32009-11-19 16:46:02 +0000314core_initcall(consistent_init);
315
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100316static void *__alloc_from_contiguous(struct device *dev, size_t size,
Laura Abbott88d97db2012-10-29 13:38:25 -0700317 pgprot_t prot, struct page **ret_page,
318 bool no_kernel_mapping);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100319
320static struct arm_vmregion_head coherent_head = {
321 .vm_lock = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock),
322 .vm_list = LIST_HEAD_INIT(coherent_head.vm_list),
323};
324
Sachin Kamat7af10a32012-06-06 08:03:35 +0200325static size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8;
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100326
327static int __init early_coherent_pool(char *p)
328{
329 coherent_pool_size = memparse(p, &p);
330 return 0;
331}
332early_param("coherent_pool", early_coherent_pool);
333
334/*
335 * Initialise the coherent pool for atomic allocations.
336 */
337static int __init coherent_init(void)
338{
339 pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
340 size_t size = coherent_pool_size;
341 struct page *page;
342 void *ptr;
343
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200344 if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100345 return 0;
346
Laura Abbott88d97db2012-10-29 13:38:25 -0700347 ptr = __alloc_from_contiguous(NULL, size, prot, &page, false);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100348 if (ptr) {
349 coherent_head.vm_start = (unsigned long) ptr;
350 coherent_head.vm_end = (unsigned long) ptr + size;
351 printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n",
352 (unsigned)size / 1024);
353 return 0;
354 }
355 printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
356 (unsigned)size / 1024);
357 return -ENOMEM;
358}
359/*
360 * CMA is activated by core_initcall, so we must be called after it.
361 */
362postcore_initcall(coherent_init);
363
364struct dma_contig_early_reserve {
365 phys_addr_t base;
366 unsigned long size;
367};
368
369static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
370
371static int dma_mmu_remap_num __initdata;
372
373void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
374{
375 dma_mmu_remap[dma_mmu_remap_num].base = base;
376 dma_mmu_remap[dma_mmu_remap_num].size = size;
377 dma_mmu_remap_num++;
378}
379
380void __init dma_contiguous_remap(void)
381{
382 int i;
383 for (i = 0; i < dma_mmu_remap_num; i++) {
384 phys_addr_t start = dma_mmu_remap[i].base;
385 phys_addr_t end = start + dma_mmu_remap[i].size;
386 struct map_desc map;
387 unsigned long addr;
388
389 if (end > arm_lowmem_limit)
390 end = arm_lowmem_limit;
391 if (start >= end)
Chris Brand9b86b7c2012-08-07 14:01:14 +0200392 continue;
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100393
394 map.pfn = __phys_to_pfn(start);
395 map.virtual = __phys_to_virt(start);
396 map.length = end - start;
397 map.type = MT_MEMORY_DMA_READY;
398
399 /*
400 * Clear previous low-memory mapping
401 */
402 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov8b5439b2012-05-14 13:49:56 -0400403 addr += PMD_SIZE)
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100404 pmd_clear(pmd_off_k(addr));
405
406 iotable_init(&map, 1);
407 }
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static void *
Russell King45cd5292012-01-12 23:08:07 +0000411__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
412 const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Russell King13ccf3a2009-11-19 15:07:04 +0000414 struct arm_vmregion *c;
Russell King5bc23d32010-07-25 08:57:02 +0100415 size_t align;
416 int bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jon Medhurst99d17172011-08-02 17:28:27 +0100418 if (!consistent_pte) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100419 pr_err("%s: not initialised\n", __func__);
Russell Kingebd7a842009-11-19 20:58:31 +0000420 dump_stack();
Russell Kingebd7a842009-11-19 20:58:31 +0000421 return NULL;
422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
Russell King5bc23d32010-07-25 08:57:02 +0100425 * Align the virtual region allocation - maximum alignment is
426 * a section size, minimum is a page size. This helps reduce
427 * fragmentation of the DMA space, and also prevents allocations
428 * smaller than a section from crossing a section boundary.
429 */
Russell Kingc947f692010-11-03 16:00:15 +0000430 bit = fls(size - 1);
Russell King5bc23d32010-07-25 08:57:02 +0100431 if (bit > SECTION_SHIFT)
432 bit = SECTION_SHIFT;
433 align = 1 << bit;
434
435 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * Allocate a virtual address in the consistent mapping region.
437 */
Russell King5bc23d32010-07-25 08:57:02 +0100438 c = arm_vmregion_alloc(&consistent_head, align, size,
Russell King45cd5292012-01-12 23:08:07 +0000439 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (c) {
Kevin Hilman37134cd2006-01-12 16:12:21 +0000441 pte_t *pte;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000442 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
443 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Kevin Hilman37134cd2006-01-12 16:12:21 +0000445 pte = consistent_pte[idx] + off;
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +0200446 c->priv = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 do {
449 BUG_ON(!pte_none(*pte));
450
Russell Kingad1ae2f2006-12-13 14:34:43 +0000451 set_pte_ext(pte, mk_pte(page, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 page++;
453 pte++;
Kevin Hilman37134cd2006-01-12 16:12:21 +0000454 off++;
455 if (off >= PTRS_PER_PTE) {
456 off = 0;
457 pte = consistent_pte[++idx];
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } while (size -= PAGE_SIZE);
460
Russell King2be23c42010-09-08 16:27:56 +0100461 dsb();
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return (void *)c->vm_start;
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return NULL;
466}
Russell King695ae0a2009-11-19 16:31:39 +0000467
468static void __dma_free_remap(void *cpu_addr, size_t size)
469{
470 struct arm_vmregion *c;
471 unsigned long addr;
472 pte_t *ptep;
473 int idx;
474 u32 off;
475
476 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
477 if (!c) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100478 pr_err("%s: trying to free invalid coherent area: %p\n",
Russell King695ae0a2009-11-19 16:31:39 +0000479 __func__, cpu_addr);
480 dump_stack();
481 return;
482 }
483
484 if ((c->vm_end - c->vm_start) != size) {
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100485 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
Russell King695ae0a2009-11-19 16:31:39 +0000486 __func__, c->vm_end - c->vm_start, size);
487 dump_stack();
488 size = c->vm_end - c->vm_start;
489 }
490
491 idx = CONSISTENT_PTE_INDEX(c->vm_start);
492 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
493 ptep = consistent_pte[idx] + off;
494 addr = c->vm_start;
495 do {
496 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
Russell King695ae0a2009-11-19 16:31:39 +0000497
498 ptep++;
499 addr += PAGE_SIZE;
500 off++;
501 if (off >= PTRS_PER_PTE) {
502 off = 0;
503 ptep = consistent_pte[++idx];
504 }
505
Russell Kingacaac252009-11-20 18:19:52 +0000506 if (pte_none(pte) || !pte_present(pte))
Marek Szyprowskie2a8e412012-02-28 10:19:14 +0100507 pr_crit("%s: bad page in kernel page table\n",
508 __func__);
Russell King695ae0a2009-11-19 16:31:39 +0000509 } while (size -= PAGE_SIZE);
510
511 flush_tlb_kernel_range(c->vm_start, c->vm_end);
512
513 arm_vmregion_free(&consistent_head, c);
514}
515
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100516static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
517 void *data)
518{
519 struct page *page = virt_to_page(addr);
520 pgprot_t prot = *(pgprot_t *)data;
521
522 set_pte_ext(pte, mk_pte(page, prot), 0);
523 return 0;
524}
525
Laura Abbott88d97db2012-10-29 13:38:25 -0700526static int __dma_clear_pte(pte_t *pte, pgtable_t token, unsigned long addr,
527 void *data)
528{
529 pte_clear(&init_mm, addr, pte);
530 return 0;
531}
532
533static void __dma_remap(struct page *page, size_t size, pgprot_t prot,
534 bool no_kernel_map)
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100535{
536 unsigned long start = (unsigned long) page_address(page);
537 unsigned end = start + size;
Laura Abbott88d97db2012-10-29 13:38:25 -0700538 int (*func)(pte_t *pte, pgtable_t token, unsigned long addr,
539 void *data);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100540
Laura Abbott88d97db2012-10-29 13:38:25 -0700541 if (no_kernel_map)
542 func = __dma_clear_pte;
543 else
544 func = __dma_update_pte;
545
546 apply_to_page_range(&init_mm, start, size, func, &prot);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100547 dsb();
548 flush_tlb_kernel_range(start, end);
549}
550
551static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
552 pgprot_t prot, struct page **ret_page,
553 const void *caller)
554{
555 struct page *page;
556 void *ptr;
557 page = __dma_alloc_buffer(dev, size, gfp);
558 if (!page)
559 return NULL;
560
561 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
562 if (!ptr) {
563 __dma_free_buffer(page, size);
564 return NULL;
565 }
566
567 *ret_page = page;
568 return ptr;
569}
570
571static void *__alloc_from_pool(struct device *dev, size_t size,
572 struct page **ret_page, const void *caller)
573{
574 struct arm_vmregion *c;
575 size_t align;
576
577 if (!coherent_head.vm_start) {
578 printk(KERN_ERR "%s: coherent pool not initialised!\n",
579 __func__);
580 dump_stack();
581 return NULL;
582 }
583
584 /*
585 * Align the region allocation - allocations from pool are rather
586 * small, so align them to their order in pages, minimum is a page
587 * size. This helps reduce fragmentation of the DMA space.
588 */
589 align = PAGE_SIZE << get_order(size);
590 c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller);
591 if (c) {
592 void *ptr = (void *)c->vm_start;
593 struct page *page = virt_to_page(ptr);
594 *ret_page = page;
595 return ptr;
596 }
597 return NULL;
598}
599
600static int __free_from_pool(void *cpu_addr, size_t size)
601{
602 unsigned long start = (unsigned long)cpu_addr;
603 unsigned long end = start + size;
604 struct arm_vmregion *c;
605
606 if (start < coherent_head.vm_start || end > coherent_head.vm_end)
607 return 0;
608
609 c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start);
610
611 if ((c->vm_end - c->vm_start) != size) {
612 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
613 __func__, c->vm_end - c->vm_start, size);
614 dump_stack();
615 size = c->vm_end - c->vm_start;
616 }
617
618 arm_vmregion_free(&coherent_head, c);
619 return 1;
620}
621
622static void *__alloc_from_contiguous(struct device *dev, size_t size,
Laura Abbott88d97db2012-10-29 13:38:25 -0700623 pgprot_t prot, struct page **ret_page,
624 bool no_kernel_mapping)
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100625{
626 unsigned long order = get_order(size);
627 size_t count = size >> PAGE_SHIFT;
628 struct page *page;
629
630 page = dma_alloc_from_contiguous(dev, count, order);
631 if (!page)
632 return NULL;
633
634 __dma_clear_buffer(page, size);
Laura Abbott88d97db2012-10-29 13:38:25 -0700635 __dma_remap(page, size, prot, no_kernel_mapping);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100636
637 *ret_page = page;
638 return page_address(page);
639}
640
641static void __free_from_contiguous(struct device *dev, struct page *page,
642 size_t size)
643{
Laura Abbott88d97db2012-10-29 13:38:25 -0700644 __dma_remap(page, size, pgprot_kernel, false);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100645 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
646}
647
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200648static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
649{
Laura Abbott92b9ec92012-10-29 11:54:38 -0700650 if (dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs))
651 prot = pgprot_writecombine(prot);
652 else if (dma_get_attr(DMA_ATTR_STRONGLY_ORDERED, attrs))
653 prot = pgprot_stronglyordered(prot);
654 /* if non-consistent just pass back what was given */
655 else if (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs))
656 prot = pgprot_dmacoherent(prot);
657
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200658 return prot;
659}
660
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100661#define nommu() 0
662
Catalin Marinasab6494f2009-07-24 12:35:02 +0100663#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000664
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100665#define nommu() 1
666
667#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
668#define __alloc_from_pool(dev, size, ret_page, c) NULL
Laura Abbott88d97db2012-10-29 13:38:25 -0700669#define __alloc_from_contiguous(dev, size, prot, ret, w) NULL
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100670#define __free_from_pool(cpu_addr, size) 0
671#define __free_from_contiguous(dev, page, size) do { } while (0)
672#define __dma_free_remap(cpu_addr, size) do { } while (0)
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200673#define __get_dma_pgprot(attrs, prot) __pgprot(0)
Russell King31ebf942009-11-19 21:12:17 +0000674
675#endif /* CONFIG_MMU */
676
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100677static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
678 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100679{
Russell King04da5692009-11-19 15:54:45 +0000680 struct page *page;
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100681 page = __dma_alloc_buffer(dev, size, gfp);
682 if (!page)
683 return NULL;
684
685 *ret_page = page;
686 return page_address(page);
687}
688
689
690
691static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Laura Abbott88d97db2012-10-29 13:38:25 -0700692 gfp_t gfp, pgprot_t prot, const void *caller,
693 bool no_kernel_mapping)
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100694{
695 u64 mask = get_coherent_dma_mask(dev);
696 struct page *page;
Russell King31ebf942009-11-19 21:12:17 +0000697 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100698
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100699#ifdef CONFIG_DMA_API_DEBUG
700 u64 limit = (mask + 1) & ~mask;
701 if (limit && size >= limit) {
702 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
703 size, mask);
704 return NULL;
705 }
706#endif
707
708 if (!mask)
709 return NULL;
710
711 if (mask < 0xffffffffULL)
712 gfp |= GFP_DMA;
713
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100714 /*
715 * Following is a work-around (a.k.a. hack) to prevent pages
716 * with __GFP_COMP being passed to split_page() which cannot
717 * handle them. The real problem is that this flag probably
718 * should be 0 on ARM as it is not supported on this
719 * platform; see CONFIG_HUGETLBFS.
720 */
721 gfp &= ~(__GFP_COMP);
722
Marek Szyprowski1dc8f002012-02-29 14:45:28 +0100723 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000724 size = PAGE_ALIGN(size);
725
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100726 if (arch_is_coherent() || nommu())
727 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200728 else if (!IS_ENABLED(CONFIG_CMA))
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100729 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
730 else if (gfp & GFP_ATOMIC)
731 addr = __alloc_from_pool(dev, size, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000732 else
Laura Abbott88d97db2012-10-29 13:38:25 -0700733 addr = __alloc_from_contiguous(dev, size, prot, &page,
734 no_kernel_mapping);
Russell King31ebf942009-11-19 21:12:17 +0000735
736 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000737 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000738
739 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100740}
Russell King695ae0a2009-11-19 16:31:39 +0000741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/*
743 * Allocate DMA-coherent memory space and return both the kernel remapped
744 * virtual and bus address for that space.
745 */
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200746void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
747 gfp_t gfp, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200749 pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400750 void *memory;
Laura Abbott88d97db2012-10-29 13:38:25 -0700751 bool no_kernel_mapping = dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING,
752 attrs);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400753
754 if (dma_alloc_from_coherent(dev, size, handle, &memory))
755 return memory;
756
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200757 return __dma_alloc(dev, size, handle, gfp, prot,
Laura Abbott88d97db2012-10-29 13:38:25 -0700758 __builtin_return_address(0), no_kernel_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761/*
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200762 * Create userspace mapping for the DMA-coherent memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200764int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
765 void *cpu_addr, dma_addr_t dma_addr, size_t size,
766 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100768 int ret = -ENXIO;
769#ifdef CONFIG_MMU
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100770 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200771 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Marek Szyprowskif504f8e2012-05-15 19:04:13 +0200772
773 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
774 return ret;
775
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100776 ret = remap_pfn_range(vma, vma->vm_start,
777 pfn + vma->vm_pgoff,
778 vma->vm_end - vma->vm_start,
779 vma->vm_page_prot);
Catalin Marinasab6494f2009-07-24 12:35:02 +0100780#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return ret;
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/*
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100786 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
Marek Szyprowskif8f9d072012-05-16 18:31:23 +0200788void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
789 dma_addr_t handle, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100791 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000792
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400793 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
794 return;
795
Russell King3e82d012009-11-19 15:38:12 +0000796 size = PAGE_ALIGN(size);
797
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100798 if (arch_is_coherent() || nommu()) {
799 __dma_free_buffer(page, size);
Marek Szyprowski5ee6b062012-05-30 10:48:29 +0200800 } else if (!IS_ENABLED(CONFIG_CMA)) {
Russell King695ae0a2009-11-19 16:31:39 +0000801 __dma_free_remap(cpu_addr, size);
Marek Szyprowskid4398df2011-12-29 13:09:51 +0100802 __dma_free_buffer(page, size);
803 } else {
804 if (__free_from_pool(cpu_addr, size))
805 return;
806 /*
807 * Non-atomic allocations cannot be freed with IRQs disabled
808 */
809 WARN_ON(irqs_disabled());
810 __free_from_contiguous(dev, page, size);
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Russell King65af1912009-11-24 17:53:33 +0000814static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000815 size_t size, enum dma_data_direction dir,
816 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000817{
818 /*
819 * A single sg entry may refer to multiple physically contiguous
820 * pages. But we still need to process highmem pages individually.
821 * If highmem is not configured then the bulk of this loop gets
822 * optimized out.
823 */
824 size_t left = size;
825 do {
826 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000827 void *vaddr;
828
829 if (PageHighMem(page)) {
830 if (len + offset > PAGE_SIZE) {
831 if (offset >= PAGE_SIZE) {
832 page += offset / PAGE_SIZE;
833 offset %= PAGE_SIZE;
834 }
835 len = PAGE_SIZE - offset;
Russell King65af1912009-11-24 17:53:33 +0000836 }
Russell King93f1d622009-11-24 14:41:01 +0000837 vaddr = kmap_high_get(page);
838 if (vaddr) {
839 vaddr += offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000840 op(vaddr, len, dir);
Russell King93f1d622009-11-24 14:41:01 +0000841 kunmap_high(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100842 } else if (cache_is_vipt()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500843 /* unmapped pages might still be cached */
844 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100845 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500846 kunmap_atomic(vaddr);
Russell King93f1d622009-11-24 14:41:01 +0000847 }
848 } else {
849 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000850 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000851 }
Russell King65af1912009-11-24 17:53:33 +0000852 offset = 0;
853 page++;
854 left -= len;
855 } while (left);
856}
857
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100858/*
859 * Make an area consistent for devices.
860 * Note: Drivers should NOT use this function directly, as it will break
861 * platforms with CONFIG_DMABOUNCE.
862 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
863 */
864static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000865 size_t size, enum dma_data_direction dir)
866{
Nicolas Pitre43377452009-03-12 22:52:09 -0400867 unsigned long paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400868
Russell Kinga9c91472009-11-26 16:19:58 +0000869 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400870
Russell King65af1912009-11-24 17:53:33 +0000871 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000872 if (dir == DMA_FROM_DEVICE) {
873 outer_inv_range(paddr, paddr + size);
874 } else {
875 outer_clean_range(paddr, paddr + size);
876 }
877 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400878}
Russell King4ea0d732009-11-24 16:27:17 +0000879
Marek Szyprowski53e207d2012-02-10 19:55:20 +0100880static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000881 size_t size, enum dma_data_direction dir)
882{
Russell King2ffe2da2009-10-31 16:52:16 +0000883 unsigned long paddr = page_to_phys(page) + off;
884
885 /* FIXME: non-speculating: not required */
886 /* don't bother invalidating if DMA to device */
887 if (dir != DMA_TO_DEVICE)
888 outer_inv_range(paddr, paddr + size);
889
Russell Kinga9c91472009-11-26 16:19:58 +0000890 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
Catalin Marinasc0177802010-09-13 15:57:36 +0100891
892 /*
893 * Mark the D-cache clean for this page to avoid extra flushing.
894 */
895 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE)
896 set_bit(PG_dcache_clean, &page->flags);
Russell King4ea0d732009-11-24 16:27:17 +0000897}
Nicolas Pitre43377452009-03-12 22:52:09 -0400898
Russell Kingafd1a322008-09-25 16:30:57 +0100899/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100900 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100901 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
902 * @sg: list of buffers
903 * @nents: number of buffers to map
904 * @dir: DMA transfer direction
905 *
906 * Map a set of buffers described by scatterlist in streaming mode for DMA.
907 * This is the scatter-gather version of the dma_map_single interface.
908 * Here the scatter gather list elements are each tagged with the
909 * appropriate dma address and length. They are obtained via
910 * sg_dma_{address,length}.
911 *
912 * Device ownership issues as mentioned for dma_map_single are the same
913 * here.
914 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100915int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
916 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100917{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100918 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100919 struct scatterlist *s;
Russell King01135d92008-09-25 21:05:02 +0100920 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100921
922 for_each_sg(sg, s, nents, i) {
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +0200923#ifdef CONFIG_NEED_SG_DMA_LENGTH
924 s->dma_length = s->length;
925#endif
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100926 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
927 s->length, dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100928 if (dma_mapping_error(dev, s->dma_address))
929 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100930 }
Russell Kingafd1a322008-09-25 16:30:57 +0100931 return nents;
Russell King01135d92008-09-25 21:05:02 +0100932
933 bad_mapping:
934 for_each_sg(sg, s, i, j)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100935 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d92008-09-25 21:05:02 +0100936 return 0;
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_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
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
Linus Walleij0adfca62011-01-12 18:50:37 +0100943 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100944 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
945 *
946 * Unmap a set of streaming mode DMA translations. Again, CPU access
947 * rules concerning calls here are the same as for dma_unmap_single().
948 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100949void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
950 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100951{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100952 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d92008-09-25 21:05:02 +0100953 struct scatterlist *s;
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100954
Russell King01135d92008-09-25 21:05:02 +0100955 int i;
956
957 for_each_sg(sg, s, nents, i)
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100958 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100959}
Russell Kingafd1a322008-09-25 16:30:57 +0100960
961/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100962 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100963 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
964 * @sg: list of buffers
965 * @nents: number of buffers to map (returned from dma_map_sg)
966 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
967 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100968void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100969 int nents, enum dma_data_direction dir)
970{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100971 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100972 struct scatterlist *s;
973 int i;
974
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100975 for_each_sg(sg, s, nents, i)
976 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
977 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100978}
Russell Kingafd1a322008-09-25 16:30:57 +0100979
980/**
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100981 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100982 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
983 * @sg: list of buffers
984 * @nents: number of buffers to map (returned from dma_map_sg)
985 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
986 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100987void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100988 int nents, enum dma_data_direction dir)
989{
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100990 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100991 struct scatterlist *s;
992 int i;
993
Marek Szyprowski36dbd4c2012-02-10 19:55:20 +0100994 for_each_sg(sg, s, nents, i)
995 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
996 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100997}
Russell King24056f52011-01-03 11:29:28 +0000998
Russell King022ae532011-07-08 21:26:59 +0100999/*
1000 * Return whether the given device DMA address mask can be supported
1001 * properly. For example, if your device can only drive the low 24-bits
1002 * during bus mastering, then you would pass 0x00ffffff as the mask
1003 * to this function.
1004 */
1005int dma_supported(struct device *dev, u64 mask)
1006{
1007 if (mask < (u64)arm_dma_limit)
1008 return 0;
1009 return 1;
1010}
1011EXPORT_SYMBOL(dma_supported);
1012
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +01001013static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +01001014{
1015 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1016 return -EIO;
1017
Russell King022ae532011-07-08 21:26:59 +01001018 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +01001019
1020 return 0;
1021}
Russell King022ae532011-07-08 21:26:59 +01001022
Russell King24056f52011-01-03 11:29:28 +00001023#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1024
1025static int __init dma_debug_do_init(void)
1026{
Russell King45cd5292012-01-12 23:08:07 +00001027#ifdef CONFIG_MMU
1028 arm_vmregion_create_proc("dma-mappings", &consistent_head);
1029#endif
Russell King24056f52011-01-03 11:29:28 +00001030 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1031 return 0;
1032}
1033fs_initcall(dma_debug_do_init);
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +02001034
1035#ifdef CONFIG_ARM_DMA_USE_IOMMU
1036
1037/* IOMMU */
1038
1039static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1040 size_t size)
1041{
1042 unsigned int order = get_order(size);
1043 unsigned int align = 0;
1044 unsigned int count, start;
1045 unsigned long flags;
1046
1047 count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
1048 (1 << mapping->order) - 1) >> mapping->order;
1049
1050 if (order > mapping->order)
1051 align = (1 << (order - mapping->order)) - 1;
1052
1053 spin_lock_irqsave(&mapping->lock, flags);
1054 start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
1055 count, align);
1056 if (start > mapping->bits) {
1057 spin_unlock_irqrestore(&mapping->lock, flags);
1058 return DMA_ERROR_CODE;
1059 }
1060
1061 bitmap_set(mapping->bitmap, start, count);
1062 spin_unlock_irqrestore(&mapping->lock, flags);
1063
1064 return mapping->base + (start << (mapping->order + PAGE_SHIFT));
1065}
1066
1067static inline void __free_iova(struct dma_iommu_mapping *mapping,
1068 dma_addr_t addr, size_t size)
1069{
1070 unsigned int start = (addr - mapping->base) >>
1071 (mapping->order + PAGE_SHIFT);
1072 unsigned int count = ((size >> PAGE_SHIFT) +
1073 (1 << mapping->order) - 1) >> mapping->order;
1074 unsigned long flags;
1075
1076 spin_lock_irqsave(&mapping->lock, flags);
1077 bitmap_clear(mapping->bitmap, start, count);
1078 spin_unlock_irqrestore(&mapping->lock, flags);
1079}
1080
1081static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
1082{
1083 struct page **pages;
1084 int count = size >> PAGE_SHIFT;
1085 int array_size = count * sizeof(struct page *);
1086 int i = 0;
1087
1088 if (array_size <= PAGE_SIZE)
1089 pages = kzalloc(array_size, gfp);
1090 else
1091 pages = vzalloc(array_size);
1092 if (!pages)
1093 return NULL;
1094
1095 while (count) {
Marek Szyprowski51b57942012-06-21 11:48:11 +02001096 int j, order = __fls(count);
Marek Szyprowski2bbb1b92012-05-16 15:48:21 +02001097
1098 pages[i] = alloc_pages(gfp | __GFP_NOWARN, order);
1099 while (!pages[i] && order)
1100 pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order);
1101 if (!pages[i])
1102 goto error;
1103
1104 if (order)
1105 split_page(pages[i], order);
1106 j = 1 << order;
1107 while (--j)
1108 pages[i + j] = pages[i] + j;
1109
1110 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
1111 i += 1 << order;
1112 count -= 1 << order;
1113 }
1114
1115 return pages;
1116error:
1117 while (--i)
1118 if (pages[i])
1119 __free_pages(pages[i], 0);
1120 if (array_size < PAGE_SIZE)
1121 kfree(pages);
1122 else
1123 vfree(pages);
1124 return NULL;
1125}
1126
1127static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size)
1128{
1129 int count = size >> PAGE_SHIFT;
1130 int array_size = count * sizeof(struct page *);
1131 int i;
1132 for (i = 0; i < count; i++)
1133 if (pages[i])
1134 __free_pages(pages[i], 0);
1135 if (array_size < PAGE_SIZE)
1136 kfree(pages);
1137 else
1138 vfree(pages);
1139 return 0;
1140}
1141
1142/*
1143 * Create a CPU mapping for a specified pages
1144 */
1145static void *
1146__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot)
1147{
1148 struct arm_vmregion *c;
1149 size_t align;
1150 size_t count = size >> PAGE_SHIFT;
1151 int bit;
1152
1153 if (!consistent_pte[0]) {
1154 pr_err("%s: not initialised\n", __func__);
1155 dump_stack();
1156 return NULL;
1157 }
1158
1159 /*
1160 * Align the virtual region allocation - maximum alignment is
1161 * a section size, minimum is a page size. This helps reduce
1162 * fragmentation of the DMA space, and also prevents allocations
1163 * smaller than a section from crossing a section boundary.
1164 */
1165 bit = fls(size - 1);
1166 if (bit > SECTION_SHIFT)
1167 bit = SECTION_SHIFT;
1168 align = 1 << bit;
1169
1170 /*
1171 * Allocate a virtual address in the consistent mapping region.
1172 */
1173 c = arm_vmregion_alloc(&consistent_head, align, size,
1174 gfp & ~(__GFP_DMA | __GFP_HIGHMEM), NULL);
1175 if (c) {
1176 pte_t *pte;
1177 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
1178 int i = 0;
1179 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
1180
1181 pte = consistent_pte[idx] + off;
1182 c->priv = pages;
1183
1184 do {
1185 BUG_ON(!pte_none(*pte));
1186
1187 set_pte_ext(pte, mk_pte(pages[i], prot), 0);
1188 pte++;
1189 off++;
1190 i++;
1191 if (off >= PTRS_PER_PTE) {
1192 off = 0;
1193 pte = consistent_pte[++idx];
1194 }
1195 } while (i < count);
1196
1197 dsb();
1198
1199 return (void *)c->vm_start;
1200 }
1201 return NULL;
1202}
1203
1204/*
1205 * Create a mapping in device IO address space for specified pages
1206 */
1207static dma_addr_t
1208__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1209{
1210 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1211 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1212 dma_addr_t dma_addr, iova;
1213 int i, ret = DMA_ERROR_CODE;
1214
1215 dma_addr = __alloc_iova(mapping, size);
1216 if (dma_addr == DMA_ERROR_CODE)
1217 return dma_addr;
1218
1219 iova = dma_addr;
1220 for (i = 0; i < count; ) {
1221 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1222 phys_addr_t phys = page_to_phys(pages[i]);
1223 unsigned int len, j;
1224
1225 for (j = i + 1; j < count; j++, next_pfn++)
1226 if (page_to_pfn(pages[j]) != next_pfn)
1227 break;
1228
1229 len = (j - i) << PAGE_SHIFT;
1230 ret = iommu_map(mapping->domain, iova, phys, len, 0);
1231 if (ret < 0)
1232 goto fail;
1233 iova += len;
1234 i = j;
1235 }
1236 return dma_addr;
1237fail:
1238 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1239 __free_iova(mapping, dma_addr, size);
1240 return DMA_ERROR_CODE;
1241}
1242
1243static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1244{
1245 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1246
1247 /*
1248 * add optional in-page offset from iova to size and align
1249 * result to page size
1250 */
1251 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1252 iova &= PAGE_MASK;
1253
1254 iommu_unmap(mapping->domain, iova, size);
1255 __free_iova(mapping, iova, size);
1256 return 0;
1257}
1258
1259static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1260 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1261{
1262 pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
1263 struct page **pages;
1264 void *addr = NULL;
1265
1266 *handle = DMA_ERROR_CODE;
1267 size = PAGE_ALIGN(size);
1268
1269 pages = __iommu_alloc_buffer(dev, size, gfp);
1270 if (!pages)
1271 return NULL;
1272
1273 *handle = __iommu_create_mapping(dev, pages, size);
1274 if (*handle == DMA_ERROR_CODE)
1275 goto err_buffer;
1276
1277 addr = __iommu_alloc_remap(pages, size, gfp, prot);
1278 if (!addr)
1279 goto err_mapping;
1280
1281 return addr;
1282
1283err_mapping:
1284 __iommu_remove_mapping(dev, *handle, size);
1285err_buffer:
1286 __iommu_free_buffer(dev, pages, size);
1287 return NULL;
1288}
1289
1290static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1291 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1292 struct dma_attrs *attrs)
1293{
1294 struct arm_vmregion *c;
1295
1296 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1297 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
1298
1299 if (c) {
1300 struct page **pages = c->priv;
1301
1302 unsigned long uaddr = vma->vm_start;
1303 unsigned long usize = vma->vm_end - vma->vm_start;
1304 int i = 0;
1305
1306 do {
1307 int ret;
1308
1309 ret = vm_insert_page(vma, uaddr, pages[i++]);
1310 if (ret) {
1311 pr_err("Remapping memory, error: %d\n", ret);
1312 return ret;
1313 }
1314
1315 uaddr += PAGE_SIZE;
1316 usize -= PAGE_SIZE;
1317 } while (usize > 0);
1318 }
1319 return 0;
1320}
1321
1322/*
1323 * free a page as defined by the above mapping.
1324 * Must not be called with IRQs disabled.
1325 */
1326void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1327 dma_addr_t handle, struct dma_attrs *attrs)
1328{
1329 struct arm_vmregion *c;
1330 size = PAGE_ALIGN(size);
1331
1332 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
1333 if (c) {
1334 struct page **pages = c->priv;
1335 __dma_free_remap(cpu_addr, size);
1336 __iommu_remove_mapping(dev, handle, size);
1337 __iommu_free_buffer(dev, pages, size);
1338 }
1339}
1340
1341/*
1342 * Map a part of the scatter-gather list into contiguous io address space
1343 */
1344static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1345 size_t size, dma_addr_t *handle,
1346 enum dma_data_direction dir)
1347{
1348 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1349 dma_addr_t iova, iova_base;
1350 int ret = 0;
1351 unsigned int count;
1352 struct scatterlist *s;
1353
1354 size = PAGE_ALIGN(size);
1355 *handle = DMA_ERROR_CODE;
1356
1357 iova_base = iova = __alloc_iova(mapping, size);
1358 if (iova == DMA_ERROR_CODE)
1359 return -ENOMEM;
1360
1361 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1362 phys_addr_t phys = page_to_phys(sg_page(s));
1363 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1364
1365 if (!arch_is_coherent())
1366 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1367
1368 ret = iommu_map(mapping->domain, iova, phys, len, 0);
1369 if (ret < 0)
1370 goto fail;
1371 count += len >> PAGE_SHIFT;
1372 iova += len;
1373 }
1374 *handle = iova_base;
1375
1376 return 0;
1377fail:
1378 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1379 __free_iova(mapping, iova_base, size);
1380 return ret;
1381}
1382
1383/**
1384 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1385 * @dev: valid struct device pointer
1386 * @sg: list of buffers
1387 * @nents: number of buffers to map
1388 * @dir: DMA transfer direction
1389 *
1390 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1391 * The scatter gather list elements are merged together (if possible) and
1392 * tagged with the appropriate dma address and length. They are obtained via
1393 * sg_dma_{address,length}.
1394 */
1395int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1396 enum dma_data_direction dir, struct dma_attrs *attrs)
1397{
1398 struct scatterlist *s = sg, *dma = sg, *start = sg;
1399 int i, count = 0;
1400 unsigned int offset = s->offset;
1401 unsigned int size = s->offset + s->length;
1402 unsigned int max = dma_get_max_seg_size(dev);
1403
1404 for (i = 1; i < nents; i++) {
1405 s = sg_next(s);
1406
1407 s->dma_address = DMA_ERROR_CODE;
1408 s->dma_length = 0;
1409
1410 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1411 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
1412 dir) < 0)
1413 goto bad_mapping;
1414
1415 dma->dma_address += offset;
1416 dma->dma_length = size - offset;
1417
1418 size = offset = s->offset;
1419 start = s;
1420 dma = sg_next(dma);
1421 count += 1;
1422 }
1423 size += s->length;
1424 }
1425 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir) < 0)
1426 goto bad_mapping;
1427
1428 dma->dma_address += offset;
1429 dma->dma_length = size - offset;
1430
1431 return count+1;
1432
1433bad_mapping:
1434 for_each_sg(sg, s, count, i)
1435 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1436 return 0;
1437}
1438
1439/**
1440 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1441 * @dev: valid struct device pointer
1442 * @sg: list of buffers
1443 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1444 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1445 *
1446 * Unmap a set of streaming mode DMA translations. Again, CPU access
1447 * rules concerning calls here are the same as for dma_unmap_single().
1448 */
1449void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1450 enum dma_data_direction dir, struct dma_attrs *attrs)
1451{
1452 struct scatterlist *s;
1453 int i;
1454
1455 for_each_sg(sg, s, nents, i) {
1456 if (sg_dma_len(s))
1457 __iommu_remove_mapping(dev, sg_dma_address(s),
1458 sg_dma_len(s));
1459 if (!arch_is_coherent())
1460 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1461 s->length, dir);
1462 }
1463}
1464
1465/**
1466 * arm_iommu_sync_sg_for_cpu
1467 * @dev: valid struct device pointer
1468 * @sg: list of buffers
1469 * @nents: number of buffers to map (returned from dma_map_sg)
1470 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1471 */
1472void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1473 int nents, enum dma_data_direction dir)
1474{
1475 struct scatterlist *s;
1476 int i;
1477
1478 for_each_sg(sg, s, nents, i)
1479 if (!arch_is_coherent())
1480 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
1481
1482}
1483
1484/**
1485 * arm_iommu_sync_sg_for_device
1486 * @dev: valid struct device pointer
1487 * @sg: list of buffers
1488 * @nents: number of buffers to map (returned from dma_map_sg)
1489 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1490 */
1491void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1492 int nents, enum dma_data_direction dir)
1493{
1494 struct scatterlist *s;
1495 int i;
1496
1497 for_each_sg(sg, s, nents, i)
1498 if (!arch_is_coherent())
1499 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1500}
1501
1502
1503/**
1504 * arm_iommu_map_page
1505 * @dev: valid struct device pointer
1506 * @page: page that buffer resides in
1507 * @offset: offset into page for start of buffer
1508 * @size: size of buffer to map
1509 * @dir: DMA transfer direction
1510 *
1511 * IOMMU aware version of arm_dma_map_page()
1512 */
1513static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1514 unsigned long offset, size_t size, enum dma_data_direction dir,
1515 struct dma_attrs *attrs)
1516{
1517 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1518 dma_addr_t dma_addr;
1519 int ret, len = PAGE_ALIGN(size + offset);
1520
1521 if (!arch_is_coherent())
1522 __dma_page_cpu_to_dev(page, offset, size, dir);
1523
1524 dma_addr = __alloc_iova(mapping, len);
1525 if (dma_addr == DMA_ERROR_CODE)
1526 return dma_addr;
1527
1528 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0);
1529 if (ret < 0)
1530 goto fail;
1531
1532 return dma_addr + offset;
1533fail:
1534 __free_iova(mapping, dma_addr, len);
1535 return DMA_ERROR_CODE;
1536}
1537
1538/**
1539 * arm_iommu_unmap_page
1540 * @dev: valid struct device pointer
1541 * @handle: DMA address of buffer
1542 * @size: size of buffer (same as passed to dma_map_page)
1543 * @dir: DMA transfer direction (same as passed to dma_map_page)
1544 *
1545 * IOMMU aware version of arm_dma_unmap_page()
1546 */
1547static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1548 size_t size, enum dma_data_direction dir,
1549 struct dma_attrs *attrs)
1550{
1551 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1552 dma_addr_t iova = handle & PAGE_MASK;
1553 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1554 int offset = handle & ~PAGE_MASK;
1555 int len = PAGE_ALIGN(size + offset);
1556
1557 if (!iova)
1558 return;
1559
1560 if (!arch_is_coherent())
1561 __dma_page_dev_to_cpu(page, offset, size, dir);
1562
1563 iommu_unmap(mapping->domain, iova, len);
1564 __free_iova(mapping, iova, len);
1565}
1566
1567static void arm_iommu_sync_single_for_cpu(struct device *dev,
1568 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1569{
1570 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1571 dma_addr_t iova = handle & PAGE_MASK;
1572 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1573 unsigned int offset = handle & ~PAGE_MASK;
1574
1575 if (!iova)
1576 return;
1577
1578 if (!arch_is_coherent())
1579 __dma_page_dev_to_cpu(page, offset, size, dir);
1580}
1581
1582static void arm_iommu_sync_single_for_device(struct device *dev,
1583 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1584{
1585 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1586 dma_addr_t iova = handle & PAGE_MASK;
1587 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1588 unsigned int offset = handle & ~PAGE_MASK;
1589
1590 if (!iova)
1591 return;
1592
1593 __dma_page_cpu_to_dev(page, offset, size, dir);
1594}
1595
1596struct dma_map_ops iommu_ops = {
1597 .alloc = arm_iommu_alloc_attrs,
1598 .free = arm_iommu_free_attrs,
1599 .mmap = arm_iommu_mmap_attrs,
1600
1601 .map_page = arm_iommu_map_page,
1602 .unmap_page = arm_iommu_unmap_page,
1603 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
1604 .sync_single_for_device = arm_iommu_sync_single_for_device,
1605
1606 .map_sg = arm_iommu_map_sg,
1607 .unmap_sg = arm_iommu_unmap_sg,
1608 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
1609 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
1610};
1611
1612/**
1613 * arm_iommu_create_mapping
1614 * @bus: pointer to the bus holding the client device (for IOMMU calls)
1615 * @base: start address of the valid IO address space
1616 * @size: size of the valid IO address space
1617 * @order: accuracy of the IO addresses allocations
1618 *
1619 * Creates a mapping structure which holds information about used/unused
1620 * IO address ranges, which is required to perform memory allocation and
1621 * mapping with IOMMU aware functions.
1622 *
1623 * The client device need to be attached to the mapping with
1624 * arm_iommu_attach_device function.
1625 */
1626struct dma_iommu_mapping *
1627arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
1628 int order)
1629{
1630 unsigned int count = size >> (PAGE_SHIFT + order);
1631 unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
1632 struct dma_iommu_mapping *mapping;
1633 int err = -ENOMEM;
1634
1635 if (!count)
1636 return ERR_PTR(-EINVAL);
1637
1638 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1639 if (!mapping)
1640 goto err;
1641
1642 mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1643 if (!mapping->bitmap)
1644 goto err2;
1645
1646 mapping->base = base;
1647 mapping->bits = BITS_PER_BYTE * bitmap_size;
1648 mapping->order = order;
1649 spin_lock_init(&mapping->lock);
1650
1651 mapping->domain = iommu_domain_alloc(bus);
1652 if (!mapping->domain)
1653 goto err3;
1654
1655 kref_init(&mapping->kref);
1656 return mapping;
1657err3:
1658 kfree(mapping->bitmap);
1659err2:
1660 kfree(mapping);
1661err:
1662 return ERR_PTR(err);
1663}
1664
1665static void release_iommu_mapping(struct kref *kref)
1666{
1667 struct dma_iommu_mapping *mapping =
1668 container_of(kref, struct dma_iommu_mapping, kref);
1669
1670 iommu_domain_free(mapping->domain);
1671 kfree(mapping->bitmap);
1672 kfree(mapping);
1673}
1674
1675void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1676{
1677 if (mapping)
1678 kref_put(&mapping->kref, release_iommu_mapping);
1679}
1680
1681/**
1682 * arm_iommu_attach_device
1683 * @dev: valid struct device pointer
1684 * @mapping: io address space mapping structure (returned from
1685 * arm_iommu_create_mapping)
1686 *
1687 * Attaches specified io address space mapping to the provided device,
1688 * this replaces the dma operations (dma_map_ops pointer) with the
1689 * IOMMU aware version. More than one client might be attached to
1690 * the same io address space mapping.
1691 */
1692int arm_iommu_attach_device(struct device *dev,
1693 struct dma_iommu_mapping *mapping)
1694{
1695 int err;
1696
1697 err = iommu_attach_device(mapping->domain, dev);
1698 if (err)
1699 return err;
1700
1701 kref_get(&mapping->kref);
1702 dev->archdata.mapping = mapping;
1703 set_dma_ops(dev, &iommu_ops);
1704
1705 pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev));
1706 return 0;
1707}
1708
1709#endif