xref: /qemu/hw/s390x/s390-virtio-ccw.c (revision bf8d4924)
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 "hw/s390x/ioinst.h"
22 #include "hw/s390x/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 
184     s390x_new_cpu(machine->cpu_model, id, errp);
185 }
186 
187 static void ccw_machine_class_init(ObjectClass *oc, void *data)
188 {
189     MachineClass *mc = MACHINE_CLASS(oc);
190     NMIClass *nc = NMI_CLASS(oc);
191     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
192     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
193 
194     s390mc->ri_allowed = true;
195     mc->init = ccw_init;
196     mc->reset = s390_machine_reset;
197     mc->hot_add_cpu = s390_hot_add_cpu;
198     mc->block_default_type = IF_VIRTIO;
199     mc->no_cdrom = 1;
200     mc->no_floppy = 1;
201     mc->no_serial = 1;
202     mc->no_parallel = 1;
203     mc->no_sdcard = 1;
204     mc->use_sclp = 1;
205     mc->max_cpus = 248;
206     mc->get_hotplug_handler = s390_get_hotplug_handler;
207     hc->plug = s390_machine_device_plug;
208     nc->nmi_monitor_handler = s390_nmi;
209 }
210 
211 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
212 {
213     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
214 
215     return ms->aes_key_wrap;
216 }
217 
218 static inline void machine_set_aes_key_wrap(Object *obj, bool value,
219                                             Error **errp)
220 {
221     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
222 
223     ms->aes_key_wrap = value;
224 }
225 
226 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
227 {
228     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
229 
230     return ms->dea_key_wrap;
231 }
232 
233 static inline void machine_set_dea_key_wrap(Object *obj, bool value,
234                                             Error **errp)
235 {
236     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
237 
238     ms->dea_key_wrap = value;
239 }
240 
241 bool ri_allowed(void)
242 {
243     if (kvm_enabled()) {
244         MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
245         if (object_class_dynamic_cast(OBJECT_CLASS(mc),
246                                       TYPE_S390_CCW_MACHINE)) {
247             S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
248 
249             return s390mc->ri_allowed;
250         }
251     }
252     return 0;
253 }
254 
255 static inline void s390_machine_initfn(Object *obj)
256 {
257     object_property_add_bool(obj, "aes-key-wrap",
258                              machine_get_aes_key_wrap,
259                              machine_set_aes_key_wrap, NULL);
260     object_property_set_description(obj, "aes-key-wrap",
261             "enable/disable AES key wrapping using the CPACF wrapping key",
262             NULL);
263     object_property_set_bool(obj, true, "aes-key-wrap", NULL);
264 
265     object_property_add_bool(obj, "dea-key-wrap",
266                              machine_get_dea_key_wrap,
267                              machine_set_dea_key_wrap, NULL);
268     object_property_set_description(obj, "dea-key-wrap",
269             "enable/disable DEA key wrapping using the CPACF wrapping key",
270             NULL);
271     object_property_set_bool(obj, true, "dea-key-wrap", NULL);
272 }
273 
274 static const TypeInfo ccw_machine_info = {
275     .name          = TYPE_S390_CCW_MACHINE,
276     .parent        = TYPE_MACHINE,
277     .abstract      = true,
278     .instance_size = sizeof(S390CcwMachineState),
279     .instance_init = s390_machine_initfn,
280     .class_size = sizeof(S390CcwMachineClass),
281     .class_init    = ccw_machine_class_init,
282     .interfaces = (InterfaceInfo[]) {
283         { TYPE_NMI },
284         { TYPE_HOTPLUG_HANDLER},
285         { }
286     },
287 };
288 
289 #define DEFINE_CCW_MACHINE(suffix, verstr, latest)                            \
290     static void ccw_machine_##suffix##_class_init(ObjectClass *oc,            \
291                                                   void *data)                 \
292     {                                                                         \
293         MachineClass *mc = MACHINE_CLASS(oc);                                 \
294         ccw_machine_##suffix##_class_options(mc);                             \
295         mc->desc = "VirtIO-ccw based S390 machine v" verstr;                  \
296         if (latest) {                                                         \
297             mc->alias = "s390-ccw-virtio";                                    \
298             mc->is_default = 1;                                               \
299         }                                                                     \
300     }                                                                         \
301     static void ccw_machine_##suffix##_instance_init(Object *obj)             \
302     {                                                                         \
303         MachineState *machine = MACHINE(obj);                                 \
304         ccw_machine_##suffix##_instance_options(machine);                     \
305     }                                                                         \
306     static const TypeInfo ccw_machine_##suffix##_info = {                     \
307         .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr),                 \
308         .parent = TYPE_S390_CCW_MACHINE,                                      \
309         .class_init = ccw_machine_##suffix##_class_init,                      \
310         .instance_init = ccw_machine_##suffix##_instance_init,                \
311     };                                                                        \
312     static void ccw_machine_register_##suffix(void)                           \
313     {                                                                         \
314         type_register_static(&ccw_machine_##suffix##_info);                   \
315     }                                                                         \
316     type_init(ccw_machine_register_##suffix)
317 
318 #define CCW_COMPAT_2_6 \
319         HW_COMPAT_2_6 \
320         {\
321             .driver   = TYPE_S390_IPL,\
322             .property = "iplbext_migration",\
323             .value    = "off",\
324         },
325 
326 #define CCW_COMPAT_2_5 \
327         CCW_COMPAT_2_6 \
328         HW_COMPAT_2_5
329 
330 #define CCW_COMPAT_2_4 \
331         HW_COMPAT_2_4 \
332         {\
333             .driver   = TYPE_S390_SKEYS,\
334             .property = "migration-enabled",\
335             .value    = "off",\
336         },{\
337             .driver   = "virtio-blk-ccw",\
338             .property = "max_revision",\
339             .value    = "0",\
340         },{\
341             .driver   = "virtio-balloon-ccw",\
342             .property = "max_revision",\
343             .value    = "0",\
344         },{\
345             .driver   = "virtio-serial-ccw",\
346             .property = "max_revision",\
347             .value    = "0",\
348         },{\
349             .driver   = "virtio-9p-ccw",\
350             .property = "max_revision",\
351             .value    = "0",\
352         },{\
353             .driver   = "virtio-rng-ccw",\
354             .property = "max_revision",\
355             .value    = "0",\
356         },{\
357             .driver   = "virtio-net-ccw",\
358             .property = "max_revision",\
359             .value    = "0",\
360         },{\
361             .driver   = "virtio-scsi-ccw",\
362             .property = "max_revision",\
363             .value    = "0",\
364         },{\
365             .driver   = "vhost-scsi-ccw",\
366             .property = "max_revision",\
367             .value    = "0",\
368         },
369 
370 static void ccw_machine_2_7_instance_options(MachineState *machine)
371 {
372 }
373 
374 static void ccw_machine_2_7_class_options(MachineClass *mc)
375 {
376 }
377 DEFINE_CCW_MACHINE(2_7, "2.7", true);
378 
379 static void ccw_machine_2_6_instance_options(MachineState *machine)
380 {
381     ccw_machine_2_7_instance_options(machine);
382 }
383 
384 static void ccw_machine_2_6_class_options(MachineClass *mc)
385 {
386     S390CcwMachineClass *s390mc = S390_MACHINE_CLASS(mc);
387 
388     s390mc->ri_allowed = false;
389     ccw_machine_2_7_class_options(mc);
390     SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_6);
391 }
392 DEFINE_CCW_MACHINE(2_6, "2.6", false);
393 
394 static void ccw_machine_2_5_instance_options(MachineState *machine)
395 {
396     ccw_machine_2_6_instance_options(machine);
397 }
398 
399 static void ccw_machine_2_5_class_options(MachineClass *mc)
400 {
401     ccw_machine_2_6_class_options(mc);
402     SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_5);
403 }
404 DEFINE_CCW_MACHINE(2_5, "2.5", false);
405 
406 static void ccw_machine_2_4_instance_options(MachineState *machine)
407 {
408     ccw_machine_2_5_instance_options(machine);
409 }
410 
411 static void ccw_machine_2_4_class_options(MachineClass *mc)
412 {
413     ccw_machine_2_5_class_options(mc);
414     SET_MACHINE_COMPAT(mc, CCW_COMPAT_2_4);
415 }
416 DEFINE_CCW_MACHINE(2_4, "2.4", false);
417 
418 static void ccw_machine_register_types(void)
419 {
420     type_register_static(&ccw_machine_info);
421 }
422 
423 type_init(ccw_machine_register_types)
424