1 /*
2  * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <common/debug.h>
9 #include <common/desc_image_load.h>
10 #include <drivers/arm/css/sds.h>
11 #include <libfdt.h>
12 
13 #include "morello_def.h"
14 #include <plat/arm/common/plat_arm.h>
15 #include <plat/common/platform.h>
16 
17 /* In client mode, a part of the DDR memory is reserved for Tag bits.
18  * Calculate the usable memory size after subtracting the Tag memory.
19  */
get_mem_client_mode(uint64_t size)20 static inline uint64_t get_mem_client_mode(uint64_t size)
21 {
22 	return (size - (size / 128ULL));
23 }
24 
25 /*******************************************************************************
26  * This function inserts Platform information and firmware versions
27  * via device tree nodes as,
28  *	platform-info {
29  *		local-ddr-size = <0x0 0x0>;
30  *#ifdef TARGET_PLATFORM_SOC
31  *		remote-ddr-size = <0x0 0x0>;
32  *		remote-chip-count = <0x0>;
33  *		multichip-mode = <0x0>;
34  *		scc-config = <0x0>;
35  *#endif
36  *	};
37  *	firmware-version {
38  *#ifdef TARGET_PLATFORM_SOC
39  *		mcc-fw-version = <0x0>;
40  *		pcc-fw-version = <0x0>;
41  *#endif
42  *		scp-fw-version = <0x0>;
43  *		scp-fw-commit = <0x0>;
44  *		tfa-fw-version = "unknown-dirty_00000000";
45  *	};
46  ******************************************************************************/
plat_morello_append_config_node(struct morello_plat_info * plat_info,struct morello_firmware_version * fw_version)47 static int plat_morello_append_config_node(struct morello_plat_info *plat_info,
48 				struct morello_firmware_version *fw_version)
49 {
50 	bl_mem_params_node_t *mem_params;
51 	void *fdt;
52 	int nodeoffset_plat, nodeoffset_fw, err;
53 	uint64_t usable_mem_size;
54 
55 	usable_mem_size = plat_info->local_ddr_size;
56 
57 	mem_params = get_bl_mem_params_node(NT_FW_CONFIG_ID);
58 	if (mem_params == NULL) {
59 		ERROR("NT_FW CONFIG base address is NULL\n");
60 		return -1;
61 	}
62 
63 	fdt = (void *)(mem_params->image_info.image_base);
64 
65 	/* Check the validity of the fdt */
66 	if (fdt_check_header(fdt) != 0) {
67 		ERROR("Invalid NT_FW_CONFIG DTB passed\n");
68 		return -1;
69 	}
70 
71 	nodeoffset_plat = fdt_subnode_offset(fdt, 0, "platform-info");
72 	if (nodeoffset_plat < 0) {
73 		ERROR("NT_FW_CONFIG: Failed to get platform-info node offset\n");
74 		return -1;
75 	}
76 
77 	nodeoffset_fw = fdt_subnode_offset(fdt, 0, "firmware-version");
78 	if (nodeoffset_fw < 0) {
79 		ERROR("NT_FW_CONFIG: Failed to get firmware-version node offset\n");
80 		return -1;
81 	}
82 
83 #ifdef TARGET_PLATFORM_SOC
84 	err = fdt_setprop_u64(fdt, nodeoffset_plat, "remote-ddr-size",
85 			plat_info->remote_ddr_size);
86 	if (err < 0) {
87 		ERROR("NT_FW_CONFIG: Failed to set remote-ddr-size\n");
88 		return -1;
89 	}
90 
91 	err = fdt_setprop_u32(fdt, nodeoffset_plat, "remote-chip-count",
92 			plat_info->remote_chip_count);
93 	if (err < 0) {
94 		ERROR("NT_FW_CONFIG: Failed to set remote-chip-count\n");
95 		return -1;
96 	}
97 
98 	err = fdt_setprop_u32(fdt, nodeoffset_plat, "multichip-mode",
99 			plat_info->multichip_mode);
100 	if (err < 0) {
101 		ERROR("NT_FW_CONFIG: Failed to set multichip-mode\n");
102 		return -1;
103 	}
104 
105 	err = fdt_setprop_u32(fdt, nodeoffset_plat, "scc-config",
106 			plat_info->scc_config);
107 	if (err < 0) {
108 		ERROR("NT_FW_CONFIG: Failed to set scc-config\n");
109 		return -1;
110 	}
111 
112 	if (plat_info->scc_config & MORELLO_SCC_CLIENT_MODE_MASK) {
113 		usable_mem_size = get_mem_client_mode(plat_info->local_ddr_size);
114 	}
115 
116 	err = fdt_setprop_u32(fdt, nodeoffset_fw, "mcc-fw-version",
117 			fw_version->mcc_fw_ver);
118 	if (err < 0) {
119 		ERROR("NT_FW_CONFIG: Failed to set mcc-fw-version\n");
120 		return -1;
121 	}
122 
123 	err = fdt_setprop_u32(fdt, nodeoffset_fw, "pcc-fw-version",
124 			fw_version->pcc_fw_ver);
125 	if (err < 0) {
126 		ERROR("NT_FW_CONFIG: Failed to set pcc-fw-version\n");
127 		return -1;
128 	}
129 #endif
130 	err = fdt_setprop_u32(fdt, nodeoffset_fw, "scp-fw-version",
131 			fw_version->scp_fw_ver);
132 	if (err < 0) {
133 		ERROR("NT_FW_CONFIG: Failed to set scp-fw-version\n");
134 		return -1;
135 	}
136 
137 	err = fdt_setprop_u32(fdt, nodeoffset_fw, "scp-fw-commit",
138 			fw_version->scp_fw_commit);
139 	if (err < 0) {
140 		ERROR("NT_FW_CONFIG: Failed to set scp-fw-commit\n");
141 		return -1;
142 	}
143 
144 	err = fdt_setprop_string(fdt, nodeoffset_fw, "tfa-fw-version", version_string);
145 	if (err < 0) {
146 		WARN("NT_FW_CONFIG: Unable to set tfa-fw-version\n");
147 	}
148 
149 	err = fdt_setprop_u64(fdt, nodeoffset_plat, "local-ddr-size",
150 			usable_mem_size);
151 	if (err < 0) {
152 		ERROR("NT_FW_CONFIG: Failed to set local-ddr-size\n");
153 		return -1;
154 	}
155 
156 	flush_dcache_range((uintptr_t)fdt, mem_params->image_info.image_size);
157 
158 	return 0;
159 }
160 
161 /*******************************************************************************
162  * This function returns the list of executable images.
163  ******************************************************************************/
plat_get_next_bl_params(void)164 bl_params_t *plat_get_next_bl_params(void)
165 {
166 	int ret;
167 	struct morello_plat_info plat_info;
168 	struct morello_firmware_version fw_version;
169 
170 	ret = sds_init();
171 	if (ret != SDS_OK) {
172 		ERROR("SDS initialization failed. ret:%d\n", ret);
173 		panic();
174 	}
175 
176 	ret = sds_struct_read(MORELLO_SDS_PLATFORM_INFO_STRUCT_ID,
177 				MORELLO_SDS_PLATFORM_INFO_OFFSET,
178 				&plat_info,
179 				MORELLO_SDS_PLATFORM_INFO_SIZE,
180 				SDS_ACCESS_MODE_NON_CACHED);
181 	if (ret != SDS_OK) {
182 		ERROR("Error getting platform info from SDS. ret:%d\n", ret);
183 		panic();
184 	}
185 
186 	ret = sds_struct_read(MORELLO_SDS_FIRMWARE_VERSION_STRUCT_ID,
187 				MORELLO_SDS_FIRMWARE_VERSION_OFFSET,
188 				&fw_version,
189 				MORELLO_SDS_FIRMWARE_VERSION_SIZE,
190 				SDS_ACCESS_MODE_NON_CACHED);
191 	if (ret != SDS_OK) {
192 		ERROR("Error getting firmware version from SDS. ret:%d\n", ret);
193 		panic();
194 	}
195 
196 	/* Validate plat_info SDS */
197 #ifdef TARGET_PLATFORM_FVP
198 	if (plat_info.local_ddr_size == 0U) {
199 #else
200 	if ((plat_info.local_ddr_size == 0U)
201 		|| (plat_info.local_ddr_size > MORELLO_MAX_DDR_CAPACITY)
202 		|| (plat_info.remote_ddr_size > MORELLO_MAX_DDR_CAPACITY)
203 		|| (plat_info.remote_chip_count > MORELLO_MAX_REMOTE_CHIP_COUNT)
204 		){
205 #endif
206 		ERROR("platform info SDS is corrupted\n");
207 		panic();
208 	}
209 
210 	ret = plat_morello_append_config_node(&plat_info, &fw_version);
211 	if (ret != 0) {
212 		panic();
213 	}
214 
215 	return arm_get_next_bl_params();
216 }
217