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