blob: 99d0eee9829c506b9e96eb46f02e97f9a8298afb [file] [log] [blame]
Andre Przywaraffbacb02019-07-10 17:27:17 +01001/*
2 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
3 *
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
17#include <string.h>
18
19#include <libfdt.h>
20
21#include <common/debug.h>
22#include <drivers/console.h>
23#include <lib/psci/psci.h>
24
25#include <common/fdt_fixup.h>
26
27static int append_psci_compatible(void *fdt, int offs, const char *str)
28{
29 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
30}
31
Andre Przywara68855602019-09-19 10:55:25 +010032/*
33 * Those defines are for PSCI v0.1 legacy clients, which we expect to use
34 * the same execution state (AArch32/AArch64) as TF-A.
35 * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2.
36 */
37#ifdef __aarch64__
38#define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH64
39#define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH64
40#else
41#define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH32
42#define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH32
43#endif
44
Andre Przywara16e64192019-09-19 10:45:28 +010045/*******************************************************************************
46 * dt_add_psci_node() - Add a PSCI node into an existing device tree
47 * @fdt: pointer to the device tree blob in memory
48 *
49 * Add a device tree node describing PSCI into the root level of an existing
50 * device tree blob in memory.
51 * This will add v0.1, v0.2 and v1.0 compatible strings and the standard
52 * function IDs for v0.1 compatibility.
53 * An existing PSCI node will not be touched, the function will return success
54 * in this case. This function will not touch the /cpus enable methods, use
55 * dt_add_psci_cpu_enable_methods() for that.
56 *
57 * Return: 0 on success, -1 otherwise.
58 ******************************************************************************/
Andre Przywaraffbacb02019-07-10 17:27:17 +010059int dt_add_psci_node(void *fdt)
60{
61 int offs;
62
63 if (fdt_path_offset(fdt, "/psci") >= 0) {
64 WARN("PSCI Device Tree node already exists!\n");
65 return 0;
66 }
67
68 offs = fdt_path_offset(fdt, "/");
69 if (offs < 0)
70 return -1;
71 offs = fdt_add_subnode(fdt, offs, "psci");
72 if (offs < 0)
73 return -1;
74 if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
75 return -1;
76 if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
77 return -1;
78 if (append_psci_compatible(fdt, offs, "arm,psci"))
79 return -1;
80 if (fdt_setprop_string(fdt, offs, "method", "smc"))
81 return -1;
Andre Przywara68855602019-09-19 10:55:25 +010082 if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID))
Andre Przywaraffbacb02019-07-10 17:27:17 +010083 return -1;
84 if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
85 return -1;
Andre Przywara68855602019-09-19 10:55:25 +010086 if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID))
Andre Przywaraffbacb02019-07-10 17:27:17 +010087 return -1;
88 return 0;
89}
90
91/*
92 * Find the first subnode that has a "device_type" property with the value
93 * "cpu" and which's enable-method is not "psci" (yet).
94 * Returns 0 if no such subnode is found, so all have already been patched
95 * or none have to be patched in the first place.
96 * Returns 1 if *one* such subnode has been found and successfully changed
97 * to "psci".
98 * Returns -1 on error.
99 *
100 * Call in a loop until it returns 0. Recalculate the node offset after
101 * it has returned 1.
102 */
103static int dt_update_one_cpu_node(void *fdt, int offset)
104{
105 int offs;
106
107 /* Iterate over all subnodes to find those with device_type = "cpu". */
108 for (offs = fdt_first_subnode(fdt, offset); offs >= 0;
109 offs = fdt_next_subnode(fdt, offs)) {
110 const char *prop;
111 int len;
112
113 prop = fdt_getprop(fdt, offs, "device_type", &len);
114 if (!prop)
115 continue;
116 if (memcmp(prop, "cpu", 4) != 0 || len != 4)
117 continue;
118
119 /* Ignore any nodes which already use "psci". */
120 prop = fdt_getprop(fdt, offs, "enable-method", &len);
121 if (prop && memcmp(prop, "psci", 5) == 0 && len == 5)
122 continue;
123
124 if (fdt_setprop_string(fdt, offs, "enable-method", "psci"))
125 return -1;
126 /*
127 * Subnode found and patched.
128 * Restart to accommodate potentially changed offsets.
129 */
130 return 1;
131 }
132
133 if (offs == -FDT_ERR_NOTFOUND)
134 return 0;
135
136 return offs;
137}
138
Andre Przywara16e64192019-09-19 10:45:28 +0100139/*******************************************************************************
140 * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI
141 * @fdt: pointer to the device tree blob in memory
142 *
143 * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change
144 * the enable-method to PSCI. This will add the enable-method properties, if
145 * required, or will change existing properties to read "psci".
146 *
147 * Return: 0 on success, or a negative error value otherwise.
148 ******************************************************************************/
149
Andre Przywaraffbacb02019-07-10 17:27:17 +0100150int dt_add_psci_cpu_enable_methods(void *fdt)
151{
152 int offs, ret;
153
154 do {
155 offs = fdt_path_offset(fdt, "/cpus");
156 if (offs < 0)
157 return offs;
158
159 ret = dt_update_one_cpu_node(fdt, offs);
160 } while (ret > 0);
161
162 return ret;
163}
Andre Przywara83fc8392019-07-15 09:00:23 +0100164
165#define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0)
166
Andre Przywara16e64192019-09-19 10:45:28 +0100167/*******************************************************************************
168 * fdt_add_reserved_memory() - reserve (secure) memory regions in DT
169 * @dtb: pointer to the device tree blob in memory
170 * @node_name: name of the subnode to be used
171 * @base: physical base address of the reserved region
172 * @size: size of the reserved region
173 *
174 * Add a region of memory to the /reserved-memory node in a device tree in
175 * memory, creating that node if required. Each region goes into a subnode
176 * of that node and has a @node_name, a @base address and a @size.
177 * This will prevent any device tree consumer from using that memory. It
178 * can be used to announce secure memory regions, as it adds the "no-map"
179 * property to prevent mapping and speculative operations on that region.
180 *
181 * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding
182 * documentation for details.
183 *
184 * Return: 0 on success, a negative error value otherwise.
185 ******************************************************************************/
Andre Przywara83fc8392019-07-15 09:00:23 +0100186int fdt_add_reserved_memory(void *dtb, const char *node_name,
187 uintptr_t base, size_t size)
188{
189 int offs = fdt_path_offset(dtb, "/reserved-memory");
190 uint32_t addresses[3];
191
192 if (offs < 0) { /* create if not existing yet */
193 offs = fdt_add_subnode(dtb, 0, "reserved-memory");
194 if (offs < 0)
195 return offs;
196 fdt_setprop_u32(dtb, offs, "#address-cells", 2);
197 fdt_setprop_u32(dtb, offs, "#size-cells", 1);
198 fdt_setprop(dtb, offs, "ranges", NULL, 0);
199 }
200
201 addresses[0] = cpu_to_fdt32(HIGH_BITS(base));
202 addresses[1] = cpu_to_fdt32(base & 0xffffffff);
203 addresses[2] = cpu_to_fdt32(size & 0xffffffff);
204 offs = fdt_add_subnode(dtb, offs, node_name);
205 fdt_setprop(dtb, offs, "no-map", NULL, 0);
206 fdt_setprop(dtb, offs, "reg", addresses, 12);
207
208 return 0;
209}