vfio_pci.c 29 KB

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