blob: eecc8e60deeada05f299824948209a816c702246 [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 */
Russell King11a5aa32013-11-25 21:52:25 +000012#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/list.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010021#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050022#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010023#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010024#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020025#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020026#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020027#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010028#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070029#include <linux/cma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010031#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040032#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010035#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020036#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010037#include <asm/mach/map.h>
38#include <asm/system_info.h>
39#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Russell King022ae532011-07-08 21:26:59 +010041#include "mm.h"
42
Marek Szyprowski15237e12012-02-10 19:55:20 +010043/*
44 * The DMA API is built upon the notion of "buffer ownership". A buffer
45 * is either exclusively owned by the CPU (and therefore may be accessed
46 * by it) or exclusively owned by the DMA device. These helper functions
47 * represent the transitions between these two ownership states.
48 *
49 * Note, however, that on later ARMs, this notion does not work due to
50 * speculative prefetches. We model our approach on the assumption that
51 * the CPU does do speculative prefetches, which means we clean caches
52 * before transfers and delay cache invalidation until transfer completion.
53 *
Marek Szyprowski15237e12012-02-10 19:55:20 +010054 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010055static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010056 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +010057static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010058 size_t, enum dma_data_direction);
59
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010060/**
61 * arm_dma_map_page - map a portion of a page for streaming DMA
62 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
63 * @page: page that buffer resides in
64 * @offset: offset into page for start of buffer
65 * @size: size of buffer to map
66 * @dir: DMA transfer direction
67 *
68 * Ensure that any data held in the cache is appropriately discarded
69 * or written back.
70 *
71 * The device owns this memory once this call has completed. The CPU
72 * can regain ownership by calling dma_unmap_page().
73 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010074static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010075 unsigned long offset, size_t size, enum dma_data_direction dir,
76 struct dma_attrs *attrs)
77{
Rob Herringdd37e942012-08-21 12:20:17 +020078 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +010079 __dma_page_cpu_to_dev(page, offset, size, dir);
80 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010081}
82
Rob Herringdd37e942012-08-21 12:20:17 +020083static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
84 unsigned long offset, size_t size, enum dma_data_direction dir,
85 struct dma_attrs *attrs)
86{
87 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
88}
89
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010090/**
91 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
92 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
93 * @handle: DMA address of buffer
94 * @size: size of buffer (same as passed to dma_map_page)
95 * @dir: DMA transfer direction (same as passed to dma_map_page)
96 *
97 * Unmap a page streaming mode DMA translation. The handle and size
98 * must match what was provided in the previous dma_map_page() call.
99 * All other usages are undefined.
100 *
101 * After this call, reads by the CPU to the buffer are guaranteed to see
102 * whatever the device wrote there.
103 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100104static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100105 size_t size, enum dma_data_direction dir,
106 struct dma_attrs *attrs)
107{
Rob Herringdd37e942012-08-21 12:20:17 +0200108 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100109 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
110 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100111}
112
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100113static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100114 dma_addr_t handle, size_t size, enum dma_data_direction dir)
115{
116 unsigned int offset = handle & (PAGE_SIZE - 1);
117 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200118 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100119}
120
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100121static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100122 dma_addr_t handle, size_t size, enum dma_data_direction dir)
123{
124 unsigned int offset = handle & (PAGE_SIZE - 1);
125 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200126 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100127}
128
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100129struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200130 .alloc = arm_dma_alloc,
131 .free = arm_dma_free,
132 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200133 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100134 .map_page = arm_dma_map_page,
135 .unmap_page = arm_dma_unmap_page,
136 .map_sg = arm_dma_map_sg,
137 .unmap_sg = arm_dma_unmap_sg,
138 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
139 .sync_single_for_device = arm_dma_sync_single_for_device,
140 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
141 .sync_sg_for_device = arm_dma_sync_sg_for_device,
142 .set_dma_mask = arm_dma_set_mask,
143};
144EXPORT_SYMBOL(arm_dma_ops);
145
Rob Herringdd37e942012-08-21 12:20:17 +0200146static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
147 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
148static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
149 dma_addr_t handle, struct dma_attrs *attrs);
150
151struct dma_map_ops arm_coherent_dma_ops = {
152 .alloc = arm_coherent_dma_alloc,
153 .free = arm_coherent_dma_free,
154 .mmap = arm_dma_mmap,
155 .get_sgtable = arm_dma_get_sgtable,
156 .map_page = arm_coherent_dma_map_page,
157 .map_sg = arm_dma_map_sg,
158 .set_dma_mask = arm_dma_set_mask,
159};
160EXPORT_SYMBOL(arm_coherent_dma_ops);
161
Russell King9f28cde2013-12-06 12:30:42 +0000162static int __dma_supported(struct device *dev, u64 mask, bool warn)
163{
164 unsigned long max_dma_pfn;
165
166 /*
167 * If the mask allows for more memory than we can address,
168 * and we actually have that much memory, then we must
169 * indicate that DMA to this device is not supported.
170 */
171 if (sizeof(mask) != sizeof(dma_addr_t) &&
172 mask > (dma_addr_t)~0 &&
173 dma_to_pfn(dev, ~0) < max_pfn) {
174 if (warn) {
175 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
176 mask);
177 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
178 }
179 return 0;
180 }
181
182 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
183
184 /*
185 * Translate the device's DMA mask to a PFN limit. This
186 * PFN number includes the page which we can DMA to.
187 */
188 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
189 if (warn)
190 dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
191 mask,
192 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
193 max_dma_pfn + 1);
194 return 0;
195 }
196
197 return 1;
198}
199
Catalin Marinasab6494f2009-07-24 12:35:02 +0100200static u64 get_coherent_dma_mask(struct device *dev)
201{
Russell King4dcfa602013-07-09 12:14:49 +0100202 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Catalin Marinasab6494f2009-07-24 12:35:02 +0100204 if (dev) {
205 mask = dev->coherent_dma_mask;
206
207 /*
208 * Sanity check the DMA mask - it must be non-zero, and
209 * must be able to be satisfied by a DMA allocation.
210 */
211 if (mask == 0) {
212 dev_warn(dev, "coherent DMA mask is unset\n");
213 return 0;
214 }
215
Russell King9f28cde2013-12-06 12:30:42 +0000216 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100217 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100218 }
219
220 return mask;
221}
222
Marek Szyprowskic7909502011-12-29 13:09:51 +0100223static void __dma_clear_buffer(struct page *page, size_t size)
224{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100225 /*
226 * Ensure that the allocated pages are zeroed, and that any data
227 * lurking in the kernel direct-mapped region is invalidated.
228 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100229 if (PageHighMem(page)) {
230 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
231 phys_addr_t end = base + size;
232 while (size > 0) {
233 void *ptr = kmap_atomic(page);
234 memset(ptr, 0, PAGE_SIZE);
235 dmac_flush_range(ptr, ptr + PAGE_SIZE);
236 kunmap_atomic(ptr);
237 page++;
238 size -= PAGE_SIZE;
239 }
240 outer_flush_range(base, end);
241 } else {
242 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200243 memset(ptr, 0, size);
244 dmac_flush_range(ptr, ptr + size);
245 outer_flush_range(__pa(ptr), __pa(ptr) + size);
246 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100247}
248
Russell King7a9a32a2009-11-19 15:31:07 +0000249/*
250 * Allocate a DMA buffer for 'dev' of size 'size' using the
251 * specified gfp mask. Note that 'size' must be page aligned.
252 */
253static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
254{
255 unsigned long order = get_order(size);
256 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000257
258 page = alloc_pages(gfp, order);
259 if (!page)
260 return NULL;
261
262 /*
263 * Now split the huge page and free the excess pages
264 */
265 split_page(page, order);
266 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
267 __free_page(p);
268
Marek Szyprowskic7909502011-12-29 13:09:51 +0100269 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000270
271 return page;
272}
273
274/*
275 * Free a DMA buffer. 'size' must be page aligned.
276 */
277static void __dma_free_buffer(struct page *page, size_t size)
278{
279 struct page *e = page + (size >> PAGE_SHIFT);
280
281 while (page < e) {
282 __free_page(page);
283 page++;
284 }
285}
286
Catalin Marinasab6494f2009-07-24 12:35:02 +0100287#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Marek Szyprowskic7909502011-12-29 13:09:51 +0100289static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100290 pgprot_t prot, struct page **ret_page,
291 const void *caller);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100292
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200293static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
294 pgprot_t prot, struct page **ret_page,
295 const void *caller);
296
297static void *
298__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
299 const void *caller)
300{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200301 /*
302 * DMA allocation can be mapped to user space, so lets
303 * set VM_USERMAP flags too.
304 */
Laura Abbott513510d2014-10-09 15:26:40 -0700305 return dma_common_contiguous_remap(page, size,
306 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
307 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200308}
309
310static void __dma_free_remap(void *cpu_addr, size_t size)
311{
Laura Abbott513510d2014-10-09 15:26:40 -0700312 dma_common_free_remap(cpu_addr, size,
313 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200314}
315
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200316#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
317
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200318struct dma_pool {
319 size_t size;
320 spinlock_t lock;
321 unsigned long *bitmap;
322 unsigned long nr_pages;
323 void *vaddr;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300324 struct page **pages;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100325};
326
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200327static struct dma_pool atomic_pool = {
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200328 .size = DEFAULT_DMA_COHERENT_POOL_SIZE,
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200329};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100330
331static int __init early_coherent_pool(char *p)
332{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200333 atomic_pool.size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100334 return 0;
335}
336early_param("coherent_pool", early_coherent_pool);
337
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200338void __init init_dma_coherent_pool_size(unsigned long size)
339{
340 /*
341 * Catch any attempt to set the pool size too late.
342 */
343 BUG_ON(atomic_pool.vaddr);
344
345 /*
346 * Set architecture specific coherent pool size only if
347 * it has not been changed by kernel command line parameter.
348 */
349 if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE)
350 atomic_pool.size = size;
351}
352
Marek Szyprowskic7909502011-12-29 13:09:51 +0100353/*
354 * Initialise the coherent pool for atomic allocations.
355 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200356static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100357{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200358 struct dma_pool *pool = &atomic_pool;
Russell King71b55662013-11-25 12:01:03 +0000359 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100360 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200361 unsigned long nr_pages = pool->size >> PAGE_SHIFT;
362 unsigned long *bitmap;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100363 struct page *page;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300364 struct page **pages;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100365 void *ptr;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200366 int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100367
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200368 bitmap = kzalloc(bitmap_size, GFP_KERNEL);
369 if (!bitmap)
370 goto no_bitmap;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100371
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300372 pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
373 if (!pages)
374 goto no_pages;
375
Gioh Kime464ef12014-05-22 13:38:23 +0900376 if (dev_get_cma_area(NULL))
Marek Szyprowski9848e482013-01-16 15:38:44 +0100377 ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page,
378 atomic_pool_init);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200379 else
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100380 ptr = __alloc_remap_buffer(NULL, pool->size, gfp, prot, &page,
381 atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100382 if (ptr) {
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300383 int i;
384
385 for (i = 0; i < nr_pages; i++)
386 pages[i] = page + i;
387
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200388 spin_lock_init(&pool->lock);
389 pool->vaddr = ptr;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300390 pool->pages = pages;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200391 pool->bitmap = bitmap;
392 pool->nr_pages = nr_pages;
393 pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
394 (unsigned)pool->size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100395 return 0;
396 }
Sachin Kamatec106652012-09-24 08:35:03 +0200397
398 kfree(pages);
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300399no_pages:
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200400 kfree(bitmap);
401no_bitmap:
402 pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
403 (unsigned)pool->size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100404 return -ENOMEM;
405}
406/*
407 * CMA is activated by core_initcall, so we must be called after it.
408 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200409postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100410
411struct dma_contig_early_reserve {
412 phys_addr_t base;
413 unsigned long size;
414};
415
416static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
417
418static int dma_mmu_remap_num __initdata;
419
420void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
421{
422 dma_mmu_remap[dma_mmu_remap_num].base = base;
423 dma_mmu_remap[dma_mmu_remap_num].size = size;
424 dma_mmu_remap_num++;
425}
426
427void __init dma_contiguous_remap(void)
428{
429 int i;
430 for (i = 0; i < dma_mmu_remap_num; i++) {
431 phys_addr_t start = dma_mmu_remap[i].base;
432 phys_addr_t end = start + dma_mmu_remap[i].size;
433 struct map_desc map;
434 unsigned long addr;
435
436 if (end > arm_lowmem_limit)
437 end = arm_lowmem_limit;
438 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200439 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100440
441 map.pfn = __phys_to_pfn(start);
442 map.virtual = __phys_to_virt(start);
443 map.length = end - start;
444 map.type = MT_MEMORY_DMA_READY;
445
446 /*
Russell King6b076992014-07-17 12:17:45 +0100447 * Clear previous low-memory mapping to ensure that the
448 * TLB does not see any conflicting entries, then flush
449 * the TLB of the old entries before creating new mappings.
450 *
451 * This ensures that any speculatively loaded TLB entries
452 * (even though they may be rare) can not cause any problems,
453 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100454 */
455 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400456 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100457 pmd_clear(pmd_off_k(addr));
458
Russell King6b076992014-07-17 12:17:45 +0100459 flush_tlb_kernel_range(__phys_to_virt(start),
460 __phys_to_virt(end));
461
Marek Szyprowskic7909502011-12-29 13:09:51 +0100462 iotable_init(&map, 1);
463 }
464}
465
Marek Szyprowskic7909502011-12-29 13:09:51 +0100466static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
467 void *data)
468{
469 struct page *page = virt_to_page(addr);
470 pgprot_t prot = *(pgprot_t *)data;
471
472 set_pte_ext(pte, mk_pte(page, prot), 0);
473 return 0;
474}
475
476static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
477{
478 unsigned long start = (unsigned long) page_address(page);
479 unsigned end = start + size;
480
481 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100482 flush_tlb_kernel_range(start, end);
483}
484
485static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
486 pgprot_t prot, struct page **ret_page,
487 const void *caller)
488{
489 struct page *page;
490 void *ptr;
491 page = __dma_alloc_buffer(dev, size, gfp);
492 if (!page)
493 return NULL;
494
495 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
496 if (!ptr) {
497 __dma_free_buffer(page, size);
498 return NULL;
499 }
500
501 *ret_page = page;
502 return ptr;
503}
504
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200505static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100506{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200507 struct dma_pool *pool = &atomic_pool;
508 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
509 unsigned int pageno;
510 unsigned long flags;
511 void *ptr = NULL;
Aaro Koskinene4ea6912012-08-07 14:39:25 +0200512 unsigned long align_mask;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100513
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200514 if (!pool->vaddr) {
515 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100516 return NULL;
517 }
518
519 /*
520 * Align the region allocation - allocations from pool are rather
521 * small, so align them to their order in pages, minimum is a page
522 * size. This helps reduce fragmentation of the DMA space.
523 */
Aaro Koskinene4ea6912012-08-07 14:39:25 +0200524 align_mask = (1 << get_order(size)) - 1;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200525
526 spin_lock_irqsave(&pool->lock, flags);
527 pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
Aaro Koskinene4ea6912012-08-07 14:39:25 +0200528 0, count, align_mask);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200529 if (pageno < pool->nr_pages) {
530 bitmap_set(pool->bitmap, pageno, count);
531 ptr = pool->vaddr + PAGE_SIZE * pageno;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300532 *ret_page = pool->pages[pageno];
Marek Szyprowskifb712852012-08-22 14:50:42 +0200533 } else {
534 pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n"
535 "Please increase it with coherent_pool= kernel parameter!\n",
536 (unsigned)pool->size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100537 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200538 spin_unlock_irqrestore(&pool->lock, flags);
539
540 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100541}
542
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300543static bool __in_atomic_pool(void *start, size_t size)
544{
545 struct dma_pool *pool = &atomic_pool;
546 void *end = start + size;
547 void *pool_start = pool->vaddr;
548 void *pool_end = pool->vaddr + pool->size;
549
Thomas Petazzonif3d87522012-09-10 16:14:16 +0200550 if (start < pool_start || start >= pool_end)
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300551 return false;
552
553 if (end <= pool_end)
554 return true;
555
556 WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n",
557 start, end - 1, pool_start, pool_end - 1);
558
559 return false;
560}
561
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200562static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100563{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200564 struct dma_pool *pool = &atomic_pool;
565 unsigned long pageno, count;
566 unsigned long flags;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100567
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300568 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100569 return 0;
570
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200571 pageno = (start - pool->vaddr) >> PAGE_SHIFT;
572 count = size >> PAGE_SHIFT;
573
574 spin_lock_irqsave(&pool->lock, flags);
575 bitmap_clear(pool->bitmap, pageno, count);
576 spin_unlock_irqrestore(&pool->lock, flags);
577
Marek Szyprowskic7909502011-12-29 13:09:51 +0100578 return 1;
579}
580
581static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100582 pgprot_t prot, struct page **ret_page,
583 const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100584{
585 unsigned long order = get_order(size);
586 size_t count = size >> PAGE_SHIFT;
587 struct page *page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100588 void *ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100589
590 page = dma_alloc_from_contiguous(dev, count, order);
591 if (!page)
592 return NULL;
593
594 __dma_clear_buffer(page, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100595
Marek Szyprowski9848e482013-01-16 15:38:44 +0100596 if (PageHighMem(page)) {
597 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
598 if (!ptr) {
599 dma_release_from_contiguous(dev, page, count);
600 return NULL;
601 }
602 } else {
603 __dma_remap(page, size, prot);
604 ptr = page_address(page);
605 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100606 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100607 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100608}
609
610static void __free_from_contiguous(struct device *dev, struct page *page,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100611 void *cpu_addr, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100612{
Marek Szyprowski9848e482013-01-16 15:38:44 +0100613 if (PageHighMem(page))
614 __dma_free_remap(cpu_addr, size);
615 else
Russell King71b55662013-11-25 12:01:03 +0000616 __dma_remap(page, size, PAGE_KERNEL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100617 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
618}
619
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200620static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
621{
622 prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
623 pgprot_writecombine(prot) :
624 pgprot_dmacoherent(prot);
625 return prot;
626}
627
Marek Szyprowskic7909502011-12-29 13:09:51 +0100628#define nommu() 0
629
Catalin Marinasab6494f2009-07-24 12:35:02 +0100630#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000631
Marek Szyprowskic7909502011-12-29 13:09:51 +0100632#define nommu() 1
633
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200634#define __get_dma_pgprot(attrs, prot) __pgprot(0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100635#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200636#define __alloc_from_pool(size, ret_page) NULL
Marek Szyprowski9848e482013-01-16 15:38:44 +0100637#define __alloc_from_contiguous(dev, size, prot, ret, c) NULL
Marek Szyprowskic7909502011-12-29 13:09:51 +0100638#define __free_from_pool(cpu_addr, size) 0
Marek Szyprowski9848e482013-01-16 15:38:44 +0100639#define __free_from_contiguous(dev, page, cpu_addr, size) do { } while (0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100640#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000641
642#endif /* CONFIG_MMU */
643
Marek Szyprowskic7909502011-12-29 13:09:51 +0100644static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
645 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100646{
Russell King04da5692009-11-19 15:54:45 +0000647 struct page *page;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100648 page = __dma_alloc_buffer(dev, size, gfp);
649 if (!page)
650 return NULL;
651
652 *ret_page = page;
653 return page_address(page);
654}
655
656
657
658static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Rob Herringdd37e942012-08-21 12:20:17 +0200659 gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100660{
661 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900662 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000663 void *addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100664
Marek Szyprowskic7909502011-12-29 13:09:51 +0100665#ifdef CONFIG_DMA_API_DEBUG
666 u64 limit = (mask + 1) & ~mask;
667 if (limit && size >= limit) {
668 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
669 size, mask);
670 return NULL;
671 }
672#endif
673
674 if (!mask)
675 return NULL;
676
677 if (mask < 0xffffffffULL)
678 gfp |= GFP_DMA;
679
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100680 /*
681 * Following is a work-around (a.k.a. hack) to prevent pages
682 * with __GFP_COMP being passed to split_page() which cannot
683 * handle them. The real problem is that this flag probably
684 * should be 0 on ARM as it is not supported on this
685 * platform; see CONFIG_HUGETLBFS.
686 */
687 gfp &= ~(__GFP_COMP);
688
Marek Szyprowski553ac782012-02-29 14:45:28 +0100689 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000690 size = PAGE_ALIGN(size);
691
Rob Herringdd37e942012-08-21 12:20:17 +0200692 if (is_coherent || nommu())
Marek Szyprowskic7909502011-12-29 13:09:51 +0100693 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Russell King633dc922013-01-30 23:55:35 +0000694 else if (!(gfp & __GFP_WAIT))
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200695 addr = __alloc_from_pool(size, &page);
Gioh Kime464ef12014-05-22 13:38:23 +0900696 else if (!dev_get_cma_area(dev))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100697 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000698 else
Marek Szyprowski9848e482013-01-16 15:38:44 +0100699 addr = __alloc_from_contiguous(dev, size, prot, &page, caller);
Russell King31ebf942009-11-19 21:12:17 +0000700
701 if (addr)
Russell King9eedd962011-01-03 00:00:17 +0000702 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000703
704 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100705}
Russell King695ae0a2009-11-19 16:31:39 +0000706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/*
708 * Allocate DMA-coherent memory space and return both the kernel remapped
709 * virtual and bus address for that space.
710 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200711void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
712 gfp_t gfp, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Russell King0ea1ec72013-10-23 16:14:59 +0100714 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400715 void *memory;
716
717 if (dma_alloc_from_coherent(dev, size, handle, &memory))
718 return memory;
719
Rob Herringdd37e942012-08-21 12:20:17 +0200720 return __dma_alloc(dev, size, handle, gfp, prot, false,
721 __builtin_return_address(0));
722}
723
724static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
725 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
726{
Russell King0ea1ec72013-10-23 16:14:59 +0100727 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Rob Herringdd37e942012-08-21 12:20:17 +0200728 void *memory;
729
730 if (dma_alloc_from_coherent(dev, size, handle, &memory))
731 return memory;
732
733 return __dma_alloc(dev, size, handle, gfp, prot, true,
Russell King45cd5292012-01-12 23:08:07 +0000734 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737/*
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200738 * Create userspace mapping for the DMA-coherent memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200740int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
741 void *cpu_addr, dma_addr_t dma_addr, size_t size,
742 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100744 int ret = -ENXIO;
745#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200746 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
747 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100748 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200749 unsigned long off = vma->vm_pgoff;
750
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200751 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
752
Marek Szyprowski47142f02012-05-15 19:04:13 +0200753 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
754 return ret;
755
Marek Szyprowski50262a42012-07-30 09:35:26 +0200756 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
757 ret = remap_pfn_range(vma, vma->vm_start,
758 pfn + off,
759 vma->vm_end - vma->vm_start,
760 vma->vm_page_prot);
761 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100762#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return ret;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100768 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Rob Herringdd37e942012-08-21 12:20:17 +0200770static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
771 dma_addr_t handle, struct dma_attrs *attrs,
772 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100774 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Russell King5edf71a2005-11-25 15:52:51 +0000775
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400776 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
777 return;
778
Russell King3e82d012009-11-19 15:38:12 +0000779 size = PAGE_ALIGN(size);
780
Rob Herringdd37e942012-08-21 12:20:17 +0200781 if (is_coherent || nommu()) {
Marek Szyprowskic7909502011-12-29 13:09:51 +0100782 __dma_free_buffer(page, size);
Aaro Koskinend9e0d142012-08-07 14:44:05 +0200783 } else if (__free_from_pool(cpu_addr, size)) {
784 return;
Gioh Kime464ef12014-05-22 13:38:23 +0900785 } else if (!dev_get_cma_area(dev)) {
Russell King695ae0a2009-11-19 16:31:39 +0000786 __dma_free_remap(cpu_addr, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100787 __dma_free_buffer(page, size);
788 } else {
Marek Szyprowskic7909502011-12-29 13:09:51 +0100789 /*
790 * Non-atomic allocations cannot be freed with IRQs disabled
791 */
792 WARN_ON(irqs_disabled());
Marek Szyprowski9848e482013-01-16 15:38:44 +0100793 __free_from_contiguous(dev, page, cpu_addr, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Russell Kingafd1a322008-09-25 16:30:57 +0100796
Rob Herringdd37e942012-08-21 12:20:17 +0200797void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
798 dma_addr_t handle, struct dma_attrs *attrs)
799{
800 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
801}
802
803static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
804 dma_addr_t handle, struct dma_attrs *attrs)
805{
806 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
807}
808
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200809int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
810 void *cpu_addr, dma_addr_t handle, size_t size,
811 struct dma_attrs *attrs)
812{
813 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
814 int ret;
815
816 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
817 if (unlikely(ret))
818 return ret;
819
820 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
821 return 0;
822}
823
Russell King65af1912009-11-24 17:53:33 +0000824static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000825 size_t size, enum dma_data_direction dir,
826 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000827{
Russell King15653372013-01-19 11:05:57 +0000828 unsigned long pfn;
829 size_t left = size;
830
831 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
832 offset %= PAGE_SIZE;
833
Russell King65af1912009-11-24 17:53:33 +0000834 /*
835 * A single sg entry may refer to multiple physically contiguous
836 * pages. But we still need to process highmem pages individually.
837 * If highmem is not configured then the bulk of this loop gets
838 * optimized out.
839 */
Russell King65af1912009-11-24 17:53:33 +0000840 do {
841 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000842 void *vaddr;
843
Russell King15653372013-01-19 11:05:57 +0000844 page = pfn_to_page(pfn);
845
Russell King93f1d622009-11-24 14:41:01 +0000846 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000847 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000848 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100849
850 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500851 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100852 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500853 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100854 } else {
855 vaddr = kmap_high_get(page);
856 if (vaddr) {
857 op(vaddr + offset, len, dir);
858 kunmap_high(page);
859 }
Russell King93f1d622009-11-24 14:41:01 +0000860 }
861 } else {
862 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000863 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000864 }
Russell King65af1912009-11-24 17:53:33 +0000865 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000866 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000867 left -= len;
868 } while (left);
869}
870
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100871/*
872 * Make an area consistent for devices.
873 * Note: Drivers should NOT use this function directly, as it will break
874 * platforms with CONFIG_DMABOUNCE.
875 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
876 */
877static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000878 size_t size, enum dma_data_direction dir)
879{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400880 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400881
Russell Kinga9c91472009-11-26 16:19:58 +0000882 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400883
Russell King65af1912009-11-24 17:53:33 +0000884 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000885 if (dir == DMA_FROM_DEVICE) {
886 outer_inv_range(paddr, paddr + size);
887 } else {
888 outer_clean_range(paddr, paddr + size);
889 }
890 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400891}
Russell King4ea0d732009-11-24 16:27:17 +0000892
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100893static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000894 size_t size, enum dma_data_direction dir)
895{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400896 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000897
898 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +0100899 /* in any case, don't bother invalidating if DMA to device */
900 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +0000901 outer_inv_range(paddr, paddr + size);
902
Russell Kingdeace4a2014-05-03 11:06:55 +0100903 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
904 }
Catalin Marinasc0177802010-09-13 15:57:36 +0100905
906 /*
Ming Leib2a234e2013-05-18 11:21:36 +0100907 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +0100908 */
Ming Leib2a234e2013-05-18 11:21:36 +0100909 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
910 unsigned long pfn;
911 size_t left = size;
912
913 pfn = page_to_pfn(page) + off / PAGE_SIZE;
914 off %= PAGE_SIZE;
915 if (off) {
916 pfn++;
917 left -= PAGE_SIZE - off;
918 }
919 while (left >= PAGE_SIZE) {
920 page = pfn_to_page(pfn++);
921 set_bit(PG_dcache_clean, &page->flags);
922 left -= PAGE_SIZE;
923 }
924 }
Russell King4ea0d732009-11-24 16:27:17 +0000925}
Nicolas Pitre43377452009-03-12 22:52:09 -0400926
Russell Kingafd1a322008-09-25 16:30:57 +0100927/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100928 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100929 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
930 * @sg: list of buffers
931 * @nents: number of buffers to map
932 * @dir: DMA transfer direction
933 *
934 * Map a set of buffers described by scatterlist in streaming mode for DMA.
935 * This is the scatter-gather version of the dma_map_single interface.
936 * Here the scatter gather list elements are each tagged with the
937 * appropriate dma address and length. They are obtained via
938 * sg_dma_{address,length}.
939 *
940 * Device ownership issues as mentioned for dma_map_single are the same
941 * here.
942 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100943int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
944 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100945{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100946 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100947 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100948 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100949
950 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200951#ifdef CONFIG_NEED_SG_DMA_LENGTH
952 s->dma_length = s->length;
953#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100954 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
955 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100956 if (dma_mapping_error(dev, s->dma_address))
957 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100958 }
Russell Kingafd1a322008-09-25 16:30:57 +0100959 return nents;
Russell King01135d922008-09-25 21:05:02 +0100960
961 bad_mapping:
962 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100963 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100964 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100965}
Russell Kingafd1a322008-09-25 16:30:57 +0100966
967/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100968 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100969 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
970 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100971 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100972 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
973 *
974 * Unmap a set of streaming mode DMA translations. Again, CPU access
975 * rules concerning calls here are the same as for dma_unmap_single().
976 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100977void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
978 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100979{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100980 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100981 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100982
Russell King01135d922008-09-25 21:05:02 +0100983 int i;
Russell King24056f52011-01-03 11:29:28 +0000984
Russell King01135d922008-09-25 21:05:02 +0100985 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100986 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100987}
Russell Kingafd1a322008-09-25 16:30:57 +0100988
989/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100990 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100991 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
992 * @sg: list of buffers
993 * @nents: number of buffers to map (returned from dma_map_sg)
994 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
995 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100996void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100997 int nents, enum dma_data_direction dir)
998{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100999 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001000 struct scatterlist *s;
1001 int i;
1002
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001003 for_each_sg(sg, s, nents, i)
1004 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1005 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001006}
Russell Kingafd1a322008-09-25 16:30:57 +01001007
1008/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001009 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001010 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1011 * @sg: list of buffers
1012 * @nents: number of buffers to map (returned from dma_map_sg)
1013 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1014 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001015void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001016 int nents, enum dma_data_direction dir)
1017{
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001018 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001019 struct scatterlist *s;
1020 int i;
1021
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001022 for_each_sg(sg, s, nents, i)
1023 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1024 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001025}
Russell King24056f52011-01-03 11:29:28 +00001026
Russell King022ae532011-07-08 21:26:59 +01001027/*
1028 * Return whether the given device DMA address mask can be supported
1029 * properly. For example, if your device can only drive the low 24-bits
1030 * during bus mastering, then you would pass 0x00ffffff as the mask
1031 * to this function.
1032 */
1033int dma_supported(struct device *dev, u64 mask)
1034{
Russell King9f28cde2013-12-06 12:30:42 +00001035 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001036}
1037EXPORT_SYMBOL(dma_supported);
1038
Gregory CLEMENT87b54e72012-11-21 09:39:19 +01001039int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +01001040{
1041 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1042 return -EIO;
1043
Russell King022ae532011-07-08 21:26:59 +01001044 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +01001045
1046 return 0;
1047}
Russell King022ae532011-07-08 21:26:59 +01001048
Russell King24056f52011-01-03 11:29:28 +00001049#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1050
1051static int __init dma_debug_do_init(void)
1052{
1053 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1054 return 0;
1055}
1056fs_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001057
1058#ifdef CONFIG_ARM_DMA_USE_IOMMU
1059
1060/* IOMMU */
1061
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001062static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1063
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001064static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1065 size_t size)
1066{
1067 unsigned int order = get_order(size);
1068 unsigned int align = 0;
1069 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301070 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001071 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001072 dma_addr_t iova;
1073 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001074
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001075 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1076 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1077
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001078 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1079 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001080
1081 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001082 for (i = 0; i < mapping->nr_bitmaps; i++) {
1083 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1084 mapping->bits, 0, count, align);
1085
1086 if (start > mapping->bits)
1087 continue;
1088
1089 bitmap_set(mapping->bitmaps[i], start, count);
1090 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001091 }
1092
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001093 /*
1094 * No unused range found. Try to extend the existing mapping
1095 * and perform a second attempt to reserve an IO virtual
1096 * address range of size bytes.
1097 */
1098 if (i == mapping->nr_bitmaps) {
1099 if (extend_iommu_mapping(mapping)) {
1100 spin_unlock_irqrestore(&mapping->lock, flags);
1101 return DMA_ERROR_CODE;
1102 }
1103
1104 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1105 mapping->bits, 0, count, align);
1106
1107 if (start > mapping->bits) {
1108 spin_unlock_irqrestore(&mapping->lock, flags);
1109 return DMA_ERROR_CODE;
1110 }
1111
1112 bitmap_set(mapping->bitmaps[i], start, count);
1113 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001114 spin_unlock_irqrestore(&mapping->lock, flags);
1115
Ritesh Harjani006f8412014-05-20 10:02:59 +05301116 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001117 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001118
1119 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001120}
1121
1122static inline void __free_iova(struct dma_iommu_mapping *mapping,
1123 dma_addr_t addr, size_t size)
1124{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001125 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301126 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001127 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001128 dma_addr_t bitmap_base;
1129 u32 bitmap_index;
1130
1131 if (!size)
1132 return;
1133
Ritesh Harjani006f8412014-05-20 10:02:59 +05301134 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001135 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1136
Ritesh Harjani006f8412014-05-20 10:02:59 +05301137 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001138
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001139 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001140
Ritesh Harjani006f8412014-05-20 10:02:59 +05301141 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001142 /*
1143 * The address range to be freed reaches into the iova
1144 * range of the next bitmap. This should not happen as
1145 * we don't allow this in __alloc_iova (at the
1146 * moment).
1147 */
1148 BUG();
1149 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001150 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001151
1152 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001153 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001154 spin_unlock_irqrestore(&mapping->lock, flags);
1155}
1156
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001157static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1158 gfp_t gfp, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001159{
1160 struct page **pages;
1161 int count = size >> PAGE_SHIFT;
1162 int array_size = count * sizeof(struct page *);
1163 int i = 0;
1164
1165 if (array_size <= PAGE_SIZE)
1166 pages = kzalloc(array_size, gfp);
1167 else
1168 pages = vzalloc(array_size);
1169 if (!pages)
1170 return NULL;
1171
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001172 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
1173 {
1174 unsigned long order = get_order(size);
1175 struct page *page;
1176
1177 page = dma_alloc_from_contiguous(dev, count, order);
1178 if (!page)
1179 goto error;
1180
1181 __dma_clear_buffer(page, size);
1182
1183 for (i = 0; i < count; i++)
1184 pages[i] = page + i;
1185
1186 return pages;
1187 }
1188
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001189 /*
1190 * IOMMU can map any pages, so himem can also be used here
1191 */
1192 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1193
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001194 while (count) {
Marek Szyprowski593f4732012-06-21 11:48:11 +02001195 int j, order = __fls(count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001196
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001197 pages[i] = alloc_pages(gfp, order);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001198 while (!pages[i] && order)
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001199 pages[i] = alloc_pages(gfp, --order);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001200 if (!pages[i])
1201 goto error;
1202
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001203 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001204 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001205 j = 1 << order;
1206 while (--j)
1207 pages[i + j] = pages[i] + j;
1208 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001209
1210 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
1211 i += 1 << order;
1212 count -= 1 << order;
1213 }
1214
1215 return pages;
1216error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001217 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001218 if (pages[i])
1219 __free_pages(pages[i], 0);
Prathyush K46c87852012-07-16 08:59:55 +02001220 if (array_size <= PAGE_SIZE)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001221 kfree(pages);
1222 else
1223 vfree(pages);
1224 return NULL;
1225}
1226
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001227static int __iommu_free_buffer(struct device *dev, struct page **pages,
1228 size_t size, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001229{
1230 int count = size >> PAGE_SHIFT;
1231 int array_size = count * sizeof(struct page *);
1232 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001233
1234 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
1235 dma_release_from_contiguous(dev, pages[0], count);
1236 } else {
1237 for (i = 0; i < count; i++)
1238 if (pages[i])
1239 __free_pages(pages[i], 0);
1240 }
1241
Prathyush K46c87852012-07-16 08:59:55 +02001242 if (array_size <= PAGE_SIZE)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001243 kfree(pages);
1244 else
1245 vfree(pages);
1246 return 0;
1247}
1248
1249/*
1250 * Create a CPU mapping for a specified pages
1251 */
1252static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001253__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1254 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001255{
Laura Abbott513510d2014-10-09 15:26:40 -07001256 return dma_common_pages_remap(pages, size,
1257 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001258 return NULL;
1259}
1260
1261/*
1262 * Create a mapping in device IO address space for specified pages
1263 */
1264static dma_addr_t
1265__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1266{
1267 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1268 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1269 dma_addr_t dma_addr, iova;
1270 int i, ret = DMA_ERROR_CODE;
1271
1272 dma_addr = __alloc_iova(mapping, size);
1273 if (dma_addr == DMA_ERROR_CODE)
1274 return dma_addr;
1275
1276 iova = dma_addr;
1277 for (i = 0; i < count; ) {
1278 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1279 phys_addr_t phys = page_to_phys(pages[i]);
1280 unsigned int len, j;
1281
1282 for (j = i + 1; j < count; j++, next_pfn++)
1283 if (page_to_pfn(pages[j]) != next_pfn)
1284 break;
1285
1286 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001287 ret = iommu_map(mapping->domain, iova, phys, len,
1288 IOMMU_READ|IOMMU_WRITE);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001289 if (ret < 0)
1290 goto fail;
1291 iova += len;
1292 i = j;
1293 }
1294 return dma_addr;
1295fail:
1296 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1297 __free_iova(mapping, dma_addr, size);
1298 return DMA_ERROR_CODE;
1299}
1300
1301static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1302{
1303 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1304
1305 /*
1306 * add optional in-page offset from iova to size and align
1307 * result to page size
1308 */
1309 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1310 iova &= PAGE_MASK;
1311
1312 iommu_unmap(mapping->domain, iova, size);
1313 __free_iova(mapping, iova, size);
1314 return 0;
1315}
1316
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001317static struct page **__atomic_get_pages(void *addr)
1318{
1319 struct dma_pool *pool = &atomic_pool;
1320 struct page **pages = pool->pages;
1321 int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
1322
1323 return pages + offs;
1324}
1325
Marek Szyprowski955c7572012-05-16 19:38:58 +01001326static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001327{
1328 struct vm_struct *area;
1329
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001330 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1331 return __atomic_get_pages(cpu_addr);
1332
Marek Szyprowski955c7572012-05-16 19:38:58 +01001333 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1334 return cpu_addr;
1335
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001336 area = find_vm_area(cpu_addr);
1337 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1338 return area->pages;
1339 return NULL;
1340}
1341
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001342static void *__iommu_alloc_atomic(struct device *dev, size_t size,
1343 dma_addr_t *handle)
1344{
1345 struct page *page;
1346 void *addr;
1347
1348 addr = __alloc_from_pool(size, &page);
1349 if (!addr)
1350 return NULL;
1351
1352 *handle = __iommu_create_mapping(dev, &page, size);
1353 if (*handle == DMA_ERROR_CODE)
1354 goto err_mapping;
1355
1356 return addr;
1357
1358err_mapping:
1359 __free_from_pool(addr, size);
1360 return NULL;
1361}
1362
Marek Szyprowskid5898292013-02-08 10:54:48 +01001363static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001364 dma_addr_t handle, size_t size)
1365{
1366 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowskid5898292013-02-08 10:54:48 +01001367 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001368}
1369
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001370static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1371 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1372{
Russell King71b55662013-11-25 12:01:03 +00001373 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001374 struct page **pages;
1375 void *addr = NULL;
1376
1377 *handle = DMA_ERROR_CODE;
1378 size = PAGE_ALIGN(size);
1379
Marek Szyprowski10c85622014-01-16 15:39:17 +01001380 if (!(gfp & __GFP_WAIT))
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001381 return __iommu_alloc_atomic(dev, size, handle);
1382
Richard Zhao5b91a982013-06-20 20:31:00 +08001383 /*
1384 * Following is a work-around (a.k.a. hack) to prevent pages
1385 * with __GFP_COMP being passed to split_page() which cannot
1386 * handle them. The real problem is that this flag probably
1387 * should be 0 on ARM as it is not supported on this
1388 * platform; see CONFIG_HUGETLBFS.
1389 */
1390 gfp &= ~(__GFP_COMP);
1391
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001392 pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001393 if (!pages)
1394 return NULL;
1395
1396 *handle = __iommu_create_mapping(dev, pages, size);
1397 if (*handle == DMA_ERROR_CODE)
1398 goto err_buffer;
1399
Marek Szyprowski955c7572012-05-16 19:38:58 +01001400 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1401 return pages;
1402
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001403 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1404 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001405 if (!addr)
1406 goto err_mapping;
1407
1408 return addr;
1409
1410err_mapping:
1411 __iommu_remove_mapping(dev, *handle, size);
1412err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001413 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001414 return NULL;
1415}
1416
1417static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1418 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1419 struct dma_attrs *attrs)
1420{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001421 unsigned long uaddr = vma->vm_start;
1422 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001423 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001424
1425 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001426
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001427 if (!pages)
1428 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001429
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001430 do {
1431 int ret = vm_insert_page(vma, uaddr, *pages++);
1432 if (ret) {
1433 pr_err("Remapping memory failed: %d\n", ret);
1434 return ret;
1435 }
1436 uaddr += PAGE_SIZE;
1437 usize -= PAGE_SIZE;
1438 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001439
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001440 return 0;
1441}
1442
1443/*
1444 * free a page as defined by the above mapping.
1445 * Must not be called with IRQs disabled.
1446 */
1447void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1448 dma_addr_t handle, struct dma_attrs *attrs)
1449{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001450 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001451 size = PAGE_ALIGN(size);
1452
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001453 if (__in_atomic_pool(cpu_addr, size)) {
Marek Szyprowskid5898292013-02-08 10:54:48 +01001454 __iommu_free_atomic(dev, cpu_addr, handle, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001455 return;
1456 }
1457
YoungJun Cho836bfa02013-06-17 13:18:52 +09001458 pages = __iommu_get_pages(cpu_addr, attrs);
1459 if (!pages) {
1460 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1461 return;
1462 }
1463
Marek Szyprowski955c7572012-05-16 19:38:58 +01001464 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
Laura Abbott513510d2014-10-09 15:26:40 -07001465 dma_common_free_remap(cpu_addr, size,
1466 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001467 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001468
1469 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001470 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001471}
1472
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001473static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1474 void *cpu_addr, dma_addr_t dma_addr,
1475 size_t size, struct dma_attrs *attrs)
1476{
1477 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1478 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1479
1480 if (!pages)
1481 return -ENXIO;
1482
1483 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1484 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001485}
1486
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001487static int __dma_direction_to_prot(enum dma_data_direction dir)
1488{
1489 int prot;
1490
1491 switch (dir) {
1492 case DMA_BIDIRECTIONAL:
1493 prot = IOMMU_READ | IOMMU_WRITE;
1494 break;
1495 case DMA_TO_DEVICE:
1496 prot = IOMMU_READ;
1497 break;
1498 case DMA_FROM_DEVICE:
1499 prot = IOMMU_WRITE;
1500 break;
1501 default:
1502 prot = 0;
1503 }
1504
1505 return prot;
1506}
1507
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001508/*
1509 * Map a part of the scatter-gather list into contiguous io address space
1510 */
1511static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1512 size_t size, dma_addr_t *handle,
Rob Herring0fa478d2012-08-21 12:23:23 +02001513 enum dma_data_direction dir, struct dma_attrs *attrs,
1514 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001515{
1516 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1517 dma_addr_t iova, iova_base;
1518 int ret = 0;
1519 unsigned int count;
1520 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001521 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001522
1523 size = PAGE_ALIGN(size);
1524 *handle = DMA_ERROR_CODE;
1525
1526 iova_base = iova = __alloc_iova(mapping, size);
1527 if (iova == DMA_ERROR_CODE)
1528 return -ENOMEM;
1529
1530 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1531 phys_addr_t phys = page_to_phys(sg_page(s));
1532 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1533
Rob Herring0fa478d2012-08-21 12:23:23 +02001534 if (!is_coherent &&
1535 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001536 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1537
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001538 prot = __dma_direction_to_prot(dir);
1539
1540 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001541 if (ret < 0)
1542 goto fail;
1543 count += len >> PAGE_SHIFT;
1544 iova += len;
1545 }
1546 *handle = iova_base;
1547
1548 return 0;
1549fail:
1550 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1551 __free_iova(mapping, iova_base, size);
1552 return ret;
1553}
1554
Rob Herring0fa478d2012-08-21 12:23:23 +02001555static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1556 enum dma_data_direction dir, struct dma_attrs *attrs,
1557 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001558{
1559 struct scatterlist *s = sg, *dma = sg, *start = sg;
1560 int i, count = 0;
1561 unsigned int offset = s->offset;
1562 unsigned int size = s->offset + s->length;
1563 unsigned int max = dma_get_max_seg_size(dev);
1564
1565 for (i = 1; i < nents; i++) {
1566 s = sg_next(s);
1567
1568 s->dma_address = DMA_ERROR_CODE;
1569 s->dma_length = 0;
1570
1571 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1572 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001573 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001574 goto bad_mapping;
1575
1576 dma->dma_address += offset;
1577 dma->dma_length = size - offset;
1578
1579 size = offset = s->offset;
1580 start = s;
1581 dma = sg_next(dma);
1582 count += 1;
1583 }
1584 size += s->length;
1585 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001586 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1587 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001588 goto bad_mapping;
1589
1590 dma->dma_address += offset;
1591 dma->dma_length = size - offset;
1592
1593 return count+1;
1594
1595bad_mapping:
1596 for_each_sg(sg, s, count, i)
1597 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1598 return 0;
1599}
1600
1601/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001602 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1603 * @dev: valid struct device pointer
1604 * @sg: list of buffers
1605 * @nents: number of buffers to map
1606 * @dir: DMA transfer direction
1607 *
1608 * Map a set of i/o coherent buffers described by scatterlist in streaming
1609 * mode for DMA. The scatter gather list elements are merged together (if
1610 * possible) and tagged with the appropriate dma address and length. They are
1611 * obtained via sg_dma_{address,length}.
1612 */
1613int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1614 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1615{
1616 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1617}
1618
1619/**
1620 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1621 * @dev: valid struct device pointer
1622 * @sg: list of buffers
1623 * @nents: number of buffers to map
1624 * @dir: DMA transfer direction
1625 *
1626 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1627 * The scatter gather list elements are merged together (if possible) and
1628 * tagged with the appropriate dma address and length. They are obtained via
1629 * sg_dma_{address,length}.
1630 */
1631int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1632 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1633{
1634 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1635}
1636
1637static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1638 int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
1639 bool is_coherent)
1640{
1641 struct scatterlist *s;
1642 int i;
1643
1644 for_each_sg(sg, s, nents, i) {
1645 if (sg_dma_len(s))
1646 __iommu_remove_mapping(dev, sg_dma_address(s),
1647 sg_dma_len(s));
1648 if (!is_coherent &&
1649 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1650 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1651 s->length, dir);
1652 }
1653}
1654
1655/**
1656 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1657 * @dev: valid struct device pointer
1658 * @sg: list of buffers
1659 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1660 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1661 *
1662 * Unmap a set of streaming mode DMA translations. Again, CPU access
1663 * rules concerning calls here are the same as for dma_unmap_single().
1664 */
1665void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1666 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1667{
1668 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1669}
1670
1671/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001672 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1673 * @dev: valid struct device pointer
1674 * @sg: list of buffers
1675 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1676 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1677 *
1678 * Unmap a set of streaming mode DMA translations. Again, CPU access
1679 * rules concerning calls here are the same as for dma_unmap_single().
1680 */
1681void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1682 enum dma_data_direction dir, struct dma_attrs *attrs)
1683{
Rob Herring0fa478d2012-08-21 12:23:23 +02001684 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001685}
1686
1687/**
1688 * arm_iommu_sync_sg_for_cpu
1689 * @dev: valid struct device pointer
1690 * @sg: list of buffers
1691 * @nents: number of buffers to map (returned from dma_map_sg)
1692 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1693 */
1694void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1695 int nents, enum dma_data_direction dir)
1696{
1697 struct scatterlist *s;
1698 int i;
1699
1700 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001701 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001702
1703}
1704
1705/**
1706 * arm_iommu_sync_sg_for_device
1707 * @dev: valid struct device pointer
1708 * @sg: list of buffers
1709 * @nents: number of buffers to map (returned from dma_map_sg)
1710 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1711 */
1712void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1713 int nents, enum dma_data_direction dir)
1714{
1715 struct scatterlist *s;
1716 int i;
1717
1718 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001719 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001720}
1721
1722
1723/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001724 * arm_coherent_iommu_map_page
1725 * @dev: valid struct device pointer
1726 * @page: page that buffer resides in
1727 * @offset: offset into page for start of buffer
1728 * @size: size of buffer to map
1729 * @dir: DMA transfer direction
1730 *
1731 * Coherent IOMMU aware version of arm_dma_map_page()
1732 */
1733static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1734 unsigned long offset, size_t size, enum dma_data_direction dir,
1735 struct dma_attrs *attrs)
1736{
1737 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1738 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001739 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001740
1741 dma_addr = __alloc_iova(mapping, len);
1742 if (dma_addr == DMA_ERROR_CODE)
1743 return dma_addr;
1744
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001745 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01001746
1747 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001748 if (ret < 0)
1749 goto fail;
1750
1751 return dma_addr + offset;
1752fail:
1753 __free_iova(mapping, dma_addr, len);
1754 return DMA_ERROR_CODE;
1755}
1756
1757/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001758 * arm_iommu_map_page
1759 * @dev: valid struct device pointer
1760 * @page: page that buffer resides in
1761 * @offset: offset into page for start of buffer
1762 * @size: size of buffer to map
1763 * @dir: DMA transfer direction
1764 *
1765 * IOMMU aware version of arm_dma_map_page()
1766 */
1767static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1768 unsigned long offset, size_t size, enum dma_data_direction dir,
1769 struct dma_attrs *attrs)
1770{
Rob Herring0fa478d2012-08-21 12:23:23 +02001771 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001772 __dma_page_cpu_to_dev(page, offset, size, dir);
1773
Rob Herring0fa478d2012-08-21 12:23:23 +02001774 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1775}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001776
Rob Herring0fa478d2012-08-21 12:23:23 +02001777/**
1778 * arm_coherent_iommu_unmap_page
1779 * @dev: valid struct device pointer
1780 * @handle: DMA address of buffer
1781 * @size: size of buffer (same as passed to dma_map_page)
1782 * @dir: DMA transfer direction (same as passed to dma_map_page)
1783 *
1784 * Coherent IOMMU aware version of arm_dma_unmap_page()
1785 */
1786static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1787 size_t size, enum dma_data_direction dir,
1788 struct dma_attrs *attrs)
1789{
1790 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1791 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001792 int offset = handle & ~PAGE_MASK;
1793 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001794
Rob Herring0fa478d2012-08-21 12:23:23 +02001795 if (!iova)
1796 return;
1797
1798 iommu_unmap(mapping->domain, iova, len);
1799 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001800}
1801
1802/**
1803 * arm_iommu_unmap_page
1804 * @dev: valid struct device pointer
1805 * @handle: DMA address of buffer
1806 * @size: size of buffer (same as passed to dma_map_page)
1807 * @dir: DMA transfer direction (same as passed to dma_map_page)
1808 *
1809 * IOMMU aware version of arm_dma_unmap_page()
1810 */
1811static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1812 size_t size, enum dma_data_direction dir,
1813 struct dma_attrs *attrs)
1814{
1815 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1816 dma_addr_t iova = handle & PAGE_MASK;
1817 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1818 int offset = handle & ~PAGE_MASK;
1819 int len = PAGE_ALIGN(size + offset);
1820
1821 if (!iova)
1822 return;
1823
Rob Herring0fa478d2012-08-21 12:23:23 +02001824 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001825 __dma_page_dev_to_cpu(page, offset, size, dir);
1826
1827 iommu_unmap(mapping->domain, iova, len);
1828 __free_iova(mapping, iova, len);
1829}
1830
1831static void arm_iommu_sync_single_for_cpu(struct device *dev,
1832 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1833{
1834 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1835 dma_addr_t iova = handle & PAGE_MASK;
1836 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1837 unsigned int offset = handle & ~PAGE_MASK;
1838
1839 if (!iova)
1840 return;
1841
Rob Herring0fa478d2012-08-21 12:23:23 +02001842 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001843}
1844
1845static void arm_iommu_sync_single_for_device(struct device *dev,
1846 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1847{
1848 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1849 dma_addr_t iova = handle & PAGE_MASK;
1850 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1851 unsigned int offset = handle & ~PAGE_MASK;
1852
1853 if (!iova)
1854 return;
1855
1856 __dma_page_cpu_to_dev(page, offset, size, dir);
1857}
1858
1859struct dma_map_ops iommu_ops = {
1860 .alloc = arm_iommu_alloc_attrs,
1861 .free = arm_iommu_free_attrs,
1862 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001863 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001864
1865 .map_page = arm_iommu_map_page,
1866 .unmap_page = arm_iommu_unmap_page,
1867 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
1868 .sync_single_for_device = arm_iommu_sync_single_for_device,
1869
1870 .map_sg = arm_iommu_map_sg,
1871 .unmap_sg = arm_iommu_unmap_sg,
1872 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
1873 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Hiroshi Doyud09e1332013-01-29 14:57:07 +02001874
1875 .set_dma_mask = arm_dma_set_mask,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001876};
1877
Rob Herring0fa478d2012-08-21 12:23:23 +02001878struct dma_map_ops iommu_coherent_ops = {
1879 .alloc = arm_iommu_alloc_attrs,
1880 .free = arm_iommu_free_attrs,
1881 .mmap = arm_iommu_mmap_attrs,
1882 .get_sgtable = arm_iommu_get_sgtable,
1883
1884 .map_page = arm_coherent_iommu_map_page,
1885 .unmap_page = arm_coherent_iommu_unmap_page,
1886
1887 .map_sg = arm_coherent_iommu_map_sg,
1888 .unmap_sg = arm_coherent_iommu_unmap_sg,
Hiroshi Doyud09e1332013-01-29 14:57:07 +02001889
1890 .set_dma_mask = arm_dma_set_mask,
Rob Herring0fa478d2012-08-21 12:23:23 +02001891};
1892
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001893/**
1894 * arm_iommu_create_mapping
1895 * @bus: pointer to the bus holding the client device (for IOMMU calls)
1896 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001897 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001898 *
1899 * Creates a mapping structure which holds information about used/unused
1900 * IO address ranges, which is required to perform memory allocation and
1901 * mapping with IOMMU aware functions.
1902 *
1903 * The client device need to be attached to the mapping with
1904 * arm_iommu_attach_device function.
1905 */
1906struct dma_iommu_mapping *
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001907arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001908{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001909 unsigned int bits = size >> PAGE_SHIFT;
1910 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001911 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001912 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001913 int err = -ENOMEM;
1914
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001915 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001916 return ERR_PTR(-EINVAL);
1917
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001918 if (bitmap_size > PAGE_SIZE) {
1919 extensions = bitmap_size / PAGE_SIZE;
1920 bitmap_size = PAGE_SIZE;
1921 }
1922
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001923 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1924 if (!mapping)
1925 goto err;
1926
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001927 mapping->bitmap_size = bitmap_size;
1928 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001929 GFP_KERNEL);
1930 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001931 goto err2;
1932
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001933 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001934 if (!mapping->bitmaps[0])
1935 goto err3;
1936
1937 mapping->nr_bitmaps = 1;
1938 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001939 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001940 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001941
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001942 spin_lock_init(&mapping->lock);
1943
1944 mapping->domain = iommu_domain_alloc(bus);
1945 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001946 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001947
1948 kref_init(&mapping->kref);
1949 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001950err4:
1951 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001952err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001953 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001954err2:
1955 kfree(mapping);
1956err:
1957 return ERR_PTR(err);
1958}
Prathyush K18177d12013-01-04 06:22:42 -05001959EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001960
1961static void release_iommu_mapping(struct kref *kref)
1962{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001963 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001964 struct dma_iommu_mapping *mapping =
1965 container_of(kref, struct dma_iommu_mapping, kref);
1966
1967 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001968 for (i = 0; i < mapping->nr_bitmaps; i++)
1969 kfree(mapping->bitmaps[i]);
1970 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001971 kfree(mapping);
1972}
1973
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001974static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
1975{
1976 int next_bitmap;
1977
1978 if (mapping->nr_bitmaps > mapping->extensions)
1979 return -EINVAL;
1980
1981 next_bitmap = mapping->nr_bitmaps;
1982 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
1983 GFP_ATOMIC);
1984 if (!mapping->bitmaps[next_bitmap])
1985 return -ENOMEM;
1986
1987 mapping->nr_bitmaps++;
1988
1989 return 0;
1990}
1991
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001992void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1993{
1994 if (mapping)
1995 kref_put(&mapping->kref, release_iommu_mapping);
1996}
Prathyush K18177d12013-01-04 06:22:42 -05001997EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001998
1999/**
2000 * arm_iommu_attach_device
2001 * @dev: valid struct device pointer
2002 * @mapping: io address space mapping structure (returned from
2003 * arm_iommu_create_mapping)
2004 *
2005 * Attaches specified io address space mapping to the provided device,
2006 * this replaces the dma operations (dma_map_ops pointer) with the
2007 * IOMMU aware version. More than one client might be attached to
2008 * the same io address space mapping.
2009 */
2010int arm_iommu_attach_device(struct device *dev,
2011 struct dma_iommu_mapping *mapping)
2012{
2013 int err;
2014
2015 err = iommu_attach_device(mapping->domain, dev);
2016 if (err)
2017 return err;
2018
2019 kref_get(&mapping->kref);
2020 dev->archdata.mapping = mapping;
2021 set_dma_ops(dev, &iommu_ops);
2022
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002023 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002024 return 0;
2025}
Prathyush K18177d12013-01-04 06:22:42 -05002026EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002027
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002028/**
2029 * arm_iommu_detach_device
2030 * @dev: valid struct device pointer
2031 *
2032 * Detaches the provided device from a previously attached map.
2033 * This voids the dma operations (dma_map_ops pointer)
2034 */
2035void arm_iommu_detach_device(struct device *dev)
2036{
2037 struct dma_iommu_mapping *mapping;
2038
2039 mapping = to_dma_iommu_mapping(dev);
2040 if (!mapping) {
2041 dev_warn(dev, "Not attached\n");
2042 return;
2043 }
2044
2045 iommu_detach_device(mapping->domain, dev);
2046 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon9e4b2592013-06-10 19:34:40 +01002047 dev->archdata.mapping = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002048 set_dma_ops(dev, NULL);
2049
2050 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2051}
Prathyush K18177d12013-01-04 06:22:42 -05002052EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002053
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002054#endif