blob: 7053110e47340909c45f177d4f0d35d444d896b5 [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
14#include <linux/device.h>
15#include <linux/eventfd.h>
Alex Williamson8b27ee62013-09-04 11:28:04 -060016#include <linux/file.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060017#include <linux/interrupt.h>
18#include <linux/iommu.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21#include <linux/notifier.h>
22#include <linux/pci.h>
23#include <linux/pm_runtime.h>
24#include <linux/slab.h>
25#include <linux/types.h>
26#include <linux/uaccess.h>
27#include <linux/vfio.h>
28
29#include "vfio_pci_private.h"
30
31#define DRIVER_VERSION "0.2"
32#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
33#define DRIVER_DESC "VFIO PCI - User Level meta-driver"
34
35static bool nointxmask;
36module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(nointxmask,
38 "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.");
39
Alex Williamson88c0dead2015-04-07 11:14:40 -060040#ifdef CONFIG_VFIO_PCI_VGA
41static bool disable_vga;
42module_param(disable_vga, bool, S_IRUGO);
43MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
44#endif
45
Alex Williamson61d79252014-08-07 11:12:04 -060046static DEFINE_MUTEX(driver_lock);
47
Alex Williamson88c0dead2015-04-07 11:14:40 -060048static inline bool vfio_vga_disabled(void)
49{
50#ifdef CONFIG_VFIO_PCI_VGA
51 return disable_vga;
52#else
53 return true;
54#endif
55}
56
Alex Williamsonbc4fba72014-08-07 11:12:07 -060057static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
58
Alex Williamson89e1f7d2012-07-31 08:16:24 -060059static int vfio_pci_enable(struct vfio_pci_device *vdev)
60{
61 struct pci_dev *pdev = vdev->pdev;
62 int ret;
63 u16 cmd;
64 u8 msix_pos;
65
Alex Williamson9c22e662014-08-07 11:12:02 -060066 /* Don't allow our initial saved state to include busmaster */
67 pci_clear_master(pdev);
68
Alex Williamson9a92c502012-12-07 13:43:51 -070069 ret = pci_enable_device(pdev);
70 if (ret)
71 return ret;
72
Alex Williamson89e1f7d2012-07-31 08:16:24 -060073 vdev->reset_works = (pci_reset_function(pdev) == 0);
74 pci_save_state(pdev);
75 vdev->pci_saved_state = pci_store_saved_state(pdev);
76 if (!vdev->pci_saved_state)
77 pr_debug("%s: Couldn't store %s saved state\n",
78 __func__, dev_name(&pdev->dev));
79
80 ret = vfio_config_init(vdev);
Alex Williamson9a92c502012-12-07 13:43:51 -070081 if (ret) {
Alex Williamsoneb5685f2014-05-30 11:35:53 -060082 kfree(vdev->pci_saved_state);
83 vdev->pci_saved_state = NULL;
Alex Williamson9a92c502012-12-07 13:43:51 -070084 pci_disable_device(pdev);
85 return ret;
86 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -060087
88 if (likely(!nointxmask))
89 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
90
91 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
92 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
93 cmd &= ~PCI_COMMAND_INTX_DISABLE;
94 pci_write_config_word(pdev, PCI_COMMAND, cmd);
95 }
96
Bjorn Helgaasa9047f22013-04-18 15:12:58 -060097 msix_pos = pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -060098 if (msix_pos) {
99 u16 flags;
100 u32 table;
101
102 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
103 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
104
Bjorn Helgaas508d1aa2013-04-18 12:42:58 -0600105 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
106 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600107 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
108 } else
109 vdev->msix_bar = 0xFF;
110
Alex Williamson88c0dead2015-04-07 11:14:40 -0600111 if (!vfio_vga_disabled() && (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
Alex Williamson84237a82013-02-18 10:11:13 -0700112 vdev->has_vga = true;
Alex Williamson84237a82013-02-18 10:11:13 -0700113
Alex Williamson9a92c502012-12-07 13:43:51 -0700114 return 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600115}
116
117static void vfio_pci_disable(struct vfio_pci_device *vdev)
118{
Alex Williamson20077222012-12-07 13:43:50 -0700119 struct pci_dev *pdev = vdev->pdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600120 int bar;
121
Alex Williamson9c22e662014-08-07 11:12:02 -0600122 /* Stop the device from further DMA */
123 pci_clear_master(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600124
125 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
126 VFIO_IRQ_SET_ACTION_TRIGGER,
127 vdev->irq_type, 0, 0, NULL);
128
129 vdev->virq_disabled = false;
130
131 vfio_config_free(vdev);
132
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600133 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
134 if (!vdev->barmap[bar])
135 continue;
Alex Williamson20077222012-12-07 13:43:50 -0700136 pci_iounmap(pdev, vdev->barmap[bar]);
137 pci_release_selected_regions(pdev, 1 << bar);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600138 vdev->barmap[bar] = NULL;
139 }
Alex Williamson20077222012-12-07 13:43:50 -0700140
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600141 vdev->needs_reset = true;
142
Alex Williamson20077222012-12-07 13:43:50 -0700143 /*
144 * If we have saved state, restore it. If we can reset the device,
145 * even better. Resetting with current state seems better than
146 * nothing, but saving and restoring current state without reset
147 * is just busy work.
148 */
149 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
150 pr_info("%s: Couldn't reload %s saved state\n",
151 __func__, dev_name(&pdev->dev));
152
153 if (!vdev->reset_works)
Alex Williamson9c22e662014-08-07 11:12:02 -0600154 goto out;
Alex Williamson20077222012-12-07 13:43:50 -0700155
156 pci_save_state(pdev);
157 }
158
159 /*
160 * Disable INTx and MSI, presumably to avoid spurious interrupts
161 * during reset. Stolen from pci_reset_function()
162 */
163 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
164
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600165 /*
Alex Williamson890ed572014-01-14 20:45:09 -0700166 * Try to reset the device. The success of this is dependent on
167 * being able to lock the device, which is not always possible.
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600168 */
169 if (vdev->reset_works) {
Alex Williamson890ed572014-01-14 20:45:09 -0700170 int ret = pci_try_reset_function(pdev);
171 if (ret)
172 pr_warn("%s: Failed to reset device %s (%d)\n",
173 __func__, dev_name(&pdev->dev), ret);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600174 else
175 vdev->needs_reset = false;
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600176 }
Alex Williamson20077222012-12-07 13:43:50 -0700177
178 pci_restore_state(pdev);
Alex Williamson9c22e662014-08-07 11:12:02 -0600179out:
180 pci_disable_device(pdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600181
182 vfio_pci_try_bus_reset(vdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600183}
184
185static void vfio_pci_release(void *device_data)
186{
187 struct vfio_pci_device *vdev = device_data;
188
Alex Williamson61d79252014-08-07 11:12:04 -0600189 mutex_lock(&driver_lock);
190
191 if (!(--vdev->refcnt)) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000192 vfio_spapr_pci_eeh_release(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600193 vfio_pci_disable(vdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000194 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600195
Alex Williamson61d79252014-08-07 11:12:04 -0600196 mutex_unlock(&driver_lock);
197
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600198 module_put(THIS_MODULE);
199}
200
201static int vfio_pci_open(void *device_data)
202{
203 struct vfio_pci_device *vdev = device_data;
Alex Williamson61d79252014-08-07 11:12:04 -0600204 int ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600205
206 if (!try_module_get(THIS_MODULE))
207 return -ENODEV;
208
Alex Williamson61d79252014-08-07 11:12:04 -0600209 mutex_lock(&driver_lock);
210
211 if (!vdev->refcnt) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000212 ret = vfio_pci_enable(vdev);
213 if (ret)
214 goto error;
215
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600216 vfio_spapr_pci_eeh_open(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600217 }
Alex Williamson61d79252014-08-07 11:12:04 -0600218 vdev->refcnt++;
Gavin Shan1b69be52014-06-10 11:41:57 +1000219error:
Alex Williamson61d79252014-08-07 11:12:04 -0600220 mutex_unlock(&driver_lock);
221 if (ret)
222 module_put(THIS_MODULE);
Gavin Shan1b69be52014-06-10 11:41:57 +1000223 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600224}
225
226static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
227{
228 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
229 u8 pin;
230 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700231 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600232 return 1;
233
234 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
235 u8 pos;
236 u16 flags;
237
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600238 pos = vdev->pdev->msi_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600239 if (pos) {
240 pci_read_config_word(vdev->pdev,
241 pos + PCI_MSI_FLAGS, &flags);
Gavin Shanfd49c812014-05-30 11:35:54 -0600242 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600243 }
244 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
245 u8 pos;
246 u16 flags;
247
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600248 pos = vdev->pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600249 if (pos) {
250 pci_read_config_word(vdev->pdev,
251 pos + PCI_MSIX_FLAGS, &flags);
252
253 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
254 }
Alex Williamson6140a8f2015-02-06 15:05:08 -0700255 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600256 if (pci_is_pcie(vdev->pdev))
257 return 1;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700258 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
259 return 1;
260 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600261
262 return 0;
263}
264
Alex Williamson8b27ee62013-09-04 11:28:04 -0600265static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
266{
267 (*(int *)data)++;
268 return 0;
269}
270
271struct vfio_pci_fill_info {
272 int max;
273 int cur;
274 struct vfio_pci_dependent_device *devices;
275};
276
277static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
278{
279 struct vfio_pci_fill_info *fill = data;
280 struct iommu_group *iommu_group;
281
282 if (fill->cur == fill->max)
283 return -EAGAIN; /* Something changed, try again */
284
285 iommu_group = iommu_group_get(&pdev->dev);
286 if (!iommu_group)
287 return -EPERM; /* Cannot reset non-isolated devices */
288
289 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
290 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
291 fill->devices[fill->cur].bus = pdev->bus->number;
292 fill->devices[fill->cur].devfn = pdev->devfn;
293 fill->cur++;
294 iommu_group_put(iommu_group);
295 return 0;
296}
297
298struct vfio_pci_group_entry {
299 struct vfio_group *group;
300 int id;
301};
302
303struct vfio_pci_group_info {
304 int count;
305 struct vfio_pci_group_entry *groups;
306};
307
308static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
309{
310 struct vfio_pci_group_info *info = data;
311 struct iommu_group *group;
312 int id, i;
313
314 group = iommu_group_get(&pdev->dev);
315 if (!group)
316 return -EPERM;
317
318 id = iommu_group_id(group);
319
320 for (i = 0; i < info->count; i++)
321 if (info->groups[i].id == id)
322 break;
323
324 iommu_group_put(group);
325
326 return (i == info->count) ? -EINVAL : 0;
327}
328
329static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
330{
331 for (; pdev; pdev = pdev->bus->self)
332 if (pdev->bus == slot->bus)
333 return (pdev->slot == slot);
334 return false;
335}
336
337struct vfio_pci_walk_info {
338 int (*fn)(struct pci_dev *, void *data);
339 void *data;
340 struct pci_dev *pdev;
341 bool slot;
342 int ret;
343};
344
345static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
346{
347 struct vfio_pci_walk_info *walk = data;
348
349 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
350 walk->ret = walk->fn(pdev, walk->data);
351
352 return walk->ret;
353}
354
355static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
356 int (*fn)(struct pci_dev *,
357 void *data), void *data,
358 bool slot)
359{
360 struct vfio_pci_walk_info walk = {
361 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
362 };
363
364 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
365
366 return walk.ret;
367}
368
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600369static long vfio_pci_ioctl(void *device_data,
370 unsigned int cmd, unsigned long arg)
371{
372 struct vfio_pci_device *vdev = device_data;
373 unsigned long minsz;
374
375 if (cmd == VFIO_DEVICE_GET_INFO) {
376 struct vfio_device_info info;
377
378 minsz = offsetofend(struct vfio_device_info, num_irqs);
379
380 if (copy_from_user(&info, (void __user *)arg, minsz))
381 return -EFAULT;
382
383 if (info.argsz < minsz)
384 return -EINVAL;
385
386 info.flags = VFIO_DEVICE_FLAGS_PCI;
387
388 if (vdev->reset_works)
389 info.flags |= VFIO_DEVICE_FLAGS_RESET;
390
391 info.num_regions = VFIO_PCI_NUM_REGIONS;
392 info.num_irqs = VFIO_PCI_NUM_IRQS;
393
394 return copy_to_user((void __user *)arg, &info, minsz);
395
396 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
397 struct pci_dev *pdev = vdev->pdev;
398 struct vfio_region_info info;
399
400 minsz = offsetofend(struct vfio_region_info, offset);
401
402 if (copy_from_user(&info, (void __user *)arg, minsz))
403 return -EFAULT;
404
405 if (info.argsz < minsz)
406 return -EINVAL;
407
408 switch (info.index) {
409 case VFIO_PCI_CONFIG_REGION_INDEX:
410 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
411 info.size = pdev->cfg_size;
412 info.flags = VFIO_REGION_INFO_FLAG_READ |
413 VFIO_REGION_INFO_FLAG_WRITE;
414 break;
415 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
416 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
417 info.size = pci_resource_len(pdev, info.index);
418 if (!info.size) {
419 info.flags = 0;
420 break;
421 }
422
423 info.flags = VFIO_REGION_INFO_FLAG_READ |
424 VFIO_REGION_INFO_FLAG_WRITE;
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700425 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
426 pci_resource_flags(pdev, info.index) &
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600427 IORESOURCE_MEM && info.size >= PAGE_SIZE)
428 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
429 break;
430 case VFIO_PCI_ROM_REGION_INDEX:
431 {
432 void __iomem *io;
433 size_t size;
434
435 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
436 info.flags = 0;
437
438 /* Report the BAR size, not the ROM size */
439 info.size = pci_resource_len(pdev, info.index);
440 if (!info.size)
441 break;
442
443 /* Is it really there? */
444 io = pci_map_rom(pdev, &size);
445 if (!io || !size) {
446 info.size = 0;
447 break;
448 }
449 pci_unmap_rom(pdev, io);
450
451 info.flags = VFIO_REGION_INFO_FLAG_READ;
452 break;
453 }
Alex Williamson84237a82013-02-18 10:11:13 -0700454 case VFIO_PCI_VGA_REGION_INDEX:
455 if (!vdev->has_vga)
456 return -EINVAL;
457
458 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
459 info.size = 0xc0000;
460 info.flags = VFIO_REGION_INFO_FLAG_READ |
461 VFIO_REGION_INFO_FLAG_WRITE;
462
463 break;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600464 default:
465 return -EINVAL;
466 }
467
468 return copy_to_user((void __user *)arg, &info, minsz);
469
470 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
471 struct vfio_irq_info info;
472
473 minsz = offsetofend(struct vfio_irq_info, count);
474
475 if (copy_from_user(&info, (void __user *)arg, minsz))
476 return -EFAULT;
477
478 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
479 return -EINVAL;
480
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600481 switch (info.index) {
482 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
Alex Williamson6140a8f2015-02-06 15:05:08 -0700483 case VFIO_PCI_REQ_IRQ_INDEX:
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600484 break;
485 case VFIO_PCI_ERR_IRQ_INDEX:
486 if (pci_is_pcie(vdev->pdev))
487 break;
488 /* pass thru to return error */
489 default:
490 return -EINVAL;
491 }
492
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600493 info.flags = VFIO_IRQ_INFO_EVENTFD;
494
495 info.count = vfio_pci_get_irq_count(vdev, info.index);
496
497 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
498 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
499 VFIO_IRQ_INFO_AUTOMASKED);
500 else
501 info.flags |= VFIO_IRQ_INFO_NORESIZE;
502
503 return copy_to_user((void __user *)arg, &info, minsz);
504
505 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
506 struct vfio_irq_set hdr;
507 u8 *data = NULL;
508 int ret = 0;
509
510 minsz = offsetofend(struct vfio_irq_set, count);
511
512 if (copy_from_user(&hdr, (void __user *)arg, minsz))
513 return -EFAULT;
514
515 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
516 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
517 VFIO_IRQ_SET_ACTION_TYPE_MASK))
518 return -EINVAL;
519
520 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
521 size_t size;
Alex Williamson904c6802013-03-26 11:33:16 -0600522 int max = vfio_pci_get_irq_count(vdev, hdr.index);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600523
524 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
525 size = sizeof(uint8_t);
526 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
527 size = sizeof(int32_t);
528 else
529 return -EINVAL;
530
531 if (hdr.argsz - minsz < hdr.count * size ||
Alex Williamson904c6802013-03-26 11:33:16 -0600532 hdr.start >= max || hdr.start + hdr.count > max)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600533 return -EINVAL;
534
Fengguang Wu3a1f7042012-12-07 13:43:49 -0700535 data = memdup_user((void __user *)(arg + minsz),
536 hdr.count * size);
537 if (IS_ERR(data))
538 return PTR_ERR(data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600539 }
540
541 mutex_lock(&vdev->igate);
542
543 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
544 hdr.start, hdr.count, data);
545
546 mutex_unlock(&vdev->igate);
547 kfree(data);
548
549 return ret;
550
Alex Williamson8b27ee62013-09-04 11:28:04 -0600551 } else if (cmd == VFIO_DEVICE_RESET) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600552 return vdev->reset_works ?
Alex Williamson890ed572014-01-14 20:45:09 -0700553 pci_try_reset_function(vdev->pdev) : -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600554
Alex Williamson8b27ee62013-09-04 11:28:04 -0600555 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
556 struct vfio_pci_hot_reset_info hdr;
557 struct vfio_pci_fill_info fill = { 0 };
558 struct vfio_pci_dependent_device *devices = NULL;
559 bool slot = false;
560 int ret = 0;
561
562 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
563
564 if (copy_from_user(&hdr, (void __user *)arg, minsz))
565 return -EFAULT;
566
567 if (hdr.argsz < minsz)
568 return -EINVAL;
569
570 hdr.flags = 0;
571
572 /* Can we do a slot or bus reset or neither? */
573 if (!pci_probe_reset_slot(vdev->pdev->slot))
574 slot = true;
575 else if (pci_probe_reset_bus(vdev->pdev->bus))
576 return -ENODEV;
577
578 /* How many devices are affected? */
579 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
580 vfio_pci_count_devs,
581 &fill.max, slot);
582 if (ret)
583 return ret;
584
585 WARN_ON(!fill.max); /* Should always be at least one */
586
587 /*
588 * If there's enough space, fill it now, otherwise return
589 * -ENOSPC and the number of devices affected.
590 */
591 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
592 ret = -ENOSPC;
593 hdr.count = fill.max;
594 goto reset_info_exit;
595 }
596
597 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
598 if (!devices)
599 return -ENOMEM;
600
601 fill.devices = devices;
602
603 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
604 vfio_pci_fill_devs,
605 &fill, slot);
606
607 /*
608 * If a device was removed between counting and filling,
609 * we may come up short of fill.max. If a device was
610 * added, we'll have a return of -EAGAIN above.
611 */
612 if (!ret)
613 hdr.count = fill.cur;
614
615reset_info_exit:
616 if (copy_to_user((void __user *)arg, &hdr, minsz))
617 ret = -EFAULT;
618
619 if (!ret) {
620 if (copy_to_user((void __user *)(arg + minsz), devices,
621 hdr.count * sizeof(*devices)))
622 ret = -EFAULT;
623 }
624
625 kfree(devices);
626 return ret;
627
628 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
629 struct vfio_pci_hot_reset hdr;
630 int32_t *group_fds;
631 struct vfio_pci_group_entry *groups;
632 struct vfio_pci_group_info info;
633 bool slot = false;
634 int i, count = 0, ret = 0;
635
636 minsz = offsetofend(struct vfio_pci_hot_reset, count);
637
638 if (copy_from_user(&hdr, (void __user *)arg, minsz))
639 return -EFAULT;
640
641 if (hdr.argsz < minsz || hdr.flags)
642 return -EINVAL;
643
644 /* Can we do a slot or bus reset or neither? */
645 if (!pci_probe_reset_slot(vdev->pdev->slot))
646 slot = true;
647 else if (pci_probe_reset_bus(vdev->pdev->bus))
648 return -ENODEV;
649
650 /*
651 * We can't let userspace give us an arbitrarily large
652 * buffer to copy, so verify how many we think there
653 * could be. Note groups can have multiple devices so
654 * one group per device is the max.
655 */
656 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
657 vfio_pci_count_devs,
658 &count, slot);
659 if (ret)
660 return ret;
661
662 /* Somewhere between 1 and count is OK */
663 if (!hdr.count || hdr.count > count)
664 return -EINVAL;
665
666 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
667 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
668 if (!group_fds || !groups) {
669 kfree(group_fds);
670 kfree(groups);
671 return -ENOMEM;
672 }
673
674 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
675 hdr.count * sizeof(*group_fds))) {
676 kfree(group_fds);
677 kfree(groups);
678 return -EFAULT;
679 }
680
681 /*
682 * For each group_fd, get the group through the vfio external
683 * user interface and store the group and iommu ID. This
684 * ensures the group is held across the reset.
685 */
686 for (i = 0; i < hdr.count; i++) {
687 struct vfio_group *group;
688 struct fd f = fdget(group_fds[i]);
689 if (!f.file) {
690 ret = -EBADF;
691 break;
692 }
693
694 group = vfio_group_get_external_user(f.file);
695 fdput(f);
696 if (IS_ERR(group)) {
697 ret = PTR_ERR(group);
698 break;
699 }
700
701 groups[i].group = group;
702 groups[i].id = vfio_external_user_iommu_id(group);
703 }
704
705 kfree(group_fds);
706
707 /* release reference to groups on error */
708 if (ret)
709 goto hot_reset_release;
710
711 info.count = hdr.count;
712 info.groups = groups;
713
714 /*
715 * Test whether all the affected devices are contained
716 * by the set of groups provided by the user.
717 */
718 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
719 vfio_pci_validate_devs,
720 &info, slot);
721 if (!ret)
722 /* User has access, do the reset */
Alex Williamson890ed572014-01-14 20:45:09 -0700723 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
724 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamson8b27ee62013-09-04 11:28:04 -0600725
726hot_reset_release:
727 for (i--; i >= 0; i--)
728 vfio_group_put_external_user(groups[i].group);
729
730 kfree(groups);
731 return ret;
732 }
733
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600734 return -ENOTTY;
735}
736
Alex Williamson5b279a12013-02-14 14:02:12 -0700737static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
738 size_t count, loff_t *ppos, bool iswrite)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600739{
740 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
741 struct vfio_pci_device *vdev = device_data;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600742
743 if (index >= VFIO_PCI_NUM_REGIONS)
744 return -EINVAL;
745
Alex Williamson5b279a12013-02-14 14:02:12 -0700746 switch (index) {
747 case VFIO_PCI_CONFIG_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700748 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
749
Alex Williamson5b279a12013-02-14 14:02:12 -0700750 case VFIO_PCI_ROM_REGION_INDEX:
751 if (iswrite)
752 return -EINVAL;
Alex Williamson906ee992013-02-14 14:02:12 -0700753 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600754
Alex Williamson5b279a12013-02-14 14:02:12 -0700755 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700756 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson84237a82013-02-18 10:11:13 -0700757
758 case VFIO_PCI_VGA_REGION_INDEX:
759 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson5b279a12013-02-14 14:02:12 -0700760 }
761
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600762 return -EINVAL;
763}
764
Alex Williamson5b279a12013-02-14 14:02:12 -0700765static ssize_t vfio_pci_read(void *device_data, char __user *buf,
766 size_t count, loff_t *ppos)
767{
Alex Williamson906ee992013-02-14 14:02:12 -0700768 if (!count)
769 return 0;
770
Alex Williamson5b279a12013-02-14 14:02:12 -0700771 return vfio_pci_rw(device_data, buf, count, ppos, false);
772}
773
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600774static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
775 size_t count, loff_t *ppos)
776{
Alex Williamson906ee992013-02-14 14:02:12 -0700777 if (!count)
778 return 0;
779
780 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600781}
782
783static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
784{
785 struct vfio_pci_device *vdev = device_data;
786 struct pci_dev *pdev = vdev->pdev;
787 unsigned int index;
Alex Williamson34002f52012-10-10 09:10:31 -0600788 u64 phys_len, req_len, pgoff, req_start;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600789 int ret;
790
791 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
792
793 if (vma->vm_end < vma->vm_start)
794 return -EINVAL;
795 if ((vma->vm_flags & VM_SHARED) == 0)
796 return -EINVAL;
797 if (index >= VFIO_PCI_ROM_REGION_INDEX)
798 return -EINVAL;
799 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
800 return -EINVAL;
801
802 phys_len = pci_resource_len(pdev, index);
803 req_len = vma->vm_end - vma->vm_start;
804 pgoff = vma->vm_pgoff &
805 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
806 req_start = pgoff << PAGE_SHIFT;
807
808 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
809 return -EINVAL;
810
811 if (index == vdev->msix_bar) {
812 /*
813 * Disallow mmaps overlapping the MSI-X table; users don't
814 * get to touch this directly. We could find somewhere
815 * else to map the overlap, but page granularity is only
816 * a recommendation, not a requirement, so the user needs
817 * to know which bits are real. Requiring them to mmap
818 * around the table makes that clear.
819 */
820
821 /* If neither entirely above nor below, then it overlaps */
822 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
823 req_start + req_len <= vdev->msix_offset))
824 return -EINVAL;
825 }
826
827 /*
828 * Even though we don't make use of the barmap for the mmap,
829 * we need to request the region and the barmap tracks that.
830 */
831 if (!vdev->barmap[index]) {
832 ret = pci_request_selected_regions(pdev,
833 1 << index, "vfio-pci");
834 if (ret)
835 return ret;
836
837 vdev->barmap[index] = pci_iomap(pdev, index, 0);
838 }
839
840 vma->vm_private_data = vdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600841 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Alex Williamson34002f52012-10-10 09:10:31 -0600842 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600843
Alex Williamson34002f52012-10-10 09:10:31 -0600844 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600845 req_len, vma->vm_page_prot);
846}
847
Alex Williamson6140a8f2015-02-06 15:05:08 -0700848static void vfio_pci_request(void *device_data, unsigned int count)
849{
850 struct vfio_pci_device *vdev = device_data;
851
852 mutex_lock(&vdev->igate);
853
854 if (vdev->req_trigger) {
855 dev_dbg(&vdev->pdev->dev, "Requesting device from user\n");
856 eventfd_signal(vdev->req_trigger, 1);
857 }
858
859 mutex_unlock(&vdev->igate);
860}
861
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600862static const struct vfio_device_ops vfio_pci_ops = {
863 .name = "vfio-pci",
864 .open = vfio_pci_open,
865 .release = vfio_pci_release,
866 .ioctl = vfio_pci_ioctl,
867 .read = vfio_pci_read,
868 .write = vfio_pci_write,
869 .mmap = vfio_pci_mmap,
Alex Williamson6140a8f2015-02-06 15:05:08 -0700870 .request = vfio_pci_request,
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600871};
872
873static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
874{
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600875 struct vfio_pci_device *vdev;
876 struct iommu_group *group;
877 int ret;
878
Wei Yang7c2e2112015-01-07 10:29:11 -0700879 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600880 return -EINVAL;
881
882 group = iommu_group_get(&pdev->dev);
883 if (!group)
884 return -EINVAL;
885
886 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
887 if (!vdev) {
888 iommu_group_put(group);
889 return -ENOMEM;
890 }
891
892 vdev->pdev = pdev;
893 vdev->irq_type = VFIO_PCI_NUM_IRQS;
894 mutex_init(&vdev->igate);
895 spin_lock_init(&vdev->irqlock);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600896
897 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
898 if (ret) {
899 iommu_group_put(group);
900 kfree(vdev);
901 }
902
903 return ret;
904}
905
906static void vfio_pci_remove(struct pci_dev *pdev)
907{
908 struct vfio_pci_device *vdev;
909
Alex Williamson61d79252014-08-07 11:12:04 -0600910 vdev = vfio_del_group_dev(&pdev->dev);
911 if (vdev) {
912 iommu_group_put(pdev->dev.iommu_group);
913 kfree(vdev);
914 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600915}
916
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600917static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
918 pci_channel_state_t state)
919{
920 struct vfio_pci_device *vdev;
921 struct vfio_device *device;
922
923 device = vfio_device_get_from_dev(&pdev->dev);
924 if (device == NULL)
925 return PCI_ERS_RESULT_DISCONNECT;
926
927 vdev = vfio_device_data(device);
928 if (vdev == NULL) {
929 vfio_device_put(device);
930 return PCI_ERS_RESULT_DISCONNECT;
931 }
932
Alex Williamson3be3a072014-01-14 16:12:55 -0700933 mutex_lock(&vdev->igate);
934
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600935 if (vdev->err_trigger)
936 eventfd_signal(vdev->err_trigger, 1);
937
Alex Williamson3be3a072014-01-14 16:12:55 -0700938 mutex_unlock(&vdev->igate);
939
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600940 vfio_device_put(device);
941
942 return PCI_ERS_RESULT_CAN_RECOVER;
943}
944
945static struct pci_error_handlers vfio_err_handlers = {
946 .error_detected = vfio_pci_aer_err_detected,
947};
948
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600949static struct pci_driver vfio_pci_driver = {
950 .name = "vfio-pci",
951 .id_table = NULL, /* only dynamic ids */
952 .probe = vfio_pci_probe,
953 .remove = vfio_pci_remove,
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600954 .err_handler = &vfio_err_handlers,
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600955};
956
Alex Williamson93899a62014-09-29 17:18:39 -0600957struct vfio_devices {
958 struct vfio_device **devices;
959 int cur_index;
960 int max_index;
961};
962
963static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600964{
Alex Williamson93899a62014-09-29 17:18:39 -0600965 struct vfio_devices *devs = data;
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600966 struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver);
967
Alex Williamson93899a62014-09-29 17:18:39 -0600968 if (pci_drv != &vfio_pci_driver)
969 return -EBUSY;
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600970
Alex Williamson93899a62014-09-29 17:18:39 -0600971 if (devs->cur_index == devs->max_index)
972 return -ENOSPC;
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600973
Alex Williamson93899a62014-09-29 17:18:39 -0600974 devs->devices[devs->cur_index] = vfio_device_get_from_dev(&pdev->dev);
975 if (!devs->devices[devs->cur_index])
976 return -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600977
Alex Williamson93899a62014-09-29 17:18:39 -0600978 devs->cur_index++;
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600979 return 0;
980}
981
982/*
983 * Attempt to do a bus/slot reset if there are devices affected by a reset for
984 * this device that are needs_reset and all of the affected devices are unused
Alex Williamson93899a62014-09-29 17:18:39 -0600985 * (!refcnt). Callers are required to hold driver_lock when calling this to
986 * prevent device opens and concurrent bus reset attempts. We prevent device
987 * unbinds by acquiring and holding a reference to the vfio_device.
988 *
989 * NB: vfio-core considers a group to be viable even if some devices are
990 * bound to drivers like pci-stub or pcieport. Here we require all devices
991 * to be bound to vfio_pci since that's the only way we can be sure they
992 * stay put.
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600993 */
994static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
995{
Alex Williamson93899a62014-09-29 17:18:39 -0600996 struct vfio_devices devs = { .cur_index = 0 };
997 int i = 0, ret = -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600998 bool needs_reset = false, slot = false;
Alex Williamson93899a62014-09-29 17:18:39 -0600999 struct vfio_pci_device *tmp;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001000
1001 if (!pci_probe_reset_slot(vdev->pdev->slot))
1002 slot = true;
1003 else if (pci_probe_reset_bus(vdev->pdev->bus))
1004 return;
1005
Alex Williamson93899a62014-09-29 17:18:39 -06001006 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1007 &i, slot) || !i)
1008 return;
1009
1010 devs.max_index = i;
1011 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1012 if (!devs.devices)
1013 return;
1014
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001015 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
Alex Williamson93899a62014-09-29 17:18:39 -06001016 vfio_pci_get_devs, &devs, slot))
1017 goto put_devs;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001018
Alex Williamson93899a62014-09-29 17:18:39 -06001019 for (i = 0; i < devs.cur_index; i++) {
1020 tmp = vfio_device_data(devs.devices[i]);
1021 if (tmp->needs_reset)
1022 needs_reset = true;
1023 if (tmp->refcnt)
1024 goto put_devs;
1025 }
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001026
Alex Williamson93899a62014-09-29 17:18:39 -06001027 if (needs_reset)
1028 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1029 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001030
Alex Williamson93899a62014-09-29 17:18:39 -06001031put_devs:
1032 for (i = 0; i < devs.cur_index; i++) {
1033 if (!ret) {
1034 tmp = vfio_device_data(devs.devices[i]);
1035 tmp->needs_reset = false;
1036 }
1037 vfio_device_put(devs.devices[i]);
1038 }
1039
1040 kfree(devs.devices);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001041}
1042
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001043static void __exit vfio_pci_cleanup(void)
1044{
1045 pci_unregister_driver(&vfio_pci_driver);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001046 vfio_pci_uninit_perm_bits();
1047}
1048
1049static int __init vfio_pci_init(void)
1050{
1051 int ret;
1052
1053 /* Allocate shared config space permision data used by all devices */
1054 ret = vfio_pci_init_perm_bits();
1055 if (ret)
1056 return ret;
1057
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001058 /* Register and scan for devices */
1059 ret = pci_register_driver(&vfio_pci_driver);
1060 if (ret)
1061 goto out_driver;
1062
1063 return 0;
1064
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001065out_driver:
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001066 vfio_pci_uninit_perm_bits();
1067 return ret;
1068}
1069
1070module_init(vfio_pci_init);
1071module_exit(vfio_pci_cleanup);
1072
1073MODULE_VERSION(DRIVER_VERSION);
1074MODULE_LICENSE("GPL v2");
1075MODULE_AUTHOR(DRIVER_AUTHOR);
1076MODULE_DESCRIPTION(DRIVER_DESC);