megasas: add MegaRAID SAS 2108 emulation
[qemu/ar7.git] / target-s390x / cpu.c
blobd2f6312e03ab4f8fd12b8be9dfb7732f19372bed
1 /*
2 * QEMU S/390 CPU
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2011 Alexander Graf
6 * Copyright (c) 2012 SUSE LINUX Products GmbH
7 * Copyright (c) 2012 IBM Corp.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see
21 * <http://www.gnu.org/licenses/lgpl-2.1.html>
22 * Contributions after 2012-12-11 are licensed under the terms of the
23 * GNU GPL, version 2 or (at your option) any later version.
26 #include "cpu.h"
27 #include "qemu-common.h"
28 #include "qemu/timer.h"
29 #include "qemu/error-report.h"
30 #include "hw/hw.h"
31 #include "trace.h"
32 #ifndef CONFIG_USER_ONLY
33 #include "sysemu/arch_init.h"
34 #endif
36 #define CR0_RESET 0xE0UL
37 #define CR14_RESET 0xC2000000UL;
39 /* generate CPU information for cpu -? */
40 void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
42 #ifdef CONFIG_KVM
43 (*cpu_fprintf)(f, "s390 %16s\n", "host");
44 #endif
47 #ifndef CONFIG_USER_ONLY
48 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
50 CpuDefinitionInfoList *entry;
51 CpuDefinitionInfo *info;
53 info = g_malloc0(sizeof(*info));
54 info->name = g_strdup("host");
56 entry = g_malloc0(sizeof(*entry));
57 entry->value = info;
59 return entry;
61 #endif
63 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
65 S390CPU *cpu = S390_CPU(cs);
67 cpu->env.psw.addr = value;
70 static bool s390_cpu_has_work(CPUState *cs)
72 S390CPU *cpu = S390_CPU(cs);
73 CPUS390XState *env = &cpu->env;
75 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
76 (env->psw.mask & PSW_MASK_EXT);
79 #if !defined(CONFIG_USER_ONLY)
80 /* S390CPUClass::load_normal() */
81 static void s390_cpu_load_normal(CPUState *s)
83 S390CPU *cpu = S390_CPU(s);
84 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
85 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
86 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
88 #endif
90 /* S390CPUClass::cpu_reset() */
91 static void s390_cpu_reset(CPUState *s)
93 S390CPU *cpu = S390_CPU(s);
94 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
95 CPUS390XState *env = &cpu->env;
97 env->pfault_token = -1UL;
98 scc->parent_reset(s);
99 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
100 tlb_flush(s, 1);
103 /* S390CPUClass::initial_reset() */
104 static void s390_cpu_initial_reset(CPUState *s)
106 S390CPU *cpu = S390_CPU(s);
107 CPUS390XState *env = &cpu->env;
109 s390_cpu_reset(s);
110 /* initial reset does not touch regs,fregs and aregs */
111 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
112 offsetof(CPUS390XState, fpc));
114 /* architectured initial values for CR 0 and 14 */
115 env->cregs[0] = CR0_RESET;
116 env->cregs[14] = CR14_RESET;
118 env->pfault_token = -1UL;
120 /* Reset state inside the kernel that we cannot access yet from QEMU. */
121 if (kvm_enabled()) {
122 kvm_s390_reset_vcpu(cpu);
126 /* CPUClass:reset() */
127 static void s390_cpu_full_reset(CPUState *s)
129 S390CPU *cpu = S390_CPU(s);
130 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
131 CPUS390XState *env = &cpu->env;
133 scc->parent_reset(s);
134 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
136 memset(env, 0, offsetof(CPUS390XState, cpu_num));
138 /* architectured initial values for CR 0 and 14 */
139 env->cregs[0] = CR0_RESET;
140 env->cregs[14] = CR14_RESET;
142 env->pfault_token = -1UL;
144 /* Reset state inside the kernel that we cannot access yet from QEMU. */
145 if (kvm_enabled()) {
146 kvm_s390_reset_vcpu(cpu);
148 tlb_flush(s, 1);
151 #if !defined(CONFIG_USER_ONLY)
152 static void s390_cpu_machine_reset_cb(void *opaque)
154 S390CPU *cpu = opaque;
156 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
158 #endif
160 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
162 CPUState *cs = CPU(dev);
163 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
165 s390_cpu_gdb_init(cs);
166 qemu_init_vcpu(cs);
167 #if !defined(CONFIG_USER_ONLY)
168 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
169 #else
170 cpu_reset(cs);
171 #endif
173 scc->parent_realize(dev, errp);
176 static void s390_cpu_initfn(Object *obj)
178 CPUState *cs = CPU(obj);
179 S390CPU *cpu = S390_CPU(obj);
180 CPUS390XState *env = &cpu->env;
181 static bool inited;
182 static int cpu_num = 0;
183 #if !defined(CONFIG_USER_ONLY)
184 struct tm tm;
185 #endif
187 cs->env_ptr = env;
188 cpu_exec_init(env);
189 #if !defined(CONFIG_USER_ONLY)
190 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
191 qemu_get_timedate(&tm, 0);
192 env->tod_offset = TOD_UNIX_EPOCH +
193 (time2tod(mktimegm(&tm)) * 1000000000ULL);
194 env->tod_basetime = 0;
195 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
196 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
197 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
198 #endif
199 env->cpu_num = cpu_num++;
200 env->ext_index = -1;
202 if (tcg_enabled() && !inited) {
203 inited = true;
204 s390x_translate_init();
208 static void s390_cpu_finalize(Object *obj)
210 #if !defined(CONFIG_USER_ONLY)
211 S390CPU *cpu = S390_CPU(obj);
213 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
214 #endif
217 #if !defined(CONFIG_USER_ONLY)
218 static bool disabled_wait(CPUState *cpu)
220 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
221 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
224 static unsigned s390_count_running_cpus(void)
226 CPUState *cpu;
227 int nr_running = 0;
229 CPU_FOREACH(cpu) {
230 uint8_t state = S390_CPU(cpu)->env.cpu_state;
231 if (state == CPU_STATE_OPERATING ||
232 state == CPU_STATE_LOAD) {
233 if (!disabled_wait(cpu)) {
234 nr_running++;
239 return nr_running;
242 unsigned int s390_cpu_halt(S390CPU *cpu)
244 CPUState *cs = CPU(cpu);
245 trace_cpu_halt(cs->cpu_index);
247 if (!cs->halted) {
248 cs->halted = 1;
249 cs->exception_index = EXCP_HLT;
252 return s390_count_running_cpus();
255 void s390_cpu_unhalt(S390CPU *cpu)
257 CPUState *cs = CPU(cpu);
258 trace_cpu_unhalt(cs->cpu_index);
260 if (cs->halted) {
261 cs->halted = 0;
262 cs->exception_index = -1;
266 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
268 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
270 switch (cpu_state) {
271 case CPU_STATE_STOPPED:
272 case CPU_STATE_CHECK_STOP:
273 /* halt the cpu for common infrastructure */
274 s390_cpu_halt(cpu);
275 break;
276 case CPU_STATE_OPERATING:
277 case CPU_STATE_LOAD:
278 /* unhalt the cpu for common infrastructure */
279 s390_cpu_unhalt(cpu);
280 break;
281 default:
282 error_report("Requested CPU state is not a valid S390 CPU state: %u",
283 cpu_state);
284 exit(1);
286 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
287 kvm_s390_set_cpu_state(cpu, cpu_state);
289 cpu->env.cpu_state = cpu_state;
291 return s390_count_running_cpus();
293 #endif
295 static void s390_cpu_class_init(ObjectClass *oc, void *data)
297 S390CPUClass *scc = S390_CPU_CLASS(oc);
298 CPUClass *cc = CPU_CLASS(scc);
299 DeviceClass *dc = DEVICE_CLASS(oc);
301 scc->parent_realize = dc->realize;
302 dc->realize = s390_cpu_realizefn;
304 scc->parent_reset = cc->reset;
305 #if !defined(CONFIG_USER_ONLY)
306 scc->load_normal = s390_cpu_load_normal;
307 #endif
308 scc->cpu_reset = s390_cpu_reset;
309 scc->initial_cpu_reset = s390_cpu_initial_reset;
310 cc->reset = s390_cpu_full_reset;
311 cc->has_work = s390_cpu_has_work;
312 cc->do_interrupt = s390_cpu_do_interrupt;
313 cc->dump_state = s390_cpu_dump_state;
314 cc->set_pc = s390_cpu_set_pc;
315 cc->gdb_read_register = s390_cpu_gdb_read_register;
316 cc->gdb_write_register = s390_cpu_gdb_write_register;
317 #ifdef CONFIG_USER_ONLY
318 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
319 #else
320 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
321 cc->vmsd = &vmstate_s390_cpu;
322 cc->write_elf64_note = s390_cpu_write_elf64_note;
323 cc->write_elf64_qemunote = s390_cpu_write_elf64_qemunote;
324 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
325 #endif
326 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
327 cc->gdb_core_xml_file = "s390x-core64.xml";
330 static const TypeInfo s390_cpu_type_info = {
331 .name = TYPE_S390_CPU,
332 .parent = TYPE_CPU,
333 .instance_size = sizeof(S390CPU),
334 .instance_init = s390_cpu_initfn,
335 .instance_finalize = s390_cpu_finalize,
336 .abstract = false,
337 .class_size = sizeof(S390CPUClass),
338 .class_init = s390_cpu_class_init,
341 static void s390_cpu_register_types(void)
343 type_register_static(&s390_cpu_type_info);
346 type_init(s390_cpu_register_types)