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 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, 447 uint32_t val, int len) 448 { 449 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 450 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 451 452 pci_default_write_config(pci_dev, address, val, len); 453 454 if (range_covers_byte(address, len, PCI_COMMAND) && 455 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 456 virtio_pci_stop_ioeventfd(proxy); 457 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); 458 } 459 } 460 461 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, 462 unsigned int queue_no, 463 unsigned int vector, 464 MSIMessage msg) 465 { 466 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 467 int ret; 468 469 if (irqfd->users == 0) { 470 ret = kvm_irqchip_add_msi_route(kvm_state, msg); 471 if (ret < 0) { 472 return ret; 473 } 474 irqfd->virq = ret; 475 } 476 irqfd->users++; 477 return 0; 478 } 479 480 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, 481 unsigned int vector) 482 { 483 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 484 if (--irqfd->users == 0) { 485 kvm_irqchip_release_virq(kvm_state, irqfd->virq); 486 } 487 } 488 489 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, 490 unsigned int queue_no, 491 unsigned int vector) 492 { 493 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 494 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 495 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 496 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 497 int ret; 498 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq); 499 return ret; 500 } 501 502 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, 503 unsigned int queue_no, 504 unsigned int vector) 505 { 506 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 507 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 508 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 509 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 510 int ret; 511 512 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq); 513 assert(ret == 0); 514 } 515 516 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) 517 { 518 PCIDevice *dev = &proxy->pci_dev; 519 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 520 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 521 unsigned int vector; 522 int ret, queue_no; 523 MSIMessage msg; 524 525 for (queue_no = 0; queue_no < nvqs; queue_no++) { 526 if (!virtio_queue_get_num(vdev, queue_no)) { 527 break; 528 } 529 vector = virtio_queue_vector(vdev, queue_no); 530 if (vector >= msix_nr_vectors_allocated(dev)) { 531 continue; 532 } 533 msg = msix_get_message(dev, vector); 534 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); 535 if (ret < 0) { 536 goto undo; 537 } 538 /* If guest supports masking, set up irqfd now. 539 * Otherwise, delay until unmasked in the frontend. 540 */ 541 if (k->guest_notifier_mask) { 542 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 543 if (ret < 0) { 544 kvm_virtio_pci_vq_vector_release(proxy, vector); 545 goto undo; 546 } 547 } 548 } 549 return 0; 550 551 undo: 552 while (--queue_no >= 0) { 553 vector = virtio_queue_vector(vdev, queue_no); 554 if (vector >= msix_nr_vectors_allocated(dev)) { 555 continue; 556 } 557 if (k->guest_notifier_mask) { 558 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 559 } 560 kvm_virtio_pci_vq_vector_release(proxy, vector); 561 } 562 return ret; 563 } 564 565 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) 566 { 567 PCIDevice *dev = &proxy->pci_dev; 568 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 569 unsigned int vector; 570 int queue_no; 571 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 572 573 for (queue_no = 0; queue_no < nvqs; queue_no++) { 574 if (!virtio_queue_get_num(vdev, queue_no)) { 575 break; 576 } 577 vector = virtio_queue_vector(vdev, queue_no); 578 if (vector >= msix_nr_vectors_allocated(dev)) { 579 continue; 580 } 581 /* If guest supports masking, clean up irqfd now. 582 * Otherwise, it was cleaned when masked in the frontend. 583 */ 584 if (k->guest_notifier_mask) { 585 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 586 } 587 kvm_virtio_pci_vq_vector_release(proxy, vector); 588 } 589 } 590 591 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, 592 unsigned int queue_no, 593 unsigned int vector, 594 MSIMessage msg) 595 { 596 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 597 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 598 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 599 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 600 VirtIOIRQFD *irqfd; 601 int ret = 0; 602 603 if (proxy->vector_irqfd) { 604 irqfd = &proxy->vector_irqfd[vector]; 605 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { 606 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg); 607 if (ret < 0) { 608 return ret; 609 } 610 } 611 } 612 613 /* If guest supports masking, irqfd is already setup, unmask it. 614 * Otherwise, set it up now. 615 */ 616 if (k->guest_notifier_mask) { 617 k->guest_notifier_mask(vdev, queue_no, false); 618 /* Test after unmasking to avoid losing events. */ 619 if (k->guest_notifier_pending && 620 k->guest_notifier_pending(vdev, queue_no)) { 621 event_notifier_set(n); 622 } 623 } else { 624 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 625 } 626 return ret; 627 } 628 629 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, 630 unsigned int queue_no, 631 unsigned int vector) 632 { 633 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 634 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 635 636 /* If guest supports masking, keep irqfd but mask it. 637 * Otherwise, clean it up now. 638 */ 639 if (k->guest_notifier_mask) { 640 k->guest_notifier_mask(vdev, queue_no, true); 641 } else { 642 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 643 } 644 } 645 646 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, 647 MSIMessage msg) 648 { 649 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 650 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 651 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 652 int ret, index, unmasked = 0; 653 654 while (vq) { 655 index = virtio_get_queue_index(vq); 656 if (!virtio_queue_get_num(vdev, index)) { 657 break; 658 } 659 if (index < proxy->nvqs_with_notifiers) { 660 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg); 661 if (ret < 0) { 662 goto undo; 663 } 664 ++unmasked; 665 } 666 vq = virtio_vector_next_queue(vq); 667 } 668 669 return 0; 670 671 undo: 672 vq = virtio_vector_first_queue(vdev, vector); 673 while (vq && unmasked >= 0) { 674 index = virtio_get_queue_index(vq); 675 if (index < proxy->nvqs_with_notifiers) { 676 virtio_pci_vq_vector_mask(proxy, index, vector); 677 --unmasked; 678 } 679 vq = virtio_vector_next_queue(vq); 680 } 681 return ret; 682 } 683 684 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) 685 { 686 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 687 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 688 VirtQueue *vq = virtio_vector_first_queue(vdev, vector); 689 int index; 690 691 while (vq) { 692 index = virtio_get_queue_index(vq); 693 if (!virtio_queue_get_num(vdev, index)) { 694 break; 695 } 696 if (index < proxy->nvqs_with_notifiers) { 697 virtio_pci_vq_vector_mask(proxy, index, vector); 698 } 699 vq = virtio_vector_next_queue(vq); 700 } 701 } 702 703 static void virtio_pci_vector_poll(PCIDevice *dev, 704 unsigned int vector_start, 705 unsigned int vector_end) 706 { 707 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 708 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 709 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 710 int queue_no; 711 unsigned int vector; 712 EventNotifier *notifier; 713 VirtQueue *vq; 714 715 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 716 if (!virtio_queue_get_num(vdev, queue_no)) { 717 break; 718 } 719 vector = virtio_queue_vector(vdev, queue_no); 720 if (vector < vector_start || vector >= vector_end || 721 !msix_is_masked(dev, vector)) { 722 continue; 723 } 724 vq = virtio_get_queue(vdev, queue_no); 725 notifier = virtio_queue_get_guest_notifier(vq); 726 if (k->guest_notifier_pending) { 727 if (k->guest_notifier_pending(vdev, queue_no)) { 728 msix_set_pending(dev, vector); 729 } 730 } else if (event_notifier_test_and_clear(notifier)) { 731 msix_set_pending(dev, vector); 732 } 733 } 734 } 735 736 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, 737 bool with_irqfd) 738 { 739 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 740 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 741 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 742 VirtQueue *vq = virtio_get_queue(vdev, n); 743 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 744 745 if (assign) { 746 int r = event_notifier_init(notifier, 0); 747 if (r < 0) { 748 return r; 749 } 750 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 751 } else { 752 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 753 event_notifier_cleanup(notifier); 754 } 755 756 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) { 757 vdc->guest_notifier_mask(vdev, n, !assign); 758 } 759 760 return 0; 761 } 762 763 static bool virtio_pci_query_guest_notifiers(DeviceState *d) 764 { 765 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 766 return msix_enabled(&proxy->pci_dev); 767 } 768 769 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) 770 { 771 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 772 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 773 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 774 int r, n; 775 bool with_irqfd = msix_enabled(&proxy->pci_dev) && 776 kvm_msi_via_irqfd_enabled(); 777 778 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX); 779 780 /* When deassigning, pass a consistent nvqs value 781 * to avoid leaking notifiers. 782 */ 783 assert(assign || nvqs == proxy->nvqs_with_notifiers); 784 785 proxy->nvqs_with_notifiers = nvqs; 786 787 /* Must unset vector notifier while guest notifier is still assigned */ 788 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) { 789 msix_unset_vector_notifiers(&proxy->pci_dev); 790 if (proxy->vector_irqfd) { 791 kvm_virtio_pci_vector_release(proxy, nvqs); 792 g_free(proxy->vector_irqfd); 793 proxy->vector_irqfd = NULL; 794 } 795 } 796 797 for (n = 0; n < nvqs; n++) { 798 if (!virtio_queue_get_num(vdev, n)) { 799 break; 800 } 801 802 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd); 803 if (r < 0) { 804 goto assign_error; 805 } 806 } 807 808 /* Must set vector notifier after guest notifier has been assigned */ 809 if ((with_irqfd || k->guest_notifier_mask) && assign) { 810 if (with_irqfd) { 811 proxy->vector_irqfd = 812 g_malloc0(sizeof(*proxy->vector_irqfd) * 813 msix_nr_vectors_allocated(&proxy->pci_dev)); 814 r = kvm_virtio_pci_vector_use(proxy, nvqs); 815 if (r < 0) { 816 goto assign_error; 817 } 818 } 819 r = msix_set_vector_notifiers(&proxy->pci_dev, 820 virtio_pci_vector_unmask, 821 virtio_pci_vector_mask, 822 virtio_pci_vector_poll); 823 if (r < 0) { 824 goto notifiers_error; 825 } 826 } 827 828 return 0; 829 830 notifiers_error: 831 if (with_irqfd) { 832 assert(assign); 833 kvm_virtio_pci_vector_release(proxy, nvqs); 834 } 835 836 assign_error: 837 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ 838 assert(assign); 839 while (--n >= 0) { 840 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); 841 } 842 return r; 843 } 844 845 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign) 846 { 847 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 848 849 /* Stop using ioeventfd for virtqueue kick if the device starts using host 850 * notifiers. This makes it easy to avoid stepping on each others' toes. 851 */ 852 proxy->ioeventfd_disabled = assign; 853 if (assign) { 854 virtio_pci_stop_ioeventfd(proxy); 855 } 856 /* We don't need to start here: it's not needed because backend 857 * currently only stops on status change away from ok, 858 * reset, vmstop and such. If we do add code to start here, 859 * need to check vmstate, device state etc. */ 860 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false); 861 } 862 863 static void virtio_pci_vmstate_change(DeviceState *d, bool running) 864 { 865 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 866 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 867 868 if (running) { 869 /* Old QEMU versions did not set bus master enable on status write. 870 * Detect DRIVER set and enable it. 871 */ 872 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) && 873 (vdev->status & VIRTIO_CONFIG_S_DRIVER) && 874 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 875 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, 876 proxy->pci_dev.config[PCI_COMMAND] | 877 PCI_COMMAND_MASTER, 1); 878 } 879 virtio_pci_start_ioeventfd(proxy); 880 } else { 881 virtio_pci_stop_ioeventfd(proxy); 882 } 883 } 884 885 #ifdef CONFIG_VIRTFS 886 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 887 { 888 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev); 889 DeviceState *vdev = DEVICE(&dev->vdev); 890 891 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 892 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 893 } 894 895 static Property virtio_9p_pci_properties[] = { 896 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 897 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 898 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 899 DEFINE_PROP_END_OF_LIST(), 900 }; 901 902 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data) 903 { 904 DeviceClass *dc = DEVICE_CLASS(klass); 905 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 906 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 907 908 k->realize = virtio_9p_pci_realize; 909 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 910 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P; 911 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 912 pcidev_k->class_id = 0x2; 913 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 914 dc->props = virtio_9p_pci_properties; 915 } 916 917 static void virtio_9p_pci_instance_init(Object *obj) 918 { 919 V9fsPCIState *dev = VIRTIO_9P_PCI(obj); 920 921 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 922 TYPE_VIRTIO_9P); 923 } 924 925 static const TypeInfo virtio_9p_pci_info = { 926 .name = TYPE_VIRTIO_9P_PCI, 927 .parent = TYPE_VIRTIO_PCI, 928 .instance_size = sizeof(V9fsPCIState), 929 .instance_init = virtio_9p_pci_instance_init, 930 .class_init = virtio_9p_pci_class_init, 931 }; 932 #endif /* CONFIG_VIRTFS */ 933 934 /* 935 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 936 */ 937 938 static int virtio_pci_query_nvectors(DeviceState *d) 939 { 940 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 941 942 return proxy->nvectors; 943 } 944 945 static void virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy, 946 struct virtio_pci_cap *cap) 947 { 948 PCIDevice *dev = &proxy->pci_dev; 949 int offset; 950 951 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len); 952 assert(offset > 0); 953 954 assert(cap->cap_len >= sizeof *cap); 955 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len, 956 cap->cap_len - PCI_CAP_FLAGS); 957 } 958 959 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr, 960 unsigned size) 961 { 962 VirtIOPCIProxy *proxy = opaque; 963 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 964 uint32_t val = 0; 965 int i; 966 967 switch (addr) { 968 case VIRTIO_PCI_COMMON_DFSELECT: 969 val = proxy->dfselect; 970 break; 971 case VIRTIO_PCI_COMMON_DF: 972 if (proxy->dfselect <= 1) { 973 val = vdev->host_features >> (32 * proxy->dfselect); 974 } 975 break; 976 case VIRTIO_PCI_COMMON_GFSELECT: 977 val = proxy->gfselect; 978 break; 979 case VIRTIO_PCI_COMMON_GF: 980 if (proxy->gfselect <= ARRAY_SIZE(proxy->guest_features)) { 981 val = proxy->guest_features[proxy->gfselect]; 982 } 983 break; 984 case VIRTIO_PCI_COMMON_MSIX: 985 val = vdev->config_vector; 986 break; 987 case VIRTIO_PCI_COMMON_NUMQ: 988 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) { 989 if (virtio_queue_get_num(vdev, i)) { 990 val = i + 1; 991 } 992 } 993 break; 994 case VIRTIO_PCI_COMMON_STATUS: 995 val = vdev->status; 996 break; 997 case VIRTIO_PCI_COMMON_CFGGENERATION: 998 val = vdev->generation; 999 break; 1000 case VIRTIO_PCI_COMMON_Q_SELECT: 1001 val = vdev->queue_sel; 1002 break; 1003 case VIRTIO_PCI_COMMON_Q_SIZE: 1004 val = virtio_queue_get_num(vdev, vdev->queue_sel); 1005 break; 1006 case VIRTIO_PCI_COMMON_Q_MSIX: 1007 val = virtio_queue_vector(vdev, vdev->queue_sel); 1008 break; 1009 case VIRTIO_PCI_COMMON_Q_ENABLE: 1010 val = proxy->vqs[vdev->queue_sel].enabled; 1011 break; 1012 case VIRTIO_PCI_COMMON_Q_NOFF: 1013 /* Simply map queues in order */ 1014 val = vdev->queue_sel; 1015 break; 1016 case VIRTIO_PCI_COMMON_Q_DESCLO: 1017 val = proxy->vqs[vdev->queue_sel].desc[0]; 1018 break; 1019 case VIRTIO_PCI_COMMON_Q_DESCHI: 1020 val = proxy->vqs[vdev->queue_sel].desc[1]; 1021 break; 1022 case VIRTIO_PCI_COMMON_Q_AVAILLO: 1023 val = proxy->vqs[vdev->queue_sel].avail[0]; 1024 break; 1025 case VIRTIO_PCI_COMMON_Q_AVAILHI: 1026 val = proxy->vqs[vdev->queue_sel].avail[1]; 1027 break; 1028 case VIRTIO_PCI_COMMON_Q_USEDLO: 1029 val = proxy->vqs[vdev->queue_sel].used[0]; 1030 break; 1031 case VIRTIO_PCI_COMMON_Q_USEDHI: 1032 val = proxy->vqs[vdev->queue_sel].used[1]; 1033 break; 1034 default: 1035 val = 0; 1036 } 1037 1038 return val; 1039 } 1040 1041 static void virtio_pci_common_write(void *opaque, hwaddr addr, 1042 uint64_t val, unsigned size) 1043 { 1044 VirtIOPCIProxy *proxy = opaque; 1045 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1046 1047 switch (addr) { 1048 case VIRTIO_PCI_COMMON_DFSELECT: 1049 proxy->dfselect = val; 1050 break; 1051 case VIRTIO_PCI_COMMON_GFSELECT: 1052 proxy->gfselect = val; 1053 break; 1054 case VIRTIO_PCI_COMMON_GF: 1055 if (proxy->gfselect <= ARRAY_SIZE(proxy->guest_features)) { 1056 proxy->guest_features[proxy->gfselect] = val; 1057 virtio_set_features(vdev, 1058 (((uint64_t)proxy->guest_features[1]) << 32) | 1059 proxy->guest_features[0]); 1060 } 1061 break; 1062 case VIRTIO_PCI_COMMON_MSIX: 1063 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 1064 /* Make it possible for guest to discover an error took place. */ 1065 if (msix_vector_use(&proxy->pci_dev, val) < 0) { 1066 val = VIRTIO_NO_VECTOR; 1067 } 1068 vdev->config_vector = val; 1069 break; 1070 case VIRTIO_PCI_COMMON_STATUS: 1071 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 1072 virtio_pci_stop_ioeventfd(proxy); 1073 } 1074 1075 virtio_set_status(vdev, val & 0xFF); 1076 1077 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 1078 virtio_pci_start_ioeventfd(proxy); 1079 } 1080 1081 if (vdev->status == 0) { 1082 virtio_reset(vdev); 1083 msix_unuse_all_vectors(&proxy->pci_dev); 1084 } 1085 1086 break; 1087 case VIRTIO_PCI_COMMON_Q_SELECT: 1088 if (val < VIRTIO_QUEUE_MAX) { 1089 vdev->queue_sel = val; 1090 } 1091 break; 1092 case VIRTIO_PCI_COMMON_Q_SIZE: 1093 proxy->vqs[vdev->queue_sel].num = val; 1094 break; 1095 case VIRTIO_PCI_COMMON_Q_MSIX: 1096 msix_vector_unuse(&proxy->pci_dev, 1097 virtio_queue_vector(vdev, vdev->queue_sel)); 1098 /* Make it possible for guest to discover an error took place. */ 1099 if (msix_vector_use(&proxy->pci_dev, val) < 0) { 1100 val = VIRTIO_NO_VECTOR; 1101 } 1102 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 1103 break; 1104 case VIRTIO_PCI_COMMON_Q_ENABLE: 1105 /* TODO: need a way to put num back on reset. */ 1106 virtio_queue_set_num(vdev, vdev->queue_sel, 1107 proxy->vqs[vdev->queue_sel].num); 1108 virtio_queue_set_rings(vdev, vdev->queue_sel, 1109 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 | 1110 proxy->vqs[vdev->queue_sel].desc[0], 1111 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 | 1112 proxy->vqs[vdev->queue_sel].avail[0], 1113 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 | 1114 proxy->vqs[vdev->queue_sel].used[0]); 1115 break; 1116 case VIRTIO_PCI_COMMON_Q_DESCLO: 1117 proxy->vqs[vdev->queue_sel].desc[0] = val; 1118 break; 1119 case VIRTIO_PCI_COMMON_Q_DESCHI: 1120 proxy->vqs[vdev->queue_sel].desc[1] = val; 1121 break; 1122 case VIRTIO_PCI_COMMON_Q_AVAILLO: 1123 proxy->vqs[vdev->queue_sel].avail[0] = val; 1124 break; 1125 case VIRTIO_PCI_COMMON_Q_AVAILHI: 1126 proxy->vqs[vdev->queue_sel].avail[1] = val; 1127 break; 1128 case VIRTIO_PCI_COMMON_Q_USEDLO: 1129 proxy->vqs[vdev->queue_sel].used[0] = val; 1130 break; 1131 case VIRTIO_PCI_COMMON_Q_USEDHI: 1132 proxy->vqs[vdev->queue_sel].used[1] = val; 1133 break; 1134 default: 1135 break; 1136 } 1137 } 1138 1139 1140 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr, 1141 unsigned size) 1142 { 1143 return 0; 1144 } 1145 1146 static void virtio_pci_notify_write(void *opaque, hwaddr addr, 1147 uint64_t val, unsigned size) 1148 { 1149 VirtIODevice *vdev = opaque; 1150 unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT; 1151 1152 if (queue < VIRTIO_QUEUE_MAX) { 1153 virtio_queue_notify(vdev, queue); 1154 } 1155 } 1156 1157 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr, 1158 unsigned size) 1159 { 1160 VirtIOPCIProxy *proxy = opaque; 1161 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1162 uint64_t val = vdev->isr; 1163 1164 vdev->isr = 0; 1165 pci_irq_deassert(&proxy->pci_dev); 1166 1167 return val; 1168 } 1169 1170 static void virtio_pci_isr_write(void *opaque, hwaddr addr, 1171 uint64_t val, unsigned size) 1172 { 1173 } 1174 1175 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr, 1176 unsigned size) 1177 { 1178 VirtIODevice *vdev = opaque; 1179 uint64_t val = 0; 1180 1181 switch (size) { 1182 case 1: 1183 val = virtio_config_modern_readb(vdev, addr); 1184 break; 1185 case 2: 1186 val = virtio_config_modern_readw(vdev, addr); 1187 break; 1188 case 4: 1189 val = virtio_config_modern_readl(vdev, addr); 1190 break; 1191 } 1192 return val; 1193 } 1194 1195 static void virtio_pci_device_write(void *opaque, hwaddr addr, 1196 uint64_t val, unsigned size) 1197 { 1198 VirtIODevice *vdev = opaque; 1199 switch (size) { 1200 case 1: 1201 virtio_config_modern_writeb(vdev, addr, val); 1202 break; 1203 case 2: 1204 virtio_config_modern_writew(vdev, addr, val); 1205 break; 1206 case 4: 1207 virtio_config_modern_writel(vdev, addr, val); 1208 break; 1209 } 1210 } 1211 1212 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy) 1213 { 1214 static const MemoryRegionOps common_ops = { 1215 .read = virtio_pci_common_read, 1216 .write = virtio_pci_common_write, 1217 .impl = { 1218 .min_access_size = 1, 1219 .max_access_size = 4, 1220 }, 1221 .endianness = DEVICE_LITTLE_ENDIAN, 1222 }; 1223 static const MemoryRegionOps isr_ops = { 1224 .read = virtio_pci_isr_read, 1225 .write = virtio_pci_isr_write, 1226 .impl = { 1227 .min_access_size = 1, 1228 .max_access_size = 4, 1229 }, 1230 .endianness = DEVICE_LITTLE_ENDIAN, 1231 }; 1232 static const MemoryRegionOps device_ops = { 1233 .read = virtio_pci_device_read, 1234 .write = virtio_pci_device_write, 1235 .impl = { 1236 .min_access_size = 1, 1237 .max_access_size = 4, 1238 }, 1239 .endianness = DEVICE_LITTLE_ENDIAN, 1240 }; 1241 static const MemoryRegionOps notify_ops = { 1242 .read = virtio_pci_notify_read, 1243 .write = virtio_pci_notify_write, 1244 .impl = { 1245 .min_access_size = 1, 1246 .max_access_size = 4, 1247 }, 1248 .endianness = DEVICE_LITTLE_ENDIAN, 1249 }; 1250 1251 memory_region_init_io(&proxy->common.mr, OBJECT(proxy), 1252 &common_ops, 1253 proxy, 1254 "virtio-pci-common", 1255 proxy->common.size); 1256 1257 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy), 1258 &isr_ops, 1259 proxy, 1260 "virtio-pci-isr", 1261 proxy->isr.size); 1262 1263 memory_region_init_io(&proxy->device.mr, OBJECT(proxy), 1264 &device_ops, 1265 virtio_bus_get_device(&proxy->bus), 1266 "virtio-pci-device", 1267 proxy->device.size); 1268 1269 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy), 1270 ¬ify_ops, 1271 virtio_bus_get_device(&proxy->bus), 1272 "virtio-pci-notify", 1273 proxy->notify.size); 1274 } 1275 1276 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy, 1277 VirtIOPCIRegion *region, 1278 struct virtio_pci_cap *cap) 1279 { 1280 memory_region_add_subregion(&proxy->modern_bar, 1281 region->offset, 1282 ®ion->mr); 1283 1284 cap->cfg_type = region->type; 1285 cap->bar = proxy->modern_mem_bar; 1286 cap->offset = cpu_to_le32(region->offset); 1287 cap->length = cpu_to_le32(region->size); 1288 virtio_pci_add_mem_cap(proxy, cap); 1289 } 1290 1291 /* This is called by virtio-bus just after the device is plugged. */ 1292 static void virtio_pci_device_plugged(DeviceState *d, Error **errp) 1293 { 1294 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1295 VirtioBusState *bus = &proxy->bus; 1296 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY); 1297 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN); 1298 uint8_t *config; 1299 uint32_t size; 1300 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 1301 1302 config = proxy->pci_dev.config; 1303 if (proxy->class_code) { 1304 pci_config_set_class(config, proxy->class_code); 1305 } 1306 1307 if (legacy) { 1308 /* legacy and transitional */ 1309 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, 1310 pci_get_word(config + PCI_VENDOR_ID)); 1311 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); 1312 } else { 1313 /* pure virtio-1.0 */ 1314 pci_set_word(config + PCI_VENDOR_ID, 1315 PCI_VENDOR_ID_REDHAT_QUMRANET); 1316 pci_set_word(config + PCI_DEVICE_ID, 1317 0x1040 + virtio_bus_get_vdev_id(bus)); 1318 pci_config_set_revision(config, 1); 1319 } 1320 config[PCI_INTERRUPT_PIN] = 1; 1321 1322 1323 if (modern) { 1324 struct virtio_pci_cap cap = { 1325 .cap_len = sizeof cap, 1326 }; 1327 struct virtio_pci_notify_cap notify = { 1328 .cap.cap_len = sizeof notify, 1329 .notify_off_multiplier = 1330 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT), 1331 }; 1332 1333 /* TODO: add io access for speed */ 1334 1335 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); 1336 virtio_pci_modern_regions_init(proxy); 1337 virtio_pci_modern_region_map(proxy, &proxy->common, &cap); 1338 virtio_pci_modern_region_map(proxy, &proxy->isr, &cap); 1339 virtio_pci_modern_region_map(proxy, &proxy->device, &cap); 1340 virtio_pci_modern_region_map(proxy, &proxy->notify, ¬ify.cap); 1341 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar, 1342 PCI_BASE_ADDRESS_SPACE_MEMORY | 1343 PCI_BASE_ADDRESS_MEM_PREFETCH | 1344 PCI_BASE_ADDRESS_MEM_TYPE_64, 1345 &proxy->modern_bar); 1346 } 1347 1348 if (proxy->nvectors && 1349 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1350 proxy->msix_bar)) { 1351 error_report("unable to init msix vectors to %" PRIu32, 1352 proxy->nvectors); 1353 proxy->nvectors = 0; 1354 } 1355 1356 proxy->pci_dev.config_write = virtio_write_config; 1357 1358 if (legacy) { 1359 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) 1360 + virtio_bus_get_vdev_config_len(bus); 1361 if (size & (size - 1)) { 1362 size = 1 << qemu_fls(size); 1363 } 1364 1365 memory_region_init_io(&proxy->bar, OBJECT(proxy), 1366 &virtio_pci_config_ops, 1367 proxy, "virtio-pci", size); 1368 1369 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar, 1370 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar); 1371 } 1372 1373 if (!kvm_has_many_ioeventfds()) { 1374 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; 1375 } 1376 1377 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE); 1378 } 1379 1380 static void virtio_pci_device_unplugged(DeviceState *d) 1381 { 1382 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1383 1384 virtio_pci_stop_ioeventfd(proxy); 1385 } 1386 1387 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) 1388 { 1389 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 1390 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); 1391 1392 /* 1393 * virtio pci bar layout used by default. 1394 * subclasses can re-arrange things if needed. 1395 * 1396 * region 0 -- virtio legacy io bar 1397 * region 1 -- msi-x bar 1398 * region 4+5 -- virtio modern memory (64bit) bar 1399 * 1400 */ 1401 proxy->legacy_io_bar = 0; 1402 proxy->msix_bar = 1; 1403 proxy->modern_mem_bar = 4; 1404 1405 proxy->common.offset = 0x0; 1406 proxy->common.size = 0x1000; 1407 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG; 1408 1409 proxy->isr.offset = 0x1000; 1410 proxy->isr.size = 0x1000; 1411 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG; 1412 1413 proxy->device.offset = 0x2000; 1414 proxy->device.size = 0x1000; 1415 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG; 1416 1417 proxy->notify.offset = 0x3000; 1418 proxy->notify.size = 1419 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX; 1420 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG; 1421 1422 /* subclasses can enforce modern, so do this unconditionally */ 1423 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci", 1424 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * 1425 VIRTIO_QUEUE_MAX); 1426 1427 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy); 1428 if (k->realize) { 1429 k->realize(proxy, errp); 1430 } 1431 } 1432 1433 static void virtio_pci_exit(PCIDevice *pci_dev) 1434 { 1435 msix_uninit_exclusive_bar(pci_dev); 1436 } 1437 1438 static void virtio_pci_reset(DeviceState *qdev) 1439 { 1440 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1441 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 1442 virtio_pci_stop_ioeventfd(proxy); 1443 virtio_bus_reset(bus); 1444 msix_unuse_all_vectors(&proxy->pci_dev); 1445 } 1446 1447 static Property virtio_pci_properties[] = { 1448 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags, 1449 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false), 1450 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags, 1451 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false), 1452 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags, 1453 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true), 1454 DEFINE_PROP_END_OF_LIST(), 1455 }; 1456 1457 static void virtio_pci_class_init(ObjectClass *klass, void *data) 1458 { 1459 DeviceClass *dc = DEVICE_CLASS(klass); 1460 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1461 1462 dc->props = virtio_pci_properties; 1463 k->realize = virtio_pci_realize; 1464 k->exit = virtio_pci_exit; 1465 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1466 k->revision = VIRTIO_PCI_ABI_VERSION; 1467 k->class_id = PCI_CLASS_OTHERS; 1468 dc->reset = virtio_pci_reset; 1469 } 1470 1471 static const TypeInfo virtio_pci_info = { 1472 .name = TYPE_VIRTIO_PCI, 1473 .parent = TYPE_PCI_DEVICE, 1474 .instance_size = sizeof(VirtIOPCIProxy), 1475 .class_init = virtio_pci_class_init, 1476 .class_size = sizeof(VirtioPCIClass), 1477 .abstract = true, 1478 }; 1479 1480 /* virtio-blk-pci */ 1481 1482 static Property virtio_blk_pci_properties[] = { 1483 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1484 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1485 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1486 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1487 DEFINE_PROP_END_OF_LIST(), 1488 }; 1489 1490 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1491 { 1492 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); 1493 DeviceState *vdev = DEVICE(&dev->vdev); 1494 1495 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1496 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1497 } 1498 1499 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) 1500 { 1501 DeviceClass *dc = DEVICE_CLASS(klass); 1502 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1503 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1504 1505 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1506 dc->props = virtio_blk_pci_properties; 1507 k->realize = virtio_blk_pci_realize; 1508 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1509 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; 1510 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1511 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1512 } 1513 1514 static void virtio_blk_pci_instance_init(Object *obj) 1515 { 1516 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); 1517 1518 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1519 TYPE_VIRTIO_BLK); 1520 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 1521 &error_abort); 1522 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1523 "bootindex", &error_abort); 1524 } 1525 1526 static const TypeInfo virtio_blk_pci_info = { 1527 .name = TYPE_VIRTIO_BLK_PCI, 1528 .parent = TYPE_VIRTIO_PCI, 1529 .instance_size = sizeof(VirtIOBlkPCI), 1530 .instance_init = virtio_blk_pci_instance_init, 1531 .class_init = virtio_blk_pci_class_init, 1532 }; 1533 1534 /* virtio-scsi-pci */ 1535 1536 static Property virtio_scsi_pci_properties[] = { 1537 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1538 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1539 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1540 DEV_NVECTORS_UNSPECIFIED), 1541 DEFINE_PROP_END_OF_LIST(), 1542 }; 1543 1544 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1545 { 1546 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); 1547 DeviceState *vdev = DEVICE(&dev->vdev); 1548 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1549 DeviceState *proxy = DEVICE(vpci_dev); 1550 char *bus_name; 1551 1552 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1553 vpci_dev->nvectors = vs->conf.num_queues + 3; 1554 } 1555 1556 /* 1557 * For command line compatibility, this sets the virtio-scsi-device bus 1558 * name as before. 1559 */ 1560 if (proxy->id) { 1561 bus_name = g_strdup_printf("%s.0", proxy->id); 1562 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1563 g_free(bus_name); 1564 } 1565 1566 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1567 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1568 } 1569 1570 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) 1571 { 1572 DeviceClass *dc = DEVICE_CLASS(klass); 1573 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1574 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1575 1576 k->realize = virtio_scsi_pci_realize; 1577 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1578 dc->props = virtio_scsi_pci_properties; 1579 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1580 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1581 pcidev_k->revision = 0x00; 1582 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1583 } 1584 1585 static void virtio_scsi_pci_instance_init(Object *obj) 1586 { 1587 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); 1588 1589 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1590 TYPE_VIRTIO_SCSI); 1591 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread", 1592 &error_abort); 1593 } 1594 1595 static const TypeInfo virtio_scsi_pci_info = { 1596 .name = TYPE_VIRTIO_SCSI_PCI, 1597 .parent = TYPE_VIRTIO_PCI, 1598 .instance_size = sizeof(VirtIOSCSIPCI), 1599 .instance_init = virtio_scsi_pci_instance_init, 1600 .class_init = virtio_scsi_pci_class_init, 1601 }; 1602 1603 /* vhost-scsi-pci */ 1604 1605 #ifdef CONFIG_VHOST_SCSI 1606 static Property vhost_scsi_pci_properties[] = { 1607 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1608 DEV_NVECTORS_UNSPECIFIED), 1609 DEFINE_PROP_END_OF_LIST(), 1610 }; 1611 1612 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1613 { 1614 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev); 1615 DeviceState *vdev = DEVICE(&dev->vdev); 1616 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1617 1618 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1619 vpci_dev->nvectors = vs->conf.num_queues + 3; 1620 } 1621 1622 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1623 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1624 } 1625 1626 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data) 1627 { 1628 DeviceClass *dc = DEVICE_CLASS(klass); 1629 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1630 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1631 k->realize = vhost_scsi_pci_realize; 1632 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1633 dc->props = vhost_scsi_pci_properties; 1634 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1635 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1636 pcidev_k->revision = 0x00; 1637 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1638 } 1639 1640 static void vhost_scsi_pci_instance_init(Object *obj) 1641 { 1642 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj); 1643 1644 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1645 TYPE_VHOST_SCSI); 1646 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1647 "bootindex", &error_abort); 1648 } 1649 1650 static const TypeInfo vhost_scsi_pci_info = { 1651 .name = TYPE_VHOST_SCSI_PCI, 1652 .parent = TYPE_VIRTIO_PCI, 1653 .instance_size = sizeof(VHostSCSIPCI), 1654 .instance_init = vhost_scsi_pci_instance_init, 1655 .class_init = vhost_scsi_pci_class_init, 1656 }; 1657 #endif 1658 1659 /* virtio-balloon-pci */ 1660 1661 static Property virtio_balloon_pci_properties[] = { 1662 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1663 DEFINE_PROP_END_OF_LIST(), 1664 }; 1665 1666 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1667 { 1668 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); 1669 DeviceState *vdev = DEVICE(&dev->vdev); 1670 1671 if (vpci_dev->class_code != PCI_CLASS_OTHERS && 1672 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ 1673 vpci_dev->class_code = PCI_CLASS_OTHERS; 1674 } 1675 1676 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1677 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1678 } 1679 1680 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) 1681 { 1682 DeviceClass *dc = DEVICE_CLASS(klass); 1683 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1684 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1685 k->realize = virtio_balloon_pci_realize; 1686 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1687 dc->props = virtio_balloon_pci_properties; 1688 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1689 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; 1690 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1691 pcidev_k->class_id = PCI_CLASS_OTHERS; 1692 } 1693 1694 static void virtio_balloon_pci_instance_init(Object *obj) 1695 { 1696 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); 1697 1698 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1699 TYPE_VIRTIO_BALLOON); 1700 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev), 1701 "guest-stats", &error_abort); 1702 object_property_add_alias(obj, "guest-stats-polling-interval", 1703 OBJECT(&dev->vdev), 1704 "guest-stats-polling-interval", &error_abort); 1705 } 1706 1707 static const TypeInfo virtio_balloon_pci_info = { 1708 .name = TYPE_VIRTIO_BALLOON_PCI, 1709 .parent = TYPE_VIRTIO_PCI, 1710 .instance_size = sizeof(VirtIOBalloonPCI), 1711 .instance_init = virtio_balloon_pci_instance_init, 1712 .class_init = virtio_balloon_pci_class_init, 1713 }; 1714 1715 /* virtio-serial-pci */ 1716 1717 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1718 { 1719 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); 1720 DeviceState *vdev = DEVICE(&dev->vdev); 1721 DeviceState *proxy = DEVICE(vpci_dev); 1722 char *bus_name; 1723 1724 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && 1725 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ 1726 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ 1727 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; 1728 } 1729 1730 /* backwards-compatibility with machines that were created with 1731 DEV_NVECTORS_UNSPECIFIED */ 1732 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1733 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; 1734 } 1735 1736 /* 1737 * For command line compatibility, this sets the virtio-serial-device bus 1738 * name as before. 1739 */ 1740 if (proxy->id) { 1741 bus_name = g_strdup_printf("%s.0", proxy->id); 1742 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1743 g_free(bus_name); 1744 } 1745 1746 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1747 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1748 } 1749 1750 static Property virtio_serial_pci_properties[] = { 1751 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1752 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1753 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1754 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1755 DEFINE_PROP_END_OF_LIST(), 1756 }; 1757 1758 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) 1759 { 1760 DeviceClass *dc = DEVICE_CLASS(klass); 1761 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1762 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1763 k->realize = virtio_serial_pci_realize; 1764 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1765 dc->props = virtio_serial_pci_properties; 1766 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1767 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; 1768 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1769 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 1770 } 1771 1772 static void virtio_serial_pci_instance_init(Object *obj) 1773 { 1774 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); 1775 1776 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1777 TYPE_VIRTIO_SERIAL); 1778 } 1779 1780 static const TypeInfo virtio_serial_pci_info = { 1781 .name = TYPE_VIRTIO_SERIAL_PCI, 1782 .parent = TYPE_VIRTIO_PCI, 1783 .instance_size = sizeof(VirtIOSerialPCI), 1784 .instance_init = virtio_serial_pci_instance_init, 1785 .class_init = virtio_serial_pci_class_init, 1786 }; 1787 1788 /* virtio-net-pci */ 1789 1790 static Property virtio_net_properties[] = { 1791 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1792 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), 1793 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 1794 DEFINE_PROP_END_OF_LIST(), 1795 }; 1796 1797 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1798 { 1799 DeviceState *qdev = DEVICE(vpci_dev); 1800 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); 1801 DeviceState *vdev = DEVICE(&dev->vdev); 1802 1803 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 1804 object_get_typename(OBJECT(qdev))); 1805 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1806 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1807 } 1808 1809 static void virtio_net_pci_class_init(ObjectClass *klass, void *data) 1810 { 1811 DeviceClass *dc = DEVICE_CLASS(klass); 1812 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1813 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1814 1815 k->romfile = "efi-virtio.rom"; 1816 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1817 k->device_id = PCI_DEVICE_ID_VIRTIO_NET; 1818 k->revision = VIRTIO_PCI_ABI_VERSION; 1819 k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1820 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 1821 dc->props = virtio_net_properties; 1822 vpciklass->realize = virtio_net_pci_realize; 1823 } 1824 1825 static void virtio_net_pci_instance_init(Object *obj) 1826 { 1827 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); 1828 1829 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1830 TYPE_VIRTIO_NET); 1831 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 1832 "bootindex", &error_abort); 1833 } 1834 1835 static const TypeInfo virtio_net_pci_info = { 1836 .name = TYPE_VIRTIO_NET_PCI, 1837 .parent = TYPE_VIRTIO_PCI, 1838 .instance_size = sizeof(VirtIONetPCI), 1839 .instance_init = virtio_net_pci_instance_init, 1840 .class_init = virtio_net_pci_class_init, 1841 }; 1842 1843 /* virtio-rng-pci */ 1844 1845 static Property virtio_rng_pci_properties[] = { 1846 DEFINE_PROP_END_OF_LIST(), 1847 }; 1848 1849 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1850 { 1851 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); 1852 DeviceState *vdev = DEVICE(&vrng->vdev); 1853 Error *err = NULL; 1854 1855 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1856 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 1857 if (err) { 1858 error_propagate(errp, err); 1859 return; 1860 } 1861 1862 object_property_set_link(OBJECT(vrng), 1863 OBJECT(vrng->vdev.conf.rng), "rng", 1864 NULL); 1865 } 1866 1867 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) 1868 { 1869 DeviceClass *dc = DEVICE_CLASS(klass); 1870 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1871 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1872 1873 k->realize = virtio_rng_pci_realize; 1874 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1875 dc->props = virtio_rng_pci_properties; 1876 1877 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1878 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; 1879 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1880 pcidev_k->class_id = PCI_CLASS_OTHERS; 1881 } 1882 1883 static void virtio_rng_initfn(Object *obj) 1884 { 1885 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); 1886 1887 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1888 TYPE_VIRTIO_RNG); 1889 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng", 1890 &error_abort); 1891 } 1892 1893 static const TypeInfo virtio_rng_pci_info = { 1894 .name = TYPE_VIRTIO_RNG_PCI, 1895 .parent = TYPE_VIRTIO_PCI, 1896 .instance_size = sizeof(VirtIORngPCI), 1897 .instance_init = virtio_rng_initfn, 1898 .class_init = virtio_rng_pci_class_init, 1899 }; 1900 1901 /* virtio-input-pci */ 1902 1903 static Property virtio_input_hid_pci_properties[] = { 1904 DEFINE_VIRTIO_INPUT_PROPERTIES(VirtIOInputPCI, vdev.input), 1905 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1906 DEFINE_PROP_END_OF_LIST(), 1907 }; 1908 1909 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 1910 { 1911 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev); 1912 DeviceState *vdev = DEVICE(&vinput->vdev); 1913 1914 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1915 /* force virtio-1.0 */ 1916 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN; 1917 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY; 1918 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1919 } 1920 1921 static void virtio_input_pci_class_init(ObjectClass *klass, void *data) 1922 { 1923 DeviceClass *dc = DEVICE_CLASS(klass); 1924 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1925 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1926 1927 k->realize = virtio_input_pci_realize; 1928 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1929 1930 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER; 1931 } 1932 1933 static void virtio_input_hid_pci_class_init(ObjectClass *klass, void *data) 1934 { 1935 DeviceClass *dc = DEVICE_CLASS(klass); 1936 1937 dc->props = virtio_input_hid_pci_properties; 1938 } 1939 1940 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data) 1941 { 1942 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1943 1944 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD; 1945 } 1946 1947 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass, 1948 void *data) 1949 { 1950 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1951 1952 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE; 1953 } 1954 1955 static void virtio_keyboard_initfn(Object *obj) 1956 { 1957 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 1958 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_KEYBOARD); 1959 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1960 } 1961 1962 static void virtio_mouse_initfn(Object *obj) 1963 { 1964 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 1965 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_MOUSE); 1966 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1967 } 1968 1969 static void virtio_tablet_initfn(Object *obj) 1970 { 1971 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj); 1972 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_TABLET); 1973 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1974 } 1975 1976 static const TypeInfo virtio_input_pci_info = { 1977 .name = TYPE_VIRTIO_INPUT_PCI, 1978 .parent = TYPE_VIRTIO_PCI, 1979 .instance_size = sizeof(VirtIOInputPCI), 1980 .class_init = virtio_input_pci_class_init, 1981 .abstract = true, 1982 }; 1983 1984 static const TypeInfo virtio_input_hid_pci_info = { 1985 .name = TYPE_VIRTIO_INPUT_HID_PCI, 1986 .parent = TYPE_VIRTIO_INPUT_PCI, 1987 .instance_size = sizeof(VirtIOInputHIDPCI), 1988 .class_init = virtio_input_hid_pci_class_init, 1989 .abstract = true, 1990 }; 1991 1992 static const TypeInfo virtio_keyboard_pci_info = { 1993 .name = TYPE_VIRTIO_KEYBOARD_PCI, 1994 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 1995 .class_init = virtio_input_hid_kbd_pci_class_init, 1996 .instance_size = sizeof(VirtIOInputHIDPCI), 1997 .instance_init = virtio_keyboard_initfn, 1998 }; 1999 2000 static const TypeInfo virtio_mouse_pci_info = { 2001 .name = TYPE_VIRTIO_MOUSE_PCI, 2002 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2003 .class_init = virtio_input_hid_mouse_pci_class_init, 2004 .instance_size = sizeof(VirtIOInputHIDPCI), 2005 .instance_init = virtio_mouse_initfn, 2006 }; 2007 2008 static const TypeInfo virtio_tablet_pci_info = { 2009 .name = TYPE_VIRTIO_TABLET_PCI, 2010 .parent = TYPE_VIRTIO_INPUT_HID_PCI, 2011 .instance_size = sizeof(VirtIOInputHIDPCI), 2012 .instance_init = virtio_tablet_initfn, 2013 }; 2014 2015 /* virtio-pci-bus */ 2016 2017 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 2018 VirtIOPCIProxy *dev) 2019 { 2020 DeviceState *qdev = DEVICE(dev); 2021 char virtio_bus_name[] = "virtio-bus"; 2022 2023 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 2024 virtio_bus_name); 2025 } 2026 2027 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 2028 { 2029 BusClass *bus_class = BUS_CLASS(klass); 2030 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 2031 bus_class->max_dev = 1; 2032 k->notify = virtio_pci_notify; 2033 k->save_config = virtio_pci_save_config; 2034 k->load_config = virtio_pci_load_config; 2035 k->save_queue = virtio_pci_save_queue; 2036 k->load_queue = virtio_pci_load_queue; 2037 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 2038 k->set_host_notifier = virtio_pci_set_host_notifier; 2039 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 2040 k->vmstate_change = virtio_pci_vmstate_change; 2041 k->device_plugged = virtio_pci_device_plugged; 2042 k->device_unplugged = virtio_pci_device_unplugged; 2043 k->query_nvectors = virtio_pci_query_nvectors; 2044 } 2045 2046 static const TypeInfo virtio_pci_bus_info = { 2047 .name = TYPE_VIRTIO_PCI_BUS, 2048 .parent = TYPE_VIRTIO_BUS, 2049 .instance_size = sizeof(VirtioPCIBusState), 2050 .class_init = virtio_pci_bus_class_init, 2051 }; 2052 2053 static void virtio_pci_register_types(void) 2054 { 2055 type_register_static(&virtio_rng_pci_info); 2056 type_register_static(&virtio_input_pci_info); 2057 type_register_static(&virtio_input_hid_pci_info); 2058 type_register_static(&virtio_keyboard_pci_info); 2059 type_register_static(&virtio_mouse_pci_info); 2060 type_register_static(&virtio_tablet_pci_info); 2061 type_register_static(&virtio_pci_bus_info); 2062 type_register_static(&virtio_pci_info); 2063 #ifdef CONFIG_VIRTFS 2064 type_register_static(&virtio_9p_pci_info); 2065 #endif 2066 type_register_static(&virtio_blk_pci_info); 2067 type_register_static(&virtio_scsi_pci_info); 2068 type_register_static(&virtio_balloon_pci_info); 2069 type_register_static(&virtio_serial_pci_info); 2070 type_register_static(&virtio_net_pci_info); 2071 #ifdef CONFIG_VHOST_SCSI 2072 type_register_static(&vhost_scsi_pci_info); 2073 #endif 2074 } 2075 2076 type_init(virtio_pci_register_types) 2077