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