xref: /rk3399_ARM-atf/lib/psci/psci_private.h (revision 97373c33b70725daf58e4491831537392c0d5239)
1532ed618SSoby Mathew /*
29fb8af33SRoberto Vargas  * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7*97373c33SAntonio Nino Diaz #ifndef PSCI_PRIVATE_H
8*97373c33SAntonio Nino Diaz #define PSCI_PRIVATE_H
9532ed618SSoby Mathew 
10532ed618SSoby Mathew #include <arch.h>
11532ed618SSoby Mathew #include <bakery_lock.h>
12532ed618SSoby Mathew #include <bl_common.h>
13532ed618SSoby Mathew #include <cpu_data.h>
14532ed618SSoby Mathew #include <psci.h>
15532ed618SSoby Mathew #include <spinlock.h>
16532ed618SSoby Mathew 
17a10d3632SJeenu Viswambharan #if HW_ASSISTED_COHERENCY
18b0408e87SJeenu Viswambharan 
19a10d3632SJeenu Viswambharan /*
20a10d3632SJeenu Viswambharan  * On systems with hardware-assisted coherency, make PSCI cache operations NOP,
21a10d3632SJeenu Viswambharan  * as PSCI participants are cache-coherent, and there's no need for explicit
22a10d3632SJeenu Viswambharan  * cache maintenance operations or barriers to coordinate their state.
23a10d3632SJeenu Viswambharan  */
24a10d3632SJeenu Viswambharan #define psci_flush_dcache_range(addr, size)
25a10d3632SJeenu Viswambharan #define psci_flush_cpu_data(member)
26a10d3632SJeenu Viswambharan #define psci_inv_cpu_data(member)
27a10d3632SJeenu Viswambharan 
28a10d3632SJeenu Viswambharan #define psci_dsbish()
29b0408e87SJeenu Viswambharan 
30b0408e87SJeenu Viswambharan /*
31b0408e87SJeenu Viswambharan  * On systems where participant CPUs are cache-coherent, we can use spinlocks
32b0408e87SJeenu Viswambharan  * instead of bakery locks.
33b0408e87SJeenu Viswambharan  */
34b0408e87SJeenu Viswambharan #define DEFINE_PSCI_LOCK(_name)		spinlock_t _name
35b0408e87SJeenu Viswambharan #define DECLARE_PSCI_LOCK(_name)	extern DEFINE_PSCI_LOCK(_name)
36b0408e87SJeenu Viswambharan 
37b0408e87SJeenu Viswambharan #define psci_lock_get(non_cpu_pd_node)				\
38b0408e87SJeenu Viswambharan 	spin_lock(&psci_locks[(non_cpu_pd_node)->lock_index])
39b0408e87SJeenu Viswambharan #define psci_lock_release(non_cpu_pd_node)			\
40b0408e87SJeenu Viswambharan 	spin_unlock(&psci_locks[(non_cpu_pd_node)->lock_index])
41b0408e87SJeenu Viswambharan 
42a10d3632SJeenu Viswambharan #else
43b0408e87SJeenu Viswambharan 
44a10d3632SJeenu Viswambharan /*
45a10d3632SJeenu Viswambharan  * If not all PSCI participants are cache-coherent, perform cache maintenance
46a10d3632SJeenu Viswambharan  * and issue barriers wherever required to coordinate state.
47a10d3632SJeenu Viswambharan  */
48a10d3632SJeenu Viswambharan #define psci_flush_dcache_range(addr, size)	flush_dcache_range(addr, size)
49a10d3632SJeenu Viswambharan #define psci_flush_cpu_data(member)		flush_cpu_data(member)
50a10d3632SJeenu Viswambharan #define psci_inv_cpu_data(member)		inv_cpu_data(member)
51a10d3632SJeenu Viswambharan 
52a10d3632SJeenu Viswambharan #define psci_dsbish()				dsbish()
53a10d3632SJeenu Viswambharan 
54532ed618SSoby Mathew /*
55b0408e87SJeenu Viswambharan  * Use bakery locks for state coordination as not all PSCI participants are
56b0408e87SJeenu Viswambharan  * cache coherent.
57532ed618SSoby Mathew  */
58b0408e87SJeenu Viswambharan #define DEFINE_PSCI_LOCK(_name)		DEFINE_BAKERY_LOCK(_name)
59b0408e87SJeenu Viswambharan #define DECLARE_PSCI_LOCK(_name)	DECLARE_BAKERY_LOCK(_name)
60b0408e87SJeenu Viswambharan 
61532ed618SSoby Mathew #define psci_lock_get(non_cpu_pd_node)				\
62532ed618SSoby Mathew 	bakery_lock_get(&psci_locks[(non_cpu_pd_node)->lock_index])
63532ed618SSoby Mathew #define psci_lock_release(non_cpu_pd_node)			\
64532ed618SSoby Mathew 	bakery_lock_release(&psci_locks[(non_cpu_pd_node)->lock_index])
65532ed618SSoby Mathew 
66b0408e87SJeenu Viswambharan #endif
67b0408e87SJeenu Viswambharan 
68896a5902SDaniel Boulby #define psci_lock_init(_non_cpu_pd_node, _idx)			\
69896a5902SDaniel Boulby 	((_non_cpu_pd_node)[(_idx)].lock_index = (_idx))
70b0408e87SJeenu Viswambharan 
71532ed618SSoby Mathew /*
72532ed618SSoby Mathew  * The PSCI capability which are provided by the generic code but does not
73532ed618SSoby Mathew  * depend on the platform or spd capabilities.
74532ed618SSoby Mathew  */
75532ed618SSoby Mathew #define PSCI_GENERIC_CAP	\
76532ed618SSoby Mathew 			(define_psci_cap(PSCI_VERSION) |		\
77532ed618SSoby Mathew 			define_psci_cap(PSCI_AFFINITY_INFO_AARCH64) |	\
78532ed618SSoby Mathew 			define_psci_cap(PSCI_FEATURES))
79532ed618SSoby Mathew 
80532ed618SSoby Mathew /*
81532ed618SSoby Mathew  * The PSCI capabilities mask for 64 bit functions.
82532ed618SSoby Mathew  */
83532ed618SSoby Mathew #define PSCI_CAP_64BIT_MASK	\
84532ed618SSoby Mathew 			(define_psci_cap(PSCI_CPU_SUSPEND_AARCH64) |	\
85532ed618SSoby Mathew 			define_psci_cap(PSCI_CPU_ON_AARCH64) |		\
86532ed618SSoby Mathew 			define_psci_cap(PSCI_AFFINITY_INFO_AARCH64) |	\
87532ed618SSoby Mathew 			define_psci_cap(PSCI_MIG_AARCH64) |		\
88532ed618SSoby Mathew 			define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64) |	\
8928d3d614SJeenu Viswambharan 			define_psci_cap(PSCI_NODE_HW_STATE_AARCH64) |	\
90532ed618SSoby Mathew 			define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64) |	\
91532ed618SSoby Mathew 			define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64) |	\
9236a8f8fdSRoberto Vargas 			define_psci_cap(PSCI_STAT_COUNT_AARCH64) |	\
934ce9b8eaSRoberto Vargas 			define_psci_cap(PSCI_SYSTEM_RESET2_AARCH64) |	\
944ce9b8eaSRoberto Vargas 			define_psci_cap(PSCI_MEM_CHK_RANGE_AARCH64))
95532ed618SSoby Mathew 
96532ed618SSoby Mathew /*
97*97373c33SAntonio Nino Diaz  * Helper functions to get/set the fields of PSCI per-cpu data.
98532ed618SSoby Mathew  */
99*97373c33SAntonio Nino Diaz static inline void psci_set_aff_info_state(aff_info_state_t aff_state)
100*97373c33SAntonio Nino Diaz {
101*97373c33SAntonio Nino Diaz 	set_cpu_data(psci_svc_cpu_data.aff_info_state, aff_state);
102*97373c33SAntonio Nino Diaz }
103532ed618SSoby Mathew 
104*97373c33SAntonio Nino Diaz static inline aff_info_state_t psci_get_aff_info_state(void)
105*97373c33SAntonio Nino Diaz {
106*97373c33SAntonio Nino Diaz 	return get_cpu_data(psci_svc_cpu_data.aff_info_state);
107*97373c33SAntonio Nino Diaz }
108532ed618SSoby Mathew 
109*97373c33SAntonio Nino Diaz static inline aff_info_state_t psci_get_aff_info_state_by_idx(int idx)
110*97373c33SAntonio Nino Diaz {
111*97373c33SAntonio Nino Diaz 	return get_cpu_data_by_index((unsigned int)idx,
112*97373c33SAntonio Nino Diaz 				     psci_svc_cpu_data.aff_info_state);
113*97373c33SAntonio Nino Diaz }
114*97373c33SAntonio Nino Diaz 
115*97373c33SAntonio Nino Diaz static inline void psci_set_aff_info_state_by_idx(int idx,
116*97373c33SAntonio Nino Diaz 						  aff_info_state_t aff_state)
117*97373c33SAntonio Nino Diaz {
118*97373c33SAntonio Nino Diaz 	set_cpu_data_by_index((unsigned int)idx,
119*97373c33SAntonio Nino Diaz 			      psci_svc_cpu_data.aff_info_state, aff_state);
120*97373c33SAntonio Nino Diaz }
121*97373c33SAntonio Nino Diaz 
122*97373c33SAntonio Nino Diaz static inline unsigned int psci_get_suspend_pwrlvl(void)
123*97373c33SAntonio Nino Diaz {
124*97373c33SAntonio Nino Diaz 	return get_cpu_data(psci_svc_cpu_data.target_pwrlvl);
125*97373c33SAntonio Nino Diaz }
126*97373c33SAntonio Nino Diaz 
127*97373c33SAntonio Nino Diaz static inline void psci_set_suspend_pwrlvl(unsigned int target_lvl)
128*97373c33SAntonio Nino Diaz {
129*97373c33SAntonio Nino Diaz 	set_cpu_data(psci_svc_cpu_data.target_pwrlvl, target_lvl);
130*97373c33SAntonio Nino Diaz }
131*97373c33SAntonio Nino Diaz 
132*97373c33SAntonio Nino Diaz static inline void psci_set_cpu_local_state(plat_local_state_t state)
133*97373c33SAntonio Nino Diaz {
134*97373c33SAntonio Nino Diaz 	set_cpu_data(psci_svc_cpu_data.local_state, state);
135*97373c33SAntonio Nino Diaz }
136*97373c33SAntonio Nino Diaz 
137*97373c33SAntonio Nino Diaz static inline plat_local_state_t psci_get_cpu_local_state(void)
138*97373c33SAntonio Nino Diaz {
139*97373c33SAntonio Nino Diaz 	return get_cpu_data(psci_svc_cpu_data.local_state);
140*97373c33SAntonio Nino Diaz }
141*97373c33SAntonio Nino Diaz 
142*97373c33SAntonio Nino Diaz static inline plat_local_state_t psci_get_cpu_local_state_by_idx(int idx)
143*97373c33SAntonio Nino Diaz {
144*97373c33SAntonio Nino Diaz 	return get_cpu_data_by_index((unsigned int)idx,
145*97373c33SAntonio Nino Diaz 				     psci_svc_cpu_data.local_state);
146*97373c33SAntonio Nino Diaz }
147*97373c33SAntonio Nino Diaz 
148*97373c33SAntonio Nino Diaz /* Helper function to identify a CPU standby request in PSCI Suspend call */
149*97373c33SAntonio Nino Diaz static inline int is_cpu_standby_req(unsigned int is_power_down_state,
150*97373c33SAntonio Nino Diaz 				     unsigned int retn_lvl)
151*97373c33SAntonio Nino Diaz {
152*97373c33SAntonio Nino Diaz 	return ((is_power_down_state == 0U) && (retn_lvl == 0U)) ? 1 : 0;
153*97373c33SAntonio Nino Diaz }
154532ed618SSoby Mathew 
155532ed618SSoby Mathew /*******************************************************************************
156532ed618SSoby Mathew  * The following two data structures implement the power domain tree. The tree
157532ed618SSoby Mathew  * is used to track the state of all the nodes i.e. power domain instances
158532ed618SSoby Mathew  * described by the platform. The tree consists of nodes that describe CPU power
159532ed618SSoby Mathew  * domains i.e. leaf nodes and all other power domains which are parents of a
160532ed618SSoby Mathew  * CPU power domain i.e. non-leaf nodes.
161532ed618SSoby Mathew  ******************************************************************************/
162532ed618SSoby Mathew typedef struct non_cpu_pwr_domain_node {
163532ed618SSoby Mathew 	/*
164532ed618SSoby Mathew 	 * Index of the first CPU power domain node level 0 which has this node
165532ed618SSoby Mathew 	 * as its parent.
166532ed618SSoby Mathew 	 */
167532ed618SSoby Mathew 	unsigned int cpu_start_idx;
168532ed618SSoby Mathew 
169532ed618SSoby Mathew 	/*
170532ed618SSoby Mathew 	 * Number of CPU power domains which are siblings of the domain indexed
171532ed618SSoby Mathew 	 * by 'cpu_start_idx' i.e. all the domains in the range 'cpu_start_idx
172532ed618SSoby Mathew 	 * -> cpu_start_idx + ncpus' have this node as their parent.
173532ed618SSoby Mathew 	 */
174532ed618SSoby Mathew 	unsigned int ncpus;
175532ed618SSoby Mathew 
176532ed618SSoby Mathew 	/*
177532ed618SSoby Mathew 	 * Index of the parent power domain node.
178532ed618SSoby Mathew 	 * TODO: Figure out whether to whether using pointer is more efficient.
179532ed618SSoby Mathew 	 */
180532ed618SSoby Mathew 	unsigned int parent_node;
181532ed618SSoby Mathew 
182532ed618SSoby Mathew 	plat_local_state_t local_state;
183532ed618SSoby Mathew 
184532ed618SSoby Mathew 	unsigned char level;
185532ed618SSoby Mathew 
186532ed618SSoby Mathew 	/* For indexing the psci_lock array*/
187532ed618SSoby Mathew 	unsigned char lock_index;
188532ed618SSoby Mathew } non_cpu_pd_node_t;
189532ed618SSoby Mathew 
190532ed618SSoby Mathew typedef struct cpu_pwr_domain_node {
191532ed618SSoby Mathew 	u_register_t mpidr;
192532ed618SSoby Mathew 
193532ed618SSoby Mathew 	/*
194532ed618SSoby Mathew 	 * Index of the parent power domain node.
195532ed618SSoby Mathew 	 * TODO: Figure out whether to whether using pointer is more efficient.
196532ed618SSoby Mathew 	 */
197532ed618SSoby Mathew 	unsigned int parent_node;
198532ed618SSoby Mathew 
199532ed618SSoby Mathew 	/*
200532ed618SSoby Mathew 	 * A CPU power domain does not require state coordination like its
201532ed618SSoby Mathew 	 * parent power domains. Hence this node does not include a bakery
202532ed618SSoby Mathew 	 * lock. A spinlock is required by the CPU_ON handler to prevent a race
203532ed618SSoby Mathew 	 * when multiple CPUs try to turn ON the same target CPU.
204532ed618SSoby Mathew 	 */
205532ed618SSoby Mathew 	spinlock_t cpu_lock;
206532ed618SSoby Mathew } cpu_pd_node_t;
207532ed618SSoby Mathew 
208532ed618SSoby Mathew /*******************************************************************************
209532ed618SSoby Mathew  * Data prototypes
210532ed618SSoby Mathew  ******************************************************************************/
211532ed618SSoby Mathew extern const plat_psci_ops_t *psci_plat_pm_ops;
212532ed618SSoby Mathew extern non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS];
213532ed618SSoby Mathew extern cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
214532ed618SSoby Mathew extern unsigned int psci_caps;
215532ed618SSoby Mathew 
216b0408e87SJeenu Viswambharan /* One lock is required per non-CPU power domain node */
217b0408e87SJeenu Viswambharan DECLARE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
218532ed618SSoby Mathew 
219532ed618SSoby Mathew /*******************************************************************************
220532ed618SSoby Mathew  * SPD's power management hooks registered with PSCI
221532ed618SSoby Mathew  ******************************************************************************/
222532ed618SSoby Mathew extern const spd_pm_ops_t *psci_spd_pm;
223532ed618SSoby Mathew 
224532ed618SSoby Mathew /*******************************************************************************
225532ed618SSoby Mathew  * Function prototypes
226532ed618SSoby Mathew  ******************************************************************************/
227532ed618SSoby Mathew /* Private exported functions from psci_common.c */
228532ed618SSoby Mathew int psci_validate_power_state(unsigned int power_state,
229532ed618SSoby Mathew 			      psci_power_state_t *state_info);
230532ed618SSoby Mathew void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info);
231532ed618SSoby Mathew int psci_validate_mpidr(u_register_t mpidr);
232532ed618SSoby Mathew void psci_init_req_local_pwr_states(void);
23361eae524SAchin Gupta void psci_get_target_local_pwr_states(unsigned int end_pwrlvl,
23461eae524SAchin Gupta 				      psci_power_state_t *target_state);
235532ed618SSoby Mathew int psci_validate_entry_point(entry_point_info_t *ep,
236532ed618SSoby Mathew 			uintptr_t entrypoint, u_register_t context_id);
237532ed618SSoby Mathew void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
238532ed618SSoby Mathew 				      unsigned int end_lvl,
239532ed618SSoby Mathew 				      unsigned int node_index[]);
240532ed618SSoby Mathew void psci_do_state_coordination(unsigned int end_pwrlvl,
241532ed618SSoby Mathew 				psci_power_state_t *state_info);
242532ed618SSoby Mathew void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
243532ed618SSoby Mathew 				   unsigned int cpu_idx);
244532ed618SSoby Mathew void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
245532ed618SSoby Mathew 				   unsigned int cpu_idx);
246532ed618SSoby Mathew int psci_validate_suspend_req(const psci_power_state_t *state_info,
2479fb8af33SRoberto Vargas 			      unsigned int is_power_down_state);
248532ed618SSoby Mathew unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info);
249532ed618SSoby Mathew unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info);
250532ed618SSoby Mathew void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl);
251532ed618SSoby Mathew void psci_print_power_domain_map(void);
252532ed618SSoby Mathew unsigned int psci_is_last_on_cpu(void);
253532ed618SSoby Mathew int psci_spd_migrate_info(u_register_t *mpidr);
254b0408e87SJeenu Viswambharan void psci_do_pwrdown_sequence(unsigned int power_level);
255b0408e87SJeenu Viswambharan 
256b0408e87SJeenu Viswambharan /*
257b0408e87SJeenu Viswambharan  * CPU power down is directly called only when HW_ASSISTED_COHERENCY is
258b0408e87SJeenu Viswambharan  * available. Otherwise, this needs post-call stack maintenance, which is
259b0408e87SJeenu Viswambharan  * handled in assembly.
260b0408e87SJeenu Viswambharan  */
261b0408e87SJeenu Viswambharan void prepare_cpu_pwr_dwn(unsigned int power_level);
262532ed618SSoby Mathew 
263532ed618SSoby Mathew /* Private exported functions from psci_on.c */
264532ed618SSoby Mathew int psci_cpu_on_start(u_register_t target_cpu,
265532ed618SSoby Mathew 		      entry_point_info_t *ep);
266532ed618SSoby Mathew 
267532ed618SSoby Mathew void psci_cpu_on_finish(unsigned int cpu_idx,
268532ed618SSoby Mathew 			psci_power_state_t *state_info);
269532ed618SSoby Mathew 
270532ed618SSoby Mathew /* Private exported functions from psci_off.c */
271532ed618SSoby Mathew int psci_do_cpu_off(unsigned int end_pwrlvl);
272532ed618SSoby Mathew 
273532ed618SSoby Mathew /* Private exported functions from psci_suspend.c */
274532ed618SSoby Mathew void psci_cpu_suspend_start(entry_point_info_t *ep,
275532ed618SSoby Mathew 			unsigned int end_pwrlvl,
276532ed618SSoby Mathew 			psci_power_state_t *state_info,
2779fb8af33SRoberto Vargas 			unsigned int is_power_down_state);
278532ed618SSoby Mathew 
279532ed618SSoby Mathew void psci_cpu_suspend_finish(unsigned int cpu_idx,
280532ed618SSoby Mathew 			psci_power_state_t *state_info);
281532ed618SSoby Mathew 
282532ed618SSoby Mathew /* Private exported functions from psci_helpers.S */
283532ed618SSoby Mathew void psci_do_pwrdown_cache_maintenance(unsigned int pwr_level);
284532ed618SSoby Mathew void psci_do_pwrup_cache_maintenance(void);
285532ed618SSoby Mathew 
286532ed618SSoby Mathew /* Private exported functions from psci_system_off.c */
287532ed618SSoby Mathew void __dead2 psci_system_off(void);
288532ed618SSoby Mathew void __dead2 psci_system_reset(void);
28936a8f8fdSRoberto Vargas int psci_system_reset2(uint32_t reset_type, u_register_t cookie);
290532ed618SSoby Mathew 
291532ed618SSoby Mathew /* Private exported functions from psci_stat.c */
292532ed618SSoby Mathew void psci_stats_update_pwr_down(unsigned int end_pwrlvl,
293532ed618SSoby Mathew 			const psci_power_state_t *state_info);
294532ed618SSoby Mathew void psci_stats_update_pwr_up(unsigned int end_pwrlvl,
29504c1db1eSdp-arm 			const psci_power_state_t *state_info);
296532ed618SSoby Mathew u_register_t psci_stat_residency(u_register_t target_cpu,
297532ed618SSoby Mathew 			unsigned int power_state);
298532ed618SSoby Mathew u_register_t psci_stat_count(u_register_t target_cpu,
299532ed618SSoby Mathew 			unsigned int power_state);
300532ed618SSoby Mathew 
301d4c596beSRoberto Vargas /* Private exported functions from psci_mem_protect.c */
302d4c596beSRoberto Vargas int psci_mem_protect(unsigned int enable);
303d4c596beSRoberto Vargas int psci_mem_chk_range(uintptr_t base, u_register_t length);
304d4c596beSRoberto Vargas 
305*97373c33SAntonio Nino Diaz #endif /* PSCI_PRIVATE_H */
306