blob: d2d6901444f206d3151e2520e8da4de15b6a7f56 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __POWER_MANAGEMENT_H__
8#define __POWER_MANAGEMENT_H__
9
10#include <platform_def.h>
11#include <psci.h>
12#include <spinlock.h>
13#include <stdint.h>
14#include <types.h>
15
16/* Set of states of an affinity node as seen by the Test Framework */
17typedef enum {
18 TFTF_AFFINITY_STATE_OFF = 0,
19 TFTF_AFFINITY_STATE_ON_PENDING,
20 TFTF_AFFINITY_STATE_ON,
21} tftf_affinity_info_t;
22
23/* Structure for keeping track of CPU state */
24typedef struct {
25 volatile tftf_affinity_info_t state;
26 spinlock_t lock;
27} __aligned(CACHE_WRITEBACK_GRANULE) tftf_cpu_state_t;
28
29/*
30 * Suspend information passed to the TFTF suspend helpers.
31 */
32typedef struct suspend_info {
33 /* The power state parameter to be passed to PSCI_CPU_SUSPEND */
34 unsigned int power_state;
35 /* SMC function ID of the PSCI suspend call */
36 unsigned int psci_api;
37 /* Whether the system context needs to be saved and restored */
38 unsigned int save_system_context;
39} suspend_info_t;
40
41/*
42 * Power up a core.
43 * This uses the PSCI CPU_ON API, which means it relies on the EL3 firmware's
44 * runtime services capabilities.
45 * The core will be boostrapped by the framework before handing it over
46 * to the entry point specified as the 2nd argument.
47 *
48 * target_cpu: MPID of the CPU to power up
49 * entrypoint: Address where the CPU will jump once the framework has
50 * initialized it
51 * context_id: Context identifier as defined by the PSCI specification
52 *
53 * Return: Return code of the PSCI CPU_ON call
54 * (refer to the PSCI specification for details)
55 */
56int32_t tftf_cpu_on(u_register_t target_cpu,
57 uintptr_t entrypoint,
58 u_register_t context_id);
59
60/*
61 * Tries to power up a core.
62 * This API is similar to tftf_cpu_on API with the difference being it
63 * does a SMC call to EL3 firmware without checking the status of the
64 * core with respect to the framework.
65 *
66 * A caller is expected to handle the return code given by the EL3 firmware.
67 *
68 * target_cpu: MPID of the CPU to power up
69 * entrypoint: Address where the CPU will jump once the framework has
70 * initialised it
71 * context_id: Context identifier as defined by the PSCI specification
72 *
73 * Return: Return code of the PSCI CPU_ON call
74 * (refer to the PSCI specification for details)
75 */
76int32_t tftf_try_cpu_on(u_register_t target_cpu,
77 uintptr_t entrypoint,
78 u_register_t context_id);
79
80/*
81 * Power down the calling core.
82 * This uses the PSCI CPU_OFF API, which means it relies on the EL3 firmware's
83 * runtime services capabilities.
84 *
85 * Return: This function does not return when successful.
86 * Otherwise, return the same error code as the PSCI CPU_OFF call
87 * (refer to the PSCI specification for details)
88 */
89int32_t tftf_cpu_off(void);
90
91/*
92 * It is an Api used to enter a suspend state. It does the following:
93 * - Allocates space for saving architectural and non-architectural CPU state on
94 * stack
95 * - Saves architecture state of the CPU in the space allocated which consists:
96 * a. Callee registers
97 * b. System control registers. ex: MMU, SCTLR_EL1
98 * - Depending on the state of `save_system_context` flag in suspend_info
99 * saves the context of system peripherals like GIC, timer etc.
100 * - Sets context ID to the base of the stack allocated for saving context
101 * - Calls Secure Platform Firmware to enter suspend
102 * - If suspend fails, It restores the callee registers
103 * power state: PSCI power state to be sent via SMC
104 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
105 *
106 * Note: This api might not test all use cases, as the context ID and resume
107 * entrypoint is in the control of the framework.
108 */
109int tftf_suspend(const suspend_info_t *info);
110
111
112/* ----------------------------------------------------------------------------
113 * The above APIs might not be suitable in all test scenarios.
114 * A test case could want to bypass those APIs i.e. call the PSCI APIs
115 * directly. In this case, it is the responsibility of the test case to preserve
116 * the state of the framework. The below APIs are provided to this end.
117 * ----------------------------------------------------------------------------
118 */
119
120/*
121 * The 3 following functions are used to manipulate the reference count tracking
122 * the number of CPUs participating in a test.
123 */
124
125/*
126 * Increment the reference count.
127 * Return the new, incremented value.
128 */
129unsigned int tftf_inc_ref_cnt(void);
130
131/*
132 * Decrement the reference count.
133 * Return the new, decremented value.
134 */
135unsigned int tftf_dec_ref_cnt(void);
136
137/* Return the current reference count value */
138unsigned int tftf_get_ref_cnt(void);
139
140/*
141 * Set the calling CPU online/offline. This only adjusts the view of the core
142 * from the framework's point of view, it doesn't actually power up/down the
143 * core.
144 */
145void tftf_set_cpu_online(void);
146void tftf_init_cpus_status_map(void);
147void tftf_set_cpu_offline(void);
148
149/*
150 * Query the state of a core.
151 * Return: 1 if the core is online, 0 otherwise.
152 */
153unsigned int tftf_is_cpu_online(unsigned int mpid);
154
155unsigned int tftf_is_core_pos_online(unsigned int core_pos);
156
157/* TFTF Suspend helpers */
158static inline int tftf_cpu_suspend(unsigned int pwr_state)
159{
160 suspend_info_t info = {
161 .power_state = pwr_state,
162 .save_system_context = 0,
163 .psci_api = SMC_PSCI_CPU_SUSPEND,
164 };
165
166 return tftf_suspend(&info);
167}
168
169static inline int tftf_cpu_suspend_save_sys_ctx(unsigned int pwr_state)
170{
171 suspend_info_t info = {
172 .power_state = pwr_state,
173 .save_system_context = 1,
174 .psci_api = SMC_PSCI_CPU_SUSPEND,
175 };
176
177 return tftf_suspend(&info);
178}
179
180
181static inline int tftf_system_suspend(void)
182{
183 suspend_info_t info = {
184 .power_state = 0,
185 .save_system_context = 1,
186 .psci_api = SMC_PSCI_SYSTEM_SUSPEND,
187 };
188
189 return tftf_suspend(&info);
190}
191
192#endif /* __POWER_MANAGEMENT_H__ */