blob: cba12f34ff774ca08216c5d2b6061b07b68b1a99 [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>
Laura Abbott36d0fd22014-10-09 15:26:42 -070015#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/list.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010022#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050023#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010024#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010025#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020026#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020027#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020028#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010029#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070030#include <linux/cma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010032#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040033#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010036#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020037#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010038#include <asm/mach/map.h>
39#include <asm/system_info.h>
40#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Russell King022ae532011-07-08 21:26:59 +010042#include "mm.h"
43
Marek Szyprowski15237e12012-02-10 19:55:20 +010044/*
45 * The DMA API is built upon the notion of "buffer ownership". A buffer
46 * is either exclusively owned by the CPU (and therefore may be accessed
47 * by it) or exclusively owned by the DMA device. These helper functions
48 * represent the transitions between these two ownership states.
49 *
50 * Note, however, that on later ARMs, this notion does not work due to
51 * speculative prefetches. We model our approach on the assumption that
52 * the CPU does do speculative prefetches, which means we clean caches
53 * before transfers and delay cache invalidation until transfer completion.
54 *
Marek Szyprowski15237e12012-02-10 19:55:20 +010055 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010056static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010057 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +010058static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010059 size_t, enum dma_data_direction);
60
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010061/**
62 * arm_dma_map_page - map a portion of a page for streaming DMA
63 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
64 * @page: page that buffer resides in
65 * @offset: offset into page for start of buffer
66 * @size: size of buffer to map
67 * @dir: DMA transfer direction
68 *
69 * Ensure that any data held in the cache is appropriately discarded
70 * or written back.
71 *
72 * The device owns this memory once this call has completed. The CPU
73 * can regain ownership by calling dma_unmap_page().
74 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010075static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010076 unsigned long offset, size_t size, enum dma_data_direction dir,
77 struct dma_attrs *attrs)
78{
Rob Herringdd37e942012-08-21 12:20:17 +020079 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +010080 __dma_page_cpu_to_dev(page, offset, size, dir);
81 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010082}
83
Rob Herringdd37e942012-08-21 12:20:17 +020084static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
85 unsigned long offset, size_t size, enum dma_data_direction dir,
86 struct dma_attrs *attrs)
87{
88 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
89}
90
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010091/**
92 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
93 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
94 * @handle: DMA address of buffer
95 * @size: size of buffer (same as passed to dma_map_page)
96 * @dir: DMA transfer direction (same as passed to dma_map_page)
97 *
98 * Unmap a page streaming mode DMA translation. The handle and size
99 * must match what was provided in the previous dma_map_page() call.
100 * All other usages are undefined.
101 *
102 * After this call, reads by the CPU to the buffer are guaranteed to see
103 * whatever the device wrote there.
104 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100105static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100106 size_t size, enum dma_data_direction dir,
107 struct dma_attrs *attrs)
108{
Rob Herringdd37e942012-08-21 12:20:17 +0200109 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100110 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
111 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100112}
113
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100114static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100115 dma_addr_t handle, size_t size, enum dma_data_direction dir)
116{
117 unsigned int offset = handle & (PAGE_SIZE - 1);
118 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200119 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100120}
121
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100122static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100123 dma_addr_t handle, size_t size, enum dma_data_direction dir)
124{
125 unsigned int offset = handle & (PAGE_SIZE - 1);
126 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200127 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100128}
129
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100130struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200131 .alloc = arm_dma_alloc,
132 .free = arm_dma_free,
133 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200134 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100135 .map_page = arm_dma_map_page,
136 .unmap_page = arm_dma_unmap_page,
137 .map_sg = arm_dma_map_sg,
138 .unmap_sg = arm_dma_unmap_sg,
139 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
140 .sync_single_for_device = arm_dma_sync_single_for_device,
141 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
142 .sync_sg_for_device = arm_dma_sync_sg_for_device,
143 .set_dma_mask = arm_dma_set_mask,
144};
145EXPORT_SYMBOL(arm_dma_ops);
146
Rob Herringdd37e942012-08-21 12:20:17 +0200147static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
148 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
149static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
150 dma_addr_t handle, struct dma_attrs *attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100151static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
152 void *cpu_addr, dma_addr_t dma_addr, size_t size,
153 struct dma_attrs *attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200154
155struct dma_map_ops arm_coherent_dma_ops = {
156 .alloc = arm_coherent_dma_alloc,
157 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100158 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200159 .get_sgtable = arm_dma_get_sgtable,
160 .map_page = arm_coherent_dma_map_page,
161 .map_sg = arm_dma_map_sg,
162 .set_dma_mask = arm_dma_set_mask,
163};
164EXPORT_SYMBOL(arm_coherent_dma_ops);
165
Russell King9f28cde2013-12-06 12:30:42 +0000166static int __dma_supported(struct device *dev, u64 mask, bool warn)
167{
168 unsigned long max_dma_pfn;
169
170 /*
171 * If the mask allows for more memory than we can address,
172 * and we actually have that much memory, then we must
173 * indicate that DMA to this device is not supported.
174 */
175 if (sizeof(mask) != sizeof(dma_addr_t) &&
176 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000177 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000178 if (warn) {
179 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
180 mask);
181 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
182 }
183 return 0;
184 }
185
186 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
187
188 /*
189 * Translate the device's DMA mask to a PFN limit. This
190 * PFN number includes the page which we can DMA to.
191 */
192 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
193 if (warn)
194 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",
195 mask,
196 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
197 max_dma_pfn + 1);
198 return 0;
199 }
200
201 return 1;
202}
203
Catalin Marinasab6494f2009-07-24 12:35:02 +0100204static u64 get_coherent_dma_mask(struct device *dev)
205{
Russell King4dcfa602013-07-09 12:14:49 +0100206 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Catalin Marinasab6494f2009-07-24 12:35:02 +0100208 if (dev) {
209 mask = dev->coherent_dma_mask;
210
211 /*
212 * Sanity check the DMA mask - it must be non-zero, and
213 * must be able to be satisfied by a DMA allocation.
214 */
215 if (mask == 0) {
216 dev_warn(dev, "coherent DMA mask is unset\n");
217 return 0;
218 }
219
Russell King9f28cde2013-12-06 12:30:42 +0000220 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100221 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100222 }
223
224 return mask;
225}
226
Marek Szyprowskic7909502011-12-29 13:09:51 +0100227static void __dma_clear_buffer(struct page *page, size_t size)
228{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100229 /*
230 * Ensure that the allocated pages are zeroed, and that any data
231 * lurking in the kernel direct-mapped region is invalidated.
232 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100233 if (PageHighMem(page)) {
234 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
235 phys_addr_t end = base + size;
236 while (size > 0) {
237 void *ptr = kmap_atomic(page);
238 memset(ptr, 0, PAGE_SIZE);
239 dmac_flush_range(ptr, ptr + PAGE_SIZE);
240 kunmap_atomic(ptr);
241 page++;
242 size -= PAGE_SIZE;
243 }
244 outer_flush_range(base, end);
245 } else {
246 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200247 memset(ptr, 0, size);
248 dmac_flush_range(ptr, ptr + size);
249 outer_flush_range(__pa(ptr), __pa(ptr) + size);
250 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100251}
252
Russell King7a9a32a2009-11-19 15:31:07 +0000253/*
254 * Allocate a DMA buffer for 'dev' of size 'size' using the
255 * specified gfp mask. Note that 'size' must be page aligned.
256 */
257static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
258{
259 unsigned long order = get_order(size);
260 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000261
262 page = alloc_pages(gfp, order);
263 if (!page)
264 return NULL;
265
266 /*
267 * Now split the huge page and free the excess pages
268 */
269 split_page(page, order);
270 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
271 __free_page(p);
272
Marek Szyprowskic7909502011-12-29 13:09:51 +0100273 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000274
275 return page;
276}
277
278/*
279 * Free a DMA buffer. 'size' must be page aligned.
280 */
281static void __dma_free_buffer(struct page *page, size_t size)
282{
283 struct page *e = page + (size >> PAGE_SHIFT);
284
285 while (page < e) {
286 __free_page(page);
287 page++;
288 }
289}
290
Catalin Marinasab6494f2009-07-24 12:35:02 +0100291#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Marek Szyprowskic7909502011-12-29 13:09:51 +0100293static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100294 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100295 const void *caller, bool want_vaddr);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100296
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200297static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
298 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100299 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200300
301static void *
302__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
303 const void *caller)
304{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200305 /*
306 * DMA allocation can be mapped to user space, so lets
307 * set VM_USERMAP flags too.
308 */
Laura Abbott513510d2014-10-09 15:26:40 -0700309 return dma_common_contiguous_remap(page, size,
310 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
311 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200312}
313
314static void __dma_free_remap(void *cpu_addr, size_t size)
315{
Laura Abbott513510d2014-10-09 15:26:40 -0700316 dma_common_free_remap(cpu_addr, size,
317 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200318}
319
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200320#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700321static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200322
Laura Abbott36d0fd22014-10-09 15:26:42 -0700323static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100324
325static int __init early_coherent_pool(char *p)
326{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700327 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100328 return 0;
329}
330early_param("coherent_pool", early_coherent_pool);
331
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200332void __init init_dma_coherent_pool_size(unsigned long size)
333{
334 /*
335 * Catch any attempt to set the pool size too late.
336 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700337 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200338
339 /*
340 * Set architecture specific coherent pool size only if
341 * it has not been changed by kernel command line parameter.
342 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700343 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
344 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200345}
346
Marek Szyprowskic7909502011-12-29 13:09:51 +0100347/*
348 * Initialise the coherent pool for atomic allocations.
349 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200350static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100351{
Russell King71b55662013-11-25 12:01:03 +0000352 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100353 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100354 struct page *page;
355 void *ptr;
356
Laura Abbott36d0fd22014-10-09 15:26:42 -0700357 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
358 if (!atomic_pool)
359 goto out;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300360
Gioh Kime464ef12014-05-22 13:38:23 +0900361 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700362 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100363 &page, atomic_pool_init, true);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200364 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700365 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100366 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100367 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700368 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300369
Laura Abbott36d0fd22014-10-09 15:26:42 -0700370 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
371 page_to_phys(page),
372 atomic_pool_size, -1);
373 if (ret)
374 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300375
Laura Abbott36d0fd22014-10-09 15:26:42 -0700376 gen_pool_set_algo(atomic_pool,
377 gen_pool_first_fit_order_align,
378 (void *)PAGE_SHIFT);
379 pr_info("DMA: preallocated %zd KiB pool for atomic coherent allocations\n",
380 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100381 return 0;
382 }
Sachin Kamatec106652012-09-24 08:35:03 +0200383
Laura Abbott36d0fd22014-10-09 15:26:42 -0700384destroy_genpool:
385 gen_pool_destroy(atomic_pool);
386 atomic_pool = NULL;
387out:
388 pr_err("DMA: failed to allocate %zx KiB pool for atomic coherent allocation\n",
389 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100390 return -ENOMEM;
391}
392/*
393 * CMA is activated by core_initcall, so we must be called after it.
394 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200395postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100396
397struct dma_contig_early_reserve {
398 phys_addr_t base;
399 unsigned long size;
400};
401
402static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
403
404static int dma_mmu_remap_num __initdata;
405
406void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
407{
408 dma_mmu_remap[dma_mmu_remap_num].base = base;
409 dma_mmu_remap[dma_mmu_remap_num].size = size;
410 dma_mmu_remap_num++;
411}
412
413void __init dma_contiguous_remap(void)
414{
415 int i;
416 for (i = 0; i < dma_mmu_remap_num; i++) {
417 phys_addr_t start = dma_mmu_remap[i].base;
418 phys_addr_t end = start + dma_mmu_remap[i].size;
419 struct map_desc map;
420 unsigned long addr;
421
422 if (end > arm_lowmem_limit)
423 end = arm_lowmem_limit;
424 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200425 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100426
427 map.pfn = __phys_to_pfn(start);
428 map.virtual = __phys_to_virt(start);
429 map.length = end - start;
430 map.type = MT_MEMORY_DMA_READY;
431
432 /*
Russell King6b076992014-07-17 12:17:45 +0100433 * Clear previous low-memory mapping to ensure that the
434 * TLB does not see any conflicting entries, then flush
435 * the TLB of the old entries before creating new mappings.
436 *
437 * This ensures that any speculatively loaded TLB entries
438 * (even though they may be rare) can not cause any problems,
439 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100440 */
441 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400442 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100443 pmd_clear(pmd_off_k(addr));
444
Russell King6b076992014-07-17 12:17:45 +0100445 flush_tlb_kernel_range(__phys_to_virt(start),
446 __phys_to_virt(end));
447
Marek Szyprowskic7909502011-12-29 13:09:51 +0100448 iotable_init(&map, 1);
449 }
450}
451
Marek Szyprowskic7909502011-12-29 13:09:51 +0100452static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
453 void *data)
454{
455 struct page *page = virt_to_page(addr);
456 pgprot_t prot = *(pgprot_t *)data;
457
458 set_pte_ext(pte, mk_pte(page, prot), 0);
459 return 0;
460}
461
462static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
463{
464 unsigned long start = (unsigned long) page_address(page);
465 unsigned end = start + size;
466
467 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100468 flush_tlb_kernel_range(start, end);
469}
470
471static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
472 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100473 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100474{
475 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100476 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100477 page = __dma_alloc_buffer(dev, size, gfp);
478 if (!page)
479 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100480 if (!want_vaddr)
481 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100482
483 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
484 if (!ptr) {
485 __dma_free_buffer(page, size);
486 return NULL;
487 }
488
Carlo Caione6e8266e2015-02-09 10:38:35 +0100489 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100490 *ret_page = page;
491 return ptr;
492}
493
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200494static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100495{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700496 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200497 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100498
Laura Abbott36d0fd22014-10-09 15:26:42 -0700499 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200500 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100501 return NULL;
502 }
503
Laura Abbott36d0fd22014-10-09 15:26:42 -0700504 val = gen_pool_alloc(atomic_pool, size);
505 if (val) {
506 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200507
Laura Abbott36d0fd22014-10-09 15:26:42 -0700508 *ret_page = phys_to_page(phys);
509 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100510 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200511
512 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100513}
514
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300515static bool __in_atomic_pool(void *start, size_t size)
516{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700517 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300518}
519
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200520static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100521{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300522 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100523 return 0;
524
Laura Abbott36d0fd22014-10-09 15:26:42 -0700525 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200526
Marek Szyprowskic7909502011-12-29 13:09:51 +0100527 return 1;
528}
529
530static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100531 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100532 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100533{
534 unsigned long order = get_order(size);
535 size_t count = size >> PAGE_SHIFT;
536 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100537 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100538
539 page = dma_alloc_from_contiguous(dev, count, order);
540 if (!page)
541 return NULL;
542
543 __dma_clear_buffer(page, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100544
Carlo Caione6e8266e2015-02-09 10:38:35 +0100545 if (!want_vaddr)
546 goto out;
547
Marek Szyprowski9848e482013-01-16 15:38:44 +0100548 if (PageHighMem(page)) {
549 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
550 if (!ptr) {
551 dma_release_from_contiguous(dev, page, count);
552 return NULL;
553 }
554 } else {
555 __dma_remap(page, size, prot);
556 ptr = page_address(page);
557 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100558
559 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100560 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100561 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100562}
563
564static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100565 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100566{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100567 if (want_vaddr) {
568 if (PageHighMem(page))
569 __dma_free_remap(cpu_addr, size);
570 else
571 __dma_remap(page, size, PAGE_KERNEL);
572 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100573 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
574}
575
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200576static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
577{
578 prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
579 pgprot_writecombine(prot) :
580 pgprot_dmacoherent(prot);
581 return prot;
582}
583
Marek Szyprowskic7909502011-12-29 13:09:51 +0100584#define nommu() 0
585
Catalin Marinasab6494f2009-07-24 12:35:02 +0100586#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000587
Marek Szyprowskic7909502011-12-29 13:09:51 +0100588#define nommu() 1
589
Carlo Caione6e8266e2015-02-09 10:38:35 +0100590#define __get_dma_pgprot(attrs, prot) __pgprot(0)
591#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200592#define __alloc_from_pool(size, ret_page) NULL
Carlo Caione6e8266e2015-02-09 10:38:35 +0100593#define __alloc_from_contiguous(dev, size, prot, ret, c, wv) NULL
Marek Szyprowskic7909502011-12-29 13:09:51 +0100594#define __free_from_pool(cpu_addr, size) 0
Carlo Caione6e8266e2015-02-09 10:38:35 +0100595#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100596#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000597
598#endif /* CONFIG_MMU */
599
Marek Szyprowskic7909502011-12-29 13:09:51 +0100600static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
601 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100602{
Russell King04da5692009-11-19 15:54:45 +0000603 struct page *page;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100604 page = __dma_alloc_buffer(dev, size, gfp);
605 if (!page)
606 return NULL;
607
608 *ret_page = page;
609 return page_address(page);
610}
611
612
613
614static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100615 gfp_t gfp, pgprot_t prot, bool is_coherent,
616 struct dma_attrs *attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100617{
618 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900619 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000620 void *addr;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100621 bool want_vaddr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100622
Marek Szyprowskic7909502011-12-29 13:09:51 +0100623#ifdef CONFIG_DMA_API_DEBUG
624 u64 limit = (mask + 1) & ~mask;
625 if (limit && size >= limit) {
626 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
627 size, mask);
628 return NULL;
629 }
630#endif
631
632 if (!mask)
633 return NULL;
634
635 if (mask < 0xffffffffULL)
636 gfp |= GFP_DMA;
637
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100638 /*
639 * Following is a work-around (a.k.a. hack) to prevent pages
640 * with __GFP_COMP being passed to split_page() which cannot
641 * handle them. The real problem is that this flag probably
642 * should be 0 on ARM as it is not supported on this
643 * platform; see CONFIG_HUGETLBFS.
644 */
645 gfp &= ~(__GFP_COMP);
646
Marek Szyprowski553ac782012-02-29 14:45:28 +0100647 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000648 size = PAGE_ALIGN(size);
Carlo Caione6e8266e2015-02-09 10:38:35 +0100649 want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
Russell King04da5692009-11-19 15:54:45 +0000650
Rob Herringdd37e942012-08-21 12:20:17 +0200651 if (is_coherent || nommu())
Marek Szyprowskic7909502011-12-29 13:09:51 +0100652 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Russell King633dc922013-01-30 23:55:35 +0000653 else if (!(gfp & __GFP_WAIT))
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200654 addr = __alloc_from_pool(size, &page);
Gioh Kime464ef12014-05-22 13:38:23 +0900655 else if (!dev_get_cma_area(dev))
Carlo Caione6e8266e2015-02-09 10:38:35 +0100656 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller, want_vaddr);
Russell King31ebf942009-11-19 21:12:17 +0000657 else
Carlo Caione6e8266e2015-02-09 10:38:35 +0100658 addr = __alloc_from_contiguous(dev, size, prot, &page, caller, want_vaddr);
Russell King31ebf942009-11-19 21:12:17 +0000659
Carlo Caione6e8266e2015-02-09 10:38:35 +0100660 if (page)
Russell King9eedd962011-01-03 00:00:17 +0000661 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000662
Carlo Caione6e8266e2015-02-09 10:38:35 +0100663 return want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100664}
Russell King695ae0a2009-11-19 16:31:39 +0000665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*
667 * Allocate DMA-coherent memory space and return both the kernel remapped
668 * virtual and bus address for that space.
669 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200670void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
671 gfp_t gfp, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Russell King0ea1ec72013-10-23 16:14:59 +0100673 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400674 void *memory;
675
676 if (dma_alloc_from_coherent(dev, size, handle, &memory))
677 return memory;
678
Rob Herringdd37e942012-08-21 12:20:17 +0200679 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100680 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200681}
682
683static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
684 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
685{
Russell King0ea1ec72013-10-23 16:14:59 +0100686 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Rob Herringdd37e942012-08-21 12:20:17 +0200687 void *memory;
688
689 if (dma_alloc_from_coherent(dev, size, handle, &memory))
690 return memory;
691
692 return __dma_alloc(dev, size, handle, gfp, prot, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100693 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Mike Looijmans55af8a92015-06-03 11:25:31 +0100696static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200697 void *cpu_addr, dma_addr_t dma_addr, size_t size,
698 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100700 int ret = -ENXIO;
701#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200702 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
703 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100704 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200705 unsigned long off = vma->vm_pgoff;
706
Marek Szyprowski47142f02012-05-15 19:04:13 +0200707 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
708 return ret;
709
Marek Szyprowski50262a42012-07-30 09:35:26 +0200710 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
711 ret = remap_pfn_range(vma, vma->vm_start,
712 pfn + off,
713 vma->vm_end - vma->vm_start,
714 vma->vm_page_prot);
715 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100716#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 return ret;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100722 * Create userspace mapping for the DMA-coherent memory.
723 */
724static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
725 void *cpu_addr, dma_addr_t dma_addr, size_t size,
726 struct dma_attrs *attrs)
727{
728 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
729}
730
731int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
732 void *cpu_addr, dma_addr_t dma_addr, size_t size,
733 struct dma_attrs *attrs)
734{
735#ifdef CONFIG_MMU
736 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
737#endif /* CONFIG_MMU */
738 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
739}
740
741/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100742 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Rob Herringdd37e942012-08-21 12:20:17 +0200744static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
745 dma_addr_t handle, struct dma_attrs *attrs,
746 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100748 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Carlo Caione6e8266e2015-02-09 10:38:35 +0100749 bool want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
Russell King5edf71a2005-11-25 15:52:51 +0000750
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400751 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
752 return;
753
Russell King3e82d012009-11-19 15:38:12 +0000754 size = PAGE_ALIGN(size);
755
Rob Herringdd37e942012-08-21 12:20:17 +0200756 if (is_coherent || nommu()) {
Marek Szyprowskic7909502011-12-29 13:09:51 +0100757 __dma_free_buffer(page, size);
Aaro Koskinend9e0d142012-08-07 14:44:05 +0200758 } else if (__free_from_pool(cpu_addr, size)) {
759 return;
Gioh Kime464ef12014-05-22 13:38:23 +0900760 } else if (!dev_get_cma_area(dev)) {
Carlo Caione6e8266e2015-02-09 10:38:35 +0100761 if (want_vaddr)
762 __dma_free_remap(cpu_addr, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100763 __dma_free_buffer(page, size);
764 } else {
Marek Szyprowskic7909502011-12-29 13:09:51 +0100765 /*
766 * Non-atomic allocations cannot be freed with IRQs disabled
767 */
768 WARN_ON(irqs_disabled());
Carlo Caione6e8266e2015-02-09 10:38:35 +0100769 __free_from_contiguous(dev, page, cpu_addr, size, want_vaddr);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Russell Kingafd1a322008-09-25 16:30:57 +0100772
Rob Herringdd37e942012-08-21 12:20:17 +0200773void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
774 dma_addr_t handle, struct dma_attrs *attrs)
775{
776 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
777}
778
779static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
780 dma_addr_t handle, struct dma_attrs *attrs)
781{
782 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
783}
784
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200785int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
786 void *cpu_addr, dma_addr_t handle, size_t size,
787 struct dma_attrs *attrs)
788{
789 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
790 int ret;
791
792 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
793 if (unlikely(ret))
794 return ret;
795
796 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
797 return 0;
798}
799
Russell King65af1912009-11-24 17:53:33 +0000800static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000801 size_t size, enum dma_data_direction dir,
802 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000803{
Russell King15653372013-01-19 11:05:57 +0000804 unsigned long pfn;
805 size_t left = size;
806
807 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
808 offset %= PAGE_SIZE;
809
Russell King65af1912009-11-24 17:53:33 +0000810 /*
811 * A single sg entry may refer to multiple physically contiguous
812 * pages. But we still need to process highmem pages individually.
813 * If highmem is not configured then the bulk of this loop gets
814 * optimized out.
815 */
Russell King65af1912009-11-24 17:53:33 +0000816 do {
817 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000818 void *vaddr;
819
Russell King15653372013-01-19 11:05:57 +0000820 page = pfn_to_page(pfn);
821
Russell King93f1d622009-11-24 14:41:01 +0000822 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000823 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000824 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100825
826 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500827 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100828 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500829 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100830 } else {
831 vaddr = kmap_high_get(page);
832 if (vaddr) {
833 op(vaddr + offset, len, dir);
834 kunmap_high(page);
835 }
Russell King93f1d622009-11-24 14:41:01 +0000836 }
837 } else {
838 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000839 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000840 }
Russell King65af1912009-11-24 17:53:33 +0000841 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000842 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000843 left -= len;
844 } while (left);
845}
846
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100847/*
848 * Make an area consistent for devices.
849 * Note: Drivers should NOT use this function directly, as it will break
850 * platforms with CONFIG_DMABOUNCE.
851 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
852 */
853static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000854 size_t size, enum dma_data_direction dir)
855{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400856 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400857
Russell Kinga9c91472009-11-26 16:19:58 +0000858 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400859
Russell King65af1912009-11-24 17:53:33 +0000860 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000861 if (dir == DMA_FROM_DEVICE) {
862 outer_inv_range(paddr, paddr + size);
863 } else {
864 outer_clean_range(paddr, paddr + size);
865 }
866 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400867}
Russell King4ea0d732009-11-24 16:27:17 +0000868
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100869static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000870 size_t size, enum dma_data_direction dir)
871{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400872 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000873
874 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +0100875 /* in any case, don't bother invalidating if DMA to device */
876 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +0000877 outer_inv_range(paddr, paddr + size);
878
Russell Kingdeace4a2014-05-03 11:06:55 +0100879 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
880 }
Catalin Marinasc0177802010-09-13 15:57:36 +0100881
882 /*
Ming Leib2a234e2013-05-18 11:21:36 +0100883 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +0100884 */
Ming Leib2a234e2013-05-18 11:21:36 +0100885 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
886 unsigned long pfn;
887 size_t left = size;
888
889 pfn = page_to_pfn(page) + off / PAGE_SIZE;
890 off %= PAGE_SIZE;
891 if (off) {
892 pfn++;
893 left -= PAGE_SIZE - off;
894 }
895 while (left >= PAGE_SIZE) {
896 page = pfn_to_page(pfn++);
897 set_bit(PG_dcache_clean, &page->flags);
898 left -= PAGE_SIZE;
899 }
900 }
Russell King4ea0d732009-11-24 16:27:17 +0000901}
Nicolas Pitre43377452009-03-12 22:52:09 -0400902
Russell Kingafd1a322008-09-25 16:30:57 +0100903/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100904 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100905 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
906 * @sg: list of buffers
907 * @nents: number of buffers to map
908 * @dir: DMA transfer direction
909 *
910 * Map a set of buffers described by scatterlist in streaming mode for DMA.
911 * This is the scatter-gather version of the dma_map_single interface.
912 * Here the scatter gather list elements are each tagged with the
913 * appropriate dma address and length. They are obtained via
914 * sg_dma_{address,length}.
915 *
916 * Device ownership issues as mentioned for dma_map_single are the same
917 * here.
918 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100919int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
920 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100921{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100922 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100923 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100924 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100925
926 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200927#ifdef CONFIG_NEED_SG_DMA_LENGTH
928 s->dma_length = s->length;
929#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100930 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
931 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100932 if (dma_mapping_error(dev, s->dma_address))
933 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100934 }
Russell Kingafd1a322008-09-25 16:30:57 +0100935 return nents;
Russell King01135d922008-09-25 21:05:02 +0100936
937 bad_mapping:
938 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100939 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100940 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100941}
Russell Kingafd1a322008-09-25 16:30:57 +0100942
943/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100944 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100945 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
946 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100947 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100948 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
949 *
950 * Unmap a set of streaming mode DMA translations. Again, CPU access
951 * rules concerning calls here are the same as for dma_unmap_single().
952 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100953void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
954 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100955{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100956 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100957 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100958
Russell King01135d922008-09-25 21:05:02 +0100959 int i;
Russell King24056f52011-01-03 11:29:28 +0000960
Russell King01135d922008-09-25 21:05:02 +0100961 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100962 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100963}
Russell Kingafd1a322008-09-25 16:30:57 +0100964
965/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100966 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100967 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
968 * @sg: list of buffers
969 * @nents: number of buffers to map (returned from dma_map_sg)
970 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
971 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100972void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100973 int nents, enum dma_data_direction dir)
974{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100975 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100976 struct scatterlist *s;
977 int i;
978
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100979 for_each_sg(sg, s, nents, i)
980 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
981 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100982}
Russell Kingafd1a322008-09-25 16:30:57 +0100983
984/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100985 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100986 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
987 * @sg: list of buffers
988 * @nents: number of buffers to map (returned from dma_map_sg)
989 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
990 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100991void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100992 int nents, enum dma_data_direction dir)
993{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100994 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100995 struct scatterlist *s;
996 int i;
997
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100998 for_each_sg(sg, s, nents, i)
999 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1000 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001001}
Russell King24056f52011-01-03 11:29:28 +00001002
Russell King022ae532011-07-08 21:26:59 +01001003/*
1004 * Return whether the given device DMA address mask can be supported
1005 * properly. For example, if your device can only drive the low 24-bits
1006 * during bus mastering, then you would pass 0x00ffffff as the mask
1007 * to this function.
1008 */
1009int dma_supported(struct device *dev, u64 mask)
1010{
Russell King9f28cde2013-12-06 12:30:42 +00001011 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001012}
1013EXPORT_SYMBOL(dma_supported);
1014
Gregory CLEMENT87b54e72012-11-21 09:39:19 +01001015int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +01001016{
1017 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1018 return -EIO;
1019
Russell King022ae532011-07-08 21:26:59 +01001020 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +01001021
1022 return 0;
1023}
Russell King022ae532011-07-08 21:26:59 +01001024
Russell King24056f52011-01-03 11:29:28 +00001025#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1026
1027static int __init dma_debug_do_init(void)
1028{
1029 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1030 return 0;
1031}
1032fs_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001033
1034#ifdef CONFIG_ARM_DMA_USE_IOMMU
1035
1036/* IOMMU */
1037
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001038static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1039
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001040static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1041 size_t size)
1042{
1043 unsigned int order = get_order(size);
1044 unsigned int align = 0;
1045 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301046 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001047 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001048 dma_addr_t iova;
1049 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001050
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001051 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1052 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1053
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001054 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1055 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001056
1057 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001058 for (i = 0; i < mapping->nr_bitmaps; i++) {
1059 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1060 mapping->bits, 0, count, align);
1061
1062 if (start > mapping->bits)
1063 continue;
1064
1065 bitmap_set(mapping->bitmaps[i], start, count);
1066 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001067 }
1068
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001069 /*
1070 * No unused range found. Try to extend the existing mapping
1071 * and perform a second attempt to reserve an IO virtual
1072 * address range of size bytes.
1073 */
1074 if (i == mapping->nr_bitmaps) {
1075 if (extend_iommu_mapping(mapping)) {
1076 spin_unlock_irqrestore(&mapping->lock, flags);
1077 return DMA_ERROR_CODE;
1078 }
1079
1080 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1081 mapping->bits, 0, count, align);
1082
1083 if (start > mapping->bits) {
1084 spin_unlock_irqrestore(&mapping->lock, flags);
1085 return DMA_ERROR_CODE;
1086 }
1087
1088 bitmap_set(mapping->bitmaps[i], start, count);
1089 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001090 spin_unlock_irqrestore(&mapping->lock, flags);
1091
Ritesh Harjani006f8412014-05-20 10:02:59 +05301092 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001093 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001094
1095 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001096}
1097
1098static inline void __free_iova(struct dma_iommu_mapping *mapping,
1099 dma_addr_t addr, size_t size)
1100{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001101 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301102 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001103 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001104 dma_addr_t bitmap_base;
1105 u32 bitmap_index;
1106
1107 if (!size)
1108 return;
1109
Ritesh Harjani006f8412014-05-20 10:02:59 +05301110 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001111 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1112
Ritesh Harjani006f8412014-05-20 10:02:59 +05301113 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001114
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001115 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001116
Ritesh Harjani006f8412014-05-20 10:02:59 +05301117 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001118 /*
1119 * The address range to be freed reaches into the iova
1120 * range of the next bitmap. This should not happen as
1121 * we don't allow this in __alloc_iova (at the
1122 * moment).
1123 */
1124 BUG();
1125 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001126 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001127
1128 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001129 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001130 spin_unlock_irqrestore(&mapping->lock, flags);
1131}
1132
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001133static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1134 gfp_t gfp, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001135{
1136 struct page **pages;
1137 int count = size >> PAGE_SHIFT;
1138 int array_size = count * sizeof(struct page *);
1139 int i = 0;
1140
1141 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001142 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001143 else
1144 pages = vzalloc(array_size);
1145 if (!pages)
1146 return NULL;
1147
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001148 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
1149 {
1150 unsigned long order = get_order(size);
1151 struct page *page;
1152
1153 page = dma_alloc_from_contiguous(dev, count, order);
1154 if (!page)
1155 goto error;
1156
1157 __dma_clear_buffer(page, size);
1158
1159 for (i = 0; i < count; i++)
1160 pages[i] = page + i;
1161
1162 return pages;
1163 }
1164
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001165 /*
1166 * IOMMU can map any pages, so himem can also be used here
1167 */
1168 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1169
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001170 while (count) {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001171 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001172
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001173 for (order = __fls(count); order > 0; --order) {
1174 /*
1175 * We do not want OOM killer to be invoked as long
1176 * as we can fall back to single pages, so we force
1177 * __GFP_NORETRY for orders higher than zero.
1178 */
1179 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1180 if (pages[i])
1181 break;
1182 }
1183
1184 if (!pages[i]) {
1185 /*
1186 * Fall back to single page allocation.
1187 * Might invoke OOM killer as last resort.
1188 */
1189 pages[i] = alloc_pages(gfp, 0);
1190 if (!pages[i])
1191 goto error;
1192 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001193
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001194 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001195 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001196 j = 1 << order;
1197 while (--j)
1198 pages[i + j] = pages[i] + j;
1199 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001200
1201 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
1202 i += 1 << order;
1203 count -= 1 << order;
1204 }
1205
1206 return pages;
1207error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001208 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001209 if (pages[i])
1210 __free_pages(pages[i], 0);
Prathyush K46c87852012-07-16 08:59:55 +02001211 if (array_size <= PAGE_SIZE)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001212 kfree(pages);
1213 else
1214 vfree(pages);
1215 return NULL;
1216}
1217
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001218static int __iommu_free_buffer(struct device *dev, struct page **pages,
1219 size_t size, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001220{
1221 int count = size >> PAGE_SHIFT;
1222 int array_size = count * sizeof(struct page *);
1223 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001224
1225 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
1226 dma_release_from_contiguous(dev, pages[0], count);
1227 } else {
1228 for (i = 0; i < count; i++)
1229 if (pages[i])
1230 __free_pages(pages[i], 0);
1231 }
1232
Prathyush K46c87852012-07-16 08:59:55 +02001233 if (array_size <= PAGE_SIZE)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001234 kfree(pages);
1235 else
1236 vfree(pages);
1237 return 0;
1238}
1239
1240/*
1241 * Create a CPU mapping for a specified pages
1242 */
1243static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001244__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1245 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001246{
Laura Abbott513510d2014-10-09 15:26:40 -07001247 return dma_common_pages_remap(pages, size,
1248 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001249}
1250
1251/*
1252 * Create a mapping in device IO address space for specified pages
1253 */
1254static dma_addr_t
1255__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1256{
Will Deacon89cfdb12015-01-16 18:02:15 +01001257 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001258 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1259 dma_addr_t dma_addr, iova;
1260 int i, ret = DMA_ERROR_CODE;
1261
1262 dma_addr = __alloc_iova(mapping, size);
1263 if (dma_addr == DMA_ERROR_CODE)
1264 return dma_addr;
1265
1266 iova = dma_addr;
1267 for (i = 0; i < count; ) {
1268 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1269 phys_addr_t phys = page_to_phys(pages[i]);
1270 unsigned int len, j;
1271
1272 for (j = i + 1; j < count; j++, next_pfn++)
1273 if (page_to_pfn(pages[j]) != next_pfn)
1274 break;
1275
1276 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001277 ret = iommu_map(mapping->domain, iova, phys, len,
1278 IOMMU_READ|IOMMU_WRITE);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001279 if (ret < 0)
1280 goto fail;
1281 iova += len;
1282 i = j;
1283 }
1284 return dma_addr;
1285fail:
1286 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1287 __free_iova(mapping, dma_addr, size);
1288 return DMA_ERROR_CODE;
1289}
1290
1291static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1292{
Will Deacon89cfdb12015-01-16 18:02:15 +01001293 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001294
1295 /*
1296 * add optional in-page offset from iova to size and align
1297 * result to page size
1298 */
1299 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1300 iova &= PAGE_MASK;
1301
1302 iommu_unmap(mapping->domain, iova, size);
1303 __free_iova(mapping, iova, size);
1304 return 0;
1305}
1306
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001307static struct page **__atomic_get_pages(void *addr)
1308{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001309 struct page *page;
1310 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001311
Laura Abbott36d0fd22014-10-09 15:26:42 -07001312 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1313 page = phys_to_page(phys);
1314
1315 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001316}
1317
Marek Szyprowski955c7572012-05-16 19:38:58 +01001318static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001319{
1320 struct vm_struct *area;
1321
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001322 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1323 return __atomic_get_pages(cpu_addr);
1324
Marek Szyprowski955c7572012-05-16 19:38:58 +01001325 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1326 return cpu_addr;
1327
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001328 area = find_vm_area(cpu_addr);
1329 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1330 return area->pages;
1331 return NULL;
1332}
1333
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001334static void *__iommu_alloc_atomic(struct device *dev, size_t size,
1335 dma_addr_t *handle)
1336{
1337 struct page *page;
1338 void *addr;
1339
1340 addr = __alloc_from_pool(size, &page);
1341 if (!addr)
1342 return NULL;
1343
1344 *handle = __iommu_create_mapping(dev, &page, size);
1345 if (*handle == DMA_ERROR_CODE)
1346 goto err_mapping;
1347
1348 return addr;
1349
1350err_mapping:
1351 __free_from_pool(addr, size);
1352 return NULL;
1353}
1354
Marek Szyprowskid5898292013-02-08 10:54:48 +01001355static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001356 dma_addr_t handle, size_t size)
1357{
1358 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowskid5898292013-02-08 10:54:48 +01001359 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001360}
1361
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001362static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1363 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1364{
Russell King71b55662013-11-25 12:01:03 +00001365 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001366 struct page **pages;
1367 void *addr = NULL;
1368
1369 *handle = DMA_ERROR_CODE;
1370 size = PAGE_ALIGN(size);
1371
Marek Szyprowski10c85622014-01-16 15:39:17 +01001372 if (!(gfp & __GFP_WAIT))
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001373 return __iommu_alloc_atomic(dev, size, handle);
1374
Richard Zhao5b91a982013-06-20 20:31:00 +08001375 /*
1376 * Following is a work-around (a.k.a. hack) to prevent pages
1377 * with __GFP_COMP being passed to split_page() which cannot
1378 * handle them. The real problem is that this flag probably
1379 * should be 0 on ARM as it is not supported on this
1380 * platform; see CONFIG_HUGETLBFS.
1381 */
1382 gfp &= ~(__GFP_COMP);
1383
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001384 pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001385 if (!pages)
1386 return NULL;
1387
1388 *handle = __iommu_create_mapping(dev, pages, size);
1389 if (*handle == DMA_ERROR_CODE)
1390 goto err_buffer;
1391
Marek Szyprowski955c7572012-05-16 19:38:58 +01001392 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1393 return pages;
1394
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001395 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1396 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001397 if (!addr)
1398 goto err_mapping;
1399
1400 return addr;
1401
1402err_mapping:
1403 __iommu_remove_mapping(dev, *handle, size);
1404err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001405 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001406 return NULL;
1407}
1408
1409static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1410 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1411 struct dma_attrs *attrs)
1412{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001413 unsigned long uaddr = vma->vm_start;
1414 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001415 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001416
1417 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001418
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001419 if (!pages)
1420 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001421
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001422 do {
1423 int ret = vm_insert_page(vma, uaddr, *pages++);
1424 if (ret) {
1425 pr_err("Remapping memory failed: %d\n", ret);
1426 return ret;
1427 }
1428 uaddr += PAGE_SIZE;
1429 usize -= PAGE_SIZE;
1430 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001431
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001432 return 0;
1433}
1434
1435/*
1436 * free a page as defined by the above mapping.
1437 * Must not be called with IRQs disabled.
1438 */
1439void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1440 dma_addr_t handle, struct dma_attrs *attrs)
1441{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001442 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001443 size = PAGE_ALIGN(size);
1444
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001445 if (__in_atomic_pool(cpu_addr, size)) {
Marek Szyprowskid5898292013-02-08 10:54:48 +01001446 __iommu_free_atomic(dev, cpu_addr, handle, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001447 return;
1448 }
1449
YoungJun Cho836bfa02013-06-17 13:18:52 +09001450 pages = __iommu_get_pages(cpu_addr, attrs);
1451 if (!pages) {
1452 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1453 return;
1454 }
1455
Marek Szyprowski955c7572012-05-16 19:38:58 +01001456 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
Laura Abbott513510d2014-10-09 15:26:40 -07001457 dma_common_free_remap(cpu_addr, size,
1458 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001459 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001460
1461 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001462 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001463}
1464
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001465static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1466 void *cpu_addr, dma_addr_t dma_addr,
1467 size_t size, struct dma_attrs *attrs)
1468{
1469 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1470 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1471
1472 if (!pages)
1473 return -ENXIO;
1474
1475 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1476 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001477}
1478
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001479static int __dma_direction_to_prot(enum dma_data_direction dir)
1480{
1481 int prot;
1482
1483 switch (dir) {
1484 case DMA_BIDIRECTIONAL:
1485 prot = IOMMU_READ | IOMMU_WRITE;
1486 break;
1487 case DMA_TO_DEVICE:
1488 prot = IOMMU_READ;
1489 break;
1490 case DMA_FROM_DEVICE:
1491 prot = IOMMU_WRITE;
1492 break;
1493 default:
1494 prot = 0;
1495 }
1496
1497 return prot;
1498}
1499
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001500/*
1501 * Map a part of the scatter-gather list into contiguous io address space
1502 */
1503static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1504 size_t size, dma_addr_t *handle,
Rob Herring0fa478d2012-08-21 12:23:23 +02001505 enum dma_data_direction dir, struct dma_attrs *attrs,
1506 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001507{
Will Deacon89cfdb12015-01-16 18:02:15 +01001508 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001509 dma_addr_t iova, iova_base;
1510 int ret = 0;
1511 unsigned int count;
1512 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001513 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001514
1515 size = PAGE_ALIGN(size);
1516 *handle = DMA_ERROR_CODE;
1517
1518 iova_base = iova = __alloc_iova(mapping, size);
1519 if (iova == DMA_ERROR_CODE)
1520 return -ENOMEM;
1521
1522 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1523 phys_addr_t phys = page_to_phys(sg_page(s));
1524 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1525
Rob Herring0fa478d2012-08-21 12:23:23 +02001526 if (!is_coherent &&
1527 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001528 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1529
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001530 prot = __dma_direction_to_prot(dir);
1531
1532 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001533 if (ret < 0)
1534 goto fail;
1535 count += len >> PAGE_SHIFT;
1536 iova += len;
1537 }
1538 *handle = iova_base;
1539
1540 return 0;
1541fail:
1542 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1543 __free_iova(mapping, iova_base, size);
1544 return ret;
1545}
1546
Rob Herring0fa478d2012-08-21 12:23:23 +02001547static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1548 enum dma_data_direction dir, struct dma_attrs *attrs,
1549 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001550{
1551 struct scatterlist *s = sg, *dma = sg, *start = sg;
1552 int i, count = 0;
1553 unsigned int offset = s->offset;
1554 unsigned int size = s->offset + s->length;
1555 unsigned int max = dma_get_max_seg_size(dev);
1556
1557 for (i = 1; i < nents; i++) {
1558 s = sg_next(s);
1559
1560 s->dma_address = DMA_ERROR_CODE;
1561 s->dma_length = 0;
1562
1563 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1564 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001565 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001566 goto bad_mapping;
1567
1568 dma->dma_address += offset;
1569 dma->dma_length = size - offset;
1570
1571 size = offset = s->offset;
1572 start = s;
1573 dma = sg_next(dma);
1574 count += 1;
1575 }
1576 size += s->length;
1577 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001578 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1579 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001580 goto bad_mapping;
1581
1582 dma->dma_address += offset;
1583 dma->dma_length = size - offset;
1584
1585 return count+1;
1586
1587bad_mapping:
1588 for_each_sg(sg, s, count, i)
1589 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1590 return 0;
1591}
1592
1593/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001594 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1595 * @dev: valid struct device pointer
1596 * @sg: list of buffers
1597 * @nents: number of buffers to map
1598 * @dir: DMA transfer direction
1599 *
1600 * Map a set of i/o coherent buffers described by scatterlist in streaming
1601 * mode for DMA. The scatter gather list elements are merged together (if
1602 * possible) and tagged with the appropriate dma address and length. They are
1603 * obtained via sg_dma_{address,length}.
1604 */
1605int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1606 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1607{
1608 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1609}
1610
1611/**
1612 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1613 * @dev: valid struct device pointer
1614 * @sg: list of buffers
1615 * @nents: number of buffers to map
1616 * @dir: DMA transfer direction
1617 *
1618 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1619 * The scatter gather list elements are merged together (if possible) and
1620 * tagged with the appropriate dma address and length. They are obtained via
1621 * sg_dma_{address,length}.
1622 */
1623int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1624 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1625{
1626 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1627}
1628
1629static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1630 int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
1631 bool is_coherent)
1632{
1633 struct scatterlist *s;
1634 int i;
1635
1636 for_each_sg(sg, s, nents, i) {
1637 if (sg_dma_len(s))
1638 __iommu_remove_mapping(dev, sg_dma_address(s),
1639 sg_dma_len(s));
1640 if (!is_coherent &&
1641 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1642 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1643 s->length, dir);
1644 }
1645}
1646
1647/**
1648 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1649 * @dev: valid struct device pointer
1650 * @sg: list of buffers
1651 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1652 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1653 *
1654 * Unmap a set of streaming mode DMA translations. Again, CPU access
1655 * rules concerning calls here are the same as for dma_unmap_single().
1656 */
1657void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1658 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1659{
1660 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1661}
1662
1663/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001664 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1665 * @dev: valid struct device pointer
1666 * @sg: list of buffers
1667 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1668 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1669 *
1670 * Unmap a set of streaming mode DMA translations. Again, CPU access
1671 * rules concerning calls here are the same as for dma_unmap_single().
1672 */
1673void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1674 enum dma_data_direction dir, struct dma_attrs *attrs)
1675{
Rob Herring0fa478d2012-08-21 12:23:23 +02001676 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001677}
1678
1679/**
1680 * arm_iommu_sync_sg_for_cpu
1681 * @dev: valid struct device pointer
1682 * @sg: list of buffers
1683 * @nents: number of buffers to map (returned from dma_map_sg)
1684 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1685 */
1686void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1687 int nents, enum dma_data_direction dir)
1688{
1689 struct scatterlist *s;
1690 int i;
1691
1692 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001693 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001694
1695}
1696
1697/**
1698 * arm_iommu_sync_sg_for_device
1699 * @dev: valid struct device pointer
1700 * @sg: list of buffers
1701 * @nents: number of buffers to map (returned from dma_map_sg)
1702 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1703 */
1704void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1705 int nents, enum dma_data_direction dir)
1706{
1707 struct scatterlist *s;
1708 int i;
1709
1710 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001711 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001712}
1713
1714
1715/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001716 * arm_coherent_iommu_map_page
1717 * @dev: valid struct device pointer
1718 * @page: page that buffer resides in
1719 * @offset: offset into page for start of buffer
1720 * @size: size of buffer to map
1721 * @dir: DMA transfer direction
1722 *
1723 * Coherent IOMMU aware version of arm_dma_map_page()
1724 */
1725static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1726 unsigned long offset, size_t size, enum dma_data_direction dir,
1727 struct dma_attrs *attrs)
1728{
Will Deacon89cfdb12015-01-16 18:02:15 +01001729 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001730 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001731 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001732
1733 dma_addr = __alloc_iova(mapping, len);
1734 if (dma_addr == DMA_ERROR_CODE)
1735 return dma_addr;
1736
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001737 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01001738
1739 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001740 if (ret < 0)
1741 goto fail;
1742
1743 return dma_addr + offset;
1744fail:
1745 __free_iova(mapping, dma_addr, len);
1746 return DMA_ERROR_CODE;
1747}
1748
1749/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001750 * arm_iommu_map_page
1751 * @dev: valid struct device pointer
1752 * @page: page that buffer resides in
1753 * @offset: offset into page for start of buffer
1754 * @size: size of buffer to map
1755 * @dir: DMA transfer direction
1756 *
1757 * IOMMU aware version of arm_dma_map_page()
1758 */
1759static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1760 unsigned long offset, size_t size, enum dma_data_direction dir,
1761 struct dma_attrs *attrs)
1762{
Rob Herring0fa478d2012-08-21 12:23:23 +02001763 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001764 __dma_page_cpu_to_dev(page, offset, size, dir);
1765
Rob Herring0fa478d2012-08-21 12:23:23 +02001766 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1767}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001768
Rob Herring0fa478d2012-08-21 12:23:23 +02001769/**
1770 * arm_coherent_iommu_unmap_page
1771 * @dev: valid struct device pointer
1772 * @handle: DMA address of buffer
1773 * @size: size of buffer (same as passed to dma_map_page)
1774 * @dir: DMA transfer direction (same as passed to dma_map_page)
1775 *
1776 * Coherent IOMMU aware version of arm_dma_unmap_page()
1777 */
1778static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1779 size_t size, enum dma_data_direction dir,
1780 struct dma_attrs *attrs)
1781{
Will Deacon89cfdb12015-01-16 18:02:15 +01001782 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001783 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001784 int offset = handle & ~PAGE_MASK;
1785 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001786
Rob Herring0fa478d2012-08-21 12:23:23 +02001787 if (!iova)
1788 return;
1789
1790 iommu_unmap(mapping->domain, iova, len);
1791 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001792}
1793
1794/**
1795 * arm_iommu_unmap_page
1796 * @dev: valid struct device pointer
1797 * @handle: DMA address of buffer
1798 * @size: size of buffer (same as passed to dma_map_page)
1799 * @dir: DMA transfer direction (same as passed to dma_map_page)
1800 *
1801 * IOMMU aware version of arm_dma_unmap_page()
1802 */
1803static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1804 size_t size, enum dma_data_direction dir,
1805 struct dma_attrs *attrs)
1806{
Will Deacon89cfdb12015-01-16 18:02:15 +01001807 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001808 dma_addr_t iova = handle & PAGE_MASK;
1809 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1810 int offset = handle & ~PAGE_MASK;
1811 int len = PAGE_ALIGN(size + offset);
1812
1813 if (!iova)
1814 return;
1815
Rob Herring0fa478d2012-08-21 12:23:23 +02001816 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001817 __dma_page_dev_to_cpu(page, offset, size, dir);
1818
1819 iommu_unmap(mapping->domain, iova, len);
1820 __free_iova(mapping, iova, len);
1821}
1822
1823static void arm_iommu_sync_single_for_cpu(struct device *dev,
1824 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1825{
Will Deacon89cfdb12015-01-16 18:02:15 +01001826 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001827 dma_addr_t iova = handle & PAGE_MASK;
1828 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1829 unsigned int offset = handle & ~PAGE_MASK;
1830
1831 if (!iova)
1832 return;
1833
Rob Herring0fa478d2012-08-21 12:23:23 +02001834 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001835}
1836
1837static void arm_iommu_sync_single_for_device(struct device *dev,
1838 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1839{
Will Deacon89cfdb12015-01-16 18:02:15 +01001840 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001841 dma_addr_t iova = handle & PAGE_MASK;
1842 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1843 unsigned int offset = handle & ~PAGE_MASK;
1844
1845 if (!iova)
1846 return;
1847
1848 __dma_page_cpu_to_dev(page, offset, size, dir);
1849}
1850
1851struct dma_map_ops iommu_ops = {
1852 .alloc = arm_iommu_alloc_attrs,
1853 .free = arm_iommu_free_attrs,
1854 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001855 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001856
1857 .map_page = arm_iommu_map_page,
1858 .unmap_page = arm_iommu_unmap_page,
1859 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
1860 .sync_single_for_device = arm_iommu_sync_single_for_device,
1861
1862 .map_sg = arm_iommu_map_sg,
1863 .unmap_sg = arm_iommu_unmap_sg,
1864 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
1865 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Hiroshi Doyud09e1332013-01-29 14:57:07 +02001866
1867 .set_dma_mask = arm_dma_set_mask,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001868};
1869
Rob Herring0fa478d2012-08-21 12:23:23 +02001870struct dma_map_ops iommu_coherent_ops = {
1871 .alloc = arm_iommu_alloc_attrs,
1872 .free = arm_iommu_free_attrs,
1873 .mmap = arm_iommu_mmap_attrs,
1874 .get_sgtable = arm_iommu_get_sgtable,
1875
1876 .map_page = arm_coherent_iommu_map_page,
1877 .unmap_page = arm_coherent_iommu_unmap_page,
1878
1879 .map_sg = arm_coherent_iommu_map_sg,
1880 .unmap_sg = arm_coherent_iommu_unmap_sg,
Hiroshi Doyud09e1332013-01-29 14:57:07 +02001881
1882 .set_dma_mask = arm_dma_set_mask,
Rob Herring0fa478d2012-08-21 12:23:23 +02001883};
1884
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001885/**
1886 * arm_iommu_create_mapping
1887 * @bus: pointer to the bus holding the client device (for IOMMU calls)
1888 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001889 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001890 *
1891 * Creates a mapping structure which holds information about used/unused
1892 * IO address ranges, which is required to perform memory allocation and
1893 * mapping with IOMMU aware functions.
1894 *
1895 * The client device need to be attached to the mapping with
1896 * arm_iommu_attach_device function.
1897 */
1898struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01001899arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001900{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001901 unsigned int bits = size >> PAGE_SHIFT;
1902 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001903 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001904 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001905 int err = -ENOMEM;
1906
Marek Szyprowski14245322015-04-29 11:29:19 +01001907 /* currently only 32-bit DMA address space is supported */
1908 if (size > DMA_BIT_MASK(32) + 1)
1909 return ERR_PTR(-ERANGE);
1910
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001911 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001912 return ERR_PTR(-EINVAL);
1913
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001914 if (bitmap_size > PAGE_SIZE) {
1915 extensions = bitmap_size / PAGE_SIZE;
1916 bitmap_size = PAGE_SIZE;
1917 }
1918
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001919 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1920 if (!mapping)
1921 goto err;
1922
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001923 mapping->bitmap_size = bitmap_size;
1924 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001925 GFP_KERNEL);
1926 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001927 goto err2;
1928
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001929 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001930 if (!mapping->bitmaps[0])
1931 goto err3;
1932
1933 mapping->nr_bitmaps = 1;
1934 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001935 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001936 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001937
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001938 spin_lock_init(&mapping->lock);
1939
1940 mapping->domain = iommu_domain_alloc(bus);
1941 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001942 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001943
1944 kref_init(&mapping->kref);
1945 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001946err4:
1947 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001948err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001949 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001950err2:
1951 kfree(mapping);
1952err:
1953 return ERR_PTR(err);
1954}
Prathyush K18177d12013-01-04 06:22:42 -05001955EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001956
1957static void release_iommu_mapping(struct kref *kref)
1958{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001959 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001960 struct dma_iommu_mapping *mapping =
1961 container_of(kref, struct dma_iommu_mapping, kref);
1962
1963 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001964 for (i = 0; i < mapping->nr_bitmaps; i++)
1965 kfree(mapping->bitmaps[i]);
1966 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001967 kfree(mapping);
1968}
1969
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001970static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
1971{
1972 int next_bitmap;
1973
Marek Szyprowski462859aa2015-07-08 13:21:55 +01001974 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001975 return -EINVAL;
1976
1977 next_bitmap = mapping->nr_bitmaps;
1978 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
1979 GFP_ATOMIC);
1980 if (!mapping->bitmaps[next_bitmap])
1981 return -ENOMEM;
1982
1983 mapping->nr_bitmaps++;
1984
1985 return 0;
1986}
1987
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001988void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1989{
1990 if (mapping)
1991 kref_put(&mapping->kref, release_iommu_mapping);
1992}
Prathyush K18177d12013-01-04 06:22:42 -05001993EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001994
Laurent Pincharteab8d652015-01-23 16:21:49 +02001995static int __arm_iommu_attach_device(struct device *dev,
1996 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001997{
1998 int err;
1999
2000 err = iommu_attach_device(mapping->domain, dev);
2001 if (err)
2002 return err;
2003
2004 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002005 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002006
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002007 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002008 return 0;
2009}
2010
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002011/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002012 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002013 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002014 * @mapping: io address space mapping structure (returned from
2015 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002016 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002017 * Attaches specified io address space mapping to the provided device.
2018 * This replaces the dma operations (dma_map_ops pointer) with the
2019 * IOMMU aware version.
2020 *
2021 * More than one client might be attached to the same io address space
2022 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002023 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002024int arm_iommu_attach_device(struct device *dev,
2025 struct dma_iommu_mapping *mapping)
2026{
2027 int err;
2028
2029 err = __arm_iommu_attach_device(dev, mapping);
2030 if (err)
2031 return err;
2032
2033 set_dma_ops(dev, &iommu_ops);
2034 return 0;
2035}
2036EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2037
2038static void __arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002039{
2040 struct dma_iommu_mapping *mapping;
2041
2042 mapping = to_dma_iommu_mapping(dev);
2043 if (!mapping) {
2044 dev_warn(dev, "Not attached\n");
2045 return;
2046 }
2047
2048 iommu_detach_device(mapping->domain, dev);
2049 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002050 to_dma_iommu_mapping(dev) = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002051
2052 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2053}
Laurent Pincharteab8d652015-01-23 16:21:49 +02002054
2055/**
2056 * arm_iommu_detach_device
2057 * @dev: valid struct device pointer
2058 *
2059 * Detaches the provided device from a previously attached map.
2060 * This voids the dma operations (dma_map_ops pointer)
2061 */
2062void arm_iommu_detach_device(struct device *dev)
2063{
2064 __arm_iommu_detach_device(dev);
2065 set_dma_ops(dev, NULL);
2066}
Prathyush K18177d12013-01-04 06:22:42 -05002067EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002068
Will Deacon4bb25782014-08-27 17:52:44 +01002069static struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
2070{
2071 return coherent ? &iommu_coherent_ops : &iommu_ops;
2072}
2073
2074static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
2075 struct iommu_ops *iommu)
2076{
2077 struct dma_iommu_mapping *mapping;
2078
2079 if (!iommu)
2080 return false;
2081
2082 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2083 if (IS_ERR(mapping)) {
2084 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2085 size, dev_name(dev));
2086 return false;
2087 }
2088
Laurent Pincharteab8d652015-01-23 16:21:49 +02002089 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002090 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2091 dev_name(dev));
2092 arm_iommu_release_mapping(mapping);
2093 return false;
2094 }
2095
2096 return true;
2097}
2098
2099static void arm_teardown_iommu_dma_ops(struct device *dev)
2100{
Will Deacon89cfdb12015-01-16 18:02:15 +01002101 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002102
Will Deaconc2273a12015-01-16 18:01:43 +01002103 if (!mapping)
2104 return;
2105
Laurent Pincharteab8d652015-01-23 16:21:49 +02002106 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002107 arm_iommu_release_mapping(mapping);
2108}
2109
2110#else
2111
2112static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
2113 struct iommu_ops *iommu)
2114{
2115 return false;
2116}
2117
2118static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2119
2120#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2121
2122#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2123
2124static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
2125{
2126 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2127}
2128
2129void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
2130 struct iommu_ops *iommu, bool coherent)
2131{
2132 struct dma_map_ops *dma_ops;
2133
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002134 dev->archdata.dma_coherent = coherent;
Will Deacon4bb25782014-08-27 17:52:44 +01002135 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2136 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2137 else
2138 dma_ops = arm_get_dma_map_ops(coherent);
2139
2140 set_dma_ops(dev, dma_ops);
2141}
2142
2143void arch_teardown_dma_ops(struct device *dev)
2144{
2145 arm_teardown_iommu_dma_ops(dev);
2146}