blob: cb2624db37d893af72539f38444730c451d190b0 [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);
114
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600115static int vfio_pci_enable(struct vfio_pci_device *vdev)
116{
117 struct pci_dev *pdev = vdev->pdev;
118 int ret;
119 u16 cmd;
120 u8 msix_pos;
121
Alex Williamson6eb70182015-04-07 11:14:46 -0600122 pci_set_power_state(pdev, PCI_D0);
123
Alex Williamson9c22e662014-08-07 11:12:02 -0600124 /* Don't allow our initial saved state to include busmaster */
125 pci_clear_master(pdev);
126
Alex Williamson9a92c502012-12-07 13:43:51 -0700127 ret = pci_enable_device(pdev);
128 if (ret)
129 return ret;
130
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600131 vdev->reset_works = (pci_reset_function(pdev) == 0);
132 pci_save_state(pdev);
133 vdev->pci_saved_state = pci_store_saved_state(pdev);
134 if (!vdev->pci_saved_state)
135 pr_debug("%s: Couldn't store %s saved state\n",
136 __func__, dev_name(&pdev->dev));
137
138 ret = vfio_config_init(vdev);
Alex Williamson9a92c502012-12-07 13:43:51 -0700139 if (ret) {
Alex Williamsoneb5685f2014-05-30 11:35:53 -0600140 kfree(vdev->pci_saved_state);
141 vdev->pci_saved_state = NULL;
Alex Williamson9a92c502012-12-07 13:43:51 -0700142 pci_disable_device(pdev);
143 return ret;
144 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600145
146 if (likely(!nointxmask))
147 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
148
149 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
150 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
151 cmd &= ~PCI_COMMAND_INTX_DISABLE;
152 pci_write_config_word(pdev, PCI_COMMAND, cmd);
153 }
154
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600155 msix_pos = pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600156 if (msix_pos) {
157 u16 flags;
158 u32 table;
159
160 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
161 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
162
Bjorn Helgaas508d1aa2013-04-18 12:42:58 -0600163 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
164 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600165 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
166 } else
167 vdev->msix_bar = 0xFF;
168
Alex Williamsonecaa1f62015-04-07 11:14:41 -0600169 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
Alex Williamson84237a82013-02-18 10:11:13 -0700170 vdev->has_vga = true;
Alex Williamson84237a82013-02-18 10:11:13 -0700171
Alex Williamson5846ff52016-02-22 16:02:43 -0700172
173 if (vfio_pci_is_vga(pdev) && pdev->vendor == PCI_VENDOR_ID_INTEL) {
174 if (vfio_pci_igd_opregion_init(vdev) == 0)
175 dev_info(&pdev->dev,
176 "Intel IGD OpRegion support enabled\n");
177 }
178
Alex Williamson9a92c502012-12-07 13:43:51 -0700179 return 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600180}
181
182static void vfio_pci_disable(struct vfio_pci_device *vdev)
183{
Alex Williamson20077222012-12-07 13:43:50 -0700184 struct pci_dev *pdev = vdev->pdev;
Alex Williamson28541d42016-02-22 16:02:39 -0700185 int i, bar;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600186
Alex Williamson9c22e662014-08-07 11:12:02 -0600187 /* Stop the device from further DMA */
188 pci_clear_master(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600189
190 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
191 VFIO_IRQ_SET_ACTION_TRIGGER,
192 vdev->irq_type, 0, 0, NULL);
193
194 vdev->virq_disabled = false;
195
Alex Williamson28541d42016-02-22 16:02:39 -0700196 for (i = 0; i < vdev->num_regions; i++)
197 vdev->region[i].ops->release(vdev, &vdev->region[i]);
198
199 vdev->num_regions = 0;
200 kfree(vdev->region);
201 vdev->region = NULL; /* don't krealloc a freed pointer */
202
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600203 vfio_config_free(vdev);
204
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600205 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
206 if (!vdev->barmap[bar])
207 continue;
Alex Williamson20077222012-12-07 13:43:50 -0700208 pci_iounmap(pdev, vdev->barmap[bar]);
209 pci_release_selected_regions(pdev, 1 << bar);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600210 vdev->barmap[bar] = NULL;
211 }
Alex Williamson20077222012-12-07 13:43:50 -0700212
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600213 vdev->needs_reset = true;
214
Alex Williamson20077222012-12-07 13:43:50 -0700215 /*
216 * If we have saved state, restore it. If we can reset the device,
217 * even better. Resetting with current state seems better than
218 * nothing, but saving and restoring current state without reset
219 * is just busy work.
220 */
221 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
222 pr_info("%s: Couldn't reload %s saved state\n",
223 __func__, dev_name(&pdev->dev));
224
225 if (!vdev->reset_works)
Alex Williamson9c22e662014-08-07 11:12:02 -0600226 goto out;
Alex Williamson20077222012-12-07 13:43:50 -0700227
228 pci_save_state(pdev);
229 }
230
231 /*
232 * Disable INTx and MSI, presumably to avoid spurious interrupts
233 * during reset. Stolen from pci_reset_function()
234 */
235 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
236
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600237 /*
Alex Williamson890ed572014-01-14 20:45:09 -0700238 * Try to reset the device. The success of this is dependent on
239 * being able to lock the device, which is not always possible.
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600240 */
Alex Williamson561d72d2015-04-07 11:14:44 -0600241 if (vdev->reset_works && !pci_try_reset_function(pdev))
242 vdev->needs_reset = false;
Alex Williamson20077222012-12-07 13:43:50 -0700243
244 pci_restore_state(pdev);
Alex Williamson9c22e662014-08-07 11:12:02 -0600245out:
246 pci_disable_device(pdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600247
248 vfio_pci_try_bus_reset(vdev);
Alex Williamson6eb70182015-04-07 11:14:46 -0600249
250 if (!disable_idle_d3)
251 pci_set_power_state(pdev, PCI_D3hot);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600252}
253
254static void vfio_pci_release(void *device_data)
255{
256 struct vfio_pci_device *vdev = device_data;
257
Alex Williamson61d79252014-08-07 11:12:04 -0600258 mutex_lock(&driver_lock);
259
260 if (!(--vdev->refcnt)) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000261 vfio_spapr_pci_eeh_release(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600262 vfio_pci_disable(vdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000263 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600264
Alex Williamson61d79252014-08-07 11:12:04 -0600265 mutex_unlock(&driver_lock);
266
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600267 module_put(THIS_MODULE);
268}
269
270static int vfio_pci_open(void *device_data)
271{
272 struct vfio_pci_device *vdev = device_data;
Alex Williamson61d79252014-08-07 11:12:04 -0600273 int ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600274
275 if (!try_module_get(THIS_MODULE))
276 return -ENODEV;
277
Alex Williamson61d79252014-08-07 11:12:04 -0600278 mutex_lock(&driver_lock);
279
280 if (!vdev->refcnt) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000281 ret = vfio_pci_enable(vdev);
282 if (ret)
283 goto error;
284
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600285 vfio_spapr_pci_eeh_open(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600286 }
Alex Williamson61d79252014-08-07 11:12:04 -0600287 vdev->refcnt++;
Gavin Shan1b69be52014-06-10 11:41:57 +1000288error:
Alex Williamson61d79252014-08-07 11:12:04 -0600289 mutex_unlock(&driver_lock);
290 if (ret)
291 module_put(THIS_MODULE);
Gavin Shan1b69be52014-06-10 11:41:57 +1000292 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600293}
294
295static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
296{
297 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
298 u8 pin;
299 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700300 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600301 return 1;
302
303 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
304 u8 pos;
305 u16 flags;
306
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600307 pos = vdev->pdev->msi_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600308 if (pos) {
309 pci_read_config_word(vdev->pdev,
310 pos + PCI_MSI_FLAGS, &flags);
Gavin Shanfd49c81f2014-05-30 11:35:54 -0600311 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600312 }
313 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
314 u8 pos;
315 u16 flags;
316
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600317 pos = vdev->pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600318 if (pos) {
319 pci_read_config_word(vdev->pdev,
320 pos + PCI_MSIX_FLAGS, &flags);
321
322 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
323 }
Alex Williamson6140a8f2015-02-06 15:05:08 -0700324 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600325 if (pci_is_pcie(vdev->pdev))
326 return 1;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700327 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
328 return 1;
329 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600330
331 return 0;
332}
333
Alex Williamson8b27ee62013-09-04 11:28:04 -0600334static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
335{
336 (*(int *)data)++;
337 return 0;
338}
339
340struct vfio_pci_fill_info {
341 int max;
342 int cur;
343 struct vfio_pci_dependent_device *devices;
344};
345
346static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
347{
348 struct vfio_pci_fill_info *fill = data;
349 struct iommu_group *iommu_group;
350
351 if (fill->cur == fill->max)
352 return -EAGAIN; /* Something changed, try again */
353
354 iommu_group = iommu_group_get(&pdev->dev);
355 if (!iommu_group)
356 return -EPERM; /* Cannot reset non-isolated devices */
357
358 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
359 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
360 fill->devices[fill->cur].bus = pdev->bus->number;
361 fill->devices[fill->cur].devfn = pdev->devfn;
362 fill->cur++;
363 iommu_group_put(iommu_group);
364 return 0;
365}
366
367struct vfio_pci_group_entry {
368 struct vfio_group *group;
369 int id;
370};
371
372struct vfio_pci_group_info {
373 int count;
374 struct vfio_pci_group_entry *groups;
375};
376
377static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
378{
379 struct vfio_pci_group_info *info = data;
380 struct iommu_group *group;
381 int id, i;
382
383 group = iommu_group_get(&pdev->dev);
384 if (!group)
385 return -EPERM;
386
387 id = iommu_group_id(group);
388
389 for (i = 0; i < info->count; i++)
390 if (info->groups[i].id == id)
391 break;
392
393 iommu_group_put(group);
394
395 return (i == info->count) ? -EINVAL : 0;
396}
397
398static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
399{
400 for (; pdev; pdev = pdev->bus->self)
401 if (pdev->bus == slot->bus)
402 return (pdev->slot == slot);
403 return false;
404}
405
406struct vfio_pci_walk_info {
407 int (*fn)(struct pci_dev *, void *data);
408 void *data;
409 struct pci_dev *pdev;
410 bool slot;
411 int ret;
412};
413
414static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
415{
416 struct vfio_pci_walk_info *walk = data;
417
418 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
419 walk->ret = walk->fn(pdev, walk->data);
420
421 return walk->ret;
422}
423
424static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
425 int (*fn)(struct pci_dev *,
426 void *data), void *data,
427 bool slot)
428{
429 struct vfio_pci_walk_info walk = {
430 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
431 };
432
433 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
434
435 return walk.ret;
436}
437
Alex Williamson188ad9d2016-02-22 16:02:36 -0700438static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
439 struct vfio_info_cap *caps)
440{
441 struct vfio_info_cap_header *header;
442 struct vfio_region_info_cap_sparse_mmap *sparse;
443 size_t end, size;
444 int nr_areas = 2, i = 0;
445
446 end = pci_resource_len(vdev->pdev, vdev->msix_bar);
447
448 /* If MSI-X table is aligned to the start or end, only one area */
449 if (((vdev->msix_offset & PAGE_MASK) == 0) ||
450 (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
451 nr_areas = 1;
452
453 size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
454
455 header = vfio_info_cap_add(caps, size,
456 VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
457 if (IS_ERR(header))
458 return PTR_ERR(header);
459
460 sparse = container_of(header,
461 struct vfio_region_info_cap_sparse_mmap, header);
462 sparse->nr_areas = nr_areas;
463
464 if (vdev->msix_offset & PAGE_MASK) {
465 sparse->areas[i].offset = 0;
466 sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
467 i++;
468 }
469
470 if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
471 sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
472 vdev->msix_size);
473 sparse->areas[i].size = end - sparse->areas[i].offset;
474 i++;
475 }
476
477 return 0;
478}
479
Alex Williamson28541d42016-02-22 16:02:39 -0700480static int region_type_cap(struct vfio_pci_device *vdev,
481 struct vfio_info_cap *caps,
482 unsigned int type, unsigned int subtype)
483{
484 struct vfio_info_cap_header *header;
485 struct vfio_region_info_cap_type *cap;
486
487 header = vfio_info_cap_add(caps, sizeof(*cap),
488 VFIO_REGION_INFO_CAP_TYPE, 1);
489 if (IS_ERR(header))
490 return PTR_ERR(header);
491
492 cap = container_of(header, struct vfio_region_info_cap_type, header);
493 cap->type = type;
494 cap->subtype = subtype;
495
496 return 0;
497}
498
499int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
500 unsigned int type, unsigned int subtype,
501 const struct vfio_pci_regops *ops,
502 size_t size, u32 flags, void *data)
503{
504 struct vfio_pci_region *region;
505
506 region = krealloc(vdev->region,
507 (vdev->num_regions + 1) * sizeof(*region),
508 GFP_KERNEL);
509 if (!region)
510 return -ENOMEM;
511
512 vdev->region = region;
513 vdev->region[vdev->num_regions].type = type;
514 vdev->region[vdev->num_regions].subtype = subtype;
515 vdev->region[vdev->num_regions].ops = ops;
516 vdev->region[vdev->num_regions].size = size;
517 vdev->region[vdev->num_regions].flags = flags;
518 vdev->region[vdev->num_regions].data = data;
519
520 vdev->num_regions++;
521
522 return 0;
523}
524
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600525static long vfio_pci_ioctl(void *device_data,
526 unsigned int cmd, unsigned long arg)
527{
528 struct vfio_pci_device *vdev = device_data;
529 unsigned long minsz;
530
531 if (cmd == VFIO_DEVICE_GET_INFO) {
532 struct vfio_device_info info;
533
534 minsz = offsetofend(struct vfio_device_info, num_irqs);
535
536 if (copy_from_user(&info, (void __user *)arg, minsz))
537 return -EFAULT;
538
539 if (info.argsz < minsz)
540 return -EINVAL;
541
542 info.flags = VFIO_DEVICE_FLAGS_PCI;
543
544 if (vdev->reset_works)
545 info.flags |= VFIO_DEVICE_FLAGS_RESET;
546
Alex Williamson28541d42016-02-22 16:02:39 -0700547 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600548 info.num_irqs = VFIO_PCI_NUM_IRQS;
549
550 return copy_to_user((void __user *)arg, &info, minsz);
551
552 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
553 struct pci_dev *pdev = vdev->pdev;
554 struct vfio_region_info info;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700555 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
Alex Williamson28541d42016-02-22 16:02:39 -0700556 int i, ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600557
558 minsz = offsetofend(struct vfio_region_info, offset);
559
560 if (copy_from_user(&info, (void __user *)arg, minsz))
561 return -EFAULT;
562
563 if (info.argsz < minsz)
564 return -EINVAL;
565
566 switch (info.index) {
567 case VFIO_PCI_CONFIG_REGION_INDEX:
568 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
569 info.size = pdev->cfg_size;
570 info.flags = VFIO_REGION_INFO_FLAG_READ |
571 VFIO_REGION_INFO_FLAG_WRITE;
572 break;
573 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
574 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
575 info.size = pci_resource_len(pdev, info.index);
576 if (!info.size) {
577 info.flags = 0;
578 break;
579 }
580
581 info.flags = VFIO_REGION_INFO_FLAG_READ |
582 VFIO_REGION_INFO_FLAG_WRITE;
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700583 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
584 pci_resource_flags(pdev, info.index) &
Alex Williamson188ad9d2016-02-22 16:02:36 -0700585 IORESOURCE_MEM && info.size >= PAGE_SIZE) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600586 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700587 if (info.index == vdev->msix_bar) {
588 ret = msix_sparse_mmap_cap(vdev, &caps);
589 if (ret)
590 return ret;
591 }
592 }
593
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600594 break;
595 case VFIO_PCI_ROM_REGION_INDEX:
596 {
597 void __iomem *io;
598 size_t size;
599
600 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
601 info.flags = 0;
602
603 /* Report the BAR size, not the ROM size */
604 info.size = pci_resource_len(pdev, info.index);
605 if (!info.size)
606 break;
607
608 /* Is it really there? */
609 io = pci_map_rom(pdev, &size);
610 if (!io || !size) {
611 info.size = 0;
612 break;
613 }
614 pci_unmap_rom(pdev, io);
615
616 info.flags = VFIO_REGION_INFO_FLAG_READ;
617 break;
618 }
Alex Williamson84237a82013-02-18 10:11:13 -0700619 case VFIO_PCI_VGA_REGION_INDEX:
620 if (!vdev->has_vga)
621 return -EINVAL;
622
623 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
624 info.size = 0xc0000;
625 info.flags = VFIO_REGION_INFO_FLAG_READ |
626 VFIO_REGION_INFO_FLAG_WRITE;
627
628 break;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600629 default:
Alex Williamson28541d42016-02-22 16:02:39 -0700630 if (info.index >=
631 VFIO_PCI_NUM_REGIONS + vdev->num_regions)
632 return -EINVAL;
633
634 i = info.index - VFIO_PCI_NUM_REGIONS;
635
636 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
637 info.size = vdev->region[i].size;
638 info.flags = vdev->region[i].flags;
639
640 ret = region_type_cap(vdev, &caps,
641 vdev->region[i].type,
642 vdev->region[i].subtype);
643 if (ret)
644 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600645 }
646
Alex Williamson188ad9d2016-02-22 16:02:36 -0700647 if (caps.size) {
648 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
649 if (info.argsz < sizeof(info) + caps.size) {
650 info.argsz = sizeof(info) + caps.size;
651 info.cap_offset = 0;
652 } else {
653 vfio_info_cap_shift(&caps, sizeof(info));
654 ret = copy_to_user((void __user *)arg +
655 sizeof(info), caps.buf,
656 caps.size);
657 if (ret) {
658 kfree(caps.buf);
659 return ret;
660 }
661 info.cap_offset = sizeof(info);
662 }
663
664 kfree(caps.buf);
665 }
666
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600667 return copy_to_user((void __user *)arg, &info, minsz);
668
669 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
670 struct vfio_irq_info info;
671
672 minsz = offsetofend(struct vfio_irq_info, count);
673
674 if (copy_from_user(&info, (void __user *)arg, minsz))
675 return -EFAULT;
676
677 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
678 return -EINVAL;
679
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600680 switch (info.index) {
681 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
Alex Williamson6140a8f2015-02-06 15:05:08 -0700682 case VFIO_PCI_REQ_IRQ_INDEX:
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600683 break;
684 case VFIO_PCI_ERR_IRQ_INDEX:
685 if (pci_is_pcie(vdev->pdev))
686 break;
687 /* pass thru to return error */
688 default:
689 return -EINVAL;
690 }
691
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600692 info.flags = VFIO_IRQ_INFO_EVENTFD;
693
694 info.count = vfio_pci_get_irq_count(vdev, info.index);
695
696 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
697 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
698 VFIO_IRQ_INFO_AUTOMASKED);
699 else
700 info.flags |= VFIO_IRQ_INFO_NORESIZE;
701
702 return copy_to_user((void __user *)arg, &info, minsz);
703
704 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
705 struct vfio_irq_set hdr;
706 u8 *data = NULL;
707 int ret = 0;
708
709 minsz = offsetofend(struct vfio_irq_set, count);
710
711 if (copy_from_user(&hdr, (void __user *)arg, minsz))
712 return -EFAULT;
713
714 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
715 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
716 VFIO_IRQ_SET_ACTION_TYPE_MASK))
717 return -EINVAL;
718
719 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
720 size_t size;
Alex Williamson904c6802013-03-26 11:33:16 -0600721 int max = vfio_pci_get_irq_count(vdev, hdr.index);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600722
723 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
724 size = sizeof(uint8_t);
725 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
726 size = sizeof(int32_t);
727 else
728 return -EINVAL;
729
730 if (hdr.argsz - minsz < hdr.count * size ||
Alex Williamson904c6802013-03-26 11:33:16 -0600731 hdr.start >= max || hdr.start + hdr.count > max)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600732 return -EINVAL;
733
Fengguang Wu3a1f7042012-12-07 13:43:49 -0700734 data = memdup_user((void __user *)(arg + minsz),
735 hdr.count * size);
736 if (IS_ERR(data))
737 return PTR_ERR(data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600738 }
739
740 mutex_lock(&vdev->igate);
741
742 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
743 hdr.start, hdr.count, data);
744
745 mutex_unlock(&vdev->igate);
746 kfree(data);
747
748 return ret;
749
Alex Williamson8b27ee62013-09-04 11:28:04 -0600750 } else if (cmd == VFIO_DEVICE_RESET) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600751 return vdev->reset_works ?
Alex Williamson890ed572014-01-14 20:45:09 -0700752 pci_try_reset_function(vdev->pdev) : -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600753
Alex Williamson8b27ee62013-09-04 11:28:04 -0600754 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
755 struct vfio_pci_hot_reset_info hdr;
756 struct vfio_pci_fill_info fill = { 0 };
757 struct vfio_pci_dependent_device *devices = NULL;
758 bool slot = false;
759 int ret = 0;
760
761 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
762
763 if (copy_from_user(&hdr, (void __user *)arg, minsz))
764 return -EFAULT;
765
766 if (hdr.argsz < minsz)
767 return -EINVAL;
768
769 hdr.flags = 0;
770
771 /* Can we do a slot or bus reset or neither? */
772 if (!pci_probe_reset_slot(vdev->pdev->slot))
773 slot = true;
774 else if (pci_probe_reset_bus(vdev->pdev->bus))
775 return -ENODEV;
776
777 /* How many devices are affected? */
778 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
779 vfio_pci_count_devs,
780 &fill.max, slot);
781 if (ret)
782 return ret;
783
784 WARN_ON(!fill.max); /* Should always be at least one */
785
786 /*
787 * If there's enough space, fill it now, otherwise return
788 * -ENOSPC and the number of devices affected.
789 */
790 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
791 ret = -ENOSPC;
792 hdr.count = fill.max;
793 goto reset_info_exit;
794 }
795
796 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
797 if (!devices)
798 return -ENOMEM;
799
800 fill.devices = devices;
801
802 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
803 vfio_pci_fill_devs,
804 &fill, slot);
805
806 /*
807 * If a device was removed between counting and filling,
808 * we may come up short of fill.max. If a device was
809 * added, we'll have a return of -EAGAIN above.
810 */
811 if (!ret)
812 hdr.count = fill.cur;
813
814reset_info_exit:
815 if (copy_to_user((void __user *)arg, &hdr, minsz))
816 ret = -EFAULT;
817
818 if (!ret) {
819 if (copy_to_user((void __user *)(arg + minsz), devices,
820 hdr.count * sizeof(*devices)))
821 ret = -EFAULT;
822 }
823
824 kfree(devices);
825 return ret;
826
827 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
828 struct vfio_pci_hot_reset hdr;
829 int32_t *group_fds;
830 struct vfio_pci_group_entry *groups;
831 struct vfio_pci_group_info info;
832 bool slot = false;
833 int i, count = 0, ret = 0;
834
835 minsz = offsetofend(struct vfio_pci_hot_reset, count);
836
837 if (copy_from_user(&hdr, (void __user *)arg, minsz))
838 return -EFAULT;
839
840 if (hdr.argsz < minsz || hdr.flags)
841 return -EINVAL;
842
843 /* Can we do a slot or bus reset or neither? */
844 if (!pci_probe_reset_slot(vdev->pdev->slot))
845 slot = true;
846 else if (pci_probe_reset_bus(vdev->pdev->bus))
847 return -ENODEV;
848
849 /*
850 * We can't let userspace give us an arbitrarily large
851 * buffer to copy, so verify how many we think there
852 * could be. Note groups can have multiple devices so
853 * one group per device is the max.
854 */
855 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
856 vfio_pci_count_devs,
857 &count, slot);
858 if (ret)
859 return ret;
860
861 /* Somewhere between 1 and count is OK */
862 if (!hdr.count || hdr.count > count)
863 return -EINVAL;
864
865 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
866 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
867 if (!group_fds || !groups) {
868 kfree(group_fds);
869 kfree(groups);
870 return -ENOMEM;
871 }
872
873 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
874 hdr.count * sizeof(*group_fds))) {
875 kfree(group_fds);
876 kfree(groups);
877 return -EFAULT;
878 }
879
880 /*
881 * For each group_fd, get the group through the vfio external
882 * user interface and store the group and iommu ID. This
883 * ensures the group is held across the reset.
884 */
885 for (i = 0; i < hdr.count; i++) {
886 struct vfio_group *group;
887 struct fd f = fdget(group_fds[i]);
888 if (!f.file) {
889 ret = -EBADF;
890 break;
891 }
892
893 group = vfio_group_get_external_user(f.file);
894 fdput(f);
895 if (IS_ERR(group)) {
896 ret = PTR_ERR(group);
897 break;
898 }
899
900 groups[i].group = group;
901 groups[i].id = vfio_external_user_iommu_id(group);
902 }
903
904 kfree(group_fds);
905
906 /* release reference to groups on error */
907 if (ret)
908 goto hot_reset_release;
909
910 info.count = hdr.count;
911 info.groups = groups;
912
913 /*
914 * Test whether all the affected devices are contained
915 * by the set of groups provided by the user.
916 */
917 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
918 vfio_pci_validate_devs,
919 &info, slot);
920 if (!ret)
921 /* User has access, do the reset */
Alex Williamson890ed572014-01-14 20:45:09 -0700922 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
923 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamson8b27ee62013-09-04 11:28:04 -0600924
925hot_reset_release:
926 for (i--; i >= 0; i--)
927 vfio_group_put_external_user(groups[i].group);
928
929 kfree(groups);
930 return ret;
931 }
932
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600933 return -ENOTTY;
934}
935
Alex Williamson5b279a12013-02-14 14:02:12 -0700936static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
937 size_t count, loff_t *ppos, bool iswrite)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600938{
939 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
940 struct vfio_pci_device *vdev = device_data;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600941
Alex Williamson28541d42016-02-22 16:02:39 -0700942 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600943 return -EINVAL;
944
Alex Williamson5b279a12013-02-14 14:02:12 -0700945 switch (index) {
946 case VFIO_PCI_CONFIG_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700947 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
948
Alex Williamson5b279a12013-02-14 14:02:12 -0700949 case VFIO_PCI_ROM_REGION_INDEX:
950 if (iswrite)
951 return -EINVAL;
Alex Williamson906ee992013-02-14 14:02:12 -0700952 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600953
Alex Williamson5b279a12013-02-14 14:02:12 -0700954 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700955 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson84237a82013-02-18 10:11:13 -0700956
957 case VFIO_PCI_VGA_REGION_INDEX:
958 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson28541d42016-02-22 16:02:39 -0700959 default:
960 index -= VFIO_PCI_NUM_REGIONS;
961 return vdev->region[index].ops->rw(vdev, buf,
962 count, ppos, iswrite);
Alex Williamson5b279a12013-02-14 14:02:12 -0700963 }
964
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600965 return -EINVAL;
966}
967
Alex Williamson5b279a12013-02-14 14:02:12 -0700968static ssize_t vfio_pci_read(void *device_data, char __user *buf,
969 size_t count, loff_t *ppos)
970{
Alex Williamson906ee992013-02-14 14:02:12 -0700971 if (!count)
972 return 0;
973
Alex Williamson5b279a12013-02-14 14:02:12 -0700974 return vfio_pci_rw(device_data, buf, count, ppos, false);
975}
976
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600977static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
978 size_t count, loff_t *ppos)
979{
Alex Williamson906ee992013-02-14 14:02:12 -0700980 if (!count)
981 return 0;
982
983 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600984}
985
986static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
987{
988 struct vfio_pci_device *vdev = device_data;
989 struct pci_dev *pdev = vdev->pdev;
990 unsigned int index;
Alex Williamson34002f52012-10-10 09:10:31 -0600991 u64 phys_len, req_len, pgoff, req_start;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600992 int ret;
993
994 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
995
996 if (vma->vm_end < vma->vm_start)
997 return -EINVAL;
998 if ((vma->vm_flags & VM_SHARED) == 0)
999 return -EINVAL;
1000 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1001 return -EINVAL;
1002 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
1003 return -EINVAL;
1004
1005 phys_len = pci_resource_len(pdev, index);
1006 req_len = vma->vm_end - vma->vm_start;
1007 pgoff = vma->vm_pgoff &
1008 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1009 req_start = pgoff << PAGE_SHIFT;
1010
1011 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
1012 return -EINVAL;
1013
1014 if (index == vdev->msix_bar) {
1015 /*
1016 * Disallow mmaps overlapping the MSI-X table; users don't
1017 * get to touch this directly. We could find somewhere
1018 * else to map the overlap, but page granularity is only
1019 * a recommendation, not a requirement, so the user needs
1020 * to know which bits are real. Requiring them to mmap
1021 * around the table makes that clear.
1022 */
1023
1024 /* If neither entirely above nor below, then it overlaps */
1025 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
1026 req_start + req_len <= vdev->msix_offset))
1027 return -EINVAL;
1028 }
1029
1030 /*
1031 * Even though we don't make use of the barmap for the mmap,
1032 * we need to request the region and the barmap tracks that.
1033 */
1034 if (!vdev->barmap[index]) {
1035 ret = pci_request_selected_regions(pdev,
1036 1 << index, "vfio-pci");
1037 if (ret)
1038 return ret;
1039
1040 vdev->barmap[index] = pci_iomap(pdev, index, 0);
1041 }
1042
1043 vma->vm_private_data = vdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001044 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Alex Williamson34002f52012-10-10 09:10:31 -06001045 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001046
Alex Williamson34002f52012-10-10 09:10:31 -06001047 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001048 req_len, vma->vm_page_prot);
1049}
1050
Alex Williamson6140a8f2015-02-06 15:05:08 -07001051static void vfio_pci_request(void *device_data, unsigned int count)
1052{
1053 struct vfio_pci_device *vdev = device_data;
1054
1055 mutex_lock(&vdev->igate);
1056
1057 if (vdev->req_trigger) {
Alex Williamson5f55d2a2015-04-28 10:23:30 -06001058 if (!(count % 10))
1059 dev_notice_ratelimited(&vdev->pdev->dev,
1060 "Relaying device request to user (#%u)\n",
1061 count);
Alex Williamson6140a8f2015-02-06 15:05:08 -07001062 eventfd_signal(vdev->req_trigger, 1);
Alex Williamson5f55d2a2015-04-28 10:23:30 -06001063 } else if (count == 0) {
1064 dev_warn(&vdev->pdev->dev,
1065 "No device request channel registered, blocked until released by user\n");
Alex Williamson6140a8f2015-02-06 15:05:08 -07001066 }
1067
1068 mutex_unlock(&vdev->igate);
1069}
1070
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001071static const struct vfio_device_ops vfio_pci_ops = {
1072 .name = "vfio-pci",
1073 .open = vfio_pci_open,
1074 .release = vfio_pci_release,
1075 .ioctl = vfio_pci_ioctl,
1076 .read = vfio_pci_read,
1077 .write = vfio_pci_write,
1078 .mmap = vfio_pci_mmap,
Alex Williamson6140a8f2015-02-06 15:05:08 -07001079 .request = vfio_pci_request,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001080};
1081
1082static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1083{
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001084 struct vfio_pci_device *vdev;
1085 struct iommu_group *group;
1086 int ret;
1087
Wei Yang7c2e2112015-01-07 10:29:11 -07001088 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001089 return -EINVAL;
1090
Alex Williamson03a76b62015-12-21 15:13:33 -07001091 group = vfio_iommu_group_get(&pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001092 if (!group)
1093 return -EINVAL;
1094
1095 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1096 if (!vdev) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001097 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001098 return -ENOMEM;
1099 }
1100
1101 vdev->pdev = pdev;
1102 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1103 mutex_init(&vdev->igate);
1104 spin_lock_init(&vdev->irqlock);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001105
1106 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1107 if (ret) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001108 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001109 kfree(vdev);
Alex Williamson5a0ff172015-04-08 08:11:51 -06001110 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001111 }
1112
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001113 if (vfio_pci_is_vga(pdev)) {
1114 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1115 vga_set_legacy_decoding(pdev,
1116 vfio_pci_set_vga_decode(vdev, false));
1117 }
1118
Alex Williamson6eb70182015-04-07 11:14:46 -06001119 if (!disable_idle_d3) {
1120 /*
1121 * pci-core sets the device power state to an unknown value at
1122 * bootup and after being removed from a driver. The only
1123 * transition it allows from this unknown state is to D0, which
1124 * typically happens when a driver calls pci_enable_device().
1125 * We're not ready to enable the device yet, but we do want to
1126 * be able to get to D3. Therefore first do a D0 transition
1127 * before going to D3.
1128 */
1129 pci_set_power_state(pdev, PCI_D0);
1130 pci_set_power_state(pdev, PCI_D3hot);
1131 }
1132
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001133 return ret;
1134}
1135
1136static void vfio_pci_remove(struct pci_dev *pdev)
1137{
1138 struct vfio_pci_device *vdev;
1139
Alex Williamson61d79252014-08-07 11:12:04 -06001140 vdev = vfio_del_group_dev(&pdev->dev);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001141 if (!vdev)
1142 return;
1143
Alex Williamson03a76b62015-12-21 15:13:33 -07001144 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
Alex Williamson28541d42016-02-22 16:02:39 -07001145 kfree(vdev->region);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001146 kfree(vdev);
1147
1148 if (vfio_pci_is_vga(pdev)) {
1149 vga_client_register(pdev, NULL, NULL, NULL);
1150 vga_set_legacy_decoding(pdev,
1151 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1152 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
Alex Williamson61d79252014-08-07 11:12:04 -06001153 }
Alex Williamson6eb70182015-04-07 11:14:46 -06001154
1155 if (!disable_idle_d3)
1156 pci_set_power_state(pdev, PCI_D0);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001157}
1158
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001159static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1160 pci_channel_state_t state)
1161{
1162 struct vfio_pci_device *vdev;
1163 struct vfio_device *device;
1164
1165 device = vfio_device_get_from_dev(&pdev->dev);
1166 if (device == NULL)
1167 return PCI_ERS_RESULT_DISCONNECT;
1168
1169 vdev = vfio_device_data(device);
1170 if (vdev == NULL) {
1171 vfio_device_put(device);
1172 return PCI_ERS_RESULT_DISCONNECT;
1173 }
1174
Alex Williamson3be3a072014-01-14 16:12:55 -07001175 mutex_lock(&vdev->igate);
1176
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001177 if (vdev->err_trigger)
1178 eventfd_signal(vdev->err_trigger, 1);
1179
Alex Williamson3be3a072014-01-14 16:12:55 -07001180 mutex_unlock(&vdev->igate);
1181
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001182 vfio_device_put(device);
1183
1184 return PCI_ERS_RESULT_CAN_RECOVER;
1185}
1186
Julia Lawall7d10f4e02015-11-14 11:07:01 +01001187static const struct pci_error_handlers vfio_err_handlers = {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001188 .error_detected = vfio_pci_aer_err_detected,
1189};
1190
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001191static struct pci_driver vfio_pci_driver = {
1192 .name = "vfio-pci",
1193 .id_table = NULL, /* only dynamic ids */
1194 .probe = vfio_pci_probe,
1195 .remove = vfio_pci_remove,
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001196 .err_handler = &vfio_err_handlers,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001197};
1198
Alex Williamson93899a62014-09-29 17:18:39 -06001199struct vfio_devices {
1200 struct vfio_device **devices;
1201 int cur_index;
1202 int max_index;
1203};
1204
1205static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001206{
Alex Williamson93899a62014-09-29 17:18:39 -06001207 struct vfio_devices *devs = data;
Alex Williamson20f30012015-06-09 10:08:57 -06001208 struct vfio_device *device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001209
Alex Williamson93899a62014-09-29 17:18:39 -06001210 if (devs->cur_index == devs->max_index)
1211 return -ENOSPC;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001212
Alex Williamson20f30012015-06-09 10:08:57 -06001213 device = vfio_device_get_from_dev(&pdev->dev);
1214 if (!device)
Alex Williamson93899a62014-09-29 17:18:39 -06001215 return -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001216
Alex Williamson20f30012015-06-09 10:08:57 -06001217 if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1218 vfio_device_put(device);
1219 return -EBUSY;
1220 }
1221
1222 devs->devices[devs->cur_index++] = device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001223 return 0;
1224}
1225
1226/*
1227 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1228 * this device that are needs_reset and all of the affected devices are unused
Alex Williamson93899a62014-09-29 17:18:39 -06001229 * (!refcnt). Callers are required to hold driver_lock when calling this to
1230 * prevent device opens and concurrent bus reset attempts. We prevent device
1231 * unbinds by acquiring and holding a reference to the vfio_device.
1232 *
1233 * NB: vfio-core considers a group to be viable even if some devices are
1234 * bound to drivers like pci-stub or pcieport. Here we require all devices
1235 * to be bound to vfio_pci since that's the only way we can be sure they
1236 * stay put.
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001237 */
1238static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1239{
Alex Williamson93899a62014-09-29 17:18:39 -06001240 struct vfio_devices devs = { .cur_index = 0 };
1241 int i = 0, ret = -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001242 bool needs_reset = false, slot = false;
Alex Williamson93899a62014-09-29 17:18:39 -06001243 struct vfio_pci_device *tmp;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001244
1245 if (!pci_probe_reset_slot(vdev->pdev->slot))
1246 slot = true;
1247 else if (pci_probe_reset_bus(vdev->pdev->bus))
1248 return;
1249
Alex Williamson93899a62014-09-29 17:18:39 -06001250 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1251 &i, slot) || !i)
1252 return;
1253
1254 devs.max_index = i;
1255 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1256 if (!devs.devices)
1257 return;
1258
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001259 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
Alex Williamson93899a62014-09-29 17:18:39 -06001260 vfio_pci_get_devs, &devs, slot))
1261 goto put_devs;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001262
Alex Williamson93899a62014-09-29 17:18:39 -06001263 for (i = 0; i < devs.cur_index; i++) {
1264 tmp = vfio_device_data(devs.devices[i]);
1265 if (tmp->needs_reset)
1266 needs_reset = true;
1267 if (tmp->refcnt)
1268 goto put_devs;
1269 }
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001270
Alex Williamson93899a62014-09-29 17:18:39 -06001271 if (needs_reset)
1272 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1273 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001274
Alex Williamson93899a62014-09-29 17:18:39 -06001275put_devs:
1276 for (i = 0; i < devs.cur_index; i++) {
Alex Williamson6eb70182015-04-07 11:14:46 -06001277 tmp = vfio_device_data(devs.devices[i]);
1278 if (!ret)
Alex Williamson93899a62014-09-29 17:18:39 -06001279 tmp->needs_reset = false;
Alex Williamson6eb70182015-04-07 11:14:46 -06001280
1281 if (!tmp->refcnt && !disable_idle_d3)
1282 pci_set_power_state(tmp->pdev, PCI_D3hot);
1283
Alex Williamson93899a62014-09-29 17:18:39 -06001284 vfio_device_put(devs.devices[i]);
1285 }
1286
1287 kfree(devs.devices);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001288}
1289
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001290static void __exit vfio_pci_cleanup(void)
1291{
1292 pci_unregister_driver(&vfio_pci_driver);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001293 vfio_pci_uninit_perm_bits();
1294}
1295
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001296static void __init vfio_pci_fill_ids(void)
1297{
1298 char *p, *id;
1299 int rc;
1300
1301 /* no ids passed actually */
1302 if (ids[0] == '\0')
1303 return;
1304
1305 /* add ids specified in the module parameter */
1306 p = ids;
1307 while ((id = strsep(&p, ","))) {
1308 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1309 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1310 int fields;
1311
1312 if (!strlen(id))
1313 continue;
1314
1315 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1316 &vendor, &device, &subvendor, &subdevice,
1317 &class, &class_mask);
1318
1319 if (fields < 2) {
1320 pr_warn("invalid id string \"%s\"\n", id);
1321 continue;
1322 }
1323
1324 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1325 subvendor, subdevice, class, class_mask, 0);
1326 if (rc)
1327 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1328 vendor, device, subvendor, subdevice,
1329 class, class_mask, rc);
1330 else
1331 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1332 vendor, device, subvendor, subdevice,
1333 class, class_mask);
1334 }
1335}
1336
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001337static int __init vfio_pci_init(void)
1338{
1339 int ret;
1340
1341 /* Allocate shared config space permision data used by all devices */
1342 ret = vfio_pci_init_perm_bits();
1343 if (ret)
1344 return ret;
1345
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001346 /* Register and scan for devices */
1347 ret = pci_register_driver(&vfio_pci_driver);
1348 if (ret)
1349 goto out_driver;
1350
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001351 vfio_pci_fill_ids();
1352
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001353 return 0;
1354
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001355out_driver:
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001356 vfio_pci_uninit_perm_bits();
1357 return ret;
1358}
1359
1360module_init(vfio_pci_init);
1361module_exit(vfio_pci_cleanup);
1362
1363MODULE_VERSION(DRIVER_VERSION);
1364MODULE_LICENSE("GPL v2");
1365MODULE_AUTHOR(DRIVER_AUTHOR);
1366MODULE_DESCRIPTION(DRIVER_DESC);