xref: /aosp_15_r20/external/arm-trusted-firmware/lib/psci/psci_stat.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2016-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 
9*54fd6939SJiyong Park #include <platform_def.h>
10*54fd6939SJiyong Park 
11*54fd6939SJiyong Park #include <common/debug.h>
12*54fd6939SJiyong Park #include <plat/common/platform.h>
13*54fd6939SJiyong Park 
14*54fd6939SJiyong Park #include "psci_private.h"
15*54fd6939SJiyong Park 
16*54fd6939SJiyong Park #ifndef PLAT_MAX_PWR_LVL_STATES
17*54fd6939SJiyong Park #define PLAT_MAX_PWR_LVL_STATES		2U
18*54fd6939SJiyong Park #endif
19*54fd6939SJiyong Park 
20*54fd6939SJiyong Park /* Following structure is used for PSCI STAT */
21*54fd6939SJiyong Park typedef struct psci_stat {
22*54fd6939SJiyong Park 	u_register_t residency;
23*54fd6939SJiyong Park 	u_register_t count;
24*54fd6939SJiyong Park } psci_stat_t;
25*54fd6939SJiyong Park 
26*54fd6939SJiyong Park /*
27*54fd6939SJiyong Park  * Following is used to keep track of the last cpu
28*54fd6939SJiyong Park  * that goes to power down in non cpu power domains.
29*54fd6939SJiyong Park  */
30*54fd6939SJiyong Park static int last_cpu_in_non_cpu_pd[PSCI_NUM_NON_CPU_PWR_DOMAINS] = {
31*54fd6939SJiyong Park 		[0 ... PSCI_NUM_NON_CPU_PWR_DOMAINS - 1U] = -1};
32*54fd6939SJiyong Park 
33*54fd6939SJiyong Park /*
34*54fd6939SJiyong Park  * Following are used to store PSCI STAT values for
35*54fd6939SJiyong Park  * CPU and non CPU power domains.
36*54fd6939SJiyong Park  */
37*54fd6939SJiyong Park static psci_stat_t psci_cpu_stat[PLATFORM_CORE_COUNT]
38*54fd6939SJiyong Park 				[PLAT_MAX_PWR_LVL_STATES];
39*54fd6939SJiyong Park static psci_stat_t psci_non_cpu_stat[PSCI_NUM_NON_CPU_PWR_DOMAINS]
40*54fd6939SJiyong Park 				[PLAT_MAX_PWR_LVL_STATES];
41*54fd6939SJiyong Park 
42*54fd6939SJiyong Park /*
43*54fd6939SJiyong Park  * This functions returns the index into the `psci_stat_t` array given the
44*54fd6939SJiyong Park  * local power state and power domain level. If the platform implements the
45*54fd6939SJiyong Park  * `get_pwr_lvl_state_idx` pm hook, then that will be used to return the index.
46*54fd6939SJiyong Park  */
get_stat_idx(plat_local_state_t local_state,unsigned int pwr_lvl)47*54fd6939SJiyong Park static int get_stat_idx(plat_local_state_t local_state, unsigned int pwr_lvl)
48*54fd6939SJiyong Park {
49*54fd6939SJiyong Park 	int idx;
50*54fd6939SJiyong Park 
51*54fd6939SJiyong Park 	if (psci_plat_pm_ops->get_pwr_lvl_state_idx == NULL) {
52*54fd6939SJiyong Park 		assert(PLAT_MAX_PWR_LVL_STATES == 2U);
53*54fd6939SJiyong Park 		if (is_local_state_retn(local_state) != 0)
54*54fd6939SJiyong Park 			return 0;
55*54fd6939SJiyong Park 
56*54fd6939SJiyong Park 		assert(is_local_state_off(local_state) != 0);
57*54fd6939SJiyong Park 		return 1;
58*54fd6939SJiyong Park 	}
59*54fd6939SJiyong Park 
60*54fd6939SJiyong Park 	idx = psci_plat_pm_ops->get_pwr_lvl_state_idx(local_state, pwr_lvl);
61*54fd6939SJiyong Park 	assert((idx >= 0) && (idx < (int) PLAT_MAX_PWR_LVL_STATES));
62*54fd6939SJiyong Park 	return idx;
63*54fd6939SJiyong Park }
64*54fd6939SJiyong Park 
65*54fd6939SJiyong Park /*******************************************************************************
66*54fd6939SJiyong Park  * This function is passed the target local power states for each power
67*54fd6939SJiyong Park  * domain (state_info) between the current CPU domain and its ancestors until
68*54fd6939SJiyong Park  * the target power level (end_pwrlvl).
69*54fd6939SJiyong Park  *
70*54fd6939SJiyong Park  * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
71*54fd6939SJiyong Park  * updates the `last_cpu_in_non_cpu_pd[]` with last power down cpu id.
72*54fd6939SJiyong Park  *
73*54fd6939SJiyong Park  * This function will only be invoked with data cache enabled and while
74*54fd6939SJiyong Park  * powering down a core.
75*54fd6939SJiyong Park  ******************************************************************************/
psci_stats_update_pwr_down(unsigned int end_pwrlvl,const psci_power_state_t * state_info)76*54fd6939SJiyong Park void psci_stats_update_pwr_down(unsigned int end_pwrlvl,
77*54fd6939SJiyong Park 			const psci_power_state_t *state_info)
78*54fd6939SJiyong Park {
79*54fd6939SJiyong Park 	unsigned int lvl, parent_idx;
80*54fd6939SJiyong Park 	unsigned int cpu_idx = plat_my_core_pos();
81*54fd6939SJiyong Park 
82*54fd6939SJiyong Park 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
83*54fd6939SJiyong Park 	assert(state_info != NULL);
84*54fd6939SJiyong Park 
85*54fd6939SJiyong Park 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
86*54fd6939SJiyong Park 
87*54fd6939SJiyong Park 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
88*54fd6939SJiyong Park 
89*54fd6939SJiyong Park 		/* Break early if the target power state is RUN */
90*54fd6939SJiyong Park 		if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0)
91*54fd6939SJiyong Park 			break;
92*54fd6939SJiyong Park 
93*54fd6939SJiyong Park 		/*
94*54fd6939SJiyong Park 		 * The power domain is entering a low power state, so this is
95*54fd6939SJiyong Park 		 * the last CPU for this power domain
96*54fd6939SJiyong Park 		 */
97*54fd6939SJiyong Park 		last_cpu_in_non_cpu_pd[parent_idx] = (int)cpu_idx;
98*54fd6939SJiyong Park 
99*54fd6939SJiyong Park 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
100*54fd6939SJiyong Park 	}
101*54fd6939SJiyong Park 
102*54fd6939SJiyong Park }
103*54fd6939SJiyong Park 
104*54fd6939SJiyong Park /*******************************************************************************
105*54fd6939SJiyong Park  * This function updates the PSCI STATS(residency time and count) for CPU
106*54fd6939SJiyong Park  * and NON-CPU power domains.
107*54fd6939SJiyong Park  * It is called with caches enabled and locks acquired(for NON-CPU domain)
108*54fd6939SJiyong Park  ******************************************************************************/
psci_stats_update_pwr_up(unsigned int end_pwrlvl,const psci_power_state_t * state_info)109*54fd6939SJiyong Park void psci_stats_update_pwr_up(unsigned int end_pwrlvl,
110*54fd6939SJiyong Park 			const psci_power_state_t *state_info)
111*54fd6939SJiyong Park {
112*54fd6939SJiyong Park 	unsigned int lvl, parent_idx;
113*54fd6939SJiyong Park 	unsigned int cpu_idx = plat_my_core_pos();
114*54fd6939SJiyong Park 	int stat_idx;
115*54fd6939SJiyong Park 	plat_local_state_t local_state;
116*54fd6939SJiyong Park 	u_register_t residency;
117*54fd6939SJiyong Park 
118*54fd6939SJiyong Park 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
119*54fd6939SJiyong Park 	assert(state_info != NULL);
120*54fd6939SJiyong Park 
121*54fd6939SJiyong Park 	/* Get the index into the stats array */
122*54fd6939SJiyong Park 	local_state = state_info->pwr_domain_state[PSCI_CPU_PWR_LVL];
123*54fd6939SJiyong Park 	stat_idx = get_stat_idx(local_state, PSCI_CPU_PWR_LVL);
124*54fd6939SJiyong Park 
125*54fd6939SJiyong Park 	/* Call into platform interface to calculate residency. */
126*54fd6939SJiyong Park 	residency = plat_psci_stat_get_residency(PSCI_CPU_PWR_LVL,
127*54fd6939SJiyong Park 	    state_info, cpu_idx);
128*54fd6939SJiyong Park 
129*54fd6939SJiyong Park 	/* Update CPU stats. */
130*54fd6939SJiyong Park 	psci_cpu_stat[cpu_idx][stat_idx].residency += residency;
131*54fd6939SJiyong Park 	psci_cpu_stat[cpu_idx][stat_idx].count++;
132*54fd6939SJiyong Park 
133*54fd6939SJiyong Park 	/*
134*54fd6939SJiyong Park 	 * Check what power domains above CPU were off
135*54fd6939SJiyong Park 	 * prior to this CPU powering on.
136*54fd6939SJiyong Park 	 */
137*54fd6939SJiyong Park 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
138*54fd6939SJiyong Park 	/* Return early if this is the first power up. */
139*54fd6939SJiyong Park 	if (last_cpu_in_non_cpu_pd[parent_idx] == -1)
140*54fd6939SJiyong Park 		return;
141*54fd6939SJiyong Park 
142*54fd6939SJiyong Park 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
143*54fd6939SJiyong Park 		local_state = state_info->pwr_domain_state[lvl];
144*54fd6939SJiyong Park 		if (is_local_state_run(local_state) != 0) {
145*54fd6939SJiyong Park 			/* Break early */
146*54fd6939SJiyong Park 			break;
147*54fd6939SJiyong Park 		}
148*54fd6939SJiyong Park 
149*54fd6939SJiyong Park 		assert(last_cpu_in_non_cpu_pd[parent_idx] != -1);
150*54fd6939SJiyong Park 
151*54fd6939SJiyong Park 		/* Call into platform interface to calculate residency. */
152*54fd6939SJiyong Park 		residency = plat_psci_stat_get_residency(lvl, state_info,
153*54fd6939SJiyong Park 			(unsigned int)last_cpu_in_non_cpu_pd[parent_idx]);
154*54fd6939SJiyong Park 
155*54fd6939SJiyong Park 		/* Initialize back to reset value */
156*54fd6939SJiyong Park 		last_cpu_in_non_cpu_pd[parent_idx] = -1;
157*54fd6939SJiyong Park 
158*54fd6939SJiyong Park 		/* Get the index into the stats array */
159*54fd6939SJiyong Park 		stat_idx = get_stat_idx(local_state, lvl);
160*54fd6939SJiyong Park 
161*54fd6939SJiyong Park 		/* Update non cpu stats */
162*54fd6939SJiyong Park 		psci_non_cpu_stat[parent_idx][stat_idx].residency += residency;
163*54fd6939SJiyong Park 		psci_non_cpu_stat[parent_idx][stat_idx].count++;
164*54fd6939SJiyong Park 
165*54fd6939SJiyong Park 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
166*54fd6939SJiyong Park 	}
167*54fd6939SJiyong Park 
168*54fd6939SJiyong Park }
169*54fd6939SJiyong Park 
170*54fd6939SJiyong Park /*******************************************************************************
171*54fd6939SJiyong Park  * This function returns the appropriate count and residency time of the
172*54fd6939SJiyong Park  * local state for the highest power level expressed in the `power_state`
173*54fd6939SJiyong Park  * for the node represented by `target_cpu`.
174*54fd6939SJiyong Park  ******************************************************************************/
psci_get_stat(u_register_t target_cpu,unsigned int power_state,psci_stat_t * psci_stat)175*54fd6939SJiyong Park static int psci_get_stat(u_register_t target_cpu, unsigned int power_state,
176*54fd6939SJiyong Park 			 psci_stat_t *psci_stat)
177*54fd6939SJiyong Park {
178*54fd6939SJiyong Park 	int rc;
179*54fd6939SJiyong Park 	unsigned int pwrlvl, lvl, parent_idx, target_idx;
180*54fd6939SJiyong Park 	int stat_idx;
181*54fd6939SJiyong Park 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
182*54fd6939SJiyong Park 	plat_local_state_t local_state;
183*54fd6939SJiyong Park 
184*54fd6939SJiyong Park 	/* Validate the target_cpu parameter and determine the cpu index */
185*54fd6939SJiyong Park 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_cpu);
186*54fd6939SJiyong Park 	if (target_idx == (unsigned int) -1)
187*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
188*54fd6939SJiyong Park 
189*54fd6939SJiyong Park 	/* Validate the power_state parameter */
190*54fd6939SJiyong Park 	if (psci_plat_pm_ops->translate_power_state_by_mpidr == NULL)
191*54fd6939SJiyong Park 		rc = psci_validate_power_state(power_state, &state_info);
192*54fd6939SJiyong Park 	else
193*54fd6939SJiyong Park 		rc = psci_plat_pm_ops->translate_power_state_by_mpidr(
194*54fd6939SJiyong Park 				target_cpu, power_state, &state_info);
195*54fd6939SJiyong Park 
196*54fd6939SJiyong Park 	if (rc != PSCI_E_SUCCESS)
197*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
198*54fd6939SJiyong Park 
199*54fd6939SJiyong Park 	/* Find the highest power level */
200*54fd6939SJiyong Park 	pwrlvl = psci_find_target_suspend_lvl(&state_info);
201*54fd6939SJiyong Park 	if (pwrlvl == PSCI_INVALID_PWR_LVL) {
202*54fd6939SJiyong Park 		ERROR("Invalid target power level for PSCI statistics operation\n");
203*54fd6939SJiyong Park 		panic();
204*54fd6939SJiyong Park 	}
205*54fd6939SJiyong Park 
206*54fd6939SJiyong Park 	/* Get the index into the stats array */
207*54fd6939SJiyong Park 	local_state = state_info.pwr_domain_state[pwrlvl];
208*54fd6939SJiyong Park 	stat_idx = get_stat_idx(local_state, pwrlvl);
209*54fd6939SJiyong Park 
210*54fd6939SJiyong Park 	if (pwrlvl > PSCI_CPU_PWR_LVL) {
211*54fd6939SJiyong Park 		/* Get the power domain index */
212*54fd6939SJiyong Park 		parent_idx = SPECULATION_SAFE_VALUE(psci_cpu_pd_nodes[target_idx].parent_node);
213*54fd6939SJiyong Park 		for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl < pwrlvl; lvl++)
214*54fd6939SJiyong Park 			parent_idx = SPECULATION_SAFE_VALUE(psci_non_cpu_pd_nodes[parent_idx].parent_node);
215*54fd6939SJiyong Park 
216*54fd6939SJiyong Park 		/* Get the non cpu power domain stats */
217*54fd6939SJiyong Park 		*psci_stat = psci_non_cpu_stat[parent_idx][stat_idx];
218*54fd6939SJiyong Park 	} else {
219*54fd6939SJiyong Park 		/* Get the cpu power domain stats */
220*54fd6939SJiyong Park 		*psci_stat = psci_cpu_stat[target_idx][stat_idx];
221*54fd6939SJiyong Park 	}
222*54fd6939SJiyong Park 
223*54fd6939SJiyong Park 	return PSCI_E_SUCCESS;
224*54fd6939SJiyong Park }
225*54fd6939SJiyong Park 
226*54fd6939SJiyong Park /* This is the top level function for PSCI_STAT_RESIDENCY SMC. */
psci_stat_residency(u_register_t target_cpu,unsigned int power_state)227*54fd6939SJiyong Park u_register_t psci_stat_residency(u_register_t target_cpu,
228*54fd6939SJiyong Park 		unsigned int power_state)
229*54fd6939SJiyong Park {
230*54fd6939SJiyong Park 	psci_stat_t psci_stat;
231*54fd6939SJiyong Park 	int rc = psci_get_stat(target_cpu, power_state, &psci_stat);
232*54fd6939SJiyong Park 
233*54fd6939SJiyong Park 	if (rc == PSCI_E_SUCCESS)
234*54fd6939SJiyong Park 		return psci_stat.residency;
235*54fd6939SJiyong Park 	else
236*54fd6939SJiyong Park 		return 0;
237*54fd6939SJiyong Park }
238*54fd6939SJiyong Park 
239*54fd6939SJiyong Park /* This is the top level function for PSCI_STAT_COUNT SMC. */
psci_stat_count(u_register_t target_cpu,unsigned int power_state)240*54fd6939SJiyong Park u_register_t psci_stat_count(u_register_t target_cpu,
241*54fd6939SJiyong Park 	unsigned int power_state)
242*54fd6939SJiyong Park {
243*54fd6939SJiyong Park 	psci_stat_t psci_stat;
244*54fd6939SJiyong Park 	int rc = psci_get_stat(target_cpu, power_state, &psci_stat);
245*54fd6939SJiyong Park 
246*54fd6939SJiyong Park 	if (rc == PSCI_E_SUCCESS)
247*54fd6939SJiyong Park 		return psci_stat.count;
248*54fd6939SJiyong Park 	else
249*54fd6939SJiyong Park 		return 0;
250*54fd6939SJiyong Park }
251