vfio_pci.c 23 KB

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