test: add test-qmp-commands to make check
[qemu.git] / hw / s390-virtio.c
blob1ebe70d0e3e0f58fb8cb58257af9350260384732
1 /*
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/>.
20 #include "hw.h"
21 #include "block.h"
22 #include "blockdev.h"
23 #include "sysemu.h"
24 #include "net.h"
25 #include "boards.h"
26 #include "monitor.h"
27 #include "loader.h"
28 #include "elf.h"
29 #include "hw/virtio.h"
30 #include "hw/sysbus.h"
31 #include "kvm.h"
32 #include "exec-memory.h"
34 #include "hw/s390-virtio-bus.h"
36 //#define DEBUG_S390
38 #ifdef DEBUG_S390
39 #define dprintf(fmt, ...) \
40 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
41 #else
42 #define dprintf(fmt, ...) \
43 do { } while (0)
44 #endif
46 #define KVM_S390_VIRTIO_NOTIFY 0
47 #define KVM_S390_VIRTIO_RESET 1
48 #define KVM_S390_VIRTIO_SET_STATUS 2
50 #define KERN_IMAGE_START 0x010000UL
51 #define KERN_PARM_AREA 0x010480UL
52 #define INITRD_START 0x800000UL
53 #define INITRD_PARM_START 0x010408UL
54 #define INITRD_PARM_SIZE 0x010410UL
55 #define PARMFILE_START 0x001000UL
57 #define ZIPL_START 0x009000UL
58 #define ZIPL_LOAD_ADDR 0x009000UL
59 #define ZIPL_FILENAME "s390-zipl.rom"
61 #define MAX_BLK_DEVS 10
63 static VirtIOS390Bus *s390_bus;
64 static CPUS390XState **ipi_states;
66 CPUS390XState *s390_cpu_addr2state(uint16_t cpu_addr)
68 if (cpu_addr >= smp_cpus) {
69 return NULL;
72 return ipi_states[cpu_addr];
75 int s390_virtio_hypercall(CPUS390XState *env, uint64_t mem, uint64_t hypercall)
77 int r = 0, i;
79 dprintf("KVM hypercall: %ld\n", hypercall);
80 switch (hypercall) {
81 case KVM_S390_VIRTIO_NOTIFY:
82 if (mem > ram_size) {
83 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
84 mem, &i);
85 if (dev) {
86 virtio_queue_notify(dev->vdev, i);
87 } else {
88 r = -EINVAL;
90 } else {
91 /* Early printk */
93 break;
94 case KVM_S390_VIRTIO_RESET:
96 VirtIOS390Device *dev;
98 dev = s390_virtio_bus_find_mem(s390_bus, mem);
99 virtio_reset(dev->vdev);
100 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
101 s390_virtio_device_sync(dev);
102 break;
104 case KVM_S390_VIRTIO_SET_STATUS:
106 VirtIOS390Device *dev;
108 dev = s390_virtio_bus_find_mem(s390_bus, mem);
109 if (dev) {
110 s390_virtio_device_update_status(dev);
111 } else {
112 r = -EINVAL;
114 break;
116 default:
117 r = -EINVAL;
118 break;
121 return r;
125 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
126 * being either stopped or disabled (for interrupts) waiting. We have to
127 * track this number to call the shutdown sequence accordingly. This
128 * number is modified either on startup or while holding the big qemu lock.
130 static unsigned s390_running_cpus;
132 void s390_add_running_cpu(CPUS390XState *env)
134 if (env->halted) {
135 s390_running_cpus++;
136 env->halted = 0;
137 env->exception_index = -1;
141 unsigned s390_del_running_cpu(CPUS390XState *env)
143 if (env->halted == 0) {
144 assert(s390_running_cpus >= 1);
145 s390_running_cpus--;
146 env->halted = 1;
147 env->exception_index = EXCP_HLT;
149 return s390_running_cpus;
152 /* PC hardware initialisation */
153 static void s390_init(ram_addr_t my_ram_size,
154 const char *boot_device,
155 const char *kernel_filename,
156 const char *kernel_cmdline,
157 const char *initrd_filename,
158 const char *cpu_model)
160 CPUS390XState *env = NULL;
161 MemoryRegion *sysmem = get_system_memory();
162 MemoryRegion *ram = g_new(MemoryRegion, 1);
163 ram_addr_t kernel_size = 0;
164 ram_addr_t initrd_offset;
165 ram_addr_t initrd_size = 0;
166 int shift = 0;
167 uint8_t *storage_keys;
168 void *virtio_region;
169 target_phys_addr_t virtio_region_len;
170 target_phys_addr_t virtio_region_start;
171 int i;
173 /* s390x ram size detection needs a 16bit multiplier + an increment. So
174 guests > 64GB can be specified in 2MB steps etc. */
175 while ((my_ram_size >> (20 + shift)) > 65535) {
176 shift++;
178 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
180 /* lets propagate the changed ram size into the global variable. */
181 ram_size = my_ram_size;
183 /* get a BUS */
184 s390_bus = s390_virtio_bus_init(&my_ram_size);
186 /* allocate RAM */
187 memory_region_init_ram(ram, "s390.ram", my_ram_size);
188 vmstate_register_ram_global(ram);
189 memory_region_add_subregion(sysmem, 0, ram);
191 /* clear virtio region */
192 virtio_region_len = my_ram_size - ram_size;
193 virtio_region_start = ram_size;
194 virtio_region = cpu_physical_memory_map(virtio_region_start,
195 &virtio_region_len, true);
196 memset(virtio_region, 0, virtio_region_len);
197 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
198 virtio_region_len);
200 /* allocate storage keys */
201 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
203 /* init CPUs */
204 if (cpu_model == NULL) {
205 cpu_model = "host";
208 ipi_states = g_malloc(sizeof(CPUS390XState *) * smp_cpus);
210 for (i = 0; i < smp_cpus; i++) {
211 CPUS390XState *tmp_env;
213 tmp_env = cpu_init(cpu_model);
214 if (!env) {
215 env = tmp_env;
217 ipi_states[i] = tmp_env;
218 tmp_env->halted = 1;
219 tmp_env->exception_index = EXCP_HLT;
220 tmp_env->storage_keys = storage_keys;
223 /* One CPU has to run */
224 s390_add_running_cpu(env);
226 if (kernel_filename) {
228 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, NULL,
229 NULL, 1, ELF_MACHINE, 0);
230 if (kernel_size == -1UL) {
231 kernel_size = load_image_targphys(kernel_filename, 0, ram_size);
234 * we can not rely on the ELF entry point, since up to 3.2 this
235 * value was 0x800 (the SALIPL loader) and it wont work. For
236 * all (Linux) cases 0x10000 (KERN_IMAGE_START) should be fine.
238 env->psw.addr = KERN_IMAGE_START;
239 env->psw.mask = 0x0000000180000000ULL;
240 } else {
241 ram_addr_t bios_size = 0;
242 char *bios_filename;
244 /* Load zipl bootloader */
245 if (bios_name == NULL) {
246 bios_name = ZIPL_FILENAME;
249 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
250 bios_size = load_image_targphys(bios_filename, ZIPL_LOAD_ADDR, 4096);
251 g_free(bios_filename);
253 if ((long)bios_size < 0) {
254 hw_error("could not load bootloader '%s'\n", bios_name);
257 if (bios_size > 4096) {
258 hw_error("stage1 bootloader is > 4k\n");
261 env->psw.addr = ZIPL_START;
262 env->psw.mask = 0x0000000180000000ULL;
265 if (initrd_filename) {
266 initrd_offset = INITRD_START;
267 while (kernel_size + 0x100000 > initrd_offset) {
268 initrd_offset += 0x100000;
270 initrd_size = load_image_targphys(initrd_filename, initrd_offset,
271 ram_size - initrd_offset);
272 /* we have to overwrite values in the kernel image, which are "rom" */
273 memcpy(rom_ptr(INITRD_PARM_START), &initrd_offset, 8);
274 memcpy(rom_ptr(INITRD_PARM_SIZE), &initrd_size, 8);
277 if (kernel_cmdline) {
278 /* we have to overwrite values in the kernel image, which are "rom" */
279 memcpy(rom_ptr(KERN_PARM_AREA), kernel_cmdline,
280 strlen(kernel_cmdline) + 1);
283 /* Create VirtIO network adapters */
284 for(i = 0; i < nb_nics; i++) {
285 NICInfo *nd = &nd_table[i];
286 DeviceState *dev;
288 if (!nd->model) {
289 nd->model = g_strdup("virtio");
292 if (strcmp(nd->model, "virtio")) {
293 fprintf(stderr, "S390 only supports VirtIO nics\n");
294 exit(1);
297 dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
298 qdev_set_nic_properties(dev, nd);
299 qdev_init_nofail(dev);
302 /* Create VirtIO disk drives */
303 for(i = 0; i < MAX_BLK_DEVS; i++) {
304 DriveInfo *dinfo;
305 DeviceState *dev;
307 dinfo = drive_get(IF_IDE, 0, i);
308 if (!dinfo) {
309 continue;
312 dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
313 qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
314 qdev_init_nofail(dev);
318 static QEMUMachine s390_machine = {
319 .name = "s390-virtio",
320 .alias = "s390",
321 .desc = "VirtIO based S390 machine",
322 .init = s390_init,
323 .no_serial = 1,
324 .no_parallel = 1,
325 .use_virtcon = 1,
326 .max_cpus = 255,
327 .is_default = 1,
330 static void s390_machine_init(void)
332 qemu_register_machine(&s390_machine);
335 machine_init(s390_machine_init);