vfio_pci.c 15 KB

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