blob: cab3574ab7d94f7e6a0a65a0bd88516e0cb9fa5d [file] [log] [blame]
Catalin Marinas09b55412012-03-05 11:49:30 +00001/*
2 * SWIOTLB-based DMA API implementation
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/gfp.h>
Arnd Bergmann1dccb592015-11-16 17:25:48 +010021#include <linux/acpi.h>
Jisheng Zhangb67a8b22016-06-08 15:53:46 +080022#include <linux/bootmem.h>
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080023#include <linux/cache.h>
Catalin Marinas09b55412012-03-05 11:49:30 +000024#include <linux/export.h>
25#include <linux/slab.h>
Laura Abbottd4932f92014-10-09 15:26:44 -070026#include <linux/genalloc.h>
Catalin Marinas09b55412012-03-05 11:49:30 +000027#include <linux/dma-mapping.h>
Laura Abbott6ac21042013-12-12 19:28:33 +000028#include <linux/dma-contiguous.h>
Catalin Marinas09b55412012-03-05 11:49:30 +000029#include <linux/vmalloc.h>
30#include <linux/swiotlb.h>
31
32#include <asm/cacheflush.h>
33
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080034static int swiotlb __ro_after_init;
Jisheng Zhangb67a8b22016-06-08 15:53:46 +080035
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070036static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
Laura Abbott214fdbe2014-03-14 19:52:24 +000037 bool coherent)
38{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070039 if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
Laura Abbott214fdbe2014-03-14 19:52:24 +000040 return pgprot_writecombine(prot);
Laura Abbott214fdbe2014-03-14 19:52:24 +000041 return prot;
42}
43
Laura Abbottd4932f92014-10-09 15:26:44 -070044static struct gen_pool *atomic_pool;
45
46#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Jisheng Zhanga7c61a32015-11-20 17:59:10 +080047static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
Laura Abbottd4932f92014-10-09 15:26:44 -070048
49static int __init early_coherent_pool(char *p)
50{
51 atomic_pool_size = memparse(p, &p);
52 return 0;
53}
54early_param("coherent_pool", early_coherent_pool);
55
Suzuki K. Poulose71328132015-03-19 18:17:09 +000056static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
Laura Abbottd4932f92014-10-09 15:26:44 -070057{
58 unsigned long val;
59 void *ptr = NULL;
60
61 if (!atomic_pool) {
62 WARN(1, "coherent pool not initialised!\n");
63 return NULL;
64 }
65
66 val = gen_pool_alloc(atomic_pool, size);
67 if (val) {
68 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
69
70 *ret_page = phys_to_page(phys);
71 ptr = (void *)val;
Marek Szyprowski6829e272015-04-23 12:46:16 +010072 memset(ptr, 0, size);
Laura Abbottd4932f92014-10-09 15:26:44 -070073 }
74
75 return ptr;
76}
77
78static bool __in_atomic_pool(void *start, size_t size)
79{
80 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
81}
82
83static int __free_from_pool(void *start, size_t size)
84{
85 if (!__in_atomic_pool(start, size))
86 return 0;
87
88 gen_pool_free(atomic_pool, (unsigned long)start, size);
89
90 return 1;
91}
92
Ritesh Harjanibb10eb72014-02-06 17:21:51 +053093static void *__dma_alloc_coherent(struct device *dev, size_t size,
94 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070095 unsigned long attrs)
Catalin Marinas09b55412012-03-05 11:49:30 +000096{
Laura Abbottc666e8d2013-12-12 19:28:32 +000097 if (dev == NULL) {
98 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
99 return NULL;
100 }
101
Catalin Marinas19e76402014-02-27 12:09:22 +0000102 if (IS_ENABLED(CONFIG_ZONE_DMA) &&
Catalin Marinas09b55412012-03-05 11:49:30 +0000103 dev->coherent_dma_mask <= DMA_BIT_MASK(32))
Catalin Marinas19e76402014-02-27 12:09:22 +0000104 flags |= GFP_DMA;
Mel Gormand0164ad2015-11-06 16:28:21 -0800105 if (dev_get_cma_area(dev) && gfpflags_allow_blocking(flags)) {
Laura Abbott6ac21042013-12-12 19:28:33 +0000106 struct page *page;
Suzuki K. Poulose71328132015-03-19 18:17:09 +0000107 void *addr;
Laura Abbott6ac21042013-12-12 19:28:33 +0000108
109 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
110 get_order(size));
111 if (!page)
112 return NULL;
113
114 *dma_handle = phys_to_dma(dev, page_to_phys(page));
Suzuki K. Poulose71328132015-03-19 18:17:09 +0000115 addr = page_address(page);
Marek Szyprowski6829e272015-04-23 12:46:16 +0100116 memset(addr, 0, size);
Suzuki K. Poulose71328132015-03-19 18:17:09 +0000117 return addr;
Laura Abbott6ac21042013-12-12 19:28:33 +0000118 } else {
119 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
120 }
Catalin Marinas09b55412012-03-05 11:49:30 +0000121}
122
Ritesh Harjanibb10eb72014-02-06 17:21:51 +0530123static void __dma_free_coherent(struct device *dev, size_t size,
124 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700125 unsigned long attrs)
Catalin Marinas09b55412012-03-05 11:49:30 +0000126{
Laura Abbottd4932f92014-10-09 15:26:44 -0700127 bool freed;
128 phys_addr_t paddr = dma_to_phys(dev, dma_handle);
129
Laura Abbottc666e8d2013-12-12 19:28:32 +0000130 if (dev == NULL) {
131 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
132 return;
133 }
134
Laura Abbottd4932f92014-10-09 15:26:44 -0700135 freed = dma_release_from_contiguous(dev,
Laura Abbott6ac21042013-12-12 19:28:33 +0000136 phys_to_page(paddr),
137 size >> PAGE_SHIFT);
Laura Abbottd4932f92014-10-09 15:26:44 -0700138 if (!freed)
Laura Abbott6ac21042013-12-12 19:28:33 +0000139 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
Catalin Marinas09b55412012-03-05 11:49:30 +0000140}
141
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000142static void *__dma_alloc(struct device *dev, size_t size,
143 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700144 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100145{
Laura Abbottd4932f92014-10-09 15:26:44 -0700146 struct page *page;
Catalin Marinas73635902013-05-21 17:35:19 +0100147 void *ptr, *coherent_ptr;
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000148 bool coherent = is_device_dma_coherent(dev);
Robin Murphy97942c22015-07-31 18:28:34 +0100149 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, false);
Catalin Marinas73635902013-05-21 17:35:19 +0100150
151 size = PAGE_ALIGN(size);
Laura Abbottd4932f92014-10-09 15:26:44 -0700152
Mel Gormand0164ad2015-11-06 16:28:21 -0800153 if (!coherent && !gfpflags_allow_blocking(flags)) {
Laura Abbottd4932f92014-10-09 15:26:44 -0700154 struct page *page = NULL;
Suzuki K. Poulose71328132015-03-19 18:17:09 +0000155 void *addr = __alloc_from_pool(size, &page, flags);
Laura Abbottd4932f92014-10-09 15:26:44 -0700156
157 if (addr)
158 *dma_handle = phys_to_dma(dev, page_to_phys(page));
159
160 return addr;
Laura Abbottd4932f92014-10-09 15:26:44 -0700161 }
Catalin Marinas73635902013-05-21 17:35:19 +0100162
163 ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
164 if (!ptr)
165 goto no_mem;
Catalin Marinas73635902013-05-21 17:35:19 +0100166
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000167 /* no need for non-cacheable mapping if coherent */
168 if (coherent)
169 return ptr;
170
Catalin Marinas73635902013-05-21 17:35:19 +0100171 /* remove any dirty cache lines on the kernel alias */
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900172 __dma_flush_area(ptr, size);
Catalin Marinas73635902013-05-21 17:35:19 +0100173
174 /* create a coherent mapping */
175 page = virt_to_page(ptr);
Laura Abbottd4932f92014-10-09 15:26:44 -0700176 coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
Robin Murphy97942c22015-07-31 18:28:34 +0100177 prot, NULL);
Catalin Marinas73635902013-05-21 17:35:19 +0100178 if (!coherent_ptr)
179 goto no_map;
180
181 return coherent_ptr;
182
183no_map:
184 __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
185no_mem:
Sean Paula52ce122014-10-01 16:31:50 +0100186 *dma_handle = DMA_ERROR_CODE;
Catalin Marinas73635902013-05-21 17:35:19 +0100187 return NULL;
188}
189
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000190static void __dma_free(struct device *dev, size_t size,
191 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700192 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100193{
194 void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
195
Dean Nelson2cff98b2015-04-29 16:09:18 +0100196 size = PAGE_ALIGN(size);
197
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000198 if (!is_device_dma_coherent(dev)) {
199 if (__free_from_pool(vaddr, size))
200 return;
201 vunmap(vaddr);
202 }
Catalin Marinas73635902013-05-21 17:35:19 +0100203 __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
204}
205
206static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
207 unsigned long offset, size_t size,
208 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700209 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100210{
211 dma_addr_t dev_addr;
212
213 dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000214 if (!is_device_dma_coherent(dev))
215 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100216
217 return dev_addr;
218}
219
220
221static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
222 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700223 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100224{
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000225 if (!is_device_dma_coherent(dev))
226 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100227 swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
228}
229
230static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
231 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700232 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100233{
234 struct scatterlist *sg;
235 int i, ret;
236
237 ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000238 if (!is_device_dma_coherent(dev))
239 for_each_sg(sgl, sg, ret, i)
240 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
241 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100242
243 return ret;
244}
245
246static void __swiotlb_unmap_sg_attrs(struct device *dev,
247 struct scatterlist *sgl, int nelems,
248 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700249 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100250{
251 struct scatterlist *sg;
252 int i;
253
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000254 if (!is_device_dma_coherent(dev))
255 for_each_sg(sgl, sg, nelems, i)
256 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
257 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100258 swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
259}
260
261static void __swiotlb_sync_single_for_cpu(struct device *dev,
262 dma_addr_t dev_addr, size_t size,
263 enum dma_data_direction dir)
264{
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000265 if (!is_device_dma_coherent(dev))
266 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100267 swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
268}
269
270static void __swiotlb_sync_single_for_device(struct device *dev,
271 dma_addr_t dev_addr, size_t size,
272 enum dma_data_direction dir)
273{
274 swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000275 if (!is_device_dma_coherent(dev))
276 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100277}
278
279static void __swiotlb_sync_sg_for_cpu(struct device *dev,
280 struct scatterlist *sgl, int nelems,
281 enum dma_data_direction dir)
282{
283 struct scatterlist *sg;
284 int i;
285
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000286 if (!is_device_dma_coherent(dev))
287 for_each_sg(sgl, sg, nelems, i)
288 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
289 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100290 swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
291}
292
293static void __swiotlb_sync_sg_for_device(struct device *dev,
294 struct scatterlist *sgl, int nelems,
295 enum dma_data_direction dir)
296{
297 struct scatterlist *sg;
298 int i;
299
300 swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000301 if (!is_device_dma_coherent(dev))
302 for_each_sg(sgl, sg, nelems, i)
303 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
304 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100305}
306
Robin Murphyaaf6f2f2015-07-10 16:47:56 +0100307static int __swiotlb_mmap(struct device *dev,
308 struct vm_area_struct *vma,
309 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700310 unsigned long attrs)
Laura Abbott6e8d7962014-03-14 19:52:23 +0000311{
312 int ret = -ENXIO;
313 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
314 PAGE_SHIFT;
315 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
316 unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
317 unsigned long off = vma->vm_pgoff;
318
Robin Murphyaaf6f2f2015-07-10 16:47:56 +0100319 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
320 is_device_dma_coherent(dev));
321
Laura Abbott6e8d7962014-03-14 19:52:23 +0000322 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
323 return ret;
324
325 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
326 ret = remap_pfn_range(vma, vma->vm_start,
327 pfn + off,
328 vma->vm_end - vma->vm_start,
329 vma->vm_page_prot);
330 }
331
332 return ret;
333}
334
Robin Murphy1d1ddf62015-07-17 16:58:21 +0100335static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
336 void *cpu_addr, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700337 unsigned long attrs)
Robin Murphy1d1ddf62015-07-17 16:58:21 +0100338{
339 int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
340
341 if (!ret)
342 sg_set_page(sgt->sgl, phys_to_page(dma_to_phys(dev, handle)),
343 PAGE_ALIGN(size), 0);
344
345 return ret;
346}
347
Jisheng Zhangb67a8b22016-06-08 15:53:46 +0800348static int __swiotlb_dma_supported(struct device *hwdev, u64 mask)
349{
350 if (swiotlb)
351 return swiotlb_dma_supported(hwdev, mask);
352 return 1;
353}
354
Robin Murphya8123042017-01-25 18:31:31 +0000355static int __swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t addr)
356{
357 if (swiotlb)
358 return swiotlb_dma_mapping_error(hwdev, addr);
359 return 0;
360}
361
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000362static struct dma_map_ops swiotlb_dma_ops = {
363 .alloc = __dma_alloc,
364 .free = __dma_free,
365 .mmap = __swiotlb_mmap,
Robin Murphy1d1ddf62015-07-17 16:58:21 +0100366 .get_sgtable = __swiotlb_get_sgtable,
Catalin Marinas73635902013-05-21 17:35:19 +0100367 .map_page = __swiotlb_map_page,
368 .unmap_page = __swiotlb_unmap_page,
369 .map_sg = __swiotlb_map_sg_attrs,
370 .unmap_sg = __swiotlb_unmap_sg_attrs,
371 .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
372 .sync_single_for_device = __swiotlb_sync_single_for_device,
373 .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
374 .sync_sg_for_device = __swiotlb_sync_sg_for_device,
Jisheng Zhangb67a8b22016-06-08 15:53:46 +0800375 .dma_supported = __swiotlb_dma_supported,
Robin Murphya8123042017-01-25 18:31:31 +0000376 .mapping_error = __swiotlb_dma_mapping_error,
Catalin Marinas73635902013-05-21 17:35:19 +0100377};
Catalin Marinas09b55412012-03-05 11:49:30 +0000378
Laura Abbottd4932f92014-10-09 15:26:44 -0700379static int __init atomic_pool_init(void)
380{
381 pgprot_t prot = __pgprot(PROT_NORMAL_NC);
382 unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
383 struct page *page;
384 void *addr;
385 unsigned int pool_size_order = get_order(atomic_pool_size);
386
387 if (dev_get_cma_area(NULL))
388 page = dma_alloc_from_contiguous(NULL, nr_pages,
389 pool_size_order);
390 else
391 page = alloc_pages(GFP_DMA, pool_size_order);
392
393 if (page) {
394 int ret;
395 void *page_addr = page_address(page);
396
397 memset(page_addr, 0, atomic_pool_size);
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900398 __dma_flush_area(page_addr, atomic_pool_size);
Laura Abbottd4932f92014-10-09 15:26:44 -0700399
400 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
401 if (!atomic_pool)
402 goto free_page;
403
404 addr = dma_common_contiguous_remap(page, atomic_pool_size,
405 VM_USERMAP, prot, atomic_pool_init);
406
407 if (!addr)
408 goto destroy_genpool;
409
410 ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
411 page_to_phys(page),
412 atomic_pool_size, -1);
413 if (ret)
414 goto remove_mapping;
415
416 gen_pool_set_algo(atomic_pool,
417 gen_pool_first_fit_order_align,
418 (void *)PAGE_SHIFT);
419
420 pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
421 atomic_pool_size / 1024);
422 return 0;
423 }
424 goto out;
425
426remove_mapping:
427 dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
428destroy_genpool:
429 gen_pool_destroy(atomic_pool);
430 atomic_pool = NULL;
431free_page:
432 if (!dma_release_from_contiguous(NULL, page, nr_pages))
433 __free_pages(page, pool_size_order);
434out:
435 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
436 atomic_pool_size / 1024);
437 return -ENOMEM;
438}
439
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500440/********************************************
441 * The following APIs are for dummy DMA ops *
442 ********************************************/
443
444static void *__dummy_alloc(struct device *dev, size_t size,
445 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700446 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500447{
448 return NULL;
449}
450
451static void __dummy_free(struct device *dev, size_t size,
452 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700453 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500454{
455}
456
457static int __dummy_mmap(struct device *dev,
458 struct vm_area_struct *vma,
459 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700460 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500461{
462 return -ENXIO;
463}
464
465static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
466 unsigned long offset, size_t size,
467 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700468 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500469{
470 return DMA_ERROR_CODE;
471}
472
473static void __dummy_unmap_page(struct device *dev, dma_addr_t dev_addr,
474 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700475 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500476{
477}
478
479static int __dummy_map_sg(struct device *dev, struct scatterlist *sgl,
480 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700481 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500482{
483 return 0;
484}
485
486static void __dummy_unmap_sg(struct device *dev,
487 struct scatterlist *sgl, int nelems,
488 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700489 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500490{
491}
492
493static void __dummy_sync_single(struct device *dev,
494 dma_addr_t dev_addr, size_t size,
495 enum dma_data_direction dir)
496{
497}
498
499static void __dummy_sync_sg(struct device *dev,
500 struct scatterlist *sgl, int nelems,
501 enum dma_data_direction dir)
502{
503}
504
505static int __dummy_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
506{
507 return 1;
508}
509
510static int __dummy_dma_supported(struct device *hwdev, u64 mask)
511{
512 return 0;
513}
514
515struct dma_map_ops dummy_dma_ops = {
516 .alloc = __dummy_alloc,
517 .free = __dummy_free,
518 .mmap = __dummy_mmap,
519 .map_page = __dummy_map_page,
520 .unmap_page = __dummy_unmap_page,
521 .map_sg = __dummy_map_sg,
522 .unmap_sg = __dummy_unmap_sg,
523 .sync_single_for_cpu = __dummy_sync_single,
524 .sync_single_for_device = __dummy_sync_single,
525 .sync_sg_for_cpu = __dummy_sync_sg,
526 .sync_sg_for_device = __dummy_sync_sg,
527 .mapping_error = __dummy_mapping_error,
528 .dma_supported = __dummy_dma_supported,
529};
530EXPORT_SYMBOL(dummy_dma_ops);
531
Catalin Marinasa1e50a82015-02-05 18:01:53 +0000532static int __init arm64_dma_init(void)
Catalin Marinas09b55412012-03-05 11:49:30 +0000533{
Geert Uytterhoeven1fd1e6c2016-12-16 14:28:41 +0100534 if (swiotlb_force == SWIOTLB_FORCE ||
535 max_pfn > (arm64_dma_phys_limit >> PAGE_SHIFT))
Jisheng Zhangb67a8b22016-06-08 15:53:46 +0800536 swiotlb = 1;
537
Arnd Bergmann1dccb592015-11-16 17:25:48 +0100538 return atomic_pool_init();
Laura Abbottd4932f92014-10-09 15:26:44 -0700539}
540arch_initcall(arm64_dma_init);
Catalin Marinas09b55412012-03-05 11:49:30 +0000541
542#define PREALLOC_DMA_DEBUG_ENTRIES 4096
543
544static int __init dma_debug_do_init(void)
545{
546 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
547 return 0;
548}
549fs_initcall(dma_debug_do_init);
Robin Murphy13b86292015-10-01 20:13:59 +0100550
551
552#ifdef CONFIG_IOMMU_DMA
553#include <linux/dma-iommu.h>
554#include <linux/platform_device.h>
555#include <linux/amba/bus.h>
556
557/* Thankfully, all cache ops are by VA so we can ignore phys here */
558static void flush_page(struct device *dev, const void *virt, phys_addr_t phys)
559{
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900560 __dma_flush_area(virt, PAGE_SIZE);
Robin Murphy13b86292015-10-01 20:13:59 +0100561}
562
563static void *__iommu_alloc_attrs(struct device *dev, size_t size,
564 dma_addr_t *handle, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700565 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100566{
567 bool coherent = is_device_dma_coherent(dev);
568 int ioprot = dma_direction_to_prot(DMA_BIDIRECTIONAL, coherent);
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000569 size_t iosize = size;
Robin Murphy13b86292015-10-01 20:13:59 +0100570 void *addr;
571
572 if (WARN(!dev, "cannot create IOMMU mapping for unknown device\n"))
573 return NULL;
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000574
575 size = PAGE_ALIGN(size);
576
Robin Murphy13b86292015-10-01 20:13:59 +0100577 /*
578 * Some drivers rely on this, and we probably don't want the
579 * possibility of stale kernel data being read by devices anyway.
580 */
581 gfp |= __GFP_ZERO;
582
Andrew Mortonce5c2d22015-11-07 16:06:59 -0800583 if (gfpflags_allow_blocking(gfp)) {
Robin Murphy13b86292015-10-01 20:13:59 +0100584 struct page **pages;
585 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, coherent);
586
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100587 pages = iommu_dma_alloc(dev, iosize, gfp, attrs, ioprot,
588 handle, flush_page);
Robin Murphy13b86292015-10-01 20:13:59 +0100589 if (!pages)
590 return NULL;
591
592 addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
593 __builtin_return_address(0));
594 if (!addr)
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000595 iommu_dma_free(dev, pages, iosize, handle);
Robin Murphy13b86292015-10-01 20:13:59 +0100596 } else {
597 struct page *page;
598 /*
599 * In atomic context we can't remap anything, so we'll only
600 * get the virtually contiguous buffer we need by way of a
601 * physically contiguous allocation.
602 */
603 if (coherent) {
604 page = alloc_pages(gfp, get_order(size));
605 addr = page ? page_address(page) : NULL;
606 } else {
607 addr = __alloc_from_pool(size, &page, gfp);
608 }
609 if (!addr)
610 return NULL;
611
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000612 *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
Robin Murphy13b86292015-10-01 20:13:59 +0100613 if (iommu_dma_mapping_error(dev, *handle)) {
614 if (coherent)
615 __free_pages(page, get_order(size));
616 else
617 __free_from_pool(addr, size);
618 addr = NULL;
619 }
620 }
621 return addr;
622}
623
624static void __iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700625 dma_addr_t handle, unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100626{
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000627 size_t iosize = size;
628
629 size = PAGE_ALIGN(size);
Robin Murphy13b86292015-10-01 20:13:59 +0100630 /*
631 * @cpu_addr will be one of 3 things depending on how it was allocated:
632 * - A remapped array of pages from iommu_dma_alloc(), for all
633 * non-atomic allocations.
634 * - A non-cacheable alias from the atomic pool, for atomic
635 * allocations by non-coherent devices.
636 * - A normal lowmem address, for atomic allocations by
637 * coherent devices.
638 * Hence how dodgy the below logic looks...
639 */
640 if (__in_atomic_pool(cpu_addr, size)) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700641 iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
Robin Murphy13b86292015-10-01 20:13:59 +0100642 __free_from_pool(cpu_addr, size);
643 } else if (is_vmalloc_addr(cpu_addr)){
644 struct vm_struct *area = find_vm_area(cpu_addr);
645
646 if (WARN_ON(!area || !area->pages))
647 return;
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000648 iommu_dma_free(dev, area->pages, iosize, &handle);
Robin Murphy13b86292015-10-01 20:13:59 +0100649 dma_common_free_remap(cpu_addr, size, VM_USERMAP);
650 } else {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700651 iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
Robin Murphy13b86292015-10-01 20:13:59 +0100652 __free_pages(virt_to_page(cpu_addr), get_order(size));
653 }
654}
655
656static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
657 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700658 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100659{
660 struct vm_struct *area;
661 int ret;
662
663 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
664 is_device_dma_coherent(dev));
665
666 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
667 return ret;
668
669 area = find_vm_area(cpu_addr);
670 if (WARN_ON(!area || !area->pages))
671 return -ENXIO;
672
673 return iommu_dma_mmap(area->pages, size, vma);
674}
675
676static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
677 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700678 size_t size, unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100679{
680 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
681 struct vm_struct *area = find_vm_area(cpu_addr);
682
683 if (WARN_ON(!area || !area->pages))
684 return -ENXIO;
685
686 return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,
687 GFP_KERNEL);
688}
689
690static void __iommu_sync_single_for_cpu(struct device *dev,
691 dma_addr_t dev_addr, size_t size,
692 enum dma_data_direction dir)
693{
694 phys_addr_t phys;
695
696 if (is_device_dma_coherent(dev))
697 return;
698
699 phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
700 __dma_unmap_area(phys_to_virt(phys), size, dir);
701}
702
703static void __iommu_sync_single_for_device(struct device *dev,
704 dma_addr_t dev_addr, size_t size,
705 enum dma_data_direction dir)
706{
707 phys_addr_t phys;
708
709 if (is_device_dma_coherent(dev))
710 return;
711
712 phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
713 __dma_map_area(phys_to_virt(phys), size, dir);
714}
715
716static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
717 unsigned long offset, size_t size,
718 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700719 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100720{
721 bool coherent = is_device_dma_coherent(dev);
722 int prot = dma_direction_to_prot(dir, coherent);
723 dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
724
725 if (!iommu_dma_mapping_error(dev, dev_addr) &&
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700726 (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100727 __iommu_sync_single_for_device(dev, dev_addr, size, dir);
728
729 return dev_addr;
730}
731
732static void __iommu_unmap_page(struct device *dev, dma_addr_t dev_addr,
733 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700734 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100735{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700736 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100737 __iommu_sync_single_for_cpu(dev, dev_addr, size, dir);
738
739 iommu_dma_unmap_page(dev, dev_addr, size, dir, attrs);
740}
741
742static void __iommu_sync_sg_for_cpu(struct device *dev,
743 struct scatterlist *sgl, int nelems,
744 enum dma_data_direction dir)
745{
746 struct scatterlist *sg;
747 int i;
748
749 if (is_device_dma_coherent(dev))
750 return;
751
752 for_each_sg(sgl, sg, nelems, i)
753 __dma_unmap_area(sg_virt(sg), sg->length, dir);
754}
755
756static void __iommu_sync_sg_for_device(struct device *dev,
757 struct scatterlist *sgl, int nelems,
758 enum dma_data_direction dir)
759{
760 struct scatterlist *sg;
761 int i;
762
763 if (is_device_dma_coherent(dev))
764 return;
765
766 for_each_sg(sgl, sg, nelems, i)
767 __dma_map_area(sg_virt(sg), sg->length, dir);
768}
769
770static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
771 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700772 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100773{
774 bool coherent = is_device_dma_coherent(dev);
775
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700776 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100777 __iommu_sync_sg_for_device(dev, sgl, nelems, dir);
778
779 return iommu_dma_map_sg(dev, sgl, nelems,
780 dma_direction_to_prot(dir, coherent));
781}
782
783static void __iommu_unmap_sg_attrs(struct device *dev,
784 struct scatterlist *sgl, int nelems,
785 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700786 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100787{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700788 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100789 __iommu_sync_sg_for_cpu(dev, sgl, nelems, dir);
790
791 iommu_dma_unmap_sg(dev, sgl, nelems, dir, attrs);
792}
793
794static struct dma_map_ops iommu_dma_ops = {
795 .alloc = __iommu_alloc_attrs,
796 .free = __iommu_free_attrs,
797 .mmap = __iommu_mmap_attrs,
798 .get_sgtable = __iommu_get_sgtable,
799 .map_page = __iommu_map_page,
800 .unmap_page = __iommu_unmap_page,
801 .map_sg = __iommu_map_sg_attrs,
802 .unmap_sg = __iommu_unmap_sg_attrs,
803 .sync_single_for_cpu = __iommu_sync_single_for_cpu,
804 .sync_single_for_device = __iommu_sync_single_for_device,
805 .sync_sg_for_cpu = __iommu_sync_sg_for_cpu,
806 .sync_sg_for_device = __iommu_sync_sg_for_device,
807 .dma_supported = iommu_dma_supported,
808 .mapping_error = iommu_dma_mapping_error,
809};
810
811/*
812 * TODO: Right now __iommu_setup_dma_ops() gets called too early to do
813 * everything it needs to - the device is only partially created and the
814 * IOMMU driver hasn't seen it yet, so it can't have a group. Thus we
815 * need this delayed attachment dance. Once IOMMU probe ordering is sorted
816 * to move the arch_setup_dma_ops() call later, all the notifier bits below
817 * become unnecessary, and will go away.
818 */
819struct iommu_dma_notifier_data {
820 struct list_head list;
821 struct device *dev;
822 const struct iommu_ops *ops;
823 u64 dma_base;
824 u64 size;
825};
826static LIST_HEAD(iommu_dma_masters);
827static DEFINE_MUTEX(iommu_dma_notifier_lock);
828
Robin Murphy13b86292015-10-01 20:13:59 +0100829static bool do_iommu_attach(struct device *dev, const struct iommu_ops *ops,
830 u64 dma_base, u64 size)
831{
832 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
833
834 /*
Robin Murphy921b1f52016-04-19 17:01:32 +0100835 * If the IOMMU driver has the DMA domain support that we require,
836 * then the IOMMU core will have already configured a group for this
837 * device, and allocated the default domain for that group.
Robin Murphy13b86292015-10-01 20:13:59 +0100838 */
Will Deacondea9c752017-01-06 10:49:12 +0000839 if (!domain)
840 goto out_err;
841
842 if (domain->type == IOMMU_DOMAIN_DMA) {
843 if (iommu_dma_init_domain(domain, dma_base, size, dev))
844 goto out_err;
845
846 dev->archdata.dma_ops = &iommu_dma_ops;
Robin Murphy13b86292015-10-01 20:13:59 +0100847 }
848
Robin Murphy13b86292015-10-01 20:13:59 +0100849 return true;
Will Deacondea9c752017-01-06 10:49:12 +0000850out_err:
851 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
852 dev_name(dev));
853 return false;
Robin Murphy13b86292015-10-01 20:13:59 +0100854}
855
856static void queue_iommu_attach(struct device *dev, const struct iommu_ops *ops,
857 u64 dma_base, u64 size)
858{
859 struct iommu_dma_notifier_data *iommudata;
860
861 iommudata = kzalloc(sizeof(*iommudata), GFP_KERNEL);
862 if (!iommudata)
863 return;
864
865 iommudata->dev = dev;
866 iommudata->ops = ops;
867 iommudata->dma_base = dma_base;
868 iommudata->size = size;
869
870 mutex_lock(&iommu_dma_notifier_lock);
871 list_add(&iommudata->list, &iommu_dma_masters);
872 mutex_unlock(&iommu_dma_notifier_lock);
873}
874
875static int __iommu_attach_notifier(struct notifier_block *nb,
876 unsigned long action, void *data)
877{
878 struct iommu_dma_notifier_data *master, *tmp;
879
Lorenzo Pieralisi16c11322016-07-01 17:50:10 +0100880 if (action != BUS_NOTIFY_BIND_DRIVER)
Robin Murphy13b86292015-10-01 20:13:59 +0100881 return 0;
882
883 mutex_lock(&iommu_dma_notifier_lock);
884 list_for_each_entry_safe(master, tmp, &iommu_dma_masters, list) {
Lorenzo Pieralisi16c11322016-07-01 17:50:10 +0100885 if (data == master->dev && do_iommu_attach(master->dev,
886 master->ops, master->dma_base, master->size)) {
Robin Murphy13b86292015-10-01 20:13:59 +0100887 list_del(&master->list);
888 kfree(master);
Lorenzo Pieralisi16c11322016-07-01 17:50:10 +0100889 break;
Robin Murphy13b86292015-10-01 20:13:59 +0100890 }
891 }
892 mutex_unlock(&iommu_dma_notifier_lock);
893 return 0;
894}
895
Jisheng Zhanga7c61a32015-11-20 17:59:10 +0800896static int __init register_iommu_dma_ops_notifier(struct bus_type *bus)
Robin Murphy13b86292015-10-01 20:13:59 +0100897{
898 struct notifier_block *nb = kzalloc(sizeof(*nb), GFP_KERNEL);
899 int ret;
900
901 if (!nb)
902 return -ENOMEM;
Lorenzo Pieralisi16c11322016-07-01 17:50:10 +0100903
Robin Murphy13b86292015-10-01 20:13:59 +0100904 nb->notifier_call = __iommu_attach_notifier;
Robin Murphy13b86292015-10-01 20:13:59 +0100905
906 ret = bus_register_notifier(bus, nb);
907 if (ret) {
908 pr_warn("Failed to register DMA domain notifier; IOMMU DMA ops unavailable on bus '%s'\n",
909 bus->name);
910 kfree(nb);
911 }
912 return ret;
913}
914
915static int __init __iommu_dma_init(void)
916{
917 int ret;
918
919 ret = iommu_dma_init();
920 if (!ret)
921 ret = register_iommu_dma_ops_notifier(&platform_bus_type);
922 if (!ret)
923 ret = register_iommu_dma_ops_notifier(&amba_bustype);
Robin Murphy226d89c2016-04-19 17:01:31 +0100924#ifdef CONFIG_PCI
925 if (!ret)
926 ret = register_iommu_dma_ops_notifier(&pci_bus_type);
927#endif
Robin Murphy13b86292015-10-01 20:13:59 +0100928 return ret;
929}
930arch_initcall(__iommu_dma_init);
931
932static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
933 const struct iommu_ops *ops)
934{
935 struct iommu_group *group;
936
937 if (!ops)
938 return;
939 /*
940 * TODO: As a concession to the future, we're ready to handle being
941 * called both early and late (i.e. after bus_add_device). Once all
942 * the platform bus code is reworked to call us late and the notifier
943 * junk above goes away, move the body of do_iommu_attach here.
944 */
945 group = iommu_group_get(dev);
946 if (group) {
947 do_iommu_attach(dev, ops, dma_base, size);
948 iommu_group_put(group);
949 } else {
950 queue_iommu_attach(dev, ops, dma_base, size);
951 }
952}
953
Robin Murphy876945d2015-10-01 20:14:00 +0100954void arch_teardown_dma_ops(struct device *dev)
955{
956 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
957
Robin Murphy921b1f52016-04-19 17:01:32 +0100958 if (WARN_ON(domain))
Robin Murphy876945d2015-10-01 20:14:00 +0100959 iommu_detach_device(domain, dev);
Robin Murphy876945d2015-10-01 20:14:00 +0100960
961 dev->archdata.dma_ops = NULL;
962}
963
Robin Murphy13b86292015-10-01 20:13:59 +0100964#else
965
966static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +0100967 const struct iommu_ops *iommu)
Robin Murphy13b86292015-10-01 20:13:59 +0100968{ }
969
970#endif /* CONFIG_IOMMU_DMA */
971
Robin Murphy876945d2015-10-01 20:14:00 +0100972void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +0100973 const struct iommu_ops *iommu, bool coherent)
Robin Murphy876945d2015-10-01 20:14:00 +0100974{
Arnd Bergmann1dccb592015-11-16 17:25:48 +0100975 if (!dev->archdata.dma_ops)
976 dev->archdata.dma_ops = &swiotlb_dma_ops;
Robin Murphy876945d2015-10-01 20:14:00 +0100977
978 dev->archdata.dma_coherent = coherent;
979 __iommu_setup_dma_ops(dev, dma_base, size, iommu);
980}