]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio-pci.c
Merge remote-tracking branch 'mst/for_anthony' into staging
[mirror_qemu.git] / hw / 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 */
15
16 #include <inttypes.h>
17
18 #include "virtio.h"
19 #include "virtio-blk.h"
20 #include "virtio-net.h"
21 #include "virtio-serial.h"
22 #include "pci.h"
23 #include "qemu-error.h"
24 #include "msix.h"
25 #include "net.h"
26 #include "loader.h"
27 #include "kvm.h"
28 #include "blockdev.h"
29 #include "virtio-pci.h"
30 #include "range.h"
31
32 /* from Linux's linux/virtio_pci.h */
33
34 /* A 32-bit r/o bitmask of the features supported by the host */
35 #define VIRTIO_PCI_HOST_FEATURES 0
36
37 /* A 32-bit r/w bitmask of features activated by the guest */
38 #define VIRTIO_PCI_GUEST_FEATURES 4
39
40 /* A 32-bit r/w PFN for the currently selected queue */
41 #define VIRTIO_PCI_QUEUE_PFN 8
42
43 /* A 16-bit r/o queue size for the currently selected queue */
44 #define VIRTIO_PCI_QUEUE_NUM 12
45
46 /* A 16-bit r/w queue selector */
47 #define VIRTIO_PCI_QUEUE_SEL 14
48
49 /* A 16-bit r/w queue notifier */
50 #define VIRTIO_PCI_QUEUE_NOTIFY 16
51
52 /* An 8-bit device status register. */
53 #define VIRTIO_PCI_STATUS 18
54
55 /* An 8-bit r/o interrupt status register. Reading the value will return the
56 * current contents of the ISR and will also clear it. This is effectively
57 * a read-and-acknowledge. */
58 #define VIRTIO_PCI_ISR 19
59
60 /* MSI-X registers: only enabled if MSI-X is enabled. */
61 /* A 16-bit vector for configuration changes. */
62 #define VIRTIO_MSI_CONFIG_VECTOR 20
63 /* A 16-bit vector for selected queue notifications. */
64 #define VIRTIO_MSI_QUEUE_VECTOR 22
65
66 /* Config space size */
67 #define VIRTIO_PCI_CONFIG_NOMSI 20
68 #define VIRTIO_PCI_CONFIG_MSI 24
69 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
70 VIRTIO_PCI_CONFIG_MSI : \
71 VIRTIO_PCI_CONFIG_NOMSI)
72
73 /* The remaining space is defined by each driver as the per-driver
74 * configuration space */
75 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
78
79 /* How many bits to shift physical queue address written to QUEUE_PFN.
80 * 12 is historical, and due to x86 page size. */
81 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
82
83 /* Flags track per-device state like workarounds for quirks in older guests. */
84 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
85
86 /* Performance improves when virtqueue kick processing is decoupled from the
87 * vcpu thread using ioeventfd for some devices. */
88 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
89 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
90
91 /* QEMU doesn't strictly need write barriers since everything runs in
92 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
93 * KVM or if kqemu gets SMP support.
94 */
95 #define wmb() do { } while (0)
96
97 /* virtio device */
98
99 static void virtio_pci_notify(void *opaque, uint16_t vector)
100 {
101 VirtIOPCIProxy *proxy = opaque;
102 if (msix_enabled(&proxy->pci_dev))
103 msix_notify(&proxy->pci_dev, vector);
104 else
105 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
106 }
107
108 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
109 {
110 VirtIOPCIProxy *proxy = opaque;
111 pci_device_save(&proxy->pci_dev, f);
112 msix_save(&proxy->pci_dev, f);
113 if (msix_present(&proxy->pci_dev))
114 qemu_put_be16(f, proxy->vdev->config_vector);
115 }
116
117 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
118 {
119 VirtIOPCIProxy *proxy = opaque;
120 if (msix_present(&proxy->pci_dev))
121 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
122 }
123
124 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
125 {
126 VirtIOPCIProxy *proxy = opaque;
127 int ret;
128 ret = pci_device_load(&proxy->pci_dev, f);
129 if (ret) {
130 return ret;
131 }
132 msix_load(&proxy->pci_dev, f);
133 if (msix_present(&proxy->pci_dev)) {
134 qemu_get_be16s(f, &proxy->vdev->config_vector);
135 } else {
136 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
137 }
138 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
139 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
140 }
141 return 0;
142 }
143
144 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
145 {
146 VirtIOPCIProxy *proxy = opaque;
147 uint16_t vector;
148 if (msix_present(&proxy->pci_dev)) {
149 qemu_get_be16s(f, &vector);
150 } else {
151 vector = VIRTIO_NO_VECTOR;
152 }
153 virtio_queue_set_vector(proxy->vdev, n, vector);
154 if (vector != VIRTIO_NO_VECTOR) {
155 return msix_vector_use(&proxy->pci_dev, vector);
156 }
157 return 0;
158 }
159
160 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
161 int n, bool assign)
162 {
163 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
164 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
165 int r;
166 if (assign) {
167 r = event_notifier_init(notifier, 1);
168 if (r < 0) {
169 error_report("%s: unable to init event notifier: %d",
170 __func__, r);
171 return r;
172 }
173 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
174 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
175 n, assign);
176 if (r < 0) {
177 error_report("%s: unable to map ioeventfd: %d",
178 __func__, r);
179 event_notifier_cleanup(notifier);
180 }
181 } else {
182 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
183 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
184 n, assign);
185 if (r < 0) {
186 error_report("%s: unable to unmap ioeventfd: %d",
187 __func__, r);
188 return r;
189 }
190
191 /* Handle the race condition where the guest kicked and we deassigned
192 * before we got around to handling the kick.
193 */
194 if (event_notifier_test_and_clear(notifier)) {
195 virtio_queue_notify_vq(vq);
196 }
197
198 event_notifier_cleanup(notifier);
199 }
200 return r;
201 }
202
203 static void virtio_pci_host_notifier_read(void *opaque)
204 {
205 VirtQueue *vq = opaque;
206 EventNotifier *n = virtio_queue_get_host_notifier(vq);
207 if (event_notifier_test_and_clear(n)) {
208 virtio_queue_notify_vq(vq);
209 }
210 }
211
212 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
213 int n, bool assign)
214 {
215 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
216 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
217 if (assign) {
218 qemu_set_fd_handler(event_notifier_get_fd(notifier),
219 virtio_pci_host_notifier_read, NULL, vq);
220 } else {
221 qemu_set_fd_handler(event_notifier_get_fd(notifier),
222 NULL, NULL, NULL);
223 }
224 }
225
226 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
227 {
228 int n, r;
229
230 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
231 proxy->ioeventfd_disabled ||
232 proxy->ioeventfd_started) {
233 return;
234 }
235
236 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
237 if (!virtio_queue_get_num(proxy->vdev, n)) {
238 continue;
239 }
240
241 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
242 if (r < 0) {
243 goto assign_error;
244 }
245
246 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
247 }
248 proxy->ioeventfd_started = true;
249 return;
250
251 assign_error:
252 while (--n >= 0) {
253 if (!virtio_queue_get_num(proxy->vdev, n)) {
254 continue;
255 }
256
257 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
258 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
259 assert(r >= 0);
260 }
261 proxy->ioeventfd_started = false;
262 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
263 }
264
265 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
266 {
267 int r;
268 int n;
269
270 if (!proxy->ioeventfd_started) {
271 return;
272 }
273
274 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
275 if (!virtio_queue_get_num(proxy->vdev, n)) {
276 continue;
277 }
278
279 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
280 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
281 assert(r >= 0);
282 }
283 proxy->ioeventfd_started = false;
284 }
285
286 static void virtio_pci_reset(DeviceState *d)
287 {
288 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
289 virtio_pci_stop_ioeventfd(proxy);
290 virtio_reset(proxy->vdev);
291 msix_reset(&proxy->pci_dev);
292 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
293 }
294
295 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
296 {
297 VirtIOPCIProxy *proxy = opaque;
298 VirtIODevice *vdev = proxy->vdev;
299 target_phys_addr_t pa;
300
301 switch (addr) {
302 case VIRTIO_PCI_GUEST_FEATURES:
303 /* Guest does not negotiate properly? We have to assume nothing. */
304 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
305 if (vdev->bad_features)
306 val = proxy->host_features & vdev->bad_features(vdev);
307 else
308 val = 0;
309 }
310 if (vdev->set_features)
311 vdev->set_features(vdev, val);
312 vdev->guest_features = val;
313 break;
314 case VIRTIO_PCI_QUEUE_PFN:
315 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
316 if (pa == 0) {
317 virtio_pci_stop_ioeventfd(proxy);
318 virtio_reset(proxy->vdev);
319 msix_unuse_all_vectors(&proxy->pci_dev);
320 }
321 else
322 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
323 break;
324 case VIRTIO_PCI_QUEUE_SEL:
325 if (val < VIRTIO_PCI_QUEUE_MAX)
326 vdev->queue_sel = val;
327 break;
328 case VIRTIO_PCI_QUEUE_NOTIFY:
329 if (val < VIRTIO_PCI_QUEUE_MAX) {
330 virtio_queue_notify(vdev, val);
331 }
332 break;
333 case VIRTIO_PCI_STATUS:
334 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
335 virtio_pci_stop_ioeventfd(proxy);
336 }
337
338 virtio_set_status(vdev, val & 0xFF);
339
340 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
341 virtio_pci_start_ioeventfd(proxy);
342 }
343
344 if (vdev->status == 0) {
345 virtio_reset(proxy->vdev);
346 msix_unuse_all_vectors(&proxy->pci_dev);
347 }
348
349 /* Linux before 2.6.34 sets the device as OK without enabling
350 the PCI device bus master bit. In this case we need to disable
351 some safety checks. */
352 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
353 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
354 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
355 }
356 break;
357 case VIRTIO_MSI_CONFIG_VECTOR:
358 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
359 /* Make it possible for guest to discover an error took place. */
360 if (msix_vector_use(&proxy->pci_dev, val) < 0)
361 val = VIRTIO_NO_VECTOR;
362 vdev->config_vector = val;
363 break;
364 case VIRTIO_MSI_QUEUE_VECTOR:
365 msix_vector_unuse(&proxy->pci_dev,
366 virtio_queue_vector(vdev, vdev->queue_sel));
367 /* Make it possible for guest to discover an error took place. */
368 if (msix_vector_use(&proxy->pci_dev, val) < 0)
369 val = VIRTIO_NO_VECTOR;
370 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
371 break;
372 default:
373 error_report("%s: unexpected address 0x%x value 0x%x",
374 __func__, addr, val);
375 break;
376 }
377 }
378
379 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
380 {
381 VirtIODevice *vdev = proxy->vdev;
382 uint32_t ret = 0xFFFFFFFF;
383
384 switch (addr) {
385 case VIRTIO_PCI_HOST_FEATURES:
386 ret = proxy->host_features;
387 break;
388 case VIRTIO_PCI_GUEST_FEATURES:
389 ret = vdev->guest_features;
390 break;
391 case VIRTIO_PCI_QUEUE_PFN:
392 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
393 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
394 break;
395 case VIRTIO_PCI_QUEUE_NUM:
396 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
397 break;
398 case VIRTIO_PCI_QUEUE_SEL:
399 ret = vdev->queue_sel;
400 break;
401 case VIRTIO_PCI_STATUS:
402 ret = vdev->status;
403 break;
404 case VIRTIO_PCI_ISR:
405 /* reading from the ISR also clears it. */
406 ret = vdev->isr;
407 vdev->isr = 0;
408 qemu_set_irq(proxy->pci_dev.irq[0], 0);
409 break;
410 case VIRTIO_MSI_CONFIG_VECTOR:
411 ret = vdev->config_vector;
412 break;
413 case VIRTIO_MSI_QUEUE_VECTOR:
414 ret = virtio_queue_vector(vdev, vdev->queue_sel);
415 break;
416 default:
417 break;
418 }
419
420 return ret;
421 }
422
423 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
424 {
425 VirtIOPCIProxy *proxy = opaque;
426 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
427 addr -= proxy->addr;
428 if (addr < config)
429 return virtio_ioport_read(proxy, addr);
430 addr -= config;
431 return virtio_config_readb(proxy->vdev, addr);
432 }
433
434 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
435 {
436 VirtIOPCIProxy *proxy = opaque;
437 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
438 addr -= proxy->addr;
439 if (addr < config)
440 return virtio_ioport_read(proxy, addr);
441 addr -= config;
442 return virtio_config_readw(proxy->vdev, addr);
443 }
444
445 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
446 {
447 VirtIOPCIProxy *proxy = opaque;
448 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
449 addr -= proxy->addr;
450 if (addr < config)
451 return virtio_ioport_read(proxy, addr);
452 addr -= config;
453 return virtio_config_readl(proxy->vdev, addr);
454 }
455
456 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
457 {
458 VirtIOPCIProxy *proxy = opaque;
459 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
460 addr -= proxy->addr;
461 if (addr < config) {
462 virtio_ioport_write(proxy, addr, val);
463 return;
464 }
465 addr -= config;
466 virtio_config_writeb(proxy->vdev, addr, val);
467 }
468
469 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
470 {
471 VirtIOPCIProxy *proxy = opaque;
472 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
473 addr -= proxy->addr;
474 if (addr < config) {
475 virtio_ioport_write(proxy, addr, val);
476 return;
477 }
478 addr -= config;
479 virtio_config_writew(proxy->vdev, addr, val);
480 }
481
482 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
483 {
484 VirtIOPCIProxy *proxy = opaque;
485 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
486 addr -= proxy->addr;
487 if (addr < config) {
488 virtio_ioport_write(proxy, addr, val);
489 return;
490 }
491 addr -= config;
492 virtio_config_writel(proxy->vdev, addr, val);
493 }
494
495 static void virtio_map(PCIDevice *pci_dev, int region_num,
496 pcibus_t addr, pcibus_t size, int type)
497 {
498 VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
499 VirtIODevice *vdev = proxy->vdev;
500 unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
501
502 proxy->addr = addr;
503
504 register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
505 register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
506 register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
507 register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
508 register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
509 register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
510
511 if (vdev->config_len)
512 vdev->get_config(vdev, vdev->config);
513 }
514
515 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
516 uint32_t val, int len)
517 {
518 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
519
520 pci_default_write_config(pci_dev, address, val, len);
521
522 if (range_covers_byte(address, len, PCI_COMMAND) &&
523 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
524 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
525 virtio_pci_stop_ioeventfd(proxy);
526 virtio_set_status(proxy->vdev,
527 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
528 }
529
530 msix_write_config(pci_dev, address, val, len);
531 }
532
533 static unsigned virtio_pci_get_features(void *opaque)
534 {
535 VirtIOPCIProxy *proxy = opaque;
536 return proxy->host_features;
537 }
538
539 static void virtio_pci_guest_notifier_read(void *opaque)
540 {
541 VirtQueue *vq = opaque;
542 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
543 if (event_notifier_test_and_clear(n)) {
544 virtio_irq(vq);
545 }
546 }
547
548 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
549 {
550 VirtIOPCIProxy *proxy = opaque;
551 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
552 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
553
554 if (assign) {
555 int r = event_notifier_init(notifier, 0);
556 if (r < 0) {
557 return r;
558 }
559 qemu_set_fd_handler(event_notifier_get_fd(notifier),
560 virtio_pci_guest_notifier_read, NULL, vq);
561 } else {
562 qemu_set_fd_handler(event_notifier_get_fd(notifier),
563 NULL, NULL, NULL);
564 event_notifier_cleanup(notifier);
565 }
566
567 return 0;
568 }
569
570 static bool virtio_pci_query_guest_notifiers(void *opaque)
571 {
572 VirtIOPCIProxy *proxy = opaque;
573 return msix_enabled(&proxy->pci_dev);
574 }
575
576 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
577 {
578 VirtIOPCIProxy *proxy = opaque;
579 VirtIODevice *vdev = proxy->vdev;
580 int r, n;
581
582 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
583 if (!virtio_queue_get_num(vdev, n)) {
584 break;
585 }
586
587 r = virtio_pci_set_guest_notifier(opaque, n, assign);
588 if (r < 0) {
589 goto assign_error;
590 }
591 }
592
593 return 0;
594
595 assign_error:
596 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
597 while (--n >= 0) {
598 virtio_pci_set_guest_notifier(opaque, n, !assign);
599 }
600 return r;
601 }
602
603 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
604 {
605 VirtIOPCIProxy *proxy = opaque;
606
607 /* Stop using ioeventfd for virtqueue kick if the device starts using host
608 * notifiers. This makes it easy to avoid stepping on each others' toes.
609 */
610 proxy->ioeventfd_disabled = assign;
611 if (assign) {
612 virtio_pci_stop_ioeventfd(proxy);
613 }
614 /* We don't need to start here: it's not needed because backend
615 * currently only stops on status change away from ok,
616 * reset, vmstop and such. If we do add code to start here,
617 * need to check vmstate, device state etc. */
618 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
619 }
620
621 static void virtio_pci_vmstate_change(void *opaque, bool running)
622 {
623 VirtIOPCIProxy *proxy = opaque;
624
625 if (running) {
626 /* Try to find out if the guest has bus master disabled, but is
627 in ready state. Then we have a buggy guest OS. */
628 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
629 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
630 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
631 }
632 virtio_pci_start_ioeventfd(proxy);
633 } else {
634 virtio_pci_stop_ioeventfd(proxy);
635 }
636 }
637
638 static const VirtIOBindings virtio_pci_bindings = {
639 .notify = virtio_pci_notify,
640 .save_config = virtio_pci_save_config,
641 .load_config = virtio_pci_load_config,
642 .save_queue = virtio_pci_save_queue,
643 .load_queue = virtio_pci_load_queue,
644 .get_features = virtio_pci_get_features,
645 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
646 .set_host_notifier = virtio_pci_set_host_notifier,
647 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
648 .vmstate_change = virtio_pci_vmstate_change,
649 };
650
651 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
652 {
653 uint8_t *config;
654 uint32_t size;
655
656 proxy->vdev = vdev;
657
658 config = proxy->pci_dev.config;
659
660 if (proxy->class_code) {
661 pci_config_set_class(config, proxy->class_code);
662 }
663 pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
664 pci_set_word(config + 0x2e, vdev->device_id);
665 config[0x3d] = 1;
666
667 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
668 pci_register_bar(&proxy->pci_dev, 1,
669 msix_bar_size(&proxy->pci_dev),
670 PCI_BASE_ADDRESS_SPACE_MEMORY,
671 msix_mmio_map);
672 } else
673 vdev->nvectors = 0;
674
675 proxy->pci_dev.config_write = virtio_write_config;
676
677 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
678 if (size & (size-1))
679 size = 1 << qemu_fls(size);
680
681 pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
682 virtio_map);
683
684 if (!kvm_has_many_ioeventfds()) {
685 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
686 }
687
688 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
689 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
690 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
691 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
692 }
693
694 static int virtio_blk_init_pci(PCIDevice *pci_dev)
695 {
696 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
697 VirtIODevice *vdev;
698
699 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
700 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
701 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
702
703 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
704 &proxy->block_serial);
705 if (!vdev) {
706 return -1;
707 }
708 vdev->nvectors = proxy->nvectors;
709 virtio_init_pci(proxy, vdev);
710 /* make the actual value visible */
711 proxy->nvectors = vdev->nvectors;
712 return 0;
713 }
714
715 static int virtio_exit_pci(PCIDevice *pci_dev)
716 {
717 return msix_uninit(pci_dev);
718 }
719
720 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
721 {
722 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
723
724 virtio_pci_stop_ioeventfd(proxy);
725 virtio_blk_exit(proxy->vdev);
726 blockdev_mark_auto_del(proxy->block.bs);
727 return virtio_exit_pci(pci_dev);
728 }
729
730 static int virtio_serial_init_pci(PCIDevice *pci_dev)
731 {
732 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
733 VirtIODevice *vdev;
734
735 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
736 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
737 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
738 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
739
740 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
741 if (!vdev) {
742 return -1;
743 }
744 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
745 ? proxy->serial.max_virtserial_ports + 1
746 : proxy->nvectors;
747 virtio_init_pci(proxy, vdev);
748 proxy->nvectors = vdev->nvectors;
749 return 0;
750 }
751
752 static int virtio_serial_exit_pci(PCIDevice *pci_dev)
753 {
754 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
755
756 virtio_pci_stop_ioeventfd(proxy);
757 virtio_serial_exit(proxy->vdev);
758 return virtio_exit_pci(pci_dev);
759 }
760
761 static int virtio_net_init_pci(PCIDevice *pci_dev)
762 {
763 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
764 VirtIODevice *vdev;
765
766 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
767
768 vdev->nvectors = proxy->nvectors;
769 virtio_init_pci(proxy, vdev);
770
771 /* make the actual value visible */
772 proxy->nvectors = vdev->nvectors;
773 return 0;
774 }
775
776 static int virtio_net_exit_pci(PCIDevice *pci_dev)
777 {
778 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
779
780 virtio_pci_stop_ioeventfd(proxy);
781 virtio_net_exit(proxy->vdev);
782 return virtio_exit_pci(pci_dev);
783 }
784
785 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
786 {
787 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
788 VirtIODevice *vdev;
789
790 vdev = virtio_balloon_init(&pci_dev->qdev);
791 if (!vdev) {
792 return -1;
793 }
794 virtio_init_pci(proxy, vdev);
795 return 0;
796 }
797
798 static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
799 {
800 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
801
802 virtio_pci_stop_ioeventfd(proxy);
803 virtio_balloon_exit(proxy->vdev);
804 return virtio_exit_pci(pci_dev);
805 }
806
807 static PCIDeviceInfo virtio_info[] = {
808 {
809 .qdev.name = "virtio-blk-pci",
810 .qdev.alias = "virtio-blk",
811 .qdev.size = sizeof(VirtIOPCIProxy),
812 .init = virtio_blk_init_pci,
813 .exit = virtio_blk_exit_pci,
814 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
815 .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
816 .revision = VIRTIO_PCI_ABI_VERSION,
817 .class_id = PCI_CLASS_STORAGE_SCSI,
818 .qdev.props = (Property[]) {
819 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
820 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
821 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
822 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
823 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
824 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
825 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
826 DEFINE_PROP_END_OF_LIST(),
827 },
828 .qdev.reset = virtio_pci_reset,
829 },{
830 .qdev.name = "virtio-net-pci",
831 .qdev.alias = "virtio-net",
832 .qdev.size = sizeof(VirtIOPCIProxy),
833 .init = virtio_net_init_pci,
834 .exit = virtio_net_exit_pci,
835 .romfile = "pxe-virtio.rom",
836 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
837 .device_id = PCI_DEVICE_ID_VIRTIO_NET,
838 .revision = VIRTIO_PCI_ABI_VERSION,
839 .class_id = PCI_CLASS_NETWORK_ETHERNET,
840 .qdev.props = (Property[]) {
841 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
842 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
843 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
844 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
845 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
846 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
847 net.txtimer, TX_TIMER_INTERVAL),
848 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
849 net.txburst, TX_BURST),
850 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
851 DEFINE_PROP_END_OF_LIST(),
852 },
853 .qdev.reset = virtio_pci_reset,
854 },{
855 .qdev.name = "virtio-serial-pci",
856 .qdev.alias = "virtio-serial",
857 .qdev.size = sizeof(VirtIOPCIProxy),
858 .init = virtio_serial_init_pci,
859 .exit = virtio_serial_exit_pci,
860 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
861 .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
862 .revision = VIRTIO_PCI_ABI_VERSION,
863 .class_id = PCI_CLASS_COMMUNICATION_OTHER,
864 .qdev.props = (Property[]) {
865 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
866 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
867 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
868 DEV_NVECTORS_UNSPECIFIED),
869 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
870 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
871 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
872 serial.max_virtserial_ports, 31),
873 DEFINE_PROP_END_OF_LIST(),
874 },
875 .qdev.reset = virtio_pci_reset,
876 },{
877 .qdev.name = "virtio-balloon-pci",
878 .qdev.alias = "virtio-balloon",
879 .qdev.size = sizeof(VirtIOPCIProxy),
880 .init = virtio_balloon_init_pci,
881 .exit = virtio_balloon_exit_pci,
882 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
883 .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
884 .revision = VIRTIO_PCI_ABI_VERSION,
885 .class_id = PCI_CLASS_MEMORY_RAM,
886 .qdev.props = (Property[]) {
887 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
888 DEFINE_PROP_END_OF_LIST(),
889 },
890 .qdev.reset = virtio_pci_reset,
891 },{
892 /* end of list */
893 }
894 };
895
896 static void virtio_pci_register_devices(void)
897 {
898 pci_qdev_register_many(virtio_info);
899 }
900
901 device_init(virtio_pci_register_devices)