blob: 1c7e9600a7bb9aeaebd880f1f52eebc3d6da3f71 [file] [log] [blame]
Soby Mathewc11ba852016-05-05 14:32:05 +01001/*
Yann Gautiera1255c72024-01-18 18:20:43 +01002 * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
Soby Mathewc11ba852016-05-05 14:32:05 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewc11ba852016-05-05 14:32:05 +01005 */
6
Soby Mathewc11ba852016-05-05 14:32:05 +01007#include <assert.h>
Soby Mathewc11ba852016-05-05 14:32:05 +01008#include <stddef.h>
9#include <stdint.h>
10#include <string.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011
12#include <platform_def.h>
13
14#include <arch.h>
15#include <arch_helpers.h>
16#include <common/bl_common.h>
17#include <common/debug.h>
18#include <common/runtime_svc.h>
19#include <context.h>
20#include <drivers/console.h>
21#include <lib/el3_runtime/context_mgmt.h>
Bence Szépkúti0531ada2019-11-07 12:09:24 +010022#include <lib/pmf/pmf.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000023#include <lib/psci/psci.h>
Bence Szépkúti0531ada2019-11-07 12:09:24 +010024#include <lib/runtime_instr.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000025#include <lib/utils.h>
26#include <plat/common/platform.h>
27#include <platform_sp_min.h>
28#include <services/std_svc.h>
29#include <smccc_helpers.h>
30
Soby Mathewc11ba852016-05-05 14:32:05 +010031#include "sp_min_private.h"
32
Bence Szépkúti0531ada2019-11-07 12:09:24 +010033#if ENABLE_RUNTIME_INSTRUMENTATION
34PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
35 RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
36#endif
37
Soby Mathewc11ba852016-05-05 14:32:05 +010038/* Pointers to per-core cpu contexts */
39static void *sp_min_cpu_ctx_ptr[PLATFORM_CORE_COUNT];
40
41/* SP_MIN only stores the non secure smc context */
42static smc_ctx_t sp_min_smc_context[PLATFORM_CORE_COUNT];
43
44/******************************************************************************
Paul Beesley8aabea32019-01-11 18:26:51 +000045 * Define the smccc helper library APIs
Soby Mathewc11ba852016-05-05 14:32:05 +010046 *****************************************************************************/
Etienne Carriere55074082017-06-07 16:45:42 +020047void *smc_get_ctx(unsigned int security_state)
Soby Mathewc11ba852016-05-05 14:32:05 +010048{
49 assert(security_state == NON_SECURE);
50 return &sp_min_smc_context[plat_my_core_pos()];
51}
52
Etienne Carriere55074082017-06-07 16:45:42 +020053void smc_set_next_ctx(unsigned int security_state)
Soby Mathewc11ba852016-05-05 14:32:05 +010054{
55 assert(security_state == NON_SECURE);
56 /* SP_MIN stores only non secure smc context. Nothing to do here */
57}
58
59void *smc_get_next_ctx(void)
60{
61 return &sp_min_smc_context[plat_my_core_pos()];
62}
63
64/*******************************************************************************
65 * This function returns a pointer to the most recent 'cpu_context' structure
66 * for the calling CPU that was set as the context for the specified security
67 * state. NULL is returned if no such structure has been specified.
68 ******************************************************************************/
69void *cm_get_context(uint32_t security_state)
70{
71 assert(security_state == NON_SECURE);
72 return sp_min_cpu_ctx_ptr[plat_my_core_pos()];
73}
74
75/*******************************************************************************
76 * This function sets the pointer to the current 'cpu_context' structure for the
77 * specified security state for the calling CPU
78 ******************************************************************************/
79void cm_set_context(void *context, uint32_t security_state)
80{
81 assert(security_state == NON_SECURE);
82 sp_min_cpu_ctx_ptr[plat_my_core_pos()] = context;
83}
84
85/*******************************************************************************
86 * This function returns a pointer to the most recent 'cpu_context' structure
87 * for the CPU identified by `cpu_idx` that was set as the context for the
88 * specified security state. NULL is returned if no such structure has been
89 * specified.
90 ******************************************************************************/
91void *cm_get_context_by_index(unsigned int cpu_idx,
92 unsigned int security_state)
93{
94 assert(security_state == NON_SECURE);
95 return sp_min_cpu_ctx_ptr[cpu_idx];
96}
97
98/*******************************************************************************
99 * This function sets the pointer to the current 'cpu_context' structure for the
100 * specified security state for the CPU identified by CPU index.
101 ******************************************************************************/
102void cm_set_context_by_index(unsigned int cpu_idx, void *context,
103 unsigned int security_state)
104{
105 assert(security_state == NON_SECURE);
106 sp_min_cpu_ctx_ptr[cpu_idx] = context;
107}
108
109static void copy_cpu_ctx_to_smc_stx(const regs_t *cpu_reg_ctx,
110 smc_ctx_t *next_smc_ctx)
111{
112 next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
Manish Pandeyed2c4f42018-11-02 13:28:25 +0000113 next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
114 next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
Soby Mathewc11ba852016-05-05 14:32:05 +0100115 next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
116 next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
Soby Mathewb6285d62017-03-30 14:42:54 +0100117 next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
Soby Mathewc11ba852016-05-05 14:32:05 +0100118}
119
120/*******************************************************************************
121 * This function invokes the PSCI library interface to initialize the
122 * non secure cpu context and copies the relevant cpu context register values
123 * to smc context. These registers will get programmed during `smc_exit`.
124 ******************************************************************************/
125static void sp_min_prepare_next_image_entry(void)
126{
127 entry_point_info_t *next_image_info;
Soby Mathewb6285d62017-03-30 14:42:54 +0100128 cpu_context_t *ctx = cm_get_context(NON_SECURE);
129 u_register_t ns_sctlr;
Soby Mathewc11ba852016-05-05 14:32:05 +0100130
131 /* Program system registers to proceed to non-secure */
132 next_image_info = sp_min_plat_get_bl33_ep_info();
133 assert(next_image_info);
134 assert(NON_SECURE == GET_SECURITY_STATE(next_image_info->h.attr));
135
136 INFO("SP_MIN: Preparing exit to normal world\n");
Stephan Gerhold94e1be22023-04-02 16:05:58 +0200137 print_entry_point_info(next_image_info);
Soby Mathewc11ba852016-05-05 14:32:05 +0100138
139 psci_prepare_next_non_secure_ctx(next_image_info);
140 smc_set_next_ctx(NON_SECURE);
141
142 /* Copy r0, lr and spsr from cpu context to SMC context */
143 copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
144 smc_get_next_ctx());
Soby Mathewb6285d62017-03-30 14:42:54 +0100145
146 /* Temporarily set the NS bit to access NS SCTLR */
147 write_scr(read_scr() | SCR_NS_BIT);
148 isb();
149 ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
150 write_sctlr(ns_sctlr);
151 isb();
152
153 write_scr(read_scr() & ~SCR_NS_BIT);
154 isb();
Soby Mathewc11ba852016-05-05 14:32:05 +0100155}
156
157/******************************************************************************
Soby Mathew58e946a2016-09-19 17:21:15 +0100158 * Implement the ARM Standard Service function to get arguments for a
159 * particular service.
160 *****************************************************************************/
161uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
162{
163 /* Setup the arguments for PSCI Library */
164 DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, sp_min_warm_entrypoint);
165
166 /* PSCI is the only ARM Standard Service implemented */
167 assert(svc_mask == PSCI_FID_MASK);
168
169 return (uintptr_t)&psci_args;
170}
171
172/******************************************************************************
Yann Gautiera1255c72024-01-18 18:20:43 +0100173 * The SP_MIN setup function. Calls platforms init functions
174 *****************************************************************************/
175void sp_min_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
176 u_register_t arg3)
177{
178 /* Perform early platform-specific setup */
179 sp_min_early_platform_setup2(arg0, arg1, arg2, arg3);
180 sp_min_plat_arch_setup();
181}
182
183/******************************************************************************
Soby Mathewc11ba852016-05-05 14:32:05 +0100184 * The SP_MIN main function. Do the platform and PSCI Library setup. Also
185 * initialize the runtime service framework.
186 *****************************************************************************/
187void sp_min_main(void)
188{
Soby Mathewf426fc02016-09-13 14:19:08 +0100189 NOTICE("SP_MIN: %s\n", version_string);
190 NOTICE("SP_MIN: %s\n", build_message);
191
192 /* Perform the SP_MIN platform setup */
Soby Mathewc11ba852016-05-05 14:32:05 +0100193 sp_min_platform_setup();
194
Soby Mathew58e946a2016-09-19 17:21:15 +0100195 /* Initialize the runtime services e.g. psci */
Soby Mathewc11ba852016-05-05 14:32:05 +0100196 INFO("SP_MIN: Initializing runtime services\n");
197 runtime_svc_init();
198
199 /*
200 * We are ready to enter the next EL. Prepare entry into the image
201 * corresponding to the desired security state after the next ERET.
202 */
203 sp_min_prepare_next_image_entry();
Dimitris Papastamos21568302017-06-07 13:45:41 +0100204
205 /*
206 * Perform any platform specific runtime setup prior to cold boot exit
207 * from SP_MIN.
208 */
209 sp_min_plat_runtime_setup();
Dimitris Papastamos10d664c2017-06-07 12:22:01 +0100210
211 console_flush();
Soby Mathewc11ba852016-05-05 14:32:05 +0100212}
213
214/******************************************************************************
215 * This function is invoked during warm boot. Invoke the PSCI library
216 * warm boot entry point which takes care of Architectural and platform setup/
217 * restore. Copy the relevant cpu_context register values to smc context which
218 * will get programmed during `smc_exit`.
219 *****************************************************************************/
220void sp_min_warm_boot(void)
221{
222 smc_ctx_t *next_smc_ctx;
David Cunado88ad1462017-09-04 16:41:37 +0100223 cpu_context_t *ctx = cm_get_context(NON_SECURE);
224 u_register_t ns_sctlr;
Soby Mathewc11ba852016-05-05 14:32:05 +0100225
226 psci_warmboot_entrypoint();
227
228 smc_set_next_ctx(NON_SECURE);
229
230 next_smc_ctx = smc_get_next_ctx();
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000231 zeromem(next_smc_ctx, sizeof(smc_ctx_t));
Soby Mathewc11ba852016-05-05 14:32:05 +0100232
233 copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
234 next_smc_ctx);
David Cunado88ad1462017-09-04 16:41:37 +0100235
236 /* Temporarily set the NS bit to access NS SCTLR */
237 write_scr(read_scr() | SCR_NS_BIT);
238 isb();
239 ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
240 write_sctlr(ns_sctlr);
241 isb();
242
243 write_scr(read_scr() & ~SCR_NS_BIT);
244 isb();
Soby Mathewc11ba852016-05-05 14:32:05 +0100245}
Etienne Carriere71816092017-08-09 15:48:53 +0200246
247#if SP_MIN_WITH_SECURE_FIQ
248/******************************************************************************
249 * This function is invoked on secure interrupts. By construction of the
250 * SP_MIN, secure interrupts can only be handled when core executes in non
251 * secure state.
252 *****************************************************************************/
253void sp_min_fiq(void)
254{
255 uint32_t id;
256
257 id = plat_ic_acknowledge_interrupt();
258 sp_min_plat_fiq_handler(id);
259 plat_ic_end_of_interrupt(id);
260}
261#endif /* SP_MIN_WITH_SECURE_FIQ */