Commit 8ee24980 authored by Soby Mathew's avatar Soby Mathew Committed by Achin Gupta
Browse files

PSCI: Add framework to handle composite power states

The state-id field in the power-state parameter of a CPU_SUSPEND call can be
used to describe composite power states specific to a platform. The current PSCI
implementation does not interpret the state-id field. It relies on the target
power level and the state type fields in the power-state parameter to perform
state coordination and power management operations. The framework introduced
in this patch allows the PSCI implementation to intepret generic global states
like RUN, RETENTION or OFF from the State-ID to make global state coordination
decisions and reduce the complexity of platform ports. It adds support to
involve the platform in state coordination which facilitates the use of
composite power states and improves the support for entering standby states
at multiple power domains.

The patch also includes support for extended state-id format for the power
state parameter as specified by PSCIv1.0.

The PSCI implementation now defines a generic representation of the power-state
parameter. It depends on the platform port to convert the power-state parameter
(possibly encoding a composite power state) passed in a CPU_SUSPEND call to this
representation via the `validate_power_state()` plat_psci_ops handler. It is an
array where each index corresponds to a power level. Each entry contains the
local power state the power domain at that power level could enter.

The meaning of the local power state values is platform defined, and may vary
between levels in a single platform. The PSCI implementation constrains the
values only so that it can classify the state as RUN, RETENTION or OFF as
required by the specification:
   * zero means RUN
   * all OFF state values at all levels must be higher than all RETENTION
     state values at all levels
   * the platform provides PLAT_MAX_RET_STATE and PLAT_MAX_OFF_STATE values
     to the framework

The platform also must define the macros PLAT_MAX_RET_STATE and
PLAT_MAX_OFF_STATE which lets the PSCI implementation find out which power
domains have been requested to enter a retention or power down state. The PSCI
implementation does not interpret the local power states defined by the
platform. The only constraint is that the PLAT_MAX_RET_STATE <
PLAT_MAX_OFF_STATE.

For a power domain tree, the generic implementation maintains an array of local
power states. These are the states requested for each power domain by all the
cores contained within the domain. During a request to place multiple power
domains in a low power state, the platform is passed an array of requested
power-states for each power domain through the plat_get_target_pwr_state()
API. It coordinates amongst these states to determine a target local power
state for the power domain. A default weak implementation of this API is
provided in the platform layer which returns the minimum of the requested
power-states back to the PSCI state coordination.

Finally, the plat_psci_ops power management handlers are passed the target
local power states for each affected power domain using the generic
representation described above. The platform executes operations specific to
these target states.

The platform power management handler for placing a power domain in a standby
state (plat_pm_ops_t.pwr_domain_standby()) is now only used as a fast path for
placing a core power domain into a standby or retention state should now be
used to only place the core power domain in a standby or retention state.

The extended state-id power state format can be enabled by setting the
build flag PSCI_EXTENDED_STATE_ID=1 and it is disabled by default.

Change-Id: I9d4123d97e179529802c1f589baaa4101759d80c
parent 82dcc039
...@@ -66,6 +66,9 @@ ARM_CCI_PRODUCT_ID := 400 ...@@ -66,6 +66,9 @@ ARM_CCI_PRODUCT_ID := 400
ASM_ASSERTION := ${DEBUG} ASM_ASSERTION := ${DEBUG}
# Build option to choose whether Trusted firmware uses Coherent memory or not. # Build option to choose whether Trusted firmware uses Coherent memory or not.
USE_COHERENT_MEM := 1 USE_COHERENT_MEM := 1
# Flag used to choose the power state format viz Extended State-ID or the Original
# format.
PSCI_EXTENDED_STATE_ID := 0
# Default FIP file name # Default FIP file name
FIP_NAME := fip.bin FIP_NAME := fip.bin
# By default, use the -pedantic option in the gcc command line # By default, use the -pedantic option in the gcc command line
...@@ -268,6 +271,10 @@ $(eval $(call add_define,LOG_LEVEL)) ...@@ -268,6 +271,10 @@ $(eval $(call add_define,LOG_LEVEL))
$(eval $(call assert_boolean,USE_COHERENT_MEM)) $(eval $(call assert_boolean,USE_COHERENT_MEM))
$(eval $(call add_define,USE_COHERENT_MEM)) $(eval $(call add_define,USE_COHERENT_MEM))
# Process PSCI_EXTENDED_STATE_ID flag
$(eval $(call assert_boolean,PSCI_EXTENDED_STATE_ID))
$(eval $(call add_define,PSCI_EXTENDED_STATE_ID))
# Process Generate CoT flags # Process Generate CoT flags
$(eval $(call assert_boolean,GENERATE_COT)) $(eval $(call assert_boolean,GENERATE_COT))
$(eval $(call assert_boolean,CREATE_KEYS)) $(eval $(call assert_boolean,CREATE_KEYS))
......
...@@ -353,6 +353,15 @@ performed. ...@@ -353,6 +353,15 @@ performed.
vector address can be programmed or is fixed on the platform. It can take vector address can be programmed or is fixed on the platform. It can take
either 0 (fixed) or 1 (programmable). Default is 0. either 0 (fixed) or 1 (programmable). Default is 0.
* `PSCI_EXTENDED_STATE_ID`: As per PSCI1.0 Specification, there are 2 formats
possible for the PSCI power-state parameter viz original and extended
State-ID formats. This flag if set to 1, configures the generic PSCI layer
to use the extended format. The default value of this flag is 0, which
means by default the original power-state format is used by the PSCI
implementation. This flag should be specified by the platform makefile
and it governs the return value of PSCI_FEATURES API for CPU_SUSPEND
smc function id.
#### ARM development platform specific build options #### ARM development platform specific build options
* `ARM_TSP_RAM_LOCATION`: location of the TSP binary. Options: * `ARM_TSP_RAM_LOCATION`: location of the TSP binary. Options:
......
...@@ -129,6 +129,9 @@ void init_cpu_ops(void); ...@@ -129,6 +129,9 @@ void init_cpu_ops(void);
#define flush_cpu_data(_m) flush_dcache_range((uint64_t) \ #define flush_cpu_data(_m) flush_dcache_range((uint64_t) \
&(_cpu_data()->_m), \ &(_cpu_data()->_m), \
sizeof(_cpu_data()->_m)) sizeof(_cpu_data()->_m))
#define inv_cpu_data(_m) inv_dcache_range((uint64_t) \
&(_cpu_data()->_m), \
sizeof(_cpu_data()->_m))
#define flush_cpu_data_by_index(_ix, _m) \ #define flush_cpu_data_by_index(_ix, _m) \
flush_dcache_range((uint64_t) \ flush_dcache_range((uint64_t) \
&(_cpu_data_by_index(_ix)->_m), \ &(_cpu_data_by_index(_ix)->_m), \
......
...@@ -97,27 +97,35 @@ ...@@ -97,27 +97,35 @@
* PSCI CPU_SUSPEND 'power_state' parameter specific defines * PSCI CPU_SUSPEND 'power_state' parameter specific defines
******************************************************************************/ ******************************************************************************/
#define PSTATE_ID_SHIFT 0 #define PSTATE_ID_SHIFT 0
#if PSCI_EXTENDED_STATE_ID
#define PSTATE_VALID_MASK 0xB0000000
#define PSTATE_TYPE_SHIFT 30
#define PSTATE_ID_MASK 0xfffffff
#else
#define PSTATE_VALID_MASK 0xFCFE0000
#define PSTATE_TYPE_SHIFT 16 #define PSTATE_TYPE_SHIFT 16
#define PSTATE_PWR_LVL_SHIFT 24 #define PSTATE_PWR_LVL_SHIFT 24
#define PSTATE_ID_MASK 0xffff #define PSTATE_ID_MASK 0xffff
#define PSTATE_TYPE_MASK 0x1
#define PSTATE_PWR_LVL_MASK 0x3 #define PSTATE_PWR_LVL_MASK 0x3
#define PSTATE_VALID_MASK 0xFCFE0000
#define psci_get_pstate_pwrlvl(pstate) (((pstate) >> PSTATE_PWR_LVL_SHIFT) & \
PSTATE_PWR_LVL_MASK)
#define psci_make_powerstate(state_id, type, pwrlvl) \
(((state_id) & PSTATE_ID_MASK) << PSTATE_ID_SHIFT) |\
(((type) & PSTATE_TYPE_MASK) << PSTATE_TYPE_SHIFT) |\
(((pwrlvl) & PSTATE_PWR_LVL_MASK) << PSTATE_PWR_LVL_SHIFT)
#endif /* __PSCI_EXTENDED_STATE_ID__ */
#define PSTATE_TYPE_STANDBY 0x0 #define PSTATE_TYPE_STANDBY 0x0
#define PSTATE_TYPE_POWERDOWN 0x1 #define PSTATE_TYPE_POWERDOWN 0x1
#define PSTATE_TYPE_MASK 0x1
#define psci_get_pstate_id(pstate) (((pstate) >> PSTATE_ID_SHIFT) & \ #define psci_get_pstate_id(pstate) (((pstate) >> PSTATE_ID_SHIFT) & \
PSTATE_ID_MASK) PSTATE_ID_MASK)
#define psci_get_pstate_type(pstate) (((pstate) >> PSTATE_TYPE_SHIFT) & \ #define psci_get_pstate_type(pstate) (((pstate) >> PSTATE_TYPE_SHIFT) & \
PSTATE_TYPE_MASK) PSTATE_TYPE_MASK)
#define psci_get_pstate_pwrlvl(pstate) ((pstate >> PSTATE_PWR_LVL_SHIFT) & \ #define psci_check_power_state(pstate) ((pstate) & PSTATE_VALID_MASK)
PSTATE_PWR_LVL_MASK)
#define psci_make_powerstate(state_id, type, pwrlvl) \
(((state_id) & PSTATE_ID_MASK) << PSTATE_ID_SHIFT) |\
(((type) & PSTATE_TYPE_MASK) << PSTATE_TYPE_SHIFT) |\
(((pwrlvl) & PSTATE_PWR_LVL_MASK) << PSTATE_PWR_LVL_SHIFT)
/******************************************************************************* /*******************************************************************************
* PSCI CPU_FEATURES feature flag specific defines * PSCI CPU_FEATURES feature flag specific defines
...@@ -126,6 +134,11 @@ ...@@ -126,6 +134,11 @@
#define FF_PSTATE_SHIFT 1 #define FF_PSTATE_SHIFT 1
#define FF_PSTATE_ORIG 0 #define FF_PSTATE_ORIG 0
#define FF_PSTATE_EXTENDED 1 #define FF_PSTATE_EXTENDED 1
#if PSCI_EXTENDED_STATE_ID
#define FF_PSTATE FF_PSTATE_EXTENDED
#else
#define FF_PSTATE FF_PSTATE_ORIG
#endif
/* Features flags for CPU SUSPEND OS Initiated mode support. Bits [0:0] */ /* Features flags for CPU SUSPEND OS Initiated mode support. Bits [0:0] */
#define FF_MODE_SUPPORT_SHIFT 0 #define FF_MODE_SUPPORT_SHIFT 0
...@@ -152,25 +165,70 @@ ...@@ -152,25 +165,70 @@
#define PSCI_INVALID_MPIDR ~(0ULL) #define PSCI_INVALID_MPIDR ~(0ULL)
/******************************************************************************* #ifndef __ASSEMBLY__
* PSCI power domain state related constants.
******************************************************************************/ #include <stdint.h>
#define PSCI_STATE_ON 0x0 #include <types.h>
#define PSCI_STATE_OFF 0x1
#define PSCI_STATE_ON_PENDING 0x2
#define PSCI_STATE_SUSPEND 0x3
/*
* These are the states reported by the PSCI_AFFINITY_INFO API for the specified
* CPU. The definitions of these states can be found in Section 5.7.1 in the
* PSCI specification (ARM DEN 0022C).
*/
typedef enum aff_info_state {
AFF_STATE_ON = 0,
AFF_STATE_OFF = 1,
AFF_STATE_ON_PENDING = 2
} aff_info_state_t;
/*
* Macro to represent invalid affinity level within PSCI.
*/
#define PSCI_INVALID_DATA -1 #define PSCI_INVALID_DATA -1
#define get_phys_state(x) (x != PSCI_STATE_ON ? \ /*
PSCI_STATE_OFF : PSCI_STATE_ON) * Type for representing the local power state at a particular level.
*/
typedef uint8_t plat_local_state_t;
#define psci_validate_power_state(pstate) (pstate & PSTATE_VALID_MASK) /* The local state macro used to represent RUN state. */
#define PSCI_LOCAL_STATE_RUN 0
/*
* Macro to test whether the plat_local_state is RUN state
*/
#define is_local_state_run(plat_local_state) \
((plat_local_state) == PSCI_LOCAL_STATE_RUN)
#ifndef __ASSEMBLY__ /*
* Macro to test whether the plat_local_state is RETENTION state
*/
#define is_local_state_retn(plat_local_state) \
(((plat_local_state) > PSCI_LOCAL_STATE_RUN) && \
((plat_local_state) <= PLAT_MAX_RET_STATE))
#include <stdint.h> /*
* Macro to test whether the plat_local_state is OFF state
*/
#define is_local_state_off(plat_local_state) \
(((plat_local_state) > PLAT_MAX_RET_STATE) && \
((plat_local_state) <= PLAT_MAX_OFF_STATE))
/*****************************************************************************
* This data structure defines the representation of the power state parameter
* for its exchange between the generic PSCI code and the platform port. For
* example, it is used by the platform port to specify the requested power
* states during a power management operation. It is used by the generic code
* to inform the platform about the target power states that each level
* should enter.
****************************************************************************/
typedef struct psci_power_state {
/*
* The pwr_domain_state[] stores the local power state at each level
* for the CPU.
*/
plat_local_state_t pwr_domain_state[PLAT_MAX_PWR_LVL + 1];
} psci_power_state_t;
/******************************************************************************* /*******************************************************************************
* Structure used to store per-cpu information relevant to the PSCI service. * Structure used to store per-cpu information relevant to the PSCI service.
...@@ -178,8 +236,15 @@ ...@@ -178,8 +236,15 @@
* this information will not reside on a cache line shared with another cpu. * this information will not reside on a cache line shared with another cpu.
******************************************************************************/ ******************************************************************************/
typedef struct psci_cpu_data { typedef struct psci_cpu_data {
uint32_t power_state; /* The power state from CPU_SUSPEND */ /* State as seen by PSCI Affinity Info API */
unsigned char psci_state; /* The state of this CPU as seen by PSCI */ aff_info_state_t aff_info_state;
/*
* Highest power level which takes part in a power management
* operation.
*/
int8_t target_pwrlvl;
/* The local power state of this CPU */
plat_local_state_t local_state;
#if !USE_COHERENT_MEM #if !USE_COHERENT_MEM
bakery_info_t pcpu_bakery_info[PSCI_NUM_NON_CPU_PWR_DOMAINS]; bakery_info_t pcpu_bakery_info[PSCI_NUM_NON_CPU_PWR_DOMAINS];
#endif #endif
...@@ -189,22 +254,23 @@ typedef struct psci_cpu_data { ...@@ -189,22 +254,23 @@ typedef struct psci_cpu_data {
* Structure populated by platform specific code to export routines which * Structure populated by platform specific code to export routines which
* perform common low level pm functions * perform common low level pm functions
******************************************************************************/ ******************************************************************************/
typedef struct plat_pm_ops { typedef struct plat_psci_ops {
void (*pwr_domain_standby)(unsigned int power_state); void (*cpu_standby)(plat_local_state_t cpu_state);
int (*pwr_domain_on)(unsigned long mpidr, int (*pwr_domain_on)(u_register_t mpidr,
unsigned long sec_entrypoint, unsigned long sec_entrypoint);
unsigned int pwrlvl); void (*pwr_domain_off)(const psci_power_state_t *target_state);
void (*pwr_domain_off)(unsigned int pwrlvl);
void (*pwr_domain_suspend)(unsigned long sec_entrypoint, void (*pwr_domain_suspend)(unsigned long sec_entrypoint,
unsigned int pwrlvl); const psci_power_state_t *target_state);
void (*pwr_domain_on_finish)(unsigned int pwrlvl); void (*pwr_domain_on_finish)(const psci_power_state_t *target_state);
void (*pwr_domain_suspend_finish)(unsigned int pwrlvl); void (*pwr_domain_suspend_finish)(const psci_power_state_t *target_state);
void (*system_off)(void) __dead2; void (*system_off)(void) __dead2;
void (*system_reset)(void) __dead2; void (*system_reset)(void) __dead2;
int (*validate_power_state)(unsigned int power_state); int (*validate_power_state)(unsigned int power_state,
psci_power_state_t *req_state);
int (*validate_ns_entrypoint)(unsigned long ns_entrypoint); int (*validate_ns_entrypoint)(unsigned long ns_entrypoint);
unsigned int (*get_sys_suspend_power_state)(void); void (*get_sys_suspend_power_state)(
} plat_pm_ops_t; psci_power_state_t *req_state);
} plat_psci_ops_t;
/******************************************************************************* /*******************************************************************************
* Optional structure populated by the Secure Payload Dispatcher to be given a * Optional structure populated by the Secure Payload Dispatcher to be given a
...@@ -239,9 +305,6 @@ void __dead2 psci_power_down_wfi(void); ...@@ -239,9 +305,6 @@ void __dead2 psci_power_down_wfi(void);
void psci_cpu_on_finish_entry(void); void psci_cpu_on_finish_entry(void);
void psci_cpu_suspend_finish_entry(void); void psci_cpu_suspend_finish_entry(void);
void psci_register_spd_pm_hook(const spd_pm_ops_t *); void psci_register_spd_pm_hook(const spd_pm_ops_t *);
int psci_get_suspend_stateid_by_idx(unsigned long);
int psci_get_suspend_stateid(void);
int psci_get_suspend_pwrlvl(void);
uint64_t psci_smc_handler(uint32_t smc_fid, uint64_t psci_smc_handler(uint32_t smc_fid,
uint64_t x1, uint64_t x1,
......
...@@ -32,12 +32,11 @@ ...@@ -32,12 +32,11 @@
#define __PLATFORM_H__ #define __PLATFORM_H__
#include <stdint.h> #include <stdint.h>
#include <psci.h>
/******************************************************************************* /*******************************************************************************
* Forward declarations * Forward declarations
******************************************************************************/ ******************************************************************************/
struct plat_pm_ops;
struct meminfo; struct meminfo;
struct image_info; struct image_info;
struct entry_point_info; struct entry_point_info;
...@@ -182,9 +181,16 @@ struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type); ...@@ -182,9 +181,16 @@ struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type);
/******************************************************************************* /*******************************************************************************
* Mandatory PSCI functions (BL3-1) * Mandatory PSCI functions (BL3-1)
******************************************************************************/ ******************************************************************************/
int platform_setup_pm(const struct plat_pm_ops **); int plat_setup_psci_ops(const struct plat_psci_ops **);
const unsigned char *plat_get_power_domain_tree_desc(void); const unsigned char *plat_get_power_domain_tree_desc(void);
/*******************************************************************************
* Optional PSCI functions (BL3-1).
******************************************************************************/
plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
const plat_local_state_t *states,
unsigned int ncpu);
/******************************************************************************* /*******************************************************************************
* Optional BL3-1 functions (may be overridden) * Optional BL3-1 functions (may be overridden)
******************************************************************************/ ******************************************************************************/
......
/*
* Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of ARM nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <arch.h>
#include <assert.h>
#include <platform.h>
#include <psci.h>
/*
* The PSCI generic code uses this API to let the platform participate in state
* coordination during a power management operation. It compares the platform
* specific local power states requested by each cpu for a given power domain
* and returns the coordinated target power state that the domain should
* enter. A platform assigns a number to a local power state. This default
* implementation assumes that the platform assigns these numbers in order of
* increasing depth of the power state i.e. for two power states X & Y, if X < Y
* then X represents a shallower power state than Y. As a result, the
* coordinated target local power state for a power domain will be the minimum
* of the requested local power states.
*/
plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
const plat_local_state_t *states,
unsigned int ncpu)
{
plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
assert(ncpu);
do {
temp = *states++;
if (temp < target)
target = temp;
} while (--ncpu);
return target;
}
This diff is collapsed.
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <platform.h> #include <platform.h>
#include <runtime_svc.h> #include <runtime_svc.h>
#include <std_svc.h> #include <std_svc.h>
#include <string.h>
#include "psci_private.h" #include "psci_private.h"
/******************************************************************************* /*******************************************************************************
...@@ -93,51 +94,61 @@ int psci_cpu_suspend(unsigned int power_state, ...@@ -93,51 +94,61 @@ int psci_cpu_suspend(unsigned int power_state,
unsigned long context_id) unsigned long context_id)
{ {
int rc; int rc;
unsigned int target_pwrlvl, pstate_type; unsigned int target_pwrlvl, is_power_down_state;
entry_point_info_t ep; entry_point_info_t ep;
psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
plat_local_state_t cpu_pd_state;
/* Check SBZ bits in power state are zero */ /* Validate the power_state parameter */
if (psci_validate_power_state(power_state)) rc = psci_validate_power_state(power_state, &state_info);
return PSCI_E_INVALID_PARAMS;
/* Sanity check the requested state */
target_pwrlvl = psci_get_pstate_pwrlvl(power_state);
if (target_pwrlvl > PLAT_MAX_PWR_LVL)
return PSCI_E_INVALID_PARAMS;
/* Validate the power_state using platform pm_ops */
if (psci_plat_pm_ops->validate_power_state) {
rc = psci_plat_pm_ops->validate_power_state(power_state);
if (rc != PSCI_E_SUCCESS) { if (rc != PSCI_E_SUCCESS) {
assert(rc == PSCI_E_INVALID_PARAMS); assert(rc == PSCI_E_INVALID_PARAMS);
return PSCI_E_INVALID_PARAMS; return rc;
}
} }
/* Validate the entrypoint using platform pm_ops */ /*
if (psci_plat_pm_ops->validate_ns_entrypoint) { * Get the value of the state type bit from the power state parameter.
rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint); */
if (rc != PSCI_E_SUCCESS) { is_power_down_state = psci_get_pstate_type(power_state);
assert(rc == PSCI_E_INVALID_PARAMS);
return PSCI_E_INVALID_PARAMS; /* Sanity check the requested suspend levels */
} assert (psci_validate_suspend_req(&state_info, is_power_down_state)
} == PSCI_E_SUCCESS);
/* Determine the 'state type' in the 'power_state' parameter */ target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
pstate_type = psci_get_pstate_type(power_state);
/* Fast path for CPU standby.*/
if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
if (!psci_plat_pm_ops->cpu_standby)
return PSCI_E_INVALID_PARAMS;
/* /*
* Ensure that we have a platform specific handler for entering * Set the state of the CPU power domain to the platform
* a standby state. * specific retention state and enter the standby state.
*/ */
if (pstate_type == PSTATE_TYPE_STANDBY) { cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
if (!psci_plat_pm_ops->pwr_domain_standby) psci_set_cpu_local_state(cpu_pd_state);
return PSCI_E_INVALID_PARAMS; psci_plat_pm_ops->cpu_standby(cpu_pd_state);
/* Upon exit from standby, set the state back to RUN. */
psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
psci_plat_pm_ops->pwr_domain_standby(power_state);
return PSCI_E_SUCCESS; return PSCI_E_SUCCESS;
} }
/*
* If a power down state has been requested, we need to verify entry
* point and program entry information.
*/
if (is_power_down_state) {
if (psci_plat_pm_ops->validate_ns_entrypoint) {
rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
if (rc != PSCI_E_SUCCESS) {
assert(rc == PSCI_E_INVALID_PARAMS);
return rc;
}
}
/* /*
* Verify and derive the re-entry information for * Verify and derive the re-entry information for
* the non-secure world from the non-secure state from * the non-secure world from the non-secure state from
...@@ -146,19 +157,19 @@ int psci_cpu_suspend(unsigned int power_state, ...@@ -146,19 +157,19 @@ int psci_cpu_suspend(unsigned int power_state,
rc = psci_get_ns_ep_info(&ep, entrypoint, context_id); rc = psci_get_ns_ep_info(&ep, entrypoint, context_id);
if (rc != PSCI_E_SUCCESS) if (rc != PSCI_E_SUCCESS)
return rc; return rc;
}
/* Save PSCI power state parameter for the core in suspend context */
psci_set_suspend_power_state(power_state);
/* /*
* Do what is needed to enter the power down state. Upon success, * Do what is needed to enter the power down state. Upon success,
* enter the final wfi which will power down this CPU. * enter the final wfi which will power down this CPU. This function
* might return if the power down was abandoned for any reason, e.g.
* arrival of an interrupt
*/ */
psci_cpu_suspend_start(&ep, psci_cpu_suspend_start(&ep,
target_pwrlvl); target_pwrlvl,
&state_info,
is_power_down_state);
/* Reset PSCI power state parameter for the core. */
psci_set_suspend_power_state(PSCI_INVALID_DATA);
return PSCI_E_SUCCESS; return PSCI_E_SUCCESS;
} }
...@@ -166,7 +177,7 @@ int psci_system_suspend(unsigned long entrypoint, ...@@ -166,7 +177,7 @@ int psci_system_suspend(unsigned long entrypoint,
unsigned long context_id) unsigned long context_id)
{ {
int rc; int rc;
unsigned int power_state; psci_power_state_t state_info;
entry_point_info_t ep; entry_point_info_t ep;
/* Validate the entrypoint using platform pm_ops */ /* Validate the entrypoint using platform pm_ops */
...@@ -174,7 +185,7 @@ int psci_system_suspend(unsigned long entrypoint, ...@@ -174,7 +185,7 @@ int psci_system_suspend(unsigned long entrypoint,
rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint); rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
if (rc != PSCI_E_SUCCESS) { if (rc != PSCI_E_SUCCESS) {
assert(rc == PSCI_E_INVALID_PARAMS); assert(rc == PSCI_E_INVALID_PARAMS);
return PSCI_E_INVALID_PARAMS; return rc;
} }
} }
...@@ -191,28 +202,25 @@ int psci_system_suspend(unsigned long entrypoint, ...@@ -191,28 +202,25 @@ int psci_system_suspend(unsigned long entrypoint,
if (rc != PSCI_E_SUCCESS) if (rc != PSCI_E_SUCCESS)
return rc; return rc;
/* /* Query the psci_power_state for system suspend */
* Assert that the required pm_ops hook is implemented to ensure that psci_query_sys_suspend_pwrstate(&state_info);
* the capability detected during psci_setup() is valid.
*/
assert(psci_plat_pm_ops->get_sys_suspend_power_state);
/*
* Query the platform for the power_state required for system suspend
*/
power_state = psci_plat_pm_ops->get_sys_suspend_power_state();
/* Save PSCI power state parameter for the core in suspend context */ /* Ensure that the psci_power_state makes sense */
psci_set_suspend_power_state(power_state); assert(psci_find_target_suspend_lvl(&state_info) == PLAT_MAX_PWR_LVL);
assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
== PSCI_E_SUCCESS);
assert(is_local_state_off(state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]));
/* /*
* Do what is needed to enter the power down state. Upon success, * Do what is needed to enter the system suspend state. This function
* enter the final wfi which will power down this cpu. * might return if the power down was abandoned for any reason, e.g.
* arrival of an interrupt
*/ */
psci_cpu_suspend_start(&ep, PLAT_MAX_PWR_LVL); psci_cpu_suspend_start(&ep,
PLAT_MAX_PWR_LVL,
&state_info,
PSTATE_TYPE_POWERDOWN);
/* Reset PSCI power state parameter for the core. */
psci_set_suspend_power_state(PSCI_INVALID_DATA);
return PSCI_E_SUCCESS; return PSCI_E_SUCCESS;
} }
...@@ -240,26 +248,18 @@ int psci_cpu_off(void) ...@@ -240,26 +248,18 @@ int psci_cpu_off(void)
int psci_affinity_info(unsigned long target_affinity, int psci_affinity_info(unsigned long target_affinity,
unsigned int lowest_affinity_level) unsigned int lowest_affinity_level)
{ {
unsigned int cpu_idx; unsigned int target_idx;
unsigned char cpu_pwr_domain_state;
/* We dont support level higher than PSCI_CPU_PWR_LVL */ /* We dont support level higher than PSCI_CPU_PWR_LVL */
if (lowest_affinity_level > PSCI_CPU_PWR_LVL) if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
return PSCI_E_INVALID_PARAMS; return PSCI_E_INVALID_PARAMS;
/* Calculate the cpu index of the target */ /* Calculate the cpu index of the target */
cpu_idx = plat_core_pos_by_mpidr(target_affinity); target_idx = plat_core_pos_by_mpidr(target_affinity);
if (cpu_idx == -1) if (target_idx == -1)
return PSCI_E_INVALID_PARAMS; return PSCI_E_INVALID_PARAMS;
cpu_pwr_domain_state = psci_get_state(cpu_idx, PSCI_CPU_PWR_LVL); return psci_get_aff_info_state_by_idx(target_idx);
/* A suspended cpu is available & on for the OS */
if (cpu_pwr_domain_state == PSCI_STATE_SUSPEND) {
cpu_pwr_domain_state = PSCI_STATE_ON;
}
return cpu_pwr_domain_state;
} }
int psci_migrate(unsigned long target_cpu) int psci_migrate(unsigned long target_cpu)
...@@ -337,10 +337,9 @@ int psci_features(unsigned int psci_fid) ...@@ -337,10 +337,9 @@ int psci_features(unsigned int psci_fid)
if (psci_fid == PSCI_CPU_SUSPEND_AARCH32 || if (psci_fid == PSCI_CPU_SUSPEND_AARCH32 ||
psci_fid == PSCI_CPU_SUSPEND_AARCH64) { psci_fid == PSCI_CPU_SUSPEND_AARCH64) {
/* /*
* The trusted firmware uses the original power state format * The trusted firmware does not support OS Initiated Mode.
* and does not support OS Initiated Mode.
*/ */
return (FF_PSTATE_ORIG << FF_PSTATE_SHIFT) | return (FF_PSTATE << FF_PSTATE_SHIFT) |
((!FF_SUPPORTS_OS_INIT_MODE) << FF_MODE_SUPPORT_SHIFT); ((!FF_SUPPORTS_OS_INIT_MODE) << FF_MODE_SUPPORT_SHIFT);
} }
......
...@@ -36,6 +36,17 @@ ...@@ -36,6 +36,17 @@
#include <string.h> #include <string.h>
#include "psci_private.h" #include "psci_private.h"
/******************************************************************************
* Construct the psci_power_state to request power OFF at all power levels.
******************************************************************************/
static void psci_set_power_off_state(psci_power_state_t *state_info)
{
int lvl;
for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++)
state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE;
}
/****************************************************************************** /******************************************************************************
* Top level handler which is called when a cpu wants to power itself down. * Top level handler which is called when a cpu wants to power itself down.
* It's assumed that along with turning the cpu power domain off, power * It's assumed that along with turning the cpu power domain off, power
...@@ -52,7 +63,7 @@ ...@@ -52,7 +63,7 @@
int psci_do_cpu_off(int end_pwrlvl) int psci_do_cpu_off(int end_pwrlvl)
{ {
int rc, idx = plat_my_core_pos(); int rc, idx = plat_my_core_pos();
unsigned int max_phys_off_pwrlvl; psci_power_state_t state_info;
/* /*
* This function must only be called on platforms where the * This function must only be called on platforms where the
...@@ -79,29 +90,27 @@ int psci_do_cpu_off(int end_pwrlvl) ...@@ -79,29 +90,27 @@ int psci_do_cpu_off(int end_pwrlvl)
goto exit; goto exit;
} }
/* Construct the psci_power_state for CPU_OFF */
psci_set_power_off_state(&state_info);
/* /*
* This function updates the state of each power domain instance * This function is passed the requested state info and
* corresponding to the cpu index in the range of power levels * it returns the negotiated state info for each power level upto
* specified. * the end level specified.
*/ */
psci_do_state_coordination(end_pwrlvl, psci_do_state_coordination(end_pwrlvl, &state_info);
idx,
PSCI_STATE_OFF);
max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(end_pwrlvl, idx);
assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA);
/* /*
* Arch. management. Perform the necessary steps to flush all * Arch. management. Perform the necessary steps to flush all
* cpu caches. * cpu caches.
*/ */
psci_do_pwrdown_cache_maintenance(max_phys_off_pwrlvl); psci_do_pwrdown_cache_maintenance(psci_find_max_off_lvl(&state_info));
/* /*
* Plat. management: Perform platform specific actions to turn this * Plat. management: Perform platform specific actions to turn this
* cpu off e.g. exit cpu coherency, program the power controller etc. * cpu off e.g. exit cpu coherency, program the power controller etc.
*/ */
psci_plat_pm_ops->pwr_domain_off(max_phys_off_pwrlvl); psci_plat_pm_ops->pwr_domain_off(&state_info);
exit: exit:
/* /*
...@@ -111,6 +120,16 @@ exit: ...@@ -111,6 +120,16 @@ exit:
psci_release_pwr_domain_locks(end_pwrlvl, psci_release_pwr_domain_locks(end_pwrlvl,
idx); idx);
/*
* Set the affinity info state to OFF. This writes directly to main
* memory as caches are disabled, so cache maintenance is required
* to ensure that later cached reads of aff_info_state return
* AFF_STATE_OFF.
*/
flush_cpu_data(psci_svc_cpu_data.aff_info_state);
psci_set_aff_info_state(AFF_STATE_OFF);
inv_cpu_data(psci_svc_cpu_data.aff_info_state);
/* /*
* Check if all actions needed to safely power down this cpu have * Check if all actions needed to safely power down this cpu have
* successfully completed. Enter a wfi loop which will allow the * successfully completed. Enter a wfi loop which will allow the
......
...@@ -44,18 +44,36 @@ ...@@ -44,18 +44,36 @@
* This function checks whether a cpu which has been requested to be turned on * This function checks whether a cpu which has been requested to be turned on
* is OFF to begin with. * is OFF to begin with.
******************************************************************************/ ******************************************************************************/
static int cpu_on_validate_state(unsigned int psci_state) static int cpu_on_validate_state(aff_info_state_t aff_state)
{ {
if (psci_state == PSCI_STATE_ON || psci_state == PSCI_STATE_SUSPEND) if (aff_state == AFF_STATE_ON)
return PSCI_E_ALREADY_ON; return PSCI_E_ALREADY_ON;
if (psci_state == PSCI_STATE_ON_PENDING) if (aff_state == AFF_STATE_ON_PENDING)
return PSCI_E_ON_PENDING; return PSCI_E_ON_PENDING;
assert(psci_state == PSCI_STATE_OFF); assert(aff_state == AFF_STATE_OFF);
return PSCI_E_SUCCESS; return PSCI_E_SUCCESS;
} }
/*******************************************************************************
* This function sets the aff_info_state in the per-cpu data of the CPU
* specified by cpu_idx
******************************************************************************/
static void psci_set_aff_info_state_by_idx(unsigned int cpu_idx,
aff_info_state_t aff_state)
{
set_cpu_data_by_index(cpu_idx,
psci_svc_cpu_data.aff_info_state,
aff_state);
/*
* Flush aff_info_state as it will be accessed with caches turned OFF.
*/
flush_cpu_data_by_index(cpu_idx, psci_svc_cpu_data.aff_info_state);
}
/******************************************************************************* /*******************************************************************************
* Generic handler which is called to physically power on a cpu identified by * Generic handler which is called to physically power on a cpu identified by
* its mpidr. It performs the generic, architectural, platform setup and state * its mpidr. It performs the generic, architectural, platform setup and state
...@@ -88,7 +106,7 @@ int psci_cpu_on_start(unsigned long target_cpu, ...@@ -88,7 +106,7 @@ int psci_cpu_on_start(unsigned long target_cpu,
* Generic management: Ensure that the cpu is off to be * Generic management: Ensure that the cpu is off to be
* turned on. * turned on.
*/ */
rc = cpu_on_validate_state(psci_get_state(target_idx, PSCI_CPU_PWR_LVL)); rc = cpu_on_validate_state(psci_get_aff_info_state_by_idx(target_idx));
if (rc != PSCI_E_SUCCESS) if (rc != PSCI_E_SUCCESS)
goto exit; goto exit;
...@@ -101,13 +119,9 @@ int psci_cpu_on_start(unsigned long target_cpu, ...@@ -101,13 +119,9 @@ int psci_cpu_on_start(unsigned long target_cpu,
psci_spd_pm->svc_on(target_cpu); psci_spd_pm->svc_on(target_cpu);
/* /*
* This function updates the state of each affinity instance * Set the Affinity info state of the target cpu to ON_PENDING.
* corresponding to the mpidr in the range of power domain levels
* specified.
*/ */
psci_do_state_coordination(end_pwrlvl, psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING);
target_idx,
PSCI_STATE_ON_PENDING);
/* /*
* Perform generic, architecture and platform specific handling. * Perform generic, architecture and platform specific handling.
...@@ -120,9 +134,8 @@ int psci_cpu_on_start(unsigned long target_cpu, ...@@ -120,9 +134,8 @@ int psci_cpu_on_start(unsigned long target_cpu,
* of the target cpu to allow it to perform the necessary * of the target cpu to allow it to perform the necessary
* steps to power on. * steps to power on.
*/ */
rc = psci_plat_pm_ops->pwr_domain_on(target_cpu, rc = psci_plat_pm_ops->pwr_domain_on((u_register_t)target_cpu,
psci_entrypoint, psci_entrypoint);
MPIDR_AFFLVL0);
assert(rc == PSCI_E_SUCCESS || rc == PSCI_E_INTERN_FAIL); assert(rc == PSCI_E_SUCCESS || rc == PSCI_E_INTERN_FAIL);
if (rc == PSCI_E_SUCCESS) if (rc == PSCI_E_SUCCESS)
...@@ -130,9 +143,7 @@ int psci_cpu_on_start(unsigned long target_cpu, ...@@ -130,9 +143,7 @@ int psci_cpu_on_start(unsigned long target_cpu,
cm_init_context_by_index(target_idx, ep); cm_init_context_by_index(target_idx, ep);
else else
/* Restore the state on error. */ /* Restore the state on error. */
psci_do_state_coordination(end_pwrlvl, psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_OFF);
target_idx,
PSCI_STATE_OFF);
exit: exit:
psci_spin_unlock_cpu(target_idx); psci_spin_unlock_cpu(target_idx);
...@@ -141,22 +152,19 @@ exit: ...@@ -141,22 +152,19 @@ exit:
/******************************************************************************* /*******************************************************************************
* The following function finish an earlier power on request. They * The following function finish an earlier power on request. They
* are called by the common finisher routine in psci_common.c. * are called by the common finisher routine in psci_common.c. The `state_info`
* is the psci_power_state from which this CPU has woken up from.
******************************************************************************/ ******************************************************************************/
void psci_cpu_on_finish(unsigned int cpu_idx, void psci_cpu_on_finish(unsigned int cpu_idx,
int max_off_pwrlvl) psci_power_state_t *state_info)
{ {
/* Ensure we have been explicitly woken up by another cpu */
assert(psci_get_state(cpu_idx, PSCI_CPU_PWR_LVL)
== PSCI_STATE_ON_PENDING);
/* /*
* Plat. management: Perform the platform specific actions * Plat. management: Perform the platform specific actions
* for this cpu e.g. enabling the gic or zeroing the mailbox * for this cpu e.g. enabling the gic or zeroing the mailbox
* register. The actual state of this cpu has already been * register. The actual state of this cpu has already been
* changed. * changed.
*/ */
psci_plat_pm_ops->pwr_domain_on_finish(max_off_pwrlvl); psci_plat_pm_ops->pwr_domain_on_finish(state_info);
/* /*
* Arch. management: Enable data cache and manage stack memory * Arch. management: Enable data cache and manage stack memory
...@@ -179,6 +187,9 @@ void psci_cpu_on_finish(unsigned int cpu_idx, ...@@ -179,6 +187,9 @@ void psci_cpu_on_finish(unsigned int cpu_idx,
psci_spin_lock_cpu(cpu_idx); psci_spin_lock_cpu(cpu_idx);
psci_spin_unlock_cpu(cpu_idx); psci_spin_unlock_cpu(cpu_idx);
/* Ensure we have been explicitly woken up by another cpu */
assert(psci_get_aff_info_state() == AFF_STATE_ON_PENDING);
/* /*
* Call the cpu on finish handler registered by the Secure Payload * Call the cpu on finish handler registered by the Secure Payload
* Dispatcher to let it do any bookeeping. If the handler encounters an * Dispatcher to let it do any bookeeping. If the handler encounters an
......
...@@ -80,12 +80,36 @@ ...@@ -80,12 +80,36 @@
define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64) | \ define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64) | \
define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64)) define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64))
/*
* Helper macros to get/set the fields of PSCI per-cpu data.
*/
#define psci_set_aff_info_state(aff_state) \
set_cpu_data(psci_svc_cpu_data.aff_info_state, aff_state)
#define psci_get_aff_info_state() \
get_cpu_data(psci_svc_cpu_data.aff_info_state)
#define psci_get_aff_info_state_by_idx(idx) \
get_cpu_data_by_index(idx, psci_svc_cpu_data.aff_info_state)
#define psci_get_suspend_pwrlvl() \
get_cpu_data(psci_svc_cpu_data.target_pwrlvl)
#define psci_set_suspend_pwrlvl(target_lvl) \
set_cpu_data(psci_svc_cpu_data.target_pwrlvl, target_lvl)
#define psci_set_cpu_local_state(state) \
set_cpu_data(psci_svc_cpu_data.local_state, state)
#define psci_get_cpu_local_state() \
get_cpu_data(psci_svc_cpu_data.local_state)
#define psci_get_cpu_local_state_by_idx(idx) \
get_cpu_data_by_index(idx, psci_svc_cpu_data.local_state)
/* /*
* Helper macros for the CPU level spinlocks * Helper macros for the CPU level spinlocks
*/ */
#define psci_spin_lock_cpu(idx) spin_lock(&psci_cpu_pd_nodes[idx].cpu_lock) #define psci_spin_lock_cpu(idx) spin_lock(&psci_cpu_pd_nodes[idx].cpu_lock)
#define psci_spin_unlock_cpu(idx) spin_unlock(&psci_cpu_pd_nodes[idx].cpu_lock) #define psci_spin_unlock_cpu(idx) spin_unlock(&psci_cpu_pd_nodes[idx].cpu_lock)
/* Helper macro to identify a CPU standby request in PSCI Suspend call */
#define is_cpu_standby_req(is_power_down_state, retn_lvl) \
(((!(is_power_down_state)) && ((retn_lvl) == 0)) ? 1 : 0)
/******************************************************************************* /*******************************************************************************
* The following two data structures implement the power domain tree. The tree * The following two data structures implement the power domain tree. The tree
* is used to track the state of all the nodes i.e. power domain instances * is used to track the state of all the nodes i.e. power domain instances
...@@ -113,7 +137,8 @@ typedef struct non_cpu_pwr_domain_node { ...@@ -113,7 +137,8 @@ typedef struct non_cpu_pwr_domain_node {
*/ */
unsigned int parent_node; unsigned int parent_node;
unsigned char ref_count; plat_local_state_t local_state;
unsigned char level; unsigned char level;
#if USE_COHERENT_MEM #if USE_COHERENT_MEM
bakery_lock_t lock; bakery_lock_t lock;
...@@ -142,12 +167,12 @@ typedef struct cpu_pwr_domain_node { ...@@ -142,12 +167,12 @@ typedef struct cpu_pwr_domain_node {
} cpu_pd_node_t; } cpu_pd_node_t;
typedef void (*pwrlvl_power_on_finisher_t)(unsigned int cpu_idx, typedef void (*pwrlvl_power_on_finisher_t)(unsigned int cpu_idx,
int max_off_pwrlvl); psci_power_state_t *state_info);
/******************************************************************************* /*******************************************************************************
* Data prototypes * Data prototypes
******************************************************************************/ ******************************************************************************/
extern const plat_pm_ops_t *psci_plat_pm_ops; extern const plat_psci_ops_t *psci_plat_pm_ops;
extern non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]; extern non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS];
extern cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT]; extern cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
extern uint32_t psci_caps; extern uint32_t psci_caps;
...@@ -161,28 +186,31 @@ extern const spd_pm_ops_t *psci_spd_pm; ...@@ -161,28 +186,31 @@ extern const spd_pm_ops_t *psci_spd_pm;
* Function prototypes * Function prototypes
******************************************************************************/ ******************************************************************************/
/* Private exported functions from psci_common.c */ /* Private exported functions from psci_common.c */
unsigned short psci_get_state(unsigned int idx, int level); int psci_validate_power_state(unsigned int power_state,
unsigned short psci_get_phys_state(unsigned int idx, int level); psci_power_state_t *state_info);
void psci_set_state(unsigned int idx, unsigned short state, int level); void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info);
int psci_validate_mpidr(unsigned long mpidr); int psci_validate_mpidr(unsigned long mpidr);
int get_power_on_target_pwrlvl(void); int get_power_on_target_pwrlvl(void);
void psci_init_req_local_pwr_states(void);
void psci_power_up_finish(int end_pwrlvl, void psci_power_up_finish(int end_pwrlvl,
pwrlvl_power_on_finisher_t pon_handler); pwrlvl_power_on_finisher_t power_on_handler);
int psci_get_ns_ep_info(entry_point_info_t *ep, int psci_get_ns_ep_info(entry_point_info_t *ep,
uint64_t entrypoint, uint64_t context_id); uint64_t entrypoint, uint64_t context_id);
void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx, void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
int end_lvl, int end_lvl,
unsigned int node_index[]); unsigned int node_index[]);
void psci_do_state_coordination(int end_pwrlvl, void psci_do_state_coordination(int end_pwrlvl,
unsigned int cpu_idx, psci_power_state_t *state_info);
uint32_t state);
void psci_acquire_pwr_domain_locks(int end_pwrlvl, void psci_acquire_pwr_domain_locks(int end_pwrlvl,
unsigned int cpu_idx); unsigned int cpu_idx);
void psci_release_pwr_domain_locks(int end_pwrlvl, void psci_release_pwr_domain_locks(int end_pwrlvl,
unsigned int cpu_idx); unsigned int cpu_idx);
int psci_validate_suspend_req(const psci_power_state_t *state_info,
unsigned int is_power_down_state_req);
unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info);
unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info);
void psci_set_pwr_domains_to_run(uint32_t end_pwrlvl);
void psci_print_power_domain_map(void); void psci_print_power_domain_map(void);
uint32_t psci_find_max_phys_off_pwrlvl(uint32_t end_pwrlvl,
unsigned int cpu_idx);
unsigned int psci_is_last_on_cpu(void); unsigned int psci_is_last_on_cpu(void);
int psci_spd_migrate_info(uint64_t *mpidr); int psci_spd_migrate_info(uint64_t *mpidr);
...@@ -192,18 +220,19 @@ int psci_cpu_on_start(unsigned long target_cpu, ...@@ -192,18 +220,19 @@ int psci_cpu_on_start(unsigned long target_cpu,
int end_pwrlvl); int end_pwrlvl);
void psci_cpu_on_finish(unsigned int cpu_idx, void psci_cpu_on_finish(unsigned int cpu_idx,
int max_off_pwrlvl); psci_power_state_t *state_info);
/* Private exported functions from psci_cpu_off.c */ /* Private exported functions from psci_cpu_off.c */
int psci_do_cpu_off(int end_pwrlvl); int psci_do_cpu_off(int end_pwrlvl);
/* Private exported functions from psci_suspend.c */ /* Private exported functions from psci_pwrlvl_suspend.c */
void psci_cpu_suspend_start(entry_point_info_t *ep, void psci_cpu_suspend_start(entry_point_info_t *ep,
int end_pwrlvl); int end_pwrlvl,
void psci_cpu_suspend_finish(unsigned int cpu_idx, psci_power_state_t *state_info,
int max_off_pwrlvl); unsigned int is_power_down_state_req);
void psci_set_suspend_power_state(unsigned int power_state); void psci_cpu_suspend_finish(unsigned int cpu_idx,
psci_power_state_t *state_info);
/* Private exported functions from psci_helpers.S */ /* Private exported functions from psci_helpers.S */
void psci_do_pwrdown_cache_maintenance(uint32_t pwr_level); void psci_do_pwrdown_cache_maintenance(uint32_t pwr_level);
......
...@@ -61,26 +61,30 @@ static void psci_init_pwr_domain_node(int node_idx, int parent_idx, int level) ...@@ -61,26 +61,30 @@ static void psci_init_pwr_domain_node(int node_idx, int parent_idx, int level)
psci_non_cpu_pd_nodes[node_idx].level = level; psci_non_cpu_pd_nodes[node_idx].level = level;
psci_lock_init(psci_non_cpu_pd_nodes, node_idx); psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx; psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
psci_non_cpu_pd_nodes[node_idx].local_state =
PLAT_MAX_OFF_STATE;
} else { } else {
psci_cpu_data_t *svc_cpu_data;
psci_cpu_pd_nodes[node_idx].parent_node = parent_idx; psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
/* Initialize with an invalid mpidr */ /* Initialize with an invalid mpidr */
psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR; psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
/* svc_cpu_data =
* Mark the cpu as OFF. &(_cpu_data_by_index(node_idx)->psci_svc_cpu_data);
*/
set_cpu_data_by_index(node_idx, /* Set the Affinity Info for the cores as OFF */
psci_svc_cpu_data.psci_state, svc_cpu_data->aff_info_state = AFF_STATE_OFF;
PSCI_STATE_OFF);
/* Invalidate the suspend context for the node */ /* Invalidate the suspend level for the cpu */
set_cpu_data_by_index(node_idx, svc_cpu_data->target_pwrlvl = PSCI_INVALID_DATA;
psci_svc_cpu_data.power_state,
PSCI_INVALID_DATA);
flush_cpu_data_by_index(node_idx, psci_svc_cpu_data); /* Set the power state to OFF state */
svc_cpu_data->local_state = PLAT_MAX_OFF_STATE;
flush_dcache_range((uint64_t)svc_cpu_data,
sizeof(*svc_cpu_data));
cm_set_context_by_index(node_idx, cm_set_context_by_index(node_idx,
(void *) &psci_ns_context[node_idx], (void *) &psci_ns_context[node_idx],
...@@ -233,14 +237,15 @@ int32_t psci_setup(void) ...@@ -233,14 +237,15 @@ int32_t psci_setup(void)
flush_dcache_range((uint64_t) &psci_cpu_pd_nodes, flush_dcache_range((uint64_t) &psci_cpu_pd_nodes,
sizeof(psci_cpu_pd_nodes)); sizeof(psci_cpu_pd_nodes));
psci_init_req_local_pwr_states();
/* /*
* Mark the current CPU and its parent power domains as ON. No need to lock * Set the requested and target state of this CPU and all the higher
* as the system is UP on the primary at this stage of boot. * power domain levels for this CPU to run.
*/ */
psci_do_state_coordination(PLAT_MAX_PWR_LVL, plat_my_core_pos(), psci_set_pwr_domains_to_run(PLAT_MAX_PWR_LVL);
PSCI_STATE_ON);
platform_setup_pm(&psci_plat_pm_ops); plat_setup_psci_ops(&psci_plat_pm_ops);
assert(psci_plat_pm_ops); assert(psci_plat_pm_ops);
/* Initialize the psci capability */ /* Initialize the psci capability */
......
...@@ -42,82 +42,97 @@ ...@@ -42,82 +42,97 @@
#include "psci_private.h" #include "psci_private.h"
/******************************************************************************* /*******************************************************************************
* This function saves the power state parameter passed in the current PSCI * This function does generic and platform specific operations after a wake-up
* cpu_suspend call in the per-cpu data array. * from standby/retention states at multiple power levels.
******************************************************************************/ ******************************************************************************/
void psci_set_suspend_power_state(unsigned int power_state) static void psci_suspend_to_standby_finisher(unsigned int cpu_idx,
psci_power_state_t *state_info,
unsigned int end_pwrlvl)
{ {
set_cpu_data(psci_svc_cpu_data.power_state, power_state); psci_acquire_pwr_domain_locks(end_pwrlvl,
flush_cpu_data(psci_svc_cpu_data.power_state); cpu_idx);
}
/******************************************************************************* /*
* This function gets the power level till which the current cpu could be * Plat. management: Allow the platform to do operations
* powered down during a cpu_suspend call. Returns PSCI_INVALID_DATA if the * on waking up from retention.
* power state is invalid. */
******************************************************************************/ psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
int psci_get_suspend_pwrlvl(void)
{
unsigned int power_state;
power_state = get_cpu_data(psci_svc_cpu_data.power_state); /*
* Set the requested and target state of this CPU and all the higher
* power domain levels for this CPU to run.
*/
psci_set_pwr_domains_to_run(end_pwrlvl);
return ((power_state == PSCI_INVALID_DATA) ? psci_release_pwr_domain_locks(end_pwrlvl,
power_state : psci_get_pstate_pwrlvl(power_state)); cpu_idx);
} }
/******************************************************************************* /*******************************************************************************
* This function gets the state id of the current cpu from the power state * This function does generic and platform specific suspend to power down
* parameter saved in the per-cpu data array. Returns PSCI_INVALID_DATA if the * operations.
* power state saved is invalid.
******************************************************************************/ ******************************************************************************/
int psci_get_suspend_stateid(void) static void psci_suspend_to_pwrdown_start(int end_pwrlvl,
entry_point_info_t *ep,
psci_power_state_t *state_info)
{ {
unsigned int power_state; /* Save PSCI target power level for the suspend finisher handler */
psci_set_suspend_pwrlvl(end_pwrlvl);
power_state = get_cpu_data(psci_svc_cpu_data.power_state);
return ((power_state == PSCI_INVALID_DATA) ? /*
power_state : psci_get_pstate_id(power_state)); * Flush the target power level as it will be accessed on power up with
} * Data cache disabled.
*/
flush_cpu_data(psci_svc_cpu_data.target_pwrlvl);
/******************************************************************************* /*
* This function gets the state id of the cpu specified by the cpu index * Call the cpu suspend handler registered by the Secure Payload
* from the power state parameter saved in the per-cpu data array. Returns * Dispatcher to let it do any book-keeping. If the handler encounters an
* PSCI_INVALID_DATA if the power state saved is invalid. * error, it's expected to assert within
******************************************************************************/ */
int psci_get_suspend_stateid_by_idx(unsigned long cpu_idx) if (psci_spd_pm && psci_spd_pm->svc_suspend)
{ psci_spd_pm->svc_suspend(0);
unsigned int power_state;
power_state = get_cpu_data_by_index(cpu_idx, /*
psci_svc_cpu_data.power_state); * Store the re-entry information for the non-secure world.
*/
cm_init_my_context(ep);
return ((power_state == PSCI_INVALID_DATA) ? /*
power_state : psci_get_pstate_id(power_state)); * Arch. management. Perform the necessary steps to flush all
* cpu caches. Currently we assume that the power level correspond
* the cache level.
* TODO : Introduce a mechanism to query the cache level to flush
* and the cpu-ops power down to perform from the platform.
*/
psci_do_pwrdown_cache_maintenance(psci_find_max_off_lvl(state_info));
} }
/******************************************************************************* /*******************************************************************************
* Top level handler which is called when a cpu wants to suspend its execution. * Top level handler which is called when a cpu wants to suspend its execution.
* It is assumed that along with suspending the cpu power domain, power domains * It is assumed that along with suspending the cpu power domain, power domains
* at higher levels until the target power level will be suspended as well. * at higher levels until the target power level will be suspended as well. It
* It finds the highest level where a domain has to be suspended by traversing * coordinates with the platform to negotiate the target state for each of
* the node information and then performs generic, architectural, platform * the power domain level till the target power domain level. It then performs
* setup and state management required to suspend that power domain and domains * generic, architectural, platform setup and state management required to
* below it. * e.g. For a cpu that's to be suspended, it could mean programming * suspend that power domain level and power domain levels below it.
* the power controller whereas for a cluster that's to be suspended, it will * e.g. For a cpu that's to be suspended, it could mean programming the
* call the platform specific code which will disable coherency at the * power controller whereas for a cluster that's to be suspended, it will call
* interconnect level if the cpu is the last in the cluster and also the * the platform specific code which will disable coherency at the interconnect
* program the power controller. * level if the cpu is the last in the cluster and also the program the power
* controller.
* *
* All the required parameter checks are performed at the beginning and after * All the required parameter checks are performed at the beginning and after
* the state transition has been done, no further error is expected and it is * the state transition has been done, no further error is expected and it is
* not possible to undo any of the actions taken beyond that point. * not possible to undo any of the actions taken beyond that point.
******************************************************************************/ ******************************************************************************/
void psci_cpu_suspend_start(entry_point_info_t *ep, int end_pwrlvl) void psci_cpu_suspend_start(entry_point_info_t *ep,
int end_pwrlvl,
psci_power_state_t *state_info,
unsigned int is_power_down_state)
{ {
int skip_wfi = 0; int skip_wfi = 0;
unsigned int max_phys_off_pwrlvl, idx = plat_my_core_pos(); unsigned int idx = plat_my_core_pos();
unsigned long psci_entrypoint; unsigned long psci_entrypoint;
/* /*
...@@ -146,39 +161,20 @@ void psci_cpu_suspend_start(entry_point_info_t *ep, int end_pwrlvl) ...@@ -146,39 +161,20 @@ void psci_cpu_suspend_start(entry_point_info_t *ep, int end_pwrlvl)
} }
/* /*
* Call the cpu suspend handler registered by the Secure Payload * This function is passed the requested state info and
* Dispatcher to let it do any bookeeping. If the handler encounters an * it returns the negotiated state info for each power level upto
* error, it's expected to assert within * the end level specified.
*/
if (psci_spd_pm && psci_spd_pm->svc_suspend)
psci_spd_pm->svc_suspend(0);
/*
* This function updates the state of each power domain instance
* corresponding to the cpu index in the range of power levels
* specified.
*/
psci_do_state_coordination(end_pwrlvl,
idx,
PSCI_STATE_SUSPEND);
max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(end_pwrlvl,
idx);
assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA);
/*
* Store the re-entry information for the non-secure world.
*/ */
cm_init_my_context(ep); psci_do_state_coordination(end_pwrlvl, state_info);
/* Set the secure world (EL3) re-entry point after BL1 */ psci_entrypoint = 0;
psci_entrypoint = (unsigned long) psci_cpu_suspend_finish_entry; if (is_power_down_state) {
psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info);
/* /* Set the secure world (EL3) re-entry point after BL1. */
* Arch. management. Perform the necessary steps to flush all psci_entrypoint =
* cpu caches. (unsigned long) psci_cpu_suspend_finish_entry;
*/ }
psci_do_pwrdown_cache_maintenance(max_phys_off_pwrlvl);
/* /*
* Plat. management: Allow the platform to perform the * Plat. management: Allow the platform to perform the
...@@ -186,8 +182,7 @@ void psci_cpu_suspend_start(entry_point_info_t *ep, int end_pwrlvl) ...@@ -186,8 +182,7 @@ void psci_cpu_suspend_start(entry_point_info_t *ep, int end_pwrlvl)
* platform defined mailbox with the psci entrypoint, * platform defined mailbox with the psci entrypoint,
* program the power controller etc. * program the power controller etc.
*/ */
psci_plat_pm_ops->pwr_domain_suspend(psci_entrypoint, psci_plat_pm_ops->pwr_domain_suspend(psci_entrypoint, state_info);
max_phys_off_pwrlvl);
exit: exit:
/* /*
...@@ -196,22 +191,40 @@ exit: ...@@ -196,22 +191,40 @@ exit:
*/ */
psci_release_pwr_domain_locks(end_pwrlvl, psci_release_pwr_domain_locks(end_pwrlvl,
idx); idx);
if (!skip_wfi) if (skip_wfi)
return;
if (is_power_down_state)
psci_power_down_wfi(); psci_power_down_wfi();
/*
* We will reach here if only retention/standby states have been
* requested at multiple power levels. This means that the cpu
* context will be preserved.
*/
wfi();
/*
* After we wake up from context retaining suspend, call the
* context retaining suspend finisher.
*/
psci_suspend_to_standby_finisher(idx, state_info, end_pwrlvl);
} }
/******************************************************************************* /*******************************************************************************
* The following functions finish an earlier suspend request. They * The following functions finish an earlier suspend request. They
* are called by the common finisher routine in psci_common.c. * are called by the common finisher routine in psci_common.c. The `state_info`
* is the psci_power_state from which this CPU has woken up from.
******************************************************************************/ ******************************************************************************/
void psci_cpu_suspend_finish(unsigned int cpu_idx, int max_off_pwrlvl) void psci_cpu_suspend_finish(unsigned int cpu_idx,
psci_power_state_t *state_info)
{ {
int32_t suspend_level; int32_t suspend_level;
uint64_t counter_freq; uint64_t counter_freq;
/* Ensure we have been woken up from a suspended state */ /* Ensure we have been woken up from a suspended state */
assert(psci_get_state(cpu_idx, PSCI_CPU_PWR_LVL) assert(psci_get_aff_info_state() == AFF_STATE_ON && is_local_state_off(\
== PSCI_STATE_SUSPEND); state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]));
/* /*
* Plat. management: Perform the platform specific actions * Plat. management: Perform the platform specific actions
...@@ -220,7 +233,7 @@ void psci_cpu_suspend_finish(unsigned int cpu_idx, int max_off_pwrlvl) ...@@ -220,7 +233,7 @@ void psci_cpu_suspend_finish(unsigned int cpu_idx, int max_off_pwrlvl)
* wrong then assert as there is no way to recover from this * wrong then assert as there is no way to recover from this
* situation. * situation.
*/ */
psci_plat_pm_ops->pwr_domain_suspend_finish(max_off_pwrlvl); psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
/* /*
* Arch. management: Enable the data cache, manage stack memory and * Arch. management: Enable the data cache, manage stack memory and
...@@ -244,8 +257,8 @@ void psci_cpu_suspend_finish(unsigned int cpu_idx, int max_off_pwrlvl) ...@@ -244,8 +257,8 @@ void psci_cpu_suspend_finish(unsigned int cpu_idx, int max_off_pwrlvl)
psci_spd_pm->svc_suspend_finish(suspend_level); psci_spd_pm->svc_suspend_finish(suspend_level);
} }
/* Invalidate the suspend context for the node */ /* Invalidate the suspend level for the cpu */
psci_set_suspend_power_state(PSCI_INVALID_DATA); psci_set_suspend_pwrlvl(PSCI_INVALID_DATA);
/* /*
* Generic management: Now we just need to retrieve the * Generic management: Now we just need to retrieve the
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment