vfio_pci.c 23 KB

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