xref: /aosp_15_r20/external/arm-trusted-firmware/plat/mediatek/mt8173/plat_pm.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <errno.h>
9*54fd6939SJiyong Park 
10*54fd6939SJiyong Park #include <arch_helpers.h>
11*54fd6939SJiyong Park #include <common/debug.h>
12*54fd6939SJiyong Park #include <drivers/arm/cci.h>
13*54fd6939SJiyong Park #include <drivers/arm/gicv2.h>
14*54fd6939SJiyong Park #include <drivers/ti/uart/uart_16550.h>
15*54fd6939SJiyong Park #include <lib/bakery_lock.h>
16*54fd6939SJiyong Park #include <lib/mmio.h>
17*54fd6939SJiyong Park #include <lib/psci/psci.h>
18*54fd6939SJiyong Park #include <plat/arm/common/plat_arm.h>
19*54fd6939SJiyong Park 
20*54fd6939SJiyong Park #include <mcucfg.h>
21*54fd6939SJiyong Park #include <mt8173_def.h>
22*54fd6939SJiyong Park #include <mt_cpuxgpt.h> /* generic_timer_backup() */
23*54fd6939SJiyong Park #include <plat_private.h>
24*54fd6939SJiyong Park #include <power_tracer.h>
25*54fd6939SJiyong Park #include <rtc.h>
26*54fd6939SJiyong Park #include <scu.h>
27*54fd6939SJiyong Park #include <spm_hotplug.h>
28*54fd6939SJiyong Park #include <spm_mcdi.h>
29*54fd6939SJiyong Park #include <spm_suspend.h>
30*54fd6939SJiyong Park #include <wdt.h>
31*54fd6939SJiyong Park 
32*54fd6939SJiyong Park #define MTK_PWR_LVL0	0
33*54fd6939SJiyong Park #define MTK_PWR_LVL1	1
34*54fd6939SJiyong Park #define MTK_PWR_LVL2	2
35*54fd6939SJiyong Park 
36*54fd6939SJiyong Park /* Macros to read the MTK power domain state */
37*54fd6939SJiyong Park #define MTK_CORE_PWR_STATE(state)	(state)->pwr_domain_state[MTK_PWR_LVL0]
38*54fd6939SJiyong Park #define MTK_CLUSTER_PWR_STATE(state)	(state)->pwr_domain_state[MTK_PWR_LVL1]
39*54fd6939SJiyong Park #define MTK_SYSTEM_PWR_STATE(state)	((PLAT_MAX_PWR_LVL > MTK_PWR_LVL1) ?\
40*54fd6939SJiyong Park 			(state)->pwr_domain_state[MTK_PWR_LVL2] : 0)
41*54fd6939SJiyong Park 
42*54fd6939SJiyong Park #if PSCI_EXTENDED_STATE_ID
43*54fd6939SJiyong Park /*
44*54fd6939SJiyong Park  *  The table storing the valid idle power states. Ensure that the
45*54fd6939SJiyong Park  *  array entries are populated in ascending order of state-id to
46*54fd6939SJiyong Park  *  enable us to use binary search during power state validation.
47*54fd6939SJiyong Park  *  The table must be terminated by a NULL entry.
48*54fd6939SJiyong Park  */
49*54fd6939SJiyong Park const unsigned int mtk_pm_idle_states[] = {
50*54fd6939SJiyong Park 	/* State-id - 0x001 */
51*54fd6939SJiyong Park 	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_RUN, MTK_LOCAL_STATE_RUN,
52*54fd6939SJiyong Park 		MTK_LOCAL_STATE_RET, MTK_PWR_LVL0, PSTATE_TYPE_STANDBY),
53*54fd6939SJiyong Park 	/* State-id - 0x002 */
54*54fd6939SJiyong Park 	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_RUN, MTK_LOCAL_STATE_RUN,
55*54fd6939SJiyong Park 		MTK_LOCAL_STATE_OFF, MTK_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
56*54fd6939SJiyong Park 	/* State-id - 0x022 */
57*54fd6939SJiyong Park 	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_RUN, MTK_LOCAL_STATE_OFF,
58*54fd6939SJiyong Park 		MTK_LOCAL_STATE_OFF, MTK_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
59*54fd6939SJiyong Park #if PLAT_MAX_PWR_LVL > MTK_PWR_LVL1
60*54fd6939SJiyong Park 	/* State-id - 0x222 */
61*54fd6939SJiyong Park 	mtk_make_pwrstate_lvl2(MTK_LOCAL_STATE_OFF, MTK_LOCAL_STATE_OFF,
62*54fd6939SJiyong Park 		MTK_LOCAL_STATE_OFF, MTK_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
63*54fd6939SJiyong Park #endif
64*54fd6939SJiyong Park 	0,
65*54fd6939SJiyong Park };
66*54fd6939SJiyong Park #endif
67*54fd6939SJiyong Park 
68*54fd6939SJiyong Park struct core_context {
69*54fd6939SJiyong Park 	unsigned long timer_data[8];
70*54fd6939SJiyong Park 	unsigned int count;
71*54fd6939SJiyong Park 	unsigned int rst;
72*54fd6939SJiyong Park 	unsigned int abt;
73*54fd6939SJiyong Park 	unsigned int brk;
74*54fd6939SJiyong Park };
75*54fd6939SJiyong Park 
76*54fd6939SJiyong Park struct cluster_context {
77*54fd6939SJiyong Park 	struct core_context core[PLATFORM_MAX_CPUS_PER_CLUSTER];
78*54fd6939SJiyong Park };
79*54fd6939SJiyong Park 
80*54fd6939SJiyong Park /*
81*54fd6939SJiyong Park  * Top level structure to hold the complete context of a multi cluster system
82*54fd6939SJiyong Park  */
83*54fd6939SJiyong Park struct system_context {
84*54fd6939SJiyong Park 	struct cluster_context cluster[PLATFORM_CLUSTER_COUNT];
85*54fd6939SJiyong Park };
86*54fd6939SJiyong Park 
87*54fd6939SJiyong Park /*
88*54fd6939SJiyong Park  * Top level structure which encapsulates the context of the entire system
89*54fd6939SJiyong Park  */
90*54fd6939SJiyong Park static struct system_context dormant_data[1];
91*54fd6939SJiyong Park 
system_cluster(struct system_context * system,uint32_t clusterid)92*54fd6939SJiyong Park static inline struct cluster_context *system_cluster(
93*54fd6939SJiyong Park 						struct system_context *system,
94*54fd6939SJiyong Park 						uint32_t clusterid)
95*54fd6939SJiyong Park {
96*54fd6939SJiyong Park 	return &system->cluster[clusterid];
97*54fd6939SJiyong Park }
98*54fd6939SJiyong Park 
cluster_core(struct cluster_context * cluster,uint32_t cpuid)99*54fd6939SJiyong Park static inline struct core_context *cluster_core(struct cluster_context *cluster,
100*54fd6939SJiyong Park 						uint32_t cpuid)
101*54fd6939SJiyong Park {
102*54fd6939SJiyong Park 	return &cluster->core[cpuid];
103*54fd6939SJiyong Park }
104*54fd6939SJiyong Park 
get_cluster_data(unsigned long mpidr)105*54fd6939SJiyong Park static struct cluster_context *get_cluster_data(unsigned long mpidr)
106*54fd6939SJiyong Park {
107*54fd6939SJiyong Park 	uint32_t clusterid;
108*54fd6939SJiyong Park 
109*54fd6939SJiyong Park 	clusterid = (mpidr & MPIDR_CLUSTER_MASK) >> MPIDR_AFFINITY_BITS;
110*54fd6939SJiyong Park 
111*54fd6939SJiyong Park 	return system_cluster(dormant_data, clusterid);
112*54fd6939SJiyong Park }
113*54fd6939SJiyong Park 
get_core_data(unsigned long mpidr)114*54fd6939SJiyong Park static struct core_context *get_core_data(unsigned long mpidr)
115*54fd6939SJiyong Park {
116*54fd6939SJiyong Park 	struct cluster_context *cluster;
117*54fd6939SJiyong Park 	uint32_t cpuid;
118*54fd6939SJiyong Park 
119*54fd6939SJiyong Park 	cluster = get_cluster_data(mpidr);
120*54fd6939SJiyong Park 	cpuid = mpidr & MPIDR_CPU_MASK;
121*54fd6939SJiyong Park 
122*54fd6939SJiyong Park 	return cluster_core(cluster, cpuid);
123*54fd6939SJiyong Park }
124*54fd6939SJiyong Park 
mt_save_generic_timer(unsigned long * container)125*54fd6939SJiyong Park static void mt_save_generic_timer(unsigned long *container)
126*54fd6939SJiyong Park {
127*54fd6939SJiyong Park 	uint64_t ctl;
128*54fd6939SJiyong Park 	uint64_t val;
129*54fd6939SJiyong Park 
130*54fd6939SJiyong Park 	__asm__ volatile("mrs	%x0, cntkctl_el1\n\t"
131*54fd6939SJiyong Park 			 "mrs	%x1, cntp_cval_el0\n\t"
132*54fd6939SJiyong Park 			 "stp	%x0, %x1, [%2, #0]"
133*54fd6939SJiyong Park 			 : "=&r" (ctl), "=&r" (val)
134*54fd6939SJiyong Park 			 : "r" (container)
135*54fd6939SJiyong Park 			 : "memory");
136*54fd6939SJiyong Park 
137*54fd6939SJiyong Park 	__asm__ volatile("mrs	%x0, cntp_tval_el0\n\t"
138*54fd6939SJiyong Park 			 "mrs	%x1, cntp_ctl_el0\n\t"
139*54fd6939SJiyong Park 			 "stp	%x0, %x1, [%2, #16]"
140*54fd6939SJiyong Park 			 : "=&r" (val), "=&r" (ctl)
141*54fd6939SJiyong Park 			 : "r" (container)
142*54fd6939SJiyong Park 			 : "memory");
143*54fd6939SJiyong Park 
144*54fd6939SJiyong Park 	__asm__ volatile("mrs	%x0, cntv_tval_el0\n\t"
145*54fd6939SJiyong Park 			 "mrs	%x1, cntv_ctl_el0\n\t"
146*54fd6939SJiyong Park 			 "stp	%x0, %x1, [%2, #32]"
147*54fd6939SJiyong Park 			 : "=&r" (val), "=&r" (ctl)
148*54fd6939SJiyong Park 			 : "r" (container)
149*54fd6939SJiyong Park 			 : "memory");
150*54fd6939SJiyong Park }
151*54fd6939SJiyong Park 
mt_restore_generic_timer(unsigned long * container)152*54fd6939SJiyong Park static void mt_restore_generic_timer(unsigned long *container)
153*54fd6939SJiyong Park {
154*54fd6939SJiyong Park 	uint64_t ctl;
155*54fd6939SJiyong Park 	uint64_t val;
156*54fd6939SJiyong Park 
157*54fd6939SJiyong Park 	__asm__ volatile("ldp	%x0, %x1, [%2, #0]\n\t"
158*54fd6939SJiyong Park 			 "msr	cntkctl_el1, %x0\n\t"
159*54fd6939SJiyong Park 			 "msr	cntp_cval_el0, %x1"
160*54fd6939SJiyong Park 			 : "=&r" (ctl), "=&r" (val)
161*54fd6939SJiyong Park 			 : "r" (container)
162*54fd6939SJiyong Park 			 : "memory");
163*54fd6939SJiyong Park 
164*54fd6939SJiyong Park 	__asm__ volatile("ldp	%x0, %x1, [%2, #16]\n\t"
165*54fd6939SJiyong Park 			 "msr	cntp_tval_el0, %x0\n\t"
166*54fd6939SJiyong Park 			 "msr	cntp_ctl_el0, %x1"
167*54fd6939SJiyong Park 			 : "=&r" (val), "=&r" (ctl)
168*54fd6939SJiyong Park 			 : "r" (container)
169*54fd6939SJiyong Park 			 : "memory");
170*54fd6939SJiyong Park 
171*54fd6939SJiyong Park 	__asm__ volatile("ldp	%x0, %x1, [%2, #32]\n\t"
172*54fd6939SJiyong Park 			 "msr	cntv_tval_el0, %x0\n\t"
173*54fd6939SJiyong Park 			 "msr	cntv_ctl_el0, %x1"
174*54fd6939SJiyong Park 			 : "=&r" (val), "=&r" (ctl)
175*54fd6939SJiyong Park 			 : "r" (container)
176*54fd6939SJiyong Park 			 : "memory");
177*54fd6939SJiyong Park }
178*54fd6939SJiyong Park 
read_cntpctl(void)179*54fd6939SJiyong Park static inline uint64_t read_cntpctl(void)
180*54fd6939SJiyong Park {
181*54fd6939SJiyong Park 	uint64_t cntpctl;
182*54fd6939SJiyong Park 
183*54fd6939SJiyong Park 	__asm__ volatile("mrs	%x0, cntp_ctl_el0"
184*54fd6939SJiyong Park 			 : "=r" (cntpctl) : : "memory");
185*54fd6939SJiyong Park 
186*54fd6939SJiyong Park 	return cntpctl;
187*54fd6939SJiyong Park }
188*54fd6939SJiyong Park 
write_cntpctl(uint64_t cntpctl)189*54fd6939SJiyong Park static inline void write_cntpctl(uint64_t cntpctl)
190*54fd6939SJiyong Park {
191*54fd6939SJiyong Park 	__asm__ volatile("msr	cntp_ctl_el0, %x0" : : "r"(cntpctl));
192*54fd6939SJiyong Park }
193*54fd6939SJiyong Park 
stop_generic_timer(void)194*54fd6939SJiyong Park static void stop_generic_timer(void)
195*54fd6939SJiyong Park {
196*54fd6939SJiyong Park 	/*
197*54fd6939SJiyong Park 	 * Disable the timer and mask the irq to prevent
198*54fd6939SJiyong Park 	 * suprious interrupts on this cpu interface. It
199*54fd6939SJiyong Park 	 * will bite us when we come back if we don't. It
200*54fd6939SJiyong Park 	 * will be replayed on the inbound cluster.
201*54fd6939SJiyong Park 	 */
202*54fd6939SJiyong Park 	uint64_t cntpctl = read_cntpctl();
203*54fd6939SJiyong Park 
204*54fd6939SJiyong Park 	write_cntpctl(clr_cntp_ctl_enable(cntpctl));
205*54fd6939SJiyong Park }
206*54fd6939SJiyong Park 
mt_cpu_save(unsigned long mpidr)207*54fd6939SJiyong Park static void mt_cpu_save(unsigned long mpidr)
208*54fd6939SJiyong Park {
209*54fd6939SJiyong Park 	struct core_context *core;
210*54fd6939SJiyong Park 
211*54fd6939SJiyong Park 	core = get_core_data(mpidr);
212*54fd6939SJiyong Park 	mt_save_generic_timer(core->timer_data);
213*54fd6939SJiyong Park 
214*54fd6939SJiyong Park 	/* disable timer irq, and upper layer should enable it again. */
215*54fd6939SJiyong Park 	stop_generic_timer();
216*54fd6939SJiyong Park }
217*54fd6939SJiyong Park 
mt_cpu_restore(unsigned long mpidr)218*54fd6939SJiyong Park static void mt_cpu_restore(unsigned long mpidr)
219*54fd6939SJiyong Park {
220*54fd6939SJiyong Park 	struct core_context *core;
221*54fd6939SJiyong Park 
222*54fd6939SJiyong Park 	core = get_core_data(mpidr);
223*54fd6939SJiyong Park 	mt_restore_generic_timer(core->timer_data);
224*54fd6939SJiyong Park }
225*54fd6939SJiyong Park 
mt_platform_save_context(unsigned long mpidr)226*54fd6939SJiyong Park static void mt_platform_save_context(unsigned long mpidr)
227*54fd6939SJiyong Park {
228*54fd6939SJiyong Park 	/* mcusys_save_context: */
229*54fd6939SJiyong Park 	mt_cpu_save(mpidr);
230*54fd6939SJiyong Park }
231*54fd6939SJiyong Park 
mt_platform_restore_context(unsigned long mpidr)232*54fd6939SJiyong Park static void mt_platform_restore_context(unsigned long mpidr)
233*54fd6939SJiyong Park {
234*54fd6939SJiyong Park 	/* mcusys_restore_context: */
235*54fd6939SJiyong Park 	mt_cpu_restore(mpidr);
236*54fd6939SJiyong Park }
237*54fd6939SJiyong Park 
plat_cpu_standby(plat_local_state_t cpu_state)238*54fd6939SJiyong Park static void plat_cpu_standby(plat_local_state_t cpu_state)
239*54fd6939SJiyong Park {
240*54fd6939SJiyong Park 	u_register_t scr;
241*54fd6939SJiyong Park 
242*54fd6939SJiyong Park 	scr = read_scr_el3();
243*54fd6939SJiyong Park 	write_scr_el3(scr | SCR_IRQ_BIT);
244*54fd6939SJiyong Park 	isb();
245*54fd6939SJiyong Park 	dsb();
246*54fd6939SJiyong Park 	wfi();
247*54fd6939SJiyong Park 	write_scr_el3(scr);
248*54fd6939SJiyong Park }
249*54fd6939SJiyong Park 
250*54fd6939SJiyong Park /*******************************************************************************
251*54fd6939SJiyong Park  * MTK_platform handler called when an affinity instance is about to be turned
252*54fd6939SJiyong Park  * on. The level and mpidr determine the affinity instance.
253*54fd6939SJiyong Park  ******************************************************************************/
254*54fd6939SJiyong Park static uintptr_t secure_entrypoint;
255*54fd6939SJiyong Park 
plat_power_domain_on(unsigned long mpidr)256*54fd6939SJiyong Park static int plat_power_domain_on(unsigned long mpidr)
257*54fd6939SJiyong Park {
258*54fd6939SJiyong Park 	int rc = PSCI_E_SUCCESS;
259*54fd6939SJiyong Park 	unsigned long cpu_id;
260*54fd6939SJiyong Park 	unsigned long cluster_id;
261*54fd6939SJiyong Park 	uintptr_t rv;
262*54fd6939SJiyong Park 
263*54fd6939SJiyong Park 	cpu_id = mpidr & MPIDR_CPU_MASK;
264*54fd6939SJiyong Park 	cluster_id = mpidr & MPIDR_CLUSTER_MASK;
265*54fd6939SJiyong Park 
266*54fd6939SJiyong Park 	if (cluster_id)
267*54fd6939SJiyong Park 		rv = (uintptr_t)&mt8173_mcucfg->mp1_rv_addr[cpu_id].rv_addr_lw;
268*54fd6939SJiyong Park 	else
269*54fd6939SJiyong Park 		rv = (uintptr_t)&mt8173_mcucfg->mp0_rv_addr[cpu_id].rv_addr_lw;
270*54fd6939SJiyong Park 
271*54fd6939SJiyong Park 	mmio_write_32(rv, secure_entrypoint);
272*54fd6939SJiyong Park 	INFO("mt_on[%ld:%ld], entry %x\n",
273*54fd6939SJiyong Park 		cluster_id, cpu_id, mmio_read_32(rv));
274*54fd6939SJiyong Park 
275*54fd6939SJiyong Park 	spm_hotplug_on(mpidr);
276*54fd6939SJiyong Park 	return rc;
277*54fd6939SJiyong Park }
278*54fd6939SJiyong Park 
279*54fd6939SJiyong Park /*******************************************************************************
280*54fd6939SJiyong Park  * MTK_platform handler called when an affinity instance is about to be turned
281*54fd6939SJiyong Park  * off. The level and mpidr determine the affinity instance. The 'state' arg.
282*54fd6939SJiyong Park  * allows the platform to decide whether the cluster is being turned off and
283*54fd6939SJiyong Park  * take apt actions.
284*54fd6939SJiyong Park  *
285*54fd6939SJiyong Park  * CAUTION: This function is called with coherent stacks so that caches can be
286*54fd6939SJiyong Park  * turned off, flushed and coherency disabled. There is no guarantee that caches
287*54fd6939SJiyong Park  * will remain turned on across calls to this function as each affinity level is
288*54fd6939SJiyong Park  * dealt with. So do not write & read global variables across calls. It will be
289*54fd6939SJiyong Park  * wise to do flush a write to the global to prevent unpredictable results.
290*54fd6939SJiyong Park  ******************************************************************************/
plat_power_domain_off(const psci_power_state_t * state)291*54fd6939SJiyong Park static void plat_power_domain_off(const psci_power_state_t *state)
292*54fd6939SJiyong Park {
293*54fd6939SJiyong Park 	unsigned long mpidr = read_mpidr_el1();
294*54fd6939SJiyong Park 
295*54fd6939SJiyong Park 	/* Prevent interrupts from spuriously waking up this cpu */
296*54fd6939SJiyong Park 	gicv2_cpuif_disable();
297*54fd6939SJiyong Park 
298*54fd6939SJiyong Park 	spm_hotplug_off(mpidr);
299*54fd6939SJiyong Park 
300*54fd6939SJiyong Park 	trace_power_flow(mpidr, CPU_DOWN);
301*54fd6939SJiyong Park 
302*54fd6939SJiyong Park 	if (MTK_CLUSTER_PWR_STATE(state) == MTK_LOCAL_STATE_OFF) {
303*54fd6939SJiyong Park 		/* Disable coherency if this cluster is to be turned off */
304*54fd6939SJiyong Park 		plat_cci_disable();
305*54fd6939SJiyong Park 
306*54fd6939SJiyong Park 		trace_power_flow(mpidr, CLUSTER_DOWN);
307*54fd6939SJiyong Park 	}
308*54fd6939SJiyong Park }
309*54fd6939SJiyong Park 
310*54fd6939SJiyong Park /*******************************************************************************
311*54fd6939SJiyong Park  * MTK_platform handler called when an affinity instance is about to be
312*54fd6939SJiyong Park  * suspended. The level and mpidr determine the affinity instance. The 'state'
313*54fd6939SJiyong Park  * arg. allows the platform to decide whether the cluster is being turned off
314*54fd6939SJiyong Park  * and take apt actions.
315*54fd6939SJiyong Park  *
316*54fd6939SJiyong Park  * CAUTION: This function is called with coherent stacks so that caches can be
317*54fd6939SJiyong Park  * turned off, flushed and coherency disabled. There is no guarantee that caches
318*54fd6939SJiyong Park  * will remain turned on across calls to this function as each affinity level is
319*54fd6939SJiyong Park  * dealt with. So do not write & read global variables across calls. It will be
320*54fd6939SJiyong Park  * wise to do flush a write to the global to prevent unpredictable results.
321*54fd6939SJiyong Park  ******************************************************************************/
plat_power_domain_suspend(const psci_power_state_t * state)322*54fd6939SJiyong Park static void plat_power_domain_suspend(const psci_power_state_t *state)
323*54fd6939SJiyong Park {
324*54fd6939SJiyong Park 	unsigned long mpidr = read_mpidr_el1();
325*54fd6939SJiyong Park 	unsigned long cluster_id;
326*54fd6939SJiyong Park 	unsigned long cpu_id;
327*54fd6939SJiyong Park 	uintptr_t rv;
328*54fd6939SJiyong Park 
329*54fd6939SJiyong Park 	cpu_id = mpidr & MPIDR_CPU_MASK;
330*54fd6939SJiyong Park 	cluster_id = mpidr & MPIDR_CLUSTER_MASK;
331*54fd6939SJiyong Park 
332*54fd6939SJiyong Park 	if (cluster_id)
333*54fd6939SJiyong Park 		rv = (uintptr_t)&mt8173_mcucfg->mp1_rv_addr[cpu_id].rv_addr_lw;
334*54fd6939SJiyong Park 	else
335*54fd6939SJiyong Park 		rv = (uintptr_t)&mt8173_mcucfg->mp0_rv_addr[cpu_id].rv_addr_lw;
336*54fd6939SJiyong Park 
337*54fd6939SJiyong Park 	mmio_write_32(rv, secure_entrypoint);
338*54fd6939SJiyong Park 
339*54fd6939SJiyong Park 	if (MTK_SYSTEM_PWR_STATE(state) != MTK_LOCAL_STATE_OFF) {
340*54fd6939SJiyong Park 		spm_mcdi_prepare_for_off_state(mpidr, MTK_PWR_LVL0);
341*54fd6939SJiyong Park 		if (MTK_CLUSTER_PWR_STATE(state) == MTK_LOCAL_STATE_OFF)
342*54fd6939SJiyong Park 			spm_mcdi_prepare_for_off_state(mpidr, MTK_PWR_LVL1);
343*54fd6939SJiyong Park 	}
344*54fd6939SJiyong Park 
345*54fd6939SJiyong Park 	mt_platform_save_context(mpidr);
346*54fd6939SJiyong Park 
347*54fd6939SJiyong Park 	/* Perform the common cluster specific operations */
348*54fd6939SJiyong Park 	if (MTK_CLUSTER_PWR_STATE(state) == MTK_LOCAL_STATE_OFF) {
349*54fd6939SJiyong Park 		/* Disable coherency if this cluster is to be turned off */
350*54fd6939SJiyong Park 		plat_cci_disable();
351*54fd6939SJiyong Park 	}
352*54fd6939SJiyong Park 
353*54fd6939SJiyong Park 	if (MTK_SYSTEM_PWR_STATE(state) == MTK_LOCAL_STATE_OFF) {
354*54fd6939SJiyong Park 		wdt_suspend();
355*54fd6939SJiyong Park 		disable_scu(mpidr);
356*54fd6939SJiyong Park 		generic_timer_backup();
357*54fd6939SJiyong Park 		spm_system_suspend();
358*54fd6939SJiyong Park 		/* Prevent interrupts from spuriously waking up this cpu */
359*54fd6939SJiyong Park 		gicv2_cpuif_disable();
360*54fd6939SJiyong Park 	}
361*54fd6939SJiyong Park }
362*54fd6939SJiyong Park 
363*54fd6939SJiyong Park /*******************************************************************************
364*54fd6939SJiyong Park  * MTK_platform handler called when an affinity instance has just been powered
365*54fd6939SJiyong Park  * on after being turned off earlier. The level and mpidr determine the affinity
366*54fd6939SJiyong Park  * instance. The 'state' arg. allows the platform to decide whether the cluster
367*54fd6939SJiyong Park  * was turned off prior to wakeup and do what's necessary to setup it up
368*54fd6939SJiyong Park  * correctly.
369*54fd6939SJiyong Park  ******************************************************************************/
370*54fd6939SJiyong Park void mtk_system_pwr_domain_resume(void);
371*54fd6939SJiyong Park 
plat_power_domain_on_finish(const psci_power_state_t * state)372*54fd6939SJiyong Park static void plat_power_domain_on_finish(const psci_power_state_t *state)
373*54fd6939SJiyong Park {
374*54fd6939SJiyong Park 	unsigned long mpidr = read_mpidr_el1();
375*54fd6939SJiyong Park 
376*54fd6939SJiyong Park 	assert(state->pwr_domain_state[MPIDR_AFFLVL0] == MTK_LOCAL_STATE_OFF);
377*54fd6939SJiyong Park 
378*54fd6939SJiyong Park 	if ((PLAT_MAX_PWR_LVL > MTK_PWR_LVL1) &&
379*54fd6939SJiyong Park 		(state->pwr_domain_state[MTK_PWR_LVL2] == MTK_LOCAL_STATE_OFF))
380*54fd6939SJiyong Park 		mtk_system_pwr_domain_resume();
381*54fd6939SJiyong Park 
382*54fd6939SJiyong Park 	if (state->pwr_domain_state[MPIDR_AFFLVL1] == MTK_LOCAL_STATE_OFF) {
383*54fd6939SJiyong Park 		plat_cci_enable();
384*54fd6939SJiyong Park 		trace_power_flow(mpidr, CLUSTER_UP);
385*54fd6939SJiyong Park 	}
386*54fd6939SJiyong Park 
387*54fd6939SJiyong Park 	if ((PLAT_MAX_PWR_LVL > MTK_PWR_LVL1) &&
388*54fd6939SJiyong Park 		(state->pwr_domain_state[MTK_PWR_LVL2] == MTK_LOCAL_STATE_OFF))
389*54fd6939SJiyong Park 		return;
390*54fd6939SJiyong Park 
391*54fd6939SJiyong Park 	/* Enable the gic cpu interface */
392*54fd6939SJiyong Park 	gicv2_cpuif_enable();
393*54fd6939SJiyong Park 	gicv2_pcpu_distif_init();
394*54fd6939SJiyong Park 	trace_power_flow(mpidr, CPU_UP);
395*54fd6939SJiyong Park }
396*54fd6939SJiyong Park 
397*54fd6939SJiyong Park /*******************************************************************************
398*54fd6939SJiyong Park  * MTK_platform handler called when an affinity instance has just been powered
399*54fd6939SJiyong Park  * on after having been suspended earlier. The level and mpidr determine the
400*54fd6939SJiyong Park  * affinity instance.
401*54fd6939SJiyong Park  ******************************************************************************/
plat_power_domain_suspend_finish(const psci_power_state_t * state)402*54fd6939SJiyong Park static void plat_power_domain_suspend_finish(const psci_power_state_t *state)
403*54fd6939SJiyong Park {
404*54fd6939SJiyong Park 	unsigned long mpidr = read_mpidr_el1();
405*54fd6939SJiyong Park 
406*54fd6939SJiyong Park 	if (state->pwr_domain_state[MTK_PWR_LVL0] == MTK_LOCAL_STATE_RET)
407*54fd6939SJiyong Park 		return;
408*54fd6939SJiyong Park 
409*54fd6939SJiyong Park 	if (MTK_SYSTEM_PWR_STATE(state) == MTK_LOCAL_STATE_OFF) {
410*54fd6939SJiyong Park 		/* Enable the gic cpu interface */
411*54fd6939SJiyong Park 		plat_arm_gic_init();
412*54fd6939SJiyong Park 		spm_system_suspend_finish();
413*54fd6939SJiyong Park 		enable_scu(mpidr);
414*54fd6939SJiyong Park 		wdt_resume();
415*54fd6939SJiyong Park 	}
416*54fd6939SJiyong Park 
417*54fd6939SJiyong Park 	/* Perform the common cluster specific operations */
418*54fd6939SJiyong Park 	if (MTK_CLUSTER_PWR_STATE(state) == MTK_LOCAL_STATE_OFF) {
419*54fd6939SJiyong Park 		/* Enable coherency if this cluster was off */
420*54fd6939SJiyong Park 		plat_cci_enable();
421*54fd6939SJiyong Park 	}
422*54fd6939SJiyong Park 
423*54fd6939SJiyong Park 	mt_platform_restore_context(mpidr);
424*54fd6939SJiyong Park 
425*54fd6939SJiyong Park 	if (MTK_SYSTEM_PWR_STATE(state) != MTK_LOCAL_STATE_OFF) {
426*54fd6939SJiyong Park 		spm_mcdi_finish_for_on_state(mpidr, MTK_PWR_LVL0);
427*54fd6939SJiyong Park 		if (MTK_CLUSTER_PWR_STATE(state) == MTK_LOCAL_STATE_OFF)
428*54fd6939SJiyong Park 			spm_mcdi_finish_for_on_state(mpidr, MTK_PWR_LVL1);
429*54fd6939SJiyong Park 	}
430*54fd6939SJiyong Park 
431*54fd6939SJiyong Park 	gicv2_pcpu_distif_init();
432*54fd6939SJiyong Park }
433*54fd6939SJiyong Park 
plat_get_sys_suspend_power_state(psci_power_state_t * req_state)434*54fd6939SJiyong Park static void plat_get_sys_suspend_power_state(psci_power_state_t *req_state)
435*54fd6939SJiyong Park {
436*54fd6939SJiyong Park 	assert(PLAT_MAX_PWR_LVL >= 2);
437*54fd6939SJiyong Park 
438*54fd6939SJiyong Park 	for (int i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
439*54fd6939SJiyong Park 		req_state->pwr_domain_state[i] = MTK_LOCAL_STATE_OFF;
440*54fd6939SJiyong Park }
441*54fd6939SJiyong Park 
442*54fd6939SJiyong Park /*******************************************************************************
443*54fd6939SJiyong Park  * MTK handlers to shutdown/reboot the system
444*54fd6939SJiyong Park  ******************************************************************************/
plat_system_off(void)445*54fd6939SJiyong Park static void __dead2 plat_system_off(void)
446*54fd6939SJiyong Park {
447*54fd6939SJiyong Park 	INFO("MTK System Off\n");
448*54fd6939SJiyong Park 
449*54fd6939SJiyong Park 	rtc_bbpu_power_down();
450*54fd6939SJiyong Park 
451*54fd6939SJiyong Park 	wfi();
452*54fd6939SJiyong Park 	ERROR("MTK System Off: operation not handled.\n");
453*54fd6939SJiyong Park 	panic();
454*54fd6939SJiyong Park }
455*54fd6939SJiyong Park 
plat_system_reset(void)456*54fd6939SJiyong Park static void __dead2 plat_system_reset(void)
457*54fd6939SJiyong Park {
458*54fd6939SJiyong Park 	/* Write the System Configuration Control Register */
459*54fd6939SJiyong Park 	INFO("MTK System Reset\n");
460*54fd6939SJiyong Park 
461*54fd6939SJiyong Park 	wdt_trigger_reset();
462*54fd6939SJiyong Park 
463*54fd6939SJiyong Park 	wfi();
464*54fd6939SJiyong Park 	ERROR("MTK System Reset: operation not handled.\n");
465*54fd6939SJiyong Park 	panic();
466*54fd6939SJiyong Park }
467*54fd6939SJiyong Park 
468*54fd6939SJiyong Park #if !PSCI_EXTENDED_STATE_ID
plat_validate_power_state(unsigned int power_state,psci_power_state_t * req_state)469*54fd6939SJiyong Park static int plat_validate_power_state(unsigned int power_state,
470*54fd6939SJiyong Park 					psci_power_state_t *req_state)
471*54fd6939SJiyong Park {
472*54fd6939SJiyong Park 	int pstate = psci_get_pstate_type(power_state);
473*54fd6939SJiyong Park 	int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
474*54fd6939SJiyong Park 	int i;
475*54fd6939SJiyong Park 
476*54fd6939SJiyong Park 	assert(req_state);
477*54fd6939SJiyong Park 
478*54fd6939SJiyong Park 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
479*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
480*54fd6939SJiyong Park 
481*54fd6939SJiyong Park 	/* Sanity check the requested state */
482*54fd6939SJiyong Park 	if (pstate == PSTATE_TYPE_STANDBY) {
483*54fd6939SJiyong Park 		/*
484*54fd6939SJiyong Park 		 * It's possible to enter standby only on power level 0
485*54fd6939SJiyong Park 		 * Ignore any other power level.
486*54fd6939SJiyong Park 		 */
487*54fd6939SJiyong Park 		if (pwr_lvl != 0)
488*54fd6939SJiyong Park 			return PSCI_E_INVALID_PARAMS;
489*54fd6939SJiyong Park 
490*54fd6939SJiyong Park 		req_state->pwr_domain_state[MTK_PWR_LVL0] =
491*54fd6939SJiyong Park 					MTK_LOCAL_STATE_RET;
492*54fd6939SJiyong Park 	} else {
493*54fd6939SJiyong Park 		for (i = 0; i <= pwr_lvl; i++)
494*54fd6939SJiyong Park 			req_state->pwr_domain_state[i] =
495*54fd6939SJiyong Park 					MTK_LOCAL_STATE_OFF;
496*54fd6939SJiyong Park 	}
497*54fd6939SJiyong Park 
498*54fd6939SJiyong Park 	/*
499*54fd6939SJiyong Park 	 * We expect the 'state id' to be zero.
500*54fd6939SJiyong Park 	 */
501*54fd6939SJiyong Park 	if (psci_get_pstate_id(power_state))
502*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
503*54fd6939SJiyong Park 
504*54fd6939SJiyong Park 	return PSCI_E_SUCCESS;
505*54fd6939SJiyong Park }
506*54fd6939SJiyong Park #else
plat_validate_power_state(unsigned int power_state,psci_power_state_t * req_state)507*54fd6939SJiyong Park int plat_validate_power_state(unsigned int power_state,
508*54fd6939SJiyong Park 				psci_power_state_t *req_state)
509*54fd6939SJiyong Park {
510*54fd6939SJiyong Park 	unsigned int state_id;
511*54fd6939SJiyong Park 	int i;
512*54fd6939SJiyong Park 
513*54fd6939SJiyong Park 	assert(req_state);
514*54fd6939SJiyong Park 
515*54fd6939SJiyong Park 	/*
516*54fd6939SJiyong Park 	 *  Currently we are using a linear search for finding the matching
517*54fd6939SJiyong Park 	 *  entry in the idle power state array. This can be made a binary
518*54fd6939SJiyong Park 	 *  search if the number of entries justify the additional complexity.
519*54fd6939SJiyong Park 	 */
520*54fd6939SJiyong Park 	for (i = 0; !!mtk_pm_idle_states[i]; i++) {
521*54fd6939SJiyong Park 		if (power_state == mtk_pm_idle_states[i])
522*54fd6939SJiyong Park 			break;
523*54fd6939SJiyong Park 	}
524*54fd6939SJiyong Park 
525*54fd6939SJiyong Park 	/* Return error if entry not found in the idle state array */
526*54fd6939SJiyong Park 	if (!mtk_pm_idle_states[i])
527*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
528*54fd6939SJiyong Park 
529*54fd6939SJiyong Park 	i = 0;
530*54fd6939SJiyong Park 	state_id = psci_get_pstate_id(power_state);
531*54fd6939SJiyong Park 
532*54fd6939SJiyong Park 	/* Parse the State ID and populate the state info parameter */
533*54fd6939SJiyong Park 	while (state_id) {
534*54fd6939SJiyong Park 		req_state->pwr_domain_state[i++] = state_id &
535*54fd6939SJiyong Park 						MTK_LOCAL_PSTATE_MASK;
536*54fd6939SJiyong Park 		state_id >>= MTK_LOCAL_PSTATE_WIDTH;
537*54fd6939SJiyong Park 	}
538*54fd6939SJiyong Park 
539*54fd6939SJiyong Park 	return PSCI_E_SUCCESS;
540*54fd6939SJiyong Park }
541*54fd6939SJiyong Park #endif
542*54fd6939SJiyong Park 
mtk_system_pwr_domain_resume(void)543*54fd6939SJiyong Park void mtk_system_pwr_domain_resume(void)
544*54fd6939SJiyong Park {
545*54fd6939SJiyong Park 	console_switch_state(CONSOLE_FLAG_BOOT);
546*54fd6939SJiyong Park 
547*54fd6939SJiyong Park 	/* Assert system power domain is available on the platform */
548*54fd6939SJiyong Park 	assert(PLAT_MAX_PWR_LVL >= MTK_PWR_LVL2);
549*54fd6939SJiyong Park 
550*54fd6939SJiyong Park 	plat_arm_gic_init();
551*54fd6939SJiyong Park 
552*54fd6939SJiyong Park 	console_switch_state(CONSOLE_FLAG_RUNTIME);
553*54fd6939SJiyong Park }
554*54fd6939SJiyong Park 
555*54fd6939SJiyong Park static const plat_psci_ops_t plat_plat_pm_ops = {
556*54fd6939SJiyong Park 	.cpu_standby			= plat_cpu_standby,
557*54fd6939SJiyong Park 	.pwr_domain_on			= plat_power_domain_on,
558*54fd6939SJiyong Park 	.pwr_domain_on_finish		= plat_power_domain_on_finish,
559*54fd6939SJiyong Park 	.pwr_domain_off			= plat_power_domain_off,
560*54fd6939SJiyong Park 	.pwr_domain_suspend		= plat_power_domain_suspend,
561*54fd6939SJiyong Park 	.pwr_domain_suspend_finish	= plat_power_domain_suspend_finish,
562*54fd6939SJiyong Park 	.system_off			= plat_system_off,
563*54fd6939SJiyong Park 	.system_reset			= plat_system_reset,
564*54fd6939SJiyong Park 	.validate_power_state		= plat_validate_power_state,
565*54fd6939SJiyong Park 	.get_sys_suspend_power_state	= plat_get_sys_suspend_power_state,
566*54fd6939SJiyong Park };
567*54fd6939SJiyong Park 
plat_setup_psci_ops(uintptr_t sec_entrypoint,const plat_psci_ops_t ** psci_ops)568*54fd6939SJiyong Park int plat_setup_psci_ops(uintptr_t sec_entrypoint,
569*54fd6939SJiyong Park 			const plat_psci_ops_t **psci_ops)
570*54fd6939SJiyong Park {
571*54fd6939SJiyong Park 	*psci_ops = &plat_plat_pm_ops;
572*54fd6939SJiyong Park 	secure_entrypoint = sec_entrypoint;
573*54fd6939SJiyong Park 	return 0;
574*54fd6939SJiyong Park }
575*54fd6939SJiyong Park 
576*54fd6939SJiyong Park /*
577*54fd6939SJiyong Park  * The PSCI generic code uses this API to let the platform participate in state
578*54fd6939SJiyong Park  * coordination during a power management operation. It compares the platform
579*54fd6939SJiyong Park  * specific local power states requested by each cpu for a given power domain
580*54fd6939SJiyong Park  * and returns the coordinated target power state that the domain should
581*54fd6939SJiyong Park  * enter. A platform assigns a number to a local power state. This default
582*54fd6939SJiyong Park  * implementation assumes that the platform assigns these numbers in order of
583*54fd6939SJiyong Park  * increasing depth of the power state i.e. for two power states X & Y, if X < Y
584*54fd6939SJiyong Park  * then X represents a shallower power state than Y. As a result, the
585*54fd6939SJiyong Park  * coordinated target local power state for a power domain will be the minimum
586*54fd6939SJiyong Park  * of the requested local power states.
587*54fd6939SJiyong Park  */
plat_get_target_pwr_state(unsigned int lvl,const plat_local_state_t * states,unsigned int ncpu)588*54fd6939SJiyong Park plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
589*54fd6939SJiyong Park 					     const plat_local_state_t *states,
590*54fd6939SJiyong Park 					     unsigned int ncpu)
591*54fd6939SJiyong Park {
592*54fd6939SJiyong Park 	plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
593*54fd6939SJiyong Park 
594*54fd6939SJiyong Park 	assert(ncpu);
595*54fd6939SJiyong Park 
596*54fd6939SJiyong Park 	do {
597*54fd6939SJiyong Park 		temp = *states++;
598*54fd6939SJiyong Park 		if (temp < target)
599*54fd6939SJiyong Park 			target = temp;
600*54fd6939SJiyong Park 	} while (--ncpu);
601*54fd6939SJiyong Park 
602*54fd6939SJiyong Park 	return target;
603*54fd6939SJiyong Park }
604