vfio_pci.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. -EFAULT : 0;
  358. } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
  359. struct pci_dev *pdev = vdev->pdev;
  360. struct vfio_region_info info;
  361. minsz = offsetofend(struct vfio_region_info, offset);
  362. if (copy_from_user(&info, (void __user *)arg, minsz))
  363. return -EFAULT;
  364. if (info.argsz < minsz)
  365. return -EINVAL;
  366. switch (info.index) {
  367. case VFIO_PCI_CONFIG_REGION_INDEX:
  368. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  369. info.size = pdev->cfg_size;
  370. info.flags = VFIO_REGION_INFO_FLAG_READ |
  371. VFIO_REGION_INFO_FLAG_WRITE;
  372. break;
  373. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  374. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  375. info.size = pci_resource_len(pdev, info.index);
  376. if (!info.size) {
  377. info.flags = 0;
  378. break;
  379. }
  380. info.flags = VFIO_REGION_INFO_FLAG_READ |
  381. VFIO_REGION_INFO_FLAG_WRITE;
  382. if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
  383. pci_resource_flags(pdev, info.index) &
  384. IORESOURCE_MEM && info.size >= PAGE_SIZE)
  385. info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
  386. break;
  387. case VFIO_PCI_ROM_REGION_INDEX:
  388. {
  389. void __iomem *io;
  390. size_t size;
  391. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  392. info.flags = 0;
  393. /* Report the BAR size, not the ROM size */
  394. info.size = pci_resource_len(pdev, info.index);
  395. if (!info.size)
  396. break;
  397. /* Is it really there? */
  398. io = pci_map_rom(pdev, &size);
  399. if (!io || !size) {
  400. info.size = 0;
  401. break;
  402. }
  403. pci_unmap_rom(pdev, io);
  404. info.flags = VFIO_REGION_INFO_FLAG_READ;
  405. break;
  406. }
  407. case VFIO_PCI_VGA_REGION_INDEX:
  408. if (!vdev->has_vga)
  409. return -EINVAL;
  410. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  411. info.size = 0xc0000;
  412. info.flags = VFIO_REGION_INFO_FLAG_READ |
  413. VFIO_REGION_INFO_FLAG_WRITE;
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. return copy_to_user((void __user *)arg, &info, minsz) ?
  419. -EFAULT : 0;
  420. } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
  421. struct vfio_irq_info info;
  422. minsz = offsetofend(struct vfio_irq_info, count);
  423. if (copy_from_user(&info, (void __user *)arg, minsz))
  424. return -EFAULT;
  425. if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
  426. return -EINVAL;
  427. switch (info.index) {
  428. case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
  429. case VFIO_PCI_REQ_IRQ_INDEX:
  430. break;
  431. case VFIO_PCI_ERR_IRQ_INDEX:
  432. if (pci_is_pcie(vdev->pdev))
  433. break;
  434. /* pass thru to return error */
  435. default:
  436. return -EINVAL;
  437. }
  438. info.flags = VFIO_IRQ_INFO_EVENTFD;
  439. info.count = vfio_pci_get_irq_count(vdev, info.index);
  440. if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
  441. info.flags |= (VFIO_IRQ_INFO_MASKABLE |
  442. VFIO_IRQ_INFO_AUTOMASKED);
  443. else
  444. info.flags |= VFIO_IRQ_INFO_NORESIZE;
  445. return copy_to_user((void __user *)arg, &info, minsz) ?
  446. -EFAULT : 0;
  447. } else if (cmd == VFIO_DEVICE_SET_IRQS) {
  448. struct vfio_irq_set hdr;
  449. size_t size;
  450. u8 *data = NULL;
  451. int max, ret = 0;
  452. minsz = offsetofend(struct vfio_irq_set, count);
  453. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  454. return -EFAULT;
  455. if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
  456. hdr.count >= (U32_MAX - hdr.start) ||
  457. hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  458. VFIO_IRQ_SET_ACTION_TYPE_MASK))
  459. return -EINVAL;
  460. max = vfio_pci_get_irq_count(vdev, hdr.index);
  461. if (hdr.start >= max || hdr.start + hdr.count > max)
  462. return -EINVAL;
  463. switch (hdr.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
  464. case VFIO_IRQ_SET_DATA_NONE:
  465. size = 0;
  466. break;
  467. case VFIO_IRQ_SET_DATA_BOOL:
  468. size = sizeof(uint8_t);
  469. break;
  470. case VFIO_IRQ_SET_DATA_EVENTFD:
  471. size = sizeof(int32_t);
  472. break;
  473. default:
  474. return -EINVAL;
  475. }
  476. if (size) {
  477. if (hdr.argsz - minsz < hdr.count * size)
  478. return -EINVAL;
  479. data = memdup_user((void __user *)(arg + minsz),
  480. hdr.count * size);
  481. if (IS_ERR(data))
  482. return PTR_ERR(data);
  483. }
  484. mutex_lock(&vdev->igate);
  485. ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
  486. hdr.start, hdr.count, data);
  487. mutex_unlock(&vdev->igate);
  488. kfree(data);
  489. return ret;
  490. } else if (cmd == VFIO_DEVICE_RESET) {
  491. return vdev->reset_works ?
  492. pci_try_reset_function(vdev->pdev) : -EINVAL;
  493. } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
  494. struct vfio_pci_hot_reset_info hdr;
  495. struct vfio_pci_fill_info fill = { 0 };
  496. struct vfio_pci_dependent_device *devices = NULL;
  497. bool slot = false;
  498. int ret = 0;
  499. minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
  500. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  501. return -EFAULT;
  502. if (hdr.argsz < minsz)
  503. return -EINVAL;
  504. hdr.flags = 0;
  505. /* Can we do a slot or bus reset or neither? */
  506. if (!pci_probe_reset_slot(vdev->pdev->slot))
  507. slot = true;
  508. else if (pci_probe_reset_bus(vdev->pdev->bus))
  509. return -ENODEV;
  510. /* How many devices are affected? */
  511. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  512. vfio_pci_count_devs,
  513. &fill.max, slot);
  514. if (ret)
  515. return ret;
  516. WARN_ON(!fill.max); /* Should always be at least one */
  517. /*
  518. * If there's enough space, fill it now, otherwise return
  519. * -ENOSPC and the number of devices affected.
  520. */
  521. if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
  522. ret = -ENOSPC;
  523. hdr.count = fill.max;
  524. goto reset_info_exit;
  525. }
  526. devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
  527. if (!devices)
  528. return -ENOMEM;
  529. fill.devices = devices;
  530. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  531. vfio_pci_fill_devs,
  532. &fill, slot);
  533. /*
  534. * If a device was removed between counting and filling,
  535. * we may come up short of fill.max. If a device was
  536. * added, we'll have a return of -EAGAIN above.
  537. */
  538. if (!ret)
  539. hdr.count = fill.cur;
  540. reset_info_exit:
  541. if (copy_to_user((void __user *)arg, &hdr, minsz))
  542. ret = -EFAULT;
  543. if (!ret) {
  544. if (copy_to_user((void __user *)(arg + minsz), devices,
  545. hdr.count * sizeof(*devices)))
  546. ret = -EFAULT;
  547. }
  548. kfree(devices);
  549. return ret;
  550. } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
  551. struct vfio_pci_hot_reset hdr;
  552. int32_t *group_fds;
  553. struct vfio_pci_group_entry *groups;
  554. struct vfio_pci_group_info info;
  555. bool slot = false;
  556. int i, count = 0, ret = 0;
  557. minsz = offsetofend(struct vfio_pci_hot_reset, count);
  558. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  559. return -EFAULT;
  560. if (hdr.argsz < minsz || hdr.flags)
  561. return -EINVAL;
  562. /* Can we do a slot or bus reset or neither? */
  563. if (!pci_probe_reset_slot(vdev->pdev->slot))
  564. slot = true;
  565. else if (pci_probe_reset_bus(vdev->pdev->bus))
  566. return -ENODEV;
  567. /*
  568. * We can't let userspace give us an arbitrarily large
  569. * buffer to copy, so verify how many we think there
  570. * could be. Note groups can have multiple devices so
  571. * one group per device is the max.
  572. */
  573. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  574. vfio_pci_count_devs,
  575. &count, slot);
  576. if (ret)
  577. return ret;
  578. /* Somewhere between 1 and count is OK */
  579. if (!hdr.count || hdr.count > count)
  580. return -EINVAL;
  581. group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
  582. groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
  583. if (!group_fds || !groups) {
  584. kfree(group_fds);
  585. kfree(groups);
  586. return -ENOMEM;
  587. }
  588. if (copy_from_user(group_fds, (void __user *)(arg + minsz),
  589. hdr.count * sizeof(*group_fds))) {
  590. kfree(group_fds);
  591. kfree(groups);
  592. return -EFAULT;
  593. }
  594. /*
  595. * For each group_fd, get the group through the vfio external
  596. * user interface and store the group and iommu ID. This
  597. * ensures the group is held across the reset.
  598. */
  599. for (i = 0; i < hdr.count; i++) {
  600. struct vfio_group *group;
  601. struct fd f = fdget(group_fds[i]);
  602. if (!f.file) {
  603. ret = -EBADF;
  604. break;
  605. }
  606. group = vfio_group_get_external_user(f.file);
  607. fdput(f);
  608. if (IS_ERR(group)) {
  609. ret = PTR_ERR(group);
  610. break;
  611. }
  612. groups[i].group = group;
  613. groups[i].id = vfio_external_user_iommu_id(group);
  614. }
  615. kfree(group_fds);
  616. /* release reference to groups on error */
  617. if (ret)
  618. goto hot_reset_release;
  619. info.count = hdr.count;
  620. info.groups = groups;
  621. /*
  622. * Test whether all the affected devices are contained
  623. * by the set of groups provided by the user.
  624. */
  625. ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
  626. vfio_pci_validate_devs,
  627. &info, slot);
  628. if (!ret)
  629. /* User has access, do the reset */
  630. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  631. pci_try_reset_bus(vdev->pdev->bus);
  632. hot_reset_release:
  633. for (i--; i >= 0; i--)
  634. vfio_group_put_external_user(groups[i].group);
  635. kfree(groups);
  636. return ret;
  637. }
  638. return -ENOTTY;
  639. }
  640. static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
  641. size_t count, loff_t *ppos, bool iswrite)
  642. {
  643. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  644. struct vfio_pci_device *vdev = device_data;
  645. if (index >= VFIO_PCI_NUM_REGIONS)
  646. return -EINVAL;
  647. switch (index) {
  648. case VFIO_PCI_CONFIG_REGION_INDEX:
  649. return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
  650. case VFIO_PCI_ROM_REGION_INDEX:
  651. if (iswrite)
  652. return -EINVAL;
  653. return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
  654. case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
  655. return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
  656. case VFIO_PCI_VGA_REGION_INDEX:
  657. return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
  658. }
  659. return -EINVAL;
  660. }
  661. static ssize_t vfio_pci_read(void *device_data, char __user *buf,
  662. size_t count, loff_t *ppos)
  663. {
  664. if (!count)
  665. return 0;
  666. return vfio_pci_rw(device_data, buf, count, ppos, false);
  667. }
  668. static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
  669. size_t count, loff_t *ppos)
  670. {
  671. if (!count)
  672. return 0;
  673. return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
  674. }
  675. static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
  676. {
  677. struct vfio_pci_device *vdev = device_data;
  678. struct pci_dev *pdev = vdev->pdev;
  679. unsigned int index;
  680. u64 phys_len, req_len, pgoff, req_start;
  681. int ret;
  682. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  683. if (vma->vm_end < vma->vm_start)
  684. return -EINVAL;
  685. if ((vma->vm_flags & VM_SHARED) == 0)
  686. return -EINVAL;
  687. if (index >= VFIO_PCI_ROM_REGION_INDEX)
  688. return -EINVAL;
  689. if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
  690. return -EINVAL;
  691. phys_len = pci_resource_len(pdev, index);
  692. req_len = vma->vm_end - vma->vm_start;
  693. pgoff = vma->vm_pgoff &
  694. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  695. req_start = pgoff << PAGE_SHIFT;
  696. if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
  697. return -EINVAL;
  698. if (index == vdev->msix_bar) {
  699. /*
  700. * Disallow mmaps overlapping the MSI-X table; users don't
  701. * get to touch this directly. We could find somewhere
  702. * else to map the overlap, but page granularity is only
  703. * a recommendation, not a requirement, so the user needs
  704. * to know which bits are real. Requiring them to mmap
  705. * around the table makes that clear.
  706. */
  707. /* If neither entirely above nor below, then it overlaps */
  708. if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
  709. req_start + req_len <= vdev->msix_offset))
  710. return -EINVAL;
  711. }
  712. /*
  713. * Even though we don't make use of the barmap for the mmap,
  714. * we need to request the region and the barmap tracks that.
  715. */
  716. if (!vdev->barmap[index]) {
  717. ret = pci_request_selected_regions(pdev,
  718. 1 << index, "vfio-pci");
  719. if (ret)
  720. return ret;
  721. vdev->barmap[index] = pci_iomap(pdev, index, 0);
  722. if (!vdev->barmap[index]) {
  723. pci_release_selected_regions(pdev, 1 << index);
  724. return -ENOMEM;
  725. }
  726. }
  727. vma->vm_private_data = vdev;
  728. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  729. vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
  730. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  731. req_len, vma->vm_page_prot);
  732. }
  733. static void vfio_pci_request(void *device_data, unsigned int count)
  734. {
  735. struct vfio_pci_device *vdev = device_data;
  736. mutex_lock(&vdev->igate);
  737. if (vdev->req_trigger) {
  738. if (!(count % 10))
  739. dev_notice_ratelimited(&vdev->pdev->dev,
  740. "Relaying device request to user (#%u)\n",
  741. count);
  742. eventfd_signal(vdev->req_trigger, 1);
  743. } else if (count == 0) {
  744. dev_warn(&vdev->pdev->dev,
  745. "No device request channel registered, blocked until released by user\n");
  746. }
  747. mutex_unlock(&vdev->igate);
  748. }
  749. static const struct vfio_device_ops vfio_pci_ops = {
  750. .name = "vfio-pci",
  751. .open = vfio_pci_open,
  752. .release = vfio_pci_release,
  753. .ioctl = vfio_pci_ioctl,
  754. .read = vfio_pci_read,
  755. .write = vfio_pci_write,
  756. .mmap = vfio_pci_mmap,
  757. .request = vfio_pci_request,
  758. };
  759. static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  760. {
  761. struct vfio_pci_device *vdev;
  762. struct iommu_group *group;
  763. int ret;
  764. if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  765. return -EINVAL;
  766. group = iommu_group_get(&pdev->dev);
  767. if (!group)
  768. return -EINVAL;
  769. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  770. if (!vdev) {
  771. iommu_group_put(group);
  772. return -ENOMEM;
  773. }
  774. vdev->pdev = pdev;
  775. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  776. mutex_init(&vdev->igate);
  777. spin_lock_init(&vdev->irqlock);
  778. ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
  779. if (ret) {
  780. iommu_group_put(group);
  781. kfree(vdev);
  782. return ret;
  783. }
  784. if (vfio_pci_is_vga(pdev)) {
  785. vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
  786. vga_set_legacy_decoding(pdev,
  787. vfio_pci_set_vga_decode(vdev, false));
  788. }
  789. if (!disable_idle_d3) {
  790. /*
  791. * pci-core sets the device power state to an unknown value at
  792. * bootup and after being removed from a driver. The only
  793. * transition it allows from this unknown state is to D0, which
  794. * typically happens when a driver calls pci_enable_device().
  795. * We're not ready to enable the device yet, but we do want to
  796. * be able to get to D3. Therefore first do a D0 transition
  797. * before going to D3.
  798. */
  799. pci_set_power_state(pdev, PCI_D0);
  800. pci_set_power_state(pdev, PCI_D3hot);
  801. }
  802. return ret;
  803. }
  804. static void vfio_pci_remove(struct pci_dev *pdev)
  805. {
  806. struct vfio_pci_device *vdev;
  807. vdev = vfio_del_group_dev(&pdev->dev);
  808. if (!vdev)
  809. return;
  810. iommu_group_put(pdev->dev.iommu_group);
  811. kfree(vdev);
  812. if (vfio_pci_is_vga(pdev)) {
  813. vga_client_register(pdev, NULL, NULL, NULL);
  814. vga_set_legacy_decoding(pdev,
  815. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
  816. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
  817. }
  818. if (!disable_idle_d3)
  819. pci_set_power_state(pdev, PCI_D0);
  820. }
  821. static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
  822. pci_channel_state_t state)
  823. {
  824. struct vfio_pci_device *vdev;
  825. struct vfio_device *device;
  826. device = vfio_device_get_from_dev(&pdev->dev);
  827. if (device == NULL)
  828. return PCI_ERS_RESULT_DISCONNECT;
  829. vdev = vfio_device_data(device);
  830. if (vdev == NULL) {
  831. vfio_device_put(device);
  832. return PCI_ERS_RESULT_DISCONNECT;
  833. }
  834. mutex_lock(&vdev->igate);
  835. if (vdev->err_trigger)
  836. eventfd_signal(vdev->err_trigger, 1);
  837. mutex_unlock(&vdev->igate);
  838. vfio_device_put(device);
  839. return PCI_ERS_RESULT_CAN_RECOVER;
  840. }
  841. static const struct pci_error_handlers vfio_err_handlers = {
  842. .error_detected = vfio_pci_aer_err_detected,
  843. };
  844. static struct pci_driver vfio_pci_driver = {
  845. .name = "vfio-pci",
  846. .id_table = NULL, /* only dynamic ids */
  847. .probe = vfio_pci_probe,
  848. .remove = vfio_pci_remove,
  849. .err_handler = &vfio_err_handlers,
  850. };
  851. struct vfio_devices {
  852. struct vfio_device **devices;
  853. int cur_index;
  854. int max_index;
  855. };
  856. static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
  857. {
  858. struct vfio_devices *devs = data;
  859. struct vfio_device *device;
  860. if (devs->cur_index == devs->max_index)
  861. return -ENOSPC;
  862. device = vfio_device_get_from_dev(&pdev->dev);
  863. if (!device)
  864. return -EINVAL;
  865. if (pci_dev_driver(pdev) != &vfio_pci_driver) {
  866. vfio_device_put(device);
  867. return -EBUSY;
  868. }
  869. devs->devices[devs->cur_index++] = device;
  870. return 0;
  871. }
  872. /*
  873. * Attempt to do a bus/slot reset if there are devices affected by a reset for
  874. * this device that are needs_reset and all of the affected devices are unused
  875. * (!refcnt). Callers are required to hold driver_lock when calling this to
  876. * prevent device opens and concurrent bus reset attempts. We prevent device
  877. * unbinds by acquiring and holding a reference to the vfio_device.
  878. *
  879. * NB: vfio-core considers a group to be viable even if some devices are
  880. * bound to drivers like pci-stub or pcieport. Here we require all devices
  881. * to be bound to vfio_pci since that's the only way we can be sure they
  882. * stay put.
  883. */
  884. static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
  885. {
  886. struct vfio_devices devs = { .cur_index = 0 };
  887. int i = 0, ret = -EINVAL;
  888. bool needs_reset = false, slot = false;
  889. struct vfio_pci_device *tmp;
  890. if (!pci_probe_reset_slot(vdev->pdev->slot))
  891. slot = true;
  892. else if (pci_probe_reset_bus(vdev->pdev->bus))
  893. return;
  894. if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
  895. &i, slot) || !i)
  896. return;
  897. devs.max_index = i;
  898. devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
  899. if (!devs.devices)
  900. return;
  901. if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
  902. vfio_pci_get_devs, &devs, slot))
  903. goto put_devs;
  904. for (i = 0; i < devs.cur_index; i++) {
  905. tmp = vfio_device_data(devs.devices[i]);
  906. if (tmp->needs_reset)
  907. needs_reset = true;
  908. if (tmp->refcnt)
  909. goto put_devs;
  910. }
  911. if (needs_reset)
  912. ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
  913. pci_try_reset_bus(vdev->pdev->bus);
  914. put_devs:
  915. for (i = 0; i < devs.cur_index; i++) {
  916. tmp = vfio_device_data(devs.devices[i]);
  917. if (!ret)
  918. tmp->needs_reset = false;
  919. if (!tmp->refcnt && !disable_idle_d3)
  920. pci_set_power_state(tmp->pdev, PCI_D3hot);
  921. vfio_device_put(devs.devices[i]);
  922. }
  923. kfree(devs.devices);
  924. }
  925. static void __exit vfio_pci_cleanup(void)
  926. {
  927. pci_unregister_driver(&vfio_pci_driver);
  928. vfio_pci_uninit_perm_bits();
  929. }
  930. static void __init vfio_pci_fill_ids(void)
  931. {
  932. char *p, *id;
  933. int rc;
  934. /* no ids passed actually */
  935. if (ids[0] == '\0')
  936. return;
  937. /* add ids specified in the module parameter */
  938. p = ids;
  939. while ((id = strsep(&p, ","))) {
  940. unsigned int vendor, device, subvendor = PCI_ANY_ID,
  941. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  942. int fields;
  943. if (!strlen(id))
  944. continue;
  945. fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
  946. &vendor, &device, &subvendor, &subdevice,
  947. &class, &class_mask);
  948. if (fields < 2) {
  949. pr_warn("invalid id string \"%s\"\n", id);
  950. continue;
  951. }
  952. rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
  953. subvendor, subdevice, class, class_mask, 0);
  954. if (rc)
  955. pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n",
  956. vendor, device, subvendor, subdevice,
  957. class, class_mask, rc);
  958. else
  959. pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n",
  960. vendor, device, subvendor, subdevice,
  961. class, class_mask);
  962. }
  963. }
  964. static int __init vfio_pci_init(void)
  965. {
  966. int ret;
  967. /* Allocate shared config space permision data used by all devices */
  968. ret = vfio_pci_init_perm_bits();
  969. if (ret)
  970. return ret;
  971. /* Register and scan for devices */
  972. ret = pci_register_driver(&vfio_pci_driver);
  973. if (ret)
  974. goto out_driver;
  975. vfio_pci_fill_ids();
  976. return 0;
  977. out_driver:
  978. vfio_pci_uninit_perm_bits();
  979. return ret;
  980. }
  981. module_init(vfio_pci_init);
  982. module_exit(vfio_pci_cleanup);
  983. MODULE_VERSION(DRIVER_VERSION);
  984. MODULE_LICENSE("GPL v2");
  985. MODULE_AUTHOR(DRIVER_AUTHOR);
  986. MODULE_DESCRIPTION(DRIVER_DESC);