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