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