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