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