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