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