power7-pmu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. * Power7 event codes.
  52. */
  53. #define PME_PM_CYC 0x1e
  54. #define PME_PM_GCT_NOSLOT_CYC 0x100f8
  55. #define PME_PM_CMPLU_STALL 0x4000a
  56. #define PME_PM_INST_CMPL 0x2
  57. #define PME_PM_LD_REF_L1 0xc880
  58. #define PME_PM_LD_MISS_L1 0x400f0
  59. #define PME_PM_BRU_FIN 0x10068
  60. #define PME_PM_BRU_MPRED 0x400f6
  61. /*
  62. * Layout of constraint bits:
  63. * 6666555555555544444444443333333333222222222211111111110000000000
  64. * 3210987654321098765432109876543210987654321098765432109876543210
  65. * < >< ><><><><><><>
  66. * L2 NC P6P5P4P3P2P1
  67. *
  68. * L2 - 16-18 - Required L2SEL value (select field)
  69. *
  70. * NC - number of counters
  71. * 15: NC error 0x8000
  72. * 12-14: number of events needing PMC1-4 0x7000
  73. *
  74. * P6
  75. * 11: P6 error 0x800
  76. * 10-11: Count of events needing PMC6
  77. *
  78. * P1..P5
  79. * 0-9: Count of events needing PMC1..PMC5
  80. */
  81. static int power7_get_constraint(u64 event, unsigned long *maskp,
  82. unsigned long *valp)
  83. {
  84. int pmc, sh, unit;
  85. unsigned long mask = 0, value = 0;
  86. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  87. if (pmc) {
  88. if (pmc > 6)
  89. return -1;
  90. sh = (pmc - 1) * 2;
  91. mask |= 2 << sh;
  92. value |= 1 << sh;
  93. if (pmc >= 5 && !(event == 0x500fa || event == 0x600f4))
  94. return -1;
  95. }
  96. if (pmc < 5) {
  97. /* need a counter from PMC1-4 set */
  98. mask |= 0x8000;
  99. value |= 0x1000;
  100. }
  101. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  102. if (unit == 6) {
  103. /* L2SEL must be identical across events */
  104. int l2sel = (event >> PM_L2SEL_SH) & PM_L2SEL_MSK;
  105. mask |= 0x7 << 16;
  106. value |= l2sel << 16;
  107. }
  108. *maskp = mask;
  109. *valp = value;
  110. return 0;
  111. }
  112. #define MAX_ALT 2 /* at most 2 alternatives for any event */
  113. static const unsigned int event_alternatives[][MAX_ALT] = {
  114. { 0x200f2, 0x300f2 }, /* PM_INST_DISP */
  115. { 0x200f4, 0x600f4 }, /* PM_RUN_CYC */
  116. { 0x400fa, 0x500fa }, /* PM_RUN_INST_CMPL */
  117. };
  118. /*
  119. * Scan the alternatives table for a match and return the
  120. * index into the alternatives table if found, else -1.
  121. */
  122. static int find_alternative(u64 event)
  123. {
  124. int i, j;
  125. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  126. if (event < event_alternatives[i][0])
  127. break;
  128. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  129. if (event == event_alternatives[i][j])
  130. return i;
  131. }
  132. return -1;
  133. }
  134. static s64 find_alternative_decode(u64 event)
  135. {
  136. int pmc, psel;
  137. /* this only handles the 4x decode events */
  138. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  139. psel = event & PM_PMCSEL_MSK;
  140. if ((pmc == 2 || pmc == 4) && (psel & ~7) == 0x40)
  141. return event - (1 << PM_PMC_SH) + 8;
  142. if ((pmc == 1 || pmc == 3) && (psel & ~7) == 0x48)
  143. return event + (1 << PM_PMC_SH) - 8;
  144. return -1;
  145. }
  146. static int power7_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  147. {
  148. int i, j, nalt = 1;
  149. s64 ae;
  150. alt[0] = event;
  151. nalt = 1;
  152. i = find_alternative(event);
  153. if (i >= 0) {
  154. for (j = 0; j < MAX_ALT; ++j) {
  155. ae = event_alternatives[i][j];
  156. if (ae && ae != event)
  157. alt[nalt++] = ae;
  158. }
  159. } else {
  160. ae = find_alternative_decode(event);
  161. if (ae > 0)
  162. alt[nalt++] = ae;
  163. }
  164. if (flags & PPMU_ONLY_COUNT_RUN) {
  165. /*
  166. * We're only counting in RUN state,
  167. * so PM_CYC is equivalent to PM_RUN_CYC
  168. * and PM_INST_CMPL === PM_RUN_INST_CMPL.
  169. * This doesn't include alternatives that don't provide
  170. * any extra flexibility in assigning PMCs.
  171. */
  172. j = nalt;
  173. for (i = 0; i < nalt; ++i) {
  174. switch (alt[i]) {
  175. case 0x1e: /* PM_CYC */
  176. alt[j++] = 0x600f4; /* PM_RUN_CYC */
  177. break;
  178. case 0x600f4: /* PM_RUN_CYC */
  179. alt[j++] = 0x1e;
  180. break;
  181. case 0x2: /* PM_PPC_CMPL */
  182. alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
  183. break;
  184. case 0x500fa: /* PM_RUN_INST_CMPL */
  185. alt[j++] = 0x2; /* PM_PPC_CMPL */
  186. break;
  187. }
  188. }
  189. nalt = j;
  190. }
  191. return nalt;
  192. }
  193. /*
  194. * Returns 1 if event counts things relating to marked instructions
  195. * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
  196. */
  197. static int power7_marked_instr_event(u64 event)
  198. {
  199. int pmc, psel;
  200. int unit;
  201. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  202. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  203. psel = event & PM_PMCSEL_MSK & ~1; /* trim off edge/level bit */
  204. if (pmc >= 5)
  205. return 0;
  206. switch (psel >> 4) {
  207. case 2:
  208. return pmc == 2 || pmc == 4;
  209. case 3:
  210. if (psel == 0x3c)
  211. return pmc == 1;
  212. if (psel == 0x3e)
  213. return pmc != 2;
  214. return 1;
  215. case 4:
  216. case 5:
  217. return unit == 0xd;
  218. case 6:
  219. if (psel == 0x64)
  220. return pmc >= 3;
  221. case 8:
  222. return unit == 0xd;
  223. }
  224. return 0;
  225. }
  226. static int power7_compute_mmcr(u64 event[], int n_ev,
  227. unsigned int hwc[], unsigned long mmcr[])
  228. {
  229. unsigned long mmcr1 = 0;
  230. unsigned long mmcra = MMCRA_SDAR_DCACHE_MISS | MMCRA_SDAR_ERAT_MISS;
  231. unsigned int pmc, unit, combine, l2sel, psel;
  232. unsigned int pmc_inuse = 0;
  233. int i;
  234. /* First pass to count resource use */
  235. for (i = 0; i < n_ev; ++i) {
  236. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  237. if (pmc) {
  238. if (pmc > 6)
  239. return -1;
  240. if (pmc_inuse & (1 << (pmc - 1)))
  241. return -1;
  242. pmc_inuse |= 1 << (pmc - 1);
  243. }
  244. }
  245. /* Second pass: assign PMCs, set all MMCR1 fields */
  246. for (i = 0; i < n_ev; ++i) {
  247. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  248. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  249. combine = (event[i] >> PM_COMBINE_SH) & PM_COMBINE_MSK;
  250. l2sel = (event[i] >> PM_L2SEL_SH) & PM_L2SEL_MSK;
  251. psel = event[i] & PM_PMCSEL_MSK;
  252. if (!pmc) {
  253. /* Bus event or any-PMC direct event */
  254. for (pmc = 0; pmc < 4; ++pmc) {
  255. if (!(pmc_inuse & (1 << pmc)))
  256. break;
  257. }
  258. if (pmc >= 4)
  259. return -1;
  260. pmc_inuse |= 1 << pmc;
  261. } else {
  262. /* Direct or decoded event */
  263. --pmc;
  264. }
  265. if (pmc <= 3) {
  266. mmcr1 |= (unsigned long) unit
  267. << (MMCR1_TTM0SEL_SH - 4 * pmc);
  268. mmcr1 |= (unsigned long) combine
  269. << (MMCR1_PMC1_COMBINE_SH - pmc);
  270. mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
  271. if (unit == 6) /* L2 events */
  272. mmcr1 |= (unsigned long) l2sel
  273. << MMCR1_L2SEL_SH;
  274. }
  275. if (power7_marked_instr_event(event[i]))
  276. mmcra |= MMCRA_SAMPLE_ENABLE;
  277. hwc[i] = pmc;
  278. }
  279. /* Return MMCRx values */
  280. mmcr[0] = 0;
  281. if (pmc_inuse & 1)
  282. mmcr[0] = MMCR0_PMC1CE;
  283. if (pmc_inuse & 0x3e)
  284. mmcr[0] |= MMCR0_PMCjCE;
  285. mmcr[1] = mmcr1;
  286. mmcr[2] = mmcra;
  287. return 0;
  288. }
  289. static void power7_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  290. {
  291. if (pmc <= 3)
  292. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SH(pmc));
  293. }
  294. static int power7_generic_events[] = {
  295. [PERF_COUNT_HW_CPU_CYCLES] = PME_PM_CYC,
  296. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PME_PM_GCT_NOSLOT_CYC,
  297. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PME_PM_CMPLU_STALL,
  298. [PERF_COUNT_HW_INSTRUCTIONS] = PME_PM_INST_CMPL,
  299. [PERF_COUNT_HW_CACHE_REFERENCES] = PME_PM_LD_REF_L1,
  300. [PERF_COUNT_HW_CACHE_MISSES] = PME_PM_LD_MISS_L1,
  301. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PME_PM_BRU_FIN,
  302. [PERF_COUNT_HW_BRANCH_MISSES] = PME_PM_BRU_MPRED,
  303. };
  304. #define C(x) PERF_COUNT_HW_CACHE_##x
  305. /*
  306. * Table of generalized cache-related events.
  307. * 0 means not supported, -1 means nonsensical, other values
  308. * are event codes.
  309. */
  310. static int power7_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  311. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  312. [C(OP_READ)] = { 0xc880, 0x400f0 },
  313. [C(OP_WRITE)] = { 0, 0x300f0 },
  314. [C(OP_PREFETCH)] = { 0xd8b8, 0 },
  315. },
  316. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  317. [C(OP_READ)] = { 0, 0x200fc },
  318. [C(OP_WRITE)] = { -1, -1 },
  319. [C(OP_PREFETCH)] = { 0x408a, 0 },
  320. },
  321. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  322. [C(OP_READ)] = { 0x16080, 0x26080 },
  323. [C(OP_WRITE)] = { 0x16082, 0x26082 },
  324. [C(OP_PREFETCH)] = { 0, 0 },
  325. },
  326. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  327. [C(OP_READ)] = { 0, 0x300fc },
  328. [C(OP_WRITE)] = { -1, -1 },
  329. [C(OP_PREFETCH)] = { -1, -1 },
  330. },
  331. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  332. [C(OP_READ)] = { 0, 0x400fc },
  333. [C(OP_WRITE)] = { -1, -1 },
  334. [C(OP_PREFETCH)] = { -1, -1 },
  335. },
  336. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  337. [C(OP_READ)] = { 0x10068, 0x400f6 },
  338. [C(OP_WRITE)] = { -1, -1 },
  339. [C(OP_PREFETCH)] = { -1, -1 },
  340. },
  341. [C(NODE)] = { /* RESULT_ACCESS RESULT_MISS */
  342. [C(OP_READ)] = { -1, -1 },
  343. [C(OP_WRITE)] = { -1, -1 },
  344. [C(OP_PREFETCH)] = { -1, -1 },
  345. },
  346. };
  347. GENERIC_EVENT_ATTR(cpu-cycles, CYC);
  348. GENERIC_EVENT_ATTR(stalled-cycles-frontend, GCT_NOSLOT_CYC);
  349. GENERIC_EVENT_ATTR(stalled-cycles-backend, CMPLU_STALL);
  350. GENERIC_EVENT_ATTR(instructions, INST_CMPL);
  351. GENERIC_EVENT_ATTR(cache-references, LD_REF_L1);
  352. GENERIC_EVENT_ATTR(cache-misses, LD_MISS_L1);
  353. GENERIC_EVENT_ATTR(branch-instructions, BRU_FIN);
  354. GENERIC_EVENT_ATTR(branch-misses, BRU_MPRED);
  355. POWER_EVENT_ATTR(CYC, CYC);
  356. POWER_EVENT_ATTR(GCT_NOSLOT_CYC, GCT_NOSLOT_CYC);
  357. POWER_EVENT_ATTR(CMPLU_STALL, CMPLU_STALL);
  358. POWER_EVENT_ATTR(INST_CMPL, INST_CMPL);
  359. POWER_EVENT_ATTR(LD_REF_L1, LD_REF_L1);
  360. POWER_EVENT_ATTR(LD_MISS_L1, LD_MISS_L1);
  361. POWER_EVENT_ATTR(BRU_FIN, BRU_FIN)
  362. POWER_EVENT_ATTR(BRU_MPRED, BRU_MPRED);
  363. static struct attribute *power7_events_attr[] = {
  364. GENERIC_EVENT_PTR(CYC),
  365. GENERIC_EVENT_PTR(GCT_NOSLOT_CYC),
  366. GENERIC_EVENT_PTR(CMPLU_STALL),
  367. GENERIC_EVENT_PTR(INST_CMPL),
  368. GENERIC_EVENT_PTR(LD_REF_L1),
  369. GENERIC_EVENT_PTR(LD_MISS_L1),
  370. GENERIC_EVENT_PTR(BRU_FIN),
  371. GENERIC_EVENT_PTR(BRU_MPRED),
  372. POWER_EVENT_PTR(CYC),
  373. POWER_EVENT_PTR(GCT_NOSLOT_CYC),
  374. POWER_EVENT_PTR(CMPLU_STALL),
  375. POWER_EVENT_PTR(INST_CMPL),
  376. POWER_EVENT_PTR(LD_REF_L1),
  377. POWER_EVENT_PTR(LD_MISS_L1),
  378. POWER_EVENT_PTR(BRU_FIN),
  379. POWER_EVENT_PTR(BRU_MPRED),
  380. NULL
  381. };
  382. static struct attribute_group power7_pmu_events_group = {
  383. .name = "events",
  384. .attrs = power7_events_attr,
  385. };
  386. PMU_FORMAT_ATTR(event, "config:0-19");
  387. static struct attribute *power7_pmu_format_attr[] = {
  388. &format_attr_event.attr,
  389. NULL,
  390. };
  391. struct attribute_group power7_pmu_format_group = {
  392. .name = "format",
  393. .attrs = power7_pmu_format_attr,
  394. };
  395. static const struct attribute_group *power7_pmu_attr_groups[] = {
  396. &power7_pmu_format_group,
  397. &power7_pmu_events_group,
  398. NULL,
  399. };
  400. static struct power_pmu power7_pmu = {
  401. .name = "POWER7",
  402. .n_counter = 6,
  403. .max_alternatives = MAX_ALT + 1,
  404. .add_fields = 0x1555ul,
  405. .test_adder = 0x3000ul,
  406. .compute_mmcr = power7_compute_mmcr,
  407. .get_constraint = power7_get_constraint,
  408. .get_alternatives = power7_get_alternatives,
  409. .disable_pmc = power7_disable_pmc,
  410. .flags = PPMU_ALT_SIPR,
  411. .attr_groups = power7_pmu_attr_groups,
  412. .n_generic = ARRAY_SIZE(power7_generic_events),
  413. .generic_events = power7_generic_events,
  414. .cache_events = &power7_cache_events,
  415. };
  416. static int __init init_power7_pmu(void)
  417. {
  418. if (!cur_cpu_spec->oprofile_cpu_type ||
  419. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power7"))
  420. return -ENODEV;
  421. if (pvr_version_is(PVR_POWER7p))
  422. power7_pmu.flags |= PPMU_SIAR_VALID;
  423. return register_power_pmu(&power7_pmu);
  424. }
  425. early_initcall(init_power7_pmu);