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