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