1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Early setup for Rockchip DMA CMA
4 *
5 * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
6 * Author: Simon Xue <xxm@rock-chips.com>
7 */
8
9 #include <linux/cma.h>
10 #include <linux/dma-map-ops.h>
11
12 #include "rk-dma-heap.h"
13
14 #define RK_DMA_HEAP_CMA_DEFAULT_SIZE SZ_32M
15
16 static unsigned long rk_dma_heap_size __initdata;
17 static unsigned long rk_dma_heap_base __initdata;
18
19 static struct cma *rk_dma_heap_cma;
20
early_dma_heap_cma(char * p)21 static int __init early_dma_heap_cma(char *p)
22 {
23 if (!p) {
24 pr_err("Config string not provided\n");
25 return -EINVAL;
26 }
27
28 rk_dma_heap_size = memparse(p, &p);
29 if (*p != '@')
30 return 0;
31
32 rk_dma_heap_base = memparse(p + 1, &p);
33
34 return 0;
35 }
36 early_param("rk_dma_heap_cma", early_dma_heap_cma);
37
38 #ifndef CONFIG_DMA_CMA
39 void __weak
dma_contiguous_early_fixup(phys_addr_t base,unsigned long size)40 dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
41 {
42 }
43 #endif
44
rk_dma_heap_cma_setup(void)45 int __init rk_dma_heap_cma_setup(void)
46 {
47 unsigned long size;
48 int ret;
49 bool fix = false;
50
51 if (rk_dma_heap_size)
52 size = rk_dma_heap_size;
53 else
54 size = RK_DMA_HEAP_CMA_DEFAULT_SIZE;
55
56 if (rk_dma_heap_base)
57 fix = true;
58
59 ret = cma_declare_contiguous(rk_dma_heap_base, PAGE_ALIGN(size), 0x0,
60 PAGE_SIZE, 0, fix, "rk-dma-heap-cma",
61 &rk_dma_heap_cma);
62 if (ret)
63 return ret;
64
65 #if !IS_ENABLED(CONFIG_CMA_INACTIVE)
66 /* Architecture specific contiguous memory fixup. */
67 dma_contiguous_early_fixup(cma_get_base(rk_dma_heap_cma),
68 cma_get_size(rk_dma_heap_cma));
69 #endif
70
71 return 0;
72 }
73
rk_dma_heap_get_cma(void)74 struct cma *rk_dma_heap_get_cma(void)
75 {
76 return rk_dma_heap_cma;
77 }
78