assigned-dev.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * Kernel-based Virtual Machine - device assignment support
  3. *
  4. * Copyright (C) 2010 Red Hat, Inc. and/or its affiliates.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include <linux/kvm_host.h>
  11. #include <linux/kvm.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/errno.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/pci.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. #include <linux/namei.h>
  20. #include <linux/fs.h>
  21. #include "irq.h"
  22. static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
  23. int assigned_dev_id)
  24. {
  25. struct list_head *ptr;
  26. struct kvm_assigned_dev_kernel *match;
  27. list_for_each(ptr, head) {
  28. match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
  29. if (match->assigned_dev_id == assigned_dev_id)
  30. return match;
  31. }
  32. return NULL;
  33. }
  34. static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
  35. *assigned_dev, int irq)
  36. {
  37. int i, index;
  38. struct msix_entry *host_msix_entries;
  39. host_msix_entries = assigned_dev->host_msix_entries;
  40. index = -1;
  41. for (i = 0; i < assigned_dev->entries_nr; i++)
  42. if (irq == host_msix_entries[i].vector) {
  43. index = i;
  44. break;
  45. }
  46. if (index < 0)
  47. printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
  48. return index;
  49. }
  50. static irqreturn_t kvm_assigned_dev_intx(int irq, void *dev_id)
  51. {
  52. struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
  53. int ret;
  54. spin_lock(&assigned_dev->intx_lock);
  55. if (pci_check_and_mask_intx(assigned_dev->dev)) {
  56. assigned_dev->host_irq_disabled = true;
  57. ret = IRQ_WAKE_THREAD;
  58. } else
  59. ret = IRQ_NONE;
  60. spin_unlock(&assigned_dev->intx_lock);
  61. return ret;
  62. }
  63. static void
  64. kvm_assigned_dev_raise_guest_irq(struct kvm_assigned_dev_kernel *assigned_dev,
  65. int vector)
  66. {
  67. if (unlikely(assigned_dev->irq_requested_type &
  68. KVM_DEV_IRQ_GUEST_INTX)) {
  69. spin_lock(&assigned_dev->intx_mask_lock);
  70. if (!(assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX))
  71. kvm_set_irq(assigned_dev->kvm,
  72. assigned_dev->irq_source_id, vector, 1,
  73. false);
  74. spin_unlock(&assigned_dev->intx_mask_lock);
  75. } else
  76. kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
  77. vector, 1, false);
  78. }
  79. static irqreturn_t kvm_assigned_dev_thread_intx(int irq, void *dev_id)
  80. {
  81. struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
  82. if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) {
  83. spin_lock_irq(&assigned_dev->intx_lock);
  84. disable_irq_nosync(irq);
  85. assigned_dev->host_irq_disabled = true;
  86. spin_unlock_irq(&assigned_dev->intx_lock);
  87. }
  88. kvm_assigned_dev_raise_guest_irq(assigned_dev,
  89. assigned_dev->guest_irq);
  90. return IRQ_HANDLED;
  91. }
  92. #ifdef __KVM_HAVE_MSI
  93. static irqreturn_t kvm_assigned_dev_msi(int irq, void *dev_id)
  94. {
  95. struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
  96. int ret = kvm_set_irq_inatomic(assigned_dev->kvm,
  97. assigned_dev->irq_source_id,
  98. assigned_dev->guest_irq, 1);
  99. return unlikely(ret == -EWOULDBLOCK) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  100. }
  101. static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
  102. {
  103. struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
  104. kvm_assigned_dev_raise_guest_irq(assigned_dev,
  105. assigned_dev->guest_irq);
  106. return IRQ_HANDLED;
  107. }
  108. #endif
  109. #ifdef __KVM_HAVE_MSIX
  110. static irqreturn_t kvm_assigned_dev_msix(int irq, void *dev_id)
  111. {
  112. struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
  113. int index = find_index_from_host_irq(assigned_dev, irq);
  114. u32 vector;
  115. int ret = 0;
  116. if (index >= 0) {
  117. vector = assigned_dev->guest_msix_entries[index].vector;
  118. ret = kvm_set_irq_inatomic(assigned_dev->kvm,
  119. assigned_dev->irq_source_id,
  120. vector, 1);
  121. }
  122. return unlikely(ret == -EWOULDBLOCK) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  123. }
  124. static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
  125. {
  126. struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
  127. int index = find_index_from_host_irq(assigned_dev, irq);
  128. u32 vector;
  129. if (index >= 0) {
  130. vector = assigned_dev->guest_msix_entries[index].vector;
  131. kvm_assigned_dev_raise_guest_irq(assigned_dev, vector);
  132. }
  133. return IRQ_HANDLED;
  134. }
  135. #endif
  136. /* Ack the irq line for an assigned device */
  137. static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
  138. {
  139. struct kvm_assigned_dev_kernel *dev =
  140. container_of(kian, struct kvm_assigned_dev_kernel,
  141. ack_notifier);
  142. kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0, false);
  143. spin_lock(&dev->intx_mask_lock);
  144. if (!(dev->flags & KVM_DEV_ASSIGN_MASK_INTX)) {
  145. bool reassert = false;
  146. spin_lock_irq(&dev->intx_lock);
  147. /*
  148. * The guest IRQ may be shared so this ack can come from an
  149. * IRQ for another guest device.
  150. */
  151. if (dev->host_irq_disabled) {
  152. if (!(dev->flags & KVM_DEV_ASSIGN_PCI_2_3))
  153. enable_irq(dev->host_irq);
  154. else if (!pci_check_and_unmask_intx(dev->dev))
  155. reassert = true;
  156. dev->host_irq_disabled = reassert;
  157. }
  158. spin_unlock_irq(&dev->intx_lock);
  159. if (reassert)
  160. kvm_set_irq(dev->kvm, dev->irq_source_id,
  161. dev->guest_irq, 1, false);
  162. }
  163. spin_unlock(&dev->intx_mask_lock);
  164. }
  165. static void deassign_guest_irq(struct kvm *kvm,
  166. struct kvm_assigned_dev_kernel *assigned_dev)
  167. {
  168. if (assigned_dev->ack_notifier.gsi != -1)
  169. kvm_unregister_irq_ack_notifier(kvm,
  170. &assigned_dev->ack_notifier);
  171. kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
  172. assigned_dev->guest_irq, 0, false);
  173. if (assigned_dev->irq_source_id != -1)
  174. kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
  175. assigned_dev->irq_source_id = -1;
  176. assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
  177. }
  178. /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
  179. static void deassign_host_irq(struct kvm *kvm,
  180. struct kvm_assigned_dev_kernel *assigned_dev)
  181. {
  182. /*
  183. * We disable irq here to prevent further events.
  184. *
  185. * Notice this maybe result in nested disable if the interrupt type is
  186. * INTx, but it's OK for we are going to free it.
  187. *
  188. * If this function is a part of VM destroy, please ensure that till
  189. * now, the kvm state is still legal for probably we also have to wait
  190. * on a currently running IRQ handler.
  191. */
  192. if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
  193. int i;
  194. for (i = 0; i < assigned_dev->entries_nr; i++)
  195. disable_irq(assigned_dev->host_msix_entries[i].vector);
  196. for (i = 0; i < assigned_dev->entries_nr; i++)
  197. free_irq(assigned_dev->host_msix_entries[i].vector,
  198. assigned_dev);
  199. assigned_dev->entries_nr = 0;
  200. kfree(assigned_dev->host_msix_entries);
  201. kfree(assigned_dev->guest_msix_entries);
  202. pci_disable_msix(assigned_dev->dev);
  203. } else {
  204. /* Deal with MSI and INTx */
  205. if ((assigned_dev->irq_requested_type &
  206. KVM_DEV_IRQ_HOST_INTX) &&
  207. (assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) {
  208. spin_lock_irq(&assigned_dev->intx_lock);
  209. pci_intx(assigned_dev->dev, false);
  210. spin_unlock_irq(&assigned_dev->intx_lock);
  211. synchronize_irq(assigned_dev->host_irq);
  212. } else
  213. disable_irq(assigned_dev->host_irq);
  214. free_irq(assigned_dev->host_irq, assigned_dev);
  215. if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
  216. pci_disable_msi(assigned_dev->dev);
  217. }
  218. assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
  219. }
  220. static int kvm_deassign_irq(struct kvm *kvm,
  221. struct kvm_assigned_dev_kernel *assigned_dev,
  222. unsigned long irq_requested_type)
  223. {
  224. unsigned long guest_irq_type, host_irq_type;
  225. if (!irqchip_in_kernel(kvm))
  226. return -EINVAL;
  227. /* no irq assignment to deassign */
  228. if (!assigned_dev->irq_requested_type)
  229. return -ENXIO;
  230. host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
  231. guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
  232. if (host_irq_type)
  233. deassign_host_irq(kvm, assigned_dev);
  234. if (guest_irq_type)
  235. deassign_guest_irq(kvm, assigned_dev);
  236. return 0;
  237. }
  238. static void kvm_free_assigned_irq(struct kvm *kvm,
  239. struct kvm_assigned_dev_kernel *assigned_dev)
  240. {
  241. kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
  242. }
  243. static void kvm_free_assigned_device(struct kvm *kvm,
  244. struct kvm_assigned_dev_kernel
  245. *assigned_dev)
  246. {
  247. kvm_free_assigned_irq(kvm, assigned_dev);
  248. pci_reset_function(assigned_dev->dev);
  249. if (pci_load_and_free_saved_state(assigned_dev->dev,
  250. &assigned_dev->pci_saved_state))
  251. printk(KERN_INFO "%s: Couldn't reload %s saved state\n",
  252. __func__, dev_name(&assigned_dev->dev->dev));
  253. else
  254. pci_restore_state(assigned_dev->dev);
  255. assigned_dev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
  256. pci_release_regions(assigned_dev->dev);
  257. pci_disable_device(assigned_dev->dev);
  258. pci_dev_put(assigned_dev->dev);
  259. list_del(&assigned_dev->list);
  260. kfree(assigned_dev);
  261. }
  262. void kvm_free_all_assigned_devices(struct kvm *kvm)
  263. {
  264. struct list_head *ptr, *ptr2;
  265. struct kvm_assigned_dev_kernel *assigned_dev;
  266. list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
  267. assigned_dev = list_entry(ptr,
  268. struct kvm_assigned_dev_kernel,
  269. list);
  270. kvm_free_assigned_device(kvm, assigned_dev);
  271. }
  272. }
  273. static int assigned_device_enable_host_intx(struct kvm *kvm,
  274. struct kvm_assigned_dev_kernel *dev)
  275. {
  276. irq_handler_t irq_handler;
  277. unsigned long flags;
  278. dev->host_irq = dev->dev->irq;
  279. /*
  280. * We can only share the IRQ line with other host devices if we are
  281. * able to disable the IRQ source at device-level - independently of
  282. * the guest driver. Otherwise host devices may suffer from unbounded
  283. * IRQ latencies when the guest keeps the line asserted.
  284. */
  285. if (dev->flags & KVM_DEV_ASSIGN_PCI_2_3) {
  286. irq_handler = kvm_assigned_dev_intx;
  287. flags = IRQF_SHARED;
  288. } else {
  289. irq_handler = NULL;
  290. flags = IRQF_ONESHOT;
  291. }
  292. if (request_threaded_irq(dev->host_irq, irq_handler,
  293. kvm_assigned_dev_thread_intx, flags,
  294. dev->irq_name, dev))
  295. return -EIO;
  296. if (dev->flags & KVM_DEV_ASSIGN_PCI_2_3) {
  297. spin_lock_irq(&dev->intx_lock);
  298. pci_intx(dev->dev, true);
  299. spin_unlock_irq(&dev->intx_lock);
  300. }
  301. return 0;
  302. }
  303. #ifdef __KVM_HAVE_MSI
  304. static int assigned_device_enable_host_msi(struct kvm *kvm,
  305. struct kvm_assigned_dev_kernel *dev)
  306. {
  307. int r;
  308. if (!dev->dev->msi_enabled) {
  309. r = pci_enable_msi(dev->dev);
  310. if (r)
  311. return r;
  312. }
  313. dev->host_irq = dev->dev->irq;
  314. if (request_threaded_irq(dev->host_irq, kvm_assigned_dev_msi,
  315. kvm_assigned_dev_thread_msi, 0,
  316. dev->irq_name, dev)) {
  317. pci_disable_msi(dev->dev);
  318. return -EIO;
  319. }
  320. return 0;
  321. }
  322. #endif
  323. #ifdef __KVM_HAVE_MSIX
  324. static int assigned_device_enable_host_msix(struct kvm *kvm,
  325. struct kvm_assigned_dev_kernel *dev)
  326. {
  327. int i, r = -EINVAL;
  328. /* host_msix_entries and guest_msix_entries should have been
  329. * initialized */
  330. if (dev->entries_nr == 0)
  331. return r;
  332. r = pci_enable_msix_exact(dev->dev,
  333. dev->host_msix_entries, dev->entries_nr);
  334. if (r)
  335. return r;
  336. for (i = 0; i < dev->entries_nr; i++) {
  337. r = request_threaded_irq(dev->host_msix_entries[i].vector,
  338. kvm_assigned_dev_msix,
  339. kvm_assigned_dev_thread_msix,
  340. 0, dev->irq_name, dev);
  341. if (r)
  342. goto err;
  343. }
  344. return 0;
  345. err:
  346. for (i -= 1; i >= 0; i--)
  347. free_irq(dev->host_msix_entries[i].vector, dev);
  348. pci_disable_msix(dev->dev);
  349. return r;
  350. }
  351. #endif
  352. static int assigned_device_enable_guest_intx(struct kvm *kvm,
  353. struct kvm_assigned_dev_kernel *dev,
  354. struct kvm_assigned_irq *irq)
  355. {
  356. dev->guest_irq = irq->guest_irq;
  357. dev->ack_notifier.gsi = irq->guest_irq;
  358. return 0;
  359. }
  360. #ifdef __KVM_HAVE_MSI
  361. static int assigned_device_enable_guest_msi(struct kvm *kvm,
  362. struct kvm_assigned_dev_kernel *dev,
  363. struct kvm_assigned_irq *irq)
  364. {
  365. dev->guest_irq = irq->guest_irq;
  366. dev->ack_notifier.gsi = -1;
  367. return 0;
  368. }
  369. #endif
  370. #ifdef __KVM_HAVE_MSIX
  371. static int assigned_device_enable_guest_msix(struct kvm *kvm,
  372. struct kvm_assigned_dev_kernel *dev,
  373. struct kvm_assigned_irq *irq)
  374. {
  375. dev->guest_irq = irq->guest_irq;
  376. dev->ack_notifier.gsi = -1;
  377. return 0;
  378. }
  379. #endif
  380. static int assign_host_irq(struct kvm *kvm,
  381. struct kvm_assigned_dev_kernel *dev,
  382. __u32 host_irq_type)
  383. {
  384. int r = -EEXIST;
  385. if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
  386. return r;
  387. snprintf(dev->irq_name, sizeof(dev->irq_name), "kvm:%s",
  388. pci_name(dev->dev));
  389. switch (host_irq_type) {
  390. case KVM_DEV_IRQ_HOST_INTX:
  391. r = assigned_device_enable_host_intx(kvm, dev);
  392. break;
  393. #ifdef __KVM_HAVE_MSI
  394. case KVM_DEV_IRQ_HOST_MSI:
  395. r = assigned_device_enable_host_msi(kvm, dev);
  396. break;
  397. #endif
  398. #ifdef __KVM_HAVE_MSIX
  399. case KVM_DEV_IRQ_HOST_MSIX:
  400. r = assigned_device_enable_host_msix(kvm, dev);
  401. break;
  402. #endif
  403. default:
  404. r = -EINVAL;
  405. }
  406. dev->host_irq_disabled = false;
  407. if (!r)
  408. dev->irq_requested_type |= host_irq_type;
  409. return r;
  410. }
  411. static int assign_guest_irq(struct kvm *kvm,
  412. struct kvm_assigned_dev_kernel *dev,
  413. struct kvm_assigned_irq *irq,
  414. unsigned long guest_irq_type)
  415. {
  416. int id;
  417. int r = -EEXIST;
  418. if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
  419. return r;
  420. id = kvm_request_irq_source_id(kvm);
  421. if (id < 0)
  422. return id;
  423. dev->irq_source_id = id;
  424. switch (guest_irq_type) {
  425. case KVM_DEV_IRQ_GUEST_INTX:
  426. r = assigned_device_enable_guest_intx(kvm, dev, irq);
  427. break;
  428. #ifdef __KVM_HAVE_MSI
  429. case KVM_DEV_IRQ_GUEST_MSI:
  430. r = assigned_device_enable_guest_msi(kvm, dev, irq);
  431. break;
  432. #endif
  433. #ifdef __KVM_HAVE_MSIX
  434. case KVM_DEV_IRQ_GUEST_MSIX:
  435. r = assigned_device_enable_guest_msix(kvm, dev, irq);
  436. break;
  437. #endif
  438. default:
  439. r = -EINVAL;
  440. }
  441. if (!r) {
  442. dev->irq_requested_type |= guest_irq_type;
  443. if (dev->ack_notifier.gsi != -1)
  444. kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
  445. } else
  446. kvm_free_irq_source_id(kvm, dev->irq_source_id);
  447. return r;
  448. }
  449. /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
  450. static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
  451. struct kvm_assigned_irq *assigned_irq)
  452. {
  453. int r = -EINVAL;
  454. struct kvm_assigned_dev_kernel *match;
  455. unsigned long host_irq_type, guest_irq_type;
  456. if (!irqchip_in_kernel(kvm))
  457. return r;
  458. mutex_lock(&kvm->lock);
  459. r = -ENODEV;
  460. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  461. assigned_irq->assigned_dev_id);
  462. if (!match)
  463. goto out;
  464. host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
  465. guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
  466. r = -EINVAL;
  467. /* can only assign one type at a time */
  468. if (hweight_long(host_irq_type) > 1)
  469. goto out;
  470. if (hweight_long(guest_irq_type) > 1)
  471. goto out;
  472. if (host_irq_type == 0 && guest_irq_type == 0)
  473. goto out;
  474. r = 0;
  475. if (host_irq_type)
  476. r = assign_host_irq(kvm, match, host_irq_type);
  477. if (r)
  478. goto out;
  479. if (guest_irq_type)
  480. r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
  481. out:
  482. mutex_unlock(&kvm->lock);
  483. return r;
  484. }
  485. static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
  486. struct kvm_assigned_irq
  487. *assigned_irq)
  488. {
  489. int r = -ENODEV;
  490. struct kvm_assigned_dev_kernel *match;
  491. unsigned long irq_type;
  492. mutex_lock(&kvm->lock);
  493. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  494. assigned_irq->assigned_dev_id);
  495. if (!match)
  496. goto out;
  497. irq_type = assigned_irq->flags & (KVM_DEV_IRQ_HOST_MASK |
  498. KVM_DEV_IRQ_GUEST_MASK);
  499. r = kvm_deassign_irq(kvm, match, irq_type);
  500. out:
  501. mutex_unlock(&kvm->lock);
  502. return r;
  503. }
  504. /*
  505. * We want to test whether the caller has been granted permissions to
  506. * use this device. To be able to configure and control the device,
  507. * the user needs access to PCI configuration space and BAR resources.
  508. * These are accessed through PCI sysfs. PCI config space is often
  509. * passed to the process calling this ioctl via file descriptor, so we
  510. * can't rely on access to that file. We can check for permissions
  511. * on each of the BAR resource files, which is a pretty clear
  512. * indicator that the user has been granted access to the device.
  513. */
  514. static int probe_sysfs_permissions(struct pci_dev *dev)
  515. {
  516. #ifdef CONFIG_SYSFS
  517. int i;
  518. bool bar_found = false;
  519. for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++) {
  520. char *kpath, *syspath;
  521. struct path path;
  522. struct inode *inode;
  523. int r;
  524. if (!pci_resource_len(dev, i))
  525. continue;
  526. kpath = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  527. if (!kpath)
  528. return -ENOMEM;
  529. /* Per sysfs-rules, sysfs is always at /sys */
  530. syspath = kasprintf(GFP_KERNEL, "/sys%s/resource%d", kpath, i);
  531. kfree(kpath);
  532. if (!syspath)
  533. return -ENOMEM;
  534. r = kern_path(syspath, LOOKUP_FOLLOW, &path);
  535. kfree(syspath);
  536. if (r)
  537. return r;
  538. inode = path.dentry->d_inode;
  539. r = inode_permission(inode, MAY_READ | MAY_WRITE | MAY_ACCESS);
  540. path_put(&path);
  541. if (r)
  542. return r;
  543. bar_found = true;
  544. }
  545. /* If no resources, probably something special */
  546. if (!bar_found)
  547. return -EPERM;
  548. return 0;
  549. #else
  550. return -EINVAL; /* No way to control the device without sysfs */
  551. #endif
  552. }
  553. static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
  554. struct kvm_assigned_pci_dev *assigned_dev)
  555. {
  556. int r = 0, idx;
  557. struct kvm_assigned_dev_kernel *match;
  558. struct pci_dev *dev;
  559. if (!(assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU))
  560. return -EINVAL;
  561. mutex_lock(&kvm->lock);
  562. idx = srcu_read_lock(&kvm->srcu);
  563. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  564. assigned_dev->assigned_dev_id);
  565. if (match) {
  566. /* device already assigned */
  567. r = -EEXIST;
  568. goto out;
  569. }
  570. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  571. if (match == NULL) {
  572. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  573. __func__);
  574. r = -ENOMEM;
  575. goto out;
  576. }
  577. dev = pci_get_domain_bus_and_slot(assigned_dev->segnr,
  578. assigned_dev->busnr,
  579. assigned_dev->devfn);
  580. if (!dev) {
  581. printk(KERN_INFO "%s: host device not found\n", __func__);
  582. r = -EINVAL;
  583. goto out_free;
  584. }
  585. /* Don't allow bridges to be assigned */
  586. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
  587. r = -EPERM;
  588. goto out_put;
  589. }
  590. r = probe_sysfs_permissions(dev);
  591. if (r)
  592. goto out_put;
  593. if (pci_enable_device(dev)) {
  594. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  595. r = -EBUSY;
  596. goto out_put;
  597. }
  598. r = pci_request_regions(dev, "kvm_assigned_device");
  599. if (r) {
  600. printk(KERN_INFO "%s: Could not get access to device regions\n",
  601. __func__);
  602. goto out_disable;
  603. }
  604. pci_reset_function(dev);
  605. pci_save_state(dev);
  606. match->pci_saved_state = pci_store_saved_state(dev);
  607. if (!match->pci_saved_state)
  608. printk(KERN_DEBUG "%s: Couldn't store %s saved state\n",
  609. __func__, dev_name(&dev->dev));
  610. if (!pci_intx_mask_supported(dev))
  611. assigned_dev->flags &= ~KVM_DEV_ASSIGN_PCI_2_3;
  612. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  613. match->host_segnr = assigned_dev->segnr;
  614. match->host_busnr = assigned_dev->busnr;
  615. match->host_devfn = assigned_dev->devfn;
  616. match->flags = assigned_dev->flags;
  617. match->dev = dev;
  618. spin_lock_init(&match->intx_lock);
  619. spin_lock_init(&match->intx_mask_lock);
  620. match->irq_source_id = -1;
  621. match->kvm = kvm;
  622. match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
  623. list_add(&match->list, &kvm->arch.assigned_dev_head);
  624. if (!kvm->arch.iommu_domain) {
  625. r = kvm_iommu_map_guest(kvm);
  626. if (r)
  627. goto out_list_del;
  628. }
  629. r = kvm_assign_device(kvm, match);
  630. if (r)
  631. goto out_list_del;
  632. out:
  633. srcu_read_unlock(&kvm->srcu, idx);
  634. mutex_unlock(&kvm->lock);
  635. return r;
  636. out_list_del:
  637. if (pci_load_and_free_saved_state(dev, &match->pci_saved_state))
  638. printk(KERN_INFO "%s: Couldn't reload %s saved state\n",
  639. __func__, dev_name(&dev->dev));
  640. list_del(&match->list);
  641. pci_release_regions(dev);
  642. out_disable:
  643. pci_disable_device(dev);
  644. out_put:
  645. pci_dev_put(dev);
  646. out_free:
  647. kfree(match);
  648. srcu_read_unlock(&kvm->srcu, idx);
  649. mutex_unlock(&kvm->lock);
  650. return r;
  651. }
  652. static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
  653. struct kvm_assigned_pci_dev *assigned_dev)
  654. {
  655. int r = 0;
  656. struct kvm_assigned_dev_kernel *match;
  657. mutex_lock(&kvm->lock);
  658. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  659. assigned_dev->assigned_dev_id);
  660. if (!match) {
  661. printk(KERN_INFO "%s: device hasn't been assigned before, "
  662. "so cannot be deassigned\n", __func__);
  663. r = -EINVAL;
  664. goto out;
  665. }
  666. kvm_deassign_device(kvm, match);
  667. kvm_free_assigned_device(kvm, match);
  668. out:
  669. mutex_unlock(&kvm->lock);
  670. return r;
  671. }
  672. #ifdef __KVM_HAVE_MSIX
  673. static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
  674. struct kvm_assigned_msix_nr *entry_nr)
  675. {
  676. int r = 0;
  677. struct kvm_assigned_dev_kernel *adev;
  678. mutex_lock(&kvm->lock);
  679. adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  680. entry_nr->assigned_dev_id);
  681. if (!adev) {
  682. r = -EINVAL;
  683. goto msix_nr_out;
  684. }
  685. if (adev->entries_nr == 0) {
  686. adev->entries_nr = entry_nr->entry_nr;
  687. if (adev->entries_nr == 0 ||
  688. adev->entries_nr > KVM_MAX_MSIX_PER_DEV) {
  689. r = -EINVAL;
  690. goto msix_nr_out;
  691. }
  692. adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
  693. entry_nr->entry_nr,
  694. GFP_KERNEL);
  695. if (!adev->host_msix_entries) {
  696. r = -ENOMEM;
  697. goto msix_nr_out;
  698. }
  699. adev->guest_msix_entries =
  700. kzalloc(sizeof(struct msix_entry) * entry_nr->entry_nr,
  701. GFP_KERNEL);
  702. if (!adev->guest_msix_entries) {
  703. kfree(adev->host_msix_entries);
  704. r = -ENOMEM;
  705. goto msix_nr_out;
  706. }
  707. } else /* Not allowed set MSI-X number twice */
  708. r = -EINVAL;
  709. msix_nr_out:
  710. mutex_unlock(&kvm->lock);
  711. return r;
  712. }
  713. static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
  714. struct kvm_assigned_msix_entry *entry)
  715. {
  716. int r = 0, i;
  717. struct kvm_assigned_dev_kernel *adev;
  718. mutex_lock(&kvm->lock);
  719. adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  720. entry->assigned_dev_id);
  721. if (!adev) {
  722. r = -EINVAL;
  723. goto msix_entry_out;
  724. }
  725. for (i = 0; i < adev->entries_nr; i++)
  726. if (adev->guest_msix_entries[i].vector == 0 ||
  727. adev->guest_msix_entries[i].entry == entry->entry) {
  728. adev->guest_msix_entries[i].entry = entry->entry;
  729. adev->guest_msix_entries[i].vector = entry->gsi;
  730. adev->host_msix_entries[i].entry = entry->entry;
  731. break;
  732. }
  733. if (i == adev->entries_nr) {
  734. r = -ENOSPC;
  735. goto msix_entry_out;
  736. }
  737. msix_entry_out:
  738. mutex_unlock(&kvm->lock);
  739. return r;
  740. }
  741. #endif
  742. static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm *kvm,
  743. struct kvm_assigned_pci_dev *assigned_dev)
  744. {
  745. int r = 0;
  746. struct kvm_assigned_dev_kernel *match;
  747. mutex_lock(&kvm->lock);
  748. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  749. assigned_dev->assigned_dev_id);
  750. if (!match) {
  751. r = -ENODEV;
  752. goto out;
  753. }
  754. spin_lock(&match->intx_mask_lock);
  755. match->flags &= ~KVM_DEV_ASSIGN_MASK_INTX;
  756. match->flags |= assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX;
  757. if (match->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
  758. if (assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX) {
  759. kvm_set_irq(match->kvm, match->irq_source_id,
  760. match->guest_irq, 0, false);
  761. /*
  762. * Masking at hardware-level is performed on demand,
  763. * i.e. when an IRQ actually arrives at the host.
  764. */
  765. } else if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) {
  766. /*
  767. * Unmask the IRQ line if required. Unmasking at
  768. * device level will be performed by user space.
  769. */
  770. spin_lock_irq(&match->intx_lock);
  771. if (match->host_irq_disabled) {
  772. enable_irq(match->host_irq);
  773. match->host_irq_disabled = false;
  774. }
  775. spin_unlock_irq(&match->intx_lock);
  776. }
  777. }
  778. spin_unlock(&match->intx_mask_lock);
  779. out:
  780. mutex_unlock(&kvm->lock);
  781. return r;
  782. }
  783. long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  784. unsigned long arg)
  785. {
  786. void __user *argp = (void __user *)arg;
  787. int r;
  788. switch (ioctl) {
  789. case KVM_ASSIGN_PCI_DEVICE: {
  790. struct kvm_assigned_pci_dev assigned_dev;
  791. r = -EFAULT;
  792. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  793. goto out;
  794. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  795. if (r)
  796. goto out;
  797. break;
  798. }
  799. case KVM_ASSIGN_IRQ: {
  800. r = -EOPNOTSUPP;
  801. break;
  802. }
  803. case KVM_ASSIGN_DEV_IRQ: {
  804. struct kvm_assigned_irq assigned_irq;
  805. r = -EFAULT;
  806. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  807. goto out;
  808. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  809. if (r)
  810. goto out;
  811. break;
  812. }
  813. case KVM_DEASSIGN_DEV_IRQ: {
  814. struct kvm_assigned_irq assigned_irq;
  815. r = -EFAULT;
  816. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  817. goto out;
  818. r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
  819. if (r)
  820. goto out;
  821. break;
  822. }
  823. case KVM_DEASSIGN_PCI_DEVICE: {
  824. struct kvm_assigned_pci_dev assigned_dev;
  825. r = -EFAULT;
  826. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  827. goto out;
  828. r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
  829. if (r)
  830. goto out;
  831. break;
  832. }
  833. #ifdef __KVM_HAVE_MSIX
  834. case KVM_ASSIGN_SET_MSIX_NR: {
  835. struct kvm_assigned_msix_nr entry_nr;
  836. r = -EFAULT;
  837. if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
  838. goto out;
  839. r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
  840. if (r)
  841. goto out;
  842. break;
  843. }
  844. case KVM_ASSIGN_SET_MSIX_ENTRY: {
  845. struct kvm_assigned_msix_entry entry;
  846. r = -EFAULT;
  847. if (copy_from_user(&entry, argp, sizeof entry))
  848. goto out;
  849. r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
  850. if (r)
  851. goto out;
  852. break;
  853. }
  854. #endif
  855. case KVM_ASSIGN_SET_INTX_MASK: {
  856. struct kvm_assigned_pci_dev assigned_dev;
  857. r = -EFAULT;
  858. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  859. goto out;
  860. r = kvm_vm_ioctl_set_pci_irq_mask(kvm, &assigned_dev);
  861. break;
  862. }
  863. default:
  864. r = -ENOTTY;
  865. break;
  866. }
  867. out:
  868. return r;
  869. }