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 "exec/memop.h" 21 #include "standard-headers/linux/virtio_pci.h" 22 #include "hw/virtio/virtio.h" 23 #include "migration/qemu-file-types.h" 24 #include "hw/pci/pci.h" 25 #include "hw/pci/pci_bus.h" 26 #include "hw/qdev-properties.h" 27 #include "qapi/error.h" 28 #include "qemu/error-report.h" 29 #include "qemu/module.h" 30 #include "hw/pci/msi.h" 31 #include "hw/pci/msix.h" 32 #include "hw/loader.h" 33 #include "sysemu/kvm.h" 34 #include "virtio-pci.h" 35 #include "qemu/range.h" 36 #include "hw/virtio/virtio-bus.h" 37 #include "qapi/visitor.h" 38 39 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev)) 40 41 #undef VIRTIO_PCI_CONFIG 42 43 /* The remaining space is defined by each driver as the per-driver 44 * configuration space */ 45 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev)) 46 47 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 48 VirtIOPCIProxy *dev); 49 static void virtio_pci_reset(DeviceState *qdev); 50 51 /* virtio device */ 52 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ 53 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) 54 { 55 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 56 } 57 58 /* DeviceState to VirtIOPCIProxy. Note: used on datapath, 59 * be careful and test performance if you change this. 60 */ 61 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) 62 { 63 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 64 } 65 66 static void virtio_pci_notify(DeviceState *d, uint16_t vector) 67 { 68 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); 69 70 if (msix_enabled(&proxy->pci_dev)) 71 msix_notify(&proxy->pci_dev, vector); 72 else { 73 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 74 pci_set_irq(&proxy->pci_dev, atomic_read(&vdev->isr) & 1); 75 } 76 } 77 78 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) 79 { 80 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 81 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 82 83 pci_device_save(&proxy->pci_dev, f); 84 msix_save(&proxy->pci_dev, f); 85 if (msix_present(&proxy->pci_dev)) 86 qemu_put_be16(f, vdev->config_vector); 87 } 88 89 static const VMStateDescription vmstate_virtio_pci_modern_queue_state = { 90 .name = "virtio_pci/modern_queue_state", 91 .version_id = 1, 92 .minimum_version_id = 1, 93 .fields = (VMStateField[]) { 94 VMSTATE_UINT16(num, VirtIOPCIQueue), 95 VMSTATE_UNUSED(1), /* enabled was stored as be16 */ 96 VMSTATE_BOOL(enabled, VirtIOPCIQueue), 97 VMSTATE_UINT32_ARRAY(desc, VirtIOPCIQueue, 2), 98 VMSTATE_UINT32_ARRAY(avail, VirtIOPCIQueue, 2), 99 VMSTATE_UINT32_ARRAY(used, VirtIOPCIQueue, 2), 100 VMSTATE_END_OF_LIST() 101 } 102 }; 103 104 static bool virtio_pci_modern_state_needed(void *opaque) 105 { 106 VirtIOPCIProxy *proxy = opaque; 107 108 return virtio_pci_modern(proxy); 109 } 110 111 static const VMStateDescription vmstate_virtio_pci_modern_state_sub = { 112 .name = "virtio_pci/modern_state", 113 .version_id = 1, 114 .minimum_version_id = 1, 115 .needed = &virtio_pci_modern_state_needed, 116 .fields = (VMStateField[]) { 117 VMSTATE_UINT32(dfselect, VirtIOPCIProxy), 118 VMSTATE_UINT32(gfselect, VirtIOPCIProxy), 119 VMSTATE_UINT32_ARRAY(guest_features, VirtIOPCIProxy, 2), 120 VMSTATE_STRUCT_ARRAY(vqs, VirtIOPCIProxy, VIRTIO_QUEUE_MAX, 0, 121 vmstate_virtio_pci_modern_queue_state, 122 VirtIOPCIQueue), 123 VMSTATE_END_OF_LIST() 124 } 125 }; 126 127 static const VMStateDescription vmstate_virtio_pci = { 128 .name = "virtio_pci", 129 .version_id = 1, 130 .minimum_version_id = 1, 131 .minimum_version_id_old = 1, 132 .fields = (VMStateField[]) { 133 VMSTATE_END_OF_LIST() 134 }, 135 .subsections = (const VMStateDescription*[]) { 136 &vmstate_virtio_pci_modern_state_sub, 137 NULL 138 } 139 }; 140 141 static bool virtio_pci_has_extra_state(DeviceState *d) 142 { 143 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 144 145 return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA; 146 } 147 148 static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f) 149 { 150 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 151 152 vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL); 153 } 154 155 static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f) 156 { 157 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 158 159 return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1); 160 } 161 162 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) 163 { 164 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 165 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 166 167 if (msix_present(&proxy->pci_dev)) 168 qemu_put_be16(f, virtio_queue_vector(vdev, n)); 169 } 170 171 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) 172 { 173 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 174 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 175 176 int ret; 177 ret = pci_device_load(&proxy->pci_dev, f); 178 if (ret) { 179 return ret; 180 } 181 msix_unuse_all_vectors(&proxy->pci_dev); 182 msix_load(&proxy->pci_dev, f); 183 if (msix_present(&proxy->pci_dev)) { 184 qemu_get_be16s(f, &vdev->config_vector); 185 } else { 186 vdev->config_vector = VIRTIO_NO_VECTOR; 187 } 188 if (vdev->config_vector != VIRTIO_NO_VECTOR) { 189 return msix_vector_use(&proxy->pci_dev, vdev->config_vector); 190 } 191 return 0; 192 } 193 194 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) 195 { 196 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 197 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 198 199 uint16_t vector; 200 if (msix_present(&proxy->pci_dev)) { 201 qemu_get_be16s(f, &vector); 202 } else { 203 vector = VIRTIO_NO_VECTOR; 204 } 205 virtio_queue_set_vector(vdev, n, vector); 206 if (vector != VIRTIO_NO_VECTOR) { 207 return msix_vector_use(&proxy->pci_dev, vector); 208 } 209 210 return 0; 211 } 212 213 static bool virtio_pci_ioeventfd_enabled(DeviceState *d) 214 { 215 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 216 217 return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0; 218 } 219 220 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000 221 222 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy) 223 { 224 return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ? 225 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4; 226 } 227 228 static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier, 229 int n, bool assign) 230 { 231 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 232 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 233 VirtQueue *vq = virtio_get_queue(vdev, n); 234 bool legacy = virtio_pci_legacy(proxy); 235 bool modern = virtio_pci_modern(proxy); 236 bool fast_mmio = kvm_ioeventfd_any_length_enabled(); 237 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; 238 MemoryRegion *modern_mr = &proxy->notify.mr; 239 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr; 240 MemoryRegion *legacy_mr = &proxy->bar; 241 hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) * 242 virtio_get_queue_index(vq); 243 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY; 244 245 if (assign) { 246 if (modern) { 247 if (fast_mmio) { 248 memory_region_add_eventfd(modern_mr, modern_addr, 0, 249 false, n, notifier); 250 } else { 251 memory_region_add_eventfd(modern_mr, modern_addr, 2, 252 false, n, notifier); 253 } 254 if (modern_pio) { 255 memory_region_add_eventfd(modern_notify_mr, 0, 2, 256 true, n, notifier); 257 } 258 } 259 if (legacy) { 260 memory_region_add_eventfd(legacy_mr, legacy_addr, 2, 261 true, n, notifier); 262 } 263 } else { 264 if (modern) { 265 if (fast_mmio) { 266 memory_region_del_eventfd(modern_mr, modern_addr, 0, 267 false, n, notifier); 268 } else { 269 memory_region_del_eventfd(modern_mr, modern_addr, 2, 270 false, n, notifier); 271 } 272 if (modern_pio) { 273 memory_region_del_eventfd(modern_notify_mr, 0, 2, 274 true, n, notifier); 275 } 276 } 277 if (legacy) { 278 memory_region_del_eventfd(legacy_mr, legacy_addr, 2, 279 true, n, notifier); 280 } 281 } 282 return 0; 283 } 284 285 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) 286 { 287 virtio_bus_start_ioeventfd(&proxy->bus); 288 } 289 290 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) 291 { 292 virtio_bus_stop_ioeventfd(&proxy->bus); 293 } 294 295 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) 296 { 297 VirtIOPCIProxy *proxy = opaque; 298 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 299 hwaddr pa; 300 301 switch (addr) { 302 case VIRTIO_PCI_GUEST_FEATURES: 303 /* Guest does not negotiate properly? We have to assume nothing. */ 304 if (val & (1 << VIRTIO_F_BAD_FEATURE)) { 305 val = virtio_bus_get_vdev_bad_features(&proxy->bus); 306 } 307 virtio_set_features(vdev, val); 308 break; 309 case VIRTIO_PCI_QUEUE_PFN: 310 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; 311 if (pa == 0) { 312 virtio_pci_reset(DEVICE(proxy)); 313 } 314 else 315 virtio_queue_set_addr(vdev, vdev->queue_sel, pa); 316 break; 317 case VIRTIO_PCI_QUEUE_SEL: 318 if (val < VIRTIO_QUEUE_MAX) 319 vdev->queue_sel = val; 320 break; 321 case VIRTIO_PCI_QUEUE_NOTIFY: 322 if (val < VIRTIO_QUEUE_MAX) { 323 virtio_queue_notify(vdev, val); 324 } 325 break; 326 case VIRTIO_PCI_STATUS: 327 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 328 virtio_pci_stop_ioeventfd(proxy); 329 } 330 331 virtio_set_status(vdev, val & 0xFF); 332 333 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 334 virtio_pci_start_ioeventfd(proxy); 335 } 336 337 if (vdev->status == 0) { 338 virtio_pci_reset(DEVICE(proxy)); 339 } 340 341 /* Linux before 2.6.34 drives the device without enabling 342 the PCI device bus master bit. Enable it automatically 343 for the guest. This is a PCI spec violation but so is 344 initiating DMA with bus master bit clear. */ 345 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) { 346 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, 347 proxy->pci_dev.config[PCI_COMMAND] | 348 PCI_COMMAND_MASTER, 1); 349 } 350 break; 351 case VIRTIO_MSI_CONFIG_VECTOR: 352 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 353 /* Make it possible for guest to discover an error took place. */ 354 if (msix_vector_use(&proxy->pci_dev, val) < 0) 355 val = VIRTIO_NO_VECTOR; 356 vdev->config_vector = val; 357 break; 358 case VIRTIO_MSI_QUEUE_VECTOR: 359 msix_vector_unuse(&proxy->pci_dev, 360 virtio_queue_vector(vdev, vdev->queue_sel)); 361 /* Make it possible for guest to discover an error took place. */ 362 if (msix_vector_use(&proxy->pci_dev, val) < 0) 363 val = VIRTIO_NO_VECTOR; 364 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 365 break; 366 default: 367 error_report("%s: unexpected address 0x%x value 0x%x", 368 __func__, addr, val); 369 break; 370 } 371 } 372 373 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) 374 { 375 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 376 uint32_t ret = 0xFFFFFFFF; 377 378 switch (addr) { 379 case VIRTIO_PCI_HOST_FEATURES: 380 ret = vdev->host_features; 381 break; 382 case VIRTIO_PCI_GUEST_FEATURES: 383 ret = vdev->guest_features; 384 break; 385 case VIRTIO_PCI_QUEUE_PFN: 386 ret = virtio_queue_get_addr(vdev, vdev->queue_sel) 387 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; 388 break; 389 case VIRTIO_PCI_QUEUE_NUM: 390 ret = virtio_queue_get_num(vdev, vdev->queue_sel); 391 break; 392 case VIRTIO_PCI_QUEUE_SEL: 393 ret = vdev->queue_sel; 394 break; 395 case VIRTIO_PCI_STATUS: 396 ret = vdev->status; 397 break; 398 case VIRTIO_PCI_ISR: 399 /* reading from the ISR also clears it. */ 400 ret = atomic_xchg(&vdev->isr, 0); 401 pci_irq_deassert(&proxy->pci_dev); 402 break; 403 case VIRTIO_MSI_CONFIG_VECTOR: 404 ret = vdev->config_vector; 405 break; 406 case VIRTIO_MSI_QUEUE_VECTOR: 407 ret = virtio_queue_vector(vdev, vdev->queue_sel); 408 break; 409 default: 410 break; 411 } 412 413 return ret; 414 } 415 416 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, 417 unsigned size) 418 { 419 VirtIOPCIProxy *proxy = opaque; 420 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 421 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); 422 uint64_t val = 0; 423 if (addr < config) { 424 return virtio_ioport_read(proxy, addr); 425 } 426 addr -= config; 427 428 switch (size) { 429 case 1: 430 val = virtio_config_readb(vdev, addr); 431 break; 432 case 2: 433 val = virtio_config_readw(vdev, addr); 434 if (virtio_is_big_endian(vdev)) { 435 val = bswap16(val); 436 } 437 break; 438 case 4: 439 val = virtio_config_readl(vdev, addr); 440 if (virtio_is_big_endian(vdev)) { 441 val = bswap32(val); 442 } 443 break; 444 } 445 return val; 446 } 447 448 static void virtio_pci_config_write(void *opaque, hwaddr addr, 449 uint64_t val, unsigned size) 450 { 451 VirtIOPCIProxy *proxy = opaque; 452 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); 453 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 454 if (addr < config) { 455 virtio_ioport_write(proxy, addr, val); 456 return; 457 } 458 addr -= config; 459 /* 460 * Virtio-PCI is odd. Ioports are LE but config space is target native 461 * endian. 462 */ 463 switch (size) { 464 case 1: 465 virtio_config_writeb(vdev, addr, val); 466 break; 467 case 2: 468 if (virtio_is_big_endian(vdev)) { 469 val = bswap16(val); 470 } 471 virtio_config_writew(vdev, addr, val); 472 break; 473 case 4: 474 if (virtio_is_big_endian(vdev)) { 475 val = bswap32(val); 476 } 477 virtio_config_writel(vdev, addr, val); 478 break; 479 } 480 } 481 482 static const MemoryRegionOps virtio_pci_config_ops = { 483 .read = virtio_pci_config_read, 484 .write = virtio_pci_config_write, 485 .impl = { 486 .min_access_size = 1, 487 .max_access_size = 4, 488 }, 489 .endianness = DEVICE_LITTLE_ENDIAN, 490 }; 491 492 static MemoryRegion *virtio_address_space_lookup(VirtIOPCIProxy *proxy, 493 hwaddr *off, int len) 494 { 495 int i; 496 VirtIOPCIRegion *reg; 497 498 for (i = 0; i < ARRAY_SIZE(proxy->regs); ++i) { 499 reg = &proxy->regs[i]; 500 if (*off >= reg->offset && 501 *off + len <= reg->offset + reg->size) { 502 *off -= reg->offset; 503 return ®->mr; 504 } 505 } 506 507 return NULL; 508 } 509 510 /* Below are generic functions to do memcpy from/to an address space, 511 * without byteswaps, with input validation. 512 * 513 * As regular address_space_* APIs all do some kind of byteswap at least for 514 * some host/target combinations, we are forced to explicitly convert to a 515 * known-endianness integer value. 516 * It doesn't really matter which endian format to go through, so the code 517 * below selects the endian that causes the least amount of work on the given 518 * host. 519 * 520 * Note: host pointer must be aligned. 521 */ 522 static 523 void virtio_address_space_write(VirtIOPCIProxy *proxy, hwaddr addr, 524 const uint8_t *buf, int len) 525 { 526 uint64_t val; 527 MemoryRegion *mr; 528 529 /* address_space_* APIs assume an aligned address. 530 * As address is under guest control, handle illegal values. 531 */ 532 addr &= ~(len - 1); 533 534 mr = virtio_address_space_lookup(proxy, &addr, len); 535 if (!mr) { 536 return; 537 } 538 539 /* Make sure caller aligned buf properly */ 540 assert(!(((uintptr_t)buf) & (len - 1))); 541 542 switch (len) { 543 case 1: 544 val = pci_get_byte(buf); 545 break; 546 case 2: 547 val = pci_get_word(buf); 548 break; 549 case 4: 550 val = pci_get_long(buf); 551 break; 552 default: 553 /* As length is under guest control, handle illegal values. */ 554 return; 555 } 556 memory_region_dispatch_write(mr, addr, val, size_memop(len) | MO_LE, 557 MEMTXATTRS_UNSPECIFIED); 558 } 559 560 static void 561 virtio_address_space_read(VirtIOPCIProxy *proxy, hwaddr addr, 562 uint8_t *buf, int len) 563 { 564 uint64_t val; 565 MemoryRegion *mr; 566 567 /* address_space_* APIs assume an aligned address. 568 * As address is under guest control, handle illegal values. 569 */ 570 addr &= ~(len - 1); 571 572 mr = virtio_address_space_lookup(proxy, &addr, len); 573 if (!mr) { 574 return; 575 } 576 577 /* Make sure caller aligned buf properly */ 578 assert(!(((uintptr_t)buf) & (len - 1))); 579 580 memory_region_dispatch_read(mr, addr, &val, size_memop(len) | MO_LE, 581 MEMTXATTRS_UNSPECIFIED); 582 switch (len) { 583 case 1: 584 pci_set_byte(buf, val); 585 break; 586 case 2: 587 pci_set_word(buf, val); 588 break; 589 case 4: 590 pci_set_long(buf, val); 591 break; 592 default: 593 /* As length is under guest control, handle illegal values. */ 594 break; 595 } 596 } 597 598 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, 599 uint32_t val, int len) 600 { 601 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 602 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 603 struct virtio_pci_cfg_cap *cfg; 604 605 pci_default_write_config(pci_dev, address, val, len); 606 607 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) { 608 pcie_cap_flr_write_config(pci_dev, address, val, len); 609 } 610 611 if (range_covers_byte(address, len, PCI_COMMAND)) { 612 if (!(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 613 virtio_set_disabled(vdev, true); 614 virtio_pci_stop_ioeventfd(proxy); 615 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); 616 } else { 617 virtio_set_disabled(vdev, false); 618 } 619 } 620 621 if (proxy->config_cap && 622 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap, 623 pci_cfg_data), 624 sizeof cfg->pci_cfg_data)) { 625 uint32_t off; 626 uint32_t len; 627 628 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap); 629 off = le32_to_cpu(cfg->cap.offset); 630 len = le32_to_cpu(cfg->cap.length); 631 632 if (len == 1 || len == 2 || len == 4) { 633 assert(len <= sizeof cfg->pci_cfg_data); 634 virtio_address_space_write(proxy, off, cfg->pci_cfg_data, len); 635 } 636 } 637 } 638 639 static uint32_t virtio_read_config(PCIDevice *pci_dev, 640 uint32_t address, int len) 641 { 642 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 643 struct virtio_pci_cfg_cap *cfg; 644 645 if (proxy->config_cap && 646 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap, 647 pci_cfg_data), 648 sizeof cfg->pci_cfg_data)) { 649 uint32_t off; 650 uint32_t len; 651 652 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap); 653 off = le32_to_cpu(cfg->cap.offset); 654 len = le32_to_cpu(cfg->cap.length); 655 656 if (len == 1 || len == 2 || len == 4) { 657 assert(len <= sizeof cfg->pci_cfg_data); 658 virtio_address_space_read(proxy, off, cfg->pci_cfg_data, len); 659 } 660 } 661 662 return pci_default_read_config(pci_dev, address, len); 663 } 664 665 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, 666 unsigned int queue_no, 667 unsigned int vector) 668 { 669 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 670 int ret; 671 672 if (irqfd->users == 0) { 673 ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev); 674 if (ret < 0) { 675 return ret; 676 } 677 irqfd->virq = ret; 678 } 679 irqfd->users++; 680 return 0; 681 } 682 683 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, 684 unsigned int vector) 685 { 686 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 687 if (--irqfd->users == 0) { 688 kvm_irqchip_release_virq(kvm_state, irqfd->virq); 689 } 690 } 691 692 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, 693 unsigned int queue_no, 694 unsigned int vector) 695 { 696 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 697 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 698 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 699 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 700 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq); 701 } 702 703 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, 704 unsigned int queue_no, 705 unsigned int vector) 706 { 707 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 708 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 709 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 710 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 711 int ret; 712 713 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq); 714 assert(ret == 0); 715 } 716 717 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) 718 { 719 PCIDevice *dev = &proxy->pci_dev; 720 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 721 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 722 unsigned int vector; 723 int ret, queue_no; 724 725 for (queue_no = 0; queue_no < nvqs; queue_no++) { 726 if (!virtio_queue_get_num(vdev, queue_no)) { 727 break; 728 } 729 vector = virtio_queue_vector(vdev, queue_no); 730 if (vector >= msix_nr_vectors_allocated(dev)) { 731 continue; 732 } 733 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector); 734 if (ret < 0) { 735 goto undo; 736 } 737 /* If guest supports masking, set up irqfd now. 738 * Otherwise, delay until unmasked in the frontend. 739 */ 740 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 741 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 742 if (ret < 0) { 743 kvm_virtio_pci_vq_vector_release(proxy, vector); 744 goto undo; 745 } 746 } 747 } 748 return 0; 749 750 undo: 751 while (--queue_no >= 0) { 752 vector = virtio_queue_vector(vdev, queue_no); 753 if (vector >= msix_nr_vectors_allocated(dev)) { 754 continue; 755 } 756 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 757 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 758 } 759 kvm_virtio_pci_vq_vector_release(proxy, vector); 760 } 761 return ret; 762 } 763 764 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) 765 { 766 PCIDevice *dev = &proxy->pci_dev; 767 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 768 unsigned int vector; 769 int queue_no; 770 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 771 772 for (queue_no = 0; queue_no < nvqs; queue_no++) { 773 if (!virtio_queue_get_num(vdev, queue_no)) { 774 break; 775 } 776 vector = virtio_queue_vector(vdev, queue_no); 777 if (vector >= msix_nr_vectors_allocated(dev)) { 778 continue; 779 } 780 /* If guest supports masking, clean up irqfd now. 781 * Otherwise, it was cleaned when masked in the frontend. 782 */ 783 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 784 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 785 } 786 kvm_virtio_pci_vq_vector_release(proxy, vector); 787 } 788 } 789 790 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, 791 unsigned int queue_no, 792 unsigned int vector, 793 MSIMessage msg) 794 { 795 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 796 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 797 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 798 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 799 VirtIOIRQFD *irqfd; 800 int ret = 0; 801 802 if (proxy->vector_irqfd) { 803 irqfd = &proxy->vector_irqfd[vector]; 804 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { 805 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg, 806 &proxy->pci_dev); 807 if (ret < 0) { 808 return ret; 809 } 810 kvm_irqchip_commit_routes(kvm_state); 811 } 812 } 813 814 /* If guest supports masking, irqfd is already setup, unmask it. 815 * Otherwise, set it up now. 816 */ 817 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 818 k->guest_notifier_mask(vdev, queue_no, false); 819 /* Test after unmasking to avoid losing events. */ 820 if (k->guest_notifier_pending && 821 k->guest_notifier_pending(vdev, queue_no)) { 822 event_notifier_set(n); 823 } 824 } else { 825 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 826 } 827 return ret; 828 } 829 830 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, 831 unsigned int queue_no, 832 unsigned int vector) 833 { 834 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 835 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 836 837 /* If guest supports masking, keep irqfd but mask it. 838 * Otherwise, clean it up now. 839 */ 840 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { 841 k->guest_notifier_mask(vdev, queue_no, true); 842 } else { 843 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 844 } 845 } 846 847 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, 848 MSIMessage msg) 849 { 850 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 851 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 852 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 853 int ret, index, unmasked = 0; 854 855 while (vq) { 856 index = virtio_get_queue_index(vq); 857 if (!virtio_queue_get_num(vdev, index)) { 858 break; 859 } 860 if (index < proxy->nvqs_with_notifiers) { 861 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg); 862 if (ret < 0) { 863 goto undo; 864 } 865 ++unmasked; 866 } 867 vq = virtio_vector_next_queue(vq); 868 } 869 870 return 0; 871 872 undo: 873 vq = virtio_vector_first_queue(vdev, vector); 874 while (vq && unmasked >= 0) { 875 index = virtio_get_queue_index(vq); 876 if (index < proxy->nvqs_with_notifiers) { 877 virtio_pci_vq_vector_mask(proxy, index, vector); 878 --unmasked; 879 } 880 vq = virtio_vector_next_queue(vq); 881 } 882 return ret; 883 } 884 885 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) 886 { 887 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 888 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 889 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 890 int index; 891 892 while (vq) { 893 index = virtio_get_queue_index(vq); 894 if (!virtio_queue_get_num(vdev, index)) { 895 break; 896 } 897 if (index < proxy->nvqs_with_notifiers) { 898 virtio_pci_vq_vector_mask(proxy, index, vector); 899 } 900 vq = virtio_vector_next_queue(vq); 901 } 902 } 903 904 static void virtio_pci_vector_poll(PCIDevice *dev, 905 unsigned int vector_start, 906 unsigned int vector_end) 907 { 908 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 909 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 910 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 911 int queue_no; 912 unsigned int vector; 913 EventNotifier *notifier; 914 VirtQueue *vq; 915 916 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 917 if (!virtio_queue_get_num(vdev, queue_no)) { 918 break; 919 } 920 vector = virtio_queue_vector(vdev, queue_no); 921 if (vector < vector_start || vector >= vector_end || 922 !msix_is_masked(dev, vector)) { 923 continue; 924 } 925 vq = virtio_get_queue(vdev, queue_no); 926 notifier = virtio_queue_get_guest_notifier(vq); 927 if (k->guest_notifier_pending) { 928 if (k->guest_notifier_pending(vdev, queue_no)) { 929 msix_set_pending(dev, vector); 930 } 931 } else if (event_notifier_test_and_clear(notifier)) { 932 msix_set_pending(dev, vector); 933 } 934 } 935 } 936 937 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, 938 bool with_irqfd) 939 { 940 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 941 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 942 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 943 VirtQueue *vq = virtio_get_queue(vdev, n); 944 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 945 946 if (assign) { 947 int r = event_notifier_init(notifier, 0); 948 if (r < 0) { 949 return r; 950 } 951 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 952 } else { 953 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 954 event_notifier_cleanup(notifier); 955 } 956 957 if (!msix_enabled(&proxy->pci_dev) && 958 vdev->use_guest_notifier_mask && 959 vdc->guest_notifier_mask) { 960 vdc->guest_notifier_mask(vdev, n, !assign); 961 } 962 963 return 0; 964 } 965 966 static bool virtio_pci_query_guest_notifiers(DeviceState *d) 967 { 968 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 969 return msix_enabled(&proxy->pci_dev); 970 } 971 972 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) 973 { 974 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 975 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 976 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 977 int r, n; 978 bool with_irqfd = msix_enabled(&proxy->pci_dev) && 979 kvm_msi_via_irqfd_enabled(); 980 981 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX); 982 983 /* When deassigning, pass a consistent nvqs value 984 * to avoid leaking notifiers. 985 */ 986 assert(assign || nvqs == proxy->nvqs_with_notifiers); 987 988 proxy->nvqs_with_notifiers = nvqs; 989 990 /* Must unset vector notifier while guest notifier is still assigned */ 991 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) { 992 msix_unset_vector_notifiers(&proxy->pci_dev); 993 if (proxy->vector_irqfd) { 994 kvm_virtio_pci_vector_release(proxy, nvqs); 995 g_free(proxy->vector_irqfd); 996 proxy->vector_irqfd = NULL; 997 } 998 } 999 1000 for (n = 0; n < nvqs; n++) { 1001 if (!virtio_queue_get_num(vdev, n)) { 1002 break; 1003 } 1004 1005 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd); 1006 if (r < 0) { 1007 goto assign_error; 1008 } 1009 } 1010 1011 /* Must set vector notifier after guest notifier has been assigned */ 1012 if ((with_irqfd || k->guest_notifier_mask) && assign) { 1013 if (with_irqfd) { 1014 proxy->vector_irqfd = 1015 g_malloc0(sizeof(*proxy->vector_irqfd) * 1016 msix_nr_vectors_allocated(&proxy->pci_dev)); 1017 r = kvm_virtio_pci_vector_use(proxy, nvqs); 1018 if (r < 0) { 1019 goto assign_error; 1020 } 1021 } 1022 r = msix_set_vector_notifiers(&proxy->pci_dev, 1023 virtio_pci_vector_unmask, 1024 virtio_pci_vector_mask, 1025 virtio_pci_vector_poll); 1026 if (r < 0) { 1027 goto notifiers_error; 1028 } 1029 } 1030 1031 return 0; 1032 1033 notifiers_error: 1034 if (with_irqfd) { 1035 assert(assign); 1036 kvm_virtio_pci_vector_release(proxy, nvqs); 1037 } 1038 1039 assign_error: 1040 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ 1041 assert(assign); 1042 while (--n >= 0) { 1043 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); 1044 } 1045 return r; 1046 } 1047 1048 static int virtio_pci_set_host_notifier_mr(DeviceState *d, int n, 1049 MemoryRegion *mr, bool assign) 1050 { 1051 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 1052 int offset; 1053 1054 if (n >= VIRTIO_QUEUE_MAX || !virtio_pci_modern(proxy) || 1055 virtio_pci_queue_mem_mult(proxy) != memory_region_size(mr)) { 1056 return -1; 1057 } 1058 1059 if (assign) { 1060 offset = virtio_pci_queue_mem_mult(proxy) * n; 1061 memory_region_add_subregion_overlap(&proxy->notify.mr, offset, mr, 1); 1062 } else { 1063 memory_region_del_subregion(&proxy->notify.mr, mr); 1064 } 1065 1066 return 0; 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 /* 1092 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 1093 */ 1094 1095 static int virtio_pci_query_nvectors(DeviceState *d) 1096 { 1097 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1098 1099 return proxy->nvectors; 1100 } 1101 1102 static AddressSpace *virtio_pci_get_dma_as(DeviceState *d) 1103 { 1104 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1105 PCIDevice *dev = &proxy->pci_dev; 1106 1107 return pci_get_address_space(dev); 1108 } 1109 1110 static bool virtio_pci_queue_enabled(DeviceState *d, int n) 1111 { 1112 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1113 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1114 1115 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1116 return proxy->vqs[n].enabled; 1117 } 1118 1119 return virtio_queue_enabled_legacy(vdev, n); 1120 } 1121 1122 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy, 1123 struct virtio_pci_cap *cap) 1124 { 1125 PCIDevice *dev = &proxy->pci_dev; 1126 int offset; 1127 1128 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, 1129 cap->cap_len, &error_abort); 1130 1131 assert(cap->cap_len >= sizeof *cap); 1132 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len, 1133 cap->cap_len - PCI_CAP_FLAGS); 1134 1135 return offset; 1136 } 1137 1138 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr, 1139 unsigned size) 1140 { 1141 VirtIOPCIProxy *proxy = opaque; 1142 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1143 uint32_t val = 0; 1144 int i; 1145 1146 switch (addr) { 1147 case VIRTIO_PCI_COMMON_DFSELECT: 1148 val = proxy->dfselect; 1149 break; 1150 case VIRTIO_PCI_COMMON_DF: 1151 if (proxy->dfselect <= 1) { 1152 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 1153 1154 val = (vdev->host_features & ~vdc->legacy_features) >> 1155 (32 * proxy->dfselect); 1156 } 1157 break; 1158 case VIRTIO_PCI_COMMON_GFSELECT: 1159 val = proxy->gfselect; 1160 break; 1161 case VIRTIO_PCI_COMMON_GF: 1162 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) { 1163 val = proxy->guest_features[proxy->gfselect]; 1164 } 1165 break; 1166 case VIRTIO_PCI_COMMON_MSIX: 1167 val = vdev->config_vector; 1168 break; 1169 case VIRTIO_PCI_COMMON_NUMQ: 1170 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) { 1171 if (virtio_queue_get_num(vdev, i)) { 1172 val = i + 1; 1173 } 1174 } 1175 break; 1176 case VIRTIO_PCI_COMMON_STATUS: 1177 val = vdev->status; 1178 break; 1179 case VIRTIO_PCI_COMMON_CFGGENERATION: 1180 val = vdev->generation; 1181 break; 1182 case VIRTIO_PCI_COMMON_Q_SELECT: 1183 val = vdev->queue_sel; 1184 break; 1185 case VIRTIO_PCI_COMMON_Q_SIZE: 1186 val = virtio_queue_get_num(vdev, vdev->queue_sel); 1187 break; 1188 case VIRTIO_PCI_COMMON_Q_MSIX: 1189 val = virtio_queue_vector(vdev, vdev->queue_sel); 1190 break; 1191 case VIRTIO_PCI_COMMON_Q_ENABLE: 1192 val = proxy->vqs[vdev->queue_sel].enabled; 1193 break; 1194 case VIRTIO_PCI_COMMON_Q_NOFF: 1195 /* Simply map queues in order */ 1196 val = vdev->queue_sel; 1197 break; 1198 case VIRTIO_PCI_COMMON_Q_DESCLO: 1199 val = proxy->vqs[vdev->queue_sel].desc[0]; 1200 break; 1201 case VIRTIO_PCI_COMMON_Q_DESCHI: 1202 val = proxy->vqs[vdev->queue_sel].desc[1]; 1203 break; 1204 case VIRTIO_PCI_COMMON_Q_AVAILLO: 1205 val = proxy->vqs[vdev->queue_sel].avail[0]; 1206 break; 1207 case VIRTIO_PCI_COMMON_Q_AVAILHI: 1208 val = proxy->vqs[vdev->queue_sel].avail[1]; 1209 break; 1210 case VIRTIO_PCI_COMMON_Q_USEDLO: 1211 val = proxy->vqs[vdev->queue_sel].used[0]; 1212 break; 1213 case VIRTIO_PCI_COMMON_Q_USEDHI: 1214 val = proxy->vqs[vdev->queue_sel].used[1]; 1215 break; 1216 default: 1217 val = 0; 1218 } 1219 1220 return val; 1221 } 1222 1223 static void virtio_pci_common_write(void *opaque, hwaddr addr, 1224 uint64_t val, unsigned size) 1225 { 1226 VirtIOPCIProxy *proxy = opaque; 1227 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1228 1229 switch (addr) { 1230 case VIRTIO_PCI_COMMON_DFSELECT: 1231 proxy->dfselect = val; 1232 break; 1233 case VIRTIO_PCI_COMMON_GFSELECT: 1234 proxy->gfselect = val; 1235 break; 1236 case VIRTIO_PCI_COMMON_GF: 1237 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) { 1238 proxy->guest_features[proxy->gfselect] = val; 1239 virtio_set_features(vdev, 1240 (((uint64_t)proxy->guest_features[1]) << 32) | 1241 proxy->guest_features[0]); 1242 } 1243 break; 1244 case VIRTIO_PCI_COMMON_MSIX: 1245 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 1246 /* Make it possible for guest to discover an error took place. */ 1247 if (msix_vector_use(&proxy->pci_dev, val) < 0) { 1248 val = VIRTIO_NO_VECTOR; 1249 } 1250 vdev->config_vector = val; 1251 break; 1252 case VIRTIO_PCI_COMMON_STATUS: 1253 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 1254 virtio_pci_stop_ioeventfd(proxy); 1255 } 1256 1257 virtio_set_status(vdev, val & 0xFF); 1258 1259 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 1260 virtio_pci_start_ioeventfd(proxy); 1261 } 1262 1263 if (vdev->status == 0) { 1264 virtio_pci_reset(DEVICE(proxy)); 1265 } 1266 1267 break; 1268 case VIRTIO_PCI_COMMON_Q_SELECT: 1269 if (val < VIRTIO_QUEUE_MAX) { 1270 vdev->queue_sel = val; 1271 } 1272 break; 1273 case VIRTIO_PCI_COMMON_Q_SIZE: 1274 proxy->vqs[vdev->queue_sel].num = val; 1275 virtio_queue_set_num(vdev, vdev->queue_sel, 1276 proxy->vqs[vdev->queue_sel].num); 1277 break; 1278 case VIRTIO_PCI_COMMON_Q_MSIX: 1279 msix_vector_unuse(&proxy->pci_dev, 1280 virtio_queue_vector(vdev, vdev->queue_sel)); 1281 /* Make it possible for guest to discover an error took place. */ 1282 if (msix_vector_use(&proxy->pci_dev, val) < 0) { 1283 val = VIRTIO_NO_VECTOR; 1284 } 1285 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 1286 break; 1287 case VIRTIO_PCI_COMMON_Q_ENABLE: 1288 if (val == 1) { 1289 virtio_queue_set_num(vdev, vdev->queue_sel, 1290 proxy->vqs[vdev->queue_sel].num); 1291 virtio_queue_set_rings(vdev, vdev->queue_sel, 1292 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 | 1293 proxy->vqs[vdev->queue_sel].desc[0], 1294 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 | 1295 proxy->vqs[vdev->queue_sel].avail[0], 1296 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 | 1297 proxy->vqs[vdev->queue_sel].used[0]); 1298 proxy->vqs[vdev->queue_sel].enabled = 1; 1299 } else { 1300 virtio_error(vdev, "wrong value for queue_enable %"PRIx64, val); 1301 } 1302 break; 1303 case VIRTIO_PCI_COMMON_Q_DESCLO: 1304 proxy->vqs[vdev->queue_sel].desc[0] = val; 1305 break; 1306 case VIRTIO_PCI_COMMON_Q_DESCHI: 1307 proxy->vqs[vdev->queue_sel].desc[1] = val; 1308 break; 1309 case VIRTIO_PCI_COMMON_Q_AVAILLO: 1310 proxy->vqs[vdev->queue_sel].avail[0] = val; 1311 break; 1312 case VIRTIO_PCI_COMMON_Q_AVAILHI: 1313 proxy->vqs[vdev->queue_sel].avail[1] = val; 1314 break; 1315 case VIRTIO_PCI_COMMON_Q_USEDLO: 1316 proxy->vqs[vdev->queue_sel].used[0] = val; 1317 break; 1318 case VIRTIO_PCI_COMMON_Q_USEDHI: 1319 proxy->vqs[vdev->queue_sel].used[1] = val; 1320 break; 1321 default: 1322 break; 1323 } 1324 } 1325 1326 1327 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr, 1328 unsigned size) 1329 { 1330 return 0; 1331 } 1332 1333 static void virtio_pci_notify_write(void *opaque, hwaddr addr, 1334 uint64_t val, unsigned size) 1335 { 1336 VirtIOPCIProxy *proxy = opaque; 1337 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1338 1339 unsigned queue = addr / virtio_pci_queue_mem_mult(proxy); 1340 1341 if (vdev != NULL && queue < VIRTIO_QUEUE_MAX) { 1342 virtio_queue_notify(vdev, queue); 1343 } 1344 } 1345 1346 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr, 1347 uint64_t val, unsigned size) 1348 { 1349 VirtIOPCIProxy *proxy = opaque; 1350 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1351 1352 unsigned queue = val; 1353 1354 if (vdev != NULL && queue < VIRTIO_QUEUE_MAX) { 1355 virtio_queue_notify(vdev, queue); 1356 } 1357 } 1358 1359 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr, 1360 unsigned size) 1361 { 1362 VirtIOPCIProxy *proxy = opaque; 1363 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1364 uint64_t val = atomic_xchg(&vdev->isr, 0); 1365 pci_irq_deassert(&proxy->pci_dev); 1366 1367 return val; 1368 } 1369 1370 static void virtio_pci_isr_write(void *opaque, hwaddr addr, 1371 uint64_t val, unsigned size) 1372 { 1373 } 1374 1375 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr, 1376 unsigned size) 1377 { 1378 VirtIOPCIProxy *proxy = opaque; 1379 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1380 uint64_t val = 0; 1381 1382 if (vdev == NULL) { 1383 return val; 1384 } 1385 1386 switch (size) { 1387 case 1: 1388 val = virtio_config_modern_readb(vdev, addr); 1389 break; 1390 case 2: 1391 val = virtio_config_modern_readw(vdev, addr); 1392 break; 1393 case 4: 1394 val = virtio_config_modern_readl(vdev, addr); 1395 break; 1396 } 1397 return val; 1398 } 1399 1400 static void virtio_pci_device_write(void *opaque, hwaddr addr, 1401 uint64_t val, unsigned size) 1402 { 1403 VirtIOPCIProxy *proxy = opaque; 1404 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1405 1406 if (vdev == NULL) { 1407 return; 1408 } 1409 1410 switch (size) { 1411 case 1: 1412 virtio_config_modern_writeb(vdev, addr, val); 1413 break; 1414 case 2: 1415 virtio_config_modern_writew(vdev, addr, val); 1416 break; 1417 case 4: 1418 virtio_config_modern_writel(vdev, addr, val); 1419 break; 1420 } 1421 } 1422 1423 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy) 1424 { 1425 static const MemoryRegionOps common_ops = { 1426 .read = virtio_pci_common_read, 1427 .write = virtio_pci_common_write, 1428 .impl = { 1429 .min_access_size = 1, 1430 .max_access_size = 4, 1431 }, 1432 .endianness = DEVICE_LITTLE_ENDIAN, 1433 }; 1434 static const MemoryRegionOps isr_ops = { 1435 .read = virtio_pci_isr_read, 1436 .write = virtio_pci_isr_write, 1437 .impl = { 1438 .min_access_size = 1, 1439 .max_access_size = 4, 1440 }, 1441 .endianness = DEVICE_LITTLE_ENDIAN, 1442 }; 1443 static const MemoryRegionOps device_ops = { 1444 .read = virtio_pci_device_read, 1445 .write = virtio_pci_device_write, 1446 .impl = { 1447 .min_access_size = 1, 1448 .max_access_size = 4, 1449 }, 1450 .endianness = DEVICE_LITTLE_ENDIAN, 1451 }; 1452 static const MemoryRegionOps notify_ops = { 1453 .read = virtio_pci_notify_read, 1454 .write = virtio_pci_notify_write, 1455 .impl = { 1456 .min_access_size = 1, 1457 .max_access_size = 4, 1458 }, 1459 .endianness = DEVICE_LITTLE_ENDIAN, 1460 }; 1461 static const MemoryRegionOps notify_pio_ops = { 1462 .read = virtio_pci_notify_read, 1463 .write = virtio_pci_notify_write_pio, 1464 .impl = { 1465 .min_access_size = 1, 1466 .max_access_size = 4, 1467 }, 1468 .endianness = DEVICE_LITTLE_ENDIAN, 1469 }; 1470 1471 1472 memory_region_init_io(&proxy->common.mr, OBJECT(proxy), 1473 &common_ops, 1474 proxy, 1475 "virtio-pci-common", 1476 proxy->common.size); 1477 1478 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy), 1479 &isr_ops, 1480 proxy, 1481 "virtio-pci-isr", 1482 proxy->isr.size); 1483 1484 memory_region_init_io(&proxy->device.mr, OBJECT(proxy), 1485 &device_ops, 1486 proxy, 1487 "virtio-pci-device", 1488 proxy->device.size); 1489 1490 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy), 1491 ¬ify_ops, 1492 proxy, 1493 "virtio-pci-notify", 1494 proxy->notify.size); 1495 1496 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy), 1497 ¬ify_pio_ops, 1498 proxy, 1499 "virtio-pci-notify-pio", 1500 proxy->notify_pio.size); 1501 } 1502 1503 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy, 1504 VirtIOPCIRegion *region, 1505 struct virtio_pci_cap *cap, 1506 MemoryRegion *mr, 1507 uint8_t bar) 1508 { 1509 memory_region_add_subregion(mr, region->offset, ®ion->mr); 1510 1511 cap->cfg_type = region->type; 1512 cap->bar = bar; 1513 cap->offset = cpu_to_le32(region->offset); 1514 cap->length = cpu_to_le32(region->size); 1515 virtio_pci_add_mem_cap(proxy, cap); 1516 1517 } 1518 1519 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy, 1520 VirtIOPCIRegion *region, 1521 struct virtio_pci_cap *cap) 1522 { 1523 virtio_pci_modern_region_map(proxy, region, cap, 1524 &proxy->modern_bar, proxy->modern_mem_bar_idx); 1525 } 1526 1527 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy, 1528 VirtIOPCIRegion *region, 1529 struct virtio_pci_cap *cap) 1530 { 1531 virtio_pci_modern_region_map(proxy, region, cap, 1532 &proxy->io_bar, proxy->modern_io_bar_idx); 1533 } 1534 1535 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy, 1536 VirtIOPCIRegion *region) 1537 { 1538 memory_region_del_subregion(&proxy->modern_bar, 1539 ®ion->mr); 1540 } 1541 1542 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy, 1543 VirtIOPCIRegion *region) 1544 { 1545 memory_region_del_subregion(&proxy->io_bar, 1546 ®ion->mr); 1547 } 1548 1549 static void virtio_pci_pre_plugged(DeviceState *d, Error **errp) 1550 { 1551 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1552 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1553 1554 if (virtio_pci_modern(proxy)) { 1555 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); 1556 } 1557 1558 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE); 1559 } 1560 1561 /* This is called by virtio-bus just after the device is plugged. */ 1562 static void virtio_pci_device_plugged(DeviceState *d, Error **errp) 1563 { 1564 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1565 VirtioBusState *bus = &proxy->bus; 1566 bool legacy = virtio_pci_legacy(proxy); 1567 bool modern; 1568 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; 1569 uint8_t *config; 1570 uint32_t size; 1571 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1572 1573 /* 1574 * Virtio capabilities present without 1575 * VIRTIO_F_VERSION_1 confuses guests 1576 */ 1577 if (!proxy->ignore_backend_features && 1578 !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) { 1579 virtio_pci_disable_modern(proxy); 1580 1581 if (!legacy) { 1582 error_setg(errp, "Device doesn't support modern mode, and legacy" 1583 " mode is disabled"); 1584 error_append_hint(errp, "Set disable-legacy to off\n"); 1585 1586 return; 1587 } 1588 } 1589 1590 modern = virtio_pci_modern(proxy); 1591 1592 config = proxy->pci_dev.config; 1593 if (proxy->class_code) { 1594 pci_config_set_class(config, proxy->class_code); 1595 } 1596 1597 if (legacy) { 1598 if (!virtio_legacy_allowed(vdev)) { 1599 error_setg(errp, "device is modern-only, use disable-legacy=on"); 1600 return; 1601 } 1602 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) { 1603 error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by" 1604 " neither legacy nor transitional device"); 1605 return ; 1606 } 1607 /* 1608 * Legacy and transitional devices use specific subsystem IDs. 1609 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID) 1610 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default. 1611 */ 1612 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); 1613 } else { 1614 /* pure virtio-1.0 */ 1615 pci_set_word(config + PCI_VENDOR_ID, 1616 PCI_VENDOR_ID_REDHAT_QUMRANET); 1617 pci_set_word(config + PCI_DEVICE_ID, 1618 0x1040 + virtio_bus_get_vdev_id(bus)); 1619 pci_config_set_revision(config, 1); 1620 } 1621 config[PCI_INTERRUPT_PIN] = 1; 1622 1623 1624 if (modern) { 1625 struct virtio_pci_cap cap = { 1626 .cap_len = sizeof cap, 1627 }; 1628 struct virtio_pci_notify_cap notify = { 1629 .cap.cap_len = sizeof notify, 1630 .notify_off_multiplier = 1631 cpu_to_le32(virtio_pci_queue_mem_mult(proxy)), 1632 }; 1633 struct virtio_pci_cfg_cap cfg = { 1634 .cap.cap_len = sizeof cfg, 1635 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG, 1636 }; 1637 struct virtio_pci_notify_cap notify_pio = { 1638 .cap.cap_len = sizeof notify, 1639 .notify_off_multiplier = cpu_to_le32(0x0), 1640 }; 1641 1642 struct virtio_pci_cfg_cap *cfg_mask; 1643 1644 virtio_pci_modern_regions_init(proxy); 1645 1646 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap); 1647 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap); 1648 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap); 1649 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap); 1650 1651 if (modern_pio) { 1652 memory_region_init(&proxy->io_bar, OBJECT(proxy), 1653 "virtio-pci-io", 0x4); 1654 1655 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx, 1656 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar); 1657 1658 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio, 1659 ¬ify_pio.cap); 1660 } 1661 1662 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx, 1663 PCI_BASE_ADDRESS_SPACE_MEMORY | 1664 PCI_BASE_ADDRESS_MEM_PREFETCH | 1665 PCI_BASE_ADDRESS_MEM_TYPE_64, 1666 &proxy->modern_bar); 1667 1668 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap); 1669 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap); 1670 pci_set_byte(&cfg_mask->cap.bar, ~0x0); 1671 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0); 1672 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0); 1673 pci_set_long(cfg_mask->pci_cfg_data, ~0x0); 1674 } 1675 1676 if (proxy->nvectors) { 1677 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1678 proxy->msix_bar_idx, NULL); 1679 if (err) { 1680 /* Notice when a system that supports MSIx can't initialize it */ 1681 if (err != -ENOTSUP) { 1682 warn_report("unable to init msix vectors to %" PRIu32, 1683 proxy->nvectors); 1684 } 1685 proxy->nvectors = 0; 1686 } 1687 } 1688 1689 proxy->pci_dev.config_write = virtio_write_config; 1690 proxy->pci_dev.config_read = virtio_read_config; 1691 1692 if (legacy) { 1693 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) 1694 + virtio_bus_get_vdev_config_len(bus); 1695 size = pow2ceil(size); 1696 1697 memory_region_init_io(&proxy->bar, OBJECT(proxy), 1698 &virtio_pci_config_ops, 1699 proxy, "virtio-pci", size); 1700 1701 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx, 1702 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar); 1703 } 1704 } 1705 1706 static void virtio_pci_device_unplugged(DeviceState *d) 1707 { 1708 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1709 bool modern = virtio_pci_modern(proxy); 1710 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; 1711 1712 virtio_pci_stop_ioeventfd(proxy); 1713 1714 if (modern) { 1715 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common); 1716 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr); 1717 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device); 1718 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify); 1719 if (modern_pio) { 1720 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio); 1721 } 1722 } 1723 } 1724 1725 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) 1726 { 1727 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 1728 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); 1729 bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) && 1730 !pci_bus_is_root(pci_get_bus(pci_dev)); 1731 1732 if (kvm_enabled() && !kvm_has_many_ioeventfds()) { 1733 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; 1734 } 1735 1736 /* 1737 * virtio pci bar layout used by default. 1738 * subclasses can re-arrange things if needed. 1739 * 1740 * region 0 -- virtio legacy io bar 1741 * region 1 -- msi-x bar 1742 * region 2 -- virtio modern io bar (off by default) 1743 * region 4+5 -- virtio modern memory (64bit) bar 1744 * 1745 */ 1746 proxy->legacy_io_bar_idx = 0; 1747 proxy->msix_bar_idx = 1; 1748 proxy->modern_io_bar_idx = 2; 1749 proxy->modern_mem_bar_idx = 4; 1750 1751 proxy->common.offset = 0x0; 1752 proxy->common.size = 0x1000; 1753 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG; 1754 1755 proxy->isr.offset = 0x1000; 1756 proxy->isr.size = 0x1000; 1757 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG; 1758 1759 proxy->device.offset = 0x2000; 1760 proxy->device.size = 0x1000; 1761 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG; 1762 1763 proxy->notify.offset = 0x3000; 1764 proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX; 1765 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG; 1766 1767 proxy->notify_pio.offset = 0x0; 1768 proxy->notify_pio.size = 0x4; 1769 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG; 1770 1771 /* subclasses can enforce modern, so do this unconditionally */ 1772 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci", 1773 /* PCI BAR regions must be powers of 2 */ 1774 pow2ceil(proxy->notify.offset + proxy->notify.size)); 1775 1776 if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) { 1777 proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; 1778 } 1779 1780 if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) { 1781 error_setg(errp, "device cannot work as neither modern nor legacy mode" 1782 " is enabled"); 1783 error_append_hint(errp, "Set either disable-modern or disable-legacy" 1784 " to off\n"); 1785 return; 1786 } 1787 1788 if (pcie_port && pci_is_express(pci_dev)) { 1789 int pos; 1790 1791 pos = pcie_endpoint_cap_init(pci_dev, 0); 1792 assert(pos > 0); 1793 1794 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, 1795 PCI_PM_SIZEOF, errp); 1796 if (pos < 0) { 1797 return; 1798 } 1799 1800 pci_dev->exp.pm_cap = pos; 1801 1802 /* 1803 * Indicates that this function complies with revision 1.2 of the 1804 * PCI Power Management Interface Specification. 1805 */ 1806 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3); 1807 1808 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) { 1809 /* Init error enabling flags */ 1810 pcie_cap_deverr_init(pci_dev); 1811 } 1812 1813 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) { 1814 /* Init Link Control Register */ 1815 pcie_cap_lnkctl_init(pci_dev); 1816 } 1817 1818 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) { 1819 /* Init Power Management Control Register */ 1820 pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL, 1821 PCI_PM_CTRL_STATE_MASK); 1822 } 1823 1824 if (proxy->flags & VIRTIO_PCI_FLAG_ATS) { 1825 pcie_ats_init(pci_dev, 256); 1826 } 1827 1828 if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) { 1829 /* Set Function Level Reset capability bit */ 1830 pcie_cap_flr_init(pci_dev); 1831 } 1832 } else { 1833 /* 1834 * make future invocations of pci_is_express() return false 1835 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE. 1836 */ 1837 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS; 1838 } 1839 1840 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy); 1841 if (k->realize) { 1842 k->realize(proxy, errp); 1843 } 1844 } 1845 1846 static void virtio_pci_exit(PCIDevice *pci_dev) 1847 { 1848 msix_uninit_exclusive_bar(pci_dev); 1849 } 1850 1851 static void virtio_pci_reset(DeviceState *qdev) 1852 { 1853 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1854 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 1855 PCIDevice *dev = PCI_DEVICE(qdev); 1856 int i; 1857 1858 virtio_pci_stop_ioeventfd(proxy); 1859 virtio_bus_reset(bus); 1860 msix_unuse_all_vectors(&proxy->pci_dev); 1861 1862 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { 1863 proxy->vqs[i].enabled = 0; 1864 proxy->vqs[i].num = 0; 1865 proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0; 1866 proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0; 1867 proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0; 1868 } 1869 1870 if (pci_is_express(dev)) { 1871 pcie_cap_deverr_reset(dev); 1872 pcie_cap_lnkctl_reset(dev); 1873 1874 pci_set_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL, 0); 1875 } 1876 } 1877 1878 static Property virtio_pci_properties[] = { 1879 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags, 1880 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false), 1881 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags, 1882 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true), 1883 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags, 1884 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false), 1885 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags, 1886 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false), 1887 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags, 1888 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false), 1889 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy, 1890 ignore_backend_features, false), 1891 DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags, 1892 VIRTIO_PCI_FLAG_ATS_BIT, false), 1893 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags, 1894 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true), 1895 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags, 1896 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true), 1897 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags, 1898 VIRTIO_PCI_FLAG_INIT_PM_BIT, true), 1899 DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy, flags, 1900 VIRTIO_PCI_FLAG_INIT_FLR_BIT, true), 1901 DEFINE_PROP_END_OF_LIST(), 1902 }; 1903 1904 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp) 1905 { 1906 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev); 1907 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1908 PCIDevice *pci_dev = &proxy->pci_dev; 1909 1910 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) && 1911 virtio_pci_modern(proxy)) { 1912 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; 1913 } 1914 1915 vpciklass->parent_dc_realize(qdev, errp); 1916 } 1917 1918 static void virtio_pci_class_init(ObjectClass *klass, void *data) 1919 { 1920 DeviceClass *dc = DEVICE_CLASS(klass); 1921 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1922 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1923 1924 device_class_set_props(dc, virtio_pci_properties); 1925 k->realize = virtio_pci_realize; 1926 k->exit = virtio_pci_exit; 1927 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1928 k->revision = VIRTIO_PCI_ABI_VERSION; 1929 k->class_id = PCI_CLASS_OTHERS; 1930 device_class_set_parent_realize(dc, virtio_pci_dc_realize, 1931 &vpciklass->parent_dc_realize); 1932 dc->reset = virtio_pci_reset; 1933 } 1934 1935 static const TypeInfo virtio_pci_info = { 1936 .name = TYPE_VIRTIO_PCI, 1937 .parent = TYPE_PCI_DEVICE, 1938 .instance_size = sizeof(VirtIOPCIProxy), 1939 .class_init = virtio_pci_class_init, 1940 .class_size = sizeof(VirtioPCIClass), 1941 .abstract = true, 1942 }; 1943 1944 static Property virtio_pci_generic_properties[] = { 1945 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy, 1946 ON_OFF_AUTO_AUTO), 1947 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false), 1948 DEFINE_PROP_END_OF_LIST(), 1949 }; 1950 1951 static void virtio_pci_base_class_init(ObjectClass *klass, void *data) 1952 { 1953 const VirtioPCIDeviceTypeInfo *t = data; 1954 if (t->class_init) { 1955 t->class_init(klass, NULL); 1956 } 1957 } 1958 1959 static void virtio_pci_generic_class_init(ObjectClass *klass, void *data) 1960 { 1961 DeviceClass *dc = DEVICE_CLASS(klass); 1962 1963 device_class_set_props(dc, virtio_pci_generic_properties); 1964 } 1965 1966 static void virtio_pci_transitional_instance_init(Object *obj) 1967 { 1968 VirtIOPCIProxy *proxy = VIRTIO_PCI(obj); 1969 1970 proxy->disable_legacy = ON_OFF_AUTO_OFF; 1971 proxy->disable_modern = false; 1972 } 1973 1974 static void virtio_pci_non_transitional_instance_init(Object *obj) 1975 { 1976 VirtIOPCIProxy *proxy = VIRTIO_PCI(obj); 1977 1978 proxy->disable_legacy = ON_OFF_AUTO_ON; 1979 proxy->disable_modern = false; 1980 } 1981 1982 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t) 1983 { 1984 char *base_name = NULL; 1985 TypeInfo base_type_info = { 1986 .name = t->base_name, 1987 .parent = t->parent ? t->parent : TYPE_VIRTIO_PCI, 1988 .instance_size = t->instance_size, 1989 .instance_init = t->instance_init, 1990 .class_size = t->class_size, 1991 .abstract = true, 1992 .interfaces = t->interfaces, 1993 }; 1994 TypeInfo generic_type_info = { 1995 .name = t->generic_name, 1996 .parent = base_type_info.name, 1997 .class_init = virtio_pci_generic_class_init, 1998 .interfaces = (InterfaceInfo[]) { 1999 { INTERFACE_PCIE_DEVICE }, 2000 { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 2001 { } 2002 }, 2003 }; 2004 2005 if (!base_type_info.name) { 2006 /* No base type -> register a single generic device type */ 2007 /* use intermediate %s-base-type to add generic device props */ 2008 base_name = g_strdup_printf("%s-base-type", t->generic_name); 2009 base_type_info.name = base_name; 2010 base_type_info.class_init = virtio_pci_generic_class_init; 2011 2012 generic_type_info.parent = base_name; 2013 generic_type_info.class_init = virtio_pci_base_class_init; 2014 generic_type_info.class_data = (void *)t; 2015 2016 assert(!t->non_transitional_name); 2017 assert(!t->transitional_name); 2018 } else { 2019 base_type_info.class_init = virtio_pci_base_class_init; 2020 base_type_info.class_data = (void *)t; 2021 } 2022 2023 type_register(&base_type_info); 2024 if (generic_type_info.name) { 2025 type_register(&generic_type_info); 2026 } 2027 2028 if (t->non_transitional_name) { 2029 const TypeInfo non_transitional_type_info = { 2030 .name = t->non_transitional_name, 2031 .parent = base_type_info.name, 2032 .instance_init = virtio_pci_non_transitional_instance_init, 2033 .interfaces = (InterfaceInfo[]) { 2034 { INTERFACE_PCIE_DEVICE }, 2035 { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 2036 { } 2037 }, 2038 }; 2039 type_register(&non_transitional_type_info); 2040 } 2041 2042 if (t->transitional_name) { 2043 const TypeInfo transitional_type_info = { 2044 .name = t->transitional_name, 2045 .parent = base_type_info.name, 2046 .instance_init = virtio_pci_transitional_instance_init, 2047 .interfaces = (InterfaceInfo[]) { 2048 /* 2049 * Transitional virtio devices work only as Conventional PCI 2050 * devices because they require PIO ports. 2051 */ 2052 { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 2053 { } 2054 }, 2055 }; 2056 type_register(&transitional_type_info); 2057 } 2058 g_free(base_name); 2059 } 2060 2061 /* virtio-pci-bus */ 2062 2063 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 2064 VirtIOPCIProxy *dev) 2065 { 2066 DeviceState *qdev = DEVICE(dev); 2067 char virtio_bus_name[] = "virtio-bus"; 2068 2069 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 2070 virtio_bus_name); 2071 } 2072 2073 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 2074 { 2075 BusClass *bus_class = BUS_CLASS(klass); 2076 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 2077 bus_class->max_dev = 1; 2078 k->notify = virtio_pci_notify; 2079 k->save_config = virtio_pci_save_config; 2080 k->load_config = virtio_pci_load_config; 2081 k->save_queue = virtio_pci_save_queue; 2082 k->load_queue = virtio_pci_load_queue; 2083 k->save_extra_state = virtio_pci_save_extra_state; 2084 k->load_extra_state = virtio_pci_load_extra_state; 2085 k->has_extra_state = virtio_pci_has_extra_state; 2086 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 2087 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 2088 k->set_host_notifier_mr = virtio_pci_set_host_notifier_mr; 2089 k->vmstate_change = virtio_pci_vmstate_change; 2090 k->pre_plugged = virtio_pci_pre_plugged; 2091 k->device_plugged = virtio_pci_device_plugged; 2092 k->device_unplugged = virtio_pci_device_unplugged; 2093 k->query_nvectors = virtio_pci_query_nvectors; 2094 k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled; 2095 k->ioeventfd_assign = virtio_pci_ioeventfd_assign; 2096 k->get_dma_as = virtio_pci_get_dma_as; 2097 k->queue_enabled = virtio_pci_queue_enabled; 2098 } 2099 2100 static const TypeInfo virtio_pci_bus_info = { 2101 .name = TYPE_VIRTIO_PCI_BUS, 2102 .parent = TYPE_VIRTIO_BUS, 2103 .instance_size = sizeof(VirtioPCIBusState), 2104 .class_init = virtio_pci_bus_class_init, 2105 }; 2106 2107 static void virtio_pci_register_types(void) 2108 { 2109 /* Base types: */ 2110 type_register_static(&virtio_pci_bus_info); 2111 type_register_static(&virtio_pci_info); 2112 } 2113 2114 type_init(virtio_pci_register_types) 2115 2116