xref: /openbmc/qemu/hw/s390x/s390-virtio-ccw.c (revision 3c0b42c68f98fb276fa248012642be8cbf2cab70)
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 "system/confidential-guest-support.h"
17 #include "hw/boards.h"
18 #include "hw/s390x/sclp.h"
19 #include "hw/s390x/s390_flic.h"
20 #include "virtio-ccw.h"
21 #include "qemu/config-file.h"
22 #include "qemu/ctype.h"
23 #include "qemu/error-report.h"
24 #include "qemu/option.h"
25 #include "qemu/qemu-print.h"
26 #include "qemu/units.h"
27 #include "hw/s390x/s390-pci-bus.h"
28 #include "system/reset.h"
29 #include "hw/s390x/storage-keys.h"
30 #include "hw/s390x/storage-attributes.h"
31 #include "hw/s390x/event-facility.h"
32 #include "ipl.h"
33 #include "hw/s390x/s390-virtio-ccw.h"
34 #include "hw/s390x/css-bridge.h"
35 #include "hw/s390x/ap-bridge.h"
36 #include "migration/register.h"
37 #include "target/s390x/cpu_models.h"
38 #include "hw/nmi.h"
39 #include "hw/qdev-properties.h"
40 #include "hw/s390x/tod.h"
41 #include "system/system.h"
42 #include "system/cpus.h"
43 #include "system/hostmem.h"
44 #include "target/s390x/kvm/pv.h"
45 #include "migration/blocker.h"
46 #include "qapi/visitor.h"
47 #include "hw/s390x/cpu-topology.h"
48 #include "kvm/kvm_s390x.h"
49 #include "hw/virtio/virtio-md-pci.h"
50 #include "hw/s390x/virtio-ccw-md.h"
51 #include "system/replay.h"
52 #include CONFIG_DEVICES
53 
54 static Error *pv_mig_blocker;
55 
56 static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
57                               Error **errp)
58 {
59     S390CPU *cpu = S390_CPU(object_new(typename));
60     S390CPU *ret = NULL;
61 
62     if (!object_property_set_int(OBJECT(cpu), "core-id", core_id, errp)) {
63         goto out;
64     }
65     if (!qdev_realize(DEVICE(cpu), NULL, errp)) {
66         goto out;
67     }
68     ret = cpu;
69 
70 out:
71     object_unref(OBJECT(cpu));
72     return ret;
73 }
74 
75 static void s390_init_cpus(MachineState *machine)
76 {
77     MachineClass *mc = MACHINE_GET_CLASS(machine);
78     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
79     int i;
80 
81     if (machine->smp.threads > s390mc->max_threads) {
82         error_report("S390 does not support more than %d threads.",
83                      s390mc->max_threads);
84         exit(1);
85     }
86 
87     /* initialize possible_cpus */
88     mc->possible_cpu_arch_ids(machine);
89 
90     for (i = 0; i < machine->smp.cpus; i++) {
91         s390x_new_cpu(machine->cpu_type, i, &error_fatal);
92     }
93 }
94 
95 static const char *const reset_dev_types[] = {
96     TYPE_VIRTUAL_CSS_BRIDGE,
97     "s390-sclp-event-facility",
98     "s390-flic",
99     "diag288",
100     TYPE_S390_PCI_HOST_BRIDGE,
101     TYPE_AP_BRIDGE,
102 };
103 
104 static void subsystem_reset(void)
105 {
106     DeviceState *dev;
107     int i;
108 
109     /*
110      * ISM firmware is sensitive to unexpected changes to the IOMMU, which can
111      * occur during reset of the vfio-pci device (unmap of entire aperture).
112      * Ensure any passthrough ISM devices are reset now, while CPUs are paused
113      * but before vfio-pci cleanup occurs.
114      */
115     s390_pci_ism_reset();
116 
117     for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
118         dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
119         if (dev) {
120             device_cold_reset(dev);
121         }
122     }
123     if (s390_has_topology()) {
124         s390_topology_reset();
125     }
126 }
127 
128 static void s390_set_memory_limit(S390CcwMachineState *s390ms,
129                                   uint64_t new_limit)
130 {
131     uint64_t hw_limit = 0;
132     int ret = 0;
133 
134     assert(!s390ms->memory_limit && new_limit);
135     if (kvm_enabled()) {
136         ret = kvm_s390_set_mem_limit(new_limit, &hw_limit);
137     }
138     if (ret == -E2BIG) {
139         error_report("host supports a maximum of %" PRIu64 " GB",
140                      hw_limit / GiB);
141         exit(EXIT_FAILURE);
142     } else if (ret) {
143         error_report("setting the guest size failed");
144         exit(EXIT_FAILURE);
145     }
146     s390ms->memory_limit = new_limit;
147 }
148 
149 static void s390_set_max_pagesize(S390CcwMachineState *s390ms,
150                                   uint64_t pagesize)
151 {
152     assert(!s390ms->max_pagesize && pagesize);
153     if (kvm_enabled()) {
154         kvm_s390_set_max_pagesize(pagesize, &error_fatal);
155     }
156     s390ms->max_pagesize = pagesize;
157 }
158 
159 static void s390_memory_init(MachineState *machine)
160 {
161     S390CcwMachineState *s390ms = S390_CCW_MACHINE(machine);
162     MemoryRegion *sysmem = get_system_memory();
163     MemoryRegion *ram = machine->ram;
164     uint64_t ram_size = memory_region_size(ram);
165     uint64_t devmem_base, devmem_size;
166 
167     if (!QEMU_IS_ALIGNED(ram_size, 1 * MiB)) {
168         /*
169          * SCLP cannot possibly expose smaller granularity right now and KVM
170          * cannot handle smaller granularity. As we don't support NUMA, the
171          * region size directly corresponds to machine->ram_size, and the region
172          * is a single RAM memory region.
173          */
174         error_report("ram size must be multiples of 1 MiB");
175         exit(EXIT_FAILURE);
176     }
177 
178     devmem_size = 0;
179     devmem_base = ram_size;
180 #ifdef CONFIG_MEM_DEVICE
181     if (machine->ram_size < machine->maxram_size) {
182 
183         /*
184          * Make sure memory devices have a sane default alignment, even
185          * when weird initial memory sizes are specified.
186          */
187         devmem_base = QEMU_ALIGN_UP(devmem_base, 1 * GiB);
188         devmem_size = machine->maxram_size - machine->ram_size;
189     }
190 #endif
191     s390_set_memory_limit(s390ms, devmem_base + devmem_size);
192 
193     /* Map the initial memory. Must happen after setting the memory limit. */
194     memory_region_add_subregion(sysmem, 0, ram);
195 
196     /* Initialize address space for memory devices. */
197 #ifdef CONFIG_MEM_DEVICE
198     if (devmem_size) {
199         machine_memory_devices_init(machine, devmem_base, devmem_size);
200     }
201 #endif /* CONFIG_MEM_DEVICE */
202 
203     /*
204      * Configure the maximum page size. As no memory devices were created
205      * yet, this is the page size of initial memory only.
206      */
207     s390_set_max_pagesize(s390ms, qemu_maxrampagesize());
208     /* Initialize storage key device */
209     s390_skeys_init();
210     /* Initialize storage attributes device */
211     s390_stattrib_init();
212 }
213 
214 static void s390_init_ipl_dev(const char *kernel_filename,
215                               const char *kernel_cmdline,
216                               const char *initrd_filename, const char *firmware,
217                               bool enforce_bios)
218 {
219     Object *new = object_new(TYPE_S390_IPL);
220     DeviceState *dev = DEVICE(new);
221 
222     if (kernel_filename) {
223         qdev_prop_set_string(dev, "kernel", kernel_filename);
224     }
225     if (initrd_filename) {
226         qdev_prop_set_string(dev, "initrd", initrd_filename);
227     }
228     qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
229     qdev_prop_set_string(dev, "firmware", firmware);
230     qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
231     object_property_add_child(qdev_get_machine(), TYPE_S390_IPL,
232                               new);
233     object_unref(new);
234     qdev_realize(dev, NULL, &error_fatal);
235 }
236 
237 static void s390_create_virtio_net(BusState *bus, const char *name)
238 {
239     DeviceState *dev;
240     int cnt = 0;
241 
242     while ((dev = qemu_create_nic_device(name, true, "virtio"))) {
243         g_autofree char *childname = g_strdup_printf("%s[%d]", name, cnt++);
244         object_property_add_child(OBJECT(bus), childname, OBJECT(dev));
245         qdev_realize_and_unref(dev, bus, &error_fatal);
246     }
247 }
248 
249 static void s390_create_sclpconsole(SCLPDevice *sclp,
250                                     const char *type, Chardev *chardev)
251 {
252     SCLPEventFacility *ef = sclp->event_facility;
253     BusState *ev_fac_bus = sclp_get_event_facility_bus(ef);
254     DeviceState *dev;
255 
256     dev = qdev_new(type);
257     object_property_add_child(OBJECT(ef), type, OBJECT(dev));
258     qdev_prop_set_chr(dev, "chardev", chardev);
259     qdev_realize_and_unref(dev, ev_fac_bus, &error_fatal);
260 }
261 
262 static void s390_create_sclpcpi(SCLPDevice *sclp)
263 {
264     SCLPEventFacility *ef = sclp->event_facility;
265     BusState *ev_fac_bus = sclp_get_event_facility_bus(ef);
266     DeviceState *dev;
267 
268     dev = qdev_new(TYPE_SCLP_EVENT_CPI);
269     object_property_add_child(OBJECT(ef), "sclpcpi", OBJECT(dev));
270     qdev_realize_and_unref(dev, ev_fac_bus, &error_fatal);
271 }
272 
273 static void ccw_init(MachineState *machine)
274 {
275     MachineClass *mc = MACHINE_GET_CLASS(machine);
276     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
277     S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
278     int ret;
279     VirtualCssBus *css_bus;
280     DeviceState *dev;
281 
282     ms->sclp = SCLP(object_new(TYPE_SCLP));
283     object_property_add_child(OBJECT(machine), TYPE_SCLP, OBJECT(ms->sclp));
284     qdev_realize_and_unref(DEVICE(ms->sclp), NULL, &error_fatal);
285 
286     /* init memory + setup max page size. Required for the CPU model */
287     s390_memory_init(machine);
288 
289     /* init CPUs (incl. CPU model) early so s390_has_feature() works */
290     s390_init_cpus(machine);
291 
292     /* Need CPU model to be determined before we can set up PV */
293     if (machine->cgs) {
294         confidential_guest_kvm_init(machine->cgs, &error_fatal);
295     }
296 
297     s390_flic_init();
298 
299     /* init the SIGP facility */
300     s390_init_sigp();
301 
302     /* create AP bridge and bus(es) */
303     s390_init_ap();
304 
305     /* get a BUS */
306     css_bus = virtual_css_bus_init();
307     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
308                       machine->initrd_filename,
309                       machine->firmware ?: "s390-ccw.img",
310                       true);
311 
312     dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE);
313     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
314                               OBJECT(dev));
315     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
316 
317     s390_enable_css_support(s390_cpu_addr2state(0));
318 
319     ret = css_create_css_image(VIRTUAL_CSSID, true);
320     assert(ret == 0);
321 
322     css_register_vmstate();
323 
324     /* Create VirtIO network adapters */
325     s390_create_virtio_net(BUS(css_bus), mc->default_nic);
326 
327     /* init consoles */
328     if (serial_hd(0)) {
329         s390_create_sclpconsole(ms->sclp, "sclpconsole", serial_hd(0));
330     }
331     if (serial_hd(1)) {
332         s390_create_sclpconsole(ms->sclp, "sclplmconsole", serial_hd(1));
333     }
334 
335     /* init the TOD clock */
336     s390_init_tod();
337 
338     /* init SCLP event Control-Program Identification */
339     if (s390mc->use_cpi) {
340         s390_create_sclpcpi(ms->sclp);
341     }
342 
343 }
344 
345 static void s390_cpu_plug(HotplugHandler *hotplug_dev,
346                         DeviceState *dev, Error **errp)
347 {
348     ERRP_GUARD();
349     MachineState *ms = MACHINE(hotplug_dev);
350     S390CPU *cpu = S390_CPU(dev);
351 
352     g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu);
353     ms->possible_cpus->cpus[cpu->env.core_id].cpu = CPU(dev);
354 
355     if (s390_has_topology()) {
356         s390_topology_setup_cpu(ms, cpu, errp);
357         if (*errp) {
358             return;
359         }
360     }
361 
362     if (dev->hotplugged) {
363         raise_irq_cpu_hotplug();
364     }
365 }
366 
367 static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg)
368 {
369     S390CPU *cpu = S390_CPU(cs);
370 
371     s390_ipl_prepare_cpu(cpu);
372     s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
373 }
374 
375 static void s390_machine_unprotect(S390CcwMachineState *ms)
376 {
377     if (!s390_pv_vm_try_disable_async(ms)) {
378         s390_pv_vm_disable();
379     }
380     ms->pv = false;
381     migrate_del_blocker(&pv_mig_blocker);
382     ram_block_discard_disable(false);
383 }
384 
385 static int s390_machine_protect(S390CcwMachineState *ms,
386                                 struct S390PVResponse *pv_resp)
387 {
388     Error *local_err = NULL;
389     int rc;
390 
391    /*
392     * Discarding of memory in RAM blocks does not work as expected with
393     * protected VMs. Sharing and unsharing pages would be required. Disable
394     * it for now, until until we have a solution to make at least Linux
395     * guests either support it (e.g., virtio-balloon) or fail gracefully.
396     */
397     rc = ram_block_discard_disable(true);
398     if (rc) {
399         error_report("protected VMs: cannot disable RAM discard");
400         return rc;
401     }
402 
403     error_setg(&pv_mig_blocker,
404                "protected VMs are currently not migratable.");
405     rc = migrate_add_blocker(&pv_mig_blocker, &local_err);
406     if (rc) {
407         ram_block_discard_disable(false);
408         error_report_err(local_err);
409         return rc;
410     }
411 
412     /* Create SE VM */
413     rc = s390_pv_vm_enable();
414     if (rc) {
415         ram_block_discard_disable(false);
416         migrate_del_blocker(&pv_mig_blocker);
417         return rc;
418     }
419 
420     ms->pv = true;
421 
422     /* Will return 0 if API is not available since it's not vital */
423     rc = s390_pv_query_info();
424     if (rc) {
425         goto out_err;
426     }
427 
428     /* Set SE header and unpack */
429     rc = s390_ipl_prepare_pv_header(pv_resp, &local_err);
430     if (rc) {
431         goto out_err;
432     }
433 
434     /* Decrypt image */
435     rc = s390_ipl_pv_unpack(pv_resp);
436     if (rc) {
437         goto out_err;
438     }
439 
440     /* Verify integrity */
441     rc = s390_pv_verify(pv_resp);
442     if (rc) {
443         goto out_err;
444     }
445     return rc;
446 
447 out_err:
448     if (local_err) {
449         error_report_err(local_err);
450     }
451     s390_machine_unprotect(ms);
452     return rc;
453 }
454 
455 static void s390_pv_prepare_reset(S390CcwMachineState *ms)
456 {
457     CPUState *cs;
458 
459     if (!s390_is_pv()) {
460         return;
461     }
462     /* Unsharing requires all cpus to be stopped */
463     CPU_FOREACH(cs) {
464         s390_cpu_set_state(S390_CPU_STATE_STOPPED, S390_CPU(cs));
465     }
466     s390_pv_unshare();
467     s390_pv_prep_reset();
468 }
469 
470 static void s390_machine_reset(MachineState *machine, ResetType type)
471 {
472     S390CcwMachineState *ms = S390_CCW_MACHINE(machine);
473     struct S390PVResponse pv_resp;
474     enum s390_reset reset_type;
475     CPUState *cs, *t;
476     S390CPU *cpu;
477 
478     /*
479      * Temporarily drop the record/replay mutex to let rr_cpu_thread_fn()
480      * process the run_on_cpu() requests below. This is safe, because at this
481      * point one of the following is true:
482      * - All CPU threads are not running, either because the machine is being
483      *   initialized, or because the guest requested a reset using diag 308.
484      *   There is no risk to desync the record/replay state.
485      * - A snapshot is about to be loaded. The record/replay state consistency
486      *   is not important.
487      */
488     replay_mutex_unlock();
489 
490     /* get the reset parameters, reset them once done */
491     s390_ipl_get_reset_request(&cs, &reset_type);
492 
493     /* all CPUs are paused and synchronized at this point */
494     s390_cmma_reset();
495 
496     cpu = S390_CPU(cs);
497 
498     switch (reset_type) {
499     case S390_RESET_EXTERNAL:
500     case S390_RESET_REIPL:
501         /*
502          * Reset the subsystem which includes a AP reset. If a PV
503          * guest had APQNs attached the AP reset is a prerequisite to
504          * unprotecting since the UV checks if all APQNs are reset.
505          */
506         subsystem_reset();
507         if (s390_is_pv()) {
508             s390_machine_unprotect(ms);
509         }
510 
511         /*
512          * Device reset includes CPU clear resets so this has to be
513          * done AFTER the unprotect call above.
514          */
515         qemu_devices_reset(type);
516         s390_crypto_reset();
517 
518         /* configure and start the ipl CPU only */
519         run_on_cpu(cs, s390_do_cpu_ipl, RUN_ON_CPU_NULL);
520         break;
521     case S390_RESET_MODIFIED_CLEAR:
522         /*
523          * Subsystem reset needs to be done before we unshare memory
524          * and lose access to VIRTIO structures in guest memory.
525          */
526         subsystem_reset();
527         s390_crypto_reset();
528         s390_pv_prepare_reset(ms);
529         CPU_FOREACH(t) {
530             run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
531         }
532         run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
533         break;
534     case S390_RESET_LOAD_NORMAL:
535         /*
536          * Subsystem reset needs to be done before we unshare memory
537          * and lose access to VIRTIO structures in guest memory.
538          */
539         subsystem_reset();
540         s390_pv_prepare_reset(ms);
541         CPU_FOREACH(t) {
542             if (t == cs) {
543                 continue;
544             }
545             run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL);
546         }
547         run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL);
548         run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
549         break;
550     case S390_RESET_PV: /* Subcode 10 */
551         subsystem_reset();
552         s390_crypto_reset();
553 
554         CPU_FOREACH(t) {
555             if (t == cs) {
556                 continue;
557             }
558             run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
559         }
560         run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL);
561 
562         if (s390_machine_protect(ms, &pv_resp)) {
563             s390_pv_inject_reset_error(cs, pv_resp);
564             /*
565              * Continue after the diag308 so the guest knows something
566              * went wrong.
567              */
568             s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
569             goto out_lock;
570         }
571 
572         run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL);
573         break;
574     default:
575         g_assert_not_reached();
576     }
577 
578     CPU_FOREACH(t) {
579         run_on_cpu(t, s390_do_cpu_set_diag318, RUN_ON_CPU_HOST_ULONG(0));
580     }
581     s390_ipl_clear_reset_request();
582 
583 out_lock:
584     /*
585      * Re-take the record/replay mutex, temporarily dropping the BQL in order
586      * to satisfy the ordering requirements.
587      */
588     bql_unlock();
589     replay_mutex_lock();
590     bql_lock();
591 }
592 
593 static void s390_machine_device_pre_plug(HotplugHandler *hotplug_dev,
594                                          DeviceState *dev, Error **errp)
595 {
596     if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) {
597         virtio_ccw_md_pre_plug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp);
598     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
599         virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
600     }
601 }
602 
603 static void s390_machine_device_plug(HotplugHandler *hotplug_dev,
604                                      DeviceState *dev, Error **errp)
605 {
606     S390CcwMachineState *s390ms = S390_CCW_MACHINE(hotplug_dev);
607 
608     if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
609         s390_cpu_plug(hotplug_dev, dev, errp);
610     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW) ||
611                object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
612         /*
613          * At this point, the device is realized and set all memdevs mapped, so
614          * qemu_maxrampagesize() will pick up the page sizes of these memdevs
615          * as well. Before we plug the device and expose any RAM memory regions
616          * to the system, make sure we don't exceed the previously set max page
617          * size. While only relevant for KVM, there is not really any use case
618          * for this with TCG, so we'll unconditionally reject it.
619          */
620         if (qemu_maxrampagesize() != s390ms->max_pagesize) {
621             error_setg(errp, "Memory device uses a bigger page size than"
622                        " initial memory");
623             return;
624         }
625         if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) {
626             virtio_ccw_md_plug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp);
627         } else {
628             virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
629         }
630     }
631 }
632 
633 static void s390_machine_device_unplug_request(HotplugHandler *hotplug_dev,
634                                                DeviceState *dev, Error **errp)
635 {
636     if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
637         error_setg(errp, "CPU hot unplug not supported on this machine");
638     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) {
639         virtio_ccw_md_unplug_request(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev),
640                                      errp);
641     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
642         virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev),
643                                      errp);
644     }
645 }
646 
647 static void s390_machine_device_unplug(HotplugHandler *hotplug_dev,
648                                        DeviceState *dev, Error **errp)
649 {
650     if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) {
651         virtio_ccw_md_unplug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp);
652     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
653         virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
654     }
655  }
656 
657 static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms,
658                                                      unsigned cpu_index)
659 {
660     MachineClass *mc = MACHINE_GET_CLASS(ms);
661     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
662 
663     assert(cpu_index < possible_cpus->len);
664     return possible_cpus->cpus[cpu_index].props;
665 }
666 
667 static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms)
668 {
669     int i;
670     unsigned int max_cpus = ms->smp.max_cpus;
671 
672     if (ms->possible_cpus) {
673         g_assert(ms->possible_cpus && ms->possible_cpus->len == max_cpus);
674         return ms->possible_cpus;
675     }
676 
677     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
678                                   sizeof(CPUArchId) * max_cpus);
679     ms->possible_cpus->len = max_cpus;
680     for (i = 0; i < ms->possible_cpus->len; i++) {
681         CpuInstanceProperties *props = &ms->possible_cpus->cpus[i].props;
682 
683         ms->possible_cpus->cpus[i].type = ms->cpu_type;
684         ms->possible_cpus->cpus[i].vcpus_count = 1;
685         ms->possible_cpus->cpus[i].arch_id = i;
686 
687         props->has_core_id = true;
688         props->core_id = i;
689         props->has_socket_id = true;
690         props->socket_id = s390_std_socket(i, &ms->smp);
691         props->has_book_id = true;
692         props->book_id = s390_std_book(i, &ms->smp);
693         props->has_drawer_id = true;
694         props->drawer_id = s390_std_drawer(i, &ms->smp);
695     }
696 
697     return ms->possible_cpus;
698 }
699 
700 static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
701                                                 DeviceState *dev)
702 {
703     if (object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
704         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW) ||
705         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
706         return HOTPLUG_HANDLER(machine);
707     }
708     return NULL;
709 }
710 
711 static void s390_nmi(NMIState *n, int cpu_index, Error **errp)
712 {
713     CPUState *cs = qemu_get_cpu(cpu_index);
714 
715     s390_cpu_restart(S390_CPU(cs));
716 }
717 
718 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
719 {
720     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
721 
722     return ms->aes_key_wrap;
723 }
724 
725 static inline void machine_set_aes_key_wrap(Object *obj, bool value,
726                                             Error **errp)
727 {
728     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
729 
730     ms->aes_key_wrap = value;
731 }
732 
733 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
734 {
735     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
736 
737     return ms->dea_key_wrap;
738 }
739 
740 static inline void machine_set_dea_key_wrap(Object *obj, bool value,
741                                             Error **errp)
742 {
743     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
744 
745     ms->dea_key_wrap = value;
746 }
747 
748 static void machine_get_loadparm(Object *obj, Visitor *v,
749                                  const char *name, void *opaque,
750                                  Error **errp)
751 {
752     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
753     char *str = g_strndup((char *) ms->loadparm, sizeof(ms->loadparm));
754 
755     visit_type_str(v, name, &str, errp);
756     g_free(str);
757 }
758 
759 static void machine_set_loadparm(Object *obj, Visitor *v,
760                                  const char *name, void *opaque,
761                                  Error **errp)
762 {
763     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
764     char *val;
765 
766     if (!visit_type_str(v, name, &val, errp)) {
767         return;
768     }
769 
770     s390_ipl_fmt_loadparm(ms->loadparm, val, errp);
771     g_free(val);
772 }
773 
774 static void ccw_machine_class_init(ObjectClass *oc, const void *data)
775 {
776     MachineClass *mc = MACHINE_CLASS(oc);
777     NMIClass *nc = NMI_CLASS(oc);
778     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
779     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
780     DumpSKeysInterface *dsi = DUMP_SKEYS_INTERFACE_CLASS(oc);
781 
782     s390mc->max_threads = 1;
783     s390mc->use_cpi = true;
784     mc->reset = s390_machine_reset;
785     mc->block_default_type = IF_VIRTIO;
786     mc->no_cdrom = 1;
787     mc->no_floppy = 1;
788     mc->no_parallel = 1;
789     mc->max_cpus = S390_MAX_CPUS;
790     mc->has_hotpluggable_cpus = true;
791     mc->smp_props.books_supported = true;
792     mc->smp_props.drawers_supported = true;
793     assert(!mc->get_hotplug_handler);
794     mc->get_hotplug_handler = s390_get_hotplug_handler;
795     mc->cpu_index_to_instance_props = s390_cpu_index_to_props;
796     mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids;
797     /* it is overridden with 'host' cpu *in kvm_arch_init* */
798     mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu");
799     hc->pre_plug = s390_machine_device_pre_plug;
800     hc->plug = s390_machine_device_plug;
801     hc->unplug_request = s390_machine_device_unplug_request;
802     hc->unplug = s390_machine_device_unplug;
803     nc->nmi_monitor_handler = s390_nmi;
804     mc->default_ram_id = "s390.ram";
805     mc->default_nic = "virtio-net-ccw";
806     dsi->qmp_dump_skeys = s390_qmp_dump_skeys;
807 
808     object_class_property_add_bool(oc, "aes-key-wrap",
809                                    machine_get_aes_key_wrap,
810                                    machine_set_aes_key_wrap);
811     object_class_property_set_description(oc, "aes-key-wrap",
812             "enable/disable AES key wrapping using the CPACF wrapping key");
813 
814     object_class_property_add_bool(oc, "dea-key-wrap",
815                                    machine_get_dea_key_wrap,
816                                    machine_set_dea_key_wrap);
817     object_class_property_set_description(oc, "dea-key-wrap",
818             "enable/disable DEA key wrapping using the CPACF wrapping key");
819 
820     object_class_property_add(oc, "loadparm", "loadparm",
821                               machine_get_loadparm, machine_set_loadparm,
822                               NULL, NULL);
823     object_class_property_set_description(oc, "loadparm",
824             "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted"
825             " to upper case) to pass to machine loader, boot manager,"
826             " and guest kernel");
827 }
828 
829 static inline void s390_machine_initfn(Object *obj)
830 {
831     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
832 
833     ms->aes_key_wrap = true;
834     ms->dea_key_wrap = true;
835 }
836 
837 static const TypeInfo ccw_machine_info = {
838     .name          = TYPE_S390_CCW_MACHINE,
839     .parent        = TYPE_MACHINE,
840     .abstract      = true,
841     .instance_size = sizeof(S390CcwMachineState),
842     .instance_init = s390_machine_initfn,
843     .class_size = sizeof(S390CcwMachineClass),
844     .class_init    = ccw_machine_class_init,
845     .interfaces = (const InterfaceInfo[]) {
846         { TYPE_NMI },
847         { TYPE_HOTPLUG_HANDLER},
848         { TYPE_DUMP_SKEYS_INTERFACE},
849         { }
850     },
851 };
852 
853 #define DEFINE_CCW_MACHINE_IMPL(latest, ...)                                  \
854     static void MACHINE_VER_SYM(mach_init, ccw, __VA_ARGS__)(MachineState *mach) \
855     {                                                                         \
856         MACHINE_VER_SYM(instance_options, ccw, __VA_ARGS__)(mach);            \
857         ccw_init(mach);                                                       \
858     }                                                                         \
859     static void MACHINE_VER_SYM(class_init, ccw, __VA_ARGS__)(                \
860         ObjectClass *oc,                                                      \
861         const void *data)                                                     \
862     {                                                                         \
863         MachineClass *mc = MACHINE_CLASS(oc);                                 \
864         MACHINE_VER_SYM(class_options, ccw, __VA_ARGS__)(mc);                 \
865         mc->desc = "Virtual s390x machine (version " MACHINE_VER_STR(__VA_ARGS__) ")"; \
866         mc->init = MACHINE_VER_SYM(mach_init, ccw, __VA_ARGS__);              \
867         MACHINE_VER_DEPRECATION(__VA_ARGS__);                                 \
868         if (latest) {                                                         \
869             mc->alias = "s390-ccw-virtio";                                    \
870             mc->is_default = true;                                            \
871         }                                                                     \
872     }                                                                         \
873     static const TypeInfo MACHINE_VER_SYM(info, ccw, __VA_ARGS__) =           \
874     {                                                                         \
875         .name = MACHINE_VER_TYPE_NAME("s390-ccw-virtio", __VA_ARGS__),        \
876         .parent = TYPE_S390_CCW_MACHINE,                                      \
877         .class_init = MACHINE_VER_SYM(class_init, ccw, __VA_ARGS__),          \
878     };                                                                        \
879     static void MACHINE_VER_SYM(register, ccw, __VA_ARGS__)(void)             \
880     {                                                                         \
881         MACHINE_VER_DELETION(__VA_ARGS__);                                    \
882         type_register_static(&MACHINE_VER_SYM(info, ccw, __VA_ARGS__));       \
883     }                                                                         \
884     type_init(MACHINE_VER_SYM(register, ccw, __VA_ARGS__))
885 
886 #define DEFINE_CCW_MACHINE_AS_LATEST(major, minor) \
887     DEFINE_CCW_MACHINE_IMPL(true, major, minor)
888 
889 #define DEFINE_CCW_MACHINE(major, minor) \
890     DEFINE_CCW_MACHINE_IMPL(false, major, minor)
891 
892 
893 static void ccw_machine_10_2_instance_options(MachineState *machine)
894 {
895 }
896 
897 static void ccw_machine_10_2_class_options(MachineClass *mc)
898 {
899 }
900 DEFINE_CCW_MACHINE_AS_LATEST(10, 2);
901 
902 static void ccw_machine_10_1_instance_options(MachineState *machine)
903 {
904     ccw_machine_10_2_instance_options(machine);
905 }
906 
907 static void ccw_machine_10_1_class_options(MachineClass *mc)
908 {
909     ccw_machine_10_2_class_options(mc);
910     compat_props_add(mc->compat_props, hw_compat_10_1, hw_compat_10_1_len);
911 }
912 DEFINE_CCW_MACHINE(10, 1);
913 
914 static void ccw_machine_10_0_instance_options(MachineState *machine)
915 {
916     ccw_machine_10_1_instance_options(machine);
917 }
918 
919 static void ccw_machine_10_0_class_options(MachineClass *mc)
920 {
921     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
922     s390mc->use_cpi = false;
923 
924     ccw_machine_10_1_class_options(mc);
925     compat_props_add(mc->compat_props, hw_compat_10_0, hw_compat_10_0_len);
926 }
927 DEFINE_CCW_MACHINE(10, 0);
928 
929 static void ccw_machine_9_2_instance_options(MachineState *machine)
930 {
931     ccw_machine_10_0_instance_options(machine);
932 }
933 
934 static void ccw_machine_9_2_class_options(MachineClass *mc)
935 {
936     static GlobalProperty compat[] = {
937         { TYPE_S390_PCI_DEVICE, "relaxed-translation", "off", },
938     };
939 
940     ccw_machine_10_0_class_options(mc);
941     compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len);
942     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
943 }
944 DEFINE_CCW_MACHINE(9, 2);
945 
946 static void ccw_machine_9_1_instance_options(MachineState *machine)
947 {
948     ccw_machine_9_2_instance_options(machine);
949 }
950 
951 static void ccw_machine_9_1_class_options(MachineClass *mc)
952 {
953     ccw_machine_9_2_class_options(mc);
954     compat_props_add(mc->compat_props, hw_compat_9_1, hw_compat_9_1_len);
955 }
956 DEFINE_CCW_MACHINE(9, 1);
957 
958 static void ccw_machine_9_0_instance_options(MachineState *machine)
959 {
960     ccw_machine_9_1_instance_options(machine);
961 }
962 
963 static void ccw_machine_9_0_class_options(MachineClass *mc)
964 {
965     static GlobalProperty compat[] = {
966         { TYPE_QEMU_S390_FLIC, "migrate-all-state", "off", },
967     };
968 
969     ccw_machine_9_1_class_options(mc);
970     compat_props_add(mc->compat_props, hw_compat_9_0, hw_compat_9_0_len);
971     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
972 }
973 DEFINE_CCW_MACHINE(9, 0);
974 
975 static void ccw_machine_8_2_instance_options(MachineState *machine)
976 {
977     ccw_machine_9_0_instance_options(machine);
978 }
979 
980 static void ccw_machine_8_2_class_options(MachineClass *mc)
981 {
982     ccw_machine_9_0_class_options(mc);
983     compat_props_add(mc->compat_props, hw_compat_8_2, hw_compat_8_2_len);
984 }
985 DEFINE_CCW_MACHINE(8, 2);
986 
987 static void ccw_machine_8_1_instance_options(MachineState *machine)
988 {
989     ccw_machine_8_2_instance_options(machine);
990 }
991 
992 static void ccw_machine_8_1_class_options(MachineClass *mc)
993 {
994     ccw_machine_8_2_class_options(mc);
995     compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len);
996     mc->smp_props.drawers_supported = false;
997     mc->smp_props.books_supported = false;
998 }
999 DEFINE_CCW_MACHINE(8, 1);
1000 
1001 static void ccw_machine_8_0_instance_options(MachineState *machine)
1002 {
1003     ccw_machine_8_1_instance_options(machine);
1004 }
1005 
1006 static void ccw_machine_8_0_class_options(MachineClass *mc)
1007 {
1008     ccw_machine_8_1_class_options(mc);
1009     compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len);
1010 }
1011 DEFINE_CCW_MACHINE(8, 0);
1012 
1013 static void ccw_machine_7_2_instance_options(MachineState *machine)
1014 {
1015     ccw_machine_8_0_instance_options(machine);
1016 }
1017 
1018 static void ccw_machine_7_2_class_options(MachineClass *mc)
1019 {
1020     ccw_machine_8_0_class_options(mc);
1021     compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len);
1022 }
1023 DEFINE_CCW_MACHINE(7, 2);
1024 
1025 static void ccw_machine_7_1_instance_options(MachineState *machine)
1026 {
1027     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_1 };
1028 
1029     ccw_machine_7_2_instance_options(machine);
1030     s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAIE);
1031     s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat);
1032 }
1033 
1034 static void ccw_machine_7_1_class_options(MachineClass *mc)
1035 {
1036     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1037     static GlobalProperty compat[] = {
1038         { TYPE_S390_PCI_DEVICE, "interpret", "off", },
1039         { TYPE_S390_PCI_DEVICE, "forwarding-assist", "off", },
1040     };
1041 
1042     ccw_machine_7_2_class_options(mc);
1043     compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len);
1044     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1045     s390mc->max_threads = S390_MAX_CPUS;
1046 }
1047 DEFINE_CCW_MACHINE(7, 1);
1048 
1049 static void ccw_machine_7_0_instance_options(MachineState *machine)
1050 {
1051     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_0 };
1052 
1053     ccw_machine_7_1_instance_options(machine);
1054     s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat);
1055 }
1056 
1057 static void ccw_machine_7_0_class_options(MachineClass *mc)
1058 {
1059     ccw_machine_7_1_class_options(mc);
1060     compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len);
1061 }
1062 DEFINE_CCW_MACHINE(7, 0);
1063 
1064 static void ccw_machine_6_2_instance_options(MachineState *machine)
1065 {
1066     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_2 };
1067 
1068     ccw_machine_7_0_instance_options(machine);
1069     s390_set_qemu_cpu_model(0x3906, 14, 2, qemu_cpu_feat);
1070 }
1071 
1072 static void ccw_machine_6_2_class_options(MachineClass *mc)
1073 {
1074     ccw_machine_7_0_class_options(mc);
1075     compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len);
1076 }
1077 DEFINE_CCW_MACHINE(6, 2);
1078 
1079 static void ccw_machine_6_1_instance_options(MachineState *machine)
1080 {
1081     ccw_machine_6_2_instance_options(machine);
1082     s390_cpudef_featoff_greater(16, 1, S390_FEAT_NNPA);
1083     s390_cpudef_featoff_greater(16, 1, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2);
1084     s390_cpudef_featoff_greater(16, 1, S390_FEAT_BEAR_ENH);
1085     s390_cpudef_featoff_greater(16, 1, S390_FEAT_RDP);
1086     s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAI);
1087 }
1088 
1089 static void ccw_machine_6_1_class_options(MachineClass *mc)
1090 {
1091     ccw_machine_6_2_class_options(mc);
1092     compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len);
1093     mc->smp_props.prefer_sockets = true;
1094 }
1095 DEFINE_CCW_MACHINE(6, 1);
1096 
1097 static void ccw_machine_6_0_instance_options(MachineState *machine)
1098 {
1099     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_0 };
1100 
1101     ccw_machine_6_1_instance_options(machine);
1102     s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
1103 }
1104 
1105 static void ccw_machine_6_0_class_options(MachineClass *mc)
1106 {
1107     ccw_machine_6_1_class_options(mc);
1108     compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len);
1109 }
1110 DEFINE_CCW_MACHINE(6, 0);
1111 
1112 static void ccw_machine_5_2_instance_options(MachineState *machine)
1113 {
1114     ccw_machine_6_0_instance_options(machine);
1115 }
1116 
1117 static void ccw_machine_5_2_class_options(MachineClass *mc)
1118 {
1119     ccw_machine_6_0_class_options(mc);
1120     compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len);
1121 }
1122 DEFINE_CCW_MACHINE(5, 2);
1123 
1124 static void ccw_machine_5_1_instance_options(MachineState *machine)
1125 {
1126     ccw_machine_5_2_instance_options(machine);
1127 }
1128 
1129 static void ccw_machine_5_1_class_options(MachineClass *mc)
1130 {
1131     ccw_machine_5_2_class_options(mc);
1132     compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len);
1133 }
1134 DEFINE_CCW_MACHINE(5, 1);
1135 
1136 static void ccw_machine_5_0_instance_options(MachineState *machine)
1137 {
1138     ccw_machine_5_1_instance_options(machine);
1139 }
1140 
1141 static void ccw_machine_5_0_class_options(MachineClass *mc)
1142 {
1143     ccw_machine_5_1_class_options(mc);
1144     compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
1145 }
1146 DEFINE_CCW_MACHINE(5, 0);
1147 
1148 static void ccw_machine_register_types(void)
1149 {
1150     type_register_static(&ccw_machine_info);
1151 }
1152 
1153 type_init(ccw_machine_register_types)
1154