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);
117 * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
118 * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
119 * If a device implements the former but not the latter we would typically
120 * expect broken_intx_masking be set and require an exclusive interrupt.
121 * However since we do have control of the device's ability to assert INTx,
122 * we can instead pretend that the device does not implement INTx, virtualizing
123 * the pin register to report zero and maintaining DisINTx set on the host.
125 static bool vfio_pci_nointx(struct pci_dev *pdev)
127 switch (pdev->vendor) {
128 case PCI_VENDOR_ID_INTEL:
129 switch (pdev->device) {
130 /* All i40e (XL710/X710) 10/20/40GbE NICs */
133 case 0x1580 ... 0x1581:
134 case 0x1583 ... 0x1589:
135 case 0x37d0 ... 0x37d2:
145 static int vfio_pci_enable(struct vfio_pci_device *vdev)
147 struct pci_dev *pdev = vdev->pdev;
152 pci_set_power_state(pdev, PCI_D0);
154 /* Don't allow our initial saved state to include busmaster */
155 pci_clear_master(pdev);
157 ret = pci_enable_device(pdev);
161 vdev->reset_works = (pci_reset_function(pdev) == 0);
162 pci_save_state(pdev);
163 vdev->pci_saved_state = pci_store_saved_state(pdev);
164 if (!vdev->pci_saved_state)
165 pr_debug("%s: Couldn't store %s saved state\n",
166 __func__, dev_name(&pdev->dev));
168 if (likely(!nointxmask)) {
169 if (vfio_pci_nointx(pdev)) {
170 dev_info(&pdev->dev, "Masking broken INTx support\n");
174 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
177 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
178 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
179 cmd &= ~PCI_COMMAND_INTX_DISABLE;
180 pci_write_config_word(pdev, PCI_COMMAND, cmd);
183 ret = vfio_config_init(vdev);
185 kfree(vdev->pci_saved_state);
186 vdev->pci_saved_state = NULL;
187 pci_disable_device(pdev);
191 msix_pos = pdev->msix_cap;
196 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
197 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
199 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
200 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
201 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
203 vdev->msix_bar = 0xFF;
205 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
206 vdev->has_vga = true;
209 if (vfio_pci_is_vga(pdev) &&
210 pdev->vendor == PCI_VENDOR_ID_INTEL &&
211 IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
212 ret = vfio_pci_igd_init(vdev);
214 dev_warn(&vdev->pdev->dev,
215 "Failed to setup Intel IGD regions\n");
216 vfio_pci_disable(vdev);
224 static void vfio_pci_disable(struct vfio_pci_device *vdev)
226 struct pci_dev *pdev = vdev->pdev;
229 /* Stop the device from further DMA */
230 pci_clear_master(pdev);
232 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
233 VFIO_IRQ_SET_ACTION_TRIGGER,
234 vdev->irq_type, 0, 0, NULL);
236 vdev->virq_disabled = false;
238 for (i = 0; i < vdev->num_regions; i++)
239 vdev->region[i].ops->release(vdev, &vdev->region[i]);
241 vdev->num_regions = 0;
243 vdev->region = NULL; /* don't krealloc a freed pointer */
245 vfio_config_free(vdev);
247 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
248 if (!vdev->barmap[bar])
250 pci_iounmap(pdev, vdev->barmap[bar]);
251 pci_release_selected_regions(pdev, 1 << bar);
252 vdev->barmap[bar] = NULL;
255 vdev->needs_reset = true;
258 * If we have saved state, restore it. If we can reset the device,
259 * even better. Resetting with current state seems better than
260 * nothing, but saving and restoring current state without reset
263 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
264 pr_info("%s: Couldn't reload %s saved state\n",
265 __func__, dev_name(&pdev->dev));
267 if (!vdev->reset_works)
270 pci_save_state(pdev);
274 * Disable INTx and MSI, presumably to avoid spurious interrupts
275 * during reset. Stolen from pci_reset_function()
277 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
280 * Try to reset the device. The success of this is dependent on
281 * being able to lock the device, which is not always possible.
283 if (vdev->reset_works && !pci_try_reset_function(pdev))
284 vdev->needs_reset = false;
286 pci_restore_state(pdev);
288 pci_disable_device(pdev);
290 vfio_pci_try_bus_reset(vdev);
292 if (!disable_idle_d3)
293 pci_set_power_state(pdev, PCI_D3hot);
296 static void vfio_pci_release(void *device_data)
298 struct vfio_pci_device *vdev = device_data;
300 mutex_lock(&driver_lock);
302 if (!(--vdev->refcnt)) {
303 vfio_spapr_pci_eeh_release(vdev->pdev);
304 vfio_pci_disable(vdev);
307 mutex_unlock(&driver_lock);
309 module_put(THIS_MODULE);
312 static int vfio_pci_open(void *device_data)
314 struct vfio_pci_device *vdev = device_data;
317 if (!try_module_get(THIS_MODULE))
320 mutex_lock(&driver_lock);
323 ret = vfio_pci_enable(vdev);
327 vfio_spapr_pci_eeh_open(vdev->pdev);
331 mutex_unlock(&driver_lock);
333 module_put(THIS_MODULE);
337 static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
339 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
341 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
342 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && !vdev->nointx && pin)
345 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
349 pos = vdev->pdev->msi_cap;
351 pci_read_config_word(vdev->pdev,
352 pos + PCI_MSI_FLAGS, &flags);
353 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
355 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
359 pos = vdev->pdev->msix_cap;
361 pci_read_config_word(vdev->pdev,
362 pos + PCI_MSIX_FLAGS, &flags);
364 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
366 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
367 if (pci_is_pcie(vdev->pdev))
369 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
376 static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
382 struct vfio_pci_fill_info {
385 struct vfio_pci_dependent_device *devices;
388 static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
390 struct vfio_pci_fill_info *fill = data;
391 struct iommu_group *iommu_group;
393 if (fill->cur == fill->max)
394 return -EAGAIN; /* Something changed, try again */
396 iommu_group = iommu_group_get(&pdev->dev);
398 return -EPERM; /* Cannot reset non-isolated devices */
400 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
401 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
402 fill->devices[fill->cur].bus = pdev->bus->number;
403 fill->devices[fill->cur].devfn = pdev->devfn;
405 iommu_group_put(iommu_group);
409 struct vfio_pci_group_entry {
410 struct vfio_group *group;
414 struct vfio_pci_group_info {
416 struct vfio_pci_group_entry *groups;
419 static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
421 struct vfio_pci_group_info *info = data;
422 struct iommu_group *group;
425 group = iommu_group_get(&pdev->dev);
429 id = iommu_group_id(group);
431 for (i = 0; i < info->count; i++)
432 if (info->groups[i].id == id)
435 iommu_group_put(group);
437 return (i == info->count) ? -EINVAL : 0;
440 static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
442 for (; pdev; pdev = pdev->bus->self)
443 if (pdev->bus == slot->bus)
444 return (pdev->slot == slot);
448 struct vfio_pci_walk_info {
449 int (*fn)(struct pci_dev *, void *data);
451 struct pci_dev *pdev;
456 static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
458 struct vfio_pci_walk_info *walk = data;
460 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
461 walk->ret = walk->fn(pdev, walk->data);
466 static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
467 int (*fn)(struct pci_dev *,
468 void *data), void *data,
471 struct vfio_pci_walk_info walk = {
472 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
475 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
480 static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
481 struct vfio_info_cap *caps)
483 struct vfio_info_cap_header *header;
484 struct vfio_region_info_cap_sparse_mmap *sparse;
486 int nr_areas = 2, i = 0;
488 end = pci_resource_len(vdev->pdev, vdev->msix_bar);
490 /* If MSI-X table is aligned to the start or end, only one area */
491 if (((vdev->msix_offset & PAGE_MASK) == 0) ||
492 (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
495 size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
497 header = vfio_info_cap_add(caps, size,
498 VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
500 return PTR_ERR(header);
502 sparse = container_of(header,
503 struct vfio_region_info_cap_sparse_mmap, header);
504 sparse->nr_areas = nr_areas;
506 if (vdev->msix_offset & PAGE_MASK) {
507 sparse->areas[i].offset = 0;
508 sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
512 if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
513 sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
515 sparse->areas[i].size = end - sparse->areas[i].offset;
522 static int region_type_cap(struct vfio_pci_device *vdev,
523 struct vfio_info_cap *caps,
524 unsigned int type, unsigned int subtype)
526 struct vfio_info_cap_header *header;
527 struct vfio_region_info_cap_type *cap;
529 header = vfio_info_cap_add(caps, sizeof(*cap),
530 VFIO_REGION_INFO_CAP_TYPE, 1);
532 return PTR_ERR(header);
534 cap = container_of(header, struct vfio_region_info_cap_type, header);
536 cap->subtype = subtype;
541 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
542 unsigned int type, unsigned int subtype,
543 const struct vfio_pci_regops *ops,
544 size_t size, u32 flags, void *data)
546 struct vfio_pci_region *region;
548 region = krealloc(vdev->region,
549 (vdev->num_regions + 1) * sizeof(*region),
554 vdev->region = region;
555 vdev->region[vdev->num_regions].type = type;
556 vdev->region[vdev->num_regions].subtype = subtype;
557 vdev->region[vdev->num_regions].ops = ops;
558 vdev->region[vdev->num_regions].size = size;
559 vdev->region[vdev->num_regions].flags = flags;
560 vdev->region[vdev->num_regions].data = data;
567 static long vfio_pci_ioctl(void *device_data,
568 unsigned int cmd, unsigned long arg)
570 struct vfio_pci_device *vdev = device_data;
573 if (cmd == VFIO_DEVICE_GET_INFO) {
574 struct vfio_device_info info;
576 minsz = offsetofend(struct vfio_device_info, num_irqs);
578 if (copy_from_user(&info, (void __user *)arg, minsz))
581 if (info.argsz < minsz)
584 info.flags = VFIO_DEVICE_FLAGS_PCI;
586 if (vdev->reset_works)
587 info.flags |= VFIO_DEVICE_FLAGS_RESET;
589 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
590 info.num_irqs = VFIO_PCI_NUM_IRQS;
592 return copy_to_user((void __user *)arg, &info, minsz) ?
595 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
596 struct pci_dev *pdev = vdev->pdev;
597 struct vfio_region_info info;
598 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
601 minsz = offsetofend(struct vfio_region_info, offset);
603 if (copy_from_user(&info, (void __user *)arg, minsz))
606 if (info.argsz < minsz)
609 switch (info.index) {
610 case VFIO_PCI_CONFIG_REGION_INDEX:
611 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
612 info.size = pdev->cfg_size;
613 info.flags = VFIO_REGION_INFO_FLAG_READ |
614 VFIO_REGION_INFO_FLAG_WRITE;
616 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
617 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
618 info.size = pci_resource_len(pdev, info.index);
624 info.flags = VFIO_REGION_INFO_FLAG_READ |
625 VFIO_REGION_INFO_FLAG_WRITE;
626 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
627 pci_resource_flags(pdev, info.index) &
628 IORESOURCE_MEM && info.size >= PAGE_SIZE) {
629 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
630 if (info.index == vdev->msix_bar) {
631 ret = msix_sparse_mmap_cap(vdev, &caps);
638 case VFIO_PCI_ROM_REGION_INDEX:
643 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
646 /* Report the BAR size, not the ROM size */
647 info.size = pci_resource_len(pdev, info.index);
649 /* Shadow ROMs appear as PCI option ROMs */
650 if (pdev->resource[PCI_ROM_RESOURCE].flags &
651 IORESOURCE_ROM_SHADOW)
657 /* Is it really there? */
658 io = pci_map_rom(pdev, &size);
663 pci_unmap_rom(pdev, io);
665 info.flags = VFIO_REGION_INFO_FLAG_READ;
668 case VFIO_PCI_VGA_REGION_INDEX:
672 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
674 info.flags = VFIO_REGION_INFO_FLAG_READ |
675 VFIO_REGION_INFO_FLAG_WRITE;
680 VFIO_PCI_NUM_REGIONS + vdev->num_regions)
683 i = info.index - VFIO_PCI_NUM_REGIONS;
685 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
686 info.size = vdev->region[i].size;
687 info.flags = vdev->region[i].flags;
689 ret = region_type_cap(vdev, &caps,
690 vdev->region[i].type,
691 vdev->region[i].subtype);
697 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
698 if (info.argsz < sizeof(info) + caps.size) {
699 info.argsz = sizeof(info) + caps.size;
702 vfio_info_cap_shift(&caps, sizeof(info));
703 if (copy_to_user((void __user *)arg +
704 sizeof(info), caps.buf,
709 info.cap_offset = sizeof(info);
715 return copy_to_user((void __user *)arg, &info, minsz) ?
718 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
719 struct vfio_irq_info info;
721 minsz = offsetofend(struct vfio_irq_info, count);
723 if (copy_from_user(&info, (void __user *)arg, minsz))
726 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
729 switch (info.index) {
730 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
731 case VFIO_PCI_REQ_IRQ_INDEX:
733 case VFIO_PCI_ERR_IRQ_INDEX:
734 if (pci_is_pcie(vdev->pdev))
736 /* pass thru to return error */
741 info.flags = VFIO_IRQ_INFO_EVENTFD;
743 info.count = vfio_pci_get_irq_count(vdev, info.index);
745 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
746 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
747 VFIO_IRQ_INFO_AUTOMASKED);
749 info.flags |= VFIO_IRQ_INFO_NORESIZE;
751 return copy_to_user((void __user *)arg, &info, minsz) ?
754 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
755 struct vfio_irq_set hdr;
759 minsz = offsetofend(struct vfio_irq_set, count);
761 if (copy_from_user(&hdr, (void __user *)arg, minsz))
764 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
765 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
766 VFIO_IRQ_SET_ACTION_TYPE_MASK))
769 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
771 int max = vfio_pci_get_irq_count(vdev, hdr.index);
773 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
774 size = sizeof(uint8_t);
775 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
776 size = sizeof(int32_t);
780 if (hdr.argsz - minsz < hdr.count * size ||
781 hdr.start >= max || hdr.start + hdr.count > max)
784 data = memdup_user((void __user *)(arg + minsz),
787 return PTR_ERR(data);
790 mutex_lock(&vdev->igate);
792 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
793 hdr.start, hdr.count, data);
795 mutex_unlock(&vdev->igate);
800 } else if (cmd == VFIO_DEVICE_RESET) {
801 return vdev->reset_works ?
802 pci_try_reset_function(vdev->pdev) : -EINVAL;
804 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
805 struct vfio_pci_hot_reset_info hdr;
806 struct vfio_pci_fill_info fill = { 0 };
807 struct vfio_pci_dependent_device *devices = NULL;
811 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
813 if (copy_from_user(&hdr, (void __user *)arg, minsz))
816 if (hdr.argsz < minsz)
821 /* Can we do a slot or bus reset or neither? */
822 if (!pci_probe_reset_slot(vdev->pdev->slot))
824 else if (pci_probe_reset_bus(vdev->pdev->bus))
827 /* How many devices are affected? */
828 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
834 WARN_ON(!fill.max); /* Should always be at least one */
837 * If there's enough space, fill it now, otherwise return
838 * -ENOSPC and the number of devices affected.
840 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
842 hdr.count = fill.max;
843 goto reset_info_exit;
846 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
850 fill.devices = devices;
852 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
857 * If a device was removed between counting and filling,
858 * we may come up short of fill.max. If a device was
859 * added, we'll have a return of -EAGAIN above.
862 hdr.count = fill.cur;
865 if (copy_to_user((void __user *)arg, &hdr, minsz))
869 if (copy_to_user((void __user *)(arg + minsz), devices,
870 hdr.count * sizeof(*devices)))
877 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
878 struct vfio_pci_hot_reset hdr;
880 struct vfio_pci_group_entry *groups;
881 struct vfio_pci_group_info info;
883 int i, count = 0, ret = 0;
885 minsz = offsetofend(struct vfio_pci_hot_reset, count);
887 if (copy_from_user(&hdr, (void __user *)arg, minsz))
890 if (hdr.argsz < minsz || hdr.flags)
893 /* Can we do a slot or bus reset or neither? */
894 if (!pci_probe_reset_slot(vdev->pdev->slot))
896 else if (pci_probe_reset_bus(vdev->pdev->bus))
900 * We can't let userspace give us an arbitrarily large
901 * buffer to copy, so verify how many we think there
902 * could be. Note groups can have multiple devices so
903 * one group per device is the max.
905 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
911 /* Somewhere between 1 and count is OK */
912 if (!hdr.count || hdr.count > count)
915 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
916 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
917 if (!group_fds || !groups) {
923 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
924 hdr.count * sizeof(*group_fds))) {
931 * For each group_fd, get the group through the vfio external
932 * user interface and store the group and iommu ID. This
933 * ensures the group is held across the reset.
935 for (i = 0; i < hdr.count; i++) {
936 struct vfio_group *group;
937 struct fd f = fdget(group_fds[i]);
943 group = vfio_group_get_external_user(f.file);
946 ret = PTR_ERR(group);
950 groups[i].group = group;
951 groups[i].id = vfio_external_user_iommu_id(group);
956 /* release reference to groups on error */
958 goto hot_reset_release;
960 info.count = hdr.count;
961 info.groups = groups;
964 * Test whether all the affected devices are contained
965 * by the set of groups provided by the user.
967 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
968 vfio_pci_validate_devs,
971 /* User has access, do the reset */
972 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
973 pci_try_reset_bus(vdev->pdev->bus);
976 for (i--; i >= 0; i--)
977 vfio_group_put_external_user(groups[i].group);
986 static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
987 size_t count, loff_t *ppos, bool iswrite)
989 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
990 struct vfio_pci_device *vdev = device_data;
992 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
996 case VFIO_PCI_CONFIG_REGION_INDEX:
997 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
999 case VFIO_PCI_ROM_REGION_INDEX:
1002 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
1004 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1005 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
1007 case VFIO_PCI_VGA_REGION_INDEX:
1008 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
1010 index -= VFIO_PCI_NUM_REGIONS;
1011 return vdev->region[index].ops->rw(vdev, buf,
1012 count, ppos, iswrite);
1018 static ssize_t vfio_pci_read(void *device_data, char __user *buf,
1019 size_t count, loff_t *ppos)
1024 return vfio_pci_rw(device_data, buf, count, ppos, false);
1027 static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
1028 size_t count, loff_t *ppos)
1033 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
1036 static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
1038 struct vfio_pci_device *vdev = device_data;
1039 struct pci_dev *pdev = vdev->pdev;
1041 u64 phys_len, req_len, pgoff, req_start;
1044 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1046 if (vma->vm_end < vma->vm_start)
1048 if ((vma->vm_flags & VM_SHARED) == 0)
1050 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1052 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
1055 phys_len = pci_resource_len(pdev, index);
1056 req_len = vma->vm_end - vma->vm_start;
1057 pgoff = vma->vm_pgoff &
1058 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1059 req_start = pgoff << PAGE_SHIFT;
1061 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
1064 if (index == vdev->msix_bar) {
1066 * Disallow mmaps overlapping the MSI-X table; users don't
1067 * get to touch this directly. We could find somewhere
1068 * else to map the overlap, but page granularity is only
1069 * a recommendation, not a requirement, so the user needs
1070 * to know which bits are real. Requiring them to mmap
1071 * around the table makes that clear.
1074 /* If neither entirely above nor below, then it overlaps */
1075 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
1076 req_start + req_len <= vdev->msix_offset))
1081 * Even though we don't make use of the barmap for the mmap,
1082 * we need to request the region and the barmap tracks that.
1084 if (!vdev->barmap[index]) {
1085 ret = pci_request_selected_regions(pdev,
1086 1 << index, "vfio-pci");
1090 vdev->barmap[index] = pci_iomap(pdev, index, 0);
1093 vma->vm_private_data = vdev;
1094 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1095 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
1097 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1098 req_len, vma->vm_page_prot);
1101 static void vfio_pci_request(void *device_data, unsigned int count)
1103 struct vfio_pci_device *vdev = device_data;
1105 mutex_lock(&vdev->igate);
1107 if (vdev->req_trigger) {
1109 dev_notice_ratelimited(&vdev->pdev->dev,
1110 "Relaying device request to user (#%u)\n",
1112 eventfd_signal(vdev->req_trigger, 1);
1113 } else if (count == 0) {
1114 dev_warn(&vdev->pdev->dev,
1115 "No device request channel registered, blocked until released by user\n");
1118 mutex_unlock(&vdev->igate);
1121 static const struct vfio_device_ops vfio_pci_ops = {
1123 .open = vfio_pci_open,
1124 .release = vfio_pci_release,
1125 .ioctl = vfio_pci_ioctl,
1126 .read = vfio_pci_read,
1127 .write = vfio_pci_write,
1128 .mmap = vfio_pci_mmap,
1129 .request = vfio_pci_request,
1132 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1134 struct vfio_pci_device *vdev;
1135 struct iommu_group *group;
1138 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1141 group = vfio_iommu_group_get(&pdev->dev);
1145 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1147 vfio_iommu_group_put(group, &pdev->dev);
1152 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1153 mutex_init(&vdev->igate);
1154 spin_lock_init(&vdev->irqlock);
1156 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1158 vfio_iommu_group_put(group, &pdev->dev);
1163 if (vfio_pci_is_vga(pdev)) {
1164 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1165 vga_set_legacy_decoding(pdev,
1166 vfio_pci_set_vga_decode(vdev, false));
1169 if (!disable_idle_d3) {
1171 * pci-core sets the device power state to an unknown value at
1172 * bootup and after being removed from a driver. The only
1173 * transition it allows from this unknown state is to D0, which
1174 * typically happens when a driver calls pci_enable_device().
1175 * We're not ready to enable the device yet, but we do want to
1176 * be able to get to D3. Therefore first do a D0 transition
1177 * before going to D3.
1179 pci_set_power_state(pdev, PCI_D0);
1180 pci_set_power_state(pdev, PCI_D3hot);
1186 static void vfio_pci_remove(struct pci_dev *pdev)
1188 struct vfio_pci_device *vdev;
1190 vdev = vfio_del_group_dev(&pdev->dev);
1194 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
1195 kfree(vdev->region);
1198 if (vfio_pci_is_vga(pdev)) {
1199 vga_client_register(pdev, NULL, NULL, NULL);
1200 vga_set_legacy_decoding(pdev,
1201 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1202 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
1205 if (!disable_idle_d3)
1206 pci_set_power_state(pdev, PCI_D0);
1209 static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1210 pci_channel_state_t state)
1212 struct vfio_pci_device *vdev;
1213 struct vfio_device *device;
1215 device = vfio_device_get_from_dev(&pdev->dev);
1217 return PCI_ERS_RESULT_DISCONNECT;
1219 vdev = vfio_device_data(device);
1221 vfio_device_put(device);
1222 return PCI_ERS_RESULT_DISCONNECT;
1225 mutex_lock(&vdev->igate);
1227 if (vdev->err_trigger)
1228 eventfd_signal(vdev->err_trigger, 1);
1230 mutex_unlock(&vdev->igate);
1232 vfio_device_put(device);
1234 return PCI_ERS_RESULT_CAN_RECOVER;
1237 static const struct pci_error_handlers vfio_err_handlers = {
1238 .error_detected = vfio_pci_aer_err_detected,
1241 static struct pci_driver vfio_pci_driver = {
1243 .id_table = NULL, /* only dynamic ids */
1244 .probe = vfio_pci_probe,
1245 .remove = vfio_pci_remove,
1246 .err_handler = &vfio_err_handlers,
1249 struct vfio_devices {
1250 struct vfio_device **devices;
1255 static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
1257 struct vfio_devices *devs = data;
1258 struct vfio_device *device;
1260 if (devs->cur_index == devs->max_index)
1263 device = vfio_device_get_from_dev(&pdev->dev);
1267 if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1268 vfio_device_put(device);
1272 devs->devices[devs->cur_index++] = device;
1277 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1278 * this device that are needs_reset and all of the affected devices are unused
1279 * (!refcnt). Callers are required to hold driver_lock when calling this to
1280 * prevent device opens and concurrent bus reset attempts. We prevent device
1281 * unbinds by acquiring and holding a reference to the vfio_device.
1283 * NB: vfio-core considers a group to be viable even if some devices are
1284 * bound to drivers like pci-stub or pcieport. Here we require all devices
1285 * to be bound to vfio_pci since that's the only way we can be sure they
1288 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1290 struct vfio_devices devs = { .cur_index = 0 };
1291 int i = 0, ret = -EINVAL;
1292 bool needs_reset = false, slot = false;
1293 struct vfio_pci_device *tmp;
1295 if (!pci_probe_reset_slot(vdev->pdev->slot))
1297 else if (pci_probe_reset_bus(vdev->pdev->bus))
1300 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1305 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1309 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
1310 vfio_pci_get_devs, &devs, slot))
1313 for (i = 0; i < devs.cur_index; i++) {
1314 tmp = vfio_device_data(devs.devices[i]);
1315 if (tmp->needs_reset)
1322 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1323 pci_try_reset_bus(vdev->pdev->bus);
1326 for (i = 0; i < devs.cur_index; i++) {
1327 tmp = vfio_device_data(devs.devices[i]);
1329 tmp->needs_reset = false;
1331 if (!tmp->refcnt && !disable_idle_d3)
1332 pci_set_power_state(tmp->pdev, PCI_D3hot);
1334 vfio_device_put(devs.devices[i]);
1337 kfree(devs.devices);
1340 static void __exit vfio_pci_cleanup(void)
1342 pci_unregister_driver(&vfio_pci_driver);
1343 vfio_pci_uninit_perm_bits();
1346 static void __init vfio_pci_fill_ids(void)
1351 /* no ids passed actually */
1355 /* add ids specified in the module parameter */
1357 while ((id = strsep(&p, ","))) {
1358 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1359 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1365 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1366 &vendor, &device, &subvendor, &subdevice,
1367 &class, &class_mask);
1370 pr_warn("invalid id string \"%s\"\n", id);
1374 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1375 subvendor, subdevice, class, class_mask, 0);
1377 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1378 vendor, device, subvendor, subdevice,
1379 class, class_mask, rc);
1381 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1382 vendor, device, subvendor, subdevice,
1387 static int __init vfio_pci_init(void)
1391 /* Allocate shared config space permision data used by all devices */
1392 ret = vfio_pci_init_perm_bits();
1396 /* Register and scan for devices */
1397 ret = pci_register_driver(&vfio_pci_driver);
1401 vfio_pci_fill_ids();
1406 vfio_pci_uninit_perm_bits();
1410 module_init(vfio_pci_init);
1411 module_exit(vfio_pci_cleanup);
1413 MODULE_VERSION(DRIVER_VERSION);
1414 MODULE_LICENSE("GPL v2");
1415 MODULE_AUTHOR(DRIVER_AUTHOR);
1416 MODULE_DESCRIPTION(DRIVER_DESC);