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