]> 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 "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"
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
42 //#define DEBUG_S390
43
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
51
52 #define MAX_BLK_DEVS                    10
53 #define ZIPL_FILENAME                   "s390-zipl.rom"
54 #define TYPE_S390_MACHINE               "s390-machine"
55
56 static VirtIOS390Bus *s390_bus;
57 static S390CPU **ipi_states;
58
59 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
60 {
61     if (cpu_addr >= smp_cpus) {
62         return NULL;
63     }
64
65     return ipi_states[cpu_addr];
66 }
67
68 static int s390_virtio_hcall_notify(const uint64_t *args)
69 {
70     uint64_t mem = args[0];
71     int r = 0, i;
72
73     if (mem > ram_size) {
74         VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
75         if (dev) {
76             virtio_queue_notify(dev->vdev, i);
77         } else {
78             r = -EINVAL;
79         }
80     } else {
81         /* Early printk */
82     }
83     return r;
84 }
85
86 static int s390_virtio_hcall_reset(const uint64_t *args)
87 {
88     uint64_t mem = args[0];
89     VirtIOS390Device *dev;
90
91     dev = s390_virtio_bus_find_mem(s390_bus, mem);
92     if (dev == NULL) {
93         return -EINVAL;
94     }
95     virtio_reset(dev->vdev);
96     stb_phys(&address_space_memory, dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
97     s390_virtio_device_sync(dev);
98     s390_virtio_reset_idx(dev);
99
100     return 0;
101 }
102
103 static int s390_virtio_hcall_set_status(const uint64_t *args)
104 {
105     uint64_t mem = args[0];
106     int r = 0;
107     VirtIOS390Device *dev;
108
109     dev = s390_virtio_bus_find_mem(s390_bus, mem);
110     if (dev) {
111         s390_virtio_device_update_status(dev);
112     } else {
113         r = -EINVAL;
114     }
115     return r;
116 }
117
118 static void s390_virtio_register_hcalls(void)
119 {
120     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
121                                    s390_virtio_hcall_notify);
122     s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
123                                    s390_virtio_hcall_reset);
124     s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
125                                    s390_virtio_hcall_set_status);
126 }
127
128 /*
129  * The number of running CPUs. On s390 a shutdown is the state of all CPUs
130  * being either stopped or disabled (for interrupts) waiting. We have to
131  * track this number to call the shutdown sequence accordingly. This
132  * number is modified either on startup or while holding the big qemu lock.
133  */
134 static unsigned s390_running_cpus;
135
136 void s390_add_running_cpu(S390CPU *cpu)
137 {
138     CPUState *cs = CPU(cpu);
139
140     if (cs->halted) {
141         s390_running_cpus++;
142         cs->halted = 0;
143         cs->exception_index = -1;
144     }
145 }
146
147 unsigned s390_del_running_cpu(S390CPU *cpu)
148 {
149     CPUState *cs = CPU(cpu);
150
151     if (cs->halted == 0) {
152         assert(s390_running_cpus >= 1);
153         s390_running_cpus--;
154         cs->halted = 1;
155         cs->exception_index = EXCP_HLT;
156     }
157     return s390_running_cpus;
158 }
159
160 void s390_init_ipl_dev(const char *kernel_filename,
161                        const char *kernel_cmdline,
162                        const char *initrd_filename,
163                        const char *firmware)
164 {
165     DeviceState *dev;
166
167     dev  = qdev_create(NULL, "s390-ipl");
168     if (kernel_filename) {
169         qdev_prop_set_string(dev, "kernel", kernel_filename);
170     }
171     if (initrd_filename) {
172         qdev_prop_set_string(dev, "initrd", initrd_filename);
173     }
174     qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
175     qdev_prop_set_string(dev, "firmware", firmware);
176     qdev_init_nofail(dev);
177 }
178
179 void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
180 {
181     int i;
182
183     if (cpu_model == NULL) {
184         cpu_model = "host";
185     }
186
187     ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
188
189     for (i = 0; i < smp_cpus; i++) {
190         S390CPU *cpu;
191         CPUState *cs;
192
193         cpu = cpu_s390x_init(cpu_model);
194         cs = CPU(cpu);
195
196         ipi_states[i] = cpu;
197         cs->halted = 1;
198         cs->exception_index = EXCP_HLT;
199         cpu->env.storage_keys = storage_keys;
200     }
201 }
202
203
204 void s390_create_virtio_net(BusState *bus, const char *name)
205 {
206     int i;
207
208     for (i = 0; i < nb_nics; i++) {
209         NICInfo *nd = &nd_table[i];
210         DeviceState *dev;
211
212         if (!nd->model) {
213             nd->model = g_strdup("virtio");
214         }
215
216         if (strcmp(nd->model, "virtio")) {
217             fprintf(stderr, "S390 only supports VirtIO nics\n");
218             exit(1);
219         }
220
221         dev = qdev_create(bus, name);
222         qdev_set_nic_properties(dev, nd);
223         qdev_init_nofail(dev);
224     }
225 }
226
227 /* PC hardware initialisation */
228 static void s390_init(MachineState *machine)
229 {
230     ram_addr_t my_ram_size = machine->ram_size;
231     MemoryRegion *sysmem = get_system_memory();
232     MemoryRegion *ram = g_new(MemoryRegion, 1);
233     int increment_size = 20;
234     uint8_t *storage_keys;
235     void *virtio_region;
236     hwaddr virtio_region_len;
237     hwaddr virtio_region_start;
238
239     /*
240      * The storage increment size is a multiple of 1M and is a power of 2.
241      * The number of storage increments must be MAX_STORAGE_INCREMENTS or
242      * fewer.
243      */
244     while ((my_ram_size >> increment_size) > MAX_STORAGE_INCREMENTS) {
245         increment_size++;
246     }
247     my_ram_size = my_ram_size >> increment_size << increment_size;
248
249     /* let's propagate the changed ram size into the global variable. */
250     ram_size = my_ram_size;
251
252     /* get a BUS */
253     s390_bus = s390_virtio_bus_init(&my_ram_size);
254     s390_sclp_init();
255     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
256                       machine->initrd_filename, ZIPL_FILENAME);
257     s390_flic_init();
258
259     /* register hypercalls */
260     s390_virtio_register_hcalls();
261
262     /* allocate RAM */
263     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size, &error_abort);
264     vmstate_register_ram_global(ram);
265     memory_region_add_subregion(sysmem, 0, ram);
266
267     /* clear virtio region */
268     virtio_region_len = my_ram_size - ram_size;
269     virtio_region_start = ram_size;
270     virtio_region = cpu_physical_memory_map(virtio_region_start,
271                                             &virtio_region_len, true);
272     memset(virtio_region, 0, virtio_region_len);
273     cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
274                               virtio_region_len);
275
276     /* allocate storage keys */
277     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
278
279     /* init CPUs */
280     s390_init_cpus(machine->cpu_model, storage_keys);
281
282     /* Create VirtIO network adapters */
283     s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
284 }
285
286 void s390_nmi(NMIState *n, int cpu_index, Error **errp)
287 {
288     CPUState *cs = qemu_get_cpu(cpu_index);
289
290     if (s390_cpu_restart(S390_CPU(cs))) {
291         error_set(errp, QERR_UNSUPPORTED);
292     }
293 }
294
295 static void s390_machine_class_init(ObjectClass *oc, void *data)
296 {
297     MachineClass *mc = MACHINE_CLASS(oc);
298     NMIClass *nc = NMI_CLASS(oc);
299
300     mc->name = "s390-virtio";
301     mc->alias = "s390";
302     mc->desc = "VirtIO based S390 machine";
303     mc->init = s390_init;
304     mc->block_default_type = IF_VIRTIO;
305     mc->max_cpus = 255;
306     mc->no_serial = 1;
307     mc->no_parallel = 1;
308     mc->use_virtcon = 1;
309     mc->no_floppy = 1;
310     mc->no_cdrom = 1;
311     mc->no_sdcard = 1;
312     mc->is_default = 1;
313     nc->nmi_monitor_handler = s390_nmi;
314 }
315
316 static const TypeInfo s390_machine_info = {
317     .name          = TYPE_S390_MACHINE,
318     .parent        = TYPE_MACHINE,
319     .class_init    = s390_machine_class_init,
320     .interfaces = (InterfaceInfo[]) {
321         { TYPE_NMI },
322         { }
323     },
324 };
325
326 static void s390_machine_register_types(void)
327 {
328     type_register_static(&s390_machine_info);
329 }
330
331 type_init(s390_machine_register_types)
This page took 0.044815 seconds and 4 git commands to generate.