virtio: rename the bar index field name in VirtIOPCIProxy
[qemu.git] / hw / virtio / virtio-pci.c
blob06831de5ff80f874ee2dd26094ff4347ba332f62
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 "qemu/osdep.h"
20 #include "standard-headers/linux/virtio_pci.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-blk.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "hw/virtio/virtio-serial.h"
25 #include "hw/virtio/virtio-scsi.h"
26 #include "hw/virtio/virtio-balloon.h"
27 #include "hw/virtio/virtio-input.h"
28 #include "hw/pci/pci.h"
29 #include "qapi/error.h"
30 #include "qemu/error-report.h"
31 #include "hw/pci/msi.h"
32 #include "hw/pci/msix.h"
33 #include "hw/loader.h"
34 #include "sysemu/kvm.h"
35 #include "sysemu/block-backend.h"
36 #include "virtio-pci.h"
37 #include "qemu/range.h"
38 #include "hw/virtio/virtio-bus.h"
39 #include "qapi/visitor.h"
41 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
43 #undef VIRTIO_PCI_CONFIG
45 /* The remaining space is defined by each driver as the per-driver
46 * configuration space */
47 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
49 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
50 VirtIOPCIProxy *dev);
51 static void virtio_pci_reset(DeviceState *qdev);
53 /* virtio device */
54 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
55 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
57 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
60 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
61 * be careful and test performance if you change this.
63 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
65 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
68 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
70 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
72 if (msix_enabled(&proxy->pci_dev))
73 msix_notify(&proxy->pci_dev, vector);
74 else {
75 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
76 pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
80 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
82 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
83 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
85 pci_device_save(&proxy->pci_dev, f);
86 msix_save(&proxy->pci_dev, f);
87 if (msix_present(&proxy->pci_dev))
88 qemu_put_be16(f, vdev->config_vector);
91 static void virtio_pci_load_modern_queue_state(VirtIOPCIQueue *vq,
92 QEMUFile *f)
94 vq->num = qemu_get_be16(f);
95 vq->enabled = qemu_get_be16(f);
96 vq->desc[0] = qemu_get_be32(f);
97 vq->desc[1] = qemu_get_be32(f);
98 vq->avail[0] = qemu_get_be32(f);
99 vq->avail[1] = qemu_get_be32(f);
100 vq->used[0] = qemu_get_be32(f);
101 vq->used[1] = qemu_get_be32(f);
104 static bool virtio_pci_has_extra_state(DeviceState *d)
106 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
108 return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
111 static int get_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
113 VirtIOPCIProxy *proxy = pv;
114 int i;
116 proxy->dfselect = qemu_get_be32(f);
117 proxy->gfselect = qemu_get_be32(f);
118 proxy->guest_features[0] = qemu_get_be32(f);
119 proxy->guest_features[1] = qemu_get_be32(f);
120 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
121 virtio_pci_load_modern_queue_state(&proxy->vqs[i], f);
124 return 0;
127 static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue *vq,
128 QEMUFile *f)
130 qemu_put_be16(f, vq->num);
131 qemu_put_be16(f, vq->enabled);
132 qemu_put_be32(f, vq->desc[0]);
133 qemu_put_be32(f, vq->desc[1]);
134 qemu_put_be32(f, vq->avail[0]);
135 qemu_put_be32(f, vq->avail[1]);
136 qemu_put_be32(f, vq->used[0]);
137 qemu_put_be32(f, vq->used[1]);
140 static void put_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
142 VirtIOPCIProxy *proxy = pv;
143 int i;
145 qemu_put_be32(f, proxy->dfselect);
146 qemu_put_be32(f, proxy->gfselect);
147 qemu_put_be32(f, proxy->guest_features[0]);
148 qemu_put_be32(f, proxy->guest_features[1]);
149 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
150 virtio_pci_save_modern_queue_state(&proxy->vqs[i], f);
154 static const VMStateInfo vmstate_info_virtio_pci_modern_state = {
155 .name = "virtqueue_state",
156 .get = get_virtio_pci_modern_state,
157 .put = put_virtio_pci_modern_state,
160 static bool virtio_pci_modern_state_needed(void *opaque)
162 VirtIOPCIProxy *proxy = opaque;
164 return virtio_pci_modern(proxy);
167 static const VMStateDescription vmstate_virtio_pci_modern_state = {
168 .name = "virtio_pci/modern_state",
169 .version_id = 1,
170 .minimum_version_id = 1,
171 .needed = &virtio_pci_modern_state_needed,
172 .fields = (VMStateField[]) {
174 .name = "modern_state",
175 .version_id = 0,
176 .field_exists = NULL,
177 .size = 0,
178 .info = &vmstate_info_virtio_pci_modern_state,
179 .flags = VMS_SINGLE,
180 .offset = 0,
182 VMSTATE_END_OF_LIST()
186 static const VMStateDescription vmstate_virtio_pci = {
187 .name = "virtio_pci",
188 .version_id = 1,
189 .minimum_version_id = 1,
190 .minimum_version_id_old = 1,
191 .fields = (VMStateField[]) {
192 VMSTATE_END_OF_LIST()
194 .subsections = (const VMStateDescription*[]) {
195 &vmstate_virtio_pci_modern_state,
196 NULL
200 static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
202 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
204 vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
207 static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
209 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
211 return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
214 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
216 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
217 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
219 if (msix_present(&proxy->pci_dev))
220 qemu_put_be16(f, virtio_queue_vector(vdev, n));
223 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
225 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
226 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
228 int ret;
229 ret = pci_device_load(&proxy->pci_dev, f);
230 if (ret) {
231 return ret;
233 msix_unuse_all_vectors(&proxy->pci_dev);
234 msix_load(&proxy->pci_dev, f);
235 if (msix_present(&proxy->pci_dev)) {
236 qemu_get_be16s(f, &vdev->config_vector);
237 } else {
238 vdev->config_vector = VIRTIO_NO_VECTOR;
240 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
241 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
243 return 0;
246 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
248 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
249 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
251 uint16_t vector;
252 if (msix_present(&proxy->pci_dev)) {
253 qemu_get_be16s(f, &vector);
254 } else {
255 vector = VIRTIO_NO_VECTOR;
257 virtio_queue_set_vector(vdev, n, vector);
258 if (vector != VIRTIO_NO_VECTOR) {
259 return msix_vector_use(&proxy->pci_dev, vector);
262 return 0;
265 static bool virtio_pci_ioeventfd_started(DeviceState *d)
267 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
269 return proxy->ioeventfd_started;
272 static void virtio_pci_ioeventfd_set_started(DeviceState *d, bool started,
273 bool err)
275 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
277 proxy->ioeventfd_started = started;
280 static bool virtio_pci_ioeventfd_disabled(DeviceState *d)
282 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
284 return proxy->ioeventfd_disabled ||
285 !(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD);
288 static void virtio_pci_ioeventfd_set_disabled(DeviceState *d, bool disabled)
290 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
292 proxy->ioeventfd_disabled = disabled;
295 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
297 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy)
299 return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ?
300 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4;
303 static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
304 int n, bool assign)
306 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
307 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
308 VirtQueue *vq = virtio_get_queue(vdev, n);
309 bool legacy = virtio_pci_legacy(proxy);
310 bool modern = virtio_pci_modern(proxy);
311 bool fast_mmio = kvm_ioeventfd_any_length_enabled();
312 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
313 MemoryRegion *modern_mr = &proxy->notify.mr;
314 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
315 MemoryRegion *legacy_mr = &proxy->bar;
316 hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) *
317 virtio_get_queue_index(vq);
318 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
320 if (assign) {
321 if (modern) {
322 if (fast_mmio) {
323 memory_region_add_eventfd(modern_mr, modern_addr, 0,
324 false, n, notifier);
325 } else {
326 memory_region_add_eventfd(modern_mr, modern_addr, 2,
327 false, n, notifier);
329 if (modern_pio) {
330 memory_region_add_eventfd(modern_notify_mr, 0, 2,
331 true, n, notifier);
334 if (legacy) {
335 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
336 true, n, notifier);
338 } else {
339 if (modern) {
340 if (fast_mmio) {
341 memory_region_del_eventfd(modern_mr, modern_addr, 0,
342 false, n, notifier);
343 } else {
344 memory_region_del_eventfd(modern_mr, modern_addr, 2,
345 false, n, notifier);
347 if (modern_pio) {
348 memory_region_del_eventfd(modern_notify_mr, 0, 2,
349 true, n, notifier);
352 if (legacy) {
353 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
354 true, n, notifier);
357 return 0;
360 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
362 virtio_bus_start_ioeventfd(&proxy->bus);
365 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
367 virtio_bus_stop_ioeventfd(&proxy->bus);
370 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
372 VirtIOPCIProxy *proxy = opaque;
373 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
374 hwaddr pa;
376 switch (addr) {
377 case VIRTIO_PCI_GUEST_FEATURES:
378 /* Guest does not negotiate properly? We have to assume nothing. */
379 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
380 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
382 virtio_set_features(vdev, val);
383 break;
384 case VIRTIO_PCI_QUEUE_PFN:
385 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
386 if (pa == 0) {
387 virtio_pci_reset(DEVICE(proxy));
389 else
390 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
391 break;
392 case VIRTIO_PCI_QUEUE_SEL:
393 if (val < VIRTIO_QUEUE_MAX)
394 vdev->queue_sel = val;
395 break;
396 case VIRTIO_PCI_QUEUE_NOTIFY:
397 if (val < VIRTIO_QUEUE_MAX) {
398 virtio_queue_notify(vdev, val);
400 break;
401 case VIRTIO_PCI_STATUS:
402 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
403 virtio_pci_stop_ioeventfd(proxy);
406 virtio_set_status(vdev, val & 0xFF);
408 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
409 virtio_pci_start_ioeventfd(proxy);
412 if (vdev->status == 0) {
413 virtio_pci_reset(DEVICE(proxy));
416 /* Linux before 2.6.34 drives the device without enabling
417 the PCI device bus master bit. Enable it automatically
418 for the guest. This is a PCI spec violation but so is
419 initiating DMA with bus master bit clear. */
420 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
421 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
422 proxy->pci_dev.config[PCI_COMMAND] |
423 PCI_COMMAND_MASTER, 1);
425 break;
426 case VIRTIO_MSI_CONFIG_VECTOR:
427 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
428 /* Make it possible for guest to discover an error took place. */
429 if (msix_vector_use(&proxy->pci_dev, val) < 0)
430 val = VIRTIO_NO_VECTOR;
431 vdev->config_vector = val;
432 break;
433 case VIRTIO_MSI_QUEUE_VECTOR:
434 msix_vector_unuse(&proxy->pci_dev,
435 virtio_queue_vector(vdev, vdev->queue_sel));
436 /* Make it possible for guest to discover an error took place. */
437 if (msix_vector_use(&proxy->pci_dev, val) < 0)
438 val = VIRTIO_NO_VECTOR;
439 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
440 break;
441 default:
442 error_report("%s: unexpected address 0x%x value 0x%x",
443 __func__, addr, val);
444 break;
448 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
450 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
451 uint32_t ret = 0xFFFFFFFF;
453 switch (addr) {
454 case VIRTIO_PCI_HOST_FEATURES:
455 ret = vdev->host_features;
456 break;
457 case VIRTIO_PCI_GUEST_FEATURES:
458 ret = vdev->guest_features;
459 break;
460 case VIRTIO_PCI_QUEUE_PFN:
461 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
462 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
463 break;
464 case VIRTIO_PCI_QUEUE_NUM:
465 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
466 break;
467 case VIRTIO_PCI_QUEUE_SEL:
468 ret = vdev->queue_sel;
469 break;
470 case VIRTIO_PCI_STATUS:
471 ret = vdev->status;
472 break;
473 case VIRTIO_PCI_ISR:
474 /* reading from the ISR also clears it. */
475 ret = vdev->isr;
476 vdev->isr = 0;
477 pci_irq_deassert(&proxy->pci_dev);
478 break;
479 case VIRTIO_MSI_CONFIG_VECTOR:
480 ret = vdev->config_vector;
481 break;
482 case VIRTIO_MSI_QUEUE_VECTOR:
483 ret = virtio_queue_vector(vdev, vdev->queue_sel);
484 break;
485 default:
486 break;
489 return ret;
492 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
493 unsigned size)
495 VirtIOPCIProxy *proxy = opaque;
496 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
497 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
498 uint64_t val = 0;
499 if (addr < config) {
500 return virtio_ioport_read(proxy, addr);
502 addr -= config;
504 switch (size) {
505 case 1:
506 val = virtio_config_readb(vdev, addr);
507 break;
508 case 2:
509 val = virtio_config_readw(vdev, addr);
510 if (virtio_is_big_endian(vdev)) {
511 val = bswap16(val);
513 break;
514 case 4:
515 val = virtio_config_readl(vdev, addr);
516 if (virtio_is_big_endian(vdev)) {
517 val = bswap32(val);
519 break;
521 return val;
524 static void virtio_pci_config_write(void *opaque, hwaddr addr,
525 uint64_t val, unsigned size)
527 VirtIOPCIProxy *proxy = opaque;
528 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
529 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
530 if (addr < config) {
531 virtio_ioport_write(proxy, addr, val);
532 return;
534 addr -= config;
536 * Virtio-PCI is odd. Ioports are LE but config space is target native
537 * endian.
539 switch (size) {
540 case 1:
541 virtio_config_writeb(vdev, addr, val);
542 break;
543 case 2:
544 if (virtio_is_big_endian(vdev)) {
545 val = bswap16(val);
547 virtio_config_writew(vdev, addr, val);
548 break;
549 case 4:
550 if (virtio_is_big_endian(vdev)) {
551 val = bswap32(val);
553 virtio_config_writel(vdev, addr, val);
554 break;
558 static const MemoryRegionOps virtio_pci_config_ops = {
559 .read = virtio_pci_config_read,
560 .write = virtio_pci_config_write,
561 .impl = {
562 .min_access_size = 1,
563 .max_access_size = 4,
565 .endianness = DEVICE_LITTLE_ENDIAN,
568 /* Below are generic functions to do memcpy from/to an address space,
569 * without byteswaps, with input validation.
571 * As regular address_space_* APIs all do some kind of byteswap at least for
572 * some host/target combinations, we are forced to explicitly convert to a
573 * known-endianness integer value.
574 * It doesn't really matter which endian format to go through, so the code
575 * below selects the endian that causes the least amount of work on the given
576 * host.
578 * Note: host pointer must be aligned.
580 static
581 void virtio_address_space_write(AddressSpace *as, hwaddr addr,
582 const uint8_t *buf, int len)
584 uint32_t val;
586 /* address_space_* APIs assume an aligned address.
587 * As address is under guest control, handle illegal values.
589 addr &= ~(len - 1);
591 /* Make sure caller aligned buf properly */
592 assert(!(((uintptr_t)buf) & (len - 1)));
594 switch (len) {
595 case 1:
596 val = pci_get_byte(buf);
597 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
598 break;
599 case 2:
600 val = pci_get_word(buf);
601 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
602 break;
603 case 4:
604 val = pci_get_long(buf);
605 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
606 break;
607 default:
608 /* As length is under guest control, handle illegal values. */
609 break;
613 static void
614 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
616 uint32_t val;
618 /* address_space_* APIs assume an aligned address.
619 * As address is under guest control, handle illegal values.
621 addr &= ~(len - 1);
623 /* Make sure caller aligned buf properly */
624 assert(!(((uintptr_t)buf) & (len - 1)));
626 switch (len) {
627 case 1:
628 val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
629 pci_set_byte(buf, val);
630 break;
631 case 2:
632 val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
633 pci_set_word(buf, val);
634 break;
635 case 4:
636 val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
637 pci_set_long(buf, val);
638 break;
639 default:
640 /* As length is under guest control, handle illegal values. */
641 break;
645 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
646 uint32_t val, int len)
648 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
649 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
650 struct virtio_pci_cfg_cap *cfg;
652 pci_default_write_config(pci_dev, address, val, len);
654 if (range_covers_byte(address, len, PCI_COMMAND) &&
655 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
656 virtio_pci_stop_ioeventfd(proxy);
657 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
660 if (proxy->config_cap &&
661 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
662 pci_cfg_data),
663 sizeof cfg->pci_cfg_data)) {
664 uint32_t off;
665 uint32_t len;
667 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
668 off = le32_to_cpu(cfg->cap.offset);
669 len = le32_to_cpu(cfg->cap.length);
671 if (len == 1 || len == 2 || len == 4) {
672 assert(len <= sizeof cfg->pci_cfg_data);
673 virtio_address_space_write(&proxy->modern_as, off,
674 cfg->pci_cfg_data, len);
679 static uint32_t virtio_read_config(PCIDevice *pci_dev,
680 uint32_t address, int len)
682 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
683 struct virtio_pci_cfg_cap *cfg;
685 if (proxy->config_cap &&
686 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
687 pci_cfg_data),
688 sizeof cfg->pci_cfg_data)) {
689 uint32_t off;
690 uint32_t len;
692 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
693 off = le32_to_cpu(cfg->cap.offset);
694 len = le32_to_cpu(cfg->cap.length);
696 if (len == 1 || len == 2 || len == 4) {
697 assert(len <= sizeof cfg->pci_cfg_data);
698 virtio_address_space_read(&proxy->modern_as, off,
699 cfg->pci_cfg_data, len);
703 return pci_default_read_config(pci_dev, address, len);
706 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
707 unsigned int queue_no,
708 unsigned int vector)
710 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
711 int ret;
713 if (irqfd->users == 0) {
714 ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev);
715 if (ret < 0) {
716 return ret;
718 irqfd->virq = ret;
720 irqfd->users++;
721 return 0;
724 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
725 unsigned int vector)
727 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
728 if (--irqfd->users == 0) {
729 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
733 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
734 unsigned int queue_no,
735 unsigned int vector)
737 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
738 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
739 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
740 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
741 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
744 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
745 unsigned int queue_no,
746 unsigned int vector)
748 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
749 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
750 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
751 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
752 int ret;
754 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
755 assert(ret == 0);
758 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
760 PCIDevice *dev = &proxy->pci_dev;
761 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
762 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
763 unsigned int vector;
764 int ret, queue_no;
766 for (queue_no = 0; queue_no < nvqs; queue_no++) {
767 if (!virtio_queue_get_num(vdev, queue_no)) {
768 break;
770 vector = virtio_queue_vector(vdev, queue_no);
771 if (vector >= msix_nr_vectors_allocated(dev)) {
772 continue;
774 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
775 if (ret < 0) {
776 goto undo;
778 /* If guest supports masking, set up irqfd now.
779 * Otherwise, delay until unmasked in the frontend.
781 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
782 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
783 if (ret < 0) {
784 kvm_virtio_pci_vq_vector_release(proxy, vector);
785 goto undo;
789 return 0;
791 undo:
792 while (--queue_no >= 0) {
793 vector = virtio_queue_vector(vdev, queue_no);
794 if (vector >= msix_nr_vectors_allocated(dev)) {
795 continue;
797 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
798 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
800 kvm_virtio_pci_vq_vector_release(proxy, vector);
802 return ret;
805 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
807 PCIDevice *dev = &proxy->pci_dev;
808 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
809 unsigned int vector;
810 int queue_no;
811 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
813 for (queue_no = 0; queue_no < nvqs; queue_no++) {
814 if (!virtio_queue_get_num(vdev, queue_no)) {
815 break;
817 vector = virtio_queue_vector(vdev, queue_no);
818 if (vector >= msix_nr_vectors_allocated(dev)) {
819 continue;
821 /* If guest supports masking, clean up irqfd now.
822 * Otherwise, it was cleaned when masked in the frontend.
824 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
825 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
827 kvm_virtio_pci_vq_vector_release(proxy, vector);
831 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
832 unsigned int queue_no,
833 unsigned int vector,
834 MSIMessage msg)
836 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
837 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
838 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
839 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
840 VirtIOIRQFD *irqfd;
841 int ret = 0;
843 if (proxy->vector_irqfd) {
844 irqfd = &proxy->vector_irqfd[vector];
845 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
846 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
847 &proxy->pci_dev);
848 if (ret < 0) {
849 return ret;
851 kvm_irqchip_commit_routes(kvm_state);
855 /* If guest supports masking, irqfd is already setup, unmask it.
856 * Otherwise, set it up now.
858 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
859 k->guest_notifier_mask(vdev, queue_no, false);
860 /* Test after unmasking to avoid losing events. */
861 if (k->guest_notifier_pending &&
862 k->guest_notifier_pending(vdev, queue_no)) {
863 event_notifier_set(n);
865 } else {
866 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
868 return ret;
871 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
872 unsigned int queue_no,
873 unsigned int vector)
875 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
876 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
878 /* If guest supports masking, keep irqfd but mask it.
879 * Otherwise, clean it up now.
881 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
882 k->guest_notifier_mask(vdev, queue_no, true);
883 } else {
884 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
888 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
889 MSIMessage msg)
891 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
892 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
893 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
894 int ret, index, unmasked = 0;
896 while (vq) {
897 index = virtio_get_queue_index(vq);
898 if (!virtio_queue_get_num(vdev, index)) {
899 break;
901 if (index < proxy->nvqs_with_notifiers) {
902 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
903 if (ret < 0) {
904 goto undo;
906 ++unmasked;
908 vq = virtio_vector_next_queue(vq);
911 return 0;
913 undo:
914 vq = virtio_vector_first_queue(vdev, vector);
915 while (vq && unmasked >= 0) {
916 index = virtio_get_queue_index(vq);
917 if (index < proxy->nvqs_with_notifiers) {
918 virtio_pci_vq_vector_mask(proxy, index, vector);
919 --unmasked;
921 vq = virtio_vector_next_queue(vq);
923 return ret;
926 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
928 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
929 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
930 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
931 int index;
933 while (vq) {
934 index = virtio_get_queue_index(vq);
935 if (!virtio_queue_get_num(vdev, index)) {
936 break;
938 if (index < proxy->nvqs_with_notifiers) {
939 virtio_pci_vq_vector_mask(proxy, index, vector);
941 vq = virtio_vector_next_queue(vq);
945 static void virtio_pci_vector_poll(PCIDevice *dev,
946 unsigned int vector_start,
947 unsigned int vector_end)
949 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
950 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
951 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
952 int queue_no;
953 unsigned int vector;
954 EventNotifier *notifier;
955 VirtQueue *vq;
957 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
958 if (!virtio_queue_get_num(vdev, queue_no)) {
959 break;
961 vector = virtio_queue_vector(vdev, queue_no);
962 if (vector < vector_start || vector >= vector_end ||
963 !msix_is_masked(dev, vector)) {
964 continue;
966 vq = virtio_get_queue(vdev, queue_no);
967 notifier = virtio_queue_get_guest_notifier(vq);
968 if (k->guest_notifier_pending) {
969 if (k->guest_notifier_pending(vdev, queue_no)) {
970 msix_set_pending(dev, vector);
972 } else if (event_notifier_test_and_clear(notifier)) {
973 msix_set_pending(dev, vector);
978 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
979 bool with_irqfd)
981 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
982 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
983 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
984 VirtQueue *vq = virtio_get_queue(vdev, n);
985 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
987 if (assign) {
988 int r = event_notifier_init(notifier, 0);
989 if (r < 0) {
990 return r;
992 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
993 } else {
994 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
995 event_notifier_cleanup(notifier);
998 if (!msix_enabled(&proxy->pci_dev) &&
999 vdev->use_guest_notifier_mask &&
1000 vdc->guest_notifier_mask) {
1001 vdc->guest_notifier_mask(vdev, n, !assign);
1004 return 0;
1007 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
1009 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1010 return msix_enabled(&proxy->pci_dev);
1013 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
1015 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1016 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1017 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1018 int r, n;
1019 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
1020 kvm_msi_via_irqfd_enabled();
1022 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1024 /* When deassigning, pass a consistent nvqs value
1025 * to avoid leaking notifiers.
1027 assert(assign || nvqs == proxy->nvqs_with_notifiers);
1029 proxy->nvqs_with_notifiers = nvqs;
1031 /* Must unset vector notifier while guest notifier is still assigned */
1032 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1033 msix_unset_vector_notifiers(&proxy->pci_dev);
1034 if (proxy->vector_irqfd) {
1035 kvm_virtio_pci_vector_release(proxy, nvqs);
1036 g_free(proxy->vector_irqfd);
1037 proxy->vector_irqfd = NULL;
1041 for (n = 0; n < nvqs; n++) {
1042 if (!virtio_queue_get_num(vdev, n)) {
1043 break;
1046 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1047 if (r < 0) {
1048 goto assign_error;
1052 /* Must set vector notifier after guest notifier has been assigned */
1053 if ((with_irqfd || k->guest_notifier_mask) && assign) {
1054 if (with_irqfd) {
1055 proxy->vector_irqfd =
1056 g_malloc0(sizeof(*proxy->vector_irqfd) *
1057 msix_nr_vectors_allocated(&proxy->pci_dev));
1058 r = kvm_virtio_pci_vector_use(proxy, nvqs);
1059 if (r < 0) {
1060 goto assign_error;
1063 r = msix_set_vector_notifiers(&proxy->pci_dev,
1064 virtio_pci_vector_unmask,
1065 virtio_pci_vector_mask,
1066 virtio_pci_vector_poll);
1067 if (r < 0) {
1068 goto notifiers_error;
1072 return 0;
1074 notifiers_error:
1075 if (with_irqfd) {
1076 assert(assign);
1077 kvm_virtio_pci_vector_release(proxy, nvqs);
1080 assign_error:
1081 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1082 assert(assign);
1083 while (--n >= 0) {
1084 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1086 return r;
1089 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1091 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1092 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1094 if (running) {
1095 /* Old QEMU versions did not set bus master enable on status write.
1096 * Detect DRIVER set and enable it.
1098 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
1099 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
1100 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1101 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
1102 proxy->pci_dev.config[PCI_COMMAND] |
1103 PCI_COMMAND_MASTER, 1);
1105 virtio_pci_start_ioeventfd(proxy);
1106 } else {
1107 virtio_pci_stop_ioeventfd(proxy);
1111 #ifdef CONFIG_VIRTFS
1112 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1114 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1115 DeviceState *vdev = DEVICE(&dev->vdev);
1117 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1118 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1121 static Property virtio_9p_pci_properties[] = {
1122 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1123 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1124 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1125 DEFINE_PROP_END_OF_LIST(),
1128 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1130 DeviceClass *dc = DEVICE_CLASS(klass);
1131 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1132 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1134 k->realize = virtio_9p_pci_realize;
1135 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1136 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1137 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1138 pcidev_k->class_id = 0x2;
1139 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1140 dc->props = virtio_9p_pci_properties;
1143 static void virtio_9p_pci_instance_init(Object *obj)
1145 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1147 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1148 TYPE_VIRTIO_9P);
1151 static const TypeInfo virtio_9p_pci_info = {
1152 .name = TYPE_VIRTIO_9P_PCI,
1153 .parent = TYPE_VIRTIO_PCI,
1154 .instance_size = sizeof(V9fsPCIState),
1155 .instance_init = virtio_9p_pci_instance_init,
1156 .class_init = virtio_9p_pci_class_init,
1158 #endif /* CONFIG_VIRTFS */
1161 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1164 static int virtio_pci_query_nvectors(DeviceState *d)
1166 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1168 return proxy->nvectors;
1171 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1172 struct virtio_pci_cap *cap)
1174 PCIDevice *dev = &proxy->pci_dev;
1175 int offset;
1177 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1178 assert(offset > 0);
1180 assert(cap->cap_len >= sizeof *cap);
1181 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1182 cap->cap_len - PCI_CAP_FLAGS);
1184 return offset;
1187 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1188 unsigned size)
1190 VirtIOPCIProxy *proxy = opaque;
1191 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1192 uint32_t val = 0;
1193 int i;
1195 switch (addr) {
1196 case VIRTIO_PCI_COMMON_DFSELECT:
1197 val = proxy->dfselect;
1198 break;
1199 case VIRTIO_PCI_COMMON_DF:
1200 if (proxy->dfselect <= 1) {
1201 val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >>
1202 (32 * proxy->dfselect);
1204 break;
1205 case VIRTIO_PCI_COMMON_GFSELECT:
1206 val = proxy->gfselect;
1207 break;
1208 case VIRTIO_PCI_COMMON_GF:
1209 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1210 val = proxy->guest_features[proxy->gfselect];
1212 break;
1213 case VIRTIO_PCI_COMMON_MSIX:
1214 val = vdev->config_vector;
1215 break;
1216 case VIRTIO_PCI_COMMON_NUMQ:
1217 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1218 if (virtio_queue_get_num(vdev, i)) {
1219 val = i + 1;
1222 break;
1223 case VIRTIO_PCI_COMMON_STATUS:
1224 val = vdev->status;
1225 break;
1226 case VIRTIO_PCI_COMMON_CFGGENERATION:
1227 val = vdev->generation;
1228 break;
1229 case VIRTIO_PCI_COMMON_Q_SELECT:
1230 val = vdev->queue_sel;
1231 break;
1232 case VIRTIO_PCI_COMMON_Q_SIZE:
1233 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1234 break;
1235 case VIRTIO_PCI_COMMON_Q_MSIX:
1236 val = virtio_queue_vector(vdev, vdev->queue_sel);
1237 break;
1238 case VIRTIO_PCI_COMMON_Q_ENABLE:
1239 val = proxy->vqs[vdev->queue_sel].enabled;
1240 break;
1241 case VIRTIO_PCI_COMMON_Q_NOFF:
1242 /* Simply map queues in order */
1243 val = vdev->queue_sel;
1244 break;
1245 case VIRTIO_PCI_COMMON_Q_DESCLO:
1246 val = proxy->vqs[vdev->queue_sel].desc[0];
1247 break;
1248 case VIRTIO_PCI_COMMON_Q_DESCHI:
1249 val = proxy->vqs[vdev->queue_sel].desc[1];
1250 break;
1251 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1252 val = proxy->vqs[vdev->queue_sel].avail[0];
1253 break;
1254 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1255 val = proxy->vqs[vdev->queue_sel].avail[1];
1256 break;
1257 case VIRTIO_PCI_COMMON_Q_USEDLO:
1258 val = proxy->vqs[vdev->queue_sel].used[0];
1259 break;
1260 case VIRTIO_PCI_COMMON_Q_USEDHI:
1261 val = proxy->vqs[vdev->queue_sel].used[1];
1262 break;
1263 default:
1264 val = 0;
1267 return val;
1270 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1271 uint64_t val, unsigned size)
1273 VirtIOPCIProxy *proxy = opaque;
1274 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1276 switch (addr) {
1277 case VIRTIO_PCI_COMMON_DFSELECT:
1278 proxy->dfselect = val;
1279 break;
1280 case VIRTIO_PCI_COMMON_GFSELECT:
1281 proxy->gfselect = val;
1282 break;
1283 case VIRTIO_PCI_COMMON_GF:
1284 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1285 proxy->guest_features[proxy->gfselect] = val;
1286 virtio_set_features(vdev,
1287 (((uint64_t)proxy->guest_features[1]) << 32) |
1288 proxy->guest_features[0]);
1290 break;
1291 case VIRTIO_PCI_COMMON_MSIX:
1292 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1293 /* Make it possible for guest to discover an error took place. */
1294 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1295 val = VIRTIO_NO_VECTOR;
1297 vdev->config_vector = val;
1298 break;
1299 case VIRTIO_PCI_COMMON_STATUS:
1300 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1301 virtio_pci_stop_ioeventfd(proxy);
1304 virtio_set_status(vdev, val & 0xFF);
1306 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1307 virtio_pci_start_ioeventfd(proxy);
1310 if (vdev->status == 0) {
1311 virtio_pci_reset(DEVICE(proxy));
1314 break;
1315 case VIRTIO_PCI_COMMON_Q_SELECT:
1316 if (val < VIRTIO_QUEUE_MAX) {
1317 vdev->queue_sel = val;
1319 break;
1320 case VIRTIO_PCI_COMMON_Q_SIZE:
1321 proxy->vqs[vdev->queue_sel].num = val;
1322 break;
1323 case VIRTIO_PCI_COMMON_Q_MSIX:
1324 msix_vector_unuse(&proxy->pci_dev,
1325 virtio_queue_vector(vdev, vdev->queue_sel));
1326 /* Make it possible for guest to discover an error took place. */
1327 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1328 val = VIRTIO_NO_VECTOR;
1330 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1331 break;
1332 case VIRTIO_PCI_COMMON_Q_ENABLE:
1333 /* TODO: need a way to put num back on reset. */
1334 virtio_queue_set_num(vdev, vdev->queue_sel,
1335 proxy->vqs[vdev->queue_sel].num);
1336 virtio_queue_set_rings(vdev, vdev->queue_sel,
1337 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1338 proxy->vqs[vdev->queue_sel].desc[0],
1339 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1340 proxy->vqs[vdev->queue_sel].avail[0],
1341 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1342 proxy->vqs[vdev->queue_sel].used[0]);
1343 proxy->vqs[vdev->queue_sel].enabled = 1;
1344 break;
1345 case VIRTIO_PCI_COMMON_Q_DESCLO:
1346 proxy->vqs[vdev->queue_sel].desc[0] = val;
1347 break;
1348 case VIRTIO_PCI_COMMON_Q_DESCHI:
1349 proxy->vqs[vdev->queue_sel].desc[1] = val;
1350 break;
1351 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1352 proxy->vqs[vdev->queue_sel].avail[0] = val;
1353 break;
1354 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1355 proxy->vqs[vdev->queue_sel].avail[1] = val;
1356 break;
1357 case VIRTIO_PCI_COMMON_Q_USEDLO:
1358 proxy->vqs[vdev->queue_sel].used[0] = val;
1359 break;
1360 case VIRTIO_PCI_COMMON_Q_USEDHI:
1361 proxy->vqs[vdev->queue_sel].used[1] = val;
1362 break;
1363 default:
1364 break;
1369 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1370 unsigned size)
1372 return 0;
1375 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1376 uint64_t val, unsigned size)
1378 VirtIODevice *vdev = opaque;
1379 VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
1380 unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1382 if (queue < VIRTIO_QUEUE_MAX) {
1383 virtio_queue_notify(vdev, queue);
1387 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
1388 uint64_t val, unsigned size)
1390 VirtIODevice *vdev = opaque;
1391 unsigned queue = val;
1393 if (queue < VIRTIO_QUEUE_MAX) {
1394 virtio_queue_notify(vdev, queue);
1398 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1399 unsigned size)
1401 VirtIOPCIProxy *proxy = opaque;
1402 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1403 uint64_t val = vdev->isr;
1405 vdev->isr = 0;
1406 pci_irq_deassert(&proxy->pci_dev);
1408 return val;
1411 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1412 uint64_t val, unsigned size)
1416 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1417 unsigned size)
1419 VirtIODevice *vdev = opaque;
1420 uint64_t val = 0;
1422 switch (size) {
1423 case 1:
1424 val = virtio_config_modern_readb(vdev, addr);
1425 break;
1426 case 2:
1427 val = virtio_config_modern_readw(vdev, addr);
1428 break;
1429 case 4:
1430 val = virtio_config_modern_readl(vdev, addr);
1431 break;
1433 return val;
1436 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1437 uint64_t val, unsigned size)
1439 VirtIODevice *vdev = opaque;
1440 switch (size) {
1441 case 1:
1442 virtio_config_modern_writeb(vdev, addr, val);
1443 break;
1444 case 2:
1445 virtio_config_modern_writew(vdev, addr, val);
1446 break;
1447 case 4:
1448 virtio_config_modern_writel(vdev, addr, val);
1449 break;
1453 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1455 static const MemoryRegionOps common_ops = {
1456 .read = virtio_pci_common_read,
1457 .write = virtio_pci_common_write,
1458 .impl = {
1459 .min_access_size = 1,
1460 .max_access_size = 4,
1462 .endianness = DEVICE_LITTLE_ENDIAN,
1464 static const MemoryRegionOps isr_ops = {
1465 .read = virtio_pci_isr_read,
1466 .write = virtio_pci_isr_write,
1467 .impl = {
1468 .min_access_size = 1,
1469 .max_access_size = 4,
1471 .endianness = DEVICE_LITTLE_ENDIAN,
1473 static const MemoryRegionOps device_ops = {
1474 .read = virtio_pci_device_read,
1475 .write = virtio_pci_device_write,
1476 .impl = {
1477 .min_access_size = 1,
1478 .max_access_size = 4,
1480 .endianness = DEVICE_LITTLE_ENDIAN,
1482 static const MemoryRegionOps notify_ops = {
1483 .read = virtio_pci_notify_read,
1484 .write = virtio_pci_notify_write,
1485 .impl = {
1486 .min_access_size = 1,
1487 .max_access_size = 4,
1489 .endianness = DEVICE_LITTLE_ENDIAN,
1491 static const MemoryRegionOps notify_pio_ops = {
1492 .read = virtio_pci_notify_read,
1493 .write = virtio_pci_notify_write_pio,
1494 .impl = {
1495 .min_access_size = 1,
1496 .max_access_size = 4,
1498 .endianness = DEVICE_LITTLE_ENDIAN,
1502 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1503 &common_ops,
1504 proxy,
1505 "virtio-pci-common",
1506 proxy->common.size);
1508 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1509 &isr_ops,
1510 proxy,
1511 "virtio-pci-isr",
1512 proxy->isr.size);
1514 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1515 &device_ops,
1516 virtio_bus_get_device(&proxy->bus),
1517 "virtio-pci-device",
1518 proxy->device.size);
1520 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1521 &notify_ops,
1522 virtio_bus_get_device(&proxy->bus),
1523 "virtio-pci-notify",
1524 proxy->notify.size);
1526 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
1527 &notify_pio_ops,
1528 virtio_bus_get_device(&proxy->bus),
1529 "virtio-pci-notify-pio",
1530 proxy->notify_pio.size);
1533 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1534 VirtIOPCIRegion *region,
1535 struct virtio_pci_cap *cap,
1536 MemoryRegion *mr,
1537 uint8_t bar)
1539 memory_region_add_subregion(mr, region->offset, &region->mr);
1541 cap->cfg_type = region->type;
1542 cap->bar = bar;
1543 cap->offset = cpu_to_le32(region->offset);
1544 cap->length = cpu_to_le32(region->size);
1545 virtio_pci_add_mem_cap(proxy, cap);
1549 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
1550 VirtIOPCIRegion *region,
1551 struct virtio_pci_cap *cap)
1553 virtio_pci_modern_region_map(proxy, region, cap,
1554 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1557 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
1558 VirtIOPCIRegion *region,
1559 struct virtio_pci_cap *cap)
1561 virtio_pci_modern_region_map(proxy, region, cap,
1562 &proxy->io_bar, proxy->modern_io_bar_idx);
1565 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
1566 VirtIOPCIRegion *region)
1568 memory_region_del_subregion(&proxy->modern_bar,
1569 &region->mr);
1572 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
1573 VirtIOPCIRegion *region)
1575 memory_region_del_subregion(&proxy->io_bar,
1576 &region->mr);
1579 static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
1581 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1582 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1584 if (virtio_pci_modern(proxy)) {
1585 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1588 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1591 /* This is called by virtio-bus just after the device is plugged. */
1592 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1594 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1595 VirtioBusState *bus = &proxy->bus;
1596 bool legacy = virtio_pci_legacy(proxy);
1597 bool modern;
1598 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1599 uint8_t *config;
1600 uint32_t size;
1601 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1604 * Virtio capabilities present without
1605 * VIRTIO_F_VERSION_1 confuses guests
1607 if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1608 virtio_pci_disable_modern(proxy);
1610 if (!legacy) {
1611 error_setg(errp, "Device doesn't support modern mode, and legacy"
1612 " mode is disabled");
1613 error_append_hint(errp, "Set disable-legacy to off\n");
1615 return;
1619 modern = virtio_pci_modern(proxy);
1621 config = proxy->pci_dev.config;
1622 if (proxy->class_code) {
1623 pci_config_set_class(config, proxy->class_code);
1626 if (legacy) {
1627 /* legacy and transitional */
1628 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1629 pci_get_word(config + PCI_VENDOR_ID));
1630 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1631 } else {
1632 /* pure virtio-1.0 */
1633 pci_set_word(config + PCI_VENDOR_ID,
1634 PCI_VENDOR_ID_REDHAT_QUMRANET);
1635 pci_set_word(config + PCI_DEVICE_ID,
1636 0x1040 + virtio_bus_get_vdev_id(bus));
1637 pci_config_set_revision(config, 1);
1639 config[PCI_INTERRUPT_PIN] = 1;
1642 if (modern) {
1643 struct virtio_pci_cap cap = {
1644 .cap_len = sizeof cap,
1646 struct virtio_pci_notify_cap notify = {
1647 .cap.cap_len = sizeof notify,
1648 .notify_off_multiplier =
1649 cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
1651 struct virtio_pci_cfg_cap cfg = {
1652 .cap.cap_len = sizeof cfg,
1653 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1655 struct virtio_pci_notify_cap notify_pio = {
1656 .cap.cap_len = sizeof notify,
1657 .notify_off_multiplier = cpu_to_le32(0x0),
1660 struct virtio_pci_cfg_cap *cfg_mask;
1662 virtio_pci_modern_regions_init(proxy);
1664 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
1665 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
1666 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
1667 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, &notify.cap);
1669 if (modern_pio) {
1670 memory_region_init(&proxy->io_bar, OBJECT(proxy),
1671 "virtio-pci-io", 0x4);
1673 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1674 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
1676 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
1677 &notify_pio.cap);
1680 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
1681 PCI_BASE_ADDRESS_SPACE_MEMORY |
1682 PCI_BASE_ADDRESS_MEM_PREFETCH |
1683 PCI_BASE_ADDRESS_MEM_TYPE_64,
1684 &proxy->modern_bar);
1686 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1687 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1688 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1689 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1690 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1691 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1694 if (proxy->nvectors) {
1695 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1696 proxy->msix_bar_idx);
1697 if (err) {
1698 /* Notice when a system that supports MSIx can't initialize it. */
1699 if (err != -ENOTSUP) {
1700 error_report("unable to init msix vectors to %" PRIu32,
1701 proxy->nvectors);
1703 proxy->nvectors = 0;
1707 proxy->pci_dev.config_write = virtio_write_config;
1708 proxy->pci_dev.config_read = virtio_read_config;
1710 if (legacy) {
1711 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1712 + virtio_bus_get_vdev_config_len(bus);
1713 size = pow2ceil(size);
1715 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1716 &virtio_pci_config_ops,
1717 proxy, "virtio-pci", size);
1719 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
1720 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1723 if (!kvm_has_many_ioeventfds()) {
1724 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1728 static void virtio_pci_device_unplugged(DeviceState *d)
1730 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1731 bool modern = virtio_pci_modern(proxy);
1732 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1734 virtio_pci_stop_ioeventfd(proxy);
1736 if (modern) {
1737 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
1738 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
1739 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
1740 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
1741 if (modern_pio) {
1742 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
1747 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1749 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1750 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1751 bool pcie_port = pci_bus_is_express(pci_dev->bus) &&
1752 !pci_bus_is_root(pci_dev->bus);
1755 * virtio pci bar layout used by default.
1756 * subclasses can re-arrange things if needed.
1758 * region 0 -- virtio legacy io bar
1759 * region 1 -- msi-x bar
1760 * region 4+5 -- virtio modern memory (64bit) bar
1763 proxy->legacy_io_bar_idx = 0;
1764 proxy->msix_bar_idx = 1;
1765 proxy->modern_io_bar_idx = 2;
1766 proxy->modern_mem_bar_idx = 4;
1768 proxy->common.offset = 0x0;
1769 proxy->common.size = 0x1000;
1770 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1772 proxy->isr.offset = 0x1000;
1773 proxy->isr.size = 0x1000;
1774 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1776 proxy->device.offset = 0x2000;
1777 proxy->device.size = 0x1000;
1778 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1780 proxy->notify.offset = 0x3000;
1781 proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1782 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1784 proxy->notify_pio.offset = 0x0;
1785 proxy->notify_pio.size = 0x4;
1786 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1788 /* subclasses can enforce modern, so do this unconditionally */
1789 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1790 /* PCI BAR regions must be powers of 2 */
1791 pow2ceil(proxy->notify.offset + proxy->notify.size));
1793 memory_region_init_alias(&proxy->modern_cfg,
1794 OBJECT(proxy),
1795 "virtio-pci-cfg",
1796 &proxy->modern_bar,
1798 memory_region_size(&proxy->modern_bar));
1800 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1802 if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
1803 proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
1806 if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1807 error_setg(errp, "device cannot work as neither modern nor legacy mode"
1808 " is enabled");
1809 error_append_hint(errp, "Set either disable-modern or disable-legacy"
1810 " to off\n");
1811 return;
1814 if (pcie_port && pci_is_express(pci_dev)) {
1815 int pos;
1817 pos = pcie_endpoint_cap_init(pci_dev, 0);
1818 assert(pos > 0);
1820 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF);
1821 assert(pos > 0);
1824 * Indicates that this function complies with revision 1.2 of the
1825 * PCI Power Management Interface Specification.
1827 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
1828 } else {
1830 * make future invocations of pci_is_express() return false
1831 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1833 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1836 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1837 if (k->realize) {
1838 k->realize(proxy, errp);
1842 static void virtio_pci_exit(PCIDevice *pci_dev)
1844 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1846 msix_uninit_exclusive_bar(pci_dev);
1847 address_space_destroy(&proxy->modern_as);
1850 static void virtio_pci_reset(DeviceState *qdev)
1852 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1853 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1854 int i;
1856 virtio_pci_stop_ioeventfd(proxy);
1857 virtio_bus_reset(bus);
1858 msix_unuse_all_vectors(&proxy->pci_dev);
1860 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1861 proxy->vqs[i].enabled = 0;
1865 static Property virtio_pci_properties[] = {
1866 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1867 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1868 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
1869 ON_OFF_AUTO_AUTO),
1870 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
1871 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1872 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1873 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
1874 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1875 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
1876 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1877 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
1878 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1879 DEFINE_PROP_END_OF_LIST(),
1882 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
1884 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
1885 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1886 PCIDevice *pci_dev = &proxy->pci_dev;
1888 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
1889 virtio_pci_modern(proxy)) {
1890 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1893 vpciklass->parent_dc_realize(qdev, errp);
1896 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1898 DeviceClass *dc = DEVICE_CLASS(klass);
1899 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1900 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1902 dc->props = virtio_pci_properties;
1903 k->realize = virtio_pci_realize;
1904 k->exit = virtio_pci_exit;
1905 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1906 k->revision = VIRTIO_PCI_ABI_VERSION;
1907 k->class_id = PCI_CLASS_OTHERS;
1908 vpciklass->parent_dc_realize = dc->realize;
1909 dc->realize = virtio_pci_dc_realize;
1910 dc->reset = virtio_pci_reset;
1913 static const TypeInfo virtio_pci_info = {
1914 .name = TYPE_VIRTIO_PCI,
1915 .parent = TYPE_PCI_DEVICE,
1916 .instance_size = sizeof(VirtIOPCIProxy),
1917 .class_init = virtio_pci_class_init,
1918 .class_size = sizeof(VirtioPCIClass),
1919 .abstract = true,
1922 /* virtio-blk-pci */
1924 static Property virtio_blk_pci_properties[] = {
1925 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1926 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1927 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1928 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1929 DEFINE_PROP_END_OF_LIST(),
1932 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1934 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1935 DeviceState *vdev = DEVICE(&dev->vdev);
1937 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1938 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1941 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1943 DeviceClass *dc = DEVICE_CLASS(klass);
1944 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1945 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1947 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1948 dc->props = virtio_blk_pci_properties;
1949 k->realize = virtio_blk_pci_realize;
1950 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1951 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1952 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1953 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1956 static void virtio_blk_pci_instance_init(Object *obj)
1958 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1960 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1961 TYPE_VIRTIO_BLK);
1962 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1963 &error_abort);
1964 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1965 "bootindex", &error_abort);
1968 static const TypeInfo virtio_blk_pci_info = {
1969 .name = TYPE_VIRTIO_BLK_PCI,
1970 .parent = TYPE_VIRTIO_PCI,
1971 .instance_size = sizeof(VirtIOBlkPCI),
1972 .instance_init = virtio_blk_pci_instance_init,
1973 .class_init = virtio_blk_pci_class_init,
1976 /* virtio-scsi-pci */
1978 static Property virtio_scsi_pci_properties[] = {
1979 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1980 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1981 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1982 DEV_NVECTORS_UNSPECIFIED),
1983 DEFINE_PROP_END_OF_LIST(),
1986 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1988 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1989 DeviceState *vdev = DEVICE(&dev->vdev);
1990 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1991 DeviceState *proxy = DEVICE(vpci_dev);
1992 char *bus_name;
1994 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1995 vpci_dev->nvectors = vs->conf.num_queues + 3;
1999 * For command line compatibility, this sets the virtio-scsi-device bus
2000 * name as before.
2002 if (proxy->id) {
2003 bus_name = g_strdup_printf("%s.0", proxy->id);
2004 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2005 g_free(bus_name);
2008 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2009 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2012 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
2014 DeviceClass *dc = DEVICE_CLASS(klass);
2015 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2016 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2018 k->realize = virtio_scsi_pci_realize;
2019 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2020 dc->props = virtio_scsi_pci_properties;
2021 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2022 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2023 pcidev_k->revision = 0x00;
2024 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2027 static void virtio_scsi_pci_instance_init(Object *obj)
2029 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
2031 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2032 TYPE_VIRTIO_SCSI);
2033 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
2034 &error_abort);
2037 static const TypeInfo virtio_scsi_pci_info = {
2038 .name = TYPE_VIRTIO_SCSI_PCI,
2039 .parent = TYPE_VIRTIO_PCI,
2040 .instance_size = sizeof(VirtIOSCSIPCI),
2041 .instance_init = virtio_scsi_pci_instance_init,
2042 .class_init = virtio_scsi_pci_class_init,
2045 /* vhost-scsi-pci */
2047 #ifdef CONFIG_VHOST_SCSI
2048 static Property vhost_scsi_pci_properties[] = {
2049 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2050 DEV_NVECTORS_UNSPECIFIED),
2051 DEFINE_PROP_END_OF_LIST(),
2054 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2056 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
2057 DeviceState *vdev = DEVICE(&dev->vdev);
2058 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2060 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2061 vpci_dev->nvectors = vs->conf.num_queues + 3;
2064 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2065 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2068 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
2070 DeviceClass *dc = DEVICE_CLASS(klass);
2071 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2072 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2073 k->realize = vhost_scsi_pci_realize;
2074 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2075 dc->props = vhost_scsi_pci_properties;
2076 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2077 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2078 pcidev_k->revision = 0x00;
2079 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2082 static void vhost_scsi_pci_instance_init(Object *obj)
2084 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2086 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2087 TYPE_VHOST_SCSI);
2088 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2089 "bootindex", &error_abort);
2092 static const TypeInfo vhost_scsi_pci_info = {
2093 .name = TYPE_VHOST_SCSI_PCI,
2094 .parent = TYPE_VIRTIO_PCI,
2095 .instance_size = sizeof(VHostSCSIPCI),
2096 .instance_init = vhost_scsi_pci_instance_init,
2097 .class_init = vhost_scsi_pci_class_init,
2099 #endif
2101 /* vhost-vsock-pci */
2103 #ifdef CONFIG_VHOST_VSOCK
2104 static Property vhost_vsock_pci_properties[] = {
2105 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2106 DEFINE_PROP_END_OF_LIST(),
2109 static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2111 VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev);
2112 DeviceState *vdev = DEVICE(&dev->vdev);
2114 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2115 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2118 static void vhost_vsock_pci_class_init(ObjectClass *klass, void *data)
2120 DeviceClass *dc = DEVICE_CLASS(klass);
2121 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2122 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2123 k->realize = vhost_vsock_pci_realize;
2124 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2125 dc->props = vhost_vsock_pci_properties;
2126 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2127 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
2128 pcidev_k->revision = 0x00;
2129 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2132 static void vhost_vsock_pci_instance_init(Object *obj)
2134 VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj);
2136 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2137 TYPE_VHOST_VSOCK);
2140 static const TypeInfo vhost_vsock_pci_info = {
2141 .name = TYPE_VHOST_VSOCK_PCI,
2142 .parent = TYPE_VIRTIO_PCI,
2143 .instance_size = sizeof(VHostVSockPCI),
2144 .instance_init = vhost_vsock_pci_instance_init,
2145 .class_init = vhost_vsock_pci_class_init,
2147 #endif
2149 /* virtio-balloon-pci */
2151 static Property virtio_balloon_pci_properties[] = {
2152 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2153 DEFINE_PROP_END_OF_LIST(),
2156 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2158 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
2159 DeviceState *vdev = DEVICE(&dev->vdev);
2161 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
2162 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
2163 vpci_dev->class_code = PCI_CLASS_OTHERS;
2166 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2167 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2170 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
2172 DeviceClass *dc = DEVICE_CLASS(klass);
2173 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2174 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2175 k->realize = virtio_balloon_pci_realize;
2176 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2177 dc->props = virtio_balloon_pci_properties;
2178 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2179 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
2180 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2181 pcidev_k->class_id = PCI_CLASS_OTHERS;
2184 static void virtio_balloon_pci_instance_init(Object *obj)
2186 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2188 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2189 TYPE_VIRTIO_BALLOON);
2190 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
2191 "guest-stats", &error_abort);
2192 object_property_add_alias(obj, "guest-stats-polling-interval",
2193 OBJECT(&dev->vdev),
2194 "guest-stats-polling-interval", &error_abort);
2197 static const TypeInfo virtio_balloon_pci_info = {
2198 .name = TYPE_VIRTIO_BALLOON_PCI,
2199 .parent = TYPE_VIRTIO_PCI,
2200 .instance_size = sizeof(VirtIOBalloonPCI),
2201 .instance_init = virtio_balloon_pci_instance_init,
2202 .class_init = virtio_balloon_pci_class_init,
2205 /* virtio-serial-pci */
2207 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2209 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2210 DeviceState *vdev = DEVICE(&dev->vdev);
2211 DeviceState *proxy = DEVICE(vpci_dev);
2212 char *bus_name;
2214 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2215 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
2216 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
2217 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2220 /* backwards-compatibility with machines that were created with
2221 DEV_NVECTORS_UNSPECIFIED */
2222 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2223 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2227 * For command line compatibility, this sets the virtio-serial-device bus
2228 * name as before.
2230 if (proxy->id) {
2231 bus_name = g_strdup_printf("%s.0", proxy->id);
2232 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2233 g_free(bus_name);
2236 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2237 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2240 static Property virtio_serial_pci_properties[] = {
2241 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2242 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2243 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2244 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2245 DEFINE_PROP_END_OF_LIST(),
2248 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2250 DeviceClass *dc = DEVICE_CLASS(klass);
2251 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2252 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2253 k->realize = virtio_serial_pci_realize;
2254 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2255 dc->props = virtio_serial_pci_properties;
2256 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2257 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2258 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2259 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2262 static void virtio_serial_pci_instance_init(Object *obj)
2264 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2266 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2267 TYPE_VIRTIO_SERIAL);
2270 static const TypeInfo virtio_serial_pci_info = {
2271 .name = TYPE_VIRTIO_SERIAL_PCI,
2272 .parent = TYPE_VIRTIO_PCI,
2273 .instance_size = sizeof(VirtIOSerialPCI),
2274 .instance_init = virtio_serial_pci_instance_init,
2275 .class_init = virtio_serial_pci_class_init,
2278 /* virtio-net-pci */
2280 static Property virtio_net_properties[] = {
2281 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2282 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
2283 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2284 DEFINE_PROP_END_OF_LIST(),
2287 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2289 DeviceState *qdev = DEVICE(vpci_dev);
2290 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2291 DeviceState *vdev = DEVICE(&dev->vdev);
2293 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2294 object_get_typename(OBJECT(qdev)));
2295 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2296 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2299 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2301 DeviceClass *dc = DEVICE_CLASS(klass);
2302 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2303 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2305 k->romfile = "efi-virtio.rom";
2306 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2307 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2308 k->revision = VIRTIO_PCI_ABI_VERSION;
2309 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2310 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2311 dc->props = virtio_net_properties;
2312 vpciklass->realize = virtio_net_pci_realize;
2315 static void virtio_net_pci_instance_init(Object *obj)
2317 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2319 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2320 TYPE_VIRTIO_NET);
2321 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2322 "bootindex", &error_abort);
2325 static const TypeInfo virtio_net_pci_info = {
2326 .name = TYPE_VIRTIO_NET_PCI,
2327 .parent = TYPE_VIRTIO_PCI,
2328 .instance_size = sizeof(VirtIONetPCI),
2329 .instance_init = virtio_net_pci_instance_init,
2330 .class_init = virtio_net_pci_class_init,
2333 /* virtio-rng-pci */
2335 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2337 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2338 DeviceState *vdev = DEVICE(&vrng->vdev);
2339 Error *err = NULL;
2341 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2342 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2343 if (err) {
2344 error_propagate(errp, err);
2345 return;
2348 object_property_set_link(OBJECT(vrng),
2349 OBJECT(vrng->vdev.conf.rng), "rng",
2350 NULL);
2353 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2355 DeviceClass *dc = DEVICE_CLASS(klass);
2356 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2357 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2359 k->realize = virtio_rng_pci_realize;
2360 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2362 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2363 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2364 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2365 pcidev_k->class_id = PCI_CLASS_OTHERS;
2368 static void virtio_rng_initfn(Object *obj)
2370 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2372 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2373 TYPE_VIRTIO_RNG);
2374 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2375 &error_abort);
2378 static const TypeInfo virtio_rng_pci_info = {
2379 .name = TYPE_VIRTIO_RNG_PCI,
2380 .parent = TYPE_VIRTIO_PCI,
2381 .instance_size = sizeof(VirtIORngPCI),
2382 .instance_init = virtio_rng_initfn,
2383 .class_init = virtio_rng_pci_class_init,
2386 /* virtio-input-pci */
2388 static Property virtio_input_pci_properties[] = {
2389 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2390 DEFINE_PROP_END_OF_LIST(),
2393 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2395 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2396 DeviceState *vdev = DEVICE(&vinput->vdev);
2398 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2399 virtio_pci_force_virtio_1(vpci_dev);
2400 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2403 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2405 DeviceClass *dc = DEVICE_CLASS(klass);
2406 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2407 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2409 dc->props = virtio_input_pci_properties;
2410 k->realize = virtio_input_pci_realize;
2411 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2413 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2416 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2418 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2420 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2423 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2424 void *data)
2426 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2428 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2431 static void virtio_keyboard_initfn(Object *obj)
2433 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2435 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2436 TYPE_VIRTIO_KEYBOARD);
2439 static void virtio_mouse_initfn(Object *obj)
2441 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2443 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2444 TYPE_VIRTIO_MOUSE);
2447 static void virtio_tablet_initfn(Object *obj)
2449 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2451 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2452 TYPE_VIRTIO_TABLET);
2455 static const TypeInfo virtio_input_pci_info = {
2456 .name = TYPE_VIRTIO_INPUT_PCI,
2457 .parent = TYPE_VIRTIO_PCI,
2458 .instance_size = sizeof(VirtIOInputPCI),
2459 .class_init = virtio_input_pci_class_init,
2460 .abstract = true,
2463 static const TypeInfo virtio_input_hid_pci_info = {
2464 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2465 .parent = TYPE_VIRTIO_INPUT_PCI,
2466 .instance_size = sizeof(VirtIOInputHIDPCI),
2467 .abstract = true,
2470 static const TypeInfo virtio_keyboard_pci_info = {
2471 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2472 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2473 .class_init = virtio_input_hid_kbd_pci_class_init,
2474 .instance_size = sizeof(VirtIOInputHIDPCI),
2475 .instance_init = virtio_keyboard_initfn,
2478 static const TypeInfo virtio_mouse_pci_info = {
2479 .name = TYPE_VIRTIO_MOUSE_PCI,
2480 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2481 .class_init = virtio_input_hid_mouse_pci_class_init,
2482 .instance_size = sizeof(VirtIOInputHIDPCI),
2483 .instance_init = virtio_mouse_initfn,
2486 static const TypeInfo virtio_tablet_pci_info = {
2487 .name = TYPE_VIRTIO_TABLET_PCI,
2488 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2489 .instance_size = sizeof(VirtIOInputHIDPCI),
2490 .instance_init = virtio_tablet_initfn,
2493 #ifdef CONFIG_LINUX
2494 static void virtio_host_initfn(Object *obj)
2496 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2498 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2499 TYPE_VIRTIO_INPUT_HOST);
2502 static const TypeInfo virtio_host_pci_info = {
2503 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2504 .parent = TYPE_VIRTIO_INPUT_PCI,
2505 .instance_size = sizeof(VirtIOInputHostPCI),
2506 .instance_init = virtio_host_initfn,
2508 #endif
2510 /* virtio-pci-bus */
2512 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2513 VirtIOPCIProxy *dev)
2515 DeviceState *qdev = DEVICE(dev);
2516 char virtio_bus_name[] = "virtio-bus";
2518 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2519 virtio_bus_name);
2522 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2524 BusClass *bus_class = BUS_CLASS(klass);
2525 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2526 bus_class->max_dev = 1;
2527 k->notify = virtio_pci_notify;
2528 k->save_config = virtio_pci_save_config;
2529 k->load_config = virtio_pci_load_config;
2530 k->save_queue = virtio_pci_save_queue;
2531 k->load_queue = virtio_pci_load_queue;
2532 k->save_extra_state = virtio_pci_save_extra_state;
2533 k->load_extra_state = virtio_pci_load_extra_state;
2534 k->has_extra_state = virtio_pci_has_extra_state;
2535 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2536 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2537 k->vmstate_change = virtio_pci_vmstate_change;
2538 k->pre_plugged = virtio_pci_pre_plugged;
2539 k->device_plugged = virtio_pci_device_plugged;
2540 k->device_unplugged = virtio_pci_device_unplugged;
2541 k->query_nvectors = virtio_pci_query_nvectors;
2542 k->ioeventfd_started = virtio_pci_ioeventfd_started;
2543 k->ioeventfd_set_started = virtio_pci_ioeventfd_set_started;
2544 k->ioeventfd_disabled = virtio_pci_ioeventfd_disabled;
2545 k->ioeventfd_set_disabled = virtio_pci_ioeventfd_set_disabled;
2546 k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2549 static const TypeInfo virtio_pci_bus_info = {
2550 .name = TYPE_VIRTIO_PCI_BUS,
2551 .parent = TYPE_VIRTIO_BUS,
2552 .instance_size = sizeof(VirtioPCIBusState),
2553 .class_init = virtio_pci_bus_class_init,
2556 static void virtio_pci_register_types(void)
2558 type_register_static(&virtio_rng_pci_info);
2559 type_register_static(&virtio_input_pci_info);
2560 type_register_static(&virtio_input_hid_pci_info);
2561 type_register_static(&virtio_keyboard_pci_info);
2562 type_register_static(&virtio_mouse_pci_info);
2563 type_register_static(&virtio_tablet_pci_info);
2564 #ifdef CONFIG_LINUX
2565 type_register_static(&virtio_host_pci_info);
2566 #endif
2567 type_register_static(&virtio_pci_bus_info);
2568 type_register_static(&virtio_pci_info);
2569 #ifdef CONFIG_VIRTFS
2570 type_register_static(&virtio_9p_pci_info);
2571 #endif
2572 type_register_static(&virtio_blk_pci_info);
2573 type_register_static(&virtio_scsi_pci_info);
2574 type_register_static(&virtio_balloon_pci_info);
2575 type_register_static(&virtio_serial_pci_info);
2576 type_register_static(&virtio_net_pci_info);
2577 #ifdef CONFIG_VHOST_SCSI
2578 type_register_static(&vhost_scsi_pci_info);
2579 #endif
2580 #ifdef CONFIG_VHOST_VSOCK
2581 type_register_static(&vhost_vsock_pci_info);
2582 #endif
2585 type_init(virtio_pci_register_types)