]> Git Repo - qemu.git/blob - hw/s390x/s390-virtio.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / hw / s390x / s390-virtio.c
1 /*
2  * QEMU S390 virtio target
3  *
4  * Copyright (c) 2009 Alexander Graf <[email protected]>
5  * Copyright IBM Corp 2012
6  *
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.
11  *
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.
16  *
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.
19  *
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/>.
22  */
23
24 #include "hw/hw.h"
25 #include "sysemu/block-backend.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"
36
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"
41 #include "cpu.h"
42
43 //#define DEBUG_S390
44
45 #ifdef DEBUG_S390
46 #define DPRINTF(fmt, ...) \
47     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
48 #else
49 #define DPRINTF(fmt, ...) \
50     do { } while (0)
51 #endif
52
53 #define MAX_BLK_DEVS                    10
54 #define ZIPL_FILENAME                   "s390-zipl.rom"
55 #define TYPE_S390_MACHINE               "s390-machine"
56
57 #define S390_TOD_CLOCK_VALUE_MISSING    0x00
58 #define S390_TOD_CLOCK_VALUE_PRESENT    0x01
59
60 static VirtIOS390Bus *s390_bus;
61 static S390CPU **ipi_states;
62
63 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
64 {
65     if (cpu_addr >= smp_cpus) {
66         return NULL;
67     }
68
69     return ipi_states[cpu_addr];
70 }
71
72 static int s390_virtio_hcall_notify(const uint64_t *args)
73 {
74     uint64_t mem = args[0];
75     int r = 0, i;
76
77     if (mem > ram_size) {
78         VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
79         if (dev) {
80             virtio_queue_notify(dev->vdev, i);
81         } else {
82             r = -EINVAL;
83         }
84     } else {
85         /* Early printk */
86     }
87     return r;
88 }
89
90 static int s390_virtio_hcall_reset(const uint64_t *args)
91 {
92     uint64_t mem = args[0];
93     VirtIOS390Device *dev;
94
95     dev = s390_virtio_bus_find_mem(s390_bus, mem);
96     if (dev == NULL) {
97         return -EINVAL;
98     }
99     virtio_reset(dev->vdev);
100     stb_phys(&address_space_memory, dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
101     s390_virtio_device_sync(dev);
102     s390_virtio_reset_idx(dev);
103
104     return 0;
105 }
106
107 static int s390_virtio_hcall_set_status(const uint64_t *args)
108 {
109     uint64_t mem = args[0];
110     int r = 0;
111     VirtIOS390Device *dev;
112
113     dev = s390_virtio_bus_find_mem(s390_bus, mem);
114     if (dev) {
115         s390_virtio_device_update_status(dev);
116     } else {
117         r = -EINVAL;
118     }
119     return r;
120 }
121
122 static void s390_virtio_register_hcalls(void)
123 {
124     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
125                                    s390_virtio_hcall_notify);
126     s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
127                                    s390_virtio_hcall_reset);
128     s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
129                                    s390_virtio_hcall_set_status);
130 }
131
132 void s390_init_ipl_dev(const char *kernel_filename,
133                        const char *kernel_cmdline,
134                        const char *initrd_filename,
135                        const char *firmware,
136                        bool enforce_bios)
137 {
138     DeviceState *dev;
139
140     dev  = qdev_create(NULL, "s390-ipl");
141     if (kernel_filename) {
142         qdev_prop_set_string(dev, "kernel", kernel_filename);
143     }
144     if (initrd_filename) {
145         qdev_prop_set_string(dev, "initrd", initrd_filename);
146     }
147     qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
148     qdev_prop_set_string(dev, "firmware", firmware);
149     qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
150     object_property_add_child(qdev_get_machine(), "s390-ipl",
151                               OBJECT(dev), NULL);
152     qdev_init_nofail(dev);
153 }
154
155 void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
156 {
157     int i;
158
159     if (cpu_model == NULL) {
160         cpu_model = "host";
161     }
162
163     ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
164
165     for (i = 0; i < smp_cpus; i++) {
166         S390CPU *cpu;
167         CPUState *cs;
168
169         cpu = cpu_s390x_init(cpu_model);
170         cs = CPU(cpu);
171
172         ipi_states[i] = cpu;
173         cs->halted = 1;
174         cs->exception_index = EXCP_HLT;
175         cpu->env.storage_keys = storage_keys;
176     }
177 }
178
179
180 void s390_create_virtio_net(BusState *bus, const char *name)
181 {
182     int i;
183
184     for (i = 0; i < nb_nics; i++) {
185         NICInfo *nd = &nd_table[i];
186         DeviceState *dev;
187
188         if (!nd->model) {
189             nd->model = g_strdup("virtio");
190         }
191
192         if (strcmp(nd->model, "virtio")) {
193             fprintf(stderr, "S390 only supports VirtIO nics\n");
194             exit(1);
195         }
196
197         dev = qdev_create(bus, name);
198         qdev_set_nic_properties(dev, nd);
199         qdev_init_nofail(dev);
200     }
201 }
202
203 void gtod_save(QEMUFile *f, void *opaque)
204 {
205     uint64_t tod_low;
206     uint8_t tod_high;
207     int r;
208
209     r = s390_get_clock(&tod_high, &tod_low);
210     if (r) {
211         fprintf(stderr, "WARNING: Unable to get guest clock for migration. "
212                         "Error code %d. Guest clock will not be migrated "
213                         "which could cause the guest to hang.\n", r);
214         qemu_put_byte(f, S390_TOD_CLOCK_VALUE_MISSING);
215         return;
216     }
217
218     qemu_put_byte(f, S390_TOD_CLOCK_VALUE_PRESENT);
219     qemu_put_byte(f, tod_high);
220     qemu_put_be64(f, tod_low);
221 }
222
223 int gtod_load(QEMUFile *f, void *opaque, int version_id)
224 {
225     uint64_t tod_low;
226     uint8_t tod_high;
227     int r;
228
229     if (qemu_get_byte(f) == S390_TOD_CLOCK_VALUE_MISSING) {
230         fprintf(stderr, "WARNING: Guest clock was not migrated. This could "
231                         "cause the guest to hang.\n");
232         return 0;
233     }
234
235     tod_high = qemu_get_byte(f);
236     tod_low = qemu_get_be64(f);
237
238     r = s390_set_clock(&tod_high, &tod_low);
239     if (r) {
240         fprintf(stderr, "WARNING: Unable to set guest clock value. "
241                         "s390_get_clock returned error %d. This could cause "
242                         "the guest to hang.\n", r);
243     }
244
245     return 0;
246 }
247
248 /* PC hardware initialisation */
249 static void s390_init(MachineState *machine)
250 {
251     ram_addr_t my_ram_size = machine->ram_size;
252     MemoryRegion *sysmem = get_system_memory();
253     MemoryRegion *ram = g_new(MemoryRegion, 1);
254     int increment_size = 20;
255     uint8_t *storage_keys;
256     void *virtio_region;
257     hwaddr virtio_region_len;
258     hwaddr virtio_region_start;
259
260     /*
261      * The storage increment size is a multiple of 1M and is a power of 2.
262      * The number of storage increments must be MAX_STORAGE_INCREMENTS or
263      * fewer.
264      */
265     while ((my_ram_size >> increment_size) > MAX_STORAGE_INCREMENTS) {
266         increment_size++;
267     }
268     my_ram_size = my_ram_size >> increment_size << increment_size;
269
270     /* let's propagate the changed ram size into the global variable. */
271     ram_size = my_ram_size;
272
273     /* get a BUS */
274     s390_bus = s390_virtio_bus_init(&my_ram_size);
275     s390_sclp_init();
276     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
277                       machine->initrd_filename, ZIPL_FILENAME, false);
278     s390_flic_init();
279
280     /* register hypercalls */
281     s390_virtio_register_hcalls();
282
283     /* allocate RAM */
284     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size, &error_abort);
285     vmstate_register_ram_global(ram);
286     memory_region_add_subregion(sysmem, 0, ram);
287
288     /* clear virtio region */
289     virtio_region_len = my_ram_size - ram_size;
290     virtio_region_start = ram_size;
291     virtio_region = cpu_physical_memory_map(virtio_region_start,
292                                             &virtio_region_len, true);
293     memset(virtio_region, 0, virtio_region_len);
294     cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
295                               virtio_region_len);
296
297     /* allocate storage keys */
298     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
299
300     /* init CPUs */
301     s390_init_cpus(machine->cpu_model, storage_keys);
302
303     /* Create VirtIO network adapters */
304     s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
305
306     /* Register savevm handler for guest TOD clock */
307     register_savevm(NULL, "todclock", 0, 1, gtod_save, gtod_load, NULL);
308 }
309
310 void s390_nmi(NMIState *n, int cpu_index, Error **errp)
311 {
312     CPUState *cs = qemu_get_cpu(cpu_index);
313
314     if (s390_cpu_restart(S390_CPU(cs))) {
315         error_set(errp, QERR_UNSUPPORTED);
316     }
317 }
318
319 static void s390_machine_class_init(ObjectClass *oc, void *data)
320 {
321     MachineClass *mc = MACHINE_CLASS(oc);
322     NMIClass *nc = NMI_CLASS(oc);
323
324     mc->name = "s390-virtio";
325     mc->alias = "s390";
326     mc->desc = "VirtIO based S390 machine";
327     mc->init = s390_init;
328     mc->block_default_type = IF_VIRTIO;
329     mc->max_cpus = 255;
330     mc->no_serial = 1;
331     mc->no_parallel = 1;
332     mc->use_virtcon = 1;
333     mc->no_floppy = 1;
334     mc->no_cdrom = 1;
335     mc->no_sdcard = 1;
336     mc->is_default = 1;
337     nc->nmi_monitor_handler = s390_nmi;
338 }
339
340 static const TypeInfo s390_machine_info = {
341     .name          = TYPE_S390_MACHINE,
342     .parent        = TYPE_MACHINE,
343     .class_init    = s390_machine_class_init,
344     .interfaces = (InterfaceInfo[]) {
345         { TYPE_NMI },
346         { }
347     },
348 };
349
350 static void s390_machine_register_types(void)
351 {
352     type_register_static(&s390_machine_info);
353 }
354
355 type_init(s390_machine_register_types)
This page took 0.042499 seconds and 4 git commands to generate.