drm_print.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 (*puts)(struct drm_printer *p, const char *str);
  69. void *arg;
  70. const char *prefix;
  71. };
  72. void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf);
  73. void __drm_puts_coredump(struct drm_printer *p, const char *str);
  74. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
  75. void __drm_puts_seq_file(struct drm_printer *p, const char *str);
  76. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
  77. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
  78. __printf(2, 3)
  79. void drm_printf(struct drm_printer *p, const char *f, ...);
  80. void drm_puts(struct drm_printer *p, const char *str);
  81. __printf(2, 0)
  82. /**
  83. * drm_vprintf - print to a &drm_printer stream
  84. * @p: the &drm_printer
  85. * @fmt: format string
  86. * @va: the va_list
  87. */
  88. static inline void
  89. drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
  90. {
  91. struct va_format vaf = { .fmt = fmt, .va = va };
  92. p->printfn(p, &vaf);
  93. }
  94. /**
  95. * drm_printf_indent - Print to a &drm_printer stream with indentation
  96. * @printer: DRM printer
  97. * @indent: Tab indentation level (max 5)
  98. * @fmt: Format string
  99. */
  100. #define drm_printf_indent(printer, indent, fmt, ...) \
  101. drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
  102. /**
  103. * struct drm_print_iterator - local struct used with drm_printer_coredump
  104. * @data: Pointer to the devcoredump output buffer
  105. * @start: The offset within the buffer to start writing
  106. * @remain: The number of bytes to write for this iteration
  107. */
  108. struct drm_print_iterator {
  109. void *data;
  110. ssize_t start;
  111. ssize_t remain;
  112. /* private: */
  113. ssize_t offset;
  114. };
  115. /**
  116. * drm_coredump_printer - construct a &drm_printer that can output to a buffer
  117. * from the read function for devcoredump
  118. * @iter: A pointer to a struct drm_print_iterator for the read instance
  119. *
  120. * This wrapper extends drm_printf() to work with a dev_coredumpm() callback
  121. * function. The passed in drm_print_iterator struct contains the buffer
  122. * pointer, size and offset as passed in from devcoredump.
  123. *
  124. * For example::
  125. *
  126. * void coredump_read(char *buffer, loff_t offset, size_t count,
  127. * void *data, size_t datalen)
  128. * {
  129. * struct drm_print_iterator iter;
  130. * struct drm_printer p;
  131. *
  132. * iter.data = buffer;
  133. * iter.start = offset;
  134. * iter.remain = count;
  135. *
  136. * p = drm_coredump_printer(&iter);
  137. *
  138. * drm_printf(p, "foo=%d\n", foo);
  139. * }
  140. *
  141. * void makecoredump(...)
  142. * {
  143. * ...
  144. * dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL,
  145. * coredump_read, ...)
  146. * }
  147. *
  148. * RETURNS:
  149. * The &drm_printer object
  150. */
  151. static inline struct drm_printer
  152. drm_coredump_printer(struct drm_print_iterator *iter)
  153. {
  154. struct drm_printer p = {
  155. .printfn = __drm_printfn_coredump,
  156. .puts = __drm_puts_coredump,
  157. .arg = iter,
  158. };
  159. /* Set the internal offset of the iterator to zero */
  160. iter->offset = 0;
  161. return p;
  162. }
  163. /**
  164. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  165. * @f: the &struct seq_file to output to
  166. *
  167. * RETURNS:
  168. * The &drm_printer object
  169. */
  170. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  171. {
  172. struct drm_printer p = {
  173. .printfn = __drm_printfn_seq_file,
  174. .puts = __drm_puts_seq_file,
  175. .arg = f,
  176. };
  177. return p;
  178. }
  179. /**
  180. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  181. * @dev: the &struct device pointer
  182. *
  183. * RETURNS:
  184. * The &drm_printer object
  185. */
  186. static inline struct drm_printer drm_info_printer(struct device *dev)
  187. {
  188. struct drm_printer p = {
  189. .printfn = __drm_printfn_info,
  190. .arg = dev,
  191. };
  192. return p;
  193. }
  194. /**
  195. * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
  196. * @prefix: debug output prefix
  197. *
  198. * RETURNS:
  199. * The &drm_printer object
  200. */
  201. static inline struct drm_printer drm_debug_printer(const char *prefix)
  202. {
  203. struct drm_printer p = {
  204. .printfn = __drm_printfn_debug,
  205. .prefix = prefix
  206. };
  207. return p;
  208. }
  209. /*
  210. * The following categories are defined:
  211. *
  212. * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
  213. * This is the category used by the DRM_DEBUG() macro.
  214. *
  215. * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
  216. * This is the category used by the DRM_DEBUG_DRIVER() macro.
  217. *
  218. * KMS: used in the modesetting code.
  219. * This is the category used by the DRM_DEBUG_KMS() macro.
  220. *
  221. * PRIME: used in the prime code.
  222. * This is the category used by the DRM_DEBUG_PRIME() macro.
  223. *
  224. * ATOMIC: used in the atomic code.
  225. * This is the category used by the DRM_DEBUG_ATOMIC() macro.
  226. *
  227. * VBL: used for verbose debug message in the vblank code
  228. * This is the category used by the DRM_DEBUG_VBL() macro.
  229. *
  230. * Enabling verbose debug messages is done through the drm.debug parameter,
  231. * each category being enabled by a bit.
  232. *
  233. * drm.debug=0x1 will enable CORE messages
  234. * drm.debug=0x2 will enable DRIVER messages
  235. * drm.debug=0x3 will enable CORE and DRIVER messages
  236. * ...
  237. * drm.debug=0x3f will enable all messages
  238. *
  239. * An interesting feature is that it's possible to enable verbose logging at
  240. * run-time by echoing the debug value in its sysfs node:
  241. * # echo 0xf > /sys/module/drm/parameters/debug
  242. */
  243. #define DRM_UT_NONE 0x00
  244. #define DRM_UT_CORE 0x01
  245. #define DRM_UT_DRIVER 0x02
  246. #define DRM_UT_KMS 0x04
  247. #define DRM_UT_PRIME 0x08
  248. #define DRM_UT_ATOMIC 0x10
  249. #define DRM_UT_VBL 0x20
  250. #define DRM_UT_STATE 0x40
  251. #define DRM_UT_LEASE 0x80
  252. #define DRM_UT_DP 0x100
  253. __printf(3, 4)
  254. void drm_dev_printk(const struct device *dev, const char *level,
  255. const char *format, ...);
  256. __printf(3, 4)
  257. void drm_dev_dbg(const struct device *dev, unsigned int category,
  258. const char *format, ...);
  259. __printf(2, 3)
  260. void drm_dbg(unsigned int category, const char *format, ...);
  261. __printf(1, 2)
  262. void drm_err(const char *format, ...);
  263. /* Macros to make printk easier */
  264. #define _DRM_PRINTK(once, level, fmt, ...) \
  265. printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
  266. #define DRM_INFO(fmt, ...) \
  267. _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
  268. #define DRM_NOTE(fmt, ...) \
  269. _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
  270. #define DRM_WARN(fmt, ...) \
  271. _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
  272. #define DRM_INFO_ONCE(fmt, ...) \
  273. _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
  274. #define DRM_NOTE_ONCE(fmt, ...) \
  275. _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
  276. #define DRM_WARN_ONCE(fmt, ...) \
  277. _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
  278. /**
  279. * Error output.
  280. *
  281. * @dev: device pointer
  282. * @fmt: printf() like format string.
  283. */
  284. #define DRM_DEV_ERROR(dev, fmt, ...) \
  285. drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__)
  286. #define DRM_ERROR(fmt, ...) \
  287. drm_err(fmt, ##__VA_ARGS__)
  288. /**
  289. * Rate limited error output. Like DRM_ERROR() but won't flood the log.
  290. *
  291. * @dev: device pointer
  292. * @fmt: printf() like format string.
  293. */
  294. #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
  295. ({ \
  296. static DEFINE_RATELIMIT_STATE(_rs, \
  297. DEFAULT_RATELIMIT_INTERVAL, \
  298. DEFAULT_RATELIMIT_BURST); \
  299. \
  300. if (__ratelimit(&_rs)) \
  301. DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
  302. })
  303. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  304. DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  305. #define DRM_DEV_INFO(dev, fmt, ...) \
  306. drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__)
  307. #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
  308. ({ \
  309. static bool __print_once __read_mostly; \
  310. if (!__print_once) { \
  311. __print_once = true; \
  312. DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
  313. } \
  314. })
  315. /**
  316. * Debug output.
  317. *
  318. * @dev: device pointer
  319. * @fmt: printf() like format string.
  320. */
  321. #define DRM_DEV_DEBUG(dev, fmt, ...) \
  322. drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  323. #define DRM_DEBUG(fmt, ...) \
  324. drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__)
  325. #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \
  326. drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  327. #define DRM_DEBUG_DRIVER(fmt, ...) \
  328. drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  329. #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \
  330. drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  331. #define DRM_DEBUG_KMS(fmt, ...) \
  332. drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__)
  333. #define DRM_DEV_DEBUG_PRIME(dev, fmt, ...) \
  334. drm_dev_dbg(dev, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  335. #define DRM_DEBUG_PRIME(fmt, ...) \
  336. drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  337. #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, ...) \
  338. drm_dev_dbg(dev, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  339. #define DRM_DEBUG_ATOMIC(fmt, ...) \
  340. drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  341. #define DRM_DEV_DEBUG_VBL(dev, fmt, ...) \
  342. drm_dev_dbg(dev, DRM_UT_VBL, fmt, ##__VA_ARGS__)
  343. #define DRM_DEBUG_VBL(fmt, ...) \
  344. drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__)
  345. #define DRM_DEBUG_LEASE(fmt, ...) \
  346. drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__)
  347. #define DRM_DEV_DEBUG_DP(dev, fmt, ...) \
  348. drm_dev_dbg(dev, DRM_UT_DP, fmt, ## __VA_ARGS__)
  349. #define DRM_DEBUG_DP(dev, fmt, ...) \
  350. drm_dbg(DRM_UT_DP, fmt, ## __VA_ARGS__)
  351. #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, category, fmt, ...) \
  352. ({ \
  353. static DEFINE_RATELIMIT_STATE(_rs, \
  354. DEFAULT_RATELIMIT_INTERVAL, \
  355. DEFAULT_RATELIMIT_BURST); \
  356. if (__ratelimit(&_rs)) \
  357. drm_dev_dbg(dev, category, fmt, ##__VA_ARGS__); \
  358. })
  359. /**
  360. * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
  361. *
  362. * @dev: device pointer
  363. * @fmt: printf() like format string.
  364. */
  365. #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, ...) \
  366. _DEV_DRM_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_CORE, \
  367. fmt, ##__VA_ARGS__)
  368. #define DRM_DEBUG_RATELIMITED(fmt, ...) \
  369. DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  370. #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, ...) \
  371. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_DRIVER, \
  372. fmt, ##__VA_ARGS__)
  373. #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, ...) \
  374. DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  375. #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, ...) \
  376. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_KMS, \
  377. fmt, ##__VA_ARGS__)
  378. #define DRM_DEBUG_KMS_RATELIMITED(fmt, ...) \
  379. DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  380. #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, ...) \
  381. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_PRIME, \
  382. fmt, ##__VA_ARGS__)
  383. #define DRM_DEBUG_PRIME_RATELIMITED(fmt, ...) \
  384. DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  385. #endif /* DRM_PRINT_H_ */