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 #include <linux/debugfs.h>
27 #include <linux/dynamic_debug.h>
28 #include <linux/io.h>
29 #include <linux/moduleparam.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/stdarg.h>
33
34 #include <drm/drm.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_print.h>
37
38 /*
39 * __drm_debug: Enable debug output.
40 * Bitmask of DRM_UT_x. See include/drm/drm_print.h for details.
41 */
42 #ifdef DRMDEBUG
43 unsigned long __drm_debug = DRM_UT_DRIVER | DRM_UT_KMS;
44 #else
45 unsigned long __drm_debug;
46 #endif
47 EXPORT_SYMBOL(__drm_debug);
48
49 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
50 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
51 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
52 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
53 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
54 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
55 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n"
56 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)\n"
57 "\t\tBit 8 (0x100) will enable DP messages (displayport code)");
58
59 #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG)
60 module_param_named(debug, __drm_debug, ulong, 0600);
61 #else
62 /* classnames must match vals of enum drm_debug_category */
63 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
64 "DRM_UT_CORE",
65 "DRM_UT_DRIVER",
66 "DRM_UT_KMS",
67 "DRM_UT_PRIME",
68 "DRM_UT_ATOMIC",
69 "DRM_UT_VBL",
70 "DRM_UT_STATE",
71 "DRM_UT_LEASE",
72 "DRM_UT_DP",
73 "DRM_UT_DRMRES");
74
75 static struct ddebug_class_param drm_debug_bitmap = {
76 .bits = &__drm_debug,
77 .flags = "p",
78 .map = &drm_debug_classes,
79 };
80 module_param_cb(debug, ¶m_ops_dyndbg_classes, &drm_debug_bitmap, 0600);
81 #endif
82
__drm_puts_coredump(struct drm_printer * p,const char * str)83 void __drm_puts_coredump(struct drm_printer *p, const char *str)
84 {
85 struct drm_print_iterator *iterator = p->arg;
86 ssize_t len;
87
88 if (!iterator->remain)
89 return;
90
91 if (iterator->offset < iterator->start) {
92 ssize_t copy;
93
94 len = strlen(str);
95
96 if (iterator->offset + len <= iterator->start) {
97 iterator->offset += len;
98 return;
99 }
100
101 copy = len - (iterator->start - iterator->offset);
102
103 if (copy > iterator->remain)
104 copy = iterator->remain;
105
106 /* Copy out the bit of the string that we need */
107 if (iterator->data)
108 memcpy(iterator->data,
109 str + (iterator->start - iterator->offset), copy);
110
111 iterator->offset = iterator->start + copy;
112 iterator->remain -= copy;
113 } else {
114 ssize_t pos = iterator->offset - iterator->start;
115
116 len = min_t(ssize_t, strlen(str), iterator->remain);
117
118 if (iterator->data)
119 memcpy(iterator->data + pos, str, len);
120
121 iterator->offset += len;
122 iterator->remain -= len;
123 }
124 }
125 EXPORT_SYMBOL(__drm_puts_coredump);
126
__drm_printfn_coredump(struct drm_printer * p,struct va_format * vaf)127 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
128 {
129 struct drm_print_iterator *iterator = p->arg;
130 size_t len;
131 char *buf;
132
133 if (!iterator->remain)
134 return;
135
136 /* Figure out how big the string will be */
137 len = snprintf(NULL, 0, "%pV", vaf);
138
139 /* This is the easiest path, we've already advanced beyond the offset */
140 if (iterator->offset + len <= iterator->start) {
141 iterator->offset += len;
142 return;
143 }
144
145 /* Then check if we can directly copy into the target buffer */
146 if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
147 ssize_t pos = iterator->offset - iterator->start;
148
149 if (iterator->data)
150 snprintf(((char *) iterator->data) + pos,
151 iterator->remain, "%pV", vaf);
152
153 iterator->offset += len;
154 iterator->remain -= len;
155
156 return;
157 }
158
159 /*
160 * Finally, hit the slow path and make a temporary string to copy over
161 * using _drm_puts_coredump
162 */
163 buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
164 if (!buf)
165 return;
166
167 snprintf(buf, len + 1, "%pV", vaf);
168 __drm_puts_coredump(p, (const char *) buf);
169
170 kfree(buf);
171 }
172 EXPORT_SYMBOL(__drm_printfn_coredump);
173
__drm_puts_seq_file(struct drm_printer * p,const char * str)174 void __drm_puts_seq_file(struct drm_printer *p, const char *str)
175 {
176 seq_puts(p->arg, str);
177 }
178 EXPORT_SYMBOL(__drm_puts_seq_file);
179
__drm_printfn_seq_file(struct drm_printer * p,struct va_format * vaf)180 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
181 {
182 seq_printf(p->arg, "%pV", vaf);
183 }
184 EXPORT_SYMBOL(__drm_printfn_seq_file);
185
__drm_dev_vprintk(const struct device * dev,const char * level,const void * origin,const char * prefix,struct va_format * vaf)186 static void __drm_dev_vprintk(const struct device *dev, const char *level,
187 const void *origin, const char *prefix,
188 struct va_format *vaf)
189 {
190 const char *prefix_pad = prefix ? " " : "";
191
192 if (!prefix)
193 prefix = "";
194
195 if (dev) {
196 if (origin)
197 dev_printk(level, dev, "[" DRM_NAME ":%ps]%s%s %pV",
198 origin, prefix_pad, prefix, vaf);
199 else
200 dev_printk(level, dev, "[" DRM_NAME "]%s%s %pV",
201 prefix_pad, prefix, vaf);
202 } else {
203 if (origin)
204 printk("%s" "[" DRM_NAME ":%ps]%s%s %pV",
205 level, origin, prefix_pad, prefix, vaf);
206 else
207 printk("%s" "[" DRM_NAME "]%s%s %pV",
208 level, prefix_pad, prefix, vaf);
209 }
210 }
211
212 #ifdef __linux__
__drm_printfn_info(struct drm_printer * p,struct va_format * vaf)213 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
214 {
215 dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
216 }
217 EXPORT_SYMBOL(__drm_printfn_info);
218 #endif
219
__drm_printfn_dbg(struct drm_printer * p,struct va_format * vaf)220 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf)
221 {
222 const struct drm_device *drm = p->arg;
223 const struct device *dev = drm ? drm->dev : NULL;
224 enum drm_debug_category category = p->category;
225
226 if (!__drm_debug_enabled(category))
227 return;
228
229 __drm_dev_vprintk(dev, KERN_DEBUG, p->origin, p->prefix, vaf);
230 }
231 EXPORT_SYMBOL(__drm_printfn_dbg);
232
233 #ifdef __linux__
__drm_printfn_err(struct drm_printer * p,struct va_format * vaf)234 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
235 {
236 struct drm_device *drm = p->arg;
237
238 if (p->prefix)
239 drm_err(drm, "%s %pV", p->prefix, vaf);
240 else
241 drm_err(drm, "%pV", vaf);
242 }
243 EXPORT_SYMBOL(__drm_printfn_err);
244 #else
__drm_printfn_info(struct drm_printer * p,struct va_format * vaf)245 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
246 {
247 #ifdef DRMDEBUG
248 printf("[" DRM_NAME "] ");
249 vprintf(vaf->fmt, *vaf->va);
250 #endif
251 }
252
__drm_printfn_debug(struct drm_printer * p,struct va_format * vaf)253 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
254 {
255 #ifdef DRMDEBUG
256 printf("%s ", p->prefix);
257 vprintf(vaf->fmt, *vaf->va);
258 #endif
259 }
260
__drm_printfn_err(struct drm_printer * p,struct va_format * vaf)261 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
262 {
263 printf("*ERROR* %s ", p->prefix);
264 vprintf(vaf->fmt, *vaf->va);
265 }
266 #endif
267
268 /**
269 * drm_puts - print a const string to a &drm_printer stream
270 * @p: the &drm printer
271 * @str: const string
272 *
273 * Allow &drm_printer types that have a constant string
274 * option to use it.
275 */
drm_puts(struct drm_printer * p,const char * str)276 void drm_puts(struct drm_printer *p, const char *str)
277 {
278 if (p->puts)
279 p->puts(p, str);
280 else
281 drm_printf(p, "%s", str);
282 }
283 EXPORT_SYMBOL(drm_puts);
284
285 /**
286 * drm_printf - print to a &drm_printer stream
287 * @p: the &drm_printer
288 * @f: format string
289 */
drm_printf(struct drm_printer * p,const char * f,...)290 void drm_printf(struct drm_printer *p, const char *f, ...)
291 {
292 va_list args;
293
294 va_start(args, f);
295 drm_vprintf(p, f, &args);
296 va_end(args);
297 }
298 EXPORT_SYMBOL(drm_printf);
299
300 /**
301 * drm_print_bits - print bits to a &drm_printer stream
302 *
303 * Print bits (in flag fields for example) in human readable form.
304 *
305 * @p: the &drm_printer
306 * @value: field value.
307 * @bits: Array with bit names.
308 * @nbits: Size of bit names array.
309 */
drm_print_bits(struct drm_printer * p,unsigned long value,const char * const bits[],unsigned int nbits)310 void drm_print_bits(struct drm_printer *p, unsigned long value,
311 const char * const bits[], unsigned int nbits)
312 {
313 bool first = true;
314 unsigned int i;
315
316 if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value)))
317 nbits = BITS_PER_TYPE(value);
318
319 for_each_set_bit(i, &value, nbits) {
320 if (WARN_ON_ONCE(!bits[i]))
321 continue;
322 drm_printf(p, "%s%s", first ? "" : ",",
323 bits[i]);
324 first = false;
325 }
326 if (first)
327 drm_printf(p, "(none)");
328 }
329 EXPORT_SYMBOL(drm_print_bits);
330
331 #ifdef __linux__
drm_dev_printk(const struct device * dev,const char * level,const char * format,...)332 void drm_dev_printk(const struct device *dev, const char *level,
333 const char *format, ...)
334 {
335 struct va_format vaf;
336 va_list args;
337
338 va_start(args, format);
339 vaf.fmt = format;
340 vaf.va = &args;
341
342 __drm_dev_vprintk(dev, level, __builtin_return_address(0), NULL, &vaf);
343
344 va_end(args);
345 }
346 EXPORT_SYMBOL(drm_dev_printk);
347
__drm_dev_dbg(struct _ddebug * desc,const struct device * dev,enum drm_debug_category category,const char * format,...)348 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
349 enum drm_debug_category category, const char *format, ...)
350 {
351 struct va_format vaf;
352 va_list args;
353
354 if (!__drm_debug_enabled(category))
355 return;
356
357 /* we know we are printing for either syslog, tracefs, or both */
358 va_start(args, format);
359 vaf.fmt = format;
360 vaf.va = &args;
361
362 __drm_dev_vprintk(dev, KERN_DEBUG, __builtin_return_address(0), NULL, &vaf);
363
364 va_end(args);
365 }
366 EXPORT_SYMBOL(__drm_dev_dbg);
367
__drm_err(const char * format,...)368 void __drm_err(const char *format, ...)
369 {
370 struct va_format vaf;
371 va_list args;
372
373 va_start(args, format);
374 vaf.fmt = format;
375 vaf.va = &args;
376
377 __drm_dev_vprintk(NULL, KERN_ERR, __builtin_return_address(0), "*ERROR*", &vaf);
378
379 va_end(args);
380 }
381 EXPORT_SYMBOL(__drm_err);
382
383 #else
384
drm_dev_printk(const struct device * dev,const char * level,const char * format,...)385 void drm_dev_printk(const struct device *dev, const char *level,
386 const char *format, ...)
387 {
388 va_list args;
389
390 #ifndef DRMDEBUG
391 if (level[0] == '\001') {
392 if (level[1] >= KERN_INFO[1] && level[1] < '9')
393 return;
394 }
395 #endif
396
397 va_start(args, format);
398 printk("[" DRM_NAME "] ");
399 vprintf(format, args);
400 va_end(args);
401 }
402
__drm_dev_dbg(struct _ddebug * desc,const struct device * dev,enum drm_debug_category category,const char * format,...)403 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
404 enum drm_debug_category category, const char *format, ...)
405 {
406 va_list args;
407
408 if (!__drm_debug_enabled(category))
409 return;
410
411 /* we know we are printing for either syslog, tracefs, or both */
412 va_start(args, format);
413 printk(KERN_DEBUG "[" DRM_NAME "] ");
414 vprintf(format, args);
415 va_end(args);
416 }
417
___drm_dbg(struct _ddebug * desc,enum drm_debug_category category,const char * format,...)418 void ___drm_dbg(struct _ddebug *desc, enum drm_debug_category category, const char *format, ...)
419 {
420 va_list args;
421
422 if (!__drm_debug_enabled(category))
423 return;
424
425 va_start(args, format);
426 printk(KERN_DEBUG "[" DRM_NAME "] ");
427 vprintf(format, args);
428 va_end(args);
429 }
430
__drm_err(const char * format,...)431 void __drm_err(const char *format, ...)
432 {
433 va_list args;
434
435 va_start(args, format);
436 printk(KERN_ERR "[" DRM_NAME "] *ERROR* ");
437 vprintf(format, args);
438 va_end(args);
439 }
440 #endif /* __linux__ */
441
442 /**
443 * drm_print_regset32 - print the contents of registers to a
444 * &drm_printer stream.
445 *
446 * @p: the &drm printer
447 * @regset: the list of registers to print.
448 *
449 * Often in driver debug, it's useful to be able to either capture the
450 * contents of registers in the steady state using debugfs or at
451 * specific points during operation. This lets the driver have a
452 * single list of registers for both.
453 */
drm_print_regset32(struct drm_printer * p,struct debugfs_regset32 * regset)454 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
455 {
456 #ifdef __linux__
457 int namelen = 0;
458 int i;
459
460 for (i = 0; i < regset->nregs; i++)
461 namelen = max(namelen, (int)strlen(regset->regs[i].name));
462
463 for (i = 0; i < regset->nregs; i++) {
464 drm_printf(p, "%*s = 0x%08x\n",
465 namelen, regset->regs[i].name,
466 readl(regset->base + regset->regs[i].offset));
467 }
468 #endif
469 }
470 EXPORT_SYMBOL(drm_print_regset32);
471