Lines Matching +full:dma +full:- +full:coherent

1 // SPDX-License-Identifier: GPL-2.0
3 * Coherent per-device memory handling.
10 #include <linux/dma-direct.h>
11 #include <linux/dma-map-ops.h>
25 if (dev && dev->dma_mem) in dev_get_coherent_memory()
26 return dev->dma_mem; in dev_get_coherent_memory()
33 if (mem->use_dev_dma_pfn_offset) in dma_get_device_base()
34 return phys_to_dma(dev, PFN_PHYS(mem->pfn_base)); in dma_get_device_base()
35 return mem->device_base; in dma_get_device_base()
47 return ERR_PTR(-EINVAL); in dma_init_coherent_memory()
51 return ERR_PTR(-EINVAL); in dma_init_coherent_memory()
56 dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); in dma_init_coherent_memory()
57 if (!dma_mem->bitmap) in dma_init_coherent_memory()
60 dma_mem->virt_base = mem_base; in dma_init_coherent_memory()
61 dma_mem->device_base = device_addr; in dma_init_coherent_memory()
62 dma_mem->pfn_base = PFN_DOWN(phys_addr); in dma_init_coherent_memory()
63 dma_mem->size = pages; in dma_init_coherent_memory()
64 dma_mem->use_dev_dma_pfn_offset = use_dma_pfn_offset; in dma_init_coherent_memory()
65 spin_lock_init(&dma_mem->spinlock); in dma_init_coherent_memory()
73 pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %zd MiB\n", in dma_init_coherent_memory()
75 return ERR_PTR(-ENOMEM); in dma_init_coherent_memory()
83 memunmap(mem->virt_base); in dma_release_coherent_memory()
84 kfree(mem->bitmap); in dma_release_coherent_memory()
92 return -ENODEV; in dma_assign_coherent_memory()
94 if (dev->dma_mem) in dma_assign_coherent_memory()
95 return -EBUSY; in dma_assign_coherent_memory()
97 dev->dma_mem = mem; in dma_assign_coherent_memory()
103 * is asked for coherent memory for this device. This shall only be used
109 * device_addr is the DMA address the device needs to be programmed with to
143 spin_lock_irqsave(&mem->spinlock, flags); in __dma_alloc_from_coherent()
145 if (unlikely(size > ((dma_addr_t)mem->size << PAGE_SHIFT))) in __dma_alloc_from_coherent()
148 pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); in __dma_alloc_from_coherent()
153 * Memory was found in the coherent area. in __dma_alloc_from_coherent()
157 ret = mem->virt_base + ((dma_addr_t)pageno << PAGE_SHIFT); in __dma_alloc_from_coherent()
158 spin_unlock_irqrestore(&mem->spinlock, flags); in __dma_alloc_from_coherent()
162 spin_unlock_irqrestore(&mem->spinlock, flags); in __dma_alloc_from_coherent()
167 * dma_alloc_from_dev_coherent() - allocate memory from device coherent pool
170 * @dma_handle: This will be filled with the correct dma handle
174 * This function should be only called from per-arch dma_alloc_coherent()
175 * to support allocation from per-device coherent memory pools.
195 if (mem && vaddr >= mem->virt_base && vaddr < in __dma_release_from_coherent()
196 (mem->virt_base + ((dma_addr_t)mem->size << PAGE_SHIFT))) { in __dma_release_from_coherent()
197 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT; in __dma_release_from_coherent()
200 spin_lock_irqsave(&mem->spinlock, flags); in __dma_release_from_coherent()
201 bitmap_release_region(mem->bitmap, page, order); in __dma_release_from_coherent()
202 spin_unlock_irqrestore(&mem->spinlock, flags); in __dma_release_from_coherent()
209 * dma_release_from_dev_coherent() - free memory to device coherent memory pool
214 * This checks whether the memory was allocated from the per-device
215 * coherent memory pool and if so, releases that memory.
230 if (mem && vaddr >= mem->virt_base && vaddr + size <= in __dma_mmap_from_coherent()
231 (mem->virt_base + ((dma_addr_t)mem->size << PAGE_SHIFT))) { in __dma_mmap_from_coherent()
232 unsigned long off = vma->vm_pgoff; in __dma_mmap_from_coherent()
233 int start = (vaddr - mem->virt_base) >> PAGE_SHIFT; in __dma_mmap_from_coherent()
237 *ret = -ENXIO; in __dma_mmap_from_coherent()
238 if (off < count && user_count <= count - off) { in __dma_mmap_from_coherent()
239 unsigned long pfn = mem->pfn_base + start + off; in __dma_mmap_from_coherent()
240 *ret = remap_pfn_range(vma, vma->vm_start, pfn, in __dma_mmap_from_coherent()
242 vma->vm_page_prot); in __dma_mmap_from_coherent()
250 * dma_mmap_from_dev_coherent() - mmap memory from the device coherent pool
257 * This checks whether the memory was allocated from the per-device
258 * coherent memory pool and if so, maps that memory to the provided vma.
260 * Returns 1 if @vaddr belongs to the device coherent pool and the caller
312 pr_info("DMA: default coherent area is set\n"); in dma_init_global_coherent()
331 if (!rmem->priv) { in rmem_dma_device_init()
334 mem = dma_init_coherent_memory(rmem->base, rmem->base, in rmem_dma_device_init()
335 rmem->size, true); in rmem_dma_device_init()
338 rmem->priv = mem; in rmem_dma_device_init()
340 dma_assign_coherent_memory(dev, rmem->priv); in rmem_dma_device_init()
348 dev->dma_mem = NULL; in rmem_dma_device_release()
358 unsigned long node = rmem->fdt_node; in rmem_dma_setup()
361 return -EINVAL; in rmem_dma_setup()
364 if (!of_get_flat_dt_prop(node, "no-map", NULL)) { in rmem_dma_setup()
365 pr_err("Reserved memory: regions without no-map are not yet supported\n"); in rmem_dma_setup()
366 return -EINVAL; in rmem_dma_setup()
371 if (of_get_flat_dt_prop(node, "linux,dma-default", NULL)) { in rmem_dma_setup()
373 "Reserved memory: region for default DMA coherent area is redefined\n"); in rmem_dma_setup()
378 rmem->ops = &rmem_dma_ops; in rmem_dma_setup()
379 pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n", in rmem_dma_setup()
380 &rmem->base, (unsigned long)rmem->size / SZ_1M); in rmem_dma_setup()
388 return -ENOMEM; in dma_init_reserved_memory()
389 return dma_init_global_coherent(dma_reserved_default_memory->base, in dma_init_reserved_memory()
390 dma_reserved_default_memory->size); in dma_init_reserved_memory()
395 RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);