virtio-ccw: Check the number of vqs in CCW_CMD_SET_IND
[qemu/kevin.git] / hw / virtio / virtio-pci.c
blob5ce42af9d44e6788feedb491abf552267ad82d2a
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, atomic_read(&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,
112 VMStateField *field)
114 VirtIOPCIProxy *proxy = pv;
115 int i;
117 proxy->dfselect = qemu_get_be32(f);
118 proxy->gfselect = qemu_get_be32(f);
119 proxy->guest_features[0] = qemu_get_be32(f);
120 proxy->guest_features[1] = qemu_get_be32(f);
121 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
122 virtio_pci_load_modern_queue_state(&proxy->vqs[i], f);
125 return 0;
128 static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue *vq,
129 QEMUFile *f)
131 qemu_put_be16(f, vq->num);
132 qemu_put_be16(f, vq->enabled);
133 qemu_put_be32(f, vq->desc[0]);
134 qemu_put_be32(f, vq->desc[1]);
135 qemu_put_be32(f, vq->avail[0]);
136 qemu_put_be32(f, vq->avail[1]);
137 qemu_put_be32(f, vq->used[0]);
138 qemu_put_be32(f, vq->used[1]);
141 static int put_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size,
142 VMStateField *field, QJSON *vmdesc)
144 VirtIOPCIProxy *proxy = pv;
145 int i;
147 qemu_put_be32(f, proxy->dfselect);
148 qemu_put_be32(f, proxy->gfselect);
149 qemu_put_be32(f, proxy->guest_features[0]);
150 qemu_put_be32(f, proxy->guest_features[1]);
151 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
152 virtio_pci_save_modern_queue_state(&proxy->vqs[i], f);
155 return 0;
158 static const VMStateInfo vmstate_info_virtio_pci_modern_state = {
159 .name = "virtqueue_state",
160 .get = get_virtio_pci_modern_state,
161 .put = put_virtio_pci_modern_state,
164 static bool virtio_pci_modern_state_needed(void *opaque)
166 VirtIOPCIProxy *proxy = opaque;
168 return virtio_pci_modern(proxy);
171 static const VMStateDescription vmstate_virtio_pci_modern_state = {
172 .name = "virtio_pci/modern_state",
173 .version_id = 1,
174 .minimum_version_id = 1,
175 .needed = &virtio_pci_modern_state_needed,
176 .fields = (VMStateField[]) {
178 .name = "modern_state",
179 .version_id = 0,
180 .field_exists = NULL,
181 .size = 0,
182 .info = &vmstate_info_virtio_pci_modern_state,
183 .flags = VMS_SINGLE,
184 .offset = 0,
186 VMSTATE_END_OF_LIST()
190 static const VMStateDescription vmstate_virtio_pci = {
191 .name = "virtio_pci",
192 .version_id = 1,
193 .minimum_version_id = 1,
194 .minimum_version_id_old = 1,
195 .fields = (VMStateField[]) {
196 VMSTATE_END_OF_LIST()
198 .subsections = (const VMStateDescription*[]) {
199 &vmstate_virtio_pci_modern_state,
200 NULL
204 static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
206 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
208 vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
211 static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
213 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
215 return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
218 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
220 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
221 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
223 if (msix_present(&proxy->pci_dev))
224 qemu_put_be16(f, virtio_queue_vector(vdev, n));
227 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
229 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
230 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
232 int ret;
233 ret = pci_device_load(&proxy->pci_dev, f);
234 if (ret) {
235 return ret;
237 msix_unuse_all_vectors(&proxy->pci_dev);
238 msix_load(&proxy->pci_dev, f);
239 if (msix_present(&proxy->pci_dev)) {
240 qemu_get_be16s(f, &vdev->config_vector);
241 } else {
242 vdev->config_vector = VIRTIO_NO_VECTOR;
244 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
245 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
247 return 0;
250 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
252 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
253 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
255 uint16_t vector;
256 if (msix_present(&proxy->pci_dev)) {
257 qemu_get_be16s(f, &vector);
258 } else {
259 vector = VIRTIO_NO_VECTOR;
261 virtio_queue_set_vector(vdev, n, vector);
262 if (vector != VIRTIO_NO_VECTOR) {
263 return msix_vector_use(&proxy->pci_dev, vector);
266 return 0;
269 static bool virtio_pci_ioeventfd_enabled(DeviceState *d)
271 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
273 return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0;
276 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
278 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy)
280 return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ?
281 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4;
284 static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
285 int n, bool assign)
287 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
288 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
289 VirtQueue *vq = virtio_get_queue(vdev, n);
290 bool legacy = virtio_pci_legacy(proxy);
291 bool modern = virtio_pci_modern(proxy);
292 bool fast_mmio = kvm_ioeventfd_any_length_enabled();
293 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
294 MemoryRegion *modern_mr = &proxy->notify.mr;
295 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
296 MemoryRegion *legacy_mr = &proxy->bar;
297 hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) *
298 virtio_get_queue_index(vq);
299 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
301 if (assign) {
302 if (modern) {
303 if (fast_mmio) {
304 memory_region_add_eventfd(modern_mr, modern_addr, 0,
305 false, n, notifier);
306 } else {
307 memory_region_add_eventfd(modern_mr, modern_addr, 2,
308 false, n, notifier);
310 if (modern_pio) {
311 memory_region_add_eventfd(modern_notify_mr, 0, 2,
312 true, n, notifier);
315 if (legacy) {
316 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
317 true, n, notifier);
319 } else {
320 if (modern) {
321 if (fast_mmio) {
322 memory_region_del_eventfd(modern_mr, modern_addr, 0,
323 false, n, notifier);
324 } else {
325 memory_region_del_eventfd(modern_mr, modern_addr, 2,
326 false, n, notifier);
328 if (modern_pio) {
329 memory_region_del_eventfd(modern_notify_mr, 0, 2,
330 true, n, notifier);
333 if (legacy) {
334 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
335 true, n, notifier);
338 return 0;
341 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
343 virtio_bus_start_ioeventfd(&proxy->bus);
346 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
348 virtio_bus_stop_ioeventfd(&proxy->bus);
351 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
353 VirtIOPCIProxy *proxy = opaque;
354 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
355 hwaddr pa;
357 switch (addr) {
358 case VIRTIO_PCI_GUEST_FEATURES:
359 /* Guest does not negotiate properly? We have to assume nothing. */
360 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
361 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
363 virtio_set_features(vdev, val);
364 break;
365 case VIRTIO_PCI_QUEUE_PFN:
366 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
367 if (pa == 0) {
368 virtio_pci_reset(DEVICE(proxy));
370 else
371 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
372 break;
373 case VIRTIO_PCI_QUEUE_SEL:
374 if (val < VIRTIO_QUEUE_MAX)
375 vdev->queue_sel = val;
376 break;
377 case VIRTIO_PCI_QUEUE_NOTIFY:
378 if (val < VIRTIO_QUEUE_MAX) {
379 virtio_queue_notify(vdev, val);
381 break;
382 case VIRTIO_PCI_STATUS:
383 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
384 virtio_pci_stop_ioeventfd(proxy);
387 virtio_set_status(vdev, val & 0xFF);
389 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
390 virtio_pci_start_ioeventfd(proxy);
393 if (vdev->status == 0) {
394 virtio_pci_reset(DEVICE(proxy));
397 /* Linux before 2.6.34 drives the device without enabling
398 the PCI device bus master bit. Enable it automatically
399 for the guest. This is a PCI spec violation but so is
400 initiating DMA with bus master bit clear. */
401 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
402 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
403 proxy->pci_dev.config[PCI_COMMAND] |
404 PCI_COMMAND_MASTER, 1);
406 break;
407 case VIRTIO_MSI_CONFIG_VECTOR:
408 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
409 /* Make it possible for guest to discover an error took place. */
410 if (msix_vector_use(&proxy->pci_dev, val) < 0)
411 val = VIRTIO_NO_VECTOR;
412 vdev->config_vector = val;
413 break;
414 case VIRTIO_MSI_QUEUE_VECTOR:
415 msix_vector_unuse(&proxy->pci_dev,
416 virtio_queue_vector(vdev, vdev->queue_sel));
417 /* Make it possible for guest to discover an error took place. */
418 if (msix_vector_use(&proxy->pci_dev, val) < 0)
419 val = VIRTIO_NO_VECTOR;
420 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
421 break;
422 default:
423 error_report("%s: unexpected address 0x%x value 0x%x",
424 __func__, addr, val);
425 break;
429 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
431 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
432 uint32_t ret = 0xFFFFFFFF;
434 switch (addr) {
435 case VIRTIO_PCI_HOST_FEATURES:
436 ret = vdev->host_features;
437 break;
438 case VIRTIO_PCI_GUEST_FEATURES:
439 ret = vdev->guest_features;
440 break;
441 case VIRTIO_PCI_QUEUE_PFN:
442 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
443 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
444 break;
445 case VIRTIO_PCI_QUEUE_NUM:
446 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
447 break;
448 case VIRTIO_PCI_QUEUE_SEL:
449 ret = vdev->queue_sel;
450 break;
451 case VIRTIO_PCI_STATUS:
452 ret = vdev->status;
453 break;
454 case VIRTIO_PCI_ISR:
455 /* reading from the ISR also clears it. */
456 ret = atomic_xchg(&vdev->isr, 0);
457 pci_irq_deassert(&proxy->pci_dev);
458 break;
459 case VIRTIO_MSI_CONFIG_VECTOR:
460 ret = vdev->config_vector;
461 break;
462 case VIRTIO_MSI_QUEUE_VECTOR:
463 ret = virtio_queue_vector(vdev, vdev->queue_sel);
464 break;
465 default:
466 break;
469 return ret;
472 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
473 unsigned size)
475 VirtIOPCIProxy *proxy = opaque;
476 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
477 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
478 uint64_t val = 0;
479 if (addr < config) {
480 return virtio_ioport_read(proxy, addr);
482 addr -= config;
484 switch (size) {
485 case 1:
486 val = virtio_config_readb(vdev, addr);
487 break;
488 case 2:
489 val = virtio_config_readw(vdev, addr);
490 if (virtio_is_big_endian(vdev)) {
491 val = bswap16(val);
493 break;
494 case 4:
495 val = virtio_config_readl(vdev, addr);
496 if (virtio_is_big_endian(vdev)) {
497 val = bswap32(val);
499 break;
501 return val;
504 static void virtio_pci_config_write(void *opaque, hwaddr addr,
505 uint64_t val, unsigned size)
507 VirtIOPCIProxy *proxy = opaque;
508 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
509 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
510 if (addr < config) {
511 virtio_ioport_write(proxy, addr, val);
512 return;
514 addr -= config;
516 * Virtio-PCI is odd. Ioports are LE but config space is target native
517 * endian.
519 switch (size) {
520 case 1:
521 virtio_config_writeb(vdev, addr, val);
522 break;
523 case 2:
524 if (virtio_is_big_endian(vdev)) {
525 val = bswap16(val);
527 virtio_config_writew(vdev, addr, val);
528 break;
529 case 4:
530 if (virtio_is_big_endian(vdev)) {
531 val = bswap32(val);
533 virtio_config_writel(vdev, addr, val);
534 break;
538 static const MemoryRegionOps virtio_pci_config_ops = {
539 .read = virtio_pci_config_read,
540 .write = virtio_pci_config_write,
541 .impl = {
542 .min_access_size = 1,
543 .max_access_size = 4,
545 .endianness = DEVICE_LITTLE_ENDIAN,
548 /* Below are generic functions to do memcpy from/to an address space,
549 * without byteswaps, with input validation.
551 * As regular address_space_* APIs all do some kind of byteswap at least for
552 * some host/target combinations, we are forced to explicitly convert to a
553 * known-endianness integer value.
554 * It doesn't really matter which endian format to go through, so the code
555 * below selects the endian that causes the least amount of work on the given
556 * host.
558 * Note: host pointer must be aligned.
560 static
561 void virtio_address_space_write(AddressSpace *as, hwaddr addr,
562 const uint8_t *buf, int len)
564 uint32_t val;
566 /* address_space_* APIs assume an aligned address.
567 * As address is under guest control, handle illegal values.
569 addr &= ~(len - 1);
571 /* Make sure caller aligned buf properly */
572 assert(!(((uintptr_t)buf) & (len - 1)));
574 switch (len) {
575 case 1:
576 val = pci_get_byte(buf);
577 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
578 break;
579 case 2:
580 val = pci_get_word(buf);
581 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
582 break;
583 case 4:
584 val = pci_get_long(buf);
585 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
586 break;
587 default:
588 /* As length is under guest control, handle illegal values. */
589 break;
593 static void
594 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
596 uint32_t val;
598 /* address_space_* APIs assume an aligned address.
599 * As address is under guest control, handle illegal values.
601 addr &= ~(len - 1);
603 /* Make sure caller aligned buf properly */
604 assert(!(((uintptr_t)buf) & (len - 1)));
606 switch (len) {
607 case 1:
608 val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
609 pci_set_byte(buf, val);
610 break;
611 case 2:
612 val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
613 pci_set_word(buf, val);
614 break;
615 case 4:
616 val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
617 pci_set_long(buf, val);
618 break;
619 default:
620 /* As length is under guest control, handle illegal values. */
621 break;
625 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
626 uint32_t val, int len)
628 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
629 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
630 struct virtio_pci_cfg_cap *cfg;
632 pci_default_write_config(pci_dev, address, val, len);
634 if (range_covers_byte(address, len, PCI_COMMAND) &&
635 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
636 virtio_pci_stop_ioeventfd(proxy);
637 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
640 if (proxy->config_cap &&
641 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
642 pci_cfg_data),
643 sizeof cfg->pci_cfg_data)) {
644 uint32_t off;
645 uint32_t len;
647 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
648 off = le32_to_cpu(cfg->cap.offset);
649 len = le32_to_cpu(cfg->cap.length);
651 if (len == 1 || len == 2 || len == 4) {
652 assert(len <= sizeof cfg->pci_cfg_data);
653 virtio_address_space_write(&proxy->modern_as, off,
654 cfg->pci_cfg_data, len);
659 static uint32_t virtio_read_config(PCIDevice *pci_dev,
660 uint32_t address, int len)
662 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
663 struct virtio_pci_cfg_cap *cfg;
665 if (proxy->config_cap &&
666 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
667 pci_cfg_data),
668 sizeof cfg->pci_cfg_data)) {
669 uint32_t off;
670 uint32_t len;
672 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
673 off = le32_to_cpu(cfg->cap.offset);
674 len = le32_to_cpu(cfg->cap.length);
676 if (len == 1 || len == 2 || len == 4) {
677 assert(len <= sizeof cfg->pci_cfg_data);
678 virtio_address_space_read(&proxy->modern_as, off,
679 cfg->pci_cfg_data, len);
683 return pci_default_read_config(pci_dev, address, len);
686 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
687 unsigned int queue_no,
688 unsigned int vector)
690 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
691 int ret;
693 if (irqfd->users == 0) {
694 ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev);
695 if (ret < 0) {
696 return ret;
698 irqfd->virq = ret;
700 irqfd->users++;
701 return 0;
704 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
705 unsigned int vector)
707 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
708 if (--irqfd->users == 0) {
709 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
713 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
714 unsigned int queue_no,
715 unsigned int vector)
717 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
718 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
719 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
720 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
721 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
724 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
725 unsigned int queue_no,
726 unsigned int vector)
728 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
729 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
730 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
731 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
732 int ret;
734 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
735 assert(ret == 0);
738 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
740 PCIDevice *dev = &proxy->pci_dev;
741 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
742 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
743 unsigned int vector;
744 int ret, queue_no;
746 for (queue_no = 0; queue_no < nvqs; queue_no++) {
747 if (!virtio_queue_get_num(vdev, queue_no)) {
748 break;
750 vector = virtio_queue_vector(vdev, queue_no);
751 if (vector >= msix_nr_vectors_allocated(dev)) {
752 continue;
754 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
755 if (ret < 0) {
756 goto undo;
758 /* If guest supports masking, set up irqfd now.
759 * Otherwise, delay until unmasked in the frontend.
761 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
762 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
763 if (ret < 0) {
764 kvm_virtio_pci_vq_vector_release(proxy, vector);
765 goto undo;
769 return 0;
771 undo:
772 while (--queue_no >= 0) {
773 vector = virtio_queue_vector(vdev, queue_no);
774 if (vector >= msix_nr_vectors_allocated(dev)) {
775 continue;
777 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
778 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
780 kvm_virtio_pci_vq_vector_release(proxy, vector);
782 return ret;
785 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
787 PCIDevice *dev = &proxy->pci_dev;
788 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
789 unsigned int vector;
790 int queue_no;
791 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
793 for (queue_no = 0; queue_no < nvqs; queue_no++) {
794 if (!virtio_queue_get_num(vdev, queue_no)) {
795 break;
797 vector = virtio_queue_vector(vdev, queue_no);
798 if (vector >= msix_nr_vectors_allocated(dev)) {
799 continue;
801 /* If guest supports masking, clean up irqfd now.
802 * Otherwise, it was cleaned when masked in the frontend.
804 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
805 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
807 kvm_virtio_pci_vq_vector_release(proxy, vector);
811 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
812 unsigned int queue_no,
813 unsigned int vector,
814 MSIMessage msg)
816 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
817 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
818 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
819 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
820 VirtIOIRQFD *irqfd;
821 int ret = 0;
823 if (proxy->vector_irqfd) {
824 irqfd = &proxy->vector_irqfd[vector];
825 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
826 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
827 &proxy->pci_dev);
828 if (ret < 0) {
829 return ret;
831 kvm_irqchip_commit_routes(kvm_state);
835 /* If guest supports masking, irqfd is already setup, unmask it.
836 * Otherwise, set it up now.
838 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
839 k->guest_notifier_mask(vdev, queue_no, false);
840 /* Test after unmasking to avoid losing events. */
841 if (k->guest_notifier_pending &&
842 k->guest_notifier_pending(vdev, queue_no)) {
843 event_notifier_set(n);
845 } else {
846 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
848 return ret;
851 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
852 unsigned int queue_no,
853 unsigned int vector)
855 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
856 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
858 /* If guest supports masking, keep irqfd but mask it.
859 * Otherwise, clean it up now.
861 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
862 k->guest_notifier_mask(vdev, queue_no, true);
863 } else {
864 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
868 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
869 MSIMessage msg)
871 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
872 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
873 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
874 int ret, index, unmasked = 0;
876 while (vq) {
877 index = virtio_get_queue_index(vq);
878 if (!virtio_queue_get_num(vdev, index)) {
879 break;
881 if (index < proxy->nvqs_with_notifiers) {
882 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
883 if (ret < 0) {
884 goto undo;
886 ++unmasked;
888 vq = virtio_vector_next_queue(vq);
891 return 0;
893 undo:
894 vq = virtio_vector_first_queue(vdev, vector);
895 while (vq && unmasked >= 0) {
896 index = virtio_get_queue_index(vq);
897 if (index < proxy->nvqs_with_notifiers) {
898 virtio_pci_vq_vector_mask(proxy, index, vector);
899 --unmasked;
901 vq = virtio_vector_next_queue(vq);
903 return ret;
906 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
908 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
909 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
910 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
911 int index;
913 while (vq) {
914 index = virtio_get_queue_index(vq);
915 if (!virtio_queue_get_num(vdev, index)) {
916 break;
918 if (index < proxy->nvqs_with_notifiers) {
919 virtio_pci_vq_vector_mask(proxy, index, vector);
921 vq = virtio_vector_next_queue(vq);
925 static void virtio_pci_vector_poll(PCIDevice *dev,
926 unsigned int vector_start,
927 unsigned int vector_end)
929 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
930 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
931 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
932 int queue_no;
933 unsigned int vector;
934 EventNotifier *notifier;
935 VirtQueue *vq;
937 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
938 if (!virtio_queue_get_num(vdev, queue_no)) {
939 break;
941 vector = virtio_queue_vector(vdev, queue_no);
942 if (vector < vector_start || vector >= vector_end ||
943 !msix_is_masked(dev, vector)) {
944 continue;
946 vq = virtio_get_queue(vdev, queue_no);
947 notifier = virtio_queue_get_guest_notifier(vq);
948 if (k->guest_notifier_pending) {
949 if (k->guest_notifier_pending(vdev, queue_no)) {
950 msix_set_pending(dev, vector);
952 } else if (event_notifier_test_and_clear(notifier)) {
953 msix_set_pending(dev, vector);
958 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
959 bool with_irqfd)
961 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
962 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
963 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
964 VirtQueue *vq = virtio_get_queue(vdev, n);
965 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
967 if (assign) {
968 int r = event_notifier_init(notifier, 0);
969 if (r < 0) {
970 return r;
972 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
973 } else {
974 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
975 event_notifier_cleanup(notifier);
978 if (!msix_enabled(&proxy->pci_dev) &&
979 vdev->use_guest_notifier_mask &&
980 vdc->guest_notifier_mask) {
981 vdc->guest_notifier_mask(vdev, n, !assign);
984 return 0;
987 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
989 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
990 return msix_enabled(&proxy->pci_dev);
993 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
995 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
996 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
997 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
998 int r, n;
999 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
1000 kvm_msi_via_irqfd_enabled();
1002 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1004 /* When deassigning, pass a consistent nvqs value
1005 * to avoid leaking notifiers.
1007 assert(assign || nvqs == proxy->nvqs_with_notifiers);
1009 proxy->nvqs_with_notifiers = nvqs;
1011 /* Must unset vector notifier while guest notifier is still assigned */
1012 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1013 msix_unset_vector_notifiers(&proxy->pci_dev);
1014 if (proxy->vector_irqfd) {
1015 kvm_virtio_pci_vector_release(proxy, nvqs);
1016 g_free(proxy->vector_irqfd);
1017 proxy->vector_irqfd = NULL;
1021 for (n = 0; n < nvqs; n++) {
1022 if (!virtio_queue_get_num(vdev, n)) {
1023 break;
1026 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1027 if (r < 0) {
1028 goto assign_error;
1032 /* Must set vector notifier after guest notifier has been assigned */
1033 if ((with_irqfd || k->guest_notifier_mask) && assign) {
1034 if (with_irqfd) {
1035 proxy->vector_irqfd =
1036 g_malloc0(sizeof(*proxy->vector_irqfd) *
1037 msix_nr_vectors_allocated(&proxy->pci_dev));
1038 r = kvm_virtio_pci_vector_use(proxy, nvqs);
1039 if (r < 0) {
1040 goto assign_error;
1043 r = msix_set_vector_notifiers(&proxy->pci_dev,
1044 virtio_pci_vector_unmask,
1045 virtio_pci_vector_mask,
1046 virtio_pci_vector_poll);
1047 if (r < 0) {
1048 goto notifiers_error;
1052 return 0;
1054 notifiers_error:
1055 if (with_irqfd) {
1056 assert(assign);
1057 kvm_virtio_pci_vector_release(proxy, nvqs);
1060 assign_error:
1061 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1062 assert(assign);
1063 while (--n >= 0) {
1064 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1066 return r;
1069 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1071 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1072 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1074 if (running) {
1075 /* Old QEMU versions did not set bus master enable on status write.
1076 * Detect DRIVER set and enable it.
1078 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
1079 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
1080 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1081 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
1082 proxy->pci_dev.config[PCI_COMMAND] |
1083 PCI_COMMAND_MASTER, 1);
1085 virtio_pci_start_ioeventfd(proxy);
1086 } else {
1087 virtio_pci_stop_ioeventfd(proxy);
1091 #ifdef CONFIG_VIRTFS
1092 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1094 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1095 DeviceState *vdev = DEVICE(&dev->vdev);
1097 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1098 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1101 static Property virtio_9p_pci_properties[] = {
1102 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1103 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1104 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1105 DEFINE_PROP_END_OF_LIST(),
1108 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1110 DeviceClass *dc = DEVICE_CLASS(klass);
1111 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1112 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1114 k->realize = virtio_9p_pci_realize;
1115 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1116 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1117 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1118 pcidev_k->class_id = 0x2;
1119 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1120 dc->props = virtio_9p_pci_properties;
1123 static void virtio_9p_pci_instance_init(Object *obj)
1125 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1127 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1128 TYPE_VIRTIO_9P);
1131 static const TypeInfo virtio_9p_pci_info = {
1132 .name = TYPE_VIRTIO_9P_PCI,
1133 .parent = TYPE_VIRTIO_PCI,
1134 .instance_size = sizeof(V9fsPCIState),
1135 .instance_init = virtio_9p_pci_instance_init,
1136 .class_init = virtio_9p_pci_class_init,
1138 #endif /* CONFIG_VIRTFS */
1141 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1144 static int virtio_pci_query_nvectors(DeviceState *d)
1146 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1148 return proxy->nvectors;
1151 static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
1153 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1154 PCIDevice *dev = &proxy->pci_dev;
1156 return pci_get_address_space(dev);
1159 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1160 struct virtio_pci_cap *cap)
1162 PCIDevice *dev = &proxy->pci_dev;
1163 int offset;
1165 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1166 assert(offset > 0);
1168 assert(cap->cap_len >= sizeof *cap);
1169 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1170 cap->cap_len - PCI_CAP_FLAGS);
1172 return offset;
1175 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1176 unsigned size)
1178 VirtIOPCIProxy *proxy = opaque;
1179 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1180 uint32_t val = 0;
1181 int i;
1183 switch (addr) {
1184 case VIRTIO_PCI_COMMON_DFSELECT:
1185 val = proxy->dfselect;
1186 break;
1187 case VIRTIO_PCI_COMMON_DF:
1188 if (proxy->dfselect <= 1) {
1189 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1191 val = (vdev->host_features & ~vdc->legacy_features) >>
1192 (32 * proxy->dfselect);
1194 break;
1195 case VIRTIO_PCI_COMMON_GFSELECT:
1196 val = proxy->gfselect;
1197 break;
1198 case VIRTIO_PCI_COMMON_GF:
1199 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1200 val = proxy->guest_features[proxy->gfselect];
1202 break;
1203 case VIRTIO_PCI_COMMON_MSIX:
1204 val = vdev->config_vector;
1205 break;
1206 case VIRTIO_PCI_COMMON_NUMQ:
1207 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1208 if (virtio_queue_get_num(vdev, i)) {
1209 val = i + 1;
1212 break;
1213 case VIRTIO_PCI_COMMON_STATUS:
1214 val = vdev->status;
1215 break;
1216 case VIRTIO_PCI_COMMON_CFGGENERATION:
1217 val = vdev->generation;
1218 break;
1219 case VIRTIO_PCI_COMMON_Q_SELECT:
1220 val = vdev->queue_sel;
1221 break;
1222 case VIRTIO_PCI_COMMON_Q_SIZE:
1223 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1224 break;
1225 case VIRTIO_PCI_COMMON_Q_MSIX:
1226 val = virtio_queue_vector(vdev, vdev->queue_sel);
1227 break;
1228 case VIRTIO_PCI_COMMON_Q_ENABLE:
1229 val = proxy->vqs[vdev->queue_sel].enabled;
1230 break;
1231 case VIRTIO_PCI_COMMON_Q_NOFF:
1232 /* Simply map queues in order */
1233 val = vdev->queue_sel;
1234 break;
1235 case VIRTIO_PCI_COMMON_Q_DESCLO:
1236 val = proxy->vqs[vdev->queue_sel].desc[0];
1237 break;
1238 case VIRTIO_PCI_COMMON_Q_DESCHI:
1239 val = proxy->vqs[vdev->queue_sel].desc[1];
1240 break;
1241 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1242 val = proxy->vqs[vdev->queue_sel].avail[0];
1243 break;
1244 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1245 val = proxy->vqs[vdev->queue_sel].avail[1];
1246 break;
1247 case VIRTIO_PCI_COMMON_Q_USEDLO:
1248 val = proxy->vqs[vdev->queue_sel].used[0];
1249 break;
1250 case VIRTIO_PCI_COMMON_Q_USEDHI:
1251 val = proxy->vqs[vdev->queue_sel].used[1];
1252 break;
1253 default:
1254 val = 0;
1257 return val;
1260 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1261 uint64_t val, unsigned size)
1263 VirtIOPCIProxy *proxy = opaque;
1264 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1266 switch (addr) {
1267 case VIRTIO_PCI_COMMON_DFSELECT:
1268 proxy->dfselect = val;
1269 break;
1270 case VIRTIO_PCI_COMMON_GFSELECT:
1271 proxy->gfselect = val;
1272 break;
1273 case VIRTIO_PCI_COMMON_GF:
1274 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1275 proxy->guest_features[proxy->gfselect] = val;
1276 virtio_set_features(vdev,
1277 (((uint64_t)proxy->guest_features[1]) << 32) |
1278 proxy->guest_features[0]);
1280 break;
1281 case VIRTIO_PCI_COMMON_MSIX:
1282 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1283 /* Make it possible for guest to discover an error took place. */
1284 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1285 val = VIRTIO_NO_VECTOR;
1287 vdev->config_vector = val;
1288 break;
1289 case VIRTIO_PCI_COMMON_STATUS:
1290 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1291 virtio_pci_stop_ioeventfd(proxy);
1294 virtio_set_status(vdev, val & 0xFF);
1296 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1297 virtio_pci_start_ioeventfd(proxy);
1300 if (vdev->status == 0) {
1301 virtio_pci_reset(DEVICE(proxy));
1304 break;
1305 case VIRTIO_PCI_COMMON_Q_SELECT:
1306 if (val < VIRTIO_QUEUE_MAX) {
1307 vdev->queue_sel = val;
1309 break;
1310 case VIRTIO_PCI_COMMON_Q_SIZE:
1311 proxy->vqs[vdev->queue_sel].num = val;
1312 break;
1313 case VIRTIO_PCI_COMMON_Q_MSIX:
1314 msix_vector_unuse(&proxy->pci_dev,
1315 virtio_queue_vector(vdev, vdev->queue_sel));
1316 /* Make it possible for guest to discover an error took place. */
1317 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1318 val = VIRTIO_NO_VECTOR;
1320 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1321 break;
1322 case VIRTIO_PCI_COMMON_Q_ENABLE:
1323 virtio_queue_set_num(vdev, vdev->queue_sel,
1324 proxy->vqs[vdev->queue_sel].num);
1325 virtio_queue_set_rings(vdev, vdev->queue_sel,
1326 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1327 proxy->vqs[vdev->queue_sel].desc[0],
1328 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1329 proxy->vqs[vdev->queue_sel].avail[0],
1330 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1331 proxy->vqs[vdev->queue_sel].used[0]);
1332 proxy->vqs[vdev->queue_sel].enabled = 1;
1333 break;
1334 case VIRTIO_PCI_COMMON_Q_DESCLO:
1335 proxy->vqs[vdev->queue_sel].desc[0] = val;
1336 break;
1337 case VIRTIO_PCI_COMMON_Q_DESCHI:
1338 proxy->vqs[vdev->queue_sel].desc[1] = val;
1339 break;
1340 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1341 proxy->vqs[vdev->queue_sel].avail[0] = val;
1342 break;
1343 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1344 proxy->vqs[vdev->queue_sel].avail[1] = val;
1345 break;
1346 case VIRTIO_PCI_COMMON_Q_USEDLO:
1347 proxy->vqs[vdev->queue_sel].used[0] = val;
1348 break;
1349 case VIRTIO_PCI_COMMON_Q_USEDHI:
1350 proxy->vqs[vdev->queue_sel].used[1] = val;
1351 break;
1352 default:
1353 break;
1358 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1359 unsigned size)
1361 return 0;
1364 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1365 uint64_t val, unsigned size)
1367 VirtIODevice *vdev = opaque;
1368 VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
1369 unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1371 if (queue < VIRTIO_QUEUE_MAX) {
1372 virtio_queue_notify(vdev, queue);
1376 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
1377 uint64_t val, unsigned size)
1379 VirtIODevice *vdev = opaque;
1380 unsigned queue = val;
1382 if (queue < VIRTIO_QUEUE_MAX) {
1383 virtio_queue_notify(vdev, queue);
1387 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1388 unsigned size)
1390 VirtIOPCIProxy *proxy = opaque;
1391 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1392 uint64_t val = atomic_xchg(&vdev->isr, 0);
1393 pci_irq_deassert(&proxy->pci_dev);
1395 return val;
1398 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1399 uint64_t val, unsigned size)
1403 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1404 unsigned size)
1406 VirtIODevice *vdev = opaque;
1407 uint64_t val = 0;
1409 switch (size) {
1410 case 1:
1411 val = virtio_config_modern_readb(vdev, addr);
1412 break;
1413 case 2:
1414 val = virtio_config_modern_readw(vdev, addr);
1415 break;
1416 case 4:
1417 val = virtio_config_modern_readl(vdev, addr);
1418 break;
1420 return val;
1423 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1424 uint64_t val, unsigned size)
1426 VirtIODevice *vdev = opaque;
1427 switch (size) {
1428 case 1:
1429 virtio_config_modern_writeb(vdev, addr, val);
1430 break;
1431 case 2:
1432 virtio_config_modern_writew(vdev, addr, val);
1433 break;
1434 case 4:
1435 virtio_config_modern_writel(vdev, addr, val);
1436 break;
1440 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1442 static const MemoryRegionOps common_ops = {
1443 .read = virtio_pci_common_read,
1444 .write = virtio_pci_common_write,
1445 .impl = {
1446 .min_access_size = 1,
1447 .max_access_size = 4,
1449 .endianness = DEVICE_LITTLE_ENDIAN,
1451 static const MemoryRegionOps isr_ops = {
1452 .read = virtio_pci_isr_read,
1453 .write = virtio_pci_isr_write,
1454 .impl = {
1455 .min_access_size = 1,
1456 .max_access_size = 4,
1458 .endianness = DEVICE_LITTLE_ENDIAN,
1460 static const MemoryRegionOps device_ops = {
1461 .read = virtio_pci_device_read,
1462 .write = virtio_pci_device_write,
1463 .impl = {
1464 .min_access_size = 1,
1465 .max_access_size = 4,
1467 .endianness = DEVICE_LITTLE_ENDIAN,
1469 static const MemoryRegionOps notify_ops = {
1470 .read = virtio_pci_notify_read,
1471 .write = virtio_pci_notify_write,
1472 .impl = {
1473 .min_access_size = 1,
1474 .max_access_size = 4,
1476 .endianness = DEVICE_LITTLE_ENDIAN,
1478 static const MemoryRegionOps notify_pio_ops = {
1479 .read = virtio_pci_notify_read,
1480 .write = virtio_pci_notify_write_pio,
1481 .impl = {
1482 .min_access_size = 1,
1483 .max_access_size = 4,
1485 .endianness = DEVICE_LITTLE_ENDIAN,
1489 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1490 &common_ops,
1491 proxy,
1492 "virtio-pci-common",
1493 proxy->common.size);
1495 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1496 &isr_ops,
1497 proxy,
1498 "virtio-pci-isr",
1499 proxy->isr.size);
1501 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1502 &device_ops,
1503 virtio_bus_get_device(&proxy->bus),
1504 "virtio-pci-device",
1505 proxy->device.size);
1507 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1508 &notify_ops,
1509 virtio_bus_get_device(&proxy->bus),
1510 "virtio-pci-notify",
1511 proxy->notify.size);
1513 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
1514 &notify_pio_ops,
1515 virtio_bus_get_device(&proxy->bus),
1516 "virtio-pci-notify-pio",
1517 proxy->notify_pio.size);
1520 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1521 VirtIOPCIRegion *region,
1522 struct virtio_pci_cap *cap,
1523 MemoryRegion *mr,
1524 uint8_t bar)
1526 memory_region_add_subregion(mr, region->offset, &region->mr);
1528 cap->cfg_type = region->type;
1529 cap->bar = bar;
1530 cap->offset = cpu_to_le32(region->offset);
1531 cap->length = cpu_to_le32(region->size);
1532 virtio_pci_add_mem_cap(proxy, cap);
1536 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
1537 VirtIOPCIRegion *region,
1538 struct virtio_pci_cap *cap)
1540 virtio_pci_modern_region_map(proxy, region, cap,
1541 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1544 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
1545 VirtIOPCIRegion *region,
1546 struct virtio_pci_cap *cap)
1548 virtio_pci_modern_region_map(proxy, region, cap,
1549 &proxy->io_bar, proxy->modern_io_bar_idx);
1552 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
1553 VirtIOPCIRegion *region)
1555 memory_region_del_subregion(&proxy->modern_bar,
1556 &region->mr);
1559 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
1560 VirtIOPCIRegion *region)
1562 memory_region_del_subregion(&proxy->io_bar,
1563 &region->mr);
1566 static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
1568 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1569 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1571 if (virtio_pci_modern(proxy)) {
1572 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1575 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1578 /* This is called by virtio-bus just after the device is plugged. */
1579 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1581 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1582 VirtioBusState *bus = &proxy->bus;
1583 bool legacy = virtio_pci_legacy(proxy);
1584 bool modern;
1585 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1586 uint8_t *config;
1587 uint32_t size;
1588 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1591 * Virtio capabilities present without
1592 * VIRTIO_F_VERSION_1 confuses guests
1594 if (!proxy->ignore_backend_features &&
1595 !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1596 virtio_pci_disable_modern(proxy);
1598 if (!legacy) {
1599 error_setg(errp, "Device doesn't support modern mode, and legacy"
1600 " mode is disabled");
1601 error_append_hint(errp, "Set disable-legacy to off\n");
1603 return;
1607 modern = virtio_pci_modern(proxy);
1609 config = proxy->pci_dev.config;
1610 if (proxy->class_code) {
1611 pci_config_set_class(config, proxy->class_code);
1614 if (legacy) {
1615 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
1616 error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1617 "neither legacy nor transitional device.");
1618 return ;
1620 /* legacy and transitional */
1621 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1622 pci_get_word(config + PCI_VENDOR_ID));
1623 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1624 } else {
1625 /* pure virtio-1.0 */
1626 pci_set_word(config + PCI_VENDOR_ID,
1627 PCI_VENDOR_ID_REDHAT_QUMRANET);
1628 pci_set_word(config + PCI_DEVICE_ID,
1629 0x1040 + virtio_bus_get_vdev_id(bus));
1630 pci_config_set_revision(config, 1);
1632 config[PCI_INTERRUPT_PIN] = 1;
1635 if (modern) {
1636 struct virtio_pci_cap cap = {
1637 .cap_len = sizeof cap,
1639 struct virtio_pci_notify_cap notify = {
1640 .cap.cap_len = sizeof notify,
1641 .notify_off_multiplier =
1642 cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
1644 struct virtio_pci_cfg_cap cfg = {
1645 .cap.cap_len = sizeof cfg,
1646 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1648 struct virtio_pci_notify_cap notify_pio = {
1649 .cap.cap_len = sizeof notify,
1650 .notify_off_multiplier = cpu_to_le32(0x0),
1653 struct virtio_pci_cfg_cap *cfg_mask;
1655 virtio_pci_modern_regions_init(proxy);
1657 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
1658 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
1659 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
1660 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, &notify.cap);
1662 if (modern_pio) {
1663 memory_region_init(&proxy->io_bar, OBJECT(proxy),
1664 "virtio-pci-io", 0x4);
1666 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1667 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
1669 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
1670 &notify_pio.cap);
1673 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
1674 PCI_BASE_ADDRESS_SPACE_MEMORY |
1675 PCI_BASE_ADDRESS_MEM_PREFETCH |
1676 PCI_BASE_ADDRESS_MEM_TYPE_64,
1677 &proxy->modern_bar);
1679 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1680 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1681 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1682 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1683 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1684 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1687 if (proxy->nvectors) {
1688 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1689 proxy->msix_bar_idx, NULL);
1690 if (err) {
1691 /* Notice when a system that supports MSIx can't initialize it */
1692 if (err != -ENOTSUP) {
1693 error_report("unable to init msix vectors to %" PRIu32,
1694 proxy->nvectors);
1696 proxy->nvectors = 0;
1700 proxy->pci_dev.config_write = virtio_write_config;
1701 proxy->pci_dev.config_read = virtio_read_config;
1703 if (legacy) {
1704 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1705 + virtio_bus_get_vdev_config_len(bus);
1706 size = pow2ceil(size);
1708 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1709 &virtio_pci_config_ops,
1710 proxy, "virtio-pci", size);
1712 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
1713 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1717 static void virtio_pci_device_unplugged(DeviceState *d)
1719 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1720 bool modern = virtio_pci_modern(proxy);
1721 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1723 virtio_pci_stop_ioeventfd(proxy);
1725 if (modern) {
1726 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
1727 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
1728 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
1729 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
1730 if (modern_pio) {
1731 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
1736 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1738 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1739 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1740 bool pcie_port = pci_bus_is_express(pci_dev->bus) &&
1741 !pci_bus_is_root(pci_dev->bus);
1743 if (!kvm_has_many_ioeventfds()) {
1744 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1748 * virtio pci bar layout used by default.
1749 * subclasses can re-arrange things if needed.
1751 * region 0 -- virtio legacy io bar
1752 * region 1 -- msi-x bar
1753 * region 4+5 -- virtio modern memory (64bit) bar
1756 proxy->legacy_io_bar_idx = 0;
1757 proxy->msix_bar_idx = 1;
1758 proxy->modern_io_bar_idx = 2;
1759 proxy->modern_mem_bar_idx = 4;
1761 proxy->common.offset = 0x0;
1762 proxy->common.size = 0x1000;
1763 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1765 proxy->isr.offset = 0x1000;
1766 proxy->isr.size = 0x1000;
1767 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1769 proxy->device.offset = 0x2000;
1770 proxy->device.size = 0x1000;
1771 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1773 proxy->notify.offset = 0x3000;
1774 proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1775 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1777 proxy->notify_pio.offset = 0x0;
1778 proxy->notify_pio.size = 0x4;
1779 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1781 /* subclasses can enforce modern, so do this unconditionally */
1782 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1783 /* PCI BAR regions must be powers of 2 */
1784 pow2ceil(proxy->notify.offset + proxy->notify.size));
1786 memory_region_init_alias(&proxy->modern_cfg,
1787 OBJECT(proxy),
1788 "virtio-pci-cfg",
1789 &proxy->modern_bar,
1791 memory_region_size(&proxy->modern_bar));
1793 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1795 if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
1796 proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
1799 if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1800 error_setg(errp, "device cannot work as neither modern nor legacy mode"
1801 " is enabled");
1802 error_append_hint(errp, "Set either disable-modern or disable-legacy"
1803 " to off\n");
1804 return;
1807 if (pcie_port && pci_is_express(pci_dev)) {
1808 int pos;
1810 pos = pcie_endpoint_cap_init(pci_dev, 0);
1811 assert(pos > 0);
1813 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF);
1814 assert(pos > 0);
1817 * Indicates that this function complies with revision 1.2 of the
1818 * PCI Power Management Interface Specification.
1820 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
1822 if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
1823 pcie_ats_init(pci_dev, 256);
1826 } else {
1828 * make future invocations of pci_is_express() return false
1829 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1831 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1834 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1835 if (k->realize) {
1836 k->realize(proxy, errp);
1840 static void virtio_pci_exit(PCIDevice *pci_dev)
1842 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1844 msix_uninit_exclusive_bar(pci_dev);
1845 address_space_destroy(&proxy->modern_as);
1848 static void virtio_pci_reset(DeviceState *qdev)
1850 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1851 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1852 int i;
1854 virtio_pci_stop_ioeventfd(proxy);
1855 virtio_bus_reset(bus);
1856 msix_unuse_all_vectors(&proxy->pci_dev);
1858 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1859 proxy->vqs[i].enabled = 0;
1863 static Property virtio_pci_properties[] = {
1864 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1865 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1866 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
1867 ON_OFF_AUTO_AUTO),
1868 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
1869 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1870 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1871 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
1872 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1873 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
1874 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1875 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
1876 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1877 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
1878 ignore_backend_features, false),
1879 DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags,
1880 VIRTIO_PCI_FLAG_ATS_BIT, false),
1881 DEFINE_PROP_END_OF_LIST(),
1884 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
1886 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
1887 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1888 PCIDevice *pci_dev = &proxy->pci_dev;
1890 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
1891 virtio_pci_modern(proxy)) {
1892 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1895 vpciklass->parent_dc_realize(qdev, errp);
1898 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1900 DeviceClass *dc = DEVICE_CLASS(klass);
1901 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1902 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1904 dc->props = virtio_pci_properties;
1905 k->realize = virtio_pci_realize;
1906 k->exit = virtio_pci_exit;
1907 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1908 k->revision = VIRTIO_PCI_ABI_VERSION;
1909 k->class_id = PCI_CLASS_OTHERS;
1910 vpciklass->parent_dc_realize = dc->realize;
1911 dc->realize = virtio_pci_dc_realize;
1912 dc->reset = virtio_pci_reset;
1915 static const TypeInfo virtio_pci_info = {
1916 .name = TYPE_VIRTIO_PCI,
1917 .parent = TYPE_PCI_DEVICE,
1918 .instance_size = sizeof(VirtIOPCIProxy),
1919 .class_init = virtio_pci_class_init,
1920 .class_size = sizeof(VirtioPCIClass),
1921 .abstract = true,
1924 /* virtio-blk-pci */
1926 static Property virtio_blk_pci_properties[] = {
1927 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1928 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1929 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1930 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1931 DEFINE_PROP_END_OF_LIST(),
1934 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1936 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1937 DeviceState *vdev = DEVICE(&dev->vdev);
1939 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1940 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1943 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1945 DeviceClass *dc = DEVICE_CLASS(klass);
1946 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1947 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1949 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1950 dc->props = virtio_blk_pci_properties;
1951 k->realize = virtio_blk_pci_realize;
1952 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1953 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1954 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1955 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1958 static void virtio_blk_pci_instance_init(Object *obj)
1960 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1962 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1963 TYPE_VIRTIO_BLK);
1964 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1965 &error_abort);
1966 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1967 "bootindex", &error_abort);
1970 static const TypeInfo virtio_blk_pci_info = {
1971 .name = TYPE_VIRTIO_BLK_PCI,
1972 .parent = TYPE_VIRTIO_PCI,
1973 .instance_size = sizeof(VirtIOBlkPCI),
1974 .instance_init = virtio_blk_pci_instance_init,
1975 .class_init = virtio_blk_pci_class_init,
1978 /* virtio-scsi-pci */
1980 static Property virtio_scsi_pci_properties[] = {
1981 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1982 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1983 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1984 DEV_NVECTORS_UNSPECIFIED),
1985 DEFINE_PROP_END_OF_LIST(),
1988 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1990 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1991 DeviceState *vdev = DEVICE(&dev->vdev);
1992 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1993 DeviceState *proxy = DEVICE(vpci_dev);
1994 char *bus_name;
1996 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1997 vpci_dev->nvectors = vs->conf.num_queues + 3;
2001 * For command line compatibility, this sets the virtio-scsi-device bus
2002 * name as before.
2004 if (proxy->id) {
2005 bus_name = g_strdup_printf("%s.0", proxy->id);
2006 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2007 g_free(bus_name);
2010 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2011 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2014 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
2016 DeviceClass *dc = DEVICE_CLASS(klass);
2017 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2018 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2020 k->realize = virtio_scsi_pci_realize;
2021 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2022 dc->props = virtio_scsi_pci_properties;
2023 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2024 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2025 pcidev_k->revision = 0x00;
2026 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2029 static void virtio_scsi_pci_instance_init(Object *obj)
2031 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
2033 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2034 TYPE_VIRTIO_SCSI);
2035 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
2036 &error_abort);
2039 static const TypeInfo virtio_scsi_pci_info = {
2040 .name = TYPE_VIRTIO_SCSI_PCI,
2041 .parent = TYPE_VIRTIO_PCI,
2042 .instance_size = sizeof(VirtIOSCSIPCI),
2043 .instance_init = virtio_scsi_pci_instance_init,
2044 .class_init = virtio_scsi_pci_class_init,
2047 /* vhost-scsi-pci */
2049 #ifdef CONFIG_VHOST_SCSI
2050 static Property vhost_scsi_pci_properties[] = {
2051 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2052 DEV_NVECTORS_UNSPECIFIED),
2053 DEFINE_PROP_END_OF_LIST(),
2056 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2058 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
2059 DeviceState *vdev = DEVICE(&dev->vdev);
2060 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2062 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2063 vpci_dev->nvectors = vs->conf.num_queues + 3;
2066 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2067 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2070 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
2072 DeviceClass *dc = DEVICE_CLASS(klass);
2073 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2074 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2075 k->realize = vhost_scsi_pci_realize;
2076 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2077 dc->props = vhost_scsi_pci_properties;
2078 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2079 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2080 pcidev_k->revision = 0x00;
2081 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2084 static void vhost_scsi_pci_instance_init(Object *obj)
2086 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2088 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2089 TYPE_VHOST_SCSI);
2090 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2091 "bootindex", &error_abort);
2094 static const TypeInfo vhost_scsi_pci_info = {
2095 .name = TYPE_VHOST_SCSI_PCI,
2096 .parent = TYPE_VIRTIO_PCI,
2097 .instance_size = sizeof(VHostSCSIPCI),
2098 .instance_init = vhost_scsi_pci_instance_init,
2099 .class_init = vhost_scsi_pci_class_init,
2101 #endif
2103 /* vhost-vsock-pci */
2105 #ifdef CONFIG_VHOST_VSOCK
2106 static Property vhost_vsock_pci_properties[] = {
2107 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2108 DEFINE_PROP_END_OF_LIST(),
2111 static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2113 VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev);
2114 DeviceState *vdev = DEVICE(&dev->vdev);
2116 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2117 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2120 static void vhost_vsock_pci_class_init(ObjectClass *klass, void *data)
2122 DeviceClass *dc = DEVICE_CLASS(klass);
2123 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2124 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2125 k->realize = vhost_vsock_pci_realize;
2126 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2127 dc->props = vhost_vsock_pci_properties;
2128 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2129 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
2130 pcidev_k->revision = 0x00;
2131 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2134 static void vhost_vsock_pci_instance_init(Object *obj)
2136 VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj);
2138 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2139 TYPE_VHOST_VSOCK);
2142 static const TypeInfo vhost_vsock_pci_info = {
2143 .name = TYPE_VHOST_VSOCK_PCI,
2144 .parent = TYPE_VIRTIO_PCI,
2145 .instance_size = sizeof(VHostVSockPCI),
2146 .instance_init = vhost_vsock_pci_instance_init,
2147 .class_init = vhost_vsock_pci_class_init,
2149 #endif
2151 /* virtio-balloon-pci */
2153 static Property virtio_balloon_pci_properties[] = {
2154 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2155 DEFINE_PROP_END_OF_LIST(),
2158 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2160 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
2161 DeviceState *vdev = DEVICE(&dev->vdev);
2163 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
2164 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
2165 vpci_dev->class_code = PCI_CLASS_OTHERS;
2168 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2169 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2172 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
2174 DeviceClass *dc = DEVICE_CLASS(klass);
2175 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2176 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2177 k->realize = virtio_balloon_pci_realize;
2178 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2179 dc->props = virtio_balloon_pci_properties;
2180 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2181 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
2182 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2183 pcidev_k->class_id = PCI_CLASS_OTHERS;
2186 static void virtio_balloon_pci_instance_init(Object *obj)
2188 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2190 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2191 TYPE_VIRTIO_BALLOON);
2192 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
2193 "guest-stats", &error_abort);
2194 object_property_add_alias(obj, "guest-stats-polling-interval",
2195 OBJECT(&dev->vdev),
2196 "guest-stats-polling-interval", &error_abort);
2199 static const TypeInfo virtio_balloon_pci_info = {
2200 .name = TYPE_VIRTIO_BALLOON_PCI,
2201 .parent = TYPE_VIRTIO_PCI,
2202 .instance_size = sizeof(VirtIOBalloonPCI),
2203 .instance_init = virtio_balloon_pci_instance_init,
2204 .class_init = virtio_balloon_pci_class_init,
2207 /* virtio-serial-pci */
2209 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2211 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2212 DeviceState *vdev = DEVICE(&dev->vdev);
2213 DeviceState *proxy = DEVICE(vpci_dev);
2214 char *bus_name;
2216 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2217 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
2218 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
2219 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2222 /* backwards-compatibility with machines that were created with
2223 DEV_NVECTORS_UNSPECIFIED */
2224 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2225 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2229 * For command line compatibility, this sets the virtio-serial-device bus
2230 * name as before.
2232 if (proxy->id) {
2233 bus_name = g_strdup_printf("%s.0", proxy->id);
2234 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2235 g_free(bus_name);
2238 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2239 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2242 static Property virtio_serial_pci_properties[] = {
2243 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2244 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2245 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2246 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2247 DEFINE_PROP_END_OF_LIST(),
2250 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2252 DeviceClass *dc = DEVICE_CLASS(klass);
2253 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2254 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2255 k->realize = virtio_serial_pci_realize;
2256 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2257 dc->props = virtio_serial_pci_properties;
2258 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2259 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2260 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2261 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2264 static void virtio_serial_pci_instance_init(Object *obj)
2266 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2268 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2269 TYPE_VIRTIO_SERIAL);
2272 static const TypeInfo virtio_serial_pci_info = {
2273 .name = TYPE_VIRTIO_SERIAL_PCI,
2274 .parent = TYPE_VIRTIO_PCI,
2275 .instance_size = sizeof(VirtIOSerialPCI),
2276 .instance_init = virtio_serial_pci_instance_init,
2277 .class_init = virtio_serial_pci_class_init,
2280 /* virtio-net-pci */
2282 static Property virtio_net_properties[] = {
2283 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2284 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2285 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2286 DEFINE_PROP_END_OF_LIST(),
2289 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2291 DeviceState *qdev = DEVICE(vpci_dev);
2292 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2293 DeviceState *vdev = DEVICE(&dev->vdev);
2295 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2296 object_get_typename(OBJECT(qdev)));
2297 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2298 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2301 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2303 DeviceClass *dc = DEVICE_CLASS(klass);
2304 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2305 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2307 k->romfile = "efi-virtio.rom";
2308 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2309 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2310 k->revision = VIRTIO_PCI_ABI_VERSION;
2311 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2312 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2313 dc->props = virtio_net_properties;
2314 vpciklass->realize = virtio_net_pci_realize;
2317 static void virtio_net_pci_instance_init(Object *obj)
2319 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2321 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2322 TYPE_VIRTIO_NET);
2323 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2324 "bootindex", &error_abort);
2327 static const TypeInfo virtio_net_pci_info = {
2328 .name = TYPE_VIRTIO_NET_PCI,
2329 .parent = TYPE_VIRTIO_PCI,
2330 .instance_size = sizeof(VirtIONetPCI),
2331 .instance_init = virtio_net_pci_instance_init,
2332 .class_init = virtio_net_pci_class_init,
2335 /* virtio-rng-pci */
2337 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2339 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2340 DeviceState *vdev = DEVICE(&vrng->vdev);
2341 Error *err = NULL;
2343 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2344 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2345 if (err) {
2346 error_propagate(errp, err);
2347 return;
2350 object_property_set_link(OBJECT(vrng),
2351 OBJECT(vrng->vdev.conf.rng), "rng",
2352 NULL);
2355 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2357 DeviceClass *dc = DEVICE_CLASS(klass);
2358 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2359 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2361 k->realize = virtio_rng_pci_realize;
2362 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2364 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2365 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2366 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2367 pcidev_k->class_id = PCI_CLASS_OTHERS;
2370 static void virtio_rng_initfn(Object *obj)
2372 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2374 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2375 TYPE_VIRTIO_RNG);
2376 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2377 &error_abort);
2380 static const TypeInfo virtio_rng_pci_info = {
2381 .name = TYPE_VIRTIO_RNG_PCI,
2382 .parent = TYPE_VIRTIO_PCI,
2383 .instance_size = sizeof(VirtIORngPCI),
2384 .instance_init = virtio_rng_initfn,
2385 .class_init = virtio_rng_pci_class_init,
2388 /* virtio-input-pci */
2390 static Property virtio_input_pci_properties[] = {
2391 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2392 DEFINE_PROP_END_OF_LIST(),
2395 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2397 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2398 DeviceState *vdev = DEVICE(&vinput->vdev);
2400 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2401 virtio_pci_force_virtio_1(vpci_dev);
2402 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2405 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2407 DeviceClass *dc = DEVICE_CLASS(klass);
2408 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2409 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2411 dc->props = virtio_input_pci_properties;
2412 k->realize = virtio_input_pci_realize;
2413 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2415 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2418 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2420 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2422 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2425 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2426 void *data)
2428 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2430 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2433 static void virtio_keyboard_initfn(Object *obj)
2435 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2437 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2438 TYPE_VIRTIO_KEYBOARD);
2441 static void virtio_mouse_initfn(Object *obj)
2443 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2445 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2446 TYPE_VIRTIO_MOUSE);
2449 static void virtio_tablet_initfn(Object *obj)
2451 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2453 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2454 TYPE_VIRTIO_TABLET);
2457 static const TypeInfo virtio_input_pci_info = {
2458 .name = TYPE_VIRTIO_INPUT_PCI,
2459 .parent = TYPE_VIRTIO_PCI,
2460 .instance_size = sizeof(VirtIOInputPCI),
2461 .class_init = virtio_input_pci_class_init,
2462 .abstract = true,
2465 static const TypeInfo virtio_input_hid_pci_info = {
2466 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2467 .parent = TYPE_VIRTIO_INPUT_PCI,
2468 .instance_size = sizeof(VirtIOInputHIDPCI),
2469 .abstract = true,
2472 static const TypeInfo virtio_keyboard_pci_info = {
2473 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2474 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2475 .class_init = virtio_input_hid_kbd_pci_class_init,
2476 .instance_size = sizeof(VirtIOInputHIDPCI),
2477 .instance_init = virtio_keyboard_initfn,
2480 static const TypeInfo virtio_mouse_pci_info = {
2481 .name = TYPE_VIRTIO_MOUSE_PCI,
2482 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2483 .class_init = virtio_input_hid_mouse_pci_class_init,
2484 .instance_size = sizeof(VirtIOInputHIDPCI),
2485 .instance_init = virtio_mouse_initfn,
2488 static const TypeInfo virtio_tablet_pci_info = {
2489 .name = TYPE_VIRTIO_TABLET_PCI,
2490 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2491 .instance_size = sizeof(VirtIOInputHIDPCI),
2492 .instance_init = virtio_tablet_initfn,
2495 #ifdef CONFIG_LINUX
2496 static void virtio_host_initfn(Object *obj)
2498 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2500 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2501 TYPE_VIRTIO_INPUT_HOST);
2504 static const TypeInfo virtio_host_pci_info = {
2505 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2506 .parent = TYPE_VIRTIO_INPUT_PCI,
2507 .instance_size = sizeof(VirtIOInputHostPCI),
2508 .instance_init = virtio_host_initfn,
2510 #endif
2512 /* virtio-pci-bus */
2514 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2515 VirtIOPCIProxy *dev)
2517 DeviceState *qdev = DEVICE(dev);
2518 char virtio_bus_name[] = "virtio-bus";
2520 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2521 virtio_bus_name);
2524 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2526 BusClass *bus_class = BUS_CLASS(klass);
2527 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2528 bus_class->max_dev = 1;
2529 k->notify = virtio_pci_notify;
2530 k->save_config = virtio_pci_save_config;
2531 k->load_config = virtio_pci_load_config;
2532 k->save_queue = virtio_pci_save_queue;
2533 k->load_queue = virtio_pci_load_queue;
2534 k->save_extra_state = virtio_pci_save_extra_state;
2535 k->load_extra_state = virtio_pci_load_extra_state;
2536 k->has_extra_state = virtio_pci_has_extra_state;
2537 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2538 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2539 k->vmstate_change = virtio_pci_vmstate_change;
2540 k->pre_plugged = virtio_pci_pre_plugged;
2541 k->device_plugged = virtio_pci_device_plugged;
2542 k->device_unplugged = virtio_pci_device_unplugged;
2543 k->query_nvectors = virtio_pci_query_nvectors;
2544 k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
2545 k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2546 k->get_dma_as = virtio_pci_get_dma_as;
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)