drm_print.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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/device.h>
  30. #include <linux/dynamic_debug.h>
  31. #include <drm/drm.h>
  32. #include <drm/drm_device.h>
  33. struct debugfs_regset32;
  34. struct drm_device;
  35. struct seq_file;
  36. /* Do *not* use outside of drm_print.[ch]! */
  37. extern unsigned long __drm_debug;
  38. /**
  39. * DOC: print
  40. *
  41. * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
  42. * debug code to be used for both debugfs and printk logging.
  43. *
  44. * For example::
  45. *
  46. * void log_some_info(struct drm_printer *p)
  47. * {
  48. * drm_printf(p, "foo=%d\n", foo);
  49. * drm_printf(p, "bar=%d\n", bar);
  50. * }
  51. *
  52. * #ifdef CONFIG_DEBUG_FS
  53. * void debugfs_show(struct seq_file *f)
  54. * {
  55. * struct drm_printer p = drm_seq_file_printer(f);
  56. * log_some_info(&p);
  57. * }
  58. * #endif
  59. *
  60. * void some_other_function(...)
  61. * {
  62. * struct drm_printer p = drm_info_printer(drm->dev);
  63. * log_some_info(&p);
  64. * }
  65. */
  66. /**
  67. * enum drm_debug_category - The DRM debug categories
  68. *
  69. * Each of the DRM debug logging macros use a specific category, and the logging
  70. * is filtered by the drm.debug module parameter. This enum specifies the values
  71. * for the interface.
  72. *
  73. * Each DRM_DEBUG_<CATEGORY> macro logs to DRM_UT_<CATEGORY> category, except
  74. * DRM_DEBUG() logs to DRM_UT_CORE.
  75. *
  76. * Enabling verbose debug messages is done through the drm.debug parameter, each
  77. * category being enabled by a bit:
  78. *
  79. * - drm.debug=0x1 will enable CORE messages
  80. * - drm.debug=0x2 will enable DRIVER messages
  81. * - drm.debug=0x3 will enable CORE and DRIVER messages
  82. * - ...
  83. * - drm.debug=0x1ff will enable all messages
  84. *
  85. * An interesting feature is that it's possible to enable verbose logging at
  86. * run-time by echoing the debug value in its sysfs node::
  87. *
  88. * # echo 0xf > /sys/module/drm/parameters/debug
  89. *
  90. */
  91. enum drm_debug_category {
  92. /* These names must match those in DYNAMIC_DEBUG_CLASSBITS */
  93. /**
  94. * @DRM_UT_CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c,
  95. * drm_memory.c, ...
  96. */
  97. DRM_UT_CORE,
  98. /**
  99. * @DRM_UT_DRIVER: Used in the vendor specific part of the driver: i915,
  100. * radeon, ... macro.
  101. */
  102. DRM_UT_DRIVER,
  103. /**
  104. * @DRM_UT_KMS: Used in the modesetting code.
  105. */
  106. DRM_UT_KMS,
  107. /**
  108. * @DRM_UT_PRIME: Used in the prime code.
  109. */
  110. DRM_UT_PRIME,
  111. /**
  112. * @DRM_UT_ATOMIC: Used in the atomic code.
  113. */
  114. DRM_UT_ATOMIC,
  115. /**
  116. * @DRM_UT_VBL: Used for verbose debug message in the vblank code.
  117. */
  118. DRM_UT_VBL,
  119. /**
  120. * @DRM_UT_STATE: Used for verbose atomic state debugging.
  121. */
  122. DRM_UT_STATE,
  123. /**
  124. * @DRM_UT_LEASE: Used in the lease code.
  125. */
  126. DRM_UT_LEASE,
  127. /**
  128. * @DRM_UT_DP: Used in the DP code.
  129. */
  130. DRM_UT_DP,
  131. /**
  132. * @DRM_UT_DRMRES: Used in the drm managed resources code.
  133. */
  134. DRM_UT_DRMRES
  135. };
  136. static inline bool drm_debug_enabled_raw(enum drm_debug_category category)
  137. {
  138. return unlikely(__drm_debug & BIT(category));
  139. }
  140. #define drm_debug_enabled_instrumented(category) \
  141. ({ \
  142. pr_debug("todo: is this frequent enough to optimize ?\n"); \
  143. drm_debug_enabled_raw(category); \
  144. })
  145. #if defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
  146. /*
  147. * the drm.debug API uses dyndbg, so each drm_*dbg macro/callsite gets
  148. * a descriptor, and only enabled callsites are reachable. They use
  149. * the private macro to avoid re-testing the enable-bit.
  150. */
  151. #define __drm_debug_enabled(category) true
  152. #define drm_debug_enabled(category) drm_debug_enabled_instrumented(category)
  153. #else
  154. #define __drm_debug_enabled(category) drm_debug_enabled_raw(category)
  155. #define drm_debug_enabled(category) drm_debug_enabled_raw(category)
  156. #endif
  157. /**
  158. * struct drm_printer - drm output "stream"
  159. *
  160. * Do not use struct members directly. Use drm_printer_seq_file(),
  161. * drm_printer_info(), etc to initialize. And drm_printf() for output.
  162. */
  163. struct drm_printer {
  164. /* private: */
  165. void (*printfn)(struct drm_printer *p, struct va_format *vaf);
  166. void (*puts)(struct drm_printer *p, const char *str);
  167. void *arg;
  168. const void *origin;
  169. const char *prefix;
  170. struct {
  171. unsigned int series;
  172. unsigned int counter;
  173. } line;
  174. enum drm_debug_category category;
  175. };
  176. void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf);
  177. void __drm_puts_coredump(struct drm_printer *p, const char *str);
  178. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
  179. void __drm_puts_seq_file(struct drm_printer *p, const char *str);
  180. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
  181. void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
  182. void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
  183. void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
  184. __printf(2, 3)
  185. void drm_printf(struct drm_printer *p, const char *f, ...);
  186. void drm_puts(struct drm_printer *p, const char *str);
  187. void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset);
  188. void drm_print_bits(struct drm_printer *p, unsigned long value,
  189. const char * const bits[], unsigned int nbits);
  190. void drm_print_hex_dump(struct drm_printer *p, const char *prefix,
  191. const u8 *buf, size_t len);
  192. __printf(2, 0)
  193. /**
  194. * drm_vprintf - print to a &drm_printer stream
  195. * @p: the &drm_printer
  196. * @fmt: format string
  197. * @va: the va_list
  198. */
  199. static inline void
  200. drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
  201. {
  202. struct va_format vaf = { .fmt = fmt, .va = va };
  203. p->printfn(p, &vaf);
  204. }
  205. /**
  206. * drm_printf_indent - Print to a &drm_printer stream with indentation
  207. * @printer: DRM printer
  208. * @indent: Tab indentation level (max 5)
  209. * @fmt: Format string
  210. */
  211. #define drm_printf_indent(printer, indent, fmt, ...) \
  212. drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
  213. /**
  214. * struct drm_print_iterator - local struct used with drm_printer_coredump
  215. * @data: Pointer to the devcoredump output buffer, can be NULL if using
  216. * drm_printer_coredump to determine size of devcoredump
  217. * @start: The offset within the buffer to start writing
  218. * @remain: The number of bytes to write for this iteration
  219. */
  220. struct drm_print_iterator {
  221. void *data;
  222. ssize_t start;
  223. ssize_t remain;
  224. /* private: */
  225. ssize_t offset;
  226. };
  227. /**
  228. * drm_coredump_printer - construct a &drm_printer that can output to a buffer
  229. * from the read function for devcoredump
  230. * @iter: A pointer to a struct drm_print_iterator for the read instance
  231. *
  232. * This wrapper extends drm_printf() to work with a dev_coredumpm() callback
  233. * function. The passed in drm_print_iterator struct contains the buffer
  234. * pointer, size and offset as passed in from devcoredump.
  235. *
  236. * For example::
  237. *
  238. * void coredump_read(char *buffer, loff_t offset, size_t count,
  239. * void *data, size_t datalen)
  240. * {
  241. * struct drm_print_iterator iter;
  242. * struct drm_printer p;
  243. *
  244. * iter.data = buffer;
  245. * iter.start = offset;
  246. * iter.remain = count;
  247. *
  248. * p = drm_coredump_printer(&iter);
  249. *
  250. * drm_printf(p, "foo=%d\n", foo);
  251. * }
  252. *
  253. * void makecoredump(...)
  254. * {
  255. * ...
  256. * dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL,
  257. * coredump_read, ...)
  258. * }
  259. *
  260. * The above example has a time complexity of O(N^2), where N is the size of the
  261. * devcoredump. This is acceptable for small devcoredumps but scales poorly for
  262. * larger ones.
  263. *
  264. * Another use case for drm_coredump_printer is to capture the devcoredump into
  265. * a saved buffer before the dev_coredump() callback. This involves two passes:
  266. * one to determine the size of the devcoredump and another to print it to a
  267. * buffer. Then, in dev_coredump(), copy from the saved buffer into the
  268. * devcoredump read buffer.
  269. *
  270. * For example::
  271. *
  272. * char *devcoredump_saved_buffer;
  273. *
  274. * ssize_t __coredump_print(char *buffer, ssize_t count, ...)
  275. * {
  276. * struct drm_print_iterator iter;
  277. * struct drm_printer p;
  278. *
  279. * iter.data = buffer;
  280. * iter.start = 0;
  281. * iter.remain = count;
  282. *
  283. * p = drm_coredump_printer(&iter);
  284. *
  285. * drm_printf(p, "foo=%d\n", foo);
  286. * ...
  287. * return count - iter.remain;
  288. * }
  289. *
  290. * void coredump_print(...)
  291. * {
  292. * ssize_t count;
  293. *
  294. * count = __coredump_print(NULL, INT_MAX, ...);
  295. * devcoredump_saved_buffer = kvmalloc(count, GFP_KERNEL);
  296. * __coredump_print(devcoredump_saved_buffer, count, ...);
  297. * }
  298. *
  299. * void coredump_read(char *buffer, loff_t offset, size_t count,
  300. * void *data, size_t datalen)
  301. * {
  302. * ...
  303. * memcpy(buffer, devcoredump_saved_buffer + offset, count);
  304. * ...
  305. * }
  306. *
  307. * The above example has a time complexity of O(N*2), where N is the size of the
  308. * devcoredump. This scales better than the previous example for larger
  309. * devcoredumps.
  310. *
  311. * RETURNS:
  312. * The &drm_printer object
  313. */
  314. static inline struct drm_printer
  315. drm_coredump_printer(struct drm_print_iterator *iter)
  316. {
  317. struct drm_printer p = {
  318. .printfn = __drm_printfn_coredump,
  319. .puts = __drm_puts_coredump,
  320. .arg = iter,
  321. };
  322. /* Set the internal offset of the iterator to zero */
  323. iter->offset = 0;
  324. return p;
  325. }
  326. /**
  327. * drm_coredump_printer_is_full() - DRM coredump printer output is full
  328. * @p: DRM coredump printer
  329. *
  330. * DRM printer output is full, useful to short circuit coredump printing once
  331. * printer is full.
  332. *
  333. * RETURNS:
  334. * True if DRM coredump printer output buffer is full, False otherwise
  335. */
  336. static inline bool drm_coredump_printer_is_full(struct drm_printer *p)
  337. {
  338. struct drm_print_iterator *iterator = p->arg;
  339. if (p->printfn != __drm_printfn_coredump)
  340. return true;
  341. return !iterator->remain;
  342. }
  343. /**
  344. * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
  345. * @f: the &struct seq_file to output to
  346. *
  347. * RETURNS:
  348. * The &drm_printer object
  349. */
  350. static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
  351. {
  352. struct drm_printer p = {
  353. .printfn = __drm_printfn_seq_file,
  354. .puts = __drm_puts_seq_file,
  355. .arg = f,
  356. };
  357. return p;
  358. }
  359. /**
  360. * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
  361. * @dev: the &struct device pointer
  362. *
  363. * RETURNS:
  364. * The &drm_printer object
  365. */
  366. static inline struct drm_printer drm_info_printer(struct device *dev)
  367. {
  368. struct drm_printer p = {
  369. .printfn = __drm_printfn_info,
  370. .arg = dev,
  371. };
  372. return p;
  373. }
  374. /**
  375. * drm_dbg_printer - construct a &drm_printer for drm device specific output
  376. * @drm: the &struct drm_device pointer, or NULL
  377. * @category: the debug category to use
  378. * @prefix: debug output prefix, or NULL for no prefix
  379. *
  380. * RETURNS:
  381. * The &drm_printer object
  382. */
  383. static inline struct drm_printer drm_dbg_printer(struct drm_device *drm,
  384. enum drm_debug_category category,
  385. const char *prefix)
  386. {
  387. struct drm_printer p = {
  388. .printfn = __drm_printfn_dbg,
  389. .arg = drm,
  390. .origin = (const void *)_THIS_IP_, /* it's fine as we will be inlined */
  391. .prefix = prefix,
  392. .category = category,
  393. };
  394. return p;
  395. }
  396. /**
  397. * drm_err_printer - construct a &drm_printer that outputs to drm_err()
  398. * @drm: the &struct drm_device pointer
  399. * @prefix: debug output prefix, or NULL for no prefix
  400. *
  401. * RETURNS:
  402. * The &drm_printer object
  403. */
  404. static inline struct drm_printer drm_err_printer(struct drm_device *drm,
  405. const char *prefix)
  406. {
  407. struct drm_printer p = {
  408. .printfn = __drm_printfn_err,
  409. .arg = drm,
  410. .prefix = prefix
  411. };
  412. return p;
  413. }
  414. /**
  415. * drm_line_printer - construct a &drm_printer that prefixes outputs with line numbers
  416. * @p: the &struct drm_printer which actually generates the output
  417. * @prefix: optional output prefix, or NULL for no prefix
  418. * @series: optional unique series identifier, or 0 to omit identifier in the output
  419. *
  420. * This printer can be used to increase the robustness of the captured output
  421. * to make sure we didn't lost any intermediate lines of the output. Helpful
  422. * while capturing some crash data.
  423. *
  424. * Example 1::
  425. *
  426. * void crash_dump(struct drm_device *drm)
  427. * {
  428. * static unsigned int id;
  429. * struct drm_printer p = drm_err_printer(drm, "crash");
  430. * struct drm_printer lp = drm_line_printer(&p, "dump", ++id);
  431. *
  432. * drm_printf(&lp, "foo");
  433. * drm_printf(&lp, "bar");
  434. * }
  435. *
  436. * Above code will print into the dmesg something like::
  437. *
  438. * [ ] 0000:00:00.0: [drm] *ERROR* crash dump 1.1: foo
  439. * [ ] 0000:00:00.0: [drm] *ERROR* crash dump 1.2: bar
  440. *
  441. * Example 2::
  442. *
  443. * void line_dump(struct device *dev)
  444. * {
  445. * struct drm_printer p = drm_info_printer(dev);
  446. * struct drm_printer lp = drm_line_printer(&p, NULL, 0);
  447. *
  448. * drm_printf(&lp, "foo");
  449. * drm_printf(&lp, "bar");
  450. * }
  451. *
  452. * Above code will print::
  453. *
  454. * [ ] 0000:00:00.0: [drm] 1: foo
  455. * [ ] 0000:00:00.0: [drm] 2: bar
  456. *
  457. * RETURNS:
  458. * The &drm_printer object
  459. */
  460. static inline struct drm_printer drm_line_printer(struct drm_printer *p,
  461. const char *prefix,
  462. unsigned int series)
  463. {
  464. struct drm_printer lp = {
  465. .printfn = __drm_printfn_line,
  466. .arg = p,
  467. .prefix = prefix,
  468. .line = { .series = series, },
  469. };
  470. return lp;
  471. }
  472. /*
  473. * struct device based logging
  474. *
  475. * Prefer drm_device based logging over device or printk based logging.
  476. */
  477. __printf(3, 4)
  478. void drm_dev_printk(const struct device *dev, const char *level,
  479. const char *format, ...);
  480. struct _ddebug;
  481. __printf(4, 5)
  482. void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
  483. enum drm_debug_category category, const char *format, ...);
  484. /**
  485. * DRM_DEV_ERROR() - Error output.
  486. *
  487. * NOTE: this is deprecated in favor of drm_err() or dev_err().
  488. *
  489. * @dev: device pointer
  490. * @fmt: printf() like format string.
  491. */
  492. #define DRM_DEV_ERROR(dev, fmt, ...) \
  493. drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__)
  494. /**
  495. * DRM_DEV_ERROR_RATELIMITED() - Rate limited error output.
  496. *
  497. * NOTE: this is deprecated in favor of drm_err_ratelimited() or
  498. * dev_err_ratelimited().
  499. *
  500. * @dev: device pointer
  501. * @fmt: printf() like format string.
  502. *
  503. * Like DRM_ERROR() but won't flood the log.
  504. */
  505. #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
  506. ({ \
  507. static DEFINE_RATELIMIT_STATE(_rs, \
  508. DEFAULT_RATELIMIT_INTERVAL, \
  509. DEFAULT_RATELIMIT_BURST); \
  510. \
  511. if (__ratelimit(&_rs)) \
  512. DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
  513. })
  514. /* NOTE: this is deprecated in favor of drm_info() or dev_info(). */
  515. #define DRM_DEV_INFO(dev, fmt, ...) \
  516. drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__)
  517. /* NOTE: this is deprecated in favor of drm_info_once() or dev_info_once(). */
  518. #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
  519. ({ \
  520. static bool __print_once __read_mostly; \
  521. if (!__print_once) { \
  522. __print_once = true; \
  523. DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
  524. } \
  525. })
  526. #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
  527. #define drm_dev_dbg(dev, cat, fmt, ...) \
  528. __drm_dev_dbg(NULL, dev, cat, fmt, ##__VA_ARGS__)
  529. #else
  530. #define drm_dev_dbg(dev, cat, fmt, ...) \
  531. _dynamic_func_call_cls(cat, fmt, __drm_dev_dbg, \
  532. dev, cat, fmt, ##__VA_ARGS__)
  533. #endif
  534. /**
  535. * DRM_DEV_DEBUG() - Debug output for generic drm code
  536. *
  537. * NOTE: this is deprecated in favor of drm_dbg_core().
  538. *
  539. * @dev: device pointer
  540. * @fmt: printf() like format string.
  541. */
  542. #define DRM_DEV_DEBUG(dev, fmt, ...) \
  543. drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  544. /**
  545. * DRM_DEV_DEBUG_DRIVER() - Debug output for vendor specific part of the driver
  546. *
  547. * NOTE: this is deprecated in favor of drm_dbg() or dev_dbg().
  548. *
  549. * @dev: device pointer
  550. * @fmt: printf() like format string.
  551. */
  552. #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \
  553. drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  554. /**
  555. * DRM_DEV_DEBUG_KMS() - Debug output for modesetting code
  556. *
  557. * NOTE: this is deprecated in favor of drm_dbg_kms().
  558. *
  559. * @dev: device pointer
  560. * @fmt: printf() like format string.
  561. */
  562. #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \
  563. drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  564. /*
  565. * struct drm_device based logging
  566. *
  567. * Prefer drm_device based logging over device or prink based logging.
  568. */
  569. /* Helper to enforce struct drm_device type */
  570. static inline struct device *__drm_to_dev(const struct drm_device *drm)
  571. {
  572. return drm ? drm->dev : NULL;
  573. }
  574. /* Helper for struct drm_device based logging. */
  575. #define __drm_printk(drm, level, type, fmt, ...) \
  576. dev_##level##type(__drm_to_dev(drm), "[drm] " fmt, ##__VA_ARGS__)
  577. #define drm_info(drm, fmt, ...) \
  578. __drm_printk((drm), info,, fmt, ##__VA_ARGS__)
  579. #define drm_notice(drm, fmt, ...) \
  580. __drm_printk((drm), notice,, fmt, ##__VA_ARGS__)
  581. #define drm_warn(drm, fmt, ...) \
  582. __drm_printk((drm), warn,, fmt, ##__VA_ARGS__)
  583. #define drm_err(drm, fmt, ...) \
  584. __drm_printk((drm), err,, "*ERROR* " fmt, ##__VA_ARGS__)
  585. #define drm_info_once(drm, fmt, ...) \
  586. __drm_printk((drm), info, _once, fmt, ##__VA_ARGS__)
  587. #define drm_notice_once(drm, fmt, ...) \
  588. __drm_printk((drm), notice, _once, fmt, ##__VA_ARGS__)
  589. #define drm_warn_once(drm, fmt, ...) \
  590. __drm_printk((drm), warn, _once, fmt, ##__VA_ARGS__)
  591. #define drm_err_once(drm, fmt, ...) \
  592. __drm_printk((drm), err, _once, "*ERROR* " fmt, ##__VA_ARGS__)
  593. #define drm_err_ratelimited(drm, fmt, ...) \
  594. __drm_printk((drm), err, _ratelimited, "*ERROR* " fmt, ##__VA_ARGS__)
  595. #define drm_dbg_core(drm, fmt, ...) \
  596. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_CORE, fmt, ##__VA_ARGS__)
  597. #define drm_dbg_driver(drm, fmt, ...) \
  598. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  599. #define drm_dbg_kms(drm, fmt, ...) \
  600. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_KMS, fmt, ##__VA_ARGS__)
  601. #define drm_dbg_prime(drm, fmt, ...) \
  602. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  603. #define drm_dbg_atomic(drm, fmt, ...) \
  604. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  605. #define drm_dbg_vbl(drm, fmt, ...) \
  606. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_VBL, fmt, ##__VA_ARGS__)
  607. #define drm_dbg_state(drm, fmt, ...) \
  608. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_STATE, fmt, ##__VA_ARGS__)
  609. #define drm_dbg_lease(drm, fmt, ...) \
  610. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_LEASE, fmt, ##__VA_ARGS__)
  611. #define drm_dbg_dp(drm, fmt, ...) \
  612. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_DP, fmt, ##__VA_ARGS__)
  613. #define drm_dbg_drmres(drm, fmt, ...) \
  614. drm_dev_dbg(__drm_to_dev(drm), DRM_UT_DRMRES, fmt, ##__VA_ARGS__)
  615. #define drm_dbg(drm, fmt, ...) drm_dbg_driver(drm, fmt, ##__VA_ARGS__)
  616. /*
  617. * printk based logging
  618. *
  619. * Prefer drm_device based logging over device or prink based logging.
  620. */
  621. __printf(1, 2)
  622. void __drm_err(const char *format, ...);
  623. #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
  624. #define __drm_dbg(cat, fmt, ...) __drm_dev_dbg(NULL, NULL, cat, fmt, ##__VA_ARGS__)
  625. #else
  626. #define __drm_dbg(cat, fmt, ...) \
  627. _dynamic_func_call_cls(cat, fmt, __drm_dev_dbg, \
  628. NULL, cat, fmt, ##__VA_ARGS__)
  629. #endif
  630. /* Macros to make printk easier */
  631. #define _DRM_PRINTK(once, level, fmt, ...) \
  632. printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
  633. /* NOTE: this is deprecated in favor of pr_info(). */
  634. #define DRM_INFO(fmt, ...) \
  635. _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
  636. /* NOTE: this is deprecated in favor of pr_notice(). */
  637. #define DRM_NOTE(fmt, ...) \
  638. _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
  639. /* NOTE: this is deprecated in favor of pr_warn(). */
  640. #define DRM_WARN(fmt, ...) \
  641. _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
  642. /* NOTE: this is deprecated in favor of pr_info_once(). */
  643. #define DRM_INFO_ONCE(fmt, ...) \
  644. _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
  645. /* NOTE: this is deprecated in favor of pr_notice_once(). */
  646. #define DRM_NOTE_ONCE(fmt, ...) \
  647. _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
  648. /* NOTE: this is deprecated in favor of pr_warn_once(). */
  649. #define DRM_WARN_ONCE(fmt, ...) \
  650. _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
  651. /* NOTE: this is deprecated in favor of pr_err(). */
  652. #define DRM_ERROR(fmt, ...) \
  653. __drm_err(fmt, ##__VA_ARGS__)
  654. /* NOTE: this is deprecated in favor of pr_err_ratelimited(). */
  655. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  656. DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  657. /* NOTE: this is deprecated in favor of drm_dbg_core(NULL, ...). */
  658. #define DRM_DEBUG(fmt, ...) \
  659. __drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__)
  660. /* NOTE: this is deprecated in favor of drm_dbg(NULL, ...). */
  661. #define DRM_DEBUG_DRIVER(fmt, ...) \
  662. __drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  663. /* NOTE: this is deprecated in favor of drm_dbg_kms(NULL, ...). */
  664. #define DRM_DEBUG_KMS(fmt, ...) \
  665. __drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__)
  666. /* NOTE: this is deprecated in favor of drm_dbg_prime(NULL, ...). */
  667. #define DRM_DEBUG_PRIME(fmt, ...) \
  668. __drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  669. /* NOTE: this is deprecated in favor of drm_dbg_atomic(NULL, ...). */
  670. #define DRM_DEBUG_ATOMIC(fmt, ...) \
  671. __drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  672. /* NOTE: this is deprecated in favor of drm_dbg_vbl(NULL, ...). */
  673. #define DRM_DEBUG_VBL(fmt, ...) \
  674. __drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__)
  675. /* NOTE: this is deprecated in favor of drm_dbg_lease(NULL, ...). */
  676. #define DRM_DEBUG_LEASE(fmt, ...) \
  677. __drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__)
  678. /* NOTE: this is deprecated in favor of drm_dbg_dp(NULL, ...). */
  679. #define DRM_DEBUG_DP(fmt, ...) \
  680. __drm_dbg(DRM_UT_DP, fmt, ## __VA_ARGS__)
  681. #define __DRM_DEFINE_DBG_RATELIMITED(category, drm, fmt, ...) \
  682. ({ \
  683. static DEFINE_RATELIMIT_STATE(rs_, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);\
  684. \
  685. if (drm_debug_enabled(DRM_UT_ ## category) && __ratelimit(&rs_)) \
  686. drm_dev_printk(__drm_to_dev(drm), KERN_DEBUG, fmt, ## __VA_ARGS__); \
  687. })
  688. #define drm_dbg_ratelimited(drm, fmt, ...) \
  689. __DRM_DEFINE_DBG_RATELIMITED(DRIVER, drm, fmt, ## __VA_ARGS__)
  690. #define drm_dbg_kms_ratelimited(drm, fmt, ...) \
  691. __DRM_DEFINE_DBG_RATELIMITED(KMS, drm, fmt, ## __VA_ARGS__)
  692. /*
  693. * struct drm_device based WARNs
  694. *
  695. * drm_WARN*() acts like WARN*(), but with the key difference of
  696. * using device specific information so that we know from which device
  697. * warning is originating from.
  698. *
  699. * Prefer drm_device based drm_WARN* over regular WARN*
  700. */
  701. /* Helper for struct drm_device based WARNs */
  702. #define drm_WARN(drm, condition, format, arg...) \
  703. WARN(condition, "%s %s: [drm] " format, \
  704. dev_driver_string(__drm_to_dev(drm)), \
  705. dev_name(__drm_to_dev(drm)), ## arg)
  706. #define drm_WARN_ONCE(drm, condition, format, arg...) \
  707. WARN_ONCE(condition, "%s %s: [drm] " format, \
  708. dev_driver_string(__drm_to_dev(drm)), \
  709. dev_name(__drm_to_dev(drm)), ## arg)
  710. #define drm_WARN_ON(drm, x) \
  711. drm_WARN((drm), (x), "%s", \
  712. "drm_WARN_ON(" __stringify(x) ")")
  713. #define drm_WARN_ON_ONCE(drm, x) \
  714. drm_WARN_ONCE((drm), (x), "%s", \
  715. "drm_WARN_ON_ONCE(" __stringify(x) ")")
  716. #endif /* DRM_PRINT_H_ */