xref: /rk3399_ARM-atf/lib/psci/psci_off.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1532ed618SSoby Mathew /*
2621d64f8SAntonio Nino Diaz  * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7*09d40e0eSAntonio Nino Diaz #include <assert.h>
8*09d40e0eSAntonio Nino Diaz #include <string.h>
9*09d40e0eSAntonio Nino Diaz 
10532ed618SSoby Mathew #include <arch.h>
11532ed618SSoby Mathew #include <arch_helpers.h>
12*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
13*09d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
14*09d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
15*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
16*09d40e0eSAntonio Nino Diaz 
17532ed618SSoby Mathew #include "psci_private.h"
18532ed618SSoby Mathew 
19532ed618SSoby Mathew /******************************************************************************
20532ed618SSoby Mathew  * Construct the psci_power_state to request power OFF at all power levels.
21532ed618SSoby Mathew  ******************************************************************************/
22532ed618SSoby Mathew static void psci_set_power_off_state(psci_power_state_t *state_info)
23532ed618SSoby Mathew {
246311f63dSVarun Wadekar 	unsigned int lvl;
25532ed618SSoby Mathew 
26532ed618SSoby Mathew 	for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++)
27532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE;
28532ed618SSoby Mathew }
29532ed618SSoby Mathew 
30532ed618SSoby Mathew /******************************************************************************
31532ed618SSoby Mathew  * Top level handler which is called when a cpu wants to power itself down.
32532ed618SSoby Mathew  * It's assumed that along with turning the cpu power domain off, power
33532ed618SSoby Mathew  * domains at higher levels will be turned off as far as possible. It finds
34532ed618SSoby Mathew  * the highest level where a domain has to be powered off by traversing the
35532ed618SSoby Mathew  * node information and then performs generic, architectural, platform setup
36532ed618SSoby Mathew  * and state management required to turn OFF that power domain and domains
37532ed618SSoby Mathew  * below it. e.g. For a cpu that's to be powered OFF, it could mean programming
38532ed618SSoby Mathew  * the power controller whereas for a cluster that's to be powered off, it will
39532ed618SSoby Mathew  * call the platform specific code which will disable coherency at the
40532ed618SSoby Mathew  * interconnect level if the cpu is the last in the cluster and also the
41532ed618SSoby Mathew  * program the power controller.
42532ed618SSoby Mathew  ******************************************************************************/
43532ed618SSoby Mathew int psci_do_cpu_off(unsigned int end_pwrlvl)
44532ed618SSoby Mathew {
45621d64f8SAntonio Nino Diaz 	int rc = PSCI_E_SUCCESS;
46621d64f8SAntonio Nino Diaz 	int idx = (int) plat_my_core_pos();
47532ed618SSoby Mathew 	psci_power_state_t state_info;
48532ed618SSoby Mathew 
49532ed618SSoby Mathew 	/*
50532ed618SSoby Mathew 	 * This function must only be called on platforms where the
51532ed618SSoby Mathew 	 * CPU_OFF platform hooks have been implemented.
52532ed618SSoby Mathew 	 */
53621d64f8SAntonio Nino Diaz 	assert(psci_plat_pm_ops->pwr_domain_off != NULL);
54532ed618SSoby Mathew 
55216e58a3SRoberto Vargas 	/* Construct the psci_power_state for CPU_OFF */
56216e58a3SRoberto Vargas 	psci_set_power_off_state(&state_info);
57216e58a3SRoberto Vargas 
58532ed618SSoby Mathew 	/*
59532ed618SSoby Mathew 	 * This function acquires the lock corresponding to each power
60532ed618SSoby Mathew 	 * level so that by the time all locks are taken, the system topology
61532ed618SSoby Mathew 	 * is snapshot and state management can be done safely.
62532ed618SSoby Mathew 	 */
63621d64f8SAntonio Nino Diaz 	psci_acquire_pwr_domain_locks(end_pwrlvl, idx);
64532ed618SSoby Mathew 
65532ed618SSoby Mathew 	/*
66532ed618SSoby Mathew 	 * Call the cpu off handler registered by the Secure Payload Dispatcher
67532ed618SSoby Mathew 	 * to let it do any bookkeeping. Assume that the SPD always reports an
68532ed618SSoby Mathew 	 * E_DENIED error if SP refuse to power down
69532ed618SSoby Mathew 	 */
70621d64f8SAntonio Nino Diaz 	if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) {
71532ed618SSoby Mathew 		rc = psci_spd_pm->svc_off(0);
72621d64f8SAntonio Nino Diaz 		if (rc != 0)
73532ed618SSoby Mathew 			goto exit;
74532ed618SSoby Mathew 	}
75532ed618SSoby Mathew 
76532ed618SSoby Mathew 	/*
77532ed618SSoby Mathew 	 * This function is passed the requested state info and
78532ed618SSoby Mathew 	 * it returns the negotiated state info for each power level upto
79532ed618SSoby Mathew 	 * the end level specified.
80532ed618SSoby Mathew 	 */
81532ed618SSoby Mathew 	psci_do_state_coordination(end_pwrlvl, &state_info);
82532ed618SSoby Mathew 
83532ed618SSoby Mathew #if ENABLE_PSCI_STAT
84532ed618SSoby Mathew 	/* Update the last cpu for each level till end_pwrlvl */
85532ed618SSoby Mathew 	psci_stats_update_pwr_down(end_pwrlvl, &state_info);
86532ed618SSoby Mathew #endif
87532ed618SSoby Mathew 
887941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
897941816aSdp-arm 
907941816aSdp-arm 	/*
917941816aSdp-arm 	 * Flush cache line so that even if CPU power down happens
927941816aSdp-arm 	 * the timestamp update is reflected in memory.
937941816aSdp-arm 	 */
947941816aSdp-arm 	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
957941816aSdp-arm 		RT_INSTR_ENTER_CFLUSH,
967941816aSdp-arm 		PMF_CACHE_MAINT);
977941816aSdp-arm #endif
987941816aSdp-arm 
99532ed618SSoby Mathew 	/*
100b0408e87SJeenu Viswambharan 	 * Arch. management. Initiate power down sequence.
101532ed618SSoby Mathew 	 */
102b0408e87SJeenu Viswambharan 	psci_do_pwrdown_sequence(psci_find_max_off_lvl(&state_info));
103532ed618SSoby Mathew 
1047941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
1057941816aSdp-arm 	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
1067941816aSdp-arm 		RT_INSTR_EXIT_CFLUSH,
1077941816aSdp-arm 		PMF_NO_CACHE_MAINT);
1087941816aSdp-arm #endif
1097941816aSdp-arm 
110532ed618SSoby Mathew 	/*
111532ed618SSoby Mathew 	 * Plat. management: Perform platform specific actions to turn this
112532ed618SSoby Mathew 	 * cpu off e.g. exit cpu coherency, program the power controller etc.
113532ed618SSoby Mathew 	 */
114532ed618SSoby Mathew 	psci_plat_pm_ops->pwr_domain_off(&state_info);
115532ed618SSoby Mathew 
116532ed618SSoby Mathew #if ENABLE_PSCI_STAT
11704c1db1eSdp-arm 	plat_psci_stat_accounting_start(&state_info);
118532ed618SSoby Mathew #endif
119532ed618SSoby Mathew 
120532ed618SSoby Mathew exit:
121532ed618SSoby Mathew 	/*
122532ed618SSoby Mathew 	 * Release the locks corresponding to each power level in the
123532ed618SSoby Mathew 	 * reverse order to which they were acquired.
124532ed618SSoby Mathew 	 */
125621d64f8SAntonio Nino Diaz 	psci_release_pwr_domain_locks(end_pwrlvl, idx);
126532ed618SSoby Mathew 
127532ed618SSoby Mathew 	/*
128532ed618SSoby Mathew 	 * Check if all actions needed to safely power down this cpu have
129532ed618SSoby Mathew 	 * successfully completed.
130532ed618SSoby Mathew 	 */
131532ed618SSoby Mathew 	if (rc == PSCI_E_SUCCESS) {
132532ed618SSoby Mathew 		/*
133a10d3632SJeenu Viswambharan 		 * Set the affinity info state to OFF. When caches are disabled,
134a10d3632SJeenu Viswambharan 		 * this writes directly to main memory, so cache maintenance is
135532ed618SSoby Mathew 		 * required to ensure that later cached reads of aff_info_state
136532ed618SSoby Mathew 		 * return AFF_STATE_OFF. A dsbish() ensures ordering of the
137532ed618SSoby Mathew 		 * update to the affinity info state prior to cache line
138532ed618SSoby Mathew 		 * invalidation.
139532ed618SSoby Mathew 		 */
140a10d3632SJeenu Viswambharan 		psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state);
141532ed618SSoby Mathew 		psci_set_aff_info_state(AFF_STATE_OFF);
142a10d3632SJeenu Viswambharan 		psci_dsbish();
143a10d3632SJeenu Viswambharan 		psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state);
144532ed618SSoby Mathew 
145872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
146872be88aSdp-arm 
147872be88aSdp-arm 		/*
148872be88aSdp-arm 		 * Update the timestamp with cache off.  We assume this
149872be88aSdp-arm 		 * timestamp can only be read from the current CPU and the
150872be88aSdp-arm 		 * timestamp cache line will be flushed before return to
151872be88aSdp-arm 		 * normal world on wakeup.
152872be88aSdp-arm 		 */
153872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
154872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
155872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
156872be88aSdp-arm #endif
157872be88aSdp-arm 
158621d64f8SAntonio Nino Diaz 		if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) {
159532ed618SSoby Mathew 			/* This function must not return */
160532ed618SSoby Mathew 			psci_plat_pm_ops->pwr_domain_pwr_down_wfi(&state_info);
161532ed618SSoby Mathew 		} else {
162532ed618SSoby Mathew 			/*
163532ed618SSoby Mathew 			 * Enter a wfi loop which will allow the power
164532ed618SSoby Mathew 			 * controller to physically power down this cpu.
165532ed618SSoby Mathew 			 */
166532ed618SSoby Mathew 			psci_power_down_wfi();
167532ed618SSoby Mathew 		}
168532ed618SSoby Mathew 	}
169532ed618SSoby Mathew 
170532ed618SSoby Mathew 	return rc;
171532ed618SSoby Mathew }
172