]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390x/s390-virtio-ccw.c
qdev: Convert uses of qdev_create() with Coccinelle
[mirror_qemu.git] / hw / s390x / s390-virtio-ccw.c
CommitLineData
a5c95808
CH
1/*
2 * virtio ccw machine
3 *
c3347ed0 4 * Copyright 2012, 2020 IBM Corp.
6286b419 5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
a5c95808 6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
c3347ed0 7 * Janosch Frank <frankja@linux.ibm.com>
a5c95808
CH
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at
10 * your option) any later version. See the COPYING file in the top-level
11 * directory.
12 */
13
9615495a 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
4771d756 16#include "cpu.h"
a5c95808
CH
17#include "hw/boards.h"
18#include "exec/address-spaces.h"
9138977b 19#include "exec/ram_addr.h"
7d577546 20#include "hw/s390x/s390-virtio-hcall.h"
83c9f4ca 21#include "hw/s390x/sclp.h"
3a553fc6 22#include "hw/s390x/s390_flic.h"
bd3f16ac
PB
23#include "hw/s390x/ioinst.h"
24#include "hw/s390x/css.h"
a5c95808 25#include "virtio-ccw.h"
b6fe0124 26#include "qemu/config-file.h"
856dfd8a 27#include "qemu/ctype.h"
b5684cd8 28#include "qemu/error-report.h"
922a01a0 29#include "qemu/option.h"
5c30ef93 30#include "qemu/qemu-print.h"
8cba80c3 31#include "s390-pci-bus.h"
71e8a915 32#include "sysemu/reset.h"
0f5f6691 33#include "hw/s390x/storage-keys.h"
903fd80b 34#include "hw/s390x/storage-attributes.h"
bc61c8c6 35#include "hw/s390x/event-facility.h"
04ca4b92 36#include "ipl.h"
8b8a61ad 37#include "hw/s390x/s390-virtio-ccw.h"
dd70bd0d 38#include "hw/s390x/css-bridge.h"
a51b3153 39#include "hw/s390x/ap-bridge.h"
f2a8f0a6 40#include "migration/register.h"
7223bcce 41#include "cpu_models.h"
6286b419 42#include "hw/nmi.h"
a27bd6c7 43#include "hw/qdev-properties.h"
8046f374 44#include "hw/s390x/tod.h"
2f780b6a 45#include "sysemu/sysemu.h"
b1697f63 46#include "sysemu/balloon.h"
c3347ed0 47#include "hw/s390x/pv.h"
0141e1b4
JF
48#include "migration/blocker.h"
49
50static Error *pv_mig_blocker;
6286b419 51
6286b419
DH
52S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
53{
2b44178d
DH
54 static MachineState *ms;
55
56 if (!ms) {
57 ms = MACHINE(qdev_get_machine());
58 g_assert(ms->possible_cpus);
6286b419
DH
59 }
60
2b44178d
DH
61 /* CPU address corresponds to the core_id and the index */
62 if (cpu_addr >= ms->possible_cpus->len) {
63 return NULL;
64 }
65 return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
6286b419
DH
66}
67
32dc6aa0
IM
68static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
69 Error **errp)
70{
71 S390CPU *cpu = S390_CPU(object_new(typename));
72 Error *err = NULL;
73
74 object_property_set_int(OBJECT(cpu), core_id, "core-id", &err);
75 if (err != NULL) {
76 goto out;
77 }
78 object_property_set_bool(OBJECT(cpu), true, "realized", &err);
79
80out:
81 object_unref(OBJECT(cpu));
82 if (err) {
83 error_propagate(errp, err);
84 cpu = NULL;
85 }
86 return cpu;
87}
88
6286b419
DH
89static void s390_init_cpus(MachineState *machine)
90{
4dc3b151 91 MachineClass *mc = MACHINE_GET_CLASS(machine);
6286b419 92 int i;
6286b419 93
4dc3b151
DH
94 /* initialize possible_cpus */
95 mc->possible_cpu_arch_ids(machine);
96
ae71ed86 97 for (i = 0; i < machine->smp.cpus; i++) {
b6805e12 98 s390x_new_cpu(machine->cpu_type, i, &error_fatal);
6286b419
DH
99 }
100}
2eb1cd07 101
09c7f58c 102static const char *const reset_dev_types[] = {
3f9e4859 103 TYPE_VIRTUAL_CSS_BRIDGE,
09c7f58c
DH
104 "s390-sclp-event-facility",
105 "s390-flic",
106 "diag288",
107};
108
a30fb811 109static void subsystem_reset(void)
4e872a3f 110{
09c7f58c
DH
111 DeviceState *dev;
112 int i;
4e872a3f 113
09c7f58c
DH
114 for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
115 dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
116 if (dev) {
117 qdev_reset_all(dev);
118 }
0c7322cf 119 }
4e872a3f
CB
120}
121
a5c95808
CH
122static int virtio_ccw_hcall_notify(const uint64_t *args)
123{
124 uint64_t subch_id = args[0];
125 uint64_t queue = args[1];
126 SubchDev *sch;
127 int cssid, ssid, schid, m;
128
129 if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
130 return -EINVAL;
131 }
132 sch = css_find_subch(m, cssid, ssid, schid);
133 if (!sch || !css_subch_visible(sch)) {
134 return -EINVAL;
135 }
b1914b82 136 if (queue >= VIRTIO_QUEUE_MAX) {
b57ed9bf
CH
137 return -EINVAL;
138 }
a5c95808
CH
139 virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
140 return 0;
141
142}
143
144static int virtio_ccw_hcall_early_printk(const uint64_t *args)
145{
146 uint64_t mem = args[0];
147
148 if (mem < ram_size) {
149 /* Early printk */
150 return 0;
151 }
152 return -EINVAL;
153}
154
155static void virtio_ccw_register_hcalls(void)
156{
157 s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
158 virtio_ccw_hcall_notify);
159 /* Tolerate early printk. */
160 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
161 virtio_ccw_hcall_early_printk);
162}
163
3a12fc61 164static void s390_memory_init(MemoryRegion *ram)
a5c95808 165{
a5c95808 166 MemoryRegion *sysmem = get_system_memory();
9138977b 167 Error *local_err = NULL;
80d23275
DH
168
169 /* allocate RAM for core */
fb1fc5a8 170 memory_region_add_subregion(sysmem, 0, ram);
80d23275 171
9138977b
DH
172 /*
173 * Configure the maximum page size. As no memory devices were created
174 * yet, this is the page size of initial memory only.
175 */
905b7ee4 176 s390_set_max_pagesize(qemu_maxrampagesize(), &local_err);
9138977b
DH
177 if (local_err) {
178 error_report_err(local_err);
179 exit(EXIT_FAILURE);
180 }
80d23275
DH
181 /* Initialize storage key device */
182 s390_skeys_init();
903fd80b
CI
183 /* Initialize storage attributes device */
184 s390_stattrib_init();
80d23275
DH
185}
186
6286b419
DH
187static void s390_init_ipl_dev(const char *kernel_filename,
188 const char *kernel_cmdline,
189 const char *initrd_filename, const char *firmware,
190 const char *netboot_fw, bool enforce_bios)
191{
192 Object *new = object_new(TYPE_S390_IPL);
193 DeviceState *dev = DEVICE(new);
d9b06db8 194 char *netboot_fw_prop;
6286b419
DH
195
196 if (kernel_filename) {
197 qdev_prop_set_string(dev, "kernel", kernel_filename);
198 }
199 if (initrd_filename) {
200 qdev_prop_set_string(dev, "initrd", initrd_filename);
201 }
202 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
203 qdev_prop_set_string(dev, "firmware", firmware);
6286b419 204 qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
d9b06db8
GK
205 netboot_fw_prop = object_property_get_str(new, "netboot_fw", &error_abort);
206 if (!strlen(netboot_fw_prop)) {
3c4e9baa
TH
207 qdev_prop_set_string(dev, "netboot_fw", netboot_fw);
208 }
d9b06db8 209 g_free(netboot_fw_prop);
6286b419 210 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL,
d2623129 211 new);
6286b419
DH
212 object_unref(new);
213 qdev_init_nofail(dev);
214}
215
216static void s390_create_virtio_net(BusState *bus, const char *name)
217{
218 int i;
219
220 for (i = 0; i < nb_nics; i++) {
221 NICInfo *nd = &nd_table[i];
222 DeviceState *dev;
223
224 if (!nd->model) {
225 nd->model = g_strdup("virtio");
226 }
227
228 qemu_check_nic_model(nd, "virtio");
229
3e80f690 230 dev = qdev_new(name);
6286b419 231 qdev_set_nic_properties(dev, nd);
3e80f690 232 qdev_realize_and_unref(dev, bus, &error_fatal);
6286b419
DH
233 }
234}
235
052888f0
TH
236static void s390_create_sclpconsole(const char *type, Chardev *chardev)
237{
238 DeviceState *dev;
239
3e80f690 240 dev = qdev_new(type);
052888f0 241 qdev_prop_set_chr(dev, "chardev", chardev);
3e80f690 242 qdev_realize_and_unref(dev, sclp_get_event_facility_bus(), &error_fatal);
052888f0
TH
243}
244
80d23275
DH
245static void ccw_init(MachineState *machine)
246{
a5c95808
CH
247 int ret;
248 VirtualCssBus *css_bus;
c1843e20 249 DeviceState *dev;
b6fe0124 250
1cf065fb 251 s390_sclp_init();
9138977b 252 /* init memory + setup max page size. Required for the CPU model */
3a12fc61 253 s390_memory_init(machine->ram);
a5c95808 254
d32bd032 255 /* init CPUs (incl. CPU model) early so s390_has_feature() works */
3720d335
YMZ
256 s390_init_cpus(machine);
257
c572d3f3
FL
258 s390_flic_init();
259
74b4c74d
DH
260 /* init the SIGP facility */
261 s390_init_sigp();
262
a51b3153
TK
263 /* create AP bridge and bus(es) */
264 s390_init_ap();
265
a5c95808
CH
266 /* get a BUS */
267 css_bus = virtual_css_bus_init();
3ef96221 268 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
5f31ade0
FA
269 machine->initrd_filename, "s390-ccw.img",
270 "s390-netboot.img", true);
a5c95808 271
3e80f690 272 dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE);
c1843e20 273 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
d2623129 274 OBJECT(dev));
3e80f690 275 qdev_realize_and_unref(dev, NULL, &error_fatal);
8cba80c3 276
a5c95808
CH
277 /* register hypercalls */
278 virtio_ccw_register_hcalls();
279
5e7164c5 280 s390_enable_css_support(s390_cpu_addr2state(0));
36699ab4
CH
281
282 ret = css_create_css_image(VIRTUAL_CSSID, true);
d69969e5 283
a5c95808 284 assert(ret == 0);
489c909f
HP
285 if (css_migration_enabled()) {
286 css_register_vmstate();
287 }
a5c95808
CH
288
289 /* Create VirtIO network adapters */
290 s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
3f9e59bb 291
052888f0
TH
292 /* init consoles */
293 if (serial_hd(0)) {
294 s390_create_sclpconsole("sclpconsole", serial_hd(0));
295 }
296 if (serial_hd(1)) {
297 s390_create_sclpconsole("sclplmconsole", serial_hd(1));
298 }
299
8046f374
DH
300 /* init the TOD clock */
301 s390_init_tod();
a5c95808
CH
302}
303
502edbf8
MR
304static void s390_cpu_plug(HotplugHandler *hotplug_dev,
305 DeviceState *dev, Error **errp)
306{
4dc3b151 307 MachineState *ms = MACHINE(hotplug_dev);
502edbf8 308 S390CPU *cpu = S390_CPU(dev);
4dc3b151
DH
309
310 g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu);
311 ms->possible_cpus->cpus[cpu->env.core_id].cpu = OBJECT(dev);
c5b93430
DH
312
313 if (dev->hotplugged) {
314 raise_irq_cpu_hotplug();
315 }
502edbf8
MR
316}
317
a30fb811
DH
318static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg)
319{
320 S390CPU *cpu = S390_CPU(cs);
321
322 s390_ipl_prepare_cpu(cpu);
323 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
324}
325
c3347ed0
JF
326static void s390_machine_unprotect(S390CcwMachineState *ms)
327{
328 s390_pv_vm_disable();
329 ms->pv = false;
0141e1b4
JF
330 migrate_del_blocker(pv_mig_blocker);
331 error_free_or_abort(&pv_mig_blocker);
b1697f63 332 qemu_balloon_inhibit(false);
c3347ed0
JF
333}
334
335static int s390_machine_protect(S390CcwMachineState *ms)
336{
0141e1b4 337 Error *local_err = NULL;
c3347ed0
JF
338 int rc;
339
b1697f63
JF
340 /*
341 * Ballooning on protected VMs needs support in the guest for
342 * sharing and unsharing balloon pages. Block ballooning for
343 * now, until we have a solution to make at least Linux guests
344 * either support it or fail gracefully.
345 */
346 qemu_balloon_inhibit(true);
0141e1b4
JF
347 error_setg(&pv_mig_blocker,
348 "protected VMs are currently not migrateable.");
349 rc = migrate_add_blocker(pv_mig_blocker, &local_err);
350 if (rc) {
b1697f63 351 qemu_balloon_inhibit(false);
0141e1b4
JF
352 error_report_err(local_err);
353 error_free_or_abort(&pv_mig_blocker);
354 return rc;
355 }
356
c3347ed0
JF
357 /* Create SE VM */
358 rc = s390_pv_vm_enable();
359 if (rc) {
b1697f63 360 qemu_balloon_inhibit(false);
0141e1b4
JF
361 migrate_del_blocker(pv_mig_blocker);
362 error_free_or_abort(&pv_mig_blocker);
c3347ed0
JF
363 return rc;
364 }
365
366 ms->pv = true;
367
368 /* Set SE header and unpack */
369 rc = s390_ipl_prepare_pv_header();
370 if (rc) {
371 goto out_err;
372 }
373
374 /* Decrypt image */
375 rc = s390_ipl_pv_unpack();
376 if (rc) {
377 goto out_err;
378 }
379
380 /* Verify integrity */
381 rc = s390_pv_verify();
382 if (rc) {
383 goto out_err;
384 }
385 return rc;
386
387out_err:
388 s390_machine_unprotect(ms);
389 return rc;
390}
391
c3347ed0
JF
392static void s390_pv_prepare_reset(S390CcwMachineState *ms)
393{
394 CPUState *cs;
395
396 if (!s390_is_pv()) {
397 return;
398 }
399 /* Unsharing requires all cpus to be stopped */
400 CPU_FOREACH(cs) {
401 s390_cpu_set_state(S390_CPU_STATE_STOPPED, S390_CPU(cs));
402 }
403 s390_pv_unshare();
9a432597 404 s390_pv_prep_reset();
c3347ed0
JF
405}
406
a0628599 407static void s390_machine_reset(MachineState *machine)
6286b419 408{
c3347ed0 409 S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
a30fb811
DH
410 enum s390_reset reset_type;
411 CPUState *cs, *t;
c3347ed0 412 S390CPU *cpu;
6286b419 413
a30fb811
DH
414 /* get the reset parameters, reset them once done */
415 s390_ipl_get_reset_request(&cs, &reset_type);
416
417 /* all CPUs are paused and synchronized at this point */
6286b419 418 s390_cmma_reset();
6286b419 419
c3347ed0
JF
420 cpu = S390_CPU(cs);
421
a30fb811
DH
422 switch (reset_type) {
423 case S390_RESET_EXTERNAL:
424 case S390_RESET_REIPL:
c3347ed0
JF
425 if (s390_is_pv()) {
426 s390_machine_unprotect(ms);
427 }
428
a30fb811
DH
429 qemu_devices_reset();
430 s390_crypto_reset();
431
432 /* configure and start the ipl CPU only */
433 run_on_cpu(cs, s390_do_cpu_ipl, RUN_ON_CPU_NULL);
434 break;
435 case S390_RESET_MODIFIED_CLEAR:
c3347ed0
JF
436 /*
437 * Susbsystem reset needs to be done before we unshare memory
438 * and lose access to VIRTIO structures in guest memory.
439 */
440 subsystem_reset();
441 s390_crypto_reset();
442 s390_pv_prepare_reset(ms);
a30fb811
DH
443 CPU_FOREACH(t) {
444 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
445 }
a30fb811
DH
446 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
447 break;
448 case S390_RESET_LOAD_NORMAL:
c3347ed0
JF
449 /*
450 * Susbsystem reset needs to be done before we unshare memory
451 * and lose access to VIRTIO structures in guest memory.
452 */
453 subsystem_reset();
454 s390_pv_prepare_reset(ms);
a30fb811 455 CPU_FOREACH(t) {
ec922733
JF
456 if (t == cs) {
457 continue;
458 }
a30fb811
DH
459 run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL);
460 }
a30fb811 461 run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL);
c3347ed0
JF
462 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
463 break;
464 case S390_RESET_PV: /* Subcode 10 */
465 subsystem_reset();
466 s390_crypto_reset();
467
468 CPU_FOREACH(t) {
469 if (t == cs) {
470 continue;
471 }
472 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
473 }
474 run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL);
475
476 if (s390_machine_protect(ms)) {
fbc1384c 477 s390_pv_inject_reset_error(cs);
c3347ed0
JF
478 /*
479 * Continue after the diag308 so the guest knows something
480 * went wrong.
481 */
482 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
483 return;
484 }
485
a30fb811
DH
486 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
487 break;
488 default:
489 g_assert_not_reached();
490 }
491 s390_ipl_clear_reset_request();
6286b419
DH
492}
493
502edbf8
MR
494static void s390_machine_device_plug(HotplugHandler *hotplug_dev,
495 DeviceState *dev, Error **errp)
496{
497 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
498 s390_cpu_plug(hotplug_dev, dev, errp);
499 }
500}
501
f2f3beb0
DH
502static void s390_machine_device_unplug_request(HotplugHandler *hotplug_dev,
503 DeviceState *dev, Error **errp)
504{
505 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
506 error_setg(errp, "CPU hot unplug not supported on this machine");
507 return;
508 }
509}
510
81ce6aa5 511static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms,
4dc3b151
DH
512 unsigned cpu_index)
513{
81ce6aa5
DH
514 MachineClass *mc = MACHINE_GET_CLASS(ms);
515 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
4dc3b151 516
81ce6aa5
DH
517 assert(cpu_index < possible_cpus->len);
518 return possible_cpus->cpus[cpu_index].props;
4dc3b151
DH
519}
520
521static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms)
522{
523 int i;
ae71ed86 524 unsigned int max_cpus = ms->smp.max_cpus;
4dc3b151
DH
525
526 if (ms->possible_cpus) {
527 g_assert(ms->possible_cpus && ms->possible_cpus->len == max_cpus);
528 return ms->possible_cpus;
529 }
530
531 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
532 sizeof(CPUArchId) * max_cpus);
533 ms->possible_cpus->len = max_cpus;
534 for (i = 0; i < ms->possible_cpus->len; i++) {
d342eb76 535 ms->possible_cpus->cpus[i].type = ms->cpu_type;
4dc3b151
DH
536 ms->possible_cpus->cpus[i].vcpus_count = 1;
537 ms->possible_cpus->cpus[i].arch_id = i;
538 ms->possible_cpus->cpus[i].props.has_core_id = true;
539 ms->possible_cpus->cpus[i].props.core_id = i;
540 }
541
542 return ms->possible_cpus;
543}
544
502edbf8
MR
545static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
546 DeviceState *dev)
547{
548 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
549 return HOTPLUG_HANDLER(machine);
550 }
551 return NULL;
552}
553
a0628599
LX
554static void s390_hot_add_cpu(MachineState *machine,
555 const int64_t id, Error **errp)
a006b67f 556{
524d18d8
DH
557 ObjectClass *oc;
558
559 g_assert(machine->possible_cpus->cpus[0].cpu);
560 oc = OBJECT_CLASS(CPU_GET_CLASS(machine->possible_cpus->cpus[0].cpu));
a006b67f 561
524d18d8 562 s390x_new_cpu(object_class_get_name(oc), id, errp);
a006b67f
MR
563}
564
6286b419
DH
565static void s390_nmi(NMIState *n, int cpu_index, Error **errp)
566{
567 CPUState *cs = qemu_get_cpu(cpu_index);
568
0fc60ca5 569 s390_cpu_restart(S390_CPU(cs));
6286b419
DH
570}
571
5c30ef93
CB
572static ram_addr_t s390_fixup_ram_size(ram_addr_t sz)
573{
574 /* same logic as in sclp.c */
575 int increment_size = 20;
576 ram_addr_t newsz;
577
578 while ((sz >> increment_size) > MAX_STORAGE_INCREMENTS) {
579 increment_size++;
580 }
581 newsz = sz >> increment_size << increment_size;
582
583 if (sz != newsz) {
584 qemu_printf("Ram size %" PRIu64 "MB was fixed up to %" PRIu64
585 "MB to match machine restrictions. Consider updating "
586 "the guest definition.\n", (uint64_t) (sz / MiB),
587 (uint64_t) (newsz / MiB));
588 }
589 return newsz;
590}
591
d07aa7c7
AK
592static void ccw_machine_class_init(ObjectClass *oc, void *data)
593{
594 MachineClass *mc = MACHINE_CLASS(oc);
3dd7852f 595 NMIClass *nc = NMI_CLASS(oc);
502edbf8 596 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
9700230b 597 S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
d07aa7c7 598
9700230b 599 s390mc->ri_allowed = true;
e73316d5 600 s390mc->cpu_model_allowed = true;
e996583e 601 s390mc->css_migration_enabled = true;
28221f9c 602 s390mc->hpage_1m_allowed = true;
d07aa7c7 603 mc->init = ccw_init;
db3b2566 604 mc->reset = s390_machine_reset;
a006b67f 605 mc->hot_add_cpu = s390_hot_add_cpu;
d07aa7c7
AK
606 mc->block_default_type = IF_VIRTIO;
607 mc->no_cdrom = 1;
608 mc->no_floppy = 1;
d07aa7c7
AK
609 mc->no_parallel = 1;
610 mc->no_sdcard = 1;
f42dc44a 611 mc->max_cpus = S390_MAX_CPUS;
4dc3b151 612 mc->has_hotpluggable_cpus = true;
debbdc00 613 assert(!mc->get_hotplug_handler);
502edbf8 614 mc->get_hotplug_handler = s390_get_hotplug_handler;
4dc3b151
DH
615 mc->cpu_index_to_instance_props = s390_cpu_index_to_props;
616 mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids;
b6805e12
IM
617 /* it is overridden with 'host' cpu *in kvm_arch_init* */
618 mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu");
502edbf8 619 hc->plug = s390_machine_device_plug;
f2f3beb0 620 hc->unplug_request = s390_machine_device_unplug_request;
3dd7852f 621 nc->nmi_monitor_handler = s390_nmi;
3a12fc61 622 mc->default_ram_id = "s390.ram";
d07aa7c7
AK
623}
624
2eb1cd07
TK
625static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
626{
627 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
628
629 return ms->aes_key_wrap;
630}
631
632static inline void machine_set_aes_key_wrap(Object *obj, bool value,
633 Error **errp)
634{
635 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
636
637 ms->aes_key_wrap = value;
638}
639
640static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
641{
642 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
643
644 return ms->dea_key_wrap;
645}
646
647static inline void machine_set_dea_key_wrap(Object *obj, bool value,
648 Error **errp)
649{
650 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
651
652 ms->dea_key_wrap = value;
653}
654
cec8bbf7 655static S390CcwMachineClass *current_mc;
9700230b 656
3e0209bb
TH
657/*
658 * Get the class of the s390-ccw-virtio machine that is currently in use.
659 * Note: libvirt is using the "none" machine to probe for the features of the
660 * host CPU, so in case this is called with the "none" machine, the function
661 * returns the TYPE_S390_CCW_MACHINE base class. In this base class, all the
662 * various "*_allowed" variables are enabled, so that the *_allowed() wrappers
663 * below return the correct default value for the "none" machine.
664 *
665 * Attention! Do *not* add additional new wrappers for CPU features (e.g. like
666 * the ri_allowed() wrapper) via this mechanism anymore. CPU features should
667 * be handled via the CPU models, i.e. checking with cpu_model_allowed() during
668 * CPU initialization and s390_has_feat() later should be sufficient.
669 */
cec8bbf7
HP
670static S390CcwMachineClass *get_machine_class(void)
671{
672 if (unlikely(!current_mc)) {
392529cb 673 /*
cec8bbf7
HP
674 * No s390 ccw machine was instantiated, we are likely to
675 * be called for the 'none' machine. The properties will
676 * have their after-initialization values.
677 */
678 current_mc = S390_MACHINE_CLASS(
679 object_class_by_name(TYPE_S390_CCW_MACHINE));
9700230b 680 }
cec8bbf7 681 return current_mc;
9700230b
FZ
682}
683
cec8bbf7 684bool ri_allowed(void)
e73316d5 685{
cec8bbf7
HP
686 return get_machine_class()->ri_allowed;
687}
688
689bool cpu_model_allowed(void)
690{
cec8bbf7 691 return get_machine_class()->cpu_model_allowed;
e73316d5
CB
692}
693
28221f9c
JF
694bool hpage_1m_allowed(void)
695{
28221f9c
JF
696 return get_machine_class()->hpage_1m_allowed;
697}
698
7104bae9
FA
699static char *machine_get_loadparm(Object *obj, Error **errp)
700{
701 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
702
703 return g_memdup(ms->loadparm, sizeof(ms->loadparm));
704}
705
706static void machine_set_loadparm(Object *obj, const char *val, Error **errp)
707{
708 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
709 int i;
710
711 for (i = 0; i < sizeof(ms->loadparm) && val[i]; i++) {
95a5befc 712 uint8_t c = qemu_toupper(val[i]); /* mimic HMC */
7104bae9
FA
713
714 if (('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || (c == '.') ||
715 (c == ' ')) {
716 ms->loadparm[i] = c;
717 } else {
718 error_setg(errp, "LOADPARM: invalid character '%c' (ASCII 0x%02x)",
719 c, c);
720 return;
721 }
722 }
723
724 for (; i < sizeof(ms->loadparm); i++) {
725 ms->loadparm[i] = ' '; /* pad right with spaces */
726 }
727}
2eb1cd07
TK
728static inline void s390_machine_initfn(Object *obj)
729{
730 object_property_add_bool(obj, "aes-key-wrap",
731 machine_get_aes_key_wrap,
d2623129 732 machine_set_aes_key_wrap);
2eb1cd07 733 object_property_set_description(obj, "aes-key-wrap",
7eecec7d 734 "enable/disable AES key wrapping using the CPACF wrapping key");
2eb1cd07
TK
735 object_property_set_bool(obj, true, "aes-key-wrap", NULL);
736
737 object_property_add_bool(obj, "dea-key-wrap",
738 machine_get_dea_key_wrap,
d2623129 739 machine_set_dea_key_wrap);
2eb1cd07 740 object_property_set_description(obj, "dea-key-wrap",
7eecec7d 741 "enable/disable DEA key wrapping using the CPACF wrapping key");
2eb1cd07 742 object_property_set_bool(obj, true, "dea-key-wrap", NULL);
7104bae9 743 object_property_add_str(obj, "loadparm",
d2623129 744 machine_get_loadparm, machine_set_loadparm);
7104bae9
FA
745 object_property_set_description(obj, "loadparm",
746 "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted"
747 " to upper case) to pass to machine loader, boot manager,"
7eecec7d 748 " and guest kernel");
2eb1cd07
TK
749}
750
d07aa7c7
AK
751static const TypeInfo ccw_machine_info = {
752 .name = TYPE_S390_CCW_MACHINE,
753 .parent = TYPE_MACHINE,
c4d3c0a2 754 .abstract = true,
2eb1cd07
TK
755 .instance_size = sizeof(S390CcwMachineState),
756 .instance_init = s390_machine_initfn,
9700230b 757 .class_size = sizeof(S390CcwMachineClass),
d07aa7c7 758 .class_init = ccw_machine_class_init,
3dd7852f
AK
759 .interfaces = (InterfaceInfo[]) {
760 { TYPE_NMI },
502edbf8 761 { TYPE_HOTPLUG_HANDLER},
3dd7852f
AK
762 { }
763 },
a5c95808
CH
764};
765
52629b3b
HP
766bool css_migration_enabled(void)
767{
768 return get_machine_class()->css_migration_enabled;
769}
770
4fca6548
JF
771#define DEFINE_CCW_MACHINE(suffix, verstr, latest) \
772 static void ccw_machine_##suffix##_class_init(ObjectClass *oc, \
773 void *data) \
774 { \
775 MachineClass *mc = MACHINE_CLASS(oc); \
776 ccw_machine_##suffix##_class_options(mc); \
777 mc->desc = "VirtIO-ccw based S390 machine v" verstr; \
778 if (latest) { \
779 mc->alias = "s390-ccw-virtio"; \
ea0ac7f6 780 mc->is_default = true; \
4fca6548
JF
781 } \
782 } \
783 static void ccw_machine_##suffix##_instance_init(Object *obj) \
784 { \
785 MachineState *machine = MACHINE(obj); \
cec8bbf7 786 current_mc = S390_MACHINE_CLASS(MACHINE_GET_CLASS(machine)); \
4fca6548
JF
787 ccw_machine_##suffix##_instance_options(machine); \
788 } \
789 static const TypeInfo ccw_machine_##suffix##_info = { \
790 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr), \
791 .parent = TYPE_S390_CCW_MACHINE, \
792 .class_init = ccw_machine_##suffix##_class_init, \
793 .instance_init = ccw_machine_##suffix##_instance_init, \
794 }; \
795 static void ccw_machine_register_##suffix(void) \
796 { \
797 type_register_static(&ccw_machine_##suffix##_info); \
798 } \
0e6aac87 799 type_init(ccw_machine_register_##suffix)
4fca6548 800
541aaa1d
CH
801static void ccw_machine_5_1_instance_options(MachineState *machine)
802{
803}
804
805static void ccw_machine_5_1_class_options(MachineClass *mc)
806{
807}
808DEFINE_CCW_MACHINE(5_1, "5.1", true);
809
3eb74d20
CH
810static void ccw_machine_5_0_instance_options(MachineState *machine)
811{
541aaa1d 812 ccw_machine_5_1_instance_options(machine);
3eb74d20
CH
813}
814
815static void ccw_machine_5_0_class_options(MachineClass *mc)
816{
541aaa1d
CH
817 ccw_machine_5_1_class_options(mc);
818 compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
3eb74d20 819}
541aaa1d 820DEFINE_CCW_MACHINE(5_0, "5.0", false);
3eb74d20 821
9aec2e52
CH
822static void ccw_machine_4_2_instance_options(MachineState *machine)
823{
3eb74d20 824 ccw_machine_5_0_instance_options(machine);
9aec2e52
CH
825}
826
827static void ccw_machine_4_2_class_options(MachineClass *mc)
828{
3eb74d20 829 ccw_machine_5_0_class_options(mc);
5c30ef93 830 mc->fixup_ram_size = s390_fixup_ram_size;
5f258577 831 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
9aec2e52 832}
3eb74d20 833DEFINE_CCW_MACHINE(4_2, "4.2", false);
9aec2e52 834
9bf2650b
CH
835static void ccw_machine_4_1_instance_options(MachineState *machine)
836{
faa40177 837 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_1 };
9aec2e52 838 ccw_machine_4_2_instance_options(machine);
faa40177 839 s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
9bf2650b
CH
840}
841
842static void ccw_machine_4_1_class_options(MachineClass *mc)
843{
9aec2e52
CH
844 ccw_machine_4_2_class_options(mc);
845 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
9bf2650b 846}
9aec2e52 847DEFINE_CCW_MACHINE(4_1, "4.1", false);
9bf2650b 848
8c7b0c73
CH
849static void ccw_machine_4_0_instance_options(MachineState *machine)
850{
08ef92d5 851 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_0 };
9bf2650b 852 ccw_machine_4_1_instance_options(machine);
08ef92d5 853 s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
8c7b0c73
CH
854}
855
856static void ccw_machine_4_0_class_options(MachineClass *mc)
857{
9bf2650b
CH
858 ccw_machine_4_1_class_options(mc);
859 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
8c7b0c73 860}
9bf2650b 861DEFINE_CCW_MACHINE(4_0, "4.0", false);
8c7b0c73 862
9ca056d6
CH
863static void ccw_machine_3_1_instance_options(MachineState *machine)
864{
d646b16b 865 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V3_1 };
8c7b0c73 866 ccw_machine_4_0_instance_options(machine);
84176c79
CW
867 s390_cpudef_featoff_greater(14, 1, S390_FEAT_MULTIPLE_EPOCH);
868 s390_cpudef_group_featoff_greater(14, 1, S390_FEAT_GROUP_MULTIPLE_EPOCH_PTFF);
d646b16b 869 s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
9ca056d6
CH
870}
871
872static void ccw_machine_3_1_class_options(MachineClass *mc)
873{
8c7b0c73 874 ccw_machine_4_0_class_options(mc);
abd93cc7 875 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
9ca056d6 876}
8c7b0c73 877DEFINE_CCW_MACHINE(3_1, "3.1", false);
9ca056d6 878
2c5a2eef 879static void ccw_machine_3_0_instance_options(MachineState *machine)
7a9cb3ad 880{
9ca056d6 881 ccw_machine_3_1_instance_options(machine);
7a9cb3ad
CH
882}
883
2c5a2eef 884static void ccw_machine_3_0_class_options(MachineClass *mc)
7a9cb3ad 885{
28221f9c
JF
886 S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
887
888 s390mc->hpage_1m_allowed = false;
9ca056d6 889 ccw_machine_3_1_class_options(mc);
ddb3235d 890 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
7a9cb3ad 891}
9ca056d6 892DEFINE_CCW_MACHINE(3_0, "3.0", false);
7a9cb3ad 893
67ee0cef
CH
894static void ccw_machine_2_12_instance_options(MachineState *machine)
895{
2c5a2eef 896 ccw_machine_3_0_instance_options(machine);
87273151
CB
897 s390_cpudef_featoff_greater(11, 1, S390_FEAT_PPA15);
898 s390_cpudef_featoff_greater(11, 1, S390_FEAT_BPB);
67ee0cef
CH
899}
900
901static void ccw_machine_2_12_class_options(MachineClass *mc)
902{
2c5a2eef 903 ccw_machine_3_0_class_options(mc);
0d47310b 904 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
67ee0cef 905}
7a9cb3ad 906DEFINE_CCW_MACHINE(2_12, "2.12", false);
67ee0cef 907
70d8d9a0
CH
908static void ccw_machine_2_11_instance_options(MachineState *machine)
909{
35b4df64 910 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V2_11 };
67ee0cef 911 ccw_machine_2_12_instance_options(machine);
35b4df64
DH
912
913 /* before 2.12 we emulated the very first z900 */
914 s390_set_qemu_cpu_model(0x2064, 7, 1, qemu_cpu_feat);
70d8d9a0
CH
915}
916
917static void ccw_machine_2_11_class_options(MachineClass *mc)
918{
88cbe073 919 static GlobalProperty compat[] = {
6c36bddf 920 { TYPE_SCLP_EVENT_FACILITY, "allow_all_mask_sizes", "off", },
88cbe073
MAL
921 };
922
67ee0cef 923 ccw_machine_2_12_class_options(mc);
43df70a9 924 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
88cbe073 925 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
70d8d9a0 926}
67ee0cef 927DEFINE_CCW_MACHINE(2_11, "2.11", false);
70d8d9a0 928
10890873
CH
929static void ccw_machine_2_10_instance_options(MachineState *machine)
930{
70d8d9a0 931 ccw_machine_2_11_instance_options(machine);
10890873
CH
932}
933
934static void ccw_machine_2_10_class_options(MachineClass *mc)
935{
70d8d9a0 936 ccw_machine_2_11_class_options(mc);
503224f4 937 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
10890873 938}
70d8d9a0 939DEFINE_CCW_MACHINE(2_10, "2.10", false);
10890873 940
113725a6
CH
941static void ccw_machine_2_9_instance_options(MachineState *machine)
942{
10890873 943 ccw_machine_2_10_instance_options(machine);
7223bcce
JH
944 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP);
945 s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2);
3b00f702
YMZ
946 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ZPCI);
947 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_INT_SUPPRESSION);
948 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_EVENT_NOTIFICATION);
113725a6
CH
949}
950
951static void ccw_machine_2_9_class_options(MachineClass *mc)
952{
52629b3b 953 S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
88cbe073 954 static GlobalProperty compat[] = {
6c36bddf 955 { TYPE_S390_STATTRIB, "migration-enabled", "off", },
88cbe073 956 };
52629b3b 957
10890873 958 ccw_machine_2_10_class_options(mc);
3e803152 959 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
88cbe073 960 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
52629b3b 961 s390mc->css_migration_enabled = false;
113725a6 962}
10890873 963DEFINE_CCW_MACHINE(2_9, "2.9", false);
113725a6 964
61823988
CH
965static void ccw_machine_2_8_instance_options(MachineState *machine)
966{
113725a6 967 ccw_machine_2_9_instance_options(machine);
61823988
CH
968}
969
970static void ccw_machine_2_8_class_options(MachineClass *mc)
971{
88cbe073 972 static GlobalProperty compat[] = {
6c36bddf 973 { TYPE_S390_FLIC_COMMON, "adapter_routes_max_batch", "64", },
88cbe073
MAL
974 };
975
113725a6 976 ccw_machine_2_9_class_options(mc);
edc24ccd 977 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
88cbe073 978 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
61823988 979}
113725a6 980DEFINE_CCW_MACHINE(2_8, "2.8", false);
61823988 981
946e55f3
CH
982static void ccw_machine_2_7_instance_options(MachineState *machine)
983{
61823988 984 ccw_machine_2_8_instance_options(machine);
946e55f3
CH
985}
986
987static void ccw_machine_2_7_class_options(MachineClass *mc)
988{
e73316d5
CB
989 S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
990
991 s390mc->cpu_model_allowed = false;
61823988 992 ccw_machine_2_8_class_options(mc);
5a995064 993 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
946e55f3 994}
61823988 995DEFINE_CCW_MACHINE(2_7, "2.7", false);
946e55f3 996
4fca6548 997static void ccw_machine_2_6_instance_options(MachineState *machine)
c4d3c0a2 998{
946e55f3 999 ccw_machine_2_7_instance_options(machine);
c4d3c0a2
CB
1000}
1001
4fca6548 1002static void ccw_machine_2_6_class_options(MachineClass *mc)
84b48ad6 1003{
9700230b 1004 S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
88cbe073 1005 static GlobalProperty compat[] = {
6c36bddf
EH
1006 { TYPE_S390_IPL, "iplbext_migration", "off", },
1007 { TYPE_VIRTUAL_CSS_BRIDGE, "css_dev_path", "off", },
88cbe073 1008 };
9700230b
FZ
1009
1010 s390mc->ri_allowed = false;
946e55f3 1011 ccw_machine_2_7_class_options(mc);
ff8f261f 1012 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
88cbe073 1013 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
84b48ad6 1014}
946e55f3 1015DEFINE_CCW_MACHINE(2_6, "2.6", false);
84b48ad6 1016
4fca6548
JF
1017static void ccw_machine_2_5_instance_options(MachineState *machine)
1018{
946e55f3 1019 ccw_machine_2_6_instance_options(machine);
4fca6548 1020}
84b48ad6 1021
4fca6548 1022static void ccw_machine_2_5_class_options(MachineClass *mc)
b21b7598 1023{
946e55f3 1024 ccw_machine_2_6_class_options(mc);
fe759610 1025 compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len);
4fca6548
JF
1026}
1027DEFINE_CCW_MACHINE(2_5, "2.5", false);
b21b7598 1028
4fca6548
JF
1029static void ccw_machine_2_4_instance_options(MachineState *machine)
1030{
1031 ccw_machine_2_5_instance_options(machine);
b21b7598
CH
1032}
1033
4fca6548
JF
1034static void ccw_machine_2_4_class_options(MachineClass *mc)
1035{
88cbe073 1036 static GlobalProperty compat[] = {
6c36bddf
EH
1037 { TYPE_S390_SKEYS, "migration-enabled", "off", },
1038 { "virtio-blk-ccw", "max_revision", "0", },
1039 { "virtio-balloon-ccw", "max_revision", "0", },
1040 { "virtio-serial-ccw", "max_revision", "0", },
1041 { "virtio-9p-ccw", "max_revision", "0", },
1042 { "virtio-rng-ccw", "max_revision", "0", },
1043 { "virtio-net-ccw", "max_revision", "0", },
1044 { "virtio-scsi-ccw", "max_revision", "0", },
1045 { "vhost-scsi-ccw", "max_revision", "0", },
88cbe073
MAL
1046 };
1047
946e55f3 1048 ccw_machine_2_5_class_options(mc);
2f99b9c2 1049 compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len);
88cbe073 1050 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4fca6548
JF
1051}
1052DEFINE_CCW_MACHINE(2_4, "2.4", false);
b21b7598 1053
d07aa7c7 1054static void ccw_machine_register_types(void)
a5c95808 1055{
d07aa7c7 1056 type_register_static(&ccw_machine_info);
a5c95808
CH
1057}
1058
d07aa7c7 1059type_init(ccw_machine_register_types)