Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. |
| 3 | * Author: Alex Williamson <alex.williamson@redhat.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Derived from original vfio: |
| 10 | * Copyright 2010 Cisco Systems, Inc. All rights reserved. |
| 11 | * Author: Tom Lyon, pugs@cisco.com |
| 12 | */ |
| 13 | |
Alex Williamson | 80c7e8c | 2015-04-07 11:14:43 -0600 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/eventfd.h> |
Alex Williamson | 8b27ee6 | 2013-09-04 11:28:04 -0600 | [diff] [blame] | 18 | #include <linux/file.h> |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/iommu.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/notifier.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/pm_runtime.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/vfio.h> |
Alex Williamson | ecaa1f6 | 2015-04-07 11:14:41 -0600 | [diff] [blame] | 30 | #include <linux/vgaarb.h> |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [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 | |
Alex Williamson | 80c7e8c | 2015-04-07 11:14:43 -0600 | [diff] [blame] | 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 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 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 | |
Alex Williamson | 88c0dead | 2015-04-07 11:14:40 -0600 | [diff] [blame] | 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 | |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 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 | |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 58 | static DEFINE_MUTEX(driver_lock); |
| 59 | |
Alex Williamson | 88c0dead | 2015-04-07 11:14:40 -0600 | [diff] [blame] | 60 | static inline bool vfio_vga_disabled(void) |
| 61 | { |
| 62 | #ifdef CONFIG_VFIO_PCI_VGA |
| 63 | return disable_vga; |
| 64 | #else |
| 65 | return true; |
| 66 | #endif |
| 67 | } |
| 68 | |
Alex Williamson | ecaa1f6 | 2015-04-07 11:14:41 -0600 | [diff] [blame] | 69 | /* |
| 70 | * Our VGA arbiter participation is limited since we don't know anything |
| 71 | * about the device itself. However, if the device is the only VGA device |
| 72 | * downstream of a bridge and VFIO VGA support is disabled, then we can |
| 73 | * safely return legacy VGA IO and memory as not decoded since the user |
| 74 | * has no way to get to it and routing can be disabled externally at the |
| 75 | * bridge. |
| 76 | */ |
| 77 | static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga) |
| 78 | { |
| 79 | struct vfio_pci_device *vdev = opaque; |
| 80 | struct pci_dev *tmp = NULL, *pdev = vdev->pdev; |
| 81 | unsigned char max_busnr; |
| 82 | unsigned int decodes; |
| 83 | |
| 84 | if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus)) |
| 85 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | |
| 86 | VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; |
| 87 | |
| 88 | max_busnr = pci_bus_max_busnr(pdev->bus); |
| 89 | decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 90 | |
| 91 | while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) { |
| 92 | if (tmp == pdev || |
| 93 | pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) || |
| 94 | pci_is_root_bus(tmp->bus)) |
| 95 | continue; |
| 96 | |
| 97 | if (tmp->bus->number >= pdev->bus->number && |
| 98 | tmp->bus->number <= max_busnr) { |
| 99 | pci_dev_put(tmp); |
| 100 | decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return decodes; |
| 106 | } |
| 107 | |
| 108 | static inline bool vfio_pci_is_vga(struct pci_dev *pdev) |
| 109 | { |
| 110 | return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; |
| 111 | } |
| 112 | |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 113 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev); |
Alex Williamson | f572a96 | 2016-02-22 16:02:45 -0700 | [diff] [blame^] | 114 | static void vfio_pci_disable(struct vfio_pci_device *vdev); |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 115 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 116 | static int vfio_pci_enable(struct vfio_pci_device *vdev) |
| 117 | { |
| 118 | struct pci_dev *pdev = vdev->pdev; |
| 119 | int ret; |
| 120 | u16 cmd; |
| 121 | u8 msix_pos; |
| 122 | |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 123 | pci_set_power_state(pdev, PCI_D0); |
| 124 | |
Alex Williamson | 9c22e66 | 2014-08-07 11:12:02 -0600 | [diff] [blame] | 125 | /* Don't allow our initial saved state to include busmaster */ |
| 126 | pci_clear_master(pdev); |
| 127 | |
Alex Williamson | 9a92c50 | 2012-12-07 13:43:51 -0700 | [diff] [blame] | 128 | ret = pci_enable_device(pdev); |
| 129 | if (ret) |
| 130 | return ret; |
| 131 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 132 | vdev->reset_works = (pci_reset_function(pdev) == 0); |
| 133 | pci_save_state(pdev); |
| 134 | vdev->pci_saved_state = pci_store_saved_state(pdev); |
| 135 | if (!vdev->pci_saved_state) |
| 136 | pr_debug("%s: Couldn't store %s saved state\n", |
| 137 | __func__, dev_name(&pdev->dev)); |
| 138 | |
| 139 | ret = vfio_config_init(vdev); |
Alex Williamson | 9a92c50 | 2012-12-07 13:43:51 -0700 | [diff] [blame] | 140 | if (ret) { |
Alex Williamson | eb5685f | 2014-05-30 11:35:53 -0600 | [diff] [blame] | 141 | kfree(vdev->pci_saved_state); |
| 142 | vdev->pci_saved_state = NULL; |
Alex Williamson | 9a92c50 | 2012-12-07 13:43:51 -0700 | [diff] [blame] | 143 | pci_disable_device(pdev); |
| 144 | return ret; |
| 145 | } |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 146 | |
| 147 | if (likely(!nointxmask)) |
| 148 | vdev->pci_2_3 = pci_intx_mask_supported(pdev); |
| 149 | |
| 150 | pci_read_config_word(pdev, PCI_COMMAND, &cmd); |
| 151 | if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) { |
| 152 | cmd &= ~PCI_COMMAND_INTX_DISABLE; |
| 153 | pci_write_config_word(pdev, PCI_COMMAND, cmd); |
| 154 | } |
| 155 | |
Bjorn Helgaas | a9047f2 | 2013-04-18 15:12:58 -0600 | [diff] [blame] | 156 | msix_pos = pdev->msix_cap; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 157 | if (msix_pos) { |
| 158 | u16 flags; |
| 159 | u32 table; |
| 160 | |
| 161 | pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags); |
| 162 | pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table); |
| 163 | |
Bjorn Helgaas | 508d1aa | 2013-04-18 12:42:58 -0600 | [diff] [blame] | 164 | vdev->msix_bar = table & PCI_MSIX_TABLE_BIR; |
| 165 | vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 166 | vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16; |
| 167 | } else |
| 168 | vdev->msix_bar = 0xFF; |
| 169 | |
Alex Williamson | ecaa1f6 | 2015-04-07 11:14:41 -0600 | [diff] [blame] | 170 | if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) |
Alex Williamson | 84237a8 | 2013-02-18 10:11:13 -0700 | [diff] [blame] | 171 | vdev->has_vga = true; |
Alex Williamson | 84237a8 | 2013-02-18 10:11:13 -0700 | [diff] [blame] | 172 | |
Alex Williamson | 5846ff5 | 2016-02-22 16:02:43 -0700 | [diff] [blame] | 173 | |
Alex Williamson | f572a96 | 2016-02-22 16:02:45 -0700 | [diff] [blame^] | 174 | if (vfio_pci_is_vga(pdev) && |
| 175 | pdev->vendor == PCI_VENDOR_ID_INTEL && |
| 176 | IS_ENABLED(CONFIG_VFIO_PCI_IGD)) { |
| 177 | ret = vfio_pci_igd_init(vdev); |
| 178 | if (ret) { |
| 179 | dev_warn(&vdev->pdev->dev, |
| 180 | "Failed to setup Intel IGD regions\n"); |
| 181 | vfio_pci_disable(vdev); |
| 182 | return ret; |
| 183 | } |
Alex Williamson | 5846ff5 | 2016-02-22 16:02:43 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Alex Williamson | 9a92c50 | 2012-12-07 13:43:51 -0700 | [diff] [blame] | 186 | return 0; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void vfio_pci_disable(struct vfio_pci_device *vdev) |
| 190 | { |
Alex Williamson | 2007722 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 191 | struct pci_dev *pdev = vdev->pdev; |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 192 | int i, bar; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 193 | |
Alex Williamson | 9c22e66 | 2014-08-07 11:12:02 -0600 | [diff] [blame] | 194 | /* Stop the device from further DMA */ |
| 195 | pci_clear_master(pdev); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 196 | |
| 197 | vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE | |
| 198 | VFIO_IRQ_SET_ACTION_TRIGGER, |
| 199 | vdev->irq_type, 0, 0, NULL); |
| 200 | |
| 201 | vdev->virq_disabled = false; |
| 202 | |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 203 | for (i = 0; i < vdev->num_regions; i++) |
| 204 | vdev->region[i].ops->release(vdev, &vdev->region[i]); |
| 205 | |
| 206 | vdev->num_regions = 0; |
| 207 | kfree(vdev->region); |
| 208 | vdev->region = NULL; /* don't krealloc a freed pointer */ |
| 209 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 210 | vfio_config_free(vdev); |
| 211 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 212 | for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) { |
| 213 | if (!vdev->barmap[bar]) |
| 214 | continue; |
Alex Williamson | 2007722 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 215 | pci_iounmap(pdev, vdev->barmap[bar]); |
| 216 | pci_release_selected_regions(pdev, 1 << bar); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 217 | vdev->barmap[bar] = NULL; |
| 218 | } |
Alex Williamson | 2007722 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 219 | |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 220 | vdev->needs_reset = true; |
| 221 | |
Alex Williamson | 2007722 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 222 | /* |
| 223 | * If we have saved state, restore it. If we can reset the device, |
| 224 | * even better. Resetting with current state seems better than |
| 225 | * nothing, but saving and restoring current state without reset |
| 226 | * is just busy work. |
| 227 | */ |
| 228 | if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { |
| 229 | pr_info("%s: Couldn't reload %s saved state\n", |
| 230 | __func__, dev_name(&pdev->dev)); |
| 231 | |
| 232 | if (!vdev->reset_works) |
Alex Williamson | 9c22e66 | 2014-08-07 11:12:02 -0600 | [diff] [blame] | 233 | goto out; |
Alex Williamson | 2007722 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 234 | |
| 235 | pci_save_state(pdev); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Disable INTx and MSI, presumably to avoid spurious interrupts |
| 240 | * during reset. Stolen from pci_reset_function() |
| 241 | */ |
| 242 | pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); |
| 243 | |
Alex Williamson | d24cdbf | 2013-06-10 16:40:57 -0600 | [diff] [blame] | 244 | /* |
Alex Williamson | 890ed57 | 2014-01-14 20:45:09 -0700 | [diff] [blame] | 245 | * Try to reset the device. The success of this is dependent on |
| 246 | * being able to lock the device, which is not always possible. |
Alex Williamson | d24cdbf | 2013-06-10 16:40:57 -0600 | [diff] [blame] | 247 | */ |
Alex Williamson | 561d72d | 2015-04-07 11:14:44 -0600 | [diff] [blame] | 248 | if (vdev->reset_works && !pci_try_reset_function(pdev)) |
| 249 | vdev->needs_reset = false; |
Alex Williamson | 2007722 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 250 | |
| 251 | pci_restore_state(pdev); |
Alex Williamson | 9c22e66 | 2014-08-07 11:12:02 -0600 | [diff] [blame] | 252 | out: |
| 253 | pci_disable_device(pdev); |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 254 | |
| 255 | vfio_pci_try_bus_reset(vdev); |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 256 | |
| 257 | if (!disable_idle_d3) |
| 258 | pci_set_power_state(pdev, PCI_D3hot); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void vfio_pci_release(void *device_data) |
| 262 | { |
| 263 | struct vfio_pci_device *vdev = device_data; |
| 264 | |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 265 | mutex_lock(&driver_lock); |
| 266 | |
| 267 | if (!(--vdev->refcnt)) { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 268 | vfio_spapr_pci_eeh_release(vdev->pdev); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 269 | vfio_pci_disable(vdev); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 270 | } |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 271 | |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 272 | mutex_unlock(&driver_lock); |
| 273 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 274 | module_put(THIS_MODULE); |
| 275 | } |
| 276 | |
| 277 | static int vfio_pci_open(void *device_data) |
| 278 | { |
| 279 | struct vfio_pci_device *vdev = device_data; |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 280 | int ret = 0; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 281 | |
| 282 | if (!try_module_get(THIS_MODULE)) |
| 283 | return -ENODEV; |
| 284 | |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 285 | mutex_lock(&driver_lock); |
| 286 | |
| 287 | if (!vdev->refcnt) { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 288 | ret = vfio_pci_enable(vdev); |
| 289 | if (ret) |
| 290 | goto error; |
| 291 | |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 292 | vfio_spapr_pci_eeh_open(vdev->pdev); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 293 | } |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 294 | vdev->refcnt++; |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 295 | error: |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 296 | mutex_unlock(&driver_lock); |
| 297 | if (ret) |
| 298 | module_put(THIS_MODULE); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 299 | return ret; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type) |
| 303 | { |
| 304 | if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) { |
| 305 | u8 pin; |
| 306 | pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin); |
Frank Blaschka | 1d53a3a | 2014-11-07 09:52:22 -0700 | [diff] [blame] | 307 | if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin) |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 308 | return 1; |
| 309 | |
| 310 | } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) { |
| 311 | u8 pos; |
| 312 | u16 flags; |
| 313 | |
Bjorn Helgaas | a9047f2 | 2013-04-18 15:12:58 -0600 | [diff] [blame] | 314 | pos = vdev->pdev->msi_cap; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 315 | if (pos) { |
| 316 | pci_read_config_word(vdev->pdev, |
| 317 | pos + PCI_MSI_FLAGS, &flags); |
Gavin Shan | fd49c81f | 2014-05-30 11:35:54 -0600 | [diff] [blame] | 318 | return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 319 | } |
| 320 | } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) { |
| 321 | u8 pos; |
| 322 | u16 flags; |
| 323 | |
Bjorn Helgaas | a9047f2 | 2013-04-18 15:12:58 -0600 | [diff] [blame] | 324 | pos = vdev->pdev->msix_cap; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 325 | if (pos) { |
| 326 | pci_read_config_word(vdev->pdev, |
| 327 | pos + PCI_MSIX_FLAGS, &flags); |
| 328 | |
| 329 | return (flags & PCI_MSIX_FLAGS_QSIZE) + 1; |
| 330 | } |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 331 | } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) { |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 332 | if (pci_is_pcie(vdev->pdev)) |
| 333 | return 1; |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 334 | } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) { |
| 335 | return 1; |
| 336 | } |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Alex Williamson | 8b27ee6 | 2013-09-04 11:28:04 -0600 | [diff] [blame] | 341 | static int vfio_pci_count_devs(struct pci_dev *pdev, void *data) |
| 342 | { |
| 343 | (*(int *)data)++; |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | struct vfio_pci_fill_info { |
| 348 | int max; |
| 349 | int cur; |
| 350 | struct vfio_pci_dependent_device *devices; |
| 351 | }; |
| 352 | |
| 353 | static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data) |
| 354 | { |
| 355 | struct vfio_pci_fill_info *fill = data; |
| 356 | struct iommu_group *iommu_group; |
| 357 | |
| 358 | if (fill->cur == fill->max) |
| 359 | return -EAGAIN; /* Something changed, try again */ |
| 360 | |
| 361 | iommu_group = iommu_group_get(&pdev->dev); |
| 362 | if (!iommu_group) |
| 363 | return -EPERM; /* Cannot reset non-isolated devices */ |
| 364 | |
| 365 | fill->devices[fill->cur].group_id = iommu_group_id(iommu_group); |
| 366 | fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus); |
| 367 | fill->devices[fill->cur].bus = pdev->bus->number; |
| 368 | fill->devices[fill->cur].devfn = pdev->devfn; |
| 369 | fill->cur++; |
| 370 | iommu_group_put(iommu_group); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | struct vfio_pci_group_entry { |
| 375 | struct vfio_group *group; |
| 376 | int id; |
| 377 | }; |
| 378 | |
| 379 | struct vfio_pci_group_info { |
| 380 | int count; |
| 381 | struct vfio_pci_group_entry *groups; |
| 382 | }; |
| 383 | |
| 384 | static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data) |
| 385 | { |
| 386 | struct vfio_pci_group_info *info = data; |
| 387 | struct iommu_group *group; |
| 388 | int id, i; |
| 389 | |
| 390 | group = iommu_group_get(&pdev->dev); |
| 391 | if (!group) |
| 392 | return -EPERM; |
| 393 | |
| 394 | id = iommu_group_id(group); |
| 395 | |
| 396 | for (i = 0; i < info->count; i++) |
| 397 | if (info->groups[i].id == id) |
| 398 | break; |
| 399 | |
| 400 | iommu_group_put(group); |
| 401 | |
| 402 | return (i == info->count) ? -EINVAL : 0; |
| 403 | } |
| 404 | |
| 405 | static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot) |
| 406 | { |
| 407 | for (; pdev; pdev = pdev->bus->self) |
| 408 | if (pdev->bus == slot->bus) |
| 409 | return (pdev->slot == slot); |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | struct vfio_pci_walk_info { |
| 414 | int (*fn)(struct pci_dev *, void *data); |
| 415 | void *data; |
| 416 | struct pci_dev *pdev; |
| 417 | bool slot; |
| 418 | int ret; |
| 419 | }; |
| 420 | |
| 421 | static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data) |
| 422 | { |
| 423 | struct vfio_pci_walk_info *walk = data; |
| 424 | |
| 425 | if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot)) |
| 426 | walk->ret = walk->fn(pdev, walk->data); |
| 427 | |
| 428 | return walk->ret; |
| 429 | } |
| 430 | |
| 431 | static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev, |
| 432 | int (*fn)(struct pci_dev *, |
| 433 | void *data), void *data, |
| 434 | bool slot) |
| 435 | { |
| 436 | struct vfio_pci_walk_info walk = { |
| 437 | .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0, |
| 438 | }; |
| 439 | |
| 440 | pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk); |
| 441 | |
| 442 | return walk.ret; |
| 443 | } |
| 444 | |
Alex Williamson | 188ad9d | 2016-02-22 16:02:36 -0700 | [diff] [blame] | 445 | static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev, |
| 446 | struct vfio_info_cap *caps) |
| 447 | { |
| 448 | struct vfio_info_cap_header *header; |
| 449 | struct vfio_region_info_cap_sparse_mmap *sparse; |
| 450 | size_t end, size; |
| 451 | int nr_areas = 2, i = 0; |
| 452 | |
| 453 | end = pci_resource_len(vdev->pdev, vdev->msix_bar); |
| 454 | |
| 455 | /* If MSI-X table is aligned to the start or end, only one area */ |
| 456 | if (((vdev->msix_offset & PAGE_MASK) == 0) || |
| 457 | (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end)) |
| 458 | nr_areas = 1; |
| 459 | |
| 460 | size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas)); |
| 461 | |
| 462 | header = vfio_info_cap_add(caps, size, |
| 463 | VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1); |
| 464 | if (IS_ERR(header)) |
| 465 | return PTR_ERR(header); |
| 466 | |
| 467 | sparse = container_of(header, |
| 468 | struct vfio_region_info_cap_sparse_mmap, header); |
| 469 | sparse->nr_areas = nr_areas; |
| 470 | |
| 471 | if (vdev->msix_offset & PAGE_MASK) { |
| 472 | sparse->areas[i].offset = 0; |
| 473 | sparse->areas[i].size = vdev->msix_offset & PAGE_MASK; |
| 474 | i++; |
| 475 | } |
| 476 | |
| 477 | if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) { |
| 478 | sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset + |
| 479 | vdev->msix_size); |
| 480 | sparse->areas[i].size = end - sparse->areas[i].offset; |
| 481 | i++; |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 487 | static int region_type_cap(struct vfio_pci_device *vdev, |
| 488 | struct vfio_info_cap *caps, |
| 489 | unsigned int type, unsigned int subtype) |
| 490 | { |
| 491 | struct vfio_info_cap_header *header; |
| 492 | struct vfio_region_info_cap_type *cap; |
| 493 | |
| 494 | header = vfio_info_cap_add(caps, sizeof(*cap), |
| 495 | VFIO_REGION_INFO_CAP_TYPE, 1); |
| 496 | if (IS_ERR(header)) |
| 497 | return PTR_ERR(header); |
| 498 | |
| 499 | cap = container_of(header, struct vfio_region_info_cap_type, header); |
| 500 | cap->type = type; |
| 501 | cap->subtype = subtype; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int vfio_pci_register_dev_region(struct vfio_pci_device *vdev, |
| 507 | unsigned int type, unsigned int subtype, |
| 508 | const struct vfio_pci_regops *ops, |
| 509 | size_t size, u32 flags, void *data) |
| 510 | { |
| 511 | struct vfio_pci_region *region; |
| 512 | |
| 513 | region = krealloc(vdev->region, |
| 514 | (vdev->num_regions + 1) * sizeof(*region), |
| 515 | GFP_KERNEL); |
| 516 | if (!region) |
| 517 | return -ENOMEM; |
| 518 | |
| 519 | vdev->region = region; |
| 520 | vdev->region[vdev->num_regions].type = type; |
| 521 | vdev->region[vdev->num_regions].subtype = subtype; |
| 522 | vdev->region[vdev->num_regions].ops = ops; |
| 523 | vdev->region[vdev->num_regions].size = size; |
| 524 | vdev->region[vdev->num_regions].flags = flags; |
| 525 | vdev->region[vdev->num_regions].data = data; |
| 526 | |
| 527 | vdev->num_regions++; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 532 | static long vfio_pci_ioctl(void *device_data, |
| 533 | unsigned int cmd, unsigned long arg) |
| 534 | { |
| 535 | struct vfio_pci_device *vdev = device_data; |
| 536 | unsigned long minsz; |
| 537 | |
| 538 | if (cmd == VFIO_DEVICE_GET_INFO) { |
| 539 | struct vfio_device_info info; |
| 540 | |
| 541 | minsz = offsetofend(struct vfio_device_info, num_irqs); |
| 542 | |
| 543 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 544 | return -EFAULT; |
| 545 | |
| 546 | if (info.argsz < minsz) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | info.flags = VFIO_DEVICE_FLAGS_PCI; |
| 550 | |
| 551 | if (vdev->reset_works) |
| 552 | info.flags |= VFIO_DEVICE_FLAGS_RESET; |
| 553 | |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 554 | info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 555 | info.num_irqs = VFIO_PCI_NUM_IRQS; |
| 556 | |
| 557 | return copy_to_user((void __user *)arg, &info, minsz); |
| 558 | |
| 559 | } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) { |
| 560 | struct pci_dev *pdev = vdev->pdev; |
| 561 | struct vfio_region_info info; |
Alex Williamson | 188ad9d | 2016-02-22 16:02:36 -0700 | [diff] [blame] | 562 | struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 563 | int i, ret; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 564 | |
| 565 | minsz = offsetofend(struct vfio_region_info, offset); |
| 566 | |
| 567 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 568 | return -EFAULT; |
| 569 | |
| 570 | if (info.argsz < minsz) |
| 571 | return -EINVAL; |
| 572 | |
| 573 | switch (info.index) { |
| 574 | case VFIO_PCI_CONFIG_REGION_INDEX: |
| 575 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 576 | info.size = pdev->cfg_size; |
| 577 | info.flags = VFIO_REGION_INFO_FLAG_READ | |
| 578 | VFIO_REGION_INFO_FLAG_WRITE; |
| 579 | break; |
| 580 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: |
| 581 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 582 | info.size = pci_resource_len(pdev, info.index); |
| 583 | if (!info.size) { |
| 584 | info.flags = 0; |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | info.flags = VFIO_REGION_INFO_FLAG_READ | |
| 589 | VFIO_REGION_INFO_FLAG_WRITE; |
Frank Blaschka | 1d53a3a | 2014-11-07 09:52:22 -0700 | [diff] [blame] | 590 | if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) && |
| 591 | pci_resource_flags(pdev, info.index) & |
Alex Williamson | 188ad9d | 2016-02-22 16:02:36 -0700 | [diff] [blame] | 592 | IORESOURCE_MEM && info.size >= PAGE_SIZE) { |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 593 | info.flags |= VFIO_REGION_INFO_FLAG_MMAP; |
Alex Williamson | 188ad9d | 2016-02-22 16:02:36 -0700 | [diff] [blame] | 594 | if (info.index == vdev->msix_bar) { |
| 595 | ret = msix_sparse_mmap_cap(vdev, &caps); |
| 596 | if (ret) |
| 597 | return ret; |
| 598 | } |
| 599 | } |
| 600 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 601 | break; |
| 602 | case VFIO_PCI_ROM_REGION_INDEX: |
| 603 | { |
| 604 | void __iomem *io; |
| 605 | size_t size; |
| 606 | |
| 607 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 608 | info.flags = 0; |
| 609 | |
| 610 | /* Report the BAR size, not the ROM size */ |
| 611 | info.size = pci_resource_len(pdev, info.index); |
| 612 | if (!info.size) |
| 613 | break; |
| 614 | |
| 615 | /* Is it really there? */ |
| 616 | io = pci_map_rom(pdev, &size); |
| 617 | if (!io || !size) { |
| 618 | info.size = 0; |
| 619 | break; |
| 620 | } |
| 621 | pci_unmap_rom(pdev, io); |
| 622 | |
| 623 | info.flags = VFIO_REGION_INFO_FLAG_READ; |
| 624 | break; |
| 625 | } |
Alex Williamson | 84237a8 | 2013-02-18 10:11:13 -0700 | [diff] [blame] | 626 | case VFIO_PCI_VGA_REGION_INDEX: |
| 627 | if (!vdev->has_vga) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 631 | info.size = 0xc0000; |
| 632 | info.flags = VFIO_REGION_INFO_FLAG_READ | |
| 633 | VFIO_REGION_INFO_FLAG_WRITE; |
| 634 | |
| 635 | break; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 636 | default: |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 637 | if (info.index >= |
| 638 | VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | i = info.index - VFIO_PCI_NUM_REGIONS; |
| 642 | |
| 643 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); |
| 644 | info.size = vdev->region[i].size; |
| 645 | info.flags = vdev->region[i].flags; |
| 646 | |
| 647 | ret = region_type_cap(vdev, &caps, |
| 648 | vdev->region[i].type, |
| 649 | vdev->region[i].subtype); |
| 650 | if (ret) |
| 651 | return ret; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 652 | } |
| 653 | |
Alex Williamson | 188ad9d | 2016-02-22 16:02:36 -0700 | [diff] [blame] | 654 | if (caps.size) { |
| 655 | info.flags |= VFIO_REGION_INFO_FLAG_CAPS; |
| 656 | if (info.argsz < sizeof(info) + caps.size) { |
| 657 | info.argsz = sizeof(info) + caps.size; |
| 658 | info.cap_offset = 0; |
| 659 | } else { |
| 660 | vfio_info_cap_shift(&caps, sizeof(info)); |
| 661 | ret = copy_to_user((void __user *)arg + |
| 662 | sizeof(info), caps.buf, |
| 663 | caps.size); |
| 664 | if (ret) { |
| 665 | kfree(caps.buf); |
| 666 | return ret; |
| 667 | } |
| 668 | info.cap_offset = sizeof(info); |
| 669 | } |
| 670 | |
| 671 | kfree(caps.buf); |
| 672 | } |
| 673 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 674 | return copy_to_user((void __user *)arg, &info, minsz); |
| 675 | |
| 676 | } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) { |
| 677 | struct vfio_irq_info info; |
| 678 | |
| 679 | minsz = offsetofend(struct vfio_irq_info, count); |
| 680 | |
| 681 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 682 | return -EFAULT; |
| 683 | |
| 684 | if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS) |
| 685 | return -EINVAL; |
| 686 | |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 687 | switch (info.index) { |
| 688 | case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX: |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 689 | case VFIO_PCI_REQ_IRQ_INDEX: |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 690 | break; |
| 691 | case VFIO_PCI_ERR_IRQ_INDEX: |
| 692 | if (pci_is_pcie(vdev->pdev)) |
| 693 | break; |
| 694 | /* pass thru to return error */ |
| 695 | default: |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 699 | info.flags = VFIO_IRQ_INFO_EVENTFD; |
| 700 | |
| 701 | info.count = vfio_pci_get_irq_count(vdev, info.index); |
| 702 | |
| 703 | if (info.index == VFIO_PCI_INTX_IRQ_INDEX) |
| 704 | info.flags |= (VFIO_IRQ_INFO_MASKABLE | |
| 705 | VFIO_IRQ_INFO_AUTOMASKED); |
| 706 | else |
| 707 | info.flags |= VFIO_IRQ_INFO_NORESIZE; |
| 708 | |
| 709 | return copy_to_user((void __user *)arg, &info, minsz); |
| 710 | |
| 711 | } else if (cmd == VFIO_DEVICE_SET_IRQS) { |
| 712 | struct vfio_irq_set hdr; |
| 713 | u8 *data = NULL; |
| 714 | int ret = 0; |
| 715 | |
| 716 | minsz = offsetofend(struct vfio_irq_set, count); |
| 717 | |
| 718 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) |
| 719 | return -EFAULT; |
| 720 | |
| 721 | if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS || |
| 722 | hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | |
| 723 | VFIO_IRQ_SET_ACTION_TYPE_MASK)) |
| 724 | return -EINVAL; |
| 725 | |
| 726 | if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) { |
| 727 | size_t size; |
Alex Williamson | 904c680 | 2013-03-26 11:33:16 -0600 | [diff] [blame] | 728 | int max = vfio_pci_get_irq_count(vdev, hdr.index); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 729 | |
| 730 | if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL) |
| 731 | size = sizeof(uint8_t); |
| 732 | else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD) |
| 733 | size = sizeof(int32_t); |
| 734 | else |
| 735 | return -EINVAL; |
| 736 | |
| 737 | if (hdr.argsz - minsz < hdr.count * size || |
Alex Williamson | 904c680 | 2013-03-26 11:33:16 -0600 | [diff] [blame] | 738 | hdr.start >= max || hdr.start + hdr.count > max) |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 739 | return -EINVAL; |
| 740 | |
Fengguang Wu | 3a1f704 | 2012-12-07 13:43:49 -0700 | [diff] [blame] | 741 | data = memdup_user((void __user *)(arg + minsz), |
| 742 | hdr.count * size); |
| 743 | if (IS_ERR(data)) |
| 744 | return PTR_ERR(data); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | mutex_lock(&vdev->igate); |
| 748 | |
| 749 | ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index, |
| 750 | hdr.start, hdr.count, data); |
| 751 | |
| 752 | mutex_unlock(&vdev->igate); |
| 753 | kfree(data); |
| 754 | |
| 755 | return ret; |
| 756 | |
Alex Williamson | 8b27ee6 | 2013-09-04 11:28:04 -0600 | [diff] [blame] | 757 | } else if (cmd == VFIO_DEVICE_RESET) { |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 758 | return vdev->reset_works ? |
Alex Williamson | 890ed57 | 2014-01-14 20:45:09 -0700 | [diff] [blame] | 759 | pci_try_reset_function(vdev->pdev) : -EINVAL; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 760 | |
Alex Williamson | 8b27ee6 | 2013-09-04 11:28:04 -0600 | [diff] [blame] | 761 | } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) { |
| 762 | struct vfio_pci_hot_reset_info hdr; |
| 763 | struct vfio_pci_fill_info fill = { 0 }; |
| 764 | struct vfio_pci_dependent_device *devices = NULL; |
| 765 | bool slot = false; |
| 766 | int ret = 0; |
| 767 | |
| 768 | minsz = offsetofend(struct vfio_pci_hot_reset_info, count); |
| 769 | |
| 770 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) |
| 771 | return -EFAULT; |
| 772 | |
| 773 | if (hdr.argsz < minsz) |
| 774 | return -EINVAL; |
| 775 | |
| 776 | hdr.flags = 0; |
| 777 | |
| 778 | /* Can we do a slot or bus reset or neither? */ |
| 779 | if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 780 | slot = true; |
| 781 | else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 782 | return -ENODEV; |
| 783 | |
| 784 | /* How many devices are affected? */ |
| 785 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 786 | vfio_pci_count_devs, |
| 787 | &fill.max, slot); |
| 788 | if (ret) |
| 789 | return ret; |
| 790 | |
| 791 | WARN_ON(!fill.max); /* Should always be at least one */ |
| 792 | |
| 793 | /* |
| 794 | * If there's enough space, fill it now, otherwise return |
| 795 | * -ENOSPC and the number of devices affected. |
| 796 | */ |
| 797 | if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) { |
| 798 | ret = -ENOSPC; |
| 799 | hdr.count = fill.max; |
| 800 | goto reset_info_exit; |
| 801 | } |
| 802 | |
| 803 | devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL); |
| 804 | if (!devices) |
| 805 | return -ENOMEM; |
| 806 | |
| 807 | fill.devices = devices; |
| 808 | |
| 809 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 810 | vfio_pci_fill_devs, |
| 811 | &fill, slot); |
| 812 | |
| 813 | /* |
| 814 | * If a device was removed between counting and filling, |
| 815 | * we may come up short of fill.max. If a device was |
| 816 | * added, we'll have a return of -EAGAIN above. |
| 817 | */ |
| 818 | if (!ret) |
| 819 | hdr.count = fill.cur; |
| 820 | |
| 821 | reset_info_exit: |
| 822 | if (copy_to_user((void __user *)arg, &hdr, minsz)) |
| 823 | ret = -EFAULT; |
| 824 | |
| 825 | if (!ret) { |
| 826 | if (copy_to_user((void __user *)(arg + minsz), devices, |
| 827 | hdr.count * sizeof(*devices))) |
| 828 | ret = -EFAULT; |
| 829 | } |
| 830 | |
| 831 | kfree(devices); |
| 832 | return ret; |
| 833 | |
| 834 | } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) { |
| 835 | struct vfio_pci_hot_reset hdr; |
| 836 | int32_t *group_fds; |
| 837 | struct vfio_pci_group_entry *groups; |
| 838 | struct vfio_pci_group_info info; |
| 839 | bool slot = false; |
| 840 | int i, count = 0, ret = 0; |
| 841 | |
| 842 | minsz = offsetofend(struct vfio_pci_hot_reset, count); |
| 843 | |
| 844 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) |
| 845 | return -EFAULT; |
| 846 | |
| 847 | if (hdr.argsz < minsz || hdr.flags) |
| 848 | return -EINVAL; |
| 849 | |
| 850 | /* Can we do a slot or bus reset or neither? */ |
| 851 | if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 852 | slot = true; |
| 853 | else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 854 | return -ENODEV; |
| 855 | |
| 856 | /* |
| 857 | * We can't let userspace give us an arbitrarily large |
| 858 | * buffer to copy, so verify how many we think there |
| 859 | * could be. Note groups can have multiple devices so |
| 860 | * one group per device is the max. |
| 861 | */ |
| 862 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 863 | vfio_pci_count_devs, |
| 864 | &count, slot); |
| 865 | if (ret) |
| 866 | return ret; |
| 867 | |
| 868 | /* Somewhere between 1 and count is OK */ |
| 869 | if (!hdr.count || hdr.count > count) |
| 870 | return -EINVAL; |
| 871 | |
| 872 | group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL); |
| 873 | groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL); |
| 874 | if (!group_fds || !groups) { |
| 875 | kfree(group_fds); |
| 876 | kfree(groups); |
| 877 | return -ENOMEM; |
| 878 | } |
| 879 | |
| 880 | if (copy_from_user(group_fds, (void __user *)(arg + minsz), |
| 881 | hdr.count * sizeof(*group_fds))) { |
| 882 | kfree(group_fds); |
| 883 | kfree(groups); |
| 884 | return -EFAULT; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * For each group_fd, get the group through the vfio external |
| 889 | * user interface and store the group and iommu ID. This |
| 890 | * ensures the group is held across the reset. |
| 891 | */ |
| 892 | for (i = 0; i < hdr.count; i++) { |
| 893 | struct vfio_group *group; |
| 894 | struct fd f = fdget(group_fds[i]); |
| 895 | if (!f.file) { |
| 896 | ret = -EBADF; |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | group = vfio_group_get_external_user(f.file); |
| 901 | fdput(f); |
| 902 | if (IS_ERR(group)) { |
| 903 | ret = PTR_ERR(group); |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | groups[i].group = group; |
| 908 | groups[i].id = vfio_external_user_iommu_id(group); |
| 909 | } |
| 910 | |
| 911 | kfree(group_fds); |
| 912 | |
| 913 | /* release reference to groups on error */ |
| 914 | if (ret) |
| 915 | goto hot_reset_release; |
| 916 | |
| 917 | info.count = hdr.count; |
| 918 | info.groups = groups; |
| 919 | |
| 920 | /* |
| 921 | * Test whether all the affected devices are contained |
| 922 | * by the set of groups provided by the user. |
| 923 | */ |
| 924 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 925 | vfio_pci_validate_devs, |
| 926 | &info, slot); |
| 927 | if (!ret) |
| 928 | /* User has access, do the reset */ |
Alex Williamson | 890ed57 | 2014-01-14 20:45:09 -0700 | [diff] [blame] | 929 | ret = slot ? pci_try_reset_slot(vdev->pdev->slot) : |
| 930 | pci_try_reset_bus(vdev->pdev->bus); |
Alex Williamson | 8b27ee6 | 2013-09-04 11:28:04 -0600 | [diff] [blame] | 931 | |
| 932 | hot_reset_release: |
| 933 | for (i--; i >= 0; i--) |
| 934 | vfio_group_put_external_user(groups[i].group); |
| 935 | |
| 936 | kfree(groups); |
| 937 | return ret; |
| 938 | } |
| 939 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 940 | return -ENOTTY; |
| 941 | } |
| 942 | |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 943 | static ssize_t vfio_pci_rw(void *device_data, char __user *buf, |
| 944 | size_t count, loff_t *ppos, bool iswrite) |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 945 | { |
| 946 | unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); |
| 947 | struct vfio_pci_device *vdev = device_data; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 948 | |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 949 | if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 950 | return -EINVAL; |
| 951 | |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 952 | switch (index) { |
| 953 | case VFIO_PCI_CONFIG_REGION_INDEX: |
Alex Williamson | 906ee99 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 954 | return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); |
| 955 | |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 956 | case VFIO_PCI_ROM_REGION_INDEX: |
| 957 | if (iswrite) |
| 958 | return -EINVAL; |
Alex Williamson | 906ee99 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 959 | return vfio_pci_bar_rw(vdev, buf, count, ppos, false); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 960 | |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 961 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: |
Alex Williamson | 906ee99 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 962 | return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); |
Alex Williamson | 84237a8 | 2013-02-18 10:11:13 -0700 | [diff] [blame] | 963 | |
| 964 | case VFIO_PCI_VGA_REGION_INDEX: |
| 965 | return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 966 | default: |
| 967 | index -= VFIO_PCI_NUM_REGIONS; |
| 968 | return vdev->region[index].ops->rw(vdev, buf, |
| 969 | count, ppos, iswrite); |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 972 | return -EINVAL; |
| 973 | } |
| 974 | |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 975 | static ssize_t vfio_pci_read(void *device_data, char __user *buf, |
| 976 | size_t count, loff_t *ppos) |
| 977 | { |
Alex Williamson | 906ee99 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 978 | if (!count) |
| 979 | return 0; |
| 980 | |
Alex Williamson | 5b279a1 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 981 | return vfio_pci_rw(device_data, buf, count, ppos, false); |
| 982 | } |
| 983 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 984 | static ssize_t vfio_pci_write(void *device_data, const char __user *buf, |
| 985 | size_t count, loff_t *ppos) |
| 986 | { |
Alex Williamson | 906ee99 | 2013-02-14 14:02:12 -0700 | [diff] [blame] | 987 | if (!count) |
| 988 | return 0; |
| 989 | |
| 990 | return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma) |
| 994 | { |
| 995 | struct vfio_pci_device *vdev = device_data; |
| 996 | struct pci_dev *pdev = vdev->pdev; |
| 997 | unsigned int index; |
Alex Williamson | 34002f5 | 2012-10-10 09:10:31 -0600 | [diff] [blame] | 998 | u64 phys_len, req_len, pgoff, req_start; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 999 | int ret; |
| 1000 | |
| 1001 | index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); |
| 1002 | |
| 1003 | if (vma->vm_end < vma->vm_start) |
| 1004 | return -EINVAL; |
| 1005 | if ((vma->vm_flags & VM_SHARED) == 0) |
| 1006 | return -EINVAL; |
| 1007 | if (index >= VFIO_PCI_ROM_REGION_INDEX) |
| 1008 | return -EINVAL; |
| 1009 | if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM)) |
| 1010 | return -EINVAL; |
| 1011 | |
| 1012 | phys_len = pci_resource_len(pdev, index); |
| 1013 | req_len = vma->vm_end - vma->vm_start; |
| 1014 | pgoff = vma->vm_pgoff & |
| 1015 | ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); |
| 1016 | req_start = pgoff << PAGE_SHIFT; |
| 1017 | |
| 1018 | if (phys_len < PAGE_SIZE || req_start + req_len > phys_len) |
| 1019 | return -EINVAL; |
| 1020 | |
| 1021 | if (index == vdev->msix_bar) { |
| 1022 | /* |
| 1023 | * Disallow mmaps overlapping the MSI-X table; users don't |
| 1024 | * get to touch this directly. We could find somewhere |
| 1025 | * else to map the overlap, but page granularity is only |
| 1026 | * a recommendation, not a requirement, so the user needs |
| 1027 | * to know which bits are real. Requiring them to mmap |
| 1028 | * around the table makes that clear. |
| 1029 | */ |
| 1030 | |
| 1031 | /* If neither entirely above nor below, then it overlaps */ |
| 1032 | if (!(req_start >= vdev->msix_offset + vdev->msix_size || |
| 1033 | req_start + req_len <= vdev->msix_offset)) |
| 1034 | return -EINVAL; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Even though we don't make use of the barmap for the mmap, |
| 1039 | * we need to request the region and the barmap tracks that. |
| 1040 | */ |
| 1041 | if (!vdev->barmap[index]) { |
| 1042 | ret = pci_request_selected_regions(pdev, |
| 1043 | 1 << index, "vfio-pci"); |
| 1044 | if (ret) |
| 1045 | return ret; |
| 1046 | |
| 1047 | vdev->barmap[index] = pci_iomap(pdev, index, 0); |
| 1048 | } |
| 1049 | |
| 1050 | vma->vm_private_data = vdev; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1051 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
Alex Williamson | 34002f5 | 2012-10-10 09:10:31 -0600 | [diff] [blame] | 1052 | vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1053 | |
Alex Williamson | 34002f5 | 2012-10-10 09:10:31 -0600 | [diff] [blame] | 1054 | return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1055 | req_len, vma->vm_page_prot); |
| 1056 | } |
| 1057 | |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 1058 | static void vfio_pci_request(void *device_data, unsigned int count) |
| 1059 | { |
| 1060 | struct vfio_pci_device *vdev = device_data; |
| 1061 | |
| 1062 | mutex_lock(&vdev->igate); |
| 1063 | |
| 1064 | if (vdev->req_trigger) { |
Alex Williamson | 5f55d2a | 2015-04-28 10:23:30 -0600 | [diff] [blame] | 1065 | if (!(count % 10)) |
| 1066 | dev_notice_ratelimited(&vdev->pdev->dev, |
| 1067 | "Relaying device request to user (#%u)\n", |
| 1068 | count); |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 1069 | eventfd_signal(vdev->req_trigger, 1); |
Alex Williamson | 5f55d2a | 2015-04-28 10:23:30 -0600 | [diff] [blame] | 1070 | } else if (count == 0) { |
| 1071 | dev_warn(&vdev->pdev->dev, |
| 1072 | "No device request channel registered, blocked until released by user\n"); |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | mutex_unlock(&vdev->igate); |
| 1076 | } |
| 1077 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1078 | static const struct vfio_device_ops vfio_pci_ops = { |
| 1079 | .name = "vfio-pci", |
| 1080 | .open = vfio_pci_open, |
| 1081 | .release = vfio_pci_release, |
| 1082 | .ioctl = vfio_pci_ioctl, |
| 1083 | .read = vfio_pci_read, |
| 1084 | .write = vfio_pci_write, |
| 1085 | .mmap = vfio_pci_mmap, |
Alex Williamson | 6140a8f | 2015-02-06 15:05:08 -0700 | [diff] [blame] | 1086 | .request = vfio_pci_request, |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1087 | }; |
| 1088 | |
| 1089 | static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 1090 | { |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1091 | struct vfio_pci_device *vdev; |
| 1092 | struct iommu_group *group; |
| 1093 | int ret; |
| 1094 | |
Wei Yang | 7c2e211 | 2015-01-07 10:29:11 -0700 | [diff] [blame] | 1095 | if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1096 | return -EINVAL; |
| 1097 | |
Alex Williamson | 03a76b6 | 2015-12-21 15:13:33 -0700 | [diff] [blame] | 1098 | group = vfio_iommu_group_get(&pdev->dev); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1099 | if (!group) |
| 1100 | return -EINVAL; |
| 1101 | |
| 1102 | vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); |
| 1103 | if (!vdev) { |
Alex Williamson | 03a76b6 | 2015-12-21 15:13:33 -0700 | [diff] [blame] | 1104 | vfio_iommu_group_put(group, &pdev->dev); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1105 | return -ENOMEM; |
| 1106 | } |
| 1107 | |
| 1108 | vdev->pdev = pdev; |
| 1109 | vdev->irq_type = VFIO_PCI_NUM_IRQS; |
| 1110 | mutex_init(&vdev->igate); |
| 1111 | spin_lock_init(&vdev->irqlock); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1112 | |
| 1113 | ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev); |
| 1114 | if (ret) { |
Alex Williamson | 03a76b6 | 2015-12-21 15:13:33 -0700 | [diff] [blame] | 1115 | vfio_iommu_group_put(group, &pdev->dev); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1116 | kfree(vdev); |
Alex Williamson | 5a0ff17 | 2015-04-08 08:11:51 -0600 | [diff] [blame] | 1117 | return ret; |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1118 | } |
| 1119 | |
Alex Williamson | ecaa1f6 | 2015-04-07 11:14:41 -0600 | [diff] [blame] | 1120 | if (vfio_pci_is_vga(pdev)) { |
| 1121 | vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode); |
| 1122 | vga_set_legacy_decoding(pdev, |
| 1123 | vfio_pci_set_vga_decode(vdev, false)); |
| 1124 | } |
| 1125 | |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 1126 | if (!disable_idle_d3) { |
| 1127 | /* |
| 1128 | * pci-core sets the device power state to an unknown value at |
| 1129 | * bootup and after being removed from a driver. The only |
| 1130 | * transition it allows from this unknown state is to D0, which |
| 1131 | * typically happens when a driver calls pci_enable_device(). |
| 1132 | * We're not ready to enable the device yet, but we do want to |
| 1133 | * be able to get to D3. Therefore first do a D0 transition |
| 1134 | * before going to D3. |
| 1135 | */ |
| 1136 | pci_set_power_state(pdev, PCI_D0); |
| 1137 | pci_set_power_state(pdev, PCI_D3hot); |
| 1138 | } |
| 1139 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1140 | return ret; |
| 1141 | } |
| 1142 | |
| 1143 | static void vfio_pci_remove(struct pci_dev *pdev) |
| 1144 | { |
| 1145 | struct vfio_pci_device *vdev; |
| 1146 | |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 1147 | vdev = vfio_del_group_dev(&pdev->dev); |
Alex Williamson | ecaa1f6 | 2015-04-07 11:14:41 -0600 | [diff] [blame] | 1148 | if (!vdev) |
| 1149 | return; |
| 1150 | |
Alex Williamson | 03a76b6 | 2015-12-21 15:13:33 -0700 | [diff] [blame] | 1151 | vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev); |
Alex Williamson | 28541d4 | 2016-02-22 16:02:39 -0700 | [diff] [blame] | 1152 | kfree(vdev->region); |
Alex Williamson | ecaa1f6 | 2015-04-07 11:14:41 -0600 | [diff] [blame] | 1153 | kfree(vdev); |
| 1154 | |
| 1155 | if (vfio_pci_is_vga(pdev)) { |
| 1156 | vga_client_register(pdev, NULL, NULL, NULL); |
| 1157 | vga_set_legacy_decoding(pdev, |
| 1158 | VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | |
| 1159 | VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM); |
Alex Williamson | 61d7925 | 2014-08-07 11:12:04 -0600 | [diff] [blame] | 1160 | } |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 1161 | |
| 1162 | if (!disable_idle_d3) |
| 1163 | pci_set_power_state(pdev, PCI_D0); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1164 | } |
| 1165 | |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 1166 | static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev, |
| 1167 | pci_channel_state_t state) |
| 1168 | { |
| 1169 | struct vfio_pci_device *vdev; |
| 1170 | struct vfio_device *device; |
| 1171 | |
| 1172 | device = vfio_device_get_from_dev(&pdev->dev); |
| 1173 | if (device == NULL) |
| 1174 | return PCI_ERS_RESULT_DISCONNECT; |
| 1175 | |
| 1176 | vdev = vfio_device_data(device); |
| 1177 | if (vdev == NULL) { |
| 1178 | vfio_device_put(device); |
| 1179 | return PCI_ERS_RESULT_DISCONNECT; |
| 1180 | } |
| 1181 | |
Alex Williamson | 3be3a07 | 2014-01-14 16:12:55 -0700 | [diff] [blame] | 1182 | mutex_lock(&vdev->igate); |
| 1183 | |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 1184 | if (vdev->err_trigger) |
| 1185 | eventfd_signal(vdev->err_trigger, 1); |
| 1186 | |
Alex Williamson | 3be3a07 | 2014-01-14 16:12:55 -0700 | [diff] [blame] | 1187 | mutex_unlock(&vdev->igate); |
| 1188 | |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 1189 | vfio_device_put(device); |
| 1190 | |
| 1191 | return PCI_ERS_RESULT_CAN_RECOVER; |
| 1192 | } |
| 1193 | |
Julia Lawall | 7d10f4e | 2015-11-14 11:07:01 +0100 | [diff] [blame] | 1194 | static const struct pci_error_handlers vfio_err_handlers = { |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 1195 | .error_detected = vfio_pci_aer_err_detected, |
| 1196 | }; |
| 1197 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1198 | static struct pci_driver vfio_pci_driver = { |
| 1199 | .name = "vfio-pci", |
| 1200 | .id_table = NULL, /* only dynamic ids */ |
| 1201 | .probe = vfio_pci_probe, |
| 1202 | .remove = vfio_pci_remove, |
Vijay Mohan Pandarathil | dad9f89 | 2013-03-11 09:31:22 -0600 | [diff] [blame] | 1203 | .err_handler = &vfio_err_handlers, |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1204 | }; |
| 1205 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1206 | struct vfio_devices { |
| 1207 | struct vfio_device **devices; |
| 1208 | int cur_index; |
| 1209 | int max_index; |
| 1210 | }; |
| 1211 | |
| 1212 | static int vfio_pci_get_devs(struct pci_dev *pdev, void *data) |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1213 | { |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1214 | struct vfio_devices *devs = data; |
Alex Williamson | 20f3001 | 2015-06-09 10:08:57 -0600 | [diff] [blame] | 1215 | struct vfio_device *device; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1216 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1217 | if (devs->cur_index == devs->max_index) |
| 1218 | return -ENOSPC; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1219 | |
Alex Williamson | 20f3001 | 2015-06-09 10:08:57 -0600 | [diff] [blame] | 1220 | device = vfio_device_get_from_dev(&pdev->dev); |
| 1221 | if (!device) |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1222 | return -EINVAL; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1223 | |
Alex Williamson | 20f3001 | 2015-06-09 10:08:57 -0600 | [diff] [blame] | 1224 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { |
| 1225 | vfio_device_put(device); |
| 1226 | return -EBUSY; |
| 1227 | } |
| 1228 | |
| 1229 | devs->devices[devs->cur_index++] = device; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Attempt to do a bus/slot reset if there are devices affected by a reset for |
| 1235 | * this device that are needs_reset and all of the affected devices are unused |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1236 | * (!refcnt). Callers are required to hold driver_lock when calling this to |
| 1237 | * prevent device opens and concurrent bus reset attempts. We prevent device |
| 1238 | * unbinds by acquiring and holding a reference to the vfio_device. |
| 1239 | * |
| 1240 | * NB: vfio-core considers a group to be viable even if some devices are |
| 1241 | * bound to drivers like pci-stub or pcieport. Here we require all devices |
| 1242 | * to be bound to vfio_pci since that's the only way we can be sure they |
| 1243 | * stay put. |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1244 | */ |
| 1245 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev) |
| 1246 | { |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1247 | struct vfio_devices devs = { .cur_index = 0 }; |
| 1248 | int i = 0, ret = -EINVAL; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1249 | bool needs_reset = false, slot = false; |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1250 | struct vfio_pci_device *tmp; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1251 | |
| 1252 | if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 1253 | slot = true; |
| 1254 | else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 1255 | return; |
| 1256 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1257 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs, |
| 1258 | &i, slot) || !i) |
| 1259 | return; |
| 1260 | |
| 1261 | devs.max_index = i; |
| 1262 | devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL); |
| 1263 | if (!devs.devices) |
| 1264 | return; |
| 1265 | |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1266 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1267 | vfio_pci_get_devs, &devs, slot)) |
| 1268 | goto put_devs; |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1269 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1270 | for (i = 0; i < devs.cur_index; i++) { |
| 1271 | tmp = vfio_device_data(devs.devices[i]); |
| 1272 | if (tmp->needs_reset) |
| 1273 | needs_reset = true; |
| 1274 | if (tmp->refcnt) |
| 1275 | goto put_devs; |
| 1276 | } |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1277 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1278 | if (needs_reset) |
| 1279 | ret = slot ? pci_try_reset_slot(vdev->pdev->slot) : |
| 1280 | pci_try_reset_bus(vdev->pdev->bus); |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1281 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1282 | put_devs: |
| 1283 | for (i = 0; i < devs.cur_index; i++) { |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 1284 | tmp = vfio_device_data(devs.devices[i]); |
| 1285 | if (!ret) |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1286 | tmp->needs_reset = false; |
Alex Williamson | 6eb7018 | 2015-04-07 11:14:46 -0600 | [diff] [blame] | 1287 | |
| 1288 | if (!tmp->refcnt && !disable_idle_d3) |
| 1289 | pci_set_power_state(tmp->pdev, PCI_D3hot); |
| 1290 | |
Alex Williamson | 93899a6 | 2014-09-29 17:18:39 -0600 | [diff] [blame] | 1291 | vfio_device_put(devs.devices[i]); |
| 1292 | } |
| 1293 | |
| 1294 | kfree(devs.devices); |
Alex Williamson | bc4fba7 | 2014-08-07 11:12:07 -0600 | [diff] [blame] | 1295 | } |
| 1296 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1297 | static void __exit vfio_pci_cleanup(void) |
| 1298 | { |
| 1299 | pci_unregister_driver(&vfio_pci_driver); |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1300 | vfio_pci_uninit_perm_bits(); |
| 1301 | } |
| 1302 | |
Alex Williamson | 80c7e8c | 2015-04-07 11:14:43 -0600 | [diff] [blame] | 1303 | static void __init vfio_pci_fill_ids(void) |
| 1304 | { |
| 1305 | char *p, *id; |
| 1306 | int rc; |
| 1307 | |
| 1308 | /* no ids passed actually */ |
| 1309 | if (ids[0] == '\0') |
| 1310 | return; |
| 1311 | |
| 1312 | /* add ids specified in the module parameter */ |
| 1313 | p = ids; |
| 1314 | while ((id = strsep(&p, ","))) { |
| 1315 | unsigned int vendor, device, subvendor = PCI_ANY_ID, |
| 1316 | subdevice = PCI_ANY_ID, class = 0, class_mask = 0; |
| 1317 | int fields; |
| 1318 | |
| 1319 | if (!strlen(id)) |
| 1320 | continue; |
| 1321 | |
| 1322 | fields = sscanf(id, "%x:%x:%x:%x:%x:%x", |
| 1323 | &vendor, &device, &subvendor, &subdevice, |
| 1324 | &class, &class_mask); |
| 1325 | |
| 1326 | if (fields < 2) { |
| 1327 | pr_warn("invalid id string \"%s\"\n", id); |
| 1328 | continue; |
| 1329 | } |
| 1330 | |
| 1331 | rc = pci_add_dynid(&vfio_pci_driver, vendor, device, |
| 1332 | subvendor, subdevice, class, class_mask, 0); |
| 1333 | if (rc) |
| 1334 | pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n", |
| 1335 | vendor, device, subvendor, subdevice, |
| 1336 | class, class_mask, rc); |
| 1337 | else |
| 1338 | pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n", |
| 1339 | vendor, device, subvendor, subdevice, |
| 1340 | class, class_mask); |
| 1341 | } |
| 1342 | } |
| 1343 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1344 | static int __init vfio_pci_init(void) |
| 1345 | { |
| 1346 | int ret; |
| 1347 | |
| 1348 | /* Allocate shared config space permision data used by all devices */ |
| 1349 | ret = vfio_pci_init_perm_bits(); |
| 1350 | if (ret) |
| 1351 | return ret; |
| 1352 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1353 | /* Register and scan for devices */ |
| 1354 | ret = pci_register_driver(&vfio_pci_driver); |
| 1355 | if (ret) |
| 1356 | goto out_driver; |
| 1357 | |
Alex Williamson | 80c7e8c | 2015-04-07 11:14:43 -0600 | [diff] [blame] | 1358 | vfio_pci_fill_ids(); |
| 1359 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1360 | return 0; |
| 1361 | |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1362 | out_driver: |
Alex Williamson | 89e1f7d | 2012-07-31 08:16:24 -0600 | [diff] [blame] | 1363 | vfio_pci_uninit_perm_bits(); |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
| 1367 | module_init(vfio_pci_init); |
| 1368 | module_exit(vfio_pci_cleanup); |
| 1369 | |
| 1370 | MODULE_VERSION(DRIVER_VERSION); |
| 1371 | MODULE_LICENSE("GPL v2"); |
| 1372 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1373 | MODULE_DESCRIPTION(DRIVER_DESC); |