op_model_power4.c 7.5 KB

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