s390: Add a hypercall registration interface.
[qemu/ar7.git] / hw / s390-virtio.c
blob5edaabb7c4f6ed4660b41ebcde1e2b0bf98b9e71
1 /*
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/>.
24 #include "hw.h"
25 #include "block/block.h"
26 #include "sysemu/blockdev.h"
27 #include "sysemu/sysemu.h"
28 #include "net/net.h"
29 #include "boards.h"
30 #include "monitor/monitor.h"
31 #include "loader.h"
32 #include "hw/virtio.h"
33 #include "hw/sysbus.h"
34 #include "sysemu/kvm.h"
35 #include "exec/address-spaces.h"
37 #include "hw/s390-virtio-bus.h"
38 #include "hw/s390x/sclp.h"
39 #include "hw/s390-virtio.h"
41 //#define DEBUG_S390
43 #ifdef DEBUG_S390
44 #define dprintf(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define dprintf(fmt, ...) \
48 do { } while (0)
49 #endif
51 #define MAX_BLK_DEVS 10
53 static VirtIOS390Bus *s390_bus;
54 static S390CPU **ipi_states;
56 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
58 if (cpu_addr >= smp_cpus) {
59 return NULL;
62 return ipi_states[cpu_addr];
65 static int s390_virtio_hcall_notify(const uint64_t *args)
67 uint64_t mem = args[0];
68 int r = 0, i;
70 if (mem > ram_size) {
71 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
72 if (dev) {
73 virtio_queue_notify(dev->vdev, i);
74 } else {
75 r = -EINVAL;
77 } else {
78 /* Early printk */
80 return r;
83 static int s390_virtio_hcall_reset(const uint64_t *args)
85 uint64_t mem = args[0];
86 VirtIOS390Device *dev;
88 dev = s390_virtio_bus_find_mem(s390_bus, mem);
89 virtio_reset(dev->vdev);
90 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
91 s390_virtio_device_sync(dev);
92 s390_virtio_reset_idx(dev);
94 return 0;
97 static int s390_virtio_hcall_set_status(const uint64_t *args)
99 uint64_t mem = args[0];
100 int r = 0;
101 VirtIOS390Device *dev;
103 dev = s390_virtio_bus_find_mem(s390_bus, mem);
104 if (dev) {
105 s390_virtio_device_update_status(dev);
106 } else {
107 r = -EINVAL;
109 return r;
112 static void s390_virtio_register_hcalls(void)
114 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
115 s390_virtio_hcall_notify);
116 s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
117 s390_virtio_hcall_reset);
118 s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
119 s390_virtio_hcall_set_status);
123 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
124 * being either stopped or disabled (for interrupts) waiting. We have to
125 * track this number to call the shutdown sequence accordingly. This
126 * number is modified either on startup or while holding the big qemu lock.
128 static unsigned s390_running_cpus;
130 void s390_add_running_cpu(CPUS390XState *env)
132 if (env->halted) {
133 s390_running_cpus++;
134 env->halted = 0;
135 env->exception_index = -1;
139 unsigned s390_del_running_cpu(CPUS390XState *env)
141 if (env->halted == 0) {
142 assert(s390_running_cpus >= 1);
143 s390_running_cpus--;
144 env->halted = 1;
145 env->exception_index = EXCP_HLT;
147 return s390_running_cpus;
150 /* PC hardware initialisation */
151 static void s390_init(QEMUMachineInitArgs *args)
153 ram_addr_t my_ram_size = args->ram_size;
154 const char *cpu_model = args->cpu_model;
155 CPUS390XState *env = NULL;
156 DeviceState *dev;
157 MemoryRegion *sysmem = get_system_memory();
158 MemoryRegion *ram = g_new(MemoryRegion, 1);
159 int shift = 0;
160 uint8_t *storage_keys;
161 void *virtio_region;
162 hwaddr virtio_region_len;
163 hwaddr virtio_region_start;
164 int i;
166 /* s390x ram size detection needs a 16bit multiplier + an increment. So
167 guests > 64GB can be specified in 2MB steps etc. */
168 while ((my_ram_size >> (20 + shift)) > 65535) {
169 shift++;
171 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
173 /* lets propagate the changed ram size into the global variable. */
174 ram_size = my_ram_size;
176 /* get a BUS */
177 s390_bus = s390_virtio_bus_init(&my_ram_size);
178 s390_sclp_init();
179 dev = qdev_create(NULL, "s390-ipl");
180 if (args->kernel_filename) {
181 qdev_prop_set_string(dev, "kernel", args->kernel_filename);
183 if (args->initrd_filename) {
184 qdev_prop_set_string(dev, "initrd", args->initrd_filename);
186 qdev_prop_set_string(dev, "cmdline", args->kernel_cmdline);
187 qdev_init_nofail(dev);
189 /* register hypercalls */
190 s390_virtio_register_hcalls();
192 /* allocate RAM */
193 memory_region_init_ram(ram, "s390.ram", my_ram_size);
194 vmstate_register_ram_global(ram);
195 memory_region_add_subregion(sysmem, 0, ram);
197 /* clear virtio region */
198 virtio_region_len = my_ram_size - ram_size;
199 virtio_region_start = ram_size;
200 virtio_region = cpu_physical_memory_map(virtio_region_start,
201 &virtio_region_len, true);
202 memset(virtio_region, 0, virtio_region_len);
203 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
204 virtio_region_len);
206 /* allocate storage keys */
207 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
209 /* init CPUs */
210 if (cpu_model == NULL) {
211 cpu_model = "host";
214 ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
216 for (i = 0; i < smp_cpus; i++) {
217 S390CPU *cpu;
218 CPUS390XState *tmp_env;
220 cpu = cpu_s390x_init(cpu_model);
221 tmp_env = &cpu->env;
222 if (!env) {
223 env = tmp_env;
225 ipi_states[i] = cpu;
226 tmp_env->halted = 1;
227 tmp_env->exception_index = EXCP_HLT;
228 tmp_env->storage_keys = storage_keys;
232 /* Create VirtIO network adapters */
233 for(i = 0; i < nb_nics; i++) {
234 NICInfo *nd = &nd_table[i];
235 DeviceState *dev;
237 if (!nd->model) {
238 nd->model = g_strdup("virtio");
241 if (strcmp(nd->model, "virtio")) {
242 fprintf(stderr, "S390 only supports VirtIO nics\n");
243 exit(1);
246 dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
247 qdev_set_nic_properties(dev, nd);
248 qdev_init_nofail(dev);
252 static QEMUMachine s390_machine = {
253 .name = "s390-virtio",
254 .alias = "s390",
255 .desc = "VirtIO based S390 machine",
256 .init = s390_init,
257 .block_default_type = IF_VIRTIO,
258 .no_cdrom = 1,
259 .no_floppy = 1,
260 .no_serial = 1,
261 .no_parallel = 1,
262 .no_sdcard = 1,
263 .use_virtcon = 1,
264 .max_cpus = 255,
265 .is_default = 1,
266 DEFAULT_MACHINE_OPTIONS,
269 static void s390_machine_init(void)
271 qemu_register_machine(&s390_machine);
274 machine_init(s390_machine_init);