4 * Copyright 2012 IBM Corp.
5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or (at
9 * your option) any later version. See the COPYING file in the top-level
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
17 #include "hw/boards.h"
18 #include "exec/address-spaces.h"
19 #include "hw/s390x/s390-virtio-hcall.h"
20 #include "hw/s390x/sclp.h"
21 #include "hw/s390x/s390_flic.h"
22 #include "hw/s390x/ioinst.h"
23 #include "hw/s390x/css.h"
24 #include "virtio-ccw.h"
25 #include "qemu/config-file.h"
26 #include "s390-pci-bus.h"
27 #include "hw/s390x/storage-keys.h"
28 #include "hw/s390x/storage-attributes.h"
29 #include "hw/compat.h"
31 #include "hw/s390x/s390-virtio-ccw.h"
32 #include "hw/s390x/css-bridge.h"
33 #include "migration/register.h"
34 #include "cpu_models.h"
35 #include "qapi/qmp/qerror.h"
38 static S390CPU
**cpu_states
;
40 S390CPU
*s390_cpu_addr2state(uint16_t cpu_addr
)
42 if (cpu_addr
>= max_cpus
) {
46 /* Fast lookup via CPU ID */
47 return cpu_states
[cpu_addr
];
50 static void s390_init_cpus(MachineState
*machine
)
55 if (machine
->cpu_model
== NULL
) {
56 machine
->cpu_model
= s390_default_cpu_model_name();
59 cpu_states
= g_new0(S390CPU
*, max_cpus
);
61 for (i
= 0; i
< max_cpus
; i
++) {
62 name
= g_strdup_printf("cpu[%i]", i
);
63 object_property_add_link(OBJECT(machine
), name
, TYPE_S390_CPU
,
64 (Object
**) &cpu_states
[i
],
65 object_property_allow_set_link
,
66 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
71 for (i
= 0; i
< smp_cpus
; i
++) {
72 s390x_new_cpu(machine
->cpu_model
, i
, &error_fatal
);
76 static const char *const reset_dev_types
[] = {
77 TYPE_VIRTUAL_CSS_BRIDGE
,
78 "s390-sclp-event-facility",
83 void subsystem_reset(void)
88 for (i
= 0; i
< ARRAY_SIZE(reset_dev_types
); i
++) {
89 dev
= DEVICE(object_resolve_path_type("", reset_dev_types
[i
], NULL
));
96 static int virtio_ccw_hcall_notify(const uint64_t *args
)
98 uint64_t subch_id
= args
[0];
99 uint64_t queue
= args
[1];
101 int cssid
, ssid
, schid
, m
;
103 if (ioinst_disassemble_sch_ident(subch_id
, &m
, &cssid
, &ssid
, &schid
)) {
106 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
107 if (!sch
|| !css_subch_visible(sch
)) {
110 if (queue
>= VIRTIO_QUEUE_MAX
) {
113 virtio_queue_notify(virtio_ccw_get_vdev(sch
), queue
);
118 static int virtio_ccw_hcall_early_printk(const uint64_t *args
)
120 uint64_t mem
= args
[0];
122 if (mem
< ram_size
) {
129 static void virtio_ccw_register_hcalls(void)
131 s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY
,
132 virtio_ccw_hcall_notify
);
133 /* Tolerate early printk. */
134 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY
,
135 virtio_ccw_hcall_early_printk
);
138 static void s390_memory_init(ram_addr_t mem_size
)
140 MemoryRegion
*sysmem
= get_system_memory();
141 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
143 /* allocate RAM for core */
144 memory_region_allocate_system_memory(ram
, NULL
, "s390.ram", mem_size
);
145 memory_region_add_subregion(sysmem
, 0, ram
);
147 /* Initialize storage key device */
149 /* Initialize storage attributes device */
150 s390_stattrib_init();
153 #define S390_TOD_CLOCK_VALUE_MISSING 0x00
154 #define S390_TOD_CLOCK_VALUE_PRESENT 0x01
156 static void gtod_save(QEMUFile
*f
, void *opaque
)
162 r
= s390_get_clock(&tod_high
, &tod_low
);
164 warn_report("Unable to get guest clock for migration: %s",
166 error_printf("Guest clock will not be migrated "
167 "which could cause the guest to hang.");
168 qemu_put_byte(f
, S390_TOD_CLOCK_VALUE_MISSING
);
172 qemu_put_byte(f
, S390_TOD_CLOCK_VALUE_PRESENT
);
173 qemu_put_byte(f
, tod_high
);
174 qemu_put_be64(f
, tod_low
);
177 static int gtod_load(QEMUFile
*f
, void *opaque
, int version_id
)
183 if (qemu_get_byte(f
) == S390_TOD_CLOCK_VALUE_MISSING
) {
184 warn_report("Guest clock was not migrated. This could "
185 "cause the guest to hang.");
189 tod_high
= qemu_get_byte(f
);
190 tod_low
= qemu_get_be64(f
);
192 r
= s390_set_clock(&tod_high
, &tod_low
);
194 warn_report("Unable to set guest clock for migration: %s",
196 error_printf("Guest clock will not be restored "
197 "which could cause the guest to hang.");
203 static SaveVMHandlers savevm_gtod
= {
204 .save_state
= gtod_save
,
205 .load_state
= gtod_load
,
208 static void s390_init_ipl_dev(const char *kernel_filename
,
209 const char *kernel_cmdline
,
210 const char *initrd_filename
, const char *firmware
,
211 const char *netboot_fw
, bool enforce_bios
)
213 Object
*new = object_new(TYPE_S390_IPL
);
214 DeviceState
*dev
= DEVICE(new);
216 if (kernel_filename
) {
217 qdev_prop_set_string(dev
, "kernel", kernel_filename
);
219 if (initrd_filename
) {
220 qdev_prop_set_string(dev
, "initrd", initrd_filename
);
222 qdev_prop_set_string(dev
, "cmdline", kernel_cmdline
);
223 qdev_prop_set_string(dev
, "firmware", firmware
);
224 qdev_prop_set_string(dev
, "netboot_fw", netboot_fw
);
225 qdev_prop_set_bit(dev
, "enforce_bios", enforce_bios
);
226 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL
,
229 qdev_init_nofail(dev
);
232 static void s390_create_virtio_net(BusState
*bus
, const char *name
)
236 for (i
= 0; i
< nb_nics
; i
++) {
237 NICInfo
*nd
= &nd_table
[i
];
241 nd
->model
= g_strdup("virtio");
244 qemu_check_nic_model(nd
, "virtio");
246 dev
= qdev_create(bus
, name
);
247 qdev_set_nic_properties(dev
, nd
);
248 qdev_init_nofail(dev
);
252 static void ccw_init(MachineState
*machine
)
255 VirtualCssBus
*css_bus
;
258 s390_memory_init(machine
->ram_size
);
260 /* init CPUs (incl. CPU model) early so s390_has_feature() works */
261 s390_init_cpus(machine
);
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);
271 if (s390_has_feat(S390_FEAT_ZPCI
)) {
272 DeviceState
*dev
= qdev_create(NULL
, TYPE_S390_PCI_HOST_BRIDGE
);
273 object_property_add_child(qdev_get_machine(),
274 TYPE_S390_PCI_HOST_BRIDGE
,
276 qdev_init_nofail(dev
);
279 /* register hypercalls */
280 virtio_ccw_register_hcalls();
282 s390_enable_css_support(s390_cpu_addr2state(0));
284 * Non mcss-e enabled guests only see the devices from the default
285 * css, which is determined by the value of the squash_mcss property.
286 * Note: we must not squash non virtual devices to css 0xFE.
288 if (css_bus
->squash_mcss
) {
289 ret
= css_create_css_image(0, true);
291 ret
= css_create_css_image(VIRTUAL_CSSID
, true);
295 /* Create VirtIO network adapters */
296 s390_create_virtio_net(BUS(css_bus
), "virtio-net-ccw");
298 /* Register savevm handler for guest TOD clock */
299 register_savevm_live(NULL
, "todclock", 0, 1, &savevm_gtod
, NULL
);
302 static void s390_cpu_plug(HotplugHandler
*hotplug_dev
,
303 DeviceState
*dev
, Error
**errp
)
306 S390CPU
*cpu
= S390_CPU(dev
);
307 CPUState
*cs
= CPU(dev
);
309 name
= g_strdup_printf("cpu[%i]", cpu
->env
.cpu_num
);
310 object_property_set_link(OBJECT(hotplug_dev
), OBJECT(cs
), name
,
315 static void s390_machine_reset(void)
317 S390CPU
*ipl_cpu
= S390_CPU(qemu_get_cpu(0));
320 qemu_devices_reset();
323 /* all cpus are stopped - configure and start the ipl cpu only */
324 s390_ipl_prepare_cpu(ipl_cpu
);
325 s390_cpu_set_state(CPU_STATE_OPERATING
, ipl_cpu
);
328 static void s390_machine_device_plug(HotplugHandler
*hotplug_dev
,
329 DeviceState
*dev
, Error
**errp
)
331 if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
332 s390_cpu_plug(hotplug_dev
, dev
, errp
);
336 static HotplugHandler
*s390_get_hotplug_handler(MachineState
*machine
,
339 if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
340 return HOTPLUG_HANDLER(machine
);
345 static void s390_hot_add_cpu(const int64_t id
, Error
**errp
)
347 MachineState
*machine
= MACHINE(qdev_get_machine());
349 s390x_new_cpu(machine
->cpu_model
, id
, errp
);
352 static void s390_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
354 CPUState
*cs
= qemu_get_cpu(cpu_index
);
356 if (s390_cpu_restart(S390_CPU(cs
))) {
357 error_setg(errp
, QERR_UNSUPPORTED
);
361 static void ccw_machine_class_init(ObjectClass
*oc
, void *data
)
363 MachineClass
*mc
= MACHINE_CLASS(oc
);
364 NMIClass
*nc
= NMI_CLASS(oc
);
365 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(oc
);
366 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
368 s390mc
->ri_allowed
= true;
369 s390mc
->cpu_model_allowed
= true;
370 s390mc
->css_migration_enabled
= true;
371 s390mc
->gs_allowed
= true;
373 mc
->reset
= s390_machine_reset
;
374 mc
->hot_add_cpu
= s390_hot_add_cpu
;
375 mc
->block_default_type
= IF_VIRTIO
;
383 mc
->get_hotplug_handler
= s390_get_hotplug_handler
;
384 hc
->plug
= s390_machine_device_plug
;
385 nc
->nmi_monitor_handler
= s390_nmi
;
388 static inline bool machine_get_aes_key_wrap(Object
*obj
, Error
**errp
)
390 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
392 return ms
->aes_key_wrap
;
395 static inline void machine_set_aes_key_wrap(Object
*obj
, bool value
,
398 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
400 ms
->aes_key_wrap
= value
;
403 static inline bool machine_get_dea_key_wrap(Object
*obj
, Error
**errp
)
405 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
407 return ms
->dea_key_wrap
;
410 static inline void machine_set_dea_key_wrap(Object
*obj
, bool value
,
413 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
415 ms
->dea_key_wrap
= value
;
418 static S390CcwMachineClass
*current_mc
;
420 static S390CcwMachineClass
*get_machine_class(void)
422 if (unlikely(!current_mc
)) {
424 * No s390 ccw machine was instantiated, we are likely to
425 * be called for the 'none' machine. The properties will
426 * have their after-initialization values.
428 current_mc
= S390_MACHINE_CLASS(
429 object_class_by_name(TYPE_S390_CCW_MACHINE
));
434 bool ri_allowed(void)
436 /* for "none" machine this results in true */
437 return get_machine_class()->ri_allowed
;
440 bool cpu_model_allowed(void)
442 /* for "none" machine this results in true */
443 return get_machine_class()->cpu_model_allowed
;
446 bool gs_allowed(void)
448 /* for "none" machine this results in true */
449 return get_machine_class()->gs_allowed
;
452 static char *machine_get_loadparm(Object
*obj
, Error
**errp
)
454 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
456 return g_memdup(ms
->loadparm
, sizeof(ms
->loadparm
));
459 static void machine_set_loadparm(Object
*obj
, const char *val
, Error
**errp
)
461 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
464 for (i
= 0; i
< sizeof(ms
->loadparm
) && val
[i
]; i
++) {
465 uint8_t c
= qemu_toupper(val
[i
]); /* mimic HMC */
467 if (('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || (c
== '.') ||
471 error_setg(errp
, "LOADPARM: invalid character '%c' (ASCII 0x%02x)",
477 for (; i
< sizeof(ms
->loadparm
); i
++) {
478 ms
->loadparm
[i
] = ' '; /* pad right with spaces */
481 static inline bool machine_get_squash_mcss(Object
*obj
, Error
**errp
)
483 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
485 return ms
->s390_squash_mcss
;
488 static inline void machine_set_squash_mcss(Object
*obj
, bool value
,
491 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
493 ms
->s390_squash_mcss
= value
;
496 static inline void s390_machine_initfn(Object
*obj
)
498 object_property_add_bool(obj
, "aes-key-wrap",
499 machine_get_aes_key_wrap
,
500 machine_set_aes_key_wrap
, NULL
);
501 object_property_set_description(obj
, "aes-key-wrap",
502 "enable/disable AES key wrapping using the CPACF wrapping key",
504 object_property_set_bool(obj
, true, "aes-key-wrap", NULL
);
506 object_property_add_bool(obj
, "dea-key-wrap",
507 machine_get_dea_key_wrap
,
508 machine_set_dea_key_wrap
, NULL
);
509 object_property_set_description(obj
, "dea-key-wrap",
510 "enable/disable DEA key wrapping using the CPACF wrapping key",
512 object_property_set_bool(obj
, true, "dea-key-wrap", NULL
);
513 object_property_add_str(obj
, "loadparm",
514 machine_get_loadparm
, machine_set_loadparm
, NULL
);
515 object_property_set_description(obj
, "loadparm",
516 "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted"
517 " to upper case) to pass to machine loader, boot manager,"
520 object_property_add_bool(obj
, "s390-squash-mcss",
521 machine_get_squash_mcss
,
522 machine_set_squash_mcss
, NULL
);
523 object_property_set_description(obj
, "s390-squash-mcss",
524 "enable/disable squashing subchannels into the default css",
526 object_property_set_bool(obj
, false, "s390-squash-mcss", NULL
);
529 static const TypeInfo ccw_machine_info
= {
530 .name
= TYPE_S390_CCW_MACHINE
,
531 .parent
= TYPE_MACHINE
,
533 .instance_size
= sizeof(S390CcwMachineState
),
534 .instance_init
= s390_machine_initfn
,
535 .class_size
= sizeof(S390CcwMachineClass
),
536 .class_init
= ccw_machine_class_init
,
537 .interfaces
= (InterfaceInfo
[]) {
539 { TYPE_HOTPLUG_HANDLER
},
544 bool css_migration_enabled(void)
546 return get_machine_class()->css_migration_enabled
;
549 #define DEFINE_CCW_MACHINE(suffix, verstr, latest) \
550 static void ccw_machine_##suffix##_class_init(ObjectClass *oc, \
553 MachineClass *mc = MACHINE_CLASS(oc); \
554 ccw_machine_##suffix##_class_options(mc); \
555 mc->desc = "VirtIO-ccw based S390 machine v" verstr; \
557 mc->alias = "s390-ccw-virtio"; \
558 mc->is_default = 1; \
561 static void ccw_machine_##suffix##_instance_init(Object *obj) \
563 MachineState *machine = MACHINE(obj); \
564 current_mc = S390_MACHINE_CLASS(MACHINE_GET_CLASS(machine)); \
565 ccw_machine_##suffix##_instance_options(machine); \
567 static const TypeInfo ccw_machine_##suffix##_info = { \
568 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr), \
569 .parent = TYPE_S390_CCW_MACHINE, \
570 .class_init = ccw_machine_##suffix##_class_init, \
571 .instance_init = ccw_machine_##suffix##_instance_init, \
573 static void ccw_machine_register_##suffix(void) \
575 type_register_static(&ccw_machine_##suffix##_info); \
577 type_init(ccw_machine_register_##suffix)
579 #define CCW_COMPAT_2_10 \
582 #define CCW_COMPAT_2_9 \
585 .driver = TYPE_S390_STATTRIB,\
586 .property = "migration-enabled",\
590 #define CCW_COMPAT_2_8 \
593 .driver = TYPE_S390_FLIC_COMMON,\
594 .property = "adapter_routes_max_batch",\
598 #define CCW_COMPAT_2_7 \
601 #define CCW_COMPAT_2_6 \
604 .driver = TYPE_S390_IPL,\
605 .property = "iplbext_migration",\
608 .driver = TYPE_VIRTUAL_CSS_BRIDGE,\
609 .property = "css_dev_path",\
613 #define CCW_COMPAT_2_5 \
616 #define CCW_COMPAT_2_4 \
619 .driver = TYPE_S390_SKEYS,\
620 .property = "migration-enabled",\
623 .driver = "virtio-blk-ccw",\
624 .property = "max_revision",\
627 .driver = "virtio-balloon-ccw",\
628 .property = "max_revision",\
631 .driver = "virtio-serial-ccw",\
632 .property = "max_revision",\
635 .driver = "virtio-9p-ccw",\
636 .property = "max_revision",\
639 .driver = "virtio-rng-ccw",\
640 .property = "max_revision",\
643 .driver = "virtio-net-ccw",\
644 .property = "max_revision",\
647 .driver = "virtio-scsi-ccw",\
648 .property = "max_revision",\
651 .driver = "vhost-scsi-ccw",\
652 .property = "max_revision",\
656 static void ccw_machine_2_11_instance_options(MachineState
*machine
)
660 static void ccw_machine_2_11_class_options(MachineClass
*mc
)
663 DEFINE_CCW_MACHINE(2_11
, "2.11", true);
665 static void ccw_machine_2_10_instance_options(MachineState
*machine
)
667 ccw_machine_2_11_instance_options(machine
);
668 if (css_migration_enabled()) {
669 css_register_vmstate();
673 static void ccw_machine_2_10_class_options(MachineClass
*mc
)
675 ccw_machine_2_11_class_options(mc
);
676 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_10
);
678 DEFINE_CCW_MACHINE(2_10
, "2.10", false);
680 static void ccw_machine_2_9_instance_options(MachineState
*machine
)
682 ccw_machine_2_10_instance_options(machine
);
683 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP
);
684 s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2
);
685 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ZPCI
);
686 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_INT_SUPPRESSION
);
687 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_EVENT_NOTIFICATION
);
690 static void ccw_machine_2_9_class_options(MachineClass
*mc
)
692 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
694 s390mc
->gs_allowed
= false;
695 ccw_machine_2_10_class_options(mc
);
696 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_9
);
697 s390mc
->css_migration_enabled
= false;
699 DEFINE_CCW_MACHINE(2_9
, "2.9", false);
701 static void ccw_machine_2_8_instance_options(MachineState
*machine
)
703 ccw_machine_2_9_instance_options(machine
);
706 static void ccw_machine_2_8_class_options(MachineClass
*mc
)
708 ccw_machine_2_9_class_options(mc
);
709 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_8
);
711 DEFINE_CCW_MACHINE(2_8
, "2.8", false);
713 static void ccw_machine_2_7_instance_options(MachineState
*machine
)
715 ccw_machine_2_8_instance_options(machine
);
718 static void ccw_machine_2_7_class_options(MachineClass
*mc
)
720 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
722 s390mc
->cpu_model_allowed
= false;
723 ccw_machine_2_8_class_options(mc
);
724 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_7
);
726 DEFINE_CCW_MACHINE(2_7
, "2.7", false);
728 static void ccw_machine_2_6_instance_options(MachineState
*machine
)
730 ccw_machine_2_7_instance_options(machine
);
733 static void ccw_machine_2_6_class_options(MachineClass
*mc
)
735 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
737 s390mc
->ri_allowed
= false;
738 ccw_machine_2_7_class_options(mc
);
739 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_6
);
741 DEFINE_CCW_MACHINE(2_6
, "2.6", false);
743 static void ccw_machine_2_5_instance_options(MachineState
*machine
)
745 ccw_machine_2_6_instance_options(machine
);
748 static void ccw_machine_2_5_class_options(MachineClass
*mc
)
750 ccw_machine_2_6_class_options(mc
);
751 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_5
);
753 DEFINE_CCW_MACHINE(2_5
, "2.5", false);
755 static void ccw_machine_2_4_instance_options(MachineState
*machine
)
757 ccw_machine_2_5_instance_options(machine
);
760 static void ccw_machine_2_4_class_options(MachineClass
*mc
)
762 ccw_machine_2_5_class_options(mc
);
763 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_4
);
765 DEFINE_CCW_MACHINE(2_4
, "2.4", false);
767 static void ccw_machine_register_types(void)
769 type_register_static(&ccw_machine_info
);
772 type_init(ccw_machine_register_types
)