virtio_vdmabuf.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* SPDX-License-Identifier: (MIT OR GPL-2.0) */
  2. #ifndef _LINUX_VIRTIO_VDMABUF_H
  3. #define _LINUX_VIRTIO_VDMABUF_H
  4. #include <uapi/linux/virtio_vdmabuf.h>
  5. #include <linux/hashtable.h>
  6. #include "../../drivers/vhost/vhost.h"
  7. #define UINT32_MAX ((u32)~0U)
  8. #define VIRTIO_VDMABUF_MAX_ID UINT32_MAX
  9. #define VIRTIO_VDMABUF_HASH_BITS 7
  10. #define VIRTIO_VDMABUF_CARVEOUT_NAME_LEN 16
  11. #define VIRTIO_VDMABUF_MAX_OPS 6
  12. #define VIRTIO_VDMABUF_MAX_BP_NUM (VIRTIO_VDAMBUF_MAX_ALLOC_SIZE / PAGE_SIZE)
  13. #define VIRTIO_VDMABUF_CARVEOUT_NAMES {{"vi"},{"vo"},{"enc"},{"dec"},{"gpu"},{"dpu"}}
  14. struct buf_pair {
  15. struct page *page;
  16. phys_addr_t addr;
  17. unsigned int size;
  18. };
  19. struct virtio_vdmabuf_buf {
  20. int vmid;
  21. size_t size;
  22. unsigned int buf_id;
  23. struct mutex lock;
  24. /* list of devices attached to this buffer */
  25. struct list_head attachments;
  26. struct dma_buf *dma_buf;
  27. /* validity of the buffer */
  28. bool valid;
  29. /* set if the buffer is imported from remote */
  30. bool imported;
  31. /* size of private */
  32. size_t sz_priv;
  33. /* private data associated with the exported buffer */
  34. void *priv;
  35. void *host;
  36. struct hlist_node node;
  37. /* real buf info */
  38. int heap_type;
  39. int carveout_type;
  40. int flags;
  41. int bp_num;
  42. struct buf_pair bp[];
  43. };
  44. struct virtio_vdmabuf_attachment {
  45. struct device *dev;
  46. struct sg_table *sgt;
  47. struct list_head list;
  48. };
  49. struct carveout_buf {
  50. phys_addr_t addr;
  51. unsigned int size;
  52. bool ready;
  53. };
  54. struct virtio_vdmabuf_event {
  55. struct list_head list;
  56. /*
  57. * 0: vmid
  58. * 1: buf_id
  59. * 2: heap_type
  60. * 3: carveout_type
  61. * 4: flags
  62. * 5: bp num
  63. */
  64. unsigned int op[VIRTIO_VDMABUF_MAX_OPS];
  65. struct buf_pair bp[];
  66. };
  67. struct virtio_vdmabuf_event_queue {
  68. wait_queue_head_t e_wait;
  69. struct list_head e_list;
  70. spinlock_t e_lock;
  71. struct mutex e_readlock;
  72. };
  73. /* driver information */
  74. struct virtio_vdmabuf_info {
  75. struct device *dev;
  76. struct list_head head_vdmabuf_list;
  77. struct list_head kvm_instances;
  78. spinlock_t local_lock;
  79. DECLARE_HASHTABLE(buf_list_local, VIRTIO_VDMABUF_HASH_BITS);
  80. spinlock_t import_lock;
  81. DECLARE_HASHTABLE(buf_list_import, VIRTIO_VDMABUF_HASH_BITS);
  82. void *priv;
  83. struct mutex g_mutex;
  84. bool host_ready;
  85. };
  86. struct vhost_vdmabuf;
  87. struct virtio_vdmabuf;
  88. /*
  89. * IOCTL definitions
  90. */
  91. typedef int (*vhost_vdmabuf_ioctl_t)(struct vhost_vdmabuf *vdmabuf,
  92. void *data);
  93. typedef int (*virtio_vdmabuf_ioctl_t)(struct virtio_vdmabuf *vdmabuf,
  94. void *data);
  95. struct vhost_vdmabuf_ioctl_desc {
  96. unsigned int cmd;
  97. int flags;
  98. vhost_vdmabuf_ioctl_t func;
  99. const char *name;
  100. };
  101. struct virtio_vdmabuf_ioctl_desc {
  102. unsigned int cmd;
  103. int flags;
  104. virtio_vdmabuf_ioctl_t func;
  105. const char *name;
  106. };
  107. #define VIRTIO_VDMABUF_IOCTL_DEF(ioctl, _func, _flags) \
  108. [_IOC_NR(ioctl)] = { \
  109. .cmd = ioctl, \
  110. .func = _func, \
  111. .flags = _flags, \
  112. .name = #ioctl \
  113. }
  114. /* msg structures */
  115. struct virtio_vdmabuf_msg {
  116. struct list_head list;
  117. unsigned int cmd;
  118. /*
  119. * 0: vmid
  120. * 1: buf_id
  121. * 2: heap_type
  122. * 3: carveout_type
  123. * 4: flags
  124. * 5: bp num
  125. */
  126. unsigned int op[VIRTIO_VDMABUF_MAX_OPS];
  127. struct buf_pair bp[];
  128. };
  129. enum {
  130. VDMABUF_VQ_RECV = 0,
  131. VDMABUF_VQ_SEND = 1,
  132. VDMABUF_VQ_MAX = 2,
  133. };
  134. enum virtio_vdmabuf_cmd {
  135. VIRTIO_VDMABUF_CMD_VMID_REQ,
  136. VIRTIO_VDMABUF_CMD_VMID_REPLY,
  137. VIRTIO_VDMABUF_CMD_IMPORT_REQ,
  138. VIRTIO_VDMABUF_CMD_IMPORT_REPLY,
  139. VIRTIO_VDMABUF_CMD_REL_NOTIFY,
  140. };
  141. struct virtio_vdmabuf {
  142. /* virtio device structure */
  143. struct virtio_device *vdev;
  144. /* virtual queue array */
  145. struct virtqueue *vqs[VDMABUF_VQ_MAX];
  146. /* ID of guest OS */
  147. unsigned int vmid;
  148. /* spin lock that needs to be acquired before accessing
  149. * virtual queue
  150. */
  151. spinlock_t vq_lock;
  152. struct mutex recv_lock;
  153. struct mutex send_lock;
  154. spinlock_t msg_list_lock;
  155. struct list_head msg_list;
  156. /* workqueue */
  157. struct workqueue_struct *wq;
  158. struct work_struct recv_work;
  159. struct work_struct send_work;
  160. struct work_struct send_msg_work;
  161. struct virtio_vdmabuf_event_queue *eq_import;
  162. };
  163. struct vhost_vdmabuf {
  164. struct vhost_dev dev;
  165. struct vhost_virtqueue vqs[VDMABUF_VQ_MAX];
  166. struct vhost_work send_work;
  167. struct virtio_vdmabuf_event_queue *eq_import;
  168. unsigned int vmid;
  169. struct list_head msg_list;
  170. struct list_head list;
  171. spinlock_t local_lock;
  172. DECLARE_HASHTABLE(buf_list_local, VIRTIO_VDMABUF_HASH_BITS);
  173. spinlock_t import_lock;
  174. DECLARE_HASHTABLE(buf_list_import, VIRTIO_VDMABUF_HASH_BITS);
  175. };
  176. static inline int vdmabuf_msg_size(int bp_num)
  177. {
  178. return sizeof(struct virtio_vdmabuf_msg) +
  179. sizeof(struct buf_pair) * bp_num;
  180. }
  181. /* guest api */
  182. static inline int
  183. virtio_vdmabuf_add_buf(struct virtio_vdmabuf_info *info,
  184. struct virtio_vdmabuf_buf *new,
  185. bool local)
  186. {
  187. if (local)
  188. hash_add(info->buf_list_local, &new->node, new->buf_id);
  189. else
  190. hash_add(info->buf_list_import, &new->node, new->buf_id);
  191. return 0;
  192. }
  193. static inline struct virtio_vdmabuf_buf
  194. *virtio_vdmabuf_find_buf(struct virtio_vdmabuf_info *info,
  195. unsigned int buf_id, bool local)
  196. {
  197. struct virtio_vdmabuf_buf *found;
  198. if (local) {
  199. hash_for_each_possible(info->buf_list_local, found, node, buf_id)
  200. if (found->buf_id == buf_id)
  201. return found;
  202. } else {
  203. hash_for_each_possible(info->buf_list_import, found, node, buf_id)
  204. if (found->buf_id == buf_id)
  205. return found;
  206. }
  207. return NULL;
  208. }
  209. static inline int
  210. virtio_vdmabuf_del_buf(struct virtio_vdmabuf_info *info,
  211. unsigned int buf_id, bool local)
  212. {
  213. struct virtio_vdmabuf_buf *found;
  214. found = virtio_vdmabuf_find_buf(info, buf_id, local);
  215. if (!found)
  216. return -ENOENT;
  217. hash_del(&found->node);
  218. return 0;
  219. }
  220. /* host api */
  221. static inline int
  222. vhost_vdmabuf_add_buf(struct vhost_vdmabuf *vdmabuf,
  223. struct virtio_vdmabuf_buf *new,
  224. bool local)
  225. {
  226. if (local)
  227. hash_add(vdmabuf->buf_list_local, &new->node, new->buf_id);
  228. else
  229. hash_add(vdmabuf->buf_list_import, &new->node, new->buf_id);
  230. return 0;
  231. }
  232. static inline struct virtio_vdmabuf_buf
  233. *vhost_vdmabuf_find_buf(struct vhost_vdmabuf *vdmabuf,
  234. unsigned int buf_id, bool local)
  235. {
  236. struct virtio_vdmabuf_buf *found;
  237. if (local) {
  238. hash_for_each_possible(vdmabuf->buf_list_local, found, node, buf_id)
  239. if (found->buf_id == buf_id)
  240. return found;
  241. } else {
  242. hash_for_each_possible(vdmabuf->buf_list_import, found, node, buf_id)
  243. if (found->buf_id == buf_id)
  244. return found;
  245. }
  246. return NULL;
  247. }
  248. static inline int
  249. vhost_vdmabuf_del_buf(struct vhost_vdmabuf *vdmabuf,
  250. unsigned int buf_id, bool local)
  251. {
  252. struct virtio_vdmabuf_buf *found;
  253. found = vhost_vdmabuf_find_buf(vdmabuf, buf_id, local);
  254. if (!found)
  255. return -ENOENT;
  256. hash_del(&found->node);
  257. return 0;
  258. }
  259. #endif