1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <stddef.h>
9*54fd6939SJiyong Park
10*54fd6939SJiyong Park #include <arch.h>
11*54fd6939SJiyong Park #include <arch_helpers.h>
12*54fd6939SJiyong Park #include <common/bl_common.h>
13*54fd6939SJiyong Park #include <common/debug.h>
14*54fd6939SJiyong Park #include <context.h>
15*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
16*54fd6939SJiyong Park #include <lib/el3_runtime/cpu_data.h>
17*54fd6939SJiyong Park #include <lib/el3_runtime/pubsub_events.h>
18*54fd6939SJiyong Park #include <lib/pmf/pmf.h>
19*54fd6939SJiyong Park #include <lib/runtime_instr.h>
20*54fd6939SJiyong Park #include <plat/common/platform.h>
21*54fd6939SJiyong Park
22*54fd6939SJiyong Park #include "psci_private.h"
23*54fd6939SJiyong Park
24*54fd6939SJiyong Park /*******************************************************************************
25*54fd6939SJiyong Park * This function does generic and platform specific operations after a wake-up
26*54fd6939SJiyong Park * from standby/retention states at multiple power levels.
27*54fd6939SJiyong Park ******************************************************************************/
psci_suspend_to_standby_finisher(unsigned int cpu_idx,unsigned int end_pwrlvl)28*54fd6939SJiyong Park static void psci_suspend_to_standby_finisher(unsigned int cpu_idx,
29*54fd6939SJiyong Park unsigned int end_pwrlvl)
30*54fd6939SJiyong Park {
31*54fd6939SJiyong Park unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
32*54fd6939SJiyong Park psci_power_state_t state_info;
33*54fd6939SJiyong Park
34*54fd6939SJiyong Park /* Get the parent nodes */
35*54fd6939SJiyong Park psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
36*54fd6939SJiyong Park
37*54fd6939SJiyong Park psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
38*54fd6939SJiyong Park
39*54fd6939SJiyong Park /*
40*54fd6939SJiyong Park * Find out which retention states this CPU has exited from until the
41*54fd6939SJiyong Park * 'end_pwrlvl'. The exit retention state could be deeper than the entry
42*54fd6939SJiyong Park * state as a result of state coordination amongst other CPUs post wfi.
43*54fd6939SJiyong Park */
44*54fd6939SJiyong Park psci_get_target_local_pwr_states(end_pwrlvl, &state_info);
45*54fd6939SJiyong Park
46*54fd6939SJiyong Park #if ENABLE_PSCI_STAT
47*54fd6939SJiyong Park plat_psci_stat_accounting_stop(&state_info);
48*54fd6939SJiyong Park psci_stats_update_pwr_up(end_pwrlvl, &state_info);
49*54fd6939SJiyong Park #endif
50*54fd6939SJiyong Park
51*54fd6939SJiyong Park /*
52*54fd6939SJiyong Park * Plat. management: Allow the platform to do operations
53*54fd6939SJiyong Park * on waking up from retention.
54*54fd6939SJiyong Park */
55*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_suspend_finish(&state_info);
56*54fd6939SJiyong Park
57*54fd6939SJiyong Park /*
58*54fd6939SJiyong Park * Set the requested and target state of this CPU and all the higher
59*54fd6939SJiyong Park * power domain levels for this CPU to run.
60*54fd6939SJiyong Park */
61*54fd6939SJiyong Park psci_set_pwr_domains_to_run(end_pwrlvl);
62*54fd6939SJiyong Park
63*54fd6939SJiyong Park psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
64*54fd6939SJiyong Park }
65*54fd6939SJiyong Park
66*54fd6939SJiyong Park /*******************************************************************************
67*54fd6939SJiyong Park * This function does generic and platform specific suspend to power down
68*54fd6939SJiyong Park * operations.
69*54fd6939SJiyong Park ******************************************************************************/
psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl,const entry_point_info_t * ep,const psci_power_state_t * state_info)70*54fd6939SJiyong Park static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl,
71*54fd6939SJiyong Park const entry_point_info_t *ep,
72*54fd6939SJiyong Park const psci_power_state_t *state_info)
73*54fd6939SJiyong Park {
74*54fd6939SJiyong Park unsigned int max_off_lvl = psci_find_max_off_lvl(state_info);
75*54fd6939SJiyong Park
76*54fd6939SJiyong Park PUBLISH_EVENT(psci_suspend_pwrdown_start);
77*54fd6939SJiyong Park
78*54fd6939SJiyong Park /* Save PSCI target power level for the suspend finisher handler */
79*54fd6939SJiyong Park psci_set_suspend_pwrlvl(end_pwrlvl);
80*54fd6939SJiyong Park
81*54fd6939SJiyong Park /*
82*54fd6939SJiyong Park * Flush the target power level as it might be accessed on power up with
83*54fd6939SJiyong Park * Data cache disabled.
84*54fd6939SJiyong Park */
85*54fd6939SJiyong Park psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl);
86*54fd6939SJiyong Park
87*54fd6939SJiyong Park /*
88*54fd6939SJiyong Park * Call the cpu suspend handler registered by the Secure Payload
89*54fd6939SJiyong Park * Dispatcher to let it do any book-keeping. If the handler encounters an
90*54fd6939SJiyong Park * error, it's expected to assert within
91*54fd6939SJiyong Park */
92*54fd6939SJiyong Park if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL))
93*54fd6939SJiyong Park psci_spd_pm->svc_suspend(max_off_lvl);
94*54fd6939SJiyong Park
95*54fd6939SJiyong Park #if !HW_ASSISTED_COHERENCY
96*54fd6939SJiyong Park /*
97*54fd6939SJiyong Park * Plat. management: Allow the platform to perform any early
98*54fd6939SJiyong Park * actions required to power down the CPU. This might be useful for
99*54fd6939SJiyong Park * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these
100*54fd6939SJiyong Park * actions with data caches enabled.
101*54fd6939SJiyong Park */
102*54fd6939SJiyong Park if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL)
103*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info);
104*54fd6939SJiyong Park #endif
105*54fd6939SJiyong Park
106*54fd6939SJiyong Park /*
107*54fd6939SJiyong Park * Store the re-entry information for the non-secure world.
108*54fd6939SJiyong Park */
109*54fd6939SJiyong Park cm_init_my_context(ep);
110*54fd6939SJiyong Park
111*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
112*54fd6939SJiyong Park
113*54fd6939SJiyong Park /*
114*54fd6939SJiyong Park * Flush cache line so that even if CPU power down happens
115*54fd6939SJiyong Park * the timestamp update is reflected in memory.
116*54fd6939SJiyong Park */
117*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
118*54fd6939SJiyong Park RT_INSTR_ENTER_CFLUSH,
119*54fd6939SJiyong Park PMF_CACHE_MAINT);
120*54fd6939SJiyong Park #endif
121*54fd6939SJiyong Park
122*54fd6939SJiyong Park /*
123*54fd6939SJiyong Park * Arch. management. Initiate power down sequence.
124*54fd6939SJiyong Park * TODO : Introduce a mechanism to query the cache level to flush
125*54fd6939SJiyong Park * and the cpu-ops power down to perform from the platform.
126*54fd6939SJiyong Park */
127*54fd6939SJiyong Park psci_do_pwrdown_sequence(max_off_lvl);
128*54fd6939SJiyong Park
129*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
130*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
131*54fd6939SJiyong Park RT_INSTR_EXIT_CFLUSH,
132*54fd6939SJiyong Park PMF_NO_CACHE_MAINT);
133*54fd6939SJiyong Park #endif
134*54fd6939SJiyong Park }
135*54fd6939SJiyong Park
136*54fd6939SJiyong Park /*******************************************************************************
137*54fd6939SJiyong Park * Top level handler which is called when a cpu wants to suspend its execution.
138*54fd6939SJiyong Park * It is assumed that along with suspending the cpu power domain, power domains
139*54fd6939SJiyong Park * at higher levels until the target power level will be suspended as well. It
140*54fd6939SJiyong Park * coordinates with the platform to negotiate the target state for each of
141*54fd6939SJiyong Park * the power domain level till the target power domain level. It then performs
142*54fd6939SJiyong Park * generic, architectural, platform setup and state management required to
143*54fd6939SJiyong Park * suspend that power domain level and power domain levels below it.
144*54fd6939SJiyong Park * e.g. For a cpu that's to be suspended, it could mean programming the
145*54fd6939SJiyong Park * power controller whereas for a cluster that's to be suspended, it will call
146*54fd6939SJiyong Park * the platform specific code which will disable coherency at the interconnect
147*54fd6939SJiyong Park * level if the cpu is the last in the cluster and also the program the power
148*54fd6939SJiyong Park * controller.
149*54fd6939SJiyong Park *
150*54fd6939SJiyong Park * All the required parameter checks are performed at the beginning and after
151*54fd6939SJiyong Park * the state transition has been done, no further error is expected and it is
152*54fd6939SJiyong Park * not possible to undo any of the actions taken beyond that point.
153*54fd6939SJiyong Park ******************************************************************************/
psci_cpu_suspend_start(const entry_point_info_t * ep,unsigned int end_pwrlvl,psci_power_state_t * state_info,unsigned int is_power_down_state)154*54fd6939SJiyong Park void psci_cpu_suspend_start(const entry_point_info_t *ep,
155*54fd6939SJiyong Park unsigned int end_pwrlvl,
156*54fd6939SJiyong Park psci_power_state_t *state_info,
157*54fd6939SJiyong Park unsigned int is_power_down_state)
158*54fd6939SJiyong Park {
159*54fd6939SJiyong Park int skip_wfi = 0;
160*54fd6939SJiyong Park unsigned int idx = plat_my_core_pos();
161*54fd6939SJiyong Park unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
162*54fd6939SJiyong Park
163*54fd6939SJiyong Park /*
164*54fd6939SJiyong Park * This function must only be called on platforms where the
165*54fd6939SJiyong Park * CPU_SUSPEND platform hooks have been implemented.
166*54fd6939SJiyong Park */
167*54fd6939SJiyong Park assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
168*54fd6939SJiyong Park (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL));
169*54fd6939SJiyong Park
170*54fd6939SJiyong Park /* Get the parent nodes */
171*54fd6939SJiyong Park psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes);
172*54fd6939SJiyong Park
173*54fd6939SJiyong Park /*
174*54fd6939SJiyong Park * This function acquires the lock corresponding to each power
175*54fd6939SJiyong Park * level so that by the time all locks are taken, the system topology
176*54fd6939SJiyong Park * is snapshot and state management can be done safely.
177*54fd6939SJiyong Park */
178*54fd6939SJiyong Park psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
179*54fd6939SJiyong Park
180*54fd6939SJiyong Park /*
181*54fd6939SJiyong Park * We check if there are any pending interrupts after the delay
182*54fd6939SJiyong Park * introduced by lock contention to increase the chances of early
183*54fd6939SJiyong Park * detection that a wake-up interrupt has fired.
184*54fd6939SJiyong Park */
185*54fd6939SJiyong Park if (read_isr_el1() != 0U) {
186*54fd6939SJiyong Park skip_wfi = 1;
187*54fd6939SJiyong Park goto exit;
188*54fd6939SJiyong Park }
189*54fd6939SJiyong Park
190*54fd6939SJiyong Park /*
191*54fd6939SJiyong Park * This function is passed the requested state info and
192*54fd6939SJiyong Park * it returns the negotiated state info for each power level upto
193*54fd6939SJiyong Park * the end level specified.
194*54fd6939SJiyong Park */
195*54fd6939SJiyong Park psci_do_state_coordination(end_pwrlvl, state_info);
196*54fd6939SJiyong Park
197*54fd6939SJiyong Park #if ENABLE_PSCI_STAT
198*54fd6939SJiyong Park /* Update the last cpu for each level till end_pwrlvl */
199*54fd6939SJiyong Park psci_stats_update_pwr_down(end_pwrlvl, state_info);
200*54fd6939SJiyong Park #endif
201*54fd6939SJiyong Park
202*54fd6939SJiyong Park if (is_power_down_state != 0U)
203*54fd6939SJiyong Park psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info);
204*54fd6939SJiyong Park
205*54fd6939SJiyong Park /*
206*54fd6939SJiyong Park * Plat. management: Allow the platform to perform the
207*54fd6939SJiyong Park * necessary actions to turn off this cpu e.g. set the
208*54fd6939SJiyong Park * platform defined mailbox with the psci entrypoint,
209*54fd6939SJiyong Park * program the power controller etc.
210*54fd6939SJiyong Park */
211*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_suspend(state_info);
212*54fd6939SJiyong Park
213*54fd6939SJiyong Park #if ENABLE_PSCI_STAT
214*54fd6939SJiyong Park plat_psci_stat_accounting_start(state_info);
215*54fd6939SJiyong Park #endif
216*54fd6939SJiyong Park
217*54fd6939SJiyong Park exit:
218*54fd6939SJiyong Park /*
219*54fd6939SJiyong Park * Release the locks corresponding to each power level in the
220*54fd6939SJiyong Park * reverse order to which they were acquired.
221*54fd6939SJiyong Park */
222*54fd6939SJiyong Park psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
223*54fd6939SJiyong Park
224*54fd6939SJiyong Park if (skip_wfi == 1)
225*54fd6939SJiyong Park return;
226*54fd6939SJiyong Park
227*54fd6939SJiyong Park if (is_power_down_state != 0U) {
228*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
229*54fd6939SJiyong Park
230*54fd6939SJiyong Park /*
231*54fd6939SJiyong Park * Update the timestamp with cache off. We assume this
232*54fd6939SJiyong Park * timestamp can only be read from the current CPU and the
233*54fd6939SJiyong Park * timestamp cache line will be flushed before return to
234*54fd6939SJiyong Park * normal world on wakeup.
235*54fd6939SJiyong Park */
236*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
237*54fd6939SJiyong Park RT_INSTR_ENTER_HW_LOW_PWR,
238*54fd6939SJiyong Park PMF_NO_CACHE_MAINT);
239*54fd6939SJiyong Park #endif
240*54fd6939SJiyong Park
241*54fd6939SJiyong Park /* The function calls below must not return */
242*54fd6939SJiyong Park if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL)
243*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info);
244*54fd6939SJiyong Park else
245*54fd6939SJiyong Park psci_power_down_wfi();
246*54fd6939SJiyong Park }
247*54fd6939SJiyong Park
248*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
249*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
250*54fd6939SJiyong Park RT_INSTR_ENTER_HW_LOW_PWR,
251*54fd6939SJiyong Park PMF_NO_CACHE_MAINT);
252*54fd6939SJiyong Park #endif
253*54fd6939SJiyong Park
254*54fd6939SJiyong Park /*
255*54fd6939SJiyong Park * We will reach here if only retention/standby states have been
256*54fd6939SJiyong Park * requested at multiple power levels. This means that the cpu
257*54fd6939SJiyong Park * context will be preserved.
258*54fd6939SJiyong Park */
259*54fd6939SJiyong Park wfi();
260*54fd6939SJiyong Park
261*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
262*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
263*54fd6939SJiyong Park RT_INSTR_EXIT_HW_LOW_PWR,
264*54fd6939SJiyong Park PMF_NO_CACHE_MAINT);
265*54fd6939SJiyong Park #endif
266*54fd6939SJiyong Park
267*54fd6939SJiyong Park /*
268*54fd6939SJiyong Park * After we wake up from context retaining suspend, call the
269*54fd6939SJiyong Park * context retaining suspend finisher.
270*54fd6939SJiyong Park */
271*54fd6939SJiyong Park psci_suspend_to_standby_finisher(idx, end_pwrlvl);
272*54fd6939SJiyong Park }
273*54fd6939SJiyong Park
274*54fd6939SJiyong Park /*******************************************************************************
275*54fd6939SJiyong Park * The following functions finish an earlier suspend request. They
276*54fd6939SJiyong Park * are called by the common finisher routine in psci_common.c. The `state_info`
277*54fd6939SJiyong Park * is the psci_power_state from which this CPU has woken up from.
278*54fd6939SJiyong Park ******************************************************************************/
psci_cpu_suspend_finish(unsigned int cpu_idx,const psci_power_state_t * state_info)279*54fd6939SJiyong Park void psci_cpu_suspend_finish(unsigned int cpu_idx, const psci_power_state_t *state_info)
280*54fd6939SJiyong Park {
281*54fd6939SJiyong Park unsigned int counter_freq;
282*54fd6939SJiyong Park unsigned int max_off_lvl;
283*54fd6939SJiyong Park
284*54fd6939SJiyong Park /* Ensure we have been woken up from a suspended state */
285*54fd6939SJiyong Park assert((psci_get_aff_info_state() == AFF_STATE_ON) &&
286*54fd6939SJiyong Park (is_local_state_off(
287*54fd6939SJiyong Park state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0));
288*54fd6939SJiyong Park
289*54fd6939SJiyong Park /*
290*54fd6939SJiyong Park * Plat. management: Perform the platform specific actions
291*54fd6939SJiyong Park * before we change the state of the cpu e.g. enabling the
292*54fd6939SJiyong Park * gic or zeroing the mailbox register. If anything goes
293*54fd6939SJiyong Park * wrong then assert as there is no way to recover from this
294*54fd6939SJiyong Park * situation.
295*54fd6939SJiyong Park */
296*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
297*54fd6939SJiyong Park
298*54fd6939SJiyong Park #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
299*54fd6939SJiyong Park /* Arch. management: Enable the data cache, stack memory maintenance. */
300*54fd6939SJiyong Park psci_do_pwrup_cache_maintenance();
301*54fd6939SJiyong Park #endif
302*54fd6939SJiyong Park
303*54fd6939SJiyong Park /* Re-init the cntfrq_el0 register */
304*54fd6939SJiyong Park counter_freq = plat_get_syscnt_freq2();
305*54fd6939SJiyong Park write_cntfrq_el0(counter_freq);
306*54fd6939SJiyong Park
307*54fd6939SJiyong Park #if ENABLE_PAUTH
308*54fd6939SJiyong Park /* Store APIAKey_EL1 key */
309*54fd6939SJiyong Park set_cpu_data(apiakey[0], read_apiakeylo_el1());
310*54fd6939SJiyong Park set_cpu_data(apiakey[1], read_apiakeyhi_el1());
311*54fd6939SJiyong Park #endif /* ENABLE_PAUTH */
312*54fd6939SJiyong Park
313*54fd6939SJiyong Park /*
314*54fd6939SJiyong Park * Call the cpu suspend finish handler registered by the Secure Payload
315*54fd6939SJiyong Park * Dispatcher to let it do any bookeeping. If the handler encounters an
316*54fd6939SJiyong Park * error, it's expected to assert within
317*54fd6939SJiyong Park */
318*54fd6939SJiyong Park if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) {
319*54fd6939SJiyong Park max_off_lvl = psci_find_max_off_lvl(state_info);
320*54fd6939SJiyong Park assert(max_off_lvl != PSCI_INVALID_PWR_LVL);
321*54fd6939SJiyong Park psci_spd_pm->svc_suspend_finish(max_off_lvl);
322*54fd6939SJiyong Park }
323*54fd6939SJiyong Park
324*54fd6939SJiyong Park /* Invalidate the suspend level for the cpu */
325*54fd6939SJiyong Park psci_set_suspend_pwrlvl(PSCI_INVALID_PWR_LVL);
326*54fd6939SJiyong Park
327*54fd6939SJiyong Park PUBLISH_EVENT(psci_suspend_pwrdown_finish);
328*54fd6939SJiyong Park
329*54fd6939SJiyong Park /*
330*54fd6939SJiyong Park * Generic management: Now we just need to retrieve the
331*54fd6939SJiyong Park * information that we had stashed away during the suspend
332*54fd6939SJiyong Park * call to set this cpu on its way.
333*54fd6939SJiyong Park */
334*54fd6939SJiyong Park cm_prepare_el3_exit(NON_SECURE);
335*54fd6939SJiyong Park }
336