power7-pmu.c 8.9 KB

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