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