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