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/export.h> 29 #include <linux/io.h> 30 #include <linux/moduleparam.h> 31 #include <linux/seq_file.h> 32 #include <linux/slab.h> 33 #include <linux/stdarg.h> 34 35 #include <drm/drm.h> 36 #include <drm/drm_drv.h> 37 #include <drm/drm_print.h> 38 39 /* 40 * __drm_debug: Enable debug output. 41 * Bitmask of DRM_UT_x. See include/drm/drm_print.h for details. 42 */ 43 unsigned long __drm_debug; 44 EXPORT_SYMBOL(__drm_debug); 45 46 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n" 47 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n" 48 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n" 49 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n" 50 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n" 51 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n" 52 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n" 53 "\t\tBit 6 (0x40) will enable STATE messages (atomic state code)\n" 54 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)\n" 55 "\t\tBit 8 (0x100) will enable DP messages (displayport code)\n" 56 "\t\tBit 9 (0x200) will enable DRMRES messages (managed resources code)"); 57 58 #if !defined(CONFIG_DRM_USE_DYNAMIC_DEBUG) 59 module_param_named(debug, __drm_debug, ulong, 0600); 60 #else 61 /* classnames must match vals of enum drm_debug_category */ 62 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0, 63 "DRM_UT_CORE", 64 "DRM_UT_DRIVER", 65 "DRM_UT_KMS", 66 "DRM_UT_PRIME", 67 "DRM_UT_ATOMIC", 68 "DRM_UT_VBL", 69 "DRM_UT_STATE", 70 "DRM_UT_LEASE", 71 "DRM_UT_DP", 72 "DRM_UT_DRMRES"); 73 74 static struct ddebug_class_param drm_debug_bitmap = { 75 .bits = &__drm_debug, 76 .flags = "p", 77 .map = &drm_debug_classes, 78 }; 79 module_param_cb(debug, ¶m_ops_dyndbg_classes, &drm_debug_bitmap, 0600); 80 #endif 81 82 void __drm_puts_coredump(struct drm_printer *p, const char *str) 83 { 84 struct drm_print_iterator *iterator = p->arg; 85 ssize_t len; 86 87 if (!iterator->remain) 88 return; 89 90 if (iterator->offset < iterator->start) { 91 ssize_t copy; 92 93 len = strlen(str); 94 95 if (iterator->offset + len <= iterator->start) { 96 iterator->offset += len; 97 return; 98 } 99 100 copy = len - (iterator->start - iterator->offset); 101 102 if (copy > iterator->remain) 103 copy = iterator->remain; 104 105 /* Copy out the bit of the string that we need */ 106 if (iterator->data) 107 memcpy(iterator->data, 108 str + (iterator->start - iterator->offset), copy); 109 110 iterator->offset = iterator->start + copy; 111 iterator->remain -= copy; 112 } else { 113 ssize_t pos = iterator->offset - iterator->start; 114 115 len = min_t(ssize_t, strlen(str), iterator->remain); 116 117 if (iterator->data) 118 memcpy(iterator->data + pos, str, len); 119 120 iterator->offset += len; 121 iterator->remain -= len; 122 } 123 } 124 EXPORT_SYMBOL(__drm_puts_coredump); 125 126 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf) 127 { 128 struct drm_print_iterator *iterator = p->arg; 129 size_t len; 130 char *buf; 131 132 if (!iterator->remain) 133 return; 134 135 /* Figure out how big the string will be */ 136 len = snprintf(NULL, 0, "%pV", vaf); 137 138 /* This is the easiest path, we've already advanced beyond the offset */ 139 if (iterator->offset + len <= iterator->start) { 140 iterator->offset += len; 141 return; 142 } 143 144 /* Then check if we can directly copy into the target buffer */ 145 if ((iterator->offset >= iterator->start) && (len < iterator->remain)) { 146 ssize_t pos = iterator->offset - iterator->start; 147 148 if (iterator->data) 149 snprintf(((char *) iterator->data) + pos, 150 iterator->remain, "%pV", vaf); 151 152 iterator->offset += len; 153 iterator->remain -= len; 154 155 return; 156 } 157 158 /* 159 * Finally, hit the slow path and make a temporary string to copy over 160 * using _drm_puts_coredump 161 */ 162 buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); 163 if (!buf) 164 return; 165 166 snprintf(buf, len + 1, "%pV", vaf); 167 __drm_puts_coredump(p, (const char *) buf); 168 169 kfree(buf); 170 } 171 EXPORT_SYMBOL(__drm_printfn_coredump); 172 173 void __drm_puts_seq_file(struct drm_printer *p, const char *str) 174 { 175 seq_puts(p->arg, str); 176 } 177 EXPORT_SYMBOL(__drm_puts_seq_file); 178 179 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf) 180 { 181 seq_printf(p->arg, "%pV", vaf); 182 } 183 EXPORT_SYMBOL(__drm_printfn_seq_file); 184 185 static void __drm_dev_vprintk(const struct device *dev, const char *level, 186 const void *origin, const char *prefix, 187 struct va_format *vaf) 188 { 189 const char *prefix_pad = prefix ? " " : ""; 190 191 if (!prefix) 192 prefix = ""; 193 194 if (dev) { 195 if (origin) 196 dev_printk(level, dev, "[" DRM_NAME ":%ps]%s%s %pV", 197 origin, prefix_pad, prefix, vaf); 198 else 199 dev_printk(level, dev, "[" DRM_NAME "]%s%s %pV", 200 prefix_pad, prefix, vaf); 201 } else { 202 if (origin) 203 printk("%s" "[" DRM_NAME ":%ps]%s%s %pV", 204 level, origin, prefix_pad, prefix, vaf); 205 else 206 printk("%s" "[" DRM_NAME "]%s%s %pV", 207 level, prefix_pad, prefix, vaf); 208 } 209 } 210 211 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf) 212 { 213 dev_info(p->arg, "[" DRM_NAME "] %pV", vaf); 214 } 215 EXPORT_SYMBOL(__drm_printfn_info); 216 217 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf) 218 { 219 const struct drm_device *drm = p->arg; 220 const struct device *dev = drm ? drm->dev : NULL; 221 enum drm_debug_category category = p->category; 222 223 if (!__drm_debug_enabled(category)) 224 return; 225 226 __drm_dev_vprintk(dev, KERN_DEBUG, p->origin, p->prefix, vaf); 227 } 228 EXPORT_SYMBOL(__drm_printfn_dbg); 229 230 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) 231 { 232 struct drm_device *drm = p->arg; 233 234 if (p->prefix) 235 drm_err(drm, "%s %pV", p->prefix, vaf); 236 else 237 drm_err(drm, "%pV", vaf); 238 } 239 EXPORT_SYMBOL(__drm_printfn_err); 240 241 void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf) 242 { 243 unsigned int counter = ++p->line.counter; 244 const char *prefix = p->prefix ?: ""; 245 const char *pad = p->prefix ? " " : ""; 246 247 if (p->line.series) 248 drm_printf(p->arg, "%s%s%u.%u: %pV", 249 prefix, pad, p->line.series, counter, vaf); 250 else 251 drm_printf(p->arg, "%s%s%u: %pV", prefix, pad, counter, vaf); 252 } 253 EXPORT_SYMBOL(__drm_printfn_line); 254 255 /** 256 * drm_puts - print a const string to a &drm_printer stream 257 * @p: the &drm printer 258 * @str: const string 259 * 260 * Allow &drm_printer types that have a constant string 261 * option to use it. 262 */ 263 void drm_puts(struct drm_printer *p, const char *str) 264 { 265 if (p->puts) 266 p->puts(p, str); 267 else 268 drm_printf(p, "%s", str); 269 } 270 EXPORT_SYMBOL(drm_puts); 271 272 /** 273 * drm_printf - print to a &drm_printer stream 274 * @p: the &drm_printer 275 * @f: format string 276 */ 277 void drm_printf(struct drm_printer *p, const char *f, ...) 278 { 279 va_list args; 280 281 va_start(args, f); 282 drm_vprintf(p, f, &args); 283 va_end(args); 284 } 285 EXPORT_SYMBOL(drm_printf); 286 287 /** 288 * drm_print_bits - print bits to a &drm_printer stream 289 * 290 * Print bits (in flag fields for example) in human readable form. 291 * 292 * @p: the &drm_printer 293 * @value: field value. 294 * @bits: Array with bit names. 295 * @nbits: Size of bit names array. 296 */ 297 void drm_print_bits(struct drm_printer *p, unsigned long value, 298 const char * const bits[], unsigned int nbits) 299 { 300 bool first = true; 301 unsigned int i; 302 303 if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value))) 304 nbits = BITS_PER_TYPE(value); 305 306 for_each_set_bit(i, &value, nbits) { 307 if (WARN_ON_ONCE(!bits[i])) 308 continue; 309 drm_printf(p, "%s%s", first ? "" : ",", 310 bits[i]); 311 first = false; 312 } 313 if (first) 314 drm_printf(p, "(none)"); 315 } 316 EXPORT_SYMBOL(drm_print_bits); 317 318 void drm_dev_printk(const struct device *dev, const char *level, 319 const char *format, ...) 320 { 321 struct va_format vaf; 322 va_list args; 323 324 va_start(args, format); 325 vaf.fmt = format; 326 vaf.va = &args; 327 328 __drm_dev_vprintk(dev, level, __builtin_return_address(0), NULL, &vaf); 329 330 va_end(args); 331 } 332 EXPORT_SYMBOL(drm_dev_printk); 333 334 void __drm_dev_dbg(struct _ddebug *desc, const struct device *dev, 335 enum drm_debug_category category, const char *format, ...) 336 { 337 struct va_format vaf; 338 va_list args; 339 340 if (!__drm_debug_enabled(category)) 341 return; 342 343 /* we know we are printing for either syslog, tracefs, or both */ 344 va_start(args, format); 345 vaf.fmt = format; 346 vaf.va = &args; 347 348 __drm_dev_vprintk(dev, KERN_DEBUG, __builtin_return_address(0), NULL, &vaf); 349 350 va_end(args); 351 } 352 EXPORT_SYMBOL(__drm_dev_dbg); 353 354 void __drm_err(const char *format, ...) 355 { 356 struct va_format vaf; 357 va_list args; 358 359 va_start(args, format); 360 vaf.fmt = format; 361 vaf.va = &args; 362 363 __drm_dev_vprintk(NULL, KERN_ERR, __builtin_return_address(0), "*ERROR*", &vaf); 364 365 va_end(args); 366 } 367 EXPORT_SYMBOL(__drm_err); 368 369 /** 370 * drm_print_regset32 - print the contents of registers to a 371 * &drm_printer stream. 372 * 373 * @p: the &drm printer 374 * @regset: the list of registers to print. 375 * 376 * Often in driver debug, it's useful to be able to either capture the 377 * contents of registers in the steady state using debugfs or at 378 * specific points during operation. This lets the driver have a 379 * single list of registers for both. 380 */ 381 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset) 382 { 383 int namelen = 0; 384 int i; 385 386 for (i = 0; i < regset->nregs; i++) 387 namelen = max(namelen, (int)strlen(regset->regs[i].name)); 388 389 for (i = 0; i < regset->nregs; i++) { 390 drm_printf(p, "%*s = 0x%08x\n", 391 namelen, regset->regs[i].name, 392 readl(regset->base + regset->regs[i].offset)); 393 } 394 } 395 EXPORT_SYMBOL(drm_print_regset32); 396 397 /** 398 * drm_print_hex_dump - print a hex dump to a &drm_printer stream 399 * @p: The &drm_printer 400 * @prefix: Prefix for each line, may be NULL for no prefix 401 * @buf: Buffer to dump 402 * @len: Length of buffer 403 * 404 * Print hex dump to &drm_printer, with 16 space-separated hex bytes per line, 405 * optionally with a prefix on each line. No separator is added after prefix. 406 */ 407 void drm_print_hex_dump(struct drm_printer *p, const char *prefix, 408 const u8 *buf, size_t len) 409 { 410 int i; 411 412 for (i = 0; i < len; i += 16) { 413 int bytes_per_line = min(16, len - i); 414 415 drm_printf(p, "%s%*ph\n", prefix ?: "", bytes_per_line, buf + i); 416 } 417 } 418 EXPORT_SYMBOL(drm_print_hex_dump); 419