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