Statistics
| Branch: | Revision:

root / hw / s390-virtio.c @ 22486aa0

History | View | Annotate | Download (7.7 kB)

1 8cb310e1 Alexander Graf
/*
2 8cb310e1 Alexander Graf
 * QEMU S390 virtio target
3 8cb310e1 Alexander Graf
 *
4 8cb310e1 Alexander Graf
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 8cb310e1 Alexander Graf
 *
6 8cb310e1 Alexander Graf
 * This library is free software; you can redistribute it and/or
7 8cb310e1 Alexander Graf
 * modify it under the terms of the GNU Lesser General Public
8 8cb310e1 Alexander Graf
 * License as published by the Free Software Foundation; either
9 8cb310e1 Alexander Graf
 * version 2 of the License, or (at your option) any later version.
10 8cb310e1 Alexander Graf
 *
11 8cb310e1 Alexander Graf
 * This library is distributed in the hope that it will be useful,
12 8cb310e1 Alexander Graf
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 8cb310e1 Alexander Graf
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 8cb310e1 Alexander Graf
 * Lesser General Public License for more details.
15 8cb310e1 Alexander Graf
 *
16 8cb310e1 Alexander Graf
 * You should have received a copy of the GNU Lesser General Public
17 8cb310e1 Alexander Graf
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 8cb310e1 Alexander Graf
 */
19 8cb310e1 Alexander Graf
20 8cb310e1 Alexander Graf
#include "hw.h"
21 8cb310e1 Alexander Graf
#include "block.h"
22 6c33286a Alexander Graf
#include "blockdev.h"
23 8cb310e1 Alexander Graf
#include "sysemu.h"
24 8cb310e1 Alexander Graf
#include "net.h"
25 8cb310e1 Alexander Graf
#include "boards.h"
26 8cb310e1 Alexander Graf
#include "monitor.h"
27 8cb310e1 Alexander Graf
#include "loader.h"
28 8cb310e1 Alexander Graf
#include "elf.h"
29 8cb310e1 Alexander Graf
#include "hw/virtio.h"
30 8cb310e1 Alexander Graf
#include "hw/sysbus.h"
31 8cb310e1 Alexander Graf
#include "kvm.h"
32 8cb310e1 Alexander Graf
33 8cb310e1 Alexander Graf
#include "hw/s390-virtio-bus.h"
34 8cb310e1 Alexander Graf
35 8cb310e1 Alexander Graf
//#define DEBUG_S390
36 8cb310e1 Alexander Graf
37 8cb310e1 Alexander Graf
#ifdef DEBUG_S390
38 8cb310e1 Alexander Graf
#define dprintf(fmt, ...) \
39 8cb310e1 Alexander Graf
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40 8cb310e1 Alexander Graf
#else
41 8cb310e1 Alexander Graf
#define dprintf(fmt, ...) \
42 8cb310e1 Alexander Graf
    do { } while (0)
43 8cb310e1 Alexander Graf
#endif
44 8cb310e1 Alexander Graf
45 8cb310e1 Alexander Graf
#define KVM_S390_VIRTIO_NOTIFY          0
46 8cb310e1 Alexander Graf
#define KVM_S390_VIRTIO_RESET           1
47 8cb310e1 Alexander Graf
#define KVM_S390_VIRTIO_SET_STATUS      2
48 8cb310e1 Alexander Graf
49 8cb310e1 Alexander Graf
#define KERN_IMAGE_START                0x010000UL
50 8cb310e1 Alexander Graf
#define KERN_PARM_AREA                  0x010480UL
51 8cb310e1 Alexander Graf
#define INITRD_START                    0x800000UL
52 8cb310e1 Alexander Graf
#define INITRD_PARM_START               0x010408UL
53 8cb310e1 Alexander Graf
#define INITRD_PARM_SIZE                0x010410UL
54 8cb310e1 Alexander Graf
#define PARMFILE_START                  0x001000UL
55 8cb310e1 Alexander Graf
56 fe270d04 Alexander Graf
#define ZIPL_START                        0x009000UL
57 fe270d04 Alexander Graf
#define ZIPL_LOAD_ADDR                        0x009000UL
58 fe270d04 Alexander Graf
#define ZIPL_FILENAME                        "s390-zipl.rom"
59 fe270d04 Alexander Graf
60 8cb310e1 Alexander Graf
#define MAX_BLK_DEVS                    10
61 8cb310e1 Alexander Graf
62 8cb310e1 Alexander Graf
static VirtIOS390Bus *s390_bus;
63 8cb310e1 Alexander Graf
static CPUState **ipi_states;
64 8cb310e1 Alexander Graf
65 8cb310e1 Alexander Graf
void irq_info(Monitor *mon);
66 8cb310e1 Alexander Graf
void pic_info(Monitor *mon);
67 8cb310e1 Alexander Graf
68 8cb310e1 Alexander Graf
void irq_info(Monitor *mon)
69 8cb310e1 Alexander Graf
{
70 8cb310e1 Alexander Graf
}
71 8cb310e1 Alexander Graf
72 8cb310e1 Alexander Graf
void pic_info(Monitor *mon)
73 8cb310e1 Alexander Graf
{
74 8cb310e1 Alexander Graf
}
75 8cb310e1 Alexander Graf
76 8cb310e1 Alexander Graf
CPUState *s390_cpu_addr2state(uint16_t cpu_addr)
77 8cb310e1 Alexander Graf
{
78 8cb310e1 Alexander Graf
    if (cpu_addr >= smp_cpus) {
79 8cb310e1 Alexander Graf
        return NULL;
80 8cb310e1 Alexander Graf
    }
81 8cb310e1 Alexander Graf
82 8cb310e1 Alexander Graf
    return ipi_states[cpu_addr];
83 8cb310e1 Alexander Graf
}
84 8cb310e1 Alexander Graf
85 8d5192ee Alexander Graf
int s390_virtio_hypercall(CPUState *env, uint64_t mem, uint64_t hypercall)
86 8cb310e1 Alexander Graf
{
87 8cb310e1 Alexander Graf
    int r = 0, i;
88 8cb310e1 Alexander Graf
89 8d5192ee Alexander Graf
    dprintf("KVM hypercall: %ld\n", hypercall);
90 8d5192ee Alexander Graf
    switch (hypercall) {
91 8cb310e1 Alexander Graf
    case KVM_S390_VIRTIO_NOTIFY:
92 8cb310e1 Alexander Graf
        if (mem > ram_size) {
93 8cb310e1 Alexander Graf
            VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
94 8cb310e1 Alexander Graf
                                                               mem, &i);
95 8cb310e1 Alexander Graf
            if (dev) {
96 8cb310e1 Alexander Graf
                virtio_queue_notify(dev->vdev, i);
97 8cb310e1 Alexander Graf
            } else {
98 8cb310e1 Alexander Graf
                r = -EINVAL;
99 8cb310e1 Alexander Graf
            }
100 8cb310e1 Alexander Graf
        } else {
101 8cb310e1 Alexander Graf
            /* Early printk */
102 8cb310e1 Alexander Graf
        }
103 8cb310e1 Alexander Graf
        break;
104 8cb310e1 Alexander Graf
    case KVM_S390_VIRTIO_RESET:
105 8cb310e1 Alexander Graf
    {
106 baf0b55a Alexander Graf
        VirtIOS390Device *dev;
107 baf0b55a Alexander Graf
108 baf0b55a Alexander Graf
        dev = s390_virtio_bus_find_mem(s390_bus, mem);
109 baf0b55a Alexander Graf
        virtio_reset(dev->vdev);
110 baf0b55a Alexander Graf
        s390_virtio_device_sync(dev);
111 8cb310e1 Alexander Graf
        break;
112 8cb310e1 Alexander Graf
    }
113 8cb310e1 Alexander Graf
    case KVM_S390_VIRTIO_SET_STATUS:
114 8cb310e1 Alexander Graf
    {
115 8cb310e1 Alexander Graf
        VirtIOS390Device *dev;
116 8cb310e1 Alexander Graf
117 8cb310e1 Alexander Graf
        dev = s390_virtio_bus_find_mem(s390_bus, mem);
118 8cb310e1 Alexander Graf
        if (dev) {
119 8cb310e1 Alexander Graf
            s390_virtio_device_update_status(dev);
120 8cb310e1 Alexander Graf
        } else {
121 8cb310e1 Alexander Graf
            r = -EINVAL;
122 8cb310e1 Alexander Graf
        }
123 8cb310e1 Alexander Graf
        break;
124 8cb310e1 Alexander Graf
    }
125 8cb310e1 Alexander Graf
    default:
126 8cb310e1 Alexander Graf
        r = -EINVAL;
127 8cb310e1 Alexander Graf
        break;
128 8cb310e1 Alexander Graf
    }
129 8cb310e1 Alexander Graf
130 8d5192ee Alexander Graf
    return r;
131 8cb310e1 Alexander Graf
}
132 8cb310e1 Alexander Graf
133 8cb310e1 Alexander Graf
/* PC hardware initialisation */
134 22486aa0 Christian Borntraeger
static void s390_init(ram_addr_t my_ram_size,
135 8cb310e1 Alexander Graf
                      const char *boot_device,
136 8cb310e1 Alexander Graf
                      const char *kernel_filename,
137 8cb310e1 Alexander Graf
                      const char *kernel_cmdline,
138 8cb310e1 Alexander Graf
                      const char *initrd_filename,
139 8cb310e1 Alexander Graf
                      const char *cpu_model)
140 8cb310e1 Alexander Graf
{
141 8cb310e1 Alexander Graf
    CPUState *env = NULL;
142 8cb310e1 Alexander Graf
    ram_addr_t ram_addr;
143 8cb310e1 Alexander Graf
    ram_addr_t kernel_size = 0;
144 8cb310e1 Alexander Graf
    ram_addr_t initrd_offset;
145 8cb310e1 Alexander Graf
    ram_addr_t initrd_size = 0;
146 22486aa0 Christian Borntraeger
    int shift = 0;
147 8d5192ee Alexander Graf
    uint8_t *storage_keys;
148 8cb310e1 Alexander Graf
    int i;
149 8cb310e1 Alexander Graf
150 22486aa0 Christian Borntraeger
    /* s390x ram size detection needs a 16bit multiplier + an increment. So
151 22486aa0 Christian Borntraeger
       guests > 64GB can be specified in 2MB steps etc. */
152 22486aa0 Christian Borntraeger
    while ((my_ram_size >> (20 + shift)) > 65535) {
153 22486aa0 Christian Borntraeger
        shift++;
154 22486aa0 Christian Borntraeger
    }
155 22486aa0 Christian Borntraeger
    my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
156 22486aa0 Christian Borntraeger
157 22486aa0 Christian Borntraeger
    /* lets propagate the changed ram size into the global variable. */
158 22486aa0 Christian Borntraeger
    ram_size = my_ram_size;
159 e249651c Alexander Graf
160 8cb310e1 Alexander Graf
    /* get a BUS */
161 22486aa0 Christian Borntraeger
    s390_bus = s390_virtio_bus_init(&my_ram_size);
162 8cb310e1 Alexander Graf
163 8cb310e1 Alexander Graf
    /* allocate RAM */
164 22486aa0 Christian Borntraeger
    ram_addr = qemu_ram_alloc(NULL, "s390.ram", my_ram_size);
165 22486aa0 Christian Borntraeger
    cpu_register_physical_memory(0, my_ram_size, ram_addr);
166 8cb310e1 Alexander Graf
167 8d5192ee Alexander Graf
    /* allocate storage keys */
168 22486aa0 Christian Borntraeger
    storage_keys = qemu_mallocz(my_ram_size / TARGET_PAGE_SIZE);
169 8d5192ee Alexander Graf
170 8cb310e1 Alexander Graf
    /* init CPUs */
171 8cb310e1 Alexander Graf
    if (cpu_model == NULL) {
172 8cb310e1 Alexander Graf
        cpu_model = "host";
173 8cb310e1 Alexander Graf
    }
174 8cb310e1 Alexander Graf
175 8cb310e1 Alexander Graf
    ipi_states = qemu_malloc(sizeof(CPUState *) * smp_cpus);
176 8cb310e1 Alexander Graf
177 8cb310e1 Alexander Graf
    for (i = 0; i < smp_cpus; i++) {
178 8cb310e1 Alexander Graf
        CPUState *tmp_env;
179 8cb310e1 Alexander Graf
180 8cb310e1 Alexander Graf
        tmp_env = cpu_init(cpu_model);
181 8cb310e1 Alexander Graf
        if (!env) {
182 8cb310e1 Alexander Graf
            env = tmp_env;
183 8cb310e1 Alexander Graf
        }
184 8cb310e1 Alexander Graf
        ipi_states[i] = tmp_env;
185 8cb310e1 Alexander Graf
        tmp_env->halted = 1;
186 8cb310e1 Alexander Graf
        tmp_env->exception_index = EXCP_HLT;
187 8d5192ee Alexander Graf
        tmp_env->storage_keys = storage_keys;
188 8cb310e1 Alexander Graf
    }
189 8cb310e1 Alexander Graf
190 8cb310e1 Alexander Graf
    env->halted = 0;
191 8cb310e1 Alexander Graf
    env->exception_index = 0;
192 8cb310e1 Alexander Graf
193 8cb310e1 Alexander Graf
    if (kernel_filename) {
194 8cb310e1 Alexander Graf
        kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0));
195 8cb310e1 Alexander Graf
196 8cb310e1 Alexander Graf
        if (lduw_phys(KERN_IMAGE_START) != 0x0dd0) {
197 8cb310e1 Alexander Graf
            fprintf(stderr, "Specified image is not an s390 boot image\n");
198 8cb310e1 Alexander Graf
            exit(1);
199 8cb310e1 Alexander Graf
        }
200 8cb310e1 Alexander Graf
201 8cb310e1 Alexander Graf
        env->psw.addr = KERN_IMAGE_START;
202 0435d393 Michael S. Tsirkin
        env->psw.mask = 0x0000000180000000ULL;
203 fe270d04 Alexander Graf
    } else {
204 fe270d04 Alexander Graf
        ram_addr_t bios_size = 0;
205 fe270d04 Alexander Graf
        char *bios_filename;
206 fe270d04 Alexander Graf
207 fe270d04 Alexander Graf
        /* Load zipl bootloader */
208 fe270d04 Alexander Graf
        if (bios_name == NULL) {
209 fe270d04 Alexander Graf
            bios_name = ZIPL_FILENAME;
210 fe270d04 Alexander Graf
        }
211 fe270d04 Alexander Graf
212 fe270d04 Alexander Graf
        bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
213 fe270d04 Alexander Graf
        bios_size = load_image(bios_filename, qemu_get_ram_ptr(ZIPL_LOAD_ADDR));
214 9f953ca0 Stefan Weil
        qemu_free(bios_filename);
215 fe270d04 Alexander Graf
216 fe270d04 Alexander Graf
        if ((long)bios_size < 0) {
217 fe270d04 Alexander Graf
            hw_error("could not load bootloader '%s'\n", bios_name);
218 fe270d04 Alexander Graf
        }
219 fe270d04 Alexander Graf
220 fe270d04 Alexander Graf
        if (bios_size > 4096) {
221 fe270d04 Alexander Graf
            hw_error("stage1 bootloader is > 4k\n");
222 fe270d04 Alexander Graf
        }
223 fe270d04 Alexander Graf
224 fe270d04 Alexander Graf
        env->psw.addr = ZIPL_START;
225 fe270d04 Alexander Graf
        env->psw.mask = 0x0000000180000000ULL;
226 8cb310e1 Alexander Graf
    }
227 8cb310e1 Alexander Graf
228 8cb310e1 Alexander Graf
    if (initrd_filename) {
229 8cb310e1 Alexander Graf
        initrd_offset = INITRD_START;
230 8cb310e1 Alexander Graf
        while (kernel_size + 0x100000 > initrd_offset) {
231 8cb310e1 Alexander Graf
            initrd_offset += 0x100000;
232 8cb310e1 Alexander Graf
        }
233 8cb310e1 Alexander Graf
        initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset));
234 8cb310e1 Alexander Graf
235 8cb310e1 Alexander Graf
        stq_phys(INITRD_PARM_START, initrd_offset);
236 8cb310e1 Alexander Graf
        stq_phys(INITRD_PARM_SIZE, initrd_size);
237 8cb310e1 Alexander Graf
    }
238 8cb310e1 Alexander Graf
239 8cb310e1 Alexander Graf
    if (kernel_cmdline) {
240 54f7b4a3 Stefan Weil
        cpu_physical_memory_write(KERN_PARM_AREA, kernel_cmdline,
241 54f7b4a3 Stefan Weil
                                  strlen(kernel_cmdline));
242 8cb310e1 Alexander Graf
    }
243 8cb310e1 Alexander Graf
244 8cb310e1 Alexander Graf
    /* Create VirtIO network adapters */
245 8cb310e1 Alexander Graf
    for(i = 0; i < nb_nics; i++) {
246 8cb310e1 Alexander Graf
        NICInfo *nd = &nd_table[i];
247 8cb310e1 Alexander Graf
        DeviceState *dev;
248 8cb310e1 Alexander Graf
249 8cb310e1 Alexander Graf
        if (!nd->model) {
250 5a2b3fc5 Stefan Weil
            nd->model = qemu_strdup("virtio");
251 8cb310e1 Alexander Graf
        }
252 8cb310e1 Alexander Graf
253 8cb310e1 Alexander Graf
        if (strcmp(nd->model, "virtio")) {
254 8cb310e1 Alexander Graf
            fprintf(stderr, "S390 only supports VirtIO nics\n");
255 8cb310e1 Alexander Graf
            exit(1);
256 8cb310e1 Alexander Graf
        }
257 8cb310e1 Alexander Graf
258 8cb310e1 Alexander Graf
        dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
259 8cb310e1 Alexander Graf
        qdev_set_nic_properties(dev, nd);
260 8cb310e1 Alexander Graf
        qdev_init_nofail(dev);
261 8cb310e1 Alexander Graf
    }
262 8cb310e1 Alexander Graf
263 8cb310e1 Alexander Graf
    /* Create VirtIO disk drives */
264 8cb310e1 Alexander Graf
    for(i = 0; i < MAX_BLK_DEVS; i++) {
265 8cb310e1 Alexander Graf
        DriveInfo *dinfo;
266 8cb310e1 Alexander Graf
        DeviceState *dev;
267 8cb310e1 Alexander Graf
268 8cb310e1 Alexander Graf
        dinfo = drive_get(IF_IDE, 0, i);
269 8cb310e1 Alexander Graf
        if (!dinfo) {
270 8cb310e1 Alexander Graf
            continue;
271 8cb310e1 Alexander Graf
        }
272 8cb310e1 Alexander Graf
273 8cb310e1 Alexander Graf
        dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
274 18846dee Markus Armbruster
        qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
275 8cb310e1 Alexander Graf
        qdev_init_nofail(dev);
276 8cb310e1 Alexander Graf
    }
277 8cb310e1 Alexander Graf
}
278 8cb310e1 Alexander Graf
279 8cb310e1 Alexander Graf
static QEMUMachine s390_machine = {
280 8cb310e1 Alexander Graf
    .name = "s390-virtio",
281 8cb310e1 Alexander Graf
    .alias = "s390",
282 8cb310e1 Alexander Graf
    .desc = "VirtIO based S390 machine",
283 8cb310e1 Alexander Graf
    .init = s390_init,
284 986c5f78 Gerd Hoffmann
    .no_serial = 1,
285 986c5f78 Gerd Hoffmann
    .no_parallel = 1,
286 cf708987 Michael S. Tsirkin
    .use_virtcon = 1,
287 986c5f78 Gerd Hoffmann
    .no_vga = 1,
288 8cb310e1 Alexander Graf
    .max_cpus = 255,
289 8cb310e1 Alexander Graf
    .is_default = 1,
290 8cb310e1 Alexander Graf
};
291 8cb310e1 Alexander Graf
292 8cb310e1 Alexander Graf
static void s390_machine_init(void)
293 8cb310e1 Alexander Graf
{
294 8cb310e1 Alexander Graf
    qemu_register_machine(&s390_machine);
295 8cb310e1 Alexander Graf
}
296 8cb310e1 Alexander Graf
297 8cb310e1 Alexander Graf
machine_init(s390_machine_init);