sde_fence.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  5. */
  6. #ifndef _SDE_FENCE_H_
  7. #define _SDE_FENCE_H_
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/mutex.h>
  11. #include <linux/soc/qcom/msm_hw_fence.h>
  12. #ifndef CHAR_BIT
  13. #define CHAR_BIT 8 /* define this if limits.h not available */
  14. #endif
  15. #define HW_FENCE_TRIGGER_SEL_CMD_MODE 0x0
  16. #define HW_FENCE_TRIGGER_SEL_PROG_LINE_COUNT 0x1
  17. #define HW_FENCE_TRIGGER_SEL_VID_MODE 0x2
  18. #define SDE_INPUT_HW_FENCE_TIMESTAMP BIT(0)
  19. #define SDE_OUTPUT_HW_FENCE_TIMESTAMP BIT(1)
  20. #define SDE_FENCE_NAME_SIZE 24
  21. #define MAX_SDE_HFENCE_OUT_SIGNAL_PING_PONG 2
  22. /**
  23. * enum sde_fence_error_state - fence error state handled in _sde_fence_trigger
  24. * @NO_ERROR: no fence error
  25. * @SET_ERROR_ONLY_CMD_RELEASE: cmd panel release fence error state
  26. * @SET_ERROR_ONLY_CMD_RETIRE: cmd panel retire fence error state
  27. * @SET_ERROR_ONLY_VID: vid panel fence error state
  28. * @HANDLE_OUT_OF_ORDER: vid panel out of order handle
  29. */
  30. enum sde_fence_error_state {
  31. NO_ERROR,
  32. SET_ERROR_ONLY_CMD_RELEASE,
  33. SET_ERROR_ONLY_CMD_RETIRE,
  34. SET_ERROR_ONLY_VID,
  35. HANDLE_OUT_OF_ORDER,
  36. };
  37. /**
  38. * sde_fence_error_ctx - reserve frame info for fence error handing
  39. * @last_good_frame_fence_seqno: last good frame fence seqno
  40. * @curr_frame_fence_seqno: currently frame fence seqno
  41. * @fence_error_status: fence error status
  42. * @sde_fence_error_state: fence error state
  43. */
  44. struct sde_fence_error_ctx {
  45. u32 last_good_frame_fence_seqno;
  46. u32 curr_frame_fence_seqno;
  47. int fence_error_status;
  48. enum sde_fence_error_state fence_error_state;
  49. };
  50. /**
  51. * struct sde_fence_context - release/retire fence context/timeline structure
  52. * @commit_count: Number of detected commits since bootup
  53. * @done_count: Number of completed commits since bootup
  54. * @drm_id: ID number of owning DRM Object
  55. * @ref: kref counter on timeline
  56. * @lock: spinlock for fence counter protection
  57. * @list_lock: spinlock for timeline protection
  58. * @context: fence context
  59. * @sde_fence_error_ctx: sde fence error context
  60. * @list_head: fence list to hold all the fence created on this context
  61. * @name: name of fence context/timeline
  62. */
  63. struct sde_fence_context {
  64. unsigned int commit_count;
  65. unsigned int done_count;
  66. uint32_t drm_id;
  67. struct kref kref;
  68. spinlock_t lock;
  69. spinlock_t list_lock;
  70. u64 context;
  71. struct sde_fence_error_ctx sde_fence_error_ctx;
  72. struct list_head fence_list_head;
  73. char name[SDE_FENCE_NAME_SIZE];
  74. };
  75. /**
  76. * struct sde_hw_fence_error_cb_data - struct passed back in fence error callback
  77. * @ctl_idx: control path index
  78. * @sde_kms: handle to sde_kms
  79. */
  80. struct sde_hw_fence_error_cb_data {
  81. int ctl_idx;
  82. struct sde_kms *sde_kms;
  83. };
  84. /**
  85. * enum sde_fence_event - sde fence event as hint fence operation
  86. * @SDE_FENCE_SIGNAL: Signal the fence cleanly with current timeline
  87. * @SDE_FENCE_RESET_TIMELINE: Reset timeline of the fence context
  88. * @SDE_FENCE_SIGNAL: Signal the fence but indicate error throughfence status
  89. */
  90. enum sde_fence_event {
  91. SDE_FENCE_SIGNAL,
  92. SDE_FENCE_RESET_TIMELINE,
  93. SDE_FENCE_SIGNAL_ERROR
  94. };
  95. /**
  96. * struct sde_hw_fence_data - contains the information of each display-client of the hw-fences
  97. * to communicate with the fence controller.
  98. * @client_id: client_id enum for the display driver.
  99. * @hw_fence_client_id: client_id enum for the hw-fence driver.
  100. * @mem_descriptor: memory descriptor with the hfi for the rx/tx queues mapping.
  101. * @txq_tx_wm_va: pointer to store virtual address of tx_wm
  102. * @txq_wr_ptr_pa: pointer to store physical address of write_ptr
  103. * @ipcc_in_client: ipcc client triggering the signal: IN_CLIENT (APPS) -> DPU
  104. * @ipcc_in_signal: ipcc signal triggered from client to dpu: IN_SIGNAL (APPS) -> DPU
  105. * @ipcc_out_signal_pp: output signal from dpu to fctl, ping-pongs between two signals
  106. * @ipcc_out_signal_pp_idx: index of the output signal ping-pong
  107. * @ipcc_out_client: destination client id (APPS for the FCTL)
  108. * @ipcc_this_client: ipcc dpu client id (For Waipio: APPS, For Kailua: DPU HW)
  109. * @dma_context: per client dma context used to create join fences
  110. * @hw_fence_array_seqno: per-client seq number counter for join fences
  111. * @sde_hw_fence_error_cb_data: data needed for hw fence cb function.
  112. */
  113. struct sde_hw_fence_data {
  114. int client_id;
  115. enum hw_fence_client_id hw_fence_client_id;
  116. void *hw_fence_handle;
  117. struct msm_hw_fence_mem_addr mem_descriptor;
  118. u32 *txq_tx_wm_va;
  119. u32 *txq_wr_ptr_pa;
  120. u32 ipcc_in_client;
  121. u32 ipcc_in_signal;
  122. u32 ipcc_out_signal_pp[MAX_SDE_HFENCE_OUT_SIGNAL_PING_PONG];
  123. u32 ipcc_out_signal_pp_idx;
  124. u32 ipcc_out_client;
  125. u32 ipcc_this_client;
  126. u64 dma_context;
  127. u32 hw_fence_array_seqno;
  128. struct sde_hw_fence_error_cb_data sde_hw_fence_error_cb_data;
  129. };
  130. #if IS_ENABLED(CONFIG_SYNC_FILE)
  131. /**
  132. * sde_sync_get - Query sync fence object from a file handle
  133. *
  134. * On success, this function also increments the refcount of the sync fence
  135. *
  136. * @fd: Integer sync fence handle
  137. *
  138. * Return: Pointer to sync fence object, or NULL
  139. */
  140. void *sde_sync_get(uint64_t fd);
  141. /**
  142. * sde_sync_put - Releases a sync fence object acquired by @sde_sync_get
  143. *
  144. * This function decrements the sync fence's reference count; the object will
  145. * be released if the reference count goes to zero.
  146. *
  147. * @fence: Pointer to sync fence
  148. */
  149. void sde_sync_put(void *fence);
  150. /**
  151. * sde_sync_wait - Query sync fence object from a file handle
  152. *
  153. * @fence: Pointer to sync fence
  154. * @timeout_ms: Time to wait, in milliseconds. Waits forever if timeout_ms < 0
  155. * @error_status: status of fence
  156. *
  157. * Return:
  158. * Zero if timed out
  159. * -ERESTARTSYS if wait interrupted
  160. * remaining jiffies in all other success cases.
  161. */
  162. signed long sde_sync_wait(void *fence, long timeout_ms, int *error_status);
  163. /**
  164. * sde_sync_get_name_prefix - get integer representation of fence name prefix
  165. *
  166. * @fence: Pointer to opaque fence structure
  167. *
  168. * Return: 32-bit integer containing first 4 characters of fence name,
  169. * big-endian notation
  170. */
  171. uint32_t sde_sync_get_name_prefix(void *fence);
  172. /**
  173. * sde_fence_init - initialize fence object
  174. *
  175. * @drm_id: ID number of owning DRM Object
  176. * @name: Timeline name
  177. *
  178. * Returns: fence context object on success
  179. */
  180. struct sde_fence_context *sde_fence_init(const char *name,
  181. uint32_t drm_id);
  182. /**
  183. * sde_fence_hw_fence_init - initialize hw-fence clients
  184. *
  185. * @hw_ctl: hw ctl client to init.
  186. * @sde_kms: used for hw fence error cb register.
  187. * @use_ipcc: boolean to indicate if hw should use dpu ipcc signals.
  188. * @mmu: mmu to map memory for queues
  189. *
  190. * Returns: Zero on success, otherwise returns an error code.
  191. */
  192. int sde_hw_fence_init(struct sde_hw_ctl *hw_ctl, struct sde_kms *sde_kms, bool use_dpu_ipcc,
  193. struct msm_mmu *mmu);
  194. /**
  195. * sde_fence_hw_fence_deinit - deinitialize hw-fence clients
  196. *
  197. * @hw_ctl: hw ctl client to init.
  198. */
  199. void sde_hw_fence_deinit(struct sde_hw_ctl *hw_ctl);
  200. /**
  201. * sde_fence_register_hw_fences_wait - registers dpu-client for wait on hw fence or fences
  202. *
  203. * @hw_ctl: hw ctl client used to register for wait.
  204. * @fences: list of dma-fences that have hw-fence support to wait-on
  205. * @num_fences: number of fences in the above list
  206. *
  207. * Returns: Zero on success, otherwise returns an error code.
  208. */
  209. int sde_fence_register_hw_fences_wait(struct sde_hw_ctl *hw_ctl, struct dma_fence **fences,
  210. u32 num_fences);
  211. /**
  212. * sde_fence_output_hw_fence_dir_write_init - update addr, mask and size for output fence dir write
  213. * @hw_ctl: hw ctl client to init dir write regs for
  214. */
  215. void sde_fence_output_hw_fence_dir_write_init(struct sde_hw_ctl *hw_ctl);
  216. /**
  217. * sde_fence_update_hw_fences_txq - updates the hw-fence txq with the list of hw-fences to signal
  218. * upon triggering the ipcc signal.
  219. *
  220. * @ctx: sde fence context
  221. * @vid_mode: is video-mode update
  222. * @line_count: prog line count value, must be non-zero
  223. *
  224. * Returns: Zero on success, otherwise returns an error code.
  225. */
  226. int sde_fence_update_hw_fences_txq(struct sde_fence_context *ctx, bool vid_mode, u32 line_count,
  227. u32 debugfs_hw_fence);
  228. /**
  229. * sde_fence_update_input_hw_fence_signal - updates input-fence ipcc signal in dpu and enables
  230. * hw-fences for the ctl.
  231. *
  232. * @ctl: hw ctl to update the input-fence and enable hw-fences
  233. * @debugfs_hw_fence: hw-fence timestamp debugfs value
  234. * @hw_mdp: pointer to hw_mdp to get timestamp registers
  235. * @disable: bool to indicate if we should disable hw-fencing for this commit
  236. *
  237. * Returns: Zero on success, otherwise returns an error code.
  238. */
  239. int sde_fence_update_input_hw_fence_signal(struct sde_hw_ctl *ctl, u32 debugfs_hw_fence,
  240. struct sde_hw_mdp *hw_mdp, bool disable);
  241. /**
  242. * sde_fence_error_ctx_update - update fence_error_state and fence_error_status in
  243. * sde_fence_error_ctx.
  244. *
  245. * @ctx: sde_fence_context
  246. * @input_fence_status: input fence status, negative if input fence error
  247. * @sde_fence_error_state: sde fence error state
  248. */
  249. void sde_fence_error_ctx_update(struct sde_fence_context *ctx, int input_fence_status,
  250. enum sde_fence_error_state sde_fence_error_state);
  251. /**
  252. * sde_fence_deinit - deinit fence container
  253. * @fence: Pointer fence container
  254. */
  255. void sde_fence_deinit(struct sde_fence_context *fence);
  256. /**
  257. * sde_fence_prepare - prepare to return fences for current commit
  258. * @fence: Pointer fence container
  259. * Returns: Zero on success
  260. */
  261. void sde_fence_prepare(struct sde_fence_context *fence);
  262. /**
  263. * sde_fence_create - create output fence object
  264. * @fence: Pointer fence container
  265. * @val: Pointer to output value variable, fence fd will be placed here
  266. * @offset: Fence signal commit offset, e.g., +1 to signal on next commit
  267. * @hw_ctl: Ctl for hw fences
  268. * Returns: Zero on success
  269. */
  270. int sde_fence_create(struct sde_fence_context *fence, uint64_t *val,
  271. uint32_t offset, struct sde_hw_ctl *hw_ctl);
  272. /**
  273. * sde_fence_signal - advance fence timeline to signal outstanding fences
  274. * @fence: Pointer fence container
  275. * @ts: fence timestamp
  276. * @fence_event: fence event to indicate nature of fence signal.
  277. * @hw_ctl: ctl to signal fences for the timeline rest event
  278. */
  279. void sde_fence_signal(struct sde_fence_context *fence, ktime_t ts,
  280. enum sde_fence_event fence_event, struct sde_hw_ctl *hw_ctl);
  281. /**
  282. * sde_fence_timeline_status - prints fence timeline status
  283. * @fence: Pointer fence container
  284. * @drm_obj Pointer to drm object associated with fence timeline
  285. */
  286. void sde_fence_timeline_status(struct sde_fence_context *ctx,
  287. struct drm_mode_object *drm_obj);
  288. /**
  289. * sde_fence_timeline_dump - utility to dump fence list info in debugfs node
  290. * @fence: Pointer fence container
  291. * @drm_obj: Pointer to drm object associated with fence timeline
  292. * @s: used to writing on debugfs node
  293. */
  294. void sde_debugfs_timeline_dump(struct sde_fence_context *ctx,
  295. struct drm_mode_object *drm_obj, struct seq_file **s);
  296. /**
  297. * sde_fence_timeline_status - dumps fence timeline in debugfs node
  298. * @fence: Pointer fence container
  299. * @s: used to writing on debugfs node
  300. */
  301. void sde_fence_list_dump(struct dma_fence *fence, struct seq_file **s);
  302. /**
  303. * sde_fence_dump - dumps fence info for specified fence
  304. * @fence: Pointer to fence to dump info for
  305. */
  306. void sde_fence_dump(struct dma_fence *fence);
  307. #else
  308. static inline void *sde_sync_get(uint64_t fd)
  309. {
  310. return NULL;
  311. }
  312. static inline void sde_sync_put(void *fence)
  313. {
  314. }
  315. static inline signed long sde_sync_wait(void *fence, long timeout_ms, int *error_status)
  316. {
  317. return 0;
  318. }
  319. static inline uint32_t sde_sync_get_name_prefix(void *fence)
  320. {
  321. return 0x0;
  322. }
  323. static inline struct sde_fence_context *sde_fence_init(const char *name,
  324. uint32_t drm_id)
  325. {
  326. /* do nothing */
  327. return NULL;
  328. }
  329. static inline void sde_fence_deinit(struct sde_fence_context *fence)
  330. {
  331. /* do nothing */
  332. }
  333. static inline int sde_fence_get(struct sde_fence_context *fence, uint64_t *val)
  334. {
  335. return -EINVAL;
  336. }
  337. static inline void sde_fence_signal(struct sde_fence_context *fence,
  338. ktime_t ts, bool reset_timeline)
  339. {
  340. /* do nothing */
  341. }
  342. static inline void sde_fence_prepare(struct sde_fence_context *fence)
  343. {
  344. /* do nothing */
  345. }
  346. static inline int sde_fence_create(struct sde_fence_context *fence,
  347. uint64_t *val, uint32_t offset)
  348. {
  349. return 0;
  350. }
  351. static inline void sde_fence_timeline_status(struct sde_fence_context *ctx,
  352. struct drm_mode_object *drm_obj);
  353. {
  354. /* do nothing */
  355. }
  356. void sde_debugfs_timeline_dump(struct sde_fence_context *ctx,
  357. struct drm_mode_object *drm_obj, struct seq_file **s)
  358. {
  359. /* do nothing */
  360. }
  361. void sde_fence_list_dump(struct dma_fence *fence, struct seq_file **s)
  362. {
  363. /* do nothing */
  364. }
  365. void sde_fence_dump(struct dma_fence *fence)
  366. {
  367. /* do nothing */
  368. }
  369. #endif /* IS_ENABLED(CONFIG_SW_SYNC) */
  370. #endif /* _SDE_FENCE_H_ */