1 /* 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. 12 * 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. 15 */ 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 27 static 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 32 /******************************************************************************* 33 * dt_add_psci_node() - Add a PSCI node into an existing device tree 34 * @fdt: pointer to the device tree blob in memory 35 * 36 * Add a device tree node describing PSCI into the root level of an existing 37 * device tree blob in memory. 38 * This will add v0.1, v0.2 and v1.0 compatible strings and the standard 39 * function IDs for v0.1 compatibility. 40 * An existing PSCI node will not be touched, the function will return success 41 * in this case. This function will not touch the /cpus enable methods, use 42 * dt_add_psci_cpu_enable_methods() for that. 43 * 44 * Return: 0 on success, -1 otherwise. 45 ******************************************************************************/ 46 int dt_add_psci_node(void *fdt) 47 { 48 int offs; 49 50 if (fdt_path_offset(fdt, "/psci") >= 0) { 51 WARN("PSCI Device Tree node already exists!\n"); 52 return 0; 53 } 54 55 offs = fdt_path_offset(fdt, "/"); 56 if (offs < 0) 57 return -1; 58 offs = fdt_add_subnode(fdt, offs, "psci"); 59 if (offs < 0) 60 return -1; 61 if (append_psci_compatible(fdt, offs, "arm,psci-1.0")) 62 return -1; 63 if (append_psci_compatible(fdt, offs, "arm,psci-0.2")) 64 return -1; 65 if (append_psci_compatible(fdt, offs, "arm,psci")) 66 return -1; 67 if (fdt_setprop_string(fdt, offs, "method", "smc")) 68 return -1; 69 if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_AARCH64)) 70 return -1; 71 if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF)) 72 return -1; 73 if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_AARCH64)) 74 return -1; 75 if (fdt_setprop_u32(fdt, offs, "sys_poweroff", PSCI_SYSTEM_OFF)) 76 return -1; 77 if (fdt_setprop_u32(fdt, offs, "sys_reset", PSCI_SYSTEM_RESET)) 78 return -1; 79 return 0; 80 } 81 82 /* 83 * Find the first subnode that has a "device_type" property with the value 84 * "cpu" and which's enable-method is not "psci" (yet). 85 * Returns 0 if no such subnode is found, so all have already been patched 86 * or none have to be patched in the first place. 87 * Returns 1 if *one* such subnode has been found and successfully changed 88 * to "psci". 89 * Returns -1 on error. 90 * 91 * Call in a loop until it returns 0. Recalculate the node offset after 92 * it has returned 1. 93 */ 94 static int dt_update_one_cpu_node(void *fdt, int offset) 95 { 96 int offs; 97 98 /* Iterate over all subnodes to find those with device_type = "cpu". */ 99 for (offs = fdt_first_subnode(fdt, offset); offs >= 0; 100 offs = fdt_next_subnode(fdt, offs)) { 101 const char *prop; 102 int len; 103 104 prop = fdt_getprop(fdt, offs, "device_type", &len); 105 if (!prop) 106 continue; 107 if (memcmp(prop, "cpu", 4) != 0 || len != 4) 108 continue; 109 110 /* Ignore any nodes which already use "psci". */ 111 prop = fdt_getprop(fdt, offs, "enable-method", &len); 112 if (prop && memcmp(prop, "psci", 5) == 0 && len == 5) 113 continue; 114 115 if (fdt_setprop_string(fdt, offs, "enable-method", "psci")) 116 return -1; 117 /* 118 * Subnode found and patched. 119 * Restart to accommodate potentially changed offsets. 120 */ 121 return 1; 122 } 123 124 if (offs == -FDT_ERR_NOTFOUND) 125 return 0; 126 127 return offs; 128 } 129 130 /******************************************************************************* 131 * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI 132 * @fdt: pointer to the device tree blob in memory 133 * 134 * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change 135 * the enable-method to PSCI. This will add the enable-method properties, if 136 * required, or will change existing properties to read "psci". 137 * 138 * Return: 0 on success, or a negative error value otherwise. 139 ******************************************************************************/ 140 141 int dt_add_psci_cpu_enable_methods(void *fdt) 142 { 143 int offs, ret; 144 145 do { 146 offs = fdt_path_offset(fdt, "/cpus"); 147 if (offs < 0) 148 return offs; 149 150 ret = dt_update_one_cpu_node(fdt, offs); 151 } while (ret > 0); 152 153 return ret; 154 } 155 156 #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0) 157 158 /******************************************************************************* 159 * fdt_add_reserved_memory() - reserve (secure) memory regions in DT 160 * @dtb: pointer to the device tree blob in memory 161 * @node_name: name of the subnode to be used 162 * @base: physical base address of the reserved region 163 * @size: size of the reserved region 164 * 165 * Add a region of memory to the /reserved-memory node in a device tree in 166 * memory, creating that node if required. Each region goes into a subnode 167 * of that node and has a @node_name, a @base address and a @size. 168 * This will prevent any device tree consumer from using that memory. It 169 * can be used to announce secure memory regions, as it adds the "no-map" 170 * property to prevent mapping and speculative operations on that region. 171 * 172 * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding 173 * documentation for details. 174 * 175 * Return: 0 on success, a negative error value otherwise. 176 ******************************************************************************/ 177 int fdt_add_reserved_memory(void *dtb, const char *node_name, 178 uintptr_t base, size_t size) 179 { 180 int offs = fdt_path_offset(dtb, "/reserved-memory"); 181 uint32_t addresses[3]; 182 183 if (offs < 0) { /* create if not existing yet */ 184 offs = fdt_add_subnode(dtb, 0, "reserved-memory"); 185 if (offs < 0) 186 return offs; 187 fdt_setprop_u32(dtb, offs, "#address-cells", 2); 188 fdt_setprop_u32(dtb, offs, "#size-cells", 1); 189 fdt_setprop(dtb, offs, "ranges", NULL, 0); 190 } 191 192 addresses[0] = cpu_to_fdt32(HIGH_BITS(base)); 193 addresses[1] = cpu_to_fdt32(base & 0xffffffff); 194 addresses[2] = cpu_to_fdt32(size & 0xffffffff); 195 offs = fdt_add_subnode(dtb, offs, node_name); 196 fdt_setprop(dtb, offs, "no-map", NULL, 0); 197 fdt_setprop(dtb, offs, "reg", addresses, 12); 198 199 return 0; 200 } 201