op_model_power4.c 8.5 KB

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