xref: /rk3399_ARM-atf/lib/psci/psci_stat.c (revision e655b00de645946f9cd494664703a871f36d7a28)
1532ed618SSoby Mathew /*
2*9f407e44SRohit Mathew  * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7532ed618SSoby Mathew #include <assert.h>
809d40e0eSAntonio Nino Diaz 
9532ed618SSoby Mathew #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <common/debug.h>
1209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1309d40e0eSAntonio Nino Diaz 
14532ed618SSoby Mathew #include "psci_private.h"
15532ed618SSoby Mathew 
16532ed618SSoby Mathew #ifndef PLAT_MAX_PWR_LVL_STATES
17abce1dceSAntonio Nino Diaz #define PLAT_MAX_PWR_LVL_STATES		2U
18532ed618SSoby Mathew #endif
19532ed618SSoby Mathew 
20532ed618SSoby Mathew /* Following structure is used for PSCI STAT */
21532ed618SSoby Mathew typedef struct psci_stat {
22532ed618SSoby Mathew 	u_register_t residency;
23532ed618SSoby Mathew 	u_register_t count;
24532ed618SSoby Mathew } psci_stat_t;
25532ed618SSoby Mathew 
26532ed618SSoby Mathew /*
27532ed618SSoby Mathew  * Following is used to keep track of the last cpu
28532ed618SSoby Mathew  * that goes to power down in non cpu power domains.
29532ed618SSoby Mathew  */
302271cb05SJonathan Wright static int last_cpu_in_non_cpu_pd[PSCI_NUM_NON_CPU_PWR_DOMAINS] = {
315b33ad17SDeepika Bhavnani 		[0 ... PSCI_NUM_NON_CPU_PWR_DOMAINS - 1U] = -1};
32532ed618SSoby Mathew 
33532ed618SSoby Mathew /*
34532ed618SSoby Mathew  * Following are used to store PSCI STAT values for
35532ed618SSoby Mathew  * CPU and non CPU power domains.
36532ed618SSoby Mathew  */
37532ed618SSoby Mathew static psci_stat_t psci_cpu_stat[PLATFORM_CORE_COUNT]
38532ed618SSoby Mathew 				[PLAT_MAX_PWR_LVL_STATES];
39532ed618SSoby Mathew static psci_stat_t psci_non_cpu_stat[PSCI_NUM_NON_CPU_PWR_DOMAINS]
40532ed618SSoby Mathew 				[PLAT_MAX_PWR_LVL_STATES];
41532ed618SSoby Mathew 
42532ed618SSoby Mathew /*
43532ed618SSoby Mathew  * This functions returns the index into the `psci_stat_t` array given the
44532ed618SSoby Mathew  * local power state and power domain level. If the platform implements the
45532ed618SSoby Mathew  * `get_pwr_lvl_state_idx` pm hook, then that will be used to return the index.
46532ed618SSoby Mathew  */
get_stat_idx(plat_local_state_t local_state,unsigned int pwr_lvl)47654ab9e0SGovindraj Raja static unsigned int get_stat_idx(plat_local_state_t local_state, unsigned int pwr_lvl)
48532ed618SSoby Mathew {
49654ab9e0SGovindraj Raja 	unsigned int idx;
50532ed618SSoby Mathew 
51532ed618SSoby Mathew 	if (psci_plat_pm_ops->get_pwr_lvl_state_idx == NULL) {
52abce1dceSAntonio Nino Diaz 		assert(PLAT_MAX_PWR_LVL_STATES == 2U);
53abce1dceSAntonio Nino Diaz 		if (is_local_state_retn(local_state) != 0)
54532ed618SSoby Mathew 			return 0;
55532ed618SSoby Mathew 
56abce1dceSAntonio Nino Diaz 		assert(is_local_state_off(local_state) != 0);
57532ed618SSoby Mathew 		return 1;
58532ed618SSoby Mathew 	}
59532ed618SSoby Mathew 
60532ed618SSoby Mathew 	idx = psci_plat_pm_ops->get_pwr_lvl_state_idx(local_state, pwr_lvl);
61654ab9e0SGovindraj Raja 	assert(idx < PLAT_MAX_PWR_LVL_STATES);
62532ed618SSoby Mathew 	return idx;
63532ed618SSoby Mathew }
64532ed618SSoby Mathew 
65532ed618SSoby Mathew /*******************************************************************************
66532ed618SSoby Mathew  * This function is passed the target local power states for each power
67532ed618SSoby Mathew  * domain (state_info) between the current CPU domain and its ancestors until
68532ed618SSoby Mathew  * the target power level (end_pwrlvl).
69532ed618SSoby Mathew  *
70532ed618SSoby Mathew  * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
71532ed618SSoby Mathew  * updates the `last_cpu_in_non_cpu_pd[]` with last power down cpu id.
72532ed618SSoby Mathew  *
73532ed618SSoby Mathew  * This function will only be invoked with data cache enabled and while
74532ed618SSoby Mathew  * powering down a core.
75532ed618SSoby Mathew  ******************************************************************************/
psci_stats_update_pwr_down(unsigned int cpu_idx,unsigned int end_pwrlvl,const psci_power_state_t * state_info)763b802105SBoyan Karatotev void psci_stats_update_pwr_down(unsigned int cpu_idx, unsigned int end_pwrlvl,
77532ed618SSoby Mathew 			const psci_power_state_t *state_info)
78532ed618SSoby Mathew {
79abce1dceSAntonio Nino Diaz 	unsigned int lvl, parent_idx;
80532ed618SSoby Mathew 
81532ed618SSoby Mathew 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
82abce1dceSAntonio Nino Diaz 	assert(state_info != NULL);
83532ed618SSoby Mathew 
84*9f407e44SRohit Mathew 	parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
85532ed618SSoby Mathew 
86abce1dceSAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
87532ed618SSoby Mathew 
88532ed618SSoby Mathew 		/* Break early if the target power state is RUN */
89abce1dceSAntonio Nino Diaz 		if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0)
90532ed618SSoby Mathew 			break;
91532ed618SSoby Mathew 
92532ed618SSoby Mathew 		/*
93532ed618SSoby Mathew 		 * The power domain is entering a low power state, so this is
94532ed618SSoby Mathew 		 * the last CPU for this power domain
95532ed618SSoby Mathew 		 */
965b33ad17SDeepika Bhavnani 		last_cpu_in_non_cpu_pd[parent_idx] = (int)cpu_idx;
97532ed618SSoby Mathew 
98532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
99532ed618SSoby Mathew 	}
100532ed618SSoby Mathew 
101532ed618SSoby Mathew }
102532ed618SSoby Mathew 
103532ed618SSoby Mathew /*******************************************************************************
104532ed618SSoby Mathew  * This function updates the PSCI STATS(residency time and count) for CPU
105532ed618SSoby Mathew  * and NON-CPU power domains.
106532ed618SSoby Mathew  * It is called with caches enabled and locks acquired(for NON-CPU domain)
107532ed618SSoby Mathew  ******************************************************************************/
psci_stats_update_pwr_up(unsigned int cpu_idx,unsigned int end_pwrlvl,const psci_power_state_t * state_info)1083b802105SBoyan Karatotev void psci_stats_update_pwr_up(unsigned int cpu_idx, unsigned int end_pwrlvl,
10904c1db1eSdp-arm 			const psci_power_state_t *state_info)
110532ed618SSoby Mathew {
111abce1dceSAntonio Nino Diaz 	unsigned int lvl, parent_idx;
112654ab9e0SGovindraj Raja 	unsigned int stat_idx;
113532ed618SSoby Mathew 	plat_local_state_t local_state;
114532ed618SSoby Mathew 	u_register_t residency;
115532ed618SSoby Mathew 
116532ed618SSoby Mathew 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
117abce1dceSAntonio Nino Diaz 	assert(state_info != NULL);
118532ed618SSoby Mathew 
119532ed618SSoby Mathew 	/* Get the index into the stats array */
120532ed618SSoby Mathew 	local_state = state_info->pwr_domain_state[PSCI_CPU_PWR_LVL];
121532ed618SSoby Mathew 	stat_idx = get_stat_idx(local_state, PSCI_CPU_PWR_LVL);
122532ed618SSoby Mathew 
12304c1db1eSdp-arm 	/* Call into platform interface to calculate residency. */
12404c1db1eSdp-arm 	residency = plat_psci_stat_get_residency(PSCI_CPU_PWR_LVL,
12504c1db1eSdp-arm 	    state_info, cpu_idx);
126532ed618SSoby Mathew 
127532ed618SSoby Mathew 	/* Update CPU stats. */
128532ed618SSoby Mathew 	psci_cpu_stat[cpu_idx][stat_idx].residency += residency;
129532ed618SSoby Mathew 	psci_cpu_stat[cpu_idx][stat_idx].count++;
130532ed618SSoby Mathew 
131532ed618SSoby Mathew 	/*
132532ed618SSoby Mathew 	 * Check what power domains above CPU were off
133532ed618SSoby Mathew 	 * prior to this CPU powering on.
134532ed618SSoby Mathew 	 */
135*9f407e44SRohit Mathew 	parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
1362271cb05SJonathan Wright 	/* Return early if this is the first power up. */
1372271cb05SJonathan Wright 	if (last_cpu_in_non_cpu_pd[parent_idx] == -1)
1382271cb05SJonathan Wright 		return;
1392271cb05SJonathan Wright 
140abce1dceSAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
141532ed618SSoby Mathew 		local_state = state_info->pwr_domain_state[lvl];
142abce1dceSAntonio Nino Diaz 		if (is_local_state_run(local_state) != 0) {
143532ed618SSoby Mathew 			/* Break early */
144532ed618SSoby Mathew 			break;
145532ed618SSoby Mathew 		}
146532ed618SSoby Mathew 
147532ed618SSoby Mathew 		assert(last_cpu_in_non_cpu_pd[parent_idx] != -1);
148532ed618SSoby Mathew 
14904c1db1eSdp-arm 		/* Call into platform interface to calculate residency. */
15004c1db1eSdp-arm 		residency = plat_psci_stat_get_residency(lvl, state_info,
1515b33ad17SDeepika Bhavnani 			(unsigned int)last_cpu_in_non_cpu_pd[parent_idx]);
152532ed618SSoby Mathew 
153532ed618SSoby Mathew 		/* Initialize back to reset value */
154532ed618SSoby Mathew 		last_cpu_in_non_cpu_pd[parent_idx] = -1;
155532ed618SSoby Mathew 
156532ed618SSoby Mathew 		/* Get the index into the stats array */
157532ed618SSoby Mathew 		stat_idx = get_stat_idx(local_state, lvl);
158532ed618SSoby Mathew 
159532ed618SSoby Mathew 		/* Update non cpu stats */
160532ed618SSoby Mathew 		psci_non_cpu_stat[parent_idx][stat_idx].residency += residency;
161532ed618SSoby Mathew 		psci_non_cpu_stat[parent_idx][stat_idx].count++;
162532ed618SSoby Mathew 
163532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
164532ed618SSoby Mathew 	}
165532ed618SSoby Mathew 
166532ed618SSoby Mathew }
167532ed618SSoby Mathew 
168532ed618SSoby Mathew /*******************************************************************************
169532ed618SSoby Mathew  * This function returns the appropriate count and residency time of the
170532ed618SSoby Mathew  * local state for the highest power level expressed in the `power_state`
171532ed618SSoby Mathew  * for the node represented by `target_cpu`.
172532ed618SSoby Mathew  ******************************************************************************/
psci_get_stat(u_register_t target_cpu,unsigned int power_state,psci_stat_t * psci_stat)173c283e05aSEtienne Carriere static int psci_get_stat(u_register_t target_cpu, unsigned int power_state,
174532ed618SSoby Mathew 			 psci_stat_t *psci_stat)
175532ed618SSoby Mathew {
176c283e05aSEtienne Carriere 	int rc;
177654ab9e0SGovindraj Raja 	unsigned int pwrlvl, lvl, parent_idx, target_idx, stat_idx;
178532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
179532ed618SSoby Mathew 	plat_local_state_t local_state;
180532ed618SSoby Mathew 
181e60c1847SManish Pandey 	/* Determine the cpu index */
182abce1dceSAntonio Nino Diaz 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_cpu);
183532ed618SSoby Mathew 
184532ed618SSoby Mathew 	/* Validate the power_state parameter */
185abce1dceSAntonio Nino Diaz 	if (psci_plat_pm_ops->translate_power_state_by_mpidr == NULL)
186532ed618SSoby Mathew 		rc = psci_validate_power_state(power_state, &state_info);
187532ed618SSoby Mathew 	else
188532ed618SSoby Mathew 		rc = psci_plat_pm_ops->translate_power_state_by_mpidr(
189532ed618SSoby Mathew 				target_cpu, power_state, &state_info);
190532ed618SSoby Mathew 
191532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
192532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
193532ed618SSoby Mathew 
194532ed618SSoby Mathew 	/* Find the highest power level */
195532ed618SSoby Mathew 	pwrlvl = psci_find_target_suspend_lvl(&state_info);
196a1c3faa6SSandrine Bailleux 	if (pwrlvl == PSCI_INVALID_PWR_LVL) {
197a1c3faa6SSandrine Bailleux 		ERROR("Invalid target power level for PSCI statistics operation\n");
198a1c3faa6SSandrine Bailleux 		panic();
199a1c3faa6SSandrine Bailleux 	}
200532ed618SSoby Mathew 
201532ed618SSoby Mathew 	/* Get the index into the stats array */
202532ed618SSoby Mathew 	local_state = state_info.pwr_domain_state[pwrlvl];
203532ed618SSoby Mathew 	stat_idx = get_stat_idx(local_state, pwrlvl);
204532ed618SSoby Mathew 
205532ed618SSoby Mathew 	if (pwrlvl > PSCI_CPU_PWR_LVL) {
206532ed618SSoby Mathew 		/* Get the power domain index */
207*9f407e44SRohit Mathew 		parent_idx = SPECULATION_SAFE_VALUE(PER_CPU_BY_INDEX(psci_cpu_pd_nodes,
208*9f407e44SRohit Mathew 				target_idx)->parent_node);
209abce1dceSAntonio Nino Diaz 		for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl < pwrlvl; lvl++)
2109edd8912SJoel Hutton 			parent_idx = SPECULATION_SAFE_VALUE(psci_non_cpu_pd_nodes[parent_idx].parent_node);
211532ed618SSoby Mathew 
212532ed618SSoby Mathew 		/* Get the non cpu power domain stats */
213532ed618SSoby Mathew 		*psci_stat = psci_non_cpu_stat[parent_idx][stat_idx];
214532ed618SSoby Mathew 	} else {
215532ed618SSoby Mathew 		/* Get the cpu power domain stats */
216532ed618SSoby Mathew 		*psci_stat = psci_cpu_stat[target_idx][stat_idx];
217532ed618SSoby Mathew 	}
218532ed618SSoby Mathew 
219532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
220532ed618SSoby Mathew }
221532ed618SSoby Mathew 
222532ed618SSoby Mathew /* This is the top level function for PSCI_STAT_RESIDENCY SMC. */
psci_stat_residency(u_register_t target_cpu,unsigned int power_state)223532ed618SSoby Mathew u_register_t psci_stat_residency(u_register_t target_cpu,
224532ed618SSoby Mathew 		unsigned int power_state)
225532ed618SSoby Mathew {
226532ed618SSoby Mathew 	psci_stat_t psci_stat;
227e60c1847SManish Pandey 
228e60c1847SManish Pandey 	/* Validate the target cpu */
229e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
230e60c1847SManish Pandey 		return 0;
231e60c1847SManish Pandey 
232532ed618SSoby Mathew 	int rc = psci_get_stat(target_cpu, power_state, &psci_stat);
233c283e05aSEtienne Carriere 
234532ed618SSoby Mathew 	if (rc == PSCI_E_SUCCESS)
235532ed618SSoby Mathew 		return psci_stat.residency;
236532ed618SSoby Mathew 	else
237532ed618SSoby Mathew 		return 0;
238532ed618SSoby Mathew }
239532ed618SSoby Mathew 
240532ed618SSoby Mathew /* This is the top level function for PSCI_STAT_COUNT SMC. */
psci_stat_count(u_register_t target_cpu,unsigned int power_state)241532ed618SSoby Mathew u_register_t psci_stat_count(u_register_t target_cpu,
242532ed618SSoby Mathew 	unsigned int power_state)
243532ed618SSoby Mathew {
244532ed618SSoby Mathew 	psci_stat_t psci_stat;
245e60c1847SManish Pandey 
246e60c1847SManish Pandey 	/* Validate the target cpu */
247e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
248e60c1847SManish Pandey 		return 0;
249e60c1847SManish Pandey 
250532ed618SSoby Mathew 	int rc = psci_get_stat(target_cpu, power_state, &psci_stat);
251c283e05aSEtienne Carriere 
252532ed618SSoby Mathew 	if (rc == PSCI_E_SUCCESS)
253532ed618SSoby Mathew 		return psci_stat.count;
254532ed618SSoby Mathew 	else
255532ed618SSoby Mathew 		return 0;
256532ed618SSoby Mathew }
257