assigned-dev.c 25 KB

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