etb.c 6.4 KB

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