4 * Copyright 2012 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
16 #include "hw/boards.h"
17 #include "exec/address-spaces.h"
18 #include "s390-virtio.h"
19 #include "hw/s390x/sclp.h"
20 #include "hw/s390x/s390_flic.h"
23 #include "virtio-ccw.h"
24 #include "qemu/config-file.h"
25 #include "s390-pci-bus.h"
26 #include "hw/s390x/storage-keys.h"
27 #include "hw/compat.h"
28 #include "hw/s390x/s390-virtio-ccw.h"
30 static const char *const reset_dev_types[] = {
32 "s390-sclp-event-facility",
37 void subsystem_reset(void)
42 for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
43 dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
50 static int virtio_ccw_hcall_notify(const uint64_t *args)
52 uint64_t subch_id = args[0];
53 uint64_t queue = args[1];
55 int cssid, ssid, schid, m;
57 if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
60 sch = css_find_subch(m, cssid, ssid, schid);
61 if (!sch || !css_subch_visible(sch)) {
64 if (queue >= VIRTIO_CCW_QUEUE_MAX) {
67 virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
72 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
74 uint64_t mem = args[0];
83 static void virtio_ccw_register_hcalls(void)
85 s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
86 virtio_ccw_hcall_notify);
87 /* Tolerate early printk. */
88 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
89 virtio_ccw_hcall_early_printk);
92 void s390_memory_init(ram_addr_t mem_size)
94 MemoryRegion *sysmem = get_system_memory();
95 MemoryRegion *ram = g_new(MemoryRegion, 1);
97 /* allocate RAM for core */
98 memory_region_allocate_system_memory(ram, NULL, "s390.ram", mem_size);
99 memory_region_add_subregion(sysmem, 0, ram);
101 /* Initialize storage key device */
105 static void ccw_init(MachineState *machine)
108 VirtualCssBus *css_bus;
112 s390_memory_init(machine->ram_size);
115 css_bus = virtual_css_bus_init();
116 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
117 machine->initrd_filename, "s390-ccw.img", true);
120 dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
121 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
123 qdev_init_nofail(dev);
125 /* register hypercalls */
126 virtio_ccw_register_hcalls();
129 s390_init_cpus(machine);
132 kvm_s390_enable_css_support(s390_cpu_addr2state(0));
135 * Create virtual css and set it as default so that non mcss-e
136 * enabled guests only see virtio devices.
138 ret = css_create_css_image(VIRTUAL_CSSID, true);
141 /* Create VirtIO network adapters */
142 s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
144 /* Register savevm handler for guest TOD clock */
145 register_savevm(NULL, "todclock", 0, 1,
146 gtod_save, gtod_load, kvm_state);
149 static void s390_cpu_plug(HotplugHandler *hotplug_dev,
150 DeviceState *dev, Error **errp)
153 S390CPU *cpu = S390_CPU(dev);
154 CPUState *cs = CPU(dev);
156 name = g_strdup_printf("cpu[%i]", cpu->env.cpu_num);
157 object_property_set_link(OBJECT(hotplug_dev), OBJECT(cs), name,
162 static void s390_machine_device_plug(HotplugHandler *hotplug_dev,
163 DeviceState *dev, Error **errp)
165 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
166 s390_cpu_plug(hotplug_dev, dev, errp);
170 static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
173 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
174 return HOTPLUG_HANDLER(machine);
179 static void s390_hot_add_cpu(const int64_t id, Error **errp)
181 MachineState *machine = MACHINE(qdev_get_machine());
184 s390x_new_cpu(machine->cpu_model, id, &err);
185 error_propagate(errp, err);
188 static void ccw_machine_class_init(ObjectClass *oc, void *data)
190 MachineClass *mc = MACHINE_CLASS(oc);
191 NMIClass *nc = NMI_CLASS(oc);
192 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
195 mc->reset = s390_machine_reset;
196 mc->hot_add_cpu = s390_hot_add_cpu;
197 mc->block_default_type = IF_VIRTIO;
205 mc->get_hotplug_handler = s390_get_hotplug_handler;
206 hc->plug = s390_machine_device_plug;
207 nc->nmi_monitor_handler = s390_nmi;
210 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
212 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
214 return ms->aes_key_wrap;
217 static inline void machine_set_aes_key_wrap(Object *obj, bool value,
220 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
222 ms->aes_key_wrap = value;
225 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
227 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
229 return ms->dea_key_wrap;
232 static inline void machine_set_dea_key_wrap(Object *obj, bool value,
235 S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
237 ms->dea_key_wrap = value;
240 static inline void s390_machine_initfn(Object *obj)
242 object_property_add_bool(obj, "aes-key-wrap",
243 machine_get_aes_key_wrap,
244 machine_set_aes_key_wrap, NULL);
245 object_property_set_description(obj, "aes-key-wrap",
246 "enable/disable AES key wrapping using the CPACF wrapping key",
248 object_property_set_bool(obj, true, "aes-key-wrap", NULL);
250 object_property_add_bool(obj, "dea-key-wrap",
251 machine_get_dea_key_wrap,
252 machine_set_dea_key_wrap, NULL);
253 object_property_set_description(obj, "dea-key-wrap",
254 "enable/disable DEA key wrapping using the CPACF wrapping key",
256 object_property_set_bool(obj, true, "dea-key-wrap", NULL);
259 static const TypeInfo ccw_machine_info = {
260 .name = TYPE_S390_CCW_MACHINE,
261 .parent = TYPE_MACHINE,
263 .instance_size = sizeof(S390CcwMachineState),
264 .instance_init = s390_machine_initfn,
265 .class_init = ccw_machine_class_init,
266 .interfaces = (InterfaceInfo[]) {
268 { TYPE_HOTPLUG_HANDLER},
273 #define DEFINE_CCW_MACHINE(suffix, verstr, latest) \
274 static void ccw_machine_##suffix##_class_init(ObjectClass *oc, \
277 MachineClass *mc = MACHINE_CLASS(oc); \
278 ccw_machine_##suffix##_class_options(mc); \
279 mc->desc = "VirtIO-ccw based S390 machine v" verstr; \
281 mc->alias = "s390-ccw-virtio"; \
282 mc->is_default = 1; \
285 static void ccw_machine_##suffix##_instance_init(Object *obj) \
287 MachineState *machine = MACHINE(obj); \
288 ccw_machine_##suffix##_instance_options(machine); \
290 static const TypeInfo ccw_machine_##suffix##_info = { \
291 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr), \
292 .parent = TYPE_S390_CCW_MACHINE, \
293 .class_init = ccw_machine_##suffix##_class_init, \
294 .instance_init = ccw_machine_##suffix##_instance_init, \
296 static void ccw_machine_register_##suffix(void) \
298 type_register_static(&ccw_machine_##suffix##_info); \
300 type_init(ccw_machine_register_##suffix)
302 #define CCW_COMPAT_2_5 \
305 #define CCW_COMPAT_2_4 \
309 .driver = TYPE_S390_SKEYS,\
310 .property = "migration-enabled",\
313 .driver = "virtio-blk-ccw",\
314 .property = "max_revision",\
317 .driver = "virtio-balloon-ccw",\
318 .property = "max_revision",\
321 .driver = "virtio-serial-ccw",\
322 .property = "max_revision",\
325 .driver = "virtio-9p-ccw",\
326 .property = "max_revision",\
329 .driver = "virtio-rng-ccw",\
330 .property = "max_revision",\
333 .driver = "virtio-net-ccw",\
334 .property = "max_revision",\
337 .driver = "virtio-scsi-ccw",\
338 .property = "max_revision",\
341 .driver = "vhost-scsi-ccw",\
342 .property = "max_revision",\
346 static void ccw_machine_2_6_instance_options(MachineState *machine)
350 static void ccw_machine_2_6_class_options(MachineClass *mc)
353 DEFINE_CCW_MACHINE(2_6, "2.6", true);
355 static void ccw_machine_2_5_instance_options(MachineState *machine)
359 static void ccw_machine_2_5_class_options(MachineClass *mc)
361 SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_5);
363 DEFINE_CCW_MACHINE(2_5, "2.5", false);
365 static void ccw_machine_2_4_instance_options(MachineState *machine)
367 ccw_machine_2_5_instance_options(machine);
370 static void ccw_machine_2_4_class_options(MachineClass *mc)
372 SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_4);
374 DEFINE_CCW_MACHINE(2_4, "2.4", false);
376 static void ccw_machine_register_types(void)
378 type_register_static(&ccw_machine_info);
381 type_init(ccw_machine_register_types)