drm_print.c revision 1.10 1 /* $NetBSD: drm_print.c,v 1.10 2021/12/19 12:04:08 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2016 Red Hat
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robdclark (at) gmail.com>
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: drm_print.c,v 1.10 2021/12/19 12:04:08 riastradh Exp $");
30
31 #ifndef __NetBSD__ /* XXX ??? */
32 #define DEBUG /* for pr_debug() */
33 #endif
34
35 #ifdef __NetBSD__
36 #include <sys/param.h>
37 #include <sys/stdarg.h>
38 #include <sys/cpu.h>
39 #include <sys/device.h>
40 #include <sys/ksyms.h>
41 #else
42 #include <stdarg.h>
43
44 #include <linux/io.h>
45 #include <linux/moduleparam.h>
46 #endif
47 #include <linux/seq_file.h>
48 #include <linux/slab.h>
49
50 #include <drm/drm.h>
51 #include <drm/drm_drv.h>
52 #include <drm/drm_print.h>
53
54 /*
55 * __drm_debug: Enable debug output.
56 * Bitmask of DRM_UT_x. See include/drm/drm_print.h for details.
57 */
58 unsigned int __drm_debug;
59 EXPORT_SYMBOL(__drm_debug);
60
61 #ifdef __linux__
62 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
63 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
64 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
65 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
66 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
67 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
68 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n"
69 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)\n"
70 "\t\tBit 8 (0x100) will enable DP messages (displayport code)");
71 module_param_named(debug, __drm_debug, int, 0600);
72 #endif
73
74 #ifdef __NetBSD__
75 static void
76 drm_symstr(vaddr_t val, char *out, size_t outsize)
77 {
78 unsigned long naddr;
79 const char *mod;
80 const char *sym;
81
82 if (!cpu_intr_p() &&
83 ksyms_getname(&mod, &sym, val, KSYMS_PROC|KSYMS_CLOSEST) == 0) {
84 char offset[32];
85
86 if (ksyms_getval(mod, sym, &naddr, KSYMS_ANY) == 0 &&
87 (val - naddr) != 0)
88 snprintf(offset, sizeof offset, "+%p",
89 (void *)(val - naddr));
90 else
91 offset[0] = '\0';
92 snprintf(out, outsize, "%s:%s%s", mod, sym, offset);
93 return;
94 }
95 snprintf(out, outsize, "%p", (void *)val);
96 }
97 #endif
98
99 void __drm_puts_coredump(struct drm_printer *p, const char *str)
100 {
101 struct drm_print_iterator *iterator = p->arg;
102 ssize_t len;
103
104 if (!iterator->remain)
105 return;
106
107 if (iterator->offset < iterator->start) {
108 ssize_t copy;
109
110 len = strlen(str);
111
112 if (iterator->offset + len <= iterator->start) {
113 iterator->offset += len;
114 return;
115 }
116
117 copy = len - (iterator->start - iterator->offset);
118
119 if (copy > iterator->remain)
120 copy = iterator->remain;
121
122 /* Copy out the bit of the string that we need */
123 memcpy(iterator->data,
124 str + (iterator->start - iterator->offset), copy);
125
126 iterator->offset = iterator->start + copy;
127 iterator->remain -= copy;
128 } else {
129 ssize_t pos = iterator->offset - iterator->start;
130
131 len = min_t(ssize_t, strlen(str), iterator->remain);
132
133 memcpy((char *)iterator->data + pos, str, len);
134
135 iterator->offset += len;
136 iterator->remain -= len;
137 }
138 }
139 EXPORT_SYMBOL(__drm_puts_coredump);
140
141 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
142 {
143 struct drm_print_iterator *iterator = p->arg;
144 size_t len;
145 char *buf;
146
147 if (!iterator->remain)
148 return;
149
150 /* Figure out how big the string will be */
151 len = snprintf(NULL, 0, "%pV", vaf);
152
153 /* This is the easiest path, we've already advanced beyond the offset */
154 if (iterator->offset + len <= iterator->start) {
155 iterator->offset += len;
156 return;
157 }
158
159 /* Then check if we can directly copy into the target buffer */
160 if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
161 ssize_t pos = iterator->offset - iterator->start;
162
163 snprintf(((char *) iterator->data) + pos,
164 iterator->remain, "%pV", vaf);
165
166 iterator->offset += len;
167 iterator->remain -= len;
168
169 return;
170 }
171
172 /*
173 * Finally, hit the slow path and make a temporary string to copy over
174 * using _drm_puts_coredump
175 */
176 buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
177 if (!buf)
178 return;
179
180 snprintf(buf, len + 1, "%pV", vaf);
181 __drm_puts_coredump(p, (const char *) buf);
182
183 kfree(buf);
184 }
185 EXPORT_SYMBOL(__drm_printfn_coredump);
186
187 #ifndef __NetBSD__ /* XXX seq file */
188 void __drm_puts_seq_file(struct drm_printer *p, const char *str)
189 {
190 seq_puts(p->arg, str);
191 }
192 EXPORT_SYMBOL(__drm_puts_seq_file);
193 #endif
194
195 #ifndef __NetBSD__ /* XXX seq file */
196 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
197 {
198 seq_printf(p->arg, "%pV", vaf);
199 }
200 EXPORT_SYMBOL(__drm_printfn_seq_file);
201 #endif
202
203 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
204 {
205 #ifdef __NetBSD__
206 dev_info(p->arg, "[" DRM_NAME "] ");
207 vprintf(vaf->fmt, *vaf->va); /* XXX */
208 #else
209 dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
210 #endif
211 }
212 EXPORT_SYMBOL(__drm_printfn_info);
213
214 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
215 {
216 #ifdef __NetBSD__
217 pr_debug("%s ", p->prefix);
218 vprintf(vaf->fmt, *vaf->va); /* XXX */
219 #else
220 pr_debug("%s %pV", p->prefix, vaf);
221 #endif
222 }
223 EXPORT_SYMBOL(__drm_printfn_debug);
224
225 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
226 {
227 #ifdef __NetBSD__
228 pr_err("*ERROR* %s ", p->prefix);
229 vprintf(vaf->fmt, *vaf->va); /* XXX */
230 #else
231 pr_err("*ERROR* %s %pV", p->prefix, vaf);
232 #endif
233 }
234 EXPORT_SYMBOL(__drm_printfn_err);
235
236 /**
237 * drm_puts - print a const string to a &drm_printer stream
238 * @p: the &drm printer
239 * @str: const string
240 *
241 * Allow &drm_printer types that have a constant string
242 * option to use it.
243 */
244 void drm_puts(struct drm_printer *p, const char *str)
245 {
246 if (p->puts)
247 p->puts(p, str);
248 else
249 drm_printf(p, "%s", str);
250 }
251 EXPORT_SYMBOL(drm_puts);
252
253 /**
254 * drm_printf - print to a &drm_printer stream
255 * @p: the &drm_printer
256 * @f: format string
257 */
258 void drm_printf(struct drm_printer *p, const char *f, ...)
259 {
260 va_list args;
261
262 va_start(args, f);
263 drm_vprintf(p, f, &args);
264 va_end(args);
265 }
266 EXPORT_SYMBOL(drm_printf);
267
268 /**
269 * drm_print_bits - print bits to a &drm_printer stream
270 *
271 * Print bits (in flag fields for example) in human readable form.
272 *
273 * @p: the &drm_printer
274 * @value: field value.
275 * @bits: Array with bit names.
276 * @nbits: Size of bit names array.
277 */
278 void drm_print_bits(struct drm_printer *p, unsigned long value,
279 const char * const bits[], unsigned int nbits)
280 {
281 bool first = true;
282 unsigned int i;
283
284 if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value)))
285 nbits = BITS_PER_TYPE(value);
286
287 for_each_set_bit(i, &value, nbits) {
288 if (WARN_ON_ONCE(!bits[i]))
289 continue;
290 drm_printf(p, "%s%s", first ? "" : ",",
291 bits[i]);
292 first = false;
293 }
294 if (first)
295 drm_printf(p, "(none)");
296 }
297 EXPORT_SYMBOL(drm_print_bits);
298
299 void drm_dev_printk(const struct device *dev, const char *level,
300 const char *format, ...)
301 {
302 #ifdef __NetBSD__
303 va_list va;
304 char symbuf[128];
305
306 drm_symstr((vaddr_t)__builtin_return_address(0), symbuf, sizeof symbuf);
307 if (dev)
308 printf("%s [" DRM_NAME ":%s] ", device_xname(__UNCONST(dev)), symbuf);
309 else
310 printf("[" DRM_NAME ":%s] ", symbuf);
311
312 va_start(va, format);
313 vprintf(format, va);
314 va_end(va);
315 #else
316 struct va_format vaf;
317 va_list args;
318
319 va_start(args, format);
320 vaf.fmt = format;
321 vaf.va = &args;
322
323 if (dev)
324 dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
325 __builtin_return_address(0), &vaf);
326 else
327 printk("%s" "[" DRM_NAME ":%ps] %pV",
328 level, __builtin_return_address(0), &vaf);
329
330 va_end(args);
331 #endif
332 }
333 EXPORT_SYMBOL(drm_dev_printk);
334
335 void drm_dev_dbg(const struct device *dev, enum drm_debug_category category,
336 const char *format, ...)
337 {
338 #ifdef __NetBSD__
339 va_list va;
340 char symbuf[128];
341
342 if (!(__drm_debug & category))
343 return;
344
345 drm_symstr((vaddr_t)__builtin_return_address(0), symbuf, sizeof symbuf);
346 if (dev)
347 printf("%s [" DRM_NAME ":%s] ", device_xname(__UNCONST(dev)), symbuf);
348 else
349 printf("[" DRM_NAME ":%s] ", symbuf);
350
351 va_start(va, format);
352 vprintf(format, va);
353 va_end(va);
354 #else
355 struct va_format vaf;
356 va_list args;
357
358 if (!drm_debug_enabled(category))
359 return;
360
361 va_start(args, format);
362 vaf.fmt = format;
363 vaf.va = &args;
364
365 if (dev)
366 dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
367 __builtin_return_address(0), &vaf);
368 else
369 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
370 __builtin_return_address(0), &vaf);
371
372 va_end(args);
373 #endif
374 }
375 EXPORT_SYMBOL(drm_dev_dbg);
376
377 void __drm_dbg(enum drm_debug_category category, const char *format, ...)
378 {
379 #ifdef __NetBSD__
380 char symbuf[128];
381 va_list va;
382
383 if (!(__drm_debug & category))
384 return;
385
386 drm_symstr((vaddr_t)__builtin_return_address(0), symbuf, sizeof symbuf);
387 printf("[" DRM_NAME ":%s] ", symbuf);
388
389 va_start(va, format);
390 vprintf(format, va);
391 va_end(va);
392 #else
393 struct va_format vaf;
394 va_list args;
395
396 if (!drm_debug_enabled(category))
397 return;
398
399 va_start(args, format);
400 vaf.fmt = format;
401 vaf.va = &args;
402
403 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
404 __builtin_return_address(0), &vaf);
405
406 va_end(args);
407 #endif
408 }
409 EXPORT_SYMBOL(__drm_dbg);
410
411 void __drm_err(const char *format, ...)
412 {
413 #ifdef __NetBSD__
414 char symbuf[128];
415 va_list va;
416
417 drm_symstr((vaddr_t)__builtin_return_address(0), symbuf, sizeof symbuf);
418 printf("[" DRM_NAME ":%s] *ERROR* ", symbuf);
419
420 va_start(va, format);
421 vprintf(format, va);
422 va_end(va);
423 #else
424 struct va_format vaf;
425 va_list args;
426
427 va_start(args, format);
428 vaf.fmt = format;
429 vaf.va = &args;
430
431 printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
432 __builtin_return_address(0), &vaf);
433
434 va_end(args);
435 #endif
436 }
437 EXPORT_SYMBOL(__drm_err);
438
439 #ifndef __NetBSD__
440 /**
441 * drm_print_regset32 - print the contents of registers to a
442 * &drm_printer stream.
443 *
444 * @p: the &drm printer
445 * @regset: the list of registers to print.
446 *
447 * Often in driver debug, it's useful to be able to either capture the
448 * contents of registers in the steady state using debugfs or at
449 * specific points during operation. This lets the driver have a
450 * single list of registers for both.
451 */
452 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
453 {
454 int namelen = 0;
455 int i;
456
457 for (i = 0; i < regset->nregs; i++)
458 namelen = max(namelen, (int)strlen(regset->regs[i].name));
459
460 for (i = 0; i < regset->nregs; i++) {
461 drm_printf(p, "%*s = 0x%08x\n",
462 namelen, regset->regs[i].name,
463 readl(regset->base + regset->regs[i].offset));
464 }
465 }
466 EXPORT_SYMBOL(drm_print_regset32);
467 #endif
468