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