virtio_pci_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Virtio PCI driver - common functionality for all device versions
  3. *
  4. * This module allows virtio devices to be used over a virtual PCI device.
  5. * This can be used with QEMU based VMMs like KVM or Xen.
  6. *
  7. * Copyright IBM Corp. 2007
  8. * Copyright Red Hat, Inc. 2014
  9. *
  10. * Authors:
  11. * Anthony Liguori <aliguori@us.ibm.com>
  12. * Rusty Russell <rusty@rustcorp.com.au>
  13. * Michael S. Tsirkin <mst@redhat.com>
  14. *
  15. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  16. * See the COPYING file in the top-level directory.
  17. *
  18. */
  19. #include "virtio_pci_common.h"
  20. /* wait for pending irq handlers */
  21. void vp_synchronize_vectors(struct virtio_device *vdev)
  22. {
  23. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  24. int i;
  25. if (vp_dev->intx_enabled)
  26. synchronize_irq(vp_dev->pci_dev->irq);
  27. for (i = 0; i < vp_dev->msix_vectors; ++i)
  28. synchronize_irq(vp_dev->msix_entries[i].vector);
  29. }
  30. /* the notify function used when creating a virt queue */
  31. bool vp_notify(struct virtqueue *vq)
  32. {
  33. /* we write the queue's selector into the notification register to
  34. * signal the other end */
  35. iowrite16(vq->index, (void __iomem *)vq->priv);
  36. return true;
  37. }
  38. /* Handle a configuration change: Tell driver if it wants to know. */
  39. static irqreturn_t vp_config_changed(int irq, void *opaque)
  40. {
  41. struct virtio_pci_device *vp_dev = opaque;
  42. virtio_config_changed(&vp_dev->vdev);
  43. return IRQ_HANDLED;
  44. }
  45. /* Notify all virtqueues on an interrupt. */
  46. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  47. {
  48. struct virtio_pci_device *vp_dev = opaque;
  49. struct virtio_pci_vq_info *info;
  50. irqreturn_t ret = IRQ_NONE;
  51. unsigned long flags;
  52. spin_lock_irqsave(&vp_dev->lock, flags);
  53. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  54. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  55. ret = IRQ_HANDLED;
  56. }
  57. spin_unlock_irqrestore(&vp_dev->lock, flags);
  58. return ret;
  59. }
  60. /* A small wrapper to also acknowledge the interrupt when it's handled.
  61. * I really need an EIO hook for the vring so I can ack the interrupt once we
  62. * know that we'll be handling the IRQ but before we invoke the callback since
  63. * the callback may notify the host which results in the host attempting to
  64. * raise an interrupt that we would then mask once we acknowledged the
  65. * interrupt. */
  66. static irqreturn_t vp_interrupt(int irq, void *opaque)
  67. {
  68. struct virtio_pci_device *vp_dev = opaque;
  69. u8 isr;
  70. /* reading the ISR has the effect of also clearing it so it's very
  71. * important to save off the value. */
  72. isr = ioread8(vp_dev->isr);
  73. /* It's definitely not us if the ISR was not high */
  74. if (!isr)
  75. return IRQ_NONE;
  76. /* Configuration change? Tell driver if it wants to know. */
  77. if (isr & VIRTIO_PCI_ISR_CONFIG)
  78. vp_config_changed(irq, opaque);
  79. return vp_vring_interrupt(irq, opaque);
  80. }
  81. static void vp_free_vectors(struct virtio_device *vdev)
  82. {
  83. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  84. int i;
  85. if (vp_dev->intx_enabled) {
  86. free_irq(vp_dev->pci_dev->irq, vp_dev);
  87. vp_dev->intx_enabled = 0;
  88. }
  89. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  90. free_irq(vp_dev->msix_entries[i].vector, vp_dev);
  91. for (i = 0; i < vp_dev->msix_vectors; i++)
  92. if (vp_dev->msix_affinity_masks[i])
  93. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  94. if (vp_dev->msix_enabled) {
  95. /* Disable the vector used for configuration */
  96. vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
  97. pci_disable_msix(vp_dev->pci_dev);
  98. vp_dev->msix_enabled = 0;
  99. }
  100. vp_dev->msix_vectors = 0;
  101. vp_dev->msix_used_vectors = 0;
  102. kfree(vp_dev->msix_names);
  103. vp_dev->msix_names = NULL;
  104. kfree(vp_dev->msix_entries);
  105. vp_dev->msix_entries = NULL;
  106. kfree(vp_dev->msix_affinity_masks);
  107. vp_dev->msix_affinity_masks = NULL;
  108. }
  109. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  110. bool per_vq_vectors)
  111. {
  112. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  113. const char *name = dev_name(&vp_dev->vdev.dev);
  114. unsigned i, v;
  115. int err = -ENOMEM;
  116. vp_dev->msix_vectors = nvectors;
  117. vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
  118. GFP_KERNEL);
  119. if (!vp_dev->msix_entries)
  120. goto error;
  121. vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
  122. GFP_KERNEL);
  123. if (!vp_dev->msix_names)
  124. goto error;
  125. vp_dev->msix_affinity_masks
  126. = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks,
  127. GFP_KERNEL);
  128. if (!vp_dev->msix_affinity_masks)
  129. goto error;
  130. for (i = 0; i < nvectors; ++i)
  131. if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
  132. GFP_KERNEL))
  133. goto error;
  134. for (i = 0; i < nvectors; ++i)
  135. vp_dev->msix_entries[i].entry = i;
  136. err = pci_enable_msix_exact(vp_dev->pci_dev,
  137. vp_dev->msix_entries, nvectors);
  138. if (err)
  139. goto error;
  140. vp_dev->msix_enabled = 1;
  141. /* Set the vector used for configuration */
  142. v = vp_dev->msix_used_vectors;
  143. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  144. "%s-config", name);
  145. err = request_irq(vp_dev->msix_entries[v].vector,
  146. vp_config_changed, 0, vp_dev->msix_names[v],
  147. vp_dev);
  148. if (err)
  149. goto error;
  150. ++vp_dev->msix_used_vectors;
  151. v = vp_dev->config_vector(vp_dev, v);
  152. /* Verify we had enough resources to assign the vector */
  153. if (v == VIRTIO_MSI_NO_VECTOR) {
  154. err = -EBUSY;
  155. goto error;
  156. }
  157. if (!per_vq_vectors) {
  158. /* Shared vector for all VQs */
  159. v = vp_dev->msix_used_vectors;
  160. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  161. "%s-virtqueues", name);
  162. err = request_irq(vp_dev->msix_entries[v].vector,
  163. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  164. vp_dev);
  165. if (err)
  166. goto error;
  167. ++vp_dev->msix_used_vectors;
  168. }
  169. return 0;
  170. error:
  171. vp_free_vectors(vdev);
  172. return err;
  173. }
  174. static int vp_request_intx(struct virtio_device *vdev)
  175. {
  176. int err;
  177. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  178. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  179. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  180. if (!err)
  181. vp_dev->intx_enabled = 1;
  182. return err;
  183. }
  184. static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index,
  185. void (*callback)(struct virtqueue *vq),
  186. const char *name,
  187. u16 msix_vec)
  188. {
  189. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  190. struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
  191. struct virtqueue *vq;
  192. unsigned long flags;
  193. /* fill out our structure that represents an active queue */
  194. if (!info)
  195. return ERR_PTR(-ENOMEM);
  196. vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, msix_vec);
  197. if (IS_ERR(vq))
  198. goto out_info;
  199. info->vq = vq;
  200. if (callback) {
  201. spin_lock_irqsave(&vp_dev->lock, flags);
  202. list_add(&info->node, &vp_dev->virtqueues);
  203. spin_unlock_irqrestore(&vp_dev->lock, flags);
  204. } else {
  205. INIT_LIST_HEAD(&info->node);
  206. }
  207. vp_dev->vqs[index] = info;
  208. return vq;
  209. out_info:
  210. kfree(info);
  211. return vq;
  212. }
  213. static void vp_del_vq(struct virtqueue *vq)
  214. {
  215. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  216. struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
  217. unsigned long flags;
  218. spin_lock_irqsave(&vp_dev->lock, flags);
  219. list_del(&info->node);
  220. spin_unlock_irqrestore(&vp_dev->lock, flags);
  221. vp_dev->del_vq(info);
  222. kfree(info);
  223. }
  224. /* the config->del_vqs() implementation */
  225. void vp_del_vqs(struct virtio_device *vdev)
  226. {
  227. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  228. struct virtqueue *vq, *n;
  229. struct virtio_pci_vq_info *info;
  230. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  231. info = vp_dev->vqs[vq->index];
  232. if (vp_dev->per_vq_vectors &&
  233. info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  234. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  235. vq);
  236. vp_del_vq(vq);
  237. }
  238. vp_dev->per_vq_vectors = false;
  239. vp_free_vectors(vdev);
  240. kfree(vp_dev->vqs);
  241. }
  242. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  243. struct virtqueue *vqs[],
  244. vq_callback_t *callbacks[],
  245. const char *names[],
  246. bool use_msix,
  247. bool per_vq_vectors)
  248. {
  249. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  250. u16 msix_vec;
  251. int i, err, nvectors, allocated_vectors;
  252. vp_dev->vqs = kmalloc(nvqs * sizeof *vp_dev->vqs, GFP_KERNEL);
  253. if (!vp_dev->vqs)
  254. return -ENOMEM;
  255. if (!use_msix) {
  256. /* Old style: one normal interrupt for change and all vqs. */
  257. err = vp_request_intx(vdev);
  258. if (err)
  259. goto error_find;
  260. } else {
  261. if (per_vq_vectors) {
  262. /* Best option: one for change interrupt, one per vq. */
  263. nvectors = 1;
  264. for (i = 0; i < nvqs; ++i)
  265. if (callbacks[i])
  266. ++nvectors;
  267. } else {
  268. /* Second best: one for change, shared for all vqs. */
  269. nvectors = 2;
  270. }
  271. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
  272. if (err)
  273. goto error_find;
  274. }
  275. vp_dev->per_vq_vectors = per_vq_vectors;
  276. allocated_vectors = vp_dev->msix_used_vectors;
  277. for (i = 0; i < nvqs; ++i) {
  278. if (!names[i]) {
  279. vqs[i] = NULL;
  280. continue;
  281. } else if (!callbacks[i] || !vp_dev->msix_enabled)
  282. msix_vec = VIRTIO_MSI_NO_VECTOR;
  283. else if (vp_dev->per_vq_vectors)
  284. msix_vec = allocated_vectors++;
  285. else
  286. msix_vec = VP_MSIX_VQ_VECTOR;
  287. vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
  288. if (IS_ERR(vqs[i])) {
  289. err = PTR_ERR(vqs[i]);
  290. goto error_find;
  291. }
  292. if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
  293. continue;
  294. /* allocate per-vq irq if available and necessary */
  295. snprintf(vp_dev->msix_names[msix_vec],
  296. sizeof *vp_dev->msix_names,
  297. "%s-%s",
  298. dev_name(&vp_dev->vdev.dev), names[i]);
  299. err = request_irq(vp_dev->msix_entries[msix_vec].vector,
  300. vring_interrupt, 0,
  301. vp_dev->msix_names[msix_vec],
  302. vqs[i]);
  303. if (err) {
  304. vp_del_vq(vqs[i]);
  305. goto error_find;
  306. }
  307. }
  308. return 0;
  309. error_find:
  310. vp_del_vqs(vdev);
  311. return err;
  312. }
  313. /* the config->find_vqs() implementation */
  314. int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  315. struct virtqueue *vqs[],
  316. vq_callback_t *callbacks[],
  317. const char *names[])
  318. {
  319. int err;
  320. /* Try MSI-X with one vector per queue. */
  321. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
  322. if (!err)
  323. return 0;
  324. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  325. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  326. true, false);
  327. if (!err)
  328. return 0;
  329. /* Finally fall back to regular interrupts. */
  330. return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  331. false, false);
  332. }
  333. const char *vp_bus_name(struct virtio_device *vdev)
  334. {
  335. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  336. return pci_name(vp_dev->pci_dev);
  337. }
  338. /* Setup the affinity for a virtqueue:
  339. * - force the affinity for per vq vector
  340. * - OR over all affinities for shared MSI
  341. * - ignore the affinity request if we're using INTX
  342. */
  343. int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
  344. {
  345. struct virtio_device *vdev = vq->vdev;
  346. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  347. struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
  348. struct cpumask *mask;
  349. unsigned int irq;
  350. if (!vq->callback)
  351. return -EINVAL;
  352. if (vp_dev->msix_enabled) {
  353. mask = vp_dev->msix_affinity_masks[info->msix_vector];
  354. irq = vp_dev->msix_entries[info->msix_vector].vector;
  355. if (cpu == -1)
  356. irq_set_affinity_hint(irq, NULL);
  357. else {
  358. cpumask_set_cpu(cpu, mask);
  359. irq_set_affinity_hint(irq, mask);
  360. }
  361. }
  362. return 0;
  363. }
  364. void virtio_pci_release_dev(struct device *_d)
  365. {
  366. /*
  367. * No need for a release method as we allocate/free
  368. * all devices together with the pci devices.
  369. * Provide an empty one to avoid getting a warning from core.
  370. */
  371. }
  372. #ifdef CONFIG_PM_SLEEP
  373. static int virtio_pci_freeze(struct device *dev)
  374. {
  375. struct pci_dev *pci_dev = to_pci_dev(dev);
  376. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  377. int ret;
  378. ret = virtio_device_freeze(&vp_dev->vdev);
  379. if (!ret)
  380. pci_disable_device(pci_dev);
  381. return ret;
  382. }
  383. static int virtio_pci_restore(struct device *dev)
  384. {
  385. struct pci_dev *pci_dev = to_pci_dev(dev);
  386. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  387. int ret;
  388. ret = pci_enable_device(pci_dev);
  389. if (ret)
  390. return ret;
  391. pci_set_master(pci_dev);
  392. return virtio_device_restore(&vp_dev->vdev);
  393. }
  394. const struct dev_pm_ops virtio_pci_pm_ops = {
  395. SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
  396. };
  397. #endif