1f240728bSAndre Przywara /* 2f240728bSAndre Przywara * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved. 3f240728bSAndre Przywara * 4f240728bSAndre Przywara * SPDX-License-Identifier: BSD-3-Clause 5f240728bSAndre Przywara */ 6f240728bSAndre Przywara 7f240728bSAndre Przywara /* 8f240728bSAndre Przywara * Contains generic routines to fix up the device tree blob passed on to 9f240728bSAndre Przywara * payloads like BL32 and BL33 (and further down the boot chain). 10f240728bSAndre Przywara * This allows to easily add PSCI nodes, when the original DT does not have 11f240728bSAndre Przywara * it or advertises another method. 123ef45ddaSAndre Przywara * Also it supports to add reserved memory nodes to describe memory that 133ef45ddaSAndre Przywara * is used by the secure world, so that non-secure software avoids using 143ef45ddaSAndre Przywara * that. 15f240728bSAndre Przywara */ 16f240728bSAndre Przywara 17f240728bSAndre Przywara #include <string.h> 18f240728bSAndre Przywara 19f240728bSAndre Przywara #include <libfdt.h> 20f240728bSAndre Przywara 21f240728bSAndre Przywara #include <common/debug.h> 22f240728bSAndre Przywara #include <drivers/console.h> 23f240728bSAndre Przywara #include <lib/psci/psci.h> 24f240728bSAndre Przywara 25f240728bSAndre Przywara #include <common/fdt_fixup.h> 26f240728bSAndre Przywara 27f240728bSAndre Przywara static int append_psci_compatible(void *fdt, int offs, const char *str) 28f240728bSAndre Przywara { 29f240728bSAndre Przywara return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1); 30f240728bSAndre Przywara } 31f240728bSAndre Przywara 32*66799507SAndre Przywara /* 33*66799507SAndre Przywara * Those defines are for PSCI v0.1 legacy clients, which we expect to use 34*66799507SAndre Przywara * the same execution state (AArch32/AArch64) as TF-A. 35*66799507SAndre Przywara * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2. 36*66799507SAndre Przywara */ 37*66799507SAndre Przywara #ifdef __aarch64__ 38*66799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH64 39*66799507SAndre Przywara #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH64 40*66799507SAndre Przywara #else 41*66799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH32 42*66799507SAndre Przywara #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH32 43*66799507SAndre Przywara #endif 44*66799507SAndre Przywara 456eaf928dSAndre Przywara /******************************************************************************* 466eaf928dSAndre Przywara * dt_add_psci_node() - Add a PSCI node into an existing device tree 476eaf928dSAndre Przywara * @fdt: pointer to the device tree blob in memory 486eaf928dSAndre Przywara * 496eaf928dSAndre Przywara * Add a device tree node describing PSCI into the root level of an existing 506eaf928dSAndre Przywara * device tree blob in memory. 516eaf928dSAndre Przywara * This will add v0.1, v0.2 and v1.0 compatible strings and the standard 526eaf928dSAndre Przywara * function IDs for v0.1 compatibility. 536eaf928dSAndre Przywara * An existing PSCI node will not be touched, the function will return success 546eaf928dSAndre Przywara * in this case. This function will not touch the /cpus enable methods, use 556eaf928dSAndre Przywara * dt_add_psci_cpu_enable_methods() for that. 566eaf928dSAndre Przywara * 576eaf928dSAndre Przywara * Return: 0 on success, -1 otherwise. 586eaf928dSAndre Przywara ******************************************************************************/ 59f240728bSAndre Przywara int dt_add_psci_node(void *fdt) 60f240728bSAndre Przywara { 61f240728bSAndre Przywara int offs; 62f240728bSAndre Przywara 63f240728bSAndre Przywara if (fdt_path_offset(fdt, "/psci") >= 0) { 64f240728bSAndre Przywara WARN("PSCI Device Tree node already exists!\n"); 65f240728bSAndre Przywara return 0; 66f240728bSAndre Przywara } 67f240728bSAndre Przywara 68f240728bSAndre Przywara offs = fdt_path_offset(fdt, "/"); 69f240728bSAndre Przywara if (offs < 0) 70f240728bSAndre Przywara return -1; 71f240728bSAndre Przywara offs = fdt_add_subnode(fdt, offs, "psci"); 72f240728bSAndre Przywara if (offs < 0) 73f240728bSAndre Przywara return -1; 74f240728bSAndre Przywara if (append_psci_compatible(fdt, offs, "arm,psci-1.0")) 75f240728bSAndre Przywara return -1; 76f240728bSAndre Przywara if (append_psci_compatible(fdt, offs, "arm,psci-0.2")) 77f240728bSAndre Przywara return -1; 78f240728bSAndre Przywara if (append_psci_compatible(fdt, offs, "arm,psci")) 79f240728bSAndre Przywara return -1; 80f240728bSAndre Przywara if (fdt_setprop_string(fdt, offs, "method", "smc")) 81f240728bSAndre Przywara return -1; 82*66799507SAndre Przywara if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID)) 83f240728bSAndre Przywara return -1; 84f240728bSAndre Przywara if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF)) 85f240728bSAndre Przywara return -1; 86*66799507SAndre Przywara if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID)) 87f240728bSAndre Przywara return -1; 88f240728bSAndre Przywara return 0; 89f240728bSAndre Przywara } 90f240728bSAndre Przywara 91f240728bSAndre Przywara /* 92f240728bSAndre Przywara * Find the first subnode that has a "device_type" property with the value 93f240728bSAndre Przywara * "cpu" and which's enable-method is not "psci" (yet). 94f240728bSAndre Przywara * Returns 0 if no such subnode is found, so all have already been patched 95f240728bSAndre Przywara * or none have to be patched in the first place. 96f240728bSAndre Przywara * Returns 1 if *one* such subnode has been found and successfully changed 97f240728bSAndre Przywara * to "psci". 98f240728bSAndre Przywara * Returns -1 on error. 99f240728bSAndre Przywara * 100f240728bSAndre Przywara * Call in a loop until it returns 0. Recalculate the node offset after 101f240728bSAndre Przywara * it has returned 1. 102f240728bSAndre Przywara */ 103f240728bSAndre Przywara static int dt_update_one_cpu_node(void *fdt, int offset) 104f240728bSAndre Przywara { 105f240728bSAndre Przywara int offs; 106f240728bSAndre Przywara 107f240728bSAndre Przywara /* Iterate over all subnodes to find those with device_type = "cpu". */ 108f240728bSAndre Przywara for (offs = fdt_first_subnode(fdt, offset); offs >= 0; 109f240728bSAndre Przywara offs = fdt_next_subnode(fdt, offs)) { 110f240728bSAndre Przywara const char *prop; 111f240728bSAndre Przywara int len; 112f240728bSAndre Przywara 113f240728bSAndre Przywara prop = fdt_getprop(fdt, offs, "device_type", &len); 114f240728bSAndre Przywara if (!prop) 115f240728bSAndre Przywara continue; 116f240728bSAndre Przywara if (memcmp(prop, "cpu", 4) != 0 || len != 4) 117f240728bSAndre Przywara continue; 118f240728bSAndre Przywara 119f240728bSAndre Przywara /* Ignore any nodes which already use "psci". */ 120f240728bSAndre Przywara prop = fdt_getprop(fdt, offs, "enable-method", &len); 121f240728bSAndre Przywara if (prop && memcmp(prop, "psci", 5) == 0 && len == 5) 122f240728bSAndre Przywara continue; 123f240728bSAndre Przywara 124f240728bSAndre Przywara if (fdt_setprop_string(fdt, offs, "enable-method", "psci")) 125f240728bSAndre Przywara return -1; 126f240728bSAndre Przywara /* 127f240728bSAndre Przywara * Subnode found and patched. 128f240728bSAndre Przywara * Restart to accommodate potentially changed offsets. 129f240728bSAndre Przywara */ 130f240728bSAndre Przywara return 1; 131f240728bSAndre Przywara } 132f240728bSAndre Przywara 133f240728bSAndre Przywara if (offs == -FDT_ERR_NOTFOUND) 134f240728bSAndre Przywara return 0; 135f240728bSAndre Przywara 136f240728bSAndre Przywara return offs; 137f240728bSAndre Przywara } 138f240728bSAndre Przywara 1396eaf928dSAndre Przywara /******************************************************************************* 1406eaf928dSAndre Przywara * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI 1416eaf928dSAndre Przywara * @fdt: pointer to the device tree blob in memory 1426eaf928dSAndre Przywara * 1436eaf928dSAndre Przywara * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change 1446eaf928dSAndre Przywara * the enable-method to PSCI. This will add the enable-method properties, if 1456eaf928dSAndre Przywara * required, or will change existing properties to read "psci". 1466eaf928dSAndre Przywara * 1476eaf928dSAndre Przywara * Return: 0 on success, or a negative error value otherwise. 1486eaf928dSAndre Przywara ******************************************************************************/ 1496eaf928dSAndre Przywara 150f240728bSAndre Przywara int dt_add_psci_cpu_enable_methods(void *fdt) 151f240728bSAndre Przywara { 152f240728bSAndre Przywara int offs, ret; 153f240728bSAndre Przywara 154f240728bSAndre Przywara do { 155f240728bSAndre Przywara offs = fdt_path_offset(fdt, "/cpus"); 156f240728bSAndre Przywara if (offs < 0) 157f240728bSAndre Przywara return offs; 158f240728bSAndre Przywara 159f240728bSAndre Przywara ret = dt_update_one_cpu_node(fdt, offs); 160f240728bSAndre Przywara } while (ret > 0); 161f240728bSAndre Przywara 162f240728bSAndre Przywara return ret; 163f240728bSAndre Przywara } 1643ef45ddaSAndre Przywara 1653ef45ddaSAndre Przywara #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0) 1663ef45ddaSAndre Przywara 1676eaf928dSAndre Przywara /******************************************************************************* 1686eaf928dSAndre Przywara * fdt_add_reserved_memory() - reserve (secure) memory regions in DT 1696eaf928dSAndre Przywara * @dtb: pointer to the device tree blob in memory 1706eaf928dSAndre Przywara * @node_name: name of the subnode to be used 1716eaf928dSAndre Przywara * @base: physical base address of the reserved region 1726eaf928dSAndre Przywara * @size: size of the reserved region 1736eaf928dSAndre Przywara * 1746eaf928dSAndre Przywara * Add a region of memory to the /reserved-memory node in a device tree in 1756eaf928dSAndre Przywara * memory, creating that node if required. Each region goes into a subnode 1766eaf928dSAndre Przywara * of that node and has a @node_name, a @base address and a @size. 1776eaf928dSAndre Przywara * This will prevent any device tree consumer from using that memory. It 1786eaf928dSAndre Przywara * can be used to announce secure memory regions, as it adds the "no-map" 1796eaf928dSAndre Przywara * property to prevent mapping and speculative operations on that region. 1806eaf928dSAndre Przywara * 1816eaf928dSAndre Przywara * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding 1826eaf928dSAndre Przywara * documentation for details. 1836eaf928dSAndre Przywara * 1846eaf928dSAndre Przywara * Return: 0 on success, a negative error value otherwise. 1856eaf928dSAndre Przywara ******************************************************************************/ 1863ef45ddaSAndre Przywara int fdt_add_reserved_memory(void *dtb, const char *node_name, 1873ef45ddaSAndre Przywara uintptr_t base, size_t size) 1883ef45ddaSAndre Przywara { 1893ef45ddaSAndre Przywara int offs = fdt_path_offset(dtb, "/reserved-memory"); 1903ef45ddaSAndre Przywara uint32_t addresses[3]; 1913ef45ddaSAndre Przywara 1923ef45ddaSAndre Przywara if (offs < 0) { /* create if not existing yet */ 1933ef45ddaSAndre Przywara offs = fdt_add_subnode(dtb, 0, "reserved-memory"); 1943ef45ddaSAndre Przywara if (offs < 0) 1953ef45ddaSAndre Przywara return offs; 1963ef45ddaSAndre Przywara fdt_setprop_u32(dtb, offs, "#address-cells", 2); 1973ef45ddaSAndre Przywara fdt_setprop_u32(dtb, offs, "#size-cells", 1); 1983ef45ddaSAndre Przywara fdt_setprop(dtb, offs, "ranges", NULL, 0); 1993ef45ddaSAndre Przywara } 2003ef45ddaSAndre Przywara 2013ef45ddaSAndre Przywara addresses[0] = cpu_to_fdt32(HIGH_BITS(base)); 2023ef45ddaSAndre Przywara addresses[1] = cpu_to_fdt32(base & 0xffffffff); 2033ef45ddaSAndre Przywara addresses[2] = cpu_to_fdt32(size & 0xffffffff); 2043ef45ddaSAndre Przywara offs = fdt_add_subnode(dtb, offs, node_name); 2053ef45ddaSAndre Przywara fdt_setprop(dtb, offs, "no-map", NULL, 0); 2063ef45ddaSAndre Przywara fdt_setprop(dtb, offs, "reg", addresses, 12); 2073ef45ddaSAndre Przywara 2083ef45ddaSAndre Przywara return 0; 2093ef45ddaSAndre Przywara } 210