cam_sync_private.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __CAM_SYNC_PRIVATE_H__
  7. #define __CAM_SYNC_PRIVATE_H__
  8. #include <linux/bitmap.h>
  9. #include <linux/videodev2.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/debugfs.h>
  13. #include <media/v4l2-fh.h>
  14. #include <media/v4l2-device.h>
  15. #include <media/v4l2-subdev.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/v4l2-ioctl.h>
  18. #include "cam_sync_api.h"
  19. #include "cam_sync_dma_fence.h"
  20. #if IS_ENABLED(CONFIG_TARGET_SYNX_ENABLE)
  21. #include "cam_sync_synx.h"
  22. #endif
  23. #if IS_REACHABLE(CONFIG_MSM_GLOBAL_SYNX) || IS_ENABLED(CONFIG_TARGET_SYNX_ENABLE)
  24. #include <synx_api.h>
  25. #endif
  26. #ifdef CONFIG_CAM_SYNC_DBG
  27. #define CDBG(fmt, args...) pr_err(fmt, ##args)
  28. #else
  29. #define CDBG(fmt, args...) pr_debug(fmt, ##args)
  30. #endif
  31. #define CAM_SYNC_OBJ_NAME_LEN 64
  32. #define CAM_SYNC_MAX_OBJS 2048
  33. #define CAM_GENERIC_FENCE_BATCH_MAX 10
  34. #define CAM_SYNC_MAX_V4L2_EVENTS 250
  35. #define CAM_SYNC_DEBUG_FILENAME "cam_debug"
  36. #define CAM_SYNC_DEBUG_BASEDIR "cam"
  37. #define CAM_SYNC_DEBUG_BUF_SIZE 32
  38. #define CAM_SYNC_PAYLOAD_WORDS 2
  39. #define CAM_SYNC_NAME "cam_sync"
  40. #define CAM_SYNC_WORKQUEUE_NAME "HIPRIO_SYNC_WORK_QUEUE"
  41. #define CAM_SYNC_TYPE_INDV 0
  42. #define CAM_SYNC_TYPE_GROUP 1
  43. /* Number of monitor table elements */
  44. #define CAM_SYNC_MONITOR_TABLE_SIZE 16
  45. /* Number of monitored objects per table entry */
  46. #define CAM_SYNC_MONITOR_TABLE_ENTRY_SZ (CAM_SYNC_MAX_OBJS / CAM_SYNC_MONITOR_TABLE_SIZE)
  47. #define CAM_SYNC_MONITOR_MAX_ENTRIES 30
  48. #define CAM_SYNC_INC_MONITOR_HEAD(head, ret) \
  49. div_u64_rem(atomic64_add_return(1, head),\
  50. CAM_SYNC_MONITOR_MAX_ENTRIES, (ret))
  51. #define CAM_SYNC_MONITOR_GET_DATA(idx) \
  52. (sync_dev->mon_data[idx / CAM_SYNC_MONITOR_TABLE_ENTRY_SZ] + \
  53. (idx % CAM_SYNC_MONITOR_TABLE_ENTRY_SZ))
  54. #define CAM_GENERIC_MONITOR_TABLE_ENTRY_SZ CAM_SYNC_MONITOR_TABLE_ENTRY_SZ
  55. #define CAM_GENERIC_MONITOR_GET_DATA(mon_data, idx) \
  56. ((mon_data)[idx / CAM_GENERIC_MONITOR_TABLE_ENTRY_SZ] + \
  57. (idx % CAM_GENERIC_MONITOR_TABLE_ENTRY_SZ))
  58. /**
  59. * Feature is enabled by setting BIT(fence_type), this will trigger the fence
  60. * dumps on any error, to explicitly trigger a dump on every fence release
  61. * below BIT(fence_type_dump) needs to be used at the same time
  62. */
  63. #define CAM_GENERIC_FENCE_DUMP_ALWAYS 0x10
  64. #define CAM_GENERIC_FENCE_TYPE_SYNC_OBJ_DUMP \
  65. (CAM_GENERIC_FENCE_TYPE_SYNC_OBJ + (CAM_GENERIC_FENCE_DUMP_ALWAYS))
  66. #define CAM_GENERIC_FENCE_TYPE_DMA_FENCE_DUMP \
  67. (CAM_GENERIC_FENCE_TYPE_DMA_FENCE + (CAM_GENERIC_FENCE_DUMP_ALWAYS))
  68. #define CAM_GENERIC_FENCE_TYPE_SYNX_OBJ_DUMP \
  69. (CAM_GENERIC_FENCE_TYPE_SYNX_OBJ + (CAM_GENERIC_FENCE_DUMP_ALWAYS))
  70. /**
  71. * enum sync_type - Enum to indicate the type of sync object,
  72. * i.e. individual or group.
  73. *
  74. * @SYNC_TYPE_INDV : Object is an individual sync object
  75. * @SYNC_TYPE_GROUP : Object is a group sync object
  76. */
  77. enum sync_type {
  78. SYNC_TYPE_INDV,
  79. SYNC_TYPE_GROUP
  80. };
  81. /**
  82. * enum sync_list_clean_type - Enum to indicate the type of list clean action
  83. * to be peformed, i.e. specific sync ID or all list sync ids.
  84. *
  85. * @SYNC_CLEAN_ONE : Specific object to be cleaned in the list
  86. * @SYNC_CLEAN_ALL : Clean all objects in the list
  87. */
  88. enum sync_list_clean_type {
  89. SYNC_LIST_CLEAN_ONE,
  90. SYNC_LIST_CLEAN_ALL
  91. };
  92. /**
  93. * struct sync_parent_info - Single node of information about a parent
  94. * of a sync object, usually part of the parents linked list
  95. *
  96. * @sync_id : Sync object id of parent
  97. * @list : List member used to append this node to a linked list
  98. */
  99. struct sync_parent_info {
  100. int32_t sync_id;
  101. struct list_head list;
  102. };
  103. /**
  104. * struct sync_parent_info - Single node of information about a child
  105. * of a sync object, usually part of the children linked list
  106. *
  107. * @sync_id : Sync object id of child
  108. * @list : List member used to append this node to a linked list
  109. */
  110. struct sync_child_info {
  111. int32_t sync_id;
  112. struct list_head list;
  113. };
  114. /**
  115. * struct sync_callback_info - Single node of information about a kernel
  116. * callback registered on a sync object
  117. *
  118. * @callback_func : Callback function, registered by client driver
  119. * @cb_data : Callback data, registered by client driver
  120. * @status : Status with which callback will be invoked in client
  121. * @sync_obj : Sync id of the object for which callback is registered
  122. * @workq_scheduled_ts : workqueue scheduled timestamp
  123. * @cb_dispatch_work : Work representing the call dispatch
  124. * @list : List member used to append this node to a linked list
  125. */
  126. struct sync_callback_info {
  127. sync_callback callback_func;
  128. void *cb_data;
  129. int status;
  130. int32_t sync_obj;
  131. ktime_t workq_scheduled_ts;
  132. struct work_struct cb_dispatch_work;
  133. struct list_head list;
  134. };
  135. /**
  136. * struct sync_user_payload - Single node of information about a user space
  137. * payload registered from user space
  138. *
  139. * @payload_data : Payload data, opaque to kernel
  140. * @list : List member used to append this node to a linked list
  141. */
  142. struct sync_user_payload {
  143. uint64_t payload_data[CAM_SYNC_PAYLOAD_WORDS];
  144. struct list_head list;
  145. };
  146. /**
  147. * struct sync_dma_fence_info - DMA fence info associated with this sync obj
  148. *
  149. * @dma_fence_fd : DMA fence fd
  150. * @dma_fence_row_idx : Index of the row corresponding to this dma fence
  151. * in the dma fence table
  152. * @sync_created_with_dma : If sync obj and dma fence are created together
  153. */
  154. struct sync_dma_fence_info {
  155. int32_t dma_fence_fd;
  156. int32_t dma_fence_row_idx;
  157. bool sync_created_with_dma;
  158. };
  159. /**
  160. * enum cam_fence_op - Enum to indicate the type of operation performed
  161. *
  162. * @CAM_FENCE_OP_CREATE : Created obj
  163. * @CAM_FENCE_OP_REGISTER_CB : Successful callback registration
  164. * @CAM_FENCE_OP_SKIP_REGISTER_CB : Callback registration skipped
  165. * @CAM_FENCE_OP_ALREADY_REGISTERED_CB : Callback already registered
  166. * @CAM_FENCE_OP_SIGNAL : Signaled obj
  167. * @CAM_FENCE_OP_UNREGISTER_ON_SIGNAL : Callback unregistered after signaling
  168. * @CAM_FENCE_OP_UNREGISTER_CB : Callback unregistered
  169. * @CAM_FENCE_OP_DESTROY : Destroyed obj
  170. */
  171. enum cam_fence_op {
  172. CAM_FENCE_OP_CREATE,
  173. CAM_FENCE_OP_REGISTER_CB,
  174. CAM_FENCE_OP_SKIP_REGISTER_CB,
  175. CAM_FENCE_OP_ALREADY_REGISTERED_CB,
  176. CAM_FENCE_OP_SIGNAL,
  177. CAM_FENCE_OP_UNREGISTER_ON_SIGNAL,
  178. CAM_FENCE_OP_UNREGISTER_CB,
  179. CAM_FENCE_OP_DESTROY,
  180. };
  181. /**
  182. * struct cam_generic_fence_monitor_entry - Single operation sync data
  183. *
  184. * @timestamp : Timestamp of op
  185. * @op : Operation id
  186. */
  187. struct cam_generic_fence_monitor_entry {
  188. struct timespec64 timestamp;
  189. enum cam_fence_op op;
  190. };
  191. /**
  192. * struct cam_generic_fence_monitor_data - All operations data from current &
  193. * previous use of a fence object
  194. *
  195. * @monitor_head : Executed operations count
  196. * @prev_name : Previous name of this fence obj
  197. * @prev_type : Previous type of this fence obj
  198. * @prev_obj_id : Previous handle of this fence obj
  199. * @prev_sync_id : Previous handle of this fence's associated sync obj
  200. * @prev_remaining : Previous count of remaining children that not been
  201. * signaled
  202. * @prev_state : Previous state (INVALID, ACTIVE, SIGNALED_SUCCESS or
  203. * SIGNALED_ERROR)
  204. * @prev_monitor_head : Previous executed ops count
  205. * @swap_monitor_entries : Flag indicating which entry table should be used
  206. * as current/previous. Used to avoid copying.
  207. * @monitor_entries : Op info entry table
  208. * @prev_monitor_entries : Previous op info entry table
  209. */
  210. struct cam_generic_fence_monitor_data {
  211. atomic64_t monitor_head;
  212. char prev_name[CAM_DMA_FENCE_NAME_LEN];
  213. enum sync_type prev_type;
  214. int32_t prev_obj_id;
  215. int32_t prev_sync_id;
  216. uint32_t prev_remaining;
  217. uint32_t prev_state;
  218. uint64_t prev_monitor_head;
  219. bool swap_monitor_entries;
  220. struct cam_generic_fence_monitor_entry monitor_entries[
  221. CAM_SYNC_MONITOR_MAX_ENTRIES];
  222. struct cam_generic_fence_monitor_entry prev_monitor_entries[
  223. CAM_SYNC_MONITOR_MAX_ENTRIES];
  224. };
  225. /**
  226. * struct cam_generic_fence_monitor_obj_info - Single object monitor info
  227. *
  228. * @name : Name of this fence obj
  229. * @sync_type : Type of this fence obj
  230. * @obj_id : Handle of this fence obj
  231. * @sync_id : Handle of this fence's associated sync obj
  232. * @state : Previous state (INVALID, ACTIVE, SIGNALED_SUCCESS or
  233. * SIGNALED_ERROR)
  234. * @remaining : Count of remaining children that not been signaled
  235. * @ref_cnt : Ref count of the number of usage of the fence.
  236. * @fence_type : Fence type - DMA/Sync/Synx
  237. * @monitor_data : Fence operations data
  238. * @monitor_entries : Op info entry table
  239. * @prev_monitor_entries : Previous op info entry table
  240. */
  241. struct cam_generic_fence_monitor_obj_info {
  242. char *name;
  243. enum sync_type sync_type;
  244. int32_t obj_id;
  245. int32_t sync_id;
  246. uint32_t state;
  247. uint32_t remaining;
  248. uint32_t ref_cnt;
  249. uint32_t fence_type;
  250. struct cam_generic_fence_monitor_data *monitor_data;
  251. struct cam_generic_fence_monitor_entry *monitor_entries;
  252. struct cam_generic_fence_monitor_entry *prev_monitor_entries;
  253. };
  254. /**
  255. * struct sync_synx_obj_info - Synx object info associated with this sync obj
  256. *
  257. * @synx_obj : Synx object handle
  258. * @synx_obj_row_idx : Index of the row corresponding to this synx obj
  259. * in the synx obj table
  260. * @sync_created_with_synx : If sync obj and synx obj are created together
  261. */
  262. struct sync_synx_obj_info {
  263. uint32_t synx_obj;
  264. int32_t synx_obj_row_idx;
  265. bool sync_created_with_synx;
  266. };
  267. /**
  268. * struct sync_table_row - Single row of information about a sync object, used
  269. * for internal book keeping in the sync driver
  270. *
  271. * @name : Optional string representation of the sync object
  272. * @type : Type of the sync object (individual or group)
  273. * @sync_id : Integer id representing this sync object
  274. * @parents_list : Linked list of parents of this sync object
  275. * @children_list : Linked list of children of this sync object
  276. * @state : State (INVALID, ACTIVE, SIGNALED_SUCCESS or
  277. * SIGNALED_ERROR)
  278. * @remaining : Count of remaining children that not been signaled
  279. * @signaled : Completion variable on which block calls will wait
  280. * @callback_list : Linked list of kernel callbacks registered
  281. * @user_payload_list : LInked list of user space payloads registered
  282. * @ref_cnt : ref count of the number of usage of the fence.
  283. * @ext_fence_mask : Mask to indicate associated external fence types
  284. * @dma_fence_info : dma fence info if associated
  285. * @synx_obj_info : synx obj info if associated
  286. */
  287. struct sync_table_row {
  288. char name[CAM_SYNC_OBJ_NAME_LEN];
  289. enum sync_type type;
  290. int32_t sync_id;
  291. /* List of parents, which are merged objects */
  292. struct list_head parents_list;
  293. /* List of children, which constitute the merged object */
  294. struct list_head children_list;
  295. uint32_t state;
  296. uint32_t remaining;
  297. struct completion signaled;
  298. struct list_head callback_list;
  299. struct list_head user_payload_list;
  300. atomic_t ref_cnt;
  301. unsigned long ext_fence_mask;
  302. struct sync_dma_fence_info dma_fence_info;
  303. struct sync_synx_obj_info synx_obj_info;
  304. };
  305. /**
  306. * struct cam_signalable_info - Information for a single sync object that is
  307. * ready to be signaled
  308. *
  309. * @sync_obj : Sync object id of signalable object
  310. * @status : Status with which to signal
  311. * @list : List member used to append this node to a linked list
  312. */
  313. struct cam_signalable_info {
  314. int32_t sync_obj;
  315. uint32_t status;
  316. struct list_head list;
  317. };
  318. /**
  319. * struct sync_device - Internal struct to book keep sync driver details
  320. *
  321. * @vdev : Video device
  322. * @v4l2_dev : V4L2 device
  323. * @sync_table : Table of all sync objects allocated when driver initializes
  324. * @row_spinlocks : Spinlock array, one for each row in the table
  325. * @table_lock : Mutex used to lock the table
  326. * @open_cnt : Count of file open calls made on the sync driver
  327. * @dentry : Debugfs entry
  328. * @work_queue : Work queue used for dispatching kernel callbacks
  329. * @cam_sync_eventq : Event queue used to dispatch user payloads to user space
  330. * @bitmap : Bitmap representation of all sync objects
  331. * @mon_data : Objects monitor data
  332. * @params : Parameters for synx call back registration
  333. * @version : version support
  334. */
  335. struct sync_device {
  336. struct video_device *vdev;
  337. struct v4l2_device v4l2_dev;
  338. struct sync_table_row *sync_table;
  339. spinlock_t row_spinlocks[CAM_SYNC_MAX_OBJS];
  340. struct mutex table_lock;
  341. int open_cnt;
  342. struct dentry *dentry;
  343. struct workqueue_struct *work_queue;
  344. struct v4l2_fh *cam_sync_eventq;
  345. spinlock_t cam_sync_eventq_lock;
  346. DECLARE_BITMAP(bitmap, CAM_SYNC_MAX_OBJS);
  347. struct cam_generic_fence_monitor_data **mon_data;
  348. #if IS_REACHABLE(CONFIG_MSM_GLOBAL_SYNX)
  349. struct synx_register_params params;
  350. #endif
  351. uint32_t version;
  352. };
  353. #endif /* __CAM_SYNC_PRIVATE_H__ */