vfio_pci.c 22 KB

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