]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390x/s390-virtio.c
s390x: no deprecation warning while testing
[mirror_qemu.git] / hw / s390x / s390-virtio.c
CommitLineData
8cb310e1
AG
1/*
2 * QEMU S390 virtio target
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
904e5fd5 5 * Copyright IBM Corp 2012
8cb310e1
AG
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
904e5fd5
VM
17 * Contributions after 2012-10-29 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
19 *
20 * You should have received a copy of the GNU (Lesser) General Public
8cb310e1
AG
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
b73d3531 24#include "hw/hw.h"
cc7a8ea7 25#include "qapi/qmp/qerror.h"
2998ffee 26#include "qemu/error-report.h"
4be74634 27#include "sysemu/block-backend.h"
9c17d615
PB
28#include "sysemu/blockdev.h"
29#include "sysemu/sysemu.h"
1422e32d 30#include "net/net.h"
b73d3531 31#include "hw/boards.h"
b73d3531 32#include "hw/loader.h"
0d09e41a 33#include "hw/virtio/virtio.h"
9c17d615 34#include "sysemu/kvm.h"
022c62cb 35#include "exec/address-spaces.h"
567c88c3 36#include "sysemu/qtest.h"
8cb310e1 37
b73d3531 38#include "hw/s390x/s390-virtio-bus.h"
559a17a1 39#include "hw/s390x/sclp.h"
3a553fc6 40#include "hw/s390x/s390_flic.h"
b73d3531 41#include "hw/s390x/s390-virtio.h"
0f5f6691 42#include "hw/s390x/storage-keys.h"
db3b2566 43#include "hw/s390x/ipl.h"
3f9e59bb 44#include "cpu.h"
8cb310e1
AG
45
46//#define DEBUG_S390
47
48#ifdef DEBUG_S390
e67137c6 49#define DPRINTF(fmt, ...) \
8cb310e1
AG
50 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51#else
e67137c6 52#define DPRINTF(fmt, ...) \
8cb310e1
AG
53 do { } while (0)
54#endif
55
8cb310e1 56#define MAX_BLK_DEVS 10
d0249ce5 57#define ZIPL_FILENAME "s390-zipl.rom"
4c264d4b
EH
58#define S390_MACHINE "s390-virtio"
59#define TYPE_S390_MACHINE MACHINE_TYPE_NAME(S390_MACHINE)
8cb310e1 60
3f9e59bb
JH
61#define S390_TOD_CLOCK_VALUE_MISSING 0x00
62#define S390_TOD_CLOCK_VALUE_PRESENT 0x01
63
8cb310e1 64static VirtIOS390Bus *s390_bus;
45fa769b 65static S390CPU **ipi_states;
8cb310e1 66
45fa769b 67S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
8cb310e1
AG
68{
69 if (cpu_addr >= smp_cpus) {
70 return NULL;
71 }
72
73 return ipi_states[cpu_addr];
74}
75
28e942f8 76static int s390_virtio_hcall_notify(const uint64_t *args)
8cb310e1 77{
28e942f8 78 uint64_t mem = args[0];
8cb310e1 79 int r = 0, i;
8cb310e1 80
28e942f8
CH
81 if (mem > ram_size) {
82 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
8cb310e1 83 if (dev) {
cb927b8a
CB
84 /*
85 * Older kernels will use the virtqueue before setting DRIVER_OK.
86 * In this case the feature bits are not yet up to date, meaning
87 * that several funny things can happen, e.g. the guest thinks
88 * EVENT_IDX is on and QEMU thinks it is off. Let's force a feature
89 * and status sync.
90 */
91 if (!(dev->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
92 s390_virtio_device_update_status(dev);
93 }
28e942f8 94 virtio_queue_notify(dev->vdev, i);
8cb310e1
AG
95 } else {
96 r = -EINVAL;
97 }
28e942f8
CH
98 } else {
99 /* Early printk */
8cb310e1 100 }
28e942f8
CH
101 return r;
102}
103
104static int s390_virtio_hcall_reset(const uint64_t *args)
105{
106 uint64_t mem = args[0];
107 VirtIOS390Device *dev;
108
109 dev = s390_virtio_bus_find_mem(s390_bus, mem);
ab290630
AF
110 if (dev == NULL) {
111 return -EINVAL;
112 }
28e942f8 113 virtio_reset(dev->vdev);
42874d3a
PM
114 address_space_stb(&address_space_memory,
115 dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0,
116 MEMTXATTRS_UNSPECIFIED, NULL);
28e942f8
CH
117 s390_virtio_device_sync(dev);
118 s390_virtio_reset_idx(dev);
119
120 return 0;
121}
122
123static int s390_virtio_hcall_set_status(const uint64_t *args)
124{
125 uint64_t mem = args[0];
126 int r = 0;
127 VirtIOS390Device *dev;
128
129 dev = s390_virtio_bus_find_mem(s390_bus, mem);
130 if (dev) {
131 s390_virtio_device_update_status(dev);
132 } else {
8cb310e1 133 r = -EINVAL;
8cb310e1 134 }
8d5192ee 135 return r;
8cb310e1
AG
136}
137
28e942f8
CH
138static void s390_virtio_register_hcalls(void)
139{
140 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
141 s390_virtio_hcall_notify);
142 s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
143 s390_virtio_hcall_reset);
144 s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
145 s390_virtio_hcall_set_status);
146}
147
fad37673
CH
148void s390_init_ipl_dev(const char *kernel_filename,
149 const char *kernel_cmdline,
d0249ce5 150 const char *initrd_filename,
f0180f91
FZ
151 const char *firmware,
152 bool enforce_bios)
fad37673 153{
04fccf10
DH
154 Object *new = object_new(TYPE_S390_IPL);
155 DeviceState *dev = DEVICE(new);
fad37673 156
fad37673
CH
157 if (kernel_filename) {
158 qdev_prop_set_string(dev, "kernel", kernel_filename);
159 }
160 if (initrd_filename) {
161 qdev_prop_set_string(dev, "initrd", initrd_filename);
162 }
163 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
d0249ce5 164 qdev_prop_set_string(dev, "firmware", firmware);
f0180f91 165 qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
04fccf10
DH
166 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL,
167 new, NULL);
168 object_unref(new);
fad37673
CH
169 qdev_init_nofail(dev);
170}
171
0f5f6691 172void s390_init_cpus(const char *cpu_model)
fad37673
CH
173{
174 int i;
175
176 if (cpu_model == NULL) {
177 cpu_model = "host";
178 }
179
180 ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
181
182 for (i = 0; i < smp_cpus; i++) {
183 S390CPU *cpu;
259186a7 184 CPUState *cs;
fad37673
CH
185
186 cpu = cpu_s390x_init(cpu_model);
259186a7 187 cs = CPU(cpu);
fad37673
CH
188
189 ipi_states[i] = cpu;
259186a7 190 cs->halted = 1;
27103424 191 cs->exception_index = EXCP_HLT;
fad37673
CH
192 }
193}
194
195
196void s390_create_virtio_net(BusState *bus, const char *name)
197{
198 int i;
199
200 for (i = 0; i < nb_nics; i++) {
201 NICInfo *nd = &nd_table[i];
202 DeviceState *dev;
203
204 if (!nd->model) {
205 nd->model = g_strdup("virtio");
206 }
207
208 if (strcmp(nd->model, "virtio")) {
209 fprintf(stderr, "S390 only supports VirtIO nics\n");
210 exit(1);
211 }
212
213 dev = qdev_create(bus, name);
214 qdev_set_nic_properties(dev, nd);
215 qdev_init_nofail(dev);
216 }
217}
218
3f9e59bb
JH
219void gtod_save(QEMUFile *f, void *opaque)
220{
221 uint64_t tod_low;
222 uint8_t tod_high;
223 int r;
224
225 r = s390_get_clock(&tod_high, &tod_low);
226 if (r) {
227 fprintf(stderr, "WARNING: Unable to get guest clock for migration. "
228 "Error code %d. Guest clock will not be migrated "
229 "which could cause the guest to hang.\n", r);
230 qemu_put_byte(f, S390_TOD_CLOCK_VALUE_MISSING);
231 return;
232 }
233
234 qemu_put_byte(f, S390_TOD_CLOCK_VALUE_PRESENT);
235 qemu_put_byte(f, tod_high);
236 qemu_put_be64(f, tod_low);
237}
238
239int gtod_load(QEMUFile *f, void *opaque, int version_id)
240{
241 uint64_t tod_low;
242 uint8_t tod_high;
243 int r;
244
245 if (qemu_get_byte(f) == S390_TOD_CLOCK_VALUE_MISSING) {
246 fprintf(stderr, "WARNING: Guest clock was not migrated. This could "
247 "cause the guest to hang.\n");
248 return 0;
249 }
250
251 tod_high = qemu_get_byte(f);
252 tod_low = qemu_get_be64(f);
253
254 r = s390_set_clock(&tod_high, &tod_low);
255 if (r) {
256 fprintf(stderr, "WARNING: Unable to set guest clock value. "
257 "s390_get_clock returned error %d. This could cause "
258 "the guest to hang.\n", r);
259 }
260
261 return 0;
262}
263
8cb310e1 264/* PC hardware initialisation */
3ef96221 265static void s390_init(MachineState *machine)
8cb310e1 266{
1cf065fb 267 ram_addr_t my_ram_size;
326384d5 268 void *virtio_region;
a8170e5e
AK
269 hwaddr virtio_region_len;
270 hwaddr virtio_region_start;
8cb310e1 271
567c88c3
CH
272 if (!qtest_enabled()) {
273 error_printf("WARNING\n"
274 "The s390-virtio machine (non-ccw) is deprecated.\n"
275 "It will be removed in 2.6. Please use s390-ccw-virtio\n");
276 }
3c4c694c 277
2998ffee
DH
278 if (machine->ram_slots) {
279 error_report("Memory hotplug not supported by the selected machine.");
280 exit(EXIT_FAILURE);
281 }
1cf065fb
DH
282 s390_sclp_init();
283 my_ram_size = machine->ram_size;
e249651c 284
8cb310e1 285 /* get a BUS */
22486aa0 286 s390_bus = s390_virtio_bus_init(&my_ram_size);
3ef96221 287 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
f0180f91 288 machine->initrd_filename, ZIPL_FILENAME, false);
3a553fc6 289 s390_flic_init();
8cb310e1 290
28e942f8
CH
291 /* register hypercalls */
292 s390_virtio_register_hcalls();
293
8cb310e1 294 /* allocate RAM */
80d23275 295 s390_memory_init(my_ram_size);
8cb310e1 296
326384d5
AG
297 /* clear virtio region */
298 virtio_region_len = my_ram_size - ram_size;
299 virtio_region_start = ram_size;
300 virtio_region = cpu_physical_memory_map(virtio_region_start,
301 &virtio_region_len, true);
302 memset(virtio_region, 0, virtio_region_len);
303 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
304 virtio_region_len);
305
8cb310e1 306 /* init CPUs */
0f5f6691 307 s390_init_cpus(machine->cpu_model);
8cb310e1 308
8cb310e1 309 /* Create VirtIO network adapters */
fad37673 310 s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
3f9e59bb
JH
311
312 /* Register savevm handler for guest TOD clock */
313 register_savevm(NULL, "todclock", 0, 1, gtod_save, gtod_load, NULL);
8cb310e1
AG
314}
315
3dd7852f
AK
316void s390_nmi(NMIState *n, int cpu_index, Error **errp)
317{
318 CPUState *cs = qemu_get_cpu(cpu_index);
319
320 if (s390_cpu_restart(S390_CPU(cs))) {
c6bd8c70 321 error_setg(errp, QERR_UNSUPPORTED);
3dd7852f
AK
322 }
323}
324
db3b2566
DH
325void s390_machine_reset(void)
326{
327 S390CPU *ipl_cpu = S390_CPU(qemu_get_cpu(0));
328
329 qemu_devices_reset();
1cd4e0f6 330 s390_cmma_reset();
4ab72920 331 s390_crypto_reset();
db3b2566
DH
332
333 /* all cpus are stopped - configure and start the ipl cpu only */
334 s390_ipl_prepare_cpu(ipl_cpu);
335 s390_cpu_set_state(CPU_STATE_OPERATING, ipl_cpu);
336}
337
d07aa7c7
AK
338static void s390_machine_class_init(ObjectClass *oc, void *data)
339{
340 MachineClass *mc = MACHINE_CLASS(oc);
3dd7852f 341 NMIClass *nc = NMI_CLASS(oc);
d07aa7c7 342
d07aa7c7 343 mc->alias = "s390";
3c4c694c 344 mc->desc = "VirtIO based S390 machine (deprecated)";
d07aa7c7 345 mc->init = s390_init;
db3b2566 346 mc->reset = s390_machine_reset;
d07aa7c7
AK
347 mc->block_default_type = IF_VIRTIO;
348 mc->max_cpus = 255;
349 mc->no_serial = 1;
350 mc->no_parallel = 1;
351 mc->use_virtcon = 1;
352 mc->no_floppy = 1;
353 mc->no_cdrom = 1;
354 mc->no_sdcard = 1;
3dd7852f 355 nc->nmi_monitor_handler = s390_nmi;
d07aa7c7
AK
356}
357
358static const TypeInfo s390_machine_info = {
359 .name = TYPE_S390_MACHINE,
360 .parent = TYPE_MACHINE,
361 .class_init = s390_machine_class_init,
3dd7852f
AK
362 .interfaces = (InterfaceInfo[]) {
363 { TYPE_NMI },
364 { }
365 },
8cb310e1
AG
366};
367
d07aa7c7 368static void s390_machine_register_types(void)
8cb310e1 369{
d07aa7c7 370 type_register_static(&s390_machine_info);
8cb310e1
AG
371}
372
d07aa7c7 373type_init(s390_machine_register_types)