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 #include <linux/device.h>
15 #include <linux/eventfd.h>
16 #include <linux/file.h>
17 #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>
29 #include "vfio_pci_private.h"
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"
35 static bool nointxmask
;
36 module_param_named(nointxmask
, nointxmask
, bool, S_IRUGO
| S_IWUSR
);
37 MODULE_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.");
40 static DEFINE_MUTEX(driver_lock
);
42 static void vfio_pci_try_bus_reset(struct vfio_pci_device
*vdev
);
44 static int vfio_pci_enable(struct vfio_pci_device
*vdev
)
46 struct pci_dev
*pdev
= vdev
->pdev
;
51 /* Don't allow our initial saved state to include busmaster */
52 pci_clear_master(pdev
);
54 ret
= pci_enable_device(pdev
);
58 vdev
->reset_works
= (pci_reset_function(pdev
) == 0);
60 vdev
->pci_saved_state
= pci_store_saved_state(pdev
);
61 if (!vdev
->pci_saved_state
)
62 pr_debug("%s: Couldn't store %s saved state\n",
63 __func__
, dev_name(&pdev
->dev
));
65 ret
= vfio_config_init(vdev
);
67 kfree(vdev
->pci_saved_state
);
68 vdev
->pci_saved_state
= NULL
;
69 pci_disable_device(pdev
);
73 if (likely(!nointxmask
))
74 vdev
->pci_2_3
= pci_intx_mask_supported(pdev
);
76 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
77 if (vdev
->pci_2_3
&& (cmd
& PCI_COMMAND_INTX_DISABLE
)) {
78 cmd
&= ~PCI_COMMAND_INTX_DISABLE
;
79 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
);
82 msix_pos
= pdev
->msix_cap
;
87 pci_read_config_word(pdev
, msix_pos
+ PCI_MSIX_FLAGS
, &flags
);
88 pci_read_config_dword(pdev
, msix_pos
+ PCI_MSIX_TABLE
, &table
);
90 vdev
->msix_bar
= table
& PCI_MSIX_TABLE_BIR
;
91 vdev
->msix_offset
= table
& PCI_MSIX_TABLE_OFFSET
;
92 vdev
->msix_size
= ((flags
& PCI_MSIX_FLAGS_QSIZE
) + 1) * 16;
94 vdev
->msix_bar
= 0xFF;
96 #ifdef CONFIG_VFIO_PCI_VGA
97 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
)
104 static void vfio_pci_disable(struct vfio_pci_device
*vdev
)
106 struct pci_dev
*pdev
= vdev
->pdev
;
109 /* Stop the device from further DMA */
110 pci_clear_master(pdev
);
112 vfio_pci_set_irqs_ioctl(vdev
, VFIO_IRQ_SET_DATA_NONE
|
113 VFIO_IRQ_SET_ACTION_TRIGGER
,
114 vdev
->irq_type
, 0, 0, NULL
);
116 vdev
->virq_disabled
= false;
118 vfio_config_free(vdev
);
120 for (bar
= PCI_STD_RESOURCES
; bar
<= PCI_STD_RESOURCE_END
; bar
++) {
121 if (!vdev
->barmap
[bar
])
123 pci_iounmap(pdev
, vdev
->barmap
[bar
]);
124 pci_release_selected_regions(pdev
, 1 << bar
);
125 vdev
->barmap
[bar
] = NULL
;
128 vdev
->needs_reset
= true;
131 * If we have saved state, restore it. If we can reset the device,
132 * even better. Resetting with current state seems better than
133 * nothing, but saving and restoring current state without reset
136 if (pci_load_and_free_saved_state(pdev
, &vdev
->pci_saved_state
)) {
137 pr_info("%s: Couldn't reload %s saved state\n",
138 __func__
, dev_name(&pdev
->dev
));
140 if (!vdev
->reset_works
)
143 pci_save_state(pdev
);
147 * Disable INTx and MSI, presumably to avoid spurious interrupts
148 * during reset. Stolen from pci_reset_function()
150 pci_write_config_word(pdev
, PCI_COMMAND
, PCI_COMMAND_INTX_DISABLE
);
153 * Try to reset the device. The success of this is dependent on
154 * being able to lock the device, which is not always possible.
156 if (vdev
->reset_works
) {
157 int ret
= pci_try_reset_function(pdev
);
159 pr_warn("%s: Failed to reset device %s (%d)\n",
160 __func__
, dev_name(&pdev
->dev
), ret
);
162 vdev
->needs_reset
= false;
165 pci_restore_state(pdev
);
167 pci_disable_device(pdev
);
169 vfio_pci_try_bus_reset(vdev
);
172 static void vfio_pci_release(void *device_data
)
174 struct vfio_pci_device
*vdev
= device_data
;
176 mutex_lock(&driver_lock
);
178 if (!(--vdev
->refcnt
)) {
179 vfio_spapr_pci_eeh_release(vdev
->pdev
);
180 vfio_pci_disable(vdev
);
183 mutex_unlock(&driver_lock
);
185 module_put(THIS_MODULE
);
188 static int vfio_pci_open(void *device_data
)
190 struct vfio_pci_device
*vdev
= device_data
;
193 if (!try_module_get(THIS_MODULE
))
196 mutex_lock(&driver_lock
);
199 ret
= vfio_pci_enable(vdev
);
203 vfio_spapr_pci_eeh_open(vdev
->pdev
);
207 mutex_unlock(&driver_lock
);
209 module_put(THIS_MODULE
);
213 static int vfio_pci_get_irq_count(struct vfio_pci_device
*vdev
, int irq_type
)
215 if (irq_type
== VFIO_PCI_INTX_IRQ_INDEX
) {
217 pci_read_config_byte(vdev
->pdev
, PCI_INTERRUPT_PIN
, &pin
);
221 } else if (irq_type
== VFIO_PCI_MSI_IRQ_INDEX
) {
225 pos
= vdev
->pdev
->msi_cap
;
227 pci_read_config_word(vdev
->pdev
,
228 pos
+ PCI_MSI_FLAGS
, &flags
);
229 return 1 << ((flags
& PCI_MSI_FLAGS_QMASK
) >> 1);
231 } else if (irq_type
== VFIO_PCI_MSIX_IRQ_INDEX
) {
235 pos
= vdev
->pdev
->msix_cap
;
237 pci_read_config_word(vdev
->pdev
,
238 pos
+ PCI_MSIX_FLAGS
, &flags
);
240 return (flags
& PCI_MSIX_FLAGS_QSIZE
) + 1;
242 } else if (irq_type
== VFIO_PCI_ERR_IRQ_INDEX
)
243 if (pci_is_pcie(vdev
->pdev
))
249 static int vfio_pci_count_devs(struct pci_dev
*pdev
, void *data
)
255 struct vfio_pci_fill_info
{
258 struct vfio_pci_dependent_device
*devices
;
261 static int vfio_pci_fill_devs(struct pci_dev
*pdev
, void *data
)
263 struct vfio_pci_fill_info
*fill
= data
;
264 struct iommu_group
*iommu_group
;
266 if (fill
->cur
== fill
->max
)
267 return -EAGAIN
; /* Something changed, try again */
269 iommu_group
= iommu_group_get(&pdev
->dev
);
271 return -EPERM
; /* Cannot reset non-isolated devices */
273 fill
->devices
[fill
->cur
].group_id
= iommu_group_id(iommu_group
);
274 fill
->devices
[fill
->cur
].segment
= pci_domain_nr(pdev
->bus
);
275 fill
->devices
[fill
->cur
].bus
= pdev
->bus
->number
;
276 fill
->devices
[fill
->cur
].devfn
= pdev
->devfn
;
278 iommu_group_put(iommu_group
);
282 struct vfio_pci_group_entry
{
283 struct vfio_group
*group
;
287 struct vfio_pci_group_info
{
289 struct vfio_pci_group_entry
*groups
;
292 static int vfio_pci_validate_devs(struct pci_dev
*pdev
, void *data
)
294 struct vfio_pci_group_info
*info
= data
;
295 struct iommu_group
*group
;
298 group
= iommu_group_get(&pdev
->dev
);
302 id
= iommu_group_id(group
);
304 for (i
= 0; i
< info
->count
; i
++)
305 if (info
->groups
[i
].id
== id
)
308 iommu_group_put(group
);
310 return (i
== info
->count
) ? -EINVAL
: 0;
313 static bool vfio_pci_dev_below_slot(struct pci_dev
*pdev
, struct pci_slot
*slot
)
315 for (; pdev
; pdev
= pdev
->bus
->self
)
316 if (pdev
->bus
== slot
->bus
)
317 return (pdev
->slot
== slot
);
321 struct vfio_pci_walk_info
{
322 int (*fn
)(struct pci_dev
*, void *data
);
324 struct pci_dev
*pdev
;
329 static int vfio_pci_walk_wrapper(struct pci_dev
*pdev
, void *data
)
331 struct vfio_pci_walk_info
*walk
= data
;
333 if (!walk
->slot
|| vfio_pci_dev_below_slot(pdev
, walk
->pdev
->slot
))
334 walk
->ret
= walk
->fn(pdev
, walk
->data
);
339 static int vfio_pci_for_each_slot_or_bus(struct pci_dev
*pdev
,
340 int (*fn
)(struct pci_dev
*,
341 void *data
), void *data
,
344 struct vfio_pci_walk_info walk
= {
345 .fn
= fn
, .data
= data
, .pdev
= pdev
, .slot
= slot
, .ret
= 0,
348 pci_walk_bus(pdev
->bus
, vfio_pci_walk_wrapper
, &walk
);
353 static long vfio_pci_ioctl(void *device_data
,
354 unsigned int cmd
, unsigned long arg
)
356 struct vfio_pci_device
*vdev
= device_data
;
359 if (cmd
== VFIO_DEVICE_GET_INFO
) {
360 struct vfio_device_info info
;
362 minsz
= offsetofend(struct vfio_device_info
, num_irqs
);
364 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
367 if (info
.argsz
< minsz
)
370 info
.flags
= VFIO_DEVICE_FLAGS_PCI
;
372 if (vdev
->reset_works
)
373 info
.flags
|= VFIO_DEVICE_FLAGS_RESET
;
375 info
.num_regions
= VFIO_PCI_NUM_REGIONS
;
376 info
.num_irqs
= VFIO_PCI_NUM_IRQS
;
378 return copy_to_user((void __user
*)arg
, &info
, minsz
);
380 } else if (cmd
== VFIO_DEVICE_GET_REGION_INFO
) {
381 struct pci_dev
*pdev
= vdev
->pdev
;
382 struct vfio_region_info info
;
384 minsz
= offsetofend(struct vfio_region_info
, offset
);
386 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
389 if (info
.argsz
< minsz
)
392 switch (info
.index
) {
393 case VFIO_PCI_CONFIG_REGION_INDEX
:
394 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
395 info
.size
= pdev
->cfg_size
;
396 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
397 VFIO_REGION_INFO_FLAG_WRITE
;
399 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
400 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
401 info
.size
= pci_resource_len(pdev
, info
.index
);
407 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
408 VFIO_REGION_INFO_FLAG_WRITE
;
409 if (pci_resource_flags(pdev
, info
.index
) &
410 IORESOURCE_MEM
&& info
.size
>= PAGE_SIZE
)
411 info
.flags
|= VFIO_REGION_INFO_FLAG_MMAP
;
413 case VFIO_PCI_ROM_REGION_INDEX
:
418 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
421 /* Report the BAR size, not the ROM size */
422 info
.size
= pci_resource_len(pdev
, info
.index
);
426 /* Is it really there? */
427 io
= pci_map_rom(pdev
, &size
);
432 pci_unmap_rom(pdev
, io
);
434 info
.flags
= VFIO_REGION_INFO_FLAG_READ
;
437 case VFIO_PCI_VGA_REGION_INDEX
:
441 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
443 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
444 VFIO_REGION_INFO_FLAG_WRITE
;
451 return copy_to_user((void __user
*)arg
, &info
, minsz
);
453 } else if (cmd
== VFIO_DEVICE_GET_IRQ_INFO
) {
454 struct vfio_irq_info info
;
456 minsz
= offsetofend(struct vfio_irq_info
, count
);
458 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
461 if (info
.argsz
< minsz
|| info
.index
>= VFIO_PCI_NUM_IRQS
)
464 switch (info
.index
) {
465 case VFIO_PCI_INTX_IRQ_INDEX
... VFIO_PCI_MSIX_IRQ_INDEX
:
467 case VFIO_PCI_ERR_IRQ_INDEX
:
468 if (pci_is_pcie(vdev
->pdev
))
470 /* pass thru to return error */
475 info
.flags
= VFIO_IRQ_INFO_EVENTFD
;
477 info
.count
= vfio_pci_get_irq_count(vdev
, info
.index
);
479 if (info
.index
== VFIO_PCI_INTX_IRQ_INDEX
)
480 info
.flags
|= (VFIO_IRQ_INFO_MASKABLE
|
481 VFIO_IRQ_INFO_AUTOMASKED
);
483 info
.flags
|= VFIO_IRQ_INFO_NORESIZE
;
485 return copy_to_user((void __user
*)arg
, &info
, minsz
);
487 } else if (cmd
== VFIO_DEVICE_SET_IRQS
) {
488 struct vfio_irq_set hdr
;
492 minsz
= offsetofend(struct vfio_irq_set
, count
);
494 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
497 if (hdr
.argsz
< minsz
|| hdr
.index
>= VFIO_PCI_NUM_IRQS
||
498 hdr
.flags
& ~(VFIO_IRQ_SET_DATA_TYPE_MASK
|
499 VFIO_IRQ_SET_ACTION_TYPE_MASK
))
502 if (!(hdr
.flags
& VFIO_IRQ_SET_DATA_NONE
)) {
504 int max
= vfio_pci_get_irq_count(vdev
, hdr
.index
);
506 if (hdr
.flags
& VFIO_IRQ_SET_DATA_BOOL
)
507 size
= sizeof(uint8_t);
508 else if (hdr
.flags
& VFIO_IRQ_SET_DATA_EVENTFD
)
509 size
= sizeof(int32_t);
513 if (hdr
.argsz
- minsz
< hdr
.count
* size
||
514 hdr
.start
>= max
|| hdr
.start
+ hdr
.count
> max
)
517 data
= memdup_user((void __user
*)(arg
+ minsz
),
520 return PTR_ERR(data
);
523 mutex_lock(&vdev
->igate
);
525 ret
= vfio_pci_set_irqs_ioctl(vdev
, hdr
.flags
, hdr
.index
,
526 hdr
.start
, hdr
.count
, data
);
528 mutex_unlock(&vdev
->igate
);
533 } else if (cmd
== VFIO_DEVICE_RESET
) {
534 return vdev
->reset_works
?
535 pci_try_reset_function(vdev
->pdev
) : -EINVAL
;
537 } else if (cmd
== VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
) {
538 struct vfio_pci_hot_reset_info hdr
;
539 struct vfio_pci_fill_info fill
= { 0 };
540 struct vfio_pci_dependent_device
*devices
= NULL
;
544 minsz
= offsetofend(struct vfio_pci_hot_reset_info
, count
);
546 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
549 if (hdr
.argsz
< minsz
)
554 /* Can we do a slot or bus reset or neither? */
555 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
557 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
560 /* How many devices are affected? */
561 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
567 WARN_ON(!fill
.max
); /* Should always be at least one */
570 * If there's enough space, fill it now, otherwise return
571 * -ENOSPC and the number of devices affected.
573 if (hdr
.argsz
< sizeof(hdr
) + (fill
.max
* sizeof(*devices
))) {
575 hdr
.count
= fill
.max
;
576 goto reset_info_exit
;
579 devices
= kcalloc(fill
.max
, sizeof(*devices
), GFP_KERNEL
);
583 fill
.devices
= devices
;
585 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
590 * If a device was removed between counting and filling,
591 * we may come up short of fill.max. If a device was
592 * added, we'll have a return of -EAGAIN above.
595 hdr
.count
= fill
.cur
;
598 if (copy_to_user((void __user
*)arg
, &hdr
, minsz
))
602 if (copy_to_user((void __user
*)(arg
+ minsz
), devices
,
603 hdr
.count
* sizeof(*devices
)))
610 } else if (cmd
== VFIO_DEVICE_PCI_HOT_RESET
) {
611 struct vfio_pci_hot_reset hdr
;
613 struct vfio_pci_group_entry
*groups
;
614 struct vfio_pci_group_info info
;
616 int i
, count
= 0, ret
= 0;
618 minsz
= offsetofend(struct vfio_pci_hot_reset
, count
);
620 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
623 if (hdr
.argsz
< minsz
|| hdr
.flags
)
626 /* Can we do a slot or bus reset or neither? */
627 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
629 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
633 * We can't let userspace give us an arbitrarily large
634 * buffer to copy, so verify how many we think there
635 * could be. Note groups can have multiple devices so
636 * one group per device is the max.
638 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
644 /* Somewhere between 1 and count is OK */
645 if (!hdr
.count
|| hdr
.count
> count
)
648 group_fds
= kcalloc(hdr
.count
, sizeof(*group_fds
), GFP_KERNEL
);
649 groups
= kcalloc(hdr
.count
, sizeof(*groups
), GFP_KERNEL
);
650 if (!group_fds
|| !groups
) {
656 if (copy_from_user(group_fds
, (void __user
*)(arg
+ minsz
),
657 hdr
.count
* sizeof(*group_fds
))) {
664 * For each group_fd, get the group through the vfio external
665 * user interface and store the group and iommu ID. This
666 * ensures the group is held across the reset.
668 for (i
= 0; i
< hdr
.count
; i
++) {
669 struct vfio_group
*group
;
670 struct fd f
= fdget(group_fds
[i
]);
676 group
= vfio_group_get_external_user(f
.file
);
679 ret
= PTR_ERR(group
);
683 groups
[i
].group
= group
;
684 groups
[i
].id
= vfio_external_user_iommu_id(group
);
689 /* release reference to groups on error */
691 goto hot_reset_release
;
693 info
.count
= hdr
.count
;
694 info
.groups
= groups
;
697 * Test whether all the affected devices are contained
698 * by the set of groups provided by the user.
700 ret
= vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
701 vfio_pci_validate_devs
,
704 /* User has access, do the reset */
705 ret
= slot
? pci_try_reset_slot(vdev
->pdev
->slot
) :
706 pci_try_reset_bus(vdev
->pdev
->bus
);
709 for (i
--; i
>= 0; i
--)
710 vfio_group_put_external_user(groups
[i
].group
);
719 static ssize_t
vfio_pci_rw(void *device_data
, char __user
*buf
,
720 size_t count
, loff_t
*ppos
, bool iswrite
)
722 unsigned int index
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
);
723 struct vfio_pci_device
*vdev
= device_data
;
725 if (index
>= VFIO_PCI_NUM_REGIONS
)
729 case VFIO_PCI_CONFIG_REGION_INDEX
:
730 return vfio_pci_config_rw(vdev
, buf
, count
, ppos
, iswrite
);
732 case VFIO_PCI_ROM_REGION_INDEX
:
735 return vfio_pci_bar_rw(vdev
, buf
, count
, ppos
, false);
737 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
738 return vfio_pci_bar_rw(vdev
, buf
, count
, ppos
, iswrite
);
740 case VFIO_PCI_VGA_REGION_INDEX
:
741 return vfio_pci_vga_rw(vdev
, buf
, count
, ppos
, iswrite
);
747 static ssize_t
vfio_pci_read(void *device_data
, char __user
*buf
,
748 size_t count
, loff_t
*ppos
)
753 return vfio_pci_rw(device_data
, buf
, count
, ppos
, false);
756 static ssize_t
vfio_pci_write(void *device_data
, const char __user
*buf
,
757 size_t count
, loff_t
*ppos
)
762 return vfio_pci_rw(device_data
, (char __user
*)buf
, count
, ppos
, true);
765 static int vfio_pci_mmap(void *device_data
, struct vm_area_struct
*vma
)
767 struct vfio_pci_device
*vdev
= device_data
;
768 struct pci_dev
*pdev
= vdev
->pdev
;
770 u64 phys_len
, req_len
, pgoff
, req_start
;
773 index
= vma
->vm_pgoff
>> (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
);
775 if (vma
->vm_end
< vma
->vm_start
)
777 if ((vma
->vm_flags
& VM_SHARED
) == 0)
779 if (index
>= VFIO_PCI_ROM_REGION_INDEX
)
781 if (!(pci_resource_flags(pdev
, index
) & IORESOURCE_MEM
))
784 phys_len
= pci_resource_len(pdev
, index
);
785 req_len
= vma
->vm_end
- vma
->vm_start
;
786 pgoff
= vma
->vm_pgoff
&
787 ((1U << (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
)) - 1);
788 req_start
= pgoff
<< PAGE_SHIFT
;
790 if (phys_len
< PAGE_SIZE
|| req_start
+ req_len
> phys_len
)
793 if (index
== vdev
->msix_bar
) {
795 * Disallow mmaps overlapping the MSI-X table; users don't
796 * get to touch this directly. We could find somewhere
797 * else to map the overlap, but page granularity is only
798 * a recommendation, not a requirement, so the user needs
799 * to know which bits are real. Requiring them to mmap
800 * around the table makes that clear.
803 /* If neither entirely above nor below, then it overlaps */
804 if (!(req_start
>= vdev
->msix_offset
+ vdev
->msix_size
||
805 req_start
+ req_len
<= vdev
->msix_offset
))
810 * Even though we don't make use of the barmap for the mmap,
811 * we need to request the region and the barmap tracks that.
813 if (!vdev
->barmap
[index
]) {
814 ret
= pci_request_selected_regions(pdev
,
815 1 << index
, "vfio-pci");
819 vdev
->barmap
[index
] = pci_iomap(pdev
, index
, 0);
822 vma
->vm_private_data
= vdev
;
823 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
824 vma
->vm_pgoff
= (pci_resource_start(pdev
, index
) >> PAGE_SHIFT
) + pgoff
;
826 return remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
827 req_len
, vma
->vm_page_prot
);
830 static const struct vfio_device_ops vfio_pci_ops
= {
832 .open
= vfio_pci_open
,
833 .release
= vfio_pci_release
,
834 .ioctl
= vfio_pci_ioctl
,
835 .read
= vfio_pci_read
,
836 .write
= vfio_pci_write
,
837 .mmap
= vfio_pci_mmap
,
840 static int vfio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
843 struct vfio_pci_device
*vdev
;
844 struct iommu_group
*group
;
847 pci_read_config_byte(pdev
, PCI_HEADER_TYPE
, &type
);
848 if ((type
& PCI_HEADER_TYPE
) != PCI_HEADER_TYPE_NORMAL
)
851 group
= iommu_group_get(&pdev
->dev
);
855 vdev
= kzalloc(sizeof(*vdev
), GFP_KERNEL
);
857 iommu_group_put(group
);
862 vdev
->irq_type
= VFIO_PCI_NUM_IRQS
;
863 mutex_init(&vdev
->igate
);
864 spin_lock_init(&vdev
->irqlock
);
866 ret
= vfio_add_group_dev(&pdev
->dev
, &vfio_pci_ops
, vdev
);
868 iommu_group_put(group
);
875 static void vfio_pci_remove(struct pci_dev
*pdev
)
877 struct vfio_pci_device
*vdev
;
879 mutex_lock(&driver_lock
);
881 vdev
= vfio_del_group_dev(&pdev
->dev
);
883 iommu_group_put(pdev
->dev
.iommu_group
);
887 mutex_unlock(&driver_lock
);
890 static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev
*pdev
,
891 pci_channel_state_t state
)
893 struct vfio_pci_device
*vdev
;
894 struct vfio_device
*device
;
896 device
= vfio_device_get_from_dev(&pdev
->dev
);
898 return PCI_ERS_RESULT_DISCONNECT
;
900 vdev
= vfio_device_data(device
);
902 vfio_device_put(device
);
903 return PCI_ERS_RESULT_DISCONNECT
;
906 mutex_lock(&vdev
->igate
);
908 if (vdev
->err_trigger
)
909 eventfd_signal(vdev
->err_trigger
, 1);
911 mutex_unlock(&vdev
->igate
);
913 vfio_device_put(device
);
915 return PCI_ERS_RESULT_CAN_RECOVER
;
918 static struct pci_error_handlers vfio_err_handlers
= {
919 .error_detected
= vfio_pci_aer_err_detected
,
922 static struct pci_driver vfio_pci_driver
= {
924 .id_table
= NULL
, /* only dynamic ids */
925 .probe
= vfio_pci_probe
,
926 .remove
= vfio_pci_remove
,
927 .err_handler
= &vfio_err_handlers
,
931 * Test whether a reset is necessary and possible. We mark devices as
932 * needs_reset when they are released, but don't have a function-local reset
933 * available. If any of these exist in the affected devices, we want to do
934 * a bus/slot reset. We also need all of the affected devices to be unused,
935 * so we abort if any device has a non-zero refcnt. driver_lock prevents a
936 * device from being opened during the scan or unbound from vfio-pci.
938 static int vfio_pci_test_bus_reset(struct pci_dev
*pdev
, void *data
)
940 bool *needs_reset
= data
;
941 struct pci_driver
*pci_drv
= ACCESS_ONCE(pdev
->driver
);
944 if (pci_drv
== &vfio_pci_driver
) {
945 struct vfio_device
*device
;
946 struct vfio_pci_device
*vdev
;
948 device
= vfio_device_get_from_dev(&pdev
->dev
);
952 vdev
= vfio_device_data(device
);
954 if (vdev
->needs_reset
)
961 vfio_device_put(device
);
965 * TODO: vfio-core considers groups to be viable even if some devices
966 * are attached to known drivers, like pci-stub or pcieport. We can't
967 * freeze devices from being unbound to those drivers like we can
968 * here though, so it would be racy to test for them. We also can't
969 * use device_lock() to prevent changes as that would interfere with
970 * PCI-core taking device_lock during bus reset. For now, we require
971 * devices to be bound to vfio-pci to get a bus/slot reset on release.
977 /* Clear needs_reset on all affected devices after successful bus/slot reset */
978 static int vfio_pci_clear_needs_reset(struct pci_dev
*pdev
, void *data
)
980 struct pci_driver
*pci_drv
= ACCESS_ONCE(pdev
->driver
);
982 if (pci_drv
== &vfio_pci_driver
) {
983 struct vfio_device
*device
;
984 struct vfio_pci_device
*vdev
;
986 device
= vfio_device_get_from_dev(&pdev
->dev
);
990 vdev
= vfio_device_data(device
);
992 vdev
->needs_reset
= false;
994 vfio_device_put(device
);
1001 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1002 * this device that are needs_reset and all of the affected devices are unused
1003 * (!refcnt). Callers of this function are required to hold driver_lock such
1004 * that devices can not be unbound from vfio-pci or opened by a user while we
1005 * test for and perform a bus/slot reset.
1007 static void vfio_pci_try_bus_reset(struct vfio_pci_device
*vdev
)
1009 bool needs_reset
= false, slot
= false;
1012 if (!pci_probe_reset_slot(vdev
->pdev
->slot
))
1014 else if (pci_probe_reset_bus(vdev
->pdev
->bus
))
1017 if (vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
1018 vfio_pci_test_bus_reset
,
1019 &needs_reset
, slot
) || !needs_reset
)
1023 ret
= pci_try_reset_slot(vdev
->pdev
->slot
);
1025 ret
= pci_try_reset_bus(vdev
->pdev
->bus
);
1030 vfio_pci_for_each_slot_or_bus(vdev
->pdev
,
1031 vfio_pci_clear_needs_reset
, NULL
, slot
);
1034 static void __exit
vfio_pci_cleanup(void)
1036 pci_unregister_driver(&vfio_pci_driver
);
1037 vfio_pci_virqfd_exit();
1038 vfio_pci_uninit_perm_bits();
1041 static int __init
vfio_pci_init(void)
1045 /* Allocate shared config space permision data used by all devices */
1046 ret
= vfio_pci_init_perm_bits();
1050 /* Start the virqfd cleanup handler */
1051 ret
= vfio_pci_virqfd_init();
1055 /* Register and scan for devices */
1056 ret
= pci_register_driver(&vfio_pci_driver
);
1063 vfio_pci_virqfd_exit();
1065 vfio_pci_uninit_perm_bits();
1069 module_init(vfio_pci_init
);
1070 module_exit(vfio_pci_cleanup
);
1072 MODULE_VERSION(DRIVER_VERSION
);
1073 MODULE_LICENSE("GPL v2");
1074 MODULE_AUTHOR(DRIVER_AUTHOR
);
1075 MODULE_DESCRIPTION(DRIVER_DESC
);