blob: 4682207b1ac8542dbeee14d268fae76ee7959e8f [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 Williamson9a92c502012-12-07 13:43:51 -0700172 return 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600173}
174
175static void vfio_pci_disable(struct vfio_pci_device *vdev)
176{
Alex Williamson20077222012-12-07 13:43:50 -0700177 struct pci_dev *pdev = vdev->pdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600178 int bar;
179
Alex Williamson9c22e662014-08-07 11:12:02 -0600180 /* Stop the device from further DMA */
181 pci_clear_master(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600182
183 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
184 VFIO_IRQ_SET_ACTION_TRIGGER,
185 vdev->irq_type, 0, 0, NULL);
186
187 vdev->virq_disabled = false;
188
189 vfio_config_free(vdev);
190
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600191 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
192 if (!vdev->barmap[bar])
193 continue;
Alex Williamson20077222012-12-07 13:43:50 -0700194 pci_iounmap(pdev, vdev->barmap[bar]);
195 pci_release_selected_regions(pdev, 1 << bar);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600196 vdev->barmap[bar] = NULL;
197 }
Alex Williamson20077222012-12-07 13:43:50 -0700198
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600199 vdev->needs_reset = true;
200
Alex Williamson20077222012-12-07 13:43:50 -0700201 /*
202 * If we have saved state, restore it. If we can reset the device,
203 * even better. Resetting with current state seems better than
204 * nothing, but saving and restoring current state without reset
205 * is just busy work.
206 */
207 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
208 pr_info("%s: Couldn't reload %s saved state\n",
209 __func__, dev_name(&pdev->dev));
210
211 if (!vdev->reset_works)
Alex Williamson9c22e662014-08-07 11:12:02 -0600212 goto out;
Alex Williamson20077222012-12-07 13:43:50 -0700213
214 pci_save_state(pdev);
215 }
216
217 /*
218 * Disable INTx and MSI, presumably to avoid spurious interrupts
219 * during reset. Stolen from pci_reset_function()
220 */
221 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
222
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600223 /*
Alex Williamson890ed572014-01-14 20:45:09 -0700224 * Try to reset the device. The success of this is dependent on
225 * being able to lock the device, which is not always possible.
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600226 */
Alex Williamson561d72d2015-04-07 11:14:44 -0600227 if (vdev->reset_works && !pci_try_reset_function(pdev))
228 vdev->needs_reset = false;
Alex Williamson20077222012-12-07 13:43:50 -0700229
230 pci_restore_state(pdev);
Alex Williamson9c22e662014-08-07 11:12:02 -0600231out:
232 pci_disable_device(pdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600233
234 vfio_pci_try_bus_reset(vdev);
Alex Williamson6eb70182015-04-07 11:14:46 -0600235
236 if (!disable_idle_d3)
237 pci_set_power_state(pdev, PCI_D3hot);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600238}
239
240static void vfio_pci_release(void *device_data)
241{
242 struct vfio_pci_device *vdev = device_data;
243
Alex Williamson61d79252014-08-07 11:12:04 -0600244 mutex_lock(&driver_lock);
245
246 if (!(--vdev->refcnt)) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000247 vfio_spapr_pci_eeh_release(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600248 vfio_pci_disable(vdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000249 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600250
Alex Williamson61d79252014-08-07 11:12:04 -0600251 mutex_unlock(&driver_lock);
252
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600253 module_put(THIS_MODULE);
254}
255
256static int vfio_pci_open(void *device_data)
257{
258 struct vfio_pci_device *vdev = device_data;
Alex Williamson61d79252014-08-07 11:12:04 -0600259 int ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600260
261 if (!try_module_get(THIS_MODULE))
262 return -ENODEV;
263
Alex Williamson61d79252014-08-07 11:12:04 -0600264 mutex_lock(&driver_lock);
265
266 if (!vdev->refcnt) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000267 ret = vfio_pci_enable(vdev);
268 if (ret)
269 goto error;
270
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600271 vfio_spapr_pci_eeh_open(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600272 }
Alex Williamson61d79252014-08-07 11:12:04 -0600273 vdev->refcnt++;
Gavin Shan1b69be52014-06-10 11:41:57 +1000274error:
Alex Williamson61d79252014-08-07 11:12:04 -0600275 mutex_unlock(&driver_lock);
276 if (ret)
277 module_put(THIS_MODULE);
Gavin Shan1b69be52014-06-10 11:41:57 +1000278 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600279}
280
281static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
282{
283 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
284 u8 pin;
285 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700286 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600287 return 1;
288
289 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
290 u8 pos;
291 u16 flags;
292
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600293 pos = vdev->pdev->msi_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600294 if (pos) {
295 pci_read_config_word(vdev->pdev,
296 pos + PCI_MSI_FLAGS, &flags);
Gavin Shanfd49c81f2014-05-30 11:35:54 -0600297 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600298 }
299 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
300 u8 pos;
301 u16 flags;
302
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600303 pos = vdev->pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600304 if (pos) {
305 pci_read_config_word(vdev->pdev,
306 pos + PCI_MSIX_FLAGS, &flags);
307
308 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
309 }
Alex Williamson6140a8f2015-02-06 15:05:08 -0700310 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600311 if (pci_is_pcie(vdev->pdev))
312 return 1;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700313 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
314 return 1;
315 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600316
317 return 0;
318}
319
Alex Williamson8b27ee62013-09-04 11:28:04 -0600320static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
321{
322 (*(int *)data)++;
323 return 0;
324}
325
326struct vfio_pci_fill_info {
327 int max;
328 int cur;
329 struct vfio_pci_dependent_device *devices;
330};
331
332static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
333{
334 struct vfio_pci_fill_info *fill = data;
335 struct iommu_group *iommu_group;
336
337 if (fill->cur == fill->max)
338 return -EAGAIN; /* Something changed, try again */
339
340 iommu_group = iommu_group_get(&pdev->dev);
341 if (!iommu_group)
342 return -EPERM; /* Cannot reset non-isolated devices */
343
344 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
345 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
346 fill->devices[fill->cur].bus = pdev->bus->number;
347 fill->devices[fill->cur].devfn = pdev->devfn;
348 fill->cur++;
349 iommu_group_put(iommu_group);
350 return 0;
351}
352
353struct vfio_pci_group_entry {
354 struct vfio_group *group;
355 int id;
356};
357
358struct vfio_pci_group_info {
359 int count;
360 struct vfio_pci_group_entry *groups;
361};
362
363static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
364{
365 struct vfio_pci_group_info *info = data;
366 struct iommu_group *group;
367 int id, i;
368
369 group = iommu_group_get(&pdev->dev);
370 if (!group)
371 return -EPERM;
372
373 id = iommu_group_id(group);
374
375 for (i = 0; i < info->count; i++)
376 if (info->groups[i].id == id)
377 break;
378
379 iommu_group_put(group);
380
381 return (i == info->count) ? -EINVAL : 0;
382}
383
384static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
385{
386 for (; pdev; pdev = pdev->bus->self)
387 if (pdev->bus == slot->bus)
388 return (pdev->slot == slot);
389 return false;
390}
391
392struct vfio_pci_walk_info {
393 int (*fn)(struct pci_dev *, void *data);
394 void *data;
395 struct pci_dev *pdev;
396 bool slot;
397 int ret;
398};
399
400static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
401{
402 struct vfio_pci_walk_info *walk = data;
403
404 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
405 walk->ret = walk->fn(pdev, walk->data);
406
407 return walk->ret;
408}
409
410static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
411 int (*fn)(struct pci_dev *,
412 void *data), void *data,
413 bool slot)
414{
415 struct vfio_pci_walk_info walk = {
416 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
417 };
418
419 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
420
421 return walk.ret;
422}
423
Alex Williamson188ad9d2016-02-22 16:02:36 -0700424static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
425 struct vfio_info_cap *caps)
426{
427 struct vfio_info_cap_header *header;
428 struct vfio_region_info_cap_sparse_mmap *sparse;
429 size_t end, size;
430 int nr_areas = 2, i = 0;
431
432 end = pci_resource_len(vdev->pdev, vdev->msix_bar);
433
434 /* If MSI-X table is aligned to the start or end, only one area */
435 if (((vdev->msix_offset & PAGE_MASK) == 0) ||
436 (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
437 nr_areas = 1;
438
439 size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
440
441 header = vfio_info_cap_add(caps, size,
442 VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
443 if (IS_ERR(header))
444 return PTR_ERR(header);
445
446 sparse = container_of(header,
447 struct vfio_region_info_cap_sparse_mmap, header);
448 sparse->nr_areas = nr_areas;
449
450 if (vdev->msix_offset & PAGE_MASK) {
451 sparse->areas[i].offset = 0;
452 sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
453 i++;
454 }
455
456 if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
457 sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
458 vdev->msix_size);
459 sparse->areas[i].size = end - sparse->areas[i].offset;
460 i++;
461 }
462
463 return 0;
464}
465
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600466static long vfio_pci_ioctl(void *device_data,
467 unsigned int cmd, unsigned long arg)
468{
469 struct vfio_pci_device *vdev = device_data;
470 unsigned long minsz;
471
472 if (cmd == VFIO_DEVICE_GET_INFO) {
473 struct vfio_device_info info;
474
475 minsz = offsetofend(struct vfio_device_info, num_irqs);
476
477 if (copy_from_user(&info, (void __user *)arg, minsz))
478 return -EFAULT;
479
480 if (info.argsz < minsz)
481 return -EINVAL;
482
483 info.flags = VFIO_DEVICE_FLAGS_PCI;
484
485 if (vdev->reset_works)
486 info.flags |= VFIO_DEVICE_FLAGS_RESET;
487
488 info.num_regions = VFIO_PCI_NUM_REGIONS;
489 info.num_irqs = VFIO_PCI_NUM_IRQS;
490
491 return copy_to_user((void __user *)arg, &info, minsz);
492
493 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
494 struct pci_dev *pdev = vdev->pdev;
495 struct vfio_region_info info;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700496 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
497 int ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600498
499 minsz = offsetofend(struct vfio_region_info, offset);
500
501 if (copy_from_user(&info, (void __user *)arg, minsz))
502 return -EFAULT;
503
504 if (info.argsz < minsz)
505 return -EINVAL;
506
507 switch (info.index) {
508 case VFIO_PCI_CONFIG_REGION_INDEX:
509 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
510 info.size = pdev->cfg_size;
511 info.flags = VFIO_REGION_INFO_FLAG_READ |
512 VFIO_REGION_INFO_FLAG_WRITE;
513 break;
514 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
515 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
516 info.size = pci_resource_len(pdev, info.index);
517 if (!info.size) {
518 info.flags = 0;
519 break;
520 }
521
522 info.flags = VFIO_REGION_INFO_FLAG_READ |
523 VFIO_REGION_INFO_FLAG_WRITE;
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700524 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
525 pci_resource_flags(pdev, info.index) &
Alex Williamson188ad9d2016-02-22 16:02:36 -0700526 IORESOURCE_MEM && info.size >= PAGE_SIZE) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600527 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700528 if (info.index == vdev->msix_bar) {
529 ret = msix_sparse_mmap_cap(vdev, &caps);
530 if (ret)
531 return ret;
532 }
533 }
534
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600535 break;
536 case VFIO_PCI_ROM_REGION_INDEX:
537 {
538 void __iomem *io;
539 size_t size;
540
541 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
542 info.flags = 0;
543
544 /* Report the BAR size, not the ROM size */
545 info.size = pci_resource_len(pdev, info.index);
546 if (!info.size)
547 break;
548
549 /* Is it really there? */
550 io = pci_map_rom(pdev, &size);
551 if (!io || !size) {
552 info.size = 0;
553 break;
554 }
555 pci_unmap_rom(pdev, io);
556
557 info.flags = VFIO_REGION_INFO_FLAG_READ;
558 break;
559 }
Alex Williamson84237a82013-02-18 10:11:13 -0700560 case VFIO_PCI_VGA_REGION_INDEX:
561 if (!vdev->has_vga)
562 return -EINVAL;
563
564 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
565 info.size = 0xc0000;
566 info.flags = VFIO_REGION_INFO_FLAG_READ |
567 VFIO_REGION_INFO_FLAG_WRITE;
568
569 break;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600570 default:
571 return -EINVAL;
572 }
573
Alex Williamson188ad9d2016-02-22 16:02:36 -0700574 if (caps.size) {
575 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
576 if (info.argsz < sizeof(info) + caps.size) {
577 info.argsz = sizeof(info) + caps.size;
578 info.cap_offset = 0;
579 } else {
580 vfio_info_cap_shift(&caps, sizeof(info));
581 ret = copy_to_user((void __user *)arg +
582 sizeof(info), caps.buf,
583 caps.size);
584 if (ret) {
585 kfree(caps.buf);
586 return ret;
587 }
588 info.cap_offset = sizeof(info);
589 }
590
591 kfree(caps.buf);
592 }
593
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600594 return copy_to_user((void __user *)arg, &info, minsz);
595
596 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
597 struct vfio_irq_info info;
598
599 minsz = offsetofend(struct vfio_irq_info, count);
600
601 if (copy_from_user(&info, (void __user *)arg, minsz))
602 return -EFAULT;
603
604 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
605 return -EINVAL;
606
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600607 switch (info.index) {
608 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
Alex Williamson6140a8f2015-02-06 15:05:08 -0700609 case VFIO_PCI_REQ_IRQ_INDEX:
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600610 break;
611 case VFIO_PCI_ERR_IRQ_INDEX:
612 if (pci_is_pcie(vdev->pdev))
613 break;
614 /* pass thru to return error */
615 default:
616 return -EINVAL;
617 }
618
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600619 info.flags = VFIO_IRQ_INFO_EVENTFD;
620
621 info.count = vfio_pci_get_irq_count(vdev, info.index);
622
623 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
624 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
625 VFIO_IRQ_INFO_AUTOMASKED);
626 else
627 info.flags |= VFIO_IRQ_INFO_NORESIZE;
628
629 return copy_to_user((void __user *)arg, &info, minsz);
630
631 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
632 struct vfio_irq_set hdr;
633 u8 *data = NULL;
634 int ret = 0;
635
636 minsz = offsetofend(struct vfio_irq_set, count);
637
638 if (copy_from_user(&hdr, (void __user *)arg, minsz))
639 return -EFAULT;
640
641 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
642 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
643 VFIO_IRQ_SET_ACTION_TYPE_MASK))
644 return -EINVAL;
645
646 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
647 size_t size;
Alex Williamson904c6802013-03-26 11:33:16 -0600648 int max = vfio_pci_get_irq_count(vdev, hdr.index);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600649
650 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
651 size = sizeof(uint8_t);
652 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
653 size = sizeof(int32_t);
654 else
655 return -EINVAL;
656
657 if (hdr.argsz - minsz < hdr.count * size ||
Alex Williamson904c6802013-03-26 11:33:16 -0600658 hdr.start >= max || hdr.start + hdr.count > max)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600659 return -EINVAL;
660
Fengguang Wu3a1f7042012-12-07 13:43:49 -0700661 data = memdup_user((void __user *)(arg + minsz),
662 hdr.count * size);
663 if (IS_ERR(data))
664 return PTR_ERR(data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600665 }
666
667 mutex_lock(&vdev->igate);
668
669 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
670 hdr.start, hdr.count, data);
671
672 mutex_unlock(&vdev->igate);
673 kfree(data);
674
675 return ret;
676
Alex Williamson8b27ee62013-09-04 11:28:04 -0600677 } else if (cmd == VFIO_DEVICE_RESET) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600678 return vdev->reset_works ?
Alex Williamson890ed572014-01-14 20:45:09 -0700679 pci_try_reset_function(vdev->pdev) : -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600680
Alex Williamson8b27ee62013-09-04 11:28:04 -0600681 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
682 struct vfio_pci_hot_reset_info hdr;
683 struct vfio_pci_fill_info fill = { 0 };
684 struct vfio_pci_dependent_device *devices = NULL;
685 bool slot = false;
686 int ret = 0;
687
688 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
689
690 if (copy_from_user(&hdr, (void __user *)arg, minsz))
691 return -EFAULT;
692
693 if (hdr.argsz < minsz)
694 return -EINVAL;
695
696 hdr.flags = 0;
697
698 /* Can we do a slot or bus reset or neither? */
699 if (!pci_probe_reset_slot(vdev->pdev->slot))
700 slot = true;
701 else if (pci_probe_reset_bus(vdev->pdev->bus))
702 return -ENODEV;
703
704 /* How many devices are affected? */
705 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
706 vfio_pci_count_devs,
707 &fill.max, slot);
708 if (ret)
709 return ret;
710
711 WARN_ON(!fill.max); /* Should always be at least one */
712
713 /*
714 * If there's enough space, fill it now, otherwise return
715 * -ENOSPC and the number of devices affected.
716 */
717 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
718 ret = -ENOSPC;
719 hdr.count = fill.max;
720 goto reset_info_exit;
721 }
722
723 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
724 if (!devices)
725 return -ENOMEM;
726
727 fill.devices = devices;
728
729 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
730 vfio_pci_fill_devs,
731 &fill, slot);
732
733 /*
734 * If a device was removed between counting and filling,
735 * we may come up short of fill.max. If a device was
736 * added, we'll have a return of -EAGAIN above.
737 */
738 if (!ret)
739 hdr.count = fill.cur;
740
741reset_info_exit:
742 if (copy_to_user((void __user *)arg, &hdr, minsz))
743 ret = -EFAULT;
744
745 if (!ret) {
746 if (copy_to_user((void __user *)(arg + minsz), devices,
747 hdr.count * sizeof(*devices)))
748 ret = -EFAULT;
749 }
750
751 kfree(devices);
752 return ret;
753
754 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
755 struct vfio_pci_hot_reset hdr;
756 int32_t *group_fds;
757 struct vfio_pci_group_entry *groups;
758 struct vfio_pci_group_info info;
759 bool slot = false;
760 int i, count = 0, ret = 0;
761
762 minsz = offsetofend(struct vfio_pci_hot_reset, count);
763
764 if (copy_from_user(&hdr, (void __user *)arg, minsz))
765 return -EFAULT;
766
767 if (hdr.argsz < minsz || hdr.flags)
768 return -EINVAL;
769
770 /* Can we do a slot or bus reset or neither? */
771 if (!pci_probe_reset_slot(vdev->pdev->slot))
772 slot = true;
773 else if (pci_probe_reset_bus(vdev->pdev->bus))
774 return -ENODEV;
775
776 /*
777 * We can't let userspace give us an arbitrarily large
778 * buffer to copy, so verify how many we think there
779 * could be. Note groups can have multiple devices so
780 * one group per device is the max.
781 */
782 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
783 vfio_pci_count_devs,
784 &count, slot);
785 if (ret)
786 return ret;
787
788 /* Somewhere between 1 and count is OK */
789 if (!hdr.count || hdr.count > count)
790 return -EINVAL;
791
792 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
793 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
794 if (!group_fds || !groups) {
795 kfree(group_fds);
796 kfree(groups);
797 return -ENOMEM;
798 }
799
800 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
801 hdr.count * sizeof(*group_fds))) {
802 kfree(group_fds);
803 kfree(groups);
804 return -EFAULT;
805 }
806
807 /*
808 * For each group_fd, get the group through the vfio external
809 * user interface and store the group and iommu ID. This
810 * ensures the group is held across the reset.
811 */
812 for (i = 0; i < hdr.count; i++) {
813 struct vfio_group *group;
814 struct fd f = fdget(group_fds[i]);
815 if (!f.file) {
816 ret = -EBADF;
817 break;
818 }
819
820 group = vfio_group_get_external_user(f.file);
821 fdput(f);
822 if (IS_ERR(group)) {
823 ret = PTR_ERR(group);
824 break;
825 }
826
827 groups[i].group = group;
828 groups[i].id = vfio_external_user_iommu_id(group);
829 }
830
831 kfree(group_fds);
832
833 /* release reference to groups on error */
834 if (ret)
835 goto hot_reset_release;
836
837 info.count = hdr.count;
838 info.groups = groups;
839
840 /*
841 * Test whether all the affected devices are contained
842 * by the set of groups provided by the user.
843 */
844 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
845 vfio_pci_validate_devs,
846 &info, slot);
847 if (!ret)
848 /* User has access, do the reset */
Alex Williamson890ed572014-01-14 20:45:09 -0700849 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
850 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamson8b27ee62013-09-04 11:28:04 -0600851
852hot_reset_release:
853 for (i--; i >= 0; i--)
854 vfio_group_put_external_user(groups[i].group);
855
856 kfree(groups);
857 return ret;
858 }
859
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600860 return -ENOTTY;
861}
862
Alex Williamson5b279a12013-02-14 14:02:12 -0700863static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
864 size_t count, loff_t *ppos, bool iswrite)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600865{
866 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
867 struct vfio_pci_device *vdev = device_data;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600868
869 if (index >= VFIO_PCI_NUM_REGIONS)
870 return -EINVAL;
871
Alex Williamson5b279a12013-02-14 14:02:12 -0700872 switch (index) {
873 case VFIO_PCI_CONFIG_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700874 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
875
Alex Williamson5b279a12013-02-14 14:02:12 -0700876 case VFIO_PCI_ROM_REGION_INDEX:
877 if (iswrite)
878 return -EINVAL;
Alex Williamson906ee992013-02-14 14:02:12 -0700879 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600880
Alex Williamson5b279a12013-02-14 14:02:12 -0700881 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700882 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson84237a82013-02-18 10:11:13 -0700883
884 case VFIO_PCI_VGA_REGION_INDEX:
885 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson5b279a12013-02-14 14:02:12 -0700886 }
887
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600888 return -EINVAL;
889}
890
Alex Williamson5b279a12013-02-14 14:02:12 -0700891static ssize_t vfio_pci_read(void *device_data, char __user *buf,
892 size_t count, loff_t *ppos)
893{
Alex Williamson906ee992013-02-14 14:02:12 -0700894 if (!count)
895 return 0;
896
Alex Williamson5b279a12013-02-14 14:02:12 -0700897 return vfio_pci_rw(device_data, buf, count, ppos, false);
898}
899
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600900static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
901 size_t count, loff_t *ppos)
902{
Alex Williamson906ee992013-02-14 14:02:12 -0700903 if (!count)
904 return 0;
905
906 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600907}
908
909static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
910{
911 struct vfio_pci_device *vdev = device_data;
912 struct pci_dev *pdev = vdev->pdev;
913 unsigned int index;
Alex Williamson34002f52012-10-10 09:10:31 -0600914 u64 phys_len, req_len, pgoff, req_start;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600915 int ret;
916
917 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
918
919 if (vma->vm_end < vma->vm_start)
920 return -EINVAL;
921 if ((vma->vm_flags & VM_SHARED) == 0)
922 return -EINVAL;
923 if (index >= VFIO_PCI_ROM_REGION_INDEX)
924 return -EINVAL;
925 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
926 return -EINVAL;
927
928 phys_len = pci_resource_len(pdev, index);
929 req_len = vma->vm_end - vma->vm_start;
930 pgoff = vma->vm_pgoff &
931 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
932 req_start = pgoff << PAGE_SHIFT;
933
934 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
935 return -EINVAL;
936
937 if (index == vdev->msix_bar) {
938 /*
939 * Disallow mmaps overlapping the MSI-X table; users don't
940 * get to touch this directly. We could find somewhere
941 * else to map the overlap, but page granularity is only
942 * a recommendation, not a requirement, so the user needs
943 * to know which bits are real. Requiring them to mmap
944 * around the table makes that clear.
945 */
946
947 /* If neither entirely above nor below, then it overlaps */
948 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
949 req_start + req_len <= vdev->msix_offset))
950 return -EINVAL;
951 }
952
953 /*
954 * Even though we don't make use of the barmap for the mmap,
955 * we need to request the region and the barmap tracks that.
956 */
957 if (!vdev->barmap[index]) {
958 ret = pci_request_selected_regions(pdev,
959 1 << index, "vfio-pci");
960 if (ret)
961 return ret;
962
963 vdev->barmap[index] = pci_iomap(pdev, index, 0);
964 }
965
966 vma->vm_private_data = vdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600967 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Alex Williamson34002f52012-10-10 09:10:31 -0600968 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600969
Alex Williamson34002f52012-10-10 09:10:31 -0600970 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600971 req_len, vma->vm_page_prot);
972}
973
Alex Williamson6140a8f2015-02-06 15:05:08 -0700974static void vfio_pci_request(void *device_data, unsigned int count)
975{
976 struct vfio_pci_device *vdev = device_data;
977
978 mutex_lock(&vdev->igate);
979
980 if (vdev->req_trigger) {
Alex Williamson5f55d2a2015-04-28 10:23:30 -0600981 if (!(count % 10))
982 dev_notice_ratelimited(&vdev->pdev->dev,
983 "Relaying device request to user (#%u)\n",
984 count);
Alex Williamson6140a8f2015-02-06 15:05:08 -0700985 eventfd_signal(vdev->req_trigger, 1);
Alex Williamson5f55d2a2015-04-28 10:23:30 -0600986 } else if (count == 0) {
987 dev_warn(&vdev->pdev->dev,
988 "No device request channel registered, blocked until released by user\n");
Alex Williamson6140a8f2015-02-06 15:05:08 -0700989 }
990
991 mutex_unlock(&vdev->igate);
992}
993
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600994static const struct vfio_device_ops vfio_pci_ops = {
995 .name = "vfio-pci",
996 .open = vfio_pci_open,
997 .release = vfio_pci_release,
998 .ioctl = vfio_pci_ioctl,
999 .read = vfio_pci_read,
1000 .write = vfio_pci_write,
1001 .mmap = vfio_pci_mmap,
Alex Williamson6140a8f2015-02-06 15:05:08 -07001002 .request = vfio_pci_request,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001003};
1004
1005static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1006{
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001007 struct vfio_pci_device *vdev;
1008 struct iommu_group *group;
1009 int ret;
1010
Wei Yang7c2e2112015-01-07 10:29:11 -07001011 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001012 return -EINVAL;
1013
Alex Williamson03a76b62015-12-21 15:13:33 -07001014 group = vfio_iommu_group_get(&pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001015 if (!group)
1016 return -EINVAL;
1017
1018 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1019 if (!vdev) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001020 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001021 return -ENOMEM;
1022 }
1023
1024 vdev->pdev = pdev;
1025 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1026 mutex_init(&vdev->igate);
1027 spin_lock_init(&vdev->irqlock);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001028
1029 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1030 if (ret) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001031 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001032 kfree(vdev);
Alex Williamson5a0ff172015-04-08 08:11:51 -06001033 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001034 }
1035
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001036 if (vfio_pci_is_vga(pdev)) {
1037 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1038 vga_set_legacy_decoding(pdev,
1039 vfio_pci_set_vga_decode(vdev, false));
1040 }
1041
Alex Williamson6eb70182015-04-07 11:14:46 -06001042 if (!disable_idle_d3) {
1043 /*
1044 * pci-core sets the device power state to an unknown value at
1045 * bootup and after being removed from a driver. The only
1046 * transition it allows from this unknown state is to D0, which
1047 * typically happens when a driver calls pci_enable_device().
1048 * We're not ready to enable the device yet, but we do want to
1049 * be able to get to D3. Therefore first do a D0 transition
1050 * before going to D3.
1051 */
1052 pci_set_power_state(pdev, PCI_D0);
1053 pci_set_power_state(pdev, PCI_D3hot);
1054 }
1055
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001056 return ret;
1057}
1058
1059static void vfio_pci_remove(struct pci_dev *pdev)
1060{
1061 struct vfio_pci_device *vdev;
1062
Alex Williamson61d79252014-08-07 11:12:04 -06001063 vdev = vfio_del_group_dev(&pdev->dev);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001064 if (!vdev)
1065 return;
1066
Alex Williamson03a76b62015-12-21 15:13:33 -07001067 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001068 kfree(vdev);
1069
1070 if (vfio_pci_is_vga(pdev)) {
1071 vga_client_register(pdev, NULL, NULL, NULL);
1072 vga_set_legacy_decoding(pdev,
1073 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1074 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
Alex Williamson61d79252014-08-07 11:12:04 -06001075 }
Alex Williamson6eb70182015-04-07 11:14:46 -06001076
1077 if (!disable_idle_d3)
1078 pci_set_power_state(pdev, PCI_D0);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001079}
1080
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001081static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1082 pci_channel_state_t state)
1083{
1084 struct vfio_pci_device *vdev;
1085 struct vfio_device *device;
1086
1087 device = vfio_device_get_from_dev(&pdev->dev);
1088 if (device == NULL)
1089 return PCI_ERS_RESULT_DISCONNECT;
1090
1091 vdev = vfio_device_data(device);
1092 if (vdev == NULL) {
1093 vfio_device_put(device);
1094 return PCI_ERS_RESULT_DISCONNECT;
1095 }
1096
Alex Williamson3be3a072014-01-14 16:12:55 -07001097 mutex_lock(&vdev->igate);
1098
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001099 if (vdev->err_trigger)
1100 eventfd_signal(vdev->err_trigger, 1);
1101
Alex Williamson3be3a072014-01-14 16:12:55 -07001102 mutex_unlock(&vdev->igate);
1103
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001104 vfio_device_put(device);
1105
1106 return PCI_ERS_RESULT_CAN_RECOVER;
1107}
1108
Julia Lawall7d10f4e02015-11-14 11:07:01 +01001109static const struct pci_error_handlers vfio_err_handlers = {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001110 .error_detected = vfio_pci_aer_err_detected,
1111};
1112
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001113static struct pci_driver vfio_pci_driver = {
1114 .name = "vfio-pci",
1115 .id_table = NULL, /* only dynamic ids */
1116 .probe = vfio_pci_probe,
1117 .remove = vfio_pci_remove,
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001118 .err_handler = &vfio_err_handlers,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001119};
1120
Alex Williamson93899a62014-09-29 17:18:39 -06001121struct vfio_devices {
1122 struct vfio_device **devices;
1123 int cur_index;
1124 int max_index;
1125};
1126
1127static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001128{
Alex Williamson93899a62014-09-29 17:18:39 -06001129 struct vfio_devices *devs = data;
Alex Williamson20f30012015-06-09 10:08:57 -06001130 struct vfio_device *device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001131
Alex Williamson93899a62014-09-29 17:18:39 -06001132 if (devs->cur_index == devs->max_index)
1133 return -ENOSPC;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001134
Alex Williamson20f30012015-06-09 10:08:57 -06001135 device = vfio_device_get_from_dev(&pdev->dev);
1136 if (!device)
Alex Williamson93899a62014-09-29 17:18:39 -06001137 return -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001138
Alex Williamson20f30012015-06-09 10:08:57 -06001139 if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1140 vfio_device_put(device);
1141 return -EBUSY;
1142 }
1143
1144 devs->devices[devs->cur_index++] = device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001145 return 0;
1146}
1147
1148/*
1149 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1150 * this device that are needs_reset and all of the affected devices are unused
Alex Williamson93899a62014-09-29 17:18:39 -06001151 * (!refcnt). Callers are required to hold driver_lock when calling this to
1152 * prevent device opens and concurrent bus reset attempts. We prevent device
1153 * unbinds by acquiring and holding a reference to the vfio_device.
1154 *
1155 * NB: vfio-core considers a group to be viable even if some devices are
1156 * bound to drivers like pci-stub or pcieport. Here we require all devices
1157 * to be bound to vfio_pci since that's the only way we can be sure they
1158 * stay put.
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001159 */
1160static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1161{
Alex Williamson93899a62014-09-29 17:18:39 -06001162 struct vfio_devices devs = { .cur_index = 0 };
1163 int i = 0, ret = -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001164 bool needs_reset = false, slot = false;
Alex Williamson93899a62014-09-29 17:18:39 -06001165 struct vfio_pci_device *tmp;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001166
1167 if (!pci_probe_reset_slot(vdev->pdev->slot))
1168 slot = true;
1169 else if (pci_probe_reset_bus(vdev->pdev->bus))
1170 return;
1171
Alex Williamson93899a62014-09-29 17:18:39 -06001172 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1173 &i, slot) || !i)
1174 return;
1175
1176 devs.max_index = i;
1177 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1178 if (!devs.devices)
1179 return;
1180
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001181 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
Alex Williamson93899a62014-09-29 17:18:39 -06001182 vfio_pci_get_devs, &devs, slot))
1183 goto put_devs;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001184
Alex Williamson93899a62014-09-29 17:18:39 -06001185 for (i = 0; i < devs.cur_index; i++) {
1186 tmp = vfio_device_data(devs.devices[i]);
1187 if (tmp->needs_reset)
1188 needs_reset = true;
1189 if (tmp->refcnt)
1190 goto put_devs;
1191 }
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001192
Alex Williamson93899a62014-09-29 17:18:39 -06001193 if (needs_reset)
1194 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1195 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001196
Alex Williamson93899a62014-09-29 17:18:39 -06001197put_devs:
1198 for (i = 0; i < devs.cur_index; i++) {
Alex Williamson6eb70182015-04-07 11:14:46 -06001199 tmp = vfio_device_data(devs.devices[i]);
1200 if (!ret)
Alex Williamson93899a62014-09-29 17:18:39 -06001201 tmp->needs_reset = false;
Alex Williamson6eb70182015-04-07 11:14:46 -06001202
1203 if (!tmp->refcnt && !disable_idle_d3)
1204 pci_set_power_state(tmp->pdev, PCI_D3hot);
1205
Alex Williamson93899a62014-09-29 17:18:39 -06001206 vfio_device_put(devs.devices[i]);
1207 }
1208
1209 kfree(devs.devices);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001210}
1211
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001212static void __exit vfio_pci_cleanup(void)
1213{
1214 pci_unregister_driver(&vfio_pci_driver);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001215 vfio_pci_uninit_perm_bits();
1216}
1217
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001218static void __init vfio_pci_fill_ids(void)
1219{
1220 char *p, *id;
1221 int rc;
1222
1223 /* no ids passed actually */
1224 if (ids[0] == '\0')
1225 return;
1226
1227 /* add ids specified in the module parameter */
1228 p = ids;
1229 while ((id = strsep(&p, ","))) {
1230 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1231 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1232 int fields;
1233
1234 if (!strlen(id))
1235 continue;
1236
1237 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1238 &vendor, &device, &subvendor, &subdevice,
1239 &class, &class_mask);
1240
1241 if (fields < 2) {
1242 pr_warn("invalid id string \"%s\"\n", id);
1243 continue;
1244 }
1245
1246 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1247 subvendor, subdevice, class, class_mask, 0);
1248 if (rc)
1249 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1250 vendor, device, subvendor, subdevice,
1251 class, class_mask, rc);
1252 else
1253 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1254 vendor, device, subvendor, subdevice,
1255 class, class_mask);
1256 }
1257}
1258
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001259static int __init vfio_pci_init(void)
1260{
1261 int ret;
1262
1263 /* Allocate shared config space permision data used by all devices */
1264 ret = vfio_pci_init_perm_bits();
1265 if (ret)
1266 return ret;
1267
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001268 /* Register and scan for devices */
1269 ret = pci_register_driver(&vfio_pci_driver);
1270 if (ret)
1271 goto out_driver;
1272
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001273 vfio_pci_fill_ids();
1274
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001275 return 0;
1276
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001277out_driver:
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001278 vfio_pci_uninit_perm_bits();
1279 return ret;
1280}
1281
1282module_init(vfio_pci_init);
1283module_exit(vfio_pci_cleanup);
1284
1285MODULE_VERSION(DRIVER_VERSION);
1286MODULE_LICENSE("GPL v2");
1287MODULE_AUTHOR(DRIVER_AUTHOR);
1288MODULE_DESCRIPTION(DRIVER_DESC);