2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
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.
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"
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
[email protected] 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:
213 static int vfio_pci_enable(struct vfio_pci_device *vdev)
215 struct pci_dev *pdev = vdev->pdev;
220 pci_set_power_state(pdev, PCI_D0);
222 /* Don't allow our initial saved state to include busmaster */
223 pci_clear_master(pdev);
225 ret = pci_enable_device(pdev);
229 /* If reset fails because of the device lock, fail this path entirely */
230 ret = pci_try_reset_function(pdev);
231 if (ret == -EAGAIN) {
232 pci_disable_device(pdev);
236 vdev->reset_works = !ret;
237 pci_save_state(pdev);
238 vdev->pci_saved_state = pci_store_saved_state(pdev);
239 if (!vdev->pci_saved_state)
240 pr_debug("%s: Couldn't store %s saved state\n",
241 __func__, dev_name(&pdev->dev));
243 if (likely(!nointxmask)) {
244 if (vfio_pci_nointx(pdev)) {
245 dev_info(&pdev->dev, "Masking broken INTx support\n");
249 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
252 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
253 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
254 cmd &= ~PCI_COMMAND_INTX_DISABLE;
255 pci_write_config_word(pdev, PCI_COMMAND, cmd);
258 ret = vfio_config_init(vdev);
260 kfree(vdev->pci_saved_state);
261 vdev->pci_saved_state = NULL;
262 pci_disable_device(pdev);
266 msix_pos = pdev->msix_cap;
271 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
272 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
274 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
275 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
276 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
278 vdev->msix_bar = 0xFF;
280 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
281 vdev->has_vga = true;
284 if (vfio_pci_is_vga(pdev) &&
285 pdev->vendor == PCI_VENDOR_ID_INTEL &&
286 IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
287 ret = vfio_pci_igd_init(vdev);
289 dev_warn(&vdev->pdev->dev,
290 "Failed to setup Intel IGD regions\n");
291 vfio_pci_disable(vdev);
296 vfio_pci_probe_mmaps(vdev);
301 static void vfio_pci_disable(struct vfio_pci_device *vdev)
303 struct pci_dev *pdev = vdev->pdev;
304 struct vfio_pci_dummy_resource *dummy_res, *tmp;
307 /* Stop the device from further DMA */
308 pci_clear_master(pdev);
310 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
311 VFIO_IRQ_SET_ACTION_TRIGGER,
312 vdev->irq_type, 0, 0, NULL);
314 vdev->virq_disabled = false;
316 for (i = 0; i < vdev->num_regions; i++)
317 vdev->region[i].ops->release(vdev, &vdev->region[i]);
319 vdev->num_regions = 0;
321 vdev->region = NULL; /* don't krealloc a freed pointer */
323 vfio_config_free(vdev);
325 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
326 if (!vdev->barmap[bar])
328 pci_iounmap(pdev, vdev->barmap[bar]);
329 pci_release_selected_regions(pdev, 1 << bar);
330 vdev->barmap[bar] = NULL;
333 list_for_each_entry_safe(dummy_res, tmp,
334 &vdev->dummy_resources_list, res_next) {
335 list_del(&dummy_res->res_next);
336 release_resource(&dummy_res->resource);
340 vdev->needs_reset = true;
343 * If we have saved state, restore it. If we can reset the device,
344 * even better. Resetting with current state seems better than
345 * nothing, but saving and restoring current state without reset
348 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
349 pr_info("%s: Couldn't reload %s saved state\n",
350 __func__, dev_name(&pdev->dev));
352 if (!vdev->reset_works)
355 pci_save_state(pdev);
359 * Disable INTx and MSI, presumably to avoid spurious interrupts
360 * during reset. Stolen from pci_reset_function()
362 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
365 * Try to reset the device. The success of this is dependent on
366 * being able to lock the device, which is not always possible.
368 if (vdev->reset_works && !pci_try_reset_function(pdev))
369 vdev->needs_reset = false;
371 pci_restore_state(pdev);
373 pci_disable_device(pdev);
375 vfio_pci_try_bus_reset(vdev);
377 if (!disable_idle_d3)
378 pci_set_power_state(pdev, PCI_D3hot);
381 static void vfio_pci_release(void *device_data)
383 struct vfio_pci_device *vdev = device_data;
385 mutex_lock(&driver_lock);
387 if (!(--vdev->refcnt)) {
388 vfio_spapr_pci_eeh_release(vdev->pdev);
389 vfio_pci_disable(vdev);
392 mutex_unlock(&driver_lock);
394 module_put(THIS_MODULE);
397 static int vfio_pci_open(void *device_data)
399 struct vfio_pci_device *vdev = device_data;
402 if (!try_module_get(THIS_MODULE))
405 mutex_lock(&driver_lock);
408 ret = vfio_pci_enable(vdev);
412 vfio_spapr_pci_eeh_open(vdev->pdev);
416 mutex_unlock(&driver_lock);
418 module_put(THIS_MODULE);
422 static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
424 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
426 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
427 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && !vdev->nointx && pin)
430 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
434 pos = vdev->pdev->msi_cap;
436 pci_read_config_word(vdev->pdev,
437 pos + PCI_MSI_FLAGS, &flags);
438 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
440 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
444 pos = vdev->pdev->msix_cap;
446 pci_read_config_word(vdev->pdev,
447 pos + PCI_MSIX_FLAGS, &flags);
449 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
451 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
452 if (pci_is_pcie(vdev->pdev))
454 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
461 static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
467 struct vfio_pci_fill_info {
470 struct vfio_pci_dependent_device *devices;
473 static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
475 struct vfio_pci_fill_info *fill = data;
476 struct iommu_group *iommu_group;
478 if (fill->cur == fill->max)
479 return -EAGAIN; /* Something changed, try again */
481 iommu_group = iommu_group_get(&pdev->dev);
483 return -EPERM; /* Cannot reset non-isolated devices */
485 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
486 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
487 fill->devices[fill->cur].bus = pdev->bus->number;
488 fill->devices[fill->cur].devfn = pdev->devfn;
490 iommu_group_put(iommu_group);
494 struct vfio_pci_group_entry {
495 struct vfio_group *group;
499 struct vfio_pci_group_info {
501 struct vfio_pci_group_entry *groups;
504 static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
506 struct vfio_pci_group_info *info = data;
507 struct iommu_group *group;
510 group = iommu_group_get(&pdev->dev);
514 id = iommu_group_id(group);
516 for (i = 0; i < info->count; i++)
517 if (info->groups[i].id == id)
520 iommu_group_put(group);
522 return (i == info->count) ? -EINVAL : 0;
525 static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
527 for (; pdev; pdev = pdev->bus->self)
528 if (pdev->bus == slot->bus)
529 return (pdev->slot == slot);
533 struct vfio_pci_walk_info {
534 int (*fn)(struct pci_dev *, void *data);
536 struct pci_dev *pdev;
541 static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
543 struct vfio_pci_walk_info *walk = data;
545 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
546 walk->ret = walk->fn(pdev, walk->data);
551 static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
552 int (*fn)(struct pci_dev *,
553 void *data), void *data,
556 struct vfio_pci_walk_info walk = {
557 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
560 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
565 static int msix_mmappable_cap(struct vfio_pci_device *vdev,
566 struct vfio_info_cap *caps)
568 struct vfio_info_cap_header header = {
569 .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE,
573 return vfio_info_add_capability(caps, &header, sizeof(header));
576 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
577 unsigned int type, unsigned int subtype,
578 const struct vfio_pci_regops *ops,
579 size_t size, u32 flags, void *data)
581 struct vfio_pci_region *region;
583 region = krealloc(vdev->region,
584 (vdev->num_regions + 1) * sizeof(*region),
589 vdev->region = region;
590 vdev->region[vdev->num_regions].type = type;
591 vdev->region[vdev->num_regions].subtype = subtype;
592 vdev->region[vdev->num_regions].ops = ops;
593 vdev->region[vdev->num_regions].size = size;
594 vdev->region[vdev->num_regions].flags = flags;
595 vdev->region[vdev->num_regions].data = data;
602 static long vfio_pci_ioctl(void *device_data,
603 unsigned int cmd, unsigned long arg)
605 struct vfio_pci_device *vdev = device_data;
608 if (cmd == VFIO_DEVICE_GET_INFO) {
609 struct vfio_device_info info;
611 minsz = offsetofend(struct vfio_device_info, num_irqs);
613 if (copy_from_user(&info, (void __user *)arg, minsz))
616 if (info.argsz < minsz)
619 info.flags = VFIO_DEVICE_FLAGS_PCI;
621 if (vdev->reset_works)
622 info.flags |= VFIO_DEVICE_FLAGS_RESET;
624 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
625 info.num_irqs = VFIO_PCI_NUM_IRQS;
627 return copy_to_user((void __user *)arg, &info, minsz) ?
630 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
631 struct pci_dev *pdev = vdev->pdev;
632 struct vfio_region_info info;
633 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
636 minsz = offsetofend(struct vfio_region_info, offset);
638 if (copy_from_user(&info, (void __user *)arg, minsz))
641 if (info.argsz < minsz)
644 switch (info.index) {
645 case VFIO_PCI_CONFIG_REGION_INDEX:
646 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
647 info.size = pdev->cfg_size;
648 info.flags = VFIO_REGION_INFO_FLAG_READ |
649 VFIO_REGION_INFO_FLAG_WRITE;
651 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
652 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
653 info.size = pci_resource_len(pdev, info.index);
659 info.flags = VFIO_REGION_INFO_FLAG_READ |
660 VFIO_REGION_INFO_FLAG_WRITE;
661 if (vdev->bar_mmap_supported[info.index]) {
662 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
663 if (info.index == vdev->msix_bar) {
664 ret = msix_mmappable_cap(vdev, &caps);
671 case VFIO_PCI_ROM_REGION_INDEX:
676 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
679 /* Report the BAR size, not the ROM size */
680 info.size = pci_resource_len(pdev, info.index);
682 /* Shadow ROMs appear as PCI option ROMs */
683 if (pdev->resource[PCI_ROM_RESOURCE].flags &
684 IORESOURCE_ROM_SHADOW)
690 /* Is it really there? */
691 io = pci_map_rom(pdev, &size);
696 pci_unmap_rom(pdev, io);
698 info.flags = VFIO_REGION_INFO_FLAG_READ;
701 case VFIO_PCI_VGA_REGION_INDEX:
705 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
707 info.flags = VFIO_REGION_INFO_FLAG_READ |
708 VFIO_REGION_INFO_FLAG_WRITE;
713 struct vfio_region_info_cap_type cap_type = {
714 .header.id = VFIO_REGION_INFO_CAP_TYPE,
715 .header.version = 1 };
718 VFIO_PCI_NUM_REGIONS + vdev->num_regions)
721 i = info.index - VFIO_PCI_NUM_REGIONS;
723 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
724 info.size = vdev->region[i].size;
725 info.flags = vdev->region[i].flags;
727 cap_type.type = vdev->region[i].type;
728 cap_type.subtype = vdev->region[i].subtype;
730 ret = vfio_info_add_capability(&caps, &cap_type.header,
739 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
740 if (info.argsz < sizeof(info) + caps.size) {
741 info.argsz = sizeof(info) + caps.size;
744 vfio_info_cap_shift(&caps, sizeof(info));
745 if (copy_to_user((void __user *)arg +
746 sizeof(info), caps.buf,
751 info.cap_offset = sizeof(info);
757 return copy_to_user((void __user *)arg, &info, minsz) ?
760 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
761 struct vfio_irq_info info;
763 minsz = offsetofend(struct vfio_irq_info, count);
765 if (copy_from_user(&info, (void __user *)arg, minsz))
768 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
771 switch (info.index) {
772 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
773 case VFIO_PCI_REQ_IRQ_INDEX:
775 case VFIO_PCI_ERR_IRQ_INDEX:
776 if (pci_is_pcie(vdev->pdev))
778 /* pass thru to return error */
783 info.flags = VFIO_IRQ_INFO_EVENTFD;
785 info.count = vfio_pci_get_irq_count(vdev, info.index);
787 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
788 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
789 VFIO_IRQ_INFO_AUTOMASKED);
791 info.flags |= VFIO_IRQ_INFO_NORESIZE;
793 return copy_to_user((void __user *)arg, &info, minsz) ?
796 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
797 struct vfio_irq_set hdr;
800 size_t data_size = 0;
802 minsz = offsetofend(struct vfio_irq_set, count);
804 if (copy_from_user(&hdr, (void __user *)arg, minsz))
807 max = vfio_pci_get_irq_count(vdev, hdr.index);
809 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
810 VFIO_PCI_NUM_IRQS, &data_size);
815 data = memdup_user((void __user *)(arg + minsz),
818 return PTR_ERR(data);
821 mutex_lock(&vdev->igate);
823 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
824 hdr.start, hdr.count, data);
826 mutex_unlock(&vdev->igate);
831 } else if (cmd == VFIO_DEVICE_RESET) {
832 return vdev->reset_works ?
833 pci_try_reset_function(vdev->pdev) : -EINVAL;
835 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
836 struct vfio_pci_hot_reset_info hdr;
837 struct vfio_pci_fill_info fill = { 0 };
838 struct vfio_pci_dependent_device *devices = NULL;
842 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
844 if (copy_from_user(&hdr, (void __user *)arg, minsz))
847 if (hdr.argsz < minsz)
852 /* Can we do a slot or bus reset or neither? */
853 if (!pci_probe_reset_slot(vdev->pdev->slot))
855 else if (pci_probe_reset_bus(vdev->pdev->bus))
858 /* How many devices are affected? */
859 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
865 WARN_ON(!fill.max); /* Should always be at least one */
868 * If there's enough space, fill it now, otherwise return
869 * -ENOSPC and the number of devices affected.
871 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
873 hdr.count = fill.max;
874 goto reset_info_exit;
877 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
881 fill.devices = devices;
883 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
888 * If a device was removed between counting and filling,
889 * we may come up short of fill.max. If a device was
890 * added, we'll have a return of -EAGAIN above.
893 hdr.count = fill.cur;
896 if (copy_to_user((void __user *)arg, &hdr, minsz))
900 if (copy_to_user((void __user *)(arg + minsz), devices,
901 hdr.count * sizeof(*devices)))
908 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
909 struct vfio_pci_hot_reset hdr;
911 struct vfio_pci_group_entry *groups;
912 struct vfio_pci_group_info info;
914 int i, count = 0, ret = 0;
916 minsz = offsetofend(struct vfio_pci_hot_reset, count);
918 if (copy_from_user(&hdr, (void __user *)arg, minsz))
921 if (hdr.argsz < minsz || hdr.flags)
924 /* Can we do a slot or bus reset or neither? */
925 if (!pci_probe_reset_slot(vdev->pdev->slot))
927 else if (pci_probe_reset_bus(vdev->pdev->bus))
931 * We can't let userspace give us an arbitrarily large
932 * buffer to copy, so verify how many we think there
933 * could be. Note groups can have multiple devices so
934 * one group per device is the max.
936 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
942 /* Somewhere between 1 and count is OK */
943 if (!hdr.count || hdr.count > count)
946 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
947 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
948 if (!group_fds || !groups) {
954 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
955 hdr.count * sizeof(*group_fds))) {
962 * For each group_fd, get the group through the vfio external
963 * user interface and store the group and iommu ID. This
964 * ensures the group is held across the reset.
966 for (i = 0; i < hdr.count; i++) {
967 struct vfio_group *group;
968 struct fd f = fdget(group_fds[i]);
974 group = vfio_group_get_external_user(f.file);
977 ret = PTR_ERR(group);
981 groups[i].group = group;
982 groups[i].id = vfio_external_user_iommu_id(group);
987 /* release reference to groups on error */
989 goto hot_reset_release;
991 info.count = hdr.count;
992 info.groups = groups;
995 * Test whether all the affected devices are contained
996 * by the set of groups provided by the user.
998 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
999 vfio_pci_validate_devs,
1002 /* User has access, do the reset */
1003 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1004 pci_try_reset_bus(vdev->pdev->bus);
1007 for (i--; i >= 0; i--)
1008 vfio_group_put_external_user(groups[i].group);
1017 static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
1018 size_t count, loff_t *ppos, bool iswrite)
1020 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1021 struct vfio_pci_device *vdev = device_data;
1023 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
1027 case VFIO_PCI_CONFIG_REGION_INDEX:
1028 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
1030 case VFIO_PCI_ROM_REGION_INDEX:
1033 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
1035 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1036 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
1038 case VFIO_PCI_VGA_REGION_INDEX:
1039 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
1041 index -= VFIO_PCI_NUM_REGIONS;
1042 return vdev->region[index].ops->rw(vdev, buf,
1043 count, ppos, iswrite);
1049 static ssize_t vfio_pci_read(void *device_data, char __user *buf,
1050 size_t count, loff_t *ppos)
1055 return vfio_pci_rw(device_data, buf, count, ppos, false);
1058 static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
1059 size_t count, loff_t *ppos)
1064 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
1067 static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
1069 struct vfio_pci_device *vdev = device_data;
1070 struct pci_dev *pdev = vdev->pdev;
1072 u64 phys_len, req_len, pgoff, req_start;
1075 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1077 if (vma->vm_end < vma->vm_start)
1079 if ((vma->vm_flags & VM_SHARED) == 0)
1081 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1083 if (!vdev->bar_mmap_supported[index])
1086 phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
1087 req_len = vma->vm_end - vma->vm_start;
1088 pgoff = vma->vm_pgoff &
1089 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1090 req_start = pgoff << PAGE_SHIFT;
1092 if (req_start + req_len > phys_len)
1096 * Even though we don't make use of the barmap for the mmap,
1097 * we need to request the region and the barmap tracks that.
1099 if (!vdev->barmap[index]) {
1100 ret = pci_request_selected_regions(pdev,
1101 1 << index, "vfio-pci");
1105 vdev->barmap[index] = pci_iomap(pdev, index, 0);
1106 if (!vdev->barmap[index]) {
1107 pci_release_selected_regions(pdev, 1 << index);
1112 vma->vm_private_data = vdev;
1113 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1114 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
1116 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1117 req_len, vma->vm_page_prot);
1120 static void vfio_pci_request(void *device_data, unsigned int count)
1122 struct vfio_pci_device *vdev = device_data;
1124 mutex_lock(&vdev->igate);
1126 if (vdev->req_trigger) {
1128 dev_notice_ratelimited(&vdev->pdev->dev,
1129 "Relaying device request to user (#%u)\n",
1131 eventfd_signal(vdev->req_trigger, 1);
1132 } else if (count == 0) {
1133 dev_warn(&vdev->pdev->dev,
1134 "No device request channel registered, blocked until released by user\n");
1137 mutex_unlock(&vdev->igate);
1140 static const struct vfio_device_ops vfio_pci_ops = {
1142 .open = vfio_pci_open,
1143 .release = vfio_pci_release,
1144 .ioctl = vfio_pci_ioctl,
1145 .read = vfio_pci_read,
1146 .write = vfio_pci_write,
1147 .mmap = vfio_pci_mmap,
1148 .request = vfio_pci_request,
1151 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1153 struct vfio_pci_device *vdev;
1154 struct iommu_group *group;
1157 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1160 group = vfio_iommu_group_get(&pdev->dev);
1164 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1166 vfio_iommu_group_put(group, &pdev->dev);
1171 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1172 mutex_init(&vdev->igate);
1173 spin_lock_init(&vdev->irqlock);
1175 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1177 vfio_iommu_group_put(group, &pdev->dev);
1182 if (vfio_pci_is_vga(pdev)) {
1183 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1184 vga_set_legacy_decoding(pdev,
1185 vfio_pci_set_vga_decode(vdev, false));
1188 if (!disable_idle_d3) {
1190 * pci-core sets the device power state to an unknown value at
1191 * bootup and after being removed from a driver. The only
1192 * transition it allows from this unknown state is to D0, which
1193 * typically happens when a driver calls pci_enable_device().
1194 * We're not ready to enable the device yet, but we do want to
1195 * be able to get to D3. Therefore first do a D0 transition
1196 * before going to D3.
1198 pci_set_power_state(pdev, PCI_D0);
1199 pci_set_power_state(pdev, PCI_D3hot);
1205 static void vfio_pci_remove(struct pci_dev *pdev)
1207 struct vfio_pci_device *vdev;
1209 vdev = vfio_del_group_dev(&pdev->dev);
1213 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
1214 kfree(vdev->region);
1217 if (vfio_pci_is_vga(pdev)) {
1218 vga_client_register(pdev, NULL, NULL, NULL);
1219 vga_set_legacy_decoding(pdev,
1220 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1221 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
1224 if (!disable_idle_d3)
1225 pci_set_power_state(pdev, PCI_D0);
1228 static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1229 pci_channel_state_t state)
1231 struct vfio_pci_device *vdev;
1232 struct vfio_device *device;
1234 device = vfio_device_get_from_dev(&pdev->dev);
1236 return PCI_ERS_RESULT_DISCONNECT;
1238 vdev = vfio_device_data(device);
1240 vfio_device_put(device);
1241 return PCI_ERS_RESULT_DISCONNECT;
1244 mutex_lock(&vdev->igate);
1246 if (vdev->err_trigger)
1247 eventfd_signal(vdev->err_trigger, 1);
1249 mutex_unlock(&vdev->igate);
1251 vfio_device_put(device);
1253 return PCI_ERS_RESULT_CAN_RECOVER;
1256 static const struct pci_error_handlers vfio_err_handlers = {
1257 .error_detected = vfio_pci_aer_err_detected,
1260 static struct pci_driver vfio_pci_driver = {
1262 .id_table = NULL, /* only dynamic ids */
1263 .probe = vfio_pci_probe,
1264 .remove = vfio_pci_remove,
1265 .err_handler = &vfio_err_handlers,
1268 struct vfio_devices {
1269 struct vfio_device **devices;
1274 static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
1276 struct vfio_devices *devs = data;
1277 struct vfio_device *device;
1279 if (devs->cur_index == devs->max_index)
1282 device = vfio_device_get_from_dev(&pdev->dev);
1286 if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1287 vfio_device_put(device);
1291 devs->devices[devs->cur_index++] = device;
1296 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1297 * this device that are needs_reset and all of the affected devices are unused
1298 * (!refcnt). Callers are required to hold driver_lock when calling this to
1299 * prevent device opens and concurrent bus reset attempts. We prevent device
1300 * unbinds by acquiring and holding a reference to the vfio_device.
1302 * NB: vfio-core considers a group to be viable even if some devices are
1303 * bound to drivers like pci-stub or pcieport. Here we require all devices
1304 * to be bound to vfio_pci since that's the only way we can be sure they
1307 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1309 struct vfio_devices devs = { .cur_index = 0 };
1310 int i = 0, ret = -EINVAL;
1311 bool needs_reset = false, slot = false;
1312 struct vfio_pci_device *tmp;
1314 if (!pci_probe_reset_slot(vdev->pdev->slot))
1316 else if (pci_probe_reset_bus(vdev->pdev->bus))
1319 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1324 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1328 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
1329 vfio_pci_get_devs, &devs, slot))
1332 for (i = 0; i < devs.cur_index; i++) {
1333 tmp = vfio_device_data(devs.devices[i]);
1334 if (tmp->needs_reset)
1341 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1342 pci_try_reset_bus(vdev->pdev->bus);
1345 for (i = 0; i < devs.cur_index; i++) {
1346 tmp = vfio_device_data(devs.devices[i]);
1348 tmp->needs_reset = false;
1350 if (!tmp->refcnt && !disable_idle_d3)
1351 pci_set_power_state(tmp->pdev, PCI_D3hot);
1353 vfio_device_put(devs.devices[i]);
1356 kfree(devs.devices);
1359 static void __exit vfio_pci_cleanup(void)
1361 pci_unregister_driver(&vfio_pci_driver);
1362 vfio_pci_uninit_perm_bits();
1365 static void __init vfio_pci_fill_ids(void)
1370 /* no ids passed actually */
1374 /* add ids specified in the module parameter */
1376 while ((id = strsep(&p, ","))) {
1377 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1378 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1384 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1385 &vendor, &device, &subvendor, &subdevice,
1386 &class, &class_mask);
1389 pr_warn("invalid id string \"%s\"\n", id);
1393 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1394 subvendor, subdevice, class, class_mask, 0);
1396 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1397 vendor, device, subvendor, subdevice,
1398 class, class_mask, rc);
1400 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1401 vendor, device, subvendor, subdevice,
1406 static int __init vfio_pci_init(void)
1410 /* Allocate shared config space permision data used by all devices */
1411 ret = vfio_pci_init_perm_bits();
1415 /* Register and scan for devices */
1416 ret = pci_register_driver(&vfio_pci_driver);
1420 vfio_pci_fill_ids();
1425 vfio_pci_uninit_perm_bits();
1429 module_init(vfio_pci_init);
1430 module_exit(vfio_pci_cleanup);
1432 MODULE_VERSION(DRIVER_VERSION);
1433 MODULE_LICENSE("GPL v2");
1434 MODULE_AUTHOR(DRIVER_AUTHOR);
1435 MODULE_DESCRIPTION(DRIVER_DESC);