op_model_pa6t.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/smp.h>
  25. #include <linux/percpu.h>
  26. #include <asm/processor.h>
  27. #include <asm/cputable.h>
  28. #include <asm/oprofile_impl.h>
  29. #include <asm/reg.h>
  30. static unsigned char oprofile_running;
  31. /* mmcr values are set in pa6t_reg_setup, used in pa6t_cpu_setup */
  32. static u64 mmcr0_val;
  33. static u64 mmcr1_val;
  34. /* inited in pa6t_reg_setup */
  35. static u64 reset_value[OP_MAX_COUNTER];
  36. static inline u64 ctr_read(unsigned int i)
  37. {
  38. switch (i) {
  39. case 0:
  40. return mfspr(SPRN_PA6T_PMC0);
  41. case 1:
  42. return mfspr(SPRN_PA6T_PMC1);
  43. case 2:
  44. return mfspr(SPRN_PA6T_PMC2);
  45. case 3:
  46. return mfspr(SPRN_PA6T_PMC3);
  47. case 4:
  48. return mfspr(SPRN_PA6T_PMC4);
  49. case 5:
  50. return mfspr(SPRN_PA6T_PMC5);
  51. default:
  52. printk(KERN_ERR "ctr_read called with bad arg %u\n", i);
  53. return 0;
  54. }
  55. }
  56. static inline void ctr_write(unsigned int i, u64 val)
  57. {
  58. switch (i) {
  59. case 0:
  60. mtspr(SPRN_PA6T_PMC0, val);
  61. break;
  62. case 1:
  63. mtspr(SPRN_PA6T_PMC1, val);
  64. break;
  65. case 2:
  66. mtspr(SPRN_PA6T_PMC2, val);
  67. break;
  68. case 3:
  69. mtspr(SPRN_PA6T_PMC3, val);
  70. break;
  71. case 4:
  72. mtspr(SPRN_PA6T_PMC4, val);
  73. break;
  74. case 5:
  75. mtspr(SPRN_PA6T_PMC5, val);
  76. break;
  77. default:
  78. printk(KERN_ERR "ctr_write called with bad arg %u\n", i);
  79. break;
  80. }
  81. }
  82. /* precompute the values to stuff in the hardware registers */
  83. static int pa6t_reg_setup(struct op_counter_config *ctr,
  84. struct op_system_config *sys,
  85. int num_ctrs)
  86. {
  87. int pmc;
  88. /*
  89. * adjust the mmcr0.en[0-5] and mmcr0.inten[0-5] values obtained from the
  90. * event_mappings file by turning off the counters that the user doesn't
  91. * care about
  92. *
  93. * setup user and kernel profiling
  94. */
  95. for (pmc = 0; pmc < cur_cpu_spec->num_pmcs; pmc++)
  96. if (!ctr[pmc].enabled) {
  97. sys->mmcr0 &= ~(0x1UL << pmc);
  98. sys->mmcr0 &= ~(0x1UL << (pmc+12));
  99. pr_debug("turned off counter %u\n", pmc);
  100. }
  101. if (sys->enable_kernel)
  102. sys->mmcr0 |= PA6T_MMCR0_SUPEN | PA6T_MMCR0_HYPEN;
  103. else
  104. sys->mmcr0 &= ~(PA6T_MMCR0_SUPEN | PA6T_MMCR0_HYPEN);
  105. if (sys->enable_user)
  106. sys->mmcr0 |= PA6T_MMCR0_PREN;
  107. else
  108. sys->mmcr0 &= ~PA6T_MMCR0_PREN;
  109. /*
  110. * The performance counter event settings are given in the mmcr0 and
  111. * mmcr1 values passed from the user in the op_system_config
  112. * structure (sys variable).
  113. */
  114. mmcr0_val = sys->mmcr0;
  115. mmcr1_val = sys->mmcr1;
  116. pr_debug("mmcr0_val inited to %016lx\n", sys->mmcr0);
  117. pr_debug("mmcr1_val inited to %016lx\n", sys->mmcr1);
  118. for (pmc = 0; pmc < cur_cpu_spec->num_pmcs; pmc++) {
  119. /* counters are 40 bit. Move to cputable at some point? */
  120. reset_value[pmc] = (0x1UL << 39) - ctr[pmc].count;
  121. pr_debug("reset_value for pmc%u inited to 0x%llx\n",
  122. pmc, reset_value[pmc]);
  123. }
  124. return 0;
  125. }
  126. /* configure registers on this cpu */
  127. static int 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. return 0;
  141. }
  142. static int pa6t_start(struct op_counter_config *ctr)
  143. {
  144. int i;
  145. /* Hold off event counting until rfid */
  146. u64 mmcr0 = mmcr0_val | PA6T_MMCR0_HANDDIS;
  147. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  148. if (ctr[i].enabled)
  149. ctr_write(i, reset_value[i]);
  150. else
  151. ctr_write(i, 0UL);
  152. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  153. oprofile_running = 1;
  154. pr_debug("start on cpu %d, mmcr0 %llx\n", smp_processor_id(), mmcr0);
  155. return 0;
  156. }
  157. static void pa6t_stop(void)
  158. {
  159. u64 mmcr0;
  160. /* freeze counters */
  161. mmcr0 = mfspr(SPRN_PA6T_MMCR0);
  162. mmcr0 |= PA6T_MMCR0_FCM0;
  163. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  164. oprofile_running = 0;
  165. pr_debug("stop on cpu %d, mmcr0 %llx\n", smp_processor_id(), mmcr0);
  166. }
  167. /* handle the perfmon overflow vector */
  168. static void pa6t_handle_interrupt(struct pt_regs *regs,
  169. struct op_counter_config *ctr)
  170. {
  171. unsigned long pc = mfspr(SPRN_PA6T_SIAR);
  172. int is_kernel = is_kernel_addr(pc);
  173. u64 val;
  174. int i;
  175. u64 mmcr0;
  176. /* disable perfmon counting until rfid */
  177. mmcr0 = mfspr(SPRN_PA6T_MMCR0);
  178. mtspr(SPRN_PA6T_MMCR0, mmcr0 | PA6T_MMCR0_HANDDIS);
  179. /* Record samples. We've got one global bit for whether a sample
  180. * was taken, so add it for any counter that triggered overflow.
  181. */
  182. for (i = 0; i < cur_cpu_spec->num_pmcs; i++) {
  183. val = ctr_read(i);
  184. if (val & (0x1UL << 39)) { /* Overflow bit set */
  185. if (oprofile_running && ctr[i].enabled) {
  186. if (mmcr0 & PA6T_MMCR0_SIARLOG)
  187. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  188. ctr_write(i, reset_value[i]);
  189. } else {
  190. ctr_write(i, 0UL);
  191. }
  192. }
  193. }
  194. /* Restore mmcr0 to a good known value since the PMI changes it */
  195. mmcr0 = mmcr0_val | PA6T_MMCR0_HANDDIS;
  196. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  197. }
  198. struct op_powerpc_model op_model_pa6t = {
  199. .reg_setup = pa6t_reg_setup,
  200. .cpu_setup = pa6t_cpu_setup,
  201. .start = pa6t_start,
  202. .stop = pa6t_stop,
  203. .handle_interrupt = pa6t_handle_interrupt,
  204. };