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