psci_stat.c 7.65 KB
Newer Older
1
/*
2
 * Copyright (c) 2016-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
 */

#include <assert.h>
8

9
#include <platform_def.h>
10
11
12
13

#include <common/debug.h>
#include <plat/common/platform.h>

14
15
16
#include "psci_private.h"

#ifndef PLAT_MAX_PWR_LVL_STATES
17
#define PLAT_MAX_PWR_LVL_STATES		2U
18
19
20
21
22
23
24
25
26
27
28
29
#endif

/* Following structure is used for PSCI STAT */
typedef struct psci_stat {
	u_register_t residency;
	u_register_t count;
} psci_stat_t;

/*
 * Following is used to keep track of the last cpu
 * that goes to power down in non cpu power domains.
 */
30
static int last_cpu_in_non_cpu_pd[PSCI_NUM_NON_CPU_PWR_DOMAINS] = {
31
		[0 ... PSCI_NUM_NON_CPU_PWR_DOMAINS - 1] = -1};
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

/*
 * Following are used to store PSCI STAT values for
 * CPU and non CPU power domains.
 */
static psci_stat_t psci_cpu_stat[PLATFORM_CORE_COUNT]
				[PLAT_MAX_PWR_LVL_STATES];
static psci_stat_t psci_non_cpu_stat[PSCI_NUM_NON_CPU_PWR_DOMAINS]
				[PLAT_MAX_PWR_LVL_STATES];

/*
 * This functions returns the index into the `psci_stat_t` array given the
 * local power state and power domain level. If the platform implements the
 * `get_pwr_lvl_state_idx` pm hook, then that will be used to return the index.
 */
47
static int get_stat_idx(plat_local_state_t local_state, unsigned int pwr_lvl)
48
49
50
51
{
	int idx;

	if (psci_plat_pm_ops->get_pwr_lvl_state_idx == NULL) {
52
53
		assert(PLAT_MAX_PWR_LVL_STATES == 2U);
		if (is_local_state_retn(local_state) != 0)
54
55
			return 0;

56
		assert(is_local_state_off(local_state) != 0);
57
58
59
60
		return 1;
	}

	idx = psci_plat_pm_ops->get_pwr_lvl_state_idx(local_state, pwr_lvl);
61
	assert((idx >= 0) && (idx < (int) PLAT_MAX_PWR_LVL_STATES));
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	return idx;
}

/*******************************************************************************
 * This function is passed the target local power states for each power
 * domain (state_info) between the current CPU domain and its ancestors until
 * the target power level (end_pwrlvl).
 *
 * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
 * updates the `last_cpu_in_non_cpu_pd[]` with last power down cpu id.
 *
 * This function will only be invoked with data cache enabled and while
 * powering down a core.
 ******************************************************************************/
void psci_stats_update_pwr_down(unsigned int end_pwrlvl,
			const psci_power_state_t *state_info)
{
79
80
	unsigned int lvl, parent_idx;
	int cpu_idx = (int) plat_my_core_pos();
81
82

	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
83
	assert(state_info != NULL);
84
85
86

	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;

87
	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
88
89

		/* Break early if the target power state is RUN */
90
		if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
			break;

		/*
		 * The power domain is entering a low power state, so this is
		 * the last CPU for this power domain
		 */
		last_cpu_in_non_cpu_pd[parent_idx] = cpu_idx;

		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
	}

}

/*******************************************************************************
 * This function updates the PSCI STATS(residency time and count) for CPU
 * and NON-CPU power domains.
 * It is called with caches enabled and locks acquired(for NON-CPU domain)
 ******************************************************************************/
void psci_stats_update_pwr_up(unsigned int end_pwrlvl,
110
			const psci_power_state_t *state_info)
111
{
112
113
	unsigned int lvl, parent_idx;
	int cpu_idx = (int) plat_my_core_pos();
Etienne Carriere's avatar
Etienne Carriere committed
114
	int stat_idx;
115
116
117
118
	plat_local_state_t local_state;
	u_register_t residency;

	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
119
	assert(state_info != NULL);
120
121
122
123
124

	/* Get the index into the stats array */
	local_state = state_info->pwr_domain_state[PSCI_CPU_PWR_LVL];
	stat_idx = get_stat_idx(local_state, PSCI_CPU_PWR_LVL);

125
126
127
	/* Call into platform interface to calculate residency. */
	residency = plat_psci_stat_get_residency(PSCI_CPU_PWR_LVL,
	    state_info, cpu_idx);
128
129
130
131
132
133
134
135
136
137

	/* Update CPU stats. */
	psci_cpu_stat[cpu_idx][stat_idx].residency += residency;
	psci_cpu_stat[cpu_idx][stat_idx].count++;

	/*
	 * Check what power domains above CPU were off
	 * prior to this CPU powering on.
	 */
	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
138
139
140
141
	/* Return early if this is the first power up. */
	if (last_cpu_in_non_cpu_pd[parent_idx] == -1)
		return;

142
	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
143
		local_state = state_info->pwr_domain_state[lvl];
144
		if (is_local_state_run(local_state) != 0) {
145
146
147
148
149
150
			/* Break early */
			break;
		}

		assert(last_cpu_in_non_cpu_pd[parent_idx] != -1);

151
152
		/* Call into platform interface to calculate residency. */
		residency = plat_psci_stat_get_residency(lvl, state_info,
153
					last_cpu_in_non_cpu_pd[parent_idx]);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

		/* Initialize back to reset value */
		last_cpu_in_non_cpu_pd[parent_idx] = -1;

		/* Get the index into the stats array */
		stat_idx = get_stat_idx(local_state, lvl);

		/* Update non cpu stats */
		psci_non_cpu_stat[parent_idx][stat_idx].residency += residency;
		psci_non_cpu_stat[parent_idx][stat_idx].count++;

		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
	}

}

/*******************************************************************************
 * This function returns the appropriate count and residency time of the
 * local state for the highest power level expressed in the `power_state`
 * for the node represented by `target_cpu`.
 ******************************************************************************/
Etienne Carriere's avatar
Etienne Carriere committed
175
static int psci_get_stat(u_register_t target_cpu, unsigned int power_state,
176
177
			 psci_stat_t *psci_stat)
{
Etienne Carriere's avatar
Etienne Carriere committed
178
	int rc;
179
180
	unsigned int pwrlvl, lvl, parent_idx, target_idx;
	int stat_idx;
181
182
183
184
	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
	plat_local_state_t local_state;

	/* Validate the target_cpu parameter and determine the cpu index */
185
186
	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_cpu);
	if (target_idx == (unsigned int) -1)
187
188
189
		return PSCI_E_INVALID_PARAMS;

	/* Validate the power_state parameter */
190
	if (psci_plat_pm_ops->translate_power_state_by_mpidr == NULL)
191
192
193
194
195
196
197
198
199
200
		rc = psci_validate_power_state(power_state, &state_info);
	else
		rc = psci_plat_pm_ops->translate_power_state_by_mpidr(
				target_cpu, power_state, &state_info);

	if (rc != PSCI_E_SUCCESS)
		return PSCI_E_INVALID_PARAMS;

	/* Find the highest power level */
	pwrlvl = psci_find_target_suspend_lvl(&state_info);
201
202
203
204
	if (pwrlvl == PSCI_INVALID_PWR_LVL) {
		ERROR("Invalid target power level for PSCI statistics operation\n");
		panic();
	}
205
206
207
208
209
210
211

	/* Get the index into the stats array */
	local_state = state_info.pwr_domain_state[pwrlvl];
	stat_idx = get_stat_idx(local_state, pwrlvl);

	if (pwrlvl > PSCI_CPU_PWR_LVL) {
		/* Get the power domain index */
212
		parent_idx = SPECULATION_SAFE_VALUE(psci_cpu_pd_nodes[target_idx].parent_node);
213
		for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl < pwrlvl; lvl++)
214
			parent_idx = SPECULATION_SAFE_VALUE(psci_non_cpu_pd_nodes[parent_idx].parent_node);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

		/* Get the non cpu power domain stats */
		*psci_stat = psci_non_cpu_stat[parent_idx][stat_idx];
	} else {
		/* Get the cpu power domain stats */
		*psci_stat = psci_cpu_stat[target_idx][stat_idx];
	}

	return PSCI_E_SUCCESS;
}

/* This is the top level function for PSCI_STAT_RESIDENCY SMC. */
u_register_t psci_stat_residency(u_register_t target_cpu,
		unsigned int power_state)
{
	psci_stat_t psci_stat;
	int rc = psci_get_stat(target_cpu, power_state, &psci_stat);
Etienne Carriere's avatar
Etienne Carriere committed
232

233
234
235
236
237
238
239
240
241
242
243
244
	if (rc == PSCI_E_SUCCESS)
		return psci_stat.residency;
	else
		return 0;
}

/* This is the top level function for PSCI_STAT_COUNT SMC. */
u_register_t psci_stat_count(u_register_t target_cpu,
	unsigned int power_state)
{
	psci_stat_t psci_stat;
	int rc = psci_get_stat(target_cpu, power_state, &psci_stat);
Etienne Carriere's avatar
Etienne Carriere committed
245

246
247
248
249
250
	if (rc == PSCI_E_SUCCESS)
		return psci_stat.count;
	else
		return 0;
}