drm_print.c 11 KB

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