blob: c6aa713ddf5f92bf35243f660a6db4725e21d0ff [file] [log] [blame]
Michael Ellermane05b9b92013-04-25 19:28:28 +00001/*
2 * Performance counter support for POWER8 processors.
3 *
4 * Copyright 2009 Paul Mackerras, IBM Corporation.
5 * Copyright 2013 Michael Ellerman, IBM Corporation.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/kernel.h>
14#include <linux/perf_event.h>
15#include <asm/firmware.h>
16
17
18/*
19 * Some power8 event codes.
20 */
21#define PM_CYC 0x0001e
22#define PM_GCT_NOSLOT_CYC 0x100f8
23#define PM_CMPLU_STALL 0x4000a
24#define PM_INST_CMPL 0x00002
25#define PM_BRU_FIN 0x10068
26#define PM_BR_MPRED_CMPL 0x400f6
27
28
29/*
30 * Raw event encoding for POWER8:
31 *
32 * 60 56 52 48 44 40 36 32
33 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
34 * [ thresh_cmp ] [ thresh_ctl ]
35 * |
36 * thresh start/stop OR FAB match -*
37 *
38 * 28 24 20 16 12 8 4 0
39 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
40 * [ ] [ sample ] [cache] [ pmc ] [unit ] c m [ pmcxsel ]
41 * | | | | |
42 * | | | | *- mark
43 * | | *- L1/L2/L3 cache_sel |
44 * | | |
45 * | *- sampling mode for marked events *- combine
46 * |
47 * *- thresh_sel
48 *
49 * Below uses IBM bit numbering.
50 *
51 * MMCR1[x:y] = unit (PMCxUNIT)
52 * MMCR1[x] = combine (PMCxCOMB)
53 *
54 * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
55 * # PM_MRK_FAB_RSP_MATCH
56 * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
57 * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
58 * # PM_MRK_FAB_RSP_MATCH_CYC
59 * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
60 * else
61 * MMCRA[48:55] = thresh_ctl (THRESH START/END)
62 *
63 * if thresh_sel:
64 * MMCRA[45:47] = thresh_sel
65 *
66 * if thresh_cmp:
67 * MMCRA[22:24] = thresh_cmp[0:2]
68 * MMCRA[25:31] = thresh_cmp[3:9]
69 *
70 * if unit == 6 or unit == 7
71 * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
72 * else if unit == 8 or unit == 9:
73 * if cache_sel[0] == 0: # L3 bank
74 * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
75 * else if cache_sel[0] == 1:
76 * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
77 * else if cache_sel[1]: # L1 event
78 * MMCR1[16] = cache_sel[2]
79 * MMCR1[17] = cache_sel[3]
80 *
81 * if mark:
82 * MMCRA[63] = 1 (SAMPLE_ENABLE)
83 * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
84 * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
85 *
86 */
87
88#define EVENT_THR_CMP_SHIFT 40 /* Threshold CMP value */
89#define EVENT_THR_CMP_MASK 0x3ff
90#define EVENT_THR_CTL_SHIFT 32 /* Threshold control value (start/stop) */
91#define EVENT_THR_CTL_MASK 0xffull
92#define EVENT_THR_SEL_SHIFT 29 /* Threshold select value */
93#define EVENT_THR_SEL_MASK 0x7
94#define EVENT_THRESH_SHIFT 29 /* All threshold bits */
95#define EVENT_THRESH_MASK 0x1fffffull
96#define EVENT_SAMPLE_SHIFT 24 /* Sampling mode & eligibility */
97#define EVENT_SAMPLE_MASK 0x1f
98#define EVENT_CACHE_SEL_SHIFT 20 /* L2/L3 cache select */
99#define EVENT_CACHE_SEL_MASK 0xf
100#define EVENT_IS_L1 (4 << EVENT_CACHE_SEL_SHIFT)
101#define EVENT_PMC_SHIFT 16 /* PMC number (1-based) */
102#define EVENT_PMC_MASK 0xf
103#define EVENT_UNIT_SHIFT 12 /* Unit */
104#define EVENT_UNIT_MASK 0xf
105#define EVENT_COMBINE_SHIFT 11 /* Combine bit */
106#define EVENT_COMBINE_MASK 0x1
107#define EVENT_MARKED_SHIFT 8 /* Marked bit */
108#define EVENT_MARKED_MASK 0x1
109#define EVENT_IS_MARKED (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT)
110#define EVENT_PSEL_MASK 0xff /* PMCxSEL value */
111
112/*
113 * Layout of constraint bits:
114 *
115 * 60 56 52 48 44 40 36 32
116 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
117 * [ fab_match ] [ thresh_cmp ] [ thresh_ctl ] [ ]
118 * |
119 * thresh_sel -*
120 *
121 * 28 24 20 16 12 8 4 0
122 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
123 * [ ] [ sample ] [ ] [6] [5] [4] [3] [2] [1]
124 * | |
125 * L1 I/D qualifier -* | Count of events for each PMC.
126 * | p1, p2, p3, p4, p5, p6.
127 * nc - number of counters -*
128 *
129 * The PMC fields P1..P6, and NC, are adder fields. As we accumulate constraints
130 * we want the low bit of each field to be added to any existing value.
131 *
132 * Everything else is a value field.
133 */
134
135#define CNST_FAB_MATCH_VAL(v) (((v) & EVENT_THR_CTL_MASK) << 56)
136#define CNST_FAB_MATCH_MASK CNST_FAB_MATCH_VAL(EVENT_THR_CTL_MASK)
137
138/* We just throw all the threshold bits into the constraint */
139#define CNST_THRESH_VAL(v) (((v) & EVENT_THRESH_MASK) << 32)
140#define CNST_THRESH_MASK CNST_THRESH_VAL(EVENT_THRESH_MASK)
141
142#define CNST_L1_QUAL_VAL(v) (((v) & 3) << 22)
143#define CNST_L1_QUAL_MASK CNST_L1_QUAL_VAL(3)
144
145#define CNST_SAMPLE_VAL(v) (((v) & EVENT_SAMPLE_MASK) << 16)
146#define CNST_SAMPLE_MASK CNST_SAMPLE_VAL(EVENT_SAMPLE_MASK)
147
148/*
149 * For NC we are counting up to 4 events. This requires three bits, and we need
150 * the fifth event to overflow and set the 4th bit. To achieve that we bias the
151 * fields by 3 in test_adder.
152 */
153#define CNST_NC_SHIFT 12
154#define CNST_NC_VAL (1 << CNST_NC_SHIFT)
155#define CNST_NC_MASK (8 << CNST_NC_SHIFT)
156#define POWER8_TEST_ADDER (3 << CNST_NC_SHIFT)
157
158/*
159 * For the per-PMC fields we have two bits. The low bit is added, so if two
160 * events ask for the same PMC the sum will overflow, setting the high bit,
161 * indicating an error. So our mask sets the high bit.
162 */
163#define CNST_PMC_SHIFT(pmc) ((pmc - 1) * 2)
164#define CNST_PMC_VAL(pmc) (1 << CNST_PMC_SHIFT(pmc))
165#define CNST_PMC_MASK(pmc) (2 << CNST_PMC_SHIFT(pmc))
166
167/* Our add_fields is defined as: */
168#define POWER8_ADD_FIELDS \
169 CNST_PMC_VAL(1) | CNST_PMC_VAL(2) | CNST_PMC_VAL(3) | \
170 CNST_PMC_VAL(4) | CNST_PMC_VAL(5) | CNST_PMC_VAL(6) | CNST_NC_VAL
171
172
173/* Bits in MMCR1 for POWER8 */
174#define MMCR1_UNIT_SHIFT(pmc) (60 - (4 * ((pmc) - 1)))
175#define MMCR1_COMBINE_SHIFT(pmc) (35 - ((pmc) - 1))
176#define MMCR1_PMCSEL_SHIFT(pmc) (24 - (((pmc) - 1)) * 8)
177#define MMCR1_DC_QUAL_SHIFT 47
178#define MMCR1_IC_QUAL_SHIFT 46
179
180/* Bits in MMCRA for POWER8 */
181#define MMCRA_SAMP_MODE_SHIFT 1
182#define MMCRA_SAMP_ELIG_SHIFT 4
183#define MMCRA_THR_CTL_SHIFT 8
184#define MMCRA_THR_SEL_SHIFT 16
185#define MMCRA_THR_CMP_SHIFT 32
186#define MMCRA_SDAR_MODE_TLB (1ull << 42)
187
188
189static inline bool event_is_fab_match(u64 event)
190{
191 /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
192 event &= 0xff0fe;
193
194 /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
195 return (event == 0x30056 || event == 0x4f052);
196}
197
198static int power8_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
199{
200 unsigned int unit, pmc, cache;
201 unsigned long mask, value;
202
203 mask = value = 0;
204
205 pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
206 unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
207 cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
208
209 if (pmc) {
210 if (pmc > 6)
211 return -1;
212
213 mask |= CNST_PMC_MASK(pmc);
214 value |= CNST_PMC_VAL(pmc);
215
216 if (pmc >= 5 && event != 0x500fa && event != 0x600f4)
217 return -1;
218 }
219
220 if (pmc <= 4) {
221 /*
222 * Add to number of counters in use. Note this includes events with
223 * a PMC of 0 - they still need a PMC, it's just assigned later.
224 * Don't count events on PMC 5 & 6, there is only one valid event
225 * on each of those counters, and they are handled above.
226 */
227 mask |= CNST_NC_MASK;
228 value |= CNST_NC_VAL;
229 }
230
231 if (unit >= 6 && unit <= 9) {
232 /*
233 * L2/L3 events contain a cache selector field, which is
234 * supposed to be programmed into MMCRC. However MMCRC is only
235 * HV writable, and there is no API for guest kernels to modify
236 * it. The solution is for the hypervisor to initialise the
237 * field to zeroes, and for us to only ever allow events that
238 * have a cache selector of zero.
239 */
240 if (cache)
241 return -1;
242
243 } else if (event & EVENT_IS_L1) {
244 mask |= CNST_L1_QUAL_MASK;
245 value |= CNST_L1_QUAL_VAL(cache);
246 }
247
248 if (event & EVENT_IS_MARKED) {
249 mask |= CNST_SAMPLE_MASK;
250 value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
251 }
252
253 /*
254 * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
255 * the threshold control bits are used for the match value.
256 */
257 if (event_is_fab_match(event)) {
258 mask |= CNST_FAB_MATCH_MASK;
259 value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
260 } else {
261 /*
262 * Check the mantissa upper two bits are not zero, unless the
263 * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
264 */
265 unsigned int cmp, exp;
266
267 cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
268 exp = cmp >> 7;
269
270 if (exp && (cmp & 0x60) == 0)
271 return -1;
272
273 mask |= CNST_THRESH_MASK;
274 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
275 }
276
277 *maskp = mask;
278 *valp = value;
279
280 return 0;
281}
282
283static int power8_compute_mmcr(u64 event[], int n_ev,
284 unsigned int hwc[], unsigned long mmcr[])
285{
286 unsigned long mmcra, mmcr1, unit, combine, psel, cache, val;
287 unsigned int pmc, pmc_inuse;
288 int i;
289
290 pmc_inuse = 0;
291
292 /* First pass to count resource use */
293 for (i = 0; i < n_ev; ++i) {
294 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
295 if (pmc)
296 pmc_inuse |= 1 << pmc;
297 }
298
299 /* In continous sampling mode, update SDAR on TLB miss */
300 mmcra = MMCRA_SDAR_MODE_TLB;
301 mmcr1 = 0;
302
303 /* Second pass: assign PMCs, set all MMCR1 fields */
304 for (i = 0; i < n_ev; ++i) {
305 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
306 unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
307 combine = (event[i] >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK;
308 psel = event[i] & EVENT_PSEL_MASK;
309
310 if (!pmc) {
311 for (pmc = 1; pmc <= 4; ++pmc) {
312 if (!(pmc_inuse & (1 << pmc)))
313 break;
314 }
315
316 pmc_inuse |= 1 << pmc;
317 }
318
319 if (pmc <= 4) {
320 mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
321 mmcr1 |= combine << MMCR1_COMBINE_SHIFT(pmc);
322 mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
323 }
324
325 if (event[i] & EVENT_IS_L1) {
326 cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
327 mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
328 cache >>= 1;
329 mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
330 }
331
332 if (event[i] & EVENT_IS_MARKED) {
333 mmcra |= MMCRA_SAMPLE_ENABLE;
334
335 val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
336 if (val) {
337 mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
338 mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
339 }
340 }
341
342 /*
343 * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
344 * the threshold bits are used for the match value.
345 */
346 if (event_is_fab_match(event[i])) {
347 mmcr1 |= (event[i] >> EVENT_THR_CTL_SHIFT) &
348 EVENT_THR_CTL_MASK;
349 } else {
350 val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
351 mmcra |= val << MMCRA_THR_CTL_SHIFT;
352 val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
353 mmcra |= val << MMCRA_THR_SEL_SHIFT;
354 val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
355 mmcra |= val << MMCRA_THR_CMP_SHIFT;
356 }
357
358 hwc[i] = pmc - 1;
359 }
360
361 /* Return MMCRx values */
362 mmcr[0] = 0;
363
364 /* pmc_inuse is 1-based */
365 if (pmc_inuse & 2)
366 mmcr[0] = MMCR0_PMC1CE;
367
368 if (pmc_inuse & 0x7c)
369 mmcr[0] |= MMCR0_PMCjCE;
370
371 mmcr[1] = mmcr1;
372 mmcr[2] = mmcra;
373
374 return 0;
375}
376
377#define MAX_ALT 2
378
379/* Table of alternatives, sorted by column 0 */
380static const unsigned int event_alternatives[][MAX_ALT] = {
381 { 0x10134, 0x301e2 }, /* PM_MRK_ST_CMPL */
382 { 0x10138, 0x40138 }, /* PM_BR_MRK_2PATH */
383 { 0x18082, 0x3e05e }, /* PM_L3_CO_MEPF */
384 { 0x1d14e, 0x401e8 }, /* PM_MRK_DATA_FROM_L2MISS */
385 { 0x1e054, 0x4000a }, /* PM_CMPLU_STALL */
386 { 0x20036, 0x40036 }, /* PM_BR_2PATH */
387 { 0x200f2, 0x300f2 }, /* PM_INST_DISP */
388 { 0x200f4, 0x600f4 }, /* PM_RUN_CYC */
389 { 0x2013c, 0x3012e }, /* PM_MRK_FILT_MATCH */
390 { 0x3e054, 0x400f0 }, /* PM_LD_MISS_L1 */
391 { 0x400fa, 0x500fa }, /* PM_RUN_INST_CMPL */
392};
393
394/*
395 * Scan the alternatives table for a match and return the
396 * index into the alternatives table if found, else -1.
397 */
398static int find_alternative(u64 event)
399{
400 int i, j;
401
402 for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
403 if (event < event_alternatives[i][0])
404 break;
405
406 for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
407 if (event == event_alternatives[i][j])
408 return i;
409 }
410
411 return -1;
412}
413
414static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
415{
416 int i, j, num_alt = 0;
417 u64 alt_event;
418
419 alt[num_alt++] = event;
420
421 i = find_alternative(event);
422 if (i >= 0) {
423 /* Filter out the original event, it's already in alt[0] */
424 for (j = 0; j < MAX_ALT; ++j) {
425 alt_event = event_alternatives[i][j];
426 if (alt_event && alt_event != event)
427 alt[num_alt++] = alt_event;
428 }
429 }
430
431 if (flags & PPMU_ONLY_COUNT_RUN) {
432 /*
433 * We're only counting in RUN state, so PM_CYC is equivalent to
434 * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
435 */
436 j = num_alt;
437 for (i = 0; i < num_alt; ++i) {
438 switch (alt[i]) {
439 case 0x1e: /* PM_CYC */
440 alt[j++] = 0x600f4; /* PM_RUN_CYC */
441 break;
442 case 0x600f4: /* PM_RUN_CYC */
443 alt[j++] = 0x1e;
444 break;
445 case 0x2: /* PM_PPC_CMPL */
446 alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
447 break;
448 case 0x500fa: /* PM_RUN_INST_CMPL */
449 alt[j++] = 0x2; /* PM_PPC_CMPL */
450 break;
451 }
452 }
453 num_alt = j;
454 }
455
456 return num_alt;
457}
458
459static void power8_disable_pmc(unsigned int pmc, unsigned long mmcr[])
460{
461 if (pmc <= 3)
462 mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
463}
464
465PMU_FORMAT_ATTR(event, "config:0-49");
466PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
467PMU_FORMAT_ATTR(mark, "config:8");
468PMU_FORMAT_ATTR(combine, "config:11");
469PMU_FORMAT_ATTR(unit, "config:12-15");
470PMU_FORMAT_ATTR(pmc, "config:16-19");
471PMU_FORMAT_ATTR(cache_sel, "config:20-23");
472PMU_FORMAT_ATTR(sample_mode, "config:24-28");
473PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
474PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
475PMU_FORMAT_ATTR(thresh_start, "config:36-39");
476PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
477
478static struct attribute *power8_pmu_format_attr[] = {
479 &format_attr_event.attr,
480 &format_attr_pmcxsel.attr,
481 &format_attr_mark.attr,
482 &format_attr_combine.attr,
483 &format_attr_unit.attr,
484 &format_attr_pmc.attr,
485 &format_attr_cache_sel.attr,
486 &format_attr_sample_mode.attr,
487 &format_attr_thresh_sel.attr,
488 &format_attr_thresh_stop.attr,
489 &format_attr_thresh_start.attr,
490 &format_attr_thresh_cmp.attr,
491 NULL,
492};
493
494struct attribute_group power8_pmu_format_group = {
495 .name = "format",
496 .attrs = power8_pmu_format_attr,
497};
498
499static const struct attribute_group *power8_pmu_attr_groups[] = {
500 &power8_pmu_format_group,
501 NULL,
502};
503
504static int power8_generic_events[] = {
505 [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
506 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
507 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
508 [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
509 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
510 [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
511};
512
513static struct power_pmu power8_pmu = {
514 .name = "POWER8",
515 .n_counter = 6,
516 .max_alternatives = MAX_ALT + 1,
517 .add_fields = POWER8_ADD_FIELDS,
518 .test_adder = POWER8_TEST_ADDER,
519 .compute_mmcr = power8_compute_mmcr,
520 .get_constraint = power8_get_constraint,
521 .get_alternatives = power8_get_alternatives,
522 .disable_pmc = power8_disable_pmc,
523 .flags = PPMU_HAS_SSLOT | PPMU_HAS_SIER,
524 .n_generic = ARRAY_SIZE(power8_generic_events),
525 .generic_events = power8_generic_events,
526 .attr_groups = power8_pmu_attr_groups,
527};
528
529static int __init init_power8_pmu(void)
530{
531 if (!cur_cpu_spec->oprofile_cpu_type ||
532 strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
533 return -ENODEV;
534
535 return register_power_pmu(&power8_pmu);
536}
537early_initcall(init_power8_pmu);