]>
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" | |
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" | |
32 | ||
33 | #include "hw/s390-virtio-bus.h" | |
34 | ||
35 | //#define DEBUG_S390 | |
36 | ||
37 | #ifdef DEBUG_S390 | |
38 | #define dprintf(fmt, ...) \ | |
39 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) | |
40 | #else | |
41 | #define dprintf(fmt, ...) \ | |
42 | do { } while (0) | |
43 | #endif | |
44 | ||
45 | #define KVM_S390_VIRTIO_NOTIFY 0 | |
46 | #define KVM_S390_VIRTIO_RESET 1 | |
47 | #define KVM_S390_VIRTIO_SET_STATUS 2 | |
48 | ||
49 | #define KERN_IMAGE_START 0x010000UL | |
50 | #define KERN_PARM_AREA 0x010480UL | |
51 | #define INITRD_START 0x800000UL | |
52 | #define INITRD_PARM_START 0x010408UL | |
53 | #define INITRD_PARM_SIZE 0x010410UL | |
54 | #define PARMFILE_START 0x001000UL | |
55 | ||
fe270d04 AG |
56 | #define ZIPL_START 0x009000UL |
57 | #define ZIPL_LOAD_ADDR 0x009000UL | |
58 | #define ZIPL_FILENAME "s390-zipl.rom" | |
59 | ||
8cb310e1 AG |
60 | #define MAX_BLK_DEVS 10 |
61 | ||
62 | static VirtIOS390Bus *s390_bus; | |
63 | static CPUState **ipi_states; | |
64 | ||
8cb310e1 AG |
65 | CPUState *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 | ||
8d5192ee | 74 | int s390_virtio_hypercall(CPUState *env, uint64_t mem, uint64_t hypercall) |
8cb310e1 AG |
75 | { |
76 | int r = 0, i; | |
8cb310e1 | 77 | |
8d5192ee AG |
78 | dprintf("KVM hypercall: %ld\n", hypercall); |
79 | switch (hypercall) { | |
8cb310e1 AG |
80 | case KVM_S390_VIRTIO_NOTIFY: |
81 | if (mem > ram_size) { | |
82 | VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, | |
83 | mem, &i); | |
84 | if (dev) { | |
85 | virtio_queue_notify(dev->vdev, i); | |
86 | } else { | |
87 | r = -EINVAL; | |
88 | } | |
89 | } else { | |
90 | /* Early printk */ | |
91 | } | |
92 | break; | |
93 | case KVM_S390_VIRTIO_RESET: | |
94 | { | |
baf0b55a AG |
95 | VirtIOS390Device *dev; |
96 | ||
97 | dev = s390_virtio_bus_find_mem(s390_bus, mem); | |
98 | virtio_reset(dev->vdev); | |
99 | s390_virtio_device_sync(dev); | |
8cb310e1 AG |
100 | break; |
101 | } | |
102 | case KVM_S390_VIRTIO_SET_STATUS: | |
103 | { | |
104 | VirtIOS390Device *dev; | |
105 | ||
106 | dev = s390_virtio_bus_find_mem(s390_bus, mem); | |
107 | if (dev) { | |
108 | s390_virtio_device_update_status(dev); | |
109 | } else { | |
110 | r = -EINVAL; | |
111 | } | |
112 | break; | |
113 | } | |
114 | default: | |
115 | r = -EINVAL; | |
116 | break; | |
117 | } | |
118 | ||
8d5192ee | 119 | return r; |
8cb310e1 AG |
120 | } |
121 | ||
122 | /* PC hardware initialisation */ | |
22486aa0 | 123 | static void s390_init(ram_addr_t my_ram_size, |
8cb310e1 AG |
124 | const char *boot_device, |
125 | const char *kernel_filename, | |
126 | const char *kernel_cmdline, | |
127 | const char *initrd_filename, | |
128 | const char *cpu_model) | |
129 | { | |
130 | CPUState *env = NULL; | |
131 | ram_addr_t ram_addr; | |
132 | ram_addr_t kernel_size = 0; | |
133 | ram_addr_t initrd_offset; | |
134 | ram_addr_t initrd_size = 0; | |
22486aa0 | 135 | int shift = 0; |
8d5192ee | 136 | uint8_t *storage_keys; |
8cb310e1 AG |
137 | int i; |
138 | ||
22486aa0 CB |
139 | /* s390x ram size detection needs a 16bit multiplier + an increment. So |
140 | guests > 64GB can be specified in 2MB steps etc. */ | |
141 | while ((my_ram_size >> (20 + shift)) > 65535) { | |
142 | shift++; | |
143 | } | |
144 | my_ram_size = my_ram_size >> (20 + shift) << (20 + shift); | |
145 | ||
146 | /* lets propagate the changed ram size into the global variable. */ | |
147 | ram_size = my_ram_size; | |
e249651c | 148 | |
8cb310e1 | 149 | /* get a BUS */ |
22486aa0 | 150 | s390_bus = s390_virtio_bus_init(&my_ram_size); |
8cb310e1 AG |
151 | |
152 | /* allocate RAM */ | |
22486aa0 CB |
153 | ram_addr = qemu_ram_alloc(NULL, "s390.ram", my_ram_size); |
154 | cpu_register_physical_memory(0, my_ram_size, ram_addr); | |
8cb310e1 | 155 | |
8d5192ee | 156 | /* allocate storage keys */ |
7267c094 | 157 | storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE); |
8d5192ee | 158 | |
8cb310e1 AG |
159 | /* init CPUs */ |
160 | if (cpu_model == NULL) { | |
161 | cpu_model = "host"; | |
162 | } | |
163 | ||
7267c094 | 164 | ipi_states = g_malloc(sizeof(CPUState *) * smp_cpus); |
8cb310e1 AG |
165 | |
166 | for (i = 0; i < smp_cpus; i++) { | |
167 | CPUState *tmp_env; | |
168 | ||
169 | tmp_env = cpu_init(cpu_model); | |
170 | if (!env) { | |
171 | env = tmp_env; | |
172 | } | |
173 | ipi_states[i] = tmp_env; | |
174 | tmp_env->halted = 1; | |
175 | tmp_env->exception_index = EXCP_HLT; | |
8d5192ee | 176 | tmp_env->storage_keys = storage_keys; |
8cb310e1 AG |
177 | } |
178 | ||
179 | env->halted = 0; | |
180 | env->exception_index = 0; | |
181 | ||
182 | if (kernel_filename) { | |
183 | kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0)); | |
184 | ||
04bc74ed | 185 | if (lduw_be_phys(KERN_IMAGE_START) != 0x0dd0) { |
8cb310e1 AG |
186 | fprintf(stderr, "Specified image is not an s390 boot image\n"); |
187 | exit(1); | |
188 | } | |
189 | ||
8cb310e1 | 190 | env->psw.addr = KERN_IMAGE_START; |
0435d393 | 191 | env->psw.mask = 0x0000000180000000ULL; |
fe270d04 AG |
192 | } else { |
193 | ram_addr_t bios_size = 0; | |
194 | char *bios_filename; | |
195 | ||
196 | /* Load zipl bootloader */ | |
197 | if (bios_name == NULL) { | |
198 | bios_name = ZIPL_FILENAME; | |
199 | } | |
200 | ||
201 | bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); | |
202 | bios_size = load_image(bios_filename, qemu_get_ram_ptr(ZIPL_LOAD_ADDR)); | |
7267c094 | 203 | g_free(bios_filename); |
fe270d04 AG |
204 | |
205 | if ((long)bios_size < 0) { | |
206 | hw_error("could not load bootloader '%s'\n", bios_name); | |
207 | } | |
208 | ||
209 | if (bios_size > 4096) { | |
210 | hw_error("stage1 bootloader is > 4k\n"); | |
211 | } | |
212 | ||
213 | env->psw.addr = ZIPL_START; | |
214 | env->psw.mask = 0x0000000180000000ULL; | |
8cb310e1 AG |
215 | } |
216 | ||
217 | if (initrd_filename) { | |
218 | initrd_offset = INITRD_START; | |
219 | while (kernel_size + 0x100000 > initrd_offset) { | |
220 | initrd_offset += 0x100000; | |
221 | } | |
222 | initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset)); | |
223 | ||
04bc74ed AG |
224 | stq_be_phys(INITRD_PARM_START, initrd_offset); |
225 | stq_be_phys(INITRD_PARM_SIZE, initrd_size); | |
8cb310e1 AG |
226 | } |
227 | ||
228 | if (kernel_cmdline) { | |
54f7b4a3 SW |
229 | cpu_physical_memory_write(KERN_PARM_AREA, kernel_cmdline, |
230 | strlen(kernel_cmdline)); | |
8cb310e1 AG |
231 | } |
232 | ||
8cb310e1 AG |
233 | /* Create VirtIO network adapters */ |
234 | for(i = 0; i < nb_nics; i++) { | |
235 | NICInfo *nd = &nd_table[i]; | |
236 | DeviceState *dev; | |
237 | ||
238 | if (!nd->model) { | |
7267c094 | 239 | nd->model = g_strdup("virtio"); |
8cb310e1 AG |
240 | } |
241 | ||
242 | if (strcmp(nd->model, "virtio")) { | |
243 | fprintf(stderr, "S390 only supports VirtIO nics\n"); | |
244 | exit(1); | |
245 | } | |
246 | ||
247 | dev = qdev_create((BusState *)s390_bus, "virtio-net-s390"); | |
248 | qdev_set_nic_properties(dev, nd); | |
249 | qdev_init_nofail(dev); | |
250 | } | |
251 | ||
252 | /* Create VirtIO disk drives */ | |
253 | for(i = 0; i < MAX_BLK_DEVS; i++) { | |
254 | DriveInfo *dinfo; | |
255 | DeviceState *dev; | |
256 | ||
257 | dinfo = drive_get(IF_IDE, 0, i); | |
258 | if (!dinfo) { | |
259 | continue; | |
260 | } | |
261 | ||
262 | dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390"); | |
18846dee | 263 | qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv); |
8cb310e1 AG |
264 | qdev_init_nofail(dev); |
265 | } | |
266 | } | |
267 | ||
268 | static QEMUMachine s390_machine = { | |
269 | .name = "s390-virtio", | |
270 | .alias = "s390", | |
271 | .desc = "VirtIO based S390 machine", | |
272 | .init = s390_init, | |
986c5f78 GH |
273 | .no_serial = 1, |
274 | .no_parallel = 1, | |
cf708987 | 275 | .use_virtcon = 1, |
986c5f78 | 276 | .no_vga = 1, |
8cb310e1 AG |
277 | .max_cpus = 255, |
278 | .is_default = 1, | |
279 | }; | |
280 | ||
281 | static void s390_machine_init(void) | |
282 | { | |
283 | qemu_register_machine(&s390_machine); | |
284 | } | |
285 | ||
286 | machine_init(s390_machine_init); |