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 "sysemu/block-backend.h"
26 #include "sysemu/blockdev.h"
27 #include "sysemu/sysemu.h"
29 #include "hw/boards.h"
30 #include "monitor/monitor.h"
31 #include "hw/loader.h"
32 #include "hw/virtio/virtio.h"
33 #include "hw/sysbus.h"
34 #include "sysemu/kvm.h"
35 #include "exec/address-spaces.h"
37 #include "hw/s390x/s390-virtio-bus.h"
38 #include "hw/s390x/sclp.h"
39 #include "hw/s390x/s390_flic.h"
40 #include "hw/s390x/s390-virtio.h"
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
48 #define DPRINTF(fmt, ...) \
52 #define MAX_BLK_DEVS 10
53 #define ZIPL_FILENAME "s390-zipl.rom"
54 #define TYPE_S390_MACHINE "s390-machine"
56 static VirtIOS390Bus
*s390_bus
;
57 static S390CPU
**ipi_states
;
59 S390CPU
*s390_cpu_addr2state(uint16_t cpu_addr
)
61 if (cpu_addr
>= smp_cpus
) {
65 return ipi_states
[cpu_addr
];
68 static int s390_virtio_hcall_notify(const uint64_t *args
)
70 uint64_t mem
= args
[0];
74 VirtIOS390Device
*dev
= s390_virtio_bus_find_vring(s390_bus
, mem
, &i
);
76 virtio_queue_notify(dev
->vdev
, i
);
86 static int s390_virtio_hcall_reset(const uint64_t *args
)
88 uint64_t mem
= args
[0];
89 VirtIOS390Device
*dev
;
91 dev
= s390_virtio_bus_find_mem(s390_bus
, mem
);
95 virtio_reset(dev
->vdev
);
96 stb_phys(&address_space_memory
, dev
->dev_offs
+ VIRTIO_DEV_OFFS_STATUS
, 0);
97 s390_virtio_device_sync(dev
);
98 s390_virtio_reset_idx(dev
);
103 static int s390_virtio_hcall_set_status(const uint64_t *args
)
105 uint64_t mem
= args
[0];
107 VirtIOS390Device
*dev
;
109 dev
= s390_virtio_bus_find_mem(s390_bus
, mem
);
111 s390_virtio_device_update_status(dev
);
118 static void s390_virtio_register_hcalls(void)
120 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY
,
121 s390_virtio_hcall_notify
);
122 s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET
,
123 s390_virtio_hcall_reset
);
124 s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS
,
125 s390_virtio_hcall_set_status
);
128 void s390_init_ipl_dev(const char *kernel_filename
,
129 const char *kernel_cmdline
,
130 const char *initrd_filename
,
131 const char *firmware
,
136 dev
= qdev_create(NULL
, "s390-ipl");
137 if (kernel_filename
) {
138 qdev_prop_set_string(dev
, "kernel", kernel_filename
);
140 if (initrd_filename
) {
141 qdev_prop_set_string(dev
, "initrd", initrd_filename
);
143 qdev_prop_set_string(dev
, "cmdline", kernel_cmdline
);
144 qdev_prop_set_string(dev
, "firmware", firmware
);
145 qdev_prop_set_bit(dev
, "enforce_bios", enforce_bios
);
146 object_property_add_child(qdev_get_machine(), "s390-ipl",
148 qdev_init_nofail(dev
);
151 void s390_init_cpus(const char *cpu_model
, uint8_t *storage_keys
)
155 if (cpu_model
== NULL
) {
159 ipi_states
= g_malloc(sizeof(S390CPU
*) * smp_cpus
);
161 for (i
= 0; i
< smp_cpus
; i
++) {
165 cpu
= cpu_s390x_init(cpu_model
);
170 cs
->exception_index
= EXCP_HLT
;
171 cpu
->env
.storage_keys
= storage_keys
;
176 void s390_create_virtio_net(BusState
*bus
, const char *name
)
180 for (i
= 0; i
< nb_nics
; i
++) {
181 NICInfo
*nd
= &nd_table
[i
];
185 nd
->model
= g_strdup("virtio");
188 if (strcmp(nd
->model
, "virtio")) {
189 fprintf(stderr
, "S390 only supports VirtIO nics\n");
193 dev
= qdev_create(bus
, name
);
194 qdev_set_nic_properties(dev
, nd
);
195 qdev_init_nofail(dev
);
199 /* PC hardware initialisation */
200 static void s390_init(MachineState
*machine
)
202 ram_addr_t my_ram_size
= machine
->ram_size
;
203 MemoryRegion
*sysmem
= get_system_memory();
204 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
205 int increment_size
= 20;
206 uint8_t *storage_keys
;
208 hwaddr virtio_region_len
;
209 hwaddr virtio_region_start
;
212 * The storage increment size is a multiple of 1M and is a power of 2.
213 * The number of storage increments must be MAX_STORAGE_INCREMENTS or
216 while ((my_ram_size
>> increment_size
) > MAX_STORAGE_INCREMENTS
) {
219 my_ram_size
= my_ram_size
>> increment_size
<< increment_size
;
221 /* let's propagate the changed ram size into the global variable. */
222 ram_size
= my_ram_size
;
225 s390_bus
= s390_virtio_bus_init(&my_ram_size
);
227 s390_init_ipl_dev(machine
->kernel_filename
, machine
->kernel_cmdline
,
228 machine
->initrd_filename
, ZIPL_FILENAME
, false);
231 /* register hypercalls */
232 s390_virtio_register_hcalls();
235 memory_region_init_ram(ram
, NULL
, "s390.ram", my_ram_size
, &error_abort
);
236 vmstate_register_ram_global(ram
);
237 memory_region_add_subregion(sysmem
, 0, ram
);
239 /* clear virtio region */
240 virtio_region_len
= my_ram_size
- ram_size
;
241 virtio_region_start
= ram_size
;
242 virtio_region
= cpu_physical_memory_map(virtio_region_start
,
243 &virtio_region_len
, true);
244 memset(virtio_region
, 0, virtio_region_len
);
245 cpu_physical_memory_unmap(virtio_region
, virtio_region_len
, 1,
248 /* allocate storage keys */
249 storage_keys
= g_malloc0(my_ram_size
/ TARGET_PAGE_SIZE
);
252 s390_init_cpus(machine
->cpu_model
, storage_keys
);
254 /* Create VirtIO network adapters */
255 s390_create_virtio_net((BusState
*)s390_bus
, "virtio-net-s390");
258 void s390_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
260 CPUState
*cs
= qemu_get_cpu(cpu_index
);
262 if (s390_cpu_restart(S390_CPU(cs
))) {
263 error_set(errp
, QERR_UNSUPPORTED
);
267 static void s390_machine_class_init(ObjectClass
*oc
, void *data
)
269 MachineClass
*mc
= MACHINE_CLASS(oc
);
270 NMIClass
*nc
= NMI_CLASS(oc
);
272 mc
->name
= "s390-virtio";
274 mc
->desc
= "VirtIO based S390 machine";
275 mc
->init
= s390_init
;
276 mc
->block_default_type
= IF_VIRTIO
;
285 nc
->nmi_monitor_handler
= s390_nmi
;
288 static const TypeInfo s390_machine_info
= {
289 .name
= TYPE_S390_MACHINE
,
290 .parent
= TYPE_MACHINE
,
291 .class_init
= s390_machine_class_init
,
292 .interfaces
= (InterfaceInfo
[]) {
298 static void s390_machine_register_types(void)
300 type_register_static(&s390_machine_info
);
303 type_init(s390_machine_register_types
)