s390-virtio.c 7.8 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
    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 136
    CPUS390XState *env = &cpu->env;

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

144
unsigned s390_del_running_cpu(S390CPU *cpu)
145
{
146 147
    CPUS390XState *env = &cpu->env;

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

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 213 214 215 216 217 218 219
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);
    }
}

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

232 233 234 235 236 237 238 239 240
    /* 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 已提交
241

242
    /* get a BUS */
243
    s390_bus = s390_virtio_bus_init(&my_ram_size);
H
Heinz Graalfs 已提交
244
    s390_sclp_init();
245 246
    s390_init_ipl_dev(args->kernel_filename, args->kernel_cmdline,
                      args->initrd_filename);
247

248 249 250
    /* register hypercalls */
    s390_virtio_register_hcalls();

251
    /* allocate RAM */
252 253
    memory_region_init_ram(ram, "s390.ram", my_ram_size);
    vmstate_register_ram_global(ram);
A
Avi Kivity 已提交
254
    memory_region_add_subregion(sysmem, 0, ram);
255

256 257 258 259 260 261 262 263 264
    /* 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);

265
    /* allocate storage keys */
266
    storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
267

268
    /* init CPUs */
269
    s390_init_cpus(args->cpu_model, storage_keys);
270 271

    /* Create VirtIO network adapters */
272
    s390_create_virtio_net((BusState *)s390_bus, "virtio-net-s390");
273 274 275 276 277 278 279
}

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

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

machine_init(s390_machine_init);