vfio_pci.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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 <linux/nospec.h>
  30. #include <linux/sched/mm.h>
  31. #include "vfio_pci_private.h"
  32. #define DRIVER_VERSION "0.2"
  33. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  34. #define DRIVER_DESC "VFIO PCI - User Level meta-driver"
  35. static char ids[1024] __initdata;
  36. module_param_string(ids, ids, sizeof(ids), 0);
  37. 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");
  38. static bool nointxmask;
  39. module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
  40. MODULE_PARM_DESC(nointxmask,
  41. "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.");
  42. #ifdef CONFIG_VFIO_PCI_VGA
  43. static bool disable_vga;
  44. module_param(disable_vga, bool, S_IRUGO);
  45. MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
  46. #endif
  47. static bool disable_idle_d3;
  48. module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
  49. MODULE_PARM_DESC(disable_idle_d3,
  50. "Disable using the PCI D3 low power state for idle, unused devices");
  51. static DEFINE_MUTEX(driver_lock);
  52. static inline bool vfio_vga_disabled(void)
  53. {
  54. #ifdef CONFIG_VFIO_PCI_VGA
  55. return disable_vga;
  56. #else
  57. return true;
  58. #endif
  59. }
  60. /*
  61. * Our VGA arbiter participation is limited since we don't know anything
  62. * about the device itself. However, if the device is the only VGA device
  63. * downstream of a bridge and VFIO VGA support is disabled, then we can
  64. * safely return legacy VGA IO and memory as not decoded since the user
  65. * has no way to get to it and routing can be disabled externally at the
  66. * bridge.
  67. */
  68. static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
  69. {
  70. struct vfio_pci_device *vdev = opaque;
  71. struct pci_dev *tmp = NULL, *pdev = vdev->pdev;
  72. unsigned char max_busnr;
  73. unsigned int decodes;
  74. if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
  75. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
  76. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  77. max_busnr = pci_bus_max_busnr(pdev->bus);
  78. decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  79. while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
  80. if (tmp == pdev ||
  81. pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
  82. pci_is_root_bus(tmp->bus))
  83. continue;
  84. if (tmp->bus->number >= pdev->bus->number &&
  85. tmp->bus->number <= max_busnr) {
  86. pci_dev_put(tmp);
  87. decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
  88. break;
  89. }
  90. }
  91. return decodes;
  92. }
  93. static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
  94. {
  95. return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
  96. }
  97. static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev)
  98. {
  99. struct resource *res;
  100. int bar;
  101. struct vfio_pci_dummy_resource *dummy_res;
  102. for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
  103. res = vdev->pdev->resource + bar;
  104. if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
  105. goto no_mmap;
  106. if (!(res->flags & IORESOURCE_MEM))
  107. goto no_mmap;
  108. /*
  109. * The PCI core shouldn't set up a resource with a
  110. * type but zero size. But there may be bugs that
  111. * cause us to do that.
  112. */
  113. if (!resource_size(res))
  114. goto no_mmap;
  115. if (resource_size(res) >= PAGE_SIZE) {
  116. vdev->bar_mmap_supported[bar] = true;
  117. continue;
  118. }
  119. if (!(res->start & ~PAGE_MASK)) {
  120. /*
  121. * Add a dummy resource to reserve the remainder
  122. * of the exclusive page in case that hot-add
  123. * device's bar is assigned into it.
  124. */
  125. dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL);
  126. if (dummy_res == NULL)
  127. goto no_mmap;
  128. dummy_res->resource.name = "vfio sub-page reserved";
  129. dummy_res->resource.start = res->end + 1;
  130. dummy_res->resource.end = res->start + PAGE_SIZE - 1;
  131. dummy_res->resource.flags = res->flags;
  132. if (request_resource(res->parent,
  133. &dummy_res->resource)) {
  134. kfree(dummy_res);
  135. goto no_mmap;
  136. }
  137. dummy_res->index = bar;
  138. list_add(&dummy_res->res_next,
  139. &vdev->dummy_resources_list);
  140. vdev->bar_mmap_supported[bar] = true;
  141. continue;
  142. }
  143. /*
  144. * Here we don't handle the case when the BAR is not page
  145. * aligned because we can't expect the BAR will be
  146. * assigned into the same location in a page in guest
  147. * when we passthrough the BAR. And it's hard to access
  148. * this BAR in userspace because we have no way to get
  149. * the BAR's location in a page.
  150. */
  151. no_mmap:
  152. vdev->bar_mmap_supported[bar] = false;
  153. }
  154. }
  155. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
  156. static void vfio_pci_disable(struct vfio_pci_device *vdev);
  157. static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data);
  158. /*
  159. * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
  160. * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
  161. * If a device implements the former but not the latter we would typically
  162. * expect broken_intx_masking be set and require an exclusive interrupt.
  163. * However since we do have control of the device's ability to assert INTx,
  164. * we can instead pretend that the device does not implement INTx, virtualizing
  165. * the pin register to report zero and maintaining DisINTx set on the host.
  166. */
  167. static bool vfio_pci_nointx(struct pci_dev *pdev)
  168. {
  169. switch (pdev->vendor) {
  170. case PCI_VENDOR_ID_INTEL:
  171. switch (pdev->device) {
  172. /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */
  173. case 0x1572:
  174. case 0x1574:
  175. case 0x1580 ... 0x1581:
  176. case 0x1583 ... 0x158b:
  177. case 0x37d0 ... 0x37d2:
  178. return true;
  179. default:
  180. return false;
  181. }
  182. }
  183. return false;
  184. }
  185. static int vfio_pci_enable(struct vfio_pci_device *vdev)
  186. {
  187. struct pci_dev *pdev = vdev->pdev;
  188. int ret;
  189. u16 cmd;
  190. u8 msix_pos;
  191. pci_set_power_state(pdev, PCI_D0);
  192. /* Don't allow our initial saved state to include busmaster */
  193. pci_clear_master(pdev);
  194. ret = pci_enable_device(pdev);
  195. if (ret)
  196. return ret;
  197. /* If reset fails because of the device lock, fail this path entirely */
  198. ret = pci_try_reset_function(pdev);
  199. if (ret == -EAGAIN) {
  200. pci_disable_device(pdev);
  201. return ret;
  202. }
  203. vdev->reset_works = !ret;
  204. pci_save_state(pdev);
  205. vdev->pci_saved_state = pci_store_saved_state(pdev);
  206. if (!vdev->pci_saved_state)
  207. pr_debug("%s: Couldn't store %s saved state\n",
  208. __func__, dev_name(&pdev->dev));
  209. if (likely(!nointxmask)) {
  210. if (vfio_pci_nointx(pdev)) {
  211. dev_info(&pdev->dev, "Masking broken INTx support\n");
  212. vdev->nointx = true;
  213. pci_intx(pdev, 0);
  214. } else
  215. vdev->pci_2_3 = pci_intx_mask_supported(pdev);
  216. }
  217. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  218. if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
  219. cmd &= ~PCI_COMMAND_INTX_DISABLE;
  220. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  221. }
  222. ret = vfio_config_init(vdev);
  223. if (ret) {
  224. kfree(vdev->pci_saved_state);
  225. vdev->pci_saved_state = NULL;
  226. pci_disable_device(pdev);
  227. return ret;
  228. }
  229. msix_pos = pdev->msix_cap;
  230. if (msix_pos) {
  231. u16 flags;
  232. u32 table;
  233. pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
  234. pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
  235. vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
  236. vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
  237. vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
  238. } else
  239. vdev->msix_bar = 0xFF;
  240. if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
  241. vdev->has_vga = true;
  242. if (vfio_pci_is_vga(pdev) &&
  243. pdev->vendor == PCI_VENDOR_ID_INTEL &&
  244. IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
  245. ret = vfio_pci_igd_init(vdev);
  246. if (ret) {
  247. dev_warn(&vdev->pdev->dev,
  248. "Failed to setup Intel IGD regions\n");
  249. vfio_pci_disable(vdev);
  250. return ret;
  251. }
  252. }
  253. vfio_pci_probe_mmaps(vdev);
  254. return 0;
  255. }
  256. static void vfio_pci_disable(struct vfio_pci_device *vdev)
  257. {
  258. struct pci_dev *pdev = vdev->pdev;
  259. struct vfio_pci_dummy_resource *dummy_res, *tmp;
  260. struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp;
  261. int i, bar;
  262. /* Stop the device from further DMA */
  263. pci_clear_master(pdev);
  264. vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
  265. VFIO_IRQ_SET_ACTION_TRIGGER,
  266. vdev->irq_type, 0, 0, NULL);
  267. /* Device closed, don't need mutex here */
  268. list_for_each_entry_safe(ioeventfd, ioeventfd_tmp,
  269. &vdev->ioeventfds_list, next) {
  270. vfio_virqfd_disable(&ioeventfd->virqfd);
  271. list_del(&ioeventfd->next);
  272. kfree(ioeventfd);
  273. }
  274. vdev->ioeventfds_nr = 0;
  275. vdev->virq_disabled = false;
  276. for (i = 0; i < vdev->num_regions; i++)
  277. vdev->region[i].ops->release(vdev, &vdev->region[i]);
  278. vdev->num_regions = 0;
  279. kfree(vdev->region);
  280. vdev->region = NULL; /* don't krealloc a freed pointer */
  281. vfio_config_free(vdev);
  282. for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
  283. if (!vdev->barmap[bar])
  284. continue;
  285. pci_iounmap(pdev, vdev->barmap[bar]);
  286. pci_release_selected_regions(pdev, 1 << bar);
  287. vdev->barmap[bar] = NULL;
  288. }
  289. list_for_each_entry_safe(dummy_res, tmp,
  290. &vdev->dummy_resources_list, res_next) {
  291. list_del(&dummy_res->res_next);
  292. release_resource(&dummy_res->resource);
  293. kfree(dummy_res);
  294. }
  295. vdev->needs_reset = true;
  296. /*
  297. * If we have saved state, restore it. If we can reset the device,
  298. * even better. Resetting with current state seems better than
  299. * nothing, but saving and restoring current state without reset
  300. * is just busy work.
  301. */
  302. if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
  303. pr_info("%s: Couldn't reload %s saved state\n",
  304. __func__, dev_name(&pdev->dev));
  305. if (!vdev->reset_works)
  306. goto out;
  307. pci_save_state(pdev);
  308. }
  309. /*
  310. * Disable INTx and MSI, presumably to avoid spurious interrupts
  311. * during reset. Stolen from pci_reset_function()
  312. */
  313. pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  314. /*
  315. * Try to get the locks ourselves to prevent a deadlock. The
  316. * success of this is dependent on being able to lock the device,
  317. * which is not always possible.
  318. * We can not use the "try" reset interface here, which will
  319. * overwrite the previously restored configuration information.
  320. */
  321. if (vdev->reset_works && pci_cfg_access_trylock(pdev)) {
  322. if (device_trylock(&pdev->dev)) {
  323. if (!__pci_reset_function_locked(pdev))
  324. vdev->needs_reset = false;
  325. device_unlock(&pdev->dev);
  326. }
  327. pci_cfg_access_unlock(pdev);
  328. }
  329. pci_restore_state(pdev);
  330. out:
  331. pci_disable_device(pdev);
  332. vfio_pci_try_bus_reset(vdev);
  333. if (!disable_idle_d3)
  334. pci_set_power_state(pdev, PCI_D3hot);
  335. }
  336. static void vfio_pci_release(void *device_data)
  337. {
  338. struct vfio_pci_device *vdev = device_data;
  339. mutex_lock(&driver_lock);
  340. if (!(--vdev->refcnt)) {
  341. vfio_spapr_pci_eeh_release(vdev->pdev);
  342. vfio_pci_disable(vdev);
  343. mutex_lock(&vdev->igate);
  344. if (vdev->err_trigger) {
  345. eventfd_ctx_put(vdev->err_trigger);
  346. vdev->err_trigger = NULL;
  347. }
  348. mutex_unlock(&vdev->igate);
  349. mutex_lock(&vdev->igate);
  350. if (vdev->req_trigger) {
  351. eventfd_ctx_put(vdev->req_trigger);
  352. vdev->req_trigger = NULL;
  353. }
  354. mutex_unlock(&vdev->igate);
  355. }
  356. mutex_unlock(&driver_lock);
  357. module_put(THIS_MODULE);
  358. }
  359. static int vfio_pci_open(void *device_data)
  360. {
  361. struct vfio_pci_device *vdev = device_data;
  362. int ret = 0;
  363. if (!try_module_get(THIS_MODULE))
  364. return -ENODEV;
  365. mutex_lock(&driver_lock);
  366. if (!vdev->refcnt) {
  367. ret = vfio_pci_enable(vdev);
  368. if (ret)
  369. goto error;
  370. vfio_spapr_pci_eeh_open(vdev->pdev);
  371. }
  372. vdev->refcnt++;
  373. error:
  374. mutex_unlock(&driver_lock);
  375. if (ret)
  376. module_put(THIS_MODULE);
  377. return ret;
  378. }
  379. static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
  380. {
  381. if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
  382. u8 pin;
  383. if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) ||
  384. vdev->nointx || vdev->pdev->is_virtfn)
  385. return 0;
  386. pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
  387. return pin ? 1 : 0;
  388. } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
  389. u8 pos;
  390. u16 flags;
  391. pos = vdev->pdev->msi_cap;
  392. if (pos) {
  393. pci_read_config_word(vdev->pdev,
  394. pos + PCI_MSI_FLAGS, &flags);
  395. return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
  396. }
  397. } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
  398. u8 pos;
  399. u16 flags;
  400. pos = vdev->pdev->msix_cap;
  401. if (pos) {
  402. pci_read_config_word(vdev->pdev,
  403. pos + PCI_MSIX_FLAGS, &flags);
  404. return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
  405. }
  406. } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
  407. if (pci_is_pcie(vdev->pdev))
  408. return 1;
  409. } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
  410. return 1;
  411. }
  412. return 0;
  413. }
  414. static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
  415. {
  416. (*(int *)data)++;
  417. return 0;
  418. }
  419. struct vfio_pci_fill_info {
  420. int max;
  421. int cur;
  422. struct vfio_pci_dependent_device *devices;
  423. };
  424. static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
  425. {
  426. struct vfio_pci_fill_info *fill = data;
  427. struct iommu_group *iommu_group;
  428. if (fill->cur == fill->max)
  429. return -EAGAIN; /* Something changed, try again */
  430. iommu_group = iommu_group_get(&pdev->dev);
  431. if (!iommu_group)
  432. return -EPERM; /* Cannot reset non-isolated devices */
  433. fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
  434. fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
  435. fill->devices[fill->cur].bus = pdev->bus->number;
  436. fill->devices[fill->cur].devfn = pdev->devfn;
  437. fill->cur++;
  438. iommu_group_put(iommu_group);
  439. return 0;
  440. }
  441. struct vfio_pci_group_entry {
  442. struct vfio_group *group;
  443. int id;
  444. };
  445. struct vfio_pci_group_info {
  446. int count;
  447. struct vfio_pci_group_entry *groups;
  448. };
  449. static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
  450. {
  451. struct vfio_pci_group_info *info = data;
  452. struct iommu_group *group;
  453. int id, i;
  454. group = iommu_group_get(&pdev->dev);
  455. if (!group)
  456. return -EPERM;
  457. id = iommu_group_id(group);
  458. for (i = 0; i < info->count; i++)
  459. if (info->groups[i].id == id)
  460. break;
  461. iommu_group_put(group);
  462. return (i == info->count) ? -EINVAL : 0;
  463. }
  464. static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
  465. {
  466. for (; pdev; pdev = pdev->bus->self)
  467. if (pdev->bus == slot->bus)
  468. return (pdev->slot == slot);
  469. return false;
  470. }
  471. struct vfio_pci_walk_info {
  472. int (*fn)(struct pci_dev *, void *data);
  473. void *data;
  474. struct pci_dev *pdev;
  475. bool slot;
  476. int ret;
  477. };
  478. static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
  479. {
  480. struct vfio_pci_walk_info *walk = data;
  481. if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
  482. walk->ret = walk->fn(pdev, walk->data);
  483. return walk->ret;
  484. }
  485. static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
  486. int (*fn)(struct pci_dev *,
  487. void *data), void *data,
  488. bool slot)
  489. {
  490. struct vfio_pci_walk_info walk = {
  491. .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
  492. };
  493. pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
  494. return walk.ret;
  495. }
  496. static int msix_mmappable_cap(struct vfio_pci_device *vdev,
  497. struct vfio_info_cap *caps)
  498. {
  499. struct vfio_info_cap_header header = {
  500. .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE,
  501. .version = 1
  502. };
  503. return vfio_info_add_capability(caps, &header, sizeof(header));
  504. }
  505. int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
  506. unsigned int type, unsigned int subtype,
  507. const struct vfio_pci_regops *ops,
  508. size_t size, u32 flags, void *data)
  509. {
  510. struct vfio_pci_region *region;
  511. region = krealloc(vdev->region,
  512. (vdev->num_regions + 1) * sizeof(*region),
  513. GFP_KERNEL);
  514. if (!region)
  515. return -ENOMEM;
  516. vdev->region = region;
  517. vdev->region[vdev->num_regions].type = type;
  518. vdev->region[vdev->num_regions].subtype = subtype;
  519. vdev->region[vdev->num_regions].ops = ops;
  520. vdev->region[vdev->num_regions].size = size;
  521. vdev->region[vdev->num_regions].flags = flags;
  522. vdev->region[vdev->num_regions].data = data;
  523. vdev->num_regions++;
  524. return 0;
  525. }
  526. struct vfio_devices {
  527. struct vfio_device **devices;
  528. int cur_index;
  529. int max_index;
  530. };
  531. static long vfio_pci_ioctl(void *device_data,
  532. unsigned int cmd, unsigned long arg)
  533. {
  534. struct vfio_pci_device *vdev = device_data;
  535. unsigned long minsz;
  536. if (cmd == VFIO_DEVICE_GET_INFO) {
  537. struct vfio_device_info info;
  538. minsz = offsetofend(struct vfio_device_info, num_irqs);
  539. if (copy_from_user(&info, (void __user *)arg, minsz))
  540. return -EFAULT;
  541. if (info.argsz < minsz)
  542. return -EINVAL;
  543. info.flags = VFIO_DEVICE_FLAGS_PCI;
  544. if (vdev->reset_works)
  545. info.flags |= VFIO_DEVICE_FLAGS_RESET;
  546. info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
  547. info.num_irqs = VFIO_PCI_NUM_IRQS;
  548. return copy_to_user((void __user *)arg, &info, minsz) ?
  549. -EFAULT : 0;
  550. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  551. struct pci_dev *pdev = vdev->pdev;
  552. struct vfio_region_info info;
  553. struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
  554. int i, ret;
  555. minsz = offsetofend(struct vfio_region_info, offset);
  556. if (copy_from_user(&info, (void __user *)arg, minsz))
  557. return -EFAULT;
  558. if (info.argsz < minsz)
  559. return -EINVAL;
  560. switch (info.index) {
  561. case VFIO_PCI_CONFIG_REGION_INDEX:
  562. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  563. info.size = pdev->cfg_size;
  564. info.flags = VFIO_REGION_INFO_FLAG_READ |
  565. VFIO_REGION_INFO_FLAG_WRITE;
  566. break;
  567. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  568. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  569. info.size = pci_resource_len(pdev, info.index);
  570. if (!info.size) {
  571. info.flags = 0;
  572. break;
  573. }
  574. info.flags = VFIO_REGION_INFO_FLAG_READ |
  575. VFIO_REGION_INFO_FLAG_WRITE;
  576. if (vdev->bar_mmap_supported[info.index]) {
  577. info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
  578. if (info.index == vdev->msix_bar) {
  579. ret = msix_mmappable_cap(vdev, &caps);
  580. if (ret)
  581. return ret;
  582. }
  583. }
  584. break;
  585. case VFIO_PCI_ROM_REGION_INDEX:
  586. {
  587. void __iomem *io;
  588. size_t size;
  589. u16 cmd;
  590. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  591. info.flags = 0;
  592. /* Report the BAR size, not the ROM size */
  593. info.size = pci_resource_len(pdev, info.index);
  594. if (!info.size) {
  595. /* Shadow ROMs appear as PCI option ROMs */
  596. if (pdev->resource[PCI_ROM_RESOURCE].flags &
  597. IORESOURCE_ROM_SHADOW)
  598. info.size = 0x20000;
  599. else
  600. break;
  601. }
  602. /*
  603. * Is it really there? Enable memory decode for
  604. * implicit access in pci_map_rom().
  605. */
  606. cmd = vfio_pci_memory_lock_and_enable(vdev);
  607. io = pci_map_rom(pdev, &size);
  608. if (io) {
  609. info.flags = VFIO_REGION_INFO_FLAG_READ;
  610. pci_unmap_rom(pdev, io);
  611. } else {
  612. info.size = 0;
  613. }
  614. vfio_pci_memory_unlock_and_restore(vdev, cmd);
  615. break;
  616. }
  617. case VFIO_PCI_VGA_REGION_INDEX:
  618. if (!vdev->has_vga)
  619. return -EINVAL;
  620. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  621. info.size = 0xc0000;
  622. info.flags = VFIO_REGION_INFO_FLAG_READ |
  623. VFIO_REGION_INFO_FLAG_WRITE;
  624. break;
  625. default:
  626. {
  627. struct vfio_region_info_cap_type cap_type = {
  628. .header.id = VFIO_REGION_INFO_CAP_TYPE,
  629. .header.version = 1 };
  630. if (info.index >=
  631. VFIO_PCI_NUM_REGIONS + vdev->num_regions)
  632. return -EINVAL;
  633. info.index = array_index_nospec(info.index,
  634. VFIO_PCI_NUM_REGIONS +
  635. vdev->num_regions);
  636. i = info.index - VFIO_PCI_NUM_REGIONS;
  637. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  638. info.size = vdev->region[i].size;
  639. info.flags = vdev->region[i].flags;
  640. cap_type.type = vdev->region[i].type;
  641. cap_type.subtype = vdev->region[i].subtype;
  642. ret = vfio_info_add_capability(&caps, &cap_type.header,
  643. sizeof(cap_type));
  644. if (ret)
  645. return ret;
  646. }
  647. }
  648. if (caps.size) {
  649. info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
  650. if (info.argsz < sizeof(info) + caps.size) {
  651. info.argsz = sizeof(info) + caps.size;
  652. info.cap_offset = 0;
  653. } else {
  654. vfio_info_cap_shift(&caps, sizeof(info));
  655. if (copy_to_user((void __user *)arg +
  656. sizeof(info), caps.buf,
  657. caps.size)) {
  658. kfree(caps.buf);
  659. return -EFAULT;
  660. }
  661. info.cap_offset = sizeof(info);
  662. }
  663. kfree(caps.buf);
  664. }
  665. return copy_to_user((void __user *)arg, &info, minsz) ?
  666. -EFAULT : 0;
  667. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  668. struct vfio_irq_info info;
  669. minsz = offsetofend(struct vfio_irq_info, count);
  670. if (copy_from_user(&info, (void __user *)arg, minsz))
  671. return -EFAULT;
  672. if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
  673. return -EINVAL;
  674. switch (info.index) {
  675. case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
  676. case VFIO_PCI_REQ_IRQ_INDEX:
  677. break;
  678. case VFIO_PCI_ERR_IRQ_INDEX:
  679. if (pci_is_pcie(vdev->pdev))
  680. break;
  681. /* fall through */
  682. default:
  683. return -EINVAL;
  684. }
  685. info.flags = VFIO_IRQ_INFO_EVENTFD;
  686. info.count = vfio_pci_get_irq_count(vdev, info.index);
  687. if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
  688. info.flags |= (VFIO_IRQ_INFO_MASKABLE |
  689. VFIO_IRQ_INFO_AUTOMASKED);
  690. else
  691. info.flags |= VFIO_IRQ_INFO_NORESIZE;
  692. return copy_to_user((void __user *)arg, &info, minsz) ?
  693. -EFAULT : 0;
  694. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  695. struct vfio_irq_set hdr;
  696. u8 *data = NULL;
  697. int max, ret = 0;
  698. size_t data_size = 0;
  699. minsz = offsetofend(struct vfio_irq_set, count);
  700. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  701. return -EFAULT;
  702. max = vfio_pci_get_irq_count(vdev, hdr.index);
  703. ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
  704. VFIO_PCI_NUM_IRQS, &data_size);
  705. if (ret)
  706. return ret;
  707. if (data_size) {
  708. data = memdup_user((void __user *)(arg + minsz),
  709. data_size);
  710. if (IS_ERR(data))
  711. return PTR_ERR(data);
  712. }
  713. mutex_lock(&vdev->igate);
  714. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  715. hdr.start, hdr.count, data);
  716. mutex_unlock(&vdev->igate);
  717. kfree(data);
  718. return ret;
  719. } else if (cmd == VFIO_DEVICE_RESET) {
  720. int ret;
  721. if (!vdev->reset_works)
  722. return -EINVAL;
  723. vfio_pci_zap_and_down_write_memory_lock(vdev);
  724. ret = pci_try_reset_function(vdev->pdev);
  725. up_write(&vdev->memory_lock);
  726. return ret;
  727. } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
  728. struct vfio_pci_hot_reset_info hdr;
  729. struct vfio_pci_fill_info fill = { 0 };
  730. struct vfio_pci_dependent_device *devices = NULL;
  731. bool slot = false;
  732. int ret = 0;
  733. minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
  734. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  735. return -EFAULT;
  736. if (hdr.argsz < minsz)
  737. return -EINVAL;
  738. hdr.flags = 0;
  739. /* Can we do a slot or bus reset or neither? */
  740. if (!pci_probe_reset_slot(vdev->pdev->slot))
  741. slot = true;
  742. else if (pci_probe_reset_bus(vdev->pdev->bus))
  743. return -ENODEV;
  744. /* How many devices are affected? */
  745. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  746. vfio_pci_count_devs,
  747. &fill.max, slot);
  748. if (ret)
  749. return ret;
  750. WARN_ON(!fill.max); /* Should always be at least one */
  751. /*
  752. * If there's enough space, fill it now, otherwise return
  753. * -ENOSPC and the number of devices affected.
  754. */
  755. if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
  756. ret = -ENOSPC;
  757. hdr.count = fill.max;
  758. goto reset_info_exit;
  759. }
  760. devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
  761. if (!devices)
  762. return -ENOMEM;
  763. fill.devices = devices;
  764. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  765. vfio_pci_fill_devs,
  766. &fill, slot);
  767. /*
  768. * If a device was removed between counting and filling,
  769. * we may come up short of fill.max. If a device was
  770. * added, we'll have a return of -EAGAIN above.
  771. */
  772. if (!ret)
  773. hdr.count = fill.cur;
  774. reset_info_exit:
  775. if (copy_to_user((void __user *)arg, &hdr, minsz))
  776. ret = -EFAULT;
  777. if (!ret) {
  778. if (copy_to_user((void __user *)(arg + minsz), devices,
  779. hdr.count * sizeof(*devices)))
  780. ret = -EFAULT;
  781. }
  782. kfree(devices);
  783. return ret;
  784. } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
  785. struct vfio_pci_hot_reset hdr;
  786. int32_t *group_fds;
  787. struct vfio_pci_group_entry *groups;
  788. struct vfio_pci_group_info info;
  789. struct vfio_devices devs = { .cur_index = 0 };
  790. bool slot = false;
  791. int i, group_idx, mem_idx = 0, count = 0, ret = 0;
  792. minsz = offsetofend(struct vfio_pci_hot_reset, count);
  793. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  794. return -EFAULT;
  795. if (hdr.argsz < minsz || hdr.flags)
  796. return -EINVAL;
  797. /* Can we do a slot or bus reset or neither? */
  798. if (!pci_probe_reset_slot(vdev->pdev->slot))
  799. slot = true;
  800. else if (pci_probe_reset_bus(vdev->pdev->bus))
  801. return -ENODEV;
  802. /*
  803. * We can't let userspace give us an arbitrarily large
  804. * buffer to copy, so verify how many we think there
  805. * could be. Note groups can have multiple devices so
  806. * one group per device is the max.
  807. */
  808. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  809. vfio_pci_count_devs,
  810. &count, slot);
  811. if (ret)
  812. return ret;
  813. /* Somewhere between 1 and count is OK */
  814. if (!hdr.count || hdr.count > count)
  815. return -EINVAL;
  816. group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
  817. groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
  818. if (!group_fds || !groups) {
  819. kfree(group_fds);
  820. kfree(groups);
  821. return -ENOMEM;
  822. }
  823. if (copy_from_user(group_fds, (void __user *)(arg + minsz),
  824. hdr.count * sizeof(*group_fds))) {
  825. kfree(group_fds);
  826. kfree(groups);
  827. return -EFAULT;
  828. }
  829. /*
  830. * For each group_fd, get the group through the vfio external
  831. * user interface and store the group and iommu ID. This
  832. * ensures the group is held across the reset.
  833. */
  834. for (group_idx = 0; group_idx < hdr.count; group_idx++) {
  835. struct vfio_group *group;
  836. struct fd f = fdget(group_fds[group_idx]);
  837. if (!f.file) {
  838. ret = -EBADF;
  839. break;
  840. }
  841. group = vfio_group_get_external_user(f.file);
  842. fdput(f);
  843. if (IS_ERR(group)) {
  844. ret = PTR_ERR(group);
  845. break;
  846. }
  847. groups[group_idx].group = group;
  848. groups[group_idx].id =
  849. vfio_external_user_iommu_id(group);
  850. }
  851. kfree(group_fds);
  852. /* release reference to groups on error */
  853. if (ret)
  854. goto hot_reset_release;
  855. info.count = hdr.count;
  856. info.groups = groups;
  857. /*
  858. * Test whether all the affected devices are contained
  859. * by the set of groups provided by the user.
  860. */
  861. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  862. vfio_pci_validate_devs,
  863. &info, slot);
  864. if (ret)
  865. goto hot_reset_release;
  866. devs.max_index = count;
  867. devs.devices = kcalloc(count, sizeof(struct vfio_device *),
  868. GFP_KERNEL);
  869. if (!devs.devices) {
  870. ret = -ENOMEM;
  871. goto hot_reset_release;
  872. }
  873. /*
  874. * We need to get memory_lock for each device, but devices
  875. * can share mmap_sem, therefore we need to zap and hold
  876. * the vma_lock for each device, and only then get each
  877. * memory_lock.
  878. */
  879. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  880. vfio_pci_try_zap_and_vma_lock_cb,
  881. &devs, slot);
  882. if (ret)
  883. goto hot_reset_release;
  884. for (; mem_idx < devs.cur_index; mem_idx++) {
  885. struct vfio_pci_device *tmp;
  886. tmp = vfio_device_data(devs.devices[mem_idx]);
  887. ret = down_write_trylock(&tmp->memory_lock);
  888. if (!ret) {
  889. ret = -EBUSY;
  890. goto hot_reset_release;
  891. }
  892. mutex_unlock(&tmp->vma_lock);
  893. }
  894. /* User has access, do the reset */
  895. ret = pci_reset_bus(vdev->pdev);
  896. hot_reset_release:
  897. for (i = 0; i < devs.cur_index; i++) {
  898. struct vfio_device *device;
  899. struct vfio_pci_device *tmp;
  900. device = devs.devices[i];
  901. tmp = vfio_device_data(device);
  902. if (i < mem_idx)
  903. up_write(&tmp->memory_lock);
  904. else
  905. mutex_unlock(&tmp->vma_lock);
  906. vfio_device_put(device);
  907. }
  908. kfree(devs.devices);
  909. for (group_idx--; group_idx >= 0; group_idx--)
  910. vfio_group_put_external_user(groups[group_idx].group);
  911. kfree(groups);
  912. return ret;
  913. } else if (cmd == VFIO_DEVICE_IOEVENTFD) {
  914. struct vfio_device_ioeventfd ioeventfd;
  915. int count;
  916. minsz = offsetofend(struct vfio_device_ioeventfd, fd);
  917. if (copy_from_user(&ioeventfd, (void __user *)arg, minsz))
  918. return -EFAULT;
  919. if (ioeventfd.argsz < minsz)
  920. return -EINVAL;
  921. if (ioeventfd.flags & ~VFIO_DEVICE_IOEVENTFD_SIZE_MASK)
  922. return -EINVAL;
  923. count = ioeventfd.flags & VFIO_DEVICE_IOEVENTFD_SIZE_MASK;
  924. if (hweight8(count) != 1 || ioeventfd.fd < -1)
  925. return -EINVAL;
  926. return vfio_pci_ioeventfd(vdev, ioeventfd.offset,
  927. ioeventfd.data, count, ioeventfd.fd);
  928. }
  929. return -ENOTTY;
  930. }
  931. static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
  932. size_t count, loff_t *ppos, bool iswrite)
  933. {
  934. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  935. struct vfio_pci_device *vdev = device_data;
  936. if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
  937. return -EINVAL;
  938. switch (index) {
  939. case VFIO_PCI_CONFIG_REGION_INDEX:
  940. return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
  941. case VFIO_PCI_ROM_REGION_INDEX:
  942. if (iswrite)
  943. return -EINVAL;
  944. return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
  945. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  946. return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
  947. case VFIO_PCI_VGA_REGION_INDEX:
  948. return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
  949. default:
  950. index -= VFIO_PCI_NUM_REGIONS;
  951. return vdev->region[index].ops->rw(vdev, buf,
  952. count, ppos, iswrite);
  953. }
  954. return -EINVAL;
  955. }
  956. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  957. size_t count, loff_t *ppos)
  958. {
  959. if (!count)
  960. return 0;
  961. return vfio_pci_rw(device_data, buf, count, ppos, false);
  962. }
  963. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  964. size_t count, loff_t *ppos)
  965. {
  966. if (!count)
  967. return 0;
  968. return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
  969. }
  970. /* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */
  971. static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
  972. {
  973. struct vfio_pci_mmap_vma *mmap_vma, *tmp;
  974. /*
  975. * Lock ordering:
  976. * vma_lock is nested under mmap_sem for vm_ops callback paths.
  977. * The memory_lock semaphore is used by both code paths calling
  978. * into this function to zap vmas and the vm_ops.fault callback
  979. * to protect the memory enable state of the device.
  980. *
  981. * When zapping vmas we need to maintain the mmap_sem => vma_lock
  982. * ordering, which requires using vma_lock to walk vma_list to
  983. * acquire an mm, then dropping vma_lock to get the mmap_sem and
  984. * reacquiring vma_lock. This logic is derived from similar
  985. * requirements in uverbs_user_mmap_disassociate().
  986. *
  987. * mmap_sem must always be the top-level lock when it is taken.
  988. * Therefore we can only hold the memory_lock write lock when
  989. * vma_list is empty, as we'd need to take mmap_sem to clear
  990. * entries. vma_list can only be guaranteed empty when holding
  991. * vma_lock, thus memory_lock is nested under vma_lock.
  992. *
  993. * This enables the vm_ops.fault callback to acquire vma_lock,
  994. * followed by memory_lock read lock, while already holding
  995. * mmap_sem without risk of deadlock.
  996. */
  997. while (1) {
  998. struct mm_struct *mm = NULL;
  999. if (try) {
  1000. if (!mutex_trylock(&vdev->vma_lock))
  1001. return 0;
  1002. } else {
  1003. mutex_lock(&vdev->vma_lock);
  1004. }
  1005. while (!list_empty(&vdev->vma_list)) {
  1006. mmap_vma = list_first_entry(&vdev->vma_list,
  1007. struct vfio_pci_mmap_vma,
  1008. vma_next);
  1009. mm = mmap_vma->vma->vm_mm;
  1010. if (mmget_not_zero(mm))
  1011. break;
  1012. list_del(&mmap_vma->vma_next);
  1013. kfree(mmap_vma);
  1014. mm = NULL;
  1015. }
  1016. if (!mm)
  1017. return 1;
  1018. mutex_unlock(&vdev->vma_lock);
  1019. if (try) {
  1020. if (!down_read_trylock(&mm->mmap_sem)) {
  1021. mmput(mm);
  1022. return 0;
  1023. }
  1024. } else {
  1025. down_read(&mm->mmap_sem);
  1026. }
  1027. if (mmget_still_valid(mm)) {
  1028. if (try) {
  1029. if (!mutex_trylock(&vdev->vma_lock)) {
  1030. up_read(&mm->mmap_sem);
  1031. mmput(mm);
  1032. return 0;
  1033. }
  1034. } else {
  1035. mutex_lock(&vdev->vma_lock);
  1036. }
  1037. list_for_each_entry_safe(mmap_vma, tmp,
  1038. &vdev->vma_list, vma_next) {
  1039. struct vm_area_struct *vma = mmap_vma->vma;
  1040. if (vma->vm_mm != mm)
  1041. continue;
  1042. list_del(&mmap_vma->vma_next);
  1043. kfree(mmap_vma);
  1044. zap_vma_ptes(vma, vma->vm_start,
  1045. vma->vm_end - vma->vm_start);
  1046. }
  1047. mutex_unlock(&vdev->vma_lock);
  1048. }
  1049. up_read(&mm->mmap_sem);
  1050. mmput(mm);
  1051. }
  1052. }
  1053. void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device *vdev)
  1054. {
  1055. vfio_pci_zap_and_vma_lock(vdev, false);
  1056. down_write(&vdev->memory_lock);
  1057. mutex_unlock(&vdev->vma_lock);
  1058. }
  1059. u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev)
  1060. {
  1061. u16 cmd;
  1062. down_write(&vdev->memory_lock);
  1063. pci_read_config_word(vdev->pdev, PCI_COMMAND, &cmd);
  1064. if (!(cmd & PCI_COMMAND_MEMORY))
  1065. pci_write_config_word(vdev->pdev, PCI_COMMAND,
  1066. cmd | PCI_COMMAND_MEMORY);
  1067. return cmd;
  1068. }
  1069. void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev, u16 cmd)
  1070. {
  1071. pci_write_config_word(vdev->pdev, PCI_COMMAND, cmd);
  1072. up_write(&vdev->memory_lock);
  1073. }
  1074. /* Caller holds vma_lock */
  1075. static int __vfio_pci_add_vma(struct vfio_pci_device *vdev,
  1076. struct vm_area_struct *vma)
  1077. {
  1078. struct vfio_pci_mmap_vma *mmap_vma;
  1079. mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL);
  1080. if (!mmap_vma)
  1081. return -ENOMEM;
  1082. mmap_vma->vma = vma;
  1083. list_add(&mmap_vma->vma_next, &vdev->vma_list);
  1084. return 0;
  1085. }
  1086. /*
  1087. * Zap mmaps on open so that we can fault them in on access and therefore
  1088. * our vma_list only tracks mappings accessed since last zap.
  1089. */
  1090. static void vfio_pci_mmap_open(struct vm_area_struct *vma)
  1091. {
  1092. zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
  1093. }
  1094. static void vfio_pci_mmap_close(struct vm_area_struct *vma)
  1095. {
  1096. struct vfio_pci_device *vdev = vma->vm_private_data;
  1097. struct vfio_pci_mmap_vma *mmap_vma;
  1098. mutex_lock(&vdev->vma_lock);
  1099. list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) {
  1100. if (mmap_vma->vma == vma) {
  1101. list_del(&mmap_vma->vma_next);
  1102. kfree(mmap_vma);
  1103. break;
  1104. }
  1105. }
  1106. mutex_unlock(&vdev->vma_lock);
  1107. }
  1108. static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
  1109. {
  1110. struct vm_area_struct *vma = vmf->vma;
  1111. struct vfio_pci_device *vdev = vma->vm_private_data;
  1112. vm_fault_t ret = VM_FAULT_NOPAGE;
  1113. mutex_lock(&vdev->vma_lock);
  1114. down_read(&vdev->memory_lock);
  1115. if (!__vfio_pci_memory_enabled(vdev)) {
  1116. ret = VM_FAULT_SIGBUS;
  1117. mutex_unlock(&vdev->vma_lock);
  1118. goto up_out;
  1119. }
  1120. if (__vfio_pci_add_vma(vdev, vma)) {
  1121. ret = VM_FAULT_OOM;
  1122. mutex_unlock(&vdev->vma_lock);
  1123. goto up_out;
  1124. }
  1125. mutex_unlock(&vdev->vma_lock);
  1126. if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1127. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  1128. ret = VM_FAULT_SIGBUS;
  1129. up_out:
  1130. up_read(&vdev->memory_lock);
  1131. return ret;
  1132. }
  1133. static const struct vm_operations_struct vfio_pci_mmap_ops = {
  1134. .open = vfio_pci_mmap_open,
  1135. .close = vfio_pci_mmap_close,
  1136. .fault = vfio_pci_mmap_fault,
  1137. };
  1138. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  1139. {
  1140. struct vfio_pci_device *vdev = device_data;
  1141. struct pci_dev *pdev = vdev->pdev;
  1142. unsigned int index;
  1143. u64 phys_len, req_len, pgoff, req_start;
  1144. int ret;
  1145. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  1146. if (vma->vm_end < vma->vm_start)
  1147. return -EINVAL;
  1148. if ((vma->vm_flags & VM_SHARED) == 0)
  1149. return -EINVAL;
  1150. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  1151. return -EINVAL;
  1152. if (!vdev->bar_mmap_supported[index])
  1153. return -EINVAL;
  1154. phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
  1155. req_len = vma->vm_end - vma->vm_start;
  1156. pgoff = vma->vm_pgoff &
  1157. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  1158. req_start = pgoff << PAGE_SHIFT;
  1159. if (req_start + req_len > phys_len)
  1160. return -EINVAL;
  1161. /*
  1162. * Even though we don't make use of the barmap for the mmap,
  1163. * we need to request the region and the barmap tracks that.
  1164. */
  1165. if (!vdev->barmap[index]) {
  1166. ret = pci_request_selected_regions(pdev,
  1167. 1 << index, "vfio-pci");
  1168. if (ret)
  1169. return ret;
  1170. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  1171. if (!vdev->barmap[index]) {
  1172. pci_release_selected_regions(pdev, 1 << index);
  1173. return -ENOMEM;
  1174. }
  1175. }
  1176. vma->vm_private_data = vdev;
  1177. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1178. vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  1179. /*
  1180. * See remap_pfn_range(), called from vfio_pci_fault() but we can't
  1181. * change vm_flags within the fault handler. Set them now.
  1182. */
  1183. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  1184. vma->vm_ops = &vfio_pci_mmap_ops;
  1185. return 0;
  1186. }
  1187. static void vfio_pci_request(void *device_data, unsigned int count)
  1188. {
  1189. struct vfio_pci_device *vdev = device_data;
  1190. mutex_lock(&vdev->igate);
  1191. if (vdev->req_trigger) {
  1192. if (!(count % 10))
  1193. dev_notice_ratelimited(&vdev->pdev->dev,
  1194. "Relaying device request to user (#%u)\n",
  1195. count);
  1196. eventfd_signal(vdev->req_trigger, 1);
  1197. } else if (count == 0) {
  1198. dev_warn(&vdev->pdev->dev,
  1199. "No device request channel registered, blocked until released by user\n");
  1200. }
  1201. mutex_unlock(&vdev->igate);
  1202. }
  1203. static const struct vfio_device_ops vfio_pci_ops = {
  1204. .name = "vfio-pci",
  1205. .open = vfio_pci_open,
  1206. .release = vfio_pci_release,
  1207. .ioctl = vfio_pci_ioctl,
  1208. .read = vfio_pci_read,
  1209. .write = vfio_pci_write,
  1210. .mmap = vfio_pci_mmap,
  1211. .request = vfio_pci_request,
  1212. };
  1213. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1214. {
  1215. struct vfio_pci_device *vdev;
  1216. struct iommu_group *group;
  1217. int ret;
  1218. if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  1219. return -EINVAL;
  1220. /*
  1221. * Prevent binding to PFs with VFs enabled, this too easily allows
  1222. * userspace instance with VFs and PFs from the same device, which
  1223. * cannot work. Disabling SR-IOV here would initiate removing the
  1224. * VFs, which would unbind the driver, which is prone to blocking
  1225. * if that VF is also in use by vfio-pci. Just reject these PFs
  1226. * and let the user sort it out.
  1227. */
  1228. if (pci_num_vf(pdev)) {
  1229. pci_warn(pdev, "Cannot bind to PF with SR-IOV enabled\n");
  1230. return -EBUSY;
  1231. }
  1232. group = vfio_iommu_group_get(&pdev->dev);
  1233. if (!group)
  1234. return -EINVAL;
  1235. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  1236. if (!vdev) {
  1237. vfio_iommu_group_put(group, &pdev->dev);
  1238. return -ENOMEM;
  1239. }
  1240. vdev->pdev = pdev;
  1241. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  1242. mutex_init(&vdev->igate);
  1243. spin_lock_init(&vdev->irqlock);
  1244. mutex_init(&vdev->ioeventfds_lock);
  1245. INIT_LIST_HEAD(&vdev->dummy_resources_list);
  1246. INIT_LIST_HEAD(&vdev->ioeventfds_list);
  1247. mutex_init(&vdev->vma_lock);
  1248. INIT_LIST_HEAD(&vdev->vma_list);
  1249. init_rwsem(&vdev->memory_lock);
  1250. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  1251. if (ret) {
  1252. vfio_iommu_group_put(group, &pdev->dev);
  1253. kfree(vdev);
  1254. return ret;
  1255. }
  1256. if (vfio_pci_is_vga(pdev)) {
  1257. vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
  1258. vga_set_legacy_decoding(pdev,
  1259. vfio_pci_set_vga_decode(vdev, false));
  1260. }
  1261. if (!disable_idle_d3) {
  1262. /*
  1263. * pci-core sets the device power state to an unknown value at
  1264. * bootup and after being removed from a driver. The only
  1265. * transition it allows from this unknown state is to D0, which
  1266. * typically happens when a driver calls pci_enable_device().
  1267. * We're not ready to enable the device yet, but we do want to
  1268. * be able to get to D3. Therefore first do a D0 transition
  1269. * before going to D3.
  1270. */
  1271. pci_set_power_state(pdev, PCI_D0);
  1272. pci_set_power_state(pdev, PCI_D3hot);
  1273. }
  1274. return ret;
  1275. }
  1276. static void vfio_pci_remove(struct pci_dev *pdev)
  1277. {
  1278. struct vfio_pci_device *vdev;
  1279. vdev = vfio_del_group_dev(&pdev->dev);
  1280. if (!vdev)
  1281. return;
  1282. vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
  1283. kfree(vdev->region);
  1284. mutex_destroy(&vdev->ioeventfds_lock);
  1285. kfree(vdev);
  1286. if (vfio_pci_is_vga(pdev)) {
  1287. vga_client_register(pdev, NULL, NULL, NULL);
  1288. vga_set_legacy_decoding(pdev,
  1289. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
  1290. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
  1291. }
  1292. if (!disable_idle_d3)
  1293. pci_set_power_state(pdev, PCI_D0);
  1294. }
  1295. static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
  1296. pci_channel_state_t state)
  1297. {
  1298. struct vfio_pci_device *vdev;
  1299. struct vfio_device *device;
  1300. device = vfio_device_get_from_dev(&pdev->dev);
  1301. if (device == NULL)
  1302. return PCI_ERS_RESULT_DISCONNECT;
  1303. vdev = vfio_device_data(device);
  1304. if (vdev == NULL) {
  1305. vfio_device_put(device);
  1306. return PCI_ERS_RESULT_DISCONNECT;
  1307. }
  1308. mutex_lock(&vdev->igate);
  1309. if (vdev->err_trigger)
  1310. eventfd_signal(vdev->err_trigger, 1);
  1311. mutex_unlock(&vdev->igate);
  1312. vfio_device_put(device);
  1313. return PCI_ERS_RESULT_CAN_RECOVER;
  1314. }
  1315. static const struct pci_error_handlers vfio_err_handlers = {
  1316. .error_detected = vfio_pci_aer_err_detected,
  1317. };
  1318. static struct pci_driver vfio_pci_driver = {
  1319. .name = "vfio-pci",
  1320. .id_table = NULL, /* only dynamic ids */
  1321. .probe = vfio_pci_probe,
  1322. .remove = vfio_pci_remove,
  1323. .err_handler = &vfio_err_handlers,
  1324. };
  1325. static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
  1326. {
  1327. struct vfio_devices *devs = data;
  1328. struct vfio_device *device;
  1329. if (devs->cur_index == devs->max_index)
  1330. return -ENOSPC;
  1331. device = vfio_device_get_from_dev(&pdev->dev);
  1332. if (!device)
  1333. return -EINVAL;
  1334. if (pci_dev_driver(pdev) != &vfio_pci_driver) {
  1335. vfio_device_put(device);
  1336. return -EBUSY;
  1337. }
  1338. devs->devices[devs->cur_index++] = device;
  1339. return 0;
  1340. }
  1341. static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data)
  1342. {
  1343. struct vfio_devices *devs = data;
  1344. struct vfio_device *device;
  1345. struct vfio_pci_device *vdev;
  1346. if (devs->cur_index == devs->max_index)
  1347. return -ENOSPC;
  1348. device = vfio_device_get_from_dev(&pdev->dev);
  1349. if (!device)
  1350. return -EINVAL;
  1351. if (pci_dev_driver(pdev) != &vfio_pci_driver) {
  1352. vfio_device_put(device);
  1353. return -EBUSY;
  1354. }
  1355. vdev = vfio_device_data(device);
  1356. /*
  1357. * Locking multiple devices is prone to deadlock, runaway and
  1358. * unwind if we hit contention.
  1359. */
  1360. if (!vfio_pci_zap_and_vma_lock(vdev, true)) {
  1361. vfio_device_put(device);
  1362. return -EBUSY;
  1363. }
  1364. devs->devices[devs->cur_index++] = device;
  1365. return 0;
  1366. }
  1367. /*
  1368. * Attempt to do a bus/slot reset if there are devices affected by a reset for
  1369. * this device that are needs_reset and all of the affected devices are unused
  1370. * (!refcnt). Callers are required to hold driver_lock when calling this to
  1371. * prevent device opens and concurrent bus reset attempts. We prevent device
  1372. * unbinds by acquiring and holding a reference to the vfio_device.
  1373. *
  1374. * NB: vfio-core considers a group to be viable even if some devices are
  1375. * bound to drivers like pci-stub or pcieport. Here we require all devices
  1376. * to be bound to vfio_pci since that's the only way we can be sure they
  1377. * stay put.
  1378. */
  1379. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
  1380. {
  1381. struct vfio_devices devs = { .cur_index = 0 };
  1382. int i = 0, ret = -EINVAL;
  1383. bool needs_reset = false, slot = false;
  1384. struct vfio_pci_device *tmp;
  1385. if (!pci_probe_reset_slot(vdev->pdev->slot))
  1386. slot = true;
  1387. else if (pci_probe_reset_bus(vdev->pdev->bus))
  1388. return;
  1389. if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
  1390. &i, slot) || !i)
  1391. return;
  1392. devs.max_index = i;
  1393. devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
  1394. if (!devs.devices)
  1395. return;
  1396. if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
  1397. vfio_pci_get_devs, &devs, slot))
  1398. goto put_devs;
  1399. for (i = 0; i < devs.cur_index; i++) {
  1400. tmp = vfio_device_data(devs.devices[i]);
  1401. if (tmp->needs_reset)
  1402. needs_reset = true;
  1403. if (tmp->refcnt)
  1404. goto put_devs;
  1405. }
  1406. if (needs_reset)
  1407. ret = pci_reset_bus(vdev->pdev);
  1408. put_devs:
  1409. for (i = 0; i < devs.cur_index; i++) {
  1410. tmp = vfio_device_data(devs.devices[i]);
  1411. if (!ret)
  1412. tmp->needs_reset = false;
  1413. if (!tmp->refcnt && !disable_idle_d3)
  1414. pci_set_power_state(tmp->pdev, PCI_D3hot);
  1415. vfio_device_put(devs.devices[i]);
  1416. }
  1417. kfree(devs.devices);
  1418. }
  1419. static void __exit vfio_pci_cleanup(void)
  1420. {
  1421. pci_unregister_driver(&vfio_pci_driver);
  1422. vfio_pci_uninit_perm_bits();
  1423. }
  1424. static void __init vfio_pci_fill_ids(void)
  1425. {
  1426. char *p, *id;
  1427. int rc;
  1428. /* no ids passed actually */
  1429. if (ids[0] == '\0')
  1430. return;
  1431. /* add ids specified in the module parameter */
  1432. p = ids;
  1433. while ((id = strsep(&p, ","))) {
  1434. unsigned int vendor, device, subvendor = PCI_ANY_ID,
  1435. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  1436. int fields;
  1437. if (!strlen(id))
  1438. continue;
  1439. fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
  1440. &vendor, &device, &subvendor, &subdevice,
  1441. &class, &class_mask);
  1442. if (fields < 2) {
  1443. pr_warn("invalid id string \"%s\"\n", id);
  1444. continue;
  1445. }
  1446. rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
  1447. subvendor, subdevice, class, class_mask, 0);
  1448. if (rc)
  1449. pr_warn("failed to add dynamic id [%04x:%04x[%04x:%04x]] class %#08x/%08x (%d)\n",
  1450. vendor, device, subvendor, subdevice,
  1451. class, class_mask, rc);
  1452. else
  1453. pr_info("add [%04x:%04x[%04x:%04x]] class %#08x/%08x\n",
  1454. vendor, device, subvendor, subdevice,
  1455. class, class_mask);
  1456. }
  1457. }
  1458. static int __init vfio_pci_init(void)
  1459. {
  1460. int ret;
  1461. /* Allocate shared config space permision data used by all devices */
  1462. ret = vfio_pci_init_perm_bits();
  1463. if (ret)
  1464. return ret;
  1465. /* Register and scan for devices */
  1466. ret = pci_register_driver(&vfio_pci_driver);
  1467. if (ret)
  1468. goto out_driver;
  1469. vfio_pci_fill_ids();
  1470. return 0;
  1471. out_driver:
  1472. vfio_pci_uninit_perm_bits();
  1473. return ret;
  1474. }
  1475. module_init(vfio_pci_init);
  1476. module_exit(vfio_pci_cleanup);
  1477. MODULE_VERSION(DRIVER_VERSION);
  1478. MODULE_LICENSE("GPL v2");
  1479. MODULE_AUTHOR(DRIVER_AUTHOR);
  1480. MODULE_DESCRIPTION(DRIVER_DESC);