vfio_pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  3. * Author: Alex Williamson <alex.williamson@redhat.com>
  4. *
  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.
  8. *
  9. * Derived from original vfio:
  10. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  11. * Author: Tom Lyon, pugs@cisco.com
  12. */
  13. #include <linux/device.h>
  14. #include <linux/eventfd.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/iommu.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/notifier.h>
  20. #include <linux/pci.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/vfio.h>
  26. #include "vfio_pci_private.h"
  27. #define DRIVER_VERSION "0.2"
  28. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  29. #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
  30. static bool nointxmask;
  31. module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(nointxmask,
  33. "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.");
  34. static int vfio_pci_enable(struct vfio_pci_device *vdev)
  35. {
  36. struct pci_dev *pdev = vdev->pdev;
  37. int ret;
  38. u16 cmd;
  39. u8 msix_pos;
  40. vdev->reset_works = (pci_reset_function(pdev) == 0);
  41. pci_save_state(pdev);
  42. vdev->pci_saved_state = pci_store_saved_state(pdev);
  43. if (!vdev->pci_saved_state)
  44. pr_debug("%s: Couldn't store %s saved state\n",
  45. __func__, dev_name(&pdev->dev));
  46. ret = vfio_config_init(vdev);
  47. if (ret)
  48. goto out;
  49. if (likely(!nointxmask))
  50. vdev->pci_2_3 = pci_intx_mask_supported(pdev);
  51. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  52. if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
  53. cmd &= ~PCI_COMMAND_INTX_DISABLE;
  54. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  55. }
  56. msix_pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  57. if (msix_pos) {
  58. u16 flags;
  59. u32 table;
  60. pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
  61. pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
  62. vdev->msix_bar = table & PCI_MSIX_FLAGS_BIRMASK;
  63. vdev->msix_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
  64. vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
  65. } else
  66. vdev->msix_bar = 0xFF;
  67. ret = pci_enable_device(pdev);
  68. if (ret)
  69. goto out;
  70. return ret;
  71. out:
  72. kfree(vdev->pci_saved_state);
  73. vdev->pci_saved_state = NULL;
  74. vfio_config_free(vdev);
  75. return ret;
  76. }
  77. static void vfio_pci_disable(struct vfio_pci_device *vdev)
  78. {
  79. int bar;
  80. pci_disable_device(vdev->pdev);
  81. vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
  82. VFIO_IRQ_SET_ACTION_TRIGGER,
  83. vdev->irq_type, 0, 0, NULL);
  84. vdev->virq_disabled = false;
  85. vfio_config_free(vdev);
  86. pci_reset_function(vdev->pdev);
  87. if (pci_load_and_free_saved_state(vdev->pdev,
  88. &vdev->pci_saved_state) == 0)
  89. pci_restore_state(vdev->pdev);
  90. else
  91. pr_info("%s: Couldn't reload %s saved state\n",
  92. __func__, dev_name(&vdev->pdev->dev));
  93. for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
  94. if (!vdev->barmap[bar])
  95. continue;
  96. pci_iounmap(vdev->pdev, vdev->barmap[bar]);
  97. pci_release_selected_regions(vdev->pdev, 1 << bar);
  98. vdev->barmap[bar] = NULL;
  99. }
  100. }
  101. static void vfio_pci_release(void *device_data)
  102. {
  103. struct vfio_pci_device *vdev = device_data;
  104. if (atomic_dec_and_test(&vdev->refcnt))
  105. vfio_pci_disable(vdev);
  106. module_put(THIS_MODULE);
  107. }
  108. static int vfio_pci_open(void *device_data)
  109. {
  110. struct vfio_pci_device *vdev = device_data;
  111. if (!try_module_get(THIS_MODULE))
  112. return -ENODEV;
  113. if (atomic_inc_return(&vdev->refcnt) == 1) {
  114. int ret = vfio_pci_enable(vdev);
  115. if (ret) {
  116. module_put(THIS_MODULE);
  117. return ret;
  118. }
  119. }
  120. return 0;
  121. }
  122. static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
  123. {
  124. if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
  125. u8 pin;
  126. pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
  127. if (pin)
  128. return 1;
  129. } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
  130. u8 pos;
  131. u16 flags;
  132. pos = pci_find_capability(vdev->pdev, PCI_CAP_ID_MSI);
  133. if (pos) {
  134. pci_read_config_word(vdev->pdev,
  135. pos + PCI_MSI_FLAGS, &flags);
  136. return 1 << (flags & PCI_MSI_FLAGS_QMASK);
  137. }
  138. } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
  139. u8 pos;
  140. u16 flags;
  141. pos = pci_find_capability(vdev->pdev, PCI_CAP_ID_MSIX);
  142. if (pos) {
  143. pci_read_config_word(vdev->pdev,
  144. pos + PCI_MSIX_FLAGS, &flags);
  145. return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
  146. }
  147. }
  148. return 0;
  149. }
  150. static long vfio_pci_ioctl(void *device_data,
  151. unsigned int cmd, unsigned long arg)
  152. {
  153. struct vfio_pci_device *vdev = device_data;
  154. unsigned long minsz;
  155. if (cmd == VFIO_DEVICE_GET_INFO) {
  156. struct vfio_device_info info;
  157. minsz = offsetofend(struct vfio_device_info, num_irqs);
  158. if (copy_from_user(&info, (void __user *)arg, minsz))
  159. return -EFAULT;
  160. if (info.argsz < minsz)
  161. return -EINVAL;
  162. info.flags = VFIO_DEVICE_FLAGS_PCI;
  163. if (vdev->reset_works)
  164. info.flags |= VFIO_DEVICE_FLAGS_RESET;
  165. info.num_regions = VFIO_PCI_NUM_REGIONS;
  166. info.num_irqs = VFIO_PCI_NUM_IRQS;
  167. return copy_to_user((void __user *)arg, &info, minsz);
  168. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  169. struct pci_dev *pdev = vdev->pdev;
  170. struct vfio_region_info info;
  171. minsz = offsetofend(struct vfio_region_info, offset);
  172. if (copy_from_user(&info, (void __user *)arg, minsz))
  173. return -EFAULT;
  174. if (info.argsz < minsz)
  175. return -EINVAL;
  176. switch (info.index) {
  177. case VFIO_PCI_CONFIG_REGION_INDEX:
  178. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  179. info.size = pdev->cfg_size;
  180. info.flags = VFIO_REGION_INFO_FLAG_READ |
  181. VFIO_REGION_INFO_FLAG_WRITE;
  182. break;
  183. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  184. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  185. info.size = pci_resource_len(pdev, info.index);
  186. if (!info.size) {
  187. info.flags = 0;
  188. break;
  189. }
  190. info.flags = VFIO_REGION_INFO_FLAG_READ |
  191. VFIO_REGION_INFO_FLAG_WRITE;
  192. if (pci_resource_flags(pdev, info.index) &
  193. IORESOURCE_MEM && info.size >= PAGE_SIZE)
  194. info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
  195. break;
  196. case VFIO_PCI_ROM_REGION_INDEX:
  197. {
  198. void __iomem *io;
  199. size_t size;
  200. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  201. info.flags = 0;
  202. /* Report the BAR size, not the ROM size */
  203. info.size = pci_resource_len(pdev, info.index);
  204. if (!info.size)
  205. break;
  206. /* Is it really there? */
  207. io = pci_map_rom(pdev, &size);
  208. if (!io || !size) {
  209. info.size = 0;
  210. break;
  211. }
  212. pci_unmap_rom(pdev, io);
  213. info.flags = VFIO_REGION_INFO_FLAG_READ;
  214. break;
  215. }
  216. default:
  217. return -EINVAL;
  218. }
  219. return copy_to_user((void __user *)arg, &info, minsz);
  220. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  221. struct vfio_irq_info info;
  222. minsz = offsetofend(struct vfio_irq_info, count);
  223. if (copy_from_user(&info, (void __user *)arg, minsz))
  224. return -EFAULT;
  225. if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
  226. return -EINVAL;
  227. info.flags = VFIO_IRQ_INFO_EVENTFD;
  228. info.count = vfio_pci_get_irq_count(vdev, info.index);
  229. if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
  230. info.flags |= (VFIO_IRQ_INFO_MASKABLE |
  231. VFIO_IRQ_INFO_AUTOMASKED);
  232. else
  233. info.flags |= VFIO_IRQ_INFO_NORESIZE;
  234. return copy_to_user((void __user *)arg, &info, minsz);
  235. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  236. struct vfio_irq_set hdr;
  237. u8 *data = NULL;
  238. int ret = 0;
  239. minsz = offsetofend(struct vfio_irq_set, count);
  240. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  241. return -EFAULT;
  242. if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
  243. hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  244. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  245. return -EINVAL;
  246. if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  247. size_t size;
  248. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  249. size = sizeof(uint8_t);
  250. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  251. size = sizeof(int32_t);
  252. else
  253. return -EINVAL;
  254. if (hdr.argsz - minsz < hdr.count * size ||
  255. hdr.count > vfio_pci_get_irq_count(vdev, hdr.index))
  256. return -EINVAL;
  257. data = kmalloc(hdr.count * size, GFP_KERNEL);
  258. if (!data)
  259. return -ENOMEM;
  260. if (copy_from_user(data, (void __user *)(arg + minsz),
  261. hdr.count * size)) {
  262. kfree(data);
  263. return -EFAULT;
  264. }
  265. }
  266. mutex_lock(&vdev->igate);
  267. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  268. hdr.start, hdr.count, data);
  269. mutex_unlock(&vdev->igate);
  270. kfree(data);
  271. return ret;
  272. } else if (cmd == VFIO_DEVICE_RESET)
  273. return vdev->reset_works ?
  274. pci_reset_function(vdev->pdev) : -EINVAL;
  275. return -ENOTTY;
  276. }
  277. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  278. size_t count, loff_t *ppos)
  279. {
  280. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  281. struct vfio_pci_device *vdev = device_data;
  282. struct pci_dev *pdev = vdev->pdev;
  283. if (index >= VFIO_PCI_NUM_REGIONS)
  284. return -EINVAL;
  285. if (index == VFIO_PCI_CONFIG_REGION_INDEX)
  286. return vfio_pci_config_readwrite(vdev, buf, count, ppos, false);
  287. else if (index == VFIO_PCI_ROM_REGION_INDEX)
  288. return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
  289. else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
  290. return vfio_pci_io_readwrite(vdev, buf, count, ppos, false);
  291. else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM)
  292. return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
  293. return -EINVAL;
  294. }
  295. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  296. size_t count, loff_t *ppos)
  297. {
  298. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  299. struct vfio_pci_device *vdev = device_data;
  300. struct pci_dev *pdev = vdev->pdev;
  301. if (index >= VFIO_PCI_NUM_REGIONS)
  302. return -EINVAL;
  303. if (index == VFIO_PCI_CONFIG_REGION_INDEX)
  304. return vfio_pci_config_readwrite(vdev, (char __user *)buf,
  305. count, ppos, true);
  306. else if (index == VFIO_PCI_ROM_REGION_INDEX)
  307. return -EINVAL;
  308. else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
  309. return vfio_pci_io_readwrite(vdev, (char __user *)buf,
  310. count, ppos, true);
  311. else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM) {
  312. return vfio_pci_mem_readwrite(vdev, (char __user *)buf,
  313. count, ppos, true);
  314. }
  315. return -EINVAL;
  316. }
  317. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  318. {
  319. struct vfio_pci_device *vdev = device_data;
  320. struct pci_dev *pdev = vdev->pdev;
  321. unsigned int index;
  322. u64 phys_len, req_len, pgoff, req_start, phys;
  323. int ret;
  324. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  325. if (vma->vm_end < vma->vm_start)
  326. return -EINVAL;
  327. if ((vma->vm_flags & VM_SHARED) == 0)
  328. return -EINVAL;
  329. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  330. return -EINVAL;
  331. if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
  332. return -EINVAL;
  333. phys_len = pci_resource_len(pdev, index);
  334. req_len = vma->vm_end - vma->vm_start;
  335. pgoff = vma->vm_pgoff &
  336. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  337. req_start = pgoff << PAGE_SHIFT;
  338. if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
  339. return -EINVAL;
  340. if (index == vdev->msix_bar) {
  341. /*
  342. * Disallow mmaps overlapping the MSI-X table; users don't
  343. * get to touch this directly. We could find somewhere
  344. * else to map the overlap, but page granularity is only
  345. * a recommendation, not a requirement, so the user needs
  346. * to know which bits are real. Requiring them to mmap
  347. * around the table makes that clear.
  348. */
  349. /* If neither entirely above nor below, then it overlaps */
  350. if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
  351. req_start + req_len <= vdev->msix_offset))
  352. return -EINVAL;
  353. }
  354. /*
  355. * Even though we don't make use of the barmap for the mmap,
  356. * we need to request the region and the barmap tracks that.
  357. */
  358. if (!vdev->barmap[index]) {
  359. ret = pci_request_selected_regions(pdev,
  360. 1 << index, "vfio-pci");
  361. if (ret)
  362. return ret;
  363. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  364. }
  365. vma->vm_private_data = vdev;
  366. vma->vm_flags |= (VM_IO | VM_RESERVED);
  367. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  368. phys = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  369. return remap_pfn_range(vma, vma->vm_start, phys,
  370. req_len, vma->vm_page_prot);
  371. }
  372. static const struct vfio_device_ops vfio_pci_ops = {
  373. .name = "vfio-pci",
  374. .open = vfio_pci_open,
  375. .release = vfio_pci_release,
  376. .ioctl = vfio_pci_ioctl,
  377. .read = vfio_pci_read,
  378. .write = vfio_pci_write,
  379. .mmap = vfio_pci_mmap,
  380. };
  381. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  382. {
  383. u8 type;
  384. struct vfio_pci_device *vdev;
  385. struct iommu_group *group;
  386. int ret;
  387. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
  388. if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
  389. return -EINVAL;
  390. group = iommu_group_get(&pdev->dev);
  391. if (!group)
  392. return -EINVAL;
  393. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  394. if (!vdev) {
  395. iommu_group_put(group);
  396. return -ENOMEM;
  397. }
  398. vdev->pdev = pdev;
  399. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  400. mutex_init(&vdev->igate);
  401. spin_lock_init(&vdev->irqlock);
  402. atomic_set(&vdev->refcnt, 0);
  403. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  404. if (ret) {
  405. iommu_group_put(group);
  406. kfree(vdev);
  407. }
  408. return ret;
  409. }
  410. static void vfio_pci_remove(struct pci_dev *pdev)
  411. {
  412. struct vfio_pci_device *vdev;
  413. vdev = vfio_del_group_dev(&pdev->dev);
  414. if (!vdev)
  415. return;
  416. iommu_group_put(pdev->dev.iommu_group);
  417. kfree(vdev);
  418. }
  419. static struct pci_driver vfio_pci_driver = {
  420. .name = "vfio-pci",
  421. .id_table = NULL, /* only dynamic ids */
  422. .probe = vfio_pci_probe,
  423. .remove = vfio_pci_remove,
  424. };
  425. static void __exit vfio_pci_cleanup(void)
  426. {
  427. pci_unregister_driver(&vfio_pci_driver);
  428. vfio_pci_virqfd_exit();
  429. vfio_pci_uninit_perm_bits();
  430. }
  431. static int __init vfio_pci_init(void)
  432. {
  433. int ret;
  434. /* Allocate shared config space permision data used by all devices */
  435. ret = vfio_pci_init_perm_bits();
  436. if (ret)
  437. return ret;
  438. /* Start the virqfd cleanup handler */
  439. ret = vfio_pci_virqfd_init();
  440. if (ret)
  441. goto out_virqfd;
  442. /* Register and scan for devices */
  443. ret = pci_register_driver(&vfio_pci_driver);
  444. if (ret)
  445. goto out_driver;
  446. return 0;
  447. out_virqfd:
  448. vfio_pci_virqfd_exit();
  449. out_driver:
  450. vfio_pci_uninit_perm_bits();
  451. return ret;
  452. }
  453. module_init(vfio_pci_init);
  454. module_exit(vfio_pci_cleanup);
  455. MODULE_VERSION(DRIVER_VERSION);
  456. MODULE_LICENSE("GPL v2");
  457. MODULE_AUTHOR(DRIVER_AUTHOR);
  458. MODULE_DESCRIPTION(DRIVER_DESC);