xref: /rk3399_rockchip-uboot/include/sysmem.h (revision 14569d26731af93622d8fcf87429b06ece85047b)
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_can_alloc() - Check if the region can be allocated
110  *
111  * @base: region base
112  * @size: region size
113  *
114  * @return true on okay.
115  */
116 bool sysmem_can_alloc(phys_size_t base, phys_size_t size);
117 
118 /**
119  * sysmem_free() - Free allocated sysmem region
120  *
121  * @base: region base
122  *
123  * @return 0 on success, otherwise error
124  */
125 int sysmem_free(phys_addr_t base);
126 
127 /**
128  * sysmem_dump() - Dump all sysmem region state
129  */
130 void sysmem_dump(void);
131 
132 /**
133  * sysmem_overflow_check() - Sysmem regions overflow check
134  */
135 void sysmem_overflow_check(void);
136 
137 /**
138  * board_sysmem_reserve() - Weak function for board to implement
139  *
140  * @sysmem: global sysmem point, ignored
141  *
142  * @return 0 on success, otherwise error
143  */
144 int board_sysmem_reserve(struct sysmem *sysmem);
145 #else
146 static inline bool sysmem_has_init(void) { return false; }
147 static inline int sysmem_init(void) { return 0; }
148 static inline int sysmem_initr(void) { return 0; }
149 static inline int sysmem_free(phys_addr_t base) { return 0; }
150 static inline void sysmem_dump(void) {}
151 void sysmem_overflow_check(void) {}
152 
153 __weak int board_sysmem_reserve(struct sysmem *sysmem) { return 0; }
154 
155 static inline void *sysmem_alloc(enum memblk_id id, phys_size_t size)
156 {
157 	return malloc(size);
158 }
159 
160 static inline bool sysmem_can_alloc(phys_size_t base, phys_size_t size)
161 {
162 	return true;
163 }
164 
165 static inline void *sysmem_alloc_base(enum memblk_id id,
166 				      phys_addr_t base, phys_size_t size)
167 {
168 	return (void *)base;
169 }
170 static inline void *sysmem_alloc_base_by_name(const char *name,
171 					      phys_addr_t base,
172 					      phys_size_t size)
173 {
174 	return (void *)base;
175 }
176 static inline void *sysmem_fdt_reserve_alloc_base(const char *name,
177 						  phys_addr_t base,
178 						  phys_size_t size)
179 {
180 	return (void *)base;
181 }
182 #endif /* CONFIG_SYSMEM */
183 #endif /* _SYSMEM_H */
184