2 * Performance counter support for MPC7450-family processors.
4 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
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.
11 #include <linux/string.h>
12 #include <linux/perf_event.h>
14 #include <asm/cputable.h>
16 #define N_COUNTER 6 /* Number of hardware counters */
17 #define MAX_ALT 3 /* Maximum number of event alternative codes */
20 * Bits in event code for MPC7450 family
22 #define PM_THRMULT_MSKS 0x40000
23 #define PM_THRESH_SH 12
24 #define PM_THRESH_MSK 0x3f
27 #define PM_PMCSEL_MSK 0x7f
30 * Classify events according to how specific their PMC requirements are.
32 * 0: can go on any PMC
33 * 1: can go on PMCs 1-4
34 * 2: can go on PMCs 1,2,4
35 * 3: can go on PMCs 1 or 2
36 * 4: can only go on one PMC
37 * -1: event code is invalid
41 static int mpc7450_classify_event(u32 event)
45 pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
51 event &= PM_PMCSEL_MSK;
64 * Events using threshold and possible threshold scale:
66 * 11e N PM_INSTQ_EXCEED_CYC
67 * 11f N PM_ALTV_IQ_EXCEED_CYC
68 * 128 Y PM_DTLB_SEARCH_EXCEED_CYC
69 * 12b Y PM_LD_MISS_EXCEED_L1_CYC
70 * 220 N PM_CQ_EXCEED_CYC
71 * 30c N PM_GPR_RB_EXCEED_CYC
72 * 30d ? PM_FPR_IQ_EXCEED_CYC ?
73 * 311 Y PM_ITLB_SEARCH_EXCEED
74 * 410 N PM_GPR_IQ_EXCEED_CYC
78 * Return use of threshold and threshold scale bits:
79 * 0 = uses neither, 1 = uses threshold, 2 = uses both
81 static int mpc7450_threshold_use(u32 event)
85 pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
86 sel = event & PM_PMCSEL_MSK;
89 if (sel == 0x1e || sel == 0x1f)
91 if (sel == 0x28 || sel == 0x2b)
99 if (sel == 0xc || sel == 0xd)
113 * Layout of constraint bits:
114 * 33222222222211111111110000000000
115 * 10987654321098765432109876543210
116 * |< >< > < > < ><><><><><><>
117 * TS TV G4 G3 G2P6P5P4P3P2P1
120 * 0 - 11: Count of events needing PMC1 .. PMC6
123 * 12 - 14: Count of events needing PMC1 or PMC2
126 * 16 - 18: Count of events needing PMC1, PMC2 or PMC4
129 * 20 - 23: Count of events needing PMC1, PMC2, PMC3 or PMC4
132 * 24 - 29: Threshold value requested
135 * 30: Threshold scale value requested
138 static u32 pmcbits[N_COUNTER][2] = {
139 { 0x00844002, 0x00111001 }, /* PMC1 mask, value: P1,G2,G3,G4 */
140 { 0x00844008, 0x00111004 }, /* PMC2: P2,G2,G3,G4 */
141 { 0x00800020, 0x00100010 }, /* PMC3: P3,G4 */
142 { 0x00840080, 0x00110040 }, /* PMC4: P4,G3,G4 */
143 { 0x00000200, 0x00000100 }, /* PMC5: P5 */
144 { 0x00000800, 0x00000400 } /* PMC6: P6 */
147 static u32 classbits[N_CLASSES - 1][2] = {
148 { 0x00000000, 0x00000000 }, /* class 0: no constraint */
149 { 0x00800000, 0x00100000 }, /* class 1: G4 */
150 { 0x00040000, 0x00010000 }, /* class 2: G3 */
151 { 0x00004000, 0x00001000 }, /* class 3: G2 */
154 static int mpc7450_get_constraint(u64 event, unsigned long *maskp,
161 class = mpc7450_classify_event(event);
165 pmc = ((unsigned int)event >> PM_PMC_SH) & PM_PMC_MSK;
166 mask = pmcbits[pmc - 1][0];
167 value = pmcbits[pmc - 1][1];
169 mask = classbits[class][0];
170 value = classbits[class][1];
173 tuse = mpc7450_threshold_use(event);
175 thresh = ((unsigned int)event >> PM_THRESH_SH) & PM_THRESH_MSK;
177 value |= thresh << 24;
180 if ((unsigned int)event & PM_THRMULT_MSKS)
190 static const unsigned int event_alternatives[][MAX_ALT] = {
191 { 0x217, 0x317 }, /* PM_L1_DCACHE_MISS */
192 { 0x418, 0x50f, 0x60f }, /* PM_SNOOP_RETRY */
193 { 0x502, 0x602 }, /* PM_L2_HIT */
194 { 0x503, 0x603 }, /* PM_L3_HIT */
195 { 0x504, 0x604 }, /* PM_L2_ICACHE_MISS */
196 { 0x505, 0x605 }, /* PM_L3_ICACHE_MISS */
197 { 0x506, 0x606 }, /* PM_L2_DCACHE_MISS */
198 { 0x507, 0x607 }, /* PM_L3_DCACHE_MISS */
199 { 0x50a, 0x623 }, /* PM_LD_HIT_L3 */
200 { 0x50b, 0x624 }, /* PM_ST_HIT_L3 */
201 { 0x50d, 0x60d }, /* PM_L2_TOUCH_HIT */
202 { 0x50e, 0x60e }, /* PM_L3_TOUCH_HIT */
203 { 0x512, 0x612 }, /* PM_INT_LOCAL */
204 { 0x513, 0x61d }, /* PM_L2_MISS */
205 { 0x514, 0x61e }, /* PM_L3_MISS */
209 * Scan the alternatives table for a match and return the
210 * index into the alternatives table if found, else -1.
212 static int find_alternative(u32 event)
216 for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
217 if (event < event_alternatives[i][0])
219 for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
220 if (event == event_alternatives[i][j])
226 static int mpc7450_get_alternatives(u64 event, unsigned int flags, u64 alt[])
233 i = find_alternative((u32)event);
235 for (j = 0; j < MAX_ALT; ++j) {
236 ae = event_alternatives[i][j];
237 if (ae && ae != (u32)event)
245 * Bitmaps of which PMCs each class can use for classes 0 - 3.
246 * Bit i is set if PMC i+1 is usable.
248 static const u8 classmap[N_CLASSES] = {
249 0x3f, 0x0f, 0x0b, 0x03, 0
252 /* Bit position and width of each PMCSEL field */
253 static const int pmcsel_shift[N_COUNTER] = {
256 static const u32 pmcsel_mask[N_COUNTER] = {
257 0x7f, 0x3f, 0x1f, 0x1f, 0x1f, 0x3f
261 * Compute MMCR0/1/2 values for a set of events.
263 static int mpc7450_compute_mmcr(u64 event[], int n_ev,
264 unsigned int hwc[], unsigned long mmcr[])
266 u8 event_index[N_CLASSES][N_COUNTER];
267 int n_classevent[N_CLASSES];
268 int i, j, class, tuse;
269 u32 pmc_inuse = 0, pmc_avail;
270 u32 mmcr0 = 0, mmcr1 = 0, mmcr2 = 0;
273 if (n_ev > N_COUNTER)
276 /* First pass: count usage in each class */
277 for (i = 0; i < N_CLASSES; ++i)
279 for (i = 0; i < n_ev; ++i) {
280 class = mpc7450_classify_event(event[i]);
283 j = n_classevent[class]++;
284 event_index[class][j] = i;
287 /* Second pass: allocate PMCs from most specific event to least */
288 for (class = N_CLASSES - 1; class >= 0; --class) {
289 for (i = 0; i < n_classevent[class]; ++i) {
290 ev = event[event_index[class][i]];
292 pmc = (ev >> PM_PMC_SH) & PM_PMC_MSK;
293 if (pmc_inuse & (1 << (pmc - 1)))
296 /* Find a suitable PMC */
297 pmc_avail = classmap[class] & ~pmc_inuse;
300 pmc = ffs(pmc_avail);
302 pmc_inuse |= 1 << (pmc - 1);
304 tuse = mpc7450_threshold_use(ev);
306 thresh = (ev >> PM_THRESH_SH) & PM_THRESH_MSK;
307 mmcr0 |= thresh << 16;
308 if (tuse == 2 && (ev & PM_THRMULT_MSKS))
311 ev &= pmcsel_mask[pmc - 1];
312 ev <<= pmcsel_shift[pmc - 1];
317 hwc[event_index[class][i]] = pmc - 1;
322 mmcr0 |= MMCR0_PMC1CE;
323 if (pmc_inuse & 0x3e)
324 mmcr0 |= MMCR0_PMCnCE;
326 /* Return MMCRx values */
334 * Disable counting by a PMC.
335 * Note that the pmc argument is 0-based here, not 1-based.
337 static void mpc7450_disable_pmc(unsigned int pmc, unsigned long mmcr[])
340 mmcr[0] &= ~(pmcsel_mask[pmc] << pmcsel_shift[pmc]);
342 mmcr[1] &= ~(pmcsel_mask[pmc] << pmcsel_shift[pmc]);
345 static int mpc7450_generic_events[] = {
346 [PERF_COUNT_HW_CPU_CYCLES] = 1,
347 [PERF_COUNT_HW_INSTRUCTIONS] = 2,
348 [PERF_COUNT_HW_CACHE_MISSES] = 0x217, /* PM_L1_DCACHE_MISS */
349 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x122, /* PM_BR_CMPL */
350 [PERF_COUNT_HW_BRANCH_MISSES] = 0x41c, /* PM_BR_MPRED */
353 #define C(x) PERF_COUNT_HW_CACHE_##x
356 * Table of generalized cache-related events.
357 * 0 means not supported, -1 means nonsensical, other values
360 static int mpc7450_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
361 [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
362 [C(OP_READ)] = { 0, 0x225 },
363 [C(OP_WRITE)] = { 0, 0x227 },
364 [C(OP_PREFETCH)] = { 0, 0 },
366 [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
367 [C(OP_READ)] = { 0x129, 0x115 },
368 [C(OP_WRITE)] = { -1, -1 },
369 [C(OP_PREFETCH)] = { 0x634, 0 },
371 [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
372 [C(OP_READ)] = { 0, 0 },
373 [C(OP_WRITE)] = { 0, 0 },
374 [C(OP_PREFETCH)] = { 0, 0 },
376 [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
377 [C(OP_READ)] = { 0, 0x312 },
378 [C(OP_WRITE)] = { -1, -1 },
379 [C(OP_PREFETCH)] = { -1, -1 },
381 [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
382 [C(OP_READ)] = { 0, 0x223 },
383 [C(OP_WRITE)] = { -1, -1 },
384 [C(OP_PREFETCH)] = { -1, -1 },
386 [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
387 [C(OP_READ)] = { 0x122, 0x41c },
388 [C(OP_WRITE)] = { -1, -1 },
389 [C(OP_PREFETCH)] = { -1, -1 },
393 struct power_pmu mpc7450_pmu = {
394 .name = "MPC7450 family",
395 .n_counter = N_COUNTER,
396 .max_alternatives = MAX_ALT,
397 .add_fields = 0x00111555ul,
398 .test_adder = 0x00301000ul,
399 .compute_mmcr = mpc7450_compute_mmcr,
400 .get_constraint = mpc7450_get_constraint,
401 .get_alternatives = mpc7450_get_alternatives,
402 .disable_pmc = mpc7450_disable_pmc,
403 .n_generic = ARRAY_SIZE(mpc7450_generic_events),
404 .generic_events = mpc7450_generic_events,
405 .cache_events = &mpc7450_cache_events,
408 static int init_mpc7450_pmu(void)
410 if (!cur_cpu_spec->oprofile_cpu_type ||
411 strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc/7450"))
414 return register_power_pmu(&mpc7450_pmu);
417 early_initcall(init_mpc7450_pmu);