2 * Copyright (C) 2016 Red Hat
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 * Rob Clark <robdclark@gmail.com>
29 #include <linux/compiler.h>
30 #include <linux/printk.h>
31 #include <linux/seq_file.h>
32 #include <linux/device.h>
33 #include <linux/debugfs.h>
34 #include <linux/dynamic_debug.h>
40 /* Do *not* use outside of drm_print.[ch]! */
41 extern unsigned long __drm_debug;
46 * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
47 * debug code to be used for both debugfs and printk logging.
51 * void log_some_info(struct drm_printer *p)
53 * drm_printf(p, "foo=%d\n", foo);
54 * drm_printf(p, "bar=%d\n", bar);
57 * #ifdef CONFIG_DEBUG_FS
58 * void debugfs_show(struct seq_file *f)
60 * struct drm_printer p = drm_seq_file_printer(f);
65 * void some_other_function(...)
67 * struct drm_printer p = drm_info_printer(drm->dev);
73 * enum drm_debug_category - The DRM debug categories
75 * Each of the DRM debug logging macros use a specific category, and the logging
76 * is filtered by the drm.debug module parameter. This enum specifies the values
79 * Each DRM_DEBUG_<CATEGORY> macro logs to DRM_UT_<CATEGORY> category, except
80 * DRM_DEBUG() logs to DRM_UT_CORE.
82 * Enabling verbose debug messages is done through the drm.debug parameter, each
83 * category being enabled by a bit:
85 * - drm.debug=0x1 will enable CORE messages
86 * - drm.debug=0x2 will enable DRIVER messages
87 * - drm.debug=0x3 will enable CORE and DRIVER messages
89 * - drm.debug=0x1ff will enable all messages
91 * An interesting feature is that it's possible to enable verbose logging at
92 * run-time by echoing the debug value in its sysfs node::
94 * # echo 0xf > /sys/module/drm/parameters/debug
97 enum drm_debug_category {
98 /* These names must match those in DYNAMIC_DEBUG_CLASSBITS */
100 * @DRM_UT_CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c,
105 * @DRM_UT_DRIVER: Used in the vendor specific part of the driver: i915,
110 * @DRM_UT_KMS: Used in the modesetting code.
114 * @DRM_UT_PRIME: Used in the prime code.
118 * @DRM_UT_ATOMIC: Used in the atomic code.
122 * @DRM_UT_VBL: Used for verbose debug message in the vblank code.
126 * @DRM_UT_STATE: Used for verbose atomic state debugging.
130 * @DRM_UT_LEASE: Used in the lease code.
134 * @DRM_UT_DP: Used in the DP code.
138 * @DRM_UT_DRMRES: Used in the drm managed resources code.
143 static inline bool drm_debug_enabled_raw(enum drm_debug_category category)
145 return unlikely(__drm_debug & BIT(category));
148 #define drm_debug_enabled_instrumented(category) \
150 pr_debug("todo: is this frequent enough to optimize ?\n"); \
151 drm_debug_enabled_raw(category); \
154 #if defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
156 * the drm.debug API uses dyndbg, so each drm_*dbg macro/callsite gets
157 * a descriptor, and only enabled callsites are reachable. They use
158 * the private macro to avoid re-testing the enable-bit.
160 #define __drm_debug_enabled(category) true
161 #define drm_debug_enabled(category) drm_debug_enabled_instrumented(category)
163 #define __drm_debug_enabled(category) drm_debug_enabled_raw(category)
164 #define drm_debug_enabled(category) drm_debug_enabled_raw(category)
168 * struct drm_printer - drm output "stream"
170 * Do not use struct members directly. Use drm_printer_seq_file(),
171 * drm_printer_info(), etc to initialize. And drm_printf() for output.
175 void (*printfn)(struct drm_printer *p, struct va_format *vaf);
176 void (*puts)(struct drm_printer *p, const char *str);
179 enum drm_debug_category category;
182 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf);
183 void __drm_puts_coredump(struct drm_printer *p, const char *str);
184 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
185 void __drm_puts_seq_file(struct drm_printer *p, const char *str);
186 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
187 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
188 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
191 void drm_printf(struct drm_printer *p, const char *f, ...);
192 void drm_puts(struct drm_printer *p, const char *str);
193 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset);
194 void drm_print_bits(struct drm_printer *p, unsigned long value,
195 const char * const bits[], unsigned int nbits);
199 * drm_vprintf - print to a &drm_printer stream
200 * @p: the &drm_printer
201 * @fmt: format string
205 drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
207 struct va_format vaf = { .fmt = fmt, .va = va };
213 * drm_printf_indent - Print to a &drm_printer stream with indentation
214 * @printer: DRM printer
215 * @indent: Tab indentation level (max 5)
216 * @fmt: Format string
218 #define drm_printf_indent(printer, indent, fmt, ...) \
219 drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
222 * struct drm_print_iterator - local struct used with drm_printer_coredump
223 * @data: Pointer to the devcoredump output buffer
224 * @start: The offset within the buffer to start writing
225 * @remain: The number of bytes to write for this iteration
227 struct drm_print_iterator {
236 * drm_coredump_printer - construct a &drm_printer that can output to a buffer
237 * from the read function for devcoredump
238 * @iter: A pointer to a struct drm_print_iterator for the read instance
240 * This wrapper extends drm_printf() to work with a dev_coredumpm() callback
241 * function. The passed in drm_print_iterator struct contains the buffer
242 * pointer, size and offset as passed in from devcoredump.
246 * void coredump_read(char *buffer, loff_t offset, size_t count,
247 * void *data, size_t datalen)
249 * struct drm_print_iterator iter;
250 * struct drm_printer p;
252 * iter.data = buffer;
253 * iter.start = offset;
254 * iter.remain = count;
256 * p = drm_coredump_printer(&iter);
258 * drm_printf(p, "foo=%d\n", foo);
261 * void makecoredump(...)
264 * dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL,
265 * coredump_read, ...)
269 * The &drm_printer object
271 static inline struct drm_printer
272 drm_coredump_printer(struct drm_print_iterator *iter)
274 struct drm_printer p = {
275 .printfn = __drm_printfn_coredump,
276 .puts = __drm_puts_coredump,
280 /* Set the internal offset of the iterator to zero */
287 * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
288 * @f: the &struct seq_file to output to
291 * The &drm_printer object
293 static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
295 struct drm_printer p = {
296 .printfn = __drm_printfn_seq_file,
297 .puts = __drm_puts_seq_file,
304 * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
305 * @dev: the &struct device pointer
308 * The &drm_printer object
310 static inline struct drm_printer drm_info_printer(struct device *dev)
312 struct drm_printer p = {
313 .printfn = __drm_printfn_info,
320 * drm_dbg_printer - construct a &drm_printer for drm device specific output
321 * @drm: the &struct drm_device pointer, or NULL
322 * @category: the debug category to use
323 * @prefix: debug output prefix, or NULL for no prefix
326 * The &drm_printer object
328 static inline struct drm_printer drm_dbg_printer(struct drm_device *drm,
329 enum drm_debug_category category,
332 struct drm_printer p = {
333 .printfn = __drm_printfn_dbg,
336 .category = category,
342 * drm_err_printer - construct a &drm_printer that outputs to drm_err()
343 * @drm: the &struct drm_device pointer
344 * @prefix: debug output prefix, or NULL for no prefix
347 * The &drm_printer object
349 static inline struct drm_printer drm_err_printer(struct drm_device *drm,
352 struct drm_printer p = {
353 .printfn = __drm_printfn_err,
361 * struct device based logging
363 * Prefer drm_device based logging over device or printk based logging.
367 void drm_dev_printk(const struct device *dev, const char *level,
368 const char *format, ...);
371 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
372 enum drm_debug_category category, const char *format, ...);
375 * DRM_DEV_ERROR() - Error output.
377 * NOTE: this is deprecated in favor of drm_err() or dev_err().
379 * @dev: device pointer
380 * @fmt: printf() like format string.
382 #define DRM_DEV_ERROR(dev, fmt, ...) \
383 drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__)
386 * DRM_DEV_ERROR_RATELIMITED() - Rate limited error output.
388 * NOTE: this is deprecated in favor of drm_err_ratelimited() or
389 * dev_err_ratelimited().
391 * @dev: device pointer
392 * @fmt: printf() like format string.
394 * Like DRM_ERROR() but won't flood the log.
396 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
398 static DEFINE_RATELIMIT_STATE(_rs, \
399 DEFAULT_RATELIMIT_INTERVAL, \
400 DEFAULT_RATELIMIT_BURST); \
402 if (__ratelimit(&_rs)) \
403 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
406 /* NOTE: this is deprecated in favor of drm_info() or dev_info(). */
407 #define DRM_DEV_INFO(dev, fmt, ...) \
408 drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__)
410 /* NOTE: this is deprecated in favor of drm_info_once() or dev_info_once(). */
411 #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
413 static bool __print_once __read_mostly; \
414 if (!__print_once) { \
415 __print_once = true; \
416 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
420 #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
421 #define drm_dev_dbg(dev, cat, fmt, ...) \
422 __drm_dev_dbg(NULL, dev, cat, fmt, ##__VA_ARGS__)
424 #define drm_dev_dbg(dev, cat, fmt, ...) \
425 _dynamic_func_call_cls(cat, fmt, __drm_dev_dbg, \
426 dev, cat, fmt, ##__VA_ARGS__)
430 * DRM_DEV_DEBUG() - Debug output for generic drm code
432 * NOTE: this is deprecated in favor of drm_dbg_core().
434 * @dev: device pointer
435 * @fmt: printf() like format string.
437 #define DRM_DEV_DEBUG(dev, fmt, ...) \
438 drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
440 * DRM_DEV_DEBUG_DRIVER() - Debug output for vendor specific part of the driver
442 * NOTE: this is deprecated in favor of drm_dbg() or dev_dbg().
444 * @dev: device pointer
445 * @fmt: printf() like format string.
447 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \
448 drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
450 * DRM_DEV_DEBUG_KMS() - Debug output for modesetting code
452 * NOTE: this is deprecated in favor of drm_dbg_kms().
454 * @dev: device pointer
455 * @fmt: printf() like format string.
457 #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \
458 drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
461 * struct drm_device based logging
463 * Prefer drm_device based logging over device or prink based logging.
466 /* Helper for struct drm_device based logging. */
467 #define __drm_printk(drm, level, type, fmt, ...) \
468 dev_##level##type((drm) ? (drm)->dev : NULL, "[drm] " fmt, ##__VA_ARGS__)
471 #define drm_info(drm, fmt, ...) \
472 __drm_printk((drm), info,, fmt, ##__VA_ARGS__)
474 #define drm_notice(drm, fmt, ...) \
475 __drm_printk((drm), notice,, fmt, ##__VA_ARGS__)
477 #define drm_warn(drm, fmt, ...) \
478 __drm_printk((drm), warn,, fmt, ##__VA_ARGS__)
480 #define drm_err(drm, fmt, ...) \
481 __drm_printk((drm), err,, "*ERROR* " fmt, ##__VA_ARGS__)
484 #define drm_info_once(drm, fmt, ...) \
485 __drm_printk((drm), info, _once, fmt, ##__VA_ARGS__)
487 #define drm_notice_once(drm, fmt, ...) \
488 __drm_printk((drm), notice, _once, fmt, ##__VA_ARGS__)
490 #define drm_warn_once(drm, fmt, ...) \
491 __drm_printk((drm), warn, _once, fmt, ##__VA_ARGS__)
493 #define drm_err_once(drm, fmt, ...) \
494 __drm_printk((drm), err, _once, "*ERROR* " fmt, ##__VA_ARGS__)
497 #define drm_err_ratelimited(drm, fmt, ...) \
498 __drm_printk((drm), err, _ratelimited, "*ERROR* " fmt, ##__VA_ARGS__)
501 #define drm_dbg_core(drm, fmt, ...) \
502 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_CORE, fmt, ##__VA_ARGS__)
503 #define drm_dbg_driver(drm, fmt, ...) \
504 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
505 #define drm_dbg_kms(drm, fmt, ...) \
506 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_KMS, fmt, ##__VA_ARGS__)
507 #define drm_dbg_prime(drm, fmt, ...) \
508 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
509 #define drm_dbg_atomic(drm, fmt, ...) \
510 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
511 #define drm_dbg_vbl(drm, fmt, ...) \
512 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_VBL, fmt, ##__VA_ARGS__)
513 #define drm_dbg_state(drm, fmt, ...) \
514 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_STATE, fmt, ##__VA_ARGS__)
515 #define drm_dbg_lease(drm, fmt, ...) \
516 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_LEASE, fmt, ##__VA_ARGS__)
517 #define drm_dbg_dp(drm, fmt, ...) \
518 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_DP, fmt, ##__VA_ARGS__)
519 #define drm_dbg_drmres(drm, fmt, ...) \
520 drm_dev_dbg((drm) ? (drm)->dev : NULL, DRM_UT_DRMRES, fmt, ##__VA_ARGS__)
522 #define drm_dbg(drm, fmt, ...) drm_dbg_driver(drm, fmt, ##__VA_ARGS__)
525 * printk based logging
527 * Prefer drm_device based logging over device or prink based logging.
531 void ___drm_dbg(struct _ddebug *desc, enum drm_debug_category category, const char *format, ...);
533 void __drm_err(const char *format, ...);
535 #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
536 #define __drm_dbg(cat, fmt, ...) ___drm_dbg(NULL, cat, fmt, ##__VA_ARGS__)
538 #define __drm_dbg(cat, fmt, ...) \
539 _dynamic_func_call_cls(cat, fmt, ___drm_dbg, \
540 cat, fmt, ##__VA_ARGS__)
543 /* Macros to make printk easier */
545 #define _DRM_PRINTK(once, level, fmt, ...) \
546 printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
548 /* NOTE: this is deprecated in favor of pr_info(). */
549 #define DRM_INFO(fmt, ...) \
550 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
551 /* NOTE: this is deprecated in favor of pr_notice(). */
552 #define DRM_NOTE(fmt, ...) \
553 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
554 /* NOTE: this is deprecated in favor of pr_warn(). */
555 #define DRM_WARN(fmt, ...) \
556 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
558 /* NOTE: this is deprecated in favor of pr_info_once(). */
559 #define DRM_INFO_ONCE(fmt, ...) \
560 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
561 /* NOTE: this is deprecated in favor of pr_notice_once(). */
562 #define DRM_NOTE_ONCE(fmt, ...) \
563 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
564 /* NOTE: this is deprecated in favor of pr_warn_once(). */
565 #define DRM_WARN_ONCE(fmt, ...) \
566 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
568 /* NOTE: this is deprecated in favor of pr_err(). */
569 #define DRM_ERROR(fmt, ...) \
570 __drm_err(fmt, ##__VA_ARGS__)
572 /* NOTE: this is deprecated in favor of pr_err_ratelimited(). */
573 #define DRM_ERROR_RATELIMITED(fmt, ...) \
574 DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
576 /* NOTE: this is deprecated in favor of drm_dbg_core(NULL, ...). */
577 #define DRM_DEBUG(fmt, ...) \
578 __drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__)
580 /* NOTE: this is deprecated in favor of drm_dbg(NULL, ...). */
581 #define DRM_DEBUG_DRIVER(fmt, ...) \
582 __drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
584 /* NOTE: this is deprecated in favor of drm_dbg_kms(NULL, ...). */
585 #define DRM_DEBUG_KMS(fmt, ...) \
586 __drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__)
588 /* NOTE: this is deprecated in favor of drm_dbg_prime(NULL, ...). */
589 #define DRM_DEBUG_PRIME(fmt, ...) \
590 __drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__)
592 /* NOTE: this is deprecated in favor of drm_dbg_atomic(NULL, ...). */
593 #define DRM_DEBUG_ATOMIC(fmt, ...) \
594 __drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
596 /* NOTE: this is deprecated in favor of drm_dbg_vbl(NULL, ...). */
597 #define DRM_DEBUG_VBL(fmt, ...) \
598 __drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__)
600 /* NOTE: this is deprecated in favor of drm_dbg_lease(NULL, ...). */
601 #define DRM_DEBUG_LEASE(fmt, ...) \
602 __drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__)
604 /* NOTE: this is deprecated in favor of drm_dbg_dp(NULL, ...). */
605 #define DRM_DEBUG_DP(fmt, ...) \
606 __drm_dbg(DRM_UT_DP, fmt, ## __VA_ARGS__)
608 #define __DRM_DEFINE_DBG_RATELIMITED(category, drm, fmt, ...) \
610 static DEFINE_RATELIMIT_STATE(rs_, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);\
611 const struct drm_device *drm_ = (drm); \
613 if (drm_debug_enabled(DRM_UT_ ## category) && __ratelimit(&rs_)) \
614 drm_dev_printk(drm_ ? drm_->dev : NULL, KERN_DEBUG, fmt, ## __VA_ARGS__); \
617 #define drm_dbg_ratelimited(drm, fmt, ...) \
618 __DRM_DEFINE_DBG_RATELIMITED(DRIVER, drm, fmt, ## __VA_ARGS__)
620 #define drm_dbg_kms_ratelimited(drm, fmt, ...) \
621 __DRM_DEFINE_DBG_RATELIMITED(KMS, drm, fmt, ## __VA_ARGS__)
624 * struct drm_device based WARNs
626 * drm_WARN*() acts like WARN*(), but with the key difference of
627 * using device specific information so that we know from which device
628 * warning is originating from.
630 * Prefer drm_device based drm_WARN* over regular WARN*
633 /* Helper for struct drm_device based WARNs */
634 #define drm_WARN(drm, condition, format, arg...) \
635 WARN(condition, "%s %s: " format, \
636 dev_driver_string((drm)->dev), \
637 dev_name((drm)->dev), ## arg)
639 #define drm_WARN_ONCE(drm, condition, format, arg...) \
640 WARN_ONCE(condition, "%s %s: " format, \
641 dev_driver_string((drm)->dev), \
642 dev_name((drm)->dev), ## arg)
644 #define drm_WARN_ON(drm, x) \
645 drm_WARN((drm), (x), "%s", \
646 "drm_WARN_ON(" __stringify(x) ")")
648 #define drm_WARN_ON_ONCE(drm, x) \
649 drm_WARN_ONCE((drm), (x), "%s", \
650 "drm_WARN_ON_ONCE(" __stringify(x) ")")
652 #endif /* DRM_PRINT_H_ */