dma-mapping: move all DMA mapping code to kernel/dma
Currently the code is split over various files with dma- prefixes in the lib/ and drives/base directories, and the number of files keeps growing. Move them into a single directory to keep the code together and remove the file name prefixes. To match the irq infrastructure this directory is placed under the kernel/ directory. Signed-off-by: Christoph Hellwig <hch@lst.de>
This commit is contained in:
@@ -41,6 +41,7 @@ obj-y += printk/
|
||||
obj-y += irq/
|
||||
obj-y += rcu/
|
||||
obj-y += livepatch/
|
||||
obj-y += dma/
|
||||
|
||||
obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o
|
||||
obj-$(CONFIG_FREEZER) += freezer.o
|
||||
|
50
kernel/dma/Kconfig
Normal file
50
kernel/dma/Kconfig
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
config HAS_DMA
|
||||
bool
|
||||
depends on !NO_DMA
|
||||
default y
|
||||
|
||||
config NEED_SG_DMA_LENGTH
|
||||
bool
|
||||
|
||||
config NEED_DMA_MAP_STATE
|
||||
bool
|
||||
|
||||
config ARCH_DMA_ADDR_T_64BIT
|
||||
def_bool 64BIT || PHYS_ADDR_T_64BIT
|
||||
|
||||
config HAVE_GENERIC_DMA_COHERENT
|
||||
bool
|
||||
|
||||
config ARCH_HAS_SYNC_DMA_FOR_DEVICE
|
||||
bool
|
||||
|
||||
config ARCH_HAS_SYNC_DMA_FOR_CPU
|
||||
bool
|
||||
select NEED_DMA_MAP_STATE
|
||||
|
||||
config DMA_DIRECT_OPS
|
||||
bool
|
||||
depends on HAS_DMA
|
||||
|
||||
config DMA_NONCOHERENT_OPS
|
||||
bool
|
||||
depends on HAS_DMA
|
||||
select DMA_DIRECT_OPS
|
||||
|
||||
config DMA_NONCOHERENT_MMAP
|
||||
bool
|
||||
depends on DMA_NONCOHERENT_OPS
|
||||
|
||||
config DMA_NONCOHERENT_CACHE_SYNC
|
||||
bool
|
||||
depends on DMA_NONCOHERENT_OPS
|
||||
|
||||
config DMA_VIRT_OPS
|
||||
bool
|
||||
depends on HAS_DMA
|
||||
|
||||
config SWIOTLB
|
||||
bool
|
||||
select DMA_DIRECT_OPS
|
||||
select NEED_DMA_MAP_STATE
|
11
kernel/dma/Makefile
Normal file
11
kernel/dma/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
obj-$(CONFIG_HAS_DMA) += mapping.o
|
||||
obj-$(CONFIG_DMA_CMA) += contiguous.o
|
||||
obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += coherent.o
|
||||
obj-$(CONFIG_DMA_DIRECT_OPS) += direct.o
|
||||
obj-$(CONFIG_DMA_NONCOHERENT_OPS) += noncoherent.o
|
||||
obj-$(CONFIG_DMA_VIRT_OPS) += virt.o
|
||||
obj-$(CONFIG_DMA_API_DEBUG) += debug.o
|
||||
obj-$(CONFIG_SWIOTLB) += swiotlb.o
|
||||
|
434
kernel/dma/coherent.c
Normal file
434
kernel/dma/coherent.c
Normal file
@@ -0,0 +1,434 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* Coherent per-device memory handling.
|
||||
* Borrowed from i386
|
||||
*/
|
||||
#include <linux/io.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/dma-mapping.h>
|
||||
|
||||
struct dma_coherent_mem {
|
||||
void *virt_base;
|
||||
dma_addr_t device_base;
|
||||
unsigned long pfn_base;
|
||||
int size;
|
||||
int flags;
|
||||
unsigned long *bitmap;
|
||||
spinlock_t spinlock;
|
||||
bool use_dev_dma_pfn_offset;
|
||||
};
|
||||
|
||||
static struct dma_coherent_mem *dma_coherent_default_memory __ro_after_init;
|
||||
|
||||
static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *dev)
|
||||
{
|
||||
if (dev && dev->dma_mem)
|
||||
return dev->dma_mem;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline dma_addr_t dma_get_device_base(struct device *dev,
|
||||
struct dma_coherent_mem * mem)
|
||||
{
|
||||
if (mem->use_dev_dma_pfn_offset)
|
||||
return (mem->pfn_base - dev->dma_pfn_offset) << PAGE_SHIFT;
|
||||
else
|
||||
return mem->device_base;
|
||||
}
|
||||
|
||||
static int dma_init_coherent_memory(
|
||||
phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags,
|
||||
struct dma_coherent_mem **mem)
|
||||
{
|
||||
struct dma_coherent_mem *dma_mem = NULL;
|
||||
void __iomem *mem_base = NULL;
|
||||
int pages = size >> PAGE_SHIFT;
|
||||
int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
|
||||
int ret;
|
||||
|
||||
if (!size) {
|
||||
ret = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
mem_base = memremap(phys_addr, size, MEMREMAP_WC);
|
||||
if (!mem_base) {
|
||||
ret = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
|
||||
if (!dma_mem) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
|
||||
if (!dma_mem->bitmap) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
dma_mem->virt_base = mem_base;
|
||||
dma_mem->device_base = device_addr;
|
||||
dma_mem->pfn_base = PFN_DOWN(phys_addr);
|
||||
dma_mem->size = pages;
|
||||
dma_mem->flags = flags;
|
||||
spin_lock_init(&dma_mem->spinlock);
|
||||
|
||||
*mem = dma_mem;
|
||||
return 0;
|
||||
|
||||
out:
|
||||
kfree(dma_mem);
|
||||
if (mem_base)
|
||||
memunmap(mem_base);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void dma_release_coherent_memory(struct dma_coherent_mem *mem)
|
||||
{
|
||||
if (!mem)
|
||||
return;
|
||||
|
||||
memunmap(mem->virt_base);
|
||||
kfree(mem->bitmap);
|
||||
kfree(mem);
|
||||
}
|
||||
|
||||
static int dma_assign_coherent_memory(struct device *dev,
|
||||
struct dma_coherent_mem *mem)
|
||||
{
|
||||
if (!dev)
|
||||
return -ENODEV;
|
||||
|
||||
if (dev->dma_mem)
|
||||
return -EBUSY;
|
||||
|
||||
dev->dma_mem = mem;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
|
||||
dma_addr_t device_addr, size_t size, int flags)
|
||||
{
|
||||
struct dma_coherent_mem *mem;
|
||||
int ret;
|
||||
|
||||
ret = dma_init_coherent_memory(phys_addr, device_addr, size, flags, &mem);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = dma_assign_coherent_memory(dev, mem);
|
||||
if (ret)
|
||||
dma_release_coherent_memory(mem);
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(dma_declare_coherent_memory);
|
||||
|
||||
void dma_release_declared_memory(struct device *dev)
|
||||
{
|
||||
struct dma_coherent_mem *mem = dev->dma_mem;
|
||||
|
||||
if (!mem)
|
||||
return;
|
||||
dma_release_coherent_memory(mem);
|
||||
dev->dma_mem = NULL;
|
||||
}
|
||||
EXPORT_SYMBOL(dma_release_declared_memory);
|
||||
|
||||
void *dma_mark_declared_memory_occupied(struct device *dev,
|
||||
dma_addr_t device_addr, size_t size)
|
||||
{
|
||||
struct dma_coherent_mem *mem = dev->dma_mem;
|
||||
unsigned long flags;
|
||||
int pos, err;
|
||||
|
||||
size += device_addr & ~PAGE_MASK;
|
||||
|
||||
if (!mem)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
spin_lock_irqsave(&mem->spinlock, flags);
|
||||
pos = PFN_DOWN(device_addr - dma_get_device_base(dev, mem));
|
||||
err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
|
||||
spin_unlock_irqrestore(&mem->spinlock, flags);
|
||||
|
||||
if (err != 0)
|
||||
return ERR_PTR(err);
|
||||
return mem->virt_base + (pos << PAGE_SHIFT);
|
||||
}
|
||||
EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
|
||||
|
||||
static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem,
|
||||
ssize_t size, dma_addr_t *dma_handle)
|
||||
{
|
||||
int order = get_order(size);
|
||||
unsigned long flags;
|
||||
int pageno;
|
||||
void *ret;
|
||||
|
||||
spin_lock_irqsave(&mem->spinlock, flags);
|
||||
|
||||
if (unlikely(size > (mem->size << PAGE_SHIFT)))
|
||||
goto err;
|
||||
|
||||
pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
|
||||
if (unlikely(pageno < 0))
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* Memory was found in the coherent area.
|
||||
*/
|
||||
*dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
|
||||
ret = mem->virt_base + (pageno << PAGE_SHIFT);
|
||||
spin_unlock_irqrestore(&mem->spinlock, flags);
|
||||
memset(ret, 0, size);
|
||||
return ret;
|
||||
err:
|
||||
spin_unlock_irqrestore(&mem->spinlock, flags);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* dma_alloc_from_dev_coherent() - allocate memory from device coherent pool
|
||||
* @dev: device from which we allocate memory
|
||||
* @size: size of requested memory area
|
||||
* @dma_handle: This will be filled with the correct dma handle
|
||||
* @ret: This pointer will be filled with the virtual address
|
||||
* to allocated area.
|
||||
*
|
||||
* This function should be only called from per-arch dma_alloc_coherent()
|
||||
* to support allocation from per-device coherent memory pools.
|
||||
*
|
||||
* Returns 0 if dma_alloc_coherent should continue with allocating from
|
||||
* generic memory areas, or !0 if dma_alloc_coherent should return @ret.
|
||||
*/
|
||||
int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
|
||||
dma_addr_t *dma_handle, void **ret)
|
||||
{
|
||||
struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
|
||||
|
||||
if (!mem)
|
||||
return 0;
|
||||
|
||||
*ret = __dma_alloc_from_coherent(mem, size, dma_handle);
|
||||
if (*ret)
|
||||
return 1;
|
||||
|
||||
/*
|
||||
* In the case where the allocation can not be satisfied from the
|
||||
* per-device area, try to fall back to generic memory if the
|
||||
* constraints allow it.
|
||||
*/
|
||||
return mem->flags & DMA_MEMORY_EXCLUSIVE;
|
||||
}
|
||||
EXPORT_SYMBOL(dma_alloc_from_dev_coherent);
|
||||
|
||||
void *dma_alloc_from_global_coherent(ssize_t size, dma_addr_t *dma_handle)
|
||||
{
|
||||
if (!dma_coherent_default_memory)
|
||||
return NULL;
|
||||
|
||||
return __dma_alloc_from_coherent(dma_coherent_default_memory, size,
|
||||
dma_handle);
|
||||
}
|
||||
|
||||
static int __dma_release_from_coherent(struct dma_coherent_mem *mem,
|
||||
int order, void *vaddr)
|
||||
{
|
||||
if (mem && vaddr >= mem->virt_base && vaddr <
|
||||
(mem->virt_base + (mem->size << PAGE_SHIFT))) {
|
||||
int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
|
||||
unsigned long flags;
|
||||
|
||||
spin_lock_irqsave(&mem->spinlock, flags);
|
||||
bitmap_release_region(mem->bitmap, page, order);
|
||||
spin_unlock_irqrestore(&mem->spinlock, flags);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* dma_release_from_dev_coherent() - free memory to device coherent memory pool
|
||||
* @dev: device from which the memory was allocated
|
||||
* @order: the order of pages allocated
|
||||
* @vaddr: virtual address of allocated pages
|
||||
*
|
||||
* This checks whether the memory was allocated from the per-device
|
||||
* coherent memory pool and if so, releases that memory.
|
||||
*
|
||||
* Returns 1 if we correctly released the memory, or 0 if the caller should
|
||||
* proceed with releasing memory from generic pools.
|
||||
*/
|
||||
int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr)
|
||||
{
|
||||
struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
|
||||
|
||||
return __dma_release_from_coherent(mem, order, vaddr);
|
||||
}
|
||||
EXPORT_SYMBOL(dma_release_from_dev_coherent);
|
||||
|
||||
int dma_release_from_global_coherent(int order, void *vaddr)
|
||||
{
|
||||
if (!dma_coherent_default_memory)
|
||||
return 0;
|
||||
|
||||
return __dma_release_from_coherent(dma_coherent_default_memory, order,
|
||||
vaddr);
|
||||
}
|
||||
|
||||
static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem,
|
||||
struct vm_area_struct *vma, void *vaddr, size_t size, int *ret)
|
||||
{
|
||||
if (mem && vaddr >= mem->virt_base && vaddr + size <=
|
||||
(mem->virt_base + (mem->size << PAGE_SHIFT))) {
|
||||
unsigned long off = vma->vm_pgoff;
|
||||
int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
|
||||
int user_count = vma_pages(vma);
|
||||
int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
||||
|
||||
*ret = -ENXIO;
|
||||
if (off < count && user_count <= count - off) {
|
||||
unsigned long pfn = mem->pfn_base + start + off;
|
||||
*ret = remap_pfn_range(vma, vma->vm_start, pfn,
|
||||
user_count << PAGE_SHIFT,
|
||||
vma->vm_page_prot);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* dma_mmap_from_dev_coherent() - mmap memory from the device coherent pool
|
||||
* @dev: device from which the memory was allocated
|
||||
* @vma: vm_area for the userspace memory
|
||||
* @vaddr: cpu address returned by dma_alloc_from_dev_coherent
|
||||
* @size: size of the memory buffer allocated
|
||||
* @ret: result from remap_pfn_range()
|
||||
*
|
||||
* This checks whether the memory was allocated from the per-device
|
||||
* coherent memory pool and if so, maps that memory to the provided vma.
|
||||
*
|
||||
* Returns 1 if @vaddr belongs to the device coherent pool and the caller
|
||||
* should return @ret, or 0 if they should proceed with mapping memory from
|
||||
* generic areas.
|
||||
*/
|
||||
int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
|
||||
void *vaddr, size_t size, int *ret)
|
||||
{
|
||||
struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
|
||||
|
||||
return __dma_mmap_from_coherent(mem, vma, vaddr, size, ret);
|
||||
}
|
||||
EXPORT_SYMBOL(dma_mmap_from_dev_coherent);
|
||||
|
||||
int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *vaddr,
|
||||
size_t size, int *ret)
|
||||
{
|
||||
if (!dma_coherent_default_memory)
|
||||
return 0;
|
||||
|
||||
return __dma_mmap_from_coherent(dma_coherent_default_memory, vma,
|
||||
vaddr, size, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Support for reserved memory regions defined in device tree
|
||||
*/
|
||||
#ifdef CONFIG_OF_RESERVED_MEM
|
||||
#include <linux/of.h>
|
||||
#include <linux/of_fdt.h>
|
||||
#include <linux/of_reserved_mem.h>
|
||||
|
||||
static struct reserved_mem *dma_reserved_default_memory __initdata;
|
||||
|
||||
static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
|
||||
{
|
||||
struct dma_coherent_mem *mem = rmem->priv;
|
||||
int ret;
|
||||
|
||||
if (!mem) {
|
||||
ret = dma_init_coherent_memory(rmem->base, rmem->base,
|
||||
rmem->size,
|
||||
DMA_MEMORY_EXCLUSIVE, &mem);
|
||||
if (ret) {
|
||||
pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n",
|
||||
&rmem->base, (unsigned long)rmem->size / SZ_1M);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
mem->use_dev_dma_pfn_offset = true;
|
||||
rmem->priv = mem;
|
||||
dma_assign_coherent_memory(dev, mem);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void rmem_dma_device_release(struct reserved_mem *rmem,
|
||||
struct device *dev)
|
||||
{
|
||||
if (dev)
|
||||
dev->dma_mem = NULL;
|
||||
}
|
||||
|
||||
static const struct reserved_mem_ops rmem_dma_ops = {
|
||||
.device_init = rmem_dma_device_init,
|
||||
.device_release = rmem_dma_device_release,
|
||||
};
|
||||
|
||||
static int __init rmem_dma_setup(struct reserved_mem *rmem)
|
||||
{
|
||||
unsigned long node = rmem->fdt_node;
|
||||
|
||||
if (of_get_flat_dt_prop(node, "reusable", NULL))
|
||||
return -EINVAL;
|
||||
|
||||
#ifdef CONFIG_ARM
|
||||
if (!of_get_flat_dt_prop(node, "no-map", NULL)) {
|
||||
pr_err("Reserved memory: regions without no-map are not yet supported\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (of_get_flat_dt_prop(node, "linux,dma-default", NULL)) {
|
||||
WARN(dma_reserved_default_memory,
|
||||
"Reserved memory: region for default DMA coherent area is redefined\n");
|
||||
dma_reserved_default_memory = rmem;
|
||||
}
|
||||
#endif
|
||||
|
||||
rmem->ops = &rmem_dma_ops;
|
||||
pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n",
|
||||
&rmem->base, (unsigned long)rmem->size / SZ_1M);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __init dma_init_reserved_memory(void)
|
||||
{
|
||||
const struct reserved_mem_ops *ops;
|
||||
int ret;
|
||||
|
||||
if (!dma_reserved_default_memory)
|
||||
return -ENOMEM;
|
||||
|
||||
ops = dma_reserved_default_memory->ops;
|
||||
|
||||
/*
|
||||
* We rely on rmem_dma_device_init() does not propagate error of
|
||||
* dma_assign_coherent_memory() for "NULL" device.
|
||||
*/
|
||||
ret = ops->device_init(dma_reserved_default_memory, NULL);
|
||||
|
||||
if (!ret) {
|
||||
dma_coherent_default_memory = dma_reserved_default_memory->priv;
|
||||
pr_info("DMA: default coherent area is set\n");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
core_initcall(dma_init_reserved_memory);
|
||||
|
||||
RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);
|
||||
#endif
|
278
kernel/dma/contiguous.c
Normal file
278
kernel/dma/contiguous.c
Normal file
@@ -0,0 +1,278 @@
|
||||
// SPDX-License-Identifier: GPL-2.0+
|
||||
/*
|
||||
* Contiguous Memory Allocator for DMA mapping framework
|
||||
* Copyright (c) 2010-2011 by Samsung Electronics.
|
||||
* Written by:
|
||||
* Marek Szyprowski <m.szyprowski@samsung.com>
|
||||
* Michal Nazarewicz <mina86@mina86.com>
|
||||
*/
|
||||
|
||||
#define pr_fmt(fmt) "cma: " fmt
|
||||
|
||||
#ifdef CONFIG_CMA_DEBUG
|
||||
#ifndef DEBUG
|
||||
# define DEBUG
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <asm/page.h>
|
||||
#include <asm/dma-contiguous.h>
|
||||
|
||||
#include <linux/memblock.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/sizes.h>
|
||||
#include <linux/dma-contiguous.h>
|
||||
#include <linux/cma.h>
|
||||
|
||||
#ifdef CONFIG_CMA_SIZE_MBYTES
|
||||
#define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
|
||||
#else
|
||||
#define CMA_SIZE_MBYTES 0
|
||||
#endif
|
||||
|
||||
struct cma *dma_contiguous_default_area;
|
||||
|
||||
/*
|
||||
* Default global CMA area size can be defined in kernel's .config.
|
||||
* This is useful mainly for distro maintainers to create a kernel
|
||||
* that works correctly for most supported systems.
|
||||
* The size can be set in bytes or as a percentage of the total memory
|
||||
* in the system.
|
||||
*
|
||||
* Users, who want to set the size of global CMA area for their system
|
||||
* should use cma= kernel parameter.
|
||||
*/
|
||||
static const phys_addr_t size_bytes = (phys_addr_t)CMA_SIZE_MBYTES * SZ_1M;
|
||||
static phys_addr_t size_cmdline = -1;
|
||||
static phys_addr_t base_cmdline;
|
||||
static phys_addr_t limit_cmdline;
|
||||
|
||||
static int __init early_cma(char *p)
|
||||
{
|
||||
pr_debug("%s(%s)\n", __func__, p);
|
||||
size_cmdline = memparse(p, &p);
|
||||
if (*p != '@')
|
||||
return 0;
|
||||
base_cmdline = memparse(p + 1, &p);
|
||||
if (*p != '-') {
|
||||
limit_cmdline = base_cmdline + size_cmdline;
|
||||
return 0;
|
||||
}
|
||||
limit_cmdline = memparse(p + 1, &p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
early_param("cma", early_cma);
|
||||
|
||||
#ifdef CONFIG_CMA_SIZE_PERCENTAGE
|
||||
|
||||
static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
|
||||
{
|
||||
struct memblock_region *reg;
|
||||
unsigned long total_pages = 0;
|
||||
|
||||
/*
|
||||
* We cannot use memblock_phys_mem_size() here, because
|
||||
* memblock_analyze() has not been called yet.
|
||||
*/
|
||||
for_each_memblock(memory, reg)
|
||||
total_pages += memblock_region_memory_end_pfn(reg) -
|
||||
memblock_region_memory_base_pfn(reg);
|
||||
|
||||
return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* dma_contiguous_reserve() - reserve area(s) for contiguous memory handling
|
||||
* @limit: End address of the reserved memory (optional, 0 for any).
|
||||
*
|
||||
* This function reserves memory from early allocator. It should be
|
||||
* called by arch specific code once the early allocator (memblock or bootmem)
|
||||
* has been activated and all other subsystems have already allocated/reserved
|
||||
* memory.
|
||||
*/
|
||||
void __init dma_contiguous_reserve(phys_addr_t limit)
|
||||
{
|
||||
phys_addr_t selected_size = 0;
|
||||
phys_addr_t selected_base = 0;
|
||||
phys_addr_t selected_limit = limit;
|
||||
bool fixed = false;
|
||||
|
||||
pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
|
||||
|
||||
if (size_cmdline != -1) {
|
||||
selected_size = size_cmdline;
|
||||
selected_base = base_cmdline;
|
||||
selected_limit = min_not_zero(limit_cmdline, limit);
|
||||
if (base_cmdline + size_cmdline == limit_cmdline)
|
||||
fixed = true;
|
||||
} else {
|
||||
#ifdef CONFIG_CMA_SIZE_SEL_MBYTES
|
||||
selected_size = size_bytes;
|
||||
#elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
|
||||
selected_size = cma_early_percent_memory();
|
||||
#elif defined(CONFIG_CMA_SIZE_SEL_MIN)
|
||||
selected_size = min(size_bytes, cma_early_percent_memory());
|
||||
#elif defined(CONFIG_CMA_SIZE_SEL_MAX)
|
||||
selected_size = max(size_bytes, cma_early_percent_memory());
|
||||
#endif
|
||||
}
|
||||
|
||||
if (selected_size && !dma_contiguous_default_area) {
|
||||
pr_debug("%s: reserving %ld MiB for global area\n", __func__,
|
||||
(unsigned long)selected_size / SZ_1M);
|
||||
|
||||
dma_contiguous_reserve_area(selected_size, selected_base,
|
||||
selected_limit,
|
||||
&dma_contiguous_default_area,
|
||||
fixed);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dma_contiguous_reserve_area() - reserve custom contiguous area
|
||||
* @size: Size of the reserved area (in bytes),
|
||||
* @base: Base address of the reserved area optional, use 0 for any
|
||||
* @limit: End address of the reserved memory (optional, 0 for any).
|
||||
* @res_cma: Pointer to store the created cma region.
|
||||
* @fixed: hint about where to place the reserved area
|
||||
*
|
||||
* This function reserves memory from early allocator. It should be
|
||||
* called by arch specific code once the early allocator (memblock or bootmem)
|
||||
* has been activated and all other subsystems have already allocated/reserved
|
||||
* memory. This function allows to create custom reserved areas for specific
|
||||
* devices.
|
||||
*
|
||||
* If @fixed is true, reserve contiguous area at exactly @base. If false,
|
||||
* reserve in range from @base to @limit.
|
||||
*/
|
||||
int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
|
||||
phys_addr_t limit, struct cma **res_cma,
|
||||
bool fixed)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed,
|
||||
"reserved", res_cma);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
/* Architecture specific contiguous memory fixup. */
|
||||
dma_contiguous_early_fixup(cma_get_base(*res_cma),
|
||||
cma_get_size(*res_cma));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* dma_alloc_from_contiguous() - allocate pages from contiguous area
|
||||
* @dev: Pointer to device for which the allocation is performed.
|
||||
* @count: Requested number of pages.
|
||||
* @align: Requested alignment of pages (in PAGE_SIZE order).
|
||||
* @gfp_mask: GFP flags to use for this allocation.
|
||||
*
|
||||
* This function allocates memory buffer for specified device. It uses
|
||||
* device specific contiguous memory area if available or the default
|
||||
* global one. Requires architecture specific dev_get_cma_area() helper
|
||||
* function.
|
||||
*/
|
||||
struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
|
||||
unsigned int align, gfp_t gfp_mask)
|
||||
{
|
||||
if (align > CONFIG_CMA_ALIGNMENT)
|
||||
align = CONFIG_CMA_ALIGNMENT;
|
||||
|
||||
return cma_alloc(dev_get_cma_area(dev), count, align, gfp_mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* dma_release_from_contiguous() - release allocated pages
|
||||
* @dev: Pointer to device for which the pages were allocated.
|
||||
* @pages: Allocated pages.
|
||||
* @count: Number of allocated pages.
|
||||
*
|
||||
* This function releases memory allocated by dma_alloc_from_contiguous().
|
||||
* It returns false when provided pages do not belong to contiguous area and
|
||||
* true otherwise.
|
||||
*/
|
||||
bool dma_release_from_contiguous(struct device *dev, struct page *pages,
|
||||
int count)
|
||||
{
|
||||
return cma_release(dev_get_cma_area(dev), pages, count);
|
||||
}
|
||||
|
||||
/*
|
||||
* Support for reserved memory regions defined in device tree
|
||||
*/
|
||||
#ifdef CONFIG_OF_RESERVED_MEM
|
||||
#include <linux/of.h>
|
||||
#include <linux/of_fdt.h>
|
||||
#include <linux/of_reserved_mem.h>
|
||||
|
||||
#undef pr_fmt
|
||||
#define pr_fmt(fmt) fmt
|
||||
|
||||
static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev)
|
||||
{
|
||||
dev_set_cma_area(dev, rmem->priv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void rmem_cma_device_release(struct reserved_mem *rmem,
|
||||
struct device *dev)
|
||||
{
|
||||
dev_set_cma_area(dev, NULL);
|
||||
}
|
||||
|
||||
static const struct reserved_mem_ops rmem_cma_ops = {
|
||||
.device_init = rmem_cma_device_init,
|
||||
.device_release = rmem_cma_device_release,
|
||||
};
|
||||
|
||||
static int __init rmem_cma_setup(struct reserved_mem *rmem)
|
||||
{
|
||||
phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
|
||||
phys_addr_t mask = align - 1;
|
||||
unsigned long node = rmem->fdt_node;
|
||||
struct cma *cma;
|
||||
int err;
|
||||
|
||||
if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
|
||||
of_get_flat_dt_prop(node, "no-map", NULL))
|
||||
return -EINVAL;
|
||||
|
||||
if ((rmem->base & mask) || (rmem->size & mask)) {
|
||||
pr_err("Reserved memory: incorrect alignment of CMA region\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma);
|
||||
if (err) {
|
||||
pr_err("Reserved memory: unable to setup CMA region\n");
|
||||
return err;
|
||||
}
|
||||
/* Architecture specific contiguous memory fixup. */
|
||||
dma_contiguous_early_fixup(rmem->base, rmem->size);
|
||||
|
||||
if (of_get_flat_dt_prop(node, "linux,cma-default", NULL))
|
||||
dma_contiguous_set_default(cma);
|
||||
|
||||
rmem->ops = &rmem_cma_ops;
|
||||
rmem->priv = cma;
|
||||
|
||||
pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
|
||||
&rmem->base, (unsigned long)rmem->size / SZ_1M);
|
||||
|
||||
return 0;
|
||||
}
|
||||
RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
|
||||
#endif
|
1773
kernel/dma/debug.c
Normal file
1773
kernel/dma/debug.c
Normal file
File diff suppressed because it is too large
Load Diff
204
kernel/dma/direct.c
Normal file
204
kernel/dma/direct.c
Normal file
@@ -0,0 +1,204 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* DMA operations that map physical memory directly without using an IOMMU or
|
||||
* flushing caches.
|
||||
*/
|
||||
#include <linux/export.h>
|
||||
#include <linux/mm.h>
|
||||
#include <linux/dma-direct.h>
|
||||
#include <linux/scatterlist.h>
|
||||
#include <linux/dma-contiguous.h>
|
||||
#include <linux/pfn.h>
|
||||
#include <linux/set_memory.h>
|
||||
|
||||
#define DIRECT_MAPPING_ERROR 0
|
||||
|
||||
/*
|
||||
* Most architectures use ZONE_DMA for the first 16 Megabytes, but
|
||||
* some use it for entirely different regions:
|
||||
*/
|
||||
#ifndef ARCH_ZONE_DMA_BITS
|
||||
#define ARCH_ZONE_DMA_BITS 24
|
||||
#endif
|
||||
|
||||
/*
|
||||
* For AMD SEV all DMA must be to unencrypted addresses.
|
||||
*/
|
||||
static inline bool force_dma_unencrypted(void)
|
||||
{
|
||||
return sev_active();
|
||||
}
|
||||
|
||||
static bool
|
||||
check_addr(struct device *dev, dma_addr_t dma_addr, size_t size,
|
||||
const char *caller)
|
||||
{
|
||||
if (unlikely(dev && !dma_capable(dev, dma_addr, size))) {
|
||||
if (!dev->dma_mask) {
|
||||
dev_err(dev,
|
||||
"%s: call on device without dma_mask\n",
|
||||
caller);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (*dev->dma_mask >= DMA_BIT_MASK(32)) {
|
||||
dev_err(dev,
|
||||
"%s: overflow %pad+%zu of device mask %llx\n",
|
||||
caller, &dma_addr, size, *dev->dma_mask);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
|
||||
{
|
||||
dma_addr_t addr = force_dma_unencrypted() ?
|
||||
__phys_to_dma(dev, phys) : phys_to_dma(dev, phys);
|
||||
return addr + size - 1 <= dev->coherent_dma_mask;
|
||||
}
|
||||
|
||||
void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
||||
gfp_t gfp, unsigned long attrs)
|
||||
{
|
||||
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
||||
int page_order = get_order(size);
|
||||
struct page *page = NULL;
|
||||
void *ret;
|
||||
|
||||
/* we always manually zero the memory once we are done: */
|
||||
gfp &= ~__GFP_ZERO;
|
||||
|
||||
/* GFP_DMA32 and GFP_DMA are no ops without the corresponding zones: */
|
||||
if (dev->coherent_dma_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
|
||||
gfp |= GFP_DMA;
|
||||
if (dev->coherent_dma_mask <= DMA_BIT_MASK(32) && !(gfp & GFP_DMA))
|
||||
gfp |= GFP_DMA32;
|
||||
|
||||
again:
|
||||
/* CMA can be used only in the context which permits sleeping */
|
||||
if (gfpflags_allow_blocking(gfp)) {
|
||||
page = dma_alloc_from_contiguous(dev, count, page_order, gfp);
|
||||
if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
|
||||
dma_release_from_contiguous(dev, page, count);
|
||||
page = NULL;
|
||||
}
|
||||
}
|
||||
if (!page)
|
||||
page = alloc_pages_node(dev_to_node(dev), gfp, page_order);
|
||||
|
||||
if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
|
||||
__free_pages(page, page_order);
|
||||
page = NULL;
|
||||
|
||||
if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
|
||||
dev->coherent_dma_mask < DMA_BIT_MASK(64) &&
|
||||
!(gfp & (GFP_DMA32 | GFP_DMA))) {
|
||||
gfp |= GFP_DMA32;
|
||||
goto again;
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_ZONE_DMA) &&
|
||||
dev->coherent_dma_mask < DMA_BIT_MASK(32) &&
|
||||
!(gfp & GFP_DMA)) {
|
||||
gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
|
||||
goto again;
|
||||
}
|
||||
}
|
||||
|
||||
if (!page)
|
||||
return NULL;
|
||||
ret = page_address(page);
|
||||
if (force_dma_unencrypted()) {
|
||||
set_memory_decrypted((unsigned long)ret, 1 << page_order);
|
||||
*dma_handle = __phys_to_dma(dev, page_to_phys(page));
|
||||
} else {
|
||||
*dma_handle = phys_to_dma(dev, page_to_phys(page));
|
||||
}
|
||||
memset(ret, 0, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* NOTE: this function must never look at the dma_addr argument, because we want
|
||||
* to be able to use it as a helper for iommu implementations as well.
|
||||
*/
|
||||
void dma_direct_free(struct device *dev, size_t size, void *cpu_addr,
|
||||
dma_addr_t dma_addr, unsigned long attrs)
|
||||
{
|
||||
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
||||
unsigned int page_order = get_order(size);
|
||||
|
||||
if (force_dma_unencrypted())
|
||||
set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order);
|
||||
if (!dma_release_from_contiguous(dev, virt_to_page(cpu_addr), count))
|
||||
free_pages((unsigned long)cpu_addr, page_order);
|
||||
}
|
||||
|
||||
dma_addr_t dma_direct_map_page(struct device *dev, struct page *page,
|
||||
unsigned long offset, size_t size, enum dma_data_direction dir,
|
||||
unsigned long attrs)
|
||||
{
|
||||
dma_addr_t dma_addr = phys_to_dma(dev, page_to_phys(page)) + offset;
|
||||
|
||||
if (!check_addr(dev, dma_addr, size, __func__))
|
||||
return DIRECT_MAPPING_ERROR;
|
||||
return dma_addr;
|
||||
}
|
||||
|
||||
int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
|
||||
enum dma_data_direction dir, unsigned long attrs)
|
||||
{
|
||||
int i;
|
||||
struct scatterlist *sg;
|
||||
|
||||
for_each_sg(sgl, sg, nents, i) {
|
||||
BUG_ON(!sg_page(sg));
|
||||
|
||||
sg_dma_address(sg) = phys_to_dma(dev, sg_phys(sg));
|
||||
if (!check_addr(dev, sg_dma_address(sg), sg->length, __func__))
|
||||
return 0;
|
||||
sg_dma_len(sg) = sg->length;
|
||||
}
|
||||
|
||||
return nents;
|
||||
}
|
||||
|
||||
int dma_direct_supported(struct device *dev, u64 mask)
|
||||
{
|
||||
#ifdef CONFIG_ZONE_DMA
|
||||
if (mask < DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
|
||||
return 0;
|
||||
#else
|
||||
/*
|
||||
* Because 32-bit DMA masks are so common we expect every architecture
|
||||
* to be able to satisfy them - either by not supporting more physical
|
||||
* memory, or by providing a ZONE_DMA32. If neither is the case, the
|
||||
* architecture needs to use an IOMMU instead of the direct mapping.
|
||||
*/
|
||||
if (mask < DMA_BIT_MASK(32))
|
||||
return 0;
|
||||
#endif
|
||||
/*
|
||||
* Various PCI/PCIe bridges have broken support for > 32bit DMA even
|
||||
* if the device itself might support it.
|
||||
*/
|
||||
if (dev->dma_32bit_limit && mask > DMA_BIT_MASK(32))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dma_direct_mapping_error(struct device *dev, dma_addr_t dma_addr)
|
||||
{
|
||||
return dma_addr == DIRECT_MAPPING_ERROR;
|
||||
}
|
||||
|
||||
const struct dma_map_ops dma_direct_ops = {
|
||||
.alloc = dma_direct_alloc,
|
||||
.free = dma_direct_free,
|
||||
.map_page = dma_direct_map_page,
|
||||
.map_sg = dma_direct_map_sg,
|
||||
.dma_supported = dma_direct_supported,
|
||||
.mapping_error = dma_direct_mapping_error,
|
||||
};
|
||||
EXPORT_SYMBOL(dma_direct_ops);
|
345
kernel/dma/mapping.c
Normal file
345
kernel/dma/mapping.c
Normal file
@@ -0,0 +1,345 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* arch-independent dma-mapping routines
|
||||
*
|
||||
* Copyright (c) 2006 SUSE Linux Products GmbH
|
||||
* Copyright (c) 2006 Tejun Heo <teheo@suse.de>
|
||||
*/
|
||||
|
||||
#include <linux/acpi.h>
|
||||
#include <linux/dma-mapping.h>
|
||||
#include <linux/export.h>
|
||||
#include <linux/gfp.h>
|
||||
#include <linux/of_device.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/vmalloc.h>
|
||||
|
||||
/*
|
||||
* Managed DMA API
|
||||
*/
|
||||
struct dma_devres {
|
||||
size_t size;
|
||||
void *vaddr;
|
||||
dma_addr_t dma_handle;
|
||||
unsigned long attrs;
|
||||
};
|
||||
|
||||
static void dmam_release(struct device *dev, void *res)
|
||||
{
|
||||
struct dma_devres *this = res;
|
||||
|
||||
dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
|
||||
this->attrs);
|
||||
}
|
||||
|
||||
static int dmam_match(struct device *dev, void *res, void *match_data)
|
||||
{
|
||||
struct dma_devres *this = res, *match = match_data;
|
||||
|
||||
if (this->vaddr == match->vaddr) {
|
||||
WARN_ON(this->size != match->size ||
|
||||
this->dma_handle != match->dma_handle);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* dmam_alloc_coherent - Managed dma_alloc_coherent()
|
||||
* @dev: Device to allocate coherent memory for
|
||||
* @size: Size of allocation
|
||||
* @dma_handle: Out argument for allocated DMA handle
|
||||
* @gfp: Allocation flags
|
||||
*
|
||||
* Managed dma_alloc_coherent(). Memory allocated using this function
|
||||
* will be automatically released on driver detach.
|
||||
*
|
||||
* RETURNS:
|
||||
* Pointer to allocated memory on success, NULL on failure.
|
||||
*/
|
||||
void *dmam_alloc_coherent(struct device *dev, size_t size,
|
||||
dma_addr_t *dma_handle, gfp_t gfp)
|
||||
{
|
||||
struct dma_devres *dr;
|
||||
void *vaddr;
|
||||
|
||||
dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
|
||||
if (!dr)
|
||||
return NULL;
|
||||
|
||||
vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
|
||||
if (!vaddr) {
|
||||
devres_free(dr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dr->vaddr = vaddr;
|
||||
dr->dma_handle = *dma_handle;
|
||||
dr->size = size;
|
||||
|
||||
devres_add(dev, dr);
|
||||
|
||||
return vaddr;
|
||||
}
|
||||
EXPORT_SYMBOL(dmam_alloc_coherent);
|
||||
|
||||
/**
|
||||
* dmam_free_coherent - Managed dma_free_coherent()
|
||||
* @dev: Device to free coherent memory for
|
||||
* @size: Size of allocation
|
||||
* @vaddr: Virtual address of the memory to free
|
||||
* @dma_handle: DMA handle of the memory to free
|
||||
*
|
||||
* Managed dma_free_coherent().
|
||||
*/
|
||||
void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
|
||||
dma_addr_t dma_handle)
|
||||
{
|
||||
struct dma_devres match_data = { size, vaddr, dma_handle };
|
||||
|
||||
dma_free_coherent(dev, size, vaddr, dma_handle);
|
||||
WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
|
||||
}
|
||||
EXPORT_SYMBOL(dmam_free_coherent);
|
||||
|
||||
/**
|
||||
* dmam_alloc_attrs - Managed dma_alloc_attrs()
|
||||
* @dev: Device to allocate non_coherent memory for
|
||||
* @size: Size of allocation
|
||||
* @dma_handle: Out argument for allocated DMA handle
|
||||
* @gfp: Allocation flags
|
||||
* @attrs: Flags in the DMA_ATTR_* namespace.
|
||||
*
|
||||
* Managed dma_alloc_attrs(). Memory allocated using this function will be
|
||||
* automatically released on driver detach.
|
||||
*
|
||||
* RETURNS:
|
||||
* Pointer to allocated memory on success, NULL on failure.
|
||||
*/
|
||||
void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
||||
gfp_t gfp, unsigned long attrs)
|
||||
{
|
||||
struct dma_devres *dr;
|
||||
void *vaddr;
|
||||
|
||||
dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
|
||||
if (!dr)
|
||||
return NULL;
|
||||
|
||||
vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
|
||||
if (!vaddr) {
|
||||
devres_free(dr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dr->vaddr = vaddr;
|
||||
dr->dma_handle = *dma_handle;
|
||||
dr->size = size;
|
||||
dr->attrs = attrs;
|
||||
|
||||
devres_add(dev, dr);
|
||||
|
||||
return vaddr;
|
||||
}
|
||||
EXPORT_SYMBOL(dmam_alloc_attrs);
|
||||
|
||||
#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
|
||||
|
||||
static void dmam_coherent_decl_release(struct device *dev, void *res)
|
||||
{
|
||||
dma_release_declared_memory(dev);
|
||||
}
|
||||
|
||||
/**
|
||||
* dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
|
||||
* @dev: Device to declare coherent memory for
|
||||
* @phys_addr: Physical address of coherent memory to be declared
|
||||
* @device_addr: Device address of coherent memory to be declared
|
||||
* @size: Size of coherent memory to be declared
|
||||
* @flags: Flags
|
||||
*
|
||||
* Managed dma_declare_coherent_memory().
|
||||
*
|
||||
* RETURNS:
|
||||
* 0 on success, -errno on failure.
|
||||
*/
|
||||
int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
|
||||
dma_addr_t device_addr, size_t size, int flags)
|
||||
{
|
||||
void *res;
|
||||
int rc;
|
||||
|
||||
res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
|
||||
if (!res)
|
||||
return -ENOMEM;
|
||||
|
||||
rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
|
||||
flags);
|
||||
if (!rc)
|
||||
devres_add(dev, res);
|
||||
else
|
||||
devres_free(res);
|
||||
|
||||
return rc;
|
||||
}
|
||||
EXPORT_SYMBOL(dmam_declare_coherent_memory);
|
||||
|
||||
/**
|
||||
* dmam_release_declared_memory - Managed dma_release_declared_memory().
|
||||
* @dev: Device to release declared coherent memory for
|
||||
*
|
||||
* Managed dmam_release_declared_memory().
|
||||
*/
|
||||
void dmam_release_declared_memory(struct device *dev)
|
||||
{
|
||||
WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
|
||||
}
|
||||
EXPORT_SYMBOL(dmam_release_declared_memory);
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Create scatter-list for the already allocated DMA buffer.
|
||||
*/
|
||||
int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
|
||||
void *cpu_addr, dma_addr_t handle, size_t size)
|
||||
{
|
||||
struct page *page = virt_to_page(cpu_addr);
|
||||
int ret;
|
||||
|
||||
ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
|
||||
if (unlikely(ret))
|
||||
return ret;
|
||||
|
||||
sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(dma_common_get_sgtable);
|
||||
|
||||
/*
|
||||
* Create userspace mapping for the DMA-coherent memory.
|
||||
*/
|
||||
int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
|
||||
void *cpu_addr, dma_addr_t dma_addr, size_t size)
|
||||
{
|
||||
int ret = -ENXIO;
|
||||
#ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP
|
||||
unsigned long user_count = vma_pages(vma);
|
||||
unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
||||
unsigned long off = vma->vm_pgoff;
|
||||
|
||||
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
|
||||
|
||||
if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
|
||||
return ret;
|
||||
|
||||
if (off < count && user_count <= (count - off))
|
||||
ret = remap_pfn_range(vma, vma->vm_start,
|
||||
page_to_pfn(virt_to_page(cpu_addr)) + off,
|
||||
user_count << PAGE_SHIFT,
|
||||
vma->vm_page_prot);
|
||||
#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
|
||||
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(dma_common_mmap);
|
||||
|
||||
#ifdef CONFIG_MMU
|
||||
static struct vm_struct *__dma_common_pages_remap(struct page **pages,
|
||||
size_t size, unsigned long vm_flags, pgprot_t prot,
|
||||
const void *caller)
|
||||
{
|
||||
struct vm_struct *area;
|
||||
|
||||
area = get_vm_area_caller(size, vm_flags, caller);
|
||||
if (!area)
|
||||
return NULL;
|
||||
|
||||
if (map_vm_area(area, prot, pages)) {
|
||||
vunmap(area->addr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return area;
|
||||
}
|
||||
|
||||
/*
|
||||
* remaps an array of PAGE_SIZE pages into another vm_area
|
||||
* Cannot be used in non-sleeping contexts
|
||||
*/
|
||||
void *dma_common_pages_remap(struct page **pages, size_t size,
|
||||
unsigned long vm_flags, pgprot_t prot,
|
||||
const void *caller)
|
||||
{
|
||||
struct vm_struct *area;
|
||||
|
||||
area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
|
||||
if (!area)
|
||||
return NULL;
|
||||
|
||||
area->pages = pages;
|
||||
|
||||
return area->addr;
|
||||
}
|
||||
|
||||
/*
|
||||
* remaps an allocated contiguous region into another vm_area.
|
||||
* Cannot be used in non-sleeping contexts
|
||||
*/
|
||||
|
||||
void *dma_common_contiguous_remap(struct page *page, size_t size,
|
||||
unsigned long vm_flags,
|
||||
pgprot_t prot, const void *caller)
|
||||
{
|
||||
int i;
|
||||
struct page **pages;
|
||||
struct vm_struct *area;
|
||||
|
||||
pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
|
||||
if (!pages)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < (size >> PAGE_SHIFT); i++)
|
||||
pages[i] = nth_page(page, i);
|
||||
|
||||
area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
|
||||
|
||||
kfree(pages);
|
||||
|
||||
if (!area)
|
||||
return NULL;
|
||||
return area->addr;
|
||||
}
|
||||
|
||||
/*
|
||||
* unmaps a range previously mapped by dma_common_*_remap
|
||||
*/
|
||||
void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
|
||||
{
|
||||
struct vm_struct *area = find_vm_area(cpu_addr);
|
||||
|
||||
if (!area || (area->flags & vm_flags) != vm_flags) {
|
||||
WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
|
||||
return;
|
||||
}
|
||||
|
||||
unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
|
||||
vunmap(cpu_addr);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* enables DMA API use for a device
|
||||
*/
|
||||
int dma_configure(struct device *dev)
|
||||
{
|
||||
if (dev->bus->dma_configure)
|
||||
return dev->bus->dma_configure(dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dma_deconfigure(struct device *dev)
|
||||
{
|
||||
of_dma_deconfigure(dev);
|
||||
acpi_dma_deconfigure(dev);
|
||||
}
|
102
kernel/dma/noncoherent.c
Normal file
102
kernel/dma/noncoherent.c
Normal file
@@ -0,0 +1,102 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* Copyright (C) 2018 Christoph Hellwig.
|
||||
*
|
||||
* DMA operations that map physical memory directly without providing cache
|
||||
* coherence.
|
||||
*/
|
||||
#include <linux/export.h>
|
||||
#include <linux/mm.h>
|
||||
#include <linux/dma-direct.h>
|
||||
#include <linux/dma-noncoherent.h>
|
||||
#include <linux/scatterlist.h>
|
||||
|
||||
static void dma_noncoherent_sync_single_for_device(struct device *dev,
|
||||
dma_addr_t addr, size_t size, enum dma_data_direction dir)
|
||||
{
|
||||
arch_sync_dma_for_device(dev, dma_to_phys(dev, addr), size, dir);
|
||||
}
|
||||
|
||||
static void dma_noncoherent_sync_sg_for_device(struct device *dev,
|
||||
struct scatterlist *sgl, int nents, enum dma_data_direction dir)
|
||||
{
|
||||
struct scatterlist *sg;
|
||||
int i;
|
||||
|
||||
for_each_sg(sgl, sg, nents, i)
|
||||
arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
|
||||
}
|
||||
|
||||
static dma_addr_t dma_noncoherent_map_page(struct device *dev, struct page *page,
|
||||
unsigned long offset, size_t size, enum dma_data_direction dir,
|
||||
unsigned long attrs)
|
||||
{
|
||||
dma_addr_t addr;
|
||||
|
||||
addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
|
||||
if (!dma_mapping_error(dev, addr) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
|
||||
arch_sync_dma_for_device(dev, page_to_phys(page) + offset,
|
||||
size, dir);
|
||||
return addr;
|
||||
}
|
||||
|
||||
static int dma_noncoherent_map_sg(struct device *dev, struct scatterlist *sgl,
|
||||
int nents, enum dma_data_direction dir, unsigned long attrs)
|
||||
{
|
||||
nents = dma_direct_map_sg(dev, sgl, nents, dir, attrs);
|
||||
if (nents > 0 && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
|
||||
dma_noncoherent_sync_sg_for_device(dev, sgl, nents, dir);
|
||||
return nents;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
|
||||
static void dma_noncoherent_sync_single_for_cpu(struct device *dev,
|
||||
dma_addr_t addr, size_t size, enum dma_data_direction dir)
|
||||
{
|
||||
arch_sync_dma_for_cpu(dev, dma_to_phys(dev, addr), size, dir);
|
||||
}
|
||||
|
||||
static void dma_noncoherent_sync_sg_for_cpu(struct device *dev,
|
||||
struct scatterlist *sgl, int nents, enum dma_data_direction dir)
|
||||
{
|
||||
struct scatterlist *sg;
|
||||
int i;
|
||||
|
||||
for_each_sg(sgl, sg, nents, i)
|
||||
arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
|
||||
}
|
||||
|
||||
static void dma_noncoherent_unmap_page(struct device *dev, dma_addr_t addr,
|
||||
size_t size, enum dma_data_direction dir, unsigned long attrs)
|
||||
{
|
||||
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
|
||||
dma_noncoherent_sync_single_for_cpu(dev, addr, size, dir);
|
||||
}
|
||||
|
||||
static void dma_noncoherent_unmap_sg(struct device *dev, struct scatterlist *sgl,
|
||||
int nents, enum dma_data_direction dir, unsigned long attrs)
|
||||
{
|
||||
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
|
||||
dma_noncoherent_sync_sg_for_cpu(dev, sgl, nents, dir);
|
||||
}
|
||||
#endif
|
||||
|
||||
const struct dma_map_ops dma_noncoherent_ops = {
|
||||
.alloc = arch_dma_alloc,
|
||||
.free = arch_dma_free,
|
||||
.mmap = arch_dma_mmap,
|
||||
.sync_single_for_device = dma_noncoherent_sync_single_for_device,
|
||||
.sync_sg_for_device = dma_noncoherent_sync_sg_for_device,
|
||||
.map_page = dma_noncoherent_map_page,
|
||||
.map_sg = dma_noncoherent_map_sg,
|
||||
#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
|
||||
.sync_single_for_cpu = dma_noncoherent_sync_single_for_cpu,
|
||||
.sync_sg_for_cpu = dma_noncoherent_sync_sg_for_cpu,
|
||||
.unmap_page = dma_noncoherent_unmap_page,
|
||||
.unmap_sg = dma_noncoherent_unmap_sg,
|
||||
#endif
|
||||
.dma_supported = dma_direct_supported,
|
||||
.mapping_error = dma_direct_mapping_error,
|
||||
.cache_sync = arch_dma_cache_sync,
|
||||
};
|
||||
EXPORT_SYMBOL(dma_noncoherent_ops);
|
1087
kernel/dma/swiotlb.c
Normal file
1087
kernel/dma/swiotlb.c
Normal file
File diff suppressed because it is too large
Load Diff
59
kernel/dma/virt.c
Normal file
59
kernel/dma/virt.c
Normal file
@@ -0,0 +1,59 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* DMA operations that map to virtual addresses without flushing memory.
|
||||
*/
|
||||
#include <linux/export.h>
|
||||
#include <linux/mm.h>
|
||||
#include <linux/dma-mapping.h>
|
||||
#include <linux/scatterlist.h>
|
||||
|
||||
static void *dma_virt_alloc(struct device *dev, size_t size,
|
||||
dma_addr_t *dma_handle, gfp_t gfp,
|
||||
unsigned long attrs)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
ret = (void *)__get_free_pages(gfp, get_order(size));
|
||||
if (ret)
|
||||
*dma_handle = (uintptr_t)ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void dma_virt_free(struct device *dev, size_t size,
|
||||
void *cpu_addr, dma_addr_t dma_addr,
|
||||
unsigned long attrs)
|
||||
{
|
||||
free_pages((unsigned long)cpu_addr, get_order(size));
|
||||
}
|
||||
|
||||
static dma_addr_t dma_virt_map_page(struct device *dev, struct page *page,
|
||||
unsigned long offset, size_t size,
|
||||
enum dma_data_direction dir,
|
||||
unsigned long attrs)
|
||||
{
|
||||
return (uintptr_t)(page_address(page) + offset);
|
||||
}
|
||||
|
||||
static int dma_virt_map_sg(struct device *dev, struct scatterlist *sgl,
|
||||
int nents, enum dma_data_direction dir,
|
||||
unsigned long attrs)
|
||||
{
|
||||
int i;
|
||||
struct scatterlist *sg;
|
||||
|
||||
for_each_sg(sgl, sg, nents, i) {
|
||||
BUG_ON(!sg_page(sg));
|
||||
sg_dma_address(sg) = (uintptr_t)sg_virt(sg);
|
||||
sg_dma_len(sg) = sg->length;
|
||||
}
|
||||
|
||||
return nents;
|
||||
}
|
||||
|
||||
const struct dma_map_ops dma_virt_ops = {
|
||||
.alloc = dma_virt_alloc,
|
||||
.free = dma_virt_free,
|
||||
.map_page = dma_virt_map_page,
|
||||
.map_sg = dma_virt_map_sg,
|
||||
};
|
||||
EXPORT_SYMBOL(dma_virt_ops);
|
Reference in New Issue
Block a user