drm_print.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2016 Red Hat
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors:
  23. * Rob Clark <robdclark@gmail.com>
  24. */
  25. #ifndef DRM_PRINT_H_
  26. #define DRM_PRINT_H_
  27. #include <linux/compiler.h>
  28. #include <linux/printk.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/device.h>
  31. /**
  32. * DOC: print
  33. *
  34. * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
  35. * debug code to be used for both debugfs and printk logging.
  36. *
  37. * For example::
  38. *
  39. * void log_some_info(struct drm_printer *p)
  40. * {
  41. * drm_printf(p, "foo=%d\n", foo);
  42. * drm_printf(p, "bar=%d\n", bar);
  43. * }
  44. *
  45. * #ifdef CONFIG_DEBUG_FS
  46. * void debugfs_show(struct seq_file *f)
  47. * {
  48. * struct drm_printer p = drm_seq_file_printer(f);
  49. * log_some_info(&p);
  50. * }
  51. * #endif
  52. *
  53. * void some_other_function(...)
  54. * {
  55. * struct drm_printer p = drm_info_printer(drm->dev);
  56. * log_some_info(&p);
  57. * }
  58. */
  59. /**
  60. * struct drm_printer - drm output "stream"
  61. *
  62. * Do not use struct members directly. Use drm_printer_seq_file(),
  63. * drm_printer_info(), etc to initialize. And drm_printf() for output.
  64. */
  65. struct drm_printer {
  66. /* private: */
  67. void (*printfn)(struct drm_printer *p, struct va_format *vaf);
  68. void *arg;
  69. const char *prefix;
  70. };
  71. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
  72. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
  73. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
  74. __printf(2, 3)
  75. void drm_printf(struct drm_printer *p, const char *f, ...);
  76. /**
  77. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  78. * @f: the &struct seq_file to output to
  79. *
  80. * RETURNS:
  81. * The &drm_printer object
  82. */
  83. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  84. {
  85. struct drm_printer p = {
  86. .printfn = __drm_printfn_seq_file,
  87. .arg = f,
  88. };
  89. return p;
  90. }
  91. /**
  92. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  93. * @dev: the &struct device pointer
  94. *
  95. * RETURNS:
  96. * The &drm_printer object
  97. */
  98. static inline struct drm_printer drm_info_printer(struct device *dev)
  99. {
  100. struct drm_printer p = {
  101. .printfn = __drm_printfn_info,
  102. .arg = dev,
  103. };
  104. return p;
  105. }
  106. /**
  107. * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
  108. * @prefix: debug output prefix
  109. *
  110. * RETURNS:
  111. * The &drm_printer object
  112. */
  113. static inline struct drm_printer drm_debug_printer(const char *prefix)
  114. {
  115. struct drm_printer p = {
  116. .printfn = __drm_printfn_debug,
  117. .prefix = prefix
  118. };
  119. return p;
  120. }
  121. /*
  122. * The following categories are defined:
  123. *
  124. * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
  125. * This is the category used by the DRM_DEBUG() macro.
  126. *
  127. * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
  128. * This is the category used by the DRM_DEBUG_DRIVER() macro.
  129. *
  130. * KMS: used in the modesetting code.
  131. * This is the category used by the DRM_DEBUG_KMS() macro.
  132. *
  133. * PRIME: used in the prime code.
  134. * This is the category used by the DRM_DEBUG_PRIME() macro.
  135. *
  136. * ATOMIC: used in the atomic code.
  137. * This is the category used by the DRM_DEBUG_ATOMIC() macro.
  138. *
  139. * VBL: used for verbose debug message in the vblank code
  140. * This is the category used by the DRM_DEBUG_VBL() macro.
  141. *
  142. * Enabling verbose debug messages is done through the drm.debug parameter,
  143. * each category being enabled by a bit.
  144. *
  145. * drm.debug=0x1 will enable CORE messages
  146. * drm.debug=0x2 will enable DRIVER messages
  147. * drm.debug=0x3 will enable CORE and DRIVER messages
  148. * ...
  149. * drm.debug=0x3f will enable all messages
  150. *
  151. * An interesting feature is that it's possible to enable verbose logging at
  152. * run-time by echoing the debug value in its sysfs node:
  153. * # echo 0xf > /sys/module/drm/parameters/debug
  154. */
  155. #define DRM_UT_NONE 0x00
  156. #define DRM_UT_CORE 0x01
  157. #define DRM_UT_DRIVER 0x02
  158. #define DRM_UT_KMS 0x04
  159. #define DRM_UT_PRIME 0x08
  160. #define DRM_UT_ATOMIC 0x10
  161. #define DRM_UT_VBL 0x20
  162. #define DRM_UT_STATE 0x40
  163. __printf(6, 7)
  164. void drm_dev_printk(const struct device *dev, const char *level,
  165. unsigned int category, const char *function_name,
  166. const char *prefix, const char *format, ...);
  167. __printf(3, 4)
  168. void drm_printk(const char *level, unsigned int category,
  169. const char *format, ...);
  170. /***********************************************************************/
  171. /** \name Macros to make printk easier */
  172. /*@{*/
  173. #define _DRM_PRINTK(once, level, fmt, ...) \
  174. do { \
  175. printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
  176. ##__VA_ARGS__); \
  177. } while (0)
  178. #define DRM_INFO(fmt, ...) \
  179. _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
  180. #define DRM_NOTE(fmt, ...) \
  181. _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
  182. #define DRM_WARN(fmt, ...) \
  183. _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
  184. #define DRM_INFO_ONCE(fmt, ...) \
  185. _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
  186. #define DRM_NOTE_ONCE(fmt, ...) \
  187. _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
  188. #define DRM_WARN_ONCE(fmt, ...) \
  189. _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
  190. /**
  191. * Error output.
  192. *
  193. * \param fmt printf() like format string.
  194. * \param arg arguments
  195. */
  196. #define DRM_DEV_ERROR(dev, fmt, ...) \
  197. drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
  198. fmt, ##__VA_ARGS__)
  199. #define DRM_ERROR(fmt, ...) \
  200. drm_printk(KERN_ERR, DRM_UT_NONE, fmt, ##__VA_ARGS__)
  201. /**
  202. * Rate limited error output. Like DRM_ERROR() but won't flood the log.
  203. *
  204. * \param fmt printf() like format string.
  205. * \param arg arguments
  206. */
  207. #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
  208. ({ \
  209. static DEFINE_RATELIMIT_STATE(_rs, \
  210. DEFAULT_RATELIMIT_INTERVAL, \
  211. DEFAULT_RATELIMIT_BURST); \
  212. \
  213. if (__ratelimit(&_rs)) \
  214. DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
  215. })
  216. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  217. DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  218. #define DRM_DEV_INFO(dev, fmt, ...) \
  219. drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt, \
  220. ##__VA_ARGS__)
  221. #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
  222. ({ \
  223. static bool __print_once __read_mostly; \
  224. if (!__print_once) { \
  225. __print_once = true; \
  226. DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
  227. } \
  228. })
  229. /**
  230. * Debug output.
  231. *
  232. * \param fmt printf() like format string.
  233. * \param arg arguments
  234. */
  235. #define DRM_DEV_DEBUG(dev, fmt, args...) \
  236. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
  237. ##args)
  238. #define DRM_DEBUG(fmt, ...) \
  239. drm_printk(KERN_DEBUG, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  240. #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...) \
  241. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "", \
  242. fmt, ##args)
  243. #define DRM_DEBUG_DRIVER(fmt, ...) \
  244. drm_printk(KERN_DEBUG, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  245. #define DRM_DEV_DEBUG_KMS(dev, fmt, args...) \
  246. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt, \
  247. ##args)
  248. #define DRM_DEBUG_KMS(fmt, ...) \
  249. drm_printk(KERN_DEBUG, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  250. #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...) \
  251. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "", \
  252. fmt, ##args)
  253. #define DRM_DEBUG_PRIME(fmt, ...) \
  254. drm_printk(KERN_DEBUG, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  255. #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...) \
  256. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "", \
  257. fmt, ##args)
  258. #define DRM_DEBUG_ATOMIC(fmt, ...) \
  259. drm_printk(KERN_DEBUG, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  260. #define DRM_DEV_DEBUG_VBL(dev, fmt, args...) \
  261. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt, \
  262. ##args)
  263. #define DRM_DEBUG_VBL(fmt, ...) \
  264. drm_printk(KERN_DEBUG, DRM_UT_VBL, fmt, ##__VA_ARGS__)
  265. #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...) \
  266. ({ \
  267. static DEFINE_RATELIMIT_STATE(_rs, \
  268. DEFAULT_RATELIMIT_INTERVAL, \
  269. DEFAULT_RATELIMIT_BURST); \
  270. if (__ratelimit(&_rs)) \
  271. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level, \
  272. __func__, "", fmt, ##args); \
  273. })
  274. /**
  275. * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
  276. *
  277. * \param fmt printf() like format string.
  278. * \param arg arguments
  279. */
  280. #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...) \
  281. DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
  282. #define DRM_DEBUG_RATELIMITED(fmt, args...) \
  283. DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
  284. #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...) \
  285. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
  286. #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...) \
  287. DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
  288. #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...) \
  289. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
  290. #define DRM_DEBUG_KMS_RATELIMITED(fmt, args...) \
  291. DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
  292. #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...) \
  293. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
  294. #define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...) \
  295. DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
  296. /* Format strings and argument splitters to simplify printing
  297. * various "complex" objects
  298. */
  299. /*@}*/
  300. #endif /* DRM_PRINT_H_ */