vfio_pci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. vdev = vfio_del_group_dev(&pdev->dev);
  699. if (vdev) {
  700. iommu_group_put(pdev->dev.iommu_group);
  701. kfree(vdev);
  702. }
  703. }
  704. static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
  705. pci_channel_state_t state)
  706. {
  707. struct vfio_pci_device *vdev;
  708. struct vfio_device *device;
  709. device = vfio_device_get_from_dev(&pdev->dev);
  710. if (device == NULL)
  711. return PCI_ERS_RESULT_DISCONNECT;
  712. vdev = vfio_device_data(device);
  713. if (vdev == NULL) {
  714. vfio_device_put(device);
  715. return PCI_ERS_RESULT_DISCONNECT;
  716. }
  717. mutex_lock(&vdev->igate);
  718. if (vdev->err_trigger)
  719. eventfd_signal(vdev->err_trigger, 1);
  720. mutex_unlock(&vdev->igate);
  721. vfio_device_put(device);
  722. return PCI_ERS_RESULT_CAN_RECOVER;
  723. }
  724. static struct pci_error_handlers vfio_err_handlers = {
  725. .error_detected = vfio_pci_aer_err_detected,
  726. };
  727. static struct pci_driver vfio_pci_driver = {
  728. .name = "vfio-pci",
  729. .id_table = NULL, /* only dynamic ids */
  730. .probe = vfio_pci_probe,
  731. .remove = vfio_pci_remove,
  732. .err_handler = &vfio_err_handlers,
  733. };
  734. struct vfio_devices {
  735. struct vfio_device **devices;
  736. int cur_index;
  737. int max_index;
  738. };
  739. static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
  740. {
  741. struct vfio_devices *devs = data;
  742. struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver);
  743. if (pci_drv != &vfio_pci_driver)
  744. return -EBUSY;
  745. if (devs->cur_index == devs->max_index)
  746. return -ENOSPC;
  747. devs->devices[devs->cur_index] = vfio_device_get_from_dev(&pdev->dev);
  748. if (!devs->devices[devs->cur_index])
  749. return -EINVAL;
  750. devs->cur_index++;
  751. return 0;
  752. }
  753. /*
  754. * Attempt to do a bus/slot reset if there are devices affected by a reset for
  755. * this device that are needs_reset and all of the affected devices are unused
  756. * (!refcnt). Callers are required to hold driver_lock when calling this to
  757. * prevent device opens and concurrent bus reset attempts. We prevent device
  758. * unbinds by acquiring and holding a reference to the vfio_device.
  759. *
  760. * NB: vfio-core considers a group to be viable even if some devices are
  761. * bound to drivers like pci-stub or pcieport. Here we require all devices
  762. * to be bound to vfio_pci since that's the only way we can be sure they
  763. * stay put.
  764. */
  765. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
  766. {
  767. struct vfio_devices devs = { .cur_index = 0 };
  768. int i = 0, ret = -EINVAL;
  769. bool needs_reset = false, slot = false;
  770. struct vfio_pci_device *tmp;
  771. if (!pci_probe_reset_slot(vdev->pdev->slot))
  772. slot = true;
  773. else if (pci_probe_reset_bus(vdev->pdev->bus))
  774. return;
  775. if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
  776. &i, slot) || !i)
  777. return;
  778. devs.max_index = i;
  779. devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
  780. if (!devs.devices)
  781. return;
  782. if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
  783. vfio_pci_get_devs, &devs, slot))
  784. goto put_devs;
  785. for (i = 0; i < devs.cur_index; i++) {
  786. tmp = vfio_device_data(devs.devices[i]);
  787. if (tmp->needs_reset)
  788. needs_reset = true;
  789. if (tmp->refcnt)
  790. goto put_devs;
  791. }
  792. if (needs_reset)
  793. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  794. pci_try_reset_bus(vdev->pdev->bus);
  795. put_devs:
  796. for (i = 0; i < devs.cur_index; i++) {
  797. if (!ret) {
  798. tmp = vfio_device_data(devs.devices[i]);
  799. tmp->needs_reset = false;
  800. }
  801. vfio_device_put(devs.devices[i]);
  802. }
  803. kfree(devs.devices);
  804. }
  805. static void __exit vfio_pci_cleanup(void)
  806. {
  807. pci_unregister_driver(&vfio_pci_driver);
  808. vfio_pci_virqfd_exit();
  809. vfio_pci_uninit_perm_bits();
  810. }
  811. static int __init vfio_pci_init(void)
  812. {
  813. int ret;
  814. /* Allocate shared config space permision data used by all devices */
  815. ret = vfio_pci_init_perm_bits();
  816. if (ret)
  817. return ret;
  818. /* Start the virqfd cleanup handler */
  819. ret = vfio_pci_virqfd_init();
  820. if (ret)
  821. goto out_virqfd;
  822. /* Register and scan for devices */
  823. ret = pci_register_driver(&vfio_pci_driver);
  824. if (ret)
  825. goto out_driver;
  826. return 0;
  827. out_driver:
  828. vfio_pci_virqfd_exit();
  829. out_virqfd:
  830. vfio_pci_uninit_perm_bits();
  831. return ret;
  832. }
  833. module_init(vfio_pci_init);
  834. module_exit(vfio_pci_cleanup);
  835. MODULE_VERSION(DRIVER_VERSION);
  836. MODULE_LICENSE("GPL v2");
  837. MODULE_AUTHOR(DRIVER_AUTHOR);
  838. MODULE_DESCRIPTION(DRIVER_DESC);