xref: /aosp_15_r20/external/arm-trusted-firmware/services/spd/tlkd/tlkd_pm.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
4*54fd6939SJiyong Park  *
5*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
6*54fd6939SJiyong Park  */
7*54fd6939SJiyong Park 
8*54fd6939SJiyong Park #include <assert.h>
9*54fd6939SJiyong Park 
10*54fd6939SJiyong Park #include <arch_helpers.h>
11*54fd6939SJiyong Park #include <bl32/payloads/tlk.h>
12*54fd6939SJiyong Park #include <common/bl_common.h>
13*54fd6939SJiyong Park #include <common/debug.h>
14*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
15*54fd6939SJiyong Park #include <lib/psci/psci.h>
16*54fd6939SJiyong Park 
17*54fd6939SJiyong Park #include "tlkd_private.h"
18*54fd6939SJiyong Park 
19*54fd6939SJiyong Park extern tlk_context_t tlk_ctx;
20*54fd6939SJiyong Park 
21*54fd6939SJiyong Park #define MPIDR_CPU0	0x80000000
22*54fd6939SJiyong Park 
23*54fd6939SJiyong Park /*******************************************************************************
24*54fd6939SJiyong Park  * Return the type of payload TLKD is dealing with. Report the current
25*54fd6939SJiyong Park  * resident cpu (mpidr format) if it is a UP/UP migratable payload.
26*54fd6939SJiyong Park  ******************************************************************************/
cpu_migrate_info(u_register_t * resident_cpu)27*54fd6939SJiyong Park static int32_t cpu_migrate_info(u_register_t *resident_cpu)
28*54fd6939SJiyong Park {
29*54fd6939SJiyong Park 	/* the payload runs only on CPU0 */
30*54fd6939SJiyong Park 	*resident_cpu = MPIDR_CPU0;
31*54fd6939SJiyong Park 
32*54fd6939SJiyong Park 	/* Uniprocessor, not migrate capable payload */
33*54fd6939SJiyong Park 	return PSCI_TOS_NOT_UP_MIG_CAP;
34*54fd6939SJiyong Park }
35*54fd6939SJiyong Park 
36*54fd6939SJiyong Park /*******************************************************************************
37*54fd6939SJiyong Park  * This cpu is being suspended. Inform TLK of the SYSTEM_SUSPEND event, so
38*54fd6939SJiyong Park  * that it can pass this information to its Trusted Apps.
39*54fd6939SJiyong Park  ******************************************************************************/
cpu_suspend_handler(u_register_t suspend_level)40*54fd6939SJiyong Park static void cpu_suspend_handler(u_register_t suspend_level)
41*54fd6939SJiyong Park {
42*54fd6939SJiyong Park 	gp_regs_t *gp_regs;
43*54fd6939SJiyong Park 	int cpu = read_mpidr() & MPIDR_CPU_MASK;
44*54fd6939SJiyong Park 	int32_t rc = 0;
45*54fd6939SJiyong Park 
46*54fd6939SJiyong Park 	/*
47*54fd6939SJiyong Park 	 * TLK runs only on CPU0 and suspends its Trusted Apps during
48*54fd6939SJiyong Park 	 * SYSTEM_SUSPEND. It has no role to play during CPU_SUSPEND.
49*54fd6939SJiyong Park 	 */
50*54fd6939SJiyong Park 	if ((cpu != 0) || (suspend_level != PLAT_MAX_PWR_LVL))
51*54fd6939SJiyong Park 		return;
52*54fd6939SJiyong Park 
53*54fd6939SJiyong Park 	/* pass system suspend event to TLK */
54*54fd6939SJiyong Park 	gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
55*54fd6939SJiyong Park 	write_ctx_reg(gp_regs, CTX_GPREG_X0, TLK_SYSTEM_SUSPEND);
56*54fd6939SJiyong Park 
57*54fd6939SJiyong Park 	/* Program the entry point and enter TLK */
58*54fd6939SJiyong Park 	rc = tlkd_synchronous_sp_entry(&tlk_ctx);
59*54fd6939SJiyong Park 
60*54fd6939SJiyong Park 	/*
61*54fd6939SJiyong Park 	 * Read the response from TLK. A non-zero return means that
62*54fd6939SJiyong Park 	 * something went wrong while communicating with it.
63*54fd6939SJiyong Park 	 */
64*54fd6939SJiyong Park 	if (rc != 0)
65*54fd6939SJiyong Park 		panic();
66*54fd6939SJiyong Park }
67*54fd6939SJiyong Park 
68*54fd6939SJiyong Park /*******************************************************************************
69*54fd6939SJiyong Park  * This cpu is being resumed. Inform TLK of the SYSTEM_SUSPEND exit, so
70*54fd6939SJiyong Park  * that it can pass this information to its Trusted Apps.
71*54fd6939SJiyong Park  ******************************************************************************/
cpu_resume_handler(u_register_t suspend_level)72*54fd6939SJiyong Park static void cpu_resume_handler(u_register_t suspend_level)
73*54fd6939SJiyong Park {
74*54fd6939SJiyong Park 	gp_regs_t *gp_regs;
75*54fd6939SJiyong Park 	int cpu = read_mpidr() & MPIDR_CPU_MASK;
76*54fd6939SJiyong Park 	int32_t rc = 0;
77*54fd6939SJiyong Park 
78*54fd6939SJiyong Park 	/*
79*54fd6939SJiyong Park 	 * TLK runs only on CPU0 and resumes its Trusted Apps during
80*54fd6939SJiyong Park 	 * SYSTEM_SUSPEND exit. It has no role to play during CPU_SUSPEND
81*54fd6939SJiyong Park 	 * exit.
82*54fd6939SJiyong Park 	 */
83*54fd6939SJiyong Park 	if ((cpu != 0) || (suspend_level != PLAT_MAX_PWR_LVL))
84*54fd6939SJiyong Park 		return;
85*54fd6939SJiyong Park 
86*54fd6939SJiyong Park 	/* pass system resume event to TLK */
87*54fd6939SJiyong Park 	gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
88*54fd6939SJiyong Park 	write_ctx_reg(gp_regs, CTX_GPREG_X0, TLK_SYSTEM_RESUME);
89*54fd6939SJiyong Park 
90*54fd6939SJiyong Park 	/* Program the entry point and enter TLK */
91*54fd6939SJiyong Park 	rc = tlkd_synchronous_sp_entry(&tlk_ctx);
92*54fd6939SJiyong Park 
93*54fd6939SJiyong Park 	/*
94*54fd6939SJiyong Park 	 * Read the response from TLK. A non-zero return means that
95*54fd6939SJiyong Park 	 * something went wrong while communicating with it.
96*54fd6939SJiyong Park 	 */
97*54fd6939SJiyong Park 	if (rc != 0)
98*54fd6939SJiyong Park 		panic();
99*54fd6939SJiyong Park }
100*54fd6939SJiyong Park 
101*54fd6939SJiyong Park /*******************************************************************************
102*54fd6939SJiyong Park  * Structure populated by the Dispatcher to be given a chance to perform any
103*54fd6939SJiyong Park  * bookkeeping before PSCI executes a power mgmt.  operation.
104*54fd6939SJiyong Park  ******************************************************************************/
105*54fd6939SJiyong Park const spd_pm_ops_t tlkd_pm_ops = {
106*54fd6939SJiyong Park 	.svc_migrate_info = cpu_migrate_info,
107*54fd6939SJiyong Park 	.svc_suspend = cpu_suspend_handler,
108*54fd6939SJiyong Park 	.svc_suspend_finish = cpu_resume_handler,
109*54fd6939SJiyong Park };
110