2 * QEMU S390 virtio target
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 #include "hw/virtio.h"
30 #include "hw/sysbus.h"
32 #include "exec-memory.h"
34 #include "hw/s390-virtio-bus.h"
35 #include "hw/s390x/sclp.h"
40 #define dprintf(fmt, ...) \
41 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
43 #define dprintf(fmt, ...) \
47 #define KVM_S390_VIRTIO_NOTIFY 0
48 #define KVM_S390_VIRTIO_RESET 1
49 #define KVM_S390_VIRTIO_SET_STATUS 2
51 #define KERN_IMAGE_START 0x010000UL
52 #define KERN_PARM_AREA 0x010480UL
53 #define INITRD_START 0x800000UL
54 #define INITRD_PARM_START 0x010408UL
55 #define INITRD_PARM_SIZE 0x010410UL
56 #define PARMFILE_START 0x001000UL
58 #define ZIPL_START 0x009000UL
59 #define ZIPL_LOAD_ADDR 0x009000UL
60 #define ZIPL_FILENAME "s390-zipl.rom"
62 #define MAX_BLK_DEVS 10
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 int s390_virtio_hypercall(CPUS390XState
*env
, uint64_t mem
, uint64_t hypercall
)
80 dprintf("KVM hypercall: %ld\n", hypercall
);
82 case KVM_S390_VIRTIO_NOTIFY
:
84 VirtIOS390Device
*dev
= s390_virtio_bus_find_vring(s390_bus
,
87 virtio_queue_notify(dev
->vdev
, i
);
95 case KVM_S390_VIRTIO_RESET
:
97 VirtIOS390Device
*dev
;
99 dev
= s390_virtio_bus_find_mem(s390_bus
, mem
);
100 virtio_reset(dev
->vdev
);
101 stb_phys(dev
->dev_offs
+ VIRTIO_DEV_OFFS_STATUS
, 0);
102 s390_virtio_device_sync(dev
);
103 s390_virtio_reset_idx(dev
);
106 case KVM_S390_VIRTIO_SET_STATUS
:
108 VirtIOS390Device
*dev
;
110 dev
= s390_virtio_bus_find_mem(s390_bus
, mem
);
112 s390_virtio_device_update_status(dev
);
127 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
128 * being either stopped or disabled (for interrupts) waiting. We have to
129 * track this number to call the shutdown sequence accordingly. This
130 * number is modified either on startup or while holding the big qemu lock.
132 static unsigned s390_running_cpus
;
134 void s390_add_running_cpu(CPUS390XState
*env
)
139 env
->exception_index
= -1;
143 unsigned s390_del_running_cpu(CPUS390XState
*env
)
145 if (env
->halted
== 0) {
146 assert(s390_running_cpus
>= 1);
149 env
->exception_index
= EXCP_HLT
;
151 return s390_running_cpus
;
154 /* PC hardware initialisation */
155 static void s390_init(QEMUMachineInitArgs
*args
)
157 ram_addr_t my_ram_size
= args
->ram_size
;
158 ram_addr_t ram_size
= args
->ram_size
;
159 const char *cpu_model
= args
->cpu_model
;
160 const char *kernel_filename
= args
->kernel_filename
;
161 const char *kernel_cmdline
= args
->kernel_cmdline
;
162 const char *initrd_filename
= args
->initrd_filename
;
163 CPUS390XState
*env
= NULL
;
164 MemoryRegion
*sysmem
= get_system_memory();
165 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
166 ram_addr_t kernel_size
= 0;
167 ram_addr_t initrd_offset
;
168 ram_addr_t initrd_size
= 0;
170 uint8_t *storage_keys
;
172 hwaddr virtio_region_len
;
173 hwaddr virtio_region_start
;
176 /* s390x ram size detection needs a 16bit multiplier + an increment. So
177 guests > 64GB can be specified in 2MB steps etc. */
178 while ((my_ram_size
>> (20 + shift
)) > 65535) {
181 my_ram_size
= my_ram_size
>> (20 + shift
) << (20 + shift
);
183 /* lets propagate the changed ram size into the global variable. */
184 ram_size
= my_ram_size
;
187 s390_bus
= s390_virtio_bus_init(&my_ram_size
);
191 memory_region_init_ram(ram
, "s390.ram", my_ram_size
);
192 vmstate_register_ram_global(ram
);
193 memory_region_add_subregion(sysmem
, 0, ram
);
195 /* clear virtio region */
196 virtio_region_len
= my_ram_size
- ram_size
;
197 virtio_region_start
= ram_size
;
198 virtio_region
= cpu_physical_memory_map(virtio_region_start
,
199 &virtio_region_len
, true);
200 memset(virtio_region
, 0, virtio_region_len
);
201 cpu_physical_memory_unmap(virtio_region
, virtio_region_len
, 1,
204 /* allocate storage keys */
205 storage_keys
= g_malloc0(my_ram_size
/ TARGET_PAGE_SIZE
);
208 if (cpu_model
== NULL
) {
212 ipi_states
= g_malloc(sizeof(S390CPU
*) * smp_cpus
);
214 for (i
= 0; i
< smp_cpus
; i
++) {
216 CPUS390XState
*tmp_env
;
218 cpu
= cpu_s390x_init(cpu_model
);
225 tmp_env
->exception_index
= EXCP_HLT
;
226 tmp_env
->storage_keys
= storage_keys
;
229 /* One CPU has to run */
230 s390_add_running_cpu(env
);
232 if (kernel_filename
) {
234 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, NULL
, NULL
,
235 NULL
, 1, ELF_MACHINE
, 0);
236 if (kernel_size
== -1UL) {
237 kernel_size
= load_image_targphys(kernel_filename
, 0, ram_size
);
239 if (kernel_size
== -1UL) {
240 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
245 * we can not rely on the ELF entry point, since up to 3.2 this
246 * value was 0x800 (the SALIPL loader) and it wont work. For
247 * all (Linux) cases 0x10000 (KERN_IMAGE_START) should be fine.
249 env
->psw
.addr
= KERN_IMAGE_START
;
250 env
->psw
.mask
= 0x0000000180000000ULL
;
252 ram_addr_t bios_size
= 0;
255 /* Load zipl bootloader */
256 if (bios_name
== NULL
) {
257 bios_name
= ZIPL_FILENAME
;
260 bios_filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
261 bios_size
= load_image_targphys(bios_filename
, ZIPL_LOAD_ADDR
, 4096);
262 g_free(bios_filename
);
264 if ((long)bios_size
< 0) {
265 hw_error("could not load bootloader '%s'\n", bios_name
);
268 if (bios_size
> 4096) {
269 hw_error("stage1 bootloader is > 4k\n");
272 env
->psw
.addr
= ZIPL_START
;
273 env
->psw
.mask
= 0x0000000180000000ULL
;
276 if (initrd_filename
) {
277 initrd_offset
= INITRD_START
;
278 while (kernel_size
+ 0x100000 > initrd_offset
) {
279 initrd_offset
+= 0x100000;
281 initrd_size
= load_image_targphys(initrd_filename
, initrd_offset
,
282 ram_size
- initrd_offset
);
283 if (initrd_size
== -1UL) {
284 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
289 /* we have to overwrite values in the kernel image, which are "rom" */
290 stq_p(rom_ptr(INITRD_PARM_START
), initrd_offset
);
291 stq_p(rom_ptr(INITRD_PARM_SIZE
), initrd_size
);
294 if (rom_ptr(KERN_PARM_AREA
)) {
295 /* we have to overwrite values in the kernel image, which are "rom" */
296 memcpy(rom_ptr(KERN_PARM_AREA
), kernel_cmdline
,
297 strlen(kernel_cmdline
) + 1);
300 /* Create VirtIO network adapters */
301 for(i
= 0; i
< nb_nics
; i
++) {
302 NICInfo
*nd
= &nd_table
[i
];
306 nd
->model
= g_strdup("virtio");
309 if (strcmp(nd
->model
, "virtio")) {
310 fprintf(stderr
, "S390 only supports VirtIO nics\n");
314 dev
= qdev_create((BusState
*)s390_bus
, "virtio-net-s390");
315 qdev_set_nic_properties(dev
, nd
);
316 qdev_init_nofail(dev
);
319 /* Create VirtIO disk drives */
320 for(i
= 0; i
< MAX_BLK_DEVS
; i
++) {
324 dinfo
= drive_get(IF_IDE
, 0, i
);
329 dev
= qdev_create((BusState
*)s390_bus
, "virtio-blk-s390");
330 qdev_prop_set_drive_nofail(dev
, "drive", dinfo
->bdrv
);
331 qdev_init_nofail(dev
);
335 static QEMUMachine s390_machine
= {
336 .name
= "s390-virtio",
338 .desc
= "VirtIO based S390 machine",
350 static void s390_machine_init(void)
352 qemu_register_machine(&s390_machine
);
355 machine_init(s390_machine_init
);