1 /* 2 * Copyright (c) 2016-2022, 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 <errno.h> 18 #include <stdio.h> 19 #include <string.h> 20 21 #include <libfdt.h> 22 23 #include <arch.h> 24 #include <common/debug.h> 25 #include <common/fdt_fixup.h> 26 #include <common/fdt_wrappers.h> 27 #include <drivers/console.h> 28 #include <lib/psci/psci.h> 29 #include <plat/common/platform.h> 30 31 32 static int append_psci_compatible(void *fdt, int offs, const char *str) 33 { 34 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1); 35 } 36 37 /* 38 * Those defines are for PSCI v0.1 legacy clients, which we expect to use 39 * the same execution state (AArch32/AArch64) as TF-A. 40 * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2. 41 */ 42 #ifdef __aarch64__ 43 #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH64 44 #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH64 45 #else 46 #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH32 47 #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH32 48 #endif 49 50 /******************************************************************************* 51 * dt_add_psci_node() - Add a PSCI node into an existing device tree 52 * @fdt: pointer to the device tree blob in memory 53 * 54 * Add a device tree node describing PSCI into the root level of an existing 55 * device tree blob in memory. 56 * This will add v0.1, v0.2 and v1.0 compatible strings and the standard 57 * function IDs for v0.1 compatibility. 58 * An existing PSCI node will not be touched, the function will return success 59 * in this case. This function will not touch the /cpus enable methods, use 60 * dt_add_psci_cpu_enable_methods() for that. 61 * 62 * Return: 0 on success, -1 otherwise. 63 ******************************************************************************/ 64 int dt_add_psci_node(void *fdt) 65 { 66 int offs; 67 68 if (fdt_path_offset(fdt, "/psci") >= 0) { 69 WARN("PSCI Device Tree node already exists!\n"); 70 return 0; 71 } 72 73 offs = fdt_path_offset(fdt, "/"); 74 if (offs < 0) { 75 return -1; 76 } 77 offs = fdt_add_subnode(fdt, offs, "psci"); 78 if (offs < 0) { 79 return -1; 80 } 81 if (append_psci_compatible(fdt, offs, "arm,psci-1.0") != 0) { 82 return -1; 83 } 84 if (append_psci_compatible(fdt, offs, "arm,psci-0.2") != 0) { 85 return -1; 86 } 87 if (append_psci_compatible(fdt, offs, "arm,psci") != 0) { 88 return -1; 89 } 90 if (fdt_setprop_string(fdt, offs, "method", "smc") != 0) { 91 return -1; 92 } 93 if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID) != 0) { 94 return -1; 95 } 96 if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF) != 0) { 97 return -1; 98 } 99 if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID) != 0) { 100 return -1; 101 } 102 return 0; 103 } 104 105 /* 106 * Find the first subnode that has a "device_type" property with the value 107 * "cpu" and which's enable-method is not "psci" (yet). 108 * Returns 0 if no such subnode is found, so all have already been patched 109 * or none have to be patched in the first place. 110 * Returns 1 if *one* such subnode has been found and successfully changed 111 * to "psci". 112 * Returns negative values on error. 113 * 114 * Call in a loop until it returns 0. Recalculate the node offset after 115 * it has returned 1. 116 */ 117 static int dt_update_one_cpu_node(void *fdt, int offset) 118 { 119 int offs; 120 121 /* Iterate over all subnodes to find those with device_type = "cpu". */ 122 for (offs = fdt_first_subnode(fdt, offset); offs >= 0; 123 offs = fdt_next_subnode(fdt, offs)) { 124 const char *prop; 125 int len; 126 int ret; 127 128 prop = fdt_getprop(fdt, offs, "device_type", &len); 129 if (prop == NULL) 130 continue; 131 if ((strcmp(prop, "cpu") != 0) || (len != 4)) 132 continue; 133 134 /* Ignore any nodes which already use "psci". */ 135 prop = fdt_getprop(fdt, offs, "enable-method", &len); 136 if ((prop != NULL) && 137 (strcmp(prop, "psci") == 0) && (len == 5)) 138 continue; 139 140 ret = fdt_setprop_string(fdt, offs, "enable-method", "psci"); 141 if (ret < 0) 142 return ret; 143 /* 144 * Subnode found and patched. 145 * Restart to accommodate potentially changed offsets. 146 */ 147 return 1; 148 } 149 150 if (offs == -FDT_ERR_NOTFOUND) 151 return 0; 152 153 return offs; 154 } 155 156 /******************************************************************************* 157 * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI 158 * @fdt: pointer to the device tree blob in memory 159 * 160 * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change 161 * the enable-method to PSCI. This will add the enable-method properties, if 162 * required, or will change existing properties to read "psci". 163 * 164 * Return: 0 on success, or a negative error value otherwise. 165 ******************************************************************************/ 166 167 int dt_add_psci_cpu_enable_methods(void *fdt) 168 { 169 int offs, ret; 170 171 do { 172 offs = fdt_path_offset(fdt, "/cpus"); 173 if (offs < 0) { 174 return offs; 175 } 176 177 ret = dt_update_one_cpu_node(fdt, offs); 178 } while (ret > 0); 179 180 return ret; 181 } 182 183 #define HIGH_BITS_U32(x) ((sizeof(x) > 4U) ? (uint32_t)((x) >> 32) : (uint32_t)0) 184 185 /******************************************************************************* 186 * fdt_add_reserved_memory() - reserve (secure) memory regions in DT 187 * @dtb: pointer to the device tree blob in memory 188 * @node_name: name of the subnode to be used 189 * @base: physical base address of the reserved region 190 * @size: size of the reserved region 191 * 192 * Add a region of memory to the /reserved-memory node in a device tree in 193 * memory, creating that node if required. Each region goes into a subnode 194 * of that node and has a @node_name, a @base address and a @size. 195 * This will prevent any device tree consumer from using that memory. It 196 * can be used to announce secure memory regions, as it adds the "no-map" 197 * property to prevent mapping and speculative operations on that region. 198 * 199 * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding 200 * documentation for details. 201 * According to this binding, the address-cells and size-cells must match 202 * those of the root node. 203 * 204 * Return: 0 on success, a negative error value otherwise. 205 ******************************************************************************/ 206 int fdt_add_reserved_memory(void *dtb, const char *node_name, 207 uintptr_t base, size_t size) 208 { 209 int offs = fdt_path_offset(dtb, "/reserved-memory"); 210 int node; 211 uint32_t addresses[4]; 212 int ac, sc; 213 unsigned int idx = 0; 214 215 ac = fdt_address_cells(dtb, 0); 216 sc = fdt_size_cells(dtb, 0); 217 if (ac < 0 || sc < 0) { 218 return -EINVAL; 219 } 220 if (offs < 0) { /* create if not existing yet */ 221 offs = fdt_add_subnode(dtb, 0, "reserved-memory"); 222 if (offs < 0) { 223 return offs; 224 } 225 fdt_setprop_u32(dtb, offs, "#address-cells", (uint32_t)ac); 226 fdt_setprop_u32(dtb, offs, "#size-cells", (uint32_t)sc); 227 fdt_setprop(dtb, offs, "ranges", NULL, 0); 228 } 229 230 /* Check for existing regions */ 231 fdt_for_each_subnode(node, dtb, offs) { 232 uintptr_t c_base; 233 size_t c_size; 234 int ret; 235 236 ret = fdt_get_reg_props_by_index(dtb, node, 0, &c_base, &c_size); 237 /* Ignore illegal subnodes */ 238 if (ret != 0) { 239 continue; 240 } 241 242 /* existing region entirely contains the new region */ 243 if (base >= c_base && (base + size) <= (c_base + c_size)) { 244 return 0; 245 } 246 } 247 248 if (ac > 1) { 249 addresses[idx] = cpu_to_fdt32(HIGH_BITS_U32(base)); 250 idx++; 251 } 252 addresses[idx] = cpu_to_fdt32(base & 0xffffffff); 253 idx++; 254 if (sc > 1) { 255 addresses[idx] = cpu_to_fdt32(HIGH_BITS_U32(size)); 256 idx++; 257 } 258 addresses[idx] = cpu_to_fdt32(size & 0xffffffff); 259 idx++; 260 offs = fdt_add_subnode(dtb, offs, node_name); 261 fdt_setprop(dtb, offs, "no-map", NULL, 0); 262 fdt_setprop(dtb, offs, "reg", addresses, idx * sizeof(uint32_t)); 263 264 return 0; 265 } 266 267 /******************************************************************************* 268 * fdt_add_cpu() Add a new CPU node to the DT 269 * @dtb: Pointer to the device tree blob in memory 270 * @parent: Offset of the parent node 271 * @mpidr: MPIDR for the current CPU 272 * 273 * Create and add a new cpu node to a DTB. 274 * 275 * Return the offset of the new node or a negative value in case of error 276 ******************************************************************************/ 277 278 static int fdt_add_cpu(void *dtb, int parent, u_register_t mpidr) 279 { 280 int cpu_offs; 281 int err; 282 char snode_name[15]; 283 uint64_t reg_prop; 284 285 reg_prop = mpidr & MPID_MASK & ~MPIDR_MT_MASK; 286 287 snprintf(snode_name, sizeof(snode_name), "cpu@%x", 288 (unsigned int)reg_prop); 289 290 cpu_offs = fdt_add_subnode(dtb, parent, snode_name); 291 if (cpu_offs < 0) { 292 ERROR ("FDT: add subnode \"%s\" failed: %i\n", 293 snode_name, cpu_offs); 294 return cpu_offs; 295 } 296 297 err = fdt_setprop_string(dtb, cpu_offs, "compatible", "arm,armv8"); 298 if (err < 0) { 299 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 300 "compatible", cpu_offs); 301 return err; 302 } 303 304 err = fdt_setprop_u64(dtb, cpu_offs, "reg", reg_prop); 305 if (err < 0) { 306 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 307 "reg", cpu_offs); 308 return err; 309 } 310 311 err = fdt_setprop_string(dtb, cpu_offs, "device_type", "cpu"); 312 if (err < 0) { 313 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 314 "device_type", cpu_offs); 315 return err; 316 } 317 318 err = fdt_setprop_string(dtb, cpu_offs, "enable-method", "psci"); 319 if (err < 0) { 320 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 321 "enable-method", cpu_offs); 322 return err; 323 } 324 325 return cpu_offs; 326 } 327 328 /****************************************************************************** 329 * fdt_add_cpus_node() - Add the cpus node to the DTB 330 * @dtb: pointer to the device tree blob in memory 331 * @afflv0: Maximum number of threads per core (affinity level 0). 332 * @afflv1: Maximum number of CPUs per cluster (affinity level 1). 333 * @afflv2: Maximum number of clusters (affinity level 2). 334 * 335 * Iterate over all the possible MPIDs given the maximum affinity levels and 336 * add a cpus node to the DTB with all the valid CPUs on the system. 337 * If there is already a /cpus node, exit gracefully 338 * 339 * A system with two CPUs would generate a node equivalent or similar to: 340 * 341 * cpus { 342 * #address-cells = <2>; 343 * #size-cells = <0>; 344 * 345 * cpu0: cpu@0 { 346 * compatible = "arm,armv8"; 347 * reg = <0x0 0x0>; 348 * device_type = "cpu"; 349 * enable-method = "psci"; 350 * }; 351 * cpu1: cpu@10000 { 352 * compatible = "arm,armv8"; 353 * reg = <0x0 0x100>; 354 * device_type = "cpu"; 355 * enable-method = "psci"; 356 * }; 357 * }; 358 * 359 * Full documentation about the CPU bindings can be found at: 360 * https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/cpus.txt 361 * 362 * Return the offset of the node or a negative value on error. 363 ******************************************************************************/ 364 365 int fdt_add_cpus_node(void *dtb, unsigned int afflv0, 366 unsigned int afflv1, unsigned int afflv2) 367 { 368 int offs; 369 int err; 370 unsigned int i, j, k; 371 u_register_t mpidr; 372 int cpuid; 373 374 if (fdt_path_offset(dtb, "/cpus") >= 0) { 375 return -EEXIST; 376 } 377 378 offs = fdt_add_subnode(dtb, 0, "cpus"); 379 if (offs < 0) { 380 ERROR ("FDT: add subnode \"cpus\" node to parent node failed"); 381 return offs; 382 } 383 384 err = fdt_setprop_u32(dtb, offs, "#address-cells", 2); 385 if (err < 0) { 386 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 387 "#address-cells", offs); 388 return err; 389 } 390 391 err = fdt_setprop_u32(dtb, offs, "#size-cells", 0); 392 if (err < 0) { 393 ERROR ("FDT: write to \"%s\" property of node at offset %i failed\n", 394 "#size-cells", offs); 395 return err; 396 } 397 398 /* 399 * Populate the node with the CPUs. 400 * As libfdt prepends subnodes within a node, reverse the index count 401 * so the CPU nodes would be better ordered. 402 */ 403 for (i = afflv2; i > 0U; i--) { 404 for (j = afflv1; j > 0U; j--) { 405 for (k = afflv0; k > 0U; k--) { 406 mpidr = ((i - 1) << MPIDR_AFF2_SHIFT) | 407 ((j - 1) << MPIDR_AFF1_SHIFT) | 408 ((k - 1) << MPIDR_AFF0_SHIFT) | 409 (read_mpidr_el1() & MPIDR_MT_MASK); 410 411 cpuid = plat_core_pos_by_mpidr(mpidr); 412 if (cpuid >= 0) { 413 /* Valid MPID found */ 414 err = fdt_add_cpu(dtb, offs, mpidr); 415 if (err < 0) { 416 ERROR ("FDT: %s 0x%08x\n", 417 "error adding CPU", 418 (uint32_t)mpidr); 419 return err; 420 } 421 } 422 } 423 } 424 } 425 426 return offs; 427 } 428 429 /******************************************************************************* 430 * fdt_add_cpu_idle_states() - add PSCI CPU idle states to cpu nodes in the DT 431 * @dtb: pointer to the device tree blob in memory 432 * @states: array of idle state descriptions, ending with empty element 433 * 434 * Add information about CPU idle states to the devicetree. This function 435 * assumes that CPU idle states are not already present in the devicetree, and 436 * that all CPU states are equally applicable to all CPUs. 437 * 438 * See arm/idle-states.yaml and arm/psci.yaml in the (Linux kernel) DT binding 439 * documentation for more details. 440 * 441 * Return: 0 on success, a negative error value otherwise. 442 ******************************************************************************/ 443 int fdt_add_cpu_idle_states(void *dtb, const struct psci_cpu_idle_state *state) 444 { 445 int cpu_node, cpus_node, idle_states_node, ret; 446 uint32_t count, phandle; 447 448 ret = fdt_find_max_phandle(dtb, &phandle); 449 phandle++; 450 if (ret < 0) { 451 return ret; 452 } 453 454 cpus_node = fdt_path_offset(dtb, "/cpus"); 455 if (cpus_node < 0) { 456 return cpus_node; 457 } 458 459 /* Create the idle-states node and its child nodes. */ 460 idle_states_node = fdt_add_subnode(dtb, cpus_node, "idle-states"); 461 if (idle_states_node < 0) { 462 return idle_states_node; 463 } 464 465 ret = fdt_setprop_string(dtb, idle_states_node, "entry-method", "psci"); 466 if (ret < 0) { 467 return ret; 468 } 469 470 for (count = 0U; state->name != NULL; count++, phandle++, state++) { 471 int idle_state_node; 472 473 idle_state_node = fdt_add_subnode(dtb, idle_states_node, 474 state->name); 475 if (idle_state_node < 0) { 476 return idle_state_node; 477 } 478 479 fdt_setprop_string(dtb, idle_state_node, "compatible", 480 "arm,idle-state"); 481 fdt_setprop_u32(dtb, idle_state_node, "arm,psci-suspend-param", 482 state->power_state); 483 if (state->local_timer_stop) { 484 fdt_setprop_empty(dtb, idle_state_node, 485 "local-timer-stop"); 486 } 487 fdt_setprop_u32(dtb, idle_state_node, "entry-latency-us", 488 state->entry_latency_us); 489 fdt_setprop_u32(dtb, idle_state_node, "exit-latency-us", 490 state->exit_latency_us); 491 fdt_setprop_u32(dtb, idle_state_node, "min-residency-us", 492 state->min_residency_us); 493 if (state->wakeup_latency_us) { 494 fdt_setprop_u32(dtb, idle_state_node, 495 "wakeup-latency-us", 496 state->wakeup_latency_us); 497 } 498 fdt_setprop_u32(dtb, idle_state_node, "phandle", phandle); 499 } 500 501 if (count == 0U) { 502 return 0; 503 } 504 505 /* Link each cpu node to the idle state nodes. */ 506 fdt_for_each_subnode(cpu_node, dtb, cpus_node) { 507 const char *device_type; 508 fdt32_t *value; 509 510 /* Only process child nodes with device_type = "cpu". */ 511 device_type = fdt_getprop(dtb, cpu_node, "device_type", NULL); 512 if (device_type == NULL || strcmp(device_type, "cpu") != 0) { 513 continue; 514 } 515 516 /* Allocate space for the list of phandles. */ 517 ret = fdt_setprop_placeholder(dtb, cpu_node, "cpu-idle-states", 518 count * sizeof(phandle), 519 (void **)&value); 520 if (ret < 0) { 521 return ret; 522 } 523 524 /* Fill in the phandles of the idle state nodes. */ 525 for (uint32_t i = 0U; i < count; ++i) { 526 value[i] = cpu_to_fdt32(phandle - count + i); 527 } 528 } 529 530 return 0; 531 } 532 533 /** 534 * fdt_adjust_gic_redist() - Adjust GICv3 redistributor size 535 * @dtb: Pointer to the DT blob in memory 536 * @nr_cores: Number of CPU cores on this system. 537 * @gicr_base: Base address of the first GICR frame, or ~0 if unchanged 538 * @gicr_frame_size: Size of the GICR frame per core 539 * 540 * On a GICv3 compatible interrupt controller, the redistributor provides 541 * a number of 64k pages per each supported core. So with a dynamic topology, 542 * this size cannot be known upfront and thus can't be hardcoded into the DTB. 543 * 544 * Find the DT node describing the GICv3 interrupt controller, and adjust 545 * the size of the redistributor to match the number of actual cores on 546 * this system. 547 * A GICv4 compatible redistributor uses four 64K pages per core, whereas GICs 548 * without support for direct injection of virtual interrupts use two 64K pages. 549 * The @gicr_frame_size parameter should be 262144 and 131072, respectively. 550 * Also optionally allow adjusting the GICR frame base address, when this is 551 * different due to ITS frames between distributor and redistributor. 552 * 553 * Return: 0 on success, negative error value otherwise. 554 */ 555 int fdt_adjust_gic_redist(void *dtb, unsigned int nr_cores, 556 uintptr_t gicr_base, unsigned int gicr_frame_size) 557 { 558 int offset = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-v3"); 559 uint64_t reg_64; 560 uint32_t reg_32; 561 void *val; 562 int parent, ret; 563 int ac, sc; 564 565 if (offset < 0) { 566 return offset; 567 } 568 569 parent = fdt_parent_offset(dtb, offset); 570 if (parent < 0) { 571 return parent; 572 } 573 ac = fdt_address_cells(dtb, parent); 574 sc = fdt_size_cells(dtb, parent); 575 if (ac < 0 || sc < 0) { 576 return -EINVAL; 577 } 578 579 if (gicr_base != INVALID_BASE_ADDR) { 580 if (ac == 1) { 581 reg_32 = cpu_to_fdt32(gicr_base); 582 val = ®_32; 583 } else { 584 reg_64 = cpu_to_fdt64(gicr_base); 585 val = ®_64; 586 } 587 /* 588 * The redistributor base address is the second address in 589 * the "reg" entry, so we have to skip one address and one 590 * size cell. 591 */ 592 ret = fdt_setprop_inplace_namelen_partial(dtb, offset, 593 "reg", 3, 594 (ac + sc) * 4, 595 val, ac * 4); 596 if (ret < 0) { 597 return ret; 598 } 599 } 600 601 if (sc == 1) { 602 reg_32 = cpu_to_fdt32(nr_cores * gicr_frame_size); 603 val = ®_32; 604 } else { 605 reg_64 = cpu_to_fdt64(nr_cores * (uint64_t)gicr_frame_size); 606 val = ®_64; 607 } 608 609 /* 610 * The redistributor is described in the second "reg" entry. 611 * So we have to skip one address and one size cell, then another 612 * address cell to get to the second size cell. 613 */ 614 return fdt_setprop_inplace_namelen_partial(dtb, offset, "reg", 3, 615 (ac + sc + ac) * 4, 616 val, sc * 4); 617 } 618 /** 619 * fdt_set_mac_address () - store MAC address in device tree 620 * @dtb: pointer to the device tree blob in memory 621 * @eth_idx: number of Ethernet interface in /aliases node 622 * @mac_addr: pointer to 6 byte MAC address to store 623 * 624 * Use the generic local-mac-address property in a network device DT node 625 * to define the MAC address this device should be using. Many platform 626 * network devices lack device-specific non-volatile storage to hold this 627 * address, and leave it up to firmware to find and store a unique MAC 628 * address in the DT. 629 * The MAC address could be read from some board or firmware defined storage, 630 * or could be derived from some other unique property like a serial number. 631 * 632 * Return: 0 on success, a negative libfdt error value otherwise. 633 */ 634 int fdt_set_mac_address(void *dtb, unsigned int ethernet_idx, 635 const uint8_t *mac_addr) 636 { 637 char eth_alias[12]; 638 const char *path; 639 int node; 640 641 if (ethernet_idx > 9U) { 642 return -FDT_ERR_BADVALUE; 643 } 644 snprintf(eth_alias, sizeof(eth_alias), "ethernet%d", ethernet_idx); 645 646 path = fdt_get_alias(dtb, eth_alias); 647 if (path == NULL) { 648 return -FDT_ERR_NOTFOUND; 649 } 650 651 node = fdt_path_offset(dtb, path); 652 if (node < 0) { 653 ERROR("Path \"%s\" not found in DT: %d\n", path, node); 654 return node; 655 } 656 657 return fdt_setprop(dtb, node, "local-mac-address", mac_addr, 6); 658 } 659