You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virtio-pci.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifndef _VIRTIO_PCI_H_
  2. # define _VIRTIO_PCI_H_
  3. /* A 32-bit r/o bitmask of the features supported by the host */
  4. #define VIRTIO_PCI_HOST_FEATURES 0
  5. /* A 32-bit r/w bitmask of features activated by the guest */
  6. #define VIRTIO_PCI_GUEST_FEATURES 4
  7. /* A 32-bit r/w PFN for the currently selected queue */
  8. #define VIRTIO_PCI_QUEUE_PFN 8
  9. /* A 16-bit r/o queue size for the currently selected queue */
  10. #define VIRTIO_PCI_QUEUE_NUM 12
  11. /* A 16-bit r/w queue selector */
  12. #define VIRTIO_PCI_QUEUE_SEL 14
  13. /* A 16-bit r/w queue notifier */
  14. #define VIRTIO_PCI_QUEUE_NOTIFY 16
  15. /* An 8-bit device status register. */
  16. #define VIRTIO_PCI_STATUS 18
  17. /* An 8-bit r/o interrupt status register. Reading the value will return the
  18. * current contents of the ISR and will also clear it. This is effectively
  19. * a read-and-acknowledge. */
  20. #define VIRTIO_PCI_ISR 19
  21. /* The bit of the ISR which indicates a device configuration change. */
  22. #define VIRTIO_PCI_ISR_CONFIG 0x2
  23. /* The remaining space is defined by each driver as the per-driver
  24. * configuration space */
  25. #define VIRTIO_PCI_CONFIG 20
  26. /* Virtio ABI version, this must match exactly */
  27. #define VIRTIO_PCI_ABI_VERSION 0
  28. /* PCI capability types: */
  29. #define VIRTIO_PCI_CAP_COMMON_CFG 1 /* Common configuration */
  30. #define VIRTIO_PCI_CAP_NOTIFY_CFG 2 /* Notifications */
  31. #define VIRTIO_PCI_CAP_ISR_CFG 3 /* ISR access */
  32. #define VIRTIO_PCI_CAP_DEVICE_CFG 4 /* Device specific configuration */
  33. #define VIRTIO_PCI_CAP_PCI_CFG 5 /* PCI configuration access */
  34. #define __u8 uint8_t
  35. #define __le16 uint16_t
  36. #define __le32 uint32_t
  37. #define __le64 uint64_t
  38. /* This is the PCI capability header: */
  39. struct virtio_pci_cap {
  40. __u8 cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */
  41. __u8 cap_next; /* Generic PCI field: next ptr. */
  42. __u8 cap_len; /* Generic PCI field: capability length */
  43. __u8 cfg_type; /* Identifies the structure. */
  44. __u8 bar; /* Where to find it. */
  45. __u8 padding[3]; /* Pad to full dword. */
  46. __le32 offset; /* Offset within bar. */
  47. __le32 length; /* Length of the structure, in bytes. */
  48. };
  49. struct virtio_pci_notify_cap {
  50. struct virtio_pci_cap cap;
  51. __le32 notify_off_multiplier; /* Multiplier for queue_notify_off. */
  52. };
  53. struct virtio_pci_cfg_cap {
  54. struct virtio_pci_cap cap;
  55. __u8 pci_cfg_data[4]; /* Data for BAR access. */
  56. };
  57. /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
  58. struct virtio_pci_common_cfg {
  59. /* About the whole device. */
  60. __le32 device_feature_select; /* read-write */
  61. __le32 device_feature; /* read-only */
  62. __le32 guest_feature_select; /* read-write */
  63. __le32 guest_feature; /* read-write */
  64. __le16 msix_config; /* read-write */
  65. __le16 num_queues; /* read-only */
  66. __u8 device_status; /* read-write */
  67. __u8 config_generation; /* read-only */
  68. /* About a specific virtqueue. */
  69. __le16 queue_select; /* read-write */
  70. __le16 queue_size; /* read-write, power of 2. */
  71. __le16 queue_msix_vector; /* read-write */
  72. __le16 queue_enable; /* read-write */
  73. __le16 queue_notify_off; /* read-only */
  74. __le32 queue_desc_lo; /* read-write */
  75. __le32 queue_desc_hi; /* read-write */
  76. __le32 queue_avail_lo; /* read-write */
  77. __le32 queue_avail_hi; /* read-write */
  78. __le32 queue_used_lo; /* read-write */
  79. __le32 queue_used_hi; /* read-write */
  80. };
  81. /* Virtio 1.0 PCI region descriptor. We support memory mapped I/O, port I/O,
  82. * and PCI config space access via the cfg PCI capability as a fallback. */
  83. struct virtio_pci_region {
  84. void *base;
  85. size_t length;
  86. u8 bar;
  87. /* How to interpret the base field */
  88. #define VIRTIO_PCI_REGION_TYPE_MASK 0x00000003
  89. /* The base field is a memory address */
  90. #define VIRTIO_PCI_REGION_MEMORY 0x00000001
  91. /* The base field is a port address */
  92. #define VIRTIO_PCI_REGION_PORT 0x00000002
  93. /* The base field is an offset within the PCI bar */
  94. #define VIRTIO_PCI_REGION_PCI_CONFIG 0x00000003
  95. unsigned flags;
  96. };
  97. /* Virtio 1.0 device state */
  98. struct virtio_pci_modern_device {
  99. struct pci_device *pci;
  100. /* VIRTIO_PCI_CAP_PCI_CFG position */
  101. int cfg_cap_pos;
  102. /* VIRTIO_PCI_CAP_COMMON_CFG data */
  103. struct virtio_pci_region common;
  104. /* VIRTIO_PCI_CAP_DEVICE_CFG data */
  105. struct virtio_pci_region device;
  106. /* VIRTIO_PCI_CAP_ISR_CFG data */
  107. struct virtio_pci_region isr;
  108. /* VIRTIO_PCI_CAP_NOTIFY_CFG data */
  109. int notify_cap_pos;
  110. };
  111. static inline u32 vp_get_features(unsigned int ioaddr)
  112. {
  113. return inl(ioaddr + VIRTIO_PCI_HOST_FEATURES);
  114. }
  115. static inline void vp_set_features(unsigned int ioaddr, u32 features)
  116. {
  117. outl(features, ioaddr + VIRTIO_PCI_GUEST_FEATURES);
  118. }
  119. static inline void vp_get(unsigned int ioaddr, unsigned offset,
  120. void *buf, unsigned len)
  121. {
  122. u8 *ptr = buf;
  123. unsigned i;
  124. for (i = 0; i < len; i++)
  125. ptr[i] = inb(ioaddr + VIRTIO_PCI_CONFIG + offset + i);
  126. }
  127. static inline u8 vp_get_status(unsigned int ioaddr)
  128. {
  129. return inb(ioaddr + VIRTIO_PCI_STATUS);
  130. }
  131. static inline void vp_set_status(unsigned int ioaddr, u8 status)
  132. {
  133. if (status == 0) /* reset */
  134. return;
  135. outb(status, ioaddr + VIRTIO_PCI_STATUS);
  136. }
  137. static inline u8 vp_get_isr(unsigned int ioaddr)
  138. {
  139. return inb(ioaddr + VIRTIO_PCI_ISR);
  140. }
  141. static inline void vp_reset(unsigned int ioaddr)
  142. {
  143. outb(0, ioaddr + VIRTIO_PCI_STATUS);
  144. (void)inb(ioaddr + VIRTIO_PCI_ISR);
  145. }
  146. static inline void vp_notify(unsigned int ioaddr, int queue_index)
  147. {
  148. outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  149. }
  150. static inline void vp_del_vq(unsigned int ioaddr, int queue_index)
  151. {
  152. /* select the queue */
  153. outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_SEL);
  154. /* deactivate the queue */
  155. outl(0, ioaddr + VIRTIO_PCI_QUEUE_PFN);
  156. }
  157. struct vring_virtqueue;
  158. void vp_free_vq(struct vring_virtqueue *vq);
  159. int vp_find_vq(unsigned int ioaddr, int queue_index,
  160. struct vring_virtqueue *vq);
  161. /* Virtio 1.0 I/O routines abstract away the three possible HW access
  162. * mechanisms - memory, port I/O, and PCI cfg space access. Also built-in
  163. * are endianness conversions - to LE on write and from LE on read. */
  164. void vpm_iowrite8(struct virtio_pci_modern_device *vdev,
  165. struct virtio_pci_region *region, u8 data, size_t offset);
  166. void vpm_iowrite16(struct virtio_pci_modern_device *vdev,
  167. struct virtio_pci_region *region, u16 data, size_t offset);
  168. void vpm_iowrite32(struct virtio_pci_modern_device *vdev,
  169. struct virtio_pci_region *region, u32 data, size_t offset);
  170. static inline void vpm_iowrite64(struct virtio_pci_modern_device *vdev,
  171. struct virtio_pci_region *region,
  172. u64 data, size_t offset_lo, size_t offset_hi)
  173. {
  174. vpm_iowrite32(vdev, region, (u32)data, offset_lo);
  175. vpm_iowrite32(vdev, region, data >> 32, offset_hi);
  176. }
  177. u8 vpm_ioread8(struct virtio_pci_modern_device *vdev,
  178. struct virtio_pci_region *region, size_t offset);
  179. u16 vpm_ioread16(struct virtio_pci_modern_device *vdev,
  180. struct virtio_pci_region *region, size_t offset);
  181. u32 vpm_ioread32(struct virtio_pci_modern_device *vdev,
  182. struct virtio_pci_region *region, size_t offset);
  183. /* Virtio 1.0 device manipulation routines */
  184. #define COMMON_OFFSET(field) offsetof(struct virtio_pci_common_cfg, field)
  185. static inline void vpm_reset(struct virtio_pci_modern_device *vdev)
  186. {
  187. vpm_iowrite8(vdev, &vdev->common, 0, COMMON_OFFSET(device_status));
  188. while (vpm_ioread8(vdev, &vdev->common, COMMON_OFFSET(device_status)))
  189. mdelay(1);
  190. }
  191. static inline u8 vpm_get_status(struct virtio_pci_modern_device *vdev)
  192. {
  193. return vpm_ioread8(vdev, &vdev->common, COMMON_OFFSET(device_status));
  194. }
  195. static inline void vpm_add_status(struct virtio_pci_modern_device *vdev,
  196. u8 status)
  197. {
  198. u8 curr_status = vpm_ioread8(vdev, &vdev->common, COMMON_OFFSET(device_status));
  199. vpm_iowrite8(vdev, &vdev->common,
  200. curr_status | status, COMMON_OFFSET(device_status));
  201. }
  202. static inline u64 vpm_get_features(struct virtio_pci_modern_device *vdev)
  203. {
  204. u32 features_lo, features_hi;
  205. vpm_iowrite32(vdev, &vdev->common, 0, COMMON_OFFSET(device_feature_select));
  206. features_lo = vpm_ioread32(vdev, &vdev->common, COMMON_OFFSET(device_feature));
  207. vpm_iowrite32(vdev, &vdev->common, 1, COMMON_OFFSET(device_feature_select));
  208. features_hi = vpm_ioread32(vdev, &vdev->common, COMMON_OFFSET(device_feature));
  209. return ((u64)features_hi << 32) | features_lo;
  210. }
  211. static inline void vpm_set_features(struct virtio_pci_modern_device *vdev,
  212. u64 features)
  213. {
  214. u32 features_lo = (u32)features;
  215. u32 features_hi = features >> 32;
  216. vpm_iowrite32(vdev, &vdev->common, 0, COMMON_OFFSET(guest_feature_select));
  217. vpm_iowrite32(vdev, &vdev->common, features_lo, COMMON_OFFSET(guest_feature));
  218. vpm_iowrite32(vdev, &vdev->common, 1, COMMON_OFFSET(guest_feature_select));
  219. vpm_iowrite32(vdev, &vdev->common, features_hi, COMMON_OFFSET(guest_feature));
  220. }
  221. static inline void vpm_get(struct virtio_pci_modern_device *vdev,
  222. unsigned offset, void *buf, unsigned len)
  223. {
  224. u8 *ptr = buf;
  225. unsigned i;
  226. for (i = 0; i < len; i++)
  227. ptr[i] = vpm_ioread8(vdev, &vdev->device, offset + i);
  228. }
  229. static inline u8 vpm_get_isr(struct virtio_pci_modern_device *vdev)
  230. {
  231. return vpm_ioread8(vdev, &vdev->isr, 0);
  232. }
  233. void vpm_notify(struct virtio_pci_modern_device *vdev,
  234. struct vring_virtqueue *vq);
  235. int vpm_find_vqs(struct virtio_pci_modern_device *vdev,
  236. unsigned nvqs, struct vring_virtqueue *vqs);
  237. int virtio_pci_find_capability(struct pci_device *pci, uint8_t cfg_type);
  238. int virtio_pci_map_capability(struct pci_device *pci, int cap, size_t minlen,
  239. u32 align, u32 start, u32 size,
  240. struct virtio_pci_region *region);
  241. void virtio_pci_unmap_capability(struct virtio_pci_region *region);
  242. #endif /* _VIRTIO_PCI_H_ */