vfio_pci.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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/file.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iommu.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/notifier.h>
  21. #include <linux/pci.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/slab.h>
  24. #include <linux/types.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/vfio.h>
  27. #include "vfio_pci_private.h"
  28. #define DRIVER_VERSION "0.2"
  29. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  30. #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
  31. static bool nointxmask;
  32. module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
  33. MODULE_PARM_DESC(nointxmask,
  34. "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.");
  35. static DEFINE_MUTEX(driver_lock);
  36. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
  37. static int vfio_pci_enable(struct vfio_pci_device *vdev)
  38. {
  39. struct pci_dev *pdev = vdev->pdev;
  40. int ret;
  41. u16 cmd;
  42. u8 msix_pos;
  43. /* Don't allow our initial saved state to include busmaster */
  44. pci_clear_master(pdev);
  45. ret = pci_enable_device(pdev);
  46. if (ret)
  47. return ret;
  48. vdev->reset_works = (pci_reset_function(pdev) == 0);
  49. pci_save_state(pdev);
  50. vdev->pci_saved_state = pci_store_saved_state(pdev);
  51. if (!vdev->pci_saved_state)
  52. pr_debug("%s: Couldn't store %s saved state\n",
  53. __func__, dev_name(&pdev->dev));
  54. ret = vfio_config_init(vdev);
  55. if (ret) {
  56. kfree(vdev->pci_saved_state);
  57. vdev->pci_saved_state = NULL;
  58. pci_disable_device(pdev);
  59. return ret;
  60. }
  61. if (likely(!nointxmask))
  62. vdev->pci_2_3 = pci_intx_mask_supported(pdev);
  63. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  64. if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
  65. cmd &= ~PCI_COMMAND_INTX_DISABLE;
  66. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  67. }
  68. msix_pos = pdev->msix_cap;
  69. if (msix_pos) {
  70. u16 flags;
  71. u32 table;
  72. pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
  73. pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
  74. vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
  75. vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
  76. vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
  77. } else
  78. vdev->msix_bar = 0xFF;
  79. #ifdef CONFIG_VFIO_PCI_VGA
  80. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
  81. vdev->has_vga = true;
  82. #endif
  83. return 0;
  84. }
  85. static void vfio_pci_disable(struct vfio_pci_device *vdev)
  86. {
  87. struct pci_dev *pdev = vdev->pdev;
  88. int bar;
  89. /* Stop the device from further DMA */
  90. pci_clear_master(pdev);
  91. vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
  92. VFIO_IRQ_SET_ACTION_TRIGGER,
  93. vdev->irq_type, 0, 0, NULL);
  94. vdev->virq_disabled = false;
  95. vfio_config_free(vdev);
  96. for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
  97. if (!vdev->barmap[bar])
  98. continue;
  99. pci_iounmap(pdev, vdev->barmap[bar]);
  100. pci_release_selected_regions(pdev, 1 << bar);
  101. vdev->barmap[bar] = NULL;
  102. }
  103. vdev->needs_reset = true;
  104. /*
  105. * If we have saved state, restore it. If we can reset the device,
  106. * even better. Resetting with current state seems better than
  107. * nothing, but saving and restoring current state without reset
  108. * is just busy work.
  109. */
  110. if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
  111. pr_info("%s: Couldn't reload %s saved state\n",
  112. __func__, dev_name(&pdev->dev));
  113. if (!vdev->reset_works)
  114. goto out;
  115. pci_save_state(pdev);
  116. }
  117. /*
  118. * Disable INTx and MSI, presumably to avoid spurious interrupts
  119. * during reset. Stolen from pci_reset_function()
  120. */
  121. pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  122. /*
  123. * Try to reset the device. The success of this is dependent on
  124. * being able to lock the device, which is not always possible.
  125. */
  126. if (vdev->reset_works) {
  127. int ret = pci_try_reset_function(pdev);
  128. if (ret)
  129. pr_warn("%s: Failed to reset device %s (%d)\n",
  130. __func__, dev_name(&pdev->dev), ret);
  131. else
  132. vdev->needs_reset = false;
  133. }
  134. pci_restore_state(pdev);
  135. out:
  136. pci_disable_device(pdev);
  137. vfio_pci_try_bus_reset(vdev);
  138. }
  139. static void vfio_pci_release(void *device_data)
  140. {
  141. struct vfio_pci_device *vdev = device_data;
  142. mutex_lock(&driver_lock);
  143. if (!(--vdev->refcnt)) {
  144. vfio_spapr_pci_eeh_release(vdev->pdev);
  145. vfio_pci_disable(vdev);
  146. }
  147. mutex_unlock(&driver_lock);
  148. module_put(THIS_MODULE);
  149. }
  150. static int vfio_pci_open(void *device_data)
  151. {
  152. struct vfio_pci_device *vdev = device_data;
  153. int ret = 0;
  154. if (!try_module_get(THIS_MODULE))
  155. return -ENODEV;
  156. mutex_lock(&driver_lock);
  157. if (!vdev->refcnt) {
  158. ret = vfio_pci_enable(vdev);
  159. if (ret)
  160. goto error;
  161. vfio_spapr_pci_eeh_open(vdev->pdev);
  162. }
  163. vdev->refcnt++;
  164. error:
  165. mutex_unlock(&driver_lock);
  166. if (ret)
  167. module_put(THIS_MODULE);
  168. return ret;
  169. }
  170. static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
  171. {
  172. if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
  173. u8 pin;
  174. pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
  175. if (pin)
  176. return 1;
  177. } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
  178. u8 pos;
  179. u16 flags;
  180. pos = vdev->pdev->msi_cap;
  181. if (pos) {
  182. pci_read_config_word(vdev->pdev,
  183. pos + PCI_MSI_FLAGS, &flags);
  184. return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
  185. }
  186. } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
  187. u8 pos;
  188. u16 flags;
  189. pos = vdev->pdev->msix_cap;
  190. if (pos) {
  191. pci_read_config_word(vdev->pdev,
  192. pos + PCI_MSIX_FLAGS, &flags);
  193. return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
  194. }
  195. } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX)
  196. if (pci_is_pcie(vdev->pdev))
  197. return 1;
  198. return 0;
  199. }
  200. static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
  201. {
  202. (*(int *)data)++;
  203. return 0;
  204. }
  205. struct vfio_pci_fill_info {
  206. int max;
  207. int cur;
  208. struct vfio_pci_dependent_device *devices;
  209. };
  210. static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
  211. {
  212. struct vfio_pci_fill_info *fill = data;
  213. struct iommu_group *iommu_group;
  214. if (fill->cur == fill->max)
  215. return -EAGAIN; /* Something changed, try again */
  216. iommu_group = iommu_group_get(&pdev->dev);
  217. if (!iommu_group)
  218. return -EPERM; /* Cannot reset non-isolated devices */
  219. fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
  220. fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
  221. fill->devices[fill->cur].bus = pdev->bus->number;
  222. fill->devices[fill->cur].devfn = pdev->devfn;
  223. fill->cur++;
  224. iommu_group_put(iommu_group);
  225. return 0;
  226. }
  227. struct vfio_pci_group_entry {
  228. struct vfio_group *group;
  229. int id;
  230. };
  231. struct vfio_pci_group_info {
  232. int count;
  233. struct vfio_pci_group_entry *groups;
  234. };
  235. static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
  236. {
  237. struct vfio_pci_group_info *info = data;
  238. struct iommu_group *group;
  239. int id, i;
  240. group = iommu_group_get(&pdev->dev);
  241. if (!group)
  242. return -EPERM;
  243. id = iommu_group_id(group);
  244. for (i = 0; i < info->count; i++)
  245. if (info->groups[i].id == id)
  246. break;
  247. iommu_group_put(group);
  248. return (i == info->count) ? -EINVAL : 0;
  249. }
  250. static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
  251. {
  252. for (; pdev; pdev = pdev->bus->self)
  253. if (pdev->bus == slot->bus)
  254. return (pdev->slot == slot);
  255. return false;
  256. }
  257. struct vfio_pci_walk_info {
  258. int (*fn)(struct pci_dev *, void *data);
  259. void *data;
  260. struct pci_dev *pdev;
  261. bool slot;
  262. int ret;
  263. };
  264. static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
  265. {
  266. struct vfio_pci_walk_info *walk = data;
  267. if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
  268. walk->ret = walk->fn(pdev, walk->data);
  269. return walk->ret;
  270. }
  271. static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
  272. int (*fn)(struct pci_dev *,
  273. void *data), void *data,
  274. bool slot)
  275. {
  276. struct vfio_pci_walk_info walk = {
  277. .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
  278. };
  279. pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
  280. return walk.ret;
  281. }
  282. static long vfio_pci_ioctl(void *device_data,
  283. unsigned int cmd, unsigned long arg)
  284. {
  285. struct vfio_pci_device *vdev = device_data;
  286. unsigned long minsz;
  287. if (cmd == VFIO_DEVICE_GET_INFO) {
  288. struct vfio_device_info info;
  289. minsz = offsetofend(struct vfio_device_info, num_irqs);
  290. if (copy_from_user(&info, (void __user *)arg, minsz))
  291. return -EFAULT;
  292. if (info.argsz < minsz)
  293. return -EINVAL;
  294. info.flags = VFIO_DEVICE_FLAGS_PCI;
  295. if (vdev->reset_works)
  296. info.flags |= VFIO_DEVICE_FLAGS_RESET;
  297. info.num_regions = VFIO_PCI_NUM_REGIONS;
  298. info.num_irqs = VFIO_PCI_NUM_IRQS;
  299. return copy_to_user((void __user *)arg, &info, minsz);
  300. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  301. struct pci_dev *pdev = vdev->pdev;
  302. struct vfio_region_info info;
  303. minsz = offsetofend(struct vfio_region_info, offset);
  304. if (copy_from_user(&info, (void __user *)arg, minsz))
  305. return -EFAULT;
  306. if (info.argsz < minsz)
  307. return -EINVAL;
  308. switch (info.index) {
  309. case VFIO_PCI_CONFIG_REGION_INDEX:
  310. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  311. info.size = pdev->cfg_size;
  312. info.flags = VFIO_REGION_INFO_FLAG_READ |
  313. VFIO_REGION_INFO_FLAG_WRITE;
  314. break;
  315. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  316. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  317. info.size = pci_resource_len(pdev, info.index);
  318. if (!info.size) {
  319. info.flags = 0;
  320. break;
  321. }
  322. info.flags = VFIO_REGION_INFO_FLAG_READ |
  323. VFIO_REGION_INFO_FLAG_WRITE;
  324. if (pci_resource_flags(pdev, info.index) &
  325. IORESOURCE_MEM && info.size >= PAGE_SIZE)
  326. info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
  327. break;
  328. case VFIO_PCI_ROM_REGION_INDEX:
  329. {
  330. void __iomem *io;
  331. size_t size;
  332. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  333. info.flags = 0;
  334. /* Report the BAR size, not the ROM size */
  335. info.size = pci_resource_len(pdev, info.index);
  336. if (!info.size)
  337. break;
  338. /* Is it really there? */
  339. io = pci_map_rom(pdev, &size);
  340. if (!io || !size) {
  341. info.size = 0;
  342. break;
  343. }
  344. pci_unmap_rom(pdev, io);
  345. info.flags = VFIO_REGION_INFO_FLAG_READ;
  346. break;
  347. }
  348. case VFIO_PCI_VGA_REGION_INDEX:
  349. if (!vdev->has_vga)
  350. return -EINVAL;
  351. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  352. info.size = 0xc0000;
  353. info.flags = VFIO_REGION_INFO_FLAG_READ |
  354. VFIO_REGION_INFO_FLAG_WRITE;
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. return copy_to_user((void __user *)arg, &info, minsz);
  360. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  361. struct vfio_irq_info info;
  362. minsz = offsetofend(struct vfio_irq_info, count);
  363. if (copy_from_user(&info, (void __user *)arg, minsz))
  364. return -EFAULT;
  365. if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
  366. return -EINVAL;
  367. switch (info.index) {
  368. case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
  369. break;
  370. case VFIO_PCI_ERR_IRQ_INDEX:
  371. if (pci_is_pcie(vdev->pdev))
  372. break;
  373. /* pass thru to return error */
  374. default:
  375. return -EINVAL;
  376. }
  377. info.flags = VFIO_IRQ_INFO_EVENTFD;
  378. info.count = vfio_pci_get_irq_count(vdev, info.index);
  379. if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
  380. info.flags |= (VFIO_IRQ_INFO_MASKABLE |
  381. VFIO_IRQ_INFO_AUTOMASKED);
  382. else
  383. info.flags |= VFIO_IRQ_INFO_NORESIZE;
  384. return copy_to_user((void __user *)arg, &info, minsz);
  385. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  386. struct vfio_irq_set hdr;
  387. u8 *data = NULL;
  388. int ret = 0;
  389. minsz = offsetofend(struct vfio_irq_set, count);
  390. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  391. return -EFAULT;
  392. if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
  393. hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  394. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  395. return -EINVAL;
  396. if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  397. size_t size;
  398. int max = vfio_pci_get_irq_count(vdev, hdr.index);
  399. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  400. size = sizeof(uint8_t);
  401. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  402. size = sizeof(int32_t);
  403. else
  404. return -EINVAL;
  405. if (hdr.argsz - minsz < hdr.count * size ||
  406. hdr.start >= max || hdr.start + hdr.count > max)
  407. return -EINVAL;
  408. data = memdup_user((void __user *)(arg + minsz),
  409. hdr.count * size);
  410. if (IS_ERR(data))
  411. return PTR_ERR(data);
  412. }
  413. mutex_lock(&vdev->igate);
  414. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  415. hdr.start, hdr.count, data);
  416. mutex_unlock(&vdev->igate);
  417. kfree(data);
  418. return ret;
  419. } else if (cmd == VFIO_DEVICE_RESET) {
  420. return vdev->reset_works ?
  421. pci_try_reset_function(vdev->pdev) : -EINVAL;
  422. } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
  423. struct vfio_pci_hot_reset_info hdr;
  424. struct vfio_pci_fill_info fill = { 0 };
  425. struct vfio_pci_dependent_device *devices = NULL;
  426. bool slot = false;
  427. int ret = 0;
  428. minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
  429. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  430. return -EFAULT;
  431. if (hdr.argsz < minsz)
  432. return -EINVAL;
  433. hdr.flags = 0;
  434. /* Can we do a slot or bus reset or neither? */
  435. if (!pci_probe_reset_slot(vdev->pdev->slot))
  436. slot = true;
  437. else if (pci_probe_reset_bus(vdev->pdev->bus))
  438. return -ENODEV;
  439. /* How many devices are affected? */
  440. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  441. vfio_pci_count_devs,
  442. &fill.max, slot);
  443. if (ret)
  444. return ret;
  445. WARN_ON(!fill.max); /* Should always be at least one */
  446. /*
  447. * If there's enough space, fill it now, otherwise return
  448. * -ENOSPC and the number of devices affected.
  449. */
  450. if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
  451. ret = -ENOSPC;
  452. hdr.count = fill.max;
  453. goto reset_info_exit;
  454. }
  455. devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
  456. if (!devices)
  457. return -ENOMEM;
  458. fill.devices = devices;
  459. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  460. vfio_pci_fill_devs,
  461. &fill, slot);
  462. /*
  463. * If a device was removed between counting and filling,
  464. * we may come up short of fill.max. If a device was
  465. * added, we'll have a return of -EAGAIN above.
  466. */
  467. if (!ret)
  468. hdr.count = fill.cur;
  469. reset_info_exit:
  470. if (copy_to_user((void __user *)arg, &hdr, minsz))
  471. ret = -EFAULT;
  472. if (!ret) {
  473. if (copy_to_user((void __user *)(arg + minsz), devices,
  474. hdr.count * sizeof(*devices)))
  475. ret = -EFAULT;
  476. }
  477. kfree(devices);
  478. return ret;
  479. } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
  480. struct vfio_pci_hot_reset hdr;
  481. int32_t *group_fds;
  482. struct vfio_pci_group_entry *groups;
  483. struct vfio_pci_group_info info;
  484. bool slot = false;
  485. int i, count = 0, ret = 0;
  486. minsz = offsetofend(struct vfio_pci_hot_reset, count);
  487. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  488. return -EFAULT;
  489. if (hdr.argsz < minsz || hdr.flags)
  490. return -EINVAL;
  491. /* Can we do a slot or bus reset or neither? */
  492. if (!pci_probe_reset_slot(vdev->pdev->slot))
  493. slot = true;
  494. else if (pci_probe_reset_bus(vdev->pdev->bus))
  495. return -ENODEV;
  496. /*
  497. * We can't let userspace give us an arbitrarily large
  498. * buffer to copy, so verify how many we think there
  499. * could be. Note groups can have multiple devices so
  500. * one group per device is the max.
  501. */
  502. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  503. vfio_pci_count_devs,
  504. &count, slot);
  505. if (ret)
  506. return ret;
  507. /* Somewhere between 1 and count is OK */
  508. if (!hdr.count || hdr.count > count)
  509. return -EINVAL;
  510. group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
  511. groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
  512. if (!group_fds || !groups) {
  513. kfree(group_fds);
  514. kfree(groups);
  515. return -ENOMEM;
  516. }
  517. if (copy_from_user(group_fds, (void __user *)(arg + minsz),
  518. hdr.count * sizeof(*group_fds))) {
  519. kfree(group_fds);
  520. kfree(groups);
  521. return -EFAULT;
  522. }
  523. /*
  524. * For each group_fd, get the group through the vfio external
  525. * user interface and store the group and iommu ID. This
  526. * ensures the group is held across the reset.
  527. */
  528. for (i = 0; i < hdr.count; i++) {
  529. struct vfio_group *group;
  530. struct fd f = fdget(group_fds[i]);
  531. if (!f.file) {
  532. ret = -EBADF;
  533. break;
  534. }
  535. group = vfio_group_get_external_user(f.file);
  536. fdput(f);
  537. if (IS_ERR(group)) {
  538. ret = PTR_ERR(group);
  539. break;
  540. }
  541. groups[i].group = group;
  542. groups[i].id = vfio_external_user_iommu_id(group);
  543. }
  544. kfree(group_fds);
  545. /* release reference to groups on error */
  546. if (ret)
  547. goto hot_reset_release;
  548. info.count = hdr.count;
  549. info.groups = groups;
  550. /*
  551. * Test whether all the affected devices are contained
  552. * by the set of groups provided by the user.
  553. */
  554. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  555. vfio_pci_validate_devs,
  556. &info, slot);
  557. if (!ret)
  558. /* User has access, do the reset */
  559. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  560. pci_try_reset_bus(vdev->pdev->bus);
  561. hot_reset_release:
  562. for (i--; i >= 0; i--)
  563. vfio_group_put_external_user(groups[i].group);
  564. kfree(groups);
  565. return ret;
  566. }
  567. return -ENOTTY;
  568. }
  569. static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
  570. size_t count, loff_t *ppos, bool iswrite)
  571. {
  572. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  573. struct vfio_pci_device *vdev = device_data;
  574. if (index >= VFIO_PCI_NUM_REGIONS)
  575. return -EINVAL;
  576. switch (index) {
  577. case VFIO_PCI_CONFIG_REGION_INDEX:
  578. return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
  579. case VFIO_PCI_ROM_REGION_INDEX:
  580. if (iswrite)
  581. return -EINVAL;
  582. return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
  583. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  584. return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
  585. case VFIO_PCI_VGA_REGION_INDEX:
  586. return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
  587. }
  588. return -EINVAL;
  589. }
  590. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  591. size_t count, loff_t *ppos)
  592. {
  593. if (!count)
  594. return 0;
  595. return vfio_pci_rw(device_data, buf, count, ppos, false);
  596. }
  597. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  598. size_t count, loff_t *ppos)
  599. {
  600. if (!count)
  601. return 0;
  602. return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
  603. }
  604. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  605. {
  606. struct vfio_pci_device *vdev = device_data;
  607. struct pci_dev *pdev = vdev->pdev;
  608. unsigned int index;
  609. u64 phys_len, req_len, pgoff, req_start;
  610. int ret;
  611. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  612. if (vma->vm_end < vma->vm_start)
  613. return -EINVAL;
  614. if ((vma->vm_flags & VM_SHARED) == 0)
  615. return -EINVAL;
  616. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  617. return -EINVAL;
  618. if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
  619. return -EINVAL;
  620. phys_len = pci_resource_len(pdev, index);
  621. req_len = vma->vm_end - vma->vm_start;
  622. pgoff = vma->vm_pgoff &
  623. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  624. req_start = pgoff << PAGE_SHIFT;
  625. if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
  626. return -EINVAL;
  627. if (index == vdev->msix_bar) {
  628. /*
  629. * Disallow mmaps overlapping the MSI-X table; users don't
  630. * get to touch this directly. We could find somewhere
  631. * else to map the overlap, but page granularity is only
  632. * a recommendation, not a requirement, so the user needs
  633. * to know which bits are real. Requiring them to mmap
  634. * around the table makes that clear.
  635. */
  636. /* If neither entirely above nor below, then it overlaps */
  637. if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
  638. req_start + req_len <= vdev->msix_offset))
  639. return -EINVAL;
  640. }
  641. /*
  642. * Even though we don't make use of the barmap for the mmap,
  643. * we need to request the region and the barmap tracks that.
  644. */
  645. if (!vdev->barmap[index]) {
  646. ret = pci_request_selected_regions(pdev,
  647. 1 << index, "vfio-pci");
  648. if (ret)
  649. return ret;
  650. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  651. }
  652. vma->vm_private_data = vdev;
  653. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  654. vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  655. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  656. req_len, vma->vm_page_prot);
  657. }
  658. static const struct vfio_device_ops vfio_pci_ops = {
  659. .name = "vfio-pci",
  660. .open = vfio_pci_open,
  661. .release = vfio_pci_release,
  662. .ioctl = vfio_pci_ioctl,
  663. .read = vfio_pci_read,
  664. .write = vfio_pci_write,
  665. .mmap = vfio_pci_mmap,
  666. };
  667. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  668. {
  669. u8 type;
  670. struct vfio_pci_device *vdev;
  671. struct iommu_group *group;
  672. int ret;
  673. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
  674. if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
  675. return -EINVAL;
  676. group = iommu_group_get(&pdev->dev);
  677. if (!group)
  678. return -EINVAL;
  679. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  680. if (!vdev) {
  681. iommu_group_put(group);
  682. return -ENOMEM;
  683. }
  684. vdev->pdev = pdev;
  685. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  686. mutex_init(&vdev->igate);
  687. spin_lock_init(&vdev->irqlock);
  688. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  689. if (ret) {
  690. iommu_group_put(group);
  691. kfree(vdev);
  692. }
  693. return ret;
  694. }
  695. static void vfio_pci_remove(struct pci_dev *pdev)
  696. {
  697. struct vfio_pci_device *vdev;
  698. mutex_lock(&driver_lock);
  699. vdev = vfio_del_group_dev(&pdev->dev);
  700. if (vdev) {
  701. iommu_group_put(pdev->dev.iommu_group);
  702. kfree(vdev);
  703. }
  704. mutex_unlock(&driver_lock);
  705. }
  706. static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
  707. pci_channel_state_t state)
  708. {
  709. struct vfio_pci_device *vdev;
  710. struct vfio_device *device;
  711. device = vfio_device_get_from_dev(&pdev->dev);
  712. if (device == NULL)
  713. return PCI_ERS_RESULT_DISCONNECT;
  714. vdev = vfio_device_data(device);
  715. if (vdev == NULL) {
  716. vfio_device_put(device);
  717. return PCI_ERS_RESULT_DISCONNECT;
  718. }
  719. mutex_lock(&vdev->igate);
  720. if (vdev->err_trigger)
  721. eventfd_signal(vdev->err_trigger, 1);
  722. mutex_unlock(&vdev->igate);
  723. vfio_device_put(device);
  724. return PCI_ERS_RESULT_CAN_RECOVER;
  725. }
  726. static struct pci_error_handlers vfio_err_handlers = {
  727. .error_detected = vfio_pci_aer_err_detected,
  728. };
  729. static struct pci_driver vfio_pci_driver = {
  730. .name = "vfio-pci",
  731. .id_table = NULL, /* only dynamic ids */
  732. .probe = vfio_pci_probe,
  733. .remove = vfio_pci_remove,
  734. .err_handler = &vfio_err_handlers,
  735. };
  736. /*
  737. * Test whether a reset is necessary and possible. We mark devices as
  738. * needs_reset when they are released, but don't have a function-local reset
  739. * available. If any of these exist in the affected devices, we want to do
  740. * a bus/slot reset. We also need all of the affected devices to be unused,
  741. * so we abort if any device has a non-zero refcnt. driver_lock prevents a
  742. * device from being opened during the scan or unbound from vfio-pci.
  743. */
  744. static int vfio_pci_test_bus_reset(struct pci_dev *pdev, void *data)
  745. {
  746. bool *needs_reset = data;
  747. struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver);
  748. int ret = -EBUSY;
  749. if (pci_drv == &vfio_pci_driver) {
  750. struct vfio_device *device;
  751. struct vfio_pci_device *vdev;
  752. device = vfio_device_get_from_dev(&pdev->dev);
  753. if (!device)
  754. return ret;
  755. vdev = vfio_device_data(device);
  756. if (vdev) {
  757. if (vdev->needs_reset)
  758. *needs_reset = true;
  759. if (!vdev->refcnt)
  760. ret = 0;
  761. }
  762. vfio_device_put(device);
  763. }
  764. /*
  765. * TODO: vfio-core considers groups to be viable even if some devices
  766. * are attached to known drivers, like pci-stub or pcieport. We can't
  767. * freeze devices from being unbound to those drivers like we can
  768. * here though, so it would be racy to test for them. We also can't
  769. * use device_lock() to prevent changes as that would interfere with
  770. * PCI-core taking device_lock during bus reset. For now, we require
  771. * devices to be bound to vfio-pci to get a bus/slot reset on release.
  772. */
  773. return ret;
  774. }
  775. /* Clear needs_reset on all affected devices after successful bus/slot reset */
  776. static int vfio_pci_clear_needs_reset(struct pci_dev *pdev, void *data)
  777. {
  778. struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver);
  779. if (pci_drv == &vfio_pci_driver) {
  780. struct vfio_device *device;
  781. struct vfio_pci_device *vdev;
  782. device = vfio_device_get_from_dev(&pdev->dev);
  783. if (!device)
  784. return 0;
  785. vdev = vfio_device_data(device);
  786. if (vdev)
  787. vdev->needs_reset = false;
  788. vfio_device_put(device);
  789. }
  790. return 0;
  791. }
  792. /*
  793. * Attempt to do a bus/slot reset if there are devices affected by a reset for
  794. * this device that are needs_reset and all of the affected devices are unused
  795. * (!refcnt). Callers of this function are required to hold driver_lock such
  796. * that devices can not be unbound from vfio-pci or opened by a user while we
  797. * test for and perform a bus/slot reset.
  798. */
  799. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
  800. {
  801. bool needs_reset = false, slot = false;
  802. int ret;
  803. if (!pci_probe_reset_slot(vdev->pdev->slot))
  804. slot = true;
  805. else if (pci_probe_reset_bus(vdev->pdev->bus))
  806. return;
  807. if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
  808. vfio_pci_test_bus_reset,
  809. &needs_reset, slot) || !needs_reset)
  810. return;
  811. if (slot)
  812. ret = pci_try_reset_slot(vdev->pdev->slot);
  813. else
  814. ret = pci_try_reset_bus(vdev->pdev->bus);
  815. if (ret)
  816. return;
  817. vfio_pci_for_each_slot_or_bus(vdev->pdev,
  818. vfio_pci_clear_needs_reset, NULL, slot);
  819. }
  820. static void __exit vfio_pci_cleanup(void)
  821. {
  822. pci_unregister_driver(&vfio_pci_driver);
  823. vfio_pci_virqfd_exit();
  824. vfio_pci_uninit_perm_bits();
  825. }
  826. static int __init vfio_pci_init(void)
  827. {
  828. int ret;
  829. /* Allocate shared config space permision data used by all devices */
  830. ret = vfio_pci_init_perm_bits();
  831. if (ret)
  832. return ret;
  833. /* Start the virqfd cleanup handler */
  834. ret = vfio_pci_virqfd_init();
  835. if (ret)
  836. goto out_virqfd;
  837. /* Register and scan for devices */
  838. ret = pci_register_driver(&vfio_pci_driver);
  839. if (ret)
  840. goto out_driver;
  841. return 0;
  842. out_driver:
  843. vfio_pci_virqfd_exit();
  844. out_virqfd:
  845. vfio_pci_uninit_perm_bits();
  846. return ret;
  847. }
  848. module_init(vfio_pci_init);
  849. module_exit(vfio_pci_cleanup);
  850. MODULE_VERSION(DRIVER_VERSION);
  851. MODULE_LICENSE("GPL v2");
  852. MODULE_AUTHOR(DRIVER_AUTHOR);
  853. MODULE_DESCRIPTION(DRIVER_DESC);