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)
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);
75 static bool __in_atomic_pool(void *start, size_t size)
77 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
80 static int __free_from_pool(void *start, size_t size)
82 if (!__in_atomic_pool(start, size))
85 gen_pool_free(atomic_pool, (unsigned long)start, size);
90 static void *__dma_alloc_coherent(struct device *dev, size_t size,
91 dma_addr_t *dma_handle, gfp_t flags,
92 struct dma_attrs *attrs)
95 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
99 if (IS_ENABLED(CONFIG_ZONE_DMA) &&
100 dev->coherent_dma_mask <= DMA_BIT_MASK(32))
102 if (IS_ENABLED(CONFIG_DMA_CMA) && (flags & __GFP_WAIT)) {
105 size = PAGE_ALIGN(size);
106 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
111 *dma_handle = phys_to_dma(dev, page_to_phys(page));
112 return page_address(page);
114 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
118 static void __dma_free_coherent(struct device *dev, size_t size,
119 void *vaddr, dma_addr_t dma_handle,
120 struct dma_attrs *attrs)
123 phys_addr_t paddr = dma_to_phys(dev, dma_handle);
126 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
130 freed = dma_release_from_contiguous(dev,
134 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
137 static void *__dma_alloc_noncoherent(struct device *dev, size_t size,
138 dma_addr_t *dma_handle, gfp_t flags,
139 struct dma_attrs *attrs)
142 void *ptr, *coherent_ptr;
144 size = PAGE_ALIGN(size);
146 if (!(flags & __GFP_WAIT)) {
147 struct page *page = NULL;
148 void *addr = __alloc_from_pool(size, &page);
151 *dma_handle = phys_to_dma(dev, page_to_phys(page));
157 ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
161 /* remove any dirty cache lines on the kernel alias */
162 __dma_flush_range(ptr, ptr + size);
164 /* create a coherent mapping */
165 page = virt_to_page(ptr);
166 coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
167 __get_dma_pgprot(attrs,
168 __pgprot(PROT_NORMAL_NC), false),
176 __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
178 *dma_handle = DMA_ERROR_CODE;
182 static void __dma_free_noncoherent(struct device *dev, size_t size,
183 void *vaddr, dma_addr_t dma_handle,
184 struct dma_attrs *attrs)
186 void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
188 if (__free_from_pool(vaddr, size))
191 __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
194 static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
195 unsigned long offset, size_t size,
196 enum dma_data_direction dir,
197 struct dma_attrs *attrs)
201 dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
202 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
208 static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
209 size_t size, enum dma_data_direction dir,
210 struct dma_attrs *attrs)
212 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
213 swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
216 static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
217 int nelems, enum dma_data_direction dir,
218 struct dma_attrs *attrs)
220 struct scatterlist *sg;
223 ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
224 for_each_sg(sgl, sg, ret, i)
225 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
231 static void __swiotlb_unmap_sg_attrs(struct device *dev,
232 struct scatterlist *sgl, int nelems,
233 enum dma_data_direction dir,
234 struct dma_attrs *attrs)
236 struct scatterlist *sg;
239 for_each_sg(sgl, sg, nelems, i)
240 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
242 swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
245 static void __swiotlb_sync_single_for_cpu(struct device *dev,
246 dma_addr_t dev_addr, size_t size,
247 enum dma_data_direction dir)
249 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
250 swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
253 static void __swiotlb_sync_single_for_device(struct device *dev,
254 dma_addr_t dev_addr, size_t size,
255 enum dma_data_direction dir)
257 swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
258 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
261 static void __swiotlb_sync_sg_for_cpu(struct device *dev,
262 struct scatterlist *sgl, int nelems,
263 enum dma_data_direction dir)
265 struct scatterlist *sg;
268 for_each_sg(sgl, sg, nelems, i)
269 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
271 swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
274 static void __swiotlb_sync_sg_for_device(struct device *dev,
275 struct scatterlist *sgl, int nelems,
276 enum dma_data_direction dir)
278 struct scatterlist *sg;
281 swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
282 for_each_sg(sgl, sg, nelems, i)
283 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
287 /* vma->vm_page_prot must be set appropriately before calling this function */
288 static int __dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
289 void *cpu_addr, dma_addr_t dma_addr, size_t size)
292 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
294 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
295 unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
296 unsigned long off = vma->vm_pgoff;
298 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
301 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
302 ret = remap_pfn_range(vma, vma->vm_start,
304 vma->vm_end - vma->vm_start,
311 static int __swiotlb_mmap_noncoherent(struct device *dev,
312 struct vm_area_struct *vma,
313 void *cpu_addr, dma_addr_t dma_addr, size_t size,
314 struct dma_attrs *attrs)
316 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot, false);
317 return __dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
320 static int __swiotlb_mmap_coherent(struct device *dev,
321 struct vm_area_struct *vma,
322 void *cpu_addr, dma_addr_t dma_addr, size_t size,
323 struct dma_attrs *attrs)
325 /* Just use whatever page_prot attributes were specified */
326 return __dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
329 struct dma_map_ops noncoherent_swiotlb_dma_ops = {
330 .alloc = __dma_alloc_noncoherent,
331 .free = __dma_free_noncoherent,
332 .mmap = __swiotlb_mmap_noncoherent,
333 .map_page = __swiotlb_map_page,
334 .unmap_page = __swiotlb_unmap_page,
335 .map_sg = __swiotlb_map_sg_attrs,
336 .unmap_sg = __swiotlb_unmap_sg_attrs,
337 .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
338 .sync_single_for_device = __swiotlb_sync_single_for_device,
339 .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
340 .sync_sg_for_device = __swiotlb_sync_sg_for_device,
341 .dma_supported = swiotlb_dma_supported,
342 .mapping_error = swiotlb_dma_mapping_error,
344 EXPORT_SYMBOL(noncoherent_swiotlb_dma_ops);
346 struct dma_map_ops coherent_swiotlb_dma_ops = {
347 .alloc = __dma_alloc_coherent,
348 .free = __dma_free_coherent,
349 .mmap = __swiotlb_mmap_coherent,
350 .map_page = swiotlb_map_page,
351 .unmap_page = swiotlb_unmap_page,
352 .map_sg = swiotlb_map_sg_attrs,
353 .unmap_sg = swiotlb_unmap_sg_attrs,
354 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
355 .sync_single_for_device = swiotlb_sync_single_for_device,
356 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
357 .sync_sg_for_device = swiotlb_sync_sg_for_device,
358 .dma_supported = swiotlb_dma_supported,
359 .mapping_error = swiotlb_dma_mapping_error,
361 EXPORT_SYMBOL(coherent_swiotlb_dma_ops);
363 extern int swiotlb_late_init_with_default_size(size_t default_size);
365 static int __init atomic_pool_init(void)
367 pgprot_t prot = __pgprot(PROT_NORMAL_NC);
368 unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
371 unsigned int pool_size_order = get_order(atomic_pool_size);
373 if (dev_get_cma_area(NULL))
374 page = dma_alloc_from_contiguous(NULL, nr_pages,
377 page = alloc_pages(GFP_DMA, pool_size_order);
381 void *page_addr = page_address(page);
383 memset(page_addr, 0, atomic_pool_size);
384 __dma_flush_range(page_addr, page_addr + atomic_pool_size);
386 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
390 addr = dma_common_contiguous_remap(page, atomic_pool_size,
391 VM_USERMAP, prot, atomic_pool_init);
394 goto destroy_genpool;
396 ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
398 atomic_pool_size, -1);
402 gen_pool_set_algo(atomic_pool,
403 gen_pool_first_fit_order_align,
406 pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
407 atomic_pool_size / 1024);
413 dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
415 gen_pool_destroy(atomic_pool);
418 if (!dma_release_from_contiguous(NULL, page, nr_pages))
419 __free_pages(page, pool_size_order);
421 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
422 atomic_pool_size / 1024);
426 static int __init swiotlb_late_init(void)
428 size_t swiotlb_size = min(SZ_64M, MAX_ORDER_NR_PAGES << PAGE_SHIFT);
430 dma_ops = &noncoherent_swiotlb_dma_ops;
432 return swiotlb_late_init_with_default_size(swiotlb_size);
435 static int __init arm64_dma_init(void)
439 ret |= swiotlb_late_init();
440 ret |= atomic_pool_init();
444 arch_initcall(arm64_dma_init);
446 #define PREALLOC_DMA_DEBUG_ENTRIES 4096
448 static int __init dma_debug_do_init(void)
450 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
453 fs_initcall(dma_debug_do_init);