]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio/virtio-pci.c
virtio: Remove unnecessary OBJECT() casts
[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 /* 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, NULL, 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 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
915 dc->props = virtio_9p_pci_properties;
916 }
917
918 static void virtio_9p_pci_instance_init(Object *obj)
919 {
920 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
921 object_initialize(&dev->vdev, TYPE_VIRTIO_9P);
922 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
923 }
924
925 static const TypeInfo virtio_9p_pci_info = {
926 .name = TYPE_VIRTIO_9P_PCI,
927 .parent = TYPE_VIRTIO_PCI,
928 .instance_size = sizeof(V9fsPCIState),
929 .instance_init = virtio_9p_pci_instance_init,
930 .class_init = virtio_9p_pci_class_init,
931 };
932 #endif /* CONFIG_VIRTFS */
933
934 /*
935 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
936 */
937
938 /* This is called by virtio-bus just after the device is plugged. */
939 static void virtio_pci_device_plugged(DeviceState *d)
940 {
941 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
942 VirtioBusState *bus = &proxy->bus;
943 uint8_t *config;
944 uint32_t size;
945
946 proxy->vdev = bus->vdev;
947
948 config = proxy->pci_dev.config;
949 if (proxy->class_code) {
950 pci_config_set_class(config, proxy->class_code);
951 }
952 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
953 pci_get_word(config + PCI_VENDOR_ID));
954 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
955 config[PCI_INTERRUPT_PIN] = 1;
956
957 if (proxy->nvectors &&
958 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
959 proxy->nvectors = 0;
960 }
961
962 proxy->pci_dev.config_write = virtio_write_config;
963
964 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
965 + virtio_bus_get_vdev_config_len(bus);
966 if (size & (size - 1)) {
967 size = 1 << qemu_fls(size);
968 }
969
970 memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops,
971 proxy, "virtio-pci", size);
972 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
973 &proxy->bar);
974
975 if (!kvm_has_many_ioeventfds()) {
976 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
977 }
978
979 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
980 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
981 proxy->host_features = virtio_bus_get_vdev_features(bus,
982 proxy->host_features);
983 }
984
985 static int virtio_pci_init(PCIDevice *pci_dev)
986 {
987 VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
988 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
989 virtio_pci_bus_new(&dev->bus, dev);
990 if (k->init != NULL) {
991 return k->init(dev);
992 }
993 return 0;
994 }
995
996 static void virtio_pci_exit(PCIDevice *pci_dev)
997 {
998 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
999 virtio_pci_stop_ioeventfd(proxy);
1000 memory_region_destroy(&proxy->bar);
1001 msix_uninit_exclusive_bar(pci_dev);
1002 }
1003
1004 static void virtio_pci_reset(DeviceState *qdev)
1005 {
1006 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1007 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1008 virtio_pci_stop_ioeventfd(proxy);
1009 virtio_bus_reset(bus);
1010 msix_unuse_all_vectors(&proxy->pci_dev);
1011 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
1012 }
1013
1014 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1015 {
1016 DeviceClass *dc = DEVICE_CLASS(klass);
1017 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1018
1019 k->init = virtio_pci_init;
1020 k->exit = virtio_pci_exit;
1021 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1022 k->revision = VIRTIO_PCI_ABI_VERSION;
1023 k->class_id = PCI_CLASS_OTHERS;
1024 dc->reset = virtio_pci_reset;
1025 }
1026
1027 static const TypeInfo virtio_pci_info = {
1028 .name = TYPE_VIRTIO_PCI,
1029 .parent = TYPE_PCI_DEVICE,
1030 .instance_size = sizeof(VirtIOPCIProxy),
1031 .class_init = virtio_pci_class_init,
1032 .class_size = sizeof(VirtioPCIClass),
1033 .abstract = true,
1034 };
1035
1036 /* virtio-blk-pci */
1037
1038 static Property virtio_blk_pci_properties[] = {
1039 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1040 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1041 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1042 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1043 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1044 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false),
1045 #endif
1046 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
1047 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk),
1048 DEFINE_PROP_END_OF_LIST(),
1049 };
1050
1051 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev)
1052 {
1053 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1054 DeviceState *vdev = DEVICE(&dev->vdev);
1055 virtio_blk_set_conf(vdev, &(dev->blk));
1056 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1057 if (qdev_init(vdev) < 0) {
1058 return -1;
1059 }
1060 return 0;
1061 }
1062
1063 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1064 {
1065 DeviceClass *dc = DEVICE_CLASS(klass);
1066 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1067 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1068
1069 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1070 dc->props = virtio_blk_pci_properties;
1071 k->init = virtio_blk_pci_init;
1072 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1073 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1074 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1075 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1076 }
1077
1078 static void virtio_blk_pci_instance_init(Object *obj)
1079 {
1080 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1081 object_initialize(&dev->vdev, TYPE_VIRTIO_BLK);
1082 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1083 }
1084
1085 static const TypeInfo virtio_blk_pci_info = {
1086 .name = TYPE_VIRTIO_BLK_PCI,
1087 .parent = TYPE_VIRTIO_PCI,
1088 .instance_size = sizeof(VirtIOBlkPCI),
1089 .instance_init = virtio_blk_pci_instance_init,
1090 .class_init = virtio_blk_pci_class_init,
1091 };
1092
1093 /* virtio-scsi-pci */
1094
1095 static Property virtio_scsi_pci_properties[] = {
1096 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1097 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1098 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1099 DEV_NVECTORS_UNSPECIFIED),
1100 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features),
1101 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf),
1102 DEFINE_PROP_END_OF_LIST(),
1103 };
1104
1105 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1106 {
1107 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1108 DeviceState *vdev = DEVICE(&dev->vdev);
1109 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1110 DeviceState *proxy = DEVICE(vpci_dev);
1111 char *bus_name;
1112
1113 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1114 vpci_dev->nvectors = vs->conf.num_queues + 3;
1115 }
1116
1117 /*
1118 * For command line compatibility, this sets the virtio-scsi-device bus
1119 * name as before.
1120 */
1121 if (proxy->id) {
1122 bus_name = g_strdup_printf("%s.0", proxy->id);
1123 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1124 g_free(bus_name);
1125 }
1126
1127 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1128 if (qdev_init(vdev) < 0) {
1129 return -1;
1130 }
1131 return 0;
1132 }
1133
1134 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1135 {
1136 DeviceClass *dc = DEVICE_CLASS(klass);
1137 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1138 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1139 k->init = virtio_scsi_pci_init_pci;
1140 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1141 dc->props = virtio_scsi_pci_properties;
1142 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1143 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1144 pcidev_k->revision = 0x00;
1145 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1146 }
1147
1148 static void virtio_scsi_pci_instance_init(Object *obj)
1149 {
1150 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1151 object_initialize(&dev->vdev, TYPE_VIRTIO_SCSI);
1152 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1153 }
1154
1155 static const TypeInfo virtio_scsi_pci_info = {
1156 .name = TYPE_VIRTIO_SCSI_PCI,
1157 .parent = TYPE_VIRTIO_PCI,
1158 .instance_size = sizeof(VirtIOSCSIPCI),
1159 .instance_init = virtio_scsi_pci_instance_init,
1160 .class_init = virtio_scsi_pci_class_init,
1161 };
1162
1163 /* vhost-scsi-pci */
1164
1165 #ifdef CONFIG_VHOST_SCSI
1166 static Property vhost_scsi_pci_properties[] = {
1167 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1168 DEV_NVECTORS_UNSPECIFIED),
1169 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1170 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf),
1171 DEFINE_PROP_END_OF_LIST(),
1172 };
1173
1174 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1175 {
1176 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1177 DeviceState *vdev = DEVICE(&dev->vdev);
1178 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1179
1180 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1181 vpci_dev->nvectors = vs->conf.num_queues + 3;
1182 }
1183
1184 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1185 if (qdev_init(vdev) < 0) {
1186 return -1;
1187 }
1188 return 0;
1189 }
1190
1191 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1192 {
1193 DeviceClass *dc = DEVICE_CLASS(klass);
1194 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1195 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1196 k->init = vhost_scsi_pci_init_pci;
1197 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1198 dc->props = vhost_scsi_pci_properties;
1199 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1200 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1201 pcidev_k->revision = 0x00;
1202 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1203 }
1204
1205 static void vhost_scsi_pci_instance_init(Object *obj)
1206 {
1207 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1208 object_initialize(&dev->vdev, TYPE_VHOST_SCSI);
1209 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1210 }
1211
1212 static const TypeInfo vhost_scsi_pci_info = {
1213 .name = TYPE_VHOST_SCSI_PCI,
1214 .parent = TYPE_VIRTIO_PCI,
1215 .instance_size = sizeof(VHostSCSIPCI),
1216 .instance_init = vhost_scsi_pci_instance_init,
1217 .class_init = vhost_scsi_pci_class_init,
1218 };
1219 #endif
1220
1221 /* virtio-balloon-pci */
1222
1223 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v,
1224 void *opaque, const char *name,
1225 Error **errp)
1226 {
1227 VirtIOBalloonPCI *dev = opaque;
1228 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
1229 }
1230
1231 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v,
1232 void *opaque, const char *name,
1233 Error **errp)
1234 {
1235 VirtIOBalloonPCI *dev = opaque;
1236 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1237 errp);
1238 }
1239
1240 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v,
1241 void *opaque, const char *name,
1242 Error **errp)
1243 {
1244 VirtIOBalloonPCI *dev = opaque;
1245 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1246 errp);
1247 }
1248
1249 static Property virtio_balloon_pci_properties[] = {
1250 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1251 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1252 DEFINE_PROP_END_OF_LIST(),
1253 };
1254
1255 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
1256 {
1257 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1258 DeviceState *vdev = DEVICE(&dev->vdev);
1259
1260 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1261 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1262 vpci_dev->class_code = PCI_CLASS_OTHERS;
1263 }
1264
1265 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1266 if (qdev_init(vdev) < 0) {
1267 return -1;
1268 }
1269 return 0;
1270 }
1271
1272 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1273 {
1274 DeviceClass *dc = DEVICE_CLASS(klass);
1275 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1276 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1277 k->init = virtio_balloon_pci_init;
1278 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1279 dc->props = virtio_balloon_pci_properties;
1280 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1281 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1282 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1283 pcidev_k->class_id = PCI_CLASS_OTHERS;
1284 }
1285
1286 static void virtio_balloon_pci_instance_init(Object *obj)
1287 {
1288 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1289 object_initialize(&dev->vdev, TYPE_VIRTIO_BALLOON);
1290 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1291
1292 object_property_add(obj, "guest-stats", "guest statistics",
1293 balloon_pci_stats_get_all, NULL, NULL, dev,
1294 NULL);
1295
1296 object_property_add(obj, "guest-stats-polling-interval", "int",
1297 balloon_pci_stats_get_poll_interval,
1298 balloon_pci_stats_set_poll_interval,
1299 NULL, dev, NULL);
1300 }
1301
1302 static const TypeInfo virtio_balloon_pci_info = {
1303 .name = TYPE_VIRTIO_BALLOON_PCI,
1304 .parent = TYPE_VIRTIO_PCI,
1305 .instance_size = sizeof(VirtIOBalloonPCI),
1306 .instance_init = virtio_balloon_pci_instance_init,
1307 .class_init = virtio_balloon_pci_class_init,
1308 };
1309
1310 /* virtio-serial-pci */
1311
1312 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev)
1313 {
1314 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1315 DeviceState *vdev = DEVICE(&dev->vdev);
1316 DeviceState *proxy = DEVICE(vpci_dev);
1317 char *bus_name;
1318
1319 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1320 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1321 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
1322 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1323 }
1324
1325 /* backwards-compatibility with machines that were created with
1326 DEV_NVECTORS_UNSPECIFIED */
1327 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1328 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1329 }
1330
1331 /*
1332 * For command line compatibility, this sets the virtio-serial-device bus
1333 * name as before.
1334 */
1335 if (proxy->id) {
1336 bus_name = g_strdup_printf("%s.0", proxy->id);
1337 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1338 g_free(bus_name);
1339 }
1340
1341 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1342 if (qdev_init(vdev) < 0) {
1343 return -1;
1344 }
1345 return 0;
1346 }
1347
1348 static Property virtio_serial_pci_properties[] = {
1349 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1350 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1351 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1352 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1353 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1354 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial),
1355 DEFINE_PROP_END_OF_LIST(),
1356 };
1357
1358 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1359 {
1360 DeviceClass *dc = DEVICE_CLASS(klass);
1361 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1362 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1363 k->init = virtio_serial_pci_init;
1364 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1365 dc->props = virtio_serial_pci_properties;
1366 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1367 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1368 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1369 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1370 }
1371
1372 static void virtio_serial_pci_instance_init(Object *obj)
1373 {
1374 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1375 object_initialize(&dev->vdev, TYPE_VIRTIO_SERIAL);
1376 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1377 }
1378
1379 static const TypeInfo virtio_serial_pci_info = {
1380 .name = TYPE_VIRTIO_SERIAL_PCI,
1381 .parent = TYPE_VIRTIO_PCI,
1382 .instance_size = sizeof(VirtIOSerialPCI),
1383 .instance_init = virtio_serial_pci_instance_init,
1384 .class_init = virtio_serial_pci_class_init,
1385 };
1386
1387 /* virtio-net-pci */
1388
1389 static Property virtio_net_properties[] = {
1390 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1391 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1392 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1393 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
1394 DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf),
1395 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf),
1396 DEFINE_PROP_END_OF_LIST(),
1397 };
1398
1399 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev)
1400 {
1401 DeviceState *qdev = DEVICE(vpci_dev);
1402 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1403 DeviceState *vdev = DEVICE(&dev->vdev);
1404
1405 virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features);
1406 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1407 object_get_typename(OBJECT(qdev)));
1408 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1409 if (qdev_init(vdev) < 0) {
1410 return -1;
1411 }
1412 return 0;
1413 }
1414
1415 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1416 {
1417 DeviceClass *dc = DEVICE_CLASS(klass);
1418 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1419 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1420
1421 k->romfile = "efi-virtio.rom";
1422 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1423 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1424 k->revision = VIRTIO_PCI_ABI_VERSION;
1425 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1426 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1427 dc->props = virtio_net_properties;
1428 vpciklass->init = virtio_net_pci_init;
1429 }
1430
1431 static void virtio_net_pci_instance_init(Object *obj)
1432 {
1433 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
1434 object_initialize(&dev->vdev, TYPE_VIRTIO_NET);
1435 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1436 }
1437
1438 static const TypeInfo virtio_net_pci_info = {
1439 .name = TYPE_VIRTIO_NET_PCI,
1440 .parent = TYPE_VIRTIO_PCI,
1441 .instance_size = sizeof(VirtIONetPCI),
1442 .instance_init = virtio_net_pci_instance_init,
1443 .class_init = virtio_net_pci_class_init,
1444 };
1445
1446 /* virtio-rng-pci */
1447
1448 static Property virtio_rng_pci_properties[] = {
1449 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1450 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf),
1451 DEFINE_PROP_END_OF_LIST(),
1452 };
1453
1454 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev)
1455 {
1456 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
1457 DeviceState *vdev = DEVICE(&vrng->vdev);
1458
1459 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1460 if (qdev_init(vdev) < 0) {
1461 return -1;
1462 }
1463
1464 object_property_set_link(OBJECT(vrng),
1465 OBJECT(vrng->vdev.conf.rng), "rng",
1466 NULL);
1467
1468 return 0;
1469 }
1470
1471 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
1472 {
1473 DeviceClass *dc = DEVICE_CLASS(klass);
1474 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1475 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1476
1477 k->init = virtio_rng_pci_init;
1478 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1479 dc->props = virtio_rng_pci_properties;
1480
1481 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1482 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1483 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1484 pcidev_k->class_id = PCI_CLASS_OTHERS;
1485 }
1486
1487 static void virtio_rng_initfn(Object *obj)
1488 {
1489 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
1490 object_initialize(&dev->vdev, TYPE_VIRTIO_RNG);
1491 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1492 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1493 (Object **)&dev->vdev.conf.rng, NULL);
1494
1495 }
1496
1497 static const TypeInfo virtio_rng_pci_info = {
1498 .name = TYPE_VIRTIO_RNG_PCI,
1499 .parent = TYPE_VIRTIO_PCI,
1500 .instance_size = sizeof(VirtIORngPCI),
1501 .instance_init = virtio_rng_initfn,
1502 .class_init = virtio_rng_pci_class_init,
1503 };
1504
1505 /* virtio-pci-bus */
1506
1507 static void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev)
1508 {
1509 DeviceState *qdev = DEVICE(dev);
1510 BusState *qbus;
1511 char virtio_bus_name[] = "virtio-bus";
1512
1513 qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_PCI_BUS, qdev,
1514 virtio_bus_name);
1515 qbus = BUS(bus);
1516 qbus->allow_hotplug = 1;
1517 }
1518
1519 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
1520 {
1521 BusClass *bus_class = BUS_CLASS(klass);
1522 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1523 bus_class->max_dev = 1;
1524 k->notify = virtio_pci_notify;
1525 k->save_config = virtio_pci_save_config;
1526 k->load_config = virtio_pci_load_config;
1527 k->save_queue = virtio_pci_save_queue;
1528 k->load_queue = virtio_pci_load_queue;
1529 k->get_features = virtio_pci_get_features;
1530 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
1531 k->set_host_notifier = virtio_pci_set_host_notifier;
1532 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
1533 k->vmstate_change = virtio_pci_vmstate_change;
1534 k->device_plugged = virtio_pci_device_plugged;
1535 }
1536
1537 static const TypeInfo virtio_pci_bus_info = {
1538 .name = TYPE_VIRTIO_PCI_BUS,
1539 .parent = TYPE_VIRTIO_BUS,
1540 .instance_size = sizeof(VirtioPCIBusState),
1541 .class_init = virtio_pci_bus_class_init,
1542 };
1543
1544 static void virtio_pci_register_types(void)
1545 {
1546 type_register_static(&virtio_rng_pci_info);
1547 type_register_static(&virtio_pci_bus_info);
1548 type_register_static(&virtio_pci_info);
1549 #ifdef CONFIG_VIRTFS
1550 type_register_static(&virtio_9p_pci_info);
1551 #endif
1552 type_register_static(&virtio_blk_pci_info);
1553 type_register_static(&virtio_scsi_pci_info);
1554 type_register_static(&virtio_balloon_pci_info);
1555 type_register_static(&virtio_serial_pci_info);
1556 type_register_static(&virtio_net_pci_info);
1557 #ifdef CONFIG_VHOST_SCSI
1558 type_register_static(&vhost_scsi_pci_info);
1559 #endif
1560 }
1561
1562 type_init(virtio_pci_register_types)