• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2024, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <common/debug.h>
11 #include <common/desc_image_load.h>
12 #include <common/fdt_wrappers.h>
13 #include <drivers/io/io_storage.h>
14 #include <lib/object_pool.h>
15 #include <libfdt.h>
16 #include <plat/arm/common/arm_fconf_getter.h>
17 #include <plat/arm/common/arm_fconf_io_storage.h>
18 #include <plat/arm/common/fconf_arm_sp_getter.h>
19 #include <platform_def.h>
20 #include <tools_share/firmware_image_package.h>
21 
22 #ifdef IMAGE_BL2
23 
24 bl_mem_params_node_t sp_mem_params_descs[MAX_SP_IDS];
25 
26 struct arm_sp_t arm_sp;
27 
fconf_populate_arm_sp(uintptr_t config)28 int fconf_populate_arm_sp(uintptr_t config)
29 {
30 	int sp_node, node, err;
31 	struct uuid uuid;
32 	unsigned int index = 0;
33 	uint32_t val32;
34 	const unsigned int sip_start = SP_PKG1_ID;
35 	unsigned int sip_index = sip_start;
36 #if defined(ARM_COT_dualroot)
37 	const unsigned int sip_end = sip_start + MAX_SP_IDS / 2;
38 	/* Allocating index range for platform SPs */
39 	const unsigned int plat_start = SP_PKG5_ID;
40 	unsigned int plat_index = plat_start;
41 	const unsigned int plat_end = plat_start + MAX_SP_IDS / 2;
42 	bool is_plat_owned = false;
43 #endif /* ARM_COT_dualroot */
44 
45 	/* As libfdt use void *, we can't avoid this cast */
46 	const void *dtb = (void *)config;
47 
48 	/* Assert the node offset point to "arm,sp" compatible property */
49 	const char *compatible_str = "arm,sp";
50 
51 	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
52 	if (node < 0) {
53 		ERROR("FCONF: Can't find %s in dtb\n", compatible_str);
54 		return node;
55 	}
56 
57 	fdt_for_each_subnode(sp_node, dtb, node) {
58 		if (index == MAX_SP_IDS) {
59 			ERROR("FCONF: Reached max number of SPs\n");
60 			return -1;
61 		}
62 
63 #if defined(ARM_COT_dualroot)
64 		if ((sip_index == sip_end) || (plat_index == plat_end)) {
65 			ERROR("FCONF: Reached max number of plat/SiP SPs\n");
66 			return -1;
67 		}
68 #endif /* ARM_COT_dualroot */
69 
70 		/* Read UUID */
71 		err = fdtw_read_uuid(dtb, sp_node, "uuid", 16,
72 				     (uint8_t *)&uuid);
73 		if (err < 0) {
74 			ERROR("FCONF: cannot read SP uuid\n");
75 			return -1;
76 		}
77 
78 		memcpy_s(&arm_sp.uuids[index].uuid_struct, sizeof(struct uuid),
79 			 &uuid, sizeof(struct uuid));
80 
81 		/* Read Load address */
82 		err = fdt_read_uint32(dtb, sp_node, "load-address", &val32);
83 		if (err < 0) {
84 			ERROR("FCONF: cannot read SP load address\n");
85 			return -1;
86 		}
87 		arm_sp.load_addr[index] = val32;
88 
89 		VERBOSE("FCONF: %s UUID"
90 			" %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
91 			" load_addr=%lx\n",
92 			__func__,
93 			uuid.time_low[0], uuid.time_low[1],
94 			uuid.time_low[2], uuid.time_low[3],
95 			uuid.time_mid[0], uuid.time_mid[1],
96 			uuid.time_hi_and_version[0],
97 			uuid.time_hi_and_version[1],
98 			uuid.clock_seq_hi_and_reserved,
99 			uuid.clock_seq_low,
100 			uuid.node[0], uuid.node[1],
101 			uuid.node[2], uuid.node[3],
102 			uuid.node[4], uuid.node[5],
103 			arm_sp.load_addr[index]);
104 
105 		/* Read owner field only for dualroot CoT */
106 #if defined(ARM_COT_dualroot)
107 		/* Owner is an optional field, no need to catch error */
108 		fdtw_read_string(dtb, sp_node, "owner",
109 				arm_sp.owner[index], ARM_SP_OWNER_NAME_LEN);
110 
111 		/* If owner is empty mark it as SiP owned */
112 		if ((strncmp(arm_sp.owner[index], "SiP",
113 			     ARM_SP_OWNER_NAME_LEN) == 0) ||
114 		    (strncmp(arm_sp.owner[index], "",
115 			     ARM_SP_OWNER_NAME_LEN) == 0)) {
116 			is_plat_owned = false;
117 		} else if (strcmp(arm_sp.owner[index], "Plat") == 0) {
118 			is_plat_owned = true;
119 		} else {
120 			ERROR("FCONF: %s is not a valid SP owner\n",
121 			      arm_sp.owner[index]);
122 			return -1;
123 		}
124 		/*
125 		 * Add SP information in mem param descriptor and IO policies
126 		 * structure.
127 		 */
128 		if (is_plat_owned) {
129 			sp_mem_params_descs[index].image_id = plat_index;
130 			policies[plat_index].image_spec =
131 						(uintptr_t)&arm_sp.uuids[index];
132 			policies[plat_index].dev_handle = &fip_dev_handle;
133 			policies[plat_index].check = open_fip;
134 			plat_index++;
135 		} else
136 #endif /* ARM_COT_dualroot */
137 		{
138 			sp_mem_params_descs[index].image_id = sip_index;
139 			policies[sip_index].image_spec =
140 						(uintptr_t)&arm_sp.uuids[index];
141 			policies[sip_index].dev_handle = &fip_dev_handle;
142 			policies[sip_index].check = open_fip;
143 			sip_index++;
144 		}
145 		SET_PARAM_HEAD(&sp_mem_params_descs[index].image_info,
146 					PARAM_IMAGE_BINARY, VERSION_2, 0);
147 		sp_mem_params_descs[index].image_info.image_max_size =
148 							ARM_SP_MAX_SIZE;
149 		sp_mem_params_descs[index].next_handoff_image_id =
150 							INVALID_IMAGE_ID;
151 		sp_mem_params_descs[index].image_info.image_base =
152 							arm_sp.load_addr[index];
153 		index++;
154 	}
155 
156 	if ((sp_node < 0) && (sp_node != -FDT_ERR_NOTFOUND)) {
157 		ERROR("%u: fdt_for_each_subnode(): %d\n", __LINE__, node);
158 		return sp_node;
159 	}
160 
161 	arm_sp.number_of_sp = index;
162 	return 0;
163 }
164 
165 FCONF_REGISTER_POPULATOR(TB_FW, arm_sp, fconf_populate_arm_sp);
166 
167 #endif /* IMAGE_BL2 */
168