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