op_model_rs64.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/oprofile.h>
  10. #include <linux/smp.h>
  11. #include <asm/ptrace.h>
  12. #include <asm/processor.h>
  13. #include <asm/cputable.h>
  14. #include <asm/oprofile_impl.h>
  15. #define dbg(args...)
  16. static void ctrl_write(unsigned int i, unsigned int val)
  17. {
  18. unsigned int tmp = 0;
  19. unsigned long shift = 0, mask = 0;
  20. dbg("ctrl_write %d %x\n", i, val);
  21. switch(i) {
  22. case 0:
  23. tmp = mfspr(SPRN_MMCR0);
  24. shift = 6;
  25. mask = 0x7F;
  26. break;
  27. case 1:
  28. tmp = mfspr(SPRN_MMCR0);
  29. shift = 0;
  30. mask = 0x3F;
  31. break;
  32. case 2:
  33. tmp = mfspr(SPRN_MMCR1);
  34. shift = 31 - 4;
  35. mask = 0x1F;
  36. break;
  37. case 3:
  38. tmp = mfspr(SPRN_MMCR1);
  39. shift = 31 - 9;
  40. mask = 0x1F;
  41. break;
  42. case 4:
  43. tmp = mfspr(SPRN_MMCR1);
  44. shift = 31 - 14;
  45. mask = 0x1F;
  46. break;
  47. case 5:
  48. tmp = mfspr(SPRN_MMCR1);
  49. shift = 31 - 19;
  50. mask = 0x1F;
  51. break;
  52. case 6:
  53. tmp = mfspr(SPRN_MMCR1);
  54. shift = 31 - 24;
  55. mask = 0x1F;
  56. break;
  57. case 7:
  58. tmp = mfspr(SPRN_MMCR1);
  59. shift = 31 - 28;
  60. mask = 0xF;
  61. break;
  62. }
  63. tmp = tmp & ~(mask << shift);
  64. tmp |= val << shift;
  65. switch(i) {
  66. case 0:
  67. case 1:
  68. mtspr(SPRN_MMCR0, tmp);
  69. break;
  70. default:
  71. mtspr(SPRN_MMCR1, tmp);
  72. }
  73. dbg("ctrl_write mmcr0 %lx mmcr1 %lx\n", mfspr(SPRN_MMCR0),
  74. mfspr(SPRN_MMCR1));
  75. }
  76. static unsigned long reset_value[OP_MAX_COUNTER];
  77. static int num_counters;
  78. static int rs64_reg_setup(struct op_counter_config *ctr,
  79. struct op_system_config *sys,
  80. int num_ctrs)
  81. {
  82. int i;
  83. num_counters = num_ctrs;
  84. for (i = 0; i < num_counters; ++i)
  85. reset_value[i] = 0x80000000UL - ctr[i].count;
  86. /* XXX setup user and kernel profiling */
  87. return 0;
  88. }
  89. static int rs64_cpu_setup(struct op_counter_config *ctr)
  90. {
  91. unsigned int mmcr0;
  92. /* reset MMCR0 and set the freeze bit */
  93. mmcr0 = MMCR0_FC;
  94. mtspr(SPRN_MMCR0, mmcr0);
  95. /* reset MMCR1, MMCRA */
  96. mtspr(SPRN_MMCR1, 0);
  97. if (cpu_has_feature(CPU_FTR_MMCRA))
  98. mtspr(SPRN_MMCRA, 0);
  99. mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
  100. /* Only applies to POWER3, but should be safe on RS64 */
  101. mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
  102. mtspr(SPRN_MMCR0, mmcr0);
  103. dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
  104. mfspr(SPRN_MMCR0));
  105. dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
  106. mfspr(SPRN_MMCR1));
  107. return 0;
  108. }
  109. static int rs64_start(struct op_counter_config *ctr)
  110. {
  111. int i;
  112. unsigned int mmcr0;
  113. /* set the PMM bit (see comment below) */
  114. mtmsrd(mfmsr() | MSR_PMM);
  115. for (i = 0; i < num_counters; ++i) {
  116. if (ctr[i].enabled) {
  117. classic_ctr_write(i, reset_value[i]);
  118. ctrl_write(i, ctr[i].event);
  119. } else {
  120. classic_ctr_write(i, 0);
  121. }
  122. }
  123. mmcr0 = mfspr(SPRN_MMCR0);
  124. /*
  125. * now clear the freeze bit, counting will not start until we
  126. * rfid from this excetion, because only at that point will
  127. * the PMM bit be cleared
  128. */
  129. mmcr0 &= ~MMCR0_FC;
  130. mtspr(SPRN_MMCR0, mmcr0);
  131. dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  132. return 0;
  133. }
  134. static void rs64_stop(void)
  135. {
  136. unsigned int mmcr0;
  137. /* freeze counters */
  138. mmcr0 = mfspr(SPRN_MMCR0);
  139. mmcr0 |= MMCR0_FC;
  140. mtspr(SPRN_MMCR0, mmcr0);
  141. dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  142. mb();
  143. }
  144. static void rs64_handle_interrupt(struct pt_regs *regs,
  145. struct op_counter_config *ctr)
  146. {
  147. unsigned int mmcr0;
  148. int is_kernel;
  149. int val;
  150. int i;
  151. unsigned long pc = mfspr(SPRN_SIAR);
  152. is_kernel = is_kernel_addr(pc);
  153. /* set the PMM bit (see comment below) */
  154. mtmsrd(mfmsr() | MSR_PMM);
  155. for (i = 0; i < num_counters; ++i) {
  156. val = classic_ctr_read(i);
  157. if (val < 0) {
  158. if (ctr[i].enabled) {
  159. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  160. classic_ctr_write(i, reset_value[i]);
  161. } else {
  162. classic_ctr_write(i, 0);
  163. }
  164. }
  165. }
  166. mmcr0 = mfspr(SPRN_MMCR0);
  167. /* reset the perfmon trigger */
  168. mmcr0 |= MMCR0_PMXE;
  169. /*
  170. * now clear the freeze bit, counting will not start until we
  171. * rfid from this exception, because only at that point will
  172. * the PMM bit be cleared
  173. */
  174. mmcr0 &= ~MMCR0_FC;
  175. mtspr(SPRN_MMCR0, mmcr0);
  176. }
  177. struct op_powerpc_model op_model_rs64 = {
  178. .reg_setup = rs64_reg_setup,
  179. .cpu_setup = rs64_cpu_setup,
  180. .start = rs64_start,
  181. .stop = rs64_stop,
  182. .handle_interrupt = rs64_handle_interrupt,
  183. };