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