1532ed618SSoby Mathew /* 2*04c1db1eSdp-arm * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 4532ed618SSoby Mathew * Redistribution and use in source and binary forms, with or without 5532ed618SSoby Mathew * modification, are permitted provided that the following conditions are met: 6532ed618SSoby Mathew * 7532ed618SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8532ed618SSoby Mathew * list of conditions and the following disclaimer. 9532ed618SSoby Mathew * 10532ed618SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11532ed618SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12532ed618SSoby Mathew * and/or other materials provided with the distribution. 13532ed618SSoby Mathew * 14532ed618SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15532ed618SSoby Mathew * to endorse or promote products derived from this software without specific 16532ed618SSoby Mathew * prior written permission. 17532ed618SSoby Mathew * 18532ed618SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19532ed618SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20532ed618SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21532ed618SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22532ed618SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23532ed618SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24532ed618SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25532ed618SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26532ed618SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27532ed618SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28532ed618SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29532ed618SSoby Mathew */ 30532ed618SSoby Mathew 31532ed618SSoby Mathew #include <assert.h> 32532ed618SSoby Mathew #include <debug.h> 33532ed618SSoby Mathew #include <platform.h> 34532ed618SSoby Mathew #include <platform_def.h> 35532ed618SSoby Mathew #include "psci_private.h" 36532ed618SSoby Mathew 37532ed618SSoby Mathew #ifndef PLAT_MAX_PWR_LVL_STATES 38532ed618SSoby Mathew #define PLAT_MAX_PWR_LVL_STATES 2 39532ed618SSoby Mathew #endif 40532ed618SSoby Mathew 41532ed618SSoby Mathew /* Following structure is used for PSCI STAT */ 42532ed618SSoby Mathew typedef struct psci_stat { 43532ed618SSoby Mathew u_register_t residency; 44532ed618SSoby Mathew u_register_t count; 45532ed618SSoby Mathew } psci_stat_t; 46532ed618SSoby Mathew 47532ed618SSoby Mathew /* 48532ed618SSoby Mathew * Following is used to keep track of the last cpu 49532ed618SSoby Mathew * that goes to power down in non cpu power domains. 50532ed618SSoby Mathew */ 51532ed618SSoby Mathew static int last_cpu_in_non_cpu_pd[PSCI_NUM_NON_CPU_PWR_DOMAINS] = {-1}; 52532ed618SSoby Mathew 53532ed618SSoby Mathew /* 54532ed618SSoby Mathew * Following are used to store PSCI STAT values for 55532ed618SSoby Mathew * CPU and non CPU power domains. 56532ed618SSoby Mathew */ 57532ed618SSoby Mathew static psci_stat_t psci_cpu_stat[PLATFORM_CORE_COUNT] 58532ed618SSoby Mathew [PLAT_MAX_PWR_LVL_STATES]; 59532ed618SSoby Mathew static psci_stat_t psci_non_cpu_stat[PSCI_NUM_NON_CPU_PWR_DOMAINS] 60532ed618SSoby Mathew [PLAT_MAX_PWR_LVL_STATES]; 61532ed618SSoby Mathew 62532ed618SSoby Mathew /* 63532ed618SSoby Mathew * This functions returns the index into the `psci_stat_t` array given the 64532ed618SSoby Mathew * local power state and power domain level. If the platform implements the 65532ed618SSoby Mathew * `get_pwr_lvl_state_idx` pm hook, then that will be used to return the index. 66532ed618SSoby Mathew */ 67532ed618SSoby Mathew static int get_stat_idx(plat_local_state_t local_state, int pwr_lvl) 68532ed618SSoby Mathew { 69532ed618SSoby Mathew int idx; 70532ed618SSoby Mathew 71532ed618SSoby Mathew if (psci_plat_pm_ops->get_pwr_lvl_state_idx == NULL) { 72532ed618SSoby Mathew assert(PLAT_MAX_PWR_LVL_STATES == 2); 73532ed618SSoby Mathew if (is_local_state_retn(local_state)) 74532ed618SSoby Mathew return 0; 75532ed618SSoby Mathew 76532ed618SSoby Mathew assert(is_local_state_off(local_state)); 77532ed618SSoby Mathew return 1; 78532ed618SSoby Mathew } 79532ed618SSoby Mathew 80532ed618SSoby Mathew idx = psci_plat_pm_ops->get_pwr_lvl_state_idx(local_state, pwr_lvl); 81532ed618SSoby Mathew assert((idx >= 0) && (idx < PLAT_MAX_PWR_LVL_STATES)); 82532ed618SSoby Mathew return idx; 83532ed618SSoby Mathew } 84532ed618SSoby Mathew 85532ed618SSoby Mathew /******************************************************************************* 86532ed618SSoby Mathew * This function is passed the target local power states for each power 87532ed618SSoby Mathew * domain (state_info) between the current CPU domain and its ancestors until 88532ed618SSoby Mathew * the target power level (end_pwrlvl). 89532ed618SSoby Mathew * 90532ed618SSoby Mathew * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it 91532ed618SSoby Mathew * updates the `last_cpu_in_non_cpu_pd[]` with last power down cpu id. 92532ed618SSoby Mathew * 93532ed618SSoby Mathew * This function will only be invoked with data cache enabled and while 94532ed618SSoby Mathew * powering down a core. 95532ed618SSoby Mathew ******************************************************************************/ 96532ed618SSoby Mathew void psci_stats_update_pwr_down(unsigned int end_pwrlvl, 97532ed618SSoby Mathew const psci_power_state_t *state_info) 98532ed618SSoby Mathew { 99532ed618SSoby Mathew int lvl, parent_idx, cpu_idx = plat_my_core_pos(); 100532ed618SSoby Mathew 101532ed618SSoby Mathew assert(end_pwrlvl <= PLAT_MAX_PWR_LVL); 102532ed618SSoby Mathew assert(state_info); 103532ed618SSoby Mathew 104532ed618SSoby Mathew parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 105532ed618SSoby Mathew 106532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) { 107532ed618SSoby Mathew 108532ed618SSoby Mathew /* Break early if the target power state is RUN */ 109532ed618SSoby Mathew if (is_local_state_run(state_info->pwr_domain_state[lvl])) 110532ed618SSoby Mathew break; 111532ed618SSoby Mathew 112532ed618SSoby Mathew /* 113532ed618SSoby Mathew * The power domain is entering a low power state, so this is 114532ed618SSoby Mathew * the last CPU for this power domain 115532ed618SSoby Mathew */ 116532ed618SSoby Mathew last_cpu_in_non_cpu_pd[parent_idx] = cpu_idx; 117532ed618SSoby Mathew 118532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 119532ed618SSoby Mathew } 120532ed618SSoby Mathew 121532ed618SSoby Mathew } 122532ed618SSoby Mathew 123532ed618SSoby Mathew /******************************************************************************* 124532ed618SSoby Mathew * This function updates the PSCI STATS(residency time and count) for CPU 125532ed618SSoby Mathew * and NON-CPU power domains. 126532ed618SSoby Mathew * It is called with caches enabled and locks acquired(for NON-CPU domain) 127532ed618SSoby Mathew ******************************************************************************/ 128532ed618SSoby Mathew void psci_stats_update_pwr_up(unsigned int end_pwrlvl, 129*04c1db1eSdp-arm const psci_power_state_t *state_info) 130532ed618SSoby Mathew { 131532ed618SSoby Mathew int parent_idx, cpu_idx = plat_my_core_pos(); 132532ed618SSoby Mathew int lvl, stat_idx; 133532ed618SSoby Mathew plat_local_state_t local_state; 134532ed618SSoby Mathew u_register_t residency; 135532ed618SSoby Mathew 136532ed618SSoby Mathew assert(end_pwrlvl <= PLAT_MAX_PWR_LVL); 137532ed618SSoby Mathew assert(state_info); 138532ed618SSoby Mathew 139532ed618SSoby Mathew /* Get the index into the stats array */ 140532ed618SSoby Mathew local_state = state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]; 141532ed618SSoby Mathew stat_idx = get_stat_idx(local_state, PSCI_CPU_PWR_LVL); 142532ed618SSoby Mathew 143*04c1db1eSdp-arm /* Call into platform interface to calculate residency. */ 144*04c1db1eSdp-arm residency = plat_psci_stat_get_residency(PSCI_CPU_PWR_LVL, 145*04c1db1eSdp-arm state_info, cpu_idx); 146532ed618SSoby Mathew 147532ed618SSoby Mathew /* Update CPU stats. */ 148532ed618SSoby Mathew psci_cpu_stat[cpu_idx][stat_idx].residency += residency; 149532ed618SSoby Mathew psci_cpu_stat[cpu_idx][stat_idx].count++; 150532ed618SSoby Mathew 151532ed618SSoby Mathew /* 152532ed618SSoby Mathew * Check what power domains above CPU were off 153532ed618SSoby Mathew * prior to this CPU powering on. 154532ed618SSoby Mathew */ 155532ed618SSoby Mathew parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 156532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) { 157532ed618SSoby Mathew local_state = state_info->pwr_domain_state[lvl]; 158532ed618SSoby Mathew if (is_local_state_run(local_state)) { 159532ed618SSoby Mathew /* Break early */ 160532ed618SSoby Mathew break; 161532ed618SSoby Mathew } 162532ed618SSoby Mathew 163532ed618SSoby Mathew assert(last_cpu_in_non_cpu_pd[parent_idx] != -1); 164532ed618SSoby Mathew 165*04c1db1eSdp-arm /* Call into platform interface to calculate residency. */ 166*04c1db1eSdp-arm residency = plat_psci_stat_get_residency(lvl, state_info, 167*04c1db1eSdp-arm last_cpu_in_non_cpu_pd[parent_idx]); 168532ed618SSoby Mathew 169532ed618SSoby Mathew /* Initialize back to reset value */ 170532ed618SSoby Mathew last_cpu_in_non_cpu_pd[parent_idx] = -1; 171532ed618SSoby Mathew 172532ed618SSoby Mathew /* Get the index into the stats array */ 173532ed618SSoby Mathew stat_idx = get_stat_idx(local_state, lvl); 174532ed618SSoby Mathew 175532ed618SSoby Mathew /* Update non cpu stats */ 176532ed618SSoby Mathew psci_non_cpu_stat[parent_idx][stat_idx].residency += residency; 177532ed618SSoby Mathew psci_non_cpu_stat[parent_idx][stat_idx].count++; 178532ed618SSoby Mathew 179532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 180532ed618SSoby Mathew } 181532ed618SSoby Mathew 182532ed618SSoby Mathew } 183532ed618SSoby Mathew 184532ed618SSoby Mathew /******************************************************************************* 185532ed618SSoby Mathew * This function returns the appropriate count and residency time of the 186532ed618SSoby Mathew * local state for the highest power level expressed in the `power_state` 187532ed618SSoby Mathew * for the node represented by `target_cpu`. 188532ed618SSoby Mathew ******************************************************************************/ 189532ed618SSoby Mathew int psci_get_stat(u_register_t target_cpu, unsigned int power_state, 190532ed618SSoby Mathew psci_stat_t *psci_stat) 191532ed618SSoby Mathew { 192532ed618SSoby Mathew int rc, pwrlvl, lvl, parent_idx, stat_idx, target_idx; 193532ed618SSoby Mathew psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} }; 194532ed618SSoby Mathew plat_local_state_t local_state; 195532ed618SSoby Mathew 196532ed618SSoby Mathew /* Validate the target_cpu parameter and determine the cpu index */ 197532ed618SSoby Mathew target_idx = plat_core_pos_by_mpidr(target_cpu); 198532ed618SSoby Mathew if (target_idx == -1) 199532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 200532ed618SSoby Mathew 201532ed618SSoby Mathew /* Validate the power_state parameter */ 202532ed618SSoby Mathew if (!psci_plat_pm_ops->translate_power_state_by_mpidr) 203532ed618SSoby Mathew rc = psci_validate_power_state(power_state, &state_info); 204532ed618SSoby Mathew else 205532ed618SSoby Mathew rc = psci_plat_pm_ops->translate_power_state_by_mpidr( 206532ed618SSoby Mathew target_cpu, power_state, &state_info); 207532ed618SSoby Mathew 208532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 209532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 210532ed618SSoby Mathew 211532ed618SSoby Mathew /* Find the highest power level */ 212532ed618SSoby Mathew pwrlvl = psci_find_target_suspend_lvl(&state_info); 213a1c3faa6SSandrine Bailleux if (pwrlvl == PSCI_INVALID_PWR_LVL) { 214a1c3faa6SSandrine Bailleux ERROR("Invalid target power level for PSCI statistics operation\n"); 215a1c3faa6SSandrine Bailleux panic(); 216a1c3faa6SSandrine Bailleux } 217532ed618SSoby Mathew 218532ed618SSoby Mathew /* Get the index into the stats array */ 219532ed618SSoby Mathew local_state = state_info.pwr_domain_state[pwrlvl]; 220532ed618SSoby Mathew stat_idx = get_stat_idx(local_state, pwrlvl); 221532ed618SSoby Mathew 222532ed618SSoby Mathew if (pwrlvl > PSCI_CPU_PWR_LVL) { 223532ed618SSoby Mathew /* Get the power domain index */ 224532ed618SSoby Mathew parent_idx = psci_cpu_pd_nodes[target_idx].parent_node; 225532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL + 1; lvl < pwrlvl; lvl++) 226532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 227532ed618SSoby Mathew 228532ed618SSoby Mathew /* Get the non cpu power domain stats */ 229532ed618SSoby Mathew *psci_stat = psci_non_cpu_stat[parent_idx][stat_idx]; 230532ed618SSoby Mathew } else { 231532ed618SSoby Mathew /* Get the cpu power domain stats */ 232532ed618SSoby Mathew *psci_stat = psci_cpu_stat[target_idx][stat_idx]; 233532ed618SSoby Mathew } 234532ed618SSoby Mathew 235532ed618SSoby Mathew return PSCI_E_SUCCESS; 236532ed618SSoby Mathew } 237532ed618SSoby Mathew 238532ed618SSoby Mathew /* This is the top level function for PSCI_STAT_RESIDENCY SMC. */ 239532ed618SSoby Mathew u_register_t psci_stat_residency(u_register_t target_cpu, 240532ed618SSoby Mathew unsigned int power_state) 241532ed618SSoby Mathew { 242532ed618SSoby Mathew psci_stat_t psci_stat; 243532ed618SSoby Mathew 244532ed618SSoby Mathew int rc = psci_get_stat(target_cpu, power_state, &psci_stat); 245532ed618SSoby Mathew if (rc == PSCI_E_SUCCESS) 246532ed618SSoby Mathew return psci_stat.residency; 247532ed618SSoby Mathew else 248532ed618SSoby Mathew return 0; 249532ed618SSoby Mathew } 250532ed618SSoby Mathew 251532ed618SSoby Mathew /* This is the top level function for PSCI_STAT_COUNT SMC. */ 252532ed618SSoby Mathew u_register_t psci_stat_count(u_register_t target_cpu, 253532ed618SSoby Mathew unsigned int power_state) 254532ed618SSoby Mathew { 255532ed618SSoby Mathew psci_stat_t psci_stat; 256532ed618SSoby Mathew 257532ed618SSoby Mathew int rc = psci_get_stat(target_cpu, power_state, &psci_stat); 258532ed618SSoby Mathew if (rc == PSCI_E_SUCCESS) 259532ed618SSoby Mathew return psci_stat.count; 260532ed618SSoby Mathew else 261532ed618SSoby Mathew return 0; 262532ed618SSoby Mathew } 263