1ff53edf6SMax Gurtovoy // SPDX-License-Identifier: GPL-2.0-only
2ff53edf6SMax Gurtovoy /*
3ff53edf6SMax Gurtovoy * Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved
4ff53edf6SMax Gurtovoy *
5ff53edf6SMax Gurtovoy * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6ff53edf6SMax Gurtovoy * Author: Alex Williamson <alex.williamson@redhat.com>
7ff53edf6SMax Gurtovoy *
8ff53edf6SMax Gurtovoy * Derived from original vfio:
9ff53edf6SMax Gurtovoy * Copyright 2010 Cisco Systems, Inc. All rights reserved.
10ff53edf6SMax Gurtovoy * Author: Tom Lyon, pugs@cisco.com
11ff53edf6SMax Gurtovoy */
12ff53edf6SMax Gurtovoy
13ff53edf6SMax Gurtovoy #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14ff53edf6SMax Gurtovoy
15ff53edf6SMax Gurtovoy #include <linux/device.h>
16ff53edf6SMax Gurtovoy #include <linux/eventfd.h>
17ff53edf6SMax Gurtovoy #include <linux/file.h>
18ff53edf6SMax Gurtovoy #include <linux/interrupt.h>
19ff53edf6SMax Gurtovoy #include <linux/iommu.h>
20ff53edf6SMax Gurtovoy #include <linux/module.h>
21ff53edf6SMax Gurtovoy #include <linux/mutex.h>
22ff53edf6SMax Gurtovoy #include <linux/notifier.h>
23ff53edf6SMax Gurtovoy #include <linux/pm_runtime.h>
24ff53edf6SMax Gurtovoy #include <linux/slab.h>
25ff53edf6SMax Gurtovoy #include <linux/types.h>
26ff53edf6SMax Gurtovoy #include <linux/uaccess.h>
27ff53edf6SMax Gurtovoy
28e34a0425SJason Gunthorpe #include "vfio_pci_priv.h"
29ff53edf6SMax Gurtovoy
30ff53edf6SMax Gurtovoy #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
31ff53edf6SMax Gurtovoy #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
32ff53edf6SMax Gurtovoy
33ff53edf6SMax Gurtovoy static char ids[1024] __initdata;
34ff53edf6SMax Gurtovoy module_param_string(ids, ids, sizeof(ids), 0);
35ff53edf6SMax Gurtovoy 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");
36ff53edf6SMax Gurtovoy
37c61302aaSYishai Hadas static bool nointxmask;
38c61302aaSYishai Hadas module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
39c61302aaSYishai Hadas MODULE_PARM_DESC(nointxmask,
40c61302aaSYishai Hadas "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.");
41c61302aaSYishai Hadas
42c61302aaSYishai Hadas #ifdef CONFIG_VFIO_PCI_VGA
43c61302aaSYishai Hadas static bool disable_vga;
44c61302aaSYishai Hadas module_param(disable_vga, bool, S_IRUGO);
45c61302aaSYishai Hadas MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
46c61302aaSYishai Hadas #endif
47c61302aaSYishai Hadas
48c61302aaSYishai Hadas static bool disable_idle_d3;
49c61302aaSYishai Hadas module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
50c61302aaSYishai Hadas MODULE_PARM_DESC(disable_idle_d3,
51c61302aaSYishai Hadas "Disable using the PCI D3 low power state for idle, unused devices");
52c61302aaSYishai Hadas
53ff53edf6SMax Gurtovoy static bool enable_sriov;
54ff53edf6SMax Gurtovoy #ifdef CONFIG_PCI_IOV
55ff53edf6SMax Gurtovoy module_param(enable_sriov, bool, 0644);
56ff53edf6SMax Gurtovoy MODULE_PARM_DESC(enable_sriov, "Enable support for SR-IOV configuration. Enabling SR-IOV on a PF typically requires support of the userspace PF driver, enabling VFs without such support may result in non-functional VFs or PF.");
57ff53edf6SMax Gurtovoy #endif
58ff53edf6SMax Gurtovoy
59ff53edf6SMax Gurtovoy static bool disable_denylist;
60ff53edf6SMax Gurtovoy module_param(disable_denylist, bool, 0444);
61ff53edf6SMax Gurtovoy MODULE_PARM_DESC(disable_denylist, "Disable use of device denylist. Disabling the denylist allows binding to devices with known errata that may lead to exploitable stability or security issues when accessed by untrusted users.");
62ff53edf6SMax Gurtovoy
vfio_pci_dev_in_denylist(struct pci_dev * pdev)63ff53edf6SMax Gurtovoy static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
64ff53edf6SMax Gurtovoy {
65ff53edf6SMax Gurtovoy switch (pdev->vendor) {
66ff53edf6SMax Gurtovoy case PCI_VENDOR_ID_INTEL:
67ff53edf6SMax Gurtovoy switch (pdev->device) {
68ff53edf6SMax Gurtovoy case PCI_DEVICE_ID_INTEL_QAT_C3XXX:
69ff53edf6SMax Gurtovoy case PCI_DEVICE_ID_INTEL_QAT_C3XXX_VF:
70ff53edf6SMax Gurtovoy case PCI_DEVICE_ID_INTEL_QAT_C62X:
71ff53edf6SMax Gurtovoy case PCI_DEVICE_ID_INTEL_QAT_C62X_VF:
72ff53edf6SMax Gurtovoy case PCI_DEVICE_ID_INTEL_QAT_DH895XCC:
73ff53edf6SMax Gurtovoy case PCI_DEVICE_ID_INTEL_QAT_DH895XCC_VF:
74*c5164535SArjan van de Ven case PCI_DEVICE_ID_INTEL_DSA_SPR0:
75*c5164535SArjan van de Ven case PCI_DEVICE_ID_INTEL_IAX_SPR0:
76ff53edf6SMax Gurtovoy return true;
77ff53edf6SMax Gurtovoy default:
78ff53edf6SMax Gurtovoy return false;
79ff53edf6SMax Gurtovoy }
80ff53edf6SMax Gurtovoy }
81ff53edf6SMax Gurtovoy
82ff53edf6SMax Gurtovoy return false;
83ff53edf6SMax Gurtovoy }
84ff53edf6SMax Gurtovoy
vfio_pci_is_denylisted(struct pci_dev * pdev)85ff53edf6SMax Gurtovoy static bool vfio_pci_is_denylisted(struct pci_dev *pdev)
86ff53edf6SMax Gurtovoy {
87ff53edf6SMax Gurtovoy if (!vfio_pci_dev_in_denylist(pdev))
88ff53edf6SMax Gurtovoy return false;
89ff53edf6SMax Gurtovoy
90ff53edf6SMax Gurtovoy if (disable_denylist) {
91ff53edf6SMax Gurtovoy pci_warn(pdev,
92ff53edf6SMax Gurtovoy "device denylist disabled - allowing device %04x:%04x.\n",
93ff53edf6SMax Gurtovoy pdev->vendor, pdev->device);
94ff53edf6SMax Gurtovoy return false;
95ff53edf6SMax Gurtovoy }
96ff53edf6SMax Gurtovoy
97ff53edf6SMax Gurtovoy pci_warn(pdev, "%04x:%04x exists in vfio-pci device denylist, driver probing disallowed.\n",
98ff53edf6SMax Gurtovoy pdev->vendor, pdev->device);
99ff53edf6SMax Gurtovoy
100ff53edf6SMax Gurtovoy return true;
101ff53edf6SMax Gurtovoy }
102ff53edf6SMax Gurtovoy
vfio_pci_open_device(struct vfio_device * core_vdev)1032fb89f56SMax Gurtovoy static int vfio_pci_open_device(struct vfio_device *core_vdev)
1042fb89f56SMax Gurtovoy {
1052fb89f56SMax Gurtovoy struct vfio_pci_core_device *vdev =
1062fb89f56SMax Gurtovoy container_of(core_vdev, struct vfio_pci_core_device, vdev);
1072fb89f56SMax Gurtovoy struct pci_dev *pdev = vdev->pdev;
1082fb89f56SMax Gurtovoy int ret;
1092fb89f56SMax Gurtovoy
1102fb89f56SMax Gurtovoy ret = vfio_pci_core_enable(vdev);
1112fb89f56SMax Gurtovoy if (ret)
1122fb89f56SMax Gurtovoy return ret;
1132fb89f56SMax Gurtovoy
1142fb89f56SMax Gurtovoy if (vfio_pci_is_vga(pdev) &&
1152fb89f56SMax Gurtovoy pdev->vendor == PCI_VENDOR_ID_INTEL &&
1162fb89f56SMax Gurtovoy IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
1172fb89f56SMax Gurtovoy ret = vfio_pci_igd_init(vdev);
1182fb89f56SMax Gurtovoy if (ret && ret != -ENODEV) {
1192fb89f56SMax Gurtovoy pci_warn(pdev, "Failed to setup Intel IGD regions\n");
1202fb89f56SMax Gurtovoy vfio_pci_core_disable(vdev);
1212fb89f56SMax Gurtovoy return ret;
1222fb89f56SMax Gurtovoy }
1232fb89f56SMax Gurtovoy }
1242fb89f56SMax Gurtovoy
1252fb89f56SMax Gurtovoy vfio_pci_core_finish_enable(vdev);
1262fb89f56SMax Gurtovoy
1272fb89f56SMax Gurtovoy return 0;
1282fb89f56SMax Gurtovoy }
1292fb89f56SMax Gurtovoy
130ff53edf6SMax Gurtovoy static const struct vfio_device_ops vfio_pci_ops = {
131ff53edf6SMax Gurtovoy .name = "vfio-pci",
13263d7c779SYi Liu .init = vfio_pci_core_init_dev,
13363d7c779SYi Liu .release = vfio_pci_core_release_dev,
1342fb89f56SMax Gurtovoy .open_device = vfio_pci_open_device,
135ff53edf6SMax Gurtovoy .close_device = vfio_pci_core_close_device,
136ff53edf6SMax Gurtovoy .ioctl = vfio_pci_core_ioctl,
137445ad495SJason Gunthorpe .device_feature = vfio_pci_core_ioctl_feature,
138ff53edf6SMax Gurtovoy .read = vfio_pci_core_read,
139ff53edf6SMax Gurtovoy .write = vfio_pci_core_write,
140ff53edf6SMax Gurtovoy .mmap = vfio_pci_core_mmap,
141ff53edf6SMax Gurtovoy .request = vfio_pci_core_request,
142ff53edf6SMax Gurtovoy .match = vfio_pci_core_match,
143a4d1f91dSJason Gunthorpe .bind_iommufd = vfio_iommufd_physical_bind,
144a4d1f91dSJason Gunthorpe .unbind_iommufd = vfio_iommufd_physical_unbind,
145a4d1f91dSJason Gunthorpe .attach_ioas = vfio_iommufd_physical_attach_ioas,
1469048c734SYi Liu .detach_ioas = vfio_iommufd_physical_detach_ioas,
147ff53edf6SMax Gurtovoy };
148ff53edf6SMax Gurtovoy
vfio_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)149ff53edf6SMax Gurtovoy static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
150ff53edf6SMax Gurtovoy {
151ff53edf6SMax Gurtovoy struct vfio_pci_core_device *vdev;
152ff53edf6SMax Gurtovoy int ret;
153ff53edf6SMax Gurtovoy
154ff53edf6SMax Gurtovoy if (vfio_pci_is_denylisted(pdev))
155ff53edf6SMax Gurtovoy return -EINVAL;
156ff53edf6SMax Gurtovoy
15763d7c779SYi Liu vdev = vfio_alloc_device(vfio_pci_core_device, vdev, &pdev->dev,
15863d7c779SYi Liu &vfio_pci_ops);
15963d7c779SYi Liu if (IS_ERR(vdev))
16063d7c779SYi Liu return PTR_ERR(vdev);
161ff53edf6SMax Gurtovoy
16291be0bd6SJason Gunthorpe dev_set_drvdata(&pdev->dev, vdev);
163ff53edf6SMax Gurtovoy ret = vfio_pci_core_register_device(vdev);
164ff53edf6SMax Gurtovoy if (ret)
16563d7c779SYi Liu goto out_put_vdev;
166ff53edf6SMax Gurtovoy return 0;
167ff53edf6SMax Gurtovoy
16863d7c779SYi Liu out_put_vdev:
16963d7c779SYi Liu vfio_put_device(&vdev->vdev);
170ff53edf6SMax Gurtovoy return ret;
171ff53edf6SMax Gurtovoy }
172ff53edf6SMax Gurtovoy
vfio_pci_remove(struct pci_dev * pdev)173ff53edf6SMax Gurtovoy static void vfio_pci_remove(struct pci_dev *pdev)
174ff53edf6SMax Gurtovoy {
175ff53edf6SMax Gurtovoy struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev);
176ff53edf6SMax Gurtovoy
177ff53edf6SMax Gurtovoy vfio_pci_core_unregister_device(vdev);
17863d7c779SYi Liu vfio_put_device(&vdev->vdev);
179ff53edf6SMax Gurtovoy }
180ff53edf6SMax Gurtovoy
vfio_pci_sriov_configure(struct pci_dev * pdev,int nr_virtfn)181ff53edf6SMax Gurtovoy static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn)
182ff53edf6SMax Gurtovoy {
183ff806cbdSJason Gunthorpe struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev);
184ff806cbdSJason Gunthorpe
185ff53edf6SMax Gurtovoy if (!enable_sriov)
186ff53edf6SMax Gurtovoy return -ENOENT;
187ff53edf6SMax Gurtovoy
188ff806cbdSJason Gunthorpe return vfio_pci_core_sriov_configure(vdev, nr_virtfn);
189ff53edf6SMax Gurtovoy }
190ff53edf6SMax Gurtovoy
191cc6711b0SMax Gurtovoy static const struct pci_device_id vfio_pci_table[] = {
192cc6711b0SMax Gurtovoy { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_ANY_ID, PCI_ANY_ID) }, /* match all by default */
193cc6711b0SMax Gurtovoy {}
194cc6711b0SMax Gurtovoy };
195cc6711b0SMax Gurtovoy
196cc6711b0SMax Gurtovoy MODULE_DEVICE_TABLE(pci, vfio_pci_table);
197cc6711b0SMax Gurtovoy
198ff53edf6SMax Gurtovoy static struct pci_driver vfio_pci_driver = {
199ff53edf6SMax Gurtovoy .name = "vfio-pci",
200cc6711b0SMax Gurtovoy .id_table = vfio_pci_table,
201ff53edf6SMax Gurtovoy .probe = vfio_pci_probe,
202ff53edf6SMax Gurtovoy .remove = vfio_pci_remove,
203ff53edf6SMax Gurtovoy .sriov_configure = vfio_pci_sriov_configure,
204ff53edf6SMax Gurtovoy .err_handler = &vfio_pci_core_err_handlers,
20570693f47SLu Baolu .driver_managed_dma = true,
206ff53edf6SMax Gurtovoy };
207ff53edf6SMax Gurtovoy
vfio_pci_fill_ids(void)208ff53edf6SMax Gurtovoy static void __init vfio_pci_fill_ids(void)
209ff53edf6SMax Gurtovoy {
210ff53edf6SMax Gurtovoy char *p, *id;
211ff53edf6SMax Gurtovoy int rc;
212ff53edf6SMax Gurtovoy
213ff53edf6SMax Gurtovoy /* no ids passed actually */
214ff53edf6SMax Gurtovoy if (ids[0] == '\0')
215ff53edf6SMax Gurtovoy return;
216ff53edf6SMax Gurtovoy
217ff53edf6SMax Gurtovoy /* add ids specified in the module parameter */
218ff53edf6SMax Gurtovoy p = ids;
219ff53edf6SMax Gurtovoy while ((id = strsep(&p, ","))) {
220ff53edf6SMax Gurtovoy unsigned int vendor, device, subvendor = PCI_ANY_ID,
221ff53edf6SMax Gurtovoy subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
222ff53edf6SMax Gurtovoy int fields;
223ff53edf6SMax Gurtovoy
224ff53edf6SMax Gurtovoy if (!strlen(id))
225ff53edf6SMax Gurtovoy continue;
226ff53edf6SMax Gurtovoy
227ff53edf6SMax Gurtovoy fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
228ff53edf6SMax Gurtovoy &vendor, &device, &subvendor, &subdevice,
229ff53edf6SMax Gurtovoy &class, &class_mask);
230ff53edf6SMax Gurtovoy
231ff53edf6SMax Gurtovoy if (fields < 2) {
232ff53edf6SMax Gurtovoy pr_warn("invalid id string \"%s\"\n", id);
233ff53edf6SMax Gurtovoy continue;
234ff53edf6SMax Gurtovoy }
235ff53edf6SMax Gurtovoy
236ff53edf6SMax Gurtovoy rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
237ff53edf6SMax Gurtovoy subvendor, subdevice, class, class_mask, 0);
238ff53edf6SMax Gurtovoy if (rc)
239ff53edf6SMax Gurtovoy pr_warn("failed to add dynamic id [%04x:%04x[%04x:%04x]] class %#08x/%08x (%d)\n",
240ff53edf6SMax Gurtovoy vendor, device, subvendor, subdevice,
241ff53edf6SMax Gurtovoy class, class_mask, rc);
242ff53edf6SMax Gurtovoy else
243ff53edf6SMax Gurtovoy pr_info("add [%04x:%04x[%04x:%04x]] class %#08x/%08x\n",
244ff53edf6SMax Gurtovoy vendor, device, subvendor, subdevice,
245ff53edf6SMax Gurtovoy class, class_mask);
246ff53edf6SMax Gurtovoy }
247ff53edf6SMax Gurtovoy }
248ff53edf6SMax Gurtovoy
vfio_pci_init(void)249ff53edf6SMax Gurtovoy static int __init vfio_pci_init(void)
250ff53edf6SMax Gurtovoy {
251ff53edf6SMax Gurtovoy int ret;
252c61302aaSYishai Hadas bool is_disable_vga = true;
253c61302aaSYishai Hadas
254c61302aaSYishai Hadas #ifdef CONFIG_VFIO_PCI_VGA
255c61302aaSYishai Hadas is_disable_vga = disable_vga;
256c61302aaSYishai Hadas #endif
257c61302aaSYishai Hadas
258c61302aaSYishai Hadas vfio_pci_core_set_params(nointxmask, is_disable_vga, disable_idle_d3);
259ff53edf6SMax Gurtovoy
260ff53edf6SMax Gurtovoy /* Register and scan for devices */
261ff53edf6SMax Gurtovoy ret = pci_register_driver(&vfio_pci_driver);
262ff53edf6SMax Gurtovoy if (ret)
2637fa005caSMax Gurtovoy return ret;
264ff53edf6SMax Gurtovoy
265ff53edf6SMax Gurtovoy vfio_pci_fill_ids();
266ff53edf6SMax Gurtovoy
267ff53edf6SMax Gurtovoy if (disable_denylist)
268ff53edf6SMax Gurtovoy pr_warn("device denylist disabled.\n");
269ff53edf6SMax Gurtovoy
270ff53edf6SMax Gurtovoy return 0;
271ff53edf6SMax Gurtovoy }
272ff53edf6SMax Gurtovoy module_init(vfio_pci_init);
273ff53edf6SMax Gurtovoy
vfio_pci_cleanup(void)274ff53edf6SMax Gurtovoy static void __exit vfio_pci_cleanup(void)
275ff53edf6SMax Gurtovoy {
276ff53edf6SMax Gurtovoy pci_unregister_driver(&vfio_pci_driver);
277ff53edf6SMax Gurtovoy }
278ff53edf6SMax Gurtovoy module_exit(vfio_pci_cleanup);
279ff53edf6SMax Gurtovoy
280ff53edf6SMax Gurtovoy MODULE_LICENSE("GPL v2");
281ff53edf6SMax Gurtovoy MODULE_AUTHOR(DRIVER_AUTHOR);
282ff53edf6SMax Gurtovoy MODULE_DESCRIPTION(DRIVER_DESC);
283