460d1fb31ca21e7795e61f61a2dfbd0f1b3cc32b
[project/bcm63xx/atf.git] / services / std_svc / spm / spm_main.c
1 /*
2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <arch_helpers.h>
8 #include <assert.h>
9 #include <bl31.h>
10 #include <context_mgmt.h>
11 #include <debug.h>
12 #include <ehf.h>
13 #include <errno.h>
14 #include <interrupt_mgmt.h>
15 #include <platform.h>
16 #include <runtime_svc.h>
17 #include <smccc.h>
18 #include <smccc_helpers.h>
19 #include <spinlock.h>
20 #include <string.h>
21 #include <sprt_svc.h>
22 #include <utils.h>
23 #include <xlat_tables_v2.h>
24
25 #include "spm_private.h"
26
27 /*******************************************************************************
28 * Secure Partition context information.
29 ******************************************************************************/
30 sp_context_t sp_ctx_array[PLAT_SPM_MAX_PARTITIONS];
31
32 /* Last Secure Partition last used by the CPU */
33 sp_context_t *cpu_sp_ctx[PLATFORM_CORE_COUNT];
34
35 void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx)
36 {
37 assert(linear_id < PLATFORM_CORE_COUNT);
38
39 cpu_sp_ctx[linear_id] = sp_ctx;
40 }
41
42 sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id)
43 {
44 assert(linear_id < PLATFORM_CORE_COUNT);
45
46 return cpu_sp_ctx[linear_id];
47 }
48
49 /*******************************************************************************
50 * Functions to keep track of how many requests a Secure Partition has received
51 * and hasn't finished.
52 ******************************************************************************/
53 void spm_sp_request_increase(sp_context_t *sp_ctx)
54 {
55 spin_lock(&(sp_ctx->request_count_lock));
56 sp_ctx->request_count++;
57 spin_unlock(&(sp_ctx->request_count_lock));
58 }
59
60 void spm_sp_request_decrease(sp_context_t *sp_ctx)
61 {
62 spin_lock(&(sp_ctx->request_count_lock));
63 sp_ctx->request_count--;
64 spin_unlock(&(sp_ctx->request_count_lock));
65 }
66
67 /* Returns 0 if it was originally 0, -1 otherwise. */
68 int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx)
69 {
70 int ret = -1;
71
72 spin_lock(&(sp_ctx->request_count_lock));
73 if (sp_ctx->request_count == 0U) {
74 sp_ctx->request_count++;
75 ret = 0U;
76 }
77 spin_unlock(&(sp_ctx->request_count_lock));
78
79 return ret;
80 }
81
82 /*******************************************************************************
83 * This function returns a pointer to the context of the Secure Partition that
84 * handles the service specified by an UUID. It returns NULL if the UUID wasn't
85 * found.
86 ******************************************************************************/
87 sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4])
88 {
89 unsigned int i;
90
91 for (i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
92
93 sp_context_t *sp_ctx = &sp_ctx_array[i];
94
95 if (sp_ctx->is_present == 0) {
96 continue;
97 }
98
99 struct sp_rd_sect_service *rdsvc;
100
101 for (rdsvc = sp_ctx->rd.service; rdsvc != NULL;
102 rdsvc = rdsvc->next) {
103 uint32_t *rd_uuid = (uint32_t *)(rdsvc->uuid);
104
105 if (memcmp(rd_uuid, svc_uuid, sizeof(rd_uuid)) == 0) {
106 return sp_ctx;
107 }
108 }
109 }
110
111 return NULL;
112 }
113
114 /*******************************************************************************
115 * Set state of a Secure Partition context.
116 ******************************************************************************/
117 void sp_state_set(sp_context_t *sp_ptr, sp_state_t state)
118 {
119 spin_lock(&(sp_ptr->state_lock));
120 sp_ptr->state = state;
121 spin_unlock(&(sp_ptr->state_lock));
122 }
123
124 /*******************************************************************************
125 * Wait until the state of a Secure Partition is the specified one and change it
126 * to the desired state.
127 ******************************************************************************/
128 void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
129 {
130 int success = 0;
131
132 while (success == 0) {
133 spin_lock(&(sp_ptr->state_lock));
134
135 if (sp_ptr->state == from) {
136 sp_ptr->state = to;
137
138 success = 1;
139 }
140
141 spin_unlock(&(sp_ptr->state_lock));
142 }
143 }
144
145 /*******************************************************************************
146 * Check if the state of a Secure Partition is the specified one and, if so,
147 * change it to the desired state. Returns 0 on success, -1 on error.
148 ******************************************************************************/
149 int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
150 {
151 int ret = -1;
152
153 spin_lock(&(sp_ptr->state_lock));
154
155 if (sp_ptr->state == from) {
156 sp_ptr->state = to;
157
158 ret = 0;
159 }
160
161 spin_unlock(&(sp_ptr->state_lock));
162
163 return ret;
164 }
165
166 /*******************************************************************************
167 * This function takes an SP context pointer and performs a synchronous entry
168 * into it.
169 ******************************************************************************/
170 uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt)
171 {
172 uint64_t rc;
173 unsigned int linear_id = plat_my_core_pos();
174
175 assert(sp_ctx != NULL);
176
177 /* Assign the context of the SP to this CPU */
178 spm_cpu_set_sp_ctx(linear_id, sp_ctx);
179 cm_set_context(&(sp_ctx->cpu_ctx), SECURE);
180
181 /* Restore the context assigned above */
182 cm_el1_sysregs_context_restore(SECURE);
183 cm_set_next_eret_context(SECURE);
184
185 /* Invalidate TLBs at EL1. */
186 tlbivmalle1();
187 dsbish();
188
189 if (can_preempt == 1) {
190 enable_intr_rm_local(INTR_TYPE_NS, SECURE);
191 } else {
192 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
193 }
194
195 /* Enter Secure Partition */
196 rc = spm_secure_partition_enter(&sp_ctx->c_rt_ctx);
197
198 /* Save secure state */
199 cm_el1_sysregs_context_save(SECURE);
200
201 return rc;
202 }
203
204 /*******************************************************************************
205 * This function returns to the place where spm_sp_synchronous_entry() was
206 * called originally.
207 ******************************************************************************/
208 __dead2 void spm_sp_synchronous_exit(uint64_t rc)
209 {
210 /* Get context of the SP in use by this CPU. */
211 unsigned int linear_id = plat_my_core_pos();
212 sp_context_t *ctx = spm_cpu_get_sp_ctx(linear_id);
213
214 /*
215 * The SPM must have initiated the original request through a
216 * synchronous entry into the secure partition. Jump back to the
217 * original C runtime context with the value of rc in x0;
218 */
219 spm_secure_partition_exit(ctx->c_rt_ctx, rc);
220
221 panic();
222 }
223
224 /*******************************************************************************
225 * This function is the handler registered for Non secure interrupts by the SPM.
226 * It validates the interrupt and upon success arranges entry into the normal
227 * world for handling the interrupt.
228 ******************************************************************************/
229 static uint64_t spm_ns_interrupt_handler(uint32_t id, uint32_t flags,
230 void *handle, void *cookie)
231 {
232 /* Check the security state when the exception was generated */
233 assert(get_interrupt_src_ss(flags) == SECURE);
234
235 spm_sp_synchronous_exit(SPM_SECURE_PARTITION_PREEMPTED);
236 }
237
238 /*******************************************************************************
239 * Jump to each Secure Partition for the first time.
240 ******************************************************************************/
241 static int32_t spm_init(void)
242 {
243 uint64_t rc = 0;
244 sp_context_t *ctx;
245
246 for (unsigned int i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
247
248 ctx = &sp_ctx_array[i];
249
250 if (ctx->is_present == 0) {
251 continue;
252 }
253
254 INFO("Secure Partition %u init...\n", i);
255
256 ctx->state = SP_STATE_RESET;
257
258 rc = spm_sp_synchronous_entry(ctx, 0);
259 if (rc != SPRT_YIELD_AARCH64) {
260 ERROR("Unexpected return value 0x%llx\n", rc);
261 panic();
262 }
263
264 ctx->state = SP_STATE_IDLE;
265
266 INFO("Secure Partition %u initialized.\n", i);
267 }
268
269 return rc;
270 }
271
272 /*******************************************************************************
273 * Initialize contexts of all Secure Partitions.
274 ******************************************************************************/
275 int32_t spm_setup(void)
276 {
277 int rc;
278 sp_context_t *ctx;
279 void *sp_base, *rd_base;
280 size_t sp_size, rd_size;
281 uint64_t flags = 0U;
282
283 /* Disable MMU at EL1 (initialized by BL2) */
284 disable_mmu_icache_el1();
285
286 /*
287 * Non-blocking services can be interrupted by Non-secure interrupts.
288 * Register an interrupt handler for NS interrupts when generated while
289 * the CPU is in secure state. They are routed to EL3.
290 */
291 set_interrupt_rm_flag(flags, SECURE);
292
293 uint64_t rc_int = register_interrupt_type_handler(INTR_TYPE_NS,
294 spm_ns_interrupt_handler, flags);
295 if (rc_int) {
296 ERROR("SPM: Failed to register NS interrupt handler with rc = %llx\n",
297 rc_int);
298 panic();
299 }
300
301 /*
302 * Setup all Secure Partitions.
303 */
304 unsigned int i = 0U;
305
306 while (1) {
307 rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
308 &rd_base, &rd_size);
309 if (rc < 0) {
310 /* Reached the end of the package. */
311 break;
312 }
313
314 if (i >= PLAT_SPM_MAX_PARTITIONS) {
315 ERROR("Too many partitions in the package.\n");
316 panic();
317 }
318
319 ctx = &sp_ctx_array[i];
320
321 assert(ctx->is_present == 0);
322
323 /* Initialize context of the SP */
324 INFO("Secure Partition %u context setup start...\n", i);
325
326 /* Assign translation tables context. */
327 ctx->xlat_ctx_handle = spm_sp_xlat_context_alloc();
328
329 /* Save location of the image in physical memory */
330 ctx->image_base = (uintptr_t)sp_base;
331 ctx->image_size = sp_size;
332
333 rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
334 if (rc < 0) {
335 ERROR("Error while loading RD blob.\n");
336 panic();
337 }
338
339 spm_sp_setup(ctx);
340
341 ctx->is_present = 1;
342
343 INFO("Secure Partition %u setup done.\n", i);
344
345 i++;
346 }
347
348 if (i == 0U) {
349 ERROR("No present partitions in the package.\n");
350 panic();
351 }
352
353 /* Register init function for deferred init. */
354 bl31_register_bl32_init(&spm_init);
355
356 return 0;
357 }