plat_fdt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <common/debug.h>
  8. #include <common/fdt_fixup.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <libfdt.h>
  11. #include <lib/xlat_tables/xlat_tables_v2.h>
  12. #include <plat_fdt.h>
  13. #include <platform_def.h>
  14. #if defined(XILINX_OF_BOARD_DTB_ADDR)
  15. #define FIT_CONFS_PATH "/configurations"
  16. static uint8_t is_fit_image(void *dtb)
  17. {
  18. int64_t confs_noffset;
  19. uint8_t status = 0;
  20. confs_noffset = fdt_path_offset(dtb, FIT_CONFS_PATH);
  21. /*confs_noffset is only present on FIT image */
  22. if (confs_noffset < 0) {
  23. status = 0;
  24. } else {
  25. status = 1;
  26. }
  27. return status;
  28. }
  29. int32_t is_valid_dtb(void *fdt)
  30. {
  31. int32_t ret = 0;
  32. if (fdt_check_header(fdt) != 0) {
  33. ERROR("Can't read DT at %p\n", fdt);
  34. ret = -FDT_ERR_NOTFOUND;
  35. goto error;
  36. }
  37. ret = fdt_open_into(fdt, fdt, XILINX_OF_BOARD_DTB_MAX_SIZE);
  38. if (ret < 0) {
  39. ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
  40. ret = -FDT_ERR_NOTFOUND;
  41. goto error;
  42. }
  43. if (is_fit_image(fdt) != 0U) {
  44. WARN("FIT image detected, TF-A will not update DTB for DDR address space\n");
  45. ret = -FDT_ERR_NOTFOUND;
  46. }
  47. error:
  48. return ret;
  49. }
  50. static int add_mmap_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
  51. size_t size, unsigned int attr)
  52. {
  53. int ret = 0;
  54. #if defined(PLAT_XLAT_TABLES_DYNAMIC)
  55. ret = mmap_add_dynamic_region(base_pa, base_va, size, attr);
  56. if (ret != 0) {
  57. WARN("Failed to add dynamic region for dtb: error %d\n",
  58. ret);
  59. }
  60. #endif
  61. return ret;
  62. }
  63. static int remove_mmap_dynamic_region(uintptr_t base_va, size_t size)
  64. {
  65. int ret = 0;
  66. #if defined(PLAT_XLAT_TABLES_DYNAMIC)
  67. ret = mmap_remove_dynamic_region(base_va, size);
  68. if (ret != 0) {
  69. WARN("Failed to remove dynamic region for dtb:error %d\n",
  70. ret);
  71. }
  72. #endif
  73. return ret;
  74. }
  75. #endif
  76. #if defined(XILINX_OF_BOARD_DTB_ADDR)
  77. static int check_fdt_reserved_memory(void *dtb, const char *node_name)
  78. {
  79. int offset = fdt_path_offset(dtb, "/reserved-memory");
  80. if (offset >= 0) {
  81. offset = fdt_subnode_offset(dtb, offset, node_name);
  82. }
  83. return offset;
  84. }
  85. #endif
  86. void prepare_dtb(void)
  87. {
  88. #if defined(XILINX_OF_BOARD_DTB_ADDR)
  89. void *dtb;
  90. int map_ret = 0;
  91. int ret = 0;
  92. dtb = (void *)XILINX_OF_BOARD_DTB_ADDR;
  93. if (!IS_TFA_IN_OCM(BL31_BASE)) {
  94. map_ret = add_mmap_dynamic_region((unsigned long long)dtb,
  95. (uintptr_t)dtb,
  96. XILINX_OF_BOARD_DTB_MAX_SIZE,
  97. MT_MEMORY | MT_RW | MT_NS);
  98. if (map_ret == 0) {
  99. /* Return if no device tree is detected */
  100. if (is_valid_dtb(dtb) == 0) {
  101. if (dt_add_psci_node(dtb)) {
  102. WARN("Failed to add PSCI Device Tree node\n");
  103. }
  104. if (dt_add_psci_cpu_enable_methods(dtb)) {
  105. WARN("Failed to add PSCI cpu enable methods in DT\n");
  106. }
  107. /* Check reserved memory set in DT*/
  108. ret = check_fdt_reserved_memory(dtb, "tf-a");
  109. if (ret < 0) {
  110. /* Reserve memory used by Trusted Firmware. */
  111. ret = fdt_add_reserved_memory(dtb, "tf-a",
  112. BL31_BASE,
  113. BL31_LIMIT - BL31_BASE);
  114. if (ret < 0) {
  115. WARN("Failed to add reserved memory nodes for BL31 to DT.\n");
  116. }
  117. } else {
  118. WARN("Reserved memory pre-exists in DT.\n");
  119. }
  120. ret = fdt_pack(dtb);
  121. if (ret < 0) {
  122. WARN("Failed to pack dtb at %p: error %d\n", dtb, ret);
  123. }
  124. flush_dcache_range((uintptr_t)dtb, fdt_blob_size(dtb));
  125. INFO("Changed device tree to advertise PSCI and reserved memories.\n");
  126. }
  127. ret = remove_mmap_dynamic_region((uintptr_t)dtb,
  128. XILINX_OF_BOARD_DTB_MAX_SIZE);
  129. if (ret != 0) {
  130. WARN("Failed to remove mmap dynamic regions.\n");
  131. }
  132. }
  133. }
  134. #endif
  135. }