David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. |
| 4 | * Author: Alex Williamson <alex.williamson@redhat.com> |
| 5 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12 | #define dev_fmt pr_fmt |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 13 | |
| 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/vfio.h> |
| 28 | #include <linux/vgaarb.h> |
| 29 | #include <linux/nospec.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 30 | #include <linux/sched/mm.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 31 | |
| 32 | #include "vfio_pci_private.h" |
| 33 | |
| 34 | #define DRIVER_VERSION "0.2" |
| 35 | #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" |
| 36 | #define DRIVER_DESC "VFIO PCI - User Level meta-driver" |
| 37 | |
| 38 | static char ids[1024] __initdata; |
| 39 | module_param_string(ids, ids, sizeof(ids), 0); |
| 40 | MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the vfio driver, format is \"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\" and multiple comma separated entries can be specified"); |
| 41 | |
| 42 | static bool nointxmask; |
| 43 | module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR); |
| 44 | MODULE_PARM_DESC(nointxmask, |
| 45 | "Disable support for PCI 2.3 style INTx masking. If this resolves problems for specific devices, report lspci -vvvxxx to linux-pci@vger.kernel.org so the device can be fixed automatically via the broken_intx_masking flag."); |
| 46 | |
| 47 | #ifdef CONFIG_VFIO_PCI_VGA |
| 48 | static bool disable_vga; |
| 49 | module_param(disable_vga, bool, S_IRUGO); |
| 50 | MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci"); |
| 51 | #endif |
| 52 | |
| 53 | static bool disable_idle_d3; |
| 54 | module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR); |
| 55 | MODULE_PARM_DESC(disable_idle_d3, |
| 56 | "Disable using the PCI D3 low power state for idle, unused devices"); |
| 57 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | static inline bool vfio_vga_disabled(void) |
| 59 | { |
| 60 | #ifdef CONFIG_VFIO_PCI_VGA |
| 61 | return disable_vga; |
| 62 | #else |
| 63 | return true; |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Our VGA arbiter participation is limited since we don't know anything |
| 69 | * about the device itself. However, if the device is the only VGA device |
| 70 | * downstream of a bridge and VFIO VGA support is disabled, then we can |
| 71 | * safely return legacy VGA IO and memory as not decoded since the user |
| 72 | * has no way to get to it and routing can be disabled externally at the |
| 73 | * bridge. |
| 74 | */ |
| 75 | static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga) |
| 76 | { |
| 77 | struct vfio_pci_device *vdev = opaque; |
| 78 | struct pci_dev *tmp = NULL, *pdev = vdev->pdev; |
| 79 | unsigned char max_busnr; |
| 80 | unsigned int decodes; |
| 81 | |
| 82 | if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus)) |
| 83 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | |
| 84 | VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; |
| 85 | |
| 86 | max_busnr = pci_bus_max_busnr(pdev->bus); |
| 87 | decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 88 | |
| 89 | while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) { |
| 90 | if (tmp == pdev || |
| 91 | pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) || |
| 92 | pci_is_root_bus(tmp->bus)) |
| 93 | continue; |
| 94 | |
| 95 | if (tmp->bus->number >= pdev->bus->number && |
| 96 | tmp->bus->number <= max_busnr) { |
| 97 | pci_dev_put(tmp); |
| 98 | decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return decodes; |
| 104 | } |
| 105 | |
| 106 | static inline bool vfio_pci_is_vga(struct pci_dev *pdev) |
| 107 | { |
| 108 | return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; |
| 109 | } |
| 110 | |
| 111 | static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev) |
| 112 | { |
| 113 | struct resource *res; |
| 114 | int bar; |
| 115 | struct vfio_pci_dummy_resource *dummy_res; |
| 116 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 117 | for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) { |
| 118 | res = vdev->pdev->resource + bar; |
| 119 | |
| 120 | if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP)) |
| 121 | goto no_mmap; |
| 122 | |
| 123 | if (!(res->flags & IORESOURCE_MEM)) |
| 124 | goto no_mmap; |
| 125 | |
| 126 | /* |
| 127 | * The PCI core shouldn't set up a resource with a |
| 128 | * type but zero size. But there may be bugs that |
| 129 | * cause us to do that. |
| 130 | */ |
| 131 | if (!resource_size(res)) |
| 132 | goto no_mmap; |
| 133 | |
| 134 | if (resource_size(res) >= PAGE_SIZE) { |
| 135 | vdev->bar_mmap_supported[bar] = true; |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (!(res->start & ~PAGE_MASK)) { |
| 140 | /* |
| 141 | * Add a dummy resource to reserve the remainder |
| 142 | * of the exclusive page in case that hot-add |
| 143 | * device's bar is assigned into it. |
| 144 | */ |
| 145 | dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL); |
| 146 | if (dummy_res == NULL) |
| 147 | goto no_mmap; |
| 148 | |
| 149 | dummy_res->resource.name = "vfio sub-page reserved"; |
| 150 | dummy_res->resource.start = res->end + 1; |
| 151 | dummy_res->resource.end = res->start + PAGE_SIZE - 1; |
| 152 | dummy_res->resource.flags = res->flags; |
| 153 | if (request_resource(res->parent, |
| 154 | &dummy_res->resource)) { |
| 155 | kfree(dummy_res); |
| 156 | goto no_mmap; |
| 157 | } |
| 158 | dummy_res->index = bar; |
| 159 | list_add(&dummy_res->res_next, |
| 160 | &vdev->dummy_resources_list); |
| 161 | vdev->bar_mmap_supported[bar] = true; |
| 162 | continue; |
| 163 | } |
| 164 | /* |
| 165 | * Here we don't handle the case when the BAR is not page |
| 166 | * aligned because we can't expect the BAR will be |
| 167 | * assigned into the same location in a page in guest |
| 168 | * when we passthrough the BAR. And it's hard to access |
| 169 | * this BAR in userspace because we have no way to get |
| 170 | * the BAR's location in a page. |
| 171 | */ |
| 172 | no_mmap: |
| 173 | vdev->bar_mmap_supported[bar] = false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev); |
| 178 | static void vfio_pci_disable(struct vfio_pci_device *vdev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 179 | static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND |
| 183 | * _and_ the ability detect when the device is asserting INTx via PCI_STATUS. |
| 184 | * If a device implements the former but not the latter we would typically |
| 185 | * expect broken_intx_masking be set and require an exclusive interrupt. |
| 186 | * However since we do have control of the device's ability to assert INTx, |
| 187 | * we can instead pretend that the device does not implement INTx, virtualizing |
| 188 | * the pin register to report zero and maintaining DisINTx set on the host. |
| 189 | */ |
| 190 | static bool vfio_pci_nointx(struct pci_dev *pdev) |
| 191 | { |
| 192 | switch (pdev->vendor) { |
| 193 | case PCI_VENDOR_ID_INTEL: |
| 194 | switch (pdev->device) { |
| 195 | /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */ |
| 196 | case 0x1572: |
| 197 | case 0x1574: |
| 198 | case 0x1580 ... 0x1581: |
| 199 | case 0x1583 ... 0x158b: |
| 200 | case 0x37d0 ... 0x37d2: |
| 201 | return true; |
| 202 | default: |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 210 | static void vfio_pci_probe_power_state(struct vfio_pci_device *vdev) |
| 211 | { |
| 212 | struct pci_dev *pdev = vdev->pdev; |
| 213 | u16 pmcsr; |
| 214 | |
| 215 | if (!pdev->pm_cap) |
| 216 | return; |
| 217 | |
| 218 | pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &pmcsr); |
| 219 | |
| 220 | vdev->needs_pm_restore = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * pci_set_power_state() wrapper handling devices which perform a soft reset on |
| 225 | * D3->D0 transition. Save state prior to D0/1/2->D3, stash it on the vdev, |
| 226 | * restore when returned to D0. Saved separately from pci_saved_state for use |
| 227 | * by PM capability emulation and separately from pci_dev internal saved state |
| 228 | * to avoid it being overwritten and consumed around other resets. |
| 229 | */ |
| 230 | int vfio_pci_set_power_state(struct vfio_pci_device *vdev, pci_power_t state) |
| 231 | { |
| 232 | struct pci_dev *pdev = vdev->pdev; |
| 233 | bool needs_restore = false, needs_save = false; |
| 234 | int ret; |
| 235 | |
| 236 | if (vdev->needs_pm_restore) { |
| 237 | if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) { |
| 238 | pci_save_state(pdev); |
| 239 | needs_save = true; |
| 240 | } |
| 241 | |
| 242 | if (pdev->current_state >= PCI_D3hot && state <= PCI_D0) |
| 243 | needs_restore = true; |
| 244 | } |
| 245 | |
| 246 | ret = pci_set_power_state(pdev, state); |
| 247 | |
| 248 | if (!ret) { |
| 249 | /* D3 might be unsupported via quirk, skip unless in D3 */ |
| 250 | if (needs_save && pdev->current_state >= PCI_D3hot) { |
| 251 | vdev->pm_save = pci_store_saved_state(pdev); |
| 252 | } else if (needs_restore) { |
| 253 | pci_load_and_free_saved_state(pdev, &vdev->pm_save); |
| 254 | pci_restore_state(pdev); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 261 | static int vfio_pci_enable(struct vfio_pci_device *vdev) |
| 262 | { |
| 263 | struct pci_dev *pdev = vdev->pdev; |
| 264 | int ret; |
| 265 | u16 cmd; |
| 266 | u8 msix_pos; |
| 267 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 268 | vfio_pci_set_power_state(vdev, PCI_D0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 269 | |
| 270 | /* Don't allow our initial saved state to include busmaster */ |
| 271 | pci_clear_master(pdev); |
| 272 | |
| 273 | ret = pci_enable_device(pdev); |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
| 277 | /* If reset fails because of the device lock, fail this path entirely */ |
| 278 | ret = pci_try_reset_function(pdev); |
| 279 | if (ret == -EAGAIN) { |
| 280 | pci_disable_device(pdev); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | vdev->reset_works = !ret; |
| 285 | pci_save_state(pdev); |
| 286 | vdev->pci_saved_state = pci_store_saved_state(pdev); |
| 287 | if (!vdev->pci_saved_state) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 288 | pci_dbg(pdev, "%s: Couldn't store saved state\n", __func__); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 289 | |
| 290 | if (likely(!nointxmask)) { |
| 291 | if (vfio_pci_nointx(pdev)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 292 | pci_info(pdev, "Masking broken INTx support\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 293 | vdev->nointx = true; |
| 294 | pci_intx(pdev, 0); |
| 295 | } else |
| 296 | vdev->pci_2_3 = pci_intx_mask_supported(pdev); |
| 297 | } |
| 298 | |
| 299 | pci_read_config_word(pdev, PCI_COMMAND, &cmd); |
| 300 | if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) { |
| 301 | cmd &= ~PCI_COMMAND_INTX_DISABLE; |
| 302 | pci_write_config_word(pdev, PCI_COMMAND, cmd); |
| 303 | } |
| 304 | |
| 305 | ret = vfio_config_init(vdev); |
| 306 | if (ret) { |
| 307 | kfree(vdev->pci_saved_state); |
| 308 | vdev->pci_saved_state = NULL; |
| 309 | pci_disable_device(pdev); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | msix_pos = pdev->msix_cap; |
| 314 | if (msix_pos) { |
| 315 | u16 flags; |
| 316 | u32 table; |
| 317 | |
| 318 | pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags); |
| 319 | pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table); |
| 320 | |
| 321 | vdev->msix_bar = table & PCI_MSIX_TABLE_BIR; |
| 322 | vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET; |
| 323 | vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16; |
| 324 | } else |
| 325 | vdev->msix_bar = 0xFF; |
| 326 | |
| 327 | if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) |
| 328 | vdev->has_vga = true; |
| 329 | |
| 330 | |
| 331 | if (vfio_pci_is_vga(pdev) && |
| 332 | pdev->vendor == PCI_VENDOR_ID_INTEL && |
| 333 | IS_ENABLED(CONFIG_VFIO_PCI_IGD)) { |
| 334 | ret = vfio_pci_igd_init(vdev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 335 | if (ret && ret != -ENODEV) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 336 | pci_warn(pdev, "Failed to setup Intel IGD regions\n"); |
| 337 | goto disable_exit; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (pdev->vendor == PCI_VENDOR_ID_NVIDIA && |
| 342 | IS_ENABLED(CONFIG_VFIO_PCI_NVLINK2)) { |
| 343 | ret = vfio_pci_nvdia_v100_nvlink2_init(vdev); |
| 344 | if (ret && ret != -ENODEV) { |
| 345 | pci_warn(pdev, "Failed to setup NVIDIA NV2 RAM region\n"); |
| 346 | goto disable_exit; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (pdev->vendor == PCI_VENDOR_ID_IBM && |
| 351 | IS_ENABLED(CONFIG_VFIO_PCI_NVLINK2)) { |
| 352 | ret = vfio_pci_ibm_npu2_init(vdev); |
| 353 | if (ret && ret != -ENODEV) { |
| 354 | pci_warn(pdev, "Failed to setup NVIDIA NV2 ATSD region\n"); |
| 355 | goto disable_exit; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | vfio_pci_probe_mmaps(vdev); |
| 360 | |
| 361 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 362 | |
| 363 | disable_exit: |
| 364 | vfio_pci_disable(vdev); |
| 365 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static void vfio_pci_disable(struct vfio_pci_device *vdev) |
| 369 | { |
| 370 | struct pci_dev *pdev = vdev->pdev; |
| 371 | struct vfio_pci_dummy_resource *dummy_res, *tmp; |
| 372 | struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; |
| 373 | int i, bar; |
| 374 | |
| 375 | /* Stop the device from further DMA */ |
| 376 | pci_clear_master(pdev); |
| 377 | |
| 378 | vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE | |
| 379 | VFIO_IRQ_SET_ACTION_TRIGGER, |
| 380 | vdev->irq_type, 0, 0, NULL); |
| 381 | |
| 382 | /* Device closed, don't need mutex here */ |
| 383 | list_for_each_entry_safe(ioeventfd, ioeventfd_tmp, |
| 384 | &vdev->ioeventfds_list, next) { |
| 385 | vfio_virqfd_disable(&ioeventfd->virqfd); |
| 386 | list_del(&ioeventfd->next); |
| 387 | kfree(ioeventfd); |
| 388 | } |
| 389 | vdev->ioeventfds_nr = 0; |
| 390 | |
| 391 | vdev->virq_disabled = false; |
| 392 | |
| 393 | for (i = 0; i < vdev->num_regions; i++) |
| 394 | vdev->region[i].ops->release(vdev, &vdev->region[i]); |
| 395 | |
| 396 | vdev->num_regions = 0; |
| 397 | kfree(vdev->region); |
| 398 | vdev->region = NULL; /* don't krealloc a freed pointer */ |
| 399 | |
| 400 | vfio_config_free(vdev); |
| 401 | |
| 402 | for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) { |
| 403 | if (!vdev->barmap[bar]) |
| 404 | continue; |
| 405 | pci_iounmap(pdev, vdev->barmap[bar]); |
| 406 | pci_release_selected_regions(pdev, 1 << bar); |
| 407 | vdev->barmap[bar] = NULL; |
| 408 | } |
| 409 | |
| 410 | list_for_each_entry_safe(dummy_res, tmp, |
| 411 | &vdev->dummy_resources_list, res_next) { |
| 412 | list_del(&dummy_res->res_next); |
| 413 | release_resource(&dummy_res->resource); |
| 414 | kfree(dummy_res); |
| 415 | } |
| 416 | |
| 417 | vdev->needs_reset = true; |
| 418 | |
| 419 | /* |
| 420 | * If we have saved state, restore it. If we can reset the device, |
| 421 | * even better. Resetting with current state seems better than |
| 422 | * nothing, but saving and restoring current state without reset |
| 423 | * is just busy work. |
| 424 | */ |
| 425 | if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 426 | pci_info(pdev, "%s: Couldn't reload saved state\n", __func__); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 427 | |
| 428 | if (!vdev->reset_works) |
| 429 | goto out; |
| 430 | |
| 431 | pci_save_state(pdev); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Disable INTx and MSI, presumably to avoid spurious interrupts |
| 436 | * during reset. Stolen from pci_reset_function() |
| 437 | */ |
| 438 | pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); |
| 439 | |
| 440 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 441 | * Try to get the locks ourselves to prevent a deadlock. The |
| 442 | * success of this is dependent on being able to lock the device, |
| 443 | * which is not always possible. |
| 444 | * We can not use the "try" reset interface here, which will |
| 445 | * overwrite the previously restored configuration information. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 446 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 447 | if (vdev->reset_works && pci_cfg_access_trylock(pdev)) { |
| 448 | if (device_trylock(&pdev->dev)) { |
| 449 | if (!__pci_reset_function_locked(pdev)) |
| 450 | vdev->needs_reset = false; |
| 451 | device_unlock(&pdev->dev); |
| 452 | } |
| 453 | pci_cfg_access_unlock(pdev); |
| 454 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 455 | |
| 456 | pci_restore_state(pdev); |
| 457 | out: |
| 458 | pci_disable_device(pdev); |
| 459 | |
| 460 | vfio_pci_try_bus_reset(vdev); |
| 461 | |
| 462 | if (!disable_idle_d3) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 463 | vfio_pci_set_power_state(vdev, PCI_D3hot); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void vfio_pci_release(void *device_data) |
| 467 | { |
| 468 | struct vfio_pci_device *vdev = device_data; |
| 469 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 470 | mutex_lock(&vdev->reflck->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 471 | |
| 472 | if (!(--vdev->refcnt)) { |
| 473 | vfio_spapr_pci_eeh_release(vdev->pdev); |
| 474 | vfio_pci_disable(vdev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 475 | mutex_lock(&vdev->igate); |
| 476 | if (vdev->err_trigger) { |
| 477 | eventfd_ctx_put(vdev->err_trigger); |
| 478 | vdev->err_trigger = NULL; |
| 479 | } |
| 480 | mutex_unlock(&vdev->igate); |
| 481 | |
| 482 | mutex_lock(&vdev->igate); |
| 483 | if (vdev->req_trigger) { |
| 484 | eventfd_ctx_put(vdev->req_trigger); |
| 485 | vdev->req_trigger = NULL; |
| 486 | } |
| 487 | mutex_unlock(&vdev->igate); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 488 | } |
| 489 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 490 | mutex_unlock(&vdev->reflck->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 491 | |
| 492 | module_put(THIS_MODULE); |
| 493 | } |
| 494 | |
| 495 | static int vfio_pci_open(void *device_data) |
| 496 | { |
| 497 | struct vfio_pci_device *vdev = device_data; |
| 498 | int ret = 0; |
| 499 | |
| 500 | if (!try_module_get(THIS_MODULE)) |
| 501 | return -ENODEV; |
| 502 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 503 | mutex_lock(&vdev->reflck->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 504 | |
| 505 | if (!vdev->refcnt) { |
| 506 | ret = vfio_pci_enable(vdev); |
| 507 | if (ret) |
| 508 | goto error; |
| 509 | |
| 510 | vfio_spapr_pci_eeh_open(vdev->pdev); |
| 511 | } |
| 512 | vdev->refcnt++; |
| 513 | error: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 514 | mutex_unlock(&vdev->reflck->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 515 | if (ret) |
| 516 | module_put(THIS_MODULE); |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type) |
| 521 | { |
| 522 | if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) { |
| 523 | u8 pin; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 524 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 525 | if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) || |
| 526 | vdev->nointx || vdev->pdev->is_virtfn) |
| 527 | return 0; |
| 528 | |
| 529 | pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin); |
| 530 | |
| 531 | return pin ? 1 : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) { |
| 533 | u8 pos; |
| 534 | u16 flags; |
| 535 | |
| 536 | pos = vdev->pdev->msi_cap; |
| 537 | if (pos) { |
| 538 | pci_read_config_word(vdev->pdev, |
| 539 | pos + PCI_MSI_FLAGS, &flags); |
| 540 | return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1); |
| 541 | } |
| 542 | } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) { |
| 543 | u8 pos; |
| 544 | u16 flags; |
| 545 | |
| 546 | pos = vdev->pdev->msix_cap; |
| 547 | if (pos) { |
| 548 | pci_read_config_word(vdev->pdev, |
| 549 | pos + PCI_MSIX_FLAGS, &flags); |
| 550 | |
| 551 | return (flags & PCI_MSIX_FLAGS_QSIZE) + 1; |
| 552 | } |
| 553 | } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) { |
| 554 | if (pci_is_pcie(vdev->pdev)) |
| 555 | return 1; |
| 556 | } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) { |
| 557 | return 1; |
| 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int vfio_pci_count_devs(struct pci_dev *pdev, void *data) |
| 564 | { |
| 565 | (*(int *)data)++; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | struct vfio_pci_fill_info { |
| 570 | int max; |
| 571 | int cur; |
| 572 | struct vfio_pci_dependent_device *devices; |
| 573 | }; |
| 574 | |
| 575 | static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data) |
| 576 | { |
| 577 | struct vfio_pci_fill_info *fill = data; |
| 578 | struct iommu_group *iommu_group; |
| 579 | |
| 580 | if (fill->cur == fill->max) |
| 581 | return -EAGAIN; /* Something changed, try again */ |
| 582 | |
| 583 | iommu_group = iommu_group_get(&pdev->dev); |
| 584 | if (!iommu_group) |
| 585 | return -EPERM; /* Cannot reset non-isolated devices */ |
| 586 | |
| 587 | fill->devices[fill->cur].group_id = iommu_group_id(iommu_group); |
| 588 | fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus); |
| 589 | fill->devices[fill->cur].bus = pdev->bus->number; |
| 590 | fill->devices[fill->cur].devfn = pdev->devfn; |
| 591 | fill->cur++; |
| 592 | iommu_group_put(iommu_group); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | struct vfio_pci_group_entry { |
| 597 | struct vfio_group *group; |
| 598 | int id; |
| 599 | }; |
| 600 | |
| 601 | struct vfio_pci_group_info { |
| 602 | int count; |
| 603 | struct vfio_pci_group_entry *groups; |
| 604 | }; |
| 605 | |
| 606 | static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data) |
| 607 | { |
| 608 | struct vfio_pci_group_info *info = data; |
| 609 | struct iommu_group *group; |
| 610 | int id, i; |
| 611 | |
| 612 | group = iommu_group_get(&pdev->dev); |
| 613 | if (!group) |
| 614 | return -EPERM; |
| 615 | |
| 616 | id = iommu_group_id(group); |
| 617 | |
| 618 | for (i = 0; i < info->count; i++) |
| 619 | if (info->groups[i].id == id) |
| 620 | break; |
| 621 | |
| 622 | iommu_group_put(group); |
| 623 | |
| 624 | return (i == info->count) ? -EINVAL : 0; |
| 625 | } |
| 626 | |
| 627 | static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot) |
| 628 | { |
| 629 | for (; pdev; pdev = pdev->bus->self) |
| 630 | if (pdev->bus == slot->bus) |
| 631 | return (pdev->slot == slot); |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | struct vfio_pci_walk_info { |
| 636 | int (*fn)(struct pci_dev *, void *data); |
| 637 | void *data; |
| 638 | struct pci_dev *pdev; |
| 639 | bool slot; |
| 640 | int ret; |
| 641 | }; |
| 642 | |
| 643 | static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data) |
| 644 | { |
| 645 | struct vfio_pci_walk_info *walk = data; |
| 646 | |
| 647 | if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot)) |
| 648 | walk->ret = walk->fn(pdev, walk->data); |
| 649 | |
| 650 | return walk->ret; |
| 651 | } |
| 652 | |
| 653 | static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev, |
| 654 | int (*fn)(struct pci_dev *, |
| 655 | void *data), void *data, |
| 656 | bool slot) |
| 657 | { |
| 658 | struct vfio_pci_walk_info walk = { |
| 659 | .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0, |
| 660 | }; |
| 661 | |
| 662 | pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk); |
| 663 | |
| 664 | return walk.ret; |
| 665 | } |
| 666 | |
| 667 | static int msix_mmappable_cap(struct vfio_pci_device *vdev, |
| 668 | struct vfio_info_cap *caps) |
| 669 | { |
| 670 | struct vfio_info_cap_header header = { |
| 671 | .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE, |
| 672 | .version = 1 |
| 673 | }; |
| 674 | |
| 675 | return vfio_info_add_capability(caps, &header, sizeof(header)); |
| 676 | } |
| 677 | |
| 678 | int vfio_pci_register_dev_region(struct vfio_pci_device *vdev, |
| 679 | unsigned int type, unsigned int subtype, |
| 680 | const struct vfio_pci_regops *ops, |
| 681 | size_t size, u32 flags, void *data) |
| 682 | { |
| 683 | struct vfio_pci_region *region; |
| 684 | |
| 685 | region = krealloc(vdev->region, |
| 686 | (vdev->num_regions + 1) * sizeof(*region), |
| 687 | GFP_KERNEL); |
| 688 | if (!region) |
| 689 | return -ENOMEM; |
| 690 | |
| 691 | vdev->region = region; |
| 692 | vdev->region[vdev->num_regions].type = type; |
| 693 | vdev->region[vdev->num_regions].subtype = subtype; |
| 694 | vdev->region[vdev->num_regions].ops = ops; |
| 695 | vdev->region[vdev->num_regions].size = size; |
| 696 | vdev->region[vdev->num_regions].flags = flags; |
| 697 | vdev->region[vdev->num_regions].data = data; |
| 698 | |
| 699 | vdev->num_regions++; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 704 | struct vfio_devices { |
| 705 | struct vfio_device **devices; |
| 706 | int cur_index; |
| 707 | int max_index; |
| 708 | }; |
| 709 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 710 | static long vfio_pci_ioctl(void *device_data, |
| 711 | unsigned int cmd, unsigned long arg) |
| 712 | { |
| 713 | struct vfio_pci_device *vdev = device_data; |
| 714 | unsigned long minsz; |
| 715 | |
| 716 | if (cmd == VFIO_DEVICE_GET_INFO) { |
| 717 | struct vfio_device_info info; |
| 718 | |
| 719 | minsz = offsetofend(struct vfio_device_info, num_irqs); |
| 720 | |
| 721 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 722 | return -EFAULT; |
| 723 | |
| 724 | if (info.argsz < minsz) |
| 725 | return -EINVAL; |
| 726 | |
| 727 | info.flags = VFIO_DEVICE_FLAGS_PCI; |
| 728 | |
| 729 | if (vdev->reset_works) |
| 730 | info.flags |= VFIO_DEVICE_FLAGS_RESET; |
| 731 | |
| 732 | info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions; |
| 733 | info.num_irqs = VFIO_PCI_NUM_IRQS; |
| 734 | |
| 735 | return copy_to_user((void __user *)arg, &info, minsz) ? |
| 736 | -EFAULT : 0; |
| 737 | |
| 738 | } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) { |
| 739 | struct pci_dev *pdev = vdev->pdev; |
| 740 | struct vfio_region_info info; |
| 741 | struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; |
| 742 | int i, ret; |
| 743 | |
| 744 | minsz = offsetofend(struct vfio_region_info, offset); |
| 745 | |
| 746 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 747 | return -EFAULT; |
| 748 | |
| 749 | if (info.argsz < minsz) |
| 750 | return -EINVAL; |
| 751 | |
| 752 | switch (info.index) { |
| 753 | case VFIO_PCI_CONFIG_REGION_INDEX: |
| 754 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 755 | info.size = pdev->cfg_size; |
| 756 | info.flags = VFIO_REGION_INFO_FLAG_READ | |
| 757 | VFIO_REGION_INFO_FLAG_WRITE; |
| 758 | break; |
| 759 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: |
| 760 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 761 | info.size = pci_resource_len(pdev, info.index); |
| 762 | if (!info.size) { |
| 763 | info.flags = 0; |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | info.flags = VFIO_REGION_INFO_FLAG_READ | |
| 768 | VFIO_REGION_INFO_FLAG_WRITE; |
| 769 | if (vdev->bar_mmap_supported[info.index]) { |
| 770 | info.flags |= VFIO_REGION_INFO_FLAG_MMAP; |
| 771 | if (info.index == vdev->msix_bar) { |
| 772 | ret = msix_mmappable_cap(vdev, &caps); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | break; |
| 779 | case VFIO_PCI_ROM_REGION_INDEX: |
| 780 | { |
| 781 | void __iomem *io; |
| 782 | size_t size; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 783 | u16 cmd; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 784 | |
| 785 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 786 | info.flags = 0; |
| 787 | |
| 788 | /* Report the BAR size, not the ROM size */ |
| 789 | info.size = pci_resource_len(pdev, info.index); |
| 790 | if (!info.size) { |
| 791 | /* Shadow ROMs appear as PCI option ROMs */ |
| 792 | if (pdev->resource[PCI_ROM_RESOURCE].flags & |
| 793 | IORESOURCE_ROM_SHADOW) |
| 794 | info.size = 0x20000; |
| 795 | else |
| 796 | break; |
| 797 | } |
| 798 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 799 | /* |
| 800 | * Is it really there? Enable memory decode for |
| 801 | * implicit access in pci_map_rom(). |
| 802 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 803 | cmd = vfio_pci_memory_lock_and_enable(vdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 804 | io = pci_map_rom(pdev, &size); |
| 805 | if (io) { |
| 806 | info.flags = VFIO_REGION_INFO_FLAG_READ; |
| 807 | pci_unmap_rom(pdev, io); |
| 808 | } else { |
| 809 | info.size = 0; |
| 810 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 811 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 812 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | case VFIO_PCI_VGA_REGION_INDEX: |
| 816 | if (!vdev->has_vga) |
| 817 | return -EINVAL; |
| 818 | |
| 819 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 820 | info.size = 0xc0000; |
| 821 | info.flags = VFIO_REGION_INFO_FLAG_READ | |
| 822 | VFIO_REGION_INFO_FLAG_WRITE; |
| 823 | |
| 824 | break; |
| 825 | default: |
| 826 | { |
| 827 | struct vfio_region_info_cap_type cap_type = { |
| 828 | .header.id = VFIO_REGION_INFO_CAP_TYPE, |
| 829 | .header.version = 1 }; |
| 830 | |
| 831 | if (info.index >= |
| 832 | VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
| 833 | return -EINVAL; |
| 834 | info.index = array_index_nospec(info.index, |
| 835 | VFIO_PCI_NUM_REGIONS + |
| 836 | vdev->num_regions); |
| 837 | |
| 838 | i = info.index - VFIO_PCI_NUM_REGIONS; |
| 839 | |
| 840 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 841 | info.size = vdev->region[i].size; |
| 842 | info.flags = vdev->region[i].flags; |
| 843 | |
| 844 | cap_type.type = vdev->region[i].type; |
| 845 | cap_type.subtype = vdev->region[i].subtype; |
| 846 | |
| 847 | ret = vfio_info_add_capability(&caps, &cap_type.header, |
| 848 | sizeof(cap_type)); |
| 849 | if (ret) |
| 850 | return ret; |
| 851 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 852 | if (vdev->region[i].ops->add_capability) { |
| 853 | ret = vdev->region[i].ops->add_capability(vdev, |
| 854 | &vdev->region[i], &caps); |
| 855 | if (ret) |
| 856 | return ret; |
| 857 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
| 861 | if (caps.size) { |
| 862 | info.flags |= VFIO_REGION_INFO_FLAG_CAPS; |
| 863 | if (info.argsz < sizeof(info) + caps.size) { |
| 864 | info.argsz = sizeof(info) + caps.size; |
| 865 | info.cap_offset = 0; |
| 866 | } else { |
| 867 | vfio_info_cap_shift(&caps, sizeof(info)); |
| 868 | if (copy_to_user((void __user *)arg + |
| 869 | sizeof(info), caps.buf, |
| 870 | caps.size)) { |
| 871 | kfree(caps.buf); |
| 872 | return -EFAULT; |
| 873 | } |
| 874 | info.cap_offset = sizeof(info); |
| 875 | } |
| 876 | |
| 877 | kfree(caps.buf); |
| 878 | } |
| 879 | |
| 880 | return copy_to_user((void __user *)arg, &info, minsz) ? |
| 881 | -EFAULT : 0; |
| 882 | |
| 883 | } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) { |
| 884 | struct vfio_irq_info info; |
| 885 | |
| 886 | minsz = offsetofend(struct vfio_irq_info, count); |
| 887 | |
| 888 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 889 | return -EFAULT; |
| 890 | |
| 891 | if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS) |
| 892 | return -EINVAL; |
| 893 | |
| 894 | switch (info.index) { |
| 895 | case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX: |
| 896 | case VFIO_PCI_REQ_IRQ_INDEX: |
| 897 | break; |
| 898 | case VFIO_PCI_ERR_IRQ_INDEX: |
| 899 | if (pci_is_pcie(vdev->pdev)) |
| 900 | break; |
| 901 | /* fall through */ |
| 902 | default: |
| 903 | return -EINVAL; |
| 904 | } |
| 905 | |
| 906 | info.flags = VFIO_IRQ_INFO_EVENTFD; |
| 907 | |
| 908 | info.count = vfio_pci_get_irq_count(vdev, info.index); |
| 909 | |
| 910 | if (info.index == VFIO_PCI_INTX_IRQ_INDEX) |
| 911 | info.flags |= (VFIO_IRQ_INFO_MASKABLE | |
| 912 | VFIO_IRQ_INFO_AUTOMASKED); |
| 913 | else |
| 914 | info.flags |= VFIO_IRQ_INFO_NORESIZE; |
| 915 | |
| 916 | return copy_to_user((void __user *)arg, &info, minsz) ? |
| 917 | -EFAULT : 0; |
| 918 | |
| 919 | } else if (cmd == VFIO_DEVICE_SET_IRQS) { |
| 920 | struct vfio_irq_set hdr; |
| 921 | u8 *data = NULL; |
| 922 | int max, ret = 0; |
| 923 | size_t data_size = 0; |
| 924 | |
| 925 | minsz = offsetofend(struct vfio_irq_set, count); |
| 926 | |
| 927 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) |
| 928 | return -EFAULT; |
| 929 | |
| 930 | max = vfio_pci_get_irq_count(vdev, hdr.index); |
| 931 | |
| 932 | ret = vfio_set_irqs_validate_and_prepare(&hdr, max, |
| 933 | VFIO_PCI_NUM_IRQS, &data_size); |
| 934 | if (ret) |
| 935 | return ret; |
| 936 | |
| 937 | if (data_size) { |
| 938 | data = memdup_user((void __user *)(arg + minsz), |
| 939 | data_size); |
| 940 | if (IS_ERR(data)) |
| 941 | return PTR_ERR(data); |
| 942 | } |
| 943 | |
| 944 | mutex_lock(&vdev->igate); |
| 945 | |
| 946 | ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index, |
| 947 | hdr.start, hdr.count, data); |
| 948 | |
| 949 | mutex_unlock(&vdev->igate); |
| 950 | kfree(data); |
| 951 | |
| 952 | return ret; |
| 953 | |
| 954 | } else if (cmd == VFIO_DEVICE_RESET) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 955 | int ret; |
| 956 | |
| 957 | if (!vdev->reset_works) |
| 958 | return -EINVAL; |
| 959 | |
| 960 | vfio_pci_zap_and_down_write_memory_lock(vdev); |
| 961 | ret = pci_try_reset_function(vdev->pdev); |
| 962 | up_write(&vdev->memory_lock); |
| 963 | |
| 964 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 965 | |
| 966 | } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) { |
| 967 | struct vfio_pci_hot_reset_info hdr; |
| 968 | struct vfio_pci_fill_info fill = { 0 }; |
| 969 | struct vfio_pci_dependent_device *devices = NULL; |
| 970 | bool slot = false; |
| 971 | int ret = 0; |
| 972 | |
| 973 | minsz = offsetofend(struct vfio_pci_hot_reset_info, count); |
| 974 | |
| 975 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) |
| 976 | return -EFAULT; |
| 977 | |
| 978 | if (hdr.argsz < minsz) |
| 979 | return -EINVAL; |
| 980 | |
| 981 | hdr.flags = 0; |
| 982 | |
| 983 | /* Can we do a slot or bus reset or neither? */ |
| 984 | if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 985 | slot = true; |
| 986 | else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 987 | return -ENODEV; |
| 988 | |
| 989 | /* How many devices are affected? */ |
| 990 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 991 | vfio_pci_count_devs, |
| 992 | &fill.max, slot); |
| 993 | if (ret) |
| 994 | return ret; |
| 995 | |
| 996 | WARN_ON(!fill.max); /* Should always be at least one */ |
| 997 | |
| 998 | /* |
| 999 | * If there's enough space, fill it now, otherwise return |
| 1000 | * -ENOSPC and the number of devices affected. |
| 1001 | */ |
| 1002 | if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) { |
| 1003 | ret = -ENOSPC; |
| 1004 | hdr.count = fill.max; |
| 1005 | goto reset_info_exit; |
| 1006 | } |
| 1007 | |
| 1008 | devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL); |
| 1009 | if (!devices) |
| 1010 | return -ENOMEM; |
| 1011 | |
| 1012 | fill.devices = devices; |
| 1013 | |
| 1014 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 1015 | vfio_pci_fill_devs, |
| 1016 | &fill, slot); |
| 1017 | |
| 1018 | /* |
| 1019 | * If a device was removed between counting and filling, |
| 1020 | * we may come up short of fill.max. If a device was |
| 1021 | * added, we'll have a return of -EAGAIN above. |
| 1022 | */ |
| 1023 | if (!ret) |
| 1024 | hdr.count = fill.cur; |
| 1025 | |
| 1026 | reset_info_exit: |
| 1027 | if (copy_to_user((void __user *)arg, &hdr, minsz)) |
| 1028 | ret = -EFAULT; |
| 1029 | |
| 1030 | if (!ret) { |
| 1031 | if (copy_to_user((void __user *)(arg + minsz), devices, |
| 1032 | hdr.count * sizeof(*devices))) |
| 1033 | ret = -EFAULT; |
| 1034 | } |
| 1035 | |
| 1036 | kfree(devices); |
| 1037 | return ret; |
| 1038 | |
| 1039 | } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) { |
| 1040 | struct vfio_pci_hot_reset hdr; |
| 1041 | int32_t *group_fds; |
| 1042 | struct vfio_pci_group_entry *groups; |
| 1043 | struct vfio_pci_group_info info; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1044 | struct vfio_devices devs = { .cur_index = 0 }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1045 | bool slot = false; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1046 | int i, group_idx, mem_idx = 0, count = 0, ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1047 | |
| 1048 | minsz = offsetofend(struct vfio_pci_hot_reset, count); |
| 1049 | |
| 1050 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) |
| 1051 | return -EFAULT; |
| 1052 | |
| 1053 | if (hdr.argsz < minsz || hdr.flags) |
| 1054 | return -EINVAL; |
| 1055 | |
| 1056 | /* Can we do a slot or bus reset or neither? */ |
| 1057 | if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 1058 | slot = true; |
| 1059 | else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 1060 | return -ENODEV; |
| 1061 | |
| 1062 | /* |
| 1063 | * We can't let userspace give us an arbitrarily large |
| 1064 | * buffer to copy, so verify how many we think there |
| 1065 | * could be. Note groups can have multiple devices so |
| 1066 | * one group per device is the max. |
| 1067 | */ |
| 1068 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 1069 | vfio_pci_count_devs, |
| 1070 | &count, slot); |
| 1071 | if (ret) |
| 1072 | return ret; |
| 1073 | |
| 1074 | /* Somewhere between 1 and count is OK */ |
| 1075 | if (!hdr.count || hdr.count > count) |
| 1076 | return -EINVAL; |
| 1077 | |
| 1078 | group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL); |
| 1079 | groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL); |
| 1080 | if (!group_fds || !groups) { |
| 1081 | kfree(group_fds); |
| 1082 | kfree(groups); |
| 1083 | return -ENOMEM; |
| 1084 | } |
| 1085 | |
| 1086 | if (copy_from_user(group_fds, (void __user *)(arg + minsz), |
| 1087 | hdr.count * sizeof(*group_fds))) { |
| 1088 | kfree(group_fds); |
| 1089 | kfree(groups); |
| 1090 | return -EFAULT; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * For each group_fd, get the group through the vfio external |
| 1095 | * user interface and store the group and iommu ID. This |
| 1096 | * ensures the group is held across the reset. |
| 1097 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1098 | for (group_idx = 0; group_idx < hdr.count; group_idx++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1099 | struct vfio_group *group; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1100 | struct fd f = fdget(group_fds[group_idx]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1101 | if (!f.file) { |
| 1102 | ret = -EBADF; |
| 1103 | break; |
| 1104 | } |
| 1105 | |
| 1106 | group = vfio_group_get_external_user(f.file); |
| 1107 | fdput(f); |
| 1108 | if (IS_ERR(group)) { |
| 1109 | ret = PTR_ERR(group); |
| 1110 | break; |
| 1111 | } |
| 1112 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1113 | groups[group_idx].group = group; |
| 1114 | groups[group_idx].id = |
| 1115 | vfio_external_user_iommu_id(group); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | kfree(group_fds); |
| 1119 | |
| 1120 | /* release reference to groups on error */ |
| 1121 | if (ret) |
| 1122 | goto hot_reset_release; |
| 1123 | |
| 1124 | info.count = hdr.count; |
| 1125 | info.groups = groups; |
| 1126 | |
| 1127 | /* |
| 1128 | * Test whether all the affected devices are contained |
| 1129 | * by the set of groups provided by the user. |
| 1130 | */ |
| 1131 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 1132 | vfio_pci_validate_devs, |
| 1133 | &info, slot); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1134 | if (ret) |
| 1135 | goto hot_reset_release; |
| 1136 | |
| 1137 | devs.max_index = count; |
| 1138 | devs.devices = kcalloc(count, sizeof(struct vfio_device *), |
| 1139 | GFP_KERNEL); |
| 1140 | if (!devs.devices) { |
| 1141 | ret = -ENOMEM; |
| 1142 | goto hot_reset_release; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * We need to get memory_lock for each device, but devices |
| 1147 | * can share mmap_sem, therefore we need to zap and hold |
| 1148 | * the vma_lock for each device, and only then get each |
| 1149 | * memory_lock. |
| 1150 | */ |
| 1151 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 1152 | vfio_pci_try_zap_and_vma_lock_cb, |
| 1153 | &devs, slot); |
| 1154 | if (ret) |
| 1155 | goto hot_reset_release; |
| 1156 | |
| 1157 | for (; mem_idx < devs.cur_index; mem_idx++) { |
| 1158 | struct vfio_pci_device *tmp; |
| 1159 | |
| 1160 | tmp = vfio_device_data(devs.devices[mem_idx]); |
| 1161 | |
| 1162 | ret = down_write_trylock(&tmp->memory_lock); |
| 1163 | if (!ret) { |
| 1164 | ret = -EBUSY; |
| 1165 | goto hot_reset_release; |
| 1166 | } |
| 1167 | mutex_unlock(&tmp->vma_lock); |
| 1168 | } |
| 1169 | |
| 1170 | /* User has access, do the reset */ |
| 1171 | ret = pci_reset_bus(vdev->pdev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1172 | |
| 1173 | hot_reset_release: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1174 | for (i = 0; i < devs.cur_index; i++) { |
| 1175 | struct vfio_device *device; |
| 1176 | struct vfio_pci_device *tmp; |
| 1177 | |
| 1178 | device = devs.devices[i]; |
| 1179 | tmp = vfio_device_data(device); |
| 1180 | |
| 1181 | if (i < mem_idx) |
| 1182 | up_write(&tmp->memory_lock); |
| 1183 | else |
| 1184 | mutex_unlock(&tmp->vma_lock); |
| 1185 | vfio_device_put(device); |
| 1186 | } |
| 1187 | kfree(devs.devices); |
| 1188 | |
| 1189 | for (group_idx--; group_idx >= 0; group_idx--) |
| 1190 | vfio_group_put_external_user(groups[group_idx].group); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1191 | |
| 1192 | kfree(groups); |
| 1193 | return ret; |
| 1194 | } else if (cmd == VFIO_DEVICE_IOEVENTFD) { |
| 1195 | struct vfio_device_ioeventfd ioeventfd; |
| 1196 | int count; |
| 1197 | |
| 1198 | minsz = offsetofend(struct vfio_device_ioeventfd, fd); |
| 1199 | |
| 1200 | if (copy_from_user(&ioeventfd, (void __user *)arg, minsz)) |
| 1201 | return -EFAULT; |
| 1202 | |
| 1203 | if (ioeventfd.argsz < minsz) |
| 1204 | return -EINVAL; |
| 1205 | |
| 1206 | if (ioeventfd.flags & ~VFIO_DEVICE_IOEVENTFD_SIZE_MASK) |
| 1207 | return -EINVAL; |
| 1208 | |
| 1209 | count = ioeventfd.flags & VFIO_DEVICE_IOEVENTFD_SIZE_MASK; |
| 1210 | |
| 1211 | if (hweight8(count) != 1 || ioeventfd.fd < -1) |
| 1212 | return -EINVAL; |
| 1213 | |
| 1214 | return vfio_pci_ioeventfd(vdev, ioeventfd.offset, |
| 1215 | ioeventfd.data, count, ioeventfd.fd); |
| 1216 | } |
| 1217 | |
| 1218 | return -ENOTTY; |
| 1219 | } |
| 1220 | |
| 1221 | static ssize_t vfio_pci_rw(void *device_data, char __user *buf, |
| 1222 | size_t count, loff_t *ppos, bool iswrite) |
| 1223 | { |
| 1224 | unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); |
| 1225 | struct vfio_pci_device *vdev = device_data; |
| 1226 | |
| 1227 | if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
| 1228 | return -EINVAL; |
| 1229 | |
| 1230 | switch (index) { |
| 1231 | case VFIO_PCI_CONFIG_REGION_INDEX: |
| 1232 | return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); |
| 1233 | |
| 1234 | case VFIO_PCI_ROM_REGION_INDEX: |
| 1235 | if (iswrite) |
| 1236 | return -EINVAL; |
| 1237 | return vfio_pci_bar_rw(vdev, buf, count, ppos, false); |
| 1238 | |
| 1239 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: |
| 1240 | return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); |
| 1241 | |
| 1242 | case VFIO_PCI_VGA_REGION_INDEX: |
| 1243 | return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); |
| 1244 | default: |
| 1245 | index -= VFIO_PCI_NUM_REGIONS; |
| 1246 | return vdev->region[index].ops->rw(vdev, buf, |
| 1247 | count, ppos, iswrite); |
| 1248 | } |
| 1249 | |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | |
| 1253 | static ssize_t vfio_pci_read(void *device_data, char __user *buf, |
| 1254 | size_t count, loff_t *ppos) |
| 1255 | { |
| 1256 | if (!count) |
| 1257 | return 0; |
| 1258 | |
| 1259 | return vfio_pci_rw(device_data, buf, count, ppos, false); |
| 1260 | } |
| 1261 | |
| 1262 | static ssize_t vfio_pci_write(void *device_data, const char __user *buf, |
| 1263 | size_t count, loff_t *ppos) |
| 1264 | { |
| 1265 | if (!count) |
| 1266 | return 0; |
| 1267 | |
| 1268 | return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true); |
| 1269 | } |
| 1270 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1271 | /* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */ |
| 1272 | static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try) |
| 1273 | { |
| 1274 | struct vfio_pci_mmap_vma *mmap_vma, *tmp; |
| 1275 | |
| 1276 | /* |
| 1277 | * Lock ordering: |
| 1278 | * vma_lock is nested under mmap_sem for vm_ops callback paths. |
| 1279 | * The memory_lock semaphore is used by both code paths calling |
| 1280 | * into this function to zap vmas and the vm_ops.fault callback |
| 1281 | * to protect the memory enable state of the device. |
| 1282 | * |
| 1283 | * When zapping vmas we need to maintain the mmap_sem => vma_lock |
| 1284 | * ordering, which requires using vma_lock to walk vma_list to |
| 1285 | * acquire an mm, then dropping vma_lock to get the mmap_sem and |
| 1286 | * reacquiring vma_lock. This logic is derived from similar |
| 1287 | * requirements in uverbs_user_mmap_disassociate(). |
| 1288 | * |
| 1289 | * mmap_sem must always be the top-level lock when it is taken. |
| 1290 | * Therefore we can only hold the memory_lock write lock when |
| 1291 | * vma_list is empty, as we'd need to take mmap_sem to clear |
| 1292 | * entries. vma_list can only be guaranteed empty when holding |
| 1293 | * vma_lock, thus memory_lock is nested under vma_lock. |
| 1294 | * |
| 1295 | * This enables the vm_ops.fault callback to acquire vma_lock, |
| 1296 | * followed by memory_lock read lock, while already holding |
| 1297 | * mmap_sem without risk of deadlock. |
| 1298 | */ |
| 1299 | while (1) { |
| 1300 | struct mm_struct *mm = NULL; |
| 1301 | |
| 1302 | if (try) { |
| 1303 | if (!mutex_trylock(&vdev->vma_lock)) |
| 1304 | return 0; |
| 1305 | } else { |
| 1306 | mutex_lock(&vdev->vma_lock); |
| 1307 | } |
| 1308 | while (!list_empty(&vdev->vma_list)) { |
| 1309 | mmap_vma = list_first_entry(&vdev->vma_list, |
| 1310 | struct vfio_pci_mmap_vma, |
| 1311 | vma_next); |
| 1312 | mm = mmap_vma->vma->vm_mm; |
| 1313 | if (mmget_not_zero(mm)) |
| 1314 | break; |
| 1315 | |
| 1316 | list_del(&mmap_vma->vma_next); |
| 1317 | kfree(mmap_vma); |
| 1318 | mm = NULL; |
| 1319 | } |
| 1320 | if (!mm) |
| 1321 | return 1; |
| 1322 | mutex_unlock(&vdev->vma_lock); |
| 1323 | |
| 1324 | if (try) { |
| 1325 | if (!down_read_trylock(&mm->mmap_sem)) { |
| 1326 | mmput(mm); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | } else { |
| 1330 | down_read(&mm->mmap_sem); |
| 1331 | } |
| 1332 | if (mmget_still_valid(mm)) { |
| 1333 | if (try) { |
| 1334 | if (!mutex_trylock(&vdev->vma_lock)) { |
| 1335 | up_read(&mm->mmap_sem); |
| 1336 | mmput(mm); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | } else { |
| 1340 | mutex_lock(&vdev->vma_lock); |
| 1341 | } |
| 1342 | list_for_each_entry_safe(mmap_vma, tmp, |
| 1343 | &vdev->vma_list, vma_next) { |
| 1344 | struct vm_area_struct *vma = mmap_vma->vma; |
| 1345 | |
| 1346 | if (vma->vm_mm != mm) |
| 1347 | continue; |
| 1348 | |
| 1349 | list_del(&mmap_vma->vma_next); |
| 1350 | kfree(mmap_vma); |
| 1351 | |
| 1352 | zap_vma_ptes(vma, vma->vm_start, |
| 1353 | vma->vm_end - vma->vm_start); |
| 1354 | } |
| 1355 | mutex_unlock(&vdev->vma_lock); |
| 1356 | } |
| 1357 | up_read(&mm->mmap_sem); |
| 1358 | mmput(mm); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device *vdev) |
| 1363 | { |
| 1364 | vfio_pci_zap_and_vma_lock(vdev, false); |
| 1365 | down_write(&vdev->memory_lock); |
| 1366 | mutex_unlock(&vdev->vma_lock); |
| 1367 | } |
| 1368 | |
| 1369 | u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev) |
| 1370 | { |
| 1371 | u16 cmd; |
| 1372 | |
| 1373 | down_write(&vdev->memory_lock); |
| 1374 | pci_read_config_word(vdev->pdev, PCI_COMMAND, &cmd); |
| 1375 | if (!(cmd & PCI_COMMAND_MEMORY)) |
| 1376 | pci_write_config_word(vdev->pdev, PCI_COMMAND, |
| 1377 | cmd | PCI_COMMAND_MEMORY); |
| 1378 | |
| 1379 | return cmd; |
| 1380 | } |
| 1381 | |
| 1382 | void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev, u16 cmd) |
| 1383 | { |
| 1384 | pci_write_config_word(vdev->pdev, PCI_COMMAND, cmd); |
| 1385 | up_write(&vdev->memory_lock); |
| 1386 | } |
| 1387 | |
| 1388 | /* Caller holds vma_lock */ |
| 1389 | static int __vfio_pci_add_vma(struct vfio_pci_device *vdev, |
| 1390 | struct vm_area_struct *vma) |
| 1391 | { |
| 1392 | struct vfio_pci_mmap_vma *mmap_vma; |
| 1393 | |
| 1394 | mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL); |
| 1395 | if (!mmap_vma) |
| 1396 | return -ENOMEM; |
| 1397 | |
| 1398 | mmap_vma->vma = vma; |
| 1399 | list_add(&mmap_vma->vma_next, &vdev->vma_list); |
| 1400 | |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * Zap mmaps on open so that we can fault them in on access and therefore |
| 1406 | * our vma_list only tracks mappings accessed since last zap. |
| 1407 | */ |
| 1408 | static void vfio_pci_mmap_open(struct vm_area_struct *vma) |
| 1409 | { |
| 1410 | zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1411 | } |
| 1412 | |
| 1413 | static void vfio_pci_mmap_close(struct vm_area_struct *vma) |
| 1414 | { |
| 1415 | struct vfio_pci_device *vdev = vma->vm_private_data; |
| 1416 | struct vfio_pci_mmap_vma *mmap_vma; |
| 1417 | |
| 1418 | mutex_lock(&vdev->vma_lock); |
| 1419 | list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) { |
| 1420 | if (mmap_vma->vma == vma) { |
| 1421 | list_del(&mmap_vma->vma_next); |
| 1422 | kfree(mmap_vma); |
| 1423 | break; |
| 1424 | } |
| 1425 | } |
| 1426 | mutex_unlock(&vdev->vma_lock); |
| 1427 | } |
| 1428 | |
| 1429 | static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) |
| 1430 | { |
| 1431 | struct vm_area_struct *vma = vmf->vma; |
| 1432 | struct vfio_pci_device *vdev = vma->vm_private_data; |
| 1433 | struct vfio_pci_mmap_vma *mmap_vma; |
| 1434 | vm_fault_t ret = VM_FAULT_NOPAGE; |
| 1435 | |
| 1436 | mutex_lock(&vdev->vma_lock); |
| 1437 | down_read(&vdev->memory_lock); |
| 1438 | |
| 1439 | if (!__vfio_pci_memory_enabled(vdev)) { |
| 1440 | ret = VM_FAULT_SIGBUS; |
| 1441 | goto up_out; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * We populate the whole vma on fault, so we need to test whether |
| 1446 | * the vma has already been mapped, such as for concurrent faults |
| 1447 | * to the same vma. io_remap_pfn_range() will trigger a BUG_ON if |
| 1448 | * we ask it to fill the same range again. |
| 1449 | */ |
| 1450 | list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) { |
| 1451 | if (mmap_vma->vma == vma) |
| 1452 | goto up_out; |
| 1453 | } |
| 1454 | |
| 1455 | if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 1456 | vma->vm_end - vma->vm_start, |
| 1457 | vma->vm_page_prot)) { |
| 1458 | ret = VM_FAULT_SIGBUS; |
| 1459 | zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1460 | goto up_out; |
| 1461 | } |
| 1462 | |
| 1463 | if (__vfio_pci_add_vma(vdev, vma)) { |
| 1464 | ret = VM_FAULT_OOM; |
| 1465 | zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1466 | } |
| 1467 | |
| 1468 | up_out: |
| 1469 | up_read(&vdev->memory_lock); |
| 1470 | mutex_unlock(&vdev->vma_lock); |
| 1471 | return ret; |
| 1472 | } |
| 1473 | |
| 1474 | static const struct vm_operations_struct vfio_pci_mmap_ops = { |
| 1475 | .open = vfio_pci_mmap_open, |
| 1476 | .close = vfio_pci_mmap_close, |
| 1477 | .fault = vfio_pci_mmap_fault, |
| 1478 | }; |
| 1479 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1480 | static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma) |
| 1481 | { |
| 1482 | struct vfio_pci_device *vdev = device_data; |
| 1483 | struct pci_dev *pdev = vdev->pdev; |
| 1484 | unsigned int index; |
| 1485 | u64 phys_len, req_len, pgoff, req_start; |
| 1486 | int ret; |
| 1487 | |
| 1488 | index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); |
| 1489 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1490 | if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
| 1491 | return -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1492 | if (vma->vm_end < vma->vm_start) |
| 1493 | return -EINVAL; |
| 1494 | if ((vma->vm_flags & VM_SHARED) == 0) |
| 1495 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1496 | if (index >= VFIO_PCI_NUM_REGIONS) { |
| 1497 | int regnum = index - VFIO_PCI_NUM_REGIONS; |
| 1498 | struct vfio_pci_region *region = vdev->region + regnum; |
| 1499 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1500 | if (region->ops && region->ops->mmap && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1501 | (region->flags & VFIO_REGION_INFO_FLAG_MMAP)) |
| 1502 | return region->ops->mmap(vdev, region, vma); |
| 1503 | return -EINVAL; |
| 1504 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1505 | if (index >= VFIO_PCI_ROM_REGION_INDEX) |
| 1506 | return -EINVAL; |
| 1507 | if (!vdev->bar_mmap_supported[index]) |
| 1508 | return -EINVAL; |
| 1509 | |
| 1510 | phys_len = PAGE_ALIGN(pci_resource_len(pdev, index)); |
| 1511 | req_len = vma->vm_end - vma->vm_start; |
| 1512 | pgoff = vma->vm_pgoff & |
| 1513 | ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); |
| 1514 | req_start = pgoff << PAGE_SHIFT; |
| 1515 | |
| 1516 | if (req_start + req_len > phys_len) |
| 1517 | return -EINVAL; |
| 1518 | |
| 1519 | /* |
| 1520 | * Even though we don't make use of the barmap for the mmap, |
| 1521 | * we need to request the region and the barmap tracks that. |
| 1522 | */ |
| 1523 | if (!vdev->barmap[index]) { |
| 1524 | ret = pci_request_selected_regions(pdev, |
| 1525 | 1 << index, "vfio-pci"); |
| 1526 | if (ret) |
| 1527 | return ret; |
| 1528 | |
| 1529 | vdev->barmap[index] = pci_iomap(pdev, index, 0); |
| 1530 | if (!vdev->barmap[index]) { |
| 1531 | pci_release_selected_regions(pdev, 1 << index); |
| 1532 | return -ENOMEM; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | vma->vm_private_data = vdev; |
| 1537 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 1538 | vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff; |
| 1539 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1540 | /* |
| 1541 | * See remap_pfn_range(), called from vfio_pci_fault() but we can't |
| 1542 | * change vm_flags within the fault handler. Set them now. |
| 1543 | */ |
| 1544 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
| 1545 | vma->vm_ops = &vfio_pci_mmap_ops; |
| 1546 | |
| 1547 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static void vfio_pci_request(void *device_data, unsigned int count) |
| 1551 | { |
| 1552 | struct vfio_pci_device *vdev = device_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1553 | struct pci_dev *pdev = vdev->pdev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1554 | |
| 1555 | mutex_lock(&vdev->igate); |
| 1556 | |
| 1557 | if (vdev->req_trigger) { |
| 1558 | if (!(count % 10)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1559 | pci_notice_ratelimited(pdev, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1560 | "Relaying device request to user (#%u)\n", |
| 1561 | count); |
| 1562 | eventfd_signal(vdev->req_trigger, 1); |
| 1563 | } else if (count == 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1564 | pci_warn(pdev, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1565 | "No device request channel registered, blocked until released by user\n"); |
| 1566 | } |
| 1567 | |
| 1568 | mutex_unlock(&vdev->igate); |
| 1569 | } |
| 1570 | |
| 1571 | static const struct vfio_device_ops vfio_pci_ops = { |
| 1572 | .name = "vfio-pci", |
| 1573 | .open = vfio_pci_open, |
| 1574 | .release = vfio_pci_release, |
| 1575 | .ioctl = vfio_pci_ioctl, |
| 1576 | .read = vfio_pci_read, |
| 1577 | .write = vfio_pci_write, |
| 1578 | .mmap = vfio_pci_mmap, |
| 1579 | .request = vfio_pci_request, |
| 1580 | }; |
| 1581 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1582 | static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev); |
| 1583 | static void vfio_pci_reflck_put(struct vfio_pci_reflck *reflck); |
| 1584 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1585 | static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 1586 | { |
| 1587 | struct vfio_pci_device *vdev; |
| 1588 | struct iommu_group *group; |
| 1589 | int ret; |
| 1590 | |
| 1591 | if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
| 1592 | return -EINVAL; |
| 1593 | |
| 1594 | /* |
| 1595 | * Prevent binding to PFs with VFs enabled, this too easily allows |
| 1596 | * userspace instance with VFs and PFs from the same device, which |
| 1597 | * cannot work. Disabling SR-IOV here would initiate removing the |
| 1598 | * VFs, which would unbind the driver, which is prone to blocking |
| 1599 | * if that VF is also in use by vfio-pci. Just reject these PFs |
| 1600 | * and let the user sort it out. |
| 1601 | */ |
| 1602 | if (pci_num_vf(pdev)) { |
| 1603 | pci_warn(pdev, "Cannot bind to PF with SR-IOV enabled\n"); |
| 1604 | return -EBUSY; |
| 1605 | } |
| 1606 | |
| 1607 | group = vfio_iommu_group_get(&pdev->dev); |
| 1608 | if (!group) |
| 1609 | return -EINVAL; |
| 1610 | |
| 1611 | vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); |
| 1612 | if (!vdev) { |
| 1613 | vfio_iommu_group_put(group, &pdev->dev); |
| 1614 | return -ENOMEM; |
| 1615 | } |
| 1616 | |
| 1617 | vdev->pdev = pdev; |
| 1618 | vdev->irq_type = VFIO_PCI_NUM_IRQS; |
| 1619 | mutex_init(&vdev->igate); |
| 1620 | spin_lock_init(&vdev->irqlock); |
| 1621 | mutex_init(&vdev->ioeventfds_lock); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1622 | INIT_LIST_HEAD(&vdev->dummy_resources_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1623 | INIT_LIST_HEAD(&vdev->ioeventfds_list); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1624 | mutex_init(&vdev->vma_lock); |
| 1625 | INIT_LIST_HEAD(&vdev->vma_list); |
| 1626 | init_rwsem(&vdev->memory_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1627 | |
| 1628 | ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev); |
| 1629 | if (ret) { |
| 1630 | vfio_iommu_group_put(group, &pdev->dev); |
| 1631 | kfree(vdev); |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1635 | ret = vfio_pci_reflck_attach(vdev); |
| 1636 | if (ret) { |
| 1637 | vfio_del_group_dev(&pdev->dev); |
| 1638 | vfio_iommu_group_put(group, &pdev->dev); |
| 1639 | kfree(vdev); |
| 1640 | return ret; |
| 1641 | } |
| 1642 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1643 | if (vfio_pci_is_vga(pdev)) { |
| 1644 | vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode); |
| 1645 | vga_set_legacy_decoding(pdev, |
| 1646 | vfio_pci_set_vga_decode(vdev, false)); |
| 1647 | } |
| 1648 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1649 | vfio_pci_probe_power_state(vdev); |
| 1650 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1651 | if (!disable_idle_d3) { |
| 1652 | /* |
| 1653 | * pci-core sets the device power state to an unknown value at |
| 1654 | * bootup and after being removed from a driver. The only |
| 1655 | * transition it allows from this unknown state is to D0, which |
| 1656 | * typically happens when a driver calls pci_enable_device(). |
| 1657 | * We're not ready to enable the device yet, but we do want to |
| 1658 | * be able to get to D3. Therefore first do a D0 transition |
| 1659 | * before going to D3. |
| 1660 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1661 | vfio_pci_set_power_state(vdev, PCI_D0); |
| 1662 | vfio_pci_set_power_state(vdev, PCI_D3hot); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | return ret; |
| 1666 | } |
| 1667 | |
| 1668 | static void vfio_pci_remove(struct pci_dev *pdev) |
| 1669 | { |
| 1670 | struct vfio_pci_device *vdev; |
| 1671 | |
| 1672 | vdev = vfio_del_group_dev(&pdev->dev); |
| 1673 | if (!vdev) |
| 1674 | return; |
| 1675 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1676 | vfio_pci_reflck_put(vdev->reflck); |
| 1677 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1678 | vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev); |
| 1679 | kfree(vdev->region); |
| 1680 | mutex_destroy(&vdev->ioeventfds_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1681 | |
| 1682 | if (!disable_idle_d3) |
| 1683 | vfio_pci_set_power_state(vdev, PCI_D0); |
| 1684 | |
| 1685 | kfree(vdev->pm_save); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1686 | kfree(vdev); |
| 1687 | |
| 1688 | if (vfio_pci_is_vga(pdev)) { |
| 1689 | vga_client_register(pdev, NULL, NULL, NULL); |
| 1690 | vga_set_legacy_decoding(pdev, |
| 1691 | VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | |
| 1692 | VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM); |
| 1693 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev, |
| 1697 | pci_channel_state_t state) |
| 1698 | { |
| 1699 | struct vfio_pci_device *vdev; |
| 1700 | struct vfio_device *device; |
| 1701 | |
| 1702 | device = vfio_device_get_from_dev(&pdev->dev); |
| 1703 | if (device == NULL) |
| 1704 | return PCI_ERS_RESULT_DISCONNECT; |
| 1705 | |
| 1706 | vdev = vfio_device_data(device); |
| 1707 | if (vdev == NULL) { |
| 1708 | vfio_device_put(device); |
| 1709 | return PCI_ERS_RESULT_DISCONNECT; |
| 1710 | } |
| 1711 | |
| 1712 | mutex_lock(&vdev->igate); |
| 1713 | |
| 1714 | if (vdev->err_trigger) |
| 1715 | eventfd_signal(vdev->err_trigger, 1); |
| 1716 | |
| 1717 | mutex_unlock(&vdev->igate); |
| 1718 | |
| 1719 | vfio_device_put(device); |
| 1720 | |
| 1721 | return PCI_ERS_RESULT_CAN_RECOVER; |
| 1722 | } |
| 1723 | |
| 1724 | static const struct pci_error_handlers vfio_err_handlers = { |
| 1725 | .error_detected = vfio_pci_aer_err_detected, |
| 1726 | }; |
| 1727 | |
| 1728 | static struct pci_driver vfio_pci_driver = { |
| 1729 | .name = "vfio-pci", |
| 1730 | .id_table = NULL, /* only dynamic ids */ |
| 1731 | .probe = vfio_pci_probe, |
| 1732 | .remove = vfio_pci_remove, |
| 1733 | .err_handler = &vfio_err_handlers, |
| 1734 | }; |
| 1735 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1736 | static DEFINE_MUTEX(reflck_lock); |
| 1737 | |
| 1738 | static struct vfio_pci_reflck *vfio_pci_reflck_alloc(void) |
| 1739 | { |
| 1740 | struct vfio_pci_reflck *reflck; |
| 1741 | |
| 1742 | reflck = kzalloc(sizeof(*reflck), GFP_KERNEL); |
| 1743 | if (!reflck) |
| 1744 | return ERR_PTR(-ENOMEM); |
| 1745 | |
| 1746 | kref_init(&reflck->kref); |
| 1747 | mutex_init(&reflck->lock); |
| 1748 | |
| 1749 | return reflck; |
| 1750 | } |
| 1751 | |
| 1752 | static void vfio_pci_reflck_get(struct vfio_pci_reflck *reflck) |
| 1753 | { |
| 1754 | kref_get(&reflck->kref); |
| 1755 | } |
| 1756 | |
| 1757 | static int vfio_pci_reflck_find(struct pci_dev *pdev, void *data) |
| 1758 | { |
| 1759 | struct vfio_pci_reflck **preflck = data; |
| 1760 | struct vfio_device *device; |
| 1761 | struct vfio_pci_device *vdev; |
| 1762 | |
| 1763 | device = vfio_device_get_from_dev(&pdev->dev); |
| 1764 | if (!device) |
| 1765 | return 0; |
| 1766 | |
| 1767 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { |
| 1768 | vfio_device_put(device); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | vdev = vfio_device_data(device); |
| 1773 | |
| 1774 | if (vdev->reflck) { |
| 1775 | vfio_pci_reflck_get(vdev->reflck); |
| 1776 | *preflck = vdev->reflck; |
| 1777 | vfio_device_put(device); |
| 1778 | return 1; |
| 1779 | } |
| 1780 | |
| 1781 | vfio_device_put(device); |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev) |
| 1786 | { |
| 1787 | bool slot = !pci_probe_reset_slot(vdev->pdev->slot); |
| 1788 | |
| 1789 | mutex_lock(&reflck_lock); |
| 1790 | |
| 1791 | if (pci_is_root_bus(vdev->pdev->bus) || |
| 1792 | vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_reflck_find, |
| 1793 | &vdev->reflck, slot) <= 0) |
| 1794 | vdev->reflck = vfio_pci_reflck_alloc(); |
| 1795 | |
| 1796 | mutex_unlock(&reflck_lock); |
| 1797 | |
| 1798 | return PTR_ERR_OR_ZERO(vdev->reflck); |
| 1799 | } |
| 1800 | |
| 1801 | static void vfio_pci_reflck_release(struct kref *kref) |
| 1802 | { |
| 1803 | struct vfio_pci_reflck *reflck = container_of(kref, |
| 1804 | struct vfio_pci_reflck, |
| 1805 | kref); |
| 1806 | |
| 1807 | kfree(reflck); |
| 1808 | mutex_unlock(&reflck_lock); |
| 1809 | } |
| 1810 | |
| 1811 | static void vfio_pci_reflck_put(struct vfio_pci_reflck *reflck) |
| 1812 | { |
| 1813 | kref_put_mutex(&reflck->kref, vfio_pci_reflck_release, &reflck_lock); |
| 1814 | } |
| 1815 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1816 | static int vfio_pci_get_unused_devs(struct pci_dev *pdev, void *data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1817 | { |
| 1818 | struct vfio_devices *devs = data; |
| 1819 | struct vfio_device *device; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1820 | struct vfio_pci_device *vdev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1821 | |
| 1822 | if (devs->cur_index == devs->max_index) |
| 1823 | return -ENOSPC; |
| 1824 | |
| 1825 | device = vfio_device_get_from_dev(&pdev->dev); |
| 1826 | if (!device) |
| 1827 | return -EINVAL; |
| 1828 | |
| 1829 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { |
| 1830 | vfio_device_put(device); |
| 1831 | return -EBUSY; |
| 1832 | } |
| 1833 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1834 | vdev = vfio_device_data(device); |
| 1835 | |
| 1836 | /* Fault if the device is not unused */ |
| 1837 | if (vdev->refcnt) { |
| 1838 | vfio_device_put(device); |
| 1839 | return -EBUSY; |
| 1840 | } |
| 1841 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1842 | devs->devices[devs->cur_index++] = device; |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1846 | static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data) |
| 1847 | { |
| 1848 | struct vfio_devices *devs = data; |
| 1849 | struct vfio_device *device; |
| 1850 | struct vfio_pci_device *vdev; |
| 1851 | |
| 1852 | if (devs->cur_index == devs->max_index) |
| 1853 | return -ENOSPC; |
| 1854 | |
| 1855 | device = vfio_device_get_from_dev(&pdev->dev); |
| 1856 | if (!device) |
| 1857 | return -EINVAL; |
| 1858 | |
| 1859 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { |
| 1860 | vfio_device_put(device); |
| 1861 | return -EBUSY; |
| 1862 | } |
| 1863 | |
| 1864 | vdev = vfio_device_data(device); |
| 1865 | |
| 1866 | /* |
| 1867 | * Locking multiple devices is prone to deadlock, runaway and |
| 1868 | * unwind if we hit contention. |
| 1869 | */ |
| 1870 | if (!vfio_pci_zap_and_vma_lock(vdev, true)) { |
| 1871 | vfio_device_put(device); |
| 1872 | return -EBUSY; |
| 1873 | } |
| 1874 | |
| 1875 | devs->devices[devs->cur_index++] = device; |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1879 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1880 | * If a bus or slot reset is available for the provided device and: |
| 1881 | * - All of the devices affected by that bus or slot reset are unused |
| 1882 | * (!refcnt) |
| 1883 | * - At least one of the affected devices is marked dirty via |
| 1884 | * needs_reset (such as by lack of FLR support) |
| 1885 | * Then attempt to perform that bus or slot reset. Callers are required |
| 1886 | * to hold vdev->reflck->lock, protecting the bus/slot reset group from |
| 1887 | * concurrent opens. A vfio_device reference is acquired for each device |
| 1888 | * to prevent unbinds during the reset operation. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1889 | * |
| 1890 | * NB: vfio-core considers a group to be viable even if some devices are |
| 1891 | * bound to drivers like pci-stub or pcieport. Here we require all devices |
| 1892 | * to be bound to vfio_pci since that's the only way we can be sure they |
| 1893 | * stay put. |
| 1894 | */ |
| 1895 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev) |
| 1896 | { |
| 1897 | struct vfio_devices devs = { .cur_index = 0 }; |
| 1898 | int i = 0, ret = -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1899 | bool slot = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1900 | struct vfio_pci_device *tmp; |
| 1901 | |
| 1902 | if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 1903 | slot = true; |
| 1904 | else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 1905 | return; |
| 1906 | |
| 1907 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs, |
| 1908 | &i, slot) || !i) |
| 1909 | return; |
| 1910 | |
| 1911 | devs.max_index = i; |
| 1912 | devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL); |
| 1913 | if (!devs.devices) |
| 1914 | return; |
| 1915 | |
| 1916 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1917 | vfio_pci_get_unused_devs, |
| 1918 | &devs, slot)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1919 | goto put_devs; |
| 1920 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1921 | /* Does at least one need a reset? */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1922 | for (i = 0; i < devs.cur_index; i++) { |
| 1923 | tmp = vfio_device_data(devs.devices[i]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1924 | if (tmp->needs_reset) { |
| 1925 | ret = pci_reset_bus(vdev->pdev); |
| 1926 | break; |
| 1927 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1930 | put_devs: |
| 1931 | for (i = 0; i < devs.cur_index; i++) { |
| 1932 | tmp = vfio_device_data(devs.devices[i]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* |
| 1935 | * If reset was successful, affected devices no longer need |
| 1936 | * a reset and we should return all the collateral devices |
| 1937 | * to low power. If not successful, we either didn't reset |
| 1938 | * the bus or timed out waiting for it, so let's not touch |
| 1939 | * the power state. |
| 1940 | */ |
| 1941 | if (!ret) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1942 | tmp->needs_reset = false; |
| 1943 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1944 | if (tmp != vdev && !disable_idle_d3) |
| 1945 | vfio_pci_set_power_state(tmp, PCI_D3hot); |
| 1946 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1947 | |
| 1948 | vfio_device_put(devs.devices[i]); |
| 1949 | } |
| 1950 | |
| 1951 | kfree(devs.devices); |
| 1952 | } |
| 1953 | |
| 1954 | static void __exit vfio_pci_cleanup(void) |
| 1955 | { |
| 1956 | pci_unregister_driver(&vfio_pci_driver); |
| 1957 | vfio_pci_uninit_perm_bits(); |
| 1958 | } |
| 1959 | |
| 1960 | static void __init vfio_pci_fill_ids(void) |
| 1961 | { |
| 1962 | char *p, *id; |
| 1963 | int rc; |
| 1964 | |
| 1965 | /* no ids passed actually */ |
| 1966 | if (ids[0] == '\0') |
| 1967 | return; |
| 1968 | |
| 1969 | /* add ids specified in the module parameter */ |
| 1970 | p = ids; |
| 1971 | while ((id = strsep(&p, ","))) { |
| 1972 | unsigned int vendor, device, subvendor = PCI_ANY_ID, |
| 1973 | subdevice = PCI_ANY_ID, class = 0, class_mask = 0; |
| 1974 | int fields; |
| 1975 | |
| 1976 | if (!strlen(id)) |
| 1977 | continue; |
| 1978 | |
| 1979 | fields = sscanf(id, "%x:%x:%x:%x:%x:%x", |
| 1980 | &vendor, &device, &subvendor, &subdevice, |
| 1981 | &class, &class_mask); |
| 1982 | |
| 1983 | if (fields < 2) { |
| 1984 | pr_warn("invalid id string \"%s\"\n", id); |
| 1985 | continue; |
| 1986 | } |
| 1987 | |
| 1988 | rc = pci_add_dynid(&vfio_pci_driver, vendor, device, |
| 1989 | subvendor, subdevice, class, class_mask, 0); |
| 1990 | if (rc) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1991 | pr_warn("failed to add dynamic id [%04x:%04x[%04x:%04x]] class %#08x/%08x (%d)\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1992 | vendor, device, subvendor, subdevice, |
| 1993 | class, class_mask, rc); |
| 1994 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1995 | pr_info("add [%04x:%04x[%04x:%04x]] class %#08x/%08x\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1996 | vendor, device, subvendor, subdevice, |
| 1997 | class, class_mask); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | static int __init vfio_pci_init(void) |
| 2002 | { |
| 2003 | int ret; |
| 2004 | |
| 2005 | /* Allocate shared config space permision data used by all devices */ |
| 2006 | ret = vfio_pci_init_perm_bits(); |
| 2007 | if (ret) |
| 2008 | return ret; |
| 2009 | |
| 2010 | /* Register and scan for devices */ |
| 2011 | ret = pci_register_driver(&vfio_pci_driver); |
| 2012 | if (ret) |
| 2013 | goto out_driver; |
| 2014 | |
| 2015 | vfio_pci_fill_ids(); |
| 2016 | |
| 2017 | return 0; |
| 2018 | |
| 2019 | out_driver: |
| 2020 | vfio_pci_uninit_perm_bits(); |
| 2021 | return ret; |
| 2022 | } |
| 2023 | |
| 2024 | module_init(vfio_pci_init); |
| 2025 | module_exit(vfio_pci_cleanup); |
| 2026 | |
| 2027 | MODULE_VERSION(DRIVER_VERSION); |
| 2028 | MODULE_LICENSE("GPL v2"); |
| 2029 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2030 | MODULE_DESCRIPTION(DRIVER_DESC); |