]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio-pci.c
pci: fold BAR mapping function into its caller
[mirror_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
MT
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
95524ae8
AK
644 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
645 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
646 &proxy->msix_bar, 1, 0)) {
647 pci_register_bar_region(&proxy->pci_dev, 1,
648 PCI_BASE_ADDRESS_SPACE_MEMORY,
649 &proxy->msix_bar);
aba800a3
MT
650 } else
651 vdev->nvectors = 0;
652
ed757e14
YV
653 proxy->pci_dev.config_write = virtio_write_config;
654
aba800a3 655 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
53c25cea
PB
656 if (size & (size-1))
657 size = 1 << qemu_fls(size);
658
da146d0a
AK
659 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
660 "virtio-pci", size);
661 pci_register_bar_region(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
662 &proxy->bar);
53c25cea 663
25db9ebe
SH
664 if (!kvm_has_many_ioeventfds()) {
665 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
666 }
667
53c25cea 668 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
8172539d
MT
669 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
670 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
671 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
53c25cea
PB
672}
673
81a322d4 674static int virtio_blk_init_pci(PCIDevice *pci_dev)
53c25cea
PB
675{
676 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
677 VirtIODevice *vdev;
678
ab73ff29
GH
679 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
680 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
681 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
53c25cea 682
a8686a9b
MA
683 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
684 &proxy->block_serial);
ac0c14d7
MA
685 if (!vdev) {
686 return -1;
687 }
177539e0 688 vdev->nvectors = proxy->nvectors;
e75ccf2c 689 virtio_init_pci(proxy, vdev);
177539e0
GH
690 /* make the actual value visible */
691 proxy->nvectors = vdev->nvectors;
81a322d4 692 return 0;
21d58b57
MM
693}
694
0f457d91
MT
695static int virtio_exit_pci(PCIDevice *pci_dev)
696{
da146d0a 697 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
95524ae8 698 int r;
da146d0a
AK
699
700 memory_region_destroy(&proxy->bar);
95524ae8
AK
701 r = msix_uninit(pci_dev, &proxy->msix_bar);
702 memory_region_destroy(&proxy->msix_bar);
703 return r;
0f457d91
MT
704}
705
56a14938
GH
706static int virtio_blk_exit_pci(PCIDevice *pci_dev)
707{
708 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
709
25db9ebe 710 virtio_pci_stop_ioeventfd(proxy);
9d0d3138 711 virtio_blk_exit(proxy->vdev);
f8b6cc00 712 blockdev_mark_auto_del(proxy->block.bs);
0f457d91 713 return virtio_exit_pci(pci_dev);
56a14938
GH
714}
715
98b19252 716static int virtio_serial_init_pci(PCIDevice *pci_dev)
21d58b57 717{
d6beee99 718 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
85c2c735
MM
719 VirtIODevice *vdev;
720
d6beee99
GH
721 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
722 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
723 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
724 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
725
6b331efb 726 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
25fe3654
AS
727 if (!vdev) {
728 return -1;
729 }
573fb60c 730 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
6b331efb 731 ? proxy->serial.max_virtserial_ports + 1
573fb60c 732 : proxy->nvectors;
e75ccf2c 733 virtio_init_pci(proxy, vdev);
a1829205 734 proxy->nvectors = vdev->nvectors;
81a322d4 735 return 0;
53c25cea
PB
736}
737
8b53a865
AS
738static int virtio_serial_exit_pci(PCIDevice *pci_dev)
739{
740 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
741
32059220 742 virtio_pci_stop_ioeventfd(proxy);
8b53a865
AS
743 virtio_serial_exit(proxy->vdev);
744 return virtio_exit_pci(pci_dev);
745}
746
81a322d4 747static int virtio_net_init_pci(PCIDevice *pci_dev)
53c25cea
PB
748{
749 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
750 VirtIODevice *vdev;
751
f0c07c7c 752 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
a1e0fea5 753
97b15621 754 vdev->nvectors = proxy->nvectors;
e75ccf2c 755 virtio_init_pci(proxy, vdev);
a1e0fea5
GH
756
757 /* make the actual value visible */
758 proxy->nvectors = vdev->nvectors;
81a322d4 759 return 0;
53c25cea
PB
760}
761
97b15621
GH
762static int virtio_net_exit_pci(PCIDevice *pci_dev)
763{
764 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
765
25db9ebe 766 virtio_pci_stop_ioeventfd(proxy);
97b15621
GH
767 virtio_net_exit(proxy->vdev);
768 return virtio_exit_pci(pci_dev);
769}
770
81a322d4 771static int virtio_balloon_init_pci(PCIDevice *pci_dev)
53c25cea
PB
772{
773 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
774 VirtIODevice *vdev;
775
776 vdev = virtio_balloon_init(&pci_dev->qdev);
f76f6655
AS
777 if (!vdev) {
778 return -1;
779 }
e75ccf2c 780 virtio_init_pci(proxy, vdev);
81a322d4 781 return 0;
53c25cea
PB
782}
783
855d7e25
AS
784static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
785{
786 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
787
788 virtio_pci_stop_ioeventfd(proxy);
789 virtio_balloon_exit(proxy->vdev);
790 return virtio_exit_pci(pci_dev);
791}
792
0aab0d3a
GH
793static PCIDeviceInfo virtio_info[] = {
794 {
795 .qdev.name = "virtio-blk-pci",
779206de 796 .qdev.alias = "virtio-blk",
0aab0d3a
GH
797 .qdev.size = sizeof(VirtIOPCIProxy),
798 .init = virtio_blk_init_pci,
56a14938 799 .exit = virtio_blk_exit_pci,
e75ccf2c
IY
800 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
801 .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
802 .revision = VIRTIO_PCI_ABI_VERSION,
803 .class_id = PCI_CLASS_STORAGE_SCSI,
ab73ff29 804 .qdev.props = (Property[]) {
72c61d0b 805 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
428c149b 806 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
a8686a9b 807 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
25db9ebe
SH
808 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
809 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
177539e0 810 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
8172539d 811 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
72c61d0b 812 DEFINE_PROP_END_OF_LIST(),
ab73ff29 813 },
e489030d 814 .qdev.reset = virtio_pci_reset,
0aab0d3a 815 },{
a1e0fea5 816 .qdev.name = "virtio-net-pci",
29f82b37 817 .qdev.alias = "virtio-net",
a1e0fea5
GH
818 .qdev.size = sizeof(VirtIOPCIProxy),
819 .init = virtio_net_init_pci,
97b15621 820 .exit = virtio_net_exit_pci,
5ee8ad71 821 .romfile = "pxe-virtio.rom",
e75ccf2c
IY
822 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
823 .device_id = PCI_DEVICE_ID_VIRTIO_NET,
824 .revision = VIRTIO_PCI_ABI_VERSION,
825 .class_id = PCI_CLASS_NETWORK_ETHERNET,
a1e0fea5 826 .qdev.props = (Property[]) {
25db9ebe
SH
827 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
828 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
97b15621 829 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
8172539d 830 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
97b15621 831 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
f0c07c7c
AW
832 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
833 net.txtimer, TX_TIMER_INTERVAL),
e3f30488
AW
834 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
835 net.txburst, TX_BURST),
a697a334 836 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
72c61d0b 837 DEFINE_PROP_END_OF_LIST(),
a1e0fea5 838 },
e489030d 839 .qdev.reset = virtio_pci_reset,
0aab0d3a 840 },{
98b19252
AS
841 .qdev.name = "virtio-serial-pci",
842 .qdev.alias = "virtio-serial",
0aab0d3a 843 .qdev.size = sizeof(VirtIOPCIProxy),
98b19252 844 .init = virtio_serial_init_pci,
8b53a865 845 .exit = virtio_serial_exit_pci,
e75ccf2c
IY
846 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
847 .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
848 .revision = VIRTIO_PCI_ABI_VERSION,
849 .class_id = PCI_CLASS_COMMUNICATION_OTHER,
d6beee99 850 .qdev.props = (Property[]) {
32059220
AS
851 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
852 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
573fb60c
AS
853 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
854 DEV_NVECTORS_UNSPECIFIED),
72c61d0b 855 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
8172539d 856 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
6b331efb
AS
857 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
858 serial.max_virtserial_ports, 31),
72c61d0b 859 DEFINE_PROP_END_OF_LIST(),
d6beee99 860 },
e489030d 861 .qdev.reset = virtio_pci_reset,
0aab0d3a
GH
862 },{
863 .qdev.name = "virtio-balloon-pci",
29f82b37 864 .qdev.alias = "virtio-balloon",
0aab0d3a
GH
865 .qdev.size = sizeof(VirtIOPCIProxy),
866 .init = virtio_balloon_init_pci,
855d7e25 867 .exit = virtio_balloon_exit_pci,
e75ccf2c
IY
868 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
869 .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
870 .revision = VIRTIO_PCI_ABI_VERSION,
871 .class_id = PCI_CLASS_MEMORY_RAM,
8172539d
MT
872 .qdev.props = (Property[]) {
873 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
874 DEFINE_PROP_END_OF_LIST(),
875 },
e489030d 876 .qdev.reset = virtio_pci_reset,
0aab0d3a
GH
877 },{
878 /* end of list */
879 }
880};
881
53c25cea
PB
882static void virtio_pci_register_devices(void)
883{
0aab0d3a 884 pci_qdev_register_many(virtio_info);
53c25cea
PB
885}
886
887device_init(virtio_pci_register_devices)