etb.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <etb.h>
  32. #include <string.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <latch.h>
  36. #include <malloc.h>
  37. #include <reg.h>
  38. #include <platform/sync_write.h>
  39. #include <plat_debug_interface.h>
  40. #include "utils.h"
  41. #include <platform/sec_devinfo.h>
  42. #include <plat_sram_flag.h>
  43. static unsigned int etb_users[MAX_NR_ETB];
  44. static const char* etb_user_id_to_name[ETB_USER_TYPE] = {
  45. "big_core",
  46. "cm4",
  47. "audio_cm4",
  48. "bus_tracer",
  49. "mcsi_b_tracer_normal_mode",
  50. "reserved",
  51. "reserved",
  52. "reserved"
  53. };
  54. static void get_etb_users(void)
  55. {
  56. unsigned int i, offset = 0;
  57. unsigned long plat_sram_flag0;
  58. /* get the user id from plat_sram_flag0 */
  59. if (cfg_pc_latch.plat_sram_flag0) {
  60. plat_sram_flag0 = readl(cfg_pc_latch.plat_sram_flag0);
  61. for (i = 0; i <= cfg_etb.nr_etb-1; ++i) {
  62. if (cfg_etb.etb[i].support_multi_user == 1 && offset <= 9) {
  63. /* plat_sram_flag0 is for the first 10 ETBs that supports multi-user */
  64. etb_users[i] = extract_n2mbits(plat_sram_flag0, 2+offset*3, 4+offset*3);
  65. offset++;
  66. } else {
  67. /* user id 7 stands for "reserved" */
  68. etb_users[i] = 7;
  69. }
  70. }
  71. }
  72. }
  73. static unsigned int unlock_etb(unsigned long base)
  74. {
  75. if (base == 0)
  76. return 0;
  77. writel(ETB_UNLOCK_MAGIC, base + ETB_LAR);
  78. dsb();
  79. if (readl(base + ETB_LSR) != 0x1)
  80. return 0;
  81. return 1;
  82. }
  83. static unsigned int lock_etb(unsigned long base)
  84. {
  85. if (base == 0)
  86. return 0;
  87. dsb();
  88. writel(0, base + ETB_LAR);
  89. return 1;
  90. }
  91. static int copy_from_etb(unsigned int index, char *buf, int *wp, unsigned int max_buf_size)
  92. {
  93. unsigned long depth, etb_rp, etb_wp, ret, base;
  94. unsigned long nr_words, nr_unaligned_bytes;
  95. unsigned int i;
  96. DEF_PLAT_SRAM_FLAG* plat = NULL;
  97. if (buf == NULL || wp == NULL || (index+1 > cfg_etb.nr_etb))
  98. return -1;
  99. base = cfg_etb.etb[index].base;
  100. if (base == 0)
  101. return -1;
  102. if (unlock_etb(base) != 1) {
  103. dprintf(CRITICAL, "[ETB%d] unlock etb failed\n", index);
  104. return 0;
  105. }
  106. ret = readl(base + ETB_STATUS);
  107. etb_rp = readl(base + ETB_READADDR);
  108. etb_wp = readl(base + ETB_WRITEADDR);
  109. /* depth is counted in byte */
  110. if (ret & 0x1)
  111. depth = readl(base + ETB_DEPTH) << 2;
  112. else
  113. depth = CIRC_CNT(etb_wp, etb_rp, readl(base + ETB_DEPTH) << 2);
  114. dprintf(INFO, "[ETB%d] depth = 0x%lx bytes (etb status = 0x%lx, rp = 0x%lx, wp = 0x%lx)\n",
  115. index, depth, ret, etb_rp, etb_wp);
  116. if (depth == 0)
  117. return 0;
  118. *wp += snprintf(buf + *wp, max_buf_size - *wp, "\n*************************** ETB%d @0x%08lx ***************************\n",
  119. index, base);
  120. /* disable ETB before dump */
  121. writel(0x0, base + ETB_CTRL);
  122. nr_words = depth / 4;
  123. nr_unaligned_bytes = depth % 4;
  124. for (i = 0; i < nr_words; ++i)
  125. *wp += snprintf(buf + *wp, max_buf_size - *wp, "%08lx\n", (unsigned long) readl(base + ETB_READMEM));
  126. if (nr_unaligned_bytes != 0)
  127. /* ETB depth is not word-aligned -> not expected */
  128. *wp += snprintf(buf + *wp, max_buf_size - *wp, "[warning] etb depth is not word-aligned!\n");
  129. *wp += snprintf(buf + *wp, max_buf_size - *wp, "\n\ndepth = 0x%lx bytes (etb status = 0x%lx, rp = 0x%lx, wp = 0x%lx)\n",
  130. depth, ret, etb_rp, etb_wp);
  131. writel(0x0, base + ETB_TRIGGERCOUNT);
  132. writel(0x0, base + ETB_READADDR);
  133. writel(0x0, base + ETB_WRITEADDR);
  134. dsb();
  135. if (lock_etb(base) != 1)
  136. dprintf(INFO, "lock etb failed -> ignore due to no impact to system\n");
  137. if (!get_dbg_info_base)
  138. dprintf(CRITICAL, "[etb] get_dbg_info_base is NULL\n");
  139. plat = (DEF_PLAT_SRAM_FLAG *)get_dbg_info_base(PLAT_SRAM_FLAG_KEY);
  140. if (!plat) {
  141. dprintf(CRITICAL, "[etb] get_dbg_info_base(PLAT_SRAM_FLAG_KEY) is NULL\n");
  142. return 0;
  143. }
  144. base = cfg_etb.dem_base;
  145. *wp += snprintf(buf + *wp, max_buf_size - *wp, "\nstatus1 = 0x%x, status2 = 0x%x, time_h = 0x%x, time_l = 0x%x, stage = 0x%x, kernel_start_time = 0x%x\n",
  146. readl(base+0x88), readl(base+0x8c), readl(base+0x94), readl(base+0x90), readl(base+0x98), plat->plat_sram_flag0);
  147. *wp += snprintf(buf + *wp, max_buf_size - *wp, "\n");
  148. return 1;
  149. }
  150. int etb_get(void **data, int *len)
  151. {
  152. int ret;
  153. unsigned int i;
  154. /*
  155. * every 4-byte word would be output in 9 characters (e.g. "ffffabcd\n")
  156. * -> etb_sz * 3 is sufficient for all the etb content
  157. */
  158. unsigned long total_sz_to_dump = cfg_etb.total_etb_sz * 3;
  159. if (len == NULL || data == NULL)
  160. return -1;
  161. *len = 0;
  162. *data = NULL;
  163. if (cfg_etb.nr_etb == 0 || cfg_etb.dem_base == 0)
  164. return 0;
  165. *data = malloc(total_sz_to_dump);
  166. if (*data == NULL)
  167. return 0;
  168. /* enable ATB clock */
  169. writel(0x1, cfg_etb.dem_base + DEM_ATB_CLK);
  170. /* get the information of ETB users from sram flag */
  171. get_etb_users();
  172. ret = copy_from_etb(0, *data, len, total_sz_to_dump);
  173. if (ret < 0 || (*len > 0 && ((unsigned long)*len > total_sz_to_dump))) {
  174. if (*len > 0 && ((unsigned long)*len > total_sz_to_dump))
  175. *len = total_sz_to_dump;
  176. return ret;
  177. }
  178. return 1;
  179. }
  180. void etb_put(void **data)
  181. {
  182. free(*data);
  183. }