xref: /rk3399_ARM-atf/bl31/bl31_context_mgmt.c (revision 532ed6183868036e4a4f83cd7a71b93266a3bdb7)
1bbf8f6f9SYatharth Kochar /*
2*532ed618SSoby Mathew  * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3bbf8f6f9SYatharth Kochar  *
4bbf8f6f9SYatharth Kochar  * Redistribution and use in source and binary forms, with or without
5bbf8f6f9SYatharth Kochar  * modification, are permitted provided that the following conditions are met:
6bbf8f6f9SYatharth Kochar  *
7bbf8f6f9SYatharth Kochar  * Redistributions of source code must retain the above copyright notice, this
8bbf8f6f9SYatharth Kochar  * list of conditions and the following disclaimer.
9bbf8f6f9SYatharth Kochar  *
10bbf8f6f9SYatharth Kochar  * Redistributions in binary form must reproduce the above copyright notice,
11bbf8f6f9SYatharth Kochar  * this list of conditions and the following disclaimer in the documentation
12bbf8f6f9SYatharth Kochar  * and/or other materials provided with the distribution.
13bbf8f6f9SYatharth Kochar  *
14bbf8f6f9SYatharth Kochar  * Neither the name of ARM nor the names of its contributors may be used
15bbf8f6f9SYatharth Kochar  * to endorse or promote products derived from this software without specific
16bbf8f6f9SYatharth Kochar  * prior written permission.
17bbf8f6f9SYatharth Kochar  *
18bbf8f6f9SYatharth Kochar  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19bbf8f6f9SYatharth Kochar  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20bbf8f6f9SYatharth Kochar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21bbf8f6f9SYatharth Kochar  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22bbf8f6f9SYatharth Kochar  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23bbf8f6f9SYatharth Kochar  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24bbf8f6f9SYatharth Kochar  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25bbf8f6f9SYatharth Kochar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26bbf8f6f9SYatharth Kochar  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27bbf8f6f9SYatharth Kochar  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28bbf8f6f9SYatharth Kochar  * POSSIBILITY OF SUCH DAMAGE.
29bbf8f6f9SYatharth Kochar  */
30bbf8f6f9SYatharth Kochar 
31bbf8f6f9SYatharth Kochar #include <assert.h>
32bbf8f6f9SYatharth Kochar #include <bl31.h>
33*532ed618SSoby Mathew #include <bl_common.h>
34bbf8f6f9SYatharth Kochar #include <context.h>
35bbf8f6f9SYatharth Kochar #include <context_mgmt.h>
36bbf8f6f9SYatharth Kochar #include <cpu_data.h>
37bbf8f6f9SYatharth Kochar #include <platform.h>
38bbf8f6f9SYatharth Kochar 
39bbf8f6f9SYatharth Kochar 
40bbf8f6f9SYatharth Kochar /*******************************************************************************
41bbf8f6f9SYatharth Kochar  * This function returns a pointer to the most recent 'cpu_context' structure
42bbf8f6f9SYatharth Kochar  * for the calling CPU that was set as the context for the specified security
43bbf8f6f9SYatharth Kochar  * state. NULL is returned if no such structure has been specified.
44bbf8f6f9SYatharth Kochar  ******************************************************************************/
45bbf8f6f9SYatharth Kochar void *cm_get_context(uint32_t security_state)
46bbf8f6f9SYatharth Kochar {
47bbf8f6f9SYatharth Kochar 	assert(security_state <= NON_SECURE);
48bbf8f6f9SYatharth Kochar 
49bbf8f6f9SYatharth Kochar 	return get_cpu_data(cpu_context[security_state]);
50bbf8f6f9SYatharth Kochar }
51bbf8f6f9SYatharth Kochar 
52bbf8f6f9SYatharth Kochar /*******************************************************************************
53bbf8f6f9SYatharth Kochar  * This function sets the pointer to the current 'cpu_context' structure for the
54bbf8f6f9SYatharth Kochar  * specified security state for the calling CPU
55bbf8f6f9SYatharth Kochar  ******************************************************************************/
56bbf8f6f9SYatharth Kochar void cm_set_context(void *context, uint32_t security_state)
57bbf8f6f9SYatharth Kochar {
58bbf8f6f9SYatharth Kochar 	assert(security_state <= NON_SECURE);
59bbf8f6f9SYatharth Kochar 
60bbf8f6f9SYatharth Kochar 	set_cpu_data(cpu_context[security_state], context);
61bbf8f6f9SYatharth Kochar }
62bbf8f6f9SYatharth Kochar 
63bbf8f6f9SYatharth Kochar /*******************************************************************************
64bbf8f6f9SYatharth Kochar  * This function returns a pointer to the most recent 'cpu_context' structure
65bbf8f6f9SYatharth Kochar  * for the CPU identified by `cpu_idx` that was set as the context for the
66bbf8f6f9SYatharth Kochar  * specified security state. NULL is returned if no such structure has been
67bbf8f6f9SYatharth Kochar  * specified.
68bbf8f6f9SYatharth Kochar  ******************************************************************************/
69bbf8f6f9SYatharth Kochar void *cm_get_context_by_index(unsigned int cpu_idx,
70bbf8f6f9SYatharth Kochar 				unsigned int security_state)
71bbf8f6f9SYatharth Kochar {
72bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
73bbf8f6f9SYatharth Kochar 
74bbf8f6f9SYatharth Kochar 	return get_cpu_data_by_index(cpu_idx, cpu_context[security_state]);
75bbf8f6f9SYatharth Kochar }
76bbf8f6f9SYatharth Kochar 
77bbf8f6f9SYatharth Kochar /*******************************************************************************
78bbf8f6f9SYatharth Kochar  * This function sets the pointer to the current 'cpu_context' structure for the
79bbf8f6f9SYatharth Kochar  * specified security state for the CPU identified by CPU index.
80bbf8f6f9SYatharth Kochar  ******************************************************************************/
81bbf8f6f9SYatharth Kochar void cm_set_context_by_index(unsigned int cpu_idx, void *context,
82bbf8f6f9SYatharth Kochar 				unsigned int security_state)
83bbf8f6f9SYatharth Kochar {
84bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
85bbf8f6f9SYatharth Kochar 
86bbf8f6f9SYatharth Kochar 	set_cpu_data_by_index(cpu_idx, cpu_context[security_state], context);
87bbf8f6f9SYatharth Kochar }
88bbf8f6f9SYatharth Kochar 
89bbf8f6f9SYatharth Kochar #if !ERROR_DEPRECATED
90bbf8f6f9SYatharth Kochar /*
91bbf8f6f9SYatharth Kochar  * These context management helpers are deprecated but are maintained for use
92bbf8f6f9SYatharth Kochar  * by SPDs which have not migrated to the new API. If ERROR_DEPRECATED
93bbf8f6f9SYatharth Kochar  * is enabled, these are excluded from the build so as to force users to
94bbf8f6f9SYatharth Kochar  * migrate to the new API.
95bbf8f6f9SYatharth Kochar  */
96bbf8f6f9SYatharth Kochar 
97bbf8f6f9SYatharth Kochar /*******************************************************************************
98bbf8f6f9SYatharth Kochar  * This function returns a pointer to the most recent 'cpu_context' structure
99bbf8f6f9SYatharth Kochar  * for the CPU identified by MPIDR that was set as the context for the specified
100bbf8f6f9SYatharth Kochar  * security state. NULL is returned if no such structure has been specified.
101bbf8f6f9SYatharth Kochar  ******************************************************************************/
102bbf8f6f9SYatharth Kochar void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state)
103bbf8f6f9SYatharth Kochar {
104bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
105bbf8f6f9SYatharth Kochar 
106bbf8f6f9SYatharth Kochar 	return cm_get_context_by_index(platform_get_core_pos(mpidr), security_state);
107bbf8f6f9SYatharth Kochar }
108bbf8f6f9SYatharth Kochar 
109bbf8f6f9SYatharth Kochar /*******************************************************************************
110bbf8f6f9SYatharth Kochar  * This function sets the pointer to the current 'cpu_context' structure for the
111bbf8f6f9SYatharth Kochar  * specified security state for the CPU identified by MPIDR
112bbf8f6f9SYatharth Kochar  ******************************************************************************/
113bbf8f6f9SYatharth Kochar void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state)
114bbf8f6f9SYatharth Kochar {
115bbf8f6f9SYatharth Kochar 	assert(sec_state_is_valid(security_state));
116bbf8f6f9SYatharth Kochar 
117bbf8f6f9SYatharth Kochar 	cm_set_context_by_index(platform_get_core_pos(mpidr),
118bbf8f6f9SYatharth Kochar 						 context, security_state);
119bbf8f6f9SYatharth Kochar }
120bbf8f6f9SYatharth Kochar 
121bbf8f6f9SYatharth Kochar /*******************************************************************************
122bbf8f6f9SYatharth Kochar  * The following function provides a compatibility function for SPDs using the
123bbf8f6f9SYatharth Kochar  * existing cm library routines. This function is expected to be invoked for
124bbf8f6f9SYatharth Kochar  * initializing the cpu_context for the CPU specified by MPIDR for first use.
125bbf8f6f9SYatharth Kochar  ******************************************************************************/
126bbf8f6f9SYatharth Kochar void cm_init_context(unsigned long mpidr, const entry_point_info_t *ep)
127bbf8f6f9SYatharth Kochar {
128bbf8f6f9SYatharth Kochar 	if ((mpidr & MPIDR_AFFINITY_MASK) ==
129bbf8f6f9SYatharth Kochar 			(read_mpidr_el1() & MPIDR_AFFINITY_MASK))
130bbf8f6f9SYatharth Kochar 		cm_init_my_context(ep);
131bbf8f6f9SYatharth Kochar 	else
132bbf8f6f9SYatharth Kochar 		cm_init_context_by_index(platform_get_core_pos(mpidr), ep);
133bbf8f6f9SYatharth Kochar }
134bbf8f6f9SYatharth Kochar #endif
135