xref: /arm-trusted-firmware/plat/arm/board/fvp/fconf/fconf_hw_config_getter.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2020-2023, Arm Limited. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <inttypes.h>
9*91f16700Schasinglulu #include <stdint.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <common/debug.h>
12*91f16700Schasinglulu #include <common/fdt_wrappers.h>
13*91f16700Schasinglulu #include <fconf_hw_config_getter.h>
14*91f16700Schasinglulu #include <libfdt.h>
15*91f16700Schasinglulu #include <plat/common/platform.h>
16*91f16700Schasinglulu 
17*91f16700Schasinglulu struct gicv3_config_t gicv3_config;
18*91f16700Schasinglulu struct hw_topology_t soc_topology;
19*91f16700Schasinglulu struct uart_serial_config_t uart_serial_config;
20*91f16700Schasinglulu struct cpu_timer_t cpu_timer;
21*91f16700Schasinglulu struct ns_dram_layout dram_layout;
22*91f16700Schasinglulu 
23*91f16700Schasinglulu /*
24*91f16700Schasinglulu  * Each NS DRAM bank entry is 'reg' node property which is
25*91f16700Schasinglulu  * a sequence of (address, length) pairs of 32-bit values.
26*91f16700Schasinglulu  */
27*91f16700Schasinglulu #define DRAM_ENTRY_SIZE		(4UL * sizeof(uint32_t))
28*91f16700Schasinglulu 
29*91f16700Schasinglulu CASSERT(ARM_DRAM_NUM_BANKS == 2UL, ARM_DRAM_NUM_BANKS_mismatch);
30*91f16700Schasinglulu 
31*91f16700Schasinglulu #define ILLEGAL_ADDR	ULL(~0)
32*91f16700Schasinglulu 
33*91f16700Schasinglulu int fconf_populate_gicv3_config(uintptr_t config)
34*91f16700Schasinglulu {
35*91f16700Schasinglulu 	int err;
36*91f16700Schasinglulu 	int node;
37*91f16700Schasinglulu 	uintptr_t addr;
38*91f16700Schasinglulu 
39*91f16700Schasinglulu 	/* Necessary to work with libfdt APIs */
40*91f16700Schasinglulu 	const void *hw_config_dtb = (const void *)config;
41*91f16700Schasinglulu 
42*91f16700Schasinglulu 	/*
43*91f16700Schasinglulu 	 * Find the offset of the node containing "arm,gic-v3" compatible property.
44*91f16700Schasinglulu 	 * Populating fconf strucutures dynamically is not supported for legacy
45*91f16700Schasinglulu 	 * systems which use GICv2 IP. Simply skip extracting GIC properties.
46*91f16700Schasinglulu 	 */
47*91f16700Schasinglulu 	node = fdt_node_offset_by_compatible(hw_config_dtb, -1, "arm,gic-v3");
48*91f16700Schasinglulu 	if (node < 0) {
49*91f16700Schasinglulu 		WARN("FCONF: Unable to locate node with arm,gic-v3 compatible property\n");
50*91f16700Schasinglulu 		return 0;
51*91f16700Schasinglulu 	}
52*91f16700Schasinglulu 	/* The GICv3 DT binding holds at least two address/size pairs,
53*91f16700Schasinglulu 	 * the first describing the distributor, the second the redistributors.
54*91f16700Schasinglulu 	 * See: bindings/interrupt-controller/arm,gic-v3.yaml
55*91f16700Schasinglulu 	 */
56*91f16700Schasinglulu 	err = fdt_get_reg_props_by_index(hw_config_dtb, node, 0, &addr, NULL);
57*91f16700Schasinglulu 	if (err < 0) {
58*91f16700Schasinglulu 		ERROR("FCONF: Failed to read GICD reg property of GIC node\n");
59*91f16700Schasinglulu 		return err;
60*91f16700Schasinglulu 	}
61*91f16700Schasinglulu 	gicv3_config.gicd_base = addr;
62*91f16700Schasinglulu 
63*91f16700Schasinglulu 	err = fdt_get_reg_props_by_index(hw_config_dtb, node, 1, &addr, NULL);
64*91f16700Schasinglulu 	if (err < 0) {
65*91f16700Schasinglulu 		ERROR("FCONF: Failed to read GICR reg property of GIC node\n");
66*91f16700Schasinglulu 	} else {
67*91f16700Schasinglulu 		gicv3_config.gicr_base = addr;
68*91f16700Schasinglulu 	}
69*91f16700Schasinglulu 
70*91f16700Schasinglulu 	return err;
71*91f16700Schasinglulu }
72*91f16700Schasinglulu 
73*91f16700Schasinglulu int fconf_populate_topology(uintptr_t config)
74*91f16700Schasinglulu {
75*91f16700Schasinglulu 	int err, node, cluster_node, core_node, thread_node;
76*91f16700Schasinglulu 	uint32_t cluster_count = 0, max_cpu_per_cluster = 0, total_cpu_count = 0;
77*91f16700Schasinglulu 	uint32_t max_pwr_lvl = 0;
78*91f16700Schasinglulu 
79*91f16700Schasinglulu 	/* Necessary to work with libfdt APIs */
80*91f16700Schasinglulu 	const void *hw_config_dtb = (const void *)config;
81*91f16700Schasinglulu 
82*91f16700Schasinglulu 	/* Find the offset of the node containing "arm,psci-1.0" compatible property */
83*91f16700Schasinglulu 	node = fdt_node_offset_by_compatible(hw_config_dtb, -1, "arm,psci-1.0");
84*91f16700Schasinglulu 	if (node < 0) {
85*91f16700Schasinglulu 		ERROR("FCONF: Unable to locate node with arm,psci-1.0 compatible property\n");
86*91f16700Schasinglulu 		return node;
87*91f16700Schasinglulu 	}
88*91f16700Schasinglulu 
89*91f16700Schasinglulu 	err = fdt_read_uint32(hw_config_dtb, node, "max-pwr-lvl", &max_pwr_lvl);
90*91f16700Schasinglulu 	if (err < 0) {
91*91f16700Schasinglulu 		/*
92*91f16700Schasinglulu 		 * Some legacy FVP dts may not have this property. Assign the default
93*91f16700Schasinglulu 		 * value.
94*91f16700Schasinglulu 		 */
95*91f16700Schasinglulu 		WARN("FCONF: Could not locate max-pwr-lvl property\n");
96*91f16700Schasinglulu 		max_pwr_lvl = 2;
97*91f16700Schasinglulu 	}
98*91f16700Schasinglulu 
99*91f16700Schasinglulu 	assert(max_pwr_lvl <= MPIDR_AFFLVL2);
100*91f16700Schasinglulu 
101*91f16700Schasinglulu 	/* Find the offset of the "cpus" node */
102*91f16700Schasinglulu 	node = fdt_path_offset(hw_config_dtb, "/cpus");
103*91f16700Schasinglulu 	if (node < 0) {
104*91f16700Schasinglulu 		ERROR("FCONF: Node '%s' not found in hardware configuration dtb\n", "cpus");
105*91f16700Schasinglulu 		return node;
106*91f16700Schasinglulu 	}
107*91f16700Schasinglulu 
108*91f16700Schasinglulu 	/* A typical cpu-map node in a device tree is shown here for reference
109*91f16700Schasinglulu 	cpu-map {
110*91f16700Schasinglulu 		cluster0 {
111*91f16700Schasinglulu 			core0 {
112*91f16700Schasinglulu 				cpu = <&CPU0>;
113*91f16700Schasinglulu 			};
114*91f16700Schasinglulu 			core1 {
115*91f16700Schasinglulu 				cpu = <&CPU1>;
116*91f16700Schasinglulu 			};
117*91f16700Schasinglulu 		};
118*91f16700Schasinglulu 
119*91f16700Schasinglulu 		cluster1 {
120*91f16700Schasinglulu 			core0 {
121*91f16700Schasinglulu 				cpu = <&CPU2>;
122*91f16700Schasinglulu 			};
123*91f16700Schasinglulu 			core1 {
124*91f16700Schasinglulu 				cpu = <&CPU3>;
125*91f16700Schasinglulu 			};
126*91f16700Schasinglulu 		};
127*91f16700Schasinglulu 	};
128*91f16700Schasinglulu 	*/
129*91f16700Schasinglulu 
130*91f16700Schasinglulu 	/* Locate the cpu-map child node */
131*91f16700Schasinglulu 	node = fdt_subnode_offset(hw_config_dtb, node, "cpu-map");
132*91f16700Schasinglulu 	if (node < 0) {
133*91f16700Schasinglulu 		ERROR("FCONF: Node '%s' not found in hardware configuration dtb\n", "cpu-map");
134*91f16700Schasinglulu 		return node;
135*91f16700Schasinglulu 	}
136*91f16700Schasinglulu 
137*91f16700Schasinglulu 	uint32_t cpus_per_cluster[PLAT_ARM_CLUSTER_COUNT] = {0};
138*91f16700Schasinglulu 
139*91f16700Schasinglulu 	/* Iterate through cluster nodes */
140*91f16700Schasinglulu 	fdt_for_each_subnode(cluster_node, hw_config_dtb, node) {
141*91f16700Schasinglulu 		assert(cluster_count < PLAT_ARM_CLUSTER_COUNT);
142*91f16700Schasinglulu 
143*91f16700Schasinglulu 		/* Iterate through core nodes */
144*91f16700Schasinglulu 		fdt_for_each_subnode(core_node, hw_config_dtb, cluster_node) {
145*91f16700Schasinglulu 			/* core nodes may have child nodes i.e., "thread" nodes */
146*91f16700Schasinglulu 			if (fdt_first_subnode(hw_config_dtb, core_node) < 0) {
147*91f16700Schasinglulu 				cpus_per_cluster[cluster_count]++;
148*91f16700Schasinglulu 			} else {
149*91f16700Schasinglulu 				/* Multi-threaded CPU description is found in dtb */
150*91f16700Schasinglulu 				fdt_for_each_subnode(thread_node, hw_config_dtb, core_node) {
151*91f16700Schasinglulu 					cpus_per_cluster[cluster_count]++;
152*91f16700Schasinglulu 				}
153*91f16700Schasinglulu 
154*91f16700Schasinglulu 				/* Since in some dtbs, core nodes may not have thread node,
155*91f16700Schasinglulu 				 * no need to error if even one child node is not found.
156*91f16700Schasinglulu 				 */
157*91f16700Schasinglulu 			}
158*91f16700Schasinglulu 		}
159*91f16700Schasinglulu 
160*91f16700Schasinglulu 		/* Ensure every cluster node has at least 1 child node */
161*91f16700Schasinglulu 		if (cpus_per_cluster[cluster_count] < 1U) {
162*91f16700Schasinglulu 			ERROR("FCONF: Unable to locate the core node in cluster %d\n", cluster_count);
163*91f16700Schasinglulu 			return -1;
164*91f16700Schasinglulu 		}
165*91f16700Schasinglulu 
166*91f16700Schasinglulu 		VERBOSE("CLUSTER ID: %d cpu-count: %d\n", cluster_count,
167*91f16700Schasinglulu 					cpus_per_cluster[cluster_count]);
168*91f16700Schasinglulu 
169*91f16700Schasinglulu 		/* Find the maximum number of cpus in any cluster */
170*91f16700Schasinglulu 		max_cpu_per_cluster = MAX(max_cpu_per_cluster, cpus_per_cluster[cluster_count]);
171*91f16700Schasinglulu 		total_cpu_count += cpus_per_cluster[cluster_count];
172*91f16700Schasinglulu 		cluster_count++;
173*91f16700Schasinglulu 	}
174*91f16700Schasinglulu 
175*91f16700Schasinglulu 
176*91f16700Schasinglulu 	/* At least one cluster node is expected in hardware configuration dtb */
177*91f16700Schasinglulu 	if (cluster_count < 1U) {
178*91f16700Schasinglulu 		ERROR("FCONF: Unable to locate the cluster node in cpu-map node\n");
179*91f16700Schasinglulu 		return -1;
180*91f16700Schasinglulu 	}
181*91f16700Schasinglulu 
182*91f16700Schasinglulu 	soc_topology.plat_max_pwr_level = max_pwr_lvl;
183*91f16700Schasinglulu 	soc_topology.plat_cluster_count = cluster_count;
184*91f16700Schasinglulu 	soc_topology.cluster_cpu_count = max_cpu_per_cluster;
185*91f16700Schasinglulu 	soc_topology.plat_cpu_count = total_cpu_count;
186*91f16700Schasinglulu 
187*91f16700Schasinglulu 	return 0;
188*91f16700Schasinglulu }
189*91f16700Schasinglulu 
190*91f16700Schasinglulu int fconf_populate_uart_config(uintptr_t config)
191*91f16700Schasinglulu {
192*91f16700Schasinglulu 	int uart_node, node, err;
193*91f16700Schasinglulu 	uintptr_t addr;
194*91f16700Schasinglulu 	const char *path;
195*91f16700Schasinglulu 	uint32_t phandle;
196*91f16700Schasinglulu 	uint64_t translated_addr;
197*91f16700Schasinglulu 
198*91f16700Schasinglulu 	/* Necessary to work with libfdt APIs */
199*91f16700Schasinglulu 	const void *hw_config_dtb = (const void *)config;
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	/*
202*91f16700Schasinglulu 	 * uart child node is indirectly referenced through its path which is
203*91f16700Schasinglulu 	 * specified in the `serial1` property of the "aliases" node.
204*91f16700Schasinglulu 	 * Note that TF-A boot console is mapped to serial0 while runtime
205*91f16700Schasinglulu 	 * console is mapped to serial1.
206*91f16700Schasinglulu 	 */
207*91f16700Schasinglulu 
208*91f16700Schasinglulu 	path = fdt_get_alias(hw_config_dtb, "serial1");
209*91f16700Schasinglulu 	if (path == NULL) {
210*91f16700Schasinglulu 		ERROR("FCONF: Could not read serial1 property in aliases node\n");
211*91f16700Schasinglulu 		return -1;
212*91f16700Schasinglulu 	}
213*91f16700Schasinglulu 
214*91f16700Schasinglulu 	/* Find the offset of the uart serial node */
215*91f16700Schasinglulu 	uart_node = fdt_path_offset(hw_config_dtb, path);
216*91f16700Schasinglulu 	if (uart_node < 0) {
217*91f16700Schasinglulu 		ERROR("FCONF: Failed to locate uart serial node using its path\n");
218*91f16700Schasinglulu 		return -1;
219*91f16700Schasinglulu 	}
220*91f16700Schasinglulu 
221*91f16700Schasinglulu 	/* uart serial node has its offset and size of address in reg property */
222*91f16700Schasinglulu 	err = fdt_get_reg_props_by_index(hw_config_dtb, uart_node, 0, &addr,
223*91f16700Schasinglulu 						NULL);
224*91f16700Schasinglulu 	if (err < 0) {
225*91f16700Schasinglulu 		ERROR("FCONF: Failed to read reg property of '%s' node\n",
226*91f16700Schasinglulu 			"uart serial");
227*91f16700Schasinglulu 		return err;
228*91f16700Schasinglulu 	}
229*91f16700Schasinglulu 	VERBOSE("FCONF: UART node address: %lx\n", addr);
230*91f16700Schasinglulu 
231*91f16700Schasinglulu 	/*
232*91f16700Schasinglulu 	 * Perform address translation of local device address to CPU address
233*91f16700Schasinglulu 	 * domain.
234*91f16700Schasinglulu 	 */
235*91f16700Schasinglulu 	translated_addr = fdtw_translate_address(hw_config_dtb,
236*91f16700Schasinglulu 						 uart_node, (uint64_t)addr);
237*91f16700Schasinglulu 	if (translated_addr == ILLEGAL_ADDR) {
238*91f16700Schasinglulu 		ERROR("FCONF: failed to translate UART node base address");
239*91f16700Schasinglulu 		return -1;
240*91f16700Schasinglulu 	}
241*91f16700Schasinglulu 
242*91f16700Schasinglulu 	uart_serial_config.uart_base = translated_addr;
243*91f16700Schasinglulu 
244*91f16700Schasinglulu 	VERBOSE("FCONF: UART serial device base address: %" PRIx64 "\n",
245*91f16700Schasinglulu 		uart_serial_config.uart_base);
246*91f16700Schasinglulu 
247*91f16700Schasinglulu 	/*
248*91f16700Schasinglulu 	 * The phandle of the DT node which captures the clock info of uart
249*91f16700Schasinglulu 	 * serial node is specified in the "clocks" property.
250*91f16700Schasinglulu 	 */
251*91f16700Schasinglulu 	err = fdt_read_uint32(hw_config_dtb, uart_node, "clocks", &phandle);
252*91f16700Schasinglulu 	if (err < 0) {
253*91f16700Schasinglulu 		ERROR("FCONF: Could not read clocks property in uart serial node\n");
254*91f16700Schasinglulu 		return err;
255*91f16700Schasinglulu 	}
256*91f16700Schasinglulu 
257*91f16700Schasinglulu 	node = fdt_node_offset_by_phandle(hw_config_dtb, phandle);
258*91f16700Schasinglulu 	if (node < 0) {
259*91f16700Schasinglulu 		ERROR("FCONF: Failed to locate clk node using its path\n");
260*91f16700Schasinglulu 		return node;
261*91f16700Schasinglulu 	}
262*91f16700Schasinglulu 
263*91f16700Schasinglulu 	/*
264*91f16700Schasinglulu 	 * Retrieve clock frequency. We assume clock provider generates a fixed
265*91f16700Schasinglulu 	 * clock.
266*91f16700Schasinglulu 	 */
267*91f16700Schasinglulu 	err = fdt_read_uint32(hw_config_dtb, node, "clock-frequency",
268*91f16700Schasinglulu 				&uart_serial_config.uart_clk);
269*91f16700Schasinglulu 	if (err < 0) {
270*91f16700Schasinglulu 		ERROR("FCONF: Could not read clock-frequency property in clk node\n");
271*91f16700Schasinglulu 		return err;
272*91f16700Schasinglulu 	}
273*91f16700Schasinglulu 
274*91f16700Schasinglulu 	VERBOSE("FCONF: UART serial device clk frequency: %x\n",
275*91f16700Schasinglulu 		uart_serial_config.uart_clk);
276*91f16700Schasinglulu 
277*91f16700Schasinglulu 	return 0;
278*91f16700Schasinglulu }
279*91f16700Schasinglulu 
280*91f16700Schasinglulu int fconf_populate_cpu_timer(uintptr_t config)
281*91f16700Schasinglulu {
282*91f16700Schasinglulu 	int err, node;
283*91f16700Schasinglulu 
284*91f16700Schasinglulu 	/* Necessary to work with libfdt APIs */
285*91f16700Schasinglulu 	const void *hw_config_dtb = (const void *)config;
286*91f16700Schasinglulu 
287*91f16700Schasinglulu 	/* Find the node offset point to "arm,armv8-timer" compatible property,
288*91f16700Schasinglulu 	 * a per-core architected timer attached to a GIC to deliver its per-processor
289*91f16700Schasinglulu 	 * interrupts via PPIs */
290*91f16700Schasinglulu 	node = fdt_node_offset_by_compatible(hw_config_dtb, -1, "arm,armv8-timer");
291*91f16700Schasinglulu 	if (node < 0) {
292*91f16700Schasinglulu 		ERROR("FCONF: Unrecognized hardware configuration dtb (%d)\n", node);
293*91f16700Schasinglulu 		return node;
294*91f16700Schasinglulu 	}
295*91f16700Schasinglulu 
296*91f16700Schasinglulu 	/* Locate the cell holding the clock-frequency, an optional field */
297*91f16700Schasinglulu 	err = fdt_read_uint32(hw_config_dtb, node, "clock-frequency", &cpu_timer.clock_freq);
298*91f16700Schasinglulu 	if (err < 0) {
299*91f16700Schasinglulu 		WARN("FCONF failed to read clock-frequency property\n");
300*91f16700Schasinglulu 	}
301*91f16700Schasinglulu 
302*91f16700Schasinglulu 	return 0;
303*91f16700Schasinglulu }
304*91f16700Schasinglulu 
305*91f16700Schasinglulu int fconf_populate_dram_layout(uintptr_t config)
306*91f16700Schasinglulu {
307*91f16700Schasinglulu 	int node, len;
308*91f16700Schasinglulu 	const uint32_t *reg;
309*91f16700Schasinglulu 
310*91f16700Schasinglulu 	/* Necessary to work with libfdt APIs */
311*91f16700Schasinglulu 	const void *hw_config_dtb = (const void *)config;
312*91f16700Schasinglulu 
313*91f16700Schasinglulu 	/* Find 'memory' node */
314*91f16700Schasinglulu 	node = fdt_node_offset_by_prop_value(hw_config_dtb, -1, "device_type",
315*91f16700Schasinglulu 					     "memory", sizeof("memory"));
316*91f16700Schasinglulu 	if (node < 0) {
317*91f16700Schasinglulu 		WARN("FCONF: Unable to locate 'memory' node\n");
318*91f16700Schasinglulu 		return node;
319*91f16700Schasinglulu 	}
320*91f16700Schasinglulu 
321*91f16700Schasinglulu 	reg = fdt_getprop(hw_config_dtb, node, "reg", &len);
322*91f16700Schasinglulu 	if (reg == NULL) {
323*91f16700Schasinglulu 		ERROR("FCONF failed to read 'reg' property\n");
324*91f16700Schasinglulu 		return len;
325*91f16700Schasinglulu 	}
326*91f16700Schasinglulu 
327*91f16700Schasinglulu 	switch (len) {
328*91f16700Schasinglulu 	case DRAM_ENTRY_SIZE:
329*91f16700Schasinglulu 		/* 1 DRAM bank */
330*91f16700Schasinglulu 		dram_layout.num_banks = 1UL;
331*91f16700Schasinglulu 		break;
332*91f16700Schasinglulu 	case 2UL * DRAM_ENTRY_SIZE:
333*91f16700Schasinglulu 		/* 2 DRAM banks */
334*91f16700Schasinglulu 		dram_layout.num_banks = 2UL;
335*91f16700Schasinglulu 		break;
336*91f16700Schasinglulu 	default:
337*91f16700Schasinglulu 		ERROR("FCONF: Invalid 'memory' node\n");
338*91f16700Schasinglulu 		return -FDT_ERR_BADLAYOUT;
339*91f16700Schasinglulu 	}
340*91f16700Schasinglulu 
341*91f16700Schasinglulu 	for (unsigned long i = 0UL; i < dram_layout.num_banks; i++) {
342*91f16700Schasinglulu 		int err = fdt_get_reg_props_by_index(
343*91f16700Schasinglulu 				hw_config_dtb, node, (int)i,
344*91f16700Schasinglulu 				&dram_layout.dram_bank[i].base,
345*91f16700Schasinglulu 				(size_t *)&dram_layout.dram_bank[i].size);
346*91f16700Schasinglulu 		if (err < 0) {
347*91f16700Schasinglulu 			ERROR("FCONF: Failed to read 'reg' property #%lu of 'memory' node\n", i);
348*91f16700Schasinglulu 			return err;
349*91f16700Schasinglulu 		}
350*91f16700Schasinglulu 	}
351*91f16700Schasinglulu 
352*91f16700Schasinglulu 	return 0;
353*91f16700Schasinglulu }
354*91f16700Schasinglulu 
355*91f16700Schasinglulu FCONF_REGISTER_POPULATOR(HW_CONFIG, gicv3_config, fconf_populate_gicv3_config);
356*91f16700Schasinglulu FCONF_REGISTER_POPULATOR(HW_CONFIG, topology, fconf_populate_topology);
357*91f16700Schasinglulu FCONF_REGISTER_POPULATOR(HW_CONFIG, uart_config, fconf_populate_uart_config);
358*91f16700Schasinglulu FCONF_REGISTER_POPULATOR(HW_CONFIG, cpu_timer, fconf_populate_cpu_timer);
359*91f16700Schasinglulu FCONF_REGISTER_POPULATOR(HW_CONFIG, dram_layout, fconf_populate_dram_layout);
360