Statistics
| Branch: | Revision:

root / hw / s390-virtio.c @ ea375f9a

History | View | Annotate | Download (6.6 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 8cb310e1 Alexander Graf
#include "sysemu.h"
23 8cb310e1 Alexander Graf
#include "net.h"
24 8cb310e1 Alexander Graf
#include "boards.h"
25 8cb310e1 Alexander Graf
#include "monitor.h"
26 8cb310e1 Alexander Graf
#include "loader.h"
27 8cb310e1 Alexander Graf
#include "elf.h"
28 8cb310e1 Alexander Graf
#include "hw/virtio.h"
29 8cb310e1 Alexander Graf
#include "hw/sysbus.h"
30 8cb310e1 Alexander Graf
#include "kvm.h"
31 8cb310e1 Alexander Graf
32 8cb310e1 Alexander Graf
#include "hw/s390-virtio-bus.h"
33 8cb310e1 Alexander Graf
34 8cb310e1 Alexander Graf
//#define DEBUG_S390
35 8cb310e1 Alexander Graf
36 8cb310e1 Alexander Graf
#ifdef DEBUG_S390
37 8cb310e1 Alexander Graf
#define dprintf(fmt, ...) \
38 8cb310e1 Alexander Graf
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
39 8cb310e1 Alexander Graf
#else
40 8cb310e1 Alexander Graf
#define dprintf(fmt, ...) \
41 8cb310e1 Alexander Graf
    do { } while (0)
42 8cb310e1 Alexander Graf
#endif
43 8cb310e1 Alexander Graf
44 8cb310e1 Alexander Graf
#define KVM_S390_VIRTIO_NOTIFY          0
45 8cb310e1 Alexander Graf
#define KVM_S390_VIRTIO_RESET           1
46 8cb310e1 Alexander Graf
#define KVM_S390_VIRTIO_SET_STATUS      2
47 8cb310e1 Alexander Graf
48 8cb310e1 Alexander Graf
#define KERN_IMAGE_START                0x010000UL
49 8cb310e1 Alexander Graf
#define KERN_PARM_AREA                  0x010480UL
50 8cb310e1 Alexander Graf
#define INITRD_START                    0x800000UL
51 8cb310e1 Alexander Graf
#define INITRD_PARM_START               0x010408UL
52 8cb310e1 Alexander Graf
#define INITRD_PARM_SIZE                0x010410UL
53 8cb310e1 Alexander Graf
#define PARMFILE_START                  0x001000UL
54 8cb310e1 Alexander Graf
55 8cb310e1 Alexander Graf
#define MAX_BLK_DEVS                    10
56 8cb310e1 Alexander Graf
57 8cb310e1 Alexander Graf
static VirtIOS390Bus *s390_bus;
58 8cb310e1 Alexander Graf
static CPUState **ipi_states;
59 8cb310e1 Alexander Graf
60 8cb310e1 Alexander Graf
void irq_info(Monitor *mon);
61 8cb310e1 Alexander Graf
void pic_info(Monitor *mon);
62 8cb310e1 Alexander Graf
63 8cb310e1 Alexander Graf
void irq_info(Monitor *mon)
64 8cb310e1 Alexander Graf
{
65 8cb310e1 Alexander Graf
}
66 8cb310e1 Alexander Graf
67 8cb310e1 Alexander Graf
void pic_info(Monitor *mon)
68 8cb310e1 Alexander Graf
{
69 8cb310e1 Alexander Graf
}
70 8cb310e1 Alexander Graf
71 8cb310e1 Alexander Graf
CPUState *s390_cpu_addr2state(uint16_t cpu_addr)
72 8cb310e1 Alexander Graf
{
73 8cb310e1 Alexander Graf
    if (cpu_addr >= smp_cpus) {
74 8cb310e1 Alexander Graf
        return NULL;
75 8cb310e1 Alexander Graf
    }
76 8cb310e1 Alexander Graf
77 8cb310e1 Alexander Graf
    return ipi_states[cpu_addr];
78 8cb310e1 Alexander Graf
}
79 8cb310e1 Alexander Graf
80 8cb310e1 Alexander Graf
int s390_virtio_hypercall(CPUState *env)
81 8cb310e1 Alexander Graf
{
82 8cb310e1 Alexander Graf
    int r = 0, i;
83 8cb310e1 Alexander Graf
    target_ulong mem = env->regs[2];
84 8cb310e1 Alexander Graf
85 8cb310e1 Alexander Graf
    dprintf("KVM hypercall: %ld\n", env->regs[1]);
86 8cb310e1 Alexander Graf
    switch (env->regs[1]) {
87 8cb310e1 Alexander Graf
    case KVM_S390_VIRTIO_NOTIFY:
88 8cb310e1 Alexander Graf
        if (mem > ram_size) {
89 8cb310e1 Alexander Graf
            VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
90 8cb310e1 Alexander Graf
                                                               mem, &i);
91 8cb310e1 Alexander Graf
            if (dev) {
92 8cb310e1 Alexander Graf
                virtio_queue_notify(dev->vdev, i);
93 8cb310e1 Alexander Graf
            } else {
94 8cb310e1 Alexander Graf
                r = -EINVAL;
95 8cb310e1 Alexander Graf
            }
96 8cb310e1 Alexander Graf
        } else {
97 8cb310e1 Alexander Graf
            /* Early printk */
98 8cb310e1 Alexander Graf
        }
99 8cb310e1 Alexander Graf
        break;
100 8cb310e1 Alexander Graf
    case KVM_S390_VIRTIO_RESET:
101 8cb310e1 Alexander Graf
    {
102 8cb310e1 Alexander Graf
        /* Virtio_reset resets the internal addresses, so we'd have to sync
103 8cb310e1 Alexander Graf
           them up again. We don't want to reallocate a vring though, so let's
104 8cb310e1 Alexander Graf
           just not reset. */
105 8cb310e1 Alexander Graf
        /* virtio_reset(dev->vdev); */
106 8cb310e1 Alexander Graf
        break;
107 8cb310e1 Alexander Graf
    }
108 8cb310e1 Alexander Graf
    case KVM_S390_VIRTIO_SET_STATUS:
109 8cb310e1 Alexander Graf
    {
110 8cb310e1 Alexander Graf
        VirtIOS390Device *dev;
111 8cb310e1 Alexander Graf
112 8cb310e1 Alexander Graf
        dev = s390_virtio_bus_find_mem(s390_bus, mem);
113 8cb310e1 Alexander Graf
        if (dev) {
114 8cb310e1 Alexander Graf
            s390_virtio_device_update_status(dev);
115 8cb310e1 Alexander Graf
        } else {
116 8cb310e1 Alexander Graf
            r = -EINVAL;
117 8cb310e1 Alexander Graf
        }
118 8cb310e1 Alexander Graf
        break;
119 8cb310e1 Alexander Graf
    }
120 8cb310e1 Alexander Graf
    default:
121 8cb310e1 Alexander Graf
        r = -EINVAL;
122 8cb310e1 Alexander Graf
        break;
123 8cb310e1 Alexander Graf
    }
124 8cb310e1 Alexander Graf
125 8cb310e1 Alexander Graf
    env->regs[2] = r;
126 8cb310e1 Alexander Graf
    return 0;
127 8cb310e1 Alexander Graf
}
128 8cb310e1 Alexander Graf
129 8cb310e1 Alexander Graf
/* PC hardware initialisation */
130 8cb310e1 Alexander Graf
static void s390_init(ram_addr_t ram_size,
131 8cb310e1 Alexander Graf
                      const char *boot_device,
132 8cb310e1 Alexander Graf
                      const char *kernel_filename,
133 8cb310e1 Alexander Graf
                      const char *kernel_cmdline,
134 8cb310e1 Alexander Graf
                      const char *initrd_filename,
135 8cb310e1 Alexander Graf
                      const char *cpu_model)
136 8cb310e1 Alexander Graf
{
137 8cb310e1 Alexander Graf
    CPUState *env = NULL;
138 8cb310e1 Alexander Graf
    ram_addr_t ram_addr;
139 8cb310e1 Alexander Graf
    ram_addr_t kernel_size = 0;
140 8cb310e1 Alexander Graf
    ram_addr_t initrd_offset;
141 8cb310e1 Alexander Graf
    ram_addr_t initrd_size = 0;
142 8cb310e1 Alexander Graf
    int i;
143 8cb310e1 Alexander Graf
144 e249651c Alexander Graf
    /* XXX we only work on KVM for now */
145 e249651c Alexander Graf
146 e249651c Alexander Graf
    if (!kvm_enabled()) {
147 e249651c Alexander Graf
        fprintf(stderr, "The S390 target only works with KVM enabled\n");
148 e249651c Alexander Graf
        exit(1);
149 e249651c Alexander Graf
    }
150 e249651c Alexander Graf
151 8cb310e1 Alexander Graf
    /* get a BUS */
152 8cb310e1 Alexander Graf
    s390_bus = s390_virtio_bus_init(&ram_size);
153 8cb310e1 Alexander Graf
154 8cb310e1 Alexander Graf
    /* allocate RAM */
155 8cb310e1 Alexander Graf
    ram_addr = qemu_ram_alloc(ram_size);
156 8cb310e1 Alexander Graf
    cpu_register_physical_memory(0, ram_size, ram_addr);
157 8cb310e1 Alexander Graf
158 8cb310e1 Alexander Graf
    /* init CPUs */
159 8cb310e1 Alexander Graf
    if (cpu_model == NULL) {
160 8cb310e1 Alexander Graf
        cpu_model = "host";
161 8cb310e1 Alexander Graf
    }
162 8cb310e1 Alexander Graf
163 8cb310e1 Alexander Graf
    ipi_states = qemu_malloc(sizeof(CPUState *) * smp_cpus);
164 8cb310e1 Alexander Graf
165 8cb310e1 Alexander Graf
    for (i = 0; i < smp_cpus; i++) {
166 8cb310e1 Alexander Graf
        CPUState *tmp_env;
167 8cb310e1 Alexander Graf
168 8cb310e1 Alexander Graf
        tmp_env = cpu_init(cpu_model);
169 8cb310e1 Alexander Graf
        if (!env) {
170 8cb310e1 Alexander Graf
            env = tmp_env;
171 8cb310e1 Alexander Graf
        }
172 8cb310e1 Alexander Graf
        ipi_states[i] = tmp_env;
173 8cb310e1 Alexander Graf
        tmp_env->halted = 1;
174 8cb310e1 Alexander Graf
        tmp_env->exception_index = EXCP_HLT;
175 8cb310e1 Alexander Graf
    }
176 8cb310e1 Alexander Graf
177 8cb310e1 Alexander Graf
    env->halted = 0;
178 8cb310e1 Alexander Graf
    env->exception_index = 0;
179 8cb310e1 Alexander Graf
180 8cb310e1 Alexander Graf
    if (kernel_filename) {
181 8cb310e1 Alexander Graf
        kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0));
182 8cb310e1 Alexander Graf
183 8cb310e1 Alexander Graf
        if (lduw_phys(KERN_IMAGE_START) != 0x0dd0) {
184 8cb310e1 Alexander Graf
            fprintf(stderr, "Specified image is not an s390 boot image\n");
185 8cb310e1 Alexander Graf
            exit(1);
186 8cb310e1 Alexander Graf
        }
187 8cb310e1 Alexander Graf
188 8cb310e1 Alexander Graf
        env->psw.addr = KERN_IMAGE_START;
189 0435d393 Michael S. Tsirkin
        env->psw.mask = 0x0000000180000000ULL;
190 8cb310e1 Alexander Graf
    }
191 8cb310e1 Alexander Graf
192 8cb310e1 Alexander Graf
    if (initrd_filename) {
193 8cb310e1 Alexander Graf
        initrd_offset = INITRD_START;
194 8cb310e1 Alexander Graf
        while (kernel_size + 0x100000 > initrd_offset) {
195 8cb310e1 Alexander Graf
            initrd_offset += 0x100000;
196 8cb310e1 Alexander Graf
        }
197 8cb310e1 Alexander Graf
        initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset));
198 8cb310e1 Alexander Graf
199 8cb310e1 Alexander Graf
        stq_phys(INITRD_PARM_START, initrd_offset);
200 8cb310e1 Alexander Graf
        stq_phys(INITRD_PARM_SIZE, initrd_size);
201 8cb310e1 Alexander Graf
    }
202 8cb310e1 Alexander Graf
203 8cb310e1 Alexander Graf
    if (kernel_cmdline) {
204 8cb310e1 Alexander Graf
        cpu_physical_memory_rw(KERN_PARM_AREA, (uint8_t *)kernel_cmdline,
205 8cb310e1 Alexander Graf
                               strlen(kernel_cmdline), 1);
206 8cb310e1 Alexander Graf
    }
207 8cb310e1 Alexander Graf
208 8cb310e1 Alexander Graf
    /* Create VirtIO network adapters */
209 8cb310e1 Alexander Graf
    for(i = 0; i < nb_nics; i++) {
210 8cb310e1 Alexander Graf
        NICInfo *nd = &nd_table[i];
211 8cb310e1 Alexander Graf
        DeviceState *dev;
212 8cb310e1 Alexander Graf
213 8cb310e1 Alexander Graf
        if (!nd->model) {
214 5a2b3fc5 Stefan Weil
            nd->model = qemu_strdup("virtio");
215 8cb310e1 Alexander Graf
        }
216 8cb310e1 Alexander Graf
217 8cb310e1 Alexander Graf
        if (strcmp(nd->model, "virtio")) {
218 8cb310e1 Alexander Graf
            fprintf(stderr, "S390 only supports VirtIO nics\n");
219 8cb310e1 Alexander Graf
            exit(1);
220 8cb310e1 Alexander Graf
        }
221 8cb310e1 Alexander Graf
222 8cb310e1 Alexander Graf
        dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
223 8cb310e1 Alexander Graf
        qdev_set_nic_properties(dev, nd);
224 8cb310e1 Alexander Graf
        qdev_init_nofail(dev);
225 8cb310e1 Alexander Graf
    }
226 8cb310e1 Alexander Graf
227 8cb310e1 Alexander Graf
    /* Create VirtIO disk drives */
228 8cb310e1 Alexander Graf
    for(i = 0; i < MAX_BLK_DEVS; i++) {
229 8cb310e1 Alexander Graf
        DriveInfo *dinfo;
230 8cb310e1 Alexander Graf
        DeviceState *dev;
231 8cb310e1 Alexander Graf
232 8cb310e1 Alexander Graf
        dinfo = drive_get(IF_IDE, 0, i);
233 8cb310e1 Alexander Graf
        if (!dinfo) {
234 8cb310e1 Alexander Graf
            continue;
235 8cb310e1 Alexander Graf
        }
236 8cb310e1 Alexander Graf
237 8cb310e1 Alexander Graf
        dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
238 8cb310e1 Alexander Graf
        qdev_prop_set_drive(dev, "drive", dinfo);
239 8cb310e1 Alexander Graf
        qdev_init_nofail(dev);
240 8cb310e1 Alexander Graf
    }
241 8cb310e1 Alexander Graf
}
242 8cb310e1 Alexander Graf
243 8cb310e1 Alexander Graf
static QEMUMachine s390_machine = {
244 8cb310e1 Alexander Graf
    .name = "s390-virtio",
245 8cb310e1 Alexander Graf
    .alias = "s390",
246 8cb310e1 Alexander Graf
    .desc = "VirtIO based S390 machine",
247 8cb310e1 Alexander Graf
    .init = s390_init,
248 986c5f78 Gerd Hoffmann
    .no_serial = 1,
249 986c5f78 Gerd Hoffmann
    .no_parallel = 1,
250 cf708987 Michael S. Tsirkin
    .use_virtcon = 1,
251 986c5f78 Gerd Hoffmann
    .no_vga = 1,
252 8cb310e1 Alexander Graf
    .max_cpus = 255,
253 8cb310e1 Alexander Graf
    .is_default = 1,
254 8cb310e1 Alexander Graf
};
255 8cb310e1 Alexander Graf
256 8cb310e1 Alexander Graf
static void s390_machine_init(void)
257 8cb310e1 Alexander Graf
{
258 8cb310e1 Alexander Graf
    qemu_register_machine(&s390_machine);
259 8cb310e1 Alexander Graf
}
260 8cb310e1 Alexander Graf
261 8cb310e1 Alexander Graf
machine_init(s390_machine_init);