xref: /openbmc/qemu/hw/s390x/s390-virtio-ccw.c (revision ca27b5eb)
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/s390x/s390-virtio-hcall.h"
21 #include "hw/s390x/sclp.h"
22 #include "hw/s390x/s390_flic.h"
23 #include "hw/s390x/ioinst.h"
24 #include "hw/s390x/css.h"
25 #include "virtio-ccw.h"
26 #include "qemu/config-file.h"
27 #include "qemu/ctype.h"
28 #include "qemu/error-report.h"
29 #include "qemu/option.h"
30 #include "qemu/qemu-print.h"
31 #include "s390-pci-bus.h"
32 #include "sysemu/reset.h"
33 #include "hw/s390x/storage-keys.h"
34 #include "hw/s390x/storage-attributes.h"
35 #include "hw/s390x/event-facility.h"
36 #include "ipl.h"
37 #include "hw/s390x/s390-virtio-ccw.h"
38 #include "hw/s390x/css-bridge.h"
39 #include "hw/s390x/ap-bridge.h"
40 #include "migration/register.h"
41 #include "cpu_models.h"
42 #include "hw/nmi.h"
43 #include "hw/qdev-properties.h"
44 #include "hw/s390x/tod.h"
45 #include "sysemu/sysemu.h"
46 #include "sysemu/balloon.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     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 
80 out:
81     object_unref(OBJECT(cpu));
82     if (err) {
83         error_propagate(errp, err);
84         cpu = NULL;
85     }
86     return cpu;
87 }
88 
89 static void s390_init_cpus(MachineState *machine)
90 {
91     MachineClass *mc = MACHINE_GET_CLASS(machine);
92     int i;
93 
94     /* initialize possible_cpus */
95     mc->possible_cpu_arch_ids(machine);
96 
97     for (i = 0; i < machine->smp.cpus; i++) {
98         s390x_new_cpu(machine->cpu_type, i, &error_fatal);
99     }
100 }
101 
102 static const char *const reset_dev_types[] = {
103     TYPE_VIRTUAL_CSS_BRIDGE,
104     "s390-sclp-event-facility",
105     "s390-flic",
106     "diag288",
107 };
108 
109 static void subsystem_reset(void)
110 {
111     DeviceState *dev;
112     int i;
113 
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         }
119     }
120 }
121 
122 static 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     }
136     if (queue >= VIRTIO_QUEUE_MAX) {
137         return -EINVAL;
138     }
139     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
140     return 0;
141 
142 }
143 
144 static 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 
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     Error *local_err = NULL;
168 
169     /* allocate RAM for core */
170     memory_region_add_subregion(sysmem, 0, ram);
171 
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      */
176     s390_set_max_pagesize(qemu_maxrampagesize(), &local_err);
177     if (local_err) {
178         error_report_err(local_err);
179         exit(EXIT_FAILURE);
180     }
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_init_nofail(dev);
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_create(bus, name);
231         qdev_set_nic_properties(dev, nd);
232         qdev_init_nofail(dev);
233     }
234 }
235 
236 static void s390_create_sclpconsole(const char *type, Chardev *chardev)
237 {
238     DeviceState *dev;
239 
240     dev = qdev_create(sclp_get_event_facility_bus(), type);
241     qdev_prop_set_chr(dev, "chardev", chardev);
242     qdev_init_nofail(dev);
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     s390_flic_init();
259 
260     /* init the SIGP facility */
261     s390_init_sigp();
262 
263     /* create AP bridge and bus(es) */
264     s390_init_ap();
265 
266     /* get a BUS */
267     css_bus = virtual_css_bus_init();
268     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
269                       machine->initrd_filename, "s390-ccw.img",
270                       "s390-netboot.img", true);
271 
272     dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
273     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
274                               OBJECT(dev));
275     qdev_init_nofail(dev);
276 
277     /* register hypercalls */
278     virtio_ccw_register_hcalls();
279 
280     s390_enable_css_support(s390_cpu_addr2state(0));
281 
282     ret = css_create_css_image(VIRTUAL_CSSID, true);
283 
284     assert(ret == 0);
285     if (css_migration_enabled()) {
286         css_register_vmstate();
287     }
288 
289     /* Create VirtIO network adapters */
290     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
291 
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 
300     /* init the TOD clock */
301     s390_init_tod();
302 }
303 
304 static void s390_cpu_plug(HotplugHandler *hotplug_dev,
305                         DeviceState *dev, Error **errp)
306 {
307     MachineState *ms = MACHINE(hotplug_dev);
308     S390CPU *cpu = S390_CPU(dev);
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);
312 
313     if (dev->hotplugged) {
314         raise_irq_cpu_hotplug();
315     }
316 }
317 
318 static 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 
326 static void s390_machine_unprotect(S390CcwMachineState *ms)
327 {
328     s390_pv_vm_disable();
329     ms->pv = false;
330     migrate_del_blocker(pv_mig_blocker);
331     error_free_or_abort(&pv_mig_blocker);
332     qemu_balloon_inhibit(false);
333 }
334 
335 static int s390_machine_protect(S390CcwMachineState *ms)
336 {
337     Error *local_err = NULL;
338     int rc;
339 
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);
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) {
351         qemu_balloon_inhibit(false);
352         error_report_err(local_err);
353         error_free_or_abort(&pv_mig_blocker);
354         return rc;
355     }
356 
357     /* Create SE VM */
358     rc = s390_pv_vm_enable();
359     if (rc) {
360         qemu_balloon_inhibit(false);
361         migrate_del_blocker(pv_mig_blocker);
362         error_free_or_abort(&pv_mig_blocker);
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 
387 out_err:
388     s390_machine_unprotect(ms);
389     return rc;
390 }
391 
392 static 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();
404     s390_pv_prep_reset();
405 }
406 
407 static void s390_machine_reset(MachineState *machine)
408 {
409     S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
410     enum s390_reset reset_type;
411     CPUState *cs, *t;
412     S390CPU *cpu;
413 
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 */
418     s390_cmma_reset();
419 
420     cpu = S390_CPU(cs);
421 
422     switch (reset_type) {
423     case S390_RESET_EXTERNAL:
424     case S390_RESET_REIPL:
425         if (s390_is_pv()) {
426             s390_machine_unprotect(ms);
427         }
428 
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:
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);
443         CPU_FOREACH(t) {
444             run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
445         }
446         run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
447         break;
448     case S390_RESET_LOAD_NORMAL:
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);
455         CPU_FOREACH(t) {
456             if (t == cs) {
457                 continue;
458             }
459             run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL);
460         }
461         run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL);
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)) {
477             s390_pv_inject_reset_error(cs);
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 
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();
492 }
493 
494 static 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 
502 static 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 
511 static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms,
512                                                      unsigned cpu_index)
513 {
514     MachineClass *mc = MACHINE_GET_CLASS(ms);
515     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
516 
517     assert(cpu_index < possible_cpus->len);
518     return possible_cpus->cpus[cpu_index].props;
519 }
520 
521 static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms)
522 {
523     int i;
524     unsigned int max_cpus = ms->smp.max_cpus;
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++) {
535         ms->possible_cpus->cpus[i].type = ms->cpu_type;
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 
545 static 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 
554 static void s390_hot_add_cpu(MachineState *machine,
555                              const int64_t id, Error **errp)
556 {
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));
561 
562     s390x_new_cpu(object_class_get_name(oc), id, errp);
563 }
564 
565 static void s390_nmi(NMIState *n, int cpu_index, Error **errp)
566 {
567     CPUState *cs = qemu_get_cpu(cpu_index);
568 
569     s390_cpu_restart(S390_CPU(cs));
570 }
571 
572 static 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 
592 static void ccw_machine_class_init(ObjectClass *oc, void *data)
593 {
594     MachineClass *mc = MACHINE_CLASS(oc);
595     NMIClass *nc = NMI_CLASS(oc);
596     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
597     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
598 
599     s390mc->ri_allowed = true;
600     s390mc->cpu_model_allowed = true;
601     s390mc->css_migration_enabled = true;
602     s390mc->hpage_1m_allowed = true;
603     mc->init = ccw_init;
604     mc->reset = s390_machine_reset;
605     mc->hot_add_cpu = s390_hot_add_cpu;
606     mc->block_default_type = IF_VIRTIO;
607     mc->no_cdrom = 1;
608     mc->no_floppy = 1;
609     mc->no_parallel = 1;
610     mc->no_sdcard = 1;
611     mc->max_cpus = S390_MAX_CPUS;
612     mc->has_hotpluggable_cpus = true;
613     assert(!mc->get_hotplug_handler);
614     mc->get_hotplug_handler = s390_get_hotplug_handler;
615     mc->cpu_index_to_instance_props = s390_cpu_index_to_props;
616     mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids;
617     /* it is overridden with 'host' cpu *in kvm_arch_init* */
618     mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu");
619     hc->plug = s390_machine_device_plug;
620     hc->unplug_request = s390_machine_device_unplug_request;
621     nc->nmi_monitor_handler = s390_nmi;
622     mc->default_ram_id = "s390.ram";
623 }
624 
625 static 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 
632 static 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 
640 static 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 
647 static 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 
655 static S390CcwMachineClass *current_mc;
656 
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  */
670 static S390CcwMachineClass *get_machine_class(void)
671 {
672     if (unlikely(!current_mc)) {
673         /*
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));
680     }
681     return current_mc;
682 }
683 
684 bool ri_allowed(void)
685 {
686     return get_machine_class()->ri_allowed;
687 }
688 
689 bool cpu_model_allowed(void)
690 {
691     return get_machine_class()->cpu_model_allowed;
692 }
693 
694 bool hpage_1m_allowed(void)
695 {
696     return get_machine_class()->hpage_1m_allowed;
697 }
698 
699 static 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 
706 static 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++) {
712         uint8_t c = qemu_toupper(val[i]); /* mimic HMC */
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 }
728 static inline void s390_machine_initfn(Object *obj)
729 {
730     object_property_add_bool(obj, "aes-key-wrap",
731                              machine_get_aes_key_wrap,
732                              machine_set_aes_key_wrap);
733     object_property_set_description(obj, "aes-key-wrap",
734             "enable/disable AES key wrapping using the CPACF wrapping key");
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,
739                              machine_set_dea_key_wrap);
740     object_property_set_description(obj, "dea-key-wrap",
741             "enable/disable DEA key wrapping using the CPACF wrapping key");
742     object_property_set_bool(obj, true, "dea-key-wrap", NULL);
743     object_property_add_str(obj, "loadparm",
744             machine_get_loadparm, machine_set_loadparm);
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,"
748             " and guest kernel");
749 }
750 
751 static const TypeInfo ccw_machine_info = {
752     .name          = TYPE_S390_CCW_MACHINE,
753     .parent        = TYPE_MACHINE,
754     .abstract      = true,
755     .instance_size = sizeof(S390CcwMachineState),
756     .instance_init = s390_machine_initfn,
757     .class_size = sizeof(S390CcwMachineClass),
758     .class_init    = ccw_machine_class_init,
759     .interfaces = (InterfaceInfo[]) {
760         { TYPE_NMI },
761         { TYPE_HOTPLUG_HANDLER},
762         { }
763     },
764 };
765 
766 bool css_migration_enabled(void)
767 {
768     return get_machine_class()->css_migration_enabled;
769 }
770 
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";                                    \
780             mc->is_default = true;                                            \
781         }                                                                     \
782     }                                                                         \
783     static void ccw_machine_##suffix##_instance_init(Object *obj)             \
784     {                                                                         \
785         MachineState *machine = MACHINE(obj);                                 \
786         current_mc = S390_MACHINE_CLASS(MACHINE_GET_CLASS(machine));          \
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     }                                                                         \
799     type_init(ccw_machine_register_##suffix)
800 
801 static void ccw_machine_5_1_instance_options(MachineState *machine)
802 {
803 }
804 
805 static void ccw_machine_5_1_class_options(MachineClass *mc)
806 {
807 }
808 DEFINE_CCW_MACHINE(5_1, "5.1", true);
809 
810 static void ccw_machine_5_0_instance_options(MachineState *machine)
811 {
812     ccw_machine_5_1_instance_options(machine);
813 }
814 
815 static void ccw_machine_5_0_class_options(MachineClass *mc)
816 {
817     ccw_machine_5_1_class_options(mc);
818     compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
819 }
820 DEFINE_CCW_MACHINE(5_0, "5.0", false);
821 
822 static void ccw_machine_4_2_instance_options(MachineState *machine)
823 {
824     ccw_machine_5_0_instance_options(machine);
825 }
826 
827 static void ccw_machine_4_2_class_options(MachineClass *mc)
828 {
829     ccw_machine_5_0_class_options(mc);
830     mc->fixup_ram_size = s390_fixup_ram_size;
831     compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
832 }
833 DEFINE_CCW_MACHINE(4_2, "4.2", false);
834 
835 static void ccw_machine_4_1_instance_options(MachineState *machine)
836 {
837     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_1 };
838     ccw_machine_4_2_instance_options(machine);
839     s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
840 }
841 
842 static void ccw_machine_4_1_class_options(MachineClass *mc)
843 {
844     ccw_machine_4_2_class_options(mc);
845     compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
846 }
847 DEFINE_CCW_MACHINE(4_1, "4.1", false);
848 
849 static void ccw_machine_4_0_instance_options(MachineState *machine)
850 {
851     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_0 };
852     ccw_machine_4_1_instance_options(machine);
853     s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
854 }
855 
856 static void ccw_machine_4_0_class_options(MachineClass *mc)
857 {
858     ccw_machine_4_1_class_options(mc);
859     compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
860 }
861 DEFINE_CCW_MACHINE(4_0, "4.0", false);
862 
863 static void ccw_machine_3_1_instance_options(MachineState *machine)
864 {
865     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V3_1 };
866     ccw_machine_4_0_instance_options(machine);
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);
869     s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
870 }
871 
872 static void ccw_machine_3_1_class_options(MachineClass *mc)
873 {
874     ccw_machine_4_0_class_options(mc);
875     compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
876 }
877 DEFINE_CCW_MACHINE(3_1, "3.1", false);
878 
879 static void ccw_machine_3_0_instance_options(MachineState *machine)
880 {
881     ccw_machine_3_1_instance_options(machine);
882 }
883 
884 static void ccw_machine_3_0_class_options(MachineClass *mc)
885 {
886     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
887 
888     s390mc->hpage_1m_allowed = false;
889     ccw_machine_3_1_class_options(mc);
890     compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
891 }
892 DEFINE_CCW_MACHINE(3_0, "3.0", false);
893 
894 static void ccw_machine_2_12_instance_options(MachineState *machine)
895 {
896     ccw_machine_3_0_instance_options(machine);
897     s390_cpudef_featoff_greater(11, 1, S390_FEAT_PPA15);
898     s390_cpudef_featoff_greater(11, 1, S390_FEAT_BPB);
899 }
900 
901 static void ccw_machine_2_12_class_options(MachineClass *mc)
902 {
903     ccw_machine_3_0_class_options(mc);
904     compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
905 }
906 DEFINE_CCW_MACHINE(2_12, "2.12", false);
907 
908 static void ccw_machine_2_11_instance_options(MachineState *machine)
909 {
910     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V2_11 };
911     ccw_machine_2_12_instance_options(machine);
912 
913     /* before 2.12 we emulated the very first z900 */
914     s390_set_qemu_cpu_model(0x2064, 7, 1, qemu_cpu_feat);
915 }
916 
917 static void ccw_machine_2_11_class_options(MachineClass *mc)
918 {
919     static GlobalProperty compat[] = {
920         { TYPE_SCLP_EVENT_FACILITY, "allow_all_mask_sizes", "off", },
921     };
922 
923     ccw_machine_2_12_class_options(mc);
924     compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
925     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
926 }
927 DEFINE_CCW_MACHINE(2_11, "2.11", false);
928 
929 static void ccw_machine_2_10_instance_options(MachineState *machine)
930 {
931     ccw_machine_2_11_instance_options(machine);
932 }
933 
934 static void ccw_machine_2_10_class_options(MachineClass *mc)
935 {
936     ccw_machine_2_11_class_options(mc);
937     compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
938 }
939 DEFINE_CCW_MACHINE(2_10, "2.10", false);
940 
941 static void ccw_machine_2_9_instance_options(MachineState *machine)
942 {
943     ccw_machine_2_10_instance_options(machine);
944     s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP);
945     s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2);
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);
949 }
950 
951 static void ccw_machine_2_9_class_options(MachineClass *mc)
952 {
953     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
954     static GlobalProperty compat[] = {
955         { TYPE_S390_STATTRIB, "migration-enabled", "off", },
956     };
957 
958     ccw_machine_2_10_class_options(mc);
959     compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
960     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
961     s390mc->css_migration_enabled = false;
962 }
963 DEFINE_CCW_MACHINE(2_9, "2.9", false);
964 
965 static void ccw_machine_2_8_instance_options(MachineState *machine)
966 {
967     ccw_machine_2_9_instance_options(machine);
968 }
969 
970 static void ccw_machine_2_8_class_options(MachineClass *mc)
971 {
972     static GlobalProperty compat[] = {
973         { TYPE_S390_FLIC_COMMON, "adapter_routes_max_batch", "64", },
974     };
975 
976     ccw_machine_2_9_class_options(mc);
977     compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
978     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
979 }
980 DEFINE_CCW_MACHINE(2_8, "2.8", false);
981 
982 static void ccw_machine_2_7_instance_options(MachineState *machine)
983 {
984     ccw_machine_2_8_instance_options(machine);
985 }
986 
987 static void ccw_machine_2_7_class_options(MachineClass *mc)
988 {
989     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
990 
991     s390mc->cpu_model_allowed = false;
992     ccw_machine_2_8_class_options(mc);
993     compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
994 }
995 DEFINE_CCW_MACHINE(2_7, "2.7", false);
996 
997 static void ccw_machine_2_6_instance_options(MachineState *machine)
998 {
999     ccw_machine_2_7_instance_options(machine);
1000 }
1001 
1002 static void ccw_machine_2_6_class_options(MachineClass *mc)
1003 {
1004     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
1005     static GlobalProperty compat[] = {
1006         { TYPE_S390_IPL, "iplbext_migration", "off", },
1007          { TYPE_VIRTUAL_CSS_BRIDGE, "css_dev_path", "off", },
1008     };
1009 
1010     s390mc->ri_allowed = false;
1011     ccw_machine_2_7_class_options(mc);
1012     compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
1013     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1014 }
1015 DEFINE_CCW_MACHINE(2_6, "2.6", false);
1016 
1017 static void ccw_machine_2_5_instance_options(MachineState *machine)
1018 {
1019     ccw_machine_2_6_instance_options(machine);
1020 }
1021 
1022 static void ccw_machine_2_5_class_options(MachineClass *mc)
1023 {
1024     ccw_machine_2_6_class_options(mc);
1025     compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len);
1026 }
1027 DEFINE_CCW_MACHINE(2_5, "2.5", false);
1028 
1029 static void ccw_machine_2_4_instance_options(MachineState *machine)
1030 {
1031     ccw_machine_2_5_instance_options(machine);
1032 }
1033 
1034 static void ccw_machine_2_4_class_options(MachineClass *mc)
1035 {
1036     static GlobalProperty compat[] = {
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", },
1046     };
1047 
1048     ccw_machine_2_5_class_options(mc);
1049     compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len);
1050     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1051 }
1052 DEFINE_CCW_MACHINE(2_4, "2.4", false);
1053 
1054 static void ccw_machine_register_types(void)
1055 {
1056     type_register_static(&ccw_machine_info);
1057 }
1058 
1059 type_init(ccw_machine_register_types)
1060