xref: /openbsd-src/sys/dev/pci/drm/drm_print.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
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 <sys/stdarg.h>
27 
28 #include <linux/io.h>
29 #include <linux/moduleparam.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/dynamic_debug.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, &param_ops_dyndbg_classes, &drm_debug_bitmap, 0600);
81 #endif
82 
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 		memcpy(iterator->data,
108 			str + (iterator->start - iterator->offset), copy);
109 
110 		iterator->offset = iterator->start + copy;
111 		iterator->remain -= copy;
112 	} else {
113 		ssize_t pos = iterator->offset - iterator->start;
114 
115 		len = min_t(ssize_t, strlen(str), iterator->remain);
116 
117 		memcpy(iterator->data + pos, str, len);
118 
119 		iterator->offset += len;
120 		iterator->remain -= len;
121 	}
122 }
123 EXPORT_SYMBOL(__drm_puts_coredump);
124 
125 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
126 {
127 	struct drm_print_iterator *iterator = p->arg;
128 	size_t len;
129 	char *buf;
130 
131 	if (!iterator->remain)
132 		return;
133 
134 	/* Figure out how big the string will be */
135 	len = snprintf(NULL, 0, "%pV", vaf);
136 
137 	/* This is the easiest path, we've already advanced beyond the offset */
138 	if (iterator->offset + len <= iterator->start) {
139 		iterator->offset += len;
140 		return;
141 	}
142 
143 	/* Then check if we can directly copy into the target buffer */
144 	if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
145 		ssize_t pos = iterator->offset - iterator->start;
146 
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 
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 
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 
183 #ifdef __linux__
184 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
185 {
186 	dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
187 }
188 EXPORT_SYMBOL(__drm_printfn_info);
189 
190 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
191 {
192 	/* pr_debug callsite decorations are unhelpful here */
193 	printk(KERN_DEBUG "%s %pV", p->prefix, vaf);
194 }
195 EXPORT_SYMBOL(__drm_printfn_debug);
196 
197 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
198 {
199 	pr_err("*ERROR* %s %pV", p->prefix, vaf);
200 }
201 EXPORT_SYMBOL(__drm_printfn_err);
202 #else
203 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
204 {
205 #ifdef DRMDEBUG
206 	printf("[" DRM_NAME "] ");
207 	vprintf(vaf->fmt, *vaf->va);
208 #endif
209 }
210 
211 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
212 {
213 #ifdef DRMDEBUG
214 	printf("%s ", p->prefix);
215 	vprintf(vaf->fmt, *vaf->va);
216 #endif
217 }
218 
219 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
220 {
221 	printf("*ERROR* %s ", p->prefix);
222 	vprintf(vaf->fmt, *vaf->va);
223 }
224 #endif
225 
226 /**
227  * drm_puts - print a const string to a &drm_printer stream
228  * @p: the &drm printer
229  * @str: const string
230  *
231  * Allow &drm_printer types that have a constant string
232  * option to use it.
233  */
234 void drm_puts(struct drm_printer *p, const char *str)
235 {
236 	if (p->puts)
237 		p->puts(p, str);
238 	else
239 		drm_printf(p, "%s", str);
240 }
241 EXPORT_SYMBOL(drm_puts);
242 
243 /**
244  * drm_printf - print to a &drm_printer stream
245  * @p: the &drm_printer
246  * @f: format string
247  */
248 void drm_printf(struct drm_printer *p, const char *f, ...)
249 {
250 	va_list args;
251 
252 	va_start(args, f);
253 	drm_vprintf(p, f, &args);
254 	va_end(args);
255 }
256 EXPORT_SYMBOL(drm_printf);
257 
258 /**
259  * drm_print_bits - print bits to a &drm_printer stream
260  *
261  * Print bits (in flag fields for example) in human readable form.
262  *
263  * @p: the &drm_printer
264  * @value: field value.
265  * @bits: Array with bit names.
266  * @nbits: Size of bit names array.
267  */
268 void drm_print_bits(struct drm_printer *p, unsigned long value,
269 		    const char * const bits[], unsigned int nbits)
270 {
271 	bool first = true;
272 	unsigned int i;
273 
274 	if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value)))
275 		nbits = BITS_PER_TYPE(value);
276 
277 	for_each_set_bit(i, &value, nbits) {
278 		if (WARN_ON_ONCE(!bits[i]))
279 			continue;
280 		drm_printf(p, "%s%s", first ? "" : ",",
281 			   bits[i]);
282 		first = false;
283 	}
284 	if (first)
285 		drm_printf(p, "(none)");
286 }
287 EXPORT_SYMBOL(drm_print_bits);
288 
289 #ifdef __linux__
290 void drm_dev_printk(const struct device *dev, const char *level,
291 		    const char *format, ...)
292 {
293 	struct va_format vaf;
294 	va_list args;
295 
296 	va_start(args, format);
297 	vaf.fmt = format;
298 	vaf.va = &args;
299 
300 	if (dev)
301 		dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
302 			   __builtin_return_address(0), &vaf);
303 	else
304 		printk("%s" "[" DRM_NAME ":%ps] %pV",
305 		       level, __builtin_return_address(0), &vaf);
306 
307 	va_end(args);
308 }
309 EXPORT_SYMBOL(drm_dev_printk);
310 
311 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
312 		   enum drm_debug_category category, const char *format, ...)
313 {
314 	struct va_format vaf;
315 	va_list args;
316 
317 	if (!__drm_debug_enabled(category))
318 		return;
319 
320 	/* we know we are printing for either syslog, tracefs, or both */
321 	va_start(args, format);
322 	vaf.fmt = format;
323 	vaf.va = &args;
324 
325 	if (dev)
326 		dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
327 			   __builtin_return_address(0), &vaf);
328 	else
329 		printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
330 		       __builtin_return_address(0), &vaf);
331 
332 	va_end(args);
333 }
334 EXPORT_SYMBOL(__drm_dev_dbg);
335 
336 void ___drm_dbg(struct _ddebug *desc, enum drm_debug_category category, const char *format, ...)
337 {
338 	struct va_format vaf;
339 	va_list args;
340 
341 	if (!__drm_debug_enabled(category))
342 		return;
343 
344 	va_start(args, format);
345 	vaf.fmt = format;
346 	vaf.va = &args;
347 
348 	printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
349 	       __builtin_return_address(0), &vaf);
350 
351 	va_end(args);
352 }
353 EXPORT_SYMBOL(___drm_dbg);
354 
355 void __drm_err(const char *format, ...)
356 {
357 	struct va_format vaf;
358 	va_list args;
359 
360 	va_start(args, format);
361 	vaf.fmt = format;
362 	vaf.va = &args;
363 
364 	printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
365 	       __builtin_return_address(0), &vaf);
366 
367 	va_end(args);
368 }
369 EXPORT_SYMBOL(__drm_err);
370 
371 #else
372 
373 void drm_dev_printk(const struct device *dev, const char *level,
374 		    const char *format, ...)
375 {
376 	va_list args;
377 
378 #ifndef DRMDEBUG
379 	if (level[0] == '\001') {
380 		if (level[1] >= KERN_INFO[1] && level[1] < '9')
381 			return;
382 	}
383 #endif
384 
385 	va_start(args, format);
386 	printk("[" DRM_NAME "] ");
387 	vprintf(format, args);
388 	va_end(args);
389 }
390 
391 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev,
392 		   enum drm_debug_category category, const char *format, ...)
393 {
394 	va_list args;
395 
396 	if (!__drm_debug_enabled(category))
397 		return;
398 
399 	/* we know we are printing for either syslog, tracefs, or both */
400 	va_start(args, format);
401 	printk(KERN_DEBUG "[" DRM_NAME "] ");
402 	vprintf(format, args);
403 	va_end(args);
404 }
405 
406 void ___drm_dbg(struct _ddebug *desc, enum drm_debug_category category, const char *format, ...)
407 {
408 	va_list args;
409 
410 	if (!__drm_debug_enabled(category))
411 		return;
412 
413 	va_start(args, format);
414 	printk(KERN_DEBUG "[" DRM_NAME "] ");
415 	vprintf(format, args);
416 	va_end(args);
417 }
418 
419 void __drm_err(const char *format, ...)
420 {
421 	va_list args;
422 
423 	va_start(args, format);
424 	printk(KERN_ERR "[" DRM_NAME "] *ERROR* ");
425 	vprintf(format, args);
426 	va_end(args);
427 }
428 #endif /* __linux__ */
429 
430 /**
431  * drm_print_regset32 - print the contents of registers to a
432  * &drm_printer stream.
433  *
434  * @p: the &drm printer
435  * @regset: the list of registers to print.
436  *
437  * Often in driver debug, it's useful to be able to either capture the
438  * contents of registers in the steady state using debugfs or at
439  * specific points during operation.  This lets the driver have a
440  * single list of registers for both.
441  */
442 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
443 {
444 #ifdef __linux__
445 	int namelen = 0;
446 	int i;
447 
448 	for (i = 0; i < regset->nregs; i++)
449 		namelen = max(namelen, (int)strlen(regset->regs[i].name));
450 
451 	for (i = 0; i < regset->nregs; i++) {
452 		drm_printf(p, "%*s = 0x%08x\n",
453 			   namelen, regset->regs[i].name,
454 			   readl(regset->base + regset->regs[i].offset));
455 	}
456 #endif
457 }
458 EXPORT_SYMBOL(drm_print_regset32);
459