virtio_pci_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. static bool force_legacy = false;
  21. #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
  22. module_param(force_legacy, bool, 0444);
  23. MODULE_PARM_DESC(force_legacy,
  24. "Force legacy mode for transitional virtio 1 devices");
  25. #endif
  26. /* wait for pending irq handlers */
  27. void vp_synchronize_vectors(struct virtio_device *vdev)
  28. {
  29. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  30. int i;
  31. synchronize_irq(pci_irq_vector(vp_dev->pci_dev, 0));
  32. for (i = 1; i < vp_dev->msix_vectors; i++)
  33. synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
  34. }
  35. /* the notify function used when creating a virt queue */
  36. bool vp_notify(struct virtqueue *vq)
  37. {
  38. /* we write the queue's selector into the notification register to
  39. * signal the other end */
  40. iowrite16(vq->index, (void __iomem *)vq->priv);
  41. return true;
  42. }
  43. /* Handle a configuration change: Tell driver if it wants to know. */
  44. static irqreturn_t vp_config_changed(int irq, void *opaque)
  45. {
  46. struct virtio_pci_device *vp_dev = opaque;
  47. virtio_config_changed(&vp_dev->vdev);
  48. return IRQ_HANDLED;
  49. }
  50. /* Notify all virtqueues on an interrupt. */
  51. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  52. {
  53. struct virtio_pci_device *vp_dev = opaque;
  54. irqreturn_t ret = IRQ_NONE;
  55. struct virtqueue *vq;
  56. list_for_each_entry(vq, &vp_dev->vdev.vqs, list) {
  57. if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED)
  58. ret = IRQ_HANDLED;
  59. }
  60. return ret;
  61. }
  62. /* A small wrapper to also acknowledge the interrupt when it's handled.
  63. * I really need an EIO hook for the vring so I can ack the interrupt once we
  64. * know that we'll be handling the IRQ but before we invoke the callback since
  65. * the callback may notify the host which results in the host attempting to
  66. * raise an interrupt that we would then mask once we acknowledged the
  67. * interrupt. */
  68. static irqreturn_t vp_interrupt(int irq, void *opaque)
  69. {
  70. struct virtio_pci_device *vp_dev = opaque;
  71. u8 isr;
  72. /* reading the ISR has the effect of also clearing it so it's very
  73. * important to save off the value. */
  74. isr = ioread8(vp_dev->isr);
  75. /* It's definitely not us if the ISR was not high */
  76. if (!isr)
  77. return IRQ_NONE;
  78. /* Configuration change? Tell driver if it wants to know. */
  79. if (isr & VIRTIO_PCI_ISR_CONFIG)
  80. vp_config_changed(irq, opaque);
  81. return vp_vring_interrupt(irq, opaque);
  82. }
  83. static void vp_remove_vqs(struct virtio_device *vdev)
  84. {
  85. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  86. struct virtqueue *vq, *n;
  87. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  88. if (vp_dev->msix_vector_map) {
  89. int v = vp_dev->msix_vector_map[vq->index];
  90. if (v != VIRTIO_MSI_NO_VECTOR)
  91. free_irq(pci_irq_vector(vp_dev->pci_dev, v),
  92. vq);
  93. }
  94. vp_dev->del_vq(vq);
  95. }
  96. }
  97. /* the config->del_vqs() implementation */
  98. void vp_del_vqs(struct virtio_device *vdev)
  99. {
  100. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  101. int i;
  102. if (WARN_ON_ONCE(list_empty_careful(&vdev->vqs)))
  103. return;
  104. vp_remove_vqs(vdev);
  105. if (vp_dev->pci_dev->msix_enabled) {
  106. for (i = 0; i < vp_dev->msix_vectors; i++)
  107. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  108. /* Disable the vector used for configuration */
  109. vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
  110. kfree(vp_dev->msix_affinity_masks);
  111. kfree(vp_dev->msix_names);
  112. kfree(vp_dev->msix_vector_map);
  113. }
  114. free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
  115. pci_free_irq_vectors(vp_dev->pci_dev);
  116. }
  117. static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
  118. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  119. const char * const names[], struct irq_affinity *desc)
  120. {
  121. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  122. const char *name = dev_name(&vp_dev->vdev.dev);
  123. int i, err = -ENOMEM, allocated_vectors, nvectors;
  124. unsigned flags = PCI_IRQ_MSIX;
  125. bool shared = false;
  126. u16 msix_vec;
  127. if (desc) {
  128. flags |= PCI_IRQ_AFFINITY;
  129. desc->pre_vectors++; /* virtio config vector */
  130. }
  131. nvectors = 1;
  132. for (i = 0; i < nvqs; i++)
  133. if (callbacks[i])
  134. nvectors++;
  135. /* Try one vector per queue first. */
  136. err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
  137. nvectors, flags, desc);
  138. if (err < 0) {
  139. /* Fallback to one vector for config, one shared for queues. */
  140. shared = true;
  141. err = pci_alloc_irq_vectors(vp_dev->pci_dev, 2, 2,
  142. PCI_IRQ_MSIX);
  143. if (err < 0)
  144. return err;
  145. }
  146. if (err < 0)
  147. return err;
  148. vp_dev->msix_vectors = nvectors;
  149. vp_dev->msix_names = kmalloc_array(nvectors,
  150. sizeof(*vp_dev->msix_names), GFP_KERNEL);
  151. if (!vp_dev->msix_names)
  152. goto out_free_irq_vectors;
  153. vp_dev->msix_affinity_masks = kcalloc(nvectors,
  154. sizeof(*vp_dev->msix_affinity_masks), GFP_KERNEL);
  155. if (!vp_dev->msix_affinity_masks)
  156. goto out_free_msix_names;
  157. for (i = 0; i < nvectors; ++i) {
  158. if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
  159. GFP_KERNEL))
  160. goto out_free_msix_affinity_masks;
  161. }
  162. /* Set the vector used for configuration */
  163. snprintf(vp_dev->msix_names[0], sizeof(*vp_dev->msix_names),
  164. "%s-config", name);
  165. err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed,
  166. 0, vp_dev->msix_names[0], vp_dev);
  167. if (err)
  168. goto out_free_msix_affinity_masks;
  169. /* Verify we had enough resources to assign the vector */
  170. if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) {
  171. err = -EBUSY;
  172. goto out_free_config_irq;
  173. }
  174. vp_dev->msix_vector_map = kmalloc_array(nvqs,
  175. sizeof(*vp_dev->msix_vector_map), GFP_KERNEL);
  176. if (!vp_dev->msix_vector_map)
  177. goto out_disable_config_irq;
  178. allocated_vectors = 1; /* vector 0 is the config interrupt */
  179. for (i = 0; i < nvqs; ++i) {
  180. if (!names[i]) {
  181. vqs[i] = NULL;
  182. continue;
  183. }
  184. if (callbacks[i])
  185. msix_vec = allocated_vectors;
  186. else
  187. msix_vec = VIRTIO_MSI_NO_VECTOR;
  188. vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
  189. msix_vec);
  190. if (IS_ERR(vqs[i])) {
  191. err = PTR_ERR(vqs[i]);
  192. goto out_remove_vqs;
  193. }
  194. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  195. vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
  196. continue;
  197. }
  198. snprintf(vp_dev->msix_names[i + 1],
  199. sizeof(*vp_dev->msix_names), "%s-%s",
  200. dev_name(&vp_dev->vdev.dev), names[i]);
  201. err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
  202. vring_interrupt, IRQF_SHARED,
  203. vp_dev->msix_names[i + 1], vqs[i]);
  204. if (err) {
  205. /* don't free this irq on error */
  206. vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
  207. goto out_remove_vqs;
  208. }
  209. vp_dev->msix_vector_map[i] = msix_vec;
  210. /*
  211. * Use a different vector for each queue if they are available,
  212. * else share the same vector for all VQs.
  213. */
  214. if (!shared)
  215. allocated_vectors++;
  216. }
  217. return 0;
  218. out_remove_vqs:
  219. vp_remove_vqs(vdev);
  220. kfree(vp_dev->msix_vector_map);
  221. out_disable_config_irq:
  222. vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
  223. out_free_config_irq:
  224. free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
  225. out_free_msix_affinity_masks:
  226. for (i = 0; i < nvectors; i++) {
  227. if (vp_dev->msix_affinity_masks[i])
  228. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  229. }
  230. kfree(vp_dev->msix_affinity_masks);
  231. out_free_msix_names:
  232. kfree(vp_dev->msix_names);
  233. out_free_irq_vectors:
  234. pci_free_irq_vectors(vp_dev->pci_dev);
  235. return err;
  236. }
  237. static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
  238. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  239. const char * const names[])
  240. {
  241. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  242. int i, err;
  243. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
  244. dev_name(&vdev->dev), vp_dev);
  245. if (err)
  246. return err;
  247. for (i = 0; i < nvqs; ++i) {
  248. if (!names[i]) {
  249. vqs[i] = NULL;
  250. continue;
  251. }
  252. vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
  253. VIRTIO_MSI_NO_VECTOR);
  254. if (IS_ERR(vqs[i])) {
  255. err = PTR_ERR(vqs[i]);
  256. goto out_remove_vqs;
  257. }
  258. }
  259. return 0;
  260. out_remove_vqs:
  261. vp_remove_vqs(vdev);
  262. free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
  263. return err;
  264. }
  265. /* the config->find_vqs() implementation */
  266. int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  267. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  268. const char * const names[], struct irq_affinity *desc)
  269. {
  270. int err;
  271. err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, desc);
  272. if (!err)
  273. return 0;
  274. return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
  275. }
  276. const char *vp_bus_name(struct virtio_device *vdev)
  277. {
  278. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  279. return pci_name(vp_dev->pci_dev);
  280. }
  281. /* Setup the affinity for a virtqueue:
  282. * - force the affinity for per vq vector
  283. * - OR over all affinities for shared MSI
  284. * - ignore the affinity request if we're using INTX
  285. */
  286. int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
  287. {
  288. struct virtio_device *vdev = vq->vdev;
  289. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  290. if (!vq->callback)
  291. return -EINVAL;
  292. if (vp_dev->pci_dev->msix_enabled) {
  293. int vec = vp_dev->msix_vector_map[vq->index];
  294. struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
  295. unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
  296. if (cpu == -1)
  297. irq_set_affinity_hint(irq, NULL);
  298. else {
  299. cpumask_clear(mask);
  300. cpumask_set_cpu(cpu, mask);
  301. irq_set_affinity_hint(irq, mask);
  302. }
  303. }
  304. return 0;
  305. }
  306. const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
  307. {
  308. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  309. unsigned int *map = vp_dev->msix_vector_map;
  310. if (!map || map[index] == VIRTIO_MSI_NO_VECTOR)
  311. return NULL;
  312. return pci_irq_get_affinity(vp_dev->pci_dev, map[index]);
  313. }
  314. #ifdef CONFIG_PM_SLEEP
  315. static int virtio_pci_freeze(struct device *dev)
  316. {
  317. struct pci_dev *pci_dev = to_pci_dev(dev);
  318. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  319. int ret;
  320. ret = virtio_device_freeze(&vp_dev->vdev);
  321. if (!ret)
  322. pci_disable_device(pci_dev);
  323. return ret;
  324. }
  325. static int virtio_pci_restore(struct device *dev)
  326. {
  327. struct pci_dev *pci_dev = to_pci_dev(dev);
  328. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  329. int ret;
  330. ret = pci_enable_device(pci_dev);
  331. if (ret)
  332. return ret;
  333. pci_set_master(pci_dev);
  334. return virtio_device_restore(&vp_dev->vdev);
  335. }
  336. static const struct dev_pm_ops virtio_pci_pm_ops = {
  337. SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
  338. };
  339. #endif
  340. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  341. static const struct pci_device_id virtio_pci_id_table[] = {
  342. { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
  343. { 0 }
  344. };
  345. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  346. static void virtio_pci_release_dev(struct device *_d)
  347. {
  348. struct virtio_device *vdev = dev_to_virtio(_d);
  349. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  350. /* As struct device is a kobject, it's not safe to
  351. * free the memory (including the reference counter itself)
  352. * until it's release callback. */
  353. kfree(vp_dev);
  354. }
  355. static int virtio_pci_probe(struct pci_dev *pci_dev,
  356. const struct pci_device_id *id)
  357. {
  358. struct virtio_pci_device *vp_dev;
  359. int rc;
  360. /* allocate our structure and fill it out */
  361. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  362. if (!vp_dev)
  363. return -ENOMEM;
  364. pci_set_drvdata(pci_dev, vp_dev);
  365. vp_dev->vdev.dev.parent = &pci_dev->dev;
  366. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  367. vp_dev->pci_dev = pci_dev;
  368. /* enable the device */
  369. rc = pci_enable_device(pci_dev);
  370. if (rc)
  371. goto err_enable_device;
  372. if (force_legacy) {
  373. rc = virtio_pci_legacy_probe(vp_dev);
  374. /* Also try modern mode if we can't map BAR0 (no IO space). */
  375. if (rc == -ENODEV || rc == -ENOMEM)
  376. rc = virtio_pci_modern_probe(vp_dev);
  377. if (rc)
  378. goto err_probe;
  379. } else {
  380. rc = virtio_pci_modern_probe(vp_dev);
  381. if (rc == -ENODEV)
  382. rc = virtio_pci_legacy_probe(vp_dev);
  383. if (rc)
  384. goto err_probe;
  385. }
  386. pci_set_master(pci_dev);
  387. rc = register_virtio_device(&vp_dev->vdev);
  388. if (rc)
  389. goto err_register;
  390. return 0;
  391. err_register:
  392. if (vp_dev->ioaddr)
  393. virtio_pci_legacy_remove(vp_dev);
  394. else
  395. virtio_pci_modern_remove(vp_dev);
  396. err_probe:
  397. pci_disable_device(pci_dev);
  398. err_enable_device:
  399. kfree(vp_dev);
  400. return rc;
  401. }
  402. static void virtio_pci_remove(struct pci_dev *pci_dev)
  403. {
  404. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  405. struct device *dev = get_device(&vp_dev->vdev.dev);
  406. unregister_virtio_device(&vp_dev->vdev);
  407. if (vp_dev->ioaddr)
  408. virtio_pci_legacy_remove(vp_dev);
  409. else
  410. virtio_pci_modern_remove(vp_dev);
  411. pci_disable_device(pci_dev);
  412. put_device(dev);
  413. }
  414. static struct pci_driver virtio_pci_driver = {
  415. .name = "virtio-pci",
  416. .id_table = virtio_pci_id_table,
  417. .probe = virtio_pci_probe,
  418. .remove = virtio_pci_remove,
  419. #ifdef CONFIG_PM_SLEEP
  420. .driver.pm = &virtio_pci_pm_ops,
  421. #endif
  422. };
  423. module_pci_driver(virtio_pci_driver);
  424. MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
  425. MODULE_DESCRIPTION("virtio-pci");
  426. MODULE_LICENSE("GPL");
  427. MODULE_VERSION("1");