blob: f4df658a7e87eb8c8c8487c602a9dc8eff585206 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Vikram Kanigirifbb13012016-02-15 11:54:14 +00002 * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Achin Gupta4f6ad662013-10-25 09:08:21 +01007#include <arch_helpers.h>
Dan Handley2b6b5742015-03-19 19:17:53 +00008#include <arm_config.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +01009#include <assert.h>
Juan Castillo4dc4a472014-08-12 11:17:06 +010010#include <debug.h>
Dan Handley2b6b5742015-03-19 19:17:53 +000011#include <errno.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010012#include <mmio.h>
13#include <platform.h>
Dan Handley2b6b5742015-03-19 19:17:53 +000014#include <plat_arm.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010015#include <psci.h>
Dan Handley2b6b5742015-03-19 19:17:53 +000016#include <v2m_def.h>
Dan Handley4d2e49d2014-04-11 11:52:12 +010017#include "drivers/pwrc/fvp_pwrc.h"
Dan Handleyed6ff952014-05-14 17:44:19 +010018#include "fvp_def.h"
19#include "fvp_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010020
Dan Handley2b6b5742015-03-19 19:17:53 +000021
Soby Mathew7799cf72015-04-16 14:49:09 +010022#if ARM_RECOM_STATE_ID_ENC
23/*
24 * The table storing the valid idle power states. Ensure that the
25 * array entries are populated in ascending order of state-id to
26 * enable us to use binary search during power state validation.
27 * The table must be terminated by a NULL entry.
28 */
29const unsigned int arm_pm_idle_states[] = {
30 /* State-id - 0x01 */
31 arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET,
32 ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
33 /* State-id - 0x02 */
34 arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
35 ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
36 /* State-id - 0x22 */
37 arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
38 ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
39 0,
40};
41#endif
42
Achin Gupta4f6ad662013-10-25 09:08:21 +010043/*******************************************************************************
Achin Gupta85876392014-07-31 17:45:51 +010044 * Function which implements the common FVP specific operations to power down a
Achin Gupta85876392014-07-31 17:45:51 +010045 * cluster in response to a CPU_OFF or CPU_SUSPEND request.
46 ******************************************************************************/
Sandrine Bailleuxa64a8542015-03-05 10:54:34 +000047static void fvp_cluster_pwrdwn_common(void)
Achin Gupta85876392014-07-31 17:45:51 +010048{
49 uint64_t mpidr = read_mpidr_el1();
50
51 /* Disable coherency if this cluster is to be turned off */
Vikram Kanigirifbb13012016-02-15 11:54:14 +000052 fvp_interconnect_disable();
Achin Gupta85876392014-07-31 17:45:51 +010053
54 /* Program the power controller to turn the cluster off */
55 fvp_pwrc_write_pcoffr(mpidr);
56}
57
Soby Mathew12012dd2015-10-26 14:01:53 +000058static void fvp_power_domain_on_finish_common(const psci_power_state_t *target_state)
59{
60 unsigned long mpidr;
61
62 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
63 ARM_LOCAL_STATE_OFF);
64
65 /* Get the mpidr for this cpu */
66 mpidr = read_mpidr_el1();
67
68 /* Perform the common cluster specific operations */
69 if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
70 ARM_LOCAL_STATE_OFF) {
71 /*
72 * This CPU might have woken up whilst the cluster was
73 * attempting to power down. In this case the FVP power
74 * controller will have a pending cluster power off request
75 * which needs to be cleared by writing to the PPONR register.
76 * This prevents the power controller from interpreting a
77 * subsequent entry of this cpu into a simple wfi as a power
78 * down request.
79 */
80 fvp_pwrc_write_pponr(mpidr);
81
82 /* Enable coherency if this cluster was off */
Vikram Kanigirifbb13012016-02-15 11:54:14 +000083 fvp_interconnect_enable();
Soby Mathew12012dd2015-10-26 14:01:53 +000084 }
85
86 /*
87 * Clear PWKUPR.WEN bit to ensure interrupts do not interfere
88 * with a cpu power down unless the bit is set again
89 */
90 fvp_pwrc_clr_wen(mpidr);
91}
92
93
Achin Gupta85876392014-07-31 17:45:51 +010094/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010095 * FVP handler called when a CPU is about to enter standby.
Vikram Kanigiri3b7c59b2014-03-21 11:57:10 +000096 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010097void fvp_cpu_standby(plat_local_state_t cpu_state)
Vikram Kanigiri3b7c59b2014-03-21 11:57:10 +000098{
Soby Mathewfec4eb72015-07-01 16:16:20 +010099
100 assert(cpu_state == ARM_LOCAL_STATE_RET);
101
Andrew Thoelke42e75a72014-04-28 12:28:39 +0100102 /*
103 * Enter standby state
104 * dsb is good practice before using wfi to enter low power states
105 */
106 dsb();
Vikram Kanigiri3b7c59b2014-03-21 11:57:10 +0000107 wfi();
Vikram Kanigiri3b7c59b2014-03-21 11:57:10 +0000108}
109
110/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100111 * FVP handler called when a power domain is about to be turned on. The
112 * mpidr determines the CPU to be turned on.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100113 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100114int fvp_pwr_domain_on(u_register_t mpidr)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100115{
116 int rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100117 unsigned int psysr;
118
Achin Gupta4f6ad662013-10-25 09:08:21 +0100119 /*
Sandrine Bailleux7175bde2015-12-08 14:18:24 +0000120 * Ensure that we do not cancel an inflight power off request for the
121 * target cpu. That would leave it in a zombie wfi. Wait for it to power
122 * off and then program the power controller to turn that CPU on.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100123 */
124 do {
125 psysr = fvp_pwrc_read_psysr(mpidr);
126 } while (psysr & PSYSR_AFF_L0);
127
Achin Gupta4f6ad662013-10-25 09:08:21 +0100128 fvp_pwrc_write_pponr(mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129 return rc;
130}
131
132/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100133 * FVP handler called when a power domain is about to be turned off. The
134 * target_state encodes the power state that each level should transition to.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100135 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100136void fvp_pwr_domain_off(const psci_power_state_t *target_state)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100137{
Soby Mathewfec4eb72015-07-01 16:16:20 +0100138 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
139 ARM_LOCAL_STATE_OFF);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100140
Achin Gupta85876392014-07-31 17:45:51 +0100141 /*
Soby Mathewfec4eb72015-07-01 16:16:20 +0100142 * If execution reaches this stage then this power domain will be
143 * suspended. Perform at least the cpu specific actions followed
144 * by the cluster specific operations if applicable.
Achin Gupta85876392014-07-31 17:45:51 +0100145 */
Jeenu Viswambharan6ad35482016-12-09 11:14:34 +0000146
147 /* Prevent interrupts from spuriously waking up this cpu */
148 plat_arm_gic_cpuif_disable();
149
150 /* Turn redistributor off */
151 plat_arm_gic_redistif_off();
152
153 /* Program the power controller to power off this cpu. */
154 fvp_pwrc_write_ppoffr(read_mpidr_el1());
Achin Gupta4f6ad662013-10-25 09:08:21 +0100155
Soby Mathewfec4eb72015-07-01 16:16:20 +0100156 if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
157 ARM_LOCAL_STATE_OFF)
Achin Gupta85876392014-07-31 17:45:51 +0100158 fvp_cluster_pwrdwn_common();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100159
Achin Gupta4f6ad662013-10-25 09:08:21 +0100160}
161
162/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100163 * FVP handler called when a power domain is about to be suspended. The
164 * target_state encodes the power state that each level should transition to.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100165 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100166void fvp_pwr_domain_suspend(const psci_power_state_t *target_state)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100167{
Soby Mathewffb4ab12014-09-26 15:08:52 +0100168 unsigned long mpidr;
169
Soby Mathewfec4eb72015-07-01 16:16:20 +0100170 /*
171 * FVP has retention only at cpu level. Just return
172 * as nothing is to be done for retention.
173 */
174 if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
175 ARM_LOCAL_STATE_RET)
Soby Mathew74e52a72014-10-02 16:56:51 +0100176 return;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100177
Soby Mathewfec4eb72015-07-01 16:16:20 +0100178 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
179 ARM_LOCAL_STATE_OFF);
180
Soby Mathewffb4ab12014-09-26 15:08:52 +0100181 /* Get the mpidr for this cpu */
182 mpidr = read_mpidr_el1();
183
Achin Gupta85876392014-07-31 17:45:51 +0100184 /* Program the power controller to enable wakeup interrupts. */
185 fvp_pwrc_set_wen(mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100186
Jeenu Viswambharan6ad35482016-12-09 11:14:34 +0000187 /* Prevent interrupts from spuriously waking up this cpu */
188 plat_arm_gic_cpuif_disable();
189
190 /*
191 * The Redistributor is not powered off as it can potentially prevent
192 * wake up events reaching the CPUIF and/or might lead to losing
193 * register context.
194 */
195
196 /* Program the power controller to power off this cpu. */
197 fvp_pwrc_write_ppoffr(read_mpidr_el1());
Achin Gupta4f6ad662013-10-25 09:08:21 +0100198
Achin Gupta85876392014-07-31 17:45:51 +0100199 /* Perform the common cluster specific operations */
Soby Mathewfec4eb72015-07-01 16:16:20 +0100200 if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
201 ARM_LOCAL_STATE_OFF)
Achin Gupta85876392014-07-31 17:45:51 +0100202 fvp_cluster_pwrdwn_common();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100203}
204
205/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100206 * FVP handler called when a power domain has just been powered on after
207 * being turned off earlier. The target_state encodes the low power state that
208 * each level has woken up from.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100209 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100210void fvp_pwr_domain_on_finish(const psci_power_state_t *target_state)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100211{
Soby Mathew12012dd2015-10-26 14:01:53 +0000212 fvp_power_domain_on_finish_common(target_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100213
Achin Gupta85876392014-07-31 17:45:51 +0100214 /* Enable the gic cpu interface */
Achin Gupta1fa7eb62015-11-03 14:18:34 +0000215 plat_arm_gic_pcpu_init();
216
217 /* Program the gic per-cpu distributor or re-distributor interface */
218 plat_arm_gic_cpuif_enable();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100219}
220
221/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100222 * FVP handler called when a power domain has just been powered on after
223 * having been suspended earlier. The target_state encodes the low power state
224 * that each level has woken up from.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100225 * TODO: At the moment we reuse the on finisher and reinitialize the secure
226 * context. Need to implement a separate suspend finisher.
227 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100228void fvp_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100229{
Soby Mathewfec4eb72015-07-01 16:16:20 +0100230 /*
231 * Nothing to be done on waking up from retention from CPU level.
232 */
233 if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
234 ARM_LOCAL_STATE_RET)
235 return;
236
Soby Mathew12012dd2015-10-26 14:01:53 +0000237 fvp_power_domain_on_finish_common(target_state);
238
239 /* Enable the gic cpu interface */
Achin Gupta1fa7eb62015-11-03 14:18:34 +0000240 plat_arm_gic_cpuif_enable();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100241}
242
Juan Castillo4dc4a472014-08-12 11:17:06 +0100243/*******************************************************************************
244 * FVP handlers to shutdown/reboot the system
245 ******************************************************************************/
246static void __dead2 fvp_system_off(void)
247{
248 /* Write the System Configuration Control Register */
Dan Handley2b6b5742015-03-19 19:17:53 +0000249 mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
250 V2M_CFGCTRL_START |
251 V2M_CFGCTRL_RW |
252 V2M_CFGCTRL_FUNC(V2M_FUNC_SHUTDOWN));
Juan Castillo4dc4a472014-08-12 11:17:06 +0100253 wfi();
254 ERROR("FVP System Off: operation not handled.\n");
255 panic();
256}
257
258static void __dead2 fvp_system_reset(void)
259{
260 /* Write the System Configuration Control Register */
Dan Handley2b6b5742015-03-19 19:17:53 +0000261 mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
262 V2M_CFGCTRL_START |
263 V2M_CFGCTRL_RW |
264 V2M_CFGCTRL_FUNC(V2M_FUNC_REBOOT));
Juan Castillo4dc4a472014-08-12 11:17:06 +0100265 wfi();
266 ERROR("FVP System Reset: operation not handled.\n");
267 panic();
268}
Achin Gupta4f6ad662013-10-25 09:08:21 +0100269
Jeenu Viswambharan095529a2016-08-04 09:43:15 +0100270static int fvp_node_hw_state(u_register_t target_cpu,
271 unsigned int power_level)
272{
273 unsigned int psysr;
274 int ret;
275
276 /*
277 * The format of 'power_level' is implementation-defined, but 0 must
278 * mean a CPU. We also allow 1 to denote the cluster
279 */
280 if (power_level != ARM_PWR_LVL0 && power_level != ARM_PWR_LVL1)
281 return PSCI_E_INVALID_PARAMS;
282
283 /*
284 * Read the status of the given MPDIR from FVP power controller. The
285 * power controller only gives us on/off status, so map that to expected
286 * return values of the PSCI call
287 */
288 psysr = fvp_pwrc_read_psysr(target_cpu);
289 if (psysr == PSYSR_INVALID)
290 return PSCI_E_INVALID_PARAMS;
291
292 switch (power_level) {
293 case ARM_PWR_LVL0:
294 ret = (psysr & PSYSR_AFF_L0) ? HW_ON : HW_OFF;
295 break;
296 case ARM_PWR_LVL1:
297 ret = (psysr & PSYSR_AFF_L1) ? HW_ON : HW_OFF;
298 break;
Jeenu Viswambharan095529a2016-08-04 09:43:15 +0100299 }
300
301 return ret;
302}
303
Achin Gupta4f6ad662013-10-25 09:08:21 +0100304/*******************************************************************************
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100305 * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
306 * platform layer will take care of registering the handlers with PSCI.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100307 ******************************************************************************/
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100308plat_psci_ops_t plat_arm_psci_pm_ops = {
Soby Mathewfec4eb72015-07-01 16:16:20 +0100309 .cpu_standby = fvp_cpu_standby,
310 .pwr_domain_on = fvp_pwr_domain_on,
311 .pwr_domain_off = fvp_pwr_domain_off,
312 .pwr_domain_suspend = fvp_pwr_domain_suspend,
313 .pwr_domain_on_finish = fvp_pwr_domain_on_finish,
314 .pwr_domain_suspend_finish = fvp_pwr_domain_suspend_finish,
Juan Castillo4dc4a472014-08-12 11:17:06 +0100315 .system_off = fvp_system_off,
Soby Mathew74e52a72014-10-02 16:56:51 +0100316 .system_reset = fvp_system_reset,
Soby Mathew0d9e8522015-07-15 13:36:24 +0100317 .validate_power_state = arm_validate_power_state,
Jeenu Viswambharan095529a2016-08-04 09:43:15 +0100318 .validate_ns_entrypoint = arm_validate_ns_entrypoint,
319 .get_node_hw_state = fvp_node_hw_state
Achin Gupta4f6ad662013-10-25 09:08:21 +0100320};