]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/drm_print.c
dfa27367ebb8537ec8874616e0f0b5532de34121
[linux.git] / drivers / gpu / drm / drm_print.c
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
38 void __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 }
78 EXPORT_SYMBOL(__drm_puts_coredump);
79
80 void __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 }
124 EXPORT_SYMBOL(__drm_printfn_coredump);
125
126 void __drm_puts_seq_file(struct drm_printer *p, const char *str)
127 {
128         seq_puts(p->arg, str);
129 }
130 EXPORT_SYMBOL(__drm_puts_seq_file);
131
132 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
133 {
134         seq_printf(p->arg, "%pV", vaf);
135 }
136 EXPORT_SYMBOL(__drm_printfn_seq_file);
137
138 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
139 {
140         dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
141 }
142 EXPORT_SYMBOL(__drm_printfn_info);
143
144 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
145 {
146         pr_debug("%s %pV", p->prefix, vaf);
147 }
148 EXPORT_SYMBOL(__drm_printfn_debug);
149
150 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
151 {
152         pr_err("*ERROR* %s %pV", p->prefix, vaf);
153 }
154 EXPORT_SYMBOL(__drm_printfn_err);
155
156 /**
157  * drm_puts - print a const string to a &drm_printer stream
158  * @p: the &drm printer
159  * @str: const string
160  *
161  * Allow &drm_printer types that have a constant string
162  * option to use it.
163  */
164 void drm_puts(struct drm_printer *p, const char *str)
165 {
166         if (p->puts)
167                 p->puts(p, str);
168         else
169                 drm_printf(p, "%s", str);
170 }
171 EXPORT_SYMBOL(drm_puts);
172
173 /**
174  * drm_printf - print to a &drm_printer stream
175  * @p: the &drm_printer
176  * @f: format string
177  */
178 void drm_printf(struct drm_printer *p, const char *f, ...)
179 {
180         va_list args;
181
182         va_start(args, f);
183         drm_vprintf(p, f, &args);
184         va_end(args);
185 }
186 EXPORT_SYMBOL(drm_printf);
187
188 /**
189  * drm_print_bits - print bits to a &drm_printer stream
190  *
191  * Print bits (in flag fields for example) in human readable form.
192  * The first name in the @bits array is for the bit indexed by @from.
193  *
194  * @p: the &drm_printer
195  * @value: field value.
196  * @bits: Array with bit names.
197  * @from: start of bit range to print (inclusive).
198  * @to: end of bit range to print (exclusive).
199  */
200 void drm_print_bits(struct drm_printer *p,
201                     unsigned long value, const char *bits[],
202                     unsigned int from, unsigned int to)
203 {
204         bool first = true;
205         unsigned int i;
206
207         for (i = from; i < to; i++) {
208                 if (!(value & (1 << i)))
209                         continue;
210                 if (WARN_ON_ONCE(!bits[i-from]))
211                         continue;
212                 drm_printf(p, "%s%s", first ? "" : ",",
213                            bits[i-from]);
214                 first = false;
215         }
216         if (first)
217                 drm_printf(p, "(none)");
218 }
219 EXPORT_SYMBOL(drm_print_bits);
220
221 void drm_dev_printk(const struct device *dev, const char *level,
222                     const char *format, ...)
223 {
224         struct va_format vaf;
225         va_list args;
226
227         va_start(args, format);
228         vaf.fmt = format;
229         vaf.va = &args;
230
231         if (dev)
232                 dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
233                            __builtin_return_address(0), &vaf);
234         else
235                 printk("%s" "[" DRM_NAME ":%ps] %pV",
236                        level, __builtin_return_address(0), &vaf);
237
238         va_end(args);
239 }
240 EXPORT_SYMBOL(drm_dev_printk);
241
242 void drm_dev_dbg(const struct device *dev, unsigned int category,
243                  const char *format, ...)
244 {
245         struct va_format vaf;
246         va_list args;
247
248         if (!(drm_debug & category))
249                 return;
250
251         va_start(args, format);
252         vaf.fmt = format;
253         vaf.va = &args;
254
255         if (dev)
256                 dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
257                            __builtin_return_address(0), &vaf);
258         else
259                 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
260                        __builtin_return_address(0), &vaf);
261
262         va_end(args);
263 }
264 EXPORT_SYMBOL(drm_dev_dbg);
265
266 void drm_dbg(unsigned int category, const char *format, ...)
267 {
268         struct va_format vaf;
269         va_list args;
270
271         if (!(drm_debug & category))
272                 return;
273
274         va_start(args, format);
275         vaf.fmt = format;
276         vaf.va = &args;
277
278         printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
279                __builtin_return_address(0), &vaf);
280
281         va_end(args);
282 }
283 EXPORT_SYMBOL(drm_dbg);
284
285 void drm_err(const char *format, ...)
286 {
287         struct va_format vaf;
288         va_list args;
289
290         va_start(args, format);
291         vaf.fmt = format;
292         vaf.va = &args;
293
294         printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
295                __builtin_return_address(0), &vaf);
296
297         va_end(args);
298 }
299 EXPORT_SYMBOL(drm_err);
300
301 /**
302  * drm_print_regset32 - print the contents of registers to a
303  * &drm_printer stream.
304  *
305  * @p: the &drm printer
306  * @regset: the list of registers to print.
307  *
308  * Often in driver debug, it's useful to be able to either capture the
309  * contents of registers in the steady state using debugfs or at
310  * specific points during operation.  This lets the driver have a
311  * single list of registers for both.
312  */
313 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
314 {
315         int namelen = 0;
316         int i;
317
318         for (i = 0; i < regset->nregs; i++)
319                 namelen = max(namelen, (int)strlen(regset->regs[i].name));
320
321         for (i = 0; i < regset->nregs; i++) {
322                 drm_printf(p, "%*s = 0x%08x\n",
323                            namelen, regset->regs[i].name,
324                            readl(regset->base + regset->regs[i].offset));
325         }
326 }
327 EXPORT_SYMBOL(drm_print_regset32);