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