assigned-dev.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. if (!(assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU))
  535. return -EINVAL;
  536. mutex_lock(&kvm->lock);
  537. idx = srcu_read_lock(&kvm->srcu);
  538. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  539. assigned_dev->assigned_dev_id);
  540. if (match) {
  541. /* device already assigned */
  542. r = -EEXIST;
  543. goto out;
  544. }
  545. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  546. if (match == NULL) {
  547. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  548. __func__);
  549. r = -ENOMEM;
  550. goto out;
  551. }
  552. dev = pci_get_domain_bus_and_slot(assigned_dev->segnr,
  553. assigned_dev->busnr,
  554. assigned_dev->devfn);
  555. if (!dev) {
  556. printk(KERN_INFO "%s: host device not found\n", __func__);
  557. r = -EINVAL;
  558. goto out_free;
  559. }
  560. /* Don't allow bridges to be assigned */
  561. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
  562. r = -EPERM;
  563. goto out_put;
  564. }
  565. r = probe_sysfs_permissions(dev);
  566. if (r)
  567. goto out_put;
  568. if (pci_enable_device(dev)) {
  569. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  570. r = -EBUSY;
  571. goto out_put;
  572. }
  573. r = pci_request_regions(dev, "kvm_assigned_device");
  574. if (r) {
  575. printk(KERN_INFO "%s: Could not get access to device regions\n",
  576. __func__);
  577. goto out_disable;
  578. }
  579. pci_reset_function(dev);
  580. pci_save_state(dev);
  581. match->pci_saved_state = pci_store_saved_state(dev);
  582. if (!match->pci_saved_state)
  583. printk(KERN_DEBUG "%s: Couldn't store %s saved state\n",
  584. __func__, dev_name(&dev->dev));
  585. if (!pci_intx_mask_supported(dev))
  586. assigned_dev->flags &= ~KVM_DEV_ASSIGN_PCI_2_3;
  587. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  588. match->host_segnr = assigned_dev->segnr;
  589. match->host_busnr = assigned_dev->busnr;
  590. match->host_devfn = assigned_dev->devfn;
  591. match->flags = assigned_dev->flags;
  592. match->dev = dev;
  593. spin_lock_init(&match->intx_lock);
  594. spin_lock_init(&match->intx_mask_lock);
  595. match->irq_source_id = -1;
  596. match->kvm = kvm;
  597. match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
  598. list_add(&match->list, &kvm->arch.assigned_dev_head);
  599. if (!kvm->arch.iommu_domain) {
  600. r = kvm_iommu_map_guest(kvm);
  601. if (r)
  602. goto out_list_del;
  603. }
  604. r = kvm_assign_device(kvm, match);
  605. if (r)
  606. goto out_list_del;
  607. out:
  608. srcu_read_unlock(&kvm->srcu, idx);
  609. mutex_unlock(&kvm->lock);
  610. return r;
  611. out_list_del:
  612. if (pci_load_and_free_saved_state(dev, &match->pci_saved_state))
  613. printk(KERN_INFO "%s: Couldn't reload %s saved state\n",
  614. __func__, dev_name(&dev->dev));
  615. list_del(&match->list);
  616. pci_release_regions(dev);
  617. out_disable:
  618. pci_disable_device(dev);
  619. out_put:
  620. pci_dev_put(dev);
  621. out_free:
  622. kfree(match);
  623. srcu_read_unlock(&kvm->srcu, idx);
  624. mutex_unlock(&kvm->lock);
  625. return r;
  626. }
  627. static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
  628. struct kvm_assigned_pci_dev *assigned_dev)
  629. {
  630. int r = 0;
  631. struct kvm_assigned_dev_kernel *match;
  632. mutex_lock(&kvm->lock);
  633. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  634. assigned_dev->assigned_dev_id);
  635. if (!match) {
  636. printk(KERN_INFO "%s: device hasn't been assigned before, "
  637. "so cannot be deassigned\n", __func__);
  638. r = -EINVAL;
  639. goto out;
  640. }
  641. kvm_deassign_device(kvm, match);
  642. kvm_free_assigned_device(kvm, match);
  643. out:
  644. mutex_unlock(&kvm->lock);
  645. return r;
  646. }
  647. #ifdef __KVM_HAVE_MSIX
  648. static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
  649. struct kvm_assigned_msix_nr *entry_nr)
  650. {
  651. int r = 0;
  652. struct kvm_assigned_dev_kernel *adev;
  653. mutex_lock(&kvm->lock);
  654. adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  655. entry_nr->assigned_dev_id);
  656. if (!adev) {
  657. r = -EINVAL;
  658. goto msix_nr_out;
  659. }
  660. if (adev->entries_nr == 0) {
  661. adev->entries_nr = entry_nr->entry_nr;
  662. if (adev->entries_nr == 0 ||
  663. adev->entries_nr > KVM_MAX_MSIX_PER_DEV) {
  664. r = -EINVAL;
  665. goto msix_nr_out;
  666. }
  667. adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
  668. entry_nr->entry_nr,
  669. GFP_KERNEL);
  670. if (!adev->host_msix_entries) {
  671. r = -ENOMEM;
  672. goto msix_nr_out;
  673. }
  674. adev->guest_msix_entries =
  675. kzalloc(sizeof(struct msix_entry) * entry_nr->entry_nr,
  676. GFP_KERNEL);
  677. if (!adev->guest_msix_entries) {
  678. kfree(adev->host_msix_entries);
  679. r = -ENOMEM;
  680. goto msix_nr_out;
  681. }
  682. } else /* Not allowed set MSI-X number twice */
  683. r = -EINVAL;
  684. msix_nr_out:
  685. mutex_unlock(&kvm->lock);
  686. return r;
  687. }
  688. static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
  689. struct kvm_assigned_msix_entry *entry)
  690. {
  691. int r = 0, i;
  692. struct kvm_assigned_dev_kernel *adev;
  693. mutex_lock(&kvm->lock);
  694. adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  695. entry->assigned_dev_id);
  696. if (!adev) {
  697. r = -EINVAL;
  698. goto msix_entry_out;
  699. }
  700. for (i = 0; i < adev->entries_nr; i++)
  701. if (adev->guest_msix_entries[i].vector == 0 ||
  702. adev->guest_msix_entries[i].entry == entry->entry) {
  703. adev->guest_msix_entries[i].entry = entry->entry;
  704. adev->guest_msix_entries[i].vector = entry->gsi;
  705. adev->host_msix_entries[i].entry = entry->entry;
  706. break;
  707. }
  708. if (i == adev->entries_nr) {
  709. r = -ENOSPC;
  710. goto msix_entry_out;
  711. }
  712. msix_entry_out:
  713. mutex_unlock(&kvm->lock);
  714. return r;
  715. }
  716. #endif
  717. static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm *kvm,
  718. struct kvm_assigned_pci_dev *assigned_dev)
  719. {
  720. int r = 0;
  721. struct kvm_assigned_dev_kernel *match;
  722. mutex_lock(&kvm->lock);
  723. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  724. assigned_dev->assigned_dev_id);
  725. if (!match) {
  726. r = -ENODEV;
  727. goto out;
  728. }
  729. spin_lock(&match->intx_mask_lock);
  730. match->flags &= ~KVM_DEV_ASSIGN_MASK_INTX;
  731. match->flags |= assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX;
  732. if (match->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
  733. if (assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX) {
  734. kvm_set_irq(match->kvm, match->irq_source_id,
  735. match->guest_irq, 0);
  736. /*
  737. * Masking at hardware-level is performed on demand,
  738. * i.e. when an IRQ actually arrives at the host.
  739. */
  740. } else if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) {
  741. /*
  742. * Unmask the IRQ line if required. Unmasking at
  743. * device level will be performed by user space.
  744. */
  745. spin_lock_irq(&match->intx_lock);
  746. if (match->host_irq_disabled) {
  747. enable_irq(match->host_irq);
  748. match->host_irq_disabled = false;
  749. }
  750. spin_unlock_irq(&match->intx_lock);
  751. }
  752. }
  753. spin_unlock(&match->intx_mask_lock);
  754. out:
  755. mutex_unlock(&kvm->lock);
  756. return r;
  757. }
  758. long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  759. unsigned long arg)
  760. {
  761. void __user *argp = (void __user *)arg;
  762. int r;
  763. switch (ioctl) {
  764. case KVM_ASSIGN_PCI_DEVICE: {
  765. struct kvm_assigned_pci_dev assigned_dev;
  766. r = -EFAULT;
  767. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  768. goto out;
  769. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  770. if (r)
  771. goto out;
  772. break;
  773. }
  774. case KVM_ASSIGN_IRQ: {
  775. r = -EOPNOTSUPP;
  776. break;
  777. }
  778. case KVM_ASSIGN_DEV_IRQ: {
  779. struct kvm_assigned_irq assigned_irq;
  780. r = -EFAULT;
  781. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  782. goto out;
  783. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  784. if (r)
  785. goto out;
  786. break;
  787. }
  788. case KVM_DEASSIGN_DEV_IRQ: {
  789. struct kvm_assigned_irq assigned_irq;
  790. r = -EFAULT;
  791. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  792. goto out;
  793. r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
  794. if (r)
  795. goto out;
  796. break;
  797. }
  798. case KVM_DEASSIGN_PCI_DEVICE: {
  799. struct kvm_assigned_pci_dev assigned_dev;
  800. r = -EFAULT;
  801. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  802. goto out;
  803. r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
  804. if (r)
  805. goto out;
  806. break;
  807. }
  808. #ifdef KVM_CAP_IRQ_ROUTING
  809. case KVM_SET_GSI_ROUTING: {
  810. struct kvm_irq_routing routing;
  811. struct kvm_irq_routing __user *urouting;
  812. struct kvm_irq_routing_entry *entries;
  813. r = -EFAULT;
  814. if (copy_from_user(&routing, argp, sizeof(routing)))
  815. goto out;
  816. r = -EINVAL;
  817. if (routing.nr >= KVM_MAX_IRQ_ROUTES)
  818. goto out;
  819. if (routing.flags)
  820. goto out;
  821. r = -ENOMEM;
  822. entries = vmalloc(routing.nr * sizeof(*entries));
  823. if (!entries)
  824. goto out;
  825. r = -EFAULT;
  826. urouting = argp;
  827. if (copy_from_user(entries, urouting->entries,
  828. routing.nr * sizeof(*entries)))
  829. goto out_free_irq_routing;
  830. r = kvm_set_irq_routing(kvm, entries, routing.nr,
  831. routing.flags);
  832. out_free_irq_routing:
  833. vfree(entries);
  834. break;
  835. }
  836. #endif /* KVM_CAP_IRQ_ROUTING */
  837. #ifdef __KVM_HAVE_MSIX
  838. case KVM_ASSIGN_SET_MSIX_NR: {
  839. struct kvm_assigned_msix_nr entry_nr;
  840. r = -EFAULT;
  841. if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
  842. goto out;
  843. r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
  844. if (r)
  845. goto out;
  846. break;
  847. }
  848. case KVM_ASSIGN_SET_MSIX_ENTRY: {
  849. struct kvm_assigned_msix_entry entry;
  850. r = -EFAULT;
  851. if (copy_from_user(&entry, argp, sizeof entry))
  852. goto out;
  853. r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
  854. if (r)
  855. goto out;
  856. break;
  857. }
  858. #endif
  859. case KVM_ASSIGN_SET_INTX_MASK: {
  860. struct kvm_assigned_pci_dev assigned_dev;
  861. r = -EFAULT;
  862. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  863. goto out;
  864. r = kvm_vm_ioctl_set_pci_irq_mask(kvm, &assigned_dev);
  865. break;
  866. }
  867. default:
  868. r = -ENOTTY;
  869. break;
  870. }
  871. out:
  872. return r;
  873. }