blob: 9e979622441cfee625e430c9668bb8d0fef76f40 [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>
Charan Teja Reddy19c2f972018-09-11 18:25:30 +053033#include <linux/of.h>
34#include <linux/of_address.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010036#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040037#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010040#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020041#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010042#include <asm/mach/map.h>
43#include <asm/system_info.h>
44#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Russell King1234e3f2015-07-24 09:10:55 +010046#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010047#include "mm.h"
48
Rabin Vincentb4268672016-03-03 15:58:01 +010049struct arm_dma_alloc_args {
50 struct device *dev;
51 size_t size;
52 gfp_t gfp;
53 pgprot_t prot;
54 const void *caller;
55 bool want_vaddr;
Taniya Das31a03832014-06-12 19:42:06 +053056 bool skip_cpu_sync;
57 bool skip_zeroing;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010058 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010059};
60
61struct arm_dma_free_args {
62 struct device *dev;
63 size_t size;
64 void *cpu_addr;
65 struct page *page;
66 bool want_vaddr;
67};
68
Gregory CLEMENTf1270892016-04-15 11:15:18 +010069#define NORMAL 0
70#define COHERENT 1
71
Rabin Vincentb4268672016-03-03 15:58:01 +010072struct arm_dma_allocator {
73 void *(*alloc)(struct arm_dma_alloc_args *args,
74 struct page **ret_page);
75 void (*free)(struct arm_dma_free_args *args);
76};
77
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010078struct arm_dma_buffer {
79 struct list_head list;
80 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010081 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010082};
83
84static LIST_HEAD(arm_dma_bufs);
85static DEFINE_SPINLOCK(arm_dma_bufs_lock);
86
87static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
88{
89 struct arm_dma_buffer *buf, *found = NULL;
90 unsigned long flags;
91
92 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
93 list_for_each_entry(buf, &arm_dma_bufs, list) {
94 if (buf->virt == virt) {
95 list_del(&buf->list);
96 found = buf;
97 break;
98 }
99 }
100 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
101 return found;
102}
103
Marek Szyprowski15237e12012-02-10 19:55:20 +0100104/*
105 * The DMA API is built upon the notion of "buffer ownership". A buffer
106 * is either exclusively owned by the CPU (and therefore may be accessed
107 * by it) or exclusively owned by the DMA device. These helper functions
108 * represent the transitions between these two ownership states.
109 *
110 * Note, however, that on later ARMs, this notion does not work due to
111 * speculative prefetches. We model our approach on the assumption that
112 * the CPU does do speculative prefetches, which means we clean caches
113 * before transfers and delay cache invalidation until transfer completion.
114 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100115 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100116static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100117 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100118static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100119 size_t, enum dma_data_direction);
120
Laura Abbott4a45a332014-08-05 19:16:28 -0700121static void *
122__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
123 const void *caller);
124
125static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn);
126
Charan Teja Reddy19c2f972018-09-11 18:25:30 +0530127static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
128 bool coherent);
Laura Abbott4a45a332014-08-05 19:16:28 -0700129
130static void *arm_dma_remap(struct device *dev, void *cpu_addr,
131 dma_addr_t handle, size_t size,
132 unsigned long attrs);
133
134static void arm_dma_unremap(struct device *dev, void *remapped_addr,
135 size_t size);
136
Charan Teja Reddy19c2f972018-09-11 18:25:30 +0530137
138static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
139 bool coherent)
140{
141 if (attrs & DMA_ATTR_STRONGLY_ORDERED)
142 return pgprot_stronglyordered(prot);
143 else if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
144 return pgprot_writecombine(prot);
145 return prot;
146}
147
148static bool is_dma_coherent(struct device *dev, unsigned long attrs,
149 bool is_coherent)
150{
151 if (attrs & DMA_ATTR_FORCE_COHERENT)
152 is_coherent = true;
153 else if (attrs & DMA_ATTR_FORCE_NON_COHERENT)
154 is_coherent = false;
155 else if (is_device_dma_coherent(dev))
156 is_coherent = true;
157
158 return is_coherent;
159}
160
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100161/**
162 * arm_dma_map_page - map a portion of a page for streaming DMA
163 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
164 * @page: page that buffer resides in
165 * @offset: offset into page for start of buffer
166 * @size: size of buffer to map
167 * @dir: DMA transfer direction
168 *
169 * Ensure that any data held in the cache is appropriately discarded
170 * or written back.
171 *
172 * The device owns this memory once this call has completed. The CPU
173 * can regain ownership by calling dma_unmap_page().
174 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100175static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100176 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700177 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100178{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700179 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100180 __dma_page_cpu_to_dev(page, offset, size, dir);
181 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100182}
183
Rob Herringdd37e942012-08-21 12:20:17 +0200184static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
185 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700186 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200187{
188 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
189}
190
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100191/**
192 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
193 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
194 * @handle: DMA address of buffer
195 * @size: size of buffer (same as passed to dma_map_page)
196 * @dir: DMA transfer direction (same as passed to dma_map_page)
197 *
198 * Unmap a page streaming mode DMA translation. The handle and size
199 * must match what was provided in the previous dma_map_page() call.
200 * All other usages are undefined.
201 *
202 * After this call, reads by the CPU to the buffer are guaranteed to see
203 * whatever the device wrote there.
204 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100205static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700206 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100207{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700208 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100209 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
210 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100211}
212
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100213static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100214 dma_addr_t handle, size_t size, enum dma_data_direction dir)
215{
216 unsigned int offset = handle & (PAGE_SIZE - 1);
217 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200218 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100219}
220
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100221static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100222 dma_addr_t handle, size_t size, enum dma_data_direction dir)
223{
224 unsigned int offset = handle & (PAGE_SIZE - 1);
225 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200226 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100227}
228
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100229struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200230 .alloc = arm_dma_alloc,
231 .free = arm_dma_free,
232 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200233 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100234 .map_page = arm_dma_map_page,
235 .unmap_page = arm_dma_unmap_page,
236 .map_sg = arm_dma_map_sg,
237 .unmap_sg = arm_dma_unmap_sg,
238 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
239 .sync_single_for_device = arm_dma_sync_single_for_device,
240 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
241 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Laura Abbott4a45a332014-08-05 19:16:28 -0700242 .remap = arm_dma_remap,
243 .unremap = arm_dma_unremap,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100244};
245EXPORT_SYMBOL(arm_dma_ops);
246
Rob Herringdd37e942012-08-21 12:20:17 +0200247static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700248 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200249static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700250 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100251static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
252 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700253 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200254
255struct dma_map_ops arm_coherent_dma_ops = {
256 .alloc = arm_coherent_dma_alloc,
257 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100258 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200259 .get_sgtable = arm_dma_get_sgtable,
260 .map_page = arm_coherent_dma_map_page,
261 .map_sg = arm_dma_map_sg,
Rob Herringdd37e942012-08-21 12:20:17 +0200262};
263EXPORT_SYMBOL(arm_coherent_dma_ops);
264
Russell King9f28cde2013-12-06 12:30:42 +0000265static int __dma_supported(struct device *dev, u64 mask, bool warn)
266{
267 unsigned long max_dma_pfn;
268
269 /*
270 * If the mask allows for more memory than we can address,
271 * and we actually have that much memory, then we must
272 * indicate that DMA to this device is not supported.
273 */
274 if (sizeof(mask) != sizeof(dma_addr_t) &&
275 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000276 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000277 if (warn) {
278 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
279 mask);
280 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
281 }
282 return 0;
283 }
284
285 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
286
287 /*
288 * Translate the device's DMA mask to a PFN limit. This
289 * PFN number includes the page which we can DMA to.
290 */
291 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
292 if (warn)
293 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",
294 mask,
295 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
296 max_dma_pfn + 1);
297 return 0;
298 }
299
300 return 1;
301}
302
Catalin Marinasab6494f2009-07-24 12:35:02 +0100303static u64 get_coherent_dma_mask(struct device *dev)
304{
Russell King4dcfa602013-07-09 12:14:49 +0100305 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Catalin Marinasab6494f2009-07-24 12:35:02 +0100307 if (dev) {
308 mask = dev->coherent_dma_mask;
309
310 /*
311 * Sanity check the DMA mask - it must be non-zero, and
312 * must be able to be satisfied by a DMA allocation.
313 */
314 if (mask == 0) {
315 dev_warn(dev, "coherent DMA mask is unset\n");
316 return 0;
317 }
318
Russell King9f28cde2013-12-06 12:30:42 +0000319 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100320 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100321 }
322
323 return mask;
324}
325
Taniya Das31a03832014-06-12 19:42:06 +0530326static void __dma_clear_buffer(struct page *page, size_t size,
327 bool skip_zeroing, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100328{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100329 /*
330 * Ensure that the allocated pages are zeroed, and that any data
331 * lurking in the kernel direct-mapped region is invalidated.
332 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100333 if (PageHighMem(page)) {
334 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
335 phys_addr_t end = base + size;
336 while (size > 0) {
337 void *ptr = kmap_atomic(page);
Taniya Das31a03832014-06-12 19:42:06 +0530338 if (!skip_zeroing)
339 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100340 if (coherent_flag != COHERENT)
341 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100342 kunmap_atomic(ptr);
343 page++;
344 size -= PAGE_SIZE;
345 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100346 if (coherent_flag != COHERENT)
347 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100348 } else {
349 void *ptr = page_address(page);
Taniya Das31a03832014-06-12 19:42:06 +0530350 if (!skip_zeroing)
351 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100352 if (coherent_flag != COHERENT) {
353 dmac_flush_range(ptr, ptr + size);
354 outer_flush_range(__pa(ptr), __pa(ptr) + size);
355 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200356 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100357}
358
Russell King7a9a32a2009-11-19 15:31:07 +0000359/*
360 * Allocate a DMA buffer for 'dev' of size 'size' using the
361 * specified gfp mask. Note that 'size' must be page aligned.
362 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100363static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
364 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000365{
366 unsigned long order = get_order(size);
367 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000368
369 page = alloc_pages(gfp, order);
370 if (!page)
371 return NULL;
372
373 /*
374 * Now split the huge page and free the excess pages
375 */
376 split_page(page, order);
377 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
378 __free_page(p);
379
Taniya Das31a03832014-06-12 19:42:06 +0530380 __dma_clear_buffer(page, size, false, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000381
382 return page;
383}
384
385/*
386 * Free a DMA buffer. 'size' must be page aligned.
387 */
388static void __dma_free_buffer(struct page *page, size_t size)
389{
390 struct page *e = page + (size >> PAGE_SHIFT);
391
392 while (page < e) {
393 __free_page(page);
394 page++;
395 }
396}
397
Catalin Marinasab6494f2009-07-24 12:35:02 +0100398#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Marek Szyprowskic7909502011-12-29 13:09:51 +0100400static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100401 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100402 const void *caller, bool want_vaddr,
Taniya Das31a03832014-06-12 19:42:06 +0530403 bool skip_cpu_sync, bool skip_zeroing,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100404 int coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100405
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200406static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
407 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100408 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200409
410static void *
411__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
412 const void *caller)
413{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200414 /*
415 * DMA allocation can be mapped to user space, so lets
416 * set VM_USERMAP flags too.
417 */
Laura Abbott513510d2014-10-09 15:26:40 -0700418 return dma_common_contiguous_remap(page, size,
419 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
420 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200421}
422
Laura Abbott172b6f02013-01-16 18:23:19 -0800423static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200424{
Laura Abbott513510d2014-10-09 15:26:40 -0700425 dma_common_free_remap(cpu_addr, size,
Laura Abbott172b6f02013-01-16 18:23:19 -0800426 VM_ARM_DMA_CONSISTENT | VM_USERMAP, no_warn);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200427}
428
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200429#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700430static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200431
Laura Abbott36d0fd22014-10-09 15:26:42 -0700432static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100433
434static int __init early_coherent_pool(char *p)
435{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700436 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100437 return 0;
438}
439early_param("coherent_pool", early_coherent_pool);
440
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200441void __init init_dma_coherent_pool_size(unsigned long size)
442{
443 /*
444 * Catch any attempt to set the pool size too late.
445 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700446 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200447
448 /*
449 * Set architecture specific coherent pool size only if
450 * it has not been changed by kernel command line parameter.
451 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700452 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
453 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200454}
455
Marek Szyprowskic7909502011-12-29 13:09:51 +0100456/*
457 * Initialise the coherent pool for atomic allocations.
458 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200459static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100460{
Russell King71b55662013-11-25 12:01:03 +0000461 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100462 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100463 struct page *page;
464 void *ptr;
465
Laura Abbott36d0fd22014-10-09 15:26:42 -0700466 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
467 if (!atomic_pool)
468 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100469 /*
470 * The atomic pool is only used for non-coherent allocations
471 * so we must pass NORMAL for coherent_flag.
472 */
Gioh Kime464ef12014-05-22 13:38:23 +0900473 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700474 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Taniya Das31a03832014-06-12 19:42:06 +0530475 &page, atomic_pool_init, true, false,
476 false, NORMAL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200477 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700478 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100479 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100480 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700481 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300482
Laura Abbott36d0fd22014-10-09 15:26:42 -0700483 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
484 page_to_phys(page),
485 atomic_pool_size, -1);
486 if (ret)
487 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300488
Laura Abbott36d0fd22014-10-09 15:26:42 -0700489 gen_pool_set_algo(atomic_pool,
490 gen_pool_first_fit_order_align,
491 (void *)PAGE_SHIFT);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100492 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700493 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100494 return 0;
495 }
Sachin Kamatec106652012-09-24 08:35:03 +0200496
Laura Abbott36d0fd22014-10-09 15:26:42 -0700497destroy_genpool:
498 gen_pool_destroy(atomic_pool);
499 atomic_pool = NULL;
500out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100501 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700502 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100503 return -ENOMEM;
504}
505/*
506 * CMA is activated by core_initcall, so we must be called after it.
507 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200508postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100509
510struct dma_contig_early_reserve {
511 phys_addr_t base;
512 unsigned long size;
513};
514
515static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
516
517static int dma_mmu_remap_num __initdata;
518
519void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
520{
521 dma_mmu_remap[dma_mmu_remap_num].base = base;
522 dma_mmu_remap[dma_mmu_remap_num].size = size;
523 dma_mmu_remap_num++;
524}
525
526void __init dma_contiguous_remap(void)
527{
528 int i;
529 for (i = 0; i < dma_mmu_remap_num; i++) {
530 phys_addr_t start = dma_mmu_remap[i].base;
531 phys_addr_t end = start + dma_mmu_remap[i].size;
532 struct map_desc map;
533 unsigned long addr;
534
Shiraz Hashim956128d2015-09-12 11:50:24 +0530535 /*
536 * Make start and end PMD_SIZE aligned, observing memory
537 * boundaries
538 */
539 if (memblock_is_memory(start & PMD_MASK))
540 start = start & PMD_MASK;
541 if (memblock_is_memory(ALIGN(end, PMD_SIZE)))
542 end = ALIGN(end, PMD_SIZE);
543
Marek Szyprowskic7909502011-12-29 13:09:51 +0100544 if (end > arm_lowmem_limit)
545 end = arm_lowmem_limit;
546 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200547 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100548
549 map.pfn = __phys_to_pfn(start);
550 map.virtual = __phys_to_virt(start);
551 map.length = end - start;
552 map.type = MT_MEMORY_DMA_READY;
553
554 /*
Russell King6b076992014-07-17 12:17:45 +0100555 * Clear previous low-memory mapping to ensure that the
556 * TLB does not see any conflicting entries, then flush
557 * the TLB of the old entries before creating new mappings.
558 *
559 * This ensures that any speculatively loaded TLB entries
560 * (even though they may be rare) can not cause any problems,
561 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100562 */
563 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Shiraz Hashim956128d2015-09-12 11:50:24 +0530564 addr += PMD_SIZE) {
565 pmd_t *pmd;
566
567 pmd = pmd_off_k(addr);
568 if (pmd_bad(*pmd))
569 pmd_clear(pmd);
570 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100571
Russell King6b076992014-07-17 12:17:45 +0100572 flush_tlb_kernel_range(__phys_to_virt(start),
573 __phys_to_virt(end));
574
Marek Szyprowskic7909502011-12-29 13:09:51 +0100575 iotable_init(&map, 1);
576 }
577}
578
Marek Szyprowskic7909502011-12-29 13:09:51 +0100579static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
580 void *data)
581{
582 struct page *page = virt_to_page(addr);
583 pgprot_t prot = *(pgprot_t *)data;
584
585 set_pte_ext(pte, mk_pte(page, prot), 0);
586 return 0;
587}
588
Patrick Dalyca238022016-02-17 15:58:22 -0800589static int __dma_clear_pte(pte_t *pte, pgtable_t token, unsigned long addr,
590 void *data)
591{
592 pte_clear(&init_mm, addr, pte);
593 return 0;
594}
595
596static void __dma_remap(struct page *page, size_t size, pgprot_t prot,
597 bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100598{
599 unsigned long start = (unsigned long) page_address(page);
600 unsigned end = start + size;
Patrick Dalyca238022016-02-17 15:58:22 -0800601 int (*func)(pte_t *pte, pgtable_t token, unsigned long addr,
602 void *data);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100603
Patrick Dalyca238022016-02-17 15:58:22 -0800604 if (!want_vaddr)
605 func = __dma_clear_pte;
606 else
607 func = __dma_update_pte;
608
609 apply_to_page_range(&init_mm, start, size, func, &prot);
610 mb(); /*Ensure pte's are updated */
Marek Szyprowskic7909502011-12-29 13:09:51 +0100611 flush_tlb_kernel_range(start, end);
612}
613
Laura Abbott172b6f02013-01-16 18:23:19 -0800614
615#define NO_KERNEL_MAPPING_DUMMY 0x2222
Marek Szyprowskic7909502011-12-29 13:09:51 +0100616static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
617 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100618 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100619{
620 struct page *page;
Laura Abbott172b6f02013-01-16 18:23:19 -0800621 void *ptr = (void *)NO_KERNEL_MAPPING_DUMMY;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100622 /*
623 * __alloc_remap_buffer is only called when the device is
624 * non-coherent
625 */
626 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100627 if (!page)
628 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100629 if (!want_vaddr)
630 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100631
632 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
633 if (!ptr) {
634 __dma_free_buffer(page, size);
635 return NULL;
636 }
637
Carlo Caione6e8266e2015-02-09 10:38:35 +0100638 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100639 *ret_page = page;
640 return ptr;
641}
642
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200643static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100644{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700645 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200646 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100647
Laura Abbott36d0fd22014-10-09 15:26:42 -0700648 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200649 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100650 return NULL;
651 }
652
Laura Abbott36d0fd22014-10-09 15:26:42 -0700653 val = gen_pool_alloc(atomic_pool, size);
654 if (val) {
655 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200656
Laura Abbott36d0fd22014-10-09 15:26:42 -0700657 *ret_page = phys_to_page(phys);
658 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100659 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200660
661 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100662}
663
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300664static bool __in_atomic_pool(void *start, size_t size)
665{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700666 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300667}
668
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200669static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100670{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300671 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100672 return 0;
673
Laura Abbott36d0fd22014-10-09 15:26:42 -0700674 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200675
Marek Szyprowskic7909502011-12-29 13:09:51 +0100676 return 1;
677}
678
679static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100680 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100681 const void *caller, bool want_vaddr,
Taniya Das31a03832014-06-12 19:42:06 +0530682 bool skip_cpu_sync, bool skip_zeroing,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100683 int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100684{
685 unsigned long order = get_order(size);
686 size_t count = size >> PAGE_SHIFT;
687 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100688 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100689
690 page = dma_alloc_from_contiguous(dev, count, order);
691 if (!page)
692 return NULL;
693
Taniya Das31a03832014-06-12 19:42:06 +0530694 /*
695 * skip completely if we neither need to zero nor sync.
696 */
697 if (!(skip_cpu_sync && skip_zeroing))
698 __dma_clear_buffer(page, size, skip_zeroing, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100699
Marek Szyprowski9848e482013-01-16 15:38:44 +0100700 if (PageHighMem(page)) {
Laura Abbott172b6f02013-01-16 18:23:19 -0800701 if (!want_vaddr) {
702 /*
703 * Something non-NULL needs to be returned here. Give
704 * back a dummy address that is unmapped to catch
705 * clients trying to use the address incorrectly
706 */
707 ptr = (void *)NO_KERNEL_MAPPING_DUMMY;
Shiraz Hashima279aaa2015-06-25 16:45:24 +0530708
709 /* also flush out the stale highmem mappings */
710 kmap_flush_unused();
711 kmap_atomic_flush_unused();
Laura Abbott172b6f02013-01-16 18:23:19 -0800712 } else {
713 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot,
714 caller);
715 if (!ptr) {
716 dma_release_from_contiguous(dev, page, count);
717 return NULL;
718 }
Marek Szyprowski9848e482013-01-16 15:38:44 +0100719 }
720 } else {
Patrick Dalyca238022016-02-17 15:58:22 -0800721 __dma_remap(page, size, prot, want_vaddr);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100722 ptr = page_address(page);
723 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100724
Marek Szyprowskic7909502011-12-29 13:09:51 +0100725 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100726 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100727}
728
729static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100730 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100731{
Patrick Dalyca238022016-02-17 15:58:22 -0800732 if (PageHighMem(page))
Laura Abbott172b6f02013-01-16 18:23:19 -0800733 __dma_free_remap(cpu_addr, size, true);
Patrick Dalyca238022016-02-17 15:58:22 -0800734 else
735 __dma_remap(page, size, PAGE_KERNEL, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100736 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
737}
738
739#define nommu() 0
740
Catalin Marinasab6494f2009-07-24 12:35:02 +0100741#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000742
Marek Szyprowskic7909502011-12-29 13:09:51 +0100743#define nommu() 1
744
Carlo Caione6e8266e2015-02-09 10:38:35 +0100745#define __get_dma_pgprot(attrs, prot) __pgprot(0)
746#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200747#define __alloc_from_pool(size, ret_page) NULL
Taniya Das31a03832014-06-12 19:42:06 +0530748#define __alloc_from_contiguous(dev, size, prot, ret, c, \
749 wv, scs, sz, coherent_flag) NULL
Rabin Vincentb4268672016-03-03 15:58:01 +0100750#define __free_from_pool(cpu_addr, size) do { } while (0)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100751#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Laura Abbott172b6f02013-01-16 18:23:19 -0800752#define __dma_free_remap(cpu_addr, size, w) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000753
754#endif /* CONFIG_MMU */
755
Marek Szyprowskic7909502011-12-29 13:09:51 +0100756static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
757 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100758{
Russell King04da5692009-11-19 15:54:45 +0000759 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100760 /* __alloc_simple_buffer is only called when the device is coherent */
761 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100762 if (!page)
763 return NULL;
764
765 *ret_page = page;
766 return page_address(page);
767}
768
Rabin Vincentb4268672016-03-03 15:58:01 +0100769static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
770 struct page **ret_page)
771{
772 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
773 ret_page);
774}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100775
Rabin Vincentb4268672016-03-03 15:58:01 +0100776static void simple_allocator_free(struct arm_dma_free_args *args)
777{
778 __dma_free_buffer(args->page, args->size);
779}
780
781static struct arm_dma_allocator simple_allocator = {
782 .alloc = simple_allocator_alloc,
783 .free = simple_allocator_free,
784};
785
786static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
787 struct page **ret_page)
788{
789 return __alloc_from_contiguous(args->dev, args->size, args->prot,
790 ret_page, args->caller,
Taniya Das31a03832014-06-12 19:42:06 +0530791 args->want_vaddr, args->skip_cpu_sync,
792 args->skip_zeroing, args->coherent_flag);
Rabin Vincentb4268672016-03-03 15:58:01 +0100793}
794
795static void cma_allocator_free(struct arm_dma_free_args *args)
796{
797 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
798 args->size, args->want_vaddr);
799}
800
801static struct arm_dma_allocator cma_allocator = {
802 .alloc = cma_allocator_alloc,
803 .free = cma_allocator_free,
804};
805
806static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
807 struct page **ret_page)
808{
809 return __alloc_from_pool(args->size, ret_page);
810}
811
812static void pool_allocator_free(struct arm_dma_free_args *args)
813{
814 __free_from_pool(args->cpu_addr, args->size);
815}
816
817static struct arm_dma_allocator pool_allocator = {
818 .alloc = pool_allocator_alloc,
819 .free = pool_allocator_free,
820};
821
822static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
823 struct page **ret_page)
824{
825 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
826 args->prot, ret_page, args->caller,
827 args->want_vaddr);
828}
829
830static void remap_allocator_free(struct arm_dma_free_args *args)
831{
832 if (args->want_vaddr)
Laura Abbott172b6f02013-01-16 18:23:19 -0800833 __dma_free_remap(args->cpu_addr, args->size, false);
Rabin Vincentb4268672016-03-03 15:58:01 +0100834
835 __dma_free_buffer(args->page, args->size);
836}
837
838static struct arm_dma_allocator remap_allocator = {
839 .alloc = remap_allocator_alloc,
840 .free = remap_allocator_free,
841};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100842
843static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100844 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700845 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100846{
847 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900848 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000849 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100850 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100851 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100852 struct arm_dma_alloc_args args = {
853 .dev = dev,
854 .size = PAGE_ALIGN(size),
855 .gfp = gfp,
856 .prot = prot,
857 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700858 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Taniya Das31a03832014-06-12 19:42:06 +0530859 .skip_cpu_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC),
860 .skip_zeroing = (attrs & DMA_ATTR_SKIP_ZEROING),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100861 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100862 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100863
Marek Szyprowskic7909502011-12-29 13:09:51 +0100864#ifdef CONFIG_DMA_API_DEBUG
865 u64 limit = (mask + 1) & ~mask;
866 if (limit && size >= limit) {
867 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
868 size, mask);
869 return NULL;
870 }
871#endif
872
873 if (!mask)
874 return NULL;
875
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100876 buf = kzalloc(sizeof(*buf),
877 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100878 if (!buf)
879 return NULL;
880
Marek Szyprowskic7909502011-12-29 13:09:51 +0100881 if (mask < 0xffffffffULL)
882 gfp |= GFP_DMA;
883
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100884 /*
885 * Following is a work-around (a.k.a. hack) to prevent pages
886 * with __GFP_COMP being passed to split_page() which cannot
887 * handle them. The real problem is that this flag probably
888 * should be 0 on ARM as it is not supported on this
889 * platform; see CONFIG_HUGETLBFS.
890 */
891 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100892 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100893
Marek Szyprowski553ac782012-02-29 14:45:28 +0100894 *handle = DMA_ERROR_CODE;
Rabin Vincentb4268672016-03-03 15:58:01 +0100895 allowblock = gfpflags_allow_blocking(gfp);
896 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000897
Rabin Vincentb4268672016-03-03 15:58:01 +0100898 if (cma)
899 buf->allocator = &cma_allocator;
900 else if (nommu() || is_coherent)
901 buf->allocator = &simple_allocator;
902 else if (allowblock)
903 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000904 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100905 buf->allocator = &pool_allocator;
906
907 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000908
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100909 if (page) {
910 unsigned long flags;
911
Russell King9eedd962011-01-03 00:00:17 +0000912 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100913 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100914
915 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
916 list_add(&buf->list, &arm_dma_bufs);
917 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
918 } else {
919 kfree(buf);
920 }
Russell King31ebf942009-11-19 21:12:17 +0000921
Laura Abbott172b6f02013-01-16 18:23:19 -0800922 return addr;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100923}
Russell King695ae0a2009-11-19 16:31:39 +0000924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Allocate DMA-coherent memory space and return both the kernel remapped
927 * virtual and bus address for that space.
928 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200929void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700930 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Charan Teja Reddy19c2f972018-09-11 18:25:30 +0530932 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, false);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400933
Rob Herringdd37e942012-08-21 12:20:17 +0200934 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100935 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200936}
937
938static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700939 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200940{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100941 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100942 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Mike Looijmans55af8a92015-06-03 11:25:31 +0100945static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200946 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700947 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100949 int ret = -ENXIO;
950#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200951 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
952 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100953 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200954 unsigned long off = vma->vm_pgoff;
955
Marek Szyprowski47142f02012-05-15 19:04:13 +0200956 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
957 return ret;
958
Marek Szyprowski50262a42012-07-30 09:35:26 +0200959 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
960 ret = remap_pfn_range(vma, vma->vm_start,
961 pfn + off,
962 vma->vm_end - vma->vm_start,
963 vma->vm_page_prot);
964 }
Catalin Marinasab6494f2009-07-24 12:35:02 +0100965#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 return ret;
968}
969
Laura Abbott4a45a332014-08-05 19:16:28 -0700970static void *arm_dma_remap(struct device *dev, void *cpu_addr,
971 dma_addr_t handle, size_t size,
972 unsigned long attrs)
973{
974 void *ptr;
975 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Charan Teja Reddy19c2f972018-09-11 18:25:30 +0530976 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, false);
Laura Abbott4a45a332014-08-05 19:16:28 -0700977 unsigned long offset = handle & ~PAGE_MASK;
978
979 size = PAGE_ALIGN(size + offset);
980 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot,
981 __builtin_return_address(0));
982 return ptr ? ptr + offset : ptr;
983}
984
985static void arm_dma_unremap(struct device *dev, void *remapped_addr,
986 size_t size)
987{
988 unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
989 struct vm_struct *area;
990
Shiraz Hashim9e6d9842016-11-03 20:44:14 +0530991 size = PAGE_ALIGN(size);
Laura Abbott4a45a332014-08-05 19:16:28 -0700992 remapped_addr = (void *)((unsigned long)remapped_addr & PAGE_MASK);
993
994 area = find_vm_area(remapped_addr);
995 if (!area || (area->flags & flags) != flags) {
996 WARN(1, "trying to free invalid coherent area: %p\n",
997 remapped_addr);
998 return;
999 }
1000
1001 vunmap(remapped_addr);
Vinayak Menonf806a8e2016-03-11 18:24:49 +05301002 flush_tlb_kernel_range((unsigned long)remapped_addr,
1003 (unsigned long)(remapped_addr + size));
Laura Abbott4a45a332014-08-05 19:16:28 -07001004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
Mike Looijmans55af8a92015-06-03 11:25:31 +01001006 * Create userspace mapping for the DMA-coherent memory.
1007 */
1008static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
1009 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001010 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +01001011{
1012 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
1013}
1014
1015int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
1016 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001017 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +01001018{
1019#ifdef CONFIG_MMU
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301020 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
1021 false);
Mike Looijmans55af8a92015-06-03 11:25:31 +01001022#endif /* CONFIG_MMU */
1023 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
1024}
1025
1026/*
Marek Szyprowskic7909502011-12-29 13:09:51 +01001027 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
Rob Herringdd37e942012-08-21 12:20:17 +02001029static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001030 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +02001031 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Marek Szyprowskic7909502011-12-29 13:09:51 +01001033 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001034 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +01001035 struct arm_dma_free_args args = {
1036 .dev = dev,
1037 .size = PAGE_ALIGN(size),
1038 .cpu_addr = cpu_addr,
1039 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001040 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +01001041 };
Shiraz Hashim745dfdc2018-03-19 20:02:23 +05301042 void *addr = (args.want_vaddr) ? cpu_addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001043
Shiraz Hashim745dfdc2018-03-19 20:02:23 +05301044 buf = arm_dma_buffer_find(addr);
1045 if (WARN(!buf, "Freeing invalid buffer %pK\n", addr))
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001046 return;
Russell King5edf71a2005-11-25 15:52:51 +00001047
Rabin Vincentb4268672016-03-03 15:58:01 +01001048 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +01001049 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
Russell Kingafd1a322008-09-25 16:30:57 +01001051
Rob Herringdd37e942012-08-21 12:20:17 +02001052void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001053 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +02001054{
1055 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
1056}
1057
1058static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001059 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +02001060{
1061 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
1062}
1063
Russell King5460e462017-03-29 17:12:47 +01001064/*
1065 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
1066 * that the intention is to allow exporting memory allocated via the
1067 * coherent DMA APIs through the dma_buf API, which only accepts a
1068 * scattertable. This presents a couple of problems:
1069 * 1. Not all memory allocated via the coherent DMA APIs is backed by
1070 * a struct page
1071 * 2. Passing coherent DMA memory into the streaming APIs is not allowed
1072 * as we will try to flush the memory through a different alias to that
1073 * actually being used (and the flushes are redundant.)
1074 */
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001075int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
1076 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001077 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001078{
Russell King5460e462017-03-29 17:12:47 +01001079 unsigned long pfn = dma_to_pfn(dev, handle);
1080 struct page *page;
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001081 int ret;
1082
Russell King5460e462017-03-29 17:12:47 +01001083 /* If the PFN is not valid, we do not have a struct page */
1084 if (!pfn_valid(pfn))
1085 return -ENXIO;
1086
1087 page = pfn_to_page(pfn);
1088
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001089 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1090 if (unlikely(ret))
1091 return ret;
1092
1093 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1094 return 0;
1095}
1096
Russell King65af1912009-11-24 17:53:33 +00001097static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +00001098 size_t size, enum dma_data_direction dir,
1099 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +00001100{
Russell King15653372013-01-19 11:05:57 +00001101 unsigned long pfn;
1102 size_t left = size;
1103
1104 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
1105 offset %= PAGE_SIZE;
1106
Russell King65af1912009-11-24 17:53:33 +00001107 /*
1108 * A single sg entry may refer to multiple physically contiguous
1109 * pages. But we still need to process highmem pages individually.
1110 * If highmem is not configured then the bulk of this loop gets
1111 * optimized out.
1112 */
Russell King65af1912009-11-24 17:53:33 +00001113 do {
1114 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +00001115 void *vaddr;
1116
Russell King15653372013-01-19 11:05:57 +00001117 page = pfn_to_page(pfn);
1118
Russell King93f1d622009-11-24 14:41:01 +00001119 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +00001120 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +00001121 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001122
1123 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001124 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +01001125 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -05001126 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +01001127 } else {
1128 vaddr = kmap_high_get(page);
1129 if (vaddr) {
1130 op(vaddr + offset, len, dir);
1131 kunmap_high(page);
1132 }
Russell King93f1d622009-11-24 14:41:01 +00001133 }
1134 } else {
1135 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +00001136 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +00001137 }
Russell King65af1912009-11-24 17:53:33 +00001138 offset = 0;
Russell King15653372013-01-19 11:05:57 +00001139 pfn++;
Russell King65af1912009-11-24 17:53:33 +00001140 left -= len;
1141 } while (left);
1142}
1143
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001144/*
1145 * Make an area consistent for devices.
1146 * Note: Drivers should NOT use this function directly, as it will break
1147 * platforms with CONFIG_DMABOUNCE.
1148 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
1149 */
1150static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +00001151 size_t size, enum dma_data_direction dir)
1152{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001153 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -04001154
Russell Kinga9c91472009-11-26 16:19:58 +00001155 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -04001156
Russell King65af1912009-11-24 17:53:33 +00001157 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001158 if (dir == DMA_FROM_DEVICE) {
1159 outer_inv_range(paddr, paddr + size);
1160 } else {
1161 outer_clean_range(paddr, paddr + size);
1162 }
1163 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -04001164}
Russell King4ea0d732009-11-24 16:27:17 +00001165
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001166static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001167 size_t size, enum dma_data_direction dir)
1168{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001169 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001170
1171 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001172 /* in any case, don't bother invalidating if DMA to device */
1173 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001174 outer_inv_range(paddr, paddr + size);
1175
Russell Kingdeace4a2014-05-03 11:06:55 +01001176 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1177 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001178
1179 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001180 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001181 */
Ming Leib2a234e2013-05-18 11:21:36 +01001182 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1183 unsigned long pfn;
1184 size_t left = size;
1185
1186 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1187 off %= PAGE_SIZE;
1188 if (off) {
1189 pfn++;
1190 left -= PAGE_SIZE - off;
1191 }
1192 while (left >= PAGE_SIZE) {
1193 page = pfn_to_page(pfn++);
1194 set_bit(PG_dcache_clean, &page->flags);
1195 left -= PAGE_SIZE;
1196 }
1197 }
Russell King4ea0d732009-11-24 16:27:17 +00001198}
Nicolas Pitre43377452009-03-12 22:52:09 -04001199
Russell Kingafd1a322008-09-25 16:30:57 +01001200/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001201 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001202 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1203 * @sg: list of buffers
1204 * @nents: number of buffers to map
1205 * @dir: DMA transfer direction
1206 *
1207 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1208 * This is the scatter-gather version of the dma_map_single interface.
1209 * Here the scatter gather list elements are each tagged with the
1210 * appropriate dma address and length. They are obtained via
1211 * sg_dma_{address,length}.
1212 *
1213 * Device ownership issues as mentioned for dma_map_single are the same
1214 * here.
1215 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001216int arm_dma_map_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 Kingafd1a322008-09-25 16:30:57 +01001220 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001221 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001222
1223 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001224#ifdef CONFIG_NEED_SG_DMA_LENGTH
1225 s->dma_length = s->length;
1226#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001227 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1228 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001229 if (dma_mapping_error(dev, s->dma_address))
1230 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001231 }
Russell Kingafd1a322008-09-25 16:30:57 +01001232 return nents;
Russell King01135d922008-09-25 21:05:02 +01001233
1234 bad_mapping:
1235 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001236 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001237 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001238}
Russell Kingafd1a322008-09-25 16:30:57 +01001239
1240/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001241 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001242 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1243 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001244 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001245 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1246 *
1247 * Unmap a set of streaming mode DMA translations. Again, CPU access
1248 * rules concerning calls here are the same as for dma_unmap_single().
1249 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001250void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001251 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001252{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001253 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001254 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001255
Russell King01135d922008-09-25 21:05:02 +01001256 int i;
Russell King24056f52011-01-03 11:29:28 +00001257
Russell King01135d922008-09-25 21:05:02 +01001258 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001259 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001260}
Russell Kingafd1a322008-09-25 16:30:57 +01001261
1262/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001263 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001264 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1265 * @sg: list of buffers
1266 * @nents: number of buffers to map (returned from dma_map_sg)
1267 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1268 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001269void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001270 int nents, enum dma_data_direction dir)
1271{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001272 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001273 struct scatterlist *s;
1274 int i;
1275
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001276 for_each_sg(sg, s, nents, i)
1277 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1278 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001279}
Russell Kingafd1a322008-09-25 16:30:57 +01001280
1281/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001282 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001283 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1284 * @sg: list of buffers
1285 * @nents: number of buffers to map (returned from dma_map_sg)
1286 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1287 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001288void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001289 int nents, enum dma_data_direction dir)
1290{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001291 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001292 struct scatterlist *s;
1293 int i;
1294
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001295 for_each_sg(sg, s, nents, i)
1296 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1297 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001298}
Russell King24056f52011-01-03 11:29:28 +00001299
Russell King022ae532011-07-08 21:26:59 +01001300/*
1301 * Return whether the given device DMA address mask can be supported
1302 * properly. For example, if your device can only drive the low 24-bits
1303 * during bus mastering, then you would pass 0x00ffffff as the mask
1304 * to this function.
1305 */
1306int dma_supported(struct device *dev, u64 mask)
1307{
Russell King9f28cde2013-12-06 12:30:42 +00001308 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001309}
1310EXPORT_SYMBOL(dma_supported);
1311
Russell King24056f52011-01-03 11:29:28 +00001312#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1313
1314static int __init dma_debug_do_init(void)
1315{
1316 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1317 return 0;
1318}
Marek Szyprowski256ff1c2016-11-15 14:00:53 +01001319core_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001320
1321#ifdef CONFIG_ARM_DMA_USE_IOMMU
1322
1323/* IOMMU */
1324
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001325static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1326
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001327static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1328 size_t size)
1329{
1330 unsigned int order = get_order(size);
1331 unsigned int align = 0;
1332 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301333 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001334 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001335 dma_addr_t iova;
1336 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001337
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001338 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1339 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1340
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001341 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1342 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001343
1344 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001345 for (i = 0; i < mapping->nr_bitmaps; i++) {
1346 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1347 mapping->bits, 0, count, align);
1348
1349 if (start > mapping->bits)
1350 continue;
1351
1352 bitmap_set(mapping->bitmaps[i], start, count);
1353 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001354 }
1355
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001356 /*
1357 * No unused range found. Try to extend the existing mapping
1358 * and perform a second attempt to reserve an IO virtual
1359 * address range of size bytes.
1360 */
1361 if (i == mapping->nr_bitmaps) {
1362 if (extend_iommu_mapping(mapping)) {
1363 spin_unlock_irqrestore(&mapping->lock, flags);
1364 return DMA_ERROR_CODE;
1365 }
1366
1367 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1368 mapping->bits, 0, count, align);
1369
1370 if (start > mapping->bits) {
1371 spin_unlock_irqrestore(&mapping->lock, flags);
1372 return DMA_ERROR_CODE;
1373 }
1374
1375 bitmap_set(mapping->bitmaps[i], start, count);
1376 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001377 spin_unlock_irqrestore(&mapping->lock, flags);
1378
Ritesh Harjani006f8412014-05-20 10:02:59 +05301379 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001380 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001381
1382 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001383}
1384
1385static inline void __free_iova(struct dma_iommu_mapping *mapping,
1386 dma_addr_t addr, size_t size)
1387{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001388 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301389 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001390 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001391 dma_addr_t bitmap_base;
1392 u32 bitmap_index;
1393
1394 if (!size)
1395 return;
1396
Ritesh Harjani006f8412014-05-20 10:02:59 +05301397 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001398 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1399
Ritesh Harjani006f8412014-05-20 10:02:59 +05301400 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001401
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001402 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001403
Ritesh Harjani006f8412014-05-20 10:02:59 +05301404 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001405 /*
1406 * The address range to be freed reaches into the iova
1407 * range of the next bitmap. This should not happen as
1408 * we don't allow this in __alloc_iova (at the
1409 * moment).
1410 */
1411 BUG();
1412 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001413 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001414
1415 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001416 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001417 spin_unlock_irqrestore(&mapping->lock, flags);
1418}
1419
Doug Anderson33298ef2016-01-29 23:06:08 +01001420/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1421static const int iommu_order_array[] = { 9, 8, 4, 0 };
1422
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001423static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001424 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001425 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001426{
1427 struct page **pages;
Shiraz Hashim3340eeb2016-02-21 12:45:34 +05301428 size_t count = size >> PAGE_SHIFT;
1429 size_t array_size = count * sizeof(struct page *);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001430 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001431 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001432
1433 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001434 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001435 else
1436 pages = vzalloc(array_size);
1437 if (!pages)
1438 return NULL;
1439
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001440 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001441 {
1442 unsigned long order = get_order(size);
1443 struct page *page;
1444
1445 page = dma_alloc_from_contiguous(dev, count, order);
1446 if (!page)
1447 goto error;
1448
Taniya Das31a03832014-06-12 19:42:06 +05301449 __dma_clear_buffer(page, size, false, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001450
1451 for (i = 0; i < count; i++)
1452 pages[i] = page + i;
1453
1454 return pages;
1455 }
1456
Doug Anderson14d3ae22016-01-29 23:08:46 +01001457 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001458 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001459 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1460
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001461 /*
1462 * IOMMU can map any pages, so himem can also be used here
1463 */
1464 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1465
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001466 while (count) {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001467 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001468
Doug Anderson33298ef2016-01-29 23:06:08 +01001469 order = iommu_order_array[order_idx];
1470
1471 /* Drop down when we get small */
1472 if (__fls(count) < order) {
1473 order_idx++;
1474 continue;
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001475 }
1476
Doug Anderson33298ef2016-01-29 23:06:08 +01001477 if (order) {
1478 /* See if it's easy to allocate a high-order chunk */
1479 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1480
1481 /* Go down a notch at first sign of pressure */
1482 if (!pages[i]) {
1483 order_idx++;
1484 continue;
1485 }
1486 } else {
Tomasz Figa49f28aa62015-04-01 07:26:33 +01001487 pages[i] = alloc_pages(gfp, 0);
1488 if (!pages[i])
1489 goto error;
1490 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001491
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001492 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001493 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001494 j = 1 << order;
1495 while (--j)
1496 pages[i + j] = pages[i] + j;
1497 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001498
Taniya Das31a03832014-06-12 19:42:06 +05301499 __dma_clear_buffer(pages[i], PAGE_SIZE << order,
1500 false, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001501 i += 1 << order;
1502 count -= 1 << order;
1503 }
1504
1505 return pages;
1506error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001507 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001508 if (pages[i])
1509 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001510 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001511 return NULL;
1512}
1513
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001514static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001515 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001516{
1517 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001518 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001519
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001520 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001521 dma_release_from_contiguous(dev, pages[0], count);
1522 } else {
1523 for (i = 0; i < count; i++)
1524 if (pages[i])
1525 __free_pages(pages[i], 0);
1526 }
1527
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001528 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001529 return 0;
1530}
1531
1532/*
1533 * Create a CPU mapping for a specified pages
1534 */
1535static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001536__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1537 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001538{
Laura Abbott513510d2014-10-09 15:26:40 -07001539 return dma_common_pages_remap(pages, size,
1540 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001541}
1542
1543/*
1544 * Create a mapping in device IO address space for specified pages
1545 */
1546static dma_addr_t
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301547__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1548 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001549{
Will Deacon89cfdb12015-01-16 18:02:15 +01001550 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001551 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1552 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001553 int i;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301554 int prot = IOMMU_READ | IOMMU_WRITE;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001555
1556 dma_addr = __alloc_iova(mapping, size);
1557 if (dma_addr == DMA_ERROR_CODE)
1558 return dma_addr;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301559 prot |= coherent_flag ? IOMMU_CACHE : 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001560
1561 iova = dma_addr;
1562 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001563 int ret;
1564
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001565 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1566 phys_addr_t phys = page_to_phys(pages[i]);
1567 unsigned int len, j;
1568
1569 for (j = i + 1; j < count; j++, next_pfn++)
1570 if (page_to_pfn(pages[j]) != next_pfn)
1571 break;
1572
1573 len = (j - i) << PAGE_SHIFT;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301574 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001575 if (ret < 0)
1576 goto fail;
1577 iova += len;
1578 i = j;
1579 }
1580 return dma_addr;
1581fail:
1582 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1583 __free_iova(mapping, dma_addr, size);
1584 return DMA_ERROR_CODE;
1585}
1586
1587static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1588{
Will Deacon89cfdb12015-01-16 18:02:15 +01001589 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001590
1591 /*
1592 * add optional in-page offset from iova to size and align
1593 * result to page size
1594 */
1595 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1596 iova &= PAGE_MASK;
1597
1598 iommu_unmap(mapping->domain, iova, size);
1599 __free_iova(mapping, iova, size);
1600 return 0;
1601}
1602
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001603static struct page **__atomic_get_pages(void *addr)
1604{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001605 struct page *page;
1606 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001607
Laura Abbott36d0fd22014-10-09 15:26:42 -07001608 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1609 page = phys_to_page(phys);
1610
1611 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001612}
1613
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001614static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001615{
1616 struct vm_struct *area;
1617
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001618 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1619 return __atomic_get_pages(cpu_addr);
1620
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001621 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001622 return cpu_addr;
1623
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001624 area = find_vm_area(cpu_addr);
1625 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1626 return area->pages;
1627 return NULL;
1628}
1629
Gregory CLEMENT56506822016-04-15 11:20:13 +01001630static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
1631 dma_addr_t *handle, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001632{
1633 struct page *page;
1634 void *addr;
1635
Gregory CLEMENT56506822016-04-15 11:20:13 +01001636 if (coherent_flag == COHERENT)
1637 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1638 else
1639 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001640 if (!addr)
1641 return NULL;
1642
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301643 *handle = __iommu_create_mapping(dev, &page, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001644 if (*handle == DMA_ERROR_CODE)
1645 goto err_mapping;
1646
1647 return addr;
1648
1649err_mapping:
1650 __free_from_pool(addr, size);
1651 return NULL;
1652}
1653
Marek Szyprowskid5898292013-02-08 10:54:48 +01001654static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001655 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001656{
1657 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001658 if (coherent_flag == COHERENT)
1659 __dma_free_buffer(virt_to_page(cpu_addr), size);
1660 else
1661 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001662}
1663
Gregory CLEMENT56506822016-04-15 11:20:13 +01001664static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001665 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001666 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001667{
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001668 struct page **pages;
1669 void *addr = NULL;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301670 pgprot_t prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001671
1672 *handle = DMA_ERROR_CODE;
1673 size = PAGE_ALIGN(size);
1674
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301675 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
Gregory CLEMENT56506822016-04-15 11:20:13 +01001676 return __iommu_alloc_simple(dev, size, gfp, handle,
1677 coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001678
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301679 coherent_flag = is_dma_coherent(dev, attrs, coherent_flag);
1680 prot = __get_dma_pgprot(attrs, PAGE_KERNEL, coherent_flag);
Richard Zhao5b91a982013-06-20 20:31:00 +08001681 /*
1682 * Following is a work-around (a.k.a. hack) to prevent pages
1683 * with __GFP_COMP being passed to split_page() which cannot
1684 * handle them. The real problem is that this flag probably
1685 * should be 0 on ARM as it is not supported on this
1686 * platform; see CONFIG_HUGETLBFS.
1687 */
1688 gfp &= ~(__GFP_COMP);
1689
Gregory CLEMENT56506822016-04-15 11:20:13 +01001690 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001691 if (!pages)
1692 return NULL;
1693
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301694 *handle = __iommu_create_mapping(dev, pages, size, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001695 if (*handle == DMA_ERROR_CODE)
1696 goto err_buffer;
1697
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001698 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001699 return pages;
1700
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001701 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1702 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001703 if (!addr)
1704 goto err_mapping;
1705
1706 return addr;
1707
1708err_mapping:
1709 __iommu_remove_mapping(dev, *handle, size);
1710err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001711 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001712 return NULL;
1713}
1714
Gregory CLEMENT56506822016-04-15 11:20:13 +01001715static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001716 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001717{
1718 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1719}
1720
1721static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001722 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001723{
1724 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1725}
1726
1727static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001728 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001729 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001730{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001731 unsigned long uaddr = vma->vm_start;
1732 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001733 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001734 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1735 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001736
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001737 if (!pages)
1738 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001739
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001740 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1741 return -ENXIO;
1742
Marek Szyprowski7e312102015-08-28 09:42:09 +01001743 pages += off;
1744
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001745 do {
1746 int ret = vm_insert_page(vma, uaddr, *pages++);
1747 if (ret) {
1748 pr_err("Remapping memory failed: %d\n", ret);
1749 return ret;
1750 }
1751 uaddr += PAGE_SIZE;
1752 usize -= PAGE_SIZE;
1753 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001754
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001755 return 0;
1756}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001757static int arm_iommu_mmap_attrs(struct device *dev,
1758 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001759 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001760{
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301761 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
1762 is_dma_coherent(dev, attrs, NORMAL));
Gregory CLEMENT56506822016-04-15 11:20:13 +01001763
1764 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1765}
1766
1767static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1768 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001769 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001770{
1771 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1772}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001773
1774/*
1775 * free a page as defined by the above mapping.
1776 * Must not be called with IRQs disabled.
1777 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001778void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001779 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001780{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001781 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001782 size = PAGE_ALIGN(size);
1783
Gregory CLEMENT56506822016-04-15 11:20:13 +01001784 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1785 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001786 return;
1787 }
1788
YoungJun Cho836bfa02013-06-17 13:18:52 +09001789 pages = __iommu_get_pages(cpu_addr, attrs);
1790 if (!pages) {
1791 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1792 return;
1793 }
1794
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001795 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001796 dma_common_free_remap(cpu_addr, size,
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08001797 VM_ARM_DMA_CONSISTENT | VM_USERMAP, false);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001798 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001799
1800 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001801 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001802}
1803
Gregory CLEMENT56506822016-04-15 11:20:13 +01001804void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001805 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001806{
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301807 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs,
1808 is_dma_coherent(dev, attrs, NORMAL));
Gregory CLEMENT56506822016-04-15 11:20:13 +01001809}
1810
1811void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001812 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001813{
1814 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1815}
1816
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001817static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1818 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001819 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001820{
1821 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1822 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1823
1824 if (!pages)
1825 return -ENXIO;
1826
1827 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1828 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001829}
1830
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001831static int __dma_direction_to_prot(enum dma_data_direction dir)
1832{
1833 int prot;
1834
1835 switch (dir) {
1836 case DMA_BIDIRECTIONAL:
1837 prot = IOMMU_READ | IOMMU_WRITE;
1838 break;
1839 case DMA_TO_DEVICE:
1840 prot = IOMMU_READ;
1841 break;
1842 case DMA_FROM_DEVICE:
1843 prot = IOMMU_WRITE;
1844 break;
1845 default:
1846 prot = 0;
1847 }
1848
1849 return prot;
1850}
1851
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001852/*
1853 * Map a part of the scatter-gather list into contiguous io address space
1854 */
1855static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1856 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001857 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001858 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001859{
Will Deacon89cfdb12015-01-16 18:02:15 +01001860 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001861 dma_addr_t iova, iova_base;
1862 int ret = 0;
1863 unsigned int count;
1864 struct scatterlist *s;
Charan Teja Reddye5cc8652018-10-17 18:36:52 +05301865 int prot = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001866
1867 size = PAGE_ALIGN(size);
1868 *handle = DMA_ERROR_CODE;
1869
1870 iova_base = iova = __alloc_iova(mapping, size);
1871 if (iova == DMA_ERROR_CODE)
1872 return -ENOMEM;
1873
Charan Teja Reddye5cc8652018-10-17 18:36:52 +05301874 /*
1875 * Check for coherency.
1876 */
1877 prot |= is_coherent ? IOMMU_CACHE : 0;
1878
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001879 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001880 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001881 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1882
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001883 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001884 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1885
Charan Teja Reddye5cc8652018-10-17 18:36:52 +05301886 prot |= __dma_direction_to_prot(dir);
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001887
1888 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001889 if (ret < 0)
1890 goto fail;
1891 count += len >> PAGE_SHIFT;
1892 iova += len;
1893 }
1894 *handle = iova_base;
1895
1896 return 0;
1897fail:
1898 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1899 __free_iova(mapping, iova_base, size);
1900 return ret;
1901}
1902
Rob Herring0fa478d2012-08-21 12:23:23 +02001903static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001904 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001905 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001906{
1907 struct scatterlist *s = sg, *dma = sg, *start = sg;
1908 int i, count = 0;
1909 unsigned int offset = s->offset;
1910 unsigned int size = s->offset + s->length;
1911 unsigned int max = dma_get_max_seg_size(dev);
1912
1913 for (i = 1; i < nents; i++) {
1914 s = sg_next(s);
1915
1916 s->dma_address = DMA_ERROR_CODE;
1917 s->dma_length = 0;
1918
1919 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1920 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001921 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001922 goto bad_mapping;
1923
1924 dma->dma_address += offset;
1925 dma->dma_length = size - offset;
1926
1927 size = offset = s->offset;
1928 start = s;
1929 dma = sg_next(dma);
1930 count += 1;
1931 }
1932 size += s->length;
1933 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001934 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1935 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001936 goto bad_mapping;
1937
1938 dma->dma_address += offset;
1939 dma->dma_length = size - offset;
1940
1941 return count+1;
1942
1943bad_mapping:
1944 for_each_sg(sg, s, count, i)
1945 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1946 return 0;
1947}
1948
1949/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001950 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1951 * @dev: valid struct device pointer
1952 * @sg: list of buffers
1953 * @nents: number of buffers to map
1954 * @dir: DMA transfer direction
1955 *
1956 * Map a set of i/o coherent buffers described by scatterlist in streaming
1957 * mode for DMA. The scatter gather list elements are merged together (if
1958 * possible) and tagged with the appropriate dma address and length. They are
1959 * obtained via sg_dma_{address,length}.
1960 */
1961int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001962 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001963{
1964 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1965}
1966
1967/**
1968 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1969 * @dev: valid struct device pointer
1970 * @sg: list of buffers
1971 * @nents: number of buffers to map
1972 * @dir: DMA transfer direction
1973 *
1974 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1975 * The scatter gather list elements are merged together (if possible) and
1976 * tagged with the appropriate dma address and length. They are obtained via
1977 * sg_dma_{address,length}.
1978 */
1979int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001980 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001981{
Chintan Pandyad091ab72015-05-26 17:59:11 +05301982 struct scatterlist *s;
1983 int i;
1984 size_t ret;
1985 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1986 unsigned int total_length = 0, current_offset = 0;
1987 dma_addr_t iova;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05301988 bool coherent;
Chintan Pandyad091ab72015-05-26 17:59:11 +05301989 int prot = __dma_direction_to_prot(dir);
Charan Teja Reddye5cc8652018-10-17 18:36:52 +05301990 int upstream_hint = 0;
1991 /*
1992 * This is used to check if there are any unaligned offset/size
1993 * given in the scatter list.
1994 */
1995 bool unaligned_offset_size = false;
Chintan Pandyad091ab72015-05-26 17:59:11 +05301996
Charan Teja Reddye5cc8652018-10-17 18:36:52 +05301997 for_each_sg(sg, s, nents, i) {
Chintan Pandyad091ab72015-05-26 17:59:11 +05301998 total_length += s->length;
Charan Teja Reddye5cc8652018-10-17 18:36:52 +05301999 if ((s->offset & ~PAGE_MASK) || (s->length & ~PAGE_MASK)) {
2000 unaligned_offset_size = true;
2001 break;
2002 }
2003 }
2004
2005 /*
2006 * Check for the upstream domain attribute just to catch
2007 * any abusive clients who expects the unaligned offset/size
2008 * support with out setting this attribute.
2009 * NOTE: on future kernels, we may not have this domain
2010 * attribute set where the check then will be based on just
2011 * offset/size.
2012 */
2013 iommu_domain_get_attr(mapping->domain,
2014 DOMAIN_ATTR_UPSTREAM_IOVA_ALLOCATOR,
2015 &upstream_hint);
2016 if (upstream_hint && unaligned_offset_size)
2017 return __iommu_map_sg(dev, sg, nents, dir, attrs,
2018 is_dma_coherent(dev, attrs, false));
Chintan Pandyad091ab72015-05-26 17:59:11 +05302019
2020 iova = __alloc_iova(mapping, total_length);
qqzhou0c2e37c2018-07-27 15:24:06 +08002021 if (iova == DMA_ERROR_CODE)
2022 return 0;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302023
2024 coherent = of_dma_is_coherent(dev->of_node);
2025 prot |= is_dma_coherent(dev, attrs, coherent) ? IOMMU_CACHE : 0;
2026
Chintan Pandyad091ab72015-05-26 17:59:11 +05302027 ret = iommu_map_sg(mapping->domain, iova, sg, nents, prot);
2028 if (ret != total_length) {
2029 __free_iova(mapping, iova, total_length);
2030 return 0;
2031 }
2032
2033 for_each_sg(sg, s, nents, i) {
2034 s->dma_address = iova + current_offset;
2035 s->dma_length = total_length - current_offset;
2036 current_offset += s->length;
2037 }
2038
2039 return nents;
Rob Herring0fa478d2012-08-21 12:23:23 +02002040}
2041
2042static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002043 int nents, enum dma_data_direction dir,
2044 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02002045{
2046 struct scatterlist *s;
2047 int i;
2048
2049 for_each_sg(sg, s, nents, i) {
2050 if (sg_dma_len(s))
2051 __iommu_remove_mapping(dev, sg_dma_address(s),
2052 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002053 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02002054 __dma_page_dev_to_cpu(sg_page(s), s->offset,
2055 s->length, dir);
2056 }
2057}
2058
2059/**
2060 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
2061 * @dev: valid struct device pointer
2062 * @sg: list of buffers
2063 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
2064 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2065 *
2066 * Unmap a set of streaming mode DMA translations. Again, CPU access
2067 * rules concerning calls here are the same as for dma_unmap_single().
2068 */
2069void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002070 int nents, enum dma_data_direction dir,
2071 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002072{
2073 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
2074}
2075
2076/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002077 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
2078 * @dev: valid struct device pointer
2079 * @sg: list of buffers
2080 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
2081 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2082 *
2083 * Unmap a set of streaming mode DMA translations. Again, CPU access
2084 * rules concerning calls here are the same as for dma_unmap_single().
2085 */
2086void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002087 enum dma_data_direction dir,
2088 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002089{
Chintan Pandyad091ab72015-05-26 17:59:11 +05302090 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
2091 unsigned int total_length = sg_dma_len(sg);
2092 dma_addr_t iova = sg_dma_address(sg);
2093
2094 total_length = PAGE_ALIGN((iova & ~PAGE_MASK) + total_length);
2095 iova &= PAGE_MASK;
2096
Charan Teja Reddy498cf262016-10-24 20:12:33 +05302097 iommu_unmap(mapping->domain, iova, total_length);
Chintan Pandyad091ab72015-05-26 17:59:11 +05302098 __free_iova(mapping, iova, total_length);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002099}
2100
2101/**
2102 * arm_iommu_sync_sg_for_cpu
2103 * @dev: valid struct device pointer
2104 * @sg: list of buffers
2105 * @nents: number of buffers to map (returned from dma_map_sg)
2106 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2107 */
2108void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
2109 int nents, enum dma_data_direction dir)
2110{
2111 struct scatterlist *s;
2112 int i;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302113 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
2114 dma_addr_t iova = sg_dma_address(sg);
2115 bool iova_coherent = iommu_is_iova_coherent(mapping->domain, iova);
2116
2117 if (iova_coherent)
2118 return;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002119
2120 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02002121 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002122
2123}
2124
2125/**
2126 * arm_iommu_sync_sg_for_device
2127 * @dev: valid struct device pointer
2128 * @sg: list of buffers
2129 * @nents: number of buffers to map (returned from dma_map_sg)
2130 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
2131 */
2132void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
2133 int nents, enum dma_data_direction dir)
2134{
2135 struct scatterlist *s;
2136 int i;
2137
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302138 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
2139 dma_addr_t iova = sg_dma_address(sg);
2140 bool iova_coherent = iommu_is_iova_coherent(mapping->domain, iova);
2141
2142 if (iova_coherent)
2143 return;
2144
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002145 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02002146 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002147}
2148
2149
2150/**
Rob Herring0fa478d2012-08-21 12:23:23 +02002151 * arm_coherent_iommu_map_page
2152 * @dev: valid struct device pointer
2153 * @page: page that buffer resides in
2154 * @offset: offset into page for start of buffer
2155 * @size: size of buffer to map
2156 * @dir: DMA transfer direction
2157 *
2158 * Coherent IOMMU aware version of arm_dma_map_page()
2159 */
2160static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
2161 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002162 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002163{
Will Deacon89cfdb12015-01-16 18:02:15 +01002164 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002165 dma_addr_t dma_addr;
Liam Markaeff2c12017-01-26 16:46:14 -08002166 int ret, prot, len, start_offset, map_offset;
2167
2168 map_offset = offset & ~PAGE_MASK;
2169 start_offset = offset & PAGE_MASK;
2170 len = PAGE_ALIGN(map_offset + size);
Rob Herring0fa478d2012-08-21 12:23:23 +02002171
2172 dma_addr = __alloc_iova(mapping, len);
2173 if (dma_addr == DMA_ERROR_CODE)
2174 return dma_addr;
2175
Andreas Herrmannc9b24992013-09-27 00:36:15 +02002176 prot = __dma_direction_to_prot(dir);
Will Deacon13987d62013-06-10 19:34:39 +01002177
Liam Markaeff2c12017-01-26 16:46:14 -08002178 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page) +
2179 start_offset, len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02002180 if (ret < 0)
2181 goto fail;
2182
Liam Markaeff2c12017-01-26 16:46:14 -08002183 return dma_addr + map_offset;
Rob Herring0fa478d2012-08-21 12:23:23 +02002184fail:
2185 __free_iova(mapping, dma_addr, len);
2186 return DMA_ERROR_CODE;
2187}
2188
2189/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002190 * arm_iommu_map_page
2191 * @dev: valid struct device pointer
2192 * @page: page that buffer resides in
2193 * @offset: offset into page for start of buffer
2194 * @size: size of buffer to map
2195 * @dir: DMA transfer direction
2196 *
2197 * IOMMU aware version of arm_dma_map_page()
2198 */
2199static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
2200 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002201 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002202{
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302203 if (!is_dma_coherent(dev, attrs, false) &&
2204 !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002205 __dma_page_cpu_to_dev(page, offset, size, dir);
2206
Rob Herring0fa478d2012-08-21 12:23:23 +02002207 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
2208}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002209
Rob Herring0fa478d2012-08-21 12:23:23 +02002210/**
2211 * arm_coherent_iommu_unmap_page
2212 * @dev: valid struct device pointer
2213 * @handle: DMA address of buffer
2214 * @size: size of buffer (same as passed to dma_map_page)
2215 * @dir: DMA transfer direction (same as passed to dma_map_page)
2216 *
2217 * Coherent IOMMU aware version of arm_dma_unmap_page()
2218 */
2219static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002220 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02002221{
Will Deacon89cfdb12015-01-16 18:02:15 +01002222 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02002223 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02002224 int offset = handle & ~PAGE_MASK;
2225 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002226
Rob Herring0fa478d2012-08-21 12:23:23 +02002227 iommu_unmap(mapping->domain, iova, len);
2228 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002229}
2230
2231/**
2232 * arm_iommu_unmap_page
2233 * @dev: valid struct device pointer
2234 * @handle: DMA address of buffer
2235 * @size: size of buffer (same as passed to dma_map_page)
2236 * @dir: DMA transfer direction (same as passed to dma_map_page)
2237 *
2238 * IOMMU aware version of arm_dma_unmap_page()
2239 */
2240static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002241 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002242{
Will Deacon89cfdb12015-01-16 18:02:15 +01002243 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002244 dma_addr_t iova = handle & PAGE_MASK;
2245 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2246 int offset = handle & ~PAGE_MASK;
2247 int len = PAGE_ALIGN(size + offset);
2248
2249 if (!iova)
2250 return;
2251
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302252 if (!(is_dma_coherent(dev, attrs, false) ||
2253 (attrs & DMA_ATTR_SKIP_CPU_SYNC)))
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002254 __dma_page_dev_to_cpu(page, offset, size, dir);
2255
2256 iommu_unmap(mapping->domain, iova, len);
2257 __free_iova(mapping, iova, len);
2258}
2259
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002260/**
2261 * arm_iommu_map_resource - map a device resource for DMA
2262 * @dev: valid struct device pointer
2263 * @phys_addr: physical address of resource
2264 * @size: size of resource to map
2265 * @dir: DMA transfer direction
2266 */
2267static dma_addr_t arm_iommu_map_resource(struct device *dev,
2268 phys_addr_t phys_addr, size_t size,
2269 enum dma_data_direction dir, unsigned long attrs)
2270{
2271 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2272 dma_addr_t dma_addr;
2273 int ret, prot;
2274 phys_addr_t addr = phys_addr & PAGE_MASK;
2275 unsigned int offset = phys_addr & ~PAGE_MASK;
2276 size_t len = PAGE_ALIGN(size + offset);
2277
2278 dma_addr = __alloc_iova(mapping, len);
2279 if (dma_addr == DMA_ERROR_CODE)
2280 return dma_addr;
2281
2282 prot = __dma_direction_to_prot(dir) | IOMMU_MMIO;
2283
2284 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2285 if (ret < 0)
2286 goto fail;
2287
2288 return dma_addr + offset;
2289fail:
2290 __free_iova(mapping, dma_addr, len);
2291 return DMA_ERROR_CODE;
2292}
2293
2294/**
2295 * arm_iommu_unmap_resource - unmap a device DMA resource
2296 * @dev: valid struct device pointer
2297 * @dma_handle: DMA address to resource
2298 * @size: size of resource to map
2299 * @dir: DMA transfer direction
2300 */
2301static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2302 size_t size, enum dma_data_direction dir,
2303 unsigned long attrs)
2304{
2305 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2306 dma_addr_t iova = dma_handle & PAGE_MASK;
2307 unsigned int offset = dma_handle & ~PAGE_MASK;
2308 size_t len = PAGE_ALIGN(size + offset);
2309
2310 if (!iova)
2311 return;
2312
2313 iommu_unmap(mapping->domain, iova, len);
2314 __free_iova(mapping, iova, len);
2315}
2316
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002317static void arm_iommu_sync_single_for_cpu(struct device *dev,
2318 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2319{
Will Deacon89cfdb12015-01-16 18:02:15 +01002320 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002321 dma_addr_t iova = handle & PAGE_MASK;
2322 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2323 unsigned int offset = handle & ~PAGE_MASK;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302324 bool iova_coherent = iommu_is_iova_coherent(mapping->domain, handle);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002325
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302326 if (!iova_coherent)
2327 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002328}
2329
2330static void arm_iommu_sync_single_for_device(struct device *dev,
2331 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2332{
Will Deacon89cfdb12015-01-16 18:02:15 +01002333 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002334 dma_addr_t iova = handle & PAGE_MASK;
2335 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2336 unsigned int offset = handle & ~PAGE_MASK;
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302337 bool iova_coherent = iommu_is_iova_coherent(mapping->domain, handle);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002338
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302339 if (!iova_coherent)
2340 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002341}
2342
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302343static int arm_iommu_mapping_error(struct device *dev, dma_addr_t dma_addr)
2344{
2345 return dma_addr == DMA_ERROR_CODE;
2346}
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002347const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002348 .alloc = arm_iommu_alloc_attrs,
2349 .free = arm_iommu_free_attrs,
2350 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002351 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002352
2353 .map_page = arm_iommu_map_page,
2354 .unmap_page = arm_iommu_unmap_page,
2355 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2356 .sync_single_for_device = arm_iommu_sync_single_for_device,
2357
2358 .map_sg = arm_iommu_map_sg,
2359 .unmap_sg = arm_iommu_unmap_sg,
2360 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2361 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002362
2363 .map_resource = arm_iommu_map_resource,
2364 .unmap_resource = arm_iommu_unmap_resource,
Charan Teja Reddy19c2f972018-09-11 18:25:30 +05302365
2366 .mapping_error = arm_iommu_mapping_error,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002367};
2368
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002369const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002370 .alloc = arm_coherent_iommu_alloc_attrs,
2371 .free = arm_coherent_iommu_free_attrs,
2372 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002373 .get_sgtable = arm_iommu_get_sgtable,
2374
2375 .map_page = arm_coherent_iommu_map_page,
2376 .unmap_page = arm_coherent_iommu_unmap_page,
2377
2378 .map_sg = arm_coherent_iommu_map_sg,
2379 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002380
2381 .map_resource = arm_iommu_map_resource,
2382 .unmap_resource = arm_iommu_unmap_resource,
Rob Herring0fa478d2012-08-21 12:23:23 +02002383};
2384
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002385/**
2386 * arm_iommu_create_mapping
2387 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2388 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002389 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002390 *
2391 * Creates a mapping structure which holds information about used/unused
2392 * IO address ranges, which is required to perform memory allocation and
2393 * mapping with IOMMU aware functions.
2394 *
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002395 * Clients may use iommu_domain_set_attr() to set additional flags prior
2396 * to calling arm_iommu_attach_device() to complete initialization.
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002397 */
2398struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002399arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002400{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002401 unsigned int bits = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002402 struct dma_iommu_mapping *mapping;
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002403
2404 if (!bits)
2405 return ERR_PTR(-EINVAL);
2406
2407 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2408 if (!mapping)
2409 return ERR_PTR(-ENOMEM);
2410
2411 mapping->base = base;
2412 mapping->bits = bits;
2413
2414 mapping->domain = iommu_domain_alloc(bus);
2415 if (!mapping->domain)
2416 goto err_domain_alloc;
2417
2418 mapping->init = false;
2419 return mapping;
2420
2421err_domain_alloc:
2422 kfree(mapping);
2423 return ERR_PTR(-ENOMEM);
2424}
2425EXPORT_SYMBOL(arm_iommu_create_mapping);
2426
2427static int
2428iommu_init_mapping(struct device *dev, struct dma_iommu_mapping *mapping)
2429{
2430 unsigned int bitmap_size = BITS_TO_LONGS(mapping->bits) * sizeof(long);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002431 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002432 int err = -ENOMEM;
2433
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002434 if (!bitmap_size)
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002435 return -EINVAL;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002436
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002437 if (bitmap_size > PAGE_SIZE) {
2438 extensions = bitmap_size / PAGE_SIZE;
2439 bitmap_size = PAGE_SIZE;
2440 }
2441
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002442 mapping->bitmap_size = bitmap_size;
2443 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002444 GFP_KERNEL);
2445
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002446 if (!mapping->bitmaps)
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002447 return -ENOMEM;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002448
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002449 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002450 if (!mapping->bitmaps[0])
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002451 goto err;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002452
2453 mapping->nr_bitmaps = 1;
2454 mapping->extensions = extensions;
qqzhou5d4eb4a2018-05-08 18:02:19 +08002455 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002456
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002457 spin_lock_init(&mapping->lock);
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002458 mapping->ops = &iommu_ops;
2459 return 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002460
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002461err:
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002462 kfree(mapping->bitmaps);
2463 return err;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002464}
2465
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002466static void iommu_release_mapping(struct kref *kref)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002467{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002468 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002469 struct dma_iommu_mapping *mapping =
2470 container_of(kref, struct dma_iommu_mapping, kref);
2471
2472 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002473 for (i = 0; i < mapping->nr_bitmaps; i++)
2474 kfree(mapping->bitmaps[i]);
2475 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002476 kfree(mapping);
2477}
2478
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002479static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2480{
2481 int next_bitmap;
2482
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002483 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002484 return -EINVAL;
2485
2486 next_bitmap = mapping->nr_bitmaps;
2487 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2488 GFP_ATOMIC);
2489 if (!mapping->bitmaps[next_bitmap])
2490 return -ENOMEM;
2491
2492 mapping->nr_bitmaps++;
2493
2494 return 0;
2495}
2496
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002497/*
2498 * arm_iommu_release_mapping
2499 * @mapping: allocted via arm_iommu_create_mapping()
2500 *
2501 * Frees all resources associated with the iommu mapping.
2502 * The device associated with this mapping must be in the 'detached' state
2503 */
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002504void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2505{
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002506 int is_fast = 0;
2507 void (*release)(struct kref *kref);
2508
2509 if (!mapping)
2510 return;
2511
2512 if (!mapping->init) {
2513 iommu_domain_free(mapping->domain);
2514 kfree(mapping);
2515 return;
2516 }
2517
2518 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2519
2520 if (is_fast)
2521 release = fast_smmu_release_mapping;
2522 else
2523 release = iommu_release_mapping;
2524
2525 kref_put(&mapping->kref, release);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002526}
Prathyush K18177d12013-01-04 06:22:42 -05002527EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002528
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002529static int arm_iommu_init_mapping(struct device *dev,
2530 struct dma_iommu_mapping *mapping)
2531{
2532 int err = -EINVAL;
2533 u64 size = mapping->bits << PAGE_SHIFT;
2534 int is_fast = 0;
2535
2536 if (mapping->init) {
2537 kref_get(&mapping->kref);
2538 return 0;
2539 }
2540
2541 /* currently only 32-bit DMA address space is supported */
2542 if (size > DMA_BIT_MASK(32) + 1) {
2543 dev_err(dev, "dma mask %llx too small\n", dma_get_mask(dev));
2544 return -ERANGE;
2545 }
2546
2547 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2548
2549 if (is_fast)
2550 err = fast_smmu_init_mapping(dev, mapping);
2551 else
2552 err = iommu_init_mapping(dev, mapping);
2553 if (!err) {
2554 kref_init(&mapping->kref);
2555 mapping->init = true;
2556 }
2557 return err;
2558}
2559
2560
Laurent Pincharteab8d652015-01-23 16:21:49 +02002561static int __arm_iommu_attach_device(struct device *dev,
2562 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002563{
2564 int err;
2565
Patrick Daly3281dc62018-07-20 19:32:37 -07002566 if (!dev->iommu_group)
2567 return -EINVAL;
2568
2569 err = iommu_attach_group(mapping->domain, dev->iommu_group);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002570 if (err)
2571 return err;
2572
Will Deacon89cfdb12015-01-16 18:02:15 +01002573 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002574
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002575 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002576 return 0;
2577}
2578
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002579static void __arm_iommu_detach_device(struct device *dev)
2580{
2581 struct dma_iommu_mapping *mapping;
2582 int is_fast;
2583
2584 mapping = to_dma_iommu_mapping(dev);
2585 if (!mapping) {
2586 dev_warn(dev, "Not attached\n");
2587 return;
2588 }
2589
2590 if (msm_dma_unmap_all_for_dev(dev))
2591 dev_warn(dev, "IOMMU detach with outstanding mappings\n");
2592 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_FAST, &is_fast);
2593
2594 iommu_detach_device(mapping->domain, dev);
2595 to_dma_iommu_mapping(dev) = NULL;
2596
2597 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2598}
2599
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002600/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002601 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002602 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002603 * @mapping: io address space mapping structure (returned from
2604 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002605 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002606 * Attaches specified io address space mapping to the provided device.
2607 * This replaces the dma operations (dma_map_ops pointer) with the
2608 * IOMMU aware version.
2609 *
2610 * More than one client might be attached to the same io address space
2611 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002612 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002613int arm_iommu_attach_device(struct device *dev,
2614 struct dma_iommu_mapping *mapping)
2615{
2616 int err;
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302617 int s1_bypass = 0;
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002618 struct iommu_group *group = dev->iommu_group;
Charan Teja Reddy8a8533d2017-02-09 20:44:29 +05302619
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002620 if (!group) {
2621 dev_err(dev, "No iommu associated with device\n");
2622 return -EINVAL;
2623 }
2624
2625 if (iommu_get_domain_for_dev(dev)) {
2626 dev_err(dev, "Device already attached to other iommu_domain\n");
2627 return -EINVAL;
2628 }
Laurent Pincharteab8d652015-01-23 16:21:49 +02002629
2630 err = __arm_iommu_attach_device(dev, mapping);
2631 if (err)
2632 return err;
2633
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002634 err = arm_iommu_init_mapping(dev, mapping);
2635 if (err) {
2636 __arm_iommu_detach_device(dev);
2637 return err;
2638 }
2639
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302640 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_S1_BYPASS,
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002641 &s1_bypass);
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302642 if (!s1_bypass)
Sudarshan Rajagopalan6266e5c2017-11-06 17:29:30 -08002643 set_dma_ops(dev, mapping->ops);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002644 return 0;
2645}
2646EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2647
Laurent Pincharteab8d652015-01-23 16:21:49 +02002648/**
2649 * arm_iommu_detach_device
2650 * @dev: valid struct device pointer
2651 *
2652 * Detaches the provided device from a previously attached map.
2653 * This voids the dma operations (dma_map_ops pointer)
2654 */
2655void arm_iommu_detach_device(struct device *dev)
2656{
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302657 struct dma_iommu_mapping *mapping;
2658 int s1_bypass = 0;
2659
2660 mapping = to_dma_iommu_mapping(dev);
2661 if (!mapping) {
2662 dev_warn(dev, "Not attached\n");
2663 return;
2664 }
2665
Laurent Pincharteab8d652015-01-23 16:21:49 +02002666 __arm_iommu_detach_device(dev);
Charan Teja Reddy2540c412016-12-07 18:40:57 +05302667
2668 iommu_domain_get_attr(mapping->domain, DOMAIN_ATTR_S1_BYPASS,
2669 &s1_bypass);
2670 if (!s1_bypass)
2671 set_dma_ops(dev, NULL);
Laurent Pincharteab8d652015-01-23 16:21:49 +02002672}
Prathyush K18177d12013-01-04 06:22:42 -05002673EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002674
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002675static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002676{
2677 return coherent ? &iommu_coherent_ops : &iommu_ops;
2678}
2679
2680static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002681 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002682{
2683 struct dma_iommu_mapping *mapping;
2684
2685 if (!iommu)
2686 return false;
2687
2688 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2689 if (IS_ERR(mapping)) {
2690 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2691 size, dev_name(dev));
2692 return false;
2693 }
2694
Laurent Pincharteab8d652015-01-23 16:21:49 +02002695 if (__arm_iommu_attach_device(dev, mapping)) {
Sudarshan Rajagopalanf63f97c2018-01-25 10:56:32 -08002696 pr_debug("Failed to attached device %s to IOMMU_mapping\n",
Will Deacon4bb25782014-08-27 17:52:44 +01002697 dev_name(dev));
2698 arm_iommu_release_mapping(mapping);
2699 return false;
2700 }
2701
2702 return true;
2703}
2704
2705static void arm_teardown_iommu_dma_ops(struct device *dev)
2706{
Will Deacon89cfdb12015-01-16 18:02:15 +01002707 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002708
Will Deaconc2273a12015-01-16 18:01:43 +01002709 if (!mapping)
2710 return;
2711
Laurent Pincharteab8d652015-01-23 16:21:49 +02002712 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002713 arm_iommu_release_mapping(mapping);
2714}
2715
2716#else
2717
2718static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002719 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002720{
2721 return false;
2722}
2723
2724static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2725
2726#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2727
2728#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2729
2730static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
2731{
2732 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2733}
2734
2735void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002736 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002737{
Jeevan Shrirambc1b0002017-03-02 18:17:56 -08002738 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002739
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002740 dev->archdata.dma_coherent = coherent;
Will Deacon4bb25782014-08-27 17:52:44 +01002741 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2742 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2743 else
2744 dma_ops = arm_get_dma_map_ops(coherent);
2745
2746 set_dma_ops(dev, dma_ops);
2747}
Runmin Wang8cb38812017-03-24 13:14:41 -07002748EXPORT_SYMBOL(arch_setup_dma_ops);
Will Deacon4bb25782014-08-27 17:52:44 +01002749
2750void arch_teardown_dma_ops(struct device *dev)
2751{
2752 arm_teardown_iommu_dma_ops(dev);
2753}