blob: 64f04cfabd23643f25e17e8e05e69fb2d6aba91a [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
Michael Ellermanc2e37a22014-03-14 16:00:29 +110013#define pr_fmt(fmt) "power8-pmu: " fmt
14
Michael Ellermane05b9b92013-04-25 19:28:28 +000015#include <linux/kernel.h>
16#include <linux/perf_event.h>
17#include <asm/firmware.h>
18
19
20/*
21 * Some power8 event codes.
22 */
23#define PM_CYC 0x0001e
24#define PM_GCT_NOSLOT_CYC 0x100f8
25#define PM_CMPLU_STALL 0x4000a
26#define PM_INST_CMPL 0x00002
27#define PM_BRU_FIN 0x10068
28#define PM_BR_MPRED_CMPL 0x400f6
29
Michael Ellerman2fdd3132014-01-24 15:50:51 +110030/* All L1 D cache load references counted at finish, gated by reject */
31#define PM_LD_REF_L1 0x100ee
32/* Load Missed L1 */
33#define PM_LD_MISS_L1 0x3e054
34/* Store Missed L1 */
35#define PM_ST_MISS_L1 0x300f0
36/* L1 cache data prefetches */
37#define PM_L1_PREF 0x0d8b8
38/* Instruction fetches from L1 */
39#define PM_INST_FROM_L1 0x04080
40/* Demand iCache Miss */
41#define PM_L1_ICACHE_MISS 0x200fd
42/* Instruction Demand sectors wriittent into IL1 */
43#define PM_L1_DEMAND_WRITE 0x0408c
44/* Instruction prefetch written into IL1 */
45#define PM_IC_PREF_WRITE 0x0408e
46/* The data cache was reloaded from local core's L3 due to a demand load */
47#define PM_DATA_FROM_L3 0x4c042
48/* Demand LD - L3 Miss (not L2 hit and not L3 hit) */
49#define PM_DATA_FROM_L3MISS 0x300fe
50/* All successful D-side store dispatches for this thread */
51#define PM_L2_ST 0x17080
52/* All successful D-side store dispatches for this thread that were L2 Miss */
53#define PM_L2_ST_MISS 0x17082
54/* Total HW L3 prefetches(Load+store) */
55#define PM_L3_PREF_ALL 0x4e052
56/* Data PTEG reload */
57#define PM_DTLB_MISS 0x300fc
58/* ITLB Reloaded */
59#define PM_ITLB_MISS 0x400fc
60
Michael Ellermane05b9b92013-04-25 19:28:28 +000061
62/*
63 * Raw event encoding for POWER8:
64 *
65 * 60 56 52 48 44 40 36 32
66 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
Michael Ellerman4df48992013-06-28 18:15:17 +100067 * | [ thresh_cmp ] [ thresh_ctl ]
68 * | |
69 * *- EBB (Linux) thresh start/stop OR FAB match -*
Michael Ellermane05b9b92013-04-25 19:28:28 +000070 *
71 * 28 24 20 16 12 8 4 0
72 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
73 * [ ] [ sample ] [cache] [ pmc ] [unit ] c m [ pmcxsel ]
74 * | | | | |
75 * | | | | *- mark
76 * | | *- L1/L2/L3 cache_sel |
77 * | | |
78 * | *- sampling mode for marked events *- combine
79 * |
80 * *- thresh_sel
81 *
82 * Below uses IBM bit numbering.
83 *
84 * MMCR1[x:y] = unit (PMCxUNIT)
85 * MMCR1[x] = combine (PMCxCOMB)
86 *
87 * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
88 * # PM_MRK_FAB_RSP_MATCH
89 * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
90 * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
91 * # PM_MRK_FAB_RSP_MATCH_CYC
92 * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
93 * else
94 * MMCRA[48:55] = thresh_ctl (THRESH START/END)
95 *
96 * if thresh_sel:
97 * MMCRA[45:47] = thresh_sel
98 *
99 * if thresh_cmp:
100 * MMCRA[22:24] = thresh_cmp[0:2]
101 * MMCRA[25:31] = thresh_cmp[3:9]
102 *
103 * if unit == 6 or unit == 7
104 * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
105 * else if unit == 8 or unit == 9:
106 * if cache_sel[0] == 0: # L3 bank
107 * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
108 * else if cache_sel[0] == 1:
109 * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
110 * else if cache_sel[1]: # L1 event
111 * MMCR1[16] = cache_sel[2]
112 * MMCR1[17] = cache_sel[3]
113 *
114 * if mark:
115 * MMCRA[63] = 1 (SAMPLE_ENABLE)
116 * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
117 * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
118 *
119 */
120
Michael Ellerman4df48992013-06-28 18:15:17 +1000121#define EVENT_EBB_MASK 1ull
Michael Ellermane05b9b92013-04-25 19:28:28 +0000122#define EVENT_THR_CMP_SHIFT 40 /* Threshold CMP value */
123#define EVENT_THR_CMP_MASK 0x3ff
124#define EVENT_THR_CTL_SHIFT 32 /* Threshold control value (start/stop) */
125#define EVENT_THR_CTL_MASK 0xffull
126#define EVENT_THR_SEL_SHIFT 29 /* Threshold select value */
127#define EVENT_THR_SEL_MASK 0x7
128#define EVENT_THRESH_SHIFT 29 /* All threshold bits */
129#define EVENT_THRESH_MASK 0x1fffffull
130#define EVENT_SAMPLE_SHIFT 24 /* Sampling mode & eligibility */
131#define EVENT_SAMPLE_MASK 0x1f
132#define EVENT_CACHE_SEL_SHIFT 20 /* L2/L3 cache select */
133#define EVENT_CACHE_SEL_MASK 0xf
134#define EVENT_IS_L1 (4 << EVENT_CACHE_SEL_SHIFT)
135#define EVENT_PMC_SHIFT 16 /* PMC number (1-based) */
136#define EVENT_PMC_MASK 0xf
137#define EVENT_UNIT_SHIFT 12 /* Unit */
138#define EVENT_UNIT_MASK 0xf
139#define EVENT_COMBINE_SHIFT 11 /* Combine bit */
140#define EVENT_COMBINE_MASK 0x1
141#define EVENT_MARKED_SHIFT 8 /* Marked bit */
142#define EVENT_MARKED_MASK 0x1
143#define EVENT_IS_MARKED (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT)
144#define EVENT_PSEL_MASK 0xff /* PMCxSEL value */
145
Michael Ellermand8bec4c2013-06-28 18:15:10 +1000146#define EVENT_VALID_MASK \
147 ((EVENT_THRESH_MASK << EVENT_THRESH_SHIFT) | \
148 (EVENT_SAMPLE_MASK << EVENT_SAMPLE_SHIFT) | \
149 (EVENT_CACHE_SEL_MASK << EVENT_CACHE_SEL_SHIFT) | \
150 (EVENT_PMC_MASK << EVENT_PMC_SHIFT) | \
151 (EVENT_UNIT_MASK << EVENT_UNIT_SHIFT) | \
152 (EVENT_COMBINE_MASK << EVENT_COMBINE_SHIFT) | \
153 (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT) | \
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000154 (EVENT_EBB_MASK << PERF_EVENT_CONFIG_EBB_SHIFT) | \
Michael Ellermand8bec4c2013-06-28 18:15:10 +1000155 EVENT_PSEL_MASK)
156
Anshuman Khandualb1113552013-04-22 19:42:43 +0000157/* MMCRA IFM bits - POWER8 */
158#define POWER8_MMCRA_IFM1 0x0000000040000000UL
159#define POWER8_MMCRA_IFM2 0x0000000080000000UL
160#define POWER8_MMCRA_IFM3 0x00000000C0000000UL
161
162#define ONLY_PLM \
163 (PERF_SAMPLE_BRANCH_USER |\
164 PERF_SAMPLE_BRANCH_KERNEL |\
165 PERF_SAMPLE_BRANCH_HV)
166
Michael Ellermane05b9b92013-04-25 19:28:28 +0000167/*
168 * Layout of constraint bits:
169 *
170 * 60 56 52 48 44 40 36 32
171 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
172 * [ fab_match ] [ thresh_cmp ] [ thresh_ctl ] [ ]
173 * |
174 * thresh_sel -*
175 *
176 * 28 24 20 16 12 8 4 0
177 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
Michael Ellerman4df48992013-06-28 18:15:17 +1000178 * | [ ] [ sample ] [ ] [6] [5] [4] [3] [2] [1]
179 * EBB -* | |
180 * | | Count of events for each PMC.
181 * L1 I/D qualifier -* | p1, p2, p3, p4, p5, p6.
Michael Ellermane05b9b92013-04-25 19:28:28 +0000182 * nc - number of counters -*
183 *
184 * The PMC fields P1..P6, and NC, are adder fields. As we accumulate constraints
185 * we want the low bit of each field to be added to any existing value.
186 *
187 * Everything else is a value field.
188 */
189
190#define CNST_FAB_MATCH_VAL(v) (((v) & EVENT_THR_CTL_MASK) << 56)
191#define CNST_FAB_MATCH_MASK CNST_FAB_MATCH_VAL(EVENT_THR_CTL_MASK)
192
193/* We just throw all the threshold bits into the constraint */
194#define CNST_THRESH_VAL(v) (((v) & EVENT_THRESH_MASK) << 32)
195#define CNST_THRESH_MASK CNST_THRESH_VAL(EVENT_THRESH_MASK)
196
Michael Ellerman4df48992013-06-28 18:15:17 +1000197#define CNST_EBB_VAL(v) (((v) & EVENT_EBB_MASK) << 24)
198#define CNST_EBB_MASK CNST_EBB_VAL(EVENT_EBB_MASK)
199
Michael Ellermane05b9b92013-04-25 19:28:28 +0000200#define CNST_L1_QUAL_VAL(v) (((v) & 3) << 22)
201#define CNST_L1_QUAL_MASK CNST_L1_QUAL_VAL(3)
202
203#define CNST_SAMPLE_VAL(v) (((v) & EVENT_SAMPLE_MASK) << 16)
204#define CNST_SAMPLE_MASK CNST_SAMPLE_VAL(EVENT_SAMPLE_MASK)
205
206/*
207 * For NC we are counting up to 4 events. This requires three bits, and we need
208 * the fifth event to overflow and set the 4th bit. To achieve that we bias the
209 * fields by 3 in test_adder.
210 */
211#define CNST_NC_SHIFT 12
212#define CNST_NC_VAL (1 << CNST_NC_SHIFT)
213#define CNST_NC_MASK (8 << CNST_NC_SHIFT)
214#define POWER8_TEST_ADDER (3 << CNST_NC_SHIFT)
215
216/*
217 * For the per-PMC fields we have two bits. The low bit is added, so if two
218 * events ask for the same PMC the sum will overflow, setting the high bit,
219 * indicating an error. So our mask sets the high bit.
220 */
221#define CNST_PMC_SHIFT(pmc) ((pmc - 1) * 2)
222#define CNST_PMC_VAL(pmc) (1 << CNST_PMC_SHIFT(pmc))
223#define CNST_PMC_MASK(pmc) (2 << CNST_PMC_SHIFT(pmc))
224
225/* Our add_fields is defined as: */
226#define POWER8_ADD_FIELDS \
227 CNST_PMC_VAL(1) | CNST_PMC_VAL(2) | CNST_PMC_VAL(3) | \
228 CNST_PMC_VAL(4) | CNST_PMC_VAL(5) | CNST_PMC_VAL(6) | CNST_NC_VAL
229
230
231/* Bits in MMCR1 for POWER8 */
232#define MMCR1_UNIT_SHIFT(pmc) (60 - (4 * ((pmc) - 1)))
233#define MMCR1_COMBINE_SHIFT(pmc) (35 - ((pmc) - 1))
234#define MMCR1_PMCSEL_SHIFT(pmc) (24 - (((pmc) - 1)) * 8)
Michael Ellermana53b27b2013-10-02 18:04:06 +1000235#define MMCR1_FAB_SHIFT 36
Michael Ellermane05b9b92013-04-25 19:28:28 +0000236#define MMCR1_DC_QUAL_SHIFT 47
237#define MMCR1_IC_QUAL_SHIFT 46
238
239/* Bits in MMCRA for POWER8 */
240#define MMCRA_SAMP_MODE_SHIFT 1
241#define MMCRA_SAMP_ELIG_SHIFT 4
242#define MMCRA_THR_CTL_SHIFT 8
243#define MMCRA_THR_SEL_SHIFT 16
244#define MMCRA_THR_CMP_SHIFT 32
245#define MMCRA_SDAR_MODE_TLB (1ull << 42)
246
247
248static inline bool event_is_fab_match(u64 event)
249{
250 /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
251 event &= 0xff0fe;
252
253 /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
254 return (event == 0x30056 || event == 0x4f052);
255}
256
257static int power8_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
258{
Michael Ellerman4df48992013-06-28 18:15:17 +1000259 unsigned int unit, pmc, cache, ebb;
Michael Ellermane05b9b92013-04-25 19:28:28 +0000260 unsigned long mask, value;
261
262 mask = value = 0;
263
Michael Ellermand8bec4c2013-06-28 18:15:10 +1000264 if (event & ~EVENT_VALID_MASK)
265 return -1;
266
Michael Ellerman4df48992013-06-28 18:15:17 +1000267 pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
268 unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
269 cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000270 ebb = (event >> PERF_EVENT_CONFIG_EBB_SHIFT) & EVENT_EBB_MASK;
Michael Ellerman4df48992013-06-28 18:15:17 +1000271
272 /* Clear the EBB bit in the event, so event checks work below */
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000273 event &= ~(EVENT_EBB_MASK << PERF_EVENT_CONFIG_EBB_SHIFT);
Michael Ellermane05b9b92013-04-25 19:28:28 +0000274
275 if (pmc) {
276 if (pmc > 6)
277 return -1;
278
279 mask |= CNST_PMC_MASK(pmc);
280 value |= CNST_PMC_VAL(pmc);
281
282 if (pmc >= 5 && event != 0x500fa && event != 0x600f4)
283 return -1;
284 }
285
286 if (pmc <= 4) {
287 /*
288 * Add to number of counters in use. Note this includes events with
289 * a PMC of 0 - they still need a PMC, it's just assigned later.
290 * Don't count events on PMC 5 & 6, there is only one valid event
291 * on each of those counters, and they are handled above.
292 */
293 mask |= CNST_NC_MASK;
294 value |= CNST_NC_VAL;
295 }
296
297 if (unit >= 6 && unit <= 9) {
298 /*
299 * L2/L3 events contain a cache selector field, which is
300 * supposed to be programmed into MMCRC. However MMCRC is only
301 * HV writable, and there is no API for guest kernels to modify
302 * it. The solution is for the hypervisor to initialise the
303 * field to zeroes, and for us to only ever allow events that
304 * have a cache selector of zero.
305 */
306 if (cache)
307 return -1;
308
309 } else if (event & EVENT_IS_L1) {
310 mask |= CNST_L1_QUAL_MASK;
311 value |= CNST_L1_QUAL_VAL(cache);
312 }
313
314 if (event & EVENT_IS_MARKED) {
315 mask |= CNST_SAMPLE_MASK;
316 value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
317 }
318
319 /*
320 * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
321 * the threshold control bits are used for the match value.
322 */
323 if (event_is_fab_match(event)) {
324 mask |= CNST_FAB_MATCH_MASK;
325 value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
326 } else {
327 /*
328 * Check the mantissa upper two bits are not zero, unless the
329 * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
330 */
331 unsigned int cmp, exp;
332
333 cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
334 exp = cmp >> 7;
335
336 if (exp && (cmp & 0x60) == 0)
337 return -1;
338
339 mask |= CNST_THRESH_MASK;
340 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
341 }
342
Michael Ellerman4df48992013-06-28 18:15:17 +1000343 if (!pmc && ebb)
344 /* EBB events must specify the PMC */
345 return -1;
346
347 /*
348 * All events must agree on EBB, either all request it or none.
349 * EBB events are pinned & exclusive, so this should never actually
350 * hit, but we leave it as a fallback in case.
351 */
352 mask |= CNST_EBB_VAL(ebb);
353 value |= CNST_EBB_MASK;
354
Michael Ellermane05b9b92013-04-25 19:28:28 +0000355 *maskp = mask;
356 *valp = value;
357
358 return 0;
359}
360
361static int power8_compute_mmcr(u64 event[], int n_ev,
362 unsigned int hwc[], unsigned long mmcr[])
363{
364 unsigned long mmcra, mmcr1, unit, combine, psel, cache, val;
365 unsigned int pmc, pmc_inuse;
366 int i;
367
368 pmc_inuse = 0;
369
370 /* First pass to count resource use */
371 for (i = 0; i < n_ev; ++i) {
372 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
373 if (pmc)
374 pmc_inuse |= 1 << pmc;
375 }
376
377 /* In continous sampling mode, update SDAR on TLB miss */
378 mmcra = MMCRA_SDAR_MODE_TLB;
379 mmcr1 = 0;
380
381 /* Second pass: assign PMCs, set all MMCR1 fields */
382 for (i = 0; i < n_ev; ++i) {
383 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
384 unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
385 combine = (event[i] >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK;
386 psel = event[i] & EVENT_PSEL_MASK;
387
388 if (!pmc) {
389 for (pmc = 1; pmc <= 4; ++pmc) {
390 if (!(pmc_inuse & (1 << pmc)))
391 break;
392 }
393
394 pmc_inuse |= 1 << pmc;
395 }
396
397 if (pmc <= 4) {
398 mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
399 mmcr1 |= combine << MMCR1_COMBINE_SHIFT(pmc);
400 mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
401 }
402
403 if (event[i] & EVENT_IS_L1) {
404 cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
405 mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
406 cache >>= 1;
407 mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
408 }
409
410 if (event[i] & EVENT_IS_MARKED) {
411 mmcra |= MMCRA_SAMPLE_ENABLE;
412
413 val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
414 if (val) {
415 mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
416 mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
417 }
418 }
419
420 /*
421 * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
422 * the threshold bits are used for the match value.
423 */
424 if (event_is_fab_match(event[i])) {
Michael Ellermana53b27b2013-10-02 18:04:06 +1000425 mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
426 EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
Michael Ellermane05b9b92013-04-25 19:28:28 +0000427 } else {
428 val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
429 mmcra |= val << MMCRA_THR_CTL_SHIFT;
430 val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
431 mmcra |= val << MMCRA_THR_SEL_SHIFT;
432 val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
433 mmcra |= val << MMCRA_THR_CMP_SHIFT;
434 }
435
436 hwc[i] = pmc - 1;
437 }
438
439 /* Return MMCRx values */
440 mmcr[0] = 0;
441
442 /* pmc_inuse is 1-based */
443 if (pmc_inuse & 2)
444 mmcr[0] = MMCR0_PMC1CE;
445
446 if (pmc_inuse & 0x7c)
447 mmcr[0] |= MMCR0_PMCjCE;
448
Michael Ellerman7a7a41f2013-06-28 18:15:12 +1000449 /* If we're not using PMC 5 or 6, freeze them */
450 if (!(pmc_inuse & 0x60))
451 mmcr[0] |= MMCR0_FC56;
452
Michael Ellermane05b9b92013-04-25 19:28:28 +0000453 mmcr[1] = mmcr1;
454 mmcr[2] = mmcra;
455
456 return 0;
457}
458
459#define MAX_ALT 2
460
461/* Table of alternatives, sorted by column 0 */
462static const unsigned int event_alternatives[][MAX_ALT] = {
463 { 0x10134, 0x301e2 }, /* PM_MRK_ST_CMPL */
464 { 0x10138, 0x40138 }, /* PM_BR_MRK_2PATH */
465 { 0x18082, 0x3e05e }, /* PM_L3_CO_MEPF */
466 { 0x1d14e, 0x401e8 }, /* PM_MRK_DATA_FROM_L2MISS */
467 { 0x1e054, 0x4000a }, /* PM_CMPLU_STALL */
468 { 0x20036, 0x40036 }, /* PM_BR_2PATH */
469 { 0x200f2, 0x300f2 }, /* PM_INST_DISP */
470 { 0x200f4, 0x600f4 }, /* PM_RUN_CYC */
471 { 0x2013c, 0x3012e }, /* PM_MRK_FILT_MATCH */
472 { 0x3e054, 0x400f0 }, /* PM_LD_MISS_L1 */
473 { 0x400fa, 0x500fa }, /* PM_RUN_INST_CMPL */
474};
475
476/*
477 * Scan the alternatives table for a match and return the
478 * index into the alternatives table if found, else -1.
479 */
480static int find_alternative(u64 event)
481{
482 int i, j;
483
484 for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
485 if (event < event_alternatives[i][0])
486 break;
487
488 for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
489 if (event == event_alternatives[i][j])
490 return i;
491 }
492
493 return -1;
494}
495
496static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
497{
498 int i, j, num_alt = 0;
499 u64 alt_event;
500
501 alt[num_alt++] = event;
502
503 i = find_alternative(event);
504 if (i >= 0) {
505 /* Filter out the original event, it's already in alt[0] */
506 for (j = 0; j < MAX_ALT; ++j) {
507 alt_event = event_alternatives[i][j];
508 if (alt_event && alt_event != event)
509 alt[num_alt++] = alt_event;
510 }
511 }
512
513 if (flags & PPMU_ONLY_COUNT_RUN) {
514 /*
515 * We're only counting in RUN state, so PM_CYC is equivalent to
516 * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
517 */
518 j = num_alt;
519 for (i = 0; i < num_alt; ++i) {
520 switch (alt[i]) {
521 case 0x1e: /* PM_CYC */
522 alt[j++] = 0x600f4; /* PM_RUN_CYC */
523 break;
524 case 0x600f4: /* PM_RUN_CYC */
525 alt[j++] = 0x1e;
526 break;
527 case 0x2: /* PM_PPC_CMPL */
528 alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
529 break;
530 case 0x500fa: /* PM_RUN_INST_CMPL */
531 alt[j++] = 0x2; /* PM_PPC_CMPL */
532 break;
533 }
534 }
535 num_alt = j;
536 }
537
538 return num_alt;
539}
540
541static void power8_disable_pmc(unsigned int pmc, unsigned long mmcr[])
542{
543 if (pmc <= 3)
544 mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
545}
546
547PMU_FORMAT_ATTR(event, "config:0-49");
548PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
549PMU_FORMAT_ATTR(mark, "config:8");
550PMU_FORMAT_ATTR(combine, "config:11");
551PMU_FORMAT_ATTR(unit, "config:12-15");
552PMU_FORMAT_ATTR(pmc, "config:16-19");
553PMU_FORMAT_ATTR(cache_sel, "config:20-23");
554PMU_FORMAT_ATTR(sample_mode, "config:24-28");
555PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
556PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
557PMU_FORMAT_ATTR(thresh_start, "config:36-39");
558PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
559
560static struct attribute *power8_pmu_format_attr[] = {
561 &format_attr_event.attr,
562 &format_attr_pmcxsel.attr,
563 &format_attr_mark.attr,
564 &format_attr_combine.attr,
565 &format_attr_unit.attr,
566 &format_attr_pmc.attr,
567 &format_attr_cache_sel.attr,
568 &format_attr_sample_mode.attr,
569 &format_attr_thresh_sel.attr,
570 &format_attr_thresh_stop.attr,
571 &format_attr_thresh_start.attr,
572 &format_attr_thresh_cmp.attr,
573 NULL,
574};
575
576struct attribute_group power8_pmu_format_group = {
577 .name = "format",
578 .attrs = power8_pmu_format_attr,
579};
580
581static const struct attribute_group *power8_pmu_attr_groups[] = {
582 &power8_pmu_format_group,
583 NULL,
584};
585
586static int power8_generic_events[] = {
587 [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
588 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
589 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
590 [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
591 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
592 [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
Michael Ellerman2fdd3132014-01-24 15:50:51 +1100593 [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
594 [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1,
Michael Ellermane05b9b92013-04-25 19:28:28 +0000595};
596
Anshuman Khandualb1113552013-04-22 19:42:43 +0000597static u64 power8_bhrb_filter_map(u64 branch_sample_type)
598{
599 u64 pmu_bhrb_filter = 0;
Anshuman Khandualb1113552013-04-22 19:42:43 +0000600
Anshuman Khandual7689bdc2013-06-10 11:23:28 +0530601 /* BHRB and regular PMU events share the same privilege state
Anshuman Khandualb1113552013-04-22 19:42:43 +0000602 * filter configuration. BHRB is always recorded along with a
Anshuman Khandual7689bdc2013-06-10 11:23:28 +0530603 * regular PMU event. As the privilege state filter is handled
604 * in the basic PMC configuration of the accompanying regular
605 * PMU event, we ignore any separate BHRB specific request.
Anshuman Khandualb1113552013-04-22 19:42:43 +0000606 */
Anshuman Khandualb1113552013-04-22 19:42:43 +0000607
608 /* No branch filter requested */
609 if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
610 return pmu_bhrb_filter;
611
612 /* Invalid branch filter options - HW does not support */
613 if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
614 return -1;
615
616 if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
617 return -1;
618
619 if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
620 pmu_bhrb_filter |= POWER8_MMCRA_IFM1;
621 return pmu_bhrb_filter;
622 }
623
624 /* Every thing else is unsupported */
625 return -1;
626}
627
628static void power8_config_bhrb(u64 pmu_bhrb_filter)
629{
630 /* Enable BHRB filter in PMU */
631 mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
632}
633
Michael Ellerman2fdd3132014-01-24 15:50:51 +1100634#define C(x) PERF_COUNT_HW_CACHE_##x
635
636/*
637 * Table of generalized cache-related events.
638 * 0 means not supported, -1 means nonsensical, other values
639 * are event codes.
640 */
641static int power8_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
642 [ C(L1D) ] = {
643 [ C(OP_READ) ] = {
644 [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
645 [ C(RESULT_MISS) ] = PM_LD_MISS_L1,
646 },
647 [ C(OP_WRITE) ] = {
648 [ C(RESULT_ACCESS) ] = 0,
649 [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
650 },
651 [ C(OP_PREFETCH) ] = {
652 [ C(RESULT_ACCESS) ] = PM_L1_PREF,
653 [ C(RESULT_MISS) ] = 0,
654 },
655 },
656 [ C(L1I) ] = {
657 [ C(OP_READ) ] = {
658 [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
659 [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
660 },
661 [ C(OP_WRITE) ] = {
662 [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
663 [ C(RESULT_MISS) ] = -1,
664 },
665 [ C(OP_PREFETCH) ] = {
666 [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
667 [ C(RESULT_MISS) ] = 0,
668 },
669 },
670 [ C(LL) ] = {
671 [ C(OP_READ) ] = {
672 [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
673 [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
674 },
675 [ C(OP_WRITE) ] = {
676 [ C(RESULT_ACCESS) ] = PM_L2_ST,
677 [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
678 },
679 [ C(OP_PREFETCH) ] = {
680 [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
681 [ C(RESULT_MISS) ] = 0,
682 },
683 },
684 [ C(DTLB) ] = {
685 [ C(OP_READ) ] = {
686 [ C(RESULT_ACCESS) ] = 0,
687 [ C(RESULT_MISS) ] = PM_DTLB_MISS,
688 },
689 [ C(OP_WRITE) ] = {
690 [ C(RESULT_ACCESS) ] = -1,
691 [ C(RESULT_MISS) ] = -1,
692 },
693 [ C(OP_PREFETCH) ] = {
694 [ C(RESULT_ACCESS) ] = -1,
695 [ C(RESULT_MISS) ] = -1,
696 },
697 },
698 [ C(ITLB) ] = {
699 [ C(OP_READ) ] = {
700 [ C(RESULT_ACCESS) ] = 0,
701 [ C(RESULT_MISS) ] = PM_ITLB_MISS,
702 },
703 [ C(OP_WRITE) ] = {
704 [ C(RESULT_ACCESS) ] = -1,
705 [ C(RESULT_MISS) ] = -1,
706 },
707 [ C(OP_PREFETCH) ] = {
708 [ C(RESULT_ACCESS) ] = -1,
709 [ C(RESULT_MISS) ] = -1,
710 },
711 },
712 [ C(BPU) ] = {
713 [ C(OP_READ) ] = {
714 [ C(RESULT_ACCESS) ] = PM_BRU_FIN,
715 [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
716 },
717 [ C(OP_WRITE) ] = {
718 [ C(RESULT_ACCESS) ] = -1,
719 [ C(RESULT_MISS) ] = -1,
720 },
721 [ C(OP_PREFETCH) ] = {
722 [ C(RESULT_ACCESS) ] = -1,
723 [ C(RESULT_MISS) ] = -1,
724 },
725 },
726 [ C(NODE) ] = {
727 [ C(OP_READ) ] = {
728 [ C(RESULT_ACCESS) ] = -1,
729 [ C(RESULT_MISS) ] = -1,
730 },
731 [ C(OP_WRITE) ] = {
732 [ C(RESULT_ACCESS) ] = -1,
733 [ C(RESULT_MISS) ] = -1,
734 },
735 [ C(OP_PREFETCH) ] = {
736 [ C(RESULT_ACCESS) ] = -1,
737 [ C(RESULT_MISS) ] = -1,
738 },
739 },
740};
741
742#undef C
743
Michael Ellermane05b9b92013-04-25 19:28:28 +0000744static struct power_pmu power8_pmu = {
745 .name = "POWER8",
746 .n_counter = 6,
747 .max_alternatives = MAX_ALT + 1,
748 .add_fields = POWER8_ADD_FIELDS,
749 .test_adder = POWER8_TEST_ADDER,
750 .compute_mmcr = power8_compute_mmcr,
Anshuman Khandualb1113552013-04-22 19:42:43 +0000751 .config_bhrb = power8_config_bhrb,
752 .bhrb_filter_map = power8_bhrb_filter_map,
Michael Ellermane05b9b92013-04-25 19:28:28 +0000753 .get_constraint = power8_get_constraint,
754 .get_alternatives = power8_get_alternatives,
755 .disable_pmc = power8_disable_pmc,
Michael Ellerman4df48992013-06-28 18:15:17 +1000756 .flags = PPMU_HAS_SSLOT | PPMU_HAS_SIER | PPMU_BHRB | PPMU_EBB,
Michael Ellermane05b9b92013-04-25 19:28:28 +0000757 .n_generic = ARRAY_SIZE(power8_generic_events),
758 .generic_events = power8_generic_events,
Michael Ellerman2fdd3132014-01-24 15:50:51 +1100759 .cache_events = &power8_cache_events,
Michael Ellermane05b9b92013-04-25 19:28:28 +0000760 .attr_groups = power8_pmu_attr_groups,
Anshuman Khandualb1113552013-04-22 19:42:43 +0000761 .bhrb_nr = 32,
Michael Ellermane05b9b92013-04-25 19:28:28 +0000762};
763
764static int __init init_power8_pmu(void)
765{
Michael Ellerman5d7ead02013-07-13 12:53:40 +1000766 int rc;
767
Michael Ellermane05b9b92013-04-25 19:28:28 +0000768 if (!cur_cpu_spec->oprofile_cpu_type ||
769 strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
770 return -ENODEV;
771
Michael Ellerman5d7ead02013-07-13 12:53:40 +1000772 rc = register_power_pmu(&power8_pmu);
773 if (rc)
774 return rc;
775
776 /* Tell userspace that EBB is supported */
777 cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
778
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100779 if (cpu_has_feature(CPU_FTR_PMAO_BUG))
780 pr_info("PMAO restore workaround active.\n");
781
Michael Ellerman5d7ead02013-07-13 12:53:40 +1000782 return 0;
Michael Ellermane05b9b92013-04-25 19:28:28 +0000783}
784early_initcall(init_power8_pmu);