blob: 0c72cb7aa82514616b31da89f6bd5ff6282bf2e5 [file] [log] [blame]
Antonio Nino Diazb86edcb2018-10-30 11:12:42 +00001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
9
Antonio Nino Diazb86edcb2018-10-30 11:12:42 +000010#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Antonio Nino Diazb86edcb2018-10-30 11:12:42 +000012#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013
14#include <common/debug.h>
15#include <common/fdt_wrappers.h>
16#include <lib/object_pool.h>
17#include <services/sp_res_desc.h>
Antonio Nino Diazb86edcb2018-10-30 11:12:42 +000018
19/*******************************************************************************
20 * Resource pool
21 ******************************************************************************/
22static struct sp_rd_sect_mem_region rd_mem_regions[PLAT_SPM_MEM_REGIONS_MAX];
23static OBJECT_POOL_ARRAY(rd_mem_regions_pool, rd_mem_regions);
24
25static struct sp_rd_sect_notification rd_notifs[PLAT_SPM_NOTIFICATIONS_MAX];
26static OBJECT_POOL_ARRAY(rd_notifs_pool, rd_notifs);
27
28static struct sp_rd_sect_service rd_services[PLAT_SPM_SERVICES_MAX];
29static OBJECT_POOL_ARRAY(rd_services_pool, rd_services);
30
31/*******************************************************************************
32 * Attribute section handler
33 ******************************************************************************/
34static void rd_parse_attribute(struct sp_rd_sect_attribute *attr,
35 const void *fdt, int node)
36{
37 int rc = 0;
38
39 /* The minimum size that can be read from the DTB is 32-bit. */
40 uint32_t version, sp_type, runtime_el, exec_type;
41 uint32_t panic_policy, xlat_granule;
42
43 rc |= fdtw_read_cells(fdt, node, "version", 1, &version);
44
45 if (version != 1) {
46 ERROR("Unsupported resource description version: 0x%x\n",
47 version);
48 panic();
49 }
50
51 rc |= fdtw_read_cells(fdt, node, "sp_type", 1, &sp_type);
52 rc |= fdtw_read_cells(fdt, node, "pe_mpidr", 1, &attr->pe_mpidr);
53 rc |= fdtw_read_cells(fdt, node, "runtime_el", 1, &runtime_el);
54 rc |= fdtw_read_cells(fdt, node, "exec_type", 1, &exec_type);
55 rc |= fdtw_read_cells(fdt, node, "panic_policy", 1, &panic_policy);
56 rc |= fdtw_read_cells(fdt, node, "xlat_granule", 1, &xlat_granule);
57 rc |= fdtw_read_cells(fdt, node, "binary_size", 1, &attr->binary_size);
58 rc |= fdtw_read_cells(fdt, node, "load_address", 2, &attr->load_address);
59 rc |= fdtw_read_cells(fdt, node, "entrypoint", 2, &attr->entrypoint);
60
61 attr->version = version;
62 attr->sp_type = sp_type;
63 attr->runtime_el = runtime_el;
64 attr->exec_type = exec_type;
65 attr->panic_policy = panic_policy;
66 attr->xlat_granule = xlat_granule;
67
68 VERBOSE(" Attribute Section:\n");
69 VERBOSE(" version: 0x%x\n", version);
70 VERBOSE(" sp_type: 0x%x\n", sp_type);
71 VERBOSE(" pe_mpidr: 0x%x\n", attr->pe_mpidr);
72 VERBOSE(" runtime_el: 0x%x\n", runtime_el);
73 VERBOSE(" exec_type: 0x%x\n", exec_type);
74 VERBOSE(" panic_policy: 0x%x\n", panic_policy);
75 VERBOSE(" xlat_granule: 0x%x\n", xlat_granule);
76 VERBOSE(" binary_size: 0x%x\n", attr->binary_size);
77 VERBOSE(" load_address: 0x%llx\n", attr->load_address);
78 VERBOSE(" entrypoint: 0x%llx\n", attr->entrypoint);
79
80 if (rc) {
81 ERROR("Failed to read attribute node elements.\n");
82 panic();
83 }
84}
85
86/*******************************************************************************
87 * Memory regions section handlers
88 ******************************************************************************/
89static void rd_parse_memory_region(struct sp_rd_sect_mem_region *rdmem,
90 const void *fdt, int node)
91{
92 int rc = 0;
93 char name[RD_MEM_REGION_NAME_LEN];
94
95 rc |= fdtw_read_string(fdt, node, "str", (char *)&name, sizeof(name));
96 rc |= fdtw_read_cells(fdt, node, "attr", 1, &rdmem->attr);
97 rc |= fdtw_read_cells(fdt, node, "base", 2, &rdmem->base);
98 rc |= fdtw_read_cells(fdt, node, "size", 2, &rdmem->size);
99
100 size_t len = strlcpy(rdmem->name, name, RD_MEM_REGION_NAME_LEN);
101
102 if (len >= RD_MEM_REGION_NAME_LEN) {
103 WARN("Memory region name truncated: '%s'\n", name);
104 }
105
106 VERBOSE(" Memory Region:\n");
107 VERBOSE(" name: '%s'\n", rdmem->name);
108 VERBOSE(" attr: 0x%x\n", rdmem->attr);
109 VERBOSE(" base: 0x%llx\n", rdmem->base);
110 VERBOSE(" size: 0x%llx\n", rdmem->size);
111
112 if (rc) {
113 ERROR("Failed to read mem_region node elements.\n");
114 panic();
115 }
116}
117
118static void rd_parse_memory_regions(struct sp_res_desc *rd, const void *fdt,
119 int node)
120{
121 int child;
122 struct sp_rd_sect_mem_region *rdmem, *old_rdmem;
123
124 fdt_for_each_subnode(child, fdt, node) {
125 rdmem = pool_alloc(&rd_mem_regions_pool);
126
127 /* Add element to the start of the list */
128 old_rdmem = rd->mem_region;
129 rd->mem_region = rdmem;
130 rdmem->next = old_rdmem;
131
132 rd_parse_memory_region(rdmem, fdt, child);
133 }
134
135 if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
136 ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, node);
137 panic();
138 }
139}
140
141/*******************************************************************************
142 * Notifications section handlers
143 ******************************************************************************/
144static void rd_parse_notification(struct sp_rd_sect_notification *rdnot,
145 const void *fdt, int node)
146{
147 int rc = 0;
148
149 rc |= fdtw_read_cells(fdt, node, "attr", 1, &rdnot->attr);
150 rc |= fdtw_read_cells(fdt, node, "pe", 1, &rdnot->pe);
151
152 VERBOSE(" Notification:\n");
153 VERBOSE(" attr: 0x%x\n", rdnot->attr);
154 VERBOSE(" pe: 0x%x\n", rdnot->pe);
155
156 if (rc) {
157 ERROR("Failed to read notification node elements.\n");
158 panic();
159 }
160}
161
162static void rd_parse_notifications(struct sp_res_desc *rd, const void *fdt, int node)
163{
164 int child;
165 struct sp_rd_sect_notification *rdnot, *old_rdnot;
166
167 fdt_for_each_subnode(child, fdt, node) {
168 rdnot = pool_alloc(&rd_notifs_pool);
169
170 /* Add element to the start of the list */
171 old_rdnot = rd->notification;
172 rd->notification = rdnot;
173 rdnot->next = old_rdnot;
174
175 rd_parse_notification(rdnot, fdt, child);
176 }
177
178 if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
179 ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, child);
180 panic();
181 }
182}
183
184/*******************************************************************************
185 * Services section handlers
186 ******************************************************************************/
187static void rd_parse_service(struct sp_rd_sect_service *rdsvc, const void *fdt,
188 int node)
189{
190 int rc = 0;
191
192 /* The minimum size that can be read from the DTB is 32-bit. */
193 uint32_t accessibility, request_type, connection_quota;
194
195 rc |= fdtw_read_array(fdt, node, "uuid", 4, &rdsvc->uuid);
196 rc |= fdtw_read_cells(fdt, node, "accessibility", 1, &accessibility);
197 rc |= fdtw_read_cells(fdt, node, "request_type", 1, &request_type);
198 rc |= fdtw_read_cells(fdt, node, "connection_quota", 1, &connection_quota);
199 rc |= fdtw_read_cells(fdt, node, "sec_mem_size", 1, &rdsvc->secure_mem_size);
200 rc |= fdtw_read_cells(fdt, node, "interrupt_num", 1, &rdsvc->interrupt_num);
201
202 rdsvc->accessibility = accessibility;
203 rdsvc->request_type = request_type;
204 rdsvc->connection_quota = connection_quota;
205
206 VERBOSE(" Service:\n");
207 VERBOSE(" uuid: 0x%08x 0x%08x 0x%08x 0x%08x\n", rdsvc->uuid[0],
208 rdsvc->uuid[1], rdsvc->uuid[2], rdsvc->uuid[3]);
209 VERBOSE(" accessibility: 0x%x\n", accessibility);
210 VERBOSE(" request_type: 0x%x\n", request_type);
211 VERBOSE(" connection_quota: 0x%x\n", connection_quota);
212 VERBOSE(" secure_memory_size: 0x%x\n", rdsvc->secure_mem_size);
213 VERBOSE(" interrupt_num: 0x%x\n", rdsvc->interrupt_num);
214
215 if (rc) {
216 ERROR("Failed to read attribute node elements.\n");
217 panic();
218 }
219}
220
221static void rd_parse_services(struct sp_res_desc *rd, const void *fdt, int node)
222{
223 int child;
224 struct sp_rd_sect_service *rdsvc, *old_rdsvc;
225
226 fdt_for_each_subnode(child, fdt, node) {
227 rdsvc = pool_alloc(&rd_services_pool);
228
229 /* Add element to the start of the list */
230 old_rdsvc = rd->service;
231 rd->service = rdsvc;
232 rdsvc->next = old_rdsvc;
233
234 rd_parse_service(rdsvc, fdt, child);
235 }
236
237 if ((child < 0) && (child != -FDT_ERR_NOTFOUND)) {
238 ERROR("%d: fdt_for_each_subnode(): %d\n", __LINE__, node);
239 panic();
240 }
241}
242
243/*******************************************************************************
244 * Root node handler
245 ******************************************************************************/
246static void rd_parse_root(struct sp_res_desc *rd, const void *fdt, int root)
247{
248 int node;
249 char *str;
250
251 str = "attribute";
252 node = fdt_subnode_offset_namelen(fdt, root, str, strlen(str));
253 if (node < 0) {
254 ERROR("Root node doesn't contain subnode '%s'\n", str);
255 panic();
256 } else {
257 rd_parse_attribute(&rd->attribute, fdt, node);
258 }
259
260 str = "memory_regions";
261 node = fdt_subnode_offset_namelen(fdt, root, str, strlen(str));
262 if (node < 0) {
263 ERROR("Root node doesn't contain subnode '%s'\n", str);
264 panic();
265 } else {
266 rd_parse_memory_regions(rd, fdt, node);
267 }
268
269 str = "notifications";
270 node = fdt_subnode_offset_namelen(fdt, root, str, strlen(str));
271 if (node < 0) {
272 WARN("Root node doesn't contain subnode '%s'\n", str);
273 } else {
274 rd_parse_notifications(rd, fdt, node);
275 }
276
277 str = "services";
278 node = fdt_subnode_offset_namelen(fdt, root, str, strlen(str));
279 if (node < 0) {
280 WARN("Root node doesn't contain subnode '%s'\n", str);
281 } else {
282 rd_parse_services(rd, fdt, node);
283 }
284}
285
286/*******************************************************************************
287 * Platform handler to load resource descriptor blobs into the active Secure
288 * Partition context.
289 ******************************************************************************/
290int plat_spm_sp_rd_load(struct sp_res_desc *rd, const void *ptr, size_t size)
291{
292 int rc;
293 int root_node;
294
295 assert(rd != NULL);
296 assert(ptr != NULL);
297
298 INFO("Reading RD blob at address %p\n", ptr);
299
300 rc = fdt_check_header(ptr);
301 if (rc != 0) {
302 ERROR("Wrong format for resource descriptor blob (%d).\n", rc);
303 return -1;
304 }
305
306 root_node = fdt_node_offset_by_compatible(ptr, -1, "arm,sp_rd");
307 if (root_node < 0) {
308 ERROR("Unrecognized resource descriptor blob (%d)\n", rc);
309 return -1;
310 }
311
312 rd_parse_root(rd, ptr, root_node);
313
314 return 0;
315}