rules.mak: Add "COMMA" constant
[qemu/ar7.git] / target-arm / arm-powerctl.c
blobd452230515d98c2e9daac7c4b2e966b07eb4207e
1 /*
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.
9 */
11 #include "qemu/osdep.h"
12 #include <cpu.h>
13 #include <cpu-qom.h>
14 #include "internals.h"
15 #include "arm-powerctl.h"
16 #include "qemu/log.h"
17 #include "exec/exec-all.h"
19 #ifndef DEBUG_ARM_POWERCTL
20 #define DEBUG_ARM_POWERCTL 0
21 #endif
23 #define DPRINTF(fmt, args...) \
24 do { \
25 if (DEBUG_ARM_POWERCTL) { \
26 fprintf(stderr, "[ARM]%s: " fmt , __func__, ##args); \
27 } \
28 } while (0)
30 CPUState *arm_get_cpu_by_id(uint64_t id)
32 CPUState *cpu;
34 DPRINTF("cpu %" PRId64 "\n", id);
36 CPU_FOREACH(cpu) {
37 ARMCPU *armcpu = ARM_CPU(cpu);
39 if (armcpu->mp_affinity == id) {
40 return cpu;
44 qemu_log_mask(LOG_GUEST_ERROR,
45 "[ARM]%s: Requesting unknown CPU %" PRId64 "\n",
46 __func__, id);
48 return NULL;
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;
55 ARMCPU *target_cpu;
57 DPRINTF("cpu %" PRId64 " (EL %d, %s) @ 0x%" PRIx64 " with R0 = 0x%" PRIx64
58 "\n", cpuid, target_el, target_aa64 ? "aarch64" : "aarch32", entry,
59 context_id);
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
67 * aligned.
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",
83 __func__, cpuid);
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",
108 __func__, cpuid);
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;
117 if (target_aa64) {
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);
135 } else {
136 /* We are requested to boot in AArch32 mode */
137 static uint32_t mode_for_el[] = { 0,
138 ARM_CPU_MODE_SVC,
139 ARM_CPU_MODE_HYP,
140 ARM_CPU_MODE_SVC };
142 cpsr_write(&target_cpu->env, mode_for_el[target_el], CPSR_M,
143 CPSRWriteRaw);
146 if (target_el == 3) {
147 /* Processor is in secure mode */
148 target_cpu->env.cp15.scr_el3 &= ~SCR_NS;
149 } else {
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));
157 if (target_aa64) {
158 target_cpu->env.xregs[0] = context_id;
159 target_cpu->env.thumb = false;
160 } else {
161 target_cpu->env.regs[0] = context_id;
162 target_cpu->env.thumb = entry & 1;
163 entry &= 0xfffffffe;
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;
176 ARMCPU *target_cpu;
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",
189 __func__, cpuid);
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);
197 /* notreached */
199 return QEMU_ARM_POWERCTL_RET_SUCCESS;
202 int arm_reset_cpu(uint64_t cpuid)
204 CPUState *target_cpu_state;
205 ARMCPU *target_cpu;
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",
218 __func__, cpuid);
219 return QEMU_ARM_POWERCTL_IS_OFF;
222 /* Reset the cpu */
223 cpu_reset(target_cpu_state);
225 return QEMU_ARM_POWERCTL_RET_SUCCESS;