op_model_pa6t.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 int 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%llx\n",
  123. pmc, reset_value[pmc]);
  124. }
  125. return 0;
  126. }
  127. /* configure registers on this cpu */
  128. static int pa6t_cpu_setup(struct op_counter_config *ctr)
  129. {
  130. u64 mmcr0 = mmcr0_val;
  131. u64 mmcr1 = mmcr1_val;
  132. /* Default is all PMCs off */
  133. mmcr0 &= ~(0x3FUL);
  134. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  135. /* program selected programmable events in */
  136. mtspr(SPRN_PA6T_MMCR1, mmcr1);
  137. pr_debug("setup on cpu %d, mmcr0 %016lx\n", smp_processor_id(),
  138. mfspr(SPRN_PA6T_MMCR0));
  139. pr_debug("setup on cpu %d, mmcr1 %016lx\n", smp_processor_id(),
  140. mfspr(SPRN_PA6T_MMCR1));
  141. return 0;
  142. }
  143. static int pa6t_start(struct op_counter_config *ctr)
  144. {
  145. int i;
  146. /* Hold off event counting until rfid */
  147. u64 mmcr0 = mmcr0_val | PA6T_MMCR0_HANDDIS;
  148. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  149. if (ctr[i].enabled)
  150. ctr_write(i, reset_value[i]);
  151. else
  152. ctr_write(i, 0UL);
  153. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  154. oprofile_running = 1;
  155. pr_debug("start on cpu %d, mmcr0 %llx\n", smp_processor_id(), mmcr0);
  156. return 0;
  157. }
  158. static void pa6t_stop(void)
  159. {
  160. u64 mmcr0;
  161. /* freeze counters */
  162. mmcr0 = mfspr(SPRN_PA6T_MMCR0);
  163. mmcr0 |= PA6T_MMCR0_FCM0;
  164. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  165. oprofile_running = 0;
  166. pr_debug("stop on cpu %d, mmcr0 %llx\n", smp_processor_id(), mmcr0);
  167. }
  168. /* handle the perfmon overflow vector */
  169. static void pa6t_handle_interrupt(struct pt_regs *regs,
  170. struct op_counter_config *ctr)
  171. {
  172. unsigned long pc = mfspr(SPRN_PA6T_SIAR);
  173. int is_kernel = is_kernel_addr(pc);
  174. u64 val;
  175. int i;
  176. u64 mmcr0;
  177. /* disable perfmon counting until rfid */
  178. mmcr0 = mfspr(SPRN_PA6T_MMCR0);
  179. mtspr(SPRN_PA6T_MMCR0, mmcr0 | PA6T_MMCR0_HANDDIS);
  180. /* Record samples. We've got one global bit for whether a sample
  181. * was taken, so add it for any counter that triggered overflow.
  182. */
  183. for (i = 0; i < cur_cpu_spec->num_pmcs; i++) {
  184. val = ctr_read(i);
  185. if (val & (0x1UL << 39)) { /* Overflow bit set */
  186. if (oprofile_running && ctr[i].enabled) {
  187. if (mmcr0 & PA6T_MMCR0_SIARLOG)
  188. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  189. ctr_write(i, reset_value[i]);
  190. } else {
  191. ctr_write(i, 0UL);
  192. }
  193. }
  194. }
  195. /* Restore mmcr0 to a good known value since the PMI changes it */
  196. mmcr0 = mmcr0_val | PA6T_MMCR0_HANDDIS;
  197. mtspr(SPRN_PA6T_MMCR0, mmcr0);
  198. }
  199. struct op_powerpc_model op_model_pa6t = {
  200. .reg_setup = pa6t_reg_setup,
  201. .cpu_setup = pa6t_cpu_setup,
  202. .start = pa6t_start,
  203. .stop = pa6t_stop,
  204. .handle_interrupt = pa6t_handle_interrupt,
  205. };