xref: /rk3399_ARM-atf/common/fdt_fixup.c (revision feb358b65151f7c4d6656a4ee52199f205798ce8)
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 
3266799507SAndre Przywara /*
3366799507SAndre Przywara  * Those defines are for PSCI v0.1 legacy clients, which we expect to use
3466799507SAndre Przywara  * the same execution state (AArch32/AArch64) as TF-A.
3566799507SAndre Przywara  * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2.
3666799507SAndre Przywara  */
3766799507SAndre Przywara #ifdef __aarch64__
3866799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID	PSCI_CPU_SUSPEND_AARCH64
3966799507SAndre Przywara #define PSCI_CPU_ON_FNID	PSCI_CPU_ON_AARCH64
4066799507SAndre Przywara #else
4166799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID	PSCI_CPU_SUSPEND_AARCH32
4266799507SAndre Przywara #define PSCI_CPU_ON_FNID	PSCI_CPU_ON_AARCH32
4366799507SAndre Przywara #endif
4466799507SAndre Przywara 
456eaf928dSAndre Przywara /*******************************************************************************
466eaf928dSAndre Przywara  * dt_add_psci_node() - Add a PSCI node into an existing device tree
476eaf928dSAndre Przywara  * @fdt:	pointer to the device tree blob in memory
486eaf928dSAndre Przywara  *
496eaf928dSAndre Przywara  * Add a device tree node describing PSCI into the root level of an existing
506eaf928dSAndre Przywara  * device tree blob in memory.
516eaf928dSAndre Przywara  * This will add v0.1, v0.2 and v1.0 compatible strings and the standard
526eaf928dSAndre Przywara  * function IDs for v0.1 compatibility.
536eaf928dSAndre Przywara  * An existing PSCI node will not be touched, the function will return success
546eaf928dSAndre Przywara  * in this case. This function will not touch the /cpus enable methods, use
556eaf928dSAndre Przywara  * dt_add_psci_cpu_enable_methods() for that.
566eaf928dSAndre Przywara  *
576eaf928dSAndre Przywara  * Return: 0 on success, -1 otherwise.
586eaf928dSAndre Przywara  ******************************************************************************/
59f240728bSAndre Przywara int dt_add_psci_node(void *fdt)
60f240728bSAndre Przywara {
61f240728bSAndre Przywara 	int offs;
62f240728bSAndre Przywara 
63f240728bSAndre Przywara 	if (fdt_path_offset(fdt, "/psci") >= 0) {
64f240728bSAndre Przywara 		WARN("PSCI Device Tree node already exists!\n");
65f240728bSAndre Przywara 		return 0;
66f240728bSAndre Przywara 	}
67f240728bSAndre Przywara 
68f240728bSAndre Przywara 	offs = fdt_path_offset(fdt, "/");
69f240728bSAndre Przywara 	if (offs < 0)
70f240728bSAndre Przywara 		return -1;
71f240728bSAndre Przywara 	offs = fdt_add_subnode(fdt, offs, "psci");
72f240728bSAndre Przywara 	if (offs < 0)
73f240728bSAndre Przywara 		return -1;
74f240728bSAndre Przywara 	if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
75f240728bSAndre Przywara 		return -1;
76f240728bSAndre Przywara 	if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
77f240728bSAndre Przywara 		return -1;
78f240728bSAndre Przywara 	if (append_psci_compatible(fdt, offs, "arm,psci"))
79f240728bSAndre Przywara 		return -1;
80f240728bSAndre Przywara 	if (fdt_setprop_string(fdt, offs, "method", "smc"))
81f240728bSAndre Przywara 		return -1;
8266799507SAndre Przywara 	if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID))
83f240728bSAndre Przywara 		return -1;
84f240728bSAndre Przywara 	if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
85f240728bSAndre Przywara 		return -1;
8666799507SAndre Przywara 	if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID))
87f240728bSAndre Przywara 		return -1;
88f240728bSAndre Przywara 	return 0;
89f240728bSAndre Przywara }
90f240728bSAndre Przywara 
91f240728bSAndre Przywara /*
92f240728bSAndre Przywara  * Find the first subnode that has a "device_type" property with the value
93f240728bSAndre Przywara  * "cpu" and which's enable-method is not "psci" (yet).
94f240728bSAndre Przywara  * Returns 0 if no such subnode is found, so all have already been patched
95f240728bSAndre Przywara  * or none have to be patched in the first place.
96f240728bSAndre Przywara  * Returns 1 if *one* such subnode has been found and successfully changed
97f240728bSAndre Przywara  * to "psci".
98*feb358b6SAndre Przywara  * Returns negative values on error.
99f240728bSAndre Przywara  *
100f240728bSAndre Przywara  * Call in a loop until it returns 0. Recalculate the node offset after
101f240728bSAndre Przywara  * it has returned 1.
102f240728bSAndre Przywara  */
103f240728bSAndre Przywara static int dt_update_one_cpu_node(void *fdt, int offset)
104f240728bSAndre Przywara {
105f240728bSAndre Przywara 	int offs;
106f240728bSAndre Przywara 
107f240728bSAndre Przywara 	/* Iterate over all subnodes to find those with device_type = "cpu". */
108f240728bSAndre Przywara 	for (offs = fdt_first_subnode(fdt, offset); offs >= 0;
109f240728bSAndre Przywara 	     offs = fdt_next_subnode(fdt, offs)) {
110f240728bSAndre Przywara 		const char *prop;
111f240728bSAndre Przywara 		int len;
112*feb358b6SAndre Przywara 		int ret;
113f240728bSAndre Przywara 
114f240728bSAndre Przywara 		prop = fdt_getprop(fdt, offs, "device_type", &len);
115*feb358b6SAndre Przywara 		if (prop == NULL)
116f240728bSAndre Przywara 			continue;
117*feb358b6SAndre Przywara 		if ((strcmp(prop, "cpu") != 0) || (len != 4))
118f240728bSAndre Przywara 			continue;
119f240728bSAndre Przywara 
120f240728bSAndre Przywara 		/* Ignore any nodes which already use "psci". */
121f240728bSAndre Przywara 		prop = fdt_getprop(fdt, offs, "enable-method", &len);
122*feb358b6SAndre Przywara 		if ((prop != NULL) &&
123*feb358b6SAndre Przywara 		    (strcmp(prop, "psci") == 0) && (len == 5))
124f240728bSAndre Przywara 			continue;
125f240728bSAndre Przywara 
126*feb358b6SAndre Przywara 		ret = fdt_setprop_string(fdt, offs, "enable-method", "psci");
127*feb358b6SAndre Przywara 		if (ret < 0)
128*feb358b6SAndre Przywara 			return ret;
129f240728bSAndre Przywara 		/*
130f240728bSAndre Przywara 		 * Subnode found and patched.
131f240728bSAndre Przywara 		 * Restart to accommodate potentially changed offsets.
132f240728bSAndre Przywara 		 */
133f240728bSAndre Przywara 		return 1;
134f240728bSAndre Przywara 	}
135f240728bSAndre Przywara 
136f240728bSAndre Przywara 	if (offs == -FDT_ERR_NOTFOUND)
137f240728bSAndre Przywara 		return 0;
138f240728bSAndre Przywara 
139f240728bSAndre Przywara 	return offs;
140f240728bSAndre Przywara }
141f240728bSAndre Przywara 
1426eaf928dSAndre Przywara /*******************************************************************************
1436eaf928dSAndre Przywara  * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI
1446eaf928dSAndre Przywara  * @fdt:	pointer to the device tree blob in memory
1456eaf928dSAndre Przywara  *
1466eaf928dSAndre Przywara  * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change
1476eaf928dSAndre Przywara  * the enable-method to PSCI. This will add the enable-method properties, if
1486eaf928dSAndre Przywara  * required, or will change existing properties to read "psci".
1496eaf928dSAndre Przywara  *
1506eaf928dSAndre Przywara  * Return: 0 on success, or a negative error value otherwise.
1516eaf928dSAndre Przywara  ******************************************************************************/
1526eaf928dSAndre Przywara 
153f240728bSAndre Przywara int dt_add_psci_cpu_enable_methods(void *fdt)
154f240728bSAndre Przywara {
155f240728bSAndre Przywara 	int offs, ret;
156f240728bSAndre Przywara 
157f240728bSAndre Przywara 	do {
158f240728bSAndre Przywara 		offs = fdt_path_offset(fdt, "/cpus");
159f240728bSAndre Przywara 		if (offs < 0)
160f240728bSAndre Przywara 			return offs;
161f240728bSAndre Przywara 
162f240728bSAndre Przywara 		ret = dt_update_one_cpu_node(fdt, offs);
163f240728bSAndre Przywara 	} while (ret > 0);
164f240728bSAndre Przywara 
165f240728bSAndre Przywara 	return ret;
166f240728bSAndre Przywara }
1673ef45ddaSAndre Przywara 
1683ef45ddaSAndre Przywara #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0)
1693ef45ddaSAndre Przywara 
1706eaf928dSAndre Przywara /*******************************************************************************
1716eaf928dSAndre Przywara  * fdt_add_reserved_memory() - reserve (secure) memory regions in DT
1726eaf928dSAndre Przywara  * @dtb:	pointer to the device tree blob in memory
1736eaf928dSAndre Przywara  * @node_name:	name of the subnode to be used
1746eaf928dSAndre Przywara  * @base:	physical base address of the reserved region
1756eaf928dSAndre Przywara  * @size:	size of the reserved region
1766eaf928dSAndre Przywara  *
1776eaf928dSAndre Przywara  * Add a region of memory to the /reserved-memory node in a device tree in
1786eaf928dSAndre Przywara  * memory, creating that node if required. Each region goes into a subnode
1796eaf928dSAndre Przywara  * of that node and has a @node_name, a @base address and a @size.
1806eaf928dSAndre Przywara  * This will prevent any device tree consumer from using that memory. It
1816eaf928dSAndre Przywara  * can be used to announce secure memory regions, as it adds the "no-map"
1826eaf928dSAndre Przywara  * property to prevent mapping and speculative operations on that region.
1836eaf928dSAndre Przywara  *
1846eaf928dSAndre Przywara  * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding
1856eaf928dSAndre Przywara  * documentation for details.
1866eaf928dSAndre Przywara  *
1876eaf928dSAndre Przywara  * Return: 0 on success, a negative error value otherwise.
1886eaf928dSAndre Przywara  ******************************************************************************/
1893ef45ddaSAndre Przywara int fdt_add_reserved_memory(void *dtb, const char *node_name,
1903ef45ddaSAndre Przywara 			    uintptr_t base, size_t size)
1913ef45ddaSAndre Przywara {
1923ef45ddaSAndre Przywara 	int offs = fdt_path_offset(dtb, "/reserved-memory");
1933ef45ddaSAndre Przywara 	uint32_t addresses[3];
1943ef45ddaSAndre Przywara 
1953ef45ddaSAndre Przywara 	if (offs < 0) {			/* create if not existing yet */
1963ef45ddaSAndre Przywara 		offs = fdt_add_subnode(dtb, 0, "reserved-memory");
1973ef45ddaSAndre Przywara 		if (offs < 0)
1983ef45ddaSAndre Przywara 			return offs;
1993ef45ddaSAndre Przywara 		fdt_setprop_u32(dtb, offs, "#address-cells", 2);
2003ef45ddaSAndre Przywara 		fdt_setprop_u32(dtb, offs, "#size-cells", 1);
2013ef45ddaSAndre Przywara 		fdt_setprop(dtb, offs, "ranges", NULL, 0);
2023ef45ddaSAndre Przywara 	}
2033ef45ddaSAndre Przywara 
2043ef45ddaSAndre Przywara 	addresses[0] = cpu_to_fdt32(HIGH_BITS(base));
2053ef45ddaSAndre Przywara 	addresses[1] = cpu_to_fdt32(base & 0xffffffff);
2063ef45ddaSAndre Przywara 	addresses[2] = cpu_to_fdt32(size & 0xffffffff);
2073ef45ddaSAndre Przywara 	offs = fdt_add_subnode(dtb, offs, node_name);
2083ef45ddaSAndre Przywara 	fdt_setprop(dtb, offs, "no-map", NULL, 0);
2093ef45ddaSAndre Przywara 	fdt_setprop(dtb, offs, "reg", addresses, 12);
2103ef45ddaSAndre Przywara 
2113ef45ddaSAndre Przywara 	return 0;
2123ef45ddaSAndre Przywara }
213