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