blob: 7338e43faa17e8d8f7a14b51a478c2a347d2a0f1 [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>
Gustavo A. R. Silva40974672018-07-17 12:39:00 -050031#include <linux/nospec.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060032
33#include "vfio_pci_private.h"
34
35#define DRIVER_VERSION "0.2"
36#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
37#define DRIVER_DESC "VFIO PCI - User Level meta-driver"
38
Alex Williamson80c7e8c2015-04-07 11:14:43 -060039static char ids[1024] __initdata;
40module_param_string(ids, ids, sizeof(ids), 0);
41MODULE_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");
42
Alex Williamson89e1f7d2012-07-31 08:16:24 -060043static bool nointxmask;
44module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
45MODULE_PARM_DESC(nointxmask,
46 "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.");
47
Alex Williamson88c0dead2015-04-07 11:14:40 -060048#ifdef CONFIG_VFIO_PCI_VGA
49static bool disable_vga;
50module_param(disable_vga, bool, S_IRUGO);
51MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
52#endif
53
Alex Williamson6eb70182015-04-07 11:14:46 -060054static bool disable_idle_d3;
55module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(disable_idle_d3,
57 "Disable using the PCI D3 low power state for idle, unused devices");
58
Alex Williamson61d79252014-08-07 11:12:04 -060059static DEFINE_MUTEX(driver_lock);
60
Alex Williamson88c0dead2015-04-07 11:14:40 -060061static inline bool vfio_vga_disabled(void)
62{
63#ifdef CONFIG_VFIO_PCI_VGA
64 return disable_vga;
65#else
66 return true;
67#endif
68}
69
Alex Williamsonecaa1f62015-04-07 11:14:41 -060070/*
71 * Our VGA arbiter participation is limited since we don't know anything
72 * about the device itself. However, if the device is the only VGA device
73 * downstream of a bridge and VFIO VGA support is disabled, then we can
74 * safely return legacy VGA IO and memory as not decoded since the user
75 * has no way to get to it and routing can be disabled externally at the
76 * bridge.
77 */
78static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
79{
80 struct vfio_pci_device *vdev = opaque;
81 struct pci_dev *tmp = NULL, *pdev = vdev->pdev;
82 unsigned char max_busnr;
83 unsigned int decodes;
84
85 if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
86 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
87 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
88
89 max_busnr = pci_bus_max_busnr(pdev->bus);
90 decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
91
92 while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
93 if (tmp == pdev ||
94 pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
95 pci_is_root_bus(tmp->bus))
96 continue;
97
98 if (tmp->bus->number >= pdev->bus->number &&
99 tmp->bus->number <= max_busnr) {
100 pci_dev_put(tmp);
101 decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
102 break;
103 }
104 }
105
106 return decodes;
107}
108
109static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
110{
111 return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
112}
113
Yongji Xie05f0c032016-06-30 15:21:24 +0800114static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev)
115{
116 struct resource *res;
117 int bar;
118 struct vfio_pci_dummy_resource *dummy_res;
119
120 INIT_LIST_HEAD(&vdev->dummy_resources_list);
121
122 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
123 res = vdev->pdev->resource + bar;
124
125 if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
126 goto no_mmap;
127
128 if (!(res->flags & IORESOURCE_MEM))
129 goto no_mmap;
130
131 /*
132 * The PCI core shouldn't set up a resource with a
133 * type but zero size. But there may be bugs that
134 * cause us to do that.
135 */
136 if (!resource_size(res))
137 goto no_mmap;
138
139 if (resource_size(res) >= PAGE_SIZE) {
140 vdev->bar_mmap_supported[bar] = true;
141 continue;
142 }
143
144 if (!(res->start & ~PAGE_MASK)) {
145 /*
146 * Add a dummy resource to reserve the remainder
147 * of the exclusive page in case that hot-add
148 * device's bar is assigned into it.
149 */
150 dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL);
151 if (dummy_res == NULL)
152 goto no_mmap;
153
154 dummy_res->resource.name = "vfio sub-page reserved";
155 dummy_res->resource.start = res->end + 1;
156 dummy_res->resource.end = res->start + PAGE_SIZE - 1;
157 dummy_res->resource.flags = res->flags;
158 if (request_resource(res->parent,
159 &dummy_res->resource)) {
160 kfree(dummy_res);
161 goto no_mmap;
162 }
163 dummy_res->index = bar;
164 list_add(&dummy_res->res_next,
165 &vdev->dummy_resources_list);
166 vdev->bar_mmap_supported[bar] = true;
167 continue;
168 }
169 /*
170 * Here we don't handle the case when the BAR is not page
171 * aligned because we can't expect the BAR will be
172 * assigned into the same location in a page in guest
173 * when we passthrough the BAR. And it's hard to access
174 * this BAR in userspace because we have no way to get
175 * the BAR's location in a page.
176 */
177no_mmap:
178 vdev->bar_mmap_supported[bar] = false;
179 }
180}
181
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600182static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
Alex Williamsonf572a962016-02-22 16:02:45 -0700183static void vfio_pci_disable(struct vfio_pci_device *vdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600184
Alex Williamson45074402016-03-24 13:05:18 -0600185/*
186 * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
187 * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
188 * If a device implements the former but not the latter we would typically
189 * expect broken_intx_masking be set and require an exclusive interrupt.
190 * However since we do have control of the device's ability to assert INTx,
191 * we can instead pretend that the device does not implement INTx, virtualizing
192 * the pin register to report zero and maintaining DisINTx set on the host.
193 */
194static bool vfio_pci_nointx(struct pci_dev *pdev)
195{
196 switch (pdev->vendor) {
197 case PCI_VENDOR_ID_INTEL:
198 switch (pdev->device) {
199 /* All i40e (XL710/X710) 10/20/40GbE NICs */
200 case 0x1572:
201 case 0x1574:
202 case 0x1580 ... 0x1581:
203 case 0x1583 ... 0x1589:
204 case 0x37d0 ... 0x37d2:
205 return true;
206 default:
207 return false;
208 }
209 }
210
211 return false;
212}
213
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600214static int vfio_pci_enable(struct vfio_pci_device *vdev)
215{
216 struct pci_dev *pdev = vdev->pdev;
217 int ret;
218 u16 cmd;
219 u8 msix_pos;
220
Alex Williamson6eb70182015-04-07 11:14:46 -0600221 pci_set_power_state(pdev, PCI_D0);
222
Alex Williamson9c22e662014-08-07 11:12:02 -0600223 /* Don't allow our initial saved state to include busmaster */
224 pci_clear_master(pdev);
225
Alex Williamson9a92c502012-12-07 13:43:51 -0700226 ret = pci_enable_device(pdev);
227 if (ret)
228 return ret;
229
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600230 vdev->reset_works = (pci_reset_function(pdev) == 0);
231 pci_save_state(pdev);
232 vdev->pci_saved_state = pci_store_saved_state(pdev);
233 if (!vdev->pci_saved_state)
234 pr_debug("%s: Couldn't store %s saved state\n",
235 __func__, dev_name(&pdev->dev));
236
Alex Williamson45074402016-03-24 13:05:18 -0600237 if (likely(!nointxmask)) {
238 if (vfio_pci_nointx(pdev)) {
239 dev_info(&pdev->dev, "Masking broken INTx support\n");
240 vdev->nointx = true;
241 pci_intx(pdev, 0);
242 } else
243 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
244 }
245
246 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
247 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
248 cmd &= ~PCI_COMMAND_INTX_DISABLE;
249 pci_write_config_word(pdev, PCI_COMMAND, cmd);
250 }
251
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600252 ret = vfio_config_init(vdev);
Alex Williamson9a92c502012-12-07 13:43:51 -0700253 if (ret) {
Alex Williamsoneb5685f2014-05-30 11:35:53 -0600254 kfree(vdev->pci_saved_state);
255 vdev->pci_saved_state = NULL;
Alex Williamson9a92c502012-12-07 13:43:51 -0700256 pci_disable_device(pdev);
257 return ret;
258 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600259
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600260 msix_pos = pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600261 if (msix_pos) {
262 u16 flags;
263 u32 table;
264
265 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
266 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
267
Bjorn Helgaas508d1aa2013-04-18 12:42:58 -0600268 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
269 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600270 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
271 } else
272 vdev->msix_bar = 0xFF;
273
Alex Williamsonecaa1f62015-04-07 11:14:41 -0600274 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
Alex Williamson84237a82013-02-18 10:11:13 -0700275 vdev->has_vga = true;
Alex Williamson84237a82013-02-18 10:11:13 -0700276
Alex Williamson5846ff52016-02-22 16:02:43 -0700277
Alex Williamsonf572a962016-02-22 16:02:45 -0700278 if (vfio_pci_is_vga(pdev) &&
279 pdev->vendor == PCI_VENDOR_ID_INTEL &&
280 IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
281 ret = vfio_pci_igd_init(vdev);
282 if (ret) {
283 dev_warn(&vdev->pdev->dev,
284 "Failed to setup Intel IGD regions\n");
285 vfio_pci_disable(vdev);
286 return ret;
287 }
Alex Williamson5846ff52016-02-22 16:02:43 -0700288 }
289
Yongji Xie05f0c032016-06-30 15:21:24 +0800290 vfio_pci_probe_mmaps(vdev);
291
Alex Williamson9a92c502012-12-07 13:43:51 -0700292 return 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600293}
294
295static void vfio_pci_disable(struct vfio_pci_device *vdev)
296{
Alex Williamson20077222012-12-07 13:43:50 -0700297 struct pci_dev *pdev = vdev->pdev;
Yongji Xie05f0c032016-06-30 15:21:24 +0800298 struct vfio_pci_dummy_resource *dummy_res, *tmp;
Alex Williamson28541d42016-02-22 16:02:39 -0700299 int i, bar;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600300
Alex Williamson9c22e662014-08-07 11:12:02 -0600301 /* Stop the device from further DMA */
302 pci_clear_master(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600303
304 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
305 VFIO_IRQ_SET_ACTION_TRIGGER,
306 vdev->irq_type, 0, 0, NULL);
307
308 vdev->virq_disabled = false;
309
Alex Williamson28541d42016-02-22 16:02:39 -0700310 for (i = 0; i < vdev->num_regions; i++)
311 vdev->region[i].ops->release(vdev, &vdev->region[i]);
312
313 vdev->num_regions = 0;
314 kfree(vdev->region);
315 vdev->region = NULL; /* don't krealloc a freed pointer */
316
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600317 vfio_config_free(vdev);
318
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600319 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
320 if (!vdev->barmap[bar])
321 continue;
Alex Williamson20077222012-12-07 13:43:50 -0700322 pci_iounmap(pdev, vdev->barmap[bar]);
323 pci_release_selected_regions(pdev, 1 << bar);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600324 vdev->barmap[bar] = NULL;
325 }
Alex Williamson20077222012-12-07 13:43:50 -0700326
Yongji Xie05f0c032016-06-30 15:21:24 +0800327 list_for_each_entry_safe(dummy_res, tmp,
328 &vdev->dummy_resources_list, res_next) {
329 list_del(&dummy_res->res_next);
330 release_resource(&dummy_res->resource);
331 kfree(dummy_res);
332 }
333
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600334 vdev->needs_reset = true;
335
Alex Williamson20077222012-12-07 13:43:50 -0700336 /*
337 * If we have saved state, restore it. If we can reset the device,
338 * even better. Resetting with current state seems better than
339 * nothing, but saving and restoring current state without reset
340 * is just busy work.
341 */
342 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
343 pr_info("%s: Couldn't reload %s saved state\n",
344 __func__, dev_name(&pdev->dev));
345
346 if (!vdev->reset_works)
Alex Williamson9c22e662014-08-07 11:12:02 -0600347 goto out;
Alex Williamson20077222012-12-07 13:43:50 -0700348
349 pci_save_state(pdev);
350 }
351
352 /*
353 * Disable INTx and MSI, presumably to avoid spurious interrupts
354 * during reset. Stolen from pci_reset_function()
355 */
356 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
357
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600358 /*
Alex Williamson890ed572014-01-14 20:45:09 -0700359 * Try to reset the device. The success of this is dependent on
360 * being able to lock the device, which is not always possible.
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600361 */
Alex Williamson561d72d2015-04-07 11:14:44 -0600362 if (vdev->reset_works && !pci_try_reset_function(pdev))
363 vdev->needs_reset = false;
Alex Williamson20077222012-12-07 13:43:50 -0700364
365 pci_restore_state(pdev);
Alex Williamson9c22e662014-08-07 11:12:02 -0600366out:
367 pci_disable_device(pdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600368
369 vfio_pci_try_bus_reset(vdev);
Alex Williamson6eb70182015-04-07 11:14:46 -0600370
371 if (!disable_idle_d3)
372 pci_set_power_state(pdev, PCI_D3hot);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600373}
374
375static void vfio_pci_release(void *device_data)
376{
377 struct vfio_pci_device *vdev = device_data;
378
Alex Williamson61d79252014-08-07 11:12:04 -0600379 mutex_lock(&driver_lock);
380
381 if (!(--vdev->refcnt)) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000382 vfio_spapr_pci_eeh_release(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600383 vfio_pci_disable(vdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000384 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600385
Alex Williamson61d79252014-08-07 11:12:04 -0600386 mutex_unlock(&driver_lock);
387
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600388 module_put(THIS_MODULE);
389}
390
391static int vfio_pci_open(void *device_data)
392{
393 struct vfio_pci_device *vdev = device_data;
Alex Williamson61d79252014-08-07 11:12:04 -0600394 int ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600395
396 if (!try_module_get(THIS_MODULE))
397 return -ENODEV;
398
Alex Williamson61d79252014-08-07 11:12:04 -0600399 mutex_lock(&driver_lock);
400
401 if (!vdev->refcnt) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000402 ret = vfio_pci_enable(vdev);
403 if (ret)
404 goto error;
405
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600406 vfio_spapr_pci_eeh_open(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600407 }
Alex Williamson61d79252014-08-07 11:12:04 -0600408 vdev->refcnt++;
Gavin Shan1b69be52014-06-10 11:41:57 +1000409error:
Alex Williamson61d79252014-08-07 11:12:04 -0600410 mutex_unlock(&driver_lock);
411 if (ret)
412 module_put(THIS_MODULE);
Gavin Shan1b69be52014-06-10 11:41:57 +1000413 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600414}
415
416static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
417{
418 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
419 u8 pin;
420 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
Alex Williamson45074402016-03-24 13:05:18 -0600421 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && !vdev->nointx && pin)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600422 return 1;
423
424 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
425 u8 pos;
426 u16 flags;
427
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600428 pos = vdev->pdev->msi_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600429 if (pos) {
430 pci_read_config_word(vdev->pdev,
431 pos + PCI_MSI_FLAGS, &flags);
Gavin Shanfd49c812014-05-30 11:35:54 -0600432 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600433 }
434 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
435 u8 pos;
436 u16 flags;
437
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600438 pos = vdev->pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600439 if (pos) {
440 pci_read_config_word(vdev->pdev,
441 pos + PCI_MSIX_FLAGS, &flags);
442
443 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
444 }
Alex Williamson6140a8f2015-02-06 15:05:08 -0700445 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600446 if (pci_is_pcie(vdev->pdev))
447 return 1;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700448 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
449 return 1;
450 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600451
452 return 0;
453}
454
Alex Williamson8b27ee62013-09-04 11:28:04 -0600455static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
456{
457 (*(int *)data)++;
458 return 0;
459}
460
461struct vfio_pci_fill_info {
462 int max;
463 int cur;
464 struct vfio_pci_dependent_device *devices;
465};
466
467static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
468{
469 struct vfio_pci_fill_info *fill = data;
470 struct iommu_group *iommu_group;
471
472 if (fill->cur == fill->max)
473 return -EAGAIN; /* Something changed, try again */
474
475 iommu_group = iommu_group_get(&pdev->dev);
476 if (!iommu_group)
477 return -EPERM; /* Cannot reset non-isolated devices */
478
479 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
480 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
481 fill->devices[fill->cur].bus = pdev->bus->number;
482 fill->devices[fill->cur].devfn = pdev->devfn;
483 fill->cur++;
484 iommu_group_put(iommu_group);
485 return 0;
486}
487
488struct vfio_pci_group_entry {
489 struct vfio_group *group;
490 int id;
491};
492
493struct vfio_pci_group_info {
494 int count;
495 struct vfio_pci_group_entry *groups;
496};
497
498static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
499{
500 struct vfio_pci_group_info *info = data;
501 struct iommu_group *group;
502 int id, i;
503
504 group = iommu_group_get(&pdev->dev);
505 if (!group)
506 return -EPERM;
507
508 id = iommu_group_id(group);
509
510 for (i = 0; i < info->count; i++)
511 if (info->groups[i].id == id)
512 break;
513
514 iommu_group_put(group);
515
516 return (i == info->count) ? -EINVAL : 0;
517}
518
519static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
520{
521 for (; pdev; pdev = pdev->bus->self)
522 if (pdev->bus == slot->bus)
523 return (pdev->slot == slot);
524 return false;
525}
526
527struct vfio_pci_walk_info {
528 int (*fn)(struct pci_dev *, void *data);
529 void *data;
530 struct pci_dev *pdev;
531 bool slot;
532 int ret;
533};
534
535static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
536{
537 struct vfio_pci_walk_info *walk = data;
538
539 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
540 walk->ret = walk->fn(pdev, walk->data);
541
542 return walk->ret;
543}
544
545static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
546 int (*fn)(struct pci_dev *,
547 void *data), void *data,
548 bool slot)
549{
550 struct vfio_pci_walk_info walk = {
551 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
552 };
553
554 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
555
556 return walk.ret;
557}
558
Alex Williamson188ad9d2016-02-22 16:02:36 -0700559static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
560 struct vfio_info_cap *caps)
561{
562 struct vfio_info_cap_header *header;
563 struct vfio_region_info_cap_sparse_mmap *sparse;
564 size_t end, size;
565 int nr_areas = 2, i = 0;
566
567 end = pci_resource_len(vdev->pdev, vdev->msix_bar);
568
569 /* If MSI-X table is aligned to the start or end, only one area */
570 if (((vdev->msix_offset & PAGE_MASK) == 0) ||
571 (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
572 nr_areas = 1;
573
574 size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
575
576 header = vfio_info_cap_add(caps, size,
577 VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
578 if (IS_ERR(header))
579 return PTR_ERR(header);
580
581 sparse = container_of(header,
582 struct vfio_region_info_cap_sparse_mmap, header);
583 sparse->nr_areas = nr_areas;
584
585 if (vdev->msix_offset & PAGE_MASK) {
586 sparse->areas[i].offset = 0;
587 sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
588 i++;
589 }
590
591 if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
592 sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
593 vdev->msix_size);
594 sparse->areas[i].size = end - sparse->areas[i].offset;
595 i++;
596 }
597
598 return 0;
599}
600
Alex Williamson28541d42016-02-22 16:02:39 -0700601static int region_type_cap(struct vfio_pci_device *vdev,
602 struct vfio_info_cap *caps,
603 unsigned int type, unsigned int subtype)
604{
605 struct vfio_info_cap_header *header;
606 struct vfio_region_info_cap_type *cap;
607
608 header = vfio_info_cap_add(caps, sizeof(*cap),
609 VFIO_REGION_INFO_CAP_TYPE, 1);
610 if (IS_ERR(header))
611 return PTR_ERR(header);
612
613 cap = container_of(header, struct vfio_region_info_cap_type, header);
614 cap->type = type;
615 cap->subtype = subtype;
616
617 return 0;
618}
619
620int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
621 unsigned int type, unsigned int subtype,
622 const struct vfio_pci_regops *ops,
623 size_t size, u32 flags, void *data)
624{
625 struct vfio_pci_region *region;
626
627 region = krealloc(vdev->region,
628 (vdev->num_regions + 1) * sizeof(*region),
629 GFP_KERNEL);
630 if (!region)
631 return -ENOMEM;
632
633 vdev->region = region;
634 vdev->region[vdev->num_regions].type = type;
635 vdev->region[vdev->num_regions].subtype = subtype;
636 vdev->region[vdev->num_regions].ops = ops;
637 vdev->region[vdev->num_regions].size = size;
638 vdev->region[vdev->num_regions].flags = flags;
639 vdev->region[vdev->num_regions].data = data;
640
641 vdev->num_regions++;
642
643 return 0;
644}
645
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600646static long vfio_pci_ioctl(void *device_data,
647 unsigned int cmd, unsigned long arg)
648{
649 struct vfio_pci_device *vdev = device_data;
650 unsigned long minsz;
651
652 if (cmd == VFIO_DEVICE_GET_INFO) {
653 struct vfio_device_info info;
654
655 minsz = offsetofend(struct vfio_device_info, num_irqs);
656
657 if (copy_from_user(&info, (void __user *)arg, minsz))
658 return -EFAULT;
659
660 if (info.argsz < minsz)
661 return -EINVAL;
662
663 info.flags = VFIO_DEVICE_FLAGS_PCI;
664
665 if (vdev->reset_works)
666 info.flags |= VFIO_DEVICE_FLAGS_RESET;
667
Alex Williamson28541d42016-02-22 16:02:39 -0700668 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600669 info.num_irqs = VFIO_PCI_NUM_IRQS;
670
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200671 return copy_to_user((void __user *)arg, &info, minsz) ?
672 -EFAULT : 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600673
674 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
675 struct pci_dev *pdev = vdev->pdev;
676 struct vfio_region_info info;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700677 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
Alex Williamson28541d42016-02-22 16:02:39 -0700678 int i, ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600679
680 minsz = offsetofend(struct vfio_region_info, offset);
681
682 if (copy_from_user(&info, (void __user *)arg, minsz))
683 return -EFAULT;
684
685 if (info.argsz < minsz)
686 return -EINVAL;
687
688 switch (info.index) {
689 case VFIO_PCI_CONFIG_REGION_INDEX:
690 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
691 info.size = pdev->cfg_size;
692 info.flags = VFIO_REGION_INFO_FLAG_READ |
693 VFIO_REGION_INFO_FLAG_WRITE;
694 break;
695 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
696 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
697 info.size = pci_resource_len(pdev, info.index);
698 if (!info.size) {
699 info.flags = 0;
700 break;
701 }
702
703 info.flags = VFIO_REGION_INFO_FLAG_READ |
704 VFIO_REGION_INFO_FLAG_WRITE;
Yongji Xie05f0c032016-06-30 15:21:24 +0800705 if (vdev->bar_mmap_supported[info.index]) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600706 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700707 if (info.index == vdev->msix_bar) {
708 ret = msix_sparse_mmap_cap(vdev, &caps);
709 if (ret)
710 return ret;
711 }
712 }
713
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600714 break;
715 case VFIO_PCI_ROM_REGION_INDEX:
716 {
717 void __iomem *io;
718 size_t size;
719
720 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
721 info.flags = 0;
722
723 /* Report the BAR size, not the ROM size */
724 info.size = pci_resource_len(pdev, info.index);
Alex Williamsona13b6452016-02-22 16:02:46 -0700725 if (!info.size) {
726 /* Shadow ROMs appear as PCI option ROMs */
727 if (pdev->resource[PCI_ROM_RESOURCE].flags &
728 IORESOURCE_ROM_SHADOW)
729 info.size = 0x20000;
730 else
731 break;
732 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600733
734 /* Is it really there? */
735 io = pci_map_rom(pdev, &size);
736 if (!io || !size) {
737 info.size = 0;
738 break;
739 }
740 pci_unmap_rom(pdev, io);
741
742 info.flags = VFIO_REGION_INFO_FLAG_READ;
743 break;
744 }
Alex Williamson84237a82013-02-18 10:11:13 -0700745 case VFIO_PCI_VGA_REGION_INDEX:
746 if (!vdev->has_vga)
747 return -EINVAL;
748
749 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
750 info.size = 0xc0000;
751 info.flags = VFIO_REGION_INFO_FLAG_READ |
752 VFIO_REGION_INFO_FLAG_WRITE;
753
754 break;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600755 default:
Alex Williamson28541d42016-02-22 16:02:39 -0700756 if (info.index >=
757 VFIO_PCI_NUM_REGIONS + vdev->num_regions)
758 return -EINVAL;
Gustavo A. R. Silva40974672018-07-17 12:39:00 -0500759 info.index = array_index_nospec(info.index,
760 VFIO_PCI_NUM_REGIONS +
761 vdev->num_regions);
Alex Williamson28541d42016-02-22 16:02:39 -0700762
763 i = info.index - VFIO_PCI_NUM_REGIONS;
764
765 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
766 info.size = vdev->region[i].size;
767 info.flags = vdev->region[i].flags;
768
769 ret = region_type_cap(vdev, &caps,
770 vdev->region[i].type,
771 vdev->region[i].subtype);
772 if (ret)
773 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600774 }
775
Alex Williamson188ad9d2016-02-22 16:02:36 -0700776 if (caps.size) {
777 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
778 if (info.argsz < sizeof(info) + caps.size) {
779 info.argsz = sizeof(info) + caps.size;
780 info.cap_offset = 0;
781 } else {
782 vfio_info_cap_shift(&caps, sizeof(info));
Dan Carpenterc4aec312016-02-25 10:52:12 +0300783 if (copy_to_user((void __user *)arg +
784 sizeof(info), caps.buf,
785 caps.size)) {
Alex Williamson188ad9d2016-02-22 16:02:36 -0700786 kfree(caps.buf);
Dan Carpenterc4aec312016-02-25 10:52:12 +0300787 return -EFAULT;
Alex Williamson188ad9d2016-02-22 16:02:36 -0700788 }
789 info.cap_offset = sizeof(info);
790 }
791
792 kfree(caps.buf);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600793 }
794
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200795 return copy_to_user((void __user *)arg, &info, minsz) ?
796 -EFAULT : 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600797
798 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
799 struct vfio_irq_info info;
800
801 minsz = offsetofend(struct vfio_irq_info, count);
802
803 if (copy_from_user(&info, (void __user *)arg, minsz))
804 return -EFAULT;
805
806 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
807 return -EINVAL;
808
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600809 switch (info.index) {
810 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
Alex Williamson6140a8f2015-02-06 15:05:08 -0700811 case VFIO_PCI_REQ_IRQ_INDEX:
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600812 break;
813 case VFIO_PCI_ERR_IRQ_INDEX:
814 if (pci_is_pcie(vdev->pdev))
815 break;
816 /* pass thru to return error */
817 default:
818 return -EINVAL;
819 }
820
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600821 info.flags = VFIO_IRQ_INFO_EVENTFD;
822
823 info.count = vfio_pci_get_irq_count(vdev, info.index);
824
825 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
826 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
827 VFIO_IRQ_INFO_AUTOMASKED);
828 else
829 info.flags |= VFIO_IRQ_INFO_NORESIZE;
830
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200831 return copy_to_user((void __user *)arg, &info, minsz) ?
832 -EFAULT : 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600833
834 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
835 struct vfio_irq_set hdr;
Vlad Tsyrklevich05692d72016-10-12 18:51:24 +0200836 size_t size;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600837 u8 *data = NULL;
Vlad Tsyrklevich05692d72016-10-12 18:51:24 +0200838 int max, ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600839
840 minsz = offsetofend(struct vfio_irq_set, count);
841
842 if (copy_from_user(&hdr, (void __user *)arg, minsz))
843 return -EFAULT;
844
845 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
Vlad Tsyrklevich05692d72016-10-12 18:51:24 +0200846 hdr.count >= (U32_MAX - hdr.start) ||
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600847 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
848 VFIO_IRQ_SET_ACTION_TYPE_MASK))
849 return -EINVAL;
850
Vlad Tsyrklevich05692d72016-10-12 18:51:24 +0200851 max = vfio_pci_get_irq_count(vdev, hdr.index);
852 if (hdr.start >= max || hdr.start + hdr.count > max)
853 return -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600854
Vlad Tsyrklevich05692d72016-10-12 18:51:24 +0200855 switch (hdr.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
856 case VFIO_IRQ_SET_DATA_NONE:
857 size = 0;
858 break;
859 case VFIO_IRQ_SET_DATA_BOOL:
860 size = sizeof(uint8_t);
861 break;
862 case VFIO_IRQ_SET_DATA_EVENTFD:
863 size = sizeof(int32_t);
864 break;
865 default:
866 return -EINVAL;
867 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600868
Vlad Tsyrklevich05692d72016-10-12 18:51:24 +0200869 if (size) {
870 if (hdr.argsz - minsz < hdr.count * size)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600871 return -EINVAL;
872
Fengguang Wu3a1f7042012-12-07 13:43:49 -0700873 data = memdup_user((void __user *)(arg + minsz),
874 hdr.count * size);
875 if (IS_ERR(data))
876 return PTR_ERR(data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600877 }
878
879 mutex_lock(&vdev->igate);
880
881 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
882 hdr.start, hdr.count, data);
883
884 mutex_unlock(&vdev->igate);
885 kfree(data);
886
887 return ret;
888
Alex Williamson8b27ee62013-09-04 11:28:04 -0600889 } else if (cmd == VFIO_DEVICE_RESET) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600890 return vdev->reset_works ?
Alex Williamson890ed572014-01-14 20:45:09 -0700891 pci_try_reset_function(vdev->pdev) : -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600892
Alex Williamson8b27ee62013-09-04 11:28:04 -0600893 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
894 struct vfio_pci_hot_reset_info hdr;
895 struct vfio_pci_fill_info fill = { 0 };
896 struct vfio_pci_dependent_device *devices = NULL;
897 bool slot = false;
898 int ret = 0;
899
900 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
901
902 if (copy_from_user(&hdr, (void __user *)arg, minsz))
903 return -EFAULT;
904
905 if (hdr.argsz < minsz)
906 return -EINVAL;
907
908 hdr.flags = 0;
909
910 /* Can we do a slot or bus reset or neither? */
911 if (!pci_probe_reset_slot(vdev->pdev->slot))
912 slot = true;
913 else if (pci_probe_reset_bus(vdev->pdev->bus))
914 return -ENODEV;
915
916 /* How many devices are affected? */
917 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
918 vfio_pci_count_devs,
919 &fill.max, slot);
920 if (ret)
921 return ret;
922
923 WARN_ON(!fill.max); /* Should always be at least one */
924
925 /*
926 * If there's enough space, fill it now, otherwise return
927 * -ENOSPC and the number of devices affected.
928 */
929 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
930 ret = -ENOSPC;
931 hdr.count = fill.max;
932 goto reset_info_exit;
933 }
934
935 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
936 if (!devices)
937 return -ENOMEM;
938
939 fill.devices = devices;
940
941 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
942 vfio_pci_fill_devs,
943 &fill, slot);
944
945 /*
946 * If a device was removed between counting and filling,
947 * we may come up short of fill.max. If a device was
948 * added, we'll have a return of -EAGAIN above.
949 */
950 if (!ret)
951 hdr.count = fill.cur;
952
953reset_info_exit:
954 if (copy_to_user((void __user *)arg, &hdr, minsz))
955 ret = -EFAULT;
956
957 if (!ret) {
958 if (copy_to_user((void __user *)(arg + minsz), devices,
959 hdr.count * sizeof(*devices)))
960 ret = -EFAULT;
961 }
962
963 kfree(devices);
964 return ret;
965
966 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
967 struct vfio_pci_hot_reset hdr;
968 int32_t *group_fds;
969 struct vfio_pci_group_entry *groups;
970 struct vfio_pci_group_info info;
971 bool slot = false;
972 int i, count = 0, ret = 0;
973
974 minsz = offsetofend(struct vfio_pci_hot_reset, count);
975
976 if (copy_from_user(&hdr, (void __user *)arg, minsz))
977 return -EFAULT;
978
979 if (hdr.argsz < minsz || hdr.flags)
980 return -EINVAL;
981
982 /* Can we do a slot or bus reset or neither? */
983 if (!pci_probe_reset_slot(vdev->pdev->slot))
984 slot = true;
985 else if (pci_probe_reset_bus(vdev->pdev->bus))
986 return -ENODEV;
987
988 /*
989 * We can't let userspace give us an arbitrarily large
990 * buffer to copy, so verify how many we think there
991 * could be. Note groups can have multiple devices so
992 * one group per device is the max.
993 */
994 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
995 vfio_pci_count_devs,
996 &count, slot);
997 if (ret)
998 return ret;
999
1000 /* Somewhere between 1 and count is OK */
1001 if (!hdr.count || hdr.count > count)
1002 return -EINVAL;
1003
1004 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
1005 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
1006 if (!group_fds || !groups) {
1007 kfree(group_fds);
1008 kfree(groups);
1009 return -ENOMEM;
1010 }
1011
1012 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
1013 hdr.count * sizeof(*group_fds))) {
1014 kfree(group_fds);
1015 kfree(groups);
1016 return -EFAULT;
1017 }
1018
1019 /*
1020 * For each group_fd, get the group through the vfio external
1021 * user interface and store the group and iommu ID. This
1022 * ensures the group is held across the reset.
1023 */
1024 for (i = 0; i < hdr.count; i++) {
1025 struct vfio_group *group;
1026 struct fd f = fdget(group_fds[i]);
1027 if (!f.file) {
1028 ret = -EBADF;
1029 break;
1030 }
1031
1032 group = vfio_group_get_external_user(f.file);
1033 fdput(f);
1034 if (IS_ERR(group)) {
1035 ret = PTR_ERR(group);
1036 break;
1037 }
1038
1039 groups[i].group = group;
1040 groups[i].id = vfio_external_user_iommu_id(group);
1041 }
1042
1043 kfree(group_fds);
1044
1045 /* release reference to groups on error */
1046 if (ret)
1047 goto hot_reset_release;
1048
1049 info.count = hdr.count;
1050 info.groups = groups;
1051
1052 /*
1053 * Test whether all the affected devices are contained
1054 * by the set of groups provided by the user.
1055 */
1056 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
1057 vfio_pci_validate_devs,
1058 &info, slot);
1059 if (!ret)
1060 /* User has access, do the reset */
Alex Williamson890ed572014-01-14 20:45:09 -07001061 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1062 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamson8b27ee62013-09-04 11:28:04 -06001063
1064hot_reset_release:
1065 for (i--; i >= 0; i--)
1066 vfio_group_put_external_user(groups[i].group);
1067
1068 kfree(groups);
1069 return ret;
1070 }
1071
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001072 return -ENOTTY;
1073}
1074
Alex Williamson5b279a12013-02-14 14:02:12 -07001075static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
1076 size_t count, loff_t *ppos, bool iswrite)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001077{
1078 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1079 struct vfio_pci_device *vdev = device_data;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001080
Alex Williamson28541d42016-02-22 16:02:39 -07001081 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001082 return -EINVAL;
1083
Alex Williamson5b279a12013-02-14 14:02:12 -07001084 switch (index) {
1085 case VFIO_PCI_CONFIG_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -07001086 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
1087
Alex Williamson5b279a12013-02-14 14:02:12 -07001088 case VFIO_PCI_ROM_REGION_INDEX:
1089 if (iswrite)
1090 return -EINVAL;
Alex Williamson906ee992013-02-14 14:02:12 -07001091 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001092
Alex Williamson5b279a12013-02-14 14:02:12 -07001093 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -07001094 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson84237a82013-02-18 10:11:13 -07001095
1096 case VFIO_PCI_VGA_REGION_INDEX:
1097 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson28541d42016-02-22 16:02:39 -07001098 default:
1099 index -= VFIO_PCI_NUM_REGIONS;
1100 return vdev->region[index].ops->rw(vdev, buf,
1101 count, ppos, iswrite);
Alex Williamson5b279a12013-02-14 14:02:12 -07001102 }
1103
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001104 return -EINVAL;
1105}
1106
Alex Williamson5b279a12013-02-14 14:02:12 -07001107static ssize_t vfio_pci_read(void *device_data, char __user *buf,
1108 size_t count, loff_t *ppos)
1109{
Alex Williamson906ee992013-02-14 14:02:12 -07001110 if (!count)
1111 return 0;
1112
Alex Williamson5b279a12013-02-14 14:02:12 -07001113 return vfio_pci_rw(device_data, buf, count, ppos, false);
1114}
1115
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001116static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
1117 size_t count, loff_t *ppos)
1118{
Alex Williamson906ee992013-02-14 14:02:12 -07001119 if (!count)
1120 return 0;
1121
1122 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001123}
1124
1125static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
1126{
1127 struct vfio_pci_device *vdev = device_data;
1128 struct pci_dev *pdev = vdev->pdev;
1129 unsigned int index;
Alex Williamson34002f52012-10-10 09:10:31 -06001130 u64 phys_len, req_len, pgoff, req_start;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001131 int ret;
1132
1133 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1134
1135 if (vma->vm_end < vma->vm_start)
1136 return -EINVAL;
1137 if ((vma->vm_flags & VM_SHARED) == 0)
1138 return -EINVAL;
1139 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1140 return -EINVAL;
Yongji Xie05f0c032016-06-30 15:21:24 +08001141 if (!vdev->bar_mmap_supported[index])
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001142 return -EINVAL;
1143
Yongji Xie05f0c032016-06-30 15:21:24 +08001144 phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001145 req_len = vma->vm_end - vma->vm_start;
1146 pgoff = vma->vm_pgoff &
1147 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1148 req_start = pgoff << PAGE_SHIFT;
1149
Yongji Xie05f0c032016-06-30 15:21:24 +08001150 if (req_start + req_len > phys_len)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001151 return -EINVAL;
1152
1153 if (index == vdev->msix_bar) {
1154 /*
1155 * Disallow mmaps overlapping the MSI-X table; users don't
1156 * get to touch this directly. We could find somewhere
1157 * else to map the overlap, but page granularity is only
1158 * a recommendation, not a requirement, so the user needs
1159 * to know which bits are real. Requiring them to mmap
1160 * around the table makes that clear.
1161 */
1162
1163 /* If neither entirely above nor below, then it overlaps */
1164 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
1165 req_start + req_len <= vdev->msix_offset))
1166 return -EINVAL;
1167 }
1168
1169 /*
1170 * Even though we don't make use of the barmap for the mmap,
1171 * we need to request the region and the barmap tracks that.
1172 */
1173 if (!vdev->barmap[index]) {
1174 ret = pci_request_selected_regions(pdev,
1175 1 << index, "vfio-pci");
1176 if (ret)
1177 return ret;
1178
1179 vdev->barmap[index] = pci_iomap(pdev, index, 0);
Arvind Yadav812a7df2017-01-03 17:26:46 +05301180 if (!vdev->barmap[index]) {
1181 pci_release_selected_regions(pdev, 1 << index);
1182 return -ENOMEM;
1183 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001184 }
1185
1186 vma->vm_private_data = vdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001187 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Alex Williamson34002f52012-10-10 09:10:31 -06001188 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001189
Alex Williamson34002f52012-10-10 09:10:31 -06001190 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001191 req_len, vma->vm_page_prot);
1192}
1193
Alex Williamson6140a8f2015-02-06 15:05:08 -07001194static void vfio_pci_request(void *device_data, unsigned int count)
1195{
1196 struct vfio_pci_device *vdev = device_data;
1197
1198 mutex_lock(&vdev->igate);
1199
1200 if (vdev->req_trigger) {
Alex Williamson5f55d2a2015-04-28 10:23:30 -06001201 if (!(count % 10))
1202 dev_notice_ratelimited(&vdev->pdev->dev,
1203 "Relaying device request to user (#%u)\n",
1204 count);
Alex Williamson6140a8f2015-02-06 15:05:08 -07001205 eventfd_signal(vdev->req_trigger, 1);
Alex Williamson5f55d2a2015-04-28 10:23:30 -06001206 } else if (count == 0) {
1207 dev_warn(&vdev->pdev->dev,
1208 "No device request channel registered, blocked until released by user\n");
Alex Williamson6140a8f2015-02-06 15:05:08 -07001209 }
1210
1211 mutex_unlock(&vdev->igate);
1212}
1213
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001214static const struct vfio_device_ops vfio_pci_ops = {
1215 .name = "vfio-pci",
1216 .open = vfio_pci_open,
1217 .release = vfio_pci_release,
1218 .ioctl = vfio_pci_ioctl,
1219 .read = vfio_pci_read,
1220 .write = vfio_pci_write,
1221 .mmap = vfio_pci_mmap,
Alex Williamson6140a8f2015-02-06 15:05:08 -07001222 .request = vfio_pci_request,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001223};
1224
1225static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1226{
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001227 struct vfio_pci_device *vdev;
1228 struct iommu_group *group;
1229 int ret;
1230
Wei Yang7c2e2112015-01-07 10:29:11 -07001231 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001232 return -EINVAL;
1233
Alex Williamson03a76b62015-12-21 15:13:33 -07001234 group = vfio_iommu_group_get(&pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001235 if (!group)
1236 return -EINVAL;
1237
1238 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1239 if (!vdev) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001240 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001241 return -ENOMEM;
1242 }
1243
1244 vdev->pdev = pdev;
1245 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1246 mutex_init(&vdev->igate);
1247 spin_lock_init(&vdev->irqlock);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001248
1249 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1250 if (ret) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001251 vfio_iommu_group_put(group, &pdev->dev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001252 kfree(vdev);
Alex Williamson5a0ff172015-04-08 08:11:51 -06001253 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001254 }
1255
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001256 if (vfio_pci_is_vga(pdev)) {
1257 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1258 vga_set_legacy_decoding(pdev,
1259 vfio_pci_set_vga_decode(vdev, false));
1260 }
1261
Alex Williamson6eb70182015-04-07 11:14:46 -06001262 if (!disable_idle_d3) {
1263 /*
1264 * pci-core sets the device power state to an unknown value at
1265 * bootup and after being removed from a driver. The only
1266 * transition it allows from this unknown state is to D0, which
1267 * typically happens when a driver calls pci_enable_device().
1268 * We're not ready to enable the device yet, but we do want to
1269 * be able to get to D3. Therefore first do a D0 transition
1270 * before going to D3.
1271 */
1272 pci_set_power_state(pdev, PCI_D0);
1273 pci_set_power_state(pdev, PCI_D3hot);
1274 }
1275
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001276 return ret;
1277}
1278
1279static void vfio_pci_remove(struct pci_dev *pdev)
1280{
1281 struct vfio_pci_device *vdev;
1282
Alex Williamson61d79252014-08-07 11:12:04 -06001283 vdev = vfio_del_group_dev(&pdev->dev);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001284 if (!vdev)
1285 return;
1286
Alex Williamson03a76b62015-12-21 15:13:33 -07001287 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
Alex Williamson28541d42016-02-22 16:02:39 -07001288 kfree(vdev->region);
Alex Williamsonecaa1f62015-04-07 11:14:41 -06001289 kfree(vdev);
1290
1291 if (vfio_pci_is_vga(pdev)) {
1292 vga_client_register(pdev, NULL, NULL, NULL);
1293 vga_set_legacy_decoding(pdev,
1294 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1295 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
Alex Williamson61d79252014-08-07 11:12:04 -06001296 }
Alex Williamson6eb70182015-04-07 11:14:46 -06001297
1298 if (!disable_idle_d3)
1299 pci_set_power_state(pdev, PCI_D0);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001300}
1301
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001302static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1303 pci_channel_state_t state)
1304{
1305 struct vfio_pci_device *vdev;
1306 struct vfio_device *device;
1307
1308 device = vfio_device_get_from_dev(&pdev->dev);
1309 if (device == NULL)
1310 return PCI_ERS_RESULT_DISCONNECT;
1311
1312 vdev = vfio_device_data(device);
1313 if (vdev == NULL) {
1314 vfio_device_put(device);
1315 return PCI_ERS_RESULT_DISCONNECT;
1316 }
1317
Alex Williamson3be3a072014-01-14 16:12:55 -07001318 mutex_lock(&vdev->igate);
1319
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001320 if (vdev->err_trigger)
1321 eventfd_signal(vdev->err_trigger, 1);
1322
Alex Williamson3be3a072014-01-14 16:12:55 -07001323 mutex_unlock(&vdev->igate);
1324
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001325 vfio_device_put(device);
1326
1327 return PCI_ERS_RESULT_CAN_RECOVER;
1328}
1329
Julia Lawall7d10f4e2015-11-14 11:07:01 +01001330static const struct pci_error_handlers vfio_err_handlers = {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001331 .error_detected = vfio_pci_aer_err_detected,
1332};
1333
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001334static struct pci_driver vfio_pci_driver = {
1335 .name = "vfio-pci",
1336 .id_table = NULL, /* only dynamic ids */
1337 .probe = vfio_pci_probe,
1338 .remove = vfio_pci_remove,
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001339 .err_handler = &vfio_err_handlers,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001340};
1341
Alex Williamson93899a62014-09-29 17:18:39 -06001342struct vfio_devices {
1343 struct vfio_device **devices;
1344 int cur_index;
1345 int max_index;
1346};
1347
1348static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001349{
Alex Williamson93899a62014-09-29 17:18:39 -06001350 struct vfio_devices *devs = data;
Alex Williamson20f30012015-06-09 10:08:57 -06001351 struct vfio_device *device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001352
Alex Williamson93899a62014-09-29 17:18:39 -06001353 if (devs->cur_index == devs->max_index)
1354 return -ENOSPC;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001355
Alex Williamson20f30012015-06-09 10:08:57 -06001356 device = vfio_device_get_from_dev(&pdev->dev);
1357 if (!device)
Alex Williamson93899a62014-09-29 17:18:39 -06001358 return -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001359
Alex Williamson20f30012015-06-09 10:08:57 -06001360 if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1361 vfio_device_put(device);
1362 return -EBUSY;
1363 }
1364
1365 devs->devices[devs->cur_index++] = device;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001366 return 0;
1367}
1368
1369/*
1370 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1371 * this device that are needs_reset and all of the affected devices are unused
Alex Williamson93899a62014-09-29 17:18:39 -06001372 * (!refcnt). Callers are required to hold driver_lock when calling this to
1373 * prevent device opens and concurrent bus reset attempts. We prevent device
1374 * unbinds by acquiring and holding a reference to the vfio_device.
1375 *
1376 * NB: vfio-core considers a group to be viable even if some devices are
1377 * bound to drivers like pci-stub or pcieport. Here we require all devices
1378 * to be bound to vfio_pci since that's the only way we can be sure they
1379 * stay put.
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001380 */
1381static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1382{
Alex Williamson93899a62014-09-29 17:18:39 -06001383 struct vfio_devices devs = { .cur_index = 0 };
1384 int i = 0, ret = -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001385 bool needs_reset = false, slot = false;
Alex Williamson93899a62014-09-29 17:18:39 -06001386 struct vfio_pci_device *tmp;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001387
1388 if (!pci_probe_reset_slot(vdev->pdev->slot))
1389 slot = true;
1390 else if (pci_probe_reset_bus(vdev->pdev->bus))
1391 return;
1392
Alex Williamson93899a62014-09-29 17:18:39 -06001393 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1394 &i, slot) || !i)
1395 return;
1396
1397 devs.max_index = i;
1398 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1399 if (!devs.devices)
1400 return;
1401
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001402 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
Alex Williamson93899a62014-09-29 17:18:39 -06001403 vfio_pci_get_devs, &devs, slot))
1404 goto put_devs;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001405
Alex Williamson93899a62014-09-29 17:18:39 -06001406 for (i = 0; i < devs.cur_index; i++) {
1407 tmp = vfio_device_data(devs.devices[i]);
1408 if (tmp->needs_reset)
1409 needs_reset = true;
1410 if (tmp->refcnt)
1411 goto put_devs;
1412 }
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001413
Alex Williamson93899a62014-09-29 17:18:39 -06001414 if (needs_reset)
1415 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1416 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001417
Alex Williamson93899a62014-09-29 17:18:39 -06001418put_devs:
1419 for (i = 0; i < devs.cur_index; i++) {
Alex Williamson6eb70182015-04-07 11:14:46 -06001420 tmp = vfio_device_data(devs.devices[i]);
1421 if (!ret)
Alex Williamson93899a62014-09-29 17:18:39 -06001422 tmp->needs_reset = false;
Alex Williamson6eb70182015-04-07 11:14:46 -06001423
1424 if (!tmp->refcnt && !disable_idle_d3)
1425 pci_set_power_state(tmp->pdev, PCI_D3hot);
1426
Alex Williamson93899a62014-09-29 17:18:39 -06001427 vfio_device_put(devs.devices[i]);
1428 }
1429
1430 kfree(devs.devices);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001431}
1432
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001433static void __exit vfio_pci_cleanup(void)
1434{
1435 pci_unregister_driver(&vfio_pci_driver);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001436 vfio_pci_uninit_perm_bits();
1437}
1438
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001439static void __init vfio_pci_fill_ids(void)
1440{
1441 char *p, *id;
1442 int rc;
1443
1444 /* no ids passed actually */
1445 if (ids[0] == '\0')
1446 return;
1447
1448 /* add ids specified in the module parameter */
1449 p = ids;
1450 while ((id = strsep(&p, ","))) {
1451 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1452 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1453 int fields;
1454
1455 if (!strlen(id))
1456 continue;
1457
1458 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1459 &vendor, &device, &subvendor, &subdevice,
1460 &class, &class_mask);
1461
1462 if (fields < 2) {
1463 pr_warn("invalid id string \"%s\"\n", id);
1464 continue;
1465 }
1466
1467 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1468 subvendor, subdevice, class, class_mask, 0);
1469 if (rc)
1470 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1471 vendor, device, subvendor, subdevice,
1472 class, class_mask, rc);
1473 else
1474 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1475 vendor, device, subvendor, subdevice,
1476 class, class_mask);
1477 }
1478}
1479
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001480static int __init vfio_pci_init(void)
1481{
1482 int ret;
1483
1484 /* Allocate shared config space permision data used by all devices */
1485 ret = vfio_pci_init_perm_bits();
1486 if (ret)
1487 return ret;
1488
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001489 /* Register and scan for devices */
1490 ret = pci_register_driver(&vfio_pci_driver);
1491 if (ret)
1492 goto out_driver;
1493
Alex Williamson80c7e8c2015-04-07 11:14:43 -06001494 vfio_pci_fill_ids();
1495
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001496 return 0;
1497
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001498out_driver:
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001499 vfio_pci_uninit_perm_bits();
1500 return ret;
1501}
1502
1503module_init(vfio_pci_init);
1504module_exit(vfio_pci_cleanup);
1505
1506MODULE_VERSION(DRIVER_VERSION);
1507MODULE_LICENSE("GPL v2");
1508MODULE_AUTHOR(DRIVER_AUTHOR);
1509MODULE_DESCRIPTION(DRIVER_DESC);