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