blob: 01c674f82e1483e65c004757c7d4342b2bf4e249 [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Madhukar Pappireddy2859b7d2019-06-10 16:54:36 -05002 * Copyright (c) 2015-2019, 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>
Antonio Nino Diaz326f56b2019-01-23 18:55:03 +000014#include <drivers/arm/css/css_scp.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <lib/cassert.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000016#include <plat/arm/common/plat_arm.h>
17#include <plat/arm/css/common/css_pm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <plat/common/platform.h>
19
Soby Mathewfeac8fc2015-09-29 15:47:16 +010020/* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
21#pragma weak plat_arm_psci_pm_ops
Soby Mathewfec4eb72015-07-01 16:16:20 +010022
Soby Mathew7799cf72015-04-16 14:49:09 +010023#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 */
30const unsigned int arm_pm_idle_states[] = {
Soby Mathewa869de12015-05-08 10:18:59 +010031 /* State-id - 0x001 */
32 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
33 ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
34 /* State-id - 0x002 */
35 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
36 ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
37 /* State-id - 0x022 */
38 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
39 ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
40#if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
41 /* State-id - 0x222 */
42 arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
43 ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
44#endif
Soby Mathew7799cf72015-04-16 14:49:09 +010045 0,
46};
Soby Mathewa869de12015-05-08 10:18:59 +010047#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathew7799cf72015-04-16 14:49:09 +010048
Soby Mathew61e8d0b2015-10-12 17:32:29 +010049/*
50 * All the power management helpers in this file assume at least cluster power
51 * level is supported.
52 */
53CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
54 assert_max_pwr_lvl_supported_mismatch);
55
Soby Mathew7a3b5eb2016-12-09 15:23:08 +000056/*
57 * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL
58 * assumed by the CSS layer.
59 */
60CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL,
61 assert_max_pwr_lvl_higher_than_css_sys_lvl);
62
Dan Handley9df48042015-03-19 18:58:55 +000063/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010064 * Handler called when a power domain is about to be turned on. The
Dan Handley9df48042015-03-19 18:58:55 +000065 * level and mpidr determine the affinity instance.
66 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010067int css_pwr_domain_on(u_register_t mpidr)
Dan Handley9df48042015-03-19 18:58:55 +000068{
Soby Mathew200fffd2016-10-21 11:34:59 +010069 css_scp_on(mpidr);
Dan Handley9df48042015-03-19 18:58:55 +000070
71 return PSCI_E_SUCCESS;
72}
73
Soby Mathew12012dd2015-10-26 14:01:53 +000074static void css_pwr_domain_on_finisher_common(
75 const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +000076{
Soby Mathew12012dd2015-10-26 14:01:53 +000077 assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
Soby Mathew61e8d0b2015-10-12 17:32:29 +010078
Dan Handley9df48042015-03-19 18:58:55 +000079 /*
80 * Perform the common cluster specific operations i.e enable coherency
81 * if this cluster was off.
82 */
Soby Mathew12012dd2015-10-26 14:01:53 +000083 if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
Vikram Kanigirifbb13012016-02-15 11:54:14 +000084 plat_arm_interconnect_enter_coherency();
Soby Mathew12012dd2015-10-26 14:01:53 +000085}
Dan Handley9df48042015-03-19 18:58:55 +000086
Soby Mathew12012dd2015-10-26 14:01:53 +000087/*******************************************************************************
88 * Handler called when a power level has just been powered on after
89 * being turned off earlier. The target_state encodes the low power state that
90 * each level has woken up from. This handler would never be invoked with
91 * the system power domain uninitialized as either the primary would have taken
92 * care of it as part of cold boot or the first core awakened from system
93 * suspend would have already initialized it.
94 ******************************************************************************/
95void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
96{
97 /* Assert that the system power domain need not be initialized */
Nariman Poushincd956262018-05-01 09:28:40 +010098 assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
Soby Mathew61e8d0b2015-10-12 17:32:29 +010099
Madhukar Pappireddy2859b7d2019-06-10 16:54:36 -0500100 css_pwr_domain_on_finisher_common(target_state);
101}
102
103/*******************************************************************************
104 * Handler called when a power domain has just been powered on and the cpu
105 * and its cluster are fully participating in coherent transaction on the
106 * interconnect. Data cache must be enabled for CPU at this point.
107 ******************************************************************************/
108void css_pwr_domain_on_finish_late(const psci_power_state_t *target_state)
109{
Achin Gupta1fa7eb62015-11-03 14:18:34 +0000110 /* Program the gic per-cpu distributor or re-distributor interface */
111 plat_arm_gic_pcpu_init();
112
Madhukar Pappireddy2859b7d2019-06-10 16:54:36 -0500113 /* Enable the gic cpu interface */
114 plat_arm_gic_cpuif_enable();
Dan Handley9df48042015-03-19 18:58:55 +0000115}
116
117/*******************************************************************************
118 * Common function called while turning a cpu off or suspending it. It is called
119 * from css_off() or css_suspend() when these functions in turn are called for
Soby Mathewfec4eb72015-07-01 16:16:20 +0100120 * power domain at the highest power level which will be powered down. It
121 * performs the actions common to the OFF and SUSPEND calls.
Dan Handley9df48042015-03-19 18:58:55 +0000122 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100123static void css_power_down_common(const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000124{
Dan Handley9df48042015-03-19 18:58:55 +0000125 /* Prevent interrupts from spuriously waking up this cpu */
Achin Gupta1fa7eb62015-11-03 14:18:34 +0000126 plat_arm_gic_cpuif_disable();
Dan Handley9df48042015-03-19 18:58:55 +0000127
128 /* Cluster is to be turned off, so disable coherency */
Soby Mathew200fffd2016-10-21 11:34:59 +0100129 if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
Vikram Kanigirifbb13012016-02-15 11:54:14 +0000130 plat_arm_interconnect_exit_coherency();
Dan Handley9df48042015-03-19 18:58:55 +0000131}
132
133/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100134 * Handler called when a power domain is about to be turned off. The
135 * target_state encodes the power state that each level should transition to.
Dan Handley9df48042015-03-19 18:58:55 +0000136 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100137void css_pwr_domain_off(const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000138{
Soby Mathew12012dd2015-10-26 14:01:53 +0000139 assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
Soby Mathewfec4eb72015-07-01 16:16:20 +0100140 css_power_down_common(target_state);
Soby Mathew200fffd2016-10-21 11:34:59 +0100141 css_scp_off(target_state);
Dan Handley9df48042015-03-19 18:58:55 +0000142}
143
144/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100145 * Handler called when a power domain is about to be suspended. The
146 * target_state encodes the power state that each level should transition to.
Dan Handley9df48042015-03-19 18:58:55 +0000147 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100148void css_pwr_domain_suspend(const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000149{
Soby Mathewfec4eb72015-07-01 16:16:20 +0100150 /*
Soby Mathew12012dd2015-10-26 14:01:53 +0000151 * CSS currently supports retention only at cpu level. Just return
Soby Mathewfec4eb72015-07-01 16:16:20 +0100152 * as nothing is to be done for retention.
153 */
Soby Mathew12012dd2015-10-26 14:01:53 +0000154 if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
Dan Handley9df48042015-03-19 18:58:55 +0000155 return;
156
Soby Mathew9ca28062017-10-11 16:08:58 +0100157
Soby Mathew12012dd2015-10-26 14:01:53 +0000158 assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
Soby Mathewfec4eb72015-07-01 16:16:20 +0100159 css_power_down_common(target_state);
Soby Mathew9ca28062017-10-11 16:08:58 +0100160
161 /* Perform system domain state saving if issuing system suspend */
Nariman Poushincd956262018-05-01 09:28:40 +0100162 if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
Soby Mathew9ca28062017-10-11 16:08:58 +0100163 arm_system_pwr_domain_save();
164
165 /* Power off the Redistributor after having saved its context */
166 plat_arm_gic_redistif_off();
167 }
168
Soby Mathew200fffd2016-10-21 11:34:59 +0100169 css_scp_suspend(target_state);
Dan Handley9df48042015-03-19 18:58:55 +0000170}
171
172/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100173 * Handler called when a power domain has just been powered on after
174 * having been suspended earlier. The target_state encodes the low power state
175 * that each level has woken up from.
Dan Handley9df48042015-03-19 18:58:55 +0000176 * TODO: At the moment we reuse the on finisher and reinitialize the secure
177 * context. Need to implement a separate suspend finisher.
178 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100179void css_pwr_domain_suspend_finish(
Soby Mathewfec4eb72015-07-01 16:16:20 +0100180 const psci_power_state_t *target_state)
Dan Handley9df48042015-03-19 18:58:55 +0000181{
Soby Mathew12012dd2015-10-26 14:01:53 +0000182 /* Return as nothing is to be done on waking up from retention. */
183 if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
Soby Mathewfec4eb72015-07-01 16:16:20 +0100184 return;
185
Soby Mathew12012dd2015-10-26 14:01:53 +0000186 /* Perform system domain restore if woken up from system suspend */
Nariman Poushincd956262018-05-01 09:28:40 +0100187 if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF)
Soby Mathew9ca28062017-10-11 16:08:58 +0100188 /*
189 * At this point, the Distributor must be powered on to be ready
190 * to have its state restored. The Redistributor will be powered
191 * on as part of gicv3_rdistif_init_restore.
192 */
Soby Mathew12012dd2015-10-26 14:01:53 +0000193 arm_system_pwr_domain_resume();
Soby Mathew12012dd2015-10-26 14:01:53 +0000194
195 css_pwr_domain_on_finisher_common(target_state);
Madhukar Pappireddy2859b7d2019-06-10 16:54:36 -0500196
197 /* Enable the gic cpu interface */
198 plat_arm_gic_cpuif_enable();
Dan Handley9df48042015-03-19 18:58:55 +0000199}
200
201/*******************************************************************************
202 * Handlers to shutdown/reboot the system
203 ******************************************************************************/
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100204void __dead2 css_system_off(void)
Dan Handley9df48042015-03-19 18:58:55 +0000205{
Soby Mathew200fffd2016-10-21 11:34:59 +0100206 css_scp_sys_shutdown();
Dan Handley9df48042015-03-19 18:58:55 +0000207}
208
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100209void __dead2 css_system_reset(void)
Dan Handley9df48042015-03-19 18:58:55 +0000210{
Soby Mathew200fffd2016-10-21 11:34:59 +0100211 css_scp_sys_reboot();
Dan Handley9df48042015-03-19 18:58:55 +0000212}
213
214/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +0100215 * Handler called when the CPU power domain is about to enter standby.
Dan Handley9df48042015-03-19 18:58:55 +0000216 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +0100217void css_cpu_standby(plat_local_state_t cpu_state)
Dan Handley9df48042015-03-19 18:58:55 +0000218{
219 unsigned int scr;
220
Soby Mathewfec4eb72015-07-01 16:16:20 +0100221 assert(cpu_state == ARM_LOCAL_STATE_RET);
222
Dan Handley9df48042015-03-19 18:58:55 +0000223 scr = read_scr_el3();
David Wangc1d9cfb2016-06-07 09:22:40 +0800224 /*
225 * Enable the Non secure interrupt to wake the CPU.
226 * In GICv3 affinity routing mode, the non secure group1 interrupts use
227 * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
228 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
229 * routing mode.
230 */
231 write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
Dan Handley9df48042015-03-19 18:58:55 +0000232 isb();
233 dsb();
234 wfi();
235
236 /*
237 * Restore SCR to the original value, synchronisation of scr_el3 is
238 * done by eret while el3_exit to save some execution cycles.
239 */
240 write_scr_el3(scr);
241}
242
243/*******************************************************************************
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100244 * Handler called to return the 'req_state' for system suspend.
245 ******************************************************************************/
246void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
247{
248 unsigned int i;
249
250 /*
251 * System Suspend is supported only if the system power domain node
252 * is implemented.
253 */
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000254 assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL);
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100255
256 for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
257 req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
258}
259
260/*******************************************************************************
Jeenu Viswambharan9cc4fc02016-08-04 09:43:15 +0100261 * Handler to query CPU/cluster power states from SCP
262 ******************************************************************************/
263int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
264{
Soby Mathew200fffd2016-10-21 11:34:59 +0100265 return css_scp_get_power_state(mpidr, power_level);
Jeenu Viswambharan9cc4fc02016-08-04 09:43:15 +0100266}
267
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000268/*
269 * The system power domain suspend is only supported only via
270 * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
271 * will be downgraded to the lower level.
272 */
273static int css_validate_power_state(unsigned int power_state,
274 psci_power_state_t *req_state)
275{
276 int rc;
277 rc = arm_validate_power_state(power_state, req_state);
278
279 /*
Nariman Poushin16b41092018-05-01 13:07:47 +0100280 * Ensure that we don't overrun the pwr_domain_state array in the case
281 * where the platform supported max power level is less than the system
282 * power level
283 */
284
285#if (PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL)
286
287 /*
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000288 * Ensure that the system power domain level is never suspended
289 * via PSCI CPU SUSPEND API. Currently system suspend is only
290 * supported via PSCI SYSTEM SUSPEND API.
291 */
Nariman Poushin16b41092018-05-01 13:07:47 +0100292
293 req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] =
294 ARM_LOCAL_STATE_RUN;
295#endif
296
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000297 return rc;
298}
299
300/*
301 * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the
302 * `css_validate_power_state`, we do not downgrade the system power
303 * domain level request in `power_state` as it will be used to query the
304 * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
305 */
306static int css_translate_power_state_by_mpidr(u_register_t mpidr,
307 unsigned int power_state,
308 psci_power_state_t *output_state)
309{
310 return arm_validate_power_state(power_state, output_state);
311}
312
Jeenu Viswambharan9cc4fc02016-08-04 09:43:15 +0100313/*******************************************************************************
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100314 * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
315 * platform will take care of registering the handlers with PSCI.
Dan Handley9df48042015-03-19 18:58:55 +0000316 ******************************************************************************/
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100317plat_psci_ops_t plat_arm_psci_pm_ops = {
Soby Mathewfec4eb72015-07-01 16:16:20 +0100318 .pwr_domain_on = css_pwr_domain_on,
319 .pwr_domain_on_finish = css_pwr_domain_on_finish,
Madhukar Pappireddy2859b7d2019-06-10 16:54:36 -0500320 .pwr_domain_on_finish_late = css_pwr_domain_on_finish_late,
Soby Mathewfec4eb72015-07-01 16:16:20 +0100321 .pwr_domain_off = css_pwr_domain_off,
322 .cpu_standby = css_cpu_standby,
323 .pwr_domain_suspend = css_pwr_domain_suspend,
324 .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish,
Dan Handley9df48042015-03-19 18:58:55 +0000325 .system_off = css_system_off,
326 .system_reset = css_system_reset,
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000327 .validate_power_state = css_validate_power_state,
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100328 .validate_ns_entrypoint = arm_validate_psci_entrypoint,
Soby Mathew7a3b5eb2016-12-09 15:23:08 +0000329 .translate_power_state_by_mpidr = css_translate_power_state_by_mpidr,
330 .get_node_hw_state = css_node_hw_state,
Roberto Vargasa1c16b62017-08-03 09:16:43 +0100331 .get_sys_suspend_power_state = css_get_sys_suspend_power_state,
Roberto Vargas550eb082018-01-05 16:00:05 +0000332
333#if defined(PLAT_ARM_MEM_PROT_ADDR)
Roberto Vargasa1c16b62017-08-03 09:16:43 +0100334 .mem_protect_chk = arm_psci_mem_protect_chk,
335 .read_mem_protect = arm_psci_read_mem_protect,
336 .write_mem_protect = arm_nor_psci_write_mem_protect,
337#endif
Roberto Vargas3caafd72017-08-16 08:57:45 +0100338#if CSS_USE_SCMI_SDS_DRIVER
339 .system_reset2 = css_system_reset2,
340#endif
Dan Handley9df48042015-03-19 18:58:55 +0000341};