op_model_pa6t.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2006-2007 PA Semi, Inc
  3. *
  4. * Author: Shashi Rao, PA Semi
  5. *
  6. * Maintained by: Olof Johansson <olof@lixom.net>
  7. *
  8. * Based on arch/powerpc/oprofile/op_model_power4.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/oprofile.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/percpu.h>
  27. #include <asm/processor.h>
  28. #include <asm/cputable.h>
  29. #include <asm/oprofile_impl.h>
  30. #include <asm/reg.h>
  31. static unsigned char oprofile_running;
  32. /* mmcr values are set in pa6t_reg_setup, used in pa6t_cpu_setup */
  33. static u64 mmcr0_val;
  34. static u64 mmcr1_val;
  35. /* inited in pa6t_reg_setup */
  36. static u64 reset_value[OP_MAX_COUNTER];
  37. static inline u64 ctr_read(unsigned int i)
  38. {
  39. switch (i) {
  40. case 0:
  41. return mfspr(SPRN_PA6T_PMC0);
  42. case 1:
  43. return mfspr(SPRN_PA6T_PMC1);
  44. case 2:
  45. return mfspr(SPRN_PA6T_PMC2);
  46. case 3:
  47. return mfspr(SPRN_PA6T_PMC3);
  48. case 4:
  49. return mfspr(SPRN_PA6T_PMC4);
  50. case 5:
  51. return mfspr(SPRN_PA6T_PMC5);
  52. default:
  53. printk(KERN_ERR "ctr_read called with bad arg %u\n", i);
  54. return 0;
  55. }
  56. }
  57. static inline void ctr_write(unsigned int i, u64 val)
  58. {
  59. switch (i) {
  60. case 0:
  61. mtspr(SPRN_PA6T_PMC0, val);
  62. break;
  63. case 1:
  64. mtspr(SPRN_PA6T_PMC1, val);
  65. break;
  66. case 2:
  67. mtspr(SPRN_PA6T_PMC2, val);
  68. break;
  69. case 3:
  70. mtspr(SPRN_PA6T_PMC3, val);
  71. break;
  72. case 4:
  73. mtspr(SPRN_PA6T_PMC4, val);
  74. break;
  75. case 5:
  76. mtspr(SPRN_PA6T_PMC5, val);
  77. break;
  78. default:
  79. printk(KERN_ERR "ctr_write called with bad arg %u\n", i);
  80. break;
  81. }
  82. }
  83. /* precompute the values to stuff in the hardware registers */
  84. static void pa6t_reg_setup(struct op_counter_config *ctr,
  85. struct op_system_config *sys,
  86. int num_ctrs)
  87. {
  88. int pmc;
  89. /*
  90. * adjust the mmcr0.en[0-5] and mmcr0.inten[0-5] values obtained from the
  91. * event_mappings file by turning off the counters that the user doesn't
  92. * care about
  93. *
  94. * setup user and kernel profiling
  95. */
  96. for (pmc = 0; pmc < cur_cpu_spec->num_pmcs; pmc++)
  97. if (!ctr[pmc].enabled) {
  98. sys->mmcr0 &= ~(0x1UL << pmc);
  99. sys->mmcr0 &= ~(0x1UL << (pmc+12));
  100. pr_debug("turned off counter %u\n", pmc);
  101. }
  102. if (sys->enable_kernel)
  103. sys->mmcr0 |= PA6T_MMCR0_SUPEN | PA6T_MMCR0_HYPEN;
  104. else
  105. sys->mmcr0 &= ~(PA6T_MMCR0_SUPEN | PA6T_MMCR0_HYPEN);
  106. if (sys->enable_user)
  107. sys->mmcr0 |= PA6T_MMCR0_PREN;
  108. else
  109. sys->mmcr0 &= ~PA6T_MMCR0_PREN;
  110. /*
  111. * The performance counter event settings are given in the mmcr0 and
  112. * mmcr1 values passed from the user in the op_system_config
  113. * structure (sys variable).
  114. */
  115. mmcr0_val = sys->mmcr0;
  116. mmcr1_val = sys->mmcr1;
  117. pr_debug("mmcr0_val inited to %016lx\n", sys->mmcr0);
  118. pr_debug("mmcr1_val inited to %016lx\n", sys->mmcr1);
  119. for (pmc = 0; pmc < cur_cpu_spec->num_pmcs; pmc++) {
  120. /* counters are 40 bit. Move to cputable at some point? */
  121. reset_value[pmc] = (0x1UL << 39) - ctr[pmc].count;
  122. pr_debug("reset_value for pmc%u inited to 0x%lx\n",
  123. pmc, reset_value[pmc]);
  124. }
  125. }
  126. /* configure registers on this cpu */
  127. static void pa6t_cpu_setup(struct op_counter_config *ctr)
  128. {
  129. u64 mmcr0 = mmcr0_val;
  130. u64 mmcr1 = mmcr1_val;
  131. /* Default is all PMCs off */
  132. mmcr0 &= ~(0x3FUL);
  133. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  134. /* program selected programmable events in */
  135. mtspr(SPRN_PA6T_MMCR1, mmcr1);
  136. pr_debug("setup on cpu %d, mmcr0 %016lx\n", smp_processor_id(),
  137. mfspr(SPRN_PA6T_MMCR0));
  138. pr_debug("setup on cpu %d, mmcr1 %016lx\n", smp_processor_id(),
  139. mfspr(SPRN_PA6T_MMCR1));
  140. }
  141. static void pa6t_start(struct op_counter_config *ctr)
  142. {
  143. int i;
  144. /* Hold off event counting until rfid */
  145. u64 mmcr0 = mmcr0_val | PA6T_MMCR0_HANDDIS;
  146. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  147. if (ctr[i].enabled)
  148. ctr_write(i, reset_value[i]);
  149. else
  150. ctr_write(i, 0UL);
  151. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  152. oprofile_running = 1;
  153. pr_debug("start on cpu %d, mmcr0 %lx\n", smp_processor_id(), mmcr0);
  154. }
  155. static void pa6t_stop(void)
  156. {
  157. u64 mmcr0;
  158. /* freeze counters */
  159. mmcr0 = mfspr(SPRN_PA6T_MMCR0);
  160. mmcr0 |= PA6T_MMCR0_FCM0;
  161. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  162. oprofile_running = 0;
  163. pr_debug("stop on cpu %d, mmcr0 %lx\n", smp_processor_id(), mmcr0);
  164. }
  165. /* handle the perfmon overflow vector */
  166. static void pa6t_handle_interrupt(struct pt_regs *regs,
  167. struct op_counter_config *ctr)
  168. {
  169. unsigned long pc = mfspr(SPRN_PA6T_SIAR);
  170. int is_kernel = is_kernel_addr(pc);
  171. u64 val;
  172. int i;
  173. u64 mmcr0;
  174. /* disable perfmon counting until rfid */
  175. mmcr0 = mfspr(SPRN_PA6T_MMCR0);
  176. mtspr(SPRN_PA6T_MMCR0, mmcr0 | PA6T_MMCR0_HANDDIS);
  177. /* Record samples. We've got one global bit for whether a sample
  178. * was taken, so add it for any counter that triggered overflow.
  179. */
  180. for (i = 0; i < cur_cpu_spec->num_pmcs; i++) {
  181. val = ctr_read(i);
  182. if (val & (0x1UL << 39)) { /* Overflow bit set */
  183. if (oprofile_running && ctr[i].enabled) {
  184. if (mmcr0 & PA6T_MMCR0_SIARLOG)
  185. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  186. ctr_write(i, reset_value[i]);
  187. } else {
  188. ctr_write(i, 0UL);
  189. }
  190. }
  191. }
  192. /* Restore mmcr0 to a good known value since the PMI changes it */
  193. mmcr0 = mmcr0_val | PA6T_MMCR0_HANDDIS;
  194. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  195. }
  196. struct op_powerpc_model op_model_pa6t = {
  197. .reg_setup = pa6t_reg_setup,
  198. .cpu_setup = pa6t_cpu_setup,
  199. .start = pa6t_start,
  200. .stop = pa6t_stop,
  201. .handle_interrupt = pa6t_handle_interrupt,
  202. };