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