Lines Matching +full:per +full:- +full:device
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>
23 static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *dev) in dev_get_coherent_memory()
25 if (dev && dev->dma_mem) in dev_get_coherent_memory()
26 return dev->dma_mem; in dev_get_coherent_memory()
30 static inline dma_addr_t dma_get_device_base(struct device *dev, in dma_get_device_base()
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()
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()
88 static int dma_assign_coherent_memory(struct device *dev, in dma_assign_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
104 * from platform code, usually based on the device tree description.
109 * device_addr is the DMA address the device needs to be programmed with to
116 * be declared per device.
118 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, in dma_declare_coherent_memory()
134 static void *__dma_alloc_from_coherent(struct device *dev, in __dma_alloc_from_coherent()
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()
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
168 * @dev: device from which we allocate memory
174 * This function should be only called from per-arch dma_alloc_coherent()
175 * to support allocation from per-device coherent memory pools.
180 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, in dma_alloc_from_dev_coherent()
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
210 * @dev: device from which the memory was allocated
214 * This checks whether the memory was allocated from the per-device
220 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr) in dma_release_from_dev_coherent()
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
251 * @dev: device from which the memory was allocated
257 * This checks whether the memory was allocated from the per-device
260 * Returns 1 if @vaddr belongs to the device coherent pool and the caller
264 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma, in dma_mmap_from_dev_coherent()
275 void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size, in dma_alloc_from_global_coherent()
318 * Support for reserved memory regions defined in device tree
329 static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) in rmem_dma_device_init()
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()
345 struct device *dev) in rmem_dma_device_release()
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()
378 rmem->ops = &rmem_dma_ops; 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);