drm_print.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. __printf(2, 0)
  77. /**
  78. * drm_vprintf - print to a &drm_printer stream
  79. * @p: the &drm_printer
  80. * @fmt: format string
  81. * @va: the va_list
  82. */
  83. static inline void
  84. drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
  85. {
  86. struct va_format vaf = { .fmt = fmt, .va = va };
  87. p->printfn(p, &vaf);
  88. }
  89. /**
  90. * drm_printf_indent - Print to a &drm_printer stream with indentation
  91. * @printer: DRM printer
  92. * @indent: Tab indentation level (max 5)
  93. * @fmt: Format string
  94. */
  95. #define drm_printf_indent(printer, indent, fmt, ...) \
  96. drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
  97. /**
  98. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  99. * @f: the &struct seq_file to output to
  100. *
  101. * RETURNS:
  102. * The &drm_printer object
  103. */
  104. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  105. {
  106. struct drm_printer p = {
  107. .printfn = __drm_printfn_seq_file,
  108. .arg = f,
  109. };
  110. return p;
  111. }
  112. /**
  113. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  114. * @dev: the &struct device pointer
  115. *
  116. * RETURNS:
  117. * The &drm_printer object
  118. */
  119. static inline struct drm_printer drm_info_printer(struct device *dev)
  120. {
  121. struct drm_printer p = {
  122. .printfn = __drm_printfn_info,
  123. .arg = dev,
  124. };
  125. return p;
  126. }
  127. /**
  128. * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
  129. * @prefix: debug output prefix
  130. *
  131. * RETURNS:
  132. * The &drm_printer object
  133. */
  134. static inline struct drm_printer drm_debug_printer(const char *prefix)
  135. {
  136. struct drm_printer p = {
  137. .printfn = __drm_printfn_debug,
  138. .prefix = prefix
  139. };
  140. return p;
  141. }
  142. /*
  143. * The following categories are defined:
  144. *
  145. * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
  146. * This is the category used by the DRM_DEBUG() macro.
  147. *
  148. * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
  149. * This is the category used by the DRM_DEBUG_DRIVER() macro.
  150. *
  151. * KMS: used in the modesetting code.
  152. * This is the category used by the DRM_DEBUG_KMS() macro.
  153. *
  154. * PRIME: used in the prime code.
  155. * This is the category used by the DRM_DEBUG_PRIME() macro.
  156. *
  157. * ATOMIC: used in the atomic code.
  158. * This is the category used by the DRM_DEBUG_ATOMIC() macro.
  159. *
  160. * VBL: used for verbose debug message in the vblank code
  161. * This is the category used by the DRM_DEBUG_VBL() macro.
  162. *
  163. * Enabling verbose debug messages is done through the drm.debug parameter,
  164. * each category being enabled by a bit.
  165. *
  166. * drm.debug=0x1 will enable CORE messages
  167. * drm.debug=0x2 will enable DRIVER messages
  168. * drm.debug=0x3 will enable CORE and DRIVER messages
  169. * ...
  170. * drm.debug=0x3f will enable all messages
  171. *
  172. * An interesting feature is that it's possible to enable verbose logging at
  173. * run-time by echoing the debug value in its sysfs node:
  174. * # echo 0xf > /sys/module/drm/parameters/debug
  175. */
  176. #define DRM_UT_NONE 0x00
  177. #define DRM_UT_CORE 0x01
  178. #define DRM_UT_DRIVER 0x02
  179. #define DRM_UT_KMS 0x04
  180. #define DRM_UT_PRIME 0x08
  181. #define DRM_UT_ATOMIC 0x10
  182. #define DRM_UT_VBL 0x20
  183. #define DRM_UT_STATE 0x40
  184. #define DRM_UT_LEASE 0x80
  185. __printf(3, 4)
  186. void drm_dev_printk(const struct device *dev, const char *level,
  187. const char *format, ...);
  188. __printf(3, 4)
  189. void drm_dev_dbg(const struct device *dev, unsigned int category,
  190. const char *format, ...);
  191. __printf(2, 3)
  192. void drm_dbg(unsigned int category, const char *format, ...);
  193. __printf(1, 2)
  194. void drm_err(const char *format, ...);
  195. /* Macros to make printk easier */
  196. #define _DRM_PRINTK(once, level, fmt, ...) \
  197. printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
  198. #define DRM_INFO(fmt, ...) \
  199. _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
  200. #define DRM_NOTE(fmt, ...) \
  201. _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
  202. #define DRM_WARN(fmt, ...) \
  203. _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
  204. #define DRM_INFO_ONCE(fmt, ...) \
  205. _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
  206. #define DRM_NOTE_ONCE(fmt, ...) \
  207. _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
  208. #define DRM_WARN_ONCE(fmt, ...) \
  209. _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
  210. /**
  211. * Error output.
  212. *
  213. * @dev: device pointer
  214. * @fmt: printf() like format string.
  215. */
  216. #define DRM_DEV_ERROR(dev, fmt, ...) \
  217. drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__)
  218. #define DRM_ERROR(fmt, ...) \
  219. drm_err(fmt, ##__VA_ARGS__)
  220. /**
  221. * Rate limited error output. Like DRM_ERROR() but won't flood the log.
  222. *
  223. * @dev: device pointer
  224. * @fmt: printf() like format string.
  225. */
  226. #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
  227. ({ \
  228. static DEFINE_RATELIMIT_STATE(_rs, \
  229. DEFAULT_RATELIMIT_INTERVAL, \
  230. DEFAULT_RATELIMIT_BURST); \
  231. \
  232. if (__ratelimit(&_rs)) \
  233. DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
  234. })
  235. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  236. DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  237. #define DRM_DEV_INFO(dev, fmt, ...) \
  238. drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__)
  239. #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
  240. ({ \
  241. static bool __print_once __read_mostly; \
  242. if (!__print_once) { \
  243. __print_once = true; \
  244. DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
  245. } \
  246. })
  247. /**
  248. * Debug output.
  249. *
  250. * @dev: device pointer
  251. * @fmt: printf() like format string.
  252. */
  253. #define DRM_DEV_DEBUG(dev, fmt, ...) \
  254. drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  255. #define DRM_DEBUG(fmt, ...) \
  256. drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__)
  257. #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \
  258. drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  259. #define DRM_DEBUG_DRIVER(fmt, ...) \
  260. drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  261. #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \
  262. drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  263. #define DRM_DEBUG_KMS(fmt, ...) \
  264. drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__)
  265. #define DRM_DEV_DEBUG_PRIME(dev, fmt, ...) \
  266. drm_dev_dbg(dev, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  267. #define DRM_DEBUG_PRIME(fmt, ...) \
  268. drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  269. #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, ...) \
  270. drm_dev_dbg(dev, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  271. #define DRM_DEBUG_ATOMIC(fmt, ...) \
  272. drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  273. #define DRM_DEV_DEBUG_VBL(dev, fmt, ...) \
  274. drm_dev_dbg(dev, DRM_UT_VBL, fmt, ##__VA_ARGS__)
  275. #define DRM_DEBUG_VBL(fmt, ...) \
  276. drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__)
  277. #define DRM_DEBUG_LEASE(fmt, ...) \
  278. drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__)
  279. #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, category, fmt, ...) \
  280. ({ \
  281. static DEFINE_RATELIMIT_STATE(_rs, \
  282. DEFAULT_RATELIMIT_INTERVAL, \
  283. DEFAULT_RATELIMIT_BURST); \
  284. if (__ratelimit(&_rs)) \
  285. drm_dev_dbg(dev, category, fmt, ##__VA_ARGS__); \
  286. })
  287. /**
  288. * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
  289. *
  290. * @dev: device pointer
  291. * @fmt: printf() like format string.
  292. */
  293. #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, ...) \
  294. _DEV_DRM_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_CORE, \
  295. fmt, ##__VA_ARGS__)
  296. #define DRM_DEBUG_RATELIMITED(fmt, ...) \
  297. DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  298. #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, ...) \
  299. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_DRIVER, \
  300. fmt, ##__VA_ARGS__)
  301. #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, ...) \
  302. DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  303. #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, ...) \
  304. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_KMS, \
  305. fmt, ##__VA_ARGS__)
  306. #define DRM_DEBUG_KMS_RATELIMITED(fmt, ...) \
  307. DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  308. #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, ...) \
  309. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_PRIME, \
  310. fmt, ##__VA_ARGS__)
  311. #define DRM_DEBUG_PRIME_RATELIMITED(fmt, ...) \
  312. DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  313. #endif /* DRM_PRINT_H_ */