blob: 441063f1f50e265332f7db7778a6fb4d7f3b0b73 [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>
Laura Abbott29defcc2014-08-01 16:13:40 -070031#include <linux/msm_dma_iommu_mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010033#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040034#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010037#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020038#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010039#include <asm/mach/map.h>
40#include <asm/system_info.h>
41#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Russell King1234e3f2015-07-24 09:10:55 +010043#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010044#include "mm.h"
45
Rabin Vincentb4268672016-03-03 15:58:01 +010046struct arm_dma_alloc_args {
47 struct device *dev;
48 size_t size;
49 gfp_t gfp;
50 pgprot_t prot;
51 const void *caller;
52 bool want_vaddr;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010053 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010054};
55
56struct arm_dma_free_args {
57 struct device *dev;
58 size_t size;
59 void *cpu_addr;
60 struct page *page;
61 bool want_vaddr;
62};
63
Gregory CLEMENTf1270892016-04-15 11:15:18 +010064#define NORMAL 0
65#define COHERENT 1
66
Rabin Vincentb4268672016-03-03 15:58:01 +010067struct arm_dma_allocator {
68 void *(*alloc)(struct arm_dma_alloc_args *args,
69 struct page **ret_page);
70 void (*free)(struct arm_dma_free_args *args);
71};
72
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010073struct arm_dma_buffer {
74 struct list_head list;
75 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010076 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010077};
78
79static LIST_HEAD(arm_dma_bufs);
80static DEFINE_SPINLOCK(arm_dma_bufs_lock);
81
82static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
83{
84 struct arm_dma_buffer *buf, *found = NULL;
85 unsigned long flags;
86
87 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
88 list_for_each_entry(buf, &arm_dma_bufs, list) {
89 if (buf->virt == virt) {
90 list_del(&buf->list);
91 found = buf;
92 break;
93 }
94 }
95 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
96 return found;
97}
98
Marek Szyprowski15237e12012-02-10 19:55:20 +010099/*
100 * The DMA API is built upon the notion of "buffer ownership". A buffer
101 * is either exclusively owned by the CPU (and therefore may be accessed
102 * by it) or exclusively owned by the DMA device. These helper functions
103 * represent the transitions between these two ownership states.
104 *
105 * Note, however, that on later ARMs, this notion does not work due to
106 * speculative prefetches. We model our approach on the assumption that
107 * the CPU does do speculative prefetches, which means we clean caches
108 * before transfers and delay cache invalidation until transfer completion.
109 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100110 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100111static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100112 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100113static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100114 size_t, enum dma_data_direction);
115
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100116/**
117 * arm_dma_map_page - map a portion of a page for streaming DMA
118 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
119 * @page: page that buffer resides in
120 * @offset: offset into page for start of buffer
121 * @size: size of buffer to map
122 * @dir: DMA transfer direction
123 *
124 * Ensure that any data held in the cache is appropriately discarded
125 * or written back.
126 *
127 * The device owns this memory once this call has completed. The CPU
128 * can regain ownership by calling dma_unmap_page().
129 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100130static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100131 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700132 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100133{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700134 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100135 __dma_page_cpu_to_dev(page, offset, size, dir);
136 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100137}
138
Rob Herringdd37e942012-08-21 12:20:17 +0200139static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
140 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700141 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200142{
143 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
144}
145
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100146/**
147 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
148 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
149 * @handle: DMA address of buffer
150 * @size: size of buffer (same as passed to dma_map_page)
151 * @dir: DMA transfer direction (same as passed to dma_map_page)
152 *
153 * Unmap a page streaming mode DMA translation. The handle and size
154 * must match what was provided in the previous dma_map_page() call.
155 * All other usages are undefined.
156 *
157 * After this call, reads by the CPU to the buffer are guaranteed to see
158 * whatever the device wrote there.
159 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100160static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700161 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100162{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700163 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100164 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
165 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100166}
167
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100168static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100169 dma_addr_t handle, size_t size, enum dma_data_direction dir)
170{
171 unsigned int offset = handle & (PAGE_SIZE - 1);
172 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200173 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100174}
175
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100176static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100177 dma_addr_t handle, size_t size, enum dma_data_direction dir)
178{
179 unsigned int offset = handle & (PAGE_SIZE - 1);
180 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200181 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100182}
183
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100184struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200185 .alloc = arm_dma_alloc,
186 .free = arm_dma_free,
187 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200188 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100189 .map_page = arm_dma_map_page,
190 .unmap_page = arm_dma_unmap_page,
191 .map_sg = arm_dma_map_sg,
192 .unmap_sg = arm_dma_unmap_sg,
193 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
194 .sync_single_for_device = arm_dma_sync_single_for_device,
195 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
196 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100197};
198EXPORT_SYMBOL(arm_dma_ops);
199
Rob Herringdd37e942012-08-21 12:20:17 +0200200static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700201 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200202static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700203 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100204static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
205 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700206 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200207
208struct dma_map_ops arm_coherent_dma_ops = {
209 .alloc = arm_coherent_dma_alloc,
210 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100211 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200212 .get_sgtable = arm_dma_get_sgtable,
213 .map_page = arm_coherent_dma_map_page,
214 .map_sg = arm_dma_map_sg,
Rob Herringdd37e942012-08-21 12:20:17 +0200215};
216EXPORT_SYMBOL(arm_coherent_dma_ops);
217
Russell King9f28cde2013-12-06 12:30:42 +0000218static int __dma_supported(struct device *dev, u64 mask, bool warn)
219{
220 unsigned long max_dma_pfn;
221
222 /*
223 * If the mask allows for more memory than we can address,
224 * and we actually have that much memory, then we must
225 * indicate that DMA to this device is not supported.
226 */
227 if (sizeof(mask) != sizeof(dma_addr_t) &&
228 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000229 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000230 if (warn) {
231 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
232 mask);
233 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
234 }
235 return 0;
236 }
237
238 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
239
240 /*
241 * Translate the device's DMA mask to a PFN limit. This
242 * PFN number includes the page which we can DMA to.
243 */
244 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
245 if (warn)
246 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",
247 mask,
248 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
249 max_dma_pfn + 1);
250 return 0;
251 }
252
253 return 1;
254}
255
Catalin Marinasab6494f2009-07-24 12:35:02 +0100256static u64 get_coherent_dma_mask(struct device *dev)
257{
Russell King4dcfa602013-07-09 12:14:49 +0100258 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Catalin Marinasab6494f2009-07-24 12:35:02 +0100260 if (dev) {
261 mask = dev->coherent_dma_mask;
262
263 /*
264 * Sanity check the DMA mask - it must be non-zero, and
265 * must be able to be satisfied by a DMA allocation.
266 */
267 if (mask == 0) {
268 dev_warn(dev, "coherent DMA mask is unset\n");
269 return 0;
270 }
271
Russell King9f28cde2013-12-06 12:30:42 +0000272 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100273 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100274 }
275
276 return mask;
277}
278
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100279static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100280{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100281 /*
282 * Ensure that the allocated pages are zeroed, and that any data
283 * lurking in the kernel direct-mapped region is invalidated.
284 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100285 if (PageHighMem(page)) {
286 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
287 phys_addr_t end = base + size;
288 while (size > 0) {
289 void *ptr = kmap_atomic(page);
290 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100291 if (coherent_flag != COHERENT)
292 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100293 kunmap_atomic(ptr);
294 page++;
295 size -= PAGE_SIZE;
296 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100297 if (coherent_flag != COHERENT)
298 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100299 } else {
300 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200301 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100302 if (coherent_flag != COHERENT) {
303 dmac_flush_range(ptr, ptr + size);
304 outer_flush_range(__pa(ptr), __pa(ptr) + size);
305 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200306 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100307}
308
Russell King7a9a32a2009-11-19 15:31:07 +0000309/*
310 * Allocate a DMA buffer for 'dev' of size 'size' using the
311 * specified gfp mask. Note that 'size' must be page aligned.
312 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100313static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
314 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000315{
316 unsigned long order = get_order(size);
317 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000318
319 page = alloc_pages(gfp, order);
320 if (!page)
321 return NULL;
322
323 /*
324 * Now split the huge page and free the excess pages
325 */
326 split_page(page, order);
327 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
328 __free_page(p);
329
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100330 __dma_clear_buffer(page, size, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000331
332 return page;
333}
334
335/*
336 * Free a DMA buffer. 'size' must be page aligned.
337 */
338static void __dma_free_buffer(struct page *page, size_t size)
339{
340 struct page *e = page + (size >> PAGE_SHIFT);
341
342 while (page < e) {
343 __free_page(page);
344 page++;
345 }
346}
347
Catalin Marinasab6494f2009-07-24 12:35:02 +0100348#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Marek Szyprowskic7909502011-12-29 13:09:51 +0100350static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100351 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100352 const void *caller, bool want_vaddr,
353 int coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100354
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200355static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
356 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100357 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200358
359static void *
360__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
361 const void *caller)
362{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200363 /*
364 * DMA allocation can be mapped to user space, so lets
365 * set VM_USERMAP flags too.
366 */
Laura Abbott513510d2014-10-09 15:26:40 -0700367 return dma_common_contiguous_remap(page, size,
368 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
369 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200370}
371
372static void __dma_free_remap(void *cpu_addr, size_t size)
373{
Laura Abbott513510d2014-10-09 15:26:40 -0700374 dma_common_free_remap(cpu_addr, size,
Jeevan Shrirambc1b0002017-03-02 18:17:56 -0800375 VM_ARM_DMA_CONSISTENT | VM_USERMAP, false);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200376}
377
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200378#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700379static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200380
Laura Abbott36d0fd22014-10-09 15:26:42 -0700381static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100382
383static int __init early_coherent_pool(char *p)
384{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700385 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100386 return 0;
387}
388early_param("coherent_pool", early_coherent_pool);
389
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200390void __init init_dma_coherent_pool_size(unsigned long size)
391{
392 /*
393 * Catch any attempt to set the pool size too late.
394 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700395 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200396
397 /*
398 * Set architecture specific coherent pool size only if
399 * it has not been changed by kernel command line parameter.
400 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700401 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
402 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200403}
404
Marek Szyprowskic7909502011-12-29 13:09:51 +0100405/*
406 * Initialise the coherent pool for atomic allocations.
407 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200408static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100409{
Russell King71b55662013-11-25 12:01:03 +0000410 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100411 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100412 struct page *page;
413 void *ptr;
414
Laura Abbott36d0fd22014-10-09 15:26:42 -0700415 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
416 if (!atomic_pool)
417 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100418 /*
419 * The atomic pool is only used for non-coherent allocations
420 * so we must pass NORMAL for coherent_flag.
421 */
Gioh Kime464ef12014-05-22 13:38:23 +0900422 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700423 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100424 &page, atomic_pool_init, true, NORMAL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200425 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700426 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100427 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100428 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700429 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300430
Laura Abbott36d0fd22014-10-09 15:26:42 -0700431 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
432 page_to_phys(page),
433 atomic_pool_size, -1);
434 if (ret)
435 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300436
Laura Abbott36d0fd22014-10-09 15:26:42 -0700437 gen_pool_set_algo(atomic_pool,
438 gen_pool_first_fit_order_align,
439 (void *)PAGE_SHIFT);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100440 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700441 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100442 return 0;
443 }
Sachin Kamatec106652012-09-24 08:35:03 +0200444
Laura Abbott36d0fd22014-10-09 15:26:42 -0700445destroy_genpool:
446 gen_pool_destroy(atomic_pool);
447 atomic_pool = NULL;
448out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100449 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700450 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100451 return -ENOMEM;
452}
453/*
454 * CMA is activated by core_initcall, so we must be called after it.
455 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200456postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100457
458struct dma_contig_early_reserve {
459 phys_addr_t base;
460 unsigned long size;
461};
462
463static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
464
465static int dma_mmu_remap_num __initdata;
466
467void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
468{
469 dma_mmu_remap[dma_mmu_remap_num].base = base;
470 dma_mmu_remap[dma_mmu_remap_num].size = size;
471 dma_mmu_remap_num++;
472}
473
474void __init dma_contiguous_remap(void)
475{
476 int i;
477 for (i = 0; i < dma_mmu_remap_num; i++) {
478 phys_addr_t start = dma_mmu_remap[i].base;
479 phys_addr_t end = start + dma_mmu_remap[i].size;
480 struct map_desc map;
481 unsigned long addr;
482
483 if (end > arm_lowmem_limit)
484 end = arm_lowmem_limit;
485 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200486 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100487
488 map.pfn = __phys_to_pfn(start);
489 map.virtual = __phys_to_virt(start);
490 map.length = end - start;
491 map.type = MT_MEMORY_DMA_READY;
492
493 /*
Russell King6b076992014-07-17 12:17:45 +0100494 * Clear previous low-memory mapping to ensure that the
495 * TLB does not see any conflicting entries, then flush
496 * the TLB of the old entries before creating new mappings.
497 *
498 * This ensures that any speculatively loaded TLB entries
499 * (even though they may be rare) can not cause any problems,
500 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100501 */
502 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400503 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100504 pmd_clear(pmd_off_k(addr));
505
Russell King6b076992014-07-17 12:17:45 +0100506 flush_tlb_kernel_range(__phys_to_virt(start),
507 __phys_to_virt(end));
508
Marek Szyprowskic7909502011-12-29 13:09:51 +0100509 iotable_init(&map, 1);
510 }
511}
512
Marek Szyprowskic7909502011-12-29 13:09:51 +0100513static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
514 void *data)
515{
516 struct page *page = virt_to_page(addr);
517 pgprot_t prot = *(pgprot_t *)data;
518
519 set_pte_ext(pte, mk_pte(page, prot), 0);
520 return 0;
521}
522
523static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
524{
525 unsigned long start = (unsigned long) page_address(page);
526 unsigned end = start + size;
527
528 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100529 flush_tlb_kernel_range(start, end);
530}
531
532static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
533 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100534 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100535{
536 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100537 void *ptr = NULL;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100538 /*
539 * __alloc_remap_buffer is only called when the device is
540 * non-coherent
541 */
542 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100543 if (!page)
544 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100545 if (!want_vaddr)
546 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100547
548 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
549 if (!ptr) {
550 __dma_free_buffer(page, size);
551 return NULL;
552 }
553
Carlo Caione6e8266e2015-02-09 10:38:35 +0100554 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100555 *ret_page = page;
556 return ptr;
557}
558
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200559static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100560{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700561 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200562 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100563
Laura Abbott36d0fd22014-10-09 15:26:42 -0700564 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200565 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100566 return NULL;
567 }
568
Laura Abbott36d0fd22014-10-09 15:26:42 -0700569 val = gen_pool_alloc(atomic_pool, size);
570 if (val) {
571 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200572
Laura Abbott36d0fd22014-10-09 15:26:42 -0700573 *ret_page = phys_to_page(phys);
574 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100575 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200576
577 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100578}
579
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300580static bool __in_atomic_pool(void *start, size_t size)
581{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700582 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300583}
584
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200585static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100586{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300587 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100588 return 0;
589
Laura Abbott36d0fd22014-10-09 15:26:42 -0700590 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200591
Marek Szyprowskic7909502011-12-29 13:09:51 +0100592 return 1;
593}
594
595static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100596 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100597 const void *caller, bool want_vaddr,
598 int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100599{
600 unsigned long order = get_order(size);
601 size_t count = size >> PAGE_SHIFT;
602 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100603 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100604
605 page = dma_alloc_from_contiguous(dev, count, order);
606 if (!page)
607 return NULL;
608
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100609 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100610
Carlo Caione6e8266e2015-02-09 10:38:35 +0100611 if (!want_vaddr)
612 goto out;
613
Marek Szyprowski9848e482013-01-16 15:38:44 +0100614 if (PageHighMem(page)) {
615 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
616 if (!ptr) {
617 dma_release_from_contiguous(dev, page, count);
618 return NULL;
619 }
620 } else {
621 __dma_remap(page, size, prot);
622 ptr = page_address(page);
623 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100624
625 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100626 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100627 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100628}
629
630static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100631 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100632{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100633 if (want_vaddr) {
634 if (PageHighMem(page))
635 __dma_free_remap(cpu_addr, size);
636 else
637 __dma_remap(page, size, PAGE_KERNEL);
638 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100639 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
640}
641
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700642static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200643{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700644 prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
645 pgprot_writecombine(prot) :
646 pgprot_dmacoherent(prot);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200647 return prot;
648}
649
Marek Szyprowskic7909502011-12-29 13:09:51 +0100650#define nommu() 0
651
Catalin Marinasab6494f2009-07-24 12:35:02 +0100652#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000653
Marek Szyprowskic7909502011-12-29 13:09:51 +0100654#define nommu() 1
655
Carlo Caione6e8266e2015-02-09 10:38:35 +0100656#define __get_dma_pgprot(attrs, prot) __pgprot(0)
657#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200658#define __alloc_from_pool(size, ret_page) NULL
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100659#define __alloc_from_contiguous(dev, size, prot, ret, c, wv, coherent_flag) NULL
Rabin Vincentb4268672016-03-03 15:58:01 +0100660#define __free_from_pool(cpu_addr, size) do { } while (0)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100661#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100662#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000663
664#endif /* CONFIG_MMU */
665
Marek Szyprowskic7909502011-12-29 13:09:51 +0100666static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
667 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100668{
Russell King04da5692009-11-19 15:54:45 +0000669 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100670 /* __alloc_simple_buffer is only called when the device is coherent */
671 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100672 if (!page)
673 return NULL;
674
675 *ret_page = page;
676 return page_address(page);
677}
678
Rabin Vincentb4268672016-03-03 15:58:01 +0100679static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
680 struct page **ret_page)
681{
682 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
683 ret_page);
684}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100685
Rabin Vincentb4268672016-03-03 15:58:01 +0100686static void simple_allocator_free(struct arm_dma_free_args *args)
687{
688 __dma_free_buffer(args->page, args->size);
689}
690
691static struct arm_dma_allocator simple_allocator = {
692 .alloc = simple_allocator_alloc,
693 .free = simple_allocator_free,
694};
695
696static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
697 struct page **ret_page)
698{
699 return __alloc_from_contiguous(args->dev, args->size, args->prot,
700 ret_page, args->caller,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100701 args->want_vaddr, args->coherent_flag);
Rabin Vincentb4268672016-03-03 15:58:01 +0100702}
703
704static void cma_allocator_free(struct arm_dma_free_args *args)
705{
706 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
707 args->size, args->want_vaddr);
708}
709
710static struct arm_dma_allocator cma_allocator = {
711 .alloc = cma_allocator_alloc,
712 .free = cma_allocator_free,
713};
714
715static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
716 struct page **ret_page)
717{
718 return __alloc_from_pool(args->size, ret_page);
719}
720
721static void pool_allocator_free(struct arm_dma_free_args *args)
722{
723 __free_from_pool(args->cpu_addr, args->size);
724}
725
726static struct arm_dma_allocator pool_allocator = {
727 .alloc = pool_allocator_alloc,
728 .free = pool_allocator_free,
729};
730
731static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
732 struct page **ret_page)
733{
734 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
735 args->prot, ret_page, args->caller,
736 args->want_vaddr);
737}
738
739static void remap_allocator_free(struct arm_dma_free_args *args)
740{
741 if (args->want_vaddr)
742 __dma_free_remap(args->cpu_addr, args->size);
743
744 __dma_free_buffer(args->page, args->size);
745}
746
747static struct arm_dma_allocator remap_allocator = {
748 .alloc = remap_allocator_alloc,
749 .free = remap_allocator_free,
750};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100751
752static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100753 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700754 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100755{
756 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900757 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000758 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100759 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100760 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100761 struct arm_dma_alloc_args args = {
762 .dev = dev,
763 .size = PAGE_ALIGN(size),
764 .gfp = gfp,
765 .prot = prot,
766 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700767 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100768 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100769 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100770
Marek Szyprowskic7909502011-12-29 13:09:51 +0100771#ifdef CONFIG_DMA_API_DEBUG
772 u64 limit = (mask + 1) & ~mask;
773 if (limit && size >= limit) {
774 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
775 size, mask);
776 return NULL;
777 }
778#endif
779
780 if (!mask)
781 return NULL;
782
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100783 buf = kzalloc(sizeof(*buf),
784 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100785 if (!buf)
786 return NULL;
787
Marek Szyprowskic7909502011-12-29 13:09:51 +0100788 if (mask < 0xffffffffULL)
789 gfp |= GFP_DMA;
790
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100791 /*
792 * Following is a work-around (a.k.a. hack) to prevent pages
793 * with __GFP_COMP being passed to split_page() which cannot
794 * handle them. The real problem is that this flag probably
795 * should be 0 on ARM as it is not supported on this
796 * platform; see CONFIG_HUGETLBFS.
797 */
798 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100799 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100800
Marek Szyprowski553ac782012-02-29 14:45:28 +0100801 *handle = DMA_ERROR_CODE;
Rabin Vincentb4268672016-03-03 15:58:01 +0100802 allowblock = gfpflags_allow_blocking(gfp);
803 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000804
Rabin Vincentb4268672016-03-03 15:58:01 +0100805 if (cma)
806 buf->allocator = &cma_allocator;
807 else if (nommu() || is_coherent)
808 buf->allocator = &simple_allocator;
809 else if (allowblock)
810 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000811 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100812 buf->allocator = &pool_allocator;
813
814 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000815
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100816 if (page) {
817 unsigned long flags;
818
Russell King9eedd962011-01-03 00:00:17 +0000819 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100820 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100821
822 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
823 list_add(&buf->list, &arm_dma_bufs);
824 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
825 } else {
826 kfree(buf);
827 }
Russell King31ebf942009-11-19 21:12:17 +0000828
Rabin Vincentb4268672016-03-03 15:58:01 +0100829 return args.want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100830}
Russell King695ae0a2009-11-19 16:31:39 +0000831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832/*
833 * Allocate DMA-coherent memory space and return both the kernel remapped
834 * virtual and bus address for that space.
835 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200836void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700837 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Russell King0ea1ec72013-10-23 16:14:59 +0100839 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400840
Rob Herringdd37e942012-08-21 12:20:17 +0200841 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100842 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200843}
844
845static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700846 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200847{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100848 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100849 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Mike Looijmans55af8a92015-06-03 11:25:31 +0100852static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200853 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700854 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100856 int ret = -ENXIO;
857#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200858 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
859 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100860 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200861 unsigned long off = vma->vm_pgoff;
862
Marek Szyprowski47142f02012-05-15 19:04:13 +0200863 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
864 return ret;
865
Marek Szyprowski50262a42012-07-30 09:35:26 +0200866 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
867 ret = remap_pfn_range(vma, vma->vm_start,
868 pfn + off,
869 vma->vm_end - vma->vm_start,
870 vma->vm_page_prot);
871 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100872#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 return ret;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100878 * Create userspace mapping for the DMA-coherent memory.
879 */
880static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
881 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700882 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100883{
884 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
885}
886
887int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
888 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700889 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100890{
891#ifdef CONFIG_MMU
892 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
893#endif /* CONFIG_MMU */
894 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
895}
896
897/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100898 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Rob Herringdd37e942012-08-21 12:20:17 +0200900static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700901 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +0200902 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100904 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100905 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100906 struct arm_dma_free_args args = {
907 .dev = dev,
908 .size = PAGE_ALIGN(size),
909 .cpu_addr = cpu_addr,
910 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700911 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +0100912 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100913
914 buf = arm_dma_buffer_find(cpu_addr);
915 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
916 return;
Russell King5edf71a2005-11-25 15:52:51 +0000917
Rabin Vincentb4268672016-03-03 15:58:01 +0100918 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100919 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
Russell Kingafd1a322008-09-25 16:30:57 +0100921
Rob Herringdd37e942012-08-21 12:20:17 +0200922void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700923 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200924{
925 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
926}
927
928static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700929 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200930{
931 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
932}
933
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200934int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
935 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700936 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200937{
938 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
939 int ret;
940
941 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
942 if (unlikely(ret))
943 return ret;
944
945 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
946 return 0;
947}
948
Russell King65af1912009-11-24 17:53:33 +0000949static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000950 size_t size, enum dma_data_direction dir,
951 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000952{
Russell King15653372013-01-19 11:05:57 +0000953 unsigned long pfn;
954 size_t left = size;
955
956 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
957 offset %= PAGE_SIZE;
958
Russell King65af1912009-11-24 17:53:33 +0000959 /*
960 * A single sg entry may refer to multiple physically contiguous
961 * pages. But we still need to process highmem pages individually.
962 * If highmem is not configured then the bulk of this loop gets
963 * optimized out.
964 */
Russell King65af1912009-11-24 17:53:33 +0000965 do {
966 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000967 void *vaddr;
968
Russell King15653372013-01-19 11:05:57 +0000969 page = pfn_to_page(pfn);
970
Russell King93f1d622009-11-24 14:41:01 +0000971 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000972 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000973 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100974
975 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500976 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100977 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500978 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100979 } else {
980 vaddr = kmap_high_get(page);
981 if (vaddr) {
982 op(vaddr + offset, len, dir);
983 kunmap_high(page);
984 }
Russell King93f1d622009-11-24 14:41:01 +0000985 }
986 } else {
987 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000988 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000989 }
Russell King65af1912009-11-24 17:53:33 +0000990 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000991 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000992 left -= len;
993 } while (left);
994}
995
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100996/*
997 * Make an area consistent for devices.
998 * Note: Drivers should NOT use this function directly, as it will break
999 * platforms with CONFIG_DMABOUNCE.
1000 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
1001 */
1002static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +00001003 size_t size, enum dma_data_direction dir)
1004{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001005 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -04001006
Russell Kinga9c91472009-11-26 16:19:58 +00001007 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -04001008
Russell King65af1912009-11-24 17:53:33 +00001009 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001010 if (dir == DMA_FROM_DEVICE) {
1011 outer_inv_range(paddr, paddr + size);
1012 } else {
1013 outer_clean_range(paddr, paddr + size);
1014 }
1015 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -04001016}
Russell King4ea0d732009-11-24 16:27:17 +00001017
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001018static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001019 size_t size, enum dma_data_direction dir)
1020{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001021 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001022
1023 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001024 /* in any case, don't bother invalidating if DMA to device */
1025 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001026 outer_inv_range(paddr, paddr + size);
1027
Russell Kingdeace4a2014-05-03 11:06:55 +01001028 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1029 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001030
1031 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001032 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001033 */
Ming Leib2a234e2013-05-18 11:21:36 +01001034 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1035 unsigned long pfn;
1036 size_t left = size;
1037
1038 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1039 off %= PAGE_SIZE;
1040 if (off) {
1041 pfn++;
1042 left -= PAGE_SIZE - off;
1043 }
1044 while (left >= PAGE_SIZE) {
1045 page = pfn_to_page(pfn++);
1046 set_bit(PG_dcache_clean, &page->flags);
1047 left -= PAGE_SIZE;
1048 }
1049 }
Russell King4ea0d732009-11-24 16:27:17 +00001050}
Nicolas Pitre43377452009-03-12 22:52:09 -04001051
Russell Kingafd1a322008-09-25 16:30:57 +01001052/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001053 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001054 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1055 * @sg: list of buffers
1056 * @nents: number of buffers to map
1057 * @dir: DMA transfer direction
1058 *
1059 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1060 * This is the scatter-gather version of the dma_map_single interface.
1061 * Here the scatter gather list elements are each tagged with the
1062 * appropriate dma address and length. They are obtained via
1063 * sg_dma_{address,length}.
1064 *
1065 * Device ownership issues as mentioned for dma_map_single are the same
1066 * here.
1067 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001068int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001069 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001070{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001071 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001072 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001073 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001074
1075 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001076#ifdef CONFIG_NEED_SG_DMA_LENGTH
1077 s->dma_length = s->length;
1078#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001079 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1080 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001081 if (dma_mapping_error(dev, s->dma_address))
1082 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001083 }
Russell Kingafd1a322008-09-25 16:30:57 +01001084 return nents;
Russell King01135d922008-09-25 21:05:02 +01001085
1086 bad_mapping:
1087 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001088 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001089 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001090}
Russell Kingafd1a322008-09-25 16:30:57 +01001091
1092/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001093 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001094 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1095 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001096 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001097 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1098 *
1099 * Unmap a set of streaming mode DMA translations. Again, CPU access
1100 * rules concerning calls here are the same as for dma_unmap_single().
1101 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001102void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001103 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001104{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001105 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001106 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001107
Russell King01135d922008-09-25 21:05:02 +01001108 int i;
Russell King24056f52011-01-03 11:29:28 +00001109
Russell King01135d922008-09-25 21:05:02 +01001110 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001111 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001112}
Russell Kingafd1a322008-09-25 16:30:57 +01001113
1114/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001115 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001116 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1117 * @sg: list of buffers
1118 * @nents: number of buffers to map (returned from dma_map_sg)
1119 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1120 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001121void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001122 int nents, enum dma_data_direction dir)
1123{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001124 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001125 struct scatterlist *s;
1126 int i;
1127
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001128 for_each_sg(sg, s, nents, i)
1129 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1130 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001131}
Russell Kingafd1a322008-09-25 16:30:57 +01001132
1133/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001134 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001135 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1136 * @sg: list of buffers
1137 * @nents: number of buffers to map (returned from dma_map_sg)
1138 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1139 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001140void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001141 int nents, enum dma_data_direction dir)
1142{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001143 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001144 struct scatterlist *s;
1145 int i;
1146
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001147 for_each_sg(sg, s, nents, i)
1148 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1149 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001150}
Russell King24056f52011-01-03 11:29:28 +00001151
Russell King022ae532011-07-08 21:26:59 +01001152/*
1153 * Return whether the given device DMA address mask can be supported
1154 * properly. For example, if your device can only drive the low 24-bits
1155 * during bus mastering, then you would pass 0x00ffffff as the mask
1156 * to this function.
1157 */
1158int dma_supported(struct device *dev, u64 mask)
1159{
Russell King9f28cde2013-12-06 12:30:42 +00001160 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001161}
1162EXPORT_SYMBOL(dma_supported);
1163
Russell King24056f52011-01-03 11:29:28 +00001164#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1165
1166static int __init dma_debug_do_init(void)
1167{
1168 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1169 return 0;
1170}
Marek Szyprowski256ff1c2016-11-15 14:00:53 +01001171core_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001172
1173#ifdef CONFIG_ARM_DMA_USE_IOMMU
1174
1175/* IOMMU */
1176
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001177static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1178
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001179static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1180 size_t size)
1181{
1182 unsigned int order = get_order(size);
1183 unsigned int align = 0;
1184 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301185 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001186 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001187 dma_addr_t iova;
1188 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001189
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001190 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1191 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1192
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001193 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1194 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001195
1196 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001197 for (i = 0; i < mapping->nr_bitmaps; i++) {
1198 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1199 mapping->bits, 0, count, align);
1200
1201 if (start > mapping->bits)
1202 continue;
1203
1204 bitmap_set(mapping->bitmaps[i], start, count);
1205 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001206 }
1207
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001208 /*
1209 * No unused range found. Try to extend the existing mapping
1210 * and perform a second attempt to reserve an IO virtual
1211 * address range of size bytes.
1212 */
1213 if (i == mapping->nr_bitmaps) {
1214 if (extend_iommu_mapping(mapping)) {
1215 spin_unlock_irqrestore(&mapping->lock, flags);
1216 return DMA_ERROR_CODE;
1217 }
1218
1219 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1220 mapping->bits, 0, count, align);
1221
1222 if (start > mapping->bits) {
1223 spin_unlock_irqrestore(&mapping->lock, flags);
1224 return DMA_ERROR_CODE;
1225 }
1226
1227 bitmap_set(mapping->bitmaps[i], start, count);
1228 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001229 spin_unlock_irqrestore(&mapping->lock, flags);
1230
Ritesh Harjani006f8412014-05-20 10:02:59 +05301231 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001232 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001233
1234 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001235}
1236
1237static inline void __free_iova(struct dma_iommu_mapping *mapping,
1238 dma_addr_t addr, size_t size)
1239{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001240 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301241 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001242 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001243 dma_addr_t bitmap_base;
1244 u32 bitmap_index;
1245
1246 if (!size)
1247 return;
1248
Ritesh Harjani006f8412014-05-20 10:02:59 +05301249 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001250 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1251
Ritesh Harjani006f8412014-05-20 10:02:59 +05301252 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001253
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001254 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001255
Ritesh Harjani006f8412014-05-20 10:02:59 +05301256 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001257 /*
1258 * The address range to be freed reaches into the iova
1259 * range of the next bitmap. This should not happen as
1260 * we don't allow this in __alloc_iova (at the
1261 * moment).
1262 */
1263 BUG();
1264 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001265 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001266
1267 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001268 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001269 spin_unlock_irqrestore(&mapping->lock, flags);
1270}
1271
Doug Anderson33298ef2016-01-29 23:06:08 +01001272/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1273static const int iommu_order_array[] = { 9, 8, 4, 0 };
1274
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001275static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001276 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001277 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001278{
1279 struct page **pages;
Shiraz Hashim3340eeb2016-02-21 12:45:34 +05301280 size_t count = size >> PAGE_SHIFT;
1281 size_t array_size = count * sizeof(struct page *);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001282 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001283 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001284
1285 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001286 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001287 else
1288 pages = vzalloc(array_size);
1289 if (!pages)
1290 return NULL;
1291
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001292 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001293 {
1294 unsigned long order = get_order(size);
1295 struct page *page;
1296
1297 page = dma_alloc_from_contiguous(dev, count, order);
1298 if (!page)
1299 goto error;
1300
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001301 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001302
1303 for (i = 0; i < count; i++)
1304 pages[i] = page + i;
1305
1306 return pages;
1307 }
1308
Doug Anderson14d3ae22016-01-29 23:08:46 +01001309 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001310 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001311 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1312
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001313 /*
1314 * IOMMU can map any pages, so himem can also be used here
1315 */
1316 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1317
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001318 while (count) {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001319 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001320
Doug Anderson33298ef2016-01-29 23:06:08 +01001321 order = iommu_order_array[order_idx];
1322
1323 /* Drop down when we get small */
1324 if (__fls(count) < order) {
1325 order_idx++;
1326 continue;
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001327 }
1328
Doug Anderson33298ef2016-01-29 23:06:08 +01001329 if (order) {
1330 /* See if it's easy to allocate a high-order chunk */
1331 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1332
1333 /* Go down a notch at first sign of pressure */
1334 if (!pages[i]) {
1335 order_idx++;
1336 continue;
1337 }
1338 } else {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001339 pages[i] = alloc_pages(gfp, 0);
1340 if (!pages[i])
1341 goto error;
1342 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001343
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001344 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001345 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001346 j = 1 << order;
1347 while (--j)
1348 pages[i + j] = pages[i] + j;
1349 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001350
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001351 __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001352 i += 1 << order;
1353 count -= 1 << order;
1354 }
1355
1356 return pages;
1357error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001358 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001359 if (pages[i])
1360 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001361 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001362 return NULL;
1363}
1364
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001365static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001366 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001367{
1368 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001369 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001370
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001371 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001372 dma_release_from_contiguous(dev, pages[0], count);
1373 } else {
1374 for (i = 0; i < count; i++)
1375 if (pages[i])
1376 __free_pages(pages[i], 0);
1377 }
1378
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001379 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001380 return 0;
1381}
1382
1383/*
1384 * Create a CPU mapping for a specified pages
1385 */
1386static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001387__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1388 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001389{
Laura Abbott513510d2014-10-09 15:26:40 -07001390 return dma_common_pages_remap(pages, size,
1391 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001392}
1393
1394/*
1395 * Create a mapping in device IO address space for specified pages
1396 */
1397static dma_addr_t
1398__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1399{
Will Deacon89cfdb12015-01-16 18:02:15 +01001400 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001401 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1402 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001403 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001404
1405 dma_addr = __alloc_iova(mapping, size);
1406 if (dma_addr == DMA_ERROR_CODE)
1407 return dma_addr;
1408
1409 iova = dma_addr;
1410 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001411 int ret;
1412
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001413 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1414 phys_addr_t phys = page_to_phys(pages[i]);
1415 unsigned int len, j;
1416
1417 for (j = i + 1; j < count; j++, next_pfn++)
1418 if (page_to_pfn(pages[j]) != next_pfn)
1419 break;
1420
1421 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001422 ret = iommu_map(mapping->domain, iova, phys, len,
1423 IOMMU_READ|IOMMU_WRITE);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001424 if (ret < 0)
1425 goto fail;
1426 iova += len;
1427 i = j;
1428 }
1429 return dma_addr;
1430fail:
1431 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1432 __free_iova(mapping, dma_addr, size);
1433 return DMA_ERROR_CODE;
1434}
1435
1436static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1437{
Will Deacon89cfdb12015-01-16 18:02:15 +01001438 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001439
1440 /*
1441 * add optional in-page offset from iova to size and align
1442 * result to page size
1443 */
1444 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1445 iova &= PAGE_MASK;
1446
1447 iommu_unmap(mapping->domain, iova, size);
1448 __free_iova(mapping, iova, size);
1449 return 0;
1450}
1451
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001452static struct page **__atomic_get_pages(void *addr)
1453{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001454 struct page *page;
1455 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001456
Laura Abbott36d0fd22014-10-09 15:26:42 -07001457 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1458 page = phys_to_page(phys);
1459
1460 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001461}
1462
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001463static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001464{
1465 struct vm_struct *area;
1466
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001467 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1468 return __atomic_get_pages(cpu_addr);
1469
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001470 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001471 return cpu_addr;
1472
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001473 area = find_vm_area(cpu_addr);
1474 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1475 return area->pages;
1476 return NULL;
1477}
1478
Gregory CLEMENT56506822016-04-15 11:20:13 +01001479static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
1480 dma_addr_t *handle, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001481{
1482 struct page *page;
1483 void *addr;
1484
Gregory CLEMENT56506822016-04-15 11:20:13 +01001485 if (coherent_flag == COHERENT)
1486 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1487 else
1488 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001489 if (!addr)
1490 return NULL;
1491
1492 *handle = __iommu_create_mapping(dev, &page, size);
1493 if (*handle == DMA_ERROR_CODE)
1494 goto err_mapping;
1495
1496 return addr;
1497
1498err_mapping:
1499 __free_from_pool(addr, size);
1500 return NULL;
1501}
1502
Marek Szyprowskid5898292013-02-08 10:54:48 +01001503static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001504 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001505{
1506 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001507 if (coherent_flag == COHERENT)
1508 __dma_free_buffer(virt_to_page(cpu_addr), size);
1509 else
1510 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001511}
1512
Gregory CLEMENT56506822016-04-15 11:20:13 +01001513static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001514 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001515 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001516{
Russell King71b55662013-11-25 12:01:03 +00001517 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001518 struct page **pages;
1519 void *addr = NULL;
1520
1521 *handle = DMA_ERROR_CODE;
1522 size = PAGE_ALIGN(size);
1523
Gregory CLEMENT56506822016-04-15 11:20:13 +01001524 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1525 return __iommu_alloc_simple(dev, size, gfp, handle,
1526 coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001527
Richard Zhao5b91a982013-06-20 20:31:00 +08001528 /*
1529 * Following is a work-around (a.k.a. hack) to prevent pages
1530 * with __GFP_COMP being passed to split_page() which cannot
1531 * handle them. The real problem is that this flag probably
1532 * should be 0 on ARM as it is not supported on this
1533 * platform; see CONFIG_HUGETLBFS.
1534 */
1535 gfp &= ~(__GFP_COMP);
1536
Gregory CLEMENT56506822016-04-15 11:20:13 +01001537 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001538 if (!pages)
1539 return NULL;
1540
1541 *handle = __iommu_create_mapping(dev, pages, size);
1542 if (*handle == DMA_ERROR_CODE)
1543 goto err_buffer;
1544
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001545 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001546 return pages;
1547
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001548 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1549 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001550 if (!addr)
1551 goto err_mapping;
1552
1553 return addr;
1554
1555err_mapping:
1556 __iommu_remove_mapping(dev, *handle, size);
1557err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001558 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001559 return NULL;
1560}
1561
Gregory CLEMENT56506822016-04-15 11:20:13 +01001562static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001563 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001564{
1565 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1566}
1567
1568static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001569 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001570{
1571 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1572}
1573
1574static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001575 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001576 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001577{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001578 unsigned long uaddr = vma->vm_start;
1579 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001580 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001581 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1582 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001583
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001584 if (!pages)
1585 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001586
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001587 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1588 return -ENXIO;
1589
Marek Szyprowski7e312102015-08-28 09:42:09 +01001590 pages += off;
1591
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001592 do {
1593 int ret = vm_insert_page(vma, uaddr, *pages++);
1594 if (ret) {
1595 pr_err("Remapping memory failed: %d\n", ret);
1596 return ret;
1597 }
1598 uaddr += PAGE_SIZE;
1599 usize -= PAGE_SIZE;
1600 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001601
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001602 return 0;
1603}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001604static int arm_iommu_mmap_attrs(struct device *dev,
1605 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001606 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001607{
1608 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1609
1610 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1611}
1612
1613static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1614 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001615 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001616{
1617 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1618}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001619
1620/*
1621 * free a page as defined by the above mapping.
1622 * Must not be called with IRQs disabled.
1623 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001624void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001625 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001626{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001627 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001628 size = PAGE_ALIGN(size);
1629
Gregory CLEMENT56506822016-04-15 11:20:13 +01001630 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1631 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001632 return;
1633 }
1634
YoungJun Cho836bfa02013-06-17 13:18:52 +09001635 pages = __iommu_get_pages(cpu_addr, attrs);
1636 if (!pages) {
1637 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1638 return;
1639 }
1640
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001641 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001642 dma_common_free_remap(cpu_addr, size,
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001643 VM_ARM_DMA_CONSISTENT | VM_USERMAP, false);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001644 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001645
1646 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001647 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001648}
1649
Gregory CLEMENT56506822016-04-15 11:20:13 +01001650void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001651 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001652{
1653 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1654}
1655
1656void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001657 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001658{
1659 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1660}
1661
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001662static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1663 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001664 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001665{
1666 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1667 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1668
1669 if (!pages)
1670 return -ENXIO;
1671
1672 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1673 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001674}
1675
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001676static int __dma_direction_to_prot(enum dma_data_direction dir)
1677{
1678 int prot;
1679
1680 switch (dir) {
1681 case DMA_BIDIRECTIONAL:
1682 prot = IOMMU_READ | IOMMU_WRITE;
1683 break;
1684 case DMA_TO_DEVICE:
1685 prot = IOMMU_READ;
1686 break;
1687 case DMA_FROM_DEVICE:
1688 prot = IOMMU_WRITE;
1689 break;
1690 default:
1691 prot = 0;
1692 }
1693
1694 return prot;
1695}
1696
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001697/*
1698 * Map a part of the scatter-gather list into contiguous io address space
1699 */
1700static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1701 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001702 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001703 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001704{
Will Deacon89cfdb12015-01-16 18:02:15 +01001705 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001706 dma_addr_t iova, iova_base;
1707 int ret = 0;
1708 unsigned int count;
1709 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001710 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001711
1712 size = PAGE_ALIGN(size);
1713 *handle = DMA_ERROR_CODE;
1714
1715 iova_base = iova = __alloc_iova(mapping, size);
1716 if (iova == DMA_ERROR_CODE)
1717 return -ENOMEM;
1718
1719 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001720 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001721 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1722
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001723 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001724 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1725
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001726 prot = __dma_direction_to_prot(dir);
1727
1728 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001729 if (ret < 0)
1730 goto fail;
1731 count += len >> PAGE_SHIFT;
1732 iova += len;
1733 }
1734 *handle = iova_base;
1735
1736 return 0;
1737fail:
1738 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1739 __free_iova(mapping, iova_base, size);
1740 return ret;
1741}
1742
Rob Herring0fa478d2012-08-21 12:23:23 +02001743static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001744 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001745 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001746{
1747 struct scatterlist *s = sg, *dma = sg, *start = sg;
1748 int i, count = 0;
1749 unsigned int offset = s->offset;
1750 unsigned int size = s->offset + s->length;
1751 unsigned int max = dma_get_max_seg_size(dev);
1752
1753 for (i = 1; i < nents; i++) {
1754 s = sg_next(s);
1755
1756 s->dma_address = DMA_ERROR_CODE;
1757 s->dma_length = 0;
1758
1759 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1760 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001761 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001762 goto bad_mapping;
1763
1764 dma->dma_address += offset;
1765 dma->dma_length = size - offset;
1766
1767 size = offset = s->offset;
1768 start = s;
1769 dma = sg_next(dma);
1770 count += 1;
1771 }
1772 size += s->length;
1773 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001774 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1775 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001776 goto bad_mapping;
1777
1778 dma->dma_address += offset;
1779 dma->dma_length = size - offset;
1780
1781 return count+1;
1782
1783bad_mapping:
1784 for_each_sg(sg, s, count, i)
1785 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1786 return 0;
1787}
1788
1789/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001790 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1791 * @dev: valid struct device pointer
1792 * @sg: list of buffers
1793 * @nents: number of buffers to map
1794 * @dir: DMA transfer direction
1795 *
1796 * Map a set of i/o coherent buffers described by scatterlist in streaming
1797 * mode for DMA. The scatter gather list elements are merged together (if
1798 * possible) and tagged with the appropriate dma address and length. They are
1799 * obtained via sg_dma_{address,length}.
1800 */
1801int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001802 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001803{
1804 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1805}
1806
1807/**
1808 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1809 * @dev: valid struct device pointer
1810 * @sg: list of buffers
1811 * @nents: number of buffers to map
1812 * @dir: DMA transfer direction
1813 *
1814 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1815 * The scatter gather list elements are merged together (if possible) and
1816 * tagged with the appropriate dma address and length. They are obtained via
1817 * sg_dma_{address,length}.
1818 */
1819int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001820 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001821{
1822 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1823}
1824
1825static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001826 int nents, enum dma_data_direction dir,
1827 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001828{
1829 struct scatterlist *s;
1830 int i;
1831
1832 for_each_sg(sg, s, nents, i) {
1833 if (sg_dma_len(s))
1834 __iommu_remove_mapping(dev, sg_dma_address(s),
1835 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001836 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001837 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1838 s->length, dir);
1839 }
1840}
1841
1842/**
1843 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1844 * @dev: valid struct device pointer
1845 * @sg: list of buffers
1846 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1847 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1848 *
1849 * Unmap a set of streaming mode DMA translations. Again, CPU access
1850 * rules concerning calls here are the same as for dma_unmap_single().
1851 */
1852void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001853 int nents, enum dma_data_direction dir,
1854 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001855{
1856 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1857}
1858
1859/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001860 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1861 * @dev: valid struct device pointer
1862 * @sg: list of buffers
1863 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1864 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1865 *
1866 * Unmap a set of streaming mode DMA translations. Again, CPU access
1867 * rules concerning calls here are the same as for dma_unmap_single().
1868 */
1869void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001870 enum dma_data_direction dir,
1871 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001872{
Rob Herring0fa478d2012-08-21 12:23:23 +02001873 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001874}
1875
1876/**
1877 * arm_iommu_sync_sg_for_cpu
1878 * @dev: valid struct device pointer
1879 * @sg: list of buffers
1880 * @nents: number of buffers to map (returned from dma_map_sg)
1881 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1882 */
1883void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1884 int nents, enum dma_data_direction dir)
1885{
1886 struct scatterlist *s;
1887 int i;
1888
1889 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001890 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001891
1892}
1893
1894/**
1895 * arm_iommu_sync_sg_for_device
1896 * @dev: valid struct device pointer
1897 * @sg: list of buffers
1898 * @nents: number of buffers to map (returned from dma_map_sg)
1899 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1900 */
1901void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1902 int nents, enum dma_data_direction dir)
1903{
1904 struct scatterlist *s;
1905 int i;
1906
1907 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001908 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001909}
1910
1911
1912/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001913 * arm_coherent_iommu_map_page
1914 * @dev: valid struct device pointer
1915 * @page: page that buffer resides in
1916 * @offset: offset into page for start of buffer
1917 * @size: size of buffer to map
1918 * @dir: DMA transfer direction
1919 *
1920 * Coherent IOMMU aware version of arm_dma_map_page()
1921 */
1922static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1923 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001924 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001925{
Will Deacon89cfdb12015-01-16 18:02:15 +01001926 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001927 dma_addr_t dma_addr;
Liam Markaeff2c12017-01-26 16:46:14 -08001928 int ret, prot, len, start_offset, map_offset;
1929
1930 map_offset = offset & ~PAGE_MASK;
1931 start_offset = offset & PAGE_MASK;
1932 len = PAGE_ALIGN(map_offset + size);
Rob Herring0fa478d2012-08-21 12:23:23 +02001933
1934 dma_addr = __alloc_iova(mapping, len);
1935 if (dma_addr == DMA_ERROR_CODE)
1936 return dma_addr;
1937
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001938 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01001939
Liam Markaeff2c12017-01-26 16:46:14 -08001940 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page) +
1941 start_offset, len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001942 if (ret < 0)
1943 goto fail;
1944
Liam Markaeff2c12017-01-26 16:46:14 -08001945 return dma_addr + map_offset;
Rob Herring0fa478d2012-08-21 12:23:23 +02001946fail:
1947 __free_iova(mapping, dma_addr, len);
1948 return DMA_ERROR_CODE;
1949}
1950
1951/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001952 * arm_iommu_map_page
1953 * @dev: valid struct device pointer
1954 * @page: page that buffer resides in
1955 * @offset: offset into page for start of buffer
1956 * @size: size of buffer to map
1957 * @dir: DMA transfer direction
1958 *
1959 * IOMMU aware version of arm_dma_map_page()
1960 */
1961static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1962 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001963 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001964{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001965 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001966 __dma_page_cpu_to_dev(page, offset, size, dir);
1967
Rob Herring0fa478d2012-08-21 12:23:23 +02001968 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1969}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001970
Rob Herring0fa478d2012-08-21 12:23:23 +02001971/**
1972 * arm_coherent_iommu_unmap_page
1973 * @dev: valid struct device pointer
1974 * @handle: DMA address of buffer
1975 * @size: size of buffer (same as passed to dma_map_page)
1976 * @dir: DMA transfer direction (same as passed to dma_map_page)
1977 *
1978 * Coherent IOMMU aware version of arm_dma_unmap_page()
1979 */
1980static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001981 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001982{
Will Deacon89cfdb12015-01-16 18:02:15 +01001983 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001984 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001985 int offset = handle & ~PAGE_MASK;
1986 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001987
Rob Herring0fa478d2012-08-21 12:23:23 +02001988 if (!iova)
1989 return;
1990
1991 iommu_unmap(mapping->domain, iova, len);
1992 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001993}
1994
1995/**
1996 * arm_iommu_unmap_page
1997 * @dev: valid struct device pointer
1998 * @handle: DMA address of buffer
1999 * @size: size of buffer (same as passed to dma_map_page)
2000 * @dir: DMA transfer direction (same as passed to dma_map_page)
2001 *
2002 * IOMMU aware version of arm_dma_unmap_page()
2003 */
2004static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002005 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002006{
Will Deacon89cfdb12015-01-16 18:02:15 +01002007 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002008 dma_addr_t iova = handle & PAGE_MASK;
2009 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2010 int offset = handle & ~PAGE_MASK;
2011 int len = PAGE_ALIGN(size + offset);
2012
2013 if (!iova)
2014 return;
2015
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002016 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002017 __dma_page_dev_to_cpu(page, offset, size, dir);
2018
2019 iommu_unmap(mapping->domain, iova, len);
2020 __free_iova(mapping, iova, len);
2021}
2022
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002023/**
2024 * arm_iommu_map_resource - map a device resource for DMA
2025 * @dev: valid struct device pointer
2026 * @phys_addr: physical address of resource
2027 * @size: size of resource to map
2028 * @dir: DMA transfer direction
2029 */
2030static dma_addr_t arm_iommu_map_resource(struct device *dev,
2031 phys_addr_t phys_addr, size_t size,
2032 enum dma_data_direction dir, unsigned long attrs)
2033{
2034 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2035 dma_addr_t dma_addr;
2036 int ret, prot;
2037 phys_addr_t addr = phys_addr & PAGE_MASK;
2038 unsigned int offset = phys_addr & ~PAGE_MASK;
2039 size_t len = PAGE_ALIGN(size + offset);
2040
2041 dma_addr = __alloc_iova(mapping, len);
2042 if (dma_addr == DMA_ERROR_CODE)
2043 return dma_addr;
2044
2045 prot = __dma_direction_to_prot(dir) | IOMMU_MMIO;
2046
2047 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2048 if (ret < 0)
2049 goto fail;
2050
2051 return dma_addr + offset;
2052fail:
2053 __free_iova(mapping, dma_addr, len);
2054 return DMA_ERROR_CODE;
2055}
2056
2057/**
2058 * arm_iommu_unmap_resource - unmap a device DMA resource
2059 * @dev: valid struct device pointer
2060 * @dma_handle: DMA address to resource
2061 * @size: size of resource to map
2062 * @dir: DMA transfer direction
2063 */
2064static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2065 size_t size, enum dma_data_direction dir,
2066 unsigned long attrs)
2067{
2068 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2069 dma_addr_t iova = dma_handle & PAGE_MASK;
2070 unsigned int offset = dma_handle & ~PAGE_MASK;
2071 size_t len = PAGE_ALIGN(size + offset);
2072
2073 if (!iova)
2074 return;
2075
2076 iommu_unmap(mapping->domain, iova, len);
2077 __free_iova(mapping, iova, len);
2078}
2079
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002080static void arm_iommu_sync_single_for_cpu(struct device *dev,
2081 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2082{
Will Deacon89cfdb12015-01-16 18:02:15 +01002083 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002084 dma_addr_t iova = handle & PAGE_MASK;
2085 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2086 unsigned int offset = handle & ~PAGE_MASK;
2087
2088 if (!iova)
2089 return;
2090
Rob Herring0fa478d2012-08-21 12:23:23 +02002091 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002092}
2093
2094static void arm_iommu_sync_single_for_device(struct device *dev,
2095 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2096{
Will Deacon89cfdb12015-01-16 18:02:15 +01002097 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002098 dma_addr_t iova = handle & PAGE_MASK;
2099 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2100 unsigned int offset = handle & ~PAGE_MASK;
2101
2102 if (!iova)
2103 return;
2104
2105 __dma_page_cpu_to_dev(page, offset, size, dir);
2106}
2107
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002108const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002109 .alloc = arm_iommu_alloc_attrs,
2110 .free = arm_iommu_free_attrs,
2111 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002112 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002113
2114 .map_page = arm_iommu_map_page,
2115 .unmap_page = arm_iommu_unmap_page,
2116 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2117 .sync_single_for_device = arm_iommu_sync_single_for_device,
2118
2119 .map_sg = arm_iommu_map_sg,
2120 .unmap_sg = arm_iommu_unmap_sg,
2121 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2122 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002123
2124 .map_resource = arm_iommu_map_resource,
2125 .unmap_resource = arm_iommu_unmap_resource,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002126};
2127
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002128const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002129 .alloc = arm_coherent_iommu_alloc_attrs,
2130 .free = arm_coherent_iommu_free_attrs,
2131 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002132 .get_sgtable = arm_iommu_get_sgtable,
2133
2134 .map_page = arm_coherent_iommu_map_page,
2135 .unmap_page = arm_coherent_iommu_unmap_page,
2136
2137 .map_sg = arm_coherent_iommu_map_sg,
2138 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002139
2140 .map_resource = arm_iommu_map_resource,
2141 .unmap_resource = arm_iommu_unmap_resource,
Rob Herring0fa478d2012-08-21 12:23:23 +02002142};
2143
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002144/**
2145 * arm_iommu_create_mapping
2146 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2147 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002148 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002149 *
2150 * Creates a mapping structure which holds information about used/unused
2151 * IO address ranges, which is required to perform memory allocation and
2152 * mapping with IOMMU aware functions.
2153 *
2154 * The client device need to be attached to the mapping with
2155 * arm_iommu_attach_device function.
2156 */
2157struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002158arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002159{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002160 unsigned int bits = size >> PAGE_SHIFT;
2161 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002162 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002163 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002164 int err = -ENOMEM;
2165
Marek Szyprowski14245322015-04-29 11:29:19 +01002166 /* currently only 32-bit DMA address space is supported */
2167 if (size > DMA_BIT_MASK(32) + 1)
2168 return ERR_PTR(-ERANGE);
2169
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002170 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002171 return ERR_PTR(-EINVAL);
2172
Charan Teja Reddy919aec62017-04-18 18:45:19 +05302173 WARN(!IS_ALIGNED(size, SZ_128M),
2174 "size is not aligned to 128M, alignment enforced");
2175
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002176 if (bitmap_size > PAGE_SIZE) {
2177 extensions = bitmap_size / PAGE_SIZE;
2178 bitmap_size = PAGE_SIZE;
2179 }
2180
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002181 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2182 if (!mapping)
2183 goto err;
2184
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002185 mapping->bitmap_size = bitmap_size;
2186 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002187 GFP_KERNEL);
2188 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002189 goto err2;
2190
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002191 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002192 if (!mapping->bitmaps[0])
2193 goto err3;
2194
2195 mapping->nr_bitmaps = 1;
2196 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002197 mapping->base = base;
Charan Teja Reddy919aec62017-04-18 18:45:19 +05302198 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002199
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002200 spin_lock_init(&mapping->lock);
2201
2202 mapping->domain = iommu_domain_alloc(bus);
2203 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002204 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002205
2206 kref_init(&mapping->kref);
2207 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002208err4:
2209 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002210err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002211 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002212err2:
2213 kfree(mapping);
2214err:
2215 return ERR_PTR(err);
2216}
Prathyush K18177d12013-01-04 06:22:42 -05002217EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002218
2219static void release_iommu_mapping(struct kref *kref)
2220{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002221 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002222 struct dma_iommu_mapping *mapping =
2223 container_of(kref, struct dma_iommu_mapping, kref);
2224
2225 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002226 for (i = 0; i < mapping->nr_bitmaps; i++)
2227 kfree(mapping->bitmaps[i]);
2228 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002229 kfree(mapping);
2230}
2231
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002232static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2233{
2234 int next_bitmap;
2235
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002236 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002237 return -EINVAL;
2238
2239 next_bitmap = mapping->nr_bitmaps;
2240 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2241 GFP_ATOMIC);
2242 if (!mapping->bitmaps[next_bitmap])
2243 return -ENOMEM;
2244
2245 mapping->nr_bitmaps++;
2246
2247 return 0;
2248}
2249
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002250void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2251{
2252 if (mapping)
2253 kref_put(&mapping->kref, release_iommu_mapping);
2254}
Prathyush K18177d12013-01-04 06:22:42 -05002255EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002256
Laurent Pincharteab8d652015-01-23 16:21:49 +02002257static int __arm_iommu_attach_device(struct device *dev,
2258 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002259{
2260 int err;
2261
2262 err = iommu_attach_device(mapping->domain, dev);
2263 if (err)
2264 return err;
2265
2266 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002267 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002268
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002269 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002270 return 0;
2271}
2272
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002273/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002274 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002275 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002276 * @mapping: io address space mapping structure (returned from
2277 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002278 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002279 * Attaches specified io address space mapping to the provided device.
2280 * This replaces the dma operations (dma_map_ops pointer) with the
2281 * IOMMU aware version.
2282 *
2283 * More than one client might be attached to the same io address space
2284 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002285 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002286int arm_iommu_attach_device(struct device *dev,
2287 struct dma_iommu_mapping *mapping)
2288{
2289 int err;
2290
2291 err = __arm_iommu_attach_device(dev, mapping);
2292 if (err)
2293 return err;
2294
2295 set_dma_ops(dev, &iommu_ops);
2296 return 0;
2297}
2298EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2299
2300static void __arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002301{
2302 struct dma_iommu_mapping *mapping;
2303
2304 mapping = to_dma_iommu_mapping(dev);
2305 if (!mapping) {
2306 dev_warn(dev, "Not attached\n");
2307 return;
2308 }
2309
Laura Abbott29defcc2014-08-01 16:13:40 -07002310 if (msm_dma_unmap_all_for_dev(dev))
2311 dev_warn(dev, "IOMMU detach with outstanding mappings\n");
2312
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002313 iommu_detach_device(mapping->domain, dev);
2314 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002315 to_dma_iommu_mapping(dev) = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002316
2317 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2318}
Laurent Pincharteab8d652015-01-23 16:21:49 +02002319
2320/**
2321 * arm_iommu_detach_device
2322 * @dev: valid struct device pointer
2323 *
2324 * Detaches the provided device from a previously attached map.
2325 * This voids the dma operations (dma_map_ops pointer)
2326 */
2327void arm_iommu_detach_device(struct device *dev)
2328{
2329 __arm_iommu_detach_device(dev);
2330 set_dma_ops(dev, NULL);
2331}
Prathyush K18177d12013-01-04 06:22:42 -05002332EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002333
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002334static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002335{
2336 return coherent ? &iommu_coherent_ops : &iommu_ops;
2337}
2338
2339static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002340 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002341{
2342 struct dma_iommu_mapping *mapping;
2343
2344 if (!iommu)
2345 return false;
2346
2347 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2348 if (IS_ERR(mapping)) {
2349 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2350 size, dev_name(dev));
2351 return false;
2352 }
2353
Laurent Pincharteab8d652015-01-23 16:21:49 +02002354 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002355 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2356 dev_name(dev));
2357 arm_iommu_release_mapping(mapping);
2358 return false;
2359 }
2360
2361 return true;
2362}
2363
2364static void arm_teardown_iommu_dma_ops(struct device *dev)
2365{
Will Deacon89cfdb12015-01-16 18:02:15 +01002366 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002367
Will Deaconc2273a12015-01-16 18:01:43 +01002368 if (!mapping)
2369 return;
2370
Laurent Pincharteab8d652015-01-23 16:21:49 +02002371 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002372 arm_iommu_release_mapping(mapping);
2373}
2374
2375#else
2376
2377static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002378 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002379{
2380 return false;
2381}
2382
2383static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2384
2385#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2386
2387#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2388
2389static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
2390{
2391 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2392}
2393
2394void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002395 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002396{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002397 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002398
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002399 dev->archdata.dma_coherent = coherent;
Will Deacon4bb25782014-08-27 17:52:44 +01002400 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2401 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2402 else
2403 dma_ops = arm_get_dma_map_ops(coherent);
2404
2405 set_dma_ops(dev, dma_ops);
2406}
Runmin Wang8cb38812017-03-24 13:14:41 -07002407EXPORT_SYMBOL(arch_setup_dma_ops);
Will Deacon4bb25782014-08-27 17:52:44 +01002408
2409void arch_teardown_dma_ops(struct device *dev)
2410{
2411 arm_teardown_iommu_dma_ops(dev);
2412}