vfio_pci.c 37 KB

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