xref: /openbmc/qemu/hw/s390x/s390-virtio-ccw.c (revision f1f7e4bf)
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 "hw/boards.h"
13 #include "exec/address-spaces.h"
14 #include "s390-virtio.h"
15 #include "hw/s390x/sclp.h"
16 #include "hw/s390x/s390_flic.h"
17 #include "ioinst.h"
18 #include "css.h"
19 #include "virtio-ccw.h"
20 #include "qemu/config-file.h"
21 #include "s390-pci-bus.h"
22 #include "hw/s390x/storage-keys.h"
23 #include "hw/compat.h"
24 
25 #define TYPE_S390_CCW_MACHINE               "s390-ccw-machine"
26 
27 #define S390_CCW_MACHINE(obj) \
28     OBJECT_CHECK(S390CcwMachineState, (obj), TYPE_S390_CCW_MACHINE)
29 
30 typedef struct S390CcwMachineState {
31     /*< private >*/
32     MachineState parent_obj;
33 
34     /*< public >*/
35     bool aes_key_wrap;
36     bool dea_key_wrap;
37 } S390CcwMachineState;
38 
39 static const char *const reset_dev_types[] = {
40     "virtual-css-bridge",
41     "s390-sclp-event-facility",
42     "s390-flic",
43     "diag288",
44 };
45 
46 void subsystem_reset(void)
47 {
48     DeviceState *dev;
49     int i;
50 
51     for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
52         dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
53         if (dev) {
54             qdev_reset_all(dev);
55         }
56     }
57 }
58 
59 static int virtio_ccw_hcall_notify(const uint64_t *args)
60 {
61     uint64_t subch_id = args[0];
62     uint64_t queue = args[1];
63     SubchDev *sch;
64     int cssid, ssid, schid, m;
65 
66     if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
67         return -EINVAL;
68     }
69     sch = css_find_subch(m, cssid, ssid, schid);
70     if (!sch || !css_subch_visible(sch)) {
71         return -EINVAL;
72     }
73     if (queue >= VIRTIO_CCW_QUEUE_MAX) {
74         return -EINVAL;
75     }
76     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
77     return 0;
78 
79 }
80 
81 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
82 {
83     uint64_t mem = args[0];
84 
85     if (mem < ram_size) {
86         /* Early printk */
87         return 0;
88     }
89     return -EINVAL;
90 }
91 
92 static void virtio_ccw_register_hcalls(void)
93 {
94     s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
95                                    virtio_ccw_hcall_notify);
96     /* Tolerate early printk. */
97     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
98                                    virtio_ccw_hcall_early_printk);
99 }
100 
101 void s390_memory_init(ram_addr_t mem_size)
102 {
103     MemoryRegion *sysmem = get_system_memory();
104     MemoryRegion *ram = g_new(MemoryRegion, 1);
105 
106     /* allocate RAM for core */
107     memory_region_allocate_system_memory(ram, NULL, "s390.ram", mem_size);
108     memory_region_add_subregion(sysmem, 0, ram);
109 
110     /* Initialize storage key device */
111     s390_skeys_init();
112 }
113 
114 static void ccw_init(MachineState *machine)
115 {
116     int ret;
117     VirtualCssBus *css_bus;
118     DeviceState *dev;
119 
120     s390_sclp_init();
121     s390_memory_init(machine->ram_size);
122 
123     /* get a BUS */
124     css_bus = virtual_css_bus_init();
125     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
126                       machine->initrd_filename, "s390-ccw.img", true);
127     s390_flic_init();
128 
129     dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
130     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
131                               OBJECT(dev), NULL);
132     qdev_init_nofail(dev);
133 
134     /* register hypercalls */
135     virtio_ccw_register_hcalls();
136 
137     /* init CPUs */
138     s390_init_cpus(machine->cpu_model);
139 
140     if (kvm_enabled()) {
141         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
142     }
143     /*
144      * Create virtual css and set it as default so that non mcss-e
145      * enabled guests only see virtio devices.
146      */
147     ret = css_create_css_image(VIRTUAL_CSSID, true);
148     assert(ret == 0);
149 
150     /* Create VirtIO network adapters */
151     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
152 
153     /* Register savevm handler for guest TOD clock */
154     register_savevm(NULL, "todclock", 0, 1,
155                     gtod_save, gtod_load, kvm_state);
156 }
157 
158 static void ccw_machine_class_init(ObjectClass *oc, void *data)
159 {
160     MachineClass *mc = MACHINE_CLASS(oc);
161     NMIClass *nc = NMI_CLASS(oc);
162 
163     mc->init = ccw_init;
164     mc->reset = s390_machine_reset;
165     mc->block_default_type = IF_VIRTIO;
166     mc->no_cdrom = 1;
167     mc->no_floppy = 1;
168     mc->no_serial = 1;
169     mc->no_parallel = 1;
170     mc->no_sdcard = 1;
171     mc->use_sclp = 1;
172     mc->max_cpus = 255;
173     nc->nmi_monitor_handler = s390_nmi;
174 }
175 
176 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
177 {
178     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
179 
180     return ms->aes_key_wrap;
181 }
182 
183 static inline void machine_set_aes_key_wrap(Object *obj, bool value,
184                                             Error **errp)
185 {
186     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
187 
188     ms->aes_key_wrap = value;
189 }
190 
191 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
192 {
193     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
194 
195     return ms->dea_key_wrap;
196 }
197 
198 static inline void machine_set_dea_key_wrap(Object *obj, bool value,
199                                             Error **errp)
200 {
201     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
202 
203     ms->dea_key_wrap = value;
204 }
205 
206 static inline void s390_machine_initfn(Object *obj)
207 {
208     object_property_add_bool(obj, "aes-key-wrap",
209                              machine_get_aes_key_wrap,
210                              machine_set_aes_key_wrap, NULL);
211     object_property_set_description(obj, "aes-key-wrap",
212             "enable/disable AES key wrapping using the CPACF wrapping key",
213             NULL);
214     object_property_set_bool(obj, true, "aes-key-wrap", NULL);
215 
216     object_property_add_bool(obj, "dea-key-wrap",
217                              machine_get_dea_key_wrap,
218                              machine_set_dea_key_wrap, NULL);
219     object_property_set_description(obj, "dea-key-wrap",
220             "enable/disable DEA key wrapping using the CPACF wrapping key",
221             NULL);
222     object_property_set_bool(obj, true, "dea-key-wrap", NULL);
223 }
224 
225 static const TypeInfo ccw_machine_info = {
226     .name          = TYPE_S390_CCW_MACHINE,
227     .parent        = TYPE_MACHINE,
228     .abstract      = true,
229     .instance_size = sizeof(S390CcwMachineState),
230     .instance_init = s390_machine_initfn,
231     .class_init    = ccw_machine_class_init,
232     .interfaces = (InterfaceInfo[]) {
233         { TYPE_NMI },
234         { }
235     },
236 };
237 
238 #define CCW_COMPAT_2_4 \
239         HW_COMPAT_2_4 \
240         {\
241             .driver   = TYPE_S390_SKEYS,\
242             .property = "migration-enabled",\
243             .value    = "off",\
244         },{\
245             .driver   = "virtio-blk-ccw",\
246             .property = "max_revision",\
247             .value    = "0",\
248         },{\
249             .driver   = "virtio-balloon-ccw",\
250             .property = "max_revision",\
251             .value    = "0",\
252         },{\
253             .driver   = "virtio-serial-ccw",\
254             .property = "max_revision",\
255             .value    = "0",\
256         },{\
257             .driver   = "virtio-9p-ccw",\
258             .property = "max_revision",\
259             .value    = "0",\
260         },{\
261             .driver   = "virtio-rng-ccw",\
262             .property = "max_revision",\
263             .value    = "0",\
264         },{\
265             .driver   = "virtio-net-ccw",\
266             .property = "max_revision",\
267             .value    = "0",\
268         },{\
269             .driver   = "virtio-scsi-ccw",\
270             .property = "max_revision",\
271             .value    = "0",\
272         },{\
273             .driver   = "vhost-scsi-ccw",\
274             .property = "max_revision",\
275             .value    = "0",\
276         },
277 
278 static void ccw_machine_2_4_class_init(ObjectClass *oc, void *data)
279 {
280     MachineClass *mc = MACHINE_CLASS(oc);
281     static GlobalProperty compat_props[] = {
282         CCW_COMPAT_2_4
283         { /* end of list */ }
284     };
285 
286     mc->desc = "VirtIO-ccw based S390 machine v2.4";
287     mc->compat_props = compat_props;
288 }
289 
290 static const TypeInfo ccw_machine_2_4_info = {
291     .name          = MACHINE_TYPE_NAME("s390-ccw-virtio-2.4"),
292     .parent        = TYPE_S390_CCW_MACHINE,
293     .class_init    = ccw_machine_2_4_class_init,
294 };
295 
296 static void ccw_machine_2_5_class_init(ObjectClass *oc, void *data)
297 {
298     MachineClass *mc = MACHINE_CLASS(oc);
299 
300     mc->alias = "s390-ccw-virtio";
301     mc->desc = "VirtIO-ccw based S390 machine v2.5";
302     mc->is_default = 1;
303 }
304 
305 static const TypeInfo ccw_machine_2_5_info = {
306     .name          = MACHINE_TYPE_NAME("s390-ccw-virtio-2.5"),
307     .parent        = TYPE_S390_CCW_MACHINE,
308     .class_init    = ccw_machine_2_5_class_init,
309 };
310 
311 static void ccw_machine_register_types(void)
312 {
313     type_register_static(&ccw_machine_info);
314     type_register_static(&ccw_machine_2_4_info);
315     type_register_static(&ccw_machine_2_5_info);
316 }
317 
318 type_init(ccw_machine_register_types)
319