xref: /rk3399_ARM-atf/plat/arm/board/fvp/fvp_pm.c (revision d87d5cd969246c9ea1e5627996749a4f401166b0)
1 /*
2  * Copyright (c) 2013-2026, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <arch_helpers.h>
10 #include <common/debug.h>
11 #include <drivers/arm/gicv3.h>
12 #include <drivers/arm/fvp/fvp_pwrc.h>
13 #include <lib/mmio.h>
14 #include <lib/psci/psci.h>
15 #include <plat/arm/common/arm_config.h>
16 #include <plat/arm/common/plat_arm.h>
17 #include <platform_def.h>
18 
19 #include "fvp_private.h"
20 #include "../drivers/arm/gic/v3/gicv3_private.h"
21 
22 
23 #if ARM_RECOM_STATE_ID_ENC
24 /*
25  *  The table storing the valid idle power states. Ensure that the
26  *  array entries are populated in ascending order of state-id to
27  *  enable us to use binary search during power state validation.
28  *  The table must be terminated by a NULL entry.
29  */
30 const unsigned int arm_pm_idle_states[] = {
31 	/* State-id - 0x01 */
32 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET,
33 			ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
34 	/* State-id - 0x02 */
35 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
36 			ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
37 	/* State-id - 0x22 */
38 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
39 			ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
40 	/* State-id - 0x222 */
41 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
42 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
43 	0,
44 };
45 #endif
46 
47 /*******************************************************************************
48  * Function which implements the common FVP specific operations to power down a
49  * cluster in response to a CPU_OFF or CPU_SUSPEND request.
50  ******************************************************************************/
fvp_cluster_pwrdwn_common(void)51 static void fvp_cluster_pwrdwn_common(void)
52 {
53 	uint64_t mpidr = read_mpidr_el1();
54 
55 	/* Disable coherency if this cluster is to be turned off */
56 	fvp_interconnect_disable();
57 
58 	/* Program the power controller to turn the cluster off */
59 	fvp_pwrc_write_pcoffr(mpidr);
60 }
61 
62 /*
63  * Empty implementation of these hooks avoid setting the GICR_WAKER.Sleep bit
64  * on ARM GICv3 implementations on FVP. This is required, because FVP does not
65  * support SYSTEM_SUSPEND and it is `faked` in firmware. Hence, for wake up
66  * from `fake` system suspend the GIC must not be powered off.
67  */
arm_gicv3_distif_pre_save(unsigned int rdist_proc_num)68 void arm_gicv3_distif_pre_save(unsigned int rdist_proc_num)
69 {}
70 
arm_gicv3_distif_post_restore(unsigned int rdist_proc_num)71 void arm_gicv3_distif_post_restore(unsigned int rdist_proc_num)
72 {}
73 
plat_gic_pre_pcpu_init(unsigned int cpu_idx)74 void plat_gic_pre_pcpu_init(unsigned int cpu_idx)
75 {
76 #if USE_GIC_DRIVER == 3
77 	fvp_gicv3_make_rdistrif_rw(cpu_idx);
78 #endif
79 }
80 
fvp_power_domain_on_finish_common(const psci_power_state_t * target_state)81 static void fvp_power_domain_on_finish_common(const psci_power_state_t *target_state)
82 {
83 	unsigned long mpidr;
84 
85 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
86 					ARM_LOCAL_STATE_OFF);
87 
88 	/* Get the mpidr for this cpu */
89 	mpidr = read_mpidr_el1();
90 
91 	/* Perform the common cluster specific operations */
92 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
93 					ARM_LOCAL_STATE_OFF) {
94 		/*
95 		 * This CPU might have woken up whilst the cluster was
96 		 * attempting to power down. In this case the FVP power
97 		 * controller will have a pending cluster power off request
98 		 * which needs to be cleared by writing to the PPONR register.
99 		 * This prevents the power controller from interpreting a
100 		 * subsequent entry of this cpu into a simple wfi as a power
101 		 * down request.
102 		 */
103 		fvp_pwrc_write_pponr(mpidr);
104 
105 		/* Enable coherency if this cluster was off */
106 		fvp_interconnect_enable();
107 	}
108 	/* Perform the common system specific operations */
109 	if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
110 						ARM_LOCAL_STATE_OFF)
111 		arm_system_pwr_domain_resume();
112 
113 	/*
114 	 * Clear PWKUPR.WEN bit to ensure interrupts do not interfere
115 	 * with a cpu power down unless the bit is set again
116 	 */
117 	fvp_pwrc_clr_wen(mpidr);
118 }
119 
120 /*******************************************************************************
121  * FVP handler called when a CPU is about to enter standby.
122  ******************************************************************************/
fvp_cpu_standby(plat_local_state_t cpu_state)123 static void fvp_cpu_standby(plat_local_state_t cpu_state)
124 {
125 	u_register_t scr = read_scr_el3();
126 
127 	assert(cpu_state == ARM_LOCAL_STATE_RET);
128 
129 	/*
130 	 * Enable the Non-secure interrupt to wake the CPU.
131 	 * In GICv3 affinity routing mode, the Non-secure Group 1 interrupts
132 	 * use Physical FIQ at EL3 whereas in GICv2, Physical IRQ is used.
133 	 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
134 	 * routing mode.
135 	 */
136 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
137 	isb();
138 
139 	/*
140 	 * Enter standby state.
141 	 * dsb is good practice before using wfi to enter low power states.
142 	 */
143 	dsb();
144 	wfi();
145 
146 	/*
147 	 * Restore SCR_EL3 to the original value, synchronisation of SCR_EL3
148 	 * is done by eret in el3_exit() to save some execution cycles.
149 	 */
150 	write_scr_el3(scr);
151 }
152 
153 /*******************************************************************************
154  * FVP handler called when a power domain is about to be turned on. The
155  * mpidr determines the CPU to be turned on.
156  ******************************************************************************/
fvp_pwr_domain_on(u_register_t mpidr)157 static int fvp_pwr_domain_on(u_register_t mpidr)
158 {
159 	int rc = PSCI_E_SUCCESS;
160 	unsigned int psysr;
161 
162 	/*
163 	 * Ensure that we do not cancel an inflight power off request for the
164 	 * target cpu. That would leave it in a zombie wfi. Wait for it to power
165 	 * off and then program the power controller to turn that CPU on.
166 	 */
167 	do {
168 		psysr = fvp_pwrc_read_psysr(mpidr);
169 	} while ((psysr & PSYSR_AFF_L0) != 0U);
170 
171 	fvp_pwrc_write_pponr(mpidr);
172 	return rc;
173 }
174 
175 /*******************************************************************************
176  * FVP handler called when a power domain is about to be turned off. The
177  * target_state encodes the power state that each level should transition to.
178  ******************************************************************************/
fvp_pwr_domain_off(const psci_power_state_t * target_state)179 static void fvp_pwr_domain_off(const psci_power_state_t *target_state)
180 {
181 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
182 					ARM_LOCAL_STATE_OFF);
183 
184 	/*
185 	 * If execution reaches this stage then this power domain will be
186 	 * suspended. Perform at least the cpu specific actions followed
187 	 * by the cluster specific operations if applicable.
188 	 */
189 
190 	/* Program the power controller to power off this cpu. */
191 	fvp_pwrc_write_ppoffr(read_mpidr_el1());
192 
193 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
194 					ARM_LOCAL_STATE_OFF)
195 		fvp_cluster_pwrdwn_common();
196 
197 }
198 
199 /*******************************************************************************
200  * FVP handler called when a power domain is about to be suspended. The
201  * target_state encodes the power state that each level should transition to.
202  ******************************************************************************/
fvp_pwr_domain_suspend(const psci_power_state_t * target_state)203 static void fvp_pwr_domain_suspend(const psci_power_state_t *target_state)
204 {
205 	unsigned long mpidr;
206 
207 	/*
208 	 * FVP has retention only at cpu level. Just return
209 	 * as nothing is to be done for retention.
210 	 */
211 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
212 					ARM_LOCAL_STATE_RET)
213 		return;
214 
215 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
216 					ARM_LOCAL_STATE_OFF);
217 
218 	/* Get the mpidr for this cpu */
219 	mpidr = read_mpidr_el1();
220 
221 	/* Program the power controller to enable wakeup interrupts. */
222 	fvp_pwrc_set_wen(mpidr);
223 
224 	/*
225 	 * The Redistributor is not powered off as it can potentially prevent
226 	 * wake up events reaching the CPUIF and/or might lead to losing
227 	 * register context.
228 	 */
229 
230 	/* Perform the common cluster specific operations */
231 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
232 					ARM_LOCAL_STATE_OFF)
233 		fvp_cluster_pwrdwn_common();
234 
235 	/* Perform the common system specific operations */
236 	if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
237 						ARM_LOCAL_STATE_OFF)
238 		arm_system_pwr_domain_save();
239 
240 	/* Program the power controller to power off this cpu. */
241 	fvp_pwrc_write_ppoffr(read_mpidr_el1());
242 
243 	return;
244 }
245 
246 /*******************************************************************************
247  * FVP handler called when a power domain has just been powered on after
248  * being turned off earlier. The target_state encodes the low power state that
249  * each level has woken up from.
250  ******************************************************************************/
fvp_pwr_domain_on_finish(const psci_power_state_t * target_state)251 static void fvp_pwr_domain_on_finish(const psci_power_state_t *target_state)
252 {
253 	fvp_power_domain_on_finish_common(target_state);
254 }
255 
256 /*******************************************************************************
257  * FVP handler called when a power domain has just been powered on after
258  * having been suspended earlier. The target_state encodes the low power state
259  * that each level has woken up from.
260  * TODO: At the moment we reuse the on finisher and reinitialize the secure
261  * context. Need to implement a separate suspend finisher.
262  ******************************************************************************/
fvp_pwr_domain_suspend_finish(const psci_power_state_t * target_state)263 static void fvp_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
264 {
265 	/*
266 	 * Nothing to be done on waking up from retention from CPU level.
267 	 */
268 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
269 					ARM_LOCAL_STATE_RET)
270 		return;
271 
272 	fvp_power_domain_on_finish_common(target_state);
273 }
274 
275 /*******************************************************************************
276  * FVP handlers to shutdown/reboot the system
277  ******************************************************************************/
fvp_system_off(void)278 static void fvp_system_off(void)
279 {
280 	/* Write the System Configuration Control Register */
281 	mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
282 		V2M_CFGCTRL_START |
283 		V2M_CFGCTRL_RW |
284 		V2M_CFGCTRL_FUNC(V2M_FUNC_SHUTDOWN));
285 }
286 
fvp_system_reset(void)287 static void fvp_system_reset(void)
288 {
289 	/* Write the System Configuration Control Register */
290 	mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
291 		V2M_CFGCTRL_START |
292 		V2M_CFGCTRL_RW |
293 		V2M_CFGCTRL_FUNC(V2M_FUNC_REBOOT));
294 }
295 
fvp_node_hw_state(u_register_t target_cpu,unsigned int power_level)296 static int fvp_node_hw_state(u_register_t target_cpu,
297 			     unsigned int power_level)
298 {
299 	unsigned int psysr;
300 	int ret = 0;
301 
302 	/*
303 	 * The format of 'power_level' is implementation-defined, but 0 must
304 	 * mean a CPU. We also allow 1 to denote the cluster
305 	 */
306 	if (power_level > ARM_PWR_LVL1) {
307 		return PSCI_E_INVALID_PARAMS;
308 	}
309 
310 	/*
311 	 * Read the status of the given MPDIR from FVP power controller. The
312 	 * power controller only gives us on/off status, so map that to expected
313 	 * return values of the PSCI call
314 	 */
315 	psysr = fvp_pwrc_read_psysr(target_cpu);
316 	if (psysr == PSYSR_INVALID)
317 		return PSCI_E_INVALID_PARAMS;
318 
319 	if (power_level == ARM_PWR_LVL0) {
320 		ret = ((psysr & PSYSR_AFF_L0) != 0U) ? HW_ON : HW_OFF;
321 	} else if (power_level == ARM_PWR_LVL1) {
322 	/*
323 	 * Use L1 affinity if MPIDR_EL1.MT bit is not set else use L2 affinity.
324 	 */
325 		if ((read_mpidr_el1() & MPIDR_MT_MASK) == 0U)
326 			ret = ((psysr & PSYSR_AFF_L1) != 0U) ? HW_ON : HW_OFF;
327 		else
328 			ret = ((psysr & PSYSR_AFF_L2) != 0U) ? HW_ON : HW_OFF;
329 	}
330 
331 	return ret;
332 }
333 
334 /*
335  * The FVP doesn't truly support power management at SYSTEM power domain. The
336  * SYSTEM_SUSPEND will be down-graded to the cluster level within the platform
337  * layer. The `fake` SYSTEM_SUSPEND allows us to validate some of the driver
338  * save and restore sequences on FVP.
339  */
340 #if !ARM_BL31_IN_DRAM
fvp_get_sys_suspend_power_state(psci_power_state_t * req_state)341 static void fvp_get_sys_suspend_power_state(psci_power_state_t *req_state)
342 {
343 	unsigned int i;
344 
345 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
346 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
347 
348 #if PSCI_OS_INIT_MODE
349 	req_state->last_at_pwrlvl = PLAT_MAX_PWR_LVL;
350 #endif
351 }
352 #endif
353 
354 /*******************************************************************************
355  * Handler to filter PSCI requests.
356  ******************************************************************************/
357 /*
358  * The system power domain suspend is only supported only via
359  * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
360  * will be downgraded to the lower level.
361  */
fvp_validate_power_state(unsigned int power_state,psci_power_state_t * req_state)362 static int fvp_validate_power_state(unsigned int power_state,
363 			    psci_power_state_t *req_state)
364 {
365 	int rc;
366 	rc = arm_validate_power_state(power_state, req_state);
367 
368 	/*
369 	 * Ensure that the system power domain level is never suspended
370 	 * via PSCI CPU SUSPEND API. Currently system suspend is only
371 	 * supported via PSCI SYSTEM SUSPEND API.
372 	 */
373 	req_state->pwr_domain_state[ARM_PWR_LVL2] = ARM_LOCAL_STATE_RUN;
374 	return rc;
375 }
376 
377 /*
378  * Custom `translate_power_state_by_mpidr` handler for FVP. Unlike in the
379  * `fvp_validate_power_state`, we do not downgrade the system power
380  * domain level request in `power_state` as it will be used to query the
381  * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
382  */
fvp_translate_power_state_by_mpidr(u_register_t mpidr,unsigned int power_state,psci_power_state_t * output_state)383 static int fvp_translate_power_state_by_mpidr(u_register_t mpidr,
384 		unsigned int power_state,
385 		psci_power_state_t *output_state)
386 {
387 	return arm_validate_power_state(power_state, output_state);
388 }
389 
390 /*******************************************************************************
391  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
392  * platform layer will take care of registering the handlers with PSCI.
393  ******************************************************************************/
394 plat_psci_ops_t plat_arm_psci_pm_ops = {
395 	.cpu_standby = fvp_cpu_standby,
396 	.pwr_domain_on = fvp_pwr_domain_on,
397 	.pwr_domain_off = fvp_pwr_domain_off,
398 	.pwr_domain_suspend = fvp_pwr_domain_suspend,
399 	.pwr_domain_on_finish = fvp_pwr_domain_on_finish,
400 	.pwr_domain_suspend_finish = fvp_pwr_domain_suspend_finish,
401 	.system_off = fvp_system_off,
402 	.system_reset = fvp_system_reset,
403 	.validate_power_state = fvp_validate_power_state,
404 	.validate_ns_entrypoint = arm_validate_psci_entrypoint,
405 	.translate_power_state_by_mpidr = fvp_translate_power_state_by_mpidr,
406 	.get_node_hw_state = fvp_node_hw_state,
407 #if !ARM_BL31_IN_DRAM
408 	/*
409 	 * The TrustZone Controller is set up during the warmboot sequence after
410 	 * resuming the CPU from a SYSTEM_SUSPEND. If BL31 is located in SRAM
411 	 * this is  not a problem but, if it is in TZC-secured DRAM, it tries to
412 	 * reconfigure the same memory it is running on, causing an exception.
413 	 */
414 	.get_sys_suspend_power_state = fvp_get_sys_suspend_power_state,
415 #endif
416 	.mem_protect_chk	= arm_psci_mem_protect_chk,
417 	.read_mem_protect	= arm_psci_read_mem_protect,
418 	.write_mem_protect	= arm_nor_psci_write_mem_protect,
419 };
420 
plat_arm_psci_override_pm_ops(plat_psci_ops_t * ops)421 const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
422 {
423 	return ops;
424 }
425