hw/pxa2xx_timer: Explicitly mark fallthroughs
[qemu/pbrook.git] / hw / virtio-pci.c
blob9abbcdfc7c125f306f53a400bf2133766af616ad
1 /*
2 * Virtio PCI Bindings
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
18 #include <inttypes.h>
20 #include "virtio.h"
21 #include "virtio-blk.h"
22 #include "virtio-net.h"
23 #include "virtio-serial.h"
24 #include "virtio-scsi.h"
25 #include "pci/pci.h"
26 #include "qemu/error-report.h"
27 #include "pci/msi.h"
28 #include "pci/msix.h"
29 #include "loader.h"
30 #include "sysemu/kvm.h"
31 #include "sysemu/blockdev.h"
32 #include "virtio-pci.h"
33 #include "qemu/range.h"
34 #include "virtio-bus.h"
36 /* from Linux's linux/virtio_pci.h */
38 /* A 32-bit r/o bitmask of the features supported by the host */
39 #define VIRTIO_PCI_HOST_FEATURES 0
41 /* A 32-bit r/w bitmask of features activated by the guest */
42 #define VIRTIO_PCI_GUEST_FEATURES 4
44 /* A 32-bit r/w PFN for the currently selected queue */
45 #define VIRTIO_PCI_QUEUE_PFN 8
47 /* A 16-bit r/o queue size for the currently selected queue */
48 #define VIRTIO_PCI_QUEUE_NUM 12
50 /* A 16-bit r/w queue selector */
51 #define VIRTIO_PCI_QUEUE_SEL 14
53 /* A 16-bit r/w queue notifier */
54 #define VIRTIO_PCI_QUEUE_NOTIFY 16
56 /* An 8-bit device status register. */
57 #define VIRTIO_PCI_STATUS 18
59 /* An 8-bit r/o interrupt status register. Reading the value will return the
60 * current contents of the ISR and will also clear it. This is effectively
61 * a read-and-acknowledge. */
62 #define VIRTIO_PCI_ISR 19
64 /* MSI-X registers: only enabled if MSI-X is enabled. */
65 /* A 16-bit vector for configuration changes. */
66 #define VIRTIO_MSI_CONFIG_VECTOR 20
67 /* A 16-bit vector for selected queue notifications. */
68 #define VIRTIO_MSI_QUEUE_VECTOR 22
70 /* Config space size */
71 #define VIRTIO_PCI_CONFIG_NOMSI 20
72 #define VIRTIO_PCI_CONFIG_MSI 24
73 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
74 VIRTIO_PCI_CONFIG_MSI : \
75 VIRTIO_PCI_CONFIG_NOMSI)
77 /* The remaining space is defined by each driver as the per-driver
78 * configuration space */
79 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
80 VIRTIO_PCI_CONFIG_MSI : \
81 VIRTIO_PCI_CONFIG_NOMSI)
83 /* How many bits to shift physical queue address written to QUEUE_PFN.
84 * 12 is historical, and due to x86 page size. */
85 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
87 /* Flags track per-device state like workarounds for quirks in older guests. */
88 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
90 /* QEMU doesn't strictly need write barriers since everything runs in
91 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
92 * KVM or if kqemu gets SMP support.
94 #define wmb() do { } while (0)
96 /* HACK for virtio to determine if it's running a big endian guest */
97 bool virtio_is_big_endian(void);
99 /* virtio device */
100 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
101 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
103 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
106 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
107 * be careful and test performance if you change this.
109 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
111 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
114 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
116 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 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
123 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
125 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
126 pci_device_save(&proxy->pci_dev, f);
127 msix_save(&proxy->pci_dev, f);
128 if (msix_present(&proxy->pci_dev))
129 qemu_put_be16(f, proxy->vdev->config_vector);
132 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
134 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
135 if (msix_present(&proxy->pci_dev))
136 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
139 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
141 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
142 int ret;
143 ret = pci_device_load(&proxy->pci_dev, f);
144 if (ret) {
145 return ret;
147 msix_unuse_all_vectors(&proxy->pci_dev);
148 msix_load(&proxy->pci_dev, f);
149 if (msix_present(&proxy->pci_dev)) {
150 qemu_get_be16s(f, &proxy->vdev->config_vector);
151 } else {
152 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
154 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
155 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
157 return 0;
160 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
162 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
163 uint16_t vector;
164 if (msix_present(&proxy->pci_dev)) {
165 qemu_get_be16s(f, &vector);
166 } else {
167 vector = VIRTIO_NO_VECTOR;
169 virtio_queue_set_vector(proxy->vdev, n, vector);
170 if (vector != VIRTIO_NO_VECTOR) {
171 return msix_vector_use(&proxy->pci_dev, vector);
173 return 0;
176 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
177 int n, bool assign, bool set_handler)
179 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
180 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
181 int r = 0;
183 if (assign) {
184 r = event_notifier_init(notifier, 1);
185 if (r < 0) {
186 error_report("%s: unable to init event notifier: %d",
187 __func__, r);
188 return r;
190 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
191 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
192 true, n, notifier);
193 } else {
194 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
195 true, n, notifier);
196 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
197 event_notifier_cleanup(notifier);
199 return r;
202 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
204 int n, r;
206 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
207 proxy->ioeventfd_disabled ||
208 proxy->ioeventfd_started) {
209 return;
212 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
213 if (!virtio_queue_get_num(proxy->vdev, n)) {
214 continue;
217 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
218 if (r < 0) {
219 goto assign_error;
222 proxy->ioeventfd_started = true;
223 return;
225 assign_error:
226 while (--n >= 0) {
227 if (!virtio_queue_get_num(proxy->vdev, n)) {
228 continue;
231 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
232 assert(r >= 0);
234 proxy->ioeventfd_started = false;
235 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
238 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
240 int r;
241 int n;
243 if (!proxy->ioeventfd_started) {
244 return;
247 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
248 if (!virtio_queue_get_num(proxy->vdev, n)) {
249 continue;
252 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
253 assert(r >= 0);
255 proxy->ioeventfd_started = false;
258 void virtio_pci_reset(DeviceState *d)
260 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
261 virtio_pci_stop_ioeventfd(proxy);
262 virtio_reset(proxy->vdev);
263 msix_unuse_all_vectors(&proxy->pci_dev);
264 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
267 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
269 VirtIOPCIProxy *proxy = opaque;
270 VirtIODevice *vdev = proxy->vdev;
271 hwaddr pa;
273 switch (addr) {
274 case VIRTIO_PCI_GUEST_FEATURES:
275 /* Guest does not negotiate properly? We have to assume nothing. */
276 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
277 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
279 virtio_set_features(vdev, val);
280 break;
281 case VIRTIO_PCI_QUEUE_PFN:
282 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
283 if (pa == 0) {
284 virtio_pci_stop_ioeventfd(proxy);
285 virtio_reset(proxy->vdev);
286 msix_unuse_all_vectors(&proxy->pci_dev);
288 else
289 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
290 break;
291 case VIRTIO_PCI_QUEUE_SEL:
292 if (val < VIRTIO_PCI_QUEUE_MAX)
293 vdev->queue_sel = val;
294 break;
295 case VIRTIO_PCI_QUEUE_NOTIFY:
296 if (val < VIRTIO_PCI_QUEUE_MAX) {
297 virtio_queue_notify(vdev, val);
299 break;
300 case VIRTIO_PCI_STATUS:
301 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
302 virtio_pci_stop_ioeventfd(proxy);
305 virtio_set_status(vdev, val & 0xFF);
307 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
308 virtio_pci_start_ioeventfd(proxy);
311 if (vdev->status == 0) {
312 virtio_reset(proxy->vdev);
313 msix_unuse_all_vectors(&proxy->pci_dev);
316 /* Linux before 2.6.34 sets the device as OK without enabling
317 the PCI device bus master bit. In this case we need to disable
318 some safety checks. */
319 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
320 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
321 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
323 break;
324 case VIRTIO_MSI_CONFIG_VECTOR:
325 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
326 /* Make it possible for guest to discover an error took place. */
327 if (msix_vector_use(&proxy->pci_dev, val) < 0)
328 val = VIRTIO_NO_VECTOR;
329 vdev->config_vector = val;
330 break;
331 case VIRTIO_MSI_QUEUE_VECTOR:
332 msix_vector_unuse(&proxy->pci_dev,
333 virtio_queue_vector(vdev, vdev->queue_sel));
334 /* Make it possible for guest to discover an error took place. */
335 if (msix_vector_use(&proxy->pci_dev, val) < 0)
336 val = VIRTIO_NO_VECTOR;
337 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
338 break;
339 default:
340 error_report("%s: unexpected address 0x%x value 0x%x",
341 __func__, addr, val);
342 break;
346 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
348 VirtIODevice *vdev = proxy->vdev;
349 uint32_t ret = 0xFFFFFFFF;
351 switch (addr) {
352 case VIRTIO_PCI_HOST_FEATURES:
353 ret = proxy->host_features;
354 break;
355 case VIRTIO_PCI_GUEST_FEATURES:
356 ret = vdev->guest_features;
357 break;
358 case VIRTIO_PCI_QUEUE_PFN:
359 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
360 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
361 break;
362 case VIRTIO_PCI_QUEUE_NUM:
363 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
364 break;
365 case VIRTIO_PCI_QUEUE_SEL:
366 ret = vdev->queue_sel;
367 break;
368 case VIRTIO_PCI_STATUS:
369 ret = vdev->status;
370 break;
371 case VIRTIO_PCI_ISR:
372 /* reading from the ISR also clears it. */
373 ret = vdev->isr;
374 vdev->isr = 0;
375 qemu_set_irq(proxy->pci_dev.irq[0], 0);
376 break;
377 case VIRTIO_MSI_CONFIG_VECTOR:
378 ret = vdev->config_vector;
379 break;
380 case VIRTIO_MSI_QUEUE_VECTOR:
381 ret = virtio_queue_vector(vdev, vdev->queue_sel);
382 break;
383 default:
384 break;
387 return ret;
390 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
391 unsigned size)
393 VirtIOPCIProxy *proxy = opaque;
394 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
395 uint64_t val = 0;
396 if (addr < config) {
397 return virtio_ioport_read(proxy, addr);
399 addr -= config;
401 switch (size) {
402 case 1:
403 val = virtio_config_readb(proxy->vdev, addr);
404 break;
405 case 2:
406 val = virtio_config_readw(proxy->vdev, addr);
407 if (virtio_is_big_endian()) {
408 val = bswap16(val);
410 break;
411 case 4:
412 val = virtio_config_readl(proxy->vdev, addr);
413 if (virtio_is_big_endian()) {
414 val = bswap32(val);
416 break;
418 return val;
421 static void virtio_pci_config_write(void *opaque, hwaddr addr,
422 uint64_t val, unsigned size)
424 VirtIOPCIProxy *proxy = opaque;
425 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
426 if (addr < config) {
427 virtio_ioport_write(proxy, addr, val);
428 return;
430 addr -= config;
432 * Virtio-PCI is odd. Ioports are LE but config space is target native
433 * endian.
435 switch (size) {
436 case 1:
437 virtio_config_writeb(proxy->vdev, addr, val);
438 break;
439 case 2:
440 if (virtio_is_big_endian()) {
441 val = bswap16(val);
443 virtio_config_writew(proxy->vdev, addr, val);
444 break;
445 case 4:
446 if (virtio_is_big_endian()) {
447 val = bswap32(val);
449 virtio_config_writel(proxy->vdev, addr, val);
450 break;
454 static const MemoryRegionOps virtio_pci_config_ops = {
455 .read = virtio_pci_config_read,
456 .write = virtio_pci_config_write,
457 .impl = {
458 .min_access_size = 1,
459 .max_access_size = 4,
461 .endianness = DEVICE_LITTLE_ENDIAN,
464 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
465 uint32_t val, int len)
467 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
469 pci_default_write_config(pci_dev, address, val, len);
471 if (range_covers_byte(address, len, PCI_COMMAND) &&
472 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
473 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
474 virtio_pci_stop_ioeventfd(proxy);
475 virtio_set_status(proxy->vdev,
476 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
480 static unsigned virtio_pci_get_features(DeviceState *d)
482 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
483 return proxy->host_features;
486 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
487 unsigned int queue_no,
488 unsigned int vector,
489 MSIMessage msg)
491 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
492 int ret;
494 if (irqfd->users == 0) {
495 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
496 if (ret < 0) {
497 return ret;
499 irqfd->virq = ret;
501 irqfd->users++;
502 return 0;
505 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
506 unsigned int vector)
508 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
509 if (--irqfd->users == 0) {
510 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
514 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
515 unsigned int queue_no,
516 unsigned int vector)
518 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
519 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
520 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
521 int ret;
522 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, irqfd->virq);
523 return ret;
526 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
527 unsigned int queue_no,
528 unsigned int vector)
530 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
531 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
532 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
533 int ret;
535 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
536 assert(ret == 0);
539 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
541 PCIDevice *dev = &proxy->pci_dev;
542 VirtIODevice *vdev = proxy->vdev;
543 unsigned int vector;
544 int ret, queue_no;
545 MSIMessage msg;
547 for (queue_no = 0; queue_no < nvqs; queue_no++) {
548 if (!virtio_queue_get_num(vdev, queue_no)) {
549 break;
551 vector = virtio_queue_vector(vdev, queue_no);
552 if (vector >= msix_nr_vectors_allocated(dev)) {
553 continue;
555 msg = msix_get_message(dev, vector);
556 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
557 if (ret < 0) {
558 goto undo;
560 /* If guest supports masking, set up irqfd now.
561 * Otherwise, delay until unmasked in the frontend.
563 if (proxy->vdev->guest_notifier_mask) {
564 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
565 if (ret < 0) {
566 kvm_virtio_pci_vq_vector_release(proxy, vector);
567 goto undo;
571 return 0;
573 undo:
574 while (--queue_no >= 0) {
575 vector = virtio_queue_vector(vdev, queue_no);
576 if (vector >= msix_nr_vectors_allocated(dev)) {
577 continue;
579 if (proxy->vdev->guest_notifier_mask) {
580 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
582 kvm_virtio_pci_vq_vector_release(proxy, vector);
584 return ret;
587 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
589 PCIDevice *dev = &proxy->pci_dev;
590 VirtIODevice *vdev = proxy->vdev;
591 unsigned int vector;
592 int queue_no;
594 for (queue_no = 0; queue_no < nvqs; queue_no++) {
595 if (!virtio_queue_get_num(vdev, queue_no)) {
596 break;
598 vector = virtio_queue_vector(vdev, queue_no);
599 if (vector >= msix_nr_vectors_allocated(dev)) {
600 continue;
602 /* If guest supports masking, clean up irqfd now.
603 * Otherwise, it was cleaned when masked in the frontend.
605 if (proxy->vdev->guest_notifier_mask) {
606 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
608 kvm_virtio_pci_vq_vector_release(proxy, vector);
612 static int kvm_virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
613 unsigned int queue_no,
614 unsigned int vector,
615 MSIMessage msg)
617 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
618 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
619 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
620 int ret = 0;
622 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
623 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
624 if (ret < 0) {
625 return ret;
629 /* If guest supports masking, irqfd is already setup, unmask it.
630 * Otherwise, set it up now.
632 if (proxy->vdev->guest_notifier_mask) {
633 proxy->vdev->guest_notifier_mask(proxy->vdev, queue_no, false);
634 /* Test after unmasking to avoid losing events. */
635 if (proxy->vdev->guest_notifier_pending &&
636 proxy->vdev->guest_notifier_pending(proxy->vdev, queue_no)) {
637 event_notifier_set(n);
639 } else {
640 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
642 return ret;
645 static void kvm_virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
646 unsigned int queue_no,
647 unsigned int vector)
649 /* If guest supports masking, keep irqfd but mask it.
650 * Otherwise, clean it up now.
652 if (proxy->vdev->guest_notifier_mask) {
653 proxy->vdev->guest_notifier_mask(proxy->vdev, queue_no, true);
654 } else {
655 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
659 static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
660 MSIMessage msg)
662 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
663 VirtIODevice *vdev = proxy->vdev;
664 int ret, queue_no;
666 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
667 if (!virtio_queue_get_num(vdev, queue_no)) {
668 break;
670 if (virtio_queue_vector(vdev, queue_no) != vector) {
671 continue;
673 ret = kvm_virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg);
674 if (ret < 0) {
675 goto undo;
678 return 0;
680 undo:
681 while (--queue_no >= 0) {
682 if (virtio_queue_vector(vdev, queue_no) != vector) {
683 continue;
685 kvm_virtio_pci_vq_vector_mask(proxy, queue_no, vector);
687 return ret;
690 static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
692 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
693 VirtIODevice *vdev = proxy->vdev;
694 int queue_no;
696 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
697 if (!virtio_queue_get_num(vdev, queue_no)) {
698 break;
700 if (virtio_queue_vector(vdev, queue_no) != vector) {
701 continue;
703 kvm_virtio_pci_vq_vector_mask(proxy, queue_no, vector);
707 static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
708 unsigned int vector_start,
709 unsigned int vector_end)
711 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
712 VirtIODevice *vdev = proxy->vdev;
713 int queue_no;
714 unsigned int vector;
715 EventNotifier *notifier;
716 VirtQueue *vq;
718 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
719 if (!virtio_queue_get_num(vdev, queue_no)) {
720 break;
722 vector = virtio_queue_vector(vdev, queue_no);
723 if (vector < vector_start || vector >= vector_end ||
724 !msix_is_masked(dev, vector)) {
725 continue;
727 vq = virtio_get_queue(vdev, queue_no);
728 notifier = virtio_queue_get_guest_notifier(vq);
729 if (vdev->guest_notifier_pending) {
730 if (vdev->guest_notifier_pending(vdev, queue_no)) {
731 msix_set_pending(dev, vector);
733 } else if (event_notifier_test_and_clear(notifier)) {
734 msix_set_pending(dev, vector);
739 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
740 bool with_irqfd)
742 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
743 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
744 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
746 if (assign) {
747 int r = event_notifier_init(notifier, 0);
748 if (r < 0) {
749 return r;
751 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
752 } else {
753 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
754 event_notifier_cleanup(notifier);
757 return 0;
760 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
762 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
763 return msix_enabled(&proxy->pci_dev);
766 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
768 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
769 VirtIODevice *vdev = proxy->vdev;
770 int r, n;
771 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
772 kvm_msi_via_irqfd_enabled();
774 nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX);
776 /* When deassigning, pass a consistent nvqs value
777 * to avoid leaking notifiers.
779 assert(assign || nvqs == proxy->nvqs_with_notifiers);
781 proxy->nvqs_with_notifiers = nvqs;
783 /* Must unset vector notifier while guest notifier is still assigned */
784 if (proxy->vector_irqfd && !assign) {
785 msix_unset_vector_notifiers(&proxy->pci_dev);
786 kvm_virtio_pci_vector_release(proxy, nvqs);
787 g_free(proxy->vector_irqfd);
788 proxy->vector_irqfd = NULL;
791 for (n = 0; n < nvqs; n++) {
792 if (!virtio_queue_get_num(vdev, n)) {
793 break;
796 r = virtio_pci_set_guest_notifier(d, n, assign,
797 kvm_msi_via_irqfd_enabled());
798 if (r < 0) {
799 goto assign_error;
803 /* Must set vector notifier after guest notifier has been assigned */
804 if (with_irqfd && assign) {
805 proxy->vector_irqfd =
806 g_malloc0(sizeof(*proxy->vector_irqfd) *
807 msix_nr_vectors_allocated(&proxy->pci_dev));
808 r = kvm_virtio_pci_vector_use(proxy, nvqs);
809 if (r < 0) {
810 goto assign_error;
812 r = msix_set_vector_notifiers(&proxy->pci_dev,
813 kvm_virtio_pci_vector_unmask,
814 kvm_virtio_pci_vector_mask,
815 kvm_virtio_pci_vector_poll);
816 if (r < 0) {
817 goto notifiers_error;
821 return 0;
823 notifiers_error:
824 assert(assign);
825 kvm_virtio_pci_vector_release(proxy, nvqs);
827 assign_error:
828 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
829 assert(assign);
830 while (--n >= 0) {
831 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
833 return r;
836 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
838 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
840 /* Stop using ioeventfd for virtqueue kick if the device starts using host
841 * notifiers. This makes it easy to avoid stepping on each others' toes.
843 proxy->ioeventfd_disabled = assign;
844 if (assign) {
845 virtio_pci_stop_ioeventfd(proxy);
847 /* We don't need to start here: it's not needed because backend
848 * currently only stops on status change away from ok,
849 * reset, vmstop and such. If we do add code to start here,
850 * need to check vmstate, device state etc. */
851 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
854 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
856 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
858 if (running) {
859 /* Try to find out if the guest has bus master disabled, but is
860 in ready state. Then we have a buggy guest OS. */
861 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
862 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
863 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
865 virtio_pci_start_ioeventfd(proxy);
866 } else {
867 virtio_pci_stop_ioeventfd(proxy);
871 static const VirtIOBindings virtio_pci_bindings = {
872 .notify = virtio_pci_notify,
873 .save_config = virtio_pci_save_config,
874 .load_config = virtio_pci_load_config,
875 .save_queue = virtio_pci_save_queue,
876 .load_queue = virtio_pci_load_queue,
877 .get_features = virtio_pci_get_features,
878 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
879 .set_host_notifier = virtio_pci_set_host_notifier,
880 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
881 .vmstate_change = virtio_pci_vmstate_change,
884 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
886 uint8_t *config;
887 uint32_t size;
889 proxy->vdev = vdev;
891 config = proxy->pci_dev.config;
893 if (proxy->class_code) {
894 pci_config_set_class(config, proxy->class_code);
896 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
897 pci_get_word(config + PCI_VENDOR_ID));
898 pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
899 config[PCI_INTERRUPT_PIN] = 1;
901 if (vdev->nvectors &&
902 msix_init_exclusive_bar(&proxy->pci_dev, vdev->nvectors, 1)) {
903 vdev->nvectors = 0;
906 proxy->pci_dev.config_write = virtio_write_config;
908 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
909 if (size & (size-1))
910 size = 1 << qemu_fls(size);
912 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
913 "virtio-pci", size);
914 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
915 &proxy->bar);
917 if (!kvm_has_many_ioeventfds()) {
918 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
921 virtio_bind_device(vdev, &virtio_pci_bindings, DEVICE(proxy));
922 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
923 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
924 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
927 static int virtio_blk_init_pci(PCIDevice *pci_dev)
929 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
930 VirtIODevice *vdev;
932 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
933 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
934 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
936 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk);
937 if (!vdev) {
938 return -1;
940 vdev->nvectors = proxy->nvectors;
941 virtio_init_pci(proxy, vdev);
942 /* make the actual value visible */
943 proxy->nvectors = vdev->nvectors;
944 return 0;
947 static void virtio_exit_pci(PCIDevice *pci_dev)
949 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
951 memory_region_destroy(&proxy->bar);
952 msix_uninit_exclusive_bar(pci_dev);
955 static void virtio_blk_exit_pci(PCIDevice *pci_dev)
957 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
959 virtio_pci_stop_ioeventfd(proxy);
960 virtio_blk_exit(proxy->vdev);
961 virtio_exit_pci(pci_dev);
964 static int virtio_serial_init_pci(PCIDevice *pci_dev)
966 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
967 VirtIODevice *vdev;
969 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
970 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
971 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
972 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
974 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
975 if (!vdev) {
976 return -1;
978 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
979 ? proxy->serial.max_virtserial_ports + 1
980 : proxy->nvectors;
981 virtio_init_pci(proxy, vdev);
982 proxy->nvectors = vdev->nvectors;
983 return 0;
986 static void virtio_serial_exit_pci(PCIDevice *pci_dev)
988 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
990 virtio_pci_stop_ioeventfd(proxy);
991 virtio_serial_exit(proxy->vdev);
992 virtio_exit_pci(pci_dev);
995 static int virtio_net_init_pci(PCIDevice *pci_dev)
997 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
998 VirtIODevice *vdev;
1000 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
1002 vdev->nvectors = proxy->nvectors;
1003 virtio_init_pci(proxy, vdev);
1005 /* make the actual value visible */
1006 proxy->nvectors = vdev->nvectors;
1007 return 0;
1010 static void virtio_net_exit_pci(PCIDevice *pci_dev)
1012 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1014 virtio_pci_stop_ioeventfd(proxy);
1015 virtio_net_exit(proxy->vdev);
1016 virtio_exit_pci(pci_dev);
1019 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
1021 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1022 VirtIODevice *vdev;
1024 if (proxy->class_code != PCI_CLASS_OTHERS &&
1025 proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1026 proxy->class_code = PCI_CLASS_OTHERS;
1029 vdev = virtio_balloon_init(&pci_dev->qdev);
1030 if (!vdev) {
1031 return -1;
1033 virtio_init_pci(proxy, vdev);
1034 return 0;
1037 static void virtio_balloon_exit_pci(PCIDevice *pci_dev)
1039 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1041 virtio_pci_stop_ioeventfd(proxy);
1042 virtio_balloon_exit(proxy->vdev);
1043 virtio_exit_pci(pci_dev);
1046 static int virtio_rng_init_pci(PCIDevice *pci_dev)
1048 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1049 VirtIODevice *vdev;
1051 if (proxy->rng.rng == NULL) {
1052 proxy->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
1054 object_property_add_child(OBJECT(pci_dev),
1055 "default-backend",
1056 OBJECT(proxy->rng.default_backend),
1057 NULL);
1059 object_property_set_link(OBJECT(pci_dev),
1060 OBJECT(proxy->rng.default_backend),
1061 "rng", NULL);
1064 vdev = virtio_rng_init(&pci_dev->qdev, &proxy->rng);
1065 if (!vdev) {
1066 return -1;
1068 virtio_init_pci(proxy, vdev);
1069 return 0;
1072 static void virtio_rng_exit_pci(PCIDevice *pci_dev)
1074 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1076 virtio_pci_stop_ioeventfd(proxy);
1077 virtio_rng_exit(proxy->vdev);
1078 virtio_exit_pci(pci_dev);
1081 static Property virtio_blk_properties[] = {
1082 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1083 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf),
1084 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy, blk.conf),
1085 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial),
1086 #ifdef __linux__
1087 DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true),
1088 #endif
1089 DEFINE_PROP_BIT("config-wce", VirtIOPCIProxy, blk.config_wce, 0, true),
1090 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1091 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1092 DEFINE_PROP_BIT("x-data-plane", VirtIOPCIProxy, blk.data_plane, 0, false),
1093 #endif
1094 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1095 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
1096 DEFINE_PROP_END_OF_LIST(),
1099 static void virtio_blk_class_init(ObjectClass *klass, void *data)
1101 DeviceClass *dc = DEVICE_CLASS(klass);
1102 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1104 k->init = virtio_blk_init_pci;
1105 k->exit = virtio_blk_exit_pci;
1106 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1107 k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1108 k->revision = VIRTIO_PCI_ABI_VERSION;
1109 k->class_id = PCI_CLASS_STORAGE_SCSI;
1110 dc->reset = virtio_pci_reset;
1111 dc->props = virtio_blk_properties;
1114 static const TypeInfo virtio_blk_info = {
1115 .name = "virtio-blk-pci",
1116 .parent = TYPE_PCI_DEVICE,
1117 .instance_size = sizeof(VirtIOPCIProxy),
1118 .class_init = virtio_blk_class_init,
1121 static Property virtio_net_properties[] = {
1122 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1123 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1124 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
1125 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
1126 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
1127 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
1128 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
1129 DEFINE_PROP_END_OF_LIST(),
1132 static void virtio_net_class_init(ObjectClass *klass, void *data)
1134 DeviceClass *dc = DEVICE_CLASS(klass);
1135 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1137 k->init = virtio_net_init_pci;
1138 k->exit = virtio_net_exit_pci;
1139 k->romfile = "pxe-virtio.rom";
1140 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1141 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1142 k->revision = VIRTIO_PCI_ABI_VERSION;
1143 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1144 dc->reset = virtio_pci_reset;
1145 dc->props = virtio_net_properties;
1148 static const TypeInfo virtio_net_info = {
1149 .name = "virtio-net-pci",
1150 .parent = TYPE_PCI_DEVICE,
1151 .instance_size = sizeof(VirtIOPCIProxy),
1152 .class_init = virtio_net_class_init,
1155 static Property virtio_serial_properties[] = {
1156 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1157 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
1158 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1159 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1160 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
1161 DEFINE_PROP_END_OF_LIST(),
1164 static void virtio_serial_class_init(ObjectClass *klass, void *data)
1166 DeviceClass *dc = DEVICE_CLASS(klass);
1167 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1169 k->init = virtio_serial_init_pci;
1170 k->exit = virtio_serial_exit_pci;
1171 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1172 k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1173 k->revision = VIRTIO_PCI_ABI_VERSION;
1174 k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1175 dc->reset = virtio_pci_reset;
1176 dc->props = virtio_serial_properties;
1179 static const TypeInfo virtio_serial_info = {
1180 .name = "virtio-serial-pci",
1181 .parent = TYPE_PCI_DEVICE,
1182 .instance_size = sizeof(VirtIOPCIProxy),
1183 .class_init = virtio_serial_class_init,
1186 static Property virtio_balloon_properties[] = {
1187 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1188 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1189 DEFINE_PROP_END_OF_LIST(),
1192 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
1194 DeviceClass *dc = DEVICE_CLASS(klass);
1195 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1197 k->init = virtio_balloon_init_pci;
1198 k->exit = virtio_balloon_exit_pci;
1199 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1200 k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1201 k->revision = VIRTIO_PCI_ABI_VERSION;
1202 k->class_id = PCI_CLASS_OTHERS;
1203 dc->reset = virtio_pci_reset;
1204 dc->props = virtio_balloon_properties;
1207 static const TypeInfo virtio_balloon_info = {
1208 .name = "virtio-balloon-pci",
1209 .parent = TYPE_PCI_DEVICE,
1210 .instance_size = sizeof(VirtIOPCIProxy),
1211 .class_init = virtio_balloon_class_init,
1214 static void virtio_rng_initfn(Object *obj)
1216 PCIDevice *pci_dev = PCI_DEVICE(obj);
1217 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1219 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1220 (Object **)&proxy->rng.rng, NULL);
1223 static Property virtio_rng_properties[] = {
1224 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1225 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
1226 you have an entropy source capable of generating more entropy than this
1227 and you can pass it through via virtio-rng, then hats off to you. Until
1228 then, this is unlimited for all practical purposes.
1230 DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy, rng.max_bytes, INT64_MAX),
1231 DEFINE_PROP_UINT32("period", VirtIOPCIProxy, rng.period_ms, 1 << 16),
1232 DEFINE_PROP_END_OF_LIST(),
1235 static void virtio_rng_class_init(ObjectClass *klass, void *data)
1237 DeviceClass *dc = DEVICE_CLASS(klass);
1238 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1240 k->init = virtio_rng_init_pci;
1241 k->exit = virtio_rng_exit_pci;
1242 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1243 k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1244 k->revision = VIRTIO_PCI_ABI_VERSION;
1245 k->class_id = PCI_CLASS_OTHERS;
1246 dc->reset = virtio_pci_reset;
1247 dc->props = virtio_rng_properties;
1250 static const TypeInfo virtio_rng_info = {
1251 .name = "virtio-rng-pci",
1252 .parent = TYPE_PCI_DEVICE,
1253 .instance_size = sizeof(VirtIOPCIProxy),
1254 .instance_init = virtio_rng_initfn,
1255 .class_init = virtio_rng_class_init,
1258 static int virtio_scsi_init_pci(PCIDevice *pci_dev)
1260 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1261 VirtIODevice *vdev;
1263 vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
1264 if (!vdev) {
1265 return -EINVAL;
1268 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
1269 ? proxy->scsi.num_queues + 3
1270 : proxy->nvectors;
1271 virtio_init_pci(proxy, vdev);
1273 /* make the actual value visible */
1274 proxy->nvectors = vdev->nvectors;
1275 return 0;
1278 static void virtio_scsi_exit_pci(PCIDevice *pci_dev)
1280 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1282 virtio_scsi_exit(proxy->vdev);
1283 virtio_exit_pci(pci_dev);
1286 static Property virtio_scsi_properties[] = {
1287 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1288 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
1289 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
1290 DEFINE_PROP_END_OF_LIST(),
1293 static void virtio_scsi_class_init(ObjectClass *klass, void *data)
1295 DeviceClass *dc = DEVICE_CLASS(klass);
1296 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1298 k->init = virtio_scsi_init_pci;
1299 k->exit = virtio_scsi_exit_pci;
1300 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1301 k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1302 k->revision = 0x00;
1303 k->class_id = PCI_CLASS_STORAGE_SCSI;
1304 dc->reset = virtio_pci_reset;
1305 dc->props = virtio_scsi_properties;
1308 static const TypeInfo virtio_scsi_info = {
1309 .name = "virtio-scsi-pci",
1310 .parent = TYPE_PCI_DEVICE,
1311 .instance_size = sizeof(VirtIOPCIProxy),
1312 .class_init = virtio_scsi_class_init,
1316 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1319 /* This is called by virtio-bus just after the device is plugged. */
1320 static void virtio_pci_device_plugged(DeviceState *d)
1322 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1323 VirtioBusState *bus = &proxy->bus;
1324 uint8_t *config;
1325 uint32_t size;
1327 proxy->vdev = bus->vdev;
1329 config = proxy->pci_dev.config;
1330 if (proxy->class_code) {
1331 pci_config_set_class(config, proxy->class_code);
1333 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1334 pci_get_word(config + PCI_VENDOR_ID));
1335 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1336 config[PCI_INTERRUPT_PIN] = 1;
1338 if (proxy->nvectors &&
1339 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
1340 proxy->nvectors = 0;
1343 proxy->pci_dev.config_write = virtio_write_config;
1345 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1346 + virtio_bus_get_vdev_config_len(bus);
1347 if (size & (size - 1)) {
1348 size = 1 << qemu_fls(size);
1351 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
1352 "virtio-pci", size);
1353 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
1354 &proxy->bar);
1356 if (!kvm_has_many_ioeventfds()) {
1357 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1360 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
1361 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
1362 proxy->host_features = virtio_bus_get_vdev_features(bus,
1363 proxy->host_features);
1366 /* This is called by virtio-bus just before the device is unplugged. */
1367 static void virtio_pci_device_unplug(DeviceState *d)
1369 VirtIOPCIProxy *dev = VIRTIO_PCI(d);
1370 virtio_pci_stop_ioeventfd(dev);
1373 static int virtio_pci_init(PCIDevice *pci_dev)
1375 VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
1376 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1377 virtio_pci_bus_new(&dev->bus, dev);
1378 if (k->init != NULL) {
1379 return k->init(dev);
1381 return 0;
1384 static void virtio_pci_exit(PCIDevice *pci_dev)
1386 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1387 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1388 BusState *qbus = BUS(&proxy->bus);
1389 virtio_bus_destroy_device(bus);
1390 qbus_free(qbus);
1391 virtio_exit_pci(pci_dev);
1395 * This will be renamed virtio_pci_reset at the end of the series.
1396 * virtio_pci_reset is still in use at this moment.
1398 static void virtio_pci_rst(DeviceState *qdev)
1400 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1401 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1402 virtio_pci_stop_ioeventfd(proxy);
1403 virtio_bus_reset(bus);
1404 msix_unuse_all_vectors(&proxy->pci_dev);
1405 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
1408 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1410 DeviceClass *dc = DEVICE_CLASS(klass);
1411 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1413 k->init = virtio_pci_init;
1414 k->exit = virtio_pci_exit;
1415 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1416 k->revision = VIRTIO_PCI_ABI_VERSION;
1417 k->class_id = PCI_CLASS_OTHERS;
1418 dc->reset = virtio_pci_rst;
1421 static const TypeInfo virtio_pci_info = {
1422 .name = TYPE_VIRTIO_PCI,
1423 .parent = TYPE_PCI_DEVICE,
1424 .instance_size = sizeof(VirtIOPCIProxy),
1425 .class_init = virtio_pci_class_init,
1426 .class_size = sizeof(VirtioPCIClass),
1427 .abstract = true,
1430 /* virtio-pci-bus */
1432 void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev)
1434 DeviceState *qdev = DEVICE(dev);
1435 BusState *qbus;
1436 qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_PCI_BUS, qdev, NULL);
1437 qbus = BUS(bus);
1438 qbus->allow_hotplug = 0;
1441 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
1443 BusClass *bus_class = BUS_CLASS(klass);
1444 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1445 bus_class->max_dev = 1;
1446 k->notify = virtio_pci_notify;
1447 k->save_config = virtio_pci_save_config;
1448 k->load_config = virtio_pci_load_config;
1449 k->save_queue = virtio_pci_save_queue;
1450 k->load_queue = virtio_pci_load_queue;
1451 k->get_features = virtio_pci_get_features;
1452 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
1453 k->set_host_notifier = virtio_pci_set_host_notifier;
1454 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
1455 k->vmstate_change = virtio_pci_vmstate_change;
1456 k->device_plugged = virtio_pci_device_plugged;
1457 k->device_unplug = virtio_pci_device_unplug;
1460 static const TypeInfo virtio_pci_bus_info = {
1461 .name = TYPE_VIRTIO_PCI_BUS,
1462 .parent = TYPE_VIRTIO_BUS,
1463 .instance_size = sizeof(VirtioPCIBusState),
1464 .class_init = virtio_pci_bus_class_init,
1467 static void virtio_pci_register_types(void)
1469 type_register_static(&virtio_blk_info);
1470 type_register_static(&virtio_net_info);
1471 type_register_static(&virtio_serial_info);
1472 type_register_static(&virtio_balloon_info);
1473 type_register_static(&virtio_scsi_info);
1474 type_register_static(&virtio_rng_info);
1475 type_register_static(&virtio_pci_bus_info);
1476 type_register_static(&virtio_pci_info);
1479 type_init(virtio_pci_register_types)