aee_platform_debug.c 22 KB

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