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.
26 #include <linux/debugfs.h>
27 #include <linux/dynamic_debug.h>
29 #include <linux/moduleparam.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/stdarg.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_print.h>
39 * __drm_debug: Enable debug output.
40 * Bitmask of DRM_UT_x. See include/drm/drm_print.h for details.
42 unsigned long __drm_debug;
43 EXPORT_SYMBOL(__drm_debug);
45 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
46 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
47 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
48 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
49 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
50 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
51 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n"
52 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)\n"
53 "\t\tBit 8 (0x100) will enable DP messages (displayport code)");
55 #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
56 module_param_named(debug, __drm_debug, ulong, 0600);
58 /* classnames must match vals of enum drm_debug_category */
59 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
71 static struct ddebug_class_param drm_debug_bitmap = {
74 .map = &drm_debug_classes,
76 module_param_cb(debug, ¶m_ops_dyndbg_classes, &drm_debug_bitmap, 0600);
79 void __drm_puts_coredump(struct drm_printer *p, const char *str)
81 struct drm_print_iterator *iterator = p->arg;
84 if (!iterator->remain)
87 if (iterator->offset < iterator->start) {
92 if (iterator->offset + len <= iterator->start) {
93 iterator->offset += len;
97 copy = len - (iterator->start - iterator->offset);
99 if (copy > iterator->remain)
100 copy = iterator->remain;
102 /* Copy out the bit of the string that we need */
103 memcpy(iterator->data,
104 str + (iterator->start - iterator->offset), copy);
106 iterator->offset = iterator->start + copy;
107 iterator->remain -= copy;
109 ssize_t pos = iterator->offset - iterator->start;
111 len = min_t(ssize_t, strlen(str), iterator->remain);
113 memcpy(iterator->data + pos, str, len);
115 iterator->offset += len;
116 iterator->remain -= len;
119 EXPORT_SYMBOL(__drm_puts_coredump);
121 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
123 struct drm_print_iterator *iterator = p->arg;
127 if (!iterator->remain)
130 /* Figure out how big the string will be */
131 len = snprintf(NULL, 0, "%pV", vaf);
133 /* This is the easiest path, we've already advanced beyond the offset */
134 if (iterator->offset + len <= iterator->start) {
135 iterator->offset += len;
139 /* Then check if we can directly copy into the target buffer */
140 if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
141 ssize_t pos = iterator->offset - iterator->start;
143 snprintf(((char *) iterator->data) + pos,
144 iterator->remain, "%pV", vaf);
146 iterator->offset += len;
147 iterator->remain -= len;
153 * Finally, hit the slow path and make a temporary string to copy over
154 * using _drm_puts_coredump
156 buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
160 snprintf(buf, len + 1, "%pV", vaf);
161 __drm_puts_coredump(p, (const char *) buf);
165 EXPORT_SYMBOL(__drm_printfn_coredump);
167 void __drm_puts_seq_file(struct drm_printer *p, const char *str)
169 seq_puts(p->arg, str);
171 EXPORT_SYMBOL(__drm_puts_seq_file);
173 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
175 seq_printf(p->arg, "%pV", vaf);
177 EXPORT_SYMBOL(__drm_printfn_seq_file);
179 static void __drm_dev_vprintk(const struct device *dev, const char *level,
180 const void *origin, const char *prefix,
181 struct va_format *vaf)
183 const char *prefix_pad = prefix ? " " : "";
190 dev_printk(level, dev, "[" DRM_NAME ":%ps]%s%s %pV",
191 origin, prefix_pad, prefix, vaf);
193 dev_printk(level, dev, "[" DRM_NAME "]%s%s %pV",
194 prefix_pad, prefix, vaf);
197 printk("%s" "[" DRM_NAME ":%ps]%s%s %pV",
198 level, origin, prefix_pad, prefix, vaf);
200 printk("%s" "[" DRM_NAME "]%s%s %pV",
201 level, prefix_pad, prefix, vaf);
205 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
207 dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
209 EXPORT_SYMBOL(__drm_printfn_info);
211 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf)
213 const struct drm_device *drm = p->arg;
214 const struct device *dev = drm ? drm->dev : NULL;
215 enum drm_debug_category category = p->category;
217 if (!__drm_debug_enabled(category))
220 __drm_dev_vprintk(dev, KERN_DEBUG, p->origin, p->prefix, vaf);
222 EXPORT_SYMBOL(__drm_printfn_dbg);
224 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
226 struct drm_device *drm = p->arg;
229 drm_err(drm, "%s %pV", p->prefix, vaf);
231 drm_err(drm, "%pV", vaf);
233 EXPORT_SYMBOL(__drm_printfn_err);
236 * drm_puts - print a const string to a &drm_printer stream
237 * @p: the &drm printer
240 * Allow &drm_printer types that have a constant string
243 void drm_puts(struct drm_printer *p, const char *str)
248 drm_printf(p, "%s", str);
250 EXPORT_SYMBOL(drm_puts);
253 * drm_printf - print to a &drm_printer stream
254 * @p: the &drm_printer
257 void drm_printf(struct drm_printer *p, const char *f, ...)
262 drm_vprintf(p, f, &args);
265 EXPORT_SYMBOL(drm_printf);
268 * drm_print_bits - print bits to a &drm_printer stream
270 * Print bits (in flag fields for example) in human readable form.
272 * @p: the &drm_printer
273 * @value: field value.
274 * @bits: Array with bit names.
275 * @nbits: Size of bit names array.
277 void drm_print_bits(struct drm_printer *p, unsigned long value,
278 const char * const bits[], unsigned int nbits)
283 if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value)))
284 nbits = BITS_PER_TYPE(value);
286 for_each_set_bit(i, &value, nbits) {
287 if (WARN_ON_ONCE(!bits[i]))
289 drm_printf(p, "%s%s", first ? "" : ",",
294 drm_printf(p, "(none)");
296 EXPORT_SYMBOL(drm_print_bits);
298 void drm_dev_printk(const struct device *dev, const char *level,
299 const char *format, ...)
301 struct va_format vaf;
304 va_start(args, format);
308 __drm_dev_vprintk(dev, level, __builtin_return_address(0), NULL, &vaf);
312 EXPORT_SYMBOL(drm_dev_printk);
314 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
315 enum drm_debug_category category, const char *format, ...)
317 struct va_format vaf;
320 if (!__drm_debug_enabled(category))
323 /* we know we are printing for either syslog, tracefs, or both */
324 va_start(args, format);
328 __drm_dev_vprintk(dev, KERN_DEBUG, __builtin_return_address(0), NULL, &vaf);
332 EXPORT_SYMBOL(__drm_dev_dbg);
334 void __drm_err(const char *format, ...)
336 struct va_format vaf;
339 va_start(args, format);
343 __drm_dev_vprintk(NULL, KERN_ERR, __builtin_return_address(0), "*ERROR*", &vaf);
347 EXPORT_SYMBOL(__drm_err);
350 * drm_print_regset32 - print the contents of registers to a
351 * &drm_printer stream.
353 * @p: the &drm printer
354 * @regset: the list of registers to print.
356 * Often in driver debug, it's useful to be able to either capture the
357 * contents of registers in the steady state using debugfs or at
358 * specific points during operation. This lets the driver have a
359 * single list of registers for both.
361 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
366 for (i = 0; i < regset->nregs; i++)
367 namelen = max(namelen, (int)strlen(regset->regs[i].name));
369 for (i = 0; i < regset->nregs; i++) {
370 drm_printf(p, "%*s = 0x%08x\n",
371 namelen, regset->regs[i].name,
372 readl(regset->base + regset->regs[i].offset));
375 EXPORT_SYMBOL(drm_print_regset32);