cam_common_util.h 14 KB

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