blob: 944180f55a3c6a12b4716d95ff541aa9a595b8a0 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Common Performance counter support functions for PowerISA v2.07 processors.
4 *
5 * Copyright 2009 Paul Mackerras, IBM Corporation.
6 * Copyright 2013 Michael Ellerman, IBM Corporation.
7 * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9#include "isa207-common.h"
10
11PMU_FORMAT_ATTR(event, "config:0-49");
12PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
13PMU_FORMAT_ATTR(mark, "config:8");
14PMU_FORMAT_ATTR(combine, "config:11");
15PMU_FORMAT_ATTR(unit, "config:12-15");
16PMU_FORMAT_ATTR(pmc, "config:16-19");
17PMU_FORMAT_ATTR(cache_sel, "config:20-23");
18PMU_FORMAT_ATTR(sample_mode, "config:24-28");
19PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
20PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
21PMU_FORMAT_ATTR(thresh_start, "config:36-39");
22PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
23
24struct attribute *isa207_pmu_format_attr[] = {
25 &format_attr_event.attr,
26 &format_attr_pmcxsel.attr,
27 &format_attr_mark.attr,
28 &format_attr_combine.attr,
29 &format_attr_unit.attr,
30 &format_attr_pmc.attr,
31 &format_attr_cache_sel.attr,
32 &format_attr_sample_mode.attr,
33 &format_attr_thresh_sel.attr,
34 &format_attr_thresh_stop.attr,
35 &format_attr_thresh_start.attr,
36 &format_attr_thresh_cmp.attr,
37 NULL,
38};
39
40struct attribute_group isa207_pmu_format_group = {
41 .name = "format",
42 .attrs = isa207_pmu_format_attr,
43};
44
45static inline bool event_is_fab_match(u64 event)
46{
47 /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
48 event &= 0xff0fe;
49
50 /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
51 return (event == 0x30056 || event == 0x4f052);
52}
53
54static bool is_event_valid(u64 event)
55{
56 u64 valid_mask = EVENT_VALID_MASK;
57
58 if (cpu_has_feature(CPU_FTR_ARCH_300))
59 valid_mask = p9_EVENT_VALID_MASK;
60
61 return !(event & ~valid_mask);
62}
63
64static inline bool is_event_marked(u64 event)
65{
66 if (event & EVENT_IS_MARKED)
67 return true;
68
69 return false;
70}
71
72static void mmcra_sdar_mode(u64 event, unsigned long *mmcra)
73{
74 /*
75 * MMCRA[SDAR_MODE] specifices how the SDAR should be updated in
76 * continous sampling mode.
77 *
78 * Incase of Power8:
79 * MMCRA[SDAR_MODE] will be programmed as "0b01" for continous sampling
80 * mode and will be un-changed when setting MMCRA[63] (Marked events).
81 *
82 * Incase of Power9:
83 * Marked event: MMCRA[SDAR_MODE] will be set to 0b00 ('No Updates'),
84 * or if group already have any marked events.
85 * For rest
86 * MMCRA[SDAR_MODE] will be set from event code.
87 * If sdar_mode from event is zero, default to 0b01. Hardware
88 * requires that we set a non-zero value.
89 */
90 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
91 if (is_event_marked(event) || (*mmcra & MMCRA_SAMPLE_ENABLE))
92 *mmcra &= MMCRA_SDAR_MODE_NO_UPDATES;
93 else if (p9_SDAR_MODE(event))
94 *mmcra |= p9_SDAR_MODE(event) << MMCRA_SDAR_MODE_SHIFT;
95 else
96 *mmcra |= MMCRA_SDAR_MODE_DCACHE;
97 } else
98 *mmcra |= MMCRA_SDAR_MODE_TLB;
99}
100
101static u64 thresh_cmp_val(u64 value)
102{
103 if (cpu_has_feature(CPU_FTR_ARCH_300))
104 return value << p9_MMCRA_THR_CMP_SHIFT;
105
106 return value << MMCRA_THR_CMP_SHIFT;
107}
108
109static unsigned long combine_from_event(u64 event)
110{
111 if (cpu_has_feature(CPU_FTR_ARCH_300))
112 return p9_EVENT_COMBINE(event);
113
114 return EVENT_COMBINE(event);
115}
116
117static unsigned long combine_shift(unsigned long pmc)
118{
119 if (cpu_has_feature(CPU_FTR_ARCH_300))
120 return p9_MMCR1_COMBINE_SHIFT(pmc);
121
122 return MMCR1_COMBINE_SHIFT(pmc);
123}
124
125static inline bool event_is_threshold(u64 event)
126{
127 return (event >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
128}
129
130static bool is_thresh_cmp_valid(u64 event)
131{
132 unsigned int cmp, exp;
133
134 /*
135 * Check the mantissa upper two bits are not zero, unless the
136 * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
137 */
138 cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
139 exp = cmp >> 7;
140
141 if (exp && (cmp & 0x60) == 0)
142 return false;
143
144 return true;
145}
146
David Brazdil0f672f62019-12-10 10:32:29 +0000147static unsigned int dc_ic_rld_quad_l1_sel(u64 event)
148{
149 unsigned int cache;
150
151 cache = (event >> EVENT_CACHE_SEL_SHIFT) & MMCR1_DC_IC_QUAL_MASK;
152 return cache;
153}
154
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000155static inline u64 isa207_find_source(u64 idx, u32 sub_idx)
156{
157 u64 ret = PERF_MEM_NA;
158
159 switch(idx) {
160 case 0:
161 /* Nothing to do */
162 break;
163 case 1:
164 ret = PH(LVL, L1);
165 break;
166 case 2:
167 ret = PH(LVL, L2);
168 break;
169 case 3:
170 ret = PH(LVL, L3);
171 break;
172 case 4:
173 if (sub_idx <= 1)
174 ret = PH(LVL, LOC_RAM);
175 else if (sub_idx > 1 && sub_idx <= 2)
176 ret = PH(LVL, REM_RAM1);
177 else
178 ret = PH(LVL, REM_RAM2);
179 ret |= P(SNOOP, HIT);
180 break;
181 case 5:
182 ret = PH(LVL, REM_CCE1);
183 if ((sub_idx == 0) || (sub_idx == 2) || (sub_idx == 4))
184 ret |= P(SNOOP, HIT);
185 else if ((sub_idx == 1) || (sub_idx == 3) || (sub_idx == 5))
186 ret |= P(SNOOP, HITM);
187 break;
188 case 6:
189 ret = PH(LVL, REM_CCE2);
190 if ((sub_idx == 0) || (sub_idx == 2))
191 ret |= P(SNOOP, HIT);
192 else if ((sub_idx == 1) || (sub_idx == 3))
193 ret |= P(SNOOP, HITM);
194 break;
195 case 7:
196 ret = PM(LVL, L1);
197 break;
198 }
199
200 return ret;
201}
202
203void isa207_get_mem_data_src(union perf_mem_data_src *dsrc, u32 flags,
204 struct pt_regs *regs)
205{
206 u64 idx;
207 u32 sub_idx;
208 u64 sier;
209 u64 val;
210
211 /* Skip if no SIER support */
212 if (!(flags & PPMU_HAS_SIER)) {
213 dsrc->val = 0;
214 return;
215 }
216
217 sier = mfspr(SPRN_SIER);
218 val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
219 if (val == 1 || val == 2) {
220 idx = (sier & ISA207_SIER_LDST_MASK) >> ISA207_SIER_LDST_SHIFT;
221 sub_idx = (sier & ISA207_SIER_DATA_SRC_MASK) >> ISA207_SIER_DATA_SRC_SHIFT;
222
223 dsrc->val = isa207_find_source(idx, sub_idx);
224 dsrc->val |= (val == 1) ? P(OP, LOAD) : P(OP, STORE);
225 }
226}
227
228void isa207_get_mem_weight(u64 *weight)
229{
230 u64 mmcra = mfspr(SPRN_MMCRA);
231 u64 exp = MMCRA_THR_CTR_EXP(mmcra);
232 u64 mantissa = MMCRA_THR_CTR_MANT(mmcra);
David Brazdil0f672f62019-12-10 10:32:29 +0000233 u64 sier = mfspr(SPRN_SIER);
234 u64 val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235
David Brazdil0f672f62019-12-10 10:32:29 +0000236 if (val == 0 || val == 7)
237 *weight = 0;
238 else
239 *weight = mantissa << (2 * exp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000240}
241
242int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
243{
244 unsigned int unit, pmc, cache, ebb;
245 unsigned long mask, value;
246
247 mask = value = 0;
248
249 if (!is_event_valid(event))
250 return -1;
251
252 pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
253 unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
254 cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
255 ebb = (event >> EVENT_EBB_SHIFT) & EVENT_EBB_MASK;
256
257 if (pmc) {
258 u64 base_event;
259
260 if (pmc > 6)
261 return -1;
262
263 /* Ignore Linux defined bits when checking event below */
264 base_event = event & ~EVENT_LINUX_MASK;
265
266 if (pmc >= 5 && base_event != 0x500fa &&
267 base_event != 0x600f4)
268 return -1;
269
270 mask |= CNST_PMC_MASK(pmc);
271 value |= CNST_PMC_VAL(pmc);
Olivier Deprez0e641232021-09-23 10:07:05 +0200272
273 /*
274 * PMC5 and PMC6 are used to count cycles and instructions and
275 * they do not support most of the constraint bits. Add a check
276 * to exclude PMC5/6 from most of the constraints except for
277 * EBB/BHRB.
278 */
279 if (pmc >= 5)
280 goto ebb_bhrb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 }
282
283 if (pmc <= 4) {
284 /*
285 * Add to number of counters in use. Note this includes events with
286 * a PMC of 0 - they still need a PMC, it's just assigned later.
287 * Don't count events on PMC 5 & 6, there is only one valid event
288 * on each of those counters, and they are handled above.
289 */
290 mask |= CNST_NC_MASK;
291 value |= CNST_NC_VAL;
292 }
293
294 if (unit >= 6 && unit <= 9) {
David Brazdil0f672f62019-12-10 10:32:29 +0000295 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
296 mask |= CNST_CACHE_GROUP_MASK;
297 value |= CNST_CACHE_GROUP_VAL(event & 0xff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298
David Brazdil0f672f62019-12-10 10:32:29 +0000299 mask |= CNST_CACHE_PMC4_MASK;
300 if (pmc == 4)
301 value |= CNST_CACHE_PMC4_VAL;
302 } else if (cache & 0x7) {
303 /*
304 * L2/L3 events contain a cache selector field, which is
305 * supposed to be programmed into MMCRC. However MMCRC is only
306 * HV writable, and there is no API for guest kernels to modify
307 * it. The solution is for the hypervisor to initialise the
308 * field to zeroes, and for us to only ever allow events that
309 * have a cache selector of zero. The bank selector (bit 3) is
310 * irrelevant, as long as the rest of the value is 0.
311 */
312 return -1;
313 }
314
315 } else if (cpu_has_feature(CPU_FTR_ARCH_300) || (event & EVENT_IS_L1)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 mask |= CNST_L1_QUAL_MASK;
317 value |= CNST_L1_QUAL_VAL(cache);
318 }
319
320 if (is_event_marked(event)) {
321 mask |= CNST_SAMPLE_MASK;
322 value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
323 }
324
325 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
326 if (event_is_threshold(event) && is_thresh_cmp_valid(event)) {
327 mask |= CNST_THRESH_MASK;
328 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
329 }
330 } else {
331 /*
332 * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
333 * the threshold control bits are used for the match value.
334 */
335 if (event_is_fab_match(event)) {
336 mask |= CNST_FAB_MATCH_MASK;
337 value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
338 } else {
339 if (!is_thresh_cmp_valid(event))
340 return -1;
341
342 mask |= CNST_THRESH_MASK;
343 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
344 }
345 }
346
Olivier Deprez0e641232021-09-23 10:07:05 +0200347ebb_bhrb:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 if (!pmc && ebb)
349 /* EBB events must specify the PMC */
350 return -1;
351
352 if (event & EVENT_WANTS_BHRB) {
353 if (!ebb)
354 /* Only EBB events can request BHRB */
355 return -1;
356
357 mask |= CNST_IFM_MASK;
358 value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
359 }
360
361 /*
362 * All events must agree on EBB, either all request it or none.
363 * EBB events are pinned & exclusive, so this should never actually
364 * hit, but we leave it as a fallback in case.
365 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200366 mask |= CNST_EBB_MASK;
367 value |= CNST_EBB_VAL(ebb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368
369 *maskp = mask;
370 *valp = value;
371
372 return 0;
373}
374
375int isa207_compute_mmcr(u64 event[], int n_ev,
376 unsigned int hwc[], unsigned long mmcr[],
377 struct perf_event *pevents[])
378{
379 unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
380 unsigned int pmc, pmc_inuse;
381 int i;
382
383 pmc_inuse = 0;
384
385 /* First pass to count resource use */
386 for (i = 0; i < n_ev; ++i) {
387 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
388 if (pmc)
389 pmc_inuse |= 1 << pmc;
390 }
391
392 mmcra = mmcr1 = mmcr2 = 0;
393
394 /* Second pass: assign PMCs, set all MMCR1 fields */
395 for (i = 0; i < n_ev; ++i) {
396 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
397 unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
398 combine = combine_from_event(event[i]);
399 psel = event[i] & EVENT_PSEL_MASK;
400
401 if (!pmc) {
402 for (pmc = 1; pmc <= 4; ++pmc) {
403 if (!(pmc_inuse & (1 << pmc)))
404 break;
405 }
406
407 pmc_inuse |= 1 << pmc;
408 }
409
410 if (pmc <= 4) {
411 mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
412 mmcr1 |= combine << combine_shift(pmc);
413 mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
414 }
415
416 /* In continuous sampling mode, update SDAR on TLB miss */
417 mmcra_sdar_mode(event[i], &mmcra);
418
David Brazdil0f672f62019-12-10 10:32:29 +0000419 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
420 cache = dc_ic_rld_quad_l1_sel(event[i]);
421 mmcr1 |= (cache) << MMCR1_DC_IC_QUAL_SHIFT;
422 } else {
423 if (event[i] & EVENT_IS_L1) {
424 cache = dc_ic_rld_quad_l1_sel(event[i]);
425 mmcr1 |= (cache) << MMCR1_DC_IC_QUAL_SHIFT;
426 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000427 }
428
429 if (is_event_marked(event[i])) {
430 mmcra |= MMCRA_SAMPLE_ENABLE;
431
432 val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
433 if (val) {
434 mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
435 mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
436 }
437 }
438
439 /*
440 * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
441 * the threshold bits are used for the match value.
442 */
443 if (!cpu_has_feature(CPU_FTR_ARCH_300) && event_is_fab_match(event[i])) {
444 mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
445 EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
446 } else {
447 val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
448 mmcra |= val << MMCRA_THR_CTL_SHIFT;
449 val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
450 mmcra |= val << MMCRA_THR_SEL_SHIFT;
451 val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
452 mmcra |= thresh_cmp_val(val);
453 }
454
455 if (event[i] & EVENT_WANTS_BHRB) {
456 val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
457 mmcra |= val << MMCRA_IFM_SHIFT;
458 }
459
460 if (pevents[i]->attr.exclude_user)
461 mmcr2 |= MMCR2_FCP(pmc);
462
463 if (pevents[i]->attr.exclude_hv)
464 mmcr2 |= MMCR2_FCH(pmc);
465
466 if (pevents[i]->attr.exclude_kernel) {
467 if (cpu_has_feature(CPU_FTR_HVMODE))
468 mmcr2 |= MMCR2_FCH(pmc);
469 else
470 mmcr2 |= MMCR2_FCS(pmc);
471 }
472
473 hwc[i] = pmc - 1;
474 }
475
476 /* Return MMCRx values */
477 mmcr[0] = 0;
478
479 /* pmc_inuse is 1-based */
480 if (pmc_inuse & 2)
481 mmcr[0] = MMCR0_PMC1CE;
482
483 if (pmc_inuse & 0x7c)
484 mmcr[0] |= MMCR0_PMCjCE;
485
486 /* If we're not using PMC 5 or 6, freeze them */
487 if (!(pmc_inuse & 0x60))
488 mmcr[0] |= MMCR0_FC56;
489
490 mmcr[1] = mmcr1;
491 mmcr[2] = mmcra;
492 mmcr[3] = mmcr2;
493
494 return 0;
495}
496
497void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[])
498{
499 if (pmc <= 3)
500 mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
501}
502
503static int find_alternative(u64 event, const unsigned int ev_alt[][MAX_ALT], int size)
504{
505 int i, j;
506
507 for (i = 0; i < size; ++i) {
508 if (event < ev_alt[i][0])
509 break;
510
511 for (j = 0; j < MAX_ALT && ev_alt[i][j]; ++j)
512 if (event == ev_alt[i][j])
513 return i;
514 }
515
516 return -1;
517}
518
519int isa207_get_alternatives(u64 event, u64 alt[], int size, unsigned int flags,
520 const unsigned int ev_alt[][MAX_ALT])
521{
522 int i, j, num_alt = 0;
523 u64 alt_event;
524
525 alt[num_alt++] = event;
526 i = find_alternative(event, ev_alt, size);
527 if (i >= 0) {
528 /* Filter out the original event, it's already in alt[0] */
529 for (j = 0; j < MAX_ALT; ++j) {
530 alt_event = ev_alt[i][j];
531 if (alt_event && alt_event != event)
532 alt[num_alt++] = alt_event;
533 }
534 }
535
536 if (flags & PPMU_ONLY_COUNT_RUN) {
537 /*
538 * We're only counting in RUN state, so PM_CYC is equivalent to
539 * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
540 */
541 j = num_alt;
542 for (i = 0; i < num_alt; ++i) {
543 switch (alt[i]) {
544 case 0x1e: /* PMC_CYC */
545 alt[j++] = 0x600f4; /* PM_RUN_CYC */
546 break;
547 case 0x600f4:
548 alt[j++] = 0x1e;
549 break;
550 case 0x2: /* PM_INST_CMPL */
551 alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
552 break;
553 case 0x500fa:
554 alt[j++] = 0x2;
555 break;
556 }
557 }
558 num_alt = j;
559 }
560
561 return num_alt;
562}