power7-pmu.c 11 KB

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