xref: /aosp_15_r20/external/arm-trusted-firmware/bl31/bl31_context_mgmt.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2013-2021, Arm Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park 
9*54fd6939SJiyong Park #include <bl31/bl31.h>
10*54fd6939SJiyong Park #include <common/bl_common.h>
11*54fd6939SJiyong Park #include <context.h>
12*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
13*54fd6939SJiyong Park #include <lib/el3_runtime/cpu_data.h>
14*54fd6939SJiyong Park 
15*54fd6939SJiyong Park /*******************************************************************************
16*54fd6939SJiyong Park  * This function returns a pointer to the most recent 'cpu_context' structure
17*54fd6939SJiyong Park  * for the calling CPU that was set as the context for the specified security
18*54fd6939SJiyong Park  * state. NULL is returned if no such structure has been specified.
19*54fd6939SJiyong Park  ******************************************************************************/
cm_get_context(uint32_t security_state)20*54fd6939SJiyong Park void *cm_get_context(uint32_t security_state)
21*54fd6939SJiyong Park {
22*54fd6939SJiyong Park 	assert(sec_state_is_valid(security_state));
23*54fd6939SJiyong Park 
24*54fd6939SJiyong Park 	return get_cpu_data(cpu_context[get_cpu_context_index(security_state)]);
25*54fd6939SJiyong Park }
26*54fd6939SJiyong Park 
27*54fd6939SJiyong Park /*******************************************************************************
28*54fd6939SJiyong Park  * This function sets the pointer to the current 'cpu_context' structure for the
29*54fd6939SJiyong Park  * specified security state for the calling CPU
30*54fd6939SJiyong Park  ******************************************************************************/
cm_set_context(void * context,uint32_t security_state)31*54fd6939SJiyong Park void cm_set_context(void *context, uint32_t security_state)
32*54fd6939SJiyong Park {
33*54fd6939SJiyong Park 	assert(sec_state_is_valid(security_state));
34*54fd6939SJiyong Park 
35*54fd6939SJiyong Park 	set_cpu_data(cpu_context[get_cpu_context_index(security_state)],
36*54fd6939SJiyong Park 			context);
37*54fd6939SJiyong Park }
38*54fd6939SJiyong Park 
39*54fd6939SJiyong Park /*******************************************************************************
40*54fd6939SJiyong Park  * This function returns a pointer to the most recent 'cpu_context' structure
41*54fd6939SJiyong Park  * for the CPU identified by `cpu_idx` that was set as the context for the
42*54fd6939SJiyong Park  * specified security state. NULL is returned if no such structure has been
43*54fd6939SJiyong Park  * specified.
44*54fd6939SJiyong Park  ******************************************************************************/
cm_get_context_by_index(unsigned int cpu_idx,unsigned int security_state)45*54fd6939SJiyong Park void *cm_get_context_by_index(unsigned int cpu_idx,
46*54fd6939SJiyong Park 				unsigned int security_state)
47*54fd6939SJiyong Park {
48*54fd6939SJiyong Park 	assert(sec_state_is_valid(security_state));
49*54fd6939SJiyong Park 
50*54fd6939SJiyong Park 	return get_cpu_data_by_index(cpu_idx,
51*54fd6939SJiyong Park 			cpu_context[get_cpu_context_index(security_state)]);
52*54fd6939SJiyong Park }
53*54fd6939SJiyong Park 
54*54fd6939SJiyong Park /*******************************************************************************
55*54fd6939SJiyong Park  * This function sets the pointer to the current 'cpu_context' structure for the
56*54fd6939SJiyong Park  * specified security state for the CPU identified by CPU index.
57*54fd6939SJiyong Park  ******************************************************************************/
cm_set_context_by_index(unsigned int cpu_idx,void * context,unsigned int security_state)58*54fd6939SJiyong Park void cm_set_context_by_index(unsigned int cpu_idx, void *context,
59*54fd6939SJiyong Park 				unsigned int security_state)
60*54fd6939SJiyong Park {
61*54fd6939SJiyong Park 	assert(sec_state_is_valid(security_state));
62*54fd6939SJiyong Park 
63*54fd6939SJiyong Park 	set_cpu_data_by_index(cpu_idx,
64*54fd6939SJiyong Park 			cpu_context[get_cpu_context_index(security_state)],
65*54fd6939SJiyong Park 			context);
66*54fd6939SJiyong Park }
67