power7-pmu.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Performance counter support for POWER7 processors.
  3. *
  4. * Copyright 2009 Paul Mackerras, IBM Corporation.
  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/kernel.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/string.h>
  14. #include <asm/reg.h>
  15. #include <asm/cputable.h>
  16. /*
  17. * Bits in event code for POWER7
  18. */
  19. #define PM_PMC_SH 16 /* PMC number (1-based) for direct events */
  20. #define PM_PMC_MSK 0xf
  21. #define PM_PMC_MSKS (PM_PMC_MSK << PM_PMC_SH)
  22. #define PM_UNIT_SH 12 /* TTMMUX number and setting - unit select */
  23. #define PM_UNIT_MSK 0xf
  24. #define PM_COMBINE_SH 11 /* Combined event bit */
  25. #define PM_COMBINE_MSK 1
  26. #define PM_COMBINE_MSKS 0x800
  27. #define PM_L2SEL_SH 8 /* L2 event select */
  28. #define PM_L2SEL_MSK 7
  29. #define PM_PMCSEL_MSK 0xff
  30. /*
  31. * Bits in MMCR1 for POWER7
  32. */
  33. #define MMCR1_TTM0SEL_SH 60
  34. #define MMCR1_TTM1SEL_SH 56
  35. #define MMCR1_TTM2SEL_SH 52
  36. #define MMCR1_TTM3SEL_SH 48
  37. #define MMCR1_TTMSEL_MSK 0xf
  38. #define MMCR1_L2SEL_SH 45
  39. #define MMCR1_L2SEL_MSK 7
  40. #define MMCR1_PMC1_COMBINE_SH 35
  41. #define MMCR1_PMC2_COMBINE_SH 34
  42. #define MMCR1_PMC3_COMBINE_SH 33
  43. #define MMCR1_PMC4_COMBINE_SH 32
  44. #define MMCR1_PMC1SEL_SH 24
  45. #define MMCR1_PMC2SEL_SH 16
  46. #define MMCR1_PMC3SEL_SH 8
  47. #define MMCR1_PMC4SEL_SH 0
  48. #define MMCR1_PMCSEL_SH(n) (MMCR1_PMC1SEL_SH - (n) * 8)
  49. #define MMCR1_PMCSEL_MSK 0xff
  50. /*
  51. * Layout of constraint bits:
  52. * 6666555555555544444444443333333333222222222211111111110000000000
  53. * 3210987654321098765432109876543210987654321098765432109876543210
  54. * < >< ><><><><><><>
  55. * L2 NC P6P5P4P3P2P1
  56. *
  57. * L2 - 16-18 - Required L2SEL value (select field)
  58. *
  59. * NC - number of counters
  60. * 15: NC error 0x8000
  61. * 12-14: number of events needing PMC1-4 0x7000
  62. *
  63. * P6
  64. * 11: P6 error 0x800
  65. * 10-11: Count of events needing PMC6
  66. *
  67. * P1..P5
  68. * 0-9: Count of events needing PMC1..PMC5
  69. */
  70. static int power7_get_constraint(u64 event, unsigned long *maskp,
  71. unsigned long *valp)
  72. {
  73. int pmc, sh, unit;
  74. unsigned long mask = 0, value = 0;
  75. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  76. if (pmc) {
  77. if (pmc > 6)
  78. return -1;
  79. sh = (pmc - 1) * 2;
  80. mask |= 2 << sh;
  81. value |= 1 << sh;
  82. if (pmc >= 5 && !(event == 0x500fa || event == 0x600f4))
  83. return -1;
  84. }
  85. if (pmc < 5) {
  86. /* need a counter from PMC1-4 set */
  87. mask |= 0x8000;
  88. value |= 0x1000;
  89. }
  90. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  91. if (unit == 6) {
  92. /* L2SEL must be identical across events */
  93. int l2sel = (event >> PM_L2SEL_SH) & PM_L2SEL_MSK;
  94. mask |= 0x7 << 16;
  95. value |= l2sel << 16;
  96. }
  97. *maskp = mask;
  98. *valp = value;
  99. return 0;
  100. }
  101. #define MAX_ALT 2 /* at most 2 alternatives for any event */
  102. static const unsigned int event_alternatives[][MAX_ALT] = {
  103. { 0x200f2, 0x300f2 }, /* PM_INST_DISP */
  104. { 0x200f4, 0x600f4 }, /* PM_RUN_CYC */
  105. { 0x400fa, 0x500fa }, /* PM_RUN_INST_CMPL */
  106. };
  107. /*
  108. * Scan the alternatives table for a match and return the
  109. * index into the alternatives table if found, else -1.
  110. */
  111. static int find_alternative(u64 event)
  112. {
  113. int i, j;
  114. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  115. if (event < event_alternatives[i][0])
  116. break;
  117. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  118. if (event == event_alternatives[i][j])
  119. return i;
  120. }
  121. return -1;
  122. }
  123. static s64 find_alternative_decode(u64 event)
  124. {
  125. int pmc, psel;
  126. /* this only handles the 4x decode events */
  127. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  128. psel = event & PM_PMCSEL_MSK;
  129. if ((pmc == 2 || pmc == 4) && (psel & ~7) == 0x40)
  130. return event - (1 << PM_PMC_SH) + 8;
  131. if ((pmc == 1 || pmc == 3) && (psel & ~7) == 0x48)
  132. return event + (1 << PM_PMC_SH) - 8;
  133. return -1;
  134. }
  135. static int power7_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  136. {
  137. int i, j, nalt = 1;
  138. s64 ae;
  139. alt[0] = event;
  140. nalt = 1;
  141. i = find_alternative(event);
  142. if (i >= 0) {
  143. for (j = 0; j < MAX_ALT; ++j) {
  144. ae = event_alternatives[i][j];
  145. if (ae && ae != event)
  146. alt[nalt++] = ae;
  147. }
  148. } else {
  149. ae = find_alternative_decode(event);
  150. if (ae > 0)
  151. alt[nalt++] = ae;
  152. }
  153. if (flags & PPMU_ONLY_COUNT_RUN) {
  154. /*
  155. * We're only counting in RUN state,
  156. * so PM_CYC is equivalent to PM_RUN_CYC
  157. * and PM_INST_CMPL === PM_RUN_INST_CMPL.
  158. * This doesn't include alternatives that don't provide
  159. * any extra flexibility in assigning PMCs.
  160. */
  161. j = nalt;
  162. for (i = 0; i < nalt; ++i) {
  163. switch (alt[i]) {
  164. case 0x1e: /* PM_CYC */
  165. alt[j++] = 0x600f4; /* PM_RUN_CYC */
  166. break;
  167. case 0x600f4: /* PM_RUN_CYC */
  168. alt[j++] = 0x1e;
  169. break;
  170. case 0x2: /* PM_PPC_CMPL */
  171. alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
  172. break;
  173. case 0x500fa: /* PM_RUN_INST_CMPL */
  174. alt[j++] = 0x2; /* PM_PPC_CMPL */
  175. break;
  176. }
  177. }
  178. nalt = j;
  179. }
  180. return nalt;
  181. }
  182. /*
  183. * Returns 1 if event counts things relating to marked instructions
  184. * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
  185. */
  186. static int power7_marked_instr_event(u64 event)
  187. {
  188. int pmc, psel;
  189. int unit;
  190. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  191. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  192. psel = event & PM_PMCSEL_MSK & ~1; /* trim off edge/level bit */
  193. if (pmc >= 5)
  194. return 0;
  195. switch (psel >> 4) {
  196. case 2:
  197. return pmc == 2 || pmc == 4;
  198. case 3:
  199. if (psel == 0x3c)
  200. return pmc == 1;
  201. if (psel == 0x3e)
  202. return pmc != 2;
  203. return 1;
  204. case 4:
  205. case 5:
  206. return unit == 0xd;
  207. case 6:
  208. if (psel == 0x64)
  209. return pmc >= 3;
  210. case 8:
  211. return unit == 0xd;
  212. }
  213. return 0;
  214. }
  215. static int power7_compute_mmcr(u64 event[], int n_ev,
  216. unsigned int hwc[], unsigned long mmcr[])
  217. {
  218. unsigned long mmcr1 = 0;
  219. unsigned long mmcra = MMCRA_SDAR_DCACHE_MISS | MMCRA_SDAR_ERAT_MISS;
  220. unsigned int pmc, unit, combine, l2sel, psel;
  221. unsigned int pmc_inuse = 0;
  222. int i;
  223. /* First pass to count resource use */
  224. for (i = 0; i < n_ev; ++i) {
  225. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  226. if (pmc) {
  227. if (pmc > 6)
  228. return -1;
  229. if (pmc_inuse & (1 << (pmc - 1)))
  230. return -1;
  231. pmc_inuse |= 1 << (pmc - 1);
  232. }
  233. }
  234. /* Second pass: assign PMCs, set all MMCR1 fields */
  235. for (i = 0; i < n_ev; ++i) {
  236. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  237. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  238. combine = (event[i] >> PM_COMBINE_SH) & PM_COMBINE_MSK;
  239. l2sel = (event[i] >> PM_L2SEL_SH) & PM_L2SEL_MSK;
  240. psel = event[i] & PM_PMCSEL_MSK;
  241. if (!pmc) {
  242. /* Bus event or any-PMC direct event */
  243. for (pmc = 0; pmc < 4; ++pmc) {
  244. if (!(pmc_inuse & (1 << pmc)))
  245. break;
  246. }
  247. if (pmc >= 4)
  248. return -1;
  249. pmc_inuse |= 1 << pmc;
  250. } else {
  251. /* Direct or decoded event */
  252. --pmc;
  253. }
  254. if (pmc <= 3) {
  255. mmcr1 |= (unsigned long) unit
  256. << (MMCR1_TTM0SEL_SH - 4 * pmc);
  257. mmcr1 |= (unsigned long) combine
  258. << (MMCR1_PMC1_COMBINE_SH - pmc);
  259. mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
  260. if (unit == 6) /* L2 events */
  261. mmcr1 |= (unsigned long) l2sel
  262. << MMCR1_L2SEL_SH;
  263. }
  264. if (power7_marked_instr_event(event[i]))
  265. mmcra |= MMCRA_SAMPLE_ENABLE;
  266. hwc[i] = pmc;
  267. }
  268. /* Return MMCRx values */
  269. mmcr[0] = 0;
  270. if (pmc_inuse & 1)
  271. mmcr[0] = MMCR0_PMC1CE;
  272. if (pmc_inuse & 0x3e)
  273. mmcr[0] |= MMCR0_PMCjCE;
  274. mmcr[1] = mmcr1;
  275. mmcr[2] = mmcra;
  276. return 0;
  277. }
  278. static void power7_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  279. {
  280. if (pmc <= 3)
  281. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SH(pmc));
  282. }
  283. static int power7_generic_events[] = {
  284. [PERF_COUNT_HW_CPU_CYCLES] = 0x1e,
  285. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x100f8, /* GCT_NOSLOT_CYC */
  286. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x4000a, /* CMPLU_STALL */
  287. [PERF_COUNT_HW_INSTRUCTIONS] = 2,
  288. [PERF_COUNT_HW_CACHE_REFERENCES] = 0xc880, /* LD_REF_L1_LSU*/
  289. [PERF_COUNT_HW_CACHE_MISSES] = 0x400f0, /* LD_MISS_L1 */
  290. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x10068, /* BRU_FIN */
  291. [PERF_COUNT_HW_BRANCH_MISSES] = 0x400f6, /* BR_MPRED */
  292. };
  293. #define C(x) PERF_COUNT_HW_CACHE_##x
  294. /*
  295. * Table of generalized cache-related events.
  296. * 0 means not supported, -1 means nonsensical, other values
  297. * are event codes.
  298. */
  299. static int power7_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  300. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  301. [C(OP_READ)] = { 0xc880, 0x400f0 },
  302. [C(OP_WRITE)] = { 0, 0x300f0 },
  303. [C(OP_PREFETCH)] = { 0xd8b8, 0 },
  304. },
  305. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  306. [C(OP_READ)] = { 0, 0x200fc },
  307. [C(OP_WRITE)] = { -1, -1 },
  308. [C(OP_PREFETCH)] = { 0x408a, 0 },
  309. },
  310. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  311. [C(OP_READ)] = { 0x16080, 0x26080 },
  312. [C(OP_WRITE)] = { 0x16082, 0x26082 },
  313. [C(OP_PREFETCH)] = { 0, 0 },
  314. },
  315. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  316. [C(OP_READ)] = { 0, 0x300fc },
  317. [C(OP_WRITE)] = { -1, -1 },
  318. [C(OP_PREFETCH)] = { -1, -1 },
  319. },
  320. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  321. [C(OP_READ)] = { 0, 0x400fc },
  322. [C(OP_WRITE)] = { -1, -1 },
  323. [C(OP_PREFETCH)] = { -1, -1 },
  324. },
  325. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  326. [C(OP_READ)] = { 0x10068, 0x400f6 },
  327. [C(OP_WRITE)] = { -1, -1 },
  328. [C(OP_PREFETCH)] = { -1, -1 },
  329. },
  330. [C(NODE)] = { /* RESULT_ACCESS RESULT_MISS */
  331. [C(OP_READ)] = { -1, -1 },
  332. [C(OP_WRITE)] = { -1, -1 },
  333. [C(OP_PREFETCH)] = { -1, -1 },
  334. },
  335. };
  336. static struct power_pmu power7_pmu = {
  337. .name = "POWER7",
  338. .n_counter = 6,
  339. .max_alternatives = MAX_ALT + 1,
  340. .add_fields = 0x1555ul,
  341. .test_adder = 0x3000ul,
  342. .compute_mmcr = power7_compute_mmcr,
  343. .get_constraint = power7_get_constraint,
  344. .get_alternatives = power7_get_alternatives,
  345. .disable_pmc = power7_disable_pmc,
  346. .flags = PPMU_ALT_SIPR,
  347. .n_generic = ARRAY_SIZE(power7_generic_events),
  348. .generic_events = power7_generic_events,
  349. .cache_events = &power7_cache_events,
  350. };
  351. static int __init init_power7_pmu(void)
  352. {
  353. if (!cur_cpu_spec->oprofile_cpu_type ||
  354. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power7"))
  355. return -ENODEV;
  356. if (pvr_version_is(PVR_POWER7p))
  357. power7_pmu.flags |= PPMU_SIAR_VALID;
  358. return register_power_pmu(&power7_pmu);
  359. }
  360. early_initcall(init_power7_pmu);