blob: b9677ada421f28c9be3252e6f8d32c1adf91fa1f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0ddbccd2008-09-25 15:59:19 +01002 * linux/arch/arm/mm/dma-mapping.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000-2004 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * DMA uncached mapping support.
11 */
Russell King11a5aa32013-11-25 21:52:25 +000012#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/mm.h>
Laura Abbott36d0fd22014-10-09 15:26:42 -070015#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/list.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010022#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050023#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010024#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010025#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020026#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020027#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020028#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010029#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070030#include <linux/cma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010032#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040033#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010036#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020037#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010038#include <asm/mach/map.h>
39#include <asm/system_info.h>
40#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Russell King1234e3f2015-07-24 09:10:55 +010042#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010043#include "mm.h"
44
Rabin Vincentb4268672016-03-03 15:58:01 +010045struct arm_dma_alloc_args {
46 struct device *dev;
47 size_t size;
48 gfp_t gfp;
49 pgprot_t prot;
50 const void *caller;
51 bool want_vaddr;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010052 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010053};
54
55struct arm_dma_free_args {
56 struct device *dev;
57 size_t size;
58 void *cpu_addr;
59 struct page *page;
60 bool want_vaddr;
61};
62
Gregory CLEMENTf1270892016-04-15 11:15:18 +010063#define NORMAL 0
64#define COHERENT 1
65
Rabin Vincentb4268672016-03-03 15:58:01 +010066struct arm_dma_allocator {
67 void *(*alloc)(struct arm_dma_alloc_args *args,
68 struct page **ret_page);
69 void (*free)(struct arm_dma_free_args *args);
70};
71
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010072struct arm_dma_buffer {
73 struct list_head list;
74 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010075 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010076};
77
78static LIST_HEAD(arm_dma_bufs);
79static DEFINE_SPINLOCK(arm_dma_bufs_lock);
80
81static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
82{
83 struct arm_dma_buffer *buf, *found = NULL;
84 unsigned long flags;
85
86 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
87 list_for_each_entry(buf, &arm_dma_bufs, list) {
88 if (buf->virt == virt) {
89 list_del(&buf->list);
90 found = buf;
91 break;
92 }
93 }
94 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
95 return found;
96}
97
Marek Szyprowski15237e12012-02-10 19:55:20 +010098/*
99 * The DMA API is built upon the notion of "buffer ownership". A buffer
100 * is either exclusively owned by the CPU (and therefore may be accessed
101 * by it) or exclusively owned by the DMA device. These helper functions
102 * represent the transitions between these two ownership states.
103 *
104 * Note, however, that on later ARMs, this notion does not work due to
105 * speculative prefetches. We model our approach on the assumption that
106 * the CPU does do speculative prefetches, which means we clean caches
107 * before transfers and delay cache invalidation until transfer completion.
108 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100109 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100110static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100111 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100112static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100113 size_t, enum dma_data_direction);
114
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100115/**
116 * arm_dma_map_page - map a portion of a page for streaming DMA
117 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
118 * @page: page that buffer resides in
119 * @offset: offset into page for start of buffer
120 * @size: size of buffer to map
121 * @dir: DMA transfer direction
122 *
123 * Ensure that any data held in the cache is appropriately discarded
124 * or written back.
125 *
126 * The device owns this memory once this call has completed. The CPU
127 * can regain ownership by calling dma_unmap_page().
128 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100129static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100130 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700131 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100132{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700133 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100134 __dma_page_cpu_to_dev(page, offset, size, dir);
135 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100136}
137
Rob Herringdd37e942012-08-21 12:20:17 +0200138static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
139 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700140 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200141{
142 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
143}
144
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100145/**
146 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
147 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
148 * @handle: DMA address of buffer
149 * @size: size of buffer (same as passed to dma_map_page)
150 * @dir: DMA transfer direction (same as passed to dma_map_page)
151 *
152 * Unmap a page streaming mode DMA translation. The handle and size
153 * must match what was provided in the previous dma_map_page() call.
154 * All other usages are undefined.
155 *
156 * After this call, reads by the CPU to the buffer are guaranteed to see
157 * whatever the device wrote there.
158 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100159static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700160 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100161{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700162 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100163 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
164 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100165}
166
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100167static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100168 dma_addr_t handle, size_t size, enum dma_data_direction dir)
169{
170 unsigned int offset = handle & (PAGE_SIZE - 1);
171 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200172 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100173}
174
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100175static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100176 dma_addr_t handle, size_t size, enum dma_data_direction dir)
177{
178 unsigned int offset = handle & (PAGE_SIZE - 1);
179 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200180 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100181}
182
Christoph Hellwig9eef8b82017-05-22 10:53:03 +0200183static int arm_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
184{
185 return dma_addr == ARM_MAPPING_ERROR;
186}
187
Bart Van Assche52997092017-01-20 13:04:01 -0800188const struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200189 .alloc = arm_dma_alloc,
190 .free = arm_dma_free,
191 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200192 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100193 .map_page = arm_dma_map_page,
194 .unmap_page = arm_dma_unmap_page,
195 .map_sg = arm_dma_map_sg,
196 .unmap_sg = arm_dma_unmap_sg,
197 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
198 .sync_single_for_device = arm_dma_sync_single_for_device,
199 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
200 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +0200201 .mapping_error = arm_dma_mapping_error,
Christoph Hellwig418a7a72017-05-22 11:20:18 +0200202 .dma_supported = arm_dma_supported,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100203};
204EXPORT_SYMBOL(arm_dma_ops);
205
Rob Herringdd37e942012-08-21 12:20:17 +0200206static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700207 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200208static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700209 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100210static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
211 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700212 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200213
Bart Van Assche52997092017-01-20 13:04:01 -0800214const struct dma_map_ops arm_coherent_dma_ops = {
Rob Herringdd37e942012-08-21 12:20:17 +0200215 .alloc = arm_coherent_dma_alloc,
216 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100217 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200218 .get_sgtable = arm_dma_get_sgtable,
219 .map_page = arm_coherent_dma_map_page,
220 .map_sg = arm_dma_map_sg,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +0200221 .mapping_error = arm_dma_mapping_error,
Christoph Hellwig418a7a72017-05-22 11:20:18 +0200222 .dma_supported = arm_dma_supported,
Rob Herringdd37e942012-08-21 12:20:17 +0200223};
224EXPORT_SYMBOL(arm_coherent_dma_ops);
225
Russell King9f28cde2013-12-06 12:30:42 +0000226static int __dma_supported(struct device *dev, u64 mask, bool warn)
227{
228 unsigned long max_dma_pfn;
229
230 /*
231 * If the mask allows for more memory than we can address,
232 * and we actually have that much memory, then we must
233 * indicate that DMA to this device is not supported.
234 */
235 if (sizeof(mask) != sizeof(dma_addr_t) &&
236 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000237 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000238 if (warn) {
239 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
240 mask);
241 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
242 }
243 return 0;
244 }
245
246 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
247
248 /*
249 * Translate the device's DMA mask to a PFN limit. This
250 * PFN number includes the page which we can DMA to.
251 */
252 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
253 if (warn)
254 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",
255 mask,
256 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
257 max_dma_pfn + 1);
258 return 0;
259 }
260
261 return 1;
262}
263
Catalin Marinasab6494f2009-07-24 12:35:02 +0100264static u64 get_coherent_dma_mask(struct device *dev)
265{
Russell King4dcfa602013-07-09 12:14:49 +0100266 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Catalin Marinasab6494f2009-07-24 12:35:02 +0100268 if (dev) {
269 mask = dev->coherent_dma_mask;
270
271 /*
272 * Sanity check the DMA mask - it must be non-zero, and
273 * must be able to be satisfied by a DMA allocation.
274 */
275 if (mask == 0) {
276 dev_warn(dev, "coherent DMA mask is unset\n");
277 return 0;
278 }
279
Russell King9f28cde2013-12-06 12:30:42 +0000280 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100281 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100282 }
283
284 return mask;
285}
286
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100287static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100288{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100289 /*
290 * Ensure that the allocated pages are zeroed, and that any data
291 * lurking in the kernel direct-mapped region is invalidated.
292 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100293 if (PageHighMem(page)) {
294 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
295 phys_addr_t end = base + size;
296 while (size > 0) {
297 void *ptr = kmap_atomic(page);
298 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100299 if (coherent_flag != COHERENT)
300 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100301 kunmap_atomic(ptr);
302 page++;
303 size -= PAGE_SIZE;
304 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100305 if (coherent_flag != COHERENT)
306 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100307 } else {
308 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200309 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100310 if (coherent_flag != COHERENT) {
311 dmac_flush_range(ptr, ptr + size);
312 outer_flush_range(__pa(ptr), __pa(ptr) + size);
313 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200314 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100315}
316
Russell King7a9a32a2009-11-19 15:31:07 +0000317/*
318 * Allocate a DMA buffer for 'dev' of size 'size' using the
319 * specified gfp mask. Note that 'size' must be page aligned.
320 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100321static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
322 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000323{
324 unsigned long order = get_order(size);
325 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000326
327 page = alloc_pages(gfp, order);
328 if (!page)
329 return NULL;
330
331 /*
332 * Now split the huge page and free the excess pages
333 */
334 split_page(page, order);
335 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
336 __free_page(p);
337
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100338 __dma_clear_buffer(page, size, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000339
340 return page;
341}
342
343/*
344 * Free a DMA buffer. 'size' must be page aligned.
345 */
346static void __dma_free_buffer(struct page *page, size_t size)
347{
348 struct page *e = page + (size >> PAGE_SHIFT);
349
350 while (page < e) {
351 __free_page(page);
352 page++;
353 }
354}
355
Catalin Marinasab6494f2009-07-24 12:35:02 +0100356#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Marek Szyprowskic7909502011-12-29 13:09:51 +0100358static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100359 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100360 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800361 int coherent_flag, gfp_t gfp);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100362
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200363static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
364 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100365 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200366
367static void *
368__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
369 const void *caller)
370{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200371 /*
372 * DMA allocation can be mapped to user space, so lets
373 * set VM_USERMAP flags too.
374 */
Laura Abbott513510d2014-10-09 15:26:40 -0700375 return dma_common_contiguous_remap(page, size,
376 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
377 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200378}
379
380static void __dma_free_remap(void *cpu_addr, size_t size)
381{
Laura Abbott513510d2014-10-09 15:26:40 -0700382 dma_common_free_remap(cpu_addr, size,
383 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200384}
385
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200386#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700387static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200388
Laura Abbott36d0fd22014-10-09 15:26:42 -0700389static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100390
391static int __init early_coherent_pool(char *p)
392{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700393 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100394 return 0;
395}
396early_param("coherent_pool", early_coherent_pool);
397
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200398void __init init_dma_coherent_pool_size(unsigned long size)
399{
400 /*
401 * Catch any attempt to set the pool size too late.
402 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700403 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200404
405 /*
406 * Set architecture specific coherent pool size only if
407 * it has not been changed by kernel command line parameter.
408 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700409 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
410 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200411}
412
Marek Szyprowskic7909502011-12-29 13:09:51 +0100413/*
414 * Initialise the coherent pool for atomic allocations.
415 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200416static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100417{
Russell King71b55662013-11-25 12:01:03 +0000418 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100419 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100420 struct page *page;
421 void *ptr;
422
Laura Abbott36d0fd22014-10-09 15:26:42 -0700423 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
424 if (!atomic_pool)
425 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100426 /*
427 * The atomic pool is only used for non-coherent allocations
428 * so we must pass NORMAL for coherent_flag.
429 */
Gioh Kime464ef12014-05-22 13:38:23 +0900430 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700431 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Lucas Stach712c6042017-02-24 14:58:44 -0800432 &page, atomic_pool_init, true, NORMAL,
433 GFP_KERNEL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200434 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700435 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100436 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100437 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700438 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300439
Laura Abbott36d0fd22014-10-09 15:26:42 -0700440 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
441 page_to_phys(page),
442 atomic_pool_size, -1);
443 if (ret)
444 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300445
Laura Abbott36d0fd22014-10-09 15:26:42 -0700446 gen_pool_set_algo(atomic_pool,
447 gen_pool_first_fit_order_align,
448 (void *)PAGE_SHIFT);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100449 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700450 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100451 return 0;
452 }
Sachin Kamatec106652012-09-24 08:35:03 +0200453
Laura Abbott36d0fd22014-10-09 15:26:42 -0700454destroy_genpool:
455 gen_pool_destroy(atomic_pool);
456 atomic_pool = NULL;
457out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100458 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700459 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100460 return -ENOMEM;
461}
462/*
463 * CMA is activated by core_initcall, so we must be called after it.
464 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200465postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100466
467struct dma_contig_early_reserve {
468 phys_addr_t base;
469 unsigned long size;
470};
471
472static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
473
474static int dma_mmu_remap_num __initdata;
475
476void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
477{
478 dma_mmu_remap[dma_mmu_remap_num].base = base;
479 dma_mmu_remap[dma_mmu_remap_num].size = size;
480 dma_mmu_remap_num++;
481}
482
483void __init dma_contiguous_remap(void)
484{
485 int i;
486 for (i = 0; i < dma_mmu_remap_num; i++) {
487 phys_addr_t start = dma_mmu_remap[i].base;
488 phys_addr_t end = start + dma_mmu_remap[i].size;
489 struct map_desc map;
490 unsigned long addr;
491
492 if (end > arm_lowmem_limit)
493 end = arm_lowmem_limit;
494 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200495 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100496
497 map.pfn = __phys_to_pfn(start);
498 map.virtual = __phys_to_virt(start);
499 map.length = end - start;
500 map.type = MT_MEMORY_DMA_READY;
501
502 /*
Russell King6b076992014-07-17 12:17:45 +0100503 * Clear previous low-memory mapping to ensure that the
504 * TLB does not see any conflicting entries, then flush
505 * the TLB of the old entries before creating new mappings.
506 *
507 * This ensures that any speculatively loaded TLB entries
508 * (even though they may be rare) can not cause any problems,
509 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100510 */
511 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400512 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100513 pmd_clear(pmd_off_k(addr));
514
Russell King6b076992014-07-17 12:17:45 +0100515 flush_tlb_kernel_range(__phys_to_virt(start),
516 __phys_to_virt(end));
517
Marek Szyprowskic7909502011-12-29 13:09:51 +0100518 iotable_init(&map, 1);
519 }
520}
521
Marek Szyprowskic7909502011-12-29 13:09:51 +0100522static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
523 void *data)
524{
525 struct page *page = virt_to_page(addr);
526 pgprot_t prot = *(pgprot_t *)data;
527
528 set_pte_ext(pte, mk_pte(page, prot), 0);
529 return 0;
530}
531
532static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
533{
534 unsigned long start = (unsigned long) page_address(page);
535 unsigned end = start + size;
536
537 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100538 flush_tlb_kernel_range(start, end);
539}
540
541static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
542 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100543 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100544{
545 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100546 void *ptr = NULL;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100547 /*
548 * __alloc_remap_buffer is only called when the device is
549 * non-coherent
550 */
551 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100552 if (!page)
553 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100554 if (!want_vaddr)
555 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100556
557 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
558 if (!ptr) {
559 __dma_free_buffer(page, size);
560 return NULL;
561 }
562
Carlo Caione6e8266e2015-02-09 10:38:35 +0100563 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100564 *ret_page = page;
565 return ptr;
566}
567
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200568static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100569{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700570 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200571 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100572
Laura Abbott36d0fd22014-10-09 15:26:42 -0700573 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200574 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100575 return NULL;
576 }
577
Laura Abbott36d0fd22014-10-09 15:26:42 -0700578 val = gen_pool_alloc(atomic_pool, size);
579 if (val) {
580 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200581
Laura Abbott36d0fd22014-10-09 15:26:42 -0700582 *ret_page = phys_to_page(phys);
583 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100584 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200585
586 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100587}
588
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300589static bool __in_atomic_pool(void *start, size_t size)
590{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700591 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300592}
593
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200594static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100595{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300596 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100597 return 0;
598
Laura Abbott36d0fd22014-10-09 15:26:42 -0700599 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200600
Marek Szyprowskic7909502011-12-29 13:09:51 +0100601 return 1;
602}
603
604static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100605 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100606 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800607 int coherent_flag, gfp_t gfp)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100608{
609 unsigned long order = get_order(size);
610 size_t count = size >> PAGE_SHIFT;
611 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100612 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100613
Lucas Stach712c6042017-02-24 14:58:44 -0800614 page = dma_alloc_from_contiguous(dev, count, order, gfp);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100615 if (!page)
616 return NULL;
617
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100618 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100619
Carlo Caione6e8266e2015-02-09 10:38:35 +0100620 if (!want_vaddr)
621 goto out;
622
Marek Szyprowski9848e482013-01-16 15:38:44 +0100623 if (PageHighMem(page)) {
624 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
625 if (!ptr) {
626 dma_release_from_contiguous(dev, page, count);
627 return NULL;
628 }
629 } else {
630 __dma_remap(page, size, prot);
631 ptr = page_address(page);
632 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100633
634 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100635 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100636 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100637}
638
639static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100640 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100641{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100642 if (want_vaddr) {
643 if (PageHighMem(page))
644 __dma_free_remap(cpu_addr, size);
645 else
646 __dma_remap(page, size, PAGE_KERNEL);
647 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100648 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
649}
650
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700651static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200652{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700653 prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
654 pgprot_writecombine(prot) :
655 pgprot_dmacoherent(prot);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200656 return prot;
657}
658
Marek Szyprowskic7909502011-12-29 13:09:51 +0100659#define nommu() 0
660
Catalin Marinasab6494f2009-07-24 12:35:02 +0100661#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000662
Marek Szyprowskic7909502011-12-29 13:09:51 +0100663#define nommu() 1
664
Carlo Caione6e8266e2015-02-09 10:38:35 +0100665#define __get_dma_pgprot(attrs, prot) __pgprot(0)
666#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200667#define __alloc_from_pool(size, ret_page) NULL
Lucas Stach712c6042017-02-24 14:58:44 -0800668#define __alloc_from_contiguous(dev, size, prot, ret, c, wv, coherent_flag, gfp) NULL
Rabin Vincentb4268672016-03-03 15:58:01 +0100669#define __free_from_pool(cpu_addr, size) do { } while (0)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100670#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100671#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000672
673#endif /* CONFIG_MMU */
674
Marek Szyprowskic7909502011-12-29 13:09:51 +0100675static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
676 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100677{
Russell King04da5692009-11-19 15:54:45 +0000678 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100679 /* __alloc_simple_buffer is only called when the device is coherent */
680 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100681 if (!page)
682 return NULL;
683
684 *ret_page = page;
685 return page_address(page);
686}
687
Rabin Vincentb4268672016-03-03 15:58:01 +0100688static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
689 struct page **ret_page)
690{
691 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
692 ret_page);
693}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100694
Rabin Vincentb4268672016-03-03 15:58:01 +0100695static void simple_allocator_free(struct arm_dma_free_args *args)
696{
697 __dma_free_buffer(args->page, args->size);
698}
699
700static struct arm_dma_allocator simple_allocator = {
701 .alloc = simple_allocator_alloc,
702 .free = simple_allocator_free,
703};
704
705static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
706 struct page **ret_page)
707{
708 return __alloc_from_contiguous(args->dev, args->size, args->prot,
709 ret_page, args->caller,
Lucas Stach712c6042017-02-24 14:58:44 -0800710 args->want_vaddr, args->coherent_flag,
711 args->gfp);
Rabin Vincentb4268672016-03-03 15:58:01 +0100712}
713
714static void cma_allocator_free(struct arm_dma_free_args *args)
715{
716 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
717 args->size, args->want_vaddr);
718}
719
720static struct arm_dma_allocator cma_allocator = {
721 .alloc = cma_allocator_alloc,
722 .free = cma_allocator_free,
723};
724
725static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
726 struct page **ret_page)
727{
728 return __alloc_from_pool(args->size, ret_page);
729}
730
731static void pool_allocator_free(struct arm_dma_free_args *args)
732{
733 __free_from_pool(args->cpu_addr, args->size);
734}
735
736static struct arm_dma_allocator pool_allocator = {
737 .alloc = pool_allocator_alloc,
738 .free = pool_allocator_free,
739};
740
741static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
742 struct page **ret_page)
743{
744 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
745 args->prot, ret_page, args->caller,
746 args->want_vaddr);
747}
748
749static void remap_allocator_free(struct arm_dma_free_args *args)
750{
751 if (args->want_vaddr)
752 __dma_free_remap(args->cpu_addr, args->size);
753
754 __dma_free_buffer(args->page, args->size);
755}
756
757static struct arm_dma_allocator remap_allocator = {
758 .alloc = remap_allocator_alloc,
759 .free = remap_allocator_free,
760};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100761
762static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100763 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700764 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100765{
766 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900767 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000768 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100769 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100770 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100771 struct arm_dma_alloc_args args = {
772 .dev = dev,
773 .size = PAGE_ALIGN(size),
774 .gfp = gfp,
775 .prot = prot,
776 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700777 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100778 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100779 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100780
Marek Szyprowskic7909502011-12-29 13:09:51 +0100781#ifdef CONFIG_DMA_API_DEBUG
782 u64 limit = (mask + 1) & ~mask;
783 if (limit && size >= limit) {
784 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
785 size, mask);
786 return NULL;
787 }
788#endif
789
790 if (!mask)
791 return NULL;
792
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100793 buf = kzalloc(sizeof(*buf),
794 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100795 if (!buf)
796 return NULL;
797
Marek Szyprowskic7909502011-12-29 13:09:51 +0100798 if (mask < 0xffffffffULL)
799 gfp |= GFP_DMA;
800
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100801 /*
802 * Following is a work-around (a.k.a. hack) to prevent pages
803 * with __GFP_COMP being passed to split_page() which cannot
804 * handle them. The real problem is that this flag probably
805 * should be 0 on ARM as it is not supported on this
806 * platform; see CONFIG_HUGETLBFS.
807 */
808 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100809 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100810
Christoph Hellwig9eef8b82017-05-22 10:53:03 +0200811 *handle = ARM_MAPPING_ERROR;
Rabin Vincentb4268672016-03-03 15:58:01 +0100812 allowblock = gfpflags_allow_blocking(gfp);
813 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000814
Rabin Vincentb4268672016-03-03 15:58:01 +0100815 if (cma)
816 buf->allocator = &cma_allocator;
817 else if (nommu() || is_coherent)
818 buf->allocator = &simple_allocator;
819 else if (allowblock)
820 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000821 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100822 buf->allocator = &pool_allocator;
823
824 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000825
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100826 if (page) {
827 unsigned long flags;
828
Russell King9eedd962011-01-03 00:00:17 +0000829 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100830 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100831
832 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
833 list_add(&buf->list, &arm_dma_bufs);
834 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
835 } else {
836 kfree(buf);
837 }
Russell King31ebf942009-11-19 21:12:17 +0000838
Rabin Vincentb4268672016-03-03 15:58:01 +0100839 return args.want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100840}
Russell King695ae0a2009-11-19 16:31:39 +0000841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842/*
843 * Allocate DMA-coherent memory space and return both the kernel remapped
844 * virtual and bus address for that space.
845 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200846void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700847 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Russell King0ea1ec72013-10-23 16:14:59 +0100849 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400850
Rob Herringdd37e942012-08-21 12:20:17 +0200851 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100852 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200853}
854
855static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700856 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200857{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100858 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100859 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Mike Looijmans55af8a92015-06-03 11:25:31 +0100862static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200863 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700864 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100866 int ret = -ENXIO;
867#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200868 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
869 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100870 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200871 unsigned long off = vma->vm_pgoff;
872
Marek Szyprowski47142f02012-05-15 19:04:13 +0200873 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
874 return ret;
875
Marek Szyprowski50262a42012-07-30 09:35:26 +0200876 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
877 ret = remap_pfn_range(vma, vma->vm_start,
878 pfn + off,
879 vma->vm_end - vma->vm_start,
880 vma->vm_page_prot);
881 }
Benjamin Gaignard79964a12016-12-12 09:31:18 +0100882#else
883 ret = vm_iomap_memory(vma, vma->vm_start,
884 (vma->vm_end - vma->vm_start));
Catalin Marinasab6494f2009-07-24 12:35:02 +0100885#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 return ret;
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100891 * Create userspace mapping for the DMA-coherent memory.
892 */
893static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
894 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700895 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100896{
897 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
898}
899
900int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
901 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700902 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100903{
904#ifdef CONFIG_MMU
905 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
906#endif /* CONFIG_MMU */
907 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
908}
909
910/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100911 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Rob Herringdd37e942012-08-21 12:20:17 +0200913static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700914 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +0200915 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100917 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100918 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100919 struct arm_dma_free_args args = {
920 .dev = dev,
921 .size = PAGE_ALIGN(size),
922 .cpu_addr = cpu_addr,
923 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700924 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +0100925 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100926
927 buf = arm_dma_buffer_find(cpu_addr);
928 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
929 return;
Russell King5edf71a2005-11-25 15:52:51 +0000930
Rabin Vincentb4268672016-03-03 15:58:01 +0100931 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100932 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
Russell Kingafd1a322008-09-25 16:30:57 +0100934
Rob Herringdd37e942012-08-21 12:20:17 +0200935void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700936 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200937{
938 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
939}
940
941static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700942 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200943{
944 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
945}
946
Russell King916a0082017-03-29 17:12:47 +0100947/*
948 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
949 * that the intention is to allow exporting memory allocated via the
950 * coherent DMA APIs through the dma_buf API, which only accepts a
951 * scattertable. This presents a couple of problems:
952 * 1. Not all memory allocated via the coherent DMA APIs is backed by
953 * a struct page
954 * 2. Passing coherent DMA memory into the streaming APIs is not allowed
955 * as we will try to flush the memory through a different alias to that
956 * actually being used (and the flushes are redundant.)
957 */
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200958int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
959 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700960 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200961{
Russell King916a0082017-03-29 17:12:47 +0100962 unsigned long pfn = dma_to_pfn(dev, handle);
963 struct page *page;
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200964 int ret;
965
Russell King916a0082017-03-29 17:12:47 +0100966 /* If the PFN is not valid, we do not have a struct page */
967 if (!pfn_valid(pfn))
968 return -ENXIO;
969
970 page = pfn_to_page(pfn);
971
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200972 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
973 if (unlikely(ret))
974 return ret;
975
976 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
977 return 0;
978}
979
Russell King65af1912009-11-24 17:53:33 +0000980static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000981 size_t size, enum dma_data_direction dir,
982 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000983{
Russell King15653372013-01-19 11:05:57 +0000984 unsigned long pfn;
985 size_t left = size;
986
987 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
988 offset %= PAGE_SIZE;
989
Russell King65af1912009-11-24 17:53:33 +0000990 /*
991 * A single sg entry may refer to multiple physically contiguous
992 * pages. But we still need to process highmem pages individually.
993 * If highmem is not configured then the bulk of this loop gets
994 * optimized out.
995 */
Russell King65af1912009-11-24 17:53:33 +0000996 do {
997 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000998 void *vaddr;
999
Russell King15653372013-01-19 11:05:57 +00001000 page = pfn_to_page(pfn);
1001
Russell King93f1d622009-11-24 14:41:01 +00001002 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +00001003 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +00001004 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001005
1006 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001007 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +01001008 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001009 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001010 } else {
1011 vaddr = kmap_high_get(page);
1012 if (vaddr) {
1013 op(vaddr + offset, len, dir);
1014 kunmap_high(page);
1015 }
Russell King93f1d622009-11-24 14:41:01 +00001016 }
1017 } else {
1018 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +00001019 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +00001020 }
Russell King65af1912009-11-24 17:53:33 +00001021 offset = 0;
Russell King15653372013-01-19 11:05:57 +00001022 pfn++;
Russell King65af1912009-11-24 17:53:33 +00001023 left -= len;
1024 } while (left);
1025}
1026
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001027/*
1028 * Make an area consistent for devices.
1029 * Note: Drivers should NOT use this function directly, as it will break
1030 * platforms with CONFIG_DMABOUNCE.
1031 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
1032 */
1033static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +00001034 size_t size, enum dma_data_direction dir)
1035{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001036 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -04001037
Russell Kinga9c91472009-11-26 16:19:58 +00001038 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -04001039
Russell King65af1912009-11-24 17:53:33 +00001040 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001041 if (dir == DMA_FROM_DEVICE) {
1042 outer_inv_range(paddr, paddr + size);
1043 } else {
1044 outer_clean_range(paddr, paddr + size);
1045 }
1046 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -04001047}
Russell King4ea0d732009-11-24 16:27:17 +00001048
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001049static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001050 size_t size, enum dma_data_direction dir)
1051{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001052 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001053
1054 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001055 /* in any case, don't bother invalidating if DMA to device */
1056 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001057 outer_inv_range(paddr, paddr + size);
1058
Russell Kingdeace4a2014-05-03 11:06:55 +01001059 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1060 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001061
1062 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001063 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001064 */
Ming Leib2a234e2013-05-18 11:21:36 +01001065 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1066 unsigned long pfn;
1067 size_t left = size;
1068
1069 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1070 off %= PAGE_SIZE;
1071 if (off) {
1072 pfn++;
1073 left -= PAGE_SIZE - off;
1074 }
1075 while (left >= PAGE_SIZE) {
1076 page = pfn_to_page(pfn++);
1077 set_bit(PG_dcache_clean, &page->flags);
1078 left -= PAGE_SIZE;
1079 }
1080 }
Russell King4ea0d732009-11-24 16:27:17 +00001081}
Nicolas Pitre43377452009-03-12 22:52:09 -04001082
Russell Kingafd1a322008-09-25 16:30:57 +01001083/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001084 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001085 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1086 * @sg: list of buffers
1087 * @nents: number of buffers to map
1088 * @dir: DMA transfer direction
1089 *
1090 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1091 * This is the scatter-gather version of the dma_map_single interface.
1092 * Here the scatter gather list elements are each tagged with the
1093 * appropriate dma address and length. They are obtained via
1094 * sg_dma_{address,length}.
1095 *
1096 * Device ownership issues as mentioned for dma_map_single are the same
1097 * here.
1098 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001099int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001100 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001101{
Bart Van Assche52997092017-01-20 13:04:01 -08001102 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001103 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001104 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001105
1106 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001107#ifdef CONFIG_NEED_SG_DMA_LENGTH
1108 s->dma_length = s->length;
1109#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001110 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1111 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001112 if (dma_mapping_error(dev, s->dma_address))
1113 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001114 }
Russell Kingafd1a322008-09-25 16:30:57 +01001115 return nents;
Russell King01135d922008-09-25 21:05:02 +01001116
1117 bad_mapping:
1118 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001119 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001120 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001121}
Russell Kingafd1a322008-09-25 16:30:57 +01001122
1123/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001124 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001125 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1126 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001127 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001128 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1129 *
1130 * Unmap a set of streaming mode DMA translations. Again, CPU access
1131 * rules concerning calls here are the same as for dma_unmap_single().
1132 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001133void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001134 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001135{
Bart Van Assche52997092017-01-20 13:04:01 -08001136 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001137 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001138
Russell King01135d922008-09-25 21:05:02 +01001139 int i;
Russell King24056f52011-01-03 11:29:28 +00001140
Russell King01135d922008-09-25 21:05:02 +01001141 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001142 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001143}
Russell Kingafd1a322008-09-25 16:30:57 +01001144
1145/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001146 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001147 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1148 * @sg: list of buffers
1149 * @nents: number of buffers to map (returned from dma_map_sg)
1150 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1151 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001152void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001153 int nents, enum dma_data_direction dir)
1154{
Bart Van Assche52997092017-01-20 13:04:01 -08001155 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001156 struct scatterlist *s;
1157 int i;
1158
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001159 for_each_sg(sg, s, nents, i)
1160 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1161 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001162}
Russell Kingafd1a322008-09-25 16:30:57 +01001163
1164/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001165 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001166 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1167 * @sg: list of buffers
1168 * @nents: number of buffers to map (returned from dma_map_sg)
1169 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1170 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001171void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001172 int nents, enum dma_data_direction dir)
1173{
Bart Van Assche52997092017-01-20 13:04:01 -08001174 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001175 struct scatterlist *s;
1176 int i;
1177
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001178 for_each_sg(sg, s, nents, i)
1179 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1180 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001181}
Russell King24056f52011-01-03 11:29:28 +00001182
Russell King022ae532011-07-08 21:26:59 +01001183/*
1184 * Return whether the given device DMA address mask can be supported
1185 * properly. For example, if your device can only drive the low 24-bits
1186 * during bus mastering, then you would pass 0x00ffffff as the mask
1187 * to this function.
1188 */
Christoph Hellwig418a7a72017-05-22 11:20:18 +02001189int arm_dma_supported(struct device *dev, u64 mask)
Russell King022ae532011-07-08 21:26:59 +01001190{
Russell King9f28cde2013-12-06 12:30:42 +00001191 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001192}
Russell King022ae532011-07-08 21:26:59 +01001193
Russell King24056f52011-01-03 11:29:28 +00001194#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1195
1196static int __init dma_debug_do_init(void)
1197{
1198 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1199 return 0;
1200}
Marek Szyprowski256ff1c2016-11-15 14:00:53 +01001201core_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001202
1203#ifdef CONFIG_ARM_DMA_USE_IOMMU
1204
Sricharan R7d2822d2017-01-06 18:58:13 +05301205static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
1206{
1207 int prot = 0;
1208
1209 if (attrs & DMA_ATTR_PRIVILEGED)
1210 prot |= IOMMU_PRIV;
1211
1212 switch (dir) {
1213 case DMA_BIDIRECTIONAL:
1214 return prot | IOMMU_READ | IOMMU_WRITE;
1215 case DMA_TO_DEVICE:
1216 return prot | IOMMU_READ;
1217 case DMA_FROM_DEVICE:
1218 return prot | IOMMU_WRITE;
1219 default:
1220 return prot;
1221 }
1222}
1223
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001224/* IOMMU */
1225
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001226static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1227
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001228static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1229 size_t size)
1230{
1231 unsigned int order = get_order(size);
1232 unsigned int align = 0;
1233 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301234 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001235 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001236 dma_addr_t iova;
1237 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001238
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001239 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1240 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1241
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001242 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1243 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001244
1245 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001246 for (i = 0; i < mapping->nr_bitmaps; i++) {
1247 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1248 mapping->bits, 0, count, align);
1249
1250 if (start > mapping->bits)
1251 continue;
1252
1253 bitmap_set(mapping->bitmaps[i], start, count);
1254 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001255 }
1256
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001257 /*
1258 * No unused range found. Try to extend the existing mapping
1259 * and perform a second attempt to reserve an IO virtual
1260 * address range of size bytes.
1261 */
1262 if (i == mapping->nr_bitmaps) {
1263 if (extend_iommu_mapping(mapping)) {
1264 spin_unlock_irqrestore(&mapping->lock, flags);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001265 return ARM_MAPPING_ERROR;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001266 }
1267
1268 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1269 mapping->bits, 0, count, align);
1270
1271 if (start > mapping->bits) {
1272 spin_unlock_irqrestore(&mapping->lock, flags);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001273 return ARM_MAPPING_ERROR;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001274 }
1275
1276 bitmap_set(mapping->bitmaps[i], start, count);
1277 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001278 spin_unlock_irqrestore(&mapping->lock, flags);
1279
Ritesh Harjani006f8412014-05-20 10:02:59 +05301280 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001281 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001282
1283 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001284}
1285
1286static inline void __free_iova(struct dma_iommu_mapping *mapping,
1287 dma_addr_t addr, size_t size)
1288{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001289 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301290 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001291 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001292 dma_addr_t bitmap_base;
1293 u32 bitmap_index;
1294
1295 if (!size)
1296 return;
1297
Ritesh Harjani006f8412014-05-20 10:02:59 +05301298 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001299 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1300
Ritesh Harjani006f8412014-05-20 10:02:59 +05301301 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001302
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001303 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001304
Ritesh Harjani006f8412014-05-20 10:02:59 +05301305 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001306 /*
1307 * The address range to be freed reaches into the iova
1308 * range of the next bitmap. This should not happen as
1309 * we don't allow this in __alloc_iova (at the
1310 * moment).
1311 */
1312 BUG();
1313 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001314 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001315
1316 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001317 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001318 spin_unlock_irqrestore(&mapping->lock, flags);
1319}
1320
Doug Anderson33298ef2016-01-29 23:06:08 +01001321/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1322static const int iommu_order_array[] = { 9, 8, 4, 0 };
1323
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001324static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001325 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001326 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001327{
1328 struct page **pages;
1329 int count = size >> PAGE_SHIFT;
1330 int array_size = count * sizeof(struct page *);
1331 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001332 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001333
1334 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001335 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001336 else
1337 pages = vzalloc(array_size);
1338 if (!pages)
1339 return NULL;
1340
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001341 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001342 {
1343 unsigned long order = get_order(size);
1344 struct page *page;
1345
Lucas Stach712c6042017-02-24 14:58:44 -08001346 page = dma_alloc_from_contiguous(dev, count, order, gfp);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001347 if (!page)
1348 goto error;
1349
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001350 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001351
1352 for (i = 0; i < count; i++)
1353 pages[i] = page + i;
1354
1355 return pages;
1356 }
1357
Doug Anderson14d3ae22016-01-29 23:08:46 +01001358 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001359 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001360 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1361
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001362 /*
1363 * IOMMU can map any pages, so himem can also be used here
1364 */
1365 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1366
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001367 while (count) {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001368 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001369
Doug Anderson33298ef2016-01-29 23:06:08 +01001370 order = iommu_order_array[order_idx];
1371
1372 /* Drop down when we get small */
1373 if (__fls(count) < order) {
1374 order_idx++;
1375 continue;
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001376 }
1377
Doug Anderson33298ef2016-01-29 23:06:08 +01001378 if (order) {
1379 /* See if it's easy to allocate a high-order chunk */
1380 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1381
1382 /* Go down a notch at first sign of pressure */
1383 if (!pages[i]) {
1384 order_idx++;
1385 continue;
1386 }
1387 } else {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001388 pages[i] = alloc_pages(gfp, 0);
1389 if (!pages[i])
1390 goto error;
1391 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001392
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001393 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001394 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001395 j = 1 << order;
1396 while (--j)
1397 pages[i + j] = pages[i] + j;
1398 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001399
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001400 __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001401 i += 1 << order;
1402 count -= 1 << order;
1403 }
1404
1405 return pages;
1406error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001407 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001408 if (pages[i])
1409 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001410 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001411 return NULL;
1412}
1413
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001414static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001415 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001416{
1417 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001418 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001419
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001420 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001421 dma_release_from_contiguous(dev, pages[0], count);
1422 } else {
1423 for (i = 0; i < count; i++)
1424 if (pages[i])
1425 __free_pages(pages[i], 0);
1426 }
1427
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001428 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001429 return 0;
1430}
1431
1432/*
1433 * Create a CPU mapping for a specified pages
1434 */
1435static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001436__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1437 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001438{
Laura Abbott513510d2014-10-09 15:26:40 -07001439 return dma_common_pages_remap(pages, size,
1440 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001441}
1442
1443/*
1444 * Create a mapping in device IO address space for specified pages
1445 */
1446static dma_addr_t
Sricharan R7d2822d2017-01-06 18:58:13 +05301447__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1448 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001449{
Will Deacon89cfdb12015-01-16 18:02:15 +01001450 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001451 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1452 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001453 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001454
1455 dma_addr = __alloc_iova(mapping, size);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001456 if (dma_addr == ARM_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001457 return dma_addr;
1458
1459 iova = dma_addr;
1460 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001461 int ret;
1462
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001463 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1464 phys_addr_t phys = page_to_phys(pages[i]);
1465 unsigned int len, j;
1466
1467 for (j = i + 1; j < count; j++, next_pfn++)
1468 if (page_to_pfn(pages[j]) != next_pfn)
1469 break;
1470
1471 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001472 ret = iommu_map(mapping->domain, iova, phys, len,
Sricharan R7d2822d2017-01-06 18:58:13 +05301473 __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001474 if (ret < 0)
1475 goto fail;
1476 iova += len;
1477 i = j;
1478 }
1479 return dma_addr;
1480fail:
1481 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1482 __free_iova(mapping, dma_addr, size);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001483 return ARM_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001484}
1485
1486static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1487{
Will Deacon89cfdb12015-01-16 18:02:15 +01001488 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001489
1490 /*
1491 * add optional in-page offset from iova to size and align
1492 * result to page size
1493 */
1494 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1495 iova &= PAGE_MASK;
1496
1497 iommu_unmap(mapping->domain, iova, size);
1498 __free_iova(mapping, iova, size);
1499 return 0;
1500}
1501
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001502static struct page **__atomic_get_pages(void *addr)
1503{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001504 struct page *page;
1505 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001506
Laura Abbott36d0fd22014-10-09 15:26:42 -07001507 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1508 page = phys_to_page(phys);
1509
1510 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001511}
1512
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001513static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001514{
1515 struct vm_struct *area;
1516
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001517 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1518 return __atomic_get_pages(cpu_addr);
1519
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001520 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001521 return cpu_addr;
1522
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001523 area = find_vm_area(cpu_addr);
1524 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1525 return area->pages;
1526 return NULL;
1527}
1528
Gregory CLEMENT56506822016-04-15 11:20:13 +01001529static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
Sricharan R7d2822d2017-01-06 18:58:13 +05301530 dma_addr_t *handle, int coherent_flag,
1531 unsigned long attrs)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001532{
1533 struct page *page;
1534 void *addr;
1535
Gregory CLEMENT56506822016-04-15 11:20:13 +01001536 if (coherent_flag == COHERENT)
1537 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1538 else
1539 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001540 if (!addr)
1541 return NULL;
1542
Sricharan R7d2822d2017-01-06 18:58:13 +05301543 *handle = __iommu_create_mapping(dev, &page, size, attrs);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001544 if (*handle == ARM_MAPPING_ERROR)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001545 goto err_mapping;
1546
1547 return addr;
1548
1549err_mapping:
1550 __free_from_pool(addr, size);
1551 return NULL;
1552}
1553
Marek Szyprowskid5898292013-02-08 10:54:48 +01001554static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001555 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001556{
1557 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001558 if (coherent_flag == COHERENT)
1559 __dma_free_buffer(virt_to_page(cpu_addr), size);
1560 else
1561 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001562}
1563
Gregory CLEMENT56506822016-04-15 11:20:13 +01001564static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001565 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001566 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001567{
Russell King71b55662013-11-25 12:01:03 +00001568 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001569 struct page **pages;
1570 void *addr = NULL;
1571
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001572 *handle = ARM_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001573 size = PAGE_ALIGN(size);
1574
Gregory CLEMENT56506822016-04-15 11:20:13 +01001575 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1576 return __iommu_alloc_simple(dev, size, gfp, handle,
Sricharan R7d2822d2017-01-06 18:58:13 +05301577 coherent_flag, attrs);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001578
Richard Zhao5b91a982013-06-20 20:31:00 +08001579 /*
1580 * Following is a work-around (a.k.a. hack) to prevent pages
1581 * with __GFP_COMP being passed to split_page() which cannot
1582 * handle them. The real problem is that this flag probably
1583 * should be 0 on ARM as it is not supported on this
1584 * platform; see CONFIG_HUGETLBFS.
1585 */
1586 gfp &= ~(__GFP_COMP);
1587
Gregory CLEMENT56506822016-04-15 11:20:13 +01001588 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001589 if (!pages)
1590 return NULL;
1591
Sricharan R7d2822d2017-01-06 18:58:13 +05301592 *handle = __iommu_create_mapping(dev, pages, size, attrs);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001593 if (*handle == ARM_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001594 goto err_buffer;
1595
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001596 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001597 return pages;
1598
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001599 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1600 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001601 if (!addr)
1602 goto err_mapping;
1603
1604 return addr;
1605
1606err_mapping:
1607 __iommu_remove_mapping(dev, *handle, size);
1608err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001609 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001610 return NULL;
1611}
1612
Gregory CLEMENT56506822016-04-15 11:20:13 +01001613static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001614 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001615{
1616 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1617}
1618
1619static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001620 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001621{
1622 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1623}
1624
1625static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001626 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001627 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001628{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001629 unsigned long uaddr = vma->vm_start;
1630 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001631 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001632 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1633 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001634
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001635 if (!pages)
1636 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001637
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001638 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1639 return -ENXIO;
1640
Marek Szyprowski7e312102015-08-28 09:42:09 +01001641 pages += off;
1642
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001643 do {
1644 int ret = vm_insert_page(vma, uaddr, *pages++);
1645 if (ret) {
1646 pr_err("Remapping memory failed: %d\n", ret);
1647 return ret;
1648 }
1649 uaddr += PAGE_SIZE;
1650 usize -= PAGE_SIZE;
1651 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001652
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001653 return 0;
1654}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001655static int arm_iommu_mmap_attrs(struct device *dev,
1656 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001657 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001658{
1659 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1660
1661 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1662}
1663
1664static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1665 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001666 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001667{
1668 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1669}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001670
1671/*
1672 * free a page as defined by the above mapping.
1673 * Must not be called with IRQs disabled.
1674 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001675void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001676 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001677{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001678 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001679 size = PAGE_ALIGN(size);
1680
Gregory CLEMENT56506822016-04-15 11:20:13 +01001681 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1682 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001683 return;
1684 }
1685
YoungJun Cho836bfa02013-06-17 13:18:52 +09001686 pages = __iommu_get_pages(cpu_addr, attrs);
1687 if (!pages) {
1688 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1689 return;
1690 }
1691
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001692 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001693 dma_common_free_remap(cpu_addr, size,
1694 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001695 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001696
1697 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001698 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001699}
1700
Gregory CLEMENT56506822016-04-15 11:20:13 +01001701void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001702 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001703{
1704 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1705}
1706
1707void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001708 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001709{
1710 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1711}
1712
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001713static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1714 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001715 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001716{
1717 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1718 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1719
1720 if (!pages)
1721 return -ENXIO;
1722
1723 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1724 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001725}
1726
1727/*
1728 * Map a part of the scatter-gather list into contiguous io address space
1729 */
1730static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1731 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001732 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001733 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001734{
Will Deacon89cfdb12015-01-16 18:02:15 +01001735 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001736 dma_addr_t iova, iova_base;
1737 int ret = 0;
1738 unsigned int count;
1739 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001740 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001741
1742 size = PAGE_ALIGN(size);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001743 *handle = ARM_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001744
1745 iova_base = iova = __alloc_iova(mapping, size);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001746 if (iova == ARM_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001747 return -ENOMEM;
1748
1749 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001750 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001751 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1752
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001753 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001754 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1755
Sricharan R7d2822d2017-01-06 18:58:13 +05301756 prot = __dma_info_to_prot(dir, attrs);
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001757
1758 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001759 if (ret < 0)
1760 goto fail;
1761 count += len >> PAGE_SHIFT;
1762 iova += len;
1763 }
1764 *handle = iova_base;
1765
1766 return 0;
1767fail:
1768 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1769 __free_iova(mapping, iova_base, size);
1770 return ret;
1771}
1772
Rob Herring0fa478d2012-08-21 12:23:23 +02001773static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001774 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001775 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001776{
1777 struct scatterlist *s = sg, *dma = sg, *start = sg;
1778 int i, count = 0;
1779 unsigned int offset = s->offset;
1780 unsigned int size = s->offset + s->length;
1781 unsigned int max = dma_get_max_seg_size(dev);
1782
1783 for (i = 1; i < nents; i++) {
1784 s = sg_next(s);
1785
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001786 s->dma_address = ARM_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001787 s->dma_length = 0;
1788
1789 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1790 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001791 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001792 goto bad_mapping;
1793
1794 dma->dma_address += offset;
1795 dma->dma_length = size - offset;
1796
1797 size = offset = s->offset;
1798 start = s;
1799 dma = sg_next(dma);
1800 count += 1;
1801 }
1802 size += s->length;
1803 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001804 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1805 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001806 goto bad_mapping;
1807
1808 dma->dma_address += offset;
1809 dma->dma_length = size - offset;
1810
1811 return count+1;
1812
1813bad_mapping:
1814 for_each_sg(sg, s, count, i)
1815 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1816 return 0;
1817}
1818
1819/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001820 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1821 * @dev: valid struct device pointer
1822 * @sg: list of buffers
1823 * @nents: number of buffers to map
1824 * @dir: DMA transfer direction
1825 *
1826 * Map a set of i/o coherent buffers described by scatterlist in streaming
1827 * mode for DMA. The scatter gather list elements are merged together (if
1828 * possible) and tagged with the appropriate dma address and length. They are
1829 * obtained via sg_dma_{address,length}.
1830 */
1831int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001832 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001833{
1834 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1835}
1836
1837/**
1838 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1839 * @dev: valid struct device pointer
1840 * @sg: list of buffers
1841 * @nents: number of buffers to map
1842 * @dir: DMA transfer direction
1843 *
1844 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1845 * The scatter gather list elements are merged together (if possible) and
1846 * tagged with the appropriate dma address and length. They are obtained via
1847 * sg_dma_{address,length}.
1848 */
1849int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001850 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001851{
1852 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1853}
1854
1855static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001856 int nents, enum dma_data_direction dir,
1857 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001858{
1859 struct scatterlist *s;
1860 int i;
1861
1862 for_each_sg(sg, s, nents, i) {
1863 if (sg_dma_len(s))
1864 __iommu_remove_mapping(dev, sg_dma_address(s),
1865 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001866 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001867 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1868 s->length, dir);
1869 }
1870}
1871
1872/**
1873 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1874 * @dev: valid struct device pointer
1875 * @sg: list of buffers
1876 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1877 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1878 *
1879 * Unmap a set of streaming mode DMA translations. Again, CPU access
1880 * rules concerning calls here are the same as for dma_unmap_single().
1881 */
1882void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001883 int nents, enum dma_data_direction dir,
1884 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001885{
1886 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1887}
1888
1889/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001890 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1891 * @dev: valid struct device pointer
1892 * @sg: list of buffers
1893 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1894 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1895 *
1896 * Unmap a set of streaming mode DMA translations. Again, CPU access
1897 * rules concerning calls here are the same as for dma_unmap_single().
1898 */
1899void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001900 enum dma_data_direction dir,
1901 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001902{
Rob Herring0fa478d2012-08-21 12:23:23 +02001903 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001904}
1905
1906/**
1907 * arm_iommu_sync_sg_for_cpu
1908 * @dev: valid struct device pointer
1909 * @sg: list of buffers
1910 * @nents: number of buffers to map (returned from dma_map_sg)
1911 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1912 */
1913void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1914 int nents, enum dma_data_direction dir)
1915{
1916 struct scatterlist *s;
1917 int i;
1918
1919 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001920 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001921
1922}
1923
1924/**
1925 * arm_iommu_sync_sg_for_device
1926 * @dev: valid struct device pointer
1927 * @sg: list of buffers
1928 * @nents: number of buffers to map (returned from dma_map_sg)
1929 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1930 */
1931void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1932 int nents, enum dma_data_direction dir)
1933{
1934 struct scatterlist *s;
1935 int i;
1936
1937 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001938 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001939}
1940
1941
1942/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001943 * arm_coherent_iommu_map_page
1944 * @dev: valid struct device pointer
1945 * @page: page that buffer resides in
1946 * @offset: offset into page for start of buffer
1947 * @size: size of buffer to map
1948 * @dir: DMA transfer direction
1949 *
1950 * Coherent IOMMU aware version of arm_dma_map_page()
1951 */
1952static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1953 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001954 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001955{
Will Deacon89cfdb12015-01-16 18:02:15 +01001956 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001957 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001958 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001959
1960 dma_addr = __alloc_iova(mapping, len);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001961 if (dma_addr == ARM_MAPPING_ERROR)
Rob Herring0fa478d2012-08-21 12:23:23 +02001962 return dma_addr;
1963
Sricharan R7d2822d2017-01-06 18:58:13 +05301964 prot = __dma_info_to_prot(dir, attrs);
Will Deacon13987d62013-06-10 19:34:39 +01001965
1966 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001967 if (ret < 0)
1968 goto fail;
1969
1970 return dma_addr + offset;
1971fail:
1972 __free_iova(mapping, dma_addr, len);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02001973 return ARM_MAPPING_ERROR;
Rob Herring0fa478d2012-08-21 12:23:23 +02001974}
1975
1976/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001977 * arm_iommu_map_page
1978 * @dev: valid struct device pointer
1979 * @page: page that buffer resides in
1980 * @offset: offset into page for start of buffer
1981 * @size: size of buffer to map
1982 * @dir: DMA transfer direction
1983 *
1984 * IOMMU aware version of arm_dma_map_page()
1985 */
1986static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1987 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001988 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001989{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001990 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001991 __dma_page_cpu_to_dev(page, offset, size, dir);
1992
Rob Herring0fa478d2012-08-21 12:23:23 +02001993 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1994}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001995
Rob Herring0fa478d2012-08-21 12:23:23 +02001996/**
1997 * arm_coherent_iommu_unmap_page
1998 * @dev: valid struct device pointer
1999 * @handle: DMA address of buffer
2000 * @size: size of buffer (same as passed to dma_map_page)
2001 * @dir: DMA transfer direction (same as passed to dma_map_page)
2002 *
2003 * Coherent IOMMU aware version of arm_dma_unmap_page()
2004 */
2005static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002006 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002007{
Will Deacon89cfdb12015-01-16 18:02:15 +01002008 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002009 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02002010 int offset = handle & ~PAGE_MASK;
2011 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002012
Rob Herring0fa478d2012-08-21 12:23:23 +02002013 if (!iova)
2014 return;
2015
2016 iommu_unmap(mapping->domain, iova, len);
2017 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002018}
2019
2020/**
2021 * arm_iommu_unmap_page
2022 * @dev: valid struct device pointer
2023 * @handle: DMA address of buffer
2024 * @size: size of buffer (same as passed to dma_map_page)
2025 * @dir: DMA transfer direction (same as passed to dma_map_page)
2026 *
2027 * IOMMU aware version of arm_dma_unmap_page()
2028 */
2029static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002030 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002031{
Will Deacon89cfdb12015-01-16 18:02:15 +01002032 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002033 dma_addr_t iova = handle & PAGE_MASK;
2034 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2035 int offset = handle & ~PAGE_MASK;
2036 int len = PAGE_ALIGN(size + offset);
2037
2038 if (!iova)
2039 return;
2040
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002041 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002042 __dma_page_dev_to_cpu(page, offset, size, dir);
2043
2044 iommu_unmap(mapping->domain, iova, len);
2045 __free_iova(mapping, iova, len);
2046}
2047
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002048/**
2049 * arm_iommu_map_resource - map a device resource for DMA
2050 * @dev: valid struct device pointer
2051 * @phys_addr: physical address of resource
2052 * @size: size of resource to map
2053 * @dir: DMA transfer direction
2054 */
2055static dma_addr_t arm_iommu_map_resource(struct device *dev,
2056 phys_addr_t phys_addr, size_t size,
2057 enum dma_data_direction dir, unsigned long attrs)
2058{
2059 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2060 dma_addr_t dma_addr;
2061 int ret, prot;
2062 phys_addr_t addr = phys_addr & PAGE_MASK;
2063 unsigned int offset = phys_addr & ~PAGE_MASK;
2064 size_t len = PAGE_ALIGN(size + offset);
2065
2066 dma_addr = __alloc_iova(mapping, len);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002067 if (dma_addr == ARM_MAPPING_ERROR)
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002068 return dma_addr;
2069
Sricharan R7d2822d2017-01-06 18:58:13 +05302070 prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002071
2072 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2073 if (ret < 0)
2074 goto fail;
2075
2076 return dma_addr + offset;
2077fail:
2078 __free_iova(mapping, dma_addr, len);
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002079 return ARM_MAPPING_ERROR;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002080}
2081
2082/**
2083 * arm_iommu_unmap_resource - unmap a device DMA resource
2084 * @dev: valid struct device pointer
2085 * @dma_handle: DMA address to resource
2086 * @size: size of resource to map
2087 * @dir: DMA transfer direction
2088 */
2089static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2090 size_t size, enum dma_data_direction dir,
2091 unsigned long attrs)
2092{
2093 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2094 dma_addr_t iova = dma_handle & PAGE_MASK;
2095 unsigned int offset = dma_handle & ~PAGE_MASK;
2096 size_t len = PAGE_ALIGN(size + offset);
2097
2098 if (!iova)
2099 return;
2100
2101 iommu_unmap(mapping->domain, iova, len);
2102 __free_iova(mapping, iova, len);
2103}
2104
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002105static void arm_iommu_sync_single_for_cpu(struct device *dev,
2106 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2107{
Will Deacon89cfdb12015-01-16 18:02:15 +01002108 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002109 dma_addr_t iova = handle & PAGE_MASK;
2110 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2111 unsigned int offset = handle & ~PAGE_MASK;
2112
2113 if (!iova)
2114 return;
2115
Rob Herring0fa478d2012-08-21 12:23:23 +02002116 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002117}
2118
2119static void arm_iommu_sync_single_for_device(struct device *dev,
2120 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2121{
Will Deacon89cfdb12015-01-16 18:02:15 +01002122 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002123 dma_addr_t iova = handle & PAGE_MASK;
2124 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2125 unsigned int offset = handle & ~PAGE_MASK;
2126
2127 if (!iova)
2128 return;
2129
2130 __dma_page_cpu_to_dev(page, offset, size, dir);
2131}
2132
Bart Van Assche52997092017-01-20 13:04:01 -08002133const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002134 .alloc = arm_iommu_alloc_attrs,
2135 .free = arm_iommu_free_attrs,
2136 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002137 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002138
2139 .map_page = arm_iommu_map_page,
2140 .unmap_page = arm_iommu_unmap_page,
2141 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2142 .sync_single_for_device = arm_iommu_sync_single_for_device,
2143
2144 .map_sg = arm_iommu_map_sg,
2145 .unmap_sg = arm_iommu_unmap_sg,
2146 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2147 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002148
2149 .map_resource = arm_iommu_map_resource,
2150 .unmap_resource = arm_iommu_unmap_resource,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002151
2152 .mapping_error = arm_dma_mapping_error,
Christoph Hellwig418a7a72017-05-22 11:20:18 +02002153 .dma_supported = arm_dma_supported,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002154};
2155
Bart Van Assche52997092017-01-20 13:04:01 -08002156const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002157 .alloc = arm_coherent_iommu_alloc_attrs,
2158 .free = arm_coherent_iommu_free_attrs,
2159 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002160 .get_sgtable = arm_iommu_get_sgtable,
2161
2162 .map_page = arm_coherent_iommu_map_page,
2163 .unmap_page = arm_coherent_iommu_unmap_page,
2164
2165 .map_sg = arm_coherent_iommu_map_sg,
2166 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002167
2168 .map_resource = arm_iommu_map_resource,
2169 .unmap_resource = arm_iommu_unmap_resource,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002170
2171 .mapping_error = arm_dma_mapping_error,
Christoph Hellwig418a7a72017-05-22 11:20:18 +02002172 .dma_supported = arm_dma_supported,
Rob Herring0fa478d2012-08-21 12:23:23 +02002173};
2174
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002175/**
2176 * arm_iommu_create_mapping
2177 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2178 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002179 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002180 *
2181 * Creates a mapping structure which holds information about used/unused
2182 * IO address ranges, which is required to perform memory allocation and
2183 * mapping with IOMMU aware functions.
2184 *
2185 * The client device need to be attached to the mapping with
2186 * arm_iommu_attach_device function.
2187 */
2188struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002189arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002190{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002191 unsigned int bits = size >> PAGE_SHIFT;
2192 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002193 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002194 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002195 int err = -ENOMEM;
2196
Marek Szyprowski14245322015-04-29 11:29:19 +01002197 /* currently only 32-bit DMA address space is supported */
2198 if (size > DMA_BIT_MASK(32) + 1)
2199 return ERR_PTR(-ERANGE);
2200
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002201 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002202 return ERR_PTR(-EINVAL);
2203
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002204 if (bitmap_size > PAGE_SIZE) {
2205 extensions = bitmap_size / PAGE_SIZE;
2206 bitmap_size = PAGE_SIZE;
2207 }
2208
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002209 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2210 if (!mapping)
2211 goto err;
2212
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002213 mapping->bitmap_size = bitmap_size;
2214 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002215 GFP_KERNEL);
2216 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002217 goto err2;
2218
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002219 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002220 if (!mapping->bitmaps[0])
2221 goto err3;
2222
2223 mapping->nr_bitmaps = 1;
2224 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002225 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002226 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002227
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002228 spin_lock_init(&mapping->lock);
2229
2230 mapping->domain = iommu_domain_alloc(bus);
2231 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002232 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002233
2234 kref_init(&mapping->kref);
2235 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002236err4:
2237 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002238err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002239 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002240err2:
2241 kfree(mapping);
2242err:
2243 return ERR_PTR(err);
2244}
Prathyush K18177d12013-01-04 06:22:42 -05002245EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002246
2247static void release_iommu_mapping(struct kref *kref)
2248{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002249 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002250 struct dma_iommu_mapping *mapping =
2251 container_of(kref, struct dma_iommu_mapping, kref);
2252
2253 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002254 for (i = 0; i < mapping->nr_bitmaps; i++)
2255 kfree(mapping->bitmaps[i]);
2256 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002257 kfree(mapping);
2258}
2259
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002260static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2261{
2262 int next_bitmap;
2263
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002264 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002265 return -EINVAL;
2266
2267 next_bitmap = mapping->nr_bitmaps;
2268 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2269 GFP_ATOMIC);
2270 if (!mapping->bitmaps[next_bitmap])
2271 return -ENOMEM;
2272
2273 mapping->nr_bitmaps++;
2274
2275 return 0;
2276}
2277
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002278void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2279{
2280 if (mapping)
2281 kref_put(&mapping->kref, release_iommu_mapping);
2282}
Prathyush K18177d12013-01-04 06:22:42 -05002283EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002284
Laurent Pincharteab8d652015-01-23 16:21:49 +02002285static int __arm_iommu_attach_device(struct device *dev,
2286 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002287{
2288 int err;
2289
2290 err = iommu_attach_device(mapping->domain, dev);
2291 if (err)
2292 return err;
2293
2294 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002295 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002296
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002297 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002298 return 0;
2299}
2300
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002301/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002302 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002303 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002304 * @mapping: io address space mapping structure (returned from
2305 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002306 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002307 * Attaches specified io address space mapping to the provided device.
2308 * This replaces the dma operations (dma_map_ops pointer) with the
2309 * IOMMU aware version.
2310 *
2311 * More than one client might be attached to the same io address space
2312 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002313 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002314int arm_iommu_attach_device(struct device *dev,
2315 struct dma_iommu_mapping *mapping)
2316{
2317 int err;
2318
2319 err = __arm_iommu_attach_device(dev, mapping);
2320 if (err)
2321 return err;
2322
2323 set_dma_ops(dev, &iommu_ops);
2324 return 0;
2325}
2326EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2327
Sricharan Rd3e01c52017-05-27 19:17:45 +05302328/**
2329 * arm_iommu_detach_device
2330 * @dev: valid struct device pointer
2331 *
2332 * Detaches the provided device from a previously attached map.
2333 * This voids the dma operations (dma_map_ops pointer)
2334 */
2335void arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002336{
2337 struct dma_iommu_mapping *mapping;
2338
2339 mapping = to_dma_iommu_mapping(dev);
2340 if (!mapping) {
2341 dev_warn(dev, "Not attached\n");
2342 return;
2343 }
2344
2345 iommu_detach_device(mapping->domain, dev);
2346 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002347 to_dma_iommu_mapping(dev) = NULL;
Sricharan Rd3e01c52017-05-27 19:17:45 +05302348 set_dma_ops(dev, NULL);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002349
2350 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2351}
Prathyush K18177d12013-01-04 06:22:42 -05002352EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002353
Bart Van Assche52997092017-01-20 13:04:01 -08002354static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002355{
2356 return coherent ? &iommu_coherent_ops : &iommu_ops;
2357}
2358
2359static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002360 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002361{
2362 struct dma_iommu_mapping *mapping;
2363
2364 if (!iommu)
2365 return false;
2366
2367 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2368 if (IS_ERR(mapping)) {
2369 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2370 size, dev_name(dev));
2371 return false;
2372 }
2373
Laurent Pincharteab8d652015-01-23 16:21:49 +02002374 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002375 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2376 dev_name(dev));
2377 arm_iommu_release_mapping(mapping);
2378 return false;
2379 }
2380
2381 return true;
2382}
2383
2384static void arm_teardown_iommu_dma_ops(struct device *dev)
2385{
Will Deacon89cfdb12015-01-16 18:02:15 +01002386 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002387
Will Deaconc2273a12015-01-16 18:01:43 +01002388 if (!mapping)
2389 return;
2390
Sricharan Rd3e01c52017-05-27 19:17:45 +05302391 arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002392 arm_iommu_release_mapping(mapping);
2393}
2394
2395#else
2396
2397static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002398 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002399{
2400 return false;
2401}
2402
2403static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2404
2405#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2406
2407#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2408
Bart Van Assche52997092017-01-20 13:04:01 -08002409static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002410{
2411 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2412}
2413
2414void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002415 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002416{
Bart Van Assche52997092017-01-20 13:04:01 -08002417 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002418
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002419 dev->archdata.dma_coherent = coherent;
Laurent Pinchart26b37b92015-05-15 02:00:02 +03002420
2421 /*
2422 * Don't override the dma_ops if they have already been set. Ideally
2423 * this should be the only location where dma_ops are set, remove this
2424 * check when all other callers of set_dma_ops will have disappeared.
2425 */
2426 if (dev->dma_ops)
2427 return;
2428
Will Deacon4bb25782014-08-27 17:52:44 +01002429 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2430 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2431 else
2432 dma_ops = arm_get_dma_map_ops(coherent);
2433
2434 set_dma_ops(dev, dma_ops);
Stefano Stabellinie0586322017-04-13 14:04:21 -07002435
2436#ifdef CONFIG_XEN
2437 if (xen_initial_domain()) {
2438 dev->archdata.dev_dma_ops = dev->dma_ops;
2439 dev->dma_ops = xen_dma_ops;
2440 }
2441#endif
Laurent Pincharta93a1212017-05-27 19:17:43 +05302442 dev->archdata.dma_ops_setup = true;
Will Deacon4bb25782014-08-27 17:52:44 +01002443}
2444
2445void arch_teardown_dma_ops(struct device *dev)
2446{
Laurent Pincharta93a1212017-05-27 19:17:43 +05302447 if (!dev->archdata.dma_ops_setup)
2448 return;
2449
Will Deacon4bb25782014-08-27 17:52:44 +01002450 arm_teardown_iommu_dma_ops(dev);
2451}