1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Performance event support - powerpc architecture code
5 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
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>
16 #include <asm/machdep.h>
17 #include <asm/firmware.h>
18 #include <asm/ptrace.h>
19 #include <asm/code-patching.h>
20 #include <asm/interrupt.h>
26 #define BHRB_MAX_ENTRIES 32
27 #define BHRB_TARGET 0x0000000000000002
28 #define BHRB_PREDICTION 0x0000000000000001
29 #define BHRB_EA 0xFFFFFFFFFFFFFFFCUL
31 struct cpu_hw_events {
38 struct perf_event *event[MAX_HWEVENTS];
39 u64 events[MAX_HWEVENTS];
40 unsigned int flags[MAX_HWEVENTS];
41 struct mmcr_regs mmcr;
42 struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
43 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
44 u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
45 unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
46 unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
48 unsigned int txn_flags;
52 u64 bhrb_filter; /* BHRB HW branch filter */
53 unsigned int bhrb_users;
55 struct perf_branch_stack bhrb_stack;
56 struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
59 /* Store the PMC values */
60 unsigned long pmcs[MAX_HWEVENTS];
63 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
65 static struct power_pmu *ppmu;
68 * Normally, to ignore kernel events we set the FCS (freeze counters
69 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
70 * hypervisor bit set in the MSR, or if we are running on a processor
71 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
72 * then we need to use the FCHV bit to ignore kernel events.
74 static unsigned int freeze_events_kernel = MMCR0_FCS;
77 * 32-bit doesn't have MMCRA but does have an MMCR2,
78 * and a few other names are different.
79 * Also 32-bit doesn't have MMCR3, SIER2 and SIER3.
80 * Define them as zero knowing that any code path accessing
81 * these registers (via mtspr/mfspr) are done under ppmu flag
82 * check for PPMU_ARCH_31 and we will not enter that code path
88 #define MMCR0_PMCjCE MMCR0_PMCnCE
94 #define MMCR0_PMCC_U6 0
96 #define SPRN_MMCRA SPRN_MMCR2
100 #define MMCRA_SAMPLE_ENABLE 0
101 #define MMCRA_BHRB_DISABLE 0
102 #define MMCR0_PMCCEXT 0
104 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
108 static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp) { }
109 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
113 static inline void perf_read_regs(struct pt_regs *regs)
118 static inline int siar_valid(struct pt_regs *regs)
123 static bool is_ebb_event(struct perf_event *event) { return false; }
124 static int ebb_event_check(struct perf_event *event) { return 0; }
125 static void ebb_event_add(struct perf_event *event) { }
126 static void ebb_switch_out(unsigned long mmcr0) { }
127 static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
129 return cpuhw->mmcr.mmcr0;
132 static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
133 static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
134 static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
135 static inline void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw) {}
136 static void pmao_restore_workaround(bool ebb) { }
137 #endif /* CONFIG_PPC32 */
139 bool is_sier_available(void)
144 if (ppmu->flags & PPMU_HAS_SIER)
151 * Return PMC value corresponding to the
154 unsigned long get_pmcs_ext_regs(int idx)
156 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
158 return cpuhw->pmcs[idx];
161 static bool regs_use_siar(struct pt_regs *regs)
164 * When we take a performance monitor exception the regs are setup
165 * using perf_read_regs() which overloads some fields, in particular
166 * regs->result to tell us whether to use SIAR.
168 * However if the regs are from another exception, eg. a syscall, then
169 * they have not been setup using perf_read_regs() and so regs->result
170 * is something random.
172 return ((TRAP(regs) == INTERRUPT_PERFMON) && regs->result);
176 * Things that are specific to 64-bit implementations.
180 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
182 unsigned long mmcra = regs->dsisr;
184 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
185 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
187 return 4 * (slot - 1);
194 * The user wants a data address recorded.
195 * If we're not doing instruction sampling, give them the SDAR
196 * (sampled data address). If we are doing instruction sampling, then
197 * only give them the SDAR if it corresponds to the instruction
198 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
199 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
201 static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp)
203 unsigned long mmcra = regs->dsisr;
206 if (ppmu->flags & PPMU_HAS_SIER)
207 sdar_valid = regs->dar & SIER_SDAR_VALID;
209 unsigned long sdsync;
211 if (ppmu->flags & PPMU_SIAR_VALID)
212 sdsync = POWER7P_MMCRA_SDAR_VALID;
213 else if (ppmu->flags & PPMU_ALT_SIPR)
214 sdsync = POWER6_MMCRA_SDSYNC;
215 else if (ppmu->flags & PPMU_NO_SIAR)
216 sdsync = MMCRA_SAMPLE_ENABLE;
218 sdsync = MMCRA_SDSYNC;
220 sdar_valid = mmcra & sdsync;
223 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
224 *addrp = mfspr(SPRN_SDAR);
226 if (is_kernel_addr(mfspr(SPRN_SDAR)) && event->attr.exclude_kernel)
230 static bool regs_sihv(struct pt_regs *regs)
232 unsigned long sihv = MMCRA_SIHV;
234 if (ppmu->flags & PPMU_HAS_SIER)
235 return !!(regs->dar & SIER_SIHV);
237 if (ppmu->flags & PPMU_ALT_SIPR)
238 sihv = POWER6_MMCRA_SIHV;
240 return !!(regs->dsisr & sihv);
243 static bool regs_sipr(struct pt_regs *regs)
245 unsigned long sipr = MMCRA_SIPR;
247 if (ppmu->flags & PPMU_HAS_SIER)
248 return !!(regs->dar & SIER_SIPR);
250 if (ppmu->flags & PPMU_ALT_SIPR)
251 sipr = POWER6_MMCRA_SIPR;
253 return !!(regs->dsisr & sipr);
256 static inline u32 perf_flags_from_msr(struct pt_regs *regs)
258 if (regs->msr & MSR_PR)
259 return PERF_RECORD_MISC_USER;
260 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
261 return PERF_RECORD_MISC_HYPERVISOR;
262 return PERF_RECORD_MISC_KERNEL;
265 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
267 bool use_siar = regs_use_siar(regs);
268 unsigned long mmcra = regs->dsisr;
269 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
272 return perf_flags_from_msr(regs);
275 * Check the address in SIAR to identify the
276 * privilege levels since the SIER[MSR_HV, MSR_PR]
277 * bits are not set for marked events in power10
280 if (marked && (ppmu->flags & PPMU_P10_DD1)) {
281 unsigned long siar = mfspr(SPRN_SIAR);
283 if (is_kernel_addr(siar))
284 return PERF_RECORD_MISC_KERNEL;
285 return PERF_RECORD_MISC_USER;
287 if (is_kernel_addr(regs->nip))
288 return PERF_RECORD_MISC_KERNEL;
289 return PERF_RECORD_MISC_USER;
294 * If we don't have flags in MMCRA, rather than using
295 * the MSR, we intuit the flags from the address in
296 * SIAR which should give slightly more reliable
299 if (ppmu->flags & PPMU_NO_SIPR) {
300 unsigned long siar = mfspr(SPRN_SIAR);
301 if (is_kernel_addr(siar))
302 return PERF_RECORD_MISC_KERNEL;
303 return PERF_RECORD_MISC_USER;
306 /* PR has priority over HV, so order below is important */
308 return PERF_RECORD_MISC_USER;
310 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
311 return PERF_RECORD_MISC_HYPERVISOR;
313 return PERF_RECORD_MISC_KERNEL;
317 * Overload regs->dsisr to store MMCRA so we only need to read it once
319 * Overload regs->dar to store SIER if we have it.
320 * Overload regs->result to specify whether we should use the MSR (result
321 * is zero) or the SIAR (result is non zero).
323 static inline void perf_read_regs(struct pt_regs *regs)
325 unsigned long mmcra = mfspr(SPRN_MMCRA);
326 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
331 if (ppmu->flags & PPMU_HAS_SIER)
332 regs->dar = mfspr(SPRN_SIER);
335 * If this isn't a PMU exception (eg a software event) the SIAR is
336 * not valid. Use pt_regs.
338 * If it is a marked event use the SIAR.
340 * If the PMU doesn't update the SIAR for non marked events use
343 * If the PMU has HV/PR flags then check to see if they
344 * place the exception in userspace. If so, use pt_regs. In
345 * continuous sampling mode the SIAR and the PMU exception are
346 * not synchronised, so they may be many instructions apart.
347 * This can result in confusing backtraces. We still want
348 * hypervisor samples as well as samples in the kernel with
349 * interrupts off hence the userspace check.
351 if (TRAP(regs) != INTERRUPT_PERFMON)
353 else if ((ppmu->flags & PPMU_NO_SIAR))
357 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
359 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
364 regs->result = use_siar;
368 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
369 * must be sampled only if the SIAR-valid bit is set.
371 * For unmarked instructions and for processors that don't have the SIAR-Valid
372 * bit, assume that SIAR is valid.
374 static inline int siar_valid(struct pt_regs *regs)
376 unsigned long mmcra = regs->dsisr;
377 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
381 * SIER[SIAR_VALID] is not set for some
382 * marked events on power10 DD1, so drop
383 * the check for SIER[SIAR_VALID] and return true.
385 if (ppmu->flags & PPMU_P10_DD1)
387 else if (ppmu->flags & PPMU_HAS_SIER)
388 return regs->dar & SIER_SIAR_VALID;
390 if (ppmu->flags & PPMU_SIAR_VALID)
391 return mmcra & POWER7P_MMCRA_SIAR_VALID;
398 /* Reset all possible BHRB entries */
399 static void power_pmu_bhrb_reset(void)
401 asm volatile(PPC_CLRBHRB);
404 static void power_pmu_bhrb_enable(struct perf_event *event)
406 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
411 /* Clear BHRB if we changed task context to avoid data leaks */
412 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
413 power_pmu_bhrb_reset();
414 cpuhw->bhrb_context = event->ctx;
417 perf_sched_cb_inc(event->ctx->pmu);
420 static void power_pmu_bhrb_disable(struct perf_event *event)
422 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
427 WARN_ON_ONCE(!cpuhw->bhrb_users);
429 perf_sched_cb_dec(event->ctx->pmu);
431 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
432 /* BHRB cannot be turned off when other
433 * events are active on the PMU.
436 /* avoid stale pointer */
437 cpuhw->bhrb_context = NULL;
441 /* Called from ctxsw to prevent one process's branch entries to
442 * mingle with the other process's entries during context switch.
444 static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
450 power_pmu_bhrb_reset();
452 /* Calculate the to address for a branch */
453 static __u64 power_pmu_bhrb_to(u64 addr)
458 if (is_kernel_addr(addr)) {
459 if (copy_from_kernel_nofault(&instr, (void *)addr,
463 return branch_target((struct ppc_inst *)&instr);
466 /* Userspace: need copy instruction here then translate it */
467 if (copy_from_user_nofault(&instr, (unsigned int __user *)addr,
471 target = branch_target((struct ppc_inst *)&instr);
472 if ((!target) || (instr & BRANCH_ABSOLUTE))
475 /* Translate relative branch target from kernel to user address */
476 return target - (unsigned long)&instr + addr;
479 /* Processing BHRB entries */
480 static void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw)
484 int r_index, u_index, pred;
488 while (r_index < ppmu->bhrb_nr) {
489 /* Assembly read function */
490 val = read_bhrb(r_index++);
492 /* Terminal marker: End of valid BHRB entries */
495 addr = val & BHRB_EA;
496 pred = val & BHRB_PREDICTION;
503 * BHRB rolling buffer could very much contain the kernel
504 * addresses at this point. Check the privileges before
505 * exporting it to userspace (avoid exposure of regions
506 * where we could have speculative execution)
507 * Incase of ISA v3.1, BHRB will capture only user-space
508 * addresses, hence include a check before filtering code
510 if (!(ppmu->flags & PPMU_ARCH_31) &&
511 is_kernel_addr(addr) && event->attr.exclude_kernel)
514 /* Branches are read most recent first (ie. mfbhrb 0 is
515 * the most recent branch).
516 * There are two types of valid entries:
517 * 1) a target entry which is the to address of a
518 * computed goto like a blr,bctr,btar. The next
519 * entry read from the bhrb will be branch
520 * corresponding to this target (ie. the actual
521 * blr/bctr/btar instruction).
522 * 2) a from address which is an actual branch. If a
523 * target entry proceeds this, then this is the
524 * matching branch for that target. If this is not
525 * following a target entry, then this is a branch
526 * where the target is given as an immediate field
527 * in the instruction (ie. an i or b form branch).
528 * In this case we need to read the instruction from
529 * memory to determine the target/to address.
532 if (val & BHRB_TARGET) {
533 /* Target branches use two entries
534 * (ie. computed gotos/XL form)
536 cpuhw->bhrb_entries[u_index].to = addr;
537 cpuhw->bhrb_entries[u_index].mispred = pred;
538 cpuhw->bhrb_entries[u_index].predicted = ~pred;
540 /* Get from address in next entry */
541 val = read_bhrb(r_index++);
542 addr = val & BHRB_EA;
543 if (val & BHRB_TARGET) {
544 /* Shouldn't have two targets in a
545 row.. Reset index and try again */
549 cpuhw->bhrb_entries[u_index].from = addr;
551 /* Branches to immediate field
553 cpuhw->bhrb_entries[u_index].from = addr;
554 cpuhw->bhrb_entries[u_index].to =
555 power_pmu_bhrb_to(addr);
556 cpuhw->bhrb_entries[u_index].mispred = pred;
557 cpuhw->bhrb_entries[u_index].predicted = ~pred;
563 cpuhw->bhrb_stack.nr = u_index;
564 cpuhw->bhrb_stack.hw_idx = -1ULL;
568 static bool is_ebb_event(struct perf_event *event)
571 * This could be a per-PMU callback, but we'd rather avoid the cost. We
572 * check that the PMU supports EBB, meaning those that don't can still
573 * use bit 63 of the event code for something else if they wish.
575 return (ppmu->flags & PPMU_ARCH_207S) &&
576 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
579 static int ebb_event_check(struct perf_event *event)
581 struct perf_event *leader = event->group_leader;
583 /* Event and group leader must agree on EBB */
584 if (is_ebb_event(leader) != is_ebb_event(event))
587 if (is_ebb_event(event)) {
588 if (!(event->attach_state & PERF_ATTACH_TASK))
591 if (!leader->attr.pinned || !leader->attr.exclusive)
594 if (event->attr.freq ||
595 event->attr.inherit ||
596 event->attr.sample_type ||
597 event->attr.sample_period ||
598 event->attr.enable_on_exec)
605 static void ebb_event_add(struct perf_event *event)
607 if (!is_ebb_event(event) || current->thread.used_ebb)
611 * IFF this is the first time we've added an EBB event, set
612 * PMXE in the user MMCR0 so we can detect when it's cleared by
613 * userspace. We need this so that we can context switch while
614 * userspace is in the EBB handler (where PMXE is 0).
616 current->thread.used_ebb = 1;
617 current->thread.mmcr0 |= MMCR0_PMXE;
620 static void ebb_switch_out(unsigned long mmcr0)
622 if (!(mmcr0 & MMCR0_EBE))
625 current->thread.siar = mfspr(SPRN_SIAR);
626 current->thread.sier = mfspr(SPRN_SIER);
627 current->thread.sdar = mfspr(SPRN_SDAR);
628 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
629 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
630 if (ppmu->flags & PPMU_ARCH_31) {
631 current->thread.mmcr3 = mfspr(SPRN_MMCR3);
632 current->thread.sier2 = mfspr(SPRN_SIER2);
633 current->thread.sier3 = mfspr(SPRN_SIER3);
637 static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
639 unsigned long mmcr0 = cpuhw->mmcr.mmcr0;
644 /* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
645 mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
648 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
649 * with pmao_restore_workaround() because we may add PMAO but we never
652 mmcr0 |= current->thread.mmcr0;
655 * Be careful not to set PMXE if userspace had it cleared. This is also
656 * compatible with pmao_restore_workaround() because it has already
657 * cleared PMXE and we leave PMAO alone.
659 if (!(current->thread.mmcr0 & MMCR0_PMXE))
660 mmcr0 &= ~MMCR0_PMXE;
662 mtspr(SPRN_SIAR, current->thread.siar);
663 mtspr(SPRN_SIER, current->thread.sier);
664 mtspr(SPRN_SDAR, current->thread.sdar);
667 * Merge the kernel & user values of MMCR2. The semantics we implement
668 * are that the user MMCR2 can set bits, ie. cause counters to freeze,
669 * but not clear bits. If a task wants to be able to clear bits, ie.
670 * unfreeze counters, it should not set exclude_xxx in its events and
671 * instead manage the MMCR2 entirely by itself.
673 mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2 | current->thread.mmcr2);
675 if (ppmu->flags & PPMU_ARCH_31) {
676 mtspr(SPRN_MMCR3, current->thread.mmcr3);
677 mtspr(SPRN_SIER2, current->thread.sier2);
678 mtspr(SPRN_SIER3, current->thread.sier3);
684 static void pmao_restore_workaround(bool ebb)
688 if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
692 * On POWER8E there is a hardware defect which affects the PMU context
693 * switch logic, ie. power_pmu_disable/enable().
695 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
696 * by the hardware. Sometime later the actual PMU exception is
699 * If we context switch, or simply disable/enable, the PMU prior to the
700 * exception arriving, the exception will be lost when we clear PMAO.
702 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
703 * set, and this _should_ generate an exception. However because of the
704 * defect no exception is generated when we write PMAO, and we get
705 * stuck with no counters counting but no exception delivered.
707 * The workaround is to detect this case and tweak the hardware to
708 * create another pending PMU exception.
710 * We do that by setting up PMC6 (cycles) for an imminent overflow and
711 * enabling the PMU. That causes a new exception to be generated in the
712 * chip, but we don't take it yet because we have interrupts hard
713 * disabled. We then write back the PMU state as we want it to be seen
714 * by the exception handler. When we reenable interrupts the exception
715 * handler will be called and see the correct state.
717 * The logic is the same for EBB, except that the exception is gated by
718 * us having interrupts hard disabled as well as the fact that we are
719 * not in userspace. The exception is finally delivered when we return
723 /* Only if PMAO is set and PMAO_SYNC is clear */
724 if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
727 /* If we're doing EBB, only if BESCR[GE] is set */
728 if (ebb && !(current->thread.bescr & BESCR_GE))
732 * We are already soft-disabled in power_pmu_enable(). We need to hard
733 * disable to actually prevent the PMU exception from firing.
738 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
739 * Using read/write_pmc() in a for loop adds 12 function calls and
740 * almost doubles our code size.
742 pmcs[0] = mfspr(SPRN_PMC1);
743 pmcs[1] = mfspr(SPRN_PMC2);
744 pmcs[2] = mfspr(SPRN_PMC3);
745 pmcs[3] = mfspr(SPRN_PMC4);
746 pmcs[4] = mfspr(SPRN_PMC5);
747 pmcs[5] = mfspr(SPRN_PMC6);
749 /* Ensure all freeze bits are unset */
750 mtspr(SPRN_MMCR2, 0);
752 /* Set up PMC6 to overflow in one cycle */
753 mtspr(SPRN_PMC6, 0x7FFFFFFE);
755 /* Enable exceptions and unfreeze PMC6 */
756 mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
758 /* Now we need to refreeze and restore the PMCs */
759 mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
761 mtspr(SPRN_PMC1, pmcs[0]);
762 mtspr(SPRN_PMC2, pmcs[1]);
763 mtspr(SPRN_PMC3, pmcs[2]);
764 mtspr(SPRN_PMC4, pmcs[3]);
765 mtspr(SPRN_PMC5, pmcs[4]);
766 mtspr(SPRN_PMC6, pmcs[5]);
769 #endif /* CONFIG_PPC64 */
771 static void perf_event_interrupt(struct pt_regs *regs);
774 * Read one performance monitor counter (PMC).
776 static unsigned long read_pmc(int idx)
782 val = mfspr(SPRN_PMC1);
785 val = mfspr(SPRN_PMC2);
788 val = mfspr(SPRN_PMC3);
791 val = mfspr(SPRN_PMC4);
794 val = mfspr(SPRN_PMC5);
797 val = mfspr(SPRN_PMC6);
801 val = mfspr(SPRN_PMC7);
804 val = mfspr(SPRN_PMC8);
806 #endif /* CONFIG_PPC64 */
808 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
817 static void write_pmc(int idx, unsigned long val)
821 mtspr(SPRN_PMC1, val);
824 mtspr(SPRN_PMC2, val);
827 mtspr(SPRN_PMC3, val);
830 mtspr(SPRN_PMC4, val);
833 mtspr(SPRN_PMC5, val);
836 mtspr(SPRN_PMC6, val);
840 mtspr(SPRN_PMC7, val);
843 mtspr(SPRN_PMC8, val);
845 #endif /* CONFIG_PPC64 */
847 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
851 /* Called from sysrq_handle_showregs() */
852 void perf_event_print_debug(void)
854 unsigned long sdar, sier, flags;
855 u32 pmcs[MAX_HWEVENTS];
859 pr_info("Performance monitor hardware not registered.\n");
863 if (!ppmu->n_counter)
866 local_irq_save(flags);
868 pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
869 smp_processor_id(), ppmu->name, ppmu->n_counter);
871 for (i = 0; i < ppmu->n_counter; i++)
872 pmcs[i] = read_pmc(i + 1);
874 for (; i < MAX_HWEVENTS; i++)
875 pmcs[i] = 0xdeadbeef;
877 pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
878 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
880 if (ppmu->n_counter > 4)
881 pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
882 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
884 pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
885 mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
889 sdar = mfspr(SPRN_SDAR);
891 if (ppmu->flags & PPMU_HAS_SIER)
892 sier = mfspr(SPRN_SIER);
894 if (ppmu->flags & PPMU_ARCH_207S) {
895 pr_info("MMCR2: %016lx EBBHR: %016lx\n",
896 mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
897 pr_info("EBBRR: %016lx BESCR: %016lx\n",
898 mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
901 if (ppmu->flags & PPMU_ARCH_31) {
902 pr_info("MMCR3: %016lx SIER2: %016lx SIER3: %016lx\n",
903 mfspr(SPRN_MMCR3), mfspr(SPRN_SIER2), mfspr(SPRN_SIER3));
906 pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
907 mfspr(SPRN_SIAR), sdar, sier);
909 local_irq_restore(flags);
913 * Check if a set of events can all go on the PMU at once.
914 * If they can't, this will look at alternative codes for the events
915 * and see if any combination of alternative codes is feasible.
916 * The feasible set is returned in event_id[].
918 static int power_check_constraints(struct cpu_hw_events *cpuhw,
919 u64 event_id[], unsigned int cflags[],
920 int n_ev, struct perf_event **event)
922 unsigned long mask, value, nv;
923 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
924 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
926 unsigned long addf = ppmu->add_fields;
927 unsigned long tadd = ppmu->test_adder;
928 unsigned long grp_mask = ppmu->group_constraint_mask;
929 unsigned long grp_val = ppmu->group_constraint_val;
931 if (n_ev > ppmu->n_counter)
934 /* First see if the events will go on as-is */
935 for (i = 0; i < n_ev; ++i) {
936 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
937 && !ppmu->limited_pmc_event(event_id[i])) {
938 ppmu->get_alternatives(event_id[i], cflags[i],
939 cpuhw->alternatives[i]);
940 event_id[i] = cpuhw->alternatives[i][0];
942 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
943 &cpuhw->avalues[i][0], event[i]->attr.config1))
947 for (i = 0; i < n_ev; ++i) {
948 nv = (value | cpuhw->avalues[i][0]) +
949 (value & cpuhw->avalues[i][0] & addf);
951 if (((((nv + tadd) ^ value) & mask) & (~grp_mask)) != 0)
954 if (((((nv + tadd) ^ cpuhw->avalues[i][0]) & cpuhw->amasks[i][0])
959 mask |= cpuhw->amasks[i][0];
962 if ((value & mask & grp_mask) != (mask & grp_val))
965 return 0; /* all OK */
968 /* doesn't work, gather alternatives... */
969 if (!ppmu->get_alternatives)
971 for (i = 0; i < n_ev; ++i) {
973 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
974 cpuhw->alternatives[i]);
975 for (j = 1; j < n_alt[i]; ++j)
976 ppmu->get_constraint(cpuhw->alternatives[i][j],
977 &cpuhw->amasks[i][j],
978 &cpuhw->avalues[i][j],
979 event[i]->attr.config1);
982 /* enumerate all possibilities and see if any will work */
985 value = mask = nv = 0;
988 /* we're backtracking, restore context */
994 * See if any alternative k for event_id i,
995 * where k > j, will satisfy the constraints.
997 while (++j < n_alt[i]) {
998 nv = (value | cpuhw->avalues[i][j]) +
999 (value & cpuhw->avalues[i][j] & addf);
1000 if ((((nv + tadd) ^ value) & mask) == 0 &&
1001 (((nv + tadd) ^ cpuhw->avalues[i][j])
1002 & cpuhw->amasks[i][j]) == 0)
1005 if (j >= n_alt[i]) {
1007 * No feasible alternative, backtrack
1008 * to event_id i-1 and continue enumerating its
1009 * alternatives from where we got up to.
1015 * Found a feasible alternative for event_id i,
1016 * remember where we got up to with this event_id,
1017 * go on to the next event_id, and start with
1018 * the first alternative for it.
1024 mask |= cpuhw->amasks[i][j];
1030 /* OK, we have a feasible combination, tell the caller the solution */
1031 for (i = 0; i < n_ev; ++i)
1032 event_id[i] = cpuhw->alternatives[i][choice[i]];
1037 * Check if newly-added events have consistent settings for
1038 * exclude_{user,kernel,hv} with each other and any previously
1041 static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
1042 int n_prev, int n_new)
1044 int eu = 0, ek = 0, eh = 0;
1046 struct perf_event *event;
1049 * If the PMU we're on supports per event exclude settings then we
1050 * don't need to do any of this logic. NB. This assumes no PMU has both
1051 * per event exclude and limited PMCs.
1053 if (ppmu->flags & PPMU_ARCH_207S)
1061 for (i = 0; i < n; ++i) {
1062 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
1063 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
1068 eu = event->attr.exclude_user;
1069 ek = event->attr.exclude_kernel;
1070 eh = event->attr.exclude_hv;
1072 } else if (event->attr.exclude_user != eu ||
1073 event->attr.exclude_kernel != ek ||
1074 event->attr.exclude_hv != eh) {
1080 for (i = 0; i < n; ++i)
1081 if (cflags[i] & PPMU_LIMITED_PMC_OK)
1082 cflags[i] |= PPMU_LIMITED_PMC_REQD;
1087 static u64 check_and_compute_delta(u64 prev, u64 val)
1089 u64 delta = (val - prev) & 0xfffffffful;
1092 * POWER7 can roll back counter values, if the new value is smaller
1093 * than the previous value it will cause the delta and the counter to
1094 * have bogus values unless we rolled a counter over. If a coutner is
1095 * rolled back, it will be smaller, but within 256, which is the maximum
1096 * number of events to rollback at once. If we detect a rollback
1097 * return 0. This can lead to a small lack of precision in the
1100 if (prev > val && (prev - val) < 256)
1106 static void power_pmu_read(struct perf_event *event)
1108 s64 val, delta, prev;
1110 if (event->hw.state & PERF_HES_STOPPED)
1116 if (is_ebb_event(event)) {
1117 val = read_pmc(event->hw.idx);
1118 local64_set(&event->hw.prev_count, val);
1123 * Performance monitor interrupts come even when interrupts
1124 * are soft-disabled, as long as interrupts are hard-enabled.
1125 * Therefore we treat them like NMIs.
1128 prev = local64_read(&event->hw.prev_count);
1130 val = read_pmc(event->hw.idx);
1131 delta = check_and_compute_delta(prev, val);
1134 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
1136 local64_add(delta, &event->count);
1139 * A number of places program the PMC with (0x80000000 - period_left).
1140 * We never want period_left to be less than 1 because we will program
1141 * the PMC with a value >= 0x800000000 and an edge detected PMC will
1142 * roll around to 0 before taking an exception. We have seen this
1145 * To fix this, clamp the minimum value of period_left to 1.
1148 prev = local64_read(&event->hw.period_left);
1152 } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
1156 * On some machines, PMC5 and PMC6 can't be written, don't respect
1157 * the freeze conditions, and don't generate interrupts. This tells
1158 * us if `event' is using such a PMC.
1160 static int is_limited_pmc(int pmcnum)
1162 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1163 && (pmcnum == 5 || pmcnum == 6);
1166 static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
1167 unsigned long pmc5, unsigned long pmc6)
1169 struct perf_event *event;
1170 u64 val, prev, delta;
1173 for (i = 0; i < cpuhw->n_limited; ++i) {
1174 event = cpuhw->limited_counter[i];
1177 val = (event->hw.idx == 5) ? pmc5 : pmc6;
1178 prev = local64_read(&event->hw.prev_count);
1180 delta = check_and_compute_delta(prev, val);
1182 local64_add(delta, &event->count);
1186 static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
1187 unsigned long pmc5, unsigned long pmc6)
1189 struct perf_event *event;
1193 for (i = 0; i < cpuhw->n_limited; ++i) {
1194 event = cpuhw->limited_counter[i];
1195 event->hw.idx = cpuhw->limited_hwidx[i];
1196 val = (event->hw.idx == 5) ? pmc5 : pmc6;
1197 prev = local64_read(&event->hw.prev_count);
1198 if (check_and_compute_delta(prev, val))
1199 local64_set(&event->hw.prev_count, val);
1200 perf_event_update_userpage(event);
1205 * Since limited events don't respect the freeze conditions, we
1206 * have to read them immediately after freezing or unfreezing the
1207 * other events. We try to keep the values from the limited
1208 * events as consistent as possible by keeping the delay (in
1209 * cycles and instructions) between freezing/unfreezing and reading
1210 * the limited events as small and consistent as possible.
1211 * Therefore, if any limited events are in use, we read them
1212 * both, and always in the same order, to minimize variability,
1213 * and do it inside the same asm that writes MMCR0.
1215 static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1217 unsigned long pmc5, pmc6;
1219 if (!cpuhw->n_limited) {
1220 mtspr(SPRN_MMCR0, mmcr0);
1225 * Write MMCR0, then read PMC5 and PMC6 immediately.
1226 * To ensure we don't get a performance monitor interrupt
1227 * between writing MMCR0 and freezing/thawing the limited
1228 * events, we first write MMCR0 with the event overflow
1229 * interrupt enable bits turned off.
1231 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1232 : "=&r" (pmc5), "=&r" (pmc6)
1233 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1235 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1237 if (mmcr0 & MMCR0_FC)
1238 freeze_limited_counters(cpuhw, pmc5, pmc6);
1240 thaw_limited_counters(cpuhw, pmc5, pmc6);
1243 * Write the full MMCR0 including the event overflow interrupt
1244 * enable bits, if necessary.
1246 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1247 mtspr(SPRN_MMCR0, mmcr0);
1251 * Disable all events to prevent PMU interrupts and to allow
1252 * events to be added or removed.
1254 static void power_pmu_disable(struct pmu *pmu)
1256 struct cpu_hw_events *cpuhw;
1257 unsigned long flags, mmcr0, val, mmcra;
1261 local_irq_save(flags);
1262 cpuhw = this_cpu_ptr(&cpu_hw_events);
1264 if (!cpuhw->disabled) {
1266 * Check if we ever enabled the PMU on this cpu.
1268 if (!cpuhw->pmcs_enabled) {
1270 cpuhw->pmcs_enabled = 1;
1274 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
1276 val = mmcr0 = mfspr(SPRN_MMCR0);
1278 val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
1280 /* Set mmcr0 PMCCEXT for p10 */
1281 if (ppmu->flags & PPMU_ARCH_31)
1282 val |= MMCR0_PMCCEXT;
1285 * The barrier is to make sure the mtspr has been
1286 * executed and the PMU has frozen the events etc.
1289 write_mmcr0(cpuhw, val);
1293 val = mmcra = cpuhw->mmcr.mmcra;
1296 * Disable instruction sampling if it was enabled
1298 if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE)
1299 val &= ~MMCRA_SAMPLE_ENABLE;
1301 /* Disable BHRB via mmcra (BHRBRD) for p10 */
1302 if (ppmu->flags & PPMU_ARCH_31)
1303 val |= MMCRA_BHRB_DISABLE;
1306 * Write SPRN_MMCRA if mmcra has either disabled
1307 * instruction sampling or BHRB.
1310 mtspr(SPRN_MMCRA, mmcra);
1315 cpuhw->disabled = 1;
1318 ebb_switch_out(mmcr0);
1322 * These are readable by userspace, may contain kernel
1323 * addresses and are not switched by context switch, so clear
1324 * them now to avoid leaking anything to userspace in general
1325 * including to another process.
1327 if (ppmu->flags & PPMU_ARCH_207S) {
1328 mtspr(SPRN_SDAR, 0);
1329 mtspr(SPRN_SIAR, 0);
1334 local_irq_restore(flags);
1338 * Re-enable all events if disable == 0.
1339 * If we were previously disabled and events were added, then
1340 * put the new config on the PMU.
1342 static void power_pmu_enable(struct pmu *pmu)
1344 struct perf_event *event;
1345 struct cpu_hw_events *cpuhw;
1346 unsigned long flags;
1348 unsigned long val, mmcr0;
1350 unsigned int hwc_index[MAX_HWEVENTS];
1357 local_irq_save(flags);
1359 cpuhw = this_cpu_ptr(&cpu_hw_events);
1360 if (!cpuhw->disabled)
1363 if (cpuhw->n_events == 0) {
1364 ppc_set_pmu_inuse(0);
1368 cpuhw->disabled = 0;
1371 * EBB requires an exclusive group and all events must have the EBB
1372 * flag set, or not set, so we can just check a single event. Also we
1373 * know we have at least one event.
1375 ebb = is_ebb_event(cpuhw->event[0]);
1378 * If we didn't change anything, or only removed events,
1379 * no need to recalculate MMCR* settings and reset the PMCs.
1380 * Just reenable the PMU with the current MMCR* settings
1381 * (possibly updated for removal of events).
1383 if (!cpuhw->n_added) {
1384 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE);
1385 mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1);
1386 if (ppmu->flags & PPMU_ARCH_31)
1387 mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3);
1392 * Clear all MMCR settings and recompute them for the new set of events.
1394 memset(&cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
1396 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
1397 &cpuhw->mmcr, cpuhw->event, ppmu->flags)) {
1398 /* shouldn't ever get here */
1399 printk(KERN_ERR "oops compute_mmcr failed\n");
1403 if (!(ppmu->flags & PPMU_ARCH_207S)) {
1405 * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
1406 * bits for the first event. We have already checked that all
1407 * events have the same value for these bits as the first event.
1409 event = cpuhw->event[0];
1410 if (event->attr.exclude_user)
1411 cpuhw->mmcr.mmcr0 |= MMCR0_FCP;
1412 if (event->attr.exclude_kernel)
1413 cpuhw->mmcr.mmcr0 |= freeze_events_kernel;
1414 if (event->attr.exclude_hv)
1415 cpuhw->mmcr.mmcr0 |= MMCR0_FCHV;
1419 * Write the new configuration to MMCR* with the freeze
1420 * bit set and set the hardware events to their initial values.
1421 * Then unfreeze the events.
1423 ppc_set_pmu_inuse(1);
1424 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE);
1425 mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1);
1426 mtspr(SPRN_MMCR0, (cpuhw->mmcr.mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1428 if (ppmu->flags & PPMU_ARCH_207S)
1429 mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2);
1431 if (ppmu->flags & PPMU_ARCH_31)
1432 mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3);
1435 * Read off any pre-existing events that need to move
1438 for (i = 0; i < cpuhw->n_events; ++i) {
1439 event = cpuhw->event[i];
1440 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1441 power_pmu_read(event);
1442 write_pmc(event->hw.idx, 0);
1448 * Initialize the PMCs for all the new and moved events.
1450 cpuhw->n_limited = n_lim = 0;
1451 for (i = 0; i < cpuhw->n_events; ++i) {
1452 event = cpuhw->event[i];
1455 idx = hwc_index[i] + 1;
1456 if (is_limited_pmc(idx)) {
1457 cpuhw->limited_counter[n_lim] = event;
1458 cpuhw->limited_hwidx[n_lim] = idx;
1464 val = local64_read(&event->hw.prev_count);
1467 if (event->hw.sample_period) {
1468 left = local64_read(&event->hw.period_left);
1469 if (left < 0x80000000L)
1470 val = 0x80000000L - left;
1472 local64_set(&event->hw.prev_count, val);
1475 event->hw.idx = idx;
1476 if (event->hw.state & PERF_HES_STOPPED)
1478 write_pmc(idx, val);
1480 perf_event_update_userpage(event);
1482 cpuhw->n_limited = n_lim;
1483 cpuhw->mmcr.mmcr0 |= MMCR0_PMXE | MMCR0_FCECE;
1486 pmao_restore_workaround(ebb);
1488 mmcr0 = ebb_switch_in(ebb, cpuhw);
1491 if (cpuhw->bhrb_users)
1492 ppmu->config_bhrb(cpuhw->bhrb_filter);
1494 write_mmcr0(cpuhw, mmcr0);
1497 * Enable instruction sampling if necessary
1499 if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE) {
1501 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra);
1506 local_irq_restore(flags);
1509 static int collect_events(struct perf_event *group, int max_count,
1510 struct perf_event *ctrs[], u64 *events,
1511 unsigned int *flags)
1514 struct perf_event *event;
1516 if (group->pmu->task_ctx_nr == perf_hw_context) {
1520 flags[n] = group->hw.event_base;
1521 events[n++] = group->hw.config;
1523 for_each_sibling_event(event, group) {
1524 if (event->pmu->task_ctx_nr == perf_hw_context &&
1525 event->state != PERF_EVENT_STATE_OFF) {
1529 flags[n] = event->hw.event_base;
1530 events[n++] = event->hw.config;
1537 * Add an event to the PMU.
1538 * If all events are not already frozen, then we disable and
1539 * re-enable the PMU in order to get hw_perf_enable to do the
1540 * actual work of reconfiguring the PMU.
1542 static int power_pmu_add(struct perf_event *event, int ef_flags)
1544 struct cpu_hw_events *cpuhw;
1545 unsigned long flags;
1549 local_irq_save(flags);
1550 perf_pmu_disable(event->pmu);
1553 * Add the event to the list (if there is room)
1554 * and check whether the total set is still feasible.
1556 cpuhw = this_cpu_ptr(&cpu_hw_events);
1557 n0 = cpuhw->n_events;
1558 if (n0 >= ppmu->n_counter)
1560 cpuhw->event[n0] = event;
1561 cpuhw->events[n0] = event->hw.config;
1562 cpuhw->flags[n0] = event->hw.event_base;
1565 * This event may have been disabled/stopped in record_and_restart()
1566 * because we exceeded the ->event_limit. If re-starting the event,
1567 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1568 * notification is re-enabled.
1570 if (!(ef_flags & PERF_EF_START))
1571 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1573 event->hw.state = 0;
1576 * If group events scheduling transaction was started,
1577 * skip the schedulability test here, it will be performed
1578 * at commit time(->commit_txn) as a whole
1580 if (cpuhw->txn_flags & PERF_PMU_TXN_ADD)
1583 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1585 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1, cpuhw->event))
1587 event->hw.config = cpuhw->events[n0];
1590 ebb_event_add(event);
1597 if (has_branch_stack(event)) {
1598 u64 bhrb_filter = -1;
1600 if (ppmu->bhrb_filter_map)
1601 bhrb_filter = ppmu->bhrb_filter_map(
1602 event->attr.branch_sample_type);
1604 if (bhrb_filter != -1) {
1605 cpuhw->bhrb_filter = bhrb_filter;
1606 power_pmu_bhrb_enable(event);
1610 perf_pmu_enable(event->pmu);
1611 local_irq_restore(flags);
1616 * Remove an event from the PMU.
1618 static void power_pmu_del(struct perf_event *event, int ef_flags)
1620 struct cpu_hw_events *cpuhw;
1622 unsigned long flags;
1624 local_irq_save(flags);
1625 perf_pmu_disable(event->pmu);
1627 power_pmu_read(event);
1629 cpuhw = this_cpu_ptr(&cpu_hw_events);
1630 for (i = 0; i < cpuhw->n_events; ++i) {
1631 if (event == cpuhw->event[i]) {
1632 while (++i < cpuhw->n_events) {
1633 cpuhw->event[i-1] = cpuhw->event[i];
1634 cpuhw->events[i-1] = cpuhw->events[i];
1635 cpuhw->flags[i-1] = cpuhw->flags[i];
1638 ppmu->disable_pmc(event->hw.idx - 1, &cpuhw->mmcr);
1639 if (event->hw.idx) {
1640 write_pmc(event->hw.idx, 0);
1643 perf_event_update_userpage(event);
1647 for (i = 0; i < cpuhw->n_limited; ++i)
1648 if (event == cpuhw->limited_counter[i])
1650 if (i < cpuhw->n_limited) {
1651 while (++i < cpuhw->n_limited) {
1652 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
1653 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1657 if (cpuhw->n_events == 0) {
1658 /* disable exceptions if no events are running */
1659 cpuhw->mmcr.mmcr0 &= ~(MMCR0_PMXE | MMCR0_FCECE);
1662 if (has_branch_stack(event))
1663 power_pmu_bhrb_disable(event);
1665 perf_pmu_enable(event->pmu);
1666 local_irq_restore(flags);
1670 * POWER-PMU does not support disabling individual counters, hence
1671 * program their cycle counter to their max value and ignore the interrupts.
1674 static void power_pmu_start(struct perf_event *event, int ef_flags)
1676 unsigned long flags;
1680 if (!event->hw.idx || !event->hw.sample_period)
1683 if (!(event->hw.state & PERF_HES_STOPPED))
1686 if (ef_flags & PERF_EF_RELOAD)
1687 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1689 local_irq_save(flags);
1690 perf_pmu_disable(event->pmu);
1692 event->hw.state = 0;
1693 left = local64_read(&event->hw.period_left);
1696 if (left < 0x80000000L)
1697 val = 0x80000000L - left;
1699 write_pmc(event->hw.idx, val);
1701 perf_event_update_userpage(event);
1702 perf_pmu_enable(event->pmu);
1703 local_irq_restore(flags);
1706 static void power_pmu_stop(struct perf_event *event, int ef_flags)
1708 unsigned long flags;
1710 if (!event->hw.idx || !event->hw.sample_period)
1713 if (event->hw.state & PERF_HES_STOPPED)
1716 local_irq_save(flags);
1717 perf_pmu_disable(event->pmu);
1719 power_pmu_read(event);
1720 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1721 write_pmc(event->hw.idx, 0);
1723 perf_event_update_userpage(event);
1724 perf_pmu_enable(event->pmu);
1725 local_irq_restore(flags);
1729 * Start group events scheduling transaction
1730 * Set the flag to make pmu::enable() not perform the
1731 * schedulability test, it will be performed at commit time
1733 * We only support PERF_PMU_TXN_ADD transactions. Save the
1734 * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
1737 static void power_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
1739 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1741 WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
1743 cpuhw->txn_flags = txn_flags;
1744 if (txn_flags & ~PERF_PMU_TXN_ADD)
1747 perf_pmu_disable(pmu);
1748 cpuhw->n_txn_start = cpuhw->n_events;
1752 * Stop group events scheduling transaction
1753 * Clear the flag and pmu::enable() will perform the
1754 * schedulability test.
1756 static void power_pmu_cancel_txn(struct pmu *pmu)
1758 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1759 unsigned int txn_flags;
1761 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1763 txn_flags = cpuhw->txn_flags;
1764 cpuhw->txn_flags = 0;
1765 if (txn_flags & ~PERF_PMU_TXN_ADD)
1768 perf_pmu_enable(pmu);
1772 * Commit group events scheduling transaction
1773 * Perform the group schedulability test as a whole
1774 * Return 0 if success
1776 static int power_pmu_commit_txn(struct pmu *pmu)
1778 struct cpu_hw_events *cpuhw;
1784 cpuhw = this_cpu_ptr(&cpu_hw_events);
1785 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1787 if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
1788 cpuhw->txn_flags = 0;
1792 n = cpuhw->n_events;
1793 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1795 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n, cpuhw->event);
1799 for (i = cpuhw->n_txn_start; i < n; ++i)
1800 cpuhw->event[i]->hw.config = cpuhw->events[i];
1802 cpuhw->txn_flags = 0;
1803 perf_pmu_enable(pmu);
1808 * Return 1 if we might be able to put event on a limited PMC,
1810 * An event can only go on a limited PMC if it counts something
1811 * that a limited PMC can count, doesn't require interrupts, and
1812 * doesn't exclude any processor mode.
1814 static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1818 u64 alt[MAX_EVENT_ALTERNATIVES];
1820 if (event->attr.exclude_user
1821 || event->attr.exclude_kernel
1822 || event->attr.exclude_hv
1823 || event->attr.sample_period)
1826 if (ppmu->limited_pmc_event(ev))
1830 * The requested event_id isn't on a limited PMC already;
1831 * see if any alternative code goes on a limited PMC.
1833 if (!ppmu->get_alternatives)
1836 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1837 n = ppmu->get_alternatives(ev, flags, alt);
1843 * Find an alternative event_id that goes on a normal PMC, if possible,
1844 * and return the event_id code, or 0 if there is no such alternative.
1845 * (Note: event_id code 0 is "don't count" on all machines.)
1847 static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1849 u64 alt[MAX_EVENT_ALTERNATIVES];
1852 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1853 n = ppmu->get_alternatives(ev, flags, alt);
1859 /* Number of perf_events counting hardware events */
1860 static atomic_t num_events;
1861 /* Used to avoid races in calling reserve/release_pmc_hardware */
1862 static DEFINE_MUTEX(pmc_reserve_mutex);
1865 * Release the PMU if this is the last perf_event.
1867 static void hw_perf_event_destroy(struct perf_event *event)
1869 if (!atomic_add_unless(&num_events, -1, 1)) {
1870 mutex_lock(&pmc_reserve_mutex);
1871 if (atomic_dec_return(&num_events) == 0)
1872 release_pmc_hardware();
1873 mutex_unlock(&pmc_reserve_mutex);
1878 * Translate a generic cache event_id config to a raw event_id code.
1880 static int hw_perf_cache_event(u64 config, u64 *eventp)
1882 unsigned long type, op, result;
1885 if (!ppmu->cache_events)
1889 type = config & 0xff;
1890 op = (config >> 8) & 0xff;
1891 result = (config >> 16) & 0xff;
1893 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1894 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1895 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1898 ev = (*ppmu->cache_events)[type][op][result];
1907 static bool is_event_blacklisted(u64 ev)
1911 for (i=0; i < ppmu->n_blacklist_ev; i++) {
1912 if (ppmu->blacklist_ev[i] == ev)
1919 static int power_pmu_event_init(struct perf_event *event)
1922 unsigned long flags, irq_flags;
1923 struct perf_event *ctrs[MAX_HWEVENTS];
1924 u64 events[MAX_HWEVENTS];
1925 unsigned int cflags[MAX_HWEVENTS];
1928 struct cpu_hw_events *cpuhw;
1933 if (has_branch_stack(event)) {
1934 /* PMU has BHRB enabled */
1935 if (!(ppmu->flags & PPMU_ARCH_207S))
1939 switch (event->attr.type) {
1940 case PERF_TYPE_HARDWARE:
1941 ev = event->attr.config;
1942 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1945 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1947 ev = ppmu->generic_events[ev];
1949 case PERF_TYPE_HW_CACHE:
1950 err = hw_perf_cache_event(event->attr.config, &ev);
1954 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1958 ev = event->attr.config;
1960 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1968 * PMU config registers have fields that are
1969 * reserved and some specific values for bit fields are reserved.
1970 * For ex., MMCRA[61:62] is Randome Sampling Mode (SM)
1971 * and value of 0b11 to this field is reserved.
1972 * Check for invalid values in attr.config.
1974 if (ppmu->check_attr_config &&
1975 ppmu->check_attr_config(event))
1978 event->hw.config_base = ev;
1982 * If we are not running on a hypervisor, force the
1983 * exclude_hv bit to 0 so that we don't care what
1984 * the user set it to.
1986 if (!firmware_has_feature(FW_FEATURE_LPAR))
1987 event->attr.exclude_hv = 0;
1990 * If this is a per-task event, then we can use
1991 * PM_RUN_* events interchangeably with their non RUN_*
1992 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1993 * XXX we should check if the task is an idle task.
1996 if (event->attach_state & PERF_ATTACH_TASK)
1997 flags |= PPMU_ONLY_COUNT_RUN;
2000 * If this machine has limited events, check whether this
2001 * event_id could go on a limited event.
2003 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
2004 if (can_go_on_limited_pmc(event, ev, flags)) {
2005 flags |= PPMU_LIMITED_PMC_OK;
2006 } else if (ppmu->limited_pmc_event(ev)) {
2008 * The requested event_id is on a limited PMC,
2009 * but we can't use a limited PMC; see if any
2010 * alternative goes on a normal PMC.
2012 ev = normal_pmc_alternative(ev, flags);
2018 /* Extra checks for EBB */
2019 err = ebb_event_check(event);
2024 * If this is in a group, check if it can go on with all the
2025 * other hardware events in the group. We assume the event
2026 * hasn't been linked into its leader's sibling list at this point.
2029 if (event->group_leader != event) {
2030 n = collect_events(event->group_leader, ppmu->n_counter - 1,
2031 ctrs, events, cflags);
2038 if (check_excludes(ctrs, cflags, n, 1))
2041 local_irq_save(irq_flags);
2042 cpuhw = this_cpu_ptr(&cpu_hw_events);
2044 err = power_check_constraints(cpuhw, events, cflags, n + 1, ctrs);
2046 if (has_branch_stack(event)) {
2047 u64 bhrb_filter = -1;
2049 if (ppmu->bhrb_filter_map)
2050 bhrb_filter = ppmu->bhrb_filter_map(
2051 event->attr.branch_sample_type);
2053 if (bhrb_filter == -1) {
2054 local_irq_restore(irq_flags);
2057 cpuhw->bhrb_filter = bhrb_filter;
2060 local_irq_restore(irq_flags);
2064 event->hw.config = events[n];
2065 event->hw.event_base = cflags[n];
2066 event->hw.last_period = event->hw.sample_period;
2067 local64_set(&event->hw.period_left, event->hw.last_period);
2070 * For EBB events we just context switch the PMC value, we don't do any
2071 * of the sample_period logic. We use hw.prev_count for this.
2073 if (is_ebb_event(event))
2074 local64_set(&event->hw.prev_count, 0);
2077 * See if we need to reserve the PMU.
2078 * If no events are currently in use, then we have to take a
2079 * mutex to ensure that we don't race with another task doing
2080 * reserve_pmc_hardware or release_pmc_hardware.
2083 if (!atomic_inc_not_zero(&num_events)) {
2084 mutex_lock(&pmc_reserve_mutex);
2085 if (atomic_read(&num_events) == 0 &&
2086 reserve_pmc_hardware(perf_event_interrupt))
2089 atomic_inc(&num_events);
2090 mutex_unlock(&pmc_reserve_mutex);
2092 event->destroy = hw_perf_event_destroy;
2097 static int power_pmu_event_idx(struct perf_event *event)
2099 return event->hw.idx;
2102 ssize_t power_events_sysfs_show(struct device *dev,
2103 struct device_attribute *attr, char *page)
2105 struct perf_pmu_events_attr *pmu_attr;
2107 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
2109 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
2112 static struct pmu power_pmu = {
2113 .pmu_enable = power_pmu_enable,
2114 .pmu_disable = power_pmu_disable,
2115 .event_init = power_pmu_event_init,
2116 .add = power_pmu_add,
2117 .del = power_pmu_del,
2118 .start = power_pmu_start,
2119 .stop = power_pmu_stop,
2120 .read = power_pmu_read,
2121 .start_txn = power_pmu_start_txn,
2122 .cancel_txn = power_pmu_cancel_txn,
2123 .commit_txn = power_pmu_commit_txn,
2124 .event_idx = power_pmu_event_idx,
2125 .sched_task = power_pmu_sched_task,
2128 #define PERF_SAMPLE_ADDR_TYPE (PERF_SAMPLE_ADDR | \
2129 PERF_SAMPLE_PHYS_ADDR | \
2130 PERF_SAMPLE_DATA_PAGE_SIZE)
2132 * A counter has overflowed; update its count and record
2133 * things if requested. Note that interrupts are hard-disabled
2134 * here so there is no possibility of being interrupted.
2136 static void record_and_restart(struct perf_event *event, unsigned long val,
2137 struct pt_regs *regs)
2139 u64 period = event->hw.sample_period;
2140 s64 prev, delta, left;
2143 if (event->hw.state & PERF_HES_STOPPED) {
2144 write_pmc(event->hw.idx, 0);
2148 /* we don't have to worry about interrupts here */
2149 prev = local64_read(&event->hw.prev_count);
2150 delta = check_and_compute_delta(prev, val);
2151 local64_add(delta, &event->count);
2154 * See if the total period for this event has expired,
2155 * and update for the next period.
2158 left = local64_read(&event->hw.period_left) - delta;
2168 * If address is not requested in the sample via
2169 * PERF_SAMPLE_IP, just record that sample irrespective
2170 * of SIAR valid check.
2172 if (event->attr.sample_type & PERF_SAMPLE_IP)
2173 record = siar_valid(regs);
2177 event->hw.last_period = event->hw.sample_period;
2179 if (left < 0x80000000LL)
2180 val = 0x80000000LL - left;
2183 write_pmc(event->hw.idx, val);
2184 local64_set(&event->hw.prev_count, val);
2185 local64_set(&event->hw.period_left, left);
2186 perf_event_update_userpage(event);
2189 * Due to hardware limitation, sometimes SIAR could sample a kernel
2190 * address even when freeze on supervisor state (kernel) is set in
2191 * MMCR2. Check attr.exclude_kernel and address to drop the sample in
2194 if (event->attr.exclude_kernel &&
2195 (event->attr.sample_type & PERF_SAMPLE_IP) &&
2196 is_kernel_addr(mfspr(SPRN_SIAR)))
2200 * Finally record data if requested.
2203 struct perf_sample_data data;
2205 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
2207 if (event->attr.sample_type & PERF_SAMPLE_ADDR_TYPE)
2208 perf_get_data_addr(event, regs, &data.addr);
2210 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
2211 struct cpu_hw_events *cpuhw;
2212 cpuhw = this_cpu_ptr(&cpu_hw_events);
2213 power_pmu_bhrb_read(event, cpuhw);
2214 data.br_stack = &cpuhw->bhrb_stack;
2217 if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
2218 ppmu->get_mem_data_src)
2219 ppmu->get_mem_data_src(&data.data_src, ppmu->flags, regs);
2221 if (event->attr.sample_type & PERF_SAMPLE_WEIGHT_TYPE &&
2222 ppmu->get_mem_weight)
2223 ppmu->get_mem_weight(&data.weight.full, event->attr.sample_type);
2225 if (perf_event_overflow(event, &data, regs))
2226 power_pmu_stop(event, 0);
2227 } else if (period) {
2228 /* Account for interrupt in case of invalid SIAR */
2229 if (perf_event_account_interrupt(event))
2230 power_pmu_stop(event, 0);
2235 * Called from generic code to get the misc flags (i.e. processor mode)
2238 unsigned long perf_misc_flags(struct pt_regs *regs)
2240 u32 flags = perf_get_misc_flags(regs);
2244 return user_mode(regs) ? PERF_RECORD_MISC_USER :
2245 PERF_RECORD_MISC_KERNEL;
2249 * Called from generic code to get the instruction pointer
2252 unsigned long perf_instruction_pointer(struct pt_regs *regs)
2254 bool use_siar = regs_use_siar(regs);
2255 unsigned long siar = mfspr(SPRN_SIAR);
2257 if (ppmu->flags & PPMU_P10_DD1) {
2262 } else if (use_siar && siar_valid(regs))
2263 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
2265 return 0; // no valid instruction pointer
2270 static bool pmc_overflow_power7(unsigned long val)
2273 * Events on POWER7 can roll back if a speculative event doesn't
2274 * eventually complete. Unfortunately in some rare cases they will
2275 * raise a performance monitor exception. We need to catch this to
2276 * ensure we reset the PMC. In all cases the PMC will be 256 or less
2277 * cycles from overflow.
2279 * We only do this if the first pass fails to find any overflowing
2280 * PMCs because a user might set a period of less than 256 and we
2281 * don't want to mistakenly reset them.
2283 if ((0x80000000 - val) <= 256)
2289 static bool pmc_overflow(unsigned long val)
2298 * Performance monitor interrupt stuff
2300 static void __perf_event_interrupt(struct pt_regs *regs)
2303 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
2304 struct perf_event *event;
2307 if (cpuhw->n_limited)
2308 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
2311 perf_read_regs(regs);
2313 /* Read all the PMCs since we'll need them a bunch of times */
2314 for (i = 0; i < ppmu->n_counter; ++i)
2315 cpuhw->pmcs[i] = read_pmc(i + 1);
2317 /* Try to find what caused the IRQ */
2319 for (i = 0; i < ppmu->n_counter; ++i) {
2320 if (!pmc_overflow(cpuhw->pmcs[i]))
2322 if (is_limited_pmc(i + 1))
2323 continue; /* these won't generate IRQs */
2325 * We've found one that's overflowed. For active
2326 * counters we need to log this. For inactive
2327 * counters, we need to reset it anyway
2331 for (j = 0; j < cpuhw->n_events; ++j) {
2332 event = cpuhw->event[j];
2333 if (event->hw.idx == (i + 1)) {
2335 record_and_restart(event, cpuhw->pmcs[i], regs);
2340 /* reset non active counters that have overflowed */
2341 write_pmc(i + 1, 0);
2343 if (!found && pvr_version_is(PVR_POWER7)) {
2344 /* check active counters for special buggy p7 overflow */
2345 for (i = 0; i < cpuhw->n_events; ++i) {
2346 event = cpuhw->event[i];
2347 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
2349 if (pmc_overflow_power7(cpuhw->pmcs[event->hw.idx - 1])) {
2350 /* event has overflowed in a buggy way*/
2352 record_and_restart(event,
2353 cpuhw->pmcs[event->hw.idx - 1],
2358 if (unlikely(!found) && !arch_irq_disabled_regs(regs))
2359 printk_ratelimited(KERN_WARNING "Can't find PMC that caused IRQ\n");
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.
2368 write_mmcr0(cpuhw, cpuhw->mmcr.mmcr0);
2370 /* Clear the cpuhw->pmcs */
2371 memset(&cpuhw->pmcs, 0, sizeof(cpuhw->pmcs));
2375 static void perf_event_interrupt(struct pt_regs *regs)
2377 u64 start_clock = sched_clock();
2379 __perf_event_interrupt(regs);
2380 perf_sample_event_took(sched_clock() - start_clock);
2383 static int power_pmu_prepare_cpu(unsigned int cpu)
2385 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2388 memset(cpuhw, 0, sizeof(*cpuhw));
2389 cpuhw->mmcr.mmcr0 = MMCR0_FC;
2394 int register_power_pmu(struct power_pmu *pmu)
2397 return -EBUSY; /* something's already registered */
2400 pr_info("%s performance monitor hardware support registered\n",
2403 power_pmu.attr_groups = ppmu->attr_groups;
2404 power_pmu.capabilities |= (ppmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS);
2408 * Use FCHV to ignore kernel events if MSR.HV is set.
2410 if (mfmsr() & MSR_HV)
2411 freeze_events_kernel = MMCR0_FCHV;
2412 #endif /* CONFIG_PPC64 */
2414 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
2415 cpuhp_setup_state(CPUHP_PERF_POWER, "perf/powerpc:prepare",
2416 power_pmu_prepare_cpu, NULL);
2421 static int __init init_ppc64_pmu(void)
2423 /* run through all the pmu drivers one at a time */
2424 if (!init_power5_pmu())
2426 else if (!init_power5p_pmu())
2428 else if (!init_power6_pmu())
2430 else if (!init_power7_pmu())
2432 else if (!init_power8_pmu())
2434 else if (!init_power9_pmu())
2436 else if (!init_power10_pmu())
2438 else if (!init_ppc970_pmu())
2441 return init_generic_compat_pmu();
2443 early_initcall(init_ppc64_pmu);