2 * SWIOTLB-based DMA API implementation
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
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.
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.
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/>.
20 #include <linux/gfp.h>
21 #include <linux/export.h>
22 #include <linux/slab.h>
23 #include <linux/genalloc.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/dma-contiguous.h>
26 #include <linux/vmalloc.h>
27 #include <linux/swiotlb.h>
29 #include <asm/cacheflush.h>
31 struct dma_map_ops *dma_ops;
32 EXPORT_SYMBOL(dma_ops);
34 static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
37 if (!coherent || dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs))
38 return pgprot_writecombine(prot);
42 static struct gen_pool *atomic_pool;
44 #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
45 static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
47 static int __init early_coherent_pool(char *p)
49 atomic_pool_size = memparse(p, &p);
52 early_param("coherent_pool", early_coherent_pool);
54 static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
60 WARN(1, "coherent pool not initialised!\n");
64 val = gen_pool_alloc(atomic_pool, size);
66 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
68 *ret_page = phys_to_page(phys);
76 static bool __in_atomic_pool(void *start, size_t size)
78 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
81 static int __free_from_pool(void *start, size_t size)
83 if (!__in_atomic_pool(start, size))
86 gen_pool_free(atomic_pool, (unsigned long)start, size);
91 static void *__dma_alloc_coherent(struct device *dev, size_t size,
92 dma_addr_t *dma_handle, gfp_t flags,
93 struct dma_attrs *attrs)
96 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
100 if (IS_ENABLED(CONFIG_ZONE_DMA) &&
101 dev->coherent_dma_mask <= DMA_BIT_MASK(32))
103 if (IS_ENABLED(CONFIG_DMA_CMA) && (flags & __GFP_WAIT)) {
107 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
112 *dma_handle = phys_to_dma(dev, page_to_phys(page));
113 addr = page_address(page);
114 memset(addr, 0, size);
117 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
121 static void __dma_free_coherent(struct device *dev, size_t size,
122 void *vaddr, dma_addr_t dma_handle,
123 struct dma_attrs *attrs)
126 phys_addr_t paddr = dma_to_phys(dev, dma_handle);
129 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
133 freed = dma_release_from_contiguous(dev,
137 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
140 static void *__dma_alloc(struct device *dev, size_t size,
141 dma_addr_t *dma_handle, gfp_t flags,
142 struct dma_attrs *attrs)
145 void *ptr, *coherent_ptr;
146 bool coherent = is_device_dma_coherent(dev);
148 size = PAGE_ALIGN(size);
150 if (!coherent && !(flags & __GFP_WAIT)) {
151 struct page *page = NULL;
152 void *addr = __alloc_from_pool(size, &page, flags);
155 *dma_handle = phys_to_dma(dev, page_to_phys(page));
160 ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
164 /* no need for non-cacheable mapping if coherent */
168 /* remove any dirty cache lines on the kernel alias */
169 __dma_flush_range(ptr, ptr + size);
171 /* create a coherent mapping */
172 page = virt_to_page(ptr);
173 coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
174 __get_dma_pgprot(attrs,
175 __pgprot(PROT_NORMAL_NC), false),
183 __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
185 *dma_handle = DMA_ERROR_CODE;
189 static void __dma_free(struct device *dev, size_t size,
190 void *vaddr, dma_addr_t dma_handle,
191 struct dma_attrs *attrs)
193 void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
195 size = PAGE_ALIGN(size);
197 if (!is_device_dma_coherent(dev)) {
198 if (__free_from_pool(vaddr, size))
202 __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
205 static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
206 unsigned long offset, size_t size,
207 enum dma_data_direction dir,
208 struct dma_attrs *attrs)
212 dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
213 if (!is_device_dma_coherent(dev))
214 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
220 static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
221 size_t size, enum dma_data_direction dir,
222 struct dma_attrs *attrs)
224 if (!is_device_dma_coherent(dev))
225 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
226 swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
229 static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
230 int nelems, enum dma_data_direction dir,
231 struct dma_attrs *attrs)
233 struct scatterlist *sg;
236 ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
237 if (!is_device_dma_coherent(dev))
238 for_each_sg(sgl, sg, ret, i)
239 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
245 static void __swiotlb_unmap_sg_attrs(struct device *dev,
246 struct scatterlist *sgl, int nelems,
247 enum dma_data_direction dir,
248 struct dma_attrs *attrs)
250 struct scatterlist *sg;
253 if (!is_device_dma_coherent(dev))
254 for_each_sg(sgl, sg, nelems, i)
255 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
257 swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
260 static void __swiotlb_sync_single_for_cpu(struct device *dev,
261 dma_addr_t dev_addr, size_t size,
262 enum dma_data_direction dir)
264 if (!is_device_dma_coherent(dev))
265 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
266 swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
269 static void __swiotlb_sync_single_for_device(struct device *dev,
270 dma_addr_t dev_addr, size_t size,
271 enum dma_data_direction dir)
273 swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
274 if (!is_device_dma_coherent(dev))
275 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
278 static void __swiotlb_sync_sg_for_cpu(struct device *dev,
279 struct scatterlist *sgl, int nelems,
280 enum dma_data_direction dir)
282 struct scatterlist *sg;
285 if (!is_device_dma_coherent(dev))
286 for_each_sg(sgl, sg, nelems, i)
287 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
289 swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
292 static void __swiotlb_sync_sg_for_device(struct device *dev,
293 struct scatterlist *sgl, int nelems,
294 enum dma_data_direction dir)
296 struct scatterlist *sg;
299 swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
300 if (!is_device_dma_coherent(dev))
301 for_each_sg(sgl, sg, nelems, i)
302 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
306 static int __swiotlb_mmap(struct device *dev,
307 struct vm_area_struct *vma,
308 void *cpu_addr, dma_addr_t dma_addr, size_t size,
309 struct dma_attrs *attrs)
312 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
314 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
315 unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
316 unsigned long off = vma->vm_pgoff;
318 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
319 is_device_dma_coherent(dev));
321 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
324 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
325 ret = remap_pfn_range(vma, vma->vm_start,
327 vma->vm_end - vma->vm_start,
334 static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
335 void *cpu_addr, dma_addr_t handle, size_t size,
336 struct dma_attrs *attrs)
338 int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
341 sg_set_page(sgt->sgl, phys_to_page(dma_to_phys(dev, handle)),
342 PAGE_ALIGN(size), 0);
347 static struct dma_map_ops swiotlb_dma_ops = {
348 .alloc = __dma_alloc,
350 .mmap = __swiotlb_mmap,
351 .get_sgtable = __swiotlb_get_sgtable,
352 .map_page = __swiotlb_map_page,
353 .unmap_page = __swiotlb_unmap_page,
354 .map_sg = __swiotlb_map_sg_attrs,
355 .unmap_sg = __swiotlb_unmap_sg_attrs,
356 .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
357 .sync_single_for_device = __swiotlb_sync_single_for_device,
358 .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
359 .sync_sg_for_device = __swiotlb_sync_sg_for_device,
360 .dma_supported = swiotlb_dma_supported,
361 .mapping_error = swiotlb_dma_mapping_error,
364 static int __init atomic_pool_init(void)
366 pgprot_t prot = __pgprot(PROT_NORMAL_NC);
367 unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
370 unsigned int pool_size_order = get_order(atomic_pool_size);
372 if (dev_get_cma_area(NULL))
373 page = dma_alloc_from_contiguous(NULL, nr_pages,
376 page = alloc_pages(GFP_DMA, pool_size_order);
380 void *page_addr = page_address(page);
382 memset(page_addr, 0, atomic_pool_size);
383 __dma_flush_range(page_addr, page_addr + atomic_pool_size);
385 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
389 addr = dma_common_contiguous_remap(page, atomic_pool_size,
390 VM_USERMAP, prot, atomic_pool_init);
393 goto destroy_genpool;
395 ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
397 atomic_pool_size, -1);
401 gen_pool_set_algo(atomic_pool,
402 gen_pool_first_fit_order_align,
405 pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
406 atomic_pool_size / 1024);
412 dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
414 gen_pool_destroy(atomic_pool);
417 if (!dma_release_from_contiguous(NULL, page, nr_pages))
418 __free_pages(page, pool_size_order);
420 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
421 atomic_pool_size / 1024);
425 /********************************************
426 * The following APIs are for dummy DMA ops *
427 ********************************************/
429 static void *__dummy_alloc(struct device *dev, size_t size,
430 dma_addr_t *dma_handle, gfp_t flags,
431 struct dma_attrs *attrs)
436 static void __dummy_free(struct device *dev, size_t size,
437 void *vaddr, dma_addr_t dma_handle,
438 struct dma_attrs *attrs)
442 static int __dummy_mmap(struct device *dev,
443 struct vm_area_struct *vma,
444 void *cpu_addr, dma_addr_t dma_addr, size_t size,
445 struct dma_attrs *attrs)
450 static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
451 unsigned long offset, size_t size,
452 enum dma_data_direction dir,
453 struct dma_attrs *attrs)
455 return DMA_ERROR_CODE;
458 static void __dummy_unmap_page(struct device *dev, dma_addr_t dev_addr,
459 size_t size, enum dma_data_direction dir,
460 struct dma_attrs *attrs)
464 static int __dummy_map_sg(struct device *dev, struct scatterlist *sgl,
465 int nelems, enum dma_data_direction dir,
466 struct dma_attrs *attrs)
471 static void __dummy_unmap_sg(struct device *dev,
472 struct scatterlist *sgl, int nelems,
473 enum dma_data_direction dir,
474 struct dma_attrs *attrs)
478 static void __dummy_sync_single(struct device *dev,
479 dma_addr_t dev_addr, size_t size,
480 enum dma_data_direction dir)
484 static void __dummy_sync_sg(struct device *dev,
485 struct scatterlist *sgl, int nelems,
486 enum dma_data_direction dir)
490 static int __dummy_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
495 static int __dummy_dma_supported(struct device *hwdev, u64 mask)
500 struct dma_map_ops dummy_dma_ops = {
501 .alloc = __dummy_alloc,
502 .free = __dummy_free,
503 .mmap = __dummy_mmap,
504 .map_page = __dummy_map_page,
505 .unmap_page = __dummy_unmap_page,
506 .map_sg = __dummy_map_sg,
507 .unmap_sg = __dummy_unmap_sg,
508 .sync_single_for_cpu = __dummy_sync_single,
509 .sync_single_for_device = __dummy_sync_single,
510 .sync_sg_for_cpu = __dummy_sync_sg,
511 .sync_sg_for_device = __dummy_sync_sg,
512 .mapping_error = __dummy_mapping_error,
513 .dma_supported = __dummy_dma_supported,
515 EXPORT_SYMBOL(dummy_dma_ops);
517 static int __init arm64_dma_init(void)
521 dma_ops = &swiotlb_dma_ops;
523 ret = atomic_pool_init();
527 arch_initcall(arm64_dma_init);
529 #define PREALLOC_DMA_DEBUG_ENTRIES 4096
531 static int __init dma_debug_do_init(void)
533 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
536 fs_initcall(dma_debug_do_init);