1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
4 */
5
6 #ifndef _SYSMEM_H
7 #define _SYSMEM_H
8
9 #include <memblk.h>
10 #include <malloc.h>
11
12 /*
13 * CONFIG_SYS_FDT_PAD default value is sync with bootm framework in:
14 * common/image-fdt.c
15 */
16 #ifndef CONFIG_SYS_FDT_PAD
17 #define CONFIG_SYS_FDT_PAD 0x3000
18 #endif
19
20 struct sysmem {
21 struct lmb lmb;
22 struct list_head allocated_head;
23 struct list_head kmem_resv_head;
24 ulong allocated_cnt;
25 ulong kmem_resv_cnt;
26 bool has_initf;
27 bool has_initr;
28 };
29
30 #ifdef CONFIG_SYSMEM
31 /**
32 * sysmem_has_init() - Is sysmem initialized
33 *
34 * @return true or false
35 */
36 bool sysmem_has_init(void);
37
38 /**
39 * sysmem_init() - Sysmem initialization
40 *
41 * @return 0 on success, otherwise error
42 */
43 int sysmem_init(void);
44
45 /**
46 * sysmem_initr() - Sysmem initialization after relocation
47 *
48 * @return 0 on success, otherwise error
49 */
50 int sysmem_initr(void);
51
52 /**
53 * sysmem_alloc() - Alloc sysmem region at anywhere
54 *
55 * @id: memblk id
56 * @size: region size
57 *
58 * @return NULL on error, otherwise the allocated region address ptr
59 */
60 void *sysmem_alloc(enum memblk_id id, phys_size_t size);
61
62 /**
63 * sysmem_alloc_by_name() - Alloc sysmem region by name at the expect addr
64 *
65 * @name: memblk name
66 * @size: region size
67 *
68 * @return NULL on error, otherwise the allocated region address ptr
69 */
70 void *sysmem_alloc_by_name(const char *name, phys_size_t size);
71
72 /**
73 * sysmem_alloc_base() - Alloc sysmem region at the expect addr
74 *
75 * @id: memblk id
76 * @base: region base
77 * @size: region size
78 *
79 * @return NULL on error, otherwise the allocated region address ptr
80 */
81 void *sysmem_alloc_base(enum memblk_id id, phys_addr_t base, phys_size_t size);
82
83 /**
84 * sysmem_alloc_base_by_name() - Alloc sysmem region at the expect addr by name
85 *
86 * @name: memblk name
87 * @base: region base
88 * @size: region size
89 *
90 * @return NULL on error, otherwise the allocated region address ptr
91 */
92 void *sysmem_alloc_base_by_name(const char *name,
93 phys_addr_t base, phys_size_t size);
94
95 /**
96 * sysmem_fdt_reserve_alloc_base() - Alloc sysmem region at the expect addr by name,
97 * called only for reserve memory from fdt.
98 *
99 * @name: memblk name
100 * @base: region base
101 * @size: region size
102 *
103 * @return NULL on error, otherwise the allocated region address ptr
104 */
105 void *sysmem_fdt_reserve_alloc_base(const char *name,
106 phys_addr_t base, phys_size_t size);
107
108 /**
109 * sysmem_alloc_temporary_mem() - Alloc a temporary shared memory buffer
110 *
111 * @size: region size
112 *
113 * @return 0 on failed, otherwise allocated addr.
114 */
115 ulong sysmem_alloc_temporary_mem(phys_size_t size);
116
117 /**
118 * sysmem_free() - Free allocated sysmem region
119 *
120 * @base: region base
121 *
122 * @return 0 on success, otherwise error
123 */
124 int sysmem_free(phys_addr_t base);
125
126 /**
127 * sysmem_dump() - Dump all sysmem region state
128 */
129 void sysmem_dump(void);
130
131 /**
132 * sysmem_overflow_check() - Sysmem regions overflow check
133 */
134 void sysmem_overflow_check(void);
135
136 /**
137 * board_sysmem_reserve() - Weak function for board to implement
138 *
139 * @sysmem: global sysmem point, ignored
140 *
141 * @return 0 on success, otherwise error
142 */
143 int board_sysmem_reserve(struct sysmem *sysmem);
144 #else
sysmem_has_init(void)145 static inline bool sysmem_has_init(void) { return false; }
sysmem_init(void)146 static inline int sysmem_init(void) { return 0; }
sysmem_initr(void)147 static inline int sysmem_initr(void) { return 0; }
sysmem_free(phys_addr_t base)148 static inline int sysmem_free(phys_addr_t base) { return 0; }
sysmem_dump(void)149 static inline void sysmem_dump(void) {}
sysmem_overflow_check(void)150 static inline void sysmem_overflow_check(void) {}
board_sysmem_reserve(struct sysmem * sysmem)151 __weak int board_sysmem_reserve(struct sysmem *sysmem) { return 0; }
152
sysmem_alloc(enum memblk_id id,phys_size_t size)153 static inline void *sysmem_alloc(enum memblk_id id, phys_size_t size)
154 {
155 return malloc(size);
156 }
157
sysmem_alloc_temporary_mem(phys_size_t size)158 static inline ulong sysmem_alloc_temporary_mem(phys_size_t size)
159 {
160 return 0;
161 }
162
sysmem_alloc_base(enum memblk_id id,phys_addr_t base,phys_size_t size)163 static inline void *sysmem_alloc_base(enum memblk_id id,
164 phys_addr_t base, phys_size_t size)
165 {
166 return (void *)base;
167 }
sysmem_alloc_base_by_name(const char * name,phys_addr_t base,phys_size_t size)168 static inline void *sysmem_alloc_base_by_name(const char *name,
169 phys_addr_t base,
170 phys_size_t size)
171 {
172 return (void *)base;
173 }
sysmem_fdt_reserve_alloc_base(const char * name,phys_addr_t base,phys_size_t size)174 static inline void *sysmem_fdt_reserve_alloc_base(const char *name,
175 phys_addr_t base,
176 phys_size_t size)
177 {
178 return (void *)base;
179 }
180 #endif /* CONFIG_SYSMEM */
181 #endif /* _SYSMEM_H */
182