mpc7450-pmu.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_counter.h>
  13. #include <linux/string.h>
  14. #include <asm/reg.h>
  15. #include <asm/cputable.h>
  16. #define N_COUNTER 6 /* Number of hardware counters */
  17. #define MAX_ALT 3 /* Maximum number of event alternative codes */
  18. /*
  19. * Bits in event code for MPC7450 family
  20. */
  21. #define PM_THRMULT_MSKS 0x40000
  22. #define PM_THRESH_SH 12
  23. #define PM_THRESH_MSK 0x3f
  24. #define PM_PMC_SH 8
  25. #define PM_PMC_MSK 7
  26. #define PM_PMCSEL_MSK 0x7f
  27. /*
  28. * Classify events according to how specific their PMC requirements are.
  29. * Result is:
  30. * 0: can go on any PMC
  31. * 1: can go on PMCs 1-4
  32. * 2: can go on PMCs 1,2,4
  33. * 3: can go on PMCs 1 or 2
  34. * 4: can only go on one PMC
  35. * -1: event code is invalid
  36. */
  37. #define N_CLASSES 5
  38. static int mpc7450_classify_event(u32 event)
  39. {
  40. int pmc;
  41. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  42. if (pmc) {
  43. if (pmc > N_COUNTER)
  44. return -1;
  45. return 4;
  46. }
  47. event &= PM_PMCSEL_MSK;
  48. if (event <= 1)
  49. return 0;
  50. if (event <= 7)
  51. return 1;
  52. if (event <= 13)
  53. return 2;
  54. if (event <= 22)
  55. return 3;
  56. return -1;
  57. }
  58. /*
  59. * Events using threshold and possible threshold scale:
  60. * code scale? name
  61. * 11e N PM_INSTQ_EXCEED_CYC
  62. * 11f N PM_ALTV_IQ_EXCEED_CYC
  63. * 128 Y PM_DTLB_SEARCH_EXCEED_CYC
  64. * 12b Y PM_LD_MISS_EXCEED_L1_CYC
  65. * 220 N PM_CQ_EXCEED_CYC
  66. * 30c N PM_GPR_RB_EXCEED_CYC
  67. * 30d ? PM_FPR_IQ_EXCEED_CYC ?
  68. * 311 Y PM_ITLB_SEARCH_EXCEED
  69. * 410 N PM_GPR_IQ_EXCEED_CYC
  70. */
  71. /*
  72. * Return use of threshold and threshold scale bits:
  73. * 0 = uses neither, 1 = uses threshold, 2 = uses both
  74. */
  75. static int mpc7450_threshold_use(u32 event)
  76. {
  77. int pmc, sel;
  78. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  79. sel = event & PM_PMCSEL_MSK;
  80. switch (pmc) {
  81. case 1:
  82. if (sel == 0x1e || sel == 0x1f)
  83. return 1;
  84. if (sel == 0x28 || sel == 0x2b)
  85. return 2;
  86. break;
  87. case 2:
  88. if (sel == 0x20)
  89. return 1;
  90. break;
  91. case 3:
  92. if (sel == 0xc || sel == 0xd)
  93. return 1;
  94. if (sel == 0x11)
  95. return 2;
  96. break;
  97. case 4:
  98. if (sel == 0x10)
  99. return 1;
  100. break;
  101. }
  102. return 0;
  103. }
  104. /*
  105. * Layout of constraint bits:
  106. * 33222222222211111111110000000000
  107. * 10987654321098765432109876543210
  108. * |< >< > < > < ><><><><><><>
  109. * TS TV G4 G3 G2P6P5P4P3P2P1
  110. *
  111. * P1 - P6
  112. * 0 - 11: Count of events needing PMC1 .. PMC6
  113. *
  114. * G2
  115. * 12 - 14: Count of events needing PMC1 or PMC2
  116. *
  117. * G3
  118. * 16 - 18: Count of events needing PMC1, PMC2 or PMC4
  119. *
  120. * G4
  121. * 20 - 23: Count of events needing PMC1, PMC2, PMC3 or PMC4
  122. *
  123. * TV
  124. * 24 - 29: Threshold value requested
  125. *
  126. * TS
  127. * 30: Threshold scale value requested
  128. */
  129. static u32 pmcbits[N_COUNTER][2] = {
  130. { 0x00844002, 0x00111001 }, /* PMC1 mask, value: P1,G2,G3,G4 */
  131. { 0x00844008, 0x00111004 }, /* PMC2: P2,G2,G3,G4 */
  132. { 0x00800020, 0x00100010 }, /* PMC3: P3,G4 */
  133. { 0x00840080, 0x00110040 }, /* PMC4: P4,G3,G4 */
  134. { 0x00000200, 0x00000100 }, /* PMC5: P5 */
  135. { 0x00000800, 0x00000400 } /* PMC6: P6 */
  136. };
  137. static u32 classbits[N_CLASSES - 1][2] = {
  138. { 0x00000000, 0x00000000 }, /* class 0: no constraint */
  139. { 0x00800000, 0x00100000 }, /* class 1: G4 */
  140. { 0x00040000, 0x00010000 }, /* class 2: G3 */
  141. { 0x00004000, 0x00001000 }, /* class 3: G2 */
  142. };
  143. static int mpc7450_get_constraint(u64 event, unsigned long *maskp,
  144. unsigned long *valp)
  145. {
  146. int pmc, class;
  147. u32 mask, value;
  148. int thresh, tuse;
  149. class = mpc7450_classify_event(event);
  150. if (class < 0)
  151. return -1;
  152. if (class == 4) {
  153. pmc = ((unsigned int)event >> PM_PMC_SH) & PM_PMC_MSK;
  154. mask = pmcbits[pmc - 1][0];
  155. value = pmcbits[pmc - 1][1];
  156. } else {
  157. mask = classbits[class][0];
  158. value = classbits[class][1];
  159. }
  160. tuse = mpc7450_threshold_use(event);
  161. if (tuse) {
  162. thresh = ((unsigned int)event >> PM_THRESH_SH) & PM_THRESH_MSK;
  163. mask |= 0x3f << 24;
  164. value |= thresh << 24;
  165. if (tuse == 2) {
  166. mask |= 0x40000000;
  167. if ((unsigned int)event & PM_THRMULT_MSKS)
  168. value |= 0x40000000;
  169. }
  170. }
  171. *maskp = mask;
  172. *valp = value;
  173. return 0;
  174. }
  175. static const unsigned int event_alternatives[][MAX_ALT] = {
  176. { 0x217, 0x317 }, /* PM_L1_DCACHE_MISS */
  177. { 0x418, 0x50f, 0x60f }, /* PM_SNOOP_RETRY */
  178. { 0x502, 0x602 }, /* PM_L2_HIT */
  179. { 0x503, 0x603 }, /* PM_L3_HIT */
  180. { 0x504, 0x604 }, /* PM_L2_ICACHE_MISS */
  181. { 0x505, 0x605 }, /* PM_L3_ICACHE_MISS */
  182. { 0x506, 0x606 }, /* PM_L2_DCACHE_MISS */
  183. { 0x507, 0x607 }, /* PM_L3_DCACHE_MISS */
  184. { 0x50a, 0x623 }, /* PM_LD_HIT_L3 */
  185. { 0x50b, 0x624 }, /* PM_ST_HIT_L3 */
  186. { 0x50d, 0x60d }, /* PM_L2_TOUCH_HIT */
  187. { 0x50e, 0x60e }, /* PM_L3_TOUCH_HIT */
  188. { 0x512, 0x612 }, /* PM_INT_LOCAL */
  189. { 0x513, 0x61d }, /* PM_L2_MISS */
  190. { 0x514, 0x61e }, /* PM_L3_MISS */
  191. };
  192. /*
  193. * Scan the alternatives table for a match and return the
  194. * index into the alternatives table if found, else -1.
  195. */
  196. static int find_alternative(u32 event)
  197. {
  198. int i, j;
  199. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  200. if (event < event_alternatives[i][0])
  201. break;
  202. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  203. if (event == event_alternatives[i][j])
  204. return i;
  205. }
  206. return -1;
  207. }
  208. static int mpc7450_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  209. {
  210. int i, j, nalt = 1;
  211. u32 ae;
  212. alt[0] = event;
  213. nalt = 1;
  214. i = find_alternative((u32)event);
  215. if (i >= 0) {
  216. for (j = 0; j < MAX_ALT; ++j) {
  217. ae = event_alternatives[i][j];
  218. if (ae && ae != (u32)event)
  219. alt[nalt++] = ae;
  220. }
  221. }
  222. return nalt;
  223. }
  224. /*
  225. * Bitmaps of which PMCs each class can use for classes 0 - 3.
  226. * Bit i is set if PMC i+1 is usable.
  227. */
  228. static const u8 classmap[N_CLASSES] = {
  229. 0x3f, 0x0f, 0x0b, 0x03, 0
  230. };
  231. /* Bit position and width of each PMCSEL field */
  232. static const int pmcsel_shift[N_COUNTER] = {
  233. 6, 0, 27, 22, 17, 11
  234. };
  235. static const u32 pmcsel_mask[N_COUNTER] = {
  236. 0x7f, 0x3f, 0x1f, 0x1f, 0x1f, 0x3f
  237. };
  238. /*
  239. * Compute MMCR0/1/2 values for a set of events.
  240. */
  241. static int mpc7450_compute_mmcr(u64 event[], int n_ev,
  242. unsigned int hwc[], unsigned long mmcr[])
  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. };
  360. struct power_pmu mpc7450_pmu = {
  361. .name = "MPC7450 family",
  362. .n_counter = N_COUNTER,
  363. .max_alternatives = MAX_ALT,
  364. .add_fields = 0x00111555ul,
  365. .test_adder = 0x00301000ul,
  366. .compute_mmcr = mpc7450_compute_mmcr,
  367. .get_constraint = mpc7450_get_constraint,
  368. .get_alternatives = mpc7450_get_alternatives,
  369. .disable_pmc = mpc7450_disable_pmc,
  370. .n_generic = ARRAY_SIZE(mpc7450_generic_events),
  371. .generic_events = mpc7450_generic_events,
  372. .cache_events = &mpc7450_cache_events,
  373. };
  374. static int init_mpc7450_pmu(void)
  375. {
  376. if (strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc/7450"))
  377. return -ENODEV;
  378. return register_power_pmu(&mpc7450_pmu);
  379. }
  380. arch_initcall(init_mpc7450_pmu);