op_model_power4.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/rtas.h>
  17. #include <asm/oprofile_impl.h>
  18. #include <asm/reg.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. /*
  70. * Older CPUs require the MMCRA sample bit to be always set, but newer
  71. * CPUs only want it set for some groups. Eventually we will remove all
  72. * knowledge of this bit in the kernel, oprofile userspace should be
  73. * setting it when required.
  74. *
  75. * In order to keep current installations working we force the bit for
  76. * those older CPUs. Once everyone has updated their oprofile userspace we
  77. * can remove this hack.
  78. */
  79. static inline int mmcra_must_set_sample(void)
  80. {
  81. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
  82. __is_processor(PV_970) || __is_processor(PV_970FX) ||
  83. __is_processor(PV_970MP))
  84. return 1;
  85. return 0;
  86. }
  87. static void power4_cpu_setup(void *unused)
  88. {
  89. unsigned int mmcr0 = mmcr0_val;
  90. unsigned long mmcra = mmcra_val;
  91. ppc64_enable_pmcs();
  92. /* set the freeze bit */
  93. mmcr0 |= MMCR0_FC;
  94. mtspr(SPRN_MMCR0, mmcr0);
  95. mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
  96. mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
  97. mtspr(SPRN_MMCR0, mmcr0);
  98. mtspr(SPRN_MMCR1, mmcr1_val);
  99. if (mmcra_must_set_sample())
  100. mmcra |= MMCRA_SAMPLE_ENABLE;
  101. mtspr(SPRN_MMCRA, mmcra);
  102. dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
  103. mfspr(SPRN_MMCR0));
  104. dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
  105. mfspr(SPRN_MMCR1));
  106. dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
  107. mfspr(SPRN_MMCRA));
  108. }
  109. static void power4_start(struct op_counter_config *ctr)
  110. {
  111. int i;
  112. unsigned int mmcr0;
  113. /* set the PMM bit (see comment below) */
  114. mtmsrd(mfmsr() | MSR_PMM);
  115. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  116. if (ctr[i].enabled) {
  117. ctr_write(i, reset_value[i]);
  118. } else {
  119. ctr_write(i, 0);
  120. }
  121. }
  122. mmcr0 = mfspr(SPRN_MMCR0);
  123. /*
  124. * We must clear the PMAO bit on some (GQ) chips. Just do it
  125. * all the time
  126. */
  127. mmcr0 &= ~MMCR0_PMAO;
  128. /*
  129. * now clear the freeze bit, counting will not start until we
  130. * rfid from this excetion, because only at that point will
  131. * the PMM bit be cleared
  132. */
  133. mmcr0 &= ~MMCR0_FC;
  134. mtspr(SPRN_MMCR0, mmcr0);
  135. oprofile_running = 1;
  136. dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  137. }
  138. static void power4_stop(void)
  139. {
  140. unsigned int mmcr0;
  141. /* freeze counters */
  142. mmcr0 = mfspr(SPRN_MMCR0);
  143. mmcr0 |= MMCR0_FC;
  144. mtspr(SPRN_MMCR0, mmcr0);
  145. oprofile_running = 0;
  146. dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  147. mb();
  148. }
  149. /* Fake functions used by canonicalize_pc */
  150. static void __attribute_used__ hypervisor_bucket(void)
  151. {
  152. }
  153. static void __attribute_used__ rtas_bucket(void)
  154. {
  155. }
  156. static void __attribute_used__ kernel_unknown_bucket(void)
  157. {
  158. }
  159. static unsigned long check_spinlock_pc(struct pt_regs *regs,
  160. unsigned long profile_pc)
  161. {
  162. unsigned long pc = instruction_pointer(regs);
  163. /*
  164. * If both the SIAR (sampled instruction) and the perfmon exception
  165. * occurred in a spinlock region then we account the sample to the
  166. * calling function. This isnt 100% correct, we really need soft
  167. * IRQ disable so we always get the perfmon exception at the
  168. * point at which the SIAR is set.
  169. */
  170. if (backtrace_spinlocks && in_lock_functions(pc) &&
  171. in_lock_functions(profile_pc))
  172. return regs->link;
  173. else
  174. return profile_pc;
  175. }
  176. /*
  177. * On GQ and newer the MMCRA stores the HV and PR bits at the time
  178. * the SIAR was sampled. We use that to work out if the SIAR was sampled in
  179. * the hypervisor, our exception vectors or RTAS.
  180. */
  181. static unsigned long get_pc(struct pt_regs *regs)
  182. {
  183. unsigned long pc = mfspr(SPRN_SIAR);
  184. unsigned long mmcra;
  185. /* Cant do much about it */
  186. if (!mmcra_has_sihv)
  187. return check_spinlock_pc(regs, pc);
  188. mmcra = mfspr(SPRN_MMCRA);
  189. /* Were we in the hypervisor? */
  190. if (platform_is_lpar() && (mmcra & MMCRA_SIHV))
  191. /* function descriptor madness */
  192. return *((unsigned long *)hypervisor_bucket);
  193. /* We were in userspace, nothing to do */
  194. if (mmcra & MMCRA_SIPR)
  195. return pc;
  196. #ifdef CONFIG_PPC_RTAS
  197. /* Were we in RTAS? */
  198. if (pc >= rtas.base && pc < (rtas.base + rtas.size))
  199. /* function descriptor madness */
  200. return *((unsigned long *)rtas_bucket);
  201. #endif
  202. /* Were we in our exception vectors or SLB real mode miss handler? */
  203. if (pc < 0x1000000UL)
  204. return (unsigned long)__va(pc);
  205. /* Not sure where we were */
  206. if (!is_kernel_addr(pc))
  207. /* function descriptor madness */
  208. return *((unsigned long *)kernel_unknown_bucket);
  209. return check_spinlock_pc(regs, pc);
  210. }
  211. static int get_kernel(unsigned long pc)
  212. {
  213. int is_kernel;
  214. if (!mmcra_has_sihv) {
  215. is_kernel = is_kernel_addr(pc);
  216. } else {
  217. unsigned long mmcra = mfspr(SPRN_MMCRA);
  218. is_kernel = ((mmcra & MMCRA_SIPR) == 0);
  219. }
  220. return is_kernel;
  221. }
  222. static void power4_handle_interrupt(struct pt_regs *regs,
  223. struct op_counter_config *ctr)
  224. {
  225. unsigned long pc;
  226. int is_kernel;
  227. int val;
  228. int i;
  229. unsigned int mmcr0;
  230. pc = get_pc(regs);
  231. is_kernel = get_kernel(pc);
  232. /* set the PMM bit (see comment below) */
  233. mtmsrd(mfmsr() | MSR_PMM);
  234. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  235. val = ctr_read(i);
  236. if (val < 0) {
  237. if (oprofile_running && ctr[i].enabled) {
  238. oprofile_add_pc(pc, is_kernel, i);
  239. ctr_write(i, reset_value[i]);
  240. } else {
  241. ctr_write(i, 0);
  242. }
  243. }
  244. }
  245. mmcr0 = mfspr(SPRN_MMCR0);
  246. /* reset the perfmon trigger */
  247. mmcr0 |= MMCR0_PMXE;
  248. /*
  249. * We must clear the PMAO bit on some (GQ) chips. Just do it
  250. * all the time
  251. */
  252. mmcr0 &= ~MMCR0_PMAO;
  253. /*
  254. * now clear the freeze bit, counting will not start until we
  255. * rfid from this exception, because only at that point will
  256. * the PMM bit be cleared
  257. */
  258. mmcr0 &= ~MMCR0_FC;
  259. mtspr(SPRN_MMCR0, mmcr0);
  260. }
  261. struct op_powerpc_model op_model_power4 = {
  262. .reg_setup = power4_reg_setup,
  263. .cpu_setup = power4_cpu_setup,
  264. .start = power4_start,
  265. .stop = power4_stop,
  266. .handle_interrupt = power4_handle_interrupt,
  267. };