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