drm_print.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #define DEBUG /* for pr_debug() */
  26. #include <stdarg.h>
  27. #include <linux/io.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/slab.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 int __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. module_param_named(debug, __drm_debug, int, 0600);
  50. void __drm_puts_coredump(struct drm_printer *p, const char *str)
  51. {
  52. struct drm_print_iterator *iterator = p->arg;
  53. ssize_t len;
  54. if (!iterator->remain)
  55. return;
  56. if (iterator->offset < iterator->start) {
  57. ssize_t copy;
  58. len = strlen(str);
  59. if (iterator->offset + len <= iterator->start) {
  60. iterator->offset += len;
  61. return;
  62. }
  63. copy = len - (iterator->start - iterator->offset);
  64. if (copy > iterator->remain)
  65. copy = iterator->remain;
  66. /* Copy out the bit of the string that we need */
  67. memcpy(iterator->data,
  68. str + (iterator->start - iterator->offset), copy);
  69. iterator->offset = iterator->start + copy;
  70. iterator->remain -= copy;
  71. } else {
  72. ssize_t pos = iterator->offset - iterator->start;
  73. len = min_t(ssize_t, strlen(str), iterator->remain);
  74. memcpy(iterator->data + pos, str, len);
  75. iterator->offset += len;
  76. iterator->remain -= len;
  77. }
  78. }
  79. EXPORT_SYMBOL(__drm_puts_coredump);
  80. void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
  81. {
  82. struct drm_print_iterator *iterator = p->arg;
  83. size_t len;
  84. char *buf;
  85. if (!iterator->remain)
  86. return;
  87. /* Figure out how big the string will be */
  88. len = snprintf(NULL, 0, "%pV", vaf);
  89. /* This is the easiest path, we've already advanced beyond the offset */
  90. if (iterator->offset + len <= iterator->start) {
  91. iterator->offset += len;
  92. return;
  93. }
  94. /* Then check if we can directly copy into the target buffer */
  95. if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
  96. ssize_t pos = iterator->offset - iterator->start;
  97. snprintf(((char *) iterator->data) + pos,
  98. iterator->remain, "%pV", vaf);
  99. iterator->offset += len;
  100. iterator->remain -= len;
  101. return;
  102. }
  103. /*
  104. * Finally, hit the slow path and make a temporary string to copy over
  105. * using _drm_puts_coredump
  106. */
  107. buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  108. if (!buf)
  109. return;
  110. snprintf(buf, len + 1, "%pV", vaf);
  111. __drm_puts_coredump(p, (const char *) buf);
  112. kfree(buf);
  113. }
  114. EXPORT_SYMBOL(__drm_printfn_coredump);
  115. void __drm_puts_seq_file(struct drm_printer *p, const char *str)
  116. {
  117. seq_puts(p->arg, str);
  118. }
  119. EXPORT_SYMBOL(__drm_puts_seq_file);
  120. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
  121. {
  122. seq_printf(p->arg, "%pV", vaf);
  123. }
  124. EXPORT_SYMBOL(__drm_printfn_seq_file);
  125. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
  126. {
  127. dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
  128. }
  129. EXPORT_SYMBOL(__drm_printfn_info);
  130. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
  131. {
  132. pr_debug("%s %pV", p->prefix, vaf);
  133. }
  134. EXPORT_SYMBOL(__drm_printfn_debug);
  135. void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
  136. {
  137. pr_err("*ERROR* %s %pV", p->prefix, vaf);
  138. }
  139. EXPORT_SYMBOL(__drm_printfn_err);
  140. /**
  141. * drm_puts - print a const string to a &drm_printer stream
  142. * @p: the &drm printer
  143. * @str: const string
  144. *
  145. * Allow &drm_printer types that have a constant string
  146. * option to use it.
  147. */
  148. void drm_puts(struct drm_printer *p, const char *str)
  149. {
  150. if (p->puts)
  151. p->puts(p, str);
  152. else
  153. drm_printf(p, "%s", str);
  154. }
  155. EXPORT_SYMBOL(drm_puts);
  156. /**
  157. * drm_printf - print to a &drm_printer stream
  158. * @p: the &drm_printer
  159. * @f: format string
  160. */
  161. void drm_printf(struct drm_printer *p, const char *f, ...)
  162. {
  163. va_list args;
  164. va_start(args, f);
  165. drm_vprintf(p, f, &args);
  166. va_end(args);
  167. }
  168. EXPORT_SYMBOL(drm_printf);
  169. /**
  170. * drm_print_bits - print bits to a &drm_printer stream
  171. *
  172. * Print bits (in flag fields for example) in human readable form.
  173. *
  174. * @p: the &drm_printer
  175. * @value: field value.
  176. * @bits: Array with bit names.
  177. * @nbits: Size of bit names array.
  178. */
  179. void drm_print_bits(struct drm_printer *p, unsigned long value,
  180. const char * const bits[], unsigned int nbits)
  181. {
  182. bool first = true;
  183. unsigned int i;
  184. if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value)))
  185. nbits = BITS_PER_TYPE(value);
  186. for_each_set_bit(i, &value, nbits) {
  187. if (WARN_ON_ONCE(!bits[i]))
  188. continue;
  189. drm_printf(p, "%s%s", first ? "" : ",",
  190. bits[i]);
  191. first = false;
  192. }
  193. if (first)
  194. drm_printf(p, "(none)");
  195. }
  196. EXPORT_SYMBOL(drm_print_bits);
  197. void drm_dev_printk(const struct device *dev, const char *level,
  198. const char *format, ...)
  199. {
  200. struct va_format vaf;
  201. va_list args;
  202. va_start(args, format);
  203. vaf.fmt = format;
  204. vaf.va = &args;
  205. if (dev)
  206. dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
  207. __builtin_return_address(0), &vaf);
  208. else
  209. printk("%s" "[" DRM_NAME ":%ps] %pV",
  210. level, __builtin_return_address(0), &vaf);
  211. va_end(args);
  212. }
  213. EXPORT_SYMBOL(drm_dev_printk);
  214. void drm_dev_dbg(const struct device *dev, enum drm_debug_category category,
  215. const char *format, ...)
  216. {
  217. struct va_format vaf;
  218. va_list args;
  219. if (!drm_debug_enabled(category))
  220. return;
  221. va_start(args, format);
  222. vaf.fmt = format;
  223. vaf.va = &args;
  224. if (dev)
  225. dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
  226. __builtin_return_address(0), &vaf);
  227. else
  228. printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
  229. __builtin_return_address(0), &vaf);
  230. va_end(args);
  231. }
  232. EXPORT_SYMBOL(drm_dev_dbg);
  233. void __drm_dbg(enum drm_debug_category category, const char *format, ...)
  234. {
  235. struct va_format vaf;
  236. va_list args;
  237. if (!drm_debug_enabled(category))
  238. return;
  239. va_start(args, format);
  240. vaf.fmt = format;
  241. vaf.va = &args;
  242. printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
  243. __builtin_return_address(0), &vaf);
  244. va_end(args);
  245. }
  246. EXPORT_SYMBOL(__drm_dbg);
  247. void __drm_err(const char *format, ...)
  248. {
  249. struct va_format vaf;
  250. va_list args;
  251. va_start(args, format);
  252. vaf.fmt = format;
  253. vaf.va = &args;
  254. printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
  255. __builtin_return_address(0), &vaf);
  256. va_end(args);
  257. }
  258. EXPORT_SYMBOL(__drm_err);
  259. /**
  260. * drm_print_regset32 - print the contents of registers to a
  261. * &drm_printer stream.
  262. *
  263. * @p: the &drm printer
  264. * @regset: the list of registers to print.
  265. *
  266. * Often in driver debug, it's useful to be able to either capture the
  267. * contents of registers in the steady state using debugfs or at
  268. * specific points during operation. This lets the driver have a
  269. * single list of registers for both.
  270. */
  271. void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
  272. {
  273. int namelen = 0;
  274. int i;
  275. for (i = 0; i < regset->nregs; i++)
  276. namelen = max(namelen, (int)strlen(regset->regs[i].name));
  277. for (i = 0; i < regset->nregs; i++) {
  278. drm_printf(p, "%*s = 0x%08x\n",
  279. namelen, regset->regs[i].name,
  280. readl(regset->base + regset->regs[i].offset));
  281. }
  282. }
  283. EXPORT_SYMBOL(drm_print_regset32);