mpc7450-pmu.c 10 KB

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