1
2
3
4
5
6
7
8
9
10
11
12
13#include "isa207-common.h"
14
15PMU_FORMAT_ATTR(event, "config:0-49");
16PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
17PMU_FORMAT_ATTR(mark, "config:8");
18PMU_FORMAT_ATTR(combine, "config:11");
19PMU_FORMAT_ATTR(unit, "config:12-15");
20PMU_FORMAT_ATTR(pmc, "config:16-19");
21PMU_FORMAT_ATTR(cache_sel, "config:20-23");
22PMU_FORMAT_ATTR(sample_mode, "config:24-28");
23PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
24PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
25PMU_FORMAT_ATTR(thresh_start, "config:36-39");
26PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
27
28struct attribute *isa207_pmu_format_attr[] = {
29 &format_attr_event.attr,
30 &format_attr_pmcxsel.attr,
31 &format_attr_mark.attr,
32 &format_attr_combine.attr,
33 &format_attr_unit.attr,
34 &format_attr_pmc.attr,
35 &format_attr_cache_sel.attr,
36 &format_attr_sample_mode.attr,
37 &format_attr_thresh_sel.attr,
38 &format_attr_thresh_stop.attr,
39 &format_attr_thresh_start.attr,
40 &format_attr_thresh_cmp.attr,
41 NULL,
42};
43
44struct attribute_group isa207_pmu_format_group = {
45 .name = "format",
46 .attrs = isa207_pmu_format_attr,
47};
48
49static inline bool event_is_fab_match(u64 event)
50{
51
52 event &= 0xff0fe;
53
54
55 return (event == 0x30056 || event == 0x4f052);
56}
57
58static bool is_event_valid(u64 event)
59{
60 u64 valid_mask = EVENT_VALID_MASK;
61
62 if (cpu_has_feature(CPU_FTR_ARCH_300) && !cpu_has_feature(CPU_FTR_POWER9_DD1))
63 valid_mask = p9_EVENT_VALID_MASK;
64
65 return !(event & ~valid_mask);
66}
67
68static inline bool is_event_marked(u64 event)
69{
70 if (event & EVENT_IS_MARKED)
71 return true;
72
73 return false;
74}
75
76static void mmcra_sdar_mode(u64 event, unsigned long *mmcra)
77{
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
97 if (is_event_marked(event) || (*mmcra & MMCRA_SAMPLE_ENABLE))
98 *mmcra &= MMCRA_SDAR_MODE_NO_UPDATES;
99 else if (!cpu_has_feature(CPU_FTR_POWER9_DD1) && p9_SDAR_MODE(event))
100 *mmcra |= p9_SDAR_MODE(event) << MMCRA_SDAR_MODE_SHIFT;
101 else
102 *mmcra |= MMCRA_SDAR_MODE_DCACHE;
103 } else
104 *mmcra |= MMCRA_SDAR_MODE_TLB;
105}
106
107static u64 thresh_cmp_val(u64 value)
108{
109 if (cpu_has_feature(CPU_FTR_ARCH_300) && !cpu_has_feature(CPU_FTR_POWER9_DD1))
110 return value << p9_MMCRA_THR_CMP_SHIFT;
111
112 return value << MMCRA_THR_CMP_SHIFT;
113}
114
115static unsigned long combine_from_event(u64 event)
116{
117 if (cpu_has_feature(CPU_FTR_ARCH_300) && !cpu_has_feature(CPU_FTR_POWER9_DD1))
118 return p9_EVENT_COMBINE(event);
119
120 return EVENT_COMBINE(event);
121}
122
123static unsigned long combine_shift(unsigned long pmc)
124{
125 if (cpu_has_feature(CPU_FTR_ARCH_300) && !cpu_has_feature(CPU_FTR_POWER9_DD1))
126 return p9_MMCR1_COMBINE_SHIFT(pmc);
127
128 return MMCR1_COMBINE_SHIFT(pmc);
129}
130
131static inline bool event_is_threshold(u64 event)
132{
133 return (event >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
134}
135
136static bool is_thresh_cmp_valid(u64 event)
137{
138 unsigned int cmp, exp;
139
140
141
142
143
144 cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
145 exp = cmp >> 7;
146
147 if (exp && (cmp & 0x60) == 0)
148 return false;
149
150 return true;
151}
152
153static inline u64 isa207_find_source(u64 idx, u32 sub_idx)
154{
155 u64 ret = PERF_MEM_NA;
156
157 switch(idx) {
158 case 0:
159
160 break;
161 case 1:
162 ret = PH(LVL, L1);
163 break;
164 case 2:
165 ret = PH(LVL, L2);
166 break;
167 case 3:
168 ret = PH(LVL, L3);
169 break;
170 case 4:
171 if (sub_idx <= 1)
172 ret = PH(LVL, LOC_RAM);
173 else if (sub_idx > 1 && sub_idx <= 2)
174 ret = PH(LVL, REM_RAM1);
175 else
176 ret = PH(LVL, REM_RAM2);
177 ret |= P(SNOOP, HIT);
178 break;
179 case 5:
180 ret = PH(LVL, REM_CCE1);
181 if ((sub_idx == 0) || (sub_idx == 2) || (sub_idx == 4))
182 ret |= P(SNOOP, HIT);
183 else if ((sub_idx == 1) || (sub_idx == 3) || (sub_idx == 5))
184 ret |= P(SNOOP, HITM);
185 break;
186 case 6:
187 ret = PH(LVL, REM_CCE2);
188 if ((sub_idx == 0) || (sub_idx == 2))
189 ret |= P(SNOOP, HIT);
190 else if ((sub_idx == 1) || (sub_idx == 3))
191 ret |= P(SNOOP, HITM);
192 break;
193 case 7:
194 ret = PM(LVL, L1);
195 break;
196 }
197
198 return ret;
199}
200
201void isa207_get_mem_data_src(union perf_mem_data_src *dsrc, u32 flags,
202 struct pt_regs *regs)
203{
204 u64 idx;
205 u32 sub_idx;
206 u64 sier;
207 u64 val;
208
209
210 if (!(flags & PPMU_HAS_SIER)) {
211 dsrc->val = 0;
212 return;
213 }
214
215 sier = mfspr(SPRN_SIER);
216 val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
217 if (val == 1 || val == 2) {
218 idx = (sier & ISA207_SIER_LDST_MASK) >> ISA207_SIER_LDST_SHIFT;
219 sub_idx = (sier & ISA207_SIER_DATA_SRC_MASK) >> ISA207_SIER_DATA_SRC_SHIFT;
220
221 dsrc->val = isa207_find_source(idx, sub_idx);
222 dsrc->val |= (val == 1) ? P(OP, LOAD) : P(OP, STORE);
223 }
224}
225
226void isa207_get_mem_weight(u64 *weight)
227{
228 u64 mmcra = mfspr(SPRN_MMCRA);
229 u64 exp = MMCRA_THR_CTR_EXP(mmcra);
230 u64 mantissa = MMCRA_THR_CTR_MANT(mmcra);
231
232 *weight = mantissa << (2 * exp);
233}
234
235int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
236{
237 unsigned int unit, pmc, cache, ebb;
238 unsigned long mask, value;
239
240 mask = value = 0;
241
242 if (!is_event_valid(event))
243 return -1;
244
245 pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
246 unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
247 cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
248 ebb = (event >> EVENT_EBB_SHIFT) & EVENT_EBB_MASK;
249
250 if (pmc) {
251 u64 base_event;
252
253 if (pmc > 6)
254 return -1;
255
256
257 base_event = event & ~EVENT_LINUX_MASK;
258
259 if (pmc >= 5 && base_event != 0x500fa &&
260 base_event != 0x600f4)
261 return -1;
262
263 mask |= CNST_PMC_MASK(pmc);
264 value |= CNST_PMC_VAL(pmc);
265 }
266
267 if (pmc <= 4) {
268
269
270
271
272
273
274 mask |= CNST_NC_MASK;
275 value |= CNST_NC_VAL;
276 }
277
278 if (unit >= 6 && unit <= 9) {
279
280
281
282
283
284
285
286
287
288 if (cache & 0x7)
289 return -1;
290
291 } else if (event & EVENT_IS_L1) {
292 mask |= CNST_L1_QUAL_MASK;
293 value |= CNST_L1_QUAL_VAL(cache);
294 }
295
296 if (is_event_marked(event)) {
297 mask |= CNST_SAMPLE_MASK;
298 value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
299 }
300
301 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
302 if (event_is_threshold(event) && is_thresh_cmp_valid(event)) {
303 mask |= CNST_THRESH_MASK;
304 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
305 }
306 } else {
307
308
309
310
311 if (event_is_fab_match(event)) {
312 mask |= CNST_FAB_MATCH_MASK;
313 value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
314 } else {
315 if (!is_thresh_cmp_valid(event))
316 return -1;
317
318 mask |= CNST_THRESH_MASK;
319 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
320 }
321 }
322
323 if (!pmc && ebb)
324
325 return -1;
326
327 if (event & EVENT_WANTS_BHRB) {
328 if (!ebb)
329
330 return -1;
331
332 mask |= CNST_IFM_MASK;
333 value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
334 }
335
336
337
338
339
340
341 mask |= CNST_EBB_VAL(ebb);
342 value |= CNST_EBB_MASK;
343
344 *maskp = mask;
345 *valp = value;
346
347 return 0;
348}
349
350int isa207_compute_mmcr(u64 event[], int n_ev,
351 unsigned int hwc[], unsigned long mmcr[],
352 struct perf_event *pevents[])
353{
354 unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
355 unsigned int pmc, pmc_inuse;
356 int i;
357
358 pmc_inuse = 0;
359
360
361 for (i = 0; i < n_ev; ++i) {
362 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
363 if (pmc)
364 pmc_inuse |= 1 << pmc;
365 }
366
367 mmcra = mmcr1 = mmcr2 = 0;
368
369
370 for (i = 0; i < n_ev; ++i) {
371 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
372 unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
373 combine = combine_from_event(event[i]);
374 psel = event[i] & EVENT_PSEL_MASK;
375
376 if (!pmc) {
377 for (pmc = 1; pmc <= 4; ++pmc) {
378 if (!(pmc_inuse & (1 << pmc)))
379 break;
380 }
381
382 pmc_inuse |= 1 << pmc;
383 }
384
385 if (pmc <= 4) {
386 mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
387 mmcr1 |= combine << combine_shift(pmc);
388 mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
389 }
390
391
392 mmcra_sdar_mode(event[i], &mmcra);
393
394 if (event[i] & EVENT_IS_L1) {
395 cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
396 mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
397 cache >>= 1;
398 mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
399 }
400
401 if (is_event_marked(event[i])) {
402 mmcra |= MMCRA_SAMPLE_ENABLE;
403
404 val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
405 if (val) {
406 mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
407 mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
408 }
409 }
410
411
412
413
414
415 if (!cpu_has_feature(CPU_FTR_ARCH_300) && event_is_fab_match(event[i])) {
416 mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
417 EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
418 } else {
419 val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
420 mmcra |= val << MMCRA_THR_CTL_SHIFT;
421 val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
422 mmcra |= val << MMCRA_THR_SEL_SHIFT;
423 val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
424 mmcra |= thresh_cmp_val(val);
425 }
426
427 if (event[i] & EVENT_WANTS_BHRB) {
428 val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
429 mmcra |= val << MMCRA_IFM_SHIFT;
430 }
431
432 if (pevents[i]->attr.exclude_user)
433 mmcr2 |= MMCR2_FCP(pmc);
434
435 if (pevents[i]->attr.exclude_hv)
436 mmcr2 |= MMCR2_FCH(pmc);
437
438 if (pevents[i]->attr.exclude_kernel) {
439 if (cpu_has_feature(CPU_FTR_HVMODE))
440 mmcr2 |= MMCR2_FCH(pmc);
441 else
442 mmcr2 |= MMCR2_FCS(pmc);
443 }
444
445 hwc[i] = pmc - 1;
446 }
447
448
449 mmcr[0] = 0;
450
451
452 if (pmc_inuse & 2)
453 mmcr[0] = MMCR0_PMC1CE;
454
455 if (pmc_inuse & 0x7c)
456 mmcr[0] |= MMCR0_PMCjCE;
457
458
459 if (!(pmc_inuse & 0x60))
460 mmcr[0] |= MMCR0_FC56;
461
462 mmcr[1] = mmcr1;
463 mmcr[2] = mmcra;
464 mmcr[3] = mmcr2;
465
466 return 0;
467}
468
469void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[])
470{
471 if (pmc <= 3)
472 mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
473}
474
475static int find_alternative(u64 event, const unsigned int ev_alt[][MAX_ALT], int size)
476{
477 int i, j;
478
479 for (i = 0; i < size; ++i) {
480 if (event < ev_alt[i][0])
481 break;
482
483 for (j = 0; j < MAX_ALT && ev_alt[i][j]; ++j)
484 if (event == ev_alt[i][j])
485 return i;
486 }
487
488 return -1;
489}
490
491int isa207_get_alternatives(u64 event, u64 alt[], int size, unsigned int flags,
492 const unsigned int ev_alt[][MAX_ALT])
493{
494 int i, j, num_alt = 0;
495 u64 alt_event;
496
497 alt[num_alt++] = event;
498 i = find_alternative(event, ev_alt, size);
499 if (i >= 0) {
500
501 for (j = 0; j < MAX_ALT; ++j) {
502 alt_event = ev_alt[i][j];
503 if (alt_event && alt_event != event)
504 alt[num_alt++] = alt_event;
505 }
506 }
507
508 if (flags & PPMU_ONLY_COUNT_RUN) {
509
510
511
512
513 j = num_alt;
514 for (i = 0; i < num_alt; ++i) {
515 switch (alt[i]) {
516 case 0x1e:
517 alt[j++] = 0x600f4;
518 break;
519 case 0x600f4:
520 alt[j++] = 0x1e;
521 break;
522 case 0x2:
523 alt[j++] = 0x500fa;
524 break;
525 case 0x500fa:
526 alt[j++] = 0x2;
527 break;
528 }
529 }
530 num_alt = j;
531 }
532
533 return num_alt;
534}
535