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