op_model_rs64.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/system.h>
  14. #include <asm/processor.h>
  15. #include <asm/cputable.h>
  16. #include <asm/oprofile_impl.h>
  17. #define dbg(args...)
  18. static void ctrl_write(unsigned int i, unsigned int val)
  19. {
  20. unsigned int tmp = 0;
  21. unsigned long shift = 0, mask = 0;
  22. dbg("ctrl_write %d %x\n", i, val);
  23. switch(i) {
  24. case 0:
  25. tmp = mfspr(SPRN_MMCR0);
  26. shift = 6;
  27. mask = 0x7F;
  28. break;
  29. case 1:
  30. tmp = mfspr(SPRN_MMCR0);
  31. shift = 0;
  32. mask = 0x3F;
  33. break;
  34. case 2:
  35. tmp = mfspr(SPRN_MMCR1);
  36. shift = 31 - 4;
  37. mask = 0x1F;
  38. break;
  39. case 3:
  40. tmp = mfspr(SPRN_MMCR1);
  41. shift = 31 - 9;
  42. mask = 0x1F;
  43. break;
  44. case 4:
  45. tmp = mfspr(SPRN_MMCR1);
  46. shift = 31 - 14;
  47. mask = 0x1F;
  48. break;
  49. case 5:
  50. tmp = mfspr(SPRN_MMCR1);
  51. shift = 31 - 19;
  52. mask = 0x1F;
  53. break;
  54. case 6:
  55. tmp = mfspr(SPRN_MMCR1);
  56. shift = 31 - 24;
  57. mask = 0x1F;
  58. break;
  59. case 7:
  60. tmp = mfspr(SPRN_MMCR1);
  61. shift = 31 - 28;
  62. mask = 0xF;
  63. break;
  64. }
  65. tmp = tmp & ~(mask << shift);
  66. tmp |= val << shift;
  67. switch(i) {
  68. case 0:
  69. case 1:
  70. mtspr(SPRN_MMCR0, tmp);
  71. break;
  72. default:
  73. mtspr(SPRN_MMCR1, tmp);
  74. }
  75. dbg("ctrl_write mmcr0 %lx mmcr1 %lx\n", mfspr(SPRN_MMCR0),
  76. mfspr(SPRN_MMCR1));
  77. }
  78. static unsigned long reset_value[OP_MAX_COUNTER];
  79. static int num_counters;
  80. static void rs64_reg_setup(struct op_counter_config *ctr,
  81. struct op_system_config *sys,
  82. int num_ctrs)
  83. {
  84. int i;
  85. num_counters = num_ctrs;
  86. for (i = 0; i < num_counters; ++i)
  87. reset_value[i] = 0x80000000UL - ctr[i].count;
  88. /* XXX setup user and kernel profiling */
  89. }
  90. static void rs64_cpu_setup(void *unused)
  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. }
  109. static void 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. ctr_write(i, reset_value[i]);
  118. ctrl_write(i, ctr[i].event);
  119. } else {
  120. 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. }
  133. static void rs64_stop(void)
  134. {
  135. unsigned int mmcr0;
  136. /* freeze counters */
  137. mmcr0 = mfspr(SPRN_MMCR0);
  138. mmcr0 |= MMCR0_FC;
  139. mtspr(SPRN_MMCR0, mmcr0);
  140. dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  141. mb();
  142. }
  143. static void rs64_handle_interrupt(struct pt_regs *regs,
  144. struct op_counter_config *ctr)
  145. {
  146. unsigned int mmcr0;
  147. int is_kernel;
  148. int val;
  149. int i;
  150. unsigned long pc = mfspr(SPRN_SIAR);
  151. is_kernel = is_kernel_addr(pc);
  152. /* set the PMM bit (see comment below) */
  153. mtmsrd(mfmsr() | MSR_PMM);
  154. for (i = 0; i < num_counters; ++i) {
  155. val = ctr_read(i);
  156. if (val < 0) {
  157. if (ctr[i].enabled) {
  158. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  159. ctr_write(i, reset_value[i]);
  160. } else {
  161. ctr_write(i, 0);
  162. }
  163. }
  164. }
  165. mmcr0 = mfspr(SPRN_MMCR0);
  166. /* reset the perfmon trigger */
  167. mmcr0 |= MMCR0_PMXE;
  168. /*
  169. * now clear the freeze bit, counting will not start until we
  170. * rfid from this exception, because only at that point will
  171. * the PMM bit be cleared
  172. */
  173. mmcr0 &= ~MMCR0_FC;
  174. mtspr(SPRN_MMCR0, mmcr0);
  175. }
  176. struct op_powerpc_model op_model_rs64 = {
  177. .reg_setup = rs64_reg_setup,
  178. .cpu_setup = rs64_cpu_setup,
  179. .start = rs64_start,
  180. .stop = rs64_stop,
  181. .handle_interrupt = rs64_handle_interrupt,
  182. };