Statistics
| Branch: | Revision:

root / hw / virtio-pci.c @ e6da7680

History | View | Annotate | Download (15.2 kB)

1
/*
2
 * Virtio PCI Bindings
3
 *
4
 * Copyright IBM, Corp. 2007
5
 * Copyright (c) 2009 CodeSourcery
6
 *
7
 * Authors:
8
 *  Anthony Liguori   <aliguori@us.ibm.com>
9
 *  Paul Brook        <paul@codesourcery.com>
10
 *
11
 * This work is licensed under the terms of the GNU GPL, version 2.  See
12
 * the COPYING file in the top-level directory.
13
 *
14
 */
15

    
16
#include <inttypes.h>
17

    
18
#include "virtio.h"
19
#include "pci.h"
20
//#include "sysemu.h"
21
#include "msix.h"
22

    
23
/* from Linux's linux/virtio_pci.h */
24

    
25
/* A 32-bit r/o bitmask of the features supported by the host */
26
#define VIRTIO_PCI_HOST_FEATURES        0
27

    
28
/* A 32-bit r/w bitmask of features activated by the guest */
29
#define VIRTIO_PCI_GUEST_FEATURES       4
30

    
31
/* A 32-bit r/w PFN for the currently selected queue */
32
#define VIRTIO_PCI_QUEUE_PFN            8
33

    
34
/* A 16-bit r/o queue size for the currently selected queue */
35
#define VIRTIO_PCI_QUEUE_NUM            12
36

    
37
/* A 16-bit r/w queue selector */
38
#define VIRTIO_PCI_QUEUE_SEL            14
39

    
40
/* A 16-bit r/w queue notifier */
41
#define VIRTIO_PCI_QUEUE_NOTIFY         16
42

    
43
/* An 8-bit device status register.  */
44
#define VIRTIO_PCI_STATUS               18
45

    
46
/* An 8-bit r/o interrupt status register.  Reading the value will return the
47
 * current contents of the ISR and will also clear it.  This is effectively
48
 * a read-and-acknowledge. */
49
#define VIRTIO_PCI_ISR                  19
50

    
51
/* MSI-X registers: only enabled if MSI-X is enabled. */
52
/* A 16-bit vector for configuration changes. */
53
#define VIRTIO_MSI_CONFIG_VECTOR        20
54
/* A 16-bit vector for selected queue notifications. */
55
#define VIRTIO_MSI_QUEUE_VECTOR         22
56

    
57
/* Config space size */
58
#define VIRTIO_PCI_CONFIG_NOMSI         20
59
#define VIRTIO_PCI_CONFIG_MSI           24
60
#define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
61
                                         VIRTIO_PCI_CONFIG_MSI : \
62
                                         VIRTIO_PCI_CONFIG_NOMSI)
63

    
64
/* The remaining space is defined by each driver as the per-driver
65
 * configuration space */
66
#define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
67
                                         VIRTIO_PCI_CONFIG_MSI : \
68
                                         VIRTIO_PCI_CONFIG_NOMSI)
69

    
70
/* Virtio ABI version, if we increment this, we break the guest driver. */
71
#define VIRTIO_PCI_ABI_VERSION          0
72

    
73
/* How many bits to shift physical queue address written to QUEUE_PFN.
74
 * 12 is historical, and due to x86 page size. */
75
#define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
76

    
77
/* QEMU doesn't strictly need write barriers since everything runs in
78
 * lock-step.  We'll leave the calls to wmb() in though to make it obvious for
79
 * KVM or if kqemu gets SMP support.
80
 */
81
#define wmb() do { } while (0)
82

    
83
/* PCI bindings.  */
84

    
85
typedef struct {
86
    PCIDevice pci_dev;
87
    VirtIODevice *vdev;
88
    uint32_t addr;
89

    
90
    uint16_t vendor;
91
    uint16_t device;
92
    uint16_t subvendor;
93
    uint16_t class_code;
94
    uint8_t pif;
95
} VirtIOPCIProxy;
96

    
97
/* virtio device */
98

    
99
static void virtio_pci_notify(void *opaque, uint16_t vector)
100
{
101
    VirtIOPCIProxy *proxy = opaque;
102
    if (msix_enabled(&proxy->pci_dev))
103
        msix_notify(&proxy->pci_dev, vector);
104
    else
105
        qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
106
}
107

    
108
static void virtio_pci_save_config(void * opaque, QEMUFile *f)
109
{
110
    VirtIOPCIProxy *proxy = opaque;
111
    pci_device_save(&proxy->pci_dev, f);
112
    msix_save(&proxy->pci_dev, f);
113
    if (msix_present(&proxy->pci_dev))
114
        qemu_put_be16(f, proxy->vdev->config_vector);
115
}
116

    
117
static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
118
{
119
    VirtIOPCIProxy *proxy = opaque;
120
    if (msix_present(&proxy->pci_dev))
121
        qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
122
}
123

    
124
static int virtio_pci_load_config(void * opaque, QEMUFile *f)
125
{
126
    VirtIOPCIProxy *proxy = opaque;
127
    int ret;
128
    ret = pci_device_load(&proxy->pci_dev, f);
129
    if (ret) {
130
        return ret;
131
    }
132
    msix_load(&proxy->pci_dev, f);
133
    if (msix_present(&proxy->pci_dev)) {
134
        qemu_get_be16s(f, &proxy->vdev->config_vector);
135
    } else {
136
        proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
137
    }
138
    if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
139
        return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
140
    }
141
    return 0;
142
}
143

    
144
static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
145
{
146
    VirtIOPCIProxy *proxy = opaque;
147
    uint16_t vector;
148
    if (msix_present(&proxy->pci_dev)) {
149
        qemu_get_be16s(f, &vector);
150
    } else {
151
        vector = VIRTIO_NO_VECTOR;
152
    }
153
    virtio_queue_set_vector(proxy->vdev, n, vector);
154
    if (vector != VIRTIO_NO_VECTOR) {
155
        return msix_vector_use(&proxy->pci_dev, vector);
156
    }
157
    return 0;
158
}
159

    
160
static void virtio_pci_reset(void *opaque)
161
{
162
    VirtIOPCIProxy *proxy = opaque;
163
    virtio_reset(proxy->vdev);
164
    msix_reset(&proxy->pci_dev);
165
}
166

    
167
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
168
{
169
    VirtIOPCIProxy *proxy = opaque;
170
    VirtIODevice *vdev = proxy->vdev;
171
    target_phys_addr_t pa;
172

    
173
    switch (addr) {
174
    case VIRTIO_PCI_GUEST_FEATURES:
175
        /* Guest does not negotiate properly?  We have to assume nothing. */
176
        if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
177
            if (vdev->bad_features)
178
                val = vdev->bad_features(vdev);
179
            else
180
                val = 0;
181
        }
182
        if (vdev->set_features)
183
            vdev->set_features(vdev, val);
184
        vdev->features = val;
185
        break;
186
    case VIRTIO_PCI_QUEUE_PFN:
187
        pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
188
        if (pa == 0)
189
            virtio_pci_reset(proxy);
190
        else
191
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
192
        break;
193
    case VIRTIO_PCI_QUEUE_SEL:
194
        if (val < VIRTIO_PCI_QUEUE_MAX)
195
            vdev->queue_sel = val;
196
        break;
197
    case VIRTIO_PCI_QUEUE_NOTIFY:
198
        virtio_queue_notify(vdev, val);
199
        break;
200
    case VIRTIO_PCI_STATUS:
201
        vdev->status = val & 0xFF;
202
        if (vdev->status == 0)
203
            virtio_pci_reset(proxy);
204
        break;
205
    case VIRTIO_MSI_CONFIG_VECTOR:
206
        msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
207
        /* Make it possible for guest to discover an error took place. */
208
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
209
            val = VIRTIO_NO_VECTOR;
210
        vdev->config_vector = val;
211
        break;
212
    case VIRTIO_MSI_QUEUE_VECTOR:
213
        msix_vector_unuse(&proxy->pci_dev,
214
                          virtio_queue_vector(vdev, vdev->queue_sel));
215
        /* Make it possible for guest to discover an error took place. */
216
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
217
            val = VIRTIO_NO_VECTOR;
218
        virtio_queue_set_vector(vdev, vdev->queue_sel, val);
219
        break;
220
    default:
221
        fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
222
                __func__, addr, val);
223
        break;
224
    }
225
}
226

    
227
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
228
{
229
    VirtIODevice *vdev = proxy->vdev;
230
    uint32_t ret = 0xFFFFFFFF;
231

    
232
    switch (addr) {
233
    case VIRTIO_PCI_HOST_FEATURES:
234
        ret = vdev->get_features(vdev);
235
        ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
236
        ret |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
237
        ret |= (1 << VIRTIO_F_BAD_FEATURE);
238
        break;
239
    case VIRTIO_PCI_GUEST_FEATURES:
240
        ret = vdev->features;
241
        break;
242
    case VIRTIO_PCI_QUEUE_PFN:
243
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
244
              >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
245
        break;
246
    case VIRTIO_PCI_QUEUE_NUM:
247
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
248
        break;
249
    case VIRTIO_PCI_QUEUE_SEL:
250
        ret = vdev->queue_sel;
251
        break;
252
    case VIRTIO_PCI_STATUS:
253
        ret = vdev->status;
254
        break;
255
    case VIRTIO_PCI_ISR:
256
        /* reading from the ISR also clears it. */
257
        ret = vdev->isr;
258
        vdev->isr = 0;
259
        qemu_set_irq(proxy->pci_dev.irq[0], 0);
260
        break;
261
    case VIRTIO_MSI_CONFIG_VECTOR:
262
        ret = vdev->config_vector;
263
        break;
264
    case VIRTIO_MSI_QUEUE_VECTOR:
265
        ret = virtio_queue_vector(vdev, vdev->queue_sel);
266
        break;
267
    default:
268
        break;
269
    }
270

    
271
    return ret;
272
}
273

    
274
static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
275
{
276
    VirtIOPCIProxy *proxy = opaque;
277
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
278
    addr -= proxy->addr;
279
    if (addr < config)
280
        return virtio_ioport_read(proxy, addr);
281
    addr -= config;
282
    return virtio_config_readb(proxy->vdev, addr);
283
}
284

    
285
static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
286
{
287
    VirtIOPCIProxy *proxy = opaque;
288
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
289
    addr -= proxy->addr;
290
    if (addr < config)
291
        return virtio_ioport_read(proxy, addr);
292
    addr -= config;
293
    return virtio_config_readw(proxy->vdev, addr);
294
}
295

    
296
static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
297
{
298
    VirtIOPCIProxy *proxy = opaque;
299
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
300
    addr -= proxy->addr;
301
    if (addr < config)
302
        return virtio_ioport_read(proxy, addr);
303
    addr -= config;
304
    return virtio_config_readl(proxy->vdev, addr);
305
}
306

    
307
static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
308
{
309
    VirtIOPCIProxy *proxy = opaque;
310
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
311
    addr -= proxy->addr;
312
    if (addr < config) {
313
        virtio_ioport_write(proxy, addr, val);
314
        return;
315
    }
316
    addr -= config;
317
    virtio_config_writeb(proxy->vdev, addr, val);
318
}
319

    
320
static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
321
{
322
    VirtIOPCIProxy *proxy = opaque;
323
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
324
    addr -= proxy->addr;
325
    if (addr < config) {
326
        virtio_ioport_write(proxy, addr, val);
327
        return;
328
    }
329
    addr -= config;
330
    virtio_config_writew(proxy->vdev, addr, val);
331
}
332

    
333
static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
334
{
335
    VirtIOPCIProxy *proxy = opaque;
336
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
337
    addr -= proxy->addr;
338
    if (addr < config) {
339
        virtio_ioport_write(proxy, addr, val);
340
        return;
341
    }
342
    addr -= config;
343
    virtio_config_writel(proxy->vdev, addr, val);
344
}
345

    
346
static void virtio_map(PCIDevice *pci_dev, int region_num,
347
                       uint32_t addr, uint32_t size, int type)
348
{
349
    VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
350
    VirtIODevice *vdev = proxy->vdev;
351
    unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
352

    
353
    proxy->addr = addr;
354

    
355
    register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
356
    register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
357
    register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
358
    register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
359
    register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
360
    register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
361

    
362
    if (vdev->config_len)
363
        vdev->get_config(vdev, vdev->config);
364
}
365

    
366
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
367
                                uint32_t val, int len)
368
{
369
    pci_default_write_config(pci_dev, address, val, len);
370
    msix_write_config(pci_dev, address, val, len);
371
}
372

    
373
static const VirtIOBindings virtio_pci_bindings = {
374
    .notify = virtio_pci_notify,
375
    .save_config = virtio_pci_save_config,
376
    .load_config = virtio_pci_load_config,
377
    .save_queue = virtio_pci_save_queue,
378
    .load_queue = virtio_pci_load_queue,
379
};
380

    
381
static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
382
                            uint16_t vendor, uint16_t device,
383
                            uint16_t class_code, uint8_t pif)
384
{
385
    uint8_t *config;
386
    uint32_t size;
387

    
388
    proxy->vdev = vdev;
389

    
390
    config = proxy->pci_dev.config;
391
    pci_config_set_vendor_id(config, vendor);
392
    pci_config_set_device_id(config, device);
393

    
394
    config[0x08] = VIRTIO_PCI_ABI_VERSION;
395

    
396
    config[0x09] = pif;
397
    pci_config_set_class(config, class_code);
398
    config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
399

    
400
    config[0x2c] = vendor & 0xFF;
401
    config[0x2d] = (vendor >> 8) & 0xFF;
402
    config[0x2e] = vdev->device_id & 0xFF;
403
    config[0x2f] = (vdev->device_id >> 8) & 0xFF;
404

    
405
    config[0x3d] = 1;
406

    
407
    if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
408
        pci_register_bar(&proxy->pci_dev, 1,
409
                         msix_bar_size(&proxy->pci_dev),
410
                         PCI_ADDRESS_SPACE_MEM,
411
                         msix_mmio_map);
412
        proxy->pci_dev.config_write = virtio_write_config;
413
        proxy->pci_dev.unregister = msix_uninit;
414
    } else
415
        vdev->nvectors = 0;
416

    
417
    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
418
    if (size & (size-1))
419
        size = 1 << qemu_fls(size);
420

    
421
    pci_register_bar(&proxy->pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
422
                           virtio_map);
423

    
424
    qemu_register_reset(virtio_pci_reset, proxy);
425

    
426
    virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
427
}
428

    
429
static void virtio_blk_init_pci(PCIDevice *pci_dev)
430
{
431
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
432
    VirtIODevice *vdev;
433

    
434
    vdev = virtio_blk_init(&pci_dev->qdev);
435
    virtio_init_pci(proxy, vdev,
436
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
437
                    PCI_DEVICE_ID_VIRTIO_BLOCK,
438
                    PCI_CLASS_STORAGE_OTHER,
439
                    0x00);
440
}
441

    
442
static void virtio_console_init_pci(PCIDevice *pci_dev)
443
{
444
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
445
    VirtIODevice *vdev;
446

    
447
    vdev = virtio_console_init(&pci_dev->qdev);
448
    virtio_init_pci(proxy, vdev,
449
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
450
                    PCI_DEVICE_ID_VIRTIO_CONSOLE,
451
                    PCI_CLASS_DISPLAY_OTHER,
452
                    0x00);
453
}
454

    
455
static void virtio_net_init_pci(PCIDevice *pci_dev)
456
{
457
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
458
    VirtIODevice *vdev;
459

    
460
    vdev = virtio_net_init(&pci_dev->qdev);
461
    virtio_init_pci(proxy, vdev,
462
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
463
                    PCI_DEVICE_ID_VIRTIO_NET,
464
                    PCI_CLASS_NETWORK_ETHERNET,
465
                    0x00);
466
}
467

    
468
static void virtio_balloon_init_pci(PCIDevice *pci_dev)
469
{
470
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
471
    VirtIODevice *vdev;
472

    
473
    vdev = virtio_balloon_init(&pci_dev->qdev);
474
    virtio_init_pci(proxy, vdev,
475
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
476
                    PCI_DEVICE_ID_VIRTIO_BALLOON,
477
                    PCI_CLASS_MEMORY_RAM,
478
                    0x00);
479
}
480

    
481
static PCIDeviceInfo virtio_info[] = {
482
    {
483
        .qdev.name = "virtio-blk-pci",
484
        .qdev.size = sizeof(VirtIOPCIProxy),
485
        .init      = virtio_blk_init_pci,
486
    },{
487
        .qdev.name = "virtio-net-pci",
488
        .qdev.size = sizeof(VirtIOPCIProxy),
489
        .init      = virtio_net_init_pci,
490
    },{
491
        .qdev.name = "virtio-console-pci",
492
        .qdev.size = sizeof(VirtIOPCIProxy),
493
        .init      = virtio_console_init_pci,
494
    },{
495
        .qdev.name = "virtio-balloon-pci",
496
        .qdev.size = sizeof(VirtIOPCIProxy),
497
        .init      = virtio_balloon_init_pci,
498
    },{
499
        /* end of list */
500
    }
501
};
502

    
503
static void virtio_pci_register_devices(void)
504
{
505
    pci_qdev_register_many(virtio_info);
506
}
507

    
508
device_init(virtio_pci_register_devices)