Statistics
| Branch: | Revision:

root / hw / s390-virtio-bus.c @ fecb93c4

History | View | Annotate | Download (10.6 kB)

1
/*
2
 * QEMU S390 virtio target
3
 *
4
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19

    
20
#include "hw.h"
21
#include "block.h"
22
#include "sysemu.h"
23
#include "net.h"
24
#include "boards.h"
25
#include "monitor.h"
26
#include "loader.h"
27
#include "elf.h"
28
#include "hw/virtio.h"
29
#include "hw/virtio-serial.h"
30
#include "hw/sysbus.h"
31
#include "kvm.h"
32

    
33
#include "hw/s390-virtio-bus.h"
34

    
35
/* #define DEBUG_S390 */
36

    
37
#ifdef DEBUG_S390
38
#define dprintf(fmt, ...) \
39
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40
#else
41
#define dprintf(fmt, ...) \
42
    do { } while (0)
43
#endif
44

    
45
struct BusInfo s390_virtio_bus_info = {
46
    .name       = "s390-virtio",
47
    .size       = sizeof(VirtIOS390Bus),
48
};
49

    
50
typedef struct {
51
    DeviceInfo qdev;
52
    int (*init)(VirtIOS390Device *dev);
53
} VirtIOS390DeviceInfo;
54

    
55

    
56
static const VirtIOBindings virtio_s390_bindings;
57

    
58
static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev);
59

    
60
VirtIOS390Bus *s390_virtio_bus_init(ram_addr_t *ram_size)
61
{
62
    VirtIOS390Bus *bus;
63
    BusState *_bus;
64
    DeviceState *dev;
65

    
66
    /* Create bridge device */
67
    dev = qdev_create(NULL, "s390-virtio-bridge");
68
    qdev_init_nofail(dev);
69

    
70
    /* Create bus on bridge device */
71

    
72
    _bus = qbus_create(&s390_virtio_bus_info, dev, "s390-virtio");
73
    bus = DO_UPCAST(VirtIOS390Bus, bus, _bus);
74

    
75
    bus->dev_page = *ram_size;
76
    bus->dev_offs = bus->dev_page;
77
    bus->next_ring = bus->dev_page + TARGET_PAGE_SIZE;
78

    
79
    /* Allocate RAM for VirtIO device pages (descriptors, queues, rings) */
80
    *ram_size += S390_DEVICE_PAGES * TARGET_PAGE_SIZE;
81

    
82
    return bus;
83
}
84

    
85
static int s390_virtio_device_init(VirtIOS390Device *dev, VirtIODevice *vdev)
86
{
87
    VirtIOS390Bus *bus;
88
    int dev_len;
89

    
90
    bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
91
    dev->vdev = vdev;
92
    dev->dev_offs = bus->dev_offs;
93
    dev->feat_len = sizeof(uint32_t); /* always keep 32 bits features */
94

    
95
    dev_len = VIRTIO_DEV_OFFS_CONFIG;
96
    dev_len += s390_virtio_device_num_vq(dev) * VIRTIO_VQCONFIG_LEN;
97
    dev_len += dev->feat_len * 2;
98
    dev_len += vdev->config_len;
99

    
100
    bus->dev_offs += dev_len;
101

    
102
    virtio_bind_device(vdev, &virtio_s390_bindings, dev);
103
    dev->host_features = vdev->get_features(vdev, dev->host_features);
104
    s390_virtio_device_sync(dev);
105

    
106
    return 0;
107
}
108

    
109
static int s390_virtio_net_init(VirtIOS390Device *dev)
110
{
111
    VirtIODevice *vdev;
112

    
113
    vdev = virtio_net_init((DeviceState *)dev, &dev->nic);
114
    if (!vdev) {
115
        return -1;
116
    }
117

    
118
    return s390_virtio_device_init(dev, vdev);
119
}
120

    
121
static int s390_virtio_blk_init(VirtIOS390Device *dev)
122
{
123
    VirtIODevice *vdev;
124

    
125
    vdev = virtio_blk_init((DeviceState *)dev, &dev->block);
126
    if (!vdev) {
127
        return -1;
128
    }
129

    
130
    return s390_virtio_device_init(dev, vdev);
131
}
132

    
133
static int s390_virtio_serial_init(VirtIOS390Device *dev)
134
{
135
    VirtIOS390Bus *bus;
136
    VirtIODevice *vdev;
137
    int r;
138

    
139
    bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
140

    
141
    vdev = virtio_serial_init((DeviceState *)dev, dev->max_virtserial_ports);
142
    if (!vdev) {
143
        return -1;
144
    }
145

    
146
    r = s390_virtio_device_init(dev, vdev);
147
    if (!r) {
148
        bus->console = dev;
149
    }
150

    
151
    return r;
152
}
153

    
154
static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
155
{
156
    ram_addr_t token_off;
157

    
158
    token_off = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
159
                (vq * VIRTIO_VQCONFIG_LEN) +
160
                VIRTIO_VQCONFIG_OFFS_TOKEN;
161

    
162
    return ldq_phys(token_off);
163
}
164

    
165
static ram_addr_t s390_virtio_device_num_vq(VirtIOS390Device *dev)
166
{
167
    VirtIODevice *vdev = dev->vdev;
168
    int num_vq;
169

    
170
    for (num_vq = 0; num_vq < VIRTIO_PCI_QUEUE_MAX; num_vq++) {
171
        if (!virtio_queue_get_num(vdev, num_vq)) {
172
            break;
173
        }
174
    }
175

    
176
    return num_vq;
177
}
178

    
179
static ram_addr_t s390_virtio_next_ring(VirtIOS390Bus *bus)
180
{
181
    ram_addr_t r = bus->next_ring;
182

    
183
    bus->next_ring += VIRTIO_RING_LEN;
184
    return r;
185
}
186

    
187
void s390_virtio_device_sync(VirtIOS390Device *dev)
188
{
189
    VirtIOS390Bus *bus = DO_UPCAST(VirtIOS390Bus, bus, dev->qdev.parent_bus);
190
    ram_addr_t cur_offs;
191
    uint8_t num_vq;
192
    int i;
193

    
194
    virtio_reset(dev->vdev);
195

    
196
    /* Sync dev space */
197
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_TYPE, dev->vdev->device_id);
198

    
199
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, s390_virtio_device_num_vq(dev));
200
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_FEATURE_LEN, dev->feat_len);
201

    
202
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG_LEN, dev->vdev->config_len);
203

    
204
    num_vq = s390_virtio_device_num_vq(dev);
205
    stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_NUM_VQ, num_vq);
206

    
207
    /* Sync virtqueues */
208
    for (i = 0; i < num_vq; i++) {
209
        ram_addr_t vq = (dev->dev_offs + VIRTIO_DEV_OFFS_CONFIG) +
210
                        (i * VIRTIO_VQCONFIG_LEN);
211
        ram_addr_t vring;
212

    
213
        vring = s390_virtio_next_ring(bus);
214
        virtio_queue_set_addr(dev->vdev, i, vring);
215
        virtio_queue_set_vector(dev->vdev, i, i);
216
        stq_phys(vq + VIRTIO_VQCONFIG_OFFS_ADDRESS, vring);
217
        stw_phys(vq + VIRTIO_VQCONFIG_OFFS_NUM, virtio_queue_get_num(dev->vdev, i));
218
    }
219

    
220
    cur_offs = dev->dev_offs;
221
    cur_offs += VIRTIO_DEV_OFFS_CONFIG;
222
    cur_offs += num_vq * VIRTIO_VQCONFIG_LEN;
223

    
224
    /* Sync feature bitmap */
225
    stl_phys(cur_offs, dev->host_features);
226

    
227
    dev->feat_offs = cur_offs + dev->feat_len;
228
    cur_offs += dev->feat_len * 2;
229

    
230
    /* Sync config space */
231
    if (dev->vdev->get_config) {
232
        dev->vdev->get_config(dev->vdev, dev->vdev->config);
233
    }
234

    
235
    cpu_physical_memory_rw(cur_offs, dev->vdev->config, dev->vdev->config_len, 1);
236
    cur_offs += dev->vdev->config_len;
237
}
238

    
239
void s390_virtio_device_update_status(VirtIOS390Device *dev)
240
{
241
    VirtIODevice *vdev = dev->vdev;
242
    uint32_t features;
243

    
244
    virtio_set_status(vdev, ldub_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS));
245

    
246
    /* Update guest supported feature bitmap */
247

    
248
    features = ldl_phys(dev->feat_offs);
249
    if (vdev->set_features) {
250
        vdev->set_features(vdev, features);
251
    }
252
    vdev->guest_features = features;
253
}
254

    
255
VirtIOS390Device *s390_virtio_bus_console(VirtIOS390Bus *bus)
256
{
257
    return bus->console;
258
}
259

    
260
/* Find a device by vring address */
261
VirtIOS390Device *s390_virtio_bus_find_vring(VirtIOS390Bus *bus,
262
                                             ram_addr_t mem,
263
                                             int *vq_num)
264
{
265
    VirtIOS390Device *_dev;
266
    DeviceState *dev;
267
    int i;
268

    
269
    QLIST_FOREACH(dev, &bus->bus.children, sibling) {
270
        _dev = (VirtIOS390Device *)dev;
271
        for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
272
            if (!virtio_queue_get_addr(_dev->vdev, i))
273
                break;
274
            if (virtio_queue_get_addr(_dev->vdev, i) == mem) {
275
                if (vq_num) {
276
                    *vq_num = i;
277
                }
278
                return _dev;
279
            }
280
        }
281
    }
282

    
283
    return NULL;
284
}
285

    
286
/* Find a device by device descriptor location */
287
VirtIOS390Device *s390_virtio_bus_find_mem(VirtIOS390Bus *bus, ram_addr_t mem)
288
{
289
    VirtIOS390Device *_dev;
290
    DeviceState *dev;
291

    
292
    QLIST_FOREACH(dev, &bus->bus.children, sibling) {
293
        _dev = (VirtIOS390Device *)dev;
294
        if (_dev->dev_offs == mem) {
295
            return _dev;
296
        }
297
    }
298

    
299
    return NULL;
300
}
301

    
302
static void virtio_s390_notify(void *opaque, uint16_t vector)
303
{
304
    VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
305
    uint64_t token = s390_virtio_device_vq_token(dev, vector);
306

    
307
    /* XXX kvm dependency! */
308
    kvm_s390_virtio_irq(s390_cpu_addr2state(0), 0, token);
309
}
310

    
311
static unsigned virtio_s390_get_features(void *opaque)
312
{
313
    VirtIOS390Device *dev = (VirtIOS390Device*)opaque;
314
    return dev->host_features;
315
}
316

    
317
/**************** S390 Virtio Bus Device Descriptions *******************/
318

    
319
static const VirtIOBindings virtio_s390_bindings = {
320
    .notify = virtio_s390_notify,
321
    .get_features = virtio_s390_get_features,
322
};
323

    
324
static VirtIOS390DeviceInfo s390_virtio_net = {
325
    .init = s390_virtio_net_init,
326
    .qdev.name = "virtio-net-s390",
327
    .qdev.size = sizeof(VirtIOS390Device),
328
    .qdev.props = (Property[]) {
329
        DEFINE_NIC_PROPERTIES(VirtIOS390Device, nic),
330
        DEFINE_PROP_END_OF_LIST(),
331
    },
332
};
333

    
334
static VirtIOS390DeviceInfo s390_virtio_blk = {
335
    .init = s390_virtio_blk_init,
336
    .qdev.name = "virtio-blk-s390",
337
    .qdev.size = sizeof(VirtIOS390Device),
338
    .qdev.props = (Property[]) {
339
        DEFINE_BLOCK_PROPERTIES(VirtIOS390Device, block),
340
        DEFINE_PROP_END_OF_LIST(),
341
    },
342
};
343

    
344
static VirtIOS390DeviceInfo s390_virtio_serial = {
345
    .init = s390_virtio_serial_init,
346
    .qdev.name = "virtio-serial-s390",
347
    .qdev.alias = "virtio-serial",
348
    .qdev.size = sizeof(VirtIOS390Device),
349
    .qdev.props = (Property[]) {
350
        DEFINE_PROP_UINT32("max_ports", VirtIOS390Device, max_virtserial_ports,
351
                           31),
352
        DEFINE_PROP_END_OF_LIST(),
353
    },
354
};
355

    
356
static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
357
{
358
    VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info;
359
    VirtIOS390Device *_dev = (VirtIOS390Device *)dev;
360

    
361
    return _info->init(_dev);
362
}
363

    
364
static void s390_virtio_bus_register_withprop(VirtIOS390DeviceInfo *info)
365
{
366
    info->qdev.init = s390_virtio_busdev_init;
367
    info->qdev.bus_info = &s390_virtio_bus_info;
368

    
369
    assert(info->qdev.size >= sizeof(VirtIOS390Device));
370
    qdev_register(&info->qdev);
371
}
372

    
373
static void s390_virtio_register(void)
374
{
375
    s390_virtio_bus_register_withprop(&s390_virtio_serial);
376
    s390_virtio_bus_register_withprop(&s390_virtio_blk);
377
    s390_virtio_bus_register_withprop(&s390_virtio_net);
378
}
379
device_init(s390_virtio_register);
380

    
381

    
382
/***************** S390 Virtio Bus Bridge Device *******************/
383
/* Only required to have the virtio bus as child in the system bus */
384

    
385
static int s390_virtio_bridge_init(SysBusDevice *dev)
386
{
387
    /* nothing */
388
    return 0;
389
}
390

    
391
static SysBusDeviceInfo s390_virtio_bridge_info = {
392
    .init = s390_virtio_bridge_init,
393
    .qdev.name  = "s390-virtio-bridge",
394
    .qdev.size  = sizeof(SysBusDevice),
395
    .qdev.no_user = 1,
396
};
397

    
398
static void s390_virtio_register_devices(void)
399
{
400
    sysbus_register_withprop(&s390_virtio_bridge_info);
401
}
402

    
403
device_init(s390_virtio_register_devices)