cam_common_util.h 13 KB

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