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