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/pci/pci.h" 28 #include "qemu/error-report.h" 29 #include "hw/pci/msi.h" 30 #include "hw/pci/msix.h" 31 #include "hw/loader.h" 32 #include "sysemu/kvm.h" 33 #include "sysemu/block-backend.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 /* The remaining space is defined by each driver as the per-driver 42 * configuration space */ 43 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev)) 44 45 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 46 VirtIOPCIProxy *dev); 47 48 /* virtio device */ 49 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ 50 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) 51 { 52 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 53 } 54 55 /* DeviceState to VirtIOPCIProxy. Note: used on datapath, 56 * be careful and test performance if you change this. 57 */ 58 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) 59 { 60 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 61 } 62 63 static void virtio_pci_notify(DeviceState *d, uint16_t vector) 64 { 65 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); 66 67 if (msix_enabled(&proxy->pci_dev)) 68 msix_notify(&proxy->pci_dev, vector); 69 else { 70 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 71 pci_set_irq(&proxy->pci_dev, vdev->isr & 1); 72 } 73 } 74 75 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) 76 { 77 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 78 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 79 80 pci_device_save(&proxy->pci_dev, f); 81 msix_save(&proxy->pci_dev, f); 82 if (msix_present(&proxy->pci_dev)) 83 qemu_put_be16(f, vdev->config_vector); 84 } 85 86 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) 87 { 88 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 89 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 90 91 if (msix_present(&proxy->pci_dev)) 92 qemu_put_be16(f, virtio_queue_vector(vdev, n)); 93 } 94 95 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) 96 { 97 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 98 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 99 100 int ret; 101 ret = pci_device_load(&proxy->pci_dev, f); 102 if (ret) { 103 return ret; 104 } 105 msix_unuse_all_vectors(&proxy->pci_dev); 106 msix_load(&proxy->pci_dev, f); 107 if (msix_present(&proxy->pci_dev)) { 108 qemu_get_be16s(f, &vdev->config_vector); 109 } else { 110 vdev->config_vector = VIRTIO_NO_VECTOR; 111 } 112 if (vdev->config_vector != VIRTIO_NO_VECTOR) { 113 return msix_vector_use(&proxy->pci_dev, vdev->config_vector); 114 } 115 return 0; 116 } 117 118 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) 119 { 120 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 121 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 122 123 uint16_t vector; 124 if (msix_present(&proxy->pci_dev)) { 125 qemu_get_be16s(f, &vector); 126 } else { 127 vector = VIRTIO_NO_VECTOR; 128 } 129 virtio_queue_set_vector(vdev, n, vector); 130 if (vector != VIRTIO_NO_VECTOR) { 131 return msix_vector_use(&proxy->pci_dev, vector); 132 } 133 return 0; 134 } 135 136 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, 137 int n, bool assign, bool set_handler) 138 { 139 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 140 VirtQueue *vq = virtio_get_queue(vdev, n); 141 EventNotifier *notifier = virtio_queue_get_host_notifier(vq); 142 int r = 0; 143 144 if (assign) { 145 r = event_notifier_init(notifier, 1); 146 if (r < 0) { 147 error_report("%s: unable to init event notifier: %d", 148 __func__, r); 149 return r; 150 } 151 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler); 152 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, 153 true, n, notifier); 154 } else { 155 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, 156 true, n, notifier); 157 virtio_queue_set_host_notifier_fd_handler(vq, false, false); 158 event_notifier_cleanup(notifier); 159 } 160 return r; 161 } 162 163 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) 164 { 165 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 166 int n, r; 167 168 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || 169 proxy->ioeventfd_disabled || 170 proxy->ioeventfd_started) { 171 return; 172 } 173 174 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { 175 if (!virtio_queue_get_num(vdev, n)) { 176 continue; 177 } 178 179 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true); 180 if (r < 0) { 181 goto assign_error; 182 } 183 } 184 proxy->ioeventfd_started = true; 185 return; 186 187 assign_error: 188 while (--n >= 0) { 189 if (!virtio_queue_get_num(vdev, n)) { 190 continue; 191 } 192 193 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); 194 assert(r >= 0); 195 } 196 proxy->ioeventfd_started = false; 197 error_report("%s: failed. Fallback to a userspace (slower).", __func__); 198 } 199 200 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) 201 { 202 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 203 int r; 204 int n; 205 206 if (!proxy->ioeventfd_started) { 207 return; 208 } 209 210 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) { 211 if (!virtio_queue_get_num(vdev, n)) { 212 continue; 213 } 214 215 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); 216 assert(r >= 0); 217 } 218 proxy->ioeventfd_started = false; 219 } 220 221 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) 222 { 223 VirtIOPCIProxy *proxy = opaque; 224 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 225 hwaddr pa; 226 227 switch (addr) { 228 case VIRTIO_PCI_GUEST_FEATURES: 229 /* Guest does not negotiate properly? We have to assume nothing. */ 230 if (val & (1 << VIRTIO_F_BAD_FEATURE)) { 231 val = virtio_bus_get_vdev_bad_features(&proxy->bus); 232 } 233 virtio_set_features(vdev, val); 234 break; 235 case VIRTIO_PCI_QUEUE_PFN: 236 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; 237 if (pa == 0) { 238 virtio_pci_stop_ioeventfd(proxy); 239 virtio_reset(vdev); 240 msix_unuse_all_vectors(&proxy->pci_dev); 241 } 242 else 243 virtio_queue_set_addr(vdev, vdev->queue_sel, pa); 244 break; 245 case VIRTIO_PCI_QUEUE_SEL: 246 if (val < VIRTIO_QUEUE_MAX) 247 vdev->queue_sel = val; 248 break; 249 case VIRTIO_PCI_QUEUE_NOTIFY: 250 if (val < VIRTIO_QUEUE_MAX) { 251 virtio_queue_notify(vdev, val); 252 } 253 break; 254 case VIRTIO_PCI_STATUS: 255 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 256 virtio_pci_stop_ioeventfd(proxy); 257 } 258 259 virtio_set_status(vdev, val & 0xFF); 260 261 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 262 virtio_pci_start_ioeventfd(proxy); 263 } 264 265 if (vdev->status == 0) { 266 virtio_reset(vdev); 267 msix_unuse_all_vectors(&proxy->pci_dev); 268 } 269 270 /* Linux before 2.6.34 drives the device without enabling 271 the PCI device bus master bit. Enable it automatically 272 for the guest. This is a PCI spec violation but so is 273 initiating DMA with bus master bit clear. */ 274 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) { 275 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, 276 proxy->pci_dev.config[PCI_COMMAND] | 277 PCI_COMMAND_MASTER, 1); 278 } 279 break; 280 case VIRTIO_MSI_CONFIG_VECTOR: 281 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 282 /* Make it possible for guest to discover an error took place. */ 283 if (msix_vector_use(&proxy->pci_dev, val) < 0) 284 val = VIRTIO_NO_VECTOR; 285 vdev->config_vector = val; 286 break; 287 case VIRTIO_MSI_QUEUE_VECTOR: 288 msix_vector_unuse(&proxy->pci_dev, 289 virtio_queue_vector(vdev, vdev->queue_sel)); 290 /* Make it possible for guest to discover an error took place. */ 291 if (msix_vector_use(&proxy->pci_dev, val) < 0) 292 val = VIRTIO_NO_VECTOR; 293 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 294 break; 295 default: 296 error_report("%s: unexpected address 0x%x value 0x%x", 297 __func__, addr, val); 298 break; 299 } 300 } 301 302 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) 303 { 304 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 305 uint32_t ret = 0xFFFFFFFF; 306 307 switch (addr) { 308 case VIRTIO_PCI_HOST_FEATURES: 309 ret = vdev->host_features; 310 break; 311 case VIRTIO_PCI_GUEST_FEATURES: 312 ret = vdev->guest_features; 313 break; 314 case VIRTIO_PCI_QUEUE_PFN: 315 ret = virtio_queue_get_addr(vdev, vdev->queue_sel) 316 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; 317 break; 318 case VIRTIO_PCI_QUEUE_NUM: 319 ret = virtio_queue_get_num(vdev, vdev->queue_sel); 320 break; 321 case VIRTIO_PCI_QUEUE_SEL: 322 ret = vdev->queue_sel; 323 break; 324 case VIRTIO_PCI_STATUS: 325 ret = vdev->status; 326 break; 327 case VIRTIO_PCI_ISR: 328 /* reading from the ISR also clears it. */ 329 ret = vdev->isr; 330 vdev->isr = 0; 331 pci_irq_deassert(&proxy->pci_dev); 332 break; 333 case VIRTIO_MSI_CONFIG_VECTOR: 334 ret = vdev->config_vector; 335 break; 336 case VIRTIO_MSI_QUEUE_VECTOR: 337 ret = virtio_queue_vector(vdev, vdev->queue_sel); 338 break; 339 default: 340 break; 341 } 342 343 return ret; 344 } 345 346 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, 347 unsigned size) 348 { 349 VirtIOPCIProxy *proxy = opaque; 350 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 351 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); 352 uint64_t val = 0; 353 if (addr < config) { 354 return virtio_ioport_read(proxy, addr); 355 } 356 addr -= config; 357 358 switch (size) { 359 case 1: 360 val = virtio_config_readb(vdev, addr); 361 break; 362 case 2: 363 val = virtio_config_readw(vdev, addr); 364 if (virtio_is_big_endian(vdev)) { 365 val = bswap16(val); 366 } 367 break; 368 case 4: 369 val = virtio_config_readl(vdev, addr); 370 if (virtio_is_big_endian(vdev)) { 371 val = bswap32(val); 372 } 373 break; 374 } 375 return val; 376 } 377 378 static void virtio_pci_config_write(void *opaque, hwaddr addr, 379 uint64_t val, unsigned size) 380 { 381 VirtIOPCIProxy *proxy = opaque; 382 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); 383 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 384 if (addr < config) { 385 virtio_ioport_write(proxy, addr, val); 386 return; 387 } 388 addr -= config; 389 /* 390 * Virtio-PCI is odd. Ioports are LE but config space is target native 391 * endian. 392 */ 393 switch (size) { 394 case 1: 395 virtio_config_writeb(vdev, addr, val); 396 break; 397 case 2: 398 if (virtio_is_big_endian(vdev)) { 399 val = bswap16(val); 400 } 401 virtio_config_writew(vdev, addr, val); 402 break; 403 case 4: 404 if (virtio_is_big_endian(vdev)) { 405 val = bswap32(val); 406 } 407 virtio_config_writel(vdev, addr, val); 408 break; 409 } 410 } 411 412 static const MemoryRegionOps virtio_pci_config_ops = { 413 .read = virtio_pci_config_read, 414 .write = virtio_pci_config_write, 415 .impl = { 416 .min_access_size = 1, 417 .max_access_size = 4, 418 }, 419 .endianness = DEVICE_LITTLE_ENDIAN, 420 }; 421 422 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, 423 uint32_t val, int len) 424 { 425 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 426 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 427 428 pci_default_write_config(pci_dev, address, val, len); 429 430 if (range_covers_byte(address, len, PCI_COMMAND) && 431 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 432 virtio_pci_stop_ioeventfd(proxy); 433 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); 434 } 435 } 436 437 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, 438 unsigned int queue_no, 439 unsigned int vector, 440 MSIMessage msg) 441 { 442 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 443 int ret; 444 445 if (irqfd->users == 0) { 446 ret = kvm_irqchip_add_msi_route(kvm_state, msg); 447 if (ret < 0) { 448 return ret; 449 } 450 irqfd->virq = ret; 451 } 452 irqfd->users++; 453 return 0; 454 } 455 456 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, 457 unsigned int vector) 458 { 459 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 460 if (--irqfd->users == 0) { 461 kvm_irqchip_release_virq(kvm_state, irqfd->virq); 462 } 463 } 464 465 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, 466 unsigned int queue_no, 467 unsigned int vector) 468 { 469 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 470 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 471 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 472 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 473 int ret; 474 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq); 475 return ret; 476 } 477 478 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, 479 unsigned int queue_no, 480 unsigned int vector) 481 { 482 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 483 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 484 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 485 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 486 int ret; 487 488 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq); 489 assert(ret == 0); 490 } 491 492 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) 493 { 494 PCIDevice *dev = &proxy->pci_dev; 495 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 496 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 497 unsigned int vector; 498 int ret, queue_no; 499 MSIMessage msg; 500 501 for (queue_no = 0; queue_no < nvqs; queue_no++) { 502 if (!virtio_queue_get_num(vdev, queue_no)) { 503 break; 504 } 505 vector = virtio_queue_vector(vdev, queue_no); 506 if (vector >= msix_nr_vectors_allocated(dev)) { 507 continue; 508 } 509 msg = msix_get_message(dev, vector); 510 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); 511 if (ret < 0) { 512 goto undo; 513 } 514 /* If guest supports masking, set up irqfd now. 515 * Otherwise, delay until unmasked in the frontend. 516 */ 517 if (k->guest_notifier_mask) { 518 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 519 if (ret < 0) { 520 kvm_virtio_pci_vq_vector_release(proxy, vector); 521 goto undo; 522 } 523 } 524 } 525 return 0; 526 527 undo: 528 while (--queue_no >= 0) { 529 vector = virtio_queue_vector(vdev, queue_no); 530 if (vector >= msix_nr_vectors_allocated(dev)) { 531 continue; 532 } 533 if (k->guest_notifier_mask) { 534 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 535 } 536 kvm_virtio_pci_vq_vector_release(proxy, vector); 537 } 538 return ret; 539 } 540 541 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) 542 { 543 PCIDevice *dev = &proxy->pci_dev; 544 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 545 unsigned int vector; 546 int queue_no; 547 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 548 549 for (queue_no = 0; queue_no < nvqs; queue_no++) { 550 if (!virtio_queue_get_num(vdev, queue_no)) { 551 break; 552 } 553 vector = virtio_queue_vector(vdev, queue_no); 554 if (vector >= msix_nr_vectors_allocated(dev)) { 555 continue; 556 } 557 /* If guest supports masking, clean up irqfd now. 558 * Otherwise, it was cleaned when masked in the frontend. 559 */ 560 if (k->guest_notifier_mask) { 561 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 562 } 563 kvm_virtio_pci_vq_vector_release(proxy, vector); 564 } 565 } 566 567 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, 568 unsigned int queue_no, 569 unsigned int vector, 570 MSIMessage msg) 571 { 572 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 573 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 574 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 575 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 576 VirtIOIRQFD *irqfd; 577 int ret = 0; 578 579 if (proxy->vector_irqfd) { 580 irqfd = &proxy->vector_irqfd[vector]; 581 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { 582 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg); 583 if (ret < 0) { 584 return ret; 585 } 586 } 587 } 588 589 /* If guest supports masking, irqfd is already setup, unmask it. 590 * Otherwise, set it up now. 591 */ 592 if (k->guest_notifier_mask) { 593 k->guest_notifier_mask(vdev, queue_no, false); 594 /* Test after unmasking to avoid losing events. */ 595 if (k->guest_notifier_pending && 596 k->guest_notifier_pending(vdev, queue_no)) { 597 event_notifier_set(n); 598 } 599 } else { 600 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 601 } 602 return ret; 603 } 604 605 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, 606 unsigned int queue_no, 607 unsigned int vector) 608 { 609 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 610 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 611 612 /* If guest supports masking, keep irqfd but mask it. 613 * Otherwise, clean it up now. 614 */ 615 if (k->guest_notifier_mask) { 616 k->guest_notifier_mask(vdev, queue_no, true); 617 } else { 618 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 619 } 620 } 621 622 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, 623 MSIMessage msg) 624 { 625 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 626 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 627 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 628 int ret, index, unmasked = 0; 629 630 while (vq) { 631 index = virtio_get_queue_index(vq); 632 if (!virtio_queue_get_num(vdev, index)) { 633 break; 634 } 635 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg); 636 if (ret < 0) { 637 goto undo; 638 } 639 vq = virtio_vector_next_queue(vq); 640 ++unmasked; 641 } 642 643 return 0; 644 645 undo: 646 vq = virtio_vector_first_queue(vdev, vector); 647 while (vq && --unmasked >= 0) { 648 index = virtio_get_queue_index(vq); 649 virtio_pci_vq_vector_mask(proxy, index, vector); 650 vq = virtio_vector_next_queue(vq); 651 } 652 return ret; 653 } 654 655 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) 656 { 657 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 658 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 659 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 660 int index; 661 662 while (vq) { 663 index = virtio_get_queue_index(vq); 664 if (!virtio_queue_get_num(vdev, index)) { 665 break; 666 } 667 virtio_pci_vq_vector_mask(proxy, index, vector); 668 vq = virtio_vector_next_queue(vq); 669 } 670 } 671 672 static void virtio_pci_vector_poll(PCIDevice *dev, 673 unsigned int vector_start, 674 unsigned int vector_end) 675 { 676 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 677 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 678 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 679 int queue_no; 680 unsigned int vector; 681 EventNotifier *notifier; 682 VirtQueue *vq; 683 684 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 685 if (!virtio_queue_get_num(vdev, queue_no)) { 686 break; 687 } 688 vector = virtio_queue_vector(vdev, queue_no); 689 if (vector < vector_start || vector >= vector_end || 690 !msix_is_masked(dev, vector)) { 691 continue; 692 } 693 vq = virtio_get_queue(vdev, queue_no); 694 notifier = virtio_queue_get_guest_notifier(vq); 695 if (k->guest_notifier_pending) { 696 if (k->guest_notifier_pending(vdev, queue_no)) { 697 msix_set_pending(dev, vector); 698 } 699 } else if (event_notifier_test_and_clear(notifier)) { 700 msix_set_pending(dev, vector); 701 } 702 } 703 } 704 705 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, 706 bool with_irqfd) 707 { 708 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 709 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 710 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 711 VirtQueue *vq = virtio_get_queue(vdev, n); 712 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 713 714 if (assign) { 715 int r = event_notifier_init(notifier, 0); 716 if (r < 0) { 717 return r; 718 } 719 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 720 } else { 721 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 722 event_notifier_cleanup(notifier); 723 } 724 725 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) { 726 vdc->guest_notifier_mask(vdev, n, !assign); 727 } 728 729 return 0; 730 } 731 732 static bool virtio_pci_query_guest_notifiers(DeviceState *d) 733 { 734 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 735 return msix_enabled(&proxy->pci_dev); 736 } 737 738 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) 739 { 740 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 741 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 742 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 743 int r, n; 744 bool with_irqfd = msix_enabled(&proxy->pci_dev) && 745 kvm_msi_via_irqfd_enabled(); 746 747 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX); 748 749 /* When deassigning, pass a consistent nvqs value 750 * to avoid leaking notifiers. 751 */ 752 assert(assign || nvqs == proxy->nvqs_with_notifiers); 753 754 proxy->nvqs_with_notifiers = nvqs; 755 756 /* Must unset vector notifier while guest notifier is still assigned */ 757 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) { 758 msix_unset_vector_notifiers(&proxy->pci_dev); 759 if (proxy->vector_irqfd) { 760 kvm_virtio_pci_vector_release(proxy, nvqs); 761 g_free(proxy->vector_irqfd); 762 proxy->vector_irqfd = NULL; 763 } 764 } 765 766 for (n = 0; n < nvqs; n++) { 767 if (!virtio_queue_get_num(vdev, n)) { 768 break; 769 } 770 771 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd); 772 if (r < 0) { 773 goto assign_error; 774 } 775 } 776 777 /* Must set vector notifier after guest notifier has been assigned */ 778 if ((with_irqfd || k->guest_notifier_mask) && assign) { 779 if (with_irqfd) { 780 proxy->vector_irqfd = 781 g_malloc0(sizeof(*proxy->vector_irqfd) * 782 msix_nr_vectors_allocated(&proxy->pci_dev)); 783 r = kvm_virtio_pci_vector_use(proxy, nvqs); 784 if (r < 0) { 785 goto assign_error; 786 } 787 } 788 r = msix_set_vector_notifiers(&proxy->pci_dev, 789 virtio_pci_vector_unmask, 790 virtio_pci_vector_mask, 791 virtio_pci_vector_poll); 792 if (r < 0) { 793 goto notifiers_error; 794 } 795 } 796 797 return 0; 798 799 notifiers_error: 800 if (with_irqfd) { 801 assert(assign); 802 kvm_virtio_pci_vector_release(proxy, nvqs); 803 } 804 805 assign_error: 806 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ 807 assert(assign); 808 while (--n >= 0) { 809 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); 810 } 811 return r; 812 } 813 814 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign) 815 { 816 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 817 818 /* Stop using ioeventfd for virtqueue kick if the device starts using host 819 * notifiers. This makes it easy to avoid stepping on each others' toes. 820 */ 821 proxy->ioeventfd_disabled = assign; 822 if (assign) { 823 virtio_pci_stop_ioeventfd(proxy); 824 } 825 /* We don't need to start here: it's not needed because backend 826 * currently only stops on status change away from ok, 827 * reset, vmstop and such. If we do add code to start here, 828 * need to check vmstate, device state etc. */ 829 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false); 830 } 831 832 static void virtio_pci_vmstate_change(DeviceState *d, bool running) 833 { 834 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 835 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 836 837 if (running) { 838 /* Old QEMU versions did not set bus master enable on status write. 839 * Detect DRIVER set and enable it. 840 */ 841 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) && 842 (vdev->status & VIRTIO_CONFIG_S_DRIVER) && 843 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 844 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, 845 proxy->pci_dev.config[PCI_COMMAND] | 846 PCI_COMMAND_MASTER, 1); 847 } 848 virtio_pci_start_ioeventfd(proxy); 849 } else { 850 virtio_pci_stop_ioeventfd(proxy); 851 } 852 } 853 854 #ifdef CONFIG_VIRTFS 855 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 856 { 857 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev); 858 DeviceState *vdev = DEVICE(&dev->vdev); 859 860 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 861 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 862 } 863 864 static Property virtio_9p_pci_properties[] = { 865 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 866 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 867 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 868 DEFINE_PROP_END_OF_LIST(), 869 }; 870 871 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data) 872 { 873 DeviceClass *dc = DEVICE_CLASS(klass); 874 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 875 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 876 877 k->realize = virtio_9p_pci_realize; 878 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 879 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P; 880 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 881 pcidev_k->class_id = 0x2; 882 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 883 dc->props = virtio_9p_pci_properties; 884 } 885 886 static void virtio_9p_pci_instance_init(Object *obj) 887 { 888 V9fsPCIState *dev = VIRTIO_9P_PCI(obj); 889 890 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 891 TYPE_VIRTIO_9P); 892 } 893 894 static const TypeInfo virtio_9p_pci_info = { 895 .name = TYPE_VIRTIO_9P_PCI, 896 .parent = TYPE_VIRTIO_PCI, 897 .instance_size = sizeof(V9fsPCIState), 898 .instance_init = virtio_9p_pci_instance_init, 899 .class_init = virtio_9p_pci_class_init, 900 }; 901 #endif /* CONFIG_VIRTFS */ 902 903 /* 904 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 905 */ 906 907 static int virtio_pci_query_nvectors(DeviceState *d) 908 { 909 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 910 911 return proxy->nvectors; 912 } 913 914 /* This is called by virtio-bus just after the device is plugged. */ 915 static void virtio_pci_device_plugged(DeviceState *d, Error **errp) 916 { 917 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 918 VirtioBusState *bus = &proxy->bus; 919 uint8_t *config; 920 uint32_t size; 921 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 922 923 config = proxy->pci_dev.config; 924 if (proxy->class_code) { 925 pci_config_set_class(config, proxy->class_code); 926 } 927 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, 928 pci_get_word(config + PCI_VENDOR_ID)); 929 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); 930 config[PCI_INTERRUPT_PIN] = 1; 931 932 if (proxy->nvectors && 933 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) { 934 error_report("unable to init msix vectors to %" PRIu32, 935 proxy->nvectors); 936 proxy->nvectors = 0; 937 } 938 939 proxy->pci_dev.config_write = virtio_write_config; 940 941 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) 942 + virtio_bus_get_vdev_config_len(bus); 943 if (size & (size - 1)) { 944 size = 1 << qemu_fls(size); 945 } 946 947 memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops, 948 proxy, "virtio-pci", size); 949 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, 950 &proxy->bar); 951 952 if (!kvm_has_many_ioeventfds()) { 953 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; 954 } 955 956 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE); 957 } 958 959 static void virtio_pci_device_unplugged(DeviceState *d) 960 { 961 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 962 963 virtio_pci_stop_ioeventfd(proxy); 964 } 965 966 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) 967 { 968 VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev); 969 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); 970 971 virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev); 972 if (k->realize) { 973 k->realize(dev, errp); 974 } 975 } 976 977 static void virtio_pci_exit(PCIDevice *pci_dev) 978 { 979 msix_uninit_exclusive_bar(pci_dev); 980 } 981 982 static void virtio_pci_reset(DeviceState *qdev) 983 { 984 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 985 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 986 virtio_pci_stop_ioeventfd(proxy); 987 virtio_bus_reset(bus); 988 msix_unuse_all_vectors(&proxy->pci_dev); 989 } 990 991 static Property virtio_pci_properties[] = { 992 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags, 993 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false), 994 DEFINE_PROP_END_OF_LIST(), 995 }; 996 997 static void virtio_pci_class_init(ObjectClass *klass, void *data) 998 { 999 DeviceClass *dc = DEVICE_CLASS(klass); 1000 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1001 1002 dc->props = virtio_pci_properties; 1003 k->realize = virtio_pci_realize; 1004 k->exit = virtio_pci_exit; 1005 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1006 k->revision = VIRTIO_PCI_ABI_VERSION; 1007 k->class_id = PCI_CLASS_OTHERS; 1008 dc->reset = virtio_pci_reset; 1009 } 1010 1011 static const TypeInfo virtio_pci_info = { 1012 .name = TYPE_VIRTIO_PCI, 1013 .parent = TYPE_PCI_DEVICE, 1014 .instance_size = sizeof(VirtIOPCIProxy), 1015 .class_init = virtio_pci_class_init, 1016 .class_size = sizeof(VirtioPCIClass), 1017 .abstract = true, 1018 }; 1019 1020 /* virtio-blk-pci */ 1021 1022 static Property virtio_blk_pci_properties[] = { 1023 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1024 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1025 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1026 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1027 DEFINE_PROP_END_OF_LIST(), 1028 }; 1029 1030 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1031 { 1032 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); 1033 DeviceState *vdev = DEVICE(&dev->vdev); 1034 1035 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1036 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1037 } 1038 1039 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) 1040 { 1041 DeviceClass *dc = DEVICE_CLASS(klass); 1042 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1043 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1044 1045 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1046 dc->props = virtio_blk_pci_properties; 1047 k->realize = virtio_blk_pci_realize; 1048 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1049 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; 1050 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1051 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1052 } 1053 1054 static void virtio_blk_pci_instance_init(Object *obj) 1055 { 1056 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); 1057 1058 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1059 TYPE_VIRTIO_BLK); 1060 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 1061 &error_abort); 1062 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1063 "bootindex", &error_abort); 1064 } 1065 1066 static const TypeInfo virtio_blk_pci_info = { 1067 .name = TYPE_VIRTIO_BLK_PCI, 1068 .parent = TYPE_VIRTIO_PCI, 1069 .instance_size = sizeof(VirtIOBlkPCI), 1070 .instance_init = virtio_blk_pci_instance_init, 1071 .class_init = virtio_blk_pci_class_init, 1072 }; 1073 1074 /* virtio-scsi-pci */ 1075 1076 static Property virtio_scsi_pci_properties[] = { 1077 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1078 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1079 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1080 DEV_NVECTORS_UNSPECIFIED), 1081 DEFINE_PROP_END_OF_LIST(), 1082 }; 1083 1084 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1085 { 1086 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); 1087 DeviceState *vdev = DEVICE(&dev->vdev); 1088 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1089 DeviceState *proxy = DEVICE(vpci_dev); 1090 char *bus_name; 1091 1092 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1093 vpci_dev->nvectors = vs->conf.num_queues + 3; 1094 } 1095 1096 /* 1097 * For command line compatibility, this sets the virtio-scsi-device bus 1098 * name as before. 1099 */ 1100 if (proxy->id) { 1101 bus_name = g_strdup_printf("%s.0", proxy->id); 1102 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1103 g_free(bus_name); 1104 } 1105 1106 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1107 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1108 } 1109 1110 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) 1111 { 1112 DeviceClass *dc = DEVICE_CLASS(klass); 1113 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1114 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1115 1116 k->realize = virtio_scsi_pci_realize; 1117 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1118 dc->props = virtio_scsi_pci_properties; 1119 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1120 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1121 pcidev_k->revision = 0x00; 1122 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1123 } 1124 1125 static void virtio_scsi_pci_instance_init(Object *obj) 1126 { 1127 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); 1128 1129 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1130 TYPE_VIRTIO_SCSI); 1131 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread", 1132 &error_abort); 1133 } 1134 1135 static const TypeInfo virtio_scsi_pci_info = { 1136 .name = TYPE_VIRTIO_SCSI_PCI, 1137 .parent = TYPE_VIRTIO_PCI, 1138 .instance_size = sizeof(VirtIOSCSIPCI), 1139 .instance_init = virtio_scsi_pci_instance_init, 1140 .class_init = virtio_scsi_pci_class_init, 1141 }; 1142 1143 /* vhost-scsi-pci */ 1144 1145 #ifdef CONFIG_VHOST_SCSI 1146 static Property vhost_scsi_pci_properties[] = { 1147 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1148 DEV_NVECTORS_UNSPECIFIED), 1149 DEFINE_PROP_END_OF_LIST(), 1150 }; 1151 1152 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1153 { 1154 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev); 1155 DeviceState *vdev = DEVICE(&dev->vdev); 1156 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1157 1158 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1159 vpci_dev->nvectors = vs->conf.num_queues + 3; 1160 } 1161 1162 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1163 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1164 } 1165 1166 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data) 1167 { 1168 DeviceClass *dc = DEVICE_CLASS(klass); 1169 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1170 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1171 k->realize = vhost_scsi_pci_realize; 1172 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1173 dc->props = vhost_scsi_pci_properties; 1174 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1175 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1176 pcidev_k->revision = 0x00; 1177 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1178 } 1179 1180 static void vhost_scsi_pci_instance_init(Object *obj) 1181 { 1182 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj); 1183 1184 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1185 TYPE_VHOST_SCSI); 1186 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1187 "bootindex", &error_abort); 1188 } 1189 1190 static const TypeInfo vhost_scsi_pci_info = { 1191 .name = TYPE_VHOST_SCSI_PCI, 1192 .parent = TYPE_VIRTIO_PCI, 1193 .instance_size = sizeof(VHostSCSIPCI), 1194 .instance_init = vhost_scsi_pci_instance_init, 1195 .class_init = vhost_scsi_pci_class_init, 1196 }; 1197 #endif 1198 1199 /* virtio-balloon-pci */ 1200 1201 static Property virtio_balloon_pci_properties[] = { 1202 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1203 DEFINE_PROP_END_OF_LIST(), 1204 }; 1205 1206 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1207 { 1208 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); 1209 DeviceState *vdev = DEVICE(&dev->vdev); 1210 1211 if (vpci_dev->class_code != PCI_CLASS_OTHERS && 1212 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ 1213 vpci_dev->class_code = PCI_CLASS_OTHERS; 1214 } 1215 1216 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1217 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1218 } 1219 1220 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) 1221 { 1222 DeviceClass *dc = DEVICE_CLASS(klass); 1223 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1224 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1225 k->realize = virtio_balloon_pci_realize; 1226 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1227 dc->props = virtio_balloon_pci_properties; 1228 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1229 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; 1230 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1231 pcidev_k->class_id = PCI_CLASS_OTHERS; 1232 } 1233 1234 static void virtio_balloon_pci_instance_init(Object *obj) 1235 { 1236 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); 1237 1238 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1239 TYPE_VIRTIO_BALLOON); 1240 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev), 1241 "guest-stats", &error_abort); 1242 object_property_add_alias(obj, "guest-stats-polling-interval", 1243 OBJECT(&dev->vdev), 1244 "guest-stats-polling-interval", &error_abort); 1245 } 1246 1247 static const TypeInfo virtio_balloon_pci_info = { 1248 .name = TYPE_VIRTIO_BALLOON_PCI, 1249 .parent = TYPE_VIRTIO_PCI, 1250 .instance_size = sizeof(VirtIOBalloonPCI), 1251 .instance_init = virtio_balloon_pci_instance_init, 1252 .class_init = virtio_balloon_pci_class_init, 1253 }; 1254 1255 /* virtio-serial-pci */ 1256 1257 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1258 { 1259 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); 1260 DeviceState *vdev = DEVICE(&dev->vdev); 1261 DeviceState *proxy = DEVICE(vpci_dev); 1262 char *bus_name; 1263 1264 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && 1265 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ 1266 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ 1267 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; 1268 } 1269 1270 /* backwards-compatibility with machines that were created with 1271 DEV_NVECTORS_UNSPECIFIED */ 1272 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1273 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; 1274 } 1275 1276 /* 1277 * For command line compatibility, this sets the virtio-serial-device bus 1278 * name as before. 1279 */ 1280 if (proxy->id) { 1281 bus_name = g_strdup_printf("%s.0", proxy->id); 1282 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1283 g_free(bus_name); 1284 } 1285 1286 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1287 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1288 } 1289 1290 static Property virtio_serial_pci_properties[] = { 1291 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1292 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1293 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1294 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1295 DEFINE_PROP_END_OF_LIST(), 1296 }; 1297 1298 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) 1299 { 1300 DeviceClass *dc = DEVICE_CLASS(klass); 1301 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1302 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1303 k->realize = virtio_serial_pci_realize; 1304 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1305 dc->props = virtio_serial_pci_properties; 1306 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1307 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; 1308 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1309 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 1310 } 1311 1312 static void virtio_serial_pci_instance_init(Object *obj) 1313 { 1314 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); 1315 1316 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1317 TYPE_VIRTIO_SERIAL); 1318 } 1319 1320 static const TypeInfo virtio_serial_pci_info = { 1321 .name = TYPE_VIRTIO_SERIAL_PCI, 1322 .parent = TYPE_VIRTIO_PCI, 1323 .instance_size = sizeof(VirtIOSerialPCI), 1324 .instance_init = virtio_serial_pci_instance_init, 1325 .class_init = virtio_serial_pci_class_init, 1326 }; 1327 1328 /* virtio-net-pci */ 1329 1330 static Property virtio_net_properties[] = { 1331 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1332 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), 1333 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 1334 DEFINE_PROP_END_OF_LIST(), 1335 }; 1336 1337 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1338 { 1339 DeviceState *qdev = DEVICE(vpci_dev); 1340 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); 1341 DeviceState *vdev = DEVICE(&dev->vdev); 1342 1343 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 1344 object_get_typename(OBJECT(qdev))); 1345 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1346 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1347 } 1348 1349 static void virtio_net_pci_class_init(ObjectClass *klass, void *data) 1350 { 1351 DeviceClass *dc = DEVICE_CLASS(klass); 1352 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1353 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1354 1355 k->romfile = "efi-virtio.rom"; 1356 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1357 k->device_id = PCI_DEVICE_ID_VIRTIO_NET; 1358 k->revision = VIRTIO_PCI_ABI_VERSION; 1359 k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1360 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 1361 dc->props = virtio_net_properties; 1362 vpciklass->realize = virtio_net_pci_realize; 1363 } 1364 1365 static void virtio_net_pci_instance_init(Object *obj) 1366 { 1367 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); 1368 1369 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1370 TYPE_VIRTIO_NET); 1371 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1372 "bootindex", &error_abort); 1373 } 1374 1375 static const TypeInfo virtio_net_pci_info = { 1376 .name = TYPE_VIRTIO_NET_PCI, 1377 .parent = TYPE_VIRTIO_PCI, 1378 .instance_size = sizeof(VirtIONetPCI), 1379 .instance_init = virtio_net_pci_instance_init, 1380 .class_init = virtio_net_pci_class_init, 1381 }; 1382 1383 /* virtio-rng-pci */ 1384 1385 static Property virtio_rng_pci_properties[] = { 1386 DEFINE_PROP_END_OF_LIST(), 1387 }; 1388 1389 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1390 { 1391 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); 1392 DeviceState *vdev = DEVICE(&vrng->vdev); 1393 Error *err = NULL; 1394 1395 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1396 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 1397 if (err) { 1398 error_propagate(errp, err); 1399 return; 1400 } 1401 1402 object_property_set_link(OBJECT(vrng), 1403 OBJECT(vrng->vdev.conf.rng), "rng", 1404 NULL); 1405 } 1406 1407 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) 1408 { 1409 DeviceClass *dc = DEVICE_CLASS(klass); 1410 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1411 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1412 1413 k->realize = virtio_rng_pci_realize; 1414 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1415 dc->props = virtio_rng_pci_properties; 1416 1417 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1418 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; 1419 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1420 pcidev_k->class_id = PCI_CLASS_OTHERS; 1421 } 1422 1423 static void virtio_rng_initfn(Object *obj) 1424 { 1425 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); 1426 1427 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1428 TYPE_VIRTIO_RNG); 1429 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng", 1430 &error_abort); 1431 } 1432 1433 static const TypeInfo virtio_rng_pci_info = { 1434 .name = TYPE_VIRTIO_RNG_PCI, 1435 .parent = TYPE_VIRTIO_PCI, 1436 .instance_size = sizeof(VirtIORngPCI), 1437 .instance_init = virtio_rng_initfn, 1438 .class_init = virtio_rng_pci_class_init, 1439 }; 1440 1441 /* virtio-pci-bus */ 1442 1443 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 1444 VirtIOPCIProxy *dev) 1445 { 1446 DeviceState *qdev = DEVICE(dev); 1447 char virtio_bus_name[] = "virtio-bus"; 1448 1449 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 1450 virtio_bus_name); 1451 } 1452 1453 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 1454 { 1455 BusClass *bus_class = BUS_CLASS(klass); 1456 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 1457 bus_class->max_dev = 1; 1458 k->notify = virtio_pci_notify; 1459 k->save_config = virtio_pci_save_config; 1460 k->load_config = virtio_pci_load_config; 1461 k->save_queue = virtio_pci_save_queue; 1462 k->load_queue = virtio_pci_load_queue; 1463 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 1464 k->set_host_notifier = virtio_pci_set_host_notifier; 1465 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 1466 k->vmstate_change = virtio_pci_vmstate_change; 1467 k->device_plugged = virtio_pci_device_plugged; 1468 k->device_unplugged = virtio_pci_device_unplugged; 1469 k->query_nvectors = virtio_pci_query_nvectors; 1470 } 1471 1472 static const TypeInfo virtio_pci_bus_info = { 1473 .name = TYPE_VIRTIO_PCI_BUS, 1474 .parent = TYPE_VIRTIO_BUS, 1475 .instance_size = sizeof(VirtioPCIBusState), 1476 .class_init = virtio_pci_bus_class_init, 1477 }; 1478 1479 static void virtio_pci_register_types(void) 1480 { 1481 type_register_static(&virtio_rng_pci_info); 1482 type_register_static(&virtio_pci_bus_info); 1483 type_register_static(&virtio_pci_info); 1484 #ifdef CONFIG_VIRTFS 1485 type_register_static(&virtio_9p_pci_info); 1486 #endif 1487 type_register_static(&virtio_blk_pci_info); 1488 type_register_static(&virtio_scsi_pci_info); 1489 type_register_static(&virtio_balloon_pci_info); 1490 type_register_static(&virtio_serial_pci_info); 1491 type_register_static(&virtio_net_pci_info); 1492 #ifdef CONFIG_VHOST_SCSI 1493 type_register_static(&vhost_scsi_pci_info); 1494 #endif 1495 } 1496 1497 type_init(virtio_pci_register_types) 1498