hgsl.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2020-2022, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __HGSL_H_
  7. #define __HGSL_H_
  8. #include <linux/types.h>
  9. #include <linux/cdev.h>
  10. #include <linux/dma-buf.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/sync_file.h>
  13. #include "hgsl_hyp.h"
  14. #include "hgsl_memory.h"
  15. #include "hgsl_tcsr.h"
  16. #define HGSL_TIMELINE_NAME_LEN 64
  17. #define HGSL_ISYNC_32BITS_TIMELINE 0
  18. #define HGSL_ISYNC_64BITS_TIMELINE 1
  19. /* Support upto 3 GVMs: 3 DBQs(Low/Medium/High priority) per GVM */
  20. #define MAX_DB_QUEUE 9
  21. #define HGSL_TCSR_NUM 4
  22. #define HGSL_CONTEXT_NUM 256
  23. struct qcom_hgsl;
  24. struct hgsl_hsync_timeline;
  25. #pragma pack(push, 4)
  26. struct shadow_ts {
  27. unsigned int sop;
  28. unsigned int unused1;
  29. unsigned int eop;
  30. unsigned int unused2;
  31. unsigned int reserved[6];
  32. };
  33. #pragma pack(pop)
  34. struct reg {
  35. unsigned long paddr;
  36. unsigned long size;
  37. void __iomem *vaddr;
  38. };
  39. struct hw_version {
  40. unsigned int version;
  41. unsigned int release;
  42. };
  43. struct db_buffer {
  44. int32_t dwords;
  45. void *vaddr;
  46. };
  47. struct dbq_ibdesc_priv {
  48. bool buf_inuse;
  49. uint32_t context_id;
  50. uint32_t timestamp;
  51. };
  52. struct doorbell_queue {
  53. struct dma_buf *dma;
  54. struct iosys_map map;
  55. void *vbase;
  56. uint64_t gmuaddr;
  57. struct db_buffer data;
  58. uint32_t state;
  59. int tcsr_idx;
  60. uint32_t dbq_idx;
  61. struct dbq_ibdesc_priv ibdesc_priv;
  62. uint32_t ibdesc_max_size;
  63. struct mutex lock;
  64. atomic_t seq_num;
  65. };
  66. struct doorbell_context_queue {
  67. struct hgsl_mem_node *queue_mem;
  68. struct iosys_map map;
  69. uint32_t db_signal;
  70. uint32_t seq_num;
  71. void *queue_header;
  72. void *queue_body;
  73. void *indirect_ibs;
  74. uint32_t queue_header_gmuaddr;
  75. uint32_t queue_body_gmuaddr;
  76. uint32_t indirect_ibs_gmuaddr;
  77. uint32_t queue_size;
  78. int irq_idx;
  79. uint32_t indirect_ib_ts;
  80. };
  81. struct qcom_hgsl {
  82. struct device *dev;
  83. /* character device info */
  84. struct cdev cdev;
  85. dev_t device_no;
  86. struct class *driver_class;
  87. struct device *class_dev;
  88. /* registers mapping */
  89. struct reg reg_ver;
  90. struct reg reg_dbidx;
  91. struct doorbell_queue dbq[MAX_DB_QUEUE];
  92. struct hgsl_dbq_info dbq_info[MAX_DB_QUEUE];
  93. /* Could disable db and use isync only */
  94. bool db_off;
  95. /* global doorbell tcsr */
  96. struct hgsl_tcsr *tcsr[HGSL_TCSR_NUM][HGSL_TCSR_ROLE_MAX];
  97. int tcsr_idx;
  98. struct hgsl_context **contexts;
  99. rwlock_t ctxt_lock;
  100. struct list_head active_wait_list;
  101. spinlock_t active_wait_lock;
  102. struct workqueue_struct *wq;
  103. struct work_struct ts_retire_work;
  104. struct hw_version *ver;
  105. struct hgsl_hyp_priv_t global_hyp;
  106. bool global_hyp_inited;
  107. struct mutex mutex;
  108. struct list_head active_list;
  109. struct list_head release_list;
  110. struct workqueue_struct *release_wq;
  111. struct work_struct release_work;
  112. struct idr isync_timeline_idr;
  113. spinlock_t isync_timeline_lock;
  114. atomic64_t total_mem_size;
  115. bool default_iocoherency;
  116. /* Debug nodes */
  117. struct kobject sysfs;
  118. struct kobject *clients_sysfs;
  119. struct dentry *debugfs;
  120. struct dentry *clients_debugfs;
  121. };
  122. /**
  123. * HGSL context define
  124. **/
  125. struct hgsl_context {
  126. struct hgsl_priv *priv;
  127. struct iosys_map map;
  128. uint32_t context_id;
  129. uint32_t devhandle;
  130. uint32_t flags;
  131. struct shadow_ts *shadow_ts;
  132. wait_queue_head_t wait_q;
  133. pid_t pid;
  134. bool dbq_assigned;
  135. uint32_t dbq_info;
  136. struct doorbell_queue *dbq;
  137. struct hgsl_mem_node *shadow_ts_node;
  138. uint32_t shadow_ts_flags;
  139. bool is_fe_shadow;
  140. bool in_destroy;
  141. bool destroyed;
  142. struct kref kref;
  143. uint32_t last_ts;
  144. struct hgsl_hsync_timeline *timeline;
  145. uint32_t queued_ts;
  146. bool is_killed;
  147. int tcsr_idx;
  148. struct mutex lock;
  149. struct doorbell_context_queue *dbcq;
  150. uint32_t dbcq_export_id;
  151. };
  152. struct hgsl_priv {
  153. struct qcom_hgsl *dev;
  154. pid_t pid;
  155. struct list_head node;
  156. struct hgsl_hyp_priv_t hyp_priv;
  157. struct mutex lock;
  158. struct list_head mem_mapped;
  159. struct list_head mem_allocated;
  160. int open_count;
  161. atomic64_t total_mem_size;
  162. /* sysfs stuff */
  163. struct kobject kobj;
  164. struct kobject sysfs_client;
  165. struct kobject sysfs_mem_size;
  166. struct dentry *debugfs_client;
  167. struct dentry *debugfs_mem;
  168. struct dentry *debugfs_memtype;
  169. };
  170. static inline bool hgsl_ts32_ge(uint32_t a, uint32_t b)
  171. {
  172. static const uint32_t TIMESTAMP_WINDOW = 0x80000000;
  173. return (a - b) < TIMESTAMP_WINDOW;
  174. }
  175. static inline bool hgsl_ts64_ge(uint64_t a, uint64_t b)
  176. {
  177. static const uint64_t TIMESTAMP_WINDOW = 0x8000000000000000LL;
  178. return (a - b) < TIMESTAMP_WINDOW;
  179. }
  180. static inline bool hgsl_ts_ge(uint64_t a, uint64_t b, bool is64)
  181. {
  182. if (is64)
  183. return hgsl_ts64_ge(a, b);
  184. else
  185. return hgsl_ts32_ge((uint32_t)a, (uint32_t)b);
  186. }
  187. /**
  188. * struct hgsl_hsync_timeline - A sync timeline attached under each hgsl context
  189. * @kref: Refcount to keep the struct alive
  190. * @name: String to describe this timeline
  191. * @fence_context: Used by the fence driver to identify fences belonging to
  192. * this context
  193. * @child_list_head: List head for all fences on this timeline
  194. * @lock: Spinlock to protect this timeline
  195. * @last_ts: Last timestamp when signaling fences
  196. */
  197. struct hgsl_hsync_timeline {
  198. struct kref kref;
  199. struct hgsl_context *context;
  200. char name[HGSL_TIMELINE_NAME_LEN];
  201. u64 fence_context;
  202. spinlock_t lock;
  203. struct list_head fence_list;
  204. unsigned int last_ts;
  205. };
  206. /**
  207. * struct hgsl_hsync_fence - A struct containing a fence and other data
  208. * associated with it
  209. * @fence: The fence struct
  210. * @sync_file: Pointer to the sync file
  211. * @parent: Pointer to the hgsl sync timeline this fence is on
  212. * @child_list: List of fences on the same timeline
  213. * @context_id: hgsl context id
  214. * @ts: Context timestamp that this fence is associated with
  215. */
  216. struct hgsl_hsync_fence {
  217. struct dma_fence fence;
  218. struct sync_file *sync_file;
  219. struct hgsl_hsync_timeline *timeline;
  220. struct list_head child_list;
  221. u32 context_id;
  222. unsigned int ts;
  223. };
  224. struct hgsl_isync_timeline {
  225. struct kref kref;
  226. struct list_head free_list;
  227. char name[HGSL_TIMELINE_NAME_LEN];
  228. int id;
  229. struct hgsl_priv *priv;
  230. struct list_head fence_list;
  231. u64 context;
  232. spinlock_t lock;
  233. u64 last_ts;
  234. u32 flags;
  235. bool is64bits;
  236. };
  237. struct hgsl_isync_fence {
  238. struct dma_fence fence;
  239. struct list_head free_list; /* For free in batch */
  240. struct hgsl_isync_timeline *timeline;
  241. struct list_head child_list;
  242. u64 ts;
  243. };
  244. /* Fence for commands. */
  245. struct hgsl_hsync_fence *hgsl_hsync_fence_create(
  246. struct hgsl_context *context,
  247. uint32_t ts);
  248. int hgsl_hsync_fence_create_fd(struct hgsl_context *context,
  249. uint32_t ts);
  250. int hgsl_hsync_timeline_create(struct hgsl_context *context);
  251. void hgsl_hsync_timeline_signal(struct hgsl_hsync_timeline *timeline,
  252. unsigned int ts);
  253. void hgsl_hsync_timeline_put(struct hgsl_hsync_timeline *timeline);
  254. void hgsl_hsync_timeline_fini(struct hgsl_context *context);
  255. /* Fence for process sync. */
  256. int hgsl_isync_timeline_create(struct hgsl_priv *priv,
  257. uint32_t *timeline_id,
  258. uint32_t flags,
  259. uint64_t initial_ts);
  260. int hgsl_isync_timeline_destroy(struct hgsl_priv *priv, uint32_t id);
  261. void hgsl_isync_fini(struct hgsl_priv *priv);
  262. int hgsl_isync_fence_create(struct hgsl_priv *priv, uint32_t timeline_id,
  263. uint32_t ts, bool ts_is_valid, int *fence_fd);
  264. int hgsl_isync_fence_signal(struct hgsl_priv *priv, uint32_t timeline_id,
  265. int fence_fd);
  266. int hgsl_isync_forward(struct hgsl_priv *priv, uint32_t timeline_id,
  267. uint64_t ts, bool check_owner);
  268. int hgsl_isync_query(struct hgsl_priv *priv, uint32_t timeline_id,
  269. uint64_t *ts);
  270. int hgsl_isync_wait_multiple(struct hgsl_priv *priv, struct hgsl_timeline_wait *param);
  271. #endif /* __HGSL_H_ */