assigned-dev.c 25 KB

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