xref: /rk3399_ARM-atf/bl31/bl31_context_mgmt.c (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
1bbf8f6f9SYatharth Kochar /*
2532ed618SSoby Mathew  * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3bbf8f6f9SYatharth Kochar  *
4*82cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5bbf8f6f9SYatharth Kochar  */
6bbf8f6f9SYatharth Kochar 
7bbf8f6f9SYatharth Kochar #include <assert.h>
8bbf8f6f9SYatharth Kochar #include <bl31.h>
9532ed618SSoby Mathew #include <bl_common.h>
10bbf8f6f9SYatharth Kochar #include <context.h>
11bbf8f6f9SYatharth Kochar #include <context_mgmt.h>
12bbf8f6f9SYatharth Kochar #include <cpu_data.h>
13bbf8f6f9SYatharth Kochar #include <platform.h>
14bbf8f6f9SYatharth Kochar 
15bbf8f6f9SYatharth Kochar 
16bbf8f6f9SYatharth Kochar /*******************************************************************************
17bbf8f6f9SYatharth Kochar  * This function returns a pointer to the most recent 'cpu_context' structure
18bbf8f6f9SYatharth Kochar  * for the calling CPU that was set as the context for the specified security
19bbf8f6f9SYatharth Kochar  * state. NULL is returned if no such structure has been specified.
20bbf8f6f9SYatharth Kochar  ******************************************************************************/
21bbf8f6f9SYatharth Kochar void *cm_get_context(uint32_t security_state)
22bbf8f6f9SYatharth Kochar {
23bbf8f6f9SYatharth Kochar 	assert(security_state <= NON_SECURE);
24bbf8f6f9SYatharth Kochar 
25bbf8f6f9SYatharth Kochar 	return get_cpu_data(cpu_context[security_state]);
26bbf8f6f9SYatharth Kochar }
27bbf8f6f9SYatharth Kochar 
28bbf8f6f9SYatharth Kochar /*******************************************************************************
29bbf8f6f9SYatharth Kochar  * This function sets the pointer to the current 'cpu_context' structure for the
30bbf8f6f9SYatharth Kochar  * specified security state for the calling CPU
31bbf8f6f9SYatharth Kochar  ******************************************************************************/
32bbf8f6f9SYatharth Kochar void cm_set_context(void *context, uint32_t security_state)
33bbf8f6f9SYatharth Kochar {
34bbf8f6f9SYatharth Kochar 	assert(security_state <= NON_SECURE);
35bbf8f6f9SYatharth Kochar 
36bbf8f6f9SYatharth Kochar 	set_cpu_data(cpu_context[security_state], context);
37bbf8f6f9SYatharth Kochar }
38bbf8f6f9SYatharth Kochar 
39bbf8f6f9SYatharth Kochar /*******************************************************************************
40bbf8f6f9SYatharth Kochar  * This function returns a pointer to the most recent 'cpu_context' structure
41bbf8f6f9SYatharth Kochar  * for the CPU identified by `cpu_idx` that was set as the context for the
42bbf8f6f9SYatharth Kochar  * specified security state. NULL is returned if no such structure has been
43bbf8f6f9SYatharth Kochar  * specified.
44bbf8f6f9SYatharth Kochar  ******************************************************************************/
45bbf8f6f9SYatharth Kochar void *cm_get_context_by_index(unsigned int cpu_idx,
46bbf8f6f9SYatharth Kochar 				unsigned int security_state)
47bbf8f6f9SYatharth Kochar {
48bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
49bbf8f6f9SYatharth Kochar 
50bbf8f6f9SYatharth Kochar 	return get_cpu_data_by_index(cpu_idx, cpu_context[security_state]);
51bbf8f6f9SYatharth Kochar }
52bbf8f6f9SYatharth Kochar 
53bbf8f6f9SYatharth Kochar /*******************************************************************************
54bbf8f6f9SYatharth Kochar  * This function sets the pointer to the current 'cpu_context' structure for the
55bbf8f6f9SYatharth Kochar  * specified security state for the CPU identified by CPU index.
56bbf8f6f9SYatharth Kochar  ******************************************************************************/
57bbf8f6f9SYatharth Kochar void cm_set_context_by_index(unsigned int cpu_idx, void *context,
58bbf8f6f9SYatharth Kochar 				unsigned int security_state)
59bbf8f6f9SYatharth Kochar {
60bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
61bbf8f6f9SYatharth Kochar 
62bbf8f6f9SYatharth Kochar 	set_cpu_data_by_index(cpu_idx, cpu_context[security_state], context);
63bbf8f6f9SYatharth Kochar }
64bbf8f6f9SYatharth Kochar 
65bbf8f6f9SYatharth Kochar #if !ERROR_DEPRECATED
66bbf8f6f9SYatharth Kochar /*
67bbf8f6f9SYatharth Kochar  * These context management helpers are deprecated but are maintained for use
68bbf8f6f9SYatharth Kochar  * by SPDs which have not migrated to the new API. If ERROR_DEPRECATED
69bbf8f6f9SYatharth Kochar  * is enabled, these are excluded from the build so as to force users to
70bbf8f6f9SYatharth Kochar  * migrate to the new API.
71bbf8f6f9SYatharth Kochar  */
72bbf8f6f9SYatharth Kochar 
73bbf8f6f9SYatharth Kochar /*******************************************************************************
74bbf8f6f9SYatharth Kochar  * This function returns a pointer to the most recent 'cpu_context' structure
75bbf8f6f9SYatharth Kochar  * for the CPU identified by MPIDR that was set as the context for the specified
76bbf8f6f9SYatharth Kochar  * security state. NULL is returned if no such structure has been specified.
77bbf8f6f9SYatharth Kochar  ******************************************************************************/
78bbf8f6f9SYatharth Kochar void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state)
79bbf8f6f9SYatharth Kochar {
80bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
81bbf8f6f9SYatharth Kochar 
82bbf8f6f9SYatharth Kochar 	return cm_get_context_by_index(platform_get_core_pos(mpidr), security_state);
83bbf8f6f9SYatharth Kochar }
84bbf8f6f9SYatharth Kochar 
85bbf8f6f9SYatharth Kochar /*******************************************************************************
86bbf8f6f9SYatharth Kochar  * This function sets the pointer to the current 'cpu_context' structure for the
87bbf8f6f9SYatharth Kochar  * specified security state for the CPU identified by MPIDR
88bbf8f6f9SYatharth Kochar  ******************************************************************************/
89bbf8f6f9SYatharth Kochar void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state)
90bbf8f6f9SYatharth Kochar {
91bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
92bbf8f6f9SYatharth Kochar 
93bbf8f6f9SYatharth Kochar 	cm_set_context_by_index(platform_get_core_pos(mpidr),
94bbf8f6f9SYatharth Kochar 						 context, security_state);
95bbf8f6f9SYatharth Kochar }
96bbf8f6f9SYatharth Kochar 
97bbf8f6f9SYatharth Kochar /*******************************************************************************
98bbf8f6f9SYatharth Kochar  * The following function provides a compatibility function for SPDs using the
99bbf8f6f9SYatharth Kochar  * existing cm library routines. This function is expected to be invoked for
100bbf8f6f9SYatharth Kochar  * initializing the cpu_context for the CPU specified by MPIDR for first use.
101bbf8f6f9SYatharth Kochar  ******************************************************************************/
102bbf8f6f9SYatharth Kochar void cm_init_context(unsigned long mpidr, const entry_point_info_t *ep)
103bbf8f6f9SYatharth Kochar {
104bbf8f6f9SYatharth Kochar 	if ((mpidr & MPIDR_AFFINITY_MASK) ==
105bbf8f6f9SYatharth Kochar 			(read_mpidr_el1() & MPIDR_AFFINITY_MASK))
106bbf8f6f9SYatharth Kochar 		cm_init_my_context(ep);
107bbf8f6f9SYatharth Kochar 	else
108bbf8f6f9SYatharth Kochar 		cm_init_context_by_index(platform_get_core_pos(mpidr), ep);
109bbf8f6f9SYatharth Kochar }
110bbf8f6f9SYatharth Kochar #endif
111