cam_common_util.h 11 KB

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