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