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