cam_common_util.h 11 KB

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