Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Documentation/userspace-api/dma-buf-heaps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ following heaps:
usually created either through the kernel commandline through the
``cma`` parameter, a memory region Device-Tree node with the
``linux,cma-default`` property set, or through the ``CMA_SIZE_MBYTES`` or
``CMA_SIZE_PERCENTAGE`` Kconfig options. The heap's name in devtmpfs is
``default_cma_region``. For backwards compatibility, when the
``DMABUF_HEAPS_CMA_LEGACY`` Kconfig option is set, a duplicate node is
created following legacy naming conventions; the legacy name might be
``reserved``, ``linux,cma``, or ``default-pool``.
``CMA_SIZE_PERCENTAGE`` Kconfig options. Prior
to Linux 6.17, its name wasn't stable and could be called
``reserved``, ``linux,cma``, or ``default-pool``, depending on the
platform.

- A heap will be created for each reusable region in the device tree
with the ``shared-dma-pool`` compatible, using the full device tree
node name as its name. The buffer semantics are identical to
``default-cma-region``.
1 change: 1 addition & 0 deletions arch/arm64/configs/qcom.config
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CONFIG_CPU_IDLE_THERMAL=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_DEBUG_INFO_BTF_MODULES=y
# CONFIG_MODULE_ALLOW_BTF_MISMATCH is not set
CONFIG_DMA_CMA=y
CONFIG_DMABUF_HEAPS=y
CONFIG_DMABUF_HEAPS_CMA=y
CONFIG_DMABUF_HEAPS_SYSTEM=y
Expand Down
10 changes: 0 additions & 10 deletions drivers/dma-buf/heaps/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,3 @@ config DMABUF_HEAPS_CMA
Choose this option to enable dma-buf CMA heap. This heap is backed
by the Contiguous Memory Allocator (CMA). If your system has these
regions, you should say Y here.

config DMABUF_HEAPS_CMA_LEGACY
bool "Legacy DMA-BUF CMA Heap"
default y
depends on DMABUF_HEAPS_CMA
help
Add a duplicate CMA-backed dma-buf heap with legacy naming derived
from the CMA area's devicetree node, or "reserved" if the area is not
defined in the devicetree. This uses the same underlying allocator as
CONFIG_DMABUF_HEAPS_CMA.
31 changes: 12 additions & 19 deletions drivers/dma-buf/heaps/cma_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,33 +395,26 @@ static int __init __add_cma_heap(struct cma *cma, const char *name)
return 0;
}

static int __init add_default_cma_heap(void)
static int __init add_cma_heaps(void)
{
struct cma *default_cma = dev_get_cma_area(NULL);
const char *legacy_cma_name;
struct cma *cma;
unsigned int i;
int ret;

if (!default_cma)
return 0;

ret = __add_cma_heap(default_cma, DEFAULT_CMA_NAME);
if (ret)
return ret;

if (IS_ENABLED(CONFIG_DMABUF_HEAPS_CMA_LEGACY)) {
legacy_cma_name = cma_get_name(default_cma);
if (!strcmp(legacy_cma_name, DEFAULT_CMA_NAME)) {
pr_warn("legacy name and default name are the same, skipping legacy heap\n");
return 0;
}
if (default_cma) {
ret = __add_cma_heap(default_cma, DEFAULT_CMA_NAME);
if (ret)
return ret;
}

ret = __add_cma_heap(default_cma, legacy_cma_name);
for (i = 0; (cma = dma_contiguous_get_area_by_idx(i)) != NULL; i++) {
ret = __add_cma_heap(cma, cma_get_name(cma));
if (ret)
pr_warn("failed to add legacy heap: %pe\n",
ERR_PTR(ret));
pr_warn("Failed to add CMA heap %s", cma_get_name(cma));
}

return 0;
}
module_init(add_default_cma_heap);
module_init(add_cma_heaps);
MODULE_DESCRIPTION("DMA-BUF CMA Heap");
6 changes: 6 additions & 0 deletions include/linux/dma-map-ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ static inline void set_dma_ops(struct device *dev,

#ifdef CONFIG_DMA_CMA
extern struct cma *dma_contiguous_default_area;
struct cma *dma_contiguous_get_area_by_idx(unsigned int idx);

static inline struct cma *dev_get_cma_area(struct device *dev)
{
Expand Down Expand Up @@ -153,6 +154,11 @@ static inline void dma_free_contiguous(struct device *dev, struct page *page,
{
__free_pages(page, get_order(size));
}

static inline struct cma *dma_contiguous_get_area_by_idx(unsigned int idx)
{
return NULL;
}
#endif /* CONFIG_DMA_CMA*/

#ifdef CONFIG_DMA_DECLARE_COHERENT
Expand Down
41 changes: 37 additions & 4 deletions kernel/dma/contiguous.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@
#endif

struct cma *dma_contiguous_default_area;
static struct cma *dma_contiguous_areas[MAX_CMA_AREAS];
static unsigned int dma_contiguous_areas_num;

static int dma_contiguous_insert_area(struct cma *cma)
{
if (dma_contiguous_areas_num >= ARRAY_SIZE(dma_contiguous_areas))
return -EINVAL;

dma_contiguous_areas[dma_contiguous_areas_num++] = cma;

return 0;
}

struct cma *dma_contiguous_get_area_by_idx(unsigned int idx)
{
if (idx >= dma_contiguous_areas_num)
return NULL;

return dma_contiguous_areas[idx];
}
EXPORT_SYMBOL_GPL(dma_contiguous_get_area_by_idx);

/*
* Default global CMA area size can be defined in kernel's .config.
Expand Down Expand Up @@ -241,13 +262,21 @@ void __init dma_contiguous_reserve(phys_addr_t limit)
}

if (selected_size && !dma_contiguous_default_area) {
int ret;

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);
ret = dma_contiguous_reserve_area(selected_size, selected_base,
selected_limit,
&dma_contiguous_default_area,
fixed);
if (ret)
return;

ret = dma_contiguous_insert_area(dma_contiguous_default_area);
if (ret)
pr_warn("Couldn't queue default CMA region for heap creation.\n");
}
}

Expand Down Expand Up @@ -520,6 +549,10 @@ static int __init rmem_cma_setup(unsigned long node, struct reserved_mem *rmem)
pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
&rmem->base, (unsigned long)rmem->size / SZ_1M);

ret = dma_contiguous_insert_area(cma);
if (ret)
pr_warn("Couldn't store CMA reserved area.\n");

return 0;
}

Expand Down
Loading