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