1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "qemu/osdep.h"
19
20#include "standard-headers/linux/virtio_pci.h"
21#include "hw/virtio/virtio.h"
22#include "hw/virtio/virtio-blk.h"
23#include "hw/virtio/virtio-net.h"
24#include "hw/virtio/virtio-serial.h"
25#include "hw/virtio/virtio-scsi.h"
26#include "hw/virtio/virtio-balloon.h"
27#include "hw/virtio/virtio-input.h"
28#include "hw/pci/pci.h"
29#include "qapi/error.h"
30#include "qemu/error-report.h"
31#include "hw/pci/msi.h"
32#include "hw/pci/msix.h"
33#include "hw/loader.h"
34#include "sysemu/kvm.h"
35#include "sysemu/block-backend.h"
36#include "virtio-pci.h"
37#include "qemu/range.h"
38#include "hw/virtio/virtio-bus.h"
39#include "qapi/visitor.h"
40
41#define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
42
43#undef VIRTIO_PCI_CONFIG
44
45
46
47#define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
48
49static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
50 VirtIOPCIProxy *dev);
51static void virtio_pci_reset(DeviceState *qdev);
52
53
54
55static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
56{
57 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
58}
59
60
61
62
63static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
64{
65 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
66}
67
68static void virtio_pci_notify(DeviceState *d, uint16_t vector)
69{
70 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
71
72 if (msix_enabled(&proxy->pci_dev))
73 msix_notify(&proxy->pci_dev, vector);
74 else {
75 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
76 pci_set_irq(&proxy->pci_dev, atomic_read(&vdev->isr) & 1);
77 }
78}
79
80static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
81{
82 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
83 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
84
85 pci_device_save(&proxy->pci_dev, f);
86 msix_save(&proxy->pci_dev, f);
87 if (msix_present(&proxy->pci_dev))
88 qemu_put_be16(f, vdev->config_vector);
89}
90
91static const VMStateDescription vmstate_virtio_pci_modern_queue_state = {
92 .name = "virtio_pci/modern_queue_state",
93 .version_id = 1,
94 .minimum_version_id = 1,
95 .fields = (VMStateField[]) {
96 VMSTATE_UINT16(num, VirtIOPCIQueue),
97 VMSTATE_UNUSED(1),
98 VMSTATE_BOOL(enabled, VirtIOPCIQueue),
99 VMSTATE_UINT32_ARRAY(desc, VirtIOPCIQueue, 2),
100 VMSTATE_UINT32_ARRAY(avail, VirtIOPCIQueue, 2),
101 VMSTATE_UINT32_ARRAY(used, VirtIOPCIQueue, 2),
102 VMSTATE_END_OF_LIST()
103 }
104};
105
106static bool virtio_pci_modern_state_needed(void *opaque)
107{
108 VirtIOPCIProxy *proxy = opaque;
109
110 return virtio_pci_modern(proxy);
111}
112
113static const VMStateDescription vmstate_virtio_pci_modern_state_sub = {
114 .name = "virtio_pci/modern_state",
115 .version_id = 1,
116 .minimum_version_id = 1,
117 .needed = &virtio_pci_modern_state_needed,
118 .fields = (VMStateField[]) {
119 VMSTATE_UINT32(dfselect, VirtIOPCIProxy),
120 VMSTATE_UINT32(gfselect, VirtIOPCIProxy),
121 VMSTATE_UINT32_ARRAY(guest_features, VirtIOPCIProxy, 2),
122 VMSTATE_STRUCT_ARRAY(vqs, VirtIOPCIProxy, VIRTIO_QUEUE_MAX, 0,
123 vmstate_virtio_pci_modern_queue_state,
124 VirtIOPCIQueue),
125 VMSTATE_END_OF_LIST()
126 }
127};
128
129static const VMStateDescription vmstate_virtio_pci = {
130 .name = "virtio_pci",
131 .version_id = 1,
132 .minimum_version_id = 1,
133 .minimum_version_id_old = 1,
134 .fields = (VMStateField[]) {
135 VMSTATE_END_OF_LIST()
136 },
137 .subsections = (const VMStateDescription*[]) {
138 &vmstate_virtio_pci_modern_state_sub,
139 NULL
140 }
141};
142
143static bool virtio_pci_has_extra_state(DeviceState *d)
144{
145 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
146
147 return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
148}
149
150static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
151{
152 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
153
154 vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
155}
156
157static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
158{
159 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
160
161 return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
162}
163
164static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
165{
166 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
167 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
168
169 if (msix_present(&proxy->pci_dev))
170 qemu_put_be16(f, virtio_queue_vector(vdev, n));
171}
172
173static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
174{
175 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
176 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
177
178 int ret;
179 ret = pci_device_load(&proxy->pci_dev, f);
180 if (ret) {
181 return ret;
182 }
183 msix_unuse_all_vectors(&proxy->pci_dev);
184 msix_load(&proxy->pci_dev, f);
185 if (msix_present(&proxy->pci_dev)) {
186 qemu_get_be16s(f, &vdev->config_vector);
187 } else {
188 vdev->config_vector = VIRTIO_NO_VECTOR;
189 }
190 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
191 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
192 }
193 return 0;
194}
195
196static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
197{
198 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
199 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
200
201 uint16_t vector;
202 if (msix_present(&proxy->pci_dev)) {
203 qemu_get_be16s(f, &vector);
204 } else {
205 vector = VIRTIO_NO_VECTOR;
206 }
207 virtio_queue_set_vector(vdev, n, vector);
208 if (vector != VIRTIO_NO_VECTOR) {
209 return msix_vector_use(&proxy->pci_dev, vector);
210 }
211
212 return 0;
213}
214
215static bool virtio_pci_ioeventfd_enabled(DeviceState *d)
216{
217 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
218
219 return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0;
220}
221
222#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
223
224static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy)
225{
226 return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ?
227 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4;
228}
229
230static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
231 int n, bool assign)
232{
233 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
234 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
235 VirtQueue *vq = virtio_get_queue(vdev, n);
236 bool legacy = virtio_pci_legacy(proxy);
237 bool modern = virtio_pci_modern(proxy);
238 bool fast_mmio = kvm_ioeventfd_any_length_enabled();
239 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
240 MemoryRegion *modern_mr = &proxy->notify.mr;
241 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
242 MemoryRegion *legacy_mr = &proxy->bar;
243 hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) *
244 virtio_get_queue_index(vq);
245 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
246
247 if (assign) {
248 if (modern) {
249 if (fast_mmio) {
250 memory_region_add_eventfd(modern_mr, modern_addr, 0,
251 false, n, notifier);
252 } else {
253 memory_region_add_eventfd(modern_mr, modern_addr, 2,
254 false, n, notifier);
255 }
256 if (modern_pio) {
257 memory_region_add_eventfd(modern_notify_mr, 0, 2,
258 true, n, notifier);
259 }
260 }
261 if (legacy) {
262 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
263 true, n, notifier);
264 }
265 } else {
266 if (modern) {
267 if (fast_mmio) {
268 memory_region_del_eventfd(modern_mr, modern_addr, 0,
269 false, n, notifier);
270 } else {
271 memory_region_del_eventfd(modern_mr, modern_addr, 2,
272 false, n, notifier);
273 }
274 if (modern_pio) {
275 memory_region_del_eventfd(modern_notify_mr, 0, 2,
276 true, n, notifier);
277 }
278 }
279 if (legacy) {
280 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
281 true, n, notifier);
282 }
283 }
284 return 0;
285}
286
287static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
288{
289 virtio_bus_start_ioeventfd(&proxy->bus);
290}
291
292static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
293{
294 virtio_bus_stop_ioeventfd(&proxy->bus);
295}
296
297static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
298{
299 VirtIOPCIProxy *proxy = opaque;
300 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
301 hwaddr pa;
302
303 switch (addr) {
304 case VIRTIO_PCI_GUEST_FEATURES:
305
306 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
307 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
308 }
309 virtio_set_features(vdev, val);
310 break;
311 case VIRTIO_PCI_QUEUE_PFN:
312 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
313 if (pa == 0) {
314 virtio_pci_reset(DEVICE(proxy));
315 }
316 else
317 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
318 break;
319 case VIRTIO_PCI_QUEUE_SEL:
320 if (val < VIRTIO_QUEUE_MAX)
321 vdev->queue_sel = val;
322 break;
323 case VIRTIO_PCI_QUEUE_NOTIFY:
324 if (val < VIRTIO_QUEUE_MAX) {
325 virtio_queue_notify(vdev, val);
326 }
327 break;
328 case VIRTIO_PCI_STATUS:
329 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
330 virtio_pci_stop_ioeventfd(proxy);
331 }
332
333 virtio_set_status(vdev, val & 0xFF);
334
335 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
336 virtio_pci_start_ioeventfd(proxy);
337 }
338
339 if (vdev->status == 0) {
340 virtio_pci_reset(DEVICE(proxy));
341 }
342
343
344
345
346
347 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
348 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
349 proxy->pci_dev.config[PCI_COMMAND] |
350 PCI_COMMAND_MASTER, 1);
351 }
352 break;
353 case VIRTIO_MSI_CONFIG_VECTOR:
354 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
355
356 if (msix_vector_use(&proxy->pci_dev, val) < 0)
357 val = VIRTIO_NO_VECTOR;
358 vdev->config_vector = val;
359 break;
360 case VIRTIO_MSI_QUEUE_VECTOR:
361 msix_vector_unuse(&proxy->pci_dev,
362 virtio_queue_vector(vdev, vdev->queue_sel));
363
364 if (msix_vector_use(&proxy->pci_dev, val) < 0)
365 val = VIRTIO_NO_VECTOR;
366 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
367 break;
368 default:
369 error_report("%s: unexpected address 0x%x value 0x%x",
370 __func__, addr, val);
371 break;
372 }
373}
374
375static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
376{
377 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
378 uint32_t ret = 0xFFFFFFFF;
379
380 switch (addr) {
381 case VIRTIO_PCI_HOST_FEATURES:
382 ret = vdev->host_features;
383 break;
384 case VIRTIO_PCI_GUEST_FEATURES:
385 ret = vdev->guest_features;
386 break;
387 case VIRTIO_PCI_QUEUE_PFN:
388 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
389 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
390 break;
391 case VIRTIO_PCI_QUEUE_NUM:
392 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
393 break;
394 case VIRTIO_PCI_QUEUE_SEL:
395 ret = vdev->queue_sel;
396 break;
397 case VIRTIO_PCI_STATUS:
398 ret = vdev->status;
399 break;
400 case VIRTIO_PCI_ISR:
401
402 ret = atomic_xchg(&vdev->isr, 0);
403 pci_irq_deassert(&proxy->pci_dev);
404 break;
405 case VIRTIO_MSI_CONFIG_VECTOR:
406 ret = vdev->config_vector;
407 break;
408 case VIRTIO_MSI_QUEUE_VECTOR:
409 ret = virtio_queue_vector(vdev, vdev->queue_sel);
410 break;
411 default:
412 break;
413 }
414
415 return ret;
416}
417
418static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
419 unsigned size)
420{
421 VirtIOPCIProxy *proxy = opaque;
422 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
423 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
424 uint64_t val = 0;
425 if (addr < config) {
426 return virtio_ioport_read(proxy, addr);
427 }
428 addr -= config;
429
430 switch (size) {
431 case 1:
432 val = virtio_config_readb(vdev, addr);
433 break;
434 case 2:
435 val = virtio_config_readw(vdev, addr);
436 if (virtio_is_big_endian(vdev)) {
437 val = bswap16(val);
438 }
439 break;
440 case 4:
441 val = virtio_config_readl(vdev, addr);
442 if (virtio_is_big_endian(vdev)) {
443 val = bswap32(val);
444 }
445 break;
446 }
447 return val;
448}
449
450static void virtio_pci_config_write(void *opaque, hwaddr addr,
451 uint64_t val, unsigned size)
452{
453 VirtIOPCIProxy *proxy = opaque;
454 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
455 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
456 if (addr < config) {
457 virtio_ioport_write(proxy, addr, val);
458 return;
459 }
460 addr -= config;
461
462
463
464
465 switch (size) {
466 case 1:
467 virtio_config_writeb(vdev, addr, val);
468 break;
469 case 2:
470 if (virtio_is_big_endian(vdev)) {
471 val = bswap16(val);
472 }
473 virtio_config_writew(vdev, addr, val);
474 break;
475 case 4:
476 if (virtio_is_big_endian(vdev)) {
477 val = bswap32(val);
478 }
479 virtio_config_writel(vdev, addr, val);
480 break;
481 }
482}
483
484static const MemoryRegionOps virtio_pci_config_ops = {
485 .read = virtio_pci_config_read,
486 .write = virtio_pci_config_write,
487 .impl = {
488 .min_access_size = 1,
489 .max_access_size = 4,
490 },
491 .endianness = DEVICE_LITTLE_ENDIAN,
492};
493
494static MemoryRegion *virtio_address_space_lookup(VirtIOPCIProxy *proxy,
495 hwaddr *off, int len)
496{
497 int i;
498 VirtIOPCIRegion *reg;
499
500 for (i = 0; i < ARRAY_SIZE(proxy->regs); ++i) {
501 reg = &proxy->regs[i];
502 if (*off >= reg->offset &&
503 *off + len <= reg->offset + reg->size) {
504 *off -= reg->offset;
505 return ®->mr;
506 }
507 }
508
509 return NULL;
510}
511
512
513
514
515
516
517
518
519
520
521
522
523
524static
525void virtio_address_space_write(VirtIOPCIProxy *proxy, hwaddr addr,
526 const uint8_t *buf, int len)
527{
528 uint64_t val;
529 MemoryRegion *mr;
530
531
532
533
534 addr &= ~(len - 1);
535
536 mr = virtio_address_space_lookup(proxy, &addr, len);
537 if (!mr) {
538 return;
539 }
540
541
542 assert(!(((uintptr_t)buf) & (len - 1)));
543
544 switch (len) {
545 case 1:
546 val = pci_get_byte(buf);
547 break;
548 case 2:
549 val = cpu_to_le16(pci_get_word(buf));
550 break;
551 case 4:
552 val = cpu_to_le32(pci_get_long(buf));
553 break;
554 default:
555
556 return;
557 }
558 memory_region_dispatch_write(mr, addr, val, len, MEMTXATTRS_UNSPECIFIED);
559}
560
561static void
562virtio_address_space_read(VirtIOPCIProxy *proxy, hwaddr addr,
563 uint8_t *buf, int len)
564{
565 uint64_t val;
566 MemoryRegion *mr;
567
568
569
570
571 addr &= ~(len - 1);
572
573 mr = virtio_address_space_lookup(proxy, &addr, len);
574 if (!mr) {
575 return;
576 }
577
578
579 assert(!(((uintptr_t)buf) & (len - 1)));
580
581 memory_region_dispatch_read(mr, addr, &val, len, MEMTXATTRS_UNSPECIFIED);
582 switch (len) {
583 case 1:
584 pci_set_byte(buf, val);
585 break;
586 case 2:
587 pci_set_word(buf, le16_to_cpu(val));
588 break;
589 case 4:
590 pci_set_long(buf, le32_to_cpu(val));
591 break;
592 default:
593
594 break;
595 }
596}
597
598static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
599 uint32_t val, int len)
600{
601 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
602 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
603 struct virtio_pci_cfg_cap *cfg;
604
605 pci_default_write_config(pci_dev, address, val, len);
606
607 if (range_covers_byte(address, len, PCI_COMMAND) &&
608 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
609 virtio_pci_stop_ioeventfd(proxy);
610 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
611 }
612
613 if (proxy->config_cap &&
614 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
615 pci_cfg_data),
616 sizeof cfg->pci_cfg_data)) {
617 uint32_t off;
618 uint32_t len;
619
620 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
621 off = le32_to_cpu(cfg->cap.offset);
622 len = le32_to_cpu(cfg->cap.length);
623
624 if (len == 1 || len == 2 || len == 4) {
625 assert(len <= sizeof cfg->pci_cfg_data);
626 virtio_address_space_write(proxy, off, cfg->pci_cfg_data, len);
627 }
628 }
629}
630
631static uint32_t virtio_read_config(PCIDevice *pci_dev,
632 uint32_t address, int len)
633{
634 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
635 struct virtio_pci_cfg_cap *cfg;
636
637 if (proxy->config_cap &&
638 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
639 pci_cfg_data),
640 sizeof cfg->pci_cfg_data)) {
641 uint32_t off;
642 uint32_t len;
643
644 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
645 off = le32_to_cpu(cfg->cap.offset);
646 len = le32_to_cpu(cfg->cap.length);
647
648 if (len == 1 || len == 2 || len == 4) {
649 assert(len <= sizeof cfg->pci_cfg_data);
650 virtio_address_space_read(proxy, off, cfg->pci_cfg_data, len);
651 }
652 }
653
654 return pci_default_read_config(pci_dev, address, len);
655}
656
657static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
658 unsigned int queue_no,
659 unsigned int vector)
660{
661 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
662 int ret;
663
664 if (irqfd->users == 0) {
665 ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev);
666 if (ret < 0) {
667 return ret;
668 }
669 irqfd->virq = ret;
670 }
671 irqfd->users++;
672 return 0;
673}
674
675static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
676 unsigned int vector)
677{
678 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
679 if (--irqfd->users == 0) {
680 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
681 }
682}
683
684static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
685 unsigned int queue_no,
686 unsigned int vector)
687{
688 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
689 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
690 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
691 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
692 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
693}
694
695static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
696 unsigned int queue_no,
697 unsigned int vector)
698{
699 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
700 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
701 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
702 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
703 int ret;
704
705 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
706 assert(ret == 0);
707}
708
709static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
710{
711 PCIDevice *dev = &proxy->pci_dev;
712 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
713 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
714 unsigned int vector;
715 int ret, queue_no;
716
717 for (queue_no = 0; queue_no < nvqs; queue_no++) {
718 if (!virtio_queue_get_num(vdev, queue_no)) {
719 break;
720 }
721 vector = virtio_queue_vector(vdev, queue_no);
722 if (vector >= msix_nr_vectors_allocated(dev)) {
723 continue;
724 }
725 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
726 if (ret < 0) {
727 goto undo;
728 }
729
730
731
732 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
733 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
734 if (ret < 0) {
735 kvm_virtio_pci_vq_vector_release(proxy, vector);
736 goto undo;
737 }
738 }
739 }
740 return 0;
741
742undo:
743 while (--queue_no >= 0) {
744 vector = virtio_queue_vector(vdev, queue_no);
745 if (vector >= msix_nr_vectors_allocated(dev)) {
746 continue;
747 }
748 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
749 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
750 }
751 kvm_virtio_pci_vq_vector_release(proxy, vector);
752 }
753 return ret;
754}
755
756static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
757{
758 PCIDevice *dev = &proxy->pci_dev;
759 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
760 unsigned int vector;
761 int queue_no;
762 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
763
764 for (queue_no = 0; queue_no < nvqs; queue_no++) {
765 if (!virtio_queue_get_num(vdev, queue_no)) {
766 break;
767 }
768 vector = virtio_queue_vector(vdev, queue_no);
769 if (vector >= msix_nr_vectors_allocated(dev)) {
770 continue;
771 }
772
773
774
775 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
776 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
777 }
778 kvm_virtio_pci_vq_vector_release(proxy, vector);
779 }
780}
781
782static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
783 unsigned int queue_no,
784 unsigned int vector,
785 MSIMessage msg)
786{
787 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
788 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
789 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
790 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
791 VirtIOIRQFD *irqfd;
792 int ret = 0;
793
794 if (proxy->vector_irqfd) {
795 irqfd = &proxy->vector_irqfd[vector];
796 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
797 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
798 &proxy->pci_dev);
799 if (ret < 0) {
800 return ret;
801 }
802 kvm_irqchip_commit_routes(kvm_state);
803 }
804 }
805
806
807
808
809 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
810 k->guest_notifier_mask(vdev, queue_no, false);
811
812 if (k->guest_notifier_pending &&
813 k->guest_notifier_pending(vdev, queue_no)) {
814 event_notifier_set(n);
815 }
816 } else {
817 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
818 }
819 return ret;
820}
821
822static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
823 unsigned int queue_no,
824 unsigned int vector)
825{
826 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
827 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
828
829
830
831
832 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
833 k->guest_notifier_mask(vdev, queue_no, true);
834 } else {
835 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
836 }
837}
838
839static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
840 MSIMessage msg)
841{
842 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
843 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
844 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
845 int ret, index, unmasked = 0;
846
847 while (vq) {
848 index = virtio_get_queue_index(vq);
849 if (!virtio_queue_get_num(vdev, index)) {
850 break;
851 }
852 if (index < proxy->nvqs_with_notifiers) {
853 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
854 if (ret < 0) {
855 goto undo;
856 }
857 ++unmasked;
858 }
859 vq = virtio_vector_next_queue(vq);
860 }
861
862 return 0;
863
864undo:
865 vq = virtio_vector_first_queue(vdev, vector);
866 while (vq && unmasked >= 0) {
867 index = virtio_get_queue_index(vq);
868 if (index < proxy->nvqs_with_notifiers) {
869 virtio_pci_vq_vector_mask(proxy, index, vector);
870 --unmasked;
871 }
872 vq = virtio_vector_next_queue(vq);
873 }
874 return ret;
875}
876
877static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
878{
879 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
880 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
881 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
882 int index;
883
884 while (vq) {
885 index = virtio_get_queue_index(vq);
886 if (!virtio_queue_get_num(vdev, index)) {
887 break;
888 }
889 if (index < proxy->nvqs_with_notifiers) {
890 virtio_pci_vq_vector_mask(proxy, index, vector);
891 }
892 vq = virtio_vector_next_queue(vq);
893 }
894}
895
896static void virtio_pci_vector_poll(PCIDevice *dev,
897 unsigned int vector_start,
898 unsigned int vector_end)
899{
900 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
901 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
902 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
903 int queue_no;
904 unsigned int vector;
905 EventNotifier *notifier;
906 VirtQueue *vq;
907
908 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
909 if (!virtio_queue_get_num(vdev, queue_no)) {
910 break;
911 }
912 vector = virtio_queue_vector(vdev, queue_no);
913 if (vector < vector_start || vector >= vector_end ||
914 !msix_is_masked(dev, vector)) {
915 continue;
916 }
917 vq = virtio_get_queue(vdev, queue_no);
918 notifier = virtio_queue_get_guest_notifier(vq);
919 if (k->guest_notifier_pending) {
920 if (k->guest_notifier_pending(vdev, queue_no)) {
921 msix_set_pending(dev, vector);
922 }
923 } else if (event_notifier_test_and_clear(notifier)) {
924 msix_set_pending(dev, vector);
925 }
926 }
927}
928
929static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
930 bool with_irqfd)
931{
932 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
933 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
934 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
935 VirtQueue *vq = virtio_get_queue(vdev, n);
936 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
937
938 if (assign) {
939 int r = event_notifier_init(notifier, 0);
940 if (r < 0) {
941 return r;
942 }
943 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
944 } else {
945 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
946 event_notifier_cleanup(notifier);
947 }
948
949 if (!msix_enabled(&proxy->pci_dev) &&
950 vdev->use_guest_notifier_mask &&
951 vdc->guest_notifier_mask) {
952 vdc->guest_notifier_mask(vdev, n, !assign);
953 }
954
955 return 0;
956}
957
958static bool virtio_pci_query_guest_notifiers(DeviceState *d)
959{
960 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
961 return msix_enabled(&proxy->pci_dev);
962}
963
964static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
965{
966 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
967 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
968 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
969 int r, n;
970 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
971 kvm_msi_via_irqfd_enabled();
972
973 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
974
975
976
977
978 assert(assign || nvqs == proxy->nvqs_with_notifiers);
979
980 proxy->nvqs_with_notifiers = nvqs;
981
982
983 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
984 msix_unset_vector_notifiers(&proxy->pci_dev);
985 if (proxy->vector_irqfd) {
986 kvm_virtio_pci_vector_release(proxy, nvqs);
987 g_free(proxy->vector_irqfd);
988 proxy->vector_irqfd = NULL;
989 }
990 }
991
992 for (n = 0; n < nvqs; n++) {
993 if (!virtio_queue_get_num(vdev, n)) {
994 break;
995 }
996
997 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
998 if (r < 0) {
999 goto assign_error;
1000 }
1001 }
1002
1003
1004 if ((with_irqfd || k->guest_notifier_mask) && assign) {
1005 if (with_irqfd) {
1006 proxy->vector_irqfd =
1007 g_malloc0(sizeof(*proxy->vector_irqfd) *
1008 msix_nr_vectors_allocated(&proxy->pci_dev));
1009 r = kvm_virtio_pci_vector_use(proxy, nvqs);
1010 if (r < 0) {
1011 goto assign_error;
1012 }
1013 }
1014 r = msix_set_vector_notifiers(&proxy->pci_dev,
1015 virtio_pci_vector_unmask,
1016 virtio_pci_vector_mask,
1017 virtio_pci_vector_poll);
1018 if (r < 0) {
1019 goto notifiers_error;
1020 }
1021 }
1022
1023 return 0;
1024
1025notifiers_error:
1026 if (with_irqfd) {
1027 assert(assign);
1028 kvm_virtio_pci_vector_release(proxy, nvqs);
1029 }
1030
1031assign_error:
1032
1033 assert(assign);
1034 while (--n >= 0) {
1035 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1036 }
1037 return r;
1038}
1039
1040static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1041{
1042 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1043 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1044
1045 if (running) {
1046
1047
1048
1049 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
1050 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
1051 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1052 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
1053 proxy->pci_dev.config[PCI_COMMAND] |
1054 PCI_COMMAND_MASTER, 1);
1055 }
1056 virtio_pci_start_ioeventfd(proxy);
1057 } else {
1058 virtio_pci_stop_ioeventfd(proxy);
1059 }
1060}
1061
1062#ifdef CONFIG_VIRTFS
1063static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1064{
1065 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1066 DeviceState *vdev = DEVICE(&dev->vdev);
1067
1068 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1069 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1070}
1071
1072static Property virtio_9p_pci_properties[] = {
1073 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1074 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1075 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1076 DEFINE_PROP_END_OF_LIST(),
1077};
1078
1079static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1080{
1081 DeviceClass *dc = DEVICE_CLASS(klass);
1082 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1083 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1084
1085 k->realize = virtio_9p_pci_realize;
1086 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1087 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1088 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1089 pcidev_k->class_id = 0x2;
1090 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1091 dc->props = virtio_9p_pci_properties;
1092}
1093
1094static void virtio_9p_pci_instance_init(Object *obj)
1095{
1096 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1097
1098 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1099 TYPE_VIRTIO_9P);
1100}
1101
1102static const TypeInfo virtio_9p_pci_info = {
1103 .name = TYPE_VIRTIO_9P_PCI,
1104 .parent = TYPE_VIRTIO_PCI,
1105 .instance_size = sizeof(V9fsPCIState),
1106 .instance_init = virtio_9p_pci_instance_init,
1107 .class_init = virtio_9p_pci_class_init,
1108};
1109#endif
1110
1111
1112
1113
1114
1115static int virtio_pci_query_nvectors(DeviceState *d)
1116{
1117 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1118
1119 return proxy->nvectors;
1120}
1121
1122static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
1123{
1124 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1125 PCIDevice *dev = &proxy->pci_dev;
1126
1127 return pci_get_address_space(dev);
1128}
1129
1130static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1131 struct virtio_pci_cap *cap)
1132{
1133 PCIDevice *dev = &proxy->pci_dev;
1134 int offset;
1135
1136 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0,
1137 cap->cap_len, &error_abort);
1138
1139 assert(cap->cap_len >= sizeof *cap);
1140 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1141 cap->cap_len - PCI_CAP_FLAGS);
1142
1143 return offset;
1144}
1145
1146static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1147 unsigned size)
1148{
1149 VirtIOPCIProxy *proxy = opaque;
1150 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1151 uint32_t val = 0;
1152 int i;
1153
1154 switch (addr) {
1155 case VIRTIO_PCI_COMMON_DFSELECT:
1156 val = proxy->dfselect;
1157 break;
1158 case VIRTIO_PCI_COMMON_DF:
1159 if (proxy->dfselect <= 1) {
1160 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1161
1162 val = (vdev->host_features & ~vdc->legacy_features) >>
1163 (32 * proxy->dfselect);
1164 }
1165 break;
1166 case VIRTIO_PCI_COMMON_GFSELECT:
1167 val = proxy->gfselect;
1168 break;
1169 case VIRTIO_PCI_COMMON_GF:
1170 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1171 val = proxy->guest_features[proxy->gfselect];
1172 }
1173 break;
1174 case VIRTIO_PCI_COMMON_MSIX:
1175 val = vdev->config_vector;
1176 break;
1177 case VIRTIO_PCI_COMMON_NUMQ:
1178 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1179 if (virtio_queue_get_num(vdev, i)) {
1180 val = i + 1;
1181 }
1182 }
1183 break;
1184 case VIRTIO_PCI_COMMON_STATUS:
1185 val = vdev->status;
1186 break;
1187 case VIRTIO_PCI_COMMON_CFGGENERATION:
1188 val = vdev->generation;
1189 break;
1190 case VIRTIO_PCI_COMMON_Q_SELECT:
1191 val = vdev->queue_sel;
1192 break;
1193 case VIRTIO_PCI_COMMON_Q_SIZE:
1194 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1195 break;
1196 case VIRTIO_PCI_COMMON_Q_MSIX:
1197 val = virtio_queue_vector(vdev, vdev->queue_sel);
1198 break;
1199 case VIRTIO_PCI_COMMON_Q_ENABLE:
1200 val = proxy->vqs[vdev->queue_sel].enabled;
1201 break;
1202 case VIRTIO_PCI_COMMON_Q_NOFF:
1203
1204 val = vdev->queue_sel;
1205 break;
1206 case VIRTIO_PCI_COMMON_Q_DESCLO:
1207 val = proxy->vqs[vdev->queue_sel].desc[0];
1208 break;
1209 case VIRTIO_PCI_COMMON_Q_DESCHI:
1210 val = proxy->vqs[vdev->queue_sel].desc[1];
1211 break;
1212 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1213 val = proxy->vqs[vdev->queue_sel].avail[0];
1214 break;
1215 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1216 val = proxy->vqs[vdev->queue_sel].avail[1];
1217 break;
1218 case VIRTIO_PCI_COMMON_Q_USEDLO:
1219 val = proxy->vqs[vdev->queue_sel].used[0];
1220 break;
1221 case VIRTIO_PCI_COMMON_Q_USEDHI:
1222 val = proxy->vqs[vdev->queue_sel].used[1];
1223 break;
1224 default:
1225 val = 0;
1226 }
1227
1228 return val;
1229}
1230
1231static void virtio_pci_common_write(void *opaque, hwaddr addr,
1232 uint64_t val, unsigned size)
1233{
1234 VirtIOPCIProxy *proxy = opaque;
1235 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1236
1237 switch (addr) {
1238 case VIRTIO_PCI_COMMON_DFSELECT:
1239 proxy->dfselect = val;
1240 break;
1241 case VIRTIO_PCI_COMMON_GFSELECT:
1242 proxy->gfselect = val;
1243 break;
1244 case VIRTIO_PCI_COMMON_GF:
1245 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1246 proxy->guest_features[proxy->gfselect] = val;
1247 virtio_set_features(vdev,
1248 (((uint64_t)proxy->guest_features[1]) << 32) |
1249 proxy->guest_features[0]);
1250 }
1251 break;
1252 case VIRTIO_PCI_COMMON_MSIX:
1253 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1254
1255 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1256 val = VIRTIO_NO_VECTOR;
1257 }
1258 vdev->config_vector = val;
1259 break;
1260 case VIRTIO_PCI_COMMON_STATUS:
1261 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1262 virtio_pci_stop_ioeventfd(proxy);
1263 }
1264
1265 virtio_set_status(vdev, val & 0xFF);
1266
1267 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1268 virtio_pci_start_ioeventfd(proxy);
1269 }
1270
1271 if (vdev->status == 0) {
1272 virtio_pci_reset(DEVICE(proxy));
1273 }
1274
1275 break;
1276 case VIRTIO_PCI_COMMON_Q_SELECT:
1277 if (val < VIRTIO_QUEUE_MAX) {
1278 vdev->queue_sel = val;
1279 }
1280 break;
1281 case VIRTIO_PCI_COMMON_Q_SIZE:
1282 proxy->vqs[vdev->queue_sel].num = val;
1283 break;
1284 case VIRTIO_PCI_COMMON_Q_MSIX:
1285 msix_vector_unuse(&proxy->pci_dev,
1286 virtio_queue_vector(vdev, vdev->queue_sel));
1287
1288 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1289 val = VIRTIO_NO_VECTOR;
1290 }
1291 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1292 break;
1293 case VIRTIO_PCI_COMMON_Q_ENABLE:
1294 virtio_queue_set_num(vdev, vdev->queue_sel,
1295 proxy->vqs[vdev->queue_sel].num);
1296 virtio_queue_set_rings(vdev, vdev->queue_sel,
1297 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1298 proxy->vqs[vdev->queue_sel].desc[0],
1299 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1300 proxy->vqs[vdev->queue_sel].avail[0],
1301 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1302 proxy->vqs[vdev->queue_sel].used[0]);
1303 proxy->vqs[vdev->queue_sel].enabled = 1;
1304 break;
1305 case VIRTIO_PCI_COMMON_Q_DESCLO:
1306 proxy->vqs[vdev->queue_sel].desc[0] = val;
1307 break;
1308 case VIRTIO_PCI_COMMON_Q_DESCHI:
1309 proxy->vqs[vdev->queue_sel].desc[1] = val;
1310 break;
1311 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1312 proxy->vqs[vdev->queue_sel].avail[0] = val;
1313 break;
1314 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1315 proxy->vqs[vdev->queue_sel].avail[1] = val;
1316 break;
1317 case VIRTIO_PCI_COMMON_Q_USEDLO:
1318 proxy->vqs[vdev->queue_sel].used[0] = val;
1319 break;
1320 case VIRTIO_PCI_COMMON_Q_USEDHI:
1321 proxy->vqs[vdev->queue_sel].used[1] = val;
1322 break;
1323 default:
1324 break;
1325 }
1326}
1327
1328
1329static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1330 unsigned size)
1331{
1332 return 0;
1333}
1334
1335static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1336 uint64_t val, unsigned size)
1337{
1338 VirtIODevice *vdev = opaque;
1339 VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
1340 unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1341
1342 if (queue < VIRTIO_QUEUE_MAX) {
1343 virtio_queue_notify(vdev, queue);
1344 }
1345}
1346
1347static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
1348 uint64_t val, unsigned size)
1349{
1350 VirtIODevice *vdev = opaque;
1351 unsigned queue = val;
1352
1353 if (queue < VIRTIO_QUEUE_MAX) {
1354 virtio_queue_notify(vdev, queue);
1355 }
1356}
1357
1358static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1359 unsigned size)
1360{
1361 VirtIOPCIProxy *proxy = opaque;
1362 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1363 uint64_t val = atomic_xchg(&vdev->isr, 0);
1364 pci_irq_deassert(&proxy->pci_dev);
1365
1366 return val;
1367}
1368
1369static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1370 uint64_t val, unsigned size)
1371{
1372}
1373
1374static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1375 unsigned size)
1376{
1377 VirtIODevice *vdev = opaque;
1378 uint64_t val = 0;
1379
1380 switch (size) {
1381 case 1:
1382 val = virtio_config_modern_readb(vdev, addr);
1383 break;
1384 case 2:
1385 val = virtio_config_modern_readw(vdev, addr);
1386 break;
1387 case 4:
1388 val = virtio_config_modern_readl(vdev, addr);
1389 break;
1390 }
1391 return val;
1392}
1393
1394static void virtio_pci_device_write(void *opaque, hwaddr addr,
1395 uint64_t val, unsigned size)
1396{
1397 VirtIODevice *vdev = opaque;
1398 switch (size) {
1399 case 1:
1400 virtio_config_modern_writeb(vdev, addr, val);
1401 break;
1402 case 2:
1403 virtio_config_modern_writew(vdev, addr, val);
1404 break;
1405 case 4:
1406 virtio_config_modern_writel(vdev, addr, val);
1407 break;
1408 }
1409}
1410
1411static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1412{
1413 static const MemoryRegionOps common_ops = {
1414 .read = virtio_pci_common_read,
1415 .write = virtio_pci_common_write,
1416 .impl = {
1417 .min_access_size = 1,
1418 .max_access_size = 4,
1419 },
1420 .endianness = DEVICE_LITTLE_ENDIAN,
1421 };
1422 static const MemoryRegionOps isr_ops = {
1423 .read = virtio_pci_isr_read,
1424 .write = virtio_pci_isr_write,
1425 .impl = {
1426 .min_access_size = 1,
1427 .max_access_size = 4,
1428 },
1429 .endianness = DEVICE_LITTLE_ENDIAN,
1430 };
1431 static const MemoryRegionOps device_ops = {
1432 .read = virtio_pci_device_read,
1433 .write = virtio_pci_device_write,
1434 .impl = {
1435 .min_access_size = 1,
1436 .max_access_size = 4,
1437 },
1438 .endianness = DEVICE_LITTLE_ENDIAN,
1439 };
1440 static const MemoryRegionOps notify_ops = {
1441 .read = virtio_pci_notify_read,
1442 .write = virtio_pci_notify_write,
1443 .impl = {
1444 .min_access_size = 1,
1445 .max_access_size = 4,
1446 },
1447 .endianness = DEVICE_LITTLE_ENDIAN,
1448 };
1449 static const MemoryRegionOps notify_pio_ops = {
1450 .read = virtio_pci_notify_read,
1451 .write = virtio_pci_notify_write_pio,
1452 .impl = {
1453 .min_access_size = 1,
1454 .max_access_size = 4,
1455 },
1456 .endianness = DEVICE_LITTLE_ENDIAN,
1457 };
1458
1459
1460 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1461 &common_ops,
1462 proxy,
1463 "virtio-pci-common",
1464 proxy->common.size);
1465
1466 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1467 &isr_ops,
1468 proxy,
1469 "virtio-pci-isr",
1470 proxy->isr.size);
1471
1472 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1473 &device_ops,
1474 virtio_bus_get_device(&proxy->bus),
1475 "virtio-pci-device",
1476 proxy->device.size);
1477
1478 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1479 ¬ify_ops,
1480 virtio_bus_get_device(&proxy->bus),
1481 "virtio-pci-notify",
1482 proxy->notify.size);
1483
1484 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
1485 ¬ify_pio_ops,
1486 virtio_bus_get_device(&proxy->bus),
1487 "virtio-pci-notify-pio",
1488 proxy->notify_pio.size);
1489}
1490
1491static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1492 VirtIOPCIRegion *region,
1493 struct virtio_pci_cap *cap,
1494 MemoryRegion *mr,
1495 uint8_t bar)
1496{
1497 memory_region_add_subregion(mr, region->offset, ®ion->mr);
1498
1499 cap->cfg_type = region->type;
1500 cap->bar = bar;
1501 cap->offset = cpu_to_le32(region->offset);
1502 cap->length = cpu_to_le32(region->size);
1503 virtio_pci_add_mem_cap(proxy, cap);
1504
1505}
1506
1507static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
1508 VirtIOPCIRegion *region,
1509 struct virtio_pci_cap *cap)
1510{
1511 virtio_pci_modern_region_map(proxy, region, cap,
1512 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1513}
1514
1515static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
1516 VirtIOPCIRegion *region,
1517 struct virtio_pci_cap *cap)
1518{
1519 virtio_pci_modern_region_map(proxy, region, cap,
1520 &proxy->io_bar, proxy->modern_io_bar_idx);
1521}
1522
1523static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
1524 VirtIOPCIRegion *region)
1525{
1526 memory_region_del_subregion(&proxy->modern_bar,
1527 ®ion->mr);
1528}
1529
1530static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
1531 VirtIOPCIRegion *region)
1532{
1533 memory_region_del_subregion(&proxy->io_bar,
1534 ®ion->mr);
1535}
1536
1537static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
1538{
1539 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1540 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1541
1542 if (virtio_pci_modern(proxy)) {
1543 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1544 }
1545
1546 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1547}
1548
1549
1550static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1551{
1552 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1553 VirtioBusState *bus = &proxy->bus;
1554 bool legacy = virtio_pci_legacy(proxy);
1555 bool modern;
1556 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1557 uint8_t *config;
1558 uint32_t size;
1559 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1560
1561
1562
1563
1564
1565 if (!proxy->ignore_backend_features &&
1566 !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1567 virtio_pci_disable_modern(proxy);
1568
1569 if (!legacy) {
1570 error_setg(errp, "Device doesn't support modern mode, and legacy"
1571 " mode is disabled");
1572 error_append_hint(errp, "Set disable-legacy to off\n");
1573
1574 return;
1575 }
1576 }
1577
1578 modern = virtio_pci_modern(proxy);
1579
1580 config = proxy->pci_dev.config;
1581 if (proxy->class_code) {
1582 pci_config_set_class(config, proxy->class_code);
1583 }
1584
1585 if (legacy) {
1586 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
1587 error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1588 " neither legacy nor transitional device");
1589 return ;
1590 }
1591
1592
1593
1594
1595
1596 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1597 } else {
1598
1599 pci_set_word(config + PCI_VENDOR_ID,
1600 PCI_VENDOR_ID_REDHAT_QUMRANET);
1601 pci_set_word(config + PCI_DEVICE_ID,
1602 0x1040 + virtio_bus_get_vdev_id(bus));
1603 pci_config_set_revision(config, 1);
1604 }
1605 config[PCI_INTERRUPT_PIN] = 1;
1606
1607
1608 if (modern) {
1609 struct virtio_pci_cap cap = {
1610 .cap_len = sizeof cap,
1611 };
1612 struct virtio_pci_notify_cap notify = {
1613 .cap.cap_len = sizeof notify,
1614 .notify_off_multiplier =
1615 cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
1616 };
1617 struct virtio_pci_cfg_cap cfg = {
1618 .cap.cap_len = sizeof cfg,
1619 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1620 };
1621 struct virtio_pci_notify_cap notify_pio = {
1622 .cap.cap_len = sizeof notify,
1623 .notify_off_multiplier = cpu_to_le32(0x0),
1624 };
1625
1626 struct virtio_pci_cfg_cap *cfg_mask;
1627
1628 virtio_pci_modern_regions_init(proxy);
1629
1630 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
1631 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
1632 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
1633 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);
1634
1635 if (modern_pio) {
1636 memory_region_init(&proxy->io_bar, OBJECT(proxy),
1637 "virtio-pci-io", 0x4);
1638
1639 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1640 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
1641
1642 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
1643 ¬ify_pio.cap);
1644 }
1645
1646 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
1647 PCI_BASE_ADDRESS_SPACE_MEMORY |
1648 PCI_BASE_ADDRESS_MEM_PREFETCH |
1649 PCI_BASE_ADDRESS_MEM_TYPE_64,
1650 &proxy->modern_bar);
1651
1652 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1653 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1654 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1655 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1656 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1657 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1658 }
1659
1660 if (proxy->nvectors) {
1661 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1662 proxy->msix_bar_idx, NULL);
1663 if (err) {
1664
1665 if (err != -ENOTSUP) {
1666 error_report("unable to init msix vectors to %" PRIu32,
1667 proxy->nvectors);
1668 }
1669 proxy->nvectors = 0;
1670 }
1671 }
1672
1673 proxy->pci_dev.config_write = virtio_write_config;
1674 proxy->pci_dev.config_read = virtio_read_config;
1675
1676 if (legacy) {
1677 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1678 + virtio_bus_get_vdev_config_len(bus);
1679 size = pow2ceil(size);
1680
1681 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1682 &virtio_pci_config_ops,
1683 proxy, "virtio-pci", size);
1684
1685 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
1686 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1687 }
1688}
1689
1690static void virtio_pci_device_unplugged(DeviceState *d)
1691{
1692 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1693 bool modern = virtio_pci_modern(proxy);
1694 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1695
1696 virtio_pci_stop_ioeventfd(proxy);
1697
1698 if (modern) {
1699 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
1700 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
1701 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
1702 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
1703 if (modern_pio) {
1704 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
1705 }
1706 }
1707}
1708
1709static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1710{
1711 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1712 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1713 bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) &&
1714 !pci_bus_is_root(pci_get_bus(pci_dev));
1715
1716 if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1717 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1718 }
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729 proxy->legacy_io_bar_idx = 0;
1730 proxy->msix_bar_idx = 1;
1731 proxy->modern_io_bar_idx = 2;
1732 proxy->modern_mem_bar_idx = 4;
1733
1734 proxy->common.offset = 0x0;
1735 proxy->common.size = 0x1000;
1736 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1737
1738 proxy->isr.offset = 0x1000;
1739 proxy->isr.size = 0x1000;
1740 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1741
1742 proxy->device.offset = 0x2000;
1743 proxy->device.size = 0x1000;
1744 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1745
1746 proxy->notify.offset = 0x3000;
1747 proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1748 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1749
1750 proxy->notify_pio.offset = 0x0;
1751 proxy->notify_pio.size = 0x4;
1752 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1753
1754
1755 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1756
1757 pow2ceil(proxy->notify.offset + proxy->notify.size));
1758
1759 if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
1760 proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
1761 }
1762
1763 if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1764 error_setg(errp, "device cannot work as neither modern nor legacy mode"
1765 " is enabled");
1766 error_append_hint(errp, "Set either disable-modern or disable-legacy"
1767 " to off\n");
1768 return;
1769 }
1770
1771 if (pcie_port && pci_is_express(pci_dev)) {
1772 int pos;
1773
1774 pos = pcie_endpoint_cap_init(pci_dev, 0);
1775 assert(pos > 0);
1776
1777 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0,
1778 PCI_PM_SIZEOF, errp);
1779 if (pos < 0) {
1780 return;
1781 }
1782
1783 pci_dev->exp.pm_cap = pos;
1784
1785
1786
1787
1788
1789 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
1790
1791 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) {
1792
1793 pcie_cap_deverr_init(pci_dev);
1794 }
1795
1796 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) {
1797
1798 pcie_cap_lnkctl_init(pci_dev);
1799 }
1800
1801 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) {
1802
1803 pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL,
1804 PCI_PM_CTRL_STATE_MASK);
1805 }
1806
1807 if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
1808 pcie_ats_init(pci_dev, 256);
1809 }
1810
1811 } else {
1812
1813
1814
1815
1816 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1817 }
1818
1819 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1820 if (k->realize) {
1821 k->realize(proxy, errp);
1822 }
1823}
1824
1825static void virtio_pci_exit(PCIDevice *pci_dev)
1826{
1827 msix_uninit_exclusive_bar(pci_dev);
1828}
1829
1830static void virtio_pci_reset(DeviceState *qdev)
1831{
1832 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1833 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1834 PCIDevice *dev = PCI_DEVICE(qdev);
1835 int i;
1836
1837 virtio_pci_stop_ioeventfd(proxy);
1838 virtio_bus_reset(bus);
1839 msix_unuse_all_vectors(&proxy->pci_dev);
1840
1841 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1842 proxy->vqs[i].enabled = 0;
1843 proxy->vqs[i].num = 0;
1844 proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0;
1845 proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0;
1846 proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0;
1847 }
1848
1849 if (pci_is_express(dev)) {
1850 pcie_cap_deverr_reset(dev);
1851 pcie_cap_lnkctl_reset(dev);
1852
1853 pci_set_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL, 0);
1854 }
1855}
1856
1857static Property virtio_pci_properties[] = {
1858 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1859 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1860 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
1861 ON_OFF_AUTO_AUTO),
1862 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
1863 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1864 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1865 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
1866 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1867 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
1868 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1869 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
1870 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1871 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
1872 ignore_backend_features, false),
1873 DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags,
1874 VIRTIO_PCI_FLAG_ATS_BIT, false),
1875 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags,
1876 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true),
1877 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags,
1878 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true),
1879 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags,
1880 VIRTIO_PCI_FLAG_INIT_PM_BIT, true),
1881 DEFINE_PROP_END_OF_LIST(),
1882};
1883
1884static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
1885{
1886 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
1887 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1888 PCIDevice *pci_dev = &proxy->pci_dev;
1889
1890 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
1891 virtio_pci_modern(proxy)) {
1892 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1893 }
1894
1895 vpciklass->parent_dc_realize(qdev, errp);
1896}
1897
1898static void virtio_pci_class_init(ObjectClass *klass, void *data)
1899{
1900 DeviceClass *dc = DEVICE_CLASS(klass);
1901 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1902 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1903
1904 dc->props = virtio_pci_properties;
1905 k->realize = virtio_pci_realize;
1906 k->exit = virtio_pci_exit;
1907 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1908 k->revision = VIRTIO_PCI_ABI_VERSION;
1909 k->class_id = PCI_CLASS_OTHERS;
1910 device_class_set_parent_realize(dc, virtio_pci_dc_realize,
1911 &vpciklass->parent_dc_realize);
1912 dc->reset = virtio_pci_reset;
1913}
1914
1915static const TypeInfo virtio_pci_info = {
1916 .name = TYPE_VIRTIO_PCI,
1917 .parent = TYPE_PCI_DEVICE,
1918 .instance_size = sizeof(VirtIOPCIProxy),
1919 .class_init = virtio_pci_class_init,
1920 .class_size = sizeof(VirtioPCIClass),
1921 .abstract = true,
1922 .interfaces = (InterfaceInfo[]) {
1923 { INTERFACE_PCIE_DEVICE },
1924 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1925 { }
1926 },
1927};
1928
1929
1930
1931static Property virtio_blk_pci_properties[] = {
1932 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1933 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1934 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1935 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1936 DEV_NVECTORS_UNSPECIFIED),
1937 DEFINE_PROP_END_OF_LIST(),
1938};
1939
1940static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1941{
1942 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1943 DeviceState *vdev = DEVICE(&dev->vdev);
1944
1945 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1946 vpci_dev->nvectors = dev->vdev.conf.num_queues + 1;
1947 }
1948
1949 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1950 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1951}
1952
1953static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1954{
1955 DeviceClass *dc = DEVICE_CLASS(klass);
1956 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1957 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1958
1959 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1960 dc->props = virtio_blk_pci_properties;
1961 k->realize = virtio_blk_pci_realize;
1962 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1963 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1964 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1965 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1966}
1967
1968static void virtio_blk_pci_instance_init(Object *obj)
1969{
1970 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1971
1972 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1973 TYPE_VIRTIO_BLK);
1974 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1975 "bootindex", &error_abort);
1976}
1977
1978static const TypeInfo virtio_blk_pci_info = {
1979 .name = TYPE_VIRTIO_BLK_PCI,
1980 .parent = TYPE_VIRTIO_PCI,
1981 .instance_size = sizeof(VirtIOBlkPCI),
1982 .instance_init = virtio_blk_pci_instance_init,
1983 .class_init = virtio_blk_pci_class_init,
1984};
1985
1986#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
1987
1988
1989static Property vhost_user_blk_pci_properties[] = {
1990 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1991 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1992 DEV_NVECTORS_UNSPECIFIED),
1993 DEFINE_PROP_END_OF_LIST(),
1994};
1995
1996static void vhost_user_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1997{
1998 VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(vpci_dev);
1999 DeviceState *vdev = DEVICE(&dev->vdev);
2000
2001 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2002 vpci_dev->nvectors = dev->vdev.num_queues + 1;
2003 }
2004
2005 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2006 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2007}
2008
2009static void vhost_user_blk_pci_class_init(ObjectClass *klass, void *data)
2010{
2011 DeviceClass *dc = DEVICE_CLASS(klass);
2012 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2013 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2014
2015 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2016 dc->props = vhost_user_blk_pci_properties;
2017 k->realize = vhost_user_blk_pci_realize;
2018 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2019 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
2020 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2021 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2022}
2023
2024static void vhost_user_blk_pci_instance_init(Object *obj)
2025{
2026 VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(obj);
2027
2028 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2029 TYPE_VHOST_USER_BLK);
2030 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2031 "bootindex", &error_abort);
2032}
2033
2034static const TypeInfo vhost_user_blk_pci_info = {
2035 .name = TYPE_VHOST_USER_BLK_PCI,
2036 .parent = TYPE_VIRTIO_PCI,
2037 .instance_size = sizeof(VHostUserBlkPCI),
2038 .instance_init = vhost_user_blk_pci_instance_init,
2039 .class_init = vhost_user_blk_pci_class_init,
2040};
2041#endif
2042
2043
2044
2045static Property virtio_scsi_pci_properties[] = {
2046 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2047 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2048 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2049 DEV_NVECTORS_UNSPECIFIED),
2050 DEFINE_PROP_END_OF_LIST(),
2051};
2052
2053static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2054{
2055 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
2056 DeviceState *vdev = DEVICE(&dev->vdev);
2057 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2058 DeviceState *proxy = DEVICE(vpci_dev);
2059 char *bus_name;
2060
2061 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2062 vpci_dev->nvectors = vs->conf.num_queues + 3;
2063 }
2064
2065
2066
2067
2068
2069 if (proxy->id) {
2070 bus_name = g_strdup_printf("%s.0", proxy->id);
2071 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2072 g_free(bus_name);
2073 }
2074
2075 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2076 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2077}
2078
2079static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
2080{
2081 DeviceClass *dc = DEVICE_CLASS(klass);
2082 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2083 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2084
2085 k->realize = virtio_scsi_pci_realize;
2086 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2087 dc->props = virtio_scsi_pci_properties;
2088 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2089 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2090 pcidev_k->revision = 0x00;
2091 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2092}
2093
2094static void virtio_scsi_pci_instance_init(Object *obj)
2095{
2096 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
2097
2098 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2099 TYPE_VIRTIO_SCSI);
2100}
2101
2102static const TypeInfo virtio_scsi_pci_info = {
2103 .name = TYPE_VIRTIO_SCSI_PCI,
2104 .parent = TYPE_VIRTIO_PCI,
2105 .instance_size = sizeof(VirtIOSCSIPCI),
2106 .instance_init = virtio_scsi_pci_instance_init,
2107 .class_init = virtio_scsi_pci_class_init,
2108};
2109
2110
2111
2112#ifdef CONFIG_VHOST_SCSI
2113static Property vhost_scsi_pci_properties[] = {
2114 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2115 DEV_NVECTORS_UNSPECIFIED),
2116 DEFINE_PROP_END_OF_LIST(),
2117};
2118
2119static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2120{
2121 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
2122 DeviceState *vdev = DEVICE(&dev->vdev);
2123 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2124
2125 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2126 vpci_dev->nvectors = vs->conf.num_queues + 3;
2127 }
2128
2129 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2130 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2131}
2132
2133static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
2134{
2135 DeviceClass *dc = DEVICE_CLASS(klass);
2136 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2137 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2138 k->realize = vhost_scsi_pci_realize;
2139 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2140 dc->props = vhost_scsi_pci_properties;
2141 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2142 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2143 pcidev_k->revision = 0x00;
2144 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2145}
2146
2147static void vhost_scsi_pci_instance_init(Object *obj)
2148{
2149 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2150
2151 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2152 TYPE_VHOST_SCSI);
2153 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2154 "bootindex", &error_abort);
2155}
2156
2157static const TypeInfo vhost_scsi_pci_info = {
2158 .name = TYPE_VHOST_SCSI_PCI,
2159 .parent = TYPE_VIRTIO_PCI,
2160 .instance_size = sizeof(VHostSCSIPCI),
2161 .instance_init = vhost_scsi_pci_instance_init,
2162 .class_init = vhost_scsi_pci_class_init,
2163};
2164#endif
2165
2166#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2167
2168static Property vhost_user_scsi_pci_properties[] = {
2169 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2170 DEV_NVECTORS_UNSPECIFIED),
2171 DEFINE_PROP_END_OF_LIST(),
2172};
2173
2174static void vhost_user_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2175{
2176 VHostUserSCSIPCI *dev = VHOST_USER_SCSI_PCI(vpci_dev);
2177 DeviceState *vdev = DEVICE(&dev->vdev);
2178 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2179
2180 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2181 vpci_dev->nvectors = vs->conf.num_queues + 3;
2182 }
2183
2184 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2185 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2186}
2187
2188static void vhost_user_scsi_pci_class_init(ObjectClass *klass, void *data)
2189{
2190 DeviceClass *dc = DEVICE_CLASS(klass);
2191 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2192 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2193 k->realize = vhost_user_scsi_pci_realize;
2194 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2195 dc->props = vhost_user_scsi_pci_properties;
2196 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2197 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2198 pcidev_k->revision = 0x00;
2199 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2200}
2201
2202static void vhost_user_scsi_pci_instance_init(Object *obj)
2203{
2204 VHostUserSCSIPCI *dev = VHOST_USER_SCSI_PCI(obj);
2205
2206 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2207 TYPE_VHOST_USER_SCSI);
2208 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2209 "bootindex", &error_abort);
2210}
2211
2212static const TypeInfo vhost_user_scsi_pci_info = {
2213 .name = TYPE_VHOST_USER_SCSI_PCI,
2214 .parent = TYPE_VIRTIO_PCI,
2215 .instance_size = sizeof(VHostUserSCSIPCI),
2216 .instance_init = vhost_user_scsi_pci_instance_init,
2217 .class_init = vhost_user_scsi_pci_class_init,
2218};
2219#endif
2220
2221
2222
2223#ifdef CONFIG_VHOST_VSOCK
2224static Property vhost_vsock_pci_properties[] = {
2225 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2226 DEFINE_PROP_END_OF_LIST(),
2227};
2228
2229static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2230{
2231 VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev);
2232 DeviceState *vdev = DEVICE(&dev->vdev);
2233
2234 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2235 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2236}
2237
2238static void vhost_vsock_pci_class_init(ObjectClass *klass, void *data)
2239{
2240 DeviceClass *dc = DEVICE_CLASS(klass);
2241 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2242 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2243 k->realize = vhost_vsock_pci_realize;
2244 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2245 dc->props = vhost_vsock_pci_properties;
2246 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2247 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
2248 pcidev_k->revision = 0x00;
2249 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2250}
2251
2252static void vhost_vsock_pci_instance_init(Object *obj)
2253{
2254 VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj);
2255
2256 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2257 TYPE_VHOST_VSOCK);
2258}
2259
2260static const TypeInfo vhost_vsock_pci_info = {
2261 .name = TYPE_VHOST_VSOCK_PCI,
2262 .parent = TYPE_VIRTIO_PCI,
2263 .instance_size = sizeof(VHostVSockPCI),
2264 .instance_init = vhost_vsock_pci_instance_init,
2265 .class_init = vhost_vsock_pci_class_init,
2266};
2267#endif
2268
2269
2270
2271static Property virtio_balloon_pci_properties[] = {
2272 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2273 DEFINE_PROP_END_OF_LIST(),
2274};
2275
2276static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2277{
2278 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
2279 DeviceState *vdev = DEVICE(&dev->vdev);
2280
2281 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
2282 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) {
2283 vpci_dev->class_code = PCI_CLASS_OTHERS;
2284 }
2285
2286 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2287 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2288}
2289
2290static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
2291{
2292 DeviceClass *dc = DEVICE_CLASS(klass);
2293 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2294 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2295 k->realize = virtio_balloon_pci_realize;
2296 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2297 dc->props = virtio_balloon_pci_properties;
2298 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2299 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
2300 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2301 pcidev_k->class_id = PCI_CLASS_OTHERS;
2302}
2303
2304static void virtio_balloon_pci_instance_init(Object *obj)
2305{
2306 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2307
2308 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2309 TYPE_VIRTIO_BALLOON);
2310 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
2311 "guest-stats", &error_abort);
2312 object_property_add_alias(obj, "guest-stats-polling-interval",
2313 OBJECT(&dev->vdev),
2314 "guest-stats-polling-interval", &error_abort);
2315}
2316
2317static const TypeInfo virtio_balloon_pci_info = {
2318 .name = TYPE_VIRTIO_BALLOON_PCI,
2319 .parent = TYPE_VIRTIO_PCI,
2320 .instance_size = sizeof(VirtIOBalloonPCI),
2321 .instance_init = virtio_balloon_pci_instance_init,
2322 .class_init = virtio_balloon_pci_class_init,
2323};
2324
2325
2326
2327static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2328{
2329 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2330 DeviceState *vdev = DEVICE(&dev->vdev);
2331 DeviceState *proxy = DEVICE(vpci_dev);
2332 char *bus_name;
2333
2334 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2335 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER &&
2336 vpci_dev->class_code != PCI_CLASS_OTHERS) {
2337 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2338 }
2339
2340
2341
2342 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2343 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2344 }
2345
2346
2347
2348
2349
2350 if (proxy->id) {
2351 bus_name = g_strdup_printf("%s.0", proxy->id);
2352 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2353 g_free(bus_name);
2354 }
2355
2356 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2357 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2358}
2359
2360static Property virtio_serial_pci_properties[] = {
2361 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2362 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2363 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2364 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2365 DEFINE_PROP_END_OF_LIST(),
2366};
2367
2368static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2369{
2370 DeviceClass *dc = DEVICE_CLASS(klass);
2371 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2372 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2373 k->realize = virtio_serial_pci_realize;
2374 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2375 dc->props = virtio_serial_pci_properties;
2376 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2377 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2378 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2379 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2380}
2381
2382static void virtio_serial_pci_instance_init(Object *obj)
2383{
2384 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2385
2386 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2387 TYPE_VIRTIO_SERIAL);
2388}
2389
2390static const TypeInfo virtio_serial_pci_info = {
2391 .name = TYPE_VIRTIO_SERIAL_PCI,
2392 .parent = TYPE_VIRTIO_PCI,
2393 .instance_size = sizeof(VirtIOSerialPCI),
2394 .instance_init = virtio_serial_pci_instance_init,
2395 .class_init = virtio_serial_pci_class_init,
2396};
2397
2398
2399
2400static Property virtio_net_properties[] = {
2401 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2402 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2403 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2404 DEFINE_PROP_END_OF_LIST(),
2405};
2406
2407static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2408{
2409 DeviceState *qdev = DEVICE(vpci_dev);
2410 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2411 DeviceState *vdev = DEVICE(&dev->vdev);
2412
2413 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2414 object_get_typename(OBJECT(qdev)));
2415 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2416 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2417}
2418
2419static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2420{
2421 DeviceClass *dc = DEVICE_CLASS(klass);
2422 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2423 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2424
2425 k->romfile = "efi-virtio.rom";
2426 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2427 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2428 k->revision = VIRTIO_PCI_ABI_VERSION;
2429 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2430 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2431 dc->props = virtio_net_properties;
2432 vpciklass->realize = virtio_net_pci_realize;
2433}
2434
2435static void virtio_net_pci_instance_init(Object *obj)
2436{
2437 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2438
2439 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2440 TYPE_VIRTIO_NET);
2441 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2442 "bootindex", &error_abort);
2443}
2444
2445static const TypeInfo virtio_net_pci_info = {
2446 .name = TYPE_VIRTIO_NET_PCI,
2447 .parent = TYPE_VIRTIO_PCI,
2448 .instance_size = sizeof(VirtIONetPCI),
2449 .instance_init = virtio_net_pci_instance_init,
2450 .class_init = virtio_net_pci_class_init,
2451};
2452
2453
2454
2455static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2456{
2457 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2458 DeviceState *vdev = DEVICE(&vrng->vdev);
2459 Error *err = NULL;
2460
2461 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2462 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2463 if (err) {
2464 error_propagate(errp, err);
2465 return;
2466 }
2467
2468 object_property_set_link(OBJECT(vrng),
2469 OBJECT(vrng->vdev.conf.rng), "rng",
2470 NULL);
2471}
2472
2473static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2474{
2475 DeviceClass *dc = DEVICE_CLASS(klass);
2476 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2477 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2478
2479 k->realize = virtio_rng_pci_realize;
2480 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2481
2482 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2483 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2484 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2485 pcidev_k->class_id = PCI_CLASS_OTHERS;
2486}
2487
2488static void virtio_rng_initfn(Object *obj)
2489{
2490 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2491
2492 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2493 TYPE_VIRTIO_RNG);
2494}
2495
2496static const TypeInfo virtio_rng_pci_info = {
2497 .name = TYPE_VIRTIO_RNG_PCI,
2498 .parent = TYPE_VIRTIO_PCI,
2499 .instance_size = sizeof(VirtIORngPCI),
2500 .instance_init = virtio_rng_initfn,
2501 .class_init = virtio_rng_pci_class_init,
2502};
2503
2504
2505
2506static Property virtio_input_pci_properties[] = {
2507 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2508 DEFINE_PROP_END_OF_LIST(),
2509};
2510
2511static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2512{
2513 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2514 DeviceState *vdev = DEVICE(&vinput->vdev);
2515
2516 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2517 virtio_pci_force_virtio_1(vpci_dev);
2518 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2519}
2520
2521static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2522{
2523 DeviceClass *dc = DEVICE_CLASS(klass);
2524 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2525 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2526
2527 dc->props = virtio_input_pci_properties;
2528 k->realize = virtio_input_pci_realize;
2529 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2530
2531 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2532}
2533
2534static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2535{
2536 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2537
2538 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2539}
2540
2541static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2542 void *data)
2543{
2544 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2545
2546 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2547}
2548
2549static void virtio_keyboard_initfn(Object *obj)
2550{
2551 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2552
2553 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2554 TYPE_VIRTIO_KEYBOARD);
2555}
2556
2557static void virtio_mouse_initfn(Object *obj)
2558{
2559 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2560
2561 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2562 TYPE_VIRTIO_MOUSE);
2563}
2564
2565static void virtio_tablet_initfn(Object *obj)
2566{
2567 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2568
2569 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2570 TYPE_VIRTIO_TABLET);
2571}
2572
2573static const TypeInfo virtio_input_pci_info = {
2574 .name = TYPE_VIRTIO_INPUT_PCI,
2575 .parent = TYPE_VIRTIO_PCI,
2576 .instance_size = sizeof(VirtIOInputPCI),
2577 .class_init = virtio_input_pci_class_init,
2578 .abstract = true,
2579};
2580
2581static const TypeInfo virtio_input_hid_pci_info = {
2582 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2583 .parent = TYPE_VIRTIO_INPUT_PCI,
2584 .instance_size = sizeof(VirtIOInputHIDPCI),
2585 .abstract = true,
2586};
2587
2588static const TypeInfo virtio_keyboard_pci_info = {
2589 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2590 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2591 .class_init = virtio_input_hid_kbd_pci_class_init,
2592 .instance_size = sizeof(VirtIOInputHIDPCI),
2593 .instance_init = virtio_keyboard_initfn,
2594};
2595
2596static const TypeInfo virtio_mouse_pci_info = {
2597 .name = TYPE_VIRTIO_MOUSE_PCI,
2598 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2599 .class_init = virtio_input_hid_mouse_pci_class_init,
2600 .instance_size = sizeof(VirtIOInputHIDPCI),
2601 .instance_init = virtio_mouse_initfn,
2602};
2603
2604static const TypeInfo virtio_tablet_pci_info = {
2605 .name = TYPE_VIRTIO_TABLET_PCI,
2606 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2607 .instance_size = sizeof(VirtIOInputHIDPCI),
2608 .instance_init = virtio_tablet_initfn,
2609};
2610
2611#ifdef CONFIG_LINUX
2612static void virtio_host_initfn(Object *obj)
2613{
2614 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2615
2616 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2617 TYPE_VIRTIO_INPUT_HOST);
2618}
2619
2620static const TypeInfo virtio_host_pci_info = {
2621 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2622 .parent = TYPE_VIRTIO_INPUT_PCI,
2623 .instance_size = sizeof(VirtIOInputHostPCI),
2624 .instance_init = virtio_host_initfn,
2625};
2626#endif
2627
2628
2629
2630static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2631 VirtIOPCIProxy *dev)
2632{
2633 DeviceState *qdev = DEVICE(dev);
2634 char virtio_bus_name[] = "virtio-bus";
2635
2636 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2637 virtio_bus_name);
2638}
2639
2640static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2641{
2642 BusClass *bus_class = BUS_CLASS(klass);
2643 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2644 bus_class->max_dev = 1;
2645 k->notify = virtio_pci_notify;
2646 k->save_config = virtio_pci_save_config;
2647 k->load_config = virtio_pci_load_config;
2648 k->save_queue = virtio_pci_save_queue;
2649 k->load_queue = virtio_pci_load_queue;
2650 k->save_extra_state = virtio_pci_save_extra_state;
2651 k->load_extra_state = virtio_pci_load_extra_state;
2652 k->has_extra_state = virtio_pci_has_extra_state;
2653 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2654 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2655 k->vmstate_change = virtio_pci_vmstate_change;
2656 k->pre_plugged = virtio_pci_pre_plugged;
2657 k->device_plugged = virtio_pci_device_plugged;
2658 k->device_unplugged = virtio_pci_device_unplugged;
2659 k->query_nvectors = virtio_pci_query_nvectors;
2660 k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
2661 k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2662 k->get_dma_as = virtio_pci_get_dma_as;
2663}
2664
2665static const TypeInfo virtio_pci_bus_info = {
2666 .name = TYPE_VIRTIO_PCI_BUS,
2667 .parent = TYPE_VIRTIO_BUS,
2668 .instance_size = sizeof(VirtioPCIBusState),
2669 .class_init = virtio_pci_bus_class_init,
2670};
2671
2672static void virtio_pci_register_types(void)
2673{
2674 type_register_static(&virtio_rng_pci_info);
2675 type_register_static(&virtio_input_pci_info);
2676 type_register_static(&virtio_input_hid_pci_info);
2677 type_register_static(&virtio_keyboard_pci_info);
2678 type_register_static(&virtio_mouse_pci_info);
2679 type_register_static(&virtio_tablet_pci_info);
2680#ifdef CONFIG_LINUX
2681 type_register_static(&virtio_host_pci_info);
2682#endif
2683 type_register_static(&virtio_pci_bus_info);
2684 type_register_static(&virtio_pci_info);
2685#ifdef CONFIG_VIRTFS
2686 type_register_static(&virtio_9p_pci_info);
2687#endif
2688 type_register_static(&virtio_blk_pci_info);
2689#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2690 type_register_static(&vhost_user_blk_pci_info);
2691#endif
2692 type_register_static(&virtio_scsi_pci_info);
2693 type_register_static(&virtio_balloon_pci_info);
2694 type_register_static(&virtio_serial_pci_info);
2695 type_register_static(&virtio_net_pci_info);
2696#ifdef CONFIG_VHOST_SCSI
2697 type_register_static(&vhost_scsi_pci_info);
2698#endif
2699#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2700 type_register_static(&vhost_user_scsi_pci_info);
2701#endif
2702#ifdef CONFIG_VHOST_VSOCK
2703 type_register_static(&vhost_vsock_pci_info);
2704#endif
2705}
2706
2707type_init(virtio_pci_register_types)
2708