rpi4_setup.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <inttypes.h>
  8. #include <stdint.h>
  9. #include <arch_helpers.h>
  10. #include <common/fdt_fixup.h>
  11. #include <common/fdt_wrappers.h>
  12. #include <rpi_shared.h>
  13. /*
  14. * Remove the FDT /memreserve/ entry that covers the region at the very
  15. * beginning of memory (if that exists). This is where the secondaries
  16. * originally spin, but we pull them out there.
  17. * Having overlapping /reserved-memory and /memreserve/ regions confuses
  18. * the Linux kernel, so we need to get rid of this one.
  19. */
  20. static void remove_spintable_memreserve(void *dtb)
  21. {
  22. uint64_t addr, size;
  23. int regions = fdt_num_mem_rsv(dtb);
  24. int i;
  25. for (i = 0; i < regions; i++) {
  26. if (fdt_get_mem_rsv(dtb, i, &addr, &size) != 0) {
  27. return;
  28. }
  29. if (size == 0U) {
  30. return;
  31. }
  32. /* We only look for the region at the beginning of DRAM. */
  33. if (addr != 0U) {
  34. continue;
  35. }
  36. /*
  37. * Currently the region in the existing DTs is exactly 4K
  38. * in size. Should this value ever change, there is probably
  39. * a reason for that, so inform the user about this.
  40. */
  41. if (size == 4096U) {
  42. fdt_del_mem_rsv(dtb, i);
  43. return;
  44. }
  45. WARN("Keeping unknown /memreserve/ region at 0, size: %" PRId64 "\n",
  46. size);
  47. }
  48. }
  49. static void rpi4_prepare_dtb(void)
  50. {
  51. void *dtb = (void *)rpi4_get_dtb_address();
  52. uint32_t gic_int_prop[3];
  53. int ret, offs;
  54. /* Return if no device tree is detected */
  55. if (fdt_check_header(dtb) != 0)
  56. return;
  57. ret = fdt_open_into(dtb, dtb, 0x100000);
  58. if (ret < 0) {
  59. ERROR("Invalid Device Tree at %p: error %d\n", dtb, ret);
  60. return;
  61. }
  62. if (dt_add_psci_node(dtb)) {
  63. ERROR("Failed to add PSCI Device Tree node\n");
  64. return;
  65. }
  66. if (dt_add_psci_cpu_enable_methods(dtb)) {
  67. ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
  68. return;
  69. }
  70. /*
  71. * Remove the original reserved region (used for the spintable), and
  72. * replace it with a region describing the whole of Trusted Firmware.
  73. */
  74. remove_spintable_memreserve(dtb);
  75. if (fdt_add_reserved_memory(dtb, "atf@0", 0, 0x80000))
  76. WARN("Failed to add reserved memory nodes to DT.\n");
  77. offs = fdt_node_offset_by_compatible(dtb, 0, "arm,gic-400");
  78. gic_int_prop[0] = cpu_to_fdt32(1); // PPI
  79. gic_int_prop[1] = cpu_to_fdt32(9); // PPI #9
  80. gic_int_prop[2] = cpu_to_fdt32(0x0f04); // all cores, level high
  81. fdt_setprop(dtb, offs, "interrupts", gic_int_prop, 12);
  82. offs = fdt_path_offset(dtb, "/chosen");
  83. fdt_setprop_string(dtb, offs, "stdout-path", "serial0");
  84. ret = fdt_pack(dtb);
  85. if (ret < 0)
  86. ERROR("Failed to pack Device Tree at %p: error %d\n", dtb, ret);
  87. clean_dcache_range((uintptr_t)dtb, fdt_blob_size(dtb));
  88. INFO("Changed device tree to advertise PSCI.\n");
  89. }
  90. void plat_rpi_bl31_custom_setup(void)
  91. {
  92. rpi4_prepare_dtb();
  93. }