1c11ba852SSoby Mathew /*
23fabca72SHarrison Mutai * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved.
3c11ba852SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5c11ba852SSoby Mathew */
6c11ba852SSoby Mathew
7c11ba852SSoby Mathew #include <assert.h>
8c11ba852SSoby Mathew #include <stddef.h>
9c11ba852SSoby Mathew #include <stdint.h>
10c11ba852SSoby Mathew #include <string.h>
1109d40e0eSAntonio Nino Diaz
1209d40e0eSAntonio Nino Diaz #include <platform_def.h>
1309d40e0eSAntonio Nino Diaz
1409d40e0eSAntonio Nino Diaz #include <arch.h>
1509d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1609d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
17758ccb80SChris Kay #include <common/build_message.h>
1809d40e0eSAntonio Nino Diaz #include <common/debug.h>
1909d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
2009d40e0eSAntonio Nino Diaz #include <context.h>
2109d40e0eSAntonio Nino Diaz #include <drivers/console.h>
2209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
230531ada5SBence Szépkúti #include <lib/pmf/pmf.h>
2409d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
250531ada5SBence Szépkúti #include <lib/runtime_instr.h>
2609d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2809d40e0eSAntonio Nino Diaz #include <platform_sp_min.h>
2909d40e0eSAntonio Nino Diaz #include <services/std_svc.h>
3009d40e0eSAntonio Nino Diaz #include <smccc_helpers.h>
3109d40e0eSAntonio Nino Diaz
32c11ba852SSoby Mathew #include "sp_min_private.h"
33c11ba852SSoby Mathew
340531ada5SBence Szépkúti #if ENABLE_RUNTIME_INSTRUMENTATION
PMF_REGISTER_SERVICE_SMC(rt_instr_svc,PMF_RT_INSTR_SVC_ID,RT_INSTR_TOTAL_IDS,PMF_STORE_ENABLE)350531ada5SBence Szépkúti PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
360531ada5SBence Szépkúti RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
370531ada5SBence Szépkúti #endif
380531ada5SBence Szépkúti
39c11ba852SSoby Mathew /* Pointers to per-core cpu contexts */
40c11ba852SSoby Mathew static void *sp_min_cpu_ctx_ptr[PLATFORM_CORE_COUNT];
41c11ba852SSoby Mathew
42c11ba852SSoby Mathew /* SP_MIN only stores the non secure smc context */
43c11ba852SSoby Mathew static smc_ctx_t sp_min_smc_context[PLATFORM_CORE_COUNT];
44c11ba852SSoby Mathew
45c11ba852SSoby Mathew /******************************************************************************
468aabea33SPaul Beesley * Define the smccc helper library APIs
47c11ba852SSoby Mathew *****************************************************************************/
4855074083SEtienne Carriere void *smc_get_ctx(unsigned int security_state)
49c11ba852SSoby Mathew {
50c11ba852SSoby Mathew assert(security_state == NON_SECURE);
51c11ba852SSoby Mathew return &sp_min_smc_context[plat_my_core_pos()];
52c11ba852SSoby Mathew }
53c11ba852SSoby Mathew
smc_set_next_ctx(unsigned int security_state)5455074083SEtienne Carriere void smc_set_next_ctx(unsigned int security_state)
55c11ba852SSoby Mathew {
56c11ba852SSoby Mathew assert(security_state == NON_SECURE);
57c11ba852SSoby Mathew /* SP_MIN stores only non secure smc context. Nothing to do here */
58c11ba852SSoby Mathew }
59c11ba852SSoby Mathew
smc_get_next_ctx(void)60c11ba852SSoby Mathew void *smc_get_next_ctx(void)
61c11ba852SSoby Mathew {
62c11ba852SSoby Mathew return &sp_min_smc_context[plat_my_core_pos()];
63c11ba852SSoby Mathew }
64c11ba852SSoby Mathew
65c11ba852SSoby Mathew /*******************************************************************************
66c11ba852SSoby Mathew * This function returns a pointer to the most recent 'cpu_context' structure
67c11ba852SSoby Mathew * for the calling CPU that was set as the context for the specified security
68c11ba852SSoby Mathew * state. NULL is returned if no such structure has been specified.
69c11ba852SSoby Mathew ******************************************************************************/
cm_get_context(size_t security_state)70f05b4894SMaheedhar Bollapalli void *cm_get_context(size_t security_state)
71c11ba852SSoby Mathew {
72c11ba852SSoby Mathew assert(security_state == NON_SECURE);
73c11ba852SSoby Mathew return sp_min_cpu_ctx_ptr[plat_my_core_pos()];
74c11ba852SSoby Mathew }
75c11ba852SSoby Mathew
76c11ba852SSoby Mathew /*******************************************************************************
77c11ba852SSoby Mathew * This function sets the pointer to the current 'cpu_context' structure for the
78c11ba852SSoby Mathew * specified security state for the calling CPU
79c11ba852SSoby Mathew ******************************************************************************/
cm_set_context(void * context,uint32_t security_state)80c11ba852SSoby Mathew void cm_set_context(void *context, uint32_t security_state)
81c11ba852SSoby Mathew {
82c11ba852SSoby Mathew assert(security_state == NON_SECURE);
83c11ba852SSoby Mathew sp_min_cpu_ctx_ptr[plat_my_core_pos()] = context;
84c11ba852SSoby Mathew }
85c11ba852SSoby Mathew
86c11ba852SSoby Mathew /*******************************************************************************
87c11ba852SSoby Mathew * This function returns a pointer to the most recent 'cpu_context' structure
88c11ba852SSoby Mathew * for the CPU identified by `cpu_idx` that was set as the context for the
89c11ba852SSoby Mathew * specified security state. NULL is returned if no such structure has been
90c11ba852SSoby Mathew * specified.
91c11ba852SSoby Mathew ******************************************************************************/
cm_get_context_by_index(unsigned int cpu_idx,size_t security_state)92c11ba852SSoby Mathew void *cm_get_context_by_index(unsigned int cpu_idx,
93f05b4894SMaheedhar Bollapalli size_t security_state)
94c11ba852SSoby Mathew {
95c11ba852SSoby Mathew assert(security_state == NON_SECURE);
96c11ba852SSoby Mathew return sp_min_cpu_ctx_ptr[cpu_idx];
97c11ba852SSoby Mathew }
98c11ba852SSoby Mathew
99c11ba852SSoby Mathew /*******************************************************************************
100c11ba852SSoby Mathew * This function sets the pointer to the current 'cpu_context' structure for the
101c11ba852SSoby Mathew * specified security state for the CPU identified by CPU index.
102c11ba852SSoby Mathew ******************************************************************************/
cm_set_context_by_index(unsigned int cpu_idx,void * context,unsigned int security_state)103c11ba852SSoby Mathew void cm_set_context_by_index(unsigned int cpu_idx, void *context,
104c11ba852SSoby Mathew unsigned int security_state)
105c11ba852SSoby Mathew {
106c11ba852SSoby Mathew assert(security_state == NON_SECURE);
107c11ba852SSoby Mathew sp_min_cpu_ctx_ptr[cpu_idx] = context;
108c11ba852SSoby Mathew }
109c11ba852SSoby Mathew
copy_cpu_ctx_to_smc_stx(const regs_t * cpu_reg_ctx,smc_ctx_t * next_smc_ctx)110c11ba852SSoby Mathew static void copy_cpu_ctx_to_smc_stx(const regs_t *cpu_reg_ctx,
111c11ba852SSoby Mathew smc_ctx_t *next_smc_ctx)
112c11ba852SSoby Mathew {
113c11ba852SSoby Mathew next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
114ed2c4f4aSManish Pandey next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
115ed2c4f4aSManish Pandey next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
1163fabca72SHarrison Mutai next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3);
117c11ba852SSoby Mathew next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
118c11ba852SSoby Mathew next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
119b6285d64SSoby Mathew next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
120c11ba852SSoby Mathew }
121c11ba852SSoby Mathew
122c11ba852SSoby Mathew /*******************************************************************************
123c11ba852SSoby Mathew * This function invokes the PSCI library interface to initialize the
124c11ba852SSoby Mathew * non secure cpu context and copies the relevant cpu context register values
125c11ba852SSoby Mathew * to smc context. These registers will get programmed during `smc_exit`.
126c11ba852SSoby Mathew ******************************************************************************/
sp_min_prepare_next_image_entry(void)127c11ba852SSoby Mathew static void sp_min_prepare_next_image_entry(void)
128c11ba852SSoby Mathew {
129c11ba852SSoby Mathew entry_point_info_t *next_image_info;
130*e4d0622cSBoyan Karatotev regs_t *gpregs = get_regs_ctx(cm_get_context(NON_SECURE));
131b6285d64SSoby Mathew u_register_t ns_sctlr;
132c11ba852SSoby Mathew
133c11ba852SSoby Mathew /* Program system registers to proceed to non-secure */
134c11ba852SSoby Mathew next_image_info = sp_min_plat_get_bl33_ep_info();
135c11ba852SSoby Mathew assert(next_image_info);
136c11ba852SSoby Mathew assert(NON_SECURE == GET_SECURITY_STATE(next_image_info->h.attr));
137c11ba852SSoby Mathew
138c11ba852SSoby Mathew INFO("SP_MIN: Preparing exit to normal world\n");
13994e1be2bSStephan Gerhold print_entry_point_info(next_image_info);
140c11ba852SSoby Mathew
141c11ba852SSoby Mathew psci_prepare_next_non_secure_ctx(next_image_info);
142c11ba852SSoby Mathew smc_set_next_ctx(NON_SECURE);
143c11ba852SSoby Mathew
144c11ba852SSoby Mathew /* Copy r0, lr and spsr from cpu context to SMC context */
145*e4d0622cSBoyan Karatotev copy_cpu_ctx_to_smc_stx(gpregs,
146c11ba852SSoby Mathew smc_get_next_ctx());
147b6285d64SSoby Mathew
148b6285d64SSoby Mathew /* Temporarily set the NS bit to access NS SCTLR */
149b6285d64SSoby Mathew write_scr(read_scr() | SCR_NS_BIT);
150b6285d64SSoby Mathew isb();
151*e4d0622cSBoyan Karatotev ns_sctlr = read_ctx_reg(gpregs, CTX_NS_SCTLR);
152b6285d64SSoby Mathew write_sctlr(ns_sctlr);
153b6285d64SSoby Mathew isb();
154b6285d64SSoby Mathew
155b6285d64SSoby Mathew write_scr(read_scr() & ~SCR_NS_BIT);
156b6285d64SSoby Mathew isb();
157c11ba852SSoby Mathew }
158c11ba852SSoby Mathew
159c11ba852SSoby Mathew /******************************************************************************
16058e946aeSSoby Mathew * Implement the ARM Standard Service function to get arguments for a
16158e946aeSSoby Mathew * particular service.
16258e946aeSSoby Mathew *****************************************************************************/
get_arm_std_svc_args(unsigned int svc_mask)16358e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
16458e946aeSSoby Mathew {
16558e946aeSSoby Mathew /* Setup the arguments for PSCI Library */
16658e946aeSSoby Mathew DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, sp_min_warm_entrypoint);
16758e946aeSSoby Mathew
16858e946aeSSoby Mathew /* PSCI is the only ARM Standard Service implemented */
16958e946aeSSoby Mathew assert(svc_mask == PSCI_FID_MASK);
17058e946aeSSoby Mathew
17158e946aeSSoby Mathew return (uintptr_t)&psci_args;
17258e946aeSSoby Mathew }
17358e946aeSSoby Mathew
17458e946aeSSoby Mathew /******************************************************************************
175a1255c75SYann Gautier * The SP_MIN setup function. Calls platforms init functions
176a1255c75SYann Gautier *****************************************************************************/
sp_min_setup(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)177a1255c75SYann Gautier void sp_min_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
178a1255c75SYann Gautier u_register_t arg3)
179a1255c75SYann Gautier {
180ae770fedSYann Gautier /* Enable early console if EARLY_CONSOLE flag is enabled */
181ae770fedSYann Gautier plat_setup_early_console();
182ae770fedSYann Gautier
183a1255c75SYann Gautier /* Perform early platform-specific setup */
184a1255c75SYann Gautier sp_min_early_platform_setup2(arg0, arg1, arg2, arg3);
185a1255c75SYann Gautier sp_min_plat_arch_setup();
186a1255c75SYann Gautier }
187a1255c75SYann Gautier
188a1255c75SYann Gautier /******************************************************************************
189c11ba852SSoby Mathew * The SP_MIN main function. Do the platform and PSCI Library setup. Also
190c11ba852SSoby Mathew * initialize the runtime service framework.
191c11ba852SSoby Mathew *****************************************************************************/
sp_min_main(void)192c11ba852SSoby Mathew void sp_min_main(void)
193c11ba852SSoby Mathew {
194758ccb80SChris Kay NOTICE("SP_MIN: %s\n", build_version_string);
195f426fc05SSoby Mathew NOTICE("SP_MIN: %s\n", build_message);
196f426fc05SSoby Mathew
197f426fc05SSoby Mathew /* Perform the SP_MIN platform setup */
198c11ba852SSoby Mathew sp_min_platform_setup();
199c11ba852SSoby Mathew
20058e946aeSSoby Mathew /* Initialize the runtime services e.g. psci */
201c11ba852SSoby Mathew INFO("SP_MIN: Initializing runtime services\n");
202c11ba852SSoby Mathew runtime_svc_init();
203c11ba852SSoby Mathew
204c11ba852SSoby Mathew /*
205c11ba852SSoby Mathew * We are ready to enter the next EL. Prepare entry into the image
206c11ba852SSoby Mathew * corresponding to the desired security state after the next ERET.
207c11ba852SSoby Mathew */
208c11ba852SSoby Mathew sp_min_prepare_next_image_entry();
20921568304SDimitris Papastamos
21021568304SDimitris Papastamos /*
21121568304SDimitris Papastamos * Perform any platform specific runtime setup prior to cold boot exit
21221568304SDimitris Papastamos * from SP_MIN.
21321568304SDimitris Papastamos */
21421568304SDimitris Papastamos sp_min_plat_runtime_setup();
21510d664ceSDimitris Papastamos
21610d664ceSDimitris Papastamos console_flush();
2176d415de8SSalman Nabi console_switch_state(CONSOLE_FLAG_RUNTIME);
218c11ba852SSoby Mathew }
219c11ba852SSoby Mathew
220c11ba852SSoby Mathew /******************************************************************************
221c11ba852SSoby Mathew * This function is invoked during warm boot. Invoke the PSCI library
222c11ba852SSoby Mathew * warm boot entry point which takes care of Architectural and platform setup/
223c11ba852SSoby Mathew * restore. Copy the relevant cpu_context register values to smc context which
224c11ba852SSoby Mathew * will get programmed during `smc_exit`.
225c11ba852SSoby Mathew *****************************************************************************/
sp_min_warm_boot(void)226c11ba852SSoby Mathew void sp_min_warm_boot(void)
227c11ba852SSoby Mathew {
228c11ba852SSoby Mathew smc_ctx_t *next_smc_ctx;
229*e4d0622cSBoyan Karatotev regs_t *gpregs = get_regs_ctx(cm_get_context(NON_SECURE));
23088ad1461SDavid Cunado u_register_t ns_sctlr;
231c11ba852SSoby Mathew
23263900851SBoyan Karatotev psci_warmboot_entrypoint(plat_my_core_pos());
233c11ba852SSoby Mathew
234c11ba852SSoby Mathew smc_set_next_ctx(NON_SECURE);
235c11ba852SSoby Mathew
236c11ba852SSoby Mathew next_smc_ctx = smc_get_next_ctx();
23732f0d3c6SDouglas Raillard zeromem(next_smc_ctx, sizeof(smc_ctx_t));
238c11ba852SSoby Mathew
239*e4d0622cSBoyan Karatotev copy_cpu_ctx_to_smc_stx(gpregs,
240c11ba852SSoby Mathew next_smc_ctx);
24188ad1461SDavid Cunado
24288ad1461SDavid Cunado /* Temporarily set the NS bit to access NS SCTLR */
24388ad1461SDavid Cunado write_scr(read_scr() | SCR_NS_BIT);
24488ad1461SDavid Cunado isb();
245*e4d0622cSBoyan Karatotev ns_sctlr = read_ctx_reg(gpregs, CTX_NS_SCTLR);
24688ad1461SDavid Cunado write_sctlr(ns_sctlr);
24788ad1461SDavid Cunado isb();
24888ad1461SDavid Cunado
24988ad1461SDavid Cunado write_scr(read_scr() & ~SCR_NS_BIT);
25088ad1461SDavid Cunado isb();
251c11ba852SSoby Mathew }
25271816096SEtienne Carriere
25371816096SEtienne Carriere #if SP_MIN_WITH_SECURE_FIQ
25471816096SEtienne Carriere /******************************************************************************
25571816096SEtienne Carriere * This function is invoked on secure interrupts. By construction of the
25671816096SEtienne Carriere * SP_MIN, secure interrupts can only be handled when core executes in non
25771816096SEtienne Carriere * secure state.
25871816096SEtienne Carriere *****************************************************************************/
sp_min_fiq(void)25971816096SEtienne Carriere void sp_min_fiq(void)
26071816096SEtienne Carriere {
26171816096SEtienne Carriere uint32_t id;
26271816096SEtienne Carriere
26371816096SEtienne Carriere id = plat_ic_acknowledge_interrupt();
26471816096SEtienne Carriere sp_min_plat_fiq_handler(id);
26571816096SEtienne Carriere plat_ic_end_of_interrupt(id);
26671816096SEtienne Carriere }
26771816096SEtienne Carriere #endif /* SP_MIN_WITH_SECURE_FIQ */
268