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