blob: 2cfb17bad1e638135dd6f8517577271373bf8660 [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 */
12#include <linux/module.h>
13#include <linux/mm.h>
Laura Abbott36d0fd22014-10-09 15:26:42 -070014#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/list.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/dma-mapping.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010021#include <linux/dma-contiguous.h>
Nicolas Pitre39af22a2010-12-15 15:14:45 -050022#include <linux/highmem.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010023#include <linux/memblock.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010024#include <linux/slab.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020025#include <linux/iommu.h>
Marek Szyprowskie9da6e92012-07-30 09:11:33 +020026#include <linux/io.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020027#include <linux/vmalloc.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010028#include <linux/sizes.h>
Joonsoo Kima2541292014-08-06 16:05:25 -070029#include <linux/cma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Lennert Buytenhek23759dc2006-04-02 00:07:39 +010031#include <asm/memory.h>
Nicolas Pitre43377452009-03-12 22:52:09 -040032#include <asm/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/tlbflush.h>
Jon Medhurst99d17172011-08-02 17:28:27 +010035#include <asm/mach/arch.h>
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +020036#include <asm/dma-iommu.h>
Marek Szyprowskic7909502011-12-29 13:09:51 +010037#include <asm/mach/map.h>
38#include <asm/system_info.h>
39#include <asm/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Russell King1234e3f2015-07-24 09:10:55 +010041#include "dma.h"
Russell King022ae532011-07-08 21:26:59 +010042#include "mm.h"
43
Rabin Vincentb4268672016-03-03 15:58:01 +010044struct arm_dma_alloc_args {
45 struct device *dev;
46 size_t size;
47 gfp_t gfp;
48 pgprot_t prot;
49 const void *caller;
50 bool want_vaddr;
Gregory CLEMENTf1270892016-04-15 11:15:18 +010051 int coherent_flag;
Rabin Vincentb4268672016-03-03 15:58:01 +010052};
53
54struct arm_dma_free_args {
55 struct device *dev;
56 size_t size;
57 void *cpu_addr;
58 struct page *page;
59 bool want_vaddr;
60};
61
Gregory CLEMENTf1270892016-04-15 11:15:18 +010062#define NORMAL 0
63#define COHERENT 1
64
Rabin Vincentb4268672016-03-03 15:58:01 +010065struct arm_dma_allocator {
66 void *(*alloc)(struct arm_dma_alloc_args *args,
67 struct page **ret_page);
68 void (*free)(struct arm_dma_free_args *args);
69};
70
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010071struct arm_dma_buffer {
72 struct list_head list;
73 void *virt;
Rabin Vincentb4268672016-03-03 15:58:01 +010074 struct arm_dma_allocator *allocator;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +010075};
76
77static LIST_HEAD(arm_dma_bufs);
78static DEFINE_SPINLOCK(arm_dma_bufs_lock);
79
80static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
81{
82 struct arm_dma_buffer *buf, *found = NULL;
83 unsigned long flags;
84
85 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
86 list_for_each_entry(buf, &arm_dma_bufs, list) {
87 if (buf->virt == virt) {
88 list_del(&buf->list);
89 found = buf;
90 break;
91 }
92 }
93 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
94 return found;
95}
96
Marek Szyprowski15237e12012-02-10 19:55:20 +010097/*
98 * The DMA API is built upon the notion of "buffer ownership". A buffer
99 * is either exclusively owned by the CPU (and therefore may be accessed
100 * by it) or exclusively owned by the DMA device. These helper functions
101 * represent the transitions between these two ownership states.
102 *
103 * Note, however, that on later ARMs, this notion does not work due to
104 * speculative prefetches. We model our approach on the assumption that
105 * the CPU does do speculative prefetches, which means we clean caches
106 * before transfers and delay cache invalidation until transfer completion.
107 *
Marek Szyprowski15237e12012-02-10 19:55:20 +0100108 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100109static void __dma_page_cpu_to_dev(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100110 size_t, enum dma_data_direction);
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100111static void __dma_page_dev_to_cpu(struct page *, unsigned long,
Marek Szyprowski15237e12012-02-10 19:55:20 +0100112 size_t, enum dma_data_direction);
113
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100114/**
115 * arm_dma_map_page - map a portion of a page for streaming DMA
116 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
117 * @page: page that buffer resides in
118 * @offset: offset into page for start of buffer
119 * @size: size of buffer to map
120 * @dir: DMA transfer direction
121 *
122 * Ensure that any data held in the cache is appropriately discarded
123 * or written back.
124 *
125 * The device owns this memory once this call has completed. The CPU
126 * can regain ownership by calling dma_unmap_page().
127 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100128static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100129 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700130 unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100131{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700132 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100133 __dma_page_cpu_to_dev(page, offset, size, dir);
134 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100135}
136
Rob Herringdd37e942012-08-21 12:20:17 +0200137static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
138 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700139 unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200140{
141 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
142}
143
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100144/**
145 * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
146 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
147 * @handle: DMA address of buffer
148 * @size: size of buffer (same as passed to dma_map_page)
149 * @dir: DMA transfer direction (same as passed to dma_map_page)
150 *
151 * Unmap a page streaming mode DMA translation. The handle and size
152 * must match what was provided in the previous dma_map_page() call.
153 * All other usages are undefined.
154 *
155 * After this call, reads by the CPU to the buffer are guaranteed to see
156 * whatever the device wrote there.
157 */
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100158static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700159 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100160{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700161 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100162 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
163 handle & ~PAGE_MASK, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100164}
165
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100166static void arm_dma_sync_single_for_cpu(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100167 dma_addr_t handle, size_t size, enum dma_data_direction dir)
168{
169 unsigned int offset = handle & (PAGE_SIZE - 1);
170 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200171 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100172}
173
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100174static void arm_dma_sync_single_for_device(struct device *dev,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100175 dma_addr_t handle, size_t size, enum dma_data_direction dir)
176{
177 unsigned int offset = handle & (PAGE_SIZE - 1);
178 struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
Rob Herringdd37e942012-08-21 12:20:17 +0200179 __dma_page_cpu_to_dev(page, offset, size, dir);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100180}
181
Bart Van Assche52997092017-01-20 13:04:01 -0800182const struct dma_map_ops arm_dma_ops = {
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200183 .alloc = arm_dma_alloc,
184 .free = arm_dma_free,
185 .mmap = arm_dma_mmap,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200186 .get_sgtable = arm_dma_get_sgtable,
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100187 .map_page = arm_dma_map_page,
188 .unmap_page = arm_dma_unmap_page,
189 .map_sg = arm_dma_map_sg,
190 .unmap_sg = arm_dma_unmap_sg,
191 .sync_single_for_cpu = arm_dma_sync_single_for_cpu,
192 .sync_single_for_device = arm_dma_sync_single_for_device,
193 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
194 .sync_sg_for_device = arm_dma_sync_sg_for_device,
Christoph Hellwig418a7a72017-05-22 11:20:18 +0200195 .dma_supported = arm_dma_supported,
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,
Christoph Hellwig418a7a72017-05-22 11:20:18 +0200214 .dma_supported = arm_dma_supported,
Rob Herringdd37e942012-08-21 12:20:17 +0200215};
216EXPORT_SYMBOL(arm_coherent_dma_ops);
217
Russell King9f28cde2013-12-06 12:30:42 +0000218static int __dma_supported(struct device *dev, u64 mask, bool warn)
219{
220 unsigned long max_dma_pfn;
221
222 /*
223 * If the mask allows for more memory than we can address,
224 * and we actually have that much memory, then we must
225 * indicate that DMA to this device is not supported.
226 */
227 if (sizeof(mask) != sizeof(dma_addr_t) &&
228 mask > (dma_addr_t)~0 &&
Russell King8bf12682015-03-10 16:41:35 +0000229 dma_to_pfn(dev, ~0) < max_pfn - 1) {
Russell King9f28cde2013-12-06 12:30:42 +0000230 if (warn) {
231 dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
232 mask);
233 dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
234 }
235 return 0;
236 }
237
238 max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
239
240 /*
241 * Translate the device's DMA mask to a PFN limit. This
242 * PFN number includes the page which we can DMA to.
243 */
244 if (dma_to_pfn(dev, mask) < max_dma_pfn) {
245 if (warn)
246 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",
247 mask,
248 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
249 max_dma_pfn + 1);
250 return 0;
251 }
252
253 return 1;
254}
255
Catalin Marinasab6494f2009-07-24 12:35:02 +0100256static u64 get_coherent_dma_mask(struct device *dev)
257{
Russell King4dcfa602013-07-09 12:14:49 +0100258 u64 mask = (u64)DMA_BIT_MASK(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Catalin Marinasab6494f2009-07-24 12:35:02 +0100260 if (dev) {
261 mask = dev->coherent_dma_mask;
262
263 /*
264 * Sanity check the DMA mask - it must be non-zero, and
265 * must be able to be satisfied by a DMA allocation.
266 */
267 if (mask == 0) {
268 dev_warn(dev, "coherent DMA mask is unset\n");
269 return 0;
270 }
271
Russell King9f28cde2013-12-06 12:30:42 +0000272 if (!__dma_supported(dev, mask, true))
Russell King4dcfa602013-07-09 12:14:49 +0100273 return 0;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100274 }
275
276 return mask;
277}
278
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100279static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100280{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100281 /*
282 * Ensure that the allocated pages are zeroed, and that any data
283 * lurking in the kernel direct-mapped region is invalidated.
284 */
Marek Szyprowski9848e482013-01-16 15:38:44 +0100285 if (PageHighMem(page)) {
286 phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
287 phys_addr_t end = base + size;
288 while (size > 0) {
289 void *ptr = kmap_atomic(page);
290 memset(ptr, 0, PAGE_SIZE);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100291 if (coherent_flag != COHERENT)
292 dmac_flush_range(ptr, ptr + PAGE_SIZE);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100293 kunmap_atomic(ptr);
294 page++;
295 size -= PAGE_SIZE;
296 }
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100297 if (coherent_flag != COHERENT)
298 outer_flush_range(base, end);
Marek Szyprowski9848e482013-01-16 15:38:44 +0100299 } else {
300 void *ptr = page_address(page);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200301 memset(ptr, 0, size);
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100302 if (coherent_flag != COHERENT) {
303 dmac_flush_range(ptr, ptr + size);
304 outer_flush_range(__pa(ptr), __pa(ptr) + size);
305 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +0200306 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100307}
308
Russell King7a9a32a2009-11-19 15:31:07 +0000309/*
310 * Allocate a DMA buffer for 'dev' of size 'size' using the
311 * specified gfp mask. Note that 'size' must be page aligned.
312 */
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100313static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
314 gfp_t gfp, int coherent_flag)
Russell King7a9a32a2009-11-19 15:31:07 +0000315{
316 unsigned long order = get_order(size);
317 struct page *page, *p, *e;
Russell King7a9a32a2009-11-19 15:31:07 +0000318
319 page = alloc_pages(gfp, order);
320 if (!page)
321 return NULL;
322
323 /*
324 * Now split the huge page and free the excess pages
325 */
326 split_page(page, order);
327 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
328 __free_page(p);
329
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100330 __dma_clear_buffer(page, size, coherent_flag);
Russell King7a9a32a2009-11-19 15:31:07 +0000331
332 return page;
333}
334
335/*
336 * Free a DMA buffer. 'size' must be page aligned.
337 */
338static void __dma_free_buffer(struct page *page, size_t size)
339{
340 struct page *e = page + (size >> PAGE_SHIFT);
341
342 while (page < e) {
343 __free_page(page);
344 page++;
345 }
346}
347
Marek Szyprowskic7909502011-12-29 13:09:51 +0100348static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100349 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100350 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800351 int coherent_flag, gfp_t gfp);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100352
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200353static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
354 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100355 const void *caller, bool want_vaddr);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200356
357static void *
358__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
359 const void *caller)
360{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200361 /*
362 * DMA allocation can be mapped to user space, so lets
363 * set VM_USERMAP flags too.
364 */
Laura Abbott513510d2014-10-09 15:26:40 -0700365 return dma_common_contiguous_remap(page, size,
366 VM_ARM_DMA_CONSISTENT | VM_USERMAP,
367 prot, caller);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200368}
369
370static void __dma_free_remap(void *cpu_addr, size_t size)
371{
Laura Abbott513510d2014-10-09 15:26:40 -0700372 dma_common_free_remap(cpu_addr, size,
373 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200374}
375
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200376#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Vladimir Murzinb337e1c2017-09-25 10:29:07 +0100377static struct gen_pool *atomic_pool __ro_after_init;
Marek Szyprowski6e5267a2012-08-20 11:19:25 +0200378
Vladimir Murzinb337e1c2017-09-25 10:29:07 +0100379static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100380
381static int __init early_coherent_pool(char *p)
382{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700383 atomic_pool_size = memparse(p, &p);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100384 return 0;
385}
386early_param("coherent_pool", early_coherent_pool);
387
388/*
389 * Initialise the coherent pool for atomic allocations.
390 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200391static int __init atomic_pool_init(void)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100392{
Russell King71b55662013-11-25 12:01:03 +0000393 pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
Marek Szyprowski9d1400c2013-02-26 07:46:24 +0100394 gfp_t gfp = GFP_KERNEL | GFP_DMA;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100395 struct page *page;
396 void *ptr;
397
Laura Abbott36d0fd22014-10-09 15:26:42 -0700398 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
399 if (!atomic_pool)
400 goto out;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100401 /*
402 * The atomic pool is only used for non-coherent allocations
403 * so we must pass NORMAL for coherent_flag.
404 */
Gioh Kime464ef12014-05-22 13:38:23 +0900405 if (dev_get_cma_area(NULL))
Laura Abbott36d0fd22014-10-09 15:26:42 -0700406 ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
Lucas Stach712c6042017-02-24 14:58:44 -0800407 &page, atomic_pool_init, true, NORMAL,
408 GFP_KERNEL);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200409 else
Laura Abbott36d0fd22014-10-09 15:26:42 -0700410 ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100411 &page, atomic_pool_init, true);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100412 if (ptr) {
Laura Abbott36d0fd22014-10-09 15:26:42 -0700413 int ret;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300414
Laura Abbott36d0fd22014-10-09 15:26:42 -0700415 ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
416 page_to_phys(page),
417 atomic_pool_size, -1);
418 if (ret)
419 goto destroy_genpool;
Hiroshi Doyu6b3fe472012-08-28 08:13:01 +0300420
Laura Abbott36d0fd22014-10-09 15:26:42 -0700421 gen_pool_set_algo(atomic_pool,
422 gen_pool_first_fit_order_align,
Vladimir Murzinacb62442017-09-25 10:25:53 +0100423 NULL);
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100424 pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700425 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100426 return 0;
427 }
Sachin Kamatec106652012-09-24 08:35:03 +0200428
Laura Abbott36d0fd22014-10-09 15:26:42 -0700429destroy_genpool:
430 gen_pool_destroy(atomic_pool);
431 atomic_pool = NULL;
432out:
Fabio Estevambf31c5e2016-07-18 13:09:36 +0100433 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
Laura Abbott36d0fd22014-10-09 15:26:42 -0700434 atomic_pool_size / 1024);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100435 return -ENOMEM;
436}
437/*
438 * CMA is activated by core_initcall, so we must be called after it.
439 */
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200440postcore_initcall(atomic_pool_init);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100441
442struct dma_contig_early_reserve {
443 phys_addr_t base;
444 unsigned long size;
445};
446
447static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
448
449static int dma_mmu_remap_num __initdata;
450
451void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
452{
453 dma_mmu_remap[dma_mmu_remap_num].base = base;
454 dma_mmu_remap[dma_mmu_remap_num].size = size;
455 dma_mmu_remap_num++;
456}
457
458void __init dma_contiguous_remap(void)
459{
460 int i;
461 for (i = 0; i < dma_mmu_remap_num; i++) {
462 phys_addr_t start = dma_mmu_remap[i].base;
463 phys_addr_t end = start + dma_mmu_remap[i].size;
464 struct map_desc map;
465 unsigned long addr;
466
467 if (end > arm_lowmem_limit)
468 end = arm_lowmem_limit;
469 if (start >= end)
Chris Brand39f78e72012-08-07 14:01:14 +0200470 continue;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100471
472 map.pfn = __phys_to_pfn(start);
473 map.virtual = __phys_to_virt(start);
474 map.length = end - start;
475 map.type = MT_MEMORY_DMA_READY;
476
477 /*
Russell King6b076992014-07-17 12:17:45 +0100478 * Clear previous low-memory mapping to ensure that the
479 * TLB does not see any conflicting entries, then flush
480 * the TLB of the old entries before creating new mappings.
481 *
482 * This ensures that any speculatively loaded TLB entries
483 * (even though they may be rare) can not cause any problems,
484 * and ensures that this code is architecturally compliant.
Marek Szyprowskic7909502011-12-29 13:09:51 +0100485 */
486 for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
Vitaly Andrianov61f6c7a2012-05-14 13:49:56 -0400487 addr += PMD_SIZE)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100488 pmd_clear(pmd_off_k(addr));
489
Russell King6b076992014-07-17 12:17:45 +0100490 flush_tlb_kernel_range(__phys_to_virt(start),
491 __phys_to_virt(end));
492
Joonsoo Kimd883c6c2018-05-23 10:18:21 +0900493 iotable_init(&map, 1);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100494 }
495}
496
Marek Szyprowskic7909502011-12-29 13:09:51 +0100497static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
498 void *data)
499{
500 struct page *page = virt_to_page(addr);
501 pgprot_t prot = *(pgprot_t *)data;
502
503 set_pte_ext(pte, mk_pte(page, prot), 0);
504 return 0;
505}
506
507static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
508{
509 unsigned long start = (unsigned long) page_address(page);
510 unsigned end = start + size;
511
512 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100513 flush_tlb_kernel_range(start, end);
514}
515
516static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
517 pgprot_t prot, struct page **ret_page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100518 const void *caller, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100519{
520 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100521 void *ptr = NULL;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100522 /*
523 * __alloc_remap_buffer is only called when the device is
524 * non-coherent
525 */
526 page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100527 if (!page)
528 return NULL;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100529 if (!want_vaddr)
530 goto out;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100531
532 ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
533 if (!ptr) {
534 __dma_free_buffer(page, size);
535 return NULL;
536 }
537
Carlo Caione6e8266e2015-02-09 10:38:35 +0100538 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100539 *ret_page = page;
540 return ptr;
541}
542
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200543static void *__alloc_from_pool(size_t size, struct page **ret_page)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100544{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700545 unsigned long val;
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200546 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100547
Laura Abbott36d0fd22014-10-09 15:26:42 -0700548 if (!atomic_pool) {
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200549 WARN(1, "coherent pool not initialised!\n");
Marek Szyprowskic7909502011-12-29 13:09:51 +0100550 return NULL;
551 }
552
Laura Abbott36d0fd22014-10-09 15:26:42 -0700553 val = gen_pool_alloc(atomic_pool, size);
554 if (val) {
555 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200556
Laura Abbott36d0fd22014-10-09 15:26:42 -0700557 *ret_page = phys_to_page(phys);
558 ptr = (void *)val;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100559 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200560
561 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100562}
563
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300564static bool __in_atomic_pool(void *start, size_t size)
565{
Laura Abbott36d0fd22014-10-09 15:26:42 -0700566 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300567}
568
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200569static int __free_from_pool(void *start, size_t size)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100570{
Hiroshi Doyu21d0a752012-08-28 08:13:02 +0300571 if (!__in_atomic_pool(start, size))
Marek Szyprowskic7909502011-12-29 13:09:51 +0100572 return 0;
573
Laura Abbott36d0fd22014-10-09 15:26:42 -0700574 gen_pool_free(atomic_pool, (unsigned long)start, size);
Marek Szyprowskie9da6e92012-07-30 09:11:33 +0200575
Marek Szyprowskic7909502011-12-29 13:09:51 +0100576 return 1;
577}
578
579static void *__alloc_from_contiguous(struct device *dev, size_t size,
Marek Szyprowski9848e482013-01-16 15:38:44 +0100580 pgprot_t prot, struct page **ret_page,
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100581 const void *caller, bool want_vaddr,
Lucas Stach712c6042017-02-24 14:58:44 -0800582 int coherent_flag, gfp_t gfp)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100583{
584 unsigned long order = get_order(size);
585 size_t count = size >> PAGE_SHIFT;
586 struct page *page;
Carlo Caione6e8266e2015-02-09 10:38:35 +0100587 void *ptr = NULL;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100588
Marek Szyprowskid834c5a2018-08-17 15:49:00 -0700589 page = dma_alloc_from_contiguous(dev, count, order, gfp & __GFP_NOWARN);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100590 if (!page)
591 return NULL;
592
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100593 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100594
Carlo Caione6e8266e2015-02-09 10:38:35 +0100595 if (!want_vaddr)
596 goto out;
597
Marek Szyprowski9848e482013-01-16 15:38:44 +0100598 if (PageHighMem(page)) {
599 ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
600 if (!ptr) {
601 dma_release_from_contiguous(dev, page, count);
602 return NULL;
603 }
604 } else {
605 __dma_remap(page, size, prot);
606 ptr = page_address(page);
607 }
Carlo Caione6e8266e2015-02-09 10:38:35 +0100608
609 out:
Marek Szyprowskic7909502011-12-29 13:09:51 +0100610 *ret_page = page;
Marek Szyprowski9848e482013-01-16 15:38:44 +0100611 return ptr;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100612}
613
614static void __free_from_contiguous(struct device *dev, struct page *page,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100615 void *cpu_addr, size_t size, bool want_vaddr)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100616{
Carlo Caione6e8266e2015-02-09 10:38:35 +0100617 if (want_vaddr) {
618 if (PageHighMem(page))
619 __dma_free_remap(cpu_addr, size);
620 else
621 __dma_remap(page, size, PAGE_KERNEL);
622 }
Marek Szyprowskic7909502011-12-29 13:09:51 +0100623 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
624}
625
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700626static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200627{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700628 prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
629 pgprot_writecombine(prot) :
630 pgprot_dmacoherent(prot);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200631 return prot;
632}
633
Marek Szyprowskic7909502011-12-29 13:09:51 +0100634static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
635 struct page **ret_page)
Catalin Marinasab6494f2009-07-24 12:35:02 +0100636{
Russell King04da5692009-11-19 15:54:45 +0000637 struct page *page;
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100638 /* __alloc_simple_buffer is only called when the device is coherent */
639 page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
Marek Szyprowskic7909502011-12-29 13:09:51 +0100640 if (!page)
641 return NULL;
642
643 *ret_page = page;
644 return page_address(page);
645}
646
Rabin Vincentb4268672016-03-03 15:58:01 +0100647static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
648 struct page **ret_page)
649{
650 return __alloc_simple_buffer(args->dev, args->size, args->gfp,
651 ret_page);
652}
Marek Szyprowskic7909502011-12-29 13:09:51 +0100653
Rabin Vincentb4268672016-03-03 15:58:01 +0100654static void simple_allocator_free(struct arm_dma_free_args *args)
655{
656 __dma_free_buffer(args->page, args->size);
657}
658
659static struct arm_dma_allocator simple_allocator = {
660 .alloc = simple_allocator_alloc,
661 .free = simple_allocator_free,
662};
663
664static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
665 struct page **ret_page)
666{
667 return __alloc_from_contiguous(args->dev, args->size, args->prot,
668 ret_page, args->caller,
Lucas Stach712c6042017-02-24 14:58:44 -0800669 args->want_vaddr, args->coherent_flag,
670 args->gfp);
Rabin Vincentb4268672016-03-03 15:58:01 +0100671}
672
673static void cma_allocator_free(struct arm_dma_free_args *args)
674{
675 __free_from_contiguous(args->dev, args->page, args->cpu_addr,
676 args->size, args->want_vaddr);
677}
678
679static struct arm_dma_allocator cma_allocator = {
680 .alloc = cma_allocator_alloc,
681 .free = cma_allocator_free,
682};
683
684static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
685 struct page **ret_page)
686{
687 return __alloc_from_pool(args->size, ret_page);
688}
689
690static void pool_allocator_free(struct arm_dma_free_args *args)
691{
692 __free_from_pool(args->cpu_addr, args->size);
693}
694
695static struct arm_dma_allocator pool_allocator = {
696 .alloc = pool_allocator_alloc,
697 .free = pool_allocator_free,
698};
699
700static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
701 struct page **ret_page)
702{
703 return __alloc_remap_buffer(args->dev, args->size, args->gfp,
704 args->prot, ret_page, args->caller,
705 args->want_vaddr);
706}
707
708static void remap_allocator_free(struct arm_dma_free_args *args)
709{
710 if (args->want_vaddr)
711 __dma_free_remap(args->cpu_addr, args->size);
712
713 __dma_free_buffer(args->page, args->size);
714}
715
716static struct arm_dma_allocator remap_allocator = {
717 .alloc = remap_allocator_alloc,
718 .free = remap_allocator_free,
719};
Marek Szyprowskic7909502011-12-29 13:09:51 +0100720
721static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100722 gfp_t gfp, pgprot_t prot, bool is_coherent,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700723 unsigned long attrs, const void *caller)
Marek Szyprowskic7909502011-12-29 13:09:51 +0100724{
725 u64 mask = get_coherent_dma_mask(dev);
Jingoo Han3dd7ea92012-10-24 14:09:14 +0900726 struct page *page = NULL;
Russell King31ebf942009-11-19 21:12:17 +0000727 void *addr;
Rabin Vincentb4268672016-03-03 15:58:01 +0100728 bool allowblock, cma;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100729 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100730 struct arm_dma_alloc_args args = {
731 .dev = dev,
732 .size = PAGE_ALIGN(size),
733 .gfp = gfp,
734 .prot = prot,
735 .caller = caller,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700736 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Gregory CLEMENTf1270892016-04-15 11:15:18 +0100737 .coherent_flag = is_coherent ? COHERENT : NORMAL,
Rabin Vincentb4268672016-03-03 15:58:01 +0100738 };
Catalin Marinasab6494f2009-07-24 12:35:02 +0100739
Marek Szyprowskic7909502011-12-29 13:09:51 +0100740#ifdef CONFIG_DMA_API_DEBUG
741 u64 limit = (mask + 1) & ~mask;
742 if (limit && size >= limit) {
743 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
744 size, mask);
745 return NULL;
746 }
747#endif
748
749 if (!mask)
750 return NULL;
751
Alexandre Courbot9c18fcf2016-04-13 05:55:29 +0100752 buf = kzalloc(sizeof(*buf),
753 gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100754 if (!buf)
755 return NULL;
756
Marek Szyprowskic7909502011-12-29 13:09:51 +0100757 if (mask < 0xffffffffULL)
758 gfp |= GFP_DMA;
759
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100760 /*
761 * Following is a work-around (a.k.a. hack) to prevent pages
762 * with __GFP_COMP being passed to split_page() which cannot
763 * handle them. The real problem is that this flag probably
764 * should be 0 on ARM as it is not supported on this
765 * platform; see CONFIG_HUGETLBFS.
766 */
767 gfp &= ~(__GFP_COMP);
Rabin Vincentb4268672016-03-03 15:58:01 +0100768 args.gfp = gfp;
Sumit Bhattacharyaea2e7052011-11-24 00:47:12 +0100769
Christoph Hellwig72fd97b2018-11-21 18:57:36 +0100770 *handle = DMA_MAPPING_ERROR;
Rabin Vincentb4268672016-03-03 15:58:01 +0100771 allowblock = gfpflags_allow_blocking(gfp);
772 cma = allowblock ? dev_get_cma_area(dev) : false;
Russell King04da5692009-11-19 15:54:45 +0000773
Rabin Vincentb4268672016-03-03 15:58:01 +0100774 if (cma)
775 buf->allocator = &cma_allocator;
Vladimir Murzin1655cf82017-05-24 11:24:32 +0100776 else if (is_coherent)
Rabin Vincentb4268672016-03-03 15:58:01 +0100777 buf->allocator = &simple_allocator;
778 else if (allowblock)
779 buf->allocator = &remap_allocator;
Russell King31ebf942009-11-19 21:12:17 +0000780 else
Rabin Vincentb4268672016-03-03 15:58:01 +0100781 buf->allocator = &pool_allocator;
782
783 addr = buf->allocator->alloc(&args, &page);
Russell King31ebf942009-11-19 21:12:17 +0000784
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100785 if (page) {
786 unsigned long flags;
787
Russell King9eedd962011-01-03 00:00:17 +0000788 *handle = pfn_to_dma(dev, page_to_pfn(page));
Rabin Vincentb4268672016-03-03 15:58:01 +0100789 buf->virt = args.want_vaddr ? addr : page;
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100790
791 spin_lock_irqsave(&arm_dma_bufs_lock, flags);
792 list_add(&buf->list, &arm_dma_bufs);
793 spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
794 } else {
795 kfree(buf);
796 }
Russell King31ebf942009-11-19 21:12:17 +0000797
Rabin Vincentb4268672016-03-03 15:58:01 +0100798 return args.want_vaddr ? addr : page;
Catalin Marinasab6494f2009-07-24 12:35:02 +0100799}
Russell King695ae0a2009-11-19 16:31:39 +0000800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * Allocate DMA-coherent memory space and return both the kernel remapped
803 * virtual and bus address for that space.
804 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200805void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700806 gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Russell King0ea1ec72013-10-23 16:14:59 +0100808 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +0400809
Rob Herringdd37e942012-08-21 12:20:17 +0200810 return __dma_alloc(dev, size, handle, gfp, prot, false,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100811 attrs, __builtin_return_address(0));
Rob Herringdd37e942012-08-21 12:20:17 +0200812}
813
814static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700815 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200816{
Lorenzo Nava21caf3a2015-07-02 17:28:03 +0100817 return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
Carlo Caione6e8266e2015-02-09 10:38:35 +0100818 attrs, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Mike Looijmans55af8a92015-06-03 11:25:31 +0100821static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200822 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700823 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Vladimir Murzin1655cf82017-05-24 11:24:32 +0100825 int ret;
Fabio Estevama70c3ee2018-04-23 12:30:27 +0100826 unsigned long nr_vma_pages = vma_pages(vma);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200827 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Marek Szyprowskic7909502011-12-29 13:09:51 +0100828 unsigned long pfn = dma_to_pfn(dev, dma_addr);
Marek Szyprowski50262a42012-07-30 09:35:26 +0200829 unsigned long off = vma->vm_pgoff;
830
Vladimir Murzin43fc5092017-07-20 11:19:58 +0100831 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Marek Szyprowski47142f02012-05-15 19:04:13 +0200832 return ret;
833
Marek Szyprowski50262a42012-07-30 09:35:26 +0200834 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
835 ret = remap_pfn_range(vma, vma->vm_start,
836 pfn + off,
837 vma->vm_end - vma->vm_start,
838 vma->vm_page_prot);
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 return ret;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
Mike Looijmans55af8a92015-06-03 11:25:31 +0100845 * Create userspace mapping for the DMA-coherent memory.
846 */
847static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
848 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700849 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100850{
851 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
852}
853
854int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
855 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700856 unsigned long attrs)
Mike Looijmans55af8a92015-06-03 11:25:31 +0100857{
Mike Looijmans55af8a92015-06-03 11:25:31 +0100858 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
Mike Looijmans55af8a92015-06-03 11:25:31 +0100859 return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
860}
861
862/*
Marek Szyprowskic7909502011-12-29 13:09:51 +0100863 * Free a buffer as defined by the above mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Rob Herringdd37e942012-08-21 12:20:17 +0200865static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700866 dma_addr_t handle, unsigned long attrs,
Rob Herringdd37e942012-08-21 12:20:17 +0200867 bool is_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Marek Szyprowskic7909502011-12-29 13:09:51 +0100869 struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100870 struct arm_dma_buffer *buf;
Rabin Vincentb4268672016-03-03 15:58:01 +0100871 struct arm_dma_free_args args = {
872 .dev = dev,
873 .size = PAGE_ALIGN(size),
874 .cpu_addr = cpu_addr,
875 .page = page,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700876 .want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
Rabin Vincentb4268672016-03-03 15:58:01 +0100877 };
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100878
879 buf = arm_dma_buffer_find(cpu_addr);
880 if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
881 return;
Russell King5edf71a2005-11-25 15:52:51 +0000882
Rabin Vincentb4268672016-03-03 15:58:01 +0100883 buf->allocator->free(&args);
Rabin Vincent19e6e5e2016-03-03 15:58:00 +0100884 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Russell Kingafd1a322008-09-25 16:30:57 +0100886
Rob Herringdd37e942012-08-21 12:20:17 +0200887void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700888 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200889{
890 __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
891}
892
893static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700894 dma_addr_t handle, unsigned long attrs)
Rob Herringdd37e942012-08-21 12:20:17 +0200895{
896 __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
897}
898
Russell King916a0082017-03-29 17:12:47 +0100899/*
900 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
901 * that the intention is to allow exporting memory allocated via the
902 * coherent DMA APIs through the dma_buf API, which only accepts a
903 * scattertable. This presents a couple of problems:
904 * 1. Not all memory allocated via the coherent DMA APIs is backed by
905 * a struct page
906 * 2. Passing coherent DMA memory into the streaming APIs is not allowed
907 * as we will try to flush the memory through a different alias to that
908 * actually being used (and the flushes are redundant.)
909 */
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200910int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
911 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700912 unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200913{
Russell King916a0082017-03-29 17:12:47 +0100914 unsigned long pfn = dma_to_pfn(dev, handle);
915 struct page *page;
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200916 int ret;
917
Russell King916a0082017-03-29 17:12:47 +0100918 /* If the PFN is not valid, we do not have a struct page */
919 if (!pfn_valid(pfn))
920 return -ENXIO;
921
922 page = pfn_to_page(pfn);
923
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200924 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
925 if (unlikely(ret))
926 return ret;
927
928 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
929 return 0;
930}
931
Russell King65af1912009-11-24 17:53:33 +0000932static void dma_cache_maint_page(struct page *page, unsigned long offset,
Russell Kinga9c91472009-11-26 16:19:58 +0000933 size_t size, enum dma_data_direction dir,
934 void (*op)(const void *, size_t, int))
Russell King65af1912009-11-24 17:53:33 +0000935{
Russell King15653372013-01-19 11:05:57 +0000936 unsigned long pfn;
937 size_t left = size;
938
939 pfn = page_to_pfn(page) + offset / PAGE_SIZE;
940 offset %= PAGE_SIZE;
941
Russell King65af1912009-11-24 17:53:33 +0000942 /*
943 * A single sg entry may refer to multiple physically contiguous
944 * pages. But we still need to process highmem pages individually.
945 * If highmem is not configured then the bulk of this loop gets
946 * optimized out.
947 */
Russell King65af1912009-11-24 17:53:33 +0000948 do {
949 size_t len = left;
Russell King93f1d622009-11-24 14:41:01 +0000950 void *vaddr;
951
Russell King15653372013-01-19 11:05:57 +0000952 page = pfn_to_page(pfn);
953
Russell King93f1d622009-11-24 14:41:01 +0000954 if (PageHighMem(page)) {
Russell King15653372013-01-19 11:05:57 +0000955 if (len + offset > PAGE_SIZE)
Russell King93f1d622009-11-24 14:41:01 +0000956 len = PAGE_SIZE - offset;
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100957
958 if (cache_is_vipt_nonaliasing()) {
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500959 vaddr = kmap_atomic(page);
Nicolas Pitre7e5a69e2010-03-29 21:46:02 +0100960 op(vaddr + offset, len, dir);
Nicolas Pitre39af22a2010-12-15 15:14:45 -0500961 kunmap_atomic(vaddr);
Joonsoo Kimdd0f67f2013-04-05 03:16:14 +0100962 } else {
963 vaddr = kmap_high_get(page);
964 if (vaddr) {
965 op(vaddr + offset, len, dir);
966 kunmap_high(page);
967 }
Russell King93f1d622009-11-24 14:41:01 +0000968 }
969 } else {
970 vaddr = page_address(page) + offset;
Russell Kinga9c91472009-11-26 16:19:58 +0000971 op(vaddr, len, dir);
Russell King65af1912009-11-24 17:53:33 +0000972 }
Russell King65af1912009-11-24 17:53:33 +0000973 offset = 0;
Russell King15653372013-01-19 11:05:57 +0000974 pfn++;
Russell King65af1912009-11-24 17:53:33 +0000975 left -= len;
976 } while (left);
977}
978
Marek Szyprowski51fde3492012-02-10 19:55:20 +0100979/*
980 * Make an area consistent for devices.
981 * Note: Drivers should NOT use this function directly, as it will break
982 * platforms with CONFIG_DMABOUNCE.
983 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
984 */
985static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
Russell King65af1912009-11-24 17:53:33 +0000986 size_t size, enum dma_data_direction dir)
987{
Santosh Shilimkar2161c242014-04-24 11:30:07 -0400988 phys_addr_t paddr;
Nicolas Pitre43377452009-03-12 22:52:09 -0400989
Russell Kinga9c91472009-11-26 16:19:58 +0000990 dma_cache_maint_page(page, off, size, dir, dmac_map_area);
Nicolas Pitre43377452009-03-12 22:52:09 -0400991
Russell King65af1912009-11-24 17:53:33 +0000992 paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +0000993 if (dir == DMA_FROM_DEVICE) {
994 outer_inv_range(paddr, paddr + size);
995 } else {
996 outer_clean_range(paddr, paddr + size);
997 }
998 /* FIXME: non-speculating: flush on bidirectional mappings? */
Nicolas Pitre43377452009-03-12 22:52:09 -0400999}
Russell King4ea0d732009-11-24 16:27:17 +00001000
Marek Szyprowski51fde3492012-02-10 19:55:20 +01001001static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
Russell King4ea0d732009-11-24 16:27:17 +00001002 size_t size, enum dma_data_direction dir)
1003{
Santosh Shilimkar2161c242014-04-24 11:30:07 -04001004 phys_addr_t paddr = page_to_phys(page) + off;
Russell King2ffe2da2009-10-31 16:52:16 +00001005
1006 /* FIXME: non-speculating: not required */
Russell Kingdeace4a2014-05-03 11:06:55 +01001007 /* in any case, don't bother invalidating if DMA to device */
1008 if (dir != DMA_TO_DEVICE) {
Russell King2ffe2da2009-10-31 16:52:16 +00001009 outer_inv_range(paddr, paddr + size);
1010
Russell Kingdeace4a2014-05-03 11:06:55 +01001011 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
1012 }
Catalin Marinasc0177802010-09-13 15:57:36 +01001013
1014 /*
Ming Leib2a234e2013-05-18 11:21:36 +01001015 * Mark the D-cache clean for these pages to avoid extra flushing.
Catalin Marinasc0177802010-09-13 15:57:36 +01001016 */
Ming Leib2a234e2013-05-18 11:21:36 +01001017 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
1018 unsigned long pfn;
1019 size_t left = size;
1020
1021 pfn = page_to_pfn(page) + off / PAGE_SIZE;
1022 off %= PAGE_SIZE;
1023 if (off) {
1024 pfn++;
1025 left -= PAGE_SIZE - off;
1026 }
1027 while (left >= PAGE_SIZE) {
1028 page = pfn_to_page(pfn++);
1029 set_bit(PG_dcache_clean, &page->flags);
1030 left -= PAGE_SIZE;
1031 }
1032 }
Russell King4ea0d732009-11-24 16:27:17 +00001033}
Nicolas Pitre43377452009-03-12 22:52:09 -04001034
Russell Kingafd1a322008-09-25 16:30:57 +01001035/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001036 * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
Russell Kingafd1a322008-09-25 16:30:57 +01001037 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1038 * @sg: list of buffers
1039 * @nents: number of buffers to map
1040 * @dir: DMA transfer direction
1041 *
1042 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1043 * This is the scatter-gather version of the dma_map_single interface.
1044 * Here the scatter gather list elements are each tagged with the
1045 * appropriate dma address and length. They are obtained via
1046 * sg_dma_{address,length}.
1047 *
1048 * Device ownership issues as mentioned for dma_map_single are the same
1049 * here.
1050 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001051int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001052 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001053{
Bart Van Assche52997092017-01-20 13:04:01 -08001054 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001055 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001056 int i, j;
Russell Kingafd1a322008-09-25 16:30:57 +01001057
1058 for_each_sg(sg, s, nents, i) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001059#ifdef CONFIG_NEED_SG_DMA_LENGTH
1060 s->dma_length = s->length;
1061#endif
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001062 s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1063 s->length, dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001064 if (dma_mapping_error(dev, s->dma_address))
1065 goto bad_mapping;
Russell Kingafd1a322008-09-25 16:30:57 +01001066 }
Russell Kingafd1a322008-09-25 16:30:57 +01001067 return nents;
Russell King01135d922008-09-25 21:05:02 +01001068
1069 bad_mapping:
1070 for_each_sg(sg, s, i, j)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001071 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell King01135d922008-09-25 21:05:02 +01001072 return 0;
Russell Kingafd1a322008-09-25 16:30:57 +01001073}
Russell Kingafd1a322008-09-25 16:30:57 +01001074
1075/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001076 * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
Russell Kingafd1a322008-09-25 16:30:57 +01001077 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1078 * @sg: list of buffers
Linus Walleij0adfca62011-01-12 18:50:37 +01001079 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
Russell Kingafd1a322008-09-25 16:30:57 +01001080 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1081 *
1082 * Unmap a set of streaming mode DMA translations. Again, CPU access
1083 * rules concerning calls here are the same as for dma_unmap_single().
1084 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001085void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001086 enum dma_data_direction dir, unsigned long attrs)
Russell Kingafd1a322008-09-25 16:30:57 +01001087{
Bart Van Assche52997092017-01-20 13:04:01 -08001088 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell King01135d922008-09-25 21:05:02 +01001089 struct scatterlist *s;
Russell King01135d922008-09-25 21:05:02 +01001090
Russell King01135d922008-09-25 21:05:02 +01001091 int i;
Russell King24056f52011-01-03 11:29:28 +00001092
Russell King01135d922008-09-25 21:05:02 +01001093 for_each_sg(sg, s, nents, i)
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001094 ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
Russell Kingafd1a322008-09-25 16:30:57 +01001095}
Russell Kingafd1a322008-09-25 16:30:57 +01001096
1097/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001098 * arm_dma_sync_sg_for_cpu
Russell Kingafd1a322008-09-25 16:30:57 +01001099 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1100 * @sg: list of buffers
1101 * @nents: number of buffers to map (returned from dma_map_sg)
1102 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1103 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001104void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001105 int nents, enum dma_data_direction dir)
1106{
Bart Van Assche52997092017-01-20 13:04:01 -08001107 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001108 struct scatterlist *s;
1109 int i;
1110
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001111 for_each_sg(sg, s, nents, i)
1112 ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1113 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001114}
Russell Kingafd1a322008-09-25 16:30:57 +01001115
1116/**
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001117 * arm_dma_sync_sg_for_device
Russell Kingafd1a322008-09-25 16:30:57 +01001118 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1119 * @sg: list of buffers
1120 * @nents: number of buffers to map (returned from dma_map_sg)
1121 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1122 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +01001123void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
Russell Kingafd1a322008-09-25 16:30:57 +01001124 int nents, enum dma_data_direction dir)
1125{
Bart Van Assche52997092017-01-20 13:04:01 -08001126 const struct dma_map_ops *ops = get_dma_ops(dev);
Russell Kingafd1a322008-09-25 16:30:57 +01001127 struct scatterlist *s;
1128 int i;
1129
Marek Szyprowski2a550e72012-02-10 19:55:20 +01001130 for_each_sg(sg, s, nents, i)
1131 ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1132 dir);
Russell Kingafd1a322008-09-25 16:30:57 +01001133}
Russell King24056f52011-01-03 11:29:28 +00001134
Russell King022ae532011-07-08 21:26:59 +01001135/*
1136 * Return whether the given device DMA address mask can be supported
1137 * properly. For example, if your device can only drive the low 24-bits
1138 * during bus mastering, then you would pass 0x00ffffff as the mask
1139 * to this function.
1140 */
Christoph Hellwig418a7a72017-05-22 11:20:18 +02001141int arm_dma_supported(struct device *dev, u64 mask)
Russell King022ae532011-07-08 21:26:59 +01001142{
Russell King9f28cde2013-12-06 12:30:42 +00001143 return __dma_supported(dev, mask, false);
Russell King022ae532011-07-08 21:26:59 +01001144}
Russell King022ae532011-07-08 21:26:59 +01001145
Thierry Reding18746192018-05-30 16:06:24 +02001146static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
1147{
1148 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
1149}
1150
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001151#ifdef CONFIG_ARM_DMA_USE_IOMMU
1152
Sricharan R7d2822d2017-01-06 18:58:13 +05301153static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
1154{
1155 int prot = 0;
1156
1157 if (attrs & DMA_ATTR_PRIVILEGED)
1158 prot |= IOMMU_PRIV;
1159
1160 switch (dir) {
1161 case DMA_BIDIRECTIONAL:
1162 return prot | IOMMU_READ | IOMMU_WRITE;
1163 case DMA_TO_DEVICE:
1164 return prot | IOMMU_READ;
1165 case DMA_FROM_DEVICE:
1166 return prot | IOMMU_WRITE;
1167 default:
1168 return prot;
1169 }
1170}
1171
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001172/* IOMMU */
1173
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001174static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1175
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001176static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1177 size_t size)
1178{
1179 unsigned int order = get_order(size);
1180 unsigned int align = 0;
1181 unsigned int count, start;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301182 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001183 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001184 dma_addr_t iova;
1185 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001186
Seung-Woo Kim60460ab2013-02-06 13:21:14 +09001187 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1188 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1189
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001190 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1191 align = (1 << order) - 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001192
1193 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001194 for (i = 0; i < mapping->nr_bitmaps; i++) {
1195 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1196 mapping->bits, 0, count, align);
1197
1198 if (start > mapping->bits)
1199 continue;
1200
1201 bitmap_set(mapping->bitmaps[i], start, count);
1202 break;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001203 }
1204
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001205 /*
1206 * No unused range found. Try to extend the existing mapping
1207 * and perform a second attempt to reserve an IO virtual
1208 * address range of size bytes.
1209 */
1210 if (i == mapping->nr_bitmaps) {
1211 if (extend_iommu_mapping(mapping)) {
1212 spin_unlock_irqrestore(&mapping->lock, flags);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001213 return DMA_MAPPING_ERROR;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001214 }
1215
1216 start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1217 mapping->bits, 0, count, align);
1218
1219 if (start > mapping->bits) {
1220 spin_unlock_irqrestore(&mapping->lock, flags);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001221 return DMA_MAPPING_ERROR;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001222 }
1223
1224 bitmap_set(mapping->bitmaps[i], start, count);
1225 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001226 spin_unlock_irqrestore(&mapping->lock, flags);
1227
Ritesh Harjani006f8412014-05-20 10:02:59 +05301228 iova = mapping->base + (mapping_size * i);
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001229 iova += start << PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001230
1231 return iova;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001232}
1233
1234static inline void __free_iova(struct dma_iommu_mapping *mapping,
1235 dma_addr_t addr, size_t size)
1236{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001237 unsigned int start, count;
Ritesh Harjani006f8412014-05-20 10:02:59 +05301238 size_t mapping_size = mapping->bits << PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001239 unsigned long flags;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001240 dma_addr_t bitmap_base;
1241 u32 bitmap_index;
1242
1243 if (!size)
1244 return;
1245
Ritesh Harjani006f8412014-05-20 10:02:59 +05301246 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001247 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1248
Ritesh Harjani006f8412014-05-20 10:02:59 +05301249 bitmap_base = mapping->base + mapping_size * bitmap_index;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001250
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001251 start = (addr - bitmap_base) >> PAGE_SHIFT;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001252
Ritesh Harjani006f8412014-05-20 10:02:59 +05301253 if (addr + size > bitmap_base + mapping_size) {
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001254 /*
1255 * The address range to be freed reaches into the iova
1256 * range of the next bitmap. This should not happen as
1257 * we don't allow this in __alloc_iova (at the
1258 * moment).
1259 */
1260 BUG();
1261 } else
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01001262 count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001263
1264 spin_lock_irqsave(&mapping->lock, flags);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01001265 bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001266 spin_unlock_irqrestore(&mapping->lock, flags);
1267}
1268
Doug Anderson33298ef2016-01-29 23:06:08 +01001269/* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1270static const int iommu_order_array[] = { 9, 8, 4, 0 };
1271
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001272static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001273 gfp_t gfp, unsigned long attrs,
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001274 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001275{
1276 struct page **pages;
1277 int count = size >> PAGE_SHIFT;
1278 int array_size = count * sizeof(struct page *);
1279 int i = 0;
Doug Anderson33298ef2016-01-29 23:06:08 +01001280 int order_idx = 0;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001281
1282 if (array_size <= PAGE_SIZE)
Alexandre Courbot23be7fd2015-02-19 07:29:58 +01001283 pages = kzalloc(array_size, GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001284 else
1285 pages = vzalloc(array_size);
1286 if (!pages)
1287 return NULL;
1288
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001289 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001290 {
1291 unsigned long order = get_order(size);
1292 struct page *page;
1293
Marek Szyprowskid834c5a2018-08-17 15:49:00 -07001294 page = dma_alloc_from_contiguous(dev, count, order,
1295 gfp & __GFP_NOWARN);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001296 if (!page)
1297 goto error;
1298
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001299 __dma_clear_buffer(page, size, coherent_flag);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001300
1301 for (i = 0; i < count; i++)
1302 pages[i] = page + i;
1303
1304 return pages;
1305 }
1306
Doug Anderson14d3ae22016-01-29 23:08:46 +01001307 /* Go straight to 4K chunks if caller says it's OK. */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001308 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Doug Anderson14d3ae22016-01-29 23:08:46 +01001309 order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1310
Marek Szyprowskif8669be2013-01-16 15:41:02 +01001311 /*
1312 * IOMMU can map any pages, so himem can also be used here
1313 */
1314 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1315
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001316 while (count) {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001317 int j, order;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001318
Doug Anderson33298ef2016-01-29 23:06:08 +01001319 order = iommu_order_array[order_idx];
1320
1321 /* Drop down when we get small */
1322 if (__fls(count) < order) {
1323 order_idx++;
1324 continue;
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001325 }
1326
Doug Anderson33298ef2016-01-29 23:06:08 +01001327 if (order) {
1328 /* See if it's easy to allocate a high-order chunk */
1329 pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1330
1331 /* Go down a notch at first sign of pressure */
1332 if (!pages[i]) {
1333 order_idx++;
1334 continue;
1335 }
1336 } else {
Tomasz Figa49f28aa2015-04-01 07:26:33 +01001337 pages[i] = alloc_pages(gfp, 0);
1338 if (!pages[i])
1339 goto error;
1340 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001341
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001342 if (order) {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001343 split_page(pages[i], order);
Hiroshi Doyu5a796ee2012-09-11 07:39:39 +02001344 j = 1 << order;
1345 while (--j)
1346 pages[i + j] = pages[i] + j;
1347 }
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001348
Gregory CLEMENTf1270892016-04-15 11:15:18 +01001349 __dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001350 i += 1 << order;
1351 count -= 1 << order;
1352 }
1353
1354 return pages;
1355error:
Marek Szyprowski9fa8af92012-07-27 17:12:50 +02001356 while (i--)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001357 if (pages[i])
1358 __free_pages(pages[i], 0);
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001359 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001360 return NULL;
1361}
1362
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001363static int __iommu_free_buffer(struct device *dev, struct page **pages,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001364 size_t size, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001365{
1366 int count = size >> PAGE_SHIFT;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001367 int i;
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001368
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001369 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001370 dma_release_from_contiguous(dev, pages[0], count);
1371 } else {
1372 for (i = 0; i < count; i++)
1373 if (pages[i])
1374 __free_pages(pages[i], 0);
1375 }
1376
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001377 kvfree(pages);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001378 return 0;
1379}
1380
1381/*
1382 * Create a CPU mapping for a specified pages
1383 */
1384static void *
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001385__iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1386 const void *caller)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001387{
Laura Abbott513510d2014-10-09 15:26:40 -07001388 return dma_common_pages_remap(pages, size,
1389 VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001390}
1391
1392/*
1393 * Create a mapping in device IO address space for specified pages
1394 */
1395static dma_addr_t
Sricharan R7d2822d2017-01-06 18:58:13 +05301396__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1397 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001398{
Will Deacon89cfdb12015-01-16 18:02:15 +01001399 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001400 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1401 dma_addr_t dma_addr, iova;
Andre Przywara90cde552015-09-14 17:49:02 +01001402 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001403
1404 dma_addr = __alloc_iova(mapping, size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001405 if (dma_addr == DMA_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001406 return dma_addr;
1407
1408 iova = dma_addr;
1409 for (i = 0; i < count; ) {
Andre Przywara90cde552015-09-14 17:49:02 +01001410 int ret;
1411
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001412 unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1413 phys_addr_t phys = page_to_phys(pages[i]);
1414 unsigned int len, j;
1415
1416 for (j = i + 1; j < count; j++, next_pfn++)
1417 if (page_to_pfn(pages[j]) != next_pfn)
1418 break;
1419
1420 len = (j - i) << PAGE_SHIFT;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001421 ret = iommu_map(mapping->domain, iova, phys, len,
Sricharan R7d2822d2017-01-06 18:58:13 +05301422 __dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001423 if (ret < 0)
1424 goto fail;
1425 iova += len;
1426 i = j;
1427 }
1428 return dma_addr;
1429fail:
1430 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1431 __free_iova(mapping, dma_addr, size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001432 return DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001433}
1434
1435static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1436{
Will Deacon89cfdb12015-01-16 18:02:15 +01001437 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001438
1439 /*
1440 * add optional in-page offset from iova to size and align
1441 * result to page size
1442 */
1443 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1444 iova &= PAGE_MASK;
1445
1446 iommu_unmap(mapping->domain, iova, size);
1447 __free_iova(mapping, iova, size);
1448 return 0;
1449}
1450
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001451static struct page **__atomic_get_pages(void *addr)
1452{
Laura Abbott36d0fd22014-10-09 15:26:42 -07001453 struct page *page;
1454 phys_addr_t phys;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001455
Laura Abbott36d0fd22014-10-09 15:26:42 -07001456 phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1457 page = phys_to_page(phys);
1458
1459 return (struct page **)page;
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001460}
1461
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001462static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001463{
1464 struct vm_struct *area;
1465
Hiroshi Doyu665bad72012-08-28 08:13:03 +03001466 if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1467 return __atomic_get_pages(cpu_addr);
1468
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001469 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001470 return cpu_addr;
1471
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001472 area = find_vm_area(cpu_addr);
1473 if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1474 return area->pages;
1475 return NULL;
1476}
1477
Gregory CLEMENT56506822016-04-15 11:20:13 +01001478static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
Sricharan R7d2822d2017-01-06 18:58:13 +05301479 dma_addr_t *handle, int coherent_flag,
1480 unsigned long attrs)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001481{
1482 struct page *page;
1483 void *addr;
1484
Gregory CLEMENT56506822016-04-15 11:20:13 +01001485 if (coherent_flag == COHERENT)
1486 addr = __alloc_simple_buffer(dev, size, gfp, &page);
1487 else
1488 addr = __alloc_from_pool(size, &page);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001489 if (!addr)
1490 return NULL;
1491
Sricharan R7d2822d2017-01-06 18:58:13 +05301492 *handle = __iommu_create_mapping(dev, &page, size, attrs);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001493 if (*handle == DMA_MAPPING_ERROR)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001494 goto err_mapping;
1495
1496 return addr;
1497
1498err_mapping:
1499 __free_from_pool(addr, size);
1500 return NULL;
1501}
1502
Marek Szyprowskid5898292013-02-08 10:54:48 +01001503static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001504 dma_addr_t handle, size_t size, int coherent_flag)
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001505{
1506 __iommu_remove_mapping(dev, handle, size);
Gregory CLEMENT56506822016-04-15 11:20:13 +01001507 if (coherent_flag == COHERENT)
1508 __dma_free_buffer(virt_to_page(cpu_addr), size);
1509 else
1510 __free_from_pool(cpu_addr, size);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001511}
1512
Gregory CLEMENT56506822016-04-15 11:20:13 +01001513static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001514 dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
Gregory CLEMENT56506822016-04-15 11:20:13 +01001515 int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001516{
Russell King71b55662013-11-25 12:01:03 +00001517 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001518 struct page **pages;
1519 void *addr = NULL;
1520
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001521 *handle = DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001522 size = PAGE_ALIGN(size);
1523
Gregory CLEMENT56506822016-04-15 11:20:13 +01001524 if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
1525 return __iommu_alloc_simple(dev, size, gfp, handle,
Sricharan R7d2822d2017-01-06 18:58:13 +05301526 coherent_flag, attrs);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001527
Richard Zhao5b91a982013-06-20 20:31:00 +08001528 /*
1529 * Following is a work-around (a.k.a. hack) to prevent pages
1530 * with __GFP_COMP being passed to split_page() which cannot
1531 * handle them. The real problem is that this flag probably
1532 * should be 0 on ARM as it is not supported on this
1533 * platform; see CONFIG_HUGETLBFS.
1534 */
1535 gfp &= ~(__GFP_COMP);
1536
Gregory CLEMENT56506822016-04-15 11:20:13 +01001537 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001538 if (!pages)
1539 return NULL;
1540
Sricharan R7d2822d2017-01-06 18:58:13 +05301541 *handle = __iommu_create_mapping(dev, pages, size, attrs);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001542 if (*handle == DMA_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001543 goto err_buffer;
1544
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001545 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
Marek Szyprowski955c7572012-05-16 19:38:58 +01001546 return pages;
1547
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001548 addr = __iommu_alloc_remap(pages, size, gfp, prot,
1549 __builtin_return_address(0));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001550 if (!addr)
1551 goto err_mapping;
1552
1553 return addr;
1554
1555err_mapping:
1556 __iommu_remove_mapping(dev, *handle, size);
1557err_buffer:
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001558 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001559 return NULL;
1560}
1561
Gregory CLEMENT56506822016-04-15 11:20:13 +01001562static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001563 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001564{
1565 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1566}
1567
1568static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001569 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001570{
1571 return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1572}
1573
1574static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001575 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001576 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001577{
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001578 unsigned long uaddr = vma->vm_start;
1579 unsigned long usize = vma->vm_end - vma->vm_start;
Marek Szyprowski955c7572012-05-16 19:38:58 +01001580 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001581 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1582 unsigned long off = vma->vm_pgoff;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001583
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001584 if (!pages)
1585 return -ENXIO;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001586
Marek Szyprowski371f0f02015-08-28 09:41:39 +01001587 if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
1588 return -ENXIO;
1589
Marek Szyprowski7e312102015-08-28 09:42:09 +01001590 pages += off;
1591
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001592 do {
1593 int ret = vm_insert_page(vma, uaddr, *pages++);
1594 if (ret) {
1595 pr_err("Remapping memory failed: %d\n", ret);
1596 return ret;
1597 }
1598 uaddr += PAGE_SIZE;
1599 usize -= PAGE_SIZE;
1600 } while (usize > 0);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001601
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001602 return 0;
1603}
Gregory CLEMENT56506822016-04-15 11:20:13 +01001604static int arm_iommu_mmap_attrs(struct device *dev,
1605 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001606 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001607{
1608 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1609
1610 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1611}
1612
1613static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1614 struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001615 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001616{
1617 return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1618}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001619
1620/*
1621 * free a page as defined by the above mapping.
1622 * Must not be called with IRQs disabled.
1623 */
Gregory CLEMENT56506822016-04-15 11:20:13 +01001624void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001625 dma_addr_t handle, unsigned long attrs, int coherent_flag)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001626{
YoungJun Cho836bfa02013-06-17 13:18:52 +09001627 struct page **pages;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001628 size = PAGE_ALIGN(size);
1629
Gregory CLEMENT56506822016-04-15 11:20:13 +01001630 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1631 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
Hiroshi Doyu479ed932012-08-28 08:13:04 +03001632 return;
1633 }
1634
YoungJun Cho836bfa02013-06-17 13:18:52 +09001635 pages = __iommu_get_pages(cpu_addr, attrs);
1636 if (!pages) {
1637 WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1638 return;
1639 }
1640
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001641 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) {
Laura Abbott513510d2014-10-09 15:26:40 -07001642 dma_common_free_remap(cpu_addr, size,
1643 VM_ARM_DMA_CONSISTENT | VM_USERMAP);
Marek Szyprowski955c7572012-05-16 19:38:58 +01001644 }
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001645
1646 __iommu_remove_mapping(dev, handle, size);
Marek Szyprowski549a17e2012-10-15 16:03:52 +02001647 __iommu_free_buffer(dev, pages, size, attrs);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001648}
1649
Gregory CLEMENT56506822016-04-15 11:20:13 +01001650void arm_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001651 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001652{
1653 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1654}
1655
1656void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001657 void *cpu_addr, dma_addr_t handle, unsigned long attrs)
Gregory CLEMENT56506822016-04-15 11:20:13 +01001658{
1659 __arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1660}
1661
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001662static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1663 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001664 size_t size, unsigned long attrs)
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02001665{
1666 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1667 struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1668
1669 if (!pages)
1670 return -ENXIO;
1671
1672 return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1673 GFP_KERNEL);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001674}
1675
1676/*
1677 * Map a part of the scatter-gather list into contiguous io address space
1678 */
1679static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1680 size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001681 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001682 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001683{
Will Deacon89cfdb12015-01-16 18:02:15 +01001684 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001685 dma_addr_t iova, iova_base;
1686 int ret = 0;
1687 unsigned int count;
1688 struct scatterlist *s;
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001689 int prot;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001690
1691 size = PAGE_ALIGN(size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001692 *handle = DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001693
1694 iova_base = iova = __alloc_iova(mapping, size);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001695 if (iova == DMA_MAPPING_ERROR)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001696 return -ENOMEM;
1697
1698 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001699 phys_addr_t phys = page_to_phys(sg_page(s));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001700 unsigned int len = PAGE_ALIGN(s->offset + s->length);
1701
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001702 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001703 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1704
Sricharan R7d2822d2017-01-06 18:58:13 +05301705 prot = __dma_info_to_prot(dir, attrs);
Andreas Herrmannc9b24992013-09-27 00:36:15 +02001706
1707 ret = iommu_map(mapping->domain, iova, phys, len, prot);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001708 if (ret < 0)
1709 goto fail;
1710 count += len >> PAGE_SHIFT;
1711 iova += len;
1712 }
1713 *handle = iova_base;
1714
1715 return 0;
1716fail:
1717 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1718 __free_iova(mapping, iova_base, size);
1719 return ret;
1720}
1721
Rob Herring0fa478d2012-08-21 12:23:23 +02001722static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001723 enum dma_data_direction dir, unsigned long attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02001724 bool is_coherent)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001725{
1726 struct scatterlist *s = sg, *dma = sg, *start = sg;
1727 int i, count = 0;
1728 unsigned int offset = s->offset;
1729 unsigned int size = s->offset + s->length;
1730 unsigned int max = dma_get_max_seg_size(dev);
1731
1732 for (i = 1; i < nents; i++) {
1733 s = sg_next(s);
1734
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001735 s->dma_address = DMA_MAPPING_ERROR;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001736 s->dma_length = 0;
1737
1738 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1739 if (__map_sg_chunk(dev, start, size, &dma->dma_address,
Rob Herring0fa478d2012-08-21 12:23:23 +02001740 dir, attrs, is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001741 goto bad_mapping;
1742
1743 dma->dma_address += offset;
1744 dma->dma_length = size - offset;
1745
1746 size = offset = s->offset;
1747 start = s;
1748 dma = sg_next(dma);
1749 count += 1;
1750 }
1751 size += s->length;
1752 }
Rob Herring0fa478d2012-08-21 12:23:23 +02001753 if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1754 is_coherent) < 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001755 goto bad_mapping;
1756
1757 dma->dma_address += offset;
1758 dma->dma_length = size - offset;
1759
1760 return count+1;
1761
1762bad_mapping:
1763 for_each_sg(sg, s, count, i)
1764 __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1765 return 0;
1766}
1767
1768/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001769 * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1770 * @dev: valid struct device pointer
1771 * @sg: list of buffers
1772 * @nents: number of buffers to map
1773 * @dir: DMA transfer direction
1774 *
1775 * Map a set of i/o coherent buffers described by scatterlist in streaming
1776 * mode for DMA. The scatter gather list elements are merged together (if
1777 * possible) and tagged with the appropriate dma address and length. They are
1778 * obtained via sg_dma_{address,length}.
1779 */
1780int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001781 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001782{
1783 return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1784}
1785
1786/**
1787 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1788 * @dev: valid struct device pointer
1789 * @sg: list of buffers
1790 * @nents: number of buffers to map
1791 * @dir: DMA transfer direction
1792 *
1793 * Map a set of buffers described by scatterlist in streaming mode for DMA.
1794 * The scatter gather list elements are merged together (if possible) and
1795 * tagged with the appropriate dma address and length. They are obtained via
1796 * sg_dma_{address,length}.
1797 */
1798int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001799 int nents, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001800{
1801 return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1802}
1803
1804static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001805 int nents, enum dma_data_direction dir,
1806 unsigned long attrs, bool is_coherent)
Rob Herring0fa478d2012-08-21 12:23:23 +02001807{
1808 struct scatterlist *s;
1809 int i;
1810
1811 for_each_sg(sg, s, nents, i) {
1812 if (sg_dma_len(s))
1813 __iommu_remove_mapping(dev, sg_dma_address(s),
1814 sg_dma_len(s));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001815 if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Rob Herring0fa478d2012-08-21 12:23:23 +02001816 __dma_page_dev_to_cpu(sg_page(s), s->offset,
1817 s->length, dir);
1818 }
1819}
1820
1821/**
1822 * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1823 * @dev: valid struct device pointer
1824 * @sg: list of buffers
1825 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1826 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1827 *
1828 * Unmap a set of streaming mode DMA translations. Again, CPU access
1829 * rules concerning calls here are the same as for dma_unmap_single().
1830 */
1831void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001832 int nents, enum dma_data_direction dir,
1833 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001834{
1835 __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1836}
1837
1838/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001839 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1840 * @dev: valid struct device pointer
1841 * @sg: list of buffers
1842 * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1843 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1844 *
1845 * Unmap a set of streaming mode DMA translations. Again, CPU access
1846 * rules concerning calls here are the same as for dma_unmap_single().
1847 */
1848void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001849 enum dma_data_direction dir,
1850 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001851{
Rob Herring0fa478d2012-08-21 12:23:23 +02001852 __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001853}
1854
1855/**
1856 * arm_iommu_sync_sg_for_cpu
1857 * @dev: valid struct device pointer
1858 * @sg: list of buffers
1859 * @nents: number of buffers to map (returned from dma_map_sg)
1860 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1861 */
1862void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1863 int nents, enum dma_data_direction dir)
1864{
1865 struct scatterlist *s;
1866 int i;
1867
1868 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001869 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001870
1871}
1872
1873/**
1874 * arm_iommu_sync_sg_for_device
1875 * @dev: valid struct device pointer
1876 * @sg: list of buffers
1877 * @nents: number of buffers to map (returned from dma_map_sg)
1878 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1879 */
1880void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1881 int nents, enum dma_data_direction dir)
1882{
1883 struct scatterlist *s;
1884 int i;
1885
1886 for_each_sg(sg, s, nents, i)
Rob Herring0fa478d2012-08-21 12:23:23 +02001887 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001888}
1889
1890
1891/**
Rob Herring0fa478d2012-08-21 12:23:23 +02001892 * arm_coherent_iommu_map_page
1893 * @dev: valid struct device pointer
1894 * @page: page that buffer resides in
1895 * @offset: offset into page for start of buffer
1896 * @size: size of buffer to map
1897 * @dir: DMA transfer direction
1898 *
1899 * Coherent IOMMU aware version of arm_dma_map_page()
1900 */
1901static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1902 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001903 unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001904{
Will Deacon89cfdb12015-01-16 18:02:15 +01001905 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001906 dma_addr_t dma_addr;
Will Deacon13987d62013-06-10 19:34:39 +01001907 int ret, prot, len = PAGE_ALIGN(size + offset);
Rob Herring0fa478d2012-08-21 12:23:23 +02001908
1909 dma_addr = __alloc_iova(mapping, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001910 if (dma_addr == DMA_MAPPING_ERROR)
Rob Herring0fa478d2012-08-21 12:23:23 +02001911 return dma_addr;
1912
Sricharan R7d2822d2017-01-06 18:58:13 +05301913 prot = __dma_info_to_prot(dir, attrs);
Will Deacon13987d62013-06-10 19:34:39 +01001914
1915 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
Rob Herring0fa478d2012-08-21 12:23:23 +02001916 if (ret < 0)
1917 goto fail;
1918
1919 return dma_addr + offset;
1920fail:
1921 __free_iova(mapping, dma_addr, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01001922 return DMA_MAPPING_ERROR;
Rob Herring0fa478d2012-08-21 12:23:23 +02001923}
1924
1925/**
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001926 * arm_iommu_map_page
1927 * @dev: valid struct device pointer
1928 * @page: page that buffer resides in
1929 * @offset: offset into page for start of buffer
1930 * @size: size of buffer to map
1931 * @dir: DMA transfer direction
1932 *
1933 * IOMMU aware version of arm_dma_map_page()
1934 */
1935static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1936 unsigned long offset, size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001937 unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001938{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001939 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001940 __dma_page_cpu_to_dev(page, offset, size, dir);
1941
Rob Herring0fa478d2012-08-21 12:23:23 +02001942 return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1943}
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001944
Rob Herring0fa478d2012-08-21 12:23:23 +02001945/**
1946 * arm_coherent_iommu_unmap_page
1947 * @dev: valid struct device pointer
1948 * @handle: DMA address of buffer
1949 * @size: size of buffer (same as passed to dma_map_page)
1950 * @dir: DMA transfer direction (same as passed to dma_map_page)
1951 *
1952 * Coherent IOMMU aware version of arm_dma_unmap_page()
1953 */
1954static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001955 size_t size, enum dma_data_direction dir, unsigned long attrs)
Rob Herring0fa478d2012-08-21 12:23:23 +02001956{
Will Deacon89cfdb12015-01-16 18:02:15 +01001957 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Rob Herring0fa478d2012-08-21 12:23:23 +02001958 dma_addr_t iova = handle & PAGE_MASK;
Rob Herring0fa478d2012-08-21 12:23:23 +02001959 int offset = handle & ~PAGE_MASK;
1960 int len = PAGE_ALIGN(size + offset);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001961
Rob Herring0fa478d2012-08-21 12:23:23 +02001962 if (!iova)
1963 return;
1964
1965 iommu_unmap(mapping->domain, iova, len);
1966 __free_iova(mapping, iova, len);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001967}
1968
1969/**
1970 * arm_iommu_unmap_page
1971 * @dev: valid struct device pointer
1972 * @handle: DMA address of buffer
1973 * @size: size of buffer (same as passed to dma_map_page)
1974 * @dir: DMA transfer direction (same as passed to dma_map_page)
1975 *
1976 * IOMMU aware version of arm_dma_unmap_page()
1977 */
1978static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001979 size_t size, enum dma_data_direction dir, unsigned long attrs)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001980{
Will Deacon89cfdb12015-01-16 18:02:15 +01001981 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001982 dma_addr_t iova = handle & PAGE_MASK;
1983 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1984 int offset = handle & ~PAGE_MASK;
1985 int len = PAGE_ALIGN(size + offset);
1986
1987 if (!iova)
1988 return;
1989
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001990 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02001991 __dma_page_dev_to_cpu(page, offset, size, dir);
1992
1993 iommu_unmap(mapping->domain, iova, len);
1994 __free_iova(mapping, iova, len);
1995}
1996
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02001997/**
1998 * arm_iommu_map_resource - map a device resource for DMA
1999 * @dev: valid struct device pointer
2000 * @phys_addr: physical address of resource
2001 * @size: size of resource to map
2002 * @dir: DMA transfer direction
2003 */
2004static dma_addr_t arm_iommu_map_resource(struct device *dev,
2005 phys_addr_t phys_addr, size_t size,
2006 enum dma_data_direction dir, unsigned long attrs)
2007{
2008 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2009 dma_addr_t dma_addr;
2010 int ret, prot;
2011 phys_addr_t addr = phys_addr & PAGE_MASK;
2012 unsigned int offset = phys_addr & ~PAGE_MASK;
2013 size_t len = PAGE_ALIGN(size + offset);
2014
2015 dma_addr = __alloc_iova(mapping, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01002016 if (dma_addr == DMA_MAPPING_ERROR)
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002017 return dma_addr;
2018
Sricharan R7d2822d2017-01-06 18:58:13 +05302019 prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002020
2021 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
2022 if (ret < 0)
2023 goto fail;
2024
2025 return dma_addr + offset;
2026fail:
2027 __free_iova(mapping, dma_addr, len);
Christoph Hellwig72fd97b2018-11-21 18:57:36 +01002028 return DMA_MAPPING_ERROR;
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002029}
2030
2031/**
2032 * arm_iommu_unmap_resource - unmap a device DMA resource
2033 * @dev: valid struct device pointer
2034 * @dma_handle: DMA address to resource
2035 * @size: size of resource to map
2036 * @dir: DMA transfer direction
2037 */
2038static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
2039 size_t size, enum dma_data_direction dir,
2040 unsigned long attrs)
2041{
2042 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2043 dma_addr_t iova = dma_handle & PAGE_MASK;
2044 unsigned int offset = dma_handle & ~PAGE_MASK;
2045 size_t len = PAGE_ALIGN(size + offset);
2046
2047 if (!iova)
2048 return;
2049
2050 iommu_unmap(mapping->domain, iova, len);
2051 __free_iova(mapping, iova, len);
2052}
2053
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002054static void arm_iommu_sync_single_for_cpu(struct device *dev,
2055 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2056{
Will Deacon89cfdb12015-01-16 18:02:15 +01002057 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002058 dma_addr_t iova = handle & PAGE_MASK;
2059 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2060 unsigned int offset = handle & ~PAGE_MASK;
2061
2062 if (!iova)
2063 return;
2064
Rob Herring0fa478d2012-08-21 12:23:23 +02002065 __dma_page_dev_to_cpu(page, offset, size, dir);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002066}
2067
2068static void arm_iommu_sync_single_for_device(struct device *dev,
2069 dma_addr_t handle, size_t size, enum dma_data_direction dir)
2070{
Will Deacon89cfdb12015-01-16 18:02:15 +01002071 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002072 dma_addr_t iova = handle & PAGE_MASK;
2073 struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2074 unsigned int offset = handle & ~PAGE_MASK;
2075
2076 if (!iova)
2077 return;
2078
2079 __dma_page_cpu_to_dev(page, offset, size, dir);
2080}
2081
Bart Van Assche52997092017-01-20 13:04:01 -08002082const struct dma_map_ops iommu_ops = {
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002083 .alloc = arm_iommu_alloc_attrs,
2084 .free = arm_iommu_free_attrs,
2085 .mmap = arm_iommu_mmap_attrs,
Marek Szyprowskidc2832e2012-06-13 10:01:15 +02002086 .get_sgtable = arm_iommu_get_sgtable,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002087
2088 .map_page = arm_iommu_map_page,
2089 .unmap_page = arm_iommu_unmap_page,
2090 .sync_single_for_cpu = arm_iommu_sync_single_for_cpu,
2091 .sync_single_for_device = arm_iommu_sync_single_for_device,
2092
2093 .map_sg = arm_iommu_map_sg,
2094 .unmap_sg = arm_iommu_unmap_sg,
2095 .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu,
2096 .sync_sg_for_device = arm_iommu_sync_sg_for_device,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002097
2098 .map_resource = arm_iommu_map_resource,
2099 .unmap_resource = arm_iommu_unmap_resource,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002100
Christoph Hellwig418a7a72017-05-22 11:20:18 +02002101 .dma_supported = arm_dma_supported,
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002102};
2103
Bart Van Assche52997092017-01-20 13:04:01 -08002104const struct dma_map_ops iommu_coherent_ops = {
Gregory CLEMENT56506822016-04-15 11:20:13 +01002105 .alloc = arm_coherent_iommu_alloc_attrs,
2106 .free = arm_coherent_iommu_free_attrs,
2107 .mmap = arm_coherent_iommu_mmap_attrs,
Rob Herring0fa478d2012-08-21 12:23:23 +02002108 .get_sgtable = arm_iommu_get_sgtable,
2109
2110 .map_page = arm_coherent_iommu_map_page,
2111 .unmap_page = arm_coherent_iommu_unmap_page,
2112
2113 .map_sg = arm_coherent_iommu_map_sg,
2114 .unmap_sg = arm_coherent_iommu_unmap_sg,
Niklas Söderlund24ed5d22016-08-10 13:22:17 +02002115
2116 .map_resource = arm_iommu_map_resource,
2117 .unmap_resource = arm_iommu_unmap_resource,
Christoph Hellwig9eef8b82017-05-22 10:53:03 +02002118
Christoph Hellwig418a7a72017-05-22 11:20:18 +02002119 .dma_supported = arm_dma_supported,
Rob Herring0fa478d2012-08-21 12:23:23 +02002120};
2121
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002122/**
2123 * arm_iommu_create_mapping
2124 * @bus: pointer to the bus holding the client device (for IOMMU calls)
2125 * @base: start address of the valid IO address space
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002126 * @size: maximum size of the valid IO address space
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002127 *
2128 * Creates a mapping structure which holds information about used/unused
2129 * IO address ranges, which is required to perform memory allocation and
2130 * mapping with IOMMU aware functions.
2131 *
2132 * The client device need to be attached to the mapping with
2133 * arm_iommu_attach_device function.
2134 */
2135struct dma_iommu_mapping *
Marek Szyprowski14245322015-04-29 11:29:19 +01002136arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002137{
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002138 unsigned int bits = size >> PAGE_SHIFT;
2139 unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002140 struct dma_iommu_mapping *mapping;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002141 int extensions = 1;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002142 int err = -ENOMEM;
2143
Marek Szyprowski14245322015-04-29 11:29:19 +01002144 /* currently only 32-bit DMA address space is supported */
2145 if (size > DMA_BIT_MASK(32) + 1)
2146 return ERR_PTR(-ERANGE);
2147
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002148 if (!bitmap_size)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002149 return ERR_PTR(-EINVAL);
2150
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002151 if (bitmap_size > PAGE_SIZE) {
2152 extensions = bitmap_size / PAGE_SIZE;
2153 bitmap_size = PAGE_SIZE;
2154 }
2155
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002156 mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2157 if (!mapping)
2158 goto err;
2159
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002160 mapping->bitmap_size = bitmap_size;
Kees Cook6396bb22018-06-12 14:03:40 -07002161 mapping->bitmaps = kcalloc(extensions, sizeof(unsigned long *),
2162 GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002163 if (!mapping->bitmaps)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002164 goto err2;
2165
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002166 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002167 if (!mapping->bitmaps[0])
2168 goto err3;
2169
2170 mapping->nr_bitmaps = 1;
2171 mapping->extensions = extensions;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002172 mapping->base = base;
Marek Szyprowski68efd7d2014-02-25 13:01:09 +01002173 mapping->bits = BITS_PER_BYTE * bitmap_size;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002174
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002175 spin_lock_init(&mapping->lock);
2176
2177 mapping->domain = iommu_domain_alloc(bus);
2178 if (!mapping->domain)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002179 goto err4;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002180
2181 kref_init(&mapping->kref);
2182 return mapping;
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002183err4:
2184 kfree(mapping->bitmaps[0]);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002185err3:
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002186 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002187err2:
2188 kfree(mapping);
2189err:
2190 return ERR_PTR(err);
2191}
Prathyush K18177d12013-01-04 06:22:42 -05002192EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002193
2194static void release_iommu_mapping(struct kref *kref)
2195{
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002196 int i;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002197 struct dma_iommu_mapping *mapping =
2198 container_of(kref, struct dma_iommu_mapping, kref);
2199
2200 iommu_domain_free(mapping->domain);
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002201 for (i = 0; i < mapping->nr_bitmaps; i++)
2202 kfree(mapping->bitmaps[i]);
2203 kfree(mapping->bitmaps);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002204 kfree(mapping);
2205}
2206
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002207static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2208{
2209 int next_bitmap;
2210
Marek Szyprowski462859aa2015-07-08 13:21:55 +01002211 if (mapping->nr_bitmaps >= mapping->extensions)
Andreas Herrmann4d852ef2014-02-25 13:09:53 +01002212 return -EINVAL;
2213
2214 next_bitmap = mapping->nr_bitmaps;
2215 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2216 GFP_ATOMIC);
2217 if (!mapping->bitmaps[next_bitmap])
2218 return -ENOMEM;
2219
2220 mapping->nr_bitmaps++;
2221
2222 return 0;
2223}
2224
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002225void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2226{
2227 if (mapping)
2228 kref_put(&mapping->kref, release_iommu_mapping);
2229}
Prathyush K18177d12013-01-04 06:22:42 -05002230EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002231
Laurent Pincharteab8d652015-01-23 16:21:49 +02002232static int __arm_iommu_attach_device(struct device *dev,
2233 struct dma_iommu_mapping *mapping)
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002234{
2235 int err;
2236
2237 err = iommu_attach_device(mapping->domain, dev);
2238 if (err)
2239 return err;
2240
2241 kref_get(&mapping->kref);
Will Deacon89cfdb12015-01-16 18:02:15 +01002242 to_dma_iommu_mapping(dev) = mapping;
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002243
Hiroshi Doyu75c59712012-09-11 07:39:48 +02002244 pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
Marek Szyprowski4ce63fc2012-05-16 15:48:21 +02002245 return 0;
2246}
2247
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002248/**
Laurent Pincharteab8d652015-01-23 16:21:49 +02002249 * arm_iommu_attach_device
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002250 * @dev: valid struct device pointer
Laurent Pincharteab8d652015-01-23 16:21:49 +02002251 * @mapping: io address space mapping structure (returned from
2252 * arm_iommu_create_mapping)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002253 *
Laurent Pincharteab8d652015-01-23 16:21:49 +02002254 * Attaches specified io address space mapping to the provided device.
2255 * This replaces the dma operations (dma_map_ops pointer) with the
2256 * IOMMU aware version.
2257 *
2258 * More than one client might be attached to the same io address space
2259 * mapping.
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002260 */
Laurent Pincharteab8d652015-01-23 16:21:49 +02002261int arm_iommu_attach_device(struct device *dev,
2262 struct dma_iommu_mapping *mapping)
2263{
2264 int err;
2265
2266 err = __arm_iommu_attach_device(dev, mapping);
2267 if (err)
2268 return err;
2269
2270 set_dma_ops(dev, &iommu_ops);
2271 return 0;
2272}
2273EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2274
Sricharan Rd3e01c52017-05-27 19:17:45 +05302275/**
2276 * arm_iommu_detach_device
2277 * @dev: valid struct device pointer
2278 *
2279 * Detaches the provided device from a previously attached map.
2280 * This voids the dma operations (dma_map_ops pointer)
2281 */
2282void arm_iommu_detach_device(struct device *dev)
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002283{
2284 struct dma_iommu_mapping *mapping;
2285
2286 mapping = to_dma_iommu_mapping(dev);
2287 if (!mapping) {
2288 dev_warn(dev, "Not attached\n");
2289 return;
2290 }
2291
2292 iommu_detach_device(mapping->domain, dev);
2293 kref_put(&mapping->kref, release_iommu_mapping);
Will Deacon89cfdb12015-01-16 18:02:15 +01002294 to_dma_iommu_mapping(dev) = NULL;
Thierry Reding18746192018-05-30 16:06:24 +02002295 set_dma_ops(dev, arm_get_dma_map_ops(dev->archdata.dma_coherent));
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002296
2297 pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2298}
Prathyush K18177d12013-01-04 06:22:42 -05002299EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
Hiroshi Doyu6fe36752013-01-24 15:16:57 +02002300
Bart Van Assche52997092017-01-20 13:04:01 -08002301static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002302{
2303 return coherent ? &iommu_coherent_ops : &iommu_ops;
2304}
2305
2306static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002307 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002308{
2309 struct dma_iommu_mapping *mapping;
2310
2311 if (!iommu)
2312 return false;
2313
2314 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2315 if (IS_ERR(mapping)) {
2316 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2317 size, dev_name(dev));
2318 return false;
2319 }
2320
Laurent Pincharteab8d652015-01-23 16:21:49 +02002321 if (__arm_iommu_attach_device(dev, mapping)) {
Will Deacon4bb25782014-08-27 17:52:44 +01002322 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2323 dev_name(dev));
2324 arm_iommu_release_mapping(mapping);
2325 return false;
2326 }
2327
2328 return true;
2329}
2330
2331static void arm_teardown_iommu_dma_ops(struct device *dev)
2332{
Will Deacon89cfdb12015-01-16 18:02:15 +01002333 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002334
Will Deaconc2273a12015-01-16 18:01:43 +01002335 if (!mapping)
2336 return;
2337
Sricharan Rd3e01c52017-05-27 19:17:45 +05302338 arm_iommu_detach_device(dev);
Will Deacon4bb25782014-08-27 17:52:44 +01002339 arm_iommu_release_mapping(mapping);
2340}
2341
2342#else
2343
2344static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002345 const struct iommu_ops *iommu)
Will Deacon4bb25782014-08-27 17:52:44 +01002346{
2347 return false;
2348}
2349
2350static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2351
2352#define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2353
2354#endif /* CONFIG_ARM_DMA_USE_IOMMU */
2355
Will Deacon4bb25782014-08-27 17:52:44 +01002356void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +01002357 const struct iommu_ops *iommu, bool coherent)
Will Deacon4bb25782014-08-27 17:52:44 +01002358{
Bart Van Assche52997092017-01-20 13:04:01 -08002359 const struct dma_map_ops *dma_ops;
Will Deacon4bb25782014-08-27 17:52:44 +01002360
Linus Torvalds6f51ee72014-12-16 14:53:01 -08002361 dev->archdata.dma_coherent = coherent;
Laurent Pinchart26b37b92015-05-15 02:00:02 +03002362
2363 /*
2364 * Don't override the dma_ops if they have already been set. Ideally
2365 * this should be the only location where dma_ops are set, remove this
2366 * check when all other callers of set_dma_ops will have disappeared.
2367 */
2368 if (dev->dma_ops)
2369 return;
2370
Will Deacon4bb25782014-08-27 17:52:44 +01002371 if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2372 dma_ops = arm_get_iommu_dma_map_ops(coherent);
2373 else
2374 dma_ops = arm_get_dma_map_ops(coherent);
2375
2376 set_dma_ops(dev, dma_ops);
Stefano Stabellinie0586322017-04-13 14:04:21 -07002377
2378#ifdef CONFIG_XEN
2379 if (xen_initial_domain()) {
2380 dev->archdata.dev_dma_ops = dev->dma_ops;
2381 dev->dma_ops = xen_dma_ops;
2382 }
2383#endif
Laurent Pincharta93a1212017-05-27 19:17:43 +05302384 dev->archdata.dma_ops_setup = true;
Will Deacon4bb25782014-08-27 17:52:44 +01002385}
2386
2387void arch_teardown_dma_ops(struct device *dev)
2388{
Laurent Pincharta93a1212017-05-27 19:17:43 +05302389 if (!dev->archdata.dma_ops_setup)
2390 return;
2391
Will Deacon4bb25782014-08-27 17:52:44 +01002392 arm_teardown_iommu_dma_ops(dev);
2393}