xref: /rk3399_ARM-atf/bl32/sp_min/sp_min_main.c (revision 93c78ed231a6ea30f1c30ecbbb0f245fa9d75075)
1c11ba852SSoby Mathew /*
2085e80ecSAntonio Nino Diaz  * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
3c11ba852SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5c11ba852SSoby Mathew  */
6c11ba852SSoby Mathew 
7c11ba852SSoby Mathew #include <arch.h>
8c11ba852SSoby Mathew #include <arch_helpers.h>
9c11ba852SSoby Mathew #include <assert.h>
10c11ba852SSoby Mathew #include <bl_common.h>
1110d664ceSDimitris Papastamos #include <console.h>
12c11ba852SSoby Mathew #include <context.h>
13c11ba852SSoby Mathew #include <context_mgmt.h>
14c11ba852SSoby Mathew #include <debug.h>
15c11ba852SSoby Mathew #include <platform.h>
16c11ba852SSoby Mathew #include <platform_def.h>
17c11ba852SSoby Mathew #include <platform_sp_min.h>
18c11ba852SSoby Mathew #include <psci.h>
19c11ba852SSoby Mathew #include <runtime_svc.h>
20085e80ecSAntonio Nino Diaz #include <smccc_helpers.h>
21c11ba852SSoby Mathew #include <stddef.h>
22c11ba852SSoby Mathew #include <stdint.h>
23a9b5b4aeSRoberto Vargas #include <std_svc.h>
24*93c78ed2SAntonio Nino Diaz #include <stdint.h>
25c11ba852SSoby Mathew #include <string.h>
2632f0d3c6SDouglas Raillard #include <utils.h>
27c11ba852SSoby Mathew #include "sp_min_private.h"
28c11ba852SSoby Mathew 
29c11ba852SSoby Mathew /* Pointers to per-core cpu contexts */
30c11ba852SSoby Mathew static void *sp_min_cpu_ctx_ptr[PLATFORM_CORE_COUNT];
31c11ba852SSoby Mathew 
32c11ba852SSoby Mathew /* SP_MIN only stores the non secure smc context */
33c11ba852SSoby Mathew static smc_ctx_t sp_min_smc_context[PLATFORM_CORE_COUNT];
34c11ba852SSoby Mathew 
35c11ba852SSoby Mathew /******************************************************************************
36085e80ecSAntonio Nino Diaz  * Define the smccc helper library API's
37c11ba852SSoby Mathew  *****************************************************************************/
3855074083SEtienne Carriere void *smc_get_ctx(unsigned int security_state)
39c11ba852SSoby Mathew {
40c11ba852SSoby Mathew 	assert(security_state == NON_SECURE);
41c11ba852SSoby Mathew 	return &sp_min_smc_context[plat_my_core_pos()];
42c11ba852SSoby Mathew }
43c11ba852SSoby Mathew 
4455074083SEtienne Carriere void smc_set_next_ctx(unsigned int security_state)
45c11ba852SSoby Mathew {
46c11ba852SSoby Mathew 	assert(security_state == NON_SECURE);
47c11ba852SSoby Mathew 	/* SP_MIN stores only non secure smc context. Nothing to do here */
48c11ba852SSoby Mathew }
49c11ba852SSoby Mathew 
50c11ba852SSoby Mathew void *smc_get_next_ctx(void)
51c11ba852SSoby Mathew {
52c11ba852SSoby Mathew 	return &sp_min_smc_context[plat_my_core_pos()];
53c11ba852SSoby Mathew }
54c11ba852SSoby Mathew 
55c11ba852SSoby Mathew /*******************************************************************************
56c11ba852SSoby Mathew  * This function returns a pointer to the most recent 'cpu_context' structure
57c11ba852SSoby Mathew  * for the calling CPU that was set as the context for the specified security
58c11ba852SSoby Mathew  * state. NULL is returned if no such structure has been specified.
59c11ba852SSoby Mathew  ******************************************************************************/
60c11ba852SSoby Mathew void *cm_get_context(uint32_t security_state)
61c11ba852SSoby Mathew {
62c11ba852SSoby Mathew 	assert(security_state == NON_SECURE);
63c11ba852SSoby Mathew 	return sp_min_cpu_ctx_ptr[plat_my_core_pos()];
64c11ba852SSoby Mathew }
65c11ba852SSoby Mathew 
66c11ba852SSoby Mathew /*******************************************************************************
67c11ba852SSoby Mathew  * This function sets the pointer to the current 'cpu_context' structure for the
68c11ba852SSoby Mathew  * specified security state for the calling CPU
69c11ba852SSoby Mathew  ******************************************************************************/
70c11ba852SSoby Mathew void cm_set_context(void *context, uint32_t security_state)
71c11ba852SSoby Mathew {
72c11ba852SSoby Mathew 	assert(security_state == NON_SECURE);
73c11ba852SSoby Mathew 	sp_min_cpu_ctx_ptr[plat_my_core_pos()] = context;
74c11ba852SSoby Mathew }
75c11ba852SSoby Mathew 
76c11ba852SSoby Mathew /*******************************************************************************
77c11ba852SSoby Mathew  * This function returns a pointer to the most recent 'cpu_context' structure
78c11ba852SSoby Mathew  * for the CPU identified by `cpu_idx` that was set as the context for the
79c11ba852SSoby Mathew  * specified security state. NULL is returned if no such structure has been
80c11ba852SSoby Mathew  * specified.
81c11ba852SSoby Mathew  ******************************************************************************/
82c11ba852SSoby Mathew void *cm_get_context_by_index(unsigned int cpu_idx,
83c11ba852SSoby Mathew 				unsigned int security_state)
84c11ba852SSoby Mathew {
85c11ba852SSoby Mathew 	assert(security_state == NON_SECURE);
86c11ba852SSoby Mathew 	return sp_min_cpu_ctx_ptr[cpu_idx];
87c11ba852SSoby Mathew }
88c11ba852SSoby Mathew 
89c11ba852SSoby Mathew /*******************************************************************************
90c11ba852SSoby Mathew  * This function sets the pointer to the current 'cpu_context' structure for the
91c11ba852SSoby Mathew  * specified security state for the CPU identified by CPU index.
92c11ba852SSoby Mathew  ******************************************************************************/
93c11ba852SSoby Mathew void cm_set_context_by_index(unsigned int cpu_idx, void *context,
94c11ba852SSoby Mathew 				unsigned int security_state)
95c11ba852SSoby Mathew {
96c11ba852SSoby Mathew 	assert(security_state == NON_SECURE);
97c11ba852SSoby Mathew 	sp_min_cpu_ctx_ptr[cpu_idx] = context;
98c11ba852SSoby Mathew }
99c11ba852SSoby Mathew 
100c11ba852SSoby Mathew static void copy_cpu_ctx_to_smc_stx(const regs_t *cpu_reg_ctx,
101c11ba852SSoby Mathew 				smc_ctx_t *next_smc_ctx)
102c11ba852SSoby Mathew {
103c11ba852SSoby Mathew 	next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
104c11ba852SSoby Mathew 	next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
105c11ba852SSoby Mathew 	next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
106b6285d64SSoby Mathew 	next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
107c11ba852SSoby Mathew }
108c11ba852SSoby Mathew 
109c11ba852SSoby Mathew /*******************************************************************************
110c11ba852SSoby Mathew  * This function invokes the PSCI library interface to initialize the
111c11ba852SSoby Mathew  * non secure cpu context and copies the relevant cpu context register values
112c11ba852SSoby Mathew  * to smc context. These registers will get programmed during `smc_exit`.
113c11ba852SSoby Mathew  ******************************************************************************/
114c11ba852SSoby Mathew static void sp_min_prepare_next_image_entry(void)
115c11ba852SSoby Mathew {
116c11ba852SSoby Mathew 	entry_point_info_t *next_image_info;
117b6285d64SSoby Mathew 	cpu_context_t *ctx = cm_get_context(NON_SECURE);
118b6285d64SSoby Mathew 	u_register_t ns_sctlr;
119c11ba852SSoby Mathew 
120c11ba852SSoby Mathew 	/* Program system registers to proceed to non-secure */
121c11ba852SSoby Mathew 	next_image_info = sp_min_plat_get_bl33_ep_info();
122c11ba852SSoby Mathew 	assert(next_image_info);
123c11ba852SSoby Mathew 	assert(NON_SECURE == GET_SECURITY_STATE(next_image_info->h.attr));
124c11ba852SSoby Mathew 
125c11ba852SSoby Mathew 	INFO("SP_MIN: Preparing exit to normal world\n");
126c11ba852SSoby Mathew 
127c11ba852SSoby Mathew 	psci_prepare_next_non_secure_ctx(next_image_info);
128c11ba852SSoby Mathew 	smc_set_next_ctx(NON_SECURE);
129c11ba852SSoby Mathew 
130c11ba852SSoby Mathew 	/* Copy r0, lr and spsr from cpu context to SMC context */
131c11ba852SSoby Mathew 	copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
132c11ba852SSoby Mathew 			smc_get_next_ctx());
133b6285d64SSoby Mathew 
134b6285d64SSoby Mathew 	/* Temporarily set the NS bit to access NS SCTLR */
135b6285d64SSoby Mathew 	write_scr(read_scr() | SCR_NS_BIT);
136b6285d64SSoby Mathew 	isb();
137b6285d64SSoby Mathew 	ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
138b6285d64SSoby Mathew 	write_sctlr(ns_sctlr);
139b6285d64SSoby Mathew 	isb();
140b6285d64SSoby Mathew 
141b6285d64SSoby Mathew 	write_scr(read_scr() & ~SCR_NS_BIT);
142b6285d64SSoby Mathew 	isb();
143c11ba852SSoby Mathew }
144c11ba852SSoby Mathew 
145c11ba852SSoby Mathew /******************************************************************************
14658e946aeSSoby Mathew  * Implement the ARM Standard Service function to get arguments for a
14758e946aeSSoby Mathew  * particular service.
14858e946aeSSoby Mathew  *****************************************************************************/
14958e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
15058e946aeSSoby Mathew {
15158e946aeSSoby Mathew 	/* Setup the arguments for PSCI Library */
15258e946aeSSoby Mathew 	DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, sp_min_warm_entrypoint);
15358e946aeSSoby Mathew 
15458e946aeSSoby Mathew 	/* PSCI is the only ARM Standard Service implemented */
15558e946aeSSoby Mathew 	assert(svc_mask == PSCI_FID_MASK);
15658e946aeSSoby Mathew 
15758e946aeSSoby Mathew 	return (uintptr_t)&psci_args;
15858e946aeSSoby Mathew }
15958e946aeSSoby Mathew 
16058e946aeSSoby Mathew /******************************************************************************
161c11ba852SSoby Mathew  * The SP_MIN main function. Do the platform and PSCI Library setup. Also
162c11ba852SSoby Mathew  * initialize the runtime service framework.
163c11ba852SSoby Mathew  *****************************************************************************/
164c11ba852SSoby Mathew void sp_min_main(void)
165c11ba852SSoby Mathew {
166f426fc05SSoby Mathew 	NOTICE("SP_MIN: %s\n", version_string);
167f426fc05SSoby Mathew 	NOTICE("SP_MIN: %s\n", build_message);
168f426fc05SSoby Mathew 
169f426fc05SSoby Mathew 	/* Perform the SP_MIN platform setup */
170c11ba852SSoby Mathew 	sp_min_platform_setup();
171c11ba852SSoby Mathew 
17258e946aeSSoby Mathew 	/* Initialize the runtime services e.g. psci */
173c11ba852SSoby Mathew 	INFO("SP_MIN: Initializing runtime services\n");
174c11ba852SSoby Mathew 	runtime_svc_init();
175c11ba852SSoby Mathew 
176c11ba852SSoby Mathew 	/*
177c11ba852SSoby Mathew 	 * We are ready to enter the next EL. Prepare entry into the image
178c11ba852SSoby Mathew 	 * corresponding to the desired security state after the next ERET.
179c11ba852SSoby Mathew 	 */
180c11ba852SSoby Mathew 	sp_min_prepare_next_image_entry();
18121568304SDimitris Papastamos 
18221568304SDimitris Papastamos 	/*
18321568304SDimitris Papastamos 	 * Perform any platform specific runtime setup prior to cold boot exit
18421568304SDimitris Papastamos 	 * from SP_MIN.
18521568304SDimitris Papastamos 	 */
18621568304SDimitris Papastamos 	sp_min_plat_runtime_setup();
18710d664ceSDimitris Papastamos 
18810d664ceSDimitris Papastamos 	console_flush();
189c11ba852SSoby Mathew }
190c11ba852SSoby Mathew 
191c11ba852SSoby Mathew /******************************************************************************
192c11ba852SSoby Mathew  * This function is invoked during warm boot. Invoke the PSCI library
193c11ba852SSoby Mathew  * warm boot entry point which takes care of Architectural and platform setup/
194c11ba852SSoby Mathew  * restore. Copy the relevant cpu_context register values to smc context which
195c11ba852SSoby Mathew  * will get programmed during `smc_exit`.
196c11ba852SSoby Mathew  *****************************************************************************/
197c11ba852SSoby Mathew void sp_min_warm_boot(void)
198c11ba852SSoby Mathew {
199c11ba852SSoby Mathew 	smc_ctx_t *next_smc_ctx;
20088ad1461SDavid Cunado 	cpu_context_t *ctx = cm_get_context(NON_SECURE);
20188ad1461SDavid Cunado 	u_register_t ns_sctlr;
202c11ba852SSoby Mathew 
203c11ba852SSoby Mathew 	psci_warmboot_entrypoint();
204c11ba852SSoby Mathew 
205c11ba852SSoby Mathew 	smc_set_next_ctx(NON_SECURE);
206c11ba852SSoby Mathew 
207c11ba852SSoby Mathew 	next_smc_ctx = smc_get_next_ctx();
20832f0d3c6SDouglas Raillard 	zeromem(next_smc_ctx, sizeof(smc_ctx_t));
209c11ba852SSoby Mathew 
210c11ba852SSoby Mathew 	copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
211c11ba852SSoby Mathew 			next_smc_ctx);
21288ad1461SDavid Cunado 
21388ad1461SDavid Cunado 	/* Temporarily set the NS bit to access NS SCTLR */
21488ad1461SDavid Cunado 	write_scr(read_scr() | SCR_NS_BIT);
21588ad1461SDavid Cunado 	isb();
21688ad1461SDavid Cunado 	ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
21788ad1461SDavid Cunado 	write_sctlr(ns_sctlr);
21888ad1461SDavid Cunado 	isb();
21988ad1461SDavid Cunado 
22088ad1461SDavid Cunado 	write_scr(read_scr() & ~SCR_NS_BIT);
22188ad1461SDavid Cunado 	isb();
222c11ba852SSoby Mathew }
22371816096SEtienne Carriere 
22471816096SEtienne Carriere #if SP_MIN_WITH_SECURE_FIQ
22571816096SEtienne Carriere /******************************************************************************
22671816096SEtienne Carriere  * This function is invoked on secure interrupts. By construction of the
22771816096SEtienne Carriere  * SP_MIN, secure interrupts can only be handled when core executes in non
22871816096SEtienne Carriere  * secure state.
22971816096SEtienne Carriere  *****************************************************************************/
23071816096SEtienne Carriere void sp_min_fiq(void)
23171816096SEtienne Carriere {
23271816096SEtienne Carriere 	uint32_t id;
23371816096SEtienne Carriere 
23471816096SEtienne Carriere 	id = plat_ic_acknowledge_interrupt();
23571816096SEtienne Carriere 	sp_min_plat_fiq_handler(id);
23671816096SEtienne Carriere 	plat_ic_end_of_interrupt(id);
23771816096SEtienne Carriere }
23871816096SEtienne Carriere #endif /* SP_MIN_WITH_SECURE_FIQ */
239