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 "exec/exec-all.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",
51 int arm_set_cpu_on(uint64_t cpuid
, uint64_t entry
, uint64_t context_id
,
52 uint32_t target_el
, bool target_aa64
)
54 CPUState
*target_cpu_state
;
57 DPRINTF("cpu %" PRId64
" (EL %d, %s) @ 0x%" PRIx64
" with R0 = 0x%" PRIx64
58 "\n", cpuid
, target_el
, target_aa64
? "aarch64" : "aarch32", entry
,
61 /* requested EL level need to be in the 1 to 3 range */
62 assert((target_el
> 0) && (target_el
< 4));
64 if (target_aa64
&& (entry
& 3)) {
66 * if we are booting in AArch64 mode then "entry" needs to be 4 bytes
69 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
72 /* Retrieve the cpu we are powering up */
73 target_cpu_state
= arm_get_cpu_by_id(cpuid
);
74 if (!target_cpu_state
) {
75 /* The cpu was not found */
76 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
79 target_cpu
= ARM_CPU(target_cpu_state
);
80 if (!target_cpu
->powered_off
) {
81 qemu_log_mask(LOG_GUEST_ERROR
,
82 "[ARM]%s: CPU %" PRId64
" is already on\n",
84 return QEMU_ARM_POWERCTL_ALREADY_ON
;
88 * The newly brought CPU is requested to enter the exception level
89 * "target_el" and be in the requested mode (AArch64 or AArch32).
92 if (((target_el
== 3) && !arm_feature(&target_cpu
->env
, ARM_FEATURE_EL3
)) ||
93 ((target_el
== 2) && !arm_feature(&target_cpu
->env
, ARM_FEATURE_EL2
))) {
95 * The CPU does not support requested level
97 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
100 if (!target_aa64
&& arm_feature(&target_cpu
->env
, ARM_FEATURE_AARCH64
)) {
102 * For now we don't support booting an AArch64 CPU in AArch32 mode
103 * TODO: We should add this support later
105 qemu_log_mask(LOG_UNIMP
,
106 "[ARM]%s: Starting AArch64 CPU %" PRId64
107 " in AArch32 mode is not supported yet\n",
109 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
112 /* Initialize the cpu we are turning on */
113 cpu_reset(target_cpu_state
);
114 target_cpu
->powered_off
= false;
115 target_cpu_state
->halted
= 0;
118 if ((target_el
< 3) && arm_feature(&target_cpu
->env
, ARM_FEATURE_EL3
)) {
120 * As target mode is AArch64, we need to set lower
121 * exception level (the requested level 2) to AArch64
123 target_cpu
->env
.cp15
.scr_el3
|= SCR_RW
;
126 if ((target_el
< 2) && arm_feature(&target_cpu
->env
, ARM_FEATURE_EL2
)) {
128 * As target mode is AArch64, we need to set lower
129 * exception level (the requested level 1) to AArch64
131 target_cpu
->env
.cp15
.hcr_el2
|= HCR_RW
;
134 target_cpu
->env
.pstate
= aarch64_pstate_mode(target_el
, true);
136 /* We are requested to boot in AArch32 mode */
137 static uint32_t mode_for_el
[] = { 0,
142 cpsr_write(&target_cpu
->env
, mode_for_el
[target_el
], CPSR_M
,
146 if (target_el
== 3) {
147 /* Processor is in secure mode */
148 target_cpu
->env
.cp15
.scr_el3
&= ~SCR_NS
;
150 /* Processor is not in secure mode */
151 target_cpu
->env
.cp15
.scr_el3
|= SCR_NS
;
154 /* We check if the started CPU is now at the correct level */
155 assert(target_el
== arm_current_el(&target_cpu
->env
));
158 target_cpu
->env
.xregs
[0] = context_id
;
159 target_cpu
->env
.thumb
= false;
161 target_cpu
->env
.regs
[0] = context_id
;
162 target_cpu
->env
.thumb
= entry
& 1;
166 /* Start the new CPU at the requested address */
167 cpu_set_pc(target_cpu_state
, entry
);
169 /* We are good to go */
170 return QEMU_ARM_POWERCTL_RET_SUCCESS
;
173 int arm_set_cpu_off(uint64_t cpuid
)
175 CPUState
*target_cpu_state
;
178 DPRINTF("cpu %" PRId64
"\n", cpuid
);
180 /* change to the cpu we are powering up */
181 target_cpu_state
= arm_get_cpu_by_id(cpuid
);
182 if (!target_cpu_state
) {
183 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
185 target_cpu
= ARM_CPU(target_cpu_state
);
186 if (target_cpu
->powered_off
) {
187 qemu_log_mask(LOG_GUEST_ERROR
,
188 "[ARM]%s: CPU %" PRId64
" is already off\n",
190 return QEMU_ARM_POWERCTL_IS_OFF
;
193 target_cpu
->powered_off
= true;
194 target_cpu_state
->halted
= 1;
195 target_cpu_state
->exception_index
= EXCP_HLT
;
196 cpu_loop_exit(target_cpu_state
);
199 return QEMU_ARM_POWERCTL_RET_SUCCESS
;
202 int arm_reset_cpu(uint64_t cpuid
)
204 CPUState
*target_cpu_state
;
207 DPRINTF("cpu %" PRId64
"\n", cpuid
);
209 /* change to the cpu we are resetting */
210 target_cpu_state
= arm_get_cpu_by_id(cpuid
);
211 if (!target_cpu_state
) {
212 return QEMU_ARM_POWERCTL_INVALID_PARAM
;
214 target_cpu
= ARM_CPU(target_cpu_state
);
215 if (target_cpu
->powered_off
) {
216 qemu_log_mask(LOG_GUEST_ERROR
,
217 "[ARM]%s: CPU %" PRId64
" is off\n",
219 return QEMU_ARM_POWERCTL_IS_OFF
;
223 cpu_reset(target_cpu_state
);
225 return QEMU_ARM_POWERCTL_RET_SUCCESS
;