xref: /rk3399_ARM-atf/plat/nvidia/tegra/common/tegra_pm.c (revision 544c092b9c77ebe8c23f0fee96e3342dd80f5a6d)
108438e24SVarun Wadekar /*
2500fc9e1SVarun Wadekar  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
308438e24SVarun Wadekar  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
508438e24SVarun Wadekar  */
608438e24SVarun Wadekar 
708438e24SVarun Wadekar #include <assert.h>
809d40e0eSAntonio Nino Diaz 
908438e24SVarun Wadekar #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <context.h>
1509d40e0eSAntonio Nino Diaz #include <drivers/console.h>
1609d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
1709d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1809d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
1909d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2009d40e0eSAntonio Nino Diaz 
2109d40e0eSAntonio Nino Diaz #include <memctrl.h>
2208438e24SVarun Wadekar #include <pmc.h>
2308438e24SVarun Wadekar #include <tegra_def.h>
24322e7c3eSHarvey Hsieh #include <tegra_platform.h>
2508438e24SVarun Wadekar #include <tegra_private.h>
2608438e24SVarun Wadekar 
2708438e24SVarun Wadekar extern uint64_t tegra_bl31_phys_base;
2871cb26eaSVarun Wadekar extern uint64_t tegra_sec_entry_point;
2908438e24SVarun Wadekar 
3008438e24SVarun Wadekar /*
31a9e0260cSVignesh Radhakrishnan  * tegra_fake_system_suspend acts as a boolean var controlling whether
32a9e0260cSVignesh Radhakrishnan  * we are going to take fake system suspend code or normal system suspend code
33a9e0260cSVignesh Radhakrishnan  * path. This variable is set inside the sip call handlers,when the kernel
34a9e0260cSVignesh Radhakrishnan  * requests a SIP call to set the suspend debug flags.
35a9e0260cSVignesh Radhakrishnan  */
36a9e0260cSVignesh Radhakrishnan uint8_t tegra_fake_system_suspend;
37a9e0260cSVignesh Radhakrishnan 
38a9e0260cSVignesh Radhakrishnan /*
3908438e24SVarun Wadekar  * The following platform setup functions are weakly defined. They
4008438e24SVarun Wadekar  * provide typical implementations that will be overridden by a SoC.
4108438e24SVarun Wadekar  */
42cb95a19aSVarun Wadekar #pragma weak tegra_soc_pwr_domain_suspend_pwrdown_early
430887026eSVarun Wadekar #pragma weak tegra_soc_cpu_standby
4471cb26eaSVarun Wadekar #pragma weak tegra_soc_pwr_domain_suspend
4571cb26eaSVarun Wadekar #pragma weak tegra_soc_pwr_domain_on
4671cb26eaSVarun Wadekar #pragma weak tegra_soc_pwr_domain_off
4771cb26eaSVarun Wadekar #pragma weak tegra_soc_pwr_domain_on_finish
4826c0d9b2SVarun Wadekar #pragma weak tegra_soc_pwr_domain_power_down_wfi
493b40f993SVarun Wadekar #pragma weak tegra_soc_prepare_system_reset
5031a4957cSVarun Wadekar #pragma weak tegra_soc_prepare_system_off
51a7cd0953SVarun Wadekar #pragma weak tegra_soc_get_target_pwr_state
5208438e24SVarun Wadekar 
53b36aea5aSAnthony Zhou int32_t tegra_soc_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
54cb95a19aSVarun Wadekar {
55cb95a19aSVarun Wadekar 	return PSCI_E_NOT_SUPPORTED;
56cb95a19aSVarun Wadekar }
57cb95a19aSVarun Wadekar 
580887026eSVarun Wadekar int32_t tegra_soc_cpu_standby(plat_local_state_t cpu_state)
590887026eSVarun Wadekar {
600887026eSVarun Wadekar 	(void)cpu_state;
610887026eSVarun Wadekar 	return PSCI_E_SUCCESS;
620887026eSVarun Wadekar }
630887026eSVarun Wadekar 
64b36aea5aSAnthony Zhou int32_t tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
6508438e24SVarun Wadekar {
66b36aea5aSAnthony Zhou 	(void)target_state;
6708438e24SVarun Wadekar 	return PSCI_E_NOT_SUPPORTED;
6808438e24SVarun Wadekar }
6908438e24SVarun Wadekar 
70b36aea5aSAnthony Zhou int32_t tegra_soc_pwr_domain_on(u_register_t mpidr)
7108438e24SVarun Wadekar {
72b36aea5aSAnthony Zhou 	(void)mpidr;
7308438e24SVarun Wadekar 	return PSCI_E_SUCCESS;
7408438e24SVarun Wadekar }
7508438e24SVarun Wadekar 
76b36aea5aSAnthony Zhou int32_t tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
7708438e24SVarun Wadekar {
78b36aea5aSAnthony Zhou 	(void)target_state;
7908438e24SVarun Wadekar 	return PSCI_E_SUCCESS;
8008438e24SVarun Wadekar }
8108438e24SVarun Wadekar 
82b36aea5aSAnthony Zhou int32_t tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
8308438e24SVarun Wadekar {
84b36aea5aSAnthony Zhou 	(void)target_state;
8508438e24SVarun Wadekar 	return PSCI_E_SUCCESS;
8608438e24SVarun Wadekar }
8708438e24SVarun Wadekar 
88b36aea5aSAnthony Zhou int32_t tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
8926c0d9b2SVarun Wadekar {
90b36aea5aSAnthony Zhou 	(void)target_state;
9126c0d9b2SVarun Wadekar 	return PSCI_E_SUCCESS;
9226c0d9b2SVarun Wadekar }
9326c0d9b2SVarun Wadekar 
94b36aea5aSAnthony Zhou int32_t tegra_soc_prepare_system_reset(void)
953b40f993SVarun Wadekar {
963b40f993SVarun Wadekar 	return PSCI_E_SUCCESS;
973b40f993SVarun Wadekar }
983b40f993SVarun Wadekar 
9931a4957cSVarun Wadekar __dead2 void tegra_soc_prepare_system_off(void)
10031a4957cSVarun Wadekar {
10131a4957cSVarun Wadekar 	ERROR("Tegra System Off: operation not handled.\n");
10231a4957cSVarun Wadekar 	panic();
10331a4957cSVarun Wadekar }
10431a4957cSVarun Wadekar 
105b36aea5aSAnthony Zhou plat_local_state_t tegra_soc_get_target_pwr_state(uint32_t lvl,
106a7cd0953SVarun Wadekar 					     const plat_local_state_t *states,
107b36aea5aSAnthony Zhou 					     uint32_t ncpu)
108a7cd0953SVarun Wadekar {
1098539f45dSVarun Wadekar 	plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
110b36aea5aSAnthony Zhou 	uint32_t num_cpu = ncpu;
111b36aea5aSAnthony Zhou 	const plat_local_state_t *local_state = states;
112b36aea5aSAnthony Zhou 
113b36aea5aSAnthony Zhou 	(void)lvl;
114a7cd0953SVarun Wadekar 
1154c994002SAnthony Zhou 	assert(ncpu != 0U);
116a7cd0953SVarun Wadekar 
117a7cd0953SVarun Wadekar 	do {
118b36aea5aSAnthony Zhou 		temp = *local_state;
119b36aea5aSAnthony Zhou 		if ((temp < target)) {
120a7cd0953SVarun Wadekar 			target = temp;
121b36aea5aSAnthony Zhou 		}
122b36aea5aSAnthony Zhou 		--num_cpu;
123b36aea5aSAnthony Zhou 		local_state++;
124b36aea5aSAnthony Zhou 	} while (num_cpu != 0U);
125a7cd0953SVarun Wadekar 
126a7cd0953SVarun Wadekar 	return target;
127a7cd0953SVarun Wadekar }
128a7cd0953SVarun Wadekar 
12908438e24SVarun Wadekar /*******************************************************************************
13071cb26eaSVarun Wadekar  * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
13171cb26eaSVarun Wadekar  * call to get the `power_state` parameter. This allows the platform to encode
13271cb26eaSVarun Wadekar  * the appropriate State-ID field within the `power_state` parameter which can
13371cb26eaSVarun Wadekar  * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
13408438e24SVarun Wadekar ******************************************************************************/
13571cb26eaSVarun Wadekar void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
13608438e24SVarun Wadekar {
137a7cd0953SVarun Wadekar 	/* all affinities use system suspend state id */
138b36aea5aSAnthony Zhou 	for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) {
139a7cd0953SVarun Wadekar 		req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
14008438e24SVarun Wadekar 	}
141b36aea5aSAnthony Zhou }
14208438e24SVarun Wadekar 
14308438e24SVarun Wadekar /*******************************************************************************
14408438e24SVarun Wadekar  * Handler called when an affinity instance is about to enter standby.
14508438e24SVarun Wadekar  ******************************************************************************/
14671cb26eaSVarun Wadekar void tegra_cpu_standby(plat_local_state_t cpu_state)
14708438e24SVarun Wadekar {
14807faf4d8SVignesh Radhakrishnan 	u_register_t saved_scr_el3;
14907faf4d8SVignesh Radhakrishnan 
150b36aea5aSAnthony Zhou 	(void)cpu_state;
151b36aea5aSAnthony Zhou 
1520887026eSVarun Wadekar 	/* Tegra SoC specific handler */
1530887026eSVarun Wadekar 	if (tegra_soc_cpu_standby(cpu_state) != PSCI_E_SUCCESS)
1540887026eSVarun Wadekar 		ERROR("%s failed\n", __func__);
1550887026eSVarun Wadekar 
15607faf4d8SVignesh Radhakrishnan 	saved_scr_el3 = read_scr_el3();
15707faf4d8SVignesh Radhakrishnan 
15807faf4d8SVignesh Radhakrishnan 	/*
15907faf4d8SVignesh Radhakrishnan 	 * As per ARM ARM D1.17.2, any physical IRQ interrupt received by the
16007faf4d8SVignesh Radhakrishnan 	 * PE will be treated as a wake-up event, if SCR_EL3.IRQ is set to '1',
16107faf4d8SVignesh Radhakrishnan 	 * irrespective of the value of the PSTATE.I bit value.
16207faf4d8SVignesh Radhakrishnan 	 */
16307faf4d8SVignesh Radhakrishnan 	write_scr_el3(saved_scr_el3 | SCR_IRQ_BIT);
16407faf4d8SVignesh Radhakrishnan 
16508438e24SVarun Wadekar 	/*
16608438e24SVarun Wadekar 	 * Enter standby state
16707faf4d8SVignesh Radhakrishnan 	 *
16807faf4d8SVignesh Radhakrishnan 	 * dsb & isb is good practice before using wfi to enter low power states
16908438e24SVarun Wadekar 	 */
17008438e24SVarun Wadekar 	dsb();
17107faf4d8SVignesh Radhakrishnan 	isb();
17208438e24SVarun Wadekar 	wfi();
17307faf4d8SVignesh Radhakrishnan 
17407faf4d8SVignesh Radhakrishnan 	/*
17507faf4d8SVignesh Radhakrishnan 	 * Restore saved scr_el3 that has IRQ bit cleared as we don't want EL3
17607faf4d8SVignesh Radhakrishnan 	 * handling any further interrupts
17707faf4d8SVignesh Radhakrishnan 	 */
17807faf4d8SVignesh Radhakrishnan 	write_scr_el3(saved_scr_el3);
17908438e24SVarun Wadekar }
18008438e24SVarun Wadekar 
18108438e24SVarun Wadekar /*******************************************************************************
18208438e24SVarun Wadekar  * Handler called when an affinity instance is about to be turned on. The
18308438e24SVarun Wadekar  * level and mpidr determine the affinity instance.
18408438e24SVarun Wadekar  ******************************************************************************/
185b36aea5aSAnthony Zhou int32_t tegra_pwr_domain_on(u_register_t mpidr)
18608438e24SVarun Wadekar {
18771cb26eaSVarun Wadekar 	return tegra_soc_pwr_domain_on(mpidr);
18808438e24SVarun Wadekar }
18908438e24SVarun Wadekar 
19008438e24SVarun Wadekar /*******************************************************************************
19171cb26eaSVarun Wadekar  * Handler called when a power domain is about to be turned off. The
19271cb26eaSVarun Wadekar  * target_state encodes the power state that each level should transition to.
19308438e24SVarun Wadekar  ******************************************************************************/
19471cb26eaSVarun Wadekar void tegra_pwr_domain_off(const psci_power_state_t *target_state)
19508438e24SVarun Wadekar {
196b36aea5aSAnthony Zhou 	(void)tegra_soc_pwr_domain_off(target_state);
19708438e24SVarun Wadekar }
19808438e24SVarun Wadekar 
19908438e24SVarun Wadekar /*******************************************************************************
20026c0d9b2SVarun Wadekar  * Handler called when a power domain is about to be suspended. The
20171cb26eaSVarun Wadekar  * target_state encodes the power state that each level should transition to.
202cb95a19aSVarun Wadekar  * This handler is called with SMP and data cache enabled, when
203cb95a19aSVarun Wadekar  * HW_ASSISTED_COHERENCY = 0
204cb95a19aSVarun Wadekar  ******************************************************************************/
205cb95a19aSVarun Wadekar void tegra_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
206cb95a19aSVarun Wadekar {
207cb95a19aSVarun Wadekar 	tegra_soc_pwr_domain_suspend_pwrdown_early(target_state);
208cb95a19aSVarun Wadekar }
209cb95a19aSVarun Wadekar 
210cb95a19aSVarun Wadekar /*******************************************************************************
211cb95a19aSVarun Wadekar  * Handler called when a power domain is about to be suspended. The
212cb95a19aSVarun Wadekar  * target_state encodes the power state that each level should transition to.
21308438e24SVarun Wadekar  ******************************************************************************/
21471cb26eaSVarun Wadekar void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
21508438e24SVarun Wadekar {
216b36aea5aSAnthony Zhou 	(void)tegra_soc_pwr_domain_suspend(target_state);
21708438e24SVarun Wadekar 
2185b5928e8SVarun Wadekar 	/* Disable console if we are entering deep sleep. */
2195b5928e8SVarun Wadekar 	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
220b36aea5aSAnthony Zhou 			PSTATE_ID_SOC_POWERDN) {
221*544c092bSAmbroise Vincent 		(void)console_flush();
222*544c092bSAmbroise Vincent 		console_switch_state(0);
223b36aea5aSAnthony Zhou 	}
2245b5928e8SVarun Wadekar 
22508438e24SVarun Wadekar 	/* disable GICC */
22608438e24SVarun Wadekar 	tegra_gic_cpuif_deactivate();
22708438e24SVarun Wadekar }
22808438e24SVarun Wadekar 
22908438e24SVarun Wadekar /*******************************************************************************
23026c0d9b2SVarun Wadekar  * Handler called at the end of the power domain suspend sequence. The
23126c0d9b2SVarun Wadekar  * target_state encodes the power state that each level should transition to.
23226c0d9b2SVarun Wadekar  ******************************************************************************/
23326c0d9b2SVarun Wadekar __dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
23426c0d9b2SVarun Wadekar 					     *target_state)
23526c0d9b2SVarun Wadekar {
236a9e0260cSVignesh Radhakrishnan 	uint8_t pwr_state = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
237a9e0260cSVignesh Radhakrishnan 	uint64_t rmr_el3 = 0;
238a9e0260cSVignesh Radhakrishnan 
23926c0d9b2SVarun Wadekar 	/* call the chip's power down handler */
240b36aea5aSAnthony Zhou 	(void)tegra_soc_pwr_domain_power_down_wfi(target_state);
24126c0d9b2SVarun Wadekar 
242a9e0260cSVignesh Radhakrishnan 	/*
243a9e0260cSVignesh Radhakrishnan 	 * If we are in fake system suspend mode, ensure we start doing
244a9e0260cSVignesh Radhakrishnan 	 * procedures that help in looping back towards system suspend exit
245a9e0260cSVignesh Radhakrishnan 	 * instead of calling WFI by requesting a warm reset.
246a9e0260cSVignesh Radhakrishnan 	 * Else, just call WFI to enter low power state.
247a9e0260cSVignesh Radhakrishnan 	 */
248a9e0260cSVignesh Radhakrishnan 	if ((tegra_fake_system_suspend != 0U) &&
249a9e0260cSVignesh Radhakrishnan 	    (pwr_state == (uint8_t)PSTATE_ID_SOC_POWERDN)) {
250a9e0260cSVignesh Radhakrishnan 
251a9e0260cSVignesh Radhakrishnan 		/* warm reboot */
252a9e0260cSVignesh Radhakrishnan 		rmr_el3 = read_rmr_el3();
253a9e0260cSVignesh Radhakrishnan 		write_rmr_el3(rmr_el3 | RMR_WARM_RESET_CPU);
254a9e0260cSVignesh Radhakrishnan 
255a9e0260cSVignesh Radhakrishnan 	} else {
25626c0d9b2SVarun Wadekar 		/* enter power down state */
25726c0d9b2SVarun Wadekar 		wfi();
258a9e0260cSVignesh Radhakrishnan 	}
25926c0d9b2SVarun Wadekar 
26026c0d9b2SVarun Wadekar 	/* we can never reach here */
26126c0d9b2SVarun Wadekar 	panic();
26226c0d9b2SVarun Wadekar }
26326c0d9b2SVarun Wadekar 
26426c0d9b2SVarun Wadekar /*******************************************************************************
26571cb26eaSVarun Wadekar  * Handler called when a power domain has just been powered on after
26671cb26eaSVarun Wadekar  * being turned off earlier. The target_state encodes the low power state that
26771cb26eaSVarun Wadekar  * each level has woken up from.
26808438e24SVarun Wadekar  ******************************************************************************/
26971cb26eaSVarun Wadekar void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
27008438e24SVarun Wadekar {
271b36aea5aSAnthony Zhou 	const plat_params_from_bl2_t *plat_params;
27208438e24SVarun Wadekar 
27308438e24SVarun Wadekar 	/*
27408438e24SVarun Wadekar 	 * Initialize the GIC cpu and distributor interfaces
27508438e24SVarun Wadekar 	 */
276500fc9e1SVarun Wadekar 	tegra_gic_init();
27708438e24SVarun Wadekar 
27808438e24SVarun Wadekar 	/*
27908438e24SVarun Wadekar 	 * Check if we are exiting from deep sleep.
28008438e24SVarun Wadekar 	 */
28171cb26eaSVarun Wadekar 	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
28271cb26eaSVarun Wadekar 			PSTATE_ID_SOC_POWERDN) {
28308438e24SVarun Wadekar 
284*544c092bSAmbroise Vincent 		/* Restart console output. */
285*544c092bSAmbroise Vincent 		console_switch_state(CONSOLE_FLAG_RUNTIME);
2865b5928e8SVarun Wadekar 
28708438e24SVarun Wadekar 		/*
288102e4087SVarun Wadekar 		 * Restore Memory Controller settings as it loses state
289102e4087SVarun Wadekar 		 * during system suspend.
29008438e24SVarun Wadekar 		 */
291102e4087SVarun Wadekar 		tegra_memctrl_restore_settings();
29208438e24SVarun Wadekar 
29308438e24SVarun Wadekar 		/*
29408438e24SVarun Wadekar 		 * Security configuration to allow DRAM/device access.
29508438e24SVarun Wadekar 		 */
29608438e24SVarun Wadekar 		plat_params = bl31_get_plat_params();
297e0d4158cSVarun Wadekar 		tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
298b36aea5aSAnthony Zhou 			(uint32_t)plat_params->tzdram_size);
299207680c6SVarun Wadekar 
300207680c6SVarun Wadekar 		/*
301207680c6SVarun Wadekar 		 * Set up the TZRAM memory aperture to allow only secure world
302207680c6SVarun Wadekar 		 * access
303207680c6SVarun Wadekar 		 */
304207680c6SVarun Wadekar 		tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
30508438e24SVarun Wadekar 	}
30608438e24SVarun Wadekar 
30708438e24SVarun Wadekar 	/*
30808438e24SVarun Wadekar 	 * Reset hardware settings.
30908438e24SVarun Wadekar 	 */
310b36aea5aSAnthony Zhou 	(void)tegra_soc_pwr_domain_on_finish(target_state);
31108438e24SVarun Wadekar }
31208438e24SVarun Wadekar 
31308438e24SVarun Wadekar /*******************************************************************************
31471cb26eaSVarun Wadekar  * Handler called when a power domain has just been powered on after
31571cb26eaSVarun Wadekar  * having been suspended earlier. The target_state encodes the low power state
31671cb26eaSVarun Wadekar  * that each level has woken up from.
31708438e24SVarun Wadekar  ******************************************************************************/
31871cb26eaSVarun Wadekar void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
31908438e24SVarun Wadekar {
32071cb26eaSVarun Wadekar 	tegra_pwr_domain_on_finish(target_state);
32108438e24SVarun Wadekar }
32208438e24SVarun Wadekar 
32308438e24SVarun Wadekar /*******************************************************************************
32408438e24SVarun Wadekar  * Handler called when the system wants to be powered off
32508438e24SVarun Wadekar  ******************************************************************************/
32608438e24SVarun Wadekar __dead2 void tegra_system_off(void)
32708438e24SVarun Wadekar {
32831a4957cSVarun Wadekar 	INFO("Powering down system...\n");
32931a4957cSVarun Wadekar 
33031a4957cSVarun Wadekar 	tegra_soc_prepare_system_off();
33108438e24SVarun Wadekar }
33208438e24SVarun Wadekar 
33308438e24SVarun Wadekar /*******************************************************************************
33408438e24SVarun Wadekar  * Handler called when the system wants to be restarted.
33508438e24SVarun Wadekar  ******************************************************************************/
33608438e24SVarun Wadekar __dead2 void tegra_system_reset(void)
33708438e24SVarun Wadekar {
33831a4957cSVarun Wadekar 	INFO("Restarting system...\n");
33931a4957cSVarun Wadekar 
3403b40f993SVarun Wadekar 	/* per-SoC system reset handler */
341b36aea5aSAnthony Zhou 	(void)tegra_soc_prepare_system_reset();
3423b40f993SVarun Wadekar 
34308438e24SVarun Wadekar 	/*
34408438e24SVarun Wadekar 	 * Program the PMC in order to restart the system.
34508438e24SVarun Wadekar 	 */
34608438e24SVarun Wadekar 	tegra_pmc_system_reset();
34708438e24SVarun Wadekar }
34808438e24SVarun Wadekar 
34908438e24SVarun Wadekar /*******************************************************************************
35071cb26eaSVarun Wadekar  * Handler called to check the validity of the power state parameter.
35171cb26eaSVarun Wadekar  ******************************************************************************/
352b36aea5aSAnthony Zhou int32_t tegra_validate_power_state(uint32_t power_state,
35371cb26eaSVarun Wadekar 				   psci_power_state_t *req_state)
35471cb26eaSVarun Wadekar {
3554c994002SAnthony Zhou 	assert(req_state != NULL);
35671cb26eaSVarun Wadekar 
35771cb26eaSVarun Wadekar 	return tegra_soc_validate_power_state(power_state, req_state);
35871cb26eaSVarun Wadekar }
35971cb26eaSVarun Wadekar 
36071cb26eaSVarun Wadekar /*******************************************************************************
36171cb26eaSVarun Wadekar  * Platform handler called to check the validity of the non secure entrypoint.
36271cb26eaSVarun Wadekar  ******************************************************************************/
363b36aea5aSAnthony Zhou int32_t tegra_validate_ns_entrypoint(uintptr_t entrypoint)
36471cb26eaSVarun Wadekar {
365b36aea5aSAnthony Zhou 	int32_t ret = PSCI_E_INVALID_ADDRESS;
366b36aea5aSAnthony Zhou 
36771cb26eaSVarun Wadekar 	/*
36871cb26eaSVarun Wadekar 	 * Check if the non secure entrypoint lies within the non
36971cb26eaSVarun Wadekar 	 * secure DRAM.
37071cb26eaSVarun Wadekar 	 */
371b36aea5aSAnthony Zhou 	if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END)) {
372b36aea5aSAnthony Zhou 		ret = PSCI_E_SUCCESS;
373b36aea5aSAnthony Zhou 	}
37471cb26eaSVarun Wadekar 
375b36aea5aSAnthony Zhou 	return ret;
37671cb26eaSVarun Wadekar }
37771cb26eaSVarun Wadekar 
37871cb26eaSVarun Wadekar /*******************************************************************************
37908438e24SVarun Wadekar  * Export the platform handlers to enable psci to invoke them
38008438e24SVarun Wadekar  ******************************************************************************/
38171cb26eaSVarun Wadekar static const plat_psci_ops_t tegra_plat_psci_ops = {
38271cb26eaSVarun Wadekar 	.cpu_standby			= tegra_cpu_standby,
38371cb26eaSVarun Wadekar 	.pwr_domain_on			= tegra_pwr_domain_on,
38471cb26eaSVarun Wadekar 	.pwr_domain_off			= tegra_pwr_domain_off,
385cb95a19aSVarun Wadekar 	.pwr_domain_suspend_pwrdown_early = tegra_pwr_domain_suspend_pwrdown_early,
38671cb26eaSVarun Wadekar 	.pwr_domain_suspend		= tegra_pwr_domain_suspend,
38771cb26eaSVarun Wadekar 	.pwr_domain_on_finish		= tegra_pwr_domain_on_finish,
38871cb26eaSVarun Wadekar 	.pwr_domain_suspend_finish	= tegra_pwr_domain_suspend_finish,
38926c0d9b2SVarun Wadekar 	.pwr_domain_pwr_down_wfi	= tegra_pwr_domain_power_down_wfi,
39008438e24SVarun Wadekar 	.system_off			= tegra_system_off,
39108438e24SVarun Wadekar 	.system_reset			= tegra_system_reset,
39294c672e7SVarun Wadekar 	.validate_power_state		= tegra_validate_power_state,
39371cb26eaSVarun Wadekar 	.validate_ns_entrypoint		= tegra_validate_ns_entrypoint,
39471cb26eaSVarun Wadekar 	.get_sys_suspend_power_state	= tegra_get_sys_suspend_power_state,
39508438e24SVarun Wadekar };
39608438e24SVarun Wadekar 
39708438e24SVarun Wadekar /*******************************************************************************
39871cb26eaSVarun Wadekar  * Export the platform specific power ops and initialize Power Controller
39908438e24SVarun Wadekar  ******************************************************************************/
40071cb26eaSVarun Wadekar int plat_setup_psci_ops(uintptr_t sec_entrypoint,
40171cb26eaSVarun Wadekar 			const plat_psci_ops_t **psci_ops)
40208438e24SVarun Wadekar {
40371cb26eaSVarun Wadekar 	psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
40471cb26eaSVarun Wadekar 
40571cb26eaSVarun Wadekar 	/*
40671cb26eaSVarun Wadekar 	 * Flush entrypoint variable to PoC since it will be
40771cb26eaSVarun Wadekar 	 * accessed after a reset with the caches turned off.
40871cb26eaSVarun Wadekar 	 */
40971cb26eaSVarun Wadekar 	tegra_sec_entry_point = sec_entrypoint;
41071cb26eaSVarun Wadekar 	flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
41171cb26eaSVarun Wadekar 
41208438e24SVarun Wadekar 	/*
41308438e24SVarun Wadekar 	 * Reset hardware settings.
41408438e24SVarun Wadekar 	 */
415b36aea5aSAnthony Zhou 	(void)tegra_soc_pwr_domain_on_finish(&target_state);
41608438e24SVarun Wadekar 
41708438e24SVarun Wadekar 	/*
41871cb26eaSVarun Wadekar 	 * Initialize PSCI ops struct
41908438e24SVarun Wadekar 	 */
42071cb26eaSVarun Wadekar 	*psci_ops = &tegra_plat_psci_ops;
42108438e24SVarun Wadekar 
42208438e24SVarun Wadekar 	return 0;
42308438e24SVarun Wadekar }
4242693f1dbSVarun Wadekar 
4252693f1dbSVarun Wadekar /*******************************************************************************
4262693f1dbSVarun Wadekar  * Platform handler to calculate the proper target power level at the
4272693f1dbSVarun Wadekar  * specified affinity level
4282693f1dbSVarun Wadekar  ******************************************************************************/
4292693f1dbSVarun Wadekar plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
4302693f1dbSVarun Wadekar 					     const plat_local_state_t *states,
4312693f1dbSVarun Wadekar 					     unsigned int ncpu)
4322693f1dbSVarun Wadekar {
433a7cd0953SVarun Wadekar 	return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
4342693f1dbSVarun Wadekar }
435