mpc7450-pmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Performance counter support for MPC7450-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. #define N_COUNTER 6 /* Number of hardware counters */
  16. #define MAX_ALT 3 /* Maximum number of event alternative codes */
  17. /*
  18. * Bits in event code for MPC7450 family
  19. */
  20. #define PM_THRMULT_MSKS 0x40000
  21. #define PM_THRESH_SH 12
  22. #define PM_THRESH_MSK 0x3f
  23. #define PM_PMC_SH 8
  24. #define PM_PMC_MSK 7
  25. #define PM_PMCSEL_MSK 0x7f
  26. /*
  27. * Classify events according to how specific their PMC requirements are.
  28. * Result is:
  29. * 0: can go on any PMC
  30. * 1: can go on PMCs 1-4
  31. * 2: can go on PMCs 1,2,4
  32. * 3: can go on PMCs 1 or 2
  33. * 4: can only go on one PMC
  34. * -1: event code is invalid
  35. */
  36. #define N_CLASSES 5
  37. static int mpc7450_classify_event(u32 event)
  38. {
  39. int pmc;
  40. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  41. if (pmc) {
  42. if (pmc > N_COUNTER)
  43. return -1;
  44. return 4;
  45. }
  46. event &= PM_PMCSEL_MSK;
  47. if (event <= 1)
  48. return 0;
  49. if (event <= 7)
  50. return 1;
  51. if (event <= 13)
  52. return 2;
  53. if (event <= 22)
  54. return 3;
  55. return -1;
  56. }
  57. /*
  58. * Events using threshold and possible threshold scale:
  59. * code scale? name
  60. * 11e N PM_INSTQ_EXCEED_CYC
  61. * 11f N PM_ALTV_IQ_EXCEED_CYC
  62. * 128 Y PM_DTLB_SEARCH_EXCEED_CYC
  63. * 12b Y PM_LD_MISS_EXCEED_L1_CYC
  64. * 220 N PM_CQ_EXCEED_CYC
  65. * 30c N PM_GPR_RB_EXCEED_CYC
  66. * 30d ? PM_FPR_IQ_EXCEED_CYC ?
  67. * 311 Y PM_ITLB_SEARCH_EXCEED
  68. * 410 N PM_GPR_IQ_EXCEED_CYC
  69. */
  70. /*
  71. * Return use of threshold and threshold scale bits:
  72. * 0 = uses neither, 1 = uses threshold, 2 = uses both
  73. */
  74. static int mpc7450_threshold_use(u32 event)
  75. {
  76. int pmc, sel;
  77. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  78. sel = event & PM_PMCSEL_MSK;
  79. switch (pmc) {
  80. case 1:
  81. if (sel == 0x1e || sel == 0x1f)
  82. return 1;
  83. if (sel == 0x28 || sel == 0x2b)
  84. return 2;
  85. break;
  86. case 2:
  87. if (sel == 0x20)
  88. return 1;
  89. break;
  90. case 3:
  91. if (sel == 0xc || sel == 0xd)
  92. return 1;
  93. if (sel == 0x11)
  94. return 2;
  95. break;
  96. case 4:
  97. if (sel == 0x10)
  98. return 1;
  99. break;
  100. }
  101. return 0;
  102. }
  103. /*
  104. * Layout of constraint bits:
  105. * 33222222222211111111110000000000
  106. * 10987654321098765432109876543210
  107. * |< >< > < > < ><><><><><><>
  108. * TS TV G4 G3 G2P6P5P4P3P2P1
  109. *
  110. * P1 - P6
  111. * 0 - 11: Count of events needing PMC1 .. PMC6
  112. *
  113. * G2
  114. * 12 - 14: Count of events needing PMC1 or PMC2
  115. *
  116. * G3
  117. * 16 - 18: Count of events needing PMC1, PMC2 or PMC4
  118. *
  119. * G4
  120. * 20 - 23: Count of events needing PMC1, PMC2, PMC3 or PMC4
  121. *
  122. * TV
  123. * 24 - 29: Threshold value requested
  124. *
  125. * TS
  126. * 30: Threshold scale value requested
  127. */
  128. static u32 pmcbits[N_COUNTER][2] = {
  129. { 0x00844002, 0x00111001 }, /* PMC1 mask, value: P1,G2,G3,G4 */
  130. { 0x00844008, 0x00111004 }, /* PMC2: P2,G2,G3,G4 */
  131. { 0x00800020, 0x00100010 }, /* PMC3: P3,G4 */
  132. { 0x00840080, 0x00110040 }, /* PMC4: P4,G3,G4 */
  133. { 0x00000200, 0x00000100 }, /* PMC5: P5 */
  134. { 0x00000800, 0x00000400 } /* PMC6: P6 */
  135. };
  136. static u32 classbits[N_CLASSES - 1][2] = {
  137. { 0x00000000, 0x00000000 }, /* class 0: no constraint */
  138. { 0x00800000, 0x00100000 }, /* class 1: G4 */
  139. { 0x00040000, 0x00010000 }, /* class 2: G3 */
  140. { 0x00004000, 0x00001000 }, /* class 3: G2 */
  141. };
  142. static int mpc7450_get_constraint(u64 event, unsigned long *maskp,
  143. unsigned long *valp)
  144. {
  145. int pmc, class;
  146. u32 mask, value;
  147. int thresh, tuse;
  148. class = mpc7450_classify_event(event);
  149. if (class < 0)
  150. return -1;
  151. if (class == 4) {
  152. pmc = ((unsigned int)event >> PM_PMC_SH) & PM_PMC_MSK;
  153. mask = pmcbits[pmc - 1][0];
  154. value = pmcbits[pmc - 1][1];
  155. } else {
  156. mask = classbits[class][0];
  157. value = classbits[class][1];
  158. }
  159. tuse = mpc7450_threshold_use(event);
  160. if (tuse) {
  161. thresh = ((unsigned int)event >> PM_THRESH_SH) & PM_THRESH_MSK;
  162. mask |= 0x3f << 24;
  163. value |= thresh << 24;
  164. if (tuse == 2) {
  165. mask |= 0x40000000;
  166. if ((unsigned int)event & PM_THRMULT_MSKS)
  167. value |= 0x40000000;
  168. }
  169. }
  170. *maskp = mask;
  171. *valp = value;
  172. return 0;
  173. }
  174. static const unsigned int event_alternatives[][MAX_ALT] = {
  175. { 0x217, 0x317 }, /* PM_L1_DCACHE_MISS */
  176. { 0x418, 0x50f, 0x60f }, /* PM_SNOOP_RETRY */
  177. { 0x502, 0x602 }, /* PM_L2_HIT */
  178. { 0x503, 0x603 }, /* PM_L3_HIT */
  179. { 0x504, 0x604 }, /* PM_L2_ICACHE_MISS */
  180. { 0x505, 0x605 }, /* PM_L3_ICACHE_MISS */
  181. { 0x506, 0x606 }, /* PM_L2_DCACHE_MISS */
  182. { 0x507, 0x607 }, /* PM_L3_DCACHE_MISS */
  183. { 0x50a, 0x623 }, /* PM_LD_HIT_L3 */
  184. { 0x50b, 0x624 }, /* PM_ST_HIT_L3 */
  185. { 0x50d, 0x60d }, /* PM_L2_TOUCH_HIT */
  186. { 0x50e, 0x60e }, /* PM_L3_TOUCH_HIT */
  187. { 0x512, 0x612 }, /* PM_INT_LOCAL */
  188. { 0x513, 0x61d }, /* PM_L2_MISS */
  189. { 0x514, 0x61e }, /* PM_L3_MISS */
  190. };
  191. /*
  192. * Scan the alternatives table for a match and return the
  193. * index into the alternatives table if found, else -1.
  194. */
  195. static int find_alternative(u32 event)
  196. {
  197. int i, j;
  198. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  199. if (event < event_alternatives[i][0])
  200. break;
  201. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  202. if (event == event_alternatives[i][j])
  203. return i;
  204. }
  205. return -1;
  206. }
  207. static int mpc7450_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  208. {
  209. int i, j, nalt = 1;
  210. u32 ae;
  211. alt[0] = event;
  212. nalt = 1;
  213. i = find_alternative((u32)event);
  214. if (i >= 0) {
  215. for (j = 0; j < MAX_ALT; ++j) {
  216. ae = event_alternatives[i][j];
  217. if (ae && ae != (u32)event)
  218. alt[nalt++] = ae;
  219. }
  220. }
  221. return nalt;
  222. }
  223. /*
  224. * Bitmaps of which PMCs each class can use for classes 0 - 3.
  225. * Bit i is set if PMC i+1 is usable.
  226. */
  227. static const u8 classmap[N_CLASSES] = {
  228. 0x3f, 0x0f, 0x0b, 0x03, 0
  229. };
  230. /* Bit position and width of each PMCSEL field */
  231. static const int pmcsel_shift[N_COUNTER] = {
  232. 6, 0, 27, 22, 17, 11
  233. };
  234. static const u32 pmcsel_mask[N_COUNTER] = {
  235. 0x7f, 0x3f, 0x1f, 0x1f, 0x1f, 0x3f
  236. };
  237. /*
  238. * Compute MMCR0/1/2 values for a set of events.
  239. */
  240. static int mpc7450_compute_mmcr(u64 event[], int n_ev, unsigned int hwc[],
  241. unsigned long mmcr[],
  242. struct perf_event *pevents[])
  243. {
  244. u8 event_index[N_CLASSES][N_COUNTER];
  245. int n_classevent[N_CLASSES];
  246. int i, j, class, tuse;
  247. u32 pmc_inuse = 0, pmc_avail;
  248. u32 mmcr0 = 0, mmcr1 = 0, mmcr2 = 0;
  249. u32 ev, pmc, thresh;
  250. if (n_ev > N_COUNTER)
  251. return -1;
  252. /* First pass: count usage in each class */
  253. for (i = 0; i < N_CLASSES; ++i)
  254. n_classevent[i] = 0;
  255. for (i = 0; i < n_ev; ++i) {
  256. class = mpc7450_classify_event(event[i]);
  257. if (class < 0)
  258. return -1;
  259. j = n_classevent[class]++;
  260. event_index[class][j] = i;
  261. }
  262. /* Second pass: allocate PMCs from most specific event to least */
  263. for (class = N_CLASSES - 1; class >= 0; --class) {
  264. for (i = 0; i < n_classevent[class]; ++i) {
  265. ev = event[event_index[class][i]];
  266. if (class == 4) {
  267. pmc = (ev >> PM_PMC_SH) & PM_PMC_MSK;
  268. if (pmc_inuse & (1 << (pmc - 1)))
  269. return -1;
  270. } else {
  271. /* Find a suitable PMC */
  272. pmc_avail = classmap[class] & ~pmc_inuse;
  273. if (!pmc_avail)
  274. return -1;
  275. pmc = ffs(pmc_avail);
  276. }
  277. pmc_inuse |= 1 << (pmc - 1);
  278. tuse = mpc7450_threshold_use(ev);
  279. if (tuse) {
  280. thresh = (ev >> PM_THRESH_SH) & PM_THRESH_MSK;
  281. mmcr0 |= thresh << 16;
  282. if (tuse == 2 && (ev & PM_THRMULT_MSKS))
  283. mmcr2 = 0x80000000;
  284. }
  285. ev &= pmcsel_mask[pmc - 1];
  286. ev <<= pmcsel_shift[pmc - 1];
  287. if (pmc <= 2)
  288. mmcr0 |= ev;
  289. else
  290. mmcr1 |= ev;
  291. hwc[event_index[class][i]] = pmc - 1;
  292. }
  293. }
  294. if (pmc_inuse & 1)
  295. mmcr0 |= MMCR0_PMC1CE;
  296. if (pmc_inuse & 0x3e)
  297. mmcr0 |= MMCR0_PMCnCE;
  298. /* Return MMCRx values */
  299. mmcr[0] = mmcr0;
  300. mmcr[1] = mmcr1;
  301. mmcr[2] = mmcr2;
  302. return 0;
  303. }
  304. /*
  305. * Disable counting by a PMC.
  306. * Note that the pmc argument is 0-based here, not 1-based.
  307. */
  308. static void mpc7450_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  309. {
  310. if (pmc <= 1)
  311. mmcr[0] &= ~(pmcsel_mask[pmc] << pmcsel_shift[pmc]);
  312. else
  313. mmcr[1] &= ~(pmcsel_mask[pmc] << pmcsel_shift[pmc]);
  314. }
  315. static int mpc7450_generic_events[] = {
  316. [PERF_COUNT_HW_CPU_CYCLES] = 1,
  317. [PERF_COUNT_HW_INSTRUCTIONS] = 2,
  318. [PERF_COUNT_HW_CACHE_MISSES] = 0x217, /* PM_L1_DCACHE_MISS */
  319. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x122, /* PM_BR_CMPL */
  320. [PERF_COUNT_HW_BRANCH_MISSES] = 0x41c, /* PM_BR_MPRED */
  321. };
  322. #define C(x) PERF_COUNT_HW_CACHE_##x
  323. /*
  324. * Table of generalized cache-related events.
  325. * 0 means not supported, -1 means nonsensical, other values
  326. * are event codes.
  327. */
  328. static int mpc7450_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  329. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  330. [C(OP_READ)] = { 0, 0x225 },
  331. [C(OP_WRITE)] = { 0, 0x227 },
  332. [C(OP_PREFETCH)] = { 0, 0 },
  333. },
  334. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  335. [C(OP_READ)] = { 0x129, 0x115 },
  336. [C(OP_WRITE)] = { -1, -1 },
  337. [C(OP_PREFETCH)] = { 0x634, 0 },
  338. },
  339. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  340. [C(OP_READ)] = { 0, 0 },
  341. [C(OP_WRITE)] = { 0, 0 },
  342. [C(OP_PREFETCH)] = { 0, 0 },
  343. },
  344. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  345. [C(OP_READ)] = { 0, 0x312 },
  346. [C(OP_WRITE)] = { -1, -1 },
  347. [C(OP_PREFETCH)] = { -1, -1 },
  348. },
  349. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  350. [C(OP_READ)] = { 0, 0x223 },
  351. [C(OP_WRITE)] = { -1, -1 },
  352. [C(OP_PREFETCH)] = { -1, -1 },
  353. },
  354. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  355. [C(OP_READ)] = { 0x122, 0x41c },
  356. [C(OP_WRITE)] = { -1, -1 },
  357. [C(OP_PREFETCH)] = { -1, -1 },
  358. },
  359. [C(NODE)] = { /* RESULT_ACCESS RESULT_MISS */
  360. [C(OP_READ)] = { -1, -1 },
  361. [C(OP_WRITE)] = { -1, -1 },
  362. [C(OP_PREFETCH)] = { -1, -1 },
  363. },
  364. };
  365. struct power_pmu mpc7450_pmu = {
  366. .name = "MPC7450 family",
  367. .n_counter = N_COUNTER,
  368. .max_alternatives = MAX_ALT,
  369. .add_fields = 0x00111555ul,
  370. .test_adder = 0x00301000ul,
  371. .compute_mmcr = mpc7450_compute_mmcr,
  372. .get_constraint = mpc7450_get_constraint,
  373. .get_alternatives = mpc7450_get_alternatives,
  374. .disable_pmc = mpc7450_disable_pmc,
  375. .n_generic = ARRAY_SIZE(mpc7450_generic_events),
  376. .generic_events = mpc7450_generic_events,
  377. .cache_events = &mpc7450_cache_events,
  378. };
  379. static int __init init_mpc7450_pmu(void)
  380. {
  381. if (!cur_cpu_spec->oprofile_cpu_type ||
  382. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc/7450"))
  383. return -ENODEV;
  384. return register_power_pmu(&mpc7450_pmu);
  385. }
  386. early_initcall(init_mpc7450_pmu);