]> git.proxmox.com Git - qemu.git/blame - hw/virtio-pci.c
Merge remote-tracking branch 'riku/linux-user-for-upstream' into staging
[qemu.git] / hw / virtio-pci.c
CommitLineData
53c25cea
PB
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 *
6b620ca3
PB
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
53c25cea
PB
16 */
17
18#include <inttypes.h>
19
20#include "virtio.h"
8172539d
MT
21#include "virtio-blk.h"
22#include "virtio-net.h"
6b331efb 23#include "virtio-serial.h"
973abc7f 24#include "virtio-scsi.h"
53c25cea 25#include "pci.h"
2f792016 26#include "qemu-error.h"
aba800a3 27#include "msix.h"
a1e0fea5 28#include "net.h"
97b15621 29#include "loader.h"
ade80dc8 30#include "kvm.h"
2446333c 31#include "blockdev.h"
9fe1ebeb 32#include "virtio-pci.h"
1129714f 33#include "range.h"
53c25cea
PB
34
35/* from Linux's linux/virtio_pci.h */
36
37/* A 32-bit r/o bitmask of the features supported by the host */
38#define VIRTIO_PCI_HOST_FEATURES 0
39
40/* A 32-bit r/w bitmask of features activated by the guest */
41#define VIRTIO_PCI_GUEST_FEATURES 4
42
43/* A 32-bit r/w PFN for the currently selected queue */
44#define VIRTIO_PCI_QUEUE_PFN 8
45
46/* A 16-bit r/o queue size for the currently selected queue */
47#define VIRTIO_PCI_QUEUE_NUM 12
48
49/* A 16-bit r/w queue selector */
50#define VIRTIO_PCI_QUEUE_SEL 14
51
52/* A 16-bit r/w queue notifier */
53#define VIRTIO_PCI_QUEUE_NOTIFY 16
54
55/* An 8-bit device status register. */
56#define VIRTIO_PCI_STATUS 18
57
58/* An 8-bit r/o interrupt status register. Reading the value will return the
59 * current contents of the ISR and will also clear it. This is effectively
60 * a read-and-acknowledge. */
61#define VIRTIO_PCI_ISR 19
62
aba800a3
MT
63/* MSI-X registers: only enabled if MSI-X is enabled. */
64/* A 16-bit vector for configuration changes. */
65#define VIRTIO_MSI_CONFIG_VECTOR 20
66/* A 16-bit vector for selected queue notifications. */
67#define VIRTIO_MSI_QUEUE_VECTOR 22
68
69/* Config space size */
70#define VIRTIO_PCI_CONFIG_NOMSI 20
71#define VIRTIO_PCI_CONFIG_MSI 24
72#define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
73 VIRTIO_PCI_CONFIG_MSI : \
74 VIRTIO_PCI_CONFIG_NOMSI)
75
76/* The remaining space is defined by each driver as the per-driver
77 * configuration space */
78#define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
79 VIRTIO_PCI_CONFIG_MSI : \
80 VIRTIO_PCI_CONFIG_NOMSI)
53c25cea 81
53c25cea
PB
82/* How many bits to shift physical queue address written to QUEUE_PFN.
83 * 12 is historical, and due to x86 page size. */
84#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
85
3dbca8e6
SH
86/* Flags track per-device state like workarounds for quirks in older guests. */
87#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
c81131db 88
53c25cea
PB
89/* QEMU doesn't strictly need write barriers since everything runs in
90 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
91 * KVM or if kqemu gets SMP support.
92 */
93#define wmb() do { } while (0)
94
82afa586
BH
95/* HACK for virtio to determine if it's running a big endian guest */
96bool virtio_is_big_endian(void);
97
53c25cea
PB
98/* virtio device */
99
7055e687 100static void virtio_pci_notify(void *opaque, uint16_t vector)
53c25cea
PB
101{
102 VirtIOPCIProxy *proxy = opaque;
aba800a3
MT
103 if (msix_enabled(&proxy->pci_dev))
104 msix_notify(&proxy->pci_dev, vector);
105 else
106 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
53c25cea
PB
107}
108
ff24bd58
MT
109static void virtio_pci_save_config(void * opaque, QEMUFile *f)
110{
111 VirtIOPCIProxy *proxy = opaque;
112 pci_device_save(&proxy->pci_dev, f);
113 msix_save(&proxy->pci_dev, f);
114 if (msix_present(&proxy->pci_dev))
115 qemu_put_be16(f, proxy->vdev->config_vector);
116}
117
118static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
119{
120 VirtIOPCIProxy *proxy = opaque;
121 if (msix_present(&proxy->pci_dev))
122 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
123}
124
125static int virtio_pci_load_config(void * opaque, QEMUFile *f)
126{
127 VirtIOPCIProxy *proxy = opaque;
128 int ret;
129 ret = pci_device_load(&proxy->pci_dev, f);
e6da7680 130 if (ret) {
ff24bd58 131 return ret;
e6da7680 132 }
ff24bd58 133 msix_load(&proxy->pci_dev, f);
e6da7680 134 if (msix_present(&proxy->pci_dev)) {
ff24bd58 135 qemu_get_be16s(f, &proxy->vdev->config_vector);
e6da7680
MT
136 } else {
137 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
138 }
139 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
140 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
141 }
ff24bd58
MT
142 return 0;
143}
144
145static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
146{
147 VirtIOPCIProxy *proxy = opaque;
148 uint16_t vector;
e6da7680
MT
149 if (msix_present(&proxy->pci_dev)) {
150 qemu_get_be16s(f, &vector);
151 } else {
152 vector = VIRTIO_NO_VECTOR;
153 }
ff24bd58 154 virtio_queue_set_vector(proxy->vdev, n, vector);
e6da7680
MT
155 if (vector != VIRTIO_NO_VECTOR) {
156 return msix_vector_use(&proxy->pci_dev, vector);
157 }
ff24bd58
MT
158 return 0;
159}
160
25db9ebe
SH
161static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
162 int n, bool assign)
163{
164 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
165 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
da146d0a
AK
166 int r = 0;
167
25db9ebe
SH
168 if (assign) {
169 r = event_notifier_init(notifier, 1);
170 if (r < 0) {
b36e3914
MT
171 error_report("%s: unable to init event notifier: %d",
172 __func__, r);
25db9ebe
SH
173 return r;
174 }
da146d0a
AK
175 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
176 true, n, event_notifier_get_fd(notifier));
25db9ebe 177 } else {
da146d0a
AK
178 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
179 true, n, event_notifier_get_fd(notifier));
25db9ebe
SH
180 /* Handle the race condition where the guest kicked and we deassigned
181 * before we got around to handling the kick.
182 */
183 if (event_notifier_test_and_clear(notifier)) {
184 virtio_queue_notify_vq(vq);
185 }
186
187 event_notifier_cleanup(notifier);
188 }
189 return r;
190}
191
192static void virtio_pci_host_notifier_read(void *opaque)
193{
194 VirtQueue *vq = opaque;
195 EventNotifier *n = virtio_queue_get_host_notifier(vq);
196 if (event_notifier_test_and_clear(n)) {
197 virtio_queue_notify_vq(vq);
198 }
199}
200
201static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
202 int n, bool assign)
203{
204 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
205 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
206 if (assign) {
207 qemu_set_fd_handler(event_notifier_get_fd(notifier),
208 virtio_pci_host_notifier_read, NULL, vq);
209 } else {
210 qemu_set_fd_handler(event_notifier_get_fd(notifier),
211 NULL, NULL, NULL);
212 }
213}
214
b36e3914 215static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe
SH
216{
217 int n, r;
218
219 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
220 proxy->ioeventfd_disabled ||
221 proxy->ioeventfd_started) {
b36e3914 222 return;
25db9ebe
SH
223 }
224
225 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
226 if (!virtio_queue_get_num(proxy->vdev, n)) {
227 continue;
228 }
229
230 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
231 if (r < 0) {
232 goto assign_error;
233 }
234
235 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
236 }
237 proxy->ioeventfd_started = true;
b36e3914 238 return;
25db9ebe
SH
239
240assign_error:
241 while (--n >= 0) {
242 if (!virtio_queue_get_num(proxy->vdev, n)) {
243 continue;
244 }
245
246 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
b36e3914
MT
247 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
248 assert(r >= 0);
25db9ebe
SH
249 }
250 proxy->ioeventfd_started = false;
b36e3914 251 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
25db9ebe
SH
252}
253
b36e3914 254static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe 255{
b36e3914 256 int r;
25db9ebe
SH
257 int n;
258
259 if (!proxy->ioeventfd_started) {
b36e3914 260 return;
25db9ebe
SH
261 }
262
263 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
264 if (!virtio_queue_get_num(proxy->vdev, n)) {
265 continue;
266 }
267
268 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
b36e3914
MT
269 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
270 assert(r >= 0);
25db9ebe
SH
271 }
272 proxy->ioeventfd_started = false;
25db9ebe
SH
273}
274
8798d6c9 275void virtio_pci_reset(DeviceState *d)
7055e687 276{
e489030d 277 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
25db9ebe 278 virtio_pci_stop_ioeventfd(proxy);
7055e687 279 virtio_reset(proxy->vdev);
aba800a3 280 msix_reset(&proxy->pci_dev);
25db9ebe 281 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
7055e687
MT
282}
283
53c25cea
PB
284static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
285{
286 VirtIOPCIProxy *proxy = opaque;
287 VirtIODevice *vdev = proxy->vdev;
c227f099 288 target_phys_addr_t pa;
53c25cea 289
53c25cea
PB
290 switch (addr) {
291 case VIRTIO_PCI_GUEST_FEATURES:
292 /* Guest does not negotiate properly? We have to assume nothing. */
293 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
ad0c9332 294 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
53c25cea 295 }
ad0c9332 296 virtio_set_features(vdev, val);
53c25cea
PB
297 break;
298 case VIRTIO_PCI_QUEUE_PFN:
c227f099 299 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
1b8e9b27 300 if (pa == 0) {
25db9ebe 301 virtio_pci_stop_ioeventfd(proxy);
1b8e9b27
MT
302 virtio_reset(proxy->vdev);
303 msix_unuse_all_vectors(&proxy->pci_dev);
304 }
7055e687
MT
305 else
306 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
53c25cea
PB
307 break;
308 case VIRTIO_PCI_QUEUE_SEL:
309 if (val < VIRTIO_PCI_QUEUE_MAX)
310 vdev->queue_sel = val;
311 break;
312 case VIRTIO_PCI_QUEUE_NOTIFY:
7157e2e2
SH
313 if (val < VIRTIO_PCI_QUEUE_MAX) {
314 virtio_queue_notify(vdev, val);
315 }
53c25cea
PB
316 break;
317 case VIRTIO_PCI_STATUS:
25db9ebe
SH
318 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
319 virtio_pci_stop_ioeventfd(proxy);
320 }
321
3e607cb5 322 virtio_set_status(vdev, val & 0xFF);
25db9ebe
SH
323
324 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
325 virtio_pci_start_ioeventfd(proxy);
326 }
327
1b8e9b27
MT
328 if (vdev->status == 0) {
329 virtio_reset(proxy->vdev);
330 msix_unuse_all_vectors(&proxy->pci_dev);
331 }
c81131db
AG
332
333 /* Linux before 2.6.34 sets the device as OK without enabling
334 the PCI device bus master bit. In this case we need to disable
335 some safety checks. */
336 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
337 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
3dbca8e6 338 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
c81131db 339 }
53c25cea 340 break;
aba800a3
MT
341 case VIRTIO_MSI_CONFIG_VECTOR:
342 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
343 /* Make it possible for guest to discover an error took place. */
344 if (msix_vector_use(&proxy->pci_dev, val) < 0)
345 val = VIRTIO_NO_VECTOR;
346 vdev->config_vector = val;
347 break;
348 case VIRTIO_MSI_QUEUE_VECTOR:
349 msix_vector_unuse(&proxy->pci_dev,
350 virtio_queue_vector(vdev, vdev->queue_sel));
351 /* Make it possible for guest to discover an error took place. */
352 if (msix_vector_use(&proxy->pci_dev, val) < 0)
353 val = VIRTIO_NO_VECTOR;
354 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
355 break;
356 default:
4e02d460
SH
357 error_report("%s: unexpected address 0x%x value 0x%x",
358 __func__, addr, val);
aba800a3 359 break;
53c25cea
PB
360 }
361}
362
aba800a3 363static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
53c25cea 364{
53c25cea
PB
365 VirtIODevice *vdev = proxy->vdev;
366 uint32_t ret = 0xFFFFFFFF;
367
53c25cea
PB
368 switch (addr) {
369 case VIRTIO_PCI_HOST_FEATURES:
8172539d 370 ret = proxy->host_features;
53c25cea
PB
371 break;
372 case VIRTIO_PCI_GUEST_FEATURES:
704a76fc 373 ret = vdev->guest_features;
53c25cea
PB
374 break;
375 case VIRTIO_PCI_QUEUE_PFN:
376 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
377 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
378 break;
379 case VIRTIO_PCI_QUEUE_NUM:
380 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
381 break;
382 case VIRTIO_PCI_QUEUE_SEL:
383 ret = vdev->queue_sel;
384 break;
385 case VIRTIO_PCI_STATUS:
386 ret = vdev->status;
387 break;
388 case VIRTIO_PCI_ISR:
389 /* reading from the ISR also clears it. */
390 ret = vdev->isr;
391 vdev->isr = 0;
7055e687 392 qemu_set_irq(proxy->pci_dev.irq[0], 0);
53c25cea 393 break;
aba800a3
MT
394 case VIRTIO_MSI_CONFIG_VECTOR:
395 ret = vdev->config_vector;
396 break;
397 case VIRTIO_MSI_QUEUE_VECTOR:
398 ret = virtio_queue_vector(vdev, vdev->queue_sel);
399 break;
53c25cea
PB
400 default:
401 break;
402 }
403
404 return ret;
405}
406
407static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
408{
409 VirtIOPCIProxy *proxy = opaque;
aba800a3 410 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
411 if (addr < config)
412 return virtio_ioport_read(proxy, addr);
413 addr -= config;
53c25cea
PB
414 return virtio_config_readb(proxy->vdev, addr);
415}
416
417static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
418{
419 VirtIOPCIProxy *proxy = opaque;
aba800a3 420 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
82afa586 421 uint16_t val;
aba800a3
MT
422 if (addr < config)
423 return virtio_ioport_read(proxy, addr);
424 addr -= config;
82afa586
BH
425 val = virtio_config_readw(proxy->vdev, addr);
426 if (virtio_is_big_endian()) {
427 /*
428 * virtio is odd, ioports are LE but config space is target native
429 * endian. However, in qemu, all PIO is LE, so we need to re-swap
430 * on BE targets
431 */
432 val = bswap16(val);
433 }
434 return val;
53c25cea
PB
435}
436
437static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
438{
439 VirtIOPCIProxy *proxy = opaque;
aba800a3 440 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
82afa586 441 uint32_t val;
aba800a3
MT
442 if (addr < config)
443 return virtio_ioport_read(proxy, addr);
444 addr -= config;
82afa586
BH
445 val = virtio_config_readl(proxy->vdev, addr);
446 if (virtio_is_big_endian()) {
447 val = bswap32(val);
448 }
449 return val;
53c25cea
PB
450}
451
452static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
453{
454 VirtIOPCIProxy *proxy = opaque;
aba800a3 455 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
456 if (addr < config) {
457 virtio_ioport_write(proxy, addr, val);
458 return;
459 }
460 addr -= config;
53c25cea
PB
461 virtio_config_writeb(proxy->vdev, addr, val);
462}
463
464static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
465{
466 VirtIOPCIProxy *proxy = opaque;
aba800a3 467 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
468 if (addr < config) {
469 virtio_ioport_write(proxy, addr, val);
470 return;
471 }
472 addr -= config;
82afa586
BH
473 if (virtio_is_big_endian()) {
474 val = bswap16(val);
475 }
53c25cea
PB
476 virtio_config_writew(proxy->vdev, addr, val);
477}
478
479static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
480{
481 VirtIOPCIProxy *proxy = opaque;
aba800a3 482 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
483 if (addr < config) {
484 virtio_ioport_write(proxy, addr, val);
485 return;
486 }
487 addr -= config;
82afa586
BH
488 if (virtio_is_big_endian()) {
489 val = bswap32(val);
490 }
53c25cea
PB
491 virtio_config_writel(proxy->vdev, addr, val);
492}
493
da146d0a
AK
494const MemoryRegionPortio virtio_portio[] = {
495 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
496 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
497 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
498 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
499 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
500 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
501 PORTIO_END_OF_LIST()
502};
53c25cea 503
da146d0a
AK
504static const MemoryRegionOps virtio_pci_config_ops = {
505 .old_portio = virtio_portio,
506 .endianness = DEVICE_LITTLE_ENDIAN,
507};
aba800a3
MT
508
509static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
510 uint32_t val, int len)
511{
ed757e14
YV
512 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
513
1129714f
MT
514 pci_default_write_config(pci_dev, address, val, len);
515
516 if (range_covers_byte(address, len, PCI_COMMAND) &&
517 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
518 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
519 virtio_pci_stop_ioeventfd(proxy);
520 virtio_set_status(proxy->vdev,
521 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
ed757e14
YV
522 }
523
85352471 524 msix_write_config(pci_dev, address, val, len);
53c25cea
PB
525}
526
6d74ca5a
MT
527static unsigned virtio_pci_get_features(void *opaque)
528{
8172539d
MT
529 VirtIOPCIProxy *proxy = opaque;
530 return proxy->host_features;
6d74ca5a
MT
531}
532
ade80dc8
MT
533static void virtio_pci_guest_notifier_read(void *opaque)
534{
535 VirtQueue *vq = opaque;
536 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
537 if (event_notifier_test_and_clear(n)) {
538 virtio_irq(vq);
539 }
540}
541
542static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
543{
544 VirtIOPCIProxy *proxy = opaque;
545 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
546 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
547
548 if (assign) {
549 int r = event_notifier_init(notifier, 0);
550 if (r < 0) {
551 return r;
552 }
553 qemu_set_fd_handler(event_notifier_get_fd(notifier),
554 virtio_pci_guest_notifier_read, NULL, vq);
555 } else {
556 qemu_set_fd_handler(event_notifier_get_fd(notifier),
557 NULL, NULL, NULL);
558 event_notifier_cleanup(notifier);
559 }
560
561 return 0;
562}
563
5430a28f 564static bool virtio_pci_query_guest_notifiers(void *opaque)
565{
566 VirtIOPCIProxy *proxy = opaque;
567 return msix_enabled(&proxy->pci_dev);
568}
569
54dd9321
MT
570static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
571{
572 VirtIOPCIProxy *proxy = opaque;
573 VirtIODevice *vdev = proxy->vdev;
574 int r, n;
575
576 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
577 if (!virtio_queue_get_num(vdev, n)) {
578 break;
579 }
580
581 r = virtio_pci_set_guest_notifier(opaque, n, assign);
582 if (r < 0) {
583 goto assign_error;
584 }
585 }
586
587 return 0;
588
589assign_error:
590 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
591 while (--n >= 0) {
592 virtio_pci_set_guest_notifier(opaque, n, !assign);
593 }
594 return r;
595}
596
ade80dc8
MT
597static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
598{
599 VirtIOPCIProxy *proxy = opaque;
25db9ebe
SH
600
601 /* Stop using ioeventfd for virtqueue kick if the device starts using host
602 * notifiers. This makes it easy to avoid stepping on each others' toes.
603 */
604 proxy->ioeventfd_disabled = assign;
ade80dc8 605 if (assign) {
25db9ebe
SH
606 virtio_pci_stop_ioeventfd(proxy);
607 }
608 /* We don't need to start here: it's not needed because backend
609 * currently only stops on status change away from ok,
610 * reset, vmstop and such. If we do add code to start here,
611 * need to check vmstate, device state etc. */
612 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
613}
614
615static void virtio_pci_vmstate_change(void *opaque, bool running)
616{
617 VirtIOPCIProxy *proxy = opaque;
618
619 if (running) {
89c473fd
MT
620 /* Try to find out if the guest has bus master disabled, but is
621 in ready state. Then we have a buggy guest OS. */
622 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
623 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
624 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
625 }
25db9ebe 626 virtio_pci_start_ioeventfd(proxy);
ade80dc8 627 } else {
25db9ebe 628 virtio_pci_stop_ioeventfd(proxy);
ade80dc8 629 }
ade80dc8
MT
630}
631
53c25cea 632static const VirtIOBindings virtio_pci_bindings = {
ff24bd58
MT
633 .notify = virtio_pci_notify,
634 .save_config = virtio_pci_save_config,
635 .load_config = virtio_pci_load_config,
636 .save_queue = virtio_pci_save_queue,
637 .load_queue = virtio_pci_load_queue,
6d74ca5a 638 .get_features = virtio_pci_get_features,
5430a28f 639 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
ade80dc8 640 .set_host_notifier = virtio_pci_set_host_notifier,
54dd9321 641 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
25db9ebe 642 .vmstate_change = virtio_pci_vmstate_change,
53c25cea
PB
643};
644
befeac45 645void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
53c25cea
PB
646{
647 uint8_t *config;
648 uint32_t size;
649
650 proxy->vdev = vdev;
651
652 config = proxy->pci_dev.config;
53c25cea 653
e75ccf2c
IY
654 if (proxy->class_code) {
655 pci_config_set_class(config, proxy->class_code);
656 }
ad3d11e6
HKR
657 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
658 pci_get_word(config + PCI_VENDOR_ID));
659 pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
660 config[PCI_INTERRUPT_PIN] = 1;
53c25cea 661
95524ae8
AK
662 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
663 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
664 &proxy->msix_bar, 1, 0)) {
e824b2cc
AK
665 pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
666 &proxy->msix_bar);
aba800a3
MT
667 } else
668 vdev->nvectors = 0;
669
ed757e14
YV
670 proxy->pci_dev.config_write = virtio_write_config;
671
aba800a3 672 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
53c25cea
PB
673 if (size & (size-1))
674 size = 1 << qemu_fls(size);
675
da146d0a
AK
676 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
677 "virtio-pci", size);
e824b2cc
AK
678 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
679 &proxy->bar);
53c25cea 680
25db9ebe
SH
681 if (!kvm_has_many_ioeventfds()) {
682 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
683 }
684
53c25cea 685 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
8172539d
MT
686 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
687 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
688 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
53c25cea
PB
689}
690
81a322d4 691static int virtio_blk_init_pci(PCIDevice *pci_dev)
53c25cea
PB
692{
693 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
694 VirtIODevice *vdev;
695
ab73ff29
GH
696 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
697 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
698 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
53c25cea 699
a8686a9b
MA
700 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
701 &proxy->block_serial);
ac0c14d7
MA
702 if (!vdev) {
703 return -1;
704 }
177539e0 705 vdev->nvectors = proxy->nvectors;
e75ccf2c 706 virtio_init_pci(proxy, vdev);
177539e0
GH
707 /* make the actual value visible */
708 proxy->nvectors = vdev->nvectors;
81a322d4 709 return 0;
21d58b57
MM
710}
711
0f457d91
MT
712static int virtio_exit_pci(PCIDevice *pci_dev)
713{
da146d0a 714 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
95524ae8 715 int r;
da146d0a
AK
716
717 memory_region_destroy(&proxy->bar);
95524ae8
AK
718 r = msix_uninit(pci_dev, &proxy->msix_bar);
719 memory_region_destroy(&proxy->msix_bar);
720 return r;
0f457d91
MT
721}
722
56a14938
GH
723static int virtio_blk_exit_pci(PCIDevice *pci_dev)
724{
725 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
726
25db9ebe 727 virtio_pci_stop_ioeventfd(proxy);
9d0d3138 728 virtio_blk_exit(proxy->vdev);
f8b6cc00 729 blockdev_mark_auto_del(proxy->block.bs);
0f457d91 730 return virtio_exit_pci(pci_dev);
56a14938
GH
731}
732
98b19252 733static int virtio_serial_init_pci(PCIDevice *pci_dev)
21d58b57 734{
d6beee99 735 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
85c2c735
MM
736 VirtIODevice *vdev;
737
d6beee99
GH
738 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
739 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
740 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
741 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
742
6b331efb 743 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
25fe3654
AS
744 if (!vdev) {
745 return -1;
746 }
573fb60c 747 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
6b331efb 748 ? proxy->serial.max_virtserial_ports + 1
573fb60c 749 : proxy->nvectors;
e75ccf2c 750 virtio_init_pci(proxy, vdev);
a1829205 751 proxy->nvectors = vdev->nvectors;
81a322d4 752 return 0;
53c25cea
PB
753}
754
8b53a865
AS
755static int virtio_serial_exit_pci(PCIDevice *pci_dev)
756{
757 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
758
32059220 759 virtio_pci_stop_ioeventfd(proxy);
8b53a865
AS
760 virtio_serial_exit(proxy->vdev);
761 return virtio_exit_pci(pci_dev);
762}
763
81a322d4 764static int virtio_net_init_pci(PCIDevice *pci_dev)
53c25cea
PB
765{
766 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
767 VirtIODevice *vdev;
768
f0c07c7c 769 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
a1e0fea5 770
97b15621 771 vdev->nvectors = proxy->nvectors;
e75ccf2c 772 virtio_init_pci(proxy, vdev);
a1e0fea5
GH
773
774 /* make the actual value visible */
775 proxy->nvectors = vdev->nvectors;
81a322d4 776 return 0;
53c25cea
PB
777}
778
97b15621
GH
779static int virtio_net_exit_pci(PCIDevice *pci_dev)
780{
781 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
782
25db9ebe 783 virtio_pci_stop_ioeventfd(proxy);
97b15621
GH
784 virtio_net_exit(proxy->vdev);
785 return virtio_exit_pci(pci_dev);
786}
787
81a322d4 788static int virtio_balloon_init_pci(PCIDevice *pci_dev)
53c25cea
PB
789{
790 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
791 VirtIODevice *vdev;
792
2ba1d381
DG
793 if (proxy->class_code != PCI_CLASS_OTHERS &&
794 proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
795 proxy->class_code = PCI_CLASS_OTHERS;
796 }
797
53c25cea 798 vdev = virtio_balloon_init(&pci_dev->qdev);
f76f6655
AS
799 if (!vdev) {
800 return -1;
801 }
e75ccf2c 802 virtio_init_pci(proxy, vdev);
81a322d4 803 return 0;
53c25cea
PB
804}
805
855d7e25
AS
806static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
807{
808 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
809
810 virtio_pci_stop_ioeventfd(proxy);
811 virtio_balloon_exit(proxy->vdev);
812 return virtio_exit_pci(pci_dev);
813}
814
40021f08
AL
815static Property virtio_blk_properties[] = {
816 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
817 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
818 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
819 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
820 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
821 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
822 DEFINE_PROP_END_OF_LIST(),
e855761c
AL
823};
824
40021f08
AL
825static void virtio_blk_class_init(ObjectClass *klass, void *data)
826{
39bffca2 827 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
828 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
829
830 k->init = virtio_blk_init_pci;
831 k->exit = virtio_blk_exit_pci;
832 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
833 k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
834 k->revision = VIRTIO_PCI_ABI_VERSION;
835 k->class_id = PCI_CLASS_STORAGE_SCSI;
39bffca2
AL
836 dc->reset = virtio_pci_reset;
837 dc->props = virtio_blk_properties;
40021f08
AL
838}
839
39bffca2
AL
840static TypeInfo virtio_blk_info = {
841 .name = "virtio-blk-pci",
842 .parent = TYPE_PCI_DEVICE,
843 .instance_size = sizeof(VirtIOPCIProxy),
844 .class_init = virtio_blk_class_init,
40021f08
AL
845};
846
847static Property virtio_net_properties[] = {
848 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
849 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
850 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
851 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
852 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
853 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
854 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
855 DEFINE_PROP_END_OF_LIST(),
856};
857
858static void virtio_net_class_init(ObjectClass *klass, void *data)
859{
39bffca2 860 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
861 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
862
863 k->init = virtio_net_init_pci;
864 k->exit = virtio_net_exit_pci;
865 k->romfile = "pxe-virtio.rom";
866 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
867 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
868 k->revision = VIRTIO_PCI_ABI_VERSION;
869 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
39bffca2
AL
870 dc->reset = virtio_pci_reset;
871 dc->props = virtio_net_properties;
40021f08
AL
872}
873
39bffca2
AL
874static TypeInfo virtio_net_info = {
875 .name = "virtio-net-pci",
876 .parent = TYPE_PCI_DEVICE,
877 .instance_size = sizeof(VirtIOPCIProxy),
878 .class_init = virtio_net_class_init,
e855761c
AL
879};
880
40021f08
AL
881static Property virtio_serial_properties[] = {
882 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
883 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
884 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
885 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
886 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
887 DEFINE_PROP_END_OF_LIST(),
e855761c
AL
888};
889
40021f08
AL
890static void virtio_serial_class_init(ObjectClass *klass, void *data)
891{
39bffca2 892 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
893 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
894
895 k->init = virtio_serial_init_pci;
896 k->exit = virtio_serial_exit_pci;
897 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
898 k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
899 k->revision = VIRTIO_PCI_ABI_VERSION;
900 k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
39bffca2
AL
901 dc->reset = virtio_pci_reset;
902 dc->props = virtio_serial_properties;
40021f08
AL
903}
904
39bffca2
AL
905static TypeInfo virtio_serial_info = {
906 .name = "virtio-serial-pci",
907 .parent = TYPE_PCI_DEVICE,
908 .instance_size = sizeof(VirtIOPCIProxy),
909 .class_init = virtio_serial_class_init,
40021f08
AL
910};
911
912static Property virtio_balloon_properties[] = {
913 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
2ba1d381 914 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
40021f08
AL
915 DEFINE_PROP_END_OF_LIST(),
916};
917
918static void virtio_balloon_class_init(ObjectClass *klass, void *data)
919{
39bffca2 920 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
921 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
922
923 k->init = virtio_balloon_init_pci;
924 k->exit = virtio_balloon_exit_pci;
925 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
926 k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
927 k->revision = VIRTIO_PCI_ABI_VERSION;
2ba1d381 928 k->class_id = PCI_CLASS_OTHERS;
39bffca2
AL
929 dc->reset = virtio_pci_reset;
930 dc->props = virtio_balloon_properties;
40021f08
AL
931}
932
39bffca2
AL
933static TypeInfo virtio_balloon_info = {
934 .name = "virtio-balloon-pci",
935 .parent = TYPE_PCI_DEVICE,
936 .instance_size = sizeof(VirtIOPCIProxy),
937 .class_init = virtio_balloon_class_init,
0aab0d3a
GH
938};
939
973abc7f
SH
940static int virtio_scsi_init_pci(PCIDevice *pci_dev)
941{
942 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
943 VirtIODevice *vdev;
944
945 vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
946 if (!vdev) {
947 return -EINVAL;
948 }
949
950 vdev->nvectors = proxy->nvectors;
951 virtio_init_pci(proxy, vdev);
952
953 /* make the actual value visible */
954 proxy->nvectors = vdev->nvectors;
955 return 0;
956}
957
958static int virtio_scsi_exit_pci(PCIDevice *pci_dev)
959{
960 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
961
962 virtio_scsi_exit(proxy->vdev);
963 return virtio_exit_pci(pci_dev);
964}
965
966static Property virtio_scsi_properties[] = {
967 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
968 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
969 DEFINE_PROP_END_OF_LIST(),
970};
971
972static void virtio_scsi_class_init(ObjectClass *klass, void *data)
973{
974 DeviceClass *dc = DEVICE_CLASS(klass);
975 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
976
977 k->init = virtio_scsi_init_pci;
978 k->exit = virtio_scsi_exit_pci;
979 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
980 k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
981 k->revision = 0x00;
982 k->class_id = PCI_CLASS_STORAGE_SCSI;
983 dc->reset = virtio_pci_reset;
984 dc->props = virtio_scsi_properties;
985}
986
987static TypeInfo virtio_scsi_info = {
988 .name = "virtio-scsi-pci",
989 .parent = TYPE_PCI_DEVICE,
990 .instance_size = sizeof(VirtIOPCIProxy),
991 .class_init = virtio_scsi_class_init,
992};
993
83f7d43a 994static void virtio_pci_register_types(void)
53c25cea 995{
39bffca2 996 type_register_static(&virtio_blk_info);
39bffca2 997 type_register_static(&virtio_net_info);
39bffca2 998 type_register_static(&virtio_serial_info);
39bffca2 999 type_register_static(&virtio_balloon_info);
973abc7f 1000 type_register_static(&virtio_scsi_info);
53c25cea
PB
1001}
1002
83f7d43a 1003type_init(virtio_pci_register_types)