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 "qemu/error-report.h"
27 #include "s390-pci-bus.h"
28 #include "hw/s390x/storage-keys.h"
29 #include "hw/s390x/storage-attributes.h"
30 #include "hw/compat.h"
32 #include "hw/s390x/s390-virtio-ccw.h"
33 #include "hw/s390x/css-bridge.h"
34 #include "migration/register.h"
35 #include "cpu_models.h"
36 #include "qapi/qmp/qerror.h"
39 static S390CPU
**cpu_states
;
41 S390CPU
*s390_cpu_addr2state(uint16_t cpu_addr
)
43 if (cpu_addr
>= max_cpus
) {
47 /* Fast lookup via CPU ID */
48 return cpu_states
[cpu_addr
];
51 static void s390_init_cpus(MachineState
*machine
)
56 if (machine
->cpu_model
== NULL
) {
57 machine
->cpu_model
= s390_default_cpu_model_name();
59 if (tcg_enabled() && max_cpus
> 1) {
60 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
61 "supported by TCG (1) on s390x", max_cpus
);
65 cpu_states
= g_new0(S390CPU
*, max_cpus
);
67 for (i
= 0; i
< max_cpus
; i
++) {
68 name
= g_strdup_printf("cpu[%i]", i
);
69 object_property_add_link(OBJECT(machine
), name
, TYPE_S390_CPU
,
70 (Object
**) &cpu_states
[i
],
71 object_property_allow_set_link
,
72 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
77 for (i
= 0; i
< smp_cpus
; i
++) {
78 s390x_new_cpu(machine
->cpu_model
, i
, &error_fatal
);
82 static const char *const reset_dev_types
[] = {
83 TYPE_VIRTUAL_CSS_BRIDGE
,
84 "s390-sclp-event-facility",
89 void subsystem_reset(void)
94 for (i
= 0; i
< ARRAY_SIZE(reset_dev_types
); i
++) {
95 dev
= DEVICE(object_resolve_path_type("", reset_dev_types
[i
], NULL
));
102 static int virtio_ccw_hcall_notify(const uint64_t *args
)
104 uint64_t subch_id
= args
[0];
105 uint64_t queue
= args
[1];
107 int cssid
, ssid
, schid
, m
;
109 if (ioinst_disassemble_sch_ident(subch_id
, &m
, &cssid
, &ssid
, &schid
)) {
112 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
113 if (!sch
|| !css_subch_visible(sch
)) {
116 if (queue
>= VIRTIO_QUEUE_MAX
) {
119 virtio_queue_notify(virtio_ccw_get_vdev(sch
), queue
);
124 static int virtio_ccw_hcall_early_printk(const uint64_t *args
)
126 uint64_t mem
= args
[0];
128 if (mem
< ram_size
) {
135 static void virtio_ccw_register_hcalls(void)
137 s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY
,
138 virtio_ccw_hcall_notify
);
139 /* Tolerate early printk. */
140 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY
,
141 virtio_ccw_hcall_early_printk
);
144 static void s390_memory_init(ram_addr_t mem_size
)
146 MemoryRegion
*sysmem
= get_system_memory();
147 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
149 /* allocate RAM for core */
150 memory_region_allocate_system_memory(ram
, NULL
, "s390.ram", mem_size
);
151 memory_region_add_subregion(sysmem
, 0, ram
);
153 /* Initialize storage key device */
155 /* Initialize storage attributes device */
156 s390_stattrib_init();
159 #define S390_TOD_CLOCK_VALUE_MISSING 0x00
160 #define S390_TOD_CLOCK_VALUE_PRESENT 0x01
162 static void gtod_save(QEMUFile
*f
, void *opaque
)
168 r
= s390_get_clock(&tod_high
, &tod_low
);
170 warn_report("Unable to get guest clock for migration: %s",
172 error_printf("Guest clock will not be migrated "
173 "which could cause the guest to hang.");
174 qemu_put_byte(f
, S390_TOD_CLOCK_VALUE_MISSING
);
178 qemu_put_byte(f
, S390_TOD_CLOCK_VALUE_PRESENT
);
179 qemu_put_byte(f
, tod_high
);
180 qemu_put_be64(f
, tod_low
);
183 static int gtod_load(QEMUFile
*f
, void *opaque
, int version_id
)
189 if (qemu_get_byte(f
) == S390_TOD_CLOCK_VALUE_MISSING
) {
190 warn_report("Guest clock was not migrated. This could "
191 "cause the guest to hang.");
195 tod_high
= qemu_get_byte(f
);
196 tod_low
= qemu_get_be64(f
);
198 r
= s390_set_clock(&tod_high
, &tod_low
);
200 warn_report("Unable to set guest clock for migration: %s",
202 error_printf("Guest clock will not be restored "
203 "which could cause the guest to hang.");
209 static SaveVMHandlers savevm_gtod
= {
210 .save_state
= gtod_save
,
211 .load_state
= gtod_load
,
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 const char *netboot_fw
, bool enforce_bios
)
219 Object
*new = object_new(TYPE_S390_IPL
);
220 DeviceState
*dev
= DEVICE(new);
222 if (kernel_filename
) {
223 qdev_prop_set_string(dev
, "kernel", kernel_filename
);
225 if (initrd_filename
) {
226 qdev_prop_set_string(dev
, "initrd", initrd_filename
);
228 qdev_prop_set_string(dev
, "cmdline", kernel_cmdline
);
229 qdev_prop_set_string(dev
, "firmware", firmware
);
230 qdev_prop_set_string(dev
, "netboot_fw", netboot_fw
);
231 qdev_prop_set_bit(dev
, "enforce_bios", enforce_bios
);
232 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL
,
235 qdev_init_nofail(dev
);
238 static void s390_create_virtio_net(BusState
*bus
, const char *name
)
242 for (i
= 0; i
< nb_nics
; i
++) {
243 NICInfo
*nd
= &nd_table
[i
];
247 nd
->model
= g_strdup("virtio");
250 qemu_check_nic_model(nd
, "virtio");
252 dev
= qdev_create(bus
, name
);
253 qdev_set_nic_properties(dev
, nd
);
254 qdev_init_nofail(dev
);
258 static void ccw_init(MachineState
*machine
)
261 VirtualCssBus
*css_bus
;
264 s390_memory_init(machine
->ram_size
);
266 /* init CPUs (incl. CPU model) early so s390_has_feature() works */
267 s390_init_cpus(machine
);
272 css_bus
= virtual_css_bus_init();
273 s390_init_ipl_dev(machine
->kernel_filename
, machine
->kernel_cmdline
,
274 machine
->initrd_filename
, "s390-ccw.img",
275 "s390-netboot.img", true);
277 if (s390_has_feat(S390_FEAT_ZPCI
)) {
278 DeviceState
*dev
= qdev_create(NULL
, TYPE_S390_PCI_HOST_BRIDGE
);
279 object_property_add_child(qdev_get_machine(),
280 TYPE_S390_PCI_HOST_BRIDGE
,
282 qdev_init_nofail(dev
);
285 /* register hypercalls */
286 virtio_ccw_register_hcalls();
288 s390_enable_css_support(s390_cpu_addr2state(0));
290 * Non mcss-e enabled guests only see the devices from the default
291 * css, which is determined by the value of the squash_mcss property.
292 * Note: we must not squash non virtual devices to css 0xFE.
294 if (css_bus
->squash_mcss
) {
295 ret
= css_create_css_image(0, true);
297 ret
= css_create_css_image(VIRTUAL_CSSID
, true);
301 /* Create VirtIO network adapters */
302 s390_create_virtio_net(BUS(css_bus
), "virtio-net-ccw");
304 /* Register savevm handler for guest TOD clock */
305 register_savevm_live(NULL
, "todclock", 0, 1, &savevm_gtod
, NULL
);
308 static void s390_cpu_plug(HotplugHandler
*hotplug_dev
,
309 DeviceState
*dev
, Error
**errp
)
312 S390CPU
*cpu
= S390_CPU(dev
);
313 CPUState
*cs
= CPU(dev
);
315 name
= g_strdup_printf("cpu[%i]", cpu
->env
.core_id
);
316 object_property_set_link(OBJECT(hotplug_dev
), OBJECT(cs
), name
,
321 static void s390_machine_reset(void)
323 S390CPU
*ipl_cpu
= S390_CPU(qemu_get_cpu(0));
326 qemu_devices_reset();
329 /* all cpus are stopped - configure and start the ipl cpu only */
330 s390_ipl_prepare_cpu(ipl_cpu
);
331 s390_cpu_set_state(CPU_STATE_OPERATING
, ipl_cpu
);
334 static void s390_machine_device_plug(HotplugHandler
*hotplug_dev
,
335 DeviceState
*dev
, Error
**errp
)
337 if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
338 s390_cpu_plug(hotplug_dev
, dev
, errp
);
342 static HotplugHandler
*s390_get_hotplug_handler(MachineState
*machine
,
345 if (object_dynamic_cast(OBJECT(dev
), TYPE_CPU
)) {
346 return HOTPLUG_HANDLER(machine
);
351 static void s390_hot_add_cpu(const int64_t id
, Error
**errp
)
353 MachineState
*machine
= MACHINE(qdev_get_machine());
355 s390x_new_cpu(machine
->cpu_model
, id
, errp
);
358 static void s390_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
360 CPUState
*cs
= qemu_get_cpu(cpu_index
);
362 if (s390_cpu_restart(S390_CPU(cs
))) {
363 error_setg(errp
, QERR_UNSUPPORTED
);
367 static void ccw_machine_class_init(ObjectClass
*oc
, void *data
)
369 MachineClass
*mc
= MACHINE_CLASS(oc
);
370 NMIClass
*nc
= NMI_CLASS(oc
);
371 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(oc
);
372 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
374 s390mc
->ri_allowed
= true;
375 s390mc
->cpu_model_allowed
= true;
376 s390mc
->css_migration_enabled
= true;
377 s390mc
->gs_allowed
= true;
379 mc
->reset
= s390_machine_reset
;
380 mc
->hot_add_cpu
= s390_hot_add_cpu
;
381 mc
->block_default_type
= IF_VIRTIO
;
389 mc
->get_hotplug_handler
= s390_get_hotplug_handler
;
390 hc
->plug
= s390_machine_device_plug
;
391 nc
->nmi_monitor_handler
= s390_nmi
;
394 static inline bool machine_get_aes_key_wrap(Object
*obj
, Error
**errp
)
396 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
398 return ms
->aes_key_wrap
;
401 static inline void machine_set_aes_key_wrap(Object
*obj
, bool value
,
404 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
406 ms
->aes_key_wrap
= value
;
409 static inline bool machine_get_dea_key_wrap(Object
*obj
, Error
**errp
)
411 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
413 return ms
->dea_key_wrap
;
416 static inline void machine_set_dea_key_wrap(Object
*obj
, bool value
,
419 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
421 ms
->dea_key_wrap
= value
;
424 static S390CcwMachineClass
*current_mc
;
426 static S390CcwMachineClass
*get_machine_class(void)
428 if (unlikely(!current_mc
)) {
430 * No s390 ccw machine was instantiated, we are likely to
431 * be called for the 'none' machine. The properties will
432 * have their after-initialization values.
434 current_mc
= S390_MACHINE_CLASS(
435 object_class_by_name(TYPE_S390_CCW_MACHINE
));
440 bool ri_allowed(void)
442 /* for "none" machine this results in true */
443 return get_machine_class()->ri_allowed
;
446 bool cpu_model_allowed(void)
448 /* for "none" machine this results in true */
449 return get_machine_class()->cpu_model_allowed
;
452 bool gs_allowed(void)
454 /* for "none" machine this results in true */
455 return get_machine_class()->gs_allowed
;
458 static char *machine_get_loadparm(Object
*obj
, Error
**errp
)
460 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
462 return g_memdup(ms
->loadparm
, sizeof(ms
->loadparm
));
465 static void machine_set_loadparm(Object
*obj
, const char *val
, Error
**errp
)
467 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
470 for (i
= 0; i
< sizeof(ms
->loadparm
) && val
[i
]; i
++) {
471 uint8_t c
= qemu_toupper(val
[i
]); /* mimic HMC */
473 if (('A' <= c
&& c
<= 'Z') || ('0' <= c
&& c
<= '9') || (c
== '.') ||
477 error_setg(errp
, "LOADPARM: invalid character '%c' (ASCII 0x%02x)",
483 for (; i
< sizeof(ms
->loadparm
); i
++) {
484 ms
->loadparm
[i
] = ' '; /* pad right with spaces */
487 static inline bool machine_get_squash_mcss(Object
*obj
, Error
**errp
)
489 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
491 return ms
->s390_squash_mcss
;
494 static inline void machine_set_squash_mcss(Object
*obj
, bool value
,
497 S390CcwMachineState
*ms
= S390_CCW_MACHINE(obj
);
499 ms
->s390_squash_mcss
= value
;
502 static inline void s390_machine_initfn(Object
*obj
)
504 object_property_add_bool(obj
, "aes-key-wrap",
505 machine_get_aes_key_wrap
,
506 machine_set_aes_key_wrap
, NULL
);
507 object_property_set_description(obj
, "aes-key-wrap",
508 "enable/disable AES key wrapping using the CPACF wrapping key",
510 object_property_set_bool(obj
, true, "aes-key-wrap", NULL
);
512 object_property_add_bool(obj
, "dea-key-wrap",
513 machine_get_dea_key_wrap
,
514 machine_set_dea_key_wrap
, NULL
);
515 object_property_set_description(obj
, "dea-key-wrap",
516 "enable/disable DEA key wrapping using the CPACF wrapping key",
518 object_property_set_bool(obj
, true, "dea-key-wrap", NULL
);
519 object_property_add_str(obj
, "loadparm",
520 machine_get_loadparm
, machine_set_loadparm
, NULL
);
521 object_property_set_description(obj
, "loadparm",
522 "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted"
523 " to upper case) to pass to machine loader, boot manager,"
526 object_property_add_bool(obj
, "s390-squash-mcss",
527 machine_get_squash_mcss
,
528 machine_set_squash_mcss
, NULL
);
529 object_property_set_description(obj
, "s390-squash-mcss",
530 "enable/disable squashing subchannels into the default css",
532 object_property_set_bool(obj
, false, "s390-squash-mcss", NULL
);
535 static const TypeInfo ccw_machine_info
= {
536 .name
= TYPE_S390_CCW_MACHINE
,
537 .parent
= TYPE_MACHINE
,
539 .instance_size
= sizeof(S390CcwMachineState
),
540 .instance_init
= s390_machine_initfn
,
541 .class_size
= sizeof(S390CcwMachineClass
),
542 .class_init
= ccw_machine_class_init
,
543 .interfaces
= (InterfaceInfo
[]) {
545 { TYPE_HOTPLUG_HANDLER
},
550 bool css_migration_enabled(void)
552 return get_machine_class()->css_migration_enabled
;
555 #define DEFINE_CCW_MACHINE(suffix, verstr, latest) \
556 static void ccw_machine_##suffix##_class_init(ObjectClass *oc, \
559 MachineClass *mc = MACHINE_CLASS(oc); \
560 ccw_machine_##suffix##_class_options(mc); \
561 mc->desc = "VirtIO-ccw based S390 machine v" verstr; \
563 mc->alias = "s390-ccw-virtio"; \
564 mc->is_default = 1; \
567 static void ccw_machine_##suffix##_instance_init(Object *obj) \
569 MachineState *machine = MACHINE(obj); \
570 current_mc = S390_MACHINE_CLASS(MACHINE_GET_CLASS(machine)); \
571 ccw_machine_##suffix##_instance_options(machine); \
573 static const TypeInfo ccw_machine_##suffix##_info = { \
574 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr), \
575 .parent = TYPE_S390_CCW_MACHINE, \
576 .class_init = ccw_machine_##suffix##_class_init, \
577 .instance_init = ccw_machine_##suffix##_instance_init, \
579 static void ccw_machine_register_##suffix(void) \
581 type_register_static(&ccw_machine_##suffix##_info); \
583 type_init(ccw_machine_register_##suffix)
585 #define CCW_COMPAT_2_10 \
588 #define CCW_COMPAT_2_9 \
591 .driver = TYPE_S390_STATTRIB,\
592 .property = "migration-enabled",\
596 #define CCW_COMPAT_2_8 \
599 .driver = TYPE_S390_FLIC_COMMON,\
600 .property = "adapter_routes_max_batch",\
604 #define CCW_COMPAT_2_7 \
607 #define CCW_COMPAT_2_6 \
610 .driver = TYPE_S390_IPL,\
611 .property = "iplbext_migration",\
614 .driver = TYPE_VIRTUAL_CSS_BRIDGE,\
615 .property = "css_dev_path",\
619 #define CCW_COMPAT_2_5 \
622 #define CCW_COMPAT_2_4 \
625 .driver = TYPE_S390_SKEYS,\
626 .property = "migration-enabled",\
629 .driver = "virtio-blk-ccw",\
630 .property = "max_revision",\
633 .driver = "virtio-balloon-ccw",\
634 .property = "max_revision",\
637 .driver = "virtio-serial-ccw",\
638 .property = "max_revision",\
641 .driver = "virtio-9p-ccw",\
642 .property = "max_revision",\
645 .driver = "virtio-rng-ccw",\
646 .property = "max_revision",\
649 .driver = "virtio-net-ccw",\
650 .property = "max_revision",\
653 .driver = "virtio-scsi-ccw",\
654 .property = "max_revision",\
657 .driver = "vhost-scsi-ccw",\
658 .property = "max_revision",\
662 static void ccw_machine_2_11_instance_options(MachineState
*machine
)
666 static void ccw_machine_2_11_class_options(MachineClass
*mc
)
669 DEFINE_CCW_MACHINE(2_11
, "2.11", true);
671 static void ccw_machine_2_10_instance_options(MachineState
*machine
)
673 ccw_machine_2_11_instance_options(machine
);
674 if (css_migration_enabled()) {
675 css_register_vmstate();
679 static void ccw_machine_2_10_class_options(MachineClass
*mc
)
681 ccw_machine_2_11_class_options(mc
);
682 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_10
);
684 DEFINE_CCW_MACHINE(2_10
, "2.10", false);
686 static void ccw_machine_2_9_instance_options(MachineState
*machine
)
688 ccw_machine_2_10_instance_options(machine
);
689 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP
);
690 s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2
);
691 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ZPCI
);
692 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_INT_SUPPRESSION
);
693 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_EVENT_NOTIFICATION
);
696 static void ccw_machine_2_9_class_options(MachineClass
*mc
)
698 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
700 s390mc
->gs_allowed
= false;
701 ccw_machine_2_10_class_options(mc
);
702 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_9
);
703 s390mc
->css_migration_enabled
= false;
705 DEFINE_CCW_MACHINE(2_9
, "2.9", false);
707 static void ccw_machine_2_8_instance_options(MachineState
*machine
)
709 ccw_machine_2_9_instance_options(machine
);
712 static void ccw_machine_2_8_class_options(MachineClass
*mc
)
714 ccw_machine_2_9_class_options(mc
);
715 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_8
);
717 DEFINE_CCW_MACHINE(2_8
, "2.8", false);
719 static void ccw_machine_2_7_instance_options(MachineState
*machine
)
721 ccw_machine_2_8_instance_options(machine
);
724 static void ccw_machine_2_7_class_options(MachineClass
*mc
)
726 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
728 s390mc
->cpu_model_allowed
= false;
729 ccw_machine_2_8_class_options(mc
);
730 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_7
);
732 DEFINE_CCW_MACHINE(2_7
, "2.7", false);
734 static void ccw_machine_2_6_instance_options(MachineState
*machine
)
736 ccw_machine_2_7_instance_options(machine
);
739 static void ccw_machine_2_6_class_options(MachineClass
*mc
)
741 S390CcwMachineClass
*s390mc
= S390_MACHINE_CLASS(mc
);
743 s390mc
->ri_allowed
= false;
744 ccw_machine_2_7_class_options(mc
);
745 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_6
);
747 DEFINE_CCW_MACHINE(2_6
, "2.6", false);
749 static void ccw_machine_2_5_instance_options(MachineState
*machine
)
751 ccw_machine_2_6_instance_options(machine
);
754 static void ccw_machine_2_5_class_options(MachineClass
*mc
)
756 ccw_machine_2_6_class_options(mc
);
757 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_5
);
759 DEFINE_CCW_MACHINE(2_5
, "2.5", false);
761 static void ccw_machine_2_4_instance_options(MachineState
*machine
)
763 ccw_machine_2_5_instance_options(machine
);
766 static void ccw_machine_2_4_class_options(MachineClass
*mc
)
768 ccw_machine_2_5_class_options(mc
);
769 SET_MACHINE_COMPAT(mc
, CCW_COMPAT_2_4
);
771 DEFINE_CCW_MACHINE(2_4
, "2.4", false);
773 static void ccw_machine_register_types(void)
775 type_register_static(&ccw_machine_info
);
778 type_init(ccw_machine_register_types
)