cam_common_util.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 PTR_TO_U64(ptr) ((uint64_t)(uintptr_t)ptr)
  17. #define U64_TO_PTR(ptr) ((void *)(uintptr_t)ptr)
  18. #define CAM_GET_TIMESTAMP(timestamp) ktime_get_real_ts64(&(timestamp))
  19. #define CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts_start, ts_end, diff_microsec) \
  20. ({ \
  21. diff_microsec = 0; \
  22. if (ts_end.tv_nsec >= ts_start.tv_nsec) { \
  23. diff_microsec = \
  24. (ts_end.tv_nsec - ts_start.tv_nsec) / 1000; \
  25. diff_microsec += \
  26. (ts_end.tv_sec - ts_start.tv_sec) * 1000 * 1000; \
  27. } else { \
  28. diff_microsec = \
  29. (ts_end.tv_nsec + \
  30. (1000*1000*1000 - ts_start.tv_nsec)) / 1000; \
  31. diff_microsec += \
  32. (ts_end.tv_sec - ts_start.tv_sec - 1) * 1000 * 1000; \
  33. } \
  34. })
  35. #define CAM_CONVERT_TIMESTAMP_FORMAT(ts, hrs, min, sec, ms) \
  36. ({ \
  37. uint64_t tmp = ((ts).tv_sec); \
  38. (ms) = ((ts).tv_nsec) / 1000000; \
  39. (sec) = do_div(tmp, 60); \
  40. (min) = do_div(tmp, 60); \
  41. (hrs) = do_div(tmp, 24); \
  42. })
  43. typedef unsigned long (*cam_common_mini_dump_cb) (void *dst, unsigned long len);
  44. /**
  45. * struct cam_common_mini_dump_dev_info
  46. * @dump_cb : address of data dumped
  47. * @name : Name of driver
  48. * @num_devs : Number of device registerd
  49. * @is_registered : Bool to indicate if registered
  50. */
  51. struct cam_common_mini_dump_dev_info {
  52. cam_common_mini_dump_cb dump_cb[CAM_COMMON_MINI_DUMP_DEV_NUM];
  53. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM]
  54. [CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  55. uint8_t num_devs;
  56. bool is_registered;
  57. };
  58. /**
  59. * struct cam_common_mini_dump_data
  60. * @link : address of data dumped
  61. * @name : Name of driver
  62. * @size : Size dumped
  63. */
  64. struct cam_common_mini_dump_data {
  65. void *waddr[CAM_COMMON_MINI_DUMP_DEV_NUM];
  66. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM][CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  67. unsigned long size[CAM_COMMON_MINI_DUMP_DEV_NUM];
  68. };
  69. /**
  70. * struct cam_common_hw_dump_args
  71. * @req_id : request id
  72. * @cpu_addr : address where dumping will start from
  73. * @buf_len : length of buffer where data is being dumped to
  74. * @offset : buffer offset from cpu_addr after each item dump
  75. * @ctxt_to_hw_map : context to hw map
  76. * @is_dump_all : flag to indicate if all information or just bw/clk rate
  77. * @
  78. */
  79. struct cam_common_hw_dump_args {
  80. uint64_t req_id;
  81. uintptr_t cpu_addr;
  82. size_t buf_len;
  83. size_t offset;
  84. void *ctxt_to_hw_map;
  85. bool is_dump_all;
  86. };
  87. /**
  88. * struct cam_common_hw_dump_header
  89. * @tag : string used by the parser to call parse functions
  90. * @size : size of the header in the buffer
  91. * @word_size : word size of the header
  92. * @
  93. */
  94. struct cam_common_hw_dump_header {
  95. uint8_t tag[CAM_COMMON_HW_DUMP_TAG_MAX_LEN];
  96. uint64_t size;
  97. uint32_t word_size;
  98. };
  99. /**
  100. * cam_common_util_get_string_index()
  101. *
  102. * @brief Match the string from list of strings to return
  103. * matching index
  104. *
  105. * @strings: Pointer to list of strings
  106. * @num_strings: Number of strings in 'strings'
  107. * @matching_string: String to match
  108. * @index: Pointer to index to return matching index
  109. *
  110. * @return: 0 for success
  111. * -EINVAL for Fail
  112. */
  113. int cam_common_util_get_string_index(const char **strings,
  114. uint32_t num_strings, const char *matching_string, uint32_t *index);
  115. /**
  116. * cam_common_util_remove_duplicate_arr()
  117. *
  118. * @brief Move all the unique integers to the start of
  119. * the array and return the number of unique integers
  120. *
  121. * @array: Pointer to the first integer of array
  122. * @num: Number of elements in array
  123. *
  124. * @return: Number of unique integers in array
  125. */
  126. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  127. uint32_t num);
  128. /**
  129. * cam_common_wait_for_completion_timeout()
  130. *
  131. * @brief common interface to implement wait for completion
  132. * for slow environment like presil, single debug
  133. * timeout variable can take care
  134. *
  135. * @complete: Pointer to the first integer of array
  136. * @timeout_jiffies: Timeout value in jiffie
  137. *
  138. * @return: Remaining jiffies, non-zero for success, zero
  139. * in case of failure
  140. */
  141. unsigned long cam_common_wait_for_completion_timeout(
  142. struct completion *complete,
  143. unsigned long timeout_jiffies);
  144. /**
  145. * cam_common_read_poll_timeout()
  146. *
  147. * @brief common interface to read poll timeout
  148. *
  149. * @addr: Address of IO register
  150. * @delay: Delay interval of poll
  151. * @timeout: Timeout for poll
  152. * @mask: Mask to be checked
  153. * @check_val: Value to be compared to break poll
  154. * @status: Status of register of IO
  155. *
  156. * @return: 0 if success and negative if fail
  157. * */
  158. int cam_common_read_poll_timeout(
  159. void __iomem *addr,
  160. unsigned long delay,
  161. unsigned long timeout,
  162. uint32_t mask,
  163. uint32_t check_val,
  164. uint32_t *status);
  165. /**
  166. * cam_common_modify_timer()
  167. *
  168. * @brief common interface to modify timer,
  169. *
  170. * @timer: reference to system timer
  171. * @timeout_val: timeout value for timer
  172. *
  173. * @return: 0 if success and negative if fail
  174. */
  175. int cam_common_modify_timer(struct timer_list *timer, int32_t timeout_val);
  176. /**
  177. * cam_common_util_thread_switch_delay_detect()
  178. *
  179. * @brief Detect if there is any scheduling delay
  180. *
  181. * @token: String identifier to print workq name or tasklet
  182. * @scheduled_time: Time when workq or tasklet was scheduled
  183. * @threshold: Threshold time
  184. *
  185. */
  186. void cam_common_util_thread_switch_delay_detect(const char *token,
  187. ktime_t scheduled_time, uint32_t threshold);
  188. /**
  189. * cam_common_register_mini_dump_cb()
  190. *
  191. * @brief common interface to register mini dump cb
  192. *
  193. * @mini_dump_cb: Pointer to the mini_dump_cb
  194. * @name: name of device registering
  195. *
  196. * @return: 0 if success in register non-zero if failes
  197. */
  198. #if IS_REACHABLE(CONFIG_QCOM_VA_MINIDUMP)
  199. int cam_common_register_mini_dump_cb(
  200. cam_common_mini_dump_cb mini_dump_cb, uint8_t *name);
  201. #else
  202. static inline int cam_common_register_mini_dump_cb(
  203. cam_common_mini_dump_cb mini_dump_cb,
  204. uint8_t *dev_name)
  205. {
  206. return 0;
  207. }
  208. #endif
  209. /**
  210. * cam_common_user_dump_clock()
  211. *
  212. * @brief Handles clock rate dump
  213. *
  214. * @dump_struct: Struct holding dump info
  215. * @addr_ptr: Pointer to buffer address pointer
  216. */
  217. void *cam_common_user_dump_clock(
  218. void *dump_struct,
  219. uint8_t *addr_ptr);
  220. /**
  221. * cam_common_user_dump_helper()
  222. *
  223. * @brief Handles buffer addressing and dumping for user dump
  224. *
  225. * @cmd_args: Holds cam_common_hw_dump_args pointer
  226. * @func: Function pointer for dump function
  227. * @dump_struct: Struct holding dump info
  228. * @size: Size_t value used for header word size
  229. * @tag: Tag for header, used by parser
  230. * @...: Variadic arguments, appended to tag if given
  231. */
  232. int cam_common_user_dump_helper(
  233. void *cmd_args,
  234. void *(*func)(void *, uint8_t *),
  235. void *dump_struct,
  236. size_t size,
  237. const char *tag,
  238. ...);
  239. #endif /* _CAM_COMMON_UTIL_H_ */