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