s390-virtio.c 7.9 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 44 45 46 47 48 49 50 51 52 53

//#define DEBUG_S390

#ifdef DEBUG_S390
#define dprintf(fmt, ...) \
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
#else
#define dprintf(fmt, ...) \
    do { } while (0)
#endif

#define MAX_BLK_DEVS                    10

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

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

    return ipi_states[cpu_addr];
}

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

70 71
    if (mem > ram_size) {
        VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus, mem, &i);
72
        if (dev) {
73
            virtio_queue_notify(dev->vdev, i);
74 75 76
        } else {
            r = -EINVAL;
        }
77 78
    } else {
        /* Early printk */
79
    }
80 81 82 83 84 85 86 87 88
    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);
89 90 91
    if (dev == NULL) {
        return -EINVAL;
    }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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 {
110 111
        r = -EINVAL;
    }
112
    return r;
113 114
}

115 116 117 118 119 120 121 122 123 124
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);
}

125 126 127 128 129 130 131 132
/*
 * 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;

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

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

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

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
void s390_init_ipl_dev(const char *kernel_filename,
                       const char *kernel_cmdline,
                       const char *initrd_filename)
{
    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);
    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;
188
        CPUState *cs;
189 190

        cpu = cpu_s390x_init(cpu_model);
191
        cs = CPU(cpu);
192 193

        ipi_states[i] = cpu;
194
        cs->halted = 1;
195 196 197 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
        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);
    }
}

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

236 237 238 239 240 241 242
    /* 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);

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

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

252 253 254
    /* register hypercalls */
    s390_virtio_register_hcalls();

255
    /* allocate RAM */
256 257
    memory_region_init_ram(ram, "s390.ram", my_ram_size);
    vmstate_register_ram_global(ram);
A
Avi Kivity 已提交
258
    memory_region_add_subregion(sysmem, 0, ram);
259

260 261 262 263 264 265 266 267 268
    /* 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);

269
    /* allocate storage keys */
270
    storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
271

272
    /* init CPUs */
273
    s390_init_cpus(args->cpu_model, storage_keys);
274 275

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

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

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

machine_init(s390_machine_init);