lastpc_plt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <linux/module.h>
  2. #include <linux/kallsyms.h>
  3. #include <linux/slab.h>
  4. #include <asm/io.h>
  5. #include <mt-plat/mt_io.h>
  6. #include "../lastpc.h"
  7. struct lastpc_imp {
  8. struct lastpc_plt plt;
  9. void __iomem *toprgu_reg;
  10. };
  11. #define to_lastpc_imp(p) container_of((p), struct lastpc_imp, plt)
  12. /*
  13. #define LASTPC 0X20
  14. #define LASTSP 0X24
  15. #define LASTFP 0X28
  16. #define MUX_CONTOL_C0_REG (base + 0x140)
  17. #define MUX_READ_C0_REG (base + 0x144)
  18. #define MUX_CONTOL_C1_REG (base + 0x21C)
  19. #define MUX_READ_C1_REG (base + 0x25C)
  20. static unsigned int LASTPC_MAGIC_NUM[] = {0x3, 0xB, 0x33, 0x43};
  21. */
  22. static int lastpc_plt_start(struct lastpc_plt *plt)
  23. {
  24. return 0;
  25. }
  26. static int lastpc_plt_dump(struct lastpc_plt *plt, char *buf, int len)
  27. {
  28. void __iomem *mcu_base = plt->common->base + 0x410;
  29. int ret = -1, cnt = num_possible_cpus();
  30. char *ptr = buf;
  31. unsigned long pc_value;
  32. unsigned long fp_value;
  33. unsigned long sp_value;
  34. #ifdef CONFIG_ARM64
  35. unsigned long pc_value_h;
  36. unsigned long fp_value_h;
  37. unsigned long sp_value_h;
  38. #endif
  39. unsigned long size = 0;
  40. unsigned long offset = 0;
  41. char str[KSYM_SYMBOL_LEN];
  42. int i;
  43. int cluster, cpu_in_cluster;
  44. if (cnt < 0)
  45. return ret;
  46. #ifdef CONFIG_ARM64
  47. /* Get PC, FP, SP and save to buf */
  48. for (i = 0; i < cnt; i++) {
  49. cluster = i / 4;
  50. cpu_in_cluster = i % 4;
  51. pc_value_h =
  52. readl(IOMEM((mcu_base + 0x4) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  53. pc_value =
  54. (pc_value_h << 32) |
  55. readl(IOMEM((mcu_base + 0x0) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  56. fp_value_h =
  57. readl(IOMEM((mcu_base + 0x14) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  58. fp_value =
  59. (fp_value_h << 32) |
  60. readl(IOMEM((mcu_base + 0x10) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  61. sp_value_h =
  62. readl(IOMEM((mcu_base + 0x1c) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  63. sp_value =
  64. (sp_value_h << 32) |
  65. readl(IOMEM((mcu_base + 0x18) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  66. kallsyms_lookup(pc_value, &size, &offset, NULL, str);
  67. ptr +=
  68. sprintf(ptr,
  69. "[LAST PC] CORE_%d PC = 0x%lx(%s + 0x%lx), FP = 0x%lx, SP = 0x%lx\n", i,
  70. pc_value, str, offset, fp_value, sp_value);
  71. pr_err("[LAST PC] CORE_%d PC = 0x%lx(%s), FP = 0x%lx, SP = 0x%lx\n", i, pc_value,
  72. str, fp_value, sp_value);
  73. }
  74. #else
  75. /* Get PC, FP, SP and save to buf */
  76. for (i = 0; i < cnt; i++) {
  77. cluster = i / 4;
  78. cpu_in_cluster = i % 4;
  79. pc_value =
  80. readl(IOMEM((mcu_base + 0x0) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  81. fp_value =
  82. readl(IOMEM((mcu_base + 0x8) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  83. sp_value =
  84. readl(IOMEM((mcu_base + 0xc) + (cpu_in_cluster << 5) + (0x100 * cluster)));
  85. kallsyms_lookup((unsigned long)pc_value, &size, &offset, NULL, str);
  86. ptr +=
  87. sprintf(ptr,
  88. "[LAST PC] CORE_%d PC = 0x%lx(%s + 0x%lx), FP = 0x%lx, SP = 0x%lx\n", i,
  89. pc_value, str, offset, fp_value, sp_value);
  90. pr_err("[LAST PC] CORE_%d PC = 0x%lx(%s), FP = 0x%lx, SP = 0x%lx\n", i, pc_value,
  91. str, fp_value, sp_value);
  92. }
  93. #endif
  94. #if 0
  95. /* Get PC, FP, SP and save to buf */
  96. for (i = 0; i < cnt; i++) {
  97. /* this calculation assumes that we have 4 cores in the first cluster, and 2 clusters in the system */
  98. cluster = i / 4;
  99. cpu_in_cluster = i % 4;
  100. if (cluster == 0) {
  101. writel(LASTPC + i, MUX_CONTOL_C0_REG);
  102. pc_value = readl(MUX_READ_C0_REG);
  103. writel(LASTSP + i, MUX_CONTOL_C0_REG);
  104. sp_value = readl(MUX_READ_C0_REG);
  105. writel(LASTFP + i, MUX_CONTOL_C0_REG);
  106. fp_value = readl(MUX_READ_C0_REG);
  107. kallsyms_lookup((unsigned long)pc_value, &size, &offset, NULL, str);
  108. ptr +=
  109. sprintf(ptr, "CORE_%d PC = 0x%x(%s + 0x%lx), FP = 0x%x, SP = 0x%x\n", i,
  110. pc_value, str, offset, fp_value, sp_value);
  111. } else {
  112. writel(LASTPC_MAGIC_NUM[cpu_in_cluster], MUX_CONTOL_C1_REG);
  113. pc_value = readl(MUX_READ_C1_REG);
  114. writel(LASTPC_MAGIC_NUM[cpu_in_cluster] + 1, MUX_CONTOL_C1_REG);
  115. pc_i1_value = readl(MUX_READ_C1_REG);
  116. ptr +=
  117. sprintf(ptr, "CORE_%d PC_i0 = 0x%x, PC_i1 = 0x%x\n", i, pc_value,
  118. pc_i1_value);
  119. }
  120. }
  121. #endif
  122. return 0;
  123. }
  124. static int reboot_test(struct lastpc_plt *plt)
  125. {
  126. return 0;
  127. }
  128. static struct lastpc_plt_operations lastpc_ops = {
  129. .start = lastpc_plt_start,
  130. .dump = lastpc_plt_dump,
  131. .reboot_test = reboot_test,
  132. };
  133. static int __init lastpc_init(void)
  134. {
  135. struct lastpc_imp *drv = NULL;
  136. int ret = 0;
  137. drv = kzalloc(sizeof(struct lastpc_imp), GFP_KERNEL);
  138. if (!drv)
  139. return -ENOMEM;
  140. drv->plt.ops = &lastpc_ops;
  141. drv->plt.chip_code = 0x6735;
  142. drv->plt.min_buf_len = 2048; /* TODO: can calculate the len by how many levels of bt we want */
  143. ret = lastpc_register(&drv->plt);
  144. if (ret) {
  145. pr_err("%s:%d: lastpc_register failed\n", __func__, __LINE__);
  146. goto register_lastpc_err;
  147. }
  148. return 0;
  149. register_lastpc_err:
  150. kfree(drv);
  151. return ret;
  152. }
  153. arch_initcall(lastpc_init);