Statistics
| Branch: | Revision:

root / hw / s390x / s390-virtio.c @ 0434e30a

History | View | Annotate | Download (7.9 kB)

1 8cb310e1 Alexander Graf
/*
2 8cb310e1 Alexander Graf
 * QEMU S390 virtio target
3 8cb310e1 Alexander Graf
 *
4 8cb310e1 Alexander Graf
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 904e5fd5 Viktor Mihajlovski
 * Copyright IBM Corp 2012
6 8cb310e1 Alexander Graf
 *
7 8cb310e1 Alexander Graf
 * This library is free software; you can redistribute it and/or
8 8cb310e1 Alexander Graf
 * modify it under the terms of the GNU Lesser General Public
9 8cb310e1 Alexander Graf
 * License as published by the Free Software Foundation; either
10 8cb310e1 Alexander Graf
 * version 2 of the License, or (at your option) any later version.
11 8cb310e1 Alexander Graf
 *
12 8cb310e1 Alexander Graf
 * This library is distributed in the hope that it will be useful,
13 8cb310e1 Alexander Graf
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 8cb310e1 Alexander Graf
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 8cb310e1 Alexander Graf
 * Lesser General Public License for more details.
16 8cb310e1 Alexander Graf
 *
17 904e5fd5 Viktor Mihajlovski
 * Contributions after 2012-10-29 are licensed under the terms of the
18 904e5fd5 Viktor Mihajlovski
 * GNU GPL, version 2 or (at your option) any later version.
19 904e5fd5 Viktor Mihajlovski
 *
20 904e5fd5 Viktor Mihajlovski
 * You should have received a copy of the GNU (Lesser) General Public
21 8cb310e1 Alexander Graf
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 8cb310e1 Alexander Graf
 */
23 8cb310e1 Alexander Graf
24 b73d3531 Alexander Graf
#include "hw/hw.h"
25 737e150e Paolo Bonzini
#include "block/block.h"
26 9c17d615 Paolo Bonzini
#include "sysemu/blockdev.h"
27 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
28 1422e32d Paolo Bonzini
#include "net/net.h"
29 b73d3531 Alexander Graf
#include "hw/boards.h"
30 83c9089e Paolo Bonzini
#include "monitor/monitor.h"
31 b73d3531 Alexander Graf
#include "hw/loader.h"
32 0d09e41a Paolo Bonzini
#include "hw/virtio/virtio.h"
33 8cb310e1 Alexander Graf
#include "hw/sysbus.h"
34 9c17d615 Paolo Bonzini
#include "sysemu/kvm.h"
35 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
36 8cb310e1 Alexander Graf
37 b73d3531 Alexander Graf
#include "hw/s390x/s390-virtio-bus.h"
38 559a17a1 Heinz Graalfs
#include "hw/s390x/sclp.h"
39 b73d3531 Alexander Graf
#include "hw/s390x/s390-virtio.h"
40 8cb310e1 Alexander Graf
41 8cb310e1 Alexander Graf
//#define DEBUG_S390
42 8cb310e1 Alexander Graf
43 8cb310e1 Alexander Graf
#ifdef DEBUG_S390
44 8cb310e1 Alexander Graf
#define dprintf(fmt, ...) \
45 8cb310e1 Alexander Graf
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 8cb310e1 Alexander Graf
#else
47 8cb310e1 Alexander Graf
#define dprintf(fmt, ...) \
48 8cb310e1 Alexander Graf
    do { } while (0)
49 8cb310e1 Alexander Graf
#endif
50 8cb310e1 Alexander Graf
51 8cb310e1 Alexander Graf
#define MAX_BLK_DEVS                    10
52 8cb310e1 Alexander Graf
53 8cb310e1 Alexander Graf
static VirtIOS390Bus *s390_bus;
54 45fa769b Andreas Färber
static S390CPU **ipi_states;
55 8cb310e1 Alexander Graf
56 45fa769b Andreas Färber
S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
57 8cb310e1 Alexander Graf
{
58 8cb310e1 Alexander Graf
    if (cpu_addr >= smp_cpus) {
59 8cb310e1 Alexander Graf
        return NULL;
60 8cb310e1 Alexander Graf
    }
61 8cb310e1 Alexander Graf
62 8cb310e1 Alexander Graf
    return ipi_states[cpu_addr];
63 8cb310e1 Alexander Graf
}
64 8cb310e1 Alexander Graf
65 28e942f8 Cornelia Huck
static int s390_virtio_hcall_notify(const uint64_t *args)
66 8cb310e1 Alexander Graf
{
67 28e942f8 Cornelia Huck
    uint64_t mem = args[0];
68 8cb310e1 Alexander Graf
    int r = 0, i;
69 8cb310e1 Alexander Graf
70 28e942f8 Cornelia Huck
    if (mem > ram_size) {
71 28e942f8 Cornelia Huck
        VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
72 8cb310e1 Alexander Graf
        if (dev) {
73 28e942f8 Cornelia Huck
            virtio_queue_notify(dev->vdev, i);
74 8cb310e1 Alexander Graf
        } else {
75 8cb310e1 Alexander Graf
            r = -EINVAL;
76 8cb310e1 Alexander Graf
        }
77 28e942f8 Cornelia Huck
    } else {
78 28e942f8 Cornelia Huck
        /* Early printk */
79 8cb310e1 Alexander Graf
    }
80 28e942f8 Cornelia Huck
    return r;
81 28e942f8 Cornelia Huck
}
82 28e942f8 Cornelia Huck
83 28e942f8 Cornelia Huck
static int s390_virtio_hcall_reset(const uint64_t *args)
84 28e942f8 Cornelia Huck
{
85 28e942f8 Cornelia Huck
    uint64_t mem = args[0];
86 28e942f8 Cornelia Huck
    VirtIOS390Device *dev;
87 28e942f8 Cornelia Huck
88 28e942f8 Cornelia Huck
    dev = s390_virtio_bus_find_mem(s390_bus, mem);
89 ab290630 Andreas Färber
    if (dev == NULL) {
90 ab290630 Andreas Färber
        return -EINVAL;
91 ab290630 Andreas Färber
    }
92 28e942f8 Cornelia Huck
    virtio_reset(dev->vdev);
93 28e942f8 Cornelia Huck
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
94 28e942f8 Cornelia Huck
    s390_virtio_device_sync(dev);
95 28e942f8 Cornelia Huck
    s390_virtio_reset_idx(dev);
96 28e942f8 Cornelia Huck
97 28e942f8 Cornelia Huck
    return 0;
98 28e942f8 Cornelia Huck
}
99 28e942f8 Cornelia Huck
100 28e942f8 Cornelia Huck
static int s390_virtio_hcall_set_status(const uint64_t *args)
101 28e942f8 Cornelia Huck
{
102 28e942f8 Cornelia Huck
    uint64_t mem = args[0];
103 28e942f8 Cornelia Huck
    int r = 0;
104 28e942f8 Cornelia Huck
    VirtIOS390Device *dev;
105 28e942f8 Cornelia Huck
106 28e942f8 Cornelia Huck
    dev = s390_virtio_bus_find_mem(s390_bus, mem);
107 28e942f8 Cornelia Huck
    if (dev) {
108 28e942f8 Cornelia Huck
        s390_virtio_device_update_status(dev);
109 28e942f8 Cornelia Huck
    } else {
110 8cb310e1 Alexander Graf
        r = -EINVAL;
111 8cb310e1 Alexander Graf
    }
112 8d5192ee Alexander Graf
    return r;
113 8cb310e1 Alexander Graf
}
114 8cb310e1 Alexander Graf
115 28e942f8 Cornelia Huck
static void s390_virtio_register_hcalls(void)
116 28e942f8 Cornelia Huck
{
117 28e942f8 Cornelia Huck
    s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
118 28e942f8 Cornelia Huck
                                   s390_virtio_hcall_notify);
119 28e942f8 Cornelia Huck
    s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
120 28e942f8 Cornelia Huck
                                   s390_virtio_hcall_reset);
121 28e942f8 Cornelia Huck
    s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
122 28e942f8 Cornelia Huck
                                   s390_virtio_hcall_set_status);
123 28e942f8 Cornelia Huck
}
124 28e942f8 Cornelia Huck
125 854e42f3 Christian Borntraeger
/*
126 854e42f3 Christian Borntraeger
 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
127 854e42f3 Christian Borntraeger
 * being either stopped or disabled (for interrupts) waiting. We have to
128 854e42f3 Christian Borntraeger
 * track this number to call the shutdown sequence accordingly. This
129 854e42f3 Christian Borntraeger
 * number is modified either on startup or while holding the big qemu lock.
130 854e42f3 Christian Borntraeger
 */
131 854e42f3 Christian Borntraeger
static unsigned s390_running_cpus;
132 854e42f3 Christian Borntraeger
133 49e15878 Andreas Färber
void s390_add_running_cpu(S390CPU *cpu)
134 854e42f3 Christian Borntraeger
{
135 259186a7 Andreas Färber
    CPUState *cs = CPU(cpu);
136 49e15878 Andreas Färber
    CPUS390XState *env = &cpu->env;
137 49e15878 Andreas Färber
138 259186a7 Andreas Färber
    if (cs->halted) {
139 854e42f3 Christian Borntraeger
        s390_running_cpus++;
140 259186a7 Andreas Färber
        cs->halted = 0;
141 854e42f3 Christian Borntraeger
        env->exception_index = -1;
142 854e42f3 Christian Borntraeger
    }
143 854e42f3 Christian Borntraeger
}
144 854e42f3 Christian Borntraeger
145 49e15878 Andreas Färber
unsigned s390_del_running_cpu(S390CPU *cpu)
146 854e42f3 Christian Borntraeger
{
147 259186a7 Andreas Färber
    CPUState *cs = CPU(cpu);
148 49e15878 Andreas Färber
    CPUS390XState *env = &cpu->env;
149 49e15878 Andreas Färber
150 259186a7 Andreas Färber
    if (cs->halted == 0) {
151 854e42f3 Christian Borntraeger
        assert(s390_running_cpus >= 1);
152 854e42f3 Christian Borntraeger
        s390_running_cpus--;
153 259186a7 Andreas Färber
        cs->halted = 1;
154 854e42f3 Christian Borntraeger
        env->exception_index = EXCP_HLT;
155 854e42f3 Christian Borntraeger
    }
156 854e42f3 Christian Borntraeger
    return s390_running_cpus;
157 854e42f3 Christian Borntraeger
}
158 854e42f3 Christian Borntraeger
159 fad37673 Cornelia Huck
void s390_init_ipl_dev(const char *kernel_filename,
160 fad37673 Cornelia Huck
                       const char *kernel_cmdline,
161 fad37673 Cornelia Huck
                       const char *initrd_filename)
162 fad37673 Cornelia Huck
{
163 fad37673 Cornelia Huck
    DeviceState *dev;
164 fad37673 Cornelia Huck
165 fad37673 Cornelia Huck
    dev  = qdev_create(NULL, "s390-ipl");
166 fad37673 Cornelia Huck
    if (kernel_filename) {
167 fad37673 Cornelia Huck
        qdev_prop_set_string(dev, "kernel", kernel_filename);
168 fad37673 Cornelia Huck
    }
169 fad37673 Cornelia Huck
    if (initrd_filename) {
170 fad37673 Cornelia Huck
        qdev_prop_set_string(dev, "initrd", initrd_filename);
171 fad37673 Cornelia Huck
    }
172 fad37673 Cornelia Huck
    qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
173 fad37673 Cornelia Huck
    qdev_init_nofail(dev);
174 fad37673 Cornelia Huck
}
175 fad37673 Cornelia Huck
176 fad37673 Cornelia Huck
void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
177 fad37673 Cornelia Huck
{
178 fad37673 Cornelia Huck
    int i;
179 fad37673 Cornelia Huck
180 fad37673 Cornelia Huck
    if (cpu_model == NULL) {
181 fad37673 Cornelia Huck
        cpu_model = "host";
182 fad37673 Cornelia Huck
    }
183 fad37673 Cornelia Huck
184 fad37673 Cornelia Huck
    ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);
185 fad37673 Cornelia Huck
186 fad37673 Cornelia Huck
    for (i = 0; i < smp_cpus; i++) {
187 fad37673 Cornelia Huck
        S390CPU *cpu;
188 259186a7 Andreas Färber
        CPUState *cs;
189 fad37673 Cornelia Huck
190 fad37673 Cornelia Huck
        cpu = cpu_s390x_init(cpu_model);
191 259186a7 Andreas Färber
        cs = CPU(cpu);
192 fad37673 Cornelia Huck
193 fad37673 Cornelia Huck
        ipi_states[i] = cpu;
194 259186a7 Andreas Färber
        cs->halted = 1;
195 fad37673 Cornelia Huck
        cpu->env.exception_index = EXCP_HLT;
196 fad37673 Cornelia Huck
        cpu->env.storage_keys = storage_keys;
197 fad37673 Cornelia Huck
    }
198 fad37673 Cornelia Huck
}
199 fad37673 Cornelia Huck
200 fad37673 Cornelia Huck
201 fad37673 Cornelia Huck
void s390_create_virtio_net(BusState *bus, const char *name)
202 fad37673 Cornelia Huck
{
203 fad37673 Cornelia Huck
    int i;
204 fad37673 Cornelia Huck
205 fad37673 Cornelia Huck
    for (i = 0; i < nb_nics; i++) {
206 fad37673 Cornelia Huck
        NICInfo *nd = &nd_table[i];
207 fad37673 Cornelia Huck
        DeviceState *dev;
208 fad37673 Cornelia Huck
209 fad37673 Cornelia Huck
        if (!nd->model) {
210 fad37673 Cornelia Huck
            nd->model = g_strdup("virtio");
211 fad37673 Cornelia Huck
        }
212 fad37673 Cornelia Huck
213 fad37673 Cornelia Huck
        if (strcmp(nd->model, "virtio")) {
214 fad37673 Cornelia Huck
            fprintf(stderr, "S390 only supports VirtIO nics\n");
215 fad37673 Cornelia Huck
            exit(1);
216 fad37673 Cornelia Huck
        }
217 fad37673 Cornelia Huck
218 fad37673 Cornelia Huck
        dev = qdev_create(bus, name);
219 fad37673 Cornelia Huck
        qdev_set_nic_properties(dev, nd);
220 fad37673 Cornelia Huck
        qdev_init_nofail(dev);
221 fad37673 Cornelia Huck
    }
222 fad37673 Cornelia Huck
}
223 fad37673 Cornelia Huck
224 8cb310e1 Alexander Graf
/* PC hardware initialisation */
225 5f072e1f Eduardo Habkost
static void s390_init(QEMUMachineInitArgs *args)
226 8cb310e1 Alexander Graf
{
227 5f072e1f Eduardo Habkost
    ram_addr_t my_ram_size = args->ram_size;
228 ca3dbc27 Avi Kivity
    MemoryRegion *sysmem = get_system_memory();
229 ca3dbc27 Avi Kivity
    MemoryRegion *ram = g_new(MemoryRegion, 1);
230 22486aa0 Christian Borntraeger
    int shift = 0;
231 8d5192ee Alexander Graf
    uint8_t *storage_keys;
232 326384d5 Alexander Graf
    void *virtio_region;
233 a8170e5e Avi Kivity
    hwaddr virtio_region_len;
234 a8170e5e Avi Kivity
    hwaddr virtio_region_start;
235 8cb310e1 Alexander Graf
236 22486aa0 Christian Borntraeger
    /* s390x ram size detection needs a 16bit multiplier + an increment. So
237 22486aa0 Christian Borntraeger
       guests > 64GB can be specified in 2MB steps etc. */
238 22486aa0 Christian Borntraeger
    while ((my_ram_size >> (20 + shift)) > 65535) {
239 22486aa0 Christian Borntraeger
        shift++;
240 22486aa0 Christian Borntraeger
    }
241 22486aa0 Christian Borntraeger
    my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
242 22486aa0 Christian Borntraeger
243 22486aa0 Christian Borntraeger
    /* lets propagate the changed ram size into the global variable. */
244 22486aa0 Christian Borntraeger
    ram_size = my_ram_size;
245 e249651c Alexander Graf
246 8cb310e1 Alexander Graf
    /* get a BUS */
247 22486aa0 Christian Borntraeger
    s390_bus = s390_virtio_bus_init(&my_ram_size);
248 559a17a1 Heinz Graalfs
    s390_sclp_init();
249 fad37673 Cornelia Huck
    s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
250 fad37673 Cornelia Huck
                      args->initrd_filename);
251 8cb310e1 Alexander Graf
252 28e942f8 Cornelia Huck
    /* register hypercalls */
253 28e942f8 Cornelia Huck
    s390_virtio_register_hcalls();
254 28e942f8 Cornelia Huck
255 8cb310e1 Alexander Graf
    /* allocate RAM */
256 c5705a77 Avi Kivity
    memory_region_init_ram(ram, "s390.ram", my_ram_size);
257 c5705a77 Avi Kivity
    vmstate_register_ram_global(ram);
258 ca3dbc27 Avi Kivity
    memory_region_add_subregion(sysmem, 0, ram);
259 8cb310e1 Alexander Graf
260 326384d5 Alexander Graf
    /* clear virtio region */
261 326384d5 Alexander Graf
    virtio_region_len = my_ram_size - ram_size;
262 326384d5 Alexander Graf
    virtio_region_start = ram_size;
263 326384d5 Alexander Graf
    virtio_region = cpu_physical_memory_map(virtio_region_start,
264 326384d5 Alexander Graf
                                            &virtio_region_len, true);
265 326384d5 Alexander Graf
    memset(virtio_region, 0, virtio_region_len);
266 326384d5 Alexander Graf
    cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
267 326384d5 Alexander Graf
                              virtio_region_len);
268 326384d5 Alexander Graf
269 8d5192ee Alexander Graf
    /* allocate storage keys */
270 7267c094 Anthony Liguori
    storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
271 8d5192ee Alexander Graf
272 8cb310e1 Alexander Graf
    /* init CPUs */
273 fad37673 Cornelia Huck
    s390_init_cpus(args->cpu_model, storage_keys);
274 8cb310e1 Alexander Graf
275 8cb310e1 Alexander Graf
    /* Create VirtIO network adapters */
276 fad37673 Cornelia Huck
    s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
277 8cb310e1 Alexander Graf
}
278 8cb310e1 Alexander Graf
279 8cb310e1 Alexander Graf
static QEMUMachine s390_machine = {
280 8cb310e1 Alexander Graf
    .name = "s390-virtio",
281 8cb310e1 Alexander Graf
    .alias = "s390",
282 8cb310e1 Alexander Graf
    .desc = "VirtIO based S390 machine",
283 8cb310e1 Alexander Graf
    .init = s390_init,
284 2d0d2837 Christian Borntraeger
    .block_default_type = IF_VIRTIO,
285 ad0bbc56 Einar Lueck
    .no_cdrom = 1,
286 ad0bbc56 Einar Lueck
    .no_floppy = 1,
287 986c5f78 Gerd Hoffmann
    .no_serial = 1,
288 986c5f78 Gerd Hoffmann
    .no_parallel = 1,
289 ad0bbc56 Einar Lueck
    .no_sdcard = 1,
290 cf708987 Michael S. Tsirkin
    .use_virtcon = 1,
291 8cb310e1 Alexander Graf
    .max_cpus = 255,
292 8cb310e1 Alexander Graf
    .is_default = 1,
293 e4ada29e Avik Sil
    DEFAULT_MACHINE_OPTIONS,
294 8cb310e1 Alexander Graf
};
295 8cb310e1 Alexander Graf
296 8cb310e1 Alexander Graf
static void s390_machine_init(void)
297 8cb310e1 Alexander Graf
{
298 8cb310e1 Alexander Graf
    qemu_register_machine(&s390_machine);
299 8cb310e1 Alexander Graf
}
300 8cb310e1 Alexander Graf
301 8cb310e1 Alexander Graf
machine_init(s390_machine_init);