virtio_pci_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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, j, 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 = j = 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[j],
  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[j], 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. j++;
  211. /*
  212. * Use a different vector for each queue if they are available,
  213. * else share the same vector for all VQs.
  214. */
  215. if (!shared)
  216. allocated_vectors++;
  217. }
  218. return 0;
  219. out_remove_vqs:
  220. vp_remove_vqs(vdev);
  221. kfree(vp_dev->msix_vector_map);
  222. out_disable_config_irq:
  223. vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
  224. out_free_config_irq:
  225. free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
  226. out_free_msix_affinity_masks:
  227. for (i = 0; i < nvectors; i++) {
  228. if (vp_dev->msix_affinity_masks[i])
  229. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  230. }
  231. kfree(vp_dev->msix_affinity_masks);
  232. out_free_msix_names:
  233. kfree(vp_dev->msix_names);
  234. out_free_irq_vectors:
  235. pci_free_irq_vectors(vp_dev->pci_dev);
  236. return err;
  237. }
  238. static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
  239. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  240. const char * const names[])
  241. {
  242. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  243. int i, err;
  244. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
  245. dev_name(&vdev->dev), vp_dev);
  246. if (err)
  247. return err;
  248. for (i = 0; i < nvqs; ++i) {
  249. if (!names[i]) {
  250. vqs[i] = NULL;
  251. continue;
  252. }
  253. vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
  254. VIRTIO_MSI_NO_VECTOR);
  255. if (IS_ERR(vqs[i])) {
  256. err = PTR_ERR(vqs[i]);
  257. goto out_remove_vqs;
  258. }
  259. }
  260. return 0;
  261. out_remove_vqs:
  262. vp_remove_vqs(vdev);
  263. free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
  264. return err;
  265. }
  266. /* the config->find_vqs() implementation */
  267. int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  268. struct virtqueue *vqs[], vq_callback_t *callbacks[],
  269. const char * const names[], struct irq_affinity *desc)
  270. {
  271. int err;
  272. err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, desc);
  273. if (!err)
  274. return 0;
  275. return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
  276. }
  277. const char *vp_bus_name(struct virtio_device *vdev)
  278. {
  279. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  280. return pci_name(vp_dev->pci_dev);
  281. }
  282. /* Setup the affinity for a virtqueue:
  283. * - force the affinity for per vq vector
  284. * - OR over all affinities for shared MSI
  285. * - ignore the affinity request if we're using INTX
  286. */
  287. int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
  288. {
  289. struct virtio_device *vdev = vq->vdev;
  290. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  291. if (!vq->callback)
  292. return -EINVAL;
  293. if (vp_dev->pci_dev->msix_enabled) {
  294. int vec = vp_dev->msix_vector_map[vq->index];
  295. struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
  296. unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
  297. if (cpu == -1)
  298. irq_set_affinity_hint(irq, NULL);
  299. else {
  300. cpumask_clear(mask);
  301. cpumask_set_cpu(cpu, mask);
  302. irq_set_affinity_hint(irq, mask);
  303. }
  304. }
  305. return 0;
  306. }
  307. const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
  308. {
  309. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  310. unsigned int *map = vp_dev->msix_vector_map;
  311. if (!map || map[index] == VIRTIO_MSI_NO_VECTOR)
  312. return NULL;
  313. return pci_irq_get_affinity(vp_dev->pci_dev, map[index]);
  314. }
  315. #ifdef CONFIG_PM_SLEEP
  316. static int virtio_pci_freeze(struct device *dev)
  317. {
  318. struct pci_dev *pci_dev = to_pci_dev(dev);
  319. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  320. int ret;
  321. ret = virtio_device_freeze(&vp_dev->vdev);
  322. if (!ret)
  323. pci_disable_device(pci_dev);
  324. return ret;
  325. }
  326. static int virtio_pci_restore(struct device *dev)
  327. {
  328. struct pci_dev *pci_dev = to_pci_dev(dev);
  329. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  330. int ret;
  331. ret = pci_enable_device(pci_dev);
  332. if (ret)
  333. return ret;
  334. pci_set_master(pci_dev);
  335. return virtio_device_restore(&vp_dev->vdev);
  336. }
  337. static const struct dev_pm_ops virtio_pci_pm_ops = {
  338. SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
  339. };
  340. #endif
  341. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  342. static const struct pci_device_id virtio_pci_id_table[] = {
  343. { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
  344. { 0 }
  345. };
  346. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  347. static void virtio_pci_release_dev(struct device *_d)
  348. {
  349. struct virtio_device *vdev = dev_to_virtio(_d);
  350. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  351. /* As struct device is a kobject, it's not safe to
  352. * free the memory (including the reference counter itself)
  353. * until it's release callback. */
  354. kfree(vp_dev);
  355. }
  356. static int virtio_pci_probe(struct pci_dev *pci_dev,
  357. const struct pci_device_id *id)
  358. {
  359. struct virtio_pci_device *vp_dev;
  360. int rc;
  361. /* allocate our structure and fill it out */
  362. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  363. if (!vp_dev)
  364. return -ENOMEM;
  365. pci_set_drvdata(pci_dev, vp_dev);
  366. vp_dev->vdev.dev.parent = &pci_dev->dev;
  367. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  368. vp_dev->pci_dev = pci_dev;
  369. /* enable the device */
  370. rc = pci_enable_device(pci_dev);
  371. if (rc)
  372. goto err_enable_device;
  373. if (force_legacy) {
  374. rc = virtio_pci_legacy_probe(vp_dev);
  375. /* Also try modern mode if we can't map BAR0 (no IO space). */
  376. if (rc == -ENODEV || rc == -ENOMEM)
  377. rc = virtio_pci_modern_probe(vp_dev);
  378. if (rc)
  379. goto err_probe;
  380. } else {
  381. rc = virtio_pci_modern_probe(vp_dev);
  382. if (rc == -ENODEV)
  383. rc = virtio_pci_legacy_probe(vp_dev);
  384. if (rc)
  385. goto err_probe;
  386. }
  387. pci_set_master(pci_dev);
  388. rc = register_virtio_device(&vp_dev->vdev);
  389. if (rc)
  390. goto err_register;
  391. return 0;
  392. err_register:
  393. if (vp_dev->ioaddr)
  394. virtio_pci_legacy_remove(vp_dev);
  395. else
  396. virtio_pci_modern_remove(vp_dev);
  397. err_probe:
  398. pci_disable_device(pci_dev);
  399. err_enable_device:
  400. kfree(vp_dev);
  401. return rc;
  402. }
  403. static void virtio_pci_remove(struct pci_dev *pci_dev)
  404. {
  405. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  406. struct device *dev = get_device(&vp_dev->vdev.dev);
  407. unregister_virtio_device(&vp_dev->vdev);
  408. if (vp_dev->ioaddr)
  409. virtio_pci_legacy_remove(vp_dev);
  410. else
  411. virtio_pci_modern_remove(vp_dev);
  412. pci_disable_device(pci_dev);
  413. put_device(dev);
  414. }
  415. static struct pci_driver virtio_pci_driver = {
  416. .name = "virtio-pci",
  417. .id_table = virtio_pci_id_table,
  418. .probe = virtio_pci_probe,
  419. .remove = virtio_pci_remove,
  420. #ifdef CONFIG_PM_SLEEP
  421. .driver.pm = &virtio_pci_pm_ops,
  422. #endif
  423. };
  424. module_pci_driver(virtio_pci_driver);
  425. MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
  426. MODULE_DESCRIPTION("virtio-pci");
  427. MODULE_LICENSE("GPL");
  428. MODULE_VERSION("1");