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 "block/block.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-virtio.h"
44 #define dprintf(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #define dprintf(fmt, ...) \
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) {
62 return ipi_states[cpu_addr];
65 static int s390_virtio_hcall_notify(const uint64_t *args)
67 uint64_t mem = args[0];
71 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
73 virtio_queue_notify(dev->vdev, i);
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);
92 virtio_reset(dev->vdev);
93 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
94 s390_virtio_device_sync(dev);
95 s390_virtio_reset_idx(dev);
100 static int s390_virtio_hcall_set_status(const uint64_t *args)
102 uint64_t mem = args[0];
104 VirtIOS390Device *dev;
106 dev = s390_virtio_bus_find_mem(s390_bus, mem);
108 s390_virtio_device_update_status(dev);
115 static void s390_virtio_register_hcalls(void)
117 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
118 s390_virtio_hcall_notify);
119 s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
120 s390_virtio_hcall_reset);
121 s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
122 s390_virtio_hcall_set_status);
126 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
127 * being either stopped or disabled (for interrupts) waiting. We have to
128 * track this number to call the shutdown sequence accordingly. This
129 * number is modified either on startup or while holding the big qemu lock.
131 static unsigned s390_running_cpus;
133 void s390_add_running_cpu(S390CPU *cpu)
135 CPUState *cs = CPU(cpu);
136 CPUS390XState *env = &cpu->env;
141 env->exception_index = -1;
145 unsigned s390_del_running_cpu(S390CPU *cpu)
147 CPUState *cs = CPU(cpu);
148 CPUS390XState *env = &cpu->env;
150 if (cs->halted == 0) {
151 assert(s390_running_cpus >= 1);
154 env->exception_index = EXCP_HLT;
156 return s390_running_cpus;
159 void s390_init_ipl_dev(const char *kernel_filename,
160 const char *kernel_cmdline,
161 const char *initrd_filename)
165 dev = qdev_create(NULL, "s390-ipl");
166 if (kernel_filename) {
167 qdev_prop_set_string(dev, "kernel", kernel_filename);
169 if (initrd_filename) {
170 qdev_prop_set_string(dev, "initrd", initrd_filename);
172 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
173 qdev_init_nofail(dev);
176 void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
180 if (cpu_model == NULL) {
184 ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
186 for (i = 0; i < smp_cpus; i++) {
190 cpu = cpu_s390x_init(cpu_model);
195 cpu->env.exception_index = EXCP_HLT;
196 cpu->env.storage_keys = storage_keys;
201 void s390_create_virtio_net(BusState *bus, const char *name)
205 for (i = 0; i < nb_nics; i++) {
206 NICInfo *nd = &nd_table[i];
210 nd->model = g_strdup("virtio");
213 if (strcmp(nd->model, "virtio")) {
214 fprintf(stderr, "S390 only supports VirtIO nics\n");
218 dev = qdev_create(bus, name);
219 qdev_set_nic_properties(dev, nd);
220 qdev_init_nofail(dev);
224 /* PC hardware initialisation */
225 static void s390_init(QEMUMachineInitArgs *args)
227 ram_addr_t my_ram_size = args->ram_size;
228 MemoryRegion *sysmem = get_system_memory();
229 MemoryRegion *ram = g_new(MemoryRegion, 1);
231 uint8_t *storage_keys;
233 hwaddr virtio_region_len;
234 hwaddr virtio_region_start;
236 /* s390x ram size detection needs a 16bit multiplier + an increment. So
237 guests > 64GB can be specified in 2MB steps etc. */
238 while ((my_ram_size >> (20 + shift)) > 65535) {
241 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
243 /* let's propagate the changed ram size into the global variable. */
244 ram_size = my_ram_size;
247 s390_bus = s390_virtio_bus_init(&my_ram_size);
249 s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
250 args->initrd_filename);
252 /* register hypercalls */
253 s390_virtio_register_hcalls();
256 memory_region_init_ram(ram, "s390.ram", my_ram_size);
257 vmstate_register_ram_global(ram);
258 memory_region_add_subregion(sysmem, 0, ram);
260 /* clear virtio region */
261 virtio_region_len = my_ram_size - ram_size;
262 virtio_region_start = ram_size;
263 virtio_region = cpu_physical_memory_map(virtio_region_start,
264 &virtio_region_len, true);
265 memset(virtio_region, 0, virtio_region_len);
266 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
269 /* allocate storage keys */
270 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
273 s390_init_cpus(args->cpu_model, storage_keys);
275 /* Create VirtIO network adapters */
276 s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
279 static QEMUMachine s390_machine = {
280 .name = "s390-virtio",
282 .desc = "VirtIO based S390 machine",
284 .block_default_type = IF_VIRTIO,
293 DEFAULT_MACHINE_OPTIONS,
296 static void s390_machine_init(void)
298 qemu_register_machine(&s390_machine);
301 machine_init(s390_machine_init);