xref: /rk3399_ARM-atf/plat/arm/common/arm_pm.c (revision 2a246d2e3284d853d1276b9495518aa52d8dba3c)
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>
9c1bb8a05SSoby Mathew #include <arm_gic.h>
10b4315306SDan Handley #include <assert.h>
11b4315306SDan Handley #include <errno.h>
122204afdeSSoby Mathew #include <plat_arm.h>
13e35a3fb5SSoby Mathew #include <platform.h>
14785fb92bSSoby Mathew #include <platform_def.h>
15b4315306SDan Handley #include <psci.h>
16b4315306SDan Handley 
17*2a246d2eSDimitris Papastamos /* Allow ARM Standard platforms to override these functions */
185486a965SSoby Mathew #pragma weak plat_arm_psci_override_pm_ops
19*2a246d2eSDimitris Papastamos #pragma weak plat_arm_program_trusted_mailbox
205486a965SSoby Mathew 
21785fb92bSSoby Mathew /* Standard ARM platforms are expected to export plat_arm_psci_pm_ops */
225486a965SSoby Mathew extern plat_psci_ops_t plat_arm_psci_pm_ops;
23785fb92bSSoby Mathew 
242204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
252204afdeSSoby Mathew extern unsigned int arm_pm_idle_states[];
262204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
272204afdeSSoby Mathew 
282204afdeSSoby Mathew #if !ARM_RECOM_STATE_ID_ENC
29b4315306SDan Handley /*******************************************************************************
30b4315306SDan Handley  * ARM standard platform handler called to check the validity of the power state
31b4315306SDan Handley  * parameter.
32b4315306SDan Handley  ******************************************************************************/
3338dce70fSSoby Mathew int arm_validate_power_state(unsigned int power_state,
3438dce70fSSoby Mathew 			    psci_power_state_t *req_state)
35b4315306SDan Handley {
3638dce70fSSoby Mathew 	int pstate = psci_get_pstate_type(power_state);
3738dce70fSSoby Mathew 	int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
3838dce70fSSoby Mathew 	int i;
3938dce70fSSoby Mathew 
4038dce70fSSoby Mathew 	assert(req_state);
4138dce70fSSoby Mathew 
4238dce70fSSoby Mathew 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
43b4315306SDan Handley 		return PSCI_E_INVALID_PARAMS;
4438dce70fSSoby Mathew 
4538dce70fSSoby Mathew 	/* Sanity check the requested state */
4638dce70fSSoby Mathew 	if (pstate == PSTATE_TYPE_STANDBY) {
4738dce70fSSoby Mathew 		/*
4838dce70fSSoby Mathew 		 * It's possible to enter standby only on power level 0
4938dce70fSSoby Mathew 		 * Ignore any other power level.
5038dce70fSSoby Mathew 		 */
5138dce70fSSoby Mathew 		if (pwr_lvl != ARM_PWR_LVL0)
5238dce70fSSoby Mathew 			return PSCI_E_INVALID_PARAMS;
5338dce70fSSoby Mathew 
5438dce70fSSoby Mathew 		req_state->pwr_domain_state[ARM_PWR_LVL0] =
5538dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET;
5638dce70fSSoby Mathew 	} else {
5738dce70fSSoby Mathew 		for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
5838dce70fSSoby Mathew 			req_state->pwr_domain_state[i] =
5938dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF;
60b4315306SDan Handley 	}
61b4315306SDan Handley 
62b4315306SDan Handley 	/*
63b4315306SDan Handley 	 * We expect the 'state id' to be zero.
64b4315306SDan Handley 	 */
65b4315306SDan Handley 	if (psci_get_pstate_id(power_state))
66b4315306SDan Handley 		return PSCI_E_INVALID_PARAMS;
67b4315306SDan Handley 
68b4315306SDan Handley 	return PSCI_E_SUCCESS;
69b4315306SDan Handley }
702204afdeSSoby Mathew 
712204afdeSSoby Mathew #else
722204afdeSSoby Mathew /*******************************************************************************
732204afdeSSoby Mathew  * ARM standard platform handler called to check the validity of the power
742204afdeSSoby Mathew  * state parameter. The power state parameter has to be a composite power
752204afdeSSoby Mathew  * state.
762204afdeSSoby Mathew  ******************************************************************************/
772204afdeSSoby Mathew int arm_validate_power_state(unsigned int power_state,
782204afdeSSoby Mathew 				psci_power_state_t *req_state)
792204afdeSSoby Mathew {
802204afdeSSoby Mathew 	unsigned int state_id;
812204afdeSSoby Mathew 	int i;
822204afdeSSoby Mathew 
832204afdeSSoby Mathew 	assert(req_state);
842204afdeSSoby Mathew 
852204afdeSSoby Mathew 	/*
862204afdeSSoby Mathew 	 *  Currently we are using a linear search for finding the matching
872204afdeSSoby Mathew 	 *  entry in the idle power state array. This can be made a binary
882204afdeSSoby Mathew 	 *  search if the number of entries justify the additional complexity.
892204afdeSSoby Mathew 	 */
902204afdeSSoby Mathew 	for (i = 0; !!arm_pm_idle_states[i]; i++) {
912204afdeSSoby Mathew 		if (power_state == arm_pm_idle_states[i])
922204afdeSSoby Mathew 			break;
932204afdeSSoby Mathew 	}
942204afdeSSoby Mathew 
952204afdeSSoby Mathew 	/* Return error if entry not found in the idle state array */
962204afdeSSoby Mathew 	if (!arm_pm_idle_states[i])
972204afdeSSoby Mathew 		return PSCI_E_INVALID_PARAMS;
982204afdeSSoby Mathew 
992204afdeSSoby Mathew 	i = 0;
1002204afdeSSoby Mathew 	state_id = psci_get_pstate_id(power_state);
1012204afdeSSoby Mathew 
1022204afdeSSoby Mathew 	/* Parse the State ID and populate the state info parameter */
1032204afdeSSoby Mathew 	while (state_id) {
1042204afdeSSoby Mathew 		req_state->pwr_domain_state[i++] = state_id &
1052204afdeSSoby Mathew 						ARM_LOCAL_PSTATE_MASK;
1062204afdeSSoby Mathew 		state_id >>= ARM_LOCAL_PSTATE_WIDTH;
1072204afdeSSoby Mathew 	}
1082204afdeSSoby Mathew 
1092204afdeSSoby Mathew 	return PSCI_E_SUCCESS;
1102204afdeSSoby Mathew }
1112204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
112f9e858b1SSoby Mathew 
113f9e858b1SSoby Mathew /*******************************************************************************
114f9e858b1SSoby Mathew  * ARM standard platform handler called to check the validity of the non secure
11571e7a4e5SJeenu Viswambharan  * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
116f9e858b1SSoby Mathew  ******************************************************************************/
117f9e858b1SSoby Mathew int arm_validate_ns_entrypoint(uintptr_t entrypoint)
118f9e858b1SSoby Mathew {
119f9e858b1SSoby Mathew 	/*
120f9e858b1SSoby Mathew 	 * Check if the non secure entrypoint lies within the non
121f9e858b1SSoby Mathew 	 * secure DRAM.
122f9e858b1SSoby Mathew 	 */
123f9e858b1SSoby Mathew 	if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
12471e7a4e5SJeenu Viswambharan 			(ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
12571e7a4e5SJeenu Viswambharan 		return 0;
12671e7a4e5SJeenu Viswambharan 	}
1277c7dffd8Sdp-arm #ifndef AARCH32
128f9e858b1SSoby Mathew 	if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
12971e7a4e5SJeenu Viswambharan 			(ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
13071e7a4e5SJeenu Viswambharan 		return 0;
13171e7a4e5SJeenu Viswambharan 	}
1327c7dffd8Sdp-arm #endif
133f9e858b1SSoby Mathew 
13471e7a4e5SJeenu Viswambharan 	return -1;
13571e7a4e5SJeenu Viswambharan }
13671e7a4e5SJeenu Viswambharan 
13771e7a4e5SJeenu Viswambharan int arm_validate_psci_entrypoint(uintptr_t entrypoint)
13871e7a4e5SJeenu Viswambharan {
13971e7a4e5SJeenu Viswambharan 	return arm_validate_ns_entrypoint(entrypoint) == 0 ? PSCI_E_SUCCESS :
14071e7a4e5SJeenu Viswambharan 		PSCI_E_INVALID_ADDRESS;
141f9e858b1SSoby Mathew }
142785fb92bSSoby Mathew 
143c1bb8a05SSoby Mathew /******************************************************************************
1445486a965SSoby Mathew  * Default definition on ARM standard platforms to override the plat_psci_ops.
1455486a965SSoby Mathew  *****************************************************************************/
1465486a965SSoby Mathew const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
1475486a965SSoby Mathew {
1485486a965SSoby Mathew 	return ops;
1495486a965SSoby Mathew }
1505486a965SSoby Mathew 
1515486a965SSoby Mathew /******************************************************************************
152e35a3fb5SSoby Mathew  * Helper function to save the platform state before a system suspend. Save the
153e35a3fb5SSoby Mathew  * state of the system components which are not in the Always ON power domain.
154e35a3fb5SSoby Mathew  *****************************************************************************/
155e35a3fb5SSoby Mathew void arm_system_pwr_domain_save(void)
156e35a3fb5SSoby Mathew {
157e35a3fb5SSoby Mathew 	/* Assert system power domain is available on the platform */
158e35a3fb5SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
159e35a3fb5SSoby Mathew 
160e35a3fb5SSoby Mathew 	plat_arm_gic_save();
161e35a3fb5SSoby Mathew 
162e35a3fb5SSoby Mathew 	/*
16388a0523eSAntonio Nino Diaz 	 * Unregister console now so that it is not registered for a second
16488a0523eSAntonio Nino Diaz 	 * time during resume.
16588a0523eSAntonio Nino Diaz 	 */
16688a0523eSAntonio Nino Diaz 	arm_console_runtime_end();
16788a0523eSAntonio Nino Diaz 
16888a0523eSAntonio Nino Diaz 	/*
169e35a3fb5SSoby Mathew 	 * All the other peripheral which are configured by ARM TF are
170e35a3fb5SSoby Mathew 	 * re-initialized on resume from system suspend. Hence we
171e35a3fb5SSoby Mathew 	 * don't save their state here.
172e35a3fb5SSoby Mathew 	 */
173e35a3fb5SSoby Mathew }
174e35a3fb5SSoby Mathew 
175e35a3fb5SSoby Mathew /******************************************************************************
176c1bb8a05SSoby Mathew  * Helper function to resume the platform from system suspend. Reinitialize
177c1bb8a05SSoby Mathew  * the system components which are not in the Always ON power domain.
178c1bb8a05SSoby Mathew  * TODO: Unify the platform setup when waking up from cold boot and system
179c1bb8a05SSoby Mathew  * resume in arm_bl31_platform_setup().
180c1bb8a05SSoby Mathew  *****************************************************************************/
181c1bb8a05SSoby Mathew void arm_system_pwr_domain_resume(void)
182c1bb8a05SSoby Mathew {
18388a0523eSAntonio Nino Diaz 	/* Initialize the console */
18488a0523eSAntonio Nino Diaz 	arm_console_runtime_init();
185c1bb8a05SSoby Mathew 
186c1bb8a05SSoby Mathew 	/* Assert system power domain is available on the platform */
187c1bb8a05SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
188c1bb8a05SSoby Mathew 
189e35a3fb5SSoby Mathew 	plat_arm_gic_resume();
190e35a3fb5SSoby Mathew 
191c1bb8a05SSoby Mathew 	plat_arm_security_setup();
192c1bb8a05SSoby Mathew 	arm_configure_sys_timer();
193c1bb8a05SSoby Mathew }
194c1bb8a05SSoby Mathew 
195785fb92bSSoby Mathew /*******************************************************************************
196*2a246d2eSDimitris Papastamos  * ARM platform function to program the mailbox for a cpu before it is released
197785fb92bSSoby Mathew  * from reset. This function assumes that the Trusted mail box base is within
198785fb92bSSoby Mathew  * the ARM_SHARED_RAM region
199785fb92bSSoby Mathew  ******************************************************************************/
200*2a246d2eSDimitris Papastamos void plat_arm_program_trusted_mailbox(uintptr_t address)
201785fb92bSSoby Mathew {
202785fb92bSSoby Mathew 	uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
203785fb92bSSoby Mathew 
204785fb92bSSoby Mathew 	*mailbox = address;
205785fb92bSSoby Mathew 
206785fb92bSSoby Mathew 	/*
207785fb92bSSoby Mathew 	 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
208785fb92bSSoby Mathew 	 * ARM_SHARED_RAM region.
209785fb92bSSoby Mathew 	 */
210785fb92bSSoby Mathew 	assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
211785fb92bSSoby Mathew 		((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
212785fb92bSSoby Mathew 				(ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
213785fb92bSSoby Mathew }
214785fb92bSSoby Mathew 
215785fb92bSSoby Mathew /*******************************************************************************
216785fb92bSSoby Mathew  * The ARM Standard platform definition of platform porting API
217785fb92bSSoby Mathew  * `plat_setup_psci_ops`.
218785fb92bSSoby Mathew  ******************************************************************************/
219785fb92bSSoby Mathew int plat_setup_psci_ops(uintptr_t sec_entrypoint,
220785fb92bSSoby Mathew 				const plat_psci_ops_t **psci_ops)
221785fb92bSSoby Mathew {
2225486a965SSoby Mathew 	*psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
223785fb92bSSoby Mathew 
224785fb92bSSoby Mathew 	/* Setup mailbox with entry point. */
225*2a246d2eSDimitris Papastamos 	plat_arm_program_trusted_mailbox(sec_entrypoint);
226785fb92bSSoby Mathew 	return 0;
227785fb92bSSoby Mathew }
228