drm_print.c 9.3 KB

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