op_model_power4.c 7.8 KB

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