xref: /rk3399_ARM-atf/common/fdt_fixup.c (revision 665e71b8ea28162ec7737c1411bca3ea89e5957e)
1 /*
2  * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * Contains generic routines to fix up the device tree blob passed on to
9  * payloads like BL32 and BL33 (and further down the boot chain).
10  * This allows to easily add PSCI nodes, when the original DT does not have
11  * it or advertises another method.
12  * Also it supports to add reserved memory nodes to describe memory that
13  * is used by the secure world, so that non-secure software avoids using
14  * that.
15  */
16 
17 #include <string.h>
18 
19 #include <libfdt.h>
20 
21 #include <common/debug.h>
22 #include <drivers/console.h>
23 #include <lib/psci/psci.h>
24 
25 #include <common/fdt_fixup.h>
26 
27 static int append_psci_compatible(void *fdt, int offs, const char *str)
28 {
29 	return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
30 }
31 
32 /*
33  * Those defines are for PSCI v0.1 legacy clients, which we expect to use
34  * the same execution state (AArch32/AArch64) as TF-A.
35  * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2.
36  */
37 #ifdef __aarch64__
38 #define PSCI_CPU_SUSPEND_FNID	PSCI_CPU_SUSPEND_AARCH64
39 #define PSCI_CPU_ON_FNID	PSCI_CPU_ON_AARCH64
40 #else
41 #define PSCI_CPU_SUSPEND_FNID	PSCI_CPU_SUSPEND_AARCH32
42 #define PSCI_CPU_ON_FNID	PSCI_CPU_ON_AARCH32
43 #endif
44 
45 /*******************************************************************************
46  * dt_add_psci_node() - Add a PSCI node into an existing device tree
47  * @fdt:	pointer to the device tree blob in memory
48  *
49  * Add a device tree node describing PSCI into the root level of an existing
50  * device tree blob in memory.
51  * This will add v0.1, v0.2 and v1.0 compatible strings and the standard
52  * function IDs for v0.1 compatibility.
53  * An existing PSCI node will not be touched, the function will return success
54  * in this case. This function will not touch the /cpus enable methods, use
55  * dt_add_psci_cpu_enable_methods() for that.
56  *
57  * Return: 0 on success, -1 otherwise.
58  ******************************************************************************/
59 int dt_add_psci_node(void *fdt)
60 {
61 	int offs;
62 
63 	if (fdt_path_offset(fdt, "/psci") >= 0) {
64 		WARN("PSCI Device Tree node already exists!\n");
65 		return 0;
66 	}
67 
68 	offs = fdt_path_offset(fdt, "/");
69 	if (offs < 0)
70 		return -1;
71 	offs = fdt_add_subnode(fdt, offs, "psci");
72 	if (offs < 0)
73 		return -1;
74 	if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
75 		return -1;
76 	if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
77 		return -1;
78 	if (append_psci_compatible(fdt, offs, "arm,psci"))
79 		return -1;
80 	if (fdt_setprop_string(fdt, offs, "method", "smc"))
81 		return -1;
82 	if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID))
83 		return -1;
84 	if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
85 		return -1;
86 	if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID))
87 		return -1;
88 	return 0;
89 }
90 
91 /*
92  * Find the first subnode that has a "device_type" property with the value
93  * "cpu" and which's enable-method is not "psci" (yet).
94  * Returns 0 if no such subnode is found, so all have already been patched
95  * or none have to be patched in the first place.
96  * Returns 1 if *one* such subnode has been found and successfully changed
97  * to "psci".
98  * Returns negative values on error.
99  *
100  * Call in a loop until it returns 0. Recalculate the node offset after
101  * it has returned 1.
102  */
103 static int dt_update_one_cpu_node(void *fdt, int offset)
104 {
105 	int offs;
106 
107 	/* Iterate over all subnodes to find those with device_type = "cpu". */
108 	for (offs = fdt_first_subnode(fdt, offset); offs >= 0;
109 	     offs = fdt_next_subnode(fdt, offs)) {
110 		const char *prop;
111 		int len;
112 		int ret;
113 
114 		prop = fdt_getprop(fdt, offs, "device_type", &len);
115 		if (prop == NULL)
116 			continue;
117 		if ((strcmp(prop, "cpu") != 0) || (len != 4))
118 			continue;
119 
120 		/* Ignore any nodes which already use "psci". */
121 		prop = fdt_getprop(fdt, offs, "enable-method", &len);
122 		if ((prop != NULL) &&
123 		    (strcmp(prop, "psci") == 0) && (len == 5))
124 			continue;
125 
126 		ret = fdt_setprop_string(fdt, offs, "enable-method", "psci");
127 		if (ret < 0)
128 			return ret;
129 		/*
130 		 * Subnode found and patched.
131 		 * Restart to accommodate potentially changed offsets.
132 		 */
133 		return 1;
134 	}
135 
136 	if (offs == -FDT_ERR_NOTFOUND)
137 		return 0;
138 
139 	return offs;
140 }
141 
142 /*******************************************************************************
143  * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI
144  * @fdt:	pointer to the device tree blob in memory
145  *
146  * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change
147  * the enable-method to PSCI. This will add the enable-method properties, if
148  * required, or will change existing properties to read "psci".
149  *
150  * Return: 0 on success, or a negative error value otherwise.
151  ******************************************************************************/
152 
153 int dt_add_psci_cpu_enable_methods(void *fdt)
154 {
155 	int offs, ret;
156 
157 	do {
158 		offs = fdt_path_offset(fdt, "/cpus");
159 		if (offs < 0)
160 			return offs;
161 
162 		ret = dt_update_one_cpu_node(fdt, offs);
163 	} while (ret > 0);
164 
165 	return ret;
166 }
167 
168 #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0)
169 
170 /*******************************************************************************
171  * fdt_add_reserved_memory() - reserve (secure) memory regions in DT
172  * @dtb:	pointer to the device tree blob in memory
173  * @node_name:	name of the subnode to be used
174  * @base:	physical base address of the reserved region
175  * @size:	size of the reserved region
176  *
177  * Add a region of memory to the /reserved-memory node in a device tree in
178  * memory, creating that node if required. Each region goes into a subnode
179  * of that node and has a @node_name, a @base address and a @size.
180  * This will prevent any device tree consumer from using that memory. It
181  * can be used to announce secure memory regions, as it adds the "no-map"
182  * property to prevent mapping and speculative operations on that region.
183  *
184  * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding
185  * documentation for details.
186  *
187  * Return: 0 on success, a negative error value otherwise.
188  ******************************************************************************/
189 int fdt_add_reserved_memory(void *dtb, const char *node_name,
190 			    uintptr_t base, size_t size)
191 {
192 	int offs = fdt_path_offset(dtb, "/reserved-memory");
193 	uint32_t addresses[3];
194 
195 	if (offs < 0) {			/* create if not existing yet */
196 		offs = fdt_add_subnode(dtb, 0, "reserved-memory");
197 		if (offs < 0)
198 			return offs;
199 		fdt_setprop_u32(dtb, offs, "#address-cells", 2);
200 		fdt_setprop_u32(dtb, offs, "#size-cells", 1);
201 		fdt_setprop(dtb, offs, "ranges", NULL, 0);
202 	}
203 
204 	addresses[0] = cpu_to_fdt32(HIGH_BITS(base));
205 	addresses[1] = cpu_to_fdt32(base & 0xffffffff);
206 	addresses[2] = cpu_to_fdt32(size & 0xffffffff);
207 	offs = fdt_add_subnode(dtb, offs, node_name);
208 	fdt_setprop(dtb, offs, "no-map", NULL, 0);
209 	fdt_setprop(dtb, offs, "reg", addresses, 12);
210 
211 	return 0;
212 }
213