op_model_power4.c 7.8 KB

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