s390-virtio.c 7.7 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;

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

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

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 213 214 215
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);
    }
}

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

228 229 230 231 232 233 234 235 236
    /* 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 已提交
237

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

244 245 246
    /* register hypercalls */
    s390_virtio_register_hcalls();

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

252 253 254 255 256 257 258 259 260
    /* 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);

261
    /* allocate storage keys */
262
    storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
263

264
    /* init CPUs */
265
    s390_init_cpus(args->cpu_model, storage_keys);
266 267

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

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

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

machine_init(s390_machine_init);