assigned-dev.c 25 KB

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