2 * QEMU S390 virtio target
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp 2012
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.
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.
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.
20 * You should have received a copy of the GNU (Lesser) General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "qapi/qmp/qerror.h"
26 #include "qemu/error-report.h"
27 #include "sysemu/block-backend.h"
28 #include "sysemu/blockdev.h"
29 #include "sysemu/sysemu.h"
31 #include "hw/boards.h"
32 #include "hw/loader.h"
33 #include "hw/virtio/virtio.h"
34 #include "hw/sysbus.h"
35 #include "sysemu/kvm.h"
36 #include "exec/address-spaces.h"
38 #include "hw/s390x/s390-virtio-bus.h"
39 #include "hw/s390x/sclp.h"
40 #include "hw/s390x/s390_flic.h"
41 #include "hw/s390x/s390-virtio.h"
42 #include "hw/s390x/storage-keys.h"
43 #include "hw/s390x/ipl.h"
49 #define DPRINTF(fmt, ...) \
50 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
52 #define DPRINTF(fmt, ...) \
56 #define MAX_BLK_DEVS 10
57 #define ZIPL_FILENAME "s390-zipl.rom"
58 #define S390_MACHINE "s390-virtio"
59 #define TYPE_S390_MACHINE MACHINE_TYPE_NAME(S390_MACHINE)
61 #define S390_TOD_CLOCK_VALUE_MISSING 0x00
62 #define S390_TOD_CLOCK_VALUE_PRESENT 0x01
64 static VirtIOS390Bus
*s390_bus
;
65 static S390CPU
**ipi_states
;
67 S390CPU
*s390_cpu_addr2state(uint16_t cpu_addr
)
69 if (cpu_addr
>= smp_cpus
) {
73 return ipi_states
[cpu_addr
];
76 static int s390_virtio_hcall_notify(const uint64_t *args
)
78 uint64_t mem
= args
[0];
82 VirtIOS390Device
*dev
= s390_virtio_bus_find_vring(s390_bus
, mem
, &i
);
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
91 if (!(dev
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
92 s390_virtio_device_update_status(dev
);
94 virtio_queue_notify(dev
->vdev
, i
);
104 static int s390_virtio_hcall_reset(const uint64_t *args
)
106 uint64_t mem
= args
[0];
107 VirtIOS390Device
*dev
;
109 dev
= s390_virtio_bus_find_mem(s390_bus
, mem
);
113 virtio_reset(dev
->vdev
);
114 address_space_stb(&address_space_memory
,
115 dev
->dev_offs
+ VIRTIO_DEV_OFFS_STATUS
, 0,
116 MEMTXATTRS_UNSPECIFIED
, NULL
);
117 s390_virtio_device_sync(dev
);
118 s390_virtio_reset_idx(dev
);
123 static int s390_virtio_hcall_set_status(const uint64_t *args
)
125 uint64_t mem
= args
[0];
127 VirtIOS390Device
*dev
;
129 dev
= s390_virtio_bus_find_mem(s390_bus
, mem
);
131 s390_virtio_device_update_status(dev
);
138 static void s390_virtio_register_hcalls(void)
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
);
148 void s390_init_ipl_dev(const char *kernel_filename
,
149 const char *kernel_cmdline
,
150 const char *initrd_filename
,
151 const char *firmware
,
156 dev
= qdev_create(NULL
, "s390-ipl");
157 if (kernel_filename
) {
158 qdev_prop_set_string(dev
, "kernel", kernel_filename
);
160 if (initrd_filename
) {
161 qdev_prop_set_string(dev
, "initrd", initrd_filename
);
163 qdev_prop_set_string(dev
, "cmdline", kernel_cmdline
);
164 qdev_prop_set_string(dev
, "firmware", firmware
);
165 qdev_prop_set_bit(dev
, "enforce_bios", enforce_bios
);
166 object_property_add_child(qdev_get_machine(), "s390-ipl",
168 qdev_init_nofail(dev
);
171 void s390_init_cpus(const char *cpu_model
)
175 if (cpu_model
== NULL
) {
179 ipi_states
= g_malloc(sizeof(S390CPU
*) * smp_cpus
);
181 for (i
= 0; i
< smp_cpus
; i
++) {
185 cpu
= cpu_s390x_init(cpu_model
);
190 cs
->exception_index
= EXCP_HLT
;
195 void s390_create_virtio_net(BusState
*bus
, const char *name
)
199 for (i
= 0; i
< nb_nics
; i
++) {
200 NICInfo
*nd
= &nd_table
[i
];
204 nd
->model
= g_strdup("virtio");
207 if (strcmp(nd
->model
, "virtio")) {
208 fprintf(stderr
, "S390 only supports VirtIO nics\n");
212 dev
= qdev_create(bus
, name
);
213 qdev_set_nic_properties(dev
, nd
);
214 qdev_init_nofail(dev
);
218 void gtod_save(QEMUFile
*f
, void *opaque
)
224 r
= s390_get_clock(&tod_high
, &tod_low
);
226 fprintf(stderr
, "WARNING: Unable to get guest clock for migration. "
227 "Error code %d. Guest clock will not be migrated "
228 "which could cause the guest to hang.\n", r
);
229 qemu_put_byte(f
, S390_TOD_CLOCK_VALUE_MISSING
);
233 qemu_put_byte(f
, S390_TOD_CLOCK_VALUE_PRESENT
);
234 qemu_put_byte(f
, tod_high
);
235 qemu_put_be64(f
, tod_low
);
238 int gtod_load(QEMUFile
*f
, void *opaque
, int version_id
)
244 if (qemu_get_byte(f
) == S390_TOD_CLOCK_VALUE_MISSING
) {
245 fprintf(stderr
, "WARNING: Guest clock was not migrated. This could "
246 "cause the guest to hang.\n");
250 tod_high
= qemu_get_byte(f
);
251 tod_low
= qemu_get_be64(f
);
253 r
= s390_set_clock(&tod_high
, &tod_low
);
255 fprintf(stderr
, "WARNING: Unable to set guest clock value. "
256 "s390_get_clock returned error %d. This could cause "
257 "the guest to hang.\n", r
);
263 /* PC hardware initialisation */
264 static void s390_init(MachineState
*machine
)
266 ram_addr_t my_ram_size
;
268 hwaddr virtio_region_len
;
269 hwaddr virtio_region_start
;
271 if (machine
->ram_slots
) {
272 error_report("Memory hotplug not supported by the selected machine.");
276 my_ram_size
= machine
->ram_size
;
279 s390_bus
= s390_virtio_bus_init(&my_ram_size
);
280 s390_init_ipl_dev(machine
->kernel_filename
, machine
->kernel_cmdline
,
281 machine
->initrd_filename
, ZIPL_FILENAME
, false);
284 /* register hypercalls */
285 s390_virtio_register_hcalls();
288 s390_memory_init(my_ram_size
);
290 /* clear virtio region */
291 virtio_region_len
= my_ram_size
- ram_size
;
292 virtio_region_start
= ram_size
;
293 virtio_region
= cpu_physical_memory_map(virtio_region_start
,
294 &virtio_region_len
, true);
295 memset(virtio_region
, 0, virtio_region_len
);
296 cpu_physical_memory_unmap(virtio_region
, virtio_region_len
, 1,
300 s390_init_cpus(machine
->cpu_model
);
302 /* Create VirtIO network adapters */
303 s390_create_virtio_net((BusState
*)s390_bus
, "virtio-net-s390");
305 /* Register savevm handler for guest TOD clock */
306 register_savevm(NULL
, "todclock", 0, 1, gtod_save
, gtod_load
, NULL
);
309 void s390_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
311 CPUState
*cs
= qemu_get_cpu(cpu_index
);
313 if (s390_cpu_restart(S390_CPU(cs
))) {
314 error_setg(errp
, QERR_UNSUPPORTED
);
318 void s390_machine_reset(void)
320 S390CPU
*ipl_cpu
= S390_CPU(qemu_get_cpu(0));
322 qemu_devices_reset();
324 /* all cpus are stopped - configure and start the ipl cpu only */
325 s390_ipl_prepare_cpu(ipl_cpu
);
326 s390_cpu_set_state(CPU_STATE_OPERATING
, ipl_cpu
);
329 static void s390_machine_class_init(ObjectClass
*oc
, void *data
)
331 MachineClass
*mc
= MACHINE_CLASS(oc
);
332 NMIClass
*nc
= NMI_CLASS(oc
);
335 mc
->desc
= "VirtIO based S390 machine";
336 mc
->init
= s390_init
;
337 mc
->reset
= s390_machine_reset
;
338 mc
->block_default_type
= IF_VIRTIO
;
346 nc
->nmi_monitor_handler
= s390_nmi
;
349 static const TypeInfo s390_machine_info
= {
350 .name
= TYPE_S390_MACHINE
,
351 .parent
= TYPE_MACHINE
,
352 .class_init
= s390_machine_class_init
,
353 .interfaces
= (InterfaceInfo
[]) {
359 static void s390_machine_register_types(void)
361 type_register_static(&s390_machine_info
);
364 type_init(s390_machine_register_types
)