s390-virtio.c 8.0 KB
Newer Older
1 2 3 4
/*
 * QEMU S390 virtio target
 *
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5
 * Copyright IBM Corp 2012
6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
17 18 19 20
 * Contributions after 2012-10-29 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
 *
 * You should have received a copy of the GNU (Lesser) General Public
21 22 23
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

A
Alexander Graf 已提交
24
#include "hw/hw.h"
25
#include "block/block.h"
26 27
#include "sysemu/blockdev.h"
#include "sysemu/sysemu.h"
P
Paolo Bonzini 已提交
28
#include "net/net.h"
A
Alexander Graf 已提交
29
#include "hw/boards.h"
30
#include "monitor/monitor.h"
A
Alexander Graf 已提交
31
#include "hw/loader.h"
P
Paolo Bonzini 已提交
32
#include "hw/virtio/virtio.h"
33
#include "hw/sysbus.h"
34
#include "sysemu/kvm.h"
35
#include "exec/address-spaces.h"
36

A
Alexander Graf 已提交
37
#include "hw/s390x/s390-virtio-bus.h"
H
Heinz Graalfs 已提交
38
#include "hw/s390x/sclp.h"
A
Alexander Graf 已提交
39
#include "hw/s390x/s390-virtio.h"
40 41 42 43

//#define DEBUG_S390

#ifdef DEBUG_S390
44
#define DPRINTF(fmt, ...) \
45 46
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
#else
47
#define DPRINTF(fmt, ...) \
48 49 50 51
    do { } while (0)
#endif

#define MAX_BLK_DEVS                    10
52
#define ZIPL_FILENAME                   "s390-zipl.rom"
53 54

static VirtIOS390Bus *s390_bus;
55
static S390CPU **ipi_states;
56

57
S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
58 59 60 61 62 63 64 65
{
    if (cpu_addr >= smp_cpus) {
        return NULL;
    }

    return ipi_states[cpu_addr];
}

66
static int s390_virtio_hcall_notify(const uint64_t *args)
67
{
68
    uint64_t mem = args[0];
69 70
    int r = 0, i;

71 72
    if (mem > ram_size) {
        VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
73
        if (dev) {
74
            virtio_queue_notify(dev->vdev, i);
75 76 77
        } else {
            r = -EINVAL;
        }
78 79
    } else {
        /* Early printk */
80
    }
81 82 83 84 85 86 87 88 89
    return r;
}

static int s390_virtio_hcall_reset(const uint64_t *args)
{
    uint64_t mem = args[0];
    VirtIOS390Device *dev;

    dev = s390_virtio_bus_find_mem(s390_bus, mem);
90 91 92
    if (dev == NULL) {
        return -EINVAL;
    }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    virtio_reset(dev->vdev);
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
    s390_virtio_device_sync(dev);
    s390_virtio_reset_idx(dev);

    return 0;
}

static int s390_virtio_hcall_set_status(const uint64_t *args)
{
    uint64_t mem = args[0];
    int r = 0;
    VirtIOS390Device *dev;

    dev = s390_virtio_bus_find_mem(s390_bus, mem);
    if (dev) {
        s390_virtio_device_update_status(dev);
    } else {
111 112
        r = -EINVAL;
    }
113
    return r;
114 115
}

116 117 118 119 120 121 122 123 124 125
static void s390_virtio_register_hcalls(void)
{
    s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
                                   s390_virtio_hcall_notify);
    s390_register_virtio_hypercall(KVM_S390_VIRTIO_RESET,
                                   s390_virtio_hcall_reset);
    s390_register_virtio_hypercall(KVM_S390_VIRTIO_SET_STATUS,
                                   s390_virtio_hcall_set_status);
}

126 127 128 129 130 131 132 133
/*
 * The number of running CPUs. On s390 a shutdown is the state of all CPUs
 * being either stopped or disabled (for interrupts) waiting. We have to
 * track this number to call the shutdown sequence accordingly. This
 * number is modified either on startup or while holding the big qemu lock.
 */
static unsigned s390_running_cpus;

134
void s390_add_running_cpu(S390CPU *cpu)
135
{
136
    CPUState *cs = CPU(cpu);
137 138
    CPUS390XState *env = &cpu->env;

139
    if (cs->halted) {
140
        s390_running_cpus++;
141
        cs->halted = 0;
142 143 144 145
        env->exception_index = -1;
    }
}

146
unsigned s390_del_running_cpu(S390CPU *cpu)
147
{
148
    CPUState *cs = CPU(cpu);
149 150
    CPUS390XState *env = &cpu->env;

151
    if (cs->halted == 0) {
152 153
        assert(s390_running_cpus >= 1);
        s390_running_cpus--;
154
        cs->halted = 1;
155 156 157 158 159
        env->exception_index = EXCP_HLT;
    }
    return s390_running_cpus;
}

160 161
void s390_init_ipl_dev(const char *kernel_filename,
                       const char *kernel_cmdline,
162 163
                       const char *initrd_filename,
                       const char *firmware)
164 165 166 167 168 169 170 171 172 173 174
{
    DeviceState *dev;

    dev  = qdev_create(NULL, "s390-ipl");
    if (kernel_filename) {
        qdev_prop_set_string(dev, "kernel", kernel_filename);
    }
    if (initrd_filename) {
        qdev_prop_set_string(dev, "initrd", initrd_filename);
    }
    qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
175
    qdev_prop_set_string(dev, "firmware", firmware);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    qdev_init_nofail(dev);
}

void s390_init_cpus(const char *cpu_model, uint8_t *storage_keys)
{
    int i;

    if (cpu_model == NULL) {
        cpu_model = "host";
    }

    ipi_states = g_malloc(sizeof(S390CPU *) * smp_cpus);

    for (i = 0; i < smp_cpus; i++) {
        S390CPU *cpu;
191
        CPUState *cs;
192 193

        cpu = cpu_s390x_init(cpu_model);
194
        cs = CPU(cpu);
195 196

        ipi_states[i] = cpu;
197
        cs->halted = 1;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        cpu->env.exception_index = EXCP_HLT;
        cpu->env.storage_keys = storage_keys;
    }
}


void s390_create_virtio_net(BusState *bus, const char *name)
{
    int i;

    for (i = 0; i < nb_nics; i++) {
        NICInfo *nd = &nd_table[i];
        DeviceState *dev;

        if (!nd->model) {
            nd->model = g_strdup("virtio");
        }

        if (strcmp(nd->model, "virtio")) {
            fprintf(stderr, "S390 only supports VirtIO nics\n");
            exit(1);
        }

        dev = qdev_create(bus, name);
        qdev_set_nic_properties(dev, nd);
        qdev_init_nofail(dev);
    }
}

227
/* PC hardware initialisation */
228
static void s390_init(QEMUMachineInitArgs *args)
229
{
230
    ram_addr_t my_ram_size = args->ram_size;
A
Avi Kivity 已提交
231 232
    MemoryRegion *sysmem = get_system_memory();
    MemoryRegion *ram = g_new(MemoryRegion, 1);
233
    int shift = 0;
234
    uint8_t *storage_keys;
235
    void *virtio_region;
A
Avi Kivity 已提交
236 237
    hwaddr virtio_region_len;
    hwaddr virtio_region_start;
238

239 240 241 242 243 244 245
    /* s390x ram size detection needs a 16bit multiplier + an increment. So
       guests > 64GB can be specified in 2MB steps etc. */
    while ((my_ram_size >> (20 + shift)) > 65535) {
        shift++;
    }
    my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);

246
    /* let's propagate the changed ram size into the global variable. */
247
    ram_size = my_ram_size;
A
Alexander Graf 已提交
248

249
    /* get a BUS */
250
    s390_bus = s390_virtio_bus_init(&my_ram_size);
H
Heinz Graalfs 已提交
251
    s390_sclp_init();
252
    s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
253
                      args->initrd_filename, ZIPL_FILENAME);
254

255 256 257
    /* register hypercalls */
    s390_virtio_register_hcalls();

258
    /* allocate RAM */
259
    memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size);
260
    vmstate_register_ram_global(ram);
A
Avi Kivity 已提交
261
    memory_region_add_subregion(sysmem, 0, ram);
262

263 264 265 266 267 268 269 270 271
    /* clear virtio region */
    virtio_region_len = my_ram_size - ram_size;
    virtio_region_start = ram_size;
    virtio_region = cpu_physical_memory_map(virtio_region_start,
                                            &virtio_region_len, true);
    memset(virtio_region, 0, virtio_region_len);
    cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
                              virtio_region_len);

272
    /* allocate storage keys */
273
    storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
274

275
    /* init CPUs */
276
    s390_init_cpus(args->cpu_model, storage_keys);
277 278

    /* Create VirtIO network adapters */
279
    s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
280 281 282 283 284 285 286
}

static QEMUMachine s390_machine = {
    .name = "s390-virtio",
    .alias = "s390",
    .desc = "VirtIO based S390 machine",
    .init = s390_init,
287
    .block_default_type = IF_VIRTIO,
288 289
    .no_cdrom = 1,
    .no_floppy = 1,
290 291
    .no_serial = 1,
    .no_parallel = 1,
292
    .no_sdcard = 1,
M
Michael S. Tsirkin 已提交
293
    .use_virtcon = 1,
294 295 296 297 298 299 300 301 302 303
    .max_cpus = 255,
    .is_default = 1,
};

static void s390_machine_init(void)
{
    qemu_register_machine(&s390_machine);
}

machine_init(s390_machine_init);