xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision 38dce70f51fb83b27958ba3e2ad15f5635cb1061)
1b4315306SDan Handley /*
2b4315306SDan Handley  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
4b4315306SDan Handley  * Redistribution and use in source and binary forms, with or without
5b4315306SDan Handley  * modification, are permitted provided that the following conditions are met:
6b4315306SDan Handley  *
7b4315306SDan Handley  * Redistributions of source code must retain the above copyright notice, this
8b4315306SDan Handley  * list of conditions and the following disclaimer.
9b4315306SDan Handley  *
10b4315306SDan Handley  * Redistributions in binary form must reproduce the above copyright notice,
11b4315306SDan Handley  * this list of conditions and the following disclaimer in the documentation
12b4315306SDan Handley  * and/or other materials provided with the distribution.
13b4315306SDan Handley  *
14b4315306SDan Handley  * Neither the name of ARM nor the names of its contributors may be used
15b4315306SDan Handley  * to endorse or promote products derived from this software without specific
16b4315306SDan Handley  * prior written permission.
17b4315306SDan Handley  *
18b4315306SDan Handley  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19b4315306SDan Handley  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b4315306SDan Handley  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b4315306SDan Handley  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22b4315306SDan Handley  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23b4315306SDan Handley  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b4315306SDan Handley  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b4315306SDan Handley  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26b4315306SDan Handley  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b4315306SDan Handley  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28b4315306SDan Handley  * POSSIBILITY OF SUCH DAMAGE.
29b4315306SDan Handley  */
30b4315306SDan Handley 
31b4315306SDan Handley #include <assert.h>
32b4315306SDan Handley #include <arch_helpers.h>
33b4315306SDan Handley #include <arm_gic.h>
34b4315306SDan Handley #include <cci.h>
35b4315306SDan Handley #include <css_def.h>
36b4315306SDan Handley #include <debug.h>
37b4315306SDan Handley #include <errno.h>
38b4315306SDan Handley #include <plat_arm.h>
39b4315306SDan Handley #include <platform.h>
40b4315306SDan Handley #include <platform_def.h>
41b4315306SDan Handley #include <psci.h>
42b4315306SDan Handley #include "css_scpi.h"
43b4315306SDan Handley 
44*38dce70fSSoby Mathew unsigned long wakeup_address;
45*38dce70fSSoby Mathew 
46b4315306SDan Handley /*******************************************************************************
47b4315306SDan Handley  * Private function to program the mailbox for a cpu before it is released
48b4315306SDan Handley  * from reset.
49b4315306SDan Handley  ******************************************************************************/
50b4315306SDan Handley static void css_program_mailbox(uint64_t mpidr, uint64_t address)
51b4315306SDan Handley {
52b4315306SDan Handley 	uint64_t linear_id;
53b4315306SDan Handley 	uint64_t mbox;
54b4315306SDan Handley 
55*38dce70fSSoby Mathew 	linear_id = plat_arm_calc_core_pos(mpidr);
56b4315306SDan Handley 	mbox = TRUSTED_MAILBOXES_BASE +	(linear_id << TRUSTED_MAILBOX_SHIFT);
57b4315306SDan Handley 	*((uint64_t *) mbox) = address;
58b4315306SDan Handley 	flush_dcache_range(mbox, sizeof(mbox));
59b4315306SDan Handley }
60b4315306SDan Handley 
61b4315306SDan Handley /*******************************************************************************
62*38dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
63b4315306SDan Handley  * level and mpidr determine the affinity instance.
64b4315306SDan Handley  ******************************************************************************/
65*38dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
66b4315306SDan Handley {
67b4315306SDan Handley 	/*
68*38dce70fSSoby Mathew 	 * SCP takes care of powering up parent power domains so we
69b4315306SDan Handley 	 * only need to care about level 0
70b4315306SDan Handley 	 */
71b4315306SDan Handley 
72b4315306SDan Handley 	/*
73b4315306SDan Handley 	 * Setup mailbox with address for CPU entrypoint when it next powers up
74b4315306SDan Handley 	 */
75*38dce70fSSoby Mathew 	css_program_mailbox(mpidr, wakeup_address);
76b4315306SDan Handley 
77b4315306SDan Handley 	scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on,
78b4315306SDan Handley 				 scpi_power_on);
79b4315306SDan Handley 
80b4315306SDan Handley 	return PSCI_E_SUCCESS;
81b4315306SDan Handley }
82b4315306SDan Handley 
83b4315306SDan Handley /*******************************************************************************
84*38dce70fSSoby Mathew  * Handler called when a power level has just been powered on after
85*38dce70fSSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
86*38dce70fSSoby Mathew  * each level has woken up from.
87b4315306SDan Handley  ******************************************************************************/
88*38dce70fSSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
89b4315306SDan Handley {
90*38dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
91*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
92b4315306SDan Handley 
93b4315306SDan Handley 	/*
94b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
95b4315306SDan Handley 	 * if this cluster was off.
96b4315306SDan Handley 	 */
97*38dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
98*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF)
99*38dce70fSSoby Mathew 		cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
100b4315306SDan Handley 
101b4315306SDan Handley 	/* Enable the gic cpu interface */
102b4315306SDan Handley 	arm_gic_cpuif_setup();
103b4315306SDan Handley 
104b4315306SDan Handley 	/* todo: Is this setup only needed after a cold boot? */
105b4315306SDan Handley 	arm_gic_pcpu_distif_setup();
106b4315306SDan Handley 
107b4315306SDan Handley 	/* Clear the mailbox for this cpu. */
108*38dce70fSSoby Mathew 	css_program_mailbox(read_mpidr_el1(), 0);
109b4315306SDan Handley }
110b4315306SDan Handley 
111b4315306SDan Handley /*******************************************************************************
112b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
113b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
114*38dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
115*38dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
116b4315306SDan Handley  ******************************************************************************/
117*38dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
118b4315306SDan Handley {
119b4315306SDan Handley 	uint32_t cluster_state = scpi_power_on;
120b4315306SDan Handley 
121b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
122b4315306SDan Handley 	arm_gic_cpuif_deactivate();
123b4315306SDan Handley 
124b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
125*38dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
126*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF) {
127b4315306SDan Handley 		cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
128b4315306SDan Handley 		cluster_state = scpi_power_off;
129b4315306SDan Handley 	}
130b4315306SDan Handley 
131b4315306SDan Handley 	/*
132b4315306SDan Handley 	 * Ask the SCP to power down the appropriate components depending upon
133b4315306SDan Handley 	 * their state.
134b4315306SDan Handley 	 */
135b4315306SDan Handley 	scpi_set_css_power_state(read_mpidr_el1(),
136b4315306SDan Handley 				 scpi_power_off,
137b4315306SDan Handley 				 cluster_state,
138b4315306SDan Handley 				 scpi_power_on);
139b4315306SDan Handley }
140b4315306SDan Handley 
141b4315306SDan Handley /*******************************************************************************
142*38dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
143*38dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
144b4315306SDan Handley  ******************************************************************************/
145*38dce70fSSoby Mathew static void css_pwr_domain_off(const psci_power_state_t *target_state)
146b4315306SDan Handley {
147*38dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
148*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
149b4315306SDan Handley 
150*38dce70fSSoby Mathew 	css_power_down_common(target_state);
151b4315306SDan Handley }
152b4315306SDan Handley 
153b4315306SDan Handley /*******************************************************************************
154*38dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
155*38dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
156b4315306SDan Handley  ******************************************************************************/
157*38dce70fSSoby Mathew static void css_pwr_domain_suspend(const psci_power_state_t *target_state)
158b4315306SDan Handley {
159*38dce70fSSoby Mathew 	/*
160*38dce70fSSoby Mathew 	 * Juno has retention only at cpu level. Just return
161*38dce70fSSoby Mathew 	 * as nothing is to be done for retention.
162*38dce70fSSoby Mathew 	 */
163*38dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
164*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
165b4315306SDan Handley 		return;
166b4315306SDan Handley 
167*38dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
168*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
169*38dce70fSSoby Mathew 
170b4315306SDan Handley 	/*
171b4315306SDan Handley 	 * Setup mailbox with address for CPU entrypoint when it next powers up.
172b4315306SDan Handley 	 */
173*38dce70fSSoby Mathew 	css_program_mailbox(read_mpidr_el1(), wakeup_address);
174b4315306SDan Handley 
175*38dce70fSSoby Mathew 	css_power_down_common(target_state);
176b4315306SDan Handley }
177b4315306SDan Handley 
178b4315306SDan Handley /*******************************************************************************
179*38dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
180*38dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
181*38dce70fSSoby Mathew  * that each level has woken up from.
182b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
183b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
184b4315306SDan Handley  ******************************************************************************/
185*38dce70fSSoby Mathew static void css_pwr_domain_suspend_finish(
186*38dce70fSSoby Mathew 				const psci_power_state_t *target_state)
187b4315306SDan Handley {
188*38dce70fSSoby Mathew 	/*
189*38dce70fSSoby Mathew 	 * Return as nothing is to be done on waking up from retention.
190*38dce70fSSoby Mathew 	 */
191*38dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
192*38dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
193*38dce70fSSoby Mathew 		return;
194*38dce70fSSoby Mathew 
195*38dce70fSSoby Mathew 	css_pwr_domain_on_finish(target_state);
196b4315306SDan Handley }
197b4315306SDan Handley 
198b4315306SDan Handley /*******************************************************************************
199b4315306SDan Handley  * Handlers to shutdown/reboot the system
200b4315306SDan Handley  ******************************************************************************/
201b4315306SDan Handley static void __dead2 css_system_off(void)
202b4315306SDan Handley {
203b4315306SDan Handley 	uint32_t response;
204b4315306SDan Handley 
205b4315306SDan Handley 	/* Send the power down request to the SCP */
206b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_shutdown);
207b4315306SDan Handley 
208b4315306SDan Handley 	if (response != SCP_OK) {
209b4315306SDan Handley 		ERROR("CSS System Off: SCP error %u.\n", response);
210b4315306SDan Handley 		panic();
211b4315306SDan Handley 	}
212b4315306SDan Handley 	wfi();
213b4315306SDan Handley 	ERROR("CSS System Off: operation not handled.\n");
214b4315306SDan Handley 	panic();
215b4315306SDan Handley }
216b4315306SDan Handley 
217b4315306SDan Handley static void __dead2 css_system_reset(void)
218b4315306SDan Handley {
219b4315306SDan Handley 	uint32_t response;
220b4315306SDan Handley 
221b4315306SDan Handley 	/* Send the system reset request to the SCP */
222b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_reboot);
223b4315306SDan Handley 
224b4315306SDan Handley 	if (response != SCP_OK) {
225b4315306SDan Handley 		ERROR("CSS System Reset: SCP error %u.\n", response);
226b4315306SDan Handley 		panic();
227b4315306SDan Handley 	}
228b4315306SDan Handley 	wfi();
229b4315306SDan Handley 	ERROR("CSS System Reset: operation not handled.\n");
230b4315306SDan Handley 	panic();
231b4315306SDan Handley }
232b4315306SDan Handley 
233b4315306SDan Handley /*******************************************************************************
234*38dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
235b4315306SDan Handley  ******************************************************************************/
236*38dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
237b4315306SDan Handley {
238b4315306SDan Handley 	unsigned int scr;
239b4315306SDan Handley 
240*38dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
241*38dce70fSSoby Mathew 
242b4315306SDan Handley 	scr = read_scr_el3();
243b4315306SDan Handley 	/* Enable PhysicalIRQ bit for NS world to wake the CPU */
244b4315306SDan Handley 	write_scr_el3(scr | SCR_IRQ_BIT);
245b4315306SDan Handley 	isb();
246b4315306SDan Handley 	dsb();
247b4315306SDan Handley 	wfi();
248b4315306SDan Handley 
249b4315306SDan Handley 	/*
250b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
251b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
252b4315306SDan Handley 	 */
253b4315306SDan Handley 	write_scr_el3(scr);
254b4315306SDan Handley }
255b4315306SDan Handley 
256b4315306SDan Handley /*******************************************************************************
257b4315306SDan Handley  * Export the platform handlers to enable psci to invoke them
258b4315306SDan Handley  ******************************************************************************/
259*38dce70fSSoby Mathew static const plat_psci_ops_t css_ops = {
260*38dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
261*38dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
262*38dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
263*38dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
264*38dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
265*38dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
266b4315306SDan Handley 	.system_off		= css_system_off,
267b4315306SDan Handley 	.system_reset		= css_system_reset,
268b4315306SDan Handley 	.validate_power_state	= arm_validate_power_state
269b4315306SDan Handley };
270b4315306SDan Handley 
271b4315306SDan Handley /*******************************************************************************
272*38dce70fSSoby Mathew  * Export the platform specific psci ops.
273b4315306SDan Handley  ******************************************************************************/
274*38dce70fSSoby Mathew int plat_setup_psci_ops(uintptr_t sec_entrypoint,
275*38dce70fSSoby Mathew 				const plat_psci_ops_t **psci_ops)
276b4315306SDan Handley {
277*38dce70fSSoby Mathew 	*psci_ops = &css_ops;
278*38dce70fSSoby Mathew 
279*38dce70fSSoby Mathew 	wakeup_address = sec_entrypoint;
280*38dce70fSSoby Mathew 	flush_dcache_range((unsigned long)&wakeup_address,
281*38dce70fSSoby Mathew 				sizeof(wakeup_address));
282b4315306SDan Handley 	return 0;
283b4315306SDan Handley }
284