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