cam_common_util.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 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. #define CAM_BITS_MASK_SHIFT(x, mask, shift) (((x) & (mask)) >> shift)
  11. #define CAM_36BIT_INTF_GET_IOVA_BASE(iova) ((iova) >> 8)
  12. #define CAM_36BIT_INTF_GET_IOVA_OFFSET(iova) ((iova) & 0xff)
  13. #define CAM_COMMON_MINI_DUMP_DEV_NUM 6
  14. #define CAM_COMMON_MINI_DUMP_DEV_NAME_LEN 16
  15. #define CAM_COMMON_MINI_DUMP_SIZE 10 * 1024 * 1024
  16. #define CAM_COMMON_HW_DUMP_TAG_MAX_LEN 64
  17. #define CAM_MAX_NUM_CCI_PAYLOAD_BYTES 11
  18. #define CAM_COMMON_ERR_MODULE_PARAM_MAX_LENGTH 4096
  19. #define CAM_COMMON_ERR_INJECT_BUFFER_LEN 200
  20. #define CAM_COMMON_ERR_INJECT_DEV_MAX 5
  21. #define CAM_COMMON_ERR_INJECT_PARAM_NUM 5
  22. #define CAM_COMMON_IFE_NODE "IFE"
  23. #define CAM_COMMON_ICP_NODE "IPE"
  24. #define CAM_COMMON_JPEG_NODE "JPEG"
  25. #define PTR_TO_U64(ptr) ((uint64_t)(uintptr_t)ptr)
  26. #define U64_TO_PTR(ptr) ((void *)(uintptr_t)ptr)
  27. #define CAM_TRIGGER_PANIC(format, args...) panic("CAMERA - " format "\n", ##args)
  28. #define CAM_GET_TIMESTAMP(timestamp) ktime_get_real_ts64(&(timestamp))
  29. #define CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts_start, ts_end, diff_microsec) \
  30. ({ \
  31. diff_microsec = 0; \
  32. if (ts_end.tv_nsec >= ts_start.tv_nsec) { \
  33. diff_microsec = \
  34. (ts_end.tv_nsec - ts_start.tv_nsec) / 1000; \
  35. diff_microsec += \
  36. (ts_end.tv_sec - ts_start.tv_sec) * 1000 * 1000; \
  37. } else { \
  38. diff_microsec = \
  39. (ts_end.tv_nsec + \
  40. (1000*1000*1000 - ts_start.tv_nsec)) / 1000; \
  41. diff_microsec += \
  42. (ts_end.tv_sec - ts_start.tv_sec - 1) * 1000 * 1000; \
  43. } \
  44. })
  45. #define CAM_CONVERT_TIMESTAMP_FORMAT(ts, hrs, min, sec, ms) \
  46. ({ \
  47. uint64_t tmp = ((ts).tv_sec); \
  48. (ms) = ((ts).tv_nsec) / 1000000; \
  49. (sec) = do_div(tmp, 60); \
  50. (min) = do_div(tmp, 60); \
  51. (hrs) = do_div(tmp, 24); \
  52. })
  53. #define CAM_COMMON_WAIT_FOR_COMPLETION_TIMEOUT_ERRMSG(complete, timeout_jiffies, module_id, \
  54. fmt, args...) \
  55. ({ \
  56. struct timespec64 start_time, end_time; \
  57. unsigned long rem_jiffies; \
  58. start_time = ktime_to_timespec64(ktime_get()); \
  59. rem_jiffies = cam_common_wait_for_completion_timeout((complete), (timeout_jiffies)); \
  60. if (!rem_jiffies) { \
  61. end_time = ktime_to_timespec64(ktime_get()); \
  62. CAM_ERR(module_id, fmt " (timeout: %ums start: %llu.%llu end: %llu.%llu)", \
  63. ##args, jiffies_to_msecs(timeout_jiffies), \
  64. start_time.tv_sec, (start_time.tv_nsec/NSEC_PER_USEC), \
  65. end_time.tv_sec, (end_time.tv_nsec/NSEC_PER_USEC)); \
  66. } \
  67. rem_jiffies; \
  68. })
  69. typedef unsigned long (*cam_common_mini_dump_cb) (void *dst, unsigned long len);
  70. /**
  71. * struct cam_common_mini_dump_dev_info
  72. * @dump_cb : address of data dumped
  73. * @name : Name of driver
  74. * @num_devs : Number of device registerd
  75. * @is_registered : Bool to indicate if registered
  76. */
  77. struct cam_common_mini_dump_dev_info {
  78. cam_common_mini_dump_cb dump_cb[CAM_COMMON_MINI_DUMP_DEV_NUM];
  79. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM]
  80. [CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  81. uint8_t num_devs;
  82. bool is_registered;
  83. };
  84. /**
  85. * struct cam_common_mini_dump_data
  86. * @link : address of data dumped
  87. * @name : Name of driver
  88. * @size : Size dumped
  89. */
  90. struct cam_common_mini_dump_data {
  91. void *waddr[CAM_COMMON_MINI_DUMP_DEV_NUM];
  92. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM][CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  93. unsigned long size[CAM_COMMON_MINI_DUMP_DEV_NUM];
  94. };
  95. typedef int (*cam_common_err_inject_cb) (void *err_param);
  96. int cam_common_release_err_params(uint64_t dev_hdl);
  97. enum cam_common_err_inject_hw_id {
  98. CAM_COMMON_ERR_INJECT_HW_ISP,
  99. CAM_COMMON_ERR_INJECT_HW_ICP,
  100. CAM_COMMON_ERR_INJECT_HW_JPEG,
  101. CAM_COMMON_ERR_INJECT_HW_MAX
  102. };
  103. enum cam_common_err_inject_input_param_pos {
  104. HW_NAME = 0,
  105. REQ_ID,
  106. ERR_TYPE,
  107. ERR_CODE,
  108. DEV_HDL
  109. };
  110. /**
  111. * @req_id : req id for err to be injected
  112. * @dev_hdl : dev_hdl for the context
  113. * @err_type: error type for error request
  114. * @err_code: error code for error request
  115. * @hw_id : hw id representing hw nodes of type cam_common_err_inject_hw_id
  116. */
  117. struct cam_err_inject_param {
  118. struct list_head list;
  119. uint64_t req_id;
  120. uint64_t dev_hdl;
  121. uint32_t err_type;
  122. uint32_t err_code;
  123. uint8_t hw_id;
  124. };
  125. /**
  126. * struct cam_common_err_inject_info
  127. * @err_inject_cb : address of callback
  128. * @active_err_ctx_list: list containing active err inject requests
  129. * @num_hw_registered : number of callbacks registered
  130. * @is_list_initialised: bool to check init for err_inject list
  131. */
  132. struct cam_common_err_inject_info {
  133. cam_common_err_inject_cb err_inject_cb[CAM_COMMON_ERR_INJECT_HW_MAX];
  134. struct list_head active_err_ctx_list;
  135. uint8_t num_hw_registered;
  136. bool is_list_initialised;
  137. };
  138. /**
  139. * struct cam_common_hw_dump_args
  140. * @req_id : request id
  141. * @cpu_addr : address where dumping will start from
  142. * @buf_len : length of buffer where data is being dumped to
  143. * @offset : buffer offset from cpu_addr after each item dump
  144. * @ctxt_to_hw_map : context to hw map
  145. * @is_dump_all : flag to indicate if all information or just bw/clk rate
  146. * @
  147. */
  148. struct cam_common_hw_dump_args {
  149. uint64_t req_id;
  150. uintptr_t cpu_addr;
  151. size_t buf_len;
  152. size_t offset;
  153. void *ctxt_to_hw_map;
  154. bool is_dump_all;
  155. };
  156. /**
  157. * struct cam_common_hw_dump_header
  158. * @tag : string used by the parser to call parse functions
  159. * @size : size of the header in the buffer
  160. * @word_size : word size of the header
  161. * @
  162. */
  163. struct cam_common_hw_dump_header {
  164. uint8_t tag[CAM_COMMON_HW_DUMP_TAG_MAX_LEN];
  165. uint64_t size;
  166. uint32_t word_size;
  167. };
  168. /**
  169. * cam_common_util_get_string_index()
  170. *
  171. * @brief Match the string from list of strings to return
  172. * matching index
  173. *
  174. * @strings: Pointer to list of strings
  175. * @num_strings: Number of strings in 'strings'
  176. * @matching_string: String to match
  177. * @index: Pointer to index to return matching index
  178. *
  179. * @return: 0 for success
  180. * -EINVAL for Fail
  181. */
  182. int cam_common_util_get_string_index(const char **strings,
  183. uint32_t num_strings, const char *matching_string, uint32_t *index);
  184. /**
  185. * cam_common_util_remove_duplicate_arr()
  186. *
  187. * @brief Move all the unique integers to the start of
  188. * the array and return the number of unique integers
  189. *
  190. * @array: Pointer to the first integer of array
  191. * @num: Number of elements in array
  192. *
  193. * @return: Number of unique integers in array
  194. */
  195. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  196. uint32_t num);
  197. /**
  198. * cam_common_wait_for_completion_timeout()
  199. *
  200. * @brief common interface to implement wait for completion
  201. * for slow environment like presil, single debug
  202. * timeout variable can take care
  203. *
  204. * @complete: Pointer to the first integer of array
  205. * @timeout_jiffies: Timeout value in jiffie
  206. *
  207. * @return: Remaining jiffies, non-zero for success, zero
  208. * in case of failure
  209. */
  210. unsigned long cam_common_wait_for_completion_timeout(
  211. struct completion *complete,
  212. unsigned long timeout_jiffies);
  213. /**
  214. * cam_common_read_poll_timeout()
  215. *
  216. * @brief common interface to read poll timeout
  217. *
  218. * @addr: Address of IO register
  219. * @delay: Delay interval of poll
  220. * @timeout: Timeout for poll
  221. * @mask: Mask to be checked
  222. * @check_val: Value to be compared to break poll
  223. * @status: Status of register of IO
  224. *
  225. * @return: 0 if success and negative if fail
  226. * */
  227. int cam_common_read_poll_timeout(
  228. void __iomem *addr,
  229. unsigned long delay,
  230. unsigned long timeout,
  231. uint32_t mask,
  232. uint32_t check_val,
  233. uint32_t *status);
  234. /**
  235. * cam_common_modify_timer()
  236. *
  237. * @brief common interface to modify timer,
  238. *
  239. * @timer: reference to system timer
  240. * @timeout_val: timeout value for timer
  241. *
  242. * @return: 0 if success and negative if fail
  243. */
  244. int cam_common_modify_timer(struct timer_list *timer, int32_t timeout_val);
  245. /**
  246. * cam_common_util_thread_switch_delay_detect()
  247. *
  248. * @brief Detect if there is any scheduling delay
  249. *
  250. * @token: String identifier to print workq name or tasklet
  251. * @scheduled_time: Time when workq or tasklet was scheduled
  252. * @threshold: Threshold time
  253. *
  254. */
  255. void cam_common_util_thread_switch_delay_detect(const char *token,
  256. ktime_t scheduled_time, uint32_t threshold);
  257. /**
  258. * cam_common_register_mini_dump_cb()
  259. *
  260. * @brief common interface to register mini dump cb
  261. *
  262. * @mini_dump_cb: Pointer to the mini_dump_cb
  263. * @name: name of device registering
  264. *
  265. * @return: 0 if success in register non-zero if failes
  266. */
  267. #if IS_REACHABLE(CONFIG_QCOM_VA_MINIDUMP)
  268. int cam_common_register_mini_dump_cb(
  269. cam_common_mini_dump_cb mini_dump_cb, uint8_t *name);
  270. #else
  271. static inline int cam_common_register_mini_dump_cb(
  272. cam_common_mini_dump_cb mini_dump_cb,
  273. uint8_t *dev_name)
  274. {
  275. return 0;
  276. }
  277. #endif
  278. /**
  279. * cam_common_user_dump_clock()
  280. *
  281. * @brief Handles clock rate dump
  282. *
  283. * @dump_struct: Struct holding dump info
  284. * @addr_ptr: Pointer to buffer address pointer
  285. */
  286. void *cam_common_user_dump_clock(
  287. void *dump_struct,
  288. uint8_t *addr_ptr);
  289. /**
  290. * cam_common_user_dump_helper()
  291. *
  292. * @brief Handles buffer addressing and dumping for user dump
  293. *
  294. * @cmd_args: Holds cam_common_hw_dump_args pointer
  295. * @func: Function pointer for dump function
  296. * @dump_struct: Struct holding dump info
  297. * @size: Size_t value used for header word size
  298. * @tag: Tag for header, used by parser
  299. * @...: Variadic arguments, appended to tag if given
  300. */
  301. int cam_common_user_dump_helper(
  302. void *cmd_args,
  303. void *(*func)(void *, uint8_t *),
  304. void *dump_struct,
  305. size_t size,
  306. const char *tag,
  307. ...);
  308. /**
  309. * cam_common_register_err_inject_cb()
  310. *
  311. * @brief common interface to register error inject cb
  312. *
  313. * @err_inject_cb: Pointer to err_inject_cb
  314. * @hw_id: HW id of the HW driver registering
  315. *
  316. * @return: 0 if success in register non-zero if failes
  317. */
  318. int cam_common_register_err_inject_cb(
  319. cam_common_err_inject_cb err_inject_cb,
  320. enum cam_common_err_inject_hw_id hw_id);
  321. #endif /* _CAM_COMMON_UTIL_H_ */