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