2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
3 * Author: Alex Williamson <alex.williamson@redhat.com>
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.
9 * Derived from original vfio:
10 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
11 * Author: Tom Lyon, pugs@cisco.com
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/device.h>
17 #include <linux/eventfd.h>
18 #include <linux/file.h>
19 #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>
30 #include <linux/vgaarb.h>
32 #include "vfio_pci_private.h"
34 #define DRIVER_VERSION "0.2"
35 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
36 #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
38 static char ids
[1024] __initdata
;
39 module_param_string(ids
, ids
, sizeof(ids
), 0);
40 MODULE_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 static bool nointxmask
;
43 module_param_named(nointxmask
, nointxmask
, bool, S_IRUGO
| S_IWUSR
);
44 MODULE_PARM_DESC(nointxmask
,
45 "Disable support for PCI 2.3 style INTx masking. If this resolves problems for specific devices, report lspci -vvvxxx to linux-pci@vger.kernel.org so the device can be fixed automatically via the broken_intx_masking flag.");
47 #ifdef CONFIG_VFIO_PCI_VGA
48 static bool disable_vga
;
49 module_param(disable_vga
, bool, S_IRUGO
);
50 MODULE_PARM_DESC(disable_vga
, "Disable VGA resource access through vfio-pci");
53 static bool disable_idle_d3
;
54 module_param(disable_idle_d3
, bool, S_IRUGO
| S_IWUSR
);
55 MODULE_PARM_DESC(disable_idle_d3
,
56 "Disable using the PCI D3 low power state for idle, unused devices");
58 static DEFINE_MUTEX(driver_lock
);
60 static inline bool vfio_vga_disabled(void)
62 #ifdef CONFIG_VFIO_PCI_VGA
70 * Our VGA arbiter participation is limited since we don't know anything
71 * about the device itself. However, if the device is the only VGA device
72 * downstream of a bridge and VFIO VGA support is disabled, then we can
73 * safely return legacy VGA IO and memory as not decoded since the user
74 * has no way to get to it and routing can be disabled externally at the
77 static unsigned int vfio_pci_set_vga_decode(void *opaque
, bool single_vga
)
79 struct vfio_pci_device
*vdev
= opaque
;
80 struct pci_dev
*tmp
= NULL
, *pdev
= vdev
->pdev
;
81 unsigned char max_busnr
;
84 if (single_vga
|| !vfio_vga_disabled() || pci_is_root_bus(pdev
->bus
))
85 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
|
86 VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
88 max_busnr
= pci_bus_max_busnr(pdev
->bus
);
89 decodes
= VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
91 while ((tmp
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, tmp
)) != NULL
) {
93 pci_domain_nr(tmp
->bus
) != pci_domain_nr(pdev
->bus
) ||
94 pci_is_root_bus(tmp
->bus
))
97 if (tmp
->bus
->number
>= pdev
->bus
->number
&&
98 tmp
->bus
->number
<= max_busnr
) {
100 decodes
|= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
108 static inline bool vfio_pci_is_vga(struct pci_dev
*pdev
)
110 return (pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
;
113 static void vfio_pci_try_bus_reset(struct vfio_pci_device
*vdev
);
114 static void vfio_pci_disable(struct vfio_pci_device
*vdev
);
116 static int vfio_pci_enable(struct vfio_pci_device
*vdev
)
118 struct pci_dev
*pdev
= vdev
->pdev
;
123 pci_set_power_state(pdev
, PCI_D0
);
125 /* Don't allow our initial saved state to include busmaster */
126 pci_clear_master(pdev
);
128 ret
= pci_enable_device(pdev
);
132 vdev
->reset_works
= (pci_reset_function(pdev
) == 0);
133 pci_save_state(pdev
);
134 vdev
->pci_saved_state
= pci_store_saved_state(pdev
);
135 if (!vdev
->pci_saved_state
)
136 pr_debug("%s: Couldn't store %s saved state\n",
137 __func__
, dev_name(&pdev
->dev
));
139 ret
= vfio_config_init(vdev
);
141 kfree(vdev
->pci_saved_state
);
142 vdev
->pci_saved_state
= NULL
;
143 pci_disable_device(pdev
);
147 if (likely(!nointxmask
))
148 vdev
->pci_2_3
= pci_intx_mask_supported(pdev
);
150 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
151 if (vdev
->pci_2_3
&& (cmd
& PCI_COMMAND_INTX_DISABLE
)) {
152 cmd
&= ~PCI_COMMAND_INTX_DISABLE
;
153 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
);
156 msix_pos
= pdev
->msix_cap
;
161 pci_read_config_word(pdev
, msix_pos
+ PCI_MSIX_FLAGS
, &flags
);
162 pci_read_config_dword(pdev
, msix_pos
+ PCI_MSIX_TABLE
, &table
);
164 vdev
->msix_bar
= table
& PCI_MSIX_TABLE_BIR
;
165 vdev
->msix_offset
= table
& PCI_MSIX_TABLE_OFFSET
;
166 vdev
->msix_size
= ((flags
& PCI_MSIX_FLAGS_QSIZE
) + 1) * 16;
168 vdev
->msix_bar
= 0xFF;
170 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev
))
171 vdev
->has_vga
= true;
174 if (vfio_pci_is_vga(pdev
) &&
175 pdev
->vendor
== PCI_VENDOR_ID_INTEL
&&
176 IS_ENABLED(CONFIG_VFIO_PCI_IGD
)) {
177 ret
= vfio_pci_igd_init(vdev
);
179 dev_warn(&vdev
->pdev
->dev
,
180 "Failed to setup Intel IGD regions\n");
181 vfio_pci_disable(vdev
);
189 static void vfio_pci_disable(struct vfio_pci_device
*vdev
)
191 struct pci_dev
*pdev
= vdev
->pdev
;
194 /* Stop the device from further DMA */
195 pci_clear_master(pdev
);
197 vfio_pci_set_irqs_ioctl(vdev
, VFIO_IRQ_SET_DATA_NONE
|
198 VFIO_IRQ_SET_ACTION_TRIGGER
,
199 vdev
->irq_type
, 0, 0, NULL
);
201 vdev
->virq_disabled
= false;
203 for (i
= 0; i
< vdev
->num_regions
; i
++)
204 vdev
->region
[i
].ops
->release(vdev
, &vdev
->region
[i
]);
206 vdev
->num_regions
= 0;
208 vdev
->region
= NULL
; /* don't krealloc a freed pointer */
210 vfio_config_free(vdev
);
212 for (bar
= PCI_STD_RESOURCES
; bar
<= PCI_STD_RESOURCE_END
; bar
++) {
213 if (!vdev
->barmap
[bar
])
215 pci_iounmap(pdev
, vdev
->barmap
[bar
]);
216 pci_release_selected_regions(pdev
, 1 << bar
);
217 vdev
->barmap
[bar
] = NULL
;
220 vdev
->needs_reset
= true;
223 * If we have saved state, restore it. If we can reset the device,
224 * even better. Resetting with current state seems better than
225 * nothing, but saving and restoring current state without reset
228 if (pci_load_and_free_saved_state(pdev
, &vdev
->pci_saved_state
)) {
229 pr_info("%s: Couldn't reload %s saved state\n",
230 __func__
, dev_name(&pdev
->dev
));
232 if (!vdev
->reset_works
)
235 pci_save_state(pdev
);
239 * Disable INTx and MSI, presumably to avoid spurious interrupts
240 * during reset. Stolen from pci_reset_function()
242 pci_write_config_word(pdev
, PCI_COMMAND
, PCI_COMMAND_INTX_DISABLE
);
245 * Try to reset the device. The success of this is dependent on
246 * being able to lock the device, which is not always possible.
248 if (vdev
->reset_works
&& !pci_try_reset_function(pdev
))
249 vdev
->needs_reset
= false;
251 pci_restore_state(pdev
);
253 pci_disable_device(pdev
);
255 vfio_pci_try_bus_reset(vdev
);
257 if (!disable_idle_d3
)
258 pci_set_power_state(pdev
, PCI_D3hot
);
261 static void vfio_pci_release(void *device_data
)
263 struct vfio_pci_device
*vdev
= device_data
;
265 mutex_lock(&driver_lock
);
267 if (!(--vdev
->refcnt
)) {
268 vfio_spapr_pci_eeh_release(vdev
->pdev
);
269 vfio_pci_disable(vdev
);
272 mutex_unlock(&driver_lock
);
274 module_put(THIS_MODULE
);
277 static int vfio_pci_open(void *device_data
)
279 struct vfio_pci_device
*vdev
= device_data
;
282 if (!try_module_get(THIS_MODULE
))
285 mutex_lock(&driver_lock
);
288 ret
= vfio_pci_enable(vdev
);
292 vfio_spapr_pci_eeh_open(vdev
->pdev
);
296 mutex_unlock(&driver_lock
);
298 module_put(THIS_MODULE
);
302 static int vfio_pci_get_irq_count(struct vfio_pci_device
*vdev
, int irq_type
)
304 if (irq_type
== VFIO_PCI_INTX_IRQ_INDEX
) {
306 pci_read_config_byte(vdev
->pdev
, PCI_INTERRUPT_PIN
, &pin
);
307 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX
) && pin
)
310 } else if (irq_type
== VFIO_PCI_MSI_IRQ_INDEX
) {
314 pos
= vdev
->pdev
->msi_cap
;
316 pci_read_config_word(vdev
->pdev
,
317 pos
+ PCI_MSI_FLAGS
, &flags
);
318 return 1 << ((flags
& PCI_MSI_FLAGS_QMASK
) >> 1);
320 } else if (irq_type
== VFIO_PCI_MSIX_IRQ_INDEX
) {
324 pos
= vdev
->pdev
->msix_cap
;
326 pci_read_config_word(vdev
->pdev
,
327 pos
+ PCI_MSIX_FLAGS
, &flags
);
329 return (flags
& PCI_MSIX_FLAGS_QSIZE
) + 1;
331 } else if (irq_type
== VFIO_PCI_ERR_IRQ_INDEX
) {
332 if (pci_is_pcie(vdev
->pdev
))
334 } else if (irq_type
== VFIO_PCI_REQ_IRQ_INDEX
) {
341 static int vfio_pci_count_devs(struct pci_dev
*pdev
, void *data
)
347 struct vfio_pci_fill_info
{
350 struct vfio_pci_dependent_device
*devices
;
353 static int vfio_pci_fill_devs(struct pci_dev
*pdev
, void *data
)
355 struct vfio_pci_fill_info
*fill
= data
;
356 struct iommu_group
*iommu_group
;
358 if (fill
->cur
== fill
->max
)
359 return -EAGAIN
; /* Something changed, try again */
361 iommu_group
= iommu_group_get(&pdev
->dev
);
363 return -EPERM
; /* Cannot reset non-isolated devices */
365 fill
->devices
[fill
->cur
].group_id
= iommu_group_id(iommu_group
);
366 fill
->devices
[fill
->cur
].segment
= pci_domain_nr(pdev
->bus
);
367 fill
->devices
[fill
->cur
].bus
= pdev
->bus
->number
;
368 fill
->devices
[fill
->cur
].devfn
= pdev
->devfn
;
370 iommu_group_put(iommu_group
);
374 struct vfio_pci_group_entry
{
375 struct vfio_group
*group
;
379 struct vfio_pci_group_info
{
381 struct vfio_pci_group_entry
*groups
;
384 static int vfio_pci_validate_devs(struct pci_dev
*pdev
, void *data
)
386 struct vfio_pci_group_info
*info
= data
;
387 struct iommu_group
*group
;
390 group
= iommu_group_get(&pdev
->dev
);
394 id
= iommu_group_id(group
);
396 for (i
= 0; i
< info
->count
; i
++)
397 if (info
->groups
[i
].id
== id
)
400 iommu_group_put(group
);
402 return (i
== info
->count
) ? -EINVAL
: 0;
405 static bool vfio_pci_dev_below_slot(struct pci_dev
*pdev
, struct pci_slot
*slot
)
407 for (; pdev
; pdev
= pdev
->bus
->self
)
408 if (pdev
->bus
== slot
->bus
)
409 return (pdev
->slot
== slot
);
413 struct vfio_pci_walk_info
{
414 int (*fn
)(struct pci_dev
*, void *data
);
416 struct pci_dev
*pdev
;
421 static int vfio_pci_walk_wrapper(struct pci_dev
*pdev
, void *data
)
423 struct vfio_pci_walk_info
*walk
= data
;
425 if (!walk
->slot
|| vfio_pci_dev_below_slot(pdev
, walk
->pdev
->slot
))
426 walk
->ret
= walk
->fn(pdev
, walk
->data
);
431 static int vfio_pci_for_each_slot_or_bus(struct pci_dev
*pdev
,
432 int (*fn
)(struct pci_dev
*,
433 void *data
), void *data
,
436 struct vfio_pci_walk_info walk
= {
437 .fn
= fn
, .data
= data
, .pdev
= pdev
, .slot
= slot
, .ret
= 0,
440 pci_walk_bus(pdev
->bus
, vfio_pci_walk_wrapper
, &walk
);
445 static int msix_sparse_mmap_cap(struct vfio_pci_device
*vdev
,
446 struct vfio_info_cap
*caps
)
448 struct vfio_info_cap_header
*header
;
449 struct vfio_region_info_cap_sparse_mmap
*sparse
;
451 int nr_areas
= 2, i
= 0;
453 end
= pci_resource_len(vdev
->pdev
, vdev
->msix_bar
);
455 /* If MSI-X table is aligned to the start or end, only one area */
456 if (((vdev
->msix_offset
& PAGE_MASK
) == 0) ||
457 (PAGE_ALIGN(vdev
->msix_offset
+ vdev
->msix_size
) >= end
))
460 size
= sizeof(*sparse
) + (nr_areas
* sizeof(*sparse
->areas
));
462 header
= vfio_info_cap_add(caps
, size
,
463 VFIO_REGION_INFO_CAP_SPARSE_MMAP
, 1);
465 return PTR_ERR(header
);
467 sparse
= container_of(header
,
468 struct vfio_region_info_cap_sparse_mmap
, header
);
469 sparse
->nr_areas
= nr_areas
;
471 if (vdev
->msix_offset
& PAGE_MASK
) {
472 sparse
->areas
[i
].offset
= 0;
473 sparse
->areas
[i
].size
= vdev
->msix_offset
& PAGE_MASK
;
477 if (PAGE_ALIGN(vdev
->msix_offset
+ vdev
->msix_size
) < end
) {
478 sparse
->areas
[i
].offset
= PAGE_ALIGN(vdev
->msix_offset
+
480 sparse
->areas
[i
].size
= end
- sparse
->areas
[i
].offset
;
487 static int region_type_cap(struct vfio_pci_device
*vdev
,
488 struct vfio_info_cap
*caps
,
489 unsigned int type
, unsigned int subtype
)
491 struct vfio_info_cap_header
*header
;
492 struct vfio_region_info_cap_type
*cap
;
494 header
= vfio_info_cap_add(caps
, sizeof(*cap
),
495 VFIO_REGION_INFO_CAP_TYPE
, 1);
497 return PTR_ERR(header
);
499 cap
= container_of(header
, struct vfio_region_info_cap_type
, header
);
501 cap
->subtype
= subtype
;
506 int vfio_pci_register_dev_region(struct vfio_pci_device
*vdev
,
507 unsigned int type
, unsigned int subtype
,
508 const struct vfio_pci_regops
*ops
,
509 size_t size
, u32 flags
, void *data
)
511 struct vfio_pci_region
*region
;
513 region
= krealloc(vdev
->region
,
514 (vdev
->num_regions
+ 1) * sizeof(*region
),
519 vdev
->region
= region
;
520 vdev
->region
[vdev
->num_regions
].type
= type
;
521 vdev
->region
[vdev
->num_regions
].subtype
= subtype
;
522 vdev
->region
[vdev
->num_regions
].ops
= ops
;
523 vdev
->region
[vdev
->num_regions
].size
= size
;
524 vdev
->region
[vdev
->num_regions
].flags
= flags
;
525 vdev
->region
[vdev
->num_regions
].data
= data
;
532 static long vfio_pci_ioctl(void *device_data
,
533 unsigned int cmd
, unsigned long arg
)
535 struct vfio_pci_device
*vdev
= device_data
;
538 if (cmd
== VFIO_DEVICE_GET_INFO
) {
539 struct vfio_device_info info
;
541 minsz
= offsetofend(struct vfio_device_info
, num_irqs
);
543 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
546 if (info
.argsz
< minsz
)
549 info
.flags
= VFIO_DEVICE_FLAGS_PCI
;
551 if (vdev
->reset_works
)
552 info
.flags
|= VFIO_DEVICE_FLAGS_RESET
;
554 info
.num_regions
= VFIO_PCI_NUM_REGIONS
+ vdev
->num_regions
;
555 info
.num_irqs
= VFIO_PCI_NUM_IRQS
;
557 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
560 } else if (cmd
== VFIO_DEVICE_GET_REGION_INFO
) {
561 struct pci_dev
*pdev
= vdev
->pdev
;
562 struct vfio_region_info info
;
563 struct vfio_info_cap caps
= { .buf
= NULL
, .size
= 0 };
566 minsz
= offsetofend(struct vfio_region_info
, offset
);
568 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
571 if (info
.argsz
< minsz
)
574 switch (info
.index
) {
575 case VFIO_PCI_CONFIG_REGION_INDEX
:
576 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
577 info
.size
= pdev
->cfg_size
;
578 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
579 VFIO_REGION_INFO_FLAG_WRITE
;
581 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
582 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
583 info
.size
= pci_resource_len(pdev
, info
.index
);
589 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
590 VFIO_REGION_INFO_FLAG_WRITE
;
591 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP
) &&
592 pci_resource_flags(pdev
, info
.index
) &
593 IORESOURCE_MEM
&& info
.size
>= PAGE_SIZE
) {
594 info
.flags
|= VFIO_REGION_INFO_FLAG_MMAP
;
595 if (info
.index
== vdev
->msix_bar
) {
596 ret
= msix_sparse_mmap_cap(vdev
, &caps
);
603 case VFIO_PCI_ROM_REGION_INDEX
:
608 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
611 /* Report the BAR size, not the ROM size */
612 info
.size
= pci_resource_len(pdev
, info
.index
);
614 /* Shadow ROMs appear as PCI option ROMs */
615 if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
616 IORESOURCE_ROM_SHADOW
)
622 /* Is it really there? */
623 io
= pci_map_rom(pdev
, &size
);
628 pci_unmap_rom(pdev
, io
);
630 info
.flags
= VFIO_REGION_INFO_FLAG_READ
;
633 case VFIO_PCI_VGA_REGION_INDEX
:
637 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
639 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
640 VFIO_REGION_INFO_FLAG_WRITE
;
645 VFIO_PCI_NUM_REGIONS
+ vdev
->num_regions
)
648 i
= info
.index
- VFIO_PCI_NUM_REGIONS
;
650 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
651 info
.size
= vdev
->region
[i
].size
;
652 info
.flags
= vdev
->region
[i
].flags
;
654 ret
= region_type_cap(vdev
, &caps
,
655 vdev
->region
[i
].type
,
656 vdev
->region
[i
].subtype
);
662 info
.flags
|= VFIO_REGION_INFO_FLAG_CAPS
;
663 if (info
.argsz
< sizeof(info
) + caps
.size
) {
664 info
.argsz
= sizeof(info
) + caps
.size
;
667 vfio_info_cap_shift(&caps
, sizeof(info
));
668 if (copy_to_user((void __user
*)arg
+
669 sizeof(info
), caps
.buf
,
674 info
.cap_offset
= sizeof(info
);
680 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
683 } else if (cmd
== VFIO_DEVICE_GET_IRQ_INFO
) {
684 struct vfio_irq_info info
;
686 minsz
= offsetofend(struct vfio_irq_info
, count
);
688 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
691 if (info
.argsz
< minsz
|| info
.index
>= VFIO_PCI_NUM_IRQS
)
694 switch (info
.index
) {
695 case VFIO_PCI_INTX_IRQ_INDEX
... VFIO_PCI_MSIX_IRQ_INDEX
:
696 case VFIO_PCI_REQ_IRQ_INDEX
:
698 case VFIO_PCI_ERR_IRQ_INDEX
:
699 if (pci_is_pcie(vdev
->pdev
))
701 /* pass thru to return error */
706 info
.flags
= VFIO_IRQ_INFO_EVENTFD
;
708 info
.count
= vfio_pci_get_irq_count(vdev
, info
.index
);
710 if (info
.index
== VFIO_PCI_INTX_IRQ_INDEX
)
711 info
.flags
|= (VFIO_IRQ_INFO_MASKABLE
|
712 VFIO_IRQ_INFO_AUTOMASKED
);
714 info
.flags
|= VFIO_IRQ_INFO_NORESIZE
;
716 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
719 } else if (cmd
== VFIO_DEVICE_SET_IRQS
) {
720 struct vfio_irq_set hdr
;
724 minsz
= offsetofend(struct vfio_irq_set
, count
);
726 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
729 if (hdr
.argsz
< minsz
|| hdr
.index
>= VFIO_PCI_NUM_IRQS
||
730 hdr
.flags
& ~(VFIO_IRQ_SET_DATA_TYPE_MASK
|
731 VFIO_IRQ_SET_ACTION_TYPE_MASK
))
734 if (!(hdr
.flags
& VFIO_IRQ_SET_DATA_NONE
)) {
736 int max
= vfio_pci_get_irq_count(vdev
, hdr
.index
);
738 if (hdr
.flags
& VFIO_IRQ_SET_DATA_BOOL
)
739 size
= sizeof(uint8_t);
740 else if (hdr
.flags
& VFIO_IRQ_SET_DATA_EVENTFD
)
741 size
= sizeof(int32_t);
745 if (hdr
.argsz
- minsz
< hdr
.count
* size
||
746 hdr
.start
>= max
|| hdr
.start
+ hdr
.count
> max
)
749 data
= memdup_user((void __user
*)(arg
+ minsz
),
752 return PTR_ERR(data
);
755 mutex_lock(&vdev
->igate
);
757 ret
= vfio_pci_set_irqs_ioctl(vdev
, hdr
.flags
, hdr
.index
,
758 hdr
.start
, hdr
.count
, data
);
760 mutex_unlock(&vdev
->igate
);
765 } else if (cmd
== VFIO_DEVICE_RESET
) {
766 return vdev
->reset_works
?
767 pci_try_reset_function(vdev
->pdev
) : -EINVAL
;
769 } else if (cmd
== VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
) {
770 struct vfio_pci_hot_reset_info hdr
;
771 struct vfio_pci_fill_info fill
= { 0 };
772 struct vfio_pci_dependent_device
*devices
= NULL
;
776 minsz
= offsetofend(struct vfio_pci_hot_reset_info
, count
);
778 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
781 if (hdr
.argsz
< minsz
)
786 /* Can we do a slot or bus reset or neither? */
787 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
789 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
792 /* How many devices are affected? */
793 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
799 WARN_ON(!fill
.max
); /* Should always be at least one */
802 * If there's enough space, fill it now, otherwise return
803 * -ENOSPC and the number of devices affected.
805 if (hdr
.argsz
< sizeof(hdr
) + (fill
.max
* sizeof(*devices
))) {
807 hdr
.count
= fill
.max
;
808 goto reset_info_exit
;
811 devices
= kcalloc(fill
.max
, sizeof(*devices
), GFP_KERNEL
);
815 fill
.devices
= devices
;
817 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
822 * If a device was removed between counting and filling,
823 * we may come up short of fill.max. If a device was
824 * added, we'll have a return of -EAGAIN above.
827 hdr
.count
= fill
.cur
;
830 if (copy_to_user((void __user
*)arg
, &hdr
, minsz
))
834 if (copy_to_user((void __user
*)(arg
+ minsz
), devices
,
835 hdr
.count
* sizeof(*devices
)))
842 } else if (cmd
== VFIO_DEVICE_PCI_HOT_RESET
) {
843 struct vfio_pci_hot_reset hdr
;
845 struct vfio_pci_group_entry
*groups
;
846 struct vfio_pci_group_info info
;
848 int i
, count
= 0, ret
= 0;
850 minsz
= offsetofend(struct vfio_pci_hot_reset
, count
);
852 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
855 if (hdr
.argsz
< minsz
|| hdr
.flags
)
858 /* Can we do a slot or bus reset or neither? */
859 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
861 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
865 * We can't let userspace give us an arbitrarily large
866 * buffer to copy, so verify how many we think there
867 * could be. Note groups can have multiple devices so
868 * one group per device is the max.
870 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
876 /* Somewhere between 1 and count is OK */
877 if (!hdr
.count
|| hdr
.count
> count
)
880 group_fds
= kcalloc(hdr
.count
, sizeof(*group_fds
), GFP_KERNEL
);
881 groups
= kcalloc(hdr
.count
, sizeof(*groups
), GFP_KERNEL
);
882 if (!group_fds
|| !groups
) {
888 if (copy_from_user(group_fds
, (void __user
*)(arg
+ minsz
),
889 hdr
.count
* sizeof(*group_fds
))) {
896 * For each group_fd, get the group through the vfio external
897 * user interface and store the group and iommu ID. This
898 * ensures the group is held across the reset.
900 for (i
= 0; i
< hdr
.count
; i
++) {
901 struct vfio_group
*group
;
902 struct fd f
= fdget(group_fds
[i
]);
908 group
= vfio_group_get_external_user(f
.file
);
911 ret
= PTR_ERR(group
);
915 groups
[i
].group
= group
;
916 groups
[i
].id
= vfio_external_user_iommu_id(group
);
921 /* release reference to groups on error */
923 goto hot_reset_release
;
925 info
.count
= hdr
.count
;
926 info
.groups
= groups
;
929 * Test whether all the affected devices are contained
930 * by the set of groups provided by the user.
932 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
933 vfio_pci_validate_devs
,
936 /* User has access, do the reset */
937 ret
= slot
? pci_try_reset_slot(vdev
->pdev
->slot
) :
938 pci_try_reset_bus(vdev
->pdev
->bus
);
941 for (i
--; i
>= 0; i
--)
942 vfio_group_put_external_user(groups
[i
].group
);
951 static ssize_t
vfio_pci_rw(void *device_data
, char __user
*buf
,
952 size_t count
, loff_t
*ppos
, bool iswrite
)
954 unsigned int index
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
);
955 struct vfio_pci_device
*vdev
= device_data
;
957 if (index
>= VFIO_PCI_NUM_REGIONS
+ vdev
->num_regions
)
961 case VFIO_PCI_CONFIG_REGION_INDEX
:
962 return vfio_pci_config_rw(vdev
, buf
, count
, ppos
, iswrite
);
964 case VFIO_PCI_ROM_REGION_INDEX
:
967 return vfio_pci_bar_rw(vdev
, buf
, count
, ppos
, false);
969 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
970 return vfio_pci_bar_rw(vdev
, buf
, count
, ppos
, iswrite
);
972 case VFIO_PCI_VGA_REGION_INDEX
:
973 return vfio_pci_vga_rw(vdev
, buf
, count
, ppos
, iswrite
);
975 index
-= VFIO_PCI_NUM_REGIONS
;
976 return vdev
->region
[index
].ops
->rw(vdev
, buf
,
977 count
, ppos
, iswrite
);
983 static ssize_t
vfio_pci_read(void *device_data
, char __user
*buf
,
984 size_t count
, loff_t
*ppos
)
989 return vfio_pci_rw(device_data
, buf
, count
, ppos
, false);
992 static ssize_t
vfio_pci_write(void *device_data
, const char __user
*buf
,
993 size_t count
, loff_t
*ppos
)
998 return vfio_pci_rw(device_data
, (char __user
*)buf
, count
, ppos
, true);
1001 static int vfio_pci_mmap(void *device_data
, struct vm_area_struct
*vma
)
1003 struct vfio_pci_device
*vdev
= device_data
;
1004 struct pci_dev
*pdev
= vdev
->pdev
;
1006 u64 phys_len
, req_len
, pgoff
, req_start
;
1009 index
= vma
->vm_pgoff
>> (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
);
1011 if (vma
->vm_end
< vma
->vm_start
)
1013 if ((vma
->vm_flags
& VM_SHARED
) == 0)
1015 if (index
>= VFIO_PCI_ROM_REGION_INDEX
)
1017 if (!(pci_resource_flags(pdev
, index
) & IORESOURCE_MEM
))
1020 phys_len
= pci_resource_len(pdev
, index
);
1021 req_len
= vma
->vm_end
- vma
->vm_start
;
1022 pgoff
= vma
->vm_pgoff
&
1023 ((1U << (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
)) - 1);
1024 req_start
= pgoff
<< PAGE_SHIFT
;
1026 if (phys_len
< PAGE_SIZE
|| req_start
+ req_len
> phys_len
)
1029 if (index
== vdev
->msix_bar
) {
1031 * Disallow mmaps overlapping the MSI-X table; users don't
1032 * get to touch this directly. We could find somewhere
1033 * else to map the overlap, but page granularity is only
1034 * a recommendation, not a requirement, so the user needs
1035 * to know which bits are real. Requiring them to mmap
1036 * around the table makes that clear.
1039 /* If neither entirely above nor below, then it overlaps */
1040 if (!(req_start
>= vdev
->msix_offset
+ vdev
->msix_size
||
1041 req_start
+ req_len
<= vdev
->msix_offset
))
1046 * Even though we don't make use of the barmap for the mmap,
1047 * we need to request the region and the barmap tracks that.
1049 if (!vdev
->barmap
[index
]) {
1050 ret
= pci_request_selected_regions(pdev
,
1051 1 << index
, "vfio-pci");
1055 vdev
->barmap
[index
] = pci_iomap(pdev
, index
, 0);
1058 vma
->vm_private_data
= vdev
;
1059 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1060 vma
->vm_pgoff
= (pci_resource_start(pdev
, index
) >> PAGE_SHIFT
) + pgoff
;
1062 return remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
1063 req_len
, vma
->vm_page_prot
);
1066 static void vfio_pci_request(void *device_data
, unsigned int count
)
1068 struct vfio_pci_device
*vdev
= device_data
;
1070 mutex_lock(&vdev
->igate
);
1072 if (vdev
->req_trigger
) {
1074 dev_notice_ratelimited(&vdev
->pdev
->dev
,
1075 "Relaying device request to user (#%u)\n",
1077 eventfd_signal(vdev
->req_trigger
, 1);
1078 } else if (count
== 0) {
1079 dev_warn(&vdev
->pdev
->dev
,
1080 "No device request channel registered, blocked until released by user\n");
1083 mutex_unlock(&vdev
->igate
);
1086 static const struct vfio_device_ops vfio_pci_ops
= {
1088 .open
= vfio_pci_open
,
1089 .release
= vfio_pci_release
,
1090 .ioctl
= vfio_pci_ioctl
,
1091 .read
= vfio_pci_read
,
1092 .write
= vfio_pci_write
,
1093 .mmap
= vfio_pci_mmap
,
1094 .request
= vfio_pci_request
,
1097 static int vfio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1099 struct vfio_pci_device
*vdev
;
1100 struct iommu_group
*group
;
1103 if (pdev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
)
1106 group
= vfio_iommu_group_get(&pdev
->dev
);
1110 vdev
= kzalloc(sizeof(*vdev
), GFP_KERNEL
);
1112 vfio_iommu_group_put(group
, &pdev
->dev
);
1117 vdev
->irq_type
= VFIO_PCI_NUM_IRQS
;
1118 mutex_init(&vdev
->igate
);
1119 spin_lock_init(&vdev
->irqlock
);
1121 ret
= vfio_add_group_dev(&pdev
->dev
, &vfio_pci_ops
, vdev
);
1123 vfio_iommu_group_put(group
, &pdev
->dev
);
1128 if (vfio_pci_is_vga(pdev
)) {
1129 vga_client_register(pdev
, vdev
, NULL
, vfio_pci_set_vga_decode
);
1130 vga_set_legacy_decoding(pdev
,
1131 vfio_pci_set_vga_decode(vdev
, false));
1134 if (!disable_idle_d3
) {
1136 * pci-core sets the device power state to an unknown value at
1137 * bootup and after being removed from a driver. The only
1138 * transition it allows from this unknown state is to D0, which
1139 * typically happens when a driver calls pci_enable_device().
1140 * We're not ready to enable the device yet, but we do want to
1141 * be able to get to D3. Therefore first do a D0 transition
1142 * before going to D3.
1144 pci_set_power_state(pdev
, PCI_D0
);
1145 pci_set_power_state(pdev
, PCI_D3hot
);
1151 static void vfio_pci_remove(struct pci_dev
*pdev
)
1153 struct vfio_pci_device
*vdev
;
1155 vdev
= vfio_del_group_dev(&pdev
->dev
);
1159 vfio_iommu_group_put(pdev
->dev
.iommu_group
, &pdev
->dev
);
1160 kfree(vdev
->region
);
1163 if (vfio_pci_is_vga(pdev
)) {
1164 vga_client_register(pdev
, NULL
, NULL
, NULL
);
1165 vga_set_legacy_decoding(pdev
,
1166 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
|
1167 VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
);
1170 if (!disable_idle_d3
)
1171 pci_set_power_state(pdev
, PCI_D0
);
1174 static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev
*pdev
,
1175 pci_channel_state_t state
)
1177 struct vfio_pci_device
*vdev
;
1178 struct vfio_device
*device
;
1180 device
= vfio_device_get_from_dev(&pdev
->dev
);
1182 return PCI_ERS_RESULT_DISCONNECT
;
1184 vdev
= vfio_device_data(device
);
1186 vfio_device_put(device
);
1187 return PCI_ERS_RESULT_DISCONNECT
;
1190 mutex_lock(&vdev
->igate
);
1192 if (vdev
->err_trigger
)
1193 eventfd_signal(vdev
->err_trigger
, 1);
1195 mutex_unlock(&vdev
->igate
);
1197 vfio_device_put(device
);
1199 return PCI_ERS_RESULT_CAN_RECOVER
;
1202 static const struct pci_error_handlers vfio_err_handlers
= {
1203 .error_detected
= vfio_pci_aer_err_detected
,
1206 static struct pci_driver vfio_pci_driver
= {
1208 .id_table
= NULL
, /* only dynamic ids */
1209 .probe
= vfio_pci_probe
,
1210 .remove
= vfio_pci_remove
,
1211 .err_handler
= &vfio_err_handlers
,
1214 struct vfio_devices
{
1215 struct vfio_device
**devices
;
1220 static int vfio_pci_get_devs(struct pci_dev
*pdev
, void *data
)
1222 struct vfio_devices
*devs
= data
;
1223 struct vfio_device
*device
;
1225 if (devs
->cur_index
== devs
->max_index
)
1228 device
= vfio_device_get_from_dev(&pdev
->dev
);
1232 if (pci_dev_driver(pdev
) != &vfio_pci_driver
) {
1233 vfio_device_put(device
);
1237 devs
->devices
[devs
->cur_index
++] = device
;
1242 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1243 * this device that are needs_reset and all of the affected devices are unused
1244 * (!refcnt). Callers are required to hold driver_lock when calling this to
1245 * prevent device opens and concurrent bus reset attempts. We prevent device
1246 * unbinds by acquiring and holding a reference to the vfio_device.
1248 * NB: vfio-core considers a group to be viable even if some devices are
1249 * bound to drivers like pci-stub or pcieport. Here we require all devices
1250 * to be bound to vfio_pci since that's the only way we can be sure they
1253 static void vfio_pci_try_bus_reset(struct vfio_pci_device
*vdev
)
1255 struct vfio_devices devs
= { .cur_index
= 0 };
1256 int i
= 0, ret
= -EINVAL
;
1257 bool needs_reset
= false, slot
= false;
1258 struct vfio_pci_device
*tmp
;
1260 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
1262 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
1265 if (vfio_pci_for_each_slot_or_bus(vdev
->pdev
, vfio_pci_count_devs
,
1270 devs
.devices
= kcalloc(i
, sizeof(struct vfio_device
*), GFP_KERNEL
);
1274 if (vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
1275 vfio_pci_get_devs
, &devs
, slot
))
1278 for (i
= 0; i
< devs
.cur_index
; i
++) {
1279 tmp
= vfio_device_data(devs
.devices
[i
]);
1280 if (tmp
->needs_reset
)
1287 ret
= slot
? pci_try_reset_slot(vdev
->pdev
->slot
) :
1288 pci_try_reset_bus(vdev
->pdev
->bus
);
1291 for (i
= 0; i
< devs
.cur_index
; i
++) {
1292 tmp
= vfio_device_data(devs
.devices
[i
]);
1294 tmp
->needs_reset
= false;
1296 if (!tmp
->refcnt
&& !disable_idle_d3
)
1297 pci_set_power_state(tmp
->pdev
, PCI_D3hot
);
1299 vfio_device_put(devs
.devices
[i
]);
1302 kfree(devs
.devices
);
1305 static void __exit
vfio_pci_cleanup(void)
1307 pci_unregister_driver(&vfio_pci_driver
);
1308 vfio_pci_uninit_perm_bits();
1311 static void __init
vfio_pci_fill_ids(void)
1316 /* no ids passed actually */
1320 /* add ids specified in the module parameter */
1322 while ((id
= strsep(&p
, ","))) {
1323 unsigned int vendor
, device
, subvendor
= PCI_ANY_ID
,
1324 subdevice
= PCI_ANY_ID
, class = 0, class_mask
= 0;
1330 fields
= sscanf(id
, "%x:%x:%x:%x:%x:%x",
1331 &vendor
, &device
, &subvendor
, &subdevice
,
1332 &class, &class_mask
);
1335 pr_warn("invalid id string \"%s\"\n", id
);
1339 rc
= pci_add_dynid(&vfio_pci_driver
, vendor
, device
,
1340 subvendor
, subdevice
, class, class_mask
, 0);
1342 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1343 vendor
, device
, subvendor
, subdevice
,
1344 class, class_mask
, rc
);
1346 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1347 vendor
, device
, subvendor
, subdevice
,
1352 static int __init
vfio_pci_init(void)
1356 /* Allocate shared config space permision data used by all devices */
1357 ret
= vfio_pci_init_perm_bits();
1361 /* Register and scan for devices */
1362 ret
= pci_register_driver(&vfio_pci_driver
);
1366 vfio_pci_fill_ids();
1371 vfio_pci_uninit_perm_bits();
1375 module_init(vfio_pci_init
);
1376 module_exit(vfio_pci_cleanup
);
1378 MODULE_VERSION(DRIVER_VERSION
);
1379 MODULE_LICENSE("GPL v2");
1380 MODULE_AUTHOR(DRIVER_AUTHOR
);
1381 MODULE_DESCRIPTION(DRIVER_DESC
);