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