blob: 1573be05c4279ba420b83ac4bbe463a30355885b [file] [log] [blame]
Andrew Geissler9347dd42023-03-03 12:38:41 -06001From 3c1140c29c39561848056fb4b9a03042b00279f3 Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: Jaxson Han <jaxson.han@arm.com>
3Date: Wed, 29 Dec 2021 15:17:38 +0800
4Subject: [PATCH] common: Provide firmware info using libfdt
5
6Boot-wrapper uses libfdt to provide more info in device tree.
7We add a new node to include those new firmware relevant infomation.
8The new node defined as follows:
9 fw-shared-info {
10 compatible = "firmware,shared_info";
11
12 #address-cells = <0x02>;
13 #size-cells = <0x02>;
14
15 version = "1.0";
16 regions = <START_ADDR_HIGH START_ADDR_LOW SIZE_HIGH SIZE_LOW
17 0x0 0x80000000 0x0 0x400000
18 0x0 0x90000000 0x0 0x400000
19 0x0 0xA0000000 0x0 0x400000>;
20 regions-permission = "RX", "R", "RWX", "RW";
21 regions-cache = "Cache", "NCache", "Cache", "Device"
22
23 function_entry = <ENTRY_ADDR_HIGH ENRTY_ADDR_LOW>;
24 };
25The node path is /fw-shared-info.
26For boot-wrapper, in real case, it will be:
27 fw-shared-info {
28 compatible = "firmware,shared_info";
29
30 #address-cells = <0x02>;
31 #size-cells = <0x02>;
32
33 version = "1.0";
34 regions = <0x0 firmware_start 0x0 firmware_code_size
35 0x0 firmware_data 0x0 firmware_data_size>;
36 regions-permission = "RX", "RW";
37 regions-cache = "Cache", "Cache";
38
39 function_entry = <0x0 smc_fn_entry>;
40 };
41
42Issue-Id: SCM-3816
43Upstream-Status: Inappropriate [other]
44 Implementation pending further discussion
45Signed-off-by: Jaxson Han <jaxson.han@arm.com>
46Change-Id: I6ebc59ce2bd3939b0fe066720d57821eaa1bed27
47---
48 common/device_tree.c | 271 ++++++++++++++++++++++++++++++++++++++++++-
49 1 file changed, 270 insertions(+), 1 deletion(-)
50
51diff --git a/common/device_tree.c b/common/device_tree.c
52index 4d0876c..7f7befc 100644
53--- a/common/device_tree.c
54+++ b/common/device_tree.c
55@@ -8,13 +8,225 @@
56 */
57 #include <libfdt.h>
58
59+#define DEVICE_TREE_DEBUG 1
60+
61+#define FW_NODE_NAME "/fw-shared-info"
62+#define FW_COMPAT "firmware,shared_info"
63+#define FW_INFO_VER "1.0"
64+
65+#ifdef BOOTWRAPPER_32
66+#define CELL_NUM 1
67+#define VAL_TYPE uint32_t
68+#else
69+#define CELL_NUM 2
70+#define VAL_TYPE uint64_t
71+#endif
72+
73+#define ALIGN(x) (((x) + (FDT_TAGSIZE) - 1) & ~((FDT_TAGSIZE) - 1))
74+
75 extern unsigned long dtb;
76-extern char firmware_start[], firmware_end[];
77+extern char firmware_start[], firmware_data[], firmware_end[];
78+
79+extern long smc_fn_entry(unsigned long, unsigned long, unsigned long);
80
81 extern void print_string(const char *str);
82+extern void print_hex(unsigned int val);
83
84 static void *blob;
85
86+static char *realloc_node(char *fdt, const char *name)
87+{
88+ int delta;
89+ int new_sz;
90+ /* FDT_BEGIN_NODE, node name in off_struct and FDT_END_NODE */
91+ delta = sizeof(struct fdt_node_header) + ALIGN(strlen(name) + 1)
92+ + FDT_TAGSIZE;
93+ new_sz = fdt_totalsize(fdt) + delta;
94+ fdt_open_into(fdt, fdt, new_sz);
95+ return fdt;
96+}
97+
98+static int create_node(const char *node_name)
99+{
100+ int node = 0;
101+ char *p;
102+
103+ p = strrchr(node_name, '/');
104+ if (!p) {
105+ print_string("node name without '/'\r\n");
106+ return -1;
107+ }
108+ *p = '\0';
109+
110+ blob = realloc_node(blob, p + 1);
111+
112+ if (p > node_name) {
113+ node = fdt_path_offset(blob, node_name);
114+ if (node < 0) {
115+ print_string("no node name\r\n");
116+ return -1;
117+ }
118+ }
119+
120+ node = fdt_add_subnode(blob, node, p + 1);
121+ if (node < 0) {
122+ print_string("add subnode err\r\n");
123+ return -1;
124+ }
125+
126+ return node;
127+}
128+
129+static int dt_create_fw_node(void) {
130+ int fw_node;
131+
132+ fw_node = fdt_path_offset(blob, FW_NODE_NAME);
133+
134+ if(fw_node < 0) {
135+ fw_node = create_node(FW_NODE_NAME);
136+ }
137+
138+ return fw_node;
139+}
140+
141+static char *realloc_property(char *fdt, int nodeoffset, const char *name,
142+ int newlen)
143+{
144+ int delta = 0;
145+ int oldlen = 0;
146+ int new_sz;
147+
148+ if (!fdt_get_property(fdt, nodeoffset, name, &oldlen))
149+ delta = sizeof(struct fdt_property) + strlen(name) + 1;
150+
151+ if (newlen > oldlen)
152+ delta += ALIGN(newlen) - ALIGN(oldlen);
153+
154+ new_sz = fdt_totalsize(fdt) + delta;
155+ fdt_open_into(fdt, fdt, new_sz);
156+ return fdt;
157+}
158+
159+static void dt_set_prop(int node, char *property, void *buf, int len)
160+{
161+ int err;
162+
163+ err = fdt_setprop(blob, node, property, buf, len);
164+ if (err == -FDT_ERR_NOSPACE) {
165+ blob = realloc_property(blob, node, property, len);
166+ err = fdt_setprop(blob, node, property, buf, len);
167+ }
168+ if (err) {
169+ print_string("fdt error\n\r");
170+ }
171+}
172+
173+static void dt_set_prop_u32(int node, char *property, uint32_t val)
174+{
175+ fdt32_t fdt_val = cpu_to_fdt32(val);
176+ int len = sizeof(fdt32_t);
177+
178+ dt_set_prop(node, property, (void*)&fdt_val, len);
179+}
180+
181+static void dt_set_prop_u64(int node, char *property, uint64_t val)
182+{
183+ fdt64_t fdt_val = cpu_to_fdt64(val);
184+ int len = sizeof(fdt64_t);
185+
186+ dt_set_prop(node, property, (void*)&fdt_val, len);
187+}
188+
189+/* This dt_set_prop_u32_array maybe unused according to the BOOTWRAPPER_32 */
190+__attribute__((unused))
191+static void dt_set_prop_u32_array(int node, char *property, uint32_t *vals,
192+ int size)
193+{
194+ fdt32_t *fdt_vals = (fdt32_t*)vals;
195+ int len = sizeof(fdt32_t) * size;
196+
197+ for (int i = 0; i < size; i++) {
198+ fdt_vals[i] = cpu_to_fdt32(vals[i]);
199+ }
200+
201+ dt_set_prop(node, property, (void*)fdt_vals, len);
202+}
203+
204+static void dt_set_prop_u64_array(int node, char *property, uint64_t *vals,
205+ int size)
206+{
207+ fdt64_t *fdt_vals = (fdt64_t*)vals;
208+ int len = sizeof(fdt64_t) * size;
209+
210+ for (int i = 0; i < size; i++) {
211+ fdt_vals[i] = cpu_to_fdt64(vals[i]);
212+ }
213+
214+ dt_set_prop(node, property, (void*)fdt_vals, len);
215+}
216+
217+#if DEVICE_TREE_DEBUG
218+static void dt_dump_string(const void *s, int len)
219+{
220+ char *sub = (char*)s;
221+ int sublen;
222+ while(*sub && ((uint64_t)sub - (uint64_t)s) < len) {
223+ sublen = strlen(sub) + 1;
224+ print_string(sub);
225+ print_string(" ");
226+ sub += sublen;
227+ }
228+ print_string("\n\r");
229+}
230+
231+static void dt_dump_fdt32_array(const void *vals, int len)
232+{
233+ fdt32_t *fdt_vals = (fdt32_t*)vals;
234+ len = len / sizeof(fdt32_t);
235+ for (int i = 0; i < len; i++) {
236+ print_hex(fdt32_to_cpu(fdt_vals[i]));
237+ print_string(" ");
238+ }
239+ print_string("\n\r");
240+}
241+
242+static void dt_dump(int node, char *property, char type)
243+{
244+ const void *val;
245+ int len;
246+
247+ val = fdt_getprop(blob, node, property, &len);
248+ print_string(property);
249+ print_string(": ");
250+
251+ if (type == 's') {
252+ /* string type */
253+ dt_dump_string(val, len);
254+ return;
255+ }
256+
257+ /* uint type */
258+ dt_dump_fdt32_array(val, len);
259+}
260+
261+void dt_dump_all(int node)
262+{
263+ if (node >= 0) {
264+ print_string(FW_NODE_NAME" info:\r\n");
265+ dt_dump(node, "compatible", 's');
266+ dt_dump(node, "version", 's');
267+ dt_dump(node, "function_entry", 'i');
268+ dt_dump(node, "address-cells", 'i');
269+ dt_dump(node, "size-cells", 'i');
270+ dt_dump(node, "regions", 'i');
271+ dt_dump(node, "regions-permission", 's');
272+ dt_dump(node, "regions-cache", 's');
273+ print_string("\r\n");
274+ }
275+}
276+#else
277+void dt_dump_all(int node) { (void*)node; return; }
278+#endif
279
280 void dt_add_memreserve(void)
281 {
282@@ -32,3 +244,60 @@ void dt_add_memreserve(void)
283 print_string("reserve mem add err\n\r");
284 }
285 }
286+
287+void dt_fw_node_init(int enable)
288+{
289+ int fw_node;
290+
291+ VAL_TYPE regions[] = {
292+ /* code region: start, end, ro, x, cachable */
293+ (VAL_TYPE)firmware_start,
294+ (VAL_TYPE)(firmware_data - firmware_start),
295+ /* data region: start, end, rw, xn, cachable */
296+ (VAL_TYPE)firmware_data,
297+ (VAL_TYPE)(firmware_end - firmware_data),
298+ };
299+ int regions_num = sizeof(regions) / sizeof(VAL_TYPE);
300+ char regions_permission[] = "RX\0RW";
301+ char regions_cache[] = "Cache\0Cache";
302+
303+ if (!enable)
304+ return;
305+
306+ print_string("Prepare "FW_NODE_NAME" node\n\r");
307+
308+ blob = (void*)&dtb;
309+
310+ if(fdt_path_offset(blob, "/psci") < 0) {
311+ print_string("/psci node not found\n\r");
312+ return;
313+ }
314+
315+ fw_node = dt_create_fw_node();
316+
317+ if(fw_node < 0) {
318+ print_string(FW_NODE_NAME" node create err\n\r");
319+ }
320+
321+ dt_set_prop(fw_node, "compatible", FW_COMPAT, sizeof(FW_COMPAT));
322+ dt_set_prop(fw_node, "version", FW_INFO_VER, sizeof(FW_INFO_VER));
323+
324+ dt_set_prop_u32(fw_node, "address-cells", CELL_NUM);
325+ dt_set_prop_u32(fw_node, "size-cells", CELL_NUM);
326+ dt_set_prop(fw_node, "regions-permission", regions_permission,
327+ sizeof(regions_permission));
328+ dt_set_prop(fw_node, "regions-cache", regions_cache,
329+ sizeof(regions_cache));
330+
331+#ifdef BOOTWRAPPER_32
332+ dt_set_prop_u32_array(fw_node, "regions", regions, regions_num);
333+ dt_set_prop_u32(fw_node, "function_entry", (VAL_TYPE)smc_fn_entry);
334+#else
335+ dt_set_prop_u64_array(fw_node, "regions", regions, regions_num);
336+ dt_set_prop_u64(fw_node, "function_entry", (VAL_TYPE)smc_fn_entry);
337+#endif
338+
339+ fdt_pack(blob);
340+
341+ dt_dump_all(fw_node);
342+}