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