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