mt_scp_excep.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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) 2015. 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 <stdint.h>
  32. #include <string.h>
  33. #include <debug.h>
  34. #include <platform/boot_mode.h>
  35. #include <platform/partition.h>
  36. #include <platform/verified_boot.h>
  37. #include <platform/sec_policy.h>
  38. #include <assert.h>
  39. #include <platform/mt_scp.h>
  40. #include <platform/errno.h>
  41. #include <mt_scp.h>
  42. #include <mt_scp_excep.h>
  43. #include <reg.h>
  44. #include <malloc.h>
  45. #include <lib/crc.h>
  46. #include <lib/zutil.h>
  47. #include <lib/zlib.h>
  48. #include <platform/mtk_wdt.h>
  49. #ifdef MTK_3LEVEL_PAGETABLE
  50. #include <arch/arm/mmu.h>
  51. #include <stdlib.h>
  52. #include <err.h>
  53. #endif
  54. struct reg_save_st {
  55. uint32_t addr;
  56. uint32_t size;
  57. };
  58. struct reg_save_st reg_save_list[] = {
  59. /* size must 16 byte alignment */
  60. {0x10721000, 0x120},
  61. {0x10724000, 0x170},
  62. {0x10730000, 0x180},
  63. {0x10732000, 0x260},
  64. {0x10733000, 0x120},
  65. {0x10750000, 0x330},
  66. {0x10751000, 0x10},
  67. {0x10751400, 0x70},
  68. {0x10752000, 0x340},
  69. {0x107A5000, 0x110},
  70. {0x10001B14, 0x10},
  71. };
  72. void scp_do_regdump(uint32_t *out, uint32_t *out_end)
  73. {
  74. int i = 0;
  75. void *from;
  76. uint32_t *buf = out;
  77. int size_limit = sizeof(reg_save_list) / sizeof(struct reg_save_st);
  78. for (i = 0; i < size_limit; i++) {
  79. if (((uint32_t)buf + reg_save_list[i].size
  80. + sizeof(struct reg_save_st)) > (uint32_t)out_end) {
  81. dprintf(CRITICAL, "[SCP] %s overflow\n", __func__);
  82. break;
  83. }
  84. *buf = reg_save_list[i].addr;
  85. buf++;
  86. *buf = reg_save_list[i].size;
  87. buf++;
  88. from = (void *)reg_save_list[i].addr;
  89. memcpy(buf, from, reg_save_list[i].size);
  90. buf += (reg_save_list[i].size / sizeof(uint32_t));
  91. }
  92. }
  93. void scp_do_l1cdump(uint32_t *out, uint32_t *out_end)
  94. {
  95. uint32_t *buf = out;
  96. uint32_t tmp;
  97. tmp = readl(R_SEC_CTRL);
  98. /* enable cache debug */
  99. writel(tmp | B_CORE0_CACHE_DBG_EN | B_CORE1_CACHE_DBG_EN, R_SEC_CTRL);
  100. if ((uint32_t)buf + MDUMP_L1C_SIZE > (uint32_t)out_end) {
  101. dprintf(CRITICAL, "[SCP] %s overflow\n", __func__);
  102. return;
  103. }
  104. memcpy(buf, (void *)R_CORE0_CACHE_RAM, MDUMP_L1C_SIZE);
  105. /* disable cache debug */
  106. writel(tmp, R_SEC_CTRL);
  107. }
  108. void scp_do_tbufdump(uint32_t *out, uint32_t *out_end)
  109. {
  110. uint32_t *buf = out;
  111. uint32_t tmp, tmp1, index, offset, wbuf_ptr, wbuf1_ptr;
  112. int i;
  113. wbuf1_ptr = readl(R_CORE0_TBUF_WPTR);
  114. wbuf_ptr = wbuf1_ptr & 0x1f;
  115. wbuf1_ptr = wbuf1_ptr >> 8;
  116. tmp = readl(R_CORE0_DBG_CTRL) & M_CORE_TBUF_DBG_SEL;
  117. for (i = 0; i < 32; i++) {
  118. index = ((wbuf_ptr + i) / 4) & 0x7;
  119. offset = ((wbuf_ptr + i) % 4) * 0x8;
  120. tmp1 = (index << S_CORE_TBUF_S) | (index << S_CORE_TBUF1_S);
  121. writel(tmp | tmp1, R_CORE0_DBG_CTRL);
  122. *(buf) = readl(R_CORE0_TBUF_DATA31_0 + offset);
  123. *(buf + 1) = readl(R_CORE0_TBUF_DATA63_32 + offset);
  124. buf += 2;
  125. }
  126. for (i = 0; i < 32; i++) {
  127. dprintf(INFO, "[SCP] C0:%02d:0x%08x::0x%08x\n",
  128. i, *(out + i * 2), *(out + i * 2 + 1));
  129. }
  130. for (i = 0; i < 32; i++) {
  131. index = (((wbuf1_ptr + i) / 4) & 0x7) | 0x8;
  132. offset = ((wbuf1_ptr + i) % 4) * 0x8;
  133. tmp1 = (index << S_CORE_TBUF_S) | (index << S_CORE_TBUF1_S);
  134. writel(tmp | tmp1, R_CORE0_DBG_CTRL);
  135. *(buf) = readl(R_CORE0_TBUF_DATA31_0 + offset);
  136. *(buf + 1) = readl(R_CORE0_TBUF_DATA63_32 + offset);
  137. buf += 2;
  138. }
  139. for (i = 0; i < 32; i++) {
  140. dprintf(INFO, "[SCP] C1:%02d:0x%08x::0x%08x\n",
  141. i, *(out + 64 + i * 2), *(out + 64 + i * 2 + 1));
  142. }
  143. }
  144. #define BUF_SIZE 65536
  145. int scp_zip_compress(unsigned char* input, unsigned char* output, unsigned int input_size)
  146. {
  147. int ret, flush;
  148. int have;
  149. z_stream strm;
  150. int i = 0;
  151. int j = 0;
  152. unsigned int avail_input_size = input_size;
  153. unsigned char *ibuf;
  154. unsigned char *obuf;
  155. ibuf = malloc(BUF_SIZE);
  156. if (ibuf == NULL)
  157. return Z_BUF_ERROR;
  158. obuf = malloc(BUF_SIZE);
  159. if (obuf == NULL) {
  160. free(ibuf);
  161. return Z_BUF_ERROR;
  162. }
  163. strm.zalloc = Z_NULL;
  164. strm.zfree = Z_NULL;
  165. strm.opaque = Z_NULL;
  166. /* allocate deflate state */
  167. memset(&strm, 0, sizeof(z_stream));
  168. ret = deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
  169. if (ret != Z_OK) {
  170. free(ibuf);
  171. free(obuf);
  172. return ret;
  173. }
  174. /* compress until end of file */
  175. do {
  176. strm.avail_in = BUF_SIZE;
  177. strm.next_in = ibuf;
  178. strm.avail_out = BUF_SIZE;
  179. strm.next_out = obuf;
  180. flush = Z_NO_FLUSH;
  181. if (avail_input_size < BUF_SIZE) {
  182. strm.avail_in = avail_input_size;
  183. flush = Z_FINISH;
  184. }
  185. memcpy(ibuf, (void *)&input[i], strm.avail_in);
  186. i += strm.avail_in;
  187. avail_input_size -= strm.avail_in;
  188. strm.next_in = ibuf;
  189. /* run deflate() on input until output buffer not full, finish
  190. compression if all of source has been read in */
  191. do {
  192. strm.avail_out = BUF_SIZE;
  193. strm.next_out = obuf;
  194. ret = deflate(&strm, flush); /* no bad return value */
  195. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  196. have = BUF_SIZE - strm.avail_out;
  197. memcpy((void *)&output[j], obuf, have);
  198. j+=have;
  199. mtk_wdt_restart();
  200. } while (strm.avail_out == 0);
  201. assert(strm.avail_in == 0); /* all input will be used */
  202. /* done when last data in file processed */
  203. } while (flush != Z_FINISH);
  204. assert(ret == Z_STREAM_END); /* stream will be complete */
  205. free(ibuf);
  206. free(obuf);
  207. /* Clean up and return */
  208. (void)deflateEnd(&strm);
  209. return j;
  210. }
  211. /*
  212. * this function need SCP to keeping awaken
  213. * scp_crash_dump: dump scp tcm info.
  214. * @param MemoryDump: scp dump struct
  215. * @param scp_core_id: core id
  216. * @return: scp dump size
  217. */
  218. int scp_crash_dump(void *crash_buffer)
  219. {
  220. unsigned int scp_dump_size;
  221. int datasize;
  222. struct MemoryDump *pMemoryDump;
  223. void *tmp_ptr;
  224. uint32_t dram_start = 0;
  225. uint32_t dram_size = 0;
  226. uint32_t tmp;
  227. struct scp_region_info_st* scp_region_info = (void *)(SCP_SRAM_BASE + 0x4);
  228. tmp = scp_region_info->ap_loader_start;
  229. tmp_ptr = pMemoryDump = (void *)tmp;
  230. /* region_info will be null when recovery boot fail, skip scp coredump */
  231. if (tmp == 0) {
  232. dprintf(CRITICAL, "[scp] scp_region_info = 0, skip coredump\n");
  233. return 0;
  234. }
  235. if (arch_mmu_map((uint64_t)tmp, (uint32_t)tmp,
  236. MMU_MEMORY_TYPE_NORMAL_WRITE_BACK |
  237. MMU_MEMORY_AP_P_RW_U_NA, SCP_DRAM_SIZE) != NO_ERROR) {
  238. dprintf(CRITICAL, "scp_crash_dump: tmp_dram error\n");
  239. return 0;
  240. }
  241. memset(tmp_ptr, 0, SCP_DRAM_OFFSET);
  242. memcpy((void *)&(pMemoryDump->l2tcm),
  243. (void *)(SCP_SRAM_BASE), (SCP_SRAM_SIZE));
  244. scp_do_l1cdump((void *)&(pMemoryDump->l1c),
  245. (void *)&(pMemoryDump->regdump));
  246. /* dump sys registers */
  247. scp_do_regdump((void *)&(pMemoryDump->regdump),
  248. (void *)&(pMemoryDump->tbuf));
  249. scp_do_tbufdump((void *)&(pMemoryDump->tbuf),
  250. (void *)&(pMemoryDump->dram));
  251. scp_dump_size = MDUMP_L2TCM_SIZE + MDUMP_L1C_SIZE +
  252. MDUMP_REGDUMP_SIZE + MDUMP_TBUF_SIZE;
  253. /* L1C support? */
  254. if ((int)(scp_region_info->ap_dram_size) <= 0) {
  255. dprintf(INFO, "[scp] ap_dram_size <=0\n");
  256. } else {
  257. dram_start = (uint32_t)tmp_ptr + SCP_DRAM_OFFSET;
  258. dram_size = scp_region_info->ap_dram_size;
  259. if (dram_size > MDUMP_DRAM_SIZE) {
  260. dprintf(INFO, "ap_dram_size %x > %x, reset size\n",
  261. dram_size, MDUMP_DRAM_SIZE);
  262. dram_size = MDUMP_DRAM_SIZE;
  263. }
  264. /* beware, dest overwrite source memcpy, scp_dump_size must < 2MB */
  265. memcpy((void *)&(pMemoryDump->dram), (void *)dram_start, dram_size);
  266. scp_dump_size += ROUNDUP(dram_size, 4);
  267. }
  268. datasize = scp_zip_compress((void *)tmp_ptr, (void *)crash_buffer, scp_dump_size);
  269. return datasize;
  270. }