xref: /rk3399_ARM-atf/plat/arm/common/arm_pm.c (revision e02f469f88e86c6bf90ba58b219b6629fad6e82c)
1b4315306SDan Handley /*
288a0523eSAntonio Nino Diaz  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5b4315306SDan Handley  */
6b4315306SDan Handley 
7b4315306SDan Handley #include <arch_helpers.h>
8f9e858b1SSoby Mathew #include <arm_def.h>
9b4315306SDan Handley #include <assert.h>
10b4315306SDan Handley #include <errno.h>
112204afdeSSoby Mathew #include <plat_arm.h>
12e35a3fb5SSoby Mathew #include <platform.h>
13785fb92bSSoby Mathew #include <platform_def.h>
14b4315306SDan Handley #include <psci.h>
15b4315306SDan Handley 
162a246d2eSDimitris Papastamos /* Allow ARM Standard platforms to override these functions */
175486a965SSoby Mathew #pragma weak plat_arm_psci_override_pm_ops
182a246d2eSDimitris Papastamos #pragma weak plat_arm_program_trusted_mailbox
195486a965SSoby Mathew 
202204afdeSSoby Mathew #if !ARM_RECOM_STATE_ID_ENC
21b4315306SDan Handley /*******************************************************************************
22b4315306SDan Handley  * ARM standard platform handler called to check the validity of the power state
23b4315306SDan Handley  * parameter.
24b4315306SDan Handley  ******************************************************************************/
2538dce70fSSoby Mathew int arm_validate_power_state(unsigned int power_state,
2638dce70fSSoby Mathew 			    psci_power_state_t *req_state)
27b4315306SDan Handley {
282bc3dba9SAntonio Nino Diaz 	unsigned int pstate = psci_get_pstate_type(power_state);
292bc3dba9SAntonio Nino Diaz 	unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
302bc3dba9SAntonio Nino Diaz 	unsigned int i;
3138dce70fSSoby Mathew 
32*e02f469fSSathees Balya 	assert(req_state != NULL);
3338dce70fSSoby Mathew 
3438dce70fSSoby Mathew 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
35b4315306SDan Handley 		return PSCI_E_INVALID_PARAMS;
3638dce70fSSoby Mathew 
3738dce70fSSoby Mathew 	/* Sanity check the requested state */
3838dce70fSSoby Mathew 	if (pstate == PSTATE_TYPE_STANDBY) {
3938dce70fSSoby Mathew 		/*
4038dce70fSSoby Mathew 		 * It's possible to enter standby only on power level 0
4138dce70fSSoby Mathew 		 * Ignore any other power level.
4238dce70fSSoby Mathew 		 */
4338dce70fSSoby Mathew 		if (pwr_lvl != ARM_PWR_LVL0)
4438dce70fSSoby Mathew 			return PSCI_E_INVALID_PARAMS;
4538dce70fSSoby Mathew 
4638dce70fSSoby Mathew 		req_state->pwr_domain_state[ARM_PWR_LVL0] =
4738dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET;
4838dce70fSSoby Mathew 	} else {
4938dce70fSSoby Mathew 		for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
5038dce70fSSoby Mathew 			req_state->pwr_domain_state[i] =
5138dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF;
52b4315306SDan Handley 	}
53b4315306SDan Handley 
54b4315306SDan Handley 	/*
55b4315306SDan Handley 	 * We expect the 'state id' to be zero.
56b4315306SDan Handley 	 */
572bc3dba9SAntonio Nino Diaz 	if (psci_get_pstate_id(power_state) != 0U)
58b4315306SDan Handley 		return PSCI_E_INVALID_PARAMS;
59b4315306SDan Handley 
60b4315306SDan Handley 	return PSCI_E_SUCCESS;
61b4315306SDan Handley }
622204afdeSSoby Mathew 
632204afdeSSoby Mathew #else
642204afdeSSoby Mathew /*******************************************************************************
652204afdeSSoby Mathew  * ARM standard platform handler called to check the validity of the power
662204afdeSSoby Mathew  * state parameter. The power state parameter has to be a composite power
672204afdeSSoby Mathew  * state.
682204afdeSSoby Mathew  ******************************************************************************/
692204afdeSSoby Mathew int arm_validate_power_state(unsigned int power_state,
702204afdeSSoby Mathew 				psci_power_state_t *req_state)
712204afdeSSoby Mathew {
722204afdeSSoby Mathew 	unsigned int state_id;
732204afdeSSoby Mathew 	int i;
742204afdeSSoby Mathew 
75*e02f469fSSathees Balya 	assert(req_state != NULL);
762204afdeSSoby Mathew 
772204afdeSSoby Mathew 	/*
782204afdeSSoby Mathew 	 *  Currently we are using a linear search for finding the matching
792204afdeSSoby Mathew 	 *  entry in the idle power state array. This can be made a binary
802204afdeSSoby Mathew 	 *  search if the number of entries justify the additional complexity.
812204afdeSSoby Mathew 	 */
822204afdeSSoby Mathew 	for (i = 0; !!arm_pm_idle_states[i]; i++) {
832204afdeSSoby Mathew 		if (power_state == arm_pm_idle_states[i])
842204afdeSSoby Mathew 			break;
852204afdeSSoby Mathew 	}
862204afdeSSoby Mathew 
872204afdeSSoby Mathew 	/* Return error if entry not found in the idle state array */
882204afdeSSoby Mathew 	if (!arm_pm_idle_states[i])
892204afdeSSoby Mathew 		return PSCI_E_INVALID_PARAMS;
902204afdeSSoby Mathew 
912204afdeSSoby Mathew 	i = 0;
922204afdeSSoby Mathew 	state_id = psci_get_pstate_id(power_state);
932204afdeSSoby Mathew 
942204afdeSSoby Mathew 	/* Parse the State ID and populate the state info parameter */
952204afdeSSoby Mathew 	while (state_id) {
962204afdeSSoby Mathew 		req_state->pwr_domain_state[i++] = state_id &
972204afdeSSoby Mathew 						ARM_LOCAL_PSTATE_MASK;
982204afdeSSoby Mathew 		state_id >>= ARM_LOCAL_PSTATE_WIDTH;
992204afdeSSoby Mathew 	}
1002204afdeSSoby Mathew 
1012204afdeSSoby Mathew 	return PSCI_E_SUCCESS;
1022204afdeSSoby Mathew }
1032204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
104f9e858b1SSoby Mathew 
105f9e858b1SSoby Mathew /*******************************************************************************
106f9e858b1SSoby Mathew  * ARM standard platform handler called to check the validity of the non secure
10771e7a4e5SJeenu Viswambharan  * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
108f9e858b1SSoby Mathew  ******************************************************************************/
109f9e858b1SSoby Mathew int arm_validate_ns_entrypoint(uintptr_t entrypoint)
110f9e858b1SSoby Mathew {
111f9e858b1SSoby Mathew 	/*
112f9e858b1SSoby Mathew 	 * Check if the non secure entrypoint lies within the non
113f9e858b1SSoby Mathew 	 * secure DRAM.
114f9e858b1SSoby Mathew 	 */
115f9e858b1SSoby Mathew 	if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
11671e7a4e5SJeenu Viswambharan 			(ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
11771e7a4e5SJeenu Viswambharan 		return 0;
11871e7a4e5SJeenu Viswambharan 	}
1197c7dffd8Sdp-arm #ifndef AARCH32
120f9e858b1SSoby Mathew 	if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
12171e7a4e5SJeenu Viswambharan 			(ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
12271e7a4e5SJeenu Viswambharan 		return 0;
12371e7a4e5SJeenu Viswambharan 	}
1247c7dffd8Sdp-arm #endif
125f9e858b1SSoby Mathew 
12671e7a4e5SJeenu Viswambharan 	return -1;
12771e7a4e5SJeenu Viswambharan }
12871e7a4e5SJeenu Viswambharan 
12971e7a4e5SJeenu Viswambharan int arm_validate_psci_entrypoint(uintptr_t entrypoint)
13071e7a4e5SJeenu Viswambharan {
131*e02f469fSSathees Balya 	return (arm_validate_ns_entrypoint(entrypoint) == 0) ? PSCI_E_SUCCESS :
13271e7a4e5SJeenu Viswambharan 		PSCI_E_INVALID_ADDRESS;
133f9e858b1SSoby Mathew }
134785fb92bSSoby Mathew 
135c1bb8a05SSoby Mathew /******************************************************************************
1365486a965SSoby Mathew  * Default definition on ARM standard platforms to override the plat_psci_ops.
1375486a965SSoby Mathew  *****************************************************************************/
1385486a965SSoby Mathew const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
1395486a965SSoby Mathew {
1405486a965SSoby Mathew 	return ops;
1415486a965SSoby Mathew }
1425486a965SSoby Mathew 
1435486a965SSoby Mathew /******************************************************************************
144e35a3fb5SSoby Mathew  * Helper function to save the platform state before a system suspend. Save the
145e35a3fb5SSoby Mathew  * state of the system components which are not in the Always ON power domain.
146e35a3fb5SSoby Mathew  *****************************************************************************/
147e35a3fb5SSoby Mathew void arm_system_pwr_domain_save(void)
148e35a3fb5SSoby Mathew {
149e35a3fb5SSoby Mathew 	/* Assert system power domain is available on the platform */
150e35a3fb5SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
151e35a3fb5SSoby Mathew 
152e35a3fb5SSoby Mathew 	plat_arm_gic_save();
153e35a3fb5SSoby Mathew 
154e35a3fb5SSoby Mathew 	/*
15588a0523eSAntonio Nino Diaz 	 * Unregister console now so that it is not registered for a second
15688a0523eSAntonio Nino Diaz 	 * time during resume.
15788a0523eSAntonio Nino Diaz 	 */
15888a0523eSAntonio Nino Diaz 	arm_console_runtime_end();
15988a0523eSAntonio Nino Diaz 
16088a0523eSAntonio Nino Diaz 	/*
161e35a3fb5SSoby Mathew 	 * All the other peripheral which are configured by ARM TF are
162e35a3fb5SSoby Mathew 	 * re-initialized on resume from system suspend. Hence we
163e35a3fb5SSoby Mathew 	 * don't save their state here.
164e35a3fb5SSoby Mathew 	 */
165e35a3fb5SSoby Mathew }
166e35a3fb5SSoby Mathew 
167e35a3fb5SSoby Mathew /******************************************************************************
168c1bb8a05SSoby Mathew  * Helper function to resume the platform from system suspend. Reinitialize
169c1bb8a05SSoby Mathew  * the system components which are not in the Always ON power domain.
170c1bb8a05SSoby Mathew  * TODO: Unify the platform setup when waking up from cold boot and system
171c1bb8a05SSoby Mathew  * resume in arm_bl31_platform_setup().
172c1bb8a05SSoby Mathew  *****************************************************************************/
173c1bb8a05SSoby Mathew void arm_system_pwr_domain_resume(void)
174c1bb8a05SSoby Mathew {
17588a0523eSAntonio Nino Diaz 	/* Initialize the console */
17688a0523eSAntonio Nino Diaz 	arm_console_runtime_init();
177c1bb8a05SSoby Mathew 
178c1bb8a05SSoby Mathew 	/* Assert system power domain is available on the platform */
179c1bb8a05SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
180c1bb8a05SSoby Mathew 
181e35a3fb5SSoby Mathew 	plat_arm_gic_resume();
182e35a3fb5SSoby Mathew 
183c1bb8a05SSoby Mathew 	plat_arm_security_setup();
184c1bb8a05SSoby Mathew 	arm_configure_sys_timer();
185c1bb8a05SSoby Mathew }
186c1bb8a05SSoby Mathew 
187785fb92bSSoby Mathew /*******************************************************************************
1882a246d2eSDimitris Papastamos  * ARM platform function to program the mailbox for a cpu before it is released
189785fb92bSSoby Mathew  * from reset. This function assumes that the Trusted mail box base is within
190785fb92bSSoby Mathew  * the ARM_SHARED_RAM region
191785fb92bSSoby Mathew  ******************************************************************************/
1922a246d2eSDimitris Papastamos void plat_arm_program_trusted_mailbox(uintptr_t address)
193785fb92bSSoby Mathew {
194785fb92bSSoby Mathew 	uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
195785fb92bSSoby Mathew 
196785fb92bSSoby Mathew 	*mailbox = address;
197785fb92bSSoby Mathew 
198785fb92bSSoby Mathew 	/*
199785fb92bSSoby Mathew 	 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
200785fb92bSSoby Mathew 	 * ARM_SHARED_RAM region.
201785fb92bSSoby Mathew 	 */
202785fb92bSSoby Mathew 	assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
203785fb92bSSoby Mathew 		((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
204785fb92bSSoby Mathew 				(ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
205785fb92bSSoby Mathew }
206785fb92bSSoby Mathew 
207785fb92bSSoby Mathew /*******************************************************************************
208785fb92bSSoby Mathew  * The ARM Standard platform definition of platform porting API
209785fb92bSSoby Mathew  * `plat_setup_psci_ops`.
210785fb92bSSoby Mathew  ******************************************************************************/
2114d010d0dSDaniel Boulby int __init plat_setup_psci_ops(uintptr_t sec_entrypoint,
212785fb92bSSoby Mathew 				const plat_psci_ops_t **psci_ops)
213785fb92bSSoby Mathew {
2145486a965SSoby Mathew 	*psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
215785fb92bSSoby Mathew 
216785fb92bSSoby Mathew 	/* Setup mailbox with entry point. */
2172a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(sec_entrypoint);
218785fb92bSSoby Mathew 	return 0;
219785fb92bSSoby Mathew }
220