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