dv-bfin_trace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Blackfin Trace (TBUF) model.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Analog Devices, Inc.
  4. This file is part of simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "devices.h"
  19. #include "dv-bfin_cec.h"
  20. #include "dv-bfin_trace.h"
  21. /* Note: The circular buffering here might look a little buggy wrt mid-reads
  22. and consuming the top entry, but this is simulating hardware behavior.
  23. The hardware is simple, dumb, and fast. Don't write dumb Blackfin
  24. software and you won't have a problem. */
  25. /* The hardware is limited to 16 entries and defines TBUFCTL. Let's extend it ;). */
  26. #ifndef SIM_BFIN_TRACE_DEPTH
  27. #define SIM_BFIN_TRACE_DEPTH 6
  28. #endif
  29. #define SIM_BFIN_TRACE_LEN (1 << SIM_BFIN_TRACE_DEPTH)
  30. #define SIM_BFIN_TRACE_LEN_MASK (SIM_BFIN_TRACE_LEN - 1)
  31. struct bfin_trace_entry
  32. {
  33. bu32 src, dst;
  34. };
  35. struct bfin_trace
  36. {
  37. bu32 base;
  38. struct bfin_trace_entry buffer[SIM_BFIN_TRACE_LEN];
  39. int top, bottom;
  40. bool mid;
  41. /* Order after here is important -- matches hardware MMR layout. */
  42. bu32 tbufctl, tbufstat;
  43. char _pad[0x100 - 0x8];
  44. bu32 tbuf;
  45. };
  46. #define mmr_base() offsetof(struct bfin_trace, tbufctl)
  47. #define mmr_offset(mmr) (offsetof(struct bfin_trace, mmr) - mmr_base())
  48. static const char * const mmr_names[] =
  49. {
  50. "TBUFCTL", "TBUFSTAT", [mmr_offset (tbuf) / 4] = "TBUF",
  51. };
  52. #define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>")
  53. /* Ugh, circular buffers. */
  54. #define TBUF_LEN(t) ((t)->top - (t)->bottom)
  55. #define TBUF_IDX(i) ((i) & SIM_BFIN_TRACE_LEN_MASK)
  56. /* TOP is the next slot to fill. */
  57. #define TBUF_TOP(t) (&(t)->buffer[TBUF_IDX ((t)->top)])
  58. /* LAST is the latest valid slot. */
  59. #define TBUF_LAST(t) (&(t)->buffer[TBUF_IDX ((t)->top - 1)])
  60. /* LAST_LAST is the second-to-last valid slot. */
  61. #define TBUF_LAST_LAST(t) (&(t)->buffer[TBUF_IDX ((t)->top - 2)])
  62. static unsigned
  63. bfin_trace_io_write_buffer (struct hw *me, const void *source,
  64. int space, address_word addr, unsigned nr_bytes)
  65. {
  66. struct bfin_trace *trace = hw_data (me);
  67. bu32 mmr_off;
  68. bu32 value;
  69. /* Invalid access mode is higher priority than missing register. */
  70. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, true))
  71. return 0;
  72. value = dv_load_4 (source);
  73. mmr_off = addr - trace->base;
  74. HW_TRACE_WRITE ();
  75. switch (mmr_off)
  76. {
  77. case mmr_offset(tbufctl):
  78. trace->tbufctl = value;
  79. break;
  80. case mmr_offset(tbufstat):
  81. case mmr_offset(tbuf):
  82. /* Discard writes to these. */
  83. break;
  84. default:
  85. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  86. return 0;
  87. }
  88. return nr_bytes;
  89. }
  90. static unsigned
  91. bfin_trace_io_read_buffer (struct hw *me, void *dest,
  92. int space, address_word addr, unsigned nr_bytes)
  93. {
  94. struct bfin_trace *trace = hw_data (me);
  95. bu32 mmr_off;
  96. bu32 value;
  97. /* Invalid access mode is higher priority than missing register. */
  98. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, false))
  99. return 0;
  100. mmr_off = addr - trace->base;
  101. HW_TRACE_READ ();
  102. switch (mmr_off)
  103. {
  104. case mmr_offset(tbufctl):
  105. value = trace->tbufctl;
  106. break;
  107. case mmr_offset(tbufstat):
  108. /* Hardware is limited to 16 entries, so to stay compatible with
  109. software, limit the value to 16. For software algorithms that
  110. keep reading while (TBUFSTAT != 0), they'll get all of it. */
  111. value = min (TBUF_LEN (trace), 16);
  112. break;
  113. case mmr_offset(tbuf):
  114. {
  115. struct bfin_trace_entry *e;
  116. if (TBUF_LEN (trace) == 0)
  117. {
  118. value = 0;
  119. break;
  120. }
  121. e = TBUF_LAST (trace);
  122. if (trace->mid)
  123. {
  124. value = e->src;
  125. --trace->top;
  126. }
  127. else
  128. value = e->dst;
  129. trace->mid = !trace->mid;
  130. break;
  131. }
  132. default:
  133. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  134. return 0;
  135. }
  136. dv_store_4 (dest, value);
  137. return nr_bytes;
  138. }
  139. static void
  140. attach_bfin_trace_regs (struct hw *me, struct bfin_trace *trace)
  141. {
  142. address_word attach_address;
  143. int attach_space;
  144. unsigned attach_size;
  145. reg_property_spec reg;
  146. if (hw_find_property (me, "reg") == NULL)
  147. hw_abort (me, "Missing \"reg\" property");
  148. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  149. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  150. hw_unit_address_to_attach_address (hw_parent (me),
  151. &reg.address,
  152. &attach_space, &attach_address, me);
  153. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  154. if (attach_size != BFIN_COREMMR_TRACE_SIZE)
  155. hw_abort (me, "\"reg\" size must be %#x", BFIN_COREMMR_TRACE_SIZE);
  156. hw_attach_address (hw_parent (me),
  157. 0, attach_space, attach_address, attach_size, me);
  158. trace->base = attach_address;
  159. }
  160. static void
  161. bfin_trace_finish (struct hw *me)
  162. {
  163. struct bfin_trace *trace;
  164. trace = HW_ZALLOC (me, struct bfin_trace);
  165. set_hw_data (me, trace);
  166. set_hw_io_read_buffer (me, bfin_trace_io_read_buffer);
  167. set_hw_io_write_buffer (me, bfin_trace_io_write_buffer);
  168. attach_bfin_trace_regs (me, trace);
  169. }
  170. const struct hw_descriptor dv_bfin_trace_descriptor[] =
  171. {
  172. {"bfin_trace", bfin_trace_finish,},
  173. {NULL, NULL},
  174. };
  175. #define TRACE_STATE(cpu) DV_STATE_CACHED (cpu, trace)
  176. /* This is not re-entrant, but neither is the cpu state, so this shouldn't
  177. be a big deal ... */
  178. void bfin_trace_queue (SIM_CPU *cpu, bu32 src_pc, bu32 dst_pc, int hwloop)
  179. {
  180. struct bfin_trace *trace = TRACE_STATE (cpu);
  181. struct bfin_trace_entry *e;
  182. int len, ivg;
  183. /* Only queue if powered. */
  184. if (!(trace->tbufctl & TBUFPWR))
  185. return;
  186. /* Only queue if enabled. */
  187. if (!(trace->tbufctl & TBUFEN))
  188. return;
  189. /* Ignore hardware loops.
  190. XXX: This is what the hardware does, but an option to ignore
  191. could be useful for debugging ... */
  192. if (hwloop >= 0)
  193. return;
  194. /* Only queue if at right level. */
  195. ivg = cec_get_ivg (cpu);
  196. if (ivg == IVG_RST)
  197. /* XXX: This is what the hardware does, but an option to ignore
  198. could be useful for debugging ... */
  199. return;
  200. if (ivg <= IVG_EVX && (trace->tbufctl & TBUFOVF))
  201. /* XXX: This is what the hardware does, but an option to ignore
  202. could be useful for debugging ... just don't throw an
  203. exception when full and in EVT{0..3}. */
  204. return;
  205. /* Are we full ? */
  206. len = TBUF_LEN (trace);
  207. if (len == SIM_BFIN_TRACE_LEN)
  208. {
  209. if (trace->tbufctl & TBUFOVF)
  210. {
  211. cec_exception (cpu, VEC_OVFLOW);
  212. return;
  213. }
  214. /* Overwrite next entry. */
  215. ++trace->bottom;
  216. }
  217. /* One level compression. */
  218. if (len >= 1 && (trace->tbufctl & TBUFCMPLP))
  219. {
  220. e = TBUF_LAST (trace);
  221. if (src_pc == (e->src & ~1) && dst_pc == (e->dst & ~1))
  222. {
  223. /* Hardware sets LSB when level is compressed. */
  224. e->dst |= 1;
  225. return;
  226. }
  227. }
  228. /* Two level compression. */
  229. if (len >= 2 && (trace->tbufctl & TBUFCMPLP_DOUBLE))
  230. {
  231. e = TBUF_LAST_LAST (trace);
  232. if (src_pc == (e->src & ~1) && dst_pc == (e->dst & ~1))
  233. {
  234. /* Hardware sets LSB when level is compressed. */
  235. e->src |= 1;
  236. return;
  237. }
  238. }
  239. e = TBUF_TOP (trace);
  240. e->dst = dst_pc;
  241. e->src = src_pc;
  242. ++trace->top;
  243. }