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, 1166 cap->cap_len, &error_abort); 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_enabled() && !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, 1814 PCI_PM_SIZEOF, errp); 1815 if (pos < 0) { 1816 return; 1817 } 1818 1819 pci_dev->exp.pm_cap = pos; 1820 1821 /* 1822 * Indicates that this function complies with revision 1.2 of the 1823 * PCI Power Management Interface Specification. 1824 */ 1825 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3); 1826 1827 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) { 1828 /* Init error enabling flags */ 1829 pcie_cap_deverr_init(pci_dev); 1830 } 1831 1832 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) { 1833 /* Init Link Control Register */ 1834 pcie_cap_lnkctl_init(pci_dev); 1835 } 1836 1837 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) { 1838 /* Init Power Management Control Register */ 1839 pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL, 1840 PCI_PM_CTRL_STATE_MASK); 1841 } 1842 1843 if (proxy->flags & VIRTIO_PCI_FLAG_ATS) { 1844 pcie_ats_init(pci_dev, 256); 1845 } 1846 1847 } else { 1848 /* 1849 * make future invocations of pci_is_express() return false 1850 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE. 1851 */ 1852 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS; 1853 } 1854 1855 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy); 1856 if (k->realize) { 1857 k->realize(proxy, errp); 1858 } 1859 } 1860 1861 static void virtio_pci_exit(PCIDevice *pci_dev) 1862 { 1863 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 1864 1865 msix_uninit_exclusive_bar(pci_dev); 1866 address_space_destroy(&proxy->modern_as); 1867 } 1868 1869 static void virtio_pci_reset(DeviceState *qdev) 1870 { 1871 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1872 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 1873 PCIDevice *dev = PCI_DEVICE(qdev); 1874 int i; 1875 1876 virtio_pci_stop_ioeventfd(proxy); 1877 virtio_bus_reset(bus); 1878 msix_unuse_all_vectors(&proxy->pci_dev); 1879 1880 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { 1881 proxy->vqs[i].enabled = 0; 1882 proxy->vqs[i].num = 0; 1883 proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0; 1884 proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0; 1885 proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0; 1886 } 1887 1888 if (pci_is_express(dev)) { 1889 pcie_cap_deverr_reset(dev); 1890 pcie_cap_lnkctl_reset(dev); 1891 1892 pci_set_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL, 0); 1893 } 1894 } 1895 1896 static Property virtio_pci_properties[] = { 1897 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags, 1898 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false), 1899 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy, 1900 ON_OFF_AUTO_AUTO), 1901 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false), 1902 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags, 1903 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true), 1904 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags, 1905 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false), 1906 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags, 1907 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false), 1908 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags, 1909 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false), 1910 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy, 1911 ignore_backend_features, false), 1912 DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags, 1913 VIRTIO_PCI_FLAG_ATS_BIT, false), 1914 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags, 1915 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true), 1916 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags, 1917 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true), 1918 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags, 1919 VIRTIO_PCI_FLAG_INIT_PM_BIT, true), 1920 DEFINE_PROP_END_OF_LIST(), 1921 }; 1922 1923 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp) 1924 { 1925 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev); 1926 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1927 PCIDevice *pci_dev = &proxy->pci_dev; 1928 1929 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) && 1930 virtio_pci_modern(proxy)) { 1931 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; 1932 } 1933 1934 vpciklass->parent_dc_realize(qdev, errp); 1935 } 1936 1937 static void virtio_pci_class_init(ObjectClass *klass, void *data) 1938 { 1939 DeviceClass *dc = DEVICE_CLASS(klass); 1940 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1941 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1942 1943 dc->props = virtio_pci_properties; 1944 k->realize = virtio_pci_realize; 1945 k->exit = virtio_pci_exit; 1946 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1947 k->revision = VIRTIO_PCI_ABI_VERSION; 1948 k->class_id = PCI_CLASS_OTHERS; 1949 vpciklass->parent_dc_realize = dc->realize; 1950 dc->realize = virtio_pci_dc_realize; 1951 dc->reset = virtio_pci_reset; 1952 } 1953 1954 static const TypeInfo virtio_pci_info = { 1955 .name = TYPE_VIRTIO_PCI, 1956 .parent = TYPE_PCI_DEVICE, 1957 .instance_size = sizeof(VirtIOPCIProxy), 1958 .class_init = virtio_pci_class_init, 1959 .class_size = sizeof(VirtioPCIClass), 1960 .abstract = true, 1961 }; 1962 1963 /* virtio-blk-pci */ 1964 1965 static Property virtio_blk_pci_properties[] = { 1966 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1967 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1968 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1969 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1970 DEFINE_PROP_END_OF_LIST(), 1971 }; 1972 1973 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1974 { 1975 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); 1976 DeviceState *vdev = DEVICE(&dev->vdev); 1977 1978 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1979 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1980 } 1981 1982 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) 1983 { 1984 DeviceClass *dc = DEVICE_CLASS(klass); 1985 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1986 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1987 1988 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1989 dc->props = virtio_blk_pci_properties; 1990 k->realize = virtio_blk_pci_realize; 1991 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1992 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; 1993 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1994 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1995 } 1996 1997 static void virtio_blk_pci_instance_init(Object *obj) 1998 { 1999 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); 2000 2001 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2002 TYPE_VIRTIO_BLK); 2003 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 2004 "bootindex", &error_abort); 2005 } 2006 2007 static const TypeInfo virtio_blk_pci_info = { 2008 .name = TYPE_VIRTIO_BLK_PCI, 2009 .parent = TYPE_VIRTIO_PCI, 2010 .instance_size = sizeof(VirtIOBlkPCI), 2011 .instance_init = virtio_blk_pci_instance_init, 2012 .class_init = virtio_blk_pci_class_init, 2013 }; 2014 2015 /* virtio-scsi-pci */ 2016 2017 static Property virtio_scsi_pci_properties[] = { 2018 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 2019 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 2020 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2021 DEV_NVECTORS_UNSPECIFIED), 2022 DEFINE_PROP_END_OF_LIST(), 2023 }; 2024 2025 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2026 { 2027 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); 2028 DeviceState *vdev = DEVICE(&dev->vdev); 2029 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 2030 DeviceState *proxy = DEVICE(vpci_dev); 2031 char *bus_name; 2032 2033 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 2034 vpci_dev->nvectors = vs->conf.num_queues + 3; 2035 } 2036 2037 /* 2038 * For command line compatibility, this sets the virtio-scsi-device bus 2039 * name as before. 2040 */ 2041 if (proxy->id) { 2042 bus_name = g_strdup_printf("%s.0", proxy->id); 2043 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 2044 g_free(bus_name); 2045 } 2046 2047 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2048 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2049 } 2050 2051 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) 2052 { 2053 DeviceClass *dc = DEVICE_CLASS(klass); 2054 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2055 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2056 2057 k->realize = virtio_scsi_pci_realize; 2058 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 2059 dc->props = virtio_scsi_pci_properties; 2060 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2061 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 2062 pcidev_k->revision = 0x00; 2063 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 2064 } 2065 2066 static void virtio_scsi_pci_instance_init(Object *obj) 2067 { 2068 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); 2069 2070 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2071 TYPE_VIRTIO_SCSI); 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 #ifdef CONFIG_LINUX 2139 /* vhost-user-scsi-pci */ 2140 static Property vhost_user_scsi_pci_properties[] = { 2141 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2142 DEV_NVECTORS_UNSPECIFIED), 2143 DEFINE_PROP_END_OF_LIST(), 2144 }; 2145 2146 static void vhost_user_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2147 { 2148 VHostUserSCSIPCI *dev = VHOST_USER_SCSI_PCI(vpci_dev); 2149 DeviceState *vdev = DEVICE(&dev->vdev); 2150 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 2151 2152 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 2153 vpci_dev->nvectors = vs->conf.num_queues + 3; 2154 } 2155 2156 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2157 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2158 } 2159 2160 static void vhost_user_scsi_pci_class_init(ObjectClass *klass, void *data) 2161 { 2162 DeviceClass *dc = DEVICE_CLASS(klass); 2163 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2164 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2165 k->realize = vhost_user_scsi_pci_realize; 2166 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 2167 dc->props = vhost_user_scsi_pci_properties; 2168 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2169 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 2170 pcidev_k->revision = 0x00; 2171 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 2172 } 2173 2174 static void vhost_user_scsi_pci_instance_init(Object *obj) 2175 { 2176 VHostUserSCSIPCI *dev = VHOST_USER_SCSI_PCI(obj); 2177 2178 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2179 TYPE_VHOST_USER_SCSI); 2180 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 2181 "bootindex", &error_abort); 2182 } 2183 2184 static const TypeInfo vhost_user_scsi_pci_info = { 2185 .name = TYPE_VHOST_USER_SCSI_PCI, 2186 .parent = TYPE_VIRTIO_PCI, 2187 .instance_size = sizeof(VHostUserSCSIPCI), 2188 .instance_init = vhost_user_scsi_pci_instance_init, 2189 .class_init = vhost_user_scsi_pci_class_init, 2190 }; 2191 #endif 2192 2193 /* vhost-vsock-pci */ 2194 2195 #ifdef CONFIG_VHOST_VSOCK 2196 static Property vhost_vsock_pci_properties[] = { 2197 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 2198 DEFINE_PROP_END_OF_LIST(), 2199 }; 2200 2201 static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2202 { 2203 VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev); 2204 DeviceState *vdev = DEVICE(&dev->vdev); 2205 2206 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2207 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2208 } 2209 2210 static void vhost_vsock_pci_class_init(ObjectClass *klass, void *data) 2211 { 2212 DeviceClass *dc = DEVICE_CLASS(klass); 2213 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2214 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2215 k->realize = vhost_vsock_pci_realize; 2216 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 2217 dc->props = vhost_vsock_pci_properties; 2218 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2219 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK; 2220 pcidev_k->revision = 0x00; 2221 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 2222 } 2223 2224 static void vhost_vsock_pci_instance_init(Object *obj) 2225 { 2226 VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj); 2227 2228 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2229 TYPE_VHOST_VSOCK); 2230 } 2231 2232 static const TypeInfo vhost_vsock_pci_info = { 2233 .name = TYPE_VHOST_VSOCK_PCI, 2234 .parent = TYPE_VIRTIO_PCI, 2235 .instance_size = sizeof(VHostVSockPCI), 2236 .instance_init = vhost_vsock_pci_instance_init, 2237 .class_init = vhost_vsock_pci_class_init, 2238 }; 2239 #endif 2240 2241 /* virtio-balloon-pci */ 2242 2243 static Property virtio_balloon_pci_properties[] = { 2244 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 2245 DEFINE_PROP_END_OF_LIST(), 2246 }; 2247 2248 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2249 { 2250 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); 2251 DeviceState *vdev = DEVICE(&dev->vdev); 2252 2253 if (vpci_dev->class_code != PCI_CLASS_OTHERS && 2254 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ 2255 vpci_dev->class_code = PCI_CLASS_OTHERS; 2256 } 2257 2258 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2259 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2260 } 2261 2262 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) 2263 { 2264 DeviceClass *dc = DEVICE_CLASS(klass); 2265 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2266 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2267 k->realize = virtio_balloon_pci_realize; 2268 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 2269 dc->props = virtio_balloon_pci_properties; 2270 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2271 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; 2272 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 2273 pcidev_k->class_id = PCI_CLASS_OTHERS; 2274 } 2275 2276 static void virtio_balloon_pci_instance_init(Object *obj) 2277 { 2278 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); 2279 2280 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2281 TYPE_VIRTIO_BALLOON); 2282 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev), 2283 "guest-stats", &error_abort); 2284 object_property_add_alias(obj, "guest-stats-polling-interval", 2285 OBJECT(&dev->vdev), 2286 "guest-stats-polling-interval", &error_abort); 2287 } 2288 2289 static const TypeInfo virtio_balloon_pci_info = { 2290 .name = TYPE_VIRTIO_BALLOON_PCI, 2291 .parent = TYPE_VIRTIO_PCI, 2292 .instance_size = sizeof(VirtIOBalloonPCI), 2293 .instance_init = virtio_balloon_pci_instance_init, 2294 .class_init = virtio_balloon_pci_class_init, 2295 }; 2296 2297 /* virtio-serial-pci */ 2298 2299 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2300 { 2301 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); 2302 DeviceState *vdev = DEVICE(&dev->vdev); 2303 DeviceState *proxy = DEVICE(vpci_dev); 2304 char *bus_name; 2305 2306 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && 2307 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ 2308 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ 2309 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; 2310 } 2311 2312 /* backwards-compatibility with machines that were created with 2313 DEV_NVECTORS_UNSPECIFIED */ 2314 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 2315 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; 2316 } 2317 2318 /* 2319 * For command line compatibility, this sets the virtio-serial-device bus 2320 * name as before. 2321 */ 2322 if (proxy->id) { 2323 bus_name = g_strdup_printf("%s.0", proxy->id); 2324 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 2325 g_free(bus_name); 2326 } 2327 2328 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2329 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2330 } 2331 2332 static Property virtio_serial_pci_properties[] = { 2333 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 2334 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 2335 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 2336 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 2337 DEFINE_PROP_END_OF_LIST(), 2338 }; 2339 2340 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) 2341 { 2342 DeviceClass *dc = DEVICE_CLASS(klass); 2343 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2344 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2345 k->realize = virtio_serial_pci_realize; 2346 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 2347 dc->props = virtio_serial_pci_properties; 2348 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2349 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; 2350 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 2351 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 2352 } 2353 2354 static void virtio_serial_pci_instance_init(Object *obj) 2355 { 2356 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); 2357 2358 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2359 TYPE_VIRTIO_SERIAL); 2360 } 2361 2362 static const TypeInfo virtio_serial_pci_info = { 2363 .name = TYPE_VIRTIO_SERIAL_PCI, 2364 .parent = TYPE_VIRTIO_PCI, 2365 .instance_size = sizeof(VirtIOSerialPCI), 2366 .instance_init = virtio_serial_pci_instance_init, 2367 .class_init = virtio_serial_pci_class_init, 2368 }; 2369 2370 /* virtio-net-pci */ 2371 2372 static Property virtio_net_properties[] = { 2373 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 2374 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 2375 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 2376 DEFINE_PROP_END_OF_LIST(), 2377 }; 2378 2379 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2380 { 2381 DeviceState *qdev = DEVICE(vpci_dev); 2382 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); 2383 DeviceState *vdev = DEVICE(&dev->vdev); 2384 2385 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 2386 object_get_typename(OBJECT(qdev))); 2387 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2388 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2389 } 2390 2391 static void virtio_net_pci_class_init(ObjectClass *klass, void *data) 2392 { 2393 DeviceClass *dc = DEVICE_CLASS(klass); 2394 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 2395 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 2396 2397 k->romfile = "efi-virtio.rom"; 2398 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2399 k->device_id = PCI_DEVICE_ID_VIRTIO_NET; 2400 k->revision = VIRTIO_PCI_ABI_VERSION; 2401 k->class_id = PCI_CLASS_NETWORK_ETHERNET; 2402 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 2403 dc->props = virtio_net_properties; 2404 vpciklass->realize = virtio_net_pci_realize; 2405 } 2406 2407 static void virtio_net_pci_instance_init(Object *obj) 2408 { 2409 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); 2410 2411 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2412 TYPE_VIRTIO_NET); 2413 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 2414 "bootindex", &error_abort); 2415 } 2416 2417 static const TypeInfo virtio_net_pci_info = { 2418 .name = TYPE_VIRTIO_NET_PCI, 2419 .parent = TYPE_VIRTIO_PCI, 2420 .instance_size = sizeof(VirtIONetPCI), 2421 .instance_init = virtio_net_pci_instance_init, 2422 .class_init = virtio_net_pci_class_init, 2423 }; 2424 2425 /* virtio-rng-pci */ 2426 2427 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2428 { 2429 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); 2430 DeviceState *vdev = DEVICE(&vrng->vdev); 2431 Error *err = NULL; 2432 2433 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2434 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 2435 if (err) { 2436 error_propagate(errp, err); 2437 return; 2438 } 2439 2440 object_property_set_link(OBJECT(vrng), 2441 OBJECT(vrng->vdev.conf.rng), "rng", 2442 NULL); 2443 } 2444 2445 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) 2446 { 2447 DeviceClass *dc = DEVICE_CLASS(klass); 2448 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2449 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2450 2451 k->realize = virtio_rng_pci_realize; 2452 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 2453 2454 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 2455 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; 2456 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 2457 pcidev_k->class_id = PCI_CLASS_OTHERS; 2458 } 2459 2460 static void virtio_rng_initfn(Object *obj) 2461 { 2462 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); 2463 2464 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2465 TYPE_VIRTIO_RNG); 2466 } 2467 2468 static const TypeInfo virtio_rng_pci_info = { 2469 .name = TYPE_VIRTIO_RNG_PCI, 2470 .parent = TYPE_VIRTIO_PCI, 2471 .instance_size = sizeof(VirtIORngPCI), 2472 .instance_init = virtio_rng_initfn, 2473 .class_init = virtio_rng_pci_class_init, 2474 }; 2475 2476 /* virtio-input-pci */ 2477 2478 static Property virtio_input_pci_properties[] = { 2479 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 2480 DEFINE_PROP_END_OF_LIST(), 2481 }; 2482 2483 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 2484 { 2485 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev); 2486 DeviceState *vdev = DEVICE(&vinput->vdev); 2487 2488 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 2489 virtio_pci_force_virtio_1(vpci_dev); 2490 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 2491 } 2492 2493 static void virtio_input_pci_class_init(ObjectClass *klass, void *data) 2494 { 2495 DeviceClass *dc = DEVICE_CLASS(klass); 2496 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 2497 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2498 2499 dc->props = virtio_input_pci_properties; 2500 k->realize = virtio_input_pci_realize; 2501 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 2502 2503 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER; 2504 } 2505 2506 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data) 2507 { 2508 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2509 2510 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD; 2511 } 2512 2513 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass, 2514 void *data) 2515 { 2516 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 2517 2518 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE; 2519 } 2520 2521 static void virtio_keyboard_initfn(Object *obj) 2522 { 2523 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 2524 2525 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2526 TYPE_VIRTIO_KEYBOARD); 2527 } 2528 2529 static void virtio_mouse_initfn(Object *obj) 2530 { 2531 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 2532 2533 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2534 TYPE_VIRTIO_MOUSE); 2535 } 2536 2537 static void virtio_tablet_initfn(Object *obj) 2538 { 2539 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 2540 2541 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2542 TYPE_VIRTIO_TABLET); 2543 } 2544 2545 static const TypeInfo virtio_input_pci_info = { 2546 .name = TYPE_VIRTIO_INPUT_PCI, 2547 .parent = TYPE_VIRTIO_PCI, 2548 .instance_size = sizeof(VirtIOInputPCI), 2549 .class_init = virtio_input_pci_class_init, 2550 .abstract = true, 2551 }; 2552 2553 static const TypeInfo virtio_input_hid_pci_info = { 2554 .name = TYPE_VIRTIO_INPUT_HID_PCI, 2555 .parent = TYPE_VIRTIO_INPUT_PCI, 2556 .instance_size = sizeof(VirtIOInputHIDPCI), 2557 .abstract = true, 2558 }; 2559 2560 static const TypeInfo virtio_keyboard_pci_info = { 2561 .name = TYPE_VIRTIO_KEYBOARD_PCI, 2562 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2563 .class_init = virtio_input_hid_kbd_pci_class_init, 2564 .instance_size = sizeof(VirtIOInputHIDPCI), 2565 .instance_init = virtio_keyboard_initfn, 2566 }; 2567 2568 static const TypeInfo virtio_mouse_pci_info = { 2569 .name = TYPE_VIRTIO_MOUSE_PCI, 2570 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2571 .class_init = virtio_input_hid_mouse_pci_class_init, 2572 .instance_size = sizeof(VirtIOInputHIDPCI), 2573 .instance_init = virtio_mouse_initfn, 2574 }; 2575 2576 static const TypeInfo virtio_tablet_pci_info = { 2577 .name = TYPE_VIRTIO_TABLET_PCI, 2578 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2579 .instance_size = sizeof(VirtIOInputHIDPCI), 2580 .instance_init = virtio_tablet_initfn, 2581 }; 2582 2583 #ifdef CONFIG_LINUX 2584 static void virtio_host_initfn(Object *obj) 2585 { 2586 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj); 2587 2588 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 2589 TYPE_VIRTIO_INPUT_HOST); 2590 } 2591 2592 static const TypeInfo virtio_host_pci_info = { 2593 .name = TYPE_VIRTIO_INPUT_HOST_PCI, 2594 .parent = TYPE_VIRTIO_INPUT_PCI, 2595 .instance_size = sizeof(VirtIOInputHostPCI), 2596 .instance_init = virtio_host_initfn, 2597 }; 2598 #endif 2599 2600 /* virtio-pci-bus */ 2601 2602 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 2603 VirtIOPCIProxy *dev) 2604 { 2605 DeviceState *qdev = DEVICE(dev); 2606 char virtio_bus_name[] = "virtio-bus"; 2607 2608 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 2609 virtio_bus_name); 2610 } 2611 2612 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 2613 { 2614 BusClass *bus_class = BUS_CLASS(klass); 2615 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 2616 bus_class->max_dev = 1; 2617 k->notify = virtio_pci_notify; 2618 k->save_config = virtio_pci_save_config; 2619 k->load_config = virtio_pci_load_config; 2620 k->save_queue = virtio_pci_save_queue; 2621 k->load_queue = virtio_pci_load_queue; 2622 k->save_extra_state = virtio_pci_save_extra_state; 2623 k->load_extra_state = virtio_pci_load_extra_state; 2624 k->has_extra_state = virtio_pci_has_extra_state; 2625 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 2626 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 2627 k->vmstate_change = virtio_pci_vmstate_change; 2628 k->pre_plugged = virtio_pci_pre_plugged; 2629 k->device_plugged = virtio_pci_device_plugged; 2630 k->device_unplugged = virtio_pci_device_unplugged; 2631 k->query_nvectors = virtio_pci_query_nvectors; 2632 k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled; 2633 k->ioeventfd_assign = virtio_pci_ioeventfd_assign; 2634 k->get_dma_as = virtio_pci_get_dma_as; 2635 } 2636 2637 static const TypeInfo virtio_pci_bus_info = { 2638 .name = TYPE_VIRTIO_PCI_BUS, 2639 .parent = TYPE_VIRTIO_BUS, 2640 .instance_size = sizeof(VirtioPCIBusState), 2641 .class_init = virtio_pci_bus_class_init, 2642 }; 2643 2644 static void virtio_pci_register_types(void) 2645 { 2646 type_register_static(&virtio_rng_pci_info); 2647 type_register_static(&virtio_input_pci_info); 2648 type_register_static(&virtio_input_hid_pci_info); 2649 type_register_static(&virtio_keyboard_pci_info); 2650 type_register_static(&virtio_mouse_pci_info); 2651 type_register_static(&virtio_tablet_pci_info); 2652 #ifdef CONFIG_LINUX 2653 type_register_static(&virtio_host_pci_info); 2654 #endif 2655 type_register_static(&virtio_pci_bus_info); 2656 type_register_static(&virtio_pci_info); 2657 #ifdef CONFIG_VIRTFS 2658 type_register_static(&virtio_9p_pci_info); 2659 #endif 2660 type_register_static(&virtio_blk_pci_info); 2661 type_register_static(&virtio_scsi_pci_info); 2662 type_register_static(&virtio_balloon_pci_info); 2663 type_register_static(&virtio_serial_pci_info); 2664 type_register_static(&virtio_net_pci_info); 2665 #ifdef CONFIG_VHOST_SCSI 2666 type_register_static(&vhost_scsi_pci_info); 2667 #endif 2668 #ifdef CONFIG_LINUX 2669 type_register_static(&vhost_user_scsi_pci_info); 2670 #endif 2671 #ifdef CONFIG_VHOST_VSOCK 2672 type_register_static(&vhost_vsock_pci_info); 2673 #endif 2674 } 2675 2676 type_init(virtio_pci_register_types) 2677