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 int dt_add_psci_node(void *fdt) 33 { 34 int offs; 35 36 if (fdt_path_offset(fdt, "/psci") >= 0) { 37 WARN("PSCI Device Tree node already exists!\n"); 38 return 0; 39 } 40 41 offs = fdt_path_offset(fdt, "/"); 42 if (offs < 0) 43 return -1; 44 offs = fdt_add_subnode(fdt, offs, "psci"); 45 if (offs < 0) 46 return -1; 47 if (append_psci_compatible(fdt, offs, "arm,psci-1.0")) 48 return -1; 49 if (append_psci_compatible(fdt, offs, "arm,psci-0.2")) 50 return -1; 51 if (append_psci_compatible(fdt, offs, "arm,psci")) 52 return -1; 53 if (fdt_setprop_string(fdt, offs, "method", "smc")) 54 return -1; 55 if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_AARCH64)) 56 return -1; 57 if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF)) 58 return -1; 59 if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_AARCH64)) 60 return -1; 61 if (fdt_setprop_u32(fdt, offs, "sys_poweroff", PSCI_SYSTEM_OFF)) 62 return -1; 63 if (fdt_setprop_u32(fdt, offs, "sys_reset", PSCI_SYSTEM_RESET)) 64 return -1; 65 return 0; 66 } 67 68 /* 69 * Find the first subnode that has a "device_type" property with the value 70 * "cpu" and which's enable-method is not "psci" (yet). 71 * Returns 0 if no such subnode is found, so all have already been patched 72 * or none have to be patched in the first place. 73 * Returns 1 if *one* such subnode has been found and successfully changed 74 * to "psci". 75 * Returns -1 on error. 76 * 77 * Call in a loop until it returns 0. Recalculate the node offset after 78 * it has returned 1. 79 */ 80 static int dt_update_one_cpu_node(void *fdt, int offset) 81 { 82 int offs; 83 84 /* Iterate over all subnodes to find those with device_type = "cpu". */ 85 for (offs = fdt_first_subnode(fdt, offset); offs >= 0; 86 offs = fdt_next_subnode(fdt, offs)) { 87 const char *prop; 88 int len; 89 90 prop = fdt_getprop(fdt, offs, "device_type", &len); 91 if (!prop) 92 continue; 93 if (memcmp(prop, "cpu", 4) != 0 || len != 4) 94 continue; 95 96 /* Ignore any nodes which already use "psci". */ 97 prop = fdt_getprop(fdt, offs, "enable-method", &len); 98 if (prop && memcmp(prop, "psci", 5) == 0 && len == 5) 99 continue; 100 101 if (fdt_setprop_string(fdt, offs, "enable-method", "psci")) 102 return -1; 103 /* 104 * Subnode found and patched. 105 * Restart to accommodate potentially changed offsets. 106 */ 107 return 1; 108 } 109 110 if (offs == -FDT_ERR_NOTFOUND) 111 return 0; 112 113 return offs; 114 } 115 116 int dt_add_psci_cpu_enable_methods(void *fdt) 117 { 118 int offs, ret; 119 120 do { 121 offs = fdt_path_offset(fdt, "/cpus"); 122 if (offs < 0) 123 return offs; 124 125 ret = dt_update_one_cpu_node(fdt, offs); 126 } while (ret > 0); 127 128 return ret; 129 } 130 131 #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0) 132 133 int fdt_add_reserved_memory(void *dtb, const char *node_name, 134 uintptr_t base, size_t size) 135 { 136 int offs = fdt_path_offset(dtb, "/reserved-memory"); 137 uint32_t addresses[3]; 138 139 if (offs < 0) { /* create if not existing yet */ 140 offs = fdt_add_subnode(dtb, 0, "reserved-memory"); 141 if (offs < 0) 142 return offs; 143 fdt_setprop_u32(dtb, offs, "#address-cells", 2); 144 fdt_setprop_u32(dtb, offs, "#size-cells", 1); 145 fdt_setprop(dtb, offs, "ranges", NULL, 0); 146 } 147 148 addresses[0] = cpu_to_fdt32(HIGH_BITS(base)); 149 addresses[1] = cpu_to_fdt32(base & 0xffffffff); 150 addresses[2] = cpu_to_fdt32(size & 0xffffffff); 151 offs = fdt_add_subnode(dtb, offs, node_name); 152 fdt_setprop(dtb, offs, "no-map", NULL, 0); 153 fdt_setprop(dtb, offs, "reg", addresses, 12); 154 155 return 0; 156 } 157