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_probe_mmaps(struct vfio_pci_device
*vdev
)
115 struct resource
*res
;
117 struct vfio_pci_dummy_resource
*dummy_res
;
119 INIT_LIST_HEAD(&vdev
->dummy_resources_list
);
121 for (bar
= PCI_STD_RESOURCES
; bar
<= PCI_STD_RESOURCE_END
; bar
++) {
122 res
= vdev
->pdev
->resource
+ bar
;
124 if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP
))
127 if (!(res
->flags
& IORESOURCE_MEM
))
131 * The PCI core shouldn't set up a resource with a
132 * type but zero size. But there may be bugs that
133 * cause us to do that.
135 if (!resource_size(res
))
138 if (resource_size(res
) >= PAGE_SIZE
) {
139 vdev
->bar_mmap_supported
[bar
] = true;
143 if (!(res
->start
& ~PAGE_MASK
)) {
145 * Add a dummy resource to reserve the remainder
146 * of the exclusive page in case that hot-add
147 * device's bar is assigned into it.
149 dummy_res
= kzalloc(sizeof(*dummy_res
), GFP_KERNEL
);
150 if (dummy_res
== NULL
)
153 dummy_res
->resource
.name
= "vfio sub-page reserved";
154 dummy_res
->resource
.start
= res
->end
+ 1;
155 dummy_res
->resource
.end
= res
->start
+ PAGE_SIZE
- 1;
156 dummy_res
->resource
.flags
= res
->flags
;
157 if (request_resource(res
->parent
,
158 &dummy_res
->resource
)) {
162 dummy_res
->index
= bar
;
163 list_add(&dummy_res
->res_next
,
164 &vdev
->dummy_resources_list
);
165 vdev
->bar_mmap_supported
[bar
] = true;
169 * Here we don't handle the case when the BAR is not page
170 * aligned because we can't expect the BAR will be
171 * assigned into the same location in a page in guest
172 * when we passthrough the BAR. And it's hard to access
173 * this BAR in userspace because we have no way to get
174 * the BAR's location in a page.
177 vdev
->bar_mmap_supported
[bar
] = false;
181 static void vfio_pci_try_bus_reset(struct vfio_pci_device
*vdev
);
182 static void vfio_pci_disable(struct vfio_pci_device
*vdev
);
185 * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
186 * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
187 * If a device implements the former but not the latter we would typically
188 * expect broken_intx_masking be set and require an exclusive interrupt.
189 * However since we do have control of the device's ability to assert INTx,
190 * we can instead pretend that the device does not implement INTx, virtualizing
191 * the pin register to report zero and maintaining DisINTx set on the host.
193 static bool vfio_pci_nointx(struct pci_dev
*pdev
)
195 switch (pdev
->vendor
) {
196 case PCI_VENDOR_ID_INTEL
:
197 switch (pdev
->device
) {
198 /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */
201 case 0x1580 ... 0x1581:
202 case 0x1583 ... 0x158b:
203 case 0x37d0 ... 0x37d2:
216 static int vfio_pci_enable(struct vfio_pci_device
*vdev
)
218 struct pci_dev
*pdev
= vdev
->pdev
;
223 pci_set_power_state(pdev
, PCI_D0
);
225 /* Don't allow our initial saved state to include busmaster */
226 pci_clear_master(pdev
);
228 ret
= pci_enable_device(pdev
);
232 /* If reset fails because of the device lock, fail this path entirely */
233 ret
= pci_try_reset_function(pdev
);
234 if (ret
== -EAGAIN
) {
235 pci_disable_device(pdev
);
239 vdev
->reset_works
= !ret
;
240 pci_save_state(pdev
);
241 vdev
->pci_saved_state
= pci_store_saved_state(pdev
);
242 if (!vdev
->pci_saved_state
)
243 pr_debug("%s: Couldn't store %s saved state\n",
244 __func__
, dev_name(&pdev
->dev
));
246 if (likely(!nointxmask
)) {
247 if (vfio_pci_nointx(pdev
)) {
248 dev_info(&pdev
->dev
, "Masking broken INTx support\n");
252 vdev
->pci_2_3
= pci_intx_mask_supported(pdev
);
255 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
256 if (vdev
->pci_2_3
&& (cmd
& PCI_COMMAND_INTX_DISABLE
)) {
257 cmd
&= ~PCI_COMMAND_INTX_DISABLE
;
258 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
);
261 ret
= vfio_config_init(vdev
);
263 kfree(vdev
->pci_saved_state
);
264 vdev
->pci_saved_state
= NULL
;
265 pci_disable_device(pdev
);
269 msix_pos
= pdev
->msix_cap
;
274 pci_read_config_word(pdev
, msix_pos
+ PCI_MSIX_FLAGS
, &flags
);
275 pci_read_config_dword(pdev
, msix_pos
+ PCI_MSIX_TABLE
, &table
);
277 vdev
->msix_bar
= table
& PCI_MSIX_TABLE_BIR
;
278 vdev
->msix_offset
= table
& PCI_MSIX_TABLE_OFFSET
;
279 vdev
->msix_size
= ((flags
& PCI_MSIX_FLAGS_QSIZE
) + 1) * 16;
281 vdev
->msix_bar
= 0xFF;
283 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev
))
284 vdev
->has_vga
= true;
287 if (vfio_pci_is_vga(pdev
) &&
288 pdev
->vendor
== PCI_VENDOR_ID_INTEL
&&
289 IS_ENABLED(CONFIG_VFIO_PCI_IGD
)) {
290 ret
= vfio_pci_igd_init(vdev
);
292 dev_warn(&vdev
->pdev
->dev
,
293 "Failed to setup Intel IGD regions\n");
294 vfio_pci_disable(vdev
);
299 vfio_pci_probe_mmaps(vdev
);
304 static void vfio_pci_disable(struct vfio_pci_device
*vdev
)
306 struct pci_dev
*pdev
= vdev
->pdev
;
307 struct vfio_pci_dummy_resource
*dummy_res
, *tmp
;
310 /* Stop the device from further DMA */
311 pci_clear_master(pdev
);
313 vfio_pci_set_irqs_ioctl(vdev
, VFIO_IRQ_SET_DATA_NONE
|
314 VFIO_IRQ_SET_ACTION_TRIGGER
,
315 vdev
->irq_type
, 0, 0, NULL
);
317 vdev
->virq_disabled
= false;
319 for (i
= 0; i
< vdev
->num_regions
; i
++)
320 vdev
->region
[i
].ops
->release(vdev
, &vdev
->region
[i
]);
322 vdev
->num_regions
= 0;
324 vdev
->region
= NULL
; /* don't krealloc a freed pointer */
326 vfio_config_free(vdev
);
328 for (bar
= PCI_STD_RESOURCES
; bar
<= PCI_STD_RESOURCE_END
; bar
++) {
329 if (!vdev
->barmap
[bar
])
331 pci_iounmap(pdev
, vdev
->barmap
[bar
]);
332 pci_release_selected_regions(pdev
, 1 << bar
);
333 vdev
->barmap
[bar
] = NULL
;
336 list_for_each_entry_safe(dummy_res
, tmp
,
337 &vdev
->dummy_resources_list
, res_next
) {
338 list_del(&dummy_res
->res_next
);
339 release_resource(&dummy_res
->resource
);
343 vdev
->needs_reset
= true;
346 * If we have saved state, restore it. If we can reset the device,
347 * even better. Resetting with current state seems better than
348 * nothing, but saving and restoring current state without reset
351 if (pci_load_and_free_saved_state(pdev
, &vdev
->pci_saved_state
)) {
352 pr_info("%s: Couldn't reload %s saved state\n",
353 __func__
, dev_name(&pdev
->dev
));
355 if (!vdev
->reset_works
)
358 pci_save_state(pdev
);
362 * Disable INTx and MSI, presumably to avoid spurious interrupts
363 * during reset. Stolen from pci_reset_function()
365 pci_write_config_word(pdev
, PCI_COMMAND
, PCI_COMMAND_INTX_DISABLE
);
368 * Try to reset the device. The success of this is dependent on
369 * being able to lock the device, which is not always possible.
371 if (vdev
->reset_works
&& !pci_try_reset_function(pdev
))
372 vdev
->needs_reset
= false;
374 pci_restore_state(pdev
);
376 pci_disable_device(pdev
);
378 vfio_pci_try_bus_reset(vdev
);
380 if (!disable_idle_d3
)
381 pci_set_power_state(pdev
, PCI_D3hot
);
384 static void vfio_pci_release(void *device_data
)
386 struct vfio_pci_device
*vdev
= device_data
;
388 mutex_lock(&driver_lock
);
390 if (!(--vdev
->refcnt
)) {
391 vfio_spapr_pci_eeh_release(vdev
->pdev
);
392 vfio_pci_disable(vdev
);
395 mutex_unlock(&driver_lock
);
397 module_put(THIS_MODULE
);
400 static int vfio_pci_open(void *device_data
)
402 struct vfio_pci_device
*vdev
= device_data
;
405 if (!try_module_get(THIS_MODULE
))
408 mutex_lock(&driver_lock
);
411 ret
= vfio_pci_enable(vdev
);
415 vfio_spapr_pci_eeh_open(vdev
->pdev
);
419 mutex_unlock(&driver_lock
);
421 module_put(THIS_MODULE
);
425 static int vfio_pci_get_irq_count(struct vfio_pci_device
*vdev
, int irq_type
)
427 if (irq_type
== VFIO_PCI_INTX_IRQ_INDEX
) {
429 pci_read_config_byte(vdev
->pdev
, PCI_INTERRUPT_PIN
, &pin
);
430 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX
) && !vdev
->nointx
&& pin
)
433 } else if (irq_type
== VFIO_PCI_MSI_IRQ_INDEX
) {
437 pos
= vdev
->pdev
->msi_cap
;
439 pci_read_config_word(vdev
->pdev
,
440 pos
+ PCI_MSI_FLAGS
, &flags
);
441 return 1 << ((flags
& PCI_MSI_FLAGS_QMASK
) >> 1);
443 } else if (irq_type
== VFIO_PCI_MSIX_IRQ_INDEX
) {
447 pos
= vdev
->pdev
->msix_cap
;
449 pci_read_config_word(vdev
->pdev
,
450 pos
+ PCI_MSIX_FLAGS
, &flags
);
452 return (flags
& PCI_MSIX_FLAGS_QSIZE
) + 1;
454 } else if (irq_type
== VFIO_PCI_ERR_IRQ_INDEX
) {
455 if (pci_is_pcie(vdev
->pdev
))
457 } else if (irq_type
== VFIO_PCI_REQ_IRQ_INDEX
) {
464 static int vfio_pci_count_devs(struct pci_dev
*pdev
, void *data
)
470 struct vfio_pci_fill_info
{
473 struct vfio_pci_dependent_device
*devices
;
476 static int vfio_pci_fill_devs(struct pci_dev
*pdev
, void *data
)
478 struct vfio_pci_fill_info
*fill
= data
;
479 struct iommu_group
*iommu_group
;
481 if (fill
->cur
== fill
->max
)
482 return -EAGAIN
; /* Something changed, try again */
484 iommu_group
= iommu_group_get(&pdev
->dev
);
486 return -EPERM
; /* Cannot reset non-isolated devices */
488 fill
->devices
[fill
->cur
].group_id
= iommu_group_id(iommu_group
);
489 fill
->devices
[fill
->cur
].segment
= pci_domain_nr(pdev
->bus
);
490 fill
->devices
[fill
->cur
].bus
= pdev
->bus
->number
;
491 fill
->devices
[fill
->cur
].devfn
= pdev
->devfn
;
493 iommu_group_put(iommu_group
);
497 struct vfio_pci_group_entry
{
498 struct vfio_group
*group
;
502 struct vfio_pci_group_info
{
504 struct vfio_pci_group_entry
*groups
;
507 static int vfio_pci_validate_devs(struct pci_dev
*pdev
, void *data
)
509 struct vfio_pci_group_info
*info
= data
;
510 struct iommu_group
*group
;
513 group
= iommu_group_get(&pdev
->dev
);
517 id
= iommu_group_id(group
);
519 for (i
= 0; i
< info
->count
; i
++)
520 if (info
->groups
[i
].id
== id
)
523 iommu_group_put(group
);
525 return (i
== info
->count
) ? -EINVAL
: 0;
528 static bool vfio_pci_dev_below_slot(struct pci_dev
*pdev
, struct pci_slot
*slot
)
530 for (; pdev
; pdev
= pdev
->bus
->self
)
531 if (pdev
->bus
== slot
->bus
)
532 return (pdev
->slot
== slot
);
536 struct vfio_pci_walk_info
{
537 int (*fn
)(struct pci_dev
*, void *data
);
539 struct pci_dev
*pdev
;
544 static int vfio_pci_walk_wrapper(struct pci_dev
*pdev
, void *data
)
546 struct vfio_pci_walk_info
*walk
= data
;
548 if (!walk
->slot
|| vfio_pci_dev_below_slot(pdev
, walk
->pdev
->slot
))
549 walk
->ret
= walk
->fn(pdev
, walk
->data
);
554 static int vfio_pci_for_each_slot_or_bus(struct pci_dev
*pdev
,
555 int (*fn
)(struct pci_dev
*,
556 void *data
), void *data
,
559 struct vfio_pci_walk_info walk
= {
560 .fn
= fn
, .data
= data
, .pdev
= pdev
, .slot
= slot
, .ret
= 0,
563 pci_walk_bus(pdev
->bus
, vfio_pci_walk_wrapper
, &walk
);
568 static int msix_mmappable_cap(struct vfio_pci_device
*vdev
,
569 struct vfio_info_cap
*caps
)
571 struct vfio_info_cap_header header
= {
572 .id
= VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
,
576 return vfio_info_add_capability(caps
, &header
, sizeof(header
));
579 int vfio_pci_register_dev_region(struct vfio_pci_device
*vdev
,
580 unsigned int type
, unsigned int subtype
,
581 const struct vfio_pci_regops
*ops
,
582 size_t size
, u32 flags
, void *data
)
584 struct vfio_pci_region
*region
;
586 region
= krealloc(vdev
->region
,
587 (vdev
->num_regions
+ 1) * sizeof(*region
),
592 vdev
->region
= region
;
593 vdev
->region
[vdev
->num_regions
].type
= type
;
594 vdev
->region
[vdev
->num_regions
].subtype
= subtype
;
595 vdev
->region
[vdev
->num_regions
].ops
= ops
;
596 vdev
->region
[vdev
->num_regions
].size
= size
;
597 vdev
->region
[vdev
->num_regions
].flags
= flags
;
598 vdev
->region
[vdev
->num_regions
].data
= data
;
605 static long vfio_pci_ioctl(void *device_data
,
606 unsigned int cmd
, unsigned long arg
)
608 struct vfio_pci_device
*vdev
= device_data
;
611 if (cmd
== VFIO_DEVICE_GET_INFO
) {
612 struct vfio_device_info info
;
614 minsz
= offsetofend(struct vfio_device_info
, num_irqs
);
616 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
619 if (info
.argsz
< minsz
)
622 info
.flags
= VFIO_DEVICE_FLAGS_PCI
;
624 if (vdev
->reset_works
)
625 info
.flags
|= VFIO_DEVICE_FLAGS_RESET
;
627 info
.num_regions
= VFIO_PCI_NUM_REGIONS
+ vdev
->num_regions
;
628 info
.num_irqs
= VFIO_PCI_NUM_IRQS
;
630 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
633 } else if (cmd
== VFIO_DEVICE_GET_REGION_INFO
) {
634 struct pci_dev
*pdev
= vdev
->pdev
;
635 struct vfio_region_info info
;
636 struct vfio_info_cap caps
= { .buf
= NULL
, .size
= 0 };
639 minsz
= offsetofend(struct vfio_region_info
, offset
);
641 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
644 if (info
.argsz
< minsz
)
647 switch (info
.index
) {
648 case VFIO_PCI_CONFIG_REGION_INDEX
:
649 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
650 info
.size
= pdev
->cfg_size
;
651 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
652 VFIO_REGION_INFO_FLAG_WRITE
;
654 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
655 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
656 info
.size
= pci_resource_len(pdev
, info
.index
);
662 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
663 VFIO_REGION_INFO_FLAG_WRITE
;
664 if (vdev
->bar_mmap_supported
[info
.index
]) {
665 info
.flags
|= VFIO_REGION_INFO_FLAG_MMAP
;
666 if (info
.index
== vdev
->msix_bar
) {
667 ret
= msix_mmappable_cap(vdev
, &caps
);
674 case VFIO_PCI_ROM_REGION_INDEX
:
679 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
682 /* Report the BAR size, not the ROM size */
683 info
.size
= pci_resource_len(pdev
, info
.index
);
685 /* Shadow ROMs appear as PCI option ROMs */
686 if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
687 IORESOURCE_ROM_SHADOW
)
693 /* Is it really there? */
694 io
= pci_map_rom(pdev
, &size
);
699 pci_unmap_rom(pdev
, io
);
701 info
.flags
= VFIO_REGION_INFO_FLAG_READ
;
704 case VFIO_PCI_VGA_REGION_INDEX
:
708 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
710 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
711 VFIO_REGION_INFO_FLAG_WRITE
;
716 struct vfio_region_info_cap_type cap_type
= {
717 .header
.id
= VFIO_REGION_INFO_CAP_TYPE
,
718 .header
.version
= 1 };
721 VFIO_PCI_NUM_REGIONS
+ vdev
->num_regions
)
724 i
= info
.index
- VFIO_PCI_NUM_REGIONS
;
726 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
727 info
.size
= vdev
->region
[i
].size
;
728 info
.flags
= vdev
->region
[i
].flags
;
730 cap_type
.type
= vdev
->region
[i
].type
;
731 cap_type
.subtype
= vdev
->region
[i
].subtype
;
733 ret
= vfio_info_add_capability(&caps
, &cap_type
.header
,
742 info
.flags
|= VFIO_REGION_INFO_FLAG_CAPS
;
743 if (info
.argsz
< sizeof(info
) + caps
.size
) {
744 info
.argsz
= sizeof(info
) + caps
.size
;
747 vfio_info_cap_shift(&caps
, sizeof(info
));
748 if (copy_to_user((void __user
*)arg
+
749 sizeof(info
), caps
.buf
,
754 info
.cap_offset
= sizeof(info
);
760 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
763 } else if (cmd
== VFIO_DEVICE_GET_IRQ_INFO
) {
764 struct vfio_irq_info info
;
766 minsz
= offsetofend(struct vfio_irq_info
, count
);
768 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
771 if (info
.argsz
< minsz
|| info
.index
>= VFIO_PCI_NUM_IRQS
)
774 switch (info
.index
) {
775 case VFIO_PCI_INTX_IRQ_INDEX
... VFIO_PCI_MSIX_IRQ_INDEX
:
776 case VFIO_PCI_REQ_IRQ_INDEX
:
778 case VFIO_PCI_ERR_IRQ_INDEX
:
779 if (pci_is_pcie(vdev
->pdev
))
781 /* pass thru to return error */
786 info
.flags
= VFIO_IRQ_INFO_EVENTFD
;
788 info
.count
= vfio_pci_get_irq_count(vdev
, info
.index
);
790 if (info
.index
== VFIO_PCI_INTX_IRQ_INDEX
)
791 info
.flags
|= (VFIO_IRQ_INFO_MASKABLE
|
792 VFIO_IRQ_INFO_AUTOMASKED
);
794 info
.flags
|= VFIO_IRQ_INFO_NORESIZE
;
796 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
799 } else if (cmd
== VFIO_DEVICE_SET_IRQS
) {
800 struct vfio_irq_set hdr
;
803 size_t data_size
= 0;
805 minsz
= offsetofend(struct vfio_irq_set
, count
);
807 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
810 max
= vfio_pci_get_irq_count(vdev
, hdr
.index
);
812 ret
= vfio_set_irqs_validate_and_prepare(&hdr
, max
,
813 VFIO_PCI_NUM_IRQS
, &data_size
);
818 data
= memdup_user((void __user
*)(arg
+ minsz
),
821 return PTR_ERR(data
);
824 mutex_lock(&vdev
->igate
);
826 ret
= vfio_pci_set_irqs_ioctl(vdev
, hdr
.flags
, hdr
.index
,
827 hdr
.start
, hdr
.count
, data
);
829 mutex_unlock(&vdev
->igate
);
834 } else if (cmd
== VFIO_DEVICE_RESET
) {
835 return vdev
->reset_works
?
836 pci_try_reset_function(vdev
->pdev
) : -EINVAL
;
838 } else if (cmd
== VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
) {
839 struct vfio_pci_hot_reset_info hdr
;
840 struct vfio_pci_fill_info fill
= { 0 };
841 struct vfio_pci_dependent_device
*devices
= NULL
;
845 minsz
= offsetofend(struct vfio_pci_hot_reset_info
, count
);
847 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
850 if (hdr
.argsz
< minsz
)
855 /* Can we do a slot or bus reset or neither? */
856 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
858 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
861 /* How many devices are affected? */
862 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
868 WARN_ON(!fill
.max
); /* Should always be at least one */
871 * If there's enough space, fill it now, otherwise return
872 * -ENOSPC and the number of devices affected.
874 if (hdr
.argsz
< sizeof(hdr
) + (fill
.max
* sizeof(*devices
))) {
876 hdr
.count
= fill
.max
;
877 goto reset_info_exit
;
880 devices
= kcalloc(fill
.max
, sizeof(*devices
), GFP_KERNEL
);
884 fill
.devices
= devices
;
886 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
891 * If a device was removed between counting and filling,
892 * we may come up short of fill.max. If a device was
893 * added, we'll have a return of -EAGAIN above.
896 hdr
.count
= fill
.cur
;
899 if (copy_to_user((void __user
*)arg
, &hdr
, minsz
))
903 if (copy_to_user((void __user
*)(arg
+ minsz
), devices
,
904 hdr
.count
* sizeof(*devices
)))
911 } else if (cmd
== VFIO_DEVICE_PCI_HOT_RESET
) {
912 struct vfio_pci_hot_reset hdr
;
914 struct vfio_pci_group_entry
*groups
;
915 struct vfio_pci_group_info info
;
917 int i
, count
= 0, ret
= 0;
919 minsz
= offsetofend(struct vfio_pci_hot_reset
, count
);
921 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
924 if (hdr
.argsz
< minsz
|| hdr
.flags
)
927 /* Can we do a slot or bus reset or neither? */
928 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
930 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
934 * We can't let userspace give us an arbitrarily large
935 * buffer to copy, so verify how many we think there
936 * could be. Note groups can have multiple devices so
937 * one group per device is the max.
939 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
945 /* Somewhere between 1 and count is OK */
946 if (!hdr
.count
|| hdr
.count
> count
)
949 group_fds
= kcalloc(hdr
.count
, sizeof(*group_fds
), GFP_KERNEL
);
950 groups
= kcalloc(hdr
.count
, sizeof(*groups
), GFP_KERNEL
);
951 if (!group_fds
|| !groups
) {
957 if (copy_from_user(group_fds
, (void __user
*)(arg
+ minsz
),
958 hdr
.count
* sizeof(*group_fds
))) {
965 * For each group_fd, get the group through the vfio external
966 * user interface and store the group and iommu ID. This
967 * ensures the group is held across the reset.
969 for (i
= 0; i
< hdr
.count
; i
++) {
970 struct vfio_group
*group
;
971 struct fd f
= fdget(group_fds
[i
]);
977 group
= vfio_group_get_external_user(f
.file
);
980 ret
= PTR_ERR(group
);
984 groups
[i
].group
= group
;
985 groups
[i
].id
= vfio_external_user_iommu_id(group
);
990 /* release reference to groups on error */
992 goto hot_reset_release
;
994 info
.count
= hdr
.count
;
995 info
.groups
= groups
;
998 * Test whether all the affected devices are contained
999 * by the set of groups provided by the user.
1001 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
1002 vfio_pci_validate_devs
,
1005 /* User has access, do the reset */
1006 ret
= slot
? pci_try_reset_slot(vdev
->pdev
->slot
) :
1007 pci_try_reset_bus(vdev
->pdev
->bus
);
1010 for (i
--; i
>= 0; i
--)
1011 vfio_group_put_external_user(groups
[i
].group
);
1020 static ssize_t
vfio_pci_rw(void *device_data
, char __user
*buf
,
1021 size_t count
, loff_t
*ppos
, bool iswrite
)
1023 unsigned int index
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
);
1024 struct vfio_pci_device
*vdev
= device_data
;
1026 if (index
>= VFIO_PCI_NUM_REGIONS
+ vdev
->num_regions
)
1030 case VFIO_PCI_CONFIG_REGION_INDEX
:
1031 return vfio_pci_config_rw(vdev
, buf
, count
, ppos
, iswrite
);
1033 case VFIO_PCI_ROM_REGION_INDEX
:
1036 return vfio_pci_bar_rw(vdev
, buf
, count
, ppos
, false);
1038 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
1039 return vfio_pci_bar_rw(vdev
, buf
, count
, ppos
, iswrite
);
1041 case VFIO_PCI_VGA_REGION_INDEX
:
1042 return vfio_pci_vga_rw(vdev
, buf
, count
, ppos
, iswrite
);
1044 index
-= VFIO_PCI_NUM_REGIONS
;
1045 return vdev
->region
[index
].ops
->rw(vdev
, buf
,
1046 count
, ppos
, iswrite
);
1052 static ssize_t
vfio_pci_read(void *device_data
, char __user
*buf
,
1053 size_t count
, loff_t
*ppos
)
1058 return vfio_pci_rw(device_data
, buf
, count
, ppos
, false);
1061 static ssize_t
vfio_pci_write(void *device_data
, const char __user
*buf
,
1062 size_t count
, loff_t
*ppos
)
1067 return vfio_pci_rw(device_data
, (char __user
*)buf
, count
, ppos
, true);
1070 static int vfio_pci_mmap(void *device_data
, struct vm_area_struct
*vma
)
1072 struct vfio_pci_device
*vdev
= device_data
;
1073 struct pci_dev
*pdev
= vdev
->pdev
;
1075 u64 phys_len
, req_len
, pgoff
, req_start
;
1078 index
= vma
->vm_pgoff
>> (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
);
1080 if (vma
->vm_end
< vma
->vm_start
)
1082 if ((vma
->vm_flags
& VM_SHARED
) == 0)
1084 if (index
>= VFIO_PCI_ROM_REGION_INDEX
)
1086 if (!vdev
->bar_mmap_supported
[index
])
1089 phys_len
= PAGE_ALIGN(pci_resource_len(pdev
, index
));
1090 req_len
= vma
->vm_end
- vma
->vm_start
;
1091 pgoff
= vma
->vm_pgoff
&
1092 ((1U << (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
)) - 1);
1093 req_start
= pgoff
<< PAGE_SHIFT
;
1095 if (req_start
+ req_len
> phys_len
)
1099 * Even though we don't make use of the barmap for the mmap,
1100 * we need to request the region and the barmap tracks that.
1102 if (!vdev
->barmap
[index
]) {
1103 ret
= pci_request_selected_regions(pdev
,
1104 1 << index
, "vfio-pci");
1108 vdev
->barmap
[index
] = pci_iomap(pdev
, index
, 0);
1109 if (!vdev
->barmap
[index
]) {
1110 pci_release_selected_regions(pdev
, 1 << index
);
1115 vma
->vm_private_data
= vdev
;
1116 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1117 vma
->vm_pgoff
= (pci_resource_start(pdev
, index
) >> PAGE_SHIFT
) + pgoff
;
1119 return remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
1120 req_len
, vma
->vm_page_prot
);
1123 static void vfio_pci_request(void *device_data
, unsigned int count
)
1125 struct vfio_pci_device
*vdev
= device_data
;
1127 mutex_lock(&vdev
->igate
);
1129 if (vdev
->req_trigger
) {
1131 dev_notice_ratelimited(&vdev
->pdev
->dev
,
1132 "Relaying device request to user (#%u)\n",
1134 eventfd_signal(vdev
->req_trigger
, 1);
1135 } else if (count
== 0) {
1136 dev_warn(&vdev
->pdev
->dev
,
1137 "No device request channel registered, blocked until released by user\n");
1140 mutex_unlock(&vdev
->igate
);
1143 static const struct vfio_device_ops vfio_pci_ops
= {
1145 .open
= vfio_pci_open
,
1146 .release
= vfio_pci_release
,
1147 .ioctl
= vfio_pci_ioctl
,
1148 .read
= vfio_pci_read
,
1149 .write
= vfio_pci_write
,
1150 .mmap
= vfio_pci_mmap
,
1151 .request
= vfio_pci_request
,
1154 static int vfio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1156 struct vfio_pci_device
*vdev
;
1157 struct iommu_group
*group
;
1160 if (pdev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
)
1163 group
= vfio_iommu_group_get(&pdev
->dev
);
1167 vdev
= kzalloc(sizeof(*vdev
), GFP_KERNEL
);
1169 vfio_iommu_group_put(group
, &pdev
->dev
);
1174 vdev
->irq_type
= VFIO_PCI_NUM_IRQS
;
1175 mutex_init(&vdev
->igate
);
1176 spin_lock_init(&vdev
->irqlock
);
1178 ret
= vfio_add_group_dev(&pdev
->dev
, &vfio_pci_ops
, vdev
);
1180 vfio_iommu_group_put(group
, &pdev
->dev
);
1185 if (vfio_pci_is_vga(pdev
)) {
1186 vga_client_register(pdev
, vdev
, NULL
, vfio_pci_set_vga_decode
);
1187 vga_set_legacy_decoding(pdev
,
1188 vfio_pci_set_vga_decode(vdev
, false));
1191 if (!disable_idle_d3
) {
1193 * pci-core sets the device power state to an unknown value at
1194 * bootup and after being removed from a driver. The only
1195 * transition it allows from this unknown state is to D0, which
1196 * typically happens when a driver calls pci_enable_device().
1197 * We're not ready to enable the device yet, but we do want to
1198 * be able to get to D3. Therefore first do a D0 transition
1199 * before going to D3.
1201 pci_set_power_state(pdev
, PCI_D0
);
1202 pci_set_power_state(pdev
, PCI_D3hot
);
1208 static void vfio_pci_remove(struct pci_dev
*pdev
)
1210 struct vfio_pci_device
*vdev
;
1212 vdev
= vfio_del_group_dev(&pdev
->dev
);
1216 vfio_iommu_group_put(pdev
->dev
.iommu_group
, &pdev
->dev
);
1217 kfree(vdev
->region
);
1220 if (vfio_pci_is_vga(pdev
)) {
1221 vga_client_register(pdev
, NULL
, NULL
, NULL
);
1222 vga_set_legacy_decoding(pdev
,
1223 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
|
1224 VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
);
1227 if (!disable_idle_d3
)
1228 pci_set_power_state(pdev
, PCI_D0
);
1231 static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev
*pdev
,
1232 pci_channel_state_t state
)
1234 struct vfio_pci_device
*vdev
;
1235 struct vfio_device
*device
;
1237 device
= vfio_device_get_from_dev(&pdev
->dev
);
1239 return PCI_ERS_RESULT_DISCONNECT
;
1241 vdev
= vfio_device_data(device
);
1243 vfio_device_put(device
);
1244 return PCI_ERS_RESULT_DISCONNECT
;
1247 mutex_lock(&vdev
->igate
);
1249 if (vdev
->err_trigger
)
1250 eventfd_signal(vdev
->err_trigger
, 1);
1252 mutex_unlock(&vdev
->igate
);
1254 vfio_device_put(device
);
1256 return PCI_ERS_RESULT_CAN_RECOVER
;
1259 static const struct pci_error_handlers vfio_err_handlers
= {
1260 .error_detected
= vfio_pci_aer_err_detected
,
1263 static struct pci_driver vfio_pci_driver
= {
1265 .id_table
= NULL
, /* only dynamic ids */
1266 .probe
= vfio_pci_probe
,
1267 .remove
= vfio_pci_remove
,
1268 .err_handler
= &vfio_err_handlers
,
1271 struct vfio_devices
{
1272 struct vfio_device
**devices
;
1277 static int vfio_pci_get_devs(struct pci_dev
*pdev
, void *data
)
1279 struct vfio_devices
*devs
= data
;
1280 struct vfio_device
*device
;
1282 if (devs
->cur_index
== devs
->max_index
)
1285 device
= vfio_device_get_from_dev(&pdev
->dev
);
1289 if (pci_dev_driver(pdev
) != &vfio_pci_driver
) {
1290 vfio_device_put(device
);
1294 devs
->devices
[devs
->cur_index
++] = device
;
1299 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1300 * this device that are needs_reset and all of the affected devices are unused
1301 * (!refcnt). Callers are required to hold driver_lock when calling this to
1302 * prevent device opens and concurrent bus reset attempts. We prevent device
1303 * unbinds by acquiring and holding a reference to the vfio_device.
1305 * NB: vfio-core considers a group to be viable even if some devices are
1306 * bound to drivers like pci-stub or pcieport. Here we require all devices
1307 * to be bound to vfio_pci since that's the only way we can be sure they
1310 static void vfio_pci_try_bus_reset(struct vfio_pci_device
*vdev
)
1312 struct vfio_devices devs
= { .cur_index
= 0 };
1313 int i
= 0, ret
= -EINVAL
;
1314 bool needs_reset
= false, slot
= false;
1315 struct vfio_pci_device
*tmp
;
1317 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
1319 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
1322 if (vfio_pci_for_each_slot_or_bus(vdev
->pdev
, vfio_pci_count_devs
,
1327 devs
.devices
= kcalloc(i
, sizeof(struct vfio_device
*), GFP_KERNEL
);
1331 if (vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
1332 vfio_pci_get_devs
, &devs
, slot
))
1335 for (i
= 0; i
< devs
.cur_index
; i
++) {
1336 tmp
= vfio_device_data(devs
.devices
[i
]);
1337 if (tmp
->needs_reset
)
1344 ret
= slot
? pci_try_reset_slot(vdev
->pdev
->slot
) :
1345 pci_try_reset_bus(vdev
->pdev
->bus
);
1348 for (i
= 0; i
< devs
.cur_index
; i
++) {
1349 tmp
= vfio_device_data(devs
.devices
[i
]);
1351 tmp
->needs_reset
= false;
1353 if (!tmp
->refcnt
&& !disable_idle_d3
)
1354 pci_set_power_state(tmp
->pdev
, PCI_D3hot
);
1356 vfio_device_put(devs
.devices
[i
]);
1359 kfree(devs
.devices
);
1362 static void __exit
vfio_pci_cleanup(void)
1364 pci_unregister_driver(&vfio_pci_driver
);
1365 vfio_pci_uninit_perm_bits();
1368 static void __init
vfio_pci_fill_ids(void)
1373 /* no ids passed actually */
1377 /* add ids specified in the module parameter */
1379 while ((id
= strsep(&p
, ","))) {
1380 unsigned int vendor
, device
, subvendor
= PCI_ANY_ID
,
1381 subdevice
= PCI_ANY_ID
, class = 0, class_mask
= 0;
1387 fields
= sscanf(id
, "%x:%x:%x:%x:%x:%x",
1388 &vendor
, &device
, &subvendor
, &subdevice
,
1389 &class, &class_mask
);
1392 pr_warn("invalid id string \"%s\"\n", id
);
1396 rc
= pci_add_dynid(&vfio_pci_driver
, vendor
, device
,
1397 subvendor
, subdevice
, class, class_mask
, 0);
1399 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1400 vendor
, device
, subvendor
, subdevice
,
1401 class, class_mask
, rc
);
1403 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1404 vendor
, device
, subvendor
, subdevice
,
1409 static int __init
vfio_pci_init(void)
1413 /* Allocate shared config space permision data used by all devices */
1414 ret
= vfio_pci_init_perm_bits();
1418 /* Register and scan for devices */
1419 ret
= pci_register_driver(&vfio_pci_driver
);
1423 vfio_pci_fill_ids();
1428 vfio_pci_uninit_perm_bits();
1432 module_init(vfio_pci_init
);
1433 module_exit(vfio_pci_cleanup
);
1435 MODULE_VERSION(DRIVER_VERSION
);
1436 MODULE_LICENSE("GPL v2");
1437 MODULE_AUTHOR(DRIVER_AUTHOR
);
1438 MODULE_DESCRIPTION(DRIVER_DESC
);