vfio_pci.c 15 KB

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