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