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