op_model_power4.c 7.1 KB

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