assigned-dev.c 26 KB

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