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>
31 #include <linux/nospec.h>
33 #include "vfio_pci_private.h"
35 #define DRIVER_VERSION "0.2"
37 #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
39 static char ids[1024] __initdata;
40 module_param_string(ids, ids, sizeof(ids), 0);
41 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");
43 static bool nointxmask;
44 module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
45 MODULE_PARM_DESC(nointxmask,
46 "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.");
48 #ifdef CONFIG_VFIO_PCI_VGA
49 static bool disable_vga;
50 module_param(disable_vga, bool, S_IRUGO);
51 MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
54 static bool disable_idle_d3;
55 module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(disable_idle_d3,
57 "Disable using the PCI D3 low power state for idle, unused devices");
59 static DEFINE_MUTEX(driver_lock);
61 static inline bool vfio_vga_disabled(void)
63 #ifdef CONFIG_VFIO_PCI_VGA
71 * Our VGA arbiter participation is limited since we don't know anything
72 * about the device itself. However, if the device is the only VGA device
73 * downstream of a bridge and VFIO VGA support is disabled, then we can
74 * safely return legacy VGA IO and memory as not decoded since the user
75 * has no way to get to it and routing can be disabled externally at the
78 static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
80 struct vfio_pci_device *vdev = opaque;
81 struct pci_dev *tmp = NULL, *pdev = vdev->pdev;
82 unsigned char max_busnr;
85 if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
86 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
87 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
89 max_busnr = pci_bus_max_busnr(pdev->bus);
90 decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
92 while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
94 pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
95 pci_is_root_bus(tmp->bus))
98 if (tmp->bus->number >= pdev->bus->number &&
99 tmp->bus->number <= max_busnr) {
101 decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
109 static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
111 return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
114 static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev)
116 struct resource *res;
118 struct vfio_pci_dummy_resource *dummy_res;
120 INIT_LIST_HEAD(&vdev->dummy_resources_list);
122 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
123 res = vdev->pdev->resource + bar;
125 if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
128 if (!(res->flags & IORESOURCE_MEM))
132 * The PCI core shouldn't set up a resource with a
133 * type but zero size. But there may be bugs that
134 * cause us to do that.
136 if (!resource_size(res))
139 if (resource_size(res) >= PAGE_SIZE) {
140 vdev->bar_mmap_supported[bar] = true;
144 if (!(res->start & ~PAGE_MASK)) {
146 * Add a dummy resource to reserve the remainder
147 * of the exclusive page in case that hot-add
148 * device's bar is assigned into it.
150 dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL);
151 if (dummy_res == NULL)
154 dummy_res->resource.name = "vfio sub-page reserved";
155 dummy_res->resource.start = res->end + 1;
156 dummy_res->resource.end = res->start + PAGE_SIZE - 1;
157 dummy_res->resource.flags = res->flags;
158 if (request_resource(res->parent,
159 &dummy_res->resource)) {
163 dummy_res->index = bar;
164 list_add(&dummy_res->res_next,
165 &vdev->dummy_resources_list);
166 vdev->bar_mmap_supported[bar] = true;
170 * Here we don't handle the case when the BAR is not page
171 * aligned because we can't expect the BAR will be
172 * assigned into the same location in a page in guest
173 * when we passthrough the BAR. And it's hard to access
174 * this BAR in userspace because we have no way to get
175 * the BAR's location in a page.
178 vdev->bar_mmap_supported[bar] = false;
182 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
183 static void vfio_pci_disable(struct vfio_pci_device *vdev);
186 * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
187 * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
188 * If a device implements the former but not the latter we would typically
189 * expect broken_intx_masking be set and require an exclusive interrupt.
190 * However since we do have control of the device's ability to assert INTx,
191 * we can instead pretend that the device does not implement INTx, virtualizing
192 * the pin register to report zero and maintaining DisINTx set on the host.
194 static bool vfio_pci_nointx(struct pci_dev *pdev)
196 switch (pdev->vendor) {
197 case PCI_VENDOR_ID_INTEL:
198 switch (pdev->device) {
199 /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */
202 case 0x1580 ... 0x1581:
203 case 0x1583 ... 0x158b:
204 case 0x37d0 ... 0x37d2:
214 static int vfio_pci_enable(struct vfio_pci_device *vdev)
216 struct pci_dev *pdev = vdev->pdev;
221 pci_set_power_state(pdev, PCI_D0);
223 /* Don't allow our initial saved state to include busmaster */
224 pci_clear_master(pdev);
226 ret = pci_enable_device(pdev);
230 /* If reset fails because of the device lock, fail this path entirely */
231 ret = pci_try_reset_function(pdev);
232 if (ret == -EAGAIN) {
233 pci_disable_device(pdev);
237 vdev->reset_works = !ret;
238 pci_save_state(pdev);
239 vdev->pci_saved_state = pci_store_saved_state(pdev);
240 if (!vdev->pci_saved_state)
241 pr_debug("%s: Couldn't store %s saved state\n",
242 __func__, dev_name(&pdev->dev));
244 if (likely(!nointxmask)) {
245 if (vfio_pci_nointx(pdev)) {
246 dev_info(&pdev->dev, "Masking broken INTx support\n");
250 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
253 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
254 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
255 cmd &= ~PCI_COMMAND_INTX_DISABLE;
256 pci_write_config_word(pdev, PCI_COMMAND, cmd);
259 ret = vfio_config_init(vdev);
261 kfree(vdev->pci_saved_state);
262 vdev->pci_saved_state = NULL;
263 pci_disable_device(pdev);
267 msix_pos = pdev->msix_cap;
272 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
273 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
275 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
276 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
277 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
279 vdev->msix_bar = 0xFF;
281 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
282 vdev->has_vga = true;
285 if (vfio_pci_is_vga(pdev) &&
286 pdev->vendor == PCI_VENDOR_ID_INTEL &&
287 IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
288 ret = vfio_pci_igd_init(vdev);
290 dev_warn(&vdev->pdev->dev,
291 "Failed to setup Intel IGD regions\n");
292 vfio_pci_disable(vdev);
297 vfio_pci_probe_mmaps(vdev);
302 static void vfio_pci_disable(struct vfio_pci_device *vdev)
304 struct pci_dev *pdev = vdev->pdev;
305 struct vfio_pci_dummy_resource *dummy_res, *tmp;
306 struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp;
309 /* Stop the device from further DMA */
310 pci_clear_master(pdev);
312 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
313 VFIO_IRQ_SET_ACTION_TRIGGER,
314 vdev->irq_type, 0, 0, NULL);
316 /* Device closed, don't need mutex here */
317 list_for_each_entry_safe(ioeventfd, ioeventfd_tmp,
318 &vdev->ioeventfds_list, next) {
319 vfio_virqfd_disable(&ioeventfd->virqfd);
320 list_del(&ioeventfd->next);
323 vdev->ioeventfds_nr = 0;
325 vdev->virq_disabled = false;
327 for (i = 0; i < vdev->num_regions; i++)
328 vdev->region[i].ops->release(vdev, &vdev->region[i]);
330 vdev->num_regions = 0;
332 vdev->region = NULL; /* don't krealloc a freed pointer */
334 vfio_config_free(vdev);
336 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
337 if (!vdev->barmap[bar])
339 pci_iounmap(pdev, vdev->barmap[bar]);
340 pci_release_selected_regions(pdev, 1 << bar);
341 vdev->barmap[bar] = NULL;
344 list_for_each_entry_safe(dummy_res, tmp,
345 &vdev->dummy_resources_list, res_next) {
346 list_del(&dummy_res->res_next);
347 release_resource(&dummy_res->resource);
351 vdev->needs_reset = true;
354 * If we have saved state, restore it. If we can reset the device,
355 * even better. Resetting with current state seems better than
356 * nothing, but saving and restoring current state without reset
359 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
360 pr_info("%s: Couldn't reload %s saved state\n",
361 __func__, dev_name(&pdev->dev));
363 if (!vdev->reset_works)
366 pci_save_state(pdev);
370 * Disable INTx and MSI, presumably to avoid spurious interrupts
371 * during reset. Stolen from pci_reset_function()
373 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
376 * Try to reset the device. The success of this is dependent on
377 * being able to lock the device, which is not always possible.
379 if (vdev->reset_works && !pci_try_reset_function(pdev))
380 vdev->needs_reset = false;
382 pci_restore_state(pdev);
384 pci_disable_device(pdev);
386 vfio_pci_try_bus_reset(vdev);
388 if (!disable_idle_d3)
389 pci_set_power_state(pdev, PCI_D3hot);
392 static void vfio_pci_release(void *device_data)
394 struct vfio_pci_device *vdev = device_data;
396 mutex_lock(&driver_lock);
398 if (!(--vdev->refcnt)) {
399 vfio_spapr_pci_eeh_release(vdev->pdev);
400 vfio_pci_disable(vdev);
403 mutex_unlock(&driver_lock);
405 module_put(THIS_MODULE);
408 static int vfio_pci_open(void *device_data)
410 struct vfio_pci_device *vdev = device_data;
413 if (!try_module_get(THIS_MODULE))
416 mutex_lock(&driver_lock);
419 ret = vfio_pci_enable(vdev);
423 vfio_spapr_pci_eeh_open(vdev->pdev);
427 mutex_unlock(&driver_lock);
429 module_put(THIS_MODULE);
433 static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
435 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
438 if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) ||
439 vdev->nointx || vdev->pdev->is_virtfn)
442 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
445 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
449 pos = vdev->pdev->msi_cap;
451 pci_read_config_word(vdev->pdev,
452 pos + PCI_MSI_FLAGS, &flags);
453 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
455 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
459 pos = vdev->pdev->msix_cap;
461 pci_read_config_word(vdev->pdev,
462 pos + PCI_MSIX_FLAGS, &flags);
464 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
466 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
467 if (pci_is_pcie(vdev->pdev))
469 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
476 static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
482 struct vfio_pci_fill_info {
485 struct vfio_pci_dependent_device *devices;
488 static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
490 struct vfio_pci_fill_info *fill = data;
491 struct iommu_group *iommu_group;
493 if (fill->cur == fill->max)
494 return -EAGAIN; /* Something changed, try again */
496 iommu_group = iommu_group_get(&pdev->dev);
498 return -EPERM; /* Cannot reset non-isolated devices */
500 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
501 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
502 fill->devices[fill->cur].bus = pdev->bus->number;
503 fill->devices[fill->cur].devfn = pdev->devfn;
505 iommu_group_put(iommu_group);
509 struct vfio_pci_group_entry {
510 struct vfio_group *group;
514 struct vfio_pci_group_info {
516 struct vfio_pci_group_entry *groups;
519 static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
521 struct vfio_pci_group_info *info = data;
522 struct iommu_group *group;
525 group = iommu_group_get(&pdev->dev);
529 id = iommu_group_id(group);
531 for (i = 0; i < info->count; i++)
532 if (info->groups[i].id == id)
535 iommu_group_put(group);
537 return (i == info->count) ? -EINVAL : 0;
540 static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
542 for (; pdev; pdev = pdev->bus->self)
543 if (pdev->bus == slot->bus)
544 return (pdev->slot == slot);
548 struct vfio_pci_walk_info {
549 int (*fn)(struct pci_dev *, void *data);
551 struct pci_dev *pdev;
556 static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
558 struct vfio_pci_walk_info *walk = data;
560 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
561 walk->ret = walk->fn(pdev, walk->data);
566 static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
567 int (*fn)(struct pci_dev *,
568 void *data), void *data,
571 struct vfio_pci_walk_info walk = {
572 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
575 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
580 static int msix_mmappable_cap(struct vfio_pci_device *vdev,
581 struct vfio_info_cap *caps)
583 struct vfio_info_cap_header header = {
584 .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE,
588 return vfio_info_add_capability(caps, &header, sizeof(header));
591 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
592 unsigned int type, unsigned int subtype,
593 const struct vfio_pci_regops *ops,
594 size_t size, u32 flags, void *data)
596 struct vfio_pci_region *region;
598 region = krealloc(vdev->region,
599 (vdev->num_regions + 1) * sizeof(*region),
604 vdev->region = region;
605 vdev->region[vdev->num_regions].type = type;
606 vdev->region[vdev->num_regions].subtype = subtype;
607 vdev->region[vdev->num_regions].ops = ops;
608 vdev->region[vdev->num_regions].size = size;
609 vdev->region[vdev->num_regions].flags = flags;
610 vdev->region[vdev->num_regions].data = data;
617 static long vfio_pci_ioctl(void *device_data,
618 unsigned int cmd, unsigned long arg)
620 struct vfio_pci_device *vdev = device_data;
623 if (cmd == VFIO_DEVICE_GET_INFO) {
624 struct vfio_device_info info;
626 minsz = offsetofend(struct vfio_device_info, num_irqs);
628 if (copy_from_user(&info, (void __user *)arg, minsz))
631 if (info.argsz < minsz)
634 info.flags = VFIO_DEVICE_FLAGS_PCI;
636 if (vdev->reset_works)
637 info.flags |= VFIO_DEVICE_FLAGS_RESET;
639 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
640 info.num_irqs = VFIO_PCI_NUM_IRQS;
642 return copy_to_user((void __user *)arg, &info, minsz) ?
645 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
646 struct pci_dev *pdev = vdev->pdev;
647 struct vfio_region_info info;
648 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
651 minsz = offsetofend(struct vfio_region_info, offset);
653 if (copy_from_user(&info, (void __user *)arg, minsz))
656 if (info.argsz < minsz)
659 switch (info.index) {
660 case VFIO_PCI_CONFIG_REGION_INDEX:
661 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
662 info.size = pdev->cfg_size;
663 info.flags = VFIO_REGION_INFO_FLAG_READ |
664 VFIO_REGION_INFO_FLAG_WRITE;
666 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
667 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
668 info.size = pci_resource_len(pdev, info.index);
674 info.flags = VFIO_REGION_INFO_FLAG_READ |
675 VFIO_REGION_INFO_FLAG_WRITE;
676 if (vdev->bar_mmap_supported[info.index]) {
677 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
678 if (info.index == vdev->msix_bar) {
679 ret = msix_mmappable_cap(vdev, &caps);
686 case VFIO_PCI_ROM_REGION_INDEX:
691 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
694 /* Report the BAR size, not the ROM size */
695 info.size = pci_resource_len(pdev, info.index);
697 /* Shadow ROMs appear as PCI option ROMs */
698 if (pdev->resource[PCI_ROM_RESOURCE].flags &
699 IORESOURCE_ROM_SHADOW)
705 /* Is it really there? */
706 io = pci_map_rom(pdev, &size);
711 pci_unmap_rom(pdev, io);
713 info.flags = VFIO_REGION_INFO_FLAG_READ;
716 case VFIO_PCI_VGA_REGION_INDEX:
720 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
722 info.flags = VFIO_REGION_INFO_FLAG_READ |
723 VFIO_REGION_INFO_FLAG_WRITE;
728 struct vfio_region_info_cap_type cap_type = {
729 .header.id = VFIO_REGION_INFO_CAP_TYPE,
730 .header.version = 1 };
733 VFIO_PCI_NUM_REGIONS + vdev->num_regions)
735 info.index = array_index_nospec(info.index,
736 VFIO_PCI_NUM_REGIONS +
739 i = info.index - VFIO_PCI_NUM_REGIONS;
741 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
742 info.size = vdev->region[i].size;
743 info.flags = vdev->region[i].flags;
745 cap_type.type = vdev->region[i].type;
746 cap_type.subtype = vdev->region[i].subtype;
748 ret = vfio_info_add_capability(&caps, &cap_type.header,
757 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
758 if (info.argsz < sizeof(info) + caps.size) {
759 info.argsz = sizeof(info) + caps.size;
762 vfio_info_cap_shift(&caps, sizeof(info));
763 if (copy_to_user((void __user *)arg +
764 sizeof(info), caps.buf,
769 info.cap_offset = sizeof(info);
775 return copy_to_user((void __user *)arg, &info, minsz) ?
778 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
779 struct vfio_irq_info info;
781 minsz = offsetofend(struct vfio_irq_info, count);
783 if (copy_from_user(&info, (void __user *)arg, minsz))
786 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
789 switch (info.index) {
790 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
791 case VFIO_PCI_REQ_IRQ_INDEX:
793 case VFIO_PCI_ERR_IRQ_INDEX:
794 if (pci_is_pcie(vdev->pdev))
801 info.flags = VFIO_IRQ_INFO_EVENTFD;
803 info.count = vfio_pci_get_irq_count(vdev, info.index);
805 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
806 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
807 VFIO_IRQ_INFO_AUTOMASKED);
809 info.flags |= VFIO_IRQ_INFO_NORESIZE;
811 return copy_to_user((void __user *)arg, &info, minsz) ?
814 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
815 struct vfio_irq_set hdr;
818 size_t data_size = 0;
820 minsz = offsetofend(struct vfio_irq_set, count);
822 if (copy_from_user(&hdr, (void __user *)arg, minsz))
825 max = vfio_pci_get_irq_count(vdev, hdr.index);
827 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
828 VFIO_PCI_NUM_IRQS, &data_size);
833 data = memdup_user((void __user *)(arg + minsz),
836 return PTR_ERR(data);
839 mutex_lock(&vdev->igate);
841 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
842 hdr.start, hdr.count, data);
844 mutex_unlock(&vdev->igate);
849 } else if (cmd == VFIO_DEVICE_RESET) {
850 return vdev->reset_works ?
851 pci_try_reset_function(vdev->pdev) : -EINVAL;
853 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
854 struct vfio_pci_hot_reset_info hdr;
855 struct vfio_pci_fill_info fill = { 0 };
856 struct vfio_pci_dependent_device *devices = NULL;
860 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
862 if (copy_from_user(&hdr, (void __user *)arg, minsz))
865 if (hdr.argsz < minsz)
870 /* Can we do a slot or bus reset or neither? */
871 if (!pci_probe_reset_slot(vdev->pdev->slot))
873 else if (pci_probe_reset_bus(vdev->pdev->bus))
876 /* How many devices are affected? */
877 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
883 WARN_ON(!fill.max); /* Should always be at least one */
886 * If there's enough space, fill it now, otherwise return
887 * -ENOSPC and the number of devices affected.
889 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
891 hdr.count = fill.max;
892 goto reset_info_exit;
895 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
899 fill.devices = devices;
901 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
906 * If a device was removed between counting and filling,
907 * we may come up short of fill.max. If a device was
908 * added, we'll have a return of -EAGAIN above.
911 hdr.count = fill.cur;
914 if (copy_to_user((void __user *)arg, &hdr, minsz))
918 if (copy_to_user((void __user *)(arg + minsz), devices,
919 hdr.count * sizeof(*devices)))
926 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
927 struct vfio_pci_hot_reset hdr;
929 struct vfio_pci_group_entry *groups;
930 struct vfio_pci_group_info info;
932 int i, count = 0, ret = 0;
934 minsz = offsetofend(struct vfio_pci_hot_reset, count);
936 if (copy_from_user(&hdr, (void __user *)arg, minsz))
939 if (hdr.argsz < minsz || hdr.flags)
942 /* Can we do a slot or bus reset or neither? */
943 if (!pci_probe_reset_slot(vdev->pdev->slot))
945 else if (pci_probe_reset_bus(vdev->pdev->bus))
949 * We can't let userspace give us an arbitrarily large
950 * buffer to copy, so verify how many we think there
951 * could be. Note groups can have multiple devices so
952 * one group per device is the max.
954 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
960 /* Somewhere between 1 and count is OK */
961 if (!hdr.count || hdr.count > count)
964 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
965 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
966 if (!group_fds || !groups) {
972 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
973 hdr.count * sizeof(*group_fds))) {
980 * For each group_fd, get the group through the vfio external
981 * user interface and store the group and iommu ID. This
982 * ensures the group is held across the reset.
984 for (i = 0; i < hdr.count; i++) {
985 struct vfio_group *group;
986 struct fd f = fdget(group_fds[i]);
992 group = vfio_group_get_external_user(f.file);
995 ret = PTR_ERR(group);
999 groups[i].group = group;
1000 groups[i].id = vfio_external_user_iommu_id(group);
1005 /* release reference to groups on error */
1007 goto hot_reset_release;
1009 info.count = hdr.count;
1010 info.groups = groups;
1013 * Test whether all the affected devices are contained
1014 * by the set of groups provided by the user.
1016 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
1017 vfio_pci_validate_devs,
1020 /* User has access, do the reset */
1021 ret = pci_reset_bus(vdev->pdev);
1024 for (i--; i >= 0; i--)
1025 vfio_group_put_external_user(groups[i].group);
1029 } else if (cmd == VFIO_DEVICE_IOEVENTFD) {
1030 struct vfio_device_ioeventfd ioeventfd;
1033 minsz = offsetofend(struct vfio_device_ioeventfd, fd);
1035 if (copy_from_user(&ioeventfd, (void __user *)arg, minsz))
1038 if (ioeventfd.argsz < minsz)
1041 if (ioeventfd.flags & ~VFIO_DEVICE_IOEVENTFD_SIZE_MASK)
1044 count = ioeventfd.flags & VFIO_DEVICE_IOEVENTFD_SIZE_MASK;
1046 if (hweight8(count) != 1 || ioeventfd.fd < -1)
1049 return vfio_pci_ioeventfd(vdev, ioeventfd.offset,
1050 ioeventfd.data, count, ioeventfd.fd);
1056 static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
1057 size_t count, loff_t *ppos, bool iswrite)
1059 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1060 struct vfio_pci_device *vdev = device_data;
1062 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
1066 case VFIO_PCI_CONFIG_REGION_INDEX:
1067 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
1069 case VFIO_PCI_ROM_REGION_INDEX:
1072 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
1074 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1075 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
1077 case VFIO_PCI_VGA_REGION_INDEX:
1078 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
1080 index -= VFIO_PCI_NUM_REGIONS;
1081 return vdev->region[index].ops->rw(vdev, buf,
1082 count, ppos, iswrite);
1088 static ssize_t vfio_pci_read(void *device_data, char __user *buf,
1089 size_t count, loff_t *ppos)
1094 return vfio_pci_rw(device_data, buf, count, ppos, false);
1097 static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
1098 size_t count, loff_t *ppos)
1103 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
1106 static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
1108 struct vfio_pci_device *vdev = device_data;
1109 struct pci_dev *pdev = vdev->pdev;
1111 u64 phys_len, req_len, pgoff, req_start;
1114 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1116 if (vma->vm_end < vma->vm_start)
1118 if ((vma->vm_flags & VM_SHARED) == 0)
1120 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1122 if (!vdev->bar_mmap_supported[index])
1125 phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
1126 req_len = vma->vm_end - vma->vm_start;
1127 pgoff = vma->vm_pgoff &
1128 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1129 req_start = pgoff << PAGE_SHIFT;
1131 if (req_start + req_len > phys_len)
1135 * Even though we don't make use of the barmap for the mmap,
1136 * we need to request the region and the barmap tracks that.
1138 if (!vdev->barmap[index]) {
1139 ret = pci_request_selected_regions(pdev,
1140 1 << index, "vfio-pci");
1144 vdev->barmap[index] = pci_iomap(pdev, index, 0);
1145 if (!vdev->barmap[index]) {
1146 pci_release_selected_regions(pdev, 1 << index);
1151 vma->vm_private_data = vdev;
1152 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1153 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
1155 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1156 req_len, vma->vm_page_prot);
1159 static void vfio_pci_request(void *device_data, unsigned int count)
1161 struct vfio_pci_device *vdev = device_data;
1163 mutex_lock(&vdev->igate);
1165 if (vdev->req_trigger) {
1167 dev_notice_ratelimited(&vdev->pdev->dev,
1168 "Relaying device request to user (#%u)\n",
1170 eventfd_signal(vdev->req_trigger, 1);
1171 } else if (count == 0) {
1172 dev_warn(&vdev->pdev->dev,
1173 "No device request channel registered, blocked until released by user\n");
1176 mutex_unlock(&vdev->igate);
1179 static const struct vfio_device_ops vfio_pci_ops = {
1181 .open = vfio_pci_open,
1182 .release = vfio_pci_release,
1183 .ioctl = vfio_pci_ioctl,
1184 .read = vfio_pci_read,
1185 .write = vfio_pci_write,
1186 .mmap = vfio_pci_mmap,
1187 .request = vfio_pci_request,
1190 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1192 struct vfio_pci_device *vdev;
1193 struct iommu_group *group;
1196 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1200 * Prevent binding to PFs with VFs enabled, this too easily allows
1201 * userspace instance with VFs and PFs from the same device, which
1202 * cannot work. Disabling SR-IOV here would initiate removing the
1203 * VFs, which would unbind the driver, which is prone to blocking
1204 * if that VF is also in use by vfio-pci. Just reject these PFs
1205 * and let the user sort it out.
1207 if (pci_num_vf(pdev)) {
1208 pci_warn(pdev, "Cannot bind to PF with SR-IOV enabled\n");
1212 group = vfio_iommu_group_get(&pdev->dev);
1216 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1218 vfio_iommu_group_put(group, &pdev->dev);
1223 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1224 mutex_init(&vdev->igate);
1225 spin_lock_init(&vdev->irqlock);
1226 mutex_init(&vdev->ioeventfds_lock);
1227 INIT_LIST_HEAD(&vdev->ioeventfds_list);
1229 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1231 vfio_iommu_group_put(group, &pdev->dev);
1236 if (vfio_pci_is_vga(pdev)) {
1237 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1238 vga_set_legacy_decoding(pdev,
1239 vfio_pci_set_vga_decode(vdev, false));
1242 if (!disable_idle_d3) {
1244 * pci-core sets the device power state to an unknown value at
1245 * bootup and after being removed from a driver. The only
1246 * transition it allows from this unknown state is to D0, which
1247 * typically happens when a driver calls pci_enable_device().
1248 * We're not ready to enable the device yet, but we do want to
1249 * be able to get to D3. Therefore first do a D0 transition
1250 * before going to D3.
1252 pci_set_power_state(pdev, PCI_D0);
1253 pci_set_power_state(pdev, PCI_D3hot);
1259 static void vfio_pci_remove(struct pci_dev *pdev)
1261 struct vfio_pci_device *vdev;
1263 vdev = vfio_del_group_dev(&pdev->dev);
1267 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
1268 kfree(vdev->region);
1269 mutex_destroy(&vdev->ioeventfds_lock);
1272 if (vfio_pci_is_vga(pdev)) {
1273 vga_client_register(pdev, NULL, NULL, NULL);
1274 vga_set_legacy_decoding(pdev,
1275 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1276 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
1279 if (!disable_idle_d3)
1280 pci_set_power_state(pdev, PCI_D0);
1283 static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1284 pci_channel_state_t state)
1286 struct vfio_pci_device *vdev;
1287 struct vfio_device *device;
1289 device = vfio_device_get_from_dev(&pdev->dev);
1291 return PCI_ERS_RESULT_DISCONNECT;
1293 vdev = vfio_device_data(device);
1295 vfio_device_put(device);
1296 return PCI_ERS_RESULT_DISCONNECT;
1299 mutex_lock(&vdev->igate);
1301 if (vdev->err_trigger)
1302 eventfd_signal(vdev->err_trigger, 1);
1304 mutex_unlock(&vdev->igate);
1306 vfio_device_put(device);
1308 return PCI_ERS_RESULT_CAN_RECOVER;
1311 static const struct pci_error_handlers vfio_err_handlers = {
1312 .error_detected = vfio_pci_aer_err_detected,
1315 static struct pci_driver vfio_pci_driver = {
1317 .id_table = NULL, /* only dynamic ids */
1318 .probe = vfio_pci_probe,
1319 .remove = vfio_pci_remove,
1320 .err_handler = &vfio_err_handlers,
1323 struct vfio_devices {
1324 struct vfio_device **devices;
1329 static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
1331 struct vfio_devices *devs = data;
1332 struct vfio_device *device;
1334 if (devs->cur_index == devs->max_index)
1337 device = vfio_device_get_from_dev(&pdev->dev);
1341 if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1342 vfio_device_put(device);
1346 devs->devices[devs->cur_index++] = device;
1351 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1352 * this device that are needs_reset and all of the affected devices are unused
1353 * (!refcnt). Callers are required to hold driver_lock when calling this to
1354 * prevent device opens and concurrent bus reset attempts. We prevent device
1355 * unbinds by acquiring and holding a reference to the vfio_device.
1357 * NB: vfio-core considers a group to be viable even if some devices are
1358 * bound to drivers like pci-stub or pcieport. Here we require all devices
1359 * to be bound to vfio_pci since that's the only way we can be sure they
1362 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1364 struct vfio_devices devs = { .cur_index = 0 };
1365 int i = 0, ret = -EINVAL;
1366 bool needs_reset = false, slot = false;
1367 struct vfio_pci_device *tmp;
1369 if (!pci_probe_reset_slot(vdev->pdev->slot))
1371 else if (pci_probe_reset_bus(vdev->pdev->bus))
1374 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1379 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1383 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
1384 vfio_pci_get_devs, &devs, slot))
1387 for (i = 0; i < devs.cur_index; i++) {
1388 tmp = vfio_device_data(devs.devices[i]);
1389 if (tmp->needs_reset)
1396 ret = pci_reset_bus(vdev->pdev);
1399 for (i = 0; i < devs.cur_index; i++) {
1400 tmp = vfio_device_data(devs.devices[i]);
1402 tmp->needs_reset = false;
1404 if (!tmp->refcnt && !disable_idle_d3)
1405 pci_set_power_state(tmp->pdev, PCI_D3hot);
1407 vfio_device_put(devs.devices[i]);
1410 kfree(devs.devices);
1413 static void __exit vfio_pci_cleanup(void)
1415 pci_unregister_driver(&vfio_pci_driver);
1416 vfio_pci_uninit_perm_bits();
1419 static void __init vfio_pci_fill_ids(void)
1424 /* no ids passed actually */
1428 /* add ids specified in the module parameter */
1430 while ((id = strsep(&p, ","))) {
1431 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1432 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1438 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1439 &vendor, &device, &subvendor, &subdevice,
1440 &class, &class_mask);
1443 pr_warn("invalid id string \"%s\"\n", id);
1447 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1448 subvendor, subdevice, class, class_mask, 0);
1450 pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
1451 vendor, device, subvendor, subdevice,
1452 class, class_mask, rc);
1454 pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
1455 vendor, device, subvendor, subdevice,
1460 static int __init vfio_pci_init(void)
1464 /* Allocate shared config space permision data used by all devices */
1465 ret = vfio_pci_init_perm_bits();
1469 /* Register and scan for devices */
1470 ret = pci_register_driver(&vfio_pci_driver);
1474 vfio_pci_fill_ids();
1479 vfio_pci_uninit_perm_bits();
1483 module_init(vfio_pci_init);
1484 module_exit(vfio_pci_cleanup);
1486 MODULE_VERSION(DRIVER_VERSION);
1487 MODULE_LICENSE("GPL v2");
1488 MODULE_AUTHOR(DRIVER_AUTHOR);
1489 MODULE_DESCRIPTION(DRIVER_DESC);