vfio_pci.c 30 KB

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