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 #ifndef DRM_PRINT_H_ 27 #define DRM_PRINT_H_ 28 29 #include <linux/compiler.h> 30 #include <linux/printk.h> 31 #include <linux/seq_file.h> 32 #include <linux/device.h> 33 #include <linux/debugfs.h> 34 35 /** 36 * DOC: print 37 * 38 * A simple wrapper for dev_printk(), seq_printf(), etc. Allows same 39 * debug code to be used for both debugfs and printk logging. 40 * 41 * For example:: 42 * 43 * void log_some_info(struct drm_printer *p) 44 * { 45 * drm_printf(p, "foo=%d\n", foo); 46 * drm_printf(p, "bar=%d\n", bar); 47 * } 48 * 49 * #ifdef CONFIG_DEBUG_FS 50 * void debugfs_show(struct seq_file *f) 51 * { 52 * struct drm_printer p = drm_seq_file_printer(f); 53 * log_some_info(&p); 54 * } 55 * #endif 56 * 57 * void some_other_function(...) 58 * { 59 * struct drm_printer p = drm_info_printer(drm->dev); 60 * log_some_info(&p); 61 * } 62 */ 63 64 /** 65 * struct drm_printer - drm output "stream" 66 * 67 * Do not use struct members directly. Use drm_printer_seq_file(), 68 * drm_printer_info(), etc to initialize. And drm_printf() for output. 69 */ 70 struct drm_printer { 71 /* private: */ 72 void (*printfn)(struct drm_printer *p, struct va_format *vaf); 73 void (*puts)(struct drm_printer *p, const char *str); 74 void *arg; 75 const char *prefix; 76 }; 77 78 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf); 79 void __drm_puts_coredump(struct drm_printer *p, const char *str); 80 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf); 81 void __drm_puts_seq_file(struct drm_printer *p, const char *str); 82 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf); 83 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf); 84 85 __printf(2, 3) 86 void drm_printf(struct drm_printer *p, const char *f, ...); 87 void drm_puts(struct drm_printer *p, const char *str); 88 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset); 89 90 __printf(2, 0) 91 /** 92 * drm_vprintf - print to a &drm_printer stream 93 * @p: the &drm_printer 94 * @fmt: format string 95 * @va: the va_list 96 */ 97 static inline void 98 drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va) 99 { 100 struct va_format vaf = { .fmt = fmt, .va = va }; 101 102 p->printfn(p, &vaf); 103 } 104 105 /** 106 * drm_printf_indent - Print to a &drm_printer stream with indentation 107 * @printer: DRM printer 108 * @indent: Tab indentation level (max 5) 109 * @fmt: Format string 110 */ 111 #define drm_printf_indent(printer, indent, fmt, ...) \ 112 drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__) 113 114 /** 115 * struct drm_print_iterator - local struct used with drm_printer_coredump 116 * @data: Pointer to the devcoredump output buffer 117 * @start: The offset within the buffer to start writing 118 * @remain: The number of bytes to write for this iteration 119 */ 120 struct drm_print_iterator { 121 void *data; 122 ssize_t start; 123 ssize_t remain; 124 /* private: */ 125 ssize_t offset; 126 }; 127 128 /** 129 * drm_coredump_printer - construct a &drm_printer that can output to a buffer 130 * from the read function for devcoredump 131 * @iter: A pointer to a struct drm_print_iterator for the read instance 132 * 133 * This wrapper extends drm_printf() to work with a dev_coredumpm() callback 134 * function. The passed in drm_print_iterator struct contains the buffer 135 * pointer, size and offset as passed in from devcoredump. 136 * 137 * For example:: 138 * 139 * void coredump_read(char *buffer, loff_t offset, size_t count, 140 * void *data, size_t datalen) 141 * { 142 * struct drm_print_iterator iter; 143 * struct drm_printer p; 144 * 145 * iter.data = buffer; 146 * iter.start = offset; 147 * iter.remain = count; 148 * 149 * p = drm_coredump_printer(&iter); 150 * 151 * drm_printf(p, "foo=%d\n", foo); 152 * } 153 * 154 * void makecoredump(...) 155 * { 156 * ... 157 * dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL, 158 * coredump_read, ...) 159 * } 160 * 161 * RETURNS: 162 * The &drm_printer object 163 */ 164 static inline struct drm_printer 165 drm_coredump_printer(struct drm_print_iterator *iter) 166 { 167 struct drm_printer p = { 168 .printfn = __drm_printfn_coredump, 169 .puts = __drm_puts_coredump, 170 .arg = iter, 171 }; 172 173 /* Set the internal offset of the iterator to zero */ 174 iter->offset = 0; 175 176 return p; 177 } 178 179 /** 180 * drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file 181 * @f: the &struct seq_file to output to 182 * 183 * RETURNS: 184 * The &drm_printer object 185 */ 186 static inline struct drm_printer drm_seq_file_printer(struct seq_file *f) 187 { 188 struct drm_printer p = { 189 .printfn = __drm_printfn_seq_file, 190 .puts = __drm_puts_seq_file, 191 .arg = f, 192 }; 193 return p; 194 } 195 196 /** 197 * drm_info_printer - construct a &drm_printer that outputs to dev_printk() 198 * @dev: the &struct device pointer 199 * 200 * RETURNS: 201 * The &drm_printer object 202 */ 203 static inline struct drm_printer drm_info_printer(struct device *dev) 204 { 205 struct drm_printer p = { 206 .printfn = __drm_printfn_info, 207 .arg = dev, 208 }; 209 return p; 210 } 211 212 /** 213 * drm_debug_printer - construct a &drm_printer that outputs to pr_debug() 214 * @prefix: debug output prefix 215 * 216 * RETURNS: 217 * The &drm_printer object 218 */ 219 static inline struct drm_printer drm_debug_printer(const char *prefix) 220 { 221 struct drm_printer p = { 222 .printfn = __drm_printfn_debug, 223 .prefix = prefix 224 }; 225 return p; 226 } 227 228 /* 229 * The following categories are defined: 230 * 231 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ... 232 * This is the category used by the DRM_DEBUG() macro. 233 * 234 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ... 235 * This is the category used by the DRM_DEBUG_DRIVER() macro. 236 * 237 * KMS: used in the modesetting code. 238 * This is the category used by the DRM_DEBUG_KMS() macro. 239 * 240 * PRIME: used in the prime code. 241 * This is the category used by the DRM_DEBUG_PRIME() macro. 242 * 243 * ATOMIC: used in the atomic code. 244 * This is the category used by the DRM_DEBUG_ATOMIC() macro. 245 * 246 * VBL: used for verbose debug message in the vblank code 247 * This is the category used by the DRM_DEBUG_VBL() macro. 248 * 249 * Enabling verbose debug messages is done through the drm.debug parameter, 250 * each category being enabled by a bit. 251 * 252 * drm.debug=0x1 will enable CORE messages 253 * drm.debug=0x2 will enable DRIVER messages 254 * drm.debug=0x3 will enable CORE and DRIVER messages 255 * ... 256 * drm.debug=0x3f will enable all messages 257 * 258 * An interesting feature is that it's possible to enable verbose logging at 259 * run-time by echoing the debug value in its sysfs node: 260 * # echo 0xf > /sys/module/drm/parameters/debug 261 */ 262 #define DRM_UT_NONE 0x00 263 #define DRM_UT_CORE 0x01 264 #define DRM_UT_DRIVER 0x02 265 #define DRM_UT_KMS 0x04 266 #define DRM_UT_PRIME 0x08 267 #define DRM_UT_ATOMIC 0x10 268 #define DRM_UT_VBL 0x20 269 #define DRM_UT_STATE 0x40 270 #define DRM_UT_LEASE 0x80 271 #define DRM_UT_DP 0x100 272 273 __printf(3, 4) 274 void drm_dev_printk(const struct device *dev, const char *level, 275 const char *format, ...); 276 __printf(3, 4) 277 void drm_dev_dbg(const struct device *dev, unsigned int category, 278 const char *format, ...); 279 280 __printf(2, 3) 281 void drm_dbg(unsigned int category, const char *format, ...); 282 __printf(1, 2) 283 void drm_err(const char *format, ...); 284 285 /* Macros to make printk easier */ 286 287 #define _DRM_PRINTK(once, level, fmt, ...) \ 288 printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__) 289 290 #define DRM_INFO(fmt, ...) \ 291 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__) 292 #define DRM_NOTE(fmt, ...) \ 293 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__) 294 #define DRM_WARN(fmt, ...) \ 295 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__) 296 297 #define DRM_INFO_ONCE(fmt, ...) \ 298 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__) 299 #define DRM_NOTE_ONCE(fmt, ...) \ 300 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__) 301 #define DRM_WARN_ONCE(fmt, ...) \ 302 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__) 303 304 /** 305 * Error output. 306 * 307 * @dev: device pointer 308 * @fmt: printf() like format string. 309 */ 310 #define DRM_DEV_ERROR(dev, fmt, ...) \ 311 drm_dev_printk(dev, KERN_ERR, "*ERROR* " fmt, ##__VA_ARGS__) 312 #define DRM_ERROR(fmt, ...) \ 313 drm_err(fmt, ##__VA_ARGS__) 314 315 /** 316 * Rate limited error output. Like DRM_ERROR() but won't flood the log. 317 * 318 * @dev: device pointer 319 * @fmt: printf() like format string. 320 */ 321 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \ 322 ({ \ 323 static DEFINE_RATELIMIT_STATE(_rs, \ 324 DEFAULT_RATELIMIT_INTERVAL, \ 325 DEFAULT_RATELIMIT_BURST); \ 326 \ 327 if (__ratelimit(&_rs)) \ 328 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \ 329 }) 330 #define DRM_ERROR_RATELIMITED(fmt, ...) \ 331 DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__) 332 333 #define DRM_DEV_INFO(dev, fmt, ...) \ 334 drm_dev_printk(dev, KERN_INFO, fmt, ##__VA_ARGS__) 335 336 #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \ 337 ({ \ 338 static bool __print_once __read_mostly; \ 339 if (!__print_once) { \ 340 __print_once = true; \ 341 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \ 342 } \ 343 }) 344 345 /** 346 * Debug output. 347 * 348 * @dev: device pointer 349 * @fmt: printf() like format string. 350 */ 351 #define DRM_DEV_DEBUG(dev, fmt, ...) \ 352 drm_dev_dbg(dev, DRM_UT_CORE, fmt, ##__VA_ARGS__) 353 #define DRM_DEBUG(fmt, ...) \ 354 drm_dbg(DRM_UT_CORE, fmt, ##__VA_ARGS__) 355 356 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, ...) \ 357 drm_dev_dbg(dev, DRM_UT_DRIVER, fmt, ##__VA_ARGS__) 358 #define DRM_DEBUG_DRIVER(fmt, ...) \ 359 drm_dbg(DRM_UT_DRIVER, fmt, ##__VA_ARGS__) 360 361 #define DRM_DEV_DEBUG_KMS(dev, fmt, ...) \ 362 drm_dev_dbg(dev, DRM_UT_KMS, fmt, ##__VA_ARGS__) 363 #define DRM_DEBUG_KMS(fmt, ...) \ 364 drm_dbg(DRM_UT_KMS, fmt, ##__VA_ARGS__) 365 366 #define DRM_DEV_DEBUG_PRIME(dev, fmt, ...) \ 367 drm_dev_dbg(dev, DRM_UT_PRIME, fmt, ##__VA_ARGS__) 368 #define DRM_DEBUG_PRIME(fmt, ...) \ 369 drm_dbg(DRM_UT_PRIME, fmt, ##__VA_ARGS__) 370 371 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, ...) \ 372 drm_dev_dbg(dev, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__) 373 #define DRM_DEBUG_ATOMIC(fmt, ...) \ 374 drm_dbg(DRM_UT_ATOMIC, fmt, ##__VA_ARGS__) 375 376 #define DRM_DEV_DEBUG_VBL(dev, fmt, ...) \ 377 drm_dev_dbg(dev, DRM_UT_VBL, fmt, ##__VA_ARGS__) 378 #define DRM_DEBUG_VBL(fmt, ...) \ 379 drm_dbg(DRM_UT_VBL, fmt, ##__VA_ARGS__) 380 381 #define DRM_DEBUG_LEASE(fmt, ...) \ 382 drm_dbg(DRM_UT_LEASE, fmt, ##__VA_ARGS__) 383 384 #define DRM_DEV_DEBUG_DP(dev, fmt, ...) \ 385 drm_dev_dbg(dev, DRM_UT_DP, fmt, ## __VA_ARGS__) 386 #define DRM_DEBUG_DP(fmt, ...) \ 387 drm_dbg(DRM_UT_DP, fmt, ## __VA_ARGS__) 388 389 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, category, fmt, ...) \ 390 ({ \ 391 static DEFINE_RATELIMIT_STATE(_rs, \ 392 DEFAULT_RATELIMIT_INTERVAL, \ 393 DEFAULT_RATELIMIT_BURST); \ 394 if (__ratelimit(&_rs)) \ 395 drm_dev_dbg(dev, category, fmt, ##__VA_ARGS__); \ 396 }) 397 398 /** 399 * Rate limited debug output. Like DRM_DEBUG() but won't flood the log. 400 * 401 * @dev: device pointer 402 * @fmt: printf() like format string. 403 */ 404 #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, ...) \ 405 _DEV_DRM_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_CORE, \ 406 fmt, ##__VA_ARGS__) 407 #define DRM_DEBUG_RATELIMITED(fmt, ...) \ 408 DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##__VA_ARGS__) 409 410 #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, ...) \ 411 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_DRIVER, \ 412 fmt, ##__VA_ARGS__) 413 #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, ...) \ 414 DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##__VA_ARGS__) 415 416 #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, ...) \ 417 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_KMS, \ 418 fmt, ##__VA_ARGS__) 419 #define DRM_DEBUG_KMS_RATELIMITED(fmt, ...) \ 420 DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##__VA_ARGS__) 421 422 #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, ...) \ 423 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRM_UT_PRIME, \ 424 fmt, ##__VA_ARGS__) 425 #define DRM_DEBUG_PRIME_RATELIMITED(fmt, ...) \ 426 DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##__VA_ARGS__) 427 428 #endif /* DRM_PRINT_H_ */ 429