virtio-blk: switch off scsi-passthrough by default
[qemu.git] / hw / virtio / virtio-pci.c
blobf55dd2bf8e19029ded546b064968deab25dae8cf
1 /*
2 * Virtio PCI Bindings
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
18 #include <inttypes.h>
20 #include "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 "qemu/error-report.h"
30 #include "hw/pci/msi.h"
31 #include "hw/pci/msix.h"
32 #include "hw/loader.h"
33 #include "sysemu/kvm.h"
34 #include "sysemu/block-backend.h"
35 #include "virtio-pci.h"
36 #include "qemu/range.h"
37 #include "hw/virtio/virtio-bus.h"
38 #include "qapi/visitor.h"
40 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
42 #undef VIRTIO_PCI_CONFIG
44 /* The remaining space is defined by each driver as the per-driver
45 * configuration space */
46 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
48 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
49 VirtIOPCIProxy *dev);
51 /* virtio device */
52 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
53 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
55 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
58 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
59 * be careful and test performance if you change this.
61 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
63 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
66 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
68 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
70 if (msix_enabled(&proxy->pci_dev))
71 msix_notify(&proxy->pci_dev, vector);
72 else {
73 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
74 pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
78 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
80 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
81 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
83 pci_device_save(&proxy->pci_dev, f);
84 msix_save(&proxy->pci_dev, f);
85 if (msix_present(&proxy->pci_dev))
86 qemu_put_be16(f, vdev->config_vector);
89 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
91 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
92 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
94 if (msix_present(&proxy->pci_dev))
95 qemu_put_be16(f, virtio_queue_vector(vdev, n));
98 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
100 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
101 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
103 int ret;
104 ret = pci_device_load(&proxy->pci_dev, f);
105 if (ret) {
106 return ret;
108 msix_unuse_all_vectors(&proxy->pci_dev);
109 msix_load(&proxy->pci_dev, f);
110 if (msix_present(&proxy->pci_dev)) {
111 qemu_get_be16s(f, &vdev->config_vector);
112 } else {
113 vdev->config_vector = VIRTIO_NO_VECTOR;
115 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
116 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
118 return 0;
121 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
123 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
124 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
126 uint16_t vector;
127 if (msix_present(&proxy->pci_dev)) {
128 qemu_get_be16s(f, &vector);
129 } else {
130 vector = VIRTIO_NO_VECTOR;
132 virtio_queue_set_vector(vdev, n, vector);
133 if (vector != VIRTIO_NO_VECTOR) {
134 return msix_vector_use(&proxy->pci_dev, vector);
136 return 0;
139 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
141 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
142 int n, bool assign, bool set_handler)
144 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
145 VirtQueue *vq = virtio_get_queue(vdev, n);
146 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
147 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
148 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
149 MemoryRegion *modern_mr = &proxy->notify.mr;
150 MemoryRegion *legacy_mr = &proxy->bar;
151 hwaddr modern_addr = QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
152 virtio_get_queue_index(vq);
153 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
154 int r = 0;
156 if (assign) {
157 r = event_notifier_init(notifier, 1);
158 if (r < 0) {
159 error_report("%s: unable to init event notifier: %d",
160 __func__, r);
161 return r;
163 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
164 if (modern) {
165 memory_region_add_eventfd(modern_mr, modern_addr, 2,
166 true, n, notifier);
168 if (legacy) {
169 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
170 true, n, notifier);
172 } else {
173 if (modern) {
174 memory_region_del_eventfd(modern_mr, modern_addr, 2,
175 true, n, notifier);
177 if (legacy) {
178 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
179 true, n, notifier);
181 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
182 event_notifier_cleanup(notifier);
184 return r;
187 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
189 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
190 int n, r;
192 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
193 proxy->ioeventfd_disabled ||
194 proxy->ioeventfd_started) {
195 return;
198 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
199 if (!virtio_queue_get_num(vdev, n)) {
200 continue;
203 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
204 if (r < 0) {
205 goto assign_error;
208 proxy->ioeventfd_started = true;
209 return;
211 assign_error:
212 while (--n >= 0) {
213 if (!virtio_queue_get_num(vdev, n)) {
214 continue;
217 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
218 assert(r >= 0);
220 proxy->ioeventfd_started = false;
221 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
224 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
226 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
227 int r;
228 int n;
230 if (!proxy->ioeventfd_started) {
231 return;
234 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
235 if (!virtio_queue_get_num(vdev, n)) {
236 continue;
239 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
240 assert(r >= 0);
242 proxy->ioeventfd_started = false;
245 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
247 VirtIOPCIProxy *proxy = opaque;
248 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
249 hwaddr pa;
251 switch (addr) {
252 case VIRTIO_PCI_GUEST_FEATURES:
253 /* Guest does not negotiate properly? We have to assume nothing. */
254 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
255 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
257 virtio_set_features(vdev, val);
258 break;
259 case VIRTIO_PCI_QUEUE_PFN:
260 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
261 if (pa == 0) {
262 virtio_pci_stop_ioeventfd(proxy);
263 virtio_reset(vdev);
264 msix_unuse_all_vectors(&proxy->pci_dev);
266 else
267 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
268 break;
269 case VIRTIO_PCI_QUEUE_SEL:
270 if (val < VIRTIO_QUEUE_MAX)
271 vdev->queue_sel = val;
272 break;
273 case VIRTIO_PCI_QUEUE_NOTIFY:
274 if (val < VIRTIO_QUEUE_MAX) {
275 virtio_queue_notify(vdev, val);
277 break;
278 case VIRTIO_PCI_STATUS:
279 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
280 virtio_pci_stop_ioeventfd(proxy);
283 virtio_set_status(vdev, val & 0xFF);
285 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
286 virtio_pci_start_ioeventfd(proxy);
289 if (vdev->status == 0) {
290 virtio_reset(vdev);
291 msix_unuse_all_vectors(&proxy->pci_dev);
294 /* Linux before 2.6.34 drives the device without enabling
295 the PCI device bus master bit. Enable it automatically
296 for the guest. This is a PCI spec violation but so is
297 initiating DMA with bus master bit clear. */
298 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
299 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
300 proxy->pci_dev.config[PCI_COMMAND] |
301 PCI_COMMAND_MASTER, 1);
303 break;
304 case VIRTIO_MSI_CONFIG_VECTOR:
305 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
306 /* Make it possible for guest to discover an error took place. */
307 if (msix_vector_use(&proxy->pci_dev, val) < 0)
308 val = VIRTIO_NO_VECTOR;
309 vdev->config_vector = val;
310 break;
311 case VIRTIO_MSI_QUEUE_VECTOR:
312 msix_vector_unuse(&proxy->pci_dev,
313 virtio_queue_vector(vdev, vdev->queue_sel));
314 /* Make it possible for guest to discover an error took place. */
315 if (msix_vector_use(&proxy->pci_dev, val) < 0)
316 val = VIRTIO_NO_VECTOR;
317 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
318 break;
319 default:
320 error_report("%s: unexpected address 0x%x value 0x%x",
321 __func__, addr, val);
322 break;
326 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
328 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
329 uint32_t ret = 0xFFFFFFFF;
331 switch (addr) {
332 case VIRTIO_PCI_HOST_FEATURES:
333 ret = vdev->host_features;
334 break;
335 case VIRTIO_PCI_GUEST_FEATURES:
336 ret = vdev->guest_features;
337 break;
338 case VIRTIO_PCI_QUEUE_PFN:
339 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
340 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
341 break;
342 case VIRTIO_PCI_QUEUE_NUM:
343 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
344 break;
345 case VIRTIO_PCI_QUEUE_SEL:
346 ret = vdev->queue_sel;
347 break;
348 case VIRTIO_PCI_STATUS:
349 ret = vdev->status;
350 break;
351 case VIRTIO_PCI_ISR:
352 /* reading from the ISR also clears it. */
353 ret = vdev->isr;
354 vdev->isr = 0;
355 pci_irq_deassert(&proxy->pci_dev);
356 break;
357 case VIRTIO_MSI_CONFIG_VECTOR:
358 ret = vdev->config_vector;
359 break;
360 case VIRTIO_MSI_QUEUE_VECTOR:
361 ret = virtio_queue_vector(vdev, vdev->queue_sel);
362 break;
363 default:
364 break;
367 return ret;
370 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
371 unsigned size)
373 VirtIOPCIProxy *proxy = opaque;
374 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
375 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
376 uint64_t val = 0;
377 if (addr < config) {
378 return virtio_ioport_read(proxy, addr);
380 addr -= config;
382 switch (size) {
383 case 1:
384 val = virtio_config_readb(vdev, addr);
385 break;
386 case 2:
387 val = virtio_config_readw(vdev, addr);
388 if (virtio_is_big_endian(vdev)) {
389 val = bswap16(val);
391 break;
392 case 4:
393 val = virtio_config_readl(vdev, addr);
394 if (virtio_is_big_endian(vdev)) {
395 val = bswap32(val);
397 break;
399 return val;
402 static void virtio_pci_config_write(void *opaque, hwaddr addr,
403 uint64_t val, unsigned size)
405 VirtIOPCIProxy *proxy = opaque;
406 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
407 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
408 if (addr < config) {
409 virtio_ioport_write(proxy, addr, val);
410 return;
412 addr -= config;
414 * Virtio-PCI is odd. Ioports are LE but config space is target native
415 * endian.
417 switch (size) {
418 case 1:
419 virtio_config_writeb(vdev, addr, val);
420 break;
421 case 2:
422 if (virtio_is_big_endian(vdev)) {
423 val = bswap16(val);
425 virtio_config_writew(vdev, addr, val);
426 break;
427 case 4:
428 if (virtio_is_big_endian(vdev)) {
429 val = bswap32(val);
431 virtio_config_writel(vdev, addr, val);
432 break;
436 static const MemoryRegionOps virtio_pci_config_ops = {
437 .read = virtio_pci_config_read,
438 .write = virtio_pci_config_write,
439 .impl = {
440 .min_access_size = 1,
441 .max_access_size = 4,
443 .endianness = DEVICE_LITTLE_ENDIAN,
446 /* Below are generic functions to do memcpy from/to an address space,
447 * without byteswaps, with input validation.
449 * As regular address_space_* APIs all do some kind of byteswap at least for
450 * some host/target combinations, we are forced to explicitly convert to a
451 * known-endianness integer value.
452 * It doesn't really matter which endian format to go through, so the code
453 * below selects the endian that causes the least amount of work on the given
454 * host.
456 * Note: host pointer must be aligned.
458 static
459 void virtio_address_space_write(AddressSpace *as, hwaddr addr,
460 const uint8_t *buf, int len)
462 uint32_t val;
464 /* address_space_* APIs assume an aligned address.
465 * As address is under guest control, handle illegal values.
467 addr &= ~(len - 1);
469 /* Make sure caller aligned buf properly */
470 assert(!(((uintptr_t)buf) & (len - 1)));
472 switch (len) {
473 case 1:
474 val = pci_get_byte(buf);
475 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
476 break;
477 case 2:
478 val = pci_get_word(buf);
479 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
480 break;
481 case 4:
482 val = pci_get_long(buf);
483 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
484 break;
485 default:
486 /* As length is under guest control, handle illegal values. */
487 break;
491 static void
492 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
494 uint32_t val;
496 /* address_space_* APIs assume an aligned address.
497 * As address is under guest control, handle illegal values.
499 addr &= ~(len - 1);
501 /* Make sure caller aligned buf properly */
502 assert(!(((uintptr_t)buf) & (len - 1)));
504 switch (len) {
505 case 1:
506 val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
507 pci_set_byte(buf, val);
508 break;
509 case 2:
510 val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
511 pci_set_word(buf, val);
512 break;
513 case 4:
514 val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
515 pci_set_long(buf, val);
516 break;
517 default:
518 /* As length is under guest control, handle illegal values. */
519 break;
523 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
524 uint32_t val, int len)
526 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
527 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
528 struct virtio_pci_cfg_cap *cfg;
530 pci_default_write_config(pci_dev, address, val, len);
532 if (range_covers_byte(address, len, PCI_COMMAND) &&
533 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
534 virtio_pci_stop_ioeventfd(proxy);
535 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
538 if (proxy->config_cap &&
539 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
540 pci_cfg_data),
541 sizeof cfg->pci_cfg_data)) {
542 uint32_t off;
543 uint32_t len;
545 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
546 off = le32_to_cpu(cfg->cap.offset);
547 len = le32_to_cpu(cfg->cap.length);
549 if (len == 1 || len == 2 || len == 4) {
550 assert(len <= sizeof cfg->pci_cfg_data);
551 virtio_address_space_write(&proxy->modern_as, off,
552 cfg->pci_cfg_data, len);
557 static uint32_t virtio_read_config(PCIDevice *pci_dev,
558 uint32_t address, int len)
560 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
561 struct virtio_pci_cfg_cap *cfg;
563 if (proxy->config_cap &&
564 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
565 pci_cfg_data),
566 sizeof cfg->pci_cfg_data)) {
567 uint32_t off;
568 uint32_t len;
570 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
571 off = le32_to_cpu(cfg->cap.offset);
572 len = le32_to_cpu(cfg->cap.length);
574 if (len == 1 || len == 2 || len == 4) {
575 assert(len <= sizeof cfg->pci_cfg_data);
576 virtio_address_space_read(&proxy->modern_as, off,
577 cfg->pci_cfg_data, len);
581 return pci_default_read_config(pci_dev, address, len);
584 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
585 unsigned int queue_no,
586 unsigned int vector,
587 MSIMessage msg)
589 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
590 int ret;
592 if (irqfd->users == 0) {
593 ret = kvm_irqchip_add_msi_route(kvm_state, msg, &proxy->pci_dev);
594 if (ret < 0) {
595 return ret;
597 irqfd->virq = ret;
599 irqfd->users++;
600 return 0;
603 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
604 unsigned int vector)
606 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
607 if (--irqfd->users == 0) {
608 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
612 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
613 unsigned int queue_no,
614 unsigned int vector)
616 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
617 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
618 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
619 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
620 int ret;
621 ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
622 return ret;
625 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
626 unsigned int queue_no,
627 unsigned int vector)
629 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
630 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
631 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
632 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
633 int ret;
635 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
636 assert(ret == 0);
639 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
641 PCIDevice *dev = &proxy->pci_dev;
642 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
643 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
644 unsigned int vector;
645 int ret, queue_no;
646 MSIMessage msg;
648 for (queue_no = 0; queue_no < nvqs; queue_no++) {
649 if (!virtio_queue_get_num(vdev, queue_no)) {
650 break;
652 vector = virtio_queue_vector(vdev, queue_no);
653 if (vector >= msix_nr_vectors_allocated(dev)) {
654 continue;
656 msg = msix_get_message(dev, vector);
657 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
658 if (ret < 0) {
659 goto undo;
661 /* If guest supports masking, set up irqfd now.
662 * Otherwise, delay until unmasked in the frontend.
664 if (k->guest_notifier_mask) {
665 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
666 if (ret < 0) {
667 kvm_virtio_pci_vq_vector_release(proxy, vector);
668 goto undo;
672 return 0;
674 undo:
675 while (--queue_no >= 0) {
676 vector = virtio_queue_vector(vdev, queue_no);
677 if (vector >= msix_nr_vectors_allocated(dev)) {
678 continue;
680 if (k->guest_notifier_mask) {
681 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
683 kvm_virtio_pci_vq_vector_release(proxy, vector);
685 return ret;
688 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
690 PCIDevice *dev = &proxy->pci_dev;
691 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
692 unsigned int vector;
693 int queue_no;
694 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
696 for (queue_no = 0; queue_no < nvqs; queue_no++) {
697 if (!virtio_queue_get_num(vdev, queue_no)) {
698 break;
700 vector = virtio_queue_vector(vdev, queue_no);
701 if (vector >= msix_nr_vectors_allocated(dev)) {
702 continue;
704 /* If guest supports masking, clean up irqfd now.
705 * Otherwise, it was cleaned when masked in the frontend.
707 if (k->guest_notifier_mask) {
708 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
710 kvm_virtio_pci_vq_vector_release(proxy, vector);
714 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
715 unsigned int queue_no,
716 unsigned int vector,
717 MSIMessage msg)
719 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
720 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
721 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
722 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
723 VirtIOIRQFD *irqfd;
724 int ret = 0;
726 if (proxy->vector_irqfd) {
727 irqfd = &proxy->vector_irqfd[vector];
728 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
729 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
730 &proxy->pci_dev);
731 if (ret < 0) {
732 return ret;
737 /* If guest supports masking, irqfd is already setup, unmask it.
738 * Otherwise, set it up now.
740 if (k->guest_notifier_mask) {
741 k->guest_notifier_mask(vdev, queue_no, false);
742 /* Test after unmasking to avoid losing events. */
743 if (k->guest_notifier_pending &&
744 k->guest_notifier_pending(vdev, queue_no)) {
745 event_notifier_set(n);
747 } else {
748 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
750 return ret;
753 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
754 unsigned int queue_no,
755 unsigned int vector)
757 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
758 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
760 /* If guest supports masking, keep irqfd but mask it.
761 * Otherwise, clean it up now.
763 if (k->guest_notifier_mask) {
764 k->guest_notifier_mask(vdev, queue_no, true);
765 } else {
766 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
770 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
771 MSIMessage msg)
773 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
774 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
775 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
776 int ret, index, unmasked = 0;
778 while (vq) {
779 index = virtio_get_queue_index(vq);
780 if (!virtio_queue_get_num(vdev, index)) {
781 break;
783 if (index < proxy->nvqs_with_notifiers) {
784 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
785 if (ret < 0) {
786 goto undo;
788 ++unmasked;
790 vq = virtio_vector_next_queue(vq);
793 return 0;
795 undo:
796 vq = virtio_vector_first_queue(vdev, vector);
797 while (vq && unmasked >= 0) {
798 index = virtio_get_queue_index(vq);
799 if (index < proxy->nvqs_with_notifiers) {
800 virtio_pci_vq_vector_mask(proxy, index, vector);
801 --unmasked;
803 vq = virtio_vector_next_queue(vq);
805 return ret;
808 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
810 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
811 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
812 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
813 int index;
815 while (vq) {
816 index = virtio_get_queue_index(vq);
817 if (!virtio_queue_get_num(vdev, index)) {
818 break;
820 if (index < proxy->nvqs_with_notifiers) {
821 virtio_pci_vq_vector_mask(proxy, index, vector);
823 vq = virtio_vector_next_queue(vq);
827 static void virtio_pci_vector_poll(PCIDevice *dev,
828 unsigned int vector_start,
829 unsigned int vector_end)
831 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
832 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
833 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
834 int queue_no;
835 unsigned int vector;
836 EventNotifier *notifier;
837 VirtQueue *vq;
839 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
840 if (!virtio_queue_get_num(vdev, queue_no)) {
841 break;
843 vector = virtio_queue_vector(vdev, queue_no);
844 if (vector < vector_start || vector >= vector_end ||
845 !msix_is_masked(dev, vector)) {
846 continue;
848 vq = virtio_get_queue(vdev, queue_no);
849 notifier = virtio_queue_get_guest_notifier(vq);
850 if (k->guest_notifier_pending) {
851 if (k->guest_notifier_pending(vdev, queue_no)) {
852 msix_set_pending(dev, vector);
854 } else if (event_notifier_test_and_clear(notifier)) {
855 msix_set_pending(dev, vector);
860 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
861 bool with_irqfd)
863 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
864 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
865 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
866 VirtQueue *vq = virtio_get_queue(vdev, n);
867 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
869 if (assign) {
870 int r = event_notifier_init(notifier, 0);
871 if (r < 0) {
872 return r;
874 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
875 } else {
876 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
877 event_notifier_cleanup(notifier);
880 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
881 vdc->guest_notifier_mask(vdev, n, !assign);
884 return 0;
887 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
889 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
890 return msix_enabled(&proxy->pci_dev);
893 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
895 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
896 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
897 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
898 int r, n;
899 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
900 kvm_msi_via_irqfd_enabled();
902 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
904 /* When deassigning, pass a consistent nvqs value
905 * to avoid leaking notifiers.
907 assert(assign || nvqs == proxy->nvqs_with_notifiers);
909 proxy->nvqs_with_notifiers = nvqs;
911 /* Must unset vector notifier while guest notifier is still assigned */
912 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
913 msix_unset_vector_notifiers(&proxy->pci_dev);
914 if (proxy->vector_irqfd) {
915 kvm_virtio_pci_vector_release(proxy, nvqs);
916 g_free(proxy->vector_irqfd);
917 proxy->vector_irqfd = NULL;
921 for (n = 0; n < nvqs; n++) {
922 if (!virtio_queue_get_num(vdev, n)) {
923 break;
926 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
927 if (r < 0) {
928 goto assign_error;
932 /* Must set vector notifier after guest notifier has been assigned */
933 if ((with_irqfd || k->guest_notifier_mask) && assign) {
934 if (with_irqfd) {
935 proxy->vector_irqfd =
936 g_malloc0(sizeof(*proxy->vector_irqfd) *
937 msix_nr_vectors_allocated(&proxy->pci_dev));
938 r = kvm_virtio_pci_vector_use(proxy, nvqs);
939 if (r < 0) {
940 goto assign_error;
943 r = msix_set_vector_notifiers(&proxy->pci_dev,
944 virtio_pci_vector_unmask,
945 virtio_pci_vector_mask,
946 virtio_pci_vector_poll);
947 if (r < 0) {
948 goto notifiers_error;
952 return 0;
954 notifiers_error:
955 if (with_irqfd) {
956 assert(assign);
957 kvm_virtio_pci_vector_release(proxy, nvqs);
960 assign_error:
961 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
962 assert(assign);
963 while (--n >= 0) {
964 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
966 return r;
969 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
971 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
973 /* Stop using ioeventfd for virtqueue kick if the device starts using host
974 * notifiers. This makes it easy to avoid stepping on each others' toes.
976 proxy->ioeventfd_disabled = assign;
977 if (assign) {
978 virtio_pci_stop_ioeventfd(proxy);
980 /* We don't need to start here: it's not needed because backend
981 * currently only stops on status change away from ok,
982 * reset, vmstop and such. If we do add code to start here,
983 * need to check vmstate, device state etc. */
984 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
987 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
989 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
990 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
992 if (running) {
993 /* Old QEMU versions did not set bus master enable on status write.
994 * Detect DRIVER set and enable it.
996 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
997 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
998 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
999 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
1000 proxy->pci_dev.config[PCI_COMMAND] |
1001 PCI_COMMAND_MASTER, 1);
1003 virtio_pci_start_ioeventfd(proxy);
1004 } else {
1005 virtio_pci_stop_ioeventfd(proxy);
1009 #ifdef CONFIG_VIRTFS
1010 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1012 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1013 DeviceState *vdev = DEVICE(&dev->vdev);
1015 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1016 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1019 static Property virtio_9p_pci_properties[] = {
1020 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1021 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1022 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1023 DEFINE_PROP_END_OF_LIST(),
1026 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1028 DeviceClass *dc = DEVICE_CLASS(klass);
1029 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1030 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1032 k->realize = virtio_9p_pci_realize;
1033 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1034 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1035 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1036 pcidev_k->class_id = 0x2;
1037 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1038 dc->props = virtio_9p_pci_properties;
1041 static void virtio_9p_pci_instance_init(Object *obj)
1043 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1045 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1046 TYPE_VIRTIO_9P);
1049 static const TypeInfo virtio_9p_pci_info = {
1050 .name = TYPE_VIRTIO_9P_PCI,
1051 .parent = TYPE_VIRTIO_PCI,
1052 .instance_size = sizeof(V9fsPCIState),
1053 .instance_init = virtio_9p_pci_instance_init,
1054 .class_init = virtio_9p_pci_class_init,
1056 #endif /* CONFIG_VIRTFS */
1059 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1062 static int virtio_pci_query_nvectors(DeviceState *d)
1064 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1066 return proxy->nvectors;
1069 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1070 struct virtio_pci_cap *cap)
1072 PCIDevice *dev = &proxy->pci_dev;
1073 int offset;
1075 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1076 assert(offset > 0);
1078 assert(cap->cap_len >= sizeof *cap);
1079 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1080 cap->cap_len - PCI_CAP_FLAGS);
1082 return offset;
1085 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1086 unsigned size)
1088 VirtIOPCIProxy *proxy = opaque;
1089 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1090 uint32_t val = 0;
1091 int i;
1093 switch (addr) {
1094 case VIRTIO_PCI_COMMON_DFSELECT:
1095 val = proxy->dfselect;
1096 break;
1097 case VIRTIO_PCI_COMMON_DF:
1098 if (proxy->dfselect <= 1) {
1099 val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >>
1100 (32 * proxy->dfselect);
1102 break;
1103 case VIRTIO_PCI_COMMON_GFSELECT:
1104 val = proxy->gfselect;
1105 break;
1106 case VIRTIO_PCI_COMMON_GF:
1107 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1108 val = proxy->guest_features[proxy->gfselect];
1110 break;
1111 case VIRTIO_PCI_COMMON_MSIX:
1112 val = vdev->config_vector;
1113 break;
1114 case VIRTIO_PCI_COMMON_NUMQ:
1115 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1116 if (virtio_queue_get_num(vdev, i)) {
1117 val = i + 1;
1120 break;
1121 case VIRTIO_PCI_COMMON_STATUS:
1122 val = vdev->status;
1123 break;
1124 case VIRTIO_PCI_COMMON_CFGGENERATION:
1125 val = vdev->generation;
1126 break;
1127 case VIRTIO_PCI_COMMON_Q_SELECT:
1128 val = vdev->queue_sel;
1129 break;
1130 case VIRTIO_PCI_COMMON_Q_SIZE:
1131 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1132 break;
1133 case VIRTIO_PCI_COMMON_Q_MSIX:
1134 val = virtio_queue_vector(vdev, vdev->queue_sel);
1135 break;
1136 case VIRTIO_PCI_COMMON_Q_ENABLE:
1137 val = proxy->vqs[vdev->queue_sel].enabled;
1138 break;
1139 case VIRTIO_PCI_COMMON_Q_NOFF:
1140 /* Simply map queues in order */
1141 val = vdev->queue_sel;
1142 break;
1143 case VIRTIO_PCI_COMMON_Q_DESCLO:
1144 val = proxy->vqs[vdev->queue_sel].desc[0];
1145 break;
1146 case VIRTIO_PCI_COMMON_Q_DESCHI:
1147 val = proxy->vqs[vdev->queue_sel].desc[1];
1148 break;
1149 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1150 val = proxy->vqs[vdev->queue_sel].avail[0];
1151 break;
1152 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1153 val = proxy->vqs[vdev->queue_sel].avail[1];
1154 break;
1155 case VIRTIO_PCI_COMMON_Q_USEDLO:
1156 val = proxy->vqs[vdev->queue_sel].used[0];
1157 break;
1158 case VIRTIO_PCI_COMMON_Q_USEDHI:
1159 val = proxy->vqs[vdev->queue_sel].used[1];
1160 break;
1161 default:
1162 val = 0;
1165 return val;
1168 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1169 uint64_t val, unsigned size)
1171 VirtIOPCIProxy *proxy = opaque;
1172 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1174 switch (addr) {
1175 case VIRTIO_PCI_COMMON_DFSELECT:
1176 proxy->dfselect = val;
1177 break;
1178 case VIRTIO_PCI_COMMON_GFSELECT:
1179 proxy->gfselect = val;
1180 break;
1181 case VIRTIO_PCI_COMMON_GF:
1182 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1183 proxy->guest_features[proxy->gfselect] = val;
1184 virtio_set_features(vdev,
1185 (((uint64_t)proxy->guest_features[1]) << 32) |
1186 proxy->guest_features[0]);
1188 break;
1189 case VIRTIO_PCI_COMMON_MSIX:
1190 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1191 /* Make it possible for guest to discover an error took place. */
1192 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1193 val = VIRTIO_NO_VECTOR;
1195 vdev->config_vector = val;
1196 break;
1197 case VIRTIO_PCI_COMMON_STATUS:
1198 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1199 virtio_pci_stop_ioeventfd(proxy);
1202 virtio_set_status(vdev, val & 0xFF);
1204 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1205 virtio_pci_start_ioeventfd(proxy);
1208 if (vdev->status == 0) {
1209 virtio_reset(vdev);
1210 msix_unuse_all_vectors(&proxy->pci_dev);
1213 break;
1214 case VIRTIO_PCI_COMMON_Q_SELECT:
1215 if (val < VIRTIO_QUEUE_MAX) {
1216 vdev->queue_sel = val;
1218 break;
1219 case VIRTIO_PCI_COMMON_Q_SIZE:
1220 proxy->vqs[vdev->queue_sel].num = val;
1221 break;
1222 case VIRTIO_PCI_COMMON_Q_MSIX:
1223 msix_vector_unuse(&proxy->pci_dev,
1224 virtio_queue_vector(vdev, vdev->queue_sel));
1225 /* Make it possible for guest to discover an error took place. */
1226 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1227 val = VIRTIO_NO_VECTOR;
1229 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1230 break;
1231 case VIRTIO_PCI_COMMON_Q_ENABLE:
1232 /* TODO: need a way to put num back on reset. */
1233 virtio_queue_set_num(vdev, vdev->queue_sel,
1234 proxy->vqs[vdev->queue_sel].num);
1235 virtio_queue_set_rings(vdev, vdev->queue_sel,
1236 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1237 proxy->vqs[vdev->queue_sel].desc[0],
1238 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1239 proxy->vqs[vdev->queue_sel].avail[0],
1240 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1241 proxy->vqs[vdev->queue_sel].used[0]);
1242 break;
1243 case VIRTIO_PCI_COMMON_Q_DESCLO:
1244 proxy->vqs[vdev->queue_sel].desc[0] = val;
1245 break;
1246 case VIRTIO_PCI_COMMON_Q_DESCHI:
1247 proxy->vqs[vdev->queue_sel].desc[1] = val;
1248 break;
1249 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1250 proxy->vqs[vdev->queue_sel].avail[0] = val;
1251 break;
1252 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1253 proxy->vqs[vdev->queue_sel].avail[1] = val;
1254 break;
1255 case VIRTIO_PCI_COMMON_Q_USEDLO:
1256 proxy->vqs[vdev->queue_sel].used[0] = val;
1257 break;
1258 case VIRTIO_PCI_COMMON_Q_USEDHI:
1259 proxy->vqs[vdev->queue_sel].used[1] = val;
1260 break;
1261 default:
1262 break;
1267 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1268 unsigned size)
1270 return 0;
1273 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1274 uint64_t val, unsigned size)
1276 VirtIODevice *vdev = opaque;
1277 unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT;
1279 if (queue < VIRTIO_QUEUE_MAX) {
1280 virtio_queue_notify(vdev, queue);
1284 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1285 unsigned size)
1287 VirtIOPCIProxy *proxy = opaque;
1288 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1289 uint64_t val = vdev->isr;
1291 vdev->isr = 0;
1292 pci_irq_deassert(&proxy->pci_dev);
1294 return val;
1297 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1298 uint64_t val, unsigned size)
1302 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1303 unsigned size)
1305 VirtIODevice *vdev = opaque;
1306 uint64_t val = 0;
1308 switch (size) {
1309 case 1:
1310 val = virtio_config_modern_readb(vdev, addr);
1311 break;
1312 case 2:
1313 val = virtio_config_modern_readw(vdev, addr);
1314 break;
1315 case 4:
1316 val = virtio_config_modern_readl(vdev, addr);
1317 break;
1319 return val;
1322 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1323 uint64_t val, unsigned size)
1325 VirtIODevice *vdev = opaque;
1326 switch (size) {
1327 case 1:
1328 virtio_config_modern_writeb(vdev, addr, val);
1329 break;
1330 case 2:
1331 virtio_config_modern_writew(vdev, addr, val);
1332 break;
1333 case 4:
1334 virtio_config_modern_writel(vdev, addr, val);
1335 break;
1339 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1341 static const MemoryRegionOps common_ops = {
1342 .read = virtio_pci_common_read,
1343 .write = virtio_pci_common_write,
1344 .impl = {
1345 .min_access_size = 1,
1346 .max_access_size = 4,
1348 .endianness = DEVICE_LITTLE_ENDIAN,
1350 static const MemoryRegionOps isr_ops = {
1351 .read = virtio_pci_isr_read,
1352 .write = virtio_pci_isr_write,
1353 .impl = {
1354 .min_access_size = 1,
1355 .max_access_size = 4,
1357 .endianness = DEVICE_LITTLE_ENDIAN,
1359 static const MemoryRegionOps device_ops = {
1360 .read = virtio_pci_device_read,
1361 .write = virtio_pci_device_write,
1362 .impl = {
1363 .min_access_size = 1,
1364 .max_access_size = 4,
1366 .endianness = DEVICE_LITTLE_ENDIAN,
1368 static const MemoryRegionOps notify_ops = {
1369 .read = virtio_pci_notify_read,
1370 .write = virtio_pci_notify_write,
1371 .impl = {
1372 .min_access_size = 1,
1373 .max_access_size = 4,
1375 .endianness = DEVICE_LITTLE_ENDIAN,
1378 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1379 &common_ops,
1380 proxy,
1381 "virtio-pci-common",
1382 proxy->common.size);
1384 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1385 &isr_ops,
1386 proxy,
1387 "virtio-pci-isr",
1388 proxy->isr.size);
1390 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1391 &device_ops,
1392 virtio_bus_get_device(&proxy->bus),
1393 "virtio-pci-device",
1394 proxy->device.size);
1396 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1397 &notify_ops,
1398 virtio_bus_get_device(&proxy->bus),
1399 "virtio-pci-notify",
1400 proxy->notify.size);
1403 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1404 VirtIOPCIRegion *region,
1405 struct virtio_pci_cap *cap)
1407 memory_region_add_subregion(&proxy->modern_bar,
1408 region->offset,
1409 &region->mr);
1411 cap->cfg_type = region->type;
1412 cap->bar = proxy->modern_mem_bar;
1413 cap->offset = cpu_to_le32(region->offset);
1414 cap->length = cpu_to_le32(region->size);
1415 virtio_pci_add_mem_cap(proxy, cap);
1418 static void virtio_pci_modern_region_unmap(VirtIOPCIProxy *proxy,
1419 VirtIOPCIRegion *region)
1421 memory_region_del_subregion(&proxy->modern_bar,
1422 &region->mr);
1425 /* This is called by virtio-bus just after the device is plugged. */
1426 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1428 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1429 VirtioBusState *bus = &proxy->bus;
1430 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
1431 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1432 uint8_t *config;
1433 uint32_t size;
1434 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1436 config = proxy->pci_dev.config;
1437 if (proxy->class_code) {
1438 pci_config_set_class(config, proxy->class_code);
1441 if (legacy) {
1442 /* legacy and transitional */
1443 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1444 pci_get_word(config + PCI_VENDOR_ID));
1445 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1446 } else {
1447 /* pure virtio-1.0 */
1448 pci_set_word(config + PCI_VENDOR_ID,
1449 PCI_VENDOR_ID_REDHAT_QUMRANET);
1450 pci_set_word(config + PCI_DEVICE_ID,
1451 0x1040 + virtio_bus_get_vdev_id(bus));
1452 pci_config_set_revision(config, 1);
1454 config[PCI_INTERRUPT_PIN] = 1;
1457 if (modern) {
1458 struct virtio_pci_cap cap = {
1459 .cap_len = sizeof cap,
1461 struct virtio_pci_notify_cap notify = {
1462 .cap.cap_len = sizeof notify,
1463 .notify_off_multiplier =
1464 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
1466 struct virtio_pci_cfg_cap cfg = {
1467 .cap.cap_len = sizeof cfg,
1468 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1470 struct virtio_pci_cfg_cap *cfg_mask;
1472 /* TODO: add io access for speed */
1474 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1475 virtio_pci_modern_regions_init(proxy);
1476 virtio_pci_modern_region_map(proxy, &proxy->common, &cap);
1477 virtio_pci_modern_region_map(proxy, &proxy->isr, &cap);
1478 virtio_pci_modern_region_map(proxy, &proxy->device, &cap);
1479 virtio_pci_modern_region_map(proxy, &proxy->notify, &notify.cap);
1481 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
1482 PCI_BASE_ADDRESS_SPACE_MEMORY |
1483 PCI_BASE_ADDRESS_MEM_PREFETCH |
1484 PCI_BASE_ADDRESS_MEM_TYPE_64,
1485 &proxy->modern_bar);
1487 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1488 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1489 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1490 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1491 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1492 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1495 if (proxy->nvectors) {
1496 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1497 proxy->msix_bar);
1498 if (err) {
1499 /* Notice when a system that supports MSIx can't initialize it. */
1500 if (err != -ENOTSUP) {
1501 error_report("unable to init msix vectors to %" PRIu32,
1502 proxy->nvectors);
1504 proxy->nvectors = 0;
1508 proxy->pci_dev.config_write = virtio_write_config;
1509 proxy->pci_dev.config_read = virtio_read_config;
1511 if (legacy) {
1512 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1513 + virtio_bus_get_vdev_config_len(bus);
1514 size = pow2ceil(size);
1516 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1517 &virtio_pci_config_ops,
1518 proxy, "virtio-pci", size);
1520 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
1521 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1524 if (!kvm_has_many_ioeventfds()) {
1525 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1528 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1531 static void virtio_pci_device_unplugged(DeviceState *d)
1533 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1534 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1536 virtio_pci_stop_ioeventfd(proxy);
1538 if (modern) {
1539 virtio_pci_modern_region_unmap(proxy, &proxy->common);
1540 virtio_pci_modern_region_unmap(proxy, &proxy->isr);
1541 virtio_pci_modern_region_unmap(proxy, &proxy->device);
1542 virtio_pci_modern_region_unmap(proxy, &proxy->notify);
1546 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1548 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1549 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1552 * virtio pci bar layout used by default.
1553 * subclasses can re-arrange things if needed.
1555 * region 0 -- virtio legacy io bar
1556 * region 1 -- msi-x bar
1557 * region 4+5 -- virtio modern memory (64bit) bar
1560 proxy->legacy_io_bar = 0;
1561 proxy->msix_bar = 1;
1562 proxy->modern_mem_bar = 4;
1564 proxy->common.offset = 0x0;
1565 proxy->common.size = 0x1000;
1566 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1568 proxy->isr.offset = 0x1000;
1569 proxy->isr.size = 0x1000;
1570 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1572 proxy->device.offset = 0x2000;
1573 proxy->device.size = 0x1000;
1574 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1576 proxy->notify.offset = 0x3000;
1577 proxy->notify.size =
1578 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX;
1579 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1581 /* subclasses can enforce modern, so do this unconditionally */
1582 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1583 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
1584 VIRTIO_QUEUE_MAX);
1586 memory_region_init_alias(&proxy->modern_cfg,
1587 OBJECT(proxy),
1588 "virtio-pci-cfg",
1589 &proxy->modern_bar,
1591 memory_region_size(&proxy->modern_bar));
1593 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1595 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1596 if (k->realize) {
1597 k->realize(proxy, errp);
1601 static void virtio_pci_exit(PCIDevice *pci_dev)
1603 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1605 msix_uninit_exclusive_bar(pci_dev);
1606 address_space_destroy(&proxy->modern_as);
1609 static void virtio_pci_reset(DeviceState *qdev)
1611 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1612 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1613 virtio_pci_stop_ioeventfd(proxy);
1614 virtio_bus_reset(bus);
1615 msix_unuse_all_vectors(&proxy->pci_dev);
1618 static Property virtio_pci_properties[] = {
1619 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1620 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1621 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags,
1622 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false),
1623 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags,
1624 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true),
1625 DEFINE_PROP_END_OF_LIST(),
1628 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1630 DeviceClass *dc = DEVICE_CLASS(klass);
1631 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1633 dc->props = virtio_pci_properties;
1634 k->realize = virtio_pci_realize;
1635 k->exit = virtio_pci_exit;
1636 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1637 k->revision = VIRTIO_PCI_ABI_VERSION;
1638 k->class_id = PCI_CLASS_OTHERS;
1639 dc->reset = virtio_pci_reset;
1642 static const TypeInfo virtio_pci_info = {
1643 .name = TYPE_VIRTIO_PCI,
1644 .parent = TYPE_PCI_DEVICE,
1645 .instance_size = sizeof(VirtIOPCIProxy),
1646 .class_init = virtio_pci_class_init,
1647 .class_size = sizeof(VirtioPCIClass),
1648 .abstract = true,
1651 /* virtio-blk-pci */
1653 static Property virtio_blk_pci_properties[] = {
1654 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1655 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1656 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1657 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1658 DEFINE_PROP_END_OF_LIST(),
1661 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1663 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1664 DeviceState *vdev = DEVICE(&dev->vdev);
1666 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1667 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1670 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1672 DeviceClass *dc = DEVICE_CLASS(klass);
1673 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1674 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1676 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1677 dc->props = virtio_blk_pci_properties;
1678 k->realize = virtio_blk_pci_realize;
1679 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1680 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1681 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1682 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1685 static void virtio_blk_pci_instance_init(Object *obj)
1687 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1689 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1690 TYPE_VIRTIO_BLK);
1691 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1692 &error_abort);
1693 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1694 "bootindex", &error_abort);
1697 static const TypeInfo virtio_blk_pci_info = {
1698 .name = TYPE_VIRTIO_BLK_PCI,
1699 .parent = TYPE_VIRTIO_PCI,
1700 .instance_size = sizeof(VirtIOBlkPCI),
1701 .instance_init = virtio_blk_pci_instance_init,
1702 .class_init = virtio_blk_pci_class_init,
1705 /* virtio-scsi-pci */
1707 static Property virtio_scsi_pci_properties[] = {
1708 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1709 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1710 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1711 DEV_NVECTORS_UNSPECIFIED),
1712 DEFINE_PROP_END_OF_LIST(),
1715 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1717 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1718 DeviceState *vdev = DEVICE(&dev->vdev);
1719 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1720 DeviceState *proxy = DEVICE(vpci_dev);
1721 char *bus_name;
1723 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1724 vpci_dev->nvectors = vs->conf.num_queues + 3;
1728 * For command line compatibility, this sets the virtio-scsi-device bus
1729 * name as before.
1731 if (proxy->id) {
1732 bus_name = g_strdup_printf("%s.0", proxy->id);
1733 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1734 g_free(bus_name);
1737 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1738 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1741 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1743 DeviceClass *dc = DEVICE_CLASS(klass);
1744 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1745 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1747 k->realize = virtio_scsi_pci_realize;
1748 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1749 dc->props = virtio_scsi_pci_properties;
1750 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1751 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1752 pcidev_k->revision = 0x00;
1753 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1756 static void virtio_scsi_pci_instance_init(Object *obj)
1758 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1760 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1761 TYPE_VIRTIO_SCSI);
1762 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1763 &error_abort);
1766 static const TypeInfo virtio_scsi_pci_info = {
1767 .name = TYPE_VIRTIO_SCSI_PCI,
1768 .parent = TYPE_VIRTIO_PCI,
1769 .instance_size = sizeof(VirtIOSCSIPCI),
1770 .instance_init = virtio_scsi_pci_instance_init,
1771 .class_init = virtio_scsi_pci_class_init,
1774 /* vhost-scsi-pci */
1776 #ifdef CONFIG_VHOST_SCSI
1777 static Property vhost_scsi_pci_properties[] = {
1778 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1779 DEV_NVECTORS_UNSPECIFIED),
1780 DEFINE_PROP_END_OF_LIST(),
1783 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1785 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1786 DeviceState *vdev = DEVICE(&dev->vdev);
1787 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1789 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1790 vpci_dev->nvectors = vs->conf.num_queues + 3;
1793 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1794 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1797 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1799 DeviceClass *dc = DEVICE_CLASS(klass);
1800 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1801 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1802 k->realize = vhost_scsi_pci_realize;
1803 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1804 dc->props = vhost_scsi_pci_properties;
1805 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1806 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1807 pcidev_k->revision = 0x00;
1808 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1811 static void vhost_scsi_pci_instance_init(Object *obj)
1813 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1815 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1816 TYPE_VHOST_SCSI);
1817 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1818 "bootindex", &error_abort);
1821 static const TypeInfo vhost_scsi_pci_info = {
1822 .name = TYPE_VHOST_SCSI_PCI,
1823 .parent = TYPE_VIRTIO_PCI,
1824 .instance_size = sizeof(VHostSCSIPCI),
1825 .instance_init = vhost_scsi_pci_instance_init,
1826 .class_init = vhost_scsi_pci_class_init,
1828 #endif
1830 /* virtio-balloon-pci */
1832 static Property virtio_balloon_pci_properties[] = {
1833 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1834 DEFINE_PROP_END_OF_LIST(),
1837 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1839 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1840 DeviceState *vdev = DEVICE(&dev->vdev);
1842 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1843 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1844 vpci_dev->class_code = PCI_CLASS_OTHERS;
1847 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1848 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1851 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1853 DeviceClass *dc = DEVICE_CLASS(klass);
1854 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1855 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1856 k->realize = virtio_balloon_pci_realize;
1857 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1858 dc->props = virtio_balloon_pci_properties;
1859 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1860 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1861 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1862 pcidev_k->class_id = PCI_CLASS_OTHERS;
1865 static void virtio_balloon_pci_instance_init(Object *obj)
1867 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1869 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1870 TYPE_VIRTIO_BALLOON);
1871 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
1872 "guest-stats", &error_abort);
1873 object_property_add_alias(obj, "guest-stats-polling-interval",
1874 OBJECT(&dev->vdev),
1875 "guest-stats-polling-interval", &error_abort);
1878 static const TypeInfo virtio_balloon_pci_info = {
1879 .name = TYPE_VIRTIO_BALLOON_PCI,
1880 .parent = TYPE_VIRTIO_PCI,
1881 .instance_size = sizeof(VirtIOBalloonPCI),
1882 .instance_init = virtio_balloon_pci_instance_init,
1883 .class_init = virtio_balloon_pci_class_init,
1886 /* virtio-serial-pci */
1888 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1890 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1891 DeviceState *vdev = DEVICE(&dev->vdev);
1892 DeviceState *proxy = DEVICE(vpci_dev);
1893 char *bus_name;
1895 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1896 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1897 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
1898 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1901 /* backwards-compatibility with machines that were created with
1902 DEV_NVECTORS_UNSPECIFIED */
1903 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1904 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1908 * For command line compatibility, this sets the virtio-serial-device bus
1909 * name as before.
1911 if (proxy->id) {
1912 bus_name = g_strdup_printf("%s.0", proxy->id);
1913 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1914 g_free(bus_name);
1917 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1918 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1921 static Property virtio_serial_pci_properties[] = {
1922 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1923 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1924 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1925 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1926 DEFINE_PROP_END_OF_LIST(),
1929 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1931 DeviceClass *dc = DEVICE_CLASS(klass);
1932 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1933 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1934 k->realize = virtio_serial_pci_realize;
1935 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1936 dc->props = virtio_serial_pci_properties;
1937 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1938 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1939 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1940 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1943 static void virtio_serial_pci_instance_init(Object *obj)
1945 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1947 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1948 TYPE_VIRTIO_SERIAL);
1951 static const TypeInfo virtio_serial_pci_info = {
1952 .name = TYPE_VIRTIO_SERIAL_PCI,
1953 .parent = TYPE_VIRTIO_PCI,
1954 .instance_size = sizeof(VirtIOSerialPCI),
1955 .instance_init = virtio_serial_pci_instance_init,
1956 .class_init = virtio_serial_pci_class_init,
1959 /* virtio-net-pci */
1961 static Property virtio_net_properties[] = {
1962 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1963 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1964 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1965 DEFINE_PROP_END_OF_LIST(),
1968 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1970 DeviceState *qdev = DEVICE(vpci_dev);
1971 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1972 DeviceState *vdev = DEVICE(&dev->vdev);
1974 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1975 object_get_typename(OBJECT(qdev)));
1976 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1977 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1980 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1982 DeviceClass *dc = DEVICE_CLASS(klass);
1983 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1984 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1986 k->romfile = "efi-virtio.rom";
1987 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1988 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1989 k->revision = VIRTIO_PCI_ABI_VERSION;
1990 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1991 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1992 dc->props = virtio_net_properties;
1993 vpciklass->realize = virtio_net_pci_realize;
1996 static void virtio_net_pci_instance_init(Object *obj)
1998 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2000 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2001 TYPE_VIRTIO_NET);
2002 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2003 "bootindex", &error_abort);
2006 static const TypeInfo virtio_net_pci_info = {
2007 .name = TYPE_VIRTIO_NET_PCI,
2008 .parent = TYPE_VIRTIO_PCI,
2009 .instance_size = sizeof(VirtIONetPCI),
2010 .instance_init = virtio_net_pci_instance_init,
2011 .class_init = virtio_net_pci_class_init,
2014 /* virtio-rng-pci */
2016 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2018 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2019 DeviceState *vdev = DEVICE(&vrng->vdev);
2020 Error *err = NULL;
2022 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2023 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2024 if (err) {
2025 error_propagate(errp, err);
2026 return;
2029 object_property_set_link(OBJECT(vrng),
2030 OBJECT(vrng->vdev.conf.rng), "rng",
2031 NULL);
2034 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2036 DeviceClass *dc = DEVICE_CLASS(klass);
2037 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2038 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2040 k->realize = virtio_rng_pci_realize;
2041 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2043 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2044 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2045 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2046 pcidev_k->class_id = PCI_CLASS_OTHERS;
2049 static void virtio_rng_initfn(Object *obj)
2051 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2053 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2054 TYPE_VIRTIO_RNG);
2055 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2056 &error_abort);
2059 static const TypeInfo virtio_rng_pci_info = {
2060 .name = TYPE_VIRTIO_RNG_PCI,
2061 .parent = TYPE_VIRTIO_PCI,
2062 .instance_size = sizeof(VirtIORngPCI),
2063 .instance_init = virtio_rng_initfn,
2064 .class_init = virtio_rng_pci_class_init,
2067 /* virtio-input-pci */
2069 static Property virtio_input_pci_properties[] = {
2070 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2071 DEFINE_PROP_END_OF_LIST(),
2074 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2076 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2077 DeviceState *vdev = DEVICE(&vinput->vdev);
2079 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2080 /* force virtio-1.0 */
2081 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
2082 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
2083 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2086 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2088 DeviceClass *dc = DEVICE_CLASS(klass);
2089 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2090 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2092 dc->props = virtio_input_pci_properties;
2093 k->realize = virtio_input_pci_realize;
2094 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2096 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2099 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2101 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2103 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2106 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2107 void *data)
2109 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2111 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2114 static void virtio_keyboard_initfn(Object *obj)
2116 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2118 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2119 TYPE_VIRTIO_KEYBOARD);
2122 static void virtio_mouse_initfn(Object *obj)
2124 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2126 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2127 TYPE_VIRTIO_MOUSE);
2130 static void virtio_tablet_initfn(Object *obj)
2132 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2134 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2135 TYPE_VIRTIO_TABLET);
2138 static const TypeInfo virtio_input_pci_info = {
2139 .name = TYPE_VIRTIO_INPUT_PCI,
2140 .parent = TYPE_VIRTIO_PCI,
2141 .instance_size = sizeof(VirtIOInputPCI),
2142 .class_init = virtio_input_pci_class_init,
2143 .abstract = true,
2146 static const TypeInfo virtio_input_hid_pci_info = {
2147 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2148 .parent = TYPE_VIRTIO_INPUT_PCI,
2149 .instance_size = sizeof(VirtIOInputHIDPCI),
2150 .abstract = true,
2153 static const TypeInfo virtio_keyboard_pci_info = {
2154 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2155 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2156 .class_init = virtio_input_hid_kbd_pci_class_init,
2157 .instance_size = sizeof(VirtIOInputHIDPCI),
2158 .instance_init = virtio_keyboard_initfn,
2161 static const TypeInfo virtio_mouse_pci_info = {
2162 .name = TYPE_VIRTIO_MOUSE_PCI,
2163 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2164 .class_init = virtio_input_hid_mouse_pci_class_init,
2165 .instance_size = sizeof(VirtIOInputHIDPCI),
2166 .instance_init = virtio_mouse_initfn,
2169 static const TypeInfo virtio_tablet_pci_info = {
2170 .name = TYPE_VIRTIO_TABLET_PCI,
2171 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2172 .instance_size = sizeof(VirtIOInputHIDPCI),
2173 .instance_init = virtio_tablet_initfn,
2176 #ifdef CONFIG_LINUX
2177 static void virtio_host_initfn(Object *obj)
2179 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2181 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2182 TYPE_VIRTIO_INPUT_HOST);
2185 static const TypeInfo virtio_host_pci_info = {
2186 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2187 .parent = TYPE_VIRTIO_INPUT_PCI,
2188 .instance_size = sizeof(VirtIOInputHostPCI),
2189 .instance_init = virtio_host_initfn,
2191 #endif
2193 /* virtio-pci-bus */
2195 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2196 VirtIOPCIProxy *dev)
2198 DeviceState *qdev = DEVICE(dev);
2199 char virtio_bus_name[] = "virtio-bus";
2201 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2202 virtio_bus_name);
2205 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2207 BusClass *bus_class = BUS_CLASS(klass);
2208 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2209 bus_class->max_dev = 1;
2210 k->notify = virtio_pci_notify;
2211 k->save_config = virtio_pci_save_config;
2212 k->load_config = virtio_pci_load_config;
2213 k->save_queue = virtio_pci_save_queue;
2214 k->load_queue = virtio_pci_load_queue;
2215 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2216 k->set_host_notifier = virtio_pci_set_host_notifier;
2217 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2218 k->vmstate_change = virtio_pci_vmstate_change;
2219 k->device_plugged = virtio_pci_device_plugged;
2220 k->device_unplugged = virtio_pci_device_unplugged;
2221 k->query_nvectors = virtio_pci_query_nvectors;
2224 static const TypeInfo virtio_pci_bus_info = {
2225 .name = TYPE_VIRTIO_PCI_BUS,
2226 .parent = TYPE_VIRTIO_BUS,
2227 .instance_size = sizeof(VirtioPCIBusState),
2228 .class_init = virtio_pci_bus_class_init,
2231 static void virtio_pci_register_types(void)
2233 type_register_static(&virtio_rng_pci_info);
2234 type_register_static(&virtio_input_pci_info);
2235 type_register_static(&virtio_input_hid_pci_info);
2236 type_register_static(&virtio_keyboard_pci_info);
2237 type_register_static(&virtio_mouse_pci_info);
2238 type_register_static(&virtio_tablet_pci_info);
2239 #ifdef CONFIG_LINUX
2240 type_register_static(&virtio_host_pci_info);
2241 #endif
2242 type_register_static(&virtio_pci_bus_info);
2243 type_register_static(&virtio_pci_info);
2244 #ifdef CONFIG_VIRTFS
2245 type_register_static(&virtio_9p_pci_info);
2246 #endif
2247 type_register_static(&virtio_blk_pci_info);
2248 type_register_static(&virtio_scsi_pci_info);
2249 type_register_static(&virtio_balloon_pci_info);
2250 type_register_static(&virtio_serial_pci_info);
2251 type_register_static(&virtio_net_pci_info);
2252 #ifdef CONFIG_VHOST_SCSI
2253 type_register_static(&vhost_scsi_pci_info);
2254 #endif
2257 type_init(virtio_pci_register_types)