1532ed618SSoby Mathew /* 2532ed618SSoby Mathew * Copyright (c) 2013-2016, 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 #ifndef __PSCI_PRIVATE_H__ 32532ed618SSoby Mathew #define __PSCI_PRIVATE_H__ 33532ed618SSoby Mathew 34532ed618SSoby Mathew #include <arch.h> 35532ed618SSoby Mathew #include <bakery_lock.h> 36532ed618SSoby Mathew #include <bl_common.h> 37532ed618SSoby Mathew #include <cpu_data.h> 38532ed618SSoby Mathew #include <pmf.h> 39532ed618SSoby Mathew #include <psci.h> 40532ed618SSoby Mathew #include <spinlock.h> 41532ed618SSoby Mathew 42532ed618SSoby Mathew /* 43532ed618SSoby Mathew * The following helper macros abstract the interface to the Bakery 44532ed618SSoby Mathew * Lock API. 45532ed618SSoby Mathew */ 46532ed618SSoby Mathew #define psci_lock_init(non_cpu_pd_node, idx) \ 47532ed618SSoby Mathew ((non_cpu_pd_node)[(idx)].lock_index = (idx)) 48532ed618SSoby Mathew #define psci_lock_get(non_cpu_pd_node) \ 49532ed618SSoby Mathew bakery_lock_get(&psci_locks[(non_cpu_pd_node)->lock_index]) 50532ed618SSoby Mathew #define psci_lock_release(non_cpu_pd_node) \ 51532ed618SSoby Mathew bakery_lock_release(&psci_locks[(non_cpu_pd_node)->lock_index]) 52532ed618SSoby Mathew 53532ed618SSoby Mathew /* 54532ed618SSoby Mathew * The PSCI capability which are provided by the generic code but does not 55532ed618SSoby Mathew * depend on the platform or spd capabilities. 56532ed618SSoby Mathew */ 57532ed618SSoby Mathew #define PSCI_GENERIC_CAP \ 58532ed618SSoby Mathew (define_psci_cap(PSCI_VERSION) | \ 59532ed618SSoby Mathew define_psci_cap(PSCI_AFFINITY_INFO_AARCH64) | \ 60532ed618SSoby Mathew define_psci_cap(PSCI_FEATURES)) 61532ed618SSoby Mathew 62532ed618SSoby Mathew /* 63532ed618SSoby Mathew * The PSCI capabilities mask for 64 bit functions. 64532ed618SSoby Mathew */ 65532ed618SSoby Mathew #define PSCI_CAP_64BIT_MASK \ 66532ed618SSoby Mathew (define_psci_cap(PSCI_CPU_SUSPEND_AARCH64) | \ 67532ed618SSoby Mathew define_psci_cap(PSCI_CPU_ON_AARCH64) | \ 68532ed618SSoby Mathew define_psci_cap(PSCI_AFFINITY_INFO_AARCH64) | \ 69532ed618SSoby Mathew define_psci_cap(PSCI_MIG_AARCH64) | \ 70532ed618SSoby Mathew define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64) | \ 71532ed618SSoby Mathew define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64) | \ 72532ed618SSoby Mathew define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64) | \ 73532ed618SSoby Mathew define_psci_cap(PSCI_STAT_COUNT_AARCH64)) 74532ed618SSoby Mathew 75532ed618SSoby Mathew /* 76532ed618SSoby Mathew * Helper macros to get/set the fields of PSCI per-cpu data. 77532ed618SSoby Mathew */ 78532ed618SSoby Mathew #define psci_set_aff_info_state(aff_state) \ 79532ed618SSoby Mathew set_cpu_data(psci_svc_cpu_data.aff_info_state, aff_state) 80532ed618SSoby Mathew #define psci_get_aff_info_state() \ 81532ed618SSoby Mathew get_cpu_data(psci_svc_cpu_data.aff_info_state) 82532ed618SSoby Mathew #define psci_get_aff_info_state_by_idx(idx) \ 83532ed618SSoby Mathew get_cpu_data_by_index(idx, psci_svc_cpu_data.aff_info_state) 84532ed618SSoby Mathew #define psci_set_aff_info_state_by_idx(idx, aff_state) \ 85532ed618SSoby Mathew set_cpu_data_by_index(idx, psci_svc_cpu_data.aff_info_state,\ 86532ed618SSoby Mathew aff_state) 87532ed618SSoby Mathew #define psci_get_suspend_pwrlvl() \ 88532ed618SSoby Mathew get_cpu_data(psci_svc_cpu_data.target_pwrlvl) 89532ed618SSoby Mathew #define psci_set_suspend_pwrlvl(target_lvl) \ 90532ed618SSoby Mathew set_cpu_data(psci_svc_cpu_data.target_pwrlvl, target_lvl) 91532ed618SSoby Mathew #define psci_set_cpu_local_state(state) \ 92532ed618SSoby Mathew set_cpu_data(psci_svc_cpu_data.local_state, state) 93532ed618SSoby Mathew #define psci_get_cpu_local_state() \ 94532ed618SSoby Mathew get_cpu_data(psci_svc_cpu_data.local_state) 95532ed618SSoby Mathew #define psci_get_cpu_local_state_by_idx(idx) \ 96532ed618SSoby Mathew get_cpu_data_by_index(idx, psci_svc_cpu_data.local_state) 97532ed618SSoby Mathew 98532ed618SSoby Mathew /* 99532ed618SSoby Mathew * Helper macros for the CPU level spinlocks 100532ed618SSoby Mathew */ 101532ed618SSoby Mathew #define psci_spin_lock_cpu(idx) spin_lock(&psci_cpu_pd_nodes[idx].cpu_lock) 102532ed618SSoby Mathew #define psci_spin_unlock_cpu(idx) spin_unlock(&psci_cpu_pd_nodes[idx].cpu_lock) 103532ed618SSoby Mathew 104532ed618SSoby Mathew /* Helper macro to identify a CPU standby request in PSCI Suspend call */ 105532ed618SSoby Mathew #define is_cpu_standby_req(is_power_down_state, retn_lvl) \ 106532ed618SSoby Mathew (((!(is_power_down_state)) && ((retn_lvl) == 0)) ? 1 : 0) 107532ed618SSoby Mathew 108532ed618SSoby Mathew /* Following are used as ID's to capture time-stamp */ 109532ed618SSoby Mathew #define PSCI_STAT_ID_ENTER_LOW_PWR 0 110532ed618SSoby Mathew #define PSCI_STAT_ID_EXIT_LOW_PWR 1 111532ed618SSoby Mathew #define PSCI_STAT_TOTAL_IDS 2 112532ed618SSoby Mathew 113532ed618SSoby Mathew /* Declare PMF service functions for PSCI */ 114532ed618SSoby Mathew PMF_DECLARE_CAPTURE_TIMESTAMP(psci_svc) 115532ed618SSoby Mathew PMF_DECLARE_GET_TIMESTAMP(psci_svc) 116532ed618SSoby Mathew 117532ed618SSoby Mathew /******************************************************************************* 118532ed618SSoby Mathew * The following two data structures implement the power domain tree. The tree 119532ed618SSoby Mathew * is used to track the state of all the nodes i.e. power domain instances 120532ed618SSoby Mathew * described by the platform. The tree consists of nodes that describe CPU power 121532ed618SSoby Mathew * domains i.e. leaf nodes and all other power domains which are parents of a 122532ed618SSoby Mathew * CPU power domain i.e. non-leaf nodes. 123532ed618SSoby Mathew ******************************************************************************/ 124532ed618SSoby Mathew typedef struct non_cpu_pwr_domain_node { 125532ed618SSoby Mathew /* 126532ed618SSoby Mathew * Index of the first CPU power domain node level 0 which has this node 127532ed618SSoby Mathew * as its parent. 128532ed618SSoby Mathew */ 129532ed618SSoby Mathew unsigned int cpu_start_idx; 130532ed618SSoby Mathew 131532ed618SSoby Mathew /* 132532ed618SSoby Mathew * Number of CPU power domains which are siblings of the domain indexed 133532ed618SSoby Mathew * by 'cpu_start_idx' i.e. all the domains in the range 'cpu_start_idx 134532ed618SSoby Mathew * -> cpu_start_idx + ncpus' have this node as their parent. 135532ed618SSoby Mathew */ 136532ed618SSoby Mathew unsigned int ncpus; 137532ed618SSoby Mathew 138532ed618SSoby Mathew /* 139532ed618SSoby Mathew * Index of the parent power domain node. 140532ed618SSoby Mathew * TODO: Figure out whether to whether using pointer is more efficient. 141532ed618SSoby Mathew */ 142532ed618SSoby Mathew unsigned int parent_node; 143532ed618SSoby Mathew 144532ed618SSoby Mathew plat_local_state_t local_state; 145532ed618SSoby Mathew 146532ed618SSoby Mathew unsigned char level; 147532ed618SSoby Mathew 148532ed618SSoby Mathew /* For indexing the psci_lock array*/ 149532ed618SSoby Mathew unsigned char lock_index; 150532ed618SSoby Mathew } non_cpu_pd_node_t; 151532ed618SSoby Mathew 152532ed618SSoby Mathew typedef struct cpu_pwr_domain_node { 153532ed618SSoby Mathew u_register_t mpidr; 154532ed618SSoby Mathew 155532ed618SSoby Mathew /* 156532ed618SSoby Mathew * Index of the parent power domain node. 157532ed618SSoby Mathew * TODO: Figure out whether to whether using pointer is more efficient. 158532ed618SSoby Mathew */ 159532ed618SSoby Mathew unsigned int parent_node; 160532ed618SSoby Mathew 161532ed618SSoby Mathew /* 162532ed618SSoby Mathew * A CPU power domain does not require state coordination like its 163532ed618SSoby Mathew * parent power domains. Hence this node does not include a bakery 164532ed618SSoby Mathew * lock. A spinlock is required by the CPU_ON handler to prevent a race 165532ed618SSoby Mathew * when multiple CPUs try to turn ON the same target CPU. 166532ed618SSoby Mathew */ 167532ed618SSoby Mathew spinlock_t cpu_lock; 168532ed618SSoby Mathew } cpu_pd_node_t; 169532ed618SSoby Mathew 170532ed618SSoby Mathew /******************************************************************************* 171532ed618SSoby Mathew * Data prototypes 172532ed618SSoby Mathew ******************************************************************************/ 173532ed618SSoby Mathew extern const plat_psci_ops_t *psci_plat_pm_ops; 174532ed618SSoby Mathew extern non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]; 175532ed618SSoby Mathew extern cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT]; 176532ed618SSoby Mathew extern unsigned int psci_caps; 177532ed618SSoby Mathew 178532ed618SSoby Mathew /* One bakery lock is required for each non-cpu power domain */ 179532ed618SSoby Mathew DECLARE_BAKERY_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]); 180532ed618SSoby Mathew 181532ed618SSoby Mathew /******************************************************************************* 182532ed618SSoby Mathew * SPD's power management hooks registered with PSCI 183532ed618SSoby Mathew ******************************************************************************/ 184532ed618SSoby Mathew extern const spd_pm_ops_t *psci_spd_pm; 185532ed618SSoby Mathew 186532ed618SSoby Mathew /******************************************************************************* 187532ed618SSoby Mathew * Function prototypes 188532ed618SSoby Mathew ******************************************************************************/ 189532ed618SSoby Mathew /* Private exported functions from psci_common.c */ 190532ed618SSoby Mathew int psci_validate_power_state(unsigned int power_state, 191532ed618SSoby Mathew psci_power_state_t *state_info); 192532ed618SSoby Mathew void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info); 193532ed618SSoby Mathew int psci_validate_mpidr(u_register_t mpidr); 194532ed618SSoby Mathew void psci_init_req_local_pwr_states(void); 195*61eae524SAchin Gupta void psci_get_target_local_pwr_states(unsigned int end_pwrlvl, 196*61eae524SAchin Gupta psci_power_state_t *target_state); 197532ed618SSoby Mathew int psci_validate_entry_point(entry_point_info_t *ep, 198532ed618SSoby Mathew uintptr_t entrypoint, u_register_t context_id); 199532ed618SSoby Mathew void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx, 200532ed618SSoby Mathew unsigned int end_lvl, 201532ed618SSoby Mathew unsigned int node_index[]); 202532ed618SSoby Mathew void psci_do_state_coordination(unsigned int end_pwrlvl, 203532ed618SSoby Mathew psci_power_state_t *state_info); 204532ed618SSoby Mathew void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl, 205532ed618SSoby Mathew unsigned int cpu_idx); 206532ed618SSoby Mathew void psci_release_pwr_domain_locks(unsigned int end_pwrlvl, 207532ed618SSoby Mathew unsigned int cpu_idx); 208532ed618SSoby Mathew int psci_validate_suspend_req(const psci_power_state_t *state_info, 209532ed618SSoby Mathew unsigned int is_power_down_state_req); 210532ed618SSoby Mathew unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info); 211532ed618SSoby Mathew unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info); 212532ed618SSoby Mathew void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl); 213532ed618SSoby Mathew void psci_print_power_domain_map(void); 214532ed618SSoby Mathew unsigned int psci_is_last_on_cpu(void); 215532ed618SSoby Mathew int psci_spd_migrate_info(u_register_t *mpidr); 216532ed618SSoby Mathew 217532ed618SSoby Mathew /* Private exported functions from psci_on.c */ 218532ed618SSoby Mathew int psci_cpu_on_start(u_register_t target_cpu, 219532ed618SSoby Mathew entry_point_info_t *ep); 220532ed618SSoby Mathew 221532ed618SSoby Mathew void psci_cpu_on_finish(unsigned int cpu_idx, 222532ed618SSoby Mathew psci_power_state_t *state_info); 223532ed618SSoby Mathew 224532ed618SSoby Mathew /* Private exported functions from psci_off.c */ 225532ed618SSoby Mathew int psci_do_cpu_off(unsigned int end_pwrlvl); 226532ed618SSoby Mathew 227532ed618SSoby Mathew /* Private exported functions from psci_suspend.c */ 228532ed618SSoby Mathew void psci_cpu_suspend_start(entry_point_info_t *ep, 229532ed618SSoby Mathew unsigned int end_pwrlvl, 230532ed618SSoby Mathew psci_power_state_t *state_info, 231532ed618SSoby Mathew unsigned int is_power_down_state_req); 232532ed618SSoby Mathew 233532ed618SSoby Mathew void psci_cpu_suspend_finish(unsigned int cpu_idx, 234532ed618SSoby Mathew psci_power_state_t *state_info); 235532ed618SSoby Mathew 236532ed618SSoby Mathew /* Private exported functions from psci_helpers.S */ 237532ed618SSoby Mathew void psci_do_pwrdown_cache_maintenance(unsigned int pwr_level); 238532ed618SSoby Mathew void psci_do_pwrup_cache_maintenance(void); 239532ed618SSoby Mathew 240532ed618SSoby Mathew /* Private exported functions from psci_system_off.c */ 241532ed618SSoby Mathew void __dead2 psci_system_off(void); 242532ed618SSoby Mathew void __dead2 psci_system_reset(void); 243532ed618SSoby Mathew 244532ed618SSoby Mathew /* Private exported functions from psci_stat.c */ 245532ed618SSoby Mathew void psci_stats_update_pwr_down(unsigned int end_pwrlvl, 246532ed618SSoby Mathew const psci_power_state_t *state_info); 247532ed618SSoby Mathew void psci_stats_update_pwr_up(unsigned int end_pwrlvl, 248532ed618SSoby Mathew const psci_power_state_t *state_info, 249532ed618SSoby Mathew unsigned int flags); 250532ed618SSoby Mathew u_register_t psci_stat_residency(u_register_t target_cpu, 251532ed618SSoby Mathew unsigned int power_state); 252532ed618SSoby Mathew u_register_t psci_stat_count(u_register_t target_cpu, 253532ed618SSoby Mathew unsigned int power_state); 254532ed618SSoby Mathew 255532ed618SSoby Mathew #endif /* __PSCI_PRIVATE_H__ */ 256