misc/max111x: QOM casting sweep
[qemu.git] / hw / virtio / virtio-pci.c
blob7b91841a1df5edb32a347503d41fa509ca9a7141
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 "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "hw/virtio/virtio-serial.h"
24 #include "hw/virtio/virtio-scsi.h"
25 #include "hw/virtio/virtio-balloon.h"
26 #include "hw/pci/pci.h"
27 #include "qemu/error-report.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/msix.h"
30 #include "hw/loader.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/blockdev.h"
33 #include "virtio-pci.h"
34 #include "qemu/range.h"
35 #include "hw/virtio/virtio-bus.h"
36 #include "qapi/visitor.h"
38 /* from Linux's linux/virtio_pci.h */
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES 0
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES 4
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN 8
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM 12
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL 14
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY 16
58 /* An 8-bit device status register. */
59 #define VIRTIO_PCI_STATUS 18
61 /* An 8-bit r/o interrupt status register. Reading the value will return the
62 * current contents of the ISR and will also clear it. This is effectively
63 * a read-and-acknowledge. */
64 #define VIRTIO_PCI_ISR 19
66 /* MSI-X registers: only enabled if MSI-X is enabled. */
67 /* A 16-bit vector for configuration changes. */
68 #define VIRTIO_MSI_CONFIG_VECTOR 20
69 /* A 16-bit vector for selected queue notifications. */
70 #define VIRTIO_MSI_QUEUE_VECTOR 22
72 /* Config space size */
73 #define VIRTIO_PCI_CONFIG_NOMSI 20
74 #define VIRTIO_PCI_CONFIG_MSI 24
75 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
79 /* The remaining space is defined by each driver as the per-driver
80 * configuration space */
81 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
82 VIRTIO_PCI_CONFIG_MSI : \
83 VIRTIO_PCI_CONFIG_NOMSI)
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86 * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
92 /* HACK for virtio to determine if it's running a big endian guest */
93 bool virtio_is_big_endian(void);
95 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
96 VirtIOPCIProxy *dev);
98 /* virtio device */
99 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
100 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
102 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
105 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
106 * be careful and test performance if you change this.
108 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
110 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
113 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
115 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
117 if (msix_enabled(&proxy->pci_dev))
118 msix_notify(&proxy->pci_dev, vector);
119 else {
120 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
121 pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
125 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
127 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
128 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
130 pci_device_save(&proxy->pci_dev, f);
131 msix_save(&proxy->pci_dev, f);
132 if (msix_present(&proxy->pci_dev))
133 qemu_put_be16(f, vdev->config_vector);
136 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
138 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
139 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
141 if (msix_present(&proxy->pci_dev))
142 qemu_put_be16(f, virtio_queue_vector(vdev, n));
145 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
147 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
148 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
150 int ret;
151 ret = pci_device_load(&proxy->pci_dev, f);
152 if (ret) {
153 return ret;
155 msix_unuse_all_vectors(&proxy->pci_dev);
156 msix_load(&proxy->pci_dev, f);
157 if (msix_present(&proxy->pci_dev)) {
158 qemu_get_be16s(f, &vdev->config_vector);
159 } else {
160 vdev->config_vector = VIRTIO_NO_VECTOR;
162 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
163 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
165 return 0;
168 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
170 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
171 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
173 uint16_t vector;
174 if (msix_present(&proxy->pci_dev)) {
175 qemu_get_be16s(f, &vector);
176 } else {
177 vector = VIRTIO_NO_VECTOR;
179 virtio_queue_set_vector(vdev, n, vector);
180 if (vector != VIRTIO_NO_VECTOR) {
181 return msix_vector_use(&proxy->pci_dev, vector);
183 return 0;
186 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
187 int n, bool assign, bool set_handler)
189 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
190 VirtQueue *vq = virtio_get_queue(vdev, n);
191 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
192 int r = 0;
194 if (assign) {
195 r = event_notifier_init(notifier, 1);
196 if (r < 0) {
197 error_report("%s: unable to init event notifier: %d",
198 __func__, r);
199 return r;
201 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
202 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
203 true, n, notifier);
204 } else {
205 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
206 true, n, notifier);
207 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
208 event_notifier_cleanup(notifier);
210 return r;
213 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
215 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
216 int n, r;
218 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
219 proxy->ioeventfd_disabled ||
220 proxy->ioeventfd_started) {
221 return;
224 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
225 if (!virtio_queue_get_num(vdev, n)) {
226 continue;
229 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
230 if (r < 0) {
231 goto assign_error;
234 proxy->ioeventfd_started = true;
235 return;
237 assign_error:
238 while (--n >= 0) {
239 if (!virtio_queue_get_num(vdev, n)) {
240 continue;
243 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
244 assert(r >= 0);
246 proxy->ioeventfd_started = false;
247 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
250 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
252 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
253 int r;
254 int n;
256 if (!proxy->ioeventfd_started) {
257 return;
260 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
261 if (!virtio_queue_get_num(vdev, n)) {
262 continue;
265 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
266 assert(r >= 0);
268 proxy->ioeventfd_started = false;
271 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
273 VirtIOPCIProxy *proxy = opaque;
274 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
275 hwaddr pa;
277 switch (addr) {
278 case VIRTIO_PCI_GUEST_FEATURES:
279 /* Guest does not negotiate properly? We have to assume nothing. */
280 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
281 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
283 virtio_set_features(vdev, val);
284 break;
285 case VIRTIO_PCI_QUEUE_PFN:
286 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
287 if (pa == 0) {
288 virtio_pci_stop_ioeventfd(proxy);
289 virtio_reset(vdev);
290 msix_unuse_all_vectors(&proxy->pci_dev);
292 else
293 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
294 break;
295 case VIRTIO_PCI_QUEUE_SEL:
296 if (val < VIRTIO_PCI_QUEUE_MAX)
297 vdev->queue_sel = val;
298 break;
299 case VIRTIO_PCI_QUEUE_NOTIFY:
300 if (val < VIRTIO_PCI_QUEUE_MAX) {
301 virtio_queue_notify(vdev, val);
303 break;
304 case VIRTIO_PCI_STATUS:
305 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
306 virtio_pci_stop_ioeventfd(proxy);
309 virtio_set_status(vdev, val & 0xFF);
311 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
312 virtio_pci_start_ioeventfd(proxy);
315 if (vdev->status == 0) {
316 virtio_reset(vdev);
317 msix_unuse_all_vectors(&proxy->pci_dev);
320 /* Linux before 2.6.34 sets the device as OK without enabling
321 the PCI device bus master bit. In this case we need to disable
322 some safety checks. */
323 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
324 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
325 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
327 break;
328 case VIRTIO_MSI_CONFIG_VECTOR:
329 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
330 /* Make it possible for guest to discover an error took place. */
331 if (msix_vector_use(&proxy->pci_dev, val) < 0)
332 val = VIRTIO_NO_VECTOR;
333 vdev->config_vector = val;
334 break;
335 case VIRTIO_MSI_QUEUE_VECTOR:
336 msix_vector_unuse(&proxy->pci_dev,
337 virtio_queue_vector(vdev, vdev->queue_sel));
338 /* Make it possible for guest to discover an error took place. */
339 if (msix_vector_use(&proxy->pci_dev, val) < 0)
340 val = VIRTIO_NO_VECTOR;
341 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
342 break;
343 default:
344 error_report("%s: unexpected address 0x%x value 0x%x",
345 __func__, addr, val);
346 break;
350 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
352 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
353 uint32_t ret = 0xFFFFFFFF;
355 switch (addr) {
356 case VIRTIO_PCI_HOST_FEATURES:
357 ret = proxy->host_features;
358 break;
359 case VIRTIO_PCI_GUEST_FEATURES:
360 ret = vdev->guest_features;
361 break;
362 case VIRTIO_PCI_QUEUE_PFN:
363 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
364 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
365 break;
366 case VIRTIO_PCI_QUEUE_NUM:
367 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
368 break;
369 case VIRTIO_PCI_QUEUE_SEL:
370 ret = vdev->queue_sel;
371 break;
372 case VIRTIO_PCI_STATUS:
373 ret = vdev->status;
374 break;
375 case VIRTIO_PCI_ISR:
376 /* reading from the ISR also clears it. */
377 ret = vdev->isr;
378 vdev->isr = 0;
379 pci_irq_deassert(&proxy->pci_dev);
380 break;
381 case VIRTIO_MSI_CONFIG_VECTOR:
382 ret = vdev->config_vector;
383 break;
384 case VIRTIO_MSI_QUEUE_VECTOR:
385 ret = virtio_queue_vector(vdev, vdev->queue_sel);
386 break;
387 default:
388 break;
391 return ret;
394 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
395 unsigned size)
397 VirtIOPCIProxy *proxy = opaque;
398 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
399 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
400 uint64_t val = 0;
401 if (addr < config) {
402 return virtio_ioport_read(proxy, addr);
404 addr -= config;
406 switch (size) {
407 case 1:
408 val = virtio_config_readb(vdev, addr);
409 break;
410 case 2:
411 val = virtio_config_readw(vdev, addr);
412 if (virtio_is_big_endian()) {
413 val = bswap16(val);
415 break;
416 case 4:
417 val = virtio_config_readl(vdev, addr);
418 if (virtio_is_big_endian()) {
419 val = bswap32(val);
421 break;
423 return val;
426 static void virtio_pci_config_write(void *opaque, hwaddr addr,
427 uint64_t val, unsigned size)
429 VirtIOPCIProxy *proxy = opaque;
430 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
431 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
432 if (addr < config) {
433 virtio_ioport_write(proxy, addr, val);
434 return;
436 addr -= config;
438 * Virtio-PCI is odd. Ioports are LE but config space is target native
439 * endian.
441 switch (size) {
442 case 1:
443 virtio_config_writeb(vdev, addr, val);
444 break;
445 case 2:
446 if (virtio_is_big_endian()) {
447 val = bswap16(val);
449 virtio_config_writew(vdev, addr, val);
450 break;
451 case 4:
452 if (virtio_is_big_endian()) {
453 val = bswap32(val);
455 virtio_config_writel(vdev, addr, val);
456 break;
460 static const MemoryRegionOps virtio_pci_config_ops = {
461 .read = virtio_pci_config_read,
462 .write = virtio_pci_config_write,
463 .impl = {
464 .min_access_size = 1,
465 .max_access_size = 4,
467 .endianness = DEVICE_LITTLE_ENDIAN,
470 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
471 uint32_t val, int len)
473 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
474 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
476 pci_default_write_config(pci_dev, address, val, len);
478 if (range_covers_byte(address, len, PCI_COMMAND) &&
479 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
480 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
481 virtio_pci_stop_ioeventfd(proxy);
482 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
486 static unsigned virtio_pci_get_features(DeviceState *d)
488 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
489 return proxy->host_features;
492 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
493 unsigned int queue_no,
494 unsigned int vector,
495 MSIMessage msg)
497 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
498 int ret;
500 if (irqfd->users == 0) {
501 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
502 if (ret < 0) {
503 return ret;
505 irqfd->virq = ret;
507 irqfd->users++;
508 return 0;
511 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
512 unsigned int vector)
514 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
515 if (--irqfd->users == 0) {
516 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
520 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
521 unsigned int queue_no,
522 unsigned int vector)
524 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
525 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
526 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
527 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
528 int ret;
529 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq);
530 return ret;
533 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
534 unsigned int queue_no,
535 unsigned int vector)
537 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
538 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
539 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
540 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
541 int ret;
543 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
544 assert(ret == 0);
547 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
549 PCIDevice *dev = &proxy->pci_dev;
550 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
551 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
552 unsigned int vector;
553 int ret, queue_no;
554 MSIMessage msg;
556 for (queue_no = 0; queue_no < nvqs; queue_no++) {
557 if (!virtio_queue_get_num(vdev, queue_no)) {
558 break;
560 vector = virtio_queue_vector(vdev, queue_no);
561 if (vector >= msix_nr_vectors_allocated(dev)) {
562 continue;
564 msg = msix_get_message(dev, vector);
565 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
566 if (ret < 0) {
567 goto undo;
569 /* If guest supports masking, set up irqfd now.
570 * Otherwise, delay until unmasked in the frontend.
572 if (k->guest_notifier_mask) {
573 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
574 if (ret < 0) {
575 kvm_virtio_pci_vq_vector_release(proxy, vector);
576 goto undo;
580 return 0;
582 undo:
583 while (--queue_no >= 0) {
584 vector = virtio_queue_vector(vdev, queue_no);
585 if (vector >= msix_nr_vectors_allocated(dev)) {
586 continue;
588 if (k->guest_notifier_mask) {
589 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
591 kvm_virtio_pci_vq_vector_release(proxy, vector);
593 return ret;
596 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
598 PCIDevice *dev = &proxy->pci_dev;
599 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
600 unsigned int vector;
601 int queue_no;
602 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
604 for (queue_no = 0; queue_no < nvqs; queue_no++) {
605 if (!virtio_queue_get_num(vdev, queue_no)) {
606 break;
608 vector = virtio_queue_vector(vdev, queue_no);
609 if (vector >= msix_nr_vectors_allocated(dev)) {
610 continue;
612 /* If guest supports masking, clean up irqfd now.
613 * Otherwise, it was cleaned when masked in the frontend.
615 if (k->guest_notifier_mask) {
616 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
618 kvm_virtio_pci_vq_vector_release(proxy, vector);
622 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
623 unsigned int queue_no,
624 unsigned int vector,
625 MSIMessage msg)
627 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
628 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
629 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
630 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
631 VirtIOIRQFD *irqfd;
632 int ret = 0;
634 if (proxy->vector_irqfd) {
635 irqfd = &proxy->vector_irqfd[vector];
636 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
637 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
638 if (ret < 0) {
639 return ret;
644 /* If guest supports masking, irqfd is already setup, unmask it.
645 * Otherwise, set it up now.
647 if (k->guest_notifier_mask) {
648 k->guest_notifier_mask(vdev, queue_no, false);
649 /* Test after unmasking to avoid losing events. */
650 if (k->guest_notifier_pending &&
651 k->guest_notifier_pending(vdev, queue_no)) {
652 event_notifier_set(n);
654 } else {
655 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
657 return ret;
660 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
661 unsigned int queue_no,
662 unsigned int vector)
664 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
665 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
667 /* If guest supports masking, keep irqfd but mask it.
668 * Otherwise, clean it up now.
670 if (k->guest_notifier_mask) {
671 k->guest_notifier_mask(vdev, queue_no, true);
672 } else {
673 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
677 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
678 MSIMessage msg)
680 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
681 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
682 int ret, queue_no;
684 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
685 if (!virtio_queue_get_num(vdev, queue_no)) {
686 break;
688 if (virtio_queue_vector(vdev, queue_no) != vector) {
689 continue;
691 ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg);
692 if (ret < 0) {
693 goto undo;
696 return 0;
698 undo:
699 while (--queue_no >= 0) {
700 if (virtio_queue_vector(vdev, queue_no) != vector) {
701 continue;
703 virtio_pci_vq_vector_mask(proxy, queue_no, vector);
705 return ret;
708 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
710 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
711 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
712 int queue_no;
714 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
715 if (!virtio_queue_get_num(vdev, queue_no)) {
716 break;
718 if (virtio_queue_vector(vdev, queue_no) != vector) {
719 continue;
721 virtio_pci_vq_vector_mask(proxy, queue_no, vector);
725 static void virtio_pci_vector_poll(PCIDevice *dev,
726 unsigned int vector_start,
727 unsigned int vector_end)
729 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
730 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
731 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
732 int queue_no;
733 unsigned int vector;
734 EventNotifier *notifier;
735 VirtQueue *vq;
737 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
738 if (!virtio_queue_get_num(vdev, queue_no)) {
739 break;
741 vector = virtio_queue_vector(vdev, queue_no);
742 if (vector < vector_start || vector >= vector_end ||
743 !msix_is_masked(dev, vector)) {
744 continue;
746 vq = virtio_get_queue(vdev, queue_no);
747 notifier = virtio_queue_get_guest_notifier(vq);
748 if (k->guest_notifier_pending) {
749 if (k->guest_notifier_pending(vdev, queue_no)) {
750 msix_set_pending(dev, vector);
752 } else if (event_notifier_test_and_clear(notifier)) {
753 msix_set_pending(dev, vector);
758 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
759 bool with_irqfd)
761 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
762 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
763 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
764 VirtQueue *vq = virtio_get_queue(vdev, n);
765 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
767 if (assign) {
768 int r = event_notifier_init(notifier, 0);
769 if (r < 0) {
770 return r;
772 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
773 } else {
774 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
775 event_notifier_cleanup(notifier);
778 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
779 vdc->guest_notifier_mask(vdev, n, !assign);
782 return 0;
785 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
787 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
788 return msix_enabled(&proxy->pci_dev);
791 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
793 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
794 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
795 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
796 int r, n;
797 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
798 kvm_msi_via_irqfd_enabled();
800 nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX);
802 /* When deassigning, pass a consistent nvqs value
803 * to avoid leaking notifiers.
805 assert(assign || nvqs == proxy->nvqs_with_notifiers);
807 proxy->nvqs_with_notifiers = nvqs;
809 /* Must unset vector notifier while guest notifier is still assigned */
810 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
811 msix_unset_vector_notifiers(&proxy->pci_dev);
812 if (proxy->vector_irqfd) {
813 kvm_virtio_pci_vector_release(proxy, nvqs);
814 g_free(proxy->vector_irqfd);
815 proxy->vector_irqfd = NULL;
819 for (n = 0; n < nvqs; n++) {
820 if (!virtio_queue_get_num(vdev, n)) {
821 break;
824 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
825 if (r < 0) {
826 goto assign_error;
830 /* Must set vector notifier after guest notifier has been assigned */
831 if ((with_irqfd || k->guest_notifier_mask) && assign) {
832 if (with_irqfd) {
833 proxy->vector_irqfd =
834 g_malloc0(sizeof(*proxy->vector_irqfd) *
835 msix_nr_vectors_allocated(&proxy->pci_dev));
836 r = kvm_virtio_pci_vector_use(proxy, nvqs);
837 if (r < 0) {
838 goto assign_error;
841 r = msix_set_vector_notifiers(&proxy->pci_dev,
842 virtio_pci_vector_unmask,
843 virtio_pci_vector_mask,
844 virtio_pci_vector_poll);
845 if (r < 0) {
846 goto notifiers_error;
850 return 0;
852 notifiers_error:
853 if (with_irqfd) {
854 assert(assign);
855 kvm_virtio_pci_vector_release(proxy, nvqs);
858 assign_error:
859 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
860 assert(assign);
861 while (--n >= 0) {
862 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
864 return r;
867 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
869 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
871 /* Stop using ioeventfd for virtqueue kick if the device starts using host
872 * notifiers. This makes it easy to avoid stepping on each others' toes.
874 proxy->ioeventfd_disabled = assign;
875 if (assign) {
876 virtio_pci_stop_ioeventfd(proxy);
878 /* We don't need to start here: it's not needed because backend
879 * currently only stops on status change away from ok,
880 * reset, vmstop and such. If we do add code to start here,
881 * need to check vmstate, device state etc. */
882 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
885 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
887 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
888 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
890 if (running) {
891 /* Try to find out if the guest has bus master disabled, but is
892 in ready state. Then we have a buggy guest OS. */
893 if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
894 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
895 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
897 virtio_pci_start_ioeventfd(proxy);
898 } else {
899 virtio_pci_stop_ioeventfd(proxy);
903 #ifdef CONFIG_VIRTFS
904 static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev)
906 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
907 DeviceState *vdev = DEVICE(&dev->vdev);
909 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
910 if (qdev_init(vdev) < 0) {
911 return -1;
913 return 0;
916 static Property virtio_9p_pci_properties[] = {
917 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
918 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
919 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
920 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
921 DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState, vdev.fsconf),
922 DEFINE_PROP_END_OF_LIST(),
925 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
927 DeviceClass *dc = DEVICE_CLASS(klass);
928 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
929 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
931 k->init = virtio_9p_init_pci;
932 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
933 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
934 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
935 pcidev_k->class_id = 0x2;
936 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
937 dc->props = virtio_9p_pci_properties;
940 static void virtio_9p_pci_instance_init(Object *obj)
942 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
943 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_9P);
944 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
947 static const TypeInfo virtio_9p_pci_info = {
948 .name = TYPE_VIRTIO_9P_PCI,
949 .parent = TYPE_VIRTIO_PCI,
950 .instance_size = sizeof(V9fsPCIState),
951 .instance_init = virtio_9p_pci_instance_init,
952 .class_init = virtio_9p_pci_class_init,
954 #endif /* CONFIG_VIRTFS */
957 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
960 /* This is called by virtio-bus just after the device is plugged. */
961 static void virtio_pci_device_plugged(DeviceState *d)
963 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
964 VirtioBusState *bus = &proxy->bus;
965 uint8_t *config;
966 uint32_t size;
968 config = proxy->pci_dev.config;
969 if (proxy->class_code) {
970 pci_config_set_class(config, proxy->class_code);
972 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
973 pci_get_word(config + PCI_VENDOR_ID));
974 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
975 config[PCI_INTERRUPT_PIN] = 1;
977 if (proxy->nvectors &&
978 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
979 proxy->nvectors = 0;
982 proxy->pci_dev.config_write = virtio_write_config;
984 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
985 + virtio_bus_get_vdev_config_len(bus);
986 if (size & (size - 1)) {
987 size = 1 << qemu_fls(size);
990 memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops,
991 proxy, "virtio-pci", size);
992 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
993 &proxy->bar);
995 if (!kvm_has_many_ioeventfds()) {
996 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
999 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
1000 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
1001 proxy->host_features = virtio_bus_get_vdev_features(bus,
1002 proxy->host_features);
1005 static void virtio_pci_device_unplugged(DeviceState *d)
1007 PCIDevice *pci_dev = PCI_DEVICE(d);
1008 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1010 virtio_pci_stop_ioeventfd(proxy);
1011 msix_uninit_exclusive_bar(pci_dev);
1014 static int virtio_pci_init(PCIDevice *pci_dev)
1016 VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
1017 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1018 virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev);
1019 if (k->init != NULL) {
1020 return k->init(dev);
1022 return 0;
1025 static void virtio_pci_exit(PCIDevice *pci_dev)
1027 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1028 memory_region_destroy(&proxy->bar);
1031 static void virtio_pci_reset(DeviceState *qdev)
1033 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1034 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1035 virtio_pci_stop_ioeventfd(proxy);
1036 virtio_bus_reset(bus);
1037 msix_unuse_all_vectors(&proxy->pci_dev);
1038 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
1041 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1043 DeviceClass *dc = DEVICE_CLASS(klass);
1044 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1046 k->init = virtio_pci_init;
1047 k->exit = virtio_pci_exit;
1048 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1049 k->revision = VIRTIO_PCI_ABI_VERSION;
1050 k->class_id = PCI_CLASS_OTHERS;
1051 dc->reset = virtio_pci_reset;
1054 static const TypeInfo virtio_pci_info = {
1055 .name = TYPE_VIRTIO_PCI,
1056 .parent = TYPE_PCI_DEVICE,
1057 .instance_size = sizeof(VirtIOPCIProxy),
1058 .class_init = virtio_pci_class_init,
1059 .class_size = sizeof(VirtioPCIClass),
1060 .abstract = true,
1063 /* virtio-blk-pci */
1065 static Property virtio_blk_pci_properties[] = {
1066 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1067 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1068 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1069 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1070 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1071 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false),
1072 #endif
1073 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
1074 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk),
1075 DEFINE_PROP_END_OF_LIST(),
1078 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev)
1080 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1081 DeviceState *vdev = DEVICE(&dev->vdev);
1082 virtio_blk_set_conf(vdev, &(dev->blk));
1083 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1084 if (qdev_init(vdev) < 0) {
1085 return -1;
1087 return 0;
1090 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1092 DeviceClass *dc = DEVICE_CLASS(klass);
1093 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1094 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1096 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1097 dc->props = virtio_blk_pci_properties;
1098 k->init = virtio_blk_pci_init;
1099 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1100 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1101 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1102 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1105 static void virtio_blk_pci_instance_init(Object *obj)
1107 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1108 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK);
1109 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1112 static const TypeInfo virtio_blk_pci_info = {
1113 .name = TYPE_VIRTIO_BLK_PCI,
1114 .parent = TYPE_VIRTIO_PCI,
1115 .instance_size = sizeof(VirtIOBlkPCI),
1116 .instance_init = virtio_blk_pci_instance_init,
1117 .class_init = virtio_blk_pci_class_init,
1120 /* virtio-scsi-pci */
1122 static Property virtio_scsi_pci_properties[] = {
1123 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1124 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1125 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1126 DEV_NVECTORS_UNSPECIFIED),
1127 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features),
1128 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf),
1129 DEFINE_PROP_END_OF_LIST(),
1132 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1134 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1135 DeviceState *vdev = DEVICE(&dev->vdev);
1136 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1137 DeviceState *proxy = DEVICE(vpci_dev);
1138 char *bus_name;
1140 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1141 vpci_dev->nvectors = vs->conf.num_queues + 3;
1145 * For command line compatibility, this sets the virtio-scsi-device bus
1146 * name as before.
1148 if (proxy->id) {
1149 bus_name = g_strdup_printf("%s.0", proxy->id);
1150 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1151 g_free(bus_name);
1154 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1155 if (qdev_init(vdev) < 0) {
1156 return -1;
1158 return 0;
1161 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1163 DeviceClass *dc = DEVICE_CLASS(klass);
1164 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1165 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1166 k->init = virtio_scsi_pci_init_pci;
1167 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1168 dc->props = virtio_scsi_pci_properties;
1169 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1170 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1171 pcidev_k->revision = 0x00;
1172 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1175 static void virtio_scsi_pci_instance_init(Object *obj)
1177 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1178 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI);
1179 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1182 static const TypeInfo virtio_scsi_pci_info = {
1183 .name = TYPE_VIRTIO_SCSI_PCI,
1184 .parent = TYPE_VIRTIO_PCI,
1185 .instance_size = sizeof(VirtIOSCSIPCI),
1186 .instance_init = virtio_scsi_pci_instance_init,
1187 .class_init = virtio_scsi_pci_class_init,
1190 /* vhost-scsi-pci */
1192 #ifdef CONFIG_VHOST_SCSI
1193 static Property vhost_scsi_pci_properties[] = {
1194 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1195 DEV_NVECTORS_UNSPECIFIED),
1196 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1197 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf),
1198 DEFINE_PROP_END_OF_LIST(),
1201 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1203 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1204 DeviceState *vdev = DEVICE(&dev->vdev);
1205 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1207 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1208 vpci_dev->nvectors = vs->conf.num_queues + 3;
1211 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1212 if (qdev_init(vdev) < 0) {
1213 return -1;
1215 return 0;
1218 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1220 DeviceClass *dc = DEVICE_CLASS(klass);
1221 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1222 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1223 k->init = vhost_scsi_pci_init_pci;
1224 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1225 dc->props = vhost_scsi_pci_properties;
1226 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1227 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1228 pcidev_k->revision = 0x00;
1229 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1232 static void vhost_scsi_pci_instance_init(Object *obj)
1234 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1235 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI);
1236 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1239 static const TypeInfo vhost_scsi_pci_info = {
1240 .name = TYPE_VHOST_SCSI_PCI,
1241 .parent = TYPE_VIRTIO_PCI,
1242 .instance_size = sizeof(VHostSCSIPCI),
1243 .instance_init = vhost_scsi_pci_instance_init,
1244 .class_init = vhost_scsi_pci_class_init,
1246 #endif
1248 /* virtio-balloon-pci */
1250 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v,
1251 void *opaque, const char *name,
1252 Error **errp)
1254 VirtIOBalloonPCI *dev = opaque;
1255 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
1258 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v,
1259 void *opaque, const char *name,
1260 Error **errp)
1262 VirtIOBalloonPCI *dev = opaque;
1263 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1264 errp);
1267 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v,
1268 void *opaque, const char *name,
1269 Error **errp)
1271 VirtIOBalloonPCI *dev = opaque;
1272 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1273 errp);
1276 static Property virtio_balloon_pci_properties[] = {
1277 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1278 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1279 DEFINE_PROP_END_OF_LIST(),
1282 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
1284 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1285 DeviceState *vdev = DEVICE(&dev->vdev);
1287 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1288 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1289 vpci_dev->class_code = PCI_CLASS_OTHERS;
1292 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1293 if (qdev_init(vdev) < 0) {
1294 return -1;
1296 return 0;
1299 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1301 DeviceClass *dc = DEVICE_CLASS(klass);
1302 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1303 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1304 k->init = virtio_balloon_pci_init;
1305 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1306 dc->props = virtio_balloon_pci_properties;
1307 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1308 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1309 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1310 pcidev_k->class_id = PCI_CLASS_OTHERS;
1313 static void virtio_balloon_pci_instance_init(Object *obj)
1315 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1316 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
1317 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1319 object_property_add(obj, "guest-stats", "guest statistics",
1320 balloon_pci_stats_get_all, NULL, NULL, dev,
1321 NULL);
1323 object_property_add(obj, "guest-stats-polling-interval", "int",
1324 balloon_pci_stats_get_poll_interval,
1325 balloon_pci_stats_set_poll_interval,
1326 NULL, dev, NULL);
1329 static const TypeInfo virtio_balloon_pci_info = {
1330 .name = TYPE_VIRTIO_BALLOON_PCI,
1331 .parent = TYPE_VIRTIO_PCI,
1332 .instance_size = sizeof(VirtIOBalloonPCI),
1333 .instance_init = virtio_balloon_pci_instance_init,
1334 .class_init = virtio_balloon_pci_class_init,
1337 /* virtio-serial-pci */
1339 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev)
1341 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1342 DeviceState *vdev = DEVICE(&dev->vdev);
1343 DeviceState *proxy = DEVICE(vpci_dev);
1344 char *bus_name;
1346 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1347 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1348 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
1349 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1352 /* backwards-compatibility with machines that were created with
1353 DEV_NVECTORS_UNSPECIFIED */
1354 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1355 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1359 * For command line compatibility, this sets the virtio-serial-device bus
1360 * name as before.
1362 if (proxy->id) {
1363 bus_name = g_strdup_printf("%s.0", proxy->id);
1364 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1365 g_free(bus_name);
1368 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1369 if (qdev_init(vdev) < 0) {
1370 return -1;
1372 return 0;
1375 static Property virtio_serial_pci_properties[] = {
1376 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1377 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1378 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1379 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1380 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1381 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial),
1382 DEFINE_PROP_END_OF_LIST(),
1385 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1387 DeviceClass *dc = DEVICE_CLASS(klass);
1388 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1389 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1390 k->init = virtio_serial_pci_init;
1391 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1392 dc->props = virtio_serial_pci_properties;
1393 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1394 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1395 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1396 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1399 static void virtio_serial_pci_instance_init(Object *obj)
1401 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1402 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL);
1403 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1406 static const TypeInfo virtio_serial_pci_info = {
1407 .name = TYPE_VIRTIO_SERIAL_PCI,
1408 .parent = TYPE_VIRTIO_PCI,
1409 .instance_size = sizeof(VirtIOSerialPCI),
1410 .instance_init = virtio_serial_pci_instance_init,
1411 .class_init = virtio_serial_pci_class_init,
1414 /* virtio-net-pci */
1416 static Property virtio_net_properties[] = {
1417 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1418 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1419 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1420 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
1421 DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf),
1422 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf),
1423 DEFINE_PROP_END_OF_LIST(),
1426 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev)
1428 DeviceState *qdev = DEVICE(vpci_dev);
1429 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1430 DeviceState *vdev = DEVICE(&dev->vdev);
1432 virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features);
1433 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1434 object_get_typename(OBJECT(qdev)));
1435 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1436 if (qdev_init(vdev) < 0) {
1437 return -1;
1439 return 0;
1442 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1444 DeviceClass *dc = DEVICE_CLASS(klass);
1445 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1446 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1448 k->romfile = "efi-virtio.rom";
1449 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1450 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1451 k->revision = VIRTIO_PCI_ABI_VERSION;
1452 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1453 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1454 dc->props = virtio_net_properties;
1455 vpciklass->init = virtio_net_pci_init;
1458 static void virtio_net_pci_instance_init(Object *obj)
1460 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
1461 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET);
1462 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1465 static const TypeInfo virtio_net_pci_info = {
1466 .name = TYPE_VIRTIO_NET_PCI,
1467 .parent = TYPE_VIRTIO_PCI,
1468 .instance_size = sizeof(VirtIONetPCI),
1469 .instance_init = virtio_net_pci_instance_init,
1470 .class_init = virtio_net_pci_class_init,
1473 /* virtio-rng-pci */
1475 static Property virtio_rng_pci_properties[] = {
1476 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1477 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf),
1478 DEFINE_PROP_END_OF_LIST(),
1481 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev)
1483 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
1484 DeviceState *vdev = DEVICE(&vrng->vdev);
1486 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1487 if (qdev_init(vdev) < 0) {
1488 return -1;
1491 object_property_set_link(OBJECT(vrng),
1492 OBJECT(vrng->vdev.conf.rng), "rng",
1493 NULL);
1495 return 0;
1498 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
1500 DeviceClass *dc = DEVICE_CLASS(klass);
1501 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1502 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1504 k->init = virtio_rng_pci_init;
1505 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1506 dc->props = virtio_rng_pci_properties;
1508 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1509 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1510 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1511 pcidev_k->class_id = PCI_CLASS_OTHERS;
1514 static void virtio_rng_initfn(Object *obj)
1516 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
1517 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG);
1518 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1519 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1520 (Object **)&dev->vdev.conf.rng, NULL);
1524 static const TypeInfo virtio_rng_pci_info = {
1525 .name = TYPE_VIRTIO_RNG_PCI,
1526 .parent = TYPE_VIRTIO_PCI,
1527 .instance_size = sizeof(VirtIORngPCI),
1528 .instance_init = virtio_rng_initfn,
1529 .class_init = virtio_rng_pci_class_init,
1532 /* virtio-pci-bus */
1534 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
1535 VirtIOPCIProxy *dev)
1537 DeviceState *qdev = DEVICE(dev);
1538 BusState *qbus;
1539 char virtio_bus_name[] = "virtio-bus";
1541 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
1542 virtio_bus_name);
1543 qbus = BUS(bus);
1544 qbus->allow_hotplug = 1;
1547 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
1549 BusClass *bus_class = BUS_CLASS(klass);
1550 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1551 bus_class->max_dev = 1;
1552 k->notify = virtio_pci_notify;
1553 k->save_config = virtio_pci_save_config;
1554 k->load_config = virtio_pci_load_config;
1555 k->save_queue = virtio_pci_save_queue;
1556 k->load_queue = virtio_pci_load_queue;
1557 k->get_features = virtio_pci_get_features;
1558 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
1559 k->set_host_notifier = virtio_pci_set_host_notifier;
1560 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
1561 k->vmstate_change = virtio_pci_vmstate_change;
1562 k->device_plugged = virtio_pci_device_plugged;
1563 k->device_unplugged = virtio_pci_device_unplugged;
1566 static const TypeInfo virtio_pci_bus_info = {
1567 .name = TYPE_VIRTIO_PCI_BUS,
1568 .parent = TYPE_VIRTIO_BUS,
1569 .instance_size = sizeof(VirtioPCIBusState),
1570 .class_init = virtio_pci_bus_class_init,
1573 static void virtio_pci_register_types(void)
1575 type_register_static(&virtio_rng_pci_info);
1576 type_register_static(&virtio_pci_bus_info);
1577 type_register_static(&virtio_pci_info);
1578 #ifdef CONFIG_VIRTFS
1579 type_register_static(&virtio_9p_pci_info);
1580 #endif
1581 type_register_static(&virtio_blk_pci_info);
1582 type_register_static(&virtio_scsi_pci_info);
1583 type_register_static(&virtio_balloon_pci_info);
1584 type_register_static(&virtio_serial_pci_info);
1585 type_register_static(&virtio_net_pci_info);
1586 #ifdef CONFIG_VHOST_SCSI
1587 type_register_static(&vhost_scsi_pci_info);
1588 #endif
1591 type_init(virtio_pci_register_types)