vfio_pci.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved
  4. *
  5. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  6. * Author: Alex Williamson <alex.williamson@redhat.com>
  7. *
  8. * Derived from original vfio:
  9. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  10. * Author: Tom Lyon, pugs@cisco.com
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/device.h>
  14. #include <linux/eventfd.h>
  15. #include <linux/file.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iommu.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/notifier.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/uaccess.h>
  25. #include "vfio_pci_priv.h"
  26. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  27. #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
  28. static char ids[1024] __initdata;
  29. module_param_string(ids, ids, sizeof(ids), 0);
  30. 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");
  31. static bool nointxmask;
  32. module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
  33. MODULE_PARM_DESC(nointxmask,
  34. "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.");
  35. #ifdef CONFIG_VFIO_PCI_VGA
  36. static bool disable_vga;
  37. module_param(disable_vga, bool, S_IRUGO);
  38. MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
  39. #endif
  40. static bool disable_idle_d3;
  41. module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
  42. MODULE_PARM_DESC(disable_idle_d3,
  43. "Disable using the PCI D3 low power state for idle, unused devices");
  44. static bool enable_sriov;
  45. #ifdef CONFIG_PCI_IOV
  46. module_param(enable_sriov, bool, 0644);
  47. 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.");
  48. #endif
  49. static bool disable_denylist;
  50. module_param(disable_denylist, bool, 0444);
  51. 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.");
  52. static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
  53. {
  54. switch (pdev->vendor) {
  55. case PCI_VENDOR_ID_INTEL:
  56. switch (pdev->device) {
  57. case PCI_DEVICE_ID_INTEL_QAT_C3XXX:
  58. case PCI_DEVICE_ID_INTEL_QAT_C3XXX_VF:
  59. case PCI_DEVICE_ID_INTEL_QAT_C62X:
  60. case PCI_DEVICE_ID_INTEL_QAT_C62X_VF:
  61. case PCI_DEVICE_ID_INTEL_QAT_DH895XCC:
  62. case PCI_DEVICE_ID_INTEL_QAT_DH895XCC_VF:
  63. case PCI_DEVICE_ID_INTEL_DSA_SPR0:
  64. case PCI_DEVICE_ID_INTEL_IAX_SPR0:
  65. return true;
  66. default:
  67. return false;
  68. }
  69. }
  70. return false;
  71. }
  72. static bool vfio_pci_is_denylisted(struct pci_dev *pdev)
  73. {
  74. if (!vfio_pci_dev_in_denylist(pdev))
  75. return false;
  76. if (disable_denylist) {
  77. pci_warn(pdev,
  78. "device denylist disabled - allowing device %04x:%04x.\n",
  79. pdev->vendor, pdev->device);
  80. return false;
  81. }
  82. pci_warn(pdev, "%04x:%04x exists in vfio-pci device denylist, driver probing disallowed.\n",
  83. pdev->vendor, pdev->device);
  84. return true;
  85. }
  86. static int vfio_pci_open_device(struct vfio_device *core_vdev)
  87. {
  88. struct vfio_pci_core_device *vdev =
  89. container_of(core_vdev, struct vfio_pci_core_device, vdev);
  90. struct pci_dev *pdev = vdev->pdev;
  91. int ret;
  92. ret = vfio_pci_core_enable(vdev);
  93. if (ret)
  94. return ret;
  95. if (vfio_pci_is_vga(pdev) &&
  96. pdev->vendor == PCI_VENDOR_ID_INTEL &&
  97. IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
  98. ret = vfio_pci_igd_init(vdev);
  99. if (ret && ret != -ENODEV) {
  100. pci_warn(pdev, "Failed to setup Intel IGD regions\n");
  101. vfio_pci_core_disable(vdev);
  102. return ret;
  103. }
  104. }
  105. vfio_pci_core_finish_enable(vdev);
  106. return 0;
  107. }
  108. static const struct vfio_device_ops vfio_pci_ops = {
  109. .name = "vfio-pci",
  110. .init = vfio_pci_core_init_dev,
  111. .release = vfio_pci_core_release_dev,
  112. .open_device = vfio_pci_open_device,
  113. .close_device = vfio_pci_core_close_device,
  114. .ioctl = vfio_pci_core_ioctl,
  115. .device_feature = vfio_pci_core_ioctl_feature,
  116. .read = vfio_pci_core_read,
  117. .write = vfio_pci_core_write,
  118. .mmap = vfio_pci_core_mmap,
  119. .request = vfio_pci_core_request,
  120. .match = vfio_pci_core_match,
  121. .bind_iommufd = vfio_iommufd_physical_bind,
  122. .unbind_iommufd = vfio_iommufd_physical_unbind,
  123. .attach_ioas = vfio_iommufd_physical_attach_ioas,
  124. .detach_ioas = vfio_iommufd_physical_detach_ioas,
  125. };
  126. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  127. {
  128. struct vfio_pci_core_device *vdev;
  129. int ret;
  130. if (vfio_pci_is_denylisted(pdev))
  131. return -EINVAL;
  132. vdev = vfio_alloc_device(vfio_pci_core_device, vdev, &pdev->dev,
  133. &vfio_pci_ops);
  134. if (IS_ERR(vdev))
  135. return PTR_ERR(vdev);
  136. dev_set_drvdata(&pdev->dev, vdev);
  137. ret = vfio_pci_core_register_device(vdev);
  138. if (ret)
  139. goto out_put_vdev;
  140. return 0;
  141. out_put_vdev:
  142. vfio_put_device(&vdev->vdev);
  143. return ret;
  144. }
  145. static void vfio_pci_remove(struct pci_dev *pdev)
  146. {
  147. struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev);
  148. vfio_pci_core_unregister_device(vdev);
  149. vfio_put_device(&vdev->vdev);
  150. }
  151. static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn)
  152. {
  153. struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev);
  154. if (!enable_sriov)
  155. return -ENOENT;
  156. return vfio_pci_core_sriov_configure(vdev, nr_virtfn);
  157. }
  158. static const struct pci_device_id vfio_pci_table[] = {
  159. { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_ANY_ID, PCI_ANY_ID) }, /* match all by default */
  160. {}
  161. };
  162. MODULE_DEVICE_TABLE(pci, vfio_pci_table);
  163. static struct pci_driver vfio_pci_driver = {
  164. .name = "vfio-pci",
  165. .id_table = vfio_pci_table,
  166. .probe = vfio_pci_probe,
  167. .remove = vfio_pci_remove,
  168. .sriov_configure = vfio_pci_sriov_configure,
  169. .err_handler = &vfio_pci_core_err_handlers,
  170. .driver_managed_dma = true,
  171. };
  172. static void __init vfio_pci_fill_ids(void)
  173. {
  174. char *p, *id;
  175. int rc;
  176. /* no ids passed actually */
  177. if (ids[0] == '\0')
  178. return;
  179. /* add ids specified in the module parameter */
  180. p = ids;
  181. while ((id = strsep(&p, ","))) {
  182. unsigned int vendor, device, subvendor = PCI_ANY_ID,
  183. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  184. int fields;
  185. if (!strlen(id))
  186. continue;
  187. fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
  188. &vendor, &device, &subvendor, &subdevice,
  189. &class, &class_mask);
  190. if (fields < 2) {
  191. pr_warn("invalid id string \"%s\"\n", id);
  192. continue;
  193. }
  194. rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
  195. subvendor, subdevice, class, class_mask, 0);
  196. if (rc)
  197. pr_warn("failed to add dynamic id [%04x:%04x[%04x:%04x]] class %#08x/%08x (%d)\n",
  198. vendor, device, subvendor, subdevice,
  199. class, class_mask, rc);
  200. else
  201. pr_info("add [%04x:%04x[%04x:%04x]] class %#08x/%08x\n",
  202. vendor, device, subvendor, subdevice,
  203. class, class_mask);
  204. }
  205. }
  206. static int __init vfio_pci_init(void)
  207. {
  208. int ret;
  209. bool is_disable_vga = true;
  210. #ifdef CONFIG_VFIO_PCI_VGA
  211. is_disable_vga = disable_vga;
  212. #endif
  213. vfio_pci_core_set_params(nointxmask, is_disable_vga, disable_idle_d3);
  214. /* Register and scan for devices */
  215. ret = pci_register_driver(&vfio_pci_driver);
  216. if (ret)
  217. return ret;
  218. vfio_pci_fill_ids();
  219. if (disable_denylist)
  220. pr_warn("device denylist disabled.\n");
  221. return 0;
  222. }
  223. module_init(vfio_pci_init);
  224. static void __exit vfio_pci_cleanup(void)
  225. {
  226. pci_unregister_driver(&vfio_pci_driver);
  227. }
  228. module_exit(vfio_pci_cleanup);
  229. MODULE_LICENSE("GPL v2");
  230. MODULE_AUTHOR(DRIVER_AUTHOR);
  231. MODULE_DESCRIPTION(DRIVER_DESC);