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