virtio fix cfg endian-ness for BE targets
[qemu/cris-port.git] / hw / virtio / virtio-pci.c
blob6ca0258067617098b6b201e08022513edc47a55c
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 <= sizeof cfg->pci_cfg_data) {
550 virtio_address_space_write(&proxy->modern_as, off,
551 cfg->pci_cfg_data, len);
556 static uint32_t virtio_read_config(PCIDevice *pci_dev,
557 uint32_t address, int len)
559 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
560 struct virtio_pci_cfg_cap *cfg;
562 if (proxy->config_cap &&
563 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
564 pci_cfg_data),
565 sizeof cfg->pci_cfg_data)) {
566 uint32_t off;
567 uint32_t len;
569 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
570 off = le32_to_cpu(cfg->cap.offset);
571 len = le32_to_cpu(cfg->cap.length);
573 if (len <= sizeof cfg->pci_cfg_data) {
574 virtio_address_space_read(&proxy->modern_as, off,
575 cfg->pci_cfg_data, len);
579 return pci_default_read_config(pci_dev, address, len);
582 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
583 unsigned int queue_no,
584 unsigned int vector,
585 MSIMessage msg)
587 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
588 int ret;
590 if (irqfd->users == 0) {
591 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
592 if (ret < 0) {
593 return ret;
595 irqfd->virq = ret;
597 irqfd->users++;
598 return 0;
601 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
602 unsigned int vector)
604 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
605 if (--irqfd->users == 0) {
606 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
610 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
611 unsigned int queue_no,
612 unsigned int vector)
614 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
615 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
616 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
617 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
618 int ret;
619 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq);
620 return ret;
623 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
624 unsigned int queue_no,
625 unsigned int vector)
627 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
628 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
629 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
630 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
631 int ret;
633 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
634 assert(ret == 0);
637 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
639 PCIDevice *dev = &proxy->pci_dev;
640 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
641 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
642 unsigned int vector;
643 int ret, queue_no;
644 MSIMessage msg;
646 for (queue_no = 0; queue_no < nvqs; queue_no++) {
647 if (!virtio_queue_get_num(vdev, queue_no)) {
648 break;
650 vector = virtio_queue_vector(vdev, queue_no);
651 if (vector >= msix_nr_vectors_allocated(dev)) {
652 continue;
654 msg = msix_get_message(dev, vector);
655 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
656 if (ret < 0) {
657 goto undo;
659 /* If guest supports masking, set up irqfd now.
660 * Otherwise, delay until unmasked in the frontend.
662 if (k->guest_notifier_mask) {
663 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
664 if (ret < 0) {
665 kvm_virtio_pci_vq_vector_release(proxy, vector);
666 goto undo;
670 return 0;
672 undo:
673 while (--queue_no >= 0) {
674 vector = virtio_queue_vector(vdev, queue_no);
675 if (vector >= msix_nr_vectors_allocated(dev)) {
676 continue;
678 if (k->guest_notifier_mask) {
679 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
681 kvm_virtio_pci_vq_vector_release(proxy, vector);
683 return ret;
686 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
688 PCIDevice *dev = &proxy->pci_dev;
689 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
690 unsigned int vector;
691 int queue_no;
692 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
694 for (queue_no = 0; queue_no < nvqs; queue_no++) {
695 if (!virtio_queue_get_num(vdev, queue_no)) {
696 break;
698 vector = virtio_queue_vector(vdev, queue_no);
699 if (vector >= msix_nr_vectors_allocated(dev)) {
700 continue;
702 /* If guest supports masking, clean up irqfd now.
703 * Otherwise, it was cleaned when masked in the frontend.
705 if (k->guest_notifier_mask) {
706 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
708 kvm_virtio_pci_vq_vector_release(proxy, vector);
712 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
713 unsigned int queue_no,
714 unsigned int vector,
715 MSIMessage msg)
717 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
718 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
719 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
720 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
721 VirtIOIRQFD *irqfd;
722 int ret = 0;
724 if (proxy->vector_irqfd) {
725 irqfd = &proxy->vector_irqfd[vector];
726 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
727 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
728 if (ret < 0) {
729 return ret;
734 /* If guest supports masking, irqfd is already setup, unmask it.
735 * Otherwise, set it up now.
737 if (k->guest_notifier_mask) {
738 k->guest_notifier_mask(vdev, queue_no, false);
739 /* Test after unmasking to avoid losing events. */
740 if (k->guest_notifier_pending &&
741 k->guest_notifier_pending(vdev, queue_no)) {
742 event_notifier_set(n);
744 } else {
745 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
747 return ret;
750 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
751 unsigned int queue_no,
752 unsigned int vector)
754 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
755 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
757 /* If guest supports masking, keep irqfd but mask it.
758 * Otherwise, clean it up now.
760 if (k->guest_notifier_mask) {
761 k->guest_notifier_mask(vdev, queue_no, true);
762 } else {
763 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
767 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
768 MSIMessage msg)
770 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
771 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
772 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
773 int ret, index, unmasked = 0;
775 while (vq) {
776 index = virtio_get_queue_index(vq);
777 if (!virtio_queue_get_num(vdev, index)) {
778 break;
780 if (index < proxy->nvqs_with_notifiers) {
781 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
782 if (ret < 0) {
783 goto undo;
785 ++unmasked;
787 vq = virtio_vector_next_queue(vq);
790 return 0;
792 undo:
793 vq = virtio_vector_first_queue(vdev, vector);
794 while (vq && unmasked >= 0) {
795 index = virtio_get_queue_index(vq);
796 if (index < proxy->nvqs_with_notifiers) {
797 virtio_pci_vq_vector_mask(proxy, index, vector);
798 --unmasked;
800 vq = virtio_vector_next_queue(vq);
802 return ret;
805 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
807 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
808 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
809 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
810 int index;
812 while (vq) {
813 index = virtio_get_queue_index(vq);
814 if (!virtio_queue_get_num(vdev, index)) {
815 break;
817 if (index < proxy->nvqs_with_notifiers) {
818 virtio_pci_vq_vector_mask(proxy, index, vector);
820 vq = virtio_vector_next_queue(vq);
824 static void virtio_pci_vector_poll(PCIDevice *dev,
825 unsigned int vector_start,
826 unsigned int vector_end)
828 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
829 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
830 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
831 int queue_no;
832 unsigned int vector;
833 EventNotifier *notifier;
834 VirtQueue *vq;
836 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
837 if (!virtio_queue_get_num(vdev, queue_no)) {
838 break;
840 vector = virtio_queue_vector(vdev, queue_no);
841 if (vector < vector_start || vector >= vector_end ||
842 !msix_is_masked(dev, vector)) {
843 continue;
845 vq = virtio_get_queue(vdev, queue_no);
846 notifier = virtio_queue_get_guest_notifier(vq);
847 if (k->guest_notifier_pending) {
848 if (k->guest_notifier_pending(vdev, queue_no)) {
849 msix_set_pending(dev, vector);
851 } else if (event_notifier_test_and_clear(notifier)) {
852 msix_set_pending(dev, vector);
857 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
858 bool with_irqfd)
860 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
861 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
862 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
863 VirtQueue *vq = virtio_get_queue(vdev, n);
864 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
866 if (assign) {
867 int r = event_notifier_init(notifier, 0);
868 if (r < 0) {
869 return r;
871 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
872 } else {
873 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
874 event_notifier_cleanup(notifier);
877 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
878 vdc->guest_notifier_mask(vdev, n, !assign);
881 return 0;
884 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
886 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
887 return msix_enabled(&proxy->pci_dev);
890 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
892 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
893 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
894 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
895 int r, n;
896 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
897 kvm_msi_via_irqfd_enabled();
899 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
901 /* When deassigning, pass a consistent nvqs value
902 * to avoid leaking notifiers.
904 assert(assign || nvqs == proxy->nvqs_with_notifiers);
906 proxy->nvqs_with_notifiers = nvqs;
908 /* Must unset vector notifier while guest notifier is still assigned */
909 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
910 msix_unset_vector_notifiers(&proxy->pci_dev);
911 if (proxy->vector_irqfd) {
912 kvm_virtio_pci_vector_release(proxy, nvqs);
913 g_free(proxy->vector_irqfd);
914 proxy->vector_irqfd = NULL;
918 for (n = 0; n < nvqs; n++) {
919 if (!virtio_queue_get_num(vdev, n)) {
920 break;
923 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
924 if (r < 0) {
925 goto assign_error;
929 /* Must set vector notifier after guest notifier has been assigned */
930 if ((with_irqfd || k->guest_notifier_mask) && assign) {
931 if (with_irqfd) {
932 proxy->vector_irqfd =
933 g_malloc0(sizeof(*proxy->vector_irqfd) *
934 msix_nr_vectors_allocated(&proxy->pci_dev));
935 r = kvm_virtio_pci_vector_use(proxy, nvqs);
936 if (r < 0) {
937 goto assign_error;
940 r = msix_set_vector_notifiers(&proxy->pci_dev,
941 virtio_pci_vector_unmask,
942 virtio_pci_vector_mask,
943 virtio_pci_vector_poll);
944 if (r < 0) {
945 goto notifiers_error;
949 return 0;
951 notifiers_error:
952 if (with_irqfd) {
953 assert(assign);
954 kvm_virtio_pci_vector_release(proxy, nvqs);
957 assign_error:
958 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
959 assert(assign);
960 while (--n >= 0) {
961 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
963 return r;
966 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
968 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
970 /* Stop using ioeventfd for virtqueue kick if the device starts using host
971 * notifiers. This makes it easy to avoid stepping on each others' toes.
973 proxy->ioeventfd_disabled = assign;
974 if (assign) {
975 virtio_pci_stop_ioeventfd(proxy);
977 /* We don't need to start here: it's not needed because backend
978 * currently only stops on status change away from ok,
979 * reset, vmstop and such. If we do add code to start here,
980 * need to check vmstate, device state etc. */
981 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
984 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
986 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
987 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
989 if (running) {
990 /* Old QEMU versions did not set bus master enable on status write.
991 * Detect DRIVER set and enable it.
993 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
994 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
995 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
996 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
997 proxy->pci_dev.config[PCI_COMMAND] |
998 PCI_COMMAND_MASTER, 1);
1000 virtio_pci_start_ioeventfd(proxy);
1001 } else {
1002 virtio_pci_stop_ioeventfd(proxy);
1006 #ifdef CONFIG_VIRTFS
1007 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1009 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1010 DeviceState *vdev = DEVICE(&dev->vdev);
1012 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1013 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1016 static Property virtio_9p_pci_properties[] = {
1017 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1018 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1019 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1020 DEFINE_PROP_END_OF_LIST(),
1023 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1025 DeviceClass *dc = DEVICE_CLASS(klass);
1026 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1027 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1029 k->realize = virtio_9p_pci_realize;
1030 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1031 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1032 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1033 pcidev_k->class_id = 0x2;
1034 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1035 dc->props = virtio_9p_pci_properties;
1038 static void virtio_9p_pci_instance_init(Object *obj)
1040 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1042 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1043 TYPE_VIRTIO_9P);
1046 static const TypeInfo virtio_9p_pci_info = {
1047 .name = TYPE_VIRTIO_9P_PCI,
1048 .parent = TYPE_VIRTIO_PCI,
1049 .instance_size = sizeof(V9fsPCIState),
1050 .instance_init = virtio_9p_pci_instance_init,
1051 .class_init = virtio_9p_pci_class_init,
1053 #endif /* CONFIG_VIRTFS */
1056 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1059 static int virtio_pci_query_nvectors(DeviceState *d)
1061 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1063 return proxy->nvectors;
1066 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1067 struct virtio_pci_cap *cap)
1069 PCIDevice *dev = &proxy->pci_dev;
1070 int offset;
1072 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1073 assert(offset > 0);
1075 assert(cap->cap_len >= sizeof *cap);
1076 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1077 cap->cap_len - PCI_CAP_FLAGS);
1079 return offset;
1082 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1083 unsigned size)
1085 VirtIOPCIProxy *proxy = opaque;
1086 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1087 uint32_t val = 0;
1088 int i;
1090 switch (addr) {
1091 case VIRTIO_PCI_COMMON_DFSELECT:
1092 val = proxy->dfselect;
1093 break;
1094 case VIRTIO_PCI_COMMON_DF:
1095 if (proxy->dfselect <= 1) {
1096 val = vdev->host_features >> (32 * proxy->dfselect);
1098 break;
1099 case VIRTIO_PCI_COMMON_GFSELECT:
1100 val = proxy->gfselect;
1101 break;
1102 case VIRTIO_PCI_COMMON_GF:
1103 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1104 val = proxy->guest_features[proxy->gfselect];
1106 break;
1107 case VIRTIO_PCI_COMMON_MSIX:
1108 val = vdev->config_vector;
1109 break;
1110 case VIRTIO_PCI_COMMON_NUMQ:
1111 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1112 if (virtio_queue_get_num(vdev, i)) {
1113 val = i + 1;
1116 break;
1117 case VIRTIO_PCI_COMMON_STATUS:
1118 val = vdev->status;
1119 break;
1120 case VIRTIO_PCI_COMMON_CFGGENERATION:
1121 val = vdev->generation;
1122 break;
1123 case VIRTIO_PCI_COMMON_Q_SELECT:
1124 val = vdev->queue_sel;
1125 break;
1126 case VIRTIO_PCI_COMMON_Q_SIZE:
1127 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1128 break;
1129 case VIRTIO_PCI_COMMON_Q_MSIX:
1130 val = virtio_queue_vector(vdev, vdev->queue_sel);
1131 break;
1132 case VIRTIO_PCI_COMMON_Q_ENABLE:
1133 val = proxy->vqs[vdev->queue_sel].enabled;
1134 break;
1135 case VIRTIO_PCI_COMMON_Q_NOFF:
1136 /* Simply map queues in order */
1137 val = vdev->queue_sel;
1138 break;
1139 case VIRTIO_PCI_COMMON_Q_DESCLO:
1140 val = proxy->vqs[vdev->queue_sel].desc[0];
1141 break;
1142 case VIRTIO_PCI_COMMON_Q_DESCHI:
1143 val = proxy->vqs[vdev->queue_sel].desc[1];
1144 break;
1145 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1146 val = proxy->vqs[vdev->queue_sel].avail[0];
1147 break;
1148 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1149 val = proxy->vqs[vdev->queue_sel].avail[1];
1150 break;
1151 case VIRTIO_PCI_COMMON_Q_USEDLO:
1152 val = proxy->vqs[vdev->queue_sel].used[0];
1153 break;
1154 case VIRTIO_PCI_COMMON_Q_USEDHI:
1155 val = proxy->vqs[vdev->queue_sel].used[1];
1156 break;
1157 default:
1158 val = 0;
1161 return val;
1164 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1165 uint64_t val, unsigned size)
1167 VirtIOPCIProxy *proxy = opaque;
1168 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1170 switch (addr) {
1171 case VIRTIO_PCI_COMMON_DFSELECT:
1172 proxy->dfselect = val;
1173 break;
1174 case VIRTIO_PCI_COMMON_GFSELECT:
1175 proxy->gfselect = val;
1176 break;
1177 case VIRTIO_PCI_COMMON_GF:
1178 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1179 proxy->guest_features[proxy->gfselect] = val;
1180 virtio_set_features(vdev,
1181 (((uint64_t)proxy->guest_features[1]) << 32) |
1182 proxy->guest_features[0]);
1184 break;
1185 case VIRTIO_PCI_COMMON_MSIX:
1186 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1187 /* Make it possible for guest to discover an error took place. */
1188 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1189 val = VIRTIO_NO_VECTOR;
1191 vdev->config_vector = val;
1192 break;
1193 case VIRTIO_PCI_COMMON_STATUS:
1194 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1195 virtio_pci_stop_ioeventfd(proxy);
1198 virtio_set_status(vdev, val & 0xFF);
1200 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1201 virtio_pci_start_ioeventfd(proxy);
1204 if (vdev->status == 0) {
1205 virtio_reset(vdev);
1206 msix_unuse_all_vectors(&proxy->pci_dev);
1209 break;
1210 case VIRTIO_PCI_COMMON_Q_SELECT:
1211 if (val < VIRTIO_QUEUE_MAX) {
1212 vdev->queue_sel = val;
1214 break;
1215 case VIRTIO_PCI_COMMON_Q_SIZE:
1216 proxy->vqs[vdev->queue_sel].num = val;
1217 break;
1218 case VIRTIO_PCI_COMMON_Q_MSIX:
1219 msix_vector_unuse(&proxy->pci_dev,
1220 virtio_queue_vector(vdev, vdev->queue_sel));
1221 /* Make it possible for guest to discover an error took place. */
1222 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1223 val = VIRTIO_NO_VECTOR;
1225 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1226 break;
1227 case VIRTIO_PCI_COMMON_Q_ENABLE:
1228 /* TODO: need a way to put num back on reset. */
1229 virtio_queue_set_num(vdev, vdev->queue_sel,
1230 proxy->vqs[vdev->queue_sel].num);
1231 virtio_queue_set_rings(vdev, vdev->queue_sel,
1232 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1233 proxy->vqs[vdev->queue_sel].desc[0],
1234 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1235 proxy->vqs[vdev->queue_sel].avail[0],
1236 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1237 proxy->vqs[vdev->queue_sel].used[0]);
1238 break;
1239 case VIRTIO_PCI_COMMON_Q_DESCLO:
1240 proxy->vqs[vdev->queue_sel].desc[0] = val;
1241 break;
1242 case VIRTIO_PCI_COMMON_Q_DESCHI:
1243 proxy->vqs[vdev->queue_sel].desc[1] = val;
1244 break;
1245 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1246 proxy->vqs[vdev->queue_sel].avail[0] = val;
1247 break;
1248 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1249 proxy->vqs[vdev->queue_sel].avail[1] = val;
1250 break;
1251 case VIRTIO_PCI_COMMON_Q_USEDLO:
1252 proxy->vqs[vdev->queue_sel].used[0] = val;
1253 break;
1254 case VIRTIO_PCI_COMMON_Q_USEDHI:
1255 proxy->vqs[vdev->queue_sel].used[1] = val;
1256 break;
1257 default:
1258 break;
1263 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1264 unsigned size)
1266 return 0;
1269 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1270 uint64_t val, unsigned size)
1272 VirtIODevice *vdev = opaque;
1273 unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT;
1275 if (queue < VIRTIO_QUEUE_MAX) {
1276 virtio_queue_notify(vdev, queue);
1280 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1281 unsigned size)
1283 VirtIOPCIProxy *proxy = opaque;
1284 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1285 uint64_t val = vdev->isr;
1287 vdev->isr = 0;
1288 pci_irq_deassert(&proxy->pci_dev);
1290 return val;
1293 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1294 uint64_t val, unsigned size)
1298 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1299 unsigned size)
1301 VirtIODevice *vdev = opaque;
1302 uint64_t val = 0;
1304 switch (size) {
1305 case 1:
1306 val = virtio_config_modern_readb(vdev, addr);
1307 break;
1308 case 2:
1309 val = virtio_config_modern_readw(vdev, addr);
1310 break;
1311 case 4:
1312 val = virtio_config_modern_readl(vdev, addr);
1313 break;
1315 return val;
1318 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1319 uint64_t val, unsigned size)
1321 VirtIODevice *vdev = opaque;
1322 switch (size) {
1323 case 1:
1324 virtio_config_modern_writeb(vdev, addr, val);
1325 break;
1326 case 2:
1327 virtio_config_modern_writew(vdev, addr, val);
1328 break;
1329 case 4:
1330 virtio_config_modern_writel(vdev, addr, val);
1331 break;
1335 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1337 static const MemoryRegionOps common_ops = {
1338 .read = virtio_pci_common_read,
1339 .write = virtio_pci_common_write,
1340 .impl = {
1341 .min_access_size = 1,
1342 .max_access_size = 4,
1344 .endianness = DEVICE_LITTLE_ENDIAN,
1346 static const MemoryRegionOps isr_ops = {
1347 .read = virtio_pci_isr_read,
1348 .write = virtio_pci_isr_write,
1349 .impl = {
1350 .min_access_size = 1,
1351 .max_access_size = 4,
1353 .endianness = DEVICE_LITTLE_ENDIAN,
1355 static const MemoryRegionOps device_ops = {
1356 .read = virtio_pci_device_read,
1357 .write = virtio_pci_device_write,
1358 .impl = {
1359 .min_access_size = 1,
1360 .max_access_size = 4,
1362 .endianness = DEVICE_LITTLE_ENDIAN,
1364 static const MemoryRegionOps notify_ops = {
1365 .read = virtio_pci_notify_read,
1366 .write = virtio_pci_notify_write,
1367 .impl = {
1368 .min_access_size = 1,
1369 .max_access_size = 4,
1371 .endianness = DEVICE_LITTLE_ENDIAN,
1374 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1375 &common_ops,
1376 proxy,
1377 "virtio-pci-common",
1378 proxy->common.size);
1380 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1381 &isr_ops,
1382 proxy,
1383 "virtio-pci-isr",
1384 proxy->isr.size);
1386 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1387 &device_ops,
1388 virtio_bus_get_device(&proxy->bus),
1389 "virtio-pci-device",
1390 proxy->device.size);
1392 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1393 &notify_ops,
1394 virtio_bus_get_device(&proxy->bus),
1395 "virtio-pci-notify",
1396 proxy->notify.size);
1399 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1400 VirtIOPCIRegion *region,
1401 struct virtio_pci_cap *cap)
1403 memory_region_add_subregion(&proxy->modern_bar,
1404 region->offset,
1405 &region->mr);
1407 cap->cfg_type = region->type;
1408 cap->bar = proxy->modern_mem_bar;
1409 cap->offset = cpu_to_le32(region->offset);
1410 cap->length = cpu_to_le32(region->size);
1411 virtio_pci_add_mem_cap(proxy, cap);
1414 /* This is called by virtio-bus just after the device is plugged. */
1415 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1417 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1418 VirtioBusState *bus = &proxy->bus;
1419 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
1420 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1421 uint8_t *config;
1422 uint32_t size;
1423 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1425 config = proxy->pci_dev.config;
1426 if (proxy->class_code) {
1427 pci_config_set_class(config, proxy->class_code);
1430 if (legacy) {
1431 /* legacy and transitional */
1432 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1433 pci_get_word(config + PCI_VENDOR_ID));
1434 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1435 } else {
1436 /* pure virtio-1.0 */
1437 pci_set_word(config + PCI_VENDOR_ID,
1438 PCI_VENDOR_ID_REDHAT_QUMRANET);
1439 pci_set_word(config + PCI_DEVICE_ID,
1440 0x1040 + virtio_bus_get_vdev_id(bus));
1441 pci_config_set_revision(config, 1);
1443 config[PCI_INTERRUPT_PIN] = 1;
1446 if (modern) {
1447 struct virtio_pci_cap cap = {
1448 .cap_len = sizeof cap,
1450 struct virtio_pci_notify_cap notify = {
1451 .cap.cap_len = sizeof notify,
1452 .notify_off_multiplier =
1453 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
1455 struct virtio_pci_cfg_cap cfg = {
1456 .cap.cap_len = sizeof cfg,
1457 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1459 struct virtio_pci_cfg_cap *cfg_mask;
1461 /* TODO: add io access for speed */
1463 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1464 virtio_pci_modern_regions_init(proxy);
1465 virtio_pci_modern_region_map(proxy, &proxy->common, &cap);
1466 virtio_pci_modern_region_map(proxy, &proxy->isr, &cap);
1467 virtio_pci_modern_region_map(proxy, &proxy->device, &cap);
1468 virtio_pci_modern_region_map(proxy, &proxy->notify, &notify.cap);
1470 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
1471 PCI_BASE_ADDRESS_SPACE_MEMORY |
1472 PCI_BASE_ADDRESS_MEM_PREFETCH |
1473 PCI_BASE_ADDRESS_MEM_TYPE_64,
1474 &proxy->modern_bar);
1476 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1477 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1478 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1479 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1480 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1481 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1484 if (proxy->nvectors &&
1485 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1486 proxy->msix_bar)) {
1487 error_report("unable to init msix vectors to %" PRIu32,
1488 proxy->nvectors);
1489 proxy->nvectors = 0;
1492 proxy->pci_dev.config_write = virtio_write_config;
1493 proxy->pci_dev.config_read = virtio_read_config;
1495 if (legacy) {
1496 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1497 + virtio_bus_get_vdev_config_len(bus);
1498 if (size & (size - 1)) {
1499 size = 1 << qemu_fls(size);
1502 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1503 &virtio_pci_config_ops,
1504 proxy, "virtio-pci", size);
1506 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
1507 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1510 if (!kvm_has_many_ioeventfds()) {
1511 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1514 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1517 static void virtio_pci_device_unplugged(DeviceState *d)
1519 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1521 virtio_pci_stop_ioeventfd(proxy);
1524 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1526 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1527 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1530 * virtio pci bar layout used by default.
1531 * subclasses can re-arrange things if needed.
1533 * region 0 -- virtio legacy io bar
1534 * region 1 -- msi-x bar
1535 * region 4+5 -- virtio modern memory (64bit) bar
1538 proxy->legacy_io_bar = 0;
1539 proxy->msix_bar = 1;
1540 proxy->modern_mem_bar = 4;
1542 proxy->common.offset = 0x0;
1543 proxy->common.size = 0x1000;
1544 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1546 proxy->isr.offset = 0x1000;
1547 proxy->isr.size = 0x1000;
1548 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1550 proxy->device.offset = 0x2000;
1551 proxy->device.size = 0x1000;
1552 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1554 proxy->notify.offset = 0x3000;
1555 proxy->notify.size =
1556 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX;
1557 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1559 /* subclasses can enforce modern, so do this unconditionally */
1560 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1561 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
1562 VIRTIO_QUEUE_MAX);
1564 memory_region_init_alias(&proxy->modern_cfg,
1565 OBJECT(proxy),
1566 "virtio-pci-cfg",
1567 &proxy->modern_bar,
1569 memory_region_size(&proxy->modern_bar));
1571 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1573 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1574 if (k->realize) {
1575 k->realize(proxy, errp);
1579 static void virtio_pci_exit(PCIDevice *pci_dev)
1581 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1583 msix_uninit_exclusive_bar(pci_dev);
1584 address_space_destroy(&proxy->modern_as);
1587 static void virtio_pci_reset(DeviceState *qdev)
1589 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1590 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1591 virtio_pci_stop_ioeventfd(proxy);
1592 virtio_bus_reset(bus);
1593 msix_unuse_all_vectors(&proxy->pci_dev);
1596 static Property virtio_pci_properties[] = {
1597 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1598 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1599 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags,
1600 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false),
1601 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags,
1602 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true),
1603 DEFINE_PROP_END_OF_LIST(),
1606 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1608 DeviceClass *dc = DEVICE_CLASS(klass);
1609 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1611 dc->props = virtio_pci_properties;
1612 k->realize = virtio_pci_realize;
1613 k->exit = virtio_pci_exit;
1614 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1615 k->revision = VIRTIO_PCI_ABI_VERSION;
1616 k->class_id = PCI_CLASS_OTHERS;
1617 dc->reset = virtio_pci_reset;
1620 static const TypeInfo virtio_pci_info = {
1621 .name = TYPE_VIRTIO_PCI,
1622 .parent = TYPE_PCI_DEVICE,
1623 .instance_size = sizeof(VirtIOPCIProxy),
1624 .class_init = virtio_pci_class_init,
1625 .class_size = sizeof(VirtioPCIClass),
1626 .abstract = true,
1629 /* virtio-blk-pci */
1631 static Property virtio_blk_pci_properties[] = {
1632 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1633 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1634 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1635 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1636 DEFINE_PROP_END_OF_LIST(),
1639 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1641 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1642 DeviceState *vdev = DEVICE(&dev->vdev);
1644 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1645 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1648 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1650 DeviceClass *dc = DEVICE_CLASS(klass);
1651 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1652 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1654 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1655 dc->props = virtio_blk_pci_properties;
1656 k->realize = virtio_blk_pci_realize;
1657 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1658 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1659 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1660 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1663 static void virtio_blk_pci_instance_init(Object *obj)
1665 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1667 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1668 TYPE_VIRTIO_BLK);
1669 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1670 &error_abort);
1671 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1672 "bootindex", &error_abort);
1675 static const TypeInfo virtio_blk_pci_info = {
1676 .name = TYPE_VIRTIO_BLK_PCI,
1677 .parent = TYPE_VIRTIO_PCI,
1678 .instance_size = sizeof(VirtIOBlkPCI),
1679 .instance_init = virtio_blk_pci_instance_init,
1680 .class_init = virtio_blk_pci_class_init,
1683 /* virtio-scsi-pci */
1685 static Property virtio_scsi_pci_properties[] = {
1686 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1687 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1688 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1689 DEV_NVECTORS_UNSPECIFIED),
1690 DEFINE_PROP_END_OF_LIST(),
1693 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1695 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1696 DeviceState *vdev = DEVICE(&dev->vdev);
1697 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1698 DeviceState *proxy = DEVICE(vpci_dev);
1699 char *bus_name;
1701 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1702 vpci_dev->nvectors = vs->conf.num_queues + 3;
1706 * For command line compatibility, this sets the virtio-scsi-device bus
1707 * name as before.
1709 if (proxy->id) {
1710 bus_name = g_strdup_printf("%s.0", proxy->id);
1711 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1712 g_free(bus_name);
1715 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1716 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1719 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1721 DeviceClass *dc = DEVICE_CLASS(klass);
1722 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1723 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1725 k->realize = virtio_scsi_pci_realize;
1726 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1727 dc->props = virtio_scsi_pci_properties;
1728 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1729 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1730 pcidev_k->revision = 0x00;
1731 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1734 static void virtio_scsi_pci_instance_init(Object *obj)
1736 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1738 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1739 TYPE_VIRTIO_SCSI);
1740 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1741 &error_abort);
1744 static const TypeInfo virtio_scsi_pci_info = {
1745 .name = TYPE_VIRTIO_SCSI_PCI,
1746 .parent = TYPE_VIRTIO_PCI,
1747 .instance_size = sizeof(VirtIOSCSIPCI),
1748 .instance_init = virtio_scsi_pci_instance_init,
1749 .class_init = virtio_scsi_pci_class_init,
1752 /* vhost-scsi-pci */
1754 #ifdef CONFIG_VHOST_SCSI
1755 static Property vhost_scsi_pci_properties[] = {
1756 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1757 DEV_NVECTORS_UNSPECIFIED),
1758 DEFINE_PROP_END_OF_LIST(),
1761 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1763 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1764 DeviceState *vdev = DEVICE(&dev->vdev);
1765 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1767 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1768 vpci_dev->nvectors = vs->conf.num_queues + 3;
1771 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1772 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1775 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1777 DeviceClass *dc = DEVICE_CLASS(klass);
1778 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1779 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1780 k->realize = vhost_scsi_pci_realize;
1781 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1782 dc->props = vhost_scsi_pci_properties;
1783 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1784 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1785 pcidev_k->revision = 0x00;
1786 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1789 static void vhost_scsi_pci_instance_init(Object *obj)
1791 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1793 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1794 TYPE_VHOST_SCSI);
1795 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1796 "bootindex", &error_abort);
1799 static const TypeInfo vhost_scsi_pci_info = {
1800 .name = TYPE_VHOST_SCSI_PCI,
1801 .parent = TYPE_VIRTIO_PCI,
1802 .instance_size = sizeof(VHostSCSIPCI),
1803 .instance_init = vhost_scsi_pci_instance_init,
1804 .class_init = vhost_scsi_pci_class_init,
1806 #endif
1808 /* virtio-balloon-pci */
1810 static Property virtio_balloon_pci_properties[] = {
1811 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1812 DEFINE_PROP_END_OF_LIST(),
1815 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1817 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1818 DeviceState *vdev = DEVICE(&dev->vdev);
1820 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1821 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1822 vpci_dev->class_code = PCI_CLASS_OTHERS;
1825 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1826 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1829 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1831 DeviceClass *dc = DEVICE_CLASS(klass);
1832 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1833 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1834 k->realize = virtio_balloon_pci_realize;
1835 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1836 dc->props = virtio_balloon_pci_properties;
1837 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1838 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1839 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1840 pcidev_k->class_id = PCI_CLASS_OTHERS;
1843 static void virtio_balloon_pci_instance_init(Object *obj)
1845 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1847 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1848 TYPE_VIRTIO_BALLOON);
1849 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
1850 "guest-stats", &error_abort);
1851 object_property_add_alias(obj, "guest-stats-polling-interval",
1852 OBJECT(&dev->vdev),
1853 "guest-stats-polling-interval", &error_abort);
1856 static const TypeInfo virtio_balloon_pci_info = {
1857 .name = TYPE_VIRTIO_BALLOON_PCI,
1858 .parent = TYPE_VIRTIO_PCI,
1859 .instance_size = sizeof(VirtIOBalloonPCI),
1860 .instance_init = virtio_balloon_pci_instance_init,
1861 .class_init = virtio_balloon_pci_class_init,
1864 /* virtio-serial-pci */
1866 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1868 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1869 DeviceState *vdev = DEVICE(&dev->vdev);
1870 DeviceState *proxy = DEVICE(vpci_dev);
1871 char *bus_name;
1873 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1874 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1875 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
1876 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1879 /* backwards-compatibility with machines that were created with
1880 DEV_NVECTORS_UNSPECIFIED */
1881 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1882 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1886 * For command line compatibility, this sets the virtio-serial-device bus
1887 * name as before.
1889 if (proxy->id) {
1890 bus_name = g_strdup_printf("%s.0", proxy->id);
1891 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1892 g_free(bus_name);
1895 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1896 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1899 static Property virtio_serial_pci_properties[] = {
1900 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1901 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1902 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1903 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1904 DEFINE_PROP_END_OF_LIST(),
1907 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1909 DeviceClass *dc = DEVICE_CLASS(klass);
1910 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1911 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1912 k->realize = virtio_serial_pci_realize;
1913 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1914 dc->props = virtio_serial_pci_properties;
1915 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1916 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1917 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1918 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1921 static void virtio_serial_pci_instance_init(Object *obj)
1923 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1925 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1926 TYPE_VIRTIO_SERIAL);
1929 static const TypeInfo virtio_serial_pci_info = {
1930 .name = TYPE_VIRTIO_SERIAL_PCI,
1931 .parent = TYPE_VIRTIO_PCI,
1932 .instance_size = sizeof(VirtIOSerialPCI),
1933 .instance_init = virtio_serial_pci_instance_init,
1934 .class_init = virtio_serial_pci_class_init,
1937 /* virtio-net-pci */
1939 static Property virtio_net_properties[] = {
1940 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1941 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1942 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1943 DEFINE_PROP_END_OF_LIST(),
1946 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1948 DeviceState *qdev = DEVICE(vpci_dev);
1949 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1950 DeviceState *vdev = DEVICE(&dev->vdev);
1952 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1953 object_get_typename(OBJECT(qdev)));
1954 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1955 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1958 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1960 DeviceClass *dc = DEVICE_CLASS(klass);
1961 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1962 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1964 k->romfile = "efi-virtio.rom";
1965 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1966 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1967 k->revision = VIRTIO_PCI_ABI_VERSION;
1968 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1969 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1970 dc->props = virtio_net_properties;
1971 vpciklass->realize = virtio_net_pci_realize;
1974 static void virtio_net_pci_instance_init(Object *obj)
1976 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
1978 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1979 TYPE_VIRTIO_NET);
1980 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1981 "bootindex", &error_abort);
1984 static const TypeInfo virtio_net_pci_info = {
1985 .name = TYPE_VIRTIO_NET_PCI,
1986 .parent = TYPE_VIRTIO_PCI,
1987 .instance_size = sizeof(VirtIONetPCI),
1988 .instance_init = virtio_net_pci_instance_init,
1989 .class_init = virtio_net_pci_class_init,
1992 /* virtio-rng-pci */
1994 static Property virtio_rng_pci_properties[] = {
1995 DEFINE_PROP_END_OF_LIST(),
1998 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2000 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2001 DeviceState *vdev = DEVICE(&vrng->vdev);
2002 Error *err = NULL;
2004 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2005 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2006 if (err) {
2007 error_propagate(errp, err);
2008 return;
2011 object_property_set_link(OBJECT(vrng),
2012 OBJECT(vrng->vdev.conf.rng), "rng",
2013 NULL);
2016 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2018 DeviceClass *dc = DEVICE_CLASS(klass);
2019 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2020 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2022 k->realize = virtio_rng_pci_realize;
2023 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2024 dc->props = virtio_rng_pci_properties;
2026 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2027 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2028 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2029 pcidev_k->class_id = PCI_CLASS_OTHERS;
2032 static void virtio_rng_initfn(Object *obj)
2034 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2036 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2037 TYPE_VIRTIO_RNG);
2038 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2039 &error_abort);
2042 static const TypeInfo virtio_rng_pci_info = {
2043 .name = TYPE_VIRTIO_RNG_PCI,
2044 .parent = TYPE_VIRTIO_PCI,
2045 .instance_size = sizeof(VirtIORngPCI),
2046 .instance_init = virtio_rng_initfn,
2047 .class_init = virtio_rng_pci_class_init,
2050 /* virtio-input-pci */
2052 static Property virtio_input_pci_properties[] = {
2053 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2054 DEFINE_PROP_END_OF_LIST(),
2057 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2059 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2060 DeviceState *vdev = DEVICE(&vinput->vdev);
2062 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2063 /* force virtio-1.0 */
2064 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
2065 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
2066 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2069 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2071 DeviceClass *dc = DEVICE_CLASS(klass);
2072 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2073 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2075 dc->props = virtio_input_pci_properties;
2076 k->realize = virtio_input_pci_realize;
2077 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2079 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2082 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2084 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2086 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2089 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2090 void *data)
2092 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2094 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2097 static void virtio_keyboard_initfn(Object *obj)
2099 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2101 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2102 TYPE_VIRTIO_KEYBOARD);
2105 static void virtio_mouse_initfn(Object *obj)
2107 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2109 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2110 TYPE_VIRTIO_MOUSE);
2113 static void virtio_tablet_initfn(Object *obj)
2115 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2117 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2118 TYPE_VIRTIO_TABLET);
2121 static void virtio_host_initfn(Object *obj)
2123 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2125 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2126 TYPE_VIRTIO_INPUT_HOST);
2129 static const TypeInfo virtio_input_pci_info = {
2130 .name = TYPE_VIRTIO_INPUT_PCI,
2131 .parent = TYPE_VIRTIO_PCI,
2132 .instance_size = sizeof(VirtIOInputPCI),
2133 .class_init = virtio_input_pci_class_init,
2134 .abstract = true,
2137 static const TypeInfo virtio_input_hid_pci_info = {
2138 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2139 .parent = TYPE_VIRTIO_INPUT_PCI,
2140 .instance_size = sizeof(VirtIOInputHIDPCI),
2141 .abstract = true,
2144 static const TypeInfo virtio_keyboard_pci_info = {
2145 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2146 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2147 .class_init = virtio_input_hid_kbd_pci_class_init,
2148 .instance_size = sizeof(VirtIOInputHIDPCI),
2149 .instance_init = virtio_keyboard_initfn,
2152 static const TypeInfo virtio_mouse_pci_info = {
2153 .name = TYPE_VIRTIO_MOUSE_PCI,
2154 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2155 .class_init = virtio_input_hid_mouse_pci_class_init,
2156 .instance_size = sizeof(VirtIOInputHIDPCI),
2157 .instance_init = virtio_mouse_initfn,
2160 static const TypeInfo virtio_tablet_pci_info = {
2161 .name = TYPE_VIRTIO_TABLET_PCI,
2162 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2163 .instance_size = sizeof(VirtIOInputHIDPCI),
2164 .instance_init = virtio_tablet_initfn,
2167 static const TypeInfo virtio_host_pci_info = {
2168 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2169 .parent = TYPE_VIRTIO_INPUT_PCI,
2170 .instance_size = sizeof(VirtIOInputHostPCI),
2171 .instance_init = virtio_host_initfn,
2174 /* virtio-pci-bus */
2176 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2177 VirtIOPCIProxy *dev)
2179 DeviceState *qdev = DEVICE(dev);
2180 char virtio_bus_name[] = "virtio-bus";
2182 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2183 virtio_bus_name);
2186 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2188 BusClass *bus_class = BUS_CLASS(klass);
2189 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2190 bus_class->max_dev = 1;
2191 k->notify = virtio_pci_notify;
2192 k->save_config = virtio_pci_save_config;
2193 k->load_config = virtio_pci_load_config;
2194 k->save_queue = virtio_pci_save_queue;
2195 k->load_queue = virtio_pci_load_queue;
2196 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2197 k->set_host_notifier = virtio_pci_set_host_notifier;
2198 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2199 k->vmstate_change = virtio_pci_vmstate_change;
2200 k->device_plugged = virtio_pci_device_plugged;
2201 k->device_unplugged = virtio_pci_device_unplugged;
2202 k->query_nvectors = virtio_pci_query_nvectors;
2205 static const TypeInfo virtio_pci_bus_info = {
2206 .name = TYPE_VIRTIO_PCI_BUS,
2207 .parent = TYPE_VIRTIO_BUS,
2208 .instance_size = sizeof(VirtioPCIBusState),
2209 .class_init = virtio_pci_bus_class_init,
2212 static void virtio_pci_register_types(void)
2214 type_register_static(&virtio_rng_pci_info);
2215 type_register_static(&virtio_input_pci_info);
2216 type_register_static(&virtio_input_hid_pci_info);
2217 type_register_static(&virtio_keyboard_pci_info);
2218 type_register_static(&virtio_mouse_pci_info);
2219 type_register_static(&virtio_tablet_pci_info);
2220 type_register_static(&virtio_host_pci_info);
2221 type_register_static(&virtio_pci_bus_info);
2222 type_register_static(&virtio_pci_info);
2223 #ifdef CONFIG_VIRTFS
2224 type_register_static(&virtio_9p_pci_info);
2225 #endif
2226 type_register_static(&virtio_blk_pci_info);
2227 type_register_static(&virtio_scsi_pci_info);
2228 type_register_static(&virtio_balloon_pci_info);
2229 type_register_static(&virtio_serial_pci_info);
2230 type_register_static(&virtio_net_pci_info);
2231 #ifdef CONFIG_VHOST_SCSI
2232 type_register_static(&vhost_scsi_pci_info);
2233 #endif
2236 type_init(virtio_pci_register_types)