dv-bfin_trace.c 7.4 KB

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