xref: /arm-trusted-firmware/plat/nvidia/tegra/common/tegra_pm.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  * Copyright (c) 2020-2023, NVIDIA Corporation. All rights reserved.
4*91f16700Schasinglulu  *
5*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <assert.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <platform_def.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu #include <arch_helpers.h>
13*91f16700Schasinglulu #include <common/bl_common.h>
14*91f16700Schasinglulu #include <common/debug.h>
15*91f16700Schasinglulu #include <context.h>
16*91f16700Schasinglulu #include <drivers/console.h>
17*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
18*91f16700Schasinglulu #include <lib/mmio.h>
19*91f16700Schasinglulu #include <lib/psci/psci.h>
20*91f16700Schasinglulu #include <plat/common/platform.h>
21*91f16700Schasinglulu 
22*91f16700Schasinglulu #include <memctrl.h>
23*91f16700Schasinglulu #include <pmc.h>
24*91f16700Schasinglulu #include <tegra_def.h>
25*91f16700Schasinglulu #include <tegra_platform.h>
26*91f16700Schasinglulu #include <tegra_private.h>
27*91f16700Schasinglulu 
28*91f16700Schasinglulu extern uint64_t tegra_bl31_phys_base;
29*91f16700Schasinglulu extern uint64_t tegra_sec_entry_point;
30*91f16700Schasinglulu 
31*91f16700Schasinglulu /*******************************************************************************
32*91f16700Schasinglulu  * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
33*91f16700Schasinglulu  * call to get the `power_state` parameter. This allows the platform to encode
34*91f16700Schasinglulu  * the appropriate State-ID field within the `power_state` parameter which can
35*91f16700Schasinglulu  * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
36*91f16700Schasinglulu ******************************************************************************/
37*91f16700Schasinglulu static void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
38*91f16700Schasinglulu {
39*91f16700Schasinglulu 	/* all affinities use system suspend state id */
40*91f16700Schasinglulu 	for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) {
41*91f16700Schasinglulu 		req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
42*91f16700Schasinglulu 	}
43*91f16700Schasinglulu }
44*91f16700Schasinglulu 
45*91f16700Schasinglulu /*******************************************************************************
46*91f16700Schasinglulu  * Handler called when an affinity instance is about to enter standby.
47*91f16700Schasinglulu  ******************************************************************************/
48*91f16700Schasinglulu static void tegra_cpu_standby(plat_local_state_t cpu_state)
49*91f16700Schasinglulu {
50*91f16700Schasinglulu 	u_register_t saved_scr_el3;
51*91f16700Schasinglulu 
52*91f16700Schasinglulu 	(void)cpu_state;
53*91f16700Schasinglulu 
54*91f16700Schasinglulu 	/* Tegra SoC specific handler */
55*91f16700Schasinglulu 	if (tegra_soc_cpu_standby(cpu_state) != PSCI_E_SUCCESS)
56*91f16700Schasinglulu 		ERROR("%s failed\n", __func__);
57*91f16700Schasinglulu 
58*91f16700Schasinglulu 	saved_scr_el3 = read_scr_el3();
59*91f16700Schasinglulu 
60*91f16700Schasinglulu 	/*
61*91f16700Schasinglulu 	 * As per ARM ARM D1.17.2, any physical IRQ interrupt received by the
62*91f16700Schasinglulu 	 * PE will be treated as a wake-up event, if SCR_EL3.IRQ is set to '1',
63*91f16700Schasinglulu 	 * irrespective of the value of the PSTATE.I bit value.
64*91f16700Schasinglulu 	 */
65*91f16700Schasinglulu 	write_scr_el3(saved_scr_el3 | SCR_IRQ_BIT);
66*91f16700Schasinglulu 
67*91f16700Schasinglulu 	/*
68*91f16700Schasinglulu 	 * Enter standby state
69*91f16700Schasinglulu 	 *
70*91f16700Schasinglulu 	 * dsb & isb is good practice before using wfi to enter low power states
71*91f16700Schasinglulu 	 */
72*91f16700Schasinglulu 	dsb();
73*91f16700Schasinglulu 	isb();
74*91f16700Schasinglulu 	wfi();
75*91f16700Schasinglulu 
76*91f16700Schasinglulu 	/*
77*91f16700Schasinglulu 	 * Restore saved scr_el3 that has IRQ bit cleared as we don't want EL3
78*91f16700Schasinglulu 	 * handling any further interrupts
79*91f16700Schasinglulu 	 */
80*91f16700Schasinglulu 	write_scr_el3(saved_scr_el3);
81*91f16700Schasinglulu }
82*91f16700Schasinglulu 
83*91f16700Schasinglulu /*******************************************************************************
84*91f16700Schasinglulu  * Handler called when an affinity instance is about to be turned on. The
85*91f16700Schasinglulu  * level and mpidr determine the affinity instance.
86*91f16700Schasinglulu  ******************************************************************************/
87*91f16700Schasinglulu static int32_t tegra_pwr_domain_on(u_register_t mpidr)
88*91f16700Schasinglulu {
89*91f16700Schasinglulu 	return tegra_soc_pwr_domain_on(mpidr);
90*91f16700Schasinglulu }
91*91f16700Schasinglulu 
92*91f16700Schasinglulu /*******************************************************************************
93*91f16700Schasinglulu  * Handler called when a power domain is about to be turned off. The
94*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
95*91f16700Schasinglulu  * Return error if CPU off sequence is not allowed for the current core.
96*91f16700Schasinglulu  ******************************************************************************/
97*91f16700Schasinglulu static int tegra_pwr_domain_off_early(const psci_power_state_t *target_state)
98*91f16700Schasinglulu {
99*91f16700Schasinglulu 	return tegra_soc_pwr_domain_off_early(target_state);
100*91f16700Schasinglulu }
101*91f16700Schasinglulu 
102*91f16700Schasinglulu /*******************************************************************************
103*91f16700Schasinglulu  * Handler called when a power domain is about to be turned off. The
104*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
105*91f16700Schasinglulu  ******************************************************************************/
106*91f16700Schasinglulu static void tegra_pwr_domain_off(const psci_power_state_t *target_state)
107*91f16700Schasinglulu {
108*91f16700Schasinglulu 	(void)tegra_soc_pwr_domain_off(target_state);
109*91f16700Schasinglulu 
110*91f16700Schasinglulu 	/* disable GICC */
111*91f16700Schasinglulu 	tegra_gic_cpuif_deactivate();
112*91f16700Schasinglulu }
113*91f16700Schasinglulu 
114*91f16700Schasinglulu /*******************************************************************************
115*91f16700Schasinglulu  * Handler called when a power domain is about to be suspended. The
116*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
117*91f16700Schasinglulu  * This handler is called with SMP and data cache enabled, when
118*91f16700Schasinglulu  * HW_ASSISTED_COHERENCY = 0
119*91f16700Schasinglulu  ******************************************************************************/
120*91f16700Schasinglulu void tegra_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
121*91f16700Schasinglulu {
122*91f16700Schasinglulu 	tegra_soc_pwr_domain_suspend_pwrdown_early(target_state);
123*91f16700Schasinglulu }
124*91f16700Schasinglulu 
125*91f16700Schasinglulu /*******************************************************************************
126*91f16700Schasinglulu  * Handler called when a power domain is about to be suspended. The
127*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
128*91f16700Schasinglulu  ******************************************************************************/
129*91f16700Schasinglulu static void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
130*91f16700Schasinglulu {
131*91f16700Schasinglulu 	(void)tegra_soc_pwr_domain_suspend(target_state);
132*91f16700Schasinglulu 
133*91f16700Schasinglulu 	/* disable GICC */
134*91f16700Schasinglulu 	tegra_gic_cpuif_deactivate();
135*91f16700Schasinglulu }
136*91f16700Schasinglulu 
137*91f16700Schasinglulu /*******************************************************************************
138*91f16700Schasinglulu  * Handler called at the end of the power domain suspend sequence. The
139*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
140*91f16700Schasinglulu  ******************************************************************************/
141*91f16700Schasinglulu static __dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
142*91f16700Schasinglulu 					     *target_state)
143*91f16700Schasinglulu {
144*91f16700Schasinglulu 	/* call the chip's power down handler */
145*91f16700Schasinglulu 	(void)tegra_soc_pwr_domain_power_down_wfi(target_state);
146*91f16700Schasinglulu 
147*91f16700Schasinglulu 	/* Disable console if we are entering deep sleep. */
148*91f16700Schasinglulu 	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
149*91f16700Schasinglulu 			PSTATE_ID_SOC_POWERDN) {
150*91f16700Schasinglulu 		INFO("%s: complete. Entering System Suspend...\n", __func__);
151*91f16700Schasinglulu 		console_flush();
152*91f16700Schasinglulu 		console_switch_state(0);
153*91f16700Schasinglulu 	}
154*91f16700Schasinglulu 
155*91f16700Schasinglulu 	wfi();
156*91f16700Schasinglulu 	panic();
157*91f16700Schasinglulu }
158*91f16700Schasinglulu 
159*91f16700Schasinglulu /*******************************************************************************
160*91f16700Schasinglulu  * Handler called when a power domain has just been powered on after
161*91f16700Schasinglulu  * being turned off earlier. The target_state encodes the low power state that
162*91f16700Schasinglulu  * each level has woken up from.
163*91f16700Schasinglulu  ******************************************************************************/
164*91f16700Schasinglulu static void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
165*91f16700Schasinglulu {
166*91f16700Schasinglulu 	const plat_params_from_bl2_t *plat_params;
167*91f16700Schasinglulu 
168*91f16700Schasinglulu 	/*
169*91f16700Schasinglulu 	 * Check if we are exiting from deep sleep.
170*91f16700Schasinglulu 	 */
171*91f16700Schasinglulu 	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
172*91f16700Schasinglulu 			PSTATE_ID_SOC_POWERDN) {
173*91f16700Schasinglulu 
174*91f16700Schasinglulu 		/*
175*91f16700Schasinglulu 		 * On entering System Suspend state, the GIC loses power
176*91f16700Schasinglulu 		 * completely. Initialize the GIC global distributor and
177*91f16700Schasinglulu 		 * GIC cpu interfaces.
178*91f16700Schasinglulu 		 */
179*91f16700Schasinglulu 		tegra_gic_init();
180*91f16700Schasinglulu 
181*91f16700Schasinglulu 		/* Restart console output. */
182*91f16700Schasinglulu 		console_switch_state(CONSOLE_FLAG_RUNTIME);
183*91f16700Schasinglulu 
184*91f16700Schasinglulu 		/*
185*91f16700Schasinglulu 		 * Restore Memory Controller settings as it loses state
186*91f16700Schasinglulu 		 * during system suspend.
187*91f16700Schasinglulu 		 */
188*91f16700Schasinglulu 		tegra_memctrl_restore_settings();
189*91f16700Schasinglulu 
190*91f16700Schasinglulu 		/*
191*91f16700Schasinglulu 		 * Security configuration to allow DRAM/device access.
192*91f16700Schasinglulu 		 */
193*91f16700Schasinglulu 		plat_params = bl31_get_plat_params();
194*91f16700Schasinglulu 		tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
195*91f16700Schasinglulu 			(uint32_t)plat_params->tzdram_size);
196*91f16700Schasinglulu 
197*91f16700Schasinglulu 	} else {
198*91f16700Schasinglulu 		/*
199*91f16700Schasinglulu 		 * Initialize the GIC cpu and distributor interfaces
200*91f16700Schasinglulu 		 */
201*91f16700Schasinglulu 		tegra_gic_pcpu_init();
202*91f16700Schasinglulu 	}
203*91f16700Schasinglulu 
204*91f16700Schasinglulu 	/*
205*91f16700Schasinglulu 	 * Reset hardware settings.
206*91f16700Schasinglulu 	 */
207*91f16700Schasinglulu 	(void)tegra_soc_pwr_domain_on_finish(target_state);
208*91f16700Schasinglulu }
209*91f16700Schasinglulu 
210*91f16700Schasinglulu /*******************************************************************************
211*91f16700Schasinglulu  * Handler called when a power domain has just been powered on after
212*91f16700Schasinglulu  * having been suspended earlier. The target_state encodes the low power state
213*91f16700Schasinglulu  * that each level has woken up from.
214*91f16700Schasinglulu  ******************************************************************************/
215*91f16700Schasinglulu static void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
216*91f16700Schasinglulu {
217*91f16700Schasinglulu 	tegra_pwr_domain_on_finish(target_state);
218*91f16700Schasinglulu }
219*91f16700Schasinglulu 
220*91f16700Schasinglulu /*******************************************************************************
221*91f16700Schasinglulu  * Handler called when the system wants to be powered off
222*91f16700Schasinglulu  ******************************************************************************/
223*91f16700Schasinglulu static __dead2 void tegra_system_off(void)
224*91f16700Schasinglulu {
225*91f16700Schasinglulu 	INFO("Powering down system...\n");
226*91f16700Schasinglulu 
227*91f16700Schasinglulu 	tegra_soc_prepare_system_off();
228*91f16700Schasinglulu }
229*91f16700Schasinglulu 
230*91f16700Schasinglulu /*******************************************************************************
231*91f16700Schasinglulu  * Handler called when the system wants to be restarted.
232*91f16700Schasinglulu  ******************************************************************************/
233*91f16700Schasinglulu static __dead2 void tegra_system_reset(void)
234*91f16700Schasinglulu {
235*91f16700Schasinglulu 	INFO("Restarting system...\n");
236*91f16700Schasinglulu 
237*91f16700Schasinglulu 	/* per-SoC system reset handler */
238*91f16700Schasinglulu 	(void)tegra_soc_prepare_system_reset();
239*91f16700Schasinglulu 
240*91f16700Schasinglulu 	/* wait for the system to reset */
241*91f16700Schasinglulu 	for (;;) {
242*91f16700Schasinglulu 		;
243*91f16700Schasinglulu 	}
244*91f16700Schasinglulu }
245*91f16700Schasinglulu 
246*91f16700Schasinglulu /*******************************************************************************
247*91f16700Schasinglulu  * Handler called to check the validity of the power state parameter.
248*91f16700Schasinglulu  ******************************************************************************/
249*91f16700Schasinglulu static int32_t tegra_validate_power_state(uint32_t power_state,
250*91f16700Schasinglulu 				   psci_power_state_t *req_state)
251*91f16700Schasinglulu {
252*91f16700Schasinglulu 	assert(req_state != NULL);
253*91f16700Schasinglulu 
254*91f16700Schasinglulu 	return tegra_soc_validate_power_state(power_state, req_state);
255*91f16700Schasinglulu }
256*91f16700Schasinglulu 
257*91f16700Schasinglulu /*******************************************************************************
258*91f16700Schasinglulu  * Platform handler called to check the validity of the non secure entrypoint.
259*91f16700Schasinglulu  ******************************************************************************/
260*91f16700Schasinglulu static int32_t tegra_validate_ns_entrypoint(uintptr_t entrypoint)
261*91f16700Schasinglulu {
262*91f16700Schasinglulu 	int32_t ret = PSCI_E_INVALID_ADDRESS;
263*91f16700Schasinglulu 
264*91f16700Schasinglulu 	/*
265*91f16700Schasinglulu 	 * Check if the non secure entrypoint lies within the non
266*91f16700Schasinglulu 	 * secure DRAM.
267*91f16700Schasinglulu 	 */
268*91f16700Schasinglulu 	if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END)) {
269*91f16700Schasinglulu 		ret = PSCI_E_SUCCESS;
270*91f16700Schasinglulu 	}
271*91f16700Schasinglulu 
272*91f16700Schasinglulu 	return ret;
273*91f16700Schasinglulu }
274*91f16700Schasinglulu 
275*91f16700Schasinglulu /*******************************************************************************
276*91f16700Schasinglulu  * Export the platform handlers to enable psci to invoke them
277*91f16700Schasinglulu  ******************************************************************************/
278*91f16700Schasinglulu static plat_psci_ops_t tegra_plat_psci_ops = {
279*91f16700Schasinglulu 	.cpu_standby			= tegra_cpu_standby,
280*91f16700Schasinglulu 	.pwr_domain_on			= tegra_pwr_domain_on,
281*91f16700Schasinglulu 	.pwr_domain_off_early		= tegra_pwr_domain_off_early,
282*91f16700Schasinglulu 	.pwr_domain_off			= tegra_pwr_domain_off,
283*91f16700Schasinglulu 	.pwr_domain_suspend_pwrdown_early = tegra_pwr_domain_suspend_pwrdown_early,
284*91f16700Schasinglulu 	.pwr_domain_suspend		= tegra_pwr_domain_suspend,
285*91f16700Schasinglulu 	.pwr_domain_on_finish		= tegra_pwr_domain_on_finish,
286*91f16700Schasinglulu 	.pwr_domain_suspend_finish	= tegra_pwr_domain_suspend_finish,
287*91f16700Schasinglulu 	.pwr_domain_pwr_down_wfi	= tegra_pwr_domain_power_down_wfi,
288*91f16700Schasinglulu 	.system_off			= tegra_system_off,
289*91f16700Schasinglulu 	.system_reset			= tegra_system_reset,
290*91f16700Schasinglulu 	.validate_power_state		= tegra_validate_power_state,
291*91f16700Schasinglulu 	.validate_ns_entrypoint		= tegra_validate_ns_entrypoint,
292*91f16700Schasinglulu 	.get_sys_suspend_power_state	= tegra_get_sys_suspend_power_state,
293*91f16700Schasinglulu };
294*91f16700Schasinglulu 
295*91f16700Schasinglulu /*******************************************************************************
296*91f16700Schasinglulu  * Export the platform specific power ops and initialize Power Controller
297*91f16700Schasinglulu  ******************************************************************************/
298*91f16700Schasinglulu int plat_setup_psci_ops(uintptr_t sec_entrypoint,
299*91f16700Schasinglulu 			const plat_psci_ops_t **psci_ops)
300*91f16700Schasinglulu {
301*91f16700Schasinglulu 	psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
302*91f16700Schasinglulu 
303*91f16700Schasinglulu 	/*
304*91f16700Schasinglulu 	 * Flush entrypoint variable to PoC since it will be
305*91f16700Schasinglulu 	 * accessed after a reset with the caches turned off.
306*91f16700Schasinglulu 	 */
307*91f16700Schasinglulu 	tegra_sec_entry_point = sec_entrypoint;
308*91f16700Schasinglulu 	flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
309*91f16700Schasinglulu 
310*91f16700Schasinglulu 	/*
311*91f16700Schasinglulu 	 * Reset hardware settings.
312*91f16700Schasinglulu 	 */
313*91f16700Schasinglulu 	(void)tegra_soc_pwr_domain_on_finish(&target_state);
314*91f16700Schasinglulu 
315*91f16700Schasinglulu 	/*
316*91f16700Schasinglulu 	 * Disable System Suspend if the platform does not
317*91f16700Schasinglulu 	 * support it
318*91f16700Schasinglulu 	 */
319*91f16700Schasinglulu 	if (!plat_supports_system_suspend()) {
320*91f16700Schasinglulu 		tegra_plat_psci_ops.get_sys_suspend_power_state = NULL;
321*91f16700Schasinglulu 	}
322*91f16700Schasinglulu 
323*91f16700Schasinglulu 	/*
324*91f16700Schasinglulu 	 * Initialize PSCI ops struct
325*91f16700Schasinglulu 	 */
326*91f16700Schasinglulu 	*psci_ops = &tegra_plat_psci_ops;
327*91f16700Schasinglulu 
328*91f16700Schasinglulu 	return 0;
329*91f16700Schasinglulu }
330*91f16700Schasinglulu 
331*91f16700Schasinglulu /*******************************************************************************
332*91f16700Schasinglulu  * Platform handler to calculate the proper target power level at the
333*91f16700Schasinglulu  * specified affinity level
334*91f16700Schasinglulu  ******************************************************************************/
335*91f16700Schasinglulu plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
336*91f16700Schasinglulu 					     const plat_local_state_t *states,
337*91f16700Schasinglulu 					     unsigned int ncpu)
338*91f16700Schasinglulu {
339*91f16700Schasinglulu 	return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
340*91f16700Schasinglulu }
341