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