op_model_power4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #define OPROFILE_PM_PMCSEL_MSK 0xffULL
  23. #define OPROFILE_PM_UNIT_SHIFT 60
  24. #define OPROFILE_PM_UNIT_MSK 0xfULL
  25. #define OPROFILE_MAX_PMC_NUM 3
  26. #define OPROFILE_PMSEL_FIELD_WIDTH 8
  27. #define OPROFILE_UNIT_FIELD_WIDTH 4
  28. #define MMCRA_SIAR_VALID_MASK 0x10000000ULL
  29. static unsigned long reset_value[OP_MAX_COUNTER];
  30. static int oprofile_running;
  31. static int use_slot_nums;
  32. /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
  33. static u32 mmcr0_val;
  34. static u64 mmcr1_val;
  35. static u64 mmcra_val;
  36. static u32 cntr_marked_events;
  37. static int power7_marked_instr_event(u64 mmcr1)
  38. {
  39. u64 psel, unit;
  40. int pmc, cntr_marked_events = 0;
  41. /* Given the MMCR1 value, look at the field for each counter to
  42. * determine if it is a marked event. Code based on the function
  43. * power7_marked_instr_event() in file arch/powerpc/perf/power7-pmu.c.
  44. */
  45. for (pmc = 0; pmc < 4; pmc++) {
  46. psel = mmcr1 & (OPROFILE_PM_PMCSEL_MSK
  47. << (OPROFILE_MAX_PMC_NUM - pmc)
  48. * OPROFILE_PMSEL_FIELD_WIDTH);
  49. psel = (psel >> ((OPROFILE_MAX_PMC_NUM - pmc)
  50. * OPROFILE_PMSEL_FIELD_WIDTH)) & ~1ULL;
  51. unit = mmcr1 & (OPROFILE_PM_UNIT_MSK
  52. << (OPROFILE_PM_UNIT_SHIFT
  53. - (pmc * OPROFILE_PMSEL_FIELD_WIDTH )));
  54. unit = unit >> (OPROFILE_PM_UNIT_SHIFT
  55. - (pmc * OPROFILE_PMSEL_FIELD_WIDTH));
  56. switch (psel >> 4) {
  57. case 2:
  58. cntr_marked_events |= (pmc == 1 || pmc == 3) << pmc;
  59. break;
  60. case 3:
  61. if (psel == 0x3c) {
  62. cntr_marked_events |= (pmc == 0) << pmc;
  63. break;
  64. }
  65. if (psel == 0x3e) {
  66. cntr_marked_events |= (pmc != 1) << pmc;
  67. break;
  68. }
  69. cntr_marked_events |= 1 << pmc;
  70. break;
  71. case 4:
  72. case 5:
  73. cntr_marked_events |= (unit == 0xd) << pmc;
  74. break;
  75. case 6:
  76. if (psel == 0x64)
  77. cntr_marked_events |= (pmc >= 2) << pmc;
  78. break;
  79. case 8:
  80. cntr_marked_events |= (unit == 0xd) << pmc;
  81. break;
  82. }
  83. }
  84. return cntr_marked_events;
  85. }
  86. static int power4_reg_setup(struct op_counter_config *ctr,
  87. struct op_system_config *sys,
  88. int num_ctrs)
  89. {
  90. int i;
  91. /*
  92. * The performance counter event settings are given in the mmcr0,
  93. * mmcr1 and mmcra values passed from the user in the
  94. * op_system_config structure (sys variable).
  95. */
  96. mmcr0_val = sys->mmcr0;
  97. mmcr1_val = sys->mmcr1;
  98. mmcra_val = sys->mmcra;
  99. /* Power 7+ and newer architectures:
  100. * Determine which counter events in the group (the group of events is
  101. * specified by the bit settings in the MMCR1 register) are marked
  102. * events for use in the interrupt handler. Do the calculation once
  103. * before OProfile starts. Information is used in the interrupt
  104. * handler. Starting with Power 7+ we only record the sample for
  105. * marked events if the SIAR valid bit is set. For non marked events
  106. * the sample is always recorded.
  107. */
  108. if (pvr_version_is(PVR_POWER7p))
  109. cntr_marked_events = power7_marked_instr_event(mmcr1_val);
  110. else
  111. cntr_marked_events = 0; /* For older processors, set the bit map
  112. * to zero so the sample will always be
  113. * be recorded.
  114. */
  115. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
  116. reset_value[i] = 0x80000000UL - ctr[i].count;
  117. /* setup user and kernel profiling */
  118. if (sys->enable_kernel)
  119. mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
  120. else
  121. mmcr0_val |= MMCR0_KERNEL_DISABLE;
  122. if (sys->enable_user)
  123. mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
  124. else
  125. mmcr0_val |= MMCR0_PROBLEM_DISABLE;
  126. if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p) ||
  127. pvr_version_is(PVR_970) || pvr_version_is(PVR_970FX) ||
  128. pvr_version_is(PVR_970MP) || pvr_version_is(PVR_970GX) ||
  129. pvr_version_is(PVR_POWER5) || pvr_version_is(PVR_POWER5p))
  130. use_slot_nums = 1;
  131. return 0;
  132. }
  133. extern void ppc_enable_pmcs(void);
  134. /*
  135. * Older CPUs require the MMCRA sample bit to be always set, but newer
  136. * CPUs only want it set for some groups. Eventually we will remove all
  137. * knowledge of this bit in the kernel, oprofile userspace should be
  138. * setting it when required.
  139. *
  140. * In order to keep current installations working we force the bit for
  141. * those older CPUs. Once everyone has updated their oprofile userspace we
  142. * can remove this hack.
  143. */
  144. static inline int mmcra_must_set_sample(void)
  145. {
  146. if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p) ||
  147. pvr_version_is(PVR_970) || pvr_version_is(PVR_970FX) ||
  148. pvr_version_is(PVR_970MP) || pvr_version_is(PVR_970GX))
  149. return 1;
  150. return 0;
  151. }
  152. static int power4_cpu_setup(struct op_counter_config *ctr)
  153. {
  154. unsigned int mmcr0 = mmcr0_val;
  155. unsigned long mmcra = mmcra_val;
  156. ppc_enable_pmcs();
  157. /* set the freeze bit */
  158. mmcr0 |= MMCR0_FC;
  159. mtspr(SPRN_MMCR0, mmcr0);
  160. mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
  161. mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
  162. mtspr(SPRN_MMCR0, mmcr0);
  163. mtspr(SPRN_MMCR1, mmcr1_val);
  164. if (mmcra_must_set_sample())
  165. mmcra |= MMCRA_SAMPLE_ENABLE;
  166. mtspr(SPRN_MMCRA, mmcra);
  167. dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
  168. mfspr(SPRN_MMCR0));
  169. dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
  170. mfspr(SPRN_MMCR1));
  171. dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
  172. mfspr(SPRN_MMCRA));
  173. return 0;
  174. }
  175. static int power4_start(struct op_counter_config *ctr)
  176. {
  177. int i;
  178. unsigned int mmcr0;
  179. /* set the PMM bit (see comment below) */
  180. mtmsrd(mfmsr() | MSR_PMM);
  181. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  182. if (ctr[i].enabled) {
  183. classic_ctr_write(i, reset_value[i]);
  184. } else {
  185. classic_ctr_write(i, 0);
  186. }
  187. }
  188. mmcr0 = mfspr(SPRN_MMCR0);
  189. /*
  190. * We must clear the PMAO bit on some (GQ) chips. Just do it
  191. * all the time
  192. */
  193. mmcr0 &= ~MMCR0_PMAO;
  194. /*
  195. * now clear the freeze bit, counting will not start until we
  196. * rfid from this excetion, because only at that point will
  197. * the PMM bit be cleared
  198. */
  199. mmcr0 &= ~MMCR0_FC;
  200. mtspr(SPRN_MMCR0, mmcr0);
  201. oprofile_running = 1;
  202. dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  203. return 0;
  204. }
  205. static void power4_stop(void)
  206. {
  207. unsigned int mmcr0;
  208. /* freeze counters */
  209. mmcr0 = mfspr(SPRN_MMCR0);
  210. mmcr0 |= MMCR0_FC;
  211. mtspr(SPRN_MMCR0, mmcr0);
  212. oprofile_running = 0;
  213. dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
  214. mb();
  215. }
  216. /* Fake functions used by canonicalize_pc */
  217. static void __used hypervisor_bucket(void)
  218. {
  219. }
  220. static void __used rtas_bucket(void)
  221. {
  222. }
  223. static void __used kernel_unknown_bucket(void)
  224. {
  225. }
  226. /*
  227. * On GQ and newer the MMCRA stores the HV and PR bits at the time
  228. * the SIAR was sampled. We use that to work out if the SIAR was sampled in
  229. * the hypervisor, our exception vectors or RTAS.
  230. * If the MMCRA_SAMPLE_ENABLE bit is set, we can use the MMCRA[slot] bits
  231. * to more accurately identify the address of the sampled instruction. The
  232. * mmcra[slot] bits represent the slot number of a sampled instruction
  233. * within an instruction group. The slot will contain a value between 1
  234. * and 5 if MMCRA_SAMPLE_ENABLE is set, otherwise 0.
  235. */
  236. static unsigned long get_pc(struct pt_regs *regs)
  237. {
  238. unsigned long pc = mfspr(SPRN_SIAR);
  239. unsigned long mmcra;
  240. unsigned long slot;
  241. /* Can't do much about it */
  242. if (!cur_cpu_spec->oprofile_mmcra_sihv)
  243. return pc;
  244. mmcra = mfspr(SPRN_MMCRA);
  245. if (use_slot_nums && (mmcra & MMCRA_SAMPLE_ENABLE)) {
  246. slot = ((mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT);
  247. if (slot > 1)
  248. pc += 4 * (slot - 1);
  249. }
  250. /* Were we in the hypervisor? */
  251. if (firmware_has_feature(FW_FEATURE_LPAR) &&
  252. (mmcra & cur_cpu_spec->oprofile_mmcra_sihv))
  253. /* function descriptor madness */
  254. return *((unsigned long *)hypervisor_bucket);
  255. /* We were in userspace, nothing to do */
  256. if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr)
  257. return pc;
  258. #ifdef CONFIG_PPC_RTAS
  259. /* Were we in RTAS? */
  260. if (pc >= rtas.base && pc < (rtas.base + rtas.size))
  261. /* function descriptor madness */
  262. return *((unsigned long *)rtas_bucket);
  263. #endif
  264. /* Were we in our exception vectors or SLB real mode miss handler? */
  265. if (pc < 0x1000000UL)
  266. return (unsigned long)__va(pc);
  267. /* Not sure where we were */
  268. if (!is_kernel_addr(pc))
  269. /* function descriptor madness */
  270. return *((unsigned long *)kernel_unknown_bucket);
  271. return pc;
  272. }
  273. static int get_kernel(unsigned long pc, unsigned long mmcra)
  274. {
  275. int is_kernel;
  276. if (!cur_cpu_spec->oprofile_mmcra_sihv) {
  277. is_kernel = is_kernel_addr(pc);
  278. } else {
  279. is_kernel = ((mmcra & cur_cpu_spec->oprofile_mmcra_sipr) == 0);
  280. }
  281. return is_kernel;
  282. }
  283. static bool pmc_overflow(unsigned long val)
  284. {
  285. if ((int)val < 0)
  286. return true;
  287. /*
  288. * Events on POWER7 can roll back if a speculative event doesn't
  289. * eventually complete. Unfortunately in some rare cases they will
  290. * raise a performance monitor exception. We need to catch this to
  291. * ensure we reset the PMC. In all cases the PMC will be 256 or less
  292. * cycles from overflow.
  293. *
  294. * We only do this if the first pass fails to find any overflowing
  295. * PMCs because a user might set a period of less than 256 and we
  296. * don't want to mistakenly reset them.
  297. */
  298. if (pvr_version_is(PVR_POWER7) && ((0x80000000 - val) <= 256))
  299. return true;
  300. return false;
  301. }
  302. static void power4_handle_interrupt(struct pt_regs *regs,
  303. struct op_counter_config *ctr)
  304. {
  305. unsigned long pc;
  306. int is_kernel;
  307. int val;
  308. int i;
  309. unsigned int mmcr0;
  310. unsigned long mmcra;
  311. bool siar_valid = false;
  312. mmcra = mfspr(SPRN_MMCRA);
  313. pc = get_pc(regs);
  314. is_kernel = get_kernel(pc, mmcra);
  315. /* set the PMM bit (see comment below) */
  316. mtmsrd(mfmsr() | MSR_PMM);
  317. /* Check that the SIAR valid bit in MMCRA is set to 1. */
  318. if ((mmcra & MMCRA_SIAR_VALID_MASK) == MMCRA_SIAR_VALID_MASK)
  319. siar_valid = true;
  320. for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
  321. val = classic_ctr_read(i);
  322. if (pmc_overflow(val)) {
  323. if (oprofile_running && ctr[i].enabled) {
  324. /* Power 7+ and newer architectures:
  325. * If the event is a marked event, then only
  326. * save the sample if the SIAR valid bit is
  327. * set. If the event is not marked, then
  328. * always save the sample.
  329. * Note, the Sample enable bit in the MMCRA
  330. * register must be set to 1 if the group
  331. * contains a marked event.
  332. */
  333. if ((siar_valid &&
  334. (cntr_marked_events & (1 << i)))
  335. || !(cntr_marked_events & (1 << i)))
  336. oprofile_add_ext_sample(pc, regs, i,
  337. is_kernel);
  338. classic_ctr_write(i, reset_value[i]);
  339. } else {
  340. classic_ctr_write(i, 0);
  341. }
  342. }
  343. }
  344. mmcr0 = mfspr(SPRN_MMCR0);
  345. /* reset the perfmon trigger */
  346. mmcr0 |= MMCR0_PMXE;
  347. /*
  348. * We must clear the PMAO bit on some (GQ) chips. Just do it
  349. * all the time
  350. */
  351. mmcr0 &= ~MMCR0_PMAO;
  352. /* Clear the appropriate bits in the MMCRA */
  353. mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear;
  354. mtspr(SPRN_MMCRA, mmcra);
  355. /*
  356. * now clear the freeze bit, counting will not start until we
  357. * rfid from this exception, because only at that point will
  358. * the PMM bit be cleared
  359. */
  360. mmcr0 &= ~MMCR0_FC;
  361. mtspr(SPRN_MMCR0, mmcr0);
  362. }
  363. struct op_powerpc_model op_model_power4 = {
  364. .reg_setup = power4_reg_setup,
  365. .cpu_setup = power4_cpu_setup,
  366. .start = power4_start,
  367. .stop = power4_stop,
  368. .handle_interrupt = power4_handle_interrupt,
  369. };