blob: 0eca3812527e614e891113839992b4e20cf3fb8d [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 King1234e3f2015-07-24 09:10:55 +010042#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010043#include "mm.h"
44
Marek Szyprowski15237e12012-02-10 19:55:20 +010045/*
46 * The DMA API is built upon the notion of "buffer ownership". A buffer
47 * is either exclusively owned by the CPU (and therefore may be accessed
48 * by it) or exclusively owned by the DMA device. These helper functions
49 * represent the transitions between these two ownership states.
50 *
51 * Note, however, that on later ARMs, this notion does not work due to
52 * speculative prefetches. We model our approach on the assumption that
53 * the CPU does do speculative prefetches, which means we clean caches
54 * before transfers and delay cache invalidation until transfer completion.
55 *
Marek Szyprowski15237e12012-02-10 19:55:20 +010056 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010057static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010058 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +010059static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +010060 size_t, enum dma_data_direction);
61
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010062/**
63 * arm_dma_map_page - map a portion of a page for streaming DMA
64 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
65 * @page: page that buffer resides in
66 * @offset: offset into page for start of buffer
67 * @size: size of buffer to map
68 * @dir: DMA transfer direction
69 *
70 * Ensure that any data held in the cache is appropriately discarded
71 * or written back.
72 *
73 * The device owns this memory once this call has completed. The CPU
74 * can regain ownership by calling dma_unmap_page().
75 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +010076static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010077 unsigned long offset, size_t size, enum dma_data_direction dir,
78 struct dma_attrs *attrs)
79{
Rob Herringdd37e942012-08-21 12:20:17 +020080 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +010081 __dma_page_cpu_to_dev(page, offset, size, dir);
82 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010083}
84
Rob Herringdd37e942012-08-21 12:20:17 +020085static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
86 unsigned long offset, size_t size, enum dma_data_direction dir,
87 struct dma_attrs *attrs)
88{
89 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
90}
91
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010092/**
93 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
94 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
95 * @handle: DMA address of buffer
96 * @size: size of buffer (same as passed to dma_map_page)
97 * @dir: DMA transfer direction (same as passed to dma_map_page)
98 *
99 * Unmap a page streaming mode DMA translation. The handle and size
100 * must match what was provided in the previous dma_map_page() call.
101 * All other usages are undefined.
102 *
103 * After this call, reads by the CPU to the buffer are guaranteed to see
104 * whatever the device wrote there.
105 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100106static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100107 size_t size, enum dma_data_direction dir,
108 struct dma_attrs *attrs)
109{
Rob Herringdd37e942012-08-21 12:20:17 +0200110 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100111 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
112 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100113}
114
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100115static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100116 dma_addr_t handle, size_t size, enum dma_data_direction dir)
117{
118 unsigned int offset = handle & (PAGE_SIZE - 1);
119 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200120 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100121}
122
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100123static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100124 dma_addr_t handle, size_t size, enum dma_data_direction dir)
125{
126 unsigned int offset = handle & (PAGE_SIZE - 1);
127 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200128 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100129}
130
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100131struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200132 .alloc = arm_dma_alloc,
133 .free = arm_dma_free,
134 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200135 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100136 .map_page = arm_dma_map_page,
137 .unmap_page = arm_dma_unmap_page,
138 .map_sg = arm_dma_map_sg,
139 .unmap_sg = arm_dma_unmap_sg,
140 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
141 .sync_single_for_device = arm_dma_sync_single_for_device,
142 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
143 .sync_sg_for_device = arm_dma_sync_sg_for_device,
144 .set_dma_mask = arm_dma_set_mask,
145};
146EXPORT_SYMBOL(arm_dma_ops);
147
Rob Herringdd37e942012-08-21 12:20:17 +0200148static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
149 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
150static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
151 dma_addr_t handle, struct dma_attrs *attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100152static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
153 void *cpu_addr, dma_addr_t dma_addr, size_t size,
154 struct dma_attrs *attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200155
156struct dma_map_ops arm_coherent_dma_ops = {
157 .alloc = arm_coherent_dma_alloc,
158 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100159 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200160 .get_sgtable = arm_dma_get_sgtable,
161 .map_page = arm_coherent_dma_map_page,
162 .map_sg = arm_dma_map_sg,
163 .set_dma_mask = arm_dma_set_mask,
164};
165EXPORT_SYMBOL(arm_coherent_dma_ops);
166
Russell King9f28cde2013-12-06 12:30:42 +0000167static int __dma_supported(struct device *dev, u64 mask, bool warn)
168{
169 unsigned long max_dma_pfn;
170
171 /*
172 * If the mask allows for more memory than we can address,
173 * and we actually have that much memory, then we must
174 * indicate that DMA to this device is not supported.
175 */
176 if (sizeof(mask) != sizeof(dma_addr_t) &&
177 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000178 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000179 if (warn) {
180 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
181 mask);
182 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
183 }
184 return 0;
185 }
186
187 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
188
189 /*
190 * Translate the device's DMA mask to a PFN limit. This
191 * PFN number includes the page which we can DMA to.
192 */
193 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
194 if (warn)
195 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",
196 mask,
197 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
198 max_dma_pfn + 1);
199 return 0;
200 }
201
202 return 1;
203}
204
Catalin Marinasab6494f2009-07-24 12:35:02 +0100205static u64 get_coherent_dma_mask(struct device *dev)
206{
Russell King4dcfa602013-07-09 12:14:49 +0100207 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Catalin Marinasab6494f2009-07-24 12:35:02 +0100209 if (dev) {
210 mask = dev->coherent_dma_mask;
211
212 /*
213 * Sanity check the DMA mask - it must be non-zero, and
214 * must be able to be satisfied by a DMA allocation.
215 */
216 if (mask == 0) {
217 dev_warn(dev, "coherent DMA mask is unset\n");
218 return 0;
219 }
220
Russell King9f28cde2013-12-06 12:30:42 +0000221 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100222 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100223 }
224
225 return mask;
226}
227
Marek Szyprowskic7909502011-12-29 13:09:51 +0100228static void __dma_clear_buffer(struct page *page, size_t size)
229{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100230 /*
231 * Ensure that the allocated pages are zeroed, and that any data
232 * lurking in the kernel direct-mapped region is invalidated.
233 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100234 if (PageHighMem(page)) {
235 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
236 phys_addr_t end = base + size;
237 while (size > 0) {
238 void *ptr = kmap_atomic(page);
239 memset(ptr, 0, PAGE_SIZE);
240 dmac_flush_range(ptr, ptr + PAGE_SIZE);
241 kunmap_atomic(ptr);
242 page++;
243 size -= PAGE_SIZE;
244 }
245 outer_flush_range(base, end);
246 } else {
247 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200248 memset(ptr, 0, size);
249 dmac_flush_range(ptr, ptr + size);
250 outer_flush_range(__pa(ptr), __pa(ptr) + size);
251 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100252}
253
Russell King7a9a32a2009-11-19 15:31:07 +0000254/*
255 * Allocate a DMA buffer for 'dev' of size 'size' using the
256 * specified gfp mask. Note that 'size' must be page aligned.
257 */
258static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
259{
260 unsigned long order = get_order(size);
261 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000262
263 page = alloc_pages(gfp, order);
264 if (!page)
265 return NULL;
266
267 /*
268 * Now split the huge page and free the excess pages
269 */
270 split_page(page, order);
271 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
272 __free_page(p);
273
Marek Szyprowskic7909502011-12-29 13:09:51 +0100274 __dma_clear_buffer(page, size);
Russell King7a9a32a2009-11-19 15:31:07 +0000275
276 return page;
277}
278
279/*
280 * Free a DMA buffer. 'size' must be page aligned.
281 */
282static void __dma_free_buffer(struct page *page, size_t size)
283{
284 struct page *e = page + (size >> PAGE_SHIFT);
285
286 while (page < e) {
287 __free_page(page);
288 page++;
289 }
290}
291
Catalin Marinasab6494f2009-07-24 12:35:02 +0100292#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Marek Szyprowskic7909502011-12-29 13:09:51 +0100294static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100295 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100296 const void *caller, bool want_vaddr);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100297
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200298static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
299 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100300 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200301
302static void *
303__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
304 const void *caller)
305{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200306 /*
307 * DMA allocation can be mapped to user space, so lets
308 * set VM_USERMAP flags too.
309 */
Laura Abbott513510d2014-10-09 15:26:40 -0700310 return dma_common_contiguous_remap(page, size,
311 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
312 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200313}
314
315static void __dma_free_remap(void *cpu_addr, size_t size)
316{
Laura Abbott513510d2014-10-09 15:26:40 -0700317 dma_common_free_remap(cpu_addr, size,
318 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200319}
320
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200321#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700322static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200323
Laura Abbott36d0fd22014-10-09 15:26:42 -0700324static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100325
326static int __init early_coherent_pool(char *p)
327{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700328 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100329 return 0;
330}
331early_param("coherent_pool", early_coherent_pool);
332
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200333void __init init_dma_coherent_pool_size(unsigned long size)
334{
335 /*
336 * Catch any attempt to set the pool size too late.
337 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700338 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200339
340 /*
341 * Set architecture specific coherent pool size only if
342 * it has not been changed by kernel command line parameter.
343 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700344 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
345 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200346}
347
Marek Szyprowskic7909502011-12-29 13:09:51 +0100348/*
349 * Initialise the coherent pool for atomic allocations.
350 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200351static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100352{
Russell King71b55662013-11-25 12:01:03 +0000353 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100354 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100355 struct page *page;
356 void *ptr;
357
Laura Abbott36d0fd22014-10-09 15:26:42 -0700358 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
359 if (!atomic_pool)
360 goto out;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300361
Gioh Kime464ef12014-05-22 13:38:23 +0900362 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700363 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100364 &page, atomic_pool_init, true);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200365 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700366 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100367 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100368 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700369 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300370
Laura Abbott36d0fd22014-10-09 15:26:42 -0700371 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
372 page_to_phys(page),
373 atomic_pool_size, -1);
374 if (ret)
375 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300376
Laura Abbott36d0fd22014-10-09 15:26:42 -0700377 gen_pool_set_algo(atomic_pool,
378 gen_pool_first_fit_order_align,
379 (void *)PAGE_SHIFT);
380 pr_info("DMA: preallocated %zd KiB pool for atomic coherent allocations\n",
381 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100382 return 0;
383 }
Sachin Kamatec106652012-09-24 08:35:03 +0200384
Laura Abbott36d0fd22014-10-09 15:26:42 -0700385destroy_genpool:
386 gen_pool_destroy(atomic_pool);
387 atomic_pool = NULL;
388out:
389 pr_err("DMA: failed to allocate %zx KiB pool for atomic coherent allocation\n",
390 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100391 return -ENOMEM;
392}
393/*
394 * CMA is activated by core_initcall, so we must be called after it.
395 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200396postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100397
398struct dma_contig_early_reserve {
399 phys_addr_t base;
400 unsigned long size;
401};
402
403static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
404
405static int dma_mmu_remap_num __initdata;
406
407void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
408{
409 dma_mmu_remap[dma_mmu_remap_num].base = base;
410 dma_mmu_remap[dma_mmu_remap_num].size = size;
411 dma_mmu_remap_num++;
412}
413
414void __init dma_contiguous_remap(void)
415{
416 int i;
417 for (i = 0; i < dma_mmu_remap_num; i++) {
418 phys_addr_t start = dma_mmu_remap[i].base;
419 phys_addr_t end = start + dma_mmu_remap[i].size;
420 struct map_desc map;
421 unsigned long addr;
422
423 if (end > arm_lowmem_limit)
424 end = arm_lowmem_limit;
425 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200426 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100427
428 map.pfn = __phys_to_pfn(start);
429 map.virtual = __phys_to_virt(start);
430 map.length = end - start;
431 map.type = MT_MEMORY_DMA_READY;
432
433 /*
Russell King6b076992014-07-17 12:17:45 +0100434 * Clear previous low-memory mapping to ensure that the
435 * TLB does not see any conflicting entries, then flush
436 * the TLB of the old entries before creating new mappings.
437 *
438 * This ensures that any speculatively loaded TLB entries
439 * (even though they may be rare) can not cause any problems,
440 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100441 */
442 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400443 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100444 pmd_clear(pmd_off_k(addr));
445
Russell King6b076992014-07-17 12:17:45 +0100446 flush_tlb_kernel_range(__phys_to_virt(start),
447 __phys_to_virt(end));
448
Marek Szyprowskic7909502011-12-29 13:09:51 +0100449 iotable_init(&map, 1);
450 }
451}
452
Marek Szyprowskic7909502011-12-29 13:09:51 +0100453static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
454 void *data)
455{
456 struct page *page = virt_to_page(addr);
457 pgprot_t prot = *(pgprot_t *)data;
458
459 set_pte_ext(pte, mk_pte(page, prot), 0);
460 return 0;
461}
462
463static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
464{
465 unsigned long start = (unsigned long) page_address(page);
466 unsigned end = start + size;
467
468 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100469 flush_tlb_kernel_range(start, end);
470}
471
472static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
473 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100474 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100475{
476 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100477 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100478 page = __dma_alloc_buffer(dev, size, gfp);
479 if (!page)
480 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100481 if (!want_vaddr)
482 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100483
484 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
485 if (!ptr) {
486 __dma_free_buffer(page, size);
487 return NULL;
488 }
489
Carlo Caione6e8266e2015-02-09 10:38:35 +0100490 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100491 *ret_page = page;
492 return ptr;
493}
494
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200495static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100496{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700497 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200498 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100499
Laura Abbott36d0fd22014-10-09 15:26:42 -0700500 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200501 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100502 return NULL;
503 }
504
Laura Abbott36d0fd22014-10-09 15:26:42 -0700505 val = gen_pool_alloc(atomic_pool, size);
506 if (val) {
507 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200508
Laura Abbott36d0fd22014-10-09 15:26:42 -0700509 *ret_page = phys_to_page(phys);
510 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100511 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200512
513 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100514}
515
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300516static bool __in_atomic_pool(void *start, size_t size)
517{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700518 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300519}
520
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200521static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100522{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300523 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100524 return 0;
525
Laura Abbott36d0fd22014-10-09 15:26:42 -0700526 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200527
Marek Szyprowskic7909502011-12-29 13:09:51 +0100528 return 1;
529}
530
531static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100532 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100533 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100534{
535 unsigned long order = get_order(size);
536 size_t count = size >> PAGE_SHIFT;
537 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100538 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100539
540 page = dma_alloc_from_contiguous(dev, count, order);
541 if (!page)
542 return NULL;
543
544 __dma_clear_buffer(page, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100545
Carlo Caione6e8266e2015-02-09 10:38:35 +0100546 if (!want_vaddr)
547 goto out;
548
Marek Szyprowski9848e482013-01-16 15:38:44 +0100549 if (PageHighMem(page)) {
550 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
551 if (!ptr) {
552 dma_release_from_contiguous(dev, page, count);
553 return NULL;
554 }
555 } else {
556 __dma_remap(page, size, prot);
557 ptr = page_address(page);
558 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100559
560 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100561 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100562 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100563}
564
565static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100566 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100567{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100568 if (want_vaddr) {
569 if (PageHighMem(page))
570 __dma_free_remap(cpu_addr, size);
571 else
572 __dma_remap(page, size, PAGE_KERNEL);
573 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100574 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
575}
576
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200577static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
578{
579 prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
580 pgprot_writecombine(prot) :
581 pgprot_dmacoherent(prot);
582 return prot;
583}
584
Marek Szyprowskic7909502011-12-29 13:09:51 +0100585#define nommu() 0
586
Catalin Marinasab6494f2009-07-24 12:35:02 +0100587#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000588
Marek Szyprowskic7909502011-12-29 13:09:51 +0100589#define nommu() 1
590
Carlo Caione6e8266e2015-02-09 10:38:35 +0100591#define __get_dma_pgprot(attrs, prot) __pgprot(0)
592#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200593#define __alloc_from_pool(size, ret_page) NULL
Carlo Caione6e8266e2015-02-09 10:38:35 +0100594#define __alloc_from_contiguous(dev, size, prot, ret, c, wv) NULL
Marek Szyprowskic7909502011-12-29 13:09:51 +0100595#define __free_from_pool(cpu_addr, size) 0
Carlo Caione6e8266e2015-02-09 10:38:35 +0100596#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100597#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000598
599#endif /* CONFIG_MMU */
600
Marek Szyprowskic7909502011-12-29 13:09:51 +0100601static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
602 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100603{
Russell King04da5692009-11-19 15:54:45 +0000604 struct page *page;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100605 page = __dma_alloc_buffer(dev, size, gfp);
606 if (!page)
607 return NULL;
608
609 *ret_page = page;
610 return page_address(page);
611}
612
613
614
615static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100616 gfp_t gfp, pgprot_t prot, bool is_coherent,
617 struct dma_attrs *attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100618{
619 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900620 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000621 void *addr;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100622 bool want_vaddr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100623
Marek Szyprowskic7909502011-12-29 13:09:51 +0100624#ifdef CONFIG_DMA_API_DEBUG
625 u64 limit = (mask + 1) & ~mask;
626 if (limit && size >= limit) {
627 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
628 size, mask);
629 return NULL;
630 }
631#endif
632
633 if (!mask)
634 return NULL;
635
636 if (mask < 0xffffffffULL)
637 gfp |= GFP_DMA;
638
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100639 /*
640 * Following is a work-around (a.k.a. hack) to prevent pages
641 * with __GFP_COMP being passed to split_page() which cannot
642 * handle them. The real problem is that this flag probably
643 * should be 0 on ARM as it is not supported on this
644 * platform; see CONFIG_HUGETLBFS.
645 */
646 gfp &= ~(__GFP_COMP);
647
Marek Szyprowski553ac782012-02-29 14:45:28 +0100648 *handle = DMA_ERROR_CODE;
Russell King04da5692009-11-19 15:54:45 +0000649 size = PAGE_ALIGN(size);
Carlo Caione6e8266e2015-02-09 10:38:35 +0100650 want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
Russell King04da5692009-11-19 15:54:45 +0000651
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100652 if (nommu())
653 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Mel Gormand0164ad2015-11-06 16:28:21 -0800654 else if (dev_get_cma_area(dev) && (gfp & __GFP_DIRECT_RECLAIM))
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100655 addr = __alloc_from_contiguous(dev, size, prot, &page,
656 caller, want_vaddr);
657 else if (is_coherent)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100658 addr = __alloc_simple_buffer(dev, size, gfp, &page);
Mel Gormand0164ad2015-11-06 16:28:21 -0800659 else if (!gfpflags_allow_blocking(gfp))
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200660 addr = __alloc_from_pool(size, &page);
Russell King31ebf942009-11-19 21:12:17 +0000661 else
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100662 addr = __alloc_remap_buffer(dev, size, gfp, prot, &page,
663 caller, want_vaddr);
Russell King31ebf942009-11-19 21:12:17 +0000664
Carlo Caione6e8266e2015-02-09 10:38:35 +0100665 if (page)
Russell King9eedd962011-01-03 00:00:17 +0000666 *handle = pfn_to_dma(dev, page_to_pfn(page));
Russell King31ebf942009-11-19 21:12:17 +0000667
Carlo Caione6e8266e2015-02-09 10:38:35 +0100668 return want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100669}
Russell King695ae0a2009-11-19 16:31:39 +0000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
672 * Allocate DMA-coherent memory space and return both the kernel remapped
673 * virtual and bus address for that space.
674 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200675void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
676 gfp_t gfp, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Russell King0ea1ec72013-10-23 16:14:59 +0100678 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400679
Rob Herringdd37e942012-08-21 12:20:17 +0200680 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100681 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200682}
683
684static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
685 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
686{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100687 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100688 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Mike Looijmans55af8a92015-06-03 11:25:31 +0100691static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200692 void *cpu_addr, dma_addr_t dma_addr, size_t size,
693 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100695 int ret = -ENXIO;
696#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200697 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
698 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100699 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200700 unsigned long off = vma->vm_pgoff;
701
Marek Szyprowski47142f02012-05-15 19:04:13 +0200702 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
703 return ret;
704
Marek Szyprowski50262a42012-07-30 09:35:26 +0200705 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
706 ret = remap_pfn_range(vma, vma->vm_start,
707 pfn + off,
708 vma->vm_end - vma->vm_start,
709 vma->vm_page_prot);
710 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100711#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return ret;
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100717 * Create userspace mapping for the DMA-coherent memory.
718 */
719static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
720 void *cpu_addr, dma_addr_t dma_addr, size_t size,
721 struct dma_attrs *attrs)
722{
723 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
724}
725
726int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
727 void *cpu_addr, dma_addr_t dma_addr, size_t size,
728 struct dma_attrs *attrs)
729{
730#ifdef CONFIG_MMU
731 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
732#endif /* CONFIG_MMU */
733 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
734}
735
736/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100737 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Rob Herringdd37e942012-08-21 12:20:17 +0200739static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
740 dma_addr_t handle, struct dma_attrs *attrs,
741 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100743 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Carlo Caione6e8266e2015-02-09 10:38:35 +0100744 bool want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
Russell King5edf71a2005-11-25 15:52:51 +0000745
Russell King3e82d012009-11-19 15:38:12 +0000746 size = PAGE_ALIGN(size);
747
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100748 if (nommu()) {
Marek Szyprowskic7909502011-12-29 13:09:51 +0100749 __dma_free_buffer(page, size);
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100750 } else if (!is_coherent && __free_from_pool(cpu_addr, size)) {
Aaro Koskinend9e0d142012-08-07 14:44:05 +0200751 return;
Gioh Kime464ef12014-05-22 13:38:23 +0900752 } else if (!dev_get_cma_area(dev)) {
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100753 if (want_vaddr && !is_coherent)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100754 __dma_free_remap(cpu_addr, size);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100755 __dma_free_buffer(page, size);
756 } else {
Marek Szyprowskic7909502011-12-29 13:09:51 +0100757 /*
758 * Non-atomic allocations cannot be freed with IRQs disabled
759 */
760 WARN_ON(irqs_disabled());
Carlo Caione6e8266e2015-02-09 10:38:35 +0100761 __free_from_contiguous(dev, page, cpu_addr, size, want_vaddr);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
Russell Kingafd1a322008-09-25 16:30:57 +0100764
Rob Herringdd37e942012-08-21 12:20:17 +0200765void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
766 dma_addr_t handle, struct dma_attrs *attrs)
767{
768 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
769}
770
771static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
772 dma_addr_t handle, struct dma_attrs *attrs)
773{
774 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
775}
776
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200777int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
778 void *cpu_addr, dma_addr_t handle, size_t size,
779 struct dma_attrs *attrs)
780{
781 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
782 int ret;
783
784 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
785 if (unlikely(ret))
786 return ret;
787
788 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
789 return 0;
790}
791
Russell King65af1912009-11-24 17:53:33 +0000792static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000793 size_t size, enum dma_data_direction dir,
794 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000795{
Russell King15653372013-01-19 11:05:57 +0000796 unsigned long pfn;
797 size_t left = size;
798
799 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
800 offset %= PAGE_SIZE;
801
Russell King65af1912009-11-24 17:53:33 +0000802 /*
803 * A single sg entry may refer to multiple physically contiguous
804 * pages. But we still need to process highmem pages individually.
805 * If highmem is not configured then the bulk of this loop gets
806 * optimized out.
807 */
Russell King65af1912009-11-24 17:53:33 +0000808 do {
809 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000810 void *vaddr;
811
Russell King15653372013-01-19 11:05:57 +0000812 page = pfn_to_page(pfn);
813
Russell King93f1d622009-11-24 14:41:01 +0000814 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000815 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000816 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100817
818 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500819 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100820 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500821 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100822 } else {
823 vaddr = kmap_high_get(page);
824 if (vaddr) {
825 op(vaddr + offset, len, dir);
826 kunmap_high(page);
827 }
Russell King93f1d622009-11-24 14:41:01 +0000828 }
829 } else {
830 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000831 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000832 }
Russell King65af1912009-11-24 17:53:33 +0000833 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000834 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000835 left -= len;
836 } while (left);
837}
838
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100839/*
840 * Make an area consistent for devices.
841 * Note: Drivers should NOT use this function directly, as it will break
842 * platforms with CONFIG_DMABOUNCE.
843 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
844 */
845static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000846 size_t size, enum dma_data_direction dir)
847{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400848 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400849
Russell Kinga9c91472009-11-26 16:19:58 +0000850 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400851
Russell King65af1912009-11-24 17:53:33 +0000852 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000853 if (dir == DMA_FROM_DEVICE) {
854 outer_inv_range(paddr, paddr + size);
855 } else {
856 outer_clean_range(paddr, paddr + size);
857 }
858 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400859}
Russell King4ea0d732009-11-24 16:27:17 +0000860
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100861static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +0000862 size_t size, enum dma_data_direction dir)
863{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400864 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000865
866 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +0100867 /* in any case, don't bother invalidating if DMA to device */
868 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +0000869 outer_inv_range(paddr, paddr + size);
870
Russell Kingdeace4a2014-05-03 11:06:55 +0100871 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
872 }
Catalin Marinasc0177802010-09-13 15:57:36 +0100873
874 /*
Ming Leib2a234e2013-05-18 11:21:36 +0100875 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +0100876 */
Ming Leib2a234e2013-05-18 11:21:36 +0100877 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
878 unsigned long pfn;
879 size_t left = size;
880
881 pfn = page_to_pfn(page) + off / PAGE_SIZE;
882 off %= PAGE_SIZE;
883 if (off) {
884 pfn++;
885 left -= PAGE_SIZE - off;
886 }
887 while (left >= PAGE_SIZE) {
888 page = pfn_to_page(pfn++);
889 set_bit(PG_dcache_clean, &page->flags);
890 left -= PAGE_SIZE;
891 }
892 }
Russell King4ea0d732009-11-24 16:27:17 +0000893}
Nicolas Pitre43377452009-03-12 22:52:09 -0400894
Russell Kingafd1a322008-09-25 16:30:57 +0100895/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100896 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +0100897 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
898 * @sg: list of buffers
899 * @nents: number of buffers to map
900 * @dir: DMA transfer direction
901 *
902 * Map a set of buffers described by scatterlist in streaming mode for DMA.
903 * This is the scatter-gather version of the dma_map_single interface.
904 * Here the scatter gather list elements are each tagged with the
905 * appropriate dma address and length. They are obtained via
906 * sg_dma_{address,length}.
907 *
908 * Device ownership issues as mentioned for dma_map_single are the same
909 * here.
910 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100911int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
912 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100913{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100914 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100915 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100916 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +0100917
918 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200919#ifdef CONFIG_NEED_SG_DMA_LENGTH
920 s->dma_length = s->length;
921#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100922 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
923 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100924 if (dma_mapping_error(dev, s->dma_address))
925 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +0100926 }
Russell Kingafd1a322008-09-25 16:30:57 +0100927 return nents;
Russell King01135d922008-09-25 21:05:02 +0100928
929 bad_mapping:
930 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100931 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +0100932 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +0100933}
Russell Kingafd1a322008-09-25 16:30:57 +0100934
935/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100936 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +0100937 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
938 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +0100939 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +0100940 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
941 *
942 * Unmap a set of streaming mode DMA translations. Again, CPU access
943 * rules concerning calls here are the same as for dma_unmap_single().
944 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100945void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
946 enum dma_data_direction dir, struct dma_attrs *attrs)
Russell Kingafd1a322008-09-25 16:30:57 +0100947{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100948 struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +0100949 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +0100950
Russell King01135d922008-09-25 21:05:02 +0100951 int i;
Russell King24056f52011-01-03 11:29:28 +0000952
Russell King01135d922008-09-25 21:05:02 +0100953 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100954 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +0100955}
Russell Kingafd1a322008-09-25 16:30:57 +0100956
957/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100958 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +0100959 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
960 * @sg: list of buffers
961 * @nents: number of buffers to map (returned from dma_map_sg)
962 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
963 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100964void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100965 int nents, enum dma_data_direction dir)
966{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100967 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100968 struct scatterlist *s;
969 int i;
970
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100971 for_each_sg(sg, s, nents, i)
972 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
973 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100974}
Russell Kingafd1a322008-09-25 16:30:57 +0100975
976/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100977 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +0100978 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
979 * @sg: list of buffers
980 * @nents: number of buffers to map (returned from dma_map_sg)
981 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
982 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100983void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +0100984 int nents, enum dma_data_direction dir)
985{
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100986 struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +0100987 struct scatterlist *s;
988 int i;
989
Marek Szyprowski2a550e72012-02-10 19:55:20 +0100990 for_each_sg(sg, s, nents, i)
991 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
992 dir);
Russell Kingafd1a322008-09-25 16:30:57 +0100993}
Russell King24056f52011-01-03 11:29:28 +0000994
Russell King022ae532011-07-08 21:26:59 +0100995/*
996 * Return whether the given device DMA address mask can be supported
997 * properly. For example, if your device can only drive the low 24-bits
998 * during bus mastering, then you would pass 0x00ffffff as the mask
999 * to this function.
1000 */
1001int dma_supported(struct device *dev, u64 mask)
1002{
Russell King9f28cde2013-12-06 12:30:42 +00001003 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001004}
1005EXPORT_SYMBOL(dma_supported);
1006
Gregory CLEMENT87b54e72012-11-21 09:39:19 +01001007int arm_dma_set_mask(struct device *dev, u64 dma_mask)
Russell King022ae532011-07-08 21:26:59 +01001008{
1009 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1010 return -EIO;
1011
Russell King022ae532011-07-08 21:26:59 +01001012 *dev->dma_mask = dma_mask;
Russell King022ae532011-07-08 21:26:59 +01001013
1014 return 0;
1015}
Russell King022ae532011-07-08 21:26:59 +01001016
Russell King24056f52011-01-03 11:29:28 +00001017#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1018
1019static int __init dma_debug_do_init(void)
1020{
1021 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1022 return 0;
1023}
1024fs_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001025
1026#ifdef CONFIG_ARM_DMA_USE_IOMMU
1027
1028/* IOMMU */
1029
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001030static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1031
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001032static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1033 size_t size)
1034{
1035 unsigned int order = get_order(size);
1036 unsigned int align = 0;
1037 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301038 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001039 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001040 dma_addr_t iova;
1041 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001042
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001043 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1044 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1045
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001046 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1047 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001048
1049 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001050 for (i = 0; i < mapping->nr_bitmaps; i++) {
1051 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1052 mapping->bits, 0, count, align);
1053
1054 if (start > mapping->bits)
1055 continue;
1056
1057 bitmap_set(mapping->bitmaps[i], start, count);
1058 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001059 }
1060
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001061 /*
1062 * No unused range found. Try to extend the existing mapping
1063 * and perform a second attempt to reserve an IO virtual
1064 * address range of size bytes.
1065 */
1066 if (i == mapping->nr_bitmaps) {
1067 if (extend_iommu_mapping(mapping)) {
1068 spin_unlock_irqrestore(&mapping->lock, flags);
1069 return DMA_ERROR_CODE;
1070 }
1071
1072 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1073 mapping->bits, 0, count, align);
1074
1075 if (start > mapping->bits) {
1076 spin_unlock_irqrestore(&mapping->lock, flags);
1077 return DMA_ERROR_CODE;
1078 }
1079
1080 bitmap_set(mapping->bitmaps[i], start, count);
1081 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001082 spin_unlock_irqrestore(&mapping->lock, flags);
1083
Ritesh Harjani006f8412014-05-20 10:02:59 +05301084 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001085 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001086
1087 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001088}
1089
1090static inline void __free_iova(struct dma_iommu_mapping *mapping,
1091 dma_addr_t addr, size_t size)
1092{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001093 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301094 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001095 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001096 dma_addr_t bitmap_base;
1097 u32 bitmap_index;
1098
1099 if (!size)
1100 return;
1101
Ritesh Harjani006f8412014-05-20 10:02:59 +05301102 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001103 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1104
Ritesh Harjani006f8412014-05-20 10:02:59 +05301105 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001106
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001107 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001108
Ritesh Harjani006f8412014-05-20 10:02:59 +05301109 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001110 /*
1111 * The address range to be freed reaches into the iova
1112 * range of the next bitmap. This should not happen as
1113 * we don't allow this in __alloc_iova (at the
1114 * moment).
1115 */
1116 BUG();
1117 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001118 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001119
1120 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001121 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001122 spin_unlock_irqrestore(&mapping->lock, flags);
1123}
1124
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001125static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1126 gfp_t gfp, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001127{
1128 struct page **pages;
1129 int count = size >> PAGE_SHIFT;
1130 int array_size = count * sizeof(struct page *);
1131 int i = 0;
1132
1133 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001134 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001135 else
1136 pages = vzalloc(array_size);
1137 if (!pages)
1138 return NULL;
1139
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001140 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
1141 {
1142 unsigned long order = get_order(size);
1143 struct page *page;
1144
1145 page = dma_alloc_from_contiguous(dev, count, order);
1146 if (!page)
1147 goto error;
1148
1149 __dma_clear_buffer(page, size);
1150
1151 for (i = 0; i < count; i++)
1152 pages[i] = page + i;
1153
1154 return pages;
1155 }
1156
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001157 /*
1158 * IOMMU can map any pages, so himem can also be used here
1159 */
1160 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1161
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001162 while (count) {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001163 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001164
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001165 for (order = __fls(count); order > 0; --order) {
1166 /*
1167 * We do not want OOM killer to be invoked as long
1168 * as we can fall back to single pages, so we force
1169 * __GFP_NORETRY for orders higher than zero.
1170 */
1171 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1172 if (pages[i])
1173 break;
1174 }
1175
1176 if (!pages[i]) {
1177 /*
1178 * Fall back to single page allocation.
1179 * Might invoke OOM killer as last resort.
1180 */
1181 pages[i] = alloc_pages(gfp, 0);
1182 if (!pages[i])
1183 goto error;
1184 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001185
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001186 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001187 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001188 j = 1 << order;
1189 while (--j)
1190 pages[i + j] = pages[i] + j;
1191 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001192
1193 __dma_clear_buffer(pages[i], PAGE_SIZE << order);
1194 i += 1 << order;
1195 count -= 1 << order;
1196 }
1197
1198 return pages;
1199error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001200 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001201 if (pages[i])
1202 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001203 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001204 return NULL;
1205}
1206
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001207static int __iommu_free_buffer(struct device *dev, struct page **pages,
1208 size_t size, struct dma_attrs *attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001209{
1210 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001211 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001212
1213 if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
1214 dma_release_from_contiguous(dev, pages[0], count);
1215 } else {
1216 for (i = 0; i < count; i++)
1217 if (pages[i])
1218 __free_pages(pages[i], 0);
1219 }
1220
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001221 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001222 return 0;
1223}
1224
1225/*
1226 * Create a CPU mapping for a specified pages
1227 */
1228static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001229__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1230 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001231{
Laura Abbott513510d2014-10-09 15:26:40 -07001232 return dma_common_pages_remap(pages, size,
1233 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001234}
1235
1236/*
1237 * Create a mapping in device IO address space for specified pages
1238 */
1239static dma_addr_t
1240__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1241{
Will Deacon89cfdb12015-01-16 18:02:15 +01001242 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001243 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1244 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001245 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001246
1247 dma_addr = __alloc_iova(mapping, size);
1248 if (dma_addr == DMA_ERROR_CODE)
1249 return dma_addr;
1250
1251 iova = dma_addr;
1252 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001253 int ret;
1254
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001255 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1256 phys_addr_t phys = page_to_phys(pages[i]);
1257 unsigned int len, j;
1258
1259 for (j = i + 1; j < count; j++, next_pfn++)
1260 if (page_to_pfn(pages[j]) != next_pfn)
1261 break;
1262
1263 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001264 ret = iommu_map(mapping->domain, iova, phys, len,
1265 IOMMU_READ|IOMMU_WRITE);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001266 if (ret < 0)
1267 goto fail;
1268 iova += len;
1269 i = j;
1270 }
1271 return dma_addr;
1272fail:
1273 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1274 __free_iova(mapping, dma_addr, size);
1275 return DMA_ERROR_CODE;
1276}
1277
1278static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1279{
Will Deacon89cfdb12015-01-16 18:02:15 +01001280 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001281
1282 /*
1283 * add optional in-page offset from iova to size and align
1284 * result to page size
1285 */
1286 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1287 iova &= PAGE_MASK;
1288
1289 iommu_unmap(mapping->domain, iova, size);
1290 __free_iova(mapping, iova, size);
1291 return 0;
1292}
1293
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001294static struct page **__atomic_get_pages(void *addr)
1295{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001296 struct page *page;
1297 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001298
Laura Abbott36d0fd22014-10-09 15:26:42 -07001299 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1300 page = phys_to_page(phys);
1301
1302 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001303}
1304
Marek Szyprowski955c7572012-05-16 19:38:58 +01001305static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001306{
1307 struct vm_struct *area;
1308
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001309 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1310 return __atomic_get_pages(cpu_addr);
1311
Marek Szyprowski955c7572012-05-16 19:38:58 +01001312 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1313 return cpu_addr;
1314
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001315 area = find_vm_area(cpu_addr);
1316 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1317 return area->pages;
1318 return NULL;
1319}
1320
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001321static void *__iommu_alloc_atomic(struct device *dev, size_t size,
1322 dma_addr_t *handle)
1323{
1324 struct page *page;
1325 void *addr;
1326
1327 addr = __alloc_from_pool(size, &page);
1328 if (!addr)
1329 return NULL;
1330
1331 *handle = __iommu_create_mapping(dev, &page, size);
1332 if (*handle == DMA_ERROR_CODE)
1333 goto err_mapping;
1334
1335 return addr;
1336
1337err_mapping:
1338 __free_from_pool(addr, size);
1339 return NULL;
1340}
1341
Marek Szyprowskid5898292013-02-08 10:54:48 +01001342static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001343 dma_addr_t handle, size_t size)
1344{
1345 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowskid5898292013-02-08 10:54:48 +01001346 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001347}
1348
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001349static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1350 dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1351{
Russell King71b55662013-11-25 12:01:03 +00001352 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001353 struct page **pages;
1354 void *addr = NULL;
1355
1356 *handle = DMA_ERROR_CODE;
1357 size = PAGE_ALIGN(size);
1358
Mel Gormand0164ad2015-11-06 16:28:21 -08001359 if (!gfpflags_allow_blocking(gfp))
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001360 return __iommu_alloc_atomic(dev, size, handle);
1361
Richard Zhao5b91a982013-06-20 20:31:00 +08001362 /*
1363 * Following is a work-around (a.k.a. hack) to prevent pages
1364 * with __GFP_COMP being passed to split_page() which cannot
1365 * handle them. The real problem is that this flag probably
1366 * should be 0 on ARM as it is not supported on this
1367 * platform; see CONFIG_HUGETLBFS.
1368 */
1369 gfp &= ~(__GFP_COMP);
1370
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001371 pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001372 if (!pages)
1373 return NULL;
1374
1375 *handle = __iommu_create_mapping(dev, pages, size);
1376 if (*handle == DMA_ERROR_CODE)
1377 goto err_buffer;
1378
Marek Szyprowski955c7572012-05-16 19:38:58 +01001379 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1380 return pages;
1381
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001382 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1383 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001384 if (!addr)
1385 goto err_mapping;
1386
1387 return addr;
1388
1389err_mapping:
1390 __iommu_remove_mapping(dev, *handle, size);
1391err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001392 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001393 return NULL;
1394}
1395
1396static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1397 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1398 struct dma_attrs *attrs)
1399{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001400 unsigned long uaddr = vma->vm_start;
1401 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001402 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001403 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1404 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001405
1406 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001407
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001408 if (!pages)
1409 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001410
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001411 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1412 return -ENXIO;
1413
Marek Szyprowski7e312102015-08-28 09:42:09 +01001414 pages += off;
1415
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001416 do {
1417 int ret = vm_insert_page(vma, uaddr, *pages++);
1418 if (ret) {
1419 pr_err("Remapping memory failed: %d\n", ret);
1420 return ret;
1421 }
1422 uaddr += PAGE_SIZE;
1423 usize -= PAGE_SIZE;
1424 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001425
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001426 return 0;
1427}
1428
1429/*
1430 * free a page as defined by the above mapping.
1431 * Must not be called with IRQs disabled.
1432 */
1433void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1434 dma_addr_t handle, struct dma_attrs *attrs)
1435{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001436 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001437 size = PAGE_ALIGN(size);
1438
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001439 if (__in_atomic_pool(cpu_addr, size)) {
Marek Szyprowskid5898292013-02-08 10:54:48 +01001440 __iommu_free_atomic(dev, cpu_addr, handle, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001441 return;
1442 }
1443
YoungJun Cho836bfa02013-06-17 13:18:52 +09001444 pages = __iommu_get_pages(cpu_addr, attrs);
1445 if (!pages) {
1446 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1447 return;
1448 }
1449
Marek Szyprowski955c7572012-05-16 19:38:58 +01001450 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
Laura Abbott513510d2014-10-09 15:26:40 -07001451 dma_common_free_remap(cpu_addr, size,
1452 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001453 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001454
1455 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001456 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001457}
1458
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001459static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1460 void *cpu_addr, dma_addr_t dma_addr,
1461 size_t size, struct dma_attrs *attrs)
1462{
1463 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1464 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1465
1466 if (!pages)
1467 return -ENXIO;
1468
1469 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1470 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001471}
1472
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001473static int __dma_direction_to_prot(enum dma_data_direction dir)
1474{
1475 int prot;
1476
1477 switch (dir) {
1478 case DMA_BIDIRECTIONAL:
1479 prot = IOMMU_READ | IOMMU_WRITE;
1480 break;
1481 case DMA_TO_DEVICE:
1482 prot = IOMMU_READ;
1483 break;
1484 case DMA_FROM_DEVICE:
1485 prot = IOMMU_WRITE;
1486 break;
1487 default:
1488 prot = 0;
1489 }
1490
1491 return prot;
1492}
1493
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001494/*
1495 * Map a part of the scatter-gather list into contiguous io address space
1496 */
1497static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1498 size_t size, dma_addr_t *handle,
Rob Herring0fa478d2012-08-21 12:23:23 +02001499 enum dma_data_direction dir, struct dma_attrs *attrs,
1500 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001501{
Will Deacon89cfdb12015-01-16 18:02:15 +01001502 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001503 dma_addr_t iova, iova_base;
1504 int ret = 0;
1505 unsigned int count;
1506 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001507 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001508
1509 size = PAGE_ALIGN(size);
1510 *handle = DMA_ERROR_CODE;
1511
1512 iova_base = iova = __alloc_iova(mapping, size);
1513 if (iova == DMA_ERROR_CODE)
1514 return -ENOMEM;
1515
1516 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001517 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001518 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1519
Rob Herring0fa478d2012-08-21 12:23:23 +02001520 if (!is_coherent &&
1521 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001522 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1523
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001524 prot = __dma_direction_to_prot(dir);
1525
1526 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001527 if (ret < 0)
1528 goto fail;
1529 count += len >> PAGE_SHIFT;
1530 iova += len;
1531 }
1532 *handle = iova_base;
1533
1534 return 0;
1535fail:
1536 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1537 __free_iova(mapping, iova_base, size);
1538 return ret;
1539}
1540
Rob Herring0fa478d2012-08-21 12:23:23 +02001541static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1542 enum dma_data_direction dir, struct dma_attrs *attrs,
1543 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001544{
1545 struct scatterlist *s = sg, *dma = sg, *start = sg;
1546 int i, count = 0;
1547 unsigned int offset = s->offset;
1548 unsigned int size = s->offset + s->length;
1549 unsigned int max = dma_get_max_seg_size(dev);
1550
1551 for (i = 1; i < nents; i++) {
1552 s = sg_next(s);
1553
1554 s->dma_address = DMA_ERROR_CODE;
1555 s->dma_length = 0;
1556
1557 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1558 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001559 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001560 goto bad_mapping;
1561
1562 dma->dma_address += offset;
1563 dma->dma_length = size - offset;
1564
1565 size = offset = s->offset;
1566 start = s;
1567 dma = sg_next(dma);
1568 count += 1;
1569 }
1570 size += s->length;
1571 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001572 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1573 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001574 goto bad_mapping;
1575
1576 dma->dma_address += offset;
1577 dma->dma_length = size - offset;
1578
1579 return count+1;
1580
1581bad_mapping:
1582 for_each_sg(sg, s, count, i)
1583 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1584 return 0;
1585}
1586
1587/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001588 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1589 * @dev: valid struct device pointer
1590 * @sg: list of buffers
1591 * @nents: number of buffers to map
1592 * @dir: DMA transfer direction
1593 *
1594 * Map a set of i/o coherent buffers described by scatterlist in streaming
1595 * mode for DMA. The scatter gather list elements are merged together (if
1596 * possible) and tagged with the appropriate dma address and length. They are
1597 * obtained via sg_dma_{address,length}.
1598 */
1599int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1600 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1601{
1602 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1603}
1604
1605/**
1606 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1607 * @dev: valid struct device pointer
1608 * @sg: list of buffers
1609 * @nents: number of buffers to map
1610 * @dir: DMA transfer direction
1611 *
1612 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1613 * The scatter gather list elements are merged together (if possible) and
1614 * tagged with the appropriate dma address and length. They are obtained via
1615 * sg_dma_{address,length}.
1616 */
1617int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1618 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1619{
1620 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1621}
1622
1623static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1624 int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
1625 bool is_coherent)
1626{
1627 struct scatterlist *s;
1628 int i;
1629
1630 for_each_sg(sg, s, nents, i) {
1631 if (sg_dma_len(s))
1632 __iommu_remove_mapping(dev, sg_dma_address(s),
1633 sg_dma_len(s));
1634 if (!is_coherent &&
1635 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1636 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1637 s->length, dir);
1638 }
1639}
1640
1641/**
1642 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1643 * @dev: valid struct device pointer
1644 * @sg: list of buffers
1645 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1646 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1647 *
1648 * Unmap a set of streaming mode DMA translations. Again, CPU access
1649 * rules concerning calls here are the same as for dma_unmap_single().
1650 */
1651void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1652 int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1653{
1654 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1655}
1656
1657/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001658 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1659 * @dev: valid struct device pointer
1660 * @sg: list of buffers
1661 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1662 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1663 *
1664 * Unmap a set of streaming mode DMA translations. Again, CPU access
1665 * rules concerning calls here are the same as for dma_unmap_single().
1666 */
1667void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1668 enum dma_data_direction dir, struct dma_attrs *attrs)
1669{
Rob Herring0fa478d2012-08-21 12:23:23 +02001670 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001671}
1672
1673/**
1674 * arm_iommu_sync_sg_for_cpu
1675 * @dev: valid struct device pointer
1676 * @sg: list of buffers
1677 * @nents: number of buffers to map (returned from dma_map_sg)
1678 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1679 */
1680void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1681 int nents, enum dma_data_direction dir)
1682{
1683 struct scatterlist *s;
1684 int i;
1685
1686 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001687 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001688
1689}
1690
1691/**
1692 * arm_iommu_sync_sg_for_device
1693 * @dev: valid struct device pointer
1694 * @sg: list of buffers
1695 * @nents: number of buffers to map (returned from dma_map_sg)
1696 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1697 */
1698void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1699 int nents, enum dma_data_direction dir)
1700{
1701 struct scatterlist *s;
1702 int i;
1703
1704 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001705 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001706}
1707
1708
1709/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001710 * arm_coherent_iommu_map_page
1711 * @dev: valid struct device pointer
1712 * @page: page that buffer resides in
1713 * @offset: offset into page for start of buffer
1714 * @size: size of buffer to map
1715 * @dir: DMA transfer direction
1716 *
1717 * Coherent IOMMU aware version of arm_dma_map_page()
1718 */
1719static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1720 unsigned long offset, size_t size, enum dma_data_direction dir,
1721 struct dma_attrs *attrs)
1722{
Will Deacon89cfdb12015-01-16 18:02:15 +01001723 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001724 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001725 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001726
1727 dma_addr = __alloc_iova(mapping, len);
1728 if (dma_addr == DMA_ERROR_CODE)
1729 return dma_addr;
1730
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001731 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01001732
1733 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001734 if (ret < 0)
1735 goto fail;
1736
1737 return dma_addr + offset;
1738fail:
1739 __free_iova(mapping, dma_addr, len);
1740 return DMA_ERROR_CODE;
1741}
1742
1743/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001744 * arm_iommu_map_page
1745 * @dev: valid struct device pointer
1746 * @page: page that buffer resides in
1747 * @offset: offset into page for start of buffer
1748 * @size: size of buffer to map
1749 * @dir: DMA transfer direction
1750 *
1751 * IOMMU aware version of arm_dma_map_page()
1752 */
1753static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1754 unsigned long offset, size_t size, enum dma_data_direction dir,
1755 struct dma_attrs *attrs)
1756{
Rob Herring0fa478d2012-08-21 12:23:23 +02001757 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001758 __dma_page_cpu_to_dev(page, offset, size, dir);
1759
Rob Herring0fa478d2012-08-21 12:23:23 +02001760 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1761}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001762
Rob Herring0fa478d2012-08-21 12:23:23 +02001763/**
1764 * arm_coherent_iommu_unmap_page
1765 * @dev: valid struct device pointer
1766 * @handle: DMA address of buffer
1767 * @size: size of buffer (same as passed to dma_map_page)
1768 * @dir: DMA transfer direction (same as passed to dma_map_page)
1769 *
1770 * Coherent IOMMU aware version of arm_dma_unmap_page()
1771 */
1772static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1773 size_t size, enum dma_data_direction dir,
1774 struct dma_attrs *attrs)
1775{
Will Deacon89cfdb12015-01-16 18:02:15 +01001776 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001777 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001778 int offset = handle & ~PAGE_MASK;
1779 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001780
Rob Herring0fa478d2012-08-21 12:23:23 +02001781 if (!iova)
1782 return;
1783
1784 iommu_unmap(mapping->domain, iova, len);
1785 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001786}
1787
1788/**
1789 * arm_iommu_unmap_page
1790 * @dev: valid struct device pointer
1791 * @handle: DMA address of buffer
1792 * @size: size of buffer (same as passed to dma_map_page)
1793 * @dir: DMA transfer direction (same as passed to dma_map_page)
1794 *
1795 * IOMMU aware version of arm_dma_unmap_page()
1796 */
1797static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1798 size_t size, enum dma_data_direction dir,
1799 struct dma_attrs *attrs)
1800{
Will Deacon89cfdb12015-01-16 18:02:15 +01001801 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001802 dma_addr_t iova = handle & PAGE_MASK;
1803 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1804 int offset = handle & ~PAGE_MASK;
1805 int len = PAGE_ALIGN(size + offset);
1806
1807 if (!iova)
1808 return;
1809
Rob Herring0fa478d2012-08-21 12:23:23 +02001810 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001811 __dma_page_dev_to_cpu(page, offset, size, dir);
1812
1813 iommu_unmap(mapping->domain, iova, len);
1814 __free_iova(mapping, iova, len);
1815}
1816
1817static void arm_iommu_sync_single_for_cpu(struct device *dev,
1818 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1819{
Will Deacon89cfdb12015-01-16 18:02:15 +01001820 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001821 dma_addr_t iova = handle & PAGE_MASK;
1822 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1823 unsigned int offset = handle & ~PAGE_MASK;
1824
1825 if (!iova)
1826 return;
1827
Rob Herring0fa478d2012-08-21 12:23:23 +02001828 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001829}
1830
1831static void arm_iommu_sync_single_for_device(struct device *dev,
1832 dma_addr_t handle, size_t size, enum dma_data_direction dir)
1833{
Will Deacon89cfdb12015-01-16 18:02:15 +01001834 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001835 dma_addr_t iova = handle & PAGE_MASK;
1836 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1837 unsigned int offset = handle & ~PAGE_MASK;
1838
1839 if (!iova)
1840 return;
1841
1842 __dma_page_cpu_to_dev(page, offset, size, dir);
1843}
1844
1845struct dma_map_ops iommu_ops = {
1846 .alloc = arm_iommu_alloc_attrs,
1847 .free = arm_iommu_free_attrs,
1848 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001849 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001850
1851 .map_page = arm_iommu_map_page,
1852 .unmap_page = arm_iommu_unmap_page,
1853 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
1854 .sync_single_for_device = arm_iommu_sync_single_for_device,
1855
1856 .map_sg = arm_iommu_map_sg,
1857 .unmap_sg = arm_iommu_unmap_sg,
1858 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
1859 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Hiroshi Doyud09e1332013-01-29 14:57:07 +02001860
1861 .set_dma_mask = arm_dma_set_mask,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001862};
1863
Rob Herring0fa478d2012-08-21 12:23:23 +02001864struct dma_map_ops iommu_coherent_ops = {
1865 .alloc = arm_iommu_alloc_attrs,
1866 .free = arm_iommu_free_attrs,
1867 .mmap = arm_iommu_mmap_attrs,
1868 .get_sgtable = arm_iommu_get_sgtable,
1869
1870 .map_page = arm_coherent_iommu_map_page,
1871 .unmap_page = arm_coherent_iommu_unmap_page,
1872
1873 .map_sg = arm_coherent_iommu_map_sg,
1874 .unmap_sg = arm_coherent_iommu_unmap_sg,
Hiroshi Doyud09e1332013-01-29 14:57:07 +02001875
1876 .set_dma_mask = arm_dma_set_mask,
Rob Herring0fa478d2012-08-21 12:23:23 +02001877};
1878
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001879/**
1880 * arm_iommu_create_mapping
1881 * @bus: pointer to the bus holding the client device (for IOMMU calls)
1882 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001883 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001884 *
1885 * Creates a mapping structure which holds information about used/unused
1886 * IO address ranges, which is required to perform memory allocation and
1887 * mapping with IOMMU aware functions.
1888 *
1889 * The client device need to be attached to the mapping with
1890 * arm_iommu_attach_device function.
1891 */
1892struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01001893arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001894{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001895 unsigned int bits = size >> PAGE_SHIFT;
1896 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001897 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001898 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001899 int err = -ENOMEM;
1900
Marek Szyprowski14245322015-04-29 11:29:19 +01001901 /* currently only 32-bit DMA address space is supported */
1902 if (size > DMA_BIT_MASK(32) + 1)
1903 return ERR_PTR(-ERANGE);
1904
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001905 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001906 return ERR_PTR(-EINVAL);
1907
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001908 if (bitmap_size > PAGE_SIZE) {
1909 extensions = bitmap_size / PAGE_SIZE;
1910 bitmap_size = PAGE_SIZE;
1911 }
1912
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001913 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1914 if (!mapping)
1915 goto err;
1916
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001917 mapping->bitmap_size = bitmap_size;
1918 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001919 GFP_KERNEL);
1920 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001921 goto err2;
1922
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001923 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001924 if (!mapping->bitmaps[0])
1925 goto err3;
1926
1927 mapping->nr_bitmaps = 1;
1928 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001929 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001930 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001931
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001932 spin_lock_init(&mapping->lock);
1933
1934 mapping->domain = iommu_domain_alloc(bus);
1935 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001936 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001937
1938 kref_init(&mapping->kref);
1939 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001940err4:
1941 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001942err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001943 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001944err2:
1945 kfree(mapping);
1946err:
1947 return ERR_PTR(err);
1948}
Prathyush K18177d12013-01-04 06:22:42 -05001949EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001950
1951static void release_iommu_mapping(struct kref *kref)
1952{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001953 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001954 struct dma_iommu_mapping *mapping =
1955 container_of(kref, struct dma_iommu_mapping, kref);
1956
1957 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001958 for (i = 0; i < mapping->nr_bitmaps; i++)
1959 kfree(mapping->bitmaps[i]);
1960 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001961 kfree(mapping);
1962}
1963
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001964static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
1965{
1966 int next_bitmap;
1967
Marek Szyprowski462859aa2015-07-08 13:21:55 +01001968 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001969 return -EINVAL;
1970
1971 next_bitmap = mapping->nr_bitmaps;
1972 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
1973 GFP_ATOMIC);
1974 if (!mapping->bitmaps[next_bitmap])
1975 return -ENOMEM;
1976
1977 mapping->nr_bitmaps++;
1978
1979 return 0;
1980}
1981
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001982void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1983{
1984 if (mapping)
1985 kref_put(&mapping->kref, release_iommu_mapping);
1986}
Prathyush K18177d12013-01-04 06:22:42 -05001987EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001988
Laurent Pincharteab8d652015-01-23 16:21:49 +02001989static int __arm_iommu_attach_device(struct device *dev,
1990 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001991{
1992 int err;
1993
1994 err = iommu_attach_device(mapping->domain, dev);
1995 if (err)
1996 return err;
1997
1998 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01001999 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002000
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002001 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002002 return 0;
2003}
2004
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002005/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002006 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002007 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002008 * @mapping: io address space mapping structure (returned from
2009 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002010 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002011 * Attaches specified io address space mapping to the provided device.
2012 * This replaces the dma operations (dma_map_ops pointer) with the
2013 * IOMMU aware version.
2014 *
2015 * More than one client might be attached to the same io address space
2016 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002017 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002018int arm_iommu_attach_device(struct device *dev,
2019 struct dma_iommu_mapping *mapping)
2020{
2021 int err;
2022
2023 err = __arm_iommu_attach_device(dev, mapping);
2024 if (err)
2025 return err;
2026
2027 set_dma_ops(dev, &iommu_ops);
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2031
2032static void __arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002033{
2034 struct dma_iommu_mapping *mapping;
2035
2036 mapping = to_dma_iommu_mapping(dev);
2037 if (!mapping) {
2038 dev_warn(dev, "Not attached\n");
2039 return;
2040 }
2041
2042 iommu_detach_device(mapping->domain, dev);
2043 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002044 to_dma_iommu_mapping(dev) = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002045
2046 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2047}
Laurent Pincharteab8d652015-01-23 16:21:49 +02002048
2049/**
2050 * arm_iommu_detach_device
2051 * @dev: valid struct device pointer
2052 *
2053 * Detaches the provided device from a previously attached map.
2054 * This voids the dma operations (dma_map_ops pointer)
2055 */
2056void arm_iommu_detach_device(struct device *dev)
2057{
2058 __arm_iommu_detach_device(dev);
2059 set_dma_ops(dev, NULL);
2060}
Prathyush K18177d12013-01-04 06:22:42 -05002061EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002062
Will Deacon4bb25782014-08-27 17:52:44 +01002063static struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
2064{
2065 return coherent ? &iommu_coherent_ops : &iommu_ops;
2066}
2067
2068static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
2069 struct iommu_ops *iommu)
2070{
2071 struct dma_iommu_mapping *mapping;
2072
2073 if (!iommu)
2074 return false;
2075
2076 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2077 if (IS_ERR(mapping)) {
2078 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2079 size, dev_name(dev));
2080 return false;
2081 }
2082
Laurent Pincharteab8d652015-01-23 16:21:49 +02002083 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002084 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2085 dev_name(dev));
2086 arm_iommu_release_mapping(mapping);
2087 return false;
2088 }
2089
2090 return true;
2091}
2092
2093static void arm_teardown_iommu_dma_ops(struct device *dev)
2094{
Will Deacon89cfdb12015-01-16 18:02:15 +01002095 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002096
Will Deaconc2273a12015-01-16 18:01:43 +01002097 if (!mapping)
2098 return;
2099
Laurent Pincharteab8d652015-01-23 16:21:49 +02002100 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002101 arm_iommu_release_mapping(mapping);
2102}
2103
2104#else
2105
2106static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
2107 struct iommu_ops *iommu)
2108{
2109 return false;
2110}
2111
2112static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2113
2114#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2115
2116#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2117
2118static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
2119{
2120 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2121}
2122
2123void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
2124 struct iommu_ops *iommu, bool coherent)
2125{
2126 struct dma_map_ops *dma_ops;
2127
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002128 dev->archdata.dma_coherent = coherent;
Will Deacon4bb25782014-08-27 17:52:44 +01002129 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2130 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2131 else
2132 dma_ops = arm_get_dma_map_ops(coherent);
2133
2134 set_dma_ops(dev, dma_ops);
2135}
2136
2137void arch_teardown_dma_ops(struct device *dev)
2138{
2139 arm_teardown_iommu_dma_ops(dev);
2140}