Statistics
| Branch: | Revision:

root / hw / virtio-pci.c @ 96cc1810

History | View | Annotate | Download (16.1 kB)

1 53c25cea Paul Brook
/*
2 53c25cea Paul Brook
 * Virtio PCI Bindings
3 53c25cea Paul Brook
 *
4 53c25cea Paul Brook
 * Copyright IBM, Corp. 2007
5 53c25cea Paul Brook
 * Copyright (c) 2009 CodeSourcery
6 53c25cea Paul Brook
 *
7 53c25cea Paul Brook
 * Authors:
8 53c25cea Paul Brook
 *  Anthony Liguori   <aliguori@us.ibm.com>
9 53c25cea Paul Brook
 *  Paul Brook        <paul@codesourcery.com>
10 53c25cea Paul Brook
 *
11 53c25cea Paul Brook
 * This work is licensed under the terms of the GNU GPL, version 2.  See
12 53c25cea Paul Brook
 * the COPYING file in the top-level directory.
13 53c25cea Paul Brook
 *
14 53c25cea Paul Brook
 */
15 53c25cea Paul Brook
16 53c25cea Paul Brook
#include <inttypes.h>
17 53c25cea Paul Brook
18 53c25cea Paul Brook
#include "virtio.h"
19 53c25cea Paul Brook
#include "pci.h"
20 1ad2134f Paul Brook
//#include "sysemu.h"
21 aba800a3 Michael S. Tsirkin
#include "msix.h"
22 53c25cea Paul Brook
23 53c25cea Paul Brook
/* from Linux's linux/virtio_pci.h */
24 53c25cea Paul Brook
25 53c25cea Paul Brook
/* A 32-bit r/o bitmask of the features supported by the host */
26 53c25cea Paul Brook
#define VIRTIO_PCI_HOST_FEATURES        0
27 53c25cea Paul Brook
28 53c25cea Paul Brook
/* A 32-bit r/w bitmask of features activated by the guest */
29 53c25cea Paul Brook
#define VIRTIO_PCI_GUEST_FEATURES       4
30 53c25cea Paul Brook
31 53c25cea Paul Brook
/* A 32-bit r/w PFN for the currently selected queue */
32 53c25cea Paul Brook
#define VIRTIO_PCI_QUEUE_PFN            8
33 53c25cea Paul Brook
34 53c25cea Paul Brook
/* A 16-bit r/o queue size for the currently selected queue */
35 53c25cea Paul Brook
#define VIRTIO_PCI_QUEUE_NUM            12
36 53c25cea Paul Brook
37 53c25cea Paul Brook
/* A 16-bit r/w queue selector */
38 53c25cea Paul Brook
#define VIRTIO_PCI_QUEUE_SEL            14
39 53c25cea Paul Brook
40 53c25cea Paul Brook
/* A 16-bit r/w queue notifier */
41 53c25cea Paul Brook
#define VIRTIO_PCI_QUEUE_NOTIFY         16
42 53c25cea Paul Brook
43 53c25cea Paul Brook
/* An 8-bit device status register.  */
44 53c25cea Paul Brook
#define VIRTIO_PCI_STATUS               18
45 53c25cea Paul Brook
46 53c25cea Paul Brook
/* An 8-bit r/o interrupt status register.  Reading the value will return the
47 53c25cea Paul Brook
 * current contents of the ISR and will also clear it.  This is effectively
48 53c25cea Paul Brook
 * a read-and-acknowledge. */
49 53c25cea Paul Brook
#define VIRTIO_PCI_ISR                  19
50 53c25cea Paul Brook
51 aba800a3 Michael S. Tsirkin
/* MSI-X registers: only enabled if MSI-X is enabled. */
52 aba800a3 Michael S. Tsirkin
/* A 16-bit vector for configuration changes. */
53 aba800a3 Michael S. Tsirkin
#define VIRTIO_MSI_CONFIG_VECTOR        20
54 aba800a3 Michael S. Tsirkin
/* A 16-bit vector for selected queue notifications. */
55 aba800a3 Michael S. Tsirkin
#define VIRTIO_MSI_QUEUE_VECTOR         22
56 aba800a3 Michael S. Tsirkin
57 aba800a3 Michael S. Tsirkin
/* Config space size */
58 aba800a3 Michael S. Tsirkin
#define VIRTIO_PCI_CONFIG_NOMSI         20
59 aba800a3 Michael S. Tsirkin
#define VIRTIO_PCI_CONFIG_MSI           24
60 aba800a3 Michael S. Tsirkin
#define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
61 aba800a3 Michael S. Tsirkin
                                         VIRTIO_PCI_CONFIG_MSI : \
62 aba800a3 Michael S. Tsirkin
                                         VIRTIO_PCI_CONFIG_NOMSI)
63 aba800a3 Michael S. Tsirkin
64 aba800a3 Michael S. Tsirkin
/* The remaining space is defined by each driver as the per-driver
65 aba800a3 Michael S. Tsirkin
 * configuration space */
66 aba800a3 Michael S. Tsirkin
#define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
67 aba800a3 Michael S. Tsirkin
                                         VIRTIO_PCI_CONFIG_MSI : \
68 aba800a3 Michael S. Tsirkin
                                         VIRTIO_PCI_CONFIG_NOMSI)
69 53c25cea Paul Brook
70 53c25cea Paul Brook
/* Virtio ABI version, if we increment this, we break the guest driver. */
71 53c25cea Paul Brook
#define VIRTIO_PCI_ABI_VERSION          0
72 53c25cea Paul Brook
73 53c25cea Paul Brook
/* How many bits to shift physical queue address written to QUEUE_PFN.
74 53c25cea Paul Brook
 * 12 is historical, and due to x86 page size. */
75 53c25cea Paul Brook
#define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
76 53c25cea Paul Brook
77 53c25cea Paul Brook
/* QEMU doesn't strictly need write barriers since everything runs in
78 53c25cea Paul Brook
 * lock-step.  We'll leave the calls to wmb() in though to make it obvious for
79 53c25cea Paul Brook
 * KVM or if kqemu gets SMP support.
80 53c25cea Paul Brook
 */
81 53c25cea Paul Brook
#define wmb() do { } while (0)
82 53c25cea Paul Brook
83 53c25cea Paul Brook
/* PCI bindings.  */
84 53c25cea Paul Brook
85 53c25cea Paul Brook
typedef struct {
86 53c25cea Paul Brook
    PCIDevice pci_dev;
87 53c25cea Paul Brook
    VirtIODevice *vdev;
88 53c25cea Paul Brook
    uint32_t addr;
89 53c25cea Paul Brook
} VirtIOPCIProxy;
90 53c25cea Paul Brook
91 53c25cea Paul Brook
/* virtio device */
92 53c25cea Paul Brook
93 7055e687 Michael S. Tsirkin
static void virtio_pci_notify(void *opaque, uint16_t vector)
94 53c25cea Paul Brook
{
95 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
96 aba800a3 Michael S. Tsirkin
    if (msix_enabled(&proxy->pci_dev))
97 aba800a3 Michael S. Tsirkin
        msix_notify(&proxy->pci_dev, vector);
98 aba800a3 Michael S. Tsirkin
    else
99 aba800a3 Michael S. Tsirkin
        qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
100 53c25cea Paul Brook
}
101 53c25cea Paul Brook
102 ff24bd58 Michael S. Tsirkin
static void virtio_pci_save_config(void * opaque, QEMUFile *f)
103 ff24bd58 Michael S. Tsirkin
{
104 ff24bd58 Michael S. Tsirkin
    VirtIOPCIProxy *proxy = opaque;
105 ff24bd58 Michael S. Tsirkin
    pci_device_save(&proxy->pci_dev, f);
106 ff24bd58 Michael S. Tsirkin
    msix_save(&proxy->pci_dev, f);
107 ff24bd58 Michael S. Tsirkin
    if (msix_present(&proxy->pci_dev))
108 ff24bd58 Michael S. Tsirkin
        qemu_put_be16(f, proxy->vdev->config_vector);
109 ff24bd58 Michael S. Tsirkin
}
110 ff24bd58 Michael S. Tsirkin
111 ff24bd58 Michael S. Tsirkin
static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
112 ff24bd58 Michael S. Tsirkin
{
113 ff24bd58 Michael S. Tsirkin
    VirtIOPCIProxy *proxy = opaque;
114 ff24bd58 Michael S. Tsirkin
    if (msix_present(&proxy->pci_dev))
115 ff24bd58 Michael S. Tsirkin
        qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
116 ff24bd58 Michael S. Tsirkin
}
117 ff24bd58 Michael S. Tsirkin
118 ff24bd58 Michael S. Tsirkin
static int virtio_pci_load_config(void * opaque, QEMUFile *f)
119 ff24bd58 Michael S. Tsirkin
{
120 ff24bd58 Michael S. Tsirkin
    VirtIOPCIProxy *proxy = opaque;
121 ff24bd58 Michael S. Tsirkin
    int ret;
122 ff24bd58 Michael S. Tsirkin
    ret = pci_device_load(&proxy->pci_dev, f);
123 e6da7680 Michael S. Tsirkin
    if (ret) {
124 ff24bd58 Michael S. Tsirkin
        return ret;
125 e6da7680 Michael S. Tsirkin
    }
126 ff24bd58 Michael S. Tsirkin
    msix_load(&proxy->pci_dev, f);
127 e6da7680 Michael S. Tsirkin
    if (msix_present(&proxy->pci_dev)) {
128 ff24bd58 Michael S. Tsirkin
        qemu_get_be16s(f, &proxy->vdev->config_vector);
129 e6da7680 Michael S. Tsirkin
    } else {
130 e6da7680 Michael S. Tsirkin
        proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
131 e6da7680 Michael S. Tsirkin
    }
132 e6da7680 Michael S. Tsirkin
    if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
133 e6da7680 Michael S. Tsirkin
        return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
134 e6da7680 Michael S. Tsirkin
    }
135 ff24bd58 Michael S. Tsirkin
    return 0;
136 ff24bd58 Michael S. Tsirkin
}
137 ff24bd58 Michael S. Tsirkin
138 ff24bd58 Michael S. Tsirkin
static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
139 ff24bd58 Michael S. Tsirkin
{
140 ff24bd58 Michael S. Tsirkin
    VirtIOPCIProxy *proxy = opaque;
141 ff24bd58 Michael S. Tsirkin
    uint16_t vector;
142 e6da7680 Michael S. Tsirkin
    if (msix_present(&proxy->pci_dev)) {
143 e6da7680 Michael S. Tsirkin
        qemu_get_be16s(f, &vector);
144 e6da7680 Michael S. Tsirkin
    } else {
145 e6da7680 Michael S. Tsirkin
        vector = VIRTIO_NO_VECTOR;
146 e6da7680 Michael S. Tsirkin
    }
147 ff24bd58 Michael S. Tsirkin
    virtio_queue_set_vector(proxy->vdev, n, vector);
148 e6da7680 Michael S. Tsirkin
    if (vector != VIRTIO_NO_VECTOR) {
149 e6da7680 Michael S. Tsirkin
        return msix_vector_use(&proxy->pci_dev, vector);
150 e6da7680 Michael S. Tsirkin
    }
151 ff24bd58 Michael S. Tsirkin
    return 0;
152 ff24bd58 Michael S. Tsirkin
}
153 ff24bd58 Michael S. Tsirkin
154 7055e687 Michael S. Tsirkin
static void virtio_pci_reset(void *opaque)
155 7055e687 Michael S. Tsirkin
{
156 7055e687 Michael S. Tsirkin
    VirtIOPCIProxy *proxy = opaque;
157 7055e687 Michael S. Tsirkin
    virtio_reset(proxy->vdev);
158 aba800a3 Michael S. Tsirkin
    msix_reset(&proxy->pci_dev);
159 7055e687 Michael S. Tsirkin
}
160 7055e687 Michael S. Tsirkin
161 53c25cea Paul Brook
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
162 53c25cea Paul Brook
{
163 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
164 53c25cea Paul Brook
    VirtIODevice *vdev = proxy->vdev;
165 53c25cea Paul Brook
    target_phys_addr_t pa;
166 53c25cea Paul Brook
167 53c25cea Paul Brook
    switch (addr) {
168 53c25cea Paul Brook
    case VIRTIO_PCI_GUEST_FEATURES:
169 53c25cea Paul Brook
        /* Guest does not negotiate properly?  We have to assume nothing. */
170 53c25cea Paul Brook
        if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
171 53c25cea Paul Brook
            if (vdev->bad_features)
172 53c25cea Paul Brook
                val = vdev->bad_features(vdev);
173 53c25cea Paul Brook
            else
174 53c25cea Paul Brook
                val = 0;
175 53c25cea Paul Brook
        }
176 53c25cea Paul Brook
        if (vdev->set_features)
177 53c25cea Paul Brook
            vdev->set_features(vdev, val);
178 53c25cea Paul Brook
        vdev->features = val;
179 53c25cea Paul Brook
        break;
180 53c25cea Paul Brook
    case VIRTIO_PCI_QUEUE_PFN:
181 53c25cea Paul Brook
        pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
182 7055e687 Michael S. Tsirkin
        if (pa == 0)
183 7055e687 Michael S. Tsirkin
            virtio_pci_reset(proxy);
184 7055e687 Michael S. Tsirkin
        else
185 7055e687 Michael S. Tsirkin
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
186 53c25cea Paul Brook
        break;
187 53c25cea Paul Brook
    case VIRTIO_PCI_QUEUE_SEL:
188 53c25cea Paul Brook
        if (val < VIRTIO_PCI_QUEUE_MAX)
189 53c25cea Paul Brook
            vdev->queue_sel = val;
190 53c25cea Paul Brook
        break;
191 53c25cea Paul Brook
    case VIRTIO_PCI_QUEUE_NOTIFY:
192 53c25cea Paul Brook
        virtio_queue_notify(vdev, val);
193 53c25cea Paul Brook
        break;
194 53c25cea Paul Brook
    case VIRTIO_PCI_STATUS:
195 53c25cea Paul Brook
        vdev->status = val & 0xFF;
196 53c25cea Paul Brook
        if (vdev->status == 0)
197 7055e687 Michael S. Tsirkin
            virtio_pci_reset(proxy);
198 53c25cea Paul Brook
        break;
199 aba800a3 Michael S. Tsirkin
    case VIRTIO_MSI_CONFIG_VECTOR:
200 aba800a3 Michael S. Tsirkin
        msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
201 aba800a3 Michael S. Tsirkin
        /* Make it possible for guest to discover an error took place. */
202 aba800a3 Michael S. Tsirkin
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
203 aba800a3 Michael S. Tsirkin
            val = VIRTIO_NO_VECTOR;
204 aba800a3 Michael S. Tsirkin
        vdev->config_vector = val;
205 aba800a3 Michael S. Tsirkin
        break;
206 aba800a3 Michael S. Tsirkin
    case VIRTIO_MSI_QUEUE_VECTOR:
207 aba800a3 Michael S. Tsirkin
        msix_vector_unuse(&proxy->pci_dev,
208 aba800a3 Michael S. Tsirkin
                          virtio_queue_vector(vdev, vdev->queue_sel));
209 aba800a3 Michael S. Tsirkin
        /* Make it possible for guest to discover an error took place. */
210 aba800a3 Michael S. Tsirkin
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
211 aba800a3 Michael S. Tsirkin
            val = VIRTIO_NO_VECTOR;
212 aba800a3 Michael S. Tsirkin
        virtio_queue_set_vector(vdev, vdev->queue_sel, val);
213 aba800a3 Michael S. Tsirkin
        break;
214 aba800a3 Michael S. Tsirkin
    default:
215 aba800a3 Michael S. Tsirkin
        fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
216 aba800a3 Michael S. Tsirkin
                __func__, addr, val);
217 aba800a3 Michael S. Tsirkin
        break;
218 53c25cea Paul Brook
    }
219 53c25cea Paul Brook
}
220 53c25cea Paul Brook
221 aba800a3 Michael S. Tsirkin
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
222 53c25cea Paul Brook
{
223 53c25cea Paul Brook
    VirtIODevice *vdev = proxy->vdev;
224 53c25cea Paul Brook
    uint32_t ret = 0xFFFFFFFF;
225 53c25cea Paul Brook
226 53c25cea Paul Brook
    switch (addr) {
227 53c25cea Paul Brook
    case VIRTIO_PCI_HOST_FEATURES:
228 53c25cea Paul Brook
        ret = vdev->get_features(vdev);
229 efeea6d0 Mark McLoughlin
        ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
230 efeea6d0 Mark McLoughlin
        ret |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
231 efeea6d0 Mark McLoughlin
        ret |= (1 << VIRTIO_F_BAD_FEATURE);
232 53c25cea Paul Brook
        break;
233 53c25cea Paul Brook
    case VIRTIO_PCI_GUEST_FEATURES:
234 53c25cea Paul Brook
        ret = vdev->features;
235 53c25cea Paul Brook
        break;
236 53c25cea Paul Brook
    case VIRTIO_PCI_QUEUE_PFN:
237 53c25cea Paul Brook
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
238 53c25cea Paul Brook
              >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
239 53c25cea Paul Brook
        break;
240 53c25cea Paul Brook
    case VIRTIO_PCI_QUEUE_NUM:
241 53c25cea Paul Brook
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
242 53c25cea Paul Brook
        break;
243 53c25cea Paul Brook
    case VIRTIO_PCI_QUEUE_SEL:
244 53c25cea Paul Brook
        ret = vdev->queue_sel;
245 53c25cea Paul Brook
        break;
246 53c25cea Paul Brook
    case VIRTIO_PCI_STATUS:
247 53c25cea Paul Brook
        ret = vdev->status;
248 53c25cea Paul Brook
        break;
249 53c25cea Paul Brook
    case VIRTIO_PCI_ISR:
250 53c25cea Paul Brook
        /* reading from the ISR also clears it. */
251 53c25cea Paul Brook
        ret = vdev->isr;
252 53c25cea Paul Brook
        vdev->isr = 0;
253 7055e687 Michael S. Tsirkin
        qemu_set_irq(proxy->pci_dev.irq[0], 0);
254 53c25cea Paul Brook
        break;
255 aba800a3 Michael S. Tsirkin
    case VIRTIO_MSI_CONFIG_VECTOR:
256 aba800a3 Michael S. Tsirkin
        ret = vdev->config_vector;
257 aba800a3 Michael S. Tsirkin
        break;
258 aba800a3 Michael S. Tsirkin
    case VIRTIO_MSI_QUEUE_VECTOR:
259 aba800a3 Michael S. Tsirkin
        ret = virtio_queue_vector(vdev, vdev->queue_sel);
260 aba800a3 Michael S. Tsirkin
        break;
261 53c25cea Paul Brook
    default:
262 53c25cea Paul Brook
        break;
263 53c25cea Paul Brook
    }
264 53c25cea Paul Brook
265 53c25cea Paul Brook
    return ret;
266 53c25cea Paul Brook
}
267 53c25cea Paul Brook
268 53c25cea Paul Brook
static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
269 53c25cea Paul Brook
{
270 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
271 aba800a3 Michael S. Tsirkin
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
272 aba800a3 Michael S. Tsirkin
    addr -= proxy->addr;
273 aba800a3 Michael S. Tsirkin
    if (addr < config)
274 aba800a3 Michael S. Tsirkin
        return virtio_ioport_read(proxy, addr);
275 aba800a3 Michael S. Tsirkin
    addr -= config;
276 53c25cea Paul Brook
    return virtio_config_readb(proxy->vdev, addr);
277 53c25cea Paul Brook
}
278 53c25cea Paul Brook
279 53c25cea Paul Brook
static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
280 53c25cea Paul Brook
{
281 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
282 aba800a3 Michael S. Tsirkin
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
283 aba800a3 Michael S. Tsirkin
    addr -= proxy->addr;
284 aba800a3 Michael S. Tsirkin
    if (addr < config)
285 aba800a3 Michael S. Tsirkin
        return virtio_ioport_read(proxy, addr);
286 aba800a3 Michael S. Tsirkin
    addr -= config;
287 53c25cea Paul Brook
    return virtio_config_readw(proxy->vdev, addr);
288 53c25cea Paul Brook
}
289 53c25cea Paul Brook
290 53c25cea Paul Brook
static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
291 53c25cea Paul Brook
{
292 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
293 aba800a3 Michael S. Tsirkin
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
294 aba800a3 Michael S. Tsirkin
    addr -= proxy->addr;
295 aba800a3 Michael S. Tsirkin
    if (addr < config)
296 aba800a3 Michael S. Tsirkin
        return virtio_ioport_read(proxy, addr);
297 aba800a3 Michael S. Tsirkin
    addr -= config;
298 53c25cea Paul Brook
    return virtio_config_readl(proxy->vdev, addr);
299 53c25cea Paul Brook
}
300 53c25cea Paul Brook
301 53c25cea Paul Brook
static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
302 53c25cea Paul Brook
{
303 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
304 aba800a3 Michael S. Tsirkin
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
305 aba800a3 Michael S. Tsirkin
    addr -= proxy->addr;
306 aba800a3 Michael S. Tsirkin
    if (addr < config) {
307 aba800a3 Michael S. Tsirkin
        virtio_ioport_write(proxy, addr, val);
308 aba800a3 Michael S. Tsirkin
        return;
309 aba800a3 Michael S. Tsirkin
    }
310 aba800a3 Michael S. Tsirkin
    addr -= config;
311 53c25cea Paul Brook
    virtio_config_writeb(proxy->vdev, addr, val);
312 53c25cea Paul Brook
}
313 53c25cea Paul Brook
314 53c25cea Paul Brook
static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
315 53c25cea Paul Brook
{
316 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
317 aba800a3 Michael S. Tsirkin
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
318 aba800a3 Michael S. Tsirkin
    addr -= proxy->addr;
319 aba800a3 Michael S. Tsirkin
    if (addr < config) {
320 aba800a3 Michael S. Tsirkin
        virtio_ioport_write(proxy, addr, val);
321 aba800a3 Michael S. Tsirkin
        return;
322 aba800a3 Michael S. Tsirkin
    }
323 aba800a3 Michael S. Tsirkin
    addr -= config;
324 53c25cea Paul Brook
    virtio_config_writew(proxy->vdev, addr, val);
325 53c25cea Paul Brook
}
326 53c25cea Paul Brook
327 53c25cea Paul Brook
static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
328 53c25cea Paul Brook
{
329 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = opaque;
330 aba800a3 Michael S. Tsirkin
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
331 aba800a3 Michael S. Tsirkin
    addr -= proxy->addr;
332 aba800a3 Michael S. Tsirkin
    if (addr < config) {
333 aba800a3 Michael S. Tsirkin
        virtio_ioport_write(proxy, addr, val);
334 aba800a3 Michael S. Tsirkin
        return;
335 aba800a3 Michael S. Tsirkin
    }
336 aba800a3 Michael S. Tsirkin
    addr -= config;
337 53c25cea Paul Brook
    virtio_config_writel(proxy->vdev, addr, val);
338 53c25cea Paul Brook
}
339 53c25cea Paul Brook
340 53c25cea Paul Brook
static void virtio_map(PCIDevice *pci_dev, int region_num,
341 53c25cea Paul Brook
                       uint32_t addr, uint32_t size, int type)
342 53c25cea Paul Brook
{
343 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
344 53c25cea Paul Brook
    VirtIODevice *vdev = proxy->vdev;
345 aba800a3 Michael S. Tsirkin
    unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
346 53c25cea Paul Brook
347 53c25cea Paul Brook
    proxy->addr = addr;
348 53c25cea Paul Brook
349 aba800a3 Michael S. Tsirkin
    register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
350 aba800a3 Michael S. Tsirkin
    register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
351 aba800a3 Michael S. Tsirkin
    register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
352 aba800a3 Michael S. Tsirkin
    register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
353 aba800a3 Michael S. Tsirkin
    register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
354 aba800a3 Michael S. Tsirkin
    register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
355 53c25cea Paul Brook
356 aba800a3 Michael S. Tsirkin
    if (vdev->config_len)
357 53c25cea Paul Brook
        vdev->get_config(vdev, vdev->config);
358 aba800a3 Michael S. Tsirkin
}
359 aba800a3 Michael S. Tsirkin
360 aba800a3 Michael S. Tsirkin
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
361 aba800a3 Michael S. Tsirkin
                                uint32_t val, int len)
362 aba800a3 Michael S. Tsirkin
{
363 aba800a3 Michael S. Tsirkin
    pci_default_write_config(pci_dev, address, val, len);
364 aba800a3 Michael S. Tsirkin
    msix_write_config(pci_dev, address, val, len);
365 53c25cea Paul Brook
}
366 53c25cea Paul Brook
367 53c25cea Paul Brook
static const VirtIOBindings virtio_pci_bindings = {
368 ff24bd58 Michael S. Tsirkin
    .notify = virtio_pci_notify,
369 ff24bd58 Michael S. Tsirkin
    .save_config = virtio_pci_save_config,
370 ff24bd58 Michael S. Tsirkin
    .load_config = virtio_pci_load_config,
371 ff24bd58 Michael S. Tsirkin
    .save_queue = virtio_pci_save_queue,
372 ff24bd58 Michael S. Tsirkin
    .load_queue = virtio_pci_load_queue,
373 53c25cea Paul Brook
};
374 53c25cea Paul Brook
375 53c25cea Paul Brook
static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
376 53c25cea Paul Brook
                            uint16_t vendor, uint16_t device,
377 53c25cea Paul Brook
                            uint16_t class_code, uint8_t pif)
378 53c25cea Paul Brook
{
379 53c25cea Paul Brook
    uint8_t *config;
380 53c25cea Paul Brook
    uint32_t size;
381 53c25cea Paul Brook
382 53c25cea Paul Brook
    proxy->vdev = vdev;
383 53c25cea Paul Brook
384 53c25cea Paul Brook
    config = proxy->pci_dev.config;
385 53c25cea Paul Brook
    pci_config_set_vendor_id(config, vendor);
386 53c25cea Paul Brook
    pci_config_set_device_id(config, device);
387 53c25cea Paul Brook
388 53c25cea Paul Brook
    config[0x08] = VIRTIO_PCI_ABI_VERSION;
389 53c25cea Paul Brook
390 53c25cea Paul Brook
    config[0x09] = pif;
391 53c25cea Paul Brook
    pci_config_set_class(config, class_code);
392 53c25cea Paul Brook
    config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
393 53c25cea Paul Brook
394 53c25cea Paul Brook
    config[0x2c] = vendor & 0xFF;
395 53c25cea Paul Brook
    config[0x2d] = (vendor >> 8) & 0xFF;
396 53c25cea Paul Brook
    config[0x2e] = vdev->device_id & 0xFF;
397 53c25cea Paul Brook
    config[0x2f] = (vdev->device_id >> 8) & 0xFF;
398 53c25cea Paul Brook
399 53c25cea Paul Brook
    config[0x3d] = 1;
400 53c25cea Paul Brook
401 aba800a3 Michael S. Tsirkin
    if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
402 aba800a3 Michael S. Tsirkin
        pci_register_bar(&proxy->pci_dev, 1,
403 aba800a3 Michael S. Tsirkin
                         msix_bar_size(&proxy->pci_dev),
404 aba800a3 Michael S. Tsirkin
                         PCI_ADDRESS_SPACE_MEM,
405 aba800a3 Michael S. Tsirkin
                         msix_mmio_map);
406 aba800a3 Michael S. Tsirkin
        proxy->pci_dev.config_write = virtio_write_config;
407 aba800a3 Michael S. Tsirkin
        proxy->pci_dev.unregister = msix_uninit;
408 aba800a3 Michael S. Tsirkin
    } else
409 aba800a3 Michael S. Tsirkin
        vdev->nvectors = 0;
410 aba800a3 Michael S. Tsirkin
411 aba800a3 Michael S. Tsirkin
    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
412 53c25cea Paul Brook
    if (size & (size-1))
413 53c25cea Paul Brook
        size = 1 << qemu_fls(size);
414 53c25cea Paul Brook
415 28c2c264 Avi Kivity
    pci_register_bar(&proxy->pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
416 53c25cea Paul Brook
                           virtio_map);
417 53c25cea Paul Brook
418 a08d4367 Jan Kiszka
    qemu_register_reset(virtio_pci_reset, proxy);
419 7055e687 Michael S. Tsirkin
420 53c25cea Paul Brook
    virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
421 53c25cea Paul Brook
}
422 53c25cea Paul Brook
423 5c634ef3 Mark McLoughlin
static void virtio_blk_init_pci_with_class(PCIDevice *pci_dev,
424 5c634ef3 Mark McLoughlin
                                           uint16_t class_code)
425 53c25cea Paul Brook
{
426 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
427 53c25cea Paul Brook
    VirtIODevice *vdev;
428 53c25cea Paul Brook
429 53c25cea Paul Brook
    vdev = virtio_blk_init(&pci_dev->qdev);
430 53c25cea Paul Brook
    virtio_init_pci(proxy, vdev,
431 53c25cea Paul Brook
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
432 53c25cea Paul Brook
                    PCI_DEVICE_ID_VIRTIO_BLOCK,
433 5c634ef3 Mark McLoughlin
                    class_code, 0x00);
434 5c634ef3 Mark McLoughlin
}
435 5c634ef3 Mark McLoughlin
436 5c634ef3 Mark McLoughlin
static void virtio_blk_init_pci(PCIDevice *pci_dev)
437 5c634ef3 Mark McLoughlin
{
438 5c634ef3 Mark McLoughlin
    virtio_blk_init_pci_with_class(pci_dev, PCI_CLASS_STORAGE_SCSI);
439 5c634ef3 Mark McLoughlin
}
440 5c634ef3 Mark McLoughlin
441 5c634ef3 Mark McLoughlin
static void virtio_blk_init_pci_0_10(PCIDevice *pci_dev)
442 5c634ef3 Mark McLoughlin
{
443 5c634ef3 Mark McLoughlin
    virtio_blk_init_pci_with_class(pci_dev, PCI_CLASS_STORAGE_OTHER);
444 53c25cea Paul Brook
}
445 53c25cea Paul Brook
446 21d58b57 Mark McLoughlin
static void virtio_console_init_pci_with_class(PCIDevice *pci_dev,
447 21d58b57 Mark McLoughlin
                                               uint16_t class_code)
448 53c25cea Paul Brook
{
449 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
450 53c25cea Paul Brook
    VirtIODevice *vdev;
451 53c25cea Paul Brook
452 53c25cea Paul Brook
    vdev = virtio_console_init(&pci_dev->qdev);
453 53c25cea Paul Brook
    virtio_init_pci(proxy, vdev,
454 53c25cea Paul Brook
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
455 53c25cea Paul Brook
                    PCI_DEVICE_ID_VIRTIO_CONSOLE,
456 21d58b57 Mark McLoughlin
                    class_code, 0x00);
457 21d58b57 Mark McLoughlin
}
458 21d58b57 Mark McLoughlin
459 21d58b57 Mark McLoughlin
static void virtio_console_init_pci(PCIDevice *pci_dev)
460 21d58b57 Mark McLoughlin
{
461 21d58b57 Mark McLoughlin
    virtio_console_init_pci_with_class(pci_dev, PCI_CLASS_SERIAL_OTHER);
462 21d58b57 Mark McLoughlin
}
463 21d58b57 Mark McLoughlin
464 21d58b57 Mark McLoughlin
static void virtio_console_init_pci_0_10(PCIDevice *pci_dev)
465 21d58b57 Mark McLoughlin
{
466 21d58b57 Mark McLoughlin
    virtio_console_init_pci_with_class(pci_dev, PCI_CLASS_DISPLAY_OTHER);
467 53c25cea Paul Brook
}
468 53c25cea Paul Brook
469 53c25cea Paul Brook
static void virtio_net_init_pci(PCIDevice *pci_dev)
470 53c25cea Paul Brook
{
471 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
472 53c25cea Paul Brook
    VirtIODevice *vdev;
473 53c25cea Paul Brook
474 53c25cea Paul Brook
    vdev = virtio_net_init(&pci_dev->qdev);
475 53c25cea Paul Brook
    virtio_init_pci(proxy, vdev,
476 53c25cea Paul Brook
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
477 53c25cea Paul Brook
                    PCI_DEVICE_ID_VIRTIO_NET,
478 53c25cea Paul Brook
                    PCI_CLASS_NETWORK_ETHERNET,
479 53c25cea Paul Brook
                    0x00);
480 53c25cea Paul Brook
}
481 53c25cea Paul Brook
482 53c25cea Paul Brook
static void virtio_balloon_init_pci(PCIDevice *pci_dev)
483 53c25cea Paul Brook
{
484 53c25cea Paul Brook
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
485 53c25cea Paul Brook
    VirtIODevice *vdev;
486 53c25cea Paul Brook
487 53c25cea Paul Brook
    vdev = virtio_balloon_init(&pci_dev->qdev);
488 53c25cea Paul Brook
    virtio_init_pci(proxy, vdev,
489 53c25cea Paul Brook
                    PCI_VENDOR_ID_REDHAT_QUMRANET,
490 53c25cea Paul Brook
                    PCI_DEVICE_ID_VIRTIO_BALLOON,
491 53c25cea Paul Brook
                    PCI_CLASS_MEMORY_RAM,
492 53c25cea Paul Brook
                    0x00);
493 53c25cea Paul Brook
}
494 53c25cea Paul Brook
495 0aab0d3a Gerd Hoffmann
static PCIDeviceInfo virtio_info[] = {
496 0aab0d3a Gerd Hoffmann
    {
497 0aab0d3a Gerd Hoffmann
        .qdev.name = "virtio-blk-pci",
498 0aab0d3a Gerd Hoffmann
        .qdev.size = sizeof(VirtIOPCIProxy),
499 0aab0d3a Gerd Hoffmann
        .init      = virtio_blk_init_pci,
500 0aab0d3a Gerd Hoffmann
    },{
501 0aab0d3a Gerd Hoffmann
        .qdev.name = "virtio-net-pci",
502 0aab0d3a Gerd Hoffmann
        .qdev.size = sizeof(VirtIOPCIProxy),
503 0aab0d3a Gerd Hoffmann
        .init      = virtio_net_init_pci,
504 0aab0d3a Gerd Hoffmann
    },{
505 0aab0d3a Gerd Hoffmann
        .qdev.name = "virtio-console-pci",
506 0aab0d3a Gerd Hoffmann
        .qdev.size = sizeof(VirtIOPCIProxy),
507 0aab0d3a Gerd Hoffmann
        .init      = virtio_console_init_pci,
508 0aab0d3a Gerd Hoffmann
    },{
509 0aab0d3a Gerd Hoffmann
        .qdev.name = "virtio-balloon-pci",
510 0aab0d3a Gerd Hoffmann
        .qdev.size = sizeof(VirtIOPCIProxy),
511 0aab0d3a Gerd Hoffmann
        .init      = virtio_balloon_init_pci,
512 0aab0d3a Gerd Hoffmann
    },{
513 5c634ef3 Mark McLoughlin
        /* For compatibility with 0.10 */
514 5c634ef3 Mark McLoughlin
        .qdev.name = "virtio-blk-pci-0-10",
515 5c634ef3 Mark McLoughlin
        .qdev.size = sizeof(VirtIOPCIProxy),
516 5c634ef3 Mark McLoughlin
        .init      = virtio_blk_init_pci_0_10,
517 5c634ef3 Mark McLoughlin
    },{
518 21d58b57 Mark McLoughlin
        .qdev.name = "virtio-console-pci-0-10",
519 21d58b57 Mark McLoughlin
        .qdev.size = sizeof(VirtIOPCIProxy),
520 21d58b57 Mark McLoughlin
        .init      = virtio_console_init_pci_0_10,
521 21d58b57 Mark McLoughlin
    },{
522 0aab0d3a Gerd Hoffmann
        /* end of list */
523 0aab0d3a Gerd Hoffmann
    }
524 0aab0d3a Gerd Hoffmann
};
525 0aab0d3a Gerd Hoffmann
526 53c25cea Paul Brook
static void virtio_pci_register_devices(void)
527 53c25cea Paul Brook
{
528 0aab0d3a Gerd Hoffmann
    pci_qdev_register_many(virtio_info);
529 53c25cea Paul Brook
}
530 53c25cea Paul Brook
531 53c25cea Paul Brook
device_init(virtio_pci_register_devices)