op_model_power4.c 7.1 KB

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