]> git.proxmox.com Git - qemu.git/blame - hw/virtio-pci.c
virtio-pci: convert to memory API
[qemu.git] / hw / virtio-pci.c
CommitLineData
53c25cea
PB
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"
8172539d
MT
19#include "virtio-blk.h"
20#include "virtio-net.h"
6b331efb 21#include "virtio-serial.h"
53c25cea 22#include "pci.h"
2f792016 23#include "qemu-error.h"
aba800a3 24#include "msix.h"
a1e0fea5 25#include "net.h"
97b15621 26#include "loader.h"
ade80dc8 27#include "kvm.h"
2446333c 28#include "blockdev.h"
9fe1ebeb 29#include "virtio-pci.h"
1129714f 30#include "range.h"
53c25cea
PB
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
aba800a3
MT
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)
53c25cea 78
53c25cea
PB
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
3dbca8e6
SH
83/* Flags track per-device state like workarounds for quirks in older guests. */
84#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
c81131db 85
25db9ebe
SH
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
53c25cea
PB
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
53c25cea
PB
97/* virtio device */
98
7055e687 99static void virtio_pci_notify(void *opaque, uint16_t vector)
53c25cea
PB
100{
101 VirtIOPCIProxy *proxy = opaque;
aba800a3
MT
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);
53c25cea
PB
106}
107
ff24bd58
MT
108static 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
117static 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
124static 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);
e6da7680 129 if (ret) {
ff24bd58 130 return ret;
e6da7680 131 }
ff24bd58 132 msix_load(&proxy->pci_dev, f);
e6da7680 133 if (msix_present(&proxy->pci_dev)) {
ff24bd58 134 qemu_get_be16s(f, &proxy->vdev->config_vector);
e6da7680
MT
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 }
ff24bd58
MT
141 return 0;
142}
143
144static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
145{
146 VirtIOPCIProxy *proxy = opaque;
147 uint16_t vector;
e6da7680
MT
148 if (msix_present(&proxy->pci_dev)) {
149 qemu_get_be16s(f, &vector);
150 } else {
151 vector = VIRTIO_NO_VECTOR;
152 }
ff24bd58 153 virtio_queue_set_vector(proxy->vdev, n, vector);
e6da7680
MT
154 if (vector != VIRTIO_NO_VECTOR) {
155 return msix_vector_use(&proxy->pci_dev, vector);
156 }
ff24bd58
MT
157 return 0;
158}
159
25db9ebe
SH
160static 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);
da146d0a
AK
165 int r = 0;
166
25db9ebe
SH
167 if (assign) {
168 r = event_notifier_init(notifier, 1);
169 if (r < 0) {
b36e3914
MT
170 error_report("%s: unable to init event notifier: %d",
171 __func__, r);
25db9ebe
SH
172 return r;
173 }
da146d0a
AK
174 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
175 true, n, event_notifier_get_fd(notifier));
25db9ebe 176 } else {
da146d0a
AK
177 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
178 true, n, event_notifier_get_fd(notifier));
25db9ebe
SH
179 /* Handle the race condition where the guest kicked and we deassigned
180 * before we got around to handling the kick.
181 */
182 if (event_notifier_test_and_clear(notifier)) {
183 virtio_queue_notify_vq(vq);
184 }
185
186 event_notifier_cleanup(notifier);
187 }
188 return r;
189}
190
191static void virtio_pci_host_notifier_read(void *opaque)
192{
193 VirtQueue *vq = opaque;
194 EventNotifier *n = virtio_queue_get_host_notifier(vq);
195 if (event_notifier_test_and_clear(n)) {
196 virtio_queue_notify_vq(vq);
197 }
198}
199
200static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
201 int n, bool assign)
202{
203 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
204 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
205 if (assign) {
206 qemu_set_fd_handler(event_notifier_get_fd(notifier),
207 virtio_pci_host_notifier_read, NULL, vq);
208 } else {
209 qemu_set_fd_handler(event_notifier_get_fd(notifier),
210 NULL, NULL, NULL);
211 }
212}
213
b36e3914 214static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe
SH
215{
216 int n, r;
217
218 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
219 proxy->ioeventfd_disabled ||
220 proxy->ioeventfd_started) {
b36e3914 221 return;
25db9ebe
SH
222 }
223
224 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
225 if (!virtio_queue_get_num(proxy->vdev, n)) {
226 continue;
227 }
228
229 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
230 if (r < 0) {
231 goto assign_error;
232 }
233
234 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
235 }
236 proxy->ioeventfd_started = true;
b36e3914 237 return;
25db9ebe
SH
238
239assign_error:
240 while (--n >= 0) {
241 if (!virtio_queue_get_num(proxy->vdev, n)) {
242 continue;
243 }
244
245 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
b36e3914
MT
246 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
247 assert(r >= 0);
25db9ebe
SH
248 }
249 proxy->ioeventfd_started = false;
b36e3914 250 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
25db9ebe
SH
251}
252
b36e3914 253static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe 254{
b36e3914 255 int r;
25db9ebe
SH
256 int n;
257
258 if (!proxy->ioeventfd_started) {
b36e3914 259 return;
25db9ebe
SH
260 }
261
262 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
263 if (!virtio_queue_get_num(proxy->vdev, n)) {
264 continue;
265 }
266
267 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
b36e3914
MT
268 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
269 assert(r >= 0);
25db9ebe
SH
270 }
271 proxy->ioeventfd_started = false;
25db9ebe
SH
272}
273
e489030d 274static void virtio_pci_reset(DeviceState *d)
7055e687 275{
e489030d 276 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
25db9ebe 277 virtio_pci_stop_ioeventfd(proxy);
7055e687 278 virtio_reset(proxy->vdev);
aba800a3 279 msix_reset(&proxy->pci_dev);
25db9ebe 280 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
7055e687
MT
281}
282
53c25cea
PB
283static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
284{
285 VirtIOPCIProxy *proxy = opaque;
286 VirtIODevice *vdev = proxy->vdev;
c227f099 287 target_phys_addr_t pa;
53c25cea 288
53c25cea
PB
289 switch (addr) {
290 case VIRTIO_PCI_GUEST_FEATURES:
291 /* Guest does not negotiate properly? We have to assume nothing. */
292 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
293 if (vdev->bad_features)
8172539d 294 val = proxy->host_features & vdev->bad_features(vdev);
53c25cea
PB
295 else
296 val = 0;
297 }
298 if (vdev->set_features)
299 vdev->set_features(vdev, val);
704a76fc 300 vdev->guest_features = val;
53c25cea
PB
301 break;
302 case VIRTIO_PCI_QUEUE_PFN:
c227f099 303 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
1b8e9b27 304 if (pa == 0) {
25db9ebe 305 virtio_pci_stop_ioeventfd(proxy);
1b8e9b27
MT
306 virtio_reset(proxy->vdev);
307 msix_unuse_all_vectors(&proxy->pci_dev);
308 }
7055e687
MT
309 else
310 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
53c25cea
PB
311 break;
312 case VIRTIO_PCI_QUEUE_SEL:
313 if (val < VIRTIO_PCI_QUEUE_MAX)
314 vdev->queue_sel = val;
315 break;
316 case VIRTIO_PCI_QUEUE_NOTIFY:
7157e2e2
SH
317 if (val < VIRTIO_PCI_QUEUE_MAX) {
318 virtio_queue_notify(vdev, val);
319 }
53c25cea
PB
320 break;
321 case VIRTIO_PCI_STATUS:
25db9ebe
SH
322 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
323 virtio_pci_stop_ioeventfd(proxy);
324 }
325
3e607cb5 326 virtio_set_status(vdev, val & 0xFF);
25db9ebe
SH
327
328 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
329 virtio_pci_start_ioeventfd(proxy);
330 }
331
1b8e9b27
MT
332 if (vdev->status == 0) {
333 virtio_reset(proxy->vdev);
334 msix_unuse_all_vectors(&proxy->pci_dev);
335 }
c81131db
AG
336
337 /* Linux before 2.6.34 sets the device as OK without enabling
338 the PCI device bus master bit. In this case we need to disable
339 some safety checks. */
340 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
341 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
3dbca8e6 342 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
c81131db 343 }
53c25cea 344 break;
aba800a3
MT
345 case VIRTIO_MSI_CONFIG_VECTOR:
346 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
347 /* Make it possible for guest to discover an error took place. */
348 if (msix_vector_use(&proxy->pci_dev, val) < 0)
349 val = VIRTIO_NO_VECTOR;
350 vdev->config_vector = val;
351 break;
352 case VIRTIO_MSI_QUEUE_VECTOR:
353 msix_vector_unuse(&proxy->pci_dev,
354 virtio_queue_vector(vdev, vdev->queue_sel));
355 /* Make it possible for guest to discover an error took place. */
356 if (msix_vector_use(&proxy->pci_dev, val) < 0)
357 val = VIRTIO_NO_VECTOR;
358 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
359 break;
360 default:
4e02d460
SH
361 error_report("%s: unexpected address 0x%x value 0x%x",
362 __func__, addr, val);
aba800a3 363 break;
53c25cea
PB
364 }
365}
366
aba800a3 367static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
53c25cea 368{
53c25cea
PB
369 VirtIODevice *vdev = proxy->vdev;
370 uint32_t ret = 0xFFFFFFFF;
371
53c25cea
PB
372 switch (addr) {
373 case VIRTIO_PCI_HOST_FEATURES:
8172539d 374 ret = proxy->host_features;
53c25cea
PB
375 break;
376 case VIRTIO_PCI_GUEST_FEATURES:
704a76fc 377 ret = vdev->guest_features;
53c25cea
PB
378 break;
379 case VIRTIO_PCI_QUEUE_PFN:
380 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
381 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
382 break;
383 case VIRTIO_PCI_QUEUE_NUM:
384 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
385 break;
386 case VIRTIO_PCI_QUEUE_SEL:
387 ret = vdev->queue_sel;
388 break;
389 case VIRTIO_PCI_STATUS:
390 ret = vdev->status;
391 break;
392 case VIRTIO_PCI_ISR:
393 /* reading from the ISR also clears it. */
394 ret = vdev->isr;
395 vdev->isr = 0;
7055e687 396 qemu_set_irq(proxy->pci_dev.irq[0], 0);
53c25cea 397 break;
aba800a3
MT
398 case VIRTIO_MSI_CONFIG_VECTOR:
399 ret = vdev->config_vector;
400 break;
401 case VIRTIO_MSI_QUEUE_VECTOR:
402 ret = virtio_queue_vector(vdev, vdev->queue_sel);
403 break;
53c25cea
PB
404 default:
405 break;
406 }
407
408 return ret;
409}
410
411static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
412{
413 VirtIOPCIProxy *proxy = opaque;
aba800a3 414 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
415 if (addr < config)
416 return virtio_ioport_read(proxy, addr);
417 addr -= config;
53c25cea
PB
418 return virtio_config_readb(proxy->vdev, addr);
419}
420
421static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
422{
423 VirtIOPCIProxy *proxy = opaque;
aba800a3 424 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
425 if (addr < config)
426 return virtio_ioport_read(proxy, addr);
427 addr -= config;
53c25cea
PB
428 return virtio_config_readw(proxy->vdev, addr);
429}
430
431static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
432{
433 VirtIOPCIProxy *proxy = opaque;
aba800a3 434 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
435 if (addr < config)
436 return virtio_ioport_read(proxy, addr);
437 addr -= config;
53c25cea
PB
438 return virtio_config_readl(proxy->vdev, addr);
439}
440
441static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
442{
443 VirtIOPCIProxy *proxy = opaque;
aba800a3 444 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
445 if (addr < config) {
446 virtio_ioport_write(proxy, addr, val);
447 return;
448 }
449 addr -= config;
53c25cea
PB
450 virtio_config_writeb(proxy->vdev, addr, val);
451}
452
453static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
454{
455 VirtIOPCIProxy *proxy = opaque;
aba800a3 456 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
457 if (addr < config) {
458 virtio_ioport_write(proxy, addr, val);
459 return;
460 }
461 addr -= config;
53c25cea
PB
462 virtio_config_writew(proxy->vdev, addr, val);
463}
464
465static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
466{
467 VirtIOPCIProxy *proxy = opaque;
aba800a3 468 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
469 if (addr < config) {
470 virtio_ioport_write(proxy, addr, val);
471 return;
472 }
473 addr -= config;
53c25cea
PB
474 virtio_config_writel(proxy->vdev, addr, val);
475}
476
da146d0a
AK
477const MemoryRegionPortio virtio_portio[] = {
478 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
479 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
480 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
481 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
482 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
483 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
484 PORTIO_END_OF_LIST()
485};
53c25cea 486
da146d0a
AK
487static const MemoryRegionOps virtio_pci_config_ops = {
488 .old_portio = virtio_portio,
489 .endianness = DEVICE_LITTLE_ENDIAN,
490};
aba800a3
MT
491
492static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
493 uint32_t val, int len)
494{
ed757e14
YV
495 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
496
1129714f
MT
497 pci_default_write_config(pci_dev, address, val, len);
498
499 if (range_covers_byte(address, len, PCI_COMMAND) &&
500 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
501 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
502 virtio_pci_stop_ioeventfd(proxy);
503 virtio_set_status(proxy->vdev,
504 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
ed757e14
YV
505 }
506
85352471 507 msix_write_config(pci_dev, address, val, len);
53c25cea
PB
508}
509
6d74ca5a
MT
510static unsigned virtio_pci_get_features(void *opaque)
511{
8172539d
MT
512 VirtIOPCIProxy *proxy = opaque;
513 return proxy->host_features;
6d74ca5a
MT
514}
515
ade80dc8
MT
516static void virtio_pci_guest_notifier_read(void *opaque)
517{
518 VirtQueue *vq = opaque;
519 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
520 if (event_notifier_test_and_clear(n)) {
521 virtio_irq(vq);
522 }
523}
524
525static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
526{
527 VirtIOPCIProxy *proxy = opaque;
528 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
529 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
530
531 if (assign) {
532 int r = event_notifier_init(notifier, 0);
533 if (r < 0) {
534 return r;
535 }
536 qemu_set_fd_handler(event_notifier_get_fd(notifier),
537 virtio_pci_guest_notifier_read, NULL, vq);
538 } else {
539 qemu_set_fd_handler(event_notifier_get_fd(notifier),
540 NULL, NULL, NULL);
541 event_notifier_cleanup(notifier);
542 }
543
544 return 0;
545}
546
5430a28f 547static bool virtio_pci_query_guest_notifiers(void *opaque)
548{
549 VirtIOPCIProxy *proxy = opaque;
550 return msix_enabled(&proxy->pci_dev);
551}
552
54dd9321
MT
553static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
554{
555 VirtIOPCIProxy *proxy = opaque;
556 VirtIODevice *vdev = proxy->vdev;
557 int r, n;
558
559 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
560 if (!virtio_queue_get_num(vdev, n)) {
561 break;
562 }
563
564 r = virtio_pci_set_guest_notifier(opaque, n, assign);
565 if (r < 0) {
566 goto assign_error;
567 }
568 }
569
570 return 0;
571
572assign_error:
573 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
574 while (--n >= 0) {
575 virtio_pci_set_guest_notifier(opaque, n, !assign);
576 }
577 return r;
578}
579
ade80dc8
MT
580static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
581{
582 VirtIOPCIProxy *proxy = opaque;
25db9ebe
SH
583
584 /* Stop using ioeventfd for virtqueue kick if the device starts using host
585 * notifiers. This makes it easy to avoid stepping on each others' toes.
586 */
587 proxy->ioeventfd_disabled = assign;
ade80dc8 588 if (assign) {
25db9ebe
SH
589 virtio_pci_stop_ioeventfd(proxy);
590 }
591 /* We don't need to start here: it's not needed because backend
592 * currently only stops on status change away from ok,
593 * reset, vmstop and such. If we do add code to start here,
594 * need to check vmstate, device state etc. */
595 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
596}
597
598static void virtio_pci_vmstate_change(void *opaque, bool running)
599{
600 VirtIOPCIProxy *proxy = opaque;
601
602 if (running) {
89c473fd
MT
603 /* Try to find out if the guest has bus master disabled, but is
604 in ready state. Then we have a buggy guest OS. */
605 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
606 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
607 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
608 }
25db9ebe 609 virtio_pci_start_ioeventfd(proxy);
ade80dc8 610 } else {
25db9ebe 611 virtio_pci_stop_ioeventfd(proxy);
ade80dc8 612 }
ade80dc8
MT
613}
614
53c25cea 615static const VirtIOBindings virtio_pci_bindings = {
ff24bd58
MT
616 .notify = virtio_pci_notify,
617 .save_config = virtio_pci_save_config,
618 .load_config = virtio_pci_load_config,
619 .save_queue = virtio_pci_save_queue,
620 .load_queue = virtio_pci_load_queue,
6d74ca5a 621 .get_features = virtio_pci_get_features,
5430a28f 622 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
ade80dc8 623 .set_host_notifier = virtio_pci_set_host_notifier,
54dd9321 624 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
25db9ebe 625 .vmstate_change = virtio_pci_vmstate_change,
53c25cea
PB
626};
627
befeac45 628void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
53c25cea
PB
629{
630 uint8_t *config;
631 uint32_t size;
632
633 proxy->vdev = vdev;
634
635 config = proxy->pci_dev.config;
53c25cea 636
e75ccf2c
IY
637 if (proxy->class_code) {
638 pci_config_set_class(config, proxy->class_code);
639 }
640 pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
641 pci_set_word(config + 0x2e, vdev->device_id);
53c25cea
PB
642 config[0x3d] = 1;
643
5a1fc5e8 644 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
aba800a3
MT
645 pci_register_bar(&proxy->pci_dev, 1,
646 msix_bar_size(&proxy->pci_dev),
0392a017 647 PCI_BASE_ADDRESS_SPACE_MEMORY,
aba800a3 648 msix_mmio_map);
aba800a3
MT
649 } else
650 vdev->nvectors = 0;
651
ed757e14
YV
652 proxy->pci_dev.config_write = virtio_write_config;
653
aba800a3 654 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
53c25cea
PB
655 if (size & (size-1))
656 size = 1 << qemu_fls(size);
657
da146d0a
AK
658 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
659 "virtio-pci", size);
660 pci_register_bar_region(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
661 &proxy->bar);
53c25cea 662
25db9ebe
SH
663 if (!kvm_has_many_ioeventfds()) {
664 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
665 }
666
53c25cea 667 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
8172539d
MT
668 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
669 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
670 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
53c25cea
PB
671}
672
81a322d4 673static int virtio_blk_init_pci(PCIDevice *pci_dev)
53c25cea
PB
674{
675 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
676 VirtIODevice *vdev;
677
ab73ff29
GH
678 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
679 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
680 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
53c25cea 681
a8686a9b
MA
682 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
683 &proxy->block_serial);
ac0c14d7
MA
684 if (!vdev) {
685 return -1;
686 }
177539e0 687 vdev->nvectors = proxy->nvectors;
e75ccf2c 688 virtio_init_pci(proxy, vdev);
177539e0
GH
689 /* make the actual value visible */
690 proxy->nvectors = vdev->nvectors;
81a322d4 691 return 0;
21d58b57
MM
692}
693
0f457d91
MT
694static int virtio_exit_pci(PCIDevice *pci_dev)
695{
da146d0a
AK
696 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
697
698 memory_region_destroy(&proxy->bar);
0f457d91
MT
699 return msix_uninit(pci_dev);
700}
701
56a14938
GH
702static int virtio_blk_exit_pci(PCIDevice *pci_dev)
703{
704 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
705
25db9ebe 706 virtio_pci_stop_ioeventfd(proxy);
9d0d3138 707 virtio_blk_exit(proxy->vdev);
f8b6cc00 708 blockdev_mark_auto_del(proxy->block.bs);
0f457d91 709 return virtio_exit_pci(pci_dev);
56a14938
GH
710}
711
98b19252 712static int virtio_serial_init_pci(PCIDevice *pci_dev)
21d58b57 713{
d6beee99 714 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
85c2c735
MM
715 VirtIODevice *vdev;
716
d6beee99
GH
717 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
718 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
719 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
720 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
721
6b331efb 722 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
25fe3654
AS
723 if (!vdev) {
724 return -1;
725 }
573fb60c 726 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
6b331efb 727 ? proxy->serial.max_virtserial_ports + 1
573fb60c 728 : proxy->nvectors;
e75ccf2c 729 virtio_init_pci(proxy, vdev);
a1829205 730 proxy->nvectors = vdev->nvectors;
81a322d4 731 return 0;
53c25cea
PB
732}
733
8b53a865
AS
734static int virtio_serial_exit_pci(PCIDevice *pci_dev)
735{
736 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
737
32059220 738 virtio_pci_stop_ioeventfd(proxy);
8b53a865
AS
739 virtio_serial_exit(proxy->vdev);
740 return virtio_exit_pci(pci_dev);
741}
742
81a322d4 743static int virtio_net_init_pci(PCIDevice *pci_dev)
53c25cea
PB
744{
745 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
746 VirtIODevice *vdev;
747
f0c07c7c 748 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
a1e0fea5 749
97b15621 750 vdev->nvectors = proxy->nvectors;
e75ccf2c 751 virtio_init_pci(proxy, vdev);
a1e0fea5
GH
752
753 /* make the actual value visible */
754 proxy->nvectors = vdev->nvectors;
81a322d4 755 return 0;
53c25cea
PB
756}
757
97b15621
GH
758static int virtio_net_exit_pci(PCIDevice *pci_dev)
759{
760 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
761
25db9ebe 762 virtio_pci_stop_ioeventfd(proxy);
97b15621
GH
763 virtio_net_exit(proxy->vdev);
764 return virtio_exit_pci(pci_dev);
765}
766
81a322d4 767static int virtio_balloon_init_pci(PCIDevice *pci_dev)
53c25cea
PB
768{
769 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
770 VirtIODevice *vdev;
771
772 vdev = virtio_balloon_init(&pci_dev->qdev);
f76f6655
AS
773 if (!vdev) {
774 return -1;
775 }
e75ccf2c 776 virtio_init_pci(proxy, vdev);
81a322d4 777 return 0;
53c25cea
PB
778}
779
855d7e25
AS
780static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
781{
782 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
783
784 virtio_pci_stop_ioeventfd(proxy);
785 virtio_balloon_exit(proxy->vdev);
786 return virtio_exit_pci(pci_dev);
787}
788
0aab0d3a
GH
789static PCIDeviceInfo virtio_info[] = {
790 {
791 .qdev.name = "virtio-blk-pci",
779206de 792 .qdev.alias = "virtio-blk",
0aab0d3a
GH
793 .qdev.size = sizeof(VirtIOPCIProxy),
794 .init = virtio_blk_init_pci,
56a14938 795 .exit = virtio_blk_exit_pci,
e75ccf2c
IY
796 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
797 .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
798 .revision = VIRTIO_PCI_ABI_VERSION,
799 .class_id = PCI_CLASS_STORAGE_SCSI,
ab73ff29 800 .qdev.props = (Property[]) {
72c61d0b 801 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
428c149b 802 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
a8686a9b 803 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
25db9ebe
SH
804 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
805 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
177539e0 806 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
8172539d 807 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
72c61d0b 808 DEFINE_PROP_END_OF_LIST(),
ab73ff29 809 },
e489030d 810 .qdev.reset = virtio_pci_reset,
0aab0d3a 811 },{
a1e0fea5 812 .qdev.name = "virtio-net-pci",
29f82b37 813 .qdev.alias = "virtio-net",
a1e0fea5
GH
814 .qdev.size = sizeof(VirtIOPCIProxy),
815 .init = virtio_net_init_pci,
97b15621 816 .exit = virtio_net_exit_pci,
5ee8ad71 817 .romfile = "pxe-virtio.rom",
e75ccf2c
IY
818 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
819 .device_id = PCI_DEVICE_ID_VIRTIO_NET,
820 .revision = VIRTIO_PCI_ABI_VERSION,
821 .class_id = PCI_CLASS_NETWORK_ETHERNET,
a1e0fea5 822 .qdev.props = (Property[]) {
25db9ebe
SH
823 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
824 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
97b15621 825 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
8172539d 826 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
97b15621 827 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
f0c07c7c
AW
828 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
829 net.txtimer, TX_TIMER_INTERVAL),
e3f30488
AW
830 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
831 net.txburst, TX_BURST),
a697a334 832 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
72c61d0b 833 DEFINE_PROP_END_OF_LIST(),
a1e0fea5 834 },
e489030d 835 .qdev.reset = virtio_pci_reset,
0aab0d3a 836 },{
98b19252
AS
837 .qdev.name = "virtio-serial-pci",
838 .qdev.alias = "virtio-serial",
0aab0d3a 839 .qdev.size = sizeof(VirtIOPCIProxy),
98b19252 840 .init = virtio_serial_init_pci,
8b53a865 841 .exit = virtio_serial_exit_pci,
e75ccf2c
IY
842 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
843 .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
844 .revision = VIRTIO_PCI_ABI_VERSION,
845 .class_id = PCI_CLASS_COMMUNICATION_OTHER,
d6beee99 846 .qdev.props = (Property[]) {
32059220
AS
847 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
848 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
573fb60c
AS
849 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
850 DEV_NVECTORS_UNSPECIFIED),
72c61d0b 851 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
8172539d 852 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
6b331efb
AS
853 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
854 serial.max_virtserial_ports, 31),
72c61d0b 855 DEFINE_PROP_END_OF_LIST(),
d6beee99 856 },
e489030d 857 .qdev.reset = virtio_pci_reset,
0aab0d3a
GH
858 },{
859 .qdev.name = "virtio-balloon-pci",
29f82b37 860 .qdev.alias = "virtio-balloon",
0aab0d3a
GH
861 .qdev.size = sizeof(VirtIOPCIProxy),
862 .init = virtio_balloon_init_pci,
855d7e25 863 .exit = virtio_balloon_exit_pci,
e75ccf2c
IY
864 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
865 .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
866 .revision = VIRTIO_PCI_ABI_VERSION,
867 .class_id = PCI_CLASS_MEMORY_RAM,
8172539d
MT
868 .qdev.props = (Property[]) {
869 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
870 DEFINE_PROP_END_OF_LIST(),
871 },
e489030d 872 .qdev.reset = virtio_pci_reset,
0aab0d3a
GH
873 },{
874 /* end of list */
875 }
876};
877
53c25cea
PB
878static void virtio_pci_register_devices(void)
879{
0aab0d3a 880 pci_qdev_register_many(virtio_info);
53c25cea
PB
881}
882
883device_init(virtio_pci_register_devices)