vfio_pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 = pdev->msix_cap;
  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_TABLE_BIR;
  69. vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
  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 = vdev->pdev->msi_cap;
  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 = vdev->pdev->msix_cap;
  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. int max = vfio_pci_get_irq_count(vdev, hdr.index);
  274. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  275. size = sizeof(uint8_t);
  276. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  277. size = sizeof(int32_t);
  278. else
  279. return -EINVAL;
  280. if (hdr.argsz - minsz < hdr.count * size ||
  281. hdr.start >= max || hdr.start + hdr.count > max)
  282. return -EINVAL;
  283. data = memdup_user((void __user *)(arg + minsz),
  284. hdr.count * size);
  285. if (IS_ERR(data))
  286. return PTR_ERR(data);
  287. }
  288. mutex_lock(&vdev->igate);
  289. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  290. hdr.start, hdr.count, data);
  291. mutex_unlock(&vdev->igate);
  292. kfree(data);
  293. return ret;
  294. } else if (cmd == VFIO_DEVICE_RESET)
  295. return vdev->reset_works ?
  296. pci_reset_function(vdev->pdev) : -EINVAL;
  297. return -ENOTTY;
  298. }
  299. static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
  300. size_t count, loff_t *ppos, bool iswrite)
  301. {
  302. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  303. struct vfio_pci_device *vdev = device_data;
  304. if (index >= VFIO_PCI_NUM_REGIONS)
  305. return -EINVAL;
  306. switch (index) {
  307. case VFIO_PCI_CONFIG_REGION_INDEX:
  308. return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
  309. case VFIO_PCI_ROM_REGION_INDEX:
  310. if (iswrite)
  311. return -EINVAL;
  312. return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
  313. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  314. return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
  315. case VFIO_PCI_VGA_REGION_INDEX:
  316. return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
  317. }
  318. return -EINVAL;
  319. }
  320. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  321. size_t count, loff_t *ppos)
  322. {
  323. if (!count)
  324. return 0;
  325. return vfio_pci_rw(device_data, buf, count, ppos, false);
  326. }
  327. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  328. size_t count, loff_t *ppos)
  329. {
  330. if (!count)
  331. return 0;
  332. return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
  333. }
  334. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  335. {
  336. struct vfio_pci_device *vdev = device_data;
  337. struct pci_dev *pdev = vdev->pdev;
  338. unsigned int index;
  339. u64 phys_len, req_len, pgoff, req_start;
  340. int ret;
  341. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  342. if (vma->vm_end < vma->vm_start)
  343. return -EINVAL;
  344. if ((vma->vm_flags & VM_SHARED) == 0)
  345. return -EINVAL;
  346. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  347. return -EINVAL;
  348. if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
  349. return -EINVAL;
  350. phys_len = pci_resource_len(pdev, index);
  351. req_len = vma->vm_end - vma->vm_start;
  352. pgoff = vma->vm_pgoff &
  353. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  354. req_start = pgoff << PAGE_SHIFT;
  355. if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
  356. return -EINVAL;
  357. if (index == vdev->msix_bar) {
  358. /*
  359. * Disallow mmaps overlapping the MSI-X table; users don't
  360. * get to touch this directly. We could find somewhere
  361. * else to map the overlap, but page granularity is only
  362. * a recommendation, not a requirement, so the user needs
  363. * to know which bits are real. Requiring them to mmap
  364. * around the table makes that clear.
  365. */
  366. /* If neither entirely above nor below, then it overlaps */
  367. if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
  368. req_start + req_len <= vdev->msix_offset))
  369. return -EINVAL;
  370. }
  371. /*
  372. * Even though we don't make use of the barmap for the mmap,
  373. * we need to request the region and the barmap tracks that.
  374. */
  375. if (!vdev->barmap[index]) {
  376. ret = pci_request_selected_regions(pdev,
  377. 1 << index, "vfio-pci");
  378. if (ret)
  379. return ret;
  380. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  381. }
  382. vma->vm_private_data = vdev;
  383. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  384. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  385. vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  386. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  387. req_len, vma->vm_page_prot);
  388. }
  389. static const struct vfio_device_ops vfio_pci_ops = {
  390. .name = "vfio-pci",
  391. .open = vfio_pci_open,
  392. .release = vfio_pci_release,
  393. .ioctl = vfio_pci_ioctl,
  394. .read = vfio_pci_read,
  395. .write = vfio_pci_write,
  396. .mmap = vfio_pci_mmap,
  397. };
  398. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  399. {
  400. u8 type;
  401. struct vfio_pci_device *vdev;
  402. struct iommu_group *group;
  403. int ret;
  404. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
  405. if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
  406. return -EINVAL;
  407. group = iommu_group_get(&pdev->dev);
  408. if (!group)
  409. return -EINVAL;
  410. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  411. if (!vdev) {
  412. iommu_group_put(group);
  413. return -ENOMEM;
  414. }
  415. vdev->pdev = pdev;
  416. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  417. mutex_init(&vdev->igate);
  418. spin_lock_init(&vdev->irqlock);
  419. atomic_set(&vdev->refcnt, 0);
  420. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  421. if (ret) {
  422. iommu_group_put(group);
  423. kfree(vdev);
  424. }
  425. return ret;
  426. }
  427. static void vfio_pci_remove(struct pci_dev *pdev)
  428. {
  429. struct vfio_pci_device *vdev;
  430. vdev = vfio_del_group_dev(&pdev->dev);
  431. if (!vdev)
  432. return;
  433. iommu_group_put(pdev->dev.iommu_group);
  434. kfree(vdev);
  435. }
  436. static struct pci_driver vfio_pci_driver = {
  437. .name = "vfio-pci",
  438. .id_table = NULL, /* only dynamic ids */
  439. .probe = vfio_pci_probe,
  440. .remove = vfio_pci_remove,
  441. };
  442. static void __exit vfio_pci_cleanup(void)
  443. {
  444. pci_unregister_driver(&vfio_pci_driver);
  445. vfio_pci_virqfd_exit();
  446. vfio_pci_uninit_perm_bits();
  447. }
  448. static int __init vfio_pci_init(void)
  449. {
  450. int ret;
  451. /* Allocate shared config space permision data used by all devices */
  452. ret = vfio_pci_init_perm_bits();
  453. if (ret)
  454. return ret;
  455. /* Start the virqfd cleanup handler */
  456. ret = vfio_pci_virqfd_init();
  457. if (ret)
  458. goto out_virqfd;
  459. /* Register and scan for devices */
  460. ret = pci_register_driver(&vfio_pci_driver);
  461. if (ret)
  462. goto out_driver;
  463. return 0;
  464. out_driver:
  465. vfio_pci_virqfd_exit();
  466. out_virqfd:
  467. vfio_pci_uninit_perm_bits();
  468. return ret;
  469. }
  470. module_init(vfio_pci_init);
  471. module_exit(vfio_pci_cleanup);
  472. MODULE_VERSION(DRIVER_VERSION);
  473. MODULE_LICENSE("GPL v2");
  474. MODULE_AUTHOR(DRIVER_AUTHOR);
  475. MODULE_DESCRIPTION(DRIVER_DESC);