power5-pmu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Performance counter support for POWER5 (not POWER5++) 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 POWER5 (not POWER5++)
  18. */
  19. #define PM_PMC_SH 20 /* 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 16 /* TTMMUX number and setting - unit select */
  23. #define PM_UNIT_MSK 0xf
  24. #define PM_BYTE_SH 12 /* Byte number of event bus to use */
  25. #define PM_BYTE_MSK 7
  26. #define PM_GRS_SH 8 /* Storage subsystem mux select */
  27. #define PM_GRS_MSK 7
  28. #define PM_BUSEVENT_MSK 0x80 /* Set if event uses event bus */
  29. #define PM_PMCSEL_MSK 0x7f
  30. /* Values in PM_UNIT field */
  31. #define PM_FPU 0
  32. #define PM_ISU0 1
  33. #define PM_IFU 2
  34. #define PM_ISU1 3
  35. #define PM_IDU 4
  36. #define PM_ISU0_ALT 6
  37. #define PM_GRS 7
  38. #define PM_LSU0 8
  39. #define PM_LSU1 0xc
  40. #define PM_LASTUNIT 0xc
  41. /*
  42. * Bits in MMCR1 for POWER5
  43. */
  44. #define MMCR1_TTM0SEL_SH 62
  45. #define MMCR1_TTM1SEL_SH 60
  46. #define MMCR1_TTM2SEL_SH 58
  47. #define MMCR1_TTM3SEL_SH 56
  48. #define MMCR1_TTMSEL_MSK 3
  49. #define MMCR1_TD_CP_DBG0SEL_SH 54
  50. #define MMCR1_TD_CP_DBG1SEL_SH 52
  51. #define MMCR1_TD_CP_DBG2SEL_SH 50
  52. #define MMCR1_TD_CP_DBG3SEL_SH 48
  53. #define MMCR1_GRS_L2SEL_SH 46
  54. #define MMCR1_GRS_L2SEL_MSK 3
  55. #define MMCR1_GRS_L3SEL_SH 44
  56. #define MMCR1_GRS_L3SEL_MSK 3
  57. #define MMCR1_GRS_MCSEL_SH 41
  58. #define MMCR1_GRS_MCSEL_MSK 7
  59. #define MMCR1_GRS_FABSEL_SH 39
  60. #define MMCR1_GRS_FABSEL_MSK 3
  61. #define MMCR1_PMC1_ADDER_SEL_SH 35
  62. #define MMCR1_PMC2_ADDER_SEL_SH 34
  63. #define MMCR1_PMC3_ADDER_SEL_SH 33
  64. #define MMCR1_PMC4_ADDER_SEL_SH 32
  65. #define MMCR1_PMC1SEL_SH 25
  66. #define MMCR1_PMC2SEL_SH 17
  67. #define MMCR1_PMC3SEL_SH 9
  68. #define MMCR1_PMC4SEL_SH 1
  69. #define MMCR1_PMCSEL_SH(n) (MMCR1_PMC1SEL_SH - (n) * 8)
  70. #define MMCR1_PMCSEL_MSK 0x7f
  71. /*
  72. * Layout of constraint bits:
  73. * 6666555555555544444444443333333333222222222211111111110000000000
  74. * 3210987654321098765432109876543210987654321098765432109876543210
  75. * <><>[ ><><>< ><> [ >[ >[ >< >< >< >< ><><><><><><>
  76. * T0T1 NC G0G1G2 G3 UC PS1PS2 B0 B1 B2 B3 P6P5P4P3P2P1
  77. *
  78. * T0 - TTM0 constraint
  79. * 54-55: TTM0SEL value (0=FPU, 2=IFU, 3=ISU1) 0xc0_0000_0000_0000
  80. *
  81. * T1 - TTM1 constraint
  82. * 52-53: TTM1SEL value (0=IDU, 3=GRS) 0x30_0000_0000_0000
  83. *
  84. * NC - number of counters
  85. * 51: NC error 0x0008_0000_0000_0000
  86. * 48-50: number of events needing PMC1-4 0x0007_0000_0000_0000
  87. *
  88. * G0..G3 - GRS mux constraints
  89. * 46-47: GRS_L2SEL value
  90. * 44-45: GRS_L3SEL value
  91. * 41-44: GRS_MCSEL value
  92. * 39-40: GRS_FABSEL value
  93. * Note that these match up with their bit positions in MMCR1
  94. *
  95. * UC - unit constraint: can't have all three of FPU|IFU|ISU1, ISU0, IDU|GRS
  96. * 37: UC3 error 0x20_0000_0000
  97. * 36: FPU|IFU|ISU1 events needed 0x10_0000_0000
  98. * 35: ISU0 events needed 0x08_0000_0000
  99. * 34: IDU|GRS events needed 0x04_0000_0000
  100. *
  101. * PS1
  102. * 33: PS1 error 0x2_0000_0000
  103. * 31-32: count of events needing PMC1/2 0x1_8000_0000
  104. *
  105. * PS2
  106. * 30: PS2 error 0x4000_0000
  107. * 28-29: count of events needing PMC3/4 0x3000_0000
  108. *
  109. * B0
  110. * 24-27: Byte 0 event source 0x0f00_0000
  111. * Encoding as for the event code
  112. *
  113. * B1, B2, B3
  114. * 20-23, 16-19, 12-15: Byte 1, 2, 3 event sources
  115. *
  116. * P1..P6
  117. * 0-11: Count of events needing PMC1..PMC6
  118. */
  119. static const int grsel_shift[8] = {
  120. MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH,
  121. MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH,
  122. MMCR1_GRS_MCSEL_SH, MMCR1_GRS_FABSEL_SH
  123. };
  124. /* Masks and values for using events from the various units */
  125. static unsigned long unit_cons[PM_LASTUNIT+1][2] = {
  126. [PM_FPU] = { 0xc0002000000000ul, 0x00001000000000ul },
  127. [PM_ISU0] = { 0x00002000000000ul, 0x00000800000000ul },
  128. [PM_ISU1] = { 0xc0002000000000ul, 0xc0001000000000ul },
  129. [PM_IFU] = { 0xc0002000000000ul, 0x80001000000000ul },
  130. [PM_IDU] = { 0x30002000000000ul, 0x00000400000000ul },
  131. [PM_GRS] = { 0x30002000000000ul, 0x30000400000000ul },
  132. };
  133. static int power5_get_constraint(u64 event, unsigned long *maskp,
  134. unsigned long *valp)
  135. {
  136. int pmc, byte, unit, sh;
  137. int bit, fmask;
  138. unsigned long mask = 0, value = 0;
  139. int grp = -1;
  140. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  141. if (pmc) {
  142. if (pmc > 6)
  143. return -1;
  144. sh = (pmc - 1) * 2;
  145. mask |= 2 << sh;
  146. value |= 1 << sh;
  147. if (pmc <= 4)
  148. grp = (pmc - 1) >> 1;
  149. else if (event != 0x500009 && event != 0x600005)
  150. return -1;
  151. }
  152. if (event & PM_BUSEVENT_MSK) {
  153. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  154. if (unit > PM_LASTUNIT)
  155. return -1;
  156. if (unit == PM_ISU0_ALT)
  157. unit = PM_ISU0;
  158. mask |= unit_cons[unit][0];
  159. value |= unit_cons[unit][1];
  160. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  161. if (byte >= 4) {
  162. if (unit != PM_LSU1)
  163. return -1;
  164. /* Map LSU1 low word (bytes 4-7) to unit LSU1+1 */
  165. ++unit;
  166. byte &= 3;
  167. }
  168. if (unit == PM_GRS) {
  169. bit = event & 7;
  170. fmask = (bit == 6)? 7: 3;
  171. sh = grsel_shift[bit];
  172. mask |= (unsigned long)fmask << sh;
  173. value |= (unsigned long)((event >> PM_GRS_SH) & fmask)
  174. << sh;
  175. }
  176. /*
  177. * Bus events on bytes 0 and 2 can be counted
  178. * on PMC1/2; bytes 1 and 3 on PMC3/4.
  179. */
  180. if (!pmc)
  181. grp = byte & 1;
  182. /* Set byte lane select field */
  183. mask |= 0xfUL << (24 - 4 * byte);
  184. value |= (unsigned long)unit << (24 - 4 * byte);
  185. }
  186. if (grp == 0) {
  187. /* increment PMC1/2 field */
  188. mask |= 0x200000000ul;
  189. value |= 0x080000000ul;
  190. } else if (grp == 1) {
  191. /* increment PMC3/4 field */
  192. mask |= 0x40000000ul;
  193. value |= 0x10000000ul;
  194. }
  195. if (pmc < 5) {
  196. /* need a counter from PMC1-4 set */
  197. mask |= 0x8000000000000ul;
  198. value |= 0x1000000000000ul;
  199. }
  200. *maskp = mask;
  201. *valp = value;
  202. return 0;
  203. }
  204. #define MAX_ALT 3 /* at most 3 alternatives for any event */
  205. static const unsigned int event_alternatives[][MAX_ALT] = {
  206. { 0x120e4, 0x400002 }, /* PM_GRP_DISP_REJECT */
  207. { 0x410c7, 0x441084 }, /* PM_THRD_L2MISS_BOTH_CYC */
  208. { 0x100005, 0x600005 }, /* PM_RUN_CYC */
  209. { 0x100009, 0x200009, 0x500009 }, /* PM_INST_CMPL */
  210. { 0x300009, 0x400009 }, /* PM_INST_DISP */
  211. };
  212. /*
  213. * Scan the alternatives table for a match and return the
  214. * index into the alternatives table if found, else -1.
  215. */
  216. static int find_alternative(u64 event)
  217. {
  218. int i, j;
  219. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  220. if (event < event_alternatives[i][0])
  221. break;
  222. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  223. if (event == event_alternatives[i][j])
  224. return i;
  225. }
  226. return -1;
  227. }
  228. static const unsigned char bytedecode_alternatives[4][4] = {
  229. /* PMC 1 */ { 0x21, 0x23, 0x25, 0x27 },
  230. /* PMC 2 */ { 0x07, 0x17, 0x0e, 0x1e },
  231. /* PMC 3 */ { 0x20, 0x22, 0x24, 0x26 },
  232. /* PMC 4 */ { 0x07, 0x17, 0x0e, 0x1e }
  233. };
  234. /*
  235. * Some direct events for decodes of event bus byte 3 have alternative
  236. * PMCSEL values on other counters. This returns the alternative
  237. * event code for those that do, or -1 otherwise.
  238. */
  239. static s64 find_alternative_bdecode(u64 event)
  240. {
  241. int pmc, altpmc, pp, j;
  242. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  243. if (pmc == 0 || pmc > 4)
  244. return -1;
  245. altpmc = 5 - pmc; /* 1 <-> 4, 2 <-> 3 */
  246. pp = event & PM_PMCSEL_MSK;
  247. for (j = 0; j < 4; ++j) {
  248. if (bytedecode_alternatives[pmc - 1][j] == pp) {
  249. return (event & ~(PM_PMC_MSKS | PM_PMCSEL_MSK)) |
  250. (altpmc << PM_PMC_SH) |
  251. bytedecode_alternatives[altpmc - 1][j];
  252. }
  253. }
  254. return -1;
  255. }
  256. static int power5_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  257. {
  258. int i, j, nalt = 1;
  259. s64 ae;
  260. alt[0] = event;
  261. nalt = 1;
  262. i = find_alternative(event);
  263. if (i >= 0) {
  264. for (j = 0; j < MAX_ALT; ++j) {
  265. ae = event_alternatives[i][j];
  266. if (ae && ae != event)
  267. alt[nalt++] = ae;
  268. }
  269. } else {
  270. ae = find_alternative_bdecode(event);
  271. if (ae > 0)
  272. alt[nalt++] = ae;
  273. }
  274. return nalt;
  275. }
  276. /*
  277. * Map of which direct events on which PMCs are marked instruction events.
  278. * Indexed by PMCSEL value, bit i (LE) set if PMC i is a marked event.
  279. * Bit 0 is set if it is marked for all PMCs.
  280. * The 0x80 bit indicates a byte decode PMCSEL value.
  281. */
  282. static unsigned char direct_event_is_marked[0x28] = {
  283. 0, /* 00 */
  284. 0x1f, /* 01 PM_IOPS_CMPL */
  285. 0x2, /* 02 PM_MRK_GRP_DISP */
  286. 0xe, /* 03 PM_MRK_ST_CMPL, PM_MRK_ST_GPS, PM_MRK_ST_CMPL_INT */
  287. 0, /* 04 */
  288. 0x1c, /* 05 PM_MRK_BRU_FIN, PM_MRK_INST_FIN, PM_MRK_CRU_FIN */
  289. 0x80, /* 06 */
  290. 0x80, /* 07 */
  291. 0, 0, 0,/* 08 - 0a */
  292. 0x18, /* 0b PM_THRESH_TIMEO, PM_MRK_GRP_TIMEO */
  293. 0, /* 0c */
  294. 0x80, /* 0d */
  295. 0x80, /* 0e */
  296. 0, /* 0f */
  297. 0, /* 10 */
  298. 0x14, /* 11 PM_MRK_GRP_BR_REDIR, PM_MRK_GRP_IC_MISS */
  299. 0, /* 12 */
  300. 0x10, /* 13 PM_MRK_GRP_CMPL */
  301. 0x1f, /* 14 PM_GRP_MRK, PM_MRK_{FXU,FPU,LSU}_FIN */
  302. 0x2, /* 15 PM_MRK_GRP_ISSUED */
  303. 0x80, /* 16 */
  304. 0x80, /* 17 */
  305. 0, 0, 0, 0, 0,
  306. 0x80, /* 1d */
  307. 0x80, /* 1e */
  308. 0, /* 1f */
  309. 0x80, /* 20 */
  310. 0x80, /* 21 */
  311. 0x80, /* 22 */
  312. 0x80, /* 23 */
  313. 0x80, /* 24 */
  314. 0x80, /* 25 */
  315. 0x80, /* 26 */
  316. 0x80, /* 27 */
  317. };
  318. /*
  319. * Returns 1 if event counts things relating to marked instructions
  320. * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
  321. */
  322. static int power5_marked_instr_event(u64 event)
  323. {
  324. int pmc, psel;
  325. int bit, byte, unit;
  326. u32 mask;
  327. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  328. psel = event & PM_PMCSEL_MSK;
  329. if (pmc >= 5)
  330. return 0;
  331. bit = -1;
  332. if (psel < sizeof(direct_event_is_marked)) {
  333. if (direct_event_is_marked[psel] & (1 << pmc))
  334. return 1;
  335. if (direct_event_is_marked[psel] & 0x80)
  336. bit = 4;
  337. else if (psel == 0x08)
  338. bit = pmc - 1;
  339. else if (psel == 0x10)
  340. bit = 4 - pmc;
  341. else if (psel == 0x1b && (pmc == 1 || pmc == 3))
  342. bit = 4;
  343. } else if ((psel & 0x58) == 0x40)
  344. bit = psel & 7;
  345. if (!(event & PM_BUSEVENT_MSK))
  346. return 0;
  347. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  348. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  349. if (unit == PM_LSU0) {
  350. /* byte 1 bits 0-7, byte 2 bits 0,2-4,6 */
  351. mask = 0x5dff00;
  352. } else if (unit == PM_LSU1 && byte >= 4) {
  353. byte -= 4;
  354. /* byte 4 bits 1,3,5,7, byte 5 bits 6-7, byte 7 bits 0-4,6 */
  355. mask = 0x5f00c0aa;
  356. } else
  357. return 0;
  358. return (mask >> (byte * 8 + bit)) & 1;
  359. }
  360. static int power5_compute_mmcr(u64 event[], int n_ev,
  361. unsigned int hwc[], unsigned long mmcr[], struct perf_event *pevents[])
  362. {
  363. unsigned long mmcr1 = 0;
  364. unsigned long mmcra = MMCRA_SDAR_DCACHE_MISS | MMCRA_SDAR_ERAT_MISS;
  365. unsigned int pmc, unit, byte, psel;
  366. unsigned int ttm, grp;
  367. int i, isbus, bit, grsel;
  368. unsigned int pmc_inuse = 0;
  369. unsigned int pmc_grp_use[2];
  370. unsigned char busbyte[4];
  371. unsigned char unituse[16];
  372. int ttmuse;
  373. if (n_ev > 6)
  374. return -1;
  375. /* First pass to count resource use */
  376. pmc_grp_use[0] = pmc_grp_use[1] = 0;
  377. memset(busbyte, 0, sizeof(busbyte));
  378. memset(unituse, 0, sizeof(unituse));
  379. for (i = 0; i < n_ev; ++i) {
  380. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  381. if (pmc) {
  382. if (pmc > 6)
  383. return -1;
  384. if (pmc_inuse & (1 << (pmc - 1)))
  385. return -1;
  386. pmc_inuse |= 1 << (pmc - 1);
  387. /* count 1/2 vs 3/4 use */
  388. if (pmc <= 4)
  389. ++pmc_grp_use[(pmc - 1) >> 1];
  390. }
  391. if (event[i] & PM_BUSEVENT_MSK) {
  392. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  393. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  394. if (unit > PM_LASTUNIT)
  395. return -1;
  396. if (unit == PM_ISU0_ALT)
  397. unit = PM_ISU0;
  398. if (byte >= 4) {
  399. if (unit != PM_LSU1)
  400. return -1;
  401. ++unit;
  402. byte &= 3;
  403. }
  404. if (!pmc)
  405. ++pmc_grp_use[byte & 1];
  406. if (busbyte[byte] && busbyte[byte] != unit)
  407. return -1;
  408. busbyte[byte] = unit;
  409. unituse[unit] = 1;
  410. }
  411. }
  412. if (pmc_grp_use[0] > 2 || pmc_grp_use[1] > 2)
  413. return -1;
  414. /*
  415. * Assign resources and set multiplexer selects.
  416. *
  417. * PM_ISU0 can go either on TTM0 or TTM1, but that's the only
  418. * choice we have to deal with.
  419. */
  420. if (unituse[PM_ISU0] &
  421. (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_ISU1])) {
  422. unituse[PM_ISU0_ALT] = 1; /* move ISU to TTM1 */
  423. unituse[PM_ISU0] = 0;
  424. }
  425. /* Set TTM[01]SEL fields. */
  426. ttmuse = 0;
  427. for (i = PM_FPU; i <= PM_ISU1; ++i) {
  428. if (!unituse[i])
  429. continue;
  430. if (ttmuse++)
  431. return -1;
  432. mmcr1 |= (unsigned long)i << MMCR1_TTM0SEL_SH;
  433. }
  434. ttmuse = 0;
  435. for (; i <= PM_GRS; ++i) {
  436. if (!unituse[i])
  437. continue;
  438. if (ttmuse++)
  439. return -1;
  440. mmcr1 |= (unsigned long)(i & 3) << MMCR1_TTM1SEL_SH;
  441. }
  442. if (ttmuse > 1)
  443. return -1;
  444. /* Set byte lane select fields, TTM[23]SEL and GRS_*SEL. */
  445. for (byte = 0; byte < 4; ++byte) {
  446. unit = busbyte[byte];
  447. if (!unit)
  448. continue;
  449. if (unit == PM_ISU0 && unituse[PM_ISU0_ALT]) {
  450. /* get ISU0 through TTM1 rather than TTM0 */
  451. unit = PM_ISU0_ALT;
  452. } else if (unit == PM_LSU1 + 1) {
  453. /* select lower word of LSU1 for this byte */
  454. mmcr1 |= 1ul << (MMCR1_TTM3SEL_SH + 3 - byte);
  455. }
  456. ttm = unit >> 2;
  457. mmcr1 |= (unsigned long)ttm
  458. << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
  459. }
  460. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  461. for (i = 0; i < n_ev; ++i) {
  462. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  463. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  464. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  465. psel = event[i] & PM_PMCSEL_MSK;
  466. isbus = event[i] & PM_BUSEVENT_MSK;
  467. if (!pmc) {
  468. /* Bus event or any-PMC direct event */
  469. for (pmc = 0; pmc < 4; ++pmc) {
  470. if (pmc_inuse & (1 << pmc))
  471. continue;
  472. grp = (pmc >> 1) & 1;
  473. if (isbus) {
  474. if (grp == (byte & 1))
  475. break;
  476. } else if (pmc_grp_use[grp] < 2) {
  477. ++pmc_grp_use[grp];
  478. break;
  479. }
  480. }
  481. pmc_inuse |= 1 << pmc;
  482. } else if (pmc <= 4) {
  483. /* Direct event */
  484. --pmc;
  485. if ((psel == 8 || psel == 0x10) && isbus && (byte & 2))
  486. /* add events on higher-numbered bus */
  487. mmcr1 |= 1ul << (MMCR1_PMC1_ADDER_SEL_SH - pmc);
  488. } else {
  489. /* Instructions or run cycles on PMC5/6 */
  490. --pmc;
  491. }
  492. if (isbus && unit == PM_GRS) {
  493. bit = psel & 7;
  494. grsel = (event[i] >> PM_GRS_SH) & PM_GRS_MSK;
  495. mmcr1 |= (unsigned long)grsel << grsel_shift[bit];
  496. }
  497. if (power5_marked_instr_event(event[i]))
  498. mmcra |= MMCRA_SAMPLE_ENABLE;
  499. if (pmc <= 3)
  500. mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
  501. hwc[i] = pmc;
  502. }
  503. /* Return MMCRx values */
  504. mmcr[0] = 0;
  505. if (pmc_inuse & 1)
  506. mmcr[0] = MMCR0_PMC1CE;
  507. if (pmc_inuse & 0x3e)
  508. mmcr[0] |= MMCR0_PMCjCE;
  509. mmcr[1] = mmcr1;
  510. mmcr[2] = mmcra;
  511. return 0;
  512. }
  513. static void power5_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  514. {
  515. if (pmc <= 3)
  516. mmcr[1] &= ~(0x7fUL << MMCR1_PMCSEL_SH(pmc));
  517. }
  518. static int power5_generic_events[] = {
  519. [PERF_COUNT_HW_CPU_CYCLES] = 0xf,
  520. [PERF_COUNT_HW_INSTRUCTIONS] = 0x100009,
  521. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4c1090, /* LD_REF_L1 */
  522. [PERF_COUNT_HW_CACHE_MISSES] = 0x3c1088, /* LD_MISS_L1 */
  523. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x230e4, /* BR_ISSUED */
  524. [PERF_COUNT_HW_BRANCH_MISSES] = 0x230e5, /* BR_MPRED_CR */
  525. };
  526. #define C(x) PERF_COUNT_HW_CACHE_##x
  527. /*
  528. * Table of generalized cache-related events.
  529. * 0 means not supported, -1 means nonsensical, other values
  530. * are event codes.
  531. */
  532. static int power5_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  533. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  534. [C(OP_READ)] = { 0x4c1090, 0x3c1088 },
  535. [C(OP_WRITE)] = { 0x3c1090, 0xc10c3 },
  536. [C(OP_PREFETCH)] = { 0xc70e7, 0 },
  537. },
  538. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  539. [C(OP_READ)] = { 0, 0 },
  540. [C(OP_WRITE)] = { -1, -1 },
  541. [C(OP_PREFETCH)] = { 0, 0 },
  542. },
  543. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  544. [C(OP_READ)] = { 0, 0x3c309b },
  545. [C(OP_WRITE)] = { 0, 0 },
  546. [C(OP_PREFETCH)] = { 0xc50c3, 0 },
  547. },
  548. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  549. [C(OP_READ)] = { 0x2c4090, 0x800c4 },
  550. [C(OP_WRITE)] = { -1, -1 },
  551. [C(OP_PREFETCH)] = { -1, -1 },
  552. },
  553. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  554. [C(OP_READ)] = { 0, 0x800c0 },
  555. [C(OP_WRITE)] = { -1, -1 },
  556. [C(OP_PREFETCH)] = { -1, -1 },
  557. },
  558. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  559. [C(OP_READ)] = { 0x230e4, 0x230e5 },
  560. [C(OP_WRITE)] = { -1, -1 },
  561. [C(OP_PREFETCH)] = { -1, -1 },
  562. },
  563. [C(NODE)] = { /* RESULT_ACCESS RESULT_MISS */
  564. [C(OP_READ)] = { -1, -1 },
  565. [C(OP_WRITE)] = { -1, -1 },
  566. [C(OP_PREFETCH)] = { -1, -1 },
  567. },
  568. };
  569. static struct power_pmu power5_pmu = {
  570. .name = "POWER5",
  571. .n_counter = 6,
  572. .max_alternatives = MAX_ALT,
  573. .add_fields = 0x7000090000555ul,
  574. .test_adder = 0x3000490000000ul,
  575. .compute_mmcr = power5_compute_mmcr,
  576. .get_constraint = power5_get_constraint,
  577. .get_alternatives = power5_get_alternatives,
  578. .disable_pmc = power5_disable_pmc,
  579. .n_generic = ARRAY_SIZE(power5_generic_events),
  580. .generic_events = power5_generic_events,
  581. .cache_events = &power5_cache_events,
  582. .flags = PPMU_HAS_SSLOT,
  583. };
  584. static int __init init_power5_pmu(void)
  585. {
  586. if (!cur_cpu_spec->oprofile_cpu_type ||
  587. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power5"))
  588. return -ENODEV;
  589. return register_power_pmu(&power5_pmu);
  590. }
  591. early_initcall(init_power5_pmu);