power4-pmu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Performance counter support for POWER4 (GP) and POWER4+ (GQ) 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 POWER4
  16. */
  17. #define PM_PMC_SH 12 /* PMC number (1-based) for direct events */
  18. #define PM_PMC_MSK 0xf
  19. #define PM_UNIT_SH 8 /* TTMMUX number and setting - unit select */
  20. #define PM_UNIT_MSK 0xf
  21. #define PM_LOWER_SH 6
  22. #define PM_LOWER_MSK 1
  23. #define PM_LOWER_MSKS 0x40
  24. #define PM_BYTE_SH 4 /* Byte number of event bus to use */
  25. #define PM_BYTE_MSK 3
  26. #define PM_PMCSEL_MSK 7
  27. /*
  28. * Unit code values
  29. */
  30. #define PM_FPU 1
  31. #define PM_ISU1 2
  32. #define PM_IFU 3
  33. #define PM_IDU0 4
  34. #define PM_ISU1_ALT 6
  35. #define PM_ISU2 7
  36. #define PM_IFU_ALT 8
  37. #define PM_LSU0 9
  38. #define PM_LSU1 0xc
  39. #define PM_GPS 0xf
  40. /*
  41. * Bits in MMCR0 for POWER4
  42. */
  43. #define MMCR0_PMC1SEL_SH 8
  44. #define MMCR0_PMC2SEL_SH 1
  45. #define MMCR_PMCSEL_MSK 0x1f
  46. /*
  47. * Bits in MMCR1 for POWER4
  48. */
  49. #define MMCR1_TTM0SEL_SH 62
  50. #define MMCR1_TTC0SEL_SH 61
  51. #define MMCR1_TTM1SEL_SH 59
  52. #define MMCR1_TTC1SEL_SH 58
  53. #define MMCR1_TTM2SEL_SH 56
  54. #define MMCR1_TTC2SEL_SH 55
  55. #define MMCR1_TTM3SEL_SH 53
  56. #define MMCR1_TTC3SEL_SH 52
  57. #define MMCR1_TTMSEL_MSK 3
  58. #define MMCR1_TD_CP_DBG0SEL_SH 50
  59. #define MMCR1_TD_CP_DBG1SEL_SH 48
  60. #define MMCR1_TD_CP_DBG2SEL_SH 46
  61. #define MMCR1_TD_CP_DBG3SEL_SH 44
  62. #define MMCR1_DEBUG0SEL_SH 43
  63. #define MMCR1_DEBUG1SEL_SH 42
  64. #define MMCR1_DEBUG2SEL_SH 41
  65. #define MMCR1_DEBUG3SEL_SH 40
  66. #define MMCR1_PMC1_ADDER_SEL_SH 39
  67. #define MMCR1_PMC2_ADDER_SEL_SH 38
  68. #define MMCR1_PMC6_ADDER_SEL_SH 37
  69. #define MMCR1_PMC5_ADDER_SEL_SH 36
  70. #define MMCR1_PMC8_ADDER_SEL_SH 35
  71. #define MMCR1_PMC7_ADDER_SEL_SH 34
  72. #define MMCR1_PMC3_ADDER_SEL_SH 33
  73. #define MMCR1_PMC4_ADDER_SEL_SH 32
  74. #define MMCR1_PMC3SEL_SH 27
  75. #define MMCR1_PMC4SEL_SH 22
  76. #define MMCR1_PMC5SEL_SH 17
  77. #define MMCR1_PMC6SEL_SH 12
  78. #define MMCR1_PMC7SEL_SH 7
  79. #define MMCR1_PMC8SEL_SH 2 /* note bit 0 is in MMCRA for GP */
  80. static short mmcr1_adder_bits[8] = {
  81. MMCR1_PMC1_ADDER_SEL_SH,
  82. MMCR1_PMC2_ADDER_SEL_SH,
  83. MMCR1_PMC3_ADDER_SEL_SH,
  84. MMCR1_PMC4_ADDER_SEL_SH,
  85. MMCR1_PMC5_ADDER_SEL_SH,
  86. MMCR1_PMC6_ADDER_SEL_SH,
  87. MMCR1_PMC7_ADDER_SEL_SH,
  88. MMCR1_PMC8_ADDER_SEL_SH
  89. };
  90. /*
  91. * Bits in MMCRA
  92. */
  93. #define MMCRA_PMC8SEL0_SH 17 /* PMC8SEL bit 0 for GP */
  94. /*
  95. * Layout of constraint bits:
  96. * 6666555555555544444444443333333333222222222211111111110000000000
  97. * 3210987654321098765432109876543210987654321098765432109876543210
  98. * |[ >[ >[ >|||[ >[ >< >< >< >< ><><><><><><><><>
  99. * | UC1 UC2 UC3 ||| PS1 PS2 B0 B1 B2 B3 P1P2P3P4P5P6P7P8
  100. * \SMPL ||\TTC3SEL
  101. * |\TTC_IFU_SEL
  102. * \TTM2SEL0
  103. *
  104. * SMPL - SAMPLE_ENABLE constraint
  105. * 56: SAMPLE_ENABLE value 0x0100_0000_0000_0000
  106. *
  107. * UC1 - unit constraint 1: can't have all three of FPU/ISU1/IDU0|ISU2
  108. * 55: UC1 error 0x0080_0000_0000_0000
  109. * 54: FPU events needed 0x0040_0000_0000_0000
  110. * 53: ISU1 events needed 0x0020_0000_0000_0000
  111. * 52: IDU0|ISU2 events needed 0x0010_0000_0000_0000
  112. *
  113. * UC2 - unit constraint 2: can't have all three of FPU/IFU/LSU0
  114. * 51: UC2 error 0x0008_0000_0000_0000
  115. * 50: FPU events needed 0x0004_0000_0000_0000
  116. * 49: IFU events needed 0x0002_0000_0000_0000
  117. * 48: LSU0 events needed 0x0001_0000_0000_0000
  118. *
  119. * UC3 - unit constraint 3: can't have all four of LSU0/IFU/IDU0|ISU2/ISU1
  120. * 47: UC3 error 0x8000_0000_0000
  121. * 46: LSU0 events needed 0x4000_0000_0000
  122. * 45: IFU events needed 0x2000_0000_0000
  123. * 44: IDU0|ISU2 events needed 0x1000_0000_0000
  124. * 43: ISU1 events needed 0x0800_0000_0000
  125. *
  126. * TTM2SEL0
  127. * 42: 0 = IDU0 events needed
  128. * 1 = ISU2 events needed 0x0400_0000_0000
  129. *
  130. * TTC_IFU_SEL
  131. * 41: 0 = IFU.U events needed
  132. * 1 = IFU.L events needed 0x0200_0000_0000
  133. *
  134. * TTC3SEL
  135. * 40: 0 = LSU1.U events needed
  136. * 1 = LSU1.L events needed 0x0100_0000_0000
  137. *
  138. * PS1
  139. * 39: PS1 error 0x0080_0000_0000
  140. * 36-38: count of events needing PMC1/2/5/6 0x0070_0000_0000
  141. *
  142. * PS2
  143. * 35: PS2 error 0x0008_0000_0000
  144. * 32-34: count of events needing PMC3/4/7/8 0x0007_0000_0000
  145. *
  146. * B0
  147. * 28-31: Byte 0 event source 0xf000_0000
  148. * 1 = FPU
  149. * 2 = ISU1
  150. * 3 = IFU
  151. * 4 = IDU0
  152. * 7 = ISU2
  153. * 9 = LSU0
  154. * c = LSU1
  155. * f = GPS
  156. *
  157. * B1, B2, B3
  158. * 24-27, 20-23, 16-19: Byte 1, 2, 3 event sources
  159. *
  160. * P8
  161. * 15: P8 error 0x8000
  162. * 14-15: Count of events needing PMC8
  163. *
  164. * P1..P7
  165. * 0-13: Count of events needing PMC1..PMC7
  166. *
  167. * Note: this doesn't allow events using IFU.U to be combined with events
  168. * using IFU.L, though that is feasible (using TTM0 and TTM2). However
  169. * there are no listed events for IFU.L (they are debug events not
  170. * verified for performance monitoring) so this shouldn't cause a
  171. * problem.
  172. */
  173. static struct unitinfo {
  174. u64 value, mask;
  175. int unit;
  176. int lowerbit;
  177. } p4_unitinfo[16] = {
  178. [PM_FPU] = { 0x44000000000000ull, 0x88000000000000ull, PM_FPU, 0 },
  179. [PM_ISU1] = { 0x20080000000000ull, 0x88000000000000ull, PM_ISU1, 0 },
  180. [PM_ISU1_ALT] =
  181. { 0x20080000000000ull, 0x88000000000000ull, PM_ISU1, 0 },
  182. [PM_IFU] = { 0x02200000000000ull, 0x08820000000000ull, PM_IFU, 41 },
  183. [PM_IFU_ALT] =
  184. { 0x02200000000000ull, 0x08820000000000ull, PM_IFU, 41 },
  185. [PM_IDU0] = { 0x10100000000000ull, 0x80840000000000ull, PM_IDU0, 1 },
  186. [PM_ISU2] = { 0x10140000000000ull, 0x80840000000000ull, PM_ISU2, 0 },
  187. [PM_LSU0] = { 0x01400000000000ull, 0x08800000000000ull, PM_LSU0, 0 },
  188. [PM_LSU1] = { 0x00000000000000ull, 0x00010000000000ull, PM_LSU1, 40 },
  189. [PM_GPS] = { 0x00000000000000ull, 0x00000000000000ull, PM_GPS, 0 }
  190. };
  191. static unsigned char direct_marked_event[8] = {
  192. (1<<2) | (1<<3), /* PMC1: PM_MRK_GRP_DISP, PM_MRK_ST_CMPL */
  193. (1<<3) | (1<<5), /* PMC2: PM_THRESH_TIMEO, PM_MRK_BRU_FIN */
  194. (1<<3), /* PMC3: PM_MRK_ST_CMPL_INT */
  195. (1<<4) | (1<<5), /* PMC4: PM_MRK_GRP_CMPL, PM_MRK_CRU_FIN */
  196. (1<<4) | (1<<5), /* PMC5: PM_MRK_GRP_TIMEO */
  197. (1<<3) | (1<<4) | (1<<5),
  198. /* PMC6: PM_MRK_ST_GPS, PM_MRK_FXU_FIN, PM_MRK_GRP_ISSUED */
  199. (1<<4) | (1<<5), /* PMC7: PM_MRK_FPU_FIN, PM_MRK_INST_FIN */
  200. (1<<4), /* PMC8: PM_MRK_LSU_FIN */
  201. };
  202. /*
  203. * Returns 1 if event counts things relating to marked instructions
  204. * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
  205. */
  206. static int p4_marked_instr_event(u64 event)
  207. {
  208. int pmc, psel, unit, byte, bit;
  209. unsigned int mask;
  210. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  211. psel = event & PM_PMCSEL_MSK;
  212. if (pmc) {
  213. if (direct_marked_event[pmc - 1] & (1 << psel))
  214. return 1;
  215. if (psel == 0) /* add events */
  216. bit = (pmc <= 4)? pmc - 1: 8 - pmc;
  217. else if (psel == 6) /* decode events */
  218. bit = 4;
  219. else
  220. return 0;
  221. } else
  222. bit = psel;
  223. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  224. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  225. mask = 0;
  226. switch (unit) {
  227. case PM_LSU1:
  228. if (event & PM_LOWER_MSKS)
  229. mask = 1 << 28; /* byte 7 bit 4 */
  230. else
  231. mask = 6 << 24; /* byte 3 bits 1 and 2 */
  232. break;
  233. case PM_LSU0:
  234. /* byte 3, bit 3; byte 2 bits 0,2,3,4,5; byte 1 */
  235. mask = 0x083dff00;
  236. }
  237. return (mask >> (byte * 8 + bit)) & 1;
  238. }
  239. static int p4_get_constraint(u64 event, u64 *maskp, u64 *valp)
  240. {
  241. int pmc, byte, unit, lower, sh;
  242. u64 mask = 0, value = 0;
  243. int grp = -1;
  244. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  245. if (pmc) {
  246. if (pmc > 8)
  247. return -1;
  248. sh = (pmc - 1) * 2;
  249. mask |= 2 << sh;
  250. value |= 1 << sh;
  251. grp = ((pmc - 1) >> 1) & 1;
  252. }
  253. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  254. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  255. if (unit) {
  256. lower = (event >> PM_LOWER_SH) & PM_LOWER_MSK;
  257. /*
  258. * Bus events on bytes 0 and 2 can be counted
  259. * on PMC1/2/5/6; bytes 1 and 3 on PMC3/4/7/8.
  260. */
  261. if (!pmc)
  262. grp = byte & 1;
  263. if (!p4_unitinfo[unit].unit)
  264. return -1;
  265. mask |= p4_unitinfo[unit].mask;
  266. value |= p4_unitinfo[unit].value;
  267. sh = p4_unitinfo[unit].lowerbit;
  268. if (sh > 1)
  269. value |= (u64)lower << sh;
  270. else if (lower != sh)
  271. return -1;
  272. unit = p4_unitinfo[unit].unit;
  273. /* Set byte lane select field */
  274. mask |= 0xfULL << (28 - 4 * byte);
  275. value |= (u64)unit << (28 - 4 * byte);
  276. }
  277. if (grp == 0) {
  278. /* increment PMC1/2/5/6 field */
  279. mask |= 0x8000000000ull;
  280. value |= 0x1000000000ull;
  281. } else {
  282. /* increment PMC3/4/7/8 field */
  283. mask |= 0x800000000ull;
  284. value |= 0x100000000ull;
  285. }
  286. /* Marked instruction events need sample_enable set */
  287. if (p4_marked_instr_event(event)) {
  288. mask |= 1ull << 56;
  289. value |= 1ull << 56;
  290. }
  291. /* PMCSEL=6 decode events on byte 2 need sample_enable clear */
  292. if (pmc && (event & PM_PMCSEL_MSK) == 6 && byte == 2)
  293. mask |= 1ull << 56;
  294. *maskp = mask;
  295. *valp = value;
  296. return 0;
  297. }
  298. static unsigned int ppc_inst_cmpl[] = {
  299. 0x1001, 0x4001, 0x6001, 0x7001, 0x8001
  300. };
  301. static int p4_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  302. {
  303. int i, j, na;
  304. alt[0] = event;
  305. na = 1;
  306. /* 2 possibilities for PM_GRP_DISP_REJECT */
  307. if (event == 0x8003 || event == 0x0224) {
  308. alt[1] = event ^ (0x8003 ^ 0x0224);
  309. return 2;
  310. }
  311. /* 2 possibilities for PM_ST_MISS_L1 */
  312. if (event == 0x0c13 || event == 0x0c23) {
  313. alt[1] = event ^ (0x0c13 ^ 0x0c23);
  314. return 2;
  315. }
  316. /* several possibilities for PM_INST_CMPL */
  317. for (i = 0; i < ARRAY_SIZE(ppc_inst_cmpl); ++i) {
  318. if (event == ppc_inst_cmpl[i]) {
  319. for (j = 0; j < ARRAY_SIZE(ppc_inst_cmpl); ++j)
  320. if (j != i)
  321. alt[na++] = ppc_inst_cmpl[j];
  322. break;
  323. }
  324. }
  325. return na;
  326. }
  327. static int p4_compute_mmcr(u64 event[], int n_ev,
  328. unsigned int hwc[], u64 mmcr[])
  329. {
  330. u64 mmcr0 = 0, mmcr1 = 0, mmcra = 0;
  331. unsigned int pmc, unit, byte, psel, lower;
  332. unsigned int ttm, grp;
  333. unsigned int pmc_inuse = 0;
  334. unsigned int pmc_grp_use[2];
  335. unsigned char busbyte[4];
  336. unsigned char unituse[16];
  337. unsigned int unitlower = 0;
  338. int i;
  339. if (n_ev > 8)
  340. return -1;
  341. /* First pass to count resource use */
  342. pmc_grp_use[0] = pmc_grp_use[1] = 0;
  343. memset(busbyte, 0, sizeof(busbyte));
  344. memset(unituse, 0, sizeof(unituse));
  345. for (i = 0; i < n_ev; ++i) {
  346. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  347. if (pmc) {
  348. if (pmc_inuse & (1 << (pmc - 1)))
  349. return -1;
  350. pmc_inuse |= 1 << (pmc - 1);
  351. /* count 1/2/5/6 vs 3/4/7/8 use */
  352. ++pmc_grp_use[((pmc - 1) >> 1) & 1];
  353. }
  354. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  355. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  356. lower = (event[i] >> PM_LOWER_SH) & PM_LOWER_MSK;
  357. if (unit) {
  358. if (!pmc)
  359. ++pmc_grp_use[byte & 1];
  360. if (unit == 6 || unit == 8)
  361. /* map alt ISU1/IFU codes: 6->2, 8->3 */
  362. unit = (unit >> 1) - 1;
  363. if (busbyte[byte] && busbyte[byte] != unit)
  364. return -1;
  365. busbyte[byte] = unit;
  366. lower <<= unit;
  367. if (unituse[unit] && lower != (unitlower & lower))
  368. return -1;
  369. unituse[unit] = 1;
  370. unitlower |= lower;
  371. }
  372. }
  373. if (pmc_grp_use[0] > 4 || pmc_grp_use[1] > 4)
  374. return -1;
  375. /*
  376. * Assign resources and set multiplexer selects.
  377. *
  378. * Units 1,2,3 are on TTM0, 4,6,7 on TTM1, 8,10 on TTM2.
  379. * Each TTMx can only select one unit, but since
  380. * units 2 and 6 are both ISU1, and 3 and 8 are both IFU,
  381. * we have some choices.
  382. */
  383. if (unituse[2] & (unituse[1] | (unituse[3] & unituse[9]))) {
  384. unituse[6] = 1; /* Move 2 to 6 */
  385. unituse[2] = 0;
  386. }
  387. if (unituse[3] & (unituse[1] | unituse[2])) {
  388. unituse[8] = 1; /* Move 3 to 8 */
  389. unituse[3] = 0;
  390. unitlower = (unitlower & ~8) | ((unitlower & 8) << 5);
  391. }
  392. /* Check only one unit per TTMx */
  393. if (unituse[1] + unituse[2] + unituse[3] > 1 ||
  394. unituse[4] + unituse[6] + unituse[7] > 1 ||
  395. unituse[8] + unituse[9] > 1 ||
  396. (unituse[5] | unituse[10] | unituse[11] |
  397. unituse[13] | unituse[14]))
  398. return -1;
  399. /* Set TTMxSEL fields. Note, units 1-3 => TTM0SEL codes 0-2 */
  400. mmcr1 |= (u64)(unituse[3] * 2 + unituse[2]) << MMCR1_TTM0SEL_SH;
  401. mmcr1 |= (u64)(unituse[7] * 3 + unituse[6] * 2) << MMCR1_TTM1SEL_SH;
  402. mmcr1 |= (u64)unituse[9] << MMCR1_TTM2SEL_SH;
  403. /* Set TTCxSEL fields. */
  404. if (unitlower & 0xe)
  405. mmcr1 |= 1ull << MMCR1_TTC0SEL_SH;
  406. if (unitlower & 0xf0)
  407. mmcr1 |= 1ull << MMCR1_TTC1SEL_SH;
  408. if (unitlower & 0xf00)
  409. mmcr1 |= 1ull << MMCR1_TTC2SEL_SH;
  410. if (unitlower & 0x7000)
  411. mmcr1 |= 1ull << MMCR1_TTC3SEL_SH;
  412. /* Set byte lane select fields. */
  413. for (byte = 0; byte < 4; ++byte) {
  414. unit = busbyte[byte];
  415. if (!unit)
  416. continue;
  417. if (unit == 0xf) {
  418. /* special case for GPS */
  419. mmcr1 |= 1ull << (MMCR1_DEBUG0SEL_SH - byte);
  420. } else {
  421. if (!unituse[unit])
  422. ttm = unit - 1; /* 2->1, 3->2 */
  423. else
  424. ttm = unit >> 2;
  425. mmcr1 |= (u64)ttm << (MMCR1_TD_CP_DBG0SEL_SH - 2*byte);
  426. }
  427. }
  428. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  429. for (i = 0; i < n_ev; ++i) {
  430. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  431. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  432. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  433. psel = event[i] & PM_PMCSEL_MSK;
  434. if (!pmc) {
  435. /* Bus event or 00xxx direct event (off or cycles) */
  436. if (unit)
  437. psel |= 0x10 | ((byte & 2) << 2);
  438. for (pmc = 0; pmc < 8; ++pmc) {
  439. if (pmc_inuse & (1 << pmc))
  440. continue;
  441. grp = (pmc >> 1) & 1;
  442. if (unit) {
  443. if (grp == (byte & 1))
  444. break;
  445. } else if (pmc_grp_use[grp] < 4) {
  446. ++pmc_grp_use[grp];
  447. break;
  448. }
  449. }
  450. pmc_inuse |= 1 << pmc;
  451. } else {
  452. /* Direct event */
  453. --pmc;
  454. if (psel == 0 && (byte & 2))
  455. /* add events on higher-numbered bus */
  456. mmcr1 |= 1ull << mmcr1_adder_bits[pmc];
  457. else if (psel == 6 && byte == 3)
  458. /* seem to need to set sample_enable here */
  459. mmcra |= MMCRA_SAMPLE_ENABLE;
  460. psel |= 8;
  461. }
  462. if (pmc <= 1)
  463. mmcr0 |= psel << (MMCR0_PMC1SEL_SH - 7 * pmc);
  464. else
  465. mmcr1 |= psel << (MMCR1_PMC3SEL_SH - 5 * (pmc - 2));
  466. if (pmc == 7) /* PMC8 */
  467. mmcra |= (psel & 1) << MMCRA_PMC8SEL0_SH;
  468. hwc[i] = pmc;
  469. if (p4_marked_instr_event(event[i]))
  470. mmcra |= MMCRA_SAMPLE_ENABLE;
  471. }
  472. if (pmc_inuse & 1)
  473. mmcr0 |= MMCR0_PMC1CE;
  474. if (pmc_inuse & 0xfe)
  475. mmcr0 |= MMCR0_PMCjCE;
  476. mmcra |= 0x2000; /* mark only one IOP per PPC instruction */
  477. /* Return MMCRx values */
  478. mmcr[0] = mmcr0;
  479. mmcr[1] = mmcr1;
  480. mmcr[2] = mmcra;
  481. return 0;
  482. }
  483. static void p4_disable_pmc(unsigned int pmc, u64 mmcr[])
  484. {
  485. /*
  486. * Setting the PMCxSEL field to 0 disables PMC x.
  487. * (Note that pmc is 0-based here, not 1-based.)
  488. */
  489. if (pmc <= 1) {
  490. mmcr[0] &= ~(0x1fUL << (MMCR0_PMC1SEL_SH - 7 * pmc));
  491. } else {
  492. mmcr[1] &= ~(0x1fUL << (MMCR1_PMC3SEL_SH - 5 * (pmc - 2)));
  493. if (pmc == 7)
  494. mmcr[2] &= ~(1UL << MMCRA_PMC8SEL0_SH);
  495. }
  496. }
  497. static int p4_generic_events[] = {
  498. [PERF_COUNT_HW_CPU_CYCLES] = 7,
  499. [PERF_COUNT_HW_INSTRUCTIONS] = 0x1001,
  500. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x8c10, /* PM_LD_REF_L1 */
  501. [PERF_COUNT_HW_CACHE_MISSES] = 0x3c10, /* PM_LD_MISS_L1 */
  502. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x330, /* PM_BR_ISSUED */
  503. [PERF_COUNT_HW_BRANCH_MISSES] = 0x331, /* PM_BR_MPRED_CR */
  504. };
  505. #define C(x) PERF_COUNT_HW_CACHE_##x
  506. /*
  507. * Table of generalized cache-related events.
  508. * 0 means not supported, -1 means nonsensical, other values
  509. * are event codes.
  510. */
  511. static int power4_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  512. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  513. [C(OP_READ)] = { 0x8c10, 0x3c10 },
  514. [C(OP_WRITE)] = { 0x7c10, 0xc13 },
  515. [C(OP_PREFETCH)] = { 0xc35, 0 },
  516. },
  517. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  518. [C(OP_READ)] = { 0, 0 },
  519. [C(OP_WRITE)] = { -1, -1 },
  520. [C(OP_PREFETCH)] = { 0, 0 },
  521. },
  522. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  523. [C(OP_READ)] = { 0, 0 },
  524. [C(OP_WRITE)] = { 0, 0 },
  525. [C(OP_PREFETCH)] = { 0xc34, 0 },
  526. },
  527. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  528. [C(OP_READ)] = { 0, 0x904 },
  529. [C(OP_WRITE)] = { -1, -1 },
  530. [C(OP_PREFETCH)] = { -1, -1 },
  531. },
  532. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  533. [C(OP_READ)] = { 0, 0x900 },
  534. [C(OP_WRITE)] = { -1, -1 },
  535. [C(OP_PREFETCH)] = { -1, -1 },
  536. },
  537. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  538. [C(OP_READ)] = { 0x330, 0x331 },
  539. [C(OP_WRITE)] = { -1, -1 },
  540. [C(OP_PREFETCH)] = { -1, -1 },
  541. },
  542. };
  543. struct power_pmu power4_pmu = {
  544. .n_counter = 8,
  545. .max_alternatives = 5,
  546. .add_fields = 0x0000001100005555ull,
  547. .test_adder = 0x0011083300000000ull,
  548. .compute_mmcr = p4_compute_mmcr,
  549. .get_constraint = p4_get_constraint,
  550. .get_alternatives = p4_get_alternatives,
  551. .disable_pmc = p4_disable_pmc,
  552. .n_generic = ARRAY_SIZE(p4_generic_events),
  553. .generic_events = p4_generic_events,
  554. .cache_events = &power4_cache_events,
  555. };