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