ppc970-pmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Performance counter support for PPC970-family processors.
  3. *
  4. * Copyright 2008-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/string.h>
  12. #include <linux/perf_event.h>
  13. #include <asm/reg.h>
  14. #include <asm/cputable.h>
  15. /*
  16. * Bits in event code for PPC970
  17. */
  18. #define PM_PMC_SH 12 /* PMC number (1-based) for direct events */
  19. #define PM_PMC_MSK 0xf
  20. #define PM_UNIT_SH 8 /* TTMMUX number and setting - unit select */
  21. #define PM_UNIT_MSK 0xf
  22. #define PM_SPCSEL_SH 6
  23. #define PM_SPCSEL_MSK 3
  24. #define PM_BYTE_SH 4 /* Byte number of event bus to use */
  25. #define PM_BYTE_MSK 3
  26. #define PM_PMCSEL_MSK 0xf
  27. /* Values in PM_UNIT field */
  28. #define PM_NONE 0
  29. #define PM_FPU 1
  30. #define PM_VPU 2
  31. #define PM_ISU 3
  32. #define PM_IFU 4
  33. #define PM_IDU 5
  34. #define PM_STS 6
  35. #define PM_LSU0 7
  36. #define PM_LSU1U 8
  37. #define PM_LSU1L 9
  38. #define PM_LASTUNIT 9
  39. /*
  40. * Bits in MMCR0 for PPC970
  41. */
  42. #define MMCR0_PMC1SEL_SH 8
  43. #define MMCR0_PMC2SEL_SH 1
  44. #define MMCR_PMCSEL_MSK 0x1f
  45. /*
  46. * Bits in MMCR1 for PPC970
  47. */
  48. #define MMCR1_TTM0SEL_SH 62
  49. #define MMCR1_TTM1SEL_SH 59
  50. #define MMCR1_TTM3SEL_SH 53
  51. #define MMCR1_TTMSEL_MSK 3
  52. #define MMCR1_TD_CP_DBG0SEL_SH 50
  53. #define MMCR1_TD_CP_DBG1SEL_SH 48
  54. #define MMCR1_TD_CP_DBG2SEL_SH 46
  55. #define MMCR1_TD_CP_DBG3SEL_SH 44
  56. #define MMCR1_PMC1_ADDER_SEL_SH 39
  57. #define MMCR1_PMC2_ADDER_SEL_SH 38
  58. #define MMCR1_PMC6_ADDER_SEL_SH 37
  59. #define MMCR1_PMC5_ADDER_SEL_SH 36
  60. #define MMCR1_PMC8_ADDER_SEL_SH 35
  61. #define MMCR1_PMC7_ADDER_SEL_SH 34
  62. #define MMCR1_PMC3_ADDER_SEL_SH 33
  63. #define MMCR1_PMC4_ADDER_SEL_SH 32
  64. #define MMCR1_PMC3SEL_SH 27
  65. #define MMCR1_PMC4SEL_SH 22
  66. #define MMCR1_PMC5SEL_SH 17
  67. #define MMCR1_PMC6SEL_SH 12
  68. #define MMCR1_PMC7SEL_SH 7
  69. #define MMCR1_PMC8SEL_SH 2
  70. static short mmcr1_adder_bits[8] = {
  71. MMCR1_PMC1_ADDER_SEL_SH,
  72. MMCR1_PMC2_ADDER_SEL_SH,
  73. MMCR1_PMC3_ADDER_SEL_SH,
  74. MMCR1_PMC4_ADDER_SEL_SH,
  75. MMCR1_PMC5_ADDER_SEL_SH,
  76. MMCR1_PMC6_ADDER_SEL_SH,
  77. MMCR1_PMC7_ADDER_SEL_SH,
  78. MMCR1_PMC8_ADDER_SEL_SH
  79. };
  80. /*
  81. * Bits in MMCRA
  82. */
  83. /*
  84. * Layout of constraint bits:
  85. * 6666555555555544444444443333333333222222222211111111110000000000
  86. * 3210987654321098765432109876543210987654321098765432109876543210
  87. * <><><>[ >[ >[ >< >< >< >< ><><><><><><><><>
  88. * SPT0T1 UC PS1 PS2 B0 B1 B2 B3 P1P2P3P4P5P6P7P8
  89. *
  90. * SP - SPCSEL constraint
  91. * 48-49: SPCSEL value 0x3_0000_0000_0000
  92. *
  93. * T0 - TTM0 constraint
  94. * 46-47: TTM0SEL value (0=FPU, 2=IFU, 3=VPU) 0xC000_0000_0000
  95. *
  96. * T1 - TTM1 constraint
  97. * 44-45: TTM1SEL value (0=IDU, 3=STS) 0x3000_0000_0000
  98. *
  99. * UC - unit constraint: can't have all three of FPU|IFU|VPU, ISU, IDU|STS
  100. * 43: UC3 error 0x0800_0000_0000
  101. * 42: FPU|IFU|VPU events needed 0x0400_0000_0000
  102. * 41: ISU events needed 0x0200_0000_0000
  103. * 40: IDU|STS events needed 0x0100_0000_0000
  104. *
  105. * PS1
  106. * 39: PS1 error 0x0080_0000_0000
  107. * 36-38: count of events needing PMC1/2/5/6 0x0070_0000_0000
  108. *
  109. * PS2
  110. * 35: PS2 error 0x0008_0000_0000
  111. * 32-34: count of events needing PMC3/4/7/8 0x0007_0000_0000
  112. *
  113. * B0
  114. * 28-31: Byte 0 event source 0xf000_0000
  115. * Encoding as for the event code
  116. *
  117. * B1, B2, B3
  118. * 24-27, 20-23, 16-19: Byte 1, 2, 3 event sources
  119. *
  120. * P1
  121. * 15: P1 error 0x8000
  122. * 14-15: Count of events needing PMC1
  123. *
  124. * P2..P8
  125. * 0-13: Count of events needing PMC2..PMC8
  126. */
  127. static unsigned char direct_marked_event[8] = {
  128. (1<<2) | (1<<3), /* PMC1: PM_MRK_GRP_DISP, PM_MRK_ST_CMPL */
  129. (1<<3) | (1<<5), /* PMC2: PM_THRESH_TIMEO, PM_MRK_BRU_FIN */
  130. (1<<3) | (1<<5), /* PMC3: PM_MRK_ST_CMPL_INT, PM_MRK_VMX_FIN */
  131. (1<<4) | (1<<5), /* PMC4: PM_MRK_GRP_CMPL, PM_MRK_CRU_FIN */
  132. (1<<4) | (1<<5), /* PMC5: PM_GRP_MRK, PM_MRK_GRP_TIMEO */
  133. (1<<3) | (1<<4) | (1<<5),
  134. /* PMC6: PM_MRK_ST_STS, PM_MRK_FXU_FIN, PM_MRK_GRP_ISSUED */
  135. (1<<4) | (1<<5), /* PMC7: PM_MRK_FPU_FIN, PM_MRK_INST_FIN */
  136. (1<<4) /* PMC8: PM_MRK_LSU_FIN */
  137. };
  138. /*
  139. * Returns 1 if event counts things relating to marked instructions
  140. * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
  141. */
  142. static int p970_marked_instr_event(u64 event)
  143. {
  144. int pmc, psel, unit, byte, bit;
  145. unsigned int mask;
  146. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  147. psel = event & PM_PMCSEL_MSK;
  148. if (pmc) {
  149. if (direct_marked_event[pmc - 1] & (1 << psel))
  150. return 1;
  151. if (psel == 0) /* add events */
  152. bit = (pmc <= 4)? pmc - 1: 8 - pmc;
  153. else if (psel == 7 || psel == 13) /* decode events */
  154. bit = 4;
  155. else
  156. return 0;
  157. } else
  158. bit = psel;
  159. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  160. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  161. mask = 0;
  162. switch (unit) {
  163. case PM_VPU:
  164. mask = 0x4c; /* byte 0 bits 2,3,6 */
  165. case PM_LSU0:
  166. /* byte 2 bits 0,2,3,4,6; all of byte 1 */
  167. mask = 0x085dff00;
  168. case PM_LSU1L:
  169. mask = 0x50 << 24; /* byte 3 bits 4,6 */
  170. break;
  171. }
  172. return (mask >> (byte * 8 + bit)) & 1;
  173. }
  174. /* Masks and values for using events from the various units */
  175. static unsigned long unit_cons[PM_LASTUNIT+1][2] = {
  176. [PM_FPU] = { 0xc80000000000ull, 0x040000000000ull },
  177. [PM_VPU] = { 0xc80000000000ull, 0xc40000000000ull },
  178. [PM_ISU] = { 0x080000000000ull, 0x020000000000ull },
  179. [PM_IFU] = { 0xc80000000000ull, 0x840000000000ull },
  180. [PM_IDU] = { 0x380000000000ull, 0x010000000000ull },
  181. [PM_STS] = { 0x380000000000ull, 0x310000000000ull },
  182. };
  183. static int p970_get_constraint(u64 event, unsigned long *maskp,
  184. unsigned long *valp)
  185. {
  186. int pmc, byte, unit, sh, spcsel;
  187. unsigned long mask = 0, value = 0;
  188. int grp = -1;
  189. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  190. if (pmc) {
  191. if (pmc > 8)
  192. return -1;
  193. sh = (pmc - 1) * 2;
  194. mask |= 2 << sh;
  195. value |= 1 << sh;
  196. grp = ((pmc - 1) >> 1) & 1;
  197. }
  198. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  199. if (unit) {
  200. if (unit > PM_LASTUNIT)
  201. return -1;
  202. mask |= unit_cons[unit][0];
  203. value |= unit_cons[unit][1];
  204. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  205. /*
  206. * Bus events on bytes 0 and 2 can be counted
  207. * on PMC1/2/5/6; bytes 1 and 3 on PMC3/4/7/8.
  208. */
  209. if (!pmc)
  210. grp = byte & 1;
  211. /* Set byte lane select field */
  212. mask |= 0xfULL << (28 - 4 * byte);
  213. value |= (unsigned long)unit << (28 - 4 * byte);
  214. }
  215. if (grp == 0) {
  216. /* increment PMC1/2/5/6 field */
  217. mask |= 0x8000000000ull;
  218. value |= 0x1000000000ull;
  219. } else if (grp == 1) {
  220. /* increment PMC3/4/7/8 field */
  221. mask |= 0x800000000ull;
  222. value |= 0x100000000ull;
  223. }
  224. spcsel = (event >> PM_SPCSEL_SH) & PM_SPCSEL_MSK;
  225. if (spcsel) {
  226. mask |= 3ull << 48;
  227. value |= (unsigned long)spcsel << 48;
  228. }
  229. *maskp = mask;
  230. *valp = value;
  231. return 0;
  232. }
  233. static int p970_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  234. {
  235. alt[0] = event;
  236. /* 2 alternatives for LSU empty */
  237. if (event == 0x2002 || event == 0x3002) {
  238. alt[1] = event ^ 0x1000;
  239. return 2;
  240. }
  241. return 1;
  242. }
  243. static int p970_compute_mmcr(u64 event[], int n_ev,
  244. unsigned int hwc[], unsigned long mmcr[])
  245. {
  246. unsigned long mmcr0 = 0, mmcr1 = 0, mmcra = 0;
  247. unsigned int pmc, unit, byte, psel;
  248. unsigned int ttm, grp;
  249. unsigned int pmc_inuse = 0;
  250. unsigned int pmc_grp_use[2];
  251. unsigned char busbyte[4];
  252. unsigned char unituse[16];
  253. unsigned char unitmap[] = { 0, 0<<3, 3<<3, 1<<3, 2<<3, 0|4, 3|4 };
  254. unsigned char ttmuse[2];
  255. unsigned char pmcsel[8];
  256. int i;
  257. int spcsel;
  258. if (n_ev > 8)
  259. return -1;
  260. /* First pass to count resource use */
  261. pmc_grp_use[0] = pmc_grp_use[1] = 0;
  262. memset(busbyte, 0, sizeof(busbyte));
  263. memset(unituse, 0, sizeof(unituse));
  264. for (i = 0; i < n_ev; ++i) {
  265. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  266. if (pmc) {
  267. if (pmc_inuse & (1 << (pmc - 1)))
  268. return -1;
  269. pmc_inuse |= 1 << (pmc - 1);
  270. /* count 1/2/5/6 vs 3/4/7/8 use */
  271. ++pmc_grp_use[((pmc - 1) >> 1) & 1];
  272. }
  273. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  274. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  275. if (unit) {
  276. if (unit > PM_LASTUNIT)
  277. return -1;
  278. if (!pmc)
  279. ++pmc_grp_use[byte & 1];
  280. if (busbyte[byte] && busbyte[byte] != unit)
  281. return -1;
  282. busbyte[byte] = unit;
  283. unituse[unit] = 1;
  284. }
  285. }
  286. if (pmc_grp_use[0] > 4 || pmc_grp_use[1] > 4)
  287. return -1;
  288. /*
  289. * Assign resources and set multiplexer selects.
  290. *
  291. * PM_ISU can go either on TTM0 or TTM1, but that's the only
  292. * choice we have to deal with.
  293. */
  294. if (unituse[PM_ISU] &
  295. (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_VPU]))
  296. unitmap[PM_ISU] = 2 | 4; /* move ISU to TTM1 */
  297. /* Set TTM[01]SEL fields. */
  298. ttmuse[0] = ttmuse[1] = 0;
  299. for (i = PM_FPU; i <= PM_STS; ++i) {
  300. if (!unituse[i])
  301. continue;
  302. ttm = unitmap[i];
  303. ++ttmuse[(ttm >> 2) & 1];
  304. mmcr1 |= (unsigned long)(ttm & ~4) << MMCR1_TTM1SEL_SH;
  305. }
  306. /* Check only one unit per TTMx */
  307. if (ttmuse[0] > 1 || ttmuse[1] > 1)
  308. return -1;
  309. /* Set byte lane select fields and TTM3SEL. */
  310. for (byte = 0; byte < 4; ++byte) {
  311. unit = busbyte[byte];
  312. if (!unit)
  313. continue;
  314. if (unit <= PM_STS)
  315. ttm = (unitmap[unit] >> 2) & 1;
  316. else if (unit == PM_LSU0)
  317. ttm = 2;
  318. else {
  319. ttm = 3;
  320. if (unit == PM_LSU1L && byte >= 2)
  321. mmcr1 |= 1ull << (MMCR1_TTM3SEL_SH + 3 - byte);
  322. }
  323. mmcr1 |= (unsigned long)ttm
  324. << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
  325. }
  326. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  327. memset(pmcsel, 0x8, sizeof(pmcsel)); /* 8 means don't count */
  328. for (i = 0; i < n_ev; ++i) {
  329. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  330. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  331. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  332. psel = event[i] & PM_PMCSEL_MSK;
  333. if (!pmc) {
  334. /* Bus event or any-PMC direct event */
  335. if (unit)
  336. psel |= 0x10 | ((byte & 2) << 2);
  337. else
  338. psel |= 8;
  339. for (pmc = 0; pmc < 8; ++pmc) {
  340. if (pmc_inuse & (1 << pmc))
  341. continue;
  342. grp = (pmc >> 1) & 1;
  343. if (unit) {
  344. if (grp == (byte & 1))
  345. break;
  346. } else if (pmc_grp_use[grp] < 4) {
  347. ++pmc_grp_use[grp];
  348. break;
  349. }
  350. }
  351. pmc_inuse |= 1 << pmc;
  352. } else {
  353. /* Direct event */
  354. --pmc;
  355. if (psel == 0 && (byte & 2))
  356. /* add events on higher-numbered bus */
  357. mmcr1 |= 1ull << mmcr1_adder_bits[pmc];
  358. }
  359. pmcsel[pmc] = psel;
  360. hwc[i] = pmc;
  361. spcsel = (event[i] >> PM_SPCSEL_SH) & PM_SPCSEL_MSK;
  362. mmcr1 |= spcsel;
  363. if (p970_marked_instr_event(event[i]))
  364. mmcra |= MMCRA_SAMPLE_ENABLE;
  365. }
  366. for (pmc = 0; pmc < 2; ++pmc)
  367. mmcr0 |= pmcsel[pmc] << (MMCR0_PMC1SEL_SH - 7 * pmc);
  368. for (; pmc < 8; ++pmc)
  369. mmcr1 |= (unsigned long)pmcsel[pmc]
  370. << (MMCR1_PMC3SEL_SH - 5 * (pmc - 2));
  371. if (pmc_inuse & 1)
  372. mmcr0 |= MMCR0_PMC1CE;
  373. if (pmc_inuse & 0xfe)
  374. mmcr0 |= MMCR0_PMCjCE;
  375. mmcra |= 0x2000; /* mark only one IOP per PPC instruction */
  376. /* Return MMCRx values */
  377. mmcr[0] = mmcr0;
  378. mmcr[1] = mmcr1;
  379. mmcr[2] = mmcra;
  380. return 0;
  381. }
  382. static void p970_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  383. {
  384. int shift, i;
  385. if (pmc <= 1) {
  386. shift = MMCR0_PMC1SEL_SH - 7 * pmc;
  387. i = 0;
  388. } else {
  389. shift = MMCR1_PMC3SEL_SH - 5 * (pmc - 2);
  390. i = 1;
  391. }
  392. /*
  393. * Setting the PMCxSEL field to 0x08 disables PMC x.
  394. */
  395. mmcr[i] = (mmcr[i] & ~(0x1fUL << shift)) | (0x08UL << shift);
  396. }
  397. static int ppc970_generic_events[] = {
  398. [PERF_COUNT_HW_CPU_CYCLES] = 7,
  399. [PERF_COUNT_HW_INSTRUCTIONS] = 1,
  400. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x8810, /* PM_LD_REF_L1 */
  401. [PERF_COUNT_HW_CACHE_MISSES] = 0x3810, /* PM_LD_MISS_L1 */
  402. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x431, /* PM_BR_ISSUED */
  403. [PERF_COUNT_HW_BRANCH_MISSES] = 0x327, /* PM_GRP_BR_MPRED */
  404. };
  405. #define C(x) PERF_COUNT_HW_CACHE_##x
  406. /*
  407. * Table of generalized cache-related events.
  408. * 0 means not supported, -1 means nonsensical, other values
  409. * are event codes.
  410. */
  411. static int ppc970_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  412. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  413. [C(OP_READ)] = { 0x8810, 0x3810 },
  414. [C(OP_WRITE)] = { 0x7810, 0x813 },
  415. [C(OP_PREFETCH)] = { 0x731, 0 },
  416. },
  417. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  418. [C(OP_READ)] = { 0, 0 },
  419. [C(OP_WRITE)] = { -1, -1 },
  420. [C(OP_PREFETCH)] = { 0, 0 },
  421. },
  422. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  423. [C(OP_READ)] = { 0, 0 },
  424. [C(OP_WRITE)] = { 0, 0 },
  425. [C(OP_PREFETCH)] = { 0x733, 0 },
  426. },
  427. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  428. [C(OP_READ)] = { 0, 0x704 },
  429. [C(OP_WRITE)] = { -1, -1 },
  430. [C(OP_PREFETCH)] = { -1, -1 },
  431. },
  432. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  433. [C(OP_READ)] = { 0, 0x700 },
  434. [C(OP_WRITE)] = { -1, -1 },
  435. [C(OP_PREFETCH)] = { -1, -1 },
  436. },
  437. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  438. [C(OP_READ)] = { 0x431, 0x327 },
  439. [C(OP_WRITE)] = { -1, -1 },
  440. [C(OP_PREFETCH)] = { -1, -1 },
  441. },
  442. };
  443. static struct power_pmu ppc970_pmu = {
  444. .name = "PPC970/FX/MP",
  445. .n_counter = 8,
  446. .max_alternatives = 2,
  447. .add_fields = 0x001100005555ull,
  448. .test_adder = 0x013300000000ull,
  449. .compute_mmcr = p970_compute_mmcr,
  450. .get_constraint = p970_get_constraint,
  451. .get_alternatives = p970_get_alternatives,
  452. .disable_pmc = p970_disable_pmc,
  453. .n_generic = ARRAY_SIZE(ppc970_generic_events),
  454. .generic_events = ppc970_generic_events,
  455. .cache_events = &ppc970_cache_events,
  456. };
  457. static int init_ppc970_pmu(void)
  458. {
  459. if (!cur_cpu_spec->oprofile_cpu_type ||
  460. (strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/970")
  461. && strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/970MP")))
  462. return -ENODEV;
  463. return register_power_pmu(&ppc970_pmu);
  464. }
  465. arch_initcall(init_ppc970_pmu);