cam_common_util.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 PTR_TO_U64(ptr) ((uint64_t)(uintptr_t)ptr)
  16. #define U64_TO_PTR(ptr) ((void *)(uintptr_t)ptr)
  17. #define CAM_GET_TIMESTAMP(timestamp) ktime_get_real_ts64(&(timestamp))
  18. #define CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts_start, ts_end, diff_microsec) \
  19. ({ \
  20. diff_microsec = 0; \
  21. if (ts_end.tv_nsec >= ts_start.tv_nsec) { \
  22. diff_microsec = \
  23. (ts_end.tv_nsec - ts_start.tv_nsec) / 1000; \
  24. diff_microsec += \
  25. (ts_end.tv_sec - ts_start.tv_sec) * 1000 * 1000; \
  26. } else { \
  27. diff_microsec = \
  28. (ts_end.tv_nsec + \
  29. (1000*1000*1000 - ts_start.tv_nsec)) / 1000; \
  30. diff_microsec += \
  31. (ts_end.tv_sec - ts_start.tv_sec - 1) * 1000 * 1000; \
  32. } \
  33. })
  34. #define CAM_CONVERT_TIMESTAMP_FORMAT(ts, hrs, min, sec, ms) \
  35. ({ \
  36. uint64_t tmp = ((ts).tv_sec); \
  37. (ms) = ((ts).tv_nsec) / 1000000; \
  38. (sec) = do_div(tmp, 60); \
  39. (min) = do_div(tmp, 60); \
  40. (hrs) = do_div(tmp, 24); \
  41. })
  42. typedef unsigned long (*cam_common_mini_dump_cb) (void *dst, unsigned long len);
  43. /**
  44. * struct cam_common_mini_dump_dev_info
  45. * @dump_cb : address of data dumped
  46. * @name : Name of driver
  47. * @num_devs : Number of device registerd
  48. * @is_registered : Bool to indicate if registered
  49. */
  50. struct cam_common_mini_dump_dev_info {
  51. cam_common_mini_dump_cb dump_cb[CAM_COMMON_MINI_DUMP_DEV_NUM];
  52. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM]
  53. [CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  54. uint8_t num_devs;
  55. bool is_registered;
  56. };
  57. /**
  58. * struct cam_common_mini_dump_data
  59. * @link : address of data dumped
  60. * @name : Name of driver
  61. * @size : Size dumped
  62. */
  63. struct cam_common_mini_dump_data {
  64. void *waddr[CAM_COMMON_MINI_DUMP_DEV_NUM];
  65. uint8_t name[CAM_COMMON_MINI_DUMP_DEV_NUM][CAM_COMMON_MINI_DUMP_DEV_NAME_LEN];
  66. unsigned long size[CAM_COMMON_MINI_DUMP_DEV_NUM];
  67. };
  68. /**
  69. * cam_common_util_get_string_index()
  70. *
  71. * @brief Match the string from list of strings to return
  72. * matching index
  73. *
  74. * @strings: Pointer to list of strings
  75. * @num_strings: Number of strings in 'strings'
  76. * @matching_string: String to match
  77. * @index: Pointer to index to return matching index
  78. *
  79. * @return: 0 for success
  80. * -EINVAL for Fail
  81. */
  82. int cam_common_util_get_string_index(const char **strings,
  83. uint32_t num_strings, const char *matching_string, uint32_t *index);
  84. /**
  85. * cam_common_util_remove_duplicate_arr()
  86. *
  87. * @brief Move all the unique integers to the start of
  88. * the array and return the number of unique integers
  89. *
  90. * @array: Pointer to the first integer of array
  91. * @num: Number of elements in array
  92. *
  93. * @return: Number of unique integers in array
  94. */
  95. uint32_t cam_common_util_remove_duplicate_arr(int32_t *array,
  96. uint32_t num);
  97. /**
  98. * cam_common_wait_for_completion_timeout()
  99. *
  100. * @brief common interface to implement wait for completion
  101. * for slow environment like presil, single debug
  102. * timeout variable can take care
  103. *
  104. * @complete: Pointer to the first integer of array
  105. * @timeout_jiffies: Timeout value in jiffie
  106. *
  107. * @return: Remaining jiffies, non-zero for success, zero
  108. * in case of failure
  109. */
  110. unsigned long cam_common_wait_for_completion_timeout(
  111. struct completion *complete,
  112. unsigned long timeout_jiffies);
  113. /**
  114. * cam_common_read_poll_timeout()
  115. *
  116. * @brief common interface to read poll timeout
  117. *
  118. * @addr: Address of IO register
  119. * @delay: Delay interval of poll
  120. * @timeout: Timeout for poll
  121. * @mask: Mask to be checked
  122. * @check_val: Value to be compared to break poll
  123. * @status: Status of register of IO
  124. *
  125. * @return: 0 if success and negative if fail
  126. * */
  127. int cam_common_read_poll_timeout(
  128. void __iomem *addr,
  129. unsigned long delay,
  130. unsigned long timeout,
  131. uint32_t mask,
  132. uint32_t check_val,
  133. uint32_t *status);
  134. /**
  135. * cam_common_modify_timer()
  136. *
  137. * @brief common interface to modify timer,
  138. *
  139. * @timer: reference to system timer
  140. * @timeout_val: timeout value for timer
  141. *
  142. * @return: 0 if success and negative if fail
  143. */
  144. int cam_common_modify_timer(struct timer_list *timer, int32_t timeout_val);
  145. /**
  146. * cam_common_util_thread_switch_delay_detect()
  147. *
  148. * @brief Detect if there is any scheduling delay
  149. *
  150. * @token: String identifier to print workq name or tasklet
  151. * @scheduled_time: Time when workq or tasklet was scheduled
  152. * @threshold: Threshold time
  153. *
  154. */
  155. void cam_common_util_thread_switch_delay_detect(const char *token,
  156. ktime_t scheduled_time, uint32_t threshold);
  157. /**
  158. * cam_common_register_mini_dump_cb()
  159. *
  160. * @brief common interface to register mini dump cb
  161. *
  162. * @mini_dump_cb: Pointer to the mini_dump_cb
  163. * @name: name of device registering
  164. *
  165. * @return: 0 if success in register non-zero if failes
  166. */
  167. #if IS_REACHABLE(CONFIG_QCOM_VA_MINIDUMP)
  168. int cam_common_register_mini_dump_cb(
  169. cam_common_mini_dump_cb mini_dump_cb, uint8_t *name);
  170. #else
  171. static inline int cam_common_register_mini_dump_cb(
  172. cam_common_mini_dump_cb mini_dump_cb,
  173. uint8_t *dev_name)
  174. {
  175. return 0;
  176. }
  177. #endif
  178. #endif /* _CAM_COMMON_UTIL_H_ */