vfio_pci.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/device.h>
  15. #include <linux/eventfd.h>
  16. #include <linux/file.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/iommu.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/notifier.h>
  22. #include <linux/pci.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vfio.h>
  28. #include <linux/vgaarb.h>
  29. #include "vfio_pci_private.h"
  30. #define DRIVER_VERSION "0.2"
  31. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  32. #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
  33. static char ids[1024] __initdata;
  34. module_param_string(ids, ids, sizeof(ids), 0);
  35. MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the vfio driver, format is \"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\" and multiple comma separated entries can be specified");
  36. static bool nointxmask;
  37. module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
  38. MODULE_PARM_DESC(nointxmask,
  39. "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.");
  40. #ifdef CONFIG_VFIO_PCI_VGA
  41. static bool disable_vga;
  42. module_param(disable_vga, bool, S_IRUGO);
  43. MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
  44. #endif
  45. static bool disable_idle_d3;
  46. module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
  47. MODULE_PARM_DESC(disable_idle_d3,
  48. "Disable using the PCI D3 low power state for idle, unused devices");
  49. static DEFINE_MUTEX(driver_lock);
  50. static inline bool vfio_vga_disabled(void)
  51. {
  52. #ifdef CONFIG_VFIO_PCI_VGA
  53. return disable_vga;
  54. #else
  55. return true;
  56. #endif
  57. }
  58. /*
  59. * Our VGA arbiter participation is limited since we don't know anything
  60. * about the device itself. However, if the device is the only VGA device
  61. * downstream of a bridge and VFIO VGA support is disabled, then we can
  62. * safely return legacy VGA IO and memory as not decoded since the user
  63. * has no way to get to it and routing can be disabled externally at the
  64. * bridge.
  65. */
  66. static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
  67. {
  68. struct vfio_pci_device *vdev = opaque;
  69. struct pci_dev *tmp = NULL, *pdev = vdev->pdev;
  70. unsigned char max_busnr;
  71. unsigned int decodes;
  72. if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
  73. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
  74. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  75. max_busnr = pci_bus_max_busnr(pdev->bus);
  76. decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  77. while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
  78. if (tmp == pdev ||
  79. pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
  80. pci_is_root_bus(tmp->bus))
  81. continue;
  82. if (tmp->bus->number >= pdev->bus->number &&
  83. tmp->bus->number <= max_busnr) {
  84. pci_dev_put(tmp);
  85. decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  86. break;
  87. }
  88. }
  89. return decodes;
  90. }
  91. static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
  92. {
  93. return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
  94. }
  95. static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev)
  96. {
  97. struct resource *res;
  98. int bar;
  99. struct vfio_pci_dummy_resource *dummy_res;
  100. INIT_LIST_HEAD(&vdev->dummy_resources_list);
  101. for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
  102. res = vdev->pdev->resource + bar;
  103. if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
  104. goto no_mmap;
  105. if (!(res->flags & IORESOURCE_MEM))
  106. goto no_mmap;
  107. /*
  108. * The PCI core shouldn't set up a resource with a
  109. * type but zero size. But there may be bugs that
  110. * cause us to do that.
  111. */
  112. if (!resource_size(res))
  113. goto no_mmap;
  114. if (resource_size(res) >= PAGE_SIZE) {
  115. vdev->bar_mmap_supported[bar] = true;
  116. continue;
  117. }
  118. if (!(res->start & ~PAGE_MASK)) {
  119. /*
  120. * Add a dummy resource to reserve the remainder
  121. * of the exclusive page in case that hot-add
  122. * device's bar is assigned into it.
  123. */
  124. dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL);
  125. if (dummy_res == NULL)
  126. goto no_mmap;
  127. dummy_res->resource.name = "vfio sub-page reserved";
  128. dummy_res->resource.start = res->end + 1;
  129. dummy_res->resource.end = res->start + PAGE_SIZE - 1;
  130. dummy_res->resource.flags = res->flags;
  131. if (request_resource(res->parent,
  132. &dummy_res->resource)) {
  133. kfree(dummy_res);
  134. goto no_mmap;
  135. }
  136. dummy_res->index = bar;
  137. list_add(&dummy_res->res_next,
  138. &vdev->dummy_resources_list);
  139. vdev->bar_mmap_supported[bar] = true;
  140. continue;
  141. }
  142. /*
  143. * Here we don't handle the case when the BAR is not page
  144. * aligned because we can't expect the BAR will be
  145. * assigned into the same location in a page in guest
  146. * when we passthrough the BAR. And it's hard to access
  147. * this BAR in userspace because we have no way to get
  148. * the BAR's location in a page.
  149. */
  150. no_mmap:
  151. vdev->bar_mmap_supported[bar] = false;
  152. }
  153. }
  154. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
  155. static void vfio_pci_disable(struct vfio_pci_device *vdev);
  156. /*
  157. * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
  158. * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
  159. * If a device implements the former but not the latter we would typically
  160. * expect broken_intx_masking be set and require an exclusive interrupt.
  161. * However since we do have control of the device's ability to assert INTx,
  162. * we can instead pretend that the device does not implement INTx, virtualizing
  163. * the pin register to report zero and maintaining DisINTx set on the host.
  164. */
  165. static bool vfio_pci_nointx(struct pci_dev *pdev)
  166. {
  167. switch (pdev->vendor) {
  168. case PCI_VENDOR_ID_INTEL:
  169. switch (pdev->device) {
  170. /* All i40e (XL710/X710) 10/20/40GbE NICs */
  171. case 0x1572:
  172. case 0x1574:
  173. case 0x1580 ... 0x1581:
  174. case 0x1583 ... 0x1589:
  175. case 0x37d0 ... 0x37d2:
  176. return true;
  177. default:
  178. return false;
  179. }
  180. }
  181. return false;
  182. }
  183. static int vfio_pci_enable(struct vfio_pci_device *vdev)
  184. {
  185. struct pci_dev *pdev = vdev->pdev;
  186. int ret;
  187. u16 cmd;
  188. u8 msix_pos;
  189. pci_set_power_state(pdev, PCI_D0);
  190. /* Don't allow our initial saved state to include busmaster */
  191. pci_clear_master(pdev);
  192. ret = pci_enable_device(pdev);
  193. if (ret)
  194. return ret;
  195. vdev->reset_works = (pci_reset_function(pdev) == 0);
  196. pci_save_state(pdev);
  197. vdev->pci_saved_state = pci_store_saved_state(pdev);
  198. if (!vdev->pci_saved_state)
  199. pr_debug("%s: Couldn't store %s saved state\n",
  200. __func__, dev_name(&pdev->dev));
  201. if (likely(!nointxmask)) {
  202. if (vfio_pci_nointx(pdev)) {
  203. dev_info(&pdev->dev, "Masking broken INTx support\n");
  204. vdev->nointx = true;
  205. pci_intx(pdev, 0);
  206. } else
  207. vdev->pci_2_3 = pci_intx_mask_supported(pdev);
  208. }
  209. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  210. if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
  211. cmd &= ~PCI_COMMAND_INTX_DISABLE;
  212. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  213. }
  214. ret = vfio_config_init(vdev);
  215. if (ret) {
  216. kfree(vdev->pci_saved_state);
  217. vdev->pci_saved_state = NULL;
  218. pci_disable_device(pdev);
  219. return ret;
  220. }
  221. msix_pos = pdev->msix_cap;
  222. if (msix_pos) {
  223. u16 flags;
  224. u32 table;
  225. pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
  226. pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
  227. vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
  228. vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
  229. vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
  230. } else
  231. vdev->msix_bar = 0xFF;
  232. if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
  233. vdev->has_vga = true;
  234. if (vfio_pci_is_vga(pdev) &&
  235. pdev->vendor == PCI_VENDOR_ID_INTEL &&
  236. IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
  237. ret = vfio_pci_igd_init(vdev);
  238. if (ret) {
  239. dev_warn(&vdev->pdev->dev,
  240. "Failed to setup Intel IGD regions\n");
  241. vfio_pci_disable(vdev);
  242. return ret;
  243. }
  244. }
  245. vfio_pci_probe_mmaps(vdev);
  246. return 0;
  247. }
  248. static void vfio_pci_disable(struct vfio_pci_device *vdev)
  249. {
  250. struct pci_dev *pdev = vdev->pdev;
  251. struct vfio_pci_dummy_resource *dummy_res, *tmp;
  252. int i, bar;
  253. /* Stop the device from further DMA */
  254. pci_clear_master(pdev);
  255. vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
  256. VFIO_IRQ_SET_ACTION_TRIGGER,
  257. vdev->irq_type, 0, 0, NULL);
  258. vdev->virq_disabled = false;
  259. for (i = 0; i < vdev->num_regions; i++)
  260. vdev->region[i].ops->release(vdev, &vdev->region[i]);
  261. vdev->num_regions = 0;
  262. kfree(vdev->region);
  263. vdev->region = NULL; /* don't krealloc a freed pointer */
  264. vfio_config_free(vdev);
  265. for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
  266. if (!vdev->barmap[bar])
  267. continue;
  268. pci_iounmap(pdev, vdev->barmap[bar]);
  269. pci_release_selected_regions(pdev, 1 << bar);
  270. vdev->barmap[bar] = NULL;
  271. }
  272. list_for_each_entry_safe(dummy_res, tmp,
  273. &vdev->dummy_resources_list, res_next) {
  274. list_del(&dummy_res->res_next);
  275. release_resource(&dummy_res->resource);
  276. kfree(dummy_res);
  277. }
  278. vdev->needs_reset = true;
  279. /*
  280. * If we have saved state, restore it. If we can reset the device,
  281. * even better. Resetting with current state seems better than
  282. * nothing, but saving and restoring current state without reset
  283. * is just busy work.
  284. */
  285. if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
  286. pr_info("%s: Couldn't reload %s saved state\n",
  287. __func__, dev_name(&pdev->dev));
  288. if (!vdev->reset_works)
  289. goto out;
  290. pci_save_state(pdev);
  291. }
  292. /*
  293. * Disable INTx and MSI, presumably to avoid spurious interrupts
  294. * during reset. Stolen from pci_reset_function()
  295. */
  296. pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  297. /*
  298. * Try to reset the device. The success of this is dependent on
  299. * being able to lock the device, which is not always possible.
  300. */
  301. if (vdev->reset_works && !pci_try_reset_function(pdev))
  302. vdev->needs_reset = false;
  303. pci_restore_state(pdev);
  304. out:
  305. pci_disable_device(pdev);
  306. vfio_pci_try_bus_reset(vdev);
  307. if (!disable_idle_d3)
  308. pci_set_power_state(pdev, PCI_D3hot);
  309. }
  310. static void vfio_pci_release(void *device_data)
  311. {
  312. struct vfio_pci_device *vdev = device_data;
  313. mutex_lock(&driver_lock);
  314. if (!(--vdev->refcnt)) {
  315. vfio_spapr_pci_eeh_release(vdev->pdev);
  316. vfio_pci_disable(vdev);
  317. }
  318. mutex_unlock(&driver_lock);
  319. module_put(THIS_MODULE);
  320. }
  321. static int vfio_pci_open(void *device_data)
  322. {
  323. struct vfio_pci_device *vdev = device_data;
  324. int ret = 0;
  325. if (!try_module_get(THIS_MODULE))
  326. return -ENODEV;
  327. mutex_lock(&driver_lock);
  328. if (!vdev->refcnt) {
  329. ret = vfio_pci_enable(vdev);
  330. if (ret)
  331. goto error;
  332. vfio_spapr_pci_eeh_open(vdev->pdev);
  333. }
  334. vdev->refcnt++;
  335. error:
  336. mutex_unlock(&driver_lock);
  337. if (ret)
  338. module_put(THIS_MODULE);
  339. return ret;
  340. }
  341. static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
  342. {
  343. if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
  344. u8 pin;
  345. pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
  346. if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && !vdev->nointx && pin)
  347. return 1;
  348. } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
  349. u8 pos;
  350. u16 flags;
  351. pos = vdev->pdev->msi_cap;
  352. if (pos) {
  353. pci_read_config_word(vdev->pdev,
  354. pos + PCI_MSI_FLAGS, &flags);
  355. return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
  356. }
  357. } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
  358. u8 pos;
  359. u16 flags;
  360. pos = vdev->pdev->msix_cap;
  361. if (pos) {
  362. pci_read_config_word(vdev->pdev,
  363. pos + PCI_MSIX_FLAGS, &flags);
  364. return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
  365. }
  366. } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
  367. if (pci_is_pcie(vdev->pdev))
  368. return 1;
  369. } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
  370. return 1;
  371. }
  372. return 0;
  373. }
  374. static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
  375. {
  376. (*(int *)data)++;
  377. return 0;
  378. }
  379. struct vfio_pci_fill_info {
  380. int max;
  381. int cur;
  382. struct vfio_pci_dependent_device *devices;
  383. };
  384. static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
  385. {
  386. struct vfio_pci_fill_info *fill = data;
  387. struct iommu_group *iommu_group;
  388. if (fill->cur == fill->max)
  389. return -EAGAIN; /* Something changed, try again */
  390. iommu_group = iommu_group_get(&pdev->dev);
  391. if (!iommu_group)
  392. return -EPERM; /* Cannot reset non-isolated devices */
  393. fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
  394. fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
  395. fill->devices[fill->cur].bus = pdev->bus->number;
  396. fill->devices[fill->cur].devfn = pdev->devfn;
  397. fill->cur++;
  398. iommu_group_put(iommu_group);
  399. return 0;
  400. }
  401. struct vfio_pci_group_entry {
  402. struct vfio_group *group;
  403. int id;
  404. };
  405. struct vfio_pci_group_info {
  406. int count;
  407. struct vfio_pci_group_entry *groups;
  408. };
  409. static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
  410. {
  411. struct vfio_pci_group_info *info = data;
  412. struct iommu_group *group;
  413. int id, i;
  414. group = iommu_group_get(&pdev->dev);
  415. if (!group)
  416. return -EPERM;
  417. id = iommu_group_id(group);
  418. for (i = 0; i < info->count; i++)
  419. if (info->groups[i].id == id)
  420. break;
  421. iommu_group_put(group);
  422. return (i == info->count) ? -EINVAL : 0;
  423. }
  424. static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
  425. {
  426. for (; pdev; pdev = pdev->bus->self)
  427. if (pdev->bus == slot->bus)
  428. return (pdev->slot == slot);
  429. return false;
  430. }
  431. struct vfio_pci_walk_info {
  432. int (*fn)(struct pci_dev *, void *data);
  433. void *data;
  434. struct pci_dev *pdev;
  435. bool slot;
  436. int ret;
  437. };
  438. static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
  439. {
  440. struct vfio_pci_walk_info *walk = data;
  441. if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
  442. walk->ret = walk->fn(pdev, walk->data);
  443. return walk->ret;
  444. }
  445. static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
  446. int (*fn)(struct pci_dev *,
  447. void *data), void *data,
  448. bool slot)
  449. {
  450. struct vfio_pci_walk_info walk = {
  451. .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
  452. };
  453. pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
  454. return walk.ret;
  455. }
  456. static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
  457. struct vfio_info_cap *caps)
  458. {
  459. struct vfio_info_cap_header *header;
  460. struct vfio_region_info_cap_sparse_mmap *sparse;
  461. size_t end, size;
  462. int nr_areas = 2, i = 0;
  463. end = pci_resource_len(vdev->pdev, vdev->msix_bar);
  464. /* If MSI-X table is aligned to the start or end, only one area */
  465. if (((vdev->msix_offset & PAGE_MASK) == 0) ||
  466. (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
  467. nr_areas = 1;
  468. size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
  469. header = vfio_info_cap_add(caps, size,
  470. VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
  471. if (IS_ERR(header))
  472. return PTR_ERR(header);
  473. sparse = container_of(header,
  474. struct vfio_region_info_cap_sparse_mmap, header);
  475. sparse->nr_areas = nr_areas;
  476. if (vdev->msix_offset & PAGE_MASK) {
  477. sparse->areas[i].offset = 0;
  478. sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
  479. i++;
  480. }
  481. if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
  482. sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
  483. vdev->msix_size);
  484. sparse->areas[i].size = end - sparse->areas[i].offset;
  485. i++;
  486. }
  487. return 0;
  488. }
  489. static int region_type_cap(struct vfio_pci_device *vdev,
  490. struct vfio_info_cap *caps,
  491. unsigned int type, unsigned int subtype)
  492. {
  493. struct vfio_info_cap_header *header;
  494. struct vfio_region_info_cap_type *cap;
  495. header = vfio_info_cap_add(caps, sizeof(*cap),
  496. VFIO_REGION_INFO_CAP_TYPE, 1);
  497. if (IS_ERR(header))
  498. return PTR_ERR(header);
  499. cap = container_of(header, struct vfio_region_info_cap_type, header);
  500. cap->type = type;
  501. cap->subtype = subtype;
  502. return 0;
  503. }
  504. int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
  505. unsigned int type, unsigned int subtype,
  506. const struct vfio_pci_regops *ops,
  507. size_t size, u32 flags, void *data)
  508. {
  509. struct vfio_pci_region *region;
  510. region = krealloc(vdev->region,
  511. (vdev->num_regions + 1) * sizeof(*region),
  512. GFP_KERNEL);
  513. if (!region)
  514. return -ENOMEM;
  515. vdev->region = region;
  516. vdev->region[vdev->num_regions].type = type;
  517. vdev->region[vdev->num_regions].subtype = subtype;
  518. vdev->region[vdev->num_regions].ops = ops;
  519. vdev->region[vdev->num_regions].size = size;
  520. vdev->region[vdev->num_regions].flags = flags;
  521. vdev->region[vdev->num_regions].data = data;
  522. vdev->num_regions++;
  523. return 0;
  524. }
  525. static long vfio_pci_ioctl(void *device_data,
  526. unsigned int cmd, unsigned long arg)
  527. {
  528. struct vfio_pci_device *vdev = device_data;
  529. unsigned long minsz;
  530. if (cmd == VFIO_DEVICE_GET_INFO) {
  531. struct vfio_device_info info;
  532. minsz = offsetofend(struct vfio_device_info, num_irqs);
  533. if (copy_from_user(&info, (void __user *)arg, minsz))
  534. return -EFAULT;
  535. if (info.argsz < minsz)
  536. return -EINVAL;
  537. info.flags = VFIO_DEVICE_FLAGS_PCI;
  538. if (vdev->reset_works)
  539. info.flags |= VFIO_DEVICE_FLAGS_RESET;
  540. info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
  541. info.num_irqs = VFIO_PCI_NUM_IRQS;
  542. return copy_to_user((void __user *)arg, &info, minsz) ?
  543. -EFAULT : 0;
  544. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  545. struct pci_dev *pdev = vdev->pdev;
  546. struct vfio_region_info info;
  547. struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
  548. int i, ret;
  549. minsz = offsetofend(struct vfio_region_info, offset);
  550. if (copy_from_user(&info, (void __user *)arg, minsz))
  551. return -EFAULT;
  552. if (info.argsz < minsz)
  553. return -EINVAL;
  554. switch (info.index) {
  555. case VFIO_PCI_CONFIG_REGION_INDEX:
  556. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  557. info.size = pdev->cfg_size;
  558. info.flags = VFIO_REGION_INFO_FLAG_READ |
  559. VFIO_REGION_INFO_FLAG_WRITE;
  560. break;
  561. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  562. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  563. info.size = pci_resource_len(pdev, info.index);
  564. if (!info.size) {
  565. info.flags = 0;
  566. break;
  567. }
  568. info.flags = VFIO_REGION_INFO_FLAG_READ |
  569. VFIO_REGION_INFO_FLAG_WRITE;
  570. if (vdev->bar_mmap_supported[info.index]) {
  571. info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
  572. if (info.index == vdev->msix_bar) {
  573. ret = msix_sparse_mmap_cap(vdev, &caps);
  574. if (ret)
  575. return ret;
  576. }
  577. }
  578. break;
  579. case VFIO_PCI_ROM_REGION_INDEX:
  580. {
  581. void __iomem *io;
  582. size_t size;
  583. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  584. info.flags = 0;
  585. /* Report the BAR size, not the ROM size */
  586. info.size = pci_resource_len(pdev, info.index);
  587. if (!info.size) {
  588. /* Shadow ROMs appear as PCI option ROMs */
  589. if (pdev->resource[PCI_ROM_RESOURCE].flags &
  590. IORESOURCE_ROM_SHADOW)
  591. info.size = 0x20000;
  592. else
  593. break;
  594. }
  595. /* Is it really there? */
  596. io = pci_map_rom(pdev, &size);
  597. if (!io || !size) {
  598. info.size = 0;
  599. break;
  600. }
  601. pci_unmap_rom(pdev, io);
  602. info.flags = VFIO_REGION_INFO_FLAG_READ;
  603. break;
  604. }
  605. case VFIO_PCI_VGA_REGION_INDEX:
  606. if (!vdev->has_vga)
  607. return -EINVAL;
  608. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  609. info.size = 0xc0000;
  610. info.flags = VFIO_REGION_INFO_FLAG_READ |
  611. VFIO_REGION_INFO_FLAG_WRITE;
  612. break;
  613. default:
  614. if (info.index >=
  615. VFIO_PCI_NUM_REGIONS + vdev->num_regions)
  616. return -EINVAL;
  617. i = info.index - VFIO_PCI_NUM_REGIONS;
  618. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  619. info.size = vdev->region[i].size;
  620. info.flags = vdev->region[i].flags;
  621. ret = region_type_cap(vdev, &caps,
  622. vdev->region[i].type,
  623. vdev->region[i].subtype);
  624. if (ret)
  625. return ret;
  626. }
  627. if (caps.size) {
  628. info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
  629. if (info.argsz < sizeof(info) + caps.size) {
  630. info.argsz = sizeof(info) + caps.size;
  631. info.cap_offset = 0;
  632. } else {
  633. vfio_info_cap_shift(&caps, sizeof(info));
  634. if (copy_to_user((void __user *)arg +
  635. sizeof(info), caps.buf,
  636. caps.size)) {
  637. kfree(caps.buf);
  638. return -EFAULT;
  639. }
  640. info.cap_offset = sizeof(info);
  641. }
  642. kfree(caps.buf);
  643. }
  644. return copy_to_user((void __user *)arg, &info, minsz) ?
  645. -EFAULT : 0;
  646. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  647. struct vfio_irq_info info;
  648. minsz = offsetofend(struct vfio_irq_info, count);
  649. if (copy_from_user(&info, (void __user *)arg, minsz))
  650. return -EFAULT;
  651. if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
  652. return -EINVAL;
  653. switch (info.index) {
  654. case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
  655. case VFIO_PCI_REQ_IRQ_INDEX:
  656. break;
  657. case VFIO_PCI_ERR_IRQ_INDEX:
  658. if (pci_is_pcie(vdev->pdev))
  659. break;
  660. /* pass thru to return error */
  661. default:
  662. return -EINVAL;
  663. }
  664. info.flags = VFIO_IRQ_INFO_EVENTFD;
  665. info.count = vfio_pci_get_irq_count(vdev, info.index);
  666. if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
  667. info.flags |= (VFIO_IRQ_INFO_MASKABLE |
  668. VFIO_IRQ_INFO_AUTOMASKED);
  669. else
  670. info.flags |= VFIO_IRQ_INFO_NORESIZE;
  671. return copy_to_user((void __user *)arg, &info, minsz) ?
  672. -EFAULT : 0;
  673. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  674. struct vfio_irq_set hdr;
  675. u8 *data = NULL;
  676. int ret = 0;
  677. minsz = offsetofend(struct vfio_irq_set, count);
  678. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  679. return -EFAULT;
  680. if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
  681. hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  682. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  683. return -EINVAL;
  684. if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
  685. size_t size;
  686. int max = vfio_pci_get_irq_count(vdev, hdr.index);
  687. if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
  688. size = sizeof(uint8_t);
  689. else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
  690. size = sizeof(int32_t);
  691. else
  692. return -EINVAL;
  693. if (hdr.argsz - minsz < hdr.count * size ||
  694. hdr.start >= max || hdr.start + hdr.count > max)
  695. return -EINVAL;
  696. data = memdup_user((void __user *)(arg + minsz),
  697. hdr.count * size);
  698. if (IS_ERR(data))
  699. return PTR_ERR(data);
  700. }
  701. mutex_lock(&vdev->igate);
  702. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  703. hdr.start, hdr.count, data);
  704. mutex_unlock(&vdev->igate);
  705. kfree(data);
  706. return ret;
  707. } else if (cmd == VFIO_DEVICE_RESET) {
  708. return vdev->reset_works ?
  709. pci_try_reset_function(vdev->pdev) : -EINVAL;
  710. } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
  711. struct vfio_pci_hot_reset_info hdr;
  712. struct vfio_pci_fill_info fill = { 0 };
  713. struct vfio_pci_dependent_device *devices = NULL;
  714. bool slot = false;
  715. int ret = 0;
  716. minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
  717. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  718. return -EFAULT;
  719. if (hdr.argsz < minsz)
  720. return -EINVAL;
  721. hdr.flags = 0;
  722. /* Can we do a slot or bus reset or neither? */
  723. if (!pci_probe_reset_slot(vdev->pdev->slot))
  724. slot = true;
  725. else if (pci_probe_reset_bus(vdev->pdev->bus))
  726. return -ENODEV;
  727. /* How many devices are affected? */
  728. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  729. vfio_pci_count_devs,
  730. &fill.max, slot);
  731. if (ret)
  732. return ret;
  733. WARN_ON(!fill.max); /* Should always be at least one */
  734. /*
  735. * If there's enough space, fill it now, otherwise return
  736. * -ENOSPC and the number of devices affected.
  737. */
  738. if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
  739. ret = -ENOSPC;
  740. hdr.count = fill.max;
  741. goto reset_info_exit;
  742. }
  743. devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
  744. if (!devices)
  745. return -ENOMEM;
  746. fill.devices = devices;
  747. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  748. vfio_pci_fill_devs,
  749. &fill, slot);
  750. /*
  751. * If a device was removed between counting and filling,
  752. * we may come up short of fill.max. If a device was
  753. * added, we'll have a return of -EAGAIN above.
  754. */
  755. if (!ret)
  756. hdr.count = fill.cur;
  757. reset_info_exit:
  758. if (copy_to_user((void __user *)arg, &hdr, minsz))
  759. ret = -EFAULT;
  760. if (!ret) {
  761. if (copy_to_user((void __user *)(arg + minsz), devices,
  762. hdr.count * sizeof(*devices)))
  763. ret = -EFAULT;
  764. }
  765. kfree(devices);
  766. return ret;
  767. } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
  768. struct vfio_pci_hot_reset hdr;
  769. int32_t *group_fds;
  770. struct vfio_pci_group_entry *groups;
  771. struct vfio_pci_group_info info;
  772. bool slot = false;
  773. int i, count = 0, ret = 0;
  774. minsz = offsetofend(struct vfio_pci_hot_reset, count);
  775. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  776. return -EFAULT;
  777. if (hdr.argsz < minsz || hdr.flags)
  778. return -EINVAL;
  779. /* Can we do a slot or bus reset or neither? */
  780. if (!pci_probe_reset_slot(vdev->pdev->slot))
  781. slot = true;
  782. else if (pci_probe_reset_bus(vdev->pdev->bus))
  783. return -ENODEV;
  784. /*
  785. * We can't let userspace give us an arbitrarily large
  786. * buffer to copy, so verify how many we think there
  787. * could be. Note groups can have multiple devices so
  788. * one group per device is the max.
  789. */
  790. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  791. vfio_pci_count_devs,
  792. &count, slot);
  793. if (ret)
  794. return ret;
  795. /* Somewhere between 1 and count is OK */
  796. if (!hdr.count || hdr.count > count)
  797. return -EINVAL;
  798. group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
  799. groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
  800. if (!group_fds || !groups) {
  801. kfree(group_fds);
  802. kfree(groups);
  803. return -ENOMEM;
  804. }
  805. if (copy_from_user(group_fds, (void __user *)(arg + minsz),
  806. hdr.count * sizeof(*group_fds))) {
  807. kfree(group_fds);
  808. kfree(groups);
  809. return -EFAULT;
  810. }
  811. /*
  812. * For each group_fd, get the group through the vfio external
  813. * user interface and store the group and iommu ID. This
  814. * ensures the group is held across the reset.
  815. */
  816. for (i = 0; i < hdr.count; i++) {
  817. struct vfio_group *group;
  818. struct fd f = fdget(group_fds[i]);
  819. if (!f.file) {
  820. ret = -EBADF;
  821. break;
  822. }
  823. group = vfio_group_get_external_user(f.file);
  824. fdput(f);
  825. if (IS_ERR(group)) {
  826. ret = PTR_ERR(group);
  827. break;
  828. }
  829. groups[i].group = group;
  830. groups[i].id = vfio_external_user_iommu_id(group);
  831. }
  832. kfree(group_fds);
  833. /* release reference to groups on error */
  834. if (ret)
  835. goto hot_reset_release;
  836. info.count = hdr.count;
  837. info.groups = groups;
  838. /*
  839. * Test whether all the affected devices are contained
  840. * by the set of groups provided by the user.
  841. */
  842. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  843. vfio_pci_validate_devs,
  844. &info, slot);
  845. if (!ret)
  846. /* User has access, do the reset */
  847. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  848. pci_try_reset_bus(vdev->pdev->bus);
  849. hot_reset_release:
  850. for (i--; i >= 0; i--)
  851. vfio_group_put_external_user(groups[i].group);
  852. kfree(groups);
  853. return ret;
  854. }
  855. return -ENOTTY;
  856. }
  857. static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
  858. size_t count, loff_t *ppos, bool iswrite)
  859. {
  860. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  861. struct vfio_pci_device *vdev = device_data;
  862. if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
  863. return -EINVAL;
  864. switch (index) {
  865. case VFIO_PCI_CONFIG_REGION_INDEX:
  866. return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
  867. case VFIO_PCI_ROM_REGION_INDEX:
  868. if (iswrite)
  869. return -EINVAL;
  870. return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
  871. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  872. return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
  873. case VFIO_PCI_VGA_REGION_INDEX:
  874. return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
  875. default:
  876. index -= VFIO_PCI_NUM_REGIONS;
  877. return vdev->region[index].ops->rw(vdev, buf,
  878. count, ppos, iswrite);
  879. }
  880. return -EINVAL;
  881. }
  882. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  883. size_t count, loff_t *ppos)
  884. {
  885. if (!count)
  886. return 0;
  887. return vfio_pci_rw(device_data, buf, count, ppos, false);
  888. }
  889. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  890. size_t count, loff_t *ppos)
  891. {
  892. if (!count)
  893. return 0;
  894. return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
  895. }
  896. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  897. {
  898. struct vfio_pci_device *vdev = device_data;
  899. struct pci_dev *pdev = vdev->pdev;
  900. unsigned int index;
  901. u64 phys_len, req_len, pgoff, req_start;
  902. int ret;
  903. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  904. if (vma->vm_end < vma->vm_start)
  905. return -EINVAL;
  906. if ((vma->vm_flags & VM_SHARED) == 0)
  907. return -EINVAL;
  908. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  909. return -EINVAL;
  910. if (!vdev->bar_mmap_supported[index])
  911. return -EINVAL;
  912. phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
  913. req_len = vma->vm_end - vma->vm_start;
  914. pgoff = vma->vm_pgoff &
  915. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  916. req_start = pgoff << PAGE_SHIFT;
  917. if (req_start + req_len > phys_len)
  918. return -EINVAL;
  919. if (index == vdev->msix_bar) {
  920. /*
  921. * Disallow mmaps overlapping the MSI-X table; users don't
  922. * get to touch this directly. We could find somewhere
  923. * else to map the overlap, but page granularity is only
  924. * a recommendation, not a requirement, so the user needs
  925. * to know which bits are real. Requiring them to mmap
  926. * around the table makes that clear.
  927. */
  928. /* If neither entirely above nor below, then it overlaps */
  929. if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
  930. req_start + req_len <= vdev->msix_offset))
  931. return -EINVAL;
  932. }
  933. /*
  934. * Even though we don't make use of the barmap for the mmap,
  935. * we need to request the region and the barmap tracks that.
  936. */
  937. if (!vdev->barmap[index]) {
  938. ret = pci_request_selected_regions(pdev,
  939. 1 << index, "vfio-pci");
  940. if (ret)
  941. return ret;
  942. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  943. }
  944. vma->vm_private_data = vdev;
  945. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  946. vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  947. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  948. req_len, vma->vm_page_prot);
  949. }
  950. static void vfio_pci_request(void *device_data, unsigned int count)
  951. {
  952. struct vfio_pci_device *vdev = device_data;
  953. mutex_lock(&vdev->igate);
  954. if (vdev->req_trigger) {
  955. if (!(count % 10))
  956. dev_notice_ratelimited(&vdev->pdev->dev,
  957. "Relaying device request to user (#%u)\n",
  958. count);
  959. eventfd_signal(vdev->req_trigger, 1);
  960. } else if (count == 0) {
  961. dev_warn(&vdev->pdev->dev,
  962. "No device request channel registered, blocked until released by user\n");
  963. }
  964. mutex_unlock(&vdev->igate);
  965. }
  966. static const struct vfio_device_ops vfio_pci_ops = {
  967. .name = "vfio-pci",
  968. .open = vfio_pci_open,
  969. .release = vfio_pci_release,
  970. .ioctl = vfio_pci_ioctl,
  971. .read = vfio_pci_read,
  972. .write = vfio_pci_write,
  973. .mmap = vfio_pci_mmap,
  974. .request = vfio_pci_request,
  975. };
  976. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  977. {
  978. struct vfio_pci_device *vdev;
  979. struct iommu_group *group;
  980. int ret;
  981. if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  982. return -EINVAL;
  983. group = vfio_iommu_group_get(&pdev->dev);
  984. if (!group)
  985. return -EINVAL;
  986. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  987. if (!vdev) {
  988. vfio_iommu_group_put(group, &pdev->dev);
  989. return -ENOMEM;
  990. }
  991. vdev->pdev = pdev;
  992. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  993. mutex_init(&vdev->igate);
  994. spin_lock_init(&vdev->irqlock);
  995. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  996. if (ret) {
  997. vfio_iommu_group_put(group, &pdev->dev);
  998. kfree(vdev);
  999. return ret;
  1000. }
  1001. if (vfio_pci_is_vga(pdev)) {
  1002. vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
  1003. vga_set_legacy_decoding(pdev,
  1004. vfio_pci_set_vga_decode(vdev, false));
  1005. }
  1006. if (!disable_idle_d3) {
  1007. /*
  1008. * pci-core sets the device power state to an unknown value at
  1009. * bootup and after being removed from a driver. The only
  1010. * transition it allows from this unknown state is to D0, which
  1011. * typically happens when a driver calls pci_enable_device().
  1012. * We're not ready to enable the device yet, but we do want to
  1013. * be able to get to D3. Therefore first do a D0 transition
  1014. * before going to D3.
  1015. */
  1016. pci_set_power_state(pdev, PCI_D0);
  1017. pci_set_power_state(pdev, PCI_D3hot);
  1018. }
  1019. return ret;
  1020. }
  1021. static void vfio_pci_remove(struct pci_dev *pdev)
  1022. {
  1023. struct vfio_pci_device *vdev;
  1024. vdev = vfio_del_group_dev(&pdev->dev);
  1025. if (!vdev)
  1026. return;
  1027. vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
  1028. kfree(vdev->region);
  1029. kfree(vdev);
  1030. if (vfio_pci_is_vga(pdev)) {
  1031. vga_client_register(pdev, NULL, NULL, NULL);
  1032. vga_set_legacy_decoding(pdev,
  1033. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
  1034. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
  1035. }
  1036. if (!disable_idle_d3)
  1037. pci_set_power_state(pdev, PCI_D0);
  1038. }
  1039. static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
  1040. pci_channel_state_t state)
  1041. {
  1042. struct vfio_pci_device *vdev;
  1043. struct vfio_device *device;
  1044. device = vfio_device_get_from_dev(&pdev->dev);
  1045. if (device == NULL)
  1046. return PCI_ERS_RESULT_DISCONNECT;
  1047. vdev = vfio_device_data(device);
  1048. if (vdev == NULL) {
  1049. vfio_device_put(device);
  1050. return PCI_ERS_RESULT_DISCONNECT;
  1051. }
  1052. mutex_lock(&vdev->igate);
  1053. if (vdev->err_trigger)
  1054. eventfd_signal(vdev->err_trigger, 1);
  1055. mutex_unlock(&vdev->igate);
  1056. vfio_device_put(device);
  1057. return PCI_ERS_RESULT_CAN_RECOVER;
  1058. }
  1059. static const struct pci_error_handlers vfio_err_handlers = {
  1060. .error_detected = vfio_pci_aer_err_detected,
  1061. };
  1062. static struct pci_driver vfio_pci_driver = {
  1063. .name = "vfio-pci",
  1064. .id_table = NULL, /* only dynamic ids */
  1065. .probe = vfio_pci_probe,
  1066. .remove = vfio_pci_remove,
  1067. .err_handler = &vfio_err_handlers,
  1068. };
  1069. struct vfio_devices {
  1070. struct vfio_device **devices;
  1071. int cur_index;
  1072. int max_index;
  1073. };
  1074. static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
  1075. {
  1076. struct vfio_devices *devs = data;
  1077. struct vfio_device *device;
  1078. if (devs->cur_index == devs->max_index)
  1079. return -ENOSPC;
  1080. device = vfio_device_get_from_dev(&pdev->dev);
  1081. if (!device)
  1082. return -EINVAL;
  1083. if (pci_dev_driver(pdev) != &vfio_pci_driver) {
  1084. vfio_device_put(device);
  1085. return -EBUSY;
  1086. }
  1087. devs->devices[devs->cur_index++] = device;
  1088. return 0;
  1089. }
  1090. /*
  1091. * Attempt to do a bus/slot reset if there are devices affected by a reset for
  1092. * this device that are needs_reset and all of the affected devices are unused
  1093. * (!refcnt). Callers are required to hold driver_lock when calling this to
  1094. * prevent device opens and concurrent bus reset attempts. We prevent device
  1095. * unbinds by acquiring and holding a reference to the vfio_device.
  1096. *
  1097. * NB: vfio-core considers a group to be viable even if some devices are
  1098. * bound to drivers like pci-stub or pcieport. Here we require all devices
  1099. * to be bound to vfio_pci since that's the only way we can be sure they
  1100. * stay put.
  1101. */
  1102. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
  1103. {
  1104. struct vfio_devices devs = { .cur_index = 0 };
  1105. int i = 0, ret = -EINVAL;
  1106. bool needs_reset = false, slot = false;
  1107. struct vfio_pci_device *tmp;
  1108. if (!pci_probe_reset_slot(vdev->pdev->slot))
  1109. slot = true;
  1110. else if (pci_probe_reset_bus(vdev->pdev->bus))
  1111. return;
  1112. if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
  1113. &i, slot) || !i)
  1114. return;
  1115. devs.max_index = i;
  1116. devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
  1117. if (!devs.devices)
  1118. return;
  1119. if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
  1120. vfio_pci_get_devs, &devs, slot))
  1121. goto put_devs;
  1122. for (i = 0; i < devs.cur_index; i++) {
  1123. tmp = vfio_device_data(devs.devices[i]);
  1124. if (tmp->needs_reset)
  1125. needs_reset = true;
  1126. if (tmp->refcnt)
  1127. goto put_devs;
  1128. }
  1129. if (needs_reset)
  1130. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  1131. pci_try_reset_bus(vdev->pdev->bus);
  1132. put_devs:
  1133. for (i = 0; i < devs.cur_index; i++) {
  1134. tmp = vfio_device_data(devs.devices[i]);
  1135. if (!ret)
  1136. tmp->needs_reset = false;
  1137. if (!tmp->refcnt && !disable_idle_d3)
  1138. pci_set_power_state(tmp->pdev, PCI_D3hot);
  1139. vfio_device_put(devs.devices[i]);
  1140. }
  1141. kfree(devs.devices);
  1142. }
  1143. static void __exit vfio_pci_cleanup(void)
  1144. {
  1145. pci_unregister_driver(&vfio_pci_driver);
  1146. vfio_pci_uninit_perm_bits();
  1147. }
  1148. static void __init vfio_pci_fill_ids(void)
  1149. {
  1150. char *p, *id;
  1151. int rc;
  1152. /* no ids passed actually */
  1153. if (ids[0] == '\0')
  1154. return;
  1155. /* add ids specified in the module parameter */
  1156. p = ids;
  1157. while ((id = strsep(&p, ","))) {
  1158. unsigned int vendor, device, subvendor = PCI_ANY_ID,
  1159. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  1160. int fields;
  1161. if (!strlen(id))
  1162. continue;
  1163. fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
  1164. &vendor, &device, &subvendor, &subdevice,
  1165. &class, &class_mask);
  1166. if (fields < 2) {
  1167. pr_warn("invalid id string \"%s\"\n", id);
  1168. continue;
  1169. }
  1170. rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
  1171. subvendor, subdevice, class, class_mask, 0);
  1172. if (rc)
  1173. pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
  1174. vendor, device, subvendor, subdevice,
  1175. class, class_mask, rc);
  1176. else
  1177. pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
  1178. vendor, device, subvendor, subdevice,
  1179. class, class_mask);
  1180. }
  1181. }
  1182. static int __init vfio_pci_init(void)
  1183. {
  1184. int ret;
  1185. /* Allocate shared config space permision data used by all devices */
  1186. ret = vfio_pci_init_perm_bits();
  1187. if (ret)
  1188. return ret;
  1189. /* Register and scan for devices */
  1190. ret = pci_register_driver(&vfio_pci_driver);
  1191. if (ret)
  1192. goto out_driver;
  1193. vfio_pci_fill_ids();
  1194. return 0;
  1195. out_driver:
  1196. vfio_pci_uninit_perm_bits();
  1197. return ret;
  1198. }
  1199. module_init(vfio_pci_init);
  1200. module_exit(vfio_pci_cleanup);
  1201. MODULE_VERSION(DRIVER_VERSION);
  1202. MODULE_LICENSE("GPL v2");
  1203. MODULE_AUTHOR(DRIVER_AUTHOR);
  1204. MODULE_DESCRIPTION(DRIVER_DESC);