1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Performance event support - powerpc architecture code
4 *
5 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
6 */
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/sched/clock.h>
10 #include <linux/perf_event.h>
11 #include <linux/percpu.h>
12 #include <linux/hardirq.h>
13 #include <linux/uaccess.h>
14 #include <asm/reg.h>
15 #include <asm/pmc.h>
16 #include <asm/machdep.h>
17 #include <asm/firmware.h>
18 #include <asm/ptrace.h>
19 #include <asm/code-patching.h>
20
21 #ifdef CONFIG_PPC64
22 #include "internal.h"
23 #endif
24
25 #define BHRB_MAX_ENTRIES 32
26 #define BHRB_TARGET 0x0000000000000002
27 #define BHRB_PREDICTION 0x0000000000000001
28 #define BHRB_EA 0xFFFFFFFFFFFFFFFCUL
29
30 struct cpu_hw_events {
31 int n_events;
32 int n_percpu;
33 int disabled;
34 int n_added;
35 int n_limited;
36 u8 pmcs_enabled;
37 struct perf_event *event[MAX_HWEVENTS];
38 u64 events[MAX_HWEVENTS];
39 unsigned int flags[MAX_HWEVENTS];
40 struct mmcr_regs mmcr;
41 struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
42 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
43 u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
44 unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
45 unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
46
47 unsigned int txn_flags;
48 int n_txn_start;
49
50 /* BHRB bits */
51 u64 bhrb_filter; /* BHRB HW branch filter */
52 unsigned int bhrb_users;
53 void *bhrb_context;
54 struct perf_branch_stack bhrb_stack;
55 struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
56 u64 ic_init;
57 };
58
59 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
60
61 static struct power_pmu *ppmu;
62
63 /*
64 * Normally, to ignore kernel events we set the FCS (freeze counters
65 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
66 * hypervisor bit set in the MSR, or if we are running on a processor
67 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
68 * then we need to use the FCHV bit to ignore kernel events.
69 */
70 static unsigned int freeze_events_kernel = MMCR0_FCS;
71
72 /*
73 * 32-bit doesn't have MMCRA but does have an MMCR2,
74 * and a few other names are different.
75 * Also 32-bit doesn't have MMCR3, SIER2 and SIER3.
76 * Define them as zero knowing that any code path accessing
77 * these registers (via mtspr/mfspr) are done under ppmu flag
78 * check for PPMU_ARCH_31 and we will not enter that code path
79 * for 32-bit.
80 */
81 #ifdef CONFIG_PPC32
82
83 #define MMCR0_FCHV 0
84 #define MMCR0_PMCjCE MMCR0_PMCnCE
85 #define MMCR0_FC56 0
86 #define MMCR0_PMAO 0
87 #define MMCR0_EBE 0
88 #define MMCR0_BHRBA 0
89 #define MMCR0_PMCC 0
90 #define MMCR0_PMCC_U6 0
91
92 #define SPRN_MMCRA SPRN_MMCR2
93 #define SPRN_MMCR3 0
94 #define SPRN_SIER2 0
95 #define SPRN_SIER3 0
96 #define MMCRA_SAMPLE_ENABLE 0
97 #define MMCRA_BHRB_DISABLE 0
98 #define MMCR0_PMCCEXT 0
99
perf_ip_adjust(struct pt_regs * regs)100 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
101 {
102 return 0;
103 }
perf_get_data_addr(struct perf_event * event,struct pt_regs * regs,u64 * addrp)104 static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp) { }
perf_get_misc_flags(struct pt_regs * regs)105 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
106 {
107 return 0;
108 }
perf_read_regs(struct pt_regs * regs)109 static inline void perf_read_regs(struct pt_regs *regs)
110 {
111 regs->result = 0;
112 }
113
siar_valid(struct pt_regs * regs)114 static inline int siar_valid(struct pt_regs *regs)
115 {
116 return 1;
117 }
118
is_ebb_event(struct perf_event * event)119 static bool is_ebb_event(struct perf_event *event) { return false; }
ebb_event_check(struct perf_event * event)120 static int ebb_event_check(struct perf_event *event) { return 0; }
ebb_event_add(struct perf_event * event)121 static void ebb_event_add(struct perf_event *event) { }
ebb_switch_out(unsigned long mmcr0)122 static void ebb_switch_out(unsigned long mmcr0) { }
ebb_switch_in(bool ebb,struct cpu_hw_events * cpuhw)123 static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
124 {
125 return cpuhw->mmcr.mmcr0;
126 }
127
power_pmu_bhrb_enable(struct perf_event * event)128 static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
power_pmu_bhrb_disable(struct perf_event * event)129 static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
power_pmu_sched_task(struct perf_event_context * ctx,bool sched_in)130 static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
power_pmu_bhrb_read(struct perf_event * event,struct cpu_hw_events * cpuhw)131 static inline void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw) {}
pmao_restore_workaround(bool ebb)132 static void pmao_restore_workaround(bool ebb) { }
133 #endif /* CONFIG_PPC32 */
134
is_sier_available(void)135 bool is_sier_available(void)
136 {
137 if (!ppmu)
138 return false;
139
140 if (ppmu->flags & PPMU_HAS_SIER)
141 return true;
142
143 return false;
144 }
145
regs_use_siar(struct pt_regs * regs)146 static bool regs_use_siar(struct pt_regs *regs)
147 {
148 /*
149 * When we take a performance monitor exception the regs are setup
150 * using perf_read_regs() which overloads some fields, in particular
151 * regs->result to tell us whether to use SIAR.
152 *
153 * However if the regs are from another exception, eg. a syscall, then
154 * they have not been setup using perf_read_regs() and so regs->result
155 * is something random.
156 */
157 return ((TRAP(regs) == 0xf00) && regs->result);
158 }
159
160 /*
161 * Things that are specific to 64-bit implementations.
162 */
163 #ifdef CONFIG_PPC64
164
perf_ip_adjust(struct pt_regs * regs)165 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
166 {
167 unsigned long mmcra = regs->dsisr;
168
169 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
170 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
171 if (slot > 1)
172 return 4 * (slot - 1);
173 }
174
175 return 0;
176 }
177
178 /*
179 * The user wants a data address recorded.
180 * If we're not doing instruction sampling, give them the SDAR
181 * (sampled data address). If we are doing instruction sampling, then
182 * only give them the SDAR if it corresponds to the instruction
183 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
184 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
185 */
perf_get_data_addr(struct perf_event * event,struct pt_regs * regs,u64 * addrp)186 static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp)
187 {
188 unsigned long mmcra = regs->dsisr;
189 bool sdar_valid;
190
191 if (ppmu->flags & PPMU_HAS_SIER)
192 sdar_valid = regs->dar & SIER_SDAR_VALID;
193 else {
194 unsigned long sdsync;
195
196 if (ppmu->flags & PPMU_SIAR_VALID)
197 sdsync = POWER7P_MMCRA_SDAR_VALID;
198 else if (ppmu->flags & PPMU_ALT_SIPR)
199 sdsync = POWER6_MMCRA_SDSYNC;
200 else if (ppmu->flags & PPMU_NO_SIAR)
201 sdsync = MMCRA_SAMPLE_ENABLE;
202 else
203 sdsync = MMCRA_SDSYNC;
204
205 sdar_valid = mmcra & sdsync;
206 }
207
208 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
209 *addrp = mfspr(SPRN_SDAR);
210
211 if (is_kernel_addr(mfspr(SPRN_SDAR)) && event->attr.exclude_kernel)
212 *addrp = 0;
213 }
214
regs_sihv(struct pt_regs * regs)215 static bool regs_sihv(struct pt_regs *regs)
216 {
217 unsigned long sihv = MMCRA_SIHV;
218
219 if (ppmu->flags & PPMU_HAS_SIER)
220 return !!(regs->dar & SIER_SIHV);
221
222 if (ppmu->flags & PPMU_ALT_SIPR)
223 sihv = POWER6_MMCRA_SIHV;
224
225 return !!(regs->dsisr & sihv);
226 }
227
regs_sipr(struct pt_regs * regs)228 static bool regs_sipr(struct pt_regs *regs)
229 {
230 unsigned long sipr = MMCRA_SIPR;
231
232 if (ppmu->flags & PPMU_HAS_SIER)
233 return !!(regs->dar & SIER_SIPR);
234
235 if (ppmu->flags & PPMU_ALT_SIPR)
236 sipr = POWER6_MMCRA_SIPR;
237
238 return !!(regs->dsisr & sipr);
239 }
240
perf_flags_from_msr(struct pt_regs * regs)241 static inline u32 perf_flags_from_msr(struct pt_regs *regs)
242 {
243 if (regs->msr & MSR_PR)
244 return PERF_RECORD_MISC_USER;
245 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
246 return PERF_RECORD_MISC_HYPERVISOR;
247 return PERF_RECORD_MISC_KERNEL;
248 }
249
perf_get_misc_flags(struct pt_regs * regs)250 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
251 {
252 bool use_siar = regs_use_siar(regs);
253
254 if (!use_siar)
255 return perf_flags_from_msr(regs);
256
257 /*
258 * If we don't have flags in MMCRA, rather than using
259 * the MSR, we intuit the flags from the address in
260 * SIAR which should give slightly more reliable
261 * results
262 */
263 if (ppmu->flags & PPMU_NO_SIPR) {
264 unsigned long siar = mfspr(SPRN_SIAR);
265 if (is_kernel_addr(siar))
266 return PERF_RECORD_MISC_KERNEL;
267 return PERF_RECORD_MISC_USER;
268 }
269
270 /* PR has priority over HV, so order below is important */
271 if (regs_sipr(regs))
272 return PERF_RECORD_MISC_USER;
273
274 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
275 return PERF_RECORD_MISC_HYPERVISOR;
276
277 return PERF_RECORD_MISC_KERNEL;
278 }
279
280 /*
281 * Overload regs->dsisr to store MMCRA so we only need to read it once
282 * on each interrupt.
283 * Overload regs->dar to store SIER if we have it.
284 * Overload regs->result to specify whether we should use the MSR (result
285 * is zero) or the SIAR (result is non zero).
286 */
perf_read_regs(struct pt_regs * regs)287 static inline void perf_read_regs(struct pt_regs *regs)
288 {
289 unsigned long mmcra = mfspr(SPRN_MMCRA);
290 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
291 int use_siar;
292
293 regs->dsisr = mmcra;
294
295 if (ppmu->flags & PPMU_HAS_SIER)
296 regs->dar = mfspr(SPRN_SIER);
297
298 /*
299 * If this isn't a PMU exception (eg a software event) the SIAR is
300 * not valid. Use pt_regs.
301 *
302 * If it is a marked event use the SIAR.
303 *
304 * If the PMU doesn't update the SIAR for non marked events use
305 * pt_regs.
306 *
307 * If the PMU has HV/PR flags then check to see if they
308 * place the exception in userspace. If so, use pt_regs. In
309 * continuous sampling mode the SIAR and the PMU exception are
310 * not synchronised, so they may be many instructions apart.
311 * This can result in confusing backtraces. We still want
312 * hypervisor samples as well as samples in the kernel with
313 * interrupts off hence the userspace check.
314 */
315 if (TRAP(regs) != 0xf00)
316 use_siar = 0;
317 else if ((ppmu->flags & PPMU_NO_SIAR))
318 use_siar = 0;
319 else if (marked)
320 use_siar = 1;
321 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
322 use_siar = 0;
323 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
324 use_siar = 0;
325 else
326 use_siar = 1;
327
328 regs->result = use_siar;
329 }
330
331 /*
332 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
333 * must be sampled only if the SIAR-valid bit is set.
334 *
335 * For unmarked instructions and for processors that don't have the SIAR-Valid
336 * bit, assume that SIAR is valid.
337 */
siar_valid(struct pt_regs * regs)338 static inline int siar_valid(struct pt_regs *regs)
339 {
340 unsigned long mmcra = regs->dsisr;
341 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
342
343 if (marked) {
344 if (ppmu->flags & PPMU_HAS_SIER)
345 return regs->dar & SIER_SIAR_VALID;
346
347 if (ppmu->flags & PPMU_SIAR_VALID)
348 return mmcra & POWER7P_MMCRA_SIAR_VALID;
349 }
350
351 return 1;
352 }
353
354
355 /* Reset all possible BHRB entries */
power_pmu_bhrb_reset(void)356 static void power_pmu_bhrb_reset(void)
357 {
358 asm volatile(PPC_CLRBHRB);
359 }
360
power_pmu_bhrb_enable(struct perf_event * event)361 static void power_pmu_bhrb_enable(struct perf_event *event)
362 {
363 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
364
365 if (!ppmu->bhrb_nr)
366 return;
367
368 /* Clear BHRB if we changed task context to avoid data leaks */
369 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
370 power_pmu_bhrb_reset();
371 cpuhw->bhrb_context = event->ctx;
372 }
373 cpuhw->bhrb_users++;
374 perf_sched_cb_inc(event->ctx->pmu);
375 }
376
power_pmu_bhrb_disable(struct perf_event * event)377 static void power_pmu_bhrb_disable(struct perf_event *event)
378 {
379 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
380
381 if (!ppmu->bhrb_nr)
382 return;
383
384 WARN_ON_ONCE(!cpuhw->bhrb_users);
385 cpuhw->bhrb_users--;
386 perf_sched_cb_dec(event->ctx->pmu);
387
388 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
389 /* BHRB cannot be turned off when other
390 * events are active on the PMU.
391 */
392
393 /* avoid stale pointer */
394 cpuhw->bhrb_context = NULL;
395 }
396 }
397
398 /* Called from ctxsw to prevent one process's branch entries to
399 * mingle with the other process's entries during context switch.
400 */
power_pmu_sched_task(struct perf_event_context * ctx,bool sched_in)401 static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
402 {
403 if (!ppmu->bhrb_nr)
404 return;
405
406 if (sched_in)
407 power_pmu_bhrb_reset();
408 }
409 /* Calculate the to address for a branch */
power_pmu_bhrb_to(u64 addr)410 static __u64 power_pmu_bhrb_to(u64 addr)
411 {
412 unsigned int instr;
413 __u64 target;
414
415 if (is_kernel_addr(addr)) {
416 if (copy_from_kernel_nofault(&instr, (void *)addr,
417 sizeof(instr)))
418 return 0;
419
420 return branch_target((struct ppc_inst *)&instr);
421 }
422
423 /* Userspace: need copy instruction here then translate it */
424 if (copy_from_user_nofault(&instr, (unsigned int __user *)addr,
425 sizeof(instr)))
426 return 0;
427
428 target = branch_target((struct ppc_inst *)&instr);
429 if ((!target) || (instr & BRANCH_ABSOLUTE))
430 return target;
431
432 /* Translate relative branch target from kernel to user address */
433 return target - (unsigned long)&instr + addr;
434 }
435
436 /* Processing BHRB entries */
power_pmu_bhrb_read(struct perf_event * event,struct cpu_hw_events * cpuhw)437 static void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw)
438 {
439 u64 val;
440 u64 addr;
441 int r_index, u_index, pred;
442
443 r_index = 0;
444 u_index = 0;
445 while (r_index < ppmu->bhrb_nr) {
446 /* Assembly read function */
447 val = read_bhrb(r_index++);
448 if (!val)
449 /* Terminal marker: End of valid BHRB entries */
450 break;
451 else {
452 addr = val & BHRB_EA;
453 pred = val & BHRB_PREDICTION;
454
455 if (!addr)
456 /* invalid entry */
457 continue;
458
459 /*
460 * BHRB rolling buffer could very much contain the kernel
461 * addresses at this point. Check the privileges before
462 * exporting it to userspace (avoid exposure of regions
463 * where we could have speculative execution)
464 * Incase of ISA v3.1, BHRB will capture only user-space
465 * addresses, hence include a check before filtering code
466 */
467 if (!(ppmu->flags & PPMU_ARCH_31) &&
468 is_kernel_addr(addr) && event->attr.exclude_kernel)
469 continue;
470
471 /* Branches are read most recent first (ie. mfbhrb 0 is
472 * the most recent branch).
473 * There are two types of valid entries:
474 * 1) a target entry which is the to address of a
475 * computed goto like a blr,bctr,btar. The next
476 * entry read from the bhrb will be branch
477 * corresponding to this target (ie. the actual
478 * blr/bctr/btar instruction).
479 * 2) a from address which is an actual branch. If a
480 * target entry proceeds this, then this is the
481 * matching branch for that target. If this is not
482 * following a target entry, then this is a branch
483 * where the target is given as an immediate field
484 * in the instruction (ie. an i or b form branch).
485 * In this case we need to read the instruction from
486 * memory to determine the target/to address.
487 */
488
489 if (val & BHRB_TARGET) {
490 /* Target branches use two entries
491 * (ie. computed gotos/XL form)
492 */
493 cpuhw->bhrb_entries[u_index].to = addr;
494 cpuhw->bhrb_entries[u_index].mispred = pred;
495 cpuhw->bhrb_entries[u_index].predicted = ~pred;
496
497 /* Get from address in next entry */
498 val = read_bhrb(r_index++);
499 addr = val & BHRB_EA;
500 if (val & BHRB_TARGET) {
501 /* Shouldn't have two targets in a
502 row.. Reset index and try again */
503 r_index--;
504 addr = 0;
505 }
506 cpuhw->bhrb_entries[u_index].from = addr;
507 } else {
508 /* Branches to immediate field
509 (ie I or B form) */
510 cpuhw->bhrb_entries[u_index].from = addr;
511 cpuhw->bhrb_entries[u_index].to =
512 power_pmu_bhrb_to(addr);
513 cpuhw->bhrb_entries[u_index].mispred = pred;
514 cpuhw->bhrb_entries[u_index].predicted = ~pred;
515 }
516 u_index++;
517
518 }
519 }
520 cpuhw->bhrb_stack.nr = u_index;
521 cpuhw->bhrb_stack.hw_idx = -1ULL;
522 return;
523 }
524
is_ebb_event(struct perf_event * event)525 static bool is_ebb_event(struct perf_event *event)
526 {
527 /*
528 * This could be a per-PMU callback, but we'd rather avoid the cost. We
529 * check that the PMU supports EBB, meaning those that don't can still
530 * use bit 63 of the event code for something else if they wish.
531 */
532 return (ppmu->flags & PPMU_ARCH_207S) &&
533 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
534 }
535
ebb_event_check(struct perf_event * event)536 static int ebb_event_check(struct perf_event *event)
537 {
538 struct perf_event *leader = event->group_leader;
539
540 /* Event and group leader must agree on EBB */
541 if (is_ebb_event(leader) != is_ebb_event(event))
542 return -EINVAL;
543
544 if (is_ebb_event(event)) {
545 if (!(event->attach_state & PERF_ATTACH_TASK))
546 return -EINVAL;
547
548 if (!leader->attr.pinned || !leader->attr.exclusive)
549 return -EINVAL;
550
551 if (event->attr.freq ||
552 event->attr.inherit ||
553 event->attr.sample_type ||
554 event->attr.sample_period ||
555 event->attr.enable_on_exec)
556 return -EINVAL;
557 }
558
559 return 0;
560 }
561
ebb_event_add(struct perf_event * event)562 static void ebb_event_add(struct perf_event *event)
563 {
564 if (!is_ebb_event(event) || current->thread.used_ebb)
565 return;
566
567 /*
568 * IFF this is the first time we've added an EBB event, set
569 * PMXE in the user MMCR0 so we can detect when it's cleared by
570 * userspace. We need this so that we can context switch while
571 * userspace is in the EBB handler (where PMXE is 0).
572 */
573 current->thread.used_ebb = 1;
574 current->thread.mmcr0 |= MMCR0_PMXE;
575 }
576
ebb_switch_out(unsigned long mmcr0)577 static void ebb_switch_out(unsigned long mmcr0)
578 {
579 if (!(mmcr0 & MMCR0_EBE))
580 return;
581
582 current->thread.siar = mfspr(SPRN_SIAR);
583 current->thread.sier = mfspr(SPRN_SIER);
584 current->thread.sdar = mfspr(SPRN_SDAR);
585 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
586 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
587 if (ppmu->flags & PPMU_ARCH_31) {
588 current->thread.mmcr3 = mfspr(SPRN_MMCR3);
589 current->thread.sier2 = mfspr(SPRN_SIER2);
590 current->thread.sier3 = mfspr(SPRN_SIER3);
591 }
592 }
593
ebb_switch_in(bool ebb,struct cpu_hw_events * cpuhw)594 static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
595 {
596 unsigned long mmcr0 = cpuhw->mmcr.mmcr0;
597
598 if (!ebb)
599 goto out;
600
601 /* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
602 mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
603
604 /*
605 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
606 * with pmao_restore_workaround() because we may add PMAO but we never
607 * clear it here.
608 */
609 mmcr0 |= current->thread.mmcr0;
610
611 /*
612 * Be careful not to set PMXE if userspace had it cleared. This is also
613 * compatible with pmao_restore_workaround() because it has already
614 * cleared PMXE and we leave PMAO alone.
615 */
616 if (!(current->thread.mmcr0 & MMCR0_PMXE))
617 mmcr0 &= ~MMCR0_PMXE;
618
619 mtspr(SPRN_SIAR, current->thread.siar);
620 mtspr(SPRN_SIER, current->thread.sier);
621 mtspr(SPRN_SDAR, current->thread.sdar);
622
623 /*
624 * Merge the kernel & user values of MMCR2. The semantics we implement
625 * are that the user MMCR2 can set bits, ie. cause counters to freeze,
626 * but not clear bits. If a task wants to be able to clear bits, ie.
627 * unfreeze counters, it should not set exclude_xxx in its events and
628 * instead manage the MMCR2 entirely by itself.
629 */
630 mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2 | current->thread.mmcr2);
631
632 if (ppmu->flags & PPMU_ARCH_31) {
633 mtspr(SPRN_MMCR3, current->thread.mmcr3);
634 mtspr(SPRN_SIER2, current->thread.sier2);
635 mtspr(SPRN_SIER3, current->thread.sier3);
636 }
637 out:
638 return mmcr0;
639 }
640
pmao_restore_workaround(bool ebb)641 static void pmao_restore_workaround(bool ebb)
642 {
643 unsigned pmcs[6];
644
645 if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
646 return;
647
648 /*
649 * On POWER8E there is a hardware defect which affects the PMU context
650 * switch logic, ie. power_pmu_disable/enable().
651 *
652 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
653 * by the hardware. Sometime later the actual PMU exception is
654 * delivered.
655 *
656 * If we context switch, or simply disable/enable, the PMU prior to the
657 * exception arriving, the exception will be lost when we clear PMAO.
658 *
659 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
660 * set, and this _should_ generate an exception. However because of the
661 * defect no exception is generated when we write PMAO, and we get
662 * stuck with no counters counting but no exception delivered.
663 *
664 * The workaround is to detect this case and tweak the hardware to
665 * create another pending PMU exception.
666 *
667 * We do that by setting up PMC6 (cycles) for an imminent overflow and
668 * enabling the PMU. That causes a new exception to be generated in the
669 * chip, but we don't take it yet because we have interrupts hard
670 * disabled. We then write back the PMU state as we want it to be seen
671 * by the exception handler. When we reenable interrupts the exception
672 * handler will be called and see the correct state.
673 *
674 * The logic is the same for EBB, except that the exception is gated by
675 * us having interrupts hard disabled as well as the fact that we are
676 * not in userspace. The exception is finally delivered when we return
677 * to userspace.
678 */
679
680 /* Only if PMAO is set and PMAO_SYNC is clear */
681 if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
682 return;
683
684 /* If we're doing EBB, only if BESCR[GE] is set */
685 if (ebb && !(current->thread.bescr & BESCR_GE))
686 return;
687
688 /*
689 * We are already soft-disabled in power_pmu_enable(). We need to hard
690 * disable to actually prevent the PMU exception from firing.
691 */
692 hard_irq_disable();
693
694 /*
695 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
696 * Using read/write_pmc() in a for loop adds 12 function calls and
697 * almost doubles our code size.
698 */
699 pmcs[0] = mfspr(SPRN_PMC1);
700 pmcs[1] = mfspr(SPRN_PMC2);
701 pmcs[2] = mfspr(SPRN_PMC3);
702 pmcs[3] = mfspr(SPRN_PMC4);
703 pmcs[4] = mfspr(SPRN_PMC5);
704 pmcs[5] = mfspr(SPRN_PMC6);
705
706 /* Ensure all freeze bits are unset */
707 mtspr(SPRN_MMCR2, 0);
708
709 /* Set up PMC6 to overflow in one cycle */
710 mtspr(SPRN_PMC6, 0x7FFFFFFE);
711
712 /* Enable exceptions and unfreeze PMC6 */
713 mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
714
715 /* Now we need to refreeze and restore the PMCs */
716 mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
717
718 mtspr(SPRN_PMC1, pmcs[0]);
719 mtspr(SPRN_PMC2, pmcs[1]);
720 mtspr(SPRN_PMC3, pmcs[2]);
721 mtspr(SPRN_PMC4, pmcs[3]);
722 mtspr(SPRN_PMC5, pmcs[4]);
723 mtspr(SPRN_PMC6, pmcs[5]);
724 }
725
726 #endif /* CONFIG_PPC64 */
727
728 static void perf_event_interrupt(struct pt_regs *regs);
729
730 /*
731 * Read one performance monitor counter (PMC).
732 */
read_pmc(int idx)733 static unsigned long read_pmc(int idx)
734 {
735 unsigned long val;
736
737 switch (idx) {
738 case 1:
739 val = mfspr(SPRN_PMC1);
740 break;
741 case 2:
742 val = mfspr(SPRN_PMC2);
743 break;
744 case 3:
745 val = mfspr(SPRN_PMC3);
746 break;
747 case 4:
748 val = mfspr(SPRN_PMC4);
749 break;
750 case 5:
751 val = mfspr(SPRN_PMC5);
752 break;
753 case 6:
754 val = mfspr(SPRN_PMC6);
755 break;
756 #ifdef CONFIG_PPC64
757 case 7:
758 val = mfspr(SPRN_PMC7);
759 break;
760 case 8:
761 val = mfspr(SPRN_PMC8);
762 break;
763 #endif /* CONFIG_PPC64 */
764 default:
765 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
766 val = 0;
767 }
768 return val;
769 }
770
771 /*
772 * Write one PMC.
773 */
write_pmc(int idx,unsigned long val)774 static void write_pmc(int idx, unsigned long val)
775 {
776 switch (idx) {
777 case 1:
778 mtspr(SPRN_PMC1, val);
779 break;
780 case 2:
781 mtspr(SPRN_PMC2, val);
782 break;
783 case 3:
784 mtspr(SPRN_PMC3, val);
785 break;
786 case 4:
787 mtspr(SPRN_PMC4, val);
788 break;
789 case 5:
790 mtspr(SPRN_PMC5, val);
791 break;
792 case 6:
793 mtspr(SPRN_PMC6, val);
794 break;
795 #ifdef CONFIG_PPC64
796 case 7:
797 mtspr(SPRN_PMC7, val);
798 break;
799 case 8:
800 mtspr(SPRN_PMC8, val);
801 break;
802 #endif /* CONFIG_PPC64 */
803 default:
804 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
805 }
806 }
807
any_pmc_overflown(struct cpu_hw_events * cpuhw)808 static int any_pmc_overflown(struct cpu_hw_events *cpuhw)
809 {
810 int i, idx;
811
812 for (i = 0; i < cpuhw->n_events; i++) {
813 idx = cpuhw->event[i]->hw.idx;
814 if ((idx) && ((int)read_pmc(idx) < 0))
815 return idx;
816 }
817
818 return 0;
819 }
820
821 /* Called from sysrq_handle_showregs() */
perf_event_print_debug(void)822 void perf_event_print_debug(void)
823 {
824 unsigned long sdar, sier, flags;
825 u32 pmcs[MAX_HWEVENTS];
826 int i;
827
828 if (!ppmu) {
829 pr_info("Performance monitor hardware not registered.\n");
830 return;
831 }
832
833 if (!ppmu->n_counter)
834 return;
835
836 local_irq_save(flags);
837
838 pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
839 smp_processor_id(), ppmu->name, ppmu->n_counter);
840
841 for (i = 0; i < ppmu->n_counter; i++)
842 pmcs[i] = read_pmc(i + 1);
843
844 for (; i < MAX_HWEVENTS; i++)
845 pmcs[i] = 0xdeadbeef;
846
847 pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
848 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
849
850 if (ppmu->n_counter > 4)
851 pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
852 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
853
854 pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
855 mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
856
857 sdar = sier = 0;
858 #ifdef CONFIG_PPC64
859 sdar = mfspr(SPRN_SDAR);
860
861 if (ppmu->flags & PPMU_HAS_SIER)
862 sier = mfspr(SPRN_SIER);
863
864 if (ppmu->flags & PPMU_ARCH_207S) {
865 pr_info("MMCR2: %016lx EBBHR: %016lx\n",
866 mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
867 pr_info("EBBRR: %016lx BESCR: %016lx\n",
868 mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
869 }
870
871 if (ppmu->flags & PPMU_ARCH_31) {
872 pr_info("MMCR3: %016lx SIER2: %016lx SIER3: %016lx\n",
873 mfspr(SPRN_MMCR3), mfspr(SPRN_SIER2), mfspr(SPRN_SIER3));
874 }
875 #endif
876 pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
877 mfspr(SPRN_SIAR), sdar, sier);
878
879 local_irq_restore(flags);
880 }
881
882 /*
883 * Check if a set of events can all go on the PMU at once.
884 * If they can't, this will look at alternative codes for the events
885 * and see if any combination of alternative codes is feasible.
886 * The feasible set is returned in event_id[].
887 */
power_check_constraints(struct cpu_hw_events * cpuhw,u64 event_id[],unsigned int cflags[],int n_ev)888 static int power_check_constraints(struct cpu_hw_events *cpuhw,
889 u64 event_id[], unsigned int cflags[],
890 int n_ev)
891 {
892 unsigned long mask, value, nv;
893 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
894 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
895 int i, j;
896 unsigned long addf = ppmu->add_fields;
897 unsigned long tadd = ppmu->test_adder;
898 unsigned long grp_mask = ppmu->group_constraint_mask;
899 unsigned long grp_val = ppmu->group_constraint_val;
900
901 if (n_ev > ppmu->n_counter)
902 return -1;
903
904 /* First see if the events will go on as-is */
905 for (i = 0; i < n_ev; ++i) {
906 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
907 && !ppmu->limited_pmc_event(event_id[i])) {
908 ppmu->get_alternatives(event_id[i], cflags[i],
909 cpuhw->alternatives[i]);
910 event_id[i] = cpuhw->alternatives[i][0];
911 }
912 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
913 &cpuhw->avalues[i][0]))
914 return -1;
915 }
916 value = mask = 0;
917 for (i = 0; i < n_ev; ++i) {
918 nv = (value | cpuhw->avalues[i][0]) +
919 (value & cpuhw->avalues[i][0] & addf);
920
921 if (((((nv + tadd) ^ value) & mask) & (~grp_mask)) != 0)
922 break;
923
924 if (((((nv + tadd) ^ cpuhw->avalues[i][0]) & cpuhw->amasks[i][0])
925 & (~grp_mask)) != 0)
926 break;
927
928 value = nv;
929 mask |= cpuhw->amasks[i][0];
930 }
931 if (i == n_ev) {
932 if ((value & mask & grp_mask) != (mask & grp_val))
933 return -1;
934 else
935 return 0; /* all OK */
936 }
937
938 /* doesn't work, gather alternatives... */
939 if (!ppmu->get_alternatives)
940 return -1;
941 for (i = 0; i < n_ev; ++i) {
942 choice[i] = 0;
943 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
944 cpuhw->alternatives[i]);
945 for (j = 1; j < n_alt[i]; ++j)
946 ppmu->get_constraint(cpuhw->alternatives[i][j],
947 &cpuhw->amasks[i][j],
948 &cpuhw->avalues[i][j]);
949 }
950
951 /* enumerate all possibilities and see if any will work */
952 i = 0;
953 j = -1;
954 value = mask = nv = 0;
955 while (i < n_ev) {
956 if (j >= 0) {
957 /* we're backtracking, restore context */
958 value = svalues[i];
959 mask = smasks[i];
960 j = choice[i];
961 }
962 /*
963 * See if any alternative k for event_id i,
964 * where k > j, will satisfy the constraints.
965 */
966 while (++j < n_alt[i]) {
967 nv = (value | cpuhw->avalues[i][j]) +
968 (value & cpuhw->avalues[i][j] & addf);
969 if ((((nv + tadd) ^ value) & mask) == 0 &&
970 (((nv + tadd) ^ cpuhw->avalues[i][j])
971 & cpuhw->amasks[i][j]) == 0)
972 break;
973 }
974 if (j >= n_alt[i]) {
975 /*
976 * No feasible alternative, backtrack
977 * to event_id i-1 and continue enumerating its
978 * alternatives from where we got up to.
979 */
980 if (--i < 0)
981 return -1;
982 } else {
983 /*
984 * Found a feasible alternative for event_id i,
985 * remember where we got up to with this event_id,
986 * go on to the next event_id, and start with
987 * the first alternative for it.
988 */
989 choice[i] = j;
990 svalues[i] = value;
991 smasks[i] = mask;
992 value = nv;
993 mask |= cpuhw->amasks[i][j];
994 ++i;
995 j = -1;
996 }
997 }
998
999 /* OK, we have a feasible combination, tell the caller the solution */
1000 for (i = 0; i < n_ev; ++i)
1001 event_id[i] = cpuhw->alternatives[i][choice[i]];
1002 return 0;
1003 }
1004
1005 /*
1006 * Check if newly-added events have consistent settings for
1007 * exclude_{user,kernel,hv} with each other and any previously
1008 * added events.
1009 */
check_excludes(struct perf_event ** ctrs,unsigned int cflags[],int n_prev,int n_new)1010 static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
1011 int n_prev, int n_new)
1012 {
1013 int eu = 0, ek = 0, eh = 0;
1014 int i, n, first;
1015 struct perf_event *event;
1016
1017 /*
1018 * If the PMU we're on supports per event exclude settings then we
1019 * don't need to do any of this logic. NB. This assumes no PMU has both
1020 * per event exclude and limited PMCs.
1021 */
1022 if (ppmu->flags & PPMU_ARCH_207S)
1023 return 0;
1024
1025 n = n_prev + n_new;
1026 if (n <= 1)
1027 return 0;
1028
1029 first = 1;
1030 for (i = 0; i < n; ++i) {
1031 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
1032 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
1033 continue;
1034 }
1035 event = ctrs[i];
1036 if (first) {
1037 eu = event->attr.exclude_user;
1038 ek = event->attr.exclude_kernel;
1039 eh = event->attr.exclude_hv;
1040 first = 0;
1041 } else if (event->attr.exclude_user != eu ||
1042 event->attr.exclude_kernel != ek ||
1043 event->attr.exclude_hv != eh) {
1044 return -EAGAIN;
1045 }
1046 }
1047
1048 if (eu || ek || eh)
1049 for (i = 0; i < n; ++i)
1050 if (cflags[i] & PPMU_LIMITED_PMC_OK)
1051 cflags[i] |= PPMU_LIMITED_PMC_REQD;
1052
1053 return 0;
1054 }
1055
check_and_compute_delta(u64 prev,u64 val)1056 static u64 check_and_compute_delta(u64 prev, u64 val)
1057 {
1058 u64 delta = (val - prev) & 0xfffffffful;
1059
1060 /*
1061 * POWER7 can roll back counter values, if the new value is smaller
1062 * than the previous value it will cause the delta and the counter to
1063 * have bogus values unless we rolled a counter over. If a coutner is
1064 * rolled back, it will be smaller, but within 256, which is the maximum
1065 * number of events to rollback at once. If we detect a rollback
1066 * return 0. This can lead to a small lack of precision in the
1067 * counters.
1068 */
1069 if (prev > val && (prev - val) < 256)
1070 delta = 0;
1071
1072 return delta;
1073 }
1074
power_pmu_read(struct perf_event * event)1075 static void power_pmu_read(struct perf_event *event)
1076 {
1077 s64 val, delta, prev;
1078
1079 if (event->hw.state & PERF_HES_STOPPED)
1080 return;
1081
1082 if (!event->hw.idx)
1083 return;
1084
1085 if (is_ebb_event(event)) {
1086 val = read_pmc(event->hw.idx);
1087 local64_set(&event->hw.prev_count, val);
1088 return;
1089 }
1090
1091 /*
1092 * Performance monitor interrupts come even when interrupts
1093 * are soft-disabled, as long as interrupts are hard-enabled.
1094 * Therefore we treat them like NMIs.
1095 */
1096 do {
1097 prev = local64_read(&event->hw.prev_count);
1098 barrier();
1099 val = read_pmc(event->hw.idx);
1100 delta = check_and_compute_delta(prev, val);
1101 if (!delta)
1102 return;
1103 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
1104
1105 local64_add(delta, &event->count);
1106
1107 /*
1108 * A number of places program the PMC with (0x80000000 - period_left).
1109 * We never want period_left to be less than 1 because we will program
1110 * the PMC with a value >= 0x800000000 and an edge detected PMC will
1111 * roll around to 0 before taking an exception. We have seen this
1112 * on POWER8.
1113 *
1114 * To fix this, clamp the minimum value of period_left to 1.
1115 */
1116 do {
1117 prev = local64_read(&event->hw.period_left);
1118 val = prev - delta;
1119 if (val < 1)
1120 val = 1;
1121 } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
1122 }
1123
1124 /*
1125 * On some machines, PMC5 and PMC6 can't be written, don't respect
1126 * the freeze conditions, and don't generate interrupts. This tells
1127 * us if `event' is using such a PMC.
1128 */
is_limited_pmc(int pmcnum)1129 static int is_limited_pmc(int pmcnum)
1130 {
1131 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1132 && (pmcnum == 5 || pmcnum == 6);
1133 }
1134
freeze_limited_counters(struct cpu_hw_events * cpuhw,unsigned long pmc5,unsigned long pmc6)1135 static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
1136 unsigned long pmc5, unsigned long pmc6)
1137 {
1138 struct perf_event *event;
1139 u64 val, prev, delta;
1140 int i;
1141
1142 for (i = 0; i < cpuhw->n_limited; ++i) {
1143 event = cpuhw->limited_counter[i];
1144 if (!event->hw.idx)
1145 continue;
1146 val = (event->hw.idx == 5) ? pmc5 : pmc6;
1147 prev = local64_read(&event->hw.prev_count);
1148 event->hw.idx = 0;
1149 delta = check_and_compute_delta(prev, val);
1150 if (delta)
1151 local64_add(delta, &event->count);
1152 }
1153 }
1154
thaw_limited_counters(struct cpu_hw_events * cpuhw,unsigned long pmc5,unsigned long pmc6)1155 static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
1156 unsigned long pmc5, unsigned long pmc6)
1157 {
1158 struct perf_event *event;
1159 u64 val, prev;
1160 int i;
1161
1162 for (i = 0; i < cpuhw->n_limited; ++i) {
1163 event = cpuhw->limited_counter[i];
1164 event->hw.idx = cpuhw->limited_hwidx[i];
1165 val = (event->hw.idx == 5) ? pmc5 : pmc6;
1166 prev = local64_read(&event->hw.prev_count);
1167 if (check_and_compute_delta(prev, val))
1168 local64_set(&event->hw.prev_count, val);
1169 perf_event_update_userpage(event);
1170 }
1171 }
1172
1173 /*
1174 * Since limited events don't respect the freeze conditions, we
1175 * have to read them immediately after freezing or unfreezing the
1176 * other events. We try to keep the values from the limited
1177 * events as consistent as possible by keeping the delay (in
1178 * cycles and instructions) between freezing/unfreezing and reading
1179 * the limited events as small and consistent as possible.
1180 * Therefore, if any limited events are in use, we read them
1181 * both, and always in the same order, to minimize variability,
1182 * and do it inside the same asm that writes MMCR0.
1183 */
write_mmcr0(struct cpu_hw_events * cpuhw,unsigned long mmcr0)1184 static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1185 {
1186 unsigned long pmc5, pmc6;
1187
1188 if (!cpuhw->n_limited) {
1189 mtspr(SPRN_MMCR0, mmcr0);
1190 return;
1191 }
1192
1193 /*
1194 * Write MMCR0, then read PMC5 and PMC6 immediately.
1195 * To ensure we don't get a performance monitor interrupt
1196 * between writing MMCR0 and freezing/thawing the limited
1197 * events, we first write MMCR0 with the event overflow
1198 * interrupt enable bits turned off.
1199 */
1200 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1201 : "=&r" (pmc5), "=&r" (pmc6)
1202 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1203 "i" (SPRN_MMCR0),
1204 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1205
1206 if (mmcr0 & MMCR0_FC)
1207 freeze_limited_counters(cpuhw, pmc5, pmc6);
1208 else
1209 thaw_limited_counters(cpuhw, pmc5, pmc6);
1210
1211 /*
1212 * Write the full MMCR0 including the event overflow interrupt
1213 * enable bits, if necessary.
1214 */
1215 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1216 mtspr(SPRN_MMCR0, mmcr0);
1217 }
1218
1219 /*
1220 * Disable all events to prevent PMU interrupts and to allow
1221 * events to be added or removed.
1222 */
power_pmu_disable(struct pmu * pmu)1223 static void power_pmu_disable(struct pmu *pmu)
1224 {
1225 struct cpu_hw_events *cpuhw;
1226 unsigned long flags, mmcr0, val, mmcra;
1227
1228 if (!ppmu)
1229 return;
1230 local_irq_save(flags);
1231 cpuhw = this_cpu_ptr(&cpu_hw_events);
1232
1233 if (!cpuhw->disabled) {
1234 /*
1235 * Check if we ever enabled the PMU on this cpu.
1236 */
1237 if (!cpuhw->pmcs_enabled) {
1238 ppc_enable_pmcs();
1239 cpuhw->pmcs_enabled = 1;
1240 }
1241
1242 /*
1243 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
1244 * Also clear PMXE to disable PMI's getting triggered in some
1245 * corner cases during PMU disable.
1246 */
1247 val = mmcr0 = mfspr(SPRN_MMCR0);
1248 val |= MMCR0_FC;
1249 val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
1250 MMCR0_PMXE | MMCR0_FC56);
1251 /* Set mmcr0 PMCCEXT for p10 */
1252 if (ppmu->flags & PPMU_ARCH_31)
1253 val |= MMCR0_PMCCEXT;
1254
1255 /*
1256 * The barrier is to make sure the mtspr has been
1257 * executed and the PMU has frozen the events etc.
1258 * before we return.
1259 */
1260 write_mmcr0(cpuhw, val);
1261 mb();
1262 isync();
1263
1264 /*
1265 * Some corner cases could clear the PMU counter overflow
1266 * while a masked PMI is pending. One such case is when
1267 * a PMI happens during interrupt replay and perf counter
1268 * values are cleared by PMU callbacks before replay.
1269 *
1270 * Disable the interrupt by clearing the paca bit for PMI
1271 * since we are disabling the PMU now. Otherwise provide a
1272 * warning if there is PMI pending, but no counter is found
1273 * overflown.
1274 *
1275 * Since power_pmu_disable runs under local_irq_save, it
1276 * could happen that code hits a PMC overflow without PMI
1277 * pending in paca. Hence only clear PMI pending if it was
1278 * set.
1279 *
1280 * If a PMI is pending, then MSR[EE] must be disabled (because
1281 * the masked PMI handler disabling EE). So it is safe to
1282 * call clear_pmi_irq_pending().
1283 */
1284 if (pmi_irq_pending())
1285 clear_pmi_irq_pending();
1286
1287 val = mmcra = cpuhw->mmcr.mmcra;
1288
1289 /*
1290 * Disable instruction sampling if it was enabled
1291 */
1292 if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE)
1293 val &= ~MMCRA_SAMPLE_ENABLE;
1294
1295 /* Disable BHRB via mmcra (BHRBRD) for p10 */
1296 if (ppmu->flags & PPMU_ARCH_31)
1297 val |= MMCRA_BHRB_DISABLE;
1298
1299 /*
1300 * Write SPRN_MMCRA if mmcra has either disabled
1301 * instruction sampling or BHRB.
1302 */
1303 if (val != mmcra) {
1304 mtspr(SPRN_MMCRA, mmcra);
1305 mb();
1306 isync();
1307 }
1308
1309 cpuhw->disabled = 1;
1310 cpuhw->n_added = 0;
1311
1312 ebb_switch_out(mmcr0);
1313
1314 #ifdef CONFIG_PPC64
1315 /*
1316 * These are readable by userspace, may contain kernel
1317 * addresses and are not switched by context switch, so clear
1318 * them now to avoid leaking anything to userspace in general
1319 * including to another process.
1320 */
1321 if (ppmu->flags & PPMU_ARCH_207S) {
1322 mtspr(SPRN_SDAR, 0);
1323 mtspr(SPRN_SIAR, 0);
1324 }
1325 #endif
1326 }
1327
1328 local_irq_restore(flags);
1329 }
1330
1331 /*
1332 * Re-enable all events if disable == 0.
1333 * If we were previously disabled and events were added, then
1334 * put the new config on the PMU.
1335 */
power_pmu_enable(struct pmu * pmu)1336 static void power_pmu_enable(struct pmu *pmu)
1337 {
1338 struct perf_event *event;
1339 struct cpu_hw_events *cpuhw;
1340 unsigned long flags;
1341 long i;
1342 unsigned long val, mmcr0;
1343 s64 left;
1344 unsigned int hwc_index[MAX_HWEVENTS];
1345 int n_lim;
1346 int idx;
1347 bool ebb;
1348
1349 if (!ppmu)
1350 return;
1351 local_irq_save(flags);
1352
1353 cpuhw = this_cpu_ptr(&cpu_hw_events);
1354 if (!cpuhw->disabled)
1355 goto out;
1356
1357 if (cpuhw->n_events == 0) {
1358 ppc_set_pmu_inuse(0);
1359 goto out;
1360 }
1361
1362 cpuhw->disabled = 0;
1363
1364 /*
1365 * EBB requires an exclusive group and all events must have the EBB
1366 * flag set, or not set, so we can just check a single event. Also we
1367 * know we have at least one event.
1368 */
1369 ebb = is_ebb_event(cpuhw->event[0]);
1370
1371 /*
1372 * If we didn't change anything, or only removed events,
1373 * no need to recalculate MMCR* settings and reset the PMCs.
1374 * Just reenable the PMU with the current MMCR* settings
1375 * (possibly updated for removal of events).
1376 */
1377 if (!cpuhw->n_added) {
1378 /*
1379 * If there is any active event with an overflown PMC
1380 * value, set back PACA_IRQ_PMI which would have been
1381 * cleared in power_pmu_disable().
1382 */
1383 hard_irq_disable();
1384 if (any_pmc_overflown(cpuhw))
1385 set_pmi_irq_pending();
1386
1387 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE);
1388 mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1);
1389 if (ppmu->flags & PPMU_ARCH_31)
1390 mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3);
1391 goto out_enable;
1392 }
1393
1394 /*
1395 * Clear all MMCR settings and recompute them for the new set of events.
1396 */
1397 memset(&cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
1398
1399 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
1400 &cpuhw->mmcr, cpuhw->event)) {
1401 /* shouldn't ever get here */
1402 printk(KERN_ERR "oops compute_mmcr failed\n");
1403 goto out;
1404 }
1405
1406 if (!(ppmu->flags & PPMU_ARCH_207S)) {
1407 /*
1408 * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
1409 * bits for the first event. We have already checked that all
1410 * events have the same value for these bits as the first event.
1411 */
1412 event = cpuhw->event[0];
1413 if (event->attr.exclude_user)
1414 cpuhw->mmcr.mmcr0 |= MMCR0_FCP;
1415 if (event->attr.exclude_kernel)
1416 cpuhw->mmcr.mmcr0 |= freeze_events_kernel;
1417 if (event->attr.exclude_hv)
1418 cpuhw->mmcr.mmcr0 |= MMCR0_FCHV;
1419 }
1420
1421 /*
1422 * Write the new configuration to MMCR* with the freeze
1423 * bit set and set the hardware events to their initial values.
1424 * Then unfreeze the events.
1425 */
1426 ppc_set_pmu_inuse(1);
1427 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE);
1428 mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1);
1429 mtspr(SPRN_MMCR0, (cpuhw->mmcr.mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1430 | MMCR0_FC);
1431 if (ppmu->flags & PPMU_ARCH_207S)
1432 mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2);
1433
1434 if (ppmu->flags & PPMU_ARCH_31)
1435 mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3);
1436
1437 /*
1438 * Read off any pre-existing events that need to move
1439 * to another PMC.
1440 */
1441 for (i = 0; i < cpuhw->n_events; ++i) {
1442 event = cpuhw->event[i];
1443 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1444 power_pmu_read(event);
1445 write_pmc(event->hw.idx, 0);
1446 event->hw.idx = 0;
1447 }
1448 }
1449
1450 /*
1451 * Initialize the PMCs for all the new and moved events.
1452 */
1453 cpuhw->n_limited = n_lim = 0;
1454 for (i = 0; i < cpuhw->n_events; ++i) {
1455 event = cpuhw->event[i];
1456 if (event->hw.idx)
1457 continue;
1458 idx = hwc_index[i] + 1;
1459 if (is_limited_pmc(idx)) {
1460 cpuhw->limited_counter[n_lim] = event;
1461 cpuhw->limited_hwidx[n_lim] = idx;
1462 ++n_lim;
1463 continue;
1464 }
1465
1466 if (ebb)
1467 val = local64_read(&event->hw.prev_count);
1468 else {
1469 val = 0;
1470 if (event->hw.sample_period) {
1471 left = local64_read(&event->hw.period_left);
1472 if (left < 0x80000000L)
1473 val = 0x80000000L - left;
1474 }
1475 local64_set(&event->hw.prev_count, val);
1476 }
1477
1478 event->hw.idx = idx;
1479 if (event->hw.state & PERF_HES_STOPPED)
1480 val = 0;
1481 write_pmc(idx, val);
1482
1483 perf_event_update_userpage(event);
1484 }
1485 cpuhw->n_limited = n_lim;
1486 cpuhw->mmcr.mmcr0 |= MMCR0_PMXE | MMCR0_FCECE;
1487
1488 out_enable:
1489 pmao_restore_workaround(ebb);
1490
1491 mmcr0 = ebb_switch_in(ebb, cpuhw);
1492
1493 mb();
1494 if (cpuhw->bhrb_users)
1495 ppmu->config_bhrb(cpuhw->bhrb_filter);
1496
1497 write_mmcr0(cpuhw, mmcr0);
1498
1499 /*
1500 * Enable instruction sampling if necessary
1501 */
1502 if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE) {
1503 mb();
1504 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra);
1505 }
1506
1507 out:
1508
1509 local_irq_restore(flags);
1510 }
1511
collect_events(struct perf_event * group,int max_count,struct perf_event * ctrs[],u64 * events,unsigned int * flags)1512 static int collect_events(struct perf_event *group, int max_count,
1513 struct perf_event *ctrs[], u64 *events,
1514 unsigned int *flags)
1515 {
1516 int n = 0;
1517 struct perf_event *event;
1518
1519 if (group->pmu->task_ctx_nr == perf_hw_context) {
1520 if (n >= max_count)
1521 return -1;
1522 ctrs[n] = group;
1523 flags[n] = group->hw.event_base;
1524 events[n++] = group->hw.config;
1525 }
1526 for_each_sibling_event(event, group) {
1527 if (event->pmu->task_ctx_nr == perf_hw_context &&
1528 event->state != PERF_EVENT_STATE_OFF) {
1529 if (n >= max_count)
1530 return -1;
1531 ctrs[n] = event;
1532 flags[n] = event->hw.event_base;
1533 events[n++] = event->hw.config;
1534 }
1535 }
1536 return n;
1537 }
1538
1539 /*
1540 * Add an event to the PMU.
1541 * If all events are not already frozen, then we disable and
1542 * re-enable the PMU in order to get hw_perf_enable to do the
1543 * actual work of reconfiguring the PMU.
1544 */
power_pmu_add(struct perf_event * event,int ef_flags)1545 static int power_pmu_add(struct perf_event *event, int ef_flags)
1546 {
1547 struct cpu_hw_events *cpuhw;
1548 unsigned long flags;
1549 int n0;
1550 int ret = -EAGAIN;
1551
1552 local_irq_save(flags);
1553 perf_pmu_disable(event->pmu);
1554
1555 /*
1556 * Add the event to the list (if there is room)
1557 * and check whether the total set is still feasible.
1558 */
1559 cpuhw = this_cpu_ptr(&cpu_hw_events);
1560 n0 = cpuhw->n_events;
1561 if (n0 >= ppmu->n_counter)
1562 goto out;
1563 cpuhw->event[n0] = event;
1564 cpuhw->events[n0] = event->hw.config;
1565 cpuhw->flags[n0] = event->hw.event_base;
1566
1567 /*
1568 * This event may have been disabled/stopped in record_and_restart()
1569 * because we exceeded the ->event_limit. If re-starting the event,
1570 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1571 * notification is re-enabled.
1572 */
1573 if (!(ef_flags & PERF_EF_START))
1574 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1575 else
1576 event->hw.state = 0;
1577
1578 /*
1579 * If group events scheduling transaction was started,
1580 * skip the schedulability test here, it will be performed
1581 * at commit time(->commit_txn) as a whole
1582 */
1583 if (cpuhw->txn_flags & PERF_PMU_TXN_ADD)
1584 goto nocheck;
1585
1586 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1587 goto out;
1588 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1589 goto out;
1590 event->hw.config = cpuhw->events[n0];
1591
1592 nocheck:
1593 ebb_event_add(event);
1594
1595 ++cpuhw->n_events;
1596 ++cpuhw->n_added;
1597
1598 ret = 0;
1599 out:
1600 if (has_branch_stack(event)) {
1601 u64 bhrb_filter = -1;
1602
1603 if (ppmu->bhrb_filter_map)
1604 bhrb_filter = ppmu->bhrb_filter_map(
1605 event->attr.branch_sample_type);
1606
1607 if (bhrb_filter != -1) {
1608 cpuhw->bhrb_filter = bhrb_filter;
1609 power_pmu_bhrb_enable(event);
1610 }
1611 }
1612
1613 perf_pmu_enable(event->pmu);
1614 local_irq_restore(flags);
1615 return ret;
1616 }
1617
1618 /*
1619 * Remove an event from the PMU.
1620 */
power_pmu_del(struct perf_event * event,int ef_flags)1621 static void power_pmu_del(struct perf_event *event, int ef_flags)
1622 {
1623 struct cpu_hw_events *cpuhw;
1624 long i;
1625 unsigned long flags;
1626
1627 local_irq_save(flags);
1628 perf_pmu_disable(event->pmu);
1629
1630 power_pmu_read(event);
1631
1632 cpuhw = this_cpu_ptr(&cpu_hw_events);
1633 for (i = 0; i < cpuhw->n_events; ++i) {
1634 if (event == cpuhw->event[i]) {
1635 while (++i < cpuhw->n_events) {
1636 cpuhw->event[i-1] = cpuhw->event[i];
1637 cpuhw->events[i-1] = cpuhw->events[i];
1638 cpuhw->flags[i-1] = cpuhw->flags[i];
1639 }
1640 --cpuhw->n_events;
1641 ppmu->disable_pmc(event->hw.idx - 1, &cpuhw->mmcr);
1642 if (event->hw.idx) {
1643 write_pmc(event->hw.idx, 0);
1644 event->hw.idx = 0;
1645 }
1646 perf_event_update_userpage(event);
1647 break;
1648 }
1649 }
1650 for (i = 0; i < cpuhw->n_limited; ++i)
1651 if (event == cpuhw->limited_counter[i])
1652 break;
1653 if (i < cpuhw->n_limited) {
1654 while (++i < cpuhw->n_limited) {
1655 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
1656 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1657 }
1658 --cpuhw->n_limited;
1659 }
1660 if (cpuhw->n_events == 0) {
1661 /* disable exceptions if no events are running */
1662 cpuhw->mmcr.mmcr0 &= ~(MMCR0_PMXE | MMCR0_FCECE);
1663 }
1664
1665 if (has_branch_stack(event))
1666 power_pmu_bhrb_disable(event);
1667
1668 perf_pmu_enable(event->pmu);
1669 local_irq_restore(flags);
1670 }
1671
1672 /*
1673 * POWER-PMU does not support disabling individual counters, hence
1674 * program their cycle counter to their max value and ignore the interrupts.
1675 */
1676
power_pmu_start(struct perf_event * event,int ef_flags)1677 static void power_pmu_start(struct perf_event *event, int ef_flags)
1678 {
1679 unsigned long flags;
1680 s64 left;
1681 unsigned long val;
1682
1683 if (!event->hw.idx || !event->hw.sample_period)
1684 return;
1685
1686 if (!(event->hw.state & PERF_HES_STOPPED))
1687 return;
1688
1689 if (ef_flags & PERF_EF_RELOAD)
1690 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1691
1692 local_irq_save(flags);
1693 perf_pmu_disable(event->pmu);
1694
1695 event->hw.state = 0;
1696 left = local64_read(&event->hw.period_left);
1697
1698 val = 0;
1699 if (left < 0x80000000L)
1700 val = 0x80000000L - left;
1701
1702 write_pmc(event->hw.idx, val);
1703
1704 perf_event_update_userpage(event);
1705 perf_pmu_enable(event->pmu);
1706 local_irq_restore(flags);
1707 }
1708
power_pmu_stop(struct perf_event * event,int ef_flags)1709 static void power_pmu_stop(struct perf_event *event, int ef_flags)
1710 {
1711 unsigned long flags;
1712
1713 if (!event->hw.idx || !event->hw.sample_period)
1714 return;
1715
1716 if (event->hw.state & PERF_HES_STOPPED)
1717 return;
1718
1719 local_irq_save(flags);
1720 perf_pmu_disable(event->pmu);
1721
1722 power_pmu_read(event);
1723 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1724 write_pmc(event->hw.idx, 0);
1725
1726 perf_event_update_userpage(event);
1727 perf_pmu_enable(event->pmu);
1728 local_irq_restore(flags);
1729 }
1730
1731 /*
1732 * Start group events scheduling transaction
1733 * Set the flag to make pmu::enable() not perform the
1734 * schedulability test, it will be performed at commit time
1735 *
1736 * We only support PERF_PMU_TXN_ADD transactions. Save the
1737 * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
1738 * transactions.
1739 */
power_pmu_start_txn(struct pmu * pmu,unsigned int txn_flags)1740 static void power_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
1741 {
1742 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1743
1744 WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
1745
1746 cpuhw->txn_flags = txn_flags;
1747 if (txn_flags & ~PERF_PMU_TXN_ADD)
1748 return;
1749
1750 perf_pmu_disable(pmu);
1751 cpuhw->n_txn_start = cpuhw->n_events;
1752 }
1753
1754 /*
1755 * Stop group events scheduling transaction
1756 * Clear the flag and pmu::enable() will perform the
1757 * schedulability test.
1758 */
power_pmu_cancel_txn(struct pmu * pmu)1759 static void power_pmu_cancel_txn(struct pmu *pmu)
1760 {
1761 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1762 unsigned int txn_flags;
1763
1764 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1765
1766 txn_flags = cpuhw->txn_flags;
1767 cpuhw->txn_flags = 0;
1768 if (txn_flags & ~PERF_PMU_TXN_ADD)
1769 return;
1770
1771 perf_pmu_enable(pmu);
1772 }
1773
1774 /*
1775 * Commit group events scheduling transaction
1776 * Perform the group schedulability test as a whole
1777 * Return 0 if success
1778 */
power_pmu_commit_txn(struct pmu * pmu)1779 static int power_pmu_commit_txn(struct pmu *pmu)
1780 {
1781 struct cpu_hw_events *cpuhw;
1782 long i, n;
1783
1784 if (!ppmu)
1785 return -EAGAIN;
1786
1787 cpuhw = this_cpu_ptr(&cpu_hw_events);
1788 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1789
1790 if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
1791 cpuhw->txn_flags = 0;
1792 return 0;
1793 }
1794
1795 n = cpuhw->n_events;
1796 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1797 return -EAGAIN;
1798 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1799 if (i < 0)
1800 return -EAGAIN;
1801
1802 for (i = cpuhw->n_txn_start; i < n; ++i)
1803 cpuhw->event[i]->hw.config = cpuhw->events[i];
1804
1805 cpuhw->txn_flags = 0;
1806 perf_pmu_enable(pmu);
1807 return 0;
1808 }
1809
1810 /*
1811 * Return 1 if we might be able to put event on a limited PMC,
1812 * or 0 if not.
1813 * An event can only go on a limited PMC if it counts something
1814 * that a limited PMC can count, doesn't require interrupts, and
1815 * doesn't exclude any processor mode.
1816 */
can_go_on_limited_pmc(struct perf_event * event,u64 ev,unsigned int flags)1817 static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1818 unsigned int flags)
1819 {
1820 int n;
1821 u64 alt[MAX_EVENT_ALTERNATIVES];
1822
1823 if (event->attr.exclude_user
1824 || event->attr.exclude_kernel
1825 || event->attr.exclude_hv
1826 || event->attr.sample_period)
1827 return 0;
1828
1829 if (ppmu->limited_pmc_event(ev))
1830 return 1;
1831
1832 /*
1833 * The requested event_id isn't on a limited PMC already;
1834 * see if any alternative code goes on a limited PMC.
1835 */
1836 if (!ppmu->get_alternatives)
1837 return 0;
1838
1839 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1840 n = ppmu->get_alternatives(ev, flags, alt);
1841
1842 return n > 0;
1843 }
1844
1845 /*
1846 * Find an alternative event_id that goes on a normal PMC, if possible,
1847 * and return the event_id code, or 0 if there is no such alternative.
1848 * (Note: event_id code 0 is "don't count" on all machines.)
1849 */
normal_pmc_alternative(u64 ev,unsigned long flags)1850 static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1851 {
1852 u64 alt[MAX_EVENT_ALTERNATIVES];
1853 int n;
1854
1855 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1856 n = ppmu->get_alternatives(ev, flags, alt);
1857 if (!n)
1858 return 0;
1859 return alt[0];
1860 }
1861
1862 /* Number of perf_events counting hardware events */
1863 static atomic_t num_events;
1864 /* Used to avoid races in calling reserve/release_pmc_hardware */
1865 static DEFINE_MUTEX(pmc_reserve_mutex);
1866
1867 /*
1868 * Release the PMU if this is the last perf_event.
1869 */
hw_perf_event_destroy(struct perf_event * event)1870 static void hw_perf_event_destroy(struct perf_event *event)
1871 {
1872 if (!atomic_add_unless(&num_events, -1, 1)) {
1873 mutex_lock(&pmc_reserve_mutex);
1874 if (atomic_dec_return(&num_events) == 0)
1875 release_pmc_hardware();
1876 mutex_unlock(&pmc_reserve_mutex);
1877 }
1878 }
1879
1880 /*
1881 * Translate a generic cache event_id config to a raw event_id code.
1882 */
hw_perf_cache_event(u64 config,u64 * eventp)1883 static int hw_perf_cache_event(u64 config, u64 *eventp)
1884 {
1885 unsigned long type, op, result;
1886 u64 ev;
1887
1888 if (!ppmu->cache_events)
1889 return -EINVAL;
1890
1891 /* unpack config */
1892 type = config & 0xff;
1893 op = (config >> 8) & 0xff;
1894 result = (config >> 16) & 0xff;
1895
1896 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1897 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1898 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1899 return -EINVAL;
1900
1901 ev = (*ppmu->cache_events)[type][op][result];
1902 if (ev == 0)
1903 return -EOPNOTSUPP;
1904 if (ev == -1)
1905 return -EINVAL;
1906 *eventp = ev;
1907 return 0;
1908 }
1909
is_event_blacklisted(u64 ev)1910 static bool is_event_blacklisted(u64 ev)
1911 {
1912 int i;
1913
1914 for (i=0; i < ppmu->n_blacklist_ev; i++) {
1915 if (ppmu->blacklist_ev[i] == ev)
1916 return true;
1917 }
1918
1919 return false;
1920 }
1921
power_pmu_event_init(struct perf_event * event)1922 static int power_pmu_event_init(struct perf_event *event)
1923 {
1924 u64 ev;
1925 unsigned long flags, irq_flags;
1926 struct perf_event *ctrs[MAX_HWEVENTS];
1927 u64 events[MAX_HWEVENTS];
1928 unsigned int cflags[MAX_HWEVENTS];
1929 int n;
1930 int err;
1931 struct cpu_hw_events *cpuhw;
1932
1933 if (!ppmu)
1934 return -ENOENT;
1935
1936 if (has_branch_stack(event)) {
1937 /* PMU has BHRB enabled */
1938 if (!(ppmu->flags & PPMU_ARCH_207S))
1939 return -EOPNOTSUPP;
1940 }
1941
1942 switch (event->attr.type) {
1943 case PERF_TYPE_HARDWARE:
1944 ev = event->attr.config;
1945 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1946 return -EOPNOTSUPP;
1947
1948 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1949 return -EINVAL;
1950 ev = ppmu->generic_events[ev];
1951 break;
1952 case PERF_TYPE_HW_CACHE:
1953 err = hw_perf_cache_event(event->attr.config, &ev);
1954 if (err)
1955 return err;
1956
1957 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1958 return -EINVAL;
1959 break;
1960 case PERF_TYPE_RAW:
1961 ev = event->attr.config;
1962
1963 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1964 return -EINVAL;
1965 break;
1966 default:
1967 return -ENOENT;
1968 }
1969
1970 event->hw.config_base = ev;
1971 event->hw.idx = 0;
1972
1973 /*
1974 * If we are not running on a hypervisor, force the
1975 * exclude_hv bit to 0 so that we don't care what
1976 * the user set it to.
1977 */
1978 if (!firmware_has_feature(FW_FEATURE_LPAR))
1979 event->attr.exclude_hv = 0;
1980
1981 /*
1982 * If this is a per-task event, then we can use
1983 * PM_RUN_* events interchangeably with their non RUN_*
1984 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1985 * XXX we should check if the task is an idle task.
1986 */
1987 flags = 0;
1988 if (event->attach_state & PERF_ATTACH_TASK)
1989 flags |= PPMU_ONLY_COUNT_RUN;
1990
1991 /*
1992 * If this machine has limited events, check whether this
1993 * event_id could go on a limited event.
1994 */
1995 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1996 if (can_go_on_limited_pmc(event, ev, flags)) {
1997 flags |= PPMU_LIMITED_PMC_OK;
1998 } else if (ppmu->limited_pmc_event(ev)) {
1999 /*
2000 * The requested event_id is on a limited PMC,
2001 * but we can't use a limited PMC; see if any
2002 * alternative goes on a normal PMC.
2003 */
2004 ev = normal_pmc_alternative(ev, flags);
2005 if (!ev)
2006 return -EINVAL;
2007 }
2008 }
2009
2010 /* Extra checks for EBB */
2011 err = ebb_event_check(event);
2012 if (err)
2013 return err;
2014
2015 /*
2016 * If this is in a group, check if it can go on with all the
2017 * other hardware events in the group. We assume the event
2018 * hasn't been linked into its leader's sibling list at this point.
2019 */
2020 n = 0;
2021 if (event->group_leader != event) {
2022 n = collect_events(event->group_leader, ppmu->n_counter - 1,
2023 ctrs, events, cflags);
2024 if (n < 0)
2025 return -EINVAL;
2026 }
2027 events[n] = ev;
2028 ctrs[n] = event;
2029 cflags[n] = flags;
2030 if (check_excludes(ctrs, cflags, n, 1))
2031 return -EINVAL;
2032
2033 local_irq_save(irq_flags);
2034 cpuhw = this_cpu_ptr(&cpu_hw_events);
2035
2036 err = power_check_constraints(cpuhw, events, cflags, n + 1);
2037
2038 if (has_branch_stack(event)) {
2039 u64 bhrb_filter = -1;
2040
2041 if (ppmu->bhrb_filter_map)
2042 bhrb_filter = ppmu->bhrb_filter_map(
2043 event->attr.branch_sample_type);
2044
2045 if (bhrb_filter == -1) {
2046 local_irq_restore(irq_flags);
2047 return -EOPNOTSUPP;
2048 }
2049 cpuhw->bhrb_filter = bhrb_filter;
2050 }
2051
2052 local_irq_restore(irq_flags);
2053 if (err)
2054 return -EINVAL;
2055
2056 event->hw.config = events[n];
2057 event->hw.event_base = cflags[n];
2058 event->hw.last_period = event->hw.sample_period;
2059 local64_set(&event->hw.period_left, event->hw.last_period);
2060
2061 /*
2062 * For EBB events we just context switch the PMC value, we don't do any
2063 * of the sample_period logic. We use hw.prev_count for this.
2064 */
2065 if (is_ebb_event(event))
2066 local64_set(&event->hw.prev_count, 0);
2067
2068 /*
2069 * See if we need to reserve the PMU.
2070 * If no events are currently in use, then we have to take a
2071 * mutex to ensure that we don't race with another task doing
2072 * reserve_pmc_hardware or release_pmc_hardware.
2073 */
2074 err = 0;
2075 if (!atomic_inc_not_zero(&num_events)) {
2076 mutex_lock(&pmc_reserve_mutex);
2077 if (atomic_read(&num_events) == 0 &&
2078 reserve_pmc_hardware(perf_event_interrupt))
2079 err = -EBUSY;
2080 else
2081 atomic_inc(&num_events);
2082 mutex_unlock(&pmc_reserve_mutex);
2083 }
2084 event->destroy = hw_perf_event_destroy;
2085
2086 return err;
2087 }
2088
power_pmu_event_idx(struct perf_event * event)2089 static int power_pmu_event_idx(struct perf_event *event)
2090 {
2091 return event->hw.idx;
2092 }
2093
power_events_sysfs_show(struct device * dev,struct device_attribute * attr,char * page)2094 ssize_t power_events_sysfs_show(struct device *dev,
2095 struct device_attribute *attr, char *page)
2096 {
2097 struct perf_pmu_events_attr *pmu_attr;
2098
2099 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
2100
2101 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
2102 }
2103
2104 static struct pmu power_pmu = {
2105 .pmu_enable = power_pmu_enable,
2106 .pmu_disable = power_pmu_disable,
2107 .event_init = power_pmu_event_init,
2108 .add = power_pmu_add,
2109 .del = power_pmu_del,
2110 .start = power_pmu_start,
2111 .stop = power_pmu_stop,
2112 .read = power_pmu_read,
2113 .start_txn = power_pmu_start_txn,
2114 .cancel_txn = power_pmu_cancel_txn,
2115 .commit_txn = power_pmu_commit_txn,
2116 .event_idx = power_pmu_event_idx,
2117 .sched_task = power_pmu_sched_task,
2118 };
2119
2120 /*
2121 * A counter has overflowed; update its count and record
2122 * things if requested. Note that interrupts are hard-disabled
2123 * here so there is no possibility of being interrupted.
2124 */
record_and_restart(struct perf_event * event,unsigned long val,struct pt_regs * regs)2125 static void record_and_restart(struct perf_event *event, unsigned long val,
2126 struct pt_regs *regs)
2127 {
2128 u64 period = event->hw.sample_period;
2129 s64 prev, delta, left;
2130 int record = 0;
2131
2132 if (event->hw.state & PERF_HES_STOPPED) {
2133 write_pmc(event->hw.idx, 0);
2134 return;
2135 }
2136
2137 /* we don't have to worry about interrupts here */
2138 prev = local64_read(&event->hw.prev_count);
2139 delta = check_and_compute_delta(prev, val);
2140 local64_add(delta, &event->count);
2141
2142 /*
2143 * See if the total period for this event has expired,
2144 * and update for the next period.
2145 */
2146 val = 0;
2147 left = local64_read(&event->hw.period_left) - delta;
2148 if (delta == 0)
2149 left++;
2150 if (period) {
2151 if (left <= 0) {
2152 left += period;
2153 if (left <= 0)
2154 left = period;
2155
2156 /*
2157 * If address is not requested in the sample via
2158 * PERF_SAMPLE_IP, just record that sample irrespective
2159 * of SIAR valid check.
2160 */
2161 if (event->attr.sample_type & PERF_SAMPLE_IP)
2162 record = siar_valid(regs);
2163 else
2164 record = 1;
2165
2166 event->hw.last_period = event->hw.sample_period;
2167 }
2168 if (left < 0x80000000LL)
2169 val = 0x80000000LL - left;
2170 }
2171
2172 write_pmc(event->hw.idx, val);
2173 local64_set(&event->hw.prev_count, val);
2174 local64_set(&event->hw.period_left, left);
2175 perf_event_update_userpage(event);
2176
2177 /*
2178 * Due to hardware limitation, sometimes SIAR could sample a kernel
2179 * address even when freeze on supervisor state (kernel) is set in
2180 * MMCR2. Check attr.exclude_kernel and address to drop the sample in
2181 * these cases.
2182 */
2183 if (event->attr.exclude_kernel &&
2184 (event->attr.sample_type & PERF_SAMPLE_IP) &&
2185 is_kernel_addr(mfspr(SPRN_SIAR)))
2186 record = 0;
2187
2188 /*
2189 * Finally record data if requested.
2190 */
2191 if (record) {
2192 struct perf_sample_data data;
2193
2194 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
2195
2196 if (event->attr.sample_type &
2197 (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
2198 perf_get_data_addr(event, regs, &data.addr);
2199
2200 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
2201 struct cpu_hw_events *cpuhw;
2202 cpuhw = this_cpu_ptr(&cpu_hw_events);
2203 power_pmu_bhrb_read(event, cpuhw);
2204 data.br_stack = &cpuhw->bhrb_stack;
2205 }
2206
2207 if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
2208 ppmu->get_mem_data_src)
2209 ppmu->get_mem_data_src(&data.data_src, ppmu->flags, regs);
2210
2211 if (event->attr.sample_type & PERF_SAMPLE_WEIGHT &&
2212 ppmu->get_mem_weight)
2213 ppmu->get_mem_weight(&data.weight);
2214
2215 if (perf_event_overflow(event, &data, regs))
2216 power_pmu_stop(event, 0);
2217 } else if (period) {
2218 /* Account for interrupt in case of invalid SIAR */
2219 if (perf_event_account_interrupt(event))
2220 power_pmu_stop(event, 0);
2221 }
2222 }
2223
2224 /*
2225 * Called from generic code to get the misc flags (i.e. processor mode)
2226 * for an event_id.
2227 */
perf_misc_flags(struct pt_regs * regs)2228 unsigned long perf_misc_flags(struct pt_regs *regs)
2229 {
2230 u32 flags = perf_get_misc_flags(regs);
2231
2232 if (flags)
2233 return flags;
2234 return user_mode(regs) ? PERF_RECORD_MISC_USER :
2235 PERF_RECORD_MISC_KERNEL;
2236 }
2237
2238 /*
2239 * Called from generic code to get the instruction pointer
2240 * for an event_id.
2241 */
perf_instruction_pointer(struct pt_regs * regs)2242 unsigned long perf_instruction_pointer(struct pt_regs *regs)
2243 {
2244 bool use_siar = regs_use_siar(regs);
2245
2246 if (use_siar && siar_valid(regs))
2247 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
2248 else if (use_siar)
2249 return 0; // no valid instruction pointer
2250 else
2251 return regs->nip;
2252 }
2253
pmc_overflow_power7(unsigned long val)2254 static bool pmc_overflow_power7(unsigned long val)
2255 {
2256 /*
2257 * Events on POWER7 can roll back if a speculative event doesn't
2258 * eventually complete. Unfortunately in some rare cases they will
2259 * raise a performance monitor exception. We need to catch this to
2260 * ensure we reset the PMC. In all cases the PMC will be 256 or less
2261 * cycles from overflow.
2262 *
2263 * We only do this if the first pass fails to find any overflowing
2264 * PMCs because a user might set a period of less than 256 and we
2265 * don't want to mistakenly reset them.
2266 */
2267 if ((0x80000000 - val) <= 256)
2268 return true;
2269
2270 return false;
2271 }
2272
pmc_overflow(unsigned long val)2273 static bool pmc_overflow(unsigned long val)
2274 {
2275 if ((int)val < 0)
2276 return true;
2277
2278 return false;
2279 }
2280
2281 /*
2282 * Performance monitor interrupt stuff
2283 */
__perf_event_interrupt(struct pt_regs * regs)2284 static void __perf_event_interrupt(struct pt_regs *regs)
2285 {
2286 int i, j;
2287 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
2288 struct perf_event *event;
2289 unsigned long val[8];
2290 int found, active;
2291
2292 if (cpuhw->n_limited)
2293 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
2294 mfspr(SPRN_PMC6));
2295
2296 perf_read_regs(regs);
2297
2298 /* Read all the PMCs since we'll need them a bunch of times */
2299 for (i = 0; i < ppmu->n_counter; ++i)
2300 val[i] = read_pmc(i + 1);
2301
2302 /* Try to find what caused the IRQ */
2303 found = 0;
2304 for (i = 0; i < ppmu->n_counter; ++i) {
2305 if (!pmc_overflow(val[i]))
2306 continue;
2307 if (is_limited_pmc(i + 1))
2308 continue; /* these won't generate IRQs */
2309 /*
2310 * We've found one that's overflowed. For active
2311 * counters we need to log this. For inactive
2312 * counters, we need to reset it anyway
2313 */
2314 found = 1;
2315 active = 0;
2316 for (j = 0; j < cpuhw->n_events; ++j) {
2317 event = cpuhw->event[j];
2318 if (event->hw.idx == (i + 1)) {
2319 active = 1;
2320 record_and_restart(event, val[i], regs);
2321 break;
2322 }
2323 }
2324
2325 /*
2326 * Clear PACA_IRQ_PMI in case it was set by
2327 * set_pmi_irq_pending() when PMU was enabled
2328 * after accounting for interrupts.
2329 */
2330 clear_pmi_irq_pending();
2331
2332 if (!active)
2333 /* reset non active counters that have overflowed */
2334 write_pmc(i + 1, 0);
2335 }
2336 if (!found && pvr_version_is(PVR_POWER7)) {
2337 /* check active counters for special buggy p7 overflow */
2338 for (i = 0; i < cpuhw->n_events; ++i) {
2339 event = cpuhw->event[i];
2340 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
2341 continue;
2342 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2343 /* event has overflowed in a buggy way*/
2344 found = 1;
2345 record_and_restart(event,
2346 val[event->hw.idx - 1],
2347 regs);
2348 }
2349 }
2350 }
2351
2352 /*
2353 * During system wide profling or while specific CPU is monitored for an
2354 * event, some corner cases could cause PMC to overflow in idle path. This
2355 * will trigger a PMI after waking up from idle. Since counter values are _not_
2356 * saved/restored in idle path, can lead to below "Can't find PMC" message.
2357 */
2358 if (unlikely(!found) && !arch_irq_disabled_regs(regs))
2359 printk_ratelimited(KERN_WARNING "Can't find PMC that caused IRQ\n");
2360
2361 /*
2362 * Reset MMCR0 to its normal value. This will set PMXE and
2363 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
2364 * and thus allow interrupts to occur again.
2365 * XXX might want to use MSR.PM to keep the events frozen until
2366 * we get back out of this interrupt.
2367 */
2368 write_mmcr0(cpuhw, cpuhw->mmcr.mmcr0);
2369 }
2370
perf_event_interrupt(struct pt_regs * regs)2371 static void perf_event_interrupt(struct pt_regs *regs)
2372 {
2373 u64 start_clock = sched_clock();
2374
2375 __perf_event_interrupt(regs);
2376 perf_sample_event_took(sched_clock() - start_clock);
2377 }
2378
power_pmu_prepare_cpu(unsigned int cpu)2379 static int power_pmu_prepare_cpu(unsigned int cpu)
2380 {
2381 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2382
2383 if (ppmu) {
2384 memset(cpuhw, 0, sizeof(*cpuhw));
2385 cpuhw->mmcr.mmcr0 = MMCR0_FC;
2386 }
2387 return 0;
2388 }
2389
register_power_pmu(struct power_pmu * pmu)2390 int register_power_pmu(struct power_pmu *pmu)
2391 {
2392 if (ppmu)
2393 return -EBUSY; /* something's already registered */
2394
2395 ppmu = pmu;
2396 pr_info("%s performance monitor hardware support registered\n",
2397 pmu->name);
2398
2399 power_pmu.attr_groups = ppmu->attr_groups;
2400 power_pmu.capabilities |= (ppmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS);
2401
2402 #ifdef MSR_HV
2403 /*
2404 * Use FCHV to ignore kernel events if MSR.HV is set.
2405 */
2406 if (mfmsr() & MSR_HV)
2407 freeze_events_kernel = MMCR0_FCHV;
2408 #endif /* CONFIG_PPC64 */
2409
2410 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
2411 cpuhp_setup_state(CPUHP_PERF_POWER, "perf/powerpc:prepare",
2412 power_pmu_prepare_cpu, NULL);
2413 return 0;
2414 }
2415
2416 #ifdef CONFIG_PPC64
init_ppc64_pmu(void)2417 static int __init init_ppc64_pmu(void)
2418 {
2419 /* run through all the pmu drivers one at a time */
2420 if (!init_power5_pmu())
2421 return 0;
2422 else if (!init_power5p_pmu())
2423 return 0;
2424 else if (!init_power6_pmu())
2425 return 0;
2426 else if (!init_power7_pmu())
2427 return 0;
2428 else if (!init_power8_pmu())
2429 return 0;
2430 else if (!init_power9_pmu())
2431 return 0;
2432 else if (!init_power10_pmu())
2433 return 0;
2434 else if (!init_ppc970_pmu())
2435 return 0;
2436 else
2437 return init_generic_compat_pmu();
2438 }
2439 early_initcall(init_ppc64_pmu);
2440 #endif
2441