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