cam_common_util.h 11 KB

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