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