1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 4 * Author: Alex Williamson <alex.williamson@redhat.com> 5 * 6 * Derived from original vfio: 7 * Copyright 2010 Cisco Systems, Inc. All rights reserved. 8 * Author: Tom Lyon, pugs@cisco.com 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/aperture.h> 14 #include <linux/device.h> 15 #include <linux/eventfd.h> 16 #include <linux/file.h> 17 #include <linux/interrupt.h> 18 #include <linux/iommu.h> 19 #include <linux/module.h> 20 #include <linux/mutex.h> 21 #include <linux/notifier.h> 22 #include <linux/pci.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/slab.h> 25 #include <linux/types.h> 26 #include <linux/uaccess.h> 27 #include <linux/vgaarb.h> 28 #include <linux/nospec.h> 29 #include <linux/sched/mm.h> 30 #include <linux/iommufd.h> 31 #include <linux/pci-p2pdma.h> 32 #if IS_ENABLED(CONFIG_EEH) 33 #include <asm/eeh.h> 34 #endif 35 36 #include "vfio_pci_priv.h" 37 38 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" 39 #define DRIVER_DESC "core driver for VFIO based PCI devices" 40 41 static bool nointxmask; 42 static bool disable_vga; 43 static bool disable_idle_d3; 44 45 static void vfio_pci_eventfd_rcu_free(struct rcu_head *rcu) 46 { 47 struct vfio_pci_eventfd *eventfd = 48 container_of(rcu, struct vfio_pci_eventfd, rcu); 49 50 eventfd_ctx_put(eventfd->ctx); 51 kfree(eventfd); 52 } 53 54 int vfio_pci_eventfd_replace_locked(struct vfio_pci_core_device *vdev, 55 struct vfio_pci_eventfd __rcu **peventfd, 56 struct eventfd_ctx *ctx) 57 { 58 struct vfio_pci_eventfd *new = NULL; 59 struct vfio_pci_eventfd *old; 60 61 lockdep_assert_held(&vdev->igate); 62 63 if (ctx) { 64 new = kzalloc_obj(*new, GFP_KERNEL_ACCOUNT); 65 if (!new) 66 return -ENOMEM; 67 68 new->ctx = ctx; 69 } 70 71 old = rcu_replace_pointer(*peventfd, new, 72 lockdep_is_held(&vdev->igate)); 73 if (old) 74 call_rcu(&old->rcu, vfio_pci_eventfd_rcu_free); 75 76 return 0; 77 } 78 79 /* List of PF's that vfio_pci_core_sriov_configure() has been called on */ 80 static DEFINE_MUTEX(vfio_pci_sriov_pfs_mutex); 81 static LIST_HEAD(vfio_pci_sriov_pfs); 82 83 struct vfio_pci_dummy_resource { 84 struct resource resource; 85 int index; 86 struct list_head res_next; 87 }; 88 89 struct vfio_pci_vf_token { 90 struct mutex lock; 91 uuid_t uuid; 92 int users; 93 }; 94 95 static inline bool vfio_vga_disabled(void) 96 { 97 #ifdef CONFIG_VFIO_PCI_VGA 98 return disable_vga; 99 #else 100 return true; 101 #endif 102 } 103 104 /* 105 * Our VGA arbiter participation is limited since we don't know anything 106 * about the device itself. However, if the device is the only VGA device 107 * downstream of a bridge and VFIO VGA support is disabled, then we can 108 * safely return legacy VGA IO and memory as not decoded since the user 109 * has no way to get to it and routing can be disabled externally at the 110 * bridge. 111 */ 112 static unsigned int vfio_pci_set_decode(struct pci_dev *pdev, bool single_vga) 113 { 114 struct pci_dev *tmp = NULL; 115 unsigned char max_busnr; 116 unsigned int decodes; 117 118 if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus)) 119 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | 120 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; 121 122 max_busnr = pci_bus_max_busnr(pdev->bus); 123 decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 124 125 while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) { 126 if (tmp == pdev || 127 pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) || 128 pci_is_root_bus(tmp->bus)) 129 continue; 130 131 if (tmp->bus->number >= pdev->bus->number && 132 tmp->bus->number <= max_busnr) { 133 pci_dev_put(tmp); 134 decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; 135 break; 136 } 137 } 138 139 return decodes; 140 } 141 142 static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev) 143 { 144 struct resource *res; 145 int i; 146 struct vfio_pci_dummy_resource *dummy_res; 147 148 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 149 int bar = i + PCI_STD_RESOURCES; 150 151 res = &vdev->pdev->resource[bar]; 152 153 if (vdev->pdev->non_mappable_bars) 154 goto no_mmap; 155 156 if (!(res->flags & IORESOURCE_MEM)) 157 goto no_mmap; 158 159 /* 160 * The PCI core shouldn't set up a resource with a 161 * type but zero size. But there may be bugs that 162 * cause us to do that. 163 */ 164 if (!resource_size(res)) 165 goto no_mmap; 166 167 if (resource_size(res) >= PAGE_SIZE) { 168 vdev->bar_mmap_supported[bar] = true; 169 continue; 170 } 171 172 if (!(res->start & ~PAGE_MASK)) { 173 /* 174 * Add a dummy resource to reserve the remainder 175 * of the exclusive page in case that hot-add 176 * device's bar is assigned into it. 177 */ 178 dummy_res = kzalloc_obj(*dummy_res, GFP_KERNEL_ACCOUNT); 179 if (dummy_res == NULL) 180 goto no_mmap; 181 182 dummy_res->resource.name = "vfio sub-page reserved"; 183 dummy_res->resource.start = res->end + 1; 184 dummy_res->resource.end = res->start + PAGE_SIZE - 1; 185 dummy_res->resource.flags = res->flags; 186 if (request_resource(res->parent, 187 &dummy_res->resource)) { 188 kfree(dummy_res); 189 goto no_mmap; 190 } 191 dummy_res->index = bar; 192 list_add(&dummy_res->res_next, 193 &vdev->dummy_resources_list); 194 vdev->bar_mmap_supported[bar] = true; 195 continue; 196 } 197 /* 198 * Here we don't handle the case when the BAR is not page 199 * aligned because we can't expect the BAR will be 200 * assigned into the same location in a page in guest 201 * when we passthrough the BAR. And it's hard to access 202 * this BAR in userspace because we have no way to get 203 * the BAR's location in a page. 204 */ 205 no_mmap: 206 vdev->bar_mmap_supported[bar] = false; 207 } 208 } 209 210 struct vfio_pci_group_info; 211 static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set); 212 static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set, 213 struct vfio_pci_group_info *groups, 214 struct iommufd_ctx *iommufd_ctx); 215 216 /* 217 * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND 218 * _and_ the ability detect when the device is asserting INTx via PCI_STATUS. 219 * If a device implements the former but not the latter we would typically 220 * expect broken_intx_masking be set and require an exclusive interrupt. 221 * However since we do have control of the device's ability to assert INTx, 222 * we can instead pretend that the device does not implement INTx, virtualizing 223 * the pin register to report zero and maintaining DisINTx set on the host. 224 */ 225 static bool vfio_pci_nointx(struct pci_dev *pdev) 226 { 227 switch (pdev->vendor) { 228 case PCI_VENDOR_ID_INTEL: 229 switch (pdev->device) { 230 /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */ 231 case 0x1572: 232 case 0x1574: 233 case 0x1580 ... 0x1581: 234 case 0x1583 ... 0x158b: 235 case 0x37d0 ... 0x37d2: 236 /* X550 */ 237 case 0x1563: 238 return true; 239 default: 240 return false; 241 } 242 } 243 244 return false; 245 } 246 247 static void vfio_pci_probe_power_state(struct vfio_pci_core_device *vdev) 248 { 249 struct pci_dev *pdev = vdev->pdev; 250 u16 pmcsr; 251 252 if (!pdev->pm_cap) 253 return; 254 255 pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &pmcsr); 256 257 vdev->needs_pm_restore = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET); 258 } 259 260 /* 261 * pci_set_power_state() wrapper handling devices which perform a soft reset on 262 * D3->D0 transition. Save state prior to D0/1/2->D3, stash it on the vdev, 263 * restore when returned to D0. Saved separately from pci_saved_state for use 264 * by PM capability emulation and separately from pci_dev internal saved state 265 * to avoid it being overwritten and consumed around other resets. 266 */ 267 int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state) 268 { 269 struct pci_dev *pdev = vdev->pdev; 270 bool needs_restore = false, needs_save = false; 271 int ret; 272 273 /* Prevent changing power state for PFs with VFs enabled */ 274 if (state > PCI_D0) { 275 lockdep_assert_held_write(&vdev->memory_lock); 276 if (vdev->sriov_active) 277 return -EBUSY; 278 } 279 280 if (vdev->needs_pm_restore) { 281 if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) { 282 pci_save_state(pdev); 283 needs_save = true; 284 } 285 286 if (pdev->current_state >= PCI_D3hot && state <= PCI_D0) 287 needs_restore = true; 288 } 289 290 ret = pci_set_power_state(pdev, state); 291 292 if (!ret) { 293 /* D3 might be unsupported via quirk, skip unless in D3 */ 294 if (needs_save && pdev->current_state >= PCI_D3hot) { 295 /* 296 * The current PCI state will be saved locally in 297 * 'pm_save' during the D3hot transition. When the 298 * device state is changed to D0 again with the current 299 * function, then pci_store_saved_state() will restore 300 * the state and will free the memory pointed by 301 * 'pm_save'. There are few cases where the PCI power 302 * state can be changed to D0 without the involvement 303 * of the driver. For these cases, free the earlier 304 * allocated memory first before overwriting 'pm_save' 305 * to prevent the memory leak. 306 */ 307 kfree(vdev->pm_save); 308 vdev->pm_save = pci_store_saved_state(pdev); 309 } else if (needs_restore) { 310 pci_load_and_free_saved_state(pdev, &vdev->pm_save); 311 pci_restore_state(pdev); 312 } 313 } 314 315 return ret; 316 } 317 318 static int vfio_pci_runtime_pm_entry(struct vfio_pci_core_device *vdev, 319 struct eventfd_ctx *efdctx) 320 { 321 /* 322 * The vdev power related flags are protected with 'memory_lock' 323 * semaphore. 324 */ 325 vfio_pci_zap_and_down_write_memory_lock(vdev); 326 vfio_pci_dma_buf_move(vdev, true); 327 328 if (vdev->pm_runtime_engaged) { 329 up_write(&vdev->memory_lock); 330 return -EINVAL; 331 } 332 333 vdev->pm_runtime_engaged = true; 334 vdev->pm_wake_eventfd_ctx = efdctx; 335 pm_runtime_put_noidle(&vdev->pdev->dev); 336 up_write(&vdev->memory_lock); 337 338 return 0; 339 } 340 341 static int vfio_pci_core_pm_entry(struct vfio_pci_core_device *vdev, u32 flags, 342 void __user *arg, size_t argsz) 343 { 344 int ret; 345 346 ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, 0); 347 if (ret != 1) 348 return ret; 349 350 /* 351 * Inside vfio_pci_runtime_pm_entry(), only the runtime PM usage count 352 * will be decremented. The pm_runtime_put() will be invoked again 353 * while returning from the ioctl and then the device can go into 354 * runtime suspended state. 355 */ 356 return vfio_pci_runtime_pm_entry(vdev, NULL); 357 } 358 359 static int vfio_pci_core_pm_entry_with_wakeup( 360 struct vfio_pci_core_device *vdev, u32 flags, 361 struct vfio_device_low_power_entry_with_wakeup __user *arg, 362 size_t argsz) 363 { 364 struct vfio_device_low_power_entry_with_wakeup entry; 365 struct eventfd_ctx *efdctx; 366 int ret; 367 368 ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, 369 sizeof(entry)); 370 if (ret != 1) 371 return ret; 372 373 if (copy_from_user(&entry, arg, sizeof(entry))) 374 return -EFAULT; 375 376 if (entry.wakeup_eventfd < 0) 377 return -EINVAL; 378 379 efdctx = eventfd_ctx_fdget(entry.wakeup_eventfd); 380 if (IS_ERR(efdctx)) 381 return PTR_ERR(efdctx); 382 383 ret = vfio_pci_runtime_pm_entry(vdev, efdctx); 384 if (ret) 385 eventfd_ctx_put(efdctx); 386 387 return ret; 388 } 389 390 static void __vfio_pci_runtime_pm_exit(struct vfio_pci_core_device *vdev) 391 { 392 if (vdev->pm_runtime_engaged) { 393 vdev->pm_runtime_engaged = false; 394 pm_runtime_get_noresume(&vdev->pdev->dev); 395 396 if (vdev->pm_wake_eventfd_ctx) { 397 eventfd_ctx_put(vdev->pm_wake_eventfd_ctx); 398 vdev->pm_wake_eventfd_ctx = NULL; 399 } 400 } 401 } 402 403 static void vfio_pci_runtime_pm_exit(struct vfio_pci_core_device *vdev) 404 { 405 /* 406 * The vdev power related flags are protected with 'memory_lock' 407 * semaphore. 408 */ 409 down_write(&vdev->memory_lock); 410 __vfio_pci_runtime_pm_exit(vdev); 411 if (__vfio_pci_memory_enabled(vdev)) 412 vfio_pci_dma_buf_move(vdev, false); 413 up_write(&vdev->memory_lock); 414 } 415 416 static int vfio_pci_core_pm_exit(struct vfio_pci_core_device *vdev, u32 flags, 417 void __user *arg, size_t argsz) 418 { 419 int ret; 420 421 ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, 0); 422 if (ret != 1) 423 return ret; 424 425 /* 426 * The device is always in the active state here due to pm wrappers 427 * around ioctls. If the device had entered a low power state and 428 * pm_wake_eventfd_ctx is valid, vfio_pci_core_runtime_resume() has 429 * already signaled the eventfd and exited low power mode itself. 430 * pm_runtime_engaged protects the redundant call here. 431 */ 432 vfio_pci_runtime_pm_exit(vdev); 433 return 0; 434 } 435 436 #ifdef CONFIG_PM 437 static int vfio_pci_core_runtime_suspend(struct device *dev) 438 { 439 struct vfio_pci_core_device *vdev = dev_get_drvdata(dev); 440 441 down_write(&vdev->memory_lock); 442 /* 443 * The user can move the device into D3hot state before invoking 444 * power management IOCTL. Move the device into D0 state here and then 445 * the pci-driver core runtime PM suspend function will move the device 446 * into the low power state. Also, for the devices which have 447 * NoSoftRst-, it will help in restoring the original state 448 * (saved locally in 'vdev->pm_save'). 449 */ 450 vfio_pci_set_power_state(vdev, PCI_D0); 451 up_write(&vdev->memory_lock); 452 453 /* 454 * If INTx is enabled, then mask INTx before going into the runtime 455 * suspended state and unmask the same in the runtime resume. 456 * If INTx has already been masked by the user, then 457 * vfio_pci_intx_mask() will return false and in that case, INTx 458 * should not be unmasked in the runtime resume. 459 */ 460 vdev->pm_intx_masked = ((vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX) && 461 vfio_pci_intx_mask(vdev)); 462 463 return 0; 464 } 465 466 static int vfio_pci_core_runtime_resume(struct device *dev) 467 { 468 struct vfio_pci_core_device *vdev = dev_get_drvdata(dev); 469 470 /* 471 * Resume with a pm_wake_eventfd_ctx signals the eventfd and exit 472 * low power mode. 473 */ 474 down_write(&vdev->memory_lock); 475 if (vdev->pm_wake_eventfd_ctx) { 476 eventfd_signal(vdev->pm_wake_eventfd_ctx); 477 __vfio_pci_runtime_pm_exit(vdev); 478 } 479 up_write(&vdev->memory_lock); 480 481 if (vdev->pm_intx_masked) 482 vfio_pci_intx_unmask(vdev); 483 484 return 0; 485 } 486 #endif /* CONFIG_PM */ 487 488 /* 489 * Eager-request BAR resources, and iomap them. Soft failures are 490 * allowed, and consumers must check the barmap before use in order to 491 * give compatible user-visible behaviour with the previous on-demand 492 * allocation method. 493 */ 494 static void vfio_pci_core_map_bars(struct vfio_pci_core_device *vdev) 495 { 496 struct pci_dev *pdev = vdev->pdev; 497 int i; 498 499 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 500 int bar = i + PCI_STD_RESOURCES; 501 502 vdev->barmap[bar] = IOMEM_ERR_PTR(-ENODEV); 503 504 if (!pci_resource_len(pdev, i)) 505 continue; 506 507 if (pci_request_selected_regions(pdev, 1 << bar, "vfio")) { 508 pci_dbg(pdev, "Failed to reserve region %d\n", bar); 509 vdev->barmap[bar] = IOMEM_ERR_PTR(-EBUSY); 510 continue; 511 } 512 513 vdev->barmap[bar] = pci_iomap(pdev, bar, 0); 514 if (!vdev->barmap[bar]) { 515 pci_dbg(pdev, "Failed to iomap region %d\n", bar); 516 pci_release_selected_regions(pdev, 1 << bar); 517 vdev->barmap[bar] = IOMEM_ERR_PTR(-ENOMEM); 518 } 519 } 520 } 521 522 /* 523 * The pci-driver core runtime PM routines always save the device state 524 * before going into suspended state. If the device is going into low power 525 * state with only with runtime PM ops, then no explicit handling is needed 526 * for the devices which have NoSoftRst-. 527 */ 528 static const struct dev_pm_ops vfio_pci_core_pm_ops = { 529 SET_RUNTIME_PM_OPS(vfio_pci_core_runtime_suspend, 530 vfio_pci_core_runtime_resume, 531 NULL) 532 }; 533 534 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) 535 { 536 struct pci_dev *pdev = vdev->pdev; 537 int ret; 538 u16 cmd; 539 u8 msix_pos; 540 541 if (!disable_idle_d3) { 542 ret = pm_runtime_resume_and_get(&pdev->dev); 543 if (ret < 0) 544 return ret; 545 } 546 547 /* Don't allow our initial saved state to include busmaster */ 548 pci_clear_master(pdev); 549 550 ret = pci_enable_device(pdev); 551 if (ret) 552 goto out_power; 553 554 /* If reset fails because of the device lock, fail this path entirely */ 555 ret = pci_try_reset_function(pdev); 556 if (ret == -EAGAIN) 557 goto out_disable_device; 558 559 vdev->reset_works = !ret; 560 pci_save_state(pdev); 561 vdev->pci_saved_state = pci_store_saved_state(pdev); 562 if (!vdev->pci_saved_state) 563 pci_dbg(pdev, "%s: Couldn't store saved state\n", __func__); 564 565 if (likely(!nointxmask)) { 566 if (vfio_pci_nointx(pdev)) { 567 pci_info(pdev, "Masking broken INTx support\n"); 568 vdev->nointx = true; 569 pci_intx(pdev, 0); 570 } else 571 vdev->pci_2_3 = pci_intx_mask_supported(pdev); 572 } 573 574 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 575 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) { 576 cmd &= ~PCI_COMMAND_INTX_DISABLE; 577 pci_write_config_word(pdev, PCI_COMMAND, cmd); 578 } 579 580 ret = vfio_pci_zdev_open_device(vdev); 581 if (ret) 582 goto out_free_state; 583 584 ret = vfio_config_init(vdev); 585 if (ret) 586 goto out_free_zdev; 587 588 msix_pos = pdev->msix_cap; 589 if (msix_pos) { 590 u16 flags; 591 u32 table; 592 593 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags); 594 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table); 595 596 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR; 597 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET; 598 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16; 599 vdev->has_dyn_msix = pci_msix_can_alloc_dyn(pdev); 600 } else { 601 vdev->msix_bar = 0xFF; 602 vdev->has_dyn_msix = false; 603 } 604 605 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) 606 vdev->has_vga = true; 607 608 vfio_pci_core_map_bars(vdev); 609 610 return 0; 611 612 out_free_zdev: 613 vfio_pci_zdev_close_device(vdev); 614 out_free_state: 615 kfree(vdev->pci_saved_state); 616 vdev->pci_saved_state = NULL; 617 out_disable_device: 618 pci_disable_device(pdev); 619 out_power: 620 if (!disable_idle_d3) 621 pm_runtime_put(&pdev->dev); 622 return ret; 623 } 624 EXPORT_SYMBOL_GPL(vfio_pci_core_enable); 625 626 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) 627 { 628 struct pci_dev *bridge; 629 struct pci_dev *pdev = vdev->pdev; 630 struct vfio_pci_dummy_resource *dummy_res, *tmp; 631 struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; 632 int i, bar; 633 634 /* For needs_reset */ 635 lockdep_assert_held(&vdev->vdev.dev_set->lock); 636 637 /* 638 * This function can be invoked while the power state is non-D0. 639 * This non-D0 power state can be with or without runtime PM. 640 * vfio_pci_runtime_pm_exit() will internally increment the usage 641 * count corresponding to pm_runtime_put() called during low power 642 * feature entry and then pm_runtime_resume() will wake up the device, 643 * if the device has already gone into the suspended state. Otherwise, 644 * the vfio_pci_set_power_state() will change the device power state 645 * to D0. 646 */ 647 vfio_pci_runtime_pm_exit(vdev); 648 pm_runtime_resume(&pdev->dev); 649 650 /* 651 * This function calls __pci_reset_function_locked() which internally 652 * can use pci_pm_reset() for the function reset. pci_pm_reset() will 653 * fail if the power state is non-D0. Also, for the devices which 654 * have NoSoftRst-, the reset function can cause the PCI config space 655 * reset without restoring the original state (saved locally in 656 * 'vdev->pm_save'). 657 */ 658 vfio_pci_set_power_state(vdev, PCI_D0); 659 660 /* Stop the device from further DMA */ 661 pci_clear_master(pdev); 662 663 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE | 664 VFIO_IRQ_SET_ACTION_TRIGGER, 665 vdev->irq_type, 0, 0, NULL); 666 667 /* Device closed, don't need mutex here */ 668 list_for_each_entry_safe(ioeventfd, ioeventfd_tmp, 669 &vdev->ioeventfds_list, next) { 670 vfio_virqfd_disable(&ioeventfd->virqfd); 671 list_del(&ioeventfd->next); 672 kfree(ioeventfd); 673 } 674 vdev->ioeventfds_nr = 0; 675 676 vdev->virq_disabled = false; 677 678 for (i = 0; i < vdev->num_regions; i++) 679 vdev->region[i].ops->release(vdev, &vdev->region[i]); 680 681 vdev->num_regions = 0; 682 kfree(vdev->region); 683 vdev->region = NULL; /* don't krealloc a freed pointer */ 684 685 vfio_config_free(vdev); 686 687 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 688 bar = i + PCI_STD_RESOURCES; 689 if (IS_ERR_OR_NULL(vdev->barmap[bar])) 690 continue; 691 pci_iounmap(pdev, vdev->barmap[bar]); 692 pci_release_selected_regions(pdev, 1 << bar); 693 vdev->barmap[bar] = NULL; 694 } 695 696 list_for_each_entry_safe(dummy_res, tmp, 697 &vdev->dummy_resources_list, res_next) { 698 list_del(&dummy_res->res_next); 699 release_resource(&dummy_res->resource); 700 kfree(dummy_res); 701 } 702 703 vdev->needs_reset = true; 704 705 vfio_pci_zdev_close_device(vdev); 706 707 /* 708 * If we have saved state, restore it. If we can reset the device, 709 * even better. Resetting with current state seems better than 710 * nothing, but saving and restoring current state without reset 711 * is just busy work. 712 */ 713 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { 714 pci_info(pdev, "%s: Couldn't reload saved state\n", __func__); 715 716 if (!vdev->reset_works) 717 goto out; 718 719 pci_save_state(pdev); 720 } 721 722 /* 723 * Disable INTx and MSI, presumably to avoid spurious interrupts 724 * during reset. Stolen from pci_reset_function() 725 */ 726 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); 727 728 /* 729 * Try to get the locks ourselves to prevent a deadlock. The 730 * success of this is dependent on being able to lock the device, 731 * which is not always possible. 732 * We can not use the "try" reset interface here, which will 733 * overwrite the previously restored configuration information. 734 */ 735 if (vdev->reset_works) { 736 bridge = pci_upstream_bridge(pdev); 737 if (bridge && !pci_dev_trylock(bridge)) 738 goto out_restore_state; 739 if (pci_dev_trylock(pdev)) { 740 if (!__pci_reset_function_locked(pdev)) 741 vdev->needs_reset = false; 742 pci_dev_unlock(pdev); 743 } 744 if (bridge) 745 pci_dev_unlock(bridge); 746 } 747 748 out_restore_state: 749 pci_restore_state(pdev); 750 out: 751 pci_disable_device(pdev); 752 753 vfio_pci_dev_set_try_reset(vdev->vdev.dev_set); 754 755 /* Put the pm-runtime usage counter acquired during enable */ 756 if (!disable_idle_d3) 757 pm_runtime_put(&pdev->dev); 758 } 759 EXPORT_SYMBOL_GPL(vfio_pci_core_disable); 760 761 void vfio_pci_core_close_device(struct vfio_device *core_vdev) 762 { 763 struct vfio_pci_core_device *vdev = 764 container_of(core_vdev, struct vfio_pci_core_device, vdev); 765 766 if (vdev->sriov_pf_core_dev) { 767 mutex_lock(&vdev->sriov_pf_core_dev->vf_token->lock); 768 WARN_ON(!vdev->sriov_pf_core_dev->vf_token->users); 769 vdev->sriov_pf_core_dev->vf_token->users--; 770 mutex_unlock(&vdev->sriov_pf_core_dev->vf_token->lock); 771 } 772 #if IS_ENABLED(CONFIG_EEH) 773 eeh_dev_release(vdev->pdev); 774 #endif 775 vfio_pci_dma_buf_cleanup(vdev); 776 777 vfio_pci_core_disable(vdev); 778 779 mutex_lock(&vdev->igate); 780 vfio_pci_eventfd_replace_locked(vdev, &vdev->err_trigger, NULL); 781 vfio_pci_eventfd_replace_locked(vdev, &vdev->req_trigger, NULL); 782 mutex_unlock(&vdev->igate); 783 } 784 EXPORT_SYMBOL_GPL(vfio_pci_core_close_device); 785 786 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev) 787 { 788 vfio_pci_probe_mmaps(vdev); 789 #if IS_ENABLED(CONFIG_EEH) 790 eeh_dev_open(vdev->pdev); 791 #endif 792 793 if (vdev->sriov_pf_core_dev) { 794 mutex_lock(&vdev->sriov_pf_core_dev->vf_token->lock); 795 vdev->sriov_pf_core_dev->vf_token->users++; 796 mutex_unlock(&vdev->sriov_pf_core_dev->vf_token->lock); 797 } 798 } 799 EXPORT_SYMBOL_GPL(vfio_pci_core_finish_enable); 800 801 static int vfio_pci_get_irq_count(struct vfio_pci_core_device *vdev, int irq_type) 802 { 803 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) { 804 return vdev->vconfig[PCI_INTERRUPT_PIN] ? 1 : 0; 805 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) { 806 u8 pos; 807 u16 flags; 808 809 pos = vdev->pdev->msi_cap; 810 if (pos) { 811 pci_read_config_word(vdev->pdev, 812 pos + PCI_MSI_FLAGS, &flags); 813 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1); 814 } 815 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) { 816 u8 pos; 817 u16 flags; 818 819 pos = vdev->pdev->msix_cap; 820 if (pos) { 821 pci_read_config_word(vdev->pdev, 822 pos + PCI_MSIX_FLAGS, &flags); 823 824 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1; 825 } 826 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) { 827 if (pci_is_pcie(vdev->pdev)) 828 return 1; 829 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) { 830 return 1; 831 } 832 833 return 0; 834 } 835 836 static int vfio_pci_count_devs(struct pci_dev *pdev, void *data) 837 { 838 (*(int *)data)++; 839 return 0; 840 } 841 842 struct vfio_pci_fill_info { 843 struct vfio_device *vdev; 844 struct vfio_pci_dependent_device *devices; 845 int nr_devices; 846 u32 count; 847 u32 flags; 848 }; 849 850 static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data) 851 { 852 struct vfio_pci_dependent_device *info; 853 struct vfio_pci_fill_info *fill = data; 854 855 /* The topology changed since we counted devices */ 856 if (fill->count >= fill->nr_devices) 857 return -EAGAIN; 858 859 info = &fill->devices[fill->count++]; 860 info->segment = pci_domain_nr(pdev->bus); 861 info->bus = pdev->bus->number; 862 info->devfn = pdev->devfn; 863 864 if (fill->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID) { 865 struct iommufd_ctx *iommufd = vfio_iommufd_device_ictx(fill->vdev); 866 struct vfio_device_set *dev_set = fill->vdev->dev_set; 867 struct vfio_device *vdev; 868 869 /* 870 * hot-reset requires all affected devices be represented in 871 * the dev_set. 872 */ 873 vdev = vfio_find_device_in_devset(dev_set, &pdev->dev); 874 if (!vdev) { 875 info->devid = VFIO_PCI_DEVID_NOT_OWNED; 876 } else { 877 int id = vfio_iommufd_get_dev_id(vdev, iommufd); 878 879 if (id > 0) 880 info->devid = id; 881 else if (id == -ENOENT) 882 info->devid = VFIO_PCI_DEVID_OWNED; 883 else 884 info->devid = VFIO_PCI_DEVID_NOT_OWNED; 885 } 886 /* If devid is VFIO_PCI_DEVID_NOT_OWNED, clear owned flag. */ 887 if (info->devid == VFIO_PCI_DEVID_NOT_OWNED) 888 fill->flags &= ~VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED; 889 } else { 890 struct iommu_group *iommu_group; 891 892 iommu_group = iommu_group_get(&pdev->dev); 893 if (!iommu_group) 894 return -EPERM; /* Cannot reset non-isolated devices */ 895 896 info->group_id = iommu_group_id(iommu_group); 897 iommu_group_put(iommu_group); 898 } 899 900 return 0; 901 } 902 903 struct vfio_pci_group_info { 904 int count; 905 struct file **files; 906 }; 907 908 static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot) 909 { 910 for (; pdev; pdev = pdev->bus->self) 911 if (pdev->bus == slot->bus) 912 return (pdev->slot == slot); 913 return false; 914 } 915 916 struct vfio_pci_walk_info { 917 int (*fn)(struct pci_dev *pdev, void *data); 918 void *data; 919 struct pci_dev *pdev; 920 bool slot; 921 int ret; 922 }; 923 924 static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data) 925 { 926 struct vfio_pci_walk_info *walk = data; 927 928 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot)) 929 walk->ret = walk->fn(pdev, walk->data); 930 931 return walk->ret; 932 } 933 934 static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev, 935 int (*fn)(struct pci_dev *, 936 void *data), void *data, 937 bool slot) 938 { 939 struct vfio_pci_walk_info walk = { 940 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0, 941 }; 942 943 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk); 944 945 return walk.ret; 946 } 947 948 static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, 949 struct vfio_info_cap *caps) 950 { 951 struct vfio_info_cap_header header = { 952 .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE, 953 .version = 1 954 }; 955 956 return vfio_info_add_capability(caps, &header, sizeof(header)); 957 } 958 959 int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, 960 unsigned int type, unsigned int subtype, 961 const struct vfio_pci_regops *ops, 962 size_t size, u32 flags, void *data) 963 { 964 struct vfio_pci_region *region; 965 966 region = krealloc(vdev->region, 967 (vdev->num_regions + 1) * sizeof(*region), 968 GFP_KERNEL_ACCOUNT); 969 if (!region) 970 return -ENOMEM; 971 972 vdev->region = region; 973 vdev->region[vdev->num_regions].type = type; 974 vdev->region[vdev->num_regions].subtype = subtype; 975 vdev->region[vdev->num_regions].ops = ops; 976 vdev->region[vdev->num_regions].size = size; 977 vdev->region[vdev->num_regions].flags = flags; 978 vdev->region[vdev->num_regions].data = data; 979 980 vdev->num_regions++; 981 982 return 0; 983 } 984 EXPORT_SYMBOL_GPL(vfio_pci_core_register_dev_region); 985 986 static int vfio_pci_info_atomic_cap(struct vfio_pci_core_device *vdev, 987 struct vfio_info_cap *caps) 988 { 989 struct vfio_device_info_cap_pci_atomic_comp cap = { 990 .header.id = VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP, 991 .header.version = 1 992 }; 993 struct pci_dev *pdev = pci_physfn(vdev->pdev); 994 u32 devcap2; 995 996 pcie_capability_read_dword(pdev, PCI_EXP_DEVCAP2, &devcap2); 997 998 if ((devcap2 & PCI_EXP_DEVCAP2_ATOMIC_COMP32) && 999 !pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP32)) 1000 cap.flags |= VFIO_PCI_ATOMIC_COMP32; 1001 1002 if ((devcap2 & PCI_EXP_DEVCAP2_ATOMIC_COMP64) && 1003 !pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP64)) 1004 cap.flags |= VFIO_PCI_ATOMIC_COMP64; 1005 1006 if ((devcap2 & PCI_EXP_DEVCAP2_ATOMIC_COMP128) && 1007 !pci_enable_atomic_ops_to_root(pdev, 1008 PCI_EXP_DEVCAP2_ATOMIC_COMP128)) 1009 cap.flags |= VFIO_PCI_ATOMIC_COMP128; 1010 1011 if (!cap.flags) 1012 return -ENODEV; 1013 1014 return vfio_info_add_capability(caps, &cap.header, sizeof(cap)); 1015 } 1016 1017 static int vfio_pci_ioctl_get_info(struct vfio_pci_core_device *vdev, 1018 struct vfio_device_info __user *arg) 1019 { 1020 unsigned long minsz = offsetofend(struct vfio_device_info, num_irqs); 1021 struct vfio_device_info info = {}; 1022 struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; 1023 int ret; 1024 1025 if (copy_from_user(&info, arg, minsz)) 1026 return -EFAULT; 1027 1028 if (info.argsz < minsz) 1029 return -EINVAL; 1030 1031 minsz = min_t(size_t, info.argsz, sizeof(info)); 1032 1033 info.flags = VFIO_DEVICE_FLAGS_PCI; 1034 1035 if (vdev->reset_works) 1036 info.flags |= VFIO_DEVICE_FLAGS_RESET; 1037 1038 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions; 1039 info.num_irqs = VFIO_PCI_NUM_IRQS; 1040 1041 ret = vfio_pci_info_zdev_add_caps(vdev, &caps); 1042 if (ret && ret != -ENODEV) { 1043 pci_warn(vdev->pdev, 1044 "Failed to setup zPCI info capabilities\n"); 1045 return ret; 1046 } 1047 1048 ret = vfio_pci_info_atomic_cap(vdev, &caps); 1049 if (ret && ret != -ENODEV) { 1050 pci_warn(vdev->pdev, 1051 "Failed to setup AtomicOps info capability\n"); 1052 return ret; 1053 } 1054 1055 if (caps.size) { 1056 info.flags |= VFIO_DEVICE_FLAGS_CAPS; 1057 if (info.argsz < sizeof(info) + caps.size) { 1058 info.argsz = sizeof(info) + caps.size; 1059 } else { 1060 vfio_info_cap_shift(&caps, sizeof(info)); 1061 if (copy_to_user(arg + 1, caps.buf, caps.size)) { 1062 kfree(caps.buf); 1063 return -EFAULT; 1064 } 1065 info.cap_offset = sizeof(*arg); 1066 } 1067 1068 kfree(caps.buf); 1069 } 1070 1071 return copy_to_user(arg, &info, minsz) ? -EFAULT : 0; 1072 } 1073 1074 int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev, 1075 struct vfio_region_info *info, 1076 struct vfio_info_cap *caps) 1077 { 1078 struct vfio_pci_core_device *vdev = 1079 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1080 struct pci_dev *pdev = vdev->pdev; 1081 int i, ret; 1082 1083 switch (info->index) { 1084 case VFIO_PCI_CONFIG_REGION_INDEX: 1085 info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index); 1086 info->size = pdev->cfg_size; 1087 info->flags = VFIO_REGION_INFO_FLAG_READ | 1088 VFIO_REGION_INFO_FLAG_WRITE; 1089 break; 1090 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: 1091 info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index); 1092 info->size = pci_resource_len(pdev, info->index); 1093 if (!info->size) { 1094 info->flags = 0; 1095 break; 1096 } 1097 1098 info->flags = VFIO_REGION_INFO_FLAG_READ | 1099 VFIO_REGION_INFO_FLAG_WRITE; 1100 if (vdev->bar_mmap_supported[info->index]) { 1101 info->flags |= VFIO_REGION_INFO_FLAG_MMAP; 1102 if (info->index == vdev->msix_bar) { 1103 ret = msix_mmappable_cap(vdev, caps); 1104 if (ret) 1105 return ret; 1106 } 1107 } 1108 1109 break; 1110 case VFIO_PCI_ROM_REGION_INDEX: { 1111 void __iomem *io; 1112 size_t size; 1113 u16 cmd; 1114 1115 info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index); 1116 info->flags = 0; 1117 info->size = 0; 1118 1119 if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) { 1120 /* 1121 * Check ROM content is valid. Need to enable memory 1122 * decode for ROM access in pci_map_rom(). 1123 */ 1124 cmd = vfio_pci_memory_lock_and_enable(vdev); 1125 io = pci_map_rom(pdev, &size); 1126 if (io) { 1127 info->flags = VFIO_REGION_INFO_FLAG_READ; 1128 /* Report the BAR size, not the ROM size. */ 1129 info->size = pci_resource_len(pdev, 1130 PCI_ROM_RESOURCE); 1131 pci_unmap_rom(pdev, io); 1132 } 1133 vfio_pci_memory_unlock_and_restore(vdev, cmd); 1134 } else if (pdev->rom && pdev->romlen) { 1135 info->flags = VFIO_REGION_INFO_FLAG_READ; 1136 /* Report BAR size as power of two. */ 1137 info->size = roundup_pow_of_two(pdev->romlen); 1138 } 1139 1140 break; 1141 } 1142 case VFIO_PCI_VGA_REGION_INDEX: 1143 if (!vdev->has_vga) 1144 return -EINVAL; 1145 1146 info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index); 1147 info->size = 0xc0000; 1148 info->flags = VFIO_REGION_INFO_FLAG_READ | 1149 VFIO_REGION_INFO_FLAG_WRITE; 1150 1151 break; 1152 default: { 1153 struct vfio_region_info_cap_type cap_type = { 1154 .header.id = VFIO_REGION_INFO_CAP_TYPE, 1155 .header.version = 1 1156 }; 1157 1158 if (info->index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) 1159 return -EINVAL; 1160 info->index = array_index_nospec( 1161 info->index, VFIO_PCI_NUM_REGIONS + vdev->num_regions); 1162 1163 i = info->index - VFIO_PCI_NUM_REGIONS; 1164 1165 info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index); 1166 info->size = vdev->region[i].size; 1167 info->flags = vdev->region[i].flags; 1168 1169 cap_type.type = vdev->region[i].type; 1170 cap_type.subtype = vdev->region[i].subtype; 1171 1172 ret = vfio_info_add_capability(caps, &cap_type.header, 1173 sizeof(cap_type)); 1174 if (ret) 1175 return ret; 1176 1177 if (vdev->region[i].ops->add_capability) { 1178 ret = vdev->region[i].ops->add_capability( 1179 vdev, &vdev->region[i], caps); 1180 if (ret) 1181 return ret; 1182 } 1183 } 1184 } 1185 return 0; 1186 } 1187 EXPORT_SYMBOL_GPL(vfio_pci_ioctl_get_region_info); 1188 1189 static int vfio_pci_ioctl_get_irq_info(struct vfio_pci_core_device *vdev, 1190 struct vfio_irq_info __user *arg) 1191 { 1192 unsigned long minsz = offsetofend(struct vfio_irq_info, count); 1193 struct vfio_irq_info info; 1194 1195 if (copy_from_user(&info, arg, minsz)) 1196 return -EFAULT; 1197 1198 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS) 1199 return -EINVAL; 1200 1201 switch (info.index) { 1202 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX: 1203 case VFIO_PCI_REQ_IRQ_INDEX: 1204 break; 1205 case VFIO_PCI_ERR_IRQ_INDEX: 1206 if (pci_is_pcie(vdev->pdev)) 1207 break; 1208 fallthrough; 1209 default: 1210 return -EINVAL; 1211 } 1212 1213 info.flags = VFIO_IRQ_INFO_EVENTFD; 1214 1215 info.count = vfio_pci_get_irq_count(vdev, info.index); 1216 1217 if (info.index == VFIO_PCI_INTX_IRQ_INDEX) 1218 info.flags |= 1219 (VFIO_IRQ_INFO_MASKABLE | VFIO_IRQ_INFO_AUTOMASKED); 1220 else if (info.index != VFIO_PCI_MSIX_IRQ_INDEX || !vdev->has_dyn_msix) 1221 info.flags |= VFIO_IRQ_INFO_NORESIZE; 1222 1223 return copy_to_user(arg, &info, minsz) ? -EFAULT : 0; 1224 } 1225 1226 static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, 1227 struct vfio_irq_set __user *arg) 1228 { 1229 unsigned long minsz = offsetofend(struct vfio_irq_set, count); 1230 struct vfio_irq_set hdr; 1231 u8 *data = NULL; 1232 int max, ret = 0; 1233 size_t data_size = 0; 1234 1235 if (copy_from_user(&hdr, arg, minsz)) 1236 return -EFAULT; 1237 1238 max = vfio_pci_get_irq_count(vdev, hdr.index); 1239 1240 ret = vfio_set_irqs_validate_and_prepare(&hdr, max, VFIO_PCI_NUM_IRQS, 1241 &data_size); 1242 if (ret) 1243 return ret; 1244 1245 if (data_size) { 1246 data = memdup_user(&arg->data, data_size); 1247 if (IS_ERR(data)) 1248 return PTR_ERR(data); 1249 } 1250 1251 mutex_lock(&vdev->igate); 1252 1253 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index, hdr.start, 1254 hdr.count, data); 1255 1256 mutex_unlock(&vdev->igate); 1257 kfree(data); 1258 1259 return ret; 1260 } 1261 1262 static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev, 1263 void __user *arg) 1264 { 1265 int ret; 1266 1267 if (!vdev->reset_works) 1268 return -EINVAL; 1269 1270 vfio_pci_zap_and_down_write_memory_lock(vdev); 1271 1272 /* 1273 * This function can be invoked while the power state is non-D0. If 1274 * pci_try_reset_function() has been called while the power state is 1275 * non-D0, then pci_try_reset_function() will internally set the power 1276 * state to D0 without vfio driver involvement. For the devices which 1277 * have NoSoftRst-, the reset function can cause the PCI config space 1278 * reset without restoring the original state (saved locally in 1279 * 'vdev->pm_save'). 1280 */ 1281 vfio_pci_set_power_state(vdev, PCI_D0); 1282 1283 vfio_pci_dma_buf_move(vdev, true); 1284 ret = pci_try_reset_function(vdev->pdev); 1285 if (__vfio_pci_memory_enabled(vdev)) 1286 vfio_pci_dma_buf_move(vdev, false); 1287 up_write(&vdev->memory_lock); 1288 1289 return ret; 1290 } 1291 1292 static int vfio_pci_ioctl_get_pci_hot_reset_info( 1293 struct vfio_pci_core_device *vdev, 1294 struct vfio_pci_hot_reset_info __user *arg) 1295 { 1296 unsigned long minsz = 1297 offsetofend(struct vfio_pci_hot_reset_info, count); 1298 struct vfio_pci_dependent_device *devices = NULL; 1299 struct vfio_pci_hot_reset_info hdr; 1300 struct vfio_pci_fill_info fill = {}; 1301 bool slot = false; 1302 int ret, count = 0; 1303 1304 if (copy_from_user(&hdr, arg, minsz)) 1305 return -EFAULT; 1306 1307 if (hdr.argsz < minsz) 1308 return -EINVAL; 1309 1310 hdr.flags = 0; 1311 1312 /* Can we do a slot or bus reset or neither? */ 1313 if (!pci_probe_reset_slot(vdev->pdev->slot)) 1314 slot = true; 1315 else if (pci_probe_reset_bus(vdev->pdev->bus)) 1316 return -ENODEV; 1317 1318 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs, 1319 &count, slot); 1320 if (ret) 1321 return ret; 1322 1323 if (WARN_ON(!count)) /* Should always be at least one */ 1324 return -ERANGE; 1325 1326 if (count > (hdr.argsz - sizeof(hdr)) / sizeof(*devices)) { 1327 hdr.count = count; 1328 ret = -ENOSPC; 1329 goto header; 1330 } 1331 1332 devices = kzalloc_objs(*devices, count); 1333 if (!devices) 1334 return -ENOMEM; 1335 1336 fill.devices = devices; 1337 fill.nr_devices = count; 1338 fill.vdev = &vdev->vdev; 1339 1340 if (vfio_device_cdev_opened(&vdev->vdev)) 1341 fill.flags |= VFIO_PCI_HOT_RESET_FLAG_DEV_ID | 1342 VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED; 1343 1344 mutex_lock(&vdev->vdev.dev_set->lock); 1345 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_fill_devs, 1346 &fill, slot); 1347 mutex_unlock(&vdev->vdev.dev_set->lock); 1348 if (ret) 1349 goto out; 1350 1351 if (copy_to_user(arg->devices, devices, 1352 sizeof(*devices) * fill.count)) { 1353 ret = -EFAULT; 1354 goto out; 1355 } 1356 1357 hdr.count = fill.count; 1358 hdr.flags = fill.flags; 1359 1360 header: 1361 if (copy_to_user(arg, &hdr, minsz)) 1362 ret = -EFAULT; 1363 out: 1364 kfree(devices); 1365 return ret; 1366 } 1367 1368 static int 1369 vfio_pci_ioctl_pci_hot_reset_groups(struct vfio_pci_core_device *vdev, 1370 u32 array_count, bool slot, 1371 struct vfio_pci_hot_reset __user *arg) 1372 { 1373 int32_t *group_fds; 1374 struct file **files; 1375 struct vfio_pci_group_info info; 1376 int file_idx, count = 0, ret = 0; 1377 1378 /* 1379 * We can't let userspace give us an arbitrarily large buffer to copy, 1380 * so verify how many we think there could be. Note groups can have 1381 * multiple devices so one group per device is the max. 1382 */ 1383 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs, 1384 &count, slot); 1385 if (ret) 1386 return ret; 1387 1388 if (array_count > count) 1389 return -EINVAL; 1390 1391 group_fds = kzalloc_objs(*group_fds, array_count); 1392 files = kzalloc_objs(*files, array_count); 1393 if (!group_fds || !files) { 1394 kfree(group_fds); 1395 kfree(files); 1396 return -ENOMEM; 1397 } 1398 1399 if (copy_from_user(group_fds, arg->group_fds, 1400 array_count * sizeof(*group_fds))) { 1401 kfree(group_fds); 1402 kfree(files); 1403 return -EFAULT; 1404 } 1405 1406 /* 1407 * Get the group file for each fd to ensure the group is held across 1408 * the reset 1409 */ 1410 for (file_idx = 0; file_idx < array_count; file_idx++) { 1411 struct file *file = fget(group_fds[file_idx]); 1412 1413 if (!file) { 1414 ret = -EBADF; 1415 break; 1416 } 1417 1418 /* Ensure the FD is a vfio group FD.*/ 1419 if (!vfio_file_is_group(file)) { 1420 fput(file); 1421 ret = -EINVAL; 1422 break; 1423 } 1424 1425 files[file_idx] = file; 1426 } 1427 1428 kfree(group_fds); 1429 1430 /* release reference to groups on error */ 1431 if (ret) 1432 goto hot_reset_release; 1433 1434 info.count = array_count; 1435 info.files = files; 1436 1437 ret = vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, &info, NULL); 1438 1439 hot_reset_release: 1440 for (file_idx--; file_idx >= 0; file_idx--) 1441 fput(files[file_idx]); 1442 1443 kfree(files); 1444 return ret; 1445 } 1446 1447 static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev, 1448 struct vfio_pci_hot_reset __user *arg) 1449 { 1450 unsigned long minsz = offsetofend(struct vfio_pci_hot_reset, count); 1451 struct vfio_pci_hot_reset hdr; 1452 bool slot = false; 1453 1454 if (copy_from_user(&hdr, arg, minsz)) 1455 return -EFAULT; 1456 1457 if (hdr.argsz < minsz || hdr.flags) 1458 return -EINVAL; 1459 1460 /* zero-length array is only for cdev opened devices */ 1461 if (!!hdr.count == vfio_device_cdev_opened(&vdev->vdev)) 1462 return -EINVAL; 1463 1464 /* Can we do a slot or bus reset or neither? */ 1465 if (!pci_probe_reset_slot(vdev->pdev->slot)) 1466 slot = true; 1467 else if (pci_probe_reset_bus(vdev->pdev->bus)) 1468 return -ENODEV; 1469 1470 if (hdr.count) 1471 return vfio_pci_ioctl_pci_hot_reset_groups(vdev, hdr.count, slot, arg); 1472 1473 return vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, NULL, 1474 vfio_iommufd_device_ictx(&vdev->vdev)); 1475 } 1476 1477 static int vfio_pci_ioctl_ioeventfd(struct vfio_pci_core_device *vdev, 1478 struct vfio_device_ioeventfd __user *arg) 1479 { 1480 unsigned long minsz = offsetofend(struct vfio_device_ioeventfd, fd); 1481 struct vfio_device_ioeventfd ioeventfd; 1482 int count; 1483 1484 if (copy_from_user(&ioeventfd, arg, minsz)) 1485 return -EFAULT; 1486 1487 if (ioeventfd.argsz < minsz) 1488 return -EINVAL; 1489 1490 if (ioeventfd.flags & ~VFIO_DEVICE_IOEVENTFD_SIZE_MASK) 1491 return -EINVAL; 1492 1493 count = ioeventfd.flags & VFIO_DEVICE_IOEVENTFD_SIZE_MASK; 1494 1495 if (hweight8(count) != 1 || ioeventfd.fd < -1) 1496 return -EINVAL; 1497 1498 return vfio_pci_ioeventfd(vdev, ioeventfd.offset, ioeventfd.data, count, 1499 ioeventfd.fd); 1500 } 1501 1502 long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, 1503 unsigned long arg) 1504 { 1505 struct vfio_pci_core_device *vdev = 1506 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1507 void __user *uarg = (void __user *)arg; 1508 1509 switch (cmd) { 1510 case VFIO_DEVICE_GET_INFO: 1511 return vfio_pci_ioctl_get_info(vdev, uarg); 1512 case VFIO_DEVICE_GET_IRQ_INFO: 1513 return vfio_pci_ioctl_get_irq_info(vdev, uarg); 1514 case VFIO_DEVICE_GET_PCI_HOT_RESET_INFO: 1515 return vfio_pci_ioctl_get_pci_hot_reset_info(vdev, uarg); 1516 case VFIO_DEVICE_IOEVENTFD: 1517 return vfio_pci_ioctl_ioeventfd(vdev, uarg); 1518 case VFIO_DEVICE_PCI_HOT_RESET: 1519 return vfio_pci_ioctl_pci_hot_reset(vdev, uarg); 1520 case VFIO_DEVICE_RESET: 1521 return vfio_pci_ioctl_reset(vdev, uarg); 1522 case VFIO_DEVICE_SET_IRQS: 1523 return vfio_pci_ioctl_set_irqs(vdev, uarg); 1524 default: 1525 return -ENOTTY; 1526 } 1527 } 1528 EXPORT_SYMBOL_GPL(vfio_pci_core_ioctl); 1529 1530 static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev, 1531 u32 flags, uuid_t __user *arg, 1532 size_t argsz) 1533 { 1534 uuid_t uuid; 1535 int ret; 1536 1537 if (!vdev->vf_token) 1538 return -ENOTTY; 1539 /* 1540 * We do not support GET of the VF Token UUID as this could 1541 * expose the token of the previous device user. 1542 */ 1543 ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, 1544 sizeof(uuid)); 1545 if (ret != 1) 1546 return ret; 1547 1548 if (copy_from_user(&uuid, arg, sizeof(uuid))) 1549 return -EFAULT; 1550 1551 mutex_lock(&vdev->vf_token->lock); 1552 uuid_copy(&vdev->vf_token->uuid, &uuid); 1553 mutex_unlock(&vdev->vf_token->lock); 1554 return 0; 1555 } 1556 1557 int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, 1558 void __user *arg, size_t argsz) 1559 { 1560 struct vfio_pci_core_device *vdev = 1561 container_of(device, struct vfio_pci_core_device, vdev); 1562 1563 switch (flags & VFIO_DEVICE_FEATURE_MASK) { 1564 case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY: 1565 return vfio_pci_core_pm_entry(vdev, flags, arg, argsz); 1566 case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY_WITH_WAKEUP: 1567 return vfio_pci_core_pm_entry_with_wakeup(vdev, flags, 1568 arg, argsz); 1569 case VFIO_DEVICE_FEATURE_LOW_POWER_EXIT: 1570 return vfio_pci_core_pm_exit(vdev, flags, arg, argsz); 1571 case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: 1572 return vfio_pci_core_feature_token(vdev, flags, arg, argsz); 1573 case VFIO_DEVICE_FEATURE_DMA_BUF: 1574 return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz); 1575 default: 1576 return -ENOTTY; 1577 } 1578 } 1579 EXPORT_SYMBOL_GPL(vfio_pci_core_ioctl_feature); 1580 1581 static ssize_t vfio_pci_rw(struct vfio_pci_core_device *vdev, char __user *buf, 1582 size_t count, loff_t *ppos, bool iswrite) 1583 { 1584 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); 1585 int ret; 1586 1587 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) 1588 return -EINVAL; 1589 1590 ret = pm_runtime_resume_and_get(&vdev->pdev->dev); 1591 if (ret) { 1592 pci_info_ratelimited(vdev->pdev, "runtime resume failed %d\n", 1593 ret); 1594 return -EIO; 1595 } 1596 1597 switch (index) { 1598 case VFIO_PCI_CONFIG_REGION_INDEX: 1599 ret = vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); 1600 break; 1601 1602 case VFIO_PCI_ROM_REGION_INDEX: 1603 if (iswrite) 1604 ret = -EINVAL; 1605 else 1606 ret = vfio_pci_bar_rw(vdev, buf, count, ppos, false); 1607 break; 1608 1609 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: 1610 ret = vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); 1611 break; 1612 1613 case VFIO_PCI_VGA_REGION_INDEX: 1614 ret = vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); 1615 break; 1616 1617 default: 1618 index -= VFIO_PCI_NUM_REGIONS; 1619 ret = vdev->region[index].ops->rw(vdev, buf, 1620 count, ppos, iswrite); 1621 break; 1622 } 1623 1624 pm_runtime_put(&vdev->pdev->dev); 1625 return ret; 1626 } 1627 1628 ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf, 1629 size_t count, loff_t *ppos) 1630 { 1631 struct vfio_pci_core_device *vdev = 1632 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1633 1634 if (!count) 1635 return 0; 1636 1637 return vfio_pci_rw(vdev, buf, count, ppos, false); 1638 } 1639 EXPORT_SYMBOL_GPL(vfio_pci_core_read); 1640 1641 ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf, 1642 size_t count, loff_t *ppos) 1643 { 1644 struct vfio_pci_core_device *vdev = 1645 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1646 1647 if (!count) 1648 return 0; 1649 1650 return vfio_pci_rw(vdev, (char __user *)buf, count, ppos, true); 1651 } 1652 EXPORT_SYMBOL_GPL(vfio_pci_core_write); 1653 1654 static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) 1655 { 1656 struct vfio_device *core_vdev = &vdev->vdev; 1657 loff_t start = VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_BAR0_REGION_INDEX); 1658 loff_t end = VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_ROM_REGION_INDEX); 1659 loff_t len = end - start; 1660 1661 unmap_mapping_range(core_vdev->inode->i_mapping, start, len, true); 1662 } 1663 1664 void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *vdev) 1665 { 1666 down_write(&vdev->memory_lock); 1667 vfio_pci_zap_bars(vdev); 1668 } 1669 1670 u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_core_device *vdev) 1671 { 1672 u16 cmd; 1673 1674 down_write(&vdev->memory_lock); 1675 pci_read_config_word(vdev->pdev, PCI_COMMAND, &cmd); 1676 if (!(cmd & PCI_COMMAND_MEMORY)) 1677 pci_write_config_word(vdev->pdev, PCI_COMMAND, 1678 cmd | PCI_COMMAND_MEMORY); 1679 1680 return cmd; 1681 } 1682 1683 void vfio_pci_memory_unlock_and_restore(struct vfio_pci_core_device *vdev, u16 cmd) 1684 { 1685 pci_write_config_word(vdev->pdev, PCI_COMMAND, cmd); 1686 up_write(&vdev->memory_lock); 1687 } 1688 1689 static unsigned long vma_to_pfn(struct vm_area_struct *vma) 1690 { 1691 struct vfio_pci_core_device *vdev = vma->vm_private_data; 1692 int index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); 1693 u64 pgoff; 1694 1695 pgoff = vma->vm_pgoff & 1696 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); 1697 1698 return (pci_resource_start(vdev->pdev, index) >> PAGE_SHIFT) + pgoff; 1699 } 1700 1701 vm_fault_t vfio_pci_vmf_insert_pfn(struct vfio_pci_core_device *vdev, 1702 struct vm_fault *vmf, 1703 unsigned long pfn, 1704 unsigned int order) 1705 { 1706 lockdep_assert_held_read(&vdev->memory_lock); 1707 1708 if (vdev->pm_runtime_engaged || !__vfio_pci_memory_enabled(vdev)) 1709 return VM_FAULT_SIGBUS; 1710 1711 if (!order) 1712 return vmf_insert_pfn(vmf->vma, vmf->address, pfn); 1713 1714 if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP) && order == PMD_ORDER) 1715 return vmf_insert_pfn_pmd(vmf, pfn, false); 1716 1717 if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) && order == PUD_ORDER) 1718 return vmf_insert_pfn_pud(vmf, pfn, false); 1719 1720 return VM_FAULT_FALLBACK; 1721 } 1722 EXPORT_SYMBOL_GPL(vfio_pci_vmf_insert_pfn); 1723 1724 static vm_fault_t vfio_pci_mmap_huge_fault(struct vm_fault *vmf, 1725 unsigned int order) 1726 { 1727 struct vm_area_struct *vma = vmf->vma; 1728 struct vfio_pci_core_device *vdev = vma->vm_private_data; 1729 unsigned long addr = vmf->address & ~((PAGE_SIZE << order) - 1); 1730 unsigned long pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; 1731 unsigned long pfn = vma_to_pfn(vma) + pgoff; 1732 vm_fault_t ret = VM_FAULT_FALLBACK; 1733 1734 if (is_aligned_for_order(vma, addr, pfn, order)) { 1735 scoped_guard(rwsem_read, &vdev->memory_lock) 1736 ret = vfio_pci_vmf_insert_pfn(vdev, vmf, pfn, order); 1737 } 1738 1739 dev_dbg_ratelimited(&vdev->pdev->dev, 1740 "%s(,order = %d) BAR %ld page offset 0x%lx: 0x%x\n", 1741 __func__, order, 1742 vma->vm_pgoff >> 1743 (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT), 1744 pgoff, (unsigned int)ret); 1745 1746 return ret; 1747 } 1748 1749 static vm_fault_t vfio_pci_mmap_page_fault(struct vm_fault *vmf) 1750 { 1751 return vfio_pci_mmap_huge_fault(vmf, 0); 1752 } 1753 1754 static const struct vm_operations_struct vfio_pci_mmap_ops = { 1755 .fault = vfio_pci_mmap_page_fault, 1756 #ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP 1757 .huge_fault = vfio_pci_mmap_huge_fault, 1758 #endif 1759 }; 1760 1761 int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma) 1762 { 1763 struct vfio_pci_core_device *vdev = 1764 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1765 struct pci_dev *pdev = vdev->pdev; 1766 unsigned int index; 1767 u64 phys_len, req_len, pgoff, req_start; 1768 void __iomem *bar_io; 1769 1770 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); 1771 1772 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) 1773 return -EINVAL; 1774 if (vma->vm_end < vma->vm_start) 1775 return -EINVAL; 1776 if ((vma->vm_flags & VM_SHARED) == 0) 1777 return -EINVAL; 1778 if (index >= VFIO_PCI_NUM_REGIONS) { 1779 int regnum = index - VFIO_PCI_NUM_REGIONS; 1780 struct vfio_pci_region *region = vdev->region + regnum; 1781 1782 if (region->ops && region->ops->mmap && 1783 (region->flags & VFIO_REGION_INFO_FLAG_MMAP)) 1784 return region->ops->mmap(vdev, region, vma); 1785 return -EINVAL; 1786 } 1787 if (index >= VFIO_PCI_ROM_REGION_INDEX) 1788 return -EINVAL; 1789 if (!vdev->bar_mmap_supported[index]) 1790 return -EINVAL; 1791 1792 phys_len = PAGE_ALIGN(pci_resource_len(pdev, index)); 1793 req_len = vma->vm_end - vma->vm_start; 1794 pgoff = vma->vm_pgoff & 1795 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); 1796 req_start = pgoff << PAGE_SHIFT; 1797 1798 if (req_start + req_len > phys_len) 1799 return -EINVAL; 1800 1801 /* 1802 * Ensure the BAR resource region is reserved for use. 1803 */ 1804 bar_io = vfio_pci_core_get_iomap(vdev, index); 1805 if (IS_ERR(bar_io)) 1806 return PTR_ERR(bar_io); 1807 1808 vma->vm_private_data = vdev; 1809 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1810 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 1811 1812 /* 1813 * Set vm_flags now, they should not be changed in the fault handler. 1814 * We want the same flags and page protection (decrypted above) as 1815 * io_remap_pfn_range() would set. 1816 * 1817 * VM_ALLOW_ANY_UNCACHED: The VMA flag is implemented for ARM64, 1818 * allowing KVM stage 2 device mapping attributes to use Normal-NC 1819 * rather than DEVICE_nGnRE, which allows guest mappings 1820 * supporting write-combining attributes (WC). ARM does not 1821 * architecturally guarantee this is safe, and indeed some MMIO 1822 * regions like the GICv2 VCPU interface can trigger uncontained 1823 * faults if Normal-NC is used. 1824 * 1825 * To safely use VFIO in KVM the platform must guarantee full 1826 * safety in the guest where no action taken against a MMIO 1827 * mapping can trigger an uncontained failure. The assumption is 1828 * that most VFIO PCI platforms support this for both mapping types, 1829 * at least in common flows, based on some expectations of how 1830 * PCI IP is integrated. Hence VM_ALLOW_ANY_UNCACHED is set in 1831 * the VMA flags. 1832 */ 1833 vm_flags_set(vma, VM_ALLOW_ANY_UNCACHED | VM_IO | VM_PFNMAP | 1834 VM_DONTEXPAND | VM_DONTDUMP); 1835 vma->vm_ops = &vfio_pci_mmap_ops; 1836 1837 return 0; 1838 } 1839 EXPORT_SYMBOL_GPL(vfio_pci_core_mmap); 1840 1841 void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count) 1842 { 1843 struct vfio_pci_core_device *vdev = 1844 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1845 struct pci_dev *pdev = vdev->pdev; 1846 struct vfio_pci_eventfd *eventfd; 1847 1848 rcu_read_lock(); 1849 eventfd = rcu_dereference(vdev->req_trigger); 1850 if (eventfd) { 1851 if (!(count % 10)) 1852 pci_notice_ratelimited(pdev, 1853 "Relaying device request to user (#%u)\n", 1854 count); 1855 eventfd_signal(eventfd->ctx); 1856 } else if (count == 0) { 1857 pci_warn(pdev, 1858 "No device request channel registered, blocked until released by user\n"); 1859 } 1860 rcu_read_unlock(); 1861 } 1862 EXPORT_SYMBOL_GPL(vfio_pci_core_request); 1863 1864 int vfio_pci_core_match_token_uuid(struct vfio_device *core_vdev, 1865 const uuid_t *uuid) 1866 1867 { 1868 struct vfio_pci_core_device *vdev = 1869 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1870 1871 /* 1872 * There's always some degree of trust or collaboration between SR-IOV 1873 * PF and VFs, even if just that the PF hosts the SR-IOV capability and 1874 * can disrupt VFs with a reset, but often the PF has more explicit 1875 * access to deny service to the VF or access data passed through the 1876 * VF. We therefore require an opt-in via a shared VF token (UUID) to 1877 * represent this trust. This both prevents that a VF driver might 1878 * assume the PF driver is a trusted, in-kernel driver, and also that 1879 * a PF driver might be replaced with a rogue driver, unknown to in-use 1880 * VF drivers. 1881 * 1882 * Therefore when presented with a VF, if the PF is a vfio device and 1883 * it is bound to the vfio-pci driver, the user needs to provide a VF 1884 * token to access the device, in the form of appending a vf_token to 1885 * the device name, for example: 1886 * 1887 * "0000:04:10.0 vf_token=bd8d9d2b-5a5f-4f5a-a211-f591514ba1f3" 1888 * 1889 * When presented with a PF which has VFs in use, the user must also 1890 * provide the current VF token to prove collaboration with existing 1891 * VF users. If VFs are not in use, the VF token provided for the PF 1892 * device will act to set the VF token. 1893 * 1894 * If the VF token is provided but unused, an error is generated. 1895 */ 1896 if (vdev->pdev->is_virtfn) { 1897 struct vfio_pci_core_device *pf_vdev = vdev->sriov_pf_core_dev; 1898 bool match; 1899 1900 if (!pf_vdev) { 1901 if (!uuid) 1902 return 0; /* PF is not vfio-pci, no VF token */ 1903 1904 pci_info_ratelimited(vdev->pdev, 1905 "VF token incorrectly provided, PF not bound to vfio-pci\n"); 1906 return -EINVAL; 1907 } 1908 1909 if (!uuid) { 1910 pci_info_ratelimited(vdev->pdev, 1911 "VF token required to access device\n"); 1912 return -EACCES; 1913 } 1914 1915 mutex_lock(&pf_vdev->vf_token->lock); 1916 match = uuid_equal(uuid, &pf_vdev->vf_token->uuid); 1917 mutex_unlock(&pf_vdev->vf_token->lock); 1918 1919 if (!match) { 1920 pci_info_ratelimited(vdev->pdev, 1921 "Incorrect VF token provided for device\n"); 1922 return -EACCES; 1923 } 1924 } else if (vdev->vf_token) { 1925 mutex_lock(&vdev->vf_token->lock); 1926 if (vdev->vf_token->users) { 1927 if (!uuid) { 1928 mutex_unlock(&vdev->vf_token->lock); 1929 pci_info_ratelimited(vdev->pdev, 1930 "VF token required to access device\n"); 1931 return -EACCES; 1932 } 1933 1934 if (!uuid_equal(uuid, &vdev->vf_token->uuid)) { 1935 mutex_unlock(&vdev->vf_token->lock); 1936 pci_info_ratelimited(vdev->pdev, 1937 "Incorrect VF token provided for device\n"); 1938 return -EACCES; 1939 } 1940 } else if (uuid) { 1941 uuid_copy(&vdev->vf_token->uuid, uuid); 1942 } 1943 1944 mutex_unlock(&vdev->vf_token->lock); 1945 } else if (uuid) { 1946 pci_info_ratelimited(vdev->pdev, 1947 "VF token incorrectly provided, not a PF or VF\n"); 1948 return -EINVAL; 1949 } 1950 1951 return 0; 1952 } 1953 EXPORT_SYMBOL_GPL(vfio_pci_core_match_token_uuid); 1954 1955 #define VF_TOKEN_ARG "vf_token=" 1956 1957 int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf) 1958 { 1959 struct vfio_pci_core_device *vdev = 1960 container_of(core_vdev, struct vfio_pci_core_device, vdev); 1961 bool vf_token = false; 1962 uuid_t uuid; 1963 int ret; 1964 1965 if (strncmp(pci_name(vdev->pdev), buf, strlen(pci_name(vdev->pdev)))) 1966 return 0; /* No match */ 1967 1968 if (strlen(buf) > strlen(pci_name(vdev->pdev))) { 1969 buf += strlen(pci_name(vdev->pdev)); 1970 1971 if (*buf != ' ') 1972 return 0; /* No match: non-whitespace after name */ 1973 1974 while (*buf) { 1975 if (*buf == ' ') { 1976 buf++; 1977 continue; 1978 } 1979 1980 if (!vf_token && !strncmp(buf, VF_TOKEN_ARG, 1981 strlen(VF_TOKEN_ARG))) { 1982 buf += strlen(VF_TOKEN_ARG); 1983 1984 if (strlen(buf) < UUID_STRING_LEN) 1985 return -EINVAL; 1986 1987 ret = uuid_parse(buf, &uuid); 1988 if (ret) 1989 return ret; 1990 1991 vf_token = true; 1992 buf += UUID_STRING_LEN; 1993 } else { 1994 /* Unknown/duplicate option */ 1995 return -EINVAL; 1996 } 1997 } 1998 } 1999 2000 ret = core_vdev->ops->match_token_uuid(core_vdev, 2001 vf_token ? &uuid : NULL); 2002 if (ret) 2003 return ret; 2004 2005 return 1; /* Match */ 2006 } 2007 EXPORT_SYMBOL_GPL(vfio_pci_core_match); 2008 2009 static int vfio_pci_bus_notifier(struct notifier_block *nb, 2010 unsigned long action, void *data) 2011 { 2012 struct vfio_pci_core_device *vdev = container_of(nb, 2013 struct vfio_pci_core_device, nb); 2014 struct device *dev = data; 2015 struct pci_dev *pdev = to_pci_dev(dev); 2016 struct pci_dev *physfn = pci_physfn(pdev); 2017 2018 if (action == BUS_NOTIFY_ADD_DEVICE && 2019 pdev->is_virtfn && physfn == vdev->pdev) { 2020 pci_info(vdev->pdev, "Captured SR-IOV VF %s driver_override\n", 2021 pci_name(pdev)); 2022 WARN_ON(device_set_driver_override(&pdev->dev, 2023 vdev->vdev.ops->name)); 2024 } else if (action == BUS_NOTIFY_BOUND_DRIVER && 2025 pdev->is_virtfn && physfn == vdev->pdev) { 2026 struct pci_driver *drv = pci_dev_driver(pdev); 2027 2028 if (drv && drv != pci_dev_driver(vdev->pdev)) 2029 pci_warn(vdev->pdev, 2030 "VF %s bound to driver %s while PF bound to driver %s\n", 2031 pci_name(pdev), drv->name, 2032 pci_dev_driver(vdev->pdev)->name); 2033 } 2034 2035 return 0; 2036 } 2037 2038 static int vfio_pci_vf_init(struct vfio_pci_core_device *vdev) 2039 { 2040 struct pci_dev *pdev = vdev->pdev; 2041 struct vfio_pci_core_device *cur; 2042 struct pci_dev *physfn; 2043 int ret; 2044 2045 if (pdev->is_virtfn) { 2046 /* 2047 * If this VF was created by our vfio_pci_core_sriov_configure() 2048 * then we can find the PF vfio_pci_core_device now, and due to 2049 * the locking in pci_disable_sriov() it cannot change until 2050 * this VF device driver is removed. 2051 */ 2052 physfn = pci_physfn(vdev->pdev); 2053 mutex_lock(&vfio_pci_sriov_pfs_mutex); 2054 list_for_each_entry(cur, &vfio_pci_sriov_pfs, sriov_pfs_item) { 2055 if (cur->pdev == physfn) { 2056 vdev->sriov_pf_core_dev = cur; 2057 break; 2058 } 2059 } 2060 mutex_unlock(&vfio_pci_sriov_pfs_mutex); 2061 return 0; 2062 } 2063 2064 /* Not a SRIOV PF */ 2065 if (!pdev->is_physfn) 2066 return 0; 2067 2068 vdev->vf_token = kzalloc_obj(*vdev->vf_token); 2069 if (!vdev->vf_token) 2070 return -ENOMEM; 2071 2072 mutex_init(&vdev->vf_token->lock); 2073 uuid_gen(&vdev->vf_token->uuid); 2074 2075 vdev->nb.notifier_call = vfio_pci_bus_notifier; 2076 ret = bus_register_notifier(&pci_bus_type, &vdev->nb); 2077 if (ret) { 2078 kfree(vdev->vf_token); 2079 return ret; 2080 } 2081 return 0; 2082 } 2083 2084 static void vfio_pci_vf_uninit(struct vfio_pci_core_device *vdev) 2085 { 2086 if (!vdev->vf_token) 2087 return; 2088 2089 bus_unregister_notifier(&pci_bus_type, &vdev->nb); 2090 WARN_ON(vdev->vf_token->users); 2091 mutex_destroy(&vdev->vf_token->lock); 2092 kfree(vdev->vf_token); 2093 } 2094 2095 static int vfio_pci_vga_init(struct vfio_pci_core_device *vdev) 2096 { 2097 struct pci_dev *pdev = vdev->pdev; 2098 int ret; 2099 2100 if (!vfio_pci_is_vga(pdev)) 2101 return 0; 2102 2103 ret = aperture_remove_conflicting_pci_devices(pdev, vdev->vdev.ops->name); 2104 if (ret) 2105 return ret; 2106 2107 ret = vga_client_register(pdev, vfio_pci_set_decode); 2108 if (ret) 2109 return ret; 2110 vga_set_legacy_decoding(pdev, vfio_pci_set_decode(pdev, false)); 2111 return 0; 2112 } 2113 2114 static void vfio_pci_vga_uninit(struct vfio_pci_core_device *vdev) 2115 { 2116 struct pci_dev *pdev = vdev->pdev; 2117 2118 if (!vfio_pci_is_vga(pdev)) 2119 return; 2120 vga_client_unregister(pdev); 2121 vga_set_legacy_decoding(pdev, VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | 2122 VGA_RSRC_LEGACY_IO | 2123 VGA_RSRC_LEGACY_MEM); 2124 } 2125 2126 int vfio_pci_core_init_dev(struct vfio_device *core_vdev) 2127 { 2128 struct vfio_pci_core_device *vdev = 2129 container_of(core_vdev, struct vfio_pci_core_device, vdev); 2130 int ret; 2131 2132 vdev->pdev = to_pci_dev(core_vdev->dev); 2133 vdev->irq_type = VFIO_PCI_NUM_IRQS; 2134 mutex_init(&vdev->igate); 2135 spin_lock_init(&vdev->irqlock); 2136 mutex_init(&vdev->ioeventfds_lock); 2137 INIT_LIST_HEAD(&vdev->dummy_resources_list); 2138 INIT_LIST_HEAD(&vdev->ioeventfds_list); 2139 INIT_LIST_HEAD(&vdev->sriov_pfs_item); 2140 ret = pcim_p2pdma_init(vdev->pdev); 2141 if (ret && ret != -EOPNOTSUPP) 2142 return ret; 2143 INIT_LIST_HEAD(&vdev->dmabufs); 2144 init_rwsem(&vdev->memory_lock); 2145 xa_init(&vdev->ctx); 2146 2147 return 0; 2148 } 2149 EXPORT_SYMBOL_GPL(vfio_pci_core_init_dev); 2150 2151 void vfio_pci_core_release_dev(struct vfio_device *core_vdev) 2152 { 2153 struct vfio_pci_core_device *vdev = 2154 container_of(core_vdev, struct vfio_pci_core_device, vdev); 2155 2156 mutex_destroy(&vdev->igate); 2157 mutex_destroy(&vdev->ioeventfds_lock); 2158 kfree(vdev->region); 2159 kfree(vdev->pm_save); 2160 } 2161 EXPORT_SYMBOL_GPL(vfio_pci_core_release_dev); 2162 2163 int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) 2164 { 2165 struct pci_dev *pdev = vdev->pdev; 2166 struct device *dev = &pdev->dev; 2167 int ret; 2168 2169 /* Drivers must set the vfio_pci_core_device to their drvdata */ 2170 if (WARN_ON(vdev != dev_get_drvdata(dev))) 2171 return -EINVAL; 2172 2173 /* Drivers must set a name. Required for sequestering SR-IOV VFs */ 2174 if (WARN_ON(!vdev->vdev.ops->name)) 2175 return -EINVAL; 2176 2177 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL) 2178 return -EINVAL; 2179 2180 if (vdev->vdev.mig_ops) { 2181 if (!(vdev->vdev.mig_ops->migration_get_state && 2182 vdev->vdev.mig_ops->migration_set_state && 2183 vdev->vdev.mig_ops->migration_get_data_size) || 2184 !(vdev->vdev.migration_flags & VFIO_MIGRATION_STOP_COPY)) 2185 return -EINVAL; 2186 } 2187 2188 if (vdev->vdev.log_ops && !(vdev->vdev.log_ops->log_start && 2189 vdev->vdev.log_ops->log_stop && 2190 vdev->vdev.log_ops->log_read_and_clear)) 2191 return -EINVAL; 2192 2193 /* 2194 * Prevent binding to PFs with VFs enabled, the VFs might be in use 2195 * by the host or other users. We cannot capture the VFs if they 2196 * already exist, nor can we track VF users. Disabling SR-IOV here 2197 * would initiate removing the VFs, which would unbind the driver, 2198 * which is prone to blocking if that VF is also in use by vfio-pci. 2199 * Just reject these PFs and let the user sort it out. 2200 */ 2201 if (pci_num_vf(pdev)) { 2202 pci_warn(pdev, "Cannot bind to PF with SR-IOV enabled\n"); 2203 return -EBUSY; 2204 } 2205 2206 if (pci_is_root_bus(pdev->bus) || pdev->is_virtfn) { 2207 ret = vfio_assign_device_set(&vdev->vdev, vdev); 2208 } else if (!pci_probe_reset_slot(pdev->slot)) { 2209 ret = vfio_assign_device_set(&vdev->vdev, pdev->slot); 2210 } else { 2211 /* 2212 * If there is no slot reset support for this device, the whole 2213 * bus needs to be grouped together to support bus-wide resets. 2214 */ 2215 ret = vfio_assign_device_set(&vdev->vdev, pdev->bus); 2216 } 2217 2218 if (ret) 2219 return ret; 2220 ret = vfio_pci_vf_init(vdev); 2221 if (ret) 2222 return ret; 2223 ret = vfio_pci_vga_init(vdev); 2224 if (ret) 2225 goto out_vf; 2226 2227 vfio_pci_probe_power_state(vdev); 2228 2229 /* 2230 * pci-core sets the device power state to an unknown value at 2231 * bootup and after being removed from a driver. The only 2232 * transition it allows from this unknown state is to D0, which 2233 * typically happens when a driver calls pci_enable_device(). 2234 * We're not ready to enable the device yet, but we do want to 2235 * be able to get to D3. Therefore first do a D0 transition 2236 * before enabling runtime PM. 2237 */ 2238 vfio_pci_set_power_state(vdev, PCI_D0); 2239 2240 dev->driver->pm = &vfio_pci_core_pm_ops; 2241 pm_runtime_allow(dev); 2242 if (!disable_idle_d3) 2243 pm_runtime_put(dev); 2244 2245 ret = vfio_register_group_dev(&vdev->vdev); 2246 if (ret) 2247 goto out_power; 2248 return 0; 2249 2250 out_power: 2251 if (!disable_idle_d3) 2252 pm_runtime_get_noresume(dev); 2253 2254 pm_runtime_forbid(dev); 2255 out_vf: 2256 vfio_pci_vf_uninit(vdev); 2257 return ret; 2258 } 2259 EXPORT_SYMBOL_GPL(vfio_pci_core_register_device); 2260 2261 void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev) 2262 { 2263 vfio_pci_core_sriov_configure(vdev, 0); 2264 2265 vfio_unregister_group_dev(&vdev->vdev); 2266 2267 vfio_pci_vf_uninit(vdev); 2268 vfio_pci_vga_uninit(vdev); 2269 2270 if (!disable_idle_d3) 2271 pm_runtime_get_noresume(&vdev->pdev->dev); 2272 2273 pm_runtime_forbid(&vdev->pdev->dev); 2274 } 2275 EXPORT_SYMBOL_GPL(vfio_pci_core_unregister_device); 2276 2277 pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, 2278 pci_channel_state_t state) 2279 { 2280 struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev); 2281 struct vfio_pci_eventfd *eventfd; 2282 2283 rcu_read_lock(); 2284 eventfd = rcu_dereference(vdev->err_trigger); 2285 if (eventfd) 2286 eventfd_signal(eventfd->ctx); 2287 rcu_read_unlock(); 2288 2289 return PCI_ERS_RESULT_CAN_RECOVER; 2290 } 2291 EXPORT_SYMBOL_GPL(vfio_pci_core_aer_err_detected); 2292 2293 int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, 2294 int nr_virtfn) 2295 { 2296 struct pci_dev *pdev = vdev->pdev; 2297 int ret = 0; 2298 2299 device_lock_assert(&pdev->dev); 2300 2301 if (nr_virtfn) { 2302 mutex_lock(&vfio_pci_sriov_pfs_mutex); 2303 /* 2304 * The thread that adds the vdev to the list is the only thread 2305 * that gets to call pci_enable_sriov() and we will only allow 2306 * it to be called once without going through 2307 * pci_disable_sriov() 2308 */ 2309 if (!list_empty(&vdev->sriov_pfs_item)) { 2310 ret = -EINVAL; 2311 goto out_unlock; 2312 } 2313 list_add_tail(&vdev->sriov_pfs_item, &vfio_pci_sriov_pfs); 2314 mutex_unlock(&vfio_pci_sriov_pfs_mutex); 2315 2316 /* 2317 * The PF power state should always be higher than the VF power 2318 * state. The PF can be in low power state either with runtime 2319 * power management (when there is no user) or PCI_PM_CTRL 2320 * register write by the user. If PF is in the low power state, 2321 * then change the power state to D0 first before enabling 2322 * SR-IOV. Also, this function can be called at any time, and 2323 * userspace PCI_PM_CTRL write can race against this code path, 2324 * so protect the same with 'memory_lock'. 2325 */ 2326 ret = pm_runtime_resume_and_get(&pdev->dev); 2327 if (ret) 2328 goto out_del; 2329 2330 down_write(&vdev->memory_lock); 2331 vfio_pci_set_power_state(vdev, PCI_D0); 2332 vdev->sriov_active = true; 2333 up_write(&vdev->memory_lock); 2334 ret = pci_enable_sriov(pdev, nr_virtfn); 2335 if (ret) { 2336 pm_runtime_put(&pdev->dev); 2337 goto out_del; 2338 } 2339 return nr_virtfn; 2340 } 2341 2342 if (pci_num_vf(pdev)) { 2343 pci_disable_sriov(pdev); 2344 pm_runtime_put(&pdev->dev); 2345 } 2346 2347 out_del: 2348 /* 2349 * Avoid taking the memory_lock intentionally. A race with a power 2350 * state transition would at most result in an -EBUSY, leaving the 2351 * device in PCI_D0. 2352 */ 2353 vdev->sriov_active = false; 2354 2355 mutex_lock(&vfio_pci_sriov_pfs_mutex); 2356 list_del_init(&vdev->sriov_pfs_item); 2357 out_unlock: 2358 mutex_unlock(&vfio_pci_sriov_pfs_mutex); 2359 return ret; 2360 } 2361 EXPORT_SYMBOL_GPL(vfio_pci_core_sriov_configure); 2362 2363 const struct pci_error_handlers vfio_pci_core_err_handlers = { 2364 .error_detected = vfio_pci_core_aer_err_detected, 2365 }; 2366 EXPORT_SYMBOL_GPL(vfio_pci_core_err_handlers); 2367 2368 static bool vfio_dev_in_groups(struct vfio_device *vdev, 2369 struct vfio_pci_group_info *groups) 2370 { 2371 unsigned int i; 2372 2373 if (!groups) 2374 return false; 2375 2376 for (i = 0; i < groups->count; i++) 2377 if (vfio_file_has_dev(groups->files[i], vdev)) 2378 return true; 2379 return false; 2380 } 2381 2382 static int vfio_pci_is_device_in_set(struct pci_dev *pdev, void *data) 2383 { 2384 struct vfio_device_set *dev_set = data; 2385 2386 return vfio_find_device_in_devset(dev_set, &pdev->dev) ? 0 : -ENODEV; 2387 } 2388 2389 /* 2390 * vfio-core considers a group to be viable and will create a vfio_device even 2391 * if some devices are bound to drivers like pci-stub or pcieport. Here we 2392 * require all PCI devices to be inside our dev_set since that ensures they stay 2393 * put and that every driver controlling the device can co-ordinate with the 2394 * device reset. 2395 * 2396 * Returns the pci_dev to pass to pci_reset_bus() if every PCI device to be 2397 * reset is inside the dev_set, and pci_reset_bus() can succeed. NULL otherwise. 2398 */ 2399 static struct pci_dev * 2400 vfio_pci_dev_set_resettable(struct vfio_device_set *dev_set) 2401 { 2402 struct pci_dev *pdev; 2403 2404 lockdep_assert_held(&dev_set->lock); 2405 2406 /* 2407 * By definition all PCI devices in the dev_set share the same PCI 2408 * reset, so any pci_dev will have the same outcomes for 2409 * pci_probe_reset_*() and pci_reset_bus(). 2410 */ 2411 pdev = list_first_entry(&dev_set->device_list, 2412 struct vfio_pci_core_device, 2413 vdev.dev_set_list)->pdev; 2414 2415 /* pci_reset_bus() is supported */ 2416 if (pci_probe_reset_slot(pdev->slot) && pci_probe_reset_bus(pdev->bus)) 2417 return NULL; 2418 2419 if (vfio_pci_for_each_slot_or_bus(pdev, vfio_pci_is_device_in_set, 2420 dev_set, 2421 !pci_probe_reset_slot(pdev->slot))) 2422 return NULL; 2423 return pdev; 2424 } 2425 2426 static int vfio_pci_dev_set_pm_runtime_get(struct vfio_device_set *dev_set) 2427 { 2428 struct vfio_pci_core_device *cur; 2429 int ret; 2430 2431 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) { 2432 ret = pm_runtime_resume_and_get(&cur->pdev->dev); 2433 if (ret) 2434 goto unwind; 2435 } 2436 2437 return 0; 2438 2439 unwind: 2440 list_for_each_entry_continue_reverse(cur, &dev_set->device_list, 2441 vdev.dev_set_list) 2442 pm_runtime_put(&cur->pdev->dev); 2443 2444 return ret; 2445 } 2446 2447 static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set, 2448 struct vfio_pci_group_info *groups, 2449 struct iommufd_ctx *iommufd_ctx) 2450 { 2451 struct vfio_pci_core_device *vdev; 2452 struct pci_dev *pdev; 2453 int ret; 2454 2455 mutex_lock(&dev_set->lock); 2456 2457 pdev = vfio_pci_dev_set_resettable(dev_set); 2458 if (!pdev) { 2459 ret = -EINVAL; 2460 goto err_unlock; 2461 } 2462 2463 /* 2464 * Some of the devices in the dev_set can be in the runtime suspended 2465 * state. Increment the usage count for all the devices in the dev_set 2466 * before reset and decrement the same after reset. 2467 */ 2468 ret = vfio_pci_dev_set_pm_runtime_get(dev_set); 2469 if (ret) 2470 goto err_unlock; 2471 2472 list_for_each_entry(vdev, &dev_set->device_list, vdev.dev_set_list) { 2473 bool owned; 2474 2475 /* 2476 * Test whether all the affected devices can be reset by the 2477 * user. 2478 * 2479 * If called from a group opened device and the user provides 2480 * a set of groups, all the devices in the dev_set should be 2481 * contained by the set of groups provided by the user. 2482 * 2483 * If called from a cdev opened device and the user provides 2484 * a zero-length array, all the devices in the dev_set must 2485 * be bound to the same iommufd_ctx as the input iommufd_ctx. 2486 * If there is any device that has not been bound to any 2487 * iommufd_ctx yet, check if its iommu_group has any device 2488 * bound to the input iommufd_ctx. Such devices can be 2489 * considered owned by the input iommufd_ctx as the device 2490 * cannot be owned by another iommufd_ctx when its iommu_group 2491 * is owned. 2492 * 2493 * Otherwise, reset is not allowed. 2494 */ 2495 if (iommufd_ctx) { 2496 int devid = vfio_iommufd_get_dev_id(&vdev->vdev, 2497 iommufd_ctx); 2498 2499 owned = (devid > 0 || devid == -ENOENT); 2500 } else { 2501 owned = vfio_dev_in_groups(&vdev->vdev, groups); 2502 } 2503 2504 if (!owned) { 2505 ret = -EINVAL; 2506 break; 2507 } 2508 2509 /* 2510 * Take the memory write lock for each device and zap BAR 2511 * mappings to prevent the user accessing the device while in 2512 * reset. Locking multiple devices is prone to deadlock, 2513 * runaway and unwind if we hit contention. 2514 */ 2515 if (!down_write_trylock(&vdev->memory_lock)) { 2516 ret = -EBUSY; 2517 break; 2518 } 2519 2520 vfio_pci_dma_buf_move(vdev, true); 2521 vfio_pci_zap_bars(vdev); 2522 } 2523 2524 if (!list_entry_is_head(vdev, 2525 &dev_set->device_list, vdev.dev_set_list)) { 2526 vdev = list_prev_entry(vdev, vdev.dev_set_list); 2527 goto err_undo; 2528 } 2529 2530 /* 2531 * The pci_reset_bus() will reset all the devices in the bus. 2532 * The power state can be non-D0 for some of the devices in the bus. 2533 * For these devices, the pci_reset_bus() will internally set 2534 * the power state to D0 without vfio driver involvement. 2535 * For the devices which have NoSoftRst-, the reset function can 2536 * cause the PCI config space reset without restoring the original 2537 * state (saved locally in 'vdev->pm_save'). 2538 */ 2539 list_for_each_entry(vdev, &dev_set->device_list, vdev.dev_set_list) 2540 vfio_pci_set_power_state(vdev, PCI_D0); 2541 2542 ret = pci_reset_bus(pdev); 2543 2544 vdev = list_last_entry(&dev_set->device_list, 2545 struct vfio_pci_core_device, vdev.dev_set_list); 2546 2547 err_undo: 2548 list_for_each_entry_from_reverse(vdev, &dev_set->device_list, 2549 vdev.dev_set_list) { 2550 if (vdev->vdev.open_count && __vfio_pci_memory_enabled(vdev)) 2551 vfio_pci_dma_buf_move(vdev, false); 2552 up_write(&vdev->memory_lock); 2553 } 2554 2555 list_for_each_entry(vdev, &dev_set->device_list, vdev.dev_set_list) 2556 pm_runtime_put(&vdev->pdev->dev); 2557 2558 err_unlock: 2559 mutex_unlock(&dev_set->lock); 2560 return ret; 2561 } 2562 2563 static bool vfio_pci_dev_set_needs_reset(struct vfio_device_set *dev_set) 2564 { 2565 struct vfio_pci_core_device *cur; 2566 bool needs_reset = false; 2567 2568 /* No other VFIO device in the set can be open. */ 2569 if (vfio_device_set_open_count(dev_set) > 1) 2570 return false; 2571 2572 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) 2573 needs_reset |= cur->needs_reset; 2574 return needs_reset; 2575 } 2576 2577 /* 2578 * If a bus or slot reset is available for the provided dev_set and: 2579 * - All of the devices affected by that bus or slot reset are unused 2580 * - At least one of the affected devices is marked dirty via 2581 * needs_reset (such as by lack of FLR support) 2582 * Then attempt to perform that bus or slot reset. 2583 */ 2584 static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set) 2585 { 2586 struct vfio_pci_core_device *cur; 2587 struct pci_dev *pdev; 2588 bool reset_done = false; 2589 2590 if (!vfio_pci_dev_set_needs_reset(dev_set)) 2591 return; 2592 2593 pdev = vfio_pci_dev_set_resettable(dev_set); 2594 if (!pdev) 2595 return; 2596 2597 /* 2598 * Some of the devices in the bus can be in the runtime suspended 2599 * state. Increment the usage count for all the devices in the dev_set 2600 * before reset and decrement the same after reset. 2601 */ 2602 if (!disable_idle_d3 && vfio_pci_dev_set_pm_runtime_get(dev_set)) 2603 return; 2604 2605 if (!pci_reset_bus(pdev)) 2606 reset_done = true; 2607 2608 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) { 2609 if (reset_done) 2610 cur->needs_reset = false; 2611 2612 if (!disable_idle_d3) 2613 pm_runtime_put(&cur->pdev->dev); 2614 } 2615 } 2616 2617 void vfio_pci_core_set_params(bool is_nointxmask, bool is_disable_vga, 2618 bool is_disable_idle_d3) 2619 { 2620 nointxmask = is_nointxmask; 2621 disable_vga = is_disable_vga; 2622 disable_idle_d3 = is_disable_idle_d3; 2623 } 2624 EXPORT_SYMBOL_GPL(vfio_pci_core_set_params); 2625 2626 static void vfio_pci_core_cleanup(void) 2627 { 2628 vfio_pci_uninit_perm_bits(); 2629 } 2630 2631 static int __init vfio_pci_core_init(void) 2632 { 2633 /* Allocate shared config space permission data used by all devices */ 2634 return vfio_pci_init_perm_bits(); 2635 } 2636 2637 module_init(vfio_pci_core_init); 2638 module_exit(vfio_pci_core_cleanup); 2639 2640 MODULE_LICENSE("GPL v2"); 2641 MODULE_AUTHOR(DRIVER_AUTHOR); 2642 MODULE_DESCRIPTION(DRIVER_DESC); 2643