xref: /rk3399_rockchip-uboot/include/sysmem.h (revision 8dd9db5d1cd5826638c3cdb5f681300ff2f29f3b)
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 	ulong allocated_cnt;
24 	bool has_initf;
25 	bool has_initr;
26 };
27 
28 #ifdef CONFIG_SYSMEM
29 /**
30  * sysmem_has_init() - Is sysmem initialized
31  *
32  * @return true or false
33  */
34 bool sysmem_has_init(void);
35 
36 /**
37  * sysmem_init() - Sysmem initialization
38  *
39  * @return 0 on success, otherwise error
40  */
41 int sysmem_init(void);
42 
43 /**
44  * sysmem_initr() - Sysmem initialization after relocation
45  *
46  * @return 0 on success, otherwise error
47  */
48 int sysmem_initr(void);
49 
50 /**
51  * sysmem_alloc() - Alloc sysmem region at anywhere
52  *
53  * @id: memblk id
54  * @size: region size
55  *
56  * @return NULL on error, otherwise the allocated region address ptr
57  */
58 void *sysmem_alloc(enum memblk_id id, phys_size_t size);
59 
60 /**
61  * sysmem_alloc_base() - Alloc sysmem region at the expect addr
62  *
63  * @id: memblk id
64  * @base: region base
65  * @size: region size
66  *
67  * @return NULL on error, otherwise the allocated region address ptr
68  */
69 void *sysmem_alloc_base(enum memblk_id id, phys_addr_t base, phys_size_t size);
70 
71 /**
72  * sysmem_alloc_base_by_name() - Alloc sysmem region at the expect addr by name
73  *
74  * @name: memblk name
75  * @base: region base
76  * @size: region size
77  *
78  * @return NULL on error, otherwise the allocated region address ptr
79  */
80 void *sysmem_alloc_base_by_name(const char *name,
81 				phys_addr_t base, phys_size_t size);
82 
83 /**
84  * sysmem_fdt_reserve_alloc_base() - Alloc sysmem region at the expect addr by name,
85  *				     called only for reserve memory from fdt.
86  *
87  * @name: memblk name
88  * @base: region base
89  * @size: region size
90  *
91  * @return NULL on error, otherwise the allocated region address ptr
92  */
93 void *sysmem_fdt_reserve_alloc_base(const char *name,
94 				    phys_addr_t base, phys_size_t size);
95 
96 /**
97  * sysmem_can_alloc() - Check if the region can be allocated
98  *
99  * @base: region base
100  * @size: region size
101  *
102  * @return true on okay.
103  */
104 bool sysmem_can_alloc(phys_size_t base, phys_size_t size);
105 
106 /**
107  * sysmem_free() - Free allocated sysmem region
108  *
109  * @base: region base
110  *
111  * @return 0 on success, otherwise error
112  */
113 int sysmem_free(phys_addr_t base);
114 
115 /**
116  * sysmem_dump() - Dump all sysmem region state and check overflow
117  */
118 void sysmem_dump(void);
119 
120 /**
121  * board_sysmem_reserve() - Weak function for board to implement
122  *
123  * @sysmem: global sysmem point, ignored
124  *
125  * @return 0 on success, otherwise error
126  */
127 int board_sysmem_reserve(struct sysmem *sysmem);
128 #else
129 static inline bool sysmem_has_init(void) { return false; }
130 static inline int sysmem_init(void) { return 0; }
131 static inline int sysmem_initr(void) { return 0; }
132 static inline int sysmem_free(phys_addr_t base) { return 0; }
133 static inline void sysmem_dump(void) {}
134 __weak int board_sysmem_reserve(struct sysmem *sysmem) { return 0; }
135 
136 static inline void *sysmem_alloc(enum memblk_id id, phys_size_t size)
137 {
138 	return malloc(size);
139 }
140 
141 static inline bool sysmem_can_alloc(phys_size_t base, phys_size_t size)
142 {
143 	return true;
144 }
145 
146 static inline void *sysmem_alloc_base(enum memblk_id id,
147 				      phys_addr_t base, phys_size_t size)
148 {
149 	return (void *)base;
150 }
151 static inline void *sysmem_alloc_base_by_name(const char *name,
152 					      phys_addr_t base,
153 					      phys_size_t size)
154 {
155 	return (void *)base;
156 }
157 static inline void *sysmem_fdt_reserve_alloc_base(const char *name,
158 						  phys_addr_t base,
159 						  phys_size_t size)
160 {
161 	return (void *)base;
162 }
163 #endif /* CONFIG_SYSMEM */
164 #endif /* _SYSMEM_H */
165