1*532ed618SSoby Mathew /* 2*532ed618SSoby Mathew * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3*532ed618SSoby Mathew * 4*532ed618SSoby Mathew * Redistribution and use in source and binary forms, with or without 5*532ed618SSoby Mathew * modification, are permitted provided that the following conditions are met: 6*532ed618SSoby Mathew * 7*532ed618SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8*532ed618SSoby Mathew * list of conditions and the following disclaimer. 9*532ed618SSoby Mathew * 10*532ed618SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11*532ed618SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12*532ed618SSoby Mathew * and/or other materials provided with the distribution. 13*532ed618SSoby Mathew * 14*532ed618SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15*532ed618SSoby Mathew * to endorse or promote products derived from this software without specific 16*532ed618SSoby Mathew * prior written permission. 17*532ed618SSoby Mathew * 18*532ed618SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*532ed618SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*532ed618SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*532ed618SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*532ed618SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*532ed618SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*532ed618SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*532ed618SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*532ed618SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*532ed618SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*532ed618SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29*532ed618SSoby Mathew */ 30*532ed618SSoby Mathew 31*532ed618SSoby Mathew #include <assert.h> 32*532ed618SSoby Mathew #include <debug.h> 33*532ed618SSoby Mathew #include <platform.h> 34*532ed618SSoby Mathew #include <platform_def.h> 35*532ed618SSoby Mathew #include "psci_private.h" 36*532ed618SSoby Mathew 37*532ed618SSoby Mathew #ifndef PLAT_MAX_PWR_LVL_STATES 38*532ed618SSoby Mathew #define PLAT_MAX_PWR_LVL_STATES 2 39*532ed618SSoby Mathew #endif 40*532ed618SSoby Mathew 41*532ed618SSoby Mathew /* Ticks elapsed in one second by a signal of 1 MHz */ 42*532ed618SSoby Mathew #define MHZ_TICKS_PER_SEC 1000000 43*532ed618SSoby Mathew 44*532ed618SSoby Mathew /* Following structure is used for PSCI STAT */ 45*532ed618SSoby Mathew typedef struct psci_stat { 46*532ed618SSoby Mathew u_register_t residency; 47*532ed618SSoby Mathew u_register_t count; 48*532ed618SSoby Mathew } psci_stat_t; 49*532ed618SSoby Mathew 50*532ed618SSoby Mathew /* 51*532ed618SSoby Mathew * Following is used to keep track of the last cpu 52*532ed618SSoby Mathew * that goes to power down in non cpu power domains. 53*532ed618SSoby Mathew */ 54*532ed618SSoby Mathew static int last_cpu_in_non_cpu_pd[PSCI_NUM_NON_CPU_PWR_DOMAINS] = {-1}; 55*532ed618SSoby Mathew 56*532ed618SSoby Mathew /* 57*532ed618SSoby Mathew * Following are used to store PSCI STAT values for 58*532ed618SSoby Mathew * CPU and non CPU power domains. 59*532ed618SSoby Mathew */ 60*532ed618SSoby Mathew static psci_stat_t psci_cpu_stat[PLATFORM_CORE_COUNT] 61*532ed618SSoby Mathew [PLAT_MAX_PWR_LVL_STATES]; 62*532ed618SSoby Mathew static psci_stat_t psci_non_cpu_stat[PSCI_NUM_NON_CPU_PWR_DOMAINS] 63*532ed618SSoby Mathew [PLAT_MAX_PWR_LVL_STATES]; 64*532ed618SSoby Mathew 65*532ed618SSoby Mathew /* Register PMF PSCI service */ 66*532ed618SSoby Mathew PMF_REGISTER_SERVICE(psci_svc, PMF_PSCI_STAT_SVC_ID, 67*532ed618SSoby Mathew PSCI_STAT_TOTAL_IDS, PMF_STORE_ENABLE) 68*532ed618SSoby Mathew 69*532ed618SSoby Mathew /* The divisor to use to convert raw timestamp into microseconds */ 70*532ed618SSoby Mathew u_register_t residency_div; 71*532ed618SSoby Mathew 72*532ed618SSoby Mathew /* 73*532ed618SSoby Mathew * This macro calculates the stats residency in microseconds, 74*532ed618SSoby Mathew * taking in account the wrap around condition. 75*532ed618SSoby Mathew */ 76*532ed618SSoby Mathew #define calc_stat_residency(_pwrupts, _pwrdnts, _res) \ 77*532ed618SSoby Mathew do { \ 78*532ed618SSoby Mathew if (_pwrupts < _pwrdnts) \ 79*532ed618SSoby Mathew _res = UINT64_MAX - _pwrdnts + _pwrupts;\ 80*532ed618SSoby Mathew else \ 81*532ed618SSoby Mathew _res = _pwrupts - _pwrdnts; \ 82*532ed618SSoby Mathew /* Convert timestamp into microseconds */ \ 83*532ed618SSoby Mathew _res = _res/residency_div; \ 84*532ed618SSoby Mathew } while (0) 85*532ed618SSoby Mathew 86*532ed618SSoby Mathew /* 87*532ed618SSoby Mathew * This functions returns the index into the `psci_stat_t` array given the 88*532ed618SSoby Mathew * local power state and power domain level. If the platform implements the 89*532ed618SSoby Mathew * `get_pwr_lvl_state_idx` pm hook, then that will be used to return the index. 90*532ed618SSoby Mathew */ 91*532ed618SSoby Mathew static int get_stat_idx(plat_local_state_t local_state, int pwr_lvl) 92*532ed618SSoby Mathew { 93*532ed618SSoby Mathew int idx; 94*532ed618SSoby Mathew 95*532ed618SSoby Mathew if (psci_plat_pm_ops->get_pwr_lvl_state_idx == NULL) { 96*532ed618SSoby Mathew assert(PLAT_MAX_PWR_LVL_STATES == 2); 97*532ed618SSoby Mathew if (is_local_state_retn(local_state)) 98*532ed618SSoby Mathew return 0; 99*532ed618SSoby Mathew 100*532ed618SSoby Mathew assert(is_local_state_off(local_state)); 101*532ed618SSoby Mathew return 1; 102*532ed618SSoby Mathew } 103*532ed618SSoby Mathew 104*532ed618SSoby Mathew idx = psci_plat_pm_ops->get_pwr_lvl_state_idx(local_state, pwr_lvl); 105*532ed618SSoby Mathew assert((idx >= 0) && (idx < PLAT_MAX_PWR_LVL_STATES)); 106*532ed618SSoby Mathew return idx; 107*532ed618SSoby Mathew } 108*532ed618SSoby Mathew 109*532ed618SSoby Mathew /******************************************************************************* 110*532ed618SSoby Mathew * This function is passed the target local power states for each power 111*532ed618SSoby Mathew * domain (state_info) between the current CPU domain and its ancestors until 112*532ed618SSoby Mathew * the target power level (end_pwrlvl). 113*532ed618SSoby Mathew * 114*532ed618SSoby Mathew * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it 115*532ed618SSoby Mathew * updates the `last_cpu_in_non_cpu_pd[]` with last power down cpu id. 116*532ed618SSoby Mathew * 117*532ed618SSoby Mathew * This function will only be invoked with data cache enabled and while 118*532ed618SSoby Mathew * powering down a core. 119*532ed618SSoby Mathew ******************************************************************************/ 120*532ed618SSoby Mathew void psci_stats_update_pwr_down(unsigned int end_pwrlvl, 121*532ed618SSoby Mathew const psci_power_state_t *state_info) 122*532ed618SSoby Mathew { 123*532ed618SSoby Mathew int lvl, parent_idx, cpu_idx = plat_my_core_pos(); 124*532ed618SSoby Mathew 125*532ed618SSoby Mathew assert(end_pwrlvl <= PLAT_MAX_PWR_LVL); 126*532ed618SSoby Mathew assert(state_info); 127*532ed618SSoby Mathew 128*532ed618SSoby Mathew parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 129*532ed618SSoby Mathew 130*532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) { 131*532ed618SSoby Mathew 132*532ed618SSoby Mathew /* Break early if the target power state is RUN */ 133*532ed618SSoby Mathew if (is_local_state_run(state_info->pwr_domain_state[lvl])) 134*532ed618SSoby Mathew break; 135*532ed618SSoby Mathew 136*532ed618SSoby Mathew /* 137*532ed618SSoby Mathew * The power domain is entering a low power state, so this is 138*532ed618SSoby Mathew * the last CPU for this power domain 139*532ed618SSoby Mathew */ 140*532ed618SSoby Mathew last_cpu_in_non_cpu_pd[parent_idx] = cpu_idx; 141*532ed618SSoby Mathew 142*532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 143*532ed618SSoby Mathew } 144*532ed618SSoby Mathew 145*532ed618SSoby Mathew } 146*532ed618SSoby Mathew 147*532ed618SSoby Mathew /******************************************************************************* 148*532ed618SSoby Mathew * This function updates the PSCI STATS(residency time and count) for CPU 149*532ed618SSoby Mathew * and NON-CPU power domains. 150*532ed618SSoby Mathew * It is called with caches enabled and locks acquired(for NON-CPU domain) 151*532ed618SSoby Mathew ******************************************************************************/ 152*532ed618SSoby Mathew void psci_stats_update_pwr_up(unsigned int end_pwrlvl, 153*532ed618SSoby Mathew const psci_power_state_t *state_info, 154*532ed618SSoby Mathew unsigned int flags) 155*532ed618SSoby Mathew { 156*532ed618SSoby Mathew int parent_idx, cpu_idx = plat_my_core_pos(); 157*532ed618SSoby Mathew int lvl, stat_idx; 158*532ed618SSoby Mathew plat_local_state_t local_state; 159*532ed618SSoby Mathew unsigned long long pwrup_ts = 0, pwrdn_ts = 0; 160*532ed618SSoby Mathew u_register_t residency; 161*532ed618SSoby Mathew 162*532ed618SSoby Mathew assert(end_pwrlvl <= PLAT_MAX_PWR_LVL); 163*532ed618SSoby Mathew assert(state_info); 164*532ed618SSoby Mathew 165*532ed618SSoby Mathew /* Initialize the residency divisor if not already initialized */ 166*532ed618SSoby Mathew if (!residency_div) { 167*532ed618SSoby Mathew /* Pre-calculate divisor so that it can be directly used to 168*532ed618SSoby Mathew convert time-stamp into microseconds */ 169*532ed618SSoby Mathew residency_div = read_cntfrq_el0() / MHZ_TICKS_PER_SEC; 170*532ed618SSoby Mathew assert(residency_div); 171*532ed618SSoby Mathew } 172*532ed618SSoby Mathew 173*532ed618SSoby Mathew /* Get power down time-stamp for current CPU */ 174*532ed618SSoby Mathew PMF_GET_TIMESTAMP_BY_INDEX(psci_svc, PSCI_STAT_ID_ENTER_LOW_PWR, 175*532ed618SSoby Mathew cpu_idx, flags, pwrdn_ts); 176*532ed618SSoby Mathew 177*532ed618SSoby Mathew /* In the case of 1st power on just return */ 178*532ed618SSoby Mathew if (!pwrdn_ts) 179*532ed618SSoby Mathew return; 180*532ed618SSoby Mathew 181*532ed618SSoby Mathew /* Get power up time-stamp for current CPU */ 182*532ed618SSoby Mathew PMF_GET_TIMESTAMP_BY_INDEX(psci_svc, PSCI_STAT_ID_EXIT_LOW_PWR, 183*532ed618SSoby Mathew cpu_idx, flags, pwrup_ts); 184*532ed618SSoby Mathew 185*532ed618SSoby Mathew /* Get the index into the stats array */ 186*532ed618SSoby Mathew local_state = state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]; 187*532ed618SSoby Mathew stat_idx = get_stat_idx(local_state, PSCI_CPU_PWR_LVL); 188*532ed618SSoby Mathew 189*532ed618SSoby Mathew /* Calculate stats residency */ 190*532ed618SSoby Mathew calc_stat_residency(pwrup_ts, pwrdn_ts, residency); 191*532ed618SSoby Mathew 192*532ed618SSoby Mathew /* Update CPU stats. */ 193*532ed618SSoby Mathew psci_cpu_stat[cpu_idx][stat_idx].residency += residency; 194*532ed618SSoby Mathew psci_cpu_stat[cpu_idx][stat_idx].count++; 195*532ed618SSoby Mathew 196*532ed618SSoby Mathew /* 197*532ed618SSoby Mathew * Check what power domains above CPU were off 198*532ed618SSoby Mathew * prior to this CPU powering on. 199*532ed618SSoby Mathew */ 200*532ed618SSoby Mathew parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 201*532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) { 202*532ed618SSoby Mathew local_state = state_info->pwr_domain_state[lvl]; 203*532ed618SSoby Mathew if (is_local_state_run(local_state)) { 204*532ed618SSoby Mathew /* Break early */ 205*532ed618SSoby Mathew break; 206*532ed618SSoby Mathew } 207*532ed618SSoby Mathew 208*532ed618SSoby Mathew assert(last_cpu_in_non_cpu_pd[parent_idx] != -1); 209*532ed618SSoby Mathew 210*532ed618SSoby Mathew /* Get power down time-stamp for last CPU */ 211*532ed618SSoby Mathew PMF_GET_TIMESTAMP_BY_INDEX(psci_svc, PSCI_STAT_ID_ENTER_LOW_PWR, 212*532ed618SSoby Mathew last_cpu_in_non_cpu_pd[parent_idx], 213*532ed618SSoby Mathew flags, pwrdn_ts); 214*532ed618SSoby Mathew 215*532ed618SSoby Mathew /* Initialize back to reset value */ 216*532ed618SSoby Mathew last_cpu_in_non_cpu_pd[parent_idx] = -1; 217*532ed618SSoby Mathew 218*532ed618SSoby Mathew /* Get the index into the stats array */ 219*532ed618SSoby Mathew stat_idx = get_stat_idx(local_state, lvl); 220*532ed618SSoby Mathew 221*532ed618SSoby Mathew /* Calculate stats residency */ 222*532ed618SSoby Mathew calc_stat_residency(pwrup_ts, pwrdn_ts, residency); 223*532ed618SSoby Mathew 224*532ed618SSoby Mathew /* Update non cpu stats */ 225*532ed618SSoby Mathew psci_non_cpu_stat[parent_idx][stat_idx].residency += residency; 226*532ed618SSoby Mathew psci_non_cpu_stat[parent_idx][stat_idx].count++; 227*532ed618SSoby Mathew 228*532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 229*532ed618SSoby Mathew } 230*532ed618SSoby Mathew 231*532ed618SSoby Mathew } 232*532ed618SSoby Mathew 233*532ed618SSoby Mathew /******************************************************************************* 234*532ed618SSoby Mathew * This function returns the appropriate count and residency time of the 235*532ed618SSoby Mathew * local state for the highest power level expressed in the `power_state` 236*532ed618SSoby Mathew * for the node represented by `target_cpu`. 237*532ed618SSoby Mathew ******************************************************************************/ 238*532ed618SSoby Mathew int psci_get_stat(u_register_t target_cpu, unsigned int power_state, 239*532ed618SSoby Mathew psci_stat_t *psci_stat) 240*532ed618SSoby Mathew { 241*532ed618SSoby Mathew int rc, pwrlvl, lvl, parent_idx, stat_idx, target_idx; 242*532ed618SSoby Mathew psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} }; 243*532ed618SSoby Mathew plat_local_state_t local_state; 244*532ed618SSoby Mathew 245*532ed618SSoby Mathew /* Validate the target_cpu parameter and determine the cpu index */ 246*532ed618SSoby Mathew target_idx = plat_core_pos_by_mpidr(target_cpu); 247*532ed618SSoby Mathew if (target_idx == -1) 248*532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 249*532ed618SSoby Mathew 250*532ed618SSoby Mathew /* Validate the power_state parameter */ 251*532ed618SSoby Mathew if (!psci_plat_pm_ops->translate_power_state_by_mpidr) 252*532ed618SSoby Mathew rc = psci_validate_power_state(power_state, &state_info); 253*532ed618SSoby Mathew else 254*532ed618SSoby Mathew rc = psci_plat_pm_ops->translate_power_state_by_mpidr( 255*532ed618SSoby Mathew target_cpu, power_state, &state_info); 256*532ed618SSoby Mathew 257*532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 258*532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 259*532ed618SSoby Mathew 260*532ed618SSoby Mathew /* Find the highest power level */ 261*532ed618SSoby Mathew pwrlvl = psci_find_target_suspend_lvl(&state_info); 262*532ed618SSoby Mathew if (pwrlvl == PSCI_INVALID_PWR_LVL) 263*532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 264*532ed618SSoby Mathew 265*532ed618SSoby Mathew /* Get the index into the stats array */ 266*532ed618SSoby Mathew local_state = state_info.pwr_domain_state[pwrlvl]; 267*532ed618SSoby Mathew stat_idx = get_stat_idx(local_state, pwrlvl); 268*532ed618SSoby Mathew 269*532ed618SSoby Mathew if (pwrlvl > PSCI_CPU_PWR_LVL) { 270*532ed618SSoby Mathew /* Get the power domain index */ 271*532ed618SSoby Mathew parent_idx = psci_cpu_pd_nodes[target_idx].parent_node; 272*532ed618SSoby Mathew for (lvl = PSCI_CPU_PWR_LVL + 1; lvl < pwrlvl; lvl++) 273*532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 274*532ed618SSoby Mathew 275*532ed618SSoby Mathew /* Get the non cpu power domain stats */ 276*532ed618SSoby Mathew *psci_stat = psci_non_cpu_stat[parent_idx][stat_idx]; 277*532ed618SSoby Mathew } else { 278*532ed618SSoby Mathew /* Get the cpu power domain stats */ 279*532ed618SSoby Mathew *psci_stat = psci_cpu_stat[target_idx][stat_idx]; 280*532ed618SSoby Mathew } 281*532ed618SSoby Mathew 282*532ed618SSoby Mathew return PSCI_E_SUCCESS; 283*532ed618SSoby Mathew } 284*532ed618SSoby Mathew 285*532ed618SSoby Mathew /* This is the top level function for PSCI_STAT_RESIDENCY SMC. */ 286*532ed618SSoby Mathew u_register_t psci_stat_residency(u_register_t target_cpu, 287*532ed618SSoby Mathew unsigned int power_state) 288*532ed618SSoby Mathew { 289*532ed618SSoby Mathew psci_stat_t psci_stat; 290*532ed618SSoby Mathew 291*532ed618SSoby Mathew int rc = psci_get_stat(target_cpu, power_state, &psci_stat); 292*532ed618SSoby Mathew if (rc == PSCI_E_SUCCESS) 293*532ed618SSoby Mathew return psci_stat.residency; 294*532ed618SSoby Mathew else 295*532ed618SSoby Mathew return 0; 296*532ed618SSoby Mathew } 297*532ed618SSoby Mathew 298*532ed618SSoby Mathew /* This is the top level function for PSCI_STAT_COUNT SMC. */ 299*532ed618SSoby Mathew u_register_t psci_stat_count(u_register_t target_cpu, 300*532ed618SSoby Mathew unsigned int power_state) 301*532ed618SSoby Mathew { 302*532ed618SSoby Mathew psci_stat_t psci_stat; 303*532ed618SSoby Mathew 304*532ed618SSoby Mathew int rc = psci_get_stat(target_cpu, power_state, &psci_stat); 305*532ed618SSoby Mathew if (rc == PSCI_E_SUCCESS) 306*532ed618SSoby Mathew return psci_stat.count; 307*532ed618SSoby Mathew else 308*532ed618SSoby Mathew return 0; 309*532ed618SSoby Mathew } 310