blob: b38acff8a791f1e61f8684147e468135aa1a3af9 [file] [log] [blame]
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +05301/*
2 * Performance counter support for POWER9 processors.
3 *
4 * Copyright 2009 Paul Mackerras, IBM Corporation.
5 * Copyright 2013 Michael Ellerman, IBM Corporation.
6 * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or later version.
12 */
13
14#define pr_fmt(fmt) "power9-pmu: " fmt
15
16#include "isa207-common.h"
17
18/*
Madhavan Srinivasan18201b22016-12-02 06:05:01 +053019 * Raw event encoding for Power9:
20 *
21 * 60 56 52 48 44 40 36 32
22 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
23 * | | [ ] [ ] [ thresh_cmp ] [ thresh_ctl ]
24 * | | | | |
25 * | | *- IFM (Linux) | thresh start/stop OR FAB match -*
26 * | *- BHRB (Linux) *sm
27 * *- EBB (Linux)
28 *
29 * 28 24 20 16 12 8 4 0
30 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
31 * [ ] [ sample ] [cache] [ pmc ] [unit ] [] m [ pmcxsel ]
32 * | | | | |
33 * | | | | *- mark
34 * | | *- L1/L2/L3 cache_sel |
35 * | | |
36 * | *- sampling mode for marked events *- combine
37 * |
38 * *- thresh_sel
39 *
40 * Below uses IBM bit numbering.
41 *
42 * MMCR1[x:y] = unit (PMCxUNIT)
43 * MMCR1[24] = pmc1combine[0]
44 * MMCR1[25] = pmc1combine[1]
45 * MMCR1[26] = pmc2combine[0]
46 * MMCR1[27] = pmc2combine[1]
47 * MMCR1[28] = pmc3combine[0]
48 * MMCR1[29] = pmc3combine[1]
49 * MMCR1[30] = pmc4combine[0]
50 * MMCR1[31] = pmc4combine[1]
51 *
52 * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
53 * # PM_MRK_FAB_RSP_MATCH
54 * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
55 * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
56 * # PM_MRK_FAB_RSP_MATCH_CYC
57 * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
58 * else
59 * MMCRA[48:55] = thresh_ctl (THRESH START/END)
60 *
61 * if thresh_sel:
62 * MMCRA[45:47] = thresh_sel
63 *
64 * if thresh_cmp:
65 * MMCRA[9:11] = thresh_cmp[0:2]
66 * MMCRA[12:18] = thresh_cmp[3:9]
67 *
68 * if unit == 6 or unit == 7
69 * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
70 * else if unit == 8 or unit == 9:
71 * if cache_sel[0] == 0: # L3 bank
72 * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
73 * else if cache_sel[0] == 1:
74 * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
75 * else if cache_sel[1]: # L1 event
76 * MMCR1[16] = cache_sel[2]
77 * MMCR1[17] = cache_sel[3]
78 *
79 * if mark:
80 * MMCRA[63] = 1 (SAMPLE_ENABLE)
81 * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
82 * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
83 *
84 * if EBB and BHRB:
85 * MMCRA[32:33] = IFM
86 *
87 * MMCRA[SDAR_MODE] = sm
88 */
89
90/*
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +053091 * Some power9 event codes.
92 */
93#define EVENT(_name, _code) _name = _code,
94
95enum {
96#include "power9-events-list.h"
97};
98
99#undef EVENT
100
101/* MMCRA IFM bits - POWER9 */
102#define POWER9_MMCRA_IFM1 0x0000000040000000UL
103#define POWER9_MMCRA_IFM2 0x0000000080000000UL
104#define POWER9_MMCRA_IFM3 0x00000000C0000000UL
105
Madhavan Srinivasan60b00022016-12-02 06:04:59 +0530106/* PowerISA v2.07 format attribute structure*/
107extern struct attribute_group isa207_pmu_format_group;
108
Madhavan Srinivasana114aca2017-02-12 22:33:12 +0530109/* Table of alternatives, sorted by column 0 */
110static const unsigned int power9_event_alternatives[][MAX_ALT] = {
111 { PM_INST_DISP, PM_INST_DISP_ALT },
112};
113
114static int power9_get_alternatives(u64 event, unsigned int flags, u64 alt[])
115{
116 int num_alt = 0;
117
118 num_alt = isa207_get_alternatives(event, alt, power9_event_alternatives,
119 (int)ARRAY_SIZE(power9_event_alternatives));
120
121 return num_alt;
122}
123
Madhavan Srinivasanf1fb60b2016-06-26 23:07:09 +0530124GENERIC_EVENT_ATTR(cpu-cycles, PM_CYC);
125GENERIC_EVENT_ATTR(stalled-cycles-frontend, PM_ICT_NOSLOT_CYC);
126GENERIC_EVENT_ATTR(stalled-cycles-backend, PM_CMPLU_STALL);
127GENERIC_EVENT_ATTR(instructions, PM_INST_CMPL);
128GENERIC_EVENT_ATTR(branch-instructions, PM_BRU_CMPL);
129GENERIC_EVENT_ATTR(branch-misses, PM_BR_MPRED_CMPL);
130GENERIC_EVENT_ATTR(cache-references, PM_LD_REF_L1);
131GENERIC_EVENT_ATTR(cache-misses, PM_LD_MISS_L1_FIN);
132
133CACHE_EVENT_ATTR(L1-dcache-load-misses, PM_LD_MISS_L1_FIN);
134CACHE_EVENT_ATTR(L1-dcache-loads, PM_LD_REF_L1);
135CACHE_EVENT_ATTR(L1-dcache-prefetches, PM_L1_PREF);
136CACHE_EVENT_ATTR(L1-dcache-store-misses, PM_ST_MISS_L1);
137CACHE_EVENT_ATTR(L1-icache-load-misses, PM_L1_ICACHE_MISS);
138CACHE_EVENT_ATTR(L1-icache-loads, PM_INST_FROM_L1);
139CACHE_EVENT_ATTR(L1-icache-prefetches, PM_IC_PREF_WRITE);
140CACHE_EVENT_ATTR(LLC-load-misses, PM_DATA_FROM_L3MISS);
141CACHE_EVENT_ATTR(LLC-loads, PM_DATA_FROM_L3);
142CACHE_EVENT_ATTR(LLC-prefetches, PM_L3_PREF_ALL);
143CACHE_EVENT_ATTR(LLC-store-misses, PM_L2_ST_MISS);
144CACHE_EVENT_ATTR(LLC-stores, PM_L2_ST);
145CACHE_EVENT_ATTR(branch-load-misses, PM_BR_MPRED_CMPL);
146CACHE_EVENT_ATTR(branch-loads, PM_BRU_CMPL);
147CACHE_EVENT_ATTR(dTLB-load-misses, PM_DTLB_MISS);
148CACHE_EVENT_ATTR(iTLB-load-misses, PM_ITLB_MISS);
149
150static struct attribute *power9_events_attr[] = {
151 GENERIC_EVENT_PTR(PM_CYC),
152 GENERIC_EVENT_PTR(PM_ICT_NOSLOT_CYC),
153 GENERIC_EVENT_PTR(PM_CMPLU_STALL),
154 GENERIC_EVENT_PTR(PM_INST_CMPL),
155 GENERIC_EVENT_PTR(PM_BRU_CMPL),
156 GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
157 GENERIC_EVENT_PTR(PM_LD_REF_L1),
158 GENERIC_EVENT_PTR(PM_LD_MISS_L1_FIN),
159 CACHE_EVENT_PTR(PM_LD_MISS_L1_FIN),
160 CACHE_EVENT_PTR(PM_LD_REF_L1),
161 CACHE_EVENT_PTR(PM_L1_PREF),
162 CACHE_EVENT_PTR(PM_ST_MISS_L1),
163 CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
164 CACHE_EVENT_PTR(PM_INST_FROM_L1),
165 CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
166 CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
167 CACHE_EVENT_PTR(PM_DATA_FROM_L3),
168 CACHE_EVENT_PTR(PM_L3_PREF_ALL),
169 CACHE_EVENT_PTR(PM_L2_ST_MISS),
170 CACHE_EVENT_PTR(PM_L2_ST),
171 CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
172 CACHE_EVENT_PTR(PM_BRU_CMPL),
173 CACHE_EVENT_PTR(PM_DTLB_MISS),
174 CACHE_EVENT_PTR(PM_ITLB_MISS),
175 NULL
176};
177
178static struct attribute_group power9_pmu_events_group = {
179 .name = "events",
180 .attrs = power9_events_attr,
181};
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530182
Madhavan Srinivasan520ed5b2016-12-02 06:05:00 +0530183static const struct attribute_group *power9_isa207_pmu_attr_groups[] = {
Madhavan Srinivasan60b00022016-12-02 06:04:59 +0530184 &isa207_pmu_format_group,
Madhavan Srinivasanf1fb60b2016-06-26 23:07:09 +0530185 &power9_pmu_events_group,
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530186 NULL,
187};
188
Madhavan Srinivasan18201b22016-12-02 06:05:01 +0530189PMU_FORMAT_ATTR(event, "config:0-51");
190PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
191PMU_FORMAT_ATTR(mark, "config:8");
192PMU_FORMAT_ATTR(combine, "config:10-11");
193PMU_FORMAT_ATTR(unit, "config:12-15");
194PMU_FORMAT_ATTR(pmc, "config:16-19");
195PMU_FORMAT_ATTR(cache_sel, "config:20-23");
196PMU_FORMAT_ATTR(sample_mode, "config:24-28");
197PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
198PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
199PMU_FORMAT_ATTR(thresh_start, "config:36-39");
200PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
201PMU_FORMAT_ATTR(sdar_mode, "config:50-51");
202
203static struct attribute *power9_pmu_format_attr[] = {
204 &format_attr_event.attr,
205 &format_attr_pmcxsel.attr,
206 &format_attr_mark.attr,
207 &format_attr_combine.attr,
208 &format_attr_unit.attr,
209 &format_attr_pmc.attr,
210 &format_attr_cache_sel.attr,
211 &format_attr_sample_mode.attr,
212 &format_attr_thresh_sel.attr,
213 &format_attr_thresh_stop.attr,
214 &format_attr_thresh_start.attr,
215 &format_attr_thresh_cmp.attr,
216 &format_attr_sdar_mode.attr,
217 NULL,
218};
219
220static struct attribute_group power9_pmu_format_group = {
221 .name = "format",
222 .attrs = power9_pmu_format_attr,
223};
224
225static const struct attribute_group *power9_pmu_attr_groups[] = {
226 &power9_pmu_format_group,
227 &power9_pmu_events_group,
228 NULL,
229};
230
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530231static int power9_generic_events[] = {
232 [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
233 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_ICT_NOSLOT_CYC,
234 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
235 [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
236 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_CMPL,
237 [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
238 [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
239 [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1_FIN,
240};
241
242static u64 power9_bhrb_filter_map(u64 branch_sample_type)
243{
244 u64 pmu_bhrb_filter = 0;
245
246 /* BHRB and regular PMU events share the same privilege state
247 * filter configuration. BHRB is always recorded along with a
248 * regular PMU event. As the privilege state filter is handled
249 * in the basic PMC configuration of the accompanying regular
250 * PMU event, we ignore any separate BHRB specific request.
251 */
252
253 /* No branch filter requested */
254 if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
255 return pmu_bhrb_filter;
256
257 /* Invalid branch filter options - HW does not support */
258 if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
259 return -1;
260
261 if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
262 return -1;
263
264 if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
265 return -1;
266
267 if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
268 pmu_bhrb_filter |= POWER9_MMCRA_IFM1;
269 return pmu_bhrb_filter;
270 }
271
272 /* Every thing else is unsupported */
273 return -1;
274}
275
276static void power9_config_bhrb(u64 pmu_bhrb_filter)
277{
278 /* Enable BHRB filter in PMU */
279 mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
280}
281
282#define C(x) PERF_COUNT_HW_CACHE_##x
283
284/*
285 * Table of generalized cache-related events.
286 * 0 means not supported, -1 means nonsensical, other values
287 * are event codes.
288 */
289static int power9_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
290 [ C(L1D) ] = {
291 [ C(OP_READ) ] = {
292 [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
293 [ C(RESULT_MISS) ] = PM_LD_MISS_L1_FIN,
294 },
295 [ C(OP_WRITE) ] = {
296 [ C(RESULT_ACCESS) ] = 0,
297 [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
298 },
299 [ C(OP_PREFETCH) ] = {
300 [ C(RESULT_ACCESS) ] = PM_L1_PREF,
301 [ C(RESULT_MISS) ] = 0,
302 },
303 },
304 [ C(L1I) ] = {
305 [ C(OP_READ) ] = {
306 [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
307 [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
308 },
309 [ C(OP_WRITE) ] = {
310 [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
311 [ C(RESULT_MISS) ] = -1,
312 },
313 [ C(OP_PREFETCH) ] = {
314 [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
315 [ C(RESULT_MISS) ] = 0,
316 },
317 },
318 [ C(LL) ] = {
319 [ C(OP_READ) ] = {
320 [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
321 [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
322 },
323 [ C(OP_WRITE) ] = {
324 [ C(RESULT_ACCESS) ] = PM_L2_ST,
325 [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
326 },
327 [ C(OP_PREFETCH) ] = {
328 [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
329 [ C(RESULT_MISS) ] = 0,
330 },
331 },
332 [ C(DTLB) ] = {
333 [ C(OP_READ) ] = {
334 [ C(RESULT_ACCESS) ] = 0,
335 [ C(RESULT_MISS) ] = PM_DTLB_MISS,
336 },
337 [ C(OP_WRITE) ] = {
338 [ C(RESULT_ACCESS) ] = -1,
339 [ C(RESULT_MISS) ] = -1,
340 },
341 [ C(OP_PREFETCH) ] = {
342 [ C(RESULT_ACCESS) ] = -1,
343 [ C(RESULT_MISS) ] = -1,
344 },
345 },
346 [ C(ITLB) ] = {
347 [ C(OP_READ) ] = {
348 [ C(RESULT_ACCESS) ] = 0,
349 [ C(RESULT_MISS) ] = PM_ITLB_MISS,
350 },
351 [ C(OP_WRITE) ] = {
352 [ C(RESULT_ACCESS) ] = -1,
353 [ C(RESULT_MISS) ] = -1,
354 },
355 [ C(OP_PREFETCH) ] = {
356 [ C(RESULT_ACCESS) ] = -1,
357 [ C(RESULT_MISS) ] = -1,
358 },
359 },
360 [ C(BPU) ] = {
361 [ C(OP_READ) ] = {
362 [ C(RESULT_ACCESS) ] = PM_BRU_CMPL,
363 [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
364 },
365 [ C(OP_WRITE) ] = {
366 [ C(RESULT_ACCESS) ] = -1,
367 [ C(RESULT_MISS) ] = -1,
368 },
369 [ C(OP_PREFETCH) ] = {
370 [ C(RESULT_ACCESS) ] = -1,
371 [ C(RESULT_MISS) ] = -1,
372 },
373 },
374 [ C(NODE) ] = {
375 [ C(OP_READ) ] = {
376 [ C(RESULT_ACCESS) ] = -1,
377 [ C(RESULT_MISS) ] = -1,
378 },
379 [ C(OP_WRITE) ] = {
380 [ C(RESULT_ACCESS) ] = -1,
381 [ C(RESULT_MISS) ] = -1,
382 },
383 [ C(OP_PREFETCH) ] = {
384 [ C(RESULT_ACCESS) ] = -1,
385 [ C(RESULT_MISS) ] = -1,
386 },
387 },
388};
389
390#undef C
391
Madhavan Srinivasan520ed5b2016-12-02 06:05:00 +0530392static struct power_pmu power9_isa207_pmu = {
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530393 .name = "POWER9",
394 .n_counter = MAX_PMU_COUNTERS,
395 .add_fields = ISA207_ADD_FIELDS,
396 .test_adder = ISA207_TEST_ADDER,
397 .compute_mmcr = isa207_compute_mmcr,
398 .config_bhrb = power9_config_bhrb,
399 .bhrb_filter_map = power9_bhrb_filter_map,
400 .get_constraint = isa207_get_constraint,
Madhavan Srinivasana114aca2017-02-12 22:33:12 +0530401 .get_alternatives = power9_get_alternatives,
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530402 .disable_pmc = isa207_disable_pmc,
Madhavan Srinivasan27593d72017-01-18 09:12:56 +0530403 .flags = PPMU_NO_SIAR | PPMU_ARCH_207S,
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530404 .n_generic = ARRAY_SIZE(power9_generic_events),
405 .generic_events = power9_generic_events,
406 .cache_events = &power9_cache_events,
Madhavan Srinivasan520ed5b2016-12-02 06:05:00 +0530407 .attr_groups = power9_isa207_pmu_attr_groups,
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530408 .bhrb_nr = 32,
409};
410
Madhavan Srinivasan18201b22016-12-02 06:05:01 +0530411static struct power_pmu power9_pmu = {
412 .name = "POWER9",
413 .n_counter = MAX_PMU_COUNTERS,
414 .add_fields = ISA207_ADD_FIELDS,
415 .test_adder = ISA207_TEST_ADDER,
416 .compute_mmcr = isa207_compute_mmcr,
417 .config_bhrb = power9_config_bhrb,
418 .bhrb_filter_map = power9_bhrb_filter_map,
419 .get_constraint = isa207_get_constraint,
Madhavan Srinivasana114aca2017-02-12 22:33:12 +0530420 .get_alternatives = power9_get_alternatives,
Madhavan Srinivasan18201b22016-12-02 06:05:01 +0530421 .disable_pmc = isa207_disable_pmc,
422 .flags = PPMU_HAS_SIER | PPMU_ARCH_207S,
423 .n_generic = ARRAY_SIZE(power9_generic_events),
424 .generic_events = power9_generic_events,
425 .cache_events = &power9_cache_events,
426 .attr_groups = power9_pmu_attr_groups,
427 .bhrb_nr = 32,
428};
429
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530430static int __init init_power9_pmu(void)
431{
Madhavan Srinivasan520ed5b2016-12-02 06:05:00 +0530432 int rc = 0;
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530433
434 /* Comes from cpu_specs[] */
435 if (!cur_cpu_spec->oprofile_cpu_type ||
436 strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power9"))
437 return -ENODEV;
438
Madhavan Srinivasan520ed5b2016-12-02 06:05:00 +0530439 if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
440 rc = register_power_pmu(&power9_isa207_pmu);
Madhavan Srinivasan18201b22016-12-02 06:05:01 +0530441 } else {
442 rc = register_power_pmu(&power9_pmu);
Madhavan Srinivasan520ed5b2016-12-02 06:05:00 +0530443 }
444
Madhavan Srinivasan8c002db2016-06-26 23:07:08 +0530445 if (rc)
446 return rc;
447
448 /* Tell userspace that EBB is supported */
449 cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
450
451 return 0;
452}
453early_initcall(init_power9_pmu);