psci_off.c 5.24 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
10
 */

#include <arch.h>
#include <arch_helpers.h>
#include <assert.h>
#include <debug.h>
11
#include <platform.h>
dp-arm's avatar
dp-arm committed
12
13
#include <pmf.h>
#include <runtime_instr.h>
14
15
16
#include <string.h>
#include "psci_private.h"

17
18
19
20
21
/******************************************************************************
 * Construct the psci_power_state to request power OFF at all power levels.
 ******************************************************************************/
static void psci_set_power_off_state(psci_power_state_t *state_info)
{
22
	unsigned int lvl;
23
24
25
26
27

	for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++)
		state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE;
}

28
/******************************************************************************
29
 * Top level handler which is called when a cpu wants to power itself down.
30
31
32
33
34
35
36
37
38
39
 * It's assumed that along with turning the cpu power domain off, power
 * domains at higher levels will be turned off as far as possible. It finds
 * the highest level where a domain has to be powered off by traversing the
 * node information and then performs generic, architectural, platform setup
 * and state management required to turn OFF that power domain and domains
 * below it. e.g. For a cpu that's to be powered OFF, it could mean programming
 * the power controller whereas for a cluster that's to be powered off, it will
 * call the platform specific code which will disable coherency at the
 * interconnect level if the cpu is the last in the cluster and also the
 * program the power controller.
40
 ******************************************************************************/
41
int psci_do_cpu_off(unsigned int end_pwrlvl)
42
{
43
44
	int rc = PSCI_E_SUCCESS;
	int idx = (int) plat_my_core_pos();
45
	psci_power_state_t state_info;
46
47
48
49
50

	/*
	 * This function must only be called on platforms where the
	 * CPU_OFF platform hooks have been implemented.
	 */
51
	assert(psci_plat_pm_ops->pwr_domain_off != NULL);
52

53
54
55
	/* Construct the psci_power_state for CPU_OFF */
	psci_set_power_off_state(&state_info);

56
	/*
57
	 * This function acquires the lock corresponding to each power
58
59
60
	 * level so that by the time all locks are taken, the system topology
	 * is snapshot and state management can be done safely.
	 */
61
	psci_acquire_pwr_domain_locks(end_pwrlvl, idx);
62
63
64
65
66
67

	/*
	 * Call the cpu off handler registered by the Secure Payload Dispatcher
	 * to let it do any bookkeeping. Assume that the SPD always reports an
	 * E_DENIED error if SP refuse to power down
	 */
68
	if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) {
69
		rc = psci_spd_pm->svc_off(0);
70
		if (rc != 0)
71
72
73
74
			goto exit;
	}

	/*
75
76
77
	 * This function is passed the requested state info and
	 * it returns the negotiated state info for each power level upto
	 * the end level specified.
78
	 */
79
	psci_do_state_coordination(end_pwrlvl, &state_info);
80

81
82
83
84
85
#if ENABLE_PSCI_STAT
	/* Update the last cpu for each level till end_pwrlvl */
	psci_stats_update_pwr_down(end_pwrlvl, &state_info);
#endif

86
87
88
89
90
91
92
93
94
95
96
#if ENABLE_RUNTIME_INSTRUMENTATION

	/*
	 * Flush cache line so that even if CPU power down happens
	 * the timestamp update is reflected in memory.
	 */
	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
		RT_INSTR_ENTER_CFLUSH,
		PMF_CACHE_MAINT);
#endif

97
	/*
98
	 * Arch. management. Initiate power down sequence.
99
	 */
100
	psci_do_pwrdown_sequence(psci_find_max_off_lvl(&state_info));
101

102
103
104
105
106
107
#if ENABLE_RUNTIME_INSTRUMENTATION
	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
		RT_INSTR_EXIT_CFLUSH,
		PMF_NO_CACHE_MAINT);
#endif

108
	/*
109
110
	 * Plat. management: Perform platform specific actions to turn this
	 * cpu off e.g. exit cpu coherency, program the power controller etc.
111
	 */
112
	psci_plat_pm_ops->pwr_domain_off(&state_info);
113

114
#if ENABLE_PSCI_STAT
115
	plat_psci_stat_accounting_start(&state_info);
116
117
#endif

118
119
exit:
	/*
120
	 * Release the locks corresponding to each power level in the
121
122
	 * reverse order to which they were acquired.
	 */
123
	psci_release_pwr_domain_locks(end_pwrlvl, idx);
124
125
126

	/*
	 * Check if all actions needed to safely power down this cpu have
127
	 * successfully completed.
128
	 */
129
130
	if (rc == PSCI_E_SUCCESS) {
		/*
131
132
		 * Set the affinity info state to OFF. When caches are disabled,
		 * this writes directly to main memory, so cache maintenance is
133
		 * required to ensure that later cached reads of aff_info_state
134
		 * return AFF_STATE_OFF. A dsbish() ensures ordering of the
135
136
		 * update to the affinity info state prior to cache line
		 * invalidation.
137
		 */
138
		psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state);
139
		psci_set_aff_info_state(AFF_STATE_OFF);
140
141
		psci_dsbish();
		psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state);
142

dp-arm's avatar
dp-arm committed
143
144
145
146
147
148
149
150
151
152
153
154
155
#if ENABLE_RUNTIME_INSTRUMENTATION

		/*
		 * Update the timestamp with cache off.  We assume this
		 * timestamp can only be read from the current CPU and the
		 * timestamp cache line will be flushed before return to
		 * normal world on wakeup.
		 */
		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
		    RT_INSTR_ENTER_HW_LOW_PWR,
		    PMF_NO_CACHE_MAINT);
#endif

156
		if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) {
157
158
159
160
161
162
163
164
165
			/* This function must not return */
			psci_plat_pm_ops->pwr_domain_pwr_down_wfi(&state_info);
		} else {
			/*
			 * Enter a wfi loop which will allow the power
			 * controller to physically power down this cpu.
			 */
			psci_power_down_wfi();
		}
166
	}
167
168
169

	return rc;
}