op_model_ev67.c 7.0 KB

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