blob: baf63ea1e630747d2d5558675db12fbfa6a5a99d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0ddbccd2008-09-25 15:59:19 +01002 * linux/arch/arm/mm/dma-mapping.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000-2004 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * DMA uncached mapping support.
11 */
Russell King11a5aa32013-11-25 21:52:25 +000012#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/mm.h>
Laura Abbott36d0fd22014-10-09 15:26:42 -070015#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/list.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010022#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050023#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010024#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010025#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020026#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020027#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020028#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010029#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070030#include <linux/cma.h>
Laura Abbott29defcc2014-08-01 16:13:40 -070031#include <linux/msm_dma_iommu_mapping.h>
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +053032#include <linux/dma-mapping-fast.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010034#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040035#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010038#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020039#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010040#include <asm/mach/map.h>
41#include <asm/system_info.h>
42#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Russell King1234e3f2015-07-24 09:10:55 +010044#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010045#include "mm.h"
46
Rabin Vincentb4268672016-03-03 15:58:01 +010047struct arm_dma_alloc_args {
48 struct device *dev;
49 size_t size;
50 gfp_t gfp;
51 pgprot_t prot;
52 const void *caller;
53 bool want_vaddr;
Taniya Das31a03832014-06-12 19:42:06 +053054 bool skip_cpu_sync;
55 bool skip_zeroing;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010056 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010057};
58
59struct arm_dma_free_args {
60 struct device *dev;
61 size_t size;
62 void *cpu_addr;
63 struct page *page;
64 bool want_vaddr;
65};
66
Gregory CLEMENTf1270892016-04-15 11:15:18 +010067#define NORMAL 0
68#define COHERENT 1
69
Rabin Vincentb4268672016-03-03 15:58:01 +010070struct arm_dma_allocator {
71 void *(*alloc)(struct arm_dma_alloc_args *args,
72 struct page **ret_page);
73 void (*free)(struct arm_dma_free_args *args);
74};
75
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010076struct arm_dma_buffer {
77 struct list_head list;
78 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010079 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010080};
81
82static LIST_HEAD(arm_dma_bufs);
83static DEFINE_SPINLOCK(arm_dma_bufs_lock);
84
85static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
86{
87 struct arm_dma_buffer *buf, *found = NULL;
88 unsigned long flags;
89
90 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
91 list_for_each_entry(buf, &arm_dma_bufs, list) {
92 if (buf->virt == virt) {
93 list_del(&buf->list);
94 found = buf;
95 break;
96 }
97 }
98 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
99 return found;
100}
101
Marek Szyprowski15237e12012-02-10 19:55:20 +0100102/*
103 * The DMA API is built upon the notion of "buffer ownership". A buffer
104 * is either exclusively owned by the CPU (and therefore may be accessed
105 * by it) or exclusively owned by the DMA device. These helper functions
106 * represent the transitions between these two ownership states.
107 *
108 * Note, however, that on later ARMs, this notion does not work due to
109 * speculative prefetches. We model our approach on the assumption that
110 * the CPU does do speculative prefetches, which means we clean caches
111 * before transfers and delay cache invalidation until transfer completion.
112 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100113 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100114static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100115 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100116static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100117 size_t, enum dma_data_direction);
118
Laura Abbott4a45a332014-08-05 19:16:28 -0700119static void *
120__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
121 const void *caller);
122
123static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn);
124
125static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot);
126
127static void *arm_dma_remap(struct device *dev, void *cpu_addr,
128 dma_addr_t handle, size_t size,
129 unsigned long attrs);
130
131static void arm_dma_unremap(struct device *dev, void *remapped_addr,
132 size_t size);
133
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100134/**
135 * arm_dma_map_page - map a portion of a page for streaming DMA
136 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
137 * @page: page that buffer resides in
138 * @offset: offset into page for start of buffer
139 * @size: size of buffer to map
140 * @dir: DMA transfer direction
141 *
142 * Ensure that any data held in the cache is appropriately discarded
143 * or written back.
144 *
145 * The device owns this memory once this call has completed. The CPU
146 * can regain ownership by calling dma_unmap_page().
147 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100148static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100149 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700150 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100151{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700152 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100153 __dma_page_cpu_to_dev(page, offset, size, dir);
154 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100155}
156
Rob Herringdd37e942012-08-21 12:20:17 +0200157static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
158 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700159 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200160{
161 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
162}
163
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100164/**
165 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
166 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
167 * @handle: DMA address of buffer
168 * @size: size of buffer (same as passed to dma_map_page)
169 * @dir: DMA transfer direction (same as passed to dma_map_page)
170 *
171 * Unmap a page streaming mode DMA translation. The handle and size
172 * must match what was provided in the previous dma_map_page() call.
173 * All other usages are undefined.
174 *
175 * After this call, reads by the CPU to the buffer are guaranteed to see
176 * whatever the device wrote there.
177 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100178static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700179 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100180{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700181 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100182 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
183 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100184}
185
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100186static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100187 dma_addr_t handle, size_t size, enum dma_data_direction dir)
188{
189 unsigned int offset = handle & (PAGE_SIZE - 1);
190 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200191 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100192}
193
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100194static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100195 dma_addr_t handle, size_t size, enum dma_data_direction dir)
196{
197 unsigned int offset = handle & (PAGE_SIZE - 1);
198 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200199 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100200}
201
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100202struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200203 .alloc = arm_dma_alloc,
204 .free = arm_dma_free,
205 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200206 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100207 .map_page = arm_dma_map_page,
208 .unmap_page = arm_dma_unmap_page,
209 .map_sg = arm_dma_map_sg,
210 .unmap_sg = arm_dma_unmap_sg,
211 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
212 .sync_single_for_device = arm_dma_sync_single_for_device,
213 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
214 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Laura Abbott4a45a332014-08-05 19:16:28 -0700215 .remap = arm_dma_remap,
216 .unremap = arm_dma_unremap,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100217};
218EXPORT_SYMBOL(arm_dma_ops);
219
Rob Herringdd37e942012-08-21 12:20:17 +0200220static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700221 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200222static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700223 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100224static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
225 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700226 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200227
228struct dma_map_ops arm_coherent_dma_ops = {
229 .alloc = arm_coherent_dma_alloc,
230 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100231 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200232 .get_sgtable = arm_dma_get_sgtable,
233 .map_page = arm_coherent_dma_map_page,
234 .map_sg = arm_dma_map_sg,
Rob Herringdd37e942012-08-21 12:20:17 +0200235};
236EXPORT_SYMBOL(arm_coherent_dma_ops);
237
Russell King9f28cde2013-12-06 12:30:42 +0000238static int __dma_supported(struct device *dev, u64 mask, bool warn)
239{
240 unsigned long max_dma_pfn;
241
242 /*
243 * If the mask allows for more memory than we can address,
244 * and we actually have that much memory, then we must
245 * indicate that DMA to this device is not supported.
246 */
247 if (sizeof(mask) != sizeof(dma_addr_t) &&
248 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000249 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000250 if (warn) {
251 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
252 mask);
253 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
254 }
255 return 0;
256 }
257
258 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
259
260 /*
261 * Translate the device's DMA mask to a PFN limit. This
262 * PFN number includes the page which we can DMA to.
263 */
264 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
265 if (warn)
266 dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
267 mask,
268 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
269 max_dma_pfn + 1);
270 return 0;
271 }
272
273 return 1;
274}
275
Catalin Marinasab6494f2009-07-24 12:35:02 +0100276static u64 get_coherent_dma_mask(struct device *dev)
277{
Russell King4dcfa602013-07-09 12:14:49 +0100278 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Catalin Marinasab6494f2009-07-24 12:35:02 +0100280 if (dev) {
281 mask = dev->coherent_dma_mask;
282
283 /*
284 * Sanity check the DMA mask - it must be non-zero, and
285 * must be able to be satisfied by a DMA allocation.
286 */
287 if (mask == 0) {
288 dev_warn(dev, "coherent DMA mask is unset\n");
289 return 0;
290 }
291
Russell King9f28cde2013-12-06 12:30:42 +0000292 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100293 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100294 }
295
296 return mask;
297}
298
Taniya Das31a03832014-06-12 19:42:06 +0530299static void __dma_clear_buffer(struct page *page, size_t size,
300 bool skip_zeroing, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100301{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100302 /*
303 * Ensure that the allocated pages are zeroed, and that any data
304 * lurking in the kernel direct-mapped region is invalidated.
305 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100306 if (PageHighMem(page)) {
307 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
308 phys_addr_t end = base + size;
309 while (size > 0) {
310 void *ptr = kmap_atomic(page);
Taniya Das31a03832014-06-12 19:42:06 +0530311 if (!skip_zeroing)
312 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100313 if (coherent_flag != COHERENT)
314 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100315 kunmap_atomic(ptr);
316 page++;
317 size -= PAGE_SIZE;
318 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100319 if (coherent_flag != COHERENT)
320 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100321 } else {
322 void *ptr = page_address(page);
Taniya Das31a03832014-06-12 19:42:06 +0530323 if (!skip_zeroing)
324 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100325 if (coherent_flag != COHERENT) {
326 dmac_flush_range(ptr, ptr + size);
327 outer_flush_range(__pa(ptr), __pa(ptr) + size);
328 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200329 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100330}
331
Russell King7a9a32a2009-11-19 15:31:07 +0000332/*
333 * Allocate a DMA buffer for 'dev' of size 'size' using the
334 * specified gfp mask. Note that 'size' must be page aligned.
335 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100336static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
337 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000338{
339 unsigned long order = get_order(size);
340 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000341
342 page = alloc_pages(gfp, order);
343 if (!page)
344 return NULL;
345
346 /*
347 * Now split the huge page and free the excess pages
348 */
349 split_page(page, order);
350 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
351 __free_page(p);
352
Taniya Das31a03832014-06-12 19:42:06 +0530353 __dma_clear_buffer(page, size, false, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000354
355 return page;
356}
357
358/*
359 * Free a DMA buffer. 'size' must be page aligned.
360 */
361static void __dma_free_buffer(struct page *page, size_t size)
362{
363 struct page *e = page + (size >> PAGE_SHIFT);
364
365 while (page < e) {
366 __free_page(page);
367 page++;
368 }
369}
370
Catalin Marinasab6494f2009-07-24 12:35:02 +0100371#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Marek Szyprowskic7909502011-12-29 13:09:51 +0100373static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100374 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100375 const void *caller, bool want_vaddr,
Taniya Das31a03832014-06-12 19:42:06 +0530376 bool skip_cpu_sync, bool skip_zeroing,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100377 int coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100378
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200379static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
380 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100381 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200382
383static void *
384__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
385 const void *caller)
386{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200387 /*
388 * DMA allocation can be mapped to user space, so lets
389 * set VM_USERMAP flags too.
390 */
Laura Abbott513510d2014-10-09 15:26:40 -0700391 return dma_common_contiguous_remap(page, size,
392 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
393 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200394}
395
Laura Abbott172b6f02013-01-16 18:23:19 -0800396static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200397{
Laura Abbott513510d2014-10-09 15:26:40 -0700398 dma_common_free_remap(cpu_addr, size,
Laura Abbott172b6f02013-01-16 18:23:19 -0800399 VM_ARM_DMA_CONSISTENT | VM_USERMAP, no_warn);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200400}
401
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200402#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700403static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200404
Laura Abbott36d0fd22014-10-09 15:26:42 -0700405static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100406
407static int __init early_coherent_pool(char *p)
408{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700409 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100410 return 0;
411}
412early_param("coherent_pool", early_coherent_pool);
413
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200414void __init init_dma_coherent_pool_size(unsigned long size)
415{
416 /*
417 * Catch any attempt to set the pool size too late.
418 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700419 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200420
421 /*
422 * Set architecture specific coherent pool size only if
423 * it has not been changed by kernel command line parameter.
424 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700425 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
426 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200427}
428
Marek Szyprowskic7909502011-12-29 13:09:51 +0100429/*
430 * Initialise the coherent pool for atomic allocations.
431 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200432static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100433{
Russell King71b55662013-11-25 12:01:03 +0000434 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100435 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100436 struct page *page;
437 void *ptr;
438
Laura Abbott36d0fd22014-10-09 15:26:42 -0700439 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
440 if (!atomic_pool)
441 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100442 /*
443 * The atomic pool is only used for non-coherent allocations
444 * so we must pass NORMAL for coherent_flag.
445 */
Gioh Kime464ef12014-05-22 13:38:23 +0900446 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700447 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Taniya Das31a03832014-06-12 19:42:06 +0530448 &page, atomic_pool_init, true, false,
449 false, NORMAL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200450 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700451 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100452 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100453 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700454 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300455
Laura Abbott36d0fd22014-10-09 15:26:42 -0700456 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
457 page_to_phys(page),
458 atomic_pool_size, -1);
459 if (ret)
460 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300461
Laura Abbott36d0fd22014-10-09 15:26:42 -0700462 gen_pool_set_algo(atomic_pool,
463 gen_pool_first_fit_order_align,
464 (void *)PAGE_SHIFT);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100465 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700466 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100467 return 0;
468 }
Sachin Kamatec106652012-09-24 08:35:03 +0200469
Laura Abbott36d0fd22014-10-09 15:26:42 -0700470destroy_genpool:
471 gen_pool_destroy(atomic_pool);
472 atomic_pool = NULL;
473out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100474 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700475 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100476 return -ENOMEM;
477}
478/*
479 * CMA is activated by core_initcall, so we must be called after it.
480 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200481postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100482
483struct dma_contig_early_reserve {
484 phys_addr_t base;
485 unsigned long size;
486};
487
488static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
489
490static int dma_mmu_remap_num __initdata;
491
492void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
493{
494 dma_mmu_remap[dma_mmu_remap_num].base = base;
495 dma_mmu_remap[dma_mmu_remap_num].size = size;
496 dma_mmu_remap_num++;
497}
498
499void __init dma_contiguous_remap(void)
500{
501 int i;
502 for (i = 0; i < dma_mmu_remap_num; i++) {
503 phys_addr_t start = dma_mmu_remap[i].base;
504 phys_addr_t end = start + dma_mmu_remap[i].size;
505 struct map_desc map;
506 unsigned long addr;
507
Shiraz Hashim956128d2015-09-12 11:50:24 +0530508 /*
509 * Make start and end PMD_SIZE aligned, observing memory
510 * boundaries
511 */
512 if (memblock_is_memory(start & PMD_MASK))
513 start = start & PMD_MASK;
514 if (memblock_is_memory(ALIGN(end, PMD_SIZE)))
515 end = ALIGN(end, PMD_SIZE);
516
Marek Szyprowskic7909502011-12-29 13:09:51 +0100517 if (end > arm_lowmem_limit)
518 end = arm_lowmem_limit;
519 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200520 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100521
522 map.pfn = __phys_to_pfn(start);
523 map.virtual = __phys_to_virt(start);
524 map.length = end - start;
525 map.type = MT_MEMORY_DMA_READY;
526
527 /*
Russell King6b076992014-07-17 12:17:45 +0100528 * Clear previous low-memory mapping to ensure that the
529 * TLB does not see any conflicting entries, then flush
530 * the TLB of the old entries before creating new mappings.
531 *
532 * This ensures that any speculatively loaded TLB entries
533 * (even though they may be rare) can not cause any problems,
534 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100535 */
536 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Shiraz Hashim956128d2015-09-12 11:50:24 +0530537 addr += PMD_SIZE) {
538 pmd_t *pmd;
539
540 pmd = pmd_off_k(addr);
541 if (pmd_bad(*pmd))
542 pmd_clear(pmd);
543 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100544
Russell King6b076992014-07-17 12:17:45 +0100545 flush_tlb_kernel_range(__phys_to_virt(start),
546 __phys_to_virt(end));
547
Marek Szyprowskic7909502011-12-29 13:09:51 +0100548 iotable_init(&map, 1);
549 }
550}
551
Marek Szyprowskic7909502011-12-29 13:09:51 +0100552static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
553 void *data)
554{
555 struct page *page = virt_to_page(addr);
556 pgprot_t prot = *(pgprot_t *)data;
557
558 set_pte_ext(pte, mk_pte(page, prot), 0);
559 return 0;
560}
561
Patrick Dalyca238022016-02-17 15:58:22 -0800562static int __dma_clear_pte(pte_t *pte, pgtable_t token, unsigned long addr,
563 void *data)
564{
565 pte_clear(&init_mm, addr, pte);
566 return 0;
567}
568
569static void __dma_remap(struct page *page, size_t size, pgprot_t prot,
570 bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100571{
572 unsigned long start = (unsigned long) page_address(page);
573 unsigned end = start + size;
Patrick Dalyca238022016-02-17 15:58:22 -0800574 int (*func)(pte_t *pte, pgtable_t token, unsigned long addr,
575 void *data);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100576
Patrick Dalyca238022016-02-17 15:58:22 -0800577 if (!want_vaddr)
578 func = __dma_clear_pte;
579 else
580 func = __dma_update_pte;
581
582 apply_to_page_range(&init_mm, start, size, func, &prot);
583 mb(); /*Ensure pte's are updated */
Marek Szyprowskic7909502011-12-29 13:09:51 +0100584 flush_tlb_kernel_range(start, end);
585}
586
Laura Abbott172b6f02013-01-16 18:23:19 -0800587
588#define NO_KERNEL_MAPPING_DUMMY 0x2222
Marek Szyprowskic7909502011-12-29 13:09:51 +0100589static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
590 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100591 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100592{
593 struct page *page;
Laura Abbott172b6f02013-01-16 18:23:19 -0800594 void *ptr = (void *)NO_KERNEL_MAPPING_DUMMY;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100595 /*
596 * __alloc_remap_buffer is only called when the device is
597 * non-coherent
598 */
599 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100600 if (!page)
601 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100602 if (!want_vaddr)
603 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100604
605 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
606 if (!ptr) {
607 __dma_free_buffer(page, size);
608 return NULL;
609 }
610
Carlo Caione6e8266e2015-02-09 10:38:35 +0100611 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100612 *ret_page = page;
613 return ptr;
614}
615
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200616static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100617{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700618 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200619 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100620
Laura Abbott36d0fd22014-10-09 15:26:42 -0700621 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200622 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100623 return NULL;
624 }
625
Laura Abbott36d0fd22014-10-09 15:26:42 -0700626 val = gen_pool_alloc(atomic_pool, size);
627 if (val) {
628 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200629
Laura Abbott36d0fd22014-10-09 15:26:42 -0700630 *ret_page = phys_to_page(phys);
631 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100632 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200633
634 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100635}
636
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300637static bool __in_atomic_pool(void *start, size_t size)
638{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700639 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300640}
641
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200642static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100643{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300644 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100645 return 0;
646
Laura Abbott36d0fd22014-10-09 15:26:42 -0700647 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200648
Marek Szyprowskic7909502011-12-29 13:09:51 +0100649 return 1;
650}
651
652static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100653 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100654 const void *caller, bool want_vaddr,
Taniya Das31a03832014-06-12 19:42:06 +0530655 bool skip_cpu_sync, bool skip_zeroing,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100656 int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100657{
658 unsigned long order = get_order(size);
659 size_t count = size >> PAGE_SHIFT;
660 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100661 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100662
663 page = dma_alloc_from_contiguous(dev, count, order);
664 if (!page)
665 return NULL;
666
Taniya Das31a03832014-06-12 19:42:06 +0530667 /*
668 * skip completely if we neither need to zero nor sync.
669 */
670 if (!(skip_cpu_sync && skip_zeroing))
671 __dma_clear_buffer(page, size, skip_zeroing, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100672
Marek Szyprowski9848e482013-01-16 15:38:44 +0100673 if (PageHighMem(page)) {
Laura Abbott172b6f02013-01-16 18:23:19 -0800674 if (!want_vaddr) {
675 /*
676 * Something non-NULL needs to be returned here. Give
677 * back a dummy address that is unmapped to catch
678 * clients trying to use the address incorrectly
679 */
680 ptr = (void *)NO_KERNEL_MAPPING_DUMMY;
Shiraz Hashima279aaa2015-06-25 16:45:24 +0530681
682 /* also flush out the stale highmem mappings */
683 kmap_flush_unused();
684 kmap_atomic_flush_unused();
Laura Abbott172b6f02013-01-16 18:23:19 -0800685 } else {
686 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot,
687 caller);
688 if (!ptr) {
689 dma_release_from_contiguous(dev, page, count);
690 return NULL;
691 }
Marek Szyprowski9848e482013-01-16 15:38:44 +0100692 }
693 } else {
Patrick Dalyca238022016-02-17 15:58:22 -0800694 __dma_remap(page, size, prot, want_vaddr);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100695 ptr = page_address(page);
696 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100697
Marek Szyprowskic7909502011-12-29 13:09:51 +0100698 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100699 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100700}
701
702static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100703 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100704{
Patrick Dalyca238022016-02-17 15:58:22 -0800705 if (PageHighMem(page))
Laura Abbott172b6f02013-01-16 18:23:19 -0800706 __dma_free_remap(cpu_addr, size, true);
Patrick Dalyca238022016-02-17 15:58:22 -0800707 else
708 __dma_remap(page, size, PAGE_KERNEL, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100709 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
710}
711
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700712static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200713{
Laura Abbott6d225fd2012-10-29 11:54:38 -0700714 if (attrs & DMA_ATTR_WRITE_COMBINE)
715 prot = pgprot_writecombine(prot);
716 else if (attrs & DMA_ATTR_STRONGLY_ORDERED)
717 prot = pgprot_stronglyordered(prot);
718 /* if non-consistent just pass back what was given */
719 else if ((attrs & DMA_ATTR_NON_CONSISTENT) == 0)
720 prot = pgprot_dmacoherent(prot);
721
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200722 return prot;
723}
724
Marek Szyprowskic7909502011-12-29 13:09:51 +0100725#define nommu() 0
726
Catalin Marinasab6494f2009-07-24 12:35:02 +0100727#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000728
Marek Szyprowskic7909502011-12-29 13:09:51 +0100729#define nommu() 1
730
Carlo Caione6e8266e2015-02-09 10:38:35 +0100731#define __get_dma_pgprot(attrs, prot) __pgprot(0)
732#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200733#define __alloc_from_pool(size, ret_page) NULL
Taniya Das31a03832014-06-12 19:42:06 +0530734#define __alloc_from_contiguous(dev, size, prot, ret, c, \
735 wv, scs, sz, coherent_flag) NULL
Rabin Vincentb4268672016-03-03 15:58:01 +0100736#define __free_from_pool(cpu_addr, size) do { } while (0)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100737#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Laura Abbott172b6f02013-01-16 18:23:19 -0800738#define __dma_free_remap(cpu_addr, size, w) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000739
740#endif /* CONFIG_MMU */
741
Marek Szyprowskic7909502011-12-29 13:09:51 +0100742static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
743 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100744{
Russell King04da5692009-11-19 15:54:45 +0000745 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100746 /* __alloc_simple_buffer is only called when the device is coherent */
747 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100748 if (!page)
749 return NULL;
750
751 *ret_page = page;
752 return page_address(page);
753}
754
Rabin Vincentb4268672016-03-03 15:58:01 +0100755static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
756 struct page **ret_page)
757{
758 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
759 ret_page);
760}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100761
Rabin Vincentb4268672016-03-03 15:58:01 +0100762static void simple_allocator_free(struct arm_dma_free_args *args)
763{
764 __dma_free_buffer(args->page, args->size);
765}
766
767static struct arm_dma_allocator simple_allocator = {
768 .alloc = simple_allocator_alloc,
769 .free = simple_allocator_free,
770};
771
772static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
773 struct page **ret_page)
774{
775 return __alloc_from_contiguous(args->dev, args->size, args->prot,
776 ret_page, args->caller,
Taniya Das31a03832014-06-12 19:42:06 +0530777 args->want_vaddr, args->skip_cpu_sync,
778 args->skip_zeroing, args->coherent_flag);
Rabin Vincentb4268672016-03-03 15:58:01 +0100779}
780
781static void cma_allocator_free(struct arm_dma_free_args *args)
782{
783 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
784 args->size, args->want_vaddr);
785}
786
787static struct arm_dma_allocator cma_allocator = {
788 .alloc = cma_allocator_alloc,
789 .free = cma_allocator_free,
790};
791
792static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
793 struct page **ret_page)
794{
795 return __alloc_from_pool(args->size, ret_page);
796}
797
798static void pool_allocator_free(struct arm_dma_free_args *args)
799{
800 __free_from_pool(args->cpu_addr, args->size);
801}
802
803static struct arm_dma_allocator pool_allocator = {
804 .alloc = pool_allocator_alloc,
805 .free = pool_allocator_free,
806};
807
808static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
809 struct page **ret_page)
810{
811 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
812 args->prot, ret_page, args->caller,
813 args->want_vaddr);
814}
815
816static void remap_allocator_free(struct arm_dma_free_args *args)
817{
818 if (args->want_vaddr)
Laura Abbott172b6f02013-01-16 18:23:19 -0800819 __dma_free_remap(args->cpu_addr, args->size, false);
Rabin Vincentb4268672016-03-03 15:58:01 +0100820
821 __dma_free_buffer(args->page, args->size);
822}
823
824static struct arm_dma_allocator remap_allocator = {
825 .alloc = remap_allocator_alloc,
826 .free = remap_allocator_free,
827};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100828
829static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100830 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700831 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100832{
833 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900834 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000835 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100836 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100837 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100838 struct arm_dma_alloc_args args = {
839 .dev = dev,
840 .size = PAGE_ALIGN(size),
841 .gfp = gfp,
842 .prot = prot,
843 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700844 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Taniya Das31a03832014-06-12 19:42:06 +0530845 .skip_cpu_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC),
846 .skip_zeroing = (attrs & DMA_ATTR_SKIP_ZEROING),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100847 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100848 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100849
Marek Szyprowskic7909502011-12-29 13:09:51 +0100850#ifdef CONFIG_DMA_API_DEBUG
851 u64 limit = (mask + 1) & ~mask;
852 if (limit && size >= limit) {
853 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
854 size, mask);
855 return NULL;
856 }
857#endif
858
859 if (!mask)
860 return NULL;
861
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100862 buf = kzalloc(sizeof(*buf),
863 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100864 if (!buf)
865 return NULL;
866
Marek Szyprowskic7909502011-12-29 13:09:51 +0100867 if (mask < 0xffffffffULL)
868 gfp |= GFP_DMA;
869
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100870 /*
871 * Following is a work-around (a.k.a. hack) to prevent pages
872 * with __GFP_COMP being passed to split_page() which cannot
873 * handle them. The real problem is that this flag probably
874 * should be 0 on ARM as it is not supported on this
875 * platform; see CONFIG_HUGETLBFS.
876 */
877 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100878 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100879
Marek Szyprowski553ac782012-02-29 14:45:28 +0100880 *handle = DMA_ERROR_CODE;
Rabin Vincentb4268672016-03-03 15:58:01 +0100881 allowblock = gfpflags_allow_blocking(gfp);
882 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000883
Rabin Vincentb4268672016-03-03 15:58:01 +0100884 if (cma)
885 buf->allocator = &cma_allocator;
886 else if (nommu() || is_coherent)
887 buf->allocator = &simple_allocator;
888 else if (allowblock)
889 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000890 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100891 buf->allocator = &pool_allocator;
892
893 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000894
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100895 if (page) {
896 unsigned long flags;
897
Russell King9eedd962011-01-03 00:00:17 +0000898 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100899 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100900
901 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
902 list_add(&buf->list, &arm_dma_bufs);
903 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
904 } else {
905 kfree(buf);
906 }
Russell King31ebf942009-11-19 21:12:17 +0000907
Laura Abbott172b6f02013-01-16 18:23:19 -0800908 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100909}
Russell King695ae0a2009-11-19 16:31:39 +0000910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/*
912 * Allocate DMA-coherent memory space and return both the kernel remapped
913 * virtual and bus address for that space.
914 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200915void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700916 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Russell King0ea1ec72013-10-23 16:14:59 +0100918 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400919
Rob Herringdd37e942012-08-21 12:20:17 +0200920 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100921 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200922}
923
924static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700925 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200926{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100927 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100928 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Mike Looijmans55af8a92015-06-03 11:25:31 +0100931static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200932 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700933 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100935 int ret = -ENXIO;
936#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200937 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
938 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100939 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200940 unsigned long off = vma->vm_pgoff;
941
Marek Szyprowski47142f02012-05-15 19:04:13 +0200942 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
943 return ret;
944
Marek Szyprowski50262a42012-07-30 09:35:26 +0200945 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
946 ret = remap_pfn_range(vma, vma->vm_start,
947 pfn + off,
948 vma->vm_end - vma->vm_start,
949 vma->vm_page_prot);
950 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100951#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 return ret;
954}
955
Laura Abbott4a45a332014-08-05 19:16:28 -0700956static void *arm_dma_remap(struct device *dev, void *cpu_addr,
957 dma_addr_t handle, size_t size,
958 unsigned long attrs)
959{
960 void *ptr;
961 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
962 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
963 unsigned long offset = handle & ~PAGE_MASK;
964
965 size = PAGE_ALIGN(size + offset);
966 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot,
967 __builtin_return_address(0));
968 return ptr ? ptr + offset : ptr;
969}
970
971static void arm_dma_unremap(struct device *dev, void *remapped_addr,
972 size_t size)
973{
974 unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
975 struct vm_struct *area;
976
Shiraz Hashim9e6d9842016-11-03 20:44:14 +0530977 size = PAGE_ALIGN(size);
Laura Abbott4a45a332014-08-05 19:16:28 -0700978 remapped_addr = (void *)((unsigned long)remapped_addr & PAGE_MASK);
979
980 area = find_vm_area(remapped_addr);
981 if (!area || (area->flags & flags) != flags) {
982 WARN(1, "trying to free invalid coherent area: %p\n",
983 remapped_addr);
984 return;
985 }
986
987 vunmap(remapped_addr);
Vinayak Menonf806a8e2016-03-11 18:24:49 +0530988 flush_tlb_kernel_range((unsigned long)remapped_addr,
989 (unsigned long)(remapped_addr + size));
Laura Abbott4a45a332014-08-05 19:16:28 -0700990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100992 * Create userspace mapping for the DMA-coherent memory.
993 */
994static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
995 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700996 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100997{
998 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
999}
1000
1001int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
1002 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001003 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +01001004{
1005#ifdef CONFIG_MMU
1006 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1007#endif /* CONFIG_MMU */
1008 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
1009}
1010
1011/*
Marek Szyprowskic7909502011-12-29 13:09:51 +01001012 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 */
Rob Herringdd37e942012-08-21 12:20:17 +02001014static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001015 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +02001016 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Marek Szyprowskic7909502011-12-29 13:09:51 +01001018 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001019 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +01001020 struct arm_dma_free_args args = {
1021 .dev = dev,
1022 .size = PAGE_ALIGN(size),
1023 .cpu_addr = cpu_addr,
1024 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001025 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +01001026 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001027
1028 buf = arm_dma_buffer_find(cpu_addr);
1029 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
1030 return;
Russell King5edf71a2005-11-25 15:52:51 +00001031
Rabin Vincentb4268672016-03-03 15:58:01 +01001032 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001033 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
Russell Kingafd1a322008-09-25 16:30:57 +01001035
Rob Herringdd37e942012-08-21 12:20:17 +02001036void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001037 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +02001038{
1039 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
1040}
1041
1042static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001043 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +02001044{
1045 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
1046}
1047
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001048int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
1049 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001050 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001051{
1052 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
1053 int ret;
1054
1055 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1056 if (unlikely(ret))
1057 return ret;
1058
1059 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1060 return 0;
1061}
1062
Russell King65af1912009-11-24 17:53:33 +00001063static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +00001064 size_t size, enum dma_data_direction dir,
1065 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +00001066{
Russell King15653372013-01-19 11:05:57 +00001067 unsigned long pfn;
1068 size_t left = size;
1069
1070 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
1071 offset %= PAGE_SIZE;
1072
Russell King65af1912009-11-24 17:53:33 +00001073 /*
1074 * A single sg entry may refer to multiple physically contiguous
1075 * pages. But we still need to process highmem pages individually.
1076 * If highmem is not configured then the bulk of this loop gets
1077 * optimized out.
1078 */
Russell King65af1912009-11-24 17:53:33 +00001079 do {
1080 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +00001081 void *vaddr;
1082
Russell King15653372013-01-19 11:05:57 +00001083 page = pfn_to_page(pfn);
1084
Russell King93f1d622009-11-24 14:41:01 +00001085 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +00001086 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +00001087 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001088
1089 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001090 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +01001091 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001092 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001093 } else {
1094 vaddr = kmap_high_get(page);
1095 if (vaddr) {
1096 op(vaddr + offset, len, dir);
1097 kunmap_high(page);
1098 }
Russell King93f1d622009-11-24 14:41:01 +00001099 }
1100 } else {
1101 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +00001102 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +00001103 }
Russell King65af1912009-11-24 17:53:33 +00001104 offset = 0;
Russell King15653372013-01-19 11:05:57 +00001105 pfn++;
Russell King65af1912009-11-24 17:53:33 +00001106 left -= len;
1107 } while (left);
1108}
1109
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001110/*
1111 * Make an area consistent for devices.
1112 * Note: Drivers should NOT use this function directly, as it will break
1113 * platforms with CONFIG_DMABOUNCE.
1114 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
1115 */
1116static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +00001117 size_t size, enum dma_data_direction dir)
1118{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001119 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -04001120
Russell Kinga9c91472009-11-26 16:19:58 +00001121 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -04001122
Russell King65af1912009-11-24 17:53:33 +00001123 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001124 if (dir == DMA_FROM_DEVICE) {
1125 outer_inv_range(paddr, paddr + size);
1126 } else {
1127 outer_clean_range(paddr, paddr + size);
1128 }
1129 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -04001130}
Russell King4ea0d732009-11-24 16:27:17 +00001131
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001132static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001133 size_t size, enum dma_data_direction dir)
1134{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001135 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001136
1137 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001138 /* in any case, don't bother invalidating if DMA to device */
1139 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001140 outer_inv_range(paddr, paddr + size);
1141
Russell Kingdeace4a2014-05-03 11:06:55 +01001142 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1143 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001144
1145 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001146 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001147 */
Ming Leib2a234e2013-05-18 11:21:36 +01001148 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1149 unsigned long pfn;
1150 size_t left = size;
1151
1152 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1153 off %= PAGE_SIZE;
1154 if (off) {
1155 pfn++;
1156 left -= PAGE_SIZE - off;
1157 }
1158 while (left >= PAGE_SIZE) {
1159 page = pfn_to_page(pfn++);
1160 set_bit(PG_dcache_clean, &page->flags);
1161 left -= PAGE_SIZE;
1162 }
1163 }
Russell King4ea0d732009-11-24 16:27:17 +00001164}
Nicolas Pitre43377452009-03-12 22:52:09 -04001165
Russell Kingafd1a322008-09-25 16:30:57 +01001166/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001167 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001168 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1169 * @sg: list of buffers
1170 * @nents: number of buffers to map
1171 * @dir: DMA transfer direction
1172 *
1173 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1174 * This is the scatter-gather version of the dma_map_single interface.
1175 * Here the scatter gather list elements are each tagged with the
1176 * appropriate dma address and length. They are obtained via
1177 * sg_dma_{address,length}.
1178 *
1179 * Device ownership issues as mentioned for dma_map_single are the same
1180 * here.
1181 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001182int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001183 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001184{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001185 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001186 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001187 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001188
1189 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001190#ifdef CONFIG_NEED_SG_DMA_LENGTH
1191 s->dma_length = s->length;
1192#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001193 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1194 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001195 if (dma_mapping_error(dev, s->dma_address))
1196 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001197 }
Russell Kingafd1a322008-09-25 16:30:57 +01001198 return nents;
Russell King01135d922008-09-25 21:05:02 +01001199
1200 bad_mapping:
1201 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001202 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001203 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001204}
Russell Kingafd1a322008-09-25 16:30:57 +01001205
1206/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001207 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001208 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1209 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001210 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001211 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1212 *
1213 * Unmap a set of streaming mode DMA translations. Again, CPU access
1214 * rules concerning calls here are the same as for dma_unmap_single().
1215 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001216void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001217 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001218{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001219 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001220 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001221
Russell King01135d922008-09-25 21:05:02 +01001222 int i;
Russell King24056f52011-01-03 11:29:28 +00001223
Russell King01135d922008-09-25 21:05:02 +01001224 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001225 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001226}
Russell Kingafd1a322008-09-25 16:30:57 +01001227
1228/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001229 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001230 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1231 * @sg: list of buffers
1232 * @nents: number of buffers to map (returned from dma_map_sg)
1233 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1234 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001235void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001236 int nents, enum dma_data_direction dir)
1237{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001238 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001239 struct scatterlist *s;
1240 int i;
1241
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001242 for_each_sg(sg, s, nents, i)
1243 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1244 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001245}
Russell Kingafd1a322008-09-25 16:30:57 +01001246
1247/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001248 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001249 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1250 * @sg: list of buffers
1251 * @nents: number of buffers to map (returned from dma_map_sg)
1252 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1253 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001254void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001255 int nents, enum dma_data_direction dir)
1256{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001257 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001258 struct scatterlist *s;
1259 int i;
1260
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001261 for_each_sg(sg, s, nents, i)
1262 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1263 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001264}
Russell King24056f52011-01-03 11:29:28 +00001265
Russell King022ae532011-07-08 21:26:59 +01001266/*
1267 * Return whether the given device DMA address mask can be supported
1268 * properly. For example, if your device can only drive the low 24-bits
1269 * during bus mastering, then you would pass 0x00ffffff as the mask
1270 * to this function.
1271 */
1272int dma_supported(struct device *dev, u64 mask)
1273{
Russell King9f28cde2013-12-06 12:30:42 +00001274 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001275}
1276EXPORT_SYMBOL(dma_supported);
1277
Russell King24056f52011-01-03 11:29:28 +00001278#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1279
1280static int __init dma_debug_do_init(void)
1281{
1282 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1283 return 0;
1284}
Marek Szyprowski256ff1c2016-11-15 14:00:53 +01001285core_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001286
1287#ifdef CONFIG_ARM_DMA_USE_IOMMU
1288
1289/* IOMMU */
1290
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001291static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1292
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001293static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1294 size_t size)
1295{
1296 unsigned int order = get_order(size);
1297 unsigned int align = 0;
1298 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301299 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001300 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001301 dma_addr_t iova;
1302 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001303
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001304 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1305 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1306
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001307 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1308 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001309
1310 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001311 for (i = 0; i < mapping->nr_bitmaps; i++) {
1312 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1313 mapping->bits, 0, count, align);
1314
1315 if (start > mapping->bits)
1316 continue;
1317
1318 bitmap_set(mapping->bitmaps[i], start, count);
1319 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001320 }
1321
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001322 /*
1323 * No unused range found. Try to extend the existing mapping
1324 * and perform a second attempt to reserve an IO virtual
1325 * address range of size bytes.
1326 */
1327 if (i == mapping->nr_bitmaps) {
1328 if (extend_iommu_mapping(mapping)) {
1329 spin_unlock_irqrestore(&mapping->lock, flags);
1330 return DMA_ERROR_CODE;
1331 }
1332
1333 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1334 mapping->bits, 0, count, align);
1335
1336 if (start > mapping->bits) {
1337 spin_unlock_irqrestore(&mapping->lock, flags);
1338 return DMA_ERROR_CODE;
1339 }
1340
1341 bitmap_set(mapping->bitmaps[i], start, count);
1342 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001343 spin_unlock_irqrestore(&mapping->lock, flags);
1344
Ritesh Harjani006f8412014-05-20 10:02:59 +05301345 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001346 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001347
1348 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001349}
1350
1351static inline void __free_iova(struct dma_iommu_mapping *mapping,
1352 dma_addr_t addr, size_t size)
1353{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001354 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301355 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001356 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001357 dma_addr_t bitmap_base;
1358 u32 bitmap_index;
1359
1360 if (!size)
1361 return;
1362
Ritesh Harjani006f8412014-05-20 10:02:59 +05301363 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001364 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1365
Ritesh Harjani006f8412014-05-20 10:02:59 +05301366 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001367
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001368 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001369
Ritesh Harjani006f8412014-05-20 10:02:59 +05301370 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001371 /*
1372 * The address range to be freed reaches into the iova
1373 * range of the next bitmap. This should not happen as
1374 * we don't allow this in __alloc_iova (at the
1375 * moment).
1376 */
1377 BUG();
1378 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001379 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001380
1381 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001382 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001383 spin_unlock_irqrestore(&mapping->lock, flags);
1384}
1385
Doug Anderson33298ef2016-01-29 23:06:08 +01001386/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1387static const int iommu_order_array[] = { 9, 8, 4, 0 };
1388
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001389static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001390 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001391 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001392{
1393 struct page **pages;
Shiraz Hashim3340eeb2016-02-21 12:45:34 +05301394 size_t count = size >> PAGE_SHIFT;
1395 size_t array_size = count * sizeof(struct page *);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001396 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001397 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001398
1399 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001400 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001401 else
1402 pages = vzalloc(array_size);
1403 if (!pages)
1404 return NULL;
1405
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001406 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001407 {
1408 unsigned long order = get_order(size);
1409 struct page *page;
1410
1411 page = dma_alloc_from_contiguous(dev, count, order);
1412 if (!page)
1413 goto error;
1414
Taniya Das31a03832014-06-12 19:42:06 +05301415 __dma_clear_buffer(page, size, false, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001416
1417 for (i = 0; i < count; i++)
1418 pages[i] = page + i;
1419
1420 return pages;
1421 }
1422
Doug Anderson14d3ae22016-01-29 23:08:46 +01001423 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001424 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001425 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1426
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001427 /*
1428 * IOMMU can map any pages, so himem can also be used here
1429 */
1430 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1431
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001432 while (count) {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001433 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001434
Doug Anderson33298ef2016-01-29 23:06:08 +01001435 order = iommu_order_array[order_idx];
1436
1437 /* Drop down when we get small */
1438 if (__fls(count) < order) {
1439 order_idx++;
1440 continue;
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001441 }
1442
Doug Anderson33298ef2016-01-29 23:06:08 +01001443 if (order) {
1444 /* See if it's easy to allocate a high-order chunk */
1445 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1446
1447 /* Go down a notch at first sign of pressure */
1448 if (!pages[i]) {
1449 order_idx++;
1450 continue;
1451 }
1452 } else {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001453 pages[i] = alloc_pages(gfp, 0);
1454 if (!pages[i])
1455 goto error;
1456 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001457
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001458 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001459 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001460 j = 1 << order;
1461 while (--j)
1462 pages[i + j] = pages[i] + j;
1463 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001464
Taniya Das31a03832014-06-12 19:42:06 +05301465 __dma_clear_buffer(pages[i], PAGE_SIZE << order,
1466 false, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001467 i += 1 << order;
1468 count -= 1 << order;
1469 }
1470
1471 return pages;
1472error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001473 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001474 if (pages[i])
1475 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001476 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001477 return NULL;
1478}
1479
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001480static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001481 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001482{
1483 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001484 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001485
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001486 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001487 dma_release_from_contiguous(dev, pages[0], count);
1488 } else {
1489 for (i = 0; i < count; i++)
1490 if (pages[i])
1491 __free_pages(pages[i], 0);
1492 }
1493
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001494 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001495 return 0;
1496}
1497
1498/*
1499 * Create a CPU mapping for a specified pages
1500 */
1501static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001502__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1503 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001504{
Laura Abbott513510d2014-10-09 15:26:40 -07001505 return dma_common_pages_remap(pages, size,
1506 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001507}
1508
1509/*
1510 * Create a mapping in device IO address space for specified pages
1511 */
1512static dma_addr_t
1513__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1514{
Will Deacon89cfdb12015-01-16 18:02:15 +01001515 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001516 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1517 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001518 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001519
1520 dma_addr = __alloc_iova(mapping, size);
1521 if (dma_addr == DMA_ERROR_CODE)
1522 return dma_addr;
1523
1524 iova = dma_addr;
1525 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001526 int ret;
1527
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001528 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1529 phys_addr_t phys = page_to_phys(pages[i]);
1530 unsigned int len, j;
1531
1532 for (j = i + 1; j < count; j++, next_pfn++)
1533 if (page_to_pfn(pages[j]) != next_pfn)
1534 break;
1535
1536 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001537 ret = iommu_map(mapping->domain, iova, phys, len,
1538 IOMMU_READ|IOMMU_WRITE);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001539 if (ret < 0)
1540 goto fail;
1541 iova += len;
1542 i = j;
1543 }
1544 return dma_addr;
1545fail:
1546 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1547 __free_iova(mapping, dma_addr, size);
1548 return DMA_ERROR_CODE;
1549}
1550
1551static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1552{
Will Deacon89cfdb12015-01-16 18:02:15 +01001553 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001554
1555 /*
1556 * add optional in-page offset from iova to size and align
1557 * result to page size
1558 */
1559 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1560 iova &= PAGE_MASK;
1561
1562 iommu_unmap(mapping->domain, iova, size);
1563 __free_iova(mapping, iova, size);
1564 return 0;
1565}
1566
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001567static struct page **__atomic_get_pages(void *addr)
1568{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001569 struct page *page;
1570 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001571
Laura Abbott36d0fd22014-10-09 15:26:42 -07001572 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1573 page = phys_to_page(phys);
1574
1575 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001576}
1577
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001578static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001579{
1580 struct vm_struct *area;
1581
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001582 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1583 return __atomic_get_pages(cpu_addr);
1584
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001585 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001586 return cpu_addr;
1587
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001588 area = find_vm_area(cpu_addr);
1589 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1590 return area->pages;
1591 return NULL;
1592}
1593
Gregory CLEMENT56506822016-04-15 11:20:13 +01001594static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
1595 dma_addr_t *handle, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001596{
1597 struct page *page;
1598 void *addr;
1599
Gregory CLEMENT56506822016-04-15 11:20:13 +01001600 if (coherent_flag == COHERENT)
1601 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1602 else
1603 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001604 if (!addr)
1605 return NULL;
1606
1607 *handle = __iommu_create_mapping(dev, &page, size);
1608 if (*handle == DMA_ERROR_CODE)
1609 goto err_mapping;
1610
1611 return addr;
1612
1613err_mapping:
1614 __free_from_pool(addr, size);
1615 return NULL;
1616}
1617
Marek Szyprowskid5898292013-02-08 10:54:48 +01001618static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001619 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001620{
1621 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001622 if (coherent_flag == COHERENT)
1623 __dma_free_buffer(virt_to_page(cpu_addr), size);
1624 else
1625 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001626}
1627
Gregory CLEMENT56506822016-04-15 11:20:13 +01001628static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001629 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001630 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001631{
Russell King71b55662013-11-25 12:01:03 +00001632 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001633 struct page **pages;
1634 void *addr = NULL;
1635
1636 *handle = DMA_ERROR_CODE;
1637 size = PAGE_ALIGN(size);
1638
Gregory CLEMENT56506822016-04-15 11:20:13 +01001639 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1640 return __iommu_alloc_simple(dev, size, gfp, handle,
1641 coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001642
Richard Zhao5b91a982013-06-20 20:31:00 +08001643 /*
1644 * Following is a work-around (a.k.a. hack) to prevent pages
1645 * with __GFP_COMP being passed to split_page() which cannot
1646 * handle them. The real problem is that this flag probably
1647 * should be 0 on ARM as it is not supported on this
1648 * platform; see CONFIG_HUGETLBFS.
1649 */
1650 gfp &= ~(__GFP_COMP);
1651
Gregory CLEMENT56506822016-04-15 11:20:13 +01001652 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001653 if (!pages)
1654 return NULL;
1655
1656 *handle = __iommu_create_mapping(dev, pages, size);
1657 if (*handle == DMA_ERROR_CODE)
1658 goto err_buffer;
1659
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001660 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001661 return pages;
1662
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001663 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1664 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001665 if (!addr)
1666 goto err_mapping;
1667
1668 return addr;
1669
1670err_mapping:
1671 __iommu_remove_mapping(dev, *handle, size);
1672err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001673 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001674 return NULL;
1675}
1676
Gregory CLEMENT56506822016-04-15 11:20:13 +01001677static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001678 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001679{
1680 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1681}
1682
1683static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001684 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001685{
1686 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1687}
1688
1689static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001690 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001691 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001692{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001693 unsigned long uaddr = vma->vm_start;
1694 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001695 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001696 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1697 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001698
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001699 if (!pages)
1700 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001701
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001702 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1703 return -ENXIO;
1704
Marek Szyprowski7e312102015-08-28 09:42:09 +01001705 pages += off;
1706
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001707 do {
1708 int ret = vm_insert_page(vma, uaddr, *pages++);
1709 if (ret) {
1710 pr_err("Remapping memory failed: %d\n", ret);
1711 return ret;
1712 }
1713 uaddr += PAGE_SIZE;
1714 usize -= PAGE_SIZE;
1715 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001716
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001717 return 0;
1718}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001719static int arm_iommu_mmap_attrs(struct device *dev,
1720 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001721 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001722{
1723 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1724
1725 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1726}
1727
1728static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1729 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001730 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001731{
1732 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1733}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001734
1735/*
1736 * free a page as defined by the above mapping.
1737 * Must not be called with IRQs disabled.
1738 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001739void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001740 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001741{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001742 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001743 size = PAGE_ALIGN(size);
1744
Gregory CLEMENT56506822016-04-15 11:20:13 +01001745 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1746 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001747 return;
1748 }
1749
YoungJun Cho836bfa02013-06-17 13:18:52 +09001750 pages = __iommu_get_pages(cpu_addr, attrs);
1751 if (!pages) {
1752 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1753 return;
1754 }
1755
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001756 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001757 dma_common_free_remap(cpu_addr, size,
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001758 VM_ARM_DMA_CONSISTENT | VM_USERMAP, false);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001759 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001760
1761 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001762 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001763}
1764
Gregory CLEMENT56506822016-04-15 11:20:13 +01001765void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001766 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001767{
1768 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1769}
1770
1771void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001772 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001773{
1774 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1775}
1776
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001777static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1778 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001779 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001780{
1781 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1782 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1783
1784 if (!pages)
1785 return -ENXIO;
1786
1787 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1788 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001789}
1790
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001791static int __dma_direction_to_prot(enum dma_data_direction dir)
1792{
1793 int prot;
1794
1795 switch (dir) {
1796 case DMA_BIDIRECTIONAL:
1797 prot = IOMMU_READ | IOMMU_WRITE;
1798 break;
1799 case DMA_TO_DEVICE:
1800 prot = IOMMU_READ;
1801 break;
1802 case DMA_FROM_DEVICE:
1803 prot = IOMMU_WRITE;
1804 break;
1805 default:
1806 prot = 0;
1807 }
1808
1809 return prot;
1810}
1811
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001812/*
1813 * Map a part of the scatter-gather list into contiguous io address space
1814 */
1815static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1816 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001817 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001818 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001819{
Will Deacon89cfdb12015-01-16 18:02:15 +01001820 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001821 dma_addr_t iova, iova_base;
1822 int ret = 0;
1823 unsigned int count;
1824 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001825 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001826
1827 size = PAGE_ALIGN(size);
1828 *handle = DMA_ERROR_CODE;
1829
1830 iova_base = iova = __alloc_iova(mapping, size);
1831 if (iova == DMA_ERROR_CODE)
1832 return -ENOMEM;
1833
1834 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001835 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001836 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1837
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001838 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001839 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1840
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001841 prot = __dma_direction_to_prot(dir);
1842
1843 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001844 if (ret < 0)
1845 goto fail;
1846 count += len >> PAGE_SHIFT;
1847 iova += len;
1848 }
1849 *handle = iova_base;
1850
1851 return 0;
1852fail:
1853 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1854 __free_iova(mapping, iova_base, size);
1855 return ret;
1856}
1857
Rob Herring0fa478d2012-08-21 12:23:23 +02001858static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001859 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001860 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001861{
1862 struct scatterlist *s = sg, *dma = sg, *start = sg;
1863 int i, count = 0;
1864 unsigned int offset = s->offset;
1865 unsigned int size = s->offset + s->length;
1866 unsigned int max = dma_get_max_seg_size(dev);
1867
1868 for (i = 1; i < nents; i++) {
1869 s = sg_next(s);
1870
1871 s->dma_address = DMA_ERROR_CODE;
1872 s->dma_length = 0;
1873
1874 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1875 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001876 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001877 goto bad_mapping;
1878
1879 dma->dma_address += offset;
1880 dma->dma_length = size - offset;
1881
1882 size = offset = s->offset;
1883 start = s;
1884 dma = sg_next(dma);
1885 count += 1;
1886 }
1887 size += s->length;
1888 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001889 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1890 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001891 goto bad_mapping;
1892
1893 dma->dma_address += offset;
1894 dma->dma_length = size - offset;
1895
1896 return count+1;
1897
1898bad_mapping:
1899 for_each_sg(sg, s, count, i)
1900 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1901 return 0;
1902}
1903
1904/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001905 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1906 * @dev: valid struct device pointer
1907 * @sg: list of buffers
1908 * @nents: number of buffers to map
1909 * @dir: DMA transfer direction
1910 *
1911 * Map a set of i/o coherent buffers described by scatterlist in streaming
1912 * mode for DMA. The scatter gather list elements are merged together (if
1913 * possible) and tagged with the appropriate dma address and length. They are
1914 * obtained via sg_dma_{address,length}.
1915 */
1916int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001917 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001918{
1919 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1920}
1921
1922/**
1923 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1924 * @dev: valid struct device pointer
1925 * @sg: list of buffers
1926 * @nents: number of buffers to map
1927 * @dir: DMA transfer direction
1928 *
1929 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1930 * The scatter gather list elements are merged together (if possible) and
1931 * tagged with the appropriate dma address and length. They are obtained via
1932 * sg_dma_{address,length}.
1933 */
1934int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001935 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001936{
Chintan Pandyad091ab72015-05-26 17:59:11 +05301937 struct scatterlist *s;
1938 int i;
1939 size_t ret;
1940 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1941 unsigned int total_length = 0, current_offset = 0;
1942 dma_addr_t iova;
1943 int prot = __dma_direction_to_prot(dir);
1944
1945 for_each_sg(sg, s, nents, i)
1946 total_length += s->length;
1947
1948 iova = __alloc_iova(mapping, total_length);
1949 ret = iommu_map_sg(mapping->domain, iova, sg, nents, prot);
1950 if (ret != total_length) {
1951 __free_iova(mapping, iova, total_length);
1952 return 0;
1953 }
1954
1955 for_each_sg(sg, s, nents, i) {
1956 s->dma_address = iova + current_offset;
1957 s->dma_length = total_length - current_offset;
1958 current_offset += s->length;
1959 }
1960
1961 return nents;
Rob Herring0fa478d2012-08-21 12:23:23 +02001962}
1963
1964static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001965 int nents, enum dma_data_direction dir,
1966 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001967{
1968 struct scatterlist *s;
1969 int i;
1970
1971 for_each_sg(sg, s, nents, i) {
1972 if (sg_dma_len(s))
1973 __iommu_remove_mapping(dev, sg_dma_address(s),
1974 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001975 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001976 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1977 s->length, dir);
1978 }
1979}
1980
1981/**
1982 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1983 * @dev: valid struct device pointer
1984 * @sg: list of buffers
1985 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1986 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1987 *
1988 * Unmap a set of streaming mode DMA translations. Again, CPU access
1989 * rules concerning calls here are the same as for dma_unmap_single().
1990 */
1991void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001992 int nents, enum dma_data_direction dir,
1993 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001994{
1995 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1996}
1997
1998/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001999 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
2000 * @dev: valid struct device pointer
2001 * @sg: list of buffers
2002 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
2003 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2004 *
2005 * Unmap a set of streaming mode DMA translations. Again, CPU access
2006 * rules concerning calls here are the same as for dma_unmap_single().
2007 */
2008void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002009 enum dma_data_direction dir,
2010 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002011{
Chintan Pandyad091ab72015-05-26 17:59:11 +05302012 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
2013 unsigned int total_length = sg_dma_len(sg);
2014 dma_addr_t iova = sg_dma_address(sg);
2015
2016 total_length = PAGE_ALIGN((iova & ~PAGE_MASK) + total_length);
2017 iova &= PAGE_MASK;
2018
Charan Teja Reddy498cf262016-10-24 20:12:33 +05302019 iommu_unmap(mapping->domain, iova, total_length);
Chintan Pandyad091ab72015-05-26 17:59:11 +05302020 __free_iova(mapping, iova, total_length);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002021}
2022
2023/**
2024 * arm_iommu_sync_sg_for_cpu
2025 * @dev: valid struct device pointer
2026 * @sg: list of buffers
2027 * @nents: number of buffers to map (returned from dma_map_sg)
2028 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2029 */
2030void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
2031 int nents, enum dma_data_direction dir)
2032{
2033 struct scatterlist *s;
2034 int i;
2035
2036 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02002037 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002038
2039}
2040
2041/**
2042 * arm_iommu_sync_sg_for_device
2043 * @dev: valid struct device pointer
2044 * @sg: list of buffers
2045 * @nents: number of buffers to map (returned from dma_map_sg)
2046 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2047 */
2048void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
2049 int nents, enum dma_data_direction dir)
2050{
2051 struct scatterlist *s;
2052 int i;
2053
2054 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02002055 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002056}
2057
2058
2059/**
Rob Herring0fa478d2012-08-21 12:23:23 +02002060 * arm_coherent_iommu_map_page
2061 * @dev: valid struct device pointer
2062 * @page: page that buffer resides in
2063 * @offset: offset into page for start of buffer
2064 * @size: size of buffer to map
2065 * @dir: DMA transfer direction
2066 *
2067 * Coherent IOMMU aware version of arm_dma_map_page()
2068 */
2069static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
2070 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002071 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002072{
Will Deacon89cfdb12015-01-16 18:02:15 +01002073 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002074 dma_addr_t dma_addr;
Liam Markaeff2c12017-01-26 16:46:14 -08002075 int ret, prot, len, start_offset, map_offset;
2076
2077 map_offset = offset & ~PAGE_MASK;
2078 start_offset = offset & PAGE_MASK;
2079 len = PAGE_ALIGN(map_offset + size);
Rob Herring0fa478d2012-08-21 12:23:23 +02002080
2081 dma_addr = __alloc_iova(mapping, len);
2082 if (dma_addr == DMA_ERROR_CODE)
2083 return dma_addr;
2084
Andreas Herrmannc9b24992013-09-27 00:36:15 +02002085 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01002086
Liam Markaeff2c12017-01-26 16:46:14 -08002087 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page) +
2088 start_offset, len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02002089 if (ret < 0)
2090 goto fail;
2091
Liam Markaeff2c12017-01-26 16:46:14 -08002092 return dma_addr + map_offset;
Rob Herring0fa478d2012-08-21 12:23:23 +02002093fail:
2094 __free_iova(mapping, dma_addr, len);
2095 return DMA_ERROR_CODE;
2096}
2097
2098/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002099 * arm_iommu_map_page
2100 * @dev: valid struct device pointer
2101 * @page: page that buffer resides in
2102 * @offset: offset into page for start of buffer
2103 * @size: size of buffer to map
2104 * @dir: DMA transfer direction
2105 *
2106 * IOMMU aware version of arm_dma_map_page()
2107 */
2108static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
2109 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002110 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002111{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002112 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002113 __dma_page_cpu_to_dev(page, offset, size, dir);
2114
Rob Herring0fa478d2012-08-21 12:23:23 +02002115 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
2116}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002117
Rob Herring0fa478d2012-08-21 12:23:23 +02002118/**
2119 * arm_coherent_iommu_unmap_page
2120 * @dev: valid struct device pointer
2121 * @handle: DMA address of buffer
2122 * @size: size of buffer (same as passed to dma_map_page)
2123 * @dir: DMA transfer direction (same as passed to dma_map_page)
2124 *
2125 * Coherent IOMMU aware version of arm_dma_unmap_page()
2126 */
2127static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002128 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002129{
Will Deacon89cfdb12015-01-16 18:02:15 +01002130 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002131 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02002132 int offset = handle & ~PAGE_MASK;
2133 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002134
Rob Herring0fa478d2012-08-21 12:23:23 +02002135 iommu_unmap(mapping->domain, iova, len);
2136 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002137}
2138
2139/**
2140 * arm_iommu_unmap_page
2141 * @dev: valid struct device pointer
2142 * @handle: DMA address of buffer
2143 * @size: size of buffer (same as passed to dma_map_page)
2144 * @dir: DMA transfer direction (same as passed to dma_map_page)
2145 *
2146 * IOMMU aware version of arm_dma_unmap_page()
2147 */
2148static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002149 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002150{
Will Deacon89cfdb12015-01-16 18:02:15 +01002151 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002152 dma_addr_t iova = handle & PAGE_MASK;
2153 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2154 int offset = handle & ~PAGE_MASK;
2155 int len = PAGE_ALIGN(size + offset);
2156
2157 if (!iova)
2158 return;
2159
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002160 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002161 __dma_page_dev_to_cpu(page, offset, size, dir);
2162
2163 iommu_unmap(mapping->domain, iova, len);
2164 __free_iova(mapping, iova, len);
2165}
2166
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002167/**
2168 * arm_iommu_map_resource - map a device resource for DMA
2169 * @dev: valid struct device pointer
2170 * @phys_addr: physical address of resource
2171 * @size: size of resource to map
2172 * @dir: DMA transfer direction
2173 */
2174static dma_addr_t arm_iommu_map_resource(struct device *dev,
2175 phys_addr_t phys_addr, size_t size,
2176 enum dma_data_direction dir, unsigned long attrs)
2177{
2178 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2179 dma_addr_t dma_addr;
2180 int ret, prot;
2181 phys_addr_t addr = phys_addr & PAGE_MASK;
2182 unsigned int offset = phys_addr & ~PAGE_MASK;
2183 size_t len = PAGE_ALIGN(size + offset);
2184
2185 dma_addr = __alloc_iova(mapping, len);
2186 if (dma_addr == DMA_ERROR_CODE)
2187 return dma_addr;
2188
2189 prot = __dma_direction_to_prot(dir) | IOMMU_MMIO;
2190
2191 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2192 if (ret < 0)
2193 goto fail;
2194
2195 return dma_addr + offset;
2196fail:
2197 __free_iova(mapping, dma_addr, len);
2198 return DMA_ERROR_CODE;
2199}
2200
2201/**
2202 * arm_iommu_unmap_resource - unmap a device DMA resource
2203 * @dev: valid struct device pointer
2204 * @dma_handle: DMA address to resource
2205 * @size: size of resource to map
2206 * @dir: DMA transfer direction
2207 */
2208static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2209 size_t size, enum dma_data_direction dir,
2210 unsigned long attrs)
2211{
2212 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2213 dma_addr_t iova = dma_handle & PAGE_MASK;
2214 unsigned int offset = dma_handle & ~PAGE_MASK;
2215 size_t len = PAGE_ALIGN(size + offset);
2216
2217 if (!iova)
2218 return;
2219
2220 iommu_unmap(mapping->domain, iova, len);
2221 __free_iova(mapping, iova, len);
2222}
2223
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002224static void arm_iommu_sync_single_for_cpu(struct device *dev,
2225 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2226{
Will Deacon89cfdb12015-01-16 18:02:15 +01002227 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002228 dma_addr_t iova = handle & PAGE_MASK;
2229 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2230 unsigned int offset = handle & ~PAGE_MASK;
2231
Rob Herring0fa478d2012-08-21 12:23:23 +02002232 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002233}
2234
2235static void arm_iommu_sync_single_for_device(struct device *dev,
2236 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2237{
Will Deacon89cfdb12015-01-16 18:02:15 +01002238 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002239 dma_addr_t iova = handle & PAGE_MASK;
2240 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2241 unsigned int offset = handle & ~PAGE_MASK;
2242
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002243 __dma_page_cpu_to_dev(page, offset, size, dir);
2244}
2245
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002246const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002247 .alloc = arm_iommu_alloc_attrs,
2248 .free = arm_iommu_free_attrs,
2249 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002250 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002251
2252 .map_page = arm_iommu_map_page,
2253 .unmap_page = arm_iommu_unmap_page,
2254 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2255 .sync_single_for_device = arm_iommu_sync_single_for_device,
2256
2257 .map_sg = arm_iommu_map_sg,
2258 .unmap_sg = arm_iommu_unmap_sg,
2259 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2260 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002261
2262 .map_resource = arm_iommu_map_resource,
2263 .unmap_resource = arm_iommu_unmap_resource,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002264};
2265
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002266const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002267 .alloc = arm_coherent_iommu_alloc_attrs,
2268 .free = arm_coherent_iommu_free_attrs,
2269 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002270 .get_sgtable = arm_iommu_get_sgtable,
2271
2272 .map_page = arm_coherent_iommu_map_page,
2273 .unmap_page = arm_coherent_iommu_unmap_page,
2274
2275 .map_sg = arm_coherent_iommu_map_sg,
2276 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002277
2278 .map_resource = arm_iommu_map_resource,
2279 .unmap_resource = arm_iommu_unmap_resource,
Rob Herring0fa478d2012-08-21 12:23:23 +02002280};
2281
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002282/**
2283 * arm_iommu_create_mapping
2284 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2285 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002286 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002287 *
2288 * Creates a mapping structure which holds information about used/unused
2289 * IO address ranges, which is required to perform memory allocation and
2290 * mapping with IOMMU aware functions.
2291 *
2292 * The client device need to be attached to the mapping with
2293 * arm_iommu_attach_device function.
2294 */
2295struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002296arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002297{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002298 unsigned int bits = size >> PAGE_SHIFT;
2299 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002300 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002301 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002302 int err = -ENOMEM;
2303
Marek Szyprowski14245322015-04-29 11:29:19 +01002304 /* currently only 32-bit DMA address space is supported */
2305 if (size > DMA_BIT_MASK(32) + 1)
2306 return ERR_PTR(-ERANGE);
2307
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002308 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002309 return ERR_PTR(-EINVAL);
2310
Charan Teja Reddy919aec62017-04-18 18:45:19 +05302311 WARN(!IS_ALIGNED(size, SZ_128M),
2312 "size is not aligned to 128M, alignment enforced");
2313
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002314 if (bitmap_size > PAGE_SIZE) {
2315 extensions = bitmap_size / PAGE_SIZE;
2316 bitmap_size = PAGE_SIZE;
2317 }
2318
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002319 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2320 if (!mapping)
2321 goto err;
2322
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002323 mapping->bitmap_size = bitmap_size;
2324 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002325 GFP_KERNEL);
2326 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002327 goto err2;
2328
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002329 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002330 if (!mapping->bitmaps[0])
2331 goto err3;
2332
2333 mapping->nr_bitmaps = 1;
2334 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002335 mapping->base = base;
Charan Teja Reddy919aec62017-04-18 18:45:19 +05302336 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002337
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002338 spin_lock_init(&mapping->lock);
2339
2340 mapping->domain = iommu_domain_alloc(bus);
2341 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002342 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002343
2344 kref_init(&mapping->kref);
2345 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002346err4:
2347 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002348err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002349 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002350err2:
2351 kfree(mapping);
2352err:
2353 return ERR_PTR(err);
2354}
Prathyush K18177d12013-01-04 06:22:42 -05002355EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002356
2357static void release_iommu_mapping(struct kref *kref)
2358{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002359 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002360 struct dma_iommu_mapping *mapping =
2361 container_of(kref, struct dma_iommu_mapping, kref);
2362
2363 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002364 for (i = 0; i < mapping->nr_bitmaps; i++)
2365 kfree(mapping->bitmaps[i]);
2366 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002367 kfree(mapping);
2368}
2369
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002370static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2371{
2372 int next_bitmap;
2373
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002374 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002375 return -EINVAL;
2376
2377 next_bitmap = mapping->nr_bitmaps;
2378 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2379 GFP_ATOMIC);
2380 if (!mapping->bitmaps[next_bitmap])
2381 return -ENOMEM;
2382
2383 mapping->nr_bitmaps++;
2384
2385 return 0;
2386}
2387
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002388void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2389{
2390 if (mapping)
2391 kref_put(&mapping->kref, release_iommu_mapping);
2392}
Prathyush K18177d12013-01-04 06:22:42 -05002393EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002394
Laurent Pincharteab8d652015-01-23 16:21:49 +02002395static int __arm_iommu_attach_device(struct device *dev,
2396 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002397{
2398 int err;
2399
2400 err = iommu_attach_device(mapping->domain, dev);
2401 if (err)
2402 return err;
2403
2404 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002405 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002406
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002407 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002408 return 0;
2409}
2410
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002411/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002412 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002413 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002414 * @mapping: io address space mapping structure (returned from
2415 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002416 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002417 * Attaches specified io address space mapping to the provided device.
2418 * This replaces the dma operations (dma_map_ops pointer) with the
2419 * IOMMU aware version.
2420 *
2421 * More than one client might be attached to the same io address space
2422 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002423 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002424int arm_iommu_attach_device(struct device *dev,
2425 struct dma_iommu_mapping *mapping)
2426{
2427 int err;
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302428 int s1_bypass = 0;
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302429 int is_fast = 0;
2430
2431 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2432 if (is_fast)
2433 return fast_smmu_attach_device(dev, mapping);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002434
2435 err = __arm_iommu_attach_device(dev, mapping);
2436 if (err)
2437 return err;
2438
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302439 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_S1_BYPASS,
2440 &s1_bypass);
2441 if (!s1_bypass)
2442 set_dma_ops(dev, &iommu_ops);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002443 return 0;
2444}
2445EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2446
2447static void __arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002448{
2449 struct dma_iommu_mapping *mapping;
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302450 int is_fast;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002451
2452 mapping = to_dma_iommu_mapping(dev);
2453 if (!mapping) {
2454 dev_warn(dev, "Not attached\n");
2455 return;
2456 }
2457
Laura Abbott29defcc2014-08-01 16:13:40 -07002458 if (msm_dma_unmap_all_for_dev(dev))
2459 dev_warn(dev, "IOMMU detach with outstanding mappings\n");
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302460 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2461 if (is_fast)
2462 return fast_smmu_detach_device(dev, mapping);
Laura Abbott29defcc2014-08-01 16:13:40 -07002463
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002464 iommu_detach_device(mapping->domain, dev);
2465 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002466 to_dma_iommu_mapping(dev) = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002467
2468 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2469}
Laurent Pincharteab8d652015-01-23 16:21:49 +02002470
2471/**
2472 * arm_iommu_detach_device
2473 * @dev: valid struct device pointer
2474 *
2475 * Detaches the provided device from a previously attached map.
2476 * This voids the dma operations (dma_map_ops pointer)
2477 */
2478void arm_iommu_detach_device(struct device *dev)
2479{
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302480 struct dma_iommu_mapping *mapping;
2481 int s1_bypass = 0;
2482
2483 mapping = to_dma_iommu_mapping(dev);
2484 if (!mapping) {
2485 dev_warn(dev, "Not attached\n");
2486 return;
2487 }
2488
Laurent Pincharteab8d652015-01-23 16:21:49 +02002489 __arm_iommu_detach_device(dev);
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302490
2491 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_S1_BYPASS,
2492 &s1_bypass);
2493 if (!s1_bypass)
2494 set_dma_ops(dev, NULL);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002495}
Prathyush K18177d12013-01-04 06:22:42 -05002496EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002497
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002498static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002499{
2500 return coherent ? &iommu_coherent_ops : &iommu_ops;
2501}
2502
2503static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002504 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002505{
2506 struct dma_iommu_mapping *mapping;
2507
2508 if (!iommu)
2509 return false;
2510
2511 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2512 if (IS_ERR(mapping)) {
2513 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2514 size, dev_name(dev));
2515 return false;
2516 }
2517
Laurent Pincharteab8d652015-01-23 16:21:49 +02002518 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002519 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2520 dev_name(dev));
2521 arm_iommu_release_mapping(mapping);
2522 return false;
2523 }
2524
2525 return true;
2526}
2527
2528static void arm_teardown_iommu_dma_ops(struct device *dev)
2529{
Will Deacon89cfdb12015-01-16 18:02:15 +01002530 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002531
Will Deaconc2273a12015-01-16 18:01:43 +01002532 if (!mapping)
2533 return;
2534
Laurent Pincharteab8d652015-01-23 16:21:49 +02002535 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002536 arm_iommu_release_mapping(mapping);
2537}
2538
2539#else
2540
2541static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002542 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002543{
2544 return false;
2545}
2546
2547static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2548
2549#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2550
2551#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2552
2553static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
2554{
2555 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2556}
2557
2558void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002559 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002560{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002561 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002562
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002563 dev->archdata.dma_coherent = coherent;
Will Deacon4bb25782014-08-27 17:52:44 +01002564 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2565 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2566 else
2567 dma_ops = arm_get_dma_map_ops(coherent);
2568
2569 set_dma_ops(dev, dma_ops);
2570}
Runmin Wang8cb38812017-03-24 13:14:41 -07002571EXPORT_SYMBOL(arch_setup_dma_ops);
Will Deacon4bb25782014-08-27 17:52:44 +01002572
2573void arch_teardown_dma_ops(struct device *dev)
2574{
2575 arm_teardown_iommu_dma_ops(dev);
2576}