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