prep: qdev'ify Raven host bridge (PCIDevice)
[qemu/kevin.git] / hw / virtio-pci.c
blobcaff0aa2ebd8b8ecf8db4110fb6c0e09ee1aa829
1 /*
2 * Virtio PCI Bindings
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
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.
18 #include <inttypes.h>
20 #include "virtio.h"
21 #include "virtio-blk.h"
22 #include "virtio-net.h"
23 #include "virtio-serial.h"
24 #include "pci.h"
25 #include "qemu-error.h"
26 #include "msix.h"
27 #include "net.h"
28 #include "loader.h"
29 #include "kvm.h"
30 #include "blockdev.h"
31 #include "virtio-pci.h"
32 #include "range.h"
34 /* from Linux's linux/virtio_pci.h */
36 /* A 32-bit r/o bitmask of the features supported by the host */
37 #define VIRTIO_PCI_HOST_FEATURES 0
39 /* A 32-bit r/w bitmask of features activated by the guest */
40 #define VIRTIO_PCI_GUEST_FEATURES 4
42 /* A 32-bit r/w PFN for the currently selected queue */
43 #define VIRTIO_PCI_QUEUE_PFN 8
45 /* A 16-bit r/o queue size for the currently selected queue */
46 #define VIRTIO_PCI_QUEUE_NUM 12
48 /* A 16-bit r/w queue selector */
49 #define VIRTIO_PCI_QUEUE_SEL 14
51 /* A 16-bit r/w queue notifier */
52 #define VIRTIO_PCI_QUEUE_NOTIFY 16
54 /* An 8-bit device status register. */
55 #define VIRTIO_PCI_STATUS 18
57 /* An 8-bit r/o interrupt status register. Reading the value will return the
58 * current contents of the ISR and will also clear it. This is effectively
59 * a read-and-acknowledge. */
60 #define VIRTIO_PCI_ISR 19
62 /* MSI-X registers: only enabled if MSI-X is enabled. */
63 /* A 16-bit vector for configuration changes. */
64 #define VIRTIO_MSI_CONFIG_VECTOR 20
65 /* A 16-bit vector for selected queue notifications. */
66 #define VIRTIO_MSI_QUEUE_VECTOR 22
68 /* Config space size */
69 #define VIRTIO_PCI_CONFIG_NOMSI 20
70 #define VIRTIO_PCI_CONFIG_MSI 24
71 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
72 VIRTIO_PCI_CONFIG_MSI : \
73 VIRTIO_PCI_CONFIG_NOMSI)
75 /* The remaining space is defined by each driver as the per-driver
76 * configuration space */
77 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
78 VIRTIO_PCI_CONFIG_MSI : \
79 VIRTIO_PCI_CONFIG_NOMSI)
81 /* How many bits to shift physical queue address written to QUEUE_PFN.
82 * 12 is historical, and due to x86 page size. */
83 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
85 /* Flags track per-device state like workarounds for quirks in older guests. */
86 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
88 /* QEMU doesn't strictly need write barriers since everything runs in
89 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
90 * KVM or if kqemu gets SMP support.
92 #define wmb() do { } while (0)
94 /* virtio device */
96 static void virtio_pci_notify(void *opaque, uint16_t vector)
98 VirtIOPCIProxy *proxy = opaque;
99 if (msix_enabled(&proxy->pci_dev))
100 msix_notify(&proxy->pci_dev, vector);
101 else
102 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
105 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
107 VirtIOPCIProxy *proxy = opaque;
108 pci_device_save(&proxy->pci_dev, f);
109 msix_save(&proxy->pci_dev, f);
110 if (msix_present(&proxy->pci_dev))
111 qemu_put_be16(f, proxy->vdev->config_vector);
114 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
116 VirtIOPCIProxy *proxy = opaque;
117 if (msix_present(&proxy->pci_dev))
118 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
121 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
123 VirtIOPCIProxy *proxy = opaque;
124 int ret;
125 ret = pci_device_load(&proxy->pci_dev, f);
126 if (ret) {
127 return ret;
129 msix_load(&proxy->pci_dev, f);
130 if (msix_present(&proxy->pci_dev)) {
131 qemu_get_be16s(f, &proxy->vdev->config_vector);
132 } else {
133 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
135 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
136 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
138 return 0;
141 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
143 VirtIOPCIProxy *proxy = opaque;
144 uint16_t vector;
145 if (msix_present(&proxy->pci_dev)) {
146 qemu_get_be16s(f, &vector);
147 } else {
148 vector = VIRTIO_NO_VECTOR;
150 virtio_queue_set_vector(proxy->vdev, n, vector);
151 if (vector != VIRTIO_NO_VECTOR) {
152 return msix_vector_use(&proxy->pci_dev, vector);
154 return 0;
157 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
158 int n, bool assign)
160 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
161 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
162 int r = 0;
164 if (assign) {
165 r = event_notifier_init(notifier, 1);
166 if (r < 0) {
167 error_report("%s: unable to init event notifier: %d",
168 __func__, r);
169 return r;
171 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
172 true, n, event_notifier_get_fd(notifier));
173 } else {
174 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
175 true, n, event_notifier_get_fd(notifier));
176 /* Handle the race condition where the guest kicked and we deassigned
177 * before we got around to handling the kick.
179 if (event_notifier_test_and_clear(notifier)) {
180 virtio_queue_notify_vq(vq);
183 event_notifier_cleanup(notifier);
185 return r;
188 static void virtio_pci_host_notifier_read(void *opaque)
190 VirtQueue *vq = opaque;
191 EventNotifier *n = virtio_queue_get_host_notifier(vq);
192 if (event_notifier_test_and_clear(n)) {
193 virtio_queue_notify_vq(vq);
197 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
198 int n, bool assign)
200 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
201 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
202 if (assign) {
203 qemu_set_fd_handler(event_notifier_get_fd(notifier),
204 virtio_pci_host_notifier_read, NULL, vq);
205 } else {
206 qemu_set_fd_handler(event_notifier_get_fd(notifier),
207 NULL, NULL, NULL);
211 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
213 int n, r;
215 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
216 proxy->ioeventfd_disabled ||
217 proxy->ioeventfd_started) {
218 return;
221 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
222 if (!virtio_queue_get_num(proxy->vdev, n)) {
223 continue;
226 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
227 if (r < 0) {
228 goto assign_error;
231 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
233 proxy->ioeventfd_started = true;
234 return;
236 assign_error:
237 while (--n >= 0) {
238 if (!virtio_queue_get_num(proxy->vdev, n)) {
239 continue;
242 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
243 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
244 assert(r >= 0);
246 proxy->ioeventfd_started = false;
247 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
250 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
252 int r;
253 int n;
255 if (!proxy->ioeventfd_started) {
256 return;
259 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
260 if (!virtio_queue_get_num(proxy->vdev, n)) {
261 continue;
264 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
265 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
266 assert(r >= 0);
268 proxy->ioeventfd_started = false;
271 void virtio_pci_reset(DeviceState *d)
273 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
274 virtio_pci_stop_ioeventfd(proxy);
275 virtio_reset(proxy->vdev);
276 msix_reset(&proxy->pci_dev);
277 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
280 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
282 VirtIOPCIProxy *proxy = opaque;
283 VirtIODevice *vdev = proxy->vdev;
284 target_phys_addr_t pa;
286 switch (addr) {
287 case VIRTIO_PCI_GUEST_FEATURES:
288 /* Guest does not negotiate properly? We have to assume nothing. */
289 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
290 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
292 virtio_set_features(vdev, val);
293 break;
294 case VIRTIO_PCI_QUEUE_PFN:
295 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
296 if (pa == 0) {
297 virtio_pci_stop_ioeventfd(proxy);
298 virtio_reset(proxy->vdev);
299 msix_unuse_all_vectors(&proxy->pci_dev);
301 else
302 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
303 break;
304 case VIRTIO_PCI_QUEUE_SEL:
305 if (val < VIRTIO_PCI_QUEUE_MAX)
306 vdev->queue_sel = val;
307 break;
308 case VIRTIO_PCI_QUEUE_NOTIFY:
309 if (val < VIRTIO_PCI_QUEUE_MAX) {
310 virtio_queue_notify(vdev, val);
312 break;
313 case VIRTIO_PCI_STATUS:
314 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
315 virtio_pci_stop_ioeventfd(proxy);
318 virtio_set_status(vdev, val & 0xFF);
320 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
321 virtio_pci_start_ioeventfd(proxy);
324 if (vdev->status == 0) {
325 virtio_reset(proxy->vdev);
326 msix_unuse_all_vectors(&proxy->pci_dev);
329 /* Linux before 2.6.34 sets the device as OK without enabling
330 the PCI device bus master bit. In this case we need to disable
331 some safety checks. */
332 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
333 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
334 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
336 break;
337 case VIRTIO_MSI_CONFIG_VECTOR:
338 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
339 /* Make it possible for guest to discover an error took place. */
340 if (msix_vector_use(&proxy->pci_dev, val) < 0)
341 val = VIRTIO_NO_VECTOR;
342 vdev->config_vector = val;
343 break;
344 case VIRTIO_MSI_QUEUE_VECTOR:
345 msix_vector_unuse(&proxy->pci_dev,
346 virtio_queue_vector(vdev, vdev->queue_sel));
347 /* Make it possible for guest to discover an error took place. */
348 if (msix_vector_use(&proxy->pci_dev, val) < 0)
349 val = VIRTIO_NO_VECTOR;
350 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
351 break;
352 default:
353 error_report("%s: unexpected address 0x%x value 0x%x",
354 __func__, addr, val);
355 break;
359 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
361 VirtIODevice *vdev = proxy->vdev;
362 uint32_t ret = 0xFFFFFFFF;
364 switch (addr) {
365 case VIRTIO_PCI_HOST_FEATURES:
366 ret = proxy->host_features;
367 break;
368 case VIRTIO_PCI_GUEST_FEATURES:
369 ret = vdev->guest_features;
370 break;
371 case VIRTIO_PCI_QUEUE_PFN:
372 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
373 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
374 break;
375 case VIRTIO_PCI_QUEUE_NUM:
376 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
377 break;
378 case VIRTIO_PCI_QUEUE_SEL:
379 ret = vdev->queue_sel;
380 break;
381 case VIRTIO_PCI_STATUS:
382 ret = vdev->status;
383 break;
384 case VIRTIO_PCI_ISR:
385 /* reading from the ISR also clears it. */
386 ret = vdev->isr;
387 vdev->isr = 0;
388 qemu_set_irq(proxy->pci_dev.irq[0], 0);
389 break;
390 case VIRTIO_MSI_CONFIG_VECTOR:
391 ret = vdev->config_vector;
392 break;
393 case VIRTIO_MSI_QUEUE_VECTOR:
394 ret = virtio_queue_vector(vdev, vdev->queue_sel);
395 break;
396 default:
397 break;
400 return ret;
403 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
405 VirtIOPCIProxy *proxy = opaque;
406 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
407 if (addr < config)
408 return virtio_ioport_read(proxy, addr);
409 addr -= config;
410 return virtio_config_readb(proxy->vdev, addr);
413 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
415 VirtIOPCIProxy *proxy = opaque;
416 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
417 if (addr < config)
418 return virtio_ioport_read(proxy, addr);
419 addr -= config;
420 return virtio_config_readw(proxy->vdev, addr);
423 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
425 VirtIOPCIProxy *proxy = opaque;
426 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
427 if (addr < config)
428 return virtio_ioport_read(proxy, addr);
429 addr -= config;
430 return virtio_config_readl(proxy->vdev, addr);
433 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
435 VirtIOPCIProxy *proxy = opaque;
436 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
437 if (addr < config) {
438 virtio_ioport_write(proxy, addr, val);
439 return;
441 addr -= config;
442 virtio_config_writeb(proxy->vdev, addr, val);
445 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
447 VirtIOPCIProxy *proxy = opaque;
448 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
449 if (addr < config) {
450 virtio_ioport_write(proxy, addr, val);
451 return;
453 addr -= config;
454 virtio_config_writew(proxy->vdev, addr, val);
457 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
459 VirtIOPCIProxy *proxy = opaque;
460 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
461 if (addr < config) {
462 virtio_ioport_write(proxy, addr, val);
463 return;
465 addr -= config;
466 virtio_config_writel(proxy->vdev, addr, val);
469 const MemoryRegionPortio virtio_portio[] = {
470 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
471 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
472 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
473 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
474 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
475 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
476 PORTIO_END_OF_LIST()
479 static const MemoryRegionOps virtio_pci_config_ops = {
480 .old_portio = virtio_portio,
481 .endianness = DEVICE_LITTLE_ENDIAN,
484 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
485 uint32_t val, int len)
487 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
489 pci_default_write_config(pci_dev, address, val, len);
491 if (range_covers_byte(address, len, PCI_COMMAND) &&
492 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
493 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
494 virtio_pci_stop_ioeventfd(proxy);
495 virtio_set_status(proxy->vdev,
496 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
499 msix_write_config(pci_dev, address, val, len);
502 static unsigned virtio_pci_get_features(void *opaque)
504 VirtIOPCIProxy *proxy = opaque;
505 return proxy->host_features;
508 static void virtio_pci_guest_notifier_read(void *opaque)
510 VirtQueue *vq = opaque;
511 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
512 if (event_notifier_test_and_clear(n)) {
513 virtio_irq(vq);
517 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
519 VirtIOPCIProxy *proxy = opaque;
520 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
521 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
523 if (assign) {
524 int r = event_notifier_init(notifier, 0);
525 if (r < 0) {
526 return r;
528 qemu_set_fd_handler(event_notifier_get_fd(notifier),
529 virtio_pci_guest_notifier_read, NULL, vq);
530 } else {
531 qemu_set_fd_handler(event_notifier_get_fd(notifier),
532 NULL, NULL, NULL);
533 event_notifier_cleanup(notifier);
536 return 0;
539 static bool virtio_pci_query_guest_notifiers(void *opaque)
541 VirtIOPCIProxy *proxy = opaque;
542 return msix_enabled(&proxy->pci_dev);
545 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
547 VirtIOPCIProxy *proxy = opaque;
548 VirtIODevice *vdev = proxy->vdev;
549 int r, n;
551 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
552 if (!virtio_queue_get_num(vdev, n)) {
553 break;
556 r = virtio_pci_set_guest_notifier(opaque, n, assign);
557 if (r < 0) {
558 goto assign_error;
562 return 0;
564 assign_error:
565 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
566 while (--n >= 0) {
567 virtio_pci_set_guest_notifier(opaque, n, !assign);
569 return r;
572 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
574 VirtIOPCIProxy *proxy = opaque;
576 /* Stop using ioeventfd for virtqueue kick if the device starts using host
577 * notifiers. This makes it easy to avoid stepping on each others' toes.
579 proxy->ioeventfd_disabled = assign;
580 if (assign) {
581 virtio_pci_stop_ioeventfd(proxy);
583 /* We don't need to start here: it's not needed because backend
584 * currently only stops on status change away from ok,
585 * reset, vmstop and such. If we do add code to start here,
586 * need to check vmstate, device state etc. */
587 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
590 static void virtio_pci_vmstate_change(void *opaque, bool running)
592 VirtIOPCIProxy *proxy = opaque;
594 if (running) {
595 /* Try to find out if the guest has bus master disabled, but is
596 in ready state. Then we have a buggy guest OS. */
597 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
598 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
599 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
601 virtio_pci_start_ioeventfd(proxy);
602 } else {
603 virtio_pci_stop_ioeventfd(proxy);
607 static const VirtIOBindings virtio_pci_bindings = {
608 .notify = virtio_pci_notify,
609 .save_config = virtio_pci_save_config,
610 .load_config = virtio_pci_load_config,
611 .save_queue = virtio_pci_save_queue,
612 .load_queue = virtio_pci_load_queue,
613 .get_features = virtio_pci_get_features,
614 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
615 .set_host_notifier = virtio_pci_set_host_notifier,
616 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
617 .vmstate_change = virtio_pci_vmstate_change,
620 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
622 uint8_t *config;
623 uint32_t size;
625 proxy->vdev = vdev;
627 config = proxy->pci_dev.config;
629 if (proxy->class_code) {
630 pci_config_set_class(config, proxy->class_code);
632 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
633 pci_get_word(config + PCI_VENDOR_ID));
634 pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
635 config[PCI_INTERRUPT_PIN] = 1;
637 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
638 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
639 &proxy->msix_bar, 1, 0)) {
640 pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
641 &proxy->msix_bar);
642 } else
643 vdev->nvectors = 0;
645 proxy->pci_dev.config_write = virtio_write_config;
647 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
648 if (size & (size-1))
649 size = 1 << qemu_fls(size);
651 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
652 "virtio-pci", size);
653 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
654 &proxy->bar);
656 if (!kvm_has_many_ioeventfds()) {
657 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
660 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
661 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
662 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
663 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
666 static int virtio_blk_init_pci(PCIDevice *pci_dev)
668 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
669 VirtIODevice *vdev;
671 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
672 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
673 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
675 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
676 &proxy->block_serial);
677 if (!vdev) {
678 return -1;
680 vdev->nvectors = proxy->nvectors;
681 virtio_init_pci(proxy, vdev);
682 /* make the actual value visible */
683 proxy->nvectors = vdev->nvectors;
684 return 0;
687 static int virtio_exit_pci(PCIDevice *pci_dev)
689 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
690 int r;
692 memory_region_destroy(&proxy->bar);
693 r = msix_uninit(pci_dev, &proxy->msix_bar);
694 memory_region_destroy(&proxy->msix_bar);
695 return r;
698 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
700 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
702 virtio_pci_stop_ioeventfd(proxy);
703 virtio_blk_exit(proxy->vdev);
704 blockdev_mark_auto_del(proxy->block.bs);
705 return virtio_exit_pci(pci_dev);
708 static int virtio_serial_init_pci(PCIDevice *pci_dev)
710 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
711 VirtIODevice *vdev;
713 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
714 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
715 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
716 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
718 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
719 if (!vdev) {
720 return -1;
722 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
723 ? proxy->serial.max_virtserial_ports + 1
724 : proxy->nvectors;
725 virtio_init_pci(proxy, vdev);
726 proxy->nvectors = vdev->nvectors;
727 return 0;
730 static int virtio_serial_exit_pci(PCIDevice *pci_dev)
732 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
734 virtio_pci_stop_ioeventfd(proxy);
735 virtio_serial_exit(proxy->vdev);
736 return virtio_exit_pci(pci_dev);
739 static int virtio_net_init_pci(PCIDevice *pci_dev)
741 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
742 VirtIODevice *vdev;
744 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
746 vdev->nvectors = proxy->nvectors;
747 virtio_init_pci(proxy, vdev);
749 /* make the actual value visible */
750 proxy->nvectors = vdev->nvectors;
751 return 0;
754 static int virtio_net_exit_pci(PCIDevice *pci_dev)
756 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
758 virtio_pci_stop_ioeventfd(proxy);
759 virtio_net_exit(proxy->vdev);
760 return virtio_exit_pci(pci_dev);
763 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
765 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
766 VirtIODevice *vdev;
768 vdev = virtio_balloon_init(&pci_dev->qdev);
769 if (!vdev) {
770 return -1;
772 virtio_init_pci(proxy, vdev);
773 return 0;
776 static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
778 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
780 virtio_pci_stop_ioeventfd(proxy);
781 virtio_balloon_exit(proxy->vdev);
782 return virtio_exit_pci(pci_dev);
785 static PCIDeviceInfo virtio_info[] = {
787 .qdev.name = "virtio-blk-pci",
788 .qdev.alias = "virtio-blk",
789 .qdev.size = sizeof(VirtIOPCIProxy),
790 .init = virtio_blk_init_pci,
791 .exit = virtio_blk_exit_pci,
792 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
793 .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
794 .revision = VIRTIO_PCI_ABI_VERSION,
795 .class_id = PCI_CLASS_STORAGE_SCSI,
796 .qdev.props = (Property[]) {
797 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
798 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
799 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
800 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
801 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
802 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
803 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
804 DEFINE_PROP_END_OF_LIST(),
806 .qdev.reset = virtio_pci_reset,
808 .qdev.name = "virtio-net-pci",
809 .qdev.alias = "virtio-net",
810 .qdev.size = sizeof(VirtIOPCIProxy),
811 .init = virtio_net_init_pci,
812 .exit = virtio_net_exit_pci,
813 .romfile = "pxe-virtio.rom",
814 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
815 .device_id = PCI_DEVICE_ID_VIRTIO_NET,
816 .revision = VIRTIO_PCI_ABI_VERSION,
817 .class_id = PCI_CLASS_NETWORK_ETHERNET,
818 .qdev.props = (Property[]) {
819 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
820 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
821 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
822 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
823 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
824 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
825 net.txtimer, TX_TIMER_INTERVAL),
826 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
827 net.txburst, TX_BURST),
828 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
829 DEFINE_PROP_END_OF_LIST(),
831 .qdev.reset = virtio_pci_reset,
833 .qdev.name = "virtio-serial-pci",
834 .qdev.alias = "virtio-serial",
835 .qdev.size = sizeof(VirtIOPCIProxy),
836 .init = virtio_serial_init_pci,
837 .exit = virtio_serial_exit_pci,
838 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
839 .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
840 .revision = VIRTIO_PCI_ABI_VERSION,
841 .class_id = PCI_CLASS_COMMUNICATION_OTHER,
842 .qdev.props = (Property[]) {
843 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
844 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
845 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
846 DEV_NVECTORS_UNSPECIFIED),
847 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
848 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
849 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
850 serial.max_virtserial_ports, 31),
851 DEFINE_PROP_END_OF_LIST(),
853 .qdev.reset = virtio_pci_reset,
855 .qdev.name = "virtio-balloon-pci",
856 .qdev.alias = "virtio-balloon",
857 .qdev.size = sizeof(VirtIOPCIProxy),
858 .init = virtio_balloon_init_pci,
859 .exit = virtio_balloon_exit_pci,
860 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
861 .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
862 .revision = VIRTIO_PCI_ABI_VERSION,
863 .class_id = PCI_CLASS_MEMORY_RAM,
864 .qdev.props = (Property[]) {
865 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
866 DEFINE_PROP_END_OF_LIST(),
868 .qdev.reset = virtio_pci_reset,
870 /* end of list */
874 static void virtio_pci_register_devices(void)
876 pci_qdev_register_many(virtio_info);
879 device_init(virtio_pci_register_devices)