blob: ae2442471a980b030511fecb046b59980292fc7b [file] [log] [blame]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001/*
2 * Copyright (c) 2013-2015, 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 <context.h>
34#include <context_mgmt.h>
35#include <cpu_data.h>
36#include <platform.h>
37
38
39/*******************************************************************************
40 * This function returns a pointer to the most recent 'cpu_context' structure
41 * for the calling CPU that was set as the context for the specified security
42 * state. NULL is returned if no such structure has been specified.
43 ******************************************************************************/
44void *cm_get_context(uint32_t security_state)
45{
46 assert(security_state <= NON_SECURE);
47
48 return get_cpu_data(cpu_context[security_state]);
49}
50
51/*******************************************************************************
52 * This function sets the pointer to the current 'cpu_context' structure for the
53 * specified security state for the calling CPU
54 ******************************************************************************/
55void cm_set_context(void *context, uint32_t security_state)
56{
57 assert(security_state <= NON_SECURE);
58
59 set_cpu_data(cpu_context[security_state], context);
60}
61
62/*******************************************************************************
63 * This function returns a pointer to the most recent 'cpu_context' structure
64 * for the CPU identified by `cpu_idx` that was set as the context for the
65 * specified security state. NULL is returned if no such structure has been
66 * specified.
67 ******************************************************************************/
68void *cm_get_context_by_index(unsigned int cpu_idx,
69 unsigned int security_state)
70{
71 assert(sec_state_is_valid(security_state));
72
73 return get_cpu_data_by_index(cpu_idx, cpu_context[security_state]);
74}
75
76/*******************************************************************************
77 * This function sets the pointer to the current 'cpu_context' structure for the
78 * specified security state for the CPU identified by CPU index.
79 ******************************************************************************/
80void cm_set_context_by_index(unsigned int cpu_idx, void *context,
81 unsigned int security_state)
82{
83 assert(sec_state_is_valid(security_state));
84
85 set_cpu_data_by_index(cpu_idx, cpu_context[security_state], context);
86}
87
88#if !ERROR_DEPRECATED
89/*
90 * These context management helpers are deprecated but are maintained for use
91 * by SPDs which have not migrated to the new API. If ERROR_DEPRECATED
92 * is enabled, these are excluded from the build so as to force users to
93 * migrate to the new API.
94 */
95
96/*******************************************************************************
97 * This function returns a pointer to the most recent 'cpu_context' structure
98 * for the CPU identified by MPIDR that was set as the context for the specified
99 * security state. NULL is returned if no such structure has been specified.
100 ******************************************************************************/
101void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state)
102{
103 assert(sec_state_is_valid(security_state));
104
105 return cm_get_context_by_index(platform_get_core_pos(mpidr), security_state);
106}
107
108/*******************************************************************************
109 * This function sets the pointer to the current 'cpu_context' structure for the
110 * specified security state for the CPU identified by MPIDR
111 ******************************************************************************/
112void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state)
113{
114 assert(sec_state_is_valid(security_state));
115
116 cm_set_context_by_index(platform_get_core_pos(mpidr),
117 context, security_state);
118}
119
120/*******************************************************************************
121 * The following function provides a compatibility function for SPDs using the
122 * existing cm library routines. This function is expected to be invoked for
123 * initializing the cpu_context for the CPU specified by MPIDR for first use.
124 ******************************************************************************/
125void cm_init_context(unsigned long mpidr, const entry_point_info_t *ep)
126{
127 if ((mpidr & MPIDR_AFFINITY_MASK) ==
128 (read_mpidr_el1() & MPIDR_AFFINITY_MASK))
129 cm_init_my_context(ep);
130 else
131 cm_init_context_by_index(platform_get_core_pos(mpidr), ep);
132}
133#endif