mt_scp_excep.c 9.0 KB

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