op_model_power4.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/firmware.h>
  13. #include <asm/ptrace.h>
  14. #include <asm/system.h>
  15. #include <asm/processor.h>
  16. #include <asm/cputable.h>
  17. #include <asm/rtas.h>
  18. #include <asm/oprofile_impl.h>
  19. #include <asm/reg.h>
  20. #define dbg(args...)
  21. static unsigned long reset_value[OP_MAX_COUNTER];
  22. static int oprofile_running;
  23. /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
  24. static u32 mmcr0_val;
  25. static u64 mmcr1_val;
  26. static u64 mmcra_val;
  27. static void power4_reg_setup(struct op_counter_config *ctr,
  28. struct op_system_config *sys,
  29. int num_ctrs)
  30. {
  31. int i;
  32. /*
  33. * The performance counter event settings are given in the mmcr0,
  34. * mmcr1 and mmcra values passed from the user in the
  35. * op_system_config structure (sys variable).
  36. */
  37. mmcr0_val = sys->mmcr0;
  38. mmcr1_val = sys->mmcr1;
  39. mmcra_val = sys->mmcra;
  40. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
  41. reset_value[i] = 0x80000000UL - ctr[i].count;
  42. /* setup user and kernel profiling */
  43. if (sys->enable_kernel)
  44. mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
  45. else
  46. mmcr0_val |= MMCR0_KERNEL_DISABLE;
  47. if (sys->enable_user)
  48. mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
  49. else
  50. mmcr0_val |= MMCR0_PROBLEM_DISABLE;
  51. }
  52. extern void ppc64_enable_pmcs(void);
  53. /*
  54. * Older CPUs require the MMCRA sample bit to be always set, but newer
  55. * CPUs only want it set for some groups. Eventually we will remove all
  56. * knowledge of this bit in the kernel, oprofile userspace should be
  57. * setting it when required.
  58. *
  59. * In order to keep current installations working we force the bit for
  60. * those older CPUs. Once everyone has updated their oprofile userspace we
  61. * can remove this hack.
  62. */
  63. static inline int mmcra_must_set_sample(void)
  64. {
  65. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
  66. __is_processor(PV_970) || __is_processor(PV_970FX) ||
  67. __is_processor(PV_970MP))
  68. return 1;
  69. return 0;
  70. }
  71. static void power4_cpu_setup(void *unused)
  72. {
  73. unsigned int mmcr0 = mmcr0_val;
  74. unsigned long mmcra = mmcra_val;
  75. ppc64_enable_pmcs();
  76. /* set the freeze bit */
  77. mmcr0 |= MMCR0_FC;
  78. mtspr(SPRN_MMCR0, mmcr0);
  79. mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
  80. mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
  81. mtspr(SPRN_MMCR0, mmcr0);
  82. mtspr(SPRN_MMCR1, mmcr1_val);
  83. if (mmcra_must_set_sample())
  84. mmcra |= MMCRA_SAMPLE_ENABLE;
  85. mtspr(SPRN_MMCRA, mmcra);
  86. dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
  87. mfspr(SPRN_MMCR0));
  88. dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
  89. mfspr(SPRN_MMCR1));
  90. dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
  91. mfspr(SPRN_MMCRA));
  92. }
  93. static void power4_start(struct op_counter_config *ctr)
  94. {
  95. int i;
  96. unsigned int mmcr0;
  97. /* set the PMM bit (see comment below) */
  98. mtmsrd(mfmsr() | MSR_PMM);
  99. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  100. if (ctr[i].enabled) {
  101. ctr_write(i, reset_value[i]);
  102. } else {
  103. ctr_write(i, 0);
  104. }
  105. }
  106. mmcr0 = mfspr(SPRN_MMCR0);
  107. /*
  108. * We must clear the PMAO bit on some (GQ) chips. Just do it
  109. * all the time
  110. */
  111. mmcr0 &= ~MMCR0_PMAO;
  112. /*
  113. * now clear the freeze bit, counting will not start until we
  114. * rfid from this excetion, because only at that point will
  115. * the PMM bit be cleared
  116. */
  117. mmcr0 &= ~MMCR0_FC;
  118. mtspr(SPRN_MMCR0, mmcr0);
  119. oprofile_running = 1;
  120. dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  121. }
  122. static void power4_stop(void)
  123. {
  124. unsigned int mmcr0;
  125. /* freeze counters */
  126. mmcr0 = mfspr(SPRN_MMCR0);
  127. mmcr0 |= MMCR0_FC;
  128. mtspr(SPRN_MMCR0, mmcr0);
  129. oprofile_running = 0;
  130. dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  131. mb();
  132. }
  133. /* Fake functions used by canonicalize_pc */
  134. static void __attribute_used__ hypervisor_bucket(void)
  135. {
  136. }
  137. static void __attribute_used__ rtas_bucket(void)
  138. {
  139. }
  140. static void __attribute_used__ kernel_unknown_bucket(void)
  141. {
  142. }
  143. /*
  144. * On GQ and newer the MMCRA stores the HV and PR bits at the time
  145. * the SIAR was sampled. We use that to work out if the SIAR was sampled in
  146. * the hypervisor, our exception vectors or RTAS.
  147. */
  148. static unsigned long get_pc(struct pt_regs *regs)
  149. {
  150. unsigned long pc = mfspr(SPRN_SIAR);
  151. unsigned long mmcra;
  152. /* Cant do much about it */
  153. if (!cur_cpu_spec->oprofile_mmcra_sihv)
  154. return pc;
  155. mmcra = mfspr(SPRN_MMCRA);
  156. /* Were we in the hypervisor? */
  157. if (firmware_has_feature(FW_FEATURE_LPAR) &&
  158. (mmcra & cur_cpu_spec->oprofile_mmcra_sihv))
  159. /* function descriptor madness */
  160. return *((unsigned long *)hypervisor_bucket);
  161. /* We were in userspace, nothing to do */
  162. if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr)
  163. return pc;
  164. #ifdef CONFIG_PPC_RTAS
  165. /* Were we in RTAS? */
  166. if (pc >= rtas.base && pc < (rtas.base + rtas.size))
  167. /* function descriptor madness */
  168. return *((unsigned long *)rtas_bucket);
  169. #endif
  170. /* Were we in our exception vectors or SLB real mode miss handler? */
  171. if (pc < 0x1000000UL)
  172. return (unsigned long)__va(pc);
  173. /* Not sure where we were */
  174. if (!is_kernel_addr(pc))
  175. /* function descriptor madness */
  176. return *((unsigned long *)kernel_unknown_bucket);
  177. return pc;
  178. }
  179. static int get_kernel(unsigned long pc, unsigned long mmcra)
  180. {
  181. int is_kernel;
  182. if (!cur_cpu_spec->oprofile_mmcra_sihv) {
  183. is_kernel = is_kernel_addr(pc);
  184. } else {
  185. is_kernel = ((mmcra & cur_cpu_spec->oprofile_mmcra_sipr) == 0);
  186. }
  187. return is_kernel;
  188. }
  189. static void power4_handle_interrupt(struct pt_regs *regs,
  190. struct op_counter_config *ctr)
  191. {
  192. unsigned long pc;
  193. int is_kernel;
  194. int val;
  195. int i;
  196. unsigned int mmcr0;
  197. unsigned long mmcra;
  198. mmcra = mfspr(SPRN_MMCRA);
  199. pc = get_pc(regs);
  200. is_kernel = get_kernel(pc, mmcra);
  201. /* set the PMM bit (see comment below) */
  202. mtmsrd(mfmsr() | MSR_PMM);
  203. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  204. val = ctr_read(i);
  205. if (val < 0) {
  206. if (oprofile_running && ctr[i].enabled) {
  207. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  208. ctr_write(i, reset_value[i]);
  209. } else {
  210. ctr_write(i, 0);
  211. }
  212. }
  213. }
  214. mmcr0 = mfspr(SPRN_MMCR0);
  215. /* reset the perfmon trigger */
  216. mmcr0 |= MMCR0_PMXE;
  217. /*
  218. * We must clear the PMAO bit on some (GQ) chips. Just do it
  219. * all the time
  220. */
  221. mmcr0 &= ~MMCR0_PMAO;
  222. /* Clear the appropriate bits in the MMCRA */
  223. mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear;
  224. mtspr(SPRN_MMCRA, mmcra);
  225. /*
  226. * now clear the freeze bit, counting will not start until we
  227. * rfid from this exception, because only at that point will
  228. * the PMM bit be cleared
  229. */
  230. mmcr0 &= ~MMCR0_FC;
  231. mtspr(SPRN_MMCR0, mmcr0);
  232. }
  233. struct op_powerpc_model op_model_power4 = {
  234. .reg_setup = power4_reg_setup,
  235. .cpu_setup = power4_cpu_setup,
  236. .start = power4_start,
  237. .stop = power4_stop,
  238. .handle_interrupt = power4_handle_interrupt,
  239. };