blob: ff131928be50bc583269fca91362f5fce1735281 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0ddbccd2008-09-25 15:59:19 +01002 * linux/arch/arm/mm/dma-mapping.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000-2004 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * DMA uncached mapping support.
11 */
Russell King11a5aa32013-11-25 21:52:25 +000012#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/mm.h>
Laura Abbott36d0fd22014-10-09 15:26:42 -070015#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/list.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010022#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050023#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010024#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010025#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020026#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020027#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020028#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010029#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070030#include <linux/cma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010032#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040033#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010036#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020037#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010038#include <asm/mach/map.h>
39#include <asm/system_info.h>
40#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Russell King1234e3f2015-07-24 09:10:55 +010042#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010043#include "mm.h"
44
Rabin Vincentb4268672016-03-03 15:58:01 +010045struct arm_dma_alloc_args {
46 struct device *dev;
47 size_t size;
48 gfp_t gfp;
49 pgprot_t prot;
50 const void *caller;
51 bool want_vaddr;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010052 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010053};
54
55struct arm_dma_free_args {
56 struct device *dev;
57 size_t size;
58 void *cpu_addr;
59 struct page *page;
60 bool want_vaddr;
61};
62
Gregory CLEMENTf1270892016-04-15 11:15:18 +010063#define NORMAL 0
64#define COHERENT 1
65
Rabin Vincentb4268672016-03-03 15:58:01 +010066struct arm_dma_allocator {
67 void *(*alloc)(struct arm_dma_alloc_args *args,
68 struct page **ret_page);
69 void (*free)(struct arm_dma_free_args *args);
70};
71
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010072struct arm_dma_buffer {
73 struct list_head list;
74 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010075 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010076};
77
78static LIST_HEAD(arm_dma_bufs);
79static DEFINE_SPINLOCK(arm_dma_bufs_lock);
80
81static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
82{
83 struct arm_dma_buffer *buf, *found = NULL;
84 unsigned long flags;
85
86 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
87 list_for_each_entry(buf, &arm_dma_bufs, list) {
88 if (buf->virt == virt) {
89 list_del(&buf->list);
90 found = buf;
91 break;
92 }
93 }
94 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
95 return found;
96}
97
Marek Szyprowski15237e12012-02-10 19:55:20 +010098/*
99 * The DMA API is built upon the notion of "buffer ownership". A buffer
100 * is either exclusively owned by the CPU (and therefore may be accessed
101 * by it) or exclusively owned by the DMA device. These helper functions
102 * represent the transitions between these two ownership states.
103 *
104 * Note, however, that on later ARMs, this notion does not work due to
105 * speculative prefetches. We model our approach on the assumption that
106 * the CPU does do speculative prefetches, which means we clean caches
107 * before transfers and delay cache invalidation until transfer completion.
108 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100109 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100110static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100111 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100112static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100113 size_t, enum dma_data_direction);
114
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100115/**
116 * arm_dma_map_page - map a portion of a page for streaming DMA
117 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
118 * @page: page that buffer resides in
119 * @offset: offset into page for start of buffer
120 * @size: size of buffer to map
121 * @dir: DMA transfer direction
122 *
123 * Ensure that any data held in the cache is appropriately discarded
124 * or written back.
125 *
126 * The device owns this memory once this call has completed. The CPU
127 * can regain ownership by calling dma_unmap_page().
128 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100129static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100130 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700131 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100132{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700133 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100134 __dma_page_cpu_to_dev(page, offset, size, dir);
135 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100136}
137
Rob Herringdd37e942012-08-21 12:20:17 +0200138static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
139 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700140 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200141{
142 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
143}
144
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100145/**
146 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
147 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
148 * @handle: DMA address of buffer
149 * @size: size of buffer (same as passed to dma_map_page)
150 * @dir: DMA transfer direction (same as passed to dma_map_page)
151 *
152 * Unmap a page streaming mode DMA translation. The handle and size
153 * must match what was provided in the previous dma_map_page() call.
154 * All other usages are undefined.
155 *
156 * After this call, reads by the CPU to the buffer are guaranteed to see
157 * whatever the device wrote there.
158 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100159static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700160 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100161{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700162 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100163 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
164 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100165}
166
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100167static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100168 dma_addr_t handle, size_t size, enum dma_data_direction dir)
169{
170 unsigned int offset = handle & (PAGE_SIZE - 1);
171 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200172 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100173}
174
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100175static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100176 dma_addr_t handle, size_t size, enum dma_data_direction dir)
177{
178 unsigned int offset = handle & (PAGE_SIZE - 1);
179 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200180 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100181}
182
Bart Van Assche52997092017-01-20 13:04:01 -0800183const struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200184 .alloc = arm_dma_alloc,
185 .free = arm_dma_free,
186 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200187 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100188 .map_page = arm_dma_map_page,
189 .unmap_page = arm_dma_unmap_page,
190 .map_sg = arm_dma_map_sg,
191 .unmap_sg = arm_dma_unmap_sg,
192 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
193 .sync_single_for_device = arm_dma_sync_single_for_device,
194 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
195 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100196};
197EXPORT_SYMBOL(arm_dma_ops);
198
Rob Herringdd37e942012-08-21 12:20:17 +0200199static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700200 dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200201static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700202 dma_addr_t handle, unsigned long attrs);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100203static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
204 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700205 unsigned long attrs);
Rob Herringdd37e942012-08-21 12:20:17 +0200206
Bart Van Assche52997092017-01-20 13:04:01 -0800207const struct dma_map_ops arm_coherent_dma_ops = {
Rob Herringdd37e942012-08-21 12:20:17 +0200208 .alloc = arm_coherent_dma_alloc,
209 .free = arm_coherent_dma_free,
Mike Looijmans55af8a92015-06-03 11:25:31 +0100210 .mmap = arm_coherent_dma_mmap,
Rob Herringdd37e942012-08-21 12:20:17 +0200211 .get_sgtable = arm_dma_get_sgtable,
212 .map_page = arm_coherent_dma_map_page,
213 .map_sg = arm_dma_map_sg,
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
Catalin Marinasab6494f2009-07-24 12:35:02 +0100347#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Marek Szyprowskic7909502011-12-29 13:09:51 +0100349static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100350 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100351 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800352 int coherent_flag, gfp_t gfp);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100353
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200354static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
355 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100356 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200357
358static void *
359__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
360 const void *caller)
361{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200362 /*
363 * DMA allocation can be mapped to user space, so lets
364 * set VM_USERMAP flags too.
365 */
Laura Abbott513510d2014-10-09 15:26:40 -0700366 return dma_common_contiguous_remap(page, size,
367 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
368 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200369}
370
371static void __dma_free_remap(void *cpu_addr, size_t size)
372{
Laura Abbott513510d2014-10-09 15:26:40 -0700373 dma_common_free_remap(cpu_addr, size,
374 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200375}
376
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200377#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Laura Abbott36d0fd22014-10-09 15:26:42 -0700378static struct gen_pool *atomic_pool;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200379
Laura Abbott36d0fd22014-10-09 15:26:42 -0700380static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100381
382static int __init early_coherent_pool(char *p)
383{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700384 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100385 return 0;
386}
387early_param("coherent_pool", early_coherent_pool);
388
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200389void __init init_dma_coherent_pool_size(unsigned long size)
390{
391 /*
392 * Catch any attempt to set the pool size too late.
393 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700394 BUG_ON(atomic_pool);
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200395
396 /*
397 * Set architecture specific coherent pool size only if
398 * it has not been changed by kernel command line parameter.
399 */
Laura Abbott36d0fd22014-10-09 15:26:42 -0700400 if (atomic_pool_size == DEFAULT_DMA_COHERENT_POOL_SIZE)
401 atomic_pool_size = size;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200402}
403
Marek Szyprowskic7909502011-12-29 13:09:51 +0100404/*
405 * Initialise the coherent pool for atomic allocations.
406 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200407static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100408{
Russell King71b55662013-11-25 12:01:03 +0000409 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100410 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100411 struct page *page;
412 void *ptr;
413
Laura Abbott36d0fd22014-10-09 15:26:42 -0700414 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
415 if (!atomic_pool)
416 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100417 /*
418 * The atomic pool is only used for non-coherent allocations
419 * so we must pass NORMAL for coherent_flag.
420 */
Gioh Kime464ef12014-05-22 13:38:23 +0900421 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700422 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Lucas Stach712c6042017-02-24 14:58:44 -0800423 &page, atomic_pool_init, true, NORMAL,
424 GFP_KERNEL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200425 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700426 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100427 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100428 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700429 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300430
Laura Abbott36d0fd22014-10-09 15:26:42 -0700431 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
432 page_to_phys(page),
433 atomic_pool_size, -1);
434 if (ret)
435 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300436
Laura Abbott36d0fd22014-10-09 15:26:42 -0700437 gen_pool_set_algo(atomic_pool,
438 gen_pool_first_fit_order_align,
439 (void *)PAGE_SHIFT);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100440 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700441 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100442 return 0;
443 }
Sachin Kamatec106652012-09-24 08:35:03 +0200444
Laura Abbott36d0fd22014-10-09 15:26:42 -0700445destroy_genpool:
446 gen_pool_destroy(atomic_pool);
447 atomic_pool = NULL;
448out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100449 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700450 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100451 return -ENOMEM;
452}
453/*
454 * CMA is activated by core_initcall, so we must be called after it.
455 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200456postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100457
458struct dma_contig_early_reserve {
459 phys_addr_t base;
460 unsigned long size;
461};
462
463static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
464
465static int dma_mmu_remap_num __initdata;
466
467void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
468{
469 dma_mmu_remap[dma_mmu_remap_num].base = base;
470 dma_mmu_remap[dma_mmu_remap_num].size = size;
471 dma_mmu_remap_num++;
472}
473
474void __init dma_contiguous_remap(void)
475{
476 int i;
477 for (i = 0; i < dma_mmu_remap_num; i++) {
478 phys_addr_t start = dma_mmu_remap[i].base;
479 phys_addr_t end = start + dma_mmu_remap[i].size;
480 struct map_desc map;
481 unsigned long addr;
482
483 if (end > arm_lowmem_limit)
484 end = arm_lowmem_limit;
485 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200486 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100487
488 map.pfn = __phys_to_pfn(start);
489 map.virtual = __phys_to_virt(start);
490 map.length = end - start;
491 map.type = MT_MEMORY_DMA_READY;
492
493 /*
Russell King6b076992014-07-17 12:17:45 +0100494 * Clear previous low-memory mapping to ensure that the
495 * TLB does not see any conflicting entries, then flush
496 * the TLB of the old entries before creating new mappings.
497 *
498 * This ensures that any speculatively loaded TLB entries
499 * (even though they may be rare) can not cause any problems,
500 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100501 */
502 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400503 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100504 pmd_clear(pmd_off_k(addr));
505
Russell King6b076992014-07-17 12:17:45 +0100506 flush_tlb_kernel_range(__phys_to_virt(start),
507 __phys_to_virt(end));
508
Marek Szyprowskic7909502011-12-29 13:09:51 +0100509 iotable_init(&map, 1);
510 }
511}
512
Marek Szyprowskic7909502011-12-29 13:09:51 +0100513static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
514 void *data)
515{
516 struct page *page = virt_to_page(addr);
517 pgprot_t prot = *(pgprot_t *)data;
518
519 set_pte_ext(pte, mk_pte(page, prot), 0);
520 return 0;
521}
522
523static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
524{
525 unsigned long start = (unsigned long) page_address(page);
526 unsigned end = start + size;
527
528 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100529 flush_tlb_kernel_range(start, end);
530}
531
532static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
533 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100534 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100535{
536 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100537 void *ptr = NULL;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100538 /*
539 * __alloc_remap_buffer is only called when the device is
540 * non-coherent
541 */
542 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100543 if (!page)
544 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100545 if (!want_vaddr)
546 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100547
548 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
549 if (!ptr) {
550 __dma_free_buffer(page, size);
551 return NULL;
552 }
553
Carlo Caione6e8266e2015-02-09 10:38:35 +0100554 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100555 *ret_page = page;
556 return ptr;
557}
558
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200559static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100560{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700561 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200562 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100563
Laura Abbott36d0fd22014-10-09 15:26:42 -0700564 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200565 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100566 return NULL;
567 }
568
Laura Abbott36d0fd22014-10-09 15:26:42 -0700569 val = gen_pool_alloc(atomic_pool, size);
570 if (val) {
571 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200572
Laura Abbott36d0fd22014-10-09 15:26:42 -0700573 *ret_page = phys_to_page(phys);
574 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100575 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200576
577 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100578}
579
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300580static bool __in_atomic_pool(void *start, size_t size)
581{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700582 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300583}
584
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200585static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100586{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300587 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100588 return 0;
589
Laura Abbott36d0fd22014-10-09 15:26:42 -0700590 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200591
Marek Szyprowskic7909502011-12-29 13:09:51 +0100592 return 1;
593}
594
595static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100596 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100597 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800598 int coherent_flag, gfp_t gfp)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100599{
600 unsigned long order = get_order(size);
601 size_t count = size >> PAGE_SHIFT;
602 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100603 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100604
Lucas Stach712c6042017-02-24 14:58:44 -0800605 page = dma_alloc_from_contiguous(dev, count, order, gfp);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100606 if (!page)
607 return NULL;
608
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100609 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100610
Carlo Caione6e8266e2015-02-09 10:38:35 +0100611 if (!want_vaddr)
612 goto out;
613
Marek Szyprowski9848e482013-01-16 15:38:44 +0100614 if (PageHighMem(page)) {
615 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
616 if (!ptr) {
617 dma_release_from_contiguous(dev, page, count);
618 return NULL;
619 }
620 } else {
621 __dma_remap(page, size, prot);
622 ptr = page_address(page);
623 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100624
625 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100626 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100627 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100628}
629
630static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100631 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100632{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100633 if (want_vaddr) {
634 if (PageHighMem(page))
635 __dma_free_remap(cpu_addr, size);
636 else
637 __dma_remap(page, size, PAGE_KERNEL);
638 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100639 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
640}
641
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700642static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200643{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700644 prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
645 pgprot_writecombine(prot) :
646 pgprot_dmacoherent(prot);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200647 return prot;
648}
649
Marek Szyprowskic7909502011-12-29 13:09:51 +0100650#define nommu() 0
651
Catalin Marinasab6494f2009-07-24 12:35:02 +0100652#else /* !CONFIG_MMU */
Russell King695ae0a2009-11-19 16:31:39 +0000653
Marek Szyprowskic7909502011-12-29 13:09:51 +0100654#define nommu() 1
655
Carlo Caione6e8266e2015-02-09 10:38:35 +0100656#define __get_dma_pgprot(attrs, prot) __pgprot(0)
657#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv) NULL
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200658#define __alloc_from_pool(size, ret_page) NULL
Lucas Stach712c6042017-02-24 14:58:44 -0800659#define __alloc_from_contiguous(dev, size, prot, ret, c, wv, coherent_flag, gfp) NULL
Rabin Vincentb4268672016-03-03 15:58:01 +0100660#define __free_from_pool(cpu_addr, size) do { } while (0)
Carlo Caione6e8266e2015-02-09 10:38:35 +0100661#define __free_from_contiguous(dev, page, cpu_addr, size, wv) do { } while (0)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100662#define __dma_free_remap(cpu_addr, size) do { } while (0)
Russell King31ebf942009-11-19 21:12:17 +0000663
664#endif /* CONFIG_MMU */
665
Marek Szyprowskic7909502011-12-29 13:09:51 +0100666static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
667 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100668{
Russell King04da5692009-11-19 15:54:45 +0000669 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100670 /* __alloc_simple_buffer is only called when the device is coherent */
671 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100672 if (!page)
673 return NULL;
674
675 *ret_page = page;
676 return page_address(page);
677}
678
Rabin Vincentb4268672016-03-03 15:58:01 +0100679static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
680 struct page **ret_page)
681{
682 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
683 ret_page);
684}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100685
Rabin Vincentb4268672016-03-03 15:58:01 +0100686static void simple_allocator_free(struct arm_dma_free_args *args)
687{
688 __dma_free_buffer(args->page, args->size);
689}
690
691static struct arm_dma_allocator simple_allocator = {
692 .alloc = simple_allocator_alloc,
693 .free = simple_allocator_free,
694};
695
696static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
697 struct page **ret_page)
698{
699 return __alloc_from_contiguous(args->dev, args->size, args->prot,
700 ret_page, args->caller,
Lucas Stach712c6042017-02-24 14:58:44 -0800701 args->want_vaddr, args->coherent_flag,
702 args->gfp);
Rabin Vincentb4268672016-03-03 15:58:01 +0100703}
704
705static void cma_allocator_free(struct arm_dma_free_args *args)
706{
707 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
708 args->size, args->want_vaddr);
709}
710
711static struct arm_dma_allocator cma_allocator = {
712 .alloc = cma_allocator_alloc,
713 .free = cma_allocator_free,
714};
715
716static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
717 struct page **ret_page)
718{
719 return __alloc_from_pool(args->size, ret_page);
720}
721
722static void pool_allocator_free(struct arm_dma_free_args *args)
723{
724 __free_from_pool(args->cpu_addr, args->size);
725}
726
727static struct arm_dma_allocator pool_allocator = {
728 .alloc = pool_allocator_alloc,
729 .free = pool_allocator_free,
730};
731
732static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
733 struct page **ret_page)
734{
735 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
736 args->prot, ret_page, args->caller,
737 args->want_vaddr);
738}
739
740static void remap_allocator_free(struct arm_dma_free_args *args)
741{
742 if (args->want_vaddr)
743 __dma_free_remap(args->cpu_addr, args->size);
744
745 __dma_free_buffer(args->page, args->size);
746}
747
748static struct arm_dma_allocator remap_allocator = {
749 .alloc = remap_allocator_alloc,
750 .free = remap_allocator_free,
751};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100752
753static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100754 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700755 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100756{
757 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900758 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000759 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100760 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100761 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100762 struct arm_dma_alloc_args args = {
763 .dev = dev,
764 .size = PAGE_ALIGN(size),
765 .gfp = gfp,
766 .prot = prot,
767 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700768 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100769 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100770 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100771
Marek Szyprowskic7909502011-12-29 13:09:51 +0100772#ifdef CONFIG_DMA_API_DEBUG
773 u64 limit = (mask + 1) & ~mask;
774 if (limit && size >= limit) {
775 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
776 size, mask);
777 return NULL;
778 }
779#endif
780
781 if (!mask)
782 return NULL;
783
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100784 buf = kzalloc(sizeof(*buf),
785 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100786 if (!buf)
787 return NULL;
788
Marek Szyprowskic7909502011-12-29 13:09:51 +0100789 if (mask < 0xffffffffULL)
790 gfp |= GFP_DMA;
791
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100792 /*
793 * Following is a work-around (a.k.a. hack) to prevent pages
794 * with __GFP_COMP being passed to split_page() which cannot
795 * handle them. The real problem is that this flag probably
796 * should be 0 on ARM as it is not supported on this
797 * platform; see CONFIG_HUGETLBFS.
798 */
799 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100800 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100801
Marek Szyprowski553ac782012-02-29 14:45:28 +0100802 *handle = DMA_ERROR_CODE;
Rabin Vincentb4268672016-03-03 15:58:01 +0100803 allowblock = gfpflags_allow_blocking(gfp);
804 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000805
Rabin Vincentb4268672016-03-03 15:58:01 +0100806 if (cma)
807 buf->allocator = &cma_allocator;
808 else if (nommu() || is_coherent)
809 buf->allocator = &simple_allocator;
810 else if (allowblock)
811 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000812 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100813 buf->allocator = &pool_allocator;
814
815 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000816
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100817 if (page) {
818 unsigned long flags;
819
Russell King9eedd962011-01-03 00:00:17 +0000820 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100821 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100822
823 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
824 list_add(&buf->list, &arm_dma_bufs);
825 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
826 } else {
827 kfree(buf);
828 }
Russell King31ebf942009-11-19 21:12:17 +0000829
Rabin Vincentb4268672016-03-03 15:58:01 +0100830 return args.want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100831}
Russell King695ae0a2009-11-19 16:31:39 +0000832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/*
834 * Allocate DMA-coherent memory space and return both the kernel remapped
835 * virtual and bus address for that space.
836 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200837void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700838 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Russell King0ea1ec72013-10-23 16:14:59 +0100840 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400841
Rob Herringdd37e942012-08-21 12:20:17 +0200842 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100843 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200844}
845
846static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700847 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200848{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100849 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100850 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Mike Looijmans55af8a92015-06-03 11:25:31 +0100853static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200854 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700855 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Catalin Marinasab6494f2009-07-24 12:35:02 +0100857 int ret = -ENXIO;
858#ifdef CONFIG_MMU
Marek Szyprowski50262a42012-07-30 09:35:26 +0200859 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
860 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100861 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200862 unsigned long off = vma->vm_pgoff;
863
Marek Szyprowski47142f02012-05-15 19:04:13 +0200864 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
865 return ret;
866
Marek Szyprowski50262a42012-07-30 09:35:26 +0200867 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
868 ret = remap_pfn_range(vma, vma->vm_start,
869 pfn + off,
870 vma->vm_end - vma->vm_start,
871 vma->vm_page_prot);
872 }
Benjamin Gaignard79964a12016-12-12 09:31:18 +0100873#else
874 ret = vm_iomap_memory(vma, vma->vm_start,
875 (vma->vm_end - vma->vm_start));
Catalin Marinasab6494f2009-07-24 12:35:02 +0100876#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return ret;
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100882 * Create userspace mapping for the DMA-coherent memory.
883 */
884static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
885 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700886 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100887{
888 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
889}
890
891int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
892 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700893 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100894{
895#ifdef CONFIG_MMU
896 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
897#endif /* CONFIG_MMU */
898 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
899}
900
901/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100902 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Rob Herringdd37e942012-08-21 12:20:17 +0200904static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700905 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +0200906 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100908 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100909 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100910 struct arm_dma_free_args args = {
911 .dev = dev,
912 .size = PAGE_ALIGN(size),
913 .cpu_addr = cpu_addr,
914 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700915 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +0100916 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100917
918 buf = arm_dma_buffer_find(cpu_addr);
919 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
920 return;
Russell King5edf71a2005-11-25 15:52:51 +0000921
Rabin Vincentb4268672016-03-03 15:58:01 +0100922 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100923 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
Russell Kingafd1a322008-09-25 16:30:57 +0100925
Rob Herringdd37e942012-08-21 12:20:17 +0200926void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700927 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200928{
929 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
930}
931
932static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700933 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200934{
935 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
936}
937
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200938int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
939 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700940 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200941{
942 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
943 int ret;
944
945 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
946 if (unlikely(ret))
947 return ret;
948
949 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
950 return 0;
951}
952
Russell King65af1912009-11-24 17:53:33 +0000953static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000954 size_t size, enum dma_data_direction dir,
955 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000956{
Russell King15653372013-01-19 11:05:57 +0000957 unsigned long pfn;
958 size_t left = size;
959
960 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
961 offset %= PAGE_SIZE;
962
Russell King65af1912009-11-24 17:53:33 +0000963 /*
964 * A single sg entry may refer to multiple physically contiguous
965 * pages. But we still need to process highmem pages individually.
966 * If highmem is not configured then the bulk of this loop gets
967 * optimized out.
968 */
Russell King65af1912009-11-24 17:53:33 +0000969 do {
970 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000971 void *vaddr;
972
Russell King15653372013-01-19 11:05:57 +0000973 page = pfn_to_page(pfn);
974
Russell King93f1d622009-11-24 14:41:01 +0000975 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000976 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000977 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100978
979 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500980 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100981 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500982 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100983 } else {
984 vaddr = kmap_high_get(page);
985 if (vaddr) {
986 op(vaddr + offset, len, dir);
987 kunmap_high(page);
988 }
Russell King93f1d622009-11-24 14:41:01 +0000989 }
990 } else {
991 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000992 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000993 }
Russell King65af1912009-11-24 17:53:33 +0000994 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000995 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000996 left -= len;
997 } while (left);
998}
999
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001000/*
1001 * Make an area consistent for devices.
1002 * Note: Drivers should NOT use this function directly, as it will break
1003 * platforms with CONFIG_DMABOUNCE.
1004 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
1005 */
1006static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +00001007 size_t size, enum dma_data_direction dir)
1008{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001009 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -04001010
Russell Kinga9c91472009-11-26 16:19:58 +00001011 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -04001012
Russell King65af1912009-11-24 17:53:33 +00001013 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001014 if (dir == DMA_FROM_DEVICE) {
1015 outer_inv_range(paddr, paddr + size);
1016 } else {
1017 outer_clean_range(paddr, paddr + size);
1018 }
1019 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -04001020}
Russell King4ea0d732009-11-24 16:27:17 +00001021
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001022static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001023 size_t size, enum dma_data_direction dir)
1024{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001025 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001026
1027 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001028 /* in any case, don't bother invalidating if DMA to device */
1029 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001030 outer_inv_range(paddr, paddr + size);
1031
Russell Kingdeace4a2014-05-03 11:06:55 +01001032 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1033 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001034
1035 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001036 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001037 */
Ming Leib2a234e2013-05-18 11:21:36 +01001038 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1039 unsigned long pfn;
1040 size_t left = size;
1041
1042 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1043 off %= PAGE_SIZE;
1044 if (off) {
1045 pfn++;
1046 left -= PAGE_SIZE - off;
1047 }
1048 while (left >= PAGE_SIZE) {
1049 page = pfn_to_page(pfn++);
1050 set_bit(PG_dcache_clean, &page->flags);
1051 left -= PAGE_SIZE;
1052 }
1053 }
Russell King4ea0d732009-11-24 16:27:17 +00001054}
Nicolas Pitre43377452009-03-12 22:52:09 -04001055
Russell Kingafd1a322008-09-25 16:30:57 +01001056/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001057 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001058 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1059 * @sg: list of buffers
1060 * @nents: number of buffers to map
1061 * @dir: DMA transfer direction
1062 *
1063 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1064 * This is the scatter-gather version of the dma_map_single interface.
1065 * Here the scatter gather list elements are each tagged with the
1066 * appropriate dma address and length. They are obtained via
1067 * sg_dma_{address,length}.
1068 *
1069 * Device ownership issues as mentioned for dma_map_single are the same
1070 * here.
1071 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001072int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001073 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001074{
Bart Van Assche52997092017-01-20 13:04:01 -08001075 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001076 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001077 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001078
1079 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001080#ifdef CONFIG_NEED_SG_DMA_LENGTH
1081 s->dma_length = s->length;
1082#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001083 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1084 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001085 if (dma_mapping_error(dev, s->dma_address))
1086 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001087 }
Russell Kingafd1a322008-09-25 16:30:57 +01001088 return nents;
Russell King01135d922008-09-25 21:05:02 +01001089
1090 bad_mapping:
1091 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001092 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001093 return 0;
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_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
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
Linus Walleij0adfca62011-01-12 18:50:37 +01001100 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001101 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1102 *
1103 * Unmap a set of streaming mode DMA translations. Again, CPU access
1104 * rules concerning calls here are the same as for dma_unmap_single().
1105 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001106void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001107 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001108{
Bart Van Assche52997092017-01-20 13:04:01 -08001109 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001110 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001111
Russell King01135d922008-09-25 21:05:02 +01001112 int i;
Russell King24056f52011-01-03 11:29:28 +00001113
Russell King01135d922008-09-25 21:05:02 +01001114 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001115 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001116}
Russell Kingafd1a322008-09-25 16:30:57 +01001117
1118/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001119 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001120 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1121 * @sg: list of buffers
1122 * @nents: number of buffers to map (returned from dma_map_sg)
1123 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1124 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001125void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001126 int nents, enum dma_data_direction dir)
1127{
Bart Van Assche52997092017-01-20 13:04:01 -08001128 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001129 struct scatterlist *s;
1130 int i;
1131
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001132 for_each_sg(sg, s, nents, i)
1133 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1134 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001135}
Russell Kingafd1a322008-09-25 16:30:57 +01001136
1137/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001138 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001139 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1140 * @sg: list of buffers
1141 * @nents: number of buffers to map (returned from dma_map_sg)
1142 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1143 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001144void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001145 int nents, enum dma_data_direction dir)
1146{
Bart Van Assche52997092017-01-20 13:04:01 -08001147 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001148 struct scatterlist *s;
1149 int i;
1150
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001151 for_each_sg(sg, s, nents, i)
1152 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1153 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001154}
Russell King24056f52011-01-03 11:29:28 +00001155
Russell King022ae532011-07-08 21:26:59 +01001156/*
1157 * Return whether the given device DMA address mask can be supported
1158 * properly. For example, if your device can only drive the low 24-bits
1159 * during bus mastering, then you would pass 0x00ffffff as the mask
1160 * to this function.
1161 */
1162int dma_supported(struct device *dev, u64 mask)
1163{
Russell King9f28cde2013-12-06 12:30:42 +00001164 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001165}
1166EXPORT_SYMBOL(dma_supported);
1167
Russell King24056f52011-01-03 11:29:28 +00001168#define PREALLOC_DMA_DEBUG_ENTRIES 4096
1169
1170static int __init dma_debug_do_init(void)
1171{
1172 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1173 return 0;
1174}
Marek Szyprowski256ff1c2016-11-15 14:00:53 +01001175core_initcall(dma_debug_do_init);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001176
1177#ifdef CONFIG_ARM_DMA_USE_IOMMU
1178
Sricharan R7d2822d2017-01-06 18:58:13 +05301179static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
1180{
1181 int prot = 0;
1182
1183 if (attrs & DMA_ATTR_PRIVILEGED)
1184 prot |= IOMMU_PRIV;
1185
1186 switch (dir) {
1187 case DMA_BIDIRECTIONAL:
1188 return prot | IOMMU_READ | IOMMU_WRITE;
1189 case DMA_TO_DEVICE:
1190 return prot | IOMMU_READ;
1191 case DMA_FROM_DEVICE:
1192 return prot | IOMMU_WRITE;
1193 default:
1194 return prot;
1195 }
1196}
1197
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001198/* IOMMU */
1199
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001200static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1201
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001202static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1203 size_t size)
1204{
1205 unsigned int order = get_order(size);
1206 unsigned int align = 0;
1207 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301208 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001209 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001210 dma_addr_t iova;
1211 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001212
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001213 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1214 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1215
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001216 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1217 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001218
1219 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001220 for (i = 0; i < mapping->nr_bitmaps; i++) {
1221 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1222 mapping->bits, 0, count, align);
1223
1224 if (start > mapping->bits)
1225 continue;
1226
1227 bitmap_set(mapping->bitmaps[i], start, count);
1228 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001229 }
1230
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001231 /*
1232 * No unused range found. Try to extend the existing mapping
1233 * and perform a second attempt to reserve an IO virtual
1234 * address range of size bytes.
1235 */
1236 if (i == mapping->nr_bitmaps) {
1237 if (extend_iommu_mapping(mapping)) {
1238 spin_unlock_irqrestore(&mapping->lock, flags);
1239 return DMA_ERROR_CODE;
1240 }
1241
1242 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1243 mapping->bits, 0, count, align);
1244
1245 if (start > mapping->bits) {
1246 spin_unlock_irqrestore(&mapping->lock, flags);
1247 return DMA_ERROR_CODE;
1248 }
1249
1250 bitmap_set(mapping->bitmaps[i], start, count);
1251 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001252 spin_unlock_irqrestore(&mapping->lock, flags);
1253
Ritesh Harjani006f8412014-05-20 10:02:59 +05301254 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001255 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001256
1257 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001258}
1259
1260static inline void __free_iova(struct dma_iommu_mapping *mapping,
1261 dma_addr_t addr, size_t size)
1262{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001263 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301264 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001265 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001266 dma_addr_t bitmap_base;
1267 u32 bitmap_index;
1268
1269 if (!size)
1270 return;
1271
Ritesh Harjani006f8412014-05-20 10:02:59 +05301272 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001273 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1274
Ritesh Harjani006f8412014-05-20 10:02:59 +05301275 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001276
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001277 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001278
Ritesh Harjani006f8412014-05-20 10:02:59 +05301279 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001280 /*
1281 * The address range to be freed reaches into the iova
1282 * range of the next bitmap. This should not happen as
1283 * we don't allow this in __alloc_iova (at the
1284 * moment).
1285 */
1286 BUG();
1287 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001288 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001289
1290 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001291 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001292 spin_unlock_irqrestore(&mapping->lock, flags);
1293}
1294
Doug Anderson33298ef2016-01-29 23:06:08 +01001295/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1296static const int iommu_order_array[] = { 9, 8, 4, 0 };
1297
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001298static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001299 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001300 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001301{
1302 struct page **pages;
1303 int count = size >> PAGE_SHIFT;
1304 int array_size = count * sizeof(struct page *);
1305 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001306 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001307
1308 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001309 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001310 else
1311 pages = vzalloc(array_size);
1312 if (!pages)
1313 return NULL;
1314
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001315 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001316 {
1317 unsigned long order = get_order(size);
1318 struct page *page;
1319
Lucas Stach712c6042017-02-24 14:58:44 -08001320 page = dma_alloc_from_contiguous(dev, count, order, gfp);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001321 if (!page)
1322 goto error;
1323
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001324 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001325
1326 for (i = 0; i < count; i++)
1327 pages[i] = page + i;
1328
1329 return pages;
1330 }
1331
Doug Anderson14d3ae22016-01-29 23:08:46 +01001332 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001333 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001334 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1335
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001336 /*
1337 * IOMMU can map any pages, so himem can also be used here
1338 */
1339 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1340
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001341 while (count) {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001342 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001343
Doug Anderson33298ef2016-01-29 23:06:08 +01001344 order = iommu_order_array[order_idx];
1345
1346 /* Drop down when we get small */
1347 if (__fls(count) < order) {
1348 order_idx++;
1349 continue;
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001350 }
1351
Doug Anderson33298ef2016-01-29 23:06:08 +01001352 if (order) {
1353 /* See if it's easy to allocate a high-order chunk */
1354 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1355
1356 /* Go down a notch at first sign of pressure */
1357 if (!pages[i]) {
1358 order_idx++;
1359 continue;
1360 }
1361 } else {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001362 pages[i] = alloc_pages(gfp, 0);
1363 if (!pages[i])
1364 goto error;
1365 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001366
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001367 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001368 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001369 j = 1 << order;
1370 while (--j)
1371 pages[i + j] = pages[i] + j;
1372 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001373
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001374 __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001375 i += 1 << order;
1376 count -= 1 << order;
1377 }
1378
1379 return pages;
1380error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001381 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001382 if (pages[i])
1383 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001384 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001385 return NULL;
1386}
1387
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001388static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001389 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001390{
1391 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001392 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001393
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001394 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001395 dma_release_from_contiguous(dev, pages[0], count);
1396 } else {
1397 for (i = 0; i < count; i++)
1398 if (pages[i])
1399 __free_pages(pages[i], 0);
1400 }
1401
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001402 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001403 return 0;
1404}
1405
1406/*
1407 * Create a CPU mapping for a specified pages
1408 */
1409static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001410__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1411 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001412{
Laura Abbott513510d2014-10-09 15:26:40 -07001413 return dma_common_pages_remap(pages, size,
1414 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001415}
1416
1417/*
1418 * Create a mapping in device IO address space for specified pages
1419 */
1420static dma_addr_t
Sricharan R7d2822d2017-01-06 18:58:13 +05301421__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1422 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001423{
Will Deacon89cfdb12015-01-16 18:02:15 +01001424 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001425 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1426 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001427 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001428
1429 dma_addr = __alloc_iova(mapping, size);
1430 if (dma_addr == DMA_ERROR_CODE)
1431 return dma_addr;
1432
1433 iova = dma_addr;
1434 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001435 int ret;
1436
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001437 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1438 phys_addr_t phys = page_to_phys(pages[i]);
1439 unsigned int len, j;
1440
1441 for (j = i + 1; j < count; j++, next_pfn++)
1442 if (page_to_pfn(pages[j]) != next_pfn)
1443 break;
1444
1445 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001446 ret = iommu_map(mapping->domain, iova, phys, len,
Sricharan R7d2822d2017-01-06 18:58:13 +05301447 __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001448 if (ret < 0)
1449 goto fail;
1450 iova += len;
1451 i = j;
1452 }
1453 return dma_addr;
1454fail:
1455 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1456 __free_iova(mapping, dma_addr, size);
1457 return DMA_ERROR_CODE;
1458}
1459
1460static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1461{
Will Deacon89cfdb12015-01-16 18:02:15 +01001462 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001463
1464 /*
1465 * add optional in-page offset from iova to size and align
1466 * result to page size
1467 */
1468 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1469 iova &= PAGE_MASK;
1470
1471 iommu_unmap(mapping->domain, iova, size);
1472 __free_iova(mapping, iova, size);
1473 return 0;
1474}
1475
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001476static struct page **__atomic_get_pages(void *addr)
1477{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001478 struct page *page;
1479 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001480
Laura Abbott36d0fd22014-10-09 15:26:42 -07001481 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1482 page = phys_to_page(phys);
1483
1484 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001485}
1486
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001487static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001488{
1489 struct vm_struct *area;
1490
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001491 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1492 return __atomic_get_pages(cpu_addr);
1493
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001494 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001495 return cpu_addr;
1496
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001497 area = find_vm_area(cpu_addr);
1498 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1499 return area->pages;
1500 return NULL;
1501}
1502
Gregory CLEMENT56506822016-04-15 11:20:13 +01001503static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
Sricharan R7d2822d2017-01-06 18:58:13 +05301504 dma_addr_t *handle, int coherent_flag,
1505 unsigned long attrs)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001506{
1507 struct page *page;
1508 void *addr;
1509
Gregory CLEMENT56506822016-04-15 11:20:13 +01001510 if (coherent_flag == COHERENT)
1511 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1512 else
1513 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001514 if (!addr)
1515 return NULL;
1516
Sricharan R7d2822d2017-01-06 18:58:13 +05301517 *handle = __iommu_create_mapping(dev, &page, size, attrs);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001518 if (*handle == DMA_ERROR_CODE)
1519 goto err_mapping;
1520
1521 return addr;
1522
1523err_mapping:
1524 __free_from_pool(addr, size);
1525 return NULL;
1526}
1527
Marek Szyprowskid5898292013-02-08 10:54:48 +01001528static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001529 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001530{
1531 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001532 if (coherent_flag == COHERENT)
1533 __dma_free_buffer(virt_to_page(cpu_addr), size);
1534 else
1535 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001536}
1537
Gregory CLEMENT56506822016-04-15 11:20:13 +01001538static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001539 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001540 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001541{
Russell King71b55662013-11-25 12:01:03 +00001542 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001543 struct page **pages;
1544 void *addr = NULL;
1545
1546 *handle = DMA_ERROR_CODE;
1547 size = PAGE_ALIGN(size);
1548
Gregory CLEMENT56506822016-04-15 11:20:13 +01001549 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1550 return __iommu_alloc_simple(dev, size, gfp, handle,
Sricharan R7d2822d2017-01-06 18:58:13 +05301551 coherent_flag, attrs);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001552
Richard Zhao5b91a982013-06-20 20:31:00 +08001553 /*
1554 * Following is a work-around (a.k.a. hack) to prevent pages
1555 * with __GFP_COMP being passed to split_page() which cannot
1556 * handle them. The real problem is that this flag probably
1557 * should be 0 on ARM as it is not supported on this
1558 * platform; see CONFIG_HUGETLBFS.
1559 */
1560 gfp &= ~(__GFP_COMP);
1561
Gregory CLEMENT56506822016-04-15 11:20:13 +01001562 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001563 if (!pages)
1564 return NULL;
1565
Sricharan R7d2822d2017-01-06 18:58:13 +05301566 *handle = __iommu_create_mapping(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001567 if (*handle == DMA_ERROR_CODE)
1568 goto err_buffer;
1569
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001570 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001571 return pages;
1572
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001573 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1574 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001575 if (!addr)
1576 goto err_mapping;
1577
1578 return addr;
1579
1580err_mapping:
1581 __iommu_remove_mapping(dev, *handle, size);
1582err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001583 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001584 return NULL;
1585}
1586
Gregory CLEMENT56506822016-04-15 11:20:13 +01001587static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001588 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001589{
1590 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1591}
1592
1593static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001594 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001595{
1596 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1597}
1598
1599static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001600 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001601 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001602{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001603 unsigned long uaddr = vma->vm_start;
1604 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001605 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001606 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1607 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001608
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001609 if (!pages)
1610 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001611
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001612 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1613 return -ENXIO;
1614
Marek Szyprowski7e312102015-08-28 09:42:09 +01001615 pages += off;
1616
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001617 do {
1618 int ret = vm_insert_page(vma, uaddr, *pages++);
1619 if (ret) {
1620 pr_err("Remapping memory failed: %d\n", ret);
1621 return ret;
1622 }
1623 uaddr += PAGE_SIZE;
1624 usize -= PAGE_SIZE;
1625 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001626
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001627 return 0;
1628}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001629static int arm_iommu_mmap_attrs(struct device *dev,
1630 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001631 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001632{
1633 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1634
1635 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1636}
1637
1638static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1639 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001640 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001641{
1642 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1643}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001644
1645/*
1646 * free a page as defined by the above mapping.
1647 * Must not be called with IRQs disabled.
1648 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001649void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001650 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001651{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001652 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001653 size = PAGE_ALIGN(size);
1654
Gregory CLEMENT56506822016-04-15 11:20:13 +01001655 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1656 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001657 return;
1658 }
1659
YoungJun Cho836bfa02013-06-17 13:18:52 +09001660 pages = __iommu_get_pages(cpu_addr, attrs);
1661 if (!pages) {
1662 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1663 return;
1664 }
1665
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001666 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001667 dma_common_free_remap(cpu_addr, size,
1668 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001669 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001670
1671 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001672 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001673}
1674
Gregory CLEMENT56506822016-04-15 11:20:13 +01001675void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001676 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001677{
1678 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1679}
1680
1681void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001682 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001683{
1684 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1685}
1686
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001687static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1688 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001689 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001690{
1691 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1692 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1693
1694 if (!pages)
1695 return -ENXIO;
1696
1697 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1698 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001699}
1700
1701/*
1702 * Map a part of the scatter-gather list into contiguous io address space
1703 */
1704static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1705 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001706 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001707 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001708{
Will Deacon89cfdb12015-01-16 18:02:15 +01001709 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001710 dma_addr_t iova, iova_base;
1711 int ret = 0;
1712 unsigned int count;
1713 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001714 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001715
1716 size = PAGE_ALIGN(size);
1717 *handle = DMA_ERROR_CODE;
1718
1719 iova_base = iova = __alloc_iova(mapping, size);
1720 if (iova == DMA_ERROR_CODE)
1721 return -ENOMEM;
1722
1723 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001724 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001725 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1726
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001727 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001728 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1729
Sricharan R7d2822d2017-01-06 18:58:13 +05301730 prot = __dma_info_to_prot(dir, attrs);
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001731
1732 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001733 if (ret < 0)
1734 goto fail;
1735 count += len >> PAGE_SHIFT;
1736 iova += len;
1737 }
1738 *handle = iova_base;
1739
1740 return 0;
1741fail:
1742 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1743 __free_iova(mapping, iova_base, size);
1744 return ret;
1745}
1746
Rob Herring0fa478d2012-08-21 12:23:23 +02001747static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001748 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001749 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001750{
1751 struct scatterlist *s = sg, *dma = sg, *start = sg;
1752 int i, count = 0;
1753 unsigned int offset = s->offset;
1754 unsigned int size = s->offset + s->length;
1755 unsigned int max = dma_get_max_seg_size(dev);
1756
1757 for (i = 1; i < nents; i++) {
1758 s = sg_next(s);
1759
1760 s->dma_address = DMA_ERROR_CODE;
1761 s->dma_length = 0;
1762
1763 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1764 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001765 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001766 goto bad_mapping;
1767
1768 dma->dma_address += offset;
1769 dma->dma_length = size - offset;
1770
1771 size = offset = s->offset;
1772 start = s;
1773 dma = sg_next(dma);
1774 count += 1;
1775 }
1776 size += s->length;
1777 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001778 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1779 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001780 goto bad_mapping;
1781
1782 dma->dma_address += offset;
1783 dma->dma_length = size - offset;
1784
1785 return count+1;
1786
1787bad_mapping:
1788 for_each_sg(sg, s, count, i)
1789 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1790 return 0;
1791}
1792
1793/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001794 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1795 * @dev: valid struct device pointer
1796 * @sg: list of buffers
1797 * @nents: number of buffers to map
1798 * @dir: DMA transfer direction
1799 *
1800 * Map a set of i/o coherent buffers described by scatterlist in streaming
1801 * mode for DMA. The scatter gather list elements are merged together (if
1802 * possible) and tagged with the appropriate dma address and length. They are
1803 * obtained via sg_dma_{address,length}.
1804 */
1805int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001806 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001807{
1808 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1809}
1810
1811/**
1812 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1813 * @dev: valid struct device pointer
1814 * @sg: list of buffers
1815 * @nents: number of buffers to map
1816 * @dir: DMA transfer direction
1817 *
1818 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1819 * The scatter gather list elements are merged together (if possible) and
1820 * tagged with the appropriate dma address and length. They are obtained via
1821 * sg_dma_{address,length}.
1822 */
1823int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001824 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001825{
1826 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1827}
1828
1829static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001830 int nents, enum dma_data_direction dir,
1831 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001832{
1833 struct scatterlist *s;
1834 int i;
1835
1836 for_each_sg(sg, s, nents, i) {
1837 if (sg_dma_len(s))
1838 __iommu_remove_mapping(dev, sg_dma_address(s),
1839 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001840 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001841 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1842 s->length, dir);
1843 }
1844}
1845
1846/**
1847 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1848 * @dev: valid struct device pointer
1849 * @sg: list of buffers
1850 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1851 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1852 *
1853 * Unmap a set of streaming mode DMA translations. Again, CPU access
1854 * rules concerning calls here are the same as for dma_unmap_single().
1855 */
1856void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001857 int nents, enum dma_data_direction dir,
1858 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001859{
1860 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1861}
1862
1863/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001864 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1865 * @dev: valid struct device pointer
1866 * @sg: list of buffers
1867 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1868 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1869 *
1870 * Unmap a set of streaming mode DMA translations. Again, CPU access
1871 * rules concerning calls here are the same as for dma_unmap_single().
1872 */
1873void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001874 enum dma_data_direction dir,
1875 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001876{
Rob Herring0fa478d2012-08-21 12:23:23 +02001877 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001878}
1879
1880/**
1881 * arm_iommu_sync_sg_for_cpu
1882 * @dev: valid struct device pointer
1883 * @sg: list of buffers
1884 * @nents: number of buffers to map (returned from dma_map_sg)
1885 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1886 */
1887void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1888 int nents, enum dma_data_direction dir)
1889{
1890 struct scatterlist *s;
1891 int i;
1892
1893 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001894 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001895
1896}
1897
1898/**
1899 * arm_iommu_sync_sg_for_device
1900 * @dev: valid struct device pointer
1901 * @sg: list of buffers
1902 * @nents: number of buffers to map (returned from dma_map_sg)
1903 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1904 */
1905void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1906 int nents, enum dma_data_direction dir)
1907{
1908 struct scatterlist *s;
1909 int i;
1910
1911 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001912 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001913}
1914
1915
1916/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001917 * arm_coherent_iommu_map_page
1918 * @dev: valid struct device pointer
1919 * @page: page that buffer resides in
1920 * @offset: offset into page for start of buffer
1921 * @size: size of buffer to map
1922 * @dir: DMA transfer direction
1923 *
1924 * Coherent IOMMU aware version of arm_dma_map_page()
1925 */
1926static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1927 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001928 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001929{
Will Deacon89cfdb12015-01-16 18:02:15 +01001930 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001931 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001932 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001933
1934 dma_addr = __alloc_iova(mapping, len);
1935 if (dma_addr == DMA_ERROR_CODE)
1936 return dma_addr;
1937
Sricharan R7d2822d2017-01-06 18:58:13 +05301938 prot = __dma_info_to_prot(dir, attrs);
Will Deacon13987d62013-06-10 19:34:39 +01001939
1940 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001941 if (ret < 0)
1942 goto fail;
1943
1944 return dma_addr + offset;
1945fail:
1946 __free_iova(mapping, dma_addr, len);
1947 return DMA_ERROR_CODE;
1948}
1949
1950/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001951 * arm_iommu_map_page
1952 * @dev: valid struct device pointer
1953 * @page: page that buffer resides in
1954 * @offset: offset into page for start of buffer
1955 * @size: size of buffer to map
1956 * @dir: DMA transfer direction
1957 *
1958 * IOMMU aware version of arm_dma_map_page()
1959 */
1960static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1961 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001962 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001963{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001964 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001965 __dma_page_cpu_to_dev(page, offset, size, dir);
1966
Rob Herring0fa478d2012-08-21 12:23:23 +02001967 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1968}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001969
Rob Herring0fa478d2012-08-21 12:23:23 +02001970/**
1971 * arm_coherent_iommu_unmap_page
1972 * @dev: valid struct device pointer
1973 * @handle: DMA address of buffer
1974 * @size: size of buffer (same as passed to dma_map_page)
1975 * @dir: DMA transfer direction (same as passed to dma_map_page)
1976 *
1977 * Coherent IOMMU aware version of arm_dma_unmap_page()
1978 */
1979static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001980 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001981{
Will Deacon89cfdb12015-01-16 18:02:15 +01001982 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001983 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001984 int offset = handle & ~PAGE_MASK;
1985 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001986
Rob Herring0fa478d2012-08-21 12:23:23 +02001987 if (!iova)
1988 return;
1989
1990 iommu_unmap(mapping->domain, iova, len);
1991 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001992}
1993
1994/**
1995 * arm_iommu_unmap_page
1996 * @dev: valid struct device pointer
1997 * @handle: DMA address of buffer
1998 * @size: size of buffer (same as passed to dma_map_page)
1999 * @dir: DMA transfer direction (same as passed to dma_map_page)
2000 *
2001 * IOMMU aware version of arm_dma_unmap_page()
2002 */
2003static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002004 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002005{
Will Deacon89cfdb12015-01-16 18:02:15 +01002006 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002007 dma_addr_t iova = handle & PAGE_MASK;
2008 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2009 int offset = handle & ~PAGE_MASK;
2010 int len = PAGE_ALIGN(size + offset);
2011
2012 if (!iova)
2013 return;
2014
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002015 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002016 __dma_page_dev_to_cpu(page, offset, size, dir);
2017
2018 iommu_unmap(mapping->domain, iova, len);
2019 __free_iova(mapping, iova, len);
2020}
2021
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002022/**
2023 * arm_iommu_map_resource - map a device resource for DMA
2024 * @dev: valid struct device pointer
2025 * @phys_addr: physical address of resource
2026 * @size: size of resource to map
2027 * @dir: DMA transfer direction
2028 */
2029static dma_addr_t arm_iommu_map_resource(struct device *dev,
2030 phys_addr_t phys_addr, size_t size,
2031 enum dma_data_direction dir, unsigned long attrs)
2032{
2033 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2034 dma_addr_t dma_addr;
2035 int ret, prot;
2036 phys_addr_t addr = phys_addr & PAGE_MASK;
2037 unsigned int offset = phys_addr & ~PAGE_MASK;
2038 size_t len = PAGE_ALIGN(size + offset);
2039
2040 dma_addr = __alloc_iova(mapping, len);
2041 if (dma_addr == DMA_ERROR_CODE)
2042 return dma_addr;
2043
Sricharan R7d2822d2017-01-06 18:58:13 +05302044 prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002045
2046 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2047 if (ret < 0)
2048 goto fail;
2049
2050 return dma_addr + offset;
2051fail:
2052 __free_iova(mapping, dma_addr, len);
2053 return DMA_ERROR_CODE;
2054}
2055
2056/**
2057 * arm_iommu_unmap_resource - unmap a device DMA resource
2058 * @dev: valid struct device pointer
2059 * @dma_handle: DMA address to resource
2060 * @size: size of resource to map
2061 * @dir: DMA transfer direction
2062 */
2063static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2064 size_t size, enum dma_data_direction dir,
2065 unsigned long attrs)
2066{
2067 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2068 dma_addr_t iova = dma_handle & PAGE_MASK;
2069 unsigned int offset = dma_handle & ~PAGE_MASK;
2070 size_t len = PAGE_ALIGN(size + offset);
2071
2072 if (!iova)
2073 return;
2074
2075 iommu_unmap(mapping->domain, iova, len);
2076 __free_iova(mapping, iova, len);
2077}
2078
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002079static void arm_iommu_sync_single_for_cpu(struct device *dev,
2080 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2081{
Will Deacon89cfdb12015-01-16 18:02:15 +01002082 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002083 dma_addr_t iova = handle & PAGE_MASK;
2084 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2085 unsigned int offset = handle & ~PAGE_MASK;
2086
2087 if (!iova)
2088 return;
2089
Rob Herring0fa478d2012-08-21 12:23:23 +02002090 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002091}
2092
2093static void arm_iommu_sync_single_for_device(struct device *dev,
2094 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2095{
Will Deacon89cfdb12015-01-16 18:02:15 +01002096 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002097 dma_addr_t iova = handle & PAGE_MASK;
2098 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2099 unsigned int offset = handle & ~PAGE_MASK;
2100
2101 if (!iova)
2102 return;
2103
2104 __dma_page_cpu_to_dev(page, offset, size, dir);
2105}
2106
Bart Van Assche52997092017-01-20 13:04:01 -08002107const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002108 .alloc = arm_iommu_alloc_attrs,
2109 .free = arm_iommu_free_attrs,
2110 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002111 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002112
2113 .map_page = arm_iommu_map_page,
2114 .unmap_page = arm_iommu_unmap_page,
2115 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2116 .sync_single_for_device = arm_iommu_sync_single_for_device,
2117
2118 .map_sg = arm_iommu_map_sg,
2119 .unmap_sg = arm_iommu_unmap_sg,
2120 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2121 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002122
2123 .map_resource = arm_iommu_map_resource,
2124 .unmap_resource = arm_iommu_unmap_resource,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002125};
2126
Bart Van Assche52997092017-01-20 13:04:01 -08002127const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002128 .alloc = arm_coherent_iommu_alloc_attrs,
2129 .free = arm_coherent_iommu_free_attrs,
2130 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002131 .get_sgtable = arm_iommu_get_sgtable,
2132
2133 .map_page = arm_coherent_iommu_map_page,
2134 .unmap_page = arm_coherent_iommu_unmap_page,
2135
2136 .map_sg = arm_coherent_iommu_map_sg,
2137 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002138
2139 .map_resource = arm_iommu_map_resource,
2140 .unmap_resource = arm_iommu_unmap_resource,
Rob Herring0fa478d2012-08-21 12:23:23 +02002141};
2142
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002143/**
2144 * arm_iommu_create_mapping
2145 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2146 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002147 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002148 *
2149 * Creates a mapping structure which holds information about used/unused
2150 * IO address ranges, which is required to perform memory allocation and
2151 * mapping with IOMMU aware functions.
2152 *
2153 * The client device need to be attached to the mapping with
2154 * arm_iommu_attach_device function.
2155 */
2156struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002157arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002158{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002159 unsigned int bits = size >> PAGE_SHIFT;
2160 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002161 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002162 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002163 int err = -ENOMEM;
2164
Marek Szyprowski14245322015-04-29 11:29:19 +01002165 /* currently only 32-bit DMA address space is supported */
2166 if (size > DMA_BIT_MASK(32) + 1)
2167 return ERR_PTR(-ERANGE);
2168
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002169 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002170 return ERR_PTR(-EINVAL);
2171
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002172 if (bitmap_size > PAGE_SIZE) {
2173 extensions = bitmap_size / PAGE_SIZE;
2174 bitmap_size = PAGE_SIZE;
2175 }
2176
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002177 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2178 if (!mapping)
2179 goto err;
2180
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002181 mapping->bitmap_size = bitmap_size;
2182 mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002183 GFP_KERNEL);
2184 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002185 goto err2;
2186
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002187 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002188 if (!mapping->bitmaps[0])
2189 goto err3;
2190
2191 mapping->nr_bitmaps = 1;
2192 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002193 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002194 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002195
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002196 spin_lock_init(&mapping->lock);
2197
2198 mapping->domain = iommu_domain_alloc(bus);
2199 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002200 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002201
2202 kref_init(&mapping->kref);
2203 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002204err4:
2205 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002206err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002207 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002208err2:
2209 kfree(mapping);
2210err:
2211 return ERR_PTR(err);
2212}
Prathyush K18177d12013-01-04 06:22:42 -05002213EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002214
2215static void release_iommu_mapping(struct kref *kref)
2216{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002217 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002218 struct dma_iommu_mapping *mapping =
2219 container_of(kref, struct dma_iommu_mapping, kref);
2220
2221 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002222 for (i = 0; i < mapping->nr_bitmaps; i++)
2223 kfree(mapping->bitmaps[i]);
2224 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002225 kfree(mapping);
2226}
2227
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002228static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2229{
2230 int next_bitmap;
2231
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002232 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002233 return -EINVAL;
2234
2235 next_bitmap = mapping->nr_bitmaps;
2236 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2237 GFP_ATOMIC);
2238 if (!mapping->bitmaps[next_bitmap])
2239 return -ENOMEM;
2240
2241 mapping->nr_bitmaps++;
2242
2243 return 0;
2244}
2245
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002246void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2247{
2248 if (mapping)
2249 kref_put(&mapping->kref, release_iommu_mapping);
2250}
Prathyush K18177d12013-01-04 06:22:42 -05002251EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002252
Laurent Pincharteab8d652015-01-23 16:21:49 +02002253static int __arm_iommu_attach_device(struct device *dev,
2254 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002255{
2256 int err;
2257
2258 err = iommu_attach_device(mapping->domain, dev);
2259 if (err)
2260 return err;
2261
2262 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002263 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002264
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002265 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002266 return 0;
2267}
2268
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002269/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002270 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002271 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002272 * @mapping: io address space mapping structure (returned from
2273 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002274 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002275 * Attaches specified io address space mapping to the provided device.
2276 * This replaces the dma operations (dma_map_ops pointer) with the
2277 * IOMMU aware version.
2278 *
2279 * More than one client might be attached to the same io address space
2280 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002281 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002282int arm_iommu_attach_device(struct device *dev,
2283 struct dma_iommu_mapping *mapping)
2284{
2285 int err;
2286
2287 err = __arm_iommu_attach_device(dev, mapping);
2288 if (err)
2289 return err;
2290
2291 set_dma_ops(dev, &iommu_ops);
2292 return 0;
2293}
2294EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2295
2296static void __arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002297{
2298 struct dma_iommu_mapping *mapping;
2299
2300 mapping = to_dma_iommu_mapping(dev);
2301 if (!mapping) {
2302 dev_warn(dev, "Not attached\n");
2303 return;
2304 }
2305
2306 iommu_detach_device(mapping->domain, dev);
2307 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002308 to_dma_iommu_mapping(dev) = NULL;
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002309
2310 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2311}
Laurent Pincharteab8d652015-01-23 16:21:49 +02002312
2313/**
2314 * arm_iommu_detach_device
2315 * @dev: valid struct device pointer
2316 *
2317 * Detaches the provided device from a previously attached map.
2318 * This voids the dma operations (dma_map_ops pointer)
2319 */
2320void arm_iommu_detach_device(struct device *dev)
2321{
2322 __arm_iommu_detach_device(dev);
2323 set_dma_ops(dev, NULL);
2324}
Prathyush K18177d12013-01-04 06:22:42 -05002325EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002326
Bart Van Assche52997092017-01-20 13:04:01 -08002327static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002328{
2329 return coherent ? &iommu_coherent_ops : &iommu_ops;
2330}
2331
2332static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002333 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002334{
2335 struct dma_iommu_mapping *mapping;
2336
2337 if (!iommu)
2338 return false;
2339
2340 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2341 if (IS_ERR(mapping)) {
2342 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2343 size, dev_name(dev));
2344 return false;
2345 }
2346
Laurent Pincharteab8d652015-01-23 16:21:49 +02002347 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002348 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2349 dev_name(dev));
2350 arm_iommu_release_mapping(mapping);
2351 return false;
2352 }
2353
2354 return true;
2355}
2356
2357static void arm_teardown_iommu_dma_ops(struct device *dev)
2358{
Will Deacon89cfdb12015-01-16 18:02:15 +01002359 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002360
Will Deaconc2273a12015-01-16 18:01:43 +01002361 if (!mapping)
2362 return;
2363
Laurent Pincharteab8d652015-01-23 16:21:49 +02002364 __arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002365 arm_iommu_release_mapping(mapping);
2366}
2367
2368#else
2369
2370static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002371 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002372{
2373 return false;
2374}
2375
2376static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2377
2378#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2379
2380#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2381
Bart Van Assche52997092017-01-20 13:04:01 -08002382static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002383{
2384 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
2385}
2386
2387void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002388 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002389{
Bart Van Assche52997092017-01-20 13:04:01 -08002390 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002391
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002392 dev->archdata.dma_coherent = coherent;
Laurent Pinchart26b37b92015-05-15 02:00:02 +03002393
2394 /*
2395 * Don't override the dma_ops if they have already been set. Ideally
2396 * this should be the only location where dma_ops are set, remove this
2397 * check when all other callers of set_dma_ops will have disappeared.
2398 */
2399 if (dev->dma_ops)
2400 return;
2401
Will Deacon4bb25782014-08-27 17:52:44 +01002402 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2403 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2404 else
2405 dma_ops = arm_get_dma_map_ops(coherent);
2406
2407 set_dma_ops(dev, dma_ops);
2408}
2409
2410void arch_teardown_dma_ops(struct device *dev)
2411{
2412 arm_teardown_iommu_dma_ops(dev);
2413}