aee_platform_debug.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 <platform/mt_reg_base.h>
  34. #include <platform/mt_typedefs.h>
  35. #include <platform/mtk_wdt.h>
  36. #include <platform/platform_debug.h>
  37. #include "log_store_lk.h"
  38. #include <plat_debug_interface.h>
  39. #include <spm_common.h>
  40. #include <reg.h>
  41. #include <libfdt.h>
  42. #include <debug.h>
  43. #include <err.h>
  44. #ifdef MTK_TINYSYS_SSPM_SUPPORT
  45. #include <platform/mt_sspm.h>
  46. #include <arch/arm/mmu.h>
  47. #include <platform/timer.h>
  48. #endif
  49. #ifdef MTK_TINYSYS_SCP_SUPPORT
  50. #include <lib/zlib.h>
  51. #include <mt_scp.h>
  52. #include <mt_scp_excep.h>
  53. #endif
  54. #include <mtk_mcdi.h>
  55. #ifdef MTK_SMC_ID_MGMT
  56. #include "mtk_secure_api.h"
  57. #endif
  58. #ifdef MTK_AB_OTA_UPDATER
  59. #include <mt_boot.h>
  60. #endif
  61. #include <plat_sram_flag.h>
  62. int plt_get_cluster_id(unsigned int cpu_id, unsigned int *core_id_in_cluster)
  63. {
  64. if (core_id_in_cluster == NULL)
  65. return -1;
  66. *core_id_in_cluster = (cpu_id % 4);
  67. return (cpu_id / 4);
  68. }
  69. unsigned long plt_get_cpu_power_status_at_wdt(void)
  70. {
  71. unsigned long bitmask = 0xff, ret;
  72. ret = readl(MCUCFG_BASE + cfg_pc_latch.spm_pwr_sts);
  73. bitmask = ret & 0xff;
  74. return bitmask;
  75. }
  76. void reset_snoop_filter_ctrl(void)
  77. {
  78. #ifdef MTK_SMC_ID_MGMT
  79. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_DFD_RESET_SNOOP_FILTER_MAGIC, 0, 0, 0);
  80. #else
  81. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_DFD_RESET_SNOOP_FILTER_MAGIC, 0, 0);
  82. #endif
  83. }
  84. static void setup_snoop_filter_ram_ctrl(void)
  85. {
  86. #ifdef MTK_SMC_ID_MGMT
  87. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_DFD_SETUP_SNOOP_FILTER_MAGIC, 0, 0, 0);
  88. #else
  89. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_DFD_SETUP_SNOOP_FILTER_MAGIC, 0, 0);
  90. #endif
  91. }
  92. static void return_snoop_filter_ram_ctrl(void)
  93. {
  94. #ifdef MTK_SMC_ID_MGMT
  95. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_DFD_RETURN_SNOOP_FILTER_MAGIC, 0, 0, 0);
  96. #else
  97. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_DFD_RETURN_SNOOP_FILTER_MAGIC, 0, 0);
  98. #endif
  99. }
  100. static void circular_buffer_lock(void)
  101. {
  102. #ifdef MTK_SMC_ID_MGMT
  103. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_CIRCULAR_BUFFER_LOCK, 0, 0, 0);
  104. #else
  105. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_CIRCULAR_BUFFER_LOCK, 0, 0);
  106. #endif
  107. }
  108. static void circular_buffer_unlock(void)
  109. {
  110. #ifdef MTK_SMC_ID_MGMT
  111. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_CIRCULAR_BUFFER_UNLOCK, 0, 0, 0);
  112. #else
  113. mt_secure_call(MTK_SIP_LK_PLAT_DEBUG, PLAT_MTK_CIRCULAR_BUFFER_UNLOCK, 0, 0);
  114. #endif
  115. }
  116. static bool dfd_valid = false;
  117. static bool check_dfd_valid(u32* data)
  118. {
  119. if (data != NULL) {
  120. /*
  121. * 0x0 = 16'hAA55
  122. * 0x4 = 16'h0F0F
  123. * 0x8 = 32’h1683-0000
  124. */
  125. if ((((data[0] & 0xffff0000) >> 16) == 0xAA55)
  126. && (((data[1] & 0xffff0000) >> 16) == 0x0F0F)
  127. && data[2] == 0x16830000)
  128. dfd_valid = true;
  129. }
  130. return dfd_valid;
  131. }
  132. unsigned int plt_get_dfd_dump_type(void)
  133. {
  134. return DFD_DUMP_TO_DRAM;
  135. }
  136. static unsigned int save_cpu_bus_data(u64 offset, int *len, CALLBACK dev_write)
  137. {
  138. char *buf = NULL;
  139. int ret;
  140. unsigned int datasize = 0;
  141. /* Save latch buffer */
  142. ret = latch_get((void **)&buf, len);
  143. if (ret && (buf != NULL)) {
  144. if (*len > 0)
  145. datasize = dev_write(buf, *len);
  146. latch_put((void **)&buf);
  147. }
  148. /* Save systracker buffer */
  149. ret = systracker_get((void **)&buf, len, 32);
  150. if (ret && (buf != NULL)) {
  151. if (*len > 0)
  152. datasize += dev_write(buf, *len);
  153. systracker_put((void **)&buf);
  154. }
  155. return datasize;
  156. }
  157. static unsigned int save_dfd_data(u64 offset, int *len, CALLBACK dev_write)
  158. {
  159. char *buf = NULL;
  160. int ret;
  161. unsigned int datasize = 0;
  162. /* Save dfd buffer */
  163. ret = dfd_get((void **)&buf, len);
  164. if (buf != NULL) {
  165. if (*len > 0)
  166. datasize = dev_write(buf, *len);
  167. dfd_put((void **)&buf);
  168. }
  169. return datasize;
  170. }
  171. #ifdef MTK_PICACHU_SUPPORT
  172. extern BOOT_ARGUMENT *g_boot_arg; //from platform.c
  173. static unsigned int save_picachu_log(u64 offset, int *len, CALLBACK dev_write)
  174. {
  175. void *buf = NULL;
  176. unsigned int datasize = 0;
  177. unsigned int start = 0;
  178. unsigned int size = 0;
  179. int ret;
  180. mblock_info_t *mblock_info = NULL;
  181. reserved_t * p_reserved = NULL;
  182. mblock_info = &g_boot_arg->mblock_info;
  183. if (g_boot_arg == NULL || mblock_info == NULL) {
  184. return 0;
  185. }
  186. p_reserved = mblock_query_reserved(mblock_info,"PICACHU",0);
  187. if (p_reserved != NULL) {
  188. start = (unsigned int)p_reserved->start;
  189. size = (unsigned int)p_reserved->size;
  190. if (size < SECTION_SIZE) {
  191. ret = arch_mmu_map(start, start,
  192. MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA,
  193. SECTION_SIZE);
  194. } else {
  195. ret = arch_mmu_map(start, start,
  196. MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA,
  197. SECTION_SIZE * ((size/SECTION_SIZE) + 1));
  198. }
  199. if (ret == NO_ERROR)
  200. buf = (void *)start;
  201. }
  202. if (buf) {
  203. /* force dump 512 KB */
  204. *len = 0x80000;
  205. datasize = dev_write(buf, *len);
  206. }
  207. return datasize;
  208. }
  209. #endif
  210. void platform_clear_all_on_mux(void)
  211. {
  212. /* clear rg_mcu_pwr_iso_dis */
  213. writel(readl(MCU_ALL_PWR_ON_CTRL) & ~(1 << 2), MCU_ALL_PWR_ON_CTRL);
  214. dsb();
  215. /* clear rg_mcu_pwr_on */
  216. writel(readl(MCU_ALL_PWR_ON_CTRL) & ~(1 << 1), MCU_ALL_PWR_ON_CTRL);
  217. dsb();
  218. }
  219. /* SPM Debug Features */
  220. static unsigned int spm_wdt_latch_regs[] = {
  221. SLEEP_BASE + 0x800, /* PCM_WDT_LATCH_0 */
  222. SLEEP_BASE + 0x804, /* PCM_WDT_LATCH_1 */
  223. SLEEP_BASE + 0x808, /* PCM_WDT_LATCH_2 */
  224. SLEEP_BASE + 0x80C, /* PCM_WDT_LATCH_3 */
  225. SLEEP_BASE + 0x810, /* PCM_WDT_LATCH_4 */
  226. SLEEP_BASE + 0x814, /* PCM_WDT_LATCH_5 */
  227. SLEEP_BASE + 0x818, /* PCM_WDT_LATCH_6 */
  228. SLEEP_BASE + 0x81C, /* PCM_WDT_LATCH_7 */
  229. SLEEP_BASE + 0x820, /* PCM_WDT_LATCH_8 */
  230. SLEEP_BASE + 0x824, /* PCM_WDT_LATCH_9 */
  231. SLEEP_BASE + 0x828, /* PCM_WDT_LATCH_10 */
  232. SLEEP_BASE + 0x82C, /* PCM_WDT_LATCH_11 */
  233. SLEEP_BASE + 0x830, /* PCM_WDT_LATCH_12 */
  234. SLEEP_BASE + 0x834, /* PCM_WDT_LATCH_13 */
  235. SLEEP_BASE + 0x838, /* PCM_WDT_LATCH_14 */
  236. SLEEP_BASE + 0x83C, /* PCM_WDT_LATCH_15 */
  237. SLEEP_BASE + 0x840, /* PCM_WDT_LATCH_16 */
  238. SLEEP_BASE + 0x844, /* PCM_WDT_LATCH_17 */
  239. SLEEP_BASE + 0x848, /* PCM_WDT_LATCH_18 */
  240. SLEEP_BASE + 0x84C, /* DVFSRC_IRQ_LATCH_0 */
  241. SLEEP_BASE + 0x850, /* DVFSRC_IRQ_LATCH_1 */
  242. SLEEP_BASE + 0x854, /* DRAMC_GATING_ERR_LATCH_CH0_0 */
  243. SLEEP_BASE + 0x858, /* DRAMC_GATING_ERR_LATCH_CH0_1 */
  244. SLEEP_BASE + 0x85C, /* DRAMC_GATING_ERR_LATCH_CH0_2 */
  245. SLEEP_BASE + 0x860, /* DRAMC_GATING_ERR_LATCH_CH0_3 */
  246. SLEEP_BASE + 0x864, /* DRAMC_GATING_ERR_LATCH_CH0_4 */
  247. SLEEP_BASE + 0x868, /* DRAMC_GATING_ERR_LATCH_CH0_5 */
  248. SLEEP_BASE + 0x86C, /* DRAMC_GATING_ERR_LATCH_CH0_6 */
  249. SLEEP_BASE + 0x870, /* DRAMC_GATING_ERR_LATCH_CH0_7 */
  250. SLEEP_BASE + 0x874, /* DRAMC_GATING_ERR_LATCH_CH0_8 */
  251. SLEEP_BASE + 0x878, /* DRAMC_GATING_ERR_LATCH_CH1_0 */
  252. SLEEP_BASE + 0x87C, /* DRAMC_GATING_ERR_LATCH_CH1_1 */
  253. SLEEP_BASE + 0x880, /* DRAMC_GATING_ERR_LATCH_CH1_2 */
  254. SLEEP_BASE + 0x884, /* DRAMC_GATING_ERR_LATCH_CH1_3 */
  255. SLEEP_BASE + 0x888, /* DRAMC_GATING_ERR_LATCH_CH1_4 */
  256. SLEEP_BASE + 0x88C, /* DRAMC_GATING_ERR_LATCH_CH1_5 */
  257. SLEEP_BASE + 0x890, /* DRAMC_GATING_ERR_LATCH_CH1_6 */
  258. SLEEP_BASE + 0x894, /* DRAMC_GATING_ERR_LATCH_CH1_7 */
  259. SLEEP_BASE + 0x898, /* DRAMC_GATING_ERR_LATCH_CH1_8 */
  260. };
  261. #define DVFSRC_DUMP (DVFSRC_BASE + 0xBF0)
  262. #define DVFSRC_SIZE 0x100
  263. #define DVFSRC_LAST_L (DVFSRC_BASE + 0xAE8)
  264. #define DVFSRC_SRAM_DUMP (0x0011BBD0)
  265. #define DVFSRC_SRAM_SIZE 0x10
  266. /* need to check aee_db_file_info[] @ app/mt_boot/aee/KEDump.c */
  267. #define SPM_DATA_BUF_LENGTH (4096)
  268. static int spm_dump_data(char *buf, int *wp)
  269. {
  270. unsigned int i;
  271. unsigned val;
  272. #ifdef SPM_FW_USE_PARTITION
  273. char part_name[16] = "spmfw";
  274. #ifdef MTK_AB_OTA_UPDATER
  275. get_AB_OTA_name((void *)&part_name, sizeof(part_name));
  276. #endif /* MTK_AB_OTA_UPDATER */
  277. #endif /* SPM_FW_USE_PARTITION */
  278. if (buf == NULL || wp == NULL)
  279. return -1;
  280. for (i = 0; i < (sizeof(spm_wdt_latch_regs)/sizeof(unsigned int)); i++) {
  281. val = readl(spm_wdt_latch_regs[i]);
  282. *wp += sprintf(buf + *wp,
  283. "SPM regs(0x%x) = 0x%x\n",
  284. spm_wdt_latch_regs[i], val);
  285. }
  286. #ifdef SPM_FW_USE_PARTITION
  287. get_spmfw_version(part_name, "spmfw", buf, wp);
  288. #endif /* SPM_FW_USE_PARTITION */
  289. val = readl(DVFSRC_LAST_L);
  290. *wp += sprintf(buf + *wp,
  291. "DVFSRC(0x%x) = 0x%08x\n", DVFSRC_LAST_L, val);
  292. for (i = 0; i < DVFSRC_SIZE; i += 4) {
  293. val = readl(DVFSRC_DUMP + i);
  294. *wp += sprintf(buf + *wp,
  295. "DVFSRC(0x%x) = 0x%08x\n", DVFSRC_DUMP + i, val);
  296. }
  297. for (i = 0; i < DVFSRC_SRAM_SIZE; i += 4) {
  298. val = readl(DVFSRC_SRAM_DUMP + i);
  299. *wp += sprintf(buf + *wp,
  300. "DVFSRC(0x%x) = 0x%08x\n", DVFSRC_SRAM_DUMP + i, val);
  301. }
  302. if (*wp > SPM_DATA_BUF_LENGTH) {
  303. dprintf(CRITICAL, "[spm] out of range: 0x%x > SPM_DATA_BUF_LENGTH(0x%x)\n", *wp, SPM_DATA_BUF_LENGTH);
  304. assert(0);
  305. }
  306. return 1;
  307. }
  308. int spm_data_get(void **data, int *len)
  309. {
  310. int ret;
  311. *len = 0;
  312. *data = malloc(SPM_DATA_BUF_LENGTH);
  313. if (*data == NULL)
  314. return 0;
  315. ret = spm_dump_data(*data, len);
  316. if (ret < 0 || *len > SPM_DATA_BUF_LENGTH) {
  317. *len = (*len > SPM_DATA_BUF_LENGTH) ? SPM_DATA_BUF_LENGTH : *len;
  318. return ret;
  319. }
  320. return 1;
  321. }
  322. void spm_data_put(void **data)
  323. {
  324. free(*data);
  325. }
  326. static unsigned int save_spm_data(u64 offset, int *len, CALLBACK dev_write)
  327. {
  328. char *buf = NULL;
  329. unsigned int datasize = 0;
  330. /* Save SPM buffer */
  331. spm_data_get((void **)&buf, len);
  332. if (buf != NULL) {
  333. if (*len > 0)
  334. datasize = dev_write(buf, *len);
  335. spm_data_put((void **)&buf);
  336. }
  337. return datasize;
  338. }
  339. /* FOR DRAMC data and DRAM Calibration Log
  340. *
  341. * This area is applied for DRAM related debug.
  342. */
  343. /* DRAMC data */
  344. #define DRAM_DEBUG_SRAM_ADDRESS 0x11D800
  345. #define DRAM_DEBUG_SRAM_LENGTH 512
  346. static int plat_dram_debug_get(void **data, int *len)
  347. {
  348. *data = (void *)DRAM_DEBUG_SRAM_ADDRESS;
  349. *len = DRAM_DEBUG_SRAM_LENGTH;
  350. return 1;
  351. }
  352. /* shared SRAM for DRAMLOG calibration log */
  353. #define DRAM_KLOG_SRAM_ADDRESS 0x0011E000
  354. #define DRAM_KLOG_SRAM_LENGTH 8192
  355. #define DRAM_KLOG_VALID_ADDRESS 0x0011E00C
  356. static int plat_dram_klog_get(void **data, int *len)
  357. {
  358. *data = (void *)DRAM_KLOG_SRAM_ADDRESS;
  359. *len = DRAM_KLOG_SRAM_LENGTH;
  360. return 1;
  361. }
  362. static bool plat_dram_has_klog(void)
  363. {
  364. if (*(volatile unsigned int*)DRAM_KLOG_VALID_ADDRESS)
  365. return true;
  366. return false;
  367. }
  368. static unsigned int save_dram_data(u64 offset, int *len, CALLBACK dev_write)
  369. {
  370. char *buf = NULL;
  371. unsigned int datasize = 0, allsize = 0;
  372. if (plat_dram_debug_get((void **)&buf, len)) {
  373. datasize = dev_write(buf, *len);
  374. allsize = datasize;
  375. }
  376. if (plat_dram_klog_get((void **)&buf, len)) {
  377. buf = malloc(*len);
  378. if (buf) {
  379. mrdump_read_log(buf, *len, MRDUMP_EXPDB_DRAM_KLOG_OFFSET);
  380. datasize = dev_write(buf, *len);
  381. allsize += datasize;
  382. free(buf);
  383. }
  384. }
  385. return allsize;
  386. }
  387. #ifdef MTK_TINYSYS_SSPM_SUPPORT
  388. static unsigned int save_sspm_coredump(u64 offset, int *len, CALLBACK dev_write)
  389. {
  390. unsigned int *buf = NULL;
  391. unsigned int datasize = 0;
  392. int retry = SSPM_COREDUP_RETRY;
  393. if (!(*(unsigned int *)SSPM_BACKUP)) {
  394. return 0;
  395. }
  396. do {
  397. buf = *(unsigned int **)(SSPM_DM_ADDR);
  398. *len = *(int *)SSPM_DM_SZ;
  399. if (*len > 0) {
  400. datasize = dev_write(buf, *len);
  401. break;
  402. } else {
  403. udelay(100);
  404. }
  405. } while ( --retry);
  406. buf = *(unsigned int **)(SSPM_RM_ADDR);
  407. *len = *(int *)SSPM_RM_SZ;
  408. if (*len > 0) {
  409. datasize += dev_write(buf, *len);
  410. }
  411. return datasize;
  412. }
  413. static unsigned int save_sspm_data(u64 offset, int *len, CALLBACK dev_write)
  414. {
  415. char *buf = NULL;
  416. char owner[3] = {'P', 'S', 'L'}; /* Platform, SSPM, SSPM_LAST_LOG */
  417. unsigned int addr = 0, dispatch = 0;
  418. unsigned int ahb_status, ahb_addr_m0, ahb_addr_m1, ahb_addr_m2;
  419. unsigned int datasize = 0, tbufl, tbufh, r, i, j;
  420. int length = 0;
  421. buf = malloc(SSPM_DATA_BUF_SZ);
  422. if (!buf) {
  423. return 0;
  424. }
  425. ahb_status = DRV_Reg32(SSPM_AHB_STATUS);
  426. ahb_addr_m0 = DRV_Reg32(SSPM_AHB_M0_ADDR);
  427. ahb_addr_m1 = DRV_Reg32(SSPM_AHB_M1_ADDR);
  428. ahb_addr_m2 = DRV_Reg32(SSPM_AHB_M2_ADDR);
  429. /* Set Auto-dispatch rule according by AHB_STATUS[13,10,5,4]. */
  430. if ( (ahb_status & (BIT(13) | BIT(10) | BIT(5) | BIT(4)) ) == 0x0 )
  431. {
  432. /* Bus hang issue. */
  433. if (ahb_status & BIT(2)) /* Master = M0 */
  434. addr = ahb_addr_m0;
  435. else if (ahb_status & BIT(3)) /* Master = M1 */
  436. addr = ahb_addr_m1;
  437. else if ((ahb_status & BIT(17)) == 0) /* Master M2 trans is pending. */
  438. addr = ahb_addr_m2;
  439. if (((addr >= SYSRAM_START) && (addr < SYSRAM_END)) ||
  440. ((addr >= DRAM_START) && (addr < DRAM_END)))
  441. dispatch = TO_PLATFORM;
  442. else
  443. dispatch = TO_SSPM_OWN;
  444. }
  445. else
  446. dispatch = TO_SSPM_LAST_LOG;
  447. memset(buf, 0, SSPM_DATA_BUF_SZ);
  448. length += snprintf(buf + length, SSPM_DATA_BUF_SZ - length,
  449. "STATUS: 0x%x\n"
  450. "M0: 0x%x\n"
  451. "M1: 0x%x\n"
  452. "M2: 0x%x\n"
  453. "SP: 0x%x\n"
  454. "LR: 0x%x\n"
  455. "PC: 0x%x\n"
  456. "Dispatch: %c\n",
  457. ahb_status, ahb_addr_m0, ahb_addr_m1, ahb_addr_m2,
  458. DRV_Reg32(SSPM_SP), DRV_Reg32(SSPM_LR),
  459. DRV_Reg32(SSPM_PC),
  460. owner[dispatch]);
  461. r = DRV_Reg32(SSPM_TBUF_WPTR);
  462. length += snprintf(buf + length, (SSPM_DATA_BUF_SZ-1) - length, "\nTBUF_WPTR=%u\n", r);
  463. for (i = 0, j = r; i < 16; ++i, j = (j-1) & 0xF) {
  464. DRV_WriteReg32(SSPM_DBG_SEL, j);
  465. tbufl = DRV_Reg32(SSPM_TBUFL);
  466. tbufh = DRV_Reg32(SSPM_TBUFH);
  467. length += snprintf(buf + length, (SSPM_DATA_BUF_SZ-1) - length,
  468. "%u: TBUF[%u] _H=0x%x _L=0x%x\n", i, j, tbufh, tbufl);
  469. }
  470. *len = length;
  471. if (*len > 0) {
  472. datasize = dev_write(buf, (*len > SSPM_DATA_BUF_SZ ? SSPM_DATA_BUF_SZ : *len));
  473. }
  474. free(buf);
  475. return datasize;
  476. }
  477. static unsigned int save_sspm_xfile(u64 offset, int *len, CALLBACK dev_write)
  478. {
  479. unsigned int *buf = NULL;
  480. unsigned int *sspm_info = NULL;
  481. unsigned int datasize = 0;
  482. int ret;
  483. /* Get the information stored in *SSPM_INFO by preloader
  484. struct sspm_info_t {
  485. unsigned int sspm_dm_ofs;
  486. unsigned int sspm_dm_sz;
  487. unsigned int rd_ofs;
  488. unsigned int rd_sz;
  489. unsigned int xfile_addr;
  490. unsigned int xfile_sz;
  491. };
  492. */
  493. #ifdef MTK_3LEVEL_PAGETABLE
  494. ret = arch_mmu_map(ROUNDDOWN(*(uint64_t *)(SSPM_INFO), SECTION_SIZE),
  495. ROUNDDOWN(*(uint32_t *)(SSPM_INFO), SECTION_SIZE),
  496. MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA,
  497. SECTION_SIZE);
  498. if (ret) {
  499. dprintf(CRITICAL, "kedump: mmu map to 0x%llx fail(%d), SSPM dump might fail.\n",
  500. (unsigned long long)ROUNDDOWN(*(uint64_t *)(SSPM_INFO), SECTION_SIZE), ret);
  501. return 0;
  502. }
  503. #endif
  504. sspm_info = *(unsigned int **)(SSPM_INFO);
  505. buf = *(unsigned int **)(sspm_info + 4);
  506. *len = *(int *)(sspm_info + 5);
  507. dprintf(CRITICAL, "sspm buf 0x%x, len:0x%x\n", *buf, *len);
  508. if (*len > 0) {
  509. datasize = dev_write(buf, *len);
  510. }
  511. return datasize;
  512. }
  513. static unsigned int save_sspm_last_log(u64 offset, int *len, CALLBACK dev_write)
  514. {
  515. unsigned int *buf = NULL;
  516. unsigned int datasize = 0;
  517. buf = *(unsigned int **)(SSPM_LASTK_ADDR);
  518. *len = *(int *)SSPM_LASTK_SZ;
  519. if (*len > 0) {
  520. datasize = dev_write( buf, *len);
  521. }
  522. return datasize;
  523. }
  524. #endif /* #ifdef MTK_TINYSYS_SSPM_SUPPORT */
  525. #ifdef MTK_TINYSYS_SCP_SUPPORT
  526. #define SCP_EE_SIZE 0xD2000 //640 KB
  527. static unsigned int save_scp_coredump(u64 offset, int *len, CALLBACK dev_write)
  528. {
  529. int memory_dump_size;
  530. unsigned char *output = malloc(SCP_EE_SIZE);
  531. if (!output) {
  532. return 0;
  533. }
  534. memory_dump_size = scp_crash_dump(output);
  535. if ((memory_dump_size > SCP_EE_SIZE) || (memory_dump_size <= Z_NEED_DICT)) {
  536. dprintf(CRITICAL, "SCP memory_dump_size ERR %d\n", memory_dump_size);
  537. memory_dump_size = 0;
  538. } else
  539. memory_dump_size = dev_write(output, memory_dump_size);
  540. free(output);
  541. return memory_dump_size;
  542. }
  543. #endif
  544. static int plat_write_dram_klog(void)
  545. {
  546. char *sram_base = NULL;
  547. int len = 0;
  548. if (plat_dram_klog_get((void **)&sram_base, (int *)&len)) {
  549. if (plat_dram_has_klog()) {
  550. mrdump_write_log(MRDUMP_EXPDB_DRAM_KLOG_OFFSET, sram_base, len);
  551. }
  552. }
  553. return 0;
  554. }
  555. /* SRAM for Hybrid CPU DVFS */
  556. #define HVFS_SRAM_ADDRESS 0x0011bc00
  557. #define HVFS_SRAM_LENGTH 0x1400 /* 5K bytes */
  558. static int plat_hvfs_data_get(void **data, int *len)
  559. {
  560. *data = (void *)HVFS_SRAM_ADDRESS;
  561. *len = HVFS_SRAM_LENGTH;
  562. return 1;
  563. }
  564. static unsigned int save_hvfs_data(u64 offset, int *len, CALLBACK dev_write)
  565. {
  566. char *buf = NULL;
  567. unsigned int datasize = 0;
  568. if (plat_hvfs_data_get((void **)&buf, len)) {
  569. datasize = dev_write(buf, *len);
  570. }
  571. return datasize;
  572. }
  573. static int plat_log_dur_lkdump_get(void **data, int *len)
  574. {
  575. *data = (void *)current_buf_addr_get();
  576. *len = current_buf_pl_lk_log_size_get();
  577. if((*data == 0) || (*len == 0)) {
  578. dprintf(CRITICAL, "[LK_LOG_STORE] invalid current address or log length(addr 0x%x, len 0x%x)\n", (unsigned int)*data, (unsigned int)*len);
  579. return 0;
  580. }
  581. dprintf(CRITICAL, "[LK_LOG_STORE] the current buf addr is 0x%x, log len is 0x%x\n", (unsigned int)*data, (unsigned int)*len);
  582. return 1;
  583. }
  584. static unsigned int save_log_dur_lkdump(u64 offset, int *len, CALLBACK dev_write)
  585. {
  586. char *buf = NULL;
  587. unsigned int datasize = 0;
  588. if (plat_log_dur_lkdump_get((void **)&buf, len)) {
  589. datasize = dev_write(buf, *len);
  590. }
  591. return datasize;
  592. }
  593. static int plat_mcdi_data_get(void **data, int *len)
  594. {
  595. mcdi_setup_file_info_for_kedump();
  596. *data = (void *)MCDI_SRAM_ADDRESS;
  597. *len = MCDI_SRAM_LENGTH;
  598. return 1;
  599. }
  600. static unsigned int save_mcdi_data(u64 offset, int *len, CALLBACK dev_write)
  601. {
  602. char *buf = NULL;
  603. unsigned int datasize = 0;
  604. if (plat_mcdi_data_get((void **)&buf, len)) {
  605. datasize = dev_write(buf, *len);
  606. }
  607. return datasize;
  608. }
  609. unsigned int plat_is_perisys_timeout(const struct plt_cfg_bus_latch *self)
  610. {
  611. return (readl(PERICFG_BASE + self->perisys_offsets.bus_peri_r1) & 1);
  612. }
  613. unsigned int plt_infrasys_is_timeout(const struct plt_cfg_bus_latch *self)
  614. {
  615. return (readl(INFRACFG_AO_BASE + self->infrasys_offsets.bus_infra_ctrl) & 1);
  616. }
  617. void plat_perisys_monitor_init(const struct plt_cfg_bus_latch *self)
  618. {
  619. /* set PERIBUS_TIMEOUT_THRE and PERIBUS_TIMEOUT_THRE_TYPE */
  620. writel(self->perisys_timeout, PERICFG_BASE + self->perisys_offsets.bus_peri_r0);
  621. /* set PERIBUS_DBG_EN and PERIBUS_DBG_CKEN*/
  622. writel(0xc, PERICFG_BASE + self->perisys_offsets.bus_peri_r1);
  623. }
  624. int dfd_set_base_addr(void *fdt)
  625. {
  626. int ret = 0;
  627. int offset;
  628. int cache_dump;
  629. u64 addr;
  630. unsigned int dfd_size;
  631. u32 addr_msb;
  632. unsigned int md_addr;
  633. unsigned long long ap_addr;
  634. DEF_PLAT_SRAM_FLAG *plat = NULL;
  635. if (!fdt)
  636. return -1;
  637. ret = get_ccci_md_view_smem_addr_size(0, &ap_addr, &md_addr, &dfd_size);
  638. if (ret < 0)
  639. return ret;
  640. offset = fdt_path_offset(fdt, "/chosen");
  641. if (offset < 0)
  642. return offset;
  643. /* pass base address to kernel */
  644. addr = cpu_to_fdt64(md_addr);
  645. ret = fdt_setprop(fdt, offset, "dfd,base_addr", &addr, sizeof(addr));
  646. if (ret < 0)
  647. return ret;
  648. addr_msb = cpu_to_fdt32(ap_addr);
  649. ret = fdt_setprop(fdt, offset, "dfd,base_addr_msb", &addr_msb, sizeof(addr_msb));
  650. if (ret < 0)
  651. return ret;
  652. #ifdef MTK_DFD_ENABLE_CACHE_DUMP
  653. cache_dump = 1;
  654. #else
  655. cache_dump = 0;
  656. #endif
  657. cache_dump = cpu_to_fdt32(cache_dump);
  658. ret = fdt_setprop(fdt, offset, "dfd,cache_dump_support", &cache_dump, sizeof(cache_dump));
  659. if (ret < 0)
  660. return ret;
  661. /*
  662. * write base address[31:1] from AP view to plat_sram_flag2[31:1]
  663. * write base address[32:32] from AP view to plat_sram_flag2[0:0]
  664. */
  665. plat = (DEF_PLAT_SRAM_FLAG *)get_dbg_info_base(PLAT_SRAM_FLAG_KEY);
  666. if (!plat) {
  667. dprintf(CRITICAL, "[dfd] error: plat == NULL\n");
  668. return -1;
  669. }
  670. plat->plat_sram_flag2 = (ap_addr & ~(0x1)) | ((ap_addr >> 32) & 0x1);
  671. dprintf(CRITICAL, "[dfd] plat->plat_sram_flag2 = 0x%lx, addr = 0x%llx, addr_msb = 0x%lx\n", plat->plat_sram_flag2,
  672. addr, addr_msb);
  673. return ret;
  674. }
  675. /* platform initial function */
  676. int platform_debug_init(void)
  677. {
  678. /* function pointer assignment */
  679. plat_spm_data_get = save_spm_data;
  680. plat_dram_get = save_dram_data;
  681. plat_cpu_bus_get = save_cpu_bus_data;
  682. #ifdef MTK_TINYSYS_SSPM_SUPPORT
  683. plat_sspm_coredump_get = save_sspm_coredump;
  684. plat_sspm_data_get = save_sspm_data;
  685. plat_sspm_xfile_get = save_sspm_xfile;
  686. plat_sspm_log_get = save_sspm_last_log;
  687. #endif
  688. #ifdef MTK_TINYSYS_SCP_SUPPORT
  689. plat_scp_coredump_get = save_scp_coredump;
  690. #endif
  691. plat_hvfs_get = save_hvfs_data;
  692. plat_dur_lkdump_get = save_log_dur_lkdump;
  693. plat_mcdi_get = save_mcdi_data;
  694. #ifdef MTK_PICACHU_SUPPORT
  695. plat_picachu_log_get = save_picachu_log;
  696. #endif
  697. plat_dfd20_get = save_dfd_data;
  698. dfd_op.acquire_ram_control = setup_snoop_filter_ram_ctrl;
  699. dfd_op.release_ram_control = return_snoop_filter_ram_ctrl;
  700. dfd_op.check_dfd_valid = check_dfd_valid;
  701. circular_buffer_op.lock = circular_buffer_lock;
  702. circular_buffer_op.unlock = circular_buffer_unlock;
  703. /* routine tasks */
  704. plat_write_dram_klog();
  705. return 1;
  706. }