Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / s390x / s390-virtio-ccw.c
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
23 #define TYPE_S390_CCW_MACHINE               "s390-ccw-machine"
24
25 #define S390_CCW_MACHINE(obj) \
26     OBJECT_CHECK(S390CcwMachineState, (obj), TYPE_S390_CCW_MACHINE)
27
28 typedef struct S390CcwMachineState {
29     /*< private >*/
30     MachineState parent_obj;
31
32     /*< public >*/
33     bool aes_key_wrap;
34     bool dea_key_wrap;
35 } S390CcwMachineState;
36
37 void io_subsystem_reset(void)
38 {
39     DeviceState *css, *sclp, *flic, *diag288;
40
41     css = DEVICE(object_resolve_path_type("", "virtual-css-bridge", NULL));
42     if (css) {
43         qdev_reset_all(css);
44     }
45     sclp = DEVICE(object_resolve_path_type("",
46                   "s390-sclp-event-facility", NULL));
47     if (sclp) {
48         qdev_reset_all(sclp);
49     }
50     flic = DEVICE(object_resolve_path_type("", "s390-flic", NULL));
51     if (flic) {
52         qdev_reset_all(flic);
53     }
54     diag288 = DEVICE(object_resolve_path_type("", "diag288", NULL));
55     if (diag288) {
56         qdev_reset_all(diag288);
57     }
58 }
59
60 static int virtio_ccw_hcall_notify(const uint64_t *args)
61 {
62     uint64_t subch_id = args[0];
63     uint64_t queue = args[1];
64     SubchDev *sch;
65     int cssid, ssid, schid, m;
66
67     if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
68         return -EINVAL;
69     }
70     sch = css_find_subch(m, cssid, ssid, schid);
71     if (!sch || !css_subch_visible(sch)) {
72         return -EINVAL;
73     }
74     if (queue >= VIRTIO_CCW_QUEUE_MAX) {
75         return -EINVAL;
76     }
77     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
78     return 0;
79
80 }
81
82 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
83 {
84     uint64_t mem = args[0];
85
86     if (mem < ram_size) {
87         /* Early printk */
88         return 0;
89     }
90     return -EINVAL;
91 }
92
93 static void virtio_ccw_register_hcalls(void)
94 {
95     s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
96                                    virtio_ccw_hcall_notify);
97     /* Tolerate early printk. */
98     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
99                                    virtio_ccw_hcall_early_printk);
100 }
101
102 static void ccw_init(MachineState *machine)
103 {
104     ram_addr_t my_ram_size = machine->ram_size;
105     MemoryRegion *sysmem = get_system_memory();
106     MemoryRegion *ram = g_new(MemoryRegion, 1);
107     sclpMemoryHotplugDev *mhd = init_sclp_memory_hotplug_dev();
108     uint8_t *storage_keys;
109     int ret;
110     VirtualCssBus *css_bus;
111     DeviceState *dev;
112     QemuOpts *opts = qemu_opts_find(qemu_find_opts("memory"), NULL);
113     ram_addr_t pad_size = 0;
114     ram_addr_t maxmem = qemu_opt_get_size(opts, "maxmem", my_ram_size);
115     ram_addr_t standby_mem_size = maxmem - my_ram_size;
116     uint64_t kvm_limit;
117
118     /* The storage increment size is a multiple of 1M and is a power of 2.
119      * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer.
120      * The variable 'mhd->increment_size' is an exponent of 2 that can be
121      * used to calculate the size (in bytes) of an increment. */
122     mhd->increment_size = 20;
123     while ((my_ram_size >> mhd->increment_size) > MAX_STORAGE_INCREMENTS) {
124         mhd->increment_size++;
125     }
126     while ((standby_mem_size >> mhd->increment_size) > MAX_STORAGE_INCREMENTS) {
127         mhd->increment_size++;
128     }
129
130     /* The core and standby memory areas need to be aligned with
131      * the increment size.  In effect, this can cause the
132      * user-specified memory size to be rounded down to align
133      * with the nearest increment boundary. */
134     standby_mem_size = standby_mem_size >> mhd->increment_size
135                                         << mhd->increment_size;
136     my_ram_size = my_ram_size >> mhd->increment_size
137                               << mhd->increment_size;
138
139     /* let's propagate the changed ram size into the global variable. */
140     ram_size = my_ram_size;
141     machine->maxram_size = my_ram_size + standby_mem_size;
142
143     ret = s390_set_memory_limit(machine->maxram_size, &kvm_limit);
144     if (ret == -E2BIG) {
145         hw_error("qemu: host supports a maximum of %" PRIu64 " GB",
146                  kvm_limit >> 30);
147     } else if (ret) {
148         hw_error("qemu: setting the guest size failed");
149     }
150
151     /* get a BUS */
152     css_bus = virtual_css_bus_init();
153     s390_sclp_init();
154     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
155                       machine->initrd_filename, "s390-ccw.img", true);
156     s390_flic_init();
157
158     dev = qdev_create(NULL, TYPE_S390_PCI_HOST_BRIDGE);
159     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
160                               OBJECT(dev), NULL);
161     qdev_init_nofail(dev);
162
163     /* register hypercalls */
164     virtio_ccw_register_hcalls();
165
166     /* allocate RAM for core */
167     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size, &error_abort);
168     vmstate_register_ram_global(ram);
169     memory_region_add_subregion(sysmem, 0, ram);
170
171     /* If the size of ram is not on a MEM_SECTION_SIZE boundary,
172        calculate the pad size necessary to force this boundary. */
173     if (standby_mem_size) {
174         if (my_ram_size % MEM_SECTION_SIZE) {
175             pad_size = MEM_SECTION_SIZE - my_ram_size % MEM_SECTION_SIZE;
176         }
177         my_ram_size += standby_mem_size + pad_size;
178         mhd->pad_size = pad_size;
179         mhd->standby_mem_size = standby_mem_size;
180     }
181
182     /* allocate storage keys */
183     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
184
185     /* init CPUs */
186     s390_init_cpus(machine->cpu_model, storage_keys);
187
188     if (kvm_enabled()) {
189         kvm_s390_enable_css_support(s390_cpu_addr2state(0));
190     }
191     /*
192      * Create virtual css and set it as default so that non mcss-e
193      * enabled guests only see virtio devices.
194      */
195     ret = css_create_css_image(VIRTUAL_CSSID, true);
196     assert(ret == 0);
197
198     /* Create VirtIO network adapters */
199     s390_create_virtio_net(BUS(css_bus), "virtio-net-ccw");
200
201     /* Register savevm handler for guest TOD clock */
202     register_savevm(NULL, "todclock", 0, 1,
203                     gtod_save, gtod_load, kvm_state);
204 }
205
206 static void ccw_machine_class_init(ObjectClass *oc, void *data)
207 {
208     MachineClass *mc = MACHINE_CLASS(oc);
209     NMIClass *nc = NMI_CLASS(oc);
210
211     mc->init = ccw_init;
212     mc->block_default_type = IF_VIRTIO;
213     mc->no_cdrom = 1;
214     mc->no_floppy = 1;
215     mc->no_serial = 1;
216     mc->no_parallel = 1;
217     mc->no_sdcard = 1;
218     mc->use_sclp = 1;
219     mc->max_cpus = 255;
220     nc->nmi_monitor_handler = s390_nmi;
221 }
222
223 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp)
224 {
225     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
226
227     return ms->aes_key_wrap;
228 }
229
230 static inline void machine_set_aes_key_wrap(Object *obj, bool value,
231                                             Error **errp)
232 {
233     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
234
235     ms->aes_key_wrap = value;
236 }
237
238 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp)
239 {
240     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
241
242     return ms->dea_key_wrap;
243 }
244
245 static inline void machine_set_dea_key_wrap(Object *obj, bool value,
246                                             Error **errp)
247 {
248     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
249
250     ms->dea_key_wrap = value;
251 }
252
253 static inline void s390_machine_initfn(Object *obj)
254 {
255     object_property_add_bool(obj, "aes-key-wrap",
256                              machine_get_aes_key_wrap,
257                              machine_set_aes_key_wrap, NULL);
258     object_property_set_description(obj, "aes-key-wrap",
259             "enable/disable AES key wrapping using the CPACF wrapping key",
260             NULL);
261     object_property_set_bool(obj, true, "aes-key-wrap", NULL);
262
263     object_property_add_bool(obj, "dea-key-wrap",
264                              machine_get_dea_key_wrap,
265                              machine_set_dea_key_wrap, NULL);
266     object_property_set_description(obj, "dea-key-wrap",
267             "enable/disable DEA key wrapping using the CPACF wrapping key",
268             NULL);
269     object_property_set_bool(obj, true, "dea-key-wrap", NULL);
270 }
271
272 static const TypeInfo ccw_machine_info = {
273     .name          = TYPE_S390_CCW_MACHINE,
274     .parent        = TYPE_MACHINE,
275     .abstract      = true,
276     .instance_size = sizeof(S390CcwMachineState),
277     .instance_init = s390_machine_initfn,
278     .class_init    = ccw_machine_class_init,
279     .interfaces = (InterfaceInfo[]) {
280         { TYPE_NMI },
281         { }
282     },
283 };
284
285 static void ccw_machine_2_4_class_init(ObjectClass *oc, void *data)
286 {
287     MachineClass *mc = MACHINE_CLASS(oc);
288
289     mc->name = "s390-ccw-virtio-2.4";
290     mc->alias = "s390-ccw-virtio";
291     mc->desc = "VirtIO-ccw based S390 machine v2.4";
292     mc->is_default = 1;
293 }
294
295 static const TypeInfo ccw_machine_2_4_info = {
296     .name          = TYPE_S390_CCW_MACHINE "2.4",
297     .parent        = TYPE_S390_CCW_MACHINE,
298     .class_init    = ccw_machine_2_4_class_init,
299 };
300
301 static void ccw_machine_register_types(void)
302 {
303     type_register_static(&ccw_machine_info);
304     type_register_static(&ccw_machine_2_4_info);
305 }
306
307 type_init(ccw_machine_register_types)