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