xref: /rk3399_ARM-atf/common/fdt_fixup.c (revision 2b2b565717cc0299e75e8806004d1a3548e9fbf7)
1f240728bSAndre Przywara /*
2*2b2b5657SSamuel Holland  * Copyright (c) 2016-2022, 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 
17780dd2b3SJavier Almansa Sobrino #include <errno.h>
18780dd2b3SJavier Almansa Sobrino #include <stdio.h>
19f240728bSAndre Przywara #include <string.h>
20f240728bSAndre Przywara 
21f240728bSAndre Przywara #include <libfdt.h>
22f240728bSAndre Przywara 
23780dd2b3SJavier Almansa Sobrino #include <arch.h>
24f240728bSAndre Przywara #include <common/debug.h>
25780dd2b3SJavier Almansa Sobrino #include <common/fdt_fixup.h>
26780dd2b3SJavier Almansa Sobrino #include <common/fdt_wrappers.h>
27f240728bSAndre Przywara #include <drivers/console.h>
28f240728bSAndre Przywara #include <lib/psci/psci.h>
29780dd2b3SJavier Almansa Sobrino #include <plat/common/platform.h>
30f240728bSAndre Przywara 
31f240728bSAndre Przywara 
32f240728bSAndre Przywara static int append_psci_compatible(void *fdt, int offs, const char *str)
33f240728bSAndre Przywara {
34f240728bSAndre Przywara 	return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
35f240728bSAndre Przywara }
36f240728bSAndre Przywara 
3766799507SAndre Przywara /*
3866799507SAndre Przywara  * Those defines are for PSCI v0.1 legacy clients, which we expect to use
3966799507SAndre Przywara  * the same execution state (AArch32/AArch64) as TF-A.
4066799507SAndre Przywara  * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2.
4166799507SAndre Przywara  */
4266799507SAndre Przywara #ifdef __aarch64__
4366799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID	PSCI_CPU_SUSPEND_AARCH64
4466799507SAndre Przywara #define PSCI_CPU_ON_FNID	PSCI_CPU_ON_AARCH64
4566799507SAndre Przywara #else
4666799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID	PSCI_CPU_SUSPEND_AARCH32
4766799507SAndre Przywara #define PSCI_CPU_ON_FNID	PSCI_CPU_ON_AARCH32
4866799507SAndre Przywara #endif
4966799507SAndre Przywara 
506eaf928dSAndre Przywara /*******************************************************************************
516eaf928dSAndre Przywara  * dt_add_psci_node() - Add a PSCI node into an existing device tree
526eaf928dSAndre Przywara  * @fdt:	pointer to the device tree blob in memory
536eaf928dSAndre Przywara  *
546eaf928dSAndre Przywara  * Add a device tree node describing PSCI into the root level of an existing
556eaf928dSAndre Przywara  * device tree blob in memory.
566eaf928dSAndre Przywara  * This will add v0.1, v0.2 and v1.0 compatible strings and the standard
576eaf928dSAndre Przywara  * function IDs for v0.1 compatibility.
586eaf928dSAndre Przywara  * An existing PSCI node will not be touched, the function will return success
596eaf928dSAndre Przywara  * in this case. This function will not touch the /cpus enable methods, use
606eaf928dSAndre Przywara  * dt_add_psci_cpu_enable_methods() for that.
616eaf928dSAndre Przywara  *
626eaf928dSAndre Przywara  * Return: 0 on success, -1 otherwise.
636eaf928dSAndre Przywara  ******************************************************************************/
64f240728bSAndre Przywara int dt_add_psci_node(void *fdt)
65f240728bSAndre Przywara {
66f240728bSAndre Przywara 	int offs;
67f240728bSAndre Przywara 
68f240728bSAndre Przywara 	if (fdt_path_offset(fdt, "/psci") >= 0) {
69f240728bSAndre Przywara 		WARN("PSCI Device Tree node already exists!\n");
70f240728bSAndre Przywara 		return 0;
71f240728bSAndre Przywara 	}
72f240728bSAndre Przywara 
73f240728bSAndre Przywara 	offs = fdt_path_offset(fdt, "/");
74f240728bSAndre Przywara 	if (offs < 0)
75f240728bSAndre Przywara 		return -1;
76f240728bSAndre Przywara 	offs = fdt_add_subnode(fdt, offs, "psci");
77f240728bSAndre Przywara 	if (offs < 0)
78f240728bSAndre Przywara 		return -1;
79f240728bSAndre Przywara 	if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
80f240728bSAndre Przywara 		return -1;
81f240728bSAndre Przywara 	if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
82f240728bSAndre Przywara 		return -1;
83f240728bSAndre Przywara 	if (append_psci_compatible(fdt, offs, "arm,psci"))
84f240728bSAndre Przywara 		return -1;
85f240728bSAndre Przywara 	if (fdt_setprop_string(fdt, offs, "method", "smc"))
86f240728bSAndre Przywara 		return -1;
8766799507SAndre Przywara 	if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID))
88f240728bSAndre Przywara 		return -1;
89f240728bSAndre Przywara 	if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
90f240728bSAndre Przywara 		return -1;
9166799507SAndre Przywara 	if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID))
92f240728bSAndre Przywara 		return -1;
93f240728bSAndre Przywara 	return 0;
94f240728bSAndre Przywara }
95f240728bSAndre Przywara 
96f240728bSAndre Przywara /*
97f240728bSAndre Przywara  * Find the first subnode that has a "device_type" property with the value
98f240728bSAndre Przywara  * "cpu" and which's enable-method is not "psci" (yet).
99f240728bSAndre Przywara  * Returns 0 if no such subnode is found, so all have already been patched
100f240728bSAndre Przywara  * or none have to be patched in the first place.
101f240728bSAndre Przywara  * Returns 1 if *one* such subnode has been found and successfully changed
102f240728bSAndre Przywara  * to "psci".
103feb358b6SAndre Przywara  * Returns negative values on error.
104f240728bSAndre Przywara  *
105f240728bSAndre Przywara  * Call in a loop until it returns 0. Recalculate the node offset after
106f240728bSAndre Przywara  * it has returned 1.
107f240728bSAndre Przywara  */
108f240728bSAndre Przywara static int dt_update_one_cpu_node(void *fdt, int offset)
109f240728bSAndre Przywara {
110f240728bSAndre Przywara 	int offs;
111f240728bSAndre Przywara 
112f240728bSAndre Przywara 	/* Iterate over all subnodes to find those with device_type = "cpu". */
113f240728bSAndre Przywara 	for (offs = fdt_first_subnode(fdt, offset); offs >= 0;
114f240728bSAndre Przywara 	     offs = fdt_next_subnode(fdt, offs)) {
115f240728bSAndre Przywara 		const char *prop;
116f240728bSAndre Przywara 		int len;
117feb358b6SAndre Przywara 		int ret;
118f240728bSAndre Przywara 
119f240728bSAndre Przywara 		prop = fdt_getprop(fdt, offs, "device_type", &len);
120feb358b6SAndre Przywara 		if (prop == NULL)
121f240728bSAndre Przywara 			continue;
122feb358b6SAndre Przywara 		if ((strcmp(prop, "cpu") != 0) || (len != 4))
123f240728bSAndre Przywara 			continue;
124f240728bSAndre Przywara 
125f240728bSAndre Przywara 		/* Ignore any nodes which already use "psci". */
126f240728bSAndre Przywara 		prop = fdt_getprop(fdt, offs, "enable-method", &len);
127feb358b6SAndre Przywara 		if ((prop != NULL) &&
128feb358b6SAndre Przywara 		    (strcmp(prop, "psci") == 0) && (len == 5))
129f240728bSAndre Przywara 			continue;
130f240728bSAndre Przywara 
131feb358b6SAndre Przywara 		ret = fdt_setprop_string(fdt, offs, "enable-method", "psci");
132feb358b6SAndre Przywara 		if (ret < 0)
133feb358b6SAndre Przywara 			return ret;
134f240728bSAndre Przywara 		/*
135f240728bSAndre Przywara 		 * Subnode found and patched.
136f240728bSAndre Przywara 		 * Restart to accommodate potentially changed offsets.
137f240728bSAndre Przywara 		 */
138f240728bSAndre Przywara 		return 1;
139f240728bSAndre Przywara 	}
140f240728bSAndre Przywara 
141f240728bSAndre Przywara 	if (offs == -FDT_ERR_NOTFOUND)
142f240728bSAndre Przywara 		return 0;
143f240728bSAndre Przywara 
144f240728bSAndre Przywara 	return offs;
145f240728bSAndre Przywara }
146f240728bSAndre Przywara 
1476eaf928dSAndre Przywara /*******************************************************************************
1486eaf928dSAndre Przywara  * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI
1496eaf928dSAndre Przywara  * @fdt:	pointer to the device tree blob in memory
1506eaf928dSAndre Przywara  *
1516eaf928dSAndre Przywara  * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change
1526eaf928dSAndre Przywara  * the enable-method to PSCI. This will add the enable-method properties, if
1536eaf928dSAndre Przywara  * required, or will change existing properties to read "psci".
1546eaf928dSAndre Przywara  *
1556eaf928dSAndre Przywara  * Return: 0 on success, or a negative error value otherwise.
1566eaf928dSAndre Przywara  ******************************************************************************/
1576eaf928dSAndre Przywara 
158f240728bSAndre Przywara int dt_add_psci_cpu_enable_methods(void *fdt)
159f240728bSAndre Przywara {
160f240728bSAndre Przywara 	int offs, ret;
161f240728bSAndre Przywara 
162f240728bSAndre Przywara 	do {
163f240728bSAndre Przywara 		offs = fdt_path_offset(fdt, "/cpus");
164f240728bSAndre Przywara 		if (offs < 0)
165f240728bSAndre Przywara 			return offs;
166f240728bSAndre Przywara 
167f240728bSAndre Przywara 		ret = dt_update_one_cpu_node(fdt, offs);
168f240728bSAndre Przywara 	} while (ret > 0);
169f240728bSAndre Przywara 
170f240728bSAndre Przywara 	return ret;
171f240728bSAndre Przywara }
1723ef45ddaSAndre Przywara 
1733ef45ddaSAndre Przywara #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0)
1743ef45ddaSAndre Przywara 
1756eaf928dSAndre Przywara /*******************************************************************************
1766eaf928dSAndre Przywara  * fdt_add_reserved_memory() - reserve (secure) memory regions in DT
1776eaf928dSAndre Przywara  * @dtb:	pointer to the device tree blob in memory
1786eaf928dSAndre Przywara  * @node_name:	name of the subnode to be used
1796eaf928dSAndre Przywara  * @base:	physical base address of the reserved region
1806eaf928dSAndre Przywara  * @size:	size of the reserved region
1816eaf928dSAndre Przywara  *
1826eaf928dSAndre Przywara  * Add a region of memory to the /reserved-memory node in a device tree in
1836eaf928dSAndre Przywara  * memory, creating that node if required. Each region goes into a subnode
1846eaf928dSAndre Przywara  * of that node and has a @node_name, a @base address and a @size.
1856eaf928dSAndre Przywara  * This will prevent any device tree consumer from using that memory. It
1866eaf928dSAndre Przywara  * can be used to announce secure memory regions, as it adds the "no-map"
1876eaf928dSAndre Przywara  * property to prevent mapping and speculative operations on that region.
1886eaf928dSAndre Przywara  *
1896eaf928dSAndre Przywara  * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding
1906eaf928dSAndre Przywara  * documentation for details.
19181146c46SAndre Przywara  * According to this binding, the address-cells and size-cells must match
19281146c46SAndre Przywara  * those of the root node.
1936eaf928dSAndre Przywara  *
1946eaf928dSAndre Przywara  * Return: 0 on success, a negative error value otherwise.
1956eaf928dSAndre Przywara  ******************************************************************************/
1963ef45ddaSAndre Przywara int fdt_add_reserved_memory(void *dtb, const char *node_name,
1973ef45ddaSAndre Przywara 			    uintptr_t base, size_t size)
1983ef45ddaSAndre Przywara {
1993ef45ddaSAndre Przywara 	int offs = fdt_path_offset(dtb, "/reserved-memory");
20081146c46SAndre Przywara 	uint32_t addresses[4];
20181146c46SAndre Przywara 	int ac, sc;
20281146c46SAndre Przywara 	unsigned int idx = 0;
2033ef45ddaSAndre Przywara 
20481146c46SAndre Przywara 	ac = fdt_address_cells(dtb, 0);
20581146c46SAndre Przywara 	sc = fdt_size_cells(dtb, 0);
2063ef45ddaSAndre Przywara 	if (offs < 0) {			/* create if not existing yet */
2073ef45ddaSAndre Przywara 		offs = fdt_add_subnode(dtb, 0, "reserved-memory");
20881146c46SAndre Przywara 		if (offs < 0) {
2093ef45ddaSAndre Przywara 			return offs;
21081146c46SAndre Przywara 		}
21181146c46SAndre Przywara 		fdt_setprop_u32(dtb, offs, "#address-cells", ac);
21281146c46SAndre Przywara 		fdt_setprop_u32(dtb, offs, "#size-cells", sc);
2133ef45ddaSAndre Przywara 		fdt_setprop(dtb, offs, "ranges", NULL, 0);
2143ef45ddaSAndre Przywara 	}
2153ef45ddaSAndre Przywara 
21681146c46SAndre Przywara 	if (ac > 1) {
21781146c46SAndre Przywara 		addresses[idx] = cpu_to_fdt32(HIGH_BITS(base));
21881146c46SAndre Przywara 		idx++;
21981146c46SAndre Przywara 	}
22081146c46SAndre Przywara 	addresses[idx] = cpu_to_fdt32(base & 0xffffffff);
22181146c46SAndre Przywara 	idx++;
22281146c46SAndre Przywara 	if (sc > 1) {
22381146c46SAndre Przywara 		addresses[idx] = cpu_to_fdt32(HIGH_BITS(size));
22481146c46SAndre Przywara 		idx++;
22581146c46SAndre Przywara 	}
22681146c46SAndre Przywara 	addresses[idx] = cpu_to_fdt32(size & 0xffffffff);
22781146c46SAndre Przywara 	idx++;
2283ef45ddaSAndre Przywara 	offs = fdt_add_subnode(dtb, offs, node_name);
2293ef45ddaSAndre Przywara 	fdt_setprop(dtb, offs, "no-map", NULL, 0);
23081146c46SAndre Przywara 	fdt_setprop(dtb, offs, "reg", addresses, idx * sizeof(uint32_t));
2313ef45ddaSAndre Przywara 
2323ef45ddaSAndre Przywara 	return 0;
2333ef45ddaSAndre Przywara }
234780dd2b3SJavier Almansa Sobrino 
235780dd2b3SJavier Almansa Sobrino /*******************************************************************************
236780dd2b3SJavier Almansa Sobrino  * fdt_add_cpu()	Add a new CPU node to the DT
237780dd2b3SJavier Almansa Sobrino  * @dtb:		Pointer to the device tree blob in memory
238780dd2b3SJavier Almansa Sobrino  * @parent:		Offset of the parent node
239780dd2b3SJavier Almansa Sobrino  * @mpidr:		MPIDR for the current CPU
240780dd2b3SJavier Almansa Sobrino  *
241780dd2b3SJavier Almansa Sobrino  * Create and add a new cpu node to a DTB.
242780dd2b3SJavier Almansa Sobrino  *
243780dd2b3SJavier Almansa Sobrino  * Return the offset of the new node or a negative value in case of error
244780dd2b3SJavier Almansa Sobrino  ******************************************************************************/
245780dd2b3SJavier Almansa Sobrino 
246780dd2b3SJavier Almansa Sobrino static int fdt_add_cpu(void *dtb, int parent, u_register_t mpidr)
247780dd2b3SJavier Almansa Sobrino {
248780dd2b3SJavier Almansa Sobrino 	int cpu_offs;
249780dd2b3SJavier Almansa Sobrino 	int err;
250780dd2b3SJavier Almansa Sobrino 	char snode_name[15];
251780dd2b3SJavier Almansa Sobrino 	uint64_t reg_prop;
252780dd2b3SJavier Almansa Sobrino 
253780dd2b3SJavier Almansa Sobrino 	reg_prop = mpidr & MPID_MASK & ~MPIDR_MT_MASK;
254780dd2b3SJavier Almansa Sobrino 
255780dd2b3SJavier Almansa Sobrino 	snprintf(snode_name, sizeof(snode_name), "cpu@%x",
256780dd2b3SJavier Almansa Sobrino 					(unsigned int)reg_prop);
257780dd2b3SJavier Almansa Sobrino 
258780dd2b3SJavier Almansa Sobrino 	cpu_offs = fdt_add_subnode(dtb, parent, snode_name);
259780dd2b3SJavier Almansa Sobrino 	if (cpu_offs < 0) {
260780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: add subnode \"%s\" failed: %i\n",
261780dd2b3SJavier Almansa Sobrino 							snode_name, cpu_offs);
262780dd2b3SJavier Almansa Sobrino 		return cpu_offs;
263780dd2b3SJavier Almansa Sobrino 	}
264780dd2b3SJavier Almansa Sobrino 
265780dd2b3SJavier Almansa Sobrino 	err = fdt_setprop_string(dtb, cpu_offs, "compatible", "arm,armv8");
266780dd2b3SJavier Almansa Sobrino 	if (err < 0) {
267780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
268780dd2b3SJavier Almansa Sobrino 			"compatible", cpu_offs);
269780dd2b3SJavier Almansa Sobrino 		return err;
270780dd2b3SJavier Almansa Sobrino 	}
271780dd2b3SJavier Almansa Sobrino 
272780dd2b3SJavier Almansa Sobrino 	err = fdt_setprop_u64(dtb, cpu_offs, "reg", reg_prop);
273780dd2b3SJavier Almansa Sobrino 	if (err < 0) {
274780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
275780dd2b3SJavier Almansa Sobrino 			"reg", cpu_offs);
276780dd2b3SJavier Almansa Sobrino 		return err;
277780dd2b3SJavier Almansa Sobrino 	}
278780dd2b3SJavier Almansa Sobrino 
279780dd2b3SJavier Almansa Sobrino 	err = fdt_setprop_string(dtb, cpu_offs, "device_type", "cpu");
280780dd2b3SJavier Almansa Sobrino 	if (err < 0) {
281780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
282780dd2b3SJavier Almansa Sobrino 			"device_type", cpu_offs);
283780dd2b3SJavier Almansa Sobrino 		return err;
284780dd2b3SJavier Almansa Sobrino 	}
285780dd2b3SJavier Almansa Sobrino 
286780dd2b3SJavier Almansa Sobrino 	err = fdt_setprop_string(dtb, cpu_offs, "enable-method", "psci");
287780dd2b3SJavier Almansa Sobrino 	if (err < 0) {
288780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
289780dd2b3SJavier Almansa Sobrino 			"enable-method", cpu_offs);
290780dd2b3SJavier Almansa Sobrino 		return err;
291780dd2b3SJavier Almansa Sobrino 	}
292780dd2b3SJavier Almansa Sobrino 
293780dd2b3SJavier Almansa Sobrino 	return cpu_offs;
294780dd2b3SJavier Almansa Sobrino }
295780dd2b3SJavier Almansa Sobrino 
296780dd2b3SJavier Almansa Sobrino /******************************************************************************
297780dd2b3SJavier Almansa Sobrino  * fdt_add_cpus_node() - Add the cpus node to the DTB
298780dd2b3SJavier Almansa Sobrino  * @dtb:		pointer to the device tree blob in memory
299780dd2b3SJavier Almansa Sobrino  * @afflv0:		Maximum number of threads per core (affinity level 0).
300780dd2b3SJavier Almansa Sobrino  * @afflv1:		Maximum number of CPUs per cluster (affinity level 1).
301780dd2b3SJavier Almansa Sobrino  * @afflv2:		Maximum number of clusters (affinity level 2).
302780dd2b3SJavier Almansa Sobrino  *
303780dd2b3SJavier Almansa Sobrino  * Iterate over all the possible MPIDs given the maximum affinity levels and
304780dd2b3SJavier Almansa Sobrino  * add a cpus node to the DTB with all the valid CPUs on the system.
305780dd2b3SJavier Almansa Sobrino  * If there is already a /cpus node, exit gracefully
306780dd2b3SJavier Almansa Sobrino  *
307780dd2b3SJavier Almansa Sobrino  * A system with two CPUs would generate a node equivalent or similar to:
308780dd2b3SJavier Almansa Sobrino  *
309780dd2b3SJavier Almansa Sobrino  *	cpus {
310780dd2b3SJavier Almansa Sobrino  *		#address-cells = <2>;
311780dd2b3SJavier Almansa Sobrino  *		#size-cells = <0>;
312780dd2b3SJavier Almansa Sobrino  *
313780dd2b3SJavier Almansa Sobrino  *		cpu0: cpu@0 {
314780dd2b3SJavier Almansa Sobrino  *			compatible = "arm,armv8";
315780dd2b3SJavier Almansa Sobrino  *			reg = <0x0 0x0>;
316780dd2b3SJavier Almansa Sobrino  *			device_type = "cpu";
317780dd2b3SJavier Almansa Sobrino  *			enable-method = "psci";
318780dd2b3SJavier Almansa Sobrino  *		};
319780dd2b3SJavier Almansa Sobrino  *		cpu1: cpu@10000 {
320780dd2b3SJavier Almansa Sobrino  *			compatible = "arm,armv8";
321780dd2b3SJavier Almansa Sobrino  *			reg = <0x0 0x100>;
322780dd2b3SJavier Almansa Sobrino  *			device_type = "cpu";
323780dd2b3SJavier Almansa Sobrino  *			enable-method = "psci";
324780dd2b3SJavier Almansa Sobrino  *		};
325780dd2b3SJavier Almansa Sobrino  *	};
326780dd2b3SJavier Almansa Sobrino  *
327780dd2b3SJavier Almansa Sobrino  * Full documentation about the CPU bindings can be found at:
328780dd2b3SJavier Almansa Sobrino  * https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/cpus.txt
329780dd2b3SJavier Almansa Sobrino  *
330780dd2b3SJavier Almansa Sobrino  * Return the offset of the node or a negative value on error.
331780dd2b3SJavier Almansa Sobrino  ******************************************************************************/
332780dd2b3SJavier Almansa Sobrino 
333780dd2b3SJavier Almansa Sobrino int fdt_add_cpus_node(void *dtb, unsigned int afflv0,
334780dd2b3SJavier Almansa Sobrino 		      unsigned int afflv1, unsigned int afflv2)
335780dd2b3SJavier Almansa Sobrino {
336780dd2b3SJavier Almansa Sobrino 	int offs;
337780dd2b3SJavier Almansa Sobrino 	int err;
338780dd2b3SJavier Almansa Sobrino 	unsigned int i, j, k;
339780dd2b3SJavier Almansa Sobrino 	u_register_t mpidr;
340780dd2b3SJavier Almansa Sobrino 	int cpuid;
341780dd2b3SJavier Almansa Sobrino 
342780dd2b3SJavier Almansa Sobrino 	if (fdt_path_offset(dtb, "/cpus") >= 0) {
343780dd2b3SJavier Almansa Sobrino 		return -EEXIST;
344780dd2b3SJavier Almansa Sobrino 	}
345780dd2b3SJavier Almansa Sobrino 
346780dd2b3SJavier Almansa Sobrino 	offs = fdt_add_subnode(dtb, 0, "cpus");
347780dd2b3SJavier Almansa Sobrino 	if (offs < 0) {
348780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: add subnode \"cpus\" node to parent node failed");
349780dd2b3SJavier Almansa Sobrino 		return offs;
350780dd2b3SJavier Almansa Sobrino 	}
351780dd2b3SJavier Almansa Sobrino 
352780dd2b3SJavier Almansa Sobrino 	err = fdt_setprop_u32(dtb, offs, "#address-cells", 2);
353780dd2b3SJavier Almansa Sobrino 	if (err < 0) {
354780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
355780dd2b3SJavier Almansa Sobrino 			"#address-cells", offs);
356780dd2b3SJavier Almansa Sobrino 		return err;
357780dd2b3SJavier Almansa Sobrino 	}
358780dd2b3SJavier Almansa Sobrino 
359780dd2b3SJavier Almansa Sobrino 	err = fdt_setprop_u32(dtb, offs, "#size-cells", 0);
360780dd2b3SJavier Almansa Sobrino 	if (err < 0) {
361780dd2b3SJavier Almansa Sobrino 		ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
362780dd2b3SJavier Almansa Sobrino 			"#size-cells", offs);
363780dd2b3SJavier Almansa Sobrino 		return err;
364780dd2b3SJavier Almansa Sobrino 	}
365780dd2b3SJavier Almansa Sobrino 
366780dd2b3SJavier Almansa Sobrino 	/*
367780dd2b3SJavier Almansa Sobrino 	 * Populate the node with the CPUs.
368780dd2b3SJavier Almansa Sobrino 	 * As libfdt prepends subnodes within a node, reverse the index count
369780dd2b3SJavier Almansa Sobrino 	 * so the CPU nodes would be better ordered.
370780dd2b3SJavier Almansa Sobrino 	 */
371780dd2b3SJavier Almansa Sobrino 	for (i = afflv2; i > 0U; i--) {
372780dd2b3SJavier Almansa Sobrino 		for (j = afflv1; j > 0U; j--) {
373780dd2b3SJavier Almansa Sobrino 			for (k = afflv0; k > 0U; k--) {
374780dd2b3SJavier Almansa Sobrino 				mpidr = ((i - 1) << MPIDR_AFF2_SHIFT) |
375780dd2b3SJavier Almansa Sobrino 					((j - 1) << MPIDR_AFF1_SHIFT) |
376780dd2b3SJavier Almansa Sobrino 					((k - 1) << MPIDR_AFF0_SHIFT) |
377780dd2b3SJavier Almansa Sobrino 					(read_mpidr_el1() & MPIDR_MT_MASK);
378780dd2b3SJavier Almansa Sobrino 
379780dd2b3SJavier Almansa Sobrino 				cpuid = plat_core_pos_by_mpidr(mpidr);
380780dd2b3SJavier Almansa Sobrino 				if (cpuid >= 0) {
381780dd2b3SJavier Almansa Sobrino 					/* Valid MPID found */
382780dd2b3SJavier Almansa Sobrino 					err = fdt_add_cpu(dtb, offs, mpidr);
383780dd2b3SJavier Almansa Sobrino 					if (err < 0) {
384780dd2b3SJavier Almansa Sobrino 						ERROR ("FDT: %s 0x%08x\n",
385780dd2b3SJavier Almansa Sobrino 							"error adding CPU",
386780dd2b3SJavier Almansa Sobrino 							(uint32_t)mpidr);
387780dd2b3SJavier Almansa Sobrino 						return err;
388780dd2b3SJavier Almansa Sobrino 					}
389780dd2b3SJavier Almansa Sobrino 				}
390780dd2b3SJavier Almansa Sobrino 			}
391780dd2b3SJavier Almansa Sobrino 		}
392780dd2b3SJavier Almansa Sobrino 	}
393780dd2b3SJavier Almansa Sobrino 
394780dd2b3SJavier Almansa Sobrino 	return offs;
395780dd2b3SJavier Almansa Sobrino }
3969f7bab42SAndre Przywara 
397*2b2b5657SSamuel Holland /*******************************************************************************
398*2b2b5657SSamuel Holland  * fdt_add_cpu_idle_states() - add PSCI CPU idle states to cpu nodes in the DT
399*2b2b5657SSamuel Holland  * @dtb:	pointer to the device tree blob in memory
400*2b2b5657SSamuel Holland  * @states:	array of idle state descriptions, ending with empty element
401*2b2b5657SSamuel Holland  *
402*2b2b5657SSamuel Holland  * Add information about CPU idle states to the devicetree. This function
403*2b2b5657SSamuel Holland  * assumes that CPU idle states are not already present in the devicetree, and
404*2b2b5657SSamuel Holland  * that all CPU states are equally applicable to all CPUs.
405*2b2b5657SSamuel Holland  *
406*2b2b5657SSamuel Holland  * See arm/idle-states.yaml and arm/psci.yaml in the (Linux kernel) DT binding
407*2b2b5657SSamuel Holland  * documentation for more details.
408*2b2b5657SSamuel Holland  *
409*2b2b5657SSamuel Holland  * Return: 0 on success, a negative error value otherwise.
410*2b2b5657SSamuel Holland  ******************************************************************************/
411*2b2b5657SSamuel Holland int fdt_add_cpu_idle_states(void *dtb, const struct psci_cpu_idle_state *state)
412*2b2b5657SSamuel Holland {
413*2b2b5657SSamuel Holland 	int cpu_node, cpus_node, idle_states_node, ret;
414*2b2b5657SSamuel Holland 	uint32_t count, phandle;
415*2b2b5657SSamuel Holland 
416*2b2b5657SSamuel Holland 	ret = fdt_find_max_phandle(dtb, &phandle);
417*2b2b5657SSamuel Holland 	phandle++;
418*2b2b5657SSamuel Holland 	if (ret < 0) {
419*2b2b5657SSamuel Holland 		return ret;
420*2b2b5657SSamuel Holland 	}
421*2b2b5657SSamuel Holland 
422*2b2b5657SSamuel Holland 	cpus_node = fdt_path_offset(dtb, "/cpus");
423*2b2b5657SSamuel Holland 	if (cpus_node < 0) {
424*2b2b5657SSamuel Holland 		return cpus_node;
425*2b2b5657SSamuel Holland 	}
426*2b2b5657SSamuel Holland 
427*2b2b5657SSamuel Holland 	/* Create the idle-states node and its child nodes. */
428*2b2b5657SSamuel Holland 	idle_states_node = fdt_add_subnode(dtb, cpus_node, "idle-states");
429*2b2b5657SSamuel Holland 	if (idle_states_node < 0) {
430*2b2b5657SSamuel Holland 		return idle_states_node;
431*2b2b5657SSamuel Holland 	}
432*2b2b5657SSamuel Holland 
433*2b2b5657SSamuel Holland 	ret = fdt_setprop_string(dtb, idle_states_node, "entry-method", "psci");
434*2b2b5657SSamuel Holland 	if (ret < 0) {
435*2b2b5657SSamuel Holland 		return ret;
436*2b2b5657SSamuel Holland 	}
437*2b2b5657SSamuel Holland 
438*2b2b5657SSamuel Holland 	for (count = 0U; state->name != NULL; count++, phandle++, state++) {
439*2b2b5657SSamuel Holland 		int idle_state_node;
440*2b2b5657SSamuel Holland 
441*2b2b5657SSamuel Holland 		idle_state_node = fdt_add_subnode(dtb, idle_states_node,
442*2b2b5657SSamuel Holland 						  state->name);
443*2b2b5657SSamuel Holland 		if (idle_state_node < 0) {
444*2b2b5657SSamuel Holland 			return idle_state_node;
445*2b2b5657SSamuel Holland 		}
446*2b2b5657SSamuel Holland 
447*2b2b5657SSamuel Holland 		fdt_setprop_string(dtb, idle_state_node, "compatible",
448*2b2b5657SSamuel Holland 				   "arm,idle-state");
449*2b2b5657SSamuel Holland 		fdt_setprop_u32(dtb, idle_state_node, "arm,psci-suspend-param",
450*2b2b5657SSamuel Holland 				state->power_state);
451*2b2b5657SSamuel Holland 		if (state->local_timer_stop) {
452*2b2b5657SSamuel Holland 			fdt_setprop_empty(dtb, idle_state_node,
453*2b2b5657SSamuel Holland 					  "local-timer-stop");
454*2b2b5657SSamuel Holland 		}
455*2b2b5657SSamuel Holland 		fdt_setprop_u32(dtb, idle_state_node, "entry-latency-us",
456*2b2b5657SSamuel Holland 				state->entry_latency_us);
457*2b2b5657SSamuel Holland 		fdt_setprop_u32(dtb, idle_state_node, "exit-latency-us",
458*2b2b5657SSamuel Holland 				state->exit_latency_us);
459*2b2b5657SSamuel Holland 		fdt_setprop_u32(dtb, idle_state_node, "min-residency-us",
460*2b2b5657SSamuel Holland 				state->min_residency_us);
461*2b2b5657SSamuel Holland 		if (state->wakeup_latency_us) {
462*2b2b5657SSamuel Holland 			fdt_setprop_u32(dtb, idle_state_node,
463*2b2b5657SSamuel Holland 					"wakeup-latency-us",
464*2b2b5657SSamuel Holland 					state->wakeup_latency_us);
465*2b2b5657SSamuel Holland 		}
466*2b2b5657SSamuel Holland 		fdt_setprop_u32(dtb, idle_state_node, "phandle", phandle);
467*2b2b5657SSamuel Holland 	}
468*2b2b5657SSamuel Holland 
469*2b2b5657SSamuel Holland 	if (count == 0U) {
470*2b2b5657SSamuel Holland 		return 0;
471*2b2b5657SSamuel Holland 	}
472*2b2b5657SSamuel Holland 
473*2b2b5657SSamuel Holland 	/* Link each cpu node to the idle state nodes. */
474*2b2b5657SSamuel Holland 	fdt_for_each_subnode(cpu_node, dtb, cpus_node) {
475*2b2b5657SSamuel Holland 		const char *device_type;
476*2b2b5657SSamuel Holland 		fdt32_t *value;
477*2b2b5657SSamuel Holland 
478*2b2b5657SSamuel Holland 		/* Only process child nodes with device_type = "cpu". */
479*2b2b5657SSamuel Holland 		device_type = fdt_getprop(dtb, cpu_node, "device_type", NULL);
480*2b2b5657SSamuel Holland 		if (device_type == NULL || strcmp(device_type, "cpu") != 0) {
481*2b2b5657SSamuel Holland 			continue;
482*2b2b5657SSamuel Holland 		}
483*2b2b5657SSamuel Holland 
484*2b2b5657SSamuel Holland 		/* Allocate space for the list of phandles. */
485*2b2b5657SSamuel Holland 		ret = fdt_setprop_placeholder(dtb, cpu_node, "cpu-idle-states",
486*2b2b5657SSamuel Holland 					      count * sizeof(phandle),
487*2b2b5657SSamuel Holland 					      (void **)&value);
488*2b2b5657SSamuel Holland 		if (ret < 0) {
489*2b2b5657SSamuel Holland 			return ret;
490*2b2b5657SSamuel Holland 		}
491*2b2b5657SSamuel Holland 
492*2b2b5657SSamuel Holland 		/* Fill in the phandles of the idle state nodes. */
493*2b2b5657SSamuel Holland 		for (uint32_t i = 0U; i < count; ++i) {
494*2b2b5657SSamuel Holland 			value[i] = cpu_to_fdt32(phandle - count + i);
495*2b2b5657SSamuel Holland 		}
496*2b2b5657SSamuel Holland 	}
497*2b2b5657SSamuel Holland 
498*2b2b5657SSamuel Holland 	return 0;
499*2b2b5657SSamuel Holland }
500*2b2b5657SSamuel Holland 
5019f7bab42SAndre Przywara /**
5029f7bab42SAndre Przywara  * fdt_adjust_gic_redist() - Adjust GICv3 redistributor size
5039f7bab42SAndre Przywara  * @dtb: Pointer to the DT blob in memory
5049f7bab42SAndre Przywara  * @nr_cores: Number of CPU cores on this system.
5054d585fe5SAndre Przywara  * @gicr_base: Base address of the first GICR frame, or ~0 if unchanged
5069f7bab42SAndre Przywara  * @gicr_frame_size: Size of the GICR frame per core
5079f7bab42SAndre Przywara  *
5089f7bab42SAndre Przywara  * On a GICv3 compatible interrupt controller, the redistributor provides
5099f7bab42SAndre Przywara  * a number of 64k pages per each supported core. So with a dynamic topology,
5109f7bab42SAndre Przywara  * this size cannot be known upfront and thus can't be hardcoded into the DTB.
5119f7bab42SAndre Przywara  *
5129f7bab42SAndre Przywara  * Find the DT node describing the GICv3 interrupt controller, and adjust
5139f7bab42SAndre Przywara  * the size of the redistributor to match the number of actual cores on
5149f7bab42SAndre Przywara  * this system.
5159f7bab42SAndre Przywara  * A GICv4 compatible redistributor uses four 64K pages per core, whereas GICs
5169f7bab42SAndre Przywara  * without support for direct injection of virtual interrupts use two 64K pages.
5179f7bab42SAndre Przywara  * The @gicr_frame_size parameter should be 262144 and 131072, respectively.
5184d585fe5SAndre Przywara  * Also optionally allow adjusting the GICR frame base address, when this is
5194d585fe5SAndre Przywara  * different due to ITS frames between distributor and redistributor.
5209f7bab42SAndre Przywara  *
5219f7bab42SAndre Przywara  * Return: 0 on success, negative error value otherwise.
5229f7bab42SAndre Przywara  */
5239f7bab42SAndre Przywara int fdt_adjust_gic_redist(void *dtb, unsigned int nr_cores,
5244d585fe5SAndre Przywara 			  uintptr_t gicr_base, unsigned int gicr_frame_size)
5259f7bab42SAndre Przywara {
5269f7bab42SAndre Przywara 	int offset = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-v3");
5274d585fe5SAndre Przywara 	uint64_t reg_64;
5284d585fe5SAndre Przywara 	uint32_t reg_32;
5299f7bab42SAndre Przywara 	void *val;
5304d585fe5SAndre Przywara 	int parent, ret;
5319f7bab42SAndre Przywara 	int ac, sc;
5329f7bab42SAndre Przywara 
5339f7bab42SAndre Przywara 	if (offset < 0) {
5349f7bab42SAndre Przywara 		return offset;
5359f7bab42SAndre Przywara 	}
5369f7bab42SAndre Przywara 
5379f7bab42SAndre Przywara 	parent = fdt_parent_offset(dtb, offset);
5389f7bab42SAndre Przywara 	if (parent < 0) {
5399f7bab42SAndre Przywara 		return parent;
5409f7bab42SAndre Przywara 	}
5419f7bab42SAndre Przywara 	ac = fdt_address_cells(dtb, parent);
5429f7bab42SAndre Przywara 	sc = fdt_size_cells(dtb, parent);
5439f7bab42SAndre Przywara 	if (ac < 0 || sc < 0) {
5449f7bab42SAndre Przywara 		return -EINVAL;
5459f7bab42SAndre Przywara 	}
5469f7bab42SAndre Przywara 
5474d585fe5SAndre Przywara 	if (gicr_base != INVALID_BASE_ADDR) {
5484d585fe5SAndre Przywara 		if (ac == 1) {
5494d585fe5SAndre Przywara 			reg_32 = cpu_to_fdt32(gicr_base);
5504d585fe5SAndre Przywara 			val = &reg_32;
5519f7bab42SAndre Przywara 		} else {
5524d585fe5SAndre Przywara 			reg_64 = cpu_to_fdt64(gicr_base);
5534d585fe5SAndre Przywara 			val = &reg_64;
5544d585fe5SAndre Przywara 		}
5554d585fe5SAndre Przywara 		/*
5564d585fe5SAndre Przywara 		 * The redistributor base address is the second address in
5574d585fe5SAndre Przywara 		 * the "reg" entry, so we have to skip one address and one
5584d585fe5SAndre Przywara 		 * size cell.
5594d585fe5SAndre Przywara 		 */
5604d585fe5SAndre Przywara 		ret = fdt_setprop_inplace_namelen_partial(dtb, offset,
5614d585fe5SAndre Przywara 							  "reg", 3,
5624d585fe5SAndre Przywara 							  (ac + sc) * 4,
5634d585fe5SAndre Przywara 							  val, ac * 4);
5644d585fe5SAndre Przywara 		if (ret < 0) {
5654d585fe5SAndre Przywara 			return ret;
5664d585fe5SAndre Przywara 		}
5674d585fe5SAndre Przywara 	}
5684d585fe5SAndre Przywara 
5694d585fe5SAndre Przywara 	if (sc == 1) {
5704d585fe5SAndre Przywara 		reg_32 = cpu_to_fdt32(nr_cores * gicr_frame_size);
5714d585fe5SAndre Przywara 		val = &reg_32;
5724d585fe5SAndre Przywara 	} else {
5734d585fe5SAndre Przywara 		reg_64 = cpu_to_fdt64(nr_cores * (uint64_t)gicr_frame_size);
5744d585fe5SAndre Przywara 		val = &reg_64;
5759f7bab42SAndre Przywara 	}
5769f7bab42SAndre Przywara 
5779f7bab42SAndre Przywara 	/*
5789f7bab42SAndre Przywara 	 * The redistributor is described in the second "reg" entry.
5799f7bab42SAndre Przywara 	 * So we have to skip one address and one size cell, then another
5809f7bab42SAndre Przywara 	 * address cell to get to the second size cell.
5819f7bab42SAndre Przywara 	 */
5829f7bab42SAndre Przywara 	return fdt_setprop_inplace_namelen_partial(dtb, offset, "reg", 3,
5839f7bab42SAndre Przywara 						   (ac + sc + ac) * 4,
5849f7bab42SAndre Przywara 						   val, sc * 4);
5859f7bab42SAndre Przywara }
586