vfio_pci.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && 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. } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
  204. {
  205. (*(int *)data)++;
  206. return 0;
  207. }
  208. struct vfio_pci_fill_info {
  209. int max;
  210. int cur;
  211. struct vfio_pci_dependent_device *devices;
  212. };
  213. static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
  214. {
  215. struct vfio_pci_fill_info *fill = data;
  216. struct iommu_group *iommu_group;
  217. if (fill->cur == fill->max)
  218. return -EAGAIN; /* Something changed, try again */
  219. iommu_group = iommu_group_get(&pdev->dev);
  220. if (!iommu_group)
  221. return -EPERM; /* Cannot reset non-isolated devices */
  222. fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
  223. fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
  224. fill->devices[fill->cur].bus = pdev->bus->number;
  225. fill->devices[fill->cur].devfn = pdev->devfn;
  226. fill->cur++;
  227. iommu_group_put(iommu_group);
  228. return 0;
  229. }
  230. struct vfio_pci_group_entry {
  231. struct vfio_group *group;
  232. int id;
  233. };
  234. struct vfio_pci_group_info {
  235. int count;
  236. struct vfio_pci_group_entry *groups;
  237. };
  238. static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
  239. {
  240. struct vfio_pci_group_info *info = data;
  241. struct iommu_group *group;
  242. int id, i;
  243. group = iommu_group_get(&pdev->dev);
  244. if (!group)
  245. return -EPERM;
  246. id = iommu_group_id(group);
  247. for (i = 0; i < info->count; i++)
  248. if (info->groups[i].id == id)
  249. break;
  250. iommu_group_put(group);
  251. return (i == info->count) ? -EINVAL : 0;
  252. }
  253. static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
  254. {
  255. for (; pdev; pdev = pdev->bus->self)
  256. if (pdev->bus == slot->bus)
  257. return (pdev->slot == slot);
  258. return false;
  259. }
  260. struct vfio_pci_walk_info {
  261. int (*fn)(struct pci_dev *, void *data);
  262. void *data;
  263. struct pci_dev *pdev;
  264. bool slot;
  265. int ret;
  266. };
  267. static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
  268. {
  269. struct vfio_pci_walk_info *walk = data;
  270. if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
  271. walk->ret = walk->fn(pdev, walk->data);
  272. return walk->ret;
  273. }
  274. static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
  275. int (*fn)(struct pci_dev *,
  276. void *data), void *data,
  277. bool slot)
  278. {
  279. struct vfio_pci_walk_info walk = {
  280. .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
  281. };
  282. pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
  283. return walk.ret;
  284. }
  285. static long vfio_pci_ioctl(void *device_data,
  286. unsigned int cmd, unsigned long arg)
  287. {
  288. struct vfio_pci_device *vdev = device_data;
  289. unsigned long minsz;
  290. if (cmd == VFIO_DEVICE_GET_INFO) {
  291. struct vfio_device_info info;
  292. minsz = offsetofend(struct vfio_device_info, num_irqs);
  293. if (copy_from_user(&info, (void __user *)arg, minsz))
  294. return -EFAULT;
  295. if (info.argsz < minsz)
  296. return -EINVAL;
  297. info.flags = VFIO_DEVICE_FLAGS_PCI;
  298. if (vdev->reset_works)
  299. info.flags |= VFIO_DEVICE_FLAGS_RESET;
  300. info.num_regions = VFIO_PCI_NUM_REGIONS;
  301. info.num_irqs = VFIO_PCI_NUM_IRQS;
  302. return copy_to_user((void __user *)arg, &info, minsz);
  303. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  304. struct pci_dev *pdev = vdev->pdev;
  305. struct vfio_region_info info;
  306. minsz = offsetofend(struct vfio_region_info, offset);
  307. if (copy_from_user(&info, (void __user *)arg, minsz))
  308. return -EFAULT;
  309. if (info.argsz < minsz)
  310. return -EINVAL;
  311. switch (info.index) {
  312. case VFIO_PCI_CONFIG_REGION_INDEX:
  313. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  314. info.size = pdev->cfg_size;
  315. info.flags = VFIO_REGION_INFO_FLAG_READ |
  316. VFIO_REGION_INFO_FLAG_WRITE;
  317. break;
  318. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  319. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  320. info.size = pci_resource_len(pdev, info.index);
  321. if (!info.size) {
  322. info.flags = 0;
  323. break;
  324. }
  325. info.flags = VFIO_REGION_INFO_FLAG_READ |
  326. VFIO_REGION_INFO_FLAG_WRITE;
  327. if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
  328. pci_resource_flags(pdev, info.index) &
  329. IORESOURCE_MEM && info.size >= PAGE_SIZE)
  330. info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
  331. break;
  332. case VFIO_PCI_ROM_REGION_INDEX:
  333. {
  334. void __iomem *io;
  335. size_t size;
  336. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  337. info.flags = 0;
  338. /* Report the BAR size, not the ROM size */
  339. info.size = pci_resource_len(pdev, info.index);
  340. if (!info.size)
  341. break;
  342. /* Is it really there? */
  343. io = pci_map_rom(pdev, &size);
  344. if (!io || !size) {
  345. info.size = 0;
  346. break;
  347. }
  348. pci_unmap_rom(pdev, io);
  349. info.flags = VFIO_REGION_INFO_FLAG_READ;
  350. break;
  351. }
  352. case VFIO_PCI_VGA_REGION_INDEX:
  353. if (!vdev->has_vga)
  354. return -EINVAL;
  355. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  356. info.size = 0xc0000;
  357. info.flags = VFIO_REGION_INFO_FLAG_READ |
  358. VFIO_REGION_INFO_FLAG_WRITE;
  359. break;
  360. default:
  361. return -EINVAL;
  362. }
  363. return copy_to_user((void __user *)arg, &info, minsz);
  364. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  365. struct vfio_irq_info info;
  366. minsz = offsetofend(struct vfio_irq_info, count);
  367. if (copy_from_user(&info, (void __user *)arg, minsz))
  368. return -EFAULT;
  369. if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
  370. return -EINVAL;
  371. switch (info.index) {
  372. case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
  373. case VFIO_PCI_REQ_IRQ_INDEX:
  374. break;
  375. case VFIO_PCI_ERR_IRQ_INDEX:
  376. if (pci_is_pcie(vdev->pdev))
  377. break;
  378. /* pass thru to return error */
  379. default:
  380. return -EINVAL;
  381. }
  382. info.flags = VFIO_IRQ_INFO_EVENTFD;
  383. info.count = vfio_pci_get_irq_count(vdev, info.index);
  384. if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
  385. info.flags |= (VFIO_IRQ_INFO_MASKABLE |
  386. VFIO_IRQ_INFO_AUTOMASKED);
  387. else
  388. info.flags |= VFIO_IRQ_INFO_NORESIZE;
  389. return copy_to_user((void __user *)arg, &info, minsz);
  390. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  391. struct vfio_irq_set hdr;
  392. u8 *data = NULL;
  393. int ret = 0;
  394. minsz = offsetofend(struct vfio_irq_set, count);
  395. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  396. return -EFAULT;
  397. if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
  398. hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  399. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  400. return -EINVAL;
  401. if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  402. size_t size;
  403. int max = vfio_pci_get_irq_count(vdev, hdr.index);
  404. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  405. size = sizeof(uint8_t);
  406. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  407. size = sizeof(int32_t);
  408. else
  409. return -EINVAL;
  410. if (hdr.argsz - minsz < hdr.count * size ||
  411. hdr.start >= max || hdr.start + hdr.count > max)
  412. return -EINVAL;
  413. data = memdup_user((void __user *)(arg + minsz),
  414. hdr.count * size);
  415. if (IS_ERR(data))
  416. return PTR_ERR(data);
  417. }
  418. mutex_lock(&vdev->igate);
  419. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  420. hdr.start, hdr.count, data);
  421. mutex_unlock(&vdev->igate);
  422. kfree(data);
  423. return ret;
  424. } else if (cmd == VFIO_DEVICE_RESET) {
  425. return vdev->reset_works ?
  426. pci_try_reset_function(vdev->pdev) : -EINVAL;
  427. } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
  428. struct vfio_pci_hot_reset_info hdr;
  429. struct vfio_pci_fill_info fill = { 0 };
  430. struct vfio_pci_dependent_device *devices = NULL;
  431. bool slot = false;
  432. int ret = 0;
  433. minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
  434. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  435. return -EFAULT;
  436. if (hdr.argsz < minsz)
  437. return -EINVAL;
  438. hdr.flags = 0;
  439. /* Can we do a slot or bus reset or neither? */
  440. if (!pci_probe_reset_slot(vdev->pdev->slot))
  441. slot = true;
  442. else if (pci_probe_reset_bus(vdev->pdev->bus))
  443. return -ENODEV;
  444. /* How many devices are affected? */
  445. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  446. vfio_pci_count_devs,
  447. &fill.max, slot);
  448. if (ret)
  449. return ret;
  450. WARN_ON(!fill.max); /* Should always be at least one */
  451. /*
  452. * If there's enough space, fill it now, otherwise return
  453. * -ENOSPC and the number of devices affected.
  454. */
  455. if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
  456. ret = -ENOSPC;
  457. hdr.count = fill.max;
  458. goto reset_info_exit;
  459. }
  460. devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
  461. if (!devices)
  462. return -ENOMEM;
  463. fill.devices = devices;
  464. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  465. vfio_pci_fill_devs,
  466. &fill, slot);
  467. /*
  468. * If a device was removed between counting and filling,
  469. * we may come up short of fill.max. If a device was
  470. * added, we'll have a return of -EAGAIN above.
  471. */
  472. if (!ret)
  473. hdr.count = fill.cur;
  474. reset_info_exit:
  475. if (copy_to_user((void __user *)arg, &hdr, minsz))
  476. ret = -EFAULT;
  477. if (!ret) {
  478. if (copy_to_user((void __user *)(arg + minsz), devices,
  479. hdr.count * sizeof(*devices)))
  480. ret = -EFAULT;
  481. }
  482. kfree(devices);
  483. return ret;
  484. } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
  485. struct vfio_pci_hot_reset hdr;
  486. int32_t *group_fds;
  487. struct vfio_pci_group_entry *groups;
  488. struct vfio_pci_group_info info;
  489. bool slot = false;
  490. int i, count = 0, ret = 0;
  491. minsz = offsetofend(struct vfio_pci_hot_reset, count);
  492. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  493. return -EFAULT;
  494. if (hdr.argsz < minsz || hdr.flags)
  495. return -EINVAL;
  496. /* Can we do a slot or bus reset or neither? */
  497. if (!pci_probe_reset_slot(vdev->pdev->slot))
  498. slot = true;
  499. else if (pci_probe_reset_bus(vdev->pdev->bus))
  500. return -ENODEV;
  501. /*
  502. * We can't let userspace give us an arbitrarily large
  503. * buffer to copy, so verify how many we think there
  504. * could be. Note groups can have multiple devices so
  505. * one group per device is the max.
  506. */
  507. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  508. vfio_pci_count_devs,
  509. &count, slot);
  510. if (ret)
  511. return ret;
  512. /* Somewhere between 1 and count is OK */
  513. if (!hdr.count || hdr.count > count)
  514. return -EINVAL;
  515. group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
  516. groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
  517. if (!group_fds || !groups) {
  518. kfree(group_fds);
  519. kfree(groups);
  520. return -ENOMEM;
  521. }
  522. if (copy_from_user(group_fds, (void __user *)(arg + minsz),
  523. hdr.count * sizeof(*group_fds))) {
  524. kfree(group_fds);
  525. kfree(groups);
  526. return -EFAULT;
  527. }
  528. /*
  529. * For each group_fd, get the group through the vfio external
  530. * user interface and store the group and iommu ID. This
  531. * ensures the group is held across the reset.
  532. */
  533. for (i = 0; i < hdr.count; i++) {
  534. struct vfio_group *group;
  535. struct fd f = fdget(group_fds[i]);
  536. if (!f.file) {
  537. ret = -EBADF;
  538. break;
  539. }
  540. group = vfio_group_get_external_user(f.file);
  541. fdput(f);
  542. if (IS_ERR(group)) {
  543. ret = PTR_ERR(group);
  544. break;
  545. }
  546. groups[i].group = group;
  547. groups[i].id = vfio_external_user_iommu_id(group);
  548. }
  549. kfree(group_fds);
  550. /* release reference to groups on error */
  551. if (ret)
  552. goto hot_reset_release;
  553. info.count = hdr.count;
  554. info.groups = groups;
  555. /*
  556. * Test whether all the affected devices are contained
  557. * by the set of groups provided by the user.
  558. */
  559. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  560. vfio_pci_validate_devs,
  561. &info, slot);
  562. if (!ret)
  563. /* User has access, do the reset */
  564. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  565. pci_try_reset_bus(vdev->pdev->bus);
  566. hot_reset_release:
  567. for (i--; i >= 0; i--)
  568. vfio_group_put_external_user(groups[i].group);
  569. kfree(groups);
  570. return ret;
  571. }
  572. return -ENOTTY;
  573. }
  574. static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
  575. size_t count, loff_t *ppos, bool iswrite)
  576. {
  577. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  578. struct vfio_pci_device *vdev = device_data;
  579. if (index >= VFIO_PCI_NUM_REGIONS)
  580. return -EINVAL;
  581. switch (index) {
  582. case VFIO_PCI_CONFIG_REGION_INDEX:
  583. return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
  584. case VFIO_PCI_ROM_REGION_INDEX:
  585. if (iswrite)
  586. return -EINVAL;
  587. return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
  588. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  589. return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
  590. case VFIO_PCI_VGA_REGION_INDEX:
  591. return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
  592. }
  593. return -EINVAL;
  594. }
  595. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  596. size_t count, loff_t *ppos)
  597. {
  598. if (!count)
  599. return 0;
  600. return vfio_pci_rw(device_data, buf, count, ppos, false);
  601. }
  602. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  603. size_t count, loff_t *ppos)
  604. {
  605. if (!count)
  606. return 0;
  607. return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
  608. }
  609. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  610. {
  611. struct vfio_pci_device *vdev = device_data;
  612. struct pci_dev *pdev = vdev->pdev;
  613. unsigned int index;
  614. u64 phys_len, req_len, pgoff, req_start;
  615. int ret;
  616. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  617. if (vma->vm_end < vma->vm_start)
  618. return -EINVAL;
  619. if ((vma->vm_flags & VM_SHARED) == 0)
  620. return -EINVAL;
  621. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  622. return -EINVAL;
  623. if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
  624. return -EINVAL;
  625. phys_len = pci_resource_len(pdev, index);
  626. req_len = vma->vm_end - vma->vm_start;
  627. pgoff = vma->vm_pgoff &
  628. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  629. req_start = pgoff << PAGE_SHIFT;
  630. if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
  631. return -EINVAL;
  632. if (index == vdev->msix_bar) {
  633. /*
  634. * Disallow mmaps overlapping the MSI-X table; users don't
  635. * get to touch this directly. We could find somewhere
  636. * else to map the overlap, but page granularity is only
  637. * a recommendation, not a requirement, so the user needs
  638. * to know which bits are real. Requiring them to mmap
  639. * around the table makes that clear.
  640. */
  641. /* If neither entirely above nor below, then it overlaps */
  642. if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
  643. req_start + req_len <= vdev->msix_offset))
  644. return -EINVAL;
  645. }
  646. /*
  647. * Even though we don't make use of the barmap for the mmap,
  648. * we need to request the region and the barmap tracks that.
  649. */
  650. if (!vdev->barmap[index]) {
  651. ret = pci_request_selected_regions(pdev,
  652. 1 << index, "vfio-pci");
  653. if (ret)
  654. return ret;
  655. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  656. }
  657. vma->vm_private_data = vdev;
  658. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  659. vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  660. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  661. req_len, vma->vm_page_prot);
  662. }
  663. static void vfio_pci_request(void *device_data, unsigned int count)
  664. {
  665. struct vfio_pci_device *vdev = device_data;
  666. mutex_lock(&vdev->igate);
  667. if (vdev->req_trigger) {
  668. dev_dbg(&vdev->pdev->dev, "Requesting device from user\n");
  669. eventfd_signal(vdev->req_trigger, 1);
  670. }
  671. mutex_unlock(&vdev->igate);
  672. }
  673. static const struct vfio_device_ops vfio_pci_ops = {
  674. .name = "vfio-pci",
  675. .open = vfio_pci_open,
  676. .release = vfio_pci_release,
  677. .ioctl = vfio_pci_ioctl,
  678. .read = vfio_pci_read,
  679. .write = vfio_pci_write,
  680. .mmap = vfio_pci_mmap,
  681. .request = vfio_pci_request,
  682. };
  683. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  684. {
  685. struct vfio_pci_device *vdev;
  686. struct iommu_group *group;
  687. int ret;
  688. if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  689. return -EINVAL;
  690. group = iommu_group_get(&pdev->dev);
  691. if (!group)
  692. return -EINVAL;
  693. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  694. if (!vdev) {
  695. iommu_group_put(group);
  696. return -ENOMEM;
  697. }
  698. vdev->pdev = pdev;
  699. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  700. mutex_init(&vdev->igate);
  701. spin_lock_init(&vdev->irqlock);
  702. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  703. if (ret) {
  704. iommu_group_put(group);
  705. kfree(vdev);
  706. }
  707. return ret;
  708. }
  709. static void vfio_pci_remove(struct pci_dev *pdev)
  710. {
  711. struct vfio_pci_device *vdev;
  712. vdev = vfio_del_group_dev(&pdev->dev);
  713. if (vdev) {
  714. iommu_group_put(pdev->dev.iommu_group);
  715. kfree(vdev);
  716. }
  717. }
  718. static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
  719. pci_channel_state_t state)
  720. {
  721. struct vfio_pci_device *vdev;
  722. struct vfio_device *device;
  723. device = vfio_device_get_from_dev(&pdev->dev);
  724. if (device == NULL)
  725. return PCI_ERS_RESULT_DISCONNECT;
  726. vdev = vfio_device_data(device);
  727. if (vdev == NULL) {
  728. vfio_device_put(device);
  729. return PCI_ERS_RESULT_DISCONNECT;
  730. }
  731. mutex_lock(&vdev->igate);
  732. if (vdev->err_trigger)
  733. eventfd_signal(vdev->err_trigger, 1);
  734. mutex_unlock(&vdev->igate);
  735. vfio_device_put(device);
  736. return PCI_ERS_RESULT_CAN_RECOVER;
  737. }
  738. static struct pci_error_handlers vfio_err_handlers = {
  739. .error_detected = vfio_pci_aer_err_detected,
  740. };
  741. static struct pci_driver vfio_pci_driver = {
  742. .name = "vfio-pci",
  743. .id_table = NULL, /* only dynamic ids */
  744. .probe = vfio_pci_probe,
  745. .remove = vfio_pci_remove,
  746. .err_handler = &vfio_err_handlers,
  747. };
  748. struct vfio_devices {
  749. struct vfio_device **devices;
  750. int cur_index;
  751. int max_index;
  752. };
  753. static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
  754. {
  755. struct vfio_devices *devs = data;
  756. struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver);
  757. if (pci_drv != &vfio_pci_driver)
  758. return -EBUSY;
  759. if (devs->cur_index == devs->max_index)
  760. return -ENOSPC;
  761. devs->devices[devs->cur_index] = vfio_device_get_from_dev(&pdev->dev);
  762. if (!devs->devices[devs->cur_index])
  763. return -EINVAL;
  764. devs->cur_index++;
  765. return 0;
  766. }
  767. /*
  768. * Attempt to do a bus/slot reset if there are devices affected by a reset for
  769. * this device that are needs_reset and all of the affected devices are unused
  770. * (!refcnt). Callers are required to hold driver_lock when calling this to
  771. * prevent device opens and concurrent bus reset attempts. We prevent device
  772. * unbinds by acquiring and holding a reference to the vfio_device.
  773. *
  774. * NB: vfio-core considers a group to be viable even if some devices are
  775. * bound to drivers like pci-stub or pcieport. Here we require all devices
  776. * to be bound to vfio_pci since that's the only way we can be sure they
  777. * stay put.
  778. */
  779. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
  780. {
  781. struct vfio_devices devs = { .cur_index = 0 };
  782. int i = 0, ret = -EINVAL;
  783. bool needs_reset = false, slot = false;
  784. struct vfio_pci_device *tmp;
  785. if (!pci_probe_reset_slot(vdev->pdev->slot))
  786. slot = true;
  787. else if (pci_probe_reset_bus(vdev->pdev->bus))
  788. return;
  789. if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
  790. &i, slot) || !i)
  791. return;
  792. devs.max_index = i;
  793. devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
  794. if (!devs.devices)
  795. return;
  796. if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
  797. vfio_pci_get_devs, &devs, slot))
  798. goto put_devs;
  799. for (i = 0; i < devs.cur_index; i++) {
  800. tmp = vfio_device_data(devs.devices[i]);
  801. if (tmp->needs_reset)
  802. needs_reset = true;
  803. if (tmp->refcnt)
  804. goto put_devs;
  805. }
  806. if (needs_reset)
  807. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  808. pci_try_reset_bus(vdev->pdev->bus);
  809. put_devs:
  810. for (i = 0; i < devs.cur_index; i++) {
  811. if (!ret) {
  812. tmp = vfio_device_data(devs.devices[i]);
  813. tmp->needs_reset = false;
  814. }
  815. vfio_device_put(devs.devices[i]);
  816. }
  817. kfree(devs.devices);
  818. }
  819. static void __exit vfio_pci_cleanup(void)
  820. {
  821. pci_unregister_driver(&vfio_pci_driver);
  822. vfio_pci_virqfd_exit();
  823. vfio_pci_uninit_perm_bits();
  824. }
  825. static int __init vfio_pci_init(void)
  826. {
  827. int ret;
  828. /* Allocate shared config space permision data used by all devices */
  829. ret = vfio_pci_init_perm_bits();
  830. if (ret)
  831. return ret;
  832. /* Start the virqfd cleanup handler */
  833. ret = vfio_pci_virqfd_init();
  834. if (ret)
  835. goto out_virqfd;
  836. /* Register and scan for devices */
  837. ret = pci_register_driver(&vfio_pci_driver);
  838. if (ret)
  839. goto out_driver;
  840. return 0;
  841. out_driver:
  842. vfio_pci_virqfd_exit();
  843. out_virqfd:
  844. vfio_pci_uninit_perm_bits();
  845. return ret;
  846. }
  847. module_init(vfio_pci_init);
  848. module_exit(vfio_pci_cleanup);
  849. MODULE_VERSION(DRIVER_VERSION);
  850. MODULE_LICENSE("GPL v2");
  851. MODULE_AUTHOR(DRIVER_AUTHOR);
  852. MODULE_DESCRIPTION(DRIVER_DESC);