]>
Commit | Line | Data |
---|---|---|
8cb310e1 AG |
1 | /* |
2 | * QEMU S390 virtio target | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "hw.h" | |
21 | #include "block.h" | |
6c33286a | 22 | #include "blockdev.h" |
8cb310e1 AG |
23 | #include "sysemu.h" |
24 | #include "net.h" | |
25 | #include "boards.h" | |
26 | #include "monitor.h" | |
27 | #include "loader.h" | |
28 | #include "elf.h" | |
29 | #include "hw/virtio.h" | |
8cb310e1 AG |
30 | #include "hw/sysbus.h" |
31 | #include "kvm.h" | |
ca3dbc27 | 32 | #include "exec-memory.h" |
8cb310e1 AG |
33 | |
34 | #include "hw/s390-virtio-bus.h" | |
35 | ||
36 | //#define DEBUG_S390 | |
37 | ||
38 | #ifdef DEBUG_S390 | |
39 | #define dprintf(fmt, ...) \ | |
40 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) | |
41 | #else | |
42 | #define dprintf(fmt, ...) \ | |
43 | do { } while (0) | |
44 | #endif | |
45 | ||
46 | #define KVM_S390_VIRTIO_NOTIFY 0 | |
47 | #define KVM_S390_VIRTIO_RESET 1 | |
48 | #define KVM_S390_VIRTIO_SET_STATUS 2 | |
49 | ||
50 | #define KERN_IMAGE_START 0x010000UL | |
51 | #define KERN_PARM_AREA 0x010480UL | |
52 | #define INITRD_START 0x800000UL | |
53 | #define INITRD_PARM_START 0x010408UL | |
54 | #define INITRD_PARM_SIZE 0x010410UL | |
55 | #define PARMFILE_START 0x001000UL | |
56 | ||
fe270d04 AG |
57 | #define ZIPL_START 0x009000UL |
58 | #define ZIPL_LOAD_ADDR 0x009000UL | |
59 | #define ZIPL_FILENAME "s390-zipl.rom" | |
60 | ||
8cb310e1 AG |
61 | #define MAX_BLK_DEVS 10 |
62 | ||
63 | static VirtIOS390Bus *s390_bus; | |
0e4213a7 | 64 | static CPUS390XState **ipi_states; |
8cb310e1 | 65 | |
0e4213a7 | 66 | CPUS390XState *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 | ||
0e4213a7 | 75 | int s390_virtio_hypercall(CPUS390XState *env, uint64_t mem, uint64_t hypercall) |
8cb310e1 AG |
76 | { |
77 | int r = 0, i; | |
8cb310e1 | 78 | |
8d5192ee AG |
79 | dprintf("KVM hypercall: %ld\n", hypercall); |
80 | switch (hypercall) { | |
8cb310e1 AG |
81 | case KVM_S390_VIRTIO_NOTIFY: |
82 | if (mem > ram_size) { | |
83 | VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, | |
84 | mem, &i); | |
85 | if (dev) { | |
86 | virtio_queue_notify(dev->vdev, i); | |
87 | } else { | |
88 | r = -EINVAL; | |
89 | } | |
90 | } else { | |
91 | /* Early printk */ | |
92 | } | |
93 | break; | |
94 | case KVM_S390_VIRTIO_RESET: | |
95 | { | |
baf0b55a AG |
96 | VirtIOS390Device *dev; |
97 | ||
98 | dev = s390_virtio_bus_find_mem(s390_bus, mem); | |
99 | virtio_reset(dev->vdev); | |
e9d86b76 | 100 | stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0); |
baf0b55a | 101 | s390_virtio_device_sync(dev); |
4170aea1 | 102 | s390_virtio_reset_idx(dev); |
8cb310e1 AG |
103 | break; |
104 | } | |
105 | case KVM_S390_VIRTIO_SET_STATUS: | |
106 | { | |
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 | break; | |
116 | } | |
117 | default: | |
118 | r = -EINVAL; | |
119 | break; | |
120 | } | |
121 | ||
8d5192ee | 122 | return r; |
8cb310e1 AG |
123 | } |
124 | ||
854e42f3 CB |
125 | /* |
126 | * The number of running CPUs. On s390 a shutdown is the state of all CPUs | |
127 | * being either stopped or disabled (for interrupts) waiting. We have to | |
128 | * track this number to call the shutdown sequence accordingly. This | |
129 | * number is modified either on startup or while holding the big qemu lock. | |
130 | */ | |
131 | static unsigned s390_running_cpus; | |
132 | ||
0e4213a7 | 133 | void s390_add_running_cpu(CPUS390XState *env) |
854e42f3 CB |
134 | { |
135 | if (env->halted) { | |
136 | s390_running_cpus++; | |
137 | env->halted = 0; | |
138 | env->exception_index = -1; | |
139 | } | |
140 | } | |
141 | ||
0e4213a7 | 142 | unsigned s390_del_running_cpu(CPUS390XState *env) |
854e42f3 CB |
143 | { |
144 | if (env->halted == 0) { | |
145 | assert(s390_running_cpus >= 1); | |
146 | s390_running_cpus--; | |
147 | env->halted = 1; | |
148 | env->exception_index = EXCP_HLT; | |
149 | } | |
150 | return s390_running_cpus; | |
151 | } | |
152 | ||
8cb310e1 | 153 | /* PC hardware initialisation */ |
22486aa0 | 154 | static void s390_init(ram_addr_t my_ram_size, |
8cb310e1 AG |
155 | const char *boot_device, |
156 | const char *kernel_filename, | |
157 | const char *kernel_cmdline, | |
158 | const char *initrd_filename, | |
159 | const char *cpu_model) | |
160 | { | |
0e4213a7 | 161 | CPUS390XState *env = NULL; |
ca3dbc27 AK |
162 | MemoryRegion *sysmem = get_system_memory(); |
163 | MemoryRegion *ram = g_new(MemoryRegion, 1); | |
8cb310e1 AG |
164 | ram_addr_t kernel_size = 0; |
165 | ram_addr_t initrd_offset; | |
166 | ram_addr_t initrd_size = 0; | |
22486aa0 | 167 | int shift = 0; |
8d5192ee | 168 | uint8_t *storage_keys; |
326384d5 AG |
169 | void *virtio_region; |
170 | target_phys_addr_t virtio_region_len; | |
171 | target_phys_addr_t virtio_region_start; | |
8cb310e1 AG |
172 | int i; |
173 | ||
22486aa0 CB |
174 | /* s390x ram size detection needs a 16bit multiplier + an increment. So |
175 | guests > 64GB can be specified in 2MB steps etc. */ | |
176 | while ((my_ram_size >> (20 + shift)) > 65535) { | |
177 | shift++; | |
178 | } | |
179 | my_ram_size = my_ram_size >> (20 + shift) << (20 + shift); | |
180 | ||
181 | /* lets propagate the changed ram size into the global variable. */ | |
182 | ram_size = my_ram_size; | |
e249651c | 183 | |
8cb310e1 | 184 | /* get a BUS */ |
22486aa0 | 185 | s390_bus = s390_virtio_bus_init(&my_ram_size); |
8cb310e1 AG |
186 | |
187 | /* allocate RAM */ | |
c5705a77 AK |
188 | memory_region_init_ram(ram, "s390.ram", my_ram_size); |
189 | vmstate_register_ram_global(ram); | |
ca3dbc27 | 190 | memory_region_add_subregion(sysmem, 0, ram); |
8cb310e1 | 191 | |
326384d5 AG |
192 | /* clear virtio region */ |
193 | virtio_region_len = my_ram_size - ram_size; | |
194 | virtio_region_start = ram_size; | |
195 | virtio_region = cpu_physical_memory_map(virtio_region_start, | |
196 | &virtio_region_len, true); | |
197 | memset(virtio_region, 0, virtio_region_len); | |
198 | cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1, | |
199 | virtio_region_len); | |
200 | ||
8d5192ee | 201 | /* allocate storage keys */ |
7267c094 | 202 | storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE); |
8d5192ee | 203 | |
8cb310e1 AG |
204 | /* init CPUs */ |
205 | if (cpu_model == NULL) { | |
206 | cpu_model = "host"; | |
207 | } | |
208 | ||
0e4213a7 | 209 | ipi_states = g_malloc(sizeof(CPUS390XState *) * smp_cpus); |
8cb310e1 AG |
210 | |
211 | for (i = 0; i < smp_cpus; i++) { | |
0e4213a7 | 212 | CPUS390XState *tmp_env; |
8cb310e1 AG |
213 | |
214 | tmp_env = cpu_init(cpu_model); | |
215 | if (!env) { | |
216 | env = tmp_env; | |
217 | } | |
218 | ipi_states[i] = tmp_env; | |
219 | tmp_env->halted = 1; | |
220 | tmp_env->exception_index = EXCP_HLT; | |
8d5192ee | 221 | tmp_env->storage_keys = storage_keys; |
8cb310e1 AG |
222 | } |
223 | ||
854e42f3 CB |
224 | /* One CPU has to run */ |
225 | s390_add_running_cpu(env); | |
8cb310e1 AG |
226 | |
227 | if (kernel_filename) { | |
8cb310e1 | 228 | |
1edb4934 CB |
229 | kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, NULL, |
230 | NULL, 1, ELF_MACHINE, 0); | |
231 | if (kernel_size == -1UL) { | |
232 | kernel_size = load_image_targphys(kernel_filename, 0, ram_size); | |
8cb310e1 | 233 | } |
118a8977 CB |
234 | if (kernel_size == -1UL) { |
235 | fprintf(stderr, "qemu: could not load kernel '%s'\n", | |
236 | kernel_filename); | |
237 | exit(1); | |
238 | } | |
1edb4934 CB |
239 | /* |
240 | * we can not rely on the ELF entry point, since up to 3.2 this | |
241 | * value was 0x800 (the SALIPL loader) and it wont work. For | |
242 | * all (Linux) cases 0x10000 (KERN_IMAGE_START) should be fine. | |
243 | */ | |
8cb310e1 | 244 | env->psw.addr = KERN_IMAGE_START; |
0435d393 | 245 | env->psw.mask = 0x0000000180000000ULL; |
fe270d04 AG |
246 | } else { |
247 | ram_addr_t bios_size = 0; | |
248 | char *bios_filename; | |
249 | ||
250 | /* Load zipl bootloader */ | |
251 | if (bios_name == NULL) { | |
252 | bios_name = ZIPL_FILENAME; | |
253 | } | |
254 | ||
255 | bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); | |
1edb4934 | 256 | bios_size = load_image_targphys(bios_filename, ZIPL_LOAD_ADDR, 4096); |
7267c094 | 257 | g_free(bios_filename); |
fe270d04 AG |
258 | |
259 | if ((long)bios_size < 0) { | |
260 | hw_error("could not load bootloader '%s'\n", bios_name); | |
261 | } | |
262 | ||
263 | if (bios_size > 4096) { | |
264 | hw_error("stage1 bootloader is > 4k\n"); | |
265 | } | |
266 | ||
267 | env->psw.addr = ZIPL_START; | |
268 | env->psw.mask = 0x0000000180000000ULL; | |
8cb310e1 AG |
269 | } |
270 | ||
271 | if (initrd_filename) { | |
272 | initrd_offset = INITRD_START; | |
273 | while (kernel_size + 0x100000 > initrd_offset) { | |
274 | initrd_offset += 0x100000; | |
275 | } | |
1edb4934 CB |
276 | initrd_size = load_image_targphys(initrd_filename, initrd_offset, |
277 | ram_size - initrd_offset); | |
118a8977 CB |
278 | if (initrd_size == -1UL) { |
279 | fprintf(stderr, "qemu: could not load initrd '%s'\n", | |
280 | initrd_filename); | |
281 | exit(1); | |
282 | } | |
283 | ||
1edb4934 CB |
284 | /* we have to overwrite values in the kernel image, which are "rom" */ |
285 | memcpy(rom_ptr(INITRD_PARM_START), &initrd_offset, 8); | |
286 | memcpy(rom_ptr(INITRD_PARM_SIZE), &initrd_size, 8); | |
8cb310e1 AG |
287 | } |
288 | ||
cc3c7384 | 289 | if (rom_ptr(KERN_PARM_AREA)) { |
1edb4934 CB |
290 | /* we have to overwrite values in the kernel image, which are "rom" */ |
291 | memcpy(rom_ptr(KERN_PARM_AREA), kernel_cmdline, | |
292 | strlen(kernel_cmdline) + 1); | |
8cb310e1 AG |
293 | } |
294 | ||
8cb310e1 AG |
295 | /* Create VirtIO network adapters */ |
296 | for(i = 0; i < nb_nics; i++) { | |
297 | NICInfo *nd = &nd_table[i]; | |
298 | DeviceState *dev; | |
299 | ||
300 | if (!nd->model) { | |
7267c094 | 301 | nd->model = g_strdup("virtio"); |
8cb310e1 AG |
302 | } |
303 | ||
304 | if (strcmp(nd->model, "virtio")) { | |
305 | fprintf(stderr, "S390 only supports VirtIO nics\n"); | |
306 | exit(1); | |
307 | } | |
308 | ||
309 | dev = qdev_create((BusState *)s390_bus, "virtio-net-s390"); | |
310 | qdev_set_nic_properties(dev, nd); | |
311 | qdev_init_nofail(dev); | |
312 | } | |
313 | ||
314 | /* Create VirtIO disk drives */ | |
315 | for(i = 0; i < MAX_BLK_DEVS; i++) { | |
316 | DriveInfo *dinfo; | |
317 | DeviceState *dev; | |
318 | ||
319 | dinfo = drive_get(IF_IDE, 0, i); | |
320 | if (!dinfo) { | |
321 | continue; | |
322 | } | |
323 | ||
324 | dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390"); | |
18846dee | 325 | qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv); |
8cb310e1 AG |
326 | qdev_init_nofail(dev); |
327 | } | |
328 | } | |
329 | ||
330 | static QEMUMachine s390_machine = { | |
331 | .name = "s390-virtio", | |
332 | .alias = "s390", | |
333 | .desc = "VirtIO based S390 machine", | |
334 | .init = s390_init, | |
ad0bbc56 EL |
335 | .no_cdrom = 1, |
336 | .no_floppy = 1, | |
986c5f78 GH |
337 | .no_serial = 1, |
338 | .no_parallel = 1, | |
ad0bbc56 | 339 | .no_sdcard = 1, |
cf708987 | 340 | .use_virtcon = 1, |
8cb310e1 AG |
341 | .max_cpus = 255, |
342 | .is_default = 1, | |
343 | }; | |
344 | ||
345 | static void s390_machine_init(void) | |
346 | { | |
347 | qemu_register_machine(&s390_machine); | |
348 | } | |
349 | ||
350 | machine_init(s390_machine_init); |