blob: 47412dfa4277f1ce4bfd1af05b92fbaa1fae74d2 [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Roberto Vargas550eb082018-01-05 16:00:05 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handley9df48042015-03-19 18:58:55 +00005 */
6
Soby Mathewfeac8fc2015-09-29 15:47:16 +01007#include <assert.h>
Dan Handley9df48042015-03-19 18:58:55 +00008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Dan Handley9df48042015-03-19 18:58:55 +000010#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <arch_helpers.h>
13#include <common/debug.h>
14#include <lib/cassert.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000015#include <plat/arm/common/plat_arm.h>
16#include <plat/arm/css/common/css_pm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <plat/common/platform.h>
18
Soby Mathew200fffd2016-10-21 11:34:59 +010019#include "../drivers/scp/css_scp.h"
Soby Mathew12012dd2015-10-26 14:01:53 +000020
Soby Mathewfeac8fc2015-09-29 15:47:16 +010021/* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
22#pragma weak plat_arm_psci_pm_ops
Soby Mathewfec4eb72015-07-01 16:16:20 +010023
Soby Mathew7799cf72015-04-16 14:49:09 +010024#if ARM_RECOM_STATE_ID_ENC
25/*
26 * The table storing the valid idle power states. Ensure that the
27 * array entries are populated in ascending order of state-id to
28 * enable us to use binary search during power state validation.
29 * The table must be terminated by a NULL entry.
30 */
31const unsigned int arm_pm_idle_states[] = {
Soby Mathewa869de12015-05-08 10:18:59 +010032 /* State-id - 0x001 */
33 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
34 ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
35 /* State-id - 0x002 */
36 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
37 ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
38 /* State-id - 0x022 */
39 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
40 ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
41#if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
42 /* State-id - 0x222 */
43 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
44 ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
45#endif
Soby Mathew7799cf72015-04-16 14:49:09 +010046 0,
47};
Soby Mathewa869de12015-05-08 10:18:59 +010048#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathew7799cf72015-04-16 14:49:09 +010049
Soby Mathew61e8d0b2015-10-12 17:32:29 +010050/*
51 * All the power management helpers in this file assume at least cluster power
52 * level is supported.
53 */
54CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
55 assert_max_pwr_lvl_supported_mismatch);
56
Soby Mathew7a3b5eb2016-12-09 15:23:08 +000057/*
58 * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL
59 * assumed by the CSS layer.
60 */
61CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL,
62 assert_max_pwr_lvl_higher_than_css_sys_lvl);
63
Dan Handley9df48042015-03-19 18:58:55 +000064/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010065 * Handler called when a power domain is about to be turned on. The
Dan Handley9df48042015-03-19 18:58:55 +000066 * level and mpidr determine the affinity instance.
67 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010068int css_pwr_domain_on(u_register_t mpidr)
Dan Handley9df48042015-03-19 18:58:55 +000069{
Soby Mathew200fffd2016-10-21 11:34:59 +010070 css_scp_on(mpidr);
Dan Handley9df48042015-03-19 18:58:55 +000071
72 return PSCI_E_SUCCESS;
73}
74
Soby Mathew12012dd2015-10-26 14:01:53 +000075static void css_pwr_domain_on_finisher_common(
76 const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +000077{
Soby Mathew12012dd2015-10-26 14:01:53 +000078 assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
Soby Mathew61e8d0b2015-10-12 17:32:29 +010079
Soby Mathew9ca28062017-10-11 16:08:58 +010080 /* Enable the gic cpu interface */
81 plat_arm_gic_cpuif_enable();
82
Dan Handley9df48042015-03-19 18:58:55 +000083 /*
84 * Perform the common cluster specific operations i.e enable coherency
85 * if this cluster was off.
86 */
Soby Mathew12012dd2015-10-26 14:01:53 +000087 if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
Vikram Kanigirifbb13012016-02-15 11:54:14 +000088 plat_arm_interconnect_enter_coherency();
Soby Mathew12012dd2015-10-26 14:01:53 +000089}
Dan Handley9df48042015-03-19 18:58:55 +000090
Soby Mathew12012dd2015-10-26 14:01:53 +000091/*******************************************************************************
92 * Handler called when a power level has just been powered on after
93 * being turned off earlier. The target_state encodes the low power state that
94 * each level has woken up from. This handler would never be invoked with
95 * the system power domain uninitialized as either the primary would have taken
96 * care of it as part of cold boot or the first core awakened from system
97 * suspend would have already initialized it.
98 ******************************************************************************/
99void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
100{
101 /* Assert that the system power domain need not be initialized */
Nariman Poushincd956262018-05-01 09:28:40 +0100102 assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100103
Achin Gupta1fa7eb62015-11-03 14:18:34 +0000104 /* Program the gic per-cpu distributor or re-distributor interface */
105 plat_arm_gic_pcpu_init();
106
Soby Mathew9ca28062017-10-11 16:08:58 +0100107 css_pwr_domain_on_finisher_common(target_state);
Dan Handley9df48042015-03-19 18:58:55 +0000108}
109
110/*******************************************************************************
111 * Common function called while turning a cpu off or suspending it. It is called
112 * from css_off() or css_suspend() when these functions in turn are called for
Soby Mathewfec4eb72015-07-01 16:16:20 +0100113 * power domain at the highest power level which will be powered down. It
114 * performs the actions common to the OFF and SUSPEND calls.
Dan Handley9df48042015-03-19 18:58:55 +0000115 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100116static void css_power_down_common(const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000117{
Dan Handley9df48042015-03-19 18:58:55 +0000118 /* Prevent interrupts from spuriously waking up this cpu */
Achin Gupta1fa7eb62015-11-03 14:18:34 +0000119 plat_arm_gic_cpuif_disable();
Dan Handley9df48042015-03-19 18:58:55 +0000120
121 /* Cluster is to be turned off, so disable coherency */
Soby Mathew200fffd2016-10-21 11:34:59 +0100122 if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
Vikram Kanigirifbb13012016-02-15 11:54:14 +0000123 plat_arm_interconnect_exit_coherency();
Dan Handley9df48042015-03-19 18:58:55 +0000124}
125
126/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100127 * Handler called when a power domain is about to be turned off. The
128 * target_state encodes the power state that each level should transition to.
Dan Handley9df48042015-03-19 18:58:55 +0000129 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100130void css_pwr_domain_off(const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000131{
Soby Mathew12012dd2015-10-26 14:01:53 +0000132 assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
Soby Mathewfec4eb72015-07-01 16:16:20 +0100133 css_power_down_common(target_state);
Soby Mathew200fffd2016-10-21 11:34:59 +0100134 css_scp_off(target_state);
Dan Handley9df48042015-03-19 18:58:55 +0000135}
136
137/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100138 * Handler called when a power domain is about to be suspended. The
139 * target_state encodes the power state that each level should transition to.
Dan Handley9df48042015-03-19 18:58:55 +0000140 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100141void css_pwr_domain_suspend(const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000142{
Soby Mathewfec4eb72015-07-01 16:16:20 +0100143 /*
Soby Mathew12012dd2015-10-26 14:01:53 +0000144 * CSS currently supports retention only at cpu level. Just return
Soby Mathewfec4eb72015-07-01 16:16:20 +0100145 * as nothing is to be done for retention.
146 */
Soby Mathew12012dd2015-10-26 14:01:53 +0000147 if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
Dan Handley9df48042015-03-19 18:58:55 +0000148 return;
149
Soby Mathew9ca28062017-10-11 16:08:58 +0100150
Soby Mathew12012dd2015-10-26 14:01:53 +0000151 assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
Soby Mathewfec4eb72015-07-01 16:16:20 +0100152 css_power_down_common(target_state);
Soby Mathew9ca28062017-10-11 16:08:58 +0100153
154 /* Perform system domain state saving if issuing system suspend */
Nariman Poushincd956262018-05-01 09:28:40 +0100155 if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
Soby Mathew9ca28062017-10-11 16:08:58 +0100156 arm_system_pwr_domain_save();
157
158 /* Power off the Redistributor after having saved its context */
159 plat_arm_gic_redistif_off();
160 }
161
Soby Mathew200fffd2016-10-21 11:34:59 +0100162 css_scp_suspend(target_state);
Dan Handley9df48042015-03-19 18:58:55 +0000163}
164
165/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100166 * Handler called when a power domain has just been powered on after
167 * having been suspended earlier. The target_state encodes the low power state
168 * that each level has woken up from.
Dan Handley9df48042015-03-19 18:58:55 +0000169 * TODO: At the moment we reuse the on finisher and reinitialize the secure
170 * context. Need to implement a separate suspend finisher.
171 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100172void css_pwr_domain_suspend_finish(
Soby Mathewfec4eb72015-07-01 16:16:20 +0100173 const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000174{
Soby Mathew12012dd2015-10-26 14:01:53 +0000175 /* Return as nothing is to be done on waking up from retention. */
176 if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
Soby Mathewfec4eb72015-07-01 16:16:20 +0100177 return;
178
Soby Mathew12012dd2015-10-26 14:01:53 +0000179 /* Perform system domain restore if woken up from system suspend */
Nariman Poushincd956262018-05-01 09:28:40 +0100180 if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF)
Soby Mathew9ca28062017-10-11 16:08:58 +0100181 /*
182 * At this point, the Distributor must be powered on to be ready
183 * to have its state restored. The Redistributor will be powered
184 * on as part of gicv3_rdistif_init_restore.
185 */
Soby Mathew12012dd2015-10-26 14:01:53 +0000186 arm_system_pwr_domain_resume();
Soby Mathew12012dd2015-10-26 14:01:53 +0000187
188 css_pwr_domain_on_finisher_common(target_state);
Dan Handley9df48042015-03-19 18:58:55 +0000189}
190
191/*******************************************************************************
192 * Handlers to shutdown/reboot the system
193 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100194void __dead2 css_system_off(void)
Dan Handley9df48042015-03-19 18:58:55 +0000195{
Soby Mathew200fffd2016-10-21 11:34:59 +0100196 css_scp_sys_shutdown();
Dan Handley9df48042015-03-19 18:58:55 +0000197}
198
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100199void __dead2 css_system_reset(void)
Dan Handley9df48042015-03-19 18:58:55 +0000200{
Soby Mathew200fffd2016-10-21 11:34:59 +0100201 css_scp_sys_reboot();
Dan Handley9df48042015-03-19 18:58:55 +0000202}
203
204/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100205 * Handler called when the CPU power domain is about to enter standby.
Dan Handley9df48042015-03-19 18:58:55 +0000206 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100207void css_cpu_standby(plat_local_state_t cpu_state)
Dan Handley9df48042015-03-19 18:58:55 +0000208{
209 unsigned int scr;
210
Soby Mathewfec4eb72015-07-01 16:16:20 +0100211 assert(cpu_state == ARM_LOCAL_STATE_RET);
212
Dan Handley9df48042015-03-19 18:58:55 +0000213 scr = read_scr_el3();
David Wangc1d9cfb2016-06-07 09:22:40 +0800214 /*
215 * Enable the Non secure interrupt to wake the CPU.
216 * In GICv3 affinity routing mode, the non secure group1 interrupts use
217 * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
218 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
219 * routing mode.
220 */
221 write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
Dan Handley9df48042015-03-19 18:58:55 +0000222 isb();
223 dsb();
224 wfi();
225
226 /*
227 * Restore SCR to the original value, synchronisation of scr_el3 is
228 * done by eret while el3_exit to save some execution cycles.
229 */
230 write_scr_el3(scr);
231}
232
233/*******************************************************************************
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100234 * Handler called to return the 'req_state' for system suspend.
235 ******************************************************************************/
236void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
237{
238 unsigned int i;
239
240 /*
241 * System Suspend is supported only if the system power domain node
242 * is implemented.
243 */
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000244 assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL);
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100245
246 for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
247 req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
248}
249
250/*******************************************************************************
Jeenu Viswambharan9cc4fc02016-08-04 09:43:15 +0100251 * Handler to query CPU/cluster power states from SCP
252 ******************************************************************************/
253int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
254{
Soby Mathew200fffd2016-10-21 11:34:59 +0100255 return css_scp_get_power_state(mpidr, power_level);
Jeenu Viswambharan9cc4fc02016-08-04 09:43:15 +0100256}
257
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000258/*
259 * The system power domain suspend is only supported only via
260 * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
261 * will be downgraded to the lower level.
262 */
263static int css_validate_power_state(unsigned int power_state,
264 psci_power_state_t *req_state)
265{
266 int rc;
267 rc = arm_validate_power_state(power_state, req_state);
268
269 /*
Nariman Poushin16b41092018-05-01 13:07:47 +0100270 * Ensure that we don't overrun the pwr_domain_state array in the case
271 * where the platform supported max power level is less than the system
272 * power level
273 */
274
275#if (PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL)
276
277 /*
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000278 * Ensure that the system power domain level is never suspended
279 * via PSCI CPU SUSPEND API. Currently system suspend is only
280 * supported via PSCI SYSTEM SUSPEND API.
281 */
Nariman Poushin16b41092018-05-01 13:07:47 +0100282
283 req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] =
284 ARM_LOCAL_STATE_RUN;
285#endif
286
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000287 return rc;
288}
289
290/*
291 * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the
292 * `css_validate_power_state`, we do not downgrade the system power
293 * domain level request in `power_state` as it will be used to query the
294 * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
295 */
296static int css_translate_power_state_by_mpidr(u_register_t mpidr,
297 unsigned int power_state,
298 psci_power_state_t *output_state)
299{
300 return arm_validate_power_state(power_state, output_state);
301}
302
Jeenu Viswambharan9cc4fc02016-08-04 09:43:15 +0100303/*******************************************************************************
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100304 * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
305 * platform will take care of registering the handlers with PSCI.
Dan Handley9df48042015-03-19 18:58:55 +0000306 ******************************************************************************/
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100307plat_psci_ops_t plat_arm_psci_pm_ops = {
Soby Mathewfec4eb72015-07-01 16:16:20 +0100308 .pwr_domain_on = css_pwr_domain_on,
309 .pwr_domain_on_finish = css_pwr_domain_on_finish,
310 .pwr_domain_off = css_pwr_domain_off,
311 .cpu_standby = css_cpu_standby,
312 .pwr_domain_suspend = css_pwr_domain_suspend,
313 .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish,
Dan Handley9df48042015-03-19 18:58:55 +0000314 .system_off = css_system_off,
315 .system_reset = css_system_reset,
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000316 .validate_power_state = css_validate_power_state,
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100317 .validate_ns_entrypoint = arm_validate_psci_entrypoint,
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000318 .translate_power_state_by_mpidr = css_translate_power_state_by_mpidr,
319 .get_node_hw_state = css_node_hw_state,
Roberto Vargasa1c16b62017-08-03 09:16:43 +0100320 .get_sys_suspend_power_state = css_get_sys_suspend_power_state,
Roberto Vargas550eb082018-01-05 16:00:05 +0000321
322#if defined(PLAT_ARM_MEM_PROT_ADDR)
Roberto Vargasa1c16b62017-08-03 09:16:43 +0100323 .mem_protect_chk = arm_psci_mem_protect_chk,
324 .read_mem_protect = arm_psci_read_mem_protect,
325 .write_mem_protect = arm_nor_psci_write_mem_protect,
326#endif
Roberto Vargas3caafd72017-08-16 08:57:45 +0100327#if CSS_USE_SCMI_SDS_DRIVER
328 .system_reset2 = css_system_reset2,
329#endif
Dan Handley9df48042015-03-19 18:58:55 +0000330};