Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Coherent per-device memory handling.
0004  * Borrowed from i386
0005  */
0006 #include <linux/io.h>
0007 #include <linux/slab.h>
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/dma-direct.h>
0011 #include <linux/dma-map-ops.h>
0012 
0013 struct dma_coherent_mem {
0014     void        *virt_base;
0015     dma_addr_t  device_base;
0016     unsigned long   pfn_base;
0017     int     size;
0018     unsigned long   *bitmap;
0019     spinlock_t  spinlock;
0020     bool        use_dev_dma_pfn_offset;
0021 };
0022 
0023 static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *dev)
0024 {
0025     if (dev && dev->dma_mem)
0026         return dev->dma_mem;
0027     return NULL;
0028 }
0029 
0030 static inline dma_addr_t dma_get_device_base(struct device *dev,
0031                          struct dma_coherent_mem * mem)
0032 {
0033     if (mem->use_dev_dma_pfn_offset)
0034         return phys_to_dma(dev, PFN_PHYS(mem->pfn_base));
0035     return mem->device_base;
0036 }
0037 
0038 static struct dma_coherent_mem *dma_init_coherent_memory(phys_addr_t phys_addr,
0039         dma_addr_t device_addr, size_t size, bool use_dma_pfn_offset)
0040 {
0041     struct dma_coherent_mem *dma_mem;
0042     int pages = size >> PAGE_SHIFT;
0043     void *mem_base;
0044 
0045     if (!size)
0046         return ERR_PTR(-EINVAL);
0047 
0048     mem_base = memremap(phys_addr, size, MEMREMAP_WC);
0049     if (!mem_base)
0050         return ERR_PTR(-EINVAL);
0051 
0052     dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
0053     if (!dma_mem)
0054         goto out_unmap_membase;
0055     dma_mem->bitmap = bitmap_zalloc(pages, GFP_KERNEL);
0056     if (!dma_mem->bitmap)
0057         goto out_free_dma_mem;
0058 
0059     dma_mem->virt_base = mem_base;
0060     dma_mem->device_base = device_addr;
0061     dma_mem->pfn_base = PFN_DOWN(phys_addr);
0062     dma_mem->size = pages;
0063     dma_mem->use_dev_dma_pfn_offset = use_dma_pfn_offset;
0064     spin_lock_init(&dma_mem->spinlock);
0065 
0066     return dma_mem;
0067 
0068 out_free_dma_mem:
0069     kfree(dma_mem);
0070 out_unmap_membase:
0071     memunmap(mem_base);
0072     pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %zd MiB\n",
0073         &phys_addr, size / SZ_1M);
0074     return ERR_PTR(-ENOMEM);
0075 }
0076 
0077 static void _dma_release_coherent_memory(struct dma_coherent_mem *mem)
0078 {
0079     if (!mem)
0080         return;
0081 
0082     memunmap(mem->virt_base);
0083     bitmap_free(mem->bitmap);
0084     kfree(mem);
0085 }
0086 
0087 static int dma_assign_coherent_memory(struct device *dev,
0088                       struct dma_coherent_mem *mem)
0089 {
0090     if (!dev)
0091         return -ENODEV;
0092 
0093     if (dev->dma_mem)
0094         return -EBUSY;
0095 
0096     dev->dma_mem = mem;
0097     return 0;
0098 }
0099 
0100 /*
0101  * Declare a region of memory to be handed out by dma_alloc_coherent() when it
0102  * is asked for coherent memory for this device.  This shall only be used
0103  * from platform code, usually based on the device tree description.
0104  *
0105  * phys_addr is the CPU physical address to which the memory is currently
0106  * assigned (this will be ioremapped so the CPU can access the region).
0107  *
0108  * device_addr is the DMA address the device needs to be programmed with to
0109  * actually address this memory (this will be handed out as the dma_addr_t in
0110  * dma_alloc_coherent()).
0111  *
0112  * size is the size of the area (must be a multiple of PAGE_SIZE).
0113  *
0114  * As a simplification for the platforms, only *one* such region of memory may
0115  * be declared per device.
0116  */
0117 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
0118                 dma_addr_t device_addr, size_t size)
0119 {
0120     struct dma_coherent_mem *mem;
0121     int ret;
0122 
0123     mem = dma_init_coherent_memory(phys_addr, device_addr, size, false);
0124     if (IS_ERR(mem))
0125         return PTR_ERR(mem);
0126 
0127     ret = dma_assign_coherent_memory(dev, mem);
0128     if (ret)
0129         _dma_release_coherent_memory(mem);
0130     return ret;
0131 }
0132 
0133 void dma_release_coherent_memory(struct device *dev)
0134 {
0135     if (dev)
0136         _dma_release_coherent_memory(dev->dma_mem);
0137 }
0138 
0139 static void *__dma_alloc_from_coherent(struct device *dev,
0140                        struct dma_coherent_mem *mem,
0141                        ssize_t size, dma_addr_t *dma_handle)
0142 {
0143     int order = get_order(size);
0144     unsigned long flags;
0145     int pageno;
0146     void *ret;
0147 
0148     spin_lock_irqsave(&mem->spinlock, flags);
0149 
0150     if (unlikely(size > ((dma_addr_t)mem->size << PAGE_SHIFT)))
0151         goto err;
0152 
0153     pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
0154     if (unlikely(pageno < 0))
0155         goto err;
0156 
0157     /*
0158      * Memory was found in the coherent area.
0159      */
0160     *dma_handle = dma_get_device_base(dev, mem) +
0161             ((dma_addr_t)pageno << PAGE_SHIFT);
0162     ret = mem->virt_base + ((dma_addr_t)pageno << PAGE_SHIFT);
0163     spin_unlock_irqrestore(&mem->spinlock, flags);
0164     memset(ret, 0, size);
0165     return ret;
0166 err:
0167     spin_unlock_irqrestore(&mem->spinlock, flags);
0168     return NULL;
0169 }
0170 
0171 /**
0172  * dma_alloc_from_dev_coherent() - allocate memory from device coherent pool
0173  * @dev:    device from which we allocate memory
0174  * @size:   size of requested memory area
0175  * @dma_handle: This will be filled with the correct dma handle
0176  * @ret:    This pointer will be filled with the virtual address
0177  *      to allocated area.
0178  *
0179  * This function should be only called from per-arch dma_alloc_coherent()
0180  * to support allocation from per-device coherent memory pools.
0181  *
0182  * Returns 0 if dma_alloc_coherent should continue with allocating from
0183  * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
0184  */
0185 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
0186         dma_addr_t *dma_handle, void **ret)
0187 {
0188     struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
0189 
0190     if (!mem)
0191         return 0;
0192 
0193     *ret = __dma_alloc_from_coherent(dev, mem, size, dma_handle);
0194     return 1;
0195 }
0196 
0197 static int __dma_release_from_coherent(struct dma_coherent_mem *mem,
0198                        int order, void *vaddr)
0199 {
0200     if (mem && vaddr >= mem->virt_base && vaddr <
0201            (mem->virt_base + ((dma_addr_t)mem->size << PAGE_SHIFT))) {
0202         int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
0203         unsigned long flags;
0204 
0205         spin_lock_irqsave(&mem->spinlock, flags);
0206         bitmap_release_region(mem->bitmap, page, order);
0207         spin_unlock_irqrestore(&mem->spinlock, flags);
0208         return 1;
0209     }
0210     return 0;
0211 }
0212 
0213 /**
0214  * dma_release_from_dev_coherent() - free memory to device coherent memory pool
0215  * @dev:    device from which the memory was allocated
0216  * @order:  the order of pages allocated
0217  * @vaddr:  virtual address of allocated pages
0218  *
0219  * This checks whether the memory was allocated from the per-device
0220  * coherent memory pool and if so, releases that memory.
0221  *
0222  * Returns 1 if we correctly released the memory, or 0 if the caller should
0223  * proceed with releasing memory from generic pools.
0224  */
0225 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr)
0226 {
0227     struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
0228 
0229     return __dma_release_from_coherent(mem, order, vaddr);
0230 }
0231 
0232 static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem,
0233         struct vm_area_struct *vma, void *vaddr, size_t size, int *ret)
0234 {
0235     if (mem && vaddr >= mem->virt_base && vaddr + size <=
0236            (mem->virt_base + ((dma_addr_t)mem->size << PAGE_SHIFT))) {
0237         unsigned long off = vma->vm_pgoff;
0238         int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
0239         unsigned long user_count = vma_pages(vma);
0240         int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
0241 
0242         *ret = -ENXIO;
0243         if (off < count && user_count <= count - off) {
0244             unsigned long pfn = mem->pfn_base + start + off;
0245             *ret = remap_pfn_range(vma, vma->vm_start, pfn,
0246                            user_count << PAGE_SHIFT,
0247                            vma->vm_page_prot);
0248         }
0249         return 1;
0250     }
0251     return 0;
0252 }
0253 
0254 /**
0255  * dma_mmap_from_dev_coherent() - mmap memory from the device coherent pool
0256  * @dev:    device from which the memory was allocated
0257  * @vma:    vm_area for the userspace memory
0258  * @vaddr:  cpu address returned by dma_alloc_from_dev_coherent
0259  * @size:   size of the memory buffer allocated
0260  * @ret:    result from remap_pfn_range()
0261  *
0262  * This checks whether the memory was allocated from the per-device
0263  * coherent memory pool and if so, maps that memory to the provided vma.
0264  *
0265  * Returns 1 if @vaddr belongs to the device coherent pool and the caller
0266  * should return @ret, or 0 if they should proceed with mapping memory from
0267  * generic areas.
0268  */
0269 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
0270                void *vaddr, size_t size, int *ret)
0271 {
0272     struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
0273 
0274     return __dma_mmap_from_coherent(mem, vma, vaddr, size, ret);
0275 }
0276 
0277 #ifdef CONFIG_DMA_GLOBAL_POOL
0278 static struct dma_coherent_mem *dma_coherent_default_memory __ro_after_init;
0279 
0280 void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
0281                      dma_addr_t *dma_handle)
0282 {
0283     if (!dma_coherent_default_memory)
0284         return NULL;
0285 
0286     return __dma_alloc_from_coherent(dev, dma_coherent_default_memory, size,
0287                      dma_handle);
0288 }
0289 
0290 int dma_release_from_global_coherent(int order, void *vaddr)
0291 {
0292     if (!dma_coherent_default_memory)
0293         return 0;
0294 
0295     return __dma_release_from_coherent(dma_coherent_default_memory, order,
0296             vaddr);
0297 }
0298 
0299 int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *vaddr,
0300                    size_t size, int *ret)
0301 {
0302     if (!dma_coherent_default_memory)
0303         return 0;
0304 
0305     return __dma_mmap_from_coherent(dma_coherent_default_memory, vma,
0306                     vaddr, size, ret);
0307 }
0308 
0309 int dma_init_global_coherent(phys_addr_t phys_addr, size_t size)
0310 {
0311     struct dma_coherent_mem *mem;
0312 
0313     mem = dma_init_coherent_memory(phys_addr, phys_addr, size, true);
0314     if (IS_ERR(mem))
0315         return PTR_ERR(mem);
0316     dma_coherent_default_memory = mem;
0317     pr_info("DMA: default coherent area is set\n");
0318     return 0;
0319 }
0320 #endif /* CONFIG_DMA_GLOBAL_POOL */
0321 
0322 /*
0323  * Support for reserved memory regions defined in device tree
0324  */
0325 #ifdef CONFIG_OF_RESERVED_MEM
0326 #include <linux/of.h>
0327 #include <linux/of_fdt.h>
0328 #include <linux/of_reserved_mem.h>
0329 
0330 #ifdef CONFIG_DMA_GLOBAL_POOL
0331 static struct reserved_mem *dma_reserved_default_memory __initdata;
0332 #endif
0333 
0334 static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
0335 {
0336     if (!rmem->priv) {
0337         struct dma_coherent_mem *mem;
0338 
0339         mem = dma_init_coherent_memory(rmem->base, rmem->base,
0340                            rmem->size, true);
0341         if (IS_ERR(mem))
0342             return PTR_ERR(mem);
0343         rmem->priv = mem;
0344     }
0345     dma_assign_coherent_memory(dev, rmem->priv);
0346     return 0;
0347 }
0348 
0349 static void rmem_dma_device_release(struct reserved_mem *rmem,
0350                     struct device *dev)
0351 {
0352     if (dev)
0353         dev->dma_mem = NULL;
0354 }
0355 
0356 static const struct reserved_mem_ops rmem_dma_ops = {
0357     .device_init    = rmem_dma_device_init,
0358     .device_release = rmem_dma_device_release,
0359 };
0360 
0361 static int __init rmem_dma_setup(struct reserved_mem *rmem)
0362 {
0363     unsigned long node = rmem->fdt_node;
0364 
0365     if (of_get_flat_dt_prop(node, "reusable", NULL))
0366         return -EINVAL;
0367 
0368 #ifdef CONFIG_ARM
0369     if (!of_get_flat_dt_prop(node, "no-map", NULL)) {
0370         pr_err("Reserved memory: regions without no-map are not yet supported\n");
0371         return -EINVAL;
0372     }
0373 #endif
0374 
0375 #ifdef CONFIG_DMA_GLOBAL_POOL
0376     if (of_get_flat_dt_prop(node, "linux,dma-default", NULL)) {
0377         WARN(dma_reserved_default_memory,
0378              "Reserved memory: region for default DMA coherent area is redefined\n");
0379         dma_reserved_default_memory = rmem;
0380     }
0381 #endif
0382 
0383     rmem->ops = &rmem_dma_ops;
0384     pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n",
0385         &rmem->base, (unsigned long)rmem->size / SZ_1M);
0386     return 0;
0387 }
0388 
0389 #ifdef CONFIG_DMA_GLOBAL_POOL
0390 static int __init dma_init_reserved_memory(void)
0391 {
0392     if (!dma_reserved_default_memory)
0393         return -ENOMEM;
0394     return dma_init_global_coherent(dma_reserved_default_memory->base,
0395                     dma_reserved_default_memory->size);
0396 }
0397 core_initcall(dma_init_reserved_memory);
0398 #endif /* CONFIG_DMA_GLOBAL_POOL */
0399 
0400 RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);
0401 #endif