blob: 83df099bc685ed8c0ec7266b92dd640bc8cfce93 [file] [log] [blame]
Soby Mathewb48349e2015-06-29 16:30:12 +01001/*
Boyan Karatotev44ee7712024-09-30 13:15:25 +01002 * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
Soby Mathewb48349e2015-06-29 16:30:12 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewb48349e2015-06-29 16:30:12 +01005 */
6
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00007#include <assert.h>
8#include <stddef.h>
9
Soby Mathewb48349e2015-06-29 16:30:12 +010010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <common/bl_common.h>
13#include <common/debug.h>
Soby Mathewb48349e2015-06-29 16:30:12 +010014#include <context.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000015#include <lib/el3_runtime/context_mgmt.h>
16#include <lib/el3_runtime/cpu_data.h>
17#include <lib/el3_runtime/pubsub_events.h>
18#include <lib/pmf/pmf.h>
19#include <lib/runtime_instr.h>
20#include <plat/common/platform.h>
21
Soby Mathewb48349e2015-06-29 16:30:12 +010022#include "psci_private.h"
23
Soby Mathewb48349e2015-06-29 16:30:12 +010024/*******************************************************************************
Soby Mathew8ee24982015-04-07 12:16:56 +010025 * This function does generic and platform specific operations after a wake-up
26 * from standby/retention states at multiple power levels.
Soby Mathewb48349e2015-06-29 16:30:12 +010027 ******************************************************************************/
Boyan Karatotev44ee7712024-09-30 13:15:25 +010028static void psci_cpu_suspend_to_standby_finish(unsigned int cpu_idx,
29 unsigned int end_pwrlvl,
30 psci_power_state_t *state_info)
Soby Mathewb48349e2015-06-29 16:30:12 +010031{
Achin Gupta61eae522016-06-28 16:46:15 +010032 /*
Soby Mathew8ee24982015-04-07 12:16:56 +010033 * Plat. management: Allow the platform to do operations
34 * on waking up from retention.
35 */
Boyan Karatotev44ee7712024-09-30 13:15:25 +010036 psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
Soby Mathew8ee24982015-04-07 12:16:56 +010037
Boyan Karatotev0c836552024-09-30 11:31:55 +010038 /* This loses its meaning when not suspending, reset so it's correct for OFF */
39 psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL);
Soby Mathewb48349e2015-06-29 16:30:12 +010040}
41
42/*******************************************************************************
Soby Mathew8ee24982015-04-07 12:16:56 +010043 * This function does generic and platform specific suspend to power down
44 * operations.
Soby Mathewb48349e2015-06-29 16:30:12 +010045 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +010046static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl,
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +010047 const entry_point_info_t *ep,
48 const psci_power_state_t *state_info)
Soby Mathewb48349e2015-06-29 16:30:12 +010049{
Achin Guptaf1054c92015-09-07 20:43:27 +010050 unsigned int max_off_lvl = psci_find_max_off_lvl(state_info);
51
Dimitris Papastamos75932522017-11-28 15:16:00 +000052 PUBLISH_EVENT(psci_suspend_pwrdown_start);
53
Wing Li606b7432022-09-14 13:18:17 -070054#if PSCI_OS_INIT_MODE
55#ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
56 end_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
57#else
58 end_pwrlvl = PLAT_MAX_PWR_LVL;
59#endif
60#endif
61
Soby Mathew8ee24982015-04-07 12:16:56 +010062 /* Save PSCI target power level for the suspend finisher handler */
63 psci_set_suspend_pwrlvl(end_pwrlvl);
Soby Mathewb48349e2015-06-29 16:30:12 +010064
Soby Mathew8ee24982015-04-07 12:16:56 +010065 /*
Jeenu Viswambharana10d3632017-01-06 14:58:11 +000066 * Flush the target power level as it might be accessed on power up with
Soby Mathew8ee24982015-04-07 12:16:56 +010067 * Data cache disabled.
68 */
Jeenu Viswambharana10d3632017-01-06 14:58:11 +000069 psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl);
Soby Mathewb48349e2015-06-29 16:30:12 +010070
Soby Mathew8ee24982015-04-07 12:16:56 +010071 /*
72 * Call the cpu suspend handler registered by the Secure Payload
73 * Dispatcher to let it do any book-keeping. If the handler encounters an
74 * error, it's expected to assert within
75 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +010076 if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL))
Achin Guptaf1054c92015-09-07 20:43:27 +010077 psci_spd_pm->svc_suspend(max_off_lvl);
Soby Mathewb48349e2015-06-29 16:30:12 +010078
Varun Wadekar1862d622017-07-10 16:02:05 -070079#if !HW_ASSISTED_COHERENCY
80 /*
81 * Plat. management: Allow the platform to perform any early
82 * actions required to power down the CPU. This might be useful for
83 * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these
84 * actions with data caches enabled.
85 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +010086 if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL)
Varun Wadekar1862d622017-07-10 16:02:05 -070087 psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info);
88#endif
89
Soby Mathew8ee24982015-04-07 12:16:56 +010090 /*
91 * Store the re-entry information for the non-secure world.
92 */
93 cm_init_my_context(ep);
Soby Mathewb48349e2015-06-29 16:30:12 +010094
dp-arm79418162016-11-15 13:25:30 +000095#if ENABLE_RUNTIME_INSTRUMENTATION
96
97 /*
98 * Flush cache line so that even if CPU power down happens
99 * the timestamp update is reflected in memory.
100 */
101 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
102 RT_INSTR_ENTER_CFLUSH,
103 PMF_CACHE_MAINT);
104#endif
105
Soby Mathew8ee24982015-04-07 12:16:56 +0100106 /*
Jeenu Viswambharanb0408e82017-01-05 11:01:02 +0000107 * Arch. management. Initiate power down sequence.
Soby Mathew8ee24982015-04-07 12:16:56 +0100108 * TODO : Introduce a mechanism to query the cache level to flush
109 * and the cpu-ops power down to perform from the platform.
110 */
Pranav Madhu65bbb932022-07-22 23:11:16 +0530111 psci_pwrdown_cpu(max_off_lvl);
dp-arm79418162016-11-15 13:25:30 +0000112
113#if ENABLE_RUNTIME_INSTRUMENTATION
114 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
115 RT_INSTR_EXIT_CFLUSH,
116 PMF_NO_CACHE_MAINT);
117#endif
Soby Mathewb48349e2015-06-29 16:30:12 +0100118}
119
120/*******************************************************************************
Soby Mathewb48349e2015-06-29 16:30:12 +0100121 * Top level handler which is called when a cpu wants to suspend its execution.
Soby Mathew4067dc32015-05-05 16:33:16 +0100122 * It is assumed that along with suspending the cpu power domain, power domains
Soby Mathew8ee24982015-04-07 12:16:56 +0100123 * at higher levels until the target power level will be suspended as well. It
124 * coordinates with the platform to negotiate the target state for each of
125 * the power domain level till the target power domain level. It then performs
126 * generic, architectural, platform setup and state management required to
127 * suspend that power domain level and power domain levels below it.
128 * e.g. For a cpu that's to be suspended, it could mean programming the
129 * power controller whereas for a cluster that's to be suspended, it will call
130 * the platform specific code which will disable coherency at the interconnect
131 * level if the cpu is the last in the cluster and also the program the power
132 * controller.
Soby Mathewb48349e2015-06-29 16:30:12 +0100133 *
134 * All the required parameter checks are performed at the beginning and after
Soby Mathew6590ce22015-06-30 11:00:24 +0100135 * the state transition has been done, no further error is expected and it is
136 * not possible to undo any of the actions taken beyond that point.
Soby Mathewb48349e2015-06-29 16:30:12 +0100137 ******************************************************************************/
Wing Li606b7432022-09-14 13:18:17 -0700138int psci_cpu_suspend_start(const entry_point_info_t *ep,
139 unsigned int end_pwrlvl,
140 psci_power_state_t *state_info,
141 unsigned int is_power_down_state)
Soby Mathewb48349e2015-06-29 16:30:12 +0100142{
Wing Li606b7432022-09-14 13:18:17 -0700143 int rc = PSCI_E_SUCCESS;
144 bool skip_wfi = false;
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600145 unsigned int idx = plat_my_core_pos();
Andrew F. Davis74d27d02019-06-04 10:46:54 -0400146 unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
Soby Mathewb48349e2015-06-29 16:30:12 +0100147
148 /*
149 * This function must only be called on platforms where the
150 * CPU_SUSPEND platform hooks have been implemented.
151 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100152 assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
153 (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL));
Soby Mathewb48349e2015-06-29 16:30:12 +0100154
Andrew F. Davis74d27d02019-06-04 10:46:54 -0400155 /* Get the parent nodes */
156 psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes);
157
Soby Mathewb48349e2015-06-29 16:30:12 +0100158 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100159 * This function acquires the lock corresponding to each power
Soby Mathewb48349e2015-06-29 16:30:12 +0100160 * level so that by the time all locks are taken, the system topology
161 * is snapshot and state management can be done safely.
162 */
Andrew F. Davis74d27d02019-06-04 10:46:54 -0400163 psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
Soby Mathewb48349e2015-06-29 16:30:12 +0100164
165 /*
166 * We check if there are any pending interrupts after the delay
167 * introduced by lock contention to increase the chances of early
168 * detection that a wake-up interrupt has fired.
169 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100170 if (read_isr_el1() != 0U) {
Wing Li606b7432022-09-14 13:18:17 -0700171 skip_wfi = true;
Soby Mathewb48349e2015-06-29 16:30:12 +0100172 goto exit;
173 }
174
Wing Li606b7432022-09-14 13:18:17 -0700175#if PSCI_OS_INIT_MODE
176 if (psci_suspend_mode == OS_INIT) {
177 /*
178 * This function validates the requested state info for
179 * OS-initiated mode.
180 */
181 rc = psci_validate_state_coordination(end_pwrlvl, state_info);
182 if (rc != PSCI_E_SUCCESS) {
183 skip_wfi = true;
184 goto exit;
185 }
186 } else {
187#endif
188 /*
189 * This function is passed the requested state info and
190 * it returns the negotiated state info for each power level upto
191 * the end level specified.
192 */
193 psci_do_state_coordination(end_pwrlvl, state_info);
194#if PSCI_OS_INIT_MODE
195 }
196#endif
Soby Mathewb48349e2015-06-29 16:30:12 +0100197
Wing Lid3488612023-05-04 08:31:19 -0700198#if PSCI_OS_INIT_MODE
199 if (psci_plat_pm_ops->pwr_domain_validate_suspend != NULL) {
200 rc = psci_plat_pm_ops->pwr_domain_validate_suspend(state_info);
201 if (rc != PSCI_E_SUCCESS) {
202 skip_wfi = true;
203 goto exit;
204 }
205 }
206#endif
207
208 /* Update the target state in the power domain nodes */
209 psci_set_target_local_pwr_states(end_pwrlvl, state_info);
210
Yatharth Kochar170fb932016-05-09 18:26:35 +0100211#if ENABLE_PSCI_STAT
212 /* Update the last cpu for each level till end_pwrlvl */
213 psci_stats_update_pwr_down(end_pwrlvl, state_info);
214#endif
215
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100216 if (is_power_down_state != 0U)
Soby Mathew8ee24982015-04-07 12:16:56 +0100217 psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info);
Soby Mathewb48349e2015-06-29 16:30:12 +0100218
Soby Mathew6590ce22015-06-30 11:00:24 +0100219 /*
220 * Plat. management: Allow the platform to perform the
221 * necessary actions to turn off this cpu e.g. set the
222 * platform defined mailbox with the psci entrypoint,
223 * program the power controller etc.
224 */
Wing Li606b7432022-09-14 13:18:17 -0700225
Sandrine Bailleuxeb975f52015-06-11 10:46:48 +0100226 psci_plat_pm_ops->pwr_domain_suspend(state_info);
Soby Mathewb48349e2015-06-29 16:30:12 +0100227
Yatharth Kochar170fb932016-05-09 18:26:35 +0100228#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000229 plat_psci_stat_accounting_start(state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100230#endif
231
Soby Mathewb48349e2015-06-29 16:30:12 +0100232exit:
233 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100234 * Release the locks corresponding to each power level in the
Soby Mathewb48349e2015-06-29 16:30:12 +0100235 * reverse order to which they were acquired.
236 */
Andrew F. Davis74d27d02019-06-04 10:46:54 -0400237 psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
238
Wing Li606b7432022-09-14 13:18:17 -0700239 if (skip_wfi) {
240 return rc;
241 }
Soby Mathew8ee24982015-04-07 12:16:56 +0100242
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100243 if (is_power_down_state != 0U) {
dp-arm872be882016-09-19 11:18:44 +0100244#if ENABLE_RUNTIME_INSTRUMENTATION
245
246 /*
247 * Update the timestamp with cache off. We assume this
248 * timestamp can only be read from the current CPU and the
249 * timestamp cache line will be flushed before return to
250 * normal world on wakeup.
251 */
252 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
253 RT_INSTR_ENTER_HW_LOW_PWR,
254 PMF_NO_CACHE_MAINT);
255#endif
256
Soby Mathewac1cc8e2016-04-27 14:46:28 +0100257 /* The function calls below must not return */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100258 if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL)
Soby Mathewac1cc8e2016-04-27 14:46:28 +0100259 psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info);
260 else
261 psci_power_down_wfi();
262 }
Soby Mathew8ee24982015-04-07 12:16:56 +0100263
dp-arm872be882016-09-19 11:18:44 +0100264#if ENABLE_RUNTIME_INSTRUMENTATION
265 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
266 RT_INSTR_ENTER_HW_LOW_PWR,
267 PMF_NO_CACHE_MAINT);
268#endif
269
Soby Mathew8ee24982015-04-07 12:16:56 +0100270 /*
271 * We will reach here if only retention/standby states have been
272 * requested at multiple power levels. This means that the cpu
273 * context will be preserved.
274 */
275 wfi();
276
dp-arm872be882016-09-19 11:18:44 +0100277#if ENABLE_RUNTIME_INSTRUMENTATION
278 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
279 RT_INSTR_EXIT_HW_LOW_PWR,
280 PMF_NO_CACHE_MAINT);
281#endif
282
Boyan Karatotev44ee7712024-09-30 13:15:25 +0100283 psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
284 /*
285 * Find out which retention states this CPU has exited from until the
286 * 'end_pwrlvl'. The exit retention state could be deeper than the entry
287 * state as a result of state coordination amongst other CPUs post wfi.
288 */
289 psci_get_target_local_pwr_states(end_pwrlvl, state_info);
290
291#if ENABLE_PSCI_STAT
292 plat_psci_stat_accounting_stop(state_info);
293 psci_stats_update_pwr_up(end_pwrlvl, state_info);
294#endif
295
Soby Mathew8ee24982015-04-07 12:16:56 +0100296 /*
297 * After we wake up from context retaining suspend, call the
298 * context retaining suspend finisher.
299 */
Boyan Karatotev44ee7712024-09-30 13:15:25 +0100300 psci_cpu_suspend_to_standby_finish(idx, end_pwrlvl, state_info);
301
302 /*
303 * Set the requested and target state of this CPU and all the higher
304 * power domain levels for this CPU to run.
305 */
306 psci_set_pwr_domains_to_run(end_pwrlvl);
307
308 psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
Wing Li606b7432022-09-14 13:18:17 -0700309
310 return rc;
Soby Mathewb48349e2015-06-29 16:30:12 +0100311}
312
313/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100314 * The following functions finish an earlier suspend request. They
Soby Mathew8ee24982015-04-07 12:16:56 +0100315 * are called by the common finisher routine in psci_common.c. The `state_info`
316 * is the psci_power_state from which this CPU has woken up from.
Soby Mathewb48349e2015-06-29 16:30:12 +0100317 ******************************************************************************/
Boyan Karatotev44ee7712024-09-30 13:15:25 +0100318void psci_cpu_suspend_to_powerdown_finish(unsigned int cpu_idx, const psci_power_state_t *state_info)
Soby Mathewb48349e2015-06-29 16:30:12 +0100319{
Antonio Nino Diazd4486392016-05-18 16:53:31 +0100320 unsigned int counter_freq;
Achin Guptaf1054c92015-09-07 20:43:27 +0100321 unsigned int max_off_lvl;
Soby Mathewb48349e2015-06-29 16:30:12 +0100322
Soby Mathewb48349e2015-06-29 16:30:12 +0100323 /* Ensure we have been woken up from a suspended state */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100324 assert((psci_get_aff_info_state() == AFF_STATE_ON) &&
325 (is_local_state_off(
326 state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0));
Soby Mathewb48349e2015-06-29 16:30:12 +0100327
328 /*
329 * Plat. management: Perform the platform specific actions
330 * before we change the state of the cpu e.g. enabling the
331 * gic or zeroing the mailbox register. If anything goes
332 * wrong then assert as there is no way to recover from this
333 * situation.
334 */
Soby Mathew8ee24982015-04-07 12:16:56 +0100335 psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
Soby Mathewb48349e2015-06-29 16:30:12 +0100336
Soby Mathewbcc3c492017-04-10 22:35:42 +0100337#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
Jeenu Viswambharanb0408e82017-01-05 11:01:02 +0000338 /* Arch. management: Enable the data cache, stack memory maintenance. */
Soby Mathewb48349e2015-06-29 16:30:12 +0100339 psci_do_pwrup_cache_maintenance();
Jeenu Viswambharanb0408e82017-01-05 11:01:02 +0000340#endif
Soby Mathewb48349e2015-06-29 16:30:12 +0100341
342 /* Re-init the cntfrq_el0 register */
Antonio Nino Diazd4486392016-05-18 16:53:31 +0100343 counter_freq = plat_get_syscnt_freq2();
Soby Mathewb48349e2015-06-29 16:30:12 +0100344 write_cntfrq_el0(counter_freq);
345
Alexei Fedoroved108b52019-09-13 14:11:59 +0100346#if ENABLE_PAUTH
347 /* Store APIAKey_EL1 key */
348 set_cpu_data(apiakey[0], read_apiakeylo_el1());
349 set_cpu_data(apiakey[1], read_apiakeyhi_el1());
350#endif /* ENABLE_PAUTH */
351
Soby Mathewb48349e2015-06-29 16:30:12 +0100352 /*
353 * Call the cpu suspend finish handler registered by the Secure Payload
354 * Dispatcher to let it do any bookeeping. If the handler encounters an
355 * error, it's expected to assert within
356 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100357 if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) {
Achin Guptaf1054c92015-09-07 20:43:27 +0100358 max_off_lvl = psci_find_max_off_lvl(state_info);
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +0100359 assert(max_off_lvl != PSCI_INVALID_PWR_LVL);
Achin Guptaf1054c92015-09-07 20:43:27 +0100360 psci_spd_pm->svc_suspend_finish(max_off_lvl);
Soby Mathewb48349e2015-06-29 16:30:12 +0100361 }
362
Boyan Karatotev0c836552024-09-30 11:31:55 +0100363 /* This loses its meaning when not suspending, reset so it's correct for OFF */
364 psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL);
Soby Mathewb48349e2015-06-29 16:30:12 +0100365
Dimitris Papastamos75932522017-11-28 15:16:00 +0000366 PUBLISH_EVENT(psci_suspend_pwrdown_finish);
Soby Mathewb48349e2015-06-29 16:30:12 +0100367}