s390-virtio.c 7.6 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"
32 33
#include "hw/virtio.h"
#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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    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);
    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 {
107 108
        r = -EINVAL;
    }
109
    return r;
110 111
}

112 113 114 115 116 117 118 119 120 121
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);
}

122 123 124 125 126 127 128 129
/*
 * 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;

A
Andreas Färber 已提交
130
void s390_add_running_cpu(CPUS390XState *env)
131 132 133 134 135 136 137 138
{
    if (env->halted) {
        s390_running_cpus++;
        env->halted = 0;
        env->exception_index = -1;
    }
}

A
Andreas Färber 已提交
139
unsigned s390_del_running_cpu(CPUS390XState *env)
140 141 142 143 144 145 146 147 148 149
{
    if (env->halted == 0) {
        assert(s390_running_cpus >= 1);
        s390_running_cpus--;
        env->halted = 1;
        env->exception_index = EXCP_HLT;
    }
    return s390_running_cpus;
}

150 151 152 153 154 155 156 157 158 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
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;

        cpu = cpu_s390x_init(cpu_model);

        ipi_states[i] = cpu;
        cpu->env.halted = 1;
        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);
    }
}

213
/* PC hardware initialisation */
214
static void s390_init(QEMUMachineInitArgs *args)
215
{
216
    ram_addr_t my_ram_size = args->ram_size;
A
Avi Kivity 已提交
217 218
    MemoryRegion *sysmem = get_system_memory();
    MemoryRegion *ram = g_new(MemoryRegion, 1);
219
    int shift = 0;
220
    uint8_t *storage_keys;
221
    void *virtio_region;
A
Avi Kivity 已提交
222 223
    hwaddr virtio_region_len;
    hwaddr virtio_region_start;
224

225 226 227 228 229 230 231 232 233
    /* 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);

    /* lets propagate the changed ram size into the global variable. */
    ram_size = my_ram_size;
A
Alexander Graf 已提交
234

235
    /* get a BUS */
236
    s390_bus = s390_virtio_bus_init(&my_ram_size);
H
Heinz Graalfs 已提交
237
    s390_sclp_init();
238 239
    s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
                      args->initrd_filename);
240

241 242 243
    /* register hypercalls */
    s390_virtio_register_hcalls();

244
    /* allocate RAM */
245 246
    memory_region_init_ram(ram, "s390.ram", my_ram_size);
    vmstate_register_ram_global(ram);
A
Avi Kivity 已提交
247
    memory_region_add_subregion(sysmem, 0, ram);
248

249 250 251 252 253 254 255 256 257
    /* 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);

258
    /* allocate storage keys */
259
    storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
260

261
    /* init CPUs */
262
    s390_init_cpus(args->cpu_model, storage_keys);
263 264

    /* Create VirtIO network adapters */
265
    s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
266 267 268 269 270 271 272
}

static QEMUMachine s390_machine = {
    .name = "s390-virtio",
    .alias = "s390",
    .desc = "VirtIO based S390 machine",
    .init = s390_init,
273
    .block_default_type = IF_VIRTIO,
274 275
    .no_cdrom = 1,
    .no_floppy = 1,
276 277
    .no_serial = 1,
    .no_parallel = 1,
278
    .no_sdcard = 1,
M
Michael S. Tsirkin 已提交
279
    .use_virtcon = 1,
280 281
    .max_cpus = 255,
    .is_default = 1,
282
    DEFAULT_MACHINE_OPTIONS,
283 284 285 286 287 288 289 290
};

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

machine_init(s390_machine_init);