xref: /rk3399_ARM-atf/plat/xilinx/versal/plat_psci.c (revision f7c48d9e30e9444f1fdb808ae5d06ed675e335fa)
1f91c3cb1SSiva Durga Prasad Paladugu /*
2b9fa2d9fSAbhyuday Godhasara  * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved.
3f91c3cb1SSiva Durga Prasad Paladugu  *
4f91c3cb1SSiva Durga Prasad Paladugu  * SPDX-License-Identifier: BSD-3-Clause
5f91c3cb1SSiva Durga Prasad Paladugu  */
6f91c3cb1SSiva Durga Prasad Paladugu 
75a8ffeabSTejas Patel #include <assert.h>
85a8ffeabSTejas Patel #include <plat_arm.h>
9d4821739STejas Patel #include <plat_private.h>
10394a65aaSTejas Patel #include <pm_common.h>
1109d40e0eSAntonio Nino Diaz #include <common/debug.h>
1209d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1309d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
1409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
150abf4bbaSSaeed Nowshadi #include <plat/arm/common/plat_arm.h>
1609d40e0eSAntonio Nino Diaz 
17394a65aaSTejas Patel #include "pm_api_sys.h"
18394a65aaSTejas Patel #include "pm_client.h"
19394a65aaSTejas Patel 
20f91c3cb1SSiva Durga Prasad Paladugu static uintptr_t versal_sec_entry;
21f91c3cb1SSiva Durga Prasad Paladugu 
22912b7a6fSVenkatesh Yadav Abbarapu static int32_t versal_pwr_domain_on(u_register_t mpidr)
23f91c3cb1SSiva Durga Prasad Paladugu {
24912b7a6fSVenkatesh Yadav Abbarapu 	int32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
25394a65aaSTejas Patel 	const struct pm_proc *proc;
26f91c3cb1SSiva Durga Prasad Paladugu 
27f91c3cb1SSiva Durga Prasad Paladugu 	VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
28f91c3cb1SSiva Durga Prasad Paladugu 
29b9fa2d9fSAbhyuday Godhasara 	if (cpu_id == -1) {
30f91c3cb1SSiva Durga Prasad Paladugu 		return PSCI_E_INTERN_FAIL;
31b9fa2d9fSAbhyuday Godhasara 	}
32f91c3cb1SSiva Durga Prasad Paladugu 
33912b7a6fSVenkatesh Yadav Abbarapu 	proc = pm_get_proc((uint32_t)cpu_id);
34f91c3cb1SSiva Durga Prasad Paladugu 
35394a65aaSTejas Patel 	/* Send request to PMC to wake up selected ACPU core */
36526a1fd1SAbhyuday Godhasara 	(void)pm_req_wakeup(proc->node_id, (versal_sec_entry & 0xFFFFFFFFU) | 0x1U,
374697164aSTejas Patel 			    versal_sec_entry >> 32, 0, SECURE_FLAG);
38f91c3cb1SSiva Durga Prasad Paladugu 
39394a65aaSTejas Patel 	/* Clear power down request */
40394a65aaSTejas Patel 	pm_client_wakeup(proc);
41f91c3cb1SSiva Durga Prasad Paladugu 
42f91c3cb1SSiva Durga Prasad Paladugu 	return PSCI_E_SUCCESS;
43f91c3cb1SSiva Durga Prasad Paladugu }
44f91c3cb1SSiva Durga Prasad Paladugu 
455a8ffeabSTejas Patel /**
465a8ffeabSTejas Patel  * versal_pwr_domain_suspend() - This function sends request to PMC to suspend
475a8ffeabSTejas Patel  * core.
485a8ffeabSTejas Patel  *
495a8ffeabSTejas Patel  * @target_state	Targated state
505a8ffeabSTejas Patel  */
515a8ffeabSTejas Patel static void versal_pwr_domain_suspend(const psci_power_state_t *target_state)
525a8ffeabSTejas Patel {
53912b7a6fSVenkatesh Yadav Abbarapu 	uint32_t state;
54912b7a6fSVenkatesh Yadav Abbarapu 	uint32_t cpu_id = plat_my_core_pos();
555a8ffeabSTejas Patel 	const struct pm_proc *proc = pm_get_proc(cpu_id);
565a8ffeabSTejas Patel 
570623dceaSAbhyuday Godhasara 	for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
585a8ffeabSTejas Patel 		VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
595a8ffeabSTejas Patel 			__func__, i, target_state->pwr_domain_state[i]);
60b9fa2d9fSAbhyuday Godhasara 	}
615a8ffeabSTejas Patel 
625a8ffeabSTejas Patel 	plat_versal_gic_cpuif_disable();
635a8ffeabSTejas Patel 
64d4c7b550SRavi Patel 	if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
655a8ffeabSTejas Patel 		plat_versal_gic_save();
66d4c7b550SRavi Patel 	}
675a8ffeabSTejas Patel 
685a8ffeabSTejas Patel 	state = target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE ?
695a8ffeabSTejas Patel 		PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
705a8ffeabSTejas Patel 
715a8ffeabSTejas Patel 	/* Send request to PMC to suspend this core */
72526a1fd1SAbhyuday Godhasara 	(void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry,
734697164aSTejas Patel 			      SECURE_FLAG);
745a8ffeabSTejas Patel 
755a8ffeabSTejas Patel 	/* APU is to be turned off */
765a8ffeabSTejas Patel 	if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
775a8ffeabSTejas Patel 		/* disable coherency */
785a8ffeabSTejas Patel 		plat_arm_interconnect_exit_coherency();
795a8ffeabSTejas Patel 	}
805a8ffeabSTejas Patel }
815a8ffeabSTejas Patel 
825a8ffeabSTejas Patel /**
835a8ffeabSTejas Patel  * versal_pwr_domain_suspend_finish() - This function performs actions to finish
845a8ffeabSTejas Patel  * suspend procedure.
855a8ffeabSTejas Patel  *
865a8ffeabSTejas Patel  * @target_state	Targated state
875a8ffeabSTejas Patel  */
885a8ffeabSTejas Patel static void versal_pwr_domain_suspend_finish(
895a8ffeabSTejas Patel 					const psci_power_state_t *target_state)
905a8ffeabSTejas Patel {
91912b7a6fSVenkatesh Yadav Abbarapu 	uint32_t cpu_id = plat_my_core_pos();
925a8ffeabSTejas Patel 	const struct pm_proc *proc = pm_get_proc(cpu_id);
935a8ffeabSTejas Patel 
940623dceaSAbhyuday Godhasara 	for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
955a8ffeabSTejas Patel 		VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
965a8ffeabSTejas Patel 			__func__, i, target_state->pwr_domain_state[i]);
97b9fa2d9fSAbhyuday Godhasara 	}
985a8ffeabSTejas Patel 
995a8ffeabSTejas Patel 	/* Clear the APU power control register for this cpu */
1005a8ffeabSTejas Patel 	pm_client_wakeup(proc);
1015a8ffeabSTejas Patel 
1025a8ffeabSTejas Patel 	/* enable coherency */
1035a8ffeabSTejas Patel 	plat_arm_interconnect_enter_coherency();
1045a8ffeabSTejas Patel 
1055a8ffeabSTejas Patel 	/* APU was turned off, so restore GIC context */
1065a8ffeabSTejas Patel 	if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
1075a8ffeabSTejas Patel 		plat_versal_gic_resume();
1085a8ffeabSTejas Patel 	}
109d4c7b550SRavi Patel 
110d4c7b550SRavi Patel 	plat_versal_gic_cpuif_enable();
1115a8ffeabSTejas Patel }
1125a8ffeabSTejas Patel 
113f91c3cb1SSiva Durga Prasad Paladugu void versal_pwr_domain_on_finish(const psci_power_state_t *target_state)
114f91c3cb1SSiva Durga Prasad Paladugu {
115f91c3cb1SSiva Durga Prasad Paladugu 	/* Enable the gic cpu interface */
116f91c3cb1SSiva Durga Prasad Paladugu 	plat_versal_gic_pcpu_init();
117f91c3cb1SSiva Durga Prasad Paladugu 
118f91c3cb1SSiva Durga Prasad Paladugu 	/* Program the gic per-cpu distributor or re-distributor interface */
119f91c3cb1SSiva Durga Prasad Paladugu 	plat_versal_gic_cpuif_enable();
120f91c3cb1SSiva Durga Prasad Paladugu }
121f91c3cb1SSiva Durga Prasad Paladugu 
1225a8ffeabSTejas Patel /**
1230abf4bbaSSaeed Nowshadi  * versal_system_off() - This function sends the system off request
1240abf4bbaSSaeed Nowshadi  * to firmware.  This function does not return.
1250abf4bbaSSaeed Nowshadi  */
1260abf4bbaSSaeed Nowshadi static void __dead2 versal_system_off(void)
1270abf4bbaSSaeed Nowshadi {
1280abf4bbaSSaeed Nowshadi 	/* Send the power down request to the PMC */
129526a1fd1SAbhyuday Godhasara 	(void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
1304697164aSTejas Patel 				 pm_get_shutdown_scope(), SECURE_FLAG);
1310abf4bbaSSaeed Nowshadi 
132b9fa2d9fSAbhyuday Godhasara 	while (1) {
1330abf4bbaSSaeed Nowshadi 		wfi();
1340abf4bbaSSaeed Nowshadi 	}
135b9fa2d9fSAbhyuday Godhasara }
1360abf4bbaSSaeed Nowshadi 
1370abf4bbaSSaeed Nowshadi /**
1380abf4bbaSSaeed Nowshadi  * versal_system_reset() - This function sends the reset request
1390abf4bbaSSaeed Nowshadi  * to firmware for the system to reset.  This function does not return.
1400abf4bbaSSaeed Nowshadi  */
1410abf4bbaSSaeed Nowshadi static void __dead2 versal_system_reset(void)
1420abf4bbaSSaeed Nowshadi {
1430abf4bbaSSaeed Nowshadi 	/* Send the system reset request to the PMC */
144526a1fd1SAbhyuday Godhasara 	(void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
1454697164aSTejas Patel 				 pm_get_shutdown_scope(), SECURE_FLAG);
1460abf4bbaSSaeed Nowshadi 
147b9fa2d9fSAbhyuday Godhasara 	while (1) {
1480abf4bbaSSaeed Nowshadi 		wfi();
1490abf4bbaSSaeed Nowshadi 	}
150b9fa2d9fSAbhyuday Godhasara }
1510abf4bbaSSaeed Nowshadi 
1520abf4bbaSSaeed Nowshadi /**
1535a8ffeabSTejas Patel  * versal_pwr_domain_off() - This function performs actions to turn off core
1545a8ffeabSTejas Patel  *
1555a8ffeabSTejas Patel  * @target_state	Targated state
1565a8ffeabSTejas Patel  */
1575a8ffeabSTejas Patel static void versal_pwr_domain_off(const psci_power_state_t *target_state)
1585a8ffeabSTejas Patel {
159912b7a6fSVenkatesh Yadav Abbarapu 	uint32_t cpu_id = plat_my_core_pos();
1605a8ffeabSTejas Patel 	const struct pm_proc *proc = pm_get_proc(cpu_id);
1615a8ffeabSTejas Patel 
1620623dceaSAbhyuday Godhasara 	for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
1635a8ffeabSTejas Patel 		VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
1645a8ffeabSTejas Patel 			__func__, i, target_state->pwr_domain_state[i]);
165b9fa2d9fSAbhyuday Godhasara 	}
1665a8ffeabSTejas Patel 
1675a8ffeabSTejas Patel 	/* Prevent interrupts from spuriously waking up this cpu */
1685a8ffeabSTejas Patel 	plat_versal_gic_cpuif_disable();
1695a8ffeabSTejas Patel 
1705a8ffeabSTejas Patel 	/*
1715a8ffeabSTejas Patel 	 * Send request to PMC to power down the appropriate APU CPU
1725a8ffeabSTejas Patel 	 * core.
1735a8ffeabSTejas Patel 	 * According to PSCI specification, CPU_off function does not
1745a8ffeabSTejas Patel 	 * have resume address and CPU core can only be woken up
1755a8ffeabSTejas Patel 	 * invoking CPU_on function, during which resume address will
1765a8ffeabSTejas Patel 	 * be set.
1775a8ffeabSTejas Patel 	 */
178526a1fd1SAbhyuday Godhasara 	(void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
1794697164aSTejas Patel 			      SECURE_FLAG);
1805a8ffeabSTejas Patel }
1815a8ffeabSTejas Patel 
1825a8ffeabSTejas Patel /**
1835a8ffeabSTejas Patel  * versal_validate_power_state() - This function ensures that the power state
1845a8ffeabSTejas Patel  * parameter in request is valid.
1855a8ffeabSTejas Patel  *
1865a8ffeabSTejas Patel  * @power_state		Power state of core
1875a8ffeabSTejas Patel  * @req_state		Requested state
1885a8ffeabSTejas Patel  *
1895a8ffeabSTejas Patel  * @return	Returns status, either success or reason
1905a8ffeabSTejas Patel  */
191912b7a6fSVenkatesh Yadav Abbarapu static int32_t versal_validate_power_state(uint32_t power_state,
1925a8ffeabSTejas Patel 				       psci_power_state_t *req_state)
1935a8ffeabSTejas Patel {
1945a8ffeabSTejas Patel 	VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
1955a8ffeabSTejas Patel 
196912b7a6fSVenkatesh Yadav Abbarapu 	uint32_t pstate = psci_get_pstate_type(power_state);
1975a8ffeabSTejas Patel 
1985a8ffeabSTejas Patel 	assert(req_state);
1995a8ffeabSTejas Patel 
2005a8ffeabSTejas Patel 	/* Sanity check the requested state */
201b9fa2d9fSAbhyuday Godhasara 	if (pstate == PSTATE_TYPE_STANDBY) {
2025a8ffeabSTejas Patel 		req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
203b9fa2d9fSAbhyuday Godhasara 	} else {
2045a8ffeabSTejas Patel 		req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
205b9fa2d9fSAbhyuday Godhasara 	}
2065a8ffeabSTejas Patel 
2075a8ffeabSTejas Patel 	/* We expect the 'state id' to be zero */
208a62c40d4SAbhyuday Godhasara 	if (psci_get_pstate_id(power_state) != 0U) {
2095a8ffeabSTejas Patel 		return PSCI_E_INVALID_PARAMS;
210b9fa2d9fSAbhyuday Godhasara 	}
2115a8ffeabSTejas Patel 
2125a8ffeabSTejas Patel 	return PSCI_E_SUCCESS;
2135a8ffeabSTejas Patel }
2145a8ffeabSTejas Patel 
2155a8ffeabSTejas Patel /**
2165a8ffeabSTejas Patel  * versal_get_sys_suspend_power_state() -  Get power state for system suspend
2175a8ffeabSTejas Patel  *
2185a8ffeabSTejas Patel  * @req_state	Requested state
2195a8ffeabSTejas Patel  */
2205a8ffeabSTejas Patel static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state)
2215a8ffeabSTejas Patel {
2225a8ffeabSTejas Patel 	req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
2235a8ffeabSTejas Patel 	req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
2245a8ffeabSTejas Patel }
2255a8ffeabSTejas Patel 
226f91c3cb1SSiva Durga Prasad Paladugu static const struct plat_psci_ops versal_nopmc_psci_ops = {
227394a65aaSTejas Patel 	.pwr_domain_on			= versal_pwr_domain_on,
2285a8ffeabSTejas Patel 	.pwr_domain_off			= versal_pwr_domain_off,
229f91c3cb1SSiva Durga Prasad Paladugu 	.pwr_domain_on_finish		= versal_pwr_domain_on_finish,
2305a8ffeabSTejas Patel 	.pwr_domain_suspend		= versal_pwr_domain_suspend,
2315a8ffeabSTejas Patel 	.pwr_domain_suspend_finish	= versal_pwr_domain_suspend_finish,
2320abf4bbaSSaeed Nowshadi 	.system_off			= versal_system_off,
2330abf4bbaSSaeed Nowshadi 	.system_reset			= versal_system_reset,
2345a8ffeabSTejas Patel 	.validate_power_state		= versal_validate_power_state,
2355a8ffeabSTejas Patel 	.get_sys_suspend_power_state	= versal_get_sys_suspend_power_state,
236f91c3cb1SSiva Durga Prasad Paladugu };
237f91c3cb1SSiva Durga Prasad Paladugu 
238f91c3cb1SSiva Durga Prasad Paladugu /*******************************************************************************
239f91c3cb1SSiva Durga Prasad Paladugu  * Export the platform specific power ops.
240f91c3cb1SSiva Durga Prasad Paladugu  ******************************************************************************/
241*f7c48d9eSVenkatesh Yadav Abbarapu int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint,
242f91c3cb1SSiva Durga Prasad Paladugu 			const struct plat_psci_ops **psci_ops)
243f91c3cb1SSiva Durga Prasad Paladugu {
244f91c3cb1SSiva Durga Prasad Paladugu 	versal_sec_entry = sec_entrypoint;
245f91c3cb1SSiva Durga Prasad Paladugu 
246f91c3cb1SSiva Durga Prasad Paladugu 	*psci_ops = &versal_nopmc_psci_ops;
247f91c3cb1SSiva Durga Prasad Paladugu 
248f91c3cb1SSiva Durga Prasad Paladugu 	return 0;
249f91c3cb1SSiva Durga Prasad Paladugu }
250