Skip to content

Commit de9e14e

Browse files
mszyprowtorvalds
authored andcommitted
drivers: dma-contiguous: add initialization from device tree
Add a function to create CMA region from previously reserved memory and add support for handling 'shared-dma-pool' reserved-memory device tree nodes. Based on previous code provided by Josh Cartwright <joshc@codeaurora.org> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Michal Nazarewicz <mina86@mina86.com> Cc: Grant Likely <grant.likely@linaro.org> Cc: Laura Abbott <lauraa@codeaurora.org> Cc: Josh Cartwright <joshc@codeaurora.org> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Kyungmin Park <kyungmin.park@samsung.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7bfa5ab commit de9e14e

3 files changed

Lines changed: 120 additions & 11 deletions

File tree

‎drivers/base/dma-contiguous.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,69 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
211211
{
212212
return cma_release(dev_get_cma_area(dev), pages, count);
213213
}
214+
215+
/*
216+
* Support for reserved memory regions defined in device tree
217+
*/
218+
#ifdef CONFIG_OF_RESERVED_MEM
219+
#include <linux/of.h>
220+
#include <linux/of_fdt.h>
221+
#include <linux/of_reserved_mem.h>
222+
223+
#undef pr_fmt
224+
#define pr_fmt(fmt) fmt
225+
226+
static void rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev)
227+
{
228+
dev_set_cma_area(dev, rmem->priv);
229+
}
230+
231+
static void rmem_cma_device_release(struct reserved_mem *rmem,
232+
struct device *dev)
233+
{
234+
dev_set_cma_area(dev, NULL);
235+
}
236+
237+
static const struct reserved_mem_ops rmem_cma_ops = {
238+
.device_init = rmem_cma_device_init,
239+
.device_release = rmem_cma_device_release,
240+
};
241+
242+
static int __init rmem_cma_setup(struct reserved_mem *rmem)
243+
{
244+
phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
245+
phys_addr_t mask = align - 1;
246+
unsigned long node = rmem->fdt_node;
247+
struct cma *cma;
248+
int err;
249+
250+
if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
251+
of_get_flat_dt_prop(node, "no-map", NULL))
252+
return -EINVAL;
253+
254+
if ((rmem->base & mask) || (rmem->size & mask)) {
255+
pr_err("Reserved memory: incorrect alignment of CMA region\n");
256+
return -EINVAL;
257+
}
258+
259+
err = cma_init_reserved_mem(rmem->base, rmem->size, 0, &cma);
260+
if (err) {
261+
pr_err("Reserved memory: unable to setup CMA region\n");
262+
return err;
263+
}
264+
/* Architecture specific contiguous memory fixup. */
265+
dma_contiguous_early_fixup(rmem->base, rmem->size);
266+
267+
if (of_get_flat_dt_prop(node, "linux,cma-default", NULL))
268+
dma_contiguous_set_default(cma);
269+
270+
rmem->ops = &rmem_cma_ops;
271+
rmem->priv = cma;
272+
273+
pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
274+
&rmem->base, (unsigned long)rmem->size / SZ_1M);
275+
276+
return 0;
277+
}
278+
RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
279+
#endif

‎include/linux/cma.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ extern int __init cma_declare_contiguous(phys_addr_t size,
2222
phys_addr_t base, phys_addr_t limit,
2323
phys_addr_t alignment, unsigned int order_per_bit,
2424
bool fixed, struct cma **res_cma);
25+
extern int cma_init_reserved_mem(phys_addr_t size,
26+
phys_addr_t base, int order_per_bit,
27+
struct cma **res_cma);
2528
extern struct page *cma_alloc(struct cma *cma, int count, unsigned int align);
2629
extern bool cma_release(struct cma *cma, struct page *pages, int count);
2730
#endif

‎mm/cma.c‎

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,54 @@ static int __init cma_init_reserved_areas(void)
142142
}
143143
core_initcall(cma_init_reserved_areas);
144144

145+
/**
146+
* cma_init_reserved_mem() - create custom contiguous area from reserved memory
147+
* @base: Base address of the reserved area
148+
* @size: Size of the reserved area (in bytes),
149+
* @order_per_bit: Order of pages represented by one bit on bitmap.
150+
* @res_cma: Pointer to store the created cma region.
151+
*
152+
* This function creates custom contiguous area from already reserved memory.
153+
*/
154+
int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
155+
int order_per_bit, struct cma **res_cma)
156+
{
157+
struct cma *cma;
158+
phys_addr_t alignment;
159+
160+
/* Sanity checks */
161+
if (cma_area_count == ARRAY_SIZE(cma_areas)) {
162+
pr_err("Not enough slots for CMA reserved regions!\n");
163+
return -ENOSPC;
164+
}
165+
166+
if (!size || !memblock_is_region_reserved(base, size))
167+
return -EINVAL;
168+
169+
/* ensure minimal alignment requied by mm core */
170+
alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
171+
172+
/* alignment should be aligned with order_per_bit */
173+
if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
174+
return -EINVAL;
175+
176+
if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
177+
return -EINVAL;
178+
179+
/*
180+
* Each reserved area must be initialised later, when more kernel
181+
* subsystems (like slab allocator) are available.
182+
*/
183+
cma = &cma_areas[cma_area_count];
184+
cma->base_pfn = PFN_DOWN(base);
185+
cma->count = size >> PAGE_SHIFT;
186+
cma->order_per_bit = order_per_bit;
187+
*res_cma = cma;
188+
cma_area_count++;
189+
190+
return 0;
191+
}
192+
145193
/**
146194
* cma_declare_contiguous() - reserve custom contiguous area
147195
* @base: Base address of the reserved area optional, use 0 for any
@@ -165,7 +213,6 @@ int __init cma_declare_contiguous(phys_addr_t base,
165213
phys_addr_t alignment, unsigned int order_per_bit,
166214
bool fixed, struct cma **res_cma)
167215
{
168-
struct cma *cma;
169216
phys_addr_t memblock_end = memblock_end_of_DRAM();
170217
phys_addr_t highmem_start = __pa(high_memory);
171218
int ret = 0;
@@ -237,16 +284,9 @@ int __init cma_declare_contiguous(phys_addr_t base,
237284
}
238285
}
239286

240-
/*
241-
* Each reserved area must be initialised later, when more kernel
242-
* subsystems (like slab allocator) are available.
243-
*/
244-
cma = &cma_areas[cma_area_count];
245-
cma->base_pfn = PFN_DOWN(base);
246-
cma->count = size >> PAGE_SHIFT;
247-
cma->order_per_bit = order_per_bit;
248-
*res_cma = cma;
249-
cma_area_count++;
287+
ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma);
288+
if (ret)
289+
goto err;
250290

251291
pr_info("Reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
252292
(unsigned long)base);

0 commit comments

Comments
 (0)