]> git.proxmox.com Git - qemu.git/blob - hw/s390x/s390-virtio.c
Merge remote-tracking branch 'afaerber-or/prep-up' into staging
[qemu.git] / hw / s390x / s390-virtio.c
1 /*
2 * QEMU S390 virtio target
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp 2012
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 *
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
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "hw/hw.h"
25 #include "block/block.h"
26 #include "sysemu/blockdev.h"
27 #include "sysemu/sysemu.h"
28 #include "net/net.h"
29 #include "hw/boards.h"
30 #include "monitor/monitor.h"
31 #include "hw/loader.h"
32 #include "hw/virtio.h"
33 #include "hw/sysbus.h"
34 #include "sysemu/kvm.h"
35 #include "exec/address-spaces.h"
36
37 #include "hw/s390x/s390-virtio-bus.h"
38 #include "hw/s390x/sclp.h"
39 #include "hw/s390x/s390-virtio.h"
40
41 //#define DEBUG_S390
42
43 #ifdef DEBUG_S390
44 #define dprintf(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define dprintf(fmt, ...) \
48 do { } while (0)
49 #endif
50
51 #define MAX_BLK_DEVS 10
52
53 static VirtIOS390Bus *s390_bus;
54 static S390CPU **ipi_states;
55
56 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
57 {
58 if (cpu_addr >= smp_cpus) {
59 return NULL;
60 }
61
62 return ipi_states[cpu_addr];
63 }
64
65 static int s390_virtio_hcall_notify(const uint64_t *args)
66 {
67 uint64_t mem = args[0];
68 int r = 0, i;
69
70 if (mem > ram_size) {
71 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
72 if (dev) {
73 virtio_queue_notify(dev->vdev, i);
74 } else {
75 r = -EINVAL;
76 }
77 } else {
78 /* Early printk */
79 }
80 return r;
81 }
82
83 static int s390_virtio_hcall_reset(const uint64_t *args)
84 {
85 uint64_t mem = args[0];
86 VirtIOS390Device *dev;
87
88 dev = s390_virtio_bus_find_mem(s390_bus, mem);
89 if (dev == NULL) {
90 return -EINVAL;
91 }
92 virtio_reset(dev->vdev);
93 stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
94 s390_virtio_device_sync(dev);
95 s390_virtio_reset_idx(dev);
96
97 return 0;
98 }
99
100 static int s390_virtio_hcall_set_status(const uint64_t *args)
101 {
102 uint64_t mem = args[0];
103 int r = 0;
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 return r;
113 }
114
115 static void s390_virtio_register_hcalls(void)
116 {
117 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
118 s390_virtio_hcall_notify);
119 s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
120 s390_virtio_hcall_reset);
121 s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
122 s390_virtio_hcall_set_status);
123 }
124
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
133 void s390_add_running_cpu(CPUS390XState *env)
134 {
135 if (env->halted) {
136 s390_running_cpus++;
137 env->halted = 0;
138 env->exception_index = -1;
139 }
140 }
141
142 unsigned s390_del_running_cpu(CPUS390XState *env)
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
153 void s390_init_ipl_dev(const char *kernel_filename,
154 const char *kernel_cmdline,
155 const char *initrd_filename)
156 {
157 DeviceState *dev;
158
159 dev = qdev_create(NULL, "s390-ipl");
160 if (kernel_filename) {
161 qdev_prop_set_string(dev, "kernel", kernel_filename);
162 }
163 if (initrd_filename) {
164 qdev_prop_set_string(dev, "initrd", initrd_filename);
165 }
166 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
167 qdev_init_nofail(dev);
168 }
169
170 void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
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;
182
183 cpu = cpu_s390x_init(cpu_model);
184
185 ipi_states[i] = cpu;
186 cpu->env.halted = 1;
187 cpu->env.exception_index = EXCP_HLT;
188 cpu->env.storage_keys = storage_keys;
189 }
190 }
191
192
193 void s390_create_virtio_net(BusState *bus, const char *name)
194 {
195 int i;
196
197 for (i = 0; i < nb_nics; i++) {
198 NICInfo *nd = &nd_table[i];
199 DeviceState *dev;
200
201 if (!nd->model) {
202 nd->model = g_strdup("virtio");
203 }
204
205 if (strcmp(nd->model, "virtio")) {
206 fprintf(stderr, "S390 only supports VirtIO nics\n");
207 exit(1);
208 }
209
210 dev = qdev_create(bus, name);
211 qdev_set_nic_properties(dev, nd);
212 qdev_init_nofail(dev);
213 }
214 }
215
216 /* PC hardware initialisation */
217 static void s390_init(QEMUMachineInitArgs *args)
218 {
219 ram_addr_t my_ram_size = args->ram_size;
220 MemoryRegion *sysmem = get_system_memory();
221 MemoryRegion *ram = g_new(MemoryRegion, 1);
222 int shift = 0;
223 uint8_t *storage_keys;
224 void *virtio_region;
225 hwaddr virtio_region_len;
226 hwaddr virtio_region_start;
227
228 /* s390x ram size detection needs a 16bit multiplier + an increment. So
229 guests > 64GB can be specified in 2MB steps etc. */
230 while ((my_ram_size >> (20 + shift)) > 65535) {
231 shift++;
232 }
233 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
234
235 /* lets propagate the changed ram size into the global variable. */
236 ram_size = my_ram_size;
237
238 /* get a BUS */
239 s390_bus = s390_virtio_bus_init(&my_ram_size);
240 s390_sclp_init();
241 s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
242 args->initrd_filename);
243
244 /* register hypercalls */
245 s390_virtio_register_hcalls();
246
247 /* allocate RAM */
248 memory_region_init_ram(ram, "s390.ram", my_ram_size);
249 vmstate_register_ram_global(ram);
250 memory_region_add_subregion(sysmem, 0, ram);
251
252 /* clear virtio region */
253 virtio_region_len = my_ram_size - ram_size;
254 virtio_region_start = ram_size;
255 virtio_region = cpu_physical_memory_map(virtio_region_start,
256 &virtio_region_len, true);
257 memset(virtio_region, 0, virtio_region_len);
258 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
259 virtio_region_len);
260
261 /* allocate storage keys */
262 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
263
264 /* init CPUs */
265 s390_init_cpus(args->cpu_model, storage_keys);
266
267 /* Create VirtIO network adapters */
268 s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
269 }
270
271 static QEMUMachine s390_machine = {
272 .name = "s390-virtio",
273 .alias = "s390",
274 .desc = "VirtIO based S390 machine",
275 .init = s390_init,
276 .block_default_type = IF_VIRTIO,
277 .no_cdrom = 1,
278 .no_floppy = 1,
279 .no_serial = 1,
280 .no_parallel = 1,
281 .no_sdcard = 1,
282 .use_virtcon = 1,
283 .max_cpus = 255,
284 .is_default = 1,
285 DEFAULT_MACHINE_OPTIONS,
286 };
287
288 static void s390_machine_init(void)
289 {
290 qemu_register_machine(&s390_machine);
291 }
292
293 machine_init(s390_machine_init);