xref: /aosp_15_r20/external/arm-trusted-firmware/plat/qemu/common/qemu_pm.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2015-2019, 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 #include <platform_def.h>
9*54fd6939SJiyong Park 
10*54fd6939SJiyong Park #include <arch_helpers.h>
11*54fd6939SJiyong Park #include <common/debug.h>
12*54fd6939SJiyong Park #include <lib/psci/psci.h>
13*54fd6939SJiyong Park #include <lib/semihosting.h>
14*54fd6939SJiyong Park #include <plat/common/platform.h>
15*54fd6939SJiyong Park #include <drivers/gpio.h>
16*54fd6939SJiyong Park 
17*54fd6939SJiyong Park #include "qemu_private.h"
18*54fd6939SJiyong Park 
19*54fd6939SJiyong Park #define ADP_STOPPED_APPLICATION_EXIT 0x20026
20*54fd6939SJiyong Park 
21*54fd6939SJiyong Park /*
22*54fd6939SJiyong Park  * The secure entry point to be used on warm reset.
23*54fd6939SJiyong Park  */
24*54fd6939SJiyong Park static unsigned long secure_entrypoint;
25*54fd6939SJiyong Park 
26*54fd6939SJiyong Park /* Make composite power state parameter till power level 0 */
27*54fd6939SJiyong Park #if PSCI_EXTENDED_STATE_ID
28*54fd6939SJiyong Park 
29*54fd6939SJiyong Park #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
30*54fd6939SJiyong Park 		(((lvl0_state) << PSTATE_ID_SHIFT) | \
31*54fd6939SJiyong Park 		 ((type) << PSTATE_TYPE_SHIFT))
32*54fd6939SJiyong Park #else
33*54fd6939SJiyong Park #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
34*54fd6939SJiyong Park 		(((lvl0_state) << PSTATE_ID_SHIFT) | \
35*54fd6939SJiyong Park 		 ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
36*54fd6939SJiyong Park 		 ((type) << PSTATE_TYPE_SHIFT))
37*54fd6939SJiyong Park #endif /* PSCI_EXTENDED_STATE_ID */
38*54fd6939SJiyong Park 
39*54fd6939SJiyong Park 
40*54fd6939SJiyong Park #define qemu_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
41*54fd6939SJiyong Park 		(((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
42*54fd6939SJiyong Park 		 qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
43*54fd6939SJiyong Park 
44*54fd6939SJiyong Park 
45*54fd6939SJiyong Park 
46*54fd6939SJiyong Park /*
47*54fd6939SJiyong Park  *  The table storing the valid idle power states. Ensure that the
48*54fd6939SJiyong Park  *  array entries are populated in ascending order of state-id to
49*54fd6939SJiyong Park  *  enable us to use binary search during power state validation.
50*54fd6939SJiyong Park  *  The table must be terminated by a NULL entry.
51*54fd6939SJiyong Park  */
52*54fd6939SJiyong Park static const unsigned int qemu_pm_idle_states[] = {
53*54fd6939SJiyong Park 	/* State-id - 0x01 */
54*54fd6939SJiyong Park 	qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
55*54fd6939SJiyong Park 				MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
56*54fd6939SJiyong Park 	/* State-id - 0x02 */
57*54fd6939SJiyong Park 	qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
58*54fd6939SJiyong Park 				MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
59*54fd6939SJiyong Park 	/* State-id - 0x22 */
60*54fd6939SJiyong Park 	qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF,
61*54fd6939SJiyong Park 				MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
62*54fd6939SJiyong Park 	0,
63*54fd6939SJiyong Park };
64*54fd6939SJiyong Park 
65*54fd6939SJiyong Park /*******************************************************************************
66*54fd6939SJiyong Park  * Platform handler called to check the validity of the power state
67*54fd6939SJiyong Park  * parameter. The power state parameter has to be a composite power state.
68*54fd6939SJiyong Park  ******************************************************************************/
qemu_validate_power_state(unsigned int power_state,psci_power_state_t * req_state)69*54fd6939SJiyong Park static int qemu_validate_power_state(unsigned int power_state,
70*54fd6939SJiyong Park 				psci_power_state_t *req_state)
71*54fd6939SJiyong Park {
72*54fd6939SJiyong Park 	unsigned int state_id;
73*54fd6939SJiyong Park 	int i;
74*54fd6939SJiyong Park 
75*54fd6939SJiyong Park 	assert(req_state);
76*54fd6939SJiyong Park 
77*54fd6939SJiyong Park 	/*
78*54fd6939SJiyong Park 	 *  Currently we are using a linear search for finding the matching
79*54fd6939SJiyong Park 	 *  entry in the idle power state array. This can be made a binary
80*54fd6939SJiyong Park 	 *  search if the number of entries justify the additional complexity.
81*54fd6939SJiyong Park 	 */
82*54fd6939SJiyong Park 	for (i = 0; !!qemu_pm_idle_states[i]; i++) {
83*54fd6939SJiyong Park 		if (power_state == qemu_pm_idle_states[i])
84*54fd6939SJiyong Park 			break;
85*54fd6939SJiyong Park 	}
86*54fd6939SJiyong Park 
87*54fd6939SJiyong Park 	/* Return error if entry not found in the idle state array */
88*54fd6939SJiyong Park 	if (!qemu_pm_idle_states[i])
89*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
90*54fd6939SJiyong Park 
91*54fd6939SJiyong Park 	i = 0;
92*54fd6939SJiyong Park 	state_id = psci_get_pstate_id(power_state);
93*54fd6939SJiyong Park 
94*54fd6939SJiyong Park 	/* Parse the State ID and populate the state info parameter */
95*54fd6939SJiyong Park 	while (state_id) {
96*54fd6939SJiyong Park 		req_state->pwr_domain_state[i++] = state_id &
97*54fd6939SJiyong Park 						PLAT_LOCAL_PSTATE_MASK;
98*54fd6939SJiyong Park 		state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
99*54fd6939SJiyong Park 	}
100*54fd6939SJiyong Park 
101*54fd6939SJiyong Park 	return PSCI_E_SUCCESS;
102*54fd6939SJiyong Park }
103*54fd6939SJiyong Park 
104*54fd6939SJiyong Park /*******************************************************************************
105*54fd6939SJiyong Park  * Platform handler called to check the validity of the non secure
106*54fd6939SJiyong Park  * entrypoint.
107*54fd6939SJiyong Park  ******************************************************************************/
qemu_validate_ns_entrypoint(uintptr_t entrypoint)108*54fd6939SJiyong Park static int qemu_validate_ns_entrypoint(uintptr_t entrypoint)
109*54fd6939SJiyong Park {
110*54fd6939SJiyong Park 	/*
111*54fd6939SJiyong Park 	 * Check if the non secure entrypoint lies within the non
112*54fd6939SJiyong Park 	 * secure DRAM.
113*54fd6939SJiyong Park 	 */
114*54fd6939SJiyong Park 	if ((entrypoint >= NS_DRAM0_BASE) &&
115*54fd6939SJiyong Park 	    (entrypoint < (NS_DRAM0_BASE + NS_DRAM0_SIZE)))
116*54fd6939SJiyong Park 		return PSCI_E_SUCCESS;
117*54fd6939SJiyong Park 	return PSCI_E_INVALID_ADDRESS;
118*54fd6939SJiyong Park }
119*54fd6939SJiyong Park 
120*54fd6939SJiyong Park /*******************************************************************************
121*54fd6939SJiyong Park  * Platform handler called when a CPU is about to enter standby.
122*54fd6939SJiyong Park  ******************************************************************************/
qemu_cpu_standby(plat_local_state_t cpu_state)123*54fd6939SJiyong Park static void qemu_cpu_standby(plat_local_state_t cpu_state)
124*54fd6939SJiyong Park {
125*54fd6939SJiyong Park 
126*54fd6939SJiyong Park 	assert(cpu_state == PLAT_LOCAL_STATE_RET);
127*54fd6939SJiyong Park 
128*54fd6939SJiyong Park 	/*
129*54fd6939SJiyong Park 	 * Enter standby state
130*54fd6939SJiyong Park 	 * dsb is good practice before using wfi to enter low power states
131*54fd6939SJiyong Park 	 */
132*54fd6939SJiyong Park 	dsb();
133*54fd6939SJiyong Park 	wfi();
134*54fd6939SJiyong Park }
135*54fd6939SJiyong Park 
136*54fd6939SJiyong Park /*******************************************************************************
137*54fd6939SJiyong Park  * Platform handler called when a power domain is about to be turned on. The
138*54fd6939SJiyong Park  * mpidr determines the CPU to be turned on.
139*54fd6939SJiyong Park  ******************************************************************************/
qemu_pwr_domain_on(u_register_t mpidr)140*54fd6939SJiyong Park static int qemu_pwr_domain_on(u_register_t mpidr)
141*54fd6939SJiyong Park {
142*54fd6939SJiyong Park 	int rc = PSCI_E_SUCCESS;
143*54fd6939SJiyong Park 	unsigned pos = plat_core_pos_by_mpidr(mpidr);
144*54fd6939SJiyong Park 	uint64_t *hold_base = (uint64_t *)PLAT_QEMU_HOLD_BASE;
145*54fd6939SJiyong Park 
146*54fd6939SJiyong Park 	hold_base[pos] = PLAT_QEMU_HOLD_STATE_GO;
147*54fd6939SJiyong Park 	sev();
148*54fd6939SJiyong Park 
149*54fd6939SJiyong Park 	return rc;
150*54fd6939SJiyong Park }
151*54fd6939SJiyong Park 
152*54fd6939SJiyong Park /*******************************************************************************
153*54fd6939SJiyong Park  * Platform handler called when a power domain is about to be turned off. The
154*54fd6939SJiyong Park  * target_state encodes the power state that each level should transition to.
155*54fd6939SJiyong Park  ******************************************************************************/
qemu_pwr_domain_off(const psci_power_state_t * target_state)156*54fd6939SJiyong Park static void qemu_pwr_domain_off(const psci_power_state_t *target_state)
157*54fd6939SJiyong Park {
158*54fd6939SJiyong Park 	qemu_pwr_gic_off();
159*54fd6939SJiyong Park }
160*54fd6939SJiyong Park 
161*54fd6939SJiyong Park void __dead2 plat_secondary_cold_boot_setup(void);
162*54fd6939SJiyong Park 
163*54fd6939SJiyong Park static void __dead2
qemu_pwr_domain_pwr_down_wfi(const psci_power_state_t * target_state)164*54fd6939SJiyong Park qemu_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state)
165*54fd6939SJiyong Park {
166*54fd6939SJiyong Park 	disable_mmu_el3();
167*54fd6939SJiyong Park 	plat_secondary_cold_boot_setup();
168*54fd6939SJiyong Park }
169*54fd6939SJiyong Park 
170*54fd6939SJiyong Park /*******************************************************************************
171*54fd6939SJiyong Park  * Platform handler called when a power domain is about to be suspended. The
172*54fd6939SJiyong Park  * target_state encodes the power state that each level should transition to.
173*54fd6939SJiyong Park  ******************************************************************************/
qemu_pwr_domain_suspend(const psci_power_state_t * target_state)174*54fd6939SJiyong Park void qemu_pwr_domain_suspend(const psci_power_state_t *target_state)
175*54fd6939SJiyong Park {
176*54fd6939SJiyong Park 	assert(0);
177*54fd6939SJiyong Park }
178*54fd6939SJiyong Park 
179*54fd6939SJiyong Park /*******************************************************************************
180*54fd6939SJiyong Park  * Platform handler called when a power domain has just been powered on after
181*54fd6939SJiyong Park  * being turned off earlier. The target_state encodes the low power state that
182*54fd6939SJiyong Park  * each level has woken up from.
183*54fd6939SJiyong Park  ******************************************************************************/
qemu_pwr_domain_on_finish(const psci_power_state_t * target_state)184*54fd6939SJiyong Park void qemu_pwr_domain_on_finish(const psci_power_state_t *target_state)
185*54fd6939SJiyong Park {
186*54fd6939SJiyong Park 	assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
187*54fd6939SJiyong Park 					PLAT_LOCAL_STATE_OFF);
188*54fd6939SJiyong Park 
189*54fd6939SJiyong Park 	qemu_pwr_gic_on_finish();
190*54fd6939SJiyong Park }
191*54fd6939SJiyong Park 
192*54fd6939SJiyong Park /*******************************************************************************
193*54fd6939SJiyong Park  * Platform handler called when a power domain has just been powered on after
194*54fd6939SJiyong Park  * having been suspended earlier. The target_state encodes the low power state
195*54fd6939SJiyong Park  * that each level has woken up from.
196*54fd6939SJiyong Park  ******************************************************************************/
qemu_pwr_domain_suspend_finish(const psci_power_state_t * target_state)197*54fd6939SJiyong Park void qemu_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
198*54fd6939SJiyong Park {
199*54fd6939SJiyong Park 	assert(0);
200*54fd6939SJiyong Park }
201*54fd6939SJiyong Park 
202*54fd6939SJiyong Park /*******************************************************************************
203*54fd6939SJiyong Park  * Platform handlers to shutdown/reboot the system
204*54fd6939SJiyong Park  ******************************************************************************/
205*54fd6939SJiyong Park 
qemu_system_off(void)206*54fd6939SJiyong Park static void __dead2 qemu_system_off(void)
207*54fd6939SJiyong Park {
208*54fd6939SJiyong Park #ifdef SECURE_GPIO_BASE
209*54fd6939SJiyong Park 	ERROR("QEMU System Power off: with GPIO.\n");
210*54fd6939SJiyong Park 	gpio_set_direction(SECURE_GPIO_POWEROFF, GPIO_DIR_OUT);
211*54fd6939SJiyong Park 	gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_LOW);
212*54fd6939SJiyong Park 	gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_HIGH);
213*54fd6939SJiyong Park #else
214*54fd6939SJiyong Park 	semihosting_exit(ADP_STOPPED_APPLICATION_EXIT, 0);
215*54fd6939SJiyong Park 	ERROR("QEMU System Off: semihosting call unexpectedly returned.\n");
216*54fd6939SJiyong Park #endif
217*54fd6939SJiyong Park 	panic();
218*54fd6939SJiyong Park }
219*54fd6939SJiyong Park 
qemu_system_reset(void)220*54fd6939SJiyong Park static void __dead2 qemu_system_reset(void)
221*54fd6939SJiyong Park {
222*54fd6939SJiyong Park 	ERROR("QEMU System Reset: with GPIO.\n");
223*54fd6939SJiyong Park #ifdef SECURE_GPIO_BASE
224*54fd6939SJiyong Park 	gpio_set_direction(SECURE_GPIO_RESET, GPIO_DIR_OUT);
225*54fd6939SJiyong Park 	gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_LOW);
226*54fd6939SJiyong Park 	gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_HIGH);
227*54fd6939SJiyong Park #else
228*54fd6939SJiyong Park 	ERROR("QEMU System Reset: operation not handled.\n");
229*54fd6939SJiyong Park #endif
230*54fd6939SJiyong Park 	panic();
231*54fd6939SJiyong Park }
232*54fd6939SJiyong Park 
233*54fd6939SJiyong Park static const plat_psci_ops_t plat_qemu_psci_pm_ops = {
234*54fd6939SJiyong Park 	.cpu_standby = qemu_cpu_standby,
235*54fd6939SJiyong Park 	.pwr_domain_on = qemu_pwr_domain_on,
236*54fd6939SJiyong Park 	.pwr_domain_off = qemu_pwr_domain_off,
237*54fd6939SJiyong Park 	.pwr_domain_pwr_down_wfi = qemu_pwr_domain_pwr_down_wfi,
238*54fd6939SJiyong Park 	.pwr_domain_suspend = qemu_pwr_domain_suspend,
239*54fd6939SJiyong Park 	.pwr_domain_on_finish = qemu_pwr_domain_on_finish,
240*54fd6939SJiyong Park 	.pwr_domain_suspend_finish = qemu_pwr_domain_suspend_finish,
241*54fd6939SJiyong Park 	.system_off = qemu_system_off,
242*54fd6939SJiyong Park 	.system_reset = qemu_system_reset,
243*54fd6939SJiyong Park 	.validate_power_state = qemu_validate_power_state,
244*54fd6939SJiyong Park 	.validate_ns_entrypoint = qemu_validate_ns_entrypoint
245*54fd6939SJiyong Park };
246*54fd6939SJiyong Park 
plat_setup_psci_ops(uintptr_t sec_entrypoint,const plat_psci_ops_t ** psci_ops)247*54fd6939SJiyong Park int plat_setup_psci_ops(uintptr_t sec_entrypoint,
248*54fd6939SJiyong Park 			const plat_psci_ops_t **psci_ops)
249*54fd6939SJiyong Park {
250*54fd6939SJiyong Park 	uintptr_t *mailbox = (void *) PLAT_QEMU_TRUSTED_MAILBOX_BASE;
251*54fd6939SJiyong Park 
252*54fd6939SJiyong Park 	*mailbox = sec_entrypoint;
253*54fd6939SJiyong Park 	secure_entrypoint = (unsigned long) sec_entrypoint;
254*54fd6939SJiyong Park 	*psci_ops = &plat_qemu_psci_pm_ops;
255*54fd6939SJiyong Park 
256*54fd6939SJiyong Park 	return 0;
257*54fd6939SJiyong Park }
258