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