]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio/virtio-pci.c
ae7fef96d581a170d29f008ad229c3562f3a1229
[mirror_qemu.git] / hw / virtio / virtio-pci.c
1 /*
2 * Virtio PCI Bindings
3 *
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
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.
16 */
17
18 #include <inttypes.h>
19
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "hw/virtio/virtio-serial.h"
24 #include "hw/virtio/virtio-scsi.h"
25 #include "hw/virtio/virtio-balloon.h"
26 #include "hw/pci/pci.h"
27 #include "qemu/error-report.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/msix.h"
30 #include "hw/loader.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/blockdev.h"
33 #include "virtio-pci.h"
34 #include "qemu/range.h"
35 #include "hw/virtio/virtio-bus.h"
36 #include "qapi/visitor.h"
37
38 /* from Linux's linux/virtio_pci.h */
39
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES 0
42
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES 4
45
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN 8
48
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM 12
51
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL 14
54
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY 16
57
58 /* An 8-bit device status register. */
59 #define VIRTIO_PCI_STATUS 18
60
61 /* An 8-bit r/o interrupt status register. Reading the value will return the
62 * current contents of the ISR and will also clear it. This is effectively
63 * a read-and-acknowledge. */
64 #define VIRTIO_PCI_ISR 19
65
66 /* MSI-X registers: only enabled if MSI-X is enabled. */
67 /* A 16-bit vector for configuration changes. */
68 #define VIRTIO_MSI_CONFIG_VECTOR 20
69 /* A 16-bit vector for selected queue notifications. */
70 #define VIRTIO_MSI_QUEUE_VECTOR 22
71
72 /* Config space size */
73 #define VIRTIO_PCI_CONFIG_NOMSI 20
74 #define VIRTIO_PCI_CONFIG_MSI 24
75 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
78
79 /* The remaining space is defined by each driver as the per-driver
80 * configuration space */
81 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
82 VIRTIO_PCI_CONFIG_MSI : \
83 VIRTIO_PCI_CONFIG_NOMSI)
84
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86 * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
88
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
91
92 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
93 VirtIOPCIProxy *dev);
94
95 /* virtio device */
96 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
97 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
98 {
99 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
100 }
101
102 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
103 * be careful and test performance if you change this.
104 */
105 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
106 {
107 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
108 }
109
110 static void virtio_pci_notify(DeviceState *d, uint16_t vector)
111 {
112 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
113
114 if (msix_enabled(&proxy->pci_dev))
115 msix_notify(&proxy->pci_dev, vector);
116 else {
117 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
118 pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
119 }
120 }
121
122 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
123 {
124 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
125 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
126
127 pci_device_save(&proxy->pci_dev, f);
128 msix_save(&proxy->pci_dev, f);
129 if (msix_present(&proxy->pci_dev))
130 qemu_put_be16(f, vdev->config_vector);
131 }
132
133 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
134 {
135 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
136 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
137
138 if (msix_present(&proxy->pci_dev))
139 qemu_put_be16(f, virtio_queue_vector(vdev, n));
140 }
141
142 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
143 {
144 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
145 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
146
147 int ret;
148 ret = pci_device_load(&proxy->pci_dev, f);
149 if (ret) {
150 return ret;
151 }
152 msix_unuse_all_vectors(&proxy->pci_dev);
153 msix_load(&proxy->pci_dev, f);
154 if (msix_present(&proxy->pci_dev)) {
155 qemu_get_be16s(f, &vdev->config_vector);
156 } else {
157 vdev->config_vector = VIRTIO_NO_VECTOR;
158 }
159 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
160 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
161 }
162 return 0;
163 }
164
165 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
166 {
167 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
168 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
169
170 uint16_t vector;
171 if (msix_present(&proxy->pci_dev)) {
172 qemu_get_be16s(f, &vector);
173 } else {
174 vector = VIRTIO_NO_VECTOR;
175 }
176 virtio_queue_set_vector(vdev, n, vector);
177 if (vector != VIRTIO_NO_VECTOR) {
178 return msix_vector_use(&proxy->pci_dev, vector);
179 }
180 return 0;
181 }
182
183 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
184 int n, bool assign, bool set_handler)
185 {
186 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
187 VirtQueue *vq = virtio_get_queue(vdev, n);
188 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
189 int r = 0;
190
191 if (assign) {
192 r = event_notifier_init(notifier, 1);
193 if (r < 0) {
194 error_report("%s: unable to init event notifier: %d",
195 __func__, r);
196 return r;
197 }
198 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
199 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
200 true, n, notifier);
201 } else {
202 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
203 true, n, notifier);
204 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
205 event_notifier_cleanup(notifier);
206 }
207 return r;
208 }
209
210 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
211 {
212 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
213 int n, r;
214
215 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
216 proxy->ioeventfd_disabled ||
217 proxy->ioeventfd_started) {
218 return;
219 }
220
221 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
222 if (!virtio_queue_get_num(vdev, n)) {
223 continue;
224 }
225
226 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
227 if (r < 0) {
228 goto assign_error;
229 }
230 }
231 proxy->ioeventfd_started = true;
232 return;
233
234 assign_error:
235 while (--n >= 0) {
236 if (!virtio_queue_get_num(vdev, n)) {
237 continue;
238 }
239
240 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
241 assert(r >= 0);
242 }
243 proxy->ioeventfd_started = false;
244 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
245 }
246
247 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
248 {
249 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
250 int r;
251 int n;
252
253 if (!proxy->ioeventfd_started) {
254 return;
255 }
256
257 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
258 if (!virtio_queue_get_num(vdev, n)) {
259 continue;
260 }
261
262 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
263 assert(r >= 0);
264 }
265 proxy->ioeventfd_started = false;
266 }
267
268 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
269 {
270 VirtIOPCIProxy *proxy = opaque;
271 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
272 hwaddr pa;
273
274 switch (addr) {
275 case VIRTIO_PCI_GUEST_FEATURES:
276 /* Guest does not negotiate properly? We have to assume nothing. */
277 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
278 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
279 }
280 virtio_set_features(vdev, val);
281 break;
282 case VIRTIO_PCI_QUEUE_PFN:
283 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
284 if (pa == 0) {
285 virtio_pci_stop_ioeventfd(proxy);
286 virtio_reset(vdev);
287 msix_unuse_all_vectors(&proxy->pci_dev);
288 }
289 else
290 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
291 break;
292 case VIRTIO_PCI_QUEUE_SEL:
293 if (val < VIRTIO_PCI_QUEUE_MAX)
294 vdev->queue_sel = val;
295 break;
296 case VIRTIO_PCI_QUEUE_NOTIFY:
297 if (val < VIRTIO_PCI_QUEUE_MAX) {
298 virtio_queue_notify(vdev, val);
299 }
300 break;
301 case VIRTIO_PCI_STATUS:
302 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
303 virtio_pci_stop_ioeventfd(proxy);
304 }
305
306 virtio_set_status(vdev, val & 0xFF);
307
308 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
309 virtio_pci_start_ioeventfd(proxy);
310 }
311
312 if (vdev->status == 0) {
313 virtio_reset(vdev);
314 msix_unuse_all_vectors(&proxy->pci_dev);
315 }
316
317 /* Linux before 2.6.34 drives the device without enabling
318 the PCI device bus master bit. Enable it automatically
319 for the guest. This is a PCI spec violation but so is
320 initiating DMA with bus master bit clear. */
321 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
322 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
323 proxy->pci_dev.config[PCI_COMMAND] |
324 PCI_COMMAND_MASTER, 1);
325 }
326
327 /* Linux before 2.6.34 sets the device as OK without enabling
328 the PCI device bus master bit. In this case we need to disable
329 some safety checks. */
330 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
331 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
332 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
333 }
334 break;
335 case VIRTIO_MSI_CONFIG_VECTOR:
336 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
337 /* Make it possible for guest to discover an error took place. */
338 if (msix_vector_use(&proxy->pci_dev, val) < 0)
339 val = VIRTIO_NO_VECTOR;
340 vdev->config_vector = val;
341 break;
342 case VIRTIO_MSI_QUEUE_VECTOR:
343 msix_vector_unuse(&proxy->pci_dev,
344 virtio_queue_vector(vdev, vdev->queue_sel));
345 /* Make it possible for guest to discover an error took place. */
346 if (msix_vector_use(&proxy->pci_dev, val) < 0)
347 val = VIRTIO_NO_VECTOR;
348 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
349 break;
350 default:
351 error_report("%s: unexpected address 0x%x value 0x%x",
352 __func__, addr, val);
353 break;
354 }
355 }
356
357 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
358 {
359 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
360 uint32_t ret = 0xFFFFFFFF;
361
362 switch (addr) {
363 case VIRTIO_PCI_HOST_FEATURES:
364 ret = proxy->host_features;
365 break;
366 case VIRTIO_PCI_GUEST_FEATURES:
367 ret = vdev->guest_features;
368 break;
369 case VIRTIO_PCI_QUEUE_PFN:
370 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
371 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
372 break;
373 case VIRTIO_PCI_QUEUE_NUM:
374 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
375 break;
376 case VIRTIO_PCI_QUEUE_SEL:
377 ret = vdev->queue_sel;
378 break;
379 case VIRTIO_PCI_STATUS:
380 ret = vdev->status;
381 break;
382 case VIRTIO_PCI_ISR:
383 /* reading from the ISR also clears it. */
384 ret = vdev->isr;
385 vdev->isr = 0;
386 pci_irq_deassert(&proxy->pci_dev);
387 break;
388 case VIRTIO_MSI_CONFIG_VECTOR:
389 ret = vdev->config_vector;
390 break;
391 case VIRTIO_MSI_QUEUE_VECTOR:
392 ret = virtio_queue_vector(vdev, vdev->queue_sel);
393 break;
394 default:
395 break;
396 }
397
398 return ret;
399 }
400
401 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
402 unsigned size)
403 {
404 VirtIOPCIProxy *proxy = opaque;
405 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
406 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
407 uint64_t val = 0;
408 if (addr < config) {
409 return virtio_ioport_read(proxy, addr);
410 }
411 addr -= config;
412
413 switch (size) {
414 case 1:
415 val = virtio_config_readb(vdev, addr);
416 break;
417 case 2:
418 val = virtio_config_readw(vdev, addr);
419 if (virtio_is_big_endian(vdev)) {
420 val = bswap16(val);
421 }
422 break;
423 case 4:
424 val = virtio_config_readl(vdev, addr);
425 if (virtio_is_big_endian(vdev)) {
426 val = bswap32(val);
427 }
428 break;
429 }
430 return val;
431 }
432
433 static void virtio_pci_config_write(void *opaque, hwaddr addr,
434 uint64_t val, unsigned size)
435 {
436 VirtIOPCIProxy *proxy = opaque;
437 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
438 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
439 if (addr < config) {
440 virtio_ioport_write(proxy, addr, val);
441 return;
442 }
443 addr -= config;
444 /*
445 * Virtio-PCI is odd. Ioports are LE but config space is target native
446 * endian.
447 */
448 switch (size) {
449 case 1:
450 virtio_config_writeb(vdev, addr, val);
451 break;
452 case 2:
453 if (virtio_is_big_endian(vdev)) {
454 val = bswap16(val);
455 }
456 virtio_config_writew(vdev, addr, val);
457 break;
458 case 4:
459 if (virtio_is_big_endian(vdev)) {
460 val = bswap32(val);
461 }
462 virtio_config_writel(vdev, addr, val);
463 break;
464 }
465 }
466
467 static const MemoryRegionOps virtio_pci_config_ops = {
468 .read = virtio_pci_config_read,
469 .write = virtio_pci_config_write,
470 .impl = {
471 .min_access_size = 1,
472 .max_access_size = 4,
473 },
474 .endianness = DEVICE_LITTLE_ENDIAN,
475 };
476
477 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
478 uint32_t val, int len)
479 {
480 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
481 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
482
483 pci_default_write_config(pci_dev, address, val, len);
484
485 if (range_covers_byte(address, len, PCI_COMMAND) &&
486 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
487 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
488 virtio_pci_stop_ioeventfd(proxy);
489 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
490 }
491 }
492
493 static unsigned virtio_pci_get_features(DeviceState *d)
494 {
495 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
496 return proxy->host_features;
497 }
498
499 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
500 unsigned int queue_no,
501 unsigned int vector,
502 MSIMessage msg)
503 {
504 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
505 int ret;
506
507 if (irqfd->users == 0) {
508 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
509 if (ret < 0) {
510 return ret;
511 }
512 irqfd->virq = ret;
513 }
514 irqfd->users++;
515 return 0;
516 }
517
518 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
519 unsigned int vector)
520 {
521 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
522 if (--irqfd->users == 0) {
523 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
524 }
525 }
526
527 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
528 unsigned int queue_no,
529 unsigned int vector)
530 {
531 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
532 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
533 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
534 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
535 int ret;
536 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq);
537 return ret;
538 }
539
540 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
541 unsigned int queue_no,
542 unsigned int vector)
543 {
544 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
545 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
546 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
547 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
548 int ret;
549
550 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
551 assert(ret == 0);
552 }
553
554 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
555 {
556 PCIDevice *dev = &proxy->pci_dev;
557 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
558 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
559 unsigned int vector;
560 int ret, queue_no;
561 MSIMessage msg;
562
563 for (queue_no = 0; queue_no < nvqs; queue_no++) {
564 if (!virtio_queue_get_num(vdev, queue_no)) {
565 break;
566 }
567 vector = virtio_queue_vector(vdev, queue_no);
568 if (vector >= msix_nr_vectors_allocated(dev)) {
569 continue;
570 }
571 msg = msix_get_message(dev, vector);
572 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
573 if (ret < 0) {
574 goto undo;
575 }
576 /* If guest supports masking, set up irqfd now.
577 * Otherwise, delay until unmasked in the frontend.
578 */
579 if (k->guest_notifier_mask) {
580 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
581 if (ret < 0) {
582 kvm_virtio_pci_vq_vector_release(proxy, vector);
583 goto undo;
584 }
585 }
586 }
587 return 0;
588
589 undo:
590 while (--queue_no >= 0) {
591 vector = virtio_queue_vector(vdev, queue_no);
592 if (vector >= msix_nr_vectors_allocated(dev)) {
593 continue;
594 }
595 if (k->guest_notifier_mask) {
596 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
597 }
598 kvm_virtio_pci_vq_vector_release(proxy, vector);
599 }
600 return ret;
601 }
602
603 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
604 {
605 PCIDevice *dev = &proxy->pci_dev;
606 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
607 unsigned int vector;
608 int queue_no;
609 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
610
611 for (queue_no = 0; queue_no < nvqs; queue_no++) {
612 if (!virtio_queue_get_num(vdev, queue_no)) {
613 break;
614 }
615 vector = virtio_queue_vector(vdev, queue_no);
616 if (vector >= msix_nr_vectors_allocated(dev)) {
617 continue;
618 }
619 /* If guest supports masking, clean up irqfd now.
620 * Otherwise, it was cleaned when masked in the frontend.
621 */
622 if (k->guest_notifier_mask) {
623 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
624 }
625 kvm_virtio_pci_vq_vector_release(proxy, vector);
626 }
627 }
628
629 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
630 unsigned int queue_no,
631 unsigned int vector,
632 MSIMessage msg)
633 {
634 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
635 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
636 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
637 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
638 VirtIOIRQFD *irqfd;
639 int ret = 0;
640
641 if (proxy->vector_irqfd) {
642 irqfd = &proxy->vector_irqfd[vector];
643 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
644 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
645 if (ret < 0) {
646 return ret;
647 }
648 }
649 }
650
651 /* If guest supports masking, irqfd is already setup, unmask it.
652 * Otherwise, set it up now.
653 */
654 if (k->guest_notifier_mask) {
655 k->guest_notifier_mask(vdev, queue_no, false);
656 /* Test after unmasking to avoid losing events. */
657 if (k->guest_notifier_pending &&
658 k->guest_notifier_pending(vdev, queue_no)) {
659 event_notifier_set(n);
660 }
661 } else {
662 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
663 }
664 return ret;
665 }
666
667 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
668 unsigned int queue_no,
669 unsigned int vector)
670 {
671 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
672 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
673
674 /* If guest supports masking, keep irqfd but mask it.
675 * Otherwise, clean it up now.
676 */
677 if (k->guest_notifier_mask) {
678 k->guest_notifier_mask(vdev, queue_no, true);
679 } else {
680 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
681 }
682 }
683
684 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
685 MSIMessage msg)
686 {
687 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
688 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
689 int ret, queue_no;
690
691 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
692 if (!virtio_queue_get_num(vdev, queue_no)) {
693 break;
694 }
695 if (virtio_queue_vector(vdev, queue_no) != vector) {
696 continue;
697 }
698 ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg);
699 if (ret < 0) {
700 goto undo;
701 }
702 }
703 return 0;
704
705 undo:
706 while (--queue_no >= 0) {
707 if (virtio_queue_vector(vdev, queue_no) != vector) {
708 continue;
709 }
710 virtio_pci_vq_vector_mask(proxy, queue_no, vector);
711 }
712 return ret;
713 }
714
715 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
716 {
717 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
718 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
719 int queue_no;
720
721 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
722 if (!virtio_queue_get_num(vdev, queue_no)) {
723 break;
724 }
725 if (virtio_queue_vector(vdev, queue_no) != vector) {
726 continue;
727 }
728 virtio_pci_vq_vector_mask(proxy, queue_no, vector);
729 }
730 }
731
732 static void virtio_pci_vector_poll(PCIDevice *dev,
733 unsigned int vector_start,
734 unsigned int vector_end)
735 {
736 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
737 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
738 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
739 int queue_no;
740 unsigned int vector;
741 EventNotifier *notifier;
742 VirtQueue *vq;
743
744 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
745 if (!virtio_queue_get_num(vdev, queue_no)) {
746 break;
747 }
748 vector = virtio_queue_vector(vdev, queue_no);
749 if (vector < vector_start || vector >= vector_end ||
750 !msix_is_masked(dev, vector)) {
751 continue;
752 }
753 vq = virtio_get_queue(vdev, queue_no);
754 notifier = virtio_queue_get_guest_notifier(vq);
755 if (k->guest_notifier_pending) {
756 if (k->guest_notifier_pending(vdev, queue_no)) {
757 msix_set_pending(dev, vector);
758 }
759 } else if (event_notifier_test_and_clear(notifier)) {
760 msix_set_pending(dev, vector);
761 }
762 }
763 }
764
765 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
766 bool with_irqfd)
767 {
768 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
769 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
770 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
771 VirtQueue *vq = virtio_get_queue(vdev, n);
772 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
773
774 if (assign) {
775 int r = event_notifier_init(notifier, 0);
776 if (r < 0) {
777 return r;
778 }
779 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
780 } else {
781 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
782 event_notifier_cleanup(notifier);
783 }
784
785 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
786 vdc->guest_notifier_mask(vdev, n, !assign);
787 }
788
789 return 0;
790 }
791
792 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
793 {
794 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
795 return msix_enabled(&proxy->pci_dev);
796 }
797
798 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
799 {
800 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
801 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
802 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
803 int r, n;
804 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
805 kvm_msi_via_irqfd_enabled();
806
807 nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX);
808
809 /* When deassigning, pass a consistent nvqs value
810 * to avoid leaking notifiers.
811 */
812 assert(assign || nvqs == proxy->nvqs_with_notifiers);
813
814 proxy->nvqs_with_notifiers = nvqs;
815
816 /* Must unset vector notifier while guest notifier is still assigned */
817 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
818 msix_unset_vector_notifiers(&proxy->pci_dev);
819 if (proxy->vector_irqfd) {
820 kvm_virtio_pci_vector_release(proxy, nvqs);
821 g_free(proxy->vector_irqfd);
822 proxy->vector_irqfd = NULL;
823 }
824 }
825
826 for (n = 0; n < nvqs; n++) {
827 if (!virtio_queue_get_num(vdev, n)) {
828 break;
829 }
830
831 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
832 if (r < 0) {
833 goto assign_error;
834 }
835 }
836
837 /* Must set vector notifier after guest notifier has been assigned */
838 if ((with_irqfd || k->guest_notifier_mask) && assign) {
839 if (with_irqfd) {
840 proxy->vector_irqfd =
841 g_malloc0(sizeof(*proxy->vector_irqfd) *
842 msix_nr_vectors_allocated(&proxy->pci_dev));
843 r = kvm_virtio_pci_vector_use(proxy, nvqs);
844 if (r < 0) {
845 goto assign_error;
846 }
847 }
848 r = msix_set_vector_notifiers(&proxy->pci_dev,
849 virtio_pci_vector_unmask,
850 virtio_pci_vector_mask,
851 virtio_pci_vector_poll);
852 if (r < 0) {
853 goto notifiers_error;
854 }
855 }
856
857 return 0;
858
859 notifiers_error:
860 if (with_irqfd) {
861 assert(assign);
862 kvm_virtio_pci_vector_release(proxy, nvqs);
863 }
864
865 assign_error:
866 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
867 assert(assign);
868 while (--n >= 0) {
869 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
870 }
871 return r;
872 }
873
874 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
875 {
876 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
877
878 /* Stop using ioeventfd for virtqueue kick if the device starts using host
879 * notifiers. This makes it easy to avoid stepping on each others' toes.
880 */
881 proxy->ioeventfd_disabled = assign;
882 if (assign) {
883 virtio_pci_stop_ioeventfd(proxy);
884 }
885 /* We don't need to start here: it's not needed because backend
886 * currently only stops on status change away from ok,
887 * reset, vmstop and such. If we do add code to start here,
888 * need to check vmstate, device state etc. */
889 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
890 }
891
892 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
893 {
894 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
895 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
896
897 if (running) {
898 /* Try to find out if the guest has bus master disabled, but is
899 in ready state. Then we have a buggy guest OS. */
900 if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
901 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
902 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
903 }
904 virtio_pci_start_ioeventfd(proxy);
905 } else {
906 virtio_pci_stop_ioeventfd(proxy);
907 }
908 }
909
910 #ifdef CONFIG_VIRTFS
911 static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev)
912 {
913 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
914 DeviceState *vdev = DEVICE(&dev->vdev);
915
916 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
917 if (qdev_init(vdev) < 0) {
918 return -1;
919 }
920 return 0;
921 }
922
923 static Property virtio_9p_pci_properties[] = {
924 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
925 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
926 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
927 DEFINE_PROP_END_OF_LIST(),
928 };
929
930 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
931 {
932 DeviceClass *dc = DEVICE_CLASS(klass);
933 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
934 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
935
936 k->init = virtio_9p_init_pci;
937 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
938 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
939 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
940 pcidev_k->class_id = 0x2;
941 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
942 dc->props = virtio_9p_pci_properties;
943 }
944
945 static void virtio_9p_pci_instance_init(Object *obj)
946 {
947 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
948
949 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
950 TYPE_VIRTIO_9P);
951 }
952
953 static const TypeInfo virtio_9p_pci_info = {
954 .name = TYPE_VIRTIO_9P_PCI,
955 .parent = TYPE_VIRTIO_PCI,
956 .instance_size = sizeof(V9fsPCIState),
957 .instance_init = virtio_9p_pci_instance_init,
958 .class_init = virtio_9p_pci_class_init,
959 };
960 #endif /* CONFIG_VIRTFS */
961
962 /*
963 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
964 */
965
966 /* This is called by virtio-bus just after the device is plugged. */
967 static void virtio_pci_device_plugged(DeviceState *d)
968 {
969 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
970 VirtioBusState *bus = &proxy->bus;
971 uint8_t *config;
972 uint32_t size;
973
974 config = proxy->pci_dev.config;
975 if (proxy->class_code) {
976 pci_config_set_class(config, proxy->class_code);
977 }
978 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
979 pci_get_word(config + PCI_VENDOR_ID));
980 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
981 config[PCI_INTERRUPT_PIN] = 1;
982
983 if (proxy->nvectors &&
984 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
985 error_report("unable to init msix vectors to %" PRIu32,
986 proxy->nvectors);
987 proxy->nvectors = 0;
988 }
989
990 proxy->pci_dev.config_write = virtio_write_config;
991
992 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
993 + virtio_bus_get_vdev_config_len(bus);
994 if (size & (size - 1)) {
995 size = 1 << qemu_fls(size);
996 }
997
998 memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops,
999 proxy, "virtio-pci", size);
1000 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
1001 &proxy->bar);
1002
1003 if (!kvm_has_many_ioeventfds()) {
1004 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1005 }
1006
1007 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
1008 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
1009 proxy->host_features = virtio_bus_get_vdev_features(bus,
1010 proxy->host_features);
1011 }
1012
1013 static void virtio_pci_device_unplugged(DeviceState *d)
1014 {
1015 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1016
1017 virtio_pci_stop_ioeventfd(proxy);
1018 }
1019
1020 static int virtio_pci_init(PCIDevice *pci_dev)
1021 {
1022 VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
1023 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1024 virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev);
1025 if (k->init != NULL) {
1026 return k->init(dev);
1027 }
1028 return 0;
1029 }
1030
1031 static void virtio_pci_exit(PCIDevice *pci_dev)
1032 {
1033 msix_uninit_exclusive_bar(pci_dev);
1034 }
1035
1036 static void virtio_pci_reset(DeviceState *qdev)
1037 {
1038 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1039 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1040 virtio_pci_stop_ioeventfd(proxy);
1041 virtio_bus_reset(bus);
1042 msix_unuse_all_vectors(&proxy->pci_dev);
1043 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
1044 }
1045
1046 static Property virtio_pci_properties[] = {
1047 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1048 DEFINE_PROP_END_OF_LIST(),
1049 };
1050
1051 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1052 {
1053 DeviceClass *dc = DEVICE_CLASS(klass);
1054 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1055
1056 dc->props = virtio_pci_properties;
1057 k->init = virtio_pci_init;
1058 k->exit = virtio_pci_exit;
1059 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1060 k->revision = VIRTIO_PCI_ABI_VERSION;
1061 k->class_id = PCI_CLASS_OTHERS;
1062 dc->reset = virtio_pci_reset;
1063 }
1064
1065 static const TypeInfo virtio_pci_info = {
1066 .name = TYPE_VIRTIO_PCI,
1067 .parent = TYPE_PCI_DEVICE,
1068 .instance_size = sizeof(VirtIOPCIProxy),
1069 .class_init = virtio_pci_class_init,
1070 .class_size = sizeof(VirtioPCIClass),
1071 .abstract = true,
1072 };
1073
1074 /* virtio-blk-pci */
1075
1076 static Property virtio_blk_pci_properties[] = {
1077 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1078 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1079 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1080 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1081 DEFINE_PROP_END_OF_LIST(),
1082 };
1083
1084 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev)
1085 {
1086 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1087 DeviceState *vdev = DEVICE(&dev->vdev);
1088 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1089 if (qdev_init(vdev) < 0) {
1090 return -1;
1091 }
1092 return 0;
1093 }
1094
1095 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1096 {
1097 DeviceClass *dc = DEVICE_CLASS(klass);
1098 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1099 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1100
1101 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1102 dc->props = virtio_blk_pci_properties;
1103 k->init = virtio_blk_pci_init;
1104 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1105 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1106 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1107 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1108 }
1109
1110 static void virtio_blk_pci_instance_init(Object *obj)
1111 {
1112 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1113
1114 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1115 TYPE_VIRTIO_BLK);
1116 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1117 &error_abort);
1118 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1119 "bootindex", &error_abort);
1120 }
1121
1122 static const TypeInfo virtio_blk_pci_info = {
1123 .name = TYPE_VIRTIO_BLK_PCI,
1124 .parent = TYPE_VIRTIO_PCI,
1125 .instance_size = sizeof(VirtIOBlkPCI),
1126 .instance_init = virtio_blk_pci_instance_init,
1127 .class_init = virtio_blk_pci_class_init,
1128 };
1129
1130 /* virtio-scsi-pci */
1131
1132 static Property virtio_scsi_pci_properties[] = {
1133 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1134 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1135 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1136 DEV_NVECTORS_UNSPECIFIED),
1137 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features),
1138 DEFINE_PROP_END_OF_LIST(),
1139 };
1140
1141 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1142 {
1143 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1144 DeviceState *vdev = DEVICE(&dev->vdev);
1145 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1146 DeviceState *proxy = DEVICE(vpci_dev);
1147 char *bus_name;
1148
1149 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1150 vpci_dev->nvectors = vs->conf.num_queues + 3;
1151 }
1152
1153 /*
1154 * For command line compatibility, this sets the virtio-scsi-device bus
1155 * name as before.
1156 */
1157 if (proxy->id) {
1158 bus_name = g_strdup_printf("%s.0", proxy->id);
1159 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1160 g_free(bus_name);
1161 }
1162
1163 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1164 if (qdev_init(vdev) < 0) {
1165 return -1;
1166 }
1167 return 0;
1168 }
1169
1170 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1171 {
1172 DeviceClass *dc = DEVICE_CLASS(klass);
1173 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1174 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1175 k->init = virtio_scsi_pci_init_pci;
1176 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1177 dc->props = virtio_scsi_pci_properties;
1178 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1179 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1180 pcidev_k->revision = 0x00;
1181 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1182 }
1183
1184 static void virtio_scsi_pci_instance_init(Object *obj)
1185 {
1186 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1187
1188 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1189 TYPE_VIRTIO_SCSI);
1190 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1191 &error_abort);
1192 }
1193
1194 static const TypeInfo virtio_scsi_pci_info = {
1195 .name = TYPE_VIRTIO_SCSI_PCI,
1196 .parent = TYPE_VIRTIO_PCI,
1197 .instance_size = sizeof(VirtIOSCSIPCI),
1198 .instance_init = virtio_scsi_pci_instance_init,
1199 .class_init = virtio_scsi_pci_class_init,
1200 };
1201
1202 /* vhost-scsi-pci */
1203
1204 #ifdef CONFIG_VHOST_SCSI
1205 static Property vhost_scsi_pci_properties[] = {
1206 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1207 DEV_NVECTORS_UNSPECIFIED),
1208 DEFINE_PROP_END_OF_LIST(),
1209 };
1210
1211 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1212 {
1213 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1214 DeviceState *vdev = DEVICE(&dev->vdev);
1215 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1216
1217 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1218 vpci_dev->nvectors = vs->conf.num_queues + 3;
1219 }
1220
1221 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1222 if (qdev_init(vdev) < 0) {
1223 return -1;
1224 }
1225 return 0;
1226 }
1227
1228 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1229 {
1230 DeviceClass *dc = DEVICE_CLASS(klass);
1231 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1232 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1233 k->init = vhost_scsi_pci_init_pci;
1234 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1235 dc->props = vhost_scsi_pci_properties;
1236 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1237 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1238 pcidev_k->revision = 0x00;
1239 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1240 }
1241
1242 static void vhost_scsi_pci_instance_init(Object *obj)
1243 {
1244 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1245
1246 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1247 TYPE_VHOST_SCSI);
1248 }
1249
1250 static const TypeInfo vhost_scsi_pci_info = {
1251 .name = TYPE_VHOST_SCSI_PCI,
1252 .parent = TYPE_VIRTIO_PCI,
1253 .instance_size = sizeof(VHostSCSIPCI),
1254 .instance_init = vhost_scsi_pci_instance_init,
1255 .class_init = vhost_scsi_pci_class_init,
1256 };
1257 #endif
1258
1259 /* virtio-balloon-pci */
1260
1261 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v,
1262 void *opaque, const char *name,
1263 Error **errp)
1264 {
1265 VirtIOBalloonPCI *dev = opaque;
1266 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
1267 }
1268
1269 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v,
1270 void *opaque, const char *name,
1271 Error **errp)
1272 {
1273 VirtIOBalloonPCI *dev = opaque;
1274 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1275 errp);
1276 }
1277
1278 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v,
1279 void *opaque, const char *name,
1280 Error **errp)
1281 {
1282 VirtIOBalloonPCI *dev = opaque;
1283 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1284 errp);
1285 }
1286
1287 static Property virtio_balloon_pci_properties[] = {
1288 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1289 DEFINE_PROP_END_OF_LIST(),
1290 };
1291
1292 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
1293 {
1294 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1295 DeviceState *vdev = DEVICE(&dev->vdev);
1296
1297 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1298 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1299 vpci_dev->class_code = PCI_CLASS_OTHERS;
1300 }
1301
1302 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1303 if (qdev_init(vdev) < 0) {
1304 return -1;
1305 }
1306 return 0;
1307 }
1308
1309 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1310 {
1311 DeviceClass *dc = DEVICE_CLASS(klass);
1312 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1313 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1314 k->init = virtio_balloon_pci_init;
1315 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1316 dc->props = virtio_balloon_pci_properties;
1317 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1318 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1319 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1320 pcidev_k->class_id = PCI_CLASS_OTHERS;
1321 }
1322
1323 static void virtio_balloon_pci_instance_init(Object *obj)
1324 {
1325 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1326 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
1327 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1328 object_unref(OBJECT(&dev->vdev));
1329 object_property_add(obj, "guest-stats", "guest statistics",
1330 balloon_pci_stats_get_all, NULL, NULL, dev,
1331 NULL);
1332
1333 object_property_add(obj, "guest-stats-polling-interval", "int",
1334 balloon_pci_stats_get_poll_interval,
1335 balloon_pci_stats_set_poll_interval,
1336 NULL, dev, NULL);
1337 }
1338
1339 static const TypeInfo virtio_balloon_pci_info = {
1340 .name = TYPE_VIRTIO_BALLOON_PCI,
1341 .parent = TYPE_VIRTIO_PCI,
1342 .instance_size = sizeof(VirtIOBalloonPCI),
1343 .instance_init = virtio_balloon_pci_instance_init,
1344 .class_init = virtio_balloon_pci_class_init,
1345 };
1346
1347 /* virtio-serial-pci */
1348
1349 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev)
1350 {
1351 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1352 DeviceState *vdev = DEVICE(&dev->vdev);
1353 DeviceState *proxy = DEVICE(vpci_dev);
1354 char *bus_name;
1355
1356 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1357 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1358 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
1359 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1360 }
1361
1362 /* backwards-compatibility with machines that were created with
1363 DEV_NVECTORS_UNSPECIFIED */
1364 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1365 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1366 }
1367
1368 /*
1369 * For command line compatibility, this sets the virtio-serial-device bus
1370 * name as before.
1371 */
1372 if (proxy->id) {
1373 bus_name = g_strdup_printf("%s.0", proxy->id);
1374 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1375 g_free(bus_name);
1376 }
1377
1378 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1379 if (qdev_init(vdev) < 0) {
1380 return -1;
1381 }
1382 return 0;
1383 }
1384
1385 static Property virtio_serial_pci_properties[] = {
1386 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1387 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1388 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1389 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1390 DEFINE_PROP_END_OF_LIST(),
1391 };
1392
1393 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1394 {
1395 DeviceClass *dc = DEVICE_CLASS(klass);
1396 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1397 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1398 k->init = virtio_serial_pci_init;
1399 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1400 dc->props = virtio_serial_pci_properties;
1401 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1402 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1403 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1404 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1405 }
1406
1407 static void virtio_serial_pci_instance_init(Object *obj)
1408 {
1409 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1410
1411 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1412 TYPE_VIRTIO_SERIAL);
1413 }
1414
1415 static const TypeInfo virtio_serial_pci_info = {
1416 .name = TYPE_VIRTIO_SERIAL_PCI,
1417 .parent = TYPE_VIRTIO_PCI,
1418 .instance_size = sizeof(VirtIOSerialPCI),
1419 .instance_init = virtio_serial_pci_instance_init,
1420 .class_init = virtio_serial_pci_class_init,
1421 };
1422
1423 /* virtio-net-pci */
1424
1425 static Property virtio_net_properties[] = {
1426 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1427 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1428 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1429 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
1430 DEFINE_PROP_END_OF_LIST(),
1431 };
1432
1433 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev)
1434 {
1435 DeviceState *qdev = DEVICE(vpci_dev);
1436 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1437 DeviceState *vdev = DEVICE(&dev->vdev);
1438
1439 virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features);
1440 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1441 object_get_typename(OBJECT(qdev)));
1442 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1443 if (qdev_init(vdev) < 0) {
1444 return -1;
1445 }
1446 return 0;
1447 }
1448
1449 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1450 {
1451 DeviceClass *dc = DEVICE_CLASS(klass);
1452 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1453 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1454
1455 k->romfile = "efi-virtio.rom";
1456 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1457 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1458 k->revision = VIRTIO_PCI_ABI_VERSION;
1459 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1460 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1461 dc->props = virtio_net_properties;
1462 vpciklass->init = virtio_net_pci_init;
1463 }
1464
1465 static void virtio_net_pci_instance_init(Object *obj)
1466 {
1467 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
1468
1469 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1470 TYPE_VIRTIO_NET);
1471 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1472 "bootindex", &error_abort);
1473 }
1474
1475 static const TypeInfo virtio_net_pci_info = {
1476 .name = TYPE_VIRTIO_NET_PCI,
1477 .parent = TYPE_VIRTIO_PCI,
1478 .instance_size = sizeof(VirtIONetPCI),
1479 .instance_init = virtio_net_pci_instance_init,
1480 .class_init = virtio_net_pci_class_init,
1481 };
1482
1483 /* virtio-rng-pci */
1484
1485 static Property virtio_rng_pci_properties[] = {
1486 DEFINE_PROP_END_OF_LIST(),
1487 };
1488
1489 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev)
1490 {
1491 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
1492 DeviceState *vdev = DEVICE(&vrng->vdev);
1493
1494 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1495 if (qdev_init(vdev) < 0) {
1496 return -1;
1497 }
1498
1499 object_property_set_link(OBJECT(vrng),
1500 OBJECT(vrng->vdev.conf.rng), "rng",
1501 NULL);
1502
1503 return 0;
1504 }
1505
1506 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
1507 {
1508 DeviceClass *dc = DEVICE_CLASS(klass);
1509 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1510 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1511
1512 k->init = virtio_rng_pci_init;
1513 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1514 dc->props = virtio_rng_pci_properties;
1515
1516 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1517 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1518 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1519 pcidev_k->class_id = PCI_CLASS_OTHERS;
1520 }
1521
1522 static void virtio_rng_initfn(Object *obj)
1523 {
1524 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
1525
1526 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1527 TYPE_VIRTIO_RNG);
1528 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1529 (Object **)&dev->vdev.conf.rng,
1530 qdev_prop_allow_set_link_before_realize,
1531 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
1532
1533 }
1534
1535 static const TypeInfo virtio_rng_pci_info = {
1536 .name = TYPE_VIRTIO_RNG_PCI,
1537 .parent = TYPE_VIRTIO_PCI,
1538 .instance_size = sizeof(VirtIORngPCI),
1539 .instance_init = virtio_rng_initfn,
1540 .class_init = virtio_rng_pci_class_init,
1541 };
1542
1543 /* virtio-pci-bus */
1544
1545 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
1546 VirtIOPCIProxy *dev)
1547 {
1548 DeviceState *qdev = DEVICE(dev);
1549 char virtio_bus_name[] = "virtio-bus";
1550
1551 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
1552 virtio_bus_name);
1553 }
1554
1555 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
1556 {
1557 BusClass *bus_class = BUS_CLASS(klass);
1558 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1559 bus_class->max_dev = 1;
1560 k->notify = virtio_pci_notify;
1561 k->save_config = virtio_pci_save_config;
1562 k->load_config = virtio_pci_load_config;
1563 k->save_queue = virtio_pci_save_queue;
1564 k->load_queue = virtio_pci_load_queue;
1565 k->get_features = virtio_pci_get_features;
1566 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
1567 k->set_host_notifier = virtio_pci_set_host_notifier;
1568 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
1569 k->vmstate_change = virtio_pci_vmstate_change;
1570 k->device_plugged = virtio_pci_device_plugged;
1571 k->device_unplugged = virtio_pci_device_unplugged;
1572 }
1573
1574 static const TypeInfo virtio_pci_bus_info = {
1575 .name = TYPE_VIRTIO_PCI_BUS,
1576 .parent = TYPE_VIRTIO_BUS,
1577 .instance_size = sizeof(VirtioPCIBusState),
1578 .class_init = virtio_pci_bus_class_init,
1579 };
1580
1581 static void virtio_pci_register_types(void)
1582 {
1583 type_register_static(&virtio_rng_pci_info);
1584 type_register_static(&virtio_pci_bus_info);
1585 type_register_static(&virtio_pci_info);
1586 #ifdef CONFIG_VIRTFS
1587 type_register_static(&virtio_9p_pci_info);
1588 #endif
1589 type_register_static(&virtio_blk_pci_info);
1590 type_register_static(&virtio_scsi_pci_info);
1591 type_register_static(&virtio_balloon_pci_info);
1592 type_register_static(&virtio_serial_pci_info);
1593 type_register_static(&virtio_net_pci_info);
1594 #ifdef CONFIG_VHOST_SCSI
1595 type_register_static(&vhost_scsi_pci_info);
1596 #endif
1597 }
1598
1599 type_init(virtio_pci_register_types)