xrp_debug.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * xrp_debug:
  3. * Permission is hereby granted, free of charge, to any person obtaining
  4. * a copy of this software and associated documentation files (the
  5. * "Software"), to deal in the Software without restriction, including
  6. * without limitation the rights to use, copy, modify, merge, publish,
  7. * distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject to
  9. * the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  18. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  19. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Alternatively you can use and distribute this file under the terms of
  23. * the GNU General Public License version 2 or later.
  24. */
  25. #include <linux/delay.h>
  26. // #include <linux/dma-noncoherent.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/of.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of_device.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/slab.h>
  36. #include <linux/io.h>
  37. #include <asm/cacheflush.h>
  38. #include "xrp_kernel_defs.h"
  39. #include "xrp_hw.h"
  40. #include "xrp_hw_simple_dsp_interface.h"
  41. #define GET_PAGE_NUM(size, offset) ((((size) + ((offset) & ~PAGE_MASK)) + PAGE_SIZE - 1) >> PAGE_SHIFT)
  42. struct xrp_panic_log{
  43. struct xrp_hw_panic __iomem *panic;
  44. phys_addr_t panic_phys;
  45. u32 last_read;
  46. struct proc_dir_entry *log_proc_file;
  47. };
  48. static void memset_hw(void __iomem *dst, int c, size_t sz)
  49. {
  50. int i;
  51. volatile u32 * d_ptr=dst;
  52. for(i=0;i<sz/4;i++)
  53. {
  54. __raw_writel(c, d_ptr++);
  55. }
  56. }
  57. static void dump_regs(const char *fn, void *hw_arg)
  58. {
  59. struct xrp_panic_log *panic_log = hw_arg;
  60. if (!panic_log->panic)
  61. return;
  62. pr_debug("%s: panic = 0x%08x, ccount = 0x%08x\n",
  63. fn,
  64. __raw_readl(&panic_log->panic->panic),
  65. __raw_readl(&panic_log->panic->ccount));
  66. pr_debug("%s: read = 0x%08x, write = 0x%08x, size = 0x%08x\n",
  67. fn,
  68. __raw_readl(&panic_log->panic->rb.read),
  69. __raw_readl(&panic_log->panic->rb.write),
  70. __raw_readl(&panic_log->panic->rb.size));
  71. }
  72. static void dump_log_page(struct xrp_panic_log *hw)
  73. {
  74. char *buf;
  75. size_t i;
  76. if (!hw->panic)
  77. return;
  78. dump_regs(__func__, hw);
  79. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  80. if (buf) {
  81. memcpy_fromio(buf, hw->panic, hw->panic->rb.size);
  82. buf+=sizeof(struct xrp_hw_panic);
  83. for (i = 0; i < hw->panic->rb.size; i += 64)
  84. pr_debug(" %*pEhp\n", 64, buf + i);
  85. kfree(buf);
  86. } else {
  87. pr_debug("(couldn't allocate copy buffer)\n");
  88. }
  89. }
  90. static int log_proc_show(struct seq_file *file, void *v)
  91. {
  92. struct xrp_panic_log *hw = file->private;
  93. char *buf;
  94. size_t i;
  95. int page_num = GET_PAGE_NUM(hw->panic->rb.size,0);
  96. dump_regs(__func__, hw);
  97. buf = kmalloc(PAGE_SIZE*page_num, GFP_KERNEL);
  98. if (buf) {
  99. memcpy_fromio(buf, hw->panic->rb.data, hw->panic->rb.size);
  100. seq_printf(file,"****************** device log >>>>>>>>>>>>>>>>>\n");
  101. for (i = 0; i < hw->panic->rb.size; i += 64)
  102. seq_printf(file," %*pEp", 64,buf+i);
  103. // pr_debug(" %*pEhp\n", 64, buf + i);
  104. // seq_printf(file," %*pEp\n",buf);
  105. kfree(buf);
  106. uint32_t write = __raw_readl(&hw->panic->rb.write);
  107. __raw_writel(write, &hw->panic->rb.read);
  108. return 0;
  109. }
  110. else
  111. {
  112. pr_debug("Fail to alloc buf\n");
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. static int log_proc_open(struct inode *inode, struct file *file)
  118. {
  119. return single_open(file, log_proc_show, NULL);
  120. }
  121. static const struct file_operations log_proc_fops = {
  122. .open = log_proc_open,
  123. .read = seq_read,
  124. .llseek = seq_lseek,
  125. .release = single_release,
  126. };
  127. static bool xrp_panic_init(struct xrp_hw_panic* panic,size_t size)
  128. {
  129. if(size < sizeof(struct xrp_hw_panic))
  130. {
  131. return false;
  132. }
  133. memset_hw(panic,0x0,size);
  134. panic->panic = 0;
  135. panic->ccount = 0;
  136. panic->rb.read = 0;
  137. panic->rb.write = 0;
  138. panic->rb.size = size - sizeof(struct xrp_hw_panic);
  139. sprintf(panic->rb.data,"Inition dsp log\n");
  140. return true;
  141. }
  142. void* xrp_create_panic_log_proc(void* dir,void * panic_addr,size_t size)
  143. {
  144. struct xrp_panic_log *panic_log = kmalloc(sizeof(struct xrp_panic_log),GFP_KERNEL);
  145. if(panic_log == NULL)
  146. return NULL;
  147. panic_log->last_read = 0;
  148. panic_log->panic = panic_addr;
  149. xrp_panic_init(panic_log->panic,size);
  150. panic_log->log_proc_file=proc_create_single_data("dsp_log",0644,dir,&log_proc_show,panic_log);
  151. if(panic_log->log_proc_file == NULL) {
  152. pr_debug("Error: Could not initialize %s\n","dsp_log");
  153. kfree(panic_log);
  154. panic_log =NULL;
  155. } else {
  156. pr_debug("%s create Success!\n","dsp_log");
  157. }
  158. return panic_log;
  159. }
  160. void xrp_remove_panic_log_proc(void *arg)
  161. {
  162. // char file_name[32];
  163. struct xrp_panic_log *panic_log = arg;
  164. // remove_proc_entry(panic_log->log_proc_file,NULL);
  165. proc_remove(panic_log->log_proc_file);
  166. kfree(arg);
  167. pr_debug("dsp proc removed\n");
  168. }
  169. bool panic_check(void *arg)
  170. {
  171. struct xrp_panic_log *panic_log = arg;
  172. uint32_t panic;
  173. uint32_t ccount;
  174. uint32_t read;
  175. uint32_t write;
  176. uint32_t size;
  177. if (!panic_log || !panic_log->panic)
  178. return true;
  179. panic = __raw_readl(&panic_log->panic->panic);
  180. ccount = __raw_readl(&panic_log->panic->ccount);
  181. read = __raw_readl(&panic_log->panic->rb.read);
  182. write = __raw_readl(&panic_log->panic->rb.write);
  183. size = __raw_readl(&panic_log->panic->rb.size);
  184. if (read == 0 && read != panic_log->last_read) {
  185. pr_debug( "****************** device restarted >>>>>>>>>>>>>>>>>\n");
  186. dump_log_page(panic_log);
  187. pr_debug ("<<<<<<<<<<<<<<<<<< device restarted *****************\n");
  188. }
  189. if (write < size && read < size && size < PAGE_SIZE) {
  190. uint32_t tail;
  191. uint32_t total;
  192. char *buf = NULL;
  193. panic_log->last_read = read;
  194. if (read < write) {
  195. tail = write - read;
  196. total = tail;
  197. } else if (read == write) {
  198. tail = 0;
  199. total = 0;
  200. } else {
  201. tail = size - read;
  202. total = write + tail;
  203. }
  204. if (total)
  205. buf = kmalloc(total, GFP_KERNEL);
  206. if (buf) {
  207. uint32_t off = 0;
  208. pr_debug("panic = 0x%08x, ccount = 0x%08x read = %d, write = %d, size = %d, total = %d",
  209. panic, ccount, read, write, size, total);
  210. while (off != total) {
  211. memcpy_fromio(buf + off,
  212. panic_log->panic->rb.data + read,
  213. tail);
  214. read = 0;
  215. off += tail;
  216. tail = total - tail;
  217. }
  218. __raw_writel(write, &panic_log->panic->rb.read);
  219. panic_log->last_read = write;
  220. pr_debug("<<<\n%.*s\n>>>\n",
  221. total, buf);
  222. kfree(buf);
  223. } else if (total) {
  224. pr_debug(
  225. "%s: couldn't allocate memory (%d) to read the dump\n",
  226. __func__, total);
  227. }
  228. } else {
  229. if (read != panic_log->last_read) {
  230. pr_debug(
  231. "nonsense in the log buffer: read = %d, write = %d, size = %d\n",
  232. read, write, size);
  233. panic_log->last_read = read;
  234. }
  235. }
  236. if (panic == 0xdeadbabe) {
  237. pr_debug("%s: panic detected, log dump:\n", __func__);
  238. dump_log_page(panic_log);
  239. }
  240. return panic == 0xdeadbabe;
  241. }