blob: cb2c9f48426a1f0bbe0e703732a0e8fd93610fbd [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>
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +053032#include <linux/dma-mapping-fast.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010034#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040035#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010038#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020039#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010040#include <asm/mach/map.h>
41#include <asm/system_info.h>
42#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Russell King1234e3f2015-07-24 09:10:55 +010044#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010045#include "mm.h"
46
Rabin Vincentb4268672016-03-03 15:58:01 +010047struct arm_dma_alloc_args {
48 struct device *dev;
49 size_t size;
50 gfp_t gfp;
51 pgprot_t prot;
52 const void *caller;
53 bool want_vaddr;
Taniya Das31a03832014-06-12 19:42:06 +053054 bool skip_cpu_sync;
55 bool skip_zeroing;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010056 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010057};
58
59struct arm_dma_free_args {
60 struct device *dev;
61 size_t size;
62 void *cpu_addr;
63 struct page *page;
64 bool want_vaddr;
65};
66
Gregory CLEMENTf1270892016-04-15 11:15:18 +010067#define NORMAL 0
68#define COHERENT 1
69
Rabin Vincentb4268672016-03-03 15:58:01 +010070struct arm_dma_allocator {
71 void *(*alloc)(struct arm_dma_alloc_args *args,
72 struct page **ret_page);
73 void (*free)(struct arm_dma_free_args *args);
74};
75
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010076struct arm_dma_buffer {
77 struct list_head list;
78 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010079 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010080};
81
82static LIST_HEAD(arm_dma_bufs);
83static DEFINE_SPINLOCK(arm_dma_bufs_lock);
84
85static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
86{
87 struct arm_dma_buffer *buf, *found = NULL;
88 unsigned long flags;
89
90 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
91 list_for_each_entry(buf, &arm_dma_bufs, list) {
92 if (buf->virt == virt) {
93 list_del(&buf->list);
94 found = buf;
95 break;
96 }
97 }
98 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
99 return found;
100}
101
Marek Szyprowski15237e12012-02-10 19:55:20 +0100102/*
103 * The DMA API is built upon the notion of "buffer ownership". A buffer
104 * is either exclusively owned by the CPU (and therefore may be accessed
105 * by it) or exclusively owned by the DMA device. These helper functions
106 * represent the transitions between these two ownership states.
107 *
108 * Note, however, that on later ARMs, this notion does not work due to
109 * speculative prefetches. We model our approach on the assumption that
110 * the CPU does do speculative prefetches, which means we clean caches
111 * before transfers and delay cache invalidation until transfer completion.
112 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100113 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100114static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100115 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100116static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100117 size_t, enum dma_data_direction);
118
Laura Abbott4a45a332014-08-05 19:16:28 -0700119static void *
120__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
121 const void *caller);
122
123static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn);
124
125static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot);
126
127static void *arm_dma_remap(struct device *dev, void *cpu_addr,
128 dma_addr_t handle, size_t size,
129 unsigned long attrs);
130
131static void arm_dma_unremap(struct device *dev, void *remapped_addr,
132 size_t size);
133
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100134/**
135 * arm_dma_map_page - map a portion of a page for streaming DMA
136 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
137 * @page: page that buffer resides in
138 * @offset: offset into page for start of buffer
139 * @size: size of buffer to map
140 * @dir: DMA transfer direction
141 *
142 * Ensure that any data held in the cache is appropriately discarded
143 * or written back.
144 *
145 * The device owns this memory once this call has completed. The CPU
146 * can regain ownership by calling dma_unmap_page().
147 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100148static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100149 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700150 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100151{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700152 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100153 __dma_page_cpu_to_dev(page, offset, size, dir);
154 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100155}
156
Rob Herringdd37e942012-08-21 12:20:17 +0200157static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
158 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700159 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200160{
161 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
162}
163
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100164/**
165 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
166 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
167 * @handle: DMA address of buffer
168 * @size: size of buffer (same as passed to dma_map_page)
169 * @dir: DMA transfer direction (same as passed to dma_map_page)
170 *
171 * Unmap a page streaming mode DMA translation. The handle and size
172 * must match what was provided in the previous dma_map_page() call.
173 * All other usages are undefined.
174 *
175 * After this call, reads by the CPU to the buffer are guaranteed to see
176 * whatever the device wrote there.
177 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100178static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700179 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100180{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700181 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100182 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
183 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100184}
185
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100186static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100187 dma_addr_t handle, size_t size, enum dma_data_direction dir)
188{
189 unsigned int offset = handle & (PAGE_SIZE - 1);
190 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200191 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100192}
193
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100194static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100195 dma_addr_t handle, size_t size, enum dma_data_direction dir)
196{
197 unsigned int offset = handle & (PAGE_SIZE - 1);
198 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200199 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100200}
201
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100202struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200203 .alloc = arm_dma_alloc,
204 .free = arm_dma_free,
205 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200206 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100207 .map_page = arm_dma_map_page,
208 .unmap_page = arm_dma_unmap_page,
209 .map_sg = arm_dma_map_sg,
210 .unmap_sg = arm_dma_unmap_sg,
211 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
212 .sync_single_for_device = arm_dma_sync_single_for_device,
213 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
214 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Laura Abbott4a45a332014-08-05 19:16:28 -0700215 .remap = arm_dma_remap,
216 .unremap = arm_dma_unremap,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100217};
218EXPORT_SYMBOL(arm_dma_ops);
219
Rob Herringdd37e942012-08-21 12:20:17 +0200220static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700221 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200222static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700223 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100224static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
225 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700226 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200227
228struct dma_map_ops arm_coherent_dma_ops = {
229 .alloc = arm_coherent_dma_alloc,
230 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100231 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200232 .get_sgtable = arm_dma_get_sgtable,
233 .map_page = arm_coherent_dma_map_page,
234 .map_sg = arm_dma_map_sg,
Rob Herringdd37e942012-08-21 12:20:17 +0200235};
236EXPORT_SYMBOL(arm_coherent_dma_ops);
237
Russell King9f28cde2013-12-06 12:30:42 +0000238static int __dma_supported(struct device *dev, u64 mask, bool warn)
239{
240 unsigned long max_dma_pfn;
241
242 /*
243 * If the mask allows for more memory than we can address,
244 * and we actually have that much memory, then we must
245 * indicate that DMA to this device is not supported.
246 */
247 if (sizeof(mask) != sizeof(dma_addr_t) &&
248 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000249 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000250 if (warn) {
251 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
252 mask);
253 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
254 }
255 return 0;
256 }
257
258 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
259
260 /*
261 * Translate the device's DMA mask to a PFN limit. This
262 * PFN number includes the page which we can DMA to.
263 */
264 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
265 if (warn)
266 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",
267 mask,
268 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
269 max_dma_pfn + 1);
270 return 0;
271 }
272
273 return 1;
274}
275
Catalin Marinasab6494f2009-07-24 12:35:02 +0100276static u64 get_coherent_dma_mask(struct device *dev)
277{
Russell King4dcfa602013-07-09 12:14:49 +0100278 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Catalin Marinasab6494f2009-07-24 12:35:02 +0100280 if (dev) {
281 mask = dev->coherent_dma_mask;
282
283 /*
284 * Sanity check the DMA mask - it must be non-zero, and
285 * must be able to be satisfied by a DMA allocation.
286 */
287 if (mask == 0) {
288 dev_warn(dev, "coherent DMA mask is unset\n");
289 return 0;
290 }
291
Russell King9f28cde2013-12-06 12:30:42 +0000292 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100293 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100294 }
295
296 return mask;
297}
298
Taniya Das31a03832014-06-12 19:42:06 +0530299static void __dma_clear_buffer(struct page *page, size_t size,
300 bool skip_zeroing, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100301{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100302 /*
303 * Ensure that the allocated pages are zeroed, and that any data
304 * lurking in the kernel direct-mapped region is invalidated.
305 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100306 if (PageHighMem(page)) {
307 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
308 phys_addr_t end = base + size;
309 while (size > 0) {
310 void *ptr = kmap_atomic(page);
Taniya Das31a03832014-06-12 19:42:06 +0530311 if (!skip_zeroing)
312 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100313 if (coherent_flag != COHERENT)
314 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100315 kunmap_atomic(ptr);
316 page++;
317 size -= PAGE_SIZE;
318 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100319 if (coherent_flag != COHERENT)
320 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100321 } else {
322 void *ptr = page_address(page);
Taniya Das31a03832014-06-12 19:42:06 +0530323 if (!skip_zeroing)
324 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100325 if (coherent_flag != COHERENT) {
326 dmac_flush_range(ptr, ptr + size);
327 outer_flush_range(__pa(ptr), __pa(ptr) + size);
328 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200329 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100330}
331
Russell King7a9a32a2009-11-19 15:31:07 +0000332/*
333 * Allocate a DMA buffer for 'dev' of size 'size' using the
334 * specified gfp mask. Note that 'size' must be page aligned.
335 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100336static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
337 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000338{
339 unsigned long order = get_order(size);
340 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000341
342 page = alloc_pages(gfp, order);
343 if (!page)
344 return NULL;
345
346 /*
347 * Now split the huge page and free the excess pages
348 */
349 split_page(page, order);
350 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
351 __free_page(p);
352
Taniya Das31a03832014-06-12 19:42:06 +0530353 __dma_clear_buffer(page, size, false, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000354
355 return page;
356}
357
358/*
359 * Free a DMA buffer. 'size' must be page aligned.
360 */
361static void __dma_free_buffer(struct page *page, size_t size)
362{
363 struct page *e = page + (size >> PAGE_SHIFT);
364
365 while (page < e) {
366 __free_page(page);
367 page++;
368 }
369}
370
Catalin Marinasab6494f2009-07-24 12:35:02 +0100371#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Marek Szyprowskic7909502011-12-29 13:09:51 +0100373static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100374 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100375 const void *caller, bool want_vaddr,
Taniya Das31a03832014-06-12 19:42:06 +0530376 bool skip_cpu_sync, bool skip_zeroing,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100377 int coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100378
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200379static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
380 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100381 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200382
383static void *
384__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
385 const void *caller)
386{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200387 /*
388 * DMA allocation can be mapped to user space, so lets
389 * set VM_USERMAP flags too.
390 */
Laura Abbott513510d2014-10-09 15:26:40 -0700391 return dma_common_contiguous_remap(page, size,
392 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
393 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200394}
395
Laura Abbott172b6f02013-01-16 18:23:19 -0800396static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200397{
Laura Abbott513510d2014-10-09 15:26:40 -0700398 dma_common_free_remap(cpu_addr, size,
Laura Abbott172b6f02013-01-16 18:23:19 -0800399 VM_ARM_DMA_CONSISTENT | VM_USERMAP, no_warn);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200400}
401
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200402#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700403static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200404
Laura Abbott36d0fd22014-10-09 15:26:42 -0700405static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100406
407static int __init early_coherent_pool(char *p)
408{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700409 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100410 return 0;
411}
412early_param("coherent_pool", early_coherent_pool);
413
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200414void __init init_dma_coherent_pool_size(unsigned long size)
415{
416 /*
417 * Catch any attempt to set the pool size too late.
418 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700419 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200420
421 /*
422 * Set architecture specific coherent pool size only if
423 * it has not been changed by kernel command line parameter.
424 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700425 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
426 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200427}
428
Marek Szyprowskic7909502011-12-29 13:09:51 +0100429/*
430 * Initialise the coherent pool for atomic allocations.
431 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200432static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100433{
Russell King71b55662013-11-25 12:01:03 +0000434 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100435 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100436 struct page *page;
437 void *ptr;
438
Laura Abbott36d0fd22014-10-09 15:26:42 -0700439 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
440 if (!atomic_pool)
441 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100442 /*
443 * The atomic pool is only used for non-coherent allocations
444 * so we must pass NORMAL for coherent_flag.
445 */
Gioh Kime464ef12014-05-22 13:38:23 +0900446 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700447 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Taniya Das31a03832014-06-12 19:42:06 +0530448 &page, atomic_pool_init, true, false,
449 false, NORMAL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200450 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700451 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100452 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100453 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700454 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300455
Laura Abbott36d0fd22014-10-09 15:26:42 -0700456 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
457 page_to_phys(page),
458 atomic_pool_size, -1);
459 if (ret)
460 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300461
Laura Abbott36d0fd22014-10-09 15:26:42 -0700462 gen_pool_set_algo(atomic_pool,
463 gen_pool_first_fit_order_align,
464 (void *)PAGE_SHIFT);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100465 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700466 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100467 return 0;
468 }
Sachin Kamatec106652012-09-24 08:35:03 +0200469
Laura Abbott36d0fd22014-10-09 15:26:42 -0700470destroy_genpool:
471 gen_pool_destroy(atomic_pool);
472 atomic_pool = NULL;
473out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100474 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700475 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100476 return -ENOMEM;
477}
478/*
479 * CMA is activated by core_initcall, so we must be called after it.
480 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200481postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100482
483struct dma_contig_early_reserve {
484 phys_addr_t base;
485 unsigned long size;
486};
487
488static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
489
490static int dma_mmu_remap_num __initdata;
491
492void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
493{
494 dma_mmu_remap[dma_mmu_remap_num].base = base;
495 dma_mmu_remap[dma_mmu_remap_num].size = size;
496 dma_mmu_remap_num++;
497}
498
499void __init dma_contiguous_remap(void)
500{
501 int i;
502 for (i = 0; i < dma_mmu_remap_num; i++) {
503 phys_addr_t start = dma_mmu_remap[i].base;
504 phys_addr_t end = start + dma_mmu_remap[i].size;
505 struct map_desc map;
506 unsigned long addr;
507
508 if (end > arm_lowmem_limit)
509 end = arm_lowmem_limit;
510 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200511 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100512
513 map.pfn = __phys_to_pfn(start);
514 map.virtual = __phys_to_virt(start);
515 map.length = end - start;
516 map.type = MT_MEMORY_DMA_READY;
517
518 /*
Russell King6b076992014-07-17 12:17:45 +0100519 * Clear previous low-memory mapping to ensure that the
520 * TLB does not see any conflicting entries, then flush
521 * the TLB of the old entries before creating new mappings.
522 *
523 * This ensures that any speculatively loaded TLB entries
524 * (even though they may be rare) can not cause any problems,
525 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100526 */
527 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400528 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100529 pmd_clear(pmd_off_k(addr));
530
Russell King6b076992014-07-17 12:17:45 +0100531 flush_tlb_kernel_range(__phys_to_virt(start),
532 __phys_to_virt(end));
533
Marek Szyprowskic7909502011-12-29 13:09:51 +0100534 iotable_init(&map, 1);
535 }
536}
537
Marek Szyprowskic7909502011-12-29 13:09:51 +0100538static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
539 void *data)
540{
541 struct page *page = virt_to_page(addr);
542 pgprot_t prot = *(pgprot_t *)data;
543
544 set_pte_ext(pte, mk_pte(page, prot), 0);
545 return 0;
546}
547
Patrick Dalyca238022016-02-17 15:58:22 -0800548static int __dma_clear_pte(pte_t *pte, pgtable_t token, unsigned long addr,
549 void *data)
550{
551 pte_clear(&init_mm, addr, pte);
552 return 0;
553}
554
555static void __dma_remap(struct page *page, size_t size, pgprot_t prot,
556 bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100557{
558 unsigned long start = (unsigned long) page_address(page);
559 unsigned end = start + size;
Patrick Dalyca238022016-02-17 15:58:22 -0800560 int (*func)(pte_t *pte, pgtable_t token, unsigned long addr,
561 void *data);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100562
Patrick Dalyca238022016-02-17 15:58:22 -0800563 if (!want_vaddr)
564 func = __dma_clear_pte;
565 else
566 func = __dma_update_pte;
567
568 apply_to_page_range(&init_mm, start, size, func, &prot);
569 mb(); /*Ensure pte's are updated */
Marek Szyprowskic7909502011-12-29 13:09:51 +0100570 flush_tlb_kernel_range(start, end);
571}
572
Laura Abbott172b6f02013-01-16 18:23:19 -0800573
574#define NO_KERNEL_MAPPING_DUMMY 0x2222
Marek Szyprowskic7909502011-12-29 13:09:51 +0100575static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
576 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100577 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100578{
579 struct page *page;
Laura Abbott172b6f02013-01-16 18:23:19 -0800580 void *ptr = (void *)NO_KERNEL_MAPPING_DUMMY;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100581 /*
582 * __alloc_remap_buffer is only called when the device is
583 * non-coherent
584 */
585 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100586 if (!page)
587 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100588 if (!want_vaddr)
589 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100590
591 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
592 if (!ptr) {
593 __dma_free_buffer(page, size);
594 return NULL;
595 }
596
Carlo Caione6e8266e2015-02-09 10:38:35 +0100597 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100598 *ret_page = page;
599 return ptr;
600}
601
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200602static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100603{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700604 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200605 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100606
Laura Abbott36d0fd22014-10-09 15:26:42 -0700607 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200608 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100609 return NULL;
610 }
611
Laura Abbott36d0fd22014-10-09 15:26:42 -0700612 val = gen_pool_alloc(atomic_pool, size);
613 if (val) {
614 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200615
Laura Abbott36d0fd22014-10-09 15:26:42 -0700616 *ret_page = phys_to_page(phys);
617 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100618 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200619
620 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100621}
622
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300623static bool __in_atomic_pool(void *start, size_t size)
624{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700625 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300626}
627
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200628static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100629{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300630 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100631 return 0;
632
Laura Abbott36d0fd22014-10-09 15:26:42 -0700633 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200634
Marek Szyprowskic7909502011-12-29 13:09:51 +0100635 return 1;
636}
637
638static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100639 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100640 const void *caller, bool want_vaddr,
Taniya Das31a03832014-06-12 19:42:06 +0530641 bool skip_cpu_sync, bool skip_zeroing,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100642 int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100643{
644 unsigned long order = get_order(size);
645 size_t count = size >> PAGE_SHIFT;
646 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100647 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100648
649 page = dma_alloc_from_contiguous(dev, count, order);
650 if (!page)
651 return NULL;
652
Taniya Das31a03832014-06-12 19:42:06 +0530653 /*
654 * skip completely if we neither need to zero nor sync.
655 */
656 if (!(skip_cpu_sync && skip_zeroing))
657 __dma_clear_buffer(page, size, skip_zeroing, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100658
Marek Szyprowski9848e482013-01-16 15:38:44 +0100659 if (PageHighMem(page)) {
Laura Abbott172b6f02013-01-16 18:23:19 -0800660 if (!want_vaddr) {
661 /*
662 * Something non-NULL needs to be returned here. Give
663 * back a dummy address that is unmapped to catch
664 * clients trying to use the address incorrectly
665 */
666 ptr = (void *)NO_KERNEL_MAPPING_DUMMY;
Shiraz Hashima279aaa2015-06-25 16:45:24 +0530667
668 /* also flush out the stale highmem mappings */
669 kmap_flush_unused();
670 kmap_atomic_flush_unused();
Laura Abbott172b6f02013-01-16 18:23:19 -0800671 } else {
672 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot,
673 caller);
674 if (!ptr) {
675 dma_release_from_contiguous(dev, page, count);
676 return NULL;
677 }
Marek Szyprowski9848e482013-01-16 15:38:44 +0100678 }
679 } else {
Patrick Dalyca238022016-02-17 15:58:22 -0800680 __dma_remap(page, size, prot, want_vaddr);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100681 ptr = page_address(page);
682 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100683
Marek Szyprowskic7909502011-12-29 13:09:51 +0100684 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100685 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100686}
687
688static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100689 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100690{
Patrick Dalyca238022016-02-17 15:58:22 -0800691 if (PageHighMem(page))
Laura Abbott172b6f02013-01-16 18:23:19 -0800692 __dma_free_remap(cpu_addr, size, true);
Patrick Dalyca238022016-02-17 15:58:22 -0800693 else
694 __dma_remap(page, size, PAGE_KERNEL, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100695 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
696}
697
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700698static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200699{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700700 prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
701 pgprot_writecombine(prot) :
702 pgprot_dmacoherent(prot);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200703 return prot;
704}
705
Marek Szyprowskic7909502011-12-29 13:09:51 +0100706#define nommu() 0
707
Catalin Marinasab6494f2009-07-24 12:35:02 +0100708#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000709
Marek Szyprowskic7909502011-12-29 13:09:51 +0100710#define nommu() 1
711
Carlo Caione6e8266e2015-02-09 10:38:35 +0100712#define __get_dma_pgprot(attrs, prot) __pgprot(0)
713#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200714#define __alloc_from_pool(size, ret_page) NULL
Taniya Das31a03832014-06-12 19:42:06 +0530715#define __alloc_from_contiguous(dev, size, prot, ret, c, \
716 wv, scs, sz, coherent_flag) NULL
Rabin Vincentb4268672016-03-03 15:58:01 +0100717#define __free_from_pool(cpu_addr, size) do { } while (0)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100718#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Laura Abbott172b6f02013-01-16 18:23:19 -0800719#define __dma_free_remap(cpu_addr, size, w) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000720
721#endif /* CONFIG_MMU */
722
Marek Szyprowskic7909502011-12-29 13:09:51 +0100723static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
724 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100725{
Russell King04da5692009-11-19 15:54:45 +0000726 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100727 /* __alloc_simple_buffer is only called when the device is coherent */
728 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100729 if (!page)
730 return NULL;
731
732 *ret_page = page;
733 return page_address(page);
734}
735
Rabin Vincentb4268672016-03-03 15:58:01 +0100736static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
737 struct page **ret_page)
738{
739 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
740 ret_page);
741}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100742
Rabin Vincentb4268672016-03-03 15:58:01 +0100743static void simple_allocator_free(struct arm_dma_free_args *args)
744{
745 __dma_free_buffer(args->page, args->size);
746}
747
748static struct arm_dma_allocator simple_allocator = {
749 .alloc = simple_allocator_alloc,
750 .free = simple_allocator_free,
751};
752
753static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
754 struct page **ret_page)
755{
756 return __alloc_from_contiguous(args->dev, args->size, args->prot,
757 ret_page, args->caller,
Taniya Das31a03832014-06-12 19:42:06 +0530758 args->want_vaddr, args->skip_cpu_sync,
759 args->skip_zeroing, args->coherent_flag);
Rabin Vincentb4268672016-03-03 15:58:01 +0100760}
761
762static void cma_allocator_free(struct arm_dma_free_args *args)
763{
764 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
765 args->size, args->want_vaddr);
766}
767
768static struct arm_dma_allocator cma_allocator = {
769 .alloc = cma_allocator_alloc,
770 .free = cma_allocator_free,
771};
772
773static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
774 struct page **ret_page)
775{
776 return __alloc_from_pool(args->size, ret_page);
777}
778
779static void pool_allocator_free(struct arm_dma_free_args *args)
780{
781 __free_from_pool(args->cpu_addr, args->size);
782}
783
784static struct arm_dma_allocator pool_allocator = {
785 .alloc = pool_allocator_alloc,
786 .free = pool_allocator_free,
787};
788
789static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
790 struct page **ret_page)
791{
792 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
793 args->prot, ret_page, args->caller,
794 args->want_vaddr);
795}
796
797static void remap_allocator_free(struct arm_dma_free_args *args)
798{
799 if (args->want_vaddr)
Laura Abbott172b6f02013-01-16 18:23:19 -0800800 __dma_free_remap(args->cpu_addr, args->size, false);
Rabin Vincentb4268672016-03-03 15:58:01 +0100801
802 __dma_free_buffer(args->page, args->size);
803}
804
805static struct arm_dma_allocator remap_allocator = {
806 .alloc = remap_allocator_alloc,
807 .free = remap_allocator_free,
808};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100809
810static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100811 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700812 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100813{
814 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900815 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000816 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100817 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100818 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100819 struct arm_dma_alloc_args args = {
820 .dev = dev,
821 .size = PAGE_ALIGN(size),
822 .gfp = gfp,
823 .prot = prot,
824 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700825 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Taniya Das31a03832014-06-12 19:42:06 +0530826 .skip_cpu_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC),
827 .skip_zeroing = (attrs & DMA_ATTR_SKIP_ZEROING),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100828 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100829 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100830
Marek Szyprowskic7909502011-12-29 13:09:51 +0100831#ifdef CONFIG_DMA_API_DEBUG
832 u64 limit = (mask + 1) & ~mask;
833 if (limit && size >= limit) {
834 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
835 size, mask);
836 return NULL;
837 }
838#endif
839
840 if (!mask)
841 return NULL;
842
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100843 buf = kzalloc(sizeof(*buf),
844 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100845 if (!buf)
846 return NULL;
847
Marek Szyprowskic7909502011-12-29 13:09:51 +0100848 if (mask < 0xffffffffULL)
849 gfp |= GFP_DMA;
850
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100851 /*
852 * Following is a work-around (a.k.a. hack) to prevent pages
853 * with __GFP_COMP being passed to split_page() which cannot
854 * handle them. The real problem is that this flag probably
855 * should be 0 on ARM as it is not supported on this
856 * platform; see CONFIG_HUGETLBFS.
857 */
858 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100859 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100860
Marek Szyprowski553ac782012-02-29 14:45:28 +0100861 *handle = DMA_ERROR_CODE;
Rabin Vincentb4268672016-03-03 15:58:01 +0100862 allowblock = gfpflags_allow_blocking(gfp);
863 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000864
Rabin Vincentb4268672016-03-03 15:58:01 +0100865 if (cma)
866 buf->allocator = &cma_allocator;
867 else if (nommu() || is_coherent)
868 buf->allocator = &simple_allocator;
869 else if (allowblock)
870 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000871 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100872 buf->allocator = &pool_allocator;
873
874 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000875
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100876 if (page) {
877 unsigned long flags;
878
Russell King9eedd962011-01-03 00:00:17 +0000879 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100880 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100881
882 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
883 list_add(&buf->list, &arm_dma_bufs);
884 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
885 } else {
886 kfree(buf);
887 }
Russell King31ebf942009-11-19 21:12:17 +0000888
Laura Abbott172b6f02013-01-16 18:23:19 -0800889 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100890}
Russell King695ae0a2009-11-19 16:31:39 +0000891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Allocate DMA-coherent memory space and return both the kernel remapped
894 * virtual and bus address for that space.
895 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200896void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700897 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Russell King0ea1ec72013-10-23 16:14:59 +0100899 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400900
Rob Herringdd37e942012-08-21 12:20:17 +0200901 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100902 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200903}
904
905static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700906 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200907{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100908 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100909 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Mike Looijmans55af8a92015-06-03 11:25:31 +0100912static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200913 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700914 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100916 int ret = -ENXIO;
917#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200918 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
919 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100920 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200921 unsigned long off = vma->vm_pgoff;
922
Marek Szyprowski47142f02012-05-15 19:04:13 +0200923 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
924 return ret;
925
Marek Szyprowski50262a42012-07-30 09:35:26 +0200926 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
927 ret = remap_pfn_range(vma, vma->vm_start,
928 pfn + off,
929 vma->vm_end - vma->vm_start,
930 vma->vm_page_prot);
931 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100932#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 return ret;
935}
936
Laura Abbott4a45a332014-08-05 19:16:28 -0700937static void *arm_dma_remap(struct device *dev, void *cpu_addr,
938 dma_addr_t handle, size_t size,
939 unsigned long attrs)
940{
941 void *ptr;
942 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
943 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
944 unsigned long offset = handle & ~PAGE_MASK;
945
946 size = PAGE_ALIGN(size + offset);
947 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot,
948 __builtin_return_address(0));
949 return ptr ? ptr + offset : ptr;
950}
951
952static void arm_dma_unremap(struct device *dev, void *remapped_addr,
953 size_t size)
954{
955 unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
956 struct vm_struct *area;
957
958 remapped_addr = (void *)((unsigned long)remapped_addr & PAGE_MASK);
959
960 area = find_vm_area(remapped_addr);
961 if (!area || (area->flags & flags) != flags) {
962 WARN(1, "trying to free invalid coherent area: %p\n",
963 remapped_addr);
964 return;
965 }
966
967 vunmap(remapped_addr);
968}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100970 * Create userspace mapping for the DMA-coherent memory.
971 */
972static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
973 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700974 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100975{
976 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
977}
978
979int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
980 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700981 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100982{
983#ifdef CONFIG_MMU
984 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
985#endif /* CONFIG_MMU */
986 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
987}
988
989/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100990 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Rob Herringdd37e942012-08-21 12:20:17 +0200992static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700993 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +0200994 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100996 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100997 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100998 struct arm_dma_free_args args = {
999 .dev = dev,
1000 .size = PAGE_ALIGN(size),
1001 .cpu_addr = cpu_addr,
1002 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001003 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +01001004 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001005
1006 buf = arm_dma_buffer_find(cpu_addr);
1007 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
1008 return;
Russell King5edf71a2005-11-25 15:52:51 +00001009
Rabin Vincentb4268672016-03-03 15:58:01 +01001010 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001011 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
Russell Kingafd1a322008-09-25 16:30:57 +01001013
Rob Herringdd37e942012-08-21 12:20:17 +02001014void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001015 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +02001016{
1017 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
1018}
1019
1020static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001021 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +02001022{
1023 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
1024}
1025
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001026int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
1027 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001028 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001029{
1030 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
1031 int ret;
1032
1033 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1034 if (unlikely(ret))
1035 return ret;
1036
1037 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1038 return 0;
1039}
1040
Russell King65af1912009-11-24 17:53:33 +00001041static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +00001042 size_t size, enum dma_data_direction dir,
1043 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +00001044{
Russell King15653372013-01-19 11:05:57 +00001045 unsigned long pfn;
1046 size_t left = size;
1047
1048 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
1049 offset %= PAGE_SIZE;
1050
Russell King65af1912009-11-24 17:53:33 +00001051 /*
1052 * A single sg entry may refer to multiple physically contiguous
1053 * pages. But we still need to process highmem pages individually.
1054 * If highmem is not configured then the bulk of this loop gets
1055 * optimized out.
1056 */
Russell King65af1912009-11-24 17:53:33 +00001057 do {
1058 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +00001059 void *vaddr;
1060
Russell King15653372013-01-19 11:05:57 +00001061 page = pfn_to_page(pfn);
1062
Russell King93f1d622009-11-24 14:41:01 +00001063 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +00001064 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +00001065 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001066
1067 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001068 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +01001069 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001070 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001071 } else {
1072 vaddr = kmap_high_get(page);
1073 if (vaddr) {
1074 op(vaddr + offset, len, dir);
1075 kunmap_high(page);
1076 }
Russell King93f1d622009-11-24 14:41:01 +00001077 }
1078 } else {
1079 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +00001080 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +00001081 }
Russell King65af1912009-11-24 17:53:33 +00001082 offset = 0;
Russell King15653372013-01-19 11:05:57 +00001083 pfn++;
Russell King65af1912009-11-24 17:53:33 +00001084 left -= len;
1085 } while (left);
1086}
1087
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001088/*
1089 * Make an area consistent for devices.
1090 * Note: Drivers should NOT use this function directly, as it will break
1091 * platforms with CONFIG_DMABOUNCE.
1092 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
1093 */
1094static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +00001095 size_t size, enum dma_data_direction dir)
1096{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001097 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -04001098
Russell Kinga9c91472009-11-26 16:19:58 +00001099 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -04001100
Russell King65af1912009-11-24 17:53:33 +00001101 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001102 if (dir == DMA_FROM_DEVICE) {
1103 outer_inv_range(paddr, paddr + size);
1104 } else {
1105 outer_clean_range(paddr, paddr + size);
1106 }
1107 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -04001108}
Russell King4ea0d732009-11-24 16:27:17 +00001109
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001110static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001111 size_t size, enum dma_data_direction dir)
1112{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001113 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001114
1115 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001116 /* in any case, don't bother invalidating if DMA to device */
1117 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001118 outer_inv_range(paddr, paddr + size);
1119
Russell Kingdeace4a2014-05-03 11:06:55 +01001120 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1121 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001122
1123 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001124 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001125 */
Ming Leib2a234e2013-05-18 11:21:36 +01001126 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1127 unsigned long pfn;
1128 size_t left = size;
1129
1130 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1131 off %= PAGE_SIZE;
1132 if (off) {
1133 pfn++;
1134 left -= PAGE_SIZE - off;
1135 }
1136 while (left >= PAGE_SIZE) {
1137 page = pfn_to_page(pfn++);
1138 set_bit(PG_dcache_clean, &page->flags);
1139 left -= PAGE_SIZE;
1140 }
1141 }
Russell King4ea0d732009-11-24 16:27:17 +00001142}
Nicolas Pitre43377452009-03-12 22:52:09 -04001143
Russell Kingafd1a322008-09-25 16:30:57 +01001144/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001145 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001146 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1147 * @sg: list of buffers
1148 * @nents: number of buffers to map
1149 * @dir: DMA transfer direction
1150 *
1151 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1152 * This is the scatter-gather version of the dma_map_single interface.
1153 * Here the scatter gather list elements are each tagged with the
1154 * appropriate dma address and length. They are obtained via
1155 * sg_dma_{address,length}.
1156 *
1157 * Device ownership issues as mentioned for dma_map_single are the same
1158 * here.
1159 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001160int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001161 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001162{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001163 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001164 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001165 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001166
1167 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001168#ifdef CONFIG_NEED_SG_DMA_LENGTH
1169 s->dma_length = s->length;
1170#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001171 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1172 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001173 if (dma_mapping_error(dev, s->dma_address))
1174 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001175 }
Russell Kingafd1a322008-09-25 16:30:57 +01001176 return nents;
Russell King01135d922008-09-25 21:05:02 +01001177
1178 bad_mapping:
1179 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001180 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001181 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001182}
Russell Kingafd1a322008-09-25 16:30:57 +01001183
1184/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001185 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001186 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1187 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001188 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001189 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1190 *
1191 * Unmap a set of streaming mode DMA translations. Again, CPU access
1192 * rules concerning calls here are the same as for dma_unmap_single().
1193 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001194void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001195 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001196{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001197 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001198 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001199
Russell King01135d922008-09-25 21:05:02 +01001200 int i;
Russell King24056f52011-01-03 11:29:28 +00001201
Russell King01135d922008-09-25 21:05:02 +01001202 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001203 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001204}
Russell Kingafd1a322008-09-25 16:30:57 +01001205
1206/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001207 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001208 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1209 * @sg: list of buffers
1210 * @nents: number of buffers to map (returned from dma_map_sg)
1211 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1212 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001213void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001214 int nents, enum dma_data_direction dir)
1215{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001216 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001217 struct scatterlist *s;
1218 int i;
1219
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001220 for_each_sg(sg, s, nents, i)
1221 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1222 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001223}
Russell Kingafd1a322008-09-25 16:30:57 +01001224
1225/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001226 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001227 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1228 * @sg: list of buffers
1229 * @nents: number of buffers to map (returned from dma_map_sg)
1230 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1231 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001232void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001233 int nents, enum dma_data_direction dir)
1234{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001235 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001236 struct scatterlist *s;
1237 int i;
1238
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001239 for_each_sg(sg, s, nents, i)
1240 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1241 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001242}
Russell King24056f52011-01-03 11:29:28 +00001243
Russell King022ae532011-07-08 21:26:59 +01001244/*
1245 * Return whether the given device DMA address mask can be supported
1246 * properly. For example, if your device can only drive the low 24-bits
1247 * during bus mastering, then you would pass 0x00ffffff as the mask
1248 * to this function.
1249 */
1250int dma_supported(struct device *dev, u64 mask)
1251{
Russell King9f28cde2013-12-06 12:30:42 +00001252 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001253}
1254EXPORT_SYMBOL(dma_supported);
1255
Russell King24056f52011-01-03 11:29:28 +00001256#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1257
1258static int __init dma_debug_do_init(void)
1259{
1260 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1261 return 0;
1262}
Marek Szyprowski256ff1c2016-11-15 14:00:53 +01001263core_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001264
1265#ifdef CONFIG_ARM_DMA_USE_IOMMU
1266
1267/* IOMMU */
1268
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001269static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1270
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001271static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1272 size_t size)
1273{
1274 unsigned int order = get_order(size);
1275 unsigned int align = 0;
1276 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301277 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001278 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001279 dma_addr_t iova;
1280 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001281
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001282 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1283 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1284
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001285 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1286 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001287
1288 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001289 for (i = 0; i < mapping->nr_bitmaps; i++) {
1290 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1291 mapping->bits, 0, count, align);
1292
1293 if (start > mapping->bits)
1294 continue;
1295
1296 bitmap_set(mapping->bitmaps[i], start, count);
1297 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001298 }
1299
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001300 /*
1301 * No unused range found. Try to extend the existing mapping
1302 * and perform a second attempt to reserve an IO virtual
1303 * address range of size bytes.
1304 */
1305 if (i == mapping->nr_bitmaps) {
1306 if (extend_iommu_mapping(mapping)) {
1307 spin_unlock_irqrestore(&mapping->lock, flags);
1308 return DMA_ERROR_CODE;
1309 }
1310
1311 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1312 mapping->bits, 0, count, align);
1313
1314 if (start > mapping->bits) {
1315 spin_unlock_irqrestore(&mapping->lock, flags);
1316 return DMA_ERROR_CODE;
1317 }
1318
1319 bitmap_set(mapping->bitmaps[i], start, count);
1320 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001321 spin_unlock_irqrestore(&mapping->lock, flags);
1322
Ritesh Harjani006f8412014-05-20 10:02:59 +05301323 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001324 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001325
1326 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001327}
1328
1329static inline void __free_iova(struct dma_iommu_mapping *mapping,
1330 dma_addr_t addr, size_t size)
1331{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001332 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301333 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001334 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001335 dma_addr_t bitmap_base;
1336 u32 bitmap_index;
1337
1338 if (!size)
1339 return;
1340
Ritesh Harjani006f8412014-05-20 10:02:59 +05301341 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001342 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1343
Ritesh Harjani006f8412014-05-20 10:02:59 +05301344 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001345
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001346 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001347
Ritesh Harjani006f8412014-05-20 10:02:59 +05301348 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001349 /*
1350 * The address range to be freed reaches into the iova
1351 * range of the next bitmap. This should not happen as
1352 * we don't allow this in __alloc_iova (at the
1353 * moment).
1354 */
1355 BUG();
1356 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001357 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001358
1359 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001360 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001361 spin_unlock_irqrestore(&mapping->lock, flags);
1362}
1363
Doug Anderson33298ef2016-01-29 23:06:08 +01001364/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1365static const int iommu_order_array[] = { 9, 8, 4, 0 };
1366
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001367static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001368 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001369 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001370{
1371 struct page **pages;
Shiraz Hashim3340eeb2016-02-21 12:45:34 +05301372 size_t count = size >> PAGE_SHIFT;
1373 size_t array_size = count * sizeof(struct page *);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001374 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001375 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001376
1377 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001378 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001379 else
1380 pages = vzalloc(array_size);
1381 if (!pages)
1382 return NULL;
1383
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001384 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001385 {
1386 unsigned long order = get_order(size);
1387 struct page *page;
1388
1389 page = dma_alloc_from_contiguous(dev, count, order);
1390 if (!page)
1391 goto error;
1392
Taniya Das31a03832014-06-12 19:42:06 +05301393 __dma_clear_buffer(page, size, false, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001394
1395 for (i = 0; i < count; i++)
1396 pages[i] = page + i;
1397
1398 return pages;
1399 }
1400
Doug Anderson14d3ae22016-01-29 23:08:46 +01001401 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001402 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001403 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1404
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001405 /*
1406 * IOMMU can map any pages, so himem can also be used here
1407 */
1408 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1409
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001410 while (count) {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001411 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001412
Doug Anderson33298ef2016-01-29 23:06:08 +01001413 order = iommu_order_array[order_idx];
1414
1415 /* Drop down when we get small */
1416 if (__fls(count) < order) {
1417 order_idx++;
1418 continue;
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001419 }
1420
Doug Anderson33298ef2016-01-29 23:06:08 +01001421 if (order) {
1422 /* See if it's easy to allocate a high-order chunk */
1423 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1424
1425 /* Go down a notch at first sign of pressure */
1426 if (!pages[i]) {
1427 order_idx++;
1428 continue;
1429 }
1430 } else {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001431 pages[i] = alloc_pages(gfp, 0);
1432 if (!pages[i])
1433 goto error;
1434 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001435
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001436 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001437 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001438 j = 1 << order;
1439 while (--j)
1440 pages[i + j] = pages[i] + j;
1441 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001442
Taniya Das31a03832014-06-12 19:42:06 +05301443 __dma_clear_buffer(pages[i], PAGE_SIZE << order,
1444 false, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001445 i += 1 << order;
1446 count -= 1 << order;
1447 }
1448
1449 return pages;
1450error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001451 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001452 if (pages[i])
1453 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001454 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001455 return NULL;
1456}
1457
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001458static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001459 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001460{
1461 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001462 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001463
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001464 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001465 dma_release_from_contiguous(dev, pages[0], count);
1466 } else {
1467 for (i = 0; i < count; i++)
1468 if (pages[i])
1469 __free_pages(pages[i], 0);
1470 }
1471
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001472 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001473 return 0;
1474}
1475
1476/*
1477 * Create a CPU mapping for a specified pages
1478 */
1479static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001480__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1481 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001482{
Laura Abbott513510d2014-10-09 15:26:40 -07001483 return dma_common_pages_remap(pages, size,
1484 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001485}
1486
1487/*
1488 * Create a mapping in device IO address space for specified pages
1489 */
1490static dma_addr_t
1491__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1492{
Will Deacon89cfdb12015-01-16 18:02:15 +01001493 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001494 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1495 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001496 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001497
1498 dma_addr = __alloc_iova(mapping, size);
1499 if (dma_addr == DMA_ERROR_CODE)
1500 return dma_addr;
1501
1502 iova = dma_addr;
1503 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001504 int ret;
1505
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001506 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1507 phys_addr_t phys = page_to_phys(pages[i]);
1508 unsigned int len, j;
1509
1510 for (j = i + 1; j < count; j++, next_pfn++)
1511 if (page_to_pfn(pages[j]) != next_pfn)
1512 break;
1513
1514 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001515 ret = iommu_map(mapping->domain, iova, phys, len,
1516 IOMMU_READ|IOMMU_WRITE);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001517 if (ret < 0)
1518 goto fail;
1519 iova += len;
1520 i = j;
1521 }
1522 return dma_addr;
1523fail:
1524 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1525 __free_iova(mapping, dma_addr, size);
1526 return DMA_ERROR_CODE;
1527}
1528
1529static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1530{
Will Deacon89cfdb12015-01-16 18:02:15 +01001531 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001532
1533 /*
1534 * add optional in-page offset from iova to size and align
1535 * result to page size
1536 */
1537 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1538 iova &= PAGE_MASK;
1539
1540 iommu_unmap(mapping->domain, iova, size);
1541 __free_iova(mapping, iova, size);
1542 return 0;
1543}
1544
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001545static struct page **__atomic_get_pages(void *addr)
1546{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001547 struct page *page;
1548 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001549
Laura Abbott36d0fd22014-10-09 15:26:42 -07001550 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1551 page = phys_to_page(phys);
1552
1553 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001554}
1555
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001556static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001557{
1558 struct vm_struct *area;
1559
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001560 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1561 return __atomic_get_pages(cpu_addr);
1562
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001563 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001564 return cpu_addr;
1565
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001566 area = find_vm_area(cpu_addr);
1567 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1568 return area->pages;
1569 return NULL;
1570}
1571
Gregory CLEMENT56506822016-04-15 11:20:13 +01001572static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
1573 dma_addr_t *handle, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001574{
1575 struct page *page;
1576 void *addr;
1577
Gregory CLEMENT56506822016-04-15 11:20:13 +01001578 if (coherent_flag == COHERENT)
1579 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1580 else
1581 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001582 if (!addr)
1583 return NULL;
1584
1585 *handle = __iommu_create_mapping(dev, &page, size);
1586 if (*handle == DMA_ERROR_CODE)
1587 goto err_mapping;
1588
1589 return addr;
1590
1591err_mapping:
1592 __free_from_pool(addr, size);
1593 return NULL;
1594}
1595
Marek Szyprowskid5898292013-02-08 10:54:48 +01001596static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001597 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001598{
1599 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001600 if (coherent_flag == COHERENT)
1601 __dma_free_buffer(virt_to_page(cpu_addr), size);
1602 else
1603 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001604}
1605
Gregory CLEMENT56506822016-04-15 11:20:13 +01001606static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001607 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001608 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001609{
Russell King71b55662013-11-25 12:01:03 +00001610 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001611 struct page **pages;
1612 void *addr = NULL;
1613
1614 *handle = DMA_ERROR_CODE;
1615 size = PAGE_ALIGN(size);
1616
Gregory CLEMENT56506822016-04-15 11:20:13 +01001617 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1618 return __iommu_alloc_simple(dev, size, gfp, handle,
1619 coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001620
Richard Zhao5b91a982013-06-20 20:31:00 +08001621 /*
1622 * Following is a work-around (a.k.a. hack) to prevent pages
1623 * with __GFP_COMP being passed to split_page() which cannot
1624 * handle them. The real problem is that this flag probably
1625 * should be 0 on ARM as it is not supported on this
1626 * platform; see CONFIG_HUGETLBFS.
1627 */
1628 gfp &= ~(__GFP_COMP);
1629
Gregory CLEMENT56506822016-04-15 11:20:13 +01001630 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001631 if (!pages)
1632 return NULL;
1633
1634 *handle = __iommu_create_mapping(dev, pages, size);
1635 if (*handle == DMA_ERROR_CODE)
1636 goto err_buffer;
1637
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001638 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001639 return pages;
1640
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001641 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1642 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001643 if (!addr)
1644 goto err_mapping;
1645
1646 return addr;
1647
1648err_mapping:
1649 __iommu_remove_mapping(dev, *handle, size);
1650err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001651 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001652 return NULL;
1653}
1654
Gregory CLEMENT56506822016-04-15 11:20:13 +01001655static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001656 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001657{
1658 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1659}
1660
1661static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001662 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001663{
1664 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1665}
1666
1667static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001668 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001669 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001670{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001671 unsigned long uaddr = vma->vm_start;
1672 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001673 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001674 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1675 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001676
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001677 if (!pages)
1678 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001679
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001680 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1681 return -ENXIO;
1682
Marek Szyprowski7e312102015-08-28 09:42:09 +01001683 pages += off;
1684
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001685 do {
1686 int ret = vm_insert_page(vma, uaddr, *pages++);
1687 if (ret) {
1688 pr_err("Remapping memory failed: %d\n", ret);
1689 return ret;
1690 }
1691 uaddr += PAGE_SIZE;
1692 usize -= PAGE_SIZE;
1693 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001694
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001695 return 0;
1696}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001697static int arm_iommu_mmap_attrs(struct device *dev,
1698 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001699 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001700{
1701 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1702
1703 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1704}
1705
1706static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1707 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001708 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001709{
1710 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1711}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001712
1713/*
1714 * free a page as defined by the above mapping.
1715 * Must not be called with IRQs disabled.
1716 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001717void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001718 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001719{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001720 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001721 size = PAGE_ALIGN(size);
1722
Gregory CLEMENT56506822016-04-15 11:20:13 +01001723 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1724 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001725 return;
1726 }
1727
YoungJun Cho836bfa02013-06-17 13:18:52 +09001728 pages = __iommu_get_pages(cpu_addr, attrs);
1729 if (!pages) {
1730 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1731 return;
1732 }
1733
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001734 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001735 dma_common_free_remap(cpu_addr, size,
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001736 VM_ARM_DMA_CONSISTENT | VM_USERMAP, false);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001737 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001738
1739 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001740 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001741}
1742
Gregory CLEMENT56506822016-04-15 11:20:13 +01001743void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001744 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001745{
1746 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1747}
1748
1749void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001750 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001751{
1752 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1753}
1754
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001755static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1756 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001757 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001758{
1759 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1760 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1761
1762 if (!pages)
1763 return -ENXIO;
1764
1765 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1766 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001767}
1768
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001769static int __dma_direction_to_prot(enum dma_data_direction dir)
1770{
1771 int prot;
1772
1773 switch (dir) {
1774 case DMA_BIDIRECTIONAL:
1775 prot = IOMMU_READ | IOMMU_WRITE;
1776 break;
1777 case DMA_TO_DEVICE:
1778 prot = IOMMU_READ;
1779 break;
1780 case DMA_FROM_DEVICE:
1781 prot = IOMMU_WRITE;
1782 break;
1783 default:
1784 prot = 0;
1785 }
1786
1787 return prot;
1788}
1789
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001790/*
1791 * Map a part of the scatter-gather list into contiguous io address space
1792 */
1793static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1794 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001795 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001796 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001797{
Will Deacon89cfdb12015-01-16 18:02:15 +01001798 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001799 dma_addr_t iova, iova_base;
1800 int ret = 0;
1801 unsigned int count;
1802 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001803 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001804
1805 size = PAGE_ALIGN(size);
1806 *handle = DMA_ERROR_CODE;
1807
1808 iova_base = iova = __alloc_iova(mapping, size);
1809 if (iova == DMA_ERROR_CODE)
1810 return -ENOMEM;
1811
1812 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001813 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001814 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1815
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001816 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001817 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1818
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001819 prot = __dma_direction_to_prot(dir);
1820
1821 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001822 if (ret < 0)
1823 goto fail;
1824 count += len >> PAGE_SHIFT;
1825 iova += len;
1826 }
1827 *handle = iova_base;
1828
1829 return 0;
1830fail:
1831 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1832 __free_iova(mapping, iova_base, size);
1833 return ret;
1834}
1835
Rob Herring0fa478d2012-08-21 12:23:23 +02001836static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001837 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001838 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001839{
1840 struct scatterlist *s = sg, *dma = sg, *start = sg;
1841 int i, count = 0;
1842 unsigned int offset = s->offset;
1843 unsigned int size = s->offset + s->length;
1844 unsigned int max = dma_get_max_seg_size(dev);
1845
1846 for (i = 1; i < nents; i++) {
1847 s = sg_next(s);
1848
1849 s->dma_address = DMA_ERROR_CODE;
1850 s->dma_length = 0;
1851
1852 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1853 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001854 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001855 goto bad_mapping;
1856
1857 dma->dma_address += offset;
1858 dma->dma_length = size - offset;
1859
1860 size = offset = s->offset;
1861 start = s;
1862 dma = sg_next(dma);
1863 count += 1;
1864 }
1865 size += s->length;
1866 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001867 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1868 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001869 goto bad_mapping;
1870
1871 dma->dma_address += offset;
1872 dma->dma_length = size - offset;
1873
1874 return count+1;
1875
1876bad_mapping:
1877 for_each_sg(sg, s, count, i)
1878 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1879 return 0;
1880}
1881
1882/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001883 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1884 * @dev: valid struct device pointer
1885 * @sg: list of buffers
1886 * @nents: number of buffers to map
1887 * @dir: DMA transfer direction
1888 *
1889 * Map a set of i/o coherent buffers described by scatterlist in streaming
1890 * mode for DMA. The scatter gather list elements are merged together (if
1891 * possible) and tagged with the appropriate dma address and length. They are
1892 * obtained via sg_dma_{address,length}.
1893 */
1894int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001895 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001896{
1897 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1898}
1899
1900/**
1901 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1902 * @dev: valid struct device pointer
1903 * @sg: list of buffers
1904 * @nents: number of buffers to map
1905 * @dir: DMA transfer direction
1906 *
1907 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1908 * The scatter gather list elements are merged together (if possible) and
1909 * tagged with the appropriate dma address and length. They are obtained via
1910 * sg_dma_{address,length}.
1911 */
1912int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001913 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001914{
1915 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1916}
1917
1918static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001919 int nents, enum dma_data_direction dir,
1920 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001921{
1922 struct scatterlist *s;
1923 int i;
1924
1925 for_each_sg(sg, s, nents, i) {
1926 if (sg_dma_len(s))
1927 __iommu_remove_mapping(dev, sg_dma_address(s),
1928 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001929 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001930 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1931 s->length, dir);
1932 }
1933}
1934
1935/**
1936 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1937 * @dev: valid struct device pointer
1938 * @sg: list of buffers
1939 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1940 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1941 *
1942 * Unmap a set of streaming mode DMA translations. Again, CPU access
1943 * rules concerning calls here are the same as for dma_unmap_single().
1944 */
1945void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001946 int nents, enum dma_data_direction dir,
1947 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001948{
1949 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1950}
1951
1952/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001953 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1954 * @dev: valid struct device pointer
1955 * @sg: list of buffers
1956 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1957 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1958 *
1959 * Unmap a set of streaming mode DMA translations. Again, CPU access
1960 * rules concerning calls here are the same as for dma_unmap_single().
1961 */
1962void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001963 enum dma_data_direction dir,
1964 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001965{
Rob Herring0fa478d2012-08-21 12:23:23 +02001966 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001967}
1968
1969/**
1970 * arm_iommu_sync_sg_for_cpu
1971 * @dev: valid struct device pointer
1972 * @sg: list of buffers
1973 * @nents: number of buffers to map (returned from dma_map_sg)
1974 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1975 */
1976void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1977 int nents, enum dma_data_direction dir)
1978{
1979 struct scatterlist *s;
1980 int i;
1981
1982 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001983 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001984
1985}
1986
1987/**
1988 * arm_iommu_sync_sg_for_device
1989 * @dev: valid struct device pointer
1990 * @sg: list of buffers
1991 * @nents: number of buffers to map (returned from dma_map_sg)
1992 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1993 */
1994void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1995 int nents, enum dma_data_direction dir)
1996{
1997 struct scatterlist *s;
1998 int i;
1999
2000 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02002001 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002002}
2003
2004
2005/**
Rob Herring0fa478d2012-08-21 12:23:23 +02002006 * arm_coherent_iommu_map_page
2007 * @dev: valid struct device pointer
2008 * @page: page that buffer resides in
2009 * @offset: offset into page for start of buffer
2010 * @size: size of buffer to map
2011 * @dir: DMA transfer direction
2012 *
2013 * Coherent IOMMU aware version of arm_dma_map_page()
2014 */
2015static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
2016 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002017 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002018{
Will Deacon89cfdb12015-01-16 18:02:15 +01002019 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002020 dma_addr_t dma_addr;
Liam Markaeff2c12017-01-26 16:46:14 -08002021 int ret, prot, len, start_offset, map_offset;
2022
2023 map_offset = offset & ~PAGE_MASK;
2024 start_offset = offset & PAGE_MASK;
2025 len = PAGE_ALIGN(map_offset + size);
Rob Herring0fa478d2012-08-21 12:23:23 +02002026
2027 dma_addr = __alloc_iova(mapping, len);
2028 if (dma_addr == DMA_ERROR_CODE)
2029 return dma_addr;
2030
Andreas Herrmannc9b24992013-09-27 00:36:15 +02002031 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01002032
Liam Markaeff2c12017-01-26 16:46:14 -08002033 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page) +
2034 start_offset, len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02002035 if (ret < 0)
2036 goto fail;
2037
Liam Markaeff2c12017-01-26 16:46:14 -08002038 return dma_addr + map_offset;
Rob Herring0fa478d2012-08-21 12:23:23 +02002039fail:
2040 __free_iova(mapping, dma_addr, len);
2041 return DMA_ERROR_CODE;
2042}
2043
2044/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002045 * arm_iommu_map_page
2046 * @dev: valid struct device pointer
2047 * @page: page that buffer resides in
2048 * @offset: offset into page for start of buffer
2049 * @size: size of buffer to map
2050 * @dir: DMA transfer direction
2051 *
2052 * IOMMU aware version of arm_dma_map_page()
2053 */
2054static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
2055 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002056 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002057{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002058 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002059 __dma_page_cpu_to_dev(page, offset, size, dir);
2060
Rob Herring0fa478d2012-08-21 12:23:23 +02002061 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
2062}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002063
Rob Herring0fa478d2012-08-21 12:23:23 +02002064/**
2065 * arm_coherent_iommu_unmap_page
2066 * @dev: valid struct device pointer
2067 * @handle: DMA address of buffer
2068 * @size: size of buffer (same as passed to dma_map_page)
2069 * @dir: DMA transfer direction (same as passed to dma_map_page)
2070 *
2071 * Coherent IOMMU aware version of arm_dma_unmap_page()
2072 */
2073static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002074 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002075{
Will Deacon89cfdb12015-01-16 18:02:15 +01002076 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002077 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02002078 int offset = handle & ~PAGE_MASK;
2079 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002080
Rob Herring0fa478d2012-08-21 12:23:23 +02002081 if (!iova)
2082 return;
2083
2084 iommu_unmap(mapping->domain, iova, len);
2085 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002086}
2087
2088/**
2089 * arm_iommu_unmap_page
2090 * @dev: valid struct device pointer
2091 * @handle: DMA address of buffer
2092 * @size: size of buffer (same as passed to dma_map_page)
2093 * @dir: DMA transfer direction (same as passed to dma_map_page)
2094 *
2095 * IOMMU aware version of arm_dma_unmap_page()
2096 */
2097static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002098 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002099{
Will Deacon89cfdb12015-01-16 18:02:15 +01002100 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002101 dma_addr_t iova = handle & PAGE_MASK;
2102 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2103 int offset = handle & ~PAGE_MASK;
2104 int len = PAGE_ALIGN(size + offset);
2105
2106 if (!iova)
2107 return;
2108
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002109 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002110 __dma_page_dev_to_cpu(page, offset, size, dir);
2111
2112 iommu_unmap(mapping->domain, iova, len);
2113 __free_iova(mapping, iova, len);
2114}
2115
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002116/**
2117 * arm_iommu_map_resource - map a device resource for DMA
2118 * @dev: valid struct device pointer
2119 * @phys_addr: physical address of resource
2120 * @size: size of resource to map
2121 * @dir: DMA transfer direction
2122 */
2123static dma_addr_t arm_iommu_map_resource(struct device *dev,
2124 phys_addr_t phys_addr, size_t size,
2125 enum dma_data_direction dir, unsigned long attrs)
2126{
2127 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2128 dma_addr_t dma_addr;
2129 int ret, prot;
2130 phys_addr_t addr = phys_addr & PAGE_MASK;
2131 unsigned int offset = phys_addr & ~PAGE_MASK;
2132 size_t len = PAGE_ALIGN(size + offset);
2133
2134 dma_addr = __alloc_iova(mapping, len);
2135 if (dma_addr == DMA_ERROR_CODE)
2136 return dma_addr;
2137
2138 prot = __dma_direction_to_prot(dir) | IOMMU_MMIO;
2139
2140 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2141 if (ret < 0)
2142 goto fail;
2143
2144 return dma_addr + offset;
2145fail:
2146 __free_iova(mapping, dma_addr, len);
2147 return DMA_ERROR_CODE;
2148}
2149
2150/**
2151 * arm_iommu_unmap_resource - unmap a device DMA resource
2152 * @dev: valid struct device pointer
2153 * @dma_handle: DMA address to resource
2154 * @size: size of resource to map
2155 * @dir: DMA transfer direction
2156 */
2157static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2158 size_t size, enum dma_data_direction dir,
2159 unsigned long attrs)
2160{
2161 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2162 dma_addr_t iova = dma_handle & PAGE_MASK;
2163 unsigned int offset = dma_handle & ~PAGE_MASK;
2164 size_t len = PAGE_ALIGN(size + offset);
2165
2166 if (!iova)
2167 return;
2168
2169 iommu_unmap(mapping->domain, iova, len);
2170 __free_iova(mapping, iova, len);
2171}
2172
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002173static void arm_iommu_sync_single_for_cpu(struct device *dev,
2174 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2175{
Will Deacon89cfdb12015-01-16 18:02:15 +01002176 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002177 dma_addr_t iova = handle & PAGE_MASK;
2178 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2179 unsigned int offset = handle & ~PAGE_MASK;
2180
2181 if (!iova)
2182 return;
2183
Rob Herring0fa478d2012-08-21 12:23:23 +02002184 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002185}
2186
2187static void arm_iommu_sync_single_for_device(struct device *dev,
2188 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2189{
Will Deacon89cfdb12015-01-16 18:02:15 +01002190 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002191 dma_addr_t iova = handle & PAGE_MASK;
2192 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2193 unsigned int offset = handle & ~PAGE_MASK;
2194
2195 if (!iova)
2196 return;
2197
2198 __dma_page_cpu_to_dev(page, offset, size, dir);
2199}
2200
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002201const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002202 .alloc = arm_iommu_alloc_attrs,
2203 .free = arm_iommu_free_attrs,
2204 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002205 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002206
2207 .map_page = arm_iommu_map_page,
2208 .unmap_page = arm_iommu_unmap_page,
2209 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2210 .sync_single_for_device = arm_iommu_sync_single_for_device,
2211
2212 .map_sg = arm_iommu_map_sg,
2213 .unmap_sg = arm_iommu_unmap_sg,
2214 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2215 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002216
2217 .map_resource = arm_iommu_map_resource,
2218 .unmap_resource = arm_iommu_unmap_resource,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002219};
2220
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002221const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002222 .alloc = arm_coherent_iommu_alloc_attrs,
2223 .free = arm_coherent_iommu_free_attrs,
2224 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002225 .get_sgtable = arm_iommu_get_sgtable,
2226
2227 .map_page = arm_coherent_iommu_map_page,
2228 .unmap_page = arm_coherent_iommu_unmap_page,
2229
2230 .map_sg = arm_coherent_iommu_map_sg,
2231 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002232
2233 .map_resource = arm_iommu_map_resource,
2234 .unmap_resource = arm_iommu_unmap_resource,
Rob Herring0fa478d2012-08-21 12:23:23 +02002235};
2236
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002237/**
2238 * arm_iommu_create_mapping
2239 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2240 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002241 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002242 *
2243 * Creates a mapping structure which holds information about used/unused
2244 * IO address ranges, which is required to perform memory allocation and
2245 * mapping with IOMMU aware functions.
2246 *
2247 * The client device need to be attached to the mapping with
2248 * arm_iommu_attach_device function.
2249 */
2250struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002251arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002252{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002253 unsigned int bits = size >> PAGE_SHIFT;
2254 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002255 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002256 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002257 int err = -ENOMEM;
2258
Marek Szyprowski14245322015-04-29 11:29:19 +01002259 /* currently only 32-bit DMA address space is supported */
2260 if (size > DMA_BIT_MASK(32) + 1)
2261 return ERR_PTR(-ERANGE);
2262
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002263 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002264 return ERR_PTR(-EINVAL);
2265
Charan Teja Reddy919aec62017-04-18 18:45:19 +05302266 WARN(!IS_ALIGNED(size, SZ_128M),
2267 "size is not aligned to 128M, alignment enforced");
2268
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002269 if (bitmap_size > PAGE_SIZE) {
2270 extensions = bitmap_size / PAGE_SIZE;
2271 bitmap_size = PAGE_SIZE;
2272 }
2273
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002274 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2275 if (!mapping)
2276 goto err;
2277
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002278 mapping->bitmap_size = bitmap_size;
2279 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002280 GFP_KERNEL);
2281 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002282 goto err2;
2283
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002284 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002285 if (!mapping->bitmaps[0])
2286 goto err3;
2287
2288 mapping->nr_bitmaps = 1;
2289 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002290 mapping->base = base;
Charan Teja Reddy919aec62017-04-18 18:45:19 +05302291 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002292
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002293 spin_lock_init(&mapping->lock);
2294
2295 mapping->domain = iommu_domain_alloc(bus);
2296 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002297 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002298
2299 kref_init(&mapping->kref);
2300 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002301err4:
2302 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002303err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002304 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002305err2:
2306 kfree(mapping);
2307err:
2308 return ERR_PTR(err);
2309}
Prathyush K18177d12013-01-04 06:22:42 -05002310EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002311
2312static void release_iommu_mapping(struct kref *kref)
2313{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002314 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002315 struct dma_iommu_mapping *mapping =
2316 container_of(kref, struct dma_iommu_mapping, kref);
2317
2318 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002319 for (i = 0; i < mapping->nr_bitmaps; i++)
2320 kfree(mapping->bitmaps[i]);
2321 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002322 kfree(mapping);
2323}
2324
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002325static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2326{
2327 int next_bitmap;
2328
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002329 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002330 return -EINVAL;
2331
2332 next_bitmap = mapping->nr_bitmaps;
2333 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2334 GFP_ATOMIC);
2335 if (!mapping->bitmaps[next_bitmap])
2336 return -ENOMEM;
2337
2338 mapping->nr_bitmaps++;
2339
2340 return 0;
2341}
2342
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002343void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2344{
2345 if (mapping)
2346 kref_put(&mapping->kref, release_iommu_mapping);
2347}
Prathyush K18177d12013-01-04 06:22:42 -05002348EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002349
Laurent Pincharteab8d652015-01-23 16:21:49 +02002350static int __arm_iommu_attach_device(struct device *dev,
2351 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002352{
2353 int err;
2354
2355 err = iommu_attach_device(mapping->domain, dev);
2356 if (err)
2357 return err;
2358
2359 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002360 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002361
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002362 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002363 return 0;
2364}
2365
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002366/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002367 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002368 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002369 * @mapping: io address space mapping structure (returned from
2370 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002371 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002372 * Attaches specified io address space mapping to the provided device.
2373 * This replaces the dma operations (dma_map_ops pointer) with the
2374 * IOMMU aware version.
2375 *
2376 * More than one client might be attached to the same io address space
2377 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002378 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002379int arm_iommu_attach_device(struct device *dev,
2380 struct dma_iommu_mapping *mapping)
2381{
2382 int err;
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302383 int s1_bypass = 0;
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302384 int is_fast = 0;
2385
2386 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2387 if (is_fast)
2388 return fast_smmu_attach_device(dev, mapping);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002389
2390 err = __arm_iommu_attach_device(dev, mapping);
2391 if (err)
2392 return err;
2393
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302394 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_S1_BYPASS,
2395 &s1_bypass);
2396 if (!s1_bypass)
2397 set_dma_ops(dev, &iommu_ops);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002398 return 0;
2399}
2400EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2401
2402static void __arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002403{
2404 struct dma_iommu_mapping *mapping;
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302405 int is_fast;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002406
2407 mapping = to_dma_iommu_mapping(dev);
2408 if (!mapping) {
2409 dev_warn(dev, "Not attached\n");
2410 return;
2411 }
2412
Laura Abbott29defcc2014-08-01 16:13:40 -07002413 if (msm_dma_unmap_all_for_dev(dev))
2414 dev_warn(dev, "IOMMU detach with outstanding mappings\n");
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302415 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2416 if (is_fast)
2417 return fast_smmu_detach_device(dev, mapping);
Laura Abbott29defcc2014-08-01 16:13:40 -07002418
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002419 iommu_detach_device(mapping->domain, dev);
2420 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002421 to_dma_iommu_mapping(dev) = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002422
2423 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2424}
Laurent Pincharteab8d652015-01-23 16:21:49 +02002425
2426/**
2427 * arm_iommu_detach_device
2428 * @dev: valid struct device pointer
2429 *
2430 * Detaches the provided device from a previously attached map.
2431 * This voids the dma operations (dma_map_ops pointer)
2432 */
2433void arm_iommu_detach_device(struct device *dev)
2434{
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302435 struct dma_iommu_mapping *mapping;
2436 int s1_bypass = 0;
2437
2438 mapping = to_dma_iommu_mapping(dev);
2439 if (!mapping) {
2440 dev_warn(dev, "Not attached\n");
2441 return;
2442 }
2443
Laurent Pincharteab8d652015-01-23 16:21:49 +02002444 __arm_iommu_detach_device(dev);
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302445
2446 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_S1_BYPASS,
2447 &s1_bypass);
2448 if (!s1_bypass)
2449 set_dma_ops(dev, NULL);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002450}
Prathyush K18177d12013-01-04 06:22:42 -05002451EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002452
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002453static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002454{
2455 return coherent ? &iommu_coherent_ops : &iommu_ops;
2456}
2457
2458static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002459 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002460{
2461 struct dma_iommu_mapping *mapping;
2462
2463 if (!iommu)
2464 return false;
2465
2466 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2467 if (IS_ERR(mapping)) {
2468 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2469 size, dev_name(dev));
2470 return false;
2471 }
2472
Laurent Pincharteab8d652015-01-23 16:21:49 +02002473 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002474 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2475 dev_name(dev));
2476 arm_iommu_release_mapping(mapping);
2477 return false;
2478 }
2479
2480 return true;
2481}
2482
2483static void arm_teardown_iommu_dma_ops(struct device *dev)
2484{
Will Deacon89cfdb12015-01-16 18:02:15 +01002485 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002486
Will Deaconc2273a12015-01-16 18:01:43 +01002487 if (!mapping)
2488 return;
2489
Laurent Pincharteab8d652015-01-23 16:21:49 +02002490 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002491 arm_iommu_release_mapping(mapping);
2492}
2493
2494#else
2495
2496static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002497 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002498{
2499 return false;
2500}
2501
2502static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2503
2504#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2505
2506#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2507
2508static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
2509{
2510 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2511}
2512
2513void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002514 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002515{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002516 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002517
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002518 dev->archdata.dma_coherent = coherent;
Will Deacon4bb25782014-08-27 17:52:44 +01002519 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2520 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2521 else
2522 dma_ops = arm_get_dma_map_ops(coherent);
2523
2524 set_dma_ops(dev, dma_ops);
2525}
Runmin Wang8cb38812017-03-24 13:14:41 -07002526EXPORT_SYMBOL(arch_setup_dma_ops);
Will Deacon4bb25782014-08-27 17:52:44 +01002527
2528void arch_teardown_dma_ops(struct device *dev)
2529{
2530 arm_teardown_iommu_dma_ops(dev);
2531}