blob: e88a5500800a3873aaaec086d3f8782e155f5680 [file] [log] [blame]
Andre Przywaraffbacb02019-07-10 17:27:17 +01001/*
Javier Almansa Sobrino40f49842020-08-25 16:16:29 +01002 * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
Andre Przywaraffbacb02019-07-10 17:27:17 +01003 *
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.
Andre Przywara83fc8392019-07-15 09:00:23 +010012 * 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.
Andre Przywaraffbacb02019-07-10 17:27:17 +010015 */
16
Javier Almansa Sobrino40f49842020-08-25 16:16:29 +010017#include <errno.h>
18#include <stdio.h>
Andre Przywaraffbacb02019-07-10 17:27:17 +010019#include <string.h>
20
21#include <libfdt.h>
22
Javier Almansa Sobrino40f49842020-08-25 16:16:29 +010023#include <arch.h>
Andre Przywaraffbacb02019-07-10 17:27:17 +010024#include <common/debug.h>
Javier Almansa Sobrino40f49842020-08-25 16:16:29 +010025#include <common/fdt_fixup.h>
26#include <common/fdt_wrappers.h>
Andre Przywaraffbacb02019-07-10 17:27:17 +010027#include <drivers/console.h>
28#include <lib/psci/psci.h>
Javier Almansa Sobrino40f49842020-08-25 16:16:29 +010029#include <plat/common/platform.h>
Andre Przywaraffbacb02019-07-10 17:27:17 +010030
Andre Przywaraffbacb02019-07-10 17:27:17 +010031
32static int append_psci_compatible(void *fdt, int offs, const char *str)
33{
34 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
35}
36
Andre Przywara68855602019-09-19 10:55:25 +010037/*
38 * Those defines are for PSCI v0.1 legacy clients, which we expect to use
39 * the same execution state (AArch32/AArch64) as TF-A.
40 * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2.
41 */
42#ifdef __aarch64__
43#define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH64
44#define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH64
45#else
46#define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH32
47#define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH32
48#endif
49
Andre Przywara16e64192019-09-19 10:45:28 +010050/*******************************************************************************
51 * dt_add_psci_node() - Add a PSCI node into an existing device tree
52 * @fdt: pointer to the device tree blob in memory
53 *
54 * Add a device tree node describing PSCI into the root level of an existing
55 * device tree blob in memory.
56 * This will add v0.1, v0.2 and v1.0 compatible strings and the standard
57 * function IDs for v0.1 compatibility.
58 * An existing PSCI node will not be touched, the function will return success
59 * in this case. This function will not touch the /cpus enable methods, use
60 * dt_add_psci_cpu_enable_methods() for that.
61 *
62 * Return: 0 on success, -1 otherwise.
63 ******************************************************************************/
Andre Przywaraffbacb02019-07-10 17:27:17 +010064int dt_add_psci_node(void *fdt)
65{
66 int offs;
67
68 if (fdt_path_offset(fdt, "/psci") >= 0) {
69 WARN("PSCI Device Tree node already exists!\n");
70 return 0;
71 }
72
73 offs = fdt_path_offset(fdt, "/");
74 if (offs < 0)
75 return -1;
76 offs = fdt_add_subnode(fdt, offs, "psci");
77 if (offs < 0)
78 return -1;
79 if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
80 return -1;
81 if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
82 return -1;
83 if (append_psci_compatible(fdt, offs, "arm,psci"))
84 return -1;
85 if (fdt_setprop_string(fdt, offs, "method", "smc"))
86 return -1;
Andre Przywara68855602019-09-19 10:55:25 +010087 if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID))
Andre Przywaraffbacb02019-07-10 17:27:17 +010088 return -1;
89 if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
90 return -1;
Andre Przywara68855602019-09-19 10:55:25 +010091 if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID))
Andre Przywaraffbacb02019-07-10 17:27:17 +010092 return -1;
93 return 0;
94}
95
96/*
97 * Find the first subnode that has a "device_type" property with the value
98 * "cpu" and which's enable-method is not "psci" (yet).
99 * Returns 0 if no such subnode is found, so all have already been patched
100 * or none have to be patched in the first place.
101 * Returns 1 if *one* such subnode has been found and successfully changed
102 * to "psci".
Andre Przywarab768a1b2019-09-16 16:50:57 +0100103 * Returns negative values on error.
Andre Przywaraffbacb02019-07-10 17:27:17 +0100104 *
105 * Call in a loop until it returns 0. Recalculate the node offset after
106 * it has returned 1.
107 */
108static int dt_update_one_cpu_node(void *fdt, int offset)
109{
110 int offs;
111
112 /* Iterate over all subnodes to find those with device_type = "cpu". */
113 for (offs = fdt_first_subnode(fdt, offset); offs >= 0;
114 offs = fdt_next_subnode(fdt, offs)) {
115 const char *prop;
116 int len;
Andre Przywarab768a1b2019-09-16 16:50:57 +0100117 int ret;
Andre Przywaraffbacb02019-07-10 17:27:17 +0100118
119 prop = fdt_getprop(fdt, offs, "device_type", &len);
Andre Przywarab768a1b2019-09-16 16:50:57 +0100120 if (prop == NULL)
Andre Przywaraffbacb02019-07-10 17:27:17 +0100121 continue;
Andre Przywarab768a1b2019-09-16 16:50:57 +0100122 if ((strcmp(prop, "cpu") != 0) || (len != 4))
Andre Przywaraffbacb02019-07-10 17:27:17 +0100123 continue;
124
125 /* Ignore any nodes which already use "psci". */
126 prop = fdt_getprop(fdt, offs, "enable-method", &len);
Andre Przywarab768a1b2019-09-16 16:50:57 +0100127 if ((prop != NULL) &&
128 (strcmp(prop, "psci") == 0) && (len == 5))
Andre Przywaraffbacb02019-07-10 17:27:17 +0100129 continue;
130
Andre Przywarab768a1b2019-09-16 16:50:57 +0100131 ret = fdt_setprop_string(fdt, offs, "enable-method", "psci");
132 if (ret < 0)
133 return ret;
Andre Przywaraffbacb02019-07-10 17:27:17 +0100134 /*
135 * Subnode found and patched.
136 * Restart to accommodate potentially changed offsets.
137 */
138 return 1;
139 }
140
141 if (offs == -FDT_ERR_NOTFOUND)
142 return 0;
143
144 return offs;
145}
146
Andre Przywara16e64192019-09-19 10:45:28 +0100147/*******************************************************************************
148 * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI
149 * @fdt: pointer to the device tree blob in memory
150 *
151 * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change
152 * the enable-method to PSCI. This will add the enable-method properties, if
153 * required, or will change existing properties to read "psci".
154 *
155 * Return: 0 on success, or a negative error value otherwise.
156 ******************************************************************************/
157
Andre Przywaraffbacb02019-07-10 17:27:17 +0100158int dt_add_psci_cpu_enable_methods(void *fdt)
159{
160 int offs, ret;
161
162 do {
163 offs = fdt_path_offset(fdt, "/cpus");
164 if (offs < 0)
165 return offs;
166
167 ret = dt_update_one_cpu_node(fdt, offs);
168 } while (ret > 0);
169
170 return ret;
171}
Andre Przywara83fc8392019-07-15 09:00:23 +0100172
173#define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0)
174
Andre Przywara16e64192019-09-19 10:45:28 +0100175/*******************************************************************************
176 * fdt_add_reserved_memory() - reserve (secure) memory regions in DT
177 * @dtb: pointer to the device tree blob in memory
178 * @node_name: name of the subnode to be used
179 * @base: physical base address of the reserved region
180 * @size: size of the reserved region
181 *
182 * Add a region of memory to the /reserved-memory node in a device tree in
183 * memory, creating that node if required. Each region goes into a subnode
184 * of that node and has a @node_name, a @base address and a @size.
185 * This will prevent any device tree consumer from using that memory. It
186 * can be used to announce secure memory regions, as it adds the "no-map"
187 * property to prevent mapping and speculative operations on that region.
188 *
189 * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding
190 * documentation for details.
191 *
192 * Return: 0 on success, a negative error value otherwise.
193 ******************************************************************************/
Andre Przywara83fc8392019-07-15 09:00:23 +0100194int fdt_add_reserved_memory(void *dtb, const char *node_name,
195 uintptr_t base, size_t size)
196{
197 int offs = fdt_path_offset(dtb, "/reserved-memory");
198 uint32_t addresses[3];
199
200 if (offs < 0) { /* create if not existing yet */
201 offs = fdt_add_subnode(dtb, 0, "reserved-memory");
202 if (offs < 0)
203 return offs;
204 fdt_setprop_u32(dtb, offs, "#address-cells", 2);
205 fdt_setprop_u32(dtb, offs, "#size-cells", 1);
206 fdt_setprop(dtb, offs, "ranges", NULL, 0);
207 }
208
209 addresses[0] = cpu_to_fdt32(HIGH_BITS(base));
210 addresses[1] = cpu_to_fdt32(base & 0xffffffff);
211 addresses[2] = cpu_to_fdt32(size & 0xffffffff);
212 offs = fdt_add_subnode(dtb, offs, node_name);
213 fdt_setprop(dtb, offs, "no-map", NULL, 0);
214 fdt_setprop(dtb, offs, "reg", addresses, 12);
215
216 return 0;
217}
Javier Almansa Sobrino40f49842020-08-25 16:16:29 +0100218
219/*******************************************************************************
220 * fdt_add_cpu() Add a new CPU node to the DT
221 * @dtb: Pointer to the device tree blob in memory
222 * @parent: Offset of the parent node
223 * @mpidr: MPIDR for the current CPU
224 *
225 * Create and add a new cpu node to a DTB.
226 *
227 * Return the offset of the new node or a negative value in case of error
228 ******************************************************************************/
229
230static int fdt_add_cpu(void *dtb, int parent, u_register_t mpidr)
231{
232 int cpu_offs;
233 int err;
234 char snode_name[15];
235 uint64_t reg_prop;
236
237 reg_prop = mpidr & MPID_MASK & ~MPIDR_MT_MASK;
238
239 snprintf(snode_name, sizeof(snode_name), "cpu@%x",
240 (unsigned int)reg_prop);
241
242 cpu_offs = fdt_add_subnode(dtb, parent, snode_name);
243 if (cpu_offs < 0) {
244 ERROR ("FDT: add subnode \"%s\" failed: %i\n",
245 snode_name, cpu_offs);
246 return cpu_offs;
247 }
248
249 err = fdt_setprop_string(dtb, cpu_offs, "compatible", "arm,armv8");
250 if (err < 0) {
251 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
252 "compatible", cpu_offs);
253 return err;
254 }
255
256 err = fdt_setprop_u64(dtb, cpu_offs, "reg", reg_prop);
257 if (err < 0) {
258 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
259 "reg", cpu_offs);
260 return err;
261 }
262
263 err = fdt_setprop_string(dtb, cpu_offs, "device_type", "cpu");
264 if (err < 0) {
265 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
266 "device_type", cpu_offs);
267 return err;
268 }
269
270 err = fdt_setprop_string(dtb, cpu_offs, "enable-method", "psci");
271 if (err < 0) {
272 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
273 "enable-method", cpu_offs);
274 return err;
275 }
276
277 return cpu_offs;
278}
279
280/******************************************************************************
281 * fdt_add_cpus_node() - Add the cpus node to the DTB
282 * @dtb: pointer to the device tree blob in memory
283 * @afflv0: Maximum number of threads per core (affinity level 0).
284 * @afflv1: Maximum number of CPUs per cluster (affinity level 1).
285 * @afflv2: Maximum number of clusters (affinity level 2).
286 *
287 * Iterate over all the possible MPIDs given the maximum affinity levels and
288 * add a cpus node to the DTB with all the valid CPUs on the system.
289 * If there is already a /cpus node, exit gracefully
290 *
291 * A system with two CPUs would generate a node equivalent or similar to:
292 *
293 * cpus {
294 * #address-cells = <2>;
295 * #size-cells = <0>;
296 *
297 * cpu0: cpu@0 {
298 * compatible = "arm,armv8";
299 * reg = <0x0 0x0>;
300 * device_type = "cpu";
301 * enable-method = "psci";
302 * };
303 * cpu1: cpu@10000 {
304 * compatible = "arm,armv8";
305 * reg = <0x0 0x100>;
306 * device_type = "cpu";
307 * enable-method = "psci";
308 * };
309 * };
310 *
311 * Full documentation about the CPU bindings can be found at:
312 * https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/cpus.txt
313 *
314 * Return the offset of the node or a negative value on error.
315 ******************************************************************************/
316
317int fdt_add_cpus_node(void *dtb, unsigned int afflv0,
318 unsigned int afflv1, unsigned int afflv2)
319{
320 int offs;
321 int err;
322 unsigned int i, j, k;
323 u_register_t mpidr;
324 int cpuid;
325
326 if (fdt_path_offset(dtb, "/cpus") >= 0) {
327 return -EEXIST;
328 }
329
330 offs = fdt_add_subnode(dtb, 0, "cpus");
331 if (offs < 0) {
332 ERROR ("FDT: add subnode \"cpus\" node to parent node failed");
333 return offs;
334 }
335
336 err = fdt_setprop_u32(dtb, offs, "#address-cells", 2);
337 if (err < 0) {
338 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
339 "#address-cells", offs);
340 return err;
341 }
342
343 err = fdt_setprop_u32(dtb, offs, "#size-cells", 0);
344 if (err < 0) {
345 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n",
346 "#size-cells", offs);
347 return err;
348 }
349
350 /*
351 * Populate the node with the CPUs.
352 * As libfdt prepends subnodes within a node, reverse the index count
353 * so the CPU nodes would be better ordered.
354 */
355 for (i = afflv2; i > 0U; i--) {
356 for (j = afflv1; j > 0U; j--) {
357 for (k = afflv0; k > 0U; k--) {
358 mpidr = ((i - 1) << MPIDR_AFF2_SHIFT) |
359 ((j - 1) << MPIDR_AFF1_SHIFT) |
360 ((k - 1) << MPIDR_AFF0_SHIFT) |
361 (read_mpidr_el1() & MPIDR_MT_MASK);
362
363 cpuid = plat_core_pos_by_mpidr(mpidr);
364 if (cpuid >= 0) {
365 /* Valid MPID found */
366 err = fdt_add_cpu(dtb, offs, mpidr);
367 if (err < 0) {
368 ERROR ("FDT: %s 0x%08x\n",
369 "error adding CPU",
370 (uint32_t)mpidr);
371 return err;
372 }
373 }
374 }
375 }
376 }
377
378 return offs;
379}
Andre Przywara64b9e142020-08-24 18:28:44 +0100380
381/**
382 * fdt_adjust_gic_redist() - Adjust GICv3 redistributor size
383 * @dtb: Pointer to the DT blob in memory
384 * @nr_cores: Number of CPU cores on this system.
385 * @gicr_frame_size: Size of the GICR frame per core
386 *
387 * On a GICv3 compatible interrupt controller, the redistributor provides
388 * a number of 64k pages per each supported core. So with a dynamic topology,
389 * this size cannot be known upfront and thus can't be hardcoded into the DTB.
390 *
391 * Find the DT node describing the GICv3 interrupt controller, and adjust
392 * the size of the redistributor to match the number of actual cores on
393 * this system.
394 * A GICv4 compatible redistributor uses four 64K pages per core, whereas GICs
395 * without support for direct injection of virtual interrupts use two 64K pages.
396 * The @gicr_frame_size parameter should be 262144 and 131072, respectively.
397 *
398 * Return: 0 on success, negative error value otherwise.
399 */
400int fdt_adjust_gic_redist(void *dtb, unsigned int nr_cores,
401 unsigned int gicr_frame_size)
402{
403 int offset = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-v3");
404 uint64_t redist_size_64;
405 uint32_t redist_size_32;
406 void *val;
407 int parent;
408 int ac, sc;
409
410 if (offset < 0) {
411 return offset;
412 }
413
414 parent = fdt_parent_offset(dtb, offset);
415 if (parent < 0) {
416 return parent;
417 }
418 ac = fdt_address_cells(dtb, parent);
419 sc = fdt_size_cells(dtb, parent);
420 if (ac < 0 || sc < 0) {
421 return -EINVAL;
422 }
423
424 if (sc == 1) {
425 redist_size_32 = cpu_to_fdt32(nr_cores * gicr_frame_size);
426 val = &redist_size_32;
427 } else {
Andre Przywaraf3310a52020-10-07 11:09:42 +0100428 redist_size_64 = cpu_to_fdt64(nr_cores *
429 (uint64_t)gicr_frame_size);
Andre Przywara64b9e142020-08-24 18:28:44 +0100430 val = &redist_size_64;
431 }
432
433 /*
434 * The redistributor is described in the second "reg" entry.
435 * So we have to skip one address and one size cell, then another
436 * address cell to get to the second size cell.
437 */
438 return fdt_setprop_inplace_namelen_partial(dtb, offset, "reg", 3,
439 (ac + sc + ac) * 4,
440 val, sc * 4);
441}