]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio-pci.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-input-20150623-1' into staging
[mirror_qemu.git] / hw / virtio / 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 *
6b620ca3
PB
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.
53c25cea
PB
16 */
17
18#include <inttypes.h>
19
cbbe4f50 20#include "standard-headers/linux/virtio_pci.h"
0d09e41a
PB
21#include "hw/virtio/virtio.h"
22#include "hw/virtio/virtio-blk.h"
23#include "hw/virtio/virtio-net.h"
24#include "hw/virtio/virtio-serial.h"
25#include "hw/virtio/virtio-scsi.h"
26#include "hw/virtio/virtio-balloon.h"
f958c8aa 27#include "hw/virtio/virtio-input.h"
83c9f4ca 28#include "hw/pci/pci.h"
1de7afc9 29#include "qemu/error-report.h"
83c9f4ca
PB
30#include "hw/pci/msi.h"
31#include "hw/pci/msix.h"
32#include "hw/loader.h"
9c17d615 33#include "sysemu/kvm.h"
4be74634 34#include "sysemu/block-backend.h"
47b43a1f 35#include "virtio-pci.h"
1de7afc9 36#include "qemu/range.h"
0d09e41a 37#include "hw/virtio/virtio-bus.h"
24a6e7f4 38#include "qapi/visitor.h"
53c25cea 39
cbbe4f50 40#define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
aba800a3 41
c17bef33
MT
42#undef VIRTIO_PCI_CONFIG
43
aba800a3
MT
44/* The remaining space is defined by each driver as the per-driver
45 * configuration space */
cbbe4f50 46#define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
53c25cea 47
ac7af112
AF
48static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
49 VirtIOPCIProxy *dev);
d51fcfac 50
53c25cea 51/* virtio device */
d2a0ccc6
MT
52/* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
53static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
54{
55 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
56}
53c25cea 57
d2a0ccc6
MT
58/* DeviceState to VirtIOPCIProxy. Note: used on datapath,
59 * be careful and test performance if you change this.
60 */
61static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
53c25cea 62{
d2a0ccc6
MT
63 return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
64}
65
66static void virtio_pci_notify(DeviceState *d, uint16_t vector)
67{
68 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
a3fc66d9 69
aba800a3
MT
70 if (msix_enabled(&proxy->pci_dev))
71 msix_notify(&proxy->pci_dev, vector);
a3fc66d9
PB
72 else {
73 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
74 pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
75 }
53c25cea
PB
76}
77
d2a0ccc6 78static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
ff24bd58 79{
d2a0ccc6 80 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9
PB
81 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
82
ff24bd58
MT
83 pci_device_save(&proxy->pci_dev, f);
84 msix_save(&proxy->pci_dev, f);
85 if (msix_present(&proxy->pci_dev))
a3fc66d9 86 qemu_put_be16(f, vdev->config_vector);
ff24bd58
MT
87}
88
d2a0ccc6 89static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
ff24bd58 90{
d2a0ccc6 91 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9
PB
92 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
93
ff24bd58 94 if (msix_present(&proxy->pci_dev))
a3fc66d9 95 qemu_put_be16(f, virtio_queue_vector(vdev, n));
ff24bd58
MT
96}
97
d2a0ccc6 98static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
ff24bd58 99{
d2a0ccc6 100 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9
PB
101 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
102
ff24bd58
MT
103 int ret;
104 ret = pci_device_load(&proxy->pci_dev, f);
e6da7680 105 if (ret) {
ff24bd58 106 return ret;
e6da7680 107 }
3cac001e 108 msix_unuse_all_vectors(&proxy->pci_dev);
ff24bd58 109 msix_load(&proxy->pci_dev, f);
e6da7680 110 if (msix_present(&proxy->pci_dev)) {
a3fc66d9 111 qemu_get_be16s(f, &vdev->config_vector);
e6da7680 112 } else {
a3fc66d9 113 vdev->config_vector = VIRTIO_NO_VECTOR;
e6da7680 114 }
a3fc66d9
PB
115 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
116 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
e6da7680 117 }
ff24bd58
MT
118 return 0;
119}
120
d2a0ccc6 121static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
ff24bd58 122{
d2a0ccc6 123 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9
PB
124 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
125
ff24bd58 126 uint16_t vector;
e6da7680
MT
127 if (msix_present(&proxy->pci_dev)) {
128 qemu_get_be16s(f, &vector);
129 } else {
130 vector = VIRTIO_NO_VECTOR;
131 }
a3fc66d9 132 virtio_queue_set_vector(vdev, n, vector);
e6da7680
MT
133 if (vector != VIRTIO_NO_VECTOR) {
134 return msix_vector_use(&proxy->pci_dev, vector);
135 }
ff24bd58
MT
136 return 0;
137}
138
975acc0a
JW
139#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
140
25db9ebe 141static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
26b9b5fe 142 int n, bool assign, bool set_handler)
25db9ebe 143{
a3fc66d9
PB
144 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
145 VirtQueue *vq = virtio_get_queue(vdev, n);
25db9ebe 146 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
975acc0a
JW
147 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
148 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
588255ad 149 MemoryRegion *modern_mr = &proxy->notify.mr;
975acc0a
JW
150 MemoryRegion *legacy_mr = &proxy->bar;
151 hwaddr modern_addr = QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
152 virtio_get_queue_index(vq);
153 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
da146d0a
AK
154 int r = 0;
155
25db9ebe
SH
156 if (assign) {
157 r = event_notifier_init(notifier, 1);
158 if (r < 0) {
b36e3914
MT
159 error_report("%s: unable to init event notifier: %d",
160 __func__, r);
25db9ebe
SH
161 return r;
162 }
26b9b5fe 163 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
975acc0a
JW
164 if (modern) {
165 memory_region_add_eventfd(modern_mr, modern_addr, 2,
166 true, n, notifier);
167 }
168 if (legacy) {
169 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
170 true, n, notifier);
171 }
25db9ebe 172 } else {
975acc0a
JW
173 if (modern) {
174 memory_region_del_eventfd(modern_mr, modern_addr, 2,
175 true, n, notifier);
176 }
177 if (legacy) {
178 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
179 true, n, notifier);
180 }
26b9b5fe 181 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
25db9ebe
SH
182 event_notifier_cleanup(notifier);
183 }
184 return r;
185}
186
b36e3914 187static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe 188{
a3fc66d9 189 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
25db9ebe
SH
190 int n, r;
191
192 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
193 proxy->ioeventfd_disabled ||
194 proxy->ioeventfd_started) {
b36e3914 195 return;
25db9ebe
SH
196 }
197
87b3bd1c 198 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
a3fc66d9 199 if (!virtio_queue_get_num(vdev, n)) {
25db9ebe
SH
200 continue;
201 }
202
26b9b5fe 203 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
25db9ebe
SH
204 if (r < 0) {
205 goto assign_error;
206 }
25db9ebe
SH
207 }
208 proxy->ioeventfd_started = true;
b36e3914 209 return;
25db9ebe
SH
210
211assign_error:
212 while (--n >= 0) {
a3fc66d9 213 if (!virtio_queue_get_num(vdev, n)) {
25db9ebe
SH
214 continue;
215 }
216
26b9b5fe 217 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
b36e3914 218 assert(r >= 0);
25db9ebe
SH
219 }
220 proxy->ioeventfd_started = false;
b36e3914 221 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
25db9ebe
SH
222}
223
b36e3914 224static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe 225{
a3fc66d9 226 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
b36e3914 227 int r;
25db9ebe
SH
228 int n;
229
230 if (!proxy->ioeventfd_started) {
b36e3914 231 return;
25db9ebe
SH
232 }
233
87b3bd1c 234 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
a3fc66d9 235 if (!virtio_queue_get_num(vdev, n)) {
25db9ebe
SH
236 continue;
237 }
238
26b9b5fe 239 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
b36e3914 240 assert(r >= 0);
25db9ebe
SH
241 }
242 proxy->ioeventfd_started = false;
25db9ebe
SH
243}
244
53c25cea
PB
245static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
246{
247 VirtIOPCIProxy *proxy = opaque;
a3fc66d9 248 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
a8170e5e 249 hwaddr pa;
53c25cea 250
53c25cea
PB
251 switch (addr) {
252 case VIRTIO_PCI_GUEST_FEATURES:
181103cd
FK
253 /* Guest does not negotiate properly? We have to assume nothing. */
254 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
255 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
256 }
ad0c9332 257 virtio_set_features(vdev, val);
53c25cea
PB
258 break;
259 case VIRTIO_PCI_QUEUE_PFN:
a8170e5e 260 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
1b8e9b27 261 if (pa == 0) {
25db9ebe 262 virtio_pci_stop_ioeventfd(proxy);
a3fc66d9 263 virtio_reset(vdev);
1b8e9b27
MT
264 msix_unuse_all_vectors(&proxy->pci_dev);
265 }
7055e687
MT
266 else
267 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
53c25cea
PB
268 break;
269 case VIRTIO_PCI_QUEUE_SEL:
87b3bd1c 270 if (val < VIRTIO_QUEUE_MAX)
53c25cea
PB
271 vdev->queue_sel = val;
272 break;
273 case VIRTIO_PCI_QUEUE_NOTIFY:
87b3bd1c 274 if (val < VIRTIO_QUEUE_MAX) {
7157e2e2
SH
275 virtio_queue_notify(vdev, val);
276 }
53c25cea
PB
277 break;
278 case VIRTIO_PCI_STATUS:
25db9ebe
SH
279 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
280 virtio_pci_stop_ioeventfd(proxy);
281 }
282
3e607cb5 283 virtio_set_status(vdev, val & 0xFF);
25db9ebe
SH
284
285 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
286 virtio_pci_start_ioeventfd(proxy);
287 }
288
1b8e9b27 289 if (vdev->status == 0) {
a3fc66d9 290 virtio_reset(vdev);
1b8e9b27
MT
291 msix_unuse_all_vectors(&proxy->pci_dev);
292 }
c81131db 293
e43c0b2e
MT
294 /* Linux before 2.6.34 drives the device without enabling
295 the PCI device bus master bit. Enable it automatically
296 for the guest. This is a PCI spec violation but so is
297 initiating DMA with bus master bit clear. */
298 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
299 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
300 proxy->pci_dev.config[PCI_COMMAND] |
301 PCI_COMMAND_MASTER, 1);
302 }
53c25cea 303 break;
aba800a3
MT
304 case VIRTIO_MSI_CONFIG_VECTOR:
305 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
306 /* Make it possible for guest to discover an error took place. */
307 if (msix_vector_use(&proxy->pci_dev, val) < 0)
308 val = VIRTIO_NO_VECTOR;
309 vdev->config_vector = val;
310 break;
311 case VIRTIO_MSI_QUEUE_VECTOR:
312 msix_vector_unuse(&proxy->pci_dev,
313 virtio_queue_vector(vdev, vdev->queue_sel));
314 /* Make it possible for guest to discover an error took place. */
315 if (msix_vector_use(&proxy->pci_dev, val) < 0)
316 val = VIRTIO_NO_VECTOR;
317 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
318 break;
319 default:
4e02d460
SH
320 error_report("%s: unexpected address 0x%x value 0x%x",
321 __func__, addr, val);
aba800a3 322 break;
53c25cea
PB
323 }
324}
325
aba800a3 326static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
53c25cea 327{
a3fc66d9 328 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
53c25cea
PB
329 uint32_t ret = 0xFFFFFFFF;
330
53c25cea
PB
331 switch (addr) {
332 case VIRTIO_PCI_HOST_FEATURES:
6b8f1020 333 ret = vdev->host_features;
53c25cea
PB
334 break;
335 case VIRTIO_PCI_GUEST_FEATURES:
704a76fc 336 ret = vdev->guest_features;
53c25cea
PB
337 break;
338 case VIRTIO_PCI_QUEUE_PFN:
339 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
340 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
341 break;
342 case VIRTIO_PCI_QUEUE_NUM:
343 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
344 break;
345 case VIRTIO_PCI_QUEUE_SEL:
346 ret = vdev->queue_sel;
347 break;
348 case VIRTIO_PCI_STATUS:
349 ret = vdev->status;
350 break;
351 case VIRTIO_PCI_ISR:
352 /* reading from the ISR also clears it. */
353 ret = vdev->isr;
354 vdev->isr = 0;
9e64f8a3 355 pci_irq_deassert(&proxy->pci_dev);
53c25cea 356 break;
aba800a3
MT
357 case VIRTIO_MSI_CONFIG_VECTOR:
358 ret = vdev->config_vector;
359 break;
360 case VIRTIO_MSI_QUEUE_VECTOR:
361 ret = virtio_queue_vector(vdev, vdev->queue_sel);
362 break;
53c25cea
PB
363 default:
364 break;
365 }
366
367 return ret;
368}
369
df6db5b3
AG
370static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
371 unsigned size)
53c25cea
PB
372{
373 VirtIOPCIProxy *proxy = opaque;
a3fc66d9 374 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
cbbe4f50 375 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
df6db5b3 376 uint64_t val = 0;
aba800a3 377 if (addr < config) {
df6db5b3 378 return virtio_ioport_read(proxy, addr);
aba800a3
MT
379 }
380 addr -= config;
53c25cea 381
df6db5b3
AG
382 switch (size) {
383 case 1:
a3fc66d9 384 val = virtio_config_readb(vdev, addr);
df6db5b3
AG
385 break;
386 case 2:
a3fc66d9 387 val = virtio_config_readw(vdev, addr);
616a6552 388 if (virtio_is_big_endian(vdev)) {
8e4a424b
BS
389 val = bswap16(val);
390 }
df6db5b3
AG
391 break;
392 case 4:
a3fc66d9 393 val = virtio_config_readl(vdev, addr);
616a6552 394 if (virtio_is_big_endian(vdev)) {
8e4a424b
BS
395 val = bswap32(val);
396 }
df6db5b3 397 break;
82afa586 398 }
df6db5b3 399 return val;
53c25cea
PB
400}
401
df6db5b3
AG
402static void virtio_pci_config_write(void *opaque, hwaddr addr,
403 uint64_t val, unsigned size)
53c25cea
PB
404{
405 VirtIOPCIProxy *proxy = opaque;
cbbe4f50 406 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
a3fc66d9 407 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
aba800a3
MT
408 if (addr < config) {
409 virtio_ioport_write(proxy, addr, val);
410 return;
411 }
412 addr -= config;
df6db5b3
AG
413 /*
414 * Virtio-PCI is odd. Ioports are LE but config space is target native
415 * endian.
416 */
417 switch (size) {
418 case 1:
a3fc66d9 419 virtio_config_writeb(vdev, addr, val);
df6db5b3
AG
420 break;
421 case 2:
616a6552 422 if (virtio_is_big_endian(vdev)) {
8e4a424b
BS
423 val = bswap16(val);
424 }
a3fc66d9 425 virtio_config_writew(vdev, addr, val);
df6db5b3
AG
426 break;
427 case 4:
616a6552 428 if (virtio_is_big_endian(vdev)) {
8e4a424b
BS
429 val = bswap32(val);
430 }
a3fc66d9 431 virtio_config_writel(vdev, addr, val);
df6db5b3 432 break;
82afa586 433 }
53c25cea
PB
434}
435
da146d0a 436static const MemoryRegionOps virtio_pci_config_ops = {
df6db5b3
AG
437 .read = virtio_pci_config_read,
438 .write = virtio_pci_config_write,
439 .impl = {
440 .min_access_size = 1,
441 .max_access_size = 4,
442 },
8e4a424b 443 .endianness = DEVICE_LITTLE_ENDIAN,
da146d0a 444};
aba800a3
MT
445
446static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
447 uint32_t val, int len)
448{
ed757e14 449 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
a3fc66d9 450 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
ed757e14 451
1129714f
MT
452 pci_default_write_config(pci_dev, address, val, len);
453
454 if (range_covers_byte(address, len, PCI_COMMAND) &&
68a27b20 455 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1129714f 456 virtio_pci_stop_ioeventfd(proxy);
45363e46 457 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
ed757e14 458 }
53c25cea
PB
459}
460
7d37d351
JK
461static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
462 unsigned int queue_no,
463 unsigned int vector,
464 MSIMessage msg)
465{
7d37d351 466 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
15b2bd18 467 int ret;
7d37d351
JK
468
469 if (irqfd->users == 0) {
470 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
471 if (ret < 0) {
472 return ret;
473 }
474 irqfd->virq = ret;
475 }
476 irqfd->users++;
7d37d351
JK
477 return 0;
478}
479
480static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
7d37d351 481 unsigned int vector)
774345f9
MT
482{
483 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
484 if (--irqfd->users == 0) {
485 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
486 }
487}
488
f1d0f15a
MT
489static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
490 unsigned int queue_no,
491 unsigned int vector)
492{
493 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
a3fc66d9
PB
494 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
495 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
f1d0f15a
MT
496 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
497 int ret;
ca916d37 498 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq);
f1d0f15a
MT
499 return ret;
500}
501
502static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
503 unsigned int queue_no,
504 unsigned int vector)
7d37d351 505{
a3fc66d9
PB
506 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
507 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
15b2bd18 508 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
7d37d351 509 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
15b2bd18 510 int ret;
7d37d351 511
b131c74a 512 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
7d37d351 513 assert(ret == 0);
f1d0f15a 514}
7d37d351 515
774345f9
MT
516static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
517{
518 PCIDevice *dev = &proxy->pci_dev;
a3fc66d9 519 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
181103cd 520 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
774345f9
MT
521 unsigned int vector;
522 int ret, queue_no;
523 MSIMessage msg;
524
525 for (queue_no = 0; queue_no < nvqs; queue_no++) {
526 if (!virtio_queue_get_num(vdev, queue_no)) {
527 break;
528 }
529 vector = virtio_queue_vector(vdev, queue_no);
530 if (vector >= msix_nr_vectors_allocated(dev)) {
531 continue;
532 }
533 msg = msix_get_message(dev, vector);
534 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
535 if (ret < 0) {
536 goto undo;
7d37d351 537 }
f1d0f15a
MT
538 /* If guest supports masking, set up irqfd now.
539 * Otherwise, delay until unmasked in the frontend.
540 */
181103cd 541 if (k->guest_notifier_mask) {
f1d0f15a
MT
542 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
543 if (ret < 0) {
544 kvm_virtio_pci_vq_vector_release(proxy, vector);
545 goto undo;
546 }
547 }
7d37d351 548 }
7d37d351 549 return 0;
774345f9
MT
550
551undo:
552 while (--queue_no >= 0) {
553 vector = virtio_queue_vector(vdev, queue_no);
554 if (vector >= msix_nr_vectors_allocated(dev)) {
555 continue;
556 }
181103cd 557 if (k->guest_notifier_mask) {
e387f99e 558 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
f1d0f15a 559 }
774345f9
MT
560 kvm_virtio_pci_vq_vector_release(proxy, vector);
561 }
562 return ret;
7d37d351
JK
563}
564
774345f9
MT
565static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
566{
567 PCIDevice *dev = &proxy->pci_dev;
a3fc66d9 568 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
774345f9
MT
569 unsigned int vector;
570 int queue_no;
181103cd 571 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
774345f9
MT
572
573 for (queue_no = 0; queue_no < nvqs; queue_no++) {
574 if (!virtio_queue_get_num(vdev, queue_no)) {
575 break;
576 }
577 vector = virtio_queue_vector(vdev, queue_no);
578 if (vector >= msix_nr_vectors_allocated(dev)) {
579 continue;
580 }
f1d0f15a
MT
581 /* If guest supports masking, clean up irqfd now.
582 * Otherwise, it was cleaned when masked in the frontend.
583 */
181103cd 584 if (k->guest_notifier_mask) {
e387f99e 585 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
f1d0f15a 586 }
774345f9
MT
587 kvm_virtio_pci_vq_vector_release(proxy, vector);
588 }
589}
590
a38b2c49
MT
591static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
592 unsigned int queue_no,
593 unsigned int vector,
594 MSIMessage msg)
774345f9 595{
a3fc66d9
PB
596 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
597 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
598 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
774345f9 599 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
a38b2c49 600 VirtIOIRQFD *irqfd;
53510bfc 601 int ret = 0;
774345f9 602
a38b2c49
MT
603 if (proxy->vector_irqfd) {
604 irqfd = &proxy->vector_irqfd[vector];
605 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
606 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
607 if (ret < 0) {
608 return ret;
609 }
774345f9
MT
610 }
611 }
612
f1d0f15a
MT
613 /* If guest supports masking, irqfd is already setup, unmask it.
614 * Otherwise, set it up now.
615 */
181103cd 616 if (k->guest_notifier_mask) {
a3fc66d9 617 k->guest_notifier_mask(vdev, queue_no, false);
f1d0f15a 618 /* Test after unmasking to avoid losing events. */
181103cd 619 if (k->guest_notifier_pending &&
a3fc66d9 620 k->guest_notifier_pending(vdev, queue_no)) {
f1d0f15a
MT
621 event_notifier_set(n);
622 }
623 } else {
624 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
7d37d351 625 }
774345f9 626 return ret;
7d37d351
JK
627}
628
a38b2c49 629static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
7d37d351
JK
630 unsigned int queue_no,
631 unsigned int vector)
632{
a3fc66d9
PB
633 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
634 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
181103cd 635
f1d0f15a
MT
636 /* If guest supports masking, keep irqfd but mask it.
637 * Otherwise, clean it up now.
638 */
181103cd 639 if (k->guest_notifier_mask) {
a3fc66d9 640 k->guest_notifier_mask(vdev, queue_no, true);
f1d0f15a 641 } else {
e387f99e 642 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
f1d0f15a 643 }
7d37d351
JK
644}
645
a38b2c49
MT
646static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
647 MSIMessage msg)
7d37d351
JK
648{
649 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
a3fc66d9 650 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
851c2a75
JW
651 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
652 int ret, index, unmasked = 0;
7d37d351 653
851c2a75
JW
654 while (vq) {
655 index = virtio_get_queue_index(vq);
656 if (!virtio_queue_get_num(vdev, index)) {
7d37d351
JK
657 break;
658 }
6652d081
JW
659 if (index < proxy->nvqs_with_notifiers) {
660 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
661 if (ret < 0) {
662 goto undo;
663 }
664 ++unmasked;
7d37d351 665 }
851c2a75 666 vq = virtio_vector_next_queue(vq);
7d37d351 667 }
851c2a75 668
7d37d351
JK
669 return 0;
670
671undo:
851c2a75 672 vq = virtio_vector_first_queue(vdev, vector);
6652d081 673 while (vq && unmasked >= 0) {
851c2a75 674 index = virtio_get_queue_index(vq);
6652d081
JW
675 if (index < proxy->nvqs_with_notifiers) {
676 virtio_pci_vq_vector_mask(proxy, index, vector);
677 --unmasked;
678 }
851c2a75 679 vq = virtio_vector_next_queue(vq);
7d37d351
JK
680 }
681 return ret;
682}
683
a38b2c49 684static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
7d37d351
JK
685{
686 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
a3fc66d9 687 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
851c2a75
JW
688 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
689 int index;
7d37d351 690
851c2a75
JW
691 while (vq) {
692 index = virtio_get_queue_index(vq);
693 if (!virtio_queue_get_num(vdev, index)) {
7d37d351
JK
694 break;
695 }
6652d081
JW
696 if (index < proxy->nvqs_with_notifiers) {
697 virtio_pci_vq_vector_mask(proxy, index, vector);
698 }
851c2a75 699 vq = virtio_vector_next_queue(vq);
7d37d351
JK
700 }
701}
702
a38b2c49
MT
703static void virtio_pci_vector_poll(PCIDevice *dev,
704 unsigned int vector_start,
705 unsigned int vector_end)
89d62be9
MT
706{
707 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
a3fc66d9 708 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
181103cd 709 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
89d62be9
MT
710 int queue_no;
711 unsigned int vector;
712 EventNotifier *notifier;
713 VirtQueue *vq;
714
2d620f59 715 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
89d62be9
MT
716 if (!virtio_queue_get_num(vdev, queue_no)) {
717 break;
718 }
719 vector = virtio_queue_vector(vdev, queue_no);
720 if (vector < vector_start || vector >= vector_end ||
721 !msix_is_masked(dev, vector)) {
722 continue;
723 }
724 vq = virtio_get_queue(vdev, queue_no);
725 notifier = virtio_queue_get_guest_notifier(vq);
181103cd
FK
726 if (k->guest_notifier_pending) {
727 if (k->guest_notifier_pending(vdev, queue_no)) {
f1d0f15a
MT
728 msix_set_pending(dev, vector);
729 }
730 } else if (event_notifier_test_and_clear(notifier)) {
89d62be9
MT
731 msix_set_pending(dev, vector);
732 }
733 }
734}
735
736static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
737 bool with_irqfd)
ade80dc8 738{
d2a0ccc6 739 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9
PB
740 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
741 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
742 VirtQueue *vq = virtio_get_queue(vdev, n);
ade80dc8
MT
743 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
744
745 if (assign) {
746 int r = event_notifier_init(notifier, 0);
747 if (r < 0) {
748 return r;
749 }
89d62be9 750 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
ade80dc8 751 } else {
89d62be9 752 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
ade80dc8
MT
753 event_notifier_cleanup(notifier);
754 }
755
62c96360 756 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
a3fc66d9 757 vdc->guest_notifier_mask(vdev, n, !assign);
62c96360
MT
758 }
759
ade80dc8
MT
760 return 0;
761}
762
d2a0ccc6 763static bool virtio_pci_query_guest_notifiers(DeviceState *d)
5430a28f 764{
d2a0ccc6 765 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
5430a28f
MT
766 return msix_enabled(&proxy->pci_dev);
767}
768
2d620f59 769static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
54dd9321 770{
d2a0ccc6 771 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9 772 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
181103cd 773 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
54dd9321 774 int r, n;
89d62be9
MT
775 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
776 kvm_msi_via_irqfd_enabled();
54dd9321 777
87b3bd1c 778 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
2d620f59
MT
779
780 /* When deassigning, pass a consistent nvqs value
781 * to avoid leaking notifiers.
782 */
783 assert(assign || nvqs == proxy->nvqs_with_notifiers);
784
785 proxy->nvqs_with_notifiers = nvqs;
786
7d37d351 787 /* Must unset vector notifier while guest notifier is still assigned */
181103cd 788 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
7d37d351 789 msix_unset_vector_notifiers(&proxy->pci_dev);
a38b2c49
MT
790 if (proxy->vector_irqfd) {
791 kvm_virtio_pci_vector_release(proxy, nvqs);
792 g_free(proxy->vector_irqfd);
793 proxy->vector_irqfd = NULL;
794 }
7d37d351
JK
795 }
796
2d620f59 797 for (n = 0; n < nvqs; n++) {
54dd9321
MT
798 if (!virtio_queue_get_num(vdev, n)) {
799 break;
800 }
801
23fe2b3f 802 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
54dd9321
MT
803 if (r < 0) {
804 goto assign_error;
805 }
806 }
807
7d37d351 808 /* Must set vector notifier after guest notifier has been assigned */
181103cd 809 if ((with_irqfd || k->guest_notifier_mask) && assign) {
a38b2c49
MT
810 if (with_irqfd) {
811 proxy->vector_irqfd =
812 g_malloc0(sizeof(*proxy->vector_irqfd) *
813 msix_nr_vectors_allocated(&proxy->pci_dev));
814 r = kvm_virtio_pci_vector_use(proxy, nvqs);
815 if (r < 0) {
816 goto assign_error;
817 }
774345f9 818 }
7d37d351 819 r = msix_set_vector_notifiers(&proxy->pci_dev,
a38b2c49
MT
820 virtio_pci_vector_unmask,
821 virtio_pci_vector_mask,
822 virtio_pci_vector_poll);
7d37d351 823 if (r < 0) {
774345f9 824 goto notifiers_error;
7d37d351
JK
825 }
826 }
827
54dd9321
MT
828 return 0;
829
774345f9 830notifiers_error:
a38b2c49
MT
831 if (with_irqfd) {
832 assert(assign);
833 kvm_virtio_pci_vector_release(proxy, nvqs);
834 }
774345f9 835
54dd9321
MT
836assign_error:
837 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
7d37d351 838 assert(assign);
54dd9321 839 while (--n >= 0) {
89d62be9 840 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
54dd9321
MT
841 }
842 return r;
843}
844
d2a0ccc6 845static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
ade80dc8 846{
d2a0ccc6 847 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
25db9ebe
SH
848
849 /* Stop using ioeventfd for virtqueue kick if the device starts using host
850 * notifiers. This makes it easy to avoid stepping on each others' toes.
851 */
852 proxy->ioeventfd_disabled = assign;
ade80dc8 853 if (assign) {
25db9ebe
SH
854 virtio_pci_stop_ioeventfd(proxy);
855 }
856 /* We don't need to start here: it's not needed because backend
857 * currently only stops on status change away from ok,
858 * reset, vmstop and such. If we do add code to start here,
859 * need to check vmstate, device state etc. */
26b9b5fe 860 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
25db9ebe
SH
861}
862
d2a0ccc6 863static void virtio_pci_vmstate_change(DeviceState *d, bool running)
25db9ebe 864{
d2a0ccc6 865 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
a3fc66d9 866 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
25db9ebe
SH
867
868 if (running) {
68a27b20
MT
869 /* Old QEMU versions did not set bus master enable on status write.
870 * Detect DRIVER set and enable it.
871 */
872 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
873 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
45363e46 874 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
68a27b20
MT
875 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
876 proxy->pci_dev.config[PCI_COMMAND] |
877 PCI_COMMAND_MASTER, 1);
89c473fd 878 }
25db9ebe 879 virtio_pci_start_ioeventfd(proxy);
ade80dc8 880 } else {
25db9ebe 881 virtio_pci_stop_ioeventfd(proxy);
ade80dc8 882 }
ade80dc8
MT
883}
884
60653b28 885#ifdef CONFIG_VIRTFS
fc079951 886static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
60653b28 887{
234a336f
FK
888 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
889 DeviceState *vdev = DEVICE(&dev->vdev);
60653b28 890
234a336f 891 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 892 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
60653b28
PB
893}
894
234a336f
FK
895static Property virtio_9p_pci_properties[] = {
896 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
897 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
60653b28 898 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
60653b28
PB
899 DEFINE_PROP_END_OF_LIST(),
900};
901
234a336f 902static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
60653b28
PB
903{
904 DeviceClass *dc = DEVICE_CLASS(klass);
234a336f
FK
905 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
906 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
60653b28 907
fc079951 908 k->realize = virtio_9p_pci_realize;
234a336f
FK
909 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
910 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
911 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
912 pcidev_k->class_id = 0x2;
125ee0ed 913 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
234a336f 914 dc->props = virtio_9p_pci_properties;
60653b28
PB
915}
916
234a336f
FK
917static void virtio_9p_pci_instance_init(Object *obj)
918{
919 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
c8075caf
GA
920
921 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
922 TYPE_VIRTIO_9P);
234a336f
FK
923}
924
925static const TypeInfo virtio_9p_pci_info = {
926 .name = TYPE_VIRTIO_9P_PCI,
927 .parent = TYPE_VIRTIO_PCI,
928 .instance_size = sizeof(V9fsPCIState),
929 .instance_init = virtio_9p_pci_instance_init,
930 .class_init = virtio_9p_pci_class_init,
60653b28 931};
234a336f 932#endif /* CONFIG_VIRTFS */
60653b28 933
085bccb7
FK
934/*
935 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
936 */
937
e0d686bf
JW
938static int virtio_pci_query_nvectors(DeviceState *d)
939{
940 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
941
942 return proxy->nvectors;
943}
944
dfb8e184
MT
945static void virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
946 struct virtio_pci_cap *cap)
947{
948 PCIDevice *dev = &proxy->pci_dev;
949 int offset;
950
dfb8e184
MT
951 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
952 assert(offset > 0);
953
954 assert(cap->cap_len >= sizeof *cap);
955 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
956 cap->cap_len - PCI_CAP_FLAGS);
957}
958
dfb8e184
MT
959static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
960 unsigned size)
961{
962 VirtIOPCIProxy *proxy = opaque;
963 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
964 uint32_t val = 0;
965 int i;
966
967 switch (addr) {
968 case VIRTIO_PCI_COMMON_DFSELECT:
969 val = proxy->dfselect;
970 break;
971 case VIRTIO_PCI_COMMON_DF:
972 if (proxy->dfselect <= 1) {
973 val = vdev->host_features >> (32 * proxy->dfselect);
974 }
975 break;
976 case VIRTIO_PCI_COMMON_GFSELECT:
977 val = proxy->gfselect;
978 break;
979 case VIRTIO_PCI_COMMON_GF:
980 if (proxy->gfselect <= ARRAY_SIZE(proxy->guest_features)) {
981 val = proxy->guest_features[proxy->gfselect];
982 }
983 break;
984 case VIRTIO_PCI_COMMON_MSIX:
985 val = vdev->config_vector;
986 break;
987 case VIRTIO_PCI_COMMON_NUMQ:
988 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
989 if (virtio_queue_get_num(vdev, i)) {
990 val = i + 1;
991 }
992 }
993 break;
994 case VIRTIO_PCI_COMMON_STATUS:
995 val = vdev->status;
996 break;
997 case VIRTIO_PCI_COMMON_CFGGENERATION:
b8f05908 998 val = vdev->generation;
dfb8e184
MT
999 break;
1000 case VIRTIO_PCI_COMMON_Q_SELECT:
1001 val = vdev->queue_sel;
1002 break;
1003 case VIRTIO_PCI_COMMON_Q_SIZE:
1004 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1005 break;
1006 case VIRTIO_PCI_COMMON_Q_MSIX:
1007 val = virtio_queue_vector(vdev, vdev->queue_sel);
1008 break;
1009 case VIRTIO_PCI_COMMON_Q_ENABLE:
1010 val = proxy->vqs[vdev->queue_sel].enabled;
1011 break;
1012 case VIRTIO_PCI_COMMON_Q_NOFF:
1013 /* Simply map queues in order */
1014 val = vdev->queue_sel;
1015 break;
1016 case VIRTIO_PCI_COMMON_Q_DESCLO:
1017 val = proxy->vqs[vdev->queue_sel].desc[0];
1018 break;
1019 case VIRTIO_PCI_COMMON_Q_DESCHI:
1020 val = proxy->vqs[vdev->queue_sel].desc[1];
1021 break;
1022 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1023 val = proxy->vqs[vdev->queue_sel].avail[0];
1024 break;
1025 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1026 val = proxy->vqs[vdev->queue_sel].avail[1];
1027 break;
1028 case VIRTIO_PCI_COMMON_Q_USEDLO:
1029 val = proxy->vqs[vdev->queue_sel].used[0];
1030 break;
1031 case VIRTIO_PCI_COMMON_Q_USEDHI:
1032 val = proxy->vqs[vdev->queue_sel].used[1];
1033 break;
1034 default:
1035 val = 0;
1036 }
1037
1038 return val;
1039}
1040
1041static void virtio_pci_common_write(void *opaque, hwaddr addr,
1042 uint64_t val, unsigned size)
1043{
1044 VirtIOPCIProxy *proxy = opaque;
1045 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1046
1047 switch (addr) {
1048 case VIRTIO_PCI_COMMON_DFSELECT:
1049 proxy->dfselect = val;
1050 break;
1051 case VIRTIO_PCI_COMMON_GFSELECT:
1052 proxy->gfselect = val;
1053 break;
1054 case VIRTIO_PCI_COMMON_GF:
1055 if (proxy->gfselect <= ARRAY_SIZE(proxy->guest_features)) {
1056 proxy->guest_features[proxy->gfselect] = val;
1057 virtio_set_features(vdev,
1058 (((uint64_t)proxy->guest_features[1]) << 32) |
1059 proxy->guest_features[0]);
1060 }
1061 break;
1062 case VIRTIO_PCI_COMMON_MSIX:
1063 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1064 /* Make it possible for guest to discover an error took place. */
1065 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1066 val = VIRTIO_NO_VECTOR;
1067 }
1068 vdev->config_vector = val;
1069 break;
1070 case VIRTIO_PCI_COMMON_STATUS:
1071 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1072 virtio_pci_stop_ioeventfd(proxy);
1073 }
1074
1075 virtio_set_status(vdev, val & 0xFF);
1076
1077 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1078 virtio_pci_start_ioeventfd(proxy);
1079 }
1080
1081 if (vdev->status == 0) {
1082 virtio_reset(vdev);
1083 msix_unuse_all_vectors(&proxy->pci_dev);
1084 }
1085
1086 break;
1087 case VIRTIO_PCI_COMMON_Q_SELECT:
1088 if (val < VIRTIO_QUEUE_MAX) {
1089 vdev->queue_sel = val;
1090 }
1091 break;
1092 case VIRTIO_PCI_COMMON_Q_SIZE:
1093 proxy->vqs[vdev->queue_sel].num = val;
1094 break;
1095 case VIRTIO_PCI_COMMON_Q_MSIX:
1096 msix_vector_unuse(&proxy->pci_dev,
1097 virtio_queue_vector(vdev, vdev->queue_sel));
1098 /* Make it possible for guest to discover an error took place. */
1099 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1100 val = VIRTIO_NO_VECTOR;
1101 }
1102 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1103 break;
1104 case VIRTIO_PCI_COMMON_Q_ENABLE:
1105 /* TODO: need a way to put num back on reset. */
1106 virtio_queue_set_num(vdev, vdev->queue_sel,
1107 proxy->vqs[vdev->queue_sel].num);
1108 virtio_queue_set_rings(vdev, vdev->queue_sel,
1109 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1110 proxy->vqs[vdev->queue_sel].desc[0],
1111 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1112 proxy->vqs[vdev->queue_sel].avail[0],
1113 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1114 proxy->vqs[vdev->queue_sel].used[0]);
1115 break;
1116 case VIRTIO_PCI_COMMON_Q_DESCLO:
1117 proxy->vqs[vdev->queue_sel].desc[0] = val;
1118 break;
1119 case VIRTIO_PCI_COMMON_Q_DESCHI:
1120 proxy->vqs[vdev->queue_sel].desc[1] = val;
1121 break;
1122 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1123 proxy->vqs[vdev->queue_sel].avail[0] = val;
1124 break;
1125 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1126 proxy->vqs[vdev->queue_sel].avail[1] = val;
1127 break;
1128 case VIRTIO_PCI_COMMON_Q_USEDLO:
1129 proxy->vqs[vdev->queue_sel].used[0] = val;
1130 break;
1131 case VIRTIO_PCI_COMMON_Q_USEDHI:
1132 proxy->vqs[vdev->queue_sel].used[1] = val;
1133 break;
1134 default:
1135 break;
1136 }
1137}
1138
1139
1140static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1141 unsigned size)
1142{
1143 return 0;
1144}
1145
1146static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1147 uint64_t val, unsigned size)
1148{
1149 VirtIODevice *vdev = opaque;
1150 unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT;
1151
1152 if (queue < VIRTIO_QUEUE_MAX) {
1153 virtio_queue_notify(vdev, queue);
1154 }
1155}
1156
1157static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1158 unsigned size)
1159{
1160 VirtIOPCIProxy *proxy = opaque;
1161 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1162 uint64_t val = vdev->isr;
1163
1164 vdev->isr = 0;
1165 pci_irq_deassert(&proxy->pci_dev);
1166
1167 return val;
1168}
1169
1170static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1171 uint64_t val, unsigned size)
1172{
1173}
1174
1175static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1176 unsigned size)
1177{
1178 VirtIODevice *vdev = opaque;
1179 uint64_t val = 0;
1180
1181 switch (size) {
1182 case 1:
54c720d4 1183 val = virtio_config_modern_readb(vdev, addr);
dfb8e184
MT
1184 break;
1185 case 2:
54c720d4 1186 val = virtio_config_modern_readw(vdev, addr);
dfb8e184
MT
1187 break;
1188 case 4:
54c720d4 1189 val = virtio_config_modern_readl(vdev, addr);
dfb8e184
MT
1190 break;
1191 }
1192 return val;
1193}
1194
1195static void virtio_pci_device_write(void *opaque, hwaddr addr,
1196 uint64_t val, unsigned size)
1197{
1198 VirtIODevice *vdev = opaque;
1199 switch (size) {
1200 case 1:
54c720d4 1201 virtio_config_modern_writeb(vdev, addr, val);
dfb8e184
MT
1202 break;
1203 case 2:
54c720d4 1204 virtio_config_modern_writew(vdev, addr, val);
dfb8e184
MT
1205 break;
1206 case 4:
54c720d4 1207 virtio_config_modern_writel(vdev, addr, val);
dfb8e184
MT
1208 break;
1209 }
1210}
1211
1141ce21
GH
1212static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1213{
1214 static const MemoryRegionOps common_ops = {
1215 .read = virtio_pci_common_read,
1216 .write = virtio_pci_common_write,
1217 .impl = {
1218 .min_access_size = 1,
1219 .max_access_size = 4,
1220 },
1221 .endianness = DEVICE_LITTLE_ENDIAN,
1222 };
1223 static const MemoryRegionOps isr_ops = {
1224 .read = virtio_pci_isr_read,
1225 .write = virtio_pci_isr_write,
1226 .impl = {
1227 .min_access_size = 1,
1228 .max_access_size = 4,
1229 },
1230 .endianness = DEVICE_LITTLE_ENDIAN,
1231 };
1232 static const MemoryRegionOps device_ops = {
1233 .read = virtio_pci_device_read,
1234 .write = virtio_pci_device_write,
1235 .impl = {
1236 .min_access_size = 1,
1237 .max_access_size = 4,
1238 },
1239 .endianness = DEVICE_LITTLE_ENDIAN,
1240 };
1241 static const MemoryRegionOps notify_ops = {
1242 .read = virtio_pci_notify_read,
1243 .write = virtio_pci_notify_write,
1244 .impl = {
1245 .min_access_size = 1,
1246 .max_access_size = 4,
1247 },
1248 .endianness = DEVICE_LITTLE_ENDIAN,
1249 };
1250
1251 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1252 &common_ops,
1253 proxy,
b6ce27a5
GH
1254 "virtio-pci-common",
1255 proxy->common.size);
a3cc2e81 1256
1141ce21
GH
1257 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1258 &isr_ops,
1259 proxy,
b6ce27a5
GH
1260 "virtio-pci-isr",
1261 proxy->isr.size);
a3cc2e81 1262
1141ce21
GH
1263 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1264 &device_ops,
1265 virtio_bus_get_device(&proxy->bus),
b6ce27a5
GH
1266 "virtio-pci-device",
1267 proxy->device.size);
a3cc2e81 1268
1141ce21
GH
1269 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1270 &notify_ops,
1271 virtio_bus_get_device(&proxy->bus),
1272 "virtio-pci-notify",
b6ce27a5 1273 proxy->notify.size);
a3cc2e81
GH
1274}
1275
1276static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
54790d71
GH
1277 VirtIOPCIRegion *region,
1278 struct virtio_pci_cap *cap)
a3cc2e81
GH
1279{
1280 memory_region_add_subregion(&proxy->modern_bar,
1281 region->offset,
1282 &region->mr);
54790d71 1283
fc004905 1284 cap->cfg_type = region->type;
b6ce27a5 1285 cap->bar = proxy->modern_mem_bar;
54790d71 1286 cap->offset = cpu_to_le32(region->offset);
b6ce27a5 1287 cap->length = cpu_to_le32(region->size);
54790d71 1288 virtio_pci_add_mem_cap(proxy, cap);
1141ce21 1289}
dfb8e184 1290
085bccb7 1291/* This is called by virtio-bus just after the device is plugged. */
e8398045 1292static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
085bccb7
FK
1293{
1294 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1295 VirtioBusState *bus = &proxy->bus;
e266d421
GH
1296 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
1297 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
085bccb7
FK
1298 uint8_t *config;
1299 uint32_t size;
6b8f1020 1300 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
085bccb7 1301
085bccb7
FK
1302 config = proxy->pci_dev.config;
1303 if (proxy->class_code) {
1304 pci_config_set_class(config, proxy->class_code);
1305 }
e266d421
GH
1306
1307 if (legacy) {
1308 /* legacy and transitional */
1309 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1310 pci_get_word(config + PCI_VENDOR_ID));
1311 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1312 } else {
1313 /* pure virtio-1.0 */
1314 pci_set_word(config + PCI_VENDOR_ID,
1315 PCI_VENDOR_ID_REDHAT_QUMRANET);
1316 pci_set_word(config + PCI_DEVICE_ID,
1317 0x1040 + virtio_bus_get_vdev_id(bus));
1318 pci_config_set_revision(config, 1);
1319 }
085bccb7
FK
1320 config[PCI_INTERRUPT_PIN] = 1;
1321
dfb8e184 1322
e266d421 1323 if (modern) {
cc52ea90
GH
1324 struct virtio_pci_cap cap = {
1325 .cap_len = sizeof cap,
dfb8e184
MT
1326 };
1327 struct virtio_pci_notify_cap notify = {
dfb8e184 1328 .cap.cap_len = sizeof notify,
dfb8e184
MT
1329 .notify_off_multiplier =
1330 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
1331 };
1332
dfb8e184 1333 /* TODO: add io access for speed */
dfb8e184
MT
1334
1335 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1141ce21 1336 virtio_pci_modern_regions_init(proxy);
cc52ea90
GH
1337 virtio_pci_modern_region_map(proxy, &proxy->common, &cap);
1338 virtio_pci_modern_region_map(proxy, &proxy->isr, &cap);
1339 virtio_pci_modern_region_map(proxy, &proxy->device, &cap);
54790d71 1340 virtio_pci_modern_region_map(proxy, &proxy->notify, &notify.cap);
b6ce27a5 1341 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
4e93a68e
GH
1342 PCI_BASE_ADDRESS_SPACE_MEMORY |
1343 PCI_BASE_ADDRESS_MEM_PREFETCH |
1344 PCI_BASE_ADDRESS_MEM_TYPE_64,
dfb8e184
MT
1345 &proxy->modern_bar);
1346 }
1347
085bccb7 1348 if (proxy->nvectors &&
b6ce27a5
GH
1349 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1350 proxy->msix_bar)) {
c7ff5482
FZ
1351 error_report("unable to init msix vectors to %" PRIu32,
1352 proxy->nvectors);
085bccb7
FK
1353 proxy->nvectors = 0;
1354 }
1355
1356 proxy->pci_dev.config_write = virtio_write_config;
1357
e266d421
GH
1358 if (legacy) {
1359 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1360 + virtio_bus_get_vdev_config_len(bus);
1361 if (size & (size - 1)) {
1362 size = 1 << qemu_fls(size);
1363 }
085bccb7 1364
e266d421
GH
1365 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1366 &virtio_pci_config_ops,
1367 proxy, "virtio-pci", size);
dfb8e184 1368
b6ce27a5 1369 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
23c5e397 1370 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
e266d421 1371 }
085bccb7
FK
1372
1373 if (!kvm_has_many_ioeventfds()) {
1374 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1375 }
1376
6b8f1020 1377 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
085bccb7
FK
1378}
1379
06a13073
PB
1380static void virtio_pci_device_unplugged(DeviceState *d)
1381{
06a13073
PB
1382 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1383
1384 virtio_pci_stop_ioeventfd(proxy);
06a13073
PB
1385}
1386
fc079951 1387static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
085bccb7 1388{
b6ce27a5 1389 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
085bccb7 1390 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
fc079951 1391
b6ce27a5
GH
1392 /*
1393 * virtio pci bar layout used by default.
1394 * subclasses can re-arrange things if needed.
1395 *
1396 * region 0 -- virtio legacy io bar
1397 * region 1 -- msi-x bar
1398 * region 4+5 -- virtio modern memory (64bit) bar
1399 *
1400 */
1401 proxy->legacy_io_bar = 0;
1402 proxy->msix_bar = 1;
1403 proxy->modern_mem_bar = 4;
1404
1405 proxy->common.offset = 0x0;
1406 proxy->common.size = 0x1000;
1407 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1408
1409 proxy->isr.offset = 0x1000;
1410 proxy->isr.size = 0x1000;
1411 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1412
1413 proxy->device.offset = 0x2000;
1414 proxy->device.size = 0x1000;
1415 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1416
1417 proxy->notify.offset = 0x3000;
1418 proxy->notify.size =
1419 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX;
1420 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1421
1422 /* subclasses can enforce modern, so do this unconditionally */
1423 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1424 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
1425 VIRTIO_QUEUE_MAX);
1426
1427 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
fc079951 1428 if (k->realize) {
b6ce27a5 1429 k->realize(proxy, errp);
085bccb7 1430 }
085bccb7
FK
1431}
1432
1433static void virtio_pci_exit(PCIDevice *pci_dev)
1434{
8b81bb3b 1435 msix_uninit_exclusive_bar(pci_dev);
085bccb7
FK
1436}
1437
59ccd20a 1438static void virtio_pci_reset(DeviceState *qdev)
085bccb7
FK
1439{
1440 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1441 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1442 virtio_pci_stop_ioeventfd(proxy);
1443 virtio_bus_reset(bus);
1444 msix_unuse_all_vectors(&proxy->pci_dev);
085bccb7
FK
1445}
1446
85d1277e 1447static Property virtio_pci_properties[] = {
68a27b20
MT
1448 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1449 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
e266d421
GH
1450 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags,
1451 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false),
1452 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags,
1453 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true),
85d1277e
ML
1454 DEFINE_PROP_END_OF_LIST(),
1455};
1456
085bccb7
FK
1457static void virtio_pci_class_init(ObjectClass *klass, void *data)
1458{
1459 DeviceClass *dc = DEVICE_CLASS(klass);
1460 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1461
85d1277e 1462 dc->props = virtio_pci_properties;
fc079951 1463 k->realize = virtio_pci_realize;
085bccb7
FK
1464 k->exit = virtio_pci_exit;
1465 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1466 k->revision = VIRTIO_PCI_ABI_VERSION;
1467 k->class_id = PCI_CLASS_OTHERS;
59ccd20a 1468 dc->reset = virtio_pci_reset;
085bccb7
FK
1469}
1470
1471static const TypeInfo virtio_pci_info = {
1472 .name = TYPE_VIRTIO_PCI,
1473 .parent = TYPE_PCI_DEVICE,
1474 .instance_size = sizeof(VirtIOPCIProxy),
1475 .class_init = virtio_pci_class_init,
1476 .class_size = sizeof(VirtioPCIClass),
1477 .abstract = true,
1478};
1479
653ced07
FK
1480/* virtio-blk-pci */
1481
1482static Property virtio_blk_pci_properties[] = {
c7bcc85d 1483 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
653ced07
FK
1484 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1485 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1486 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
653ced07
FK
1487 DEFINE_PROP_END_OF_LIST(),
1488};
1489
fc079951 1490static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
653ced07
FK
1491{
1492 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1493 DeviceState *vdev = DEVICE(&dev->vdev);
fc079951 1494
653ced07 1495 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 1496 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
653ced07
FK
1497}
1498
1499static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1500{
1501 DeviceClass *dc = DEVICE_CLASS(klass);
1502 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1503 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1504
125ee0ed 1505 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
653ced07 1506 dc->props = virtio_blk_pci_properties;
fc079951 1507 k->realize = virtio_blk_pci_realize;
653ced07
FK
1508 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1509 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1510 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1511 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1512}
1513
1514static void virtio_blk_pci_instance_init(Object *obj)
1515{
1516 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
c8075caf
GA
1517
1518 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1519 TYPE_VIRTIO_BLK);
467b3f33
SH
1520 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1521 &error_abort);
aeb98ddc
GA
1522 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1523 "bootindex", &error_abort);
653ced07
FK
1524}
1525
1526static const TypeInfo virtio_blk_pci_info = {
1527 .name = TYPE_VIRTIO_BLK_PCI,
1528 .parent = TYPE_VIRTIO_PCI,
1529 .instance_size = sizeof(VirtIOBlkPCI),
1530 .instance_init = virtio_blk_pci_instance_init,
1531 .class_init = virtio_blk_pci_class_init,
1532};
1533
bc7b90a0
FK
1534/* virtio-scsi-pci */
1535
1536static Property virtio_scsi_pci_properties[] = {
1537 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1538 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1539 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1540 DEV_NVECTORS_UNSPECIFIED),
bc7b90a0
FK
1541 DEFINE_PROP_END_OF_LIST(),
1542};
1543
fc079951 1544static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
bc7b90a0
FK
1545{
1546 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1547 DeviceState *vdev = DEVICE(&dev->vdev);
292c8e50 1548 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
6f32a6b4
FK
1549 DeviceState *proxy = DEVICE(vpci_dev);
1550 char *bus_name;
bc7b90a0
FK
1551
1552 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
292c8e50 1553 vpci_dev->nvectors = vs->conf.num_queues + 3;
bc7b90a0
FK
1554 }
1555
6f32a6b4
FK
1556 /*
1557 * For command line compatibility, this sets the virtio-scsi-device bus
1558 * name as before.
1559 */
1560 if (proxy->id) {
1561 bus_name = g_strdup_printf("%s.0", proxy->id);
1562 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1563 g_free(bus_name);
1564 }
1565
bc7b90a0 1566 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 1567 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
bc7b90a0
FK
1568}
1569
1570static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1571{
1572 DeviceClass *dc = DEVICE_CLASS(klass);
1573 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1574 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
fc079951
MA
1575
1576 k->realize = virtio_scsi_pci_realize;
125ee0ed 1577 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
bc7b90a0
FK
1578 dc->props = virtio_scsi_pci_properties;
1579 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1580 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1581 pcidev_k->revision = 0x00;
1582 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1583}
1584
1585static void virtio_scsi_pci_instance_init(Object *obj)
1586{
1587 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
c8075caf
GA
1588
1589 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1590 TYPE_VIRTIO_SCSI);
19d339f1
FZ
1591 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1592 &error_abort);
bc7b90a0
FK
1593}
1594
1595static const TypeInfo virtio_scsi_pci_info = {
1596 .name = TYPE_VIRTIO_SCSI_PCI,
1597 .parent = TYPE_VIRTIO_PCI,
1598 .instance_size = sizeof(VirtIOSCSIPCI),
1599 .instance_init = virtio_scsi_pci_instance_init,
1600 .class_init = virtio_scsi_pci_class_init,
1601};
1602
50787628
NB
1603/* vhost-scsi-pci */
1604
1605#ifdef CONFIG_VHOST_SCSI
1606static Property vhost_scsi_pci_properties[] = {
1607 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1608 DEV_NVECTORS_UNSPECIFIED),
50787628
NB
1609 DEFINE_PROP_END_OF_LIST(),
1610};
1611
fc079951 1612static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
50787628
NB
1613{
1614 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1615 DeviceState *vdev = DEVICE(&dev->vdev);
1616 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1617
1618 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1619 vpci_dev->nvectors = vs->conf.num_queues + 3;
1620 }
1621
1622 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 1623 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
50787628
NB
1624}
1625
1626static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1627{
1628 DeviceClass *dc = DEVICE_CLASS(klass);
1629 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1630 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
fc079951 1631 k->realize = vhost_scsi_pci_realize;
125ee0ed 1632 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
50787628
NB
1633 dc->props = vhost_scsi_pci_properties;
1634 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1635 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1636 pcidev_k->revision = 0x00;
1637 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1638}
1639
1640static void vhost_scsi_pci_instance_init(Object *obj)
1641{
1642 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
c8075caf
GA
1643
1644 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1645 TYPE_VHOST_SCSI);
d4433f32
GA
1646 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1647 "bootindex", &error_abort);
50787628
NB
1648}
1649
1650static const TypeInfo vhost_scsi_pci_info = {
1651 .name = TYPE_VHOST_SCSI_PCI,
1652 .parent = TYPE_VIRTIO_PCI,
1653 .instance_size = sizeof(VHostSCSIPCI),
1654 .instance_init = vhost_scsi_pci_instance_init,
1655 .class_init = vhost_scsi_pci_class_init,
1656};
1657#endif
1658
e378e88d
FK
1659/* virtio-balloon-pci */
1660
1661static Property virtio_balloon_pci_properties[] = {
c7bcc85d 1662 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
e378e88d
FK
1663 DEFINE_PROP_END_OF_LIST(),
1664};
1665
fc079951 1666static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
e378e88d
FK
1667{
1668 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1669 DeviceState *vdev = DEVICE(&dev->vdev);
1670
1671 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1672 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1673 vpci_dev->class_code = PCI_CLASS_OTHERS;
1674 }
1675
1676 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 1677 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
e378e88d
FK
1678}
1679
1680static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1681{
1682 DeviceClass *dc = DEVICE_CLASS(klass);
1683 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1684 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
fc079951 1685 k->realize = virtio_balloon_pci_realize;
125ee0ed 1686 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
e378e88d
FK
1687 dc->props = virtio_balloon_pci_properties;
1688 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1689 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1690 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1691 pcidev_k->class_id = PCI_CLASS_OTHERS;
1692}
1693
1694static void virtio_balloon_pci_instance_init(Object *obj)
1695{
1696 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
39b87c7b 1697
a6027b0f
DL
1698 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1699 TYPE_VIRTIO_BALLOON);
39b87c7b
SZ
1700 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
1701 "guest-stats", &error_abort);
1702 object_property_add_alias(obj, "guest-stats-polling-interval",
1703 OBJECT(&dev->vdev),
1704 "guest-stats-polling-interval", &error_abort);
e378e88d
FK
1705}
1706
1707static const TypeInfo virtio_balloon_pci_info = {
1708 .name = TYPE_VIRTIO_BALLOON_PCI,
1709 .parent = TYPE_VIRTIO_PCI,
1710 .instance_size = sizeof(VirtIOBalloonPCI),
1711 .instance_init = virtio_balloon_pci_instance_init,
1712 .class_init = virtio_balloon_pci_class_init,
1713};
1714
f7f7464a
FK
1715/* virtio-serial-pci */
1716
fc079951 1717static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
f7f7464a
FK
1718{
1719 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1720 DeviceState *vdev = DEVICE(&dev->vdev);
80270a19
FK
1721 DeviceState *proxy = DEVICE(vpci_dev);
1722 char *bus_name;
f7f7464a
FK
1723
1724 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1725 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1726 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
1727 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1728 }
1729
1730 /* backwards-compatibility with machines that were created with
1731 DEV_NVECTORS_UNSPECIFIED */
1732 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1733 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1734 }
1735
80270a19
FK
1736 /*
1737 * For command line compatibility, this sets the virtio-serial-device bus
1738 * name as before.
1739 */
1740 if (proxy->id) {
1741 bus_name = g_strdup_printf("%s.0", proxy->id);
1742 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1743 g_free(bus_name);
1744 }
1745
f7f7464a 1746 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 1747 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
f7f7464a
FK
1748}
1749
1750static Property virtio_serial_pci_properties[] = {
1751 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1752 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1753 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
c7bcc85d 1754 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
f7f7464a
FK
1755 DEFINE_PROP_END_OF_LIST(),
1756};
1757
1758static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1759{
1760 DeviceClass *dc = DEVICE_CLASS(klass);
1761 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1762 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
fc079951 1763 k->realize = virtio_serial_pci_realize;
125ee0ed 1764 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
f7f7464a
FK
1765 dc->props = virtio_serial_pci_properties;
1766 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1767 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1768 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1769 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1770}
1771
1772static void virtio_serial_pci_instance_init(Object *obj)
1773{
1774 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
c8075caf
GA
1775
1776 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1777 TYPE_VIRTIO_SERIAL);
f7f7464a
FK
1778}
1779
1780static const TypeInfo virtio_serial_pci_info = {
1781 .name = TYPE_VIRTIO_SERIAL_PCI,
1782 .parent = TYPE_VIRTIO_PCI,
1783 .instance_size = sizeof(VirtIOSerialPCI),
1784 .instance_init = virtio_serial_pci_instance_init,
1785 .class_init = virtio_serial_pci_class_init,
1786};
1787
e37da394
FK
1788/* virtio-net-pci */
1789
1790static Property virtio_net_properties[] = {
1791 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1792 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1793 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
e37da394
FK
1794 DEFINE_PROP_END_OF_LIST(),
1795};
1796
fc079951 1797static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
e37da394 1798{
800ced8c 1799 DeviceState *qdev = DEVICE(vpci_dev);
e37da394
FK
1800 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1801 DeviceState *vdev = DEVICE(&dev->vdev);
1802
800ced8c
FK
1803 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1804 object_get_typename(OBJECT(qdev)));
e37da394 1805 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951 1806 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
e37da394
FK
1807}
1808
1809static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1810{
1811 DeviceClass *dc = DEVICE_CLASS(klass);
1812 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1813 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1814
1815 k->romfile = "efi-virtio.rom";
1816 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1817 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1818 k->revision = VIRTIO_PCI_ABI_VERSION;
1819 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
125ee0ed 1820 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
e37da394 1821 dc->props = virtio_net_properties;
fc079951 1822 vpciklass->realize = virtio_net_pci_realize;
e37da394
FK
1823}
1824
1825static void virtio_net_pci_instance_init(Object *obj)
1826{
1827 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
c8075caf
GA
1828
1829 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1830 TYPE_VIRTIO_NET);
0cf63c3e
GA
1831 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1832 "bootindex", &error_abort);
e37da394
FK
1833}
1834
1835static const TypeInfo virtio_net_pci_info = {
1836 .name = TYPE_VIRTIO_NET_PCI,
1837 .parent = TYPE_VIRTIO_PCI,
1838 .instance_size = sizeof(VirtIONetPCI),
1839 .instance_init = virtio_net_pci_instance_init,
1840 .class_init = virtio_net_pci_class_init,
1841};
1842
59ccd20a
FK
1843/* virtio-rng-pci */
1844
1845static Property virtio_rng_pci_properties[] = {
59ccd20a
FK
1846 DEFINE_PROP_END_OF_LIST(),
1847};
1848
fc079951 1849static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
59ccd20a
FK
1850{
1851 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
1852 DeviceState *vdev = DEVICE(&vrng->vdev);
fc079951 1853 Error *err = NULL;
59ccd20a
FK
1854
1855 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
fc079951
MA
1856 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1857 if (err) {
1858 error_propagate(errp, err);
1859 return;
59ccd20a
FK
1860 }
1861
1862 object_property_set_link(OBJECT(vrng),
5b456438 1863 OBJECT(vrng->vdev.conf.rng), "rng",
59ccd20a 1864 NULL);
59ccd20a
FK
1865}
1866
1867static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
1868{
1869 DeviceClass *dc = DEVICE_CLASS(klass);
1870 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1871 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1872
fc079951 1873 k->realize = virtio_rng_pci_realize;
125ee0ed 1874 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
59ccd20a
FK
1875 dc->props = virtio_rng_pci_properties;
1876
1877 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1878 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1879 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1880 pcidev_k->class_id = PCI_CLASS_OTHERS;
1881}
1882
1883static void virtio_rng_initfn(Object *obj)
1884{
1885 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
c8075caf
GA
1886
1887 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1888 TYPE_VIRTIO_RNG);
cbd5ac69
PB
1889 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
1890 &error_abort);
59ccd20a
FK
1891}
1892
1893static const TypeInfo virtio_rng_pci_info = {
1894 .name = TYPE_VIRTIO_RNG_PCI,
1895 .parent = TYPE_VIRTIO_PCI,
1896 .instance_size = sizeof(VirtIORngPCI),
1897 .instance_init = virtio_rng_initfn,
1898 .class_init = virtio_rng_pci_class_init,
1899};
1900
f958c8aa
GH
1901/* virtio-input-pci */
1902
6f2b9a5b 1903static Property virtio_input_pci_properties[] = {
710e2d90
GH
1904 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1905 DEFINE_PROP_END_OF_LIST(),
1906};
1907
f958c8aa
GH
1908static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1909{
1910 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
1911 DeviceState *vdev = DEVICE(&vinput->vdev);
1912
1913 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1914 /* force virtio-1.0 */
1915 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
1916 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
1917 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1918}
1919
1920static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
1921{
1922 DeviceClass *dc = DEVICE_CLASS(klass);
1923 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1924 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1925
6f2b9a5b 1926 dc->props = virtio_input_pci_properties;
f958c8aa
GH
1927 k->realize = virtio_input_pci_realize;
1928 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1929
1930 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
1931}
1932
710e2d90
GH
1933static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
1934{
1935 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1936
1937 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
1938}
1939
1940static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
1941 void *data)
1942{
1943 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1944
1945 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
1946}
1947
1948static void virtio_keyboard_initfn(Object *obj)
1949{
1950 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
6f2b9a5b
GH
1951
1952 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1953 TYPE_VIRTIO_KEYBOARD);
710e2d90
GH
1954}
1955
1956static void virtio_mouse_initfn(Object *obj)
1957{
1958 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
6f2b9a5b
GH
1959
1960 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1961 TYPE_VIRTIO_MOUSE);
710e2d90
GH
1962}
1963
1964static void virtio_tablet_initfn(Object *obj)
1965{
1966 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
6f2b9a5b
GH
1967
1968 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1969 TYPE_VIRTIO_TABLET);
710e2d90
GH
1970}
1971
006a5ede
GH
1972static void virtio_host_initfn(Object *obj)
1973{
1974 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
1975
1976 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1977 TYPE_VIRTIO_INPUT_HOST);
1978}
1979
f958c8aa
GH
1980static const TypeInfo virtio_input_pci_info = {
1981 .name = TYPE_VIRTIO_INPUT_PCI,
1982 .parent = TYPE_VIRTIO_PCI,
1983 .instance_size = sizeof(VirtIOInputPCI),
1984 .class_init = virtio_input_pci_class_init,
1985 .abstract = true,
1986};
1987
710e2d90
GH
1988static const TypeInfo virtio_input_hid_pci_info = {
1989 .name = TYPE_VIRTIO_INPUT_HID_PCI,
1990 .parent = TYPE_VIRTIO_INPUT_PCI,
1991 .instance_size = sizeof(VirtIOInputHIDPCI),
710e2d90
GH
1992 .abstract = true,
1993};
1994
1995static const TypeInfo virtio_keyboard_pci_info = {
1996 .name = TYPE_VIRTIO_KEYBOARD_PCI,
1997 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
1998 .class_init = virtio_input_hid_kbd_pci_class_init,
1999 .instance_size = sizeof(VirtIOInputHIDPCI),
2000 .instance_init = virtio_keyboard_initfn,
2001};
2002
2003static const TypeInfo virtio_mouse_pci_info = {
2004 .name = TYPE_VIRTIO_MOUSE_PCI,
2005 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2006 .class_init = virtio_input_hid_mouse_pci_class_init,
2007 .instance_size = sizeof(VirtIOInputHIDPCI),
2008 .instance_init = virtio_mouse_initfn,
2009};
2010
2011static const TypeInfo virtio_tablet_pci_info = {
2012 .name = TYPE_VIRTIO_TABLET_PCI,
2013 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2014 .instance_size = sizeof(VirtIOInputHIDPCI),
2015 .instance_init = virtio_tablet_initfn,
2016};
2017
006a5ede
GH
2018static const TypeInfo virtio_host_pci_info = {
2019 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2020 .parent = TYPE_VIRTIO_INPUT_PCI,
2021 .instance_size = sizeof(VirtIOInputHostPCI),
2022 .instance_init = virtio_host_initfn,
2023};
2024
0a2acf5e
FK
2025/* virtio-pci-bus */
2026
ac7af112
AF
2027static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2028 VirtIOPCIProxy *dev)
0a2acf5e
FK
2029{
2030 DeviceState *qdev = DEVICE(dev);
f4dd69aa
FK
2031 char virtio_bus_name[] = "virtio-bus";
2032
fb17dfe0 2033 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
f4dd69aa 2034 virtio_bus_name);
0a2acf5e
FK
2035}
2036
2037static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2038{
2039 BusClass *bus_class = BUS_CLASS(klass);
2040 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2041 bus_class->max_dev = 1;
2042 k->notify = virtio_pci_notify;
2043 k->save_config = virtio_pci_save_config;
2044 k->load_config = virtio_pci_load_config;
2045 k->save_queue = virtio_pci_save_queue;
2046 k->load_queue = virtio_pci_load_queue;
0a2acf5e
FK
2047 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2048 k->set_host_notifier = virtio_pci_set_host_notifier;
2049 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2050 k->vmstate_change = virtio_pci_vmstate_change;
085bccb7 2051 k->device_plugged = virtio_pci_device_plugged;
06a13073 2052 k->device_unplugged = virtio_pci_device_unplugged;
e0d686bf 2053 k->query_nvectors = virtio_pci_query_nvectors;
0a2acf5e
FK
2054}
2055
2056static const TypeInfo virtio_pci_bus_info = {
2057 .name = TYPE_VIRTIO_PCI_BUS,
2058 .parent = TYPE_VIRTIO_BUS,
2059 .instance_size = sizeof(VirtioPCIBusState),
2060 .class_init = virtio_pci_bus_class_init,
2061};
2062
83f7d43a 2063static void virtio_pci_register_types(void)
53c25cea 2064{
59ccd20a 2065 type_register_static(&virtio_rng_pci_info);
f958c8aa 2066 type_register_static(&virtio_input_pci_info);
710e2d90
GH
2067 type_register_static(&virtio_input_hid_pci_info);
2068 type_register_static(&virtio_keyboard_pci_info);
2069 type_register_static(&virtio_mouse_pci_info);
2070 type_register_static(&virtio_tablet_pci_info);
006a5ede 2071 type_register_static(&virtio_host_pci_info);
0a2acf5e 2072 type_register_static(&virtio_pci_bus_info);
085bccb7 2073 type_register_static(&virtio_pci_info);
60653b28 2074#ifdef CONFIG_VIRTFS
234a336f 2075 type_register_static(&virtio_9p_pci_info);
60653b28 2076#endif
653ced07 2077 type_register_static(&virtio_blk_pci_info);
bc7b90a0 2078 type_register_static(&virtio_scsi_pci_info);
e378e88d 2079 type_register_static(&virtio_balloon_pci_info);
f7f7464a 2080 type_register_static(&virtio_serial_pci_info);
e37da394 2081 type_register_static(&virtio_net_pci_info);
50787628
NB
2082#ifdef CONFIG_VHOST_SCSI
2083 type_register_static(&vhost_scsi_pci_info);
2084#endif
53c25cea
PB
2085}
2086
83f7d43a 2087type_init(virtio_pci_register_types)