blob: 41dd757c82f48bc1e029c81644034bb39656cd10 [file] [log] [blame]
Rob Clarkd81871772016-11-05 11:08:07 -04001/*
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 <[email protected]>
24 */
25
26#ifndef DRM_PRINT_H_
27#define DRM_PRINT_H_
28
Joe Perches3c6d6e0f2017-02-15 15:33:18 -080029#include <linux/compiler.h>
30#include <linux/printk.h>
Rob Clarkd81871772016-11-05 11:08:07 -040031#include <linux/seq_file.h>
32#include <linux/device.h>
33
34/**
35 * DOC: print
36 *
37 * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same
38 * debug code to be used for both debugfs and printk logging.
39 *
40 * For example::
41 *
42 * void log_some_info(struct drm_printer *p)
43 * {
44 * drm_printf(p, "foo=%d\n", foo);
45 * drm_printf(p, "bar=%d\n", bar);
46 * }
47 *
48 * #ifdef CONFIG_DEBUG_FS
49 * void debugfs_show(struct seq_file *f)
50 * {
51 * struct drm_printer p = drm_seq_file_printer(f);
52 * log_some_info(&p);
53 * }
54 * #endif
55 *
56 * void some_other_function(...)
57 * {
58 * struct drm_printer p = drm_info_printer(drm->dev);
59 * log_some_info(&p);
60 * }
61 */
62
63/**
64 * struct drm_printer - drm output "stream"
Rob Clarkd81871772016-11-05 11:08:07 -040065 *
66 * Do not use struct members directly. Use drm_printer_seq_file(),
67 * drm_printer_info(), etc to initialize. And drm_printf() for output.
68 */
69struct drm_printer {
Daniel Vetter3d387d92016-12-28 17:42:09 +010070 /* private: */
Rob Clarkd81871772016-11-05 11:08:07 -040071 void (*printfn)(struct drm_printer *p, struct va_format *vaf);
72 void *arg;
Daniel Vetter3d387d92016-12-28 17:42:09 +010073 const char *prefix;
Rob Clarkd81871772016-11-05 11:08:07 -040074};
75
76void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
77void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
Daniel Vetter3d387d92016-12-28 17:42:09 +010078void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
Rob Clarkd81871772016-11-05 11:08:07 -040079
Joe Perches3c6d6e0f2017-02-15 15:33:18 -080080__printf(2, 3)
Rob Clarkd81871772016-11-05 11:08:07 -040081void drm_printf(struct drm_printer *p, const char *f, ...);
82
83
84/**
85 * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
Daniel Vetterea0dd852016-12-29 21:48:26 +010086 * @f: the &struct seq_file to output to
Rob Clarkd81871772016-11-05 11:08:07 -040087 *
88 * RETURNS:
89 * The &drm_printer object
90 */
91static inline struct drm_printer drm_seq_file_printer(struct seq_file *f)
92{
93 struct drm_printer p = {
94 .printfn = __drm_printfn_seq_file,
95 .arg = f,
96 };
97 return p;
98}
99
100/**
101 * drm_info_printer - construct a &drm_printer that outputs to dev_printk()
Daniel Vetterea0dd852016-12-29 21:48:26 +0100102 * @dev: the &struct device pointer
Rob Clarkd81871772016-11-05 11:08:07 -0400103 *
104 * RETURNS:
105 * The &drm_printer object
106 */
107static inline struct drm_printer drm_info_printer(struct device *dev)
108{
109 struct drm_printer p = {
110 .printfn = __drm_printfn_info,
111 .arg = dev,
112 };
113 return p;
114}
115
Daniel Vetter3d387d92016-12-28 17:42:09 +0100116/**
117 * drm_debug_printer - construct a &drm_printer that outputs to pr_debug()
118 * @prefix: debug output prefix
119 *
120 * RETURNS:
121 * The &drm_printer object
122 */
123static inline struct drm_printer drm_debug_printer(const char *prefix)
124{
125 struct drm_printer p = {
126 .printfn = __drm_printfn_debug,
127 .prefix = prefix
128 };
129 return p;
130}
Haneen Mohammed02c96562017-10-17 22:30:07 -0600131
132/*
133 * The following categories are defined:
134 *
135 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
136 * This is the category used by the DRM_DEBUG() macro.
137 *
138 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
139 * This is the category used by the DRM_DEBUG_DRIVER() macro.
140 *
141 * KMS: used in the modesetting code.
142 * This is the category used by the DRM_DEBUG_KMS() macro.
143 *
144 * PRIME: used in the prime code.
145 * This is the category used by the DRM_DEBUG_PRIME() macro.
146 *
147 * ATOMIC: used in the atomic code.
148 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
149 *
150 * VBL: used for verbose debug message in the vblank code
151 * This is the category used by the DRM_DEBUG_VBL() macro.
152 *
153 * Enabling verbose debug messages is done through the drm.debug parameter,
154 * each category being enabled by a bit.
155 *
156 * drm.debug=0x1 will enable CORE messages
157 * drm.debug=0x2 will enable DRIVER messages
158 * drm.debug=0x3 will enable CORE and DRIVER messages
159 * ...
160 * drm.debug=0x3f will enable all messages
161 *
162 * An interesting feature is that it's possible to enable verbose logging at
163 * run-time by echoing the debug value in its sysfs node:
164 * # echo 0xf > /sys/module/drm/parameters/debug
165 */
166#define DRM_UT_NONE 0x00
167#define DRM_UT_CORE 0x01
168#define DRM_UT_DRIVER 0x02
169#define DRM_UT_KMS 0x04
170#define DRM_UT_PRIME 0x08
171#define DRM_UT_ATOMIC 0x10
172#define DRM_UT_VBL 0x20
173#define DRM_UT_STATE 0x40
174
175__printf(6, 7)
176void drm_dev_printk(const struct device *dev, const char *level,
177 unsigned int category, const char *function_name,
178 const char *prefix, const char *format, ...);
179__printf(3, 4)
180void drm_printk(const char *level, unsigned int category,
181 const char *format, ...);
182/***********************************************************************/
183/** \name Macros to make printk easier */
184/*@{*/
185
186#define _DRM_PRINTK(once, level, fmt, ...) \
187 do { \
188 printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
189 ##__VA_ARGS__); \
190 } while (0)
191
192#define DRM_INFO(fmt, ...) \
193 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
194#define DRM_NOTE(fmt, ...) \
195 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
196#define DRM_WARN(fmt, ...) \
197 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
198
199#define DRM_INFO_ONCE(fmt, ...) \
200 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
201#define DRM_NOTE_ONCE(fmt, ...) \
202 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
203#define DRM_WARN_ONCE(fmt, ...) \
204 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
205
206/**
207 * Error output.
208 *
209 * \param fmt printf() like format string.
210 * \param arg arguments
211 */
212#define DRM_DEV_ERROR(dev, fmt, ...) \
213 drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
214 fmt, ##__VA_ARGS__)
215#define DRM_ERROR(fmt, ...) \
216 drm_printk(KERN_ERR, DRM_UT_NONE, fmt, ##__VA_ARGS__)
217
218/**
219 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
220 *
221 * \param fmt printf() like format string.
222 * \param arg arguments
223 */
224#define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
225({ \
226 static DEFINE_RATELIMIT_STATE(_rs, \
227 DEFAULT_RATELIMIT_INTERVAL, \
228 DEFAULT_RATELIMIT_BURST); \
229 \
230 if (__ratelimit(&_rs)) \
231 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
232})
233#define DRM_ERROR_RATELIMITED(fmt, ...) \
234 DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
235
236#define DRM_DEV_INFO(dev, fmt, ...) \
237 drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt, \
238 ##__VA_ARGS__)
239
240#define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
241({ \
242 static bool __print_once __read_mostly; \
243 if (!__print_once) { \
244 __print_once = true; \
245 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
246 } \
247})
248
249/**
250 * Debug output.
251 *
252 * \param fmt printf() like format string.
253 * \param arg arguments
254 */
255#define DRM_DEV_DEBUG(dev, fmt, args...) \
256 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
257 ##args)
258#define DRM_DEBUG(fmt, ...) \
259 drm_printk(KERN_DEBUG, DRM_UT_CORE, fmt, ##__VA_ARGS__)
260
261#define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...) \
262 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "", \
263 fmt, ##args)
264#define DRM_DEBUG_DRIVER(fmt, ...) \
265 drm_printk(KERN_DEBUG, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
266
267#define DRM_DEV_DEBUG_KMS(dev, fmt, args...) \
268 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt, \
269 ##args)
270#define DRM_DEBUG_KMS(fmt, ...) \
271 drm_printk(KERN_DEBUG, DRM_UT_KMS, fmt, ##__VA_ARGS__)
272
273#define DRM_DEV_DEBUG_PRIME(dev, fmt, args...) \
274 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "", \
275 fmt, ##args)
276#define DRM_DEBUG_PRIME(fmt, ...) \
277 drm_printk(KERN_DEBUG, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
278
279#define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...) \
280 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "", \
281 fmt, ##args)
282#define DRM_DEBUG_ATOMIC(fmt, ...) \
283 drm_printk(KERN_DEBUG, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
284
285#define DRM_DEV_DEBUG_VBL(dev, fmt, args...) \
286 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt, \
287 ##args)
288#define DRM_DEBUG_VBL(fmt, ...) \
289 drm_printk(KERN_DEBUG, DRM_UT_VBL, fmt, ##__VA_ARGS__)
290
291#define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...) \
292({ \
293 static DEFINE_RATELIMIT_STATE(_rs, \
294 DEFAULT_RATELIMIT_INTERVAL, \
295 DEFAULT_RATELIMIT_BURST); \
296 if (__ratelimit(&_rs)) \
297 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level, \
298 __func__, "", fmt, ##args); \
299})
300
301/**
302 * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
303 *
304 * \param fmt printf() like format string.
305 * \param arg arguments
306 */
307#define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...) \
308 DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
309#define DRM_DEBUG_RATELIMITED(fmt, args...) \
310 DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
311#define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...) \
312 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
313#define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...) \
314 DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
315#define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...) \
316 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
317#define DRM_DEBUG_KMS_RATELIMITED(fmt, args...) \
318 DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
319#define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...) \
320 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
321#define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...) \
322 DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
323
324/* Format strings and argument splitters to simplify printing
325 * various "complex" objects
326 */
327
328/*@}*/
329
Rob Clarkd81871772016-11-05 11:08:07 -0400330#endif /* DRM_PRINT_H_ */