cam_common_util.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef _CAM_COMMON_UTIL_H_
  7. #define _CAM_COMMON_UTIL_H_
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include "cam_hw_mgr_intf.h"
  11. #define CAM_BITS_MASK_SHIFT(x, mask, shift) (((x) & (mask)) >> shift)
  12. #define CAM_36BIT_INTF_GET_IOVA_BASE(iova) ((iova) >> 8)
  13. #define CAM_36BIT_INTF_GET_IOVA_OFFSET(iova) ((iova) & 0xff)
  14. #define CAM_COMMON_MINI_DUMP_DEV_NUM 6
  15. #define CAM_COMMON_MINI_DUMP_DEV_NAME_LEN 16
  16. #define CAM_COMMON_MINI_DUMP_SIZE 10 * 1024 * 1024
  17. #define CAM_COMMON_HW_DUMP_TAG_MAX_LEN 128
  18. #define CAM_MAX_NUM_CCI_PAYLOAD_BYTES 11
  19. #define CAM_COMMON_EVT_INJECT_MODULE_PARAM_MAX_LENGTH 4096
  20. #define CAM_COMMON_EVT_INJECT_BUFFER_LEN 200
  21. #define CAM_COMMON_EVT_INJECT_BUFFER_ERROR "Buffer_Error"
  22. #define CAM_COMMON_EVT_INJECT_NOTIFY_EVENT "Notify_Event"
  23. #define CAM_COMMON_IFE_NODE "IFE"
  24. #define CAM_COMMON_ICP_NODE "IPE"
  25. #define CAM_COMMON_JPEG_NODE "JPEG"
  26. #define CAM_COMMON_TFE_NODE "TFE"
  27. #define CAM_COMMON_NS_PER_MS 1000000ULL
  28. #define PTR_TO_U64(ptr) ((uint64_t)(uintptr_t)ptr)
  29. #define U64_TO_PTR(ptr) ((void *)(uintptr_t)ptr)
  30. #define CAM_TRIGGER_PANIC(format, args...) panic("CAMERA - " format "\n", ##args)
  31. #define CAM_GET_BOOT_TIMESTAMP(timestamp) ktime_get_boottime_ts64(&(timestamp))
  32. #define CAM_GET_TIMESTAMP(timestamp) ktime_get_real_ts64(&(timestamp))
  33. #define CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts_start, ts_end, diff_microsec) \
  34. ({ \
  35. diff_microsec = 0; \
  36. if (ts_end.tv_nsec >= ts_start.tv_nsec) { \
  37. diff_microsec = \
  38. (ts_end.tv_nsec - ts_start.tv_nsec) / 1000; \
  39. diff_microsec += \
  40. (ts_end.tv_sec - ts_start.tv_sec) * 1000 * 1000; \
  41. } else { \
  42. diff_microsec = \
  43. (ts_end.tv_nsec + \
  44. (1000*1000*1000 - ts_start.tv_nsec)) / 1000; \
  45. diff_microsec += \
  46. (ts_end.tv_sec - ts_start.tv_sec - 1) * 1000 * 1000; \
  47. } \
  48. })
  49. #define CAM_CONVERT_TIMESTAMP_FORMAT(ts, hrs, min, sec, ms) \
  50. ({ \
  51. uint64_t tmp = ((ts).tv_sec); \
  52. (ms) = ((ts).tv_nsec) / 1000000; \
  53. (sec) = do_div(tmp, 60); \
  54. (min) = do_div(tmp, 60); \
  55. (hrs) = do_div(tmp, 24); \
  56. })
  57. #define CAM_COMMON_WAIT_FOR_COMPLETION_TIMEOUT_ERRMSG(complete, timeout_jiffies, module_id, \
  58. fmt, args...) \
  59. ({ \
  60. struct timespec64 start_time, end_time; \
  61. unsigned long rem_jiffies; \
  62. start_time = ktime_to_timespec64(ktime_get()); \
  63. rem_jiffies = cam_common_wait_for_completion_timeout((complete), (timeout_jiffies)); \
  64. if (!rem_jiffies) { \
  65. end_time = ktime_to_timespec64(ktime_get()); \
  66. CAM_ERR(module_id, \
  67. fmt " (timeout: %ums start: timestamp:[%lld.%06lld] end: timestamp:[%lld.%06lld])",\
  68. ##args, jiffies_to_msecs(timeout_jiffies), \
  69. start_time.tv_sec, (start_time.tv_nsec/NSEC_PER_USEC), \
  70. end_time.tv_sec, (end_time.tv_nsec/NSEC_PER_USEC)); \
  71. } \
  72. rem_jiffies; \
  73. })
  74. typedef unsigned long (*cam_common_mini_dump_cb) (void *dst,
  75. unsigned long len, void *priv_data);
  76. /**
  77. * struct cam_common_mini_dump_dev_info
  78. * @dump_cb : address of data dumped
  79. * @name : Name of driver
  80. * @num_devs : Number of device registerd
  81. * @is_registered : Bool to indicate if registered
  82. */
  83. struct cam_common_mini_dump_dev_info {
  84. cam_common_mini_dump_cb dump_cb[CAM_COMMON_MINI_DUMP_DEV_NUM];
  85. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM]
  86. [CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  87. void *priv_data[CAM_COMMON_MINI_DUMP_DEV_NUM];
  88. uint8_t num_devs;
  89. bool is_registered;
  90. };
  91. /**
  92. * struct cam_common_mini_dump_data
  93. * @link : address of data dumped
  94. * @name : Name of driver
  95. * @size : Size dumped
  96. */
  97. struct cam_common_mini_dump_data {
  98. void *waddr[CAM_COMMON_MINI_DUMP_DEV_NUM];
  99. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM][CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  100. unsigned long size[CAM_COMMON_MINI_DUMP_DEV_NUM];
  101. };
  102. typedef int (*cam_common_evt_inject_cb) (void *inject_args);
  103. enum cam_common_evt_inject_str_id_type {
  104. CAM_COMMON_EVT_INJECT_BUFFER_ERROR_TYPE,
  105. CAM_COMMON_EVT_INJECT_NOTIFY_EVENT_TYPE
  106. };
  107. enum cam_common_evt_inject_hw_id {
  108. CAM_COMMON_EVT_INJECT_HW_IFE,
  109. CAM_COMMON_EVT_INJECT_HW_TFE,
  110. CAM_COMMON_EVT_INJECT_HW_ICP,
  111. CAM_COMMON_EVT_INJECT_HW_JPEG,
  112. CAM_COMMON_EVT_INJECT_HW_MAX
  113. };
  114. enum cam_common_evt_inject_common_param_pos {
  115. STRING_ID,
  116. HW_NAME,
  117. DEV_HDL,
  118. REQ_ID,
  119. COMMON_PARAM_MAX
  120. };
  121. enum cam_common_evt_inject_notify_event_pos {
  122. EVT_NOTIFY_TYPE,
  123. EVT_NOTIFY_PARAM_MAX
  124. };
  125. enum cam_evt_inject_buffer_error_event {
  126. SYNC_ERROR_CAUSE,
  127. BUFFER_ERROR_PARAM_MAX
  128. };
  129. enum cam_evt_inject_error_param_pos {
  130. ERR_PARAM_ERR_TYPE,
  131. ERR_PARAM_ERR_CODE,
  132. ERR_PARAM_MAX
  133. };
  134. enum cam_evt_inject_node_param_pos {
  135. EVENT_TYPE,
  136. EVENT_CAUSE,
  137. NODE_PARAM_MAX,
  138. };
  139. enum cam_evt_inject_pf_params_pos {
  140. PF_PARAM_CTX_FOUND,
  141. PF_PARAM_MAX
  142. };
  143. /**
  144. * struct cam_common_evt_inject_data
  145. * @buf_done_data: buf done data
  146. * @evt_params : event params for the injected event
  147. */
  148. struct cam_common_evt_inject_data {
  149. void *buf_done_data;
  150. struct cam_hw_inject_evt_param *evt_params;
  151. };
  152. typedef int (*cam_common_evt_inject_ops) (void *cam_ctx,
  153. struct cam_common_evt_inject_data *inject_evt);
  154. /**
  155. * struct cam_common_inject_evt_param
  156. * @evt_params : injection event params
  157. * @dev_hdl : device handle to match with ctx's dev_hdl
  158. * @hw_id : hw to be injected with the event
  159. */
  160. struct cam_common_inject_evt_param {
  161. struct list_head list;
  162. struct cam_hw_inject_evt_param evt_params;
  163. int32_t dev_hdl;
  164. uint8_t hw_id;
  165. };
  166. /**
  167. * struct cam_common_inject_evt_info
  168. * @evt_inject_cb : address of callback
  169. * @active_err_ctx_list: list containing active evt inject requests
  170. * @num_hw_registered : number of callbacks registered
  171. * @is_list_initialised: bool to check init for evt_inject list
  172. */
  173. struct cam_common_inject_evt_info {
  174. cam_common_evt_inject_cb evt_inject_cb[CAM_COMMON_EVT_INJECT_HW_MAX];
  175. struct list_head active_evt_ctx_list;
  176. uint8_t num_hw_registered;
  177. bool is_list_initialised;
  178. };
  179. /**
  180. * struct cam_common_hw_dump_args
  181. * @req_id : request id
  182. * @cpu_addr : address where dumping will start from
  183. * @buf_len : length of buffer where data is being dumped to
  184. * @offset : buffer offset from cpu_addr after each item dump
  185. * @ctxt_to_hw_map : context to hw map
  186. * @is_dump_all : flag to indicate if all information or just bw/clk rate
  187. * @
  188. */
  189. struct cam_common_hw_dump_args {
  190. uint64_t req_id;
  191. uintptr_t cpu_addr;
  192. size_t buf_len;
  193. size_t offset;
  194. void *ctxt_to_hw_map;
  195. bool is_dump_all;
  196. };
  197. /**
  198. * struct cam_common_hw_dump_header
  199. * @tag : string used by the parser to call parse functions
  200. * @size : size of the header in the buffer
  201. * @word_size : word size of the header
  202. * @
  203. */
  204. struct cam_common_hw_dump_header {
  205. uint8_t tag[CAM_COMMON_HW_DUMP_TAG_MAX_LEN];
  206. uint64_t size;
  207. uint32_t word_size;
  208. };
  209. /**
  210. * @brief release all event inject params in the g_inject_evt_info
  211. * for a specific dev_hdl
  212. * @dev_hdl: device handle to which the evt inject params belong to
  213. */
  214. void cam_common_release_evt_params(int32_t dev_hdl);
  215. /**
  216. * cam_common_util_get_string_index()
  217. *
  218. * @brief Match the string from list of strings to return
  219. * matching index
  220. *
  221. * @strings: Pointer to list of strings
  222. * @num_strings: Number of strings in 'strings'
  223. * @matching_string: String to match
  224. * @index: Pointer to index to return matching index
  225. *
  226. * @return: 0 for success
  227. * -EINVAL for Fail
  228. */
  229. int cam_common_util_get_string_index(const char **strings,
  230. uint32_t num_strings, const char *matching_string, uint32_t *index);
  231. /**
  232. * cam_common_util_remove_duplicate_arr()
  233. *
  234. * @brief Move all the unique integers to the start of
  235. * the array and return the number of unique integers
  236. *
  237. * @array: Pointer to the first integer of array
  238. * @num: Number of elements in array
  239. *
  240. * @return: Number of unique integers in array
  241. */
  242. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  243. uint32_t num);
  244. /**
  245. * cam_common_wait_for_completion_timeout()
  246. *
  247. * @brief common interface to implement wait for completion
  248. * for slow environment like presil, single debug
  249. * timeout variable can take care
  250. *
  251. * @complete: Pointer to the first integer of array
  252. * @timeout_jiffies: Timeout value in jiffie
  253. *
  254. * @return: Remaining jiffies, non-zero for success, zero
  255. * in case of failure
  256. */
  257. unsigned long cam_common_wait_for_completion_timeout(
  258. struct completion *complete,
  259. unsigned long timeout_jiffies);
  260. /**
  261. * cam_common_read_poll_timeout()
  262. *
  263. * @brief common interface to read poll timeout
  264. *
  265. * @addr: Address of IO register
  266. * @delay: Delay interval of poll
  267. * @timeout: Timeout for poll
  268. * @mask: Mask to be checked
  269. * @check_val: Value to be compared to break poll
  270. * @status: Status of register of IO
  271. *
  272. * @return: 0 if success and negative if fail
  273. * */
  274. int cam_common_read_poll_timeout(
  275. void __iomem *addr,
  276. unsigned long delay,
  277. unsigned long timeout,
  278. uint32_t mask,
  279. uint32_t check_val,
  280. uint32_t *status);
  281. /**
  282. * cam_common_modify_timer()
  283. *
  284. * @brief common interface to modify timer,
  285. *
  286. * @timer: reference to system timer
  287. * @timeout_val: timeout value for timer
  288. *
  289. * @return: 0 if success and negative if fail
  290. */
  291. int cam_common_modify_timer(struct timer_list *timer, int32_t timeout_val);
  292. /**
  293. * cam_common_util_thread_switch_delay_detect()
  294. *
  295. * @brief Detect if there is any scheduling delay
  296. *
  297. * @wq_name: workq name
  298. * @state: either schedule or execution
  299. * @cb: callback scheduled or executed
  300. * @scheduled_time: Time when workq or tasklet was scheduled
  301. * @threshold: Threshold time
  302. *
  303. */
  304. void cam_common_util_thread_switch_delay_detect(char *wq_name, const char *state,
  305. void *cb, ktime_t scheduled_time, uint32_t threshold);
  306. /**
  307. * cam_common_register_mini_dump_cb()
  308. *
  309. * @brief common interface to register mini dump cb
  310. *
  311. * @mini_dump_cb: Pointer to the mini_dump_cb
  312. * @name: name of device registering
  313. *
  314. * @return: 0 if success in register non-zero if failes
  315. */
  316. #if IS_REACHABLE(CONFIG_QCOM_VA_MINIDUMP)
  317. int cam_common_register_mini_dump_cb(
  318. cam_common_mini_dump_cb mini_dump_cb, uint8_t *dev_name, void *priv_data);
  319. #else
  320. static inline int cam_common_register_mini_dump_cb(
  321. cam_common_mini_dump_cb mini_dump_cb,
  322. uint8_t *dev_name, void *priv_data)
  323. {
  324. return 0;
  325. }
  326. #endif
  327. /**
  328. * cam_common_user_dump_clock()
  329. *
  330. * @brief Handles clock rate dump
  331. *
  332. * @dump_struct: Struct holding dump info
  333. * @addr_ptr: Pointer to buffer address pointer
  334. */
  335. void *cam_common_user_dump_clock(
  336. void *dump_struct,
  337. uint8_t *addr_ptr);
  338. /**
  339. * cam_common_user_dump_helper()
  340. *
  341. * @brief Handles buffer addressing and dumping for user dump
  342. *
  343. * @cmd_args: Holds cam_common_hw_dump_args pointer
  344. * @func: Function pointer for dump function
  345. * @dump_struct: Struct holding dump info
  346. * @size: Size_t value used for header word size
  347. * @tag: Tag for header, used by parser
  348. * @...: Variadic arguments, appended to tag if given
  349. */
  350. int cam_common_user_dump_helper(
  351. void *cmd_args,
  352. void *(*func)(void *, uint8_t *),
  353. void *dump_struct,
  354. size_t size,
  355. const char *tag,
  356. ...);
  357. /**
  358. * cam_common_register_evt_inject_cb()
  359. *
  360. * @brief common interface to register evt inject cb
  361. *
  362. * @evt_inject_cb: Pointer to evt_inject_cb
  363. * @hw_id: HW id of the HW driver registering
  364. *
  365. * @return: 0 if success in register non-zero if failes
  366. */
  367. int cam_common_register_evt_inject_cb(
  368. cam_common_evt_inject_cb evt_inject_cb,
  369. enum cam_common_evt_inject_hw_id hw_id);
  370. #endif /* _CAM_COMMON_UTIL_H_ */