blob: 439bb6a59a04a2d8008487bd6946614304a6b378 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Russell King0ddbccd2008-09-25 15:59:19 +01003 * linux/arch/arm/mm/dma-mapping.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2000-2004 Russell King
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * DMA uncached mapping support.
8 */
9#include <linux/module.h>
10#include <linux/mm.h>
Laura Abbott36d0fd22014-10-09 15:26:42 -070011#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/list.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010018#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050019#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010020#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010021#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020022#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020023#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020024#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010025#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070026#include <linux/cma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010028#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040029#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010032#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020033#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010034#include <asm/mach/map.h>
35#include <asm/system_info.h>
36#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Russell King1234e3f2015-07-24 09:10:55 +010038#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010039#include "mm.h"
40
Rabin Vincentb4268672016-03-03 15:58:01 +010041struct arm_dma_alloc_args {
42 struct device *dev;
43 size_t size;
44 gfp_t gfp;
45 pgprot_t prot;
46 const void *caller;
47 bool want_vaddr;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010048 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010049};
50
51struct arm_dma_free_args {
52 struct device *dev;
53 size_t size;
54 void *cpu_addr;
55 struct page *page;
56 bool want_vaddr;
57};
58
Gregory CLEMENTf1270892016-04-15 11:15:18 +010059#define NORMAL 0
60#define COHERENT 1
61
Rabin Vincentb4268672016-03-03 15:58:01 +010062struct arm_dma_allocator {
63 void *(*alloc)(struct arm_dma_alloc_args *args,
64 struct page **ret_page);
65 void (*free)(struct arm_dma_free_args *args);
66};
67
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010068struct arm_dma_buffer {
69 struct list_head list;
70 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010071 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010072};
73
74static LIST_HEAD(arm_dma_bufs);
75static DEFINE_SPINLOCK(arm_dma_bufs_lock);
76
77static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
78{
79 struct arm_dma_buffer *buf, *found = NULL;
80 unsigned long flags;
81
82 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
83 list_for_each_entry(buf, &arm_dma_bufs, list) {
84 if (buf->virt == virt) {
85 list_del(&buf->list);
86 found = buf;
87 break;
88 }
89 }
90 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
91 return found;
92}
93
Marek Szyprowski15237e12012-02-10 19:55:20 +010094/*
95 * The DMA API is built upon the notion of "buffer ownership". A buffer
96 * is either exclusively owned by the CPU (and therefore may be accessed
97 * by it) or exclusively owned by the DMA device. These helper functions
98 * represent the transitions between these two ownership states.
99 *
100 * Note, however, that on later ARMs, this notion does not work due to
101 * speculative prefetches. We model our approach on the assumption that
102 * the CPU does do speculative prefetches, which means we clean caches
103 * before transfers and delay cache invalidation until transfer completion.
104 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100105 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100106static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100107 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100108static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100109 size_t, enum dma_data_direction);
110
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100111/**
112 * arm_dma_map_page - map a portion of a page for streaming DMA
113 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
114 * @page: page that buffer resides in
115 * @offset: offset into page for start of buffer
116 * @size: size of buffer to map
117 * @dir: DMA transfer direction
118 *
119 * Ensure that any data held in the cache is appropriately discarded
120 * or written back.
121 *
122 * The device owns this memory once this call has completed. The CPU
123 * can regain ownership by calling dma_unmap_page().
124 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100125static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100126 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700127 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100128{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700129 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100130 __dma_page_cpu_to_dev(page, offset, size, dir);
131 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100132}
133
Rob Herringdd37e942012-08-21 12:20:17 +0200134static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
135 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700136 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200137{
138 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
139}
140
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100141/**
142 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
143 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
144 * @handle: DMA address of buffer
145 * @size: size of buffer (same as passed to dma_map_page)
146 * @dir: DMA transfer direction (same as passed to dma_map_page)
147 *
148 * Unmap a page streaming mode DMA translation. The handle and size
149 * must match what was provided in the previous dma_map_page() call.
150 * All other usages are undefined.
151 *
152 * After this call, reads by the CPU to the buffer are guaranteed to see
153 * whatever the device wrote there.
154 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100155static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700156 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100157{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700158 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100159 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
160 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100161}
162
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100163static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100164 dma_addr_t handle, size_t size, enum dma_data_direction dir)
165{
166 unsigned int offset = handle & (PAGE_SIZE - 1);
167 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200168 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100169}
170
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100171static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100172 dma_addr_t handle, size_t size, enum dma_data_direction dir)
173{
174 unsigned int offset = handle & (PAGE_SIZE - 1);
175 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200176 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100177}
178
Bart Van Assche52997092017-01-20 13:04:01 -0800179const struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200180 .alloc = arm_dma_alloc,
181 .free = arm_dma_free,
182 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200183 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100184 .map_page = arm_dma_map_page,
185 .unmap_page = arm_dma_unmap_page,
186 .map_sg = arm_dma_map_sg,
187 .unmap_sg = arm_dma_unmap_sg,
Christoph Hellwigcfced782019-01-04 18:20:05 +0100188 .map_resource = dma_direct_map_resource,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100189 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
190 .sync_single_for_device = arm_dma_sync_single_for_device,
191 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
192 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Christoph Hellwig418a7a72017-05-22 11:20:18 +0200193 .dma_supported = arm_dma_supported,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100194};
195EXPORT_SYMBOL(arm_dma_ops);
196
Rob Herringdd37e942012-08-21 12:20:17 +0200197static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700198 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200199static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700200 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100201static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
202 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700203 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200204
Bart Van Assche52997092017-01-20 13:04:01 -0800205const struct dma_map_ops arm_coherent_dma_ops = {
Rob Herringdd37e942012-08-21 12:20:17 +0200206 .alloc = arm_coherent_dma_alloc,
207 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100208 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200209 .get_sgtable = arm_dma_get_sgtable,
210 .map_page = arm_coherent_dma_map_page,
211 .map_sg = arm_dma_map_sg,
Christoph Hellwigcfced782019-01-04 18:20:05 +0100212 .map_resource = dma_direct_map_resource,
Christoph Hellwig418a7a72017-05-22 11:20:18 +0200213 .dma_supported = arm_dma_supported,
Rob Herringdd37e942012-08-21 12:20:17 +0200214};
215EXPORT_SYMBOL(arm_coherent_dma_ops);
216
Russell King9f28cde2013-12-06 12:30:42 +0000217static int __dma_supported(struct device *dev, u64 mask, bool warn)
218{
219 unsigned long max_dma_pfn;
220
221 /*
222 * If the mask allows for more memory than we can address,
223 * and we actually have that much memory, then we must
224 * indicate that DMA to this device is not supported.
225 */
226 if (sizeof(mask) != sizeof(dma_addr_t) &&
227 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000228 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000229 if (warn) {
230 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
231 mask);
232 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
233 }
234 return 0;
235 }
236
237 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
238
239 /*
240 * Translate the device's DMA mask to a PFN limit. This
241 * PFN number includes the page which we can DMA to.
242 */
243 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
244 if (warn)
245 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",
246 mask,
247 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
248 max_dma_pfn + 1);
249 return 0;
250 }
251
252 return 1;
253}
254
Catalin Marinasab6494f2009-07-24 12:35:02 +0100255static u64 get_coherent_dma_mask(struct device *dev)
256{
Russell King4dcfa602013-07-09 12:14:49 +0100257 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Catalin Marinasab6494f2009-07-24 12:35:02 +0100259 if (dev) {
260 mask = dev->coherent_dma_mask;
261
262 /*
263 * Sanity check the DMA mask - it must be non-zero, and
264 * must be able to be satisfied by a DMA allocation.
265 */
266 if (mask == 0) {
267 dev_warn(dev, "coherent DMA mask is unset\n");
268 return 0;
269 }
270
Russell King9f28cde2013-12-06 12:30:42 +0000271 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100272 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100273 }
274
275 return mask;
276}
277
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100278static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100279{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100280 /*
281 * Ensure that the allocated pages are zeroed, and that any data
282 * lurking in the kernel direct-mapped region is invalidated.
283 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100284 if (PageHighMem(page)) {
285 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
286 phys_addr_t end = base + size;
287 while (size > 0) {
288 void *ptr = kmap_atomic(page);
289 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100290 if (coherent_flag != COHERENT)
291 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100292 kunmap_atomic(ptr);
293 page++;
294 size -= PAGE_SIZE;
295 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100296 if (coherent_flag != COHERENT)
297 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100298 } else {
299 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200300 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100301 if (coherent_flag != COHERENT) {
302 dmac_flush_range(ptr, ptr + size);
303 outer_flush_range(__pa(ptr), __pa(ptr) + size);
304 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200305 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100306}
307
Russell King7a9a32a2009-11-19 15:31:07 +0000308/*
309 * Allocate a DMA buffer for 'dev' of size 'size' using the
310 * specified gfp mask. Note that 'size' must be page aligned.
311 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100312static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
313 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000314{
315 unsigned long order = get_order(size);
316 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000317
318 page = alloc_pages(gfp, order);
319 if (!page)
320 return NULL;
321
322 /*
323 * Now split the huge page and free the excess pages
324 */
325 split_page(page, order);
326 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
327 __free_page(p);
328
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100329 __dma_clear_buffer(page, size, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000330
331 return page;
332}
333
334/*
335 * Free a DMA buffer. 'size' must be page aligned.
336 */
337static void __dma_free_buffer(struct page *page, size_t size)
338{
339 struct page *e = page + (size >> PAGE_SHIFT);
340
341 while (page < e) {
342 __free_page(page);
343 page++;
344 }
345}
346
Marek Szyprowskic7909502011-12-29 13:09:51 +0100347static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100348 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100349 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800350 int coherent_flag, gfp_t gfp);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100351
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200352static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
353 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100354 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200355
356static void *
357__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
358 const void *caller)
359{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200360 /*
361 * DMA allocation can be mapped to user space, so lets
362 * set VM_USERMAP flags too.
363 */
Laura Abbott513510d2014-10-09 15:26:40 -0700364 return dma_common_contiguous_remap(page, size,
365 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
366 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200367}
368
369static void __dma_free_remap(void *cpu_addr, size_t size)
370{
Laura Abbott513510d2014-10-09 15:26:40 -0700371 dma_common_free_remap(cpu_addr, size,
372 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200373}
374
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200375#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Vladimir Murzinb337e1c2017-09-25 10:29:07 +0100376static struct gen_pool *atomic_pool __ro_after_init;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200377
Vladimir Murzinb337e1c2017-09-25 10:29:07 +0100378static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100379
380static int __init early_coherent_pool(char *p)
381{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700382 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100383 return 0;
384}
385early_param("coherent_pool", early_coherent_pool);
386
387/*
388 * Initialise the coherent pool for atomic allocations.
389 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200390static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100391{
Russell King71b55662013-11-25 12:01:03 +0000392 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100393 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100394 struct page *page;
395 void *ptr;
396
Laura Abbott36d0fd22014-10-09 15:26:42 -0700397 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
398 if (!atomic_pool)
399 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100400 /*
401 * The atomic pool is only used for non-coherent allocations
402 * so we must pass NORMAL for coherent_flag.
403 */
Gioh Kime464ef12014-05-22 13:38:23 +0900404 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700405 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Lucas Stach712c6042017-02-24 14:58:44 -0800406 &page, atomic_pool_init, true, NORMAL,
407 GFP_KERNEL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200408 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700409 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100410 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100411 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700412 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300413
Laura Abbott36d0fd22014-10-09 15:26:42 -0700414 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
415 page_to_phys(page),
416 atomic_pool_size, -1);
417 if (ret)
418 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300419
Laura Abbott36d0fd22014-10-09 15:26:42 -0700420 gen_pool_set_algo(atomic_pool,
421 gen_pool_first_fit_order_align,
Vladimir Murzinacb62442017-09-25 10:25:53 +0100422 NULL);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100423 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700424 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100425 return 0;
426 }
Sachin Kamatec106652012-09-24 08:35:03 +0200427
Laura Abbott36d0fd22014-10-09 15:26:42 -0700428destroy_genpool:
429 gen_pool_destroy(atomic_pool);
430 atomic_pool = NULL;
431out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100432 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700433 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100434 return -ENOMEM;
435}
436/*
437 * CMA is activated by core_initcall, so we must be called after it.
438 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200439postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100440
441struct dma_contig_early_reserve {
442 phys_addr_t base;
443 unsigned long size;
444};
445
446static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
447
448static int dma_mmu_remap_num __initdata;
449
450void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
451{
452 dma_mmu_remap[dma_mmu_remap_num].base = base;
453 dma_mmu_remap[dma_mmu_remap_num].size = size;
454 dma_mmu_remap_num++;
455}
456
457void __init dma_contiguous_remap(void)
458{
459 int i;
460 for (i = 0; i < dma_mmu_remap_num; i++) {
461 phys_addr_t start = dma_mmu_remap[i].base;
462 phys_addr_t end = start + dma_mmu_remap[i].size;
463 struct map_desc map;
464 unsigned long addr;
465
466 if (end > arm_lowmem_limit)
467 end = arm_lowmem_limit;
468 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200469 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100470
471 map.pfn = __phys_to_pfn(start);
472 map.virtual = __phys_to_virt(start);
473 map.length = end - start;
474 map.type = MT_MEMORY_DMA_READY;
475
476 /*
Russell King6b076992014-07-17 12:17:45 +0100477 * Clear previous low-memory mapping to ensure that the
478 * TLB does not see any conflicting entries, then flush
479 * the TLB of the old entries before creating new mappings.
480 *
481 * This ensures that any speculatively loaded TLB entries
482 * (even though they may be rare) can not cause any problems,
483 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100484 */
485 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400486 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100487 pmd_clear(pmd_off_k(addr));
488
Russell King6b076992014-07-17 12:17:45 +0100489 flush_tlb_kernel_range(__phys_to_virt(start),
490 __phys_to_virt(end));
491
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900492 iotable_init(&map, 1);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100493 }
494}
495
Marek Szyprowskic7909502011-12-29 13:09:51 +0100496static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
497 void *data)
498{
499 struct page *page = virt_to_page(addr);
500 pgprot_t prot = *(pgprot_t *)data;
501
502 set_pte_ext(pte, mk_pte(page, prot), 0);
503 return 0;
504}
505
506static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
507{
508 unsigned long start = (unsigned long) page_address(page);
509 unsigned end = start + size;
510
511 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100512 flush_tlb_kernel_range(start, end);
513}
514
515static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
516 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100517 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100518{
519 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100520 void *ptr = NULL;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100521 /*
522 * __alloc_remap_buffer is only called when the device is
523 * non-coherent
524 */
525 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100526 if (!page)
527 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100528 if (!want_vaddr)
529 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100530
531 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
532 if (!ptr) {
533 __dma_free_buffer(page, size);
534 return NULL;
535 }
536
Carlo Caione6e8266e2015-02-09 10:38:35 +0100537 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100538 *ret_page = page;
539 return ptr;
540}
541
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200542static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100543{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700544 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200545 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100546
Laura Abbott36d0fd22014-10-09 15:26:42 -0700547 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200548 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100549 return NULL;
550 }
551
Laura Abbott36d0fd22014-10-09 15:26:42 -0700552 val = gen_pool_alloc(atomic_pool, size);
553 if (val) {
554 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200555
Laura Abbott36d0fd22014-10-09 15:26:42 -0700556 *ret_page = phys_to_page(phys);
557 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100558 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200559
560 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100561}
562
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300563static bool __in_atomic_pool(void *start, size_t size)
564{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700565 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300566}
567
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200568static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100569{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300570 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100571 return 0;
572
Laura Abbott36d0fd22014-10-09 15:26:42 -0700573 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200574
Marek Szyprowskic7909502011-12-29 13:09:51 +0100575 return 1;
576}
577
578static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100579 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100580 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800581 int coherent_flag, gfp_t gfp)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100582{
583 unsigned long order = get_order(size);
584 size_t count = size >> PAGE_SHIFT;
585 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100586 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100587
Marek Szyprowskid834c5a2018-08-17 15:49:00 -0700588 page = dma_alloc_from_contiguous(dev, count, order, gfp & __GFP_NOWARN);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100589 if (!page)
590 return NULL;
591
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100592 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100593
Carlo Caione6e8266e2015-02-09 10:38:35 +0100594 if (!want_vaddr)
595 goto out;
596
Marek Szyprowski9848e482013-01-16 15:38:44 +0100597 if (PageHighMem(page)) {
598 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
599 if (!ptr) {
600 dma_release_from_contiguous(dev, page, count);
601 return NULL;
602 }
603 } else {
604 __dma_remap(page, size, prot);
605 ptr = page_address(page);
606 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100607
608 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100609 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100610 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100611}
612
613static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100614 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100615{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100616 if (want_vaddr) {
617 if (PageHighMem(page))
618 __dma_free_remap(cpu_addr, size);
619 else
620 __dma_remap(page, size, PAGE_KERNEL);
621 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100622 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
623}
624
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700625static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200626{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700627 prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
628 pgprot_writecombine(prot) :
629 pgprot_dmacoherent(prot);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200630 return prot;
631}
632
Marek Szyprowskic7909502011-12-29 13:09:51 +0100633static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
634 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100635{
Russell King04da5692009-11-19 15:54:45 +0000636 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100637 /* __alloc_simple_buffer is only called when the device is coherent */
638 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100639 if (!page)
640 return NULL;
641
642 *ret_page = page;
643 return page_address(page);
644}
645
Rabin Vincentb4268672016-03-03 15:58:01 +0100646static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
647 struct page **ret_page)
648{
649 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
650 ret_page);
651}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100652
Rabin Vincentb4268672016-03-03 15:58:01 +0100653static void simple_allocator_free(struct arm_dma_free_args *args)
654{
655 __dma_free_buffer(args->page, args->size);
656}
657
658static struct arm_dma_allocator simple_allocator = {
659 .alloc = simple_allocator_alloc,
660 .free = simple_allocator_free,
661};
662
663static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
664 struct page **ret_page)
665{
666 return __alloc_from_contiguous(args->dev, args->size, args->prot,
667 ret_page, args->caller,
Lucas Stach712c6042017-02-24 14:58:44 -0800668 args->want_vaddr, args->coherent_flag,
669 args->gfp);
Rabin Vincentb4268672016-03-03 15:58:01 +0100670}
671
672static void cma_allocator_free(struct arm_dma_free_args *args)
673{
674 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
675 args->size, args->want_vaddr);
676}
677
678static struct arm_dma_allocator cma_allocator = {
679 .alloc = cma_allocator_alloc,
680 .free = cma_allocator_free,
681};
682
683static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
684 struct page **ret_page)
685{
686 return __alloc_from_pool(args->size, ret_page);
687}
688
689static void pool_allocator_free(struct arm_dma_free_args *args)
690{
691 __free_from_pool(args->cpu_addr, args->size);
692}
693
694static struct arm_dma_allocator pool_allocator = {
695 .alloc = pool_allocator_alloc,
696 .free = pool_allocator_free,
697};
698
699static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
700 struct page **ret_page)
701{
702 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
703 args->prot, ret_page, args->caller,
704 args->want_vaddr);
705}
706
707static void remap_allocator_free(struct arm_dma_free_args *args)
708{
709 if (args->want_vaddr)
710 __dma_free_remap(args->cpu_addr, args->size);
711
712 __dma_free_buffer(args->page, args->size);
713}
714
715static struct arm_dma_allocator remap_allocator = {
716 .alloc = remap_allocator_alloc,
717 .free = remap_allocator_free,
718};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100719
720static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100721 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700722 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100723{
724 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900725 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000726 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100727 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100728 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100729 struct arm_dma_alloc_args args = {
730 .dev = dev,
731 .size = PAGE_ALIGN(size),
732 .gfp = gfp,
733 .prot = prot,
734 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700735 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100736 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100737 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100738
Marek Szyprowskic7909502011-12-29 13:09:51 +0100739#ifdef CONFIG_DMA_API_DEBUG
740 u64 limit = (mask + 1) & ~mask;
741 if (limit && size >= limit) {
742 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
743 size, mask);
744 return NULL;
745 }
746#endif
747
748 if (!mask)
749 return NULL;
750
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100751 buf = kzalloc(sizeof(*buf),
752 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100753 if (!buf)
754 return NULL;
755
Marek Szyprowskic7909502011-12-29 13:09:51 +0100756 if (mask < 0xffffffffULL)
757 gfp |= GFP_DMA;
758
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100759 /*
760 * Following is a work-around (a.k.a. hack) to prevent pages
761 * with __GFP_COMP being passed to split_page() which cannot
762 * handle them. The real problem is that this flag probably
763 * should be 0 on ARM as it is not supported on this
764 * platform; see CONFIG_HUGETLBFS.
765 */
766 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100767 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100768
Christoph Hellwig72fd97b2018-11-21 18:57:36 +0100769 *handle = DMA_MAPPING_ERROR;
Rabin Vincentb4268672016-03-03 15:58:01 +0100770 allowblock = gfpflags_allow_blocking(gfp);
771 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000772
Rabin Vincentb4268672016-03-03 15:58:01 +0100773 if (cma)
774 buf->allocator = &cma_allocator;
Vladimir Murzin1655cf82017-05-24 11:24:32 +0100775 else if (is_coherent)
Rabin Vincentb4268672016-03-03 15:58:01 +0100776 buf->allocator = &simple_allocator;
777 else if (allowblock)
778 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000779 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100780 buf->allocator = &pool_allocator;
781
782 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000783
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100784 if (page) {
785 unsigned long flags;
786
Russell King9eedd962011-01-03 00:00:17 +0000787 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100788 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100789
790 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
791 list_add(&buf->list, &arm_dma_bufs);
792 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
793 } else {
794 kfree(buf);
795 }
Russell King31ebf942009-11-19 21:12:17 +0000796
Rabin Vincentb4268672016-03-03 15:58:01 +0100797 return args.want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100798}
Russell King695ae0a2009-11-19 16:31:39 +0000799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * Allocate DMA-coherent memory space and return both the kernel remapped
802 * virtual and bus address for that space.
803 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200804void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700805 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Russell King0ea1ec72013-10-23 16:14:59 +0100807 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400808
Rob Herringdd37e942012-08-21 12:20:17 +0200809 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100810 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200811}
812
813static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700814 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200815{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100816 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100817 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Mike Looijmans55af8a92015-06-03 11:25:31 +0100820static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200821 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700822 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Nathan Jonesc2a38312018-12-04 10:05:32 +0100824 int ret = -ENXIO;
Fabio Estevama70c3ee2018-04-23 12:30:27 +0100825 unsigned long nr_vma_pages = vma_pages(vma);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200826 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100827 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200828 unsigned long off = vma->vm_pgoff;
829
Vladimir Murzin43fc5092017-07-20 11:19:58 +0100830 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Marek Szyprowski47142f02012-05-15 19:04:13 +0200831 return ret;
832
Marek Szyprowski50262a42012-07-30 09:35:26 +0200833 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
834 ret = remap_pfn_range(vma, vma->vm_start,
835 pfn + off,
836 vma->vm_end - vma->vm_start,
837 vma->vm_page_prot);
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 return ret;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100844 * Create userspace mapping for the DMA-coherent memory.
845 */
846static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
847 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700848 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100849{
850 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
851}
852
853int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
854 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700855 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100856{
Mike Looijmans55af8a92015-06-03 11:25:31 +0100857 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100858 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
859}
860
861/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100862 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Rob Herringdd37e942012-08-21 12:20:17 +0200864static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700865 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +0200866 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100868 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100869 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100870 struct arm_dma_free_args args = {
871 .dev = dev,
872 .size = PAGE_ALIGN(size),
873 .cpu_addr = cpu_addr,
874 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700875 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +0100876 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100877
878 buf = arm_dma_buffer_find(cpu_addr);
879 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
880 return;
Russell King5edf71a2005-11-25 15:52:51 +0000881
Rabin Vincentb4268672016-03-03 15:58:01 +0100882 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100883 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Russell Kingafd1a322008-09-25 16:30:57 +0100885
Rob Herringdd37e942012-08-21 12:20:17 +0200886void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700887 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200888{
889 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
890}
891
892static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700893 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200894{
895 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
896}
897
Russell King916a0082017-03-29 17:12:47 +0100898/*
899 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
900 * that the intention is to allow exporting memory allocated via the
901 * coherent DMA APIs through the dma_buf API, which only accepts a
902 * scattertable. This presents a couple of problems:
903 * 1. Not all memory allocated via the coherent DMA APIs is backed by
904 * a struct page
905 * 2. Passing coherent DMA memory into the streaming APIs is not allowed
906 * as we will try to flush the memory through a different alias to that
907 * actually being used (and the flushes are redundant.)
908 */
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200909int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
910 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700911 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200912{
Russell King916a0082017-03-29 17:12:47 +0100913 unsigned long pfn = dma_to_pfn(dev, handle);
914 struct page *page;
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200915 int ret;
916
Russell King916a0082017-03-29 17:12:47 +0100917 /* If the PFN is not valid, we do not have a struct page */
918 if (!pfn_valid(pfn))
919 return -ENXIO;
920
921 page = pfn_to_page(pfn);
922
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200923 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
924 if (unlikely(ret))
925 return ret;
926
927 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
928 return 0;
929}
930
Russell King65af1912009-11-24 17:53:33 +0000931static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000932 size_t size, enum dma_data_direction dir,
933 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000934{
Russell King15653372013-01-19 11:05:57 +0000935 unsigned long pfn;
936 size_t left = size;
937
938 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
939 offset %= PAGE_SIZE;
940
Russell King65af1912009-11-24 17:53:33 +0000941 /*
942 * A single sg entry may refer to multiple physically contiguous
943 * pages. But we still need to process highmem pages individually.
944 * If highmem is not configured then the bulk of this loop gets
945 * optimized out.
946 */
Russell King65af1912009-11-24 17:53:33 +0000947 do {
948 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000949 void *vaddr;
950
Russell King15653372013-01-19 11:05:57 +0000951 page = pfn_to_page(pfn);
952
Russell King93f1d622009-11-24 14:41:01 +0000953 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000954 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000955 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100956
957 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500958 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100959 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500960 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100961 } else {
962 vaddr = kmap_high_get(page);
963 if (vaddr) {
964 op(vaddr + offset, len, dir);
965 kunmap_high(page);
966 }
Russell King93f1d622009-11-24 14:41:01 +0000967 }
968 } else {
969 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000970 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000971 }
Russell King65af1912009-11-24 17:53:33 +0000972 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000973 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000974 left -= len;
975 } while (left);
976}
977
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100978/*
979 * Make an area consistent for devices.
980 * Note: Drivers should NOT use this function directly, as it will break
981 * platforms with CONFIG_DMABOUNCE.
982 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
983 */
984static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000985 size_t size, enum dma_data_direction dir)
986{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400987 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400988
Russell Kinga9c91472009-11-26 16:19:58 +0000989 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400990
Russell King65af1912009-11-24 17:53:33 +0000991 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000992 if (dir == DMA_FROM_DEVICE) {
993 outer_inv_range(paddr, paddr + size);
994 } else {
995 outer_clean_range(paddr, paddr + size);
996 }
997 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400998}
Russell King4ea0d732009-11-24 16:27:17 +0000999
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001000static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001001 size_t size, enum dma_data_direction dir)
1002{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001003 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001004
1005 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001006 /* in any case, don't bother invalidating if DMA to device */
1007 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001008 outer_inv_range(paddr, paddr + size);
1009
Russell Kingdeace4a2014-05-03 11:06:55 +01001010 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1011 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001012
1013 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001014 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001015 */
Ming Leib2a234e2013-05-18 11:21:36 +01001016 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1017 unsigned long pfn;
1018 size_t left = size;
1019
1020 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1021 off %= PAGE_SIZE;
1022 if (off) {
1023 pfn++;
1024 left -= PAGE_SIZE - off;
1025 }
1026 while (left >= PAGE_SIZE) {
1027 page = pfn_to_page(pfn++);
1028 set_bit(PG_dcache_clean, &page->flags);
1029 left -= PAGE_SIZE;
1030 }
1031 }
Russell King4ea0d732009-11-24 16:27:17 +00001032}
Nicolas Pitre43377452009-03-12 22:52:09 -04001033
Russell Kingafd1a322008-09-25 16:30:57 +01001034/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001035 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001036 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1037 * @sg: list of buffers
1038 * @nents: number of buffers to map
1039 * @dir: DMA transfer direction
1040 *
1041 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1042 * This is the scatter-gather version of the dma_map_single interface.
1043 * Here the scatter gather list elements are each tagged with the
1044 * appropriate dma address and length. They are obtained via
1045 * sg_dma_{address,length}.
1046 *
1047 * Device ownership issues as mentioned for dma_map_single are the same
1048 * here.
1049 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001050int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001051 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001052{
Bart Van Assche52997092017-01-20 13:04:01 -08001053 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001054 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001055 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001056
1057 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001058#ifdef CONFIG_NEED_SG_DMA_LENGTH
1059 s->dma_length = s->length;
1060#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001061 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1062 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001063 if (dma_mapping_error(dev, s->dma_address))
1064 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001065 }
Russell Kingafd1a322008-09-25 16:30:57 +01001066 return nents;
Russell King01135d922008-09-25 21:05:02 +01001067
1068 bad_mapping:
1069 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001070 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001071 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001072}
Russell Kingafd1a322008-09-25 16:30:57 +01001073
1074/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001075 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001076 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1077 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001078 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001079 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1080 *
1081 * Unmap a set of streaming mode DMA translations. Again, CPU access
1082 * rules concerning calls here are the same as for dma_unmap_single().
1083 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001084void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001085 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001086{
Bart Van Assche52997092017-01-20 13:04:01 -08001087 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001088 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001089
Russell King01135d922008-09-25 21:05:02 +01001090 int i;
Russell King24056f52011-01-03 11:29:28 +00001091
Russell King01135d922008-09-25 21:05:02 +01001092 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001093 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001094}
Russell Kingafd1a322008-09-25 16:30:57 +01001095
1096/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001097 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001098 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1099 * @sg: list of buffers
1100 * @nents: number of buffers to map (returned from dma_map_sg)
1101 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1102 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001103void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001104 int nents, enum dma_data_direction dir)
1105{
Bart Van Assche52997092017-01-20 13:04:01 -08001106 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001107 struct scatterlist *s;
1108 int i;
1109
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001110 for_each_sg(sg, s, nents, i)
1111 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1112 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001113}
Russell Kingafd1a322008-09-25 16:30:57 +01001114
1115/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001116 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001117 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1118 * @sg: list of buffers
1119 * @nents: number of buffers to map (returned from dma_map_sg)
1120 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1121 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001122void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001123 int nents, enum dma_data_direction dir)
1124{
Bart Van Assche52997092017-01-20 13:04:01 -08001125 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001126 struct scatterlist *s;
1127 int i;
1128
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001129 for_each_sg(sg, s, nents, i)
1130 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1131 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001132}
Russell King24056f52011-01-03 11:29:28 +00001133
Russell King022ae532011-07-08 21:26:59 +01001134/*
1135 * Return whether the given device DMA address mask can be supported
1136 * properly. For example, if your device can only drive the low 24-bits
1137 * during bus mastering, then you would pass 0x00ffffff as the mask
1138 * to this function.
1139 */
Christoph Hellwig418a7a72017-05-22 11:20:18 +02001140int arm_dma_supported(struct device *dev, u64 mask)
Russell King022ae532011-07-08 21:26:59 +01001141{
Russell King9f28cde2013-12-06 12:30:42 +00001142 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001143}
Russell King022ae532011-07-08 21:26:59 +01001144
Thierry Reding18746192018-05-30 16:06:24 +02001145static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
1146{
1147 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
1148}
1149
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001150#ifdef CONFIG_ARM_DMA_USE_IOMMU
1151
Sricharan R7d2822d2017-01-06 18:58:13 +05301152static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
1153{
1154 int prot = 0;
1155
1156 if (attrs & DMA_ATTR_PRIVILEGED)
1157 prot |= IOMMU_PRIV;
1158
1159 switch (dir) {
1160 case DMA_BIDIRECTIONAL:
1161 return prot | IOMMU_READ | IOMMU_WRITE;
1162 case DMA_TO_DEVICE:
1163 return prot | IOMMU_READ;
1164 case DMA_FROM_DEVICE:
1165 return prot | IOMMU_WRITE;
1166 default:
1167 return prot;
1168 }
1169}
1170
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001171/* IOMMU */
1172
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001173static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1174
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001175static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1176 size_t size)
1177{
1178 unsigned int order = get_order(size);
1179 unsigned int align = 0;
1180 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301181 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001182 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001183 dma_addr_t iova;
1184 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001185
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001186 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1187 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1188
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001189 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1190 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001191
1192 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001193 for (i = 0; i < mapping->nr_bitmaps; i++) {
1194 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1195 mapping->bits, 0, count, align);
1196
1197 if (start > mapping->bits)
1198 continue;
1199
1200 bitmap_set(mapping->bitmaps[i], start, count);
1201 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001202 }
1203
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001204 /*
1205 * No unused range found. Try to extend the existing mapping
1206 * and perform a second attempt to reserve an IO virtual
1207 * address range of size bytes.
1208 */
1209 if (i == mapping->nr_bitmaps) {
1210 if (extend_iommu_mapping(mapping)) {
1211 spin_unlock_irqrestore(&mapping->lock, flags);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001212 return DMA_MAPPING_ERROR;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001213 }
1214
1215 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1216 mapping->bits, 0, count, align);
1217
1218 if (start > mapping->bits) {
1219 spin_unlock_irqrestore(&mapping->lock, flags);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001220 return DMA_MAPPING_ERROR;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001221 }
1222
1223 bitmap_set(mapping->bitmaps[i], start, count);
1224 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001225 spin_unlock_irqrestore(&mapping->lock, flags);
1226
Ritesh Harjani006f8412014-05-20 10:02:59 +05301227 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001228 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001229
1230 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001231}
1232
1233static inline void __free_iova(struct dma_iommu_mapping *mapping,
1234 dma_addr_t addr, size_t size)
1235{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001236 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301237 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001238 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001239 dma_addr_t bitmap_base;
1240 u32 bitmap_index;
1241
1242 if (!size)
1243 return;
1244
Ritesh Harjani006f8412014-05-20 10:02:59 +05301245 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001246 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1247
Ritesh Harjani006f8412014-05-20 10:02:59 +05301248 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001249
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001250 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001251
Ritesh Harjani006f8412014-05-20 10:02:59 +05301252 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001253 /*
1254 * The address range to be freed reaches into the iova
1255 * range of the next bitmap. This should not happen as
1256 * we don't allow this in __alloc_iova (at the
1257 * moment).
1258 */
1259 BUG();
1260 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001261 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001262
1263 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001264 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001265 spin_unlock_irqrestore(&mapping->lock, flags);
1266}
1267
Doug Anderson33298ef2016-01-29 23:06:08 +01001268/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1269static const int iommu_order_array[] = { 9, 8, 4, 0 };
1270
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001271static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001272 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001273 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001274{
1275 struct page **pages;
1276 int count = size >> PAGE_SHIFT;
1277 int array_size = count * sizeof(struct page *);
1278 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001279 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001280
1281 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001282 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001283 else
1284 pages = vzalloc(array_size);
1285 if (!pages)
1286 return NULL;
1287
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001288 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001289 {
1290 unsigned long order = get_order(size);
1291 struct page *page;
1292
Marek Szyprowskid834c5a2018-08-17 15:49:00 -07001293 page = dma_alloc_from_contiguous(dev, count, order,
1294 gfp & __GFP_NOWARN);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001295 if (!page)
1296 goto error;
1297
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001298 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001299
1300 for (i = 0; i < count; i++)
1301 pages[i] = page + i;
1302
1303 return pages;
1304 }
1305
Doug Anderson14d3ae22016-01-29 23:08:46 +01001306 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001307 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001308 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1309
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001310 /*
1311 * IOMMU can map any pages, so himem can also be used here
1312 */
1313 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1314
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001315 while (count) {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001316 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001317
Doug Anderson33298ef2016-01-29 23:06:08 +01001318 order = iommu_order_array[order_idx];
1319
1320 /* Drop down when we get small */
1321 if (__fls(count) < order) {
1322 order_idx++;
1323 continue;
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001324 }
1325
Doug Anderson33298ef2016-01-29 23:06:08 +01001326 if (order) {
1327 /* See if it's easy to allocate a high-order chunk */
1328 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1329
1330 /* Go down a notch at first sign of pressure */
1331 if (!pages[i]) {
1332 order_idx++;
1333 continue;
1334 }
1335 } else {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001336 pages[i] = alloc_pages(gfp, 0);
1337 if (!pages[i])
1338 goto error;
1339 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001340
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001341 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001342 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001343 j = 1 << order;
1344 while (--j)
1345 pages[i + j] = pages[i] + j;
1346 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001347
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001348 __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001349 i += 1 << order;
1350 count -= 1 << order;
1351 }
1352
1353 return pages;
1354error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001355 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001356 if (pages[i])
1357 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001358 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001359 return NULL;
1360}
1361
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001362static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001363 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001364{
1365 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001366 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001367
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001368 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001369 dma_release_from_contiguous(dev, pages[0], count);
1370 } else {
1371 for (i = 0; i < count; i++)
1372 if (pages[i])
1373 __free_pages(pages[i], 0);
1374 }
1375
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001376 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001377 return 0;
1378}
1379
1380/*
1381 * Create a CPU mapping for a specified pages
1382 */
1383static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001384__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1385 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001386{
Laura Abbott513510d2014-10-09 15:26:40 -07001387 return dma_common_pages_remap(pages, size,
1388 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001389}
1390
1391/*
1392 * Create a mapping in device IO address space for specified pages
1393 */
1394static dma_addr_t
Sricharan R7d2822d2017-01-06 18:58:13 +05301395__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1396 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001397{
Will Deacon89cfdb12015-01-16 18:02:15 +01001398 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001399 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1400 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001401 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001402
1403 dma_addr = __alloc_iova(mapping, size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001404 if (dma_addr == DMA_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001405 return dma_addr;
1406
1407 iova = dma_addr;
1408 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001409 int ret;
1410
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001411 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1412 phys_addr_t phys = page_to_phys(pages[i]);
1413 unsigned int len, j;
1414
1415 for (j = i + 1; j < count; j++, next_pfn++)
1416 if (page_to_pfn(pages[j]) != next_pfn)
1417 break;
1418
1419 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001420 ret = iommu_map(mapping->domain, iova, phys, len,
Sricharan R7d2822d2017-01-06 18:58:13 +05301421 __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001422 if (ret < 0)
1423 goto fail;
1424 iova += len;
1425 i = j;
1426 }
1427 return dma_addr;
1428fail:
1429 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1430 __free_iova(mapping, dma_addr, size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001431 return DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001432}
1433
1434static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1435{
Will Deacon89cfdb12015-01-16 18:02:15 +01001436 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001437
1438 /*
1439 * add optional in-page offset from iova to size and align
1440 * result to page size
1441 */
1442 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1443 iova &= PAGE_MASK;
1444
1445 iommu_unmap(mapping->domain, iova, size);
1446 __free_iova(mapping, iova, size);
1447 return 0;
1448}
1449
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001450static struct page **__atomic_get_pages(void *addr)
1451{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001452 struct page *page;
1453 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001454
Laura Abbott36d0fd22014-10-09 15:26:42 -07001455 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1456 page = phys_to_page(phys);
1457
1458 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001459}
1460
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001461static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001462{
1463 struct vm_struct *area;
1464
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001465 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1466 return __atomic_get_pages(cpu_addr);
1467
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001468 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001469 return cpu_addr;
1470
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001471 area = find_vm_area(cpu_addr);
1472 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1473 return area->pages;
1474 return NULL;
1475}
1476
Gregory CLEMENT56506822016-04-15 11:20:13 +01001477static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
Sricharan R7d2822d2017-01-06 18:58:13 +05301478 dma_addr_t *handle, int coherent_flag,
1479 unsigned long attrs)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001480{
1481 struct page *page;
1482 void *addr;
1483
Gregory CLEMENT56506822016-04-15 11:20:13 +01001484 if (coherent_flag == COHERENT)
1485 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1486 else
1487 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001488 if (!addr)
1489 return NULL;
1490
Sricharan R7d2822d2017-01-06 18:58:13 +05301491 *handle = __iommu_create_mapping(dev, &page, size, attrs);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001492 if (*handle == DMA_MAPPING_ERROR)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001493 goto err_mapping;
1494
1495 return addr;
1496
1497err_mapping:
1498 __free_from_pool(addr, size);
1499 return NULL;
1500}
1501
Marek Szyprowskid5898292013-02-08 10:54:48 +01001502static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001503 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001504{
1505 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001506 if (coherent_flag == COHERENT)
1507 __dma_free_buffer(virt_to_page(cpu_addr), size);
1508 else
1509 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001510}
1511
Gregory CLEMENT56506822016-04-15 11:20:13 +01001512static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001513 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001514 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001515{
Russell King71b55662013-11-25 12:01:03 +00001516 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001517 struct page **pages;
1518 void *addr = NULL;
1519
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001520 *handle = DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001521 size = PAGE_ALIGN(size);
1522
Gregory CLEMENT56506822016-04-15 11:20:13 +01001523 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1524 return __iommu_alloc_simple(dev, size, gfp, handle,
Sricharan R7d2822d2017-01-06 18:58:13 +05301525 coherent_flag, attrs);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001526
Richard Zhao5b91a982013-06-20 20:31:00 +08001527 /*
1528 * Following is a work-around (a.k.a. hack) to prevent pages
1529 * with __GFP_COMP being passed to split_page() which cannot
1530 * handle them. The real problem is that this flag probably
1531 * should be 0 on ARM as it is not supported on this
1532 * platform; see CONFIG_HUGETLBFS.
1533 */
1534 gfp &= ~(__GFP_COMP);
1535
Gregory CLEMENT56506822016-04-15 11:20:13 +01001536 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001537 if (!pages)
1538 return NULL;
1539
Sricharan R7d2822d2017-01-06 18:58:13 +05301540 *handle = __iommu_create_mapping(dev, pages, size, attrs);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001541 if (*handle == DMA_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001542 goto err_buffer;
1543
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001544 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001545 return pages;
1546
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001547 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1548 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001549 if (!addr)
1550 goto err_mapping;
1551
1552 return addr;
1553
1554err_mapping:
1555 __iommu_remove_mapping(dev, *handle, size);
1556err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001557 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001558 return NULL;
1559}
1560
Gregory CLEMENT56506822016-04-15 11:20:13 +01001561static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001562 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001563{
1564 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1565}
1566
1567static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001568 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001569{
1570 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1571}
1572
1573static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001574 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001575 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001576{
Marek Szyprowski955c7572012-05-16 19:38:58 +01001577 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001578 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Souptick Joarder62484612019-05-13 17:22:00 -07001579 int err;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001580
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001581 if (!pages)
1582 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001583
Souptick Joarder62484612019-05-13 17:22:00 -07001584 if (vma->vm_pgoff >= nr_pages)
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001585 return -ENXIO;
1586
Souptick Joarder62484612019-05-13 17:22:00 -07001587 err = vm_map_pages(vma, pages, nr_pages);
1588 if (err)
1589 pr_err("Remapping memory failed: %d\n", err);
Marek Szyprowski7e312102015-08-28 09:42:09 +01001590
Souptick Joarder62484612019-05-13 17:22:00 -07001591 return err;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001592}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001593static int arm_iommu_mmap_attrs(struct device *dev,
1594 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001595 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001596{
1597 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1598
1599 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1600}
1601
1602static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1603 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001604 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001605{
1606 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1607}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001608
1609/*
1610 * free a page as defined by the above mapping.
1611 * Must not be called with IRQs disabled.
1612 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001613void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001614 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001615{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001616 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001617 size = PAGE_ALIGN(size);
1618
Gregory CLEMENT56506822016-04-15 11:20:13 +01001619 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1620 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001621 return;
1622 }
1623
YoungJun Cho836bfa02013-06-17 13:18:52 +09001624 pages = __iommu_get_pages(cpu_addr, attrs);
1625 if (!pages) {
1626 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1627 return;
1628 }
1629
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001630 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001631 dma_common_free_remap(cpu_addr, size,
1632 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001633 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001634
1635 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001636 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001637}
1638
Gregory CLEMENT56506822016-04-15 11:20:13 +01001639void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001640 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001641{
1642 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1643}
1644
1645void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001646 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001647{
1648 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1649}
1650
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001651static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1652 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001653 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001654{
1655 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1656 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1657
1658 if (!pages)
1659 return -ENXIO;
1660
1661 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1662 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001663}
1664
1665/*
1666 * Map a part of the scatter-gather list into contiguous io address space
1667 */
1668static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1669 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001670 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001671 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001672{
Will Deacon89cfdb12015-01-16 18:02:15 +01001673 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001674 dma_addr_t iova, iova_base;
1675 int ret = 0;
1676 unsigned int count;
1677 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001678 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001679
1680 size = PAGE_ALIGN(size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001681 *handle = DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001682
1683 iova_base = iova = __alloc_iova(mapping, size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001684 if (iova == DMA_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001685 return -ENOMEM;
1686
1687 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001688 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001689 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1690
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001691 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001692 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1693
Sricharan R7d2822d2017-01-06 18:58:13 +05301694 prot = __dma_info_to_prot(dir, attrs);
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001695
1696 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001697 if (ret < 0)
1698 goto fail;
1699 count += len >> PAGE_SHIFT;
1700 iova += len;
1701 }
1702 *handle = iova_base;
1703
1704 return 0;
1705fail:
1706 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1707 __free_iova(mapping, iova_base, size);
1708 return ret;
1709}
1710
Rob Herring0fa478d2012-08-21 12:23:23 +02001711static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001712 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001713 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001714{
1715 struct scatterlist *s = sg, *dma = sg, *start = sg;
1716 int i, count = 0;
1717 unsigned int offset = s->offset;
1718 unsigned int size = s->offset + s->length;
1719 unsigned int max = dma_get_max_seg_size(dev);
1720
1721 for (i = 1; i < nents; i++) {
1722 s = sg_next(s);
1723
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001724 s->dma_address = DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001725 s->dma_length = 0;
1726
1727 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1728 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001729 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001730 goto bad_mapping;
1731
1732 dma->dma_address += offset;
1733 dma->dma_length = size - offset;
1734
1735 size = offset = s->offset;
1736 start = s;
1737 dma = sg_next(dma);
1738 count += 1;
1739 }
1740 size += s->length;
1741 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001742 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1743 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001744 goto bad_mapping;
1745
1746 dma->dma_address += offset;
1747 dma->dma_length = size - offset;
1748
1749 return count+1;
1750
1751bad_mapping:
1752 for_each_sg(sg, s, count, i)
1753 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1754 return 0;
1755}
1756
1757/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001758 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1759 * @dev: valid struct device pointer
1760 * @sg: list of buffers
1761 * @nents: number of buffers to map
1762 * @dir: DMA transfer direction
1763 *
1764 * Map a set of i/o coherent buffers described by scatterlist in streaming
1765 * mode for DMA. The scatter gather list elements are merged together (if
1766 * possible) and tagged with the appropriate dma address and length. They are
1767 * obtained via sg_dma_{address,length}.
1768 */
1769int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001770 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001771{
1772 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1773}
1774
1775/**
1776 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1777 * @dev: valid struct device pointer
1778 * @sg: list of buffers
1779 * @nents: number of buffers to map
1780 * @dir: DMA transfer direction
1781 *
1782 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1783 * The scatter gather list elements are merged together (if possible) and
1784 * tagged with the appropriate dma address and length. They are obtained via
1785 * sg_dma_{address,length}.
1786 */
1787int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001788 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001789{
1790 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1791}
1792
1793static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001794 int nents, enum dma_data_direction dir,
1795 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001796{
1797 struct scatterlist *s;
1798 int i;
1799
1800 for_each_sg(sg, s, nents, i) {
1801 if (sg_dma_len(s))
1802 __iommu_remove_mapping(dev, sg_dma_address(s),
1803 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001804 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001805 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1806 s->length, dir);
1807 }
1808}
1809
1810/**
1811 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1812 * @dev: valid struct device pointer
1813 * @sg: list of buffers
1814 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1815 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1816 *
1817 * Unmap a set of streaming mode DMA translations. Again, CPU access
1818 * rules concerning calls here are the same as for dma_unmap_single().
1819 */
1820void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001821 int nents, enum dma_data_direction dir,
1822 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001823{
1824 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1825}
1826
1827/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001828 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1829 * @dev: valid struct device pointer
1830 * @sg: list of buffers
1831 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1832 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1833 *
1834 * Unmap a set of streaming mode DMA translations. Again, CPU access
1835 * rules concerning calls here are the same as for dma_unmap_single().
1836 */
1837void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001838 enum dma_data_direction dir,
1839 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001840{
Rob Herring0fa478d2012-08-21 12:23:23 +02001841 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001842}
1843
1844/**
1845 * arm_iommu_sync_sg_for_cpu
1846 * @dev: valid struct device pointer
1847 * @sg: list of buffers
1848 * @nents: number of buffers to map (returned from dma_map_sg)
1849 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1850 */
1851void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1852 int nents, enum dma_data_direction dir)
1853{
1854 struct scatterlist *s;
1855 int i;
1856
1857 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001858 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001859
1860}
1861
1862/**
1863 * arm_iommu_sync_sg_for_device
1864 * @dev: valid struct device pointer
1865 * @sg: list of buffers
1866 * @nents: number of buffers to map (returned from dma_map_sg)
1867 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1868 */
1869void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1870 int nents, enum dma_data_direction dir)
1871{
1872 struct scatterlist *s;
1873 int i;
1874
1875 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001876 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001877}
1878
1879
1880/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001881 * arm_coherent_iommu_map_page
1882 * @dev: valid struct device pointer
1883 * @page: page that buffer resides in
1884 * @offset: offset into page for start of buffer
1885 * @size: size of buffer to map
1886 * @dir: DMA transfer direction
1887 *
1888 * Coherent IOMMU aware version of arm_dma_map_page()
1889 */
1890static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1891 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001892 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001893{
Will Deacon89cfdb12015-01-16 18:02:15 +01001894 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001895 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001896 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001897
1898 dma_addr = __alloc_iova(mapping, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001899 if (dma_addr == DMA_MAPPING_ERROR)
Rob Herring0fa478d2012-08-21 12:23:23 +02001900 return dma_addr;
1901
Sricharan R7d2822d2017-01-06 18:58:13 +05301902 prot = __dma_info_to_prot(dir, attrs);
Will Deacon13987d62013-06-10 19:34:39 +01001903
1904 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001905 if (ret < 0)
1906 goto fail;
1907
1908 return dma_addr + offset;
1909fail:
1910 __free_iova(mapping, dma_addr, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001911 return DMA_MAPPING_ERROR;
Rob Herring0fa478d2012-08-21 12:23:23 +02001912}
1913
1914/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001915 * arm_iommu_map_page
1916 * @dev: valid struct device pointer
1917 * @page: page that buffer resides in
1918 * @offset: offset into page for start of buffer
1919 * @size: size of buffer to map
1920 * @dir: DMA transfer direction
1921 *
1922 * IOMMU aware version of arm_dma_map_page()
1923 */
1924static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1925 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001926 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001927{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001928 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001929 __dma_page_cpu_to_dev(page, offset, size, dir);
1930
Rob Herring0fa478d2012-08-21 12:23:23 +02001931 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1932}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001933
Rob Herring0fa478d2012-08-21 12:23:23 +02001934/**
1935 * arm_coherent_iommu_unmap_page
1936 * @dev: valid struct device pointer
1937 * @handle: DMA address of buffer
1938 * @size: size of buffer (same as passed to dma_map_page)
1939 * @dir: DMA transfer direction (same as passed to dma_map_page)
1940 *
1941 * Coherent IOMMU aware version of arm_dma_unmap_page()
1942 */
1943static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001944 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001945{
Will Deacon89cfdb12015-01-16 18:02:15 +01001946 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001947 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001948 int offset = handle & ~PAGE_MASK;
1949 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001950
Rob Herring0fa478d2012-08-21 12:23:23 +02001951 if (!iova)
1952 return;
1953
1954 iommu_unmap(mapping->domain, iova, len);
1955 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001956}
1957
1958/**
1959 * arm_iommu_unmap_page
1960 * @dev: valid struct device pointer
1961 * @handle: DMA address of buffer
1962 * @size: size of buffer (same as passed to dma_map_page)
1963 * @dir: DMA transfer direction (same as passed to dma_map_page)
1964 *
1965 * IOMMU aware version of arm_dma_unmap_page()
1966 */
1967static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001968 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001969{
Will Deacon89cfdb12015-01-16 18:02:15 +01001970 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001971 dma_addr_t iova = handle & PAGE_MASK;
1972 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1973 int offset = handle & ~PAGE_MASK;
1974 int len = PAGE_ALIGN(size + offset);
1975
1976 if (!iova)
1977 return;
1978
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001979 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001980 __dma_page_dev_to_cpu(page, offset, size, dir);
1981
1982 iommu_unmap(mapping->domain, iova, len);
1983 __free_iova(mapping, iova, len);
1984}
1985
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02001986/**
1987 * arm_iommu_map_resource - map a device resource for DMA
1988 * @dev: valid struct device pointer
1989 * @phys_addr: physical address of resource
1990 * @size: size of resource to map
1991 * @dir: DMA transfer direction
1992 */
1993static dma_addr_t arm_iommu_map_resource(struct device *dev,
1994 phys_addr_t phys_addr, size_t size,
1995 enum dma_data_direction dir, unsigned long attrs)
1996{
1997 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1998 dma_addr_t dma_addr;
1999 int ret, prot;
2000 phys_addr_t addr = phys_addr & PAGE_MASK;
2001 unsigned int offset = phys_addr & ~PAGE_MASK;
2002 size_t len = PAGE_ALIGN(size + offset);
2003
2004 dma_addr = __alloc_iova(mapping, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01002005 if (dma_addr == DMA_MAPPING_ERROR)
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002006 return dma_addr;
2007
Sricharan R7d2822d2017-01-06 18:58:13 +05302008 prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002009
2010 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2011 if (ret < 0)
2012 goto fail;
2013
2014 return dma_addr + offset;
2015fail:
2016 __free_iova(mapping, dma_addr, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01002017 return DMA_MAPPING_ERROR;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002018}
2019
2020/**
2021 * arm_iommu_unmap_resource - unmap a device DMA resource
2022 * @dev: valid struct device pointer
2023 * @dma_handle: DMA address to resource
2024 * @size: size of resource to map
2025 * @dir: DMA transfer direction
2026 */
2027static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2028 size_t size, enum dma_data_direction dir,
2029 unsigned long attrs)
2030{
2031 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2032 dma_addr_t iova = dma_handle & PAGE_MASK;
2033 unsigned int offset = dma_handle & ~PAGE_MASK;
2034 size_t len = PAGE_ALIGN(size + offset);
2035
2036 if (!iova)
2037 return;
2038
2039 iommu_unmap(mapping->domain, iova, len);
2040 __free_iova(mapping, iova, len);
2041}
2042
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002043static void arm_iommu_sync_single_for_cpu(struct device *dev,
2044 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2045{
Will Deacon89cfdb12015-01-16 18:02:15 +01002046 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002047 dma_addr_t iova = handle & PAGE_MASK;
2048 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2049 unsigned int offset = handle & ~PAGE_MASK;
2050
2051 if (!iova)
2052 return;
2053
Rob Herring0fa478d2012-08-21 12:23:23 +02002054 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002055}
2056
2057static void arm_iommu_sync_single_for_device(struct device *dev,
2058 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2059{
Will Deacon89cfdb12015-01-16 18:02:15 +01002060 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002061 dma_addr_t iova = handle & PAGE_MASK;
2062 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2063 unsigned int offset = handle & ~PAGE_MASK;
2064
2065 if (!iova)
2066 return;
2067
2068 __dma_page_cpu_to_dev(page, offset, size, dir);
2069}
2070
Bart Van Assche52997092017-01-20 13:04:01 -08002071const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002072 .alloc = arm_iommu_alloc_attrs,
2073 .free = arm_iommu_free_attrs,
2074 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002075 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002076
2077 .map_page = arm_iommu_map_page,
2078 .unmap_page = arm_iommu_unmap_page,
2079 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2080 .sync_single_for_device = arm_iommu_sync_single_for_device,
2081
2082 .map_sg = arm_iommu_map_sg,
2083 .unmap_sg = arm_iommu_unmap_sg,
2084 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2085 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002086
2087 .map_resource = arm_iommu_map_resource,
2088 .unmap_resource = arm_iommu_unmap_resource,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002089
Christoph Hellwig418a7a72017-05-22 11:20:18 +02002090 .dma_supported = arm_dma_supported,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002091};
2092
Bart Van Assche52997092017-01-20 13:04:01 -08002093const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002094 .alloc = arm_coherent_iommu_alloc_attrs,
2095 .free = arm_coherent_iommu_free_attrs,
2096 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002097 .get_sgtable = arm_iommu_get_sgtable,
2098
2099 .map_page = arm_coherent_iommu_map_page,
2100 .unmap_page = arm_coherent_iommu_unmap_page,
2101
2102 .map_sg = arm_coherent_iommu_map_sg,
2103 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002104
2105 .map_resource = arm_iommu_map_resource,
2106 .unmap_resource = arm_iommu_unmap_resource,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002107
Christoph Hellwig418a7a72017-05-22 11:20:18 +02002108 .dma_supported = arm_dma_supported,
Rob Herring0fa478d2012-08-21 12:23:23 +02002109};
2110
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002111/**
2112 * arm_iommu_create_mapping
2113 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2114 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002115 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002116 *
2117 * Creates a mapping structure which holds information about used/unused
2118 * IO address ranges, which is required to perform memory allocation and
2119 * mapping with IOMMU aware functions.
2120 *
2121 * The client device need to be attached to the mapping with
2122 * arm_iommu_attach_device function.
2123 */
2124struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002125arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002126{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002127 unsigned int bits = size >> PAGE_SHIFT;
2128 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002129 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002130 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002131 int err = -ENOMEM;
2132
Marek Szyprowski14245322015-04-29 11:29:19 +01002133 /* currently only 32-bit DMA address space is supported */
2134 if (size > DMA_BIT_MASK(32) + 1)
2135 return ERR_PTR(-ERANGE);
2136
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002137 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002138 return ERR_PTR(-EINVAL);
2139
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002140 if (bitmap_size > PAGE_SIZE) {
2141 extensions = bitmap_size / PAGE_SIZE;
2142 bitmap_size = PAGE_SIZE;
2143 }
2144
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002145 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2146 if (!mapping)
2147 goto err;
2148
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002149 mapping->bitmap_size = bitmap_size;
Kees Cook6396bb22018-06-12 14:03:40 -07002150 mapping->bitmaps = kcalloc(extensions, sizeof(unsigned long *),
2151 GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002152 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002153 goto err2;
2154
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002155 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002156 if (!mapping->bitmaps[0])
2157 goto err3;
2158
2159 mapping->nr_bitmaps = 1;
2160 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002161 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002162 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002163
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002164 spin_lock_init(&mapping->lock);
2165
2166 mapping->domain = iommu_domain_alloc(bus);
2167 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002168 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002169
2170 kref_init(&mapping->kref);
2171 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002172err4:
2173 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002174err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002175 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002176err2:
2177 kfree(mapping);
2178err:
2179 return ERR_PTR(err);
2180}
Prathyush K18177d12013-01-04 06:22:42 -05002181EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002182
2183static void release_iommu_mapping(struct kref *kref)
2184{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002185 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002186 struct dma_iommu_mapping *mapping =
2187 container_of(kref, struct dma_iommu_mapping, kref);
2188
2189 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002190 for (i = 0; i < mapping->nr_bitmaps; i++)
2191 kfree(mapping->bitmaps[i]);
2192 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002193 kfree(mapping);
2194}
2195
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002196static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2197{
2198 int next_bitmap;
2199
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002200 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002201 return -EINVAL;
2202
2203 next_bitmap = mapping->nr_bitmaps;
2204 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2205 GFP_ATOMIC);
2206 if (!mapping->bitmaps[next_bitmap])
2207 return -ENOMEM;
2208
2209 mapping->nr_bitmaps++;
2210
2211 return 0;
2212}
2213
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002214void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2215{
2216 if (mapping)
2217 kref_put(&mapping->kref, release_iommu_mapping);
2218}
Prathyush K18177d12013-01-04 06:22:42 -05002219EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002220
Laurent Pincharteab8d652015-01-23 16:21:49 +02002221static int __arm_iommu_attach_device(struct device *dev,
2222 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002223{
2224 int err;
2225
2226 err = iommu_attach_device(mapping->domain, dev);
2227 if (err)
2228 return err;
2229
2230 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002231 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002232
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002233 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002234 return 0;
2235}
2236
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002237/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002238 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002239 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002240 * @mapping: io address space mapping structure (returned from
2241 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002242 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002243 * Attaches specified io address space mapping to the provided device.
2244 * This replaces the dma operations (dma_map_ops pointer) with the
2245 * IOMMU aware version.
2246 *
2247 * More than one client might be attached to the same io address space
2248 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002249 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002250int arm_iommu_attach_device(struct device *dev,
2251 struct dma_iommu_mapping *mapping)
2252{
2253 int err;
2254
2255 err = __arm_iommu_attach_device(dev, mapping);
2256 if (err)
2257 return err;
2258
2259 set_dma_ops(dev, &iommu_ops);
2260 return 0;
2261}
2262EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2263
Sricharan Rd3e01c52017-05-27 19:17:45 +05302264/**
2265 * arm_iommu_detach_device
2266 * @dev: valid struct device pointer
2267 *
2268 * Detaches the provided device from a previously attached map.
Wolfram Sang (Renesas)4a4d68f2018-12-19 00:12:07 +01002269 * This overwrites the dma_ops pointer with appropriate non-IOMMU ops.
Sricharan Rd3e01c52017-05-27 19:17:45 +05302270 */
2271void arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002272{
2273 struct dma_iommu_mapping *mapping;
2274
2275 mapping = to_dma_iommu_mapping(dev);
2276 if (!mapping) {
2277 dev_warn(dev, "Not attached\n");
2278 return;
2279 }
2280
2281 iommu_detach_device(mapping->domain, dev);
2282 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002283 to_dma_iommu_mapping(dev) = NULL;
Thierry Reding18746192018-05-30 16:06:24 +02002284 set_dma_ops(dev, arm_get_dma_map_ops(dev->archdata.dma_coherent));
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002285
2286 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2287}
Prathyush K18177d12013-01-04 06:22:42 -05002288EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002289
Bart Van Assche52997092017-01-20 13:04:01 -08002290static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002291{
2292 return coherent ? &iommu_coherent_ops : &iommu_ops;
2293}
2294
2295static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002296 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002297{
2298 struct dma_iommu_mapping *mapping;
2299
2300 if (!iommu)
2301 return false;
2302
2303 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2304 if (IS_ERR(mapping)) {
2305 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2306 size, dev_name(dev));
2307 return false;
2308 }
2309
Laurent Pincharteab8d652015-01-23 16:21:49 +02002310 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002311 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2312 dev_name(dev));
2313 arm_iommu_release_mapping(mapping);
2314 return false;
2315 }
2316
2317 return true;
2318}
2319
2320static void arm_teardown_iommu_dma_ops(struct device *dev)
2321{
Will Deacon89cfdb12015-01-16 18:02:15 +01002322 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002323
Will Deaconc2273a12015-01-16 18:01:43 +01002324 if (!mapping)
2325 return;
2326
Sricharan Rd3e01c52017-05-27 19:17:45 +05302327 arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002328 arm_iommu_release_mapping(mapping);
2329}
2330
2331#else
2332
2333static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002334 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002335{
2336 return false;
2337}
2338
2339static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2340
2341#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2342
2343#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2344
Will Deacon4bb25782014-08-27 17:52:44 +01002345void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002346 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002347{
Bart Van Assche52997092017-01-20 13:04:01 -08002348 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002349
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002350 dev->archdata.dma_coherent = coherent;
Laurent Pinchart26b37b92015-05-15 02:00:02 +03002351
2352 /*
2353 * Don't override the dma_ops if they have already been set. Ideally
2354 * this should be the only location where dma_ops are set, remove this
2355 * check when all other callers of set_dma_ops will have disappeared.
2356 */
2357 if (dev->dma_ops)
2358 return;
2359
Will Deacon4bb25782014-08-27 17:52:44 +01002360 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2361 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2362 else
2363 dma_ops = arm_get_dma_map_ops(coherent);
2364
2365 set_dma_ops(dev, dma_ops);
Stefano Stabellinie0586322017-04-13 14:04:21 -07002366
2367#ifdef CONFIG_XEN
2368 if (xen_initial_domain()) {
2369 dev->archdata.dev_dma_ops = dev->dma_ops;
2370 dev->dma_ops = xen_dma_ops;
2371 }
2372#endif
Laurent Pincharta93a1212017-05-27 19:17:43 +05302373 dev->archdata.dma_ops_setup = true;
Will Deacon4bb25782014-08-27 17:52:44 +01002374}
2375
2376void arch_teardown_dma_ops(struct device *dev)
2377{
Laurent Pincharta93a1212017-05-27 19:17:43 +05302378 if (!dev->archdata.dma_ops_setup)
2379 return;
2380
Will Deacon4bb25782014-08-27 17:52:44 +01002381 arm_teardown_iommu_dma_ops(dev);
Robin Murphyfc67e6f2019-02-06 18:43:24 +01002382 /* Let arch_setup_dma_ops() start again from scratch upon re-probe */
2383 set_dma_ops(dev, NULL);
Will Deacon4bb25782014-08-27 17:52:44 +01002384}