virtio_pci_legacy_dev.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include "linux/virtio_pci.h"
  3. #include <linux/virtio_pci_legacy.h>
  4. #include <linux/module.h>
  5. #include <linux/pci.h>
  6. /*
  7. * vp_legacy_probe: probe the legacy virtio pci device, note that the
  8. * caller is required to enable PCI device before calling this function.
  9. * @ldev: the legacy virtio-pci device
  10. *
  11. * Return 0 on succeed otherwise fail
  12. */
  13. int vp_legacy_probe(struct virtio_pci_legacy_device *ldev)
  14. {
  15. struct pci_dev *pci_dev = ldev->pci_dev;
  16. int rc;
  17. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  18. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  19. return -ENODEV;
  20. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION)
  21. return -ENODEV;
  22. rc = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(64));
  23. if (rc) {
  24. rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(32));
  25. } else {
  26. /*
  27. * The virtio ring base address is expressed as a 32-bit PFN,
  28. * with a page size of 1 << VIRTIO_PCI_QUEUE_ADDR_SHIFT.
  29. */
  30. dma_set_coherent_mask(&pci_dev->dev,
  31. DMA_BIT_MASK(32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT));
  32. }
  33. if (rc)
  34. dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  35. rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy");
  36. if (rc)
  37. return rc;
  38. ldev->ioaddr = pci_iomap(pci_dev, 0, 0);
  39. if (!ldev->ioaddr) {
  40. rc = -EIO;
  41. goto err_iomap;
  42. }
  43. ldev->isr = ldev->ioaddr + VIRTIO_PCI_ISR;
  44. ldev->id.vendor = pci_dev->subsystem_vendor;
  45. ldev->id.device = pci_dev->subsystem_device;
  46. return 0;
  47. err_iomap:
  48. pci_release_region(pci_dev, 0);
  49. return rc;
  50. }
  51. EXPORT_SYMBOL_GPL(vp_legacy_probe);
  52. /*
  53. * vp_legacy_probe: remove and cleanup the legacy virtio pci device
  54. * @ldev: the legacy virtio-pci device
  55. */
  56. void vp_legacy_remove(struct virtio_pci_legacy_device *ldev)
  57. {
  58. struct pci_dev *pci_dev = ldev->pci_dev;
  59. pci_iounmap(pci_dev, ldev->ioaddr);
  60. pci_release_region(pci_dev, 0);
  61. }
  62. EXPORT_SYMBOL_GPL(vp_legacy_remove);
  63. /*
  64. * vp_legacy_get_features - get features from device
  65. * @ldev: the legacy virtio-pci device
  66. *
  67. * Returns the features read from the device
  68. */
  69. u64 vp_legacy_get_features(struct virtio_pci_legacy_device *ldev)
  70. {
  71. return ioread32(ldev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  72. }
  73. EXPORT_SYMBOL_GPL(vp_legacy_get_features);
  74. /*
  75. * vp_legacy_get_driver_features - get driver features from device
  76. * @ldev: the legacy virtio-pci device
  77. *
  78. * Returns the driver features read from the device
  79. */
  80. u64 vp_legacy_get_driver_features(struct virtio_pci_legacy_device *ldev)
  81. {
  82. return ioread32(ldev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
  83. }
  84. EXPORT_SYMBOL_GPL(vp_legacy_get_driver_features);
  85. /*
  86. * vp_legacy_set_features - set features to device
  87. * @ldev: the legacy virtio-pci device
  88. * @features: the features set to device
  89. */
  90. void vp_legacy_set_features(struct virtio_pci_legacy_device *ldev,
  91. u32 features)
  92. {
  93. iowrite32(features, ldev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
  94. }
  95. EXPORT_SYMBOL_GPL(vp_legacy_set_features);
  96. /*
  97. * vp_legacy_get_status - get the device status
  98. * @ldev: the legacy virtio-pci device
  99. *
  100. * Returns the status read from device
  101. */
  102. u8 vp_legacy_get_status(struct virtio_pci_legacy_device *ldev)
  103. {
  104. return ioread8(ldev->ioaddr + VIRTIO_PCI_STATUS);
  105. }
  106. EXPORT_SYMBOL_GPL(vp_legacy_get_status);
  107. /*
  108. * vp_legacy_set_status - set status to device
  109. * @ldev: the legacy virtio-pci device
  110. * @status: the status set to device
  111. */
  112. void vp_legacy_set_status(struct virtio_pci_legacy_device *ldev,
  113. u8 status)
  114. {
  115. iowrite8(status, ldev->ioaddr + VIRTIO_PCI_STATUS);
  116. }
  117. EXPORT_SYMBOL_GPL(vp_legacy_set_status);
  118. /*
  119. * vp_legacy_queue_vector - set the MSIX vector for a specific virtqueue
  120. * @ldev: the legacy virtio-pci device
  121. * @index: queue index
  122. * @vector: the config vector
  123. *
  124. * Returns the config vector read from the device
  125. */
  126. u16 vp_legacy_queue_vector(struct virtio_pci_legacy_device *ldev,
  127. u16 index, u16 vector)
  128. {
  129. iowrite16(index, ldev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  130. iowrite16(vector, ldev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  131. /* Flush the write out to device */
  132. return ioread16(ldev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  133. }
  134. EXPORT_SYMBOL_GPL(vp_legacy_queue_vector);
  135. /*
  136. * vp_legacy_config_vector - set the vector for config interrupt
  137. * @ldev: the legacy virtio-pci device
  138. * @vector: the config vector
  139. *
  140. * Returns the config vector read from the device
  141. */
  142. u16 vp_legacy_config_vector(struct virtio_pci_legacy_device *ldev,
  143. u16 vector)
  144. {
  145. /* Setup the vector used for configuration events */
  146. iowrite16(vector, ldev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  147. /* Verify we had enough resources to assign the vector */
  148. /* Will also flush the write out to device */
  149. return ioread16(ldev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  150. }
  151. EXPORT_SYMBOL_GPL(vp_legacy_config_vector);
  152. /*
  153. * vp_legacy_set_queue_address - set the virtqueue address
  154. * @ldev: the legacy virtio-pci device
  155. * @index: the queue index
  156. * @queue_pfn: pfn of the virtqueue
  157. */
  158. void vp_legacy_set_queue_address(struct virtio_pci_legacy_device *ldev,
  159. u16 index, u32 queue_pfn)
  160. {
  161. iowrite16(index, ldev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  162. iowrite32(queue_pfn, ldev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  163. }
  164. EXPORT_SYMBOL_GPL(vp_legacy_set_queue_address);
  165. /*
  166. * vp_legacy_get_queue_enable - enable a virtqueue
  167. * @ldev: the legacy virtio-pci device
  168. * @index: the queue index
  169. *
  170. * Returns whether a virtqueue is enabled or not
  171. */
  172. bool vp_legacy_get_queue_enable(struct virtio_pci_legacy_device *ldev,
  173. u16 index)
  174. {
  175. iowrite16(index, ldev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  176. return ioread32(ldev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  177. }
  178. EXPORT_SYMBOL_GPL(vp_legacy_get_queue_enable);
  179. /*
  180. * vp_legacy_get_queue_size - get size for a virtqueue
  181. * @ldev: the legacy virtio-pci device
  182. * @index: the queue index
  183. *
  184. * Returns the size of the virtqueue
  185. */
  186. u16 vp_legacy_get_queue_size(struct virtio_pci_legacy_device *ldev,
  187. u16 index)
  188. {
  189. iowrite16(index, ldev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  190. return ioread16(ldev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  191. }
  192. EXPORT_SYMBOL_GPL(vp_legacy_get_queue_size);
  193. MODULE_VERSION("0.1");
  194. MODULE_DESCRIPTION("Legacy Virtio PCI Device");
  195. MODULE_AUTHOR("Wu Zongyong <[email protected]>");
  196. MODULE_LICENSE("GPL");