blob: f39e33b6b57fa3eadcb00624d1b7f4288b0ca9ef [file] [log] [blame]
Soby Mathewec8ac1c2016-05-05 14:32:05 +01001/*
Paul Beesley1fbc97b2019-01-11 18:26:51 +00002 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
Soby Mathewec8ac1c2016-05-05 14:32:05 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewec8ac1c2016-05-05 14:32:05 +01005 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8#include <stddef.h>
9#include <stdint.h>
10#include <string.h>
11
12#include <platform_def.h>
13
Soby Mathewec8ac1c2016-05-05 14:32:05 +010014#include <arch.h>
15#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <common/bl_common.h>
17#include <common/debug.h>
18#include <common/runtime_svc.h>
Soby Mathewec8ac1c2016-05-05 14:32:05 +010019#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <drivers/console.h>
21#include <lib/el3_runtime/context_mgmt.h>
22#include <lib/psci/psci.h>
23#include <lib/utils.h>
24#include <plat/common/platform.h>
Soby Mathewec8ac1c2016-05-05 14:32:05 +010025#include <platform_sp_min.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <services/std_svc.h>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000027#include <smccc_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000028
Soby Mathewec8ac1c2016-05-05 14:32:05 +010029#include "sp_min_private.h"
30
31/* Pointers to per-core cpu contexts */
32static void *sp_min_cpu_ctx_ptr[PLATFORM_CORE_COUNT];
33
34/* SP_MIN only stores the non secure smc context */
35static smc_ctx_t sp_min_smc_context[PLATFORM_CORE_COUNT];
36
37/******************************************************************************
Paul Beesley1fbc97b2019-01-11 18:26:51 +000038 * Define the smccc helper library APIs
Soby Mathewec8ac1c2016-05-05 14:32:05 +010039 *****************************************************************************/
Etienne Carrierebfe12d32017-06-07 16:45:42 +020040void *smc_get_ctx(unsigned int security_state)
Soby Mathewec8ac1c2016-05-05 14:32:05 +010041{
42 assert(security_state == NON_SECURE);
43 return &sp_min_smc_context[plat_my_core_pos()];
44}
45
Etienne Carrierebfe12d32017-06-07 16:45:42 +020046void smc_set_next_ctx(unsigned int security_state)
Soby Mathewec8ac1c2016-05-05 14:32:05 +010047{
48 assert(security_state == NON_SECURE);
49 /* SP_MIN stores only non secure smc context. Nothing to do here */
50}
51
52void *smc_get_next_ctx(void)
53{
54 return &sp_min_smc_context[plat_my_core_pos()];
55}
56
57/*******************************************************************************
58 * This function returns a pointer to the most recent 'cpu_context' structure
59 * for the calling CPU that was set as the context for the specified security
60 * state. NULL is returned if no such structure has been specified.
61 ******************************************************************************/
62void *cm_get_context(uint32_t security_state)
63{
64 assert(security_state == NON_SECURE);
65 return sp_min_cpu_ctx_ptr[plat_my_core_pos()];
66}
67
68/*******************************************************************************
69 * This function sets the pointer to the current 'cpu_context' structure for the
70 * specified security state for the calling CPU
71 ******************************************************************************/
72void cm_set_context(void *context, uint32_t security_state)
73{
74 assert(security_state == NON_SECURE);
75 sp_min_cpu_ctx_ptr[plat_my_core_pos()] = context;
76}
77
78/*******************************************************************************
79 * This function returns a pointer to the most recent 'cpu_context' structure
80 * for the CPU identified by `cpu_idx` that was set as the context for the
81 * specified security state. NULL is returned if no such structure has been
82 * specified.
83 ******************************************************************************/
84void *cm_get_context_by_index(unsigned int cpu_idx,
85 unsigned int security_state)
86{
87 assert(security_state == NON_SECURE);
88 return sp_min_cpu_ctx_ptr[cpu_idx];
89}
90
91/*******************************************************************************
92 * This function sets the pointer to the current 'cpu_context' structure for the
93 * specified security state for the CPU identified by CPU index.
94 ******************************************************************************/
95void cm_set_context_by_index(unsigned int cpu_idx, void *context,
96 unsigned int security_state)
97{
98 assert(security_state == NON_SECURE);
99 sp_min_cpu_ctx_ptr[cpu_idx] = context;
100}
101
102static void copy_cpu_ctx_to_smc_stx(const regs_t *cpu_reg_ctx,
103 smc_ctx_t *next_smc_ctx)
104{
105 next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
Manish Pandey37c4ec22018-11-02 13:28:25 +0000106 next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
107 next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100108 next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
109 next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
Soby Mathewf3e3a432017-03-30 14:42:54 +0100110 next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100111}
112
113/*******************************************************************************
114 * This function invokes the PSCI library interface to initialize the
115 * non secure cpu context and copies the relevant cpu context register values
116 * to smc context. These registers will get programmed during `smc_exit`.
117 ******************************************************************************/
118static void sp_min_prepare_next_image_entry(void)
119{
120 entry_point_info_t *next_image_info;
Soby Mathewf3e3a432017-03-30 14:42:54 +0100121 cpu_context_t *ctx = cm_get_context(NON_SECURE);
122 u_register_t ns_sctlr;
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100123
124 /* Program system registers to proceed to non-secure */
125 next_image_info = sp_min_plat_get_bl33_ep_info();
126 assert(next_image_info);
127 assert(NON_SECURE == GET_SECURITY_STATE(next_image_info->h.attr));
128
129 INFO("SP_MIN: Preparing exit to normal world\n");
130
131 psci_prepare_next_non_secure_ctx(next_image_info);
132 smc_set_next_ctx(NON_SECURE);
133
134 /* Copy r0, lr and spsr from cpu context to SMC context */
135 copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
136 smc_get_next_ctx());
Soby Mathewf3e3a432017-03-30 14:42:54 +0100137
138 /* Temporarily set the NS bit to access NS SCTLR */
139 write_scr(read_scr() | SCR_NS_BIT);
140 isb();
141 ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
142 write_sctlr(ns_sctlr);
143 isb();
144
145 write_scr(read_scr() & ~SCR_NS_BIT);
146 isb();
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100147}
148
149/******************************************************************************
Soby Mathew8da89662016-09-19 17:21:15 +0100150 * Implement the ARM Standard Service function to get arguments for a
151 * particular service.
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100152 *****************************************************************************/
Soby Mathew8da89662016-09-19 17:21:15 +0100153uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100154{
Soby Mathew89256b82016-09-13 14:19:08 +0100155 /* Setup the arguments for PSCI Library */
156 DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, sp_min_warm_entrypoint);
157
Soby Mathew8da89662016-09-19 17:21:15 +0100158 /* PSCI is the only ARM Standard Service implemented */
159 assert(svc_mask == PSCI_FID_MASK);
160
161 return (uintptr_t)&psci_args;
162}
163
164/******************************************************************************
165 * The SP_MIN main function. Do the platform and PSCI Library setup. Also
166 * initialize the runtime service framework.
167 *****************************************************************************/
168void sp_min_main(void)
169{
Soby Mathew89256b82016-09-13 14:19:08 +0100170 NOTICE("SP_MIN: %s\n", version_string);
171 NOTICE("SP_MIN: %s\n", build_message);
172
173 /* Perform the SP_MIN platform setup */
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100174 sp_min_platform_setup();
175
Soby Mathew8da89662016-09-19 17:21:15 +0100176 /* Initialize the runtime services e.g. psci */
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100177 INFO("SP_MIN: Initializing runtime services\n");
178 runtime_svc_init();
179
180 /*
181 * We are ready to enter the next EL. Prepare entry into the image
182 * corresponding to the desired security state after the next ERET.
183 */
184 sp_min_prepare_next_image_entry();
Dimitris Papastamos52323b02017-06-07 13:45:41 +0100185
186 /*
187 * Perform any platform specific runtime setup prior to cold boot exit
188 * from SP_MIN.
189 */
190 sp_min_plat_runtime_setup();
Dimitris Papastamos9fb79122017-06-07 12:22:01 +0100191
192 console_flush();
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100193}
194
195/******************************************************************************
196 * This function is invoked during warm boot. Invoke the PSCI library
197 * warm boot entry point which takes care of Architectural and platform setup/
198 * restore. Copy the relevant cpu_context register values to smc context which
199 * will get programmed during `smc_exit`.
200 *****************************************************************************/
201void sp_min_warm_boot(void)
202{
203 smc_ctx_t *next_smc_ctx;
David Cunadoa31bcde2017-09-04 16:41:37 +0100204 cpu_context_t *ctx = cm_get_context(NON_SECURE);
205 u_register_t ns_sctlr;
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100206
207 psci_warmboot_entrypoint();
208
209 smc_set_next_ctx(NON_SECURE);
210
211 next_smc_ctx = smc_get_next_ctx();
Douglas Raillarda8954fc2017-01-26 15:54:44 +0000212 zeromem(next_smc_ctx, sizeof(smc_ctx_t));
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100213
214 copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
215 next_smc_ctx);
David Cunadoa31bcde2017-09-04 16:41:37 +0100216
217 /* Temporarily set the NS bit to access NS SCTLR */
218 write_scr(read_scr() | SCR_NS_BIT);
219 isb();
220 ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
221 write_sctlr(ns_sctlr);
222 isb();
223
224 write_scr(read_scr() & ~SCR_NS_BIT);
225 isb();
Soby Mathewec8ac1c2016-05-05 14:32:05 +0100226}
Etienne Carrieredc0fea72017-08-09 15:48:53 +0200227
228#if SP_MIN_WITH_SECURE_FIQ
229/******************************************************************************
230 * This function is invoked on secure interrupts. By construction of the
231 * SP_MIN, secure interrupts can only be handled when core executes in non
232 * secure state.
233 *****************************************************************************/
234void sp_min_fiq(void)
235{
236 uint32_t id;
237
238 id = plat_ic_acknowledge_interrupt();
239 sp_min_plat_fiq_handler(id);
240 plat_ic_end_of_interrupt(id);
241}
242#endif /* SP_MIN_WITH_SECURE_FIQ */