xref: /arm-trusted-firmware/plat/ax/lmt/lmt_pm.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /* SPDX-License-Identifier: BSD-3-Clause */
2*91f16700Schasinglulu /*
3*91f16700Schasinglulu  * Copyright (C) 2024, Charleye <wangkart@aliyun.com>
4*91f16700Schasinglulu  * All rights reserved.
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <platform_def.h>
9*91f16700Schasinglulu #include <lmt_def.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <arch_helpers.h>
12*91f16700Schasinglulu #include <common/debug.h>
13*91f16700Schasinglulu #include <lib/psci/psci.h>
14*91f16700Schasinglulu #include <lib/el3_runtime/cpu_data.h>
15*91f16700Schasinglulu #include <plat/common/platform.h>
16*91f16700Schasinglulu #include <drivers/arm/gicv3.h>
17*91f16700Schasinglulu #include <drivers/arm/gic_common.h>
18*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
19*91f16700Schasinglulu #include <lib/mmio.h>
20*91f16700Schasinglulu 
21*91f16700Schasinglulu struct cpu_offset_map {
22*91f16700Schasinglulu     uint64_t offset;
23*91f16700Schasinglulu     u_register_t cpu_idx;
24*91f16700Schasinglulu } cpu_map [] = {
25*91f16700Schasinglulu     {CPU_CL0_C0_0, 0x000},
26*91f16700Schasinglulu     {CPU_CL0_C1_0, 0x100},
27*91f16700Schasinglulu     {CPU_CL0_C2_0, 0x200},
28*91f16700Schasinglulu     {CPU_CL0_C3_0, 0x300},
29*91f16700Schasinglulu     {CPU_CL1_C0_0, 0x10000},
30*91f16700Schasinglulu     {CPU_CL1_C1_0, 0x10100},
31*91f16700Schasinglulu     {CPU_CL1_C2_0, 0x10200},
32*91f16700Schasinglulu     {CPU_CL1_C3_0, 0x10300},
33*91f16700Schasinglulu };
34*91f16700Schasinglulu 
35*91f16700Schasinglulu /*
36*91f16700Schasinglulu  * The secure entry point to be used on warm reset.
37*91f16700Schasinglulu  */
38*91f16700Schasinglulu static unsigned long secure_entrypoint;
39*91f16700Schasinglulu 
40*91f16700Schasinglulu /* Make composite power state parameter till power level 0 */
41*91f16700Schasinglulu #if PSCI_EXTENDED_STATE_ID
42*91f16700Schasinglulu 
43*91f16700Schasinglulu #define lmt_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
44*91f16700Schasinglulu 		(((lvl0_state) << PSTATE_ID_SHIFT) | \
45*91f16700Schasinglulu 		 ((type) << PSTATE_TYPE_SHIFT))
46*91f16700Schasinglulu #else
47*91f16700Schasinglulu #define lmt_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
48*91f16700Schasinglulu 		(((lvl0_state) << PSTATE_ID_SHIFT) | \
49*91f16700Schasinglulu 		 ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
50*91f16700Schasinglulu 		 ((type) << PSTATE_TYPE_SHIFT))
51*91f16700Schasinglulu #endif /* PSCI_EXTENDED_STATE_ID */
52*91f16700Schasinglulu 
53*91f16700Schasinglulu 
54*91f16700Schasinglulu #define lmt_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
55*91f16700Schasinglulu 		(((lvl1_state) << LMT_LOCAL_PSTATE_WIDTH) | \
56*91f16700Schasinglulu 		 lmt_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
57*91f16700Schasinglulu 
58*91f16700Schasinglulu /*
59*91f16700Schasinglulu  *  The table storing the valid idle power states. Ensure that the
60*91f16700Schasinglulu  *  array entries are populated in ascending order of state-id to
61*91f16700Schasinglulu  *  enable us to use binary search during power state validation.
62*91f16700Schasinglulu  *  The table must be terminated by a NULL entry.
63*91f16700Schasinglulu  */
64*91f16700Schasinglulu static const unsigned int lmt_pm_idle_states[] = {
65*91f16700Schasinglulu 	/* State-id - 0x01 */
66*91f16700Schasinglulu 	lmt_make_pwrstate_lvl1(LMT_LOCAL_STATE_RUN, LMT_LOCAL_STATE_RET,
67*91f16700Schasinglulu 				MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
68*91f16700Schasinglulu 	/* State-id - 0x02 */
69*91f16700Schasinglulu 	lmt_make_pwrstate_lvl1(LMT_LOCAL_STATE_RUN, LMT_LOCAL_STATE_OFF,
70*91f16700Schasinglulu 				MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
71*91f16700Schasinglulu 	/* State-id - 0x22 */
72*91f16700Schasinglulu 	lmt_make_pwrstate_lvl1(LMT_LOCAL_STATE_OFF, LMT_LOCAL_STATE_OFF,
73*91f16700Schasinglulu 				MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
74*91f16700Schasinglulu 	0,
75*91f16700Schasinglulu };
76*91f16700Schasinglulu 
77*91f16700Schasinglulu /**
78*91f16700Schasinglulu  * lmt_validate_power_state() - Platform handler to check if power state is valid
79*91f16700Schasinglulu  *
80*91f16700Schasinglulu  * @power_state: power state prepares to go to.
81*91f16700Schasinglulu  * @req_state: power domain level specific local states
82*91f16700Schasinglulu  *
83*91f16700Schasinglulu  * This function is called by the PSCI implementation during the ``CPU_SUSPEND``
84*91f16700Schasinglulu  * call to validate the ``power_state`` parameter of the PSCI API and if valid,
85*91f16700Schasinglulu  * populate it in ``req_state`` (second argument) array as power domain level
86*91f16700Schasinglulu  * specific local states. If the ``power_state`` is invalid, the platform must
87*91f16700Schasinglulu  * return PSCI_E_INVALID_PARAMS as error, which is propagated back to the
88*91f16700Schasinglulu  * normal world PSCI client.
89*91f16700Schasinglulu  *
90*91f16700Schasinglulu  * Return: PSCI_E_SUCCESS if success, error code otherwise.
91*91f16700Schasinglulu  */
92*91f16700Schasinglulu int lmt_validate_power_state(unsigned int power_state,
93*91f16700Schasinglulu 			    psci_power_state_t *req_state)
94*91f16700Schasinglulu {
95*91f16700Schasinglulu 	unsigned int state_id;
96*91f16700Schasinglulu 	int i;
97*91f16700Schasinglulu 
98*91f16700Schasinglulu 	assert(req_state);
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	/*
101*91f16700Schasinglulu 	 *  Currently we are using a linear search for finding the matching
102*91f16700Schasinglulu 	 *  entry in the idle power state array. This can be made a binary
103*91f16700Schasinglulu 	 *  search if the number of entries justify the additional complexity.
104*91f16700Schasinglulu 	 */
105*91f16700Schasinglulu 	for (i = 0; !!lmt_pm_idle_states[i]; i++) {
106*91f16700Schasinglulu 		if (power_state == lmt_pm_idle_states[i])
107*91f16700Schasinglulu 			break;
108*91f16700Schasinglulu 	}
109*91f16700Schasinglulu 
110*91f16700Schasinglulu 	/* Return error if entry not found in the idle state array */
111*91f16700Schasinglulu 	if (!lmt_pm_idle_states[i])
112*91f16700Schasinglulu 		return PSCI_E_INVALID_PARAMS;
113*91f16700Schasinglulu 
114*91f16700Schasinglulu 	i = 0;
115*91f16700Schasinglulu 	state_id = psci_get_pstate_id(power_state);
116*91f16700Schasinglulu 
117*91f16700Schasinglulu 	/* Parse the State ID and populate the state info parameter */
118*91f16700Schasinglulu 	while (state_id) {
119*91f16700Schasinglulu 		req_state->pwr_domain_state[i++] = state_id &
120*91f16700Schasinglulu 						LMT_LOCAL_PSTATE_MASK;
121*91f16700Schasinglulu 		state_id >>= LMT_LOCAL_PSTATE_WIDTH;
122*91f16700Schasinglulu 	}
123*91f16700Schasinglulu 
124*91f16700Schasinglulu 	return PSCI_E_SUCCESS;
125*91f16700Schasinglulu }
126*91f16700Schasinglulu 
127*91f16700Schasinglulu /**
128*91f16700Schasinglulu  * lmt_validate_ns_entrypoint() - Platform handler to check ns_entrypoint
129*91f16700Schasinglulu  *
130*91f16700Schasinglulu  * @ns_entrypoint: entrypoint address to check
131*91f16700Schasinglulu  *
132*91f16700Schasinglulu  * This function need to check if the ns_entrypoint is in non-secure world.
133*91f16700Schasinglulu  *
134*91f16700Schasinglulu  * Return: PSCI_E_SUCCESS if success, error code otherwise.
135*91f16700Schasinglulu  */
136*91f16700Schasinglulu static int lmt_validate_ns_entrypoint(uintptr_t entrypoint)
137*91f16700Schasinglulu {
138*91f16700Schasinglulu 	/*
139*91f16700Schasinglulu 	 * Check if the non secure entrypoint lies within the non
140*91f16700Schasinglulu 	 * secure DRAM.
141*91f16700Schasinglulu 	 */
142*91f16700Schasinglulu 	if ((entrypoint >= LMT_DRAM_BASE) &&
143*91f16700Schasinglulu 		(entrypoint < (LMT_DRAM_BASE + LMT_NS_DDR_SIZE)))
144*91f16700Schasinglulu 		return PSCI_E_SUCCESS;
145*91f16700Schasinglulu 
146*91f16700Schasinglulu 	return PSCI_E_INVALID_ADDRESS;
147*91f16700Schasinglulu }
148*91f16700Schasinglulu 
149*91f16700Schasinglulu /**
150*91f16700Schasinglulu  * lmt_cpu_standby() - Put CPU to standy mode
151*91f16700Schasinglulu  *
152*91f16700Schasinglulu  * @cpu_state: local cpu state
153*91f16700Schasinglulu  *
154*91f16700Schasinglulu  * This function will put cpu into idle state, and will return when cpu wakeup.
155*91f16700Schasinglulu  *
156*91f16700Schasinglulu  * Return: void
157*91f16700Schasinglulu  */
158*91f16700Schasinglulu void lmt_cpu_standby(plat_local_state_t cpu_state)
159*91f16700Schasinglulu {
160*91f16700Schasinglulu 	assert(cpu_state == LMT_LOCAL_STATE_RET);
161*91f16700Schasinglulu 
162*91f16700Schasinglulu 	/*
163*91f16700Schasinglulu 	 * Enter standby state
164*91f16700Schasinglulu 	 * dsb is good practice before using wfi to enter low power states
165*91f16700Schasinglulu 	 */
166*91f16700Schasinglulu 	dsb();
167*91f16700Schasinglulu 	wfi();
168*91f16700Schasinglulu }
169*91f16700Schasinglulu 
170*91f16700Schasinglulu static uint64_t get_offset_addr_by_mpidr(u_register_t mpidr)
171*91f16700Schasinglulu {
172*91f16700Schasinglulu 	int i;
173*91f16700Schasinglulu 
174*91f16700Schasinglulu 	for (i = 0; i < ARRAY_SIZE(cpu_map); i++) {
175*91f16700Schasinglulu 		if (cpu_map[i].cpu_idx == mpidr)
176*91f16700Schasinglulu 			return cpu_map[i].offset;
177*91f16700Schasinglulu 	}
178*91f16700Schasinglulu 
179*91f16700Schasinglulu 	return ULONG_MAX;
180*91f16700Schasinglulu }
181*91f16700Schasinglulu 
182*91f16700Schasinglulu /**
183*91f16700Schasinglulu  * lmt_pwr_domain_on() -	Power on a cpu.
184*91f16700Schasinglulu  *
185*91f16700Schasinglulu  * @mpidr: mpidr value for this cpu
186*91f16700Schasinglulu  *
187*91f16700Schasinglulu  * This will be call before cpu power on, need to take action to bring cpu on
188*91f16700Schasinglulu  *
189*91f16700Schasinglulu  * Return: PSCI_E_SUCCESS if success, error code otherwise
190*91f16700Schasinglulu  */
191*91f16700Schasinglulu static int lmt_pwr_domain_on(u_register_t mpidr)
192*91f16700Schasinglulu {
193*91f16700Schasinglulu 	int rc = PSCI_E_SUCCESS;
194*91f16700Schasinglulu 	unsigned pos = plat_core_pos_by_mpidr(mpidr);
195*91f16700Schasinglulu 	uint64_t *hold_base = (uint64_t *)PLAT_LMT_HOLD_BASE;
196*91f16700Schasinglulu 	uint64_t offset = get_offset_addr_by_mpidr(mpidr);
197*91f16700Schasinglulu 
198*91f16700Schasinglulu 	hold_base[pos] = PLAT_LMT_HOLD_STATE_GO;
199*91f16700Schasinglulu 	sev();
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	mmio_setbits_32(LMT_PMU_BASE + offset, BIT(12));
202*91f16700Schasinglulu 
203*91f16700Schasinglulu 	return rc;
204*91f16700Schasinglulu }
205*91f16700Schasinglulu 
206*91f16700Schasinglulu /**
207*91f16700Schasinglulu  * lmt_pwr_domain_off() - Power off a cpu.
208*91f16700Schasinglulu  *
209*91f16700Schasinglulu  * @target_state: CPU topological state
210*91f16700Schasinglulu  *
211*91f16700Schasinglulu  * This will be call before cpu power off, need to take action to put cpu off
212*91f16700Schasinglulu  *
213*91f16700Schasinglulu  * Return: void
214*91f16700Schasinglulu  */
215*91f16700Schasinglulu static void lmt_pwr_domain_off(const psci_power_state_t *target_state)
216*91f16700Schasinglulu {
217*91f16700Schasinglulu 	lmt_pwr_gic_off();
218*91f16700Schasinglulu }
219*91f16700Schasinglulu 
220*91f16700Schasinglulu /**
221*91f16700Schasinglulu  * lmt_pwr_domain_suspend() - Put system into suspend state.
222*91f16700Schasinglulu  *
223*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
224*91f16700Schasinglulu  *
225*91f16700Schasinglulu  * Perform the platform specific actions to prepare to suspend the calling
226*91f16700Schasinglulu  * CPU and its higher parent power domain levels as indicated by the
227*91f16700Schasinglulu  * ``target_state`` (first argument). It is called by the PSCI ``CPU_SUSPEND``
228*91f16700Schasinglulu  * API implementation.
229*91f16700Schasinglulu  *
230*91f16700Schasinglulu  * Return: void
231*91f16700Schasinglulu  */
232*91f16700Schasinglulu static void lmt_pwr_domain_suspend(const psci_power_state_t *target_state)
233*91f16700Schasinglulu {
234*91f16700Schasinglulu 	assert(0);
235*91f16700Schasinglulu }
236*91f16700Schasinglulu 
237*91f16700Schasinglulu void __dead2 plat_secondary_cold_boot_setup(void);
238*91f16700Schasinglulu /**
239*91f16700Schasinglulu  * lmt_pwr_down_wfi() - Powerdown the CPU
240*91f16700Schasinglulu  *
241*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
242*91f16700Schasinglulu  *
243*91f16700Schasinglulu  * Perform platform specific actions including the ``wfi`` invocation which
244*91f16700Schasinglulu  * allows the CPU to powerdown.
245*91f16700Schasinglulu  *
246*91f16700Schasinglulu  * Return: void
247*91f16700Schasinglulu  */
248*91f16700Schasinglulu static void __dead2 lmt_pwr_down_wfi(const psci_power_state_t *target_state)
249*91f16700Schasinglulu {
250*91f16700Schasinglulu 	disable_mmu_el3();
251*91f16700Schasinglulu 	plat_secondary_cold_boot_setup();
252*91f16700Schasinglulu }
253*91f16700Schasinglulu 
254*91f16700Schasinglulu /**
255*91f16700Schasinglulu  * lmt_pwr_domain_on_finish() - Platform handler when cpu is powered on.
256*91f16700Schasinglulu  *
257*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
258*91f16700Schasinglulu  *
259*91f16700Schasinglulu  * This function is called by the PSCI implementation after the calling CPU is
260*91f16700Schasinglulu  * powered on and released from reset in response to an earlier PSCI ``CPU_ON``
261*91f16700Schasinglulu  * call. It performs the platform-specific setup required to initialize enough
262*91f16700Schasinglulu  * state for this CPU to enter the normal world and also provide secure runtime
263*91f16700Schasinglulu  * firmware services.
264*91f16700Schasinglulu  *
265*91f16700Schasinglulu  * Return: void
266*91f16700Schasinglulu  */
267*91f16700Schasinglulu static void lmt_pwr_domain_on_finish(const psci_power_state_t *target_state)
268*91f16700Schasinglulu {
269*91f16700Schasinglulu 	assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
270*91f16700Schasinglulu 					LMT_LOCAL_STATE_OFF);
271*91f16700Schasinglulu 
272*91f16700Schasinglulu 	lmt_pwr_gic_on_finish();
273*91f16700Schasinglulu }
274*91f16700Schasinglulu 
275*91f16700Schasinglulu /**
276*91f16700Schasinglulu  * lmt_pwr_domain_suspend_finish() - Platform handler after wakeup.
277*91f16700Schasinglulu  *
278*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
279*91f16700Schasinglulu  *
280*91f16700Schasinglulu  * Performs the platform-specific setup required to restore the saved state for
281*91f16700Schasinglulu  * this CPU to resume execution in the normal world and also provide secure
282*91f16700Schasinglulu  * runtime firmware services. This function will need to restore Distributor,
283*91f16700Schasinglulu  * Redistributors or ITS context.
284*91f16700Schasinglulu  *
285*91f16700Schasinglulu  * Return: void
286*91f16700Schasinglulu  */
287*91f16700Schasinglulu static void lmt_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
288*91f16700Schasinglulu {
289*91f16700Schasinglulu 	assert(0);
290*91f16700Schasinglulu }
291*91f16700Schasinglulu 
292*91f16700Schasinglulu /**
293*91f16700Schasinglulu  * lmt_system_off() - Platform handler to poweroff the system.
294*91f16700Schasinglulu  *
295*91f16700Schasinglulu  * Performs the platform-specific setup to power off the whole system.
296*91f16700Schasinglulu  * Due to we use fake shutdown for J5, this function just while (1) to wfi().
297*91f16700Schasinglulu  *
298*91f16700Schasinglulu  * Return: void
299*91f16700Schasinglulu  */
300*91f16700Schasinglulu static void __dead2 lmt_system_off(void)
301*91f16700Schasinglulu {
302*91f16700Schasinglulu 	panic();
303*91f16700Schasinglulu }
304*91f16700Schasinglulu 
305*91f16700Schasinglulu /**
306*91f16700Schasinglulu  * lmt_system_reset() - Platform handler to reboot the system.
307*91f16700Schasinglulu  *
308*91f16700Schasinglulu  * Performs the platform-specific setup to reboot the whole system.
309*91f16700Schasinglulu  * We need to generated a wdt reset to reset the whole soc. In case of wdt
310*91f16700Schasinglulu  * trigger failed, going to panic().
311*91f16700Schasinglulu  *
312*91f16700Schasinglulu  * Return: void
313*91f16700Schasinglulu  */
314*91f16700Schasinglulu static void __dead2 lmt_system_reset(void)
315*91f16700Schasinglulu {
316*91f16700Schasinglulu 	panic();
317*91f16700Schasinglulu }
318*91f16700Schasinglulu 
319*91f16700Schasinglulu /**
320*91f16700Schasinglulu  * lmt_psci_ops - j6 platform psci ops
321*91f16700Schasinglulu  */
322*91f16700Schasinglulu static plat_psci_ops_t lmt_psci_ops = {
323*91f16700Schasinglulu 	.cpu_standby = lmt_cpu_standby,
324*91f16700Schasinglulu 	.pwr_domain_on = lmt_pwr_domain_on,
325*91f16700Schasinglulu 	.pwr_domain_off = lmt_pwr_domain_off,
326*91f16700Schasinglulu 	.pwr_domain_suspend = lmt_pwr_domain_suspend,
327*91f16700Schasinglulu 	.pwr_domain_pwr_down_wfi = lmt_pwr_down_wfi,
328*91f16700Schasinglulu 	.pwr_domain_on_finish = lmt_pwr_domain_on_finish,
329*91f16700Schasinglulu 	.pwr_domain_suspend_finish = lmt_pwr_domain_suspend_finish,
330*91f16700Schasinglulu 	.system_off = lmt_system_off,
331*91f16700Schasinglulu 	.system_reset = lmt_system_reset,
332*91f16700Schasinglulu 	.validate_power_state = lmt_validate_power_state,
333*91f16700Schasinglulu 	.validate_ns_entrypoint = lmt_validate_ns_entrypoint,
334*91f16700Schasinglulu };
335*91f16700Schasinglulu 
336*91f16700Schasinglulu /**
337*91f16700Schasinglulu  * plat_setup_psci_ops() - Platform handler to setup psci ops
338*91f16700Schasinglulu  *
339*91f16700Schasinglulu  * @sec_entrypoint: entry to going to after cpu powered on.
340*91f16700Schasinglulu  * @psci_ops: pointer to store psci ops.
341*91f16700Schasinglulu  *
342*91f16700Schasinglulu  * This function need to initialze
343*91f16700Schasinglulu  *
344*91f16700Schasinglulu  * Return: void.
345*91f16700Schasinglulu  */
346*91f16700Schasinglulu int plat_setup_psci_ops(uintptr_t sec_entrypoint,
347*91f16700Schasinglulu 			const plat_psci_ops_t **psci_ops)
348*91f16700Schasinglulu {
349*91f16700Schasinglulu 	uintptr_t *mailbox = (void *)PLAT_LMT_TRUSTED_MAILBOX_BASE;
350*91f16700Schasinglulu 
351*91f16700Schasinglulu 	*mailbox = sec_entrypoint;
352*91f16700Schasinglulu 	secure_entrypoint = (unsigned long)sec_entrypoint;
353*91f16700Schasinglulu 
354*91f16700Schasinglulu 	assert(psci_ops);
355*91f16700Schasinglulu 	*psci_ops = &lmt_psci_ops;
356*91f16700Schasinglulu 
357*91f16700Schasinglulu 	return 0;
358*91f16700Schasinglulu }
359