blob: 74a3752974945147c4c2f89dfd4dae7d7701a58c [file] [log] [blame]
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001/*
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 Williamson80c7e8c2015-04-07 11:14:43 -060014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Alex Williamson89e1f7d2012-07-31 08:16:24 -060016#include <linux/device.h>
17#include <linux/eventfd.h>
Alex Williamson8b27ee62013-09-04 11:28:04 -060018#include <linux/file.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060019#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 Williamsonecaa1f62015-04-07 11:14:41 -060030#include <linux/vgaarb.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060031
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 Williamson80c7e8c2015-04-07 11:14:43 -060038static char ids[1024] __initdata;
39module_param_string(ids, ids, sizeof(ids), 0);
40MODULE_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 Williamson89e1f7d2012-07-31 08:16:24 -060042static bool nointxmask;
43module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
44MODULE_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 Williamson88c0dead2015-04-07 11:14:40 -060047#ifdef CONFIG_VFIO_PCI_VGA
48static bool disable_vga;
49module_param(disable_vga, bool, S_IRUGO);
50MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
51#endif
52
Alex Williamson6eb70182015-04-07 11:14:46 -060053static bool disable_idle_d3;
54module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(disable_idle_d3,
56 "Disable using the PCI D3 low power state for idle, unused devices");
57
Alex Williamson61d79252014-08-07 11:12:04 -060058static DEFINE_MUTEX(driver_lock);
59
Alex Williamson88c0dead2015-04-07 11:14:40 -060060static 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 Williamsonecaa1f62015-04-07 11:14:41 -060069/*
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 */
77static 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
108static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
109{
110 return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
111}
112
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600113static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
Alex Williamsonf572a962016-02-22 16:02:45 -0700114static void vfio_pci_disable(struct vfio_pci_device *vdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600115
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600116static 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 Williamson6eb70182015-04-07 11:14:46 -0600123 pci_set_power_state(pdev, PCI_D0);
124
Alex Williamson9c22e662014-08-07 11:12:02 -0600125 /* Don't allow our initial saved state to include busmaster */
126 pci_clear_master(pdev);
127
Alex Williamson9a92c502012-12-07 13:43:51 -0700128 ret = pci_enable_device(pdev);
129 if (ret)
130 return ret;
131
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600132 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 Williamson9a92c502012-12-07 13:43:51 -0700140 if (ret) {
Alex Williamsoneb5685f2014-05-30 11:35:53 -0600141 kfree(vdev->pci_saved_state);
142 vdev->pci_saved_state = NULL;
Alex Williamson9a92c502012-12-07 13:43:51 -0700143 pci_disable_device(pdev);
144 return ret;
145 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600146
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 Helgaasa9047f22013-04-18 15:12:58 -0600156 msix_pos = pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600157 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 Helgaas508d1aa2013-04-18 12:42:58 -0600164 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
165 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600166 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
167 } else
168 vdev->msix_bar = 0xFF;
169
Alex Williamsonecaa1f62015-04-07 11:14:41 -0600170 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
Alex Williamson84237a82013-02-18 10:11:13 -0700171 vdev->has_vga = true;
Alex Williamson84237a82013-02-18 10:11:13 -0700172
Alex Williamson5846ff52016-02-22 16:02:43 -0700173
Alex Williamsonf572a962016-02-22 16:02:45 -0700174 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 Williamson5846ff52016-02-22 16:02:43 -0700184 }
185
Alex Williamson9a92c502012-12-07 13:43:51 -0700186 return 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600187}
188
189static void vfio_pci_disable(struct vfio_pci_device *vdev)
190{
Alex Williamson20077222012-12-07 13:43:50 -0700191 struct pci_dev *pdev = vdev->pdev;
Alex Williamson28541d42016-02-22 16:02:39 -0700192 int i, bar;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600193
Alex Williamson9c22e662014-08-07 11:12:02 -0600194 /* Stop the device from further DMA */
195 pci_clear_master(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600196
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 Williamson28541d42016-02-22 16:02:39 -0700203 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 Williamson89e1f7d2012-07-31 08:16:24 -0600210 vfio_config_free(vdev);
211
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600212 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
213 if (!vdev->barmap[bar])
214 continue;
Alex Williamson20077222012-12-07 13:43:50 -0700215 pci_iounmap(pdev, vdev->barmap[bar]);
216 pci_release_selected_regions(pdev, 1 << bar);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600217 vdev->barmap[bar] = NULL;
218 }
Alex Williamson20077222012-12-07 13:43:50 -0700219
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600220 vdev->needs_reset = true;
221
Alex Williamson20077222012-12-07 13:43:50 -0700222 /*
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 Williamson9c22e662014-08-07 11:12:02 -0600233 goto out;
Alex Williamson20077222012-12-07 13:43:50 -0700234
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 Williamsond24cdbf2013-06-10 16:40:57 -0600244 /*
Alex Williamson890ed572014-01-14 20:45:09 -0700245 * 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 Williamsond24cdbf2013-06-10 16:40:57 -0600247 */
Alex Williamson561d72d2015-04-07 11:14:44 -0600248 if (vdev->reset_works && !pci_try_reset_function(pdev))
249 vdev->needs_reset = false;
Alex Williamson20077222012-12-07 13:43:50 -0700250
251 pci_restore_state(pdev);
Alex Williamson9c22e662014-08-07 11:12:02 -0600252out:
253 pci_disable_device(pdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600254
255 vfio_pci_try_bus_reset(vdev);
Alex Williamson6eb70182015-04-07 11:14:46 -0600256
257 if (!disable_idle_d3)
258 pci_set_power_state(pdev, PCI_D3hot);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600259}
260
261static void vfio_pci_release(void *device_data)
262{
263 struct vfio_pci_device *vdev = device_data;
264
Alex Williamson61d79252014-08-07 11:12:04 -0600265 mutex_lock(&driver_lock);
266
267 if (!(--vdev->refcnt)) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000268 vfio_spapr_pci_eeh_release(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600269 vfio_pci_disable(vdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000270 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600271
Alex Williamson61d79252014-08-07 11:12:04 -0600272 mutex_unlock(&driver_lock);
273
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600274 module_put(THIS_MODULE);
275}
276
277static int vfio_pci_open(void *device_data)
278{
279 struct vfio_pci_device *vdev = device_data;
Alex Williamson61d79252014-08-07 11:12:04 -0600280 int ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600281
282 if (!try_module_get(THIS_MODULE))
283 return -ENODEV;
284
Alex Williamson61d79252014-08-07 11:12:04 -0600285 mutex_lock(&driver_lock);
286
287 if (!vdev->refcnt) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000288 ret = vfio_pci_enable(vdev);
289 if (ret)
290 goto error;
291
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600292 vfio_spapr_pci_eeh_open(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600293 }
Alex Williamson61d79252014-08-07 11:12:04 -0600294 vdev->refcnt++;
Gavin Shan1b69be52014-06-10 11:41:57 +1000295error:
Alex Williamson61d79252014-08-07 11:12:04 -0600296 mutex_unlock(&driver_lock);
297 if (ret)
298 module_put(THIS_MODULE);
Gavin Shan1b69be52014-06-10 11:41:57 +1000299 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600300}
301
302static 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 Blaschka1d53a3a2014-11-07 09:52:22 -0700307 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600308 return 1;
309
310 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
311 u8 pos;
312 u16 flags;
313
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600314 pos = vdev->pdev->msi_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600315 if (pos) {
316 pci_read_config_word(vdev->pdev,
317 pos + PCI_MSI_FLAGS, &flags);
Gavin Shanfd49c81f2014-05-30 11:35:54 -0600318 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600319 }
320 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
321 u8 pos;
322 u16 flags;
323
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600324 pos = vdev->pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600325 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 Williamson6140a8f2015-02-06 15:05:08 -0700331 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600332 if (pci_is_pcie(vdev->pdev))
333 return 1;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700334 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
335 return 1;
336 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600337
338 return 0;
339}
340
Alex Williamson8b27ee62013-09-04 11:28:04 -0600341static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
342{
343 (*(int *)data)++;
344 return 0;
345}
346
347struct vfio_pci_fill_info {
348 int max;
349 int cur;
350 struct vfio_pci_dependent_device *devices;
351};
352
353static 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
374struct vfio_pci_group_entry {
375 struct vfio_group *group;
376 int id;
377};
378
379struct vfio_pci_group_info {
380 int count;
381 struct vfio_pci_group_entry *groups;
382};
383
384static 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
405static 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
413struct 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
421static 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
431static 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 Williamson188ad9d2016-02-22 16:02:36 -0700445static 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 Williamson28541d42016-02-22 16:02:39 -0700487static 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
506int 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 Williamson89e1f7d2012-07-31 08:16:24 -0600532static 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 Williamson28541d42016-02-22 16:02:39 -0700554 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600555 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 Williamson188ad9d2016-02-22 16:02:36 -0700562 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
Alex Williamson28541d42016-02-22 16:02:39 -0700563 int i, ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600564
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 Blaschka1d53a3a2014-11-07 09:52:22 -0700590 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
591 pci_resource_flags(pdev, info.index) &
Alex Williamson188ad9d2016-02-22 16:02:36 -0700592 IORESOURCE_MEM && info.size >= PAGE_SIZE) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600593 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700594 if (info.index == vdev->msix_bar) {
595 ret = msix_sparse_mmap_cap(vdev, &caps);
596 if (ret)
597 return ret;
598 }
599 }
600
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600601 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 Williamson84237a82013-02-18 10:11:13 -0700626 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 Williamson89e1f7d2012-07-31 08:16:24 -0600636 default:
Alex Williamson28541d42016-02-22 16:02:39 -0700637 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 Williamson89e1f7d2012-07-31 08:16:24 -0600652 }
653
Alex Williamson188ad9d2016-02-22 16:02:36 -0700654 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 Williamson89e1f7d2012-07-31 08:16:24 -0600674 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 Pandarathildad9f892013-03-11 09:31:22 -0600687 switch (info.index) {
688 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
Alex Williamson6140a8f2015-02-06 15:05:08 -0700689 case VFIO_PCI_REQ_IRQ_INDEX:
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600690 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 Williamson89e1f7d2012-07-31 08:16:24 -0600699 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 Williamson904c6802013-03-26 11:33:16 -0600728 int max = vfio_pci_get_irq_count(vdev, hdr.index);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600729
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 Williamson904c6802013-03-26 11:33:16 -0600738 hdr.start >= max || hdr.start + hdr.count > max)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600739 return -EINVAL;
740
Fengguang Wu3a1f7042012-12-07 13:43:49 -0700741 data = memdup_user((void __user *)(arg + minsz),
742 hdr.count * size);
743 if (IS_ERR(data))
744 return PTR_ERR(data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600745 }
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 Williamson8b27ee62013-09-04 11:28:04 -0600757 } else if (cmd == VFIO_DEVICE_RESET) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600758 return vdev->reset_works ?
Alex Williamson890ed572014-01-14 20:45:09 -0700759 pci_try_reset_function(vdev->pdev) : -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600760
Alex Williamson8b27ee62013-09-04 11:28:04 -0600761 } 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
821reset_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 Williamson890ed572014-01-14 20:45:09 -0700929 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
930 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamson8b27ee62013-09-04 11:28:04 -0600931
932hot_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 Williamson89e1f7d2012-07-31 08:16:24 -0600940 return -ENOTTY;
941}
942
Alex Williamson5b279a12013-02-14 14:02:12 -0700943static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
944 size_t count, loff_t *ppos, bool iswrite)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600945{
946 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
947 struct vfio_pci_device *vdev = device_data;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600948
Alex Williamson28541d42016-02-22 16:02:39 -0700949 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600950 return -EINVAL;
951
Alex Williamson5b279a12013-02-14 14:02:12 -0700952 switch (index) {
953 case VFIO_PCI_CONFIG_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700954 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
955
Alex Williamson5b279a12013-02-14 14:02:12 -0700956 case VFIO_PCI_ROM_REGION_INDEX:
957 if (iswrite)
958 return -EINVAL;
Alex Williamson906ee992013-02-14 14:02:12 -0700959 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600960
Alex Williamson5b279a12013-02-14 14:02:12 -0700961 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700962 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson84237a82013-02-18 10:11:13 -0700963
964 case VFIO_PCI_VGA_REGION_INDEX:
965 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson28541d42016-02-22 16:02:39 -0700966 default:
967 index -= VFIO_PCI_NUM_REGIONS;
968 return vdev->region[index].ops->rw(vdev, buf,
969 count, ppos, iswrite);
Alex Williamson5b279a12013-02-14 14:02:12 -0700970 }
971
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600972 return -EINVAL;
973}
974
Alex Williamson5b279a12013-02-14 14:02:12 -0700975static ssize_t vfio_pci_read(void *device_data, char __user *buf,
976 size_t count, loff_t *ppos)
977{
Alex Williamson906ee992013-02-14 14:02:12 -0700978 if (!count)
979 return 0;
980
Alex Williamson5b279a12013-02-14 14:02:12 -0700981 return vfio_pci_rw(device_data, buf, count, ppos, false);
982}
983
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600984static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
985 size_t count, loff_t *ppos)
986{
Alex Williamson906ee992013-02-14 14:02:12 -0700987 if (!count)
988 return 0;
989
990 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600991}
992
993static 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 Williamson34002f52012-10-10 09:10:31 -0600998 u64 phys_len, req_len, pgoff, req_start;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600999 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 Williamson89e1f7d2012-07-31 08:16:24 -06001051 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Alex Williamson34002f52012-10-10 09:10:31 -06001052 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001053
Alex Williamson34002f52012-10-10 09:10:31 -06001054 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001055 req_len, vma->vm_page_prot);
1056}
1057
Alex Williamson6140a8f2015-02-06 15:05:08 -07001058static 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 Williamson5f55d2a2015-04-28 10:23:30 -06001065 if (!(count % 10))
1066 dev_notice_ratelimited(&vdev->pdev->dev,
1067 "Relaying device request to user (#%u)\n",
1068 count);
Alex Williamson6140a8f2015-02-06 15:05:08 -07001069 eventfd_signal(vdev->req_trigger, 1);
Alex Williamson5f55d2a2015-04-28 10:23:30 -06001070 } else if (count == 0) {
1071 dev_warn(&vdev->pdev->dev,
1072 "No device request channel registered, blocked until released by user\n");
Alex Williamson6140a8f2015-02-06 15:05:08 -07001073 }
1074
1075 mutex_unlock(&vdev->igate);
1076}
1077
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001078static 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 Williamson6140a8f2015-02-06 15:05:08 -07001086 .request = vfio_pci_request,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001087};
1088
1089static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1090{
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001091 struct vfio_pci_device *vdev;
1092 struct iommu_group *group;
1093 int ret;
1094
Wei Yang7c2e2112015-01-07 10:29:11 -07001095 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001096 return -EINVAL;
1097
Alex Williamson03a76b62015-12-21 15:13:33 -07001098 group = vfio_iommu_group_get(&pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001099 if (!group)
1100 return -EINVAL;
1101
1102 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1103 if (!vdev) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001104 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001105 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 Williamson89e1f7d2012-07-31 08:16:24 -06001112
1113 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1114 if (ret) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001115 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001116 kfree(vdev);
Alex Williamson5a0ff172015-04-08 08:11:51 -06001117 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001118 }
1119
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001120 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 Williamson6eb70182015-04-07 11:14:46 -06001126 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 Williamson89e1f7d2012-07-31 08:16:24 -06001140 return ret;
1141}
1142
1143static void vfio_pci_remove(struct pci_dev *pdev)
1144{
1145 struct vfio_pci_device *vdev;
1146
Alex Williamson61d79252014-08-07 11:12:04 -06001147 vdev = vfio_del_group_dev(&pdev->dev);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001148 if (!vdev)
1149 return;
1150
Alex Williamson03a76b62015-12-21 15:13:33 -07001151 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
Alex Williamson28541d42016-02-22 16:02:39 -07001152 kfree(vdev->region);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001153 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 Williamson61d79252014-08-07 11:12:04 -06001160 }
Alex Williamson6eb70182015-04-07 11:14:46 -06001161
1162 if (!disable_idle_d3)
1163 pci_set_power_state(pdev, PCI_D0);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001164}
1165
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001166static 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 Williamson3be3a072014-01-14 16:12:55 -07001182 mutex_lock(&vdev->igate);
1183
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001184 if (vdev->err_trigger)
1185 eventfd_signal(vdev->err_trigger, 1);
1186
Alex Williamson3be3a072014-01-14 16:12:55 -07001187 mutex_unlock(&vdev->igate);
1188
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001189 vfio_device_put(device);
1190
1191 return PCI_ERS_RESULT_CAN_RECOVER;
1192}
1193
Julia Lawall7d10f4e2015-11-14 11:07:01 +01001194static const struct pci_error_handlers vfio_err_handlers = {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001195 .error_detected = vfio_pci_aer_err_detected,
1196};
1197
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001198static 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 Pandarathildad9f892013-03-11 09:31:22 -06001203 .err_handler = &vfio_err_handlers,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001204};
1205
Alex Williamson93899a62014-09-29 17:18:39 -06001206struct vfio_devices {
1207 struct vfio_device **devices;
1208 int cur_index;
1209 int max_index;
1210};
1211
1212static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001213{
Alex Williamson93899a62014-09-29 17:18:39 -06001214 struct vfio_devices *devs = data;
Alex Williamson20f30012015-06-09 10:08:57 -06001215 struct vfio_device *device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001216
Alex Williamson93899a62014-09-29 17:18:39 -06001217 if (devs->cur_index == devs->max_index)
1218 return -ENOSPC;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001219
Alex Williamson20f30012015-06-09 10:08:57 -06001220 device = vfio_device_get_from_dev(&pdev->dev);
1221 if (!device)
Alex Williamson93899a62014-09-29 17:18:39 -06001222 return -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001223
Alex Williamson20f30012015-06-09 10:08:57 -06001224 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 Williamsonbc4fba72014-08-07 11:12:07 -06001230 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 Williamson93899a62014-09-29 17:18:39 -06001236 * (!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 Williamsonbc4fba72014-08-07 11:12:07 -06001244 */
1245static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1246{
Alex Williamson93899a62014-09-29 17:18:39 -06001247 struct vfio_devices devs = { .cur_index = 0 };
1248 int i = 0, ret = -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001249 bool needs_reset = false, slot = false;
Alex Williamson93899a62014-09-29 17:18:39 -06001250 struct vfio_pci_device *tmp;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001251
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 Williamson93899a62014-09-29 17:18:39 -06001257 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 Williamsonbc4fba72014-08-07 11:12:07 -06001266 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
Alex Williamson93899a62014-09-29 17:18:39 -06001267 vfio_pci_get_devs, &devs, slot))
1268 goto put_devs;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001269
Alex Williamson93899a62014-09-29 17:18:39 -06001270 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 Williamsonbc4fba72014-08-07 11:12:07 -06001277
Alex Williamson93899a62014-09-29 17:18:39 -06001278 if (needs_reset)
1279 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1280 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001281
Alex Williamson93899a62014-09-29 17:18:39 -06001282put_devs:
1283 for (i = 0; i < devs.cur_index; i++) {
Alex Williamson6eb70182015-04-07 11:14:46 -06001284 tmp = vfio_device_data(devs.devices[i]);
1285 if (!ret)
Alex Williamson93899a62014-09-29 17:18:39 -06001286 tmp->needs_reset = false;
Alex Williamson6eb70182015-04-07 11:14:46 -06001287
1288 if (!tmp->refcnt && !disable_idle_d3)
1289 pci_set_power_state(tmp->pdev, PCI_D3hot);
1290
Alex Williamson93899a62014-09-29 17:18:39 -06001291 vfio_device_put(devs.devices[i]);
1292 }
1293
1294 kfree(devs.devices);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001295}
1296
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001297static void __exit vfio_pci_cleanup(void)
1298{
1299 pci_unregister_driver(&vfio_pci_driver);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001300 vfio_pci_uninit_perm_bits();
1301}
1302
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001303static 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 Williamson89e1f7d2012-07-31 08:16:24 -06001344static 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 Williamson89e1f7d2012-07-31 08:16:24 -06001353 /* Register and scan for devices */
1354 ret = pci_register_driver(&vfio_pci_driver);
1355 if (ret)
1356 goto out_driver;
1357
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001358 vfio_pci_fill_ids();
1359
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001360 return 0;
1361
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001362out_driver:
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001363 vfio_pci_uninit_perm_bits();
1364 return ret;
1365}
1366
1367module_init(vfio_pci_init);
1368module_exit(vfio_pci_cleanup);
1369
1370MODULE_VERSION(DRIVER_VERSION);
1371MODULE_LICENSE("GPL v2");
1372MODULE_AUTHOR(DRIVER_AUTHOR);
1373MODULE_DESCRIPTION(DRIVER_DESC);