1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-2019, 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 <string.h>
9*54fd6939SJiyong Park
10*54fd6939SJiyong Park #include <arch.h>
11*54fd6939SJiyong Park #include <arch_helpers.h>
12*54fd6939SJiyong Park #include <common/debug.h>
13*54fd6939SJiyong Park #include <lib/pmf/pmf.h>
14*54fd6939SJiyong Park #include <lib/runtime_instr.h>
15*54fd6939SJiyong Park #include <plat/common/platform.h>
16*54fd6939SJiyong Park
17*54fd6939SJiyong Park #include "psci_private.h"
18*54fd6939SJiyong Park
19*54fd6939SJiyong Park /******************************************************************************
20*54fd6939SJiyong Park * Construct the psci_power_state to request power OFF at all power levels.
21*54fd6939SJiyong Park ******************************************************************************/
psci_set_power_off_state(psci_power_state_t * state_info)22*54fd6939SJiyong Park static void psci_set_power_off_state(psci_power_state_t *state_info)
23*54fd6939SJiyong Park {
24*54fd6939SJiyong Park unsigned int lvl;
25*54fd6939SJiyong Park
26*54fd6939SJiyong Park for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++)
27*54fd6939SJiyong Park state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE;
28*54fd6939SJiyong Park }
29*54fd6939SJiyong Park
30*54fd6939SJiyong Park /******************************************************************************
31*54fd6939SJiyong Park * Top level handler which is called when a cpu wants to power itself down.
32*54fd6939SJiyong Park * It's assumed that along with turning the cpu power domain off, power
33*54fd6939SJiyong Park * domains at higher levels will be turned off as far as possible. It finds
34*54fd6939SJiyong Park * the highest level where a domain has to be powered off by traversing the
35*54fd6939SJiyong Park * node information and then performs generic, architectural, platform setup
36*54fd6939SJiyong Park * and state management required to turn OFF that power domain and domains
37*54fd6939SJiyong Park * below it. e.g. For a cpu that's to be powered OFF, it could mean programming
38*54fd6939SJiyong Park * the power controller whereas for a cluster that's to be powered off, it will
39*54fd6939SJiyong Park * call the platform specific code which will disable coherency at the
40*54fd6939SJiyong Park * interconnect level if the cpu is the last in the cluster and also the
41*54fd6939SJiyong Park * program the power controller.
42*54fd6939SJiyong Park ******************************************************************************/
psci_do_cpu_off(unsigned int end_pwrlvl)43*54fd6939SJiyong Park int psci_do_cpu_off(unsigned int end_pwrlvl)
44*54fd6939SJiyong Park {
45*54fd6939SJiyong Park int rc = PSCI_E_SUCCESS;
46*54fd6939SJiyong Park unsigned int idx = plat_my_core_pos();
47*54fd6939SJiyong Park psci_power_state_t state_info;
48*54fd6939SJiyong Park unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
49*54fd6939SJiyong Park
50*54fd6939SJiyong Park /*
51*54fd6939SJiyong Park * This function must only be called on platforms where the
52*54fd6939SJiyong Park * CPU_OFF platform hooks have been implemented.
53*54fd6939SJiyong Park */
54*54fd6939SJiyong Park assert(psci_plat_pm_ops->pwr_domain_off != NULL);
55*54fd6939SJiyong Park
56*54fd6939SJiyong Park /* Construct the psci_power_state for CPU_OFF */
57*54fd6939SJiyong Park psci_set_power_off_state(&state_info);
58*54fd6939SJiyong Park
59*54fd6939SJiyong Park /*
60*54fd6939SJiyong Park * Get the parent nodes here, this is important to do before we
61*54fd6939SJiyong Park * initiate the power down sequence as after that point the core may
62*54fd6939SJiyong Park * have exited coherency and its cache may be disabled, any access to
63*54fd6939SJiyong Park * shared memory after that (such as the parent node lookup in
64*54fd6939SJiyong Park * psci_cpu_pd_nodes) can cause coherency issues on some platforms.
65*54fd6939SJiyong Park */
66*54fd6939SJiyong Park psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes);
67*54fd6939SJiyong Park
68*54fd6939SJiyong Park /*
69*54fd6939SJiyong Park * This function acquires the lock corresponding to each power
70*54fd6939SJiyong Park * level so that by the time all locks are taken, the system topology
71*54fd6939SJiyong Park * is snapshot and state management can be done safely.
72*54fd6939SJiyong Park */
73*54fd6939SJiyong Park psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
74*54fd6939SJiyong Park
75*54fd6939SJiyong Park /*
76*54fd6939SJiyong Park * Call the cpu off handler registered by the Secure Payload Dispatcher
77*54fd6939SJiyong Park * to let it do any bookkeeping. Assume that the SPD always reports an
78*54fd6939SJiyong Park * E_DENIED error if SP refuse to power down
79*54fd6939SJiyong Park */
80*54fd6939SJiyong Park if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) {
81*54fd6939SJiyong Park rc = psci_spd_pm->svc_off(0);
82*54fd6939SJiyong Park if (rc != 0)
83*54fd6939SJiyong Park goto exit;
84*54fd6939SJiyong Park }
85*54fd6939SJiyong Park
86*54fd6939SJiyong Park /*
87*54fd6939SJiyong Park * This function is passed the requested state info and
88*54fd6939SJiyong Park * it returns the negotiated state info for each power level upto
89*54fd6939SJiyong Park * the end level specified.
90*54fd6939SJiyong Park */
91*54fd6939SJiyong Park psci_do_state_coordination(end_pwrlvl, &state_info);
92*54fd6939SJiyong Park
93*54fd6939SJiyong Park #if ENABLE_PSCI_STAT
94*54fd6939SJiyong Park /* Update the last cpu for each level till end_pwrlvl */
95*54fd6939SJiyong Park psci_stats_update_pwr_down(end_pwrlvl, &state_info);
96*54fd6939SJiyong Park #endif
97*54fd6939SJiyong Park
98*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
99*54fd6939SJiyong Park
100*54fd6939SJiyong Park /*
101*54fd6939SJiyong Park * Flush cache line so that even if CPU power down happens
102*54fd6939SJiyong Park * the timestamp update is reflected in memory.
103*54fd6939SJiyong Park */
104*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
105*54fd6939SJiyong Park RT_INSTR_ENTER_CFLUSH,
106*54fd6939SJiyong Park PMF_CACHE_MAINT);
107*54fd6939SJiyong Park #endif
108*54fd6939SJiyong Park
109*54fd6939SJiyong Park /*
110*54fd6939SJiyong Park * Arch. management. Initiate power down sequence.
111*54fd6939SJiyong Park */
112*54fd6939SJiyong Park psci_do_pwrdown_sequence(psci_find_max_off_lvl(&state_info));
113*54fd6939SJiyong Park
114*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
115*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
116*54fd6939SJiyong Park RT_INSTR_EXIT_CFLUSH,
117*54fd6939SJiyong Park PMF_NO_CACHE_MAINT);
118*54fd6939SJiyong Park #endif
119*54fd6939SJiyong Park
120*54fd6939SJiyong Park /*
121*54fd6939SJiyong Park * Plat. management: Perform platform specific actions to turn this
122*54fd6939SJiyong Park * cpu off e.g. exit cpu coherency, program the power controller etc.
123*54fd6939SJiyong Park */
124*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_off(&state_info);
125*54fd6939SJiyong Park
126*54fd6939SJiyong Park #if ENABLE_PSCI_STAT
127*54fd6939SJiyong Park plat_psci_stat_accounting_start(&state_info);
128*54fd6939SJiyong Park #endif
129*54fd6939SJiyong Park
130*54fd6939SJiyong Park exit:
131*54fd6939SJiyong Park /*
132*54fd6939SJiyong Park * Release the locks corresponding to each power level in the
133*54fd6939SJiyong Park * reverse order to which they were acquired.
134*54fd6939SJiyong Park */
135*54fd6939SJiyong Park psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
136*54fd6939SJiyong Park
137*54fd6939SJiyong Park /*
138*54fd6939SJiyong Park * Check if all actions needed to safely power down this cpu have
139*54fd6939SJiyong Park * successfully completed.
140*54fd6939SJiyong Park */
141*54fd6939SJiyong Park if (rc == PSCI_E_SUCCESS) {
142*54fd6939SJiyong Park /*
143*54fd6939SJiyong Park * Set the affinity info state to OFF. When caches are disabled,
144*54fd6939SJiyong Park * this writes directly to main memory, so cache maintenance is
145*54fd6939SJiyong Park * required to ensure that later cached reads of aff_info_state
146*54fd6939SJiyong Park * return AFF_STATE_OFF. A dsbish() ensures ordering of the
147*54fd6939SJiyong Park * update to the affinity info state prior to cache line
148*54fd6939SJiyong Park * invalidation.
149*54fd6939SJiyong Park */
150*54fd6939SJiyong Park psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state);
151*54fd6939SJiyong Park psci_set_aff_info_state(AFF_STATE_OFF);
152*54fd6939SJiyong Park psci_dsbish();
153*54fd6939SJiyong Park psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state);
154*54fd6939SJiyong Park
155*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
156*54fd6939SJiyong Park
157*54fd6939SJiyong Park /*
158*54fd6939SJiyong Park * Update the timestamp with cache off. We assume this
159*54fd6939SJiyong Park * timestamp can only be read from the current CPU and the
160*54fd6939SJiyong Park * timestamp cache line will be flushed before return to
161*54fd6939SJiyong Park * normal world on wakeup.
162*54fd6939SJiyong Park */
163*54fd6939SJiyong Park PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
164*54fd6939SJiyong Park RT_INSTR_ENTER_HW_LOW_PWR,
165*54fd6939SJiyong Park PMF_NO_CACHE_MAINT);
166*54fd6939SJiyong Park #endif
167*54fd6939SJiyong Park
168*54fd6939SJiyong Park if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) {
169*54fd6939SJiyong Park /* This function must not return */
170*54fd6939SJiyong Park psci_plat_pm_ops->pwr_domain_pwr_down_wfi(&state_info);
171*54fd6939SJiyong Park } else {
172*54fd6939SJiyong Park /*
173*54fd6939SJiyong Park * Enter a wfi loop which will allow the power
174*54fd6939SJiyong Park * controller to physically power down this cpu.
175*54fd6939SJiyong Park */
176*54fd6939SJiyong Park psci_power_down_wfi();
177*54fd6939SJiyong Park }
178*54fd6939SJiyong Park }
179*54fd6939SJiyong Park
180*54fd6939SJiyong Park return rc;
181*54fd6939SJiyong Park }
182