aee_platform_debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2016. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <malloc.h>
  32. #include <dev/aee_platform_debug.h>
  33. #include <spm_common.h>
  34. #include <platform/mt_reg_base.h>
  35. #include <platform/mt_typedefs.h>
  36. #include <platform/mtk_wdt.h>
  37. #include <platform/partition.h>
  38. #include <platform/platform_debug.h>
  39. #include "log_store_lk.h"
  40. #include <plat_debug_interface.h>
  41. #include <reg.h>
  42. #include <libfdt.h>
  43. #include <debug.h>
  44. #include <mtk_mcdi.h>
  45. #ifdef MTK_AB_OTA_UPDATER
  46. #include <mt_boot.h>
  47. #endif
  48. int plt_get_cluster_id(unsigned int cpu_id, unsigned int *core_id_in_cluster)
  49. {
  50. if (core_id_in_cluster == NULL)
  51. return -1;
  52. *core_id_in_cluster = (cpu_id % 4);
  53. return (cpu_id / 4);
  54. }
  55. unsigned long plt_get_cpu_power_status_at_wdt(void)
  56. {
  57. unsigned long bitmask = 0, ret;
  58. ret = readl(SLEEP_BASE + cfg_pc_latch.spm_pwr_sts);
  59. /* CPU0 ~ CPU3 */
  60. bitmask |= (ret & (0xf << 9)) >> 9;
  61. /* CPU4 ~ CPU7 */
  62. bitmask |= (ret & (0xf << 16)) >> 12;
  63. return bitmask;
  64. }
  65. unsigned int plt_get_dfd_dump_type(void)
  66. {
  67. /* for mt6739 with DFD 3.0 -> always dump to DRAM*/
  68. if (cfg_dfd.version >= DFD_V3_0)
  69. return DFD_DUMP_TO_DRAM;
  70. else
  71. return DFD_DUMP_NOT_SUPPORT;
  72. }
  73. static unsigned int save_cpu_bus_data(u64 offset, int *len, CALLBACK dev_write)
  74. {
  75. char *buf = NULL;
  76. int ret;
  77. unsigned int datasize = 0;
  78. /* Save latch buffer */
  79. ret = latch_get((void **)&buf, len);
  80. if (ret && (buf != NULL)) {
  81. if (*len > 0)
  82. datasize = dev_write(buf, *len);
  83. latch_put((void **)&buf);
  84. }
  85. /* Save systracker buffer */
  86. ret = systracker_get((void **)&buf, len, 8);
  87. if (ret && (buf != NULL)) {
  88. if (*len > 0)
  89. datasize += dev_write(buf, *len);
  90. systracker_put((void **)&buf);
  91. }
  92. return datasize;
  93. }
  94. static unsigned int save_dfd_data(u64 offset, int *len, CALLBACK dev_write)
  95. {
  96. char *buf = NULL;
  97. unsigned int datasize = 0;
  98. /* Save dfd buffer */
  99. if (dfd_get((void **)&buf, len)) {
  100. datasize = dev_write(buf, *len);
  101. dfd_put((void **)&buf);
  102. }
  103. return datasize;
  104. }
  105. void platform_lastpc_postinit(void)
  106. {
  107. mt_secure_call(MTK_SIP_LK_LASTPC_AARCH32, 0, 0, 0);
  108. }
  109. /* SPM Debug Features */
  110. static unsigned int spm_wdt_latch_regs[] = {
  111. SLEEP_BASE + 0x800, /* PCM_WDT_LATCH_0 */
  112. SLEEP_BASE + 0x804, /* PCM_WDT_LATCH_1 */
  113. SLEEP_BASE + 0x808, /* PCM_WDT_LATCH_2 */
  114. SLEEP_BASE + 0x80c, /* PCM_WDT_LATCH_3 */
  115. SLEEP_BASE + 0x810, /* PCM_WDT_LATCH_4 */
  116. SLEEP_BASE + 0x814, /* PCM_WDT_LATCH_5 */
  117. SLEEP_BASE + 0x818, /* PCM_WDT_LATCH_6 */
  118. SLEEP_BASE + 0x81c, /* PCM_WDT_LATCH_7 */
  119. SLEEP_BASE + 0x820, /* PCM_WDT_LATCH_8 */
  120. SLEEP_BASE + 0x824, /* PCM_WDT_LATCH_9 */
  121. SLEEP_BASE + 0x838, /* PCM_WDT_LATCH_10 */
  122. SLEEP_BASE + 0x83c, /* PCM_WDT_LATCH_11 */
  123. SLEEP_BASE + 0x828, /* WDT_LATCH_SPARE0 */
  124. SLEEP_BASE + 0x82c, /* WDT_LATCH_SPARE1 */
  125. SLEEP_BASE + 0x830, /* WDT_LATCH_SPARE2 */
  126. SLEEP_BASE + 0x834, /* WDT_LATCH_SPARE3 */
  127. SLEEP_BASE + 0x840, /* DCHA_GATING_LATCH_0 */
  128. SLEEP_BASE + 0x844, /* DCHA_GATING_LATCH_1 */
  129. SLEEP_BASE + 0x848, /* DCHA_GATING_LATCH_2 */
  130. SLEEP_BASE + 0x84C, /* DCHA_GATING_LATCH_3 */
  131. SLEEP_BASE + 0x850, /* DCHA_GATING_LATCH_4 */
  132. SLEEP_BASE + 0x854, /* DCHA_GATING_LATCH_5 */
  133. SLEEP_BASE + 0x858, /* DCHA_GATING_LATCH_6 */
  134. SLEEP_BASE + 0x85C, /* DCHA_GATING_LATCH_7 */
  135. SLEEP_BASE + 0x880, /* DCHA_LATCH_RSV0 */
  136. };
  137. #define DVFSRC_DUMP (DVFSRC_BASE + 0x100)
  138. #define DVFSRC_SIZE 0xA0
  139. #define SPM_DATA_BUF_LENGTH (4096)
  140. static int spm_dump_data(char *buf, int *wp)
  141. {
  142. unsigned int i;
  143. unsigned val;
  144. #ifdef SPM_FW_USE_PARTITION
  145. char part_name[16] = "spmfw";
  146. #ifdef MTK_AB_OTA_UPDATER
  147. get_AB_OTA_name((void *)&part_name, sizeof(part_name));
  148. #endif /* MTK_AB_OTA_UPDATER */
  149. #endif /* SPM_FW_USE_PARTITION */
  150. if (buf == NULL || wp == NULL)
  151. return -1;
  152. /*
  153. * Example output:
  154. * SPM Suspend debug regs(index 1) = 0x8320535
  155. * SPM Suspend debug regs(index 2) = 0xfe114200
  156. * SPM Suspend debug regs(index 3) = 0x3920fffe
  157. * SPM Suspend debug regs(index 4) = 0x3ac06f4f
  158. */
  159. for (i = 0; i < (sizeof(spm_wdt_latch_regs)/sizeof(unsigned int)); i++) {
  160. val = readl(spm_wdt_latch_regs[i]);
  161. *wp += sprintf(buf + *wp,
  162. "SPM debug regs(0x%x) = 0x%x\n",
  163. spm_wdt_latch_regs[i], val);
  164. }
  165. #ifdef SPM_FW_USE_PARTITION
  166. get_spmfw_version(part_name, "spmfw", buf, wp);
  167. #endif /* SPM_FW_USE_PARTITION */
  168. for (i = 0; i < DVFSRC_SIZE; i+=4) {
  169. val = readl(DVFSRC_DUMP+i);
  170. *wp += sprintf(buf + *wp,
  171. "DVFSRC debug regs(0x%x) = 0x%08x\n", DVFSRC_DUMP+i, val);
  172. }
  173. return 1;
  174. }
  175. int spm_data_get(void **data, int *len)
  176. {
  177. int ret;
  178. *len = 0;
  179. *data = malloc(SPM_DATA_BUF_LENGTH);
  180. if (*data == NULL)
  181. return 0;
  182. ret = spm_dump_data(*data, len);
  183. if (ret < 0 || *len > SPM_DATA_BUF_LENGTH) {
  184. *len = (*len > SPM_DATA_BUF_LENGTH) ? SPM_DATA_BUF_LENGTH : *len;
  185. return ret;
  186. }
  187. return 1;
  188. }
  189. void spm_data_put(void **data)
  190. {
  191. free(*data);
  192. }
  193. static unsigned int save_spm_data(u64 offset, int *len, CALLBACK dev_write)
  194. {
  195. char *buf = NULL;
  196. unsigned int datasize = 0;
  197. /* Save SPM buffer */
  198. spm_data_get((void **)&buf, len);
  199. if (buf != NULL) {
  200. if (*len > 0)
  201. datasize = dev_write(buf, *len);
  202. spm_data_put((void **)&buf);
  203. }
  204. return datasize;
  205. }
  206. /* FOR DRAMC data and DRAM Calibration Log
  207. *
  208. * This area is applied for DRAM related debug.
  209. */
  210. /* DRAMC data */
  211. #define DRAM_DEBUG_SRAM_ADDRESS 0x0010E400
  212. #define DRAM_DEBUG_SRAM_LENGTH 1024
  213. static int plat_dram_debug_get(void **data, int *len)
  214. {
  215. *data = (void *)DRAM_DEBUG_SRAM_ADDRESS;
  216. *len = DRAM_DEBUG_SRAM_LENGTH;
  217. return 1;
  218. }
  219. /* shared SRAM for DRAMLOG calibration log */
  220. #define DRAM_KLOG_SRAM_ADDRESS 0x0010EA00
  221. #define DRAM_KLOG_SRAM_LENGTH 4608
  222. #define DRAM_KLOG_VALID_ADDRESS (DRAM_KLOG_SRAM_ADDRESS +0xC)
  223. static int plat_dram_klog_get(void **data, int *len)
  224. {
  225. *data = (void *)DRAM_KLOG_SRAM_ADDRESS;
  226. *len = DRAM_KLOG_SRAM_LENGTH;
  227. return 1;
  228. }
  229. static bool plat_dram_has_klog(void)
  230. {
  231. if (*(volatile unsigned int*)DRAM_KLOG_VALID_ADDRESS)
  232. return true;
  233. return false;
  234. }
  235. static unsigned int save_dram_data(u64 offset, int *len, CALLBACK dev_write)
  236. {
  237. char *buf = NULL;
  238. unsigned int datasize = 0, allsize = 0;
  239. if (plat_dram_debug_get((void **)&buf, len)) {
  240. datasize = dev_write(buf, *len);
  241. allsize = datasize;
  242. }
  243. if (plat_dram_klog_get((void **)&buf, len)) {
  244. buf = malloc(*len);
  245. if (buf) {
  246. mrdump_read_log(buf, *len, MRDUMP_EXPDB_DRAM_KLOG_OFFSET);
  247. datasize = dev_write(buf, *len);
  248. allsize += datasize;
  249. free(buf);
  250. }
  251. }
  252. return allsize;
  253. }
  254. static int plat_write_dram_klog(void)
  255. {
  256. char *sram_base = NULL;
  257. int len = 0;
  258. if (plat_dram_klog_get((void **)&sram_base, (int *)&len)) {
  259. if (plat_dram_has_klog()) {
  260. mrdump_write_log(MRDUMP_EXPDB_DRAM_KLOG_OFFSET, sram_base, len);
  261. }
  262. }
  263. return 0;
  264. }
  265. /* SRAM for Hybrid CPU DVFS */
  266. #define HVFS_SRAM_ADDRESS 0x0011bc00
  267. #define HVFS_SRAM_LENGTH 0x1400 /* 5K bytes */
  268. static int plat_hvfs_data_get(void **data, int *len)
  269. {
  270. *data = (void *)HVFS_SRAM_ADDRESS;
  271. *len = HVFS_SRAM_LENGTH;
  272. return 1;
  273. }
  274. static unsigned int save_hvfs_data(u64 offset, int *len, CALLBACK dev_write)
  275. {
  276. char *buf = NULL;
  277. unsigned int datasize = 0;
  278. if (plat_hvfs_data_get((void **)&buf, len)) {
  279. datasize = dev_write(buf, *len);
  280. }
  281. return datasize;
  282. }
  283. static int plat_mcdi_data_get(void **data, int *len)
  284. {
  285. mcdi_setup_file_info_for_kedump();
  286. *data = (void *)MCDI_SRAM_ADDRESS;
  287. *len = MCDI_SRAM_LENGTH;
  288. return 1;
  289. }
  290. static unsigned int save_mcdi_data(u64 offset, int *len, CALLBACK dev_write)
  291. {
  292. char *buf = NULL;
  293. unsigned int datasize = 0;
  294. if (plat_mcdi_data_get((void **)&buf, len)) {
  295. datasize = dev_write(buf, *len);
  296. }
  297. return datasize;
  298. }
  299. /* platform initial function */
  300. int platform_debug_init(void)
  301. {
  302. /* function pointer assignment */
  303. plat_spm_data_get = save_spm_data;
  304. plat_dram_get = save_dram_data;
  305. plat_hvfs_get = save_hvfs_data;
  306. plat_cpu_bus_get = save_cpu_bus_data;
  307. plat_mcdi_get = save_mcdi_data;
  308. /* check dfd_valid_before_reboot and efuse for DFD 3.0 */
  309. if ((readl(cfg_dfd.plat_sram_flag1) & 0x2) && (get_efuse_dfd_disabled() == 0x0)) {
  310. plat_dfd20_get = save_dfd_data;
  311. }
  312. /* routine tasks */
  313. plat_write_dram_klog();
  314. return 1;
  315. }
  316. extern int get_ccci_md_view_smem_addr(unsigned long long *ap_addr, unsigned int *md_addr);
  317. int dfd_set_base_addr(void *fdt)
  318. {
  319. int ret = 0;
  320. int offset;
  321. u64 addr;
  322. unsigned int md_addr;
  323. unsigned long long ap_addr;
  324. if (!fdt)
  325. return -1;
  326. ret = get_ccci_md_view_smem_addr(&ap_addr, &md_addr);
  327. if (ret < 0)
  328. return ret;
  329. offset = fdt_path_offset(fdt, "/chosen");
  330. if (offset < 0)
  331. return offset;
  332. /* pass base address to kernel */
  333. addr = cpu_to_fdt64(md_addr);
  334. ret = fdt_setprop(fdt, offset, "dfd,base_addr", &addr, sizeof(addr));
  335. if (ret < 0)
  336. return ret;
  337. /*
  338. * write base address[31:1] from AP view to plat_sram_flag2[31:1]
  339. * write base address[32:32] from AP view to plat_sram_flag2[0:0]
  340. */
  341. writel((ap_addr & ~(0x1)) | ((ap_addr >> 32) & 0x1),
  342. cfg_dfd.plat_sram_flag2);
  343. return ret;
  344. }