1 /* 2 * Virtio PCI Bindings 3 * 4 * Copyright IBM, Corp. 2007 5 * Copyright (c) 2009 CodeSourcery 6 * 7 * Authors: 8 * Anthony Liguori <aliguori@us.ibm.com> 9 * Paul Brook <paul@codesourcery.com> 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2. See 12 * the COPYING file in the top-level directory. 13 * 14 * Contributions after 2012-01-13 are licensed under the terms of the 15 * GNU GPL, version 2 or (at your option) any later version. 16 */ 17 18 #include "qemu/osdep.h" 19 20 #include "standard-headers/linux/virtio_pci.h" 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" 27 #include "hw/virtio/virtio-input.h" 28 #include "hw/pci/pci.h" 29 #include "qapi/error.h" 30 #include "qemu/error-report.h" 31 #include "hw/pci/msi.h" 32 #include "hw/pci/msix.h" 33 #include "hw/loader.h" 34 #include "sysemu/kvm.h" 35 #include "sysemu/block-backend.h" 36 #include "virtio-pci.h" 37 #include "qemu/range.h" 38 #include "hw/virtio/virtio-bus.h" 39 #include "qapi/visitor.h" 40 41 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev)) 42 43 #undef VIRTIO_PCI_CONFIG 44 45 /* The remaining space is defined by each driver as the per-driver 46 * configuration space */ 47 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev)) 48 49 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 50 VirtIOPCIProxy *dev); 51 static void virtio_pci_reset(DeviceState *qdev); 52 53 /* virtio device */ 54 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ 55 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) 56 { 57 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 58 } 59 60 /* DeviceState to VirtIOPCIProxy. Note: used on datapath, 61 * be careful and test performance if you change this. 62 */ 63 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) 64 { 65 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 66 } 67 68 static void virtio_pci_notify(DeviceState *d, uint16_t vector) 69 { 70 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); 71 72 if (msix_enabled(&proxy->pci_dev)) 73 msix_notify(&proxy->pci_dev, vector); 74 else { 75 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 76 pci_set_irq(&proxy->pci_dev, vdev->isr & 1); 77 } 78 } 79 80 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) 81 { 82 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 83 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 84 85 pci_device_save(&proxy->pci_dev, f); 86 msix_save(&proxy->pci_dev, f); 87 if (msix_present(&proxy->pci_dev)) 88 qemu_put_be16(f, vdev->config_vector); 89 } 90 91 static void virtio_pci_load_modern_queue_state(VirtIOPCIQueue *vq, 92 QEMUFile *f) 93 { 94 vq->num = qemu_get_be16(f); 95 vq->enabled = qemu_get_be16(f); 96 vq->desc[0] = qemu_get_be32(f); 97 vq->desc[1] = qemu_get_be32(f); 98 vq->avail[0] = qemu_get_be32(f); 99 vq->avail[1] = qemu_get_be32(f); 100 vq->used[0] = qemu_get_be32(f); 101 vq->used[1] = qemu_get_be32(f); 102 } 103 104 static bool virtio_pci_has_extra_state(DeviceState *d) 105 { 106 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 107 108 return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA; 109 } 110 111 static int get_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size) 112 { 113 VirtIOPCIProxy *proxy = pv; 114 int i; 115 116 proxy->dfselect = qemu_get_be32(f); 117 proxy->gfselect = qemu_get_be32(f); 118 proxy->guest_features[0] = qemu_get_be32(f); 119 proxy->guest_features[1] = qemu_get_be32(f); 120 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { 121 virtio_pci_load_modern_queue_state(&proxy->vqs[i], f); 122 } 123 124 return 0; 125 } 126 127 static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue *vq, 128 QEMUFile *f) 129 { 130 qemu_put_be16(f, vq->num); 131 qemu_put_be16(f, vq->enabled); 132 qemu_put_be32(f, vq->desc[0]); 133 qemu_put_be32(f, vq->desc[1]); 134 qemu_put_be32(f, vq->avail[0]); 135 qemu_put_be32(f, vq->avail[1]); 136 qemu_put_be32(f, vq->used[0]); 137 qemu_put_be32(f, vq->used[1]); 138 } 139 140 static void put_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size) 141 { 142 VirtIOPCIProxy *proxy = pv; 143 int i; 144 145 qemu_put_be32(f, proxy->dfselect); 146 qemu_put_be32(f, proxy->gfselect); 147 qemu_put_be32(f, proxy->guest_features[0]); 148 qemu_put_be32(f, proxy->guest_features[1]); 149 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { 150 virtio_pci_save_modern_queue_state(&proxy->vqs[i], f); 151 } 152 } 153 154 static const VMStateInfo vmstate_info_virtio_pci_modern_state = { 155 .name = "virtqueue_state", 156 .get = get_virtio_pci_modern_state, 157 .put = put_virtio_pci_modern_state, 158 }; 159 160 static bool virtio_pci_modern_state_needed(void *opaque) 161 { 162 VirtIOPCIProxy *proxy = opaque; 163 164 return !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN); 165 } 166 167 static const VMStateDescription vmstate_virtio_pci_modern_state = { 168 .name = "virtio_pci/modern_state", 169 .version_id = 1, 170 .minimum_version_id = 1, 171 .needed = &virtio_pci_modern_state_needed, 172 .fields = (VMStateField[]) { 173 { 174 .name = "modern_state", 175 .version_id = 0, 176 .field_exists = NULL, 177 .size = 0, 178 .info = &vmstate_info_virtio_pci_modern_state, 179 .flags = VMS_SINGLE, 180 .offset = 0, 181 }, 182 VMSTATE_END_OF_LIST() 183 } 184 }; 185 186 static const VMStateDescription vmstate_virtio_pci = { 187 .name = "virtio_pci", 188 .version_id = 1, 189 .minimum_version_id = 1, 190 .minimum_version_id_old = 1, 191 .fields = (VMStateField[]) { 192 VMSTATE_END_OF_LIST() 193 }, 194 .subsections = (const VMStateDescription*[]) { 195 &vmstate_virtio_pci_modern_state, 196 NULL 197 } 198 }; 199 200 static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f) 201 { 202 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 203 204 vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL); 205 } 206 207 static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f) 208 { 209 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 210 211 return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1); 212 } 213 214 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) 215 { 216 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 217 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 218 219 if (msix_present(&proxy->pci_dev)) 220 qemu_put_be16(f, virtio_queue_vector(vdev, n)); 221 } 222 223 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) 224 { 225 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 226 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 227 228 int ret; 229 ret = pci_device_load(&proxy->pci_dev, f); 230 if (ret) { 231 return ret; 232 } 233 msix_unuse_all_vectors(&proxy->pci_dev); 234 msix_load(&proxy->pci_dev, f); 235 if (msix_present(&proxy->pci_dev)) { 236 qemu_get_be16s(f, &vdev->config_vector); 237 } else { 238 vdev->config_vector = VIRTIO_NO_VECTOR; 239 } 240 if (vdev->config_vector != VIRTIO_NO_VECTOR) { 241 return msix_vector_use(&proxy->pci_dev, vdev->config_vector); 242 } 243 return 0; 244 } 245 246 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) 247 { 248 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 249 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 250 251 uint16_t vector; 252 if (msix_present(&proxy->pci_dev)) { 253 qemu_get_be16s(f, &vector); 254 } else { 255 vector = VIRTIO_NO_VECTOR; 256 } 257 virtio_queue_set_vector(vdev, n, vector); 258 if (vector != VIRTIO_NO_VECTOR) { 259 return msix_vector_use(&proxy->pci_dev, vector); 260 } 261 262 return 0; 263 } 264 265 static bool virtio_pci_ioeventfd_started(DeviceState *d) 266 { 267 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 268 269 return proxy->ioeventfd_started; 270 } 271 272 static void virtio_pci_ioeventfd_set_started(DeviceState *d, bool started, 273 bool err) 274 { 275 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 276 277 proxy->ioeventfd_started = started; 278 } 279 280 static bool virtio_pci_ioeventfd_disabled(DeviceState *d) 281 { 282 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 283 284 return proxy->ioeventfd_disabled || 285 !(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD); 286 } 287 288 static void virtio_pci_ioeventfd_set_disabled(DeviceState *d, bool disabled) 289 { 290 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 291 292 proxy->ioeventfd_disabled = disabled; 293 } 294 295 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000 296 297 static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier, 298 int n, bool assign) 299 { 300 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 301 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 302 VirtQueue *vq = virtio_get_queue(vdev, n); 303 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY); 304 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN); 305 bool fast_mmio = kvm_ioeventfd_any_length_enabled(); 306 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; 307 MemoryRegion *modern_mr = &proxy->notify.mr; 308 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr; 309 MemoryRegion *legacy_mr = &proxy->bar; 310 hwaddr modern_addr = QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * 311 virtio_get_queue_index(vq); 312 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY; 313 314 if (assign) { 315 if (modern) { 316 if (fast_mmio) { 317 memory_region_add_eventfd(modern_mr, modern_addr, 0, 318 false, n, notifier); 319 } else { 320 memory_region_add_eventfd(modern_mr, modern_addr, 2, 321 false, n, notifier); 322 } 323 if (modern_pio) { 324 memory_region_add_eventfd(modern_notify_mr, 0, 2, 325 true, n, notifier); 326 } 327 } 328 if (legacy) { 329 memory_region_add_eventfd(legacy_mr, legacy_addr, 2, 330 true, n, notifier); 331 } 332 } else { 333 if (modern) { 334 if (fast_mmio) { 335 memory_region_del_eventfd(modern_mr, modern_addr, 0, 336 false, n, notifier); 337 } else { 338 memory_region_del_eventfd(modern_mr, modern_addr, 2, 339 false, n, notifier); 340 } 341 if (modern_pio) { 342 memory_region_del_eventfd(modern_notify_mr, 0, 2, 343 true, n, notifier); 344 } 345 } 346 if (legacy) { 347 memory_region_del_eventfd(legacy_mr, legacy_addr, 2, 348 true, n, notifier); 349 } 350 } 351 return 0; 352 } 353 354 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) 355 { 356 virtio_bus_start_ioeventfd(&proxy->bus); 357 } 358 359 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) 360 { 361 virtio_bus_stop_ioeventfd(&proxy->bus); 362 } 363 364 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) 365 { 366 VirtIOPCIProxy *proxy = opaque; 367 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 368 hwaddr pa; 369 370 switch (addr) { 371 case VIRTIO_PCI_GUEST_FEATURES: 372 /* Guest does not negotiate properly? We have to assume nothing. */ 373 if (val & (1 << VIRTIO_F_BAD_FEATURE)) { 374 val = virtio_bus_get_vdev_bad_features(&proxy->bus); 375 } 376 virtio_set_features(vdev, val); 377 break; 378 case VIRTIO_PCI_QUEUE_PFN: 379 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; 380 if (pa == 0) { 381 virtio_pci_reset(DEVICE(proxy)); 382 } 383 else 384 virtio_queue_set_addr(vdev, vdev->queue_sel, pa); 385 break; 386 case VIRTIO_PCI_QUEUE_SEL: 387 if (val < VIRTIO_QUEUE_MAX) 388 vdev->queue_sel = val; 389 break; 390 case VIRTIO_PCI_QUEUE_NOTIFY: 391 if (val < VIRTIO_QUEUE_MAX) { 392 virtio_queue_notify(vdev, val); 393 } 394 break; 395 case VIRTIO_PCI_STATUS: 396 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 397 virtio_pci_stop_ioeventfd(proxy); 398 } 399 400 virtio_set_status(vdev, val & 0xFF); 401 402 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 403 virtio_pci_start_ioeventfd(proxy); 404 } 405 406 if (vdev->status == 0) { 407 virtio_pci_reset(DEVICE(proxy)); 408 } 409 410 /* Linux before 2.6.34 drives the device without enabling 411 the PCI device bus master bit. Enable it automatically 412 for the guest. This is a PCI spec violation but so is 413 initiating DMA with bus master bit clear. */ 414 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) { 415 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, 416 proxy->pci_dev.config[PCI_COMMAND] | 417 PCI_COMMAND_MASTER, 1); 418 } 419 break; 420 case VIRTIO_MSI_CONFIG_VECTOR: 421 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 422 /* Make it possible for guest to discover an error took place. */ 423 if (msix_vector_use(&proxy->pci_dev, val) < 0) 424 val = VIRTIO_NO_VECTOR; 425 vdev->config_vector = val; 426 break; 427 case VIRTIO_MSI_QUEUE_VECTOR: 428 msix_vector_unuse(&proxy->pci_dev, 429 virtio_queue_vector(vdev, vdev->queue_sel)); 430 /* Make it possible for guest to discover an error took place. */ 431 if (msix_vector_use(&proxy->pci_dev, val) < 0) 432 val = VIRTIO_NO_VECTOR; 433 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 434 break; 435 default: 436 error_report("%s: unexpected address 0x%x value 0x%x", 437 __func__, addr, val); 438 break; 439 } 440 } 441 442 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) 443 { 444 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 445 uint32_t ret = 0xFFFFFFFF; 446 447 switch (addr) { 448 case VIRTIO_PCI_HOST_FEATURES: 449 ret = vdev->host_features; 450 break; 451 case VIRTIO_PCI_GUEST_FEATURES: 452 ret = vdev->guest_features; 453 break; 454 case VIRTIO_PCI_QUEUE_PFN: 455 ret = virtio_queue_get_addr(vdev, vdev->queue_sel) 456 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; 457 break; 458 case VIRTIO_PCI_QUEUE_NUM: 459 ret = virtio_queue_get_num(vdev, vdev->queue_sel); 460 break; 461 case VIRTIO_PCI_QUEUE_SEL: 462 ret = vdev->queue_sel; 463 break; 464 case VIRTIO_PCI_STATUS: 465 ret = vdev->status; 466 break; 467 case VIRTIO_PCI_ISR: 468 /* reading from the ISR also clears it. */ 469 ret = vdev->isr; 470 vdev->isr = 0; 471 pci_irq_deassert(&proxy->pci_dev); 472 break; 473 case VIRTIO_MSI_CONFIG_VECTOR: 474 ret = vdev->config_vector; 475 break; 476 case VIRTIO_MSI_QUEUE_VECTOR: 477 ret = virtio_queue_vector(vdev, vdev->queue_sel); 478 break; 479 default: 480 break; 481 } 482 483 return ret; 484 } 485 486 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, 487 unsigned size) 488 { 489 VirtIOPCIProxy *proxy = opaque; 490 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 491 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); 492 uint64_t val = 0; 493 if (addr < config) { 494 return virtio_ioport_read(proxy, addr); 495 } 496 addr -= config; 497 498 switch (size) { 499 case 1: 500 val = virtio_config_readb(vdev, addr); 501 break; 502 case 2: 503 val = virtio_config_readw(vdev, addr); 504 if (virtio_is_big_endian(vdev)) { 505 val = bswap16(val); 506 } 507 break; 508 case 4: 509 val = virtio_config_readl(vdev, addr); 510 if (virtio_is_big_endian(vdev)) { 511 val = bswap32(val); 512 } 513 break; 514 } 515 return val; 516 } 517 518 static void virtio_pci_config_write(void *opaque, hwaddr addr, 519 uint64_t val, unsigned size) 520 { 521 VirtIOPCIProxy *proxy = opaque; 522 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); 523 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 524 if (addr < config) { 525 virtio_ioport_write(proxy, addr, val); 526 return; 527 } 528 addr -= config; 529 /* 530 * Virtio-PCI is odd. Ioports are LE but config space is target native 531 * endian. 532 */ 533 switch (size) { 534 case 1: 535 virtio_config_writeb(vdev, addr, val); 536 break; 537 case 2: 538 if (virtio_is_big_endian(vdev)) { 539 val = bswap16(val); 540 } 541 virtio_config_writew(vdev, addr, val); 542 break; 543 case 4: 544 if (virtio_is_big_endian(vdev)) { 545 val = bswap32(val); 546 } 547 virtio_config_writel(vdev, addr, val); 548 break; 549 } 550 } 551 552 static const MemoryRegionOps virtio_pci_config_ops = { 553 .read = virtio_pci_config_read, 554 .write = virtio_pci_config_write, 555 .impl = { 556 .min_access_size = 1, 557 .max_access_size = 4, 558 }, 559 .endianness = DEVICE_LITTLE_ENDIAN, 560 }; 561 562 /* Below are generic functions to do memcpy from/to an address space, 563 * without byteswaps, with input validation. 564 * 565 * As regular address_space_* APIs all do some kind of byteswap at least for 566 * some host/target combinations, we are forced to explicitly convert to a 567 * known-endianness integer value. 568 * It doesn't really matter which endian format to go through, so the code 569 * below selects the endian that causes the least amount of work on the given 570 * host. 571 * 572 * Note: host pointer must be aligned. 573 */ 574 static 575 void virtio_address_space_write(AddressSpace *as, hwaddr addr, 576 const uint8_t *buf, int len) 577 { 578 uint32_t val; 579 580 /* address_space_* APIs assume an aligned address. 581 * As address is under guest control, handle illegal values. 582 */ 583 addr &= ~(len - 1); 584 585 /* Make sure caller aligned buf properly */ 586 assert(!(((uintptr_t)buf) & (len - 1))); 587 588 switch (len) { 589 case 1: 590 val = pci_get_byte(buf); 591 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); 592 break; 593 case 2: 594 val = pci_get_word(buf); 595 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); 596 break; 597 case 4: 598 val = pci_get_long(buf); 599 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); 600 break; 601 default: 602 /* As length is under guest control, handle illegal values. */ 603 break; 604 } 605 } 606 607 static void 608 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len) 609 { 610 uint32_t val; 611 612 /* address_space_* APIs assume an aligned address. 613 * As address is under guest control, handle illegal values. 614 */ 615 addr &= ~(len - 1); 616 617 /* Make sure caller aligned buf properly */ 618 assert(!(((uintptr_t)buf) & (len - 1))); 619 620 switch (len) { 621 case 1: 622 val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); 623 pci_set_byte(buf, val); 624 break; 625 case 2: 626 val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); 627 pci_set_word(buf, val); 628 break; 629 case 4: 630 val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); 631 pci_set_long(buf, val); 632 break; 633 default: 634 /* As length is under guest control, handle illegal values. */ 635 break; 636 } 637 } 638 639 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, 640 uint32_t val, int len) 641 { 642 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 643 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 644 struct virtio_pci_cfg_cap *cfg; 645 646 pci_default_write_config(pci_dev, address, val, len); 647 648 if (range_covers_byte(address, len, PCI_COMMAND) && 649 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 650 virtio_pci_stop_ioeventfd(proxy); 651 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); 652 } 653 654 if (proxy->config_cap && 655 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap, 656 pci_cfg_data), 657 sizeof cfg->pci_cfg_data)) { 658 uint32_t off; 659 uint32_t len; 660 661 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap); 662 off = le32_to_cpu(cfg->cap.offset); 663 len = le32_to_cpu(cfg->cap.length); 664 665 if (len == 1 || len == 2 || len == 4) { 666 assert(len <= sizeof cfg->pci_cfg_data); 667 virtio_address_space_write(&proxy->modern_as, off, 668 cfg->pci_cfg_data, len); 669 } 670 } 671 } 672 673 static uint32_t virtio_read_config(PCIDevice *pci_dev, 674 uint32_t address, int len) 675 { 676 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 677 struct virtio_pci_cfg_cap *cfg; 678 679 if (proxy->config_cap && 680 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap, 681 pci_cfg_data), 682 sizeof cfg->pci_cfg_data)) { 683 uint32_t off; 684 uint32_t len; 685 686 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap); 687 off = le32_to_cpu(cfg->cap.offset); 688 len = le32_to_cpu(cfg->cap.length); 689 690 if (len == 1 || len == 2 || len == 4) { 691 assert(len <= sizeof cfg->pci_cfg_data); 692 virtio_address_space_read(&proxy->modern_as, off, 693 cfg->pci_cfg_data, len); 694 } 695 } 696 697 return pci_default_read_config(pci_dev, address, len); 698 } 699 700 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, 701 unsigned int queue_no, 702 unsigned int vector, 703 MSIMessage msg) 704 { 705 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 706 int ret; 707 708 if (irqfd->users == 0) { 709 ret = kvm_irqchip_add_msi_route(kvm_state, msg, &proxy->pci_dev); 710 if (ret < 0) { 711 return ret; 712 } 713 irqfd->virq = ret; 714 } 715 irqfd->users++; 716 return 0; 717 } 718 719 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, 720 unsigned int vector) 721 { 722 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 723 if (--irqfd->users == 0) { 724 kvm_irqchip_release_virq(kvm_state, irqfd->virq); 725 } 726 } 727 728 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, 729 unsigned int queue_no, 730 unsigned int vector) 731 { 732 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 733 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 734 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 735 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 736 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq); 737 } 738 739 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, 740 unsigned int queue_no, 741 unsigned int vector) 742 { 743 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 744 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 745 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 746 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 747 int ret; 748 749 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq); 750 assert(ret == 0); 751 } 752 753 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) 754 { 755 PCIDevice *dev = &proxy->pci_dev; 756 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 757 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 758 unsigned int vector; 759 int ret, queue_no; 760 MSIMessage msg; 761 762 for (queue_no = 0; queue_no < nvqs; queue_no++) { 763 if (!virtio_queue_get_num(vdev, queue_no)) { 764 break; 765 } 766 vector = virtio_queue_vector(vdev, queue_no); 767 if (vector >= msix_nr_vectors_allocated(dev)) { 768 continue; 769 } 770 msg = msix_get_message(dev, vector); 771 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); 772 if (ret < 0) { 773 goto undo; 774 } 775 /* If guest supports masking, set up irqfd now. 776 * Otherwise, delay until unmasked in the frontend. 777 */ 778 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 779 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 780 if (ret < 0) { 781 kvm_virtio_pci_vq_vector_release(proxy, vector); 782 goto undo; 783 } 784 } 785 } 786 return 0; 787 788 undo: 789 while (--queue_no >= 0) { 790 vector = virtio_queue_vector(vdev, queue_no); 791 if (vector >= msix_nr_vectors_allocated(dev)) { 792 continue; 793 } 794 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 795 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 796 } 797 kvm_virtio_pci_vq_vector_release(proxy, vector); 798 } 799 return ret; 800 } 801 802 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) 803 { 804 PCIDevice *dev = &proxy->pci_dev; 805 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 806 unsigned int vector; 807 int queue_no; 808 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 809 810 for (queue_no = 0; queue_no < nvqs; queue_no++) { 811 if (!virtio_queue_get_num(vdev, queue_no)) { 812 break; 813 } 814 vector = virtio_queue_vector(vdev, queue_no); 815 if (vector >= msix_nr_vectors_allocated(dev)) { 816 continue; 817 } 818 /* If guest supports masking, clean up irqfd now. 819 * Otherwise, it was cleaned when masked in the frontend. 820 */ 821 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 822 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 823 } 824 kvm_virtio_pci_vq_vector_release(proxy, vector); 825 } 826 } 827 828 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, 829 unsigned int queue_no, 830 unsigned int vector, 831 MSIMessage msg) 832 { 833 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 834 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 835 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 836 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 837 VirtIOIRQFD *irqfd; 838 int ret = 0; 839 840 if (proxy->vector_irqfd) { 841 irqfd = &proxy->vector_irqfd[vector]; 842 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { 843 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg, 844 &proxy->pci_dev); 845 if (ret < 0) { 846 return ret; 847 } 848 } 849 } 850 851 /* If guest supports masking, irqfd is already setup, unmask it. 852 * Otherwise, set it up now. 853 */ 854 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 855 k->guest_notifier_mask(vdev, queue_no, false); 856 /* Test after unmasking to avoid losing events. */ 857 if (k->guest_notifier_pending && 858 k->guest_notifier_pending(vdev, queue_no)) { 859 event_notifier_set(n); 860 } 861 } else { 862 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 863 } 864 return ret; 865 } 866 867 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, 868 unsigned int queue_no, 869 unsigned int vector) 870 { 871 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 872 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 873 874 /* If guest supports masking, keep irqfd but mask it. 875 * Otherwise, clean it up now. 876 */ 877 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 878 k->guest_notifier_mask(vdev, queue_no, true); 879 } else { 880 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 881 } 882 } 883 884 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, 885 MSIMessage msg) 886 { 887 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 888 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 889 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 890 int ret, index, unmasked = 0; 891 892 while (vq) { 893 index = virtio_get_queue_index(vq); 894 if (!virtio_queue_get_num(vdev, index)) { 895 break; 896 } 897 if (index < proxy->nvqs_with_notifiers) { 898 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg); 899 if (ret < 0) { 900 goto undo; 901 } 902 ++unmasked; 903 } 904 vq = virtio_vector_next_queue(vq); 905 } 906 907 return 0; 908 909 undo: 910 vq = virtio_vector_first_queue(vdev, vector); 911 while (vq && unmasked >= 0) { 912 index = virtio_get_queue_index(vq); 913 if (index < proxy->nvqs_with_notifiers) { 914 virtio_pci_vq_vector_mask(proxy, index, vector); 915 --unmasked; 916 } 917 vq = virtio_vector_next_queue(vq); 918 } 919 return ret; 920 } 921 922 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) 923 { 924 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 925 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 926 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 927 int index; 928 929 while (vq) { 930 index = virtio_get_queue_index(vq); 931 if (!virtio_queue_get_num(vdev, index)) { 932 break; 933 } 934 if (index < proxy->nvqs_with_notifiers) { 935 virtio_pci_vq_vector_mask(proxy, index, vector); 936 } 937 vq = virtio_vector_next_queue(vq); 938 } 939 } 940 941 static void virtio_pci_vector_poll(PCIDevice *dev, 942 unsigned int vector_start, 943 unsigned int vector_end) 944 { 945 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 946 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 947 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 948 int queue_no; 949 unsigned int vector; 950 EventNotifier *notifier; 951 VirtQueue *vq; 952 953 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 954 if (!virtio_queue_get_num(vdev, queue_no)) { 955 break; 956 } 957 vector = virtio_queue_vector(vdev, queue_no); 958 if (vector < vector_start || vector >= vector_end || 959 !msix_is_masked(dev, vector)) { 960 continue; 961 } 962 vq = virtio_get_queue(vdev, queue_no); 963 notifier = virtio_queue_get_guest_notifier(vq); 964 if (k->guest_notifier_pending) { 965 if (k->guest_notifier_pending(vdev, queue_no)) { 966 msix_set_pending(dev, vector); 967 } 968 } else if (event_notifier_test_and_clear(notifier)) { 969 msix_set_pending(dev, vector); 970 } 971 } 972 } 973 974 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, 975 bool with_irqfd) 976 { 977 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 978 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 979 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 980 VirtQueue *vq = virtio_get_queue(vdev, n); 981 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 982 983 if (assign) { 984 int r = event_notifier_init(notifier, 0); 985 if (r < 0) { 986 return r; 987 } 988 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 989 } else { 990 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 991 event_notifier_cleanup(notifier); 992 } 993 994 if (!msix_enabled(&proxy->pci_dev) && 995 vdev->use_guest_notifier_mask && 996 vdc->guest_notifier_mask) { 997 vdc->guest_notifier_mask(vdev, n, !assign); 998 } 999 1000 return 0; 1001 } 1002 1003 static bool virtio_pci_query_guest_notifiers(DeviceState *d) 1004 { 1005 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 1006 return msix_enabled(&proxy->pci_dev); 1007 } 1008 1009 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) 1010 { 1011 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 1012 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1013 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 1014 int r, n; 1015 bool with_irqfd = msix_enabled(&proxy->pci_dev) && 1016 kvm_msi_via_irqfd_enabled(); 1017 1018 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX); 1019 1020 /* When deassigning, pass a consistent nvqs value 1021 * to avoid leaking notifiers. 1022 */ 1023 assert(assign || nvqs == proxy->nvqs_with_notifiers); 1024 1025 proxy->nvqs_with_notifiers = nvqs; 1026 1027 /* Must unset vector notifier while guest notifier is still assigned */ 1028 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) { 1029 msix_unset_vector_notifiers(&proxy->pci_dev); 1030 if (proxy->vector_irqfd) { 1031 kvm_virtio_pci_vector_release(proxy, nvqs); 1032 g_free(proxy->vector_irqfd); 1033 proxy->vector_irqfd = NULL; 1034 } 1035 } 1036 1037 for (n = 0; n < nvqs; n++) { 1038 if (!virtio_queue_get_num(vdev, n)) { 1039 break; 1040 } 1041 1042 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd); 1043 if (r < 0) { 1044 goto assign_error; 1045 } 1046 } 1047 1048 /* Must set vector notifier after guest notifier has been assigned */ 1049 if ((with_irqfd || k->guest_notifier_mask) && assign) { 1050 if (with_irqfd) { 1051 proxy->vector_irqfd = 1052 g_malloc0(sizeof(*proxy->vector_irqfd) * 1053 msix_nr_vectors_allocated(&proxy->pci_dev)); 1054 r = kvm_virtio_pci_vector_use(proxy, nvqs); 1055 if (r < 0) { 1056 goto assign_error; 1057 } 1058 } 1059 r = msix_set_vector_notifiers(&proxy->pci_dev, 1060 virtio_pci_vector_unmask, 1061 virtio_pci_vector_mask, 1062 virtio_pci_vector_poll); 1063 if (r < 0) { 1064 goto notifiers_error; 1065 } 1066 } 1067 1068 return 0; 1069 1070 notifiers_error: 1071 if (with_irqfd) { 1072 assert(assign); 1073 kvm_virtio_pci_vector_release(proxy, nvqs); 1074 } 1075 1076 assign_error: 1077 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ 1078 assert(assign); 1079 while (--n >= 0) { 1080 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); 1081 } 1082 return r; 1083 } 1084 1085 static void virtio_pci_vmstate_change(DeviceState *d, bool running) 1086 { 1087 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 1088 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1089 1090 if (running) { 1091 /* Old QEMU versions did not set bus master enable on status write. 1092 * Detect DRIVER set and enable it. 1093 */ 1094 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) && 1095 (vdev->status & VIRTIO_CONFIG_S_DRIVER) && 1096 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 1097 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, 1098 proxy->pci_dev.config[PCI_COMMAND] | 1099 PCI_COMMAND_MASTER, 1); 1100 } 1101 virtio_pci_start_ioeventfd(proxy); 1102 } else { 1103 virtio_pci_stop_ioeventfd(proxy); 1104 } 1105 } 1106 1107 #ifdef CONFIG_VIRTFS 1108 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1109 { 1110 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev); 1111 DeviceState *vdev = DEVICE(&dev->vdev); 1112 1113 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1114 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1115 } 1116 1117 static Property virtio_9p_pci_properties[] = { 1118 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1119 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1120 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1121 DEFINE_PROP_END_OF_LIST(), 1122 }; 1123 1124 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data) 1125 { 1126 DeviceClass *dc = DEVICE_CLASS(klass); 1127 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1128 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1129 1130 k->realize = virtio_9p_pci_realize; 1131 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1132 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P; 1133 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1134 pcidev_k->class_id = 0x2; 1135 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1136 dc->props = virtio_9p_pci_properties; 1137 } 1138 1139 static void virtio_9p_pci_instance_init(Object *obj) 1140 { 1141 V9fsPCIState *dev = VIRTIO_9P_PCI(obj); 1142 1143 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1144 TYPE_VIRTIO_9P); 1145 } 1146 1147 static const TypeInfo virtio_9p_pci_info = { 1148 .name = TYPE_VIRTIO_9P_PCI, 1149 .parent = TYPE_VIRTIO_PCI, 1150 .instance_size = sizeof(V9fsPCIState), 1151 .instance_init = virtio_9p_pci_instance_init, 1152 .class_init = virtio_9p_pci_class_init, 1153 }; 1154 #endif /* CONFIG_VIRTFS */ 1155 1156 /* 1157 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 1158 */ 1159 1160 static int virtio_pci_query_nvectors(DeviceState *d) 1161 { 1162 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1163 1164 return proxy->nvectors; 1165 } 1166 1167 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy, 1168 struct virtio_pci_cap *cap) 1169 { 1170 PCIDevice *dev = &proxy->pci_dev; 1171 int offset; 1172 1173 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len); 1174 assert(offset > 0); 1175 1176 assert(cap->cap_len >= sizeof *cap); 1177 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len, 1178 cap->cap_len - PCI_CAP_FLAGS); 1179 1180 return offset; 1181 } 1182 1183 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr, 1184 unsigned size) 1185 { 1186 VirtIOPCIProxy *proxy = opaque; 1187 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1188 uint32_t val = 0; 1189 int i; 1190 1191 switch (addr) { 1192 case VIRTIO_PCI_COMMON_DFSELECT: 1193 val = proxy->dfselect; 1194 break; 1195 case VIRTIO_PCI_COMMON_DF: 1196 if (proxy->dfselect <= 1) { 1197 val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >> 1198 (32 * proxy->dfselect); 1199 } 1200 break; 1201 case VIRTIO_PCI_COMMON_GFSELECT: 1202 val = proxy->gfselect; 1203 break; 1204 case VIRTIO_PCI_COMMON_GF: 1205 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) { 1206 val = proxy->guest_features[proxy->gfselect]; 1207 } 1208 break; 1209 case VIRTIO_PCI_COMMON_MSIX: 1210 val = vdev->config_vector; 1211 break; 1212 case VIRTIO_PCI_COMMON_NUMQ: 1213 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) { 1214 if (virtio_queue_get_num(vdev, i)) { 1215 val = i + 1; 1216 } 1217 } 1218 break; 1219 case VIRTIO_PCI_COMMON_STATUS: 1220 val = vdev->status; 1221 break; 1222 case VIRTIO_PCI_COMMON_CFGGENERATION: 1223 val = vdev->generation; 1224 break; 1225 case VIRTIO_PCI_COMMON_Q_SELECT: 1226 val = vdev->queue_sel; 1227 break; 1228 case VIRTIO_PCI_COMMON_Q_SIZE: 1229 val = virtio_queue_get_num(vdev, vdev->queue_sel); 1230 break; 1231 case VIRTIO_PCI_COMMON_Q_MSIX: 1232 val = virtio_queue_vector(vdev, vdev->queue_sel); 1233 break; 1234 case VIRTIO_PCI_COMMON_Q_ENABLE: 1235 val = proxy->vqs[vdev->queue_sel].enabled; 1236 break; 1237 case VIRTIO_PCI_COMMON_Q_NOFF: 1238 /* Simply map queues in order */ 1239 val = vdev->queue_sel; 1240 break; 1241 case VIRTIO_PCI_COMMON_Q_DESCLO: 1242 val = proxy->vqs[vdev->queue_sel].desc[0]; 1243 break; 1244 case VIRTIO_PCI_COMMON_Q_DESCHI: 1245 val = proxy->vqs[vdev->queue_sel].desc[1]; 1246 break; 1247 case VIRTIO_PCI_COMMON_Q_AVAILLO: 1248 val = proxy->vqs[vdev->queue_sel].avail[0]; 1249 break; 1250 case VIRTIO_PCI_COMMON_Q_AVAILHI: 1251 val = proxy->vqs[vdev->queue_sel].avail[1]; 1252 break; 1253 case VIRTIO_PCI_COMMON_Q_USEDLO: 1254 val = proxy->vqs[vdev->queue_sel].used[0]; 1255 break; 1256 case VIRTIO_PCI_COMMON_Q_USEDHI: 1257 val = proxy->vqs[vdev->queue_sel].used[1]; 1258 break; 1259 default: 1260 val = 0; 1261 } 1262 1263 return val; 1264 } 1265 1266 static void virtio_pci_common_write(void *opaque, hwaddr addr, 1267 uint64_t val, unsigned size) 1268 { 1269 VirtIOPCIProxy *proxy = opaque; 1270 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1271 1272 switch (addr) { 1273 case VIRTIO_PCI_COMMON_DFSELECT: 1274 proxy->dfselect = val; 1275 break; 1276 case VIRTIO_PCI_COMMON_GFSELECT: 1277 proxy->gfselect = val; 1278 break; 1279 case VIRTIO_PCI_COMMON_GF: 1280 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) { 1281 proxy->guest_features[proxy->gfselect] = val; 1282 virtio_set_features(vdev, 1283 (((uint64_t)proxy->guest_features[1]) << 32) | 1284 proxy->guest_features[0]); 1285 } 1286 break; 1287 case VIRTIO_PCI_COMMON_MSIX: 1288 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 1289 /* Make it possible for guest to discover an error took place. */ 1290 if (msix_vector_use(&proxy->pci_dev, val) < 0) { 1291 val = VIRTIO_NO_VECTOR; 1292 } 1293 vdev->config_vector = val; 1294 break; 1295 case VIRTIO_PCI_COMMON_STATUS: 1296 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 1297 virtio_pci_stop_ioeventfd(proxy); 1298 } 1299 1300 virtio_set_status(vdev, val & 0xFF); 1301 1302 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 1303 virtio_pci_start_ioeventfd(proxy); 1304 } 1305 1306 if (vdev->status == 0) { 1307 virtio_pci_reset(DEVICE(proxy)); 1308 } 1309 1310 break; 1311 case VIRTIO_PCI_COMMON_Q_SELECT: 1312 if (val < VIRTIO_QUEUE_MAX) { 1313 vdev->queue_sel = val; 1314 } 1315 break; 1316 case VIRTIO_PCI_COMMON_Q_SIZE: 1317 proxy->vqs[vdev->queue_sel].num = val; 1318 break; 1319 case VIRTIO_PCI_COMMON_Q_MSIX: 1320 msix_vector_unuse(&proxy->pci_dev, 1321 virtio_queue_vector(vdev, vdev->queue_sel)); 1322 /* Make it possible for guest to discover an error took place. */ 1323 if (msix_vector_use(&proxy->pci_dev, val) < 0) { 1324 val = VIRTIO_NO_VECTOR; 1325 } 1326 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 1327 break; 1328 case VIRTIO_PCI_COMMON_Q_ENABLE: 1329 /* TODO: need a way to put num back on reset. */ 1330 virtio_queue_set_num(vdev, vdev->queue_sel, 1331 proxy->vqs[vdev->queue_sel].num); 1332 virtio_queue_set_rings(vdev, vdev->queue_sel, 1333 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 | 1334 proxy->vqs[vdev->queue_sel].desc[0], 1335 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 | 1336 proxy->vqs[vdev->queue_sel].avail[0], 1337 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 | 1338 proxy->vqs[vdev->queue_sel].used[0]); 1339 proxy->vqs[vdev->queue_sel].enabled = 1; 1340 break; 1341 case VIRTIO_PCI_COMMON_Q_DESCLO: 1342 proxy->vqs[vdev->queue_sel].desc[0] = val; 1343 break; 1344 case VIRTIO_PCI_COMMON_Q_DESCHI: 1345 proxy->vqs[vdev->queue_sel].desc[1] = val; 1346 break; 1347 case VIRTIO_PCI_COMMON_Q_AVAILLO: 1348 proxy->vqs[vdev->queue_sel].avail[0] = val; 1349 break; 1350 case VIRTIO_PCI_COMMON_Q_AVAILHI: 1351 proxy->vqs[vdev->queue_sel].avail[1] = val; 1352 break; 1353 case VIRTIO_PCI_COMMON_Q_USEDLO: 1354 proxy->vqs[vdev->queue_sel].used[0] = val; 1355 break; 1356 case VIRTIO_PCI_COMMON_Q_USEDHI: 1357 proxy->vqs[vdev->queue_sel].used[1] = val; 1358 break; 1359 default: 1360 break; 1361 } 1362 } 1363 1364 1365 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr, 1366 unsigned size) 1367 { 1368 return 0; 1369 } 1370 1371 static void virtio_pci_notify_write(void *opaque, hwaddr addr, 1372 uint64_t val, unsigned size) 1373 { 1374 VirtIODevice *vdev = opaque; 1375 unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT; 1376 1377 if (queue < VIRTIO_QUEUE_MAX) { 1378 virtio_queue_notify(vdev, queue); 1379 } 1380 } 1381 1382 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr, 1383 uint64_t val, unsigned size) 1384 { 1385 VirtIODevice *vdev = opaque; 1386 unsigned queue = val; 1387 1388 if (queue < VIRTIO_QUEUE_MAX) { 1389 virtio_queue_notify(vdev, queue); 1390 } 1391 } 1392 1393 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr, 1394 unsigned size) 1395 { 1396 VirtIOPCIProxy *proxy = opaque; 1397 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1398 uint64_t val = vdev->isr; 1399 1400 vdev->isr = 0; 1401 pci_irq_deassert(&proxy->pci_dev); 1402 1403 return val; 1404 } 1405 1406 static void virtio_pci_isr_write(void *opaque, hwaddr addr, 1407 uint64_t val, unsigned size) 1408 { 1409 } 1410 1411 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr, 1412 unsigned size) 1413 { 1414 VirtIODevice *vdev = opaque; 1415 uint64_t val = 0; 1416 1417 switch (size) { 1418 case 1: 1419 val = virtio_config_modern_readb(vdev, addr); 1420 break; 1421 case 2: 1422 val = virtio_config_modern_readw(vdev, addr); 1423 break; 1424 case 4: 1425 val = virtio_config_modern_readl(vdev, addr); 1426 break; 1427 } 1428 return val; 1429 } 1430 1431 static void virtio_pci_device_write(void *opaque, hwaddr addr, 1432 uint64_t val, unsigned size) 1433 { 1434 VirtIODevice *vdev = opaque; 1435 switch (size) { 1436 case 1: 1437 virtio_config_modern_writeb(vdev, addr, val); 1438 break; 1439 case 2: 1440 virtio_config_modern_writew(vdev, addr, val); 1441 break; 1442 case 4: 1443 virtio_config_modern_writel(vdev, addr, val); 1444 break; 1445 } 1446 } 1447 1448 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy) 1449 { 1450 static const MemoryRegionOps common_ops = { 1451 .read = virtio_pci_common_read, 1452 .write = virtio_pci_common_write, 1453 .impl = { 1454 .min_access_size = 1, 1455 .max_access_size = 4, 1456 }, 1457 .endianness = DEVICE_LITTLE_ENDIAN, 1458 }; 1459 static const MemoryRegionOps isr_ops = { 1460 .read = virtio_pci_isr_read, 1461 .write = virtio_pci_isr_write, 1462 .impl = { 1463 .min_access_size = 1, 1464 .max_access_size = 4, 1465 }, 1466 .endianness = DEVICE_LITTLE_ENDIAN, 1467 }; 1468 static const MemoryRegionOps device_ops = { 1469 .read = virtio_pci_device_read, 1470 .write = virtio_pci_device_write, 1471 .impl = { 1472 .min_access_size = 1, 1473 .max_access_size = 4, 1474 }, 1475 .endianness = DEVICE_LITTLE_ENDIAN, 1476 }; 1477 static const MemoryRegionOps notify_ops = { 1478 .read = virtio_pci_notify_read, 1479 .write = virtio_pci_notify_write, 1480 .impl = { 1481 .min_access_size = 1, 1482 .max_access_size = 4, 1483 }, 1484 .endianness = DEVICE_LITTLE_ENDIAN, 1485 }; 1486 static const MemoryRegionOps notify_pio_ops = { 1487 .read = virtio_pci_notify_read, 1488 .write = virtio_pci_notify_write_pio, 1489 .impl = { 1490 .min_access_size = 1, 1491 .max_access_size = 4, 1492 }, 1493 .endianness = DEVICE_LITTLE_ENDIAN, 1494 }; 1495 1496 1497 memory_region_init_io(&proxy->common.mr, OBJECT(proxy), 1498 &common_ops, 1499 proxy, 1500 "virtio-pci-common", 1501 proxy->common.size); 1502 1503 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy), 1504 &isr_ops, 1505 proxy, 1506 "virtio-pci-isr", 1507 proxy->isr.size); 1508 1509 memory_region_init_io(&proxy->device.mr, OBJECT(proxy), 1510 &device_ops, 1511 virtio_bus_get_device(&proxy->bus), 1512 "virtio-pci-device", 1513 proxy->device.size); 1514 1515 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy), 1516 ¬ify_ops, 1517 virtio_bus_get_device(&proxy->bus), 1518 "virtio-pci-notify", 1519 proxy->notify.size); 1520 1521 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy), 1522 ¬ify_pio_ops, 1523 virtio_bus_get_device(&proxy->bus), 1524 "virtio-pci-notify-pio", 1525 proxy->notify.size); 1526 } 1527 1528 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy, 1529 VirtIOPCIRegion *region, 1530 struct virtio_pci_cap *cap, 1531 MemoryRegion *mr, 1532 uint8_t bar) 1533 { 1534 memory_region_add_subregion(mr, region->offset, ®ion->mr); 1535 1536 cap->cfg_type = region->type; 1537 cap->bar = bar; 1538 cap->offset = cpu_to_le32(region->offset); 1539 cap->length = cpu_to_le32(region->size); 1540 virtio_pci_add_mem_cap(proxy, cap); 1541 1542 } 1543 1544 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy, 1545 VirtIOPCIRegion *region, 1546 struct virtio_pci_cap *cap) 1547 { 1548 virtio_pci_modern_region_map(proxy, region, cap, 1549 &proxy->modern_bar, proxy->modern_mem_bar); 1550 } 1551 1552 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy, 1553 VirtIOPCIRegion *region, 1554 struct virtio_pci_cap *cap) 1555 { 1556 virtio_pci_modern_region_map(proxy, region, cap, 1557 &proxy->io_bar, proxy->modern_io_bar); 1558 } 1559 1560 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy, 1561 VirtIOPCIRegion *region) 1562 { 1563 memory_region_del_subregion(&proxy->modern_bar, 1564 ®ion->mr); 1565 } 1566 1567 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy, 1568 VirtIOPCIRegion *region) 1569 { 1570 memory_region_del_subregion(&proxy->io_bar, 1571 ®ion->mr); 1572 } 1573 1574 /* This is called by virtio-bus just after the device is plugged. */ 1575 static void virtio_pci_device_plugged(DeviceState *d, Error **errp) 1576 { 1577 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1578 VirtioBusState *bus = &proxy->bus; 1579 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY); 1580 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN); 1581 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; 1582 uint8_t *config; 1583 uint32_t size; 1584 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1585 1586 config = proxy->pci_dev.config; 1587 if (proxy->class_code) { 1588 pci_config_set_class(config, proxy->class_code); 1589 } 1590 1591 if (legacy) { 1592 /* legacy and transitional */ 1593 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, 1594 pci_get_word(config + PCI_VENDOR_ID)); 1595 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); 1596 } else { 1597 /* pure virtio-1.0 */ 1598 pci_set_word(config + PCI_VENDOR_ID, 1599 PCI_VENDOR_ID_REDHAT_QUMRANET); 1600 pci_set_word(config + PCI_DEVICE_ID, 1601 0x1040 + virtio_bus_get_vdev_id(bus)); 1602 pci_config_set_revision(config, 1); 1603 } 1604 config[PCI_INTERRUPT_PIN] = 1; 1605 1606 1607 if (modern) { 1608 struct virtio_pci_cap cap = { 1609 .cap_len = sizeof cap, 1610 }; 1611 struct virtio_pci_notify_cap notify = { 1612 .cap.cap_len = sizeof notify, 1613 .notify_off_multiplier = 1614 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT), 1615 }; 1616 struct virtio_pci_cfg_cap cfg = { 1617 .cap.cap_len = sizeof cfg, 1618 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG, 1619 }; 1620 struct virtio_pci_notify_cap notify_pio = { 1621 .cap.cap_len = sizeof notify, 1622 .notify_off_multiplier = cpu_to_le32(0x0), 1623 }; 1624 1625 struct virtio_pci_cfg_cap *cfg_mask; 1626 1627 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); 1628 virtio_pci_modern_regions_init(proxy); 1629 1630 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap); 1631 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap); 1632 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap); 1633 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap); 1634 1635 if (modern_pio) { 1636 memory_region_init(&proxy->io_bar, OBJECT(proxy), 1637 "virtio-pci-io", 0x4); 1638 1639 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar, 1640 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar); 1641 1642 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio, 1643 ¬ify_pio.cap); 1644 } 1645 1646 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar, 1647 PCI_BASE_ADDRESS_SPACE_MEMORY | 1648 PCI_BASE_ADDRESS_MEM_PREFETCH | 1649 PCI_BASE_ADDRESS_MEM_TYPE_64, 1650 &proxy->modern_bar); 1651 1652 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap); 1653 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap); 1654 pci_set_byte(&cfg_mask->cap.bar, ~0x0); 1655 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0); 1656 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0); 1657 pci_set_long(cfg_mask->pci_cfg_data, ~0x0); 1658 } 1659 1660 if (proxy->nvectors) { 1661 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1662 proxy->msix_bar); 1663 if (err) { 1664 /* Notice when a system that supports MSIx can't initialize it. */ 1665 if (err != -ENOTSUP) { 1666 error_report("unable to init msix vectors to %" PRIu32, 1667 proxy->nvectors); 1668 } 1669 proxy->nvectors = 0; 1670 } 1671 } 1672 1673 proxy->pci_dev.config_write = virtio_write_config; 1674 proxy->pci_dev.config_read = virtio_read_config; 1675 1676 if (legacy) { 1677 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) 1678 + virtio_bus_get_vdev_config_len(bus); 1679 size = pow2ceil(size); 1680 1681 memory_region_init_io(&proxy->bar, OBJECT(proxy), 1682 &virtio_pci_config_ops, 1683 proxy, "virtio-pci", size); 1684 1685 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar, 1686 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar); 1687 } 1688 1689 if (!kvm_has_many_ioeventfds()) { 1690 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; 1691 } 1692 1693 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE); 1694 } 1695 1696 static void virtio_pci_device_unplugged(DeviceState *d) 1697 { 1698 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1699 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN); 1700 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; 1701 1702 virtio_pci_stop_ioeventfd(proxy); 1703 1704 if (modern) { 1705 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common); 1706 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr); 1707 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device); 1708 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify); 1709 if (modern_pio) { 1710 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio); 1711 } 1712 } 1713 } 1714 1715 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) 1716 { 1717 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 1718 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); 1719 1720 /* 1721 * virtio pci bar layout used by default. 1722 * subclasses can re-arrange things if needed. 1723 * 1724 * region 0 -- virtio legacy io bar 1725 * region 1 -- msi-x bar 1726 * region 4+5 -- virtio modern memory (64bit) bar 1727 * 1728 */ 1729 proxy->legacy_io_bar = 0; 1730 proxy->msix_bar = 1; 1731 proxy->modern_io_bar = 2; 1732 proxy->modern_mem_bar = 4; 1733 1734 proxy->common.offset = 0x0; 1735 proxy->common.size = 0x1000; 1736 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG; 1737 1738 proxy->isr.offset = 0x1000; 1739 proxy->isr.size = 0x1000; 1740 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG; 1741 1742 proxy->device.offset = 0x2000; 1743 proxy->device.size = 0x1000; 1744 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG; 1745 1746 proxy->notify.offset = 0x3000; 1747 proxy->notify.size = 1748 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX; 1749 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG; 1750 1751 proxy->notify_pio.offset = 0x0; 1752 proxy->notify_pio.size = 0x4; 1753 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG; 1754 1755 /* subclasses can enforce modern, so do this unconditionally */ 1756 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci", 1757 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * 1758 VIRTIO_QUEUE_MAX); 1759 1760 memory_region_init_alias(&proxy->modern_cfg, 1761 OBJECT(proxy), 1762 "virtio-pci-cfg", 1763 &proxy->modern_bar, 1764 0, 1765 memory_region_size(&proxy->modern_bar)); 1766 1767 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as"); 1768 1769 if (pci_is_express(pci_dev) && pci_bus_is_express(pci_dev->bus) && 1770 !pci_bus_is_root(pci_dev->bus)) { 1771 int pos; 1772 1773 pos = pcie_endpoint_cap_init(pci_dev, 0); 1774 assert(pos > 0); 1775 1776 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF); 1777 assert(pos > 0); 1778 1779 /* 1780 * Indicates that this function complies with revision 1.2 of the 1781 * PCI Power Management Interface Specification. 1782 */ 1783 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3); 1784 } else { 1785 /* 1786 * make future invocations of pci_is_express() return false 1787 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE. 1788 */ 1789 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS; 1790 } 1791 1792 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy); 1793 if (k->realize) { 1794 k->realize(proxy, errp); 1795 } 1796 } 1797 1798 static void virtio_pci_exit(PCIDevice *pci_dev) 1799 { 1800 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 1801 1802 msix_uninit_exclusive_bar(pci_dev); 1803 address_space_destroy(&proxy->modern_as); 1804 } 1805 1806 static void virtio_pci_reset(DeviceState *qdev) 1807 { 1808 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1809 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 1810 int i; 1811 1812 virtio_pci_stop_ioeventfd(proxy); 1813 virtio_bus_reset(bus); 1814 msix_unuse_all_vectors(&proxy->pci_dev); 1815 1816 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { 1817 proxy->vqs[i].enabled = 0; 1818 } 1819 } 1820 1821 static Property virtio_pci_properties[] = { 1822 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags, 1823 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false), 1824 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags, 1825 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false), 1826 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags, 1827 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true), 1828 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags, 1829 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true), 1830 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags, 1831 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false), 1832 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags, 1833 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false), 1834 DEFINE_PROP_END_OF_LIST(), 1835 }; 1836 1837 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp) 1838 { 1839 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev); 1840 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1841 PCIDevice *pci_dev = &proxy->pci_dev; 1842 1843 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) && 1844 !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN)) { 1845 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; 1846 } 1847 1848 vpciklass->parent_dc_realize(qdev, errp); 1849 } 1850 1851 static void virtio_pci_class_init(ObjectClass *klass, void *data) 1852 { 1853 DeviceClass *dc = DEVICE_CLASS(klass); 1854 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1855 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1856 1857 dc->props = virtio_pci_properties; 1858 k->realize = virtio_pci_realize; 1859 k->exit = virtio_pci_exit; 1860 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1861 k->revision = VIRTIO_PCI_ABI_VERSION; 1862 k->class_id = PCI_CLASS_OTHERS; 1863 vpciklass->parent_dc_realize = dc->realize; 1864 dc->realize = virtio_pci_dc_realize; 1865 dc->reset = virtio_pci_reset; 1866 } 1867 1868 static const TypeInfo virtio_pci_info = { 1869 .name = TYPE_VIRTIO_PCI, 1870 .parent = TYPE_PCI_DEVICE, 1871 .instance_size = sizeof(VirtIOPCIProxy), 1872 .class_init = virtio_pci_class_init, 1873 .class_size = sizeof(VirtioPCIClass), 1874 .abstract = true, 1875 }; 1876 1877 /* virtio-blk-pci */ 1878 1879 static Property virtio_blk_pci_properties[] = { 1880 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1881 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1882 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1883 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1884 DEFINE_PROP_END_OF_LIST(), 1885 }; 1886 1887 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1888 { 1889 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); 1890 DeviceState *vdev = DEVICE(&dev->vdev); 1891 1892 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1893 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1894 } 1895 1896 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) 1897 { 1898 DeviceClass *dc = DEVICE_CLASS(klass); 1899 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1900 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1901 1902 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1903 dc->props = virtio_blk_pci_properties; 1904 k->realize = virtio_blk_pci_realize; 1905 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1906 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; 1907 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1908 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1909 } 1910 1911 static void virtio_blk_pci_instance_init(Object *obj) 1912 { 1913 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); 1914 1915 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1916 TYPE_VIRTIO_BLK); 1917 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 1918 &error_abort); 1919 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1920 "bootindex", &error_abort); 1921 } 1922 1923 static const TypeInfo virtio_blk_pci_info = { 1924 .name = TYPE_VIRTIO_BLK_PCI, 1925 .parent = TYPE_VIRTIO_PCI, 1926 .instance_size = sizeof(VirtIOBlkPCI), 1927 .instance_init = virtio_blk_pci_instance_init, 1928 .class_init = virtio_blk_pci_class_init, 1929 }; 1930 1931 /* virtio-scsi-pci */ 1932 1933 static Property virtio_scsi_pci_properties[] = { 1934 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1935 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1936 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1937 DEV_NVECTORS_UNSPECIFIED), 1938 DEFINE_PROP_END_OF_LIST(), 1939 }; 1940 1941 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1942 { 1943 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); 1944 DeviceState *vdev = DEVICE(&dev->vdev); 1945 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1946 DeviceState *proxy = DEVICE(vpci_dev); 1947 char *bus_name; 1948 1949 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1950 vpci_dev->nvectors = vs->conf.num_queues + 3; 1951 } 1952 1953 /* 1954 * For command line compatibility, this sets the virtio-scsi-device bus 1955 * name as before. 1956 */ 1957 if (proxy->id) { 1958 bus_name = g_strdup_printf("%s.0", proxy->id); 1959 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1960 g_free(bus_name); 1961 } 1962 1963 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1964 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1965 } 1966 1967 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) 1968 { 1969 DeviceClass *dc = DEVICE_CLASS(klass); 1970 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1971 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1972 1973 k->realize = virtio_scsi_pci_realize; 1974 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1975 dc->props = virtio_scsi_pci_properties; 1976 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1977 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1978 pcidev_k->revision = 0x00; 1979 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1980 } 1981 1982 static void virtio_scsi_pci_instance_init(Object *obj) 1983 { 1984 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); 1985 1986 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1987 TYPE_VIRTIO_SCSI); 1988 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread", 1989 &error_abort); 1990 } 1991 1992 static const TypeInfo virtio_scsi_pci_info = { 1993 .name = TYPE_VIRTIO_SCSI_PCI, 1994 .parent = TYPE_VIRTIO_PCI, 1995 .instance_size = sizeof(VirtIOSCSIPCI), 1996 .instance_init = virtio_scsi_pci_instance_init, 1997 .class_init = virtio_scsi_pci_class_init, 1998 }; 1999 2000 /* vhost-scsi-pci */ 2001 2002 #ifdef CONFIG_VHOST_SCSI 2003 static Property vhost_scsi_pci_properties[] = { 2004 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2005 DEV_NVECTORS_UNSPECIFIED), 2006 DEFINE_PROP_END_OF_LIST(), 2007 }; 2008 2009 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2010 { 2011 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev); 2012 DeviceState *vdev = DEVICE(&dev->vdev); 2013 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 2014 2015 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 2016 vpci_dev->nvectors = vs->conf.num_queues + 3; 2017 } 2018 2019 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2020 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2021 } 2022 2023 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data) 2024 { 2025 DeviceClass *dc = DEVICE_CLASS(klass); 2026 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2027 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2028 k->realize = vhost_scsi_pci_realize; 2029 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 2030 dc->props = vhost_scsi_pci_properties; 2031 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2032 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 2033 pcidev_k->revision = 0x00; 2034 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 2035 } 2036 2037 static void vhost_scsi_pci_instance_init(Object *obj) 2038 { 2039 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj); 2040 2041 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2042 TYPE_VHOST_SCSI); 2043 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 2044 "bootindex", &error_abort); 2045 } 2046 2047 static const TypeInfo vhost_scsi_pci_info = { 2048 .name = TYPE_VHOST_SCSI_PCI, 2049 .parent = TYPE_VIRTIO_PCI, 2050 .instance_size = sizeof(VHostSCSIPCI), 2051 .instance_init = vhost_scsi_pci_instance_init, 2052 .class_init = vhost_scsi_pci_class_init, 2053 }; 2054 #endif 2055 2056 /* virtio-balloon-pci */ 2057 2058 static Property virtio_balloon_pci_properties[] = { 2059 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 2060 DEFINE_PROP_END_OF_LIST(), 2061 }; 2062 2063 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2064 { 2065 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); 2066 DeviceState *vdev = DEVICE(&dev->vdev); 2067 2068 if (vpci_dev->class_code != PCI_CLASS_OTHERS && 2069 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ 2070 vpci_dev->class_code = PCI_CLASS_OTHERS; 2071 } 2072 2073 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2074 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2075 } 2076 2077 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) 2078 { 2079 DeviceClass *dc = DEVICE_CLASS(klass); 2080 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2081 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2082 k->realize = virtio_balloon_pci_realize; 2083 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 2084 dc->props = virtio_balloon_pci_properties; 2085 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2086 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; 2087 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 2088 pcidev_k->class_id = PCI_CLASS_OTHERS; 2089 } 2090 2091 static void virtio_balloon_pci_instance_init(Object *obj) 2092 { 2093 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); 2094 2095 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2096 TYPE_VIRTIO_BALLOON); 2097 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev), 2098 "guest-stats", &error_abort); 2099 object_property_add_alias(obj, "guest-stats-polling-interval", 2100 OBJECT(&dev->vdev), 2101 "guest-stats-polling-interval", &error_abort); 2102 } 2103 2104 static const TypeInfo virtio_balloon_pci_info = { 2105 .name = TYPE_VIRTIO_BALLOON_PCI, 2106 .parent = TYPE_VIRTIO_PCI, 2107 .instance_size = sizeof(VirtIOBalloonPCI), 2108 .instance_init = virtio_balloon_pci_instance_init, 2109 .class_init = virtio_balloon_pci_class_init, 2110 }; 2111 2112 /* virtio-serial-pci */ 2113 2114 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2115 { 2116 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); 2117 DeviceState *vdev = DEVICE(&dev->vdev); 2118 DeviceState *proxy = DEVICE(vpci_dev); 2119 char *bus_name; 2120 2121 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && 2122 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ 2123 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ 2124 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; 2125 } 2126 2127 /* backwards-compatibility with machines that were created with 2128 DEV_NVECTORS_UNSPECIFIED */ 2129 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 2130 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; 2131 } 2132 2133 /* 2134 * For command line compatibility, this sets the virtio-serial-device bus 2135 * name as before. 2136 */ 2137 if (proxy->id) { 2138 bus_name = g_strdup_printf("%s.0", proxy->id); 2139 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 2140 g_free(bus_name); 2141 } 2142 2143 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2144 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2145 } 2146 2147 static Property virtio_serial_pci_properties[] = { 2148 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 2149 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 2150 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 2151 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 2152 DEFINE_PROP_END_OF_LIST(), 2153 }; 2154 2155 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) 2156 { 2157 DeviceClass *dc = DEVICE_CLASS(klass); 2158 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2159 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2160 k->realize = virtio_serial_pci_realize; 2161 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 2162 dc->props = virtio_serial_pci_properties; 2163 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2164 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; 2165 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 2166 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 2167 } 2168 2169 static void virtio_serial_pci_instance_init(Object *obj) 2170 { 2171 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); 2172 2173 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2174 TYPE_VIRTIO_SERIAL); 2175 } 2176 2177 static const TypeInfo virtio_serial_pci_info = { 2178 .name = TYPE_VIRTIO_SERIAL_PCI, 2179 .parent = TYPE_VIRTIO_PCI, 2180 .instance_size = sizeof(VirtIOSerialPCI), 2181 .instance_init = virtio_serial_pci_instance_init, 2182 .class_init = virtio_serial_pci_class_init, 2183 }; 2184 2185 /* virtio-net-pci */ 2186 2187 static Property virtio_net_properties[] = { 2188 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 2189 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), 2190 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 2191 DEFINE_PROP_END_OF_LIST(), 2192 }; 2193 2194 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2195 { 2196 DeviceState *qdev = DEVICE(vpci_dev); 2197 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); 2198 DeviceState *vdev = DEVICE(&dev->vdev); 2199 2200 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 2201 object_get_typename(OBJECT(qdev))); 2202 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2203 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2204 } 2205 2206 static void virtio_net_pci_class_init(ObjectClass *klass, void *data) 2207 { 2208 DeviceClass *dc = DEVICE_CLASS(klass); 2209 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 2210 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 2211 2212 k->romfile = "efi-virtio.rom"; 2213 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2214 k->device_id = PCI_DEVICE_ID_VIRTIO_NET; 2215 k->revision = VIRTIO_PCI_ABI_VERSION; 2216 k->class_id = PCI_CLASS_NETWORK_ETHERNET; 2217 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 2218 dc->props = virtio_net_properties; 2219 vpciklass->realize = virtio_net_pci_realize; 2220 } 2221 2222 static void virtio_net_pci_instance_init(Object *obj) 2223 { 2224 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); 2225 2226 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2227 TYPE_VIRTIO_NET); 2228 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 2229 "bootindex", &error_abort); 2230 } 2231 2232 static const TypeInfo virtio_net_pci_info = { 2233 .name = TYPE_VIRTIO_NET_PCI, 2234 .parent = TYPE_VIRTIO_PCI, 2235 .instance_size = sizeof(VirtIONetPCI), 2236 .instance_init = virtio_net_pci_instance_init, 2237 .class_init = virtio_net_pci_class_init, 2238 }; 2239 2240 /* virtio-rng-pci */ 2241 2242 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2243 { 2244 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); 2245 DeviceState *vdev = DEVICE(&vrng->vdev); 2246 Error *err = NULL; 2247 2248 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2249 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 2250 if (err) { 2251 error_propagate(errp, err); 2252 return; 2253 } 2254 2255 object_property_set_link(OBJECT(vrng), 2256 OBJECT(vrng->vdev.conf.rng), "rng", 2257 NULL); 2258 } 2259 2260 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) 2261 { 2262 DeviceClass *dc = DEVICE_CLASS(klass); 2263 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2264 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2265 2266 k->realize = virtio_rng_pci_realize; 2267 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 2268 2269 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2270 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; 2271 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 2272 pcidev_k->class_id = PCI_CLASS_OTHERS; 2273 } 2274 2275 static void virtio_rng_initfn(Object *obj) 2276 { 2277 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); 2278 2279 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2280 TYPE_VIRTIO_RNG); 2281 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng", 2282 &error_abort); 2283 } 2284 2285 static const TypeInfo virtio_rng_pci_info = { 2286 .name = TYPE_VIRTIO_RNG_PCI, 2287 .parent = TYPE_VIRTIO_PCI, 2288 .instance_size = sizeof(VirtIORngPCI), 2289 .instance_init = virtio_rng_initfn, 2290 .class_init = virtio_rng_pci_class_init, 2291 }; 2292 2293 /* virtio-input-pci */ 2294 2295 static Property virtio_input_pci_properties[] = { 2296 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 2297 DEFINE_PROP_END_OF_LIST(), 2298 }; 2299 2300 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2301 { 2302 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev); 2303 DeviceState *vdev = DEVICE(&vinput->vdev); 2304 2305 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2306 /* force virtio-1.0 */ 2307 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN; 2308 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY; 2309 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2310 } 2311 2312 static void virtio_input_pci_class_init(ObjectClass *klass, void *data) 2313 { 2314 DeviceClass *dc = DEVICE_CLASS(klass); 2315 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2316 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2317 2318 dc->props = virtio_input_pci_properties; 2319 k->realize = virtio_input_pci_realize; 2320 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 2321 2322 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER; 2323 } 2324 2325 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data) 2326 { 2327 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2328 2329 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD; 2330 } 2331 2332 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass, 2333 void *data) 2334 { 2335 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2336 2337 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE; 2338 } 2339 2340 static void virtio_keyboard_initfn(Object *obj) 2341 { 2342 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 2343 2344 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2345 TYPE_VIRTIO_KEYBOARD); 2346 } 2347 2348 static void virtio_mouse_initfn(Object *obj) 2349 { 2350 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 2351 2352 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2353 TYPE_VIRTIO_MOUSE); 2354 } 2355 2356 static void virtio_tablet_initfn(Object *obj) 2357 { 2358 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 2359 2360 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2361 TYPE_VIRTIO_TABLET); 2362 } 2363 2364 static const TypeInfo virtio_input_pci_info = { 2365 .name = TYPE_VIRTIO_INPUT_PCI, 2366 .parent = TYPE_VIRTIO_PCI, 2367 .instance_size = sizeof(VirtIOInputPCI), 2368 .class_init = virtio_input_pci_class_init, 2369 .abstract = true, 2370 }; 2371 2372 static const TypeInfo virtio_input_hid_pci_info = { 2373 .name = TYPE_VIRTIO_INPUT_HID_PCI, 2374 .parent = TYPE_VIRTIO_INPUT_PCI, 2375 .instance_size = sizeof(VirtIOInputHIDPCI), 2376 .abstract = true, 2377 }; 2378 2379 static const TypeInfo virtio_keyboard_pci_info = { 2380 .name = TYPE_VIRTIO_KEYBOARD_PCI, 2381 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2382 .class_init = virtio_input_hid_kbd_pci_class_init, 2383 .instance_size = sizeof(VirtIOInputHIDPCI), 2384 .instance_init = virtio_keyboard_initfn, 2385 }; 2386 2387 static const TypeInfo virtio_mouse_pci_info = { 2388 .name = TYPE_VIRTIO_MOUSE_PCI, 2389 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2390 .class_init = virtio_input_hid_mouse_pci_class_init, 2391 .instance_size = sizeof(VirtIOInputHIDPCI), 2392 .instance_init = virtio_mouse_initfn, 2393 }; 2394 2395 static const TypeInfo virtio_tablet_pci_info = { 2396 .name = TYPE_VIRTIO_TABLET_PCI, 2397 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2398 .instance_size = sizeof(VirtIOInputHIDPCI), 2399 .instance_init = virtio_tablet_initfn, 2400 }; 2401 2402 #ifdef CONFIG_LINUX 2403 static void virtio_host_initfn(Object *obj) 2404 { 2405 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj); 2406 2407 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2408 TYPE_VIRTIO_INPUT_HOST); 2409 } 2410 2411 static const TypeInfo virtio_host_pci_info = { 2412 .name = TYPE_VIRTIO_INPUT_HOST_PCI, 2413 .parent = TYPE_VIRTIO_INPUT_PCI, 2414 .instance_size = sizeof(VirtIOInputHostPCI), 2415 .instance_init = virtio_host_initfn, 2416 }; 2417 #endif 2418 2419 /* virtio-pci-bus */ 2420 2421 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 2422 VirtIOPCIProxy *dev) 2423 { 2424 DeviceState *qdev = DEVICE(dev); 2425 char virtio_bus_name[] = "virtio-bus"; 2426 2427 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 2428 virtio_bus_name); 2429 } 2430 2431 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 2432 { 2433 BusClass *bus_class = BUS_CLASS(klass); 2434 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 2435 bus_class->max_dev = 1; 2436 k->notify = virtio_pci_notify; 2437 k->save_config = virtio_pci_save_config; 2438 k->load_config = virtio_pci_load_config; 2439 k->save_queue = virtio_pci_save_queue; 2440 k->load_queue = virtio_pci_load_queue; 2441 k->save_extra_state = virtio_pci_save_extra_state; 2442 k->load_extra_state = virtio_pci_load_extra_state; 2443 k->has_extra_state = virtio_pci_has_extra_state; 2444 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 2445 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 2446 k->vmstate_change = virtio_pci_vmstate_change; 2447 k->device_plugged = virtio_pci_device_plugged; 2448 k->device_unplugged = virtio_pci_device_unplugged; 2449 k->query_nvectors = virtio_pci_query_nvectors; 2450 k->ioeventfd_started = virtio_pci_ioeventfd_started; 2451 k->ioeventfd_set_started = virtio_pci_ioeventfd_set_started; 2452 k->ioeventfd_disabled = virtio_pci_ioeventfd_disabled; 2453 k->ioeventfd_set_disabled = virtio_pci_ioeventfd_set_disabled; 2454 k->ioeventfd_assign = virtio_pci_ioeventfd_assign; 2455 } 2456 2457 static const TypeInfo virtio_pci_bus_info = { 2458 .name = TYPE_VIRTIO_PCI_BUS, 2459 .parent = TYPE_VIRTIO_BUS, 2460 .instance_size = sizeof(VirtioPCIBusState), 2461 .class_init = virtio_pci_bus_class_init, 2462 }; 2463 2464 static void virtio_pci_register_types(void) 2465 { 2466 type_register_static(&virtio_rng_pci_info); 2467 type_register_static(&virtio_input_pci_info); 2468 type_register_static(&virtio_input_hid_pci_info); 2469 type_register_static(&virtio_keyboard_pci_info); 2470 type_register_static(&virtio_mouse_pci_info); 2471 type_register_static(&virtio_tablet_pci_info); 2472 #ifdef CONFIG_LINUX 2473 type_register_static(&virtio_host_pci_info); 2474 #endif 2475 type_register_static(&virtio_pci_bus_info); 2476 type_register_static(&virtio_pci_info); 2477 #ifdef CONFIG_VIRTFS 2478 type_register_static(&virtio_9p_pci_info); 2479 #endif 2480 type_register_static(&virtio_blk_pci_info); 2481 type_register_static(&virtio_scsi_pci_info); 2482 type_register_static(&virtio_balloon_pci_info); 2483 type_register_static(&virtio_serial_pci_info); 2484 type_register_static(&virtio_net_pci_info); 2485 #ifdef CONFIG_VHOST_SCSI 2486 type_register_static(&vhost_scsi_pci_info); 2487 #endif 2488 } 2489 2490 type_init(virtio_pci_register_types) 2491