2 * Copyright (C) 2016 Red Hat
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #define DEBUG /* for pr_debug() */
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_print.h>
38 void __drm_puts_coredump(struct drm_printer *p, const char *str)
40 struct drm_print_iterator *iterator = p->arg;
43 if (!iterator->remain)
46 if (iterator->offset < iterator->start) {
51 if (iterator->offset + len <= iterator->start) {
52 iterator->offset += len;
56 copy = len - (iterator->start - iterator->offset);
58 if (copy > iterator->remain)
59 copy = iterator->remain;
61 /* Copy out the bit of the string that we need */
62 memcpy(iterator->data,
63 str + (iterator->start - iterator->offset), copy);
65 iterator->offset = iterator->start + copy;
66 iterator->remain -= copy;
68 ssize_t pos = iterator->offset - iterator->start;
70 len = min_t(ssize_t, strlen(str), iterator->remain);
72 memcpy(iterator->data + pos, str, len);
74 iterator->offset += len;
75 iterator->remain -= len;
78 EXPORT_SYMBOL(__drm_puts_coredump);
80 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
82 struct drm_print_iterator *iterator = p->arg;
86 if (!iterator->remain)
89 /* Figure out how big the string will be */
90 len = snprintf(NULL, 0, "%pV", vaf);
92 /* This is the easiest path, we've already advanced beyond the offset */
93 if (iterator->offset + len <= iterator->start) {
94 iterator->offset += len;
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;
102 snprintf(((char *) iterator->data) + pos,
103 iterator->remain, "%pV", vaf);
105 iterator->offset += len;
106 iterator->remain -= len;
112 * Finally, hit the slow path and make a temporary string to copy over
113 * using _drm_puts_coredump
115 buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
119 snprintf(buf, len + 1, "%pV", vaf);
120 __drm_puts_coredump(p, (const char *) buf);
124 EXPORT_SYMBOL(__drm_printfn_coredump);
126 void __drm_puts_seq_file(struct drm_printer *p, const char *str)
128 seq_puts(p->arg, str);
130 EXPORT_SYMBOL(__drm_puts_seq_file);
132 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
134 seq_printf(p->arg, "%pV", vaf);
136 EXPORT_SYMBOL(__drm_printfn_seq_file);
138 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
140 dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
142 EXPORT_SYMBOL(__drm_printfn_info);
144 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
146 pr_debug("%s %pV", p->prefix, vaf);
148 EXPORT_SYMBOL(__drm_printfn_debug);
151 * drm_puts - print a const string to a &drm_printer stream
152 * @p: the &drm printer
155 * Allow &drm_printer types that have a constant string
158 void drm_puts(struct drm_printer *p, const char *str)
163 drm_printf(p, "%s", str);
165 EXPORT_SYMBOL(drm_puts);
168 * drm_printf - print to a &drm_printer stream
169 * @p: the &drm_printer
172 void drm_printf(struct drm_printer *p, const char *f, ...)
177 drm_vprintf(p, f, &args);
180 EXPORT_SYMBOL(drm_printf);
182 void drm_dev_printk(const struct device *dev, const char *level,
183 const char *format, ...)
185 struct va_format vaf;
188 va_start(args, format);
193 dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
194 __builtin_return_address(0), &vaf);
196 printk("%s" "[" DRM_NAME ":%ps] %pV",
197 level, __builtin_return_address(0), &vaf);
201 EXPORT_SYMBOL(drm_dev_printk);
203 void drm_dev_dbg(const struct device *dev, unsigned int category,
204 const char *format, ...)
206 struct va_format vaf;
209 if (!(drm_debug & category))
212 va_start(args, format);
217 dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
218 __builtin_return_address(0), &vaf);
220 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
221 __builtin_return_address(0), &vaf);
225 EXPORT_SYMBOL(drm_dev_dbg);
227 void drm_dbg(unsigned int category, const char *format, ...)
229 struct va_format vaf;
232 if (!(drm_debug & category))
235 va_start(args, format);
239 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
240 __builtin_return_address(0), &vaf);
244 EXPORT_SYMBOL(drm_dbg);
246 void drm_err(const char *format, ...)
248 struct va_format vaf;
251 va_start(args, format);
255 printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
256 __builtin_return_address(0), &vaf);
260 EXPORT_SYMBOL(drm_err);
263 * drm_print_regset32 - print the contents of registers to a
264 * &drm_printer stream.
266 * @p: the &drm printer
267 * @regset: the list of registers to print.
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.
274 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
279 for (i = 0; i < regset->nregs; i++)
280 namelen = max(namelen, (int)strlen(regset->regs[i].name));
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));
288 EXPORT_SYMBOL(drm_print_regset32);