1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park
9*54fd6939SJiyong Park #include <arch_helpers.h>
10*54fd6939SJiyong Park #include <common/bl_common.h>
11*54fd6939SJiyong Park #include <common/debug.h>
12*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
13*54fd6939SJiyong Park #include <plat/common/platform.h>
14*54fd6939SJiyong Park
15*54fd6939SJiyong Park #include "opteed_private.h"
16*54fd6939SJiyong Park
17*54fd6939SJiyong Park /*******************************************************************************
18*54fd6939SJiyong Park * The target cpu is being turned on. Allow the OPTEED/OPTEE to perform any
19*54fd6939SJiyong Park * actions needed. Nothing at the moment.
20*54fd6939SJiyong Park ******************************************************************************/
opteed_cpu_on_handler(u_register_t target_cpu)21*54fd6939SJiyong Park static void opteed_cpu_on_handler(u_register_t target_cpu)
22*54fd6939SJiyong Park {
23*54fd6939SJiyong Park }
24*54fd6939SJiyong Park
25*54fd6939SJiyong Park /*******************************************************************************
26*54fd6939SJiyong Park * This cpu is being turned off. Allow the OPTEED/OPTEE to perform any actions
27*54fd6939SJiyong Park * needed
28*54fd6939SJiyong Park ******************************************************************************/
opteed_cpu_off_handler(u_register_t unused)29*54fd6939SJiyong Park static int32_t opteed_cpu_off_handler(u_register_t unused)
30*54fd6939SJiyong Park {
31*54fd6939SJiyong Park int32_t rc = 0;
32*54fd6939SJiyong Park uint32_t linear_id = plat_my_core_pos();
33*54fd6939SJiyong Park optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
34*54fd6939SJiyong Park
35*54fd6939SJiyong Park assert(optee_vector_table);
36*54fd6939SJiyong Park assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
37*54fd6939SJiyong Park
38*54fd6939SJiyong Park /* Program the entry point and enter OPTEE */
39*54fd6939SJiyong Park cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_off_entry);
40*54fd6939SJiyong Park rc = opteed_synchronous_sp_entry(optee_ctx);
41*54fd6939SJiyong Park
42*54fd6939SJiyong Park /*
43*54fd6939SJiyong Park * Read the response from OPTEE. A non-zero return means that
44*54fd6939SJiyong Park * something went wrong while communicating with OPTEE.
45*54fd6939SJiyong Park */
46*54fd6939SJiyong Park if (rc != 0)
47*54fd6939SJiyong Park panic();
48*54fd6939SJiyong Park
49*54fd6939SJiyong Park /*
50*54fd6939SJiyong Park * Reset OPTEE's context for a fresh start when this cpu is turned on
51*54fd6939SJiyong Park * subsequently.
52*54fd6939SJiyong Park */
53*54fd6939SJiyong Park set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF);
54*54fd6939SJiyong Park
55*54fd6939SJiyong Park return 0;
56*54fd6939SJiyong Park }
57*54fd6939SJiyong Park
58*54fd6939SJiyong Park /*******************************************************************************
59*54fd6939SJiyong Park * This cpu is being suspended. S-EL1 state must have been saved in the
60*54fd6939SJiyong Park * resident cpu (mpidr format) if it is a UP/UP migratable OPTEE.
61*54fd6939SJiyong Park ******************************************************************************/
opteed_cpu_suspend_handler(u_register_t max_off_pwrlvl)62*54fd6939SJiyong Park static void opteed_cpu_suspend_handler(u_register_t max_off_pwrlvl)
63*54fd6939SJiyong Park {
64*54fd6939SJiyong Park int32_t rc = 0;
65*54fd6939SJiyong Park uint32_t linear_id = plat_my_core_pos();
66*54fd6939SJiyong Park optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
67*54fd6939SJiyong Park
68*54fd6939SJiyong Park assert(optee_vector_table);
69*54fd6939SJiyong Park assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
70*54fd6939SJiyong Park
71*54fd6939SJiyong Park write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx), CTX_GPREG_X0,
72*54fd6939SJiyong Park max_off_pwrlvl);
73*54fd6939SJiyong Park
74*54fd6939SJiyong Park /* Program the entry point and enter OPTEE */
75*54fd6939SJiyong Park cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_suspend_entry);
76*54fd6939SJiyong Park rc = opteed_synchronous_sp_entry(optee_ctx);
77*54fd6939SJiyong Park
78*54fd6939SJiyong Park /*
79*54fd6939SJiyong Park * Read the response from OPTEE. A non-zero return means that
80*54fd6939SJiyong Park * something went wrong while communicating with OPTEE.
81*54fd6939SJiyong Park */
82*54fd6939SJiyong Park if (rc != 0)
83*54fd6939SJiyong Park panic();
84*54fd6939SJiyong Park
85*54fd6939SJiyong Park /* Update its context to reflect the state OPTEE is in */
86*54fd6939SJiyong Park set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_SUSPEND);
87*54fd6939SJiyong Park }
88*54fd6939SJiyong Park
89*54fd6939SJiyong Park /*******************************************************************************
90*54fd6939SJiyong Park * This cpu has been turned on. Enter OPTEE to initialise S-EL1 and other bits
91*54fd6939SJiyong Park * before passing control back to the Secure Monitor. Entry in S-El1 is done
92*54fd6939SJiyong Park * after initialising minimal architectural state that guarantees safe
93*54fd6939SJiyong Park * execution.
94*54fd6939SJiyong Park ******************************************************************************/
opteed_cpu_on_finish_handler(u_register_t unused)95*54fd6939SJiyong Park static void opteed_cpu_on_finish_handler(u_register_t unused)
96*54fd6939SJiyong Park {
97*54fd6939SJiyong Park int32_t rc = 0;
98*54fd6939SJiyong Park uint32_t linear_id = plat_my_core_pos();
99*54fd6939SJiyong Park optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
100*54fd6939SJiyong Park entry_point_info_t optee_on_entrypoint;
101*54fd6939SJiyong Park
102*54fd6939SJiyong Park assert(optee_vector_table);
103*54fd6939SJiyong Park assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_OFF);
104*54fd6939SJiyong Park
105*54fd6939SJiyong Park opteed_init_optee_ep_state(&optee_on_entrypoint, opteed_rw,
106*54fd6939SJiyong Park (uint64_t)&optee_vector_table->cpu_on_entry,
107*54fd6939SJiyong Park 0, 0, 0, optee_ctx);
108*54fd6939SJiyong Park
109*54fd6939SJiyong Park /* Initialise this cpu's secure context */
110*54fd6939SJiyong Park cm_init_my_context(&optee_on_entrypoint);
111*54fd6939SJiyong Park
112*54fd6939SJiyong Park /* Enter OPTEE */
113*54fd6939SJiyong Park rc = opteed_synchronous_sp_entry(optee_ctx);
114*54fd6939SJiyong Park
115*54fd6939SJiyong Park /*
116*54fd6939SJiyong Park * Read the response from OPTEE. A non-zero return means that
117*54fd6939SJiyong Park * something went wrong while communicating with OPTEE.
118*54fd6939SJiyong Park */
119*54fd6939SJiyong Park if (rc != 0)
120*54fd6939SJiyong Park panic();
121*54fd6939SJiyong Park
122*54fd6939SJiyong Park /* Update its context to reflect the state OPTEE is in */
123*54fd6939SJiyong Park set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
124*54fd6939SJiyong Park }
125*54fd6939SJiyong Park
126*54fd6939SJiyong Park /*******************************************************************************
127*54fd6939SJiyong Park * This cpu has resumed from suspend. The OPTEED saved the OPTEE context when it
128*54fd6939SJiyong Park * completed the preceding suspend call. Use that context to program an entry
129*54fd6939SJiyong Park * into OPTEE to allow it to do any remaining book keeping
130*54fd6939SJiyong Park ******************************************************************************/
opteed_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)131*54fd6939SJiyong Park static void opteed_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
132*54fd6939SJiyong Park {
133*54fd6939SJiyong Park int32_t rc = 0;
134*54fd6939SJiyong Park uint32_t linear_id = plat_my_core_pos();
135*54fd6939SJiyong Park optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
136*54fd6939SJiyong Park
137*54fd6939SJiyong Park assert(optee_vector_table);
138*54fd6939SJiyong Park assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_SUSPEND);
139*54fd6939SJiyong Park
140*54fd6939SJiyong Park /* Program the entry point, max_off_pwrlvl and enter the SP */
141*54fd6939SJiyong Park write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
142*54fd6939SJiyong Park CTX_GPREG_X0,
143*54fd6939SJiyong Park max_off_pwrlvl);
144*54fd6939SJiyong Park cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_resume_entry);
145*54fd6939SJiyong Park rc = opteed_synchronous_sp_entry(optee_ctx);
146*54fd6939SJiyong Park
147*54fd6939SJiyong Park /*
148*54fd6939SJiyong Park * Read the response from OPTEE. A non-zero return means that
149*54fd6939SJiyong Park * something went wrong while communicating with OPTEE.
150*54fd6939SJiyong Park */
151*54fd6939SJiyong Park if (rc != 0)
152*54fd6939SJiyong Park panic();
153*54fd6939SJiyong Park
154*54fd6939SJiyong Park /* Update its context to reflect the state OPTEE is in */
155*54fd6939SJiyong Park set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
156*54fd6939SJiyong Park }
157*54fd6939SJiyong Park
158*54fd6939SJiyong Park /*******************************************************************************
159*54fd6939SJiyong Park * Return the type of OPTEE the OPTEED is dealing with. Report the current
160*54fd6939SJiyong Park * resident cpu (mpidr format) if it is a UP/UP migratable OPTEE.
161*54fd6939SJiyong Park ******************************************************************************/
opteed_cpu_migrate_info(u_register_t * resident_cpu)162*54fd6939SJiyong Park static int32_t opteed_cpu_migrate_info(u_register_t *resident_cpu)
163*54fd6939SJiyong Park {
164*54fd6939SJiyong Park return OPTEE_MIGRATE_INFO;
165*54fd6939SJiyong Park }
166*54fd6939SJiyong Park
167*54fd6939SJiyong Park /*******************************************************************************
168*54fd6939SJiyong Park * System is about to be switched off. Allow the OPTEED/OPTEE to perform
169*54fd6939SJiyong Park * any actions needed.
170*54fd6939SJiyong Park ******************************************************************************/
opteed_system_off(void)171*54fd6939SJiyong Park static void opteed_system_off(void)
172*54fd6939SJiyong Park {
173*54fd6939SJiyong Park uint32_t linear_id = plat_my_core_pos();
174*54fd6939SJiyong Park optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
175*54fd6939SJiyong Park
176*54fd6939SJiyong Park assert(optee_vector_table);
177*54fd6939SJiyong Park assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
178*54fd6939SJiyong Park
179*54fd6939SJiyong Park /* Program the entry point */
180*54fd6939SJiyong Park cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->system_off_entry);
181*54fd6939SJiyong Park
182*54fd6939SJiyong Park /* Enter OPTEE. We do not care about the return value because we
183*54fd6939SJiyong Park * must continue the shutdown anyway */
184*54fd6939SJiyong Park opteed_synchronous_sp_entry(optee_ctx);
185*54fd6939SJiyong Park }
186*54fd6939SJiyong Park
187*54fd6939SJiyong Park /*******************************************************************************
188*54fd6939SJiyong Park * System is about to be reset. Allow the OPTEED/OPTEE to perform
189*54fd6939SJiyong Park * any actions needed.
190*54fd6939SJiyong Park ******************************************************************************/
opteed_system_reset(void)191*54fd6939SJiyong Park static void opteed_system_reset(void)
192*54fd6939SJiyong Park {
193*54fd6939SJiyong Park uint32_t linear_id = plat_my_core_pos();
194*54fd6939SJiyong Park optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
195*54fd6939SJiyong Park
196*54fd6939SJiyong Park assert(optee_vector_table);
197*54fd6939SJiyong Park assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
198*54fd6939SJiyong Park
199*54fd6939SJiyong Park /* Program the entry point */
200*54fd6939SJiyong Park cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->system_reset_entry);
201*54fd6939SJiyong Park
202*54fd6939SJiyong Park /* Enter OPTEE. We do not care about the return value because we
203*54fd6939SJiyong Park * must continue the reset anyway */
204*54fd6939SJiyong Park opteed_synchronous_sp_entry(optee_ctx);
205*54fd6939SJiyong Park }
206*54fd6939SJiyong Park
207*54fd6939SJiyong Park
208*54fd6939SJiyong Park /*******************************************************************************
209*54fd6939SJiyong Park * Structure populated by the OPTEE Dispatcher to be given a chance to
210*54fd6939SJiyong Park * perform any OPTEE bookkeeping before PSCI executes a power mgmt.
211*54fd6939SJiyong Park * operation.
212*54fd6939SJiyong Park ******************************************************************************/
213*54fd6939SJiyong Park const spd_pm_ops_t opteed_pm = {
214*54fd6939SJiyong Park .svc_on = opteed_cpu_on_handler,
215*54fd6939SJiyong Park .svc_off = opteed_cpu_off_handler,
216*54fd6939SJiyong Park .svc_suspend = opteed_cpu_suspend_handler,
217*54fd6939SJiyong Park .svc_on_finish = opteed_cpu_on_finish_handler,
218*54fd6939SJiyong Park .svc_suspend_finish = opteed_cpu_suspend_finish_handler,
219*54fd6939SJiyong Park .svc_migrate = NULL,
220*54fd6939SJiyong Park .svc_migrate_info = opteed_cpu_migrate_info,
221*54fd6939SJiyong Park .svc_system_off = opteed_system_off,
222*54fd6939SJiyong Park .svc_system_reset = opteed_system_reset,
223*54fd6939SJiyong Park };
224