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>
37 /* Do *not* use outside of drm_print.[ch]! */
38 extern unsigned int __drm_debug
;
43 * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
44 * debug code to be used for both debugfs and printk logging.
48 * void log_some_info(struct drm_printer *p)
50 * drm_printf(p, "foo=%d\n", foo);
51 * drm_printf(p, "bar=%d\n", bar);
54 * #ifdef CONFIG_DEBUG_FS
55 * void debugfs_show(struct seq_file *f)
57 * struct drm_printer p = drm_seq_file_printer(f);
62 * void some_other_function(...)
64 * struct drm_printer p = drm_info_printer(drm->dev);
70 * struct drm_printer - drm output "stream"
72 * Do not use struct members directly. Use drm_printer_seq_file(),
73 * drm_printer_info(), etc to initialize. And drm_printf() for output.
77 void (*printfn
)(struct drm_printer
*p
, struct va_format
*vaf
);
78 void (*puts
)(struct drm_printer
*p
, const char *str
);
83 void __drm_printfn_coredump(struct drm_printer
*p
, struct va_format
*vaf
);
84 void __drm_puts_coredump(struct drm_printer
*p
, const char *str
);
85 void __drm_printfn_seq_file(struct drm_printer
*p
, struct va_format
*vaf
);
86 void __drm_puts_seq_file(struct drm_printer
*p
, const char *str
);
87 void __drm_printfn_info(struct drm_printer
*p
, struct va_format
*vaf
);
88 void __drm_printfn_debug(struct drm_printer
*p
, struct va_format
*vaf
);
89 void __drm_printfn_err(struct drm_printer
*p
, struct va_format
*vaf
);
92 void drm_printf(struct drm_printer
*p
, const char *f
, ...);
93 void drm_puts(struct drm_printer
*p
, const char *str
);
94 void drm_print_regset32(struct drm_printer
*p
, struct debugfs_regset32
*regset
);
95 void drm_print_bits(struct drm_printer
*p
, unsigned long value
,
96 const char * const bits
[], unsigned int nbits
);
100 * drm_vprintf - print to a &drm_printer stream
101 * @p: the &drm_printer
102 * @fmt: format string
106 drm_vprintf(struct drm_printer
*p
, const char *fmt
, va_list *va
)
108 struct va_format vaf
= { .fmt
= fmt
, .va
= va
};
114 * drm_printf_indent - Print to a &drm_printer stream with indentation
115 * @printer: DRM printer
116 * @indent: Tab indentation level (max 5)
117 * @fmt: Format string
119 #define drm_printf_indent(printer, indent, fmt, ...) \
120 drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
123 * struct drm_print_iterator - local struct used with drm_printer_coredump
124 * @data: Pointer to the devcoredump output buffer
125 * @start: The offset within the buffer to start writing
126 * @remain: The number of bytes to write for this iteration
128 struct drm_print_iterator
{
137 * drm_coredump_printer - construct a &drm_printer that can output to a buffer
138 * from the read function for devcoredump
139 * @iter: A pointer to a struct drm_print_iterator for the read instance
141 * This wrapper extends drm_printf() to work with a dev_coredumpm() callback
142 * function. The passed in drm_print_iterator struct contains the buffer
143 * pointer, size and offset as passed in from devcoredump.
147 * void coredump_read(char *buffer, loff_t offset, size_t count,
148 * void *data, size_t datalen)
150 * struct drm_print_iterator iter;
151 * struct drm_printer p;
153 * iter.data = buffer;
154 * iter.start = offset;
155 * iter.remain = count;
157 * p = drm_coredump_printer(&iter);
159 * drm_printf(p, "foo=%d\n", foo);
162 * void makecoredump(...)
165 * dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL,
166 * coredump_read, ...)
170 * The &drm_printer object
172 static inline struct drm_printer
173 drm_coredump_printer(struct drm_print_iterator
*iter
)
175 struct drm_printer p
= {
176 .printfn
= __drm_printfn_coredump
,
177 .puts
= __drm_puts_coredump
,
181 /* Set the internal offset of the iterator to zero */
188 * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
189 * @f: the &struct seq_file to output to
192 * The &drm_printer object
194 static inline struct drm_printer
drm_seq_file_printer(struct seq_file
*f
)
196 struct drm_printer p
= {
197 .printfn
= __drm_printfn_seq_file
,
198 .puts
= __drm_puts_seq_file
,
205 * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
206 * @dev: the &struct device pointer
209 * The &drm_printer object
211 static inline struct drm_printer
drm_info_printer(struct device
*dev
)
213 struct drm_printer p
= {
214 .printfn
= __drm_printfn_info
,
221 * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
222 * @prefix: debug output prefix
225 * The &drm_printer object
227 static inline struct drm_printer
drm_debug_printer(const char *prefix
)
229 struct drm_printer p
= {
230 .printfn
= __drm_printfn_debug
,
237 * drm_err_printer - construct a &drm_printer that outputs to pr_err()
238 * @prefix: debug output prefix
241 * The &drm_printer object
243 static inline struct drm_printer
drm_err_printer(const char *prefix
)
245 struct drm_printer p
= {
246 .printfn
= __drm_printfn_err
,
253 * enum drm_debug_category - The DRM debug categories
255 * Each of the DRM debug logging macros use a specific category, and the logging
256 * is filtered by the drm.debug module parameter. This enum specifies the values
259 * Each DRM_DEBUG_<CATEGORY> macro logs to DRM_UT_<CATEGORY> category, except
260 * DRM_DEBUG() logs to DRM_UT_CORE.
262 * Enabling verbose debug messages is done through the drm.debug parameter, each
263 * category being enabled by a bit:
265 * - drm.debug=0x1 will enable CORE messages
266 * - drm.debug=0x2 will enable DRIVER messages
267 * - drm.debug=0x3 will enable CORE and DRIVER messages
269 * - drm.debug=0x1ff will enable all messages
271 * An interesting feature is that it's possible to enable verbose logging at
272 * run-time by echoing the debug value in its sysfs node::
274 * # echo 0xf > /sys/module/drm/parameters/debug
277 enum drm_debug_category
{
279 * @DRM_UT_CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c,
284 * @DRM_UT_DRIVER: Used in the vendor specific part of the driver: i915,
287 DRM_UT_DRIVER
= 0x02,
289 * @DRM_UT_KMS: Used in the modesetting code.
293 * @DRM_UT_PRIME: Used in the prime code.
297 * @DRM_UT_ATOMIC: Used in the atomic code.
299 DRM_UT_ATOMIC
= 0x10,
301 * @DRM_UT_VBL: Used for verbose debug message in the vblank code.
305 * @DRM_UT_STATE: Used for verbose atomic state debugging.
309 * @DRM_UT_LEASE: Used in the lease code.
313 * @DRM_UT_DP: Used in the DP code.
318 static inline bool drm_debug_enabled(enum drm_debug_category category
)
320 return unlikely(__drm_debug
& category
);
324 * struct device based logging
326 * Prefer drm_device based logging over device or prink based logging.
330 void drm_dev_printk(const struct device
*dev
, const char *level
,
331 const char *format
, ...);
333 void drm_dev_dbg(const struct device
*dev
, enum drm_debug_category category
,
334 const char *format
, ...);
339 * @dev: device pointer
340 * @fmt: printf() like format string.
342 #define DRM_DEV_ERROR(dev, fmt, ...) \
343 drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__)
346 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
348 * @dev: device pointer
349 * @fmt: printf() like format string.
351 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
353 static DEFINE_RATELIMIT_STATE(_rs, \
354 DEFAULT_RATELIMIT_INTERVAL, \
355 DEFAULT_RATELIMIT_BURST); \
357 if (__ratelimit(&_rs)) \
358 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
361 #define DRM_DEV_INFO(dev, fmt, ...) \
362 drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__)
364 #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
366 static bool __print_once __read_mostly; \
367 if (!__print_once) { \
368 __print_once = true; \
369 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
376 * @dev: device pointer
377 * @fmt: printf() like format string.
379 #define DRM_DEV_DEBUG(dev, fmt, ...) \
380 drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
381 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \
382 drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
383 #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \
384 drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
385 #define DRM_DEV_DEBUG_PRIME(dev, fmt, ...) \
386 drm_dev_dbg(dev, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
387 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, ...) \
388 drm_dev_dbg(dev, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
389 #define DRM_DEV_DEBUG_VBL(dev, fmt, ...) \
390 drm_dev_dbg(dev, DRM_UT_VBL, fmt, ##__VA_ARGS__)
391 #define DRM_DEV_DEBUG_DP(dev, fmt, ...) \
392 drm_dev_dbg(dev, DRM_UT_DP, fmt, ## __VA_ARGS__)
394 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, category, fmt, ...) \
396 static DEFINE_RATELIMIT_STATE(_rs, \
397 DEFAULT_RATELIMIT_INTERVAL, \
398 DEFAULT_RATELIMIT_BURST); \
399 if (__ratelimit(&_rs)) \
400 drm_dev_dbg(dev, category, fmt, ##__VA_ARGS__); \
404 * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
406 * @dev: device pointer
407 * @fmt: printf() like format string.
409 #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, ...) \
410 _DEV_DRM_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_CORE, \
412 #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, ...) \
413 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_DRIVER, \
415 #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, ...) \
416 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_KMS, \
418 #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, ...) \
419 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_PRIME, \
423 * struct drm_device based logging
425 * Prefer drm_device based logging over device or prink based logging.
428 /* Helper for struct drm_device based logging. */
429 #define __drm_printk(drm, level, type, fmt, ...) \
430 dev_##level##type((drm)->dev, "[drm] " fmt, ##__VA_ARGS__)
433 #define drm_info(drm, fmt, ...) \
434 __drm_printk((drm), info,, fmt, ##__VA_ARGS__)
436 #define drm_notice(drm, fmt, ...) \
437 __drm_printk((drm), notice,, fmt, ##__VA_ARGS__)
439 #define drm_warn(drm, fmt, ...) \
440 __drm_printk((drm), warn,, fmt, ##__VA_ARGS__)
442 #define drm_err(drm, fmt, ...) \
443 __drm_printk((drm), err,, "*ERROR* " fmt, ##__VA_ARGS__)
446 #define drm_info_once(drm, fmt, ...) \
447 __drm_printk((drm), info, _once, fmt, ##__VA_ARGS__)
449 #define drm_notice_once(drm, fmt, ...) \
450 __drm_printk((drm), notice, _once, fmt, ##__VA_ARGS__)
452 #define drm_warn_once(drm, fmt, ...) \
453 __drm_printk((drm), warn, _once, fmt, ##__VA_ARGS__)
455 #define drm_err_once(drm, fmt, ...) \
456 __drm_printk((drm), err, _once, "*ERROR* " fmt, ##__VA_ARGS__)
459 #define drm_err_ratelimited(drm, fmt, ...) \
460 __drm_printk((drm), err, _ratelimited, "*ERROR* " fmt, ##__VA_ARGS__)
463 #define drm_dbg_core(drm, fmt, ...) \
464 drm_dev_dbg((drm)->dev, DRM_UT_CORE, fmt, ##__VA_ARGS__)
465 #define drm_dbg(drm, fmt, ...) \
466 drm_dev_dbg((drm)->dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
467 #define drm_dbg_kms(drm, fmt, ...) \
468 drm_dev_dbg((drm)->dev, DRM_UT_KMS, fmt, ##__VA_ARGS__)
469 #define drm_dbg_prime(drm, fmt, ...) \
470 drm_dev_dbg((drm)->dev, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
471 #define drm_dbg_atomic(drm, fmt, ...) \
472 drm_dev_dbg((drm)->dev, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
473 #define drm_dbg_vbl(drm, fmt, ...) \
474 drm_dev_dbg((drm)->dev, DRM_UT_VBL, fmt, ##__VA_ARGS__)
475 #define drm_dbg_state(drm, fmt, ...) \
476 drm_dev_dbg((drm)->dev, DRM_UT_STATE, fmt, ##__VA_ARGS__)
477 #define drm_dbg_lease(drm, fmt, ...) \
478 drm_dev_dbg((drm)->dev, DRM_UT_LEASE, fmt, ##__VA_ARGS__)
479 #define drm_dbg_dp(drm, fmt, ...) \
480 drm_dev_dbg((drm)->dev, DRM_UT_DP, fmt, ##__VA_ARGS__)
484 * printk based logging
486 * Prefer drm_device based logging over device or prink based logging.
490 void __drm_dbg(enum drm_debug_category category
, const char *format
, ...);
492 void __drm_err(const char *format
, ...);
494 /* Macros to make printk easier */
496 #define _DRM_PRINTK(once, level, fmt, ...) \
497 printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
499 #define DRM_INFO(fmt, ...) \
500 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
501 #define DRM_NOTE(fmt, ...) \
502 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
503 #define DRM_WARN(fmt, ...) \
504 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
506 #define DRM_INFO_ONCE(fmt, ...) \
507 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
508 #define DRM_NOTE_ONCE(fmt, ...) \
509 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
510 #define DRM_WARN_ONCE(fmt, ...) \
511 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
513 #define DRM_ERROR(fmt, ...) \
514 __drm_err(fmt, ##__VA_ARGS__)
516 #define DRM_ERROR_RATELIMITED(fmt, ...) \
517 DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
519 #define DRM_DEBUG(fmt, ...) \
520 __drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__)
522 #define DRM_DEBUG_DRIVER(fmt, ...) \
523 __drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
525 #define DRM_DEBUG_KMS(fmt, ...) \
526 __drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__)
528 #define DRM_DEBUG_PRIME(fmt, ...) \
529 __drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__)
531 #define DRM_DEBUG_ATOMIC(fmt, ...) \
532 __drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
534 #define DRM_DEBUG_VBL(fmt, ...) \
535 __drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__)
537 #define DRM_DEBUG_LEASE(fmt, ...) \
538 __drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__)
540 #define DRM_DEBUG_DP(fmt, ...) \
541 __drm_dbg(DRM_UT_DP, fmt, ## __VA_ARGS__)
544 #define DRM_DEBUG_RATELIMITED(fmt, ...) \
545 DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
547 #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, ...) \
548 DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
550 #define DRM_DEBUG_KMS_RATELIMITED(fmt, ...) \
551 DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
553 #define DRM_DEBUG_PRIME_RATELIMITED(fmt, ...) \
554 DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
556 #endif /* DRM_PRINT_H_ */