vfio_pci.c 15 KB

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