assigned-dev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. pci_clear_dev_assigned(assigned_dev->dev);
  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. dev->irq_source_id = -1;
  448. }
  449. return r;
  450. }
  451. /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
  452. static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
  453. struct kvm_assigned_irq *assigned_irq)
  454. {
  455. int r = -EINVAL;
  456. struct kvm_assigned_dev_kernel *match;
  457. unsigned long host_irq_type, guest_irq_type;
  458. if (!irqchip_in_kernel(kvm))
  459. return r;
  460. mutex_lock(&kvm->lock);
  461. r = -ENODEV;
  462. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  463. assigned_irq->assigned_dev_id);
  464. if (!match)
  465. goto out;
  466. host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
  467. guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
  468. r = -EINVAL;
  469. /* can only assign one type at a time */
  470. if (hweight_long(host_irq_type) > 1)
  471. goto out;
  472. if (hweight_long(guest_irq_type) > 1)
  473. goto out;
  474. if (host_irq_type == 0 && guest_irq_type == 0)
  475. goto out;
  476. r = 0;
  477. if (host_irq_type)
  478. r = assign_host_irq(kvm, match, host_irq_type);
  479. if (r)
  480. goto out;
  481. if (guest_irq_type)
  482. r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
  483. out:
  484. mutex_unlock(&kvm->lock);
  485. return r;
  486. }
  487. static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
  488. struct kvm_assigned_irq
  489. *assigned_irq)
  490. {
  491. int r = -ENODEV;
  492. struct kvm_assigned_dev_kernel *match;
  493. unsigned long irq_type;
  494. mutex_lock(&kvm->lock);
  495. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  496. assigned_irq->assigned_dev_id);
  497. if (!match)
  498. goto out;
  499. irq_type = assigned_irq->flags & (KVM_DEV_IRQ_HOST_MASK |
  500. KVM_DEV_IRQ_GUEST_MASK);
  501. r = kvm_deassign_irq(kvm, match, irq_type);
  502. out:
  503. mutex_unlock(&kvm->lock);
  504. return r;
  505. }
  506. /*
  507. * We want to test whether the caller has been granted permissions to
  508. * use this device. To be able to configure and control the device,
  509. * the user needs access to PCI configuration space and BAR resources.
  510. * These are accessed through PCI sysfs. PCI config space is often
  511. * passed to the process calling this ioctl via file descriptor, so we
  512. * can't rely on access to that file. We can check for permissions
  513. * on each of the BAR resource files, which is a pretty clear
  514. * indicator that the user has been granted access to the device.
  515. */
  516. static int probe_sysfs_permissions(struct pci_dev *dev)
  517. {
  518. #ifdef CONFIG_SYSFS
  519. int i;
  520. bool bar_found = false;
  521. for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++) {
  522. char *kpath, *syspath;
  523. struct path path;
  524. struct inode *inode;
  525. int r;
  526. if (!pci_resource_len(dev, i))
  527. continue;
  528. kpath = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  529. if (!kpath)
  530. return -ENOMEM;
  531. /* Per sysfs-rules, sysfs is always at /sys */
  532. syspath = kasprintf(GFP_KERNEL, "/sys%s/resource%d", kpath, i);
  533. kfree(kpath);
  534. if (!syspath)
  535. return -ENOMEM;
  536. r = kern_path(syspath, LOOKUP_FOLLOW, &path);
  537. kfree(syspath);
  538. if (r)
  539. return r;
  540. inode = path.dentry->d_inode;
  541. r = inode_permission(inode, MAY_READ | MAY_WRITE | MAY_ACCESS);
  542. path_put(&path);
  543. if (r)
  544. return r;
  545. bar_found = true;
  546. }
  547. /* If no resources, probably something special */
  548. if (!bar_found)
  549. return -EPERM;
  550. return 0;
  551. #else
  552. return -EINVAL; /* No way to control the device without sysfs */
  553. #endif
  554. }
  555. static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
  556. struct kvm_assigned_pci_dev *assigned_dev)
  557. {
  558. int r = 0, idx;
  559. struct kvm_assigned_dev_kernel *match;
  560. struct pci_dev *dev;
  561. if (!(assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU))
  562. return -EINVAL;
  563. mutex_lock(&kvm->lock);
  564. idx = srcu_read_lock(&kvm->srcu);
  565. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  566. assigned_dev->assigned_dev_id);
  567. if (match) {
  568. /* device already assigned */
  569. r = -EEXIST;
  570. goto out;
  571. }
  572. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  573. if (match == NULL) {
  574. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  575. __func__);
  576. r = -ENOMEM;
  577. goto out;
  578. }
  579. dev = pci_get_domain_bus_and_slot(assigned_dev->segnr,
  580. assigned_dev->busnr,
  581. assigned_dev->devfn);
  582. if (!dev) {
  583. printk(KERN_INFO "%s: host device not found\n", __func__);
  584. r = -EINVAL;
  585. goto out_free;
  586. }
  587. /* Don't allow bridges to be assigned */
  588. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
  589. r = -EPERM;
  590. goto out_put;
  591. }
  592. r = probe_sysfs_permissions(dev);
  593. if (r)
  594. goto out_put;
  595. if (pci_enable_device(dev)) {
  596. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  597. r = -EBUSY;
  598. goto out_put;
  599. }
  600. r = pci_request_regions(dev, "kvm_assigned_device");
  601. if (r) {
  602. printk(KERN_INFO "%s: Could not get access to device regions\n",
  603. __func__);
  604. goto out_disable;
  605. }
  606. pci_reset_function(dev);
  607. pci_save_state(dev);
  608. match->pci_saved_state = pci_store_saved_state(dev);
  609. if (!match->pci_saved_state)
  610. printk(KERN_DEBUG "%s: Couldn't store %s saved state\n",
  611. __func__, dev_name(&dev->dev));
  612. if (!pci_intx_mask_supported(dev))
  613. assigned_dev->flags &= ~KVM_DEV_ASSIGN_PCI_2_3;
  614. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  615. match->host_segnr = assigned_dev->segnr;
  616. match->host_busnr = assigned_dev->busnr;
  617. match->host_devfn = assigned_dev->devfn;
  618. match->flags = assigned_dev->flags;
  619. match->dev = dev;
  620. spin_lock_init(&match->intx_lock);
  621. spin_lock_init(&match->intx_mask_lock);
  622. match->irq_source_id = -1;
  623. match->kvm = kvm;
  624. match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
  625. list_add(&match->list, &kvm->arch.assigned_dev_head);
  626. if (!kvm->arch.iommu_domain) {
  627. r = kvm_iommu_map_guest(kvm);
  628. if (r)
  629. goto out_list_del;
  630. }
  631. r = kvm_assign_device(kvm, match);
  632. if (r)
  633. goto out_list_del;
  634. out:
  635. srcu_read_unlock(&kvm->srcu, idx);
  636. mutex_unlock(&kvm->lock);
  637. return r;
  638. out_list_del:
  639. if (pci_load_and_free_saved_state(dev, &match->pci_saved_state))
  640. printk(KERN_INFO "%s: Couldn't reload %s saved state\n",
  641. __func__, dev_name(&dev->dev));
  642. list_del(&match->list);
  643. pci_release_regions(dev);
  644. out_disable:
  645. pci_disable_device(dev);
  646. out_put:
  647. pci_dev_put(dev);
  648. out_free:
  649. kfree(match);
  650. srcu_read_unlock(&kvm->srcu, idx);
  651. mutex_unlock(&kvm->lock);
  652. return r;
  653. }
  654. static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
  655. struct kvm_assigned_pci_dev *assigned_dev)
  656. {
  657. int r = 0;
  658. struct kvm_assigned_dev_kernel *match;
  659. mutex_lock(&kvm->lock);
  660. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  661. assigned_dev->assigned_dev_id);
  662. if (!match) {
  663. printk(KERN_INFO "%s: device hasn't been assigned before, "
  664. "so cannot be deassigned\n", __func__);
  665. r = -EINVAL;
  666. goto out;
  667. }
  668. kvm_deassign_device(kvm, match);
  669. kvm_free_assigned_device(kvm, match);
  670. out:
  671. mutex_unlock(&kvm->lock);
  672. return r;
  673. }
  674. #ifdef __KVM_HAVE_MSIX
  675. static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
  676. struct kvm_assigned_msix_nr *entry_nr)
  677. {
  678. int r = 0;
  679. struct kvm_assigned_dev_kernel *adev;
  680. mutex_lock(&kvm->lock);
  681. adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  682. entry_nr->assigned_dev_id);
  683. if (!adev) {
  684. r = -EINVAL;
  685. goto msix_nr_out;
  686. }
  687. if (adev->entries_nr == 0) {
  688. adev->entries_nr = entry_nr->entry_nr;
  689. if (adev->entries_nr == 0 ||
  690. adev->entries_nr > KVM_MAX_MSIX_PER_DEV) {
  691. r = -EINVAL;
  692. goto msix_nr_out;
  693. }
  694. adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
  695. entry_nr->entry_nr,
  696. GFP_KERNEL);
  697. if (!adev->host_msix_entries) {
  698. r = -ENOMEM;
  699. goto msix_nr_out;
  700. }
  701. adev->guest_msix_entries =
  702. kzalloc(sizeof(struct msix_entry) * entry_nr->entry_nr,
  703. GFP_KERNEL);
  704. if (!adev->guest_msix_entries) {
  705. kfree(adev->host_msix_entries);
  706. r = -ENOMEM;
  707. goto msix_nr_out;
  708. }
  709. } else /* Not allowed set MSI-X number twice */
  710. r = -EINVAL;
  711. msix_nr_out:
  712. mutex_unlock(&kvm->lock);
  713. return r;
  714. }
  715. static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
  716. struct kvm_assigned_msix_entry *entry)
  717. {
  718. int r = 0, i;
  719. struct kvm_assigned_dev_kernel *adev;
  720. mutex_lock(&kvm->lock);
  721. adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  722. entry->assigned_dev_id);
  723. if (!adev) {
  724. r = -EINVAL;
  725. goto msix_entry_out;
  726. }
  727. for (i = 0; i < adev->entries_nr; i++)
  728. if (adev->guest_msix_entries[i].vector == 0 ||
  729. adev->guest_msix_entries[i].entry == entry->entry) {
  730. adev->guest_msix_entries[i].entry = entry->entry;
  731. adev->guest_msix_entries[i].vector = entry->gsi;
  732. adev->host_msix_entries[i].entry = entry->entry;
  733. break;
  734. }
  735. if (i == adev->entries_nr) {
  736. r = -ENOSPC;
  737. goto msix_entry_out;
  738. }
  739. msix_entry_out:
  740. mutex_unlock(&kvm->lock);
  741. return r;
  742. }
  743. #endif
  744. static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm *kvm,
  745. struct kvm_assigned_pci_dev *assigned_dev)
  746. {
  747. int r = 0;
  748. struct kvm_assigned_dev_kernel *match;
  749. mutex_lock(&kvm->lock);
  750. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  751. assigned_dev->assigned_dev_id);
  752. if (!match) {
  753. r = -ENODEV;
  754. goto out;
  755. }
  756. spin_lock(&match->intx_mask_lock);
  757. match->flags &= ~KVM_DEV_ASSIGN_MASK_INTX;
  758. match->flags |= assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX;
  759. if (match->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
  760. if (assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX) {
  761. kvm_set_irq(match->kvm, match->irq_source_id,
  762. match->guest_irq, 0, false);
  763. /*
  764. * Masking at hardware-level is performed on demand,
  765. * i.e. when an IRQ actually arrives at the host.
  766. */
  767. } else if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) {
  768. /*
  769. * Unmask the IRQ line if required. Unmasking at
  770. * device level will be performed by user space.
  771. */
  772. spin_lock_irq(&match->intx_lock);
  773. if (match->host_irq_disabled) {
  774. enable_irq(match->host_irq);
  775. match->host_irq_disabled = false;
  776. }
  777. spin_unlock_irq(&match->intx_lock);
  778. }
  779. }
  780. spin_unlock(&match->intx_mask_lock);
  781. out:
  782. mutex_unlock(&kvm->lock);
  783. return r;
  784. }
  785. long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  786. unsigned long arg)
  787. {
  788. void __user *argp = (void __user *)arg;
  789. int r;
  790. switch (ioctl) {
  791. case KVM_ASSIGN_PCI_DEVICE: {
  792. struct kvm_assigned_pci_dev assigned_dev;
  793. r = -EFAULT;
  794. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  795. goto out;
  796. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  797. if (r)
  798. goto out;
  799. break;
  800. }
  801. case KVM_ASSIGN_IRQ: {
  802. r = -EOPNOTSUPP;
  803. break;
  804. }
  805. case KVM_ASSIGN_DEV_IRQ: {
  806. struct kvm_assigned_irq assigned_irq;
  807. r = -EFAULT;
  808. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  809. goto out;
  810. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  811. if (r)
  812. goto out;
  813. break;
  814. }
  815. case KVM_DEASSIGN_DEV_IRQ: {
  816. struct kvm_assigned_irq assigned_irq;
  817. r = -EFAULT;
  818. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  819. goto out;
  820. r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
  821. if (r)
  822. goto out;
  823. break;
  824. }
  825. case KVM_DEASSIGN_PCI_DEVICE: {
  826. struct kvm_assigned_pci_dev assigned_dev;
  827. r = -EFAULT;
  828. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  829. goto out;
  830. r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
  831. if (r)
  832. goto out;
  833. break;
  834. }
  835. #ifdef __KVM_HAVE_MSIX
  836. case KVM_ASSIGN_SET_MSIX_NR: {
  837. struct kvm_assigned_msix_nr entry_nr;
  838. r = -EFAULT;
  839. if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
  840. goto out;
  841. r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
  842. if (r)
  843. goto out;
  844. break;
  845. }
  846. case KVM_ASSIGN_SET_MSIX_ENTRY: {
  847. struct kvm_assigned_msix_entry entry;
  848. r = -EFAULT;
  849. if (copy_from_user(&entry, argp, sizeof entry))
  850. goto out;
  851. r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
  852. if (r)
  853. goto out;
  854. break;
  855. }
  856. #endif
  857. case KVM_ASSIGN_SET_INTX_MASK: {
  858. struct kvm_assigned_pci_dev assigned_dev;
  859. r = -EFAULT;
  860. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  861. goto out;
  862. r = kvm_vm_ioctl_set_pci_irq_mask(kvm, &assigned_dev);
  863. break;
  864. }
  865. default:
  866. r = -ENOTTY;
  867. break;
  868. }
  869. out:
  870. return r;
  871. }