xref: /rk3399_ARM-atf/include/lib/el3_runtime/context_mgmt.h (revision 2f41c9a7be46b148d557d3d933547c6e9ad1fd40)
1532ed618SSoby Mathew /*
2a6b3643cSChris Kay  * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7a0fee747SAntonio Nino Diaz #ifndef CONTEXT_MGMT_H
8a0fee747SAntonio Nino Diaz #define CONTEXT_MGMT_H
9532ed618SSoby Mathew 
10b10d4499SJeenu Viswambharan #include <assert.h>
111634cae8SAntonio Nino Diaz #include <context.h>
12b10d4499SJeenu Viswambharan #include <stdint.h>
13532ed618SSoby Mathew 
1409d40e0eSAntonio Nino Diaz #include <arch.h>
1509d40e0eSAntonio Nino Diaz 
16532ed618SSoby Mathew /*******************************************************************************
17532ed618SSoby Mathew  * Forward declarations
18532ed618SSoby Mathew  ******************************************************************************/
19532ed618SSoby Mathew struct entry_point_info;
20532ed618SSoby Mathew 
21532ed618SSoby Mathew /*******************************************************************************
22532ed618SSoby Mathew  * Function & variable prototypes
23532ed618SSoby Mathew  ******************************************************************************/
24532ed618SSoby Mathew void cm_init(void);
25532ed618SSoby Mathew void *cm_get_context_by_index(unsigned int cpu_idx,
26532ed618SSoby Mathew 			      unsigned int security_state);
27532ed618SSoby Mathew void cm_set_context_by_index(unsigned int cpu_idx,
28532ed618SSoby Mathew 			     void *context,
29532ed618SSoby Mathew 			     unsigned int security_state);
30532ed618SSoby Mathew void *cm_get_context(uint32_t security_state);
31532ed618SSoby Mathew void cm_set_context(void *context, uint32_t security_state);
32532ed618SSoby Mathew void cm_init_my_context(const struct entry_point_info *ep);
3325d819a3SVarun Wadekar void cm_setup_context(cpu_context_t *ctx, const struct entry_point_info *ep);
34532ed618SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state);
358b95e848SZelalem Aweke void cm_prepare_el3_exit_ns(void);
36e33b78a6SSoby Mathew 
37a6b3643cSChris Kay #if !IMAGE_BL1
38a6b3643cSChris Kay void cm_init_context_by_index(unsigned int cpu_idx,
39a6b3643cSChris Kay 			      const struct entry_point_info *ep);
40a6b3643cSChris Kay #endif /* !IMAGE_BL1 */
41a6b3643cSChris Kay 
42402b3cf8SJulius Werner #ifdef __aarch64__
4324a70738SBoyan Karatotev #if IMAGE_BL31
4424a70738SBoyan Karatotev void cm_manage_extensions_el3(void);
45461c0a5dSElizabeth Ho void manage_extensions_nonsecure_per_world(void);
464087ed6cSJayanth Dodderi Chidanand void cm_el3_arch_init_per_world(per_world_context_t *per_world_ctx);
47*2f41c9a7SManish Pandey void cm_handle_asymmetric_features(void);
4824a70738SBoyan Karatotev #endif
494087ed6cSJayanth Dodderi Chidanand 
5028f39f02SMax Shvetsov #if CTX_INCLUDE_EL2_REGS
5128f39f02SMax Shvetsov void cm_el2_sysregs_context_save(uint32_t security_state);
5228f39f02SMax Shvetsov void cm_el2_sysregs_context_restore(uint32_t security_state);
5328f39f02SMax Shvetsov #endif
5428f39f02SMax Shvetsov 
55532ed618SSoby Mathew void cm_el1_sysregs_context_save(uint32_t security_state);
56532ed618SSoby Mathew void cm_el1_sysregs_context_restore(uint32_t security_state);
57532ed618SSoby Mathew void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint);
58532ed618SSoby Mathew void cm_set_elr_spsr_el3(uint32_t security_state,
59532ed618SSoby Mathew 			uintptr_t entrypoint, uint32_t spsr);
60532ed618SSoby Mathew void cm_write_scr_el3_bit(uint32_t security_state,
61532ed618SSoby Mathew 			  uint32_t bit_pos,
62532ed618SSoby Mathew 			  uint32_t value);
63532ed618SSoby Mathew void cm_set_next_eret_context(uint32_t security_state);
64f1be00daSLouis Mayencourt u_register_t cm_get_scr_el3(uint32_t security_state);
65532ed618SSoby Mathew 
66532ed618SSoby Mathew /* Inline definitions */
67532ed618SSoby Mathew 
68532ed618SSoby Mathew /*******************************************************************************
69532ed618SSoby Mathew  * This function is used to program the context that's used for exception
70532ed618SSoby Mathew  * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
71532ed618SSoby Mathew  * the required security state
72532ed618SSoby Mathew  ******************************************************************************/
73532ed618SSoby Mathew static inline void cm_set_next_context(void *context)
74532ed618SSoby Mathew {
75aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
76532ed618SSoby Mathew 	uint64_t sp_mode;
77532ed618SSoby Mathew 
78532ed618SSoby Mathew 	/*
79532ed618SSoby Mathew 	 * Check that this function is called with SP_EL0 as the stack
80532ed618SSoby Mathew 	 * pointer
81532ed618SSoby Mathew 	 */
82532ed618SSoby Mathew 	__asm__ volatile("mrs	%0, SPSel\n"
83532ed618SSoby Mathew 			 : "=r" (sp_mode));
84532ed618SSoby Mathew 
85532ed618SSoby Mathew 	assert(sp_mode == MODE_SP_EL0);
86aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
87532ed618SSoby Mathew 
88532ed618SSoby Mathew 	__asm__ volatile("msr	spsel, #1\n"
89532ed618SSoby Mathew 			 "mov	sp, %0\n"
90532ed618SSoby Mathew 			 "msr	spsel, #0\n"
91532ed618SSoby Mathew 			 : : "r" (context));
92532ed618SSoby Mathew }
93f3b4914bSYatharth Kochar 
94f3b4914bSYatharth Kochar #else
95f3b4914bSYatharth Kochar void *cm_get_next_context(void);
962ed7b71eSEtienne Carriere void cm_set_next_context(void *context);
9724a70738SBoyan Karatotev static inline void cm_manage_extensions_el3(void) {}
98461c0a5dSElizabeth Ho static inline void manage_extensions_nonsecure_per_world(void) {}
99*2f41c9a7SManish Pandey static inline void cm_handle_asymmetric_features(void) {}
100402b3cf8SJulius Werner #endif /* __aarch64__ */
101f3b4914bSYatharth Kochar 
102a0fee747SAntonio Nino Diaz #endif /* CONTEXT_MGMT_H */
103