2 * QEMU support -- ARM Power Control specific functions.
4 * Copyright (c) 2016 Jean-Christophe Dubois
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
14 #include "internals.h"
15 #include "arm-powerctl.h"
17 #include "qemu/main-loop.h"
19 #ifndef DEBUG_ARM_POWERCTL
20 #define DEBUG_ARM_POWERCTL 0
23 #define DPRINTF(fmt, args...) \
25 if (DEBUG_ARM_POWERCTL) { \
26 fprintf(stderr, "[ARM]%s: " fmt , __func__, ##args); \
30 CPUState
*arm_get_cpu_by_id(uint64_t id
)
34 DPRINTF("cpu %" PRId64
"\n", id
);
37 ARMCPU
*armcpu
= ARM_CPU(cpu
);
39 if (armcpu
->mp_affinity
== id
) {
44 qemu_log_mask(LOG_GUEST_ERROR
,
45 "[ARM]%s: Requesting unknown CPU %" PRId64
"\n",
59 static void arm_set_cpu_on_async_work(CPUState
*target_cpu_state
,
62 ARMCPU
*target_cpu
= ARM_CPU(target_cpu_state
);
63 struct CpuOnInfo
*info
= (struct CpuOnInfo
*) data
.host_ptr
;
65 /* Initialize the cpu we are turning on */
66 cpu_reset(target_cpu_state
);
67 target_cpu_state
->halted
= 0;
69 if (info
->target_aa64
) {
70 if ((info
->target_el
< 3) && arm_feature(&target_cpu
->env
,
73 * As target mode is AArch64, we need to set lower
74 * exception level (the requested level 2) to AArch64
76 target_cpu
->env
.cp15
.scr_el3
|= SCR_RW
;
79 if ((info
->target_el
< 2) && arm_feature(&target_cpu
->env
,
82 * As target mode is AArch64, we need to set lower
83 * exception level (the requested level 1) to AArch64
85 target_cpu
->env
.cp15
.hcr_el2
|= HCR_RW
;
88 target_cpu
->env
.pstate
= aarch64_pstate_mode(info
->target_el
, true);
90 /* We are requested to boot in AArch32 mode */
91 static const uint32_t mode_for_el
[] = { 0,
96 cpsr_write(&target_cpu
->env
, mode_for_el
[info
->target_el
], CPSR_M
,
100 if (info
->target_el
== 3) {
101 /* Processor is in secure mode */
102 target_cpu
->env
.cp15
.scr_el3
&= ~SCR_NS
;
104 /* Processor is not in secure mode */
105 target_cpu
->env
.cp15
.scr_el3
|= SCR_NS
;
108 /* We check if the started CPU is now at the correct level */
109 assert(info
->target_el
== arm_current_el(&target_cpu
->env
));
111 if (info
->target_aa64
) {
112 target_cpu
->env
.xregs
[0] = info
->context_id
;
113 target_cpu
->env
.thumb
= false;
115 target_cpu
->env
.regs
[0] = info
->context_id
;
116 target_cpu
->env
.thumb
= info
->entry
& 1;
117 info
->entry
&= 0xfffffffe;
120 /* Start the new CPU at the requested address */
121 cpu_set_pc(target_cpu_state
, info
->entry
);
125 /* Finally set the power status */
126 assert(qemu_mutex_iothread_locked());
127 target_cpu
->power_state
= PSCI_ON
;
130 int arm_set_cpu_on(uint64_t cpuid
, uint64_t entry
, uint64_t context_id
,
131 uint32_t target_el
, bool target_aa64
)
133 CPUState
*target_cpu_state
;
135 struct CpuOnInfo
*info
;
137 assert(qemu_mutex_iothread_locked());
139 DPRINTF("cpu %" PRId64
" (EL %d, %s) @ 0x%" PRIx64
" with R0 = 0x%" PRIx64
140 "\n", cpuid
, target_el
, target_aa64
? "aarch64" : "aarch32", entry
,
143 /* requested EL level need to be in the 1 to 3 range */
144 assert((target_el
> 0) && (target_el
< 4));
146 if (target_aa64
&& (entry
& 3)) {
148 * if we are booting in AArch64 mode then "entry" needs to be 4 bytes
151 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
154 /* Retrieve the cpu we are powering up */
155 target_cpu_state
= arm_get_cpu_by_id(cpuid
);
156 if (!target_cpu_state
) {
157 /* The cpu was not found */
158 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
161 target_cpu
= ARM_CPU(target_cpu_state
);
162 if (target_cpu
->power_state
== PSCI_ON
) {
163 qemu_log_mask(LOG_GUEST_ERROR
,
164 "[ARM]%s: CPU %" PRId64
" is already on\n",
166 return QEMU_ARM_POWERCTL_ALREADY_ON
;
170 * The newly brought CPU is requested to enter the exception level
171 * "target_el" and be in the requested mode (AArch64 or AArch32).
174 if (((target_el
== 3) && !arm_feature(&target_cpu
->env
, ARM_FEATURE_EL3
)) ||
175 ((target_el
== 2) && !arm_feature(&target_cpu
->env
, ARM_FEATURE_EL2
))) {
177 * The CPU does not support requested level
179 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
182 if (!target_aa64
&& arm_feature(&target_cpu
->env
, ARM_FEATURE_AARCH64
)) {
184 * For now we don't support booting an AArch64 CPU in AArch32 mode
185 * TODO: We should add this support later
187 qemu_log_mask(LOG_UNIMP
,
188 "[ARM]%s: Starting AArch64 CPU %" PRId64
189 " in AArch32 mode is not supported yet\n",
191 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
195 * If another CPU has powered the target on we are in the state
196 * ON_PENDING and additional attempts to power on the CPU should
197 * fail (see 6.6 Implementation CPU_ON/CPU_OFF races in the PSCI
200 if (target_cpu
->power_state
== PSCI_ON_PENDING
) {
201 qemu_log_mask(LOG_GUEST_ERROR
,
202 "[ARM]%s: CPU %" PRId64
" is already powering on\n",
204 return QEMU_ARM_POWERCTL_ON_PENDING
;
207 /* To avoid racing with a CPU we are just kicking off we do the
208 * final bit of preparation for the work in the target CPUs
211 info
= g_new(struct CpuOnInfo
, 1);
213 info
->context_id
= context_id
;
214 info
->target_el
= target_el
;
215 info
->target_aa64
= target_aa64
;
217 async_run_on_cpu(target_cpu_state
, arm_set_cpu_on_async_work
,
218 RUN_ON_CPU_HOST_PTR(info
));
220 /* We are good to go */
221 return QEMU_ARM_POWERCTL_RET_SUCCESS
;
224 static void arm_set_cpu_off_async_work(CPUState
*target_cpu_state
,
225 run_on_cpu_data data
)
227 ARMCPU
*target_cpu
= ARM_CPU(target_cpu_state
);
229 assert(qemu_mutex_iothread_locked());
230 target_cpu
->power_state
= PSCI_OFF
;
231 target_cpu_state
->halted
= 1;
232 target_cpu_state
->exception_index
= EXCP_HLT
;
235 int arm_set_cpu_off(uint64_t cpuid
)
237 CPUState
*target_cpu_state
;
240 assert(qemu_mutex_iothread_locked());
242 DPRINTF("cpu %" PRId64
"\n", cpuid
);
244 /* change to the cpu we are powering up */
245 target_cpu_state
= arm_get_cpu_by_id(cpuid
);
246 if (!target_cpu_state
) {
247 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
249 target_cpu
= ARM_CPU(target_cpu_state
);
250 if (target_cpu
->power_state
== PSCI_OFF
) {
251 qemu_log_mask(LOG_GUEST_ERROR
,
252 "[ARM]%s: CPU %" PRId64
" is already off\n",
254 return QEMU_ARM_POWERCTL_IS_OFF
;
257 /* Queue work to run under the target vCPUs context */
258 async_run_on_cpu(target_cpu_state
, arm_set_cpu_off_async_work
,
261 return QEMU_ARM_POWERCTL_RET_SUCCESS
;
264 static void arm_reset_cpu_async_work(CPUState
*target_cpu_state
,
265 run_on_cpu_data data
)
268 cpu_reset(target_cpu_state
);
271 int arm_reset_cpu(uint64_t cpuid
)
273 CPUState
*target_cpu_state
;
276 assert(qemu_mutex_iothread_locked());
278 DPRINTF("cpu %" PRId64
"\n", cpuid
);
280 /* change to the cpu we are resetting */
281 target_cpu_state
= arm_get_cpu_by_id(cpuid
);
282 if (!target_cpu_state
) {
283 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
285 target_cpu
= ARM_CPU(target_cpu_state
);
287 if (target_cpu
->power_state
== PSCI_OFF
) {
288 qemu_log_mask(LOG_GUEST_ERROR
,
289 "[ARM]%s: CPU %" PRId64
" is off\n",
291 return QEMU_ARM_POWERCTL_IS_OFF
;
294 /* Queue work to run under the target vCPUs context */
295 async_run_on_cpu(target_cpu_state
, arm_reset_cpu_async_work
,
298 return QEMU_ARM_POWERCTL_RET_SUCCESS
;