op_model_power4.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
  3. * Added mmcra[slot] support:
  4. * Copyright (C) 2006-2007 Will Schmidt <willschm@us.ibm.com>, IBM
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/oprofile.h>
  12. #include <linux/init.h>
  13. #include <linux/smp.h>
  14. #include <asm/firmware.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/system.h>
  17. #include <asm/processor.h>
  18. #include <asm/cputable.h>
  19. #include <asm/rtas.h>
  20. #include <asm/oprofile_impl.h>
  21. #include <asm/reg.h>
  22. #define dbg(args...)
  23. static unsigned long reset_value[OP_MAX_COUNTER];
  24. static int oprofile_running;
  25. /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
  26. static u32 mmcr0_val;
  27. static u64 mmcr1_val;
  28. static u64 mmcra_val;
  29. static void power4_reg_setup(struct op_counter_config *ctr,
  30. struct op_system_config *sys,
  31. int num_ctrs)
  32. {
  33. int i;
  34. /*
  35. * The performance counter event settings are given in the mmcr0,
  36. * mmcr1 and mmcra values passed from the user in the
  37. * op_system_config structure (sys variable).
  38. */
  39. mmcr0_val = sys->mmcr0;
  40. mmcr1_val = sys->mmcr1;
  41. mmcra_val = sys->mmcra;
  42. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
  43. reset_value[i] = 0x80000000UL - ctr[i].count;
  44. /* setup user and kernel profiling */
  45. if (sys->enable_kernel)
  46. mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
  47. else
  48. mmcr0_val |= MMCR0_KERNEL_DISABLE;
  49. if (sys->enable_user)
  50. mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
  51. else
  52. mmcr0_val |= MMCR0_PROBLEM_DISABLE;
  53. }
  54. extern void ppc64_enable_pmcs(void);
  55. /*
  56. * Older CPUs require the MMCRA sample bit to be always set, but newer
  57. * CPUs only want it set for some groups. Eventually we will remove all
  58. * knowledge of this bit in the kernel, oprofile userspace should be
  59. * setting it when required.
  60. *
  61. * In order to keep current installations working we force the bit for
  62. * those older CPUs. Once everyone has updated their oprofile userspace we
  63. * can remove this hack.
  64. */
  65. static inline int mmcra_must_set_sample(void)
  66. {
  67. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
  68. __is_processor(PV_970) || __is_processor(PV_970FX) ||
  69. __is_processor(PV_970MP) || __is_processor(PV_970GX))
  70. return 1;
  71. return 0;
  72. }
  73. static void power4_cpu_setup(struct op_counter_config *ctr)
  74. {
  75. unsigned int mmcr0 = mmcr0_val;
  76. unsigned long mmcra = mmcra_val;
  77. ppc64_enable_pmcs();
  78. /* set the freeze bit */
  79. mmcr0 |= MMCR0_FC;
  80. mtspr(SPRN_MMCR0, mmcr0);
  81. mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
  82. mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
  83. mtspr(SPRN_MMCR0, mmcr0);
  84. mtspr(SPRN_MMCR1, mmcr1_val);
  85. if (mmcra_must_set_sample())
  86. mmcra |= MMCRA_SAMPLE_ENABLE;
  87. mtspr(SPRN_MMCRA, mmcra);
  88. dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
  89. mfspr(SPRN_MMCR0));
  90. dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
  91. mfspr(SPRN_MMCR1));
  92. dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
  93. mfspr(SPRN_MMCRA));
  94. }
  95. static void power4_start(struct op_counter_config *ctr)
  96. {
  97. int i;
  98. unsigned int mmcr0;
  99. /* set the PMM bit (see comment below) */
  100. mtmsrd(mfmsr() | MSR_PMM);
  101. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  102. if (ctr[i].enabled) {
  103. classic_ctr_write(i, reset_value[i]);
  104. } else {
  105. classic_ctr_write(i, 0);
  106. }
  107. }
  108. mmcr0 = mfspr(SPRN_MMCR0);
  109. /*
  110. * We must clear the PMAO bit on some (GQ) chips. Just do it
  111. * all the time
  112. */
  113. mmcr0 &= ~MMCR0_PMAO;
  114. /*
  115. * now clear the freeze bit, counting will not start until we
  116. * rfid from this excetion, because only at that point will
  117. * the PMM bit be cleared
  118. */
  119. mmcr0 &= ~MMCR0_FC;
  120. mtspr(SPRN_MMCR0, mmcr0);
  121. oprofile_running = 1;
  122. dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  123. }
  124. static void power4_stop(void)
  125. {
  126. unsigned int mmcr0;
  127. /* freeze counters */
  128. mmcr0 = mfspr(SPRN_MMCR0);
  129. mmcr0 |= MMCR0_FC;
  130. mtspr(SPRN_MMCR0, mmcr0);
  131. oprofile_running = 0;
  132. dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  133. mb();
  134. }
  135. /* Fake functions used by canonicalize_pc */
  136. static void __attribute_used__ hypervisor_bucket(void)
  137. {
  138. }
  139. static void __attribute_used__ rtas_bucket(void)
  140. {
  141. }
  142. static void __attribute_used__ kernel_unknown_bucket(void)
  143. {
  144. }
  145. /*
  146. * On GQ and newer the MMCRA stores the HV and PR bits at the time
  147. * the SIAR was sampled. We use that to work out if the SIAR was sampled in
  148. * the hypervisor, our exception vectors or RTAS.
  149. * If the MMCRA_SAMPLE_ENABLE bit is set, we can use the MMCRA[slot] bits
  150. * to more accurately identify the address of the sampled instruction. The
  151. * mmcra[slot] bits represent the slot number of a sampled instruction
  152. * within an instruction group. The slot will contain a value between 1
  153. * and 5 if MMCRA_SAMPLE_ENABLE is set, otherwise 0.
  154. */
  155. static unsigned long get_pc(struct pt_regs *regs)
  156. {
  157. unsigned long pc = mfspr(SPRN_SIAR);
  158. unsigned long mmcra;
  159. unsigned long slot;
  160. /* Cant do much about it */
  161. if (!cur_cpu_spec->oprofile_mmcra_sihv)
  162. return pc;
  163. mmcra = mfspr(SPRN_MMCRA);
  164. if (mmcra & MMCRA_SAMPLE_ENABLE) {
  165. slot = ((mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT);
  166. if (slot > 1)
  167. pc += 4 * (slot - 1);
  168. }
  169. /* Were we in the hypervisor? */
  170. if (firmware_has_feature(FW_FEATURE_LPAR) &&
  171. (mmcra & cur_cpu_spec->oprofile_mmcra_sihv))
  172. /* function descriptor madness */
  173. return *((unsigned long *)hypervisor_bucket);
  174. /* We were in userspace, nothing to do */
  175. if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr)
  176. return pc;
  177. #ifdef CONFIG_PPC_RTAS
  178. /* Were we in RTAS? */
  179. if (pc >= rtas.base && pc < (rtas.base + rtas.size))
  180. /* function descriptor madness */
  181. return *((unsigned long *)rtas_bucket);
  182. #endif
  183. /* Were we in our exception vectors or SLB real mode miss handler? */
  184. if (pc < 0x1000000UL)
  185. return (unsigned long)__va(pc);
  186. /* Not sure where we were */
  187. if (!is_kernel_addr(pc))
  188. /* function descriptor madness */
  189. return *((unsigned long *)kernel_unknown_bucket);
  190. return pc;
  191. }
  192. static int get_kernel(unsigned long pc, unsigned long mmcra)
  193. {
  194. int is_kernel;
  195. if (!cur_cpu_spec->oprofile_mmcra_sihv) {
  196. is_kernel = is_kernel_addr(pc);
  197. } else {
  198. is_kernel = ((mmcra & cur_cpu_spec->oprofile_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. unsigned long mmcra;
  211. mmcra = mfspr(SPRN_MMCRA);
  212. pc = get_pc(regs);
  213. is_kernel = get_kernel(pc, mmcra);
  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 = classic_ctr_read(i);
  218. if (val < 0) {
  219. if (oprofile_running && ctr[i].enabled) {
  220. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  221. classic_ctr_write(i, reset_value[i]);
  222. } else {
  223. classic_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. /* Clear the appropriate bits in the MMCRA */
  236. mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear;
  237. mtspr(SPRN_MMCRA, mmcra);
  238. /*
  239. * now clear the freeze bit, counting will not start until we
  240. * rfid from this exception, because only at that point will
  241. * the PMM bit be cleared
  242. */
  243. mmcr0 &= ~MMCR0_FC;
  244. mtspr(SPRN_MMCR0, mmcr0);
  245. }
  246. struct op_powerpc_model op_model_power4 = {
  247. .reg_setup = power4_reg_setup,
  248. .cpu_setup = power4_cpu_setup,
  249. .start = power4_start,
  250. .stop = power4_stop,
  251. .handle_interrupt = power4_handle_interrupt,
  252. };