op_model_power4.c 11 KB

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