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