]> git.proxmox.com Git - qemu.git/blame - hw/s390x/s390-virtio.c
target-s390x: Pass S390CPU to s390_{add, del}_running_cpu()
[qemu.git] / hw / s390x / s390-virtio.c
CommitLineData
8cb310e1
AG
1/*
2 * QEMU S390 virtio target
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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"
737e150e 25#include "block/block.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"
8cb310e1 32#include "hw/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"
b73d3531 39#include "hw/s390x/s390-virtio.h"
8cb310e1
AG
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
8cb310e1
AG
51#define MAX_BLK_DEVS 10
52
53static VirtIOS390Bus *s390_bus;
45fa769b 54static S390CPU **ipi_states;
8cb310e1 55
45fa769b 56S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
8cb310e1
AG
57{
58 if (cpu_addr >= smp_cpus) {
59 return NULL;
60 }
61
62 return ipi_states[cpu_addr];
63}
64
28e942f8 65static int s390_virtio_hcall_notify(const uint64_t *args)
8cb310e1 66{
28e942f8 67 uint64_t mem = args[0];
8cb310e1 68 int r = 0, i;
8cb310e1 69
28e942f8
CH
70 if (mem > ram_size) {
71 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
8cb310e1 72 if (dev) {
28e942f8 73 virtio_queue_notify(dev->vdev, i);
8cb310e1
AG
74 } else {
75 r = -EINVAL;
76 }
28e942f8
CH
77 } else {
78 /* Early printk */
8cb310e1 79 }
28e942f8
CH
80 return r;
81}
82
83static 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);
ab290630
AF
89 if (dev == NULL) {
90 return -EINVAL;
91 }
28e942f8
CH
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
100static 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 {
8cb310e1 110 r = -EINVAL;
8cb310e1 111 }
8d5192ee 112 return r;
8cb310e1
AG
113}
114
28e942f8
CH
115static 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
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 */
131static unsigned s390_running_cpus;
132
49e15878 133void s390_add_running_cpu(S390CPU *cpu)
854e42f3 134{
49e15878
AF
135 CPUS390XState *env = &cpu->env;
136
854e42f3
CB
137 if (env->halted) {
138 s390_running_cpus++;
139 env->halted = 0;
140 env->exception_index = -1;
141 }
142}
143
49e15878 144unsigned s390_del_running_cpu(S390CPU *cpu)
854e42f3 145{
49e15878
AF
146 CPUS390XState *env = &cpu->env;
147
854e42f3
CB
148 if (env->halted == 0) {
149 assert(s390_running_cpus >= 1);
150 s390_running_cpus--;
151 env->halted = 1;
152 env->exception_index = EXCP_HLT;
153 }
154 return s390_running_cpus;
155}
156
fad37673
CH
157void s390_init_ipl_dev(const char *kernel_filename,
158 const char *kernel_cmdline,
159 const char *initrd_filename)
160{
161 DeviceState *dev;
162
163 dev = qdev_create(NULL, "s390-ipl");
164 if (kernel_filename) {
165 qdev_prop_set_string(dev, "kernel", kernel_filename);
166 }
167 if (initrd_filename) {
168 qdev_prop_set_string(dev, "initrd", initrd_filename);
169 }
170 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
171 qdev_init_nofail(dev);
172}
173
174void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
175{
176 int i;
177
178 if (cpu_model == NULL) {
179 cpu_model = "host";
180 }
181
182 ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
183
184 for (i = 0; i < smp_cpus; i++) {
185 S390CPU *cpu;
186
187 cpu = cpu_s390x_init(cpu_model);
188
189 ipi_states[i] = cpu;
190 cpu->env.halted = 1;
191 cpu->env.exception_index = EXCP_HLT;
192 cpu->env.storage_keys = storage_keys;
193 }
194}
195
196
197void s390_create_virtio_net(BusState *bus, const char *name)
198{
199 int i;
200
201 for (i = 0; i < nb_nics; i++) {
202 NICInfo *nd = &nd_table[i];
203 DeviceState *dev;
204
205 if (!nd->model) {
206 nd->model = g_strdup("virtio");
207 }
208
209 if (strcmp(nd->model, "virtio")) {
210 fprintf(stderr, "S390 only supports VirtIO nics\n");
211 exit(1);
212 }
213
214 dev = qdev_create(bus, name);
215 qdev_set_nic_properties(dev, nd);
216 qdev_init_nofail(dev);
217 }
218}
219
8cb310e1 220/* PC hardware initialisation */
5f072e1f 221static void s390_init(QEMUMachineInitArgs *args)
8cb310e1 222{
5f072e1f 223 ram_addr_t my_ram_size = args->ram_size;
ca3dbc27
AK
224 MemoryRegion *sysmem = get_system_memory();
225 MemoryRegion *ram = g_new(MemoryRegion, 1);
22486aa0 226 int shift = 0;
8d5192ee 227 uint8_t *storage_keys;
326384d5 228 void *virtio_region;
a8170e5e
AK
229 hwaddr virtio_region_len;
230 hwaddr virtio_region_start;
8cb310e1 231
22486aa0
CB
232 /* s390x ram size detection needs a 16bit multiplier + an increment. So
233 guests > 64GB can be specified in 2MB steps etc. */
234 while ((my_ram_size >> (20 + shift)) > 65535) {
235 shift++;
236 }
237 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
238
239 /* lets propagate the changed ram size into the global variable. */
240 ram_size = my_ram_size;
e249651c 241
8cb310e1 242 /* get a BUS */
22486aa0 243 s390_bus = s390_virtio_bus_init(&my_ram_size);
559a17a1 244 s390_sclp_init();
fad37673
CH
245 s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
246 args->initrd_filename);
8cb310e1 247
28e942f8
CH
248 /* register hypercalls */
249 s390_virtio_register_hcalls();
250
8cb310e1 251 /* allocate RAM */
c5705a77
AK
252 memory_region_init_ram(ram, "s390.ram", my_ram_size);
253 vmstate_register_ram_global(ram);
ca3dbc27 254 memory_region_add_subregion(sysmem, 0, ram);
8cb310e1 255
326384d5
AG
256 /* clear virtio region */
257 virtio_region_len = my_ram_size - ram_size;
258 virtio_region_start = ram_size;
259 virtio_region = cpu_physical_memory_map(virtio_region_start,
260 &virtio_region_len, true);
261 memset(virtio_region, 0, virtio_region_len);
262 cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
263 virtio_region_len);
264
8d5192ee 265 /* allocate storage keys */
7267c094 266 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
8d5192ee 267
8cb310e1 268 /* init CPUs */
fad37673 269 s390_init_cpus(args->cpu_model, storage_keys);
8cb310e1 270
8cb310e1 271 /* Create VirtIO network adapters */
fad37673 272 s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
8cb310e1
AG
273}
274
275static QEMUMachine s390_machine = {
276 .name = "s390-virtio",
277 .alias = "s390",
278 .desc = "VirtIO based S390 machine",
279 .init = s390_init,
2d0d2837 280 .block_default_type = IF_VIRTIO,
ad0bbc56
EL
281 .no_cdrom = 1,
282 .no_floppy = 1,
986c5f78
GH
283 .no_serial = 1,
284 .no_parallel = 1,
ad0bbc56 285 .no_sdcard = 1,
cf708987 286 .use_virtcon = 1,
8cb310e1
AG
287 .max_cpus = 255,
288 .is_default = 1,
e4ada29e 289 DEFAULT_MACHINE_OPTIONS,
8cb310e1
AG
290};
291
292static void s390_machine_init(void)
293{
294 qemu_register_machine(&s390_machine);
295}
296
297machine_init(s390_machine_init);