vdpa.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_VDPA_H
  3. #define _LINUX_VDPA_H
  4. #include <linux/kernel.h>
  5. #include <linux/device.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/vhost_iotlb.h>
  8. /**
  9. * vDPA callback definition.
  10. * @callback: interrupt callback function
  11. * @private: the data passed to the callback function
  12. */
  13. struct vdpa_callback {
  14. irqreturn_t (*callback)(void *data);
  15. void *private;
  16. };
  17. /**
  18. * vDPA notification area
  19. * @addr: base address of the notification area
  20. * @size: size of the notification area
  21. */
  22. struct vdpa_notification_area {
  23. resource_size_t addr;
  24. resource_size_t size;
  25. };
  26. /**
  27. * vDPA vq_state definition
  28. * @avail_index: available index
  29. */
  30. struct vdpa_vq_state {
  31. u16 avail_index;
  32. };
  33. /**
  34. * vDPA device - representation of a vDPA device
  35. * @dev: underlying device
  36. * @dma_dev: the actual device that is performing DMA
  37. * @config: the configuration ops for this device.
  38. * @index: device index
  39. * @features_valid: were features initialized? for legacy guests
  40. */
  41. struct vdpa_device {
  42. struct device dev;
  43. struct device *dma_dev;
  44. const struct vdpa_config_ops *config;
  45. unsigned int index;
  46. bool features_valid;
  47. int nvqs;
  48. };
  49. /**
  50. * vDPA IOVA range - the IOVA range support by the device
  51. * @first: start of the IOVA range
  52. * @last: end of the IOVA range
  53. */
  54. struct vdpa_iova_range {
  55. u64 first;
  56. u64 last;
  57. };
  58. /**
  59. * vDPA_config_ops - operations for configuring a vDPA device.
  60. * Note: vDPA device drivers are required to implement all of the
  61. * operations unless it is mentioned to be optional in the following
  62. * list.
  63. *
  64. * @set_vq_address: Set the address of virtqueue
  65. * @vdev: vdpa device
  66. * @idx: virtqueue index
  67. * @desc_area: address of desc area
  68. * @driver_area: address of driver area
  69. * @device_area: address of device area
  70. * Returns integer: success (0) or error (< 0)
  71. * @set_vq_num: Set the size of virtqueue
  72. * @vdev: vdpa device
  73. * @idx: virtqueue index
  74. * @num: the size of virtqueue
  75. * @kick_vq: Kick the virtqueue
  76. * @vdev: vdpa device
  77. * @idx: virtqueue index
  78. * @set_vq_cb: Set the interrupt callback function for
  79. * a virtqueue
  80. * @vdev: vdpa device
  81. * @idx: virtqueue index
  82. * @cb: virtio-vdev interrupt callback structure
  83. * @set_vq_ready: Set ready status for a virtqueue
  84. * @vdev: vdpa device
  85. * @idx: virtqueue index
  86. * @ready: ready (true) not ready(false)
  87. * @get_vq_ready: Get ready status for a virtqueue
  88. * @vdev: vdpa device
  89. * @idx: virtqueue index
  90. * Returns boolean: ready (true) or not (false)
  91. * @set_vq_state: Set the state for a virtqueue
  92. * @vdev: vdpa device
  93. * @idx: virtqueue index
  94. * @state: pointer to set virtqueue state (last_avail_idx)
  95. * Returns integer: success (0) or error (< 0)
  96. * @get_vq_state: Get the state for a virtqueue
  97. * @vdev: vdpa device
  98. * @idx: virtqueue index
  99. * @state: pointer to returned state (last_avail_idx)
  100. * @get_vq_notification: Get the notification area for a virtqueue
  101. * @vdev: vdpa device
  102. * @idx: virtqueue index
  103. * Returns the notifcation area
  104. * @get_vq_irq: Get the irq number of a virtqueue (optional,
  105. * but must implemented if require vq irq offloading)
  106. * @vdev: vdpa device
  107. * @idx: virtqueue index
  108. * Returns int: irq number of a virtqueue,
  109. * negative number if no irq assigned.
  110. * @get_vq_align: Get the virtqueue align requirement
  111. * for the device
  112. * @vdev: vdpa device
  113. * Returns virtqueue algin requirement
  114. * @get_features: Get virtio features supported by the device
  115. * @vdev: vdpa device
  116. * Returns the virtio features support by the
  117. * device
  118. * @set_features: Set virtio features supported by the driver
  119. * @vdev: vdpa device
  120. * @features: feature support by the driver
  121. * Returns integer: success (0) or error (< 0)
  122. * @set_config_cb: Set the config interrupt callback
  123. * @vdev: vdpa device
  124. * @cb: virtio-vdev interrupt callback structure
  125. * @get_vq_num_max: Get the max size of virtqueue
  126. * @vdev: vdpa device
  127. * Returns u16: max size of virtqueue
  128. * @get_device_id: Get virtio device id
  129. * @vdev: vdpa device
  130. * Returns u32: virtio device id
  131. * @get_vendor_id: Get id for the vendor that provides this device
  132. * @vdev: vdpa device
  133. * Returns u32: virtio vendor id
  134. * @get_status: Get the device status
  135. * @vdev: vdpa device
  136. * Returns u8: virtio device status
  137. * @set_status: Set the device status
  138. * @vdev: vdpa device
  139. * @status: virtio device status
  140. * @get_config: Read from device specific configuration space
  141. * @vdev: vdpa device
  142. * @offset: offset from the beginning of
  143. * configuration space
  144. * @buf: buffer used to read to
  145. * @len: the length to read from
  146. * configuration space
  147. * @set_config: Write to device specific configuration space
  148. * @vdev: vdpa device
  149. * @offset: offset from the beginning of
  150. * configuration space
  151. * @buf: buffer used to write from
  152. * @len: the length to write to
  153. * configuration space
  154. * @get_generation: Get device config generation (optional)
  155. * @vdev: vdpa device
  156. * Returns u32: device generation
  157. * @get_iova_range: Get supported iova range (optional)
  158. * @vdev: vdpa device
  159. * Returns the iova range supported by
  160. * the device.
  161. * @set_map: Set device memory mapping (optional)
  162. * Needed for device that using device
  163. * specific DMA translation (on-chip IOMMU)
  164. * @vdev: vdpa device
  165. * @iotlb: vhost memory mapping to be
  166. * used by the vDPA
  167. * Returns integer: success (0) or error (< 0)
  168. * @dma_map: Map an area of PA to IOVA (optional)
  169. * Needed for device that using device
  170. * specific DMA translation (on-chip IOMMU)
  171. * and preferring incremental map.
  172. * @vdev: vdpa device
  173. * @iova: iova to be mapped
  174. * @size: size of the area
  175. * @pa: physical address for the map
  176. * @perm: device access permission (VHOST_MAP_XX)
  177. * Returns integer: success (0) or error (< 0)
  178. * @dma_unmap: Unmap an area of IOVA (optional but
  179. * must be implemented with dma_map)
  180. * Needed for device that using device
  181. * specific DMA translation (on-chip IOMMU)
  182. * and preferring incremental unmap.
  183. * @vdev: vdpa device
  184. * @iova: iova to be unmapped
  185. * @size: size of the area
  186. * Returns integer: success (0) or error (< 0)
  187. * @free: Free resources that belongs to vDPA (optional)
  188. * @vdev: vdpa device
  189. */
  190. struct vdpa_config_ops {
  191. /* Virtqueue ops */
  192. int (*set_vq_address)(struct vdpa_device *vdev,
  193. u16 idx, u64 desc_area, u64 driver_area,
  194. u64 device_area);
  195. void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
  196. void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
  197. void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
  198. struct vdpa_callback *cb);
  199. void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
  200. bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
  201. int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
  202. const struct vdpa_vq_state *state);
  203. int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
  204. struct vdpa_vq_state *state);
  205. struct vdpa_notification_area
  206. (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
  207. /* vq irq is not expected to be changed once DRIVER_OK is set */
  208. int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
  209. /* Device ops */
  210. u32 (*get_vq_align)(struct vdpa_device *vdev);
  211. u64 (*get_features)(struct vdpa_device *vdev);
  212. int (*set_features)(struct vdpa_device *vdev, u64 features);
  213. void (*set_config_cb)(struct vdpa_device *vdev,
  214. struct vdpa_callback *cb);
  215. u16 (*get_vq_num_max)(struct vdpa_device *vdev);
  216. u32 (*get_device_id)(struct vdpa_device *vdev);
  217. u32 (*get_vendor_id)(struct vdpa_device *vdev);
  218. u8 (*get_status)(struct vdpa_device *vdev);
  219. void (*set_status)(struct vdpa_device *vdev, u8 status);
  220. void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
  221. void *buf, unsigned int len);
  222. void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
  223. const void *buf, unsigned int len);
  224. u32 (*get_generation)(struct vdpa_device *vdev);
  225. struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
  226. /* DMA ops */
  227. int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
  228. int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
  229. u64 pa, u32 perm);
  230. int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
  231. /* Free device resources */
  232. void (*free)(struct vdpa_device *vdev);
  233. };
  234. struct vdpa_device *__vdpa_alloc_device(struct device *parent,
  235. const struct vdpa_config_ops *config,
  236. int nvqs,
  237. size_t size);
  238. #define vdpa_alloc_device(dev_struct, member, parent, config, nvqs) \
  239. container_of(__vdpa_alloc_device( \
  240. parent, config, nvqs, \
  241. sizeof(dev_struct) + \
  242. BUILD_BUG_ON_ZERO(offsetof( \
  243. dev_struct, member))), \
  244. dev_struct, member)
  245. int vdpa_register_device(struct vdpa_device *vdev);
  246. void vdpa_unregister_device(struct vdpa_device *vdev);
  247. /**
  248. * vdpa_driver - operations for a vDPA driver
  249. * @driver: underlying device driver
  250. * @probe: the function to call when a device is found. Returns 0 or -errno.
  251. * @remove: the function to call when a device is removed.
  252. */
  253. struct vdpa_driver {
  254. struct device_driver driver;
  255. int (*probe)(struct vdpa_device *vdev);
  256. void (*remove)(struct vdpa_device *vdev);
  257. };
  258. #define vdpa_register_driver(drv) \
  259. __vdpa_register_driver(drv, THIS_MODULE)
  260. int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
  261. void vdpa_unregister_driver(struct vdpa_driver *drv);
  262. #define module_vdpa_driver(__vdpa_driver) \
  263. module_driver(__vdpa_driver, vdpa_register_driver, \
  264. vdpa_unregister_driver)
  265. static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
  266. {
  267. return container_of(driver, struct vdpa_driver, driver);
  268. }
  269. static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
  270. {
  271. return container_of(_dev, struct vdpa_device, dev);
  272. }
  273. static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
  274. {
  275. return dev_get_drvdata(&vdev->dev);
  276. }
  277. static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
  278. {
  279. dev_set_drvdata(&vdev->dev, data);
  280. }
  281. static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
  282. {
  283. return vdev->dma_dev;
  284. }
  285. static inline void vdpa_reset(struct vdpa_device *vdev)
  286. {
  287. const struct vdpa_config_ops *ops = vdev->config;
  288. vdev->features_valid = false;
  289. ops->set_status(vdev, 0);
  290. }
  291. static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
  292. {
  293. const struct vdpa_config_ops *ops = vdev->config;
  294. vdev->features_valid = true;
  295. return ops->set_features(vdev, features);
  296. }
  297. static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
  298. void *buf, unsigned int len)
  299. {
  300. const struct vdpa_config_ops *ops = vdev->config;
  301. /*
  302. * Config accesses aren't supposed to trigger before features are set.
  303. * If it does happen we assume a legacy guest.
  304. */
  305. if (!vdev->features_valid)
  306. vdpa_set_features(vdev, 0);
  307. ops->get_config(vdev, offset, buf, len);
  308. }
  309. #endif /* _LINUX_VDPA_H */