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