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