assigned-dev.c 25 KB

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