op_model_rs64.c 4.4 KB

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