opteed_pm.c 7.64 KB
Newer Older
Jens Wiklander's avatar
Jens Wiklander committed
1
/*
Edison Ai's avatar
Edison Ai committed
2
 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
Jens Wiklander's avatar
Jens Wiklander committed
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
Jens Wiklander's avatar
Jens Wiklander committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 */

#include <arch_helpers.h>
#include <assert.h>
#include <bl_common.h>
#include <context_mgmt.h>
#include <debug.h>
#include <platform.h>
#include "opteed_private.h"

/*******************************************************************************
 * The target cpu is being turned on. Allow the OPTEED/OPTEE to perform any
 * actions needed. Nothing at the moment.
 ******************************************************************************/
19
static void opteed_cpu_on_handler(u_register_t target_cpu)
Jens Wiklander's avatar
Jens Wiklander committed
20
21
22
23
24
25
26
{
}

/*******************************************************************************
 * This cpu is being turned off. Allow the OPTEED/OPTEE to perform any actions
 * needed
 ******************************************************************************/
27
static int32_t opteed_cpu_off_handler(u_register_t unused)
Jens Wiklander's avatar
Jens Wiklander committed
28
29
{
	int32_t rc = 0;
30
	uint32_t linear_id = plat_my_core_pos();
Jens Wiklander's avatar
Jens Wiklander committed
31
32
	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];

Daniel Boulby's avatar
Daniel Boulby committed
33
	assert(optee_vector_table);
Jens Wiklander's avatar
Jens Wiklander committed
34
35
36
	assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);

	/* Program the entry point and enter OPTEE */
Daniel Boulby's avatar
Daniel Boulby committed
37
	cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_off_entry);
Jens Wiklander's avatar
Jens Wiklander committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	rc = opteed_synchronous_sp_entry(optee_ctx);

	/*
	 * Read the response from OPTEE. A non-zero return means that
	 * something went wrong while communicating with OPTEE.
	 */
	if (rc != 0)
		panic();

	/*
	 * Reset OPTEE's context for a fresh start when this cpu is turned on
	 * subsequently.
	 */
	set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF);

	 return 0;
}

/*******************************************************************************
 * This cpu is being suspended. S-EL1 state must have been saved in the
 * resident cpu (mpidr format) if it is a UP/UP migratable OPTEE.
 ******************************************************************************/
60
static void opteed_cpu_suspend_handler(u_register_t max_off_pwrlvl)
Jens Wiklander's avatar
Jens Wiklander committed
61
62
{
	int32_t rc = 0;
63
	uint32_t linear_id = plat_my_core_pos();
Jens Wiklander's avatar
Jens Wiklander committed
64
65
	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];

Daniel Boulby's avatar
Daniel Boulby committed
66
	assert(optee_vector_table);
Jens Wiklander's avatar
Jens Wiklander committed
67
68
	assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);

69
	/* Program the entry point and enter OPTEE */
Daniel Boulby's avatar
Daniel Boulby committed
70
	cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_suspend_entry);
Jens Wiklander's avatar
Jens Wiklander committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	rc = opteed_synchronous_sp_entry(optee_ctx);

	/*
	 * Read the response from OPTEE. A non-zero return means that
	 * something went wrong while communicating with OPTEE.
	 */
	if (rc != 0)
		panic();

	/* Update its context to reflect the state OPTEE is in */
	set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_SUSPEND);
}

/*******************************************************************************
 * This cpu has been turned on. Enter OPTEE to initialise S-EL1 and other bits
 * before passing control back to the Secure Monitor. Entry in S-El1 is done
 * after initialising minimal architectural state that guarantees safe
 * execution.
 ******************************************************************************/
90
static void opteed_cpu_on_finish_handler(u_register_t unused)
Jens Wiklander's avatar
Jens Wiklander committed
91
92
{
	int32_t rc = 0;
93
	uint32_t linear_id = plat_my_core_pos();
Jens Wiklander's avatar
Jens Wiklander committed
94
95
96
	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
	entry_point_info_t optee_on_entrypoint;

Daniel Boulby's avatar
Daniel Boulby committed
97
	assert(optee_vector_table);
Jens Wiklander's avatar
Jens Wiklander committed
98
99
100
	assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_OFF);

	opteed_init_optee_ep_state(&optee_on_entrypoint, opteed_rw,
Daniel Boulby's avatar
Daniel Boulby committed
101
				(uint64_t)&optee_vector_table->cpu_on_entry,
102
				0, 0, 0, optee_ctx);
Jens Wiklander's avatar
Jens Wiklander committed
103
104

	/* Initialise this cpu's secure context */
105
	cm_init_my_context(&optee_on_entrypoint);
Jens Wiklander's avatar
Jens Wiklander committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

	/* Enter OPTEE */
	rc = opteed_synchronous_sp_entry(optee_ctx);

	/*
	 * Read the response from OPTEE. A non-zero return means that
	 * something went wrong while communicating with OPTEE.
	 */
	if (rc != 0)
		panic();

	/* Update its context to reflect the state OPTEE is in */
	set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
}

/*******************************************************************************
 * This cpu has resumed from suspend. The OPTEED saved the OPTEE context when it
 * completed the preceding suspend call. Use that context to program an entry
 * into OPTEE to allow it to do any remaining book keeping
 ******************************************************************************/
126
static void opteed_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
Jens Wiklander's avatar
Jens Wiklander committed
127
128
{
	int32_t rc = 0;
129
	uint32_t linear_id = plat_my_core_pos();
Jens Wiklander's avatar
Jens Wiklander committed
130
131
	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];

Daniel Boulby's avatar
Daniel Boulby committed
132
	assert(optee_vector_table);
Jens Wiklander's avatar
Jens Wiklander committed
133
134
	assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_SUSPEND);

135
	/* Program the entry point, max_off_pwrlvl and enter the SP */
Jens Wiklander's avatar
Jens Wiklander committed
136
137
	write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
		      CTX_GPREG_X0,
138
		      max_off_pwrlvl);
Daniel Boulby's avatar
Daniel Boulby committed
139
	cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_resume_entry);
Jens Wiklander's avatar
Jens Wiklander committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	rc = opteed_synchronous_sp_entry(optee_ctx);

	/*
	 * Read the response from OPTEE. A non-zero return means that
	 * something went wrong while communicating with OPTEE.
	 */
	if (rc != 0)
		panic();

	/* Update its context to reflect the state OPTEE is in */
	set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
}

/*******************************************************************************
 * Return the type of OPTEE the OPTEED is dealing with. Report the current
 * resident cpu (mpidr format) if it is a UP/UP migratable OPTEE.
 ******************************************************************************/
157
static int32_t opteed_cpu_migrate_info(u_register_t *resident_cpu)
Jens Wiklander's avatar
Jens Wiklander committed
158
159
160
161
162
163
164
165
166
167
{
	return OPTEE_MIGRATE_INFO;
}

/*******************************************************************************
 * System is about to be switched off. Allow the OPTEED/OPTEE to perform
 * any actions needed.
 ******************************************************************************/
static void opteed_system_off(void)
{
168
	uint32_t linear_id = plat_my_core_pos();
Jens Wiklander's avatar
Jens Wiklander committed
169
170
	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];

Daniel Boulby's avatar
Daniel Boulby committed
171
	assert(optee_vector_table);
Jens Wiklander's avatar
Jens Wiklander committed
172
173
174
	assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);

	/* Program the entry point */
Daniel Boulby's avatar
Daniel Boulby committed
175
	cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->system_off_entry);
Jens Wiklander's avatar
Jens Wiklander committed
176
177
178
179
180
181
182
183
184
185
186
187

	/* Enter OPTEE. We do not care about the return value because we
	 * must continue the shutdown anyway */
	opteed_synchronous_sp_entry(optee_ctx);
}

/*******************************************************************************
 * System is about to be reset. Allow the OPTEED/OPTEE to perform
 * any actions needed.
 ******************************************************************************/
static void opteed_system_reset(void)
{
188
	uint32_t linear_id = plat_my_core_pos();
Jens Wiklander's avatar
Jens Wiklander committed
189
190
	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];

Daniel Boulby's avatar
Daniel Boulby committed
191
	assert(optee_vector_table);
Jens Wiklander's avatar
Jens Wiklander committed
192
193
194
	assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);

	/* Program the entry point */
Daniel Boulby's avatar
Daniel Boulby committed
195
	cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->system_reset_entry);
Jens Wiklander's avatar
Jens Wiklander committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

	/* Enter OPTEE. We do not care about the return value because we
	 * must continue the reset anyway */
	opteed_synchronous_sp_entry(optee_ctx);
}


/*******************************************************************************
 * Structure populated by the OPTEE Dispatcher to be given a chance to
 * perform any OPTEE bookkeeping before PSCI executes a power mgmt.
 * operation.
 ******************************************************************************/
const spd_pm_ops_t opteed_pm = {
	.svc_on = opteed_cpu_on_handler,
	.svc_off = opteed_cpu_off_handler,
	.svc_suspend = opteed_cpu_suspend_handler,
	.svc_on_finish = opteed_cpu_on_finish_handler,
	.svc_suspend_finish = opteed_cpu_suspend_finish_handler,
	.svc_migrate = NULL,
	.svc_migrate_info = opteed_cpu_migrate_info,
	.svc_system_off = opteed_system_off,
	.svc_system_reset = opteed_system_reset,
};