op_model_ev67.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @file arch/alpha/oprofile/op_model_ev67.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author Richard Henderson <rth@twiddle.net>
  8. * @author Falk Hueffner <falk@debian.org>
  9. */
  10. #include <linux/oprofile.h>
  11. #include <linux/smp.h>
  12. #include <asm/ptrace.h>
  13. #include "op_impl.h"
  14. /* Compute all of the registers in preparation for enabling profiling. */
  15. static void
  16. ev67_reg_setup(struct op_register_config *reg,
  17. struct op_counter_config *ctr,
  18. struct op_system_config *sys)
  19. {
  20. unsigned long ctl, reset, need_reset, i;
  21. /* Select desired events. */
  22. ctl = 1UL << 4; /* Enable ProfileMe mode. */
  23. /* The event numbers are chosen so we can use them directly if
  24. PCTR1 is enabled. */
  25. if (ctr[1].enabled) {
  26. ctl |= (ctr[1].event & 3) << 2;
  27. } else {
  28. if (ctr[0].event == 0) /* cycles */
  29. ctl |= 1UL << 2;
  30. }
  31. reg->mux_select = ctl;
  32. /* Select logging options. */
  33. /* ??? Need to come up with some mechanism to trace only
  34. selected processes. EV67 does not have a mechanism to
  35. select kernel or user mode only. For now, enable always. */
  36. reg->proc_mode = 0;
  37. /* EV67 cannot change the width of the counters as with the
  38. other implementations. But fortunately, we can write to
  39. the counters and set the value such that it will overflow
  40. at the right time. */
  41. reset = need_reset = 0;
  42. for (i = 0; i < 2; ++i) {
  43. unsigned long count = ctr[i].count;
  44. if (!ctr[i].enabled)
  45. continue;
  46. if (count > 0x100000)
  47. count = 0x100000;
  48. ctr[i].count = count;
  49. reset |= (0x100000 - count) << (i ? 6 : 28);
  50. if (count != 0x100000)
  51. need_reset |= 1 << i;
  52. }
  53. reg->reset_values = reset;
  54. reg->need_reset = need_reset;
  55. }
  56. /* Program all of the registers in preparation for enabling profiling. */
  57. static void
  58. ev67_cpu_setup (void *x)
  59. {
  60. struct op_register_config *reg = x;
  61. wrperfmon(2, reg->mux_select);
  62. wrperfmon(3, reg->proc_mode);
  63. wrperfmon(6, reg->reset_values | 3);
  64. }
  65. /* CTR is a counter for which the user has requested an interrupt count
  66. in between one of the widths selectable in hardware. Reset the count
  67. for CTR to the value stored in REG->RESET_VALUES. */
  68. static void
  69. ev67_reset_ctr(struct op_register_config *reg, unsigned long ctr)
  70. {
  71. wrperfmon(6, reg->reset_values | (1 << ctr));
  72. }
  73. /* ProfileMe conditions which will show up as counters. We can also
  74. detect the following, but it seems unlikely that anybody is
  75. interested in counting them:
  76. * Reset
  77. * MT_FPCR (write to floating point control register)
  78. * Arithmetic trap
  79. * Dstream Fault
  80. * Machine Check (ECC fault, etc.)
  81. * OPCDEC (illegal opcode)
  82. * Floating point disabled
  83. * Differentiate between DTB single/double misses and 3 or 4 level
  84. page tables
  85. * Istream access violation
  86. * Interrupt
  87. * Icache Parity Error.
  88. * Instruction killed (nop, trapb)
  89. Unfortunately, there seems to be no way to detect Dcache and Bcache
  90. misses; the latter could be approximated by making the counter
  91. count Bcache misses, but that is not precise.
  92. We model this as 20 counters:
  93. * PCTR0
  94. * PCTR1
  95. * 9 ProfileMe events, induced by PCTR0
  96. * 9 ProfileMe events, induced by PCTR1
  97. */
  98. enum profileme_counters {
  99. PM_STALLED, /* Stalled for at least one cycle
  100. between the fetch and map stages */
  101. PM_TAKEN, /* Conditional branch taken */
  102. PM_MISPREDICT, /* Branch caused mispredict trap */
  103. PM_ITB_MISS, /* ITB miss */
  104. PM_DTB_MISS, /* DTB miss */
  105. PM_REPLAY, /* Replay trap */
  106. PM_LOAD_STORE, /* Load-store order trap */
  107. PM_ICACHE_MISS, /* Icache miss */
  108. PM_UNALIGNED, /* Unaligned Load/Store */
  109. PM_NUM_COUNTERS
  110. };
  111. static inline void
  112. op_add_pm(unsigned long pc, int kern, unsigned long counter,
  113. struct op_counter_config *ctr, unsigned long event)
  114. {
  115. unsigned long fake_counter = 2 + event;
  116. if (counter == 1)
  117. fake_counter += PM_NUM_COUNTERS;
  118. if (ctr[fake_counter].enabled)
  119. oprofile_add_pc(pc, kern, fake_counter);
  120. }
  121. static void
  122. ev67_handle_interrupt(unsigned long which, struct pt_regs *regs,
  123. struct op_counter_config *ctr)
  124. {
  125. unsigned long pmpc, pctr_ctl;
  126. int kern = !user_mode(regs);
  127. int mispredict = 0;
  128. union {
  129. unsigned long v;
  130. struct {
  131. unsigned reserved: 30; /* 0-29 */
  132. unsigned overcount: 3; /* 30-32 */
  133. unsigned icache_miss: 1; /* 33 */
  134. unsigned trap_type: 4; /* 34-37 */
  135. unsigned load_store: 1; /* 38 */
  136. unsigned trap: 1; /* 39 */
  137. unsigned mispredict: 1; /* 40 */
  138. } fields;
  139. } i_stat;
  140. enum trap_types {
  141. TRAP_REPLAY,
  142. TRAP_INVALID0,
  143. TRAP_DTB_DOUBLE_MISS_3,
  144. TRAP_DTB_DOUBLE_MISS_4,
  145. TRAP_FP_DISABLED,
  146. TRAP_UNALIGNED,
  147. TRAP_DTB_SINGLE_MISS,
  148. TRAP_DSTREAM_FAULT,
  149. TRAP_OPCDEC,
  150. TRAP_INVALID1,
  151. TRAP_MACHINE_CHECK,
  152. TRAP_INVALID2,
  153. TRAP_ARITHMETIC,
  154. TRAP_INVALID3,
  155. TRAP_MT_FPCR,
  156. TRAP_RESET
  157. };
  158. pmpc = wrperfmon(9, 0);
  159. /* ??? Don't know how to handle physical-mode PALcode address. */
  160. if (pmpc & 1)
  161. return;
  162. pmpc &= ~2; /* clear reserved bit */
  163. i_stat.v = wrperfmon(8, 0);
  164. if (i_stat.fields.trap) {
  165. switch (i_stat.fields.trap_type) {
  166. case TRAP_INVALID1:
  167. case TRAP_INVALID2:
  168. case TRAP_INVALID3:
  169. /* Pipeline redirection occurred. PMPC points
  170. to PALcode. Recognize ITB miss by PALcode
  171. offset address, and get actual PC from
  172. EXC_ADDR. */
  173. oprofile_add_pc(regs->pc, kern, which);
  174. if ((pmpc & ((1 << 15) - 1)) == 581)
  175. op_add_pm(regs->pc, kern, which,
  176. ctr, PM_ITB_MISS);
  177. /* Most other bit and counter values will be
  178. those for the first instruction in the
  179. fault handler, so we're done. */
  180. return;
  181. case TRAP_REPLAY:
  182. op_add_pm(pmpc, kern, which, ctr,
  183. (i_stat.fields.load_store
  184. ? PM_LOAD_STORE : PM_REPLAY));
  185. break;
  186. case TRAP_DTB_DOUBLE_MISS_3:
  187. case TRAP_DTB_DOUBLE_MISS_4:
  188. case TRAP_DTB_SINGLE_MISS:
  189. op_add_pm(pmpc, kern, which, ctr, PM_DTB_MISS);
  190. break;
  191. case TRAP_UNALIGNED:
  192. op_add_pm(pmpc, kern, which, ctr, PM_UNALIGNED);
  193. break;
  194. case TRAP_INVALID0:
  195. case TRAP_FP_DISABLED:
  196. case TRAP_DSTREAM_FAULT:
  197. case TRAP_OPCDEC:
  198. case TRAP_MACHINE_CHECK:
  199. case TRAP_ARITHMETIC:
  200. case TRAP_MT_FPCR:
  201. case TRAP_RESET:
  202. break;
  203. }
  204. /* ??? JSR/JMP/RET/COR or HW_JSR/HW_JMP/HW_RET/HW_COR
  205. mispredicts do not set this bit but can be
  206. recognized by the presence of one of these
  207. instructions at the PMPC location with bit 39
  208. set. */
  209. if (i_stat.fields.mispredict) {
  210. mispredict = 1;
  211. op_add_pm(pmpc, kern, which, ctr, PM_MISPREDICT);
  212. }
  213. }
  214. oprofile_add_pc(pmpc, kern, which);
  215. pctr_ctl = wrperfmon(5, 0);
  216. if (pctr_ctl & (1UL << 27))
  217. op_add_pm(pmpc, kern, which, ctr, PM_STALLED);
  218. /* Unfortunately, TAK is undefined on mispredicted branches.
  219. ??? It is also undefined for non-cbranch insns, should
  220. check that. */
  221. if (!mispredict && pctr_ctl & (1UL << 0))
  222. op_add_pm(pmpc, kern, which, ctr, PM_TAKEN);
  223. }
  224. struct op_axp_model op_model_ev67 = {
  225. .reg_setup = ev67_reg_setup,
  226. .cpu_setup = ev67_cpu_setup,
  227. .reset_ctr = ev67_reset_ctr,
  228. .handle_interrupt = ev67_handle_interrupt,
  229. .cpu_type = "alpha/ev67",
  230. .num_counters = 20,
  231. .can_set_proc_mode = 0,
  232. };