xref: /arm-trusted-firmware/plat/ax/laguna/lua_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 <lua_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 /*
22*91f16700Schasinglulu  * The secure entry point to be used on warm reset.
23*91f16700Schasinglulu  */
24*91f16700Schasinglulu static unsigned long secure_entrypoint;
25*91f16700Schasinglulu 
26*91f16700Schasinglulu /* Make composite power state parameter till power level 0 */
27*91f16700Schasinglulu #if PSCI_EXTENDED_STATE_ID
28*91f16700Schasinglulu 
29*91f16700Schasinglulu #define lua_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
30*91f16700Schasinglulu 		(((lvl0_state) << PSTATE_ID_SHIFT) | \
31*91f16700Schasinglulu 		 ((type) << PSTATE_TYPE_SHIFT))
32*91f16700Schasinglulu #else
33*91f16700Schasinglulu #define lua_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
34*91f16700Schasinglulu 		(((lvl0_state) << PSTATE_ID_SHIFT) | \
35*91f16700Schasinglulu 		 ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
36*91f16700Schasinglulu 		 ((type) << PSTATE_TYPE_SHIFT))
37*91f16700Schasinglulu #endif /* PSCI_EXTENDED_STATE_ID */
38*91f16700Schasinglulu 
39*91f16700Schasinglulu 
40*91f16700Schasinglulu #define lua_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
41*91f16700Schasinglulu 		(((lvl1_state) << LUA_LOCAL_PSTATE_WIDTH) | \
42*91f16700Schasinglulu 		 lua_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
43*91f16700Schasinglulu 
44*91f16700Schasinglulu /*
45*91f16700Schasinglulu  *  The table storing the valid idle power states. Ensure that the
46*91f16700Schasinglulu  *  array entries are populated in ascending order of state-id to
47*91f16700Schasinglulu  *  enable us to use binary search during power state validation.
48*91f16700Schasinglulu  *  The table must be terminated by a NULL entry.
49*91f16700Schasinglulu  */
50*91f16700Schasinglulu static const unsigned int lua_pm_idle_states[] = {
51*91f16700Schasinglulu 	/* State-id - 0x01 */
52*91f16700Schasinglulu 	lua_make_pwrstate_lvl1(LUA_LOCAL_STATE_RUN, LUA_LOCAL_STATE_RET,
53*91f16700Schasinglulu 				MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
54*91f16700Schasinglulu 	/* State-id - 0x02 */
55*91f16700Schasinglulu 	lua_make_pwrstate_lvl1(LUA_LOCAL_STATE_RUN, LUA_LOCAL_STATE_OFF,
56*91f16700Schasinglulu 				MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
57*91f16700Schasinglulu 	/* State-id - 0x22 */
58*91f16700Schasinglulu 	lua_make_pwrstate_lvl1(LUA_LOCAL_STATE_OFF, LUA_LOCAL_STATE_OFF,
59*91f16700Schasinglulu 				MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
60*91f16700Schasinglulu 	0,
61*91f16700Schasinglulu };
62*91f16700Schasinglulu 
63*91f16700Schasinglulu /**
64*91f16700Schasinglulu  * lua_validate_power_state() - Platform handler to check if power state is valid
65*91f16700Schasinglulu  *
66*91f16700Schasinglulu  * @power_state: power state prepares to go to.
67*91f16700Schasinglulu  * @req_state: power domain level specific local states
68*91f16700Schasinglulu  *
69*91f16700Schasinglulu  * This function is called by the PSCI implementation during the ``CPU_SUSPEND``
70*91f16700Schasinglulu  * call to validate the ``power_state`` parameter of the PSCI API and if valid,
71*91f16700Schasinglulu  * populate it in ``req_state`` (second argument) array as power domain level
72*91f16700Schasinglulu  * specific local states. If the ``power_state`` is invalid, the platform must
73*91f16700Schasinglulu  * return PSCI_E_INVALID_PARAMS as error, which is propagated back to the
74*91f16700Schasinglulu  * normal world PSCI client.
75*91f16700Schasinglulu  *
76*91f16700Schasinglulu  * Return: PSCI_E_SUCCESS if success, error code otherwise.
77*91f16700Schasinglulu  */
78*91f16700Schasinglulu int lua_validate_power_state(unsigned int power_state,
79*91f16700Schasinglulu 			    psci_power_state_t *req_state)
80*91f16700Schasinglulu {
81*91f16700Schasinglulu 	unsigned int state_id;
82*91f16700Schasinglulu 	int i;
83*91f16700Schasinglulu 
84*91f16700Schasinglulu 	assert(req_state);
85*91f16700Schasinglulu 
86*91f16700Schasinglulu 	/*
87*91f16700Schasinglulu 	 *  Currently we are using a linear search for finding the matching
88*91f16700Schasinglulu 	 *  entry in the idle power state array. This can be made a binary
89*91f16700Schasinglulu 	 *  search if the number of entries justify the additional complexity.
90*91f16700Schasinglulu 	 */
91*91f16700Schasinglulu 	for (i = 0; !!lua_pm_idle_states[i]; i++) {
92*91f16700Schasinglulu 		if (power_state == lua_pm_idle_states[i])
93*91f16700Schasinglulu 			break;
94*91f16700Schasinglulu 	}
95*91f16700Schasinglulu 
96*91f16700Schasinglulu 	/* Return error if entry not found in the idle state array */
97*91f16700Schasinglulu 	if (!lua_pm_idle_states[i])
98*91f16700Schasinglulu 		return PSCI_E_INVALID_PARAMS;
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	i = 0;
101*91f16700Schasinglulu 	state_id = psci_get_pstate_id(power_state);
102*91f16700Schasinglulu 
103*91f16700Schasinglulu 	/* Parse the State ID and populate the state info parameter */
104*91f16700Schasinglulu 	while (state_id) {
105*91f16700Schasinglulu 		req_state->pwr_domain_state[i++] = state_id &
106*91f16700Schasinglulu 						LUA_LOCAL_PSTATE_MASK;
107*91f16700Schasinglulu 		state_id >>= LUA_LOCAL_PSTATE_WIDTH;
108*91f16700Schasinglulu 	}
109*91f16700Schasinglulu 
110*91f16700Schasinglulu 	return PSCI_E_SUCCESS;
111*91f16700Schasinglulu }
112*91f16700Schasinglulu 
113*91f16700Schasinglulu /**
114*91f16700Schasinglulu  * lua_validate_ns_entrypoint() - Platform handler to check ns_entrypoint
115*91f16700Schasinglulu  *
116*91f16700Schasinglulu  * @ns_entrypoint: entrypoint address to check
117*91f16700Schasinglulu  *
118*91f16700Schasinglulu  * This function need to check if the ns_entrypoint is in non-secure world.
119*91f16700Schasinglulu  *
120*91f16700Schasinglulu  * Return: PSCI_E_SUCCESS if success, error code otherwise.
121*91f16700Schasinglulu  */
122*91f16700Schasinglulu static int lua_validate_ns_entrypoint(uintptr_t entrypoint)
123*91f16700Schasinglulu {
124*91f16700Schasinglulu 	/*
125*91f16700Schasinglulu 	 * Check if the non secure entrypoint lies within the non
126*91f16700Schasinglulu 	 * secure DRAM.
127*91f16700Schasinglulu 	 */
128*91f16700Schasinglulu 	if ((entrypoint >= LUA_DRAM_BASE) &&
129*91f16700Schasinglulu 		(entrypoint < (LUA_DRAM_BASE + LUA_NS_DDR_SIZE)))
130*91f16700Schasinglulu 		return PSCI_E_SUCCESS;
131*91f16700Schasinglulu 
132*91f16700Schasinglulu 	return PSCI_E_INVALID_ADDRESS;
133*91f16700Schasinglulu }
134*91f16700Schasinglulu 
135*91f16700Schasinglulu /**
136*91f16700Schasinglulu  * lua_cpu_standby() - Put CPU to standy mode
137*91f16700Schasinglulu  *
138*91f16700Schasinglulu  * @cpu_state: local cpu state
139*91f16700Schasinglulu  *
140*91f16700Schasinglulu  * This function will put cpu into idle state, and will return when cpu wakeup.
141*91f16700Schasinglulu  *
142*91f16700Schasinglulu  * Return: void
143*91f16700Schasinglulu  */
144*91f16700Schasinglulu void lua_cpu_standby(plat_local_state_t cpu_state)
145*91f16700Schasinglulu {
146*91f16700Schasinglulu 	assert(cpu_state == LUA_LOCAL_STATE_RET);
147*91f16700Schasinglulu 
148*91f16700Schasinglulu 	/*
149*91f16700Schasinglulu 	 * Enter standby state
150*91f16700Schasinglulu 	 * dsb is good practice before using wfi to enter low power states
151*91f16700Schasinglulu 	 */
152*91f16700Schasinglulu 	dsb();
153*91f16700Schasinglulu 	wfi();
154*91f16700Schasinglulu }
155*91f16700Schasinglulu 
156*91f16700Schasinglulu /**
157*91f16700Schasinglulu  * lua_pwr_domain_on() -	Power on a cpu.
158*91f16700Schasinglulu  *
159*91f16700Schasinglulu  * @mpidr: mpidr value for this cpu
160*91f16700Schasinglulu  *
161*91f16700Schasinglulu  * This will be call before cpu power on, need to take action to bring cpu on
162*91f16700Schasinglulu  *
163*91f16700Schasinglulu  * Return: PSCI_E_SUCCESS if success, error code otherwise
164*91f16700Schasinglulu  */
165*91f16700Schasinglulu static int lua_pwr_domain_on(u_register_t mpidr)
166*91f16700Schasinglulu {
167*91f16700Schasinglulu 	int rc = PSCI_E_SUCCESS;
168*91f16700Schasinglulu 	unsigned pos = plat_core_pos_by_mpidr(mpidr);
169*91f16700Schasinglulu 	uint64_t *hold_base = (uint64_t *)PLAT_LUA_HOLD_BASE;
170*91f16700Schasinglulu 	uint64_t addr = CPU_SYSCTL_BASE + CA55_CORE_SW_RST_OFFSET;
171*91f16700Schasinglulu 	uint64_t rvbar_addr = addr + 8;
172*91f16700Schasinglulu 	uint32_t low = (uint32_t)secure_entrypoint;
173*91f16700Schasinglulu 	uint32_t high = (uint32_t)(secure_entrypoint >> 32);
174*91f16700Schasinglulu 
175*91f16700Schasinglulu 	assert(pos < PLATFORM_CORE_COUNT);
176*91f16700Schasinglulu 
177*91f16700Schasinglulu 	hold_base[pos] = PLAT_LUA_HOLD_STATE_GO;
178*91f16700Schasinglulu 	sev();
179*91f16700Schasinglulu 
180*91f16700Schasinglulu 	mmio_write_32(rvbar_addr + pos * 8, low);
181*91f16700Schasinglulu 	mmio_write_32(rvbar_addr + pos * 8 + 4, high);
182*91f16700Schasinglulu 
183*91f16700Schasinglulu 	mmio_clrbits_32(addr, BIT(pos));
184*91f16700Schasinglulu 
185*91f16700Schasinglulu 	return rc;
186*91f16700Schasinglulu }
187*91f16700Schasinglulu 
188*91f16700Schasinglulu /**
189*91f16700Schasinglulu  * lua_pwr_domain_off() - Power off a cpu.
190*91f16700Schasinglulu  *
191*91f16700Schasinglulu  * @target_state: CPU topological state
192*91f16700Schasinglulu  *
193*91f16700Schasinglulu  * This will be call before cpu power off, need to take action to put cpu off
194*91f16700Schasinglulu  *
195*91f16700Schasinglulu  * Return: void
196*91f16700Schasinglulu  */
197*91f16700Schasinglulu static void lua_pwr_domain_off(const psci_power_state_t *target_state)
198*91f16700Schasinglulu {
199*91f16700Schasinglulu 	lua_pwr_gic_off();
200*91f16700Schasinglulu }
201*91f16700Schasinglulu 
202*91f16700Schasinglulu /**
203*91f16700Schasinglulu  * lua_pwr_domain_suspend() - Put system into suspend state.
204*91f16700Schasinglulu  *
205*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
206*91f16700Schasinglulu  *
207*91f16700Schasinglulu  * Perform the platform specific actions to prepare to suspend the calling
208*91f16700Schasinglulu  * CPU and its higher parent power domain levels as indicated by the
209*91f16700Schasinglulu  * ``target_state`` (first argument). It is called by the PSCI ``CPU_SUSPEND``
210*91f16700Schasinglulu  * API implementation.
211*91f16700Schasinglulu  *
212*91f16700Schasinglulu  * Return: void
213*91f16700Schasinglulu  */
214*91f16700Schasinglulu static void lua_pwr_domain_suspend(const psci_power_state_t *target_state)
215*91f16700Schasinglulu {
216*91f16700Schasinglulu 	assert(0);
217*91f16700Schasinglulu }
218*91f16700Schasinglulu 
219*91f16700Schasinglulu void __dead2 plat_secondary_cold_boot_setup(void);
220*91f16700Schasinglulu /**
221*91f16700Schasinglulu  * lua_pwr_down_wfi() - Powerdown the CPU
222*91f16700Schasinglulu  *
223*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
224*91f16700Schasinglulu  *
225*91f16700Schasinglulu  * Perform platform specific actions including the ``wfi`` invocation which
226*91f16700Schasinglulu  * allows the CPU to powerdown.
227*91f16700Schasinglulu  *
228*91f16700Schasinglulu  * Return: void
229*91f16700Schasinglulu  */
230*91f16700Schasinglulu static void __dead2 lua_pwr_down_wfi(const psci_power_state_t *target_state)
231*91f16700Schasinglulu {
232*91f16700Schasinglulu 	disable_mmu_el3();
233*91f16700Schasinglulu 	plat_secondary_cold_boot_setup();
234*91f16700Schasinglulu }
235*91f16700Schasinglulu 
236*91f16700Schasinglulu /**
237*91f16700Schasinglulu  * lua_pwr_domain_on_finish() - Platform handler when cpu is powered on.
238*91f16700Schasinglulu  *
239*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
240*91f16700Schasinglulu  *
241*91f16700Schasinglulu  * This function is called by the PSCI implementation after the calling CPU is
242*91f16700Schasinglulu  * powered on and released from reset in response to an earlier PSCI ``CPU_ON``
243*91f16700Schasinglulu  * call. It performs the platform-specific setup required to initialize enough
244*91f16700Schasinglulu  * state for this CPU to enter the normal world and also provide secure runtime
245*91f16700Schasinglulu  * firmware services.
246*91f16700Schasinglulu  *
247*91f16700Schasinglulu  * Return: void
248*91f16700Schasinglulu  */
249*91f16700Schasinglulu static void lua_pwr_domain_on_finish(const psci_power_state_t *target_state)
250*91f16700Schasinglulu {
251*91f16700Schasinglulu 	assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
252*91f16700Schasinglulu 					LUA_LOCAL_STATE_OFF);
253*91f16700Schasinglulu 
254*91f16700Schasinglulu 	lua_pwr_gic_on_finish();
255*91f16700Schasinglulu }
256*91f16700Schasinglulu 
257*91f16700Schasinglulu /**
258*91f16700Schasinglulu  * lua_pwr_domain_suspend_finish() - Platform handler after wakeup.
259*91f16700Schasinglulu  *
260*91f16700Schasinglulu  * @target_state: Platform coordinated target local power states
261*91f16700Schasinglulu  *
262*91f16700Schasinglulu  * Performs the platform-specific setup required to restore the saved state for
263*91f16700Schasinglulu  * this CPU to resume execution in the normal world and also provide secure
264*91f16700Schasinglulu  * runtime firmware services. This function will need to restore Distributor,
265*91f16700Schasinglulu  * Redistributors or ITS context.
266*91f16700Schasinglulu  *
267*91f16700Schasinglulu  * Return: void
268*91f16700Schasinglulu  */
269*91f16700Schasinglulu static void lua_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
270*91f16700Schasinglulu {
271*91f16700Schasinglulu 	assert(0);
272*91f16700Schasinglulu }
273*91f16700Schasinglulu 
274*91f16700Schasinglulu /**
275*91f16700Schasinglulu  * lua_system_off() - Platform handler to poweroff the system.
276*91f16700Schasinglulu  *
277*91f16700Schasinglulu  * Performs the platform-specific setup to power off the whole system.
278*91f16700Schasinglulu  * Due to we use fake shutdown for J5, this function just while (1) to wfi().
279*91f16700Schasinglulu  *
280*91f16700Schasinglulu  * Return: void
281*91f16700Schasinglulu  */
282*91f16700Schasinglulu static void __dead2 lua_system_off(void)
283*91f16700Schasinglulu {
284*91f16700Schasinglulu 	panic();
285*91f16700Schasinglulu }
286*91f16700Schasinglulu 
287*91f16700Schasinglulu /**
288*91f16700Schasinglulu  * lua_system_reset() - Platform handler to reboot the system.
289*91f16700Schasinglulu  *
290*91f16700Schasinglulu  * Performs the platform-specific setup to reboot the whole system.
291*91f16700Schasinglulu  * We need to generated a wdt reset to reset the whole soc. In case of wdt
292*91f16700Schasinglulu  * trigger failed, going to panic().
293*91f16700Schasinglulu  *
294*91f16700Schasinglulu  * Return: void
295*91f16700Schasinglulu  */
296*91f16700Schasinglulu static void __dead2 lua_system_reset(void)
297*91f16700Schasinglulu {
298*91f16700Schasinglulu 	panic();
299*91f16700Schasinglulu }
300*91f16700Schasinglulu 
301*91f16700Schasinglulu /**
302*91f16700Schasinglulu  * lua_psci_ops - j6 platform psci ops
303*91f16700Schasinglulu  */
304*91f16700Schasinglulu static plat_psci_ops_t lua_psci_ops = {
305*91f16700Schasinglulu 	.cpu_standby = lua_cpu_standby,
306*91f16700Schasinglulu 	.pwr_domain_on = lua_pwr_domain_on,
307*91f16700Schasinglulu 	.pwr_domain_off = lua_pwr_domain_off,
308*91f16700Schasinglulu 	.pwr_domain_suspend = lua_pwr_domain_suspend,
309*91f16700Schasinglulu 	.pwr_domain_pwr_down_wfi = lua_pwr_down_wfi,
310*91f16700Schasinglulu 	.pwr_domain_on_finish = lua_pwr_domain_on_finish,
311*91f16700Schasinglulu 	.pwr_domain_suspend_finish = lua_pwr_domain_suspend_finish,
312*91f16700Schasinglulu 	.system_off = lua_system_off,
313*91f16700Schasinglulu 	.system_reset = lua_system_reset,
314*91f16700Schasinglulu 	.validate_power_state = lua_validate_power_state,
315*91f16700Schasinglulu 	.validate_ns_entrypoint = lua_validate_ns_entrypoint,
316*91f16700Schasinglulu };
317*91f16700Schasinglulu 
318*91f16700Schasinglulu /**
319*91f16700Schasinglulu  * plat_setup_psci_ops() - Platform handler to setup psci ops
320*91f16700Schasinglulu  *
321*91f16700Schasinglulu  * @sec_entrypoint: entry to going to after cpu powered on.
322*91f16700Schasinglulu  * @psci_ops: pointer to store psci ops.
323*91f16700Schasinglulu  *
324*91f16700Schasinglulu  * This function need to initialze
325*91f16700Schasinglulu  *
326*91f16700Schasinglulu  * Return: void.
327*91f16700Schasinglulu  */
328*91f16700Schasinglulu int plat_setup_psci_ops(uintptr_t sec_entrypoint,
329*91f16700Schasinglulu 			const plat_psci_ops_t **psci_ops)
330*91f16700Schasinglulu {
331*91f16700Schasinglulu 	uintptr_t *mailbox = (void *)PLAT_LUA_TRUSTED_MAILBOX_BASE;
332*91f16700Schasinglulu 
333*91f16700Schasinglulu 	*mailbox = sec_entrypoint;
334*91f16700Schasinglulu 	secure_entrypoint = (unsigned long)sec_entrypoint;
335*91f16700Schasinglulu 
336*91f16700Schasinglulu 	assert(psci_ops);
337*91f16700Schasinglulu 	*psci_ops = &lua_psci_ops;
338*91f16700Schasinglulu 
339*91f16700Schasinglulu 	return 0;
340*91f16700Schasinglulu }
341