drm_print.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. #include <linux/debugfs.h>
  32. #include <drm/drm.h>
  33. /* Do *not* use outside of drm_print.[ch]! */
  34. extern unsigned int __drm_debug;
  35. /**
  36. * DOC: print
  37. *
  38. * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
  39. * debug code to be used for both debugfs and printk logging.
  40. *
  41. * For example::
  42. *
  43. * void log_some_info(struct drm_printer *p)
  44. * {
  45. * drm_printf(p, "foo=%d\n", foo);
  46. * drm_printf(p, "bar=%d\n", bar);
  47. * }
  48. *
  49. * #ifdef CONFIG_DEBUG_FS
  50. * void debugfs_show(struct seq_file *f)
  51. * {
  52. * struct drm_printer p = drm_seq_file_printer(f);
  53. * log_some_info(&p);
  54. * }
  55. * #endif
  56. *
  57. * void some_other_function(...)
  58. * {
  59. * struct drm_printer p = drm_info_printer(drm->dev);
  60. * log_some_info(&p);
  61. * }
  62. */
  63. /**
  64. * struct drm_printer - drm output "stream"
  65. *
  66. * Do not use struct members directly. Use drm_printer_seq_file(),
  67. * drm_printer_info(), etc to initialize. And drm_printf() for output.
  68. */
  69. struct drm_printer {
  70. /* private: */
  71. void (*printfn)(struct drm_printer *p, struct va_format *vaf);
  72. void (*puts)(struct drm_printer *p, const char *str);
  73. void *arg;
  74. const char *prefix;
  75. };
  76. void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf);
  77. void __drm_puts_coredump(struct drm_printer *p, const char *str);
  78. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
  79. void __drm_puts_seq_file(struct drm_printer *p, const char *str);
  80. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
  81. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
  82. void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
  83. __printf(2, 3)
  84. void drm_printf(struct drm_printer *p, const char *f, ...);
  85. void drm_puts(struct drm_printer *p, const char *str);
  86. void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset);
  87. void drm_print_bits(struct drm_printer *p, unsigned long value,
  88. const char * const bits[], unsigned int nbits);
  89. __printf(2, 0)
  90. /**
  91. * drm_vprintf - print to a &drm_printer stream
  92. * @p: the &drm_printer
  93. * @fmt: format string
  94. * @va: the va_list
  95. */
  96. static inline void
  97. drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
  98. {
  99. struct va_format vaf = { .fmt = fmt, .va = va };
  100. p->printfn(p, &vaf);
  101. }
  102. /**
  103. * drm_printf_indent - Print to a &drm_printer stream with indentation
  104. * @printer: DRM printer
  105. * @indent: Tab indentation level (max 5)
  106. * @fmt: Format string
  107. */
  108. #define drm_printf_indent(printer, indent, fmt, ...) \
  109. drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
  110. /**
  111. * struct drm_print_iterator - local struct used with drm_printer_coredump
  112. * @data: Pointer to the devcoredump output buffer
  113. * @start: The offset within the buffer to start writing
  114. * @remain: The number of bytes to write for this iteration
  115. */
  116. struct drm_print_iterator {
  117. void *data;
  118. ssize_t start;
  119. ssize_t remain;
  120. /* private: */
  121. ssize_t offset;
  122. };
  123. /**
  124. * drm_coredump_printer - construct a &drm_printer that can output to a buffer
  125. * from the read function for devcoredump
  126. * @iter: A pointer to a struct drm_print_iterator for the read instance
  127. *
  128. * This wrapper extends drm_printf() to work with a dev_coredumpm() callback
  129. * function. The passed in drm_print_iterator struct contains the buffer
  130. * pointer, size and offset as passed in from devcoredump.
  131. *
  132. * For example::
  133. *
  134. * void coredump_read(char *buffer, loff_t offset, size_t count,
  135. * void *data, size_t datalen)
  136. * {
  137. * struct drm_print_iterator iter;
  138. * struct drm_printer p;
  139. *
  140. * iter.data = buffer;
  141. * iter.start = offset;
  142. * iter.remain = count;
  143. *
  144. * p = drm_coredump_printer(&iter);
  145. *
  146. * drm_printf(p, "foo=%d\n", foo);
  147. * }
  148. *
  149. * void makecoredump(...)
  150. * {
  151. * ...
  152. * dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL,
  153. * coredump_read, ...)
  154. * }
  155. *
  156. * RETURNS:
  157. * The &drm_printer object
  158. */
  159. static inline struct drm_printer
  160. drm_coredump_printer(struct drm_print_iterator *iter)
  161. {
  162. struct drm_printer p = {
  163. .printfn = __drm_printfn_coredump,
  164. .puts = __drm_puts_coredump,
  165. .arg = iter,
  166. };
  167. /* Set the internal offset of the iterator to zero */
  168. iter->offset = 0;
  169. return p;
  170. }
  171. /**
  172. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  173. * @f: the &struct seq_file to output to
  174. *
  175. * RETURNS:
  176. * The &drm_printer object
  177. */
  178. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  179. {
  180. struct drm_printer p = {
  181. .printfn = __drm_printfn_seq_file,
  182. .puts = __drm_puts_seq_file,
  183. .arg = f,
  184. };
  185. return p;
  186. }
  187. /**
  188. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  189. * @dev: the &struct device pointer
  190. *
  191. * RETURNS:
  192. * The &drm_printer object
  193. */
  194. static inline struct drm_printer drm_info_printer(struct device *dev)
  195. {
  196. struct drm_printer p = {
  197. .printfn = __drm_printfn_info,
  198. .arg = dev,
  199. };
  200. return p;
  201. }
  202. /**
  203. * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
  204. * @prefix: debug output prefix
  205. *
  206. * RETURNS:
  207. * The &drm_printer object
  208. */
  209. static inline struct drm_printer drm_debug_printer(const char *prefix)
  210. {
  211. struct drm_printer p = {
  212. .printfn = __drm_printfn_debug,
  213. .prefix = prefix
  214. };
  215. return p;
  216. }
  217. /**
  218. * drm_err_printer - construct a &drm_printer that outputs to pr_err()
  219. * @prefix: debug output prefix
  220. *
  221. * RETURNS:
  222. * The &drm_printer object
  223. */
  224. static inline struct drm_printer drm_err_printer(const char *prefix)
  225. {
  226. struct drm_printer p = {
  227. .printfn = __drm_printfn_err,
  228. .prefix = prefix
  229. };
  230. return p;
  231. }
  232. /**
  233. * enum drm_debug_category - The DRM debug categories
  234. *
  235. * Each of the DRM debug logging macros use a specific category, and the logging
  236. * is filtered by the drm.debug module parameter. This enum specifies the values
  237. * for the interface.
  238. *
  239. * Each DRM_DEBUG_<CATEGORY> macro logs to DRM_UT_<CATEGORY> category, except
  240. * DRM_DEBUG() logs to DRM_UT_CORE.
  241. *
  242. * Enabling verbose debug messages is done through the drm.debug parameter, each
  243. * category being enabled by a bit:
  244. *
  245. * - drm.debug=0x1 will enable CORE messages
  246. * - drm.debug=0x2 will enable DRIVER messages
  247. * - drm.debug=0x3 will enable CORE and DRIVER messages
  248. * - ...
  249. * - drm.debug=0x1ff will enable all messages
  250. *
  251. * An interesting feature is that it's possible to enable verbose logging at
  252. * run-time by echoing the debug value in its sysfs node::
  253. *
  254. * # echo 0xf > /sys/module/drm/parameters/debug
  255. *
  256. */
  257. enum drm_debug_category {
  258. /**
  259. * @DRM_UT_CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c,
  260. * drm_memory.c, ...
  261. */
  262. DRM_UT_CORE = 0x01,
  263. /**
  264. * @DRM_UT_DRIVER: Used in the vendor specific part of the driver: i915,
  265. * radeon, ... macro.
  266. */
  267. DRM_UT_DRIVER = 0x02,
  268. /**
  269. * @DRM_UT_KMS: Used in the modesetting code.
  270. */
  271. DRM_UT_KMS = 0x04,
  272. /**
  273. * @DRM_UT_PRIME: Used in the prime code.
  274. */
  275. DRM_UT_PRIME = 0x08,
  276. /**
  277. * @DRM_UT_ATOMIC: Used in the atomic code.
  278. */
  279. DRM_UT_ATOMIC = 0x10,
  280. /**
  281. * @DRM_UT_VBL: Used for verbose debug message in the vblank code.
  282. */
  283. DRM_UT_VBL = 0x20,
  284. /**
  285. * @DRM_UT_STATE: Used for verbose atomic state debugging.
  286. */
  287. DRM_UT_STATE = 0x40,
  288. /**
  289. * @DRM_UT_LEASE: Used in the lease code.
  290. */
  291. DRM_UT_LEASE = 0x80,
  292. /**
  293. * @DRM_UT_DP: Used in the DP code.
  294. */
  295. DRM_UT_DP = 0x100,
  296. /**
  297. * @DRM_UT_DRMRES: Used in the drm managed resources code.
  298. */
  299. DRM_UT_DRMRES = 0x200,
  300. };
  301. static inline bool drm_debug_enabled(enum drm_debug_category category)
  302. {
  303. return unlikely(__drm_debug & category);
  304. }
  305. /*
  306. * struct device based logging
  307. *
  308. * Prefer drm_device based logging over device or prink based logging.
  309. */
  310. __printf(3, 4)
  311. void drm_dev_printk(const struct device *dev, const char *level,
  312. const char *format, ...);
  313. __printf(3, 4)
  314. void drm_dev_dbg(const struct device *dev, enum drm_debug_category category,
  315. const char *format, ...);
  316. /**
  317. * DRM_DEV_ERROR() - Error output.
  318. *
  319. * @dev: device pointer
  320. * @fmt: printf() like format string.
  321. */
  322. #define DRM_DEV_ERROR(dev, fmt, ...) \
  323. drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__)
  324. /**
  325. * DRM_DEV_ERROR_RATELIMITED() - Rate limited error output.
  326. *
  327. * @dev: device pointer
  328. * @fmt: printf() like format string.
  329. *
  330. * Like DRM_ERROR() but won't flood the log.
  331. */
  332. #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
  333. ({ \
  334. static DEFINE_RATELIMIT_STATE(_rs, \
  335. DEFAULT_RATELIMIT_INTERVAL, \
  336. DEFAULT_RATELIMIT_BURST); \
  337. \
  338. if (__ratelimit(&_rs)) \
  339. DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
  340. })
  341. #define DRM_DEV_INFO(dev, fmt, ...) \
  342. drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__)
  343. #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
  344. ({ \
  345. static bool __print_once __read_mostly; \
  346. if (!__print_once) { \
  347. __print_once = true; \
  348. DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
  349. } \
  350. })
  351. /**
  352. * DRM_DEV_DEBUG() - Debug output for generic drm code
  353. *
  354. * @dev: device pointer
  355. * @fmt: printf() like format string.
  356. */
  357. #define DRM_DEV_DEBUG(dev, fmt, ...) \
  358. drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  359. /**
  360. * DRM_DEV_DEBUG_DRIVER() - Debug output for vendor specific part of the driver
  361. *
  362. * @dev: device pointer
  363. * @fmt: printf() like format string.
  364. */
  365. #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \
  366. drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  367. /**
  368. * DRM_DEV_DEBUG_KMS() - Debug output for modesetting code
  369. *
  370. * @dev: device pointer
  371. * @fmt: printf() like format string.
  372. */
  373. #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \
  374. drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  375. /*
  376. * struct drm_device based logging
  377. *
  378. * Prefer drm_device based logging over device or prink based logging.
  379. */
  380. /* Helper for struct drm_device based logging. */
  381. #define __drm_printk(drm, level, type, fmt, ...) \
  382. dev_##level##type((drm)->dev, "[drm] " fmt, ##__VA_ARGS__)
  383. #define drm_info(drm, fmt, ...) \
  384. __drm_printk((drm), info,, fmt, ##__VA_ARGS__)
  385. #define drm_notice(drm, fmt, ...) \
  386. __drm_printk((drm), notice,, fmt, ##__VA_ARGS__)
  387. #define drm_warn(drm, fmt, ...) \
  388. __drm_printk((drm), warn,, fmt, ##__VA_ARGS__)
  389. #define drm_err(drm, fmt, ...) \
  390. __drm_printk((drm), err,, "*ERROR* " fmt, ##__VA_ARGS__)
  391. #define drm_info_once(drm, fmt, ...) \
  392. __drm_printk((drm), info, _once, fmt, ##__VA_ARGS__)
  393. #define drm_notice_once(drm, fmt, ...) \
  394. __drm_printk((drm), notice, _once, fmt, ##__VA_ARGS__)
  395. #define drm_warn_once(drm, fmt, ...) \
  396. __drm_printk((drm), warn, _once, fmt, ##__VA_ARGS__)
  397. #define drm_err_once(drm, fmt, ...) \
  398. __drm_printk((drm), err, _once, "*ERROR* " fmt, ##__VA_ARGS__)
  399. #define drm_err_ratelimited(drm, fmt, ...) \
  400. __drm_printk((drm), err, _ratelimited, "*ERROR* " fmt, ##__VA_ARGS__)
  401. #define drm_dbg_core(drm, fmt, ...) \
  402. drm_dev_dbg((drm)->dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  403. #define drm_dbg(drm, fmt, ...) \
  404. drm_dev_dbg((drm)->dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  405. #define drm_dbg_kms(drm, fmt, ...) \
  406. drm_dev_dbg((drm)->dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  407. #define drm_dbg_prime(drm, fmt, ...) \
  408. drm_dev_dbg((drm)->dev, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  409. #define drm_dbg_atomic(drm, fmt, ...) \
  410. drm_dev_dbg((drm)->dev, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  411. #define drm_dbg_vbl(drm, fmt, ...) \
  412. drm_dev_dbg((drm)->dev, DRM_UT_VBL, fmt, ##__VA_ARGS__)
  413. #define drm_dbg_state(drm, fmt, ...) \
  414. drm_dev_dbg((drm)->dev, DRM_UT_STATE, fmt, ##__VA_ARGS__)
  415. #define drm_dbg_lease(drm, fmt, ...) \
  416. drm_dev_dbg((drm)->dev, DRM_UT_LEASE, fmt, ##__VA_ARGS__)
  417. #define drm_dbg_dp(drm, fmt, ...) \
  418. drm_dev_dbg((drm)->dev, DRM_UT_DP, fmt, ##__VA_ARGS__)
  419. #define drm_dbg_drmres(drm, fmt, ...) \
  420. drm_dev_dbg((drm)->dev, DRM_UT_DRMRES, fmt, ##__VA_ARGS__)
  421. /*
  422. * printk based logging
  423. *
  424. * Prefer drm_device based logging over device or prink based logging.
  425. */
  426. __printf(2, 3)
  427. void __drm_dbg(enum drm_debug_category category, const char *format, ...);
  428. __printf(1, 2)
  429. void __drm_err(const char *format, ...);
  430. /* Macros to make printk easier */
  431. #define _DRM_PRINTK(once, level, fmt, ...) \
  432. printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
  433. #define DRM_INFO(fmt, ...) \
  434. _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
  435. #define DRM_NOTE(fmt, ...) \
  436. _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
  437. #define DRM_WARN(fmt, ...) \
  438. _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
  439. #define DRM_INFO_ONCE(fmt, ...) \
  440. _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
  441. #define DRM_NOTE_ONCE(fmt, ...) \
  442. _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
  443. #define DRM_WARN_ONCE(fmt, ...) \
  444. _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
  445. #define DRM_ERROR(fmt, ...) \
  446. __drm_err(fmt, ##__VA_ARGS__)
  447. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  448. DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  449. #define DRM_DEBUG(fmt, ...) \
  450. __drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__)
  451. #define DRM_DEBUG_DRIVER(fmt, ...) \
  452. __drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  453. #define DRM_DEBUG_KMS(fmt, ...) \
  454. __drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__)
  455. #define DRM_DEBUG_PRIME(fmt, ...) \
  456. __drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  457. #define DRM_DEBUG_ATOMIC(fmt, ...) \
  458. __drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  459. #define DRM_DEBUG_VBL(fmt, ...) \
  460. __drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__)
  461. #define DRM_DEBUG_LEASE(fmt, ...) \
  462. __drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__)
  463. #define DRM_DEBUG_DP(fmt, ...) \
  464. __drm_dbg(DRM_UT_DP, fmt, ## __VA_ARGS__)
  465. #define DRM_DEBUG_KMS_RATELIMITED(fmt, ...) \
  466. ({ \
  467. static DEFINE_RATELIMIT_STATE(_rs, \
  468. DEFAULT_RATELIMIT_INTERVAL, \
  469. DEFAULT_RATELIMIT_BURST); \
  470. if (__ratelimit(&_rs)) \
  471. drm_dev_dbg(NULL, DRM_UT_KMS, fmt, ##__VA_ARGS__); \
  472. })
  473. /*
  474. * struct drm_device based WARNs
  475. *
  476. * drm_WARN*() acts like WARN*(), but with the key difference of
  477. * using device specific information so that we know from which device
  478. * warning is originating from.
  479. *
  480. * Prefer drm_device based drm_WARN* over regular WARN*
  481. */
  482. /* Helper for struct drm_device based WARNs */
  483. #define drm_WARN(drm, condition, format, arg...) \
  484. WARN(condition, "%s %s: " format, \
  485. dev_driver_string((drm)->dev), \
  486. dev_name((drm)->dev), ## arg)
  487. #define drm_WARN_ONCE(drm, condition, format, arg...) \
  488. WARN_ONCE(condition, "%s %s: " format, \
  489. dev_driver_string((drm)->dev), \
  490. dev_name((drm)->dev), ## arg)
  491. #define drm_WARN_ON(drm, x) \
  492. drm_WARN((drm), (x), "%s", \
  493. "drm_WARN_ON(" __stringify(x) ")")
  494. #define drm_WARN_ON_ONCE(drm, x) \
  495. drm_WARN_ONCE((drm), (x), "%s", \
  496. "drm_WARN_ON_ONCE(" __stringify(x) ")")
  497. #endif /* DRM_PRINT_H_ */