1f240728bSAndre Przywara /* 22b2b5657SSamuel Holland * Copyright (c) 2016-2022, 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 17780dd2b3SJavier Almansa Sobrino #include <errno.h> 18780dd2b3SJavier Almansa Sobrino #include <stdio.h> 19f240728bSAndre Przywara #include <string.h> 20f240728bSAndre Przywara 21f240728bSAndre Przywara #include <libfdt.h> 22f240728bSAndre Przywara 23780dd2b3SJavier Almansa Sobrino #include <arch.h> 24f240728bSAndre Przywara #include <common/debug.h> 25780dd2b3SJavier Almansa Sobrino #include <common/fdt_fixup.h> 26780dd2b3SJavier Almansa Sobrino #include <common/fdt_wrappers.h> 27f240728bSAndre Przywara #include <drivers/console.h> 28f240728bSAndre Przywara #include <lib/psci/psci.h> 29780dd2b3SJavier Almansa Sobrino #include <plat/common/platform.h> 30f240728bSAndre Przywara 31f240728bSAndre Przywara 32f240728bSAndre Przywara static int append_psci_compatible(void *fdt, int offs, const char *str) 33f240728bSAndre Przywara { 34f240728bSAndre Przywara return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1); 35f240728bSAndre Przywara } 36f240728bSAndre Przywara 3766799507SAndre Przywara /* 3866799507SAndre Przywara * Those defines are for PSCI v0.1 legacy clients, which we expect to use 3966799507SAndre Przywara * the same execution state (AArch32/AArch64) as TF-A. 4066799507SAndre Przywara * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2. 4166799507SAndre Przywara */ 4266799507SAndre Przywara #ifdef __aarch64__ 4366799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH64 4466799507SAndre Przywara #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH64 4566799507SAndre Przywara #else 4666799507SAndre Przywara #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH32 4766799507SAndre Przywara #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH32 4866799507SAndre Przywara #endif 4966799507SAndre Przywara 506eaf928dSAndre Przywara /******************************************************************************* 516eaf928dSAndre Przywara * dt_add_psci_node() - Add a PSCI node into an existing device tree 526eaf928dSAndre Przywara * @fdt: pointer to the device tree blob in memory 536eaf928dSAndre Przywara * 546eaf928dSAndre Przywara * Add a device tree node describing PSCI into the root level of an existing 556eaf928dSAndre Przywara * device tree blob in memory. 566eaf928dSAndre Przywara * This will add v0.1, v0.2 and v1.0 compatible strings and the standard 576eaf928dSAndre Przywara * function IDs for v0.1 compatibility. 586eaf928dSAndre Przywara * An existing PSCI node will not be touched, the function will return success 596eaf928dSAndre Przywara * in this case. This function will not touch the /cpus enable methods, use 606eaf928dSAndre Przywara * dt_add_psci_cpu_enable_methods() for that. 616eaf928dSAndre Przywara * 626eaf928dSAndre Przywara * Return: 0 on success, -1 otherwise. 636eaf928dSAndre Przywara ******************************************************************************/ 64f240728bSAndre Przywara int dt_add_psci_node(void *fdt) 65f240728bSAndre Przywara { 66f240728bSAndre Przywara int offs; 67f240728bSAndre Przywara 68f240728bSAndre Przywara if (fdt_path_offset(fdt, "/psci") >= 0) { 69f240728bSAndre Przywara WARN("PSCI Device Tree node already exists!\n"); 70f240728bSAndre Przywara return 0; 71f240728bSAndre Przywara } 72f240728bSAndre Przywara 73f240728bSAndre Przywara offs = fdt_path_offset(fdt, "/"); 74f240728bSAndre Przywara if (offs < 0) 75f240728bSAndre Przywara return -1; 76f240728bSAndre Przywara offs = fdt_add_subnode(fdt, offs, "psci"); 77f240728bSAndre Przywara if (offs < 0) 78f240728bSAndre Przywara return -1; 79f240728bSAndre Przywara if (append_psci_compatible(fdt, offs, "arm,psci-1.0")) 80f240728bSAndre Przywara return -1; 81f240728bSAndre Przywara if (append_psci_compatible(fdt, offs, "arm,psci-0.2")) 82f240728bSAndre Przywara return -1; 83f240728bSAndre Przywara if (append_psci_compatible(fdt, offs, "arm,psci")) 84f240728bSAndre Przywara return -1; 85f240728bSAndre Przywara if (fdt_setprop_string(fdt, offs, "method", "smc")) 86f240728bSAndre Przywara return -1; 8766799507SAndre Przywara if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID)) 88f240728bSAndre Przywara return -1; 89f240728bSAndre Przywara if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF)) 90f240728bSAndre Przywara return -1; 9166799507SAndre Przywara if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID)) 92f240728bSAndre Przywara return -1; 93f240728bSAndre Przywara return 0; 94f240728bSAndre Przywara } 95f240728bSAndre Przywara 96f240728bSAndre Przywara /* 97f240728bSAndre Przywara * Find the first subnode that has a "device_type" property with the value 98f240728bSAndre Przywara * "cpu" and which's enable-method is not "psci" (yet). 99f240728bSAndre Przywara * Returns 0 if no such subnode is found, so all have already been patched 100f240728bSAndre Przywara * or none have to be patched in the first place. 101f240728bSAndre Przywara * Returns 1 if *one* such subnode has been found and successfully changed 102f240728bSAndre Przywara * to "psci". 103feb358b6SAndre Przywara * Returns negative values on error. 104f240728bSAndre Przywara * 105f240728bSAndre Przywara * Call in a loop until it returns 0. Recalculate the node offset after 106f240728bSAndre Przywara * it has returned 1. 107f240728bSAndre Przywara */ 108f240728bSAndre Przywara static int dt_update_one_cpu_node(void *fdt, int offset) 109f240728bSAndre Przywara { 110f240728bSAndre Przywara int offs; 111f240728bSAndre Przywara 112f240728bSAndre Przywara /* Iterate over all subnodes to find those with device_type = "cpu". */ 113f240728bSAndre Przywara for (offs = fdt_first_subnode(fdt, offset); offs >= 0; 114f240728bSAndre Przywara offs = fdt_next_subnode(fdt, offs)) { 115f240728bSAndre Przywara const char *prop; 116f240728bSAndre Przywara int len; 117feb358b6SAndre Przywara int ret; 118f240728bSAndre Przywara 119f240728bSAndre Przywara prop = fdt_getprop(fdt, offs, "device_type", &len); 120feb358b6SAndre Przywara if (prop == NULL) 121f240728bSAndre Przywara continue; 122feb358b6SAndre Przywara if ((strcmp(prop, "cpu") != 0) || (len != 4)) 123f240728bSAndre Przywara continue; 124f240728bSAndre Przywara 125f240728bSAndre Przywara /* Ignore any nodes which already use "psci". */ 126f240728bSAndre Przywara prop = fdt_getprop(fdt, offs, "enable-method", &len); 127feb358b6SAndre Przywara if ((prop != NULL) && 128feb358b6SAndre Przywara (strcmp(prop, "psci") == 0) && (len == 5)) 129f240728bSAndre Przywara continue; 130f240728bSAndre Przywara 131feb358b6SAndre Przywara ret = fdt_setprop_string(fdt, offs, "enable-method", "psci"); 132feb358b6SAndre Przywara if (ret < 0) 133feb358b6SAndre Przywara return ret; 134f240728bSAndre Przywara /* 135f240728bSAndre Przywara * Subnode found and patched. 136f240728bSAndre Przywara * Restart to accommodate potentially changed offsets. 137f240728bSAndre Przywara */ 138f240728bSAndre Przywara return 1; 139f240728bSAndre Przywara } 140f240728bSAndre Przywara 141f240728bSAndre Przywara if (offs == -FDT_ERR_NOTFOUND) 142f240728bSAndre Przywara return 0; 143f240728bSAndre Przywara 144f240728bSAndre Przywara return offs; 145f240728bSAndre Przywara } 146f240728bSAndre Przywara 1476eaf928dSAndre Przywara /******************************************************************************* 1486eaf928dSAndre Przywara * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI 1496eaf928dSAndre Przywara * @fdt: pointer to the device tree blob in memory 1506eaf928dSAndre Przywara * 1516eaf928dSAndre Przywara * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change 1526eaf928dSAndre Przywara * the enable-method to PSCI. This will add the enable-method properties, if 1536eaf928dSAndre Przywara * required, or will change existing properties to read "psci". 1546eaf928dSAndre Przywara * 1556eaf928dSAndre Przywara * Return: 0 on success, or a negative error value otherwise. 1566eaf928dSAndre Przywara ******************************************************************************/ 1576eaf928dSAndre Przywara 158f240728bSAndre Przywara int dt_add_psci_cpu_enable_methods(void *fdt) 159f240728bSAndre Przywara { 160f240728bSAndre Przywara int offs, ret; 161f240728bSAndre Przywara 162f240728bSAndre Przywara do { 163f240728bSAndre Przywara offs = fdt_path_offset(fdt, "/cpus"); 164f240728bSAndre Przywara if (offs < 0) 165f240728bSAndre Przywara return offs; 166f240728bSAndre Przywara 167f240728bSAndre Przywara ret = dt_update_one_cpu_node(fdt, offs); 168f240728bSAndre Przywara } while (ret > 0); 169f240728bSAndre Przywara 170f240728bSAndre Przywara return ret; 171f240728bSAndre Przywara } 1723ef45ddaSAndre Przywara 1733ef45ddaSAndre Przywara #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0) 1743ef45ddaSAndre Przywara 1756eaf928dSAndre Przywara /******************************************************************************* 1766eaf928dSAndre Przywara * fdt_add_reserved_memory() - reserve (secure) memory regions in DT 1776eaf928dSAndre Przywara * @dtb: pointer to the device tree blob in memory 1786eaf928dSAndre Przywara * @node_name: name of the subnode to be used 1796eaf928dSAndre Przywara * @base: physical base address of the reserved region 1806eaf928dSAndre Przywara * @size: size of the reserved region 1816eaf928dSAndre Przywara * 1826eaf928dSAndre Przywara * Add a region of memory to the /reserved-memory node in a device tree in 1836eaf928dSAndre Przywara * memory, creating that node if required. Each region goes into a subnode 1846eaf928dSAndre Przywara * of that node and has a @node_name, a @base address and a @size. 1856eaf928dSAndre Przywara * This will prevent any device tree consumer from using that memory. It 1866eaf928dSAndre Przywara * can be used to announce secure memory regions, as it adds the "no-map" 1876eaf928dSAndre Przywara * property to prevent mapping and speculative operations on that region. 1886eaf928dSAndre Przywara * 1896eaf928dSAndre Przywara * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding 1906eaf928dSAndre Przywara * documentation for details. 19181146c46SAndre Przywara * According to this binding, the address-cells and size-cells must match 19281146c46SAndre Przywara * those of the root node. 1936eaf928dSAndre Przywara * 1946eaf928dSAndre Przywara * Return: 0 on success, a negative error value otherwise. 1956eaf928dSAndre Przywara ******************************************************************************/ 1963ef45ddaSAndre Przywara int fdt_add_reserved_memory(void *dtb, const char *node_name, 1973ef45ddaSAndre Przywara uintptr_t base, size_t size) 1983ef45ddaSAndre Przywara { 1993ef45ddaSAndre Przywara int offs = fdt_path_offset(dtb, "/reserved-memory"); 200*42488064SAndre Przywara int node; 20181146c46SAndre Przywara uint32_t addresses[4]; 20281146c46SAndre Przywara int ac, sc; 20381146c46SAndre Przywara unsigned int idx = 0; 2043ef45ddaSAndre Przywara 20581146c46SAndre Przywara ac = fdt_address_cells(dtb, 0); 20681146c46SAndre Przywara sc = fdt_size_cells(dtb, 0); 2073ef45ddaSAndre Przywara if (offs < 0) { /* create if not existing yet */ 2083ef45ddaSAndre Przywara offs = fdt_add_subnode(dtb, 0, "reserved-memory"); 20981146c46SAndre Przywara if (offs < 0) { 2103ef45ddaSAndre Przywara return offs; 21181146c46SAndre Przywara } 21281146c46SAndre Przywara fdt_setprop_u32(dtb, offs, "#address-cells", ac); 21381146c46SAndre Przywara fdt_setprop_u32(dtb, offs, "#size-cells", sc); 2143ef45ddaSAndre Przywara fdt_setprop(dtb, offs, "ranges", NULL, 0); 2153ef45ddaSAndre Przywara } 2163ef45ddaSAndre Przywara 217*42488064SAndre Przywara /* Check for existing regions */ 218*42488064SAndre Przywara fdt_for_each_subnode(node, dtb, offs) { 219*42488064SAndre Przywara uintptr_t c_base; 220*42488064SAndre Przywara size_t c_size; 221*42488064SAndre Przywara int ret; 222*42488064SAndre Przywara 223*42488064SAndre Przywara ret = fdt_get_reg_props_by_index(dtb, node, 0, &c_base, &c_size); 224*42488064SAndre Przywara /* Ignore illegal subnodes */ 225*42488064SAndre Przywara if (ret != 0) { 226*42488064SAndre Przywara continue; 227*42488064SAndre Przywara } 228*42488064SAndre Przywara 229*42488064SAndre Przywara /* existing region entirely contains the new region */ 230*42488064SAndre Przywara if (base >= c_base && (base + size) <= (c_base + c_size)) { 231*42488064SAndre Przywara return 0; 232*42488064SAndre Przywara } 233*42488064SAndre Przywara } 234*42488064SAndre Przywara 23581146c46SAndre Przywara if (ac > 1) { 23681146c46SAndre Przywara addresses[idx] = cpu_to_fdt32(HIGH_BITS(base)); 23781146c46SAndre Przywara idx++; 23881146c46SAndre Przywara } 23981146c46SAndre Przywara addresses[idx] = cpu_to_fdt32(base & 0xffffffff); 24081146c46SAndre Przywara idx++; 24181146c46SAndre Przywara if (sc > 1) { 24281146c46SAndre Przywara addresses[idx] = cpu_to_fdt32(HIGH_BITS(size)); 24381146c46SAndre Przywara idx++; 24481146c46SAndre Przywara } 24581146c46SAndre Przywara addresses[idx] = cpu_to_fdt32(size & 0xffffffff); 24681146c46SAndre Przywara idx++; 2473ef45ddaSAndre Przywara offs = fdt_add_subnode(dtb, offs, node_name); 2483ef45ddaSAndre Przywara fdt_setprop(dtb, offs, "no-map", NULL, 0); 24981146c46SAndre Przywara fdt_setprop(dtb, offs, "reg", addresses, idx * sizeof(uint32_t)); 2503ef45ddaSAndre Przywara 2513ef45ddaSAndre Przywara return 0; 2523ef45ddaSAndre Przywara } 253780dd2b3SJavier Almansa Sobrino 254780dd2b3SJavier Almansa Sobrino /******************************************************************************* 255780dd2b3SJavier Almansa Sobrino * fdt_add_cpu() Add a new CPU node to the DT 256780dd2b3SJavier Almansa Sobrino * @dtb: Pointer to the device tree blob in memory 257780dd2b3SJavier Almansa Sobrino * @parent: Offset of the parent node 258780dd2b3SJavier Almansa Sobrino * @mpidr: MPIDR for the current CPU 259780dd2b3SJavier Almansa Sobrino * 260780dd2b3SJavier Almansa Sobrino * Create and add a new cpu node to a DTB. 261780dd2b3SJavier Almansa Sobrino * 262780dd2b3SJavier Almansa Sobrino * Return the offset of the new node or a negative value in case of error 263780dd2b3SJavier Almansa Sobrino ******************************************************************************/ 264780dd2b3SJavier Almansa Sobrino 265780dd2b3SJavier Almansa Sobrino static int fdt_add_cpu(void *dtb, int parent, u_register_t mpidr) 266780dd2b3SJavier Almansa Sobrino { 267780dd2b3SJavier Almansa Sobrino int cpu_offs; 268780dd2b3SJavier Almansa Sobrino int err; 269780dd2b3SJavier Almansa Sobrino char snode_name[15]; 270780dd2b3SJavier Almansa Sobrino uint64_t reg_prop; 271780dd2b3SJavier Almansa Sobrino 272780dd2b3SJavier Almansa Sobrino reg_prop = mpidr & MPID_MASK & ~MPIDR_MT_MASK; 273780dd2b3SJavier Almansa Sobrino 274780dd2b3SJavier Almansa Sobrino snprintf(snode_name, sizeof(snode_name), "cpu@%x", 275780dd2b3SJavier Almansa Sobrino (unsigned int)reg_prop); 276780dd2b3SJavier Almansa Sobrino 277780dd2b3SJavier Almansa Sobrino cpu_offs = fdt_add_subnode(dtb, parent, snode_name); 278780dd2b3SJavier Almansa Sobrino if (cpu_offs < 0) { 279780dd2b3SJavier Almansa Sobrino ERROR ("FDT: add subnode \"%s\" failed: %i\n", 280780dd2b3SJavier Almansa Sobrino snode_name, cpu_offs); 281780dd2b3SJavier Almansa Sobrino return cpu_offs; 282780dd2b3SJavier Almansa Sobrino } 283780dd2b3SJavier Almansa Sobrino 284780dd2b3SJavier Almansa Sobrino err = fdt_setprop_string(dtb, cpu_offs, "compatible", "arm,armv8"); 285780dd2b3SJavier Almansa Sobrino if (err < 0) { 286780dd2b3SJavier Almansa Sobrino ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 287780dd2b3SJavier Almansa Sobrino "compatible", cpu_offs); 288780dd2b3SJavier Almansa Sobrino return err; 289780dd2b3SJavier Almansa Sobrino } 290780dd2b3SJavier Almansa Sobrino 291780dd2b3SJavier Almansa Sobrino err = fdt_setprop_u64(dtb, cpu_offs, "reg", reg_prop); 292780dd2b3SJavier Almansa Sobrino if (err < 0) { 293780dd2b3SJavier Almansa Sobrino ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 294780dd2b3SJavier Almansa Sobrino "reg", cpu_offs); 295780dd2b3SJavier Almansa Sobrino return err; 296780dd2b3SJavier Almansa Sobrino } 297780dd2b3SJavier Almansa Sobrino 298780dd2b3SJavier Almansa Sobrino err = fdt_setprop_string(dtb, cpu_offs, "device_type", "cpu"); 299780dd2b3SJavier Almansa Sobrino if (err < 0) { 300780dd2b3SJavier Almansa Sobrino ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 301780dd2b3SJavier Almansa Sobrino "device_type", cpu_offs); 302780dd2b3SJavier Almansa Sobrino return err; 303780dd2b3SJavier Almansa Sobrino } 304780dd2b3SJavier Almansa Sobrino 305780dd2b3SJavier Almansa Sobrino err = fdt_setprop_string(dtb, cpu_offs, "enable-method", "psci"); 306780dd2b3SJavier Almansa Sobrino if (err < 0) { 307780dd2b3SJavier Almansa Sobrino ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 308780dd2b3SJavier Almansa Sobrino "enable-method", cpu_offs); 309780dd2b3SJavier Almansa Sobrino return err; 310780dd2b3SJavier Almansa Sobrino } 311780dd2b3SJavier Almansa Sobrino 312780dd2b3SJavier Almansa Sobrino return cpu_offs; 313780dd2b3SJavier Almansa Sobrino } 314780dd2b3SJavier Almansa Sobrino 315780dd2b3SJavier Almansa Sobrino /****************************************************************************** 316780dd2b3SJavier Almansa Sobrino * fdt_add_cpus_node() - Add the cpus node to the DTB 317780dd2b3SJavier Almansa Sobrino * @dtb: pointer to the device tree blob in memory 318780dd2b3SJavier Almansa Sobrino * @afflv0: Maximum number of threads per core (affinity level 0). 319780dd2b3SJavier Almansa Sobrino * @afflv1: Maximum number of CPUs per cluster (affinity level 1). 320780dd2b3SJavier Almansa Sobrino * @afflv2: Maximum number of clusters (affinity level 2). 321780dd2b3SJavier Almansa Sobrino * 322780dd2b3SJavier Almansa Sobrino * Iterate over all the possible MPIDs given the maximum affinity levels and 323780dd2b3SJavier Almansa Sobrino * add a cpus node to the DTB with all the valid CPUs on the system. 324780dd2b3SJavier Almansa Sobrino * If there is already a /cpus node, exit gracefully 325780dd2b3SJavier Almansa Sobrino * 326780dd2b3SJavier Almansa Sobrino * A system with two CPUs would generate a node equivalent or similar to: 327780dd2b3SJavier Almansa Sobrino * 328780dd2b3SJavier Almansa Sobrino * cpus { 329780dd2b3SJavier Almansa Sobrino * #address-cells = <2>; 330780dd2b3SJavier Almansa Sobrino * #size-cells = <0>; 331780dd2b3SJavier Almansa Sobrino * 332780dd2b3SJavier Almansa Sobrino * cpu0: cpu@0 { 333780dd2b3SJavier Almansa Sobrino * compatible = "arm,armv8"; 334780dd2b3SJavier Almansa Sobrino * reg = <0x0 0x0>; 335780dd2b3SJavier Almansa Sobrino * device_type = "cpu"; 336780dd2b3SJavier Almansa Sobrino * enable-method = "psci"; 337780dd2b3SJavier Almansa Sobrino * }; 338780dd2b3SJavier Almansa Sobrino * cpu1: cpu@10000 { 339780dd2b3SJavier Almansa Sobrino * compatible = "arm,armv8"; 340780dd2b3SJavier Almansa Sobrino * reg = <0x0 0x100>; 341780dd2b3SJavier Almansa Sobrino * device_type = "cpu"; 342780dd2b3SJavier Almansa Sobrino * enable-method = "psci"; 343780dd2b3SJavier Almansa Sobrino * }; 344780dd2b3SJavier Almansa Sobrino * }; 345780dd2b3SJavier Almansa Sobrino * 346780dd2b3SJavier Almansa Sobrino * Full documentation about the CPU bindings can be found at: 347780dd2b3SJavier Almansa Sobrino * https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/cpus.txt 348780dd2b3SJavier Almansa Sobrino * 349780dd2b3SJavier Almansa Sobrino * Return the offset of the node or a negative value on error. 350780dd2b3SJavier Almansa Sobrino ******************************************************************************/ 351780dd2b3SJavier Almansa Sobrino 352780dd2b3SJavier Almansa Sobrino int fdt_add_cpus_node(void *dtb, unsigned int afflv0, 353780dd2b3SJavier Almansa Sobrino unsigned int afflv1, unsigned int afflv2) 354780dd2b3SJavier Almansa Sobrino { 355780dd2b3SJavier Almansa Sobrino int offs; 356780dd2b3SJavier Almansa Sobrino int err; 357780dd2b3SJavier Almansa Sobrino unsigned int i, j, k; 358780dd2b3SJavier Almansa Sobrino u_register_t mpidr; 359780dd2b3SJavier Almansa Sobrino int cpuid; 360780dd2b3SJavier Almansa Sobrino 361780dd2b3SJavier Almansa Sobrino if (fdt_path_offset(dtb, "/cpus") >= 0) { 362780dd2b3SJavier Almansa Sobrino return -EEXIST; 363780dd2b3SJavier Almansa Sobrino } 364780dd2b3SJavier Almansa Sobrino 365780dd2b3SJavier Almansa Sobrino offs = fdt_add_subnode(dtb, 0, "cpus"); 366780dd2b3SJavier Almansa Sobrino if (offs < 0) { 367780dd2b3SJavier Almansa Sobrino ERROR ("FDT: add subnode \"cpus\" node to parent node failed"); 368780dd2b3SJavier Almansa Sobrino return offs; 369780dd2b3SJavier Almansa Sobrino } 370780dd2b3SJavier Almansa Sobrino 371780dd2b3SJavier Almansa Sobrino err = fdt_setprop_u32(dtb, offs, "#address-cells", 2); 372780dd2b3SJavier Almansa Sobrino if (err < 0) { 373780dd2b3SJavier Almansa Sobrino ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 374780dd2b3SJavier Almansa Sobrino "#address-cells", offs); 375780dd2b3SJavier Almansa Sobrino return err; 376780dd2b3SJavier Almansa Sobrino } 377780dd2b3SJavier Almansa Sobrino 378780dd2b3SJavier Almansa Sobrino err = fdt_setprop_u32(dtb, offs, "#size-cells", 0); 379780dd2b3SJavier Almansa Sobrino if (err < 0) { 380780dd2b3SJavier Almansa Sobrino ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 381780dd2b3SJavier Almansa Sobrino "#size-cells", offs); 382780dd2b3SJavier Almansa Sobrino return err; 383780dd2b3SJavier Almansa Sobrino } 384780dd2b3SJavier Almansa Sobrino 385780dd2b3SJavier Almansa Sobrino /* 386780dd2b3SJavier Almansa Sobrino * Populate the node with the CPUs. 387780dd2b3SJavier Almansa Sobrino * As libfdt prepends subnodes within a node, reverse the index count 388780dd2b3SJavier Almansa Sobrino * so the CPU nodes would be better ordered. 389780dd2b3SJavier Almansa Sobrino */ 390780dd2b3SJavier Almansa Sobrino for (i = afflv2; i > 0U; i--) { 391780dd2b3SJavier Almansa Sobrino for (j = afflv1; j > 0U; j--) { 392780dd2b3SJavier Almansa Sobrino for (k = afflv0; k > 0U; k--) { 393780dd2b3SJavier Almansa Sobrino mpidr = ((i - 1) << MPIDR_AFF2_SHIFT) | 394780dd2b3SJavier Almansa Sobrino ((j - 1) << MPIDR_AFF1_SHIFT) | 395780dd2b3SJavier Almansa Sobrino ((k - 1) << MPIDR_AFF0_SHIFT) | 396780dd2b3SJavier Almansa Sobrino (read_mpidr_el1() & MPIDR_MT_MASK); 397780dd2b3SJavier Almansa Sobrino 398780dd2b3SJavier Almansa Sobrino cpuid = plat_core_pos_by_mpidr(mpidr); 399780dd2b3SJavier Almansa Sobrino if (cpuid >= 0) { 400780dd2b3SJavier Almansa Sobrino /* Valid MPID found */ 401780dd2b3SJavier Almansa Sobrino err = fdt_add_cpu(dtb, offs, mpidr); 402780dd2b3SJavier Almansa Sobrino if (err < 0) { 403780dd2b3SJavier Almansa Sobrino ERROR ("FDT: %s 0x%08x\n", 404780dd2b3SJavier Almansa Sobrino "error adding CPU", 405780dd2b3SJavier Almansa Sobrino (uint32_t)mpidr); 406780dd2b3SJavier Almansa Sobrino return err; 407780dd2b3SJavier Almansa Sobrino } 408780dd2b3SJavier Almansa Sobrino } 409780dd2b3SJavier Almansa Sobrino } 410780dd2b3SJavier Almansa Sobrino } 411780dd2b3SJavier Almansa Sobrino } 412780dd2b3SJavier Almansa Sobrino 413780dd2b3SJavier Almansa Sobrino return offs; 414780dd2b3SJavier Almansa Sobrino } 4159f7bab42SAndre Przywara 4162b2b5657SSamuel Holland /******************************************************************************* 4172b2b5657SSamuel Holland * fdt_add_cpu_idle_states() - add PSCI CPU idle states to cpu nodes in the DT 4182b2b5657SSamuel Holland * @dtb: pointer to the device tree blob in memory 4192b2b5657SSamuel Holland * @states: array of idle state descriptions, ending with empty element 4202b2b5657SSamuel Holland * 4212b2b5657SSamuel Holland * Add information about CPU idle states to the devicetree. This function 4222b2b5657SSamuel Holland * assumes that CPU idle states are not already present in the devicetree, and 4232b2b5657SSamuel Holland * that all CPU states are equally applicable to all CPUs. 4242b2b5657SSamuel Holland * 4252b2b5657SSamuel Holland * See arm/idle-states.yaml and arm/psci.yaml in the (Linux kernel) DT binding 4262b2b5657SSamuel Holland * documentation for more details. 4272b2b5657SSamuel Holland * 4282b2b5657SSamuel Holland * Return: 0 on success, a negative error value otherwise. 4292b2b5657SSamuel Holland ******************************************************************************/ 4302b2b5657SSamuel Holland int fdt_add_cpu_idle_states(void *dtb, const struct psci_cpu_idle_state *state) 4312b2b5657SSamuel Holland { 4322b2b5657SSamuel Holland int cpu_node, cpus_node, idle_states_node, ret; 4332b2b5657SSamuel Holland uint32_t count, phandle; 4342b2b5657SSamuel Holland 4352b2b5657SSamuel Holland ret = fdt_find_max_phandle(dtb, &phandle); 4362b2b5657SSamuel Holland phandle++; 4372b2b5657SSamuel Holland if (ret < 0) { 4382b2b5657SSamuel Holland return ret; 4392b2b5657SSamuel Holland } 4402b2b5657SSamuel Holland 4412b2b5657SSamuel Holland cpus_node = fdt_path_offset(dtb, "/cpus"); 4422b2b5657SSamuel Holland if (cpus_node < 0) { 4432b2b5657SSamuel Holland return cpus_node; 4442b2b5657SSamuel Holland } 4452b2b5657SSamuel Holland 4462b2b5657SSamuel Holland /* Create the idle-states node and its child nodes. */ 4472b2b5657SSamuel Holland idle_states_node = fdt_add_subnode(dtb, cpus_node, "idle-states"); 4482b2b5657SSamuel Holland if (idle_states_node < 0) { 4492b2b5657SSamuel Holland return idle_states_node; 4502b2b5657SSamuel Holland } 4512b2b5657SSamuel Holland 4522b2b5657SSamuel Holland ret = fdt_setprop_string(dtb, idle_states_node, "entry-method", "psci"); 4532b2b5657SSamuel Holland if (ret < 0) { 4542b2b5657SSamuel Holland return ret; 4552b2b5657SSamuel Holland } 4562b2b5657SSamuel Holland 4572b2b5657SSamuel Holland for (count = 0U; state->name != NULL; count++, phandle++, state++) { 4582b2b5657SSamuel Holland int idle_state_node; 4592b2b5657SSamuel Holland 4602b2b5657SSamuel Holland idle_state_node = fdt_add_subnode(dtb, idle_states_node, 4612b2b5657SSamuel Holland state->name); 4622b2b5657SSamuel Holland if (idle_state_node < 0) { 4632b2b5657SSamuel Holland return idle_state_node; 4642b2b5657SSamuel Holland } 4652b2b5657SSamuel Holland 4662b2b5657SSamuel Holland fdt_setprop_string(dtb, idle_state_node, "compatible", 4672b2b5657SSamuel Holland "arm,idle-state"); 4682b2b5657SSamuel Holland fdt_setprop_u32(dtb, idle_state_node, "arm,psci-suspend-param", 4692b2b5657SSamuel Holland state->power_state); 4702b2b5657SSamuel Holland if (state->local_timer_stop) { 4712b2b5657SSamuel Holland fdt_setprop_empty(dtb, idle_state_node, 4722b2b5657SSamuel Holland "local-timer-stop"); 4732b2b5657SSamuel Holland } 4742b2b5657SSamuel Holland fdt_setprop_u32(dtb, idle_state_node, "entry-latency-us", 4752b2b5657SSamuel Holland state->entry_latency_us); 4762b2b5657SSamuel Holland fdt_setprop_u32(dtb, idle_state_node, "exit-latency-us", 4772b2b5657SSamuel Holland state->exit_latency_us); 4782b2b5657SSamuel Holland fdt_setprop_u32(dtb, idle_state_node, "min-residency-us", 4792b2b5657SSamuel Holland state->min_residency_us); 4802b2b5657SSamuel Holland if (state->wakeup_latency_us) { 4812b2b5657SSamuel Holland fdt_setprop_u32(dtb, idle_state_node, 4822b2b5657SSamuel Holland "wakeup-latency-us", 4832b2b5657SSamuel Holland state->wakeup_latency_us); 4842b2b5657SSamuel Holland } 4852b2b5657SSamuel Holland fdt_setprop_u32(dtb, idle_state_node, "phandle", phandle); 4862b2b5657SSamuel Holland } 4872b2b5657SSamuel Holland 4882b2b5657SSamuel Holland if (count == 0U) { 4892b2b5657SSamuel Holland return 0; 4902b2b5657SSamuel Holland } 4912b2b5657SSamuel Holland 4922b2b5657SSamuel Holland /* Link each cpu node to the idle state nodes. */ 4932b2b5657SSamuel Holland fdt_for_each_subnode(cpu_node, dtb, cpus_node) { 4942b2b5657SSamuel Holland const char *device_type; 4952b2b5657SSamuel Holland fdt32_t *value; 4962b2b5657SSamuel Holland 4972b2b5657SSamuel Holland /* Only process child nodes with device_type = "cpu". */ 4982b2b5657SSamuel Holland device_type = fdt_getprop(dtb, cpu_node, "device_type", NULL); 4992b2b5657SSamuel Holland if (device_type == NULL || strcmp(device_type, "cpu") != 0) { 5002b2b5657SSamuel Holland continue; 5012b2b5657SSamuel Holland } 5022b2b5657SSamuel Holland 5032b2b5657SSamuel Holland /* Allocate space for the list of phandles. */ 5042b2b5657SSamuel Holland ret = fdt_setprop_placeholder(dtb, cpu_node, "cpu-idle-states", 5052b2b5657SSamuel Holland count * sizeof(phandle), 5062b2b5657SSamuel Holland (void **)&value); 5072b2b5657SSamuel Holland if (ret < 0) { 5082b2b5657SSamuel Holland return ret; 5092b2b5657SSamuel Holland } 5102b2b5657SSamuel Holland 5112b2b5657SSamuel Holland /* Fill in the phandles of the idle state nodes. */ 5122b2b5657SSamuel Holland for (uint32_t i = 0U; i < count; ++i) { 5132b2b5657SSamuel Holland value[i] = cpu_to_fdt32(phandle - count + i); 5142b2b5657SSamuel Holland } 5152b2b5657SSamuel Holland } 5162b2b5657SSamuel Holland 5172b2b5657SSamuel Holland return 0; 5182b2b5657SSamuel Holland } 5192b2b5657SSamuel Holland 5209f7bab42SAndre Przywara /** 5219f7bab42SAndre Przywara * fdt_adjust_gic_redist() - Adjust GICv3 redistributor size 5229f7bab42SAndre Przywara * @dtb: Pointer to the DT blob in memory 5239f7bab42SAndre Przywara * @nr_cores: Number of CPU cores on this system. 5244d585fe5SAndre Przywara * @gicr_base: Base address of the first GICR frame, or ~0 if unchanged 5259f7bab42SAndre Przywara * @gicr_frame_size: Size of the GICR frame per core 5269f7bab42SAndre Przywara * 5279f7bab42SAndre Przywara * On a GICv3 compatible interrupt controller, the redistributor provides 5289f7bab42SAndre Przywara * a number of 64k pages per each supported core. So with a dynamic topology, 5299f7bab42SAndre Przywara * this size cannot be known upfront and thus can't be hardcoded into the DTB. 5309f7bab42SAndre Przywara * 5319f7bab42SAndre Przywara * Find the DT node describing the GICv3 interrupt controller, and adjust 5329f7bab42SAndre Przywara * the size of the redistributor to match the number of actual cores on 5339f7bab42SAndre Przywara * this system. 5349f7bab42SAndre Przywara * A GICv4 compatible redistributor uses four 64K pages per core, whereas GICs 5359f7bab42SAndre Przywara * without support for direct injection of virtual interrupts use two 64K pages. 5369f7bab42SAndre Przywara * The @gicr_frame_size parameter should be 262144 and 131072, respectively. 5374d585fe5SAndre Przywara * Also optionally allow adjusting the GICR frame base address, when this is 5384d585fe5SAndre Przywara * different due to ITS frames between distributor and redistributor. 5399f7bab42SAndre Przywara * 5409f7bab42SAndre Przywara * Return: 0 on success, negative error value otherwise. 5419f7bab42SAndre Przywara */ 5429f7bab42SAndre Przywara int fdt_adjust_gic_redist(void *dtb, unsigned int nr_cores, 5434d585fe5SAndre Przywara uintptr_t gicr_base, unsigned int gicr_frame_size) 5449f7bab42SAndre Przywara { 5459f7bab42SAndre Przywara int offset = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-v3"); 5464d585fe5SAndre Przywara uint64_t reg_64; 5474d585fe5SAndre Przywara uint32_t reg_32; 5489f7bab42SAndre Przywara void *val; 5494d585fe5SAndre Przywara int parent, ret; 5509f7bab42SAndre Przywara int ac, sc; 5519f7bab42SAndre Przywara 5529f7bab42SAndre Przywara if (offset < 0) { 5539f7bab42SAndre Przywara return offset; 5549f7bab42SAndre Przywara } 5559f7bab42SAndre Przywara 5569f7bab42SAndre Przywara parent = fdt_parent_offset(dtb, offset); 5579f7bab42SAndre Przywara if (parent < 0) { 5589f7bab42SAndre Przywara return parent; 5599f7bab42SAndre Przywara } 5609f7bab42SAndre Przywara ac = fdt_address_cells(dtb, parent); 5619f7bab42SAndre Przywara sc = fdt_size_cells(dtb, parent); 5629f7bab42SAndre Przywara if (ac < 0 || sc < 0) { 5639f7bab42SAndre Przywara return -EINVAL; 5649f7bab42SAndre Przywara } 5659f7bab42SAndre Przywara 5664d585fe5SAndre Przywara if (gicr_base != INVALID_BASE_ADDR) { 5674d585fe5SAndre Przywara if (ac == 1) { 5684d585fe5SAndre Przywara reg_32 = cpu_to_fdt32(gicr_base); 5694d585fe5SAndre Przywara val = ®_32; 5709f7bab42SAndre Przywara } else { 5714d585fe5SAndre Przywara reg_64 = cpu_to_fdt64(gicr_base); 5724d585fe5SAndre Przywara val = ®_64; 5734d585fe5SAndre Przywara } 5744d585fe5SAndre Przywara /* 5754d585fe5SAndre Przywara * The redistributor base address is the second address in 5764d585fe5SAndre Przywara * the "reg" entry, so we have to skip one address and one 5774d585fe5SAndre Przywara * size cell. 5784d585fe5SAndre Przywara */ 5794d585fe5SAndre Przywara ret = fdt_setprop_inplace_namelen_partial(dtb, offset, 5804d585fe5SAndre Przywara "reg", 3, 5814d585fe5SAndre Przywara (ac + sc) * 4, 5824d585fe5SAndre Przywara val, ac * 4); 5834d585fe5SAndre Przywara if (ret < 0) { 5844d585fe5SAndre Przywara return ret; 5854d585fe5SAndre Przywara } 5864d585fe5SAndre Przywara } 5874d585fe5SAndre Przywara 5884d585fe5SAndre Przywara if (sc == 1) { 5894d585fe5SAndre Przywara reg_32 = cpu_to_fdt32(nr_cores * gicr_frame_size); 5904d585fe5SAndre Przywara val = ®_32; 5914d585fe5SAndre Przywara } else { 5924d585fe5SAndre Przywara reg_64 = cpu_to_fdt64(nr_cores * (uint64_t)gicr_frame_size); 5934d585fe5SAndre Przywara val = ®_64; 5949f7bab42SAndre Przywara } 5959f7bab42SAndre Przywara 5969f7bab42SAndre Przywara /* 5979f7bab42SAndre Przywara * The redistributor is described in the second "reg" entry. 5989f7bab42SAndre Przywara * So we have to skip one address and one size cell, then another 5999f7bab42SAndre Przywara * address cell to get to the second size cell. 6009f7bab42SAndre Przywara */ 6019f7bab42SAndre Przywara return fdt_setprop_inplace_namelen_partial(dtb, offset, "reg", 3, 6029f7bab42SAndre Przywara (ac + sc + ac) * 4, 6039f7bab42SAndre Przywara val, sc * 4); 6049f7bab42SAndre Przywara } 6051aa7e302SAndre Przywara /** 6061aa7e302SAndre Przywara * fdt_set_mac_address () - store MAC address in device tree 6071aa7e302SAndre Przywara * @dtb: pointer to the device tree blob in memory 6081aa7e302SAndre Przywara * @eth_idx: number of Ethernet interface in /aliases node 6091aa7e302SAndre Przywara * @mac_addr: pointer to 6 byte MAC address to store 6101aa7e302SAndre Przywara * 6111aa7e302SAndre Przywara * Use the generic local-mac-address property in a network device DT node 6121aa7e302SAndre Przywara * to define the MAC address this device should be using. Many platform 6131aa7e302SAndre Przywara * network devices lack device-specific non-volatile storage to hold this 6141aa7e302SAndre Przywara * address, and leave it up to firmware to find and store a unique MAC 6151aa7e302SAndre Przywara * address in the DT. 6161aa7e302SAndre Przywara * The MAC address could be read from some board or firmware defined storage, 6171aa7e302SAndre Przywara * or could be derived from some other unique property like a serial number. 6181aa7e302SAndre Przywara * 6191aa7e302SAndre Przywara * Return: 0 on success, a negative libfdt error value otherwise. 6201aa7e302SAndre Przywara */ 6211aa7e302SAndre Przywara int fdt_set_mac_address(void *dtb, unsigned int ethernet_idx, 6221aa7e302SAndre Przywara const uint8_t *mac_addr) 6231aa7e302SAndre Przywara { 6241aa7e302SAndre Przywara char eth_alias[12]; 6251aa7e302SAndre Przywara const char *path; 6261aa7e302SAndre Przywara int node; 6271aa7e302SAndre Przywara 6281aa7e302SAndre Przywara if (ethernet_idx > 9U) { 6291aa7e302SAndre Przywara return -FDT_ERR_BADVALUE; 6301aa7e302SAndre Przywara } 6311aa7e302SAndre Przywara snprintf(eth_alias, sizeof(eth_alias), "ethernet%d", ethernet_idx); 6321aa7e302SAndre Przywara 6331aa7e302SAndre Przywara path = fdt_get_alias(dtb, eth_alias); 6341aa7e302SAndre Przywara if (path == NULL) { 6351aa7e302SAndre Przywara return -FDT_ERR_NOTFOUND; 6361aa7e302SAndre Przywara } 6371aa7e302SAndre Przywara 6381aa7e302SAndre Przywara node = fdt_path_offset(dtb, path); 6391aa7e302SAndre Przywara if (node < 0) { 6401aa7e302SAndre Przywara ERROR("Path \"%s\" not found in DT: %d\n", path, node); 6411aa7e302SAndre Przywara return node; 6421aa7e302SAndre Przywara } 6431aa7e302SAndre Przywara 6441aa7e302SAndre Przywara return fdt_setprop(dtb, node, "local-mac-address", mac_addr, 6); 6451aa7e302SAndre Przywara } 646