xref: /qemu/hw/s390x/s390-virtio-ccw.c (revision d046c51d)
1 /*
2  * virtio ccw machine
3  *
4  * Copyright 2012 IBM Corp.
5  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6  *
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
9  * directory.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
15 #include "cpu.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"
21 #include "ioinst.h"
22 #include "css.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 "ipl.h"
29 #include "hw/s390x/s390-virtio-ccw.h"
30 
31 static const char *const reset_dev_types[] = {
32     "virtual-css-bridge",
33     "s390-sclp-event-facility",
34     "s390-flic",
35     "diag288",
36 };
37 
38 void subsystem_reset(void)
39 {
40     DeviceState *dev;
41     int i;
42 
43     for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
44         dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
45         if (dev) {
46             qdev_reset_all(dev);
47         }
48     }
49 }
50 
51 static int virtio_ccw_hcall_notify(const uint64_t *args)
52 {
53     uint64_t subch_id = args[0];
54     uint64_t queue = args[1];
55     SubchDev *sch;
56     int cssid, ssid, schid, m;
57 
58     if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
59         return -EINVAL;
60     }
61     sch = css_find_subch(m, cssid, ssid, schid);
62     if (!sch || !css_subch_visible(sch)) {
63         return -EINVAL;
64     }
65     if (queue >= VIRTIO_CCW_QUEUE_MAX) {
66         return -EINVAL;
67     }
68     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
69     return 0;
70 
71 }
72 
73 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
74 {
75     uint64_t mem = args[0];
76 
77     if (mem < ram_size) {
78         /* Early printk */
79         return 0;
80     }
81     return -EINVAL;
82 }
83 
84 static void virtio_ccw_register_hcalls(void)
85 {
86     s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
87                                    virtio_ccw_hcall_notify);
88     /* Tolerate early printk. */
89     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
90                                    virtio_ccw_hcall_early_printk);
91 }
92 
93 void s390_memory_init(ram_addr_t mem_size)
94 {
95     MemoryRegion *sysmem = get_system_memory();
96     MemoryRegion *ram = g_new(MemoryRegion, 1);
97 
98     /* allocate RAM for core */
99     memory_region_allocate_system_memory(ram, NULL, "s390.ram", mem_size);
100     memory_region_add_subregion(sysmem, 0, ram);
101 
102     /* Initialize storage key device */
103     s390_skeys_init();
104 }
105 
106 static void ccw_init(MachineState *machine)
107 {
108     int ret;
109     VirtualCssBus *css_bus;
110     DeviceState *dev;
111 
112     s390_sclp_init();
113     s390_memory_init(machine->ram_size);
114 
115     /* get a BUS */
116     css_bus = virtual_css_bus_init();
117     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
118                       machine->initrd_filename, "s390-ccw.img", true);
119     s390_flic_init();
120 
121     dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
122     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
123                               OBJECT(dev), NULL);
124     qdev_init_nofail(dev);
125 
126     /* register hypercalls */
127     virtio_ccw_register_hcalls();
128 
129     /* init CPUs */
130     s390_init_cpus(machine);
131 
132     if (kvm_enabled()) {
133         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
134     }
135     /*
136      * Create virtual css and set it as default so that non mcss-e
137      * enabled guests only see virtio devices.
138      */
139     ret = css_create_css_image(VIRTUAL_CSSID, true);
140     assert(ret == 0);
141 
142     /* Create VirtIO network adapters */
143     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
144 
145     /* Register savevm handler for guest TOD clock */
146     register_savevm(NULL, "todclock", 0, 1,
147                     gtod_save, gtod_load, kvm_state);
148 }
149 
150 static void s390_cpu_plug(HotplugHandler *hotplug_dev,
151                         DeviceState *dev, Error **errp)
152 {
153     gchar *name;
154     S390CPU *cpu = S390_CPU(dev);
155     CPUState *cs = CPU(dev);
156 
157     name = g_strdup_printf("cpu[%i]", cpu->env.cpu_num);
158     object_property_set_link(OBJECT(hotplug_dev), OBJECT(cs), name,
159                              errp);
160     g_free(name);
161 }
162 
163 static void s390_machine_device_plug(HotplugHandler *hotplug_dev,
164                                      DeviceState *dev, Error **errp)
165 {
166     if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
167         s390_cpu_plug(hotplug_dev, dev, errp);
168     }
169 }
170 
171 static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
172                                                 DeviceState *dev)
173 {
174     if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
175         return HOTPLUG_HANDLER(machine);
176     }
177     return NULL;
178 }
179 
180 static void s390_hot_add_cpu(const int64_t id, Error **errp)
181 {
182     MachineState *machine = MACHINE(qdev_get_machine());
183     Error *err = NULL;
184 
185     s390x_new_cpu(machine->cpu_model, id, &err);
186     error_propagate(errp, err);
187 }
188 
189 static void ccw_machine_class_init(ObjectClass *oc, void *data)
190 {
191     MachineClass *mc = MACHINE_CLASS(oc);
192     NMIClass *nc = NMI_CLASS(oc);
193     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
194     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
195 
196     s390mc->ri_allowed = true;
197     mc->init = ccw_init;
198     mc->reset = s390_machine_reset;
199     mc->hot_add_cpu = s390_hot_add_cpu;
200     mc->block_default_type = IF_VIRTIO;
201     mc->no_cdrom = 1;
202     mc->no_floppy = 1;
203     mc->no_serial = 1;
204     mc->no_parallel = 1;
205     mc->no_sdcard = 1;
206     mc->use_sclp = 1;
207     mc->max_cpus = 255;
208     mc->get_hotplug_handler = s390_get_hotplug_handler;
209     hc->plug = s390_machine_device_plug;
210     nc->nmi_monitor_handler = s390_nmi;
211 }
212 
213 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
214 {
215     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
216 
217     return ms->aes_key_wrap;
218 }
219 
220 static inline void machine_set_aes_key_wrap(Object *obj, bool value,
221                                             Error **errp)
222 {
223     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
224 
225     ms->aes_key_wrap = value;
226 }
227 
228 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
229 {
230     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
231 
232     return ms->dea_key_wrap;
233 }
234 
235 static inline void machine_set_dea_key_wrap(Object *obj, bool value,
236                                             Error **errp)
237 {
238     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
239 
240     ms->dea_key_wrap = value;
241 }
242 
243 bool ri_allowed(void)
244 {
245     if (kvm_enabled()) {
246         MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
247         if (object_class_dynamic_cast(OBJECT_CLASS(mc),
248                                       TYPE_S390_CCW_MACHINE)) {
249             S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
250 
251             return s390mc->ri_allowed;
252         }
253     }
254     return 0;
255 }
256 
257 static inline void s390_machine_initfn(Object *obj)
258 {
259     object_property_add_bool(obj, "aes-key-wrap",
260                              machine_get_aes_key_wrap,
261                              machine_set_aes_key_wrap, NULL);
262     object_property_set_description(obj, "aes-key-wrap",
263             "enable/disable AES key wrapping using the CPACF wrapping key",
264             NULL);
265     object_property_set_bool(obj, true, "aes-key-wrap", NULL);
266 
267     object_property_add_bool(obj, "dea-key-wrap",
268                              machine_get_dea_key_wrap,
269                              machine_set_dea_key_wrap, NULL);
270     object_property_set_description(obj, "dea-key-wrap",
271             "enable/disable DEA key wrapping using the CPACF wrapping key",
272             NULL);
273     object_property_set_bool(obj, true, "dea-key-wrap", NULL);
274 }
275 
276 static const TypeInfo ccw_machine_info = {
277     .name          = TYPE_S390_CCW_MACHINE,
278     .parent        = TYPE_MACHINE,
279     .abstract      = true,
280     .instance_size = sizeof(S390CcwMachineState),
281     .instance_init = s390_machine_initfn,
282     .class_size = sizeof(S390CcwMachineClass),
283     .class_init    = ccw_machine_class_init,
284     .interfaces = (InterfaceInfo[]) {
285         { TYPE_NMI },
286         { TYPE_HOTPLUG_HANDLER},
287         { }
288     },
289 };
290 
291 #define DEFINE_CCW_MACHINE(suffix, verstr, latest)                            \
292     static void ccw_machine_##suffix##_class_init(ObjectClass *oc,            \
293                                                   void *data)                 \
294     {                                                                         \
295         MachineClass *mc = MACHINE_CLASS(oc);                                 \
296         ccw_machine_##suffix##_class_options(mc);                             \
297         mc->desc = "VirtIO-ccw based S390 machine v" verstr;                  \
298         if (latest) {                                                         \
299             mc->alias = "s390-ccw-virtio";                                    \
300             mc->is_default = 1;                                               \
301         }                                                                     \
302     }                                                                         \
303     static void ccw_machine_##suffix##_instance_init(Object *obj)             \
304     {                                                                         \
305         MachineState *machine = MACHINE(obj);                                 \
306         ccw_machine_##suffix##_instance_options(machine);                     \
307     }                                                                         \
308     static const TypeInfo ccw_machine_##suffix##_info = {                     \
309         .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr),                 \
310         .parent = TYPE_S390_CCW_MACHINE,                                      \
311         .class_init = ccw_machine_##suffix##_class_init,                      \
312         .instance_init = ccw_machine_##suffix##_instance_init,                \
313     };                                                                        \
314     static void ccw_machine_register_##suffix(void)                           \
315     {                                                                         \
316         type_register_static(&ccw_machine_##suffix##_info);                   \
317     }                                                                         \
318     type_init(ccw_machine_register_##suffix)
319 
320 #define CCW_COMPAT_2_6 \
321         HW_COMPAT_2_6 \
322         {\
323             .driver   = TYPE_S390_IPL,\
324             .property = "iplbext_migration",\
325             .value    = "off",\
326         },
327 
328 #define CCW_COMPAT_2_5 \
329         CCW_COMPAT_2_6 \
330         HW_COMPAT_2_5
331 
332 #define CCW_COMPAT_2_4 \
333         CCW_COMPAT_2_5 \
334         HW_COMPAT_2_4 \
335         {\
336             .driver   = TYPE_S390_SKEYS,\
337             .property = "migration-enabled",\
338             .value    = "off",\
339         },{\
340             .driver   = "virtio-blk-ccw",\
341             .property = "max_revision",\
342             .value    = "0",\
343         },{\
344             .driver   = "virtio-balloon-ccw",\
345             .property = "max_revision",\
346             .value    = "0",\
347         },{\
348             .driver   = "virtio-serial-ccw",\
349             .property = "max_revision",\
350             .value    = "0",\
351         },{\
352             .driver   = "virtio-9p-ccw",\
353             .property = "max_revision",\
354             .value    = "0",\
355         },{\
356             .driver   = "virtio-rng-ccw",\
357             .property = "max_revision",\
358             .value    = "0",\
359         },{\
360             .driver   = "virtio-net-ccw",\
361             .property = "max_revision",\
362             .value    = "0",\
363         },{\
364             .driver   = "virtio-scsi-ccw",\
365             .property = "max_revision",\
366             .value    = "0",\
367         },{\
368             .driver   = "vhost-scsi-ccw",\
369             .property = "max_revision",\
370             .value    = "0",\
371         },
372 
373 static void ccw_machine_2_7_instance_options(MachineState *machine)
374 {
375 }
376 
377 static void ccw_machine_2_7_class_options(MachineClass *mc)
378 {
379 }
380 DEFINE_CCW_MACHINE(2_7, "2.7", true);
381 
382 static void ccw_machine_2_6_instance_options(MachineState *machine)
383 {
384     ccw_machine_2_7_instance_options(machine);
385 }
386 
387 static void ccw_machine_2_6_class_options(MachineClass *mc)
388 {
389     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
390 
391     s390mc->ri_allowed = false;
392     ccw_machine_2_7_class_options(mc);
393     SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_6);
394 }
395 DEFINE_CCW_MACHINE(2_6, "2.6", false);
396 
397 static void ccw_machine_2_5_instance_options(MachineState *machine)
398 {
399     ccw_machine_2_6_instance_options(machine);
400 }
401 
402 static void ccw_machine_2_5_class_options(MachineClass *mc)
403 {
404     ccw_machine_2_6_class_options(mc);
405     SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_5);
406 }
407 DEFINE_CCW_MACHINE(2_5, "2.5", false);
408 
409 static void ccw_machine_2_4_instance_options(MachineState *machine)
410 {
411     ccw_machine_2_5_instance_options(machine);
412 }
413 
414 static void ccw_machine_2_4_class_options(MachineClass *mc)
415 {
416     ccw_machine_2_5_class_options(mc);
417     SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_4);
418 }
419 DEFINE_CCW_MACHINE(2_4, "2.4", false);
420 
421 static void ccw_machine_register_types(void)
422 {
423     type_register_static(&ccw_machine_info);
424 }
425 
426 type_init(ccw_machine_register_types)
427