blob: ed84432264defbc3aac8ba014b1a4eea1caa5210 [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>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +010027#include <linux/dma-direct.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>
Joerg Roedel461a6942017-04-26 15:46:20 +020031#include <linux/pci.h>
Catalin Marinas09b55412012-03-05 11:49:30 +000032
33#include <asm/cacheflush.h>
34
Jisheng Zhang5a9e3e12016-08-15 14:45:46 +080035static int swiotlb __ro_after_init;
Jisheng Zhangb67a8b22016-06-08 15:53:46 +080036
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070037static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot,
Laura Abbott214fdbe2014-03-14 19:52:24 +000038 bool coherent)
39{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070040 if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE))
Laura Abbott214fdbe2014-03-14 19:52:24 +000041 return pgprot_writecombine(prot);
Laura Abbott214fdbe2014-03-14 19:52:24 +000042 return prot;
43}
44
Vladimir Murzin8165f702017-08-14 09:55:47 +010045static struct gen_pool *atomic_pool __ro_after_init;
Laura Abbottd4932f92014-10-09 15:26:44 -070046
47#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
Jisheng Zhanga7c61a32015-11-20 17:59:10 +080048static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
Laura Abbottd4932f92014-10-09 15:26:44 -070049
50static int __init early_coherent_pool(char *p)
51{
52 atomic_pool_size = memparse(p, &p);
53 return 0;
54}
55early_param("coherent_pool", early_coherent_pool);
56
Suzuki K. Poulose71328132015-03-19 18:17:09 +000057static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
Laura Abbottd4932f92014-10-09 15:26:44 -070058{
59 unsigned long val;
60 void *ptr = NULL;
61
62 if (!atomic_pool) {
63 WARN(1, "coherent pool not initialised!\n");
64 return NULL;
65 }
66
67 val = gen_pool_alloc(atomic_pool, size);
68 if (val) {
69 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
70
71 *ret_page = phys_to_page(phys);
72 ptr = (void *)val;
Marek Szyprowski6829e272015-04-23 12:46:16 +010073 memset(ptr, 0, size);
Laura Abbottd4932f92014-10-09 15:26:44 -070074 }
75
76 return ptr;
77}
78
79static bool __in_atomic_pool(void *start, size_t size)
80{
81 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
82}
83
84static int __free_from_pool(void *start, size_t size)
85{
86 if (!__in_atomic_pool(start, size))
87 return 0;
88
89 gen_pool_free(atomic_pool, (unsigned long)start, size);
90
91 return 1;
92}
93
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +000094static void *__dma_alloc(struct device *dev, size_t size,
95 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070096 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +010097{
Laura Abbottd4932f92014-10-09 15:26:44 -070098 struct page *page;
Catalin Marinas73635902013-05-21 17:35:19 +010099 void *ptr, *coherent_ptr;
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000100 bool coherent = is_device_dma_coherent(dev);
Robin Murphy97942c22015-07-31 18:28:34 +0100101 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, false);
Catalin Marinas73635902013-05-21 17:35:19 +0100102
103 size = PAGE_ALIGN(size);
Laura Abbottd4932f92014-10-09 15:26:44 -0700104
Mel Gormand0164ad2015-11-06 16:28:21 -0800105 if (!coherent && !gfpflags_allow_blocking(flags)) {
Laura Abbottd4932f92014-10-09 15:26:44 -0700106 struct page *page = NULL;
Suzuki K. Poulose71328132015-03-19 18:17:09 +0000107 void *addr = __alloc_from_pool(size, &page, flags);
Laura Abbottd4932f92014-10-09 15:26:44 -0700108
109 if (addr)
110 *dma_handle = phys_to_dma(dev, page_to_phys(page));
111
112 return addr;
Laura Abbottd4932f92014-10-09 15:26:44 -0700113 }
Catalin Marinas73635902013-05-21 17:35:19 +0100114
Christoph Hellwig0d8488a2017-12-24 13:53:50 +0100115 ptr = swiotlb_alloc(dev, size, dma_handle, flags, attrs);
Catalin Marinas73635902013-05-21 17:35:19 +0100116 if (!ptr)
117 goto no_mem;
Catalin Marinas73635902013-05-21 17:35:19 +0100118
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000119 /* no need for non-cacheable mapping if coherent */
120 if (coherent)
121 return ptr;
122
Catalin Marinas73635902013-05-21 17:35:19 +0100123 /* remove any dirty cache lines on the kernel alias */
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900124 __dma_flush_area(ptr, size);
Catalin Marinas73635902013-05-21 17:35:19 +0100125
126 /* create a coherent mapping */
127 page = virt_to_page(ptr);
Laura Abbottd4932f92014-10-09 15:26:44 -0700128 coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
Matthieu CASTET359be672017-10-02 15:01:55 +0200129 prot, __builtin_return_address(0));
Catalin Marinas73635902013-05-21 17:35:19 +0100130 if (!coherent_ptr)
131 goto no_map;
132
133 return coherent_ptr;
134
135no_map:
Christoph Hellwig0d8488a2017-12-24 13:53:50 +0100136 swiotlb_free(dev, size, ptr, *dma_handle, attrs);
Catalin Marinas73635902013-05-21 17:35:19 +0100137no_mem:
Catalin Marinas73635902013-05-21 17:35:19 +0100138 return NULL;
139}
140
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000141static void __dma_free(struct device *dev, size_t size,
142 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700143 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100144{
145 void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
146
Dean Nelson2cff98b2015-04-29 16:09:18 +0100147 size = PAGE_ALIGN(size);
148
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000149 if (!is_device_dma_coherent(dev)) {
150 if (__free_from_pool(vaddr, size))
151 return;
152 vunmap(vaddr);
153 }
Christoph Hellwig0d8488a2017-12-24 13:53:50 +0100154 swiotlb_free(dev, size, swiotlb_addr, dma_handle, attrs);
Catalin Marinas73635902013-05-21 17:35:19 +0100155}
156
157static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
158 unsigned long offset, size_t size,
159 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700160 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100161{
162 dma_addr_t dev_addr;
163
164 dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
Takeshi Kihara7f332fc2017-01-11 11:11:17 +0100165 if (!is_device_dma_coherent(dev) &&
166 (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000167 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100168
169 return dev_addr;
170}
171
172
173static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
174 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700175 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100176{
Takeshi Kihara7f332fc2017-01-11 11:11:17 +0100177 if (!is_device_dma_coherent(dev) &&
178 (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000179 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100180 swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
181}
182
183static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
184 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700185 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100186{
187 struct scatterlist *sg;
188 int i, ret;
189
190 ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
Takeshi Kihara7f332fc2017-01-11 11:11:17 +0100191 if (!is_device_dma_coherent(dev) &&
192 (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000193 for_each_sg(sgl, sg, ret, i)
194 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
195 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100196
197 return ret;
198}
199
200static void __swiotlb_unmap_sg_attrs(struct device *dev,
201 struct scatterlist *sgl, int nelems,
202 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700203 unsigned long attrs)
Catalin Marinas73635902013-05-21 17:35:19 +0100204{
205 struct scatterlist *sg;
206 int i;
207
Takeshi Kihara7f332fc2017-01-11 11:11:17 +0100208 if (!is_device_dma_coherent(dev) &&
209 (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000210 for_each_sg(sgl, sg, nelems, i)
211 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
212 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100213 swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
214}
215
216static void __swiotlb_sync_single_for_cpu(struct device *dev,
217 dma_addr_t dev_addr, size_t size,
218 enum dma_data_direction dir)
219{
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000220 if (!is_device_dma_coherent(dev))
221 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100222 swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
223}
224
225static void __swiotlb_sync_single_for_device(struct device *dev,
226 dma_addr_t dev_addr, size_t size,
227 enum dma_data_direction dir)
228{
229 swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000230 if (!is_device_dma_coherent(dev))
231 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100232}
233
234static void __swiotlb_sync_sg_for_cpu(struct device *dev,
235 struct scatterlist *sgl, int nelems,
236 enum dma_data_direction dir)
237{
238 struct scatterlist *sg;
239 int i;
240
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000241 if (!is_device_dma_coherent(dev))
242 for_each_sg(sgl, sg, nelems, i)
243 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
244 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100245 swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
246}
247
248static void __swiotlb_sync_sg_for_device(struct device *dev,
249 struct scatterlist *sgl, int nelems,
250 enum dma_data_direction dir)
251{
252 struct scatterlist *sg;
253 int i;
254
255 swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000256 if (!is_device_dma_coherent(dev))
257 for_each_sg(sgl, sg, nelems, i)
258 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
259 sg->length, dir);
Catalin Marinas73635902013-05-21 17:35:19 +0100260}
261
Catalin Marinas92f66f82017-04-25 15:42:31 +0100262static int __swiotlb_mmap_pfn(struct vm_area_struct *vma,
263 unsigned long pfn, size_t size)
Laura Abbott6e8d7962014-03-14 19:52:23 +0000264{
265 int ret = -ENXIO;
Thomas Meyerb4f4a272017-09-21 00:29:36 +0200266 unsigned long nr_vma_pages = vma_pages(vma);
Laura Abbott6e8d7962014-03-14 19:52:23 +0000267 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
Laura Abbott6e8d7962014-03-14 19:52:23 +0000268 unsigned long off = vma->vm_pgoff;
269
Laura Abbott6e8d7962014-03-14 19:52:23 +0000270 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
271 ret = remap_pfn_range(vma, vma->vm_start,
272 pfn + off,
273 vma->vm_end - vma->vm_start,
274 vma->vm_page_prot);
275 }
276
277 return ret;
278}
279
Catalin Marinas92f66f82017-04-25 15:42:31 +0100280static int __swiotlb_mmap(struct device *dev,
281 struct vm_area_struct *vma,
282 void *cpu_addr, dma_addr_t dma_addr, size_t size,
283 unsigned long attrs)
284{
285 int ret;
286 unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
287
288 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
289 is_device_dma_coherent(dev));
290
Vladimir Murzin43fc5092017-07-20 11:19:58 +0100291 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Catalin Marinas92f66f82017-04-25 15:42:31 +0100292 return ret;
293
294 return __swiotlb_mmap_pfn(vma, pfn, size);
295}
296
297static int __swiotlb_get_sgtable_page(struct sg_table *sgt,
298 struct page *page, size_t size)
Robin Murphy1d1ddf62015-07-17 16:58:21 +0100299{
300 int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
301
302 if (!ret)
Catalin Marinas92f66f82017-04-25 15:42:31 +0100303 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
Robin Murphy1d1ddf62015-07-17 16:58:21 +0100304
305 return ret;
306}
307
Catalin Marinas92f66f82017-04-25 15:42:31 +0100308static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
309 void *cpu_addr, dma_addr_t handle, size_t size,
310 unsigned long attrs)
311{
312 struct page *page = phys_to_page(dma_to_phys(dev, handle));
313
314 return __swiotlb_get_sgtable_page(sgt, page, size);
315}
316
Jisheng Zhangb67a8b22016-06-08 15:53:46 +0800317static int __swiotlb_dma_supported(struct device *hwdev, u64 mask)
318{
319 if (swiotlb)
320 return swiotlb_dma_supported(hwdev, mask);
321 return 1;
322}
323
Robin Murphyadbe7e22017-01-25 18:31:31 +0000324static int __swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t addr)
325{
326 if (swiotlb)
327 return swiotlb_dma_mapping_error(hwdev, addr);
328 return 0;
329}
330
Christoph Hellwigb7da4092017-12-23 14:00:35 +0100331static const struct dma_map_ops arm64_swiotlb_dma_ops = {
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +0000332 .alloc = __dma_alloc,
333 .free = __dma_free,
334 .mmap = __swiotlb_mmap,
Robin Murphy1d1ddf62015-07-17 16:58:21 +0100335 .get_sgtable = __swiotlb_get_sgtable,
Catalin Marinas73635902013-05-21 17:35:19 +0100336 .map_page = __swiotlb_map_page,
337 .unmap_page = __swiotlb_unmap_page,
338 .map_sg = __swiotlb_map_sg_attrs,
339 .unmap_sg = __swiotlb_unmap_sg_attrs,
340 .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
341 .sync_single_for_device = __swiotlb_sync_single_for_device,
342 .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
343 .sync_sg_for_device = __swiotlb_sync_sg_for_device,
Jisheng Zhangb67a8b22016-06-08 15:53:46 +0800344 .dma_supported = __swiotlb_dma_supported,
Robin Murphyadbe7e22017-01-25 18:31:31 +0000345 .mapping_error = __swiotlb_dma_mapping_error,
Catalin Marinas73635902013-05-21 17:35:19 +0100346};
Catalin Marinas09b55412012-03-05 11:49:30 +0000347
Laura Abbottd4932f92014-10-09 15:26:44 -0700348static int __init atomic_pool_init(void)
349{
350 pgprot_t prot = __pgprot(PROT_NORMAL_NC);
351 unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
352 struct page *page;
353 void *addr;
354 unsigned int pool_size_order = get_order(atomic_pool_size);
355
356 if (dev_get_cma_area(NULL))
357 page = dma_alloc_from_contiguous(NULL, nr_pages,
Lucas Stach712c6042017-02-24 14:58:44 -0800358 pool_size_order, GFP_KERNEL);
Laura Abbottd4932f92014-10-09 15:26:44 -0700359 else
Christoph Hellwigad67f5a2017-12-24 13:52:03 +0100360 page = alloc_pages(GFP_DMA32, pool_size_order);
Laura Abbottd4932f92014-10-09 15:26:44 -0700361
362 if (page) {
363 int ret;
364 void *page_addr = page_address(page);
365
366 memset(page_addr, 0, atomic_pool_size);
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900367 __dma_flush_area(page_addr, atomic_pool_size);
Laura Abbottd4932f92014-10-09 15:26:44 -0700368
369 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
370 if (!atomic_pool)
371 goto free_page;
372
373 addr = dma_common_contiguous_remap(page, atomic_pool_size,
374 VM_USERMAP, prot, atomic_pool_init);
375
376 if (!addr)
377 goto destroy_genpool;
378
379 ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
380 page_to_phys(page),
381 atomic_pool_size, -1);
382 if (ret)
383 goto remove_mapping;
384
385 gen_pool_set_algo(atomic_pool,
386 gen_pool_first_fit_order_align,
Vladimir Murzin2fa59ec2017-08-14 09:55:46 +0100387 NULL);
Laura Abbottd4932f92014-10-09 15:26:44 -0700388
389 pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
390 atomic_pool_size / 1024);
391 return 0;
392 }
393 goto out;
394
395remove_mapping:
396 dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
397destroy_genpool:
398 gen_pool_destroy(atomic_pool);
399 atomic_pool = NULL;
400free_page:
401 if (!dma_release_from_contiguous(NULL, page, nr_pages))
402 __free_pages(page, pool_size_order);
403out:
404 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
405 atomic_pool_size / 1024);
406 return -ENOMEM;
407}
408
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500409/********************************************
410 * The following APIs are for dummy DMA ops *
411 ********************************************/
412
413static void *__dummy_alloc(struct device *dev, size_t size,
414 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700415 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500416{
417 return NULL;
418}
419
420static void __dummy_free(struct device *dev, size_t size,
421 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700422 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500423{
424}
425
426static int __dummy_mmap(struct device *dev,
427 struct vm_area_struct *vma,
428 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700429 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500430{
431 return -ENXIO;
432}
433
434static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
435 unsigned long offset, size_t size,
436 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700437 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500438{
Christoph Hellwige0d60ac2017-05-21 12:21:50 +0200439 return 0;
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500440}
441
442static void __dummy_unmap_page(struct device *dev, dma_addr_t dev_addr,
443 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700444 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500445{
446}
447
448static int __dummy_map_sg(struct device *dev, struct scatterlist *sgl,
449 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700450 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500451{
452 return 0;
453}
454
455static void __dummy_unmap_sg(struct device *dev,
456 struct scatterlist *sgl, int nelems,
457 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700458 unsigned long attrs)
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500459{
460}
461
462static void __dummy_sync_single(struct device *dev,
463 dma_addr_t dev_addr, size_t size,
464 enum dma_data_direction dir)
465{
466}
467
468static void __dummy_sync_sg(struct device *dev,
469 struct scatterlist *sgl, int nelems,
470 enum dma_data_direction dir)
471{
472}
473
474static int __dummy_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
475{
476 return 1;
477}
478
479static int __dummy_dma_supported(struct device *hwdev, u64 mask)
480{
481 return 0;
482}
483
Bart Van Assche52997092017-01-20 13:04:01 -0800484const struct dma_map_ops dummy_dma_ops = {
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -0500485 .alloc = __dummy_alloc,
486 .free = __dummy_free,
487 .mmap = __dummy_mmap,
488 .map_page = __dummy_map_page,
489 .unmap_page = __dummy_unmap_page,
490 .map_sg = __dummy_map_sg,
491 .unmap_sg = __dummy_unmap_sg,
492 .sync_single_for_cpu = __dummy_sync_single,
493 .sync_single_for_device = __dummy_sync_single,
494 .sync_sg_for_cpu = __dummy_sync_sg,
495 .sync_sg_for_device = __dummy_sync_sg,
496 .mapping_error = __dummy_mapping_error,
497 .dma_supported = __dummy_dma_supported,
498};
499EXPORT_SYMBOL(dummy_dma_ops);
500
Catalin Marinasa1e50a82015-02-05 18:01:53 +0000501static int __init arm64_dma_init(void)
Catalin Marinas09b55412012-03-05 11:49:30 +0000502{
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +0100503 if (swiotlb_force == SWIOTLB_FORCE ||
504 max_pfn > (arm64_dma_phys_limit >> PAGE_SHIFT))
Jisheng Zhangb67a8b22016-06-08 15:53:46 +0800505 swiotlb = 1;
506
Catalin Marinasebc7e212018-05-11 13:33:12 +0100507 WARN_TAINT(ARCH_DMA_MINALIGN < cache_line_size(),
508 TAINT_CPU_OUT_OF_SPEC,
509 "ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
510 ARCH_DMA_MINALIGN, cache_line_size());
511
Arnd Bergmann1dccb592015-11-16 17:25:48 +0100512 return atomic_pool_init();
Laura Abbottd4932f92014-10-09 15:26:44 -0700513}
514arch_initcall(arm64_dma_init);
Catalin Marinas09b55412012-03-05 11:49:30 +0000515
516#define PREALLOC_DMA_DEBUG_ENTRIES 4096
517
518static int __init dma_debug_do_init(void)
519{
520 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
521 return 0;
522}
523fs_initcall(dma_debug_do_init);
Robin Murphy13b86292015-10-01 20:13:59 +0100524
525
526#ifdef CONFIG_IOMMU_DMA
527#include <linux/dma-iommu.h>
528#include <linux/platform_device.h>
529#include <linux/amba/bus.h>
530
531/* Thankfully, all cache ops are by VA so we can ignore phys here */
532static void flush_page(struct device *dev, const void *virt, phys_addr_t phys)
533{
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900534 __dma_flush_area(virt, PAGE_SIZE);
Robin Murphy13b86292015-10-01 20:13:59 +0100535}
536
537static void *__iommu_alloc_attrs(struct device *dev, size_t size,
538 dma_addr_t *handle, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700539 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100540{
541 bool coherent = is_device_dma_coherent(dev);
Mitchel Humpherys737c85c2017-01-06 18:58:12 +0530542 int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000543 size_t iosize = size;
Robin Murphy13b86292015-10-01 20:13:59 +0100544 void *addr;
545
546 if (WARN(!dev, "cannot create IOMMU mapping for unknown device\n"))
547 return NULL;
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000548
549 size = PAGE_ALIGN(size);
550
Robin Murphy13b86292015-10-01 20:13:59 +0100551 /*
552 * Some drivers rely on this, and we probably don't want the
553 * possibility of stale kernel data being read by devices anyway.
554 */
555 gfp |= __GFP_ZERO;
556
Geert Uytterhoeven44176bb2017-03-07 18:43:32 +0100557 if (!gfpflags_allow_blocking(gfp)) {
Robin Murphy13b86292015-10-01 20:13:59 +0100558 struct page *page;
559 /*
560 * In atomic context we can't remap anything, so we'll only
561 * get the virtually contiguous buffer we need by way of a
562 * physically contiguous allocation.
563 */
564 if (coherent) {
565 page = alloc_pages(gfp, get_order(size));
566 addr = page ? page_address(page) : NULL;
567 } else {
568 addr = __alloc_from_pool(size, &page, gfp);
569 }
570 if (!addr)
571 return NULL;
572
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000573 *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
Robin Murphy13b86292015-10-01 20:13:59 +0100574 if (iommu_dma_mapping_error(dev, *handle)) {
575 if (coherent)
576 __free_pages(page, get_order(size));
577 else
578 __free_from_pool(addr, size);
579 addr = NULL;
580 }
Geert Uytterhoeven44176bb2017-03-07 18:43:32 +0100581 } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
582 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, coherent);
583 struct page *page;
584
585 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
586 get_order(size), gfp);
587 if (!page)
588 return NULL;
589
590 *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
591 if (iommu_dma_mapping_error(dev, *handle)) {
592 dma_release_from_contiguous(dev, page,
593 size >> PAGE_SHIFT);
594 return NULL;
595 }
596 if (!coherent)
597 __dma_flush_area(page_to_virt(page), iosize);
598
599 addr = dma_common_contiguous_remap(page, size, VM_USERMAP,
600 prot,
601 __builtin_return_address(0));
602 if (!addr) {
603 iommu_dma_unmap_page(dev, *handle, iosize, 0, attrs);
604 dma_release_from_contiguous(dev, page,
605 size >> PAGE_SHIFT);
606 }
607 } else {
608 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, coherent);
609 struct page **pages;
610
611 pages = iommu_dma_alloc(dev, iosize, gfp, attrs, ioprot,
612 handle, flush_page);
613 if (!pages)
614 return NULL;
615
616 addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
617 __builtin_return_address(0));
618 if (!addr)
619 iommu_dma_free(dev, pages, iosize, handle);
Robin Murphy13b86292015-10-01 20:13:59 +0100620 }
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 /*
Geert Uytterhoeven44176bb2017-03-07 18:43:32 +0100631 * @cpu_addr will be one of 4 things depending on how it was allocated:
632 * - A remapped array of pages for contiguous allocations.
Robin Murphy13b86292015-10-01 20:13:59 +0100633 * - A remapped array of pages from iommu_dma_alloc(), for all
634 * non-atomic allocations.
635 * - A non-cacheable alias from the atomic pool, for atomic
636 * allocations by non-coherent devices.
637 * - A normal lowmem address, for atomic allocations by
638 * coherent devices.
639 * Hence how dodgy the below logic looks...
640 */
641 if (__in_atomic_pool(cpu_addr, size)) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700642 iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
Robin Murphy13b86292015-10-01 20:13:59 +0100643 __free_from_pool(cpu_addr, size);
Geert Uytterhoeven44176bb2017-03-07 18:43:32 +0100644 } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
645 struct page *page = vmalloc_to_page(cpu_addr);
646
647 iommu_dma_unmap_page(dev, handle, iosize, 0, attrs);
648 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
649 dma_common_free_remap(cpu_addr, size, VM_USERMAP);
Robin Murphy13b86292015-10-01 20:13:59 +0100650 } else if (is_vmalloc_addr(cpu_addr)){
651 struct vm_struct *area = find_vm_area(cpu_addr);
652
653 if (WARN_ON(!area || !area->pages))
654 return;
Robin Murphybd1c6ff2015-11-04 13:23:52 +0000655 iommu_dma_free(dev, area->pages, iosize, &handle);
Robin Murphy13b86292015-10-01 20:13:59 +0100656 dma_common_free_remap(cpu_addr, size, VM_USERMAP);
657 } else {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700658 iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
Robin Murphy13b86292015-10-01 20:13:59 +0100659 __free_pages(virt_to_page(cpu_addr), get_order(size));
660 }
661}
662
663static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
664 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700665 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100666{
667 struct vm_struct *area;
668 int ret;
669
670 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
671 is_device_dma_coherent(dev));
672
Vladimir Murzin43fc5092017-07-20 11:19:58 +0100673 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Robin Murphy13b86292015-10-01 20:13:59 +0100674 return ret;
675
Catalin Marinas92f66f82017-04-25 15:42:31 +0100676 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
677 /*
678 * DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped,
679 * hence in the vmalloc space.
680 */
681 unsigned long pfn = vmalloc_to_pfn(cpu_addr);
682 return __swiotlb_mmap_pfn(vma, pfn, size);
683 }
684
Robin Murphy13b86292015-10-01 20:13:59 +0100685 area = find_vm_area(cpu_addr);
686 if (WARN_ON(!area || !area->pages))
687 return -ENXIO;
688
689 return iommu_dma_mmap(area->pages, size, vma);
690}
691
692static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
693 void *cpu_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700694 size_t size, unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100695{
696 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
697 struct vm_struct *area = find_vm_area(cpu_addr);
698
Catalin Marinas92f66f82017-04-25 15:42:31 +0100699 if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
700 /*
701 * DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped,
702 * hence in the vmalloc space.
703 */
704 struct page *page = vmalloc_to_page(cpu_addr);
705 return __swiotlb_get_sgtable_page(sgt, page, size);
706 }
707
Robin Murphy13b86292015-10-01 20:13:59 +0100708 if (WARN_ON(!area || !area->pages))
709 return -ENXIO;
710
711 return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,
712 GFP_KERNEL);
713}
714
715static void __iommu_sync_single_for_cpu(struct device *dev,
716 dma_addr_t dev_addr, size_t size,
717 enum dma_data_direction dir)
718{
719 phys_addr_t phys;
720
721 if (is_device_dma_coherent(dev))
722 return;
723
724 phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
725 __dma_unmap_area(phys_to_virt(phys), size, dir);
726}
727
728static void __iommu_sync_single_for_device(struct device *dev,
729 dma_addr_t dev_addr, size_t size,
730 enum dma_data_direction dir)
731{
732 phys_addr_t phys;
733
734 if (is_device_dma_coherent(dev))
735 return;
736
737 phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
738 __dma_map_area(phys_to_virt(phys), size, dir);
739}
740
741static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
742 unsigned long offset, size_t size,
743 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700744 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100745{
746 bool coherent = is_device_dma_coherent(dev);
Mitchel Humpherys737c85c2017-01-06 18:58:12 +0530747 int prot = dma_info_to_prot(dir, coherent, attrs);
Robin Murphy13b86292015-10-01 20:13:59 +0100748 dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
749
750 if (!iommu_dma_mapping_error(dev, dev_addr) &&
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700751 (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100752 __iommu_sync_single_for_device(dev, dev_addr, size, dir);
753
754 return dev_addr;
755}
756
757static void __iommu_unmap_page(struct device *dev, dma_addr_t dev_addr,
758 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700759 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100760{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700761 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100762 __iommu_sync_single_for_cpu(dev, dev_addr, size, dir);
763
764 iommu_dma_unmap_page(dev, dev_addr, size, dir, attrs);
765}
766
767static void __iommu_sync_sg_for_cpu(struct device *dev,
768 struct scatterlist *sgl, int nelems,
769 enum dma_data_direction dir)
770{
771 struct scatterlist *sg;
772 int i;
773
774 if (is_device_dma_coherent(dev))
775 return;
776
777 for_each_sg(sgl, sg, nelems, i)
778 __dma_unmap_area(sg_virt(sg), sg->length, dir);
779}
780
781static void __iommu_sync_sg_for_device(struct device *dev,
782 struct scatterlist *sgl, int nelems,
783 enum dma_data_direction dir)
784{
785 struct scatterlist *sg;
786 int i;
787
788 if (is_device_dma_coherent(dev))
789 return;
790
791 for_each_sg(sgl, sg, nelems, i)
792 __dma_map_area(sg_virt(sg), sg->length, dir);
793}
794
795static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
796 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700797 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100798{
799 bool coherent = is_device_dma_coherent(dev);
800
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700801 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100802 __iommu_sync_sg_for_device(dev, sgl, nelems, dir);
803
804 return iommu_dma_map_sg(dev, sgl, nelems,
Mitchel Humpherys737c85c2017-01-06 18:58:12 +0530805 dma_info_to_prot(dir, coherent, attrs));
Robin Murphy13b86292015-10-01 20:13:59 +0100806}
807
808static void __iommu_unmap_sg_attrs(struct device *dev,
809 struct scatterlist *sgl, int nelems,
810 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700811 unsigned long attrs)
Robin Murphy13b86292015-10-01 20:13:59 +0100812{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700813 if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
Robin Murphy13b86292015-10-01 20:13:59 +0100814 __iommu_sync_sg_for_cpu(dev, sgl, nelems, dir);
815
816 iommu_dma_unmap_sg(dev, sgl, nelems, dir, attrs);
817}
818
Bart Van Assche52997092017-01-20 13:04:01 -0800819static const struct dma_map_ops iommu_dma_ops = {
Robin Murphy13b86292015-10-01 20:13:59 +0100820 .alloc = __iommu_alloc_attrs,
821 .free = __iommu_free_attrs,
822 .mmap = __iommu_mmap_attrs,
823 .get_sgtable = __iommu_get_sgtable,
824 .map_page = __iommu_map_page,
825 .unmap_page = __iommu_unmap_page,
826 .map_sg = __iommu_map_sg_attrs,
827 .unmap_sg = __iommu_unmap_sg_attrs,
828 .sync_single_for_cpu = __iommu_sync_single_for_cpu,
829 .sync_single_for_device = __iommu_sync_single_for_device,
830 .sync_sg_for_cpu = __iommu_sync_sg_for_cpu,
831 .sync_sg_for_device = __iommu_sync_sg_for_device,
Robin Murphy60c4e802016-11-14 12:16:27 +0000832 .map_resource = iommu_dma_map_resource,
833 .unmap_resource = iommu_dma_unmap_resource,
Robin Murphy13b86292015-10-01 20:13:59 +0100834 .mapping_error = iommu_dma_mapping_error,
835};
836
Sricharan Rb913efe2017-04-10 16:51:04 +0530837static int __init __iommu_dma_init(void)
Robin Murphy13b86292015-10-01 20:13:59 +0100838{
Sricharan Rb913efe2017-04-10 16:51:04 +0530839 return iommu_dma_init();
840}
841arch_initcall(__iommu_dma_init);
842
843static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
844 const struct iommu_ops *ops)
845{
846 struct iommu_domain *domain;
847
848 if (!ops)
849 return;
Robin Murphy13b86292015-10-01 20:13:59 +0100850
851 /*
Sricharan Rb913efe2017-04-10 16:51:04 +0530852 * The IOMMU core code allocates the default DMA domain, which the
853 * underlying IOMMU driver needs to support via the dma-iommu layer.
Robin Murphy13b86292015-10-01 20:13:59 +0100854 */
Sricharan Rb913efe2017-04-10 16:51:04 +0530855 domain = iommu_get_domain_for_dev(dev);
856
Will Deacon4a8d8a12017-01-06 10:49:12 +0000857 if (!domain)
858 goto out_err;
859
860 if (domain->type == IOMMU_DOMAIN_DMA) {
861 if (iommu_dma_init_domain(domain, dma_base, size, dev))
862 goto out_err;
863
Linus Torvaldsac1820f2017-02-25 13:45:43 -0800864 dev->dma_ops = &iommu_dma_ops;
Robin Murphy13b86292015-10-01 20:13:59 +0100865 }
866
Sricharan Rb913efe2017-04-10 16:51:04 +0530867 return;
868
Will Deacon4a8d8a12017-01-06 10:49:12 +0000869out_err:
Sricharan Rb913efe2017-04-10 16:51:04 +0530870 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
Will Deacon4a8d8a12017-01-06 10:49:12 +0000871 dev_name(dev));
Robin Murphy13b86292015-10-01 20:13:59 +0100872}
873
Robin Murphy876945d2015-10-01 20:14:00 +0100874void arch_teardown_dma_ops(struct device *dev)
875{
Bart Van Assche56579332017-01-20 13:04:02 -0800876 dev->dma_ops = NULL;
Robin Murphy876945d2015-10-01 20:14:00 +0100877}
878
Robin Murphy13b86292015-10-01 20:13:59 +0100879#else
880
881static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +0100882 const struct iommu_ops *iommu)
Robin Murphy13b86292015-10-01 20:13:59 +0100883{ }
884
885#endif /* CONFIG_IOMMU_DMA */
886
Robin Murphy876945d2015-10-01 20:14:00 +0100887void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
Robin Murphy53c92d72016-04-07 18:42:05 +0100888 const struct iommu_ops *iommu, bool coherent)
Robin Murphy876945d2015-10-01 20:14:00 +0100889{
Bart Van Assche56579332017-01-20 13:04:02 -0800890 if (!dev->dma_ops)
Christoph Hellwigb7da4092017-12-23 14:00:35 +0100891 dev->dma_ops = &arm64_swiotlb_dma_ops;
Robin Murphy876945d2015-10-01 20:14:00 +0100892
893 dev->archdata.dma_coherent = coherent;
894 __iommu_setup_dma_ops(dev, dma_base, size, iommu);
Stefano Stabellinie0586322017-04-13 14:04:21 -0700895
896#ifdef CONFIG_XEN
897 if (xen_initial_domain()) {
898 dev->archdata.dev_dma_ops = dev->dma_ops;
899 dev->dma_ops = xen_dma_ops;
900 }
901#endif
Robin Murphy876945d2015-10-01 20:14:00 +0100902}