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