xref: /rk3399_ARM-atf/plat/arm/common/arm_pm.c (revision 2204afded5cf9557ef1bb934fd15a74b9fb42244)
1b4315306SDan Handley /*
2b4315306SDan Handley  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
4b4315306SDan Handley  * Redistribution and use in source and binary forms, with or without
5b4315306SDan Handley  * modification, are permitted provided that the following conditions are met:
6b4315306SDan Handley  *
7b4315306SDan Handley  * Redistributions of source code must retain the above copyright notice, this
8b4315306SDan Handley  * list of conditions and the following disclaimer.
9b4315306SDan Handley  *
10b4315306SDan Handley  * Redistributions in binary form must reproduce the above copyright notice,
11b4315306SDan Handley  * this list of conditions and the following disclaimer in the documentation
12b4315306SDan Handley  * and/or other materials provided with the distribution.
13b4315306SDan Handley  *
14b4315306SDan Handley  * Neither the name of ARM nor the names of its contributors may be used
15b4315306SDan Handley  * to endorse or promote products derived from this software without specific
16b4315306SDan Handley  * prior written permission.
17b4315306SDan Handley  *
18b4315306SDan Handley  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19b4315306SDan Handley  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b4315306SDan Handley  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b4315306SDan Handley  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22b4315306SDan Handley  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23b4315306SDan Handley  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b4315306SDan Handley  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b4315306SDan Handley  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26b4315306SDan Handley  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b4315306SDan Handley  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28b4315306SDan Handley  * POSSIBILITY OF SUCH DAMAGE.
29b4315306SDan Handley  */
30b4315306SDan Handley 
31b4315306SDan Handley #include <arch_helpers.h>
32b4315306SDan Handley #include <assert.h>
33b4315306SDan Handley #include <errno.h>
34*2204afdeSSoby Mathew #include <plat_arm.h>
35b4315306SDan Handley #include <psci.h>
36b4315306SDan Handley 
37*2204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
38*2204afdeSSoby Mathew extern unsigned int arm_pm_idle_states[];
39*2204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
40*2204afdeSSoby Mathew 
41*2204afdeSSoby Mathew 
42*2204afdeSSoby Mathew #if !ARM_RECOM_STATE_ID_ENC
43b4315306SDan Handley /*******************************************************************************
44b4315306SDan Handley  * ARM standard platform handler called to check the validity of the power state
45b4315306SDan Handley  * parameter.
46b4315306SDan Handley  ******************************************************************************/
4738dce70fSSoby Mathew int arm_validate_power_state(unsigned int power_state,
4838dce70fSSoby Mathew 			    psci_power_state_t *req_state)
49b4315306SDan Handley {
5038dce70fSSoby Mathew 	int pstate = psci_get_pstate_type(power_state);
5138dce70fSSoby Mathew 	int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
5238dce70fSSoby Mathew 	int i;
5338dce70fSSoby Mathew 
5438dce70fSSoby Mathew 	assert(req_state);
5538dce70fSSoby Mathew 
5638dce70fSSoby Mathew 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
57b4315306SDan Handley 		return PSCI_E_INVALID_PARAMS;
5838dce70fSSoby Mathew 
5938dce70fSSoby Mathew 	/* Sanity check the requested state */
6038dce70fSSoby Mathew 	if (pstate == PSTATE_TYPE_STANDBY) {
6138dce70fSSoby Mathew 		/*
6238dce70fSSoby Mathew 		 * It's possible to enter standby only on power level 0
6338dce70fSSoby Mathew 		 * Ignore any other power level.
6438dce70fSSoby Mathew 		 */
6538dce70fSSoby Mathew 		if (pwr_lvl != ARM_PWR_LVL0)
6638dce70fSSoby Mathew 			return PSCI_E_INVALID_PARAMS;
6738dce70fSSoby Mathew 
6838dce70fSSoby Mathew 		req_state->pwr_domain_state[ARM_PWR_LVL0] =
6938dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET;
7038dce70fSSoby Mathew 	} else {
7138dce70fSSoby Mathew 		for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
7238dce70fSSoby Mathew 			req_state->pwr_domain_state[i] =
7338dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF;
74b4315306SDan Handley 	}
75b4315306SDan Handley 
76b4315306SDan Handley 	/*
77b4315306SDan Handley 	 * We expect the 'state id' to be zero.
78b4315306SDan Handley 	 */
79b4315306SDan Handley 	if (psci_get_pstate_id(power_state))
80b4315306SDan Handley 		return PSCI_E_INVALID_PARAMS;
81b4315306SDan Handley 
82b4315306SDan Handley 	return PSCI_E_SUCCESS;
83b4315306SDan Handley }
84*2204afdeSSoby Mathew 
85*2204afdeSSoby Mathew #else
86*2204afdeSSoby Mathew /*******************************************************************************
87*2204afdeSSoby Mathew  * ARM standard platform handler called to check the validity of the power
88*2204afdeSSoby Mathew  * state parameter. The power state parameter has to be a composite power
89*2204afdeSSoby Mathew  * state.
90*2204afdeSSoby Mathew  ******************************************************************************/
91*2204afdeSSoby Mathew int arm_validate_power_state(unsigned int power_state,
92*2204afdeSSoby Mathew 				psci_power_state_t *req_state)
93*2204afdeSSoby Mathew {
94*2204afdeSSoby Mathew 	unsigned int state_id;
95*2204afdeSSoby Mathew 	int i;
96*2204afdeSSoby Mathew 
97*2204afdeSSoby Mathew 	assert(req_state);
98*2204afdeSSoby Mathew 
99*2204afdeSSoby Mathew 	/*
100*2204afdeSSoby Mathew 	 *  Currently we are using a linear search for finding the matching
101*2204afdeSSoby Mathew 	 *  entry in the idle power state array. This can be made a binary
102*2204afdeSSoby Mathew 	 *  search if the number of entries justify the additional complexity.
103*2204afdeSSoby Mathew 	 */
104*2204afdeSSoby Mathew 	for (i = 0; !!arm_pm_idle_states[i]; i++) {
105*2204afdeSSoby Mathew 		if (power_state == arm_pm_idle_states[i])
106*2204afdeSSoby Mathew 			break;
107*2204afdeSSoby Mathew 	}
108*2204afdeSSoby Mathew 
109*2204afdeSSoby Mathew 	/* Return error if entry not found in the idle state array */
110*2204afdeSSoby Mathew 	if (!arm_pm_idle_states[i])
111*2204afdeSSoby Mathew 		return PSCI_E_INVALID_PARAMS;
112*2204afdeSSoby Mathew 
113*2204afdeSSoby Mathew 	i = 0;
114*2204afdeSSoby Mathew 	state_id = psci_get_pstate_id(power_state);
115*2204afdeSSoby Mathew 
116*2204afdeSSoby Mathew 	/* Parse the State ID and populate the state info parameter */
117*2204afdeSSoby Mathew 	while (state_id) {
118*2204afdeSSoby Mathew 		req_state->pwr_domain_state[i++] = state_id &
119*2204afdeSSoby Mathew 						ARM_LOCAL_PSTATE_MASK;
120*2204afdeSSoby Mathew 		state_id >>= ARM_LOCAL_PSTATE_WIDTH;
121*2204afdeSSoby Mathew 	}
122*2204afdeSSoby Mathew 
123*2204afdeSSoby Mathew 	return PSCI_E_SUCCESS;
124*2204afdeSSoby Mathew }
125*2204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
126