s390x/kvm: implement floating-interrupt controller device
[qemu.git] / hw / s390x / s390-virtio.c
blob0f03fd18b9d2209fd65f4e8f6e8ba00110f762d6
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/hw.h"
25 #include "block/block.h"
26 #include "sysemu/blockdev.h"
27 #include "sysemu/sysemu.h"
28 #include "net/net.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"
42 //#define DEBUG_S390
44 #ifdef DEBUG_S390
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define DPRINTF(fmt, ...) \
49 do { } while (0)
50 #endif
52 #define MAX_BLK_DEVS 10
53 #define ZIPL_FILENAME "s390-zipl.rom"
55 static VirtIOS390Bus *s390_bus;
56 static S390CPU **ipi_states;
58 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
60 if (cpu_addr >= smp_cpus) {
61 return NULL;
64 return ipi_states[cpu_addr];
67 static int s390_virtio_hcall_notify(const uint64_t *args)
69 uint64_t mem = args[0];
70 int r = 0, i;
72 if (mem > ram_size) {
73 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
74 if (dev) {
75 virtio_queue_notify(dev->vdev, i);
76 } else {
77 r = -EINVAL;
79 } else {
80 /* Early printk */
82 return r;
85 static int s390_virtio_hcall_reset(const uint64_t *args)
87 uint64_t mem = args[0];
88 VirtIOS390Device *dev;
90 dev = s390_virtio_bus_find_mem(s390_bus, mem);
91 if (dev == NULL) {
92 return -EINVAL;
94 virtio_reset(dev->vdev);
95 stb_phys(&address_space_memory, dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
96 s390_virtio_device_sync(dev);
97 s390_virtio_reset_idx(dev);
99 return 0;
102 static int s390_virtio_hcall_set_status(const uint64_t *args)
104 uint64_t mem = args[0];
105 int r = 0;
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 return r;
117 static void s390_virtio_register_hcalls(void)
119 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
120 s390_virtio_hcall_notify);
121 s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
122 s390_virtio_hcall_reset);
123 s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
124 s390_virtio_hcall_set_status);
128 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
129 * being either stopped or disabled (for interrupts) waiting. We have to
130 * track this number to call the shutdown sequence accordingly. This
131 * number is modified either on startup or while holding the big qemu lock.
133 static unsigned s390_running_cpus;
135 void s390_add_running_cpu(S390CPU *cpu)
137 CPUState *cs = CPU(cpu);
138 CPUS390XState *env = &cpu->env;
140 if (cs->halted) {
141 s390_running_cpus++;
142 cs->halted = 0;
143 env->exception_index = -1;
147 unsigned s390_del_running_cpu(S390CPU *cpu)
149 CPUState *cs = CPU(cpu);
150 CPUS390XState *env = &cpu->env;
152 if (cs->halted == 0) {
153 assert(s390_running_cpus >= 1);
154 s390_running_cpus--;
155 cs->halted = 1;
156 env->exception_index = EXCP_HLT;
158 return s390_running_cpus;
161 void s390_init_ipl_dev(const char *kernel_filename,
162 const char *kernel_cmdline,
163 const char *initrd_filename,
164 const char *firmware)
166 DeviceState *dev;
168 dev = qdev_create(NULL, "s390-ipl");
169 if (kernel_filename) {
170 qdev_prop_set_string(dev, "kernel", kernel_filename);
172 if (initrd_filename) {
173 qdev_prop_set_string(dev, "initrd", initrd_filename);
175 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
176 qdev_prop_set_string(dev, "firmware", firmware);
177 qdev_init_nofail(dev);
180 void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
182 int i;
184 if (cpu_model == NULL) {
185 cpu_model = "host";
188 ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
190 for (i = 0; i < smp_cpus; i++) {
191 S390CPU *cpu;
192 CPUState *cs;
194 cpu = cpu_s390x_init(cpu_model);
195 cs = CPU(cpu);
197 ipi_states[i] = cpu;
198 cs->halted = 1;
199 cpu->env.exception_index = EXCP_HLT;
200 cpu->env.storage_keys = storage_keys;
205 void s390_create_virtio_net(BusState *bus, const char *name)
207 int i;
209 for (i = 0; i < nb_nics; i++) {
210 NICInfo *nd = &nd_table[i];
211 DeviceState *dev;
213 if (!nd->model) {
214 nd->model = g_strdup("virtio");
217 if (strcmp(nd->model, "virtio")) {
218 fprintf(stderr, "S390 only supports VirtIO nics\n");
219 exit(1);
222 dev = qdev_create(bus, name);
223 qdev_set_nic_properties(dev, nd);
224 qdev_init_nofail(dev);
228 /* PC hardware initialisation */
229 static void s390_init(QEMUMachineInitArgs *args)
231 ram_addr_t my_ram_size = args->ram_size;
232 MemoryRegion *sysmem = get_system_memory();
233 MemoryRegion *ram = g_new(MemoryRegion, 1);
234 int shift = 0;
235 uint8_t *storage_keys;
236 void *virtio_region;
237 hwaddr virtio_region_len;
238 hwaddr virtio_region_start;
240 /* s390x ram size detection needs a 16bit multiplier + an increment. So
241 guests > 64GB can be specified in 2MB steps etc. */
242 while ((my_ram_size >> (20 + shift)) > 65535) {
243 shift++;
245 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
247 /* let's propagate the changed ram size into the global variable. */
248 ram_size = my_ram_size;
250 /* get a BUS */
251 s390_bus = s390_virtio_bus_init(&my_ram_size);
252 s390_sclp_init();
253 s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
254 args->initrd_filename, ZIPL_FILENAME);
255 s390_flic_init();
257 /* register hypercalls */
258 s390_virtio_register_hcalls();
260 /* allocate RAM */
261 memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size);
262 vmstate_register_ram_global(ram);
263 memory_region_add_subregion(sysmem, 0, ram);
265 /* clear virtio region */
266 virtio_region_len = my_ram_size - ram_size;
267 virtio_region_start = ram_size;
268 virtio_region = cpu_physical_memory_map(virtio_region_start,
269 &virtio_region_len, true);
270 memset(virtio_region, 0, virtio_region_len);
271 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
272 virtio_region_len);
274 /* allocate storage keys */
275 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
277 /* init CPUs */
278 s390_init_cpus(args->cpu_model, storage_keys);
280 /* Create VirtIO network adapters */
281 s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
284 static QEMUMachine s390_machine = {
285 .name = "s390-virtio",
286 .alias = "s390",
287 .desc = "VirtIO based S390 machine",
288 .init = s390_init,
289 .block_default_type = IF_VIRTIO,
290 .no_cdrom = 1,
291 .no_floppy = 1,
292 .no_serial = 1,
293 .no_parallel = 1,
294 .no_sdcard = 1,
295 .use_virtcon = 1,
296 .max_cpus = 255,
297 .is_default = 1,
300 static void s390_machine_init(void)
302 qemu_register_machine(&s390_machine);
305 machine_init(s390_machine_init);