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