Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / powerpc / perf / core-book3s.c
blob28206b1fe172a45b737edd4b29da56c1b36d0055
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Performance event support - powerpc architecture code
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>
21 #ifdef CONFIG_PPC64
22 #include "internal.h"
23 #endif
25 #define BHRB_MAX_ENTRIES 32
26 #define BHRB_TARGET 0x0000000000000002
27 #define BHRB_PREDICTION 0x0000000000000001
28 #define BHRB_EA 0xFFFFFFFFFFFFFFFCUL
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];
47 unsigned int txn_flags;
48 int n_txn_start;
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;
59 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
61 static struct power_pmu *ppmu;
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.
70 static unsigned int freeze_events_kernel = MMCR0_FCS;
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.
81 #ifdef CONFIG_PPC32
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
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
100 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
102 return 0;
104 static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp) { }
105 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
107 return 0;
109 static inline void perf_read_regs(struct pt_regs *regs)
111 regs->result = 0;
113 static inline int perf_intr_is_nmi(struct pt_regs *regs)
115 return 0;
118 static inline int siar_valid(struct pt_regs *regs)
120 return 1;
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)
141 if (!ppmu)
142 return false;
144 if (ppmu->flags & PPMU_HAS_SIER)
145 return true;
147 return false;
150 static bool regs_use_siar(struct pt_regs *regs)
153 * When we take a performance monitor exception the regs are setup
154 * using perf_read_regs() which overloads some fields, in particular
155 * regs->result to tell us whether to use SIAR.
157 * However if the regs are from another exception, eg. a syscall, then
158 * they have not been setup using perf_read_regs() and so regs->result
159 * is something random.
161 return ((TRAP(regs) == 0xf00) && regs->result);
165 * Things that are specific to 64-bit implementations.
167 #ifdef CONFIG_PPC64
169 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
171 unsigned long mmcra = regs->dsisr;
173 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
174 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
175 if (slot > 1)
176 return 4 * (slot - 1);
179 return 0;
183 * The user wants a data address recorded.
184 * If we're not doing instruction sampling, give them the SDAR
185 * (sampled data address). If we are doing instruction sampling, then
186 * only give them the SDAR if it corresponds to the instruction
187 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
188 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
190 static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp)
192 unsigned long mmcra = regs->dsisr;
193 bool sdar_valid;
195 if (ppmu->flags & PPMU_HAS_SIER)
196 sdar_valid = regs->dar & SIER_SDAR_VALID;
197 else {
198 unsigned long sdsync;
200 if (ppmu->flags & PPMU_SIAR_VALID)
201 sdsync = POWER7P_MMCRA_SDAR_VALID;
202 else if (ppmu->flags & PPMU_ALT_SIPR)
203 sdsync = POWER6_MMCRA_SDSYNC;
204 else if (ppmu->flags & PPMU_NO_SIAR)
205 sdsync = MMCRA_SAMPLE_ENABLE;
206 else
207 sdsync = MMCRA_SDSYNC;
209 sdar_valid = mmcra & sdsync;
212 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
213 *addrp = mfspr(SPRN_SDAR);
215 if (is_kernel_addr(mfspr(SPRN_SDAR)) && perf_allow_kernel(&event->attr) != 0)
216 *addrp = 0;
219 static bool regs_sihv(struct pt_regs *regs)
221 unsigned long sihv = MMCRA_SIHV;
223 if (ppmu->flags & PPMU_HAS_SIER)
224 return !!(regs->dar & SIER_SIHV);
226 if (ppmu->flags & PPMU_ALT_SIPR)
227 sihv = POWER6_MMCRA_SIHV;
229 return !!(regs->dsisr & sihv);
232 static bool regs_sipr(struct pt_regs *regs)
234 unsigned long sipr = MMCRA_SIPR;
236 if (ppmu->flags & PPMU_HAS_SIER)
237 return !!(regs->dar & SIER_SIPR);
239 if (ppmu->flags & PPMU_ALT_SIPR)
240 sipr = POWER6_MMCRA_SIPR;
242 return !!(regs->dsisr & sipr);
245 static inline u32 perf_flags_from_msr(struct pt_regs *regs)
247 if (regs->msr & MSR_PR)
248 return PERF_RECORD_MISC_USER;
249 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
250 return PERF_RECORD_MISC_HYPERVISOR;
251 return PERF_RECORD_MISC_KERNEL;
254 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
256 bool use_siar = regs_use_siar(regs);
257 unsigned long mmcra = regs->dsisr;
258 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
260 if (!use_siar)
261 return perf_flags_from_msr(regs);
264 * Check the address in SIAR to identify the
265 * privilege levels since the SIER[MSR_HV, MSR_PR]
266 * bits are not set for marked events in power10
267 * DD1.
269 if (marked && (ppmu->flags & PPMU_P10_DD1)) {
270 unsigned long siar = mfspr(SPRN_SIAR);
271 if (siar) {
272 if (is_kernel_addr(siar))
273 return PERF_RECORD_MISC_KERNEL;
274 return PERF_RECORD_MISC_USER;
275 } else {
276 if (is_kernel_addr(regs->nip))
277 return PERF_RECORD_MISC_KERNEL;
278 return PERF_RECORD_MISC_USER;
283 * If we don't have flags in MMCRA, rather than using
284 * the MSR, we intuit the flags from the address in
285 * SIAR which should give slightly more reliable
286 * results
288 if (ppmu->flags & PPMU_NO_SIPR) {
289 unsigned long siar = mfspr(SPRN_SIAR);
290 if (is_kernel_addr(siar))
291 return PERF_RECORD_MISC_KERNEL;
292 return PERF_RECORD_MISC_USER;
295 /* PR has priority over HV, so order below is important */
296 if (regs_sipr(regs))
297 return PERF_RECORD_MISC_USER;
299 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
300 return PERF_RECORD_MISC_HYPERVISOR;
302 return PERF_RECORD_MISC_KERNEL;
306 * Overload regs->dsisr to store MMCRA so we only need to read it once
307 * on each interrupt.
308 * Overload regs->dar to store SIER if we have it.
309 * Overload regs->result to specify whether we should use the MSR (result
310 * is zero) or the SIAR (result is non zero).
312 static inline void perf_read_regs(struct pt_regs *regs)
314 unsigned long mmcra = mfspr(SPRN_MMCRA);
315 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
316 int use_siar;
318 regs->dsisr = mmcra;
320 if (ppmu->flags & PPMU_HAS_SIER)
321 regs->dar = mfspr(SPRN_SIER);
324 * If this isn't a PMU exception (eg a software event) the SIAR is
325 * not valid. Use pt_regs.
327 * If it is a marked event use the SIAR.
329 * If the PMU doesn't update the SIAR for non marked events use
330 * pt_regs.
332 * If the PMU has HV/PR flags then check to see if they
333 * place the exception in userspace. If so, use pt_regs. In
334 * continuous sampling mode the SIAR and the PMU exception are
335 * not synchronised, so they may be many instructions apart.
336 * This can result in confusing backtraces. We still want
337 * hypervisor samples as well as samples in the kernel with
338 * interrupts off hence the userspace check.
340 if (TRAP(regs) != 0xf00)
341 use_siar = 0;
342 else if ((ppmu->flags & PPMU_NO_SIAR))
343 use_siar = 0;
344 else if (marked)
345 use_siar = 1;
346 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
347 use_siar = 0;
348 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
349 use_siar = 0;
350 else
351 use_siar = 1;
353 regs->result = use_siar;
357 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
358 * it as an NMI.
360 static inline int perf_intr_is_nmi(struct pt_regs *regs)
362 return (regs->softe & IRQS_DISABLED);
366 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
367 * must be sampled only if the SIAR-valid bit is set.
369 * For unmarked instructions and for processors that don't have the SIAR-Valid
370 * bit, assume that SIAR is valid.
372 static inline int siar_valid(struct pt_regs *regs)
374 unsigned long mmcra = regs->dsisr;
375 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
377 if (marked) {
379 * SIER[SIAR_VALID] is not set for some
380 * marked events on power10 DD1, so drop
381 * the check for SIER[SIAR_VALID] and return true.
383 if (ppmu->flags & PPMU_P10_DD1)
384 return 0x1;
385 else if (ppmu->flags & PPMU_HAS_SIER)
386 return regs->dar & SIER_SIAR_VALID;
388 if (ppmu->flags & PPMU_SIAR_VALID)
389 return mmcra & POWER7P_MMCRA_SIAR_VALID;
392 return 1;
396 /* Reset all possible BHRB entries */
397 static void power_pmu_bhrb_reset(void)
399 asm volatile(PPC_CLRBHRB);
402 static void power_pmu_bhrb_enable(struct perf_event *event)
404 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
406 if (!ppmu->bhrb_nr)
407 return;
409 /* Clear BHRB if we changed task context to avoid data leaks */
410 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
411 power_pmu_bhrb_reset();
412 cpuhw->bhrb_context = event->ctx;
414 cpuhw->bhrb_users++;
415 perf_sched_cb_inc(event->ctx->pmu);
418 static void power_pmu_bhrb_disable(struct perf_event *event)
420 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
422 if (!ppmu->bhrb_nr)
423 return;
425 WARN_ON_ONCE(!cpuhw->bhrb_users);
426 cpuhw->bhrb_users--;
427 perf_sched_cb_dec(event->ctx->pmu);
429 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
430 /* BHRB cannot be turned off when other
431 * events are active on the PMU.
434 /* avoid stale pointer */
435 cpuhw->bhrb_context = NULL;
439 /* Called from ctxsw to prevent one process's branch entries to
440 * mingle with the other process's entries during context switch.
442 static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
444 if (!ppmu->bhrb_nr)
445 return;
447 if (sched_in)
448 power_pmu_bhrb_reset();
450 /* Calculate the to address for a branch */
451 static __u64 power_pmu_bhrb_to(u64 addr)
453 unsigned int instr;
454 __u64 target;
456 if (is_kernel_addr(addr)) {
457 if (copy_from_kernel_nofault(&instr, (void *)addr,
458 sizeof(instr)))
459 return 0;
461 return branch_target((struct ppc_inst *)&instr);
464 /* Userspace: need copy instruction here then translate it */
465 if (copy_from_user_nofault(&instr, (unsigned int __user *)addr,
466 sizeof(instr)))
467 return 0;
469 target = branch_target((struct ppc_inst *)&instr);
470 if ((!target) || (instr & BRANCH_ABSOLUTE))
471 return target;
473 /* Translate relative branch target from kernel to user address */
474 return target - (unsigned long)&instr + addr;
477 /* Processing BHRB entries */
478 static void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw)
480 u64 val;
481 u64 addr;
482 int r_index, u_index, pred;
484 r_index = 0;
485 u_index = 0;
486 while (r_index < ppmu->bhrb_nr) {
487 /* Assembly read function */
488 val = read_bhrb(r_index++);
489 if (!val)
490 /* Terminal marker: End of valid BHRB entries */
491 break;
492 else {
493 addr = val & BHRB_EA;
494 pred = val & BHRB_PREDICTION;
496 if (!addr)
497 /* invalid entry */
498 continue;
501 * BHRB rolling buffer could very much contain the kernel
502 * addresses at this point. Check the privileges before
503 * exporting it to userspace (avoid exposure of regions
504 * where we could have speculative execution)
505 * Incase of ISA v3.1, BHRB will capture only user-space
506 * addresses, hence include a check before filtering code
508 if (!(ppmu->flags & PPMU_ARCH_31) &&
509 is_kernel_addr(addr) && perf_allow_kernel(&event->attr) != 0)
510 continue;
512 /* Branches are read most recent first (ie. mfbhrb 0 is
513 * the most recent branch).
514 * There are two types of valid entries:
515 * 1) a target entry which is the to address of a
516 * computed goto like a blr,bctr,btar. The next
517 * entry read from the bhrb will be branch
518 * corresponding to this target (ie. the actual
519 * blr/bctr/btar instruction).
520 * 2) a from address which is an actual branch. If a
521 * target entry proceeds this, then this is the
522 * matching branch for that target. If this is not
523 * following a target entry, then this is a branch
524 * where the target is given as an immediate field
525 * in the instruction (ie. an i or b form branch).
526 * In this case we need to read the instruction from
527 * memory to determine the target/to address.
530 if (val & BHRB_TARGET) {
531 /* Target branches use two entries
532 * (ie. computed gotos/XL form)
534 cpuhw->bhrb_entries[u_index].to = addr;
535 cpuhw->bhrb_entries[u_index].mispred = pred;
536 cpuhw->bhrb_entries[u_index].predicted = ~pred;
538 /* Get from address in next entry */
539 val = read_bhrb(r_index++);
540 addr = val & BHRB_EA;
541 if (val & BHRB_TARGET) {
542 /* Shouldn't have two targets in a
543 row.. Reset index and try again */
544 r_index--;
545 addr = 0;
547 cpuhw->bhrb_entries[u_index].from = addr;
548 } else {
549 /* Branches to immediate field
550 (ie I or B form) */
551 cpuhw->bhrb_entries[u_index].from = addr;
552 cpuhw->bhrb_entries[u_index].to =
553 power_pmu_bhrb_to(addr);
554 cpuhw->bhrb_entries[u_index].mispred = pred;
555 cpuhw->bhrb_entries[u_index].predicted = ~pred;
557 u_index++;
561 cpuhw->bhrb_stack.nr = u_index;
562 cpuhw->bhrb_stack.hw_idx = -1ULL;
563 return;
566 static bool is_ebb_event(struct perf_event *event)
569 * This could be a per-PMU callback, but we'd rather avoid the cost. We
570 * check that the PMU supports EBB, meaning those that don't can still
571 * use bit 63 of the event code for something else if they wish.
573 return (ppmu->flags & PPMU_ARCH_207S) &&
574 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
577 static int ebb_event_check(struct perf_event *event)
579 struct perf_event *leader = event->group_leader;
581 /* Event and group leader must agree on EBB */
582 if (is_ebb_event(leader) != is_ebb_event(event))
583 return -EINVAL;
585 if (is_ebb_event(event)) {
586 if (!(event->attach_state & PERF_ATTACH_TASK))
587 return -EINVAL;
589 if (!leader->attr.pinned || !leader->attr.exclusive)
590 return -EINVAL;
592 if (event->attr.freq ||
593 event->attr.inherit ||
594 event->attr.sample_type ||
595 event->attr.sample_period ||
596 event->attr.enable_on_exec)
597 return -EINVAL;
600 return 0;
603 static void ebb_event_add(struct perf_event *event)
605 if (!is_ebb_event(event) || current->thread.used_ebb)
606 return;
609 * IFF this is the first time we've added an EBB event, set
610 * PMXE in the user MMCR0 so we can detect when it's cleared by
611 * userspace. We need this so that we can context switch while
612 * userspace is in the EBB handler (where PMXE is 0).
614 current->thread.used_ebb = 1;
615 current->thread.mmcr0 |= MMCR0_PMXE;
618 static void ebb_switch_out(unsigned long mmcr0)
620 if (!(mmcr0 & MMCR0_EBE))
621 return;
623 current->thread.siar = mfspr(SPRN_SIAR);
624 current->thread.sier = mfspr(SPRN_SIER);
625 current->thread.sdar = mfspr(SPRN_SDAR);
626 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
627 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
628 if (ppmu->flags & PPMU_ARCH_31) {
629 current->thread.mmcr3 = mfspr(SPRN_MMCR3);
630 current->thread.sier2 = mfspr(SPRN_SIER2);
631 current->thread.sier3 = mfspr(SPRN_SIER3);
635 static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
637 unsigned long mmcr0 = cpuhw->mmcr.mmcr0;
639 if (!ebb)
640 goto out;
642 /* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
643 mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
646 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
647 * with pmao_restore_workaround() because we may add PMAO but we never
648 * clear it here.
650 mmcr0 |= current->thread.mmcr0;
653 * Be careful not to set PMXE if userspace had it cleared. This is also
654 * compatible with pmao_restore_workaround() because it has already
655 * cleared PMXE and we leave PMAO alone.
657 if (!(current->thread.mmcr0 & MMCR0_PMXE))
658 mmcr0 &= ~MMCR0_PMXE;
660 mtspr(SPRN_SIAR, current->thread.siar);
661 mtspr(SPRN_SIER, current->thread.sier);
662 mtspr(SPRN_SDAR, current->thread.sdar);
665 * Merge the kernel & user values of MMCR2. The semantics we implement
666 * are that the user MMCR2 can set bits, ie. cause counters to freeze,
667 * but not clear bits. If a task wants to be able to clear bits, ie.
668 * unfreeze counters, it should not set exclude_xxx in its events and
669 * instead manage the MMCR2 entirely by itself.
671 mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2 | current->thread.mmcr2);
673 if (ppmu->flags & PPMU_ARCH_31) {
674 mtspr(SPRN_MMCR3, current->thread.mmcr3);
675 mtspr(SPRN_SIER2, current->thread.sier2);
676 mtspr(SPRN_SIER3, current->thread.sier3);
678 out:
679 return mmcr0;
682 static void pmao_restore_workaround(bool ebb)
684 unsigned pmcs[6];
686 if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
687 return;
690 * On POWER8E there is a hardware defect which affects the PMU context
691 * switch logic, ie. power_pmu_disable/enable().
693 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
694 * by the hardware. Sometime later the actual PMU exception is
695 * delivered.
697 * If we context switch, or simply disable/enable, the PMU prior to the
698 * exception arriving, the exception will be lost when we clear PMAO.
700 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
701 * set, and this _should_ generate an exception. However because of the
702 * defect no exception is generated when we write PMAO, and we get
703 * stuck with no counters counting but no exception delivered.
705 * The workaround is to detect this case and tweak the hardware to
706 * create another pending PMU exception.
708 * We do that by setting up PMC6 (cycles) for an imminent overflow and
709 * enabling the PMU. That causes a new exception to be generated in the
710 * chip, but we don't take it yet because we have interrupts hard
711 * disabled. We then write back the PMU state as we want it to be seen
712 * by the exception handler. When we reenable interrupts the exception
713 * handler will be called and see the correct state.
715 * The logic is the same for EBB, except that the exception is gated by
716 * us having interrupts hard disabled as well as the fact that we are
717 * not in userspace. The exception is finally delivered when we return
718 * to userspace.
721 /* Only if PMAO is set and PMAO_SYNC is clear */
722 if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
723 return;
725 /* If we're doing EBB, only if BESCR[GE] is set */
726 if (ebb && !(current->thread.bescr & BESCR_GE))
727 return;
730 * We are already soft-disabled in power_pmu_enable(). We need to hard
731 * disable to actually prevent the PMU exception from firing.
733 hard_irq_disable();
736 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
737 * Using read/write_pmc() in a for loop adds 12 function calls and
738 * almost doubles our code size.
740 pmcs[0] = mfspr(SPRN_PMC1);
741 pmcs[1] = mfspr(SPRN_PMC2);
742 pmcs[2] = mfspr(SPRN_PMC3);
743 pmcs[3] = mfspr(SPRN_PMC4);
744 pmcs[4] = mfspr(SPRN_PMC5);
745 pmcs[5] = mfspr(SPRN_PMC6);
747 /* Ensure all freeze bits are unset */
748 mtspr(SPRN_MMCR2, 0);
750 /* Set up PMC6 to overflow in one cycle */
751 mtspr(SPRN_PMC6, 0x7FFFFFFE);
753 /* Enable exceptions and unfreeze PMC6 */
754 mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
756 /* Now we need to refreeze and restore the PMCs */
757 mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
759 mtspr(SPRN_PMC1, pmcs[0]);
760 mtspr(SPRN_PMC2, pmcs[1]);
761 mtspr(SPRN_PMC3, pmcs[2]);
762 mtspr(SPRN_PMC4, pmcs[3]);
763 mtspr(SPRN_PMC5, pmcs[4]);
764 mtspr(SPRN_PMC6, pmcs[5]);
767 #endif /* CONFIG_PPC64 */
769 static void perf_event_interrupt(struct pt_regs *regs);
772 * Read one performance monitor counter (PMC).
774 static unsigned long read_pmc(int idx)
776 unsigned long val;
778 switch (idx) {
779 case 1:
780 val = mfspr(SPRN_PMC1);
781 break;
782 case 2:
783 val = mfspr(SPRN_PMC2);
784 break;
785 case 3:
786 val = mfspr(SPRN_PMC3);
787 break;
788 case 4:
789 val = mfspr(SPRN_PMC4);
790 break;
791 case 5:
792 val = mfspr(SPRN_PMC5);
793 break;
794 case 6:
795 val = mfspr(SPRN_PMC6);
796 break;
797 #ifdef CONFIG_PPC64
798 case 7:
799 val = mfspr(SPRN_PMC7);
800 break;
801 case 8:
802 val = mfspr(SPRN_PMC8);
803 break;
804 #endif /* CONFIG_PPC64 */
805 default:
806 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
807 val = 0;
809 return val;
813 * Write one PMC.
815 static void write_pmc(int idx, unsigned long val)
817 switch (idx) {
818 case 1:
819 mtspr(SPRN_PMC1, val);
820 break;
821 case 2:
822 mtspr(SPRN_PMC2, val);
823 break;
824 case 3:
825 mtspr(SPRN_PMC3, val);
826 break;
827 case 4:
828 mtspr(SPRN_PMC4, val);
829 break;
830 case 5:
831 mtspr(SPRN_PMC5, val);
832 break;
833 case 6:
834 mtspr(SPRN_PMC6, val);
835 break;
836 #ifdef CONFIG_PPC64
837 case 7:
838 mtspr(SPRN_PMC7, val);
839 break;
840 case 8:
841 mtspr(SPRN_PMC8, val);
842 break;
843 #endif /* CONFIG_PPC64 */
844 default:
845 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
849 /* Called from sysrq_handle_showregs() */
850 void perf_event_print_debug(void)
852 unsigned long sdar, sier, flags;
853 u32 pmcs[MAX_HWEVENTS];
854 int i;
856 if (!ppmu) {
857 pr_info("Performance monitor hardware not registered.\n");
858 return;
861 if (!ppmu->n_counter)
862 return;
864 local_irq_save(flags);
866 pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
867 smp_processor_id(), ppmu->name, ppmu->n_counter);
869 for (i = 0; i < ppmu->n_counter; i++)
870 pmcs[i] = read_pmc(i + 1);
872 for (; i < MAX_HWEVENTS; i++)
873 pmcs[i] = 0xdeadbeef;
875 pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
876 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
878 if (ppmu->n_counter > 4)
879 pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
880 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
882 pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
883 mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
885 sdar = sier = 0;
886 #ifdef CONFIG_PPC64
887 sdar = mfspr(SPRN_SDAR);
889 if (ppmu->flags & PPMU_HAS_SIER)
890 sier = mfspr(SPRN_SIER);
892 if (ppmu->flags & PPMU_ARCH_207S) {
893 pr_info("MMCR2: %016lx EBBHR: %016lx\n",
894 mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
895 pr_info("EBBRR: %016lx BESCR: %016lx\n",
896 mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
899 if (ppmu->flags & PPMU_ARCH_31) {
900 pr_info("MMCR3: %016lx SIER2: %016lx SIER3: %016lx\n",
901 mfspr(SPRN_MMCR3), mfspr(SPRN_SIER2), mfspr(SPRN_SIER3));
903 #endif
904 pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
905 mfspr(SPRN_SIAR), sdar, sier);
907 local_irq_restore(flags);
911 * Check if a set of events can all go on the PMU at once.
912 * If they can't, this will look at alternative codes for the events
913 * and see if any combination of alternative codes is feasible.
914 * The feasible set is returned in event_id[].
916 static int power_check_constraints(struct cpu_hw_events *cpuhw,
917 u64 event_id[], unsigned int cflags[],
918 int n_ev)
920 unsigned long mask, value, nv;
921 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
922 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
923 int i, j;
924 unsigned long addf = ppmu->add_fields;
925 unsigned long tadd = ppmu->test_adder;
926 unsigned long grp_mask = ppmu->group_constraint_mask;
927 unsigned long grp_val = ppmu->group_constraint_val;
929 if (n_ev > ppmu->n_counter)
930 return -1;
932 /* First see if the events will go on as-is */
933 for (i = 0; i < n_ev; ++i) {
934 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
935 && !ppmu->limited_pmc_event(event_id[i])) {
936 ppmu->get_alternatives(event_id[i], cflags[i],
937 cpuhw->alternatives[i]);
938 event_id[i] = cpuhw->alternatives[i][0];
940 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
941 &cpuhw->avalues[i][0]))
942 return -1;
944 value = mask = 0;
945 for (i = 0; i < n_ev; ++i) {
946 nv = (value | cpuhw->avalues[i][0]) +
947 (value & cpuhw->avalues[i][0] & addf);
949 if (((((nv + tadd) ^ value) & mask) & (~grp_mask)) != 0)
950 break;
952 if (((((nv + tadd) ^ cpuhw->avalues[i][0]) & cpuhw->amasks[i][0])
953 & (~grp_mask)) != 0)
954 break;
956 value = nv;
957 mask |= cpuhw->amasks[i][0];
959 if (i == n_ev) {
960 if ((value & mask & grp_mask) != (mask & grp_val))
961 return -1;
962 else
963 return 0; /* all OK */
966 /* doesn't work, gather alternatives... */
967 if (!ppmu->get_alternatives)
968 return -1;
969 for (i = 0; i < n_ev; ++i) {
970 choice[i] = 0;
971 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
972 cpuhw->alternatives[i]);
973 for (j = 1; j < n_alt[i]; ++j)
974 ppmu->get_constraint(cpuhw->alternatives[i][j],
975 &cpuhw->amasks[i][j],
976 &cpuhw->avalues[i][j]);
979 /* enumerate all possibilities and see if any will work */
980 i = 0;
981 j = -1;
982 value = mask = nv = 0;
983 while (i < n_ev) {
984 if (j >= 0) {
985 /* we're backtracking, restore context */
986 value = svalues[i];
987 mask = smasks[i];
988 j = choice[i];
991 * See if any alternative k for event_id i,
992 * where k > j, will satisfy the constraints.
994 while (++j < n_alt[i]) {
995 nv = (value | cpuhw->avalues[i][j]) +
996 (value & cpuhw->avalues[i][j] & addf);
997 if ((((nv + tadd) ^ value) & mask) == 0 &&
998 (((nv + tadd) ^ cpuhw->avalues[i][j])
999 & cpuhw->amasks[i][j]) == 0)
1000 break;
1002 if (j >= n_alt[i]) {
1004 * No feasible alternative, backtrack
1005 * to event_id i-1 and continue enumerating its
1006 * alternatives from where we got up to.
1008 if (--i < 0)
1009 return -1;
1010 } else {
1012 * Found a feasible alternative for event_id i,
1013 * remember where we got up to with this event_id,
1014 * go on to the next event_id, and start with
1015 * the first alternative for it.
1017 choice[i] = j;
1018 svalues[i] = value;
1019 smasks[i] = mask;
1020 value = nv;
1021 mask |= cpuhw->amasks[i][j];
1022 ++i;
1023 j = -1;
1027 /* OK, we have a feasible combination, tell the caller the solution */
1028 for (i = 0; i < n_ev; ++i)
1029 event_id[i] = cpuhw->alternatives[i][choice[i]];
1030 return 0;
1034 * Check if newly-added events have consistent settings for
1035 * exclude_{user,kernel,hv} with each other and any previously
1036 * added events.
1038 static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
1039 int n_prev, int n_new)
1041 int eu = 0, ek = 0, eh = 0;
1042 int i, n, first;
1043 struct perf_event *event;
1046 * If the PMU we're on supports per event exclude settings then we
1047 * don't need to do any of this logic. NB. This assumes no PMU has both
1048 * per event exclude and limited PMCs.
1050 if (ppmu->flags & PPMU_ARCH_207S)
1051 return 0;
1053 n = n_prev + n_new;
1054 if (n <= 1)
1055 return 0;
1057 first = 1;
1058 for (i = 0; i < n; ++i) {
1059 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
1060 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
1061 continue;
1063 event = ctrs[i];
1064 if (first) {
1065 eu = event->attr.exclude_user;
1066 ek = event->attr.exclude_kernel;
1067 eh = event->attr.exclude_hv;
1068 first = 0;
1069 } else if (event->attr.exclude_user != eu ||
1070 event->attr.exclude_kernel != ek ||
1071 event->attr.exclude_hv != eh) {
1072 return -EAGAIN;
1076 if (eu || ek || eh)
1077 for (i = 0; i < n; ++i)
1078 if (cflags[i] & PPMU_LIMITED_PMC_OK)
1079 cflags[i] |= PPMU_LIMITED_PMC_REQD;
1081 return 0;
1084 static u64 check_and_compute_delta(u64 prev, u64 val)
1086 u64 delta = (val - prev) & 0xfffffffful;
1089 * POWER7 can roll back counter values, if the new value is smaller
1090 * than the previous value it will cause the delta and the counter to
1091 * have bogus values unless we rolled a counter over. If a coutner is
1092 * rolled back, it will be smaller, but within 256, which is the maximum
1093 * number of events to rollback at once. If we detect a rollback
1094 * return 0. This can lead to a small lack of precision in the
1095 * counters.
1097 if (prev > val && (prev - val) < 256)
1098 delta = 0;
1100 return delta;
1103 static void power_pmu_read(struct perf_event *event)
1105 s64 val, delta, prev;
1107 if (event->hw.state & PERF_HES_STOPPED)
1108 return;
1110 if (!event->hw.idx)
1111 return;
1113 if (is_ebb_event(event)) {
1114 val = read_pmc(event->hw.idx);
1115 local64_set(&event->hw.prev_count, val);
1116 return;
1120 * Performance monitor interrupts come even when interrupts
1121 * are soft-disabled, as long as interrupts are hard-enabled.
1122 * Therefore we treat them like NMIs.
1124 do {
1125 prev = local64_read(&event->hw.prev_count);
1126 barrier();
1127 val = read_pmc(event->hw.idx);
1128 delta = check_and_compute_delta(prev, val);
1129 if (!delta)
1130 return;
1131 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
1133 local64_add(delta, &event->count);
1136 * A number of places program the PMC with (0x80000000 - period_left).
1137 * We never want period_left to be less than 1 because we will program
1138 * the PMC with a value >= 0x800000000 and an edge detected PMC will
1139 * roll around to 0 before taking an exception. We have seen this
1140 * on POWER8.
1142 * To fix this, clamp the minimum value of period_left to 1.
1144 do {
1145 prev = local64_read(&event->hw.period_left);
1146 val = prev - delta;
1147 if (val < 1)
1148 val = 1;
1149 } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
1153 * On some machines, PMC5 and PMC6 can't be written, don't respect
1154 * the freeze conditions, and don't generate interrupts. This tells
1155 * us if `event' is using such a PMC.
1157 static int is_limited_pmc(int pmcnum)
1159 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1160 && (pmcnum == 5 || pmcnum == 6);
1163 static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
1164 unsigned long pmc5, unsigned long pmc6)
1166 struct perf_event *event;
1167 u64 val, prev, delta;
1168 int i;
1170 for (i = 0; i < cpuhw->n_limited; ++i) {
1171 event = cpuhw->limited_counter[i];
1172 if (!event->hw.idx)
1173 continue;
1174 val = (event->hw.idx == 5) ? pmc5 : pmc6;
1175 prev = local64_read(&event->hw.prev_count);
1176 event->hw.idx = 0;
1177 delta = check_and_compute_delta(prev, val);
1178 if (delta)
1179 local64_add(delta, &event->count);
1183 static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
1184 unsigned long pmc5, unsigned long pmc6)
1186 struct perf_event *event;
1187 u64 val, prev;
1188 int i;
1190 for (i = 0; i < cpuhw->n_limited; ++i) {
1191 event = cpuhw->limited_counter[i];
1192 event->hw.idx = cpuhw->limited_hwidx[i];
1193 val = (event->hw.idx == 5) ? pmc5 : pmc6;
1194 prev = local64_read(&event->hw.prev_count);
1195 if (check_and_compute_delta(prev, val))
1196 local64_set(&event->hw.prev_count, val);
1197 perf_event_update_userpage(event);
1202 * Since limited events don't respect the freeze conditions, we
1203 * have to read them immediately after freezing or unfreezing the
1204 * other events. We try to keep the values from the limited
1205 * events as consistent as possible by keeping the delay (in
1206 * cycles and instructions) between freezing/unfreezing and reading
1207 * the limited events as small and consistent as possible.
1208 * Therefore, if any limited events are in use, we read them
1209 * both, and always in the same order, to minimize variability,
1210 * and do it inside the same asm that writes MMCR0.
1212 static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1214 unsigned long pmc5, pmc6;
1216 if (!cpuhw->n_limited) {
1217 mtspr(SPRN_MMCR0, mmcr0);
1218 return;
1222 * Write MMCR0, then read PMC5 and PMC6 immediately.
1223 * To ensure we don't get a performance monitor interrupt
1224 * between writing MMCR0 and freezing/thawing the limited
1225 * events, we first write MMCR0 with the event overflow
1226 * interrupt enable bits turned off.
1228 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1229 : "=&r" (pmc5), "=&r" (pmc6)
1230 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1231 "i" (SPRN_MMCR0),
1232 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1234 if (mmcr0 & MMCR0_FC)
1235 freeze_limited_counters(cpuhw, pmc5, pmc6);
1236 else
1237 thaw_limited_counters(cpuhw, pmc5, pmc6);
1240 * Write the full MMCR0 including the event overflow interrupt
1241 * enable bits, if necessary.
1243 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1244 mtspr(SPRN_MMCR0, mmcr0);
1248 * Disable all events to prevent PMU interrupts and to allow
1249 * events to be added or removed.
1251 static void power_pmu_disable(struct pmu *pmu)
1253 struct cpu_hw_events *cpuhw;
1254 unsigned long flags, mmcr0, val, mmcra;
1256 if (!ppmu)
1257 return;
1258 local_irq_save(flags);
1259 cpuhw = this_cpu_ptr(&cpu_hw_events);
1261 if (!cpuhw->disabled) {
1263 * Check if we ever enabled the PMU on this cpu.
1265 if (!cpuhw->pmcs_enabled) {
1266 ppc_enable_pmcs();
1267 cpuhw->pmcs_enabled = 1;
1271 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
1273 val = mmcr0 = mfspr(SPRN_MMCR0);
1274 val |= MMCR0_FC;
1275 val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
1276 MMCR0_FC56);
1277 /* Set mmcr0 PMCCEXT for p10 */
1278 if (ppmu->flags & PPMU_ARCH_31)
1279 val |= MMCR0_PMCCEXT;
1282 * The barrier is to make sure the mtspr has been
1283 * executed and the PMU has frozen the events etc.
1284 * before we return.
1286 write_mmcr0(cpuhw, val);
1287 mb();
1288 isync();
1290 val = mmcra = cpuhw->mmcr.mmcra;
1293 * Disable instruction sampling if it was enabled
1295 if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE)
1296 val &= ~MMCRA_SAMPLE_ENABLE;
1298 /* Disable BHRB via mmcra (BHRBRD) for p10 */
1299 if (ppmu->flags & PPMU_ARCH_31)
1300 val |= MMCRA_BHRB_DISABLE;
1303 * Write SPRN_MMCRA if mmcra has either disabled
1304 * instruction sampling or BHRB.
1306 if (val != mmcra) {
1307 mtspr(SPRN_MMCRA, mmcra);
1308 mb();
1309 isync();
1312 cpuhw->disabled = 1;
1313 cpuhw->n_added = 0;
1315 ebb_switch_out(mmcr0);
1317 #ifdef CONFIG_PPC64
1319 * These are readable by userspace, may contain kernel
1320 * addresses and are not switched by context switch, so clear
1321 * them now to avoid leaking anything to userspace in general
1322 * including to another process.
1324 if (ppmu->flags & PPMU_ARCH_207S) {
1325 mtspr(SPRN_SDAR, 0);
1326 mtspr(SPRN_SIAR, 0);
1328 #endif
1331 local_irq_restore(flags);
1335 * Re-enable all events if disable == 0.
1336 * If we were previously disabled and events were added, then
1337 * put the new config on the PMU.
1339 static void power_pmu_enable(struct pmu *pmu)
1341 struct perf_event *event;
1342 struct cpu_hw_events *cpuhw;
1343 unsigned long flags;
1344 long i;
1345 unsigned long val, mmcr0;
1346 s64 left;
1347 unsigned int hwc_index[MAX_HWEVENTS];
1348 int n_lim;
1349 int idx;
1350 bool ebb;
1352 if (!ppmu)
1353 return;
1354 local_irq_save(flags);
1356 cpuhw = this_cpu_ptr(&cpu_hw_events);
1357 if (!cpuhw->disabled)
1358 goto out;
1360 if (cpuhw->n_events == 0) {
1361 ppc_set_pmu_inuse(0);
1362 goto out;
1365 cpuhw->disabled = 0;
1368 * EBB requires an exclusive group and all events must have the EBB
1369 * flag set, or not set, so we can just check a single event. Also we
1370 * know we have at least one event.
1372 ebb = is_ebb_event(cpuhw->event[0]);
1375 * If we didn't change anything, or only removed events,
1376 * no need to recalculate MMCR* settings and reset the PMCs.
1377 * Just reenable the PMU with the current MMCR* settings
1378 * (possibly updated for removal of events).
1380 if (!cpuhw->n_added) {
1381 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE);
1382 mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1);
1383 if (ppmu->flags & PPMU_ARCH_31)
1384 mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3);
1385 goto out_enable;
1389 * Clear all MMCR settings and recompute them for the new set of events.
1391 memset(&cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
1393 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
1394 &cpuhw->mmcr, cpuhw->event)) {
1395 /* shouldn't ever get here */
1396 printk(KERN_ERR "oops compute_mmcr failed\n");
1397 goto out;
1400 if (!(ppmu->flags & PPMU_ARCH_207S)) {
1402 * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
1403 * bits for the first event. We have already checked that all
1404 * events have the same value for these bits as the first event.
1406 event = cpuhw->event[0];
1407 if (event->attr.exclude_user)
1408 cpuhw->mmcr.mmcr0 |= MMCR0_FCP;
1409 if (event->attr.exclude_kernel)
1410 cpuhw->mmcr.mmcr0 |= freeze_events_kernel;
1411 if (event->attr.exclude_hv)
1412 cpuhw->mmcr.mmcr0 |= MMCR0_FCHV;
1416 * Write the new configuration to MMCR* with the freeze
1417 * bit set and set the hardware events to their initial values.
1418 * Then unfreeze the events.
1420 ppc_set_pmu_inuse(1);
1421 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra & ~MMCRA_SAMPLE_ENABLE);
1422 mtspr(SPRN_MMCR1, cpuhw->mmcr.mmcr1);
1423 mtspr(SPRN_MMCR0, (cpuhw->mmcr.mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1424 | MMCR0_FC);
1425 if (ppmu->flags & PPMU_ARCH_207S)
1426 mtspr(SPRN_MMCR2, cpuhw->mmcr.mmcr2);
1428 if (ppmu->flags & PPMU_ARCH_31)
1429 mtspr(SPRN_MMCR3, cpuhw->mmcr.mmcr3);
1432 * Read off any pre-existing events that need to move
1433 * to another PMC.
1435 for (i = 0; i < cpuhw->n_events; ++i) {
1436 event = cpuhw->event[i];
1437 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1438 power_pmu_read(event);
1439 write_pmc(event->hw.idx, 0);
1440 event->hw.idx = 0;
1445 * Initialize the PMCs for all the new and moved events.
1447 cpuhw->n_limited = n_lim = 0;
1448 for (i = 0; i < cpuhw->n_events; ++i) {
1449 event = cpuhw->event[i];
1450 if (event->hw.idx)
1451 continue;
1452 idx = hwc_index[i] + 1;
1453 if (is_limited_pmc(idx)) {
1454 cpuhw->limited_counter[n_lim] = event;
1455 cpuhw->limited_hwidx[n_lim] = idx;
1456 ++n_lim;
1457 continue;
1460 if (ebb)
1461 val = local64_read(&event->hw.prev_count);
1462 else {
1463 val = 0;
1464 if (event->hw.sample_period) {
1465 left = local64_read(&event->hw.period_left);
1466 if (left < 0x80000000L)
1467 val = 0x80000000L - left;
1469 local64_set(&event->hw.prev_count, val);
1472 event->hw.idx = idx;
1473 if (event->hw.state & PERF_HES_STOPPED)
1474 val = 0;
1475 write_pmc(idx, val);
1477 perf_event_update_userpage(event);
1479 cpuhw->n_limited = n_lim;
1480 cpuhw->mmcr.mmcr0 |= MMCR0_PMXE | MMCR0_FCECE;
1482 out_enable:
1483 pmao_restore_workaround(ebb);
1485 mmcr0 = ebb_switch_in(ebb, cpuhw);
1487 mb();
1488 if (cpuhw->bhrb_users)
1489 ppmu->config_bhrb(cpuhw->bhrb_filter);
1491 write_mmcr0(cpuhw, mmcr0);
1494 * Enable instruction sampling if necessary
1496 if (cpuhw->mmcr.mmcra & MMCRA_SAMPLE_ENABLE) {
1497 mb();
1498 mtspr(SPRN_MMCRA, cpuhw->mmcr.mmcra);
1501 out:
1503 local_irq_restore(flags);
1506 static int collect_events(struct perf_event *group, int max_count,
1507 struct perf_event *ctrs[], u64 *events,
1508 unsigned int *flags)
1510 int n = 0;
1511 struct perf_event *event;
1513 if (group->pmu->task_ctx_nr == perf_hw_context) {
1514 if (n >= max_count)
1515 return -1;
1516 ctrs[n] = group;
1517 flags[n] = group->hw.event_base;
1518 events[n++] = group->hw.config;
1520 for_each_sibling_event(event, group) {
1521 if (event->pmu->task_ctx_nr == perf_hw_context &&
1522 event->state != PERF_EVENT_STATE_OFF) {
1523 if (n >= max_count)
1524 return -1;
1525 ctrs[n] = event;
1526 flags[n] = event->hw.event_base;
1527 events[n++] = event->hw.config;
1530 return n;
1534 * Add an event to the PMU.
1535 * If all events are not already frozen, then we disable and
1536 * re-enable the PMU in order to get hw_perf_enable to do the
1537 * actual work of reconfiguring the PMU.
1539 static int power_pmu_add(struct perf_event *event, int ef_flags)
1541 struct cpu_hw_events *cpuhw;
1542 unsigned long flags;
1543 int n0;
1544 int ret = -EAGAIN;
1546 local_irq_save(flags);
1547 perf_pmu_disable(event->pmu);
1550 * Add the event to the list (if there is room)
1551 * and check whether the total set is still feasible.
1553 cpuhw = this_cpu_ptr(&cpu_hw_events);
1554 n0 = cpuhw->n_events;
1555 if (n0 >= ppmu->n_counter)
1556 goto out;
1557 cpuhw->event[n0] = event;
1558 cpuhw->events[n0] = event->hw.config;
1559 cpuhw->flags[n0] = event->hw.event_base;
1562 * This event may have been disabled/stopped in record_and_restart()
1563 * because we exceeded the ->event_limit. If re-starting the event,
1564 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1565 * notification is re-enabled.
1567 if (!(ef_flags & PERF_EF_START))
1568 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1569 else
1570 event->hw.state = 0;
1573 * If group events scheduling transaction was started,
1574 * skip the schedulability test here, it will be performed
1575 * at commit time(->commit_txn) as a whole
1577 if (cpuhw->txn_flags & PERF_PMU_TXN_ADD)
1578 goto nocheck;
1580 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1581 goto out;
1582 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1583 goto out;
1584 event->hw.config = cpuhw->events[n0];
1586 nocheck:
1587 ebb_event_add(event);
1589 ++cpuhw->n_events;
1590 ++cpuhw->n_added;
1592 ret = 0;
1593 out:
1594 if (has_branch_stack(event)) {
1595 u64 bhrb_filter = -1;
1597 if (ppmu->bhrb_filter_map)
1598 bhrb_filter = ppmu->bhrb_filter_map(
1599 event->attr.branch_sample_type);
1601 if (bhrb_filter != -1) {
1602 cpuhw->bhrb_filter = bhrb_filter;
1603 power_pmu_bhrb_enable(event);
1607 perf_pmu_enable(event->pmu);
1608 local_irq_restore(flags);
1609 return ret;
1613 * Remove an event from the PMU.
1615 static void power_pmu_del(struct perf_event *event, int ef_flags)
1617 struct cpu_hw_events *cpuhw;
1618 long i;
1619 unsigned long flags;
1621 local_irq_save(flags);
1622 perf_pmu_disable(event->pmu);
1624 power_pmu_read(event);
1626 cpuhw = this_cpu_ptr(&cpu_hw_events);
1627 for (i = 0; i < cpuhw->n_events; ++i) {
1628 if (event == cpuhw->event[i]) {
1629 while (++i < cpuhw->n_events) {
1630 cpuhw->event[i-1] = cpuhw->event[i];
1631 cpuhw->events[i-1] = cpuhw->events[i];
1632 cpuhw->flags[i-1] = cpuhw->flags[i];
1634 --cpuhw->n_events;
1635 ppmu->disable_pmc(event->hw.idx - 1, &cpuhw->mmcr);
1636 if (event->hw.idx) {
1637 write_pmc(event->hw.idx, 0);
1638 event->hw.idx = 0;
1640 perf_event_update_userpage(event);
1641 break;
1644 for (i = 0; i < cpuhw->n_limited; ++i)
1645 if (event == cpuhw->limited_counter[i])
1646 break;
1647 if (i < cpuhw->n_limited) {
1648 while (++i < cpuhw->n_limited) {
1649 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
1650 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1652 --cpuhw->n_limited;
1654 if (cpuhw->n_events == 0) {
1655 /* disable exceptions if no events are running */
1656 cpuhw->mmcr.mmcr0 &= ~(MMCR0_PMXE | MMCR0_FCECE);
1659 if (has_branch_stack(event))
1660 power_pmu_bhrb_disable(event);
1662 perf_pmu_enable(event->pmu);
1663 local_irq_restore(flags);
1667 * POWER-PMU does not support disabling individual counters, hence
1668 * program their cycle counter to their max value and ignore the interrupts.
1671 static void power_pmu_start(struct perf_event *event, int ef_flags)
1673 unsigned long flags;
1674 s64 left;
1675 unsigned long val;
1677 if (!event->hw.idx || !event->hw.sample_period)
1678 return;
1680 if (!(event->hw.state & PERF_HES_STOPPED))
1681 return;
1683 if (ef_flags & PERF_EF_RELOAD)
1684 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1686 local_irq_save(flags);
1687 perf_pmu_disable(event->pmu);
1689 event->hw.state = 0;
1690 left = local64_read(&event->hw.period_left);
1692 val = 0;
1693 if (left < 0x80000000L)
1694 val = 0x80000000L - left;
1696 write_pmc(event->hw.idx, val);
1698 perf_event_update_userpage(event);
1699 perf_pmu_enable(event->pmu);
1700 local_irq_restore(flags);
1703 static void power_pmu_stop(struct perf_event *event, int ef_flags)
1705 unsigned long flags;
1707 if (!event->hw.idx || !event->hw.sample_period)
1708 return;
1710 if (event->hw.state & PERF_HES_STOPPED)
1711 return;
1713 local_irq_save(flags);
1714 perf_pmu_disable(event->pmu);
1716 power_pmu_read(event);
1717 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1718 write_pmc(event->hw.idx, 0);
1720 perf_event_update_userpage(event);
1721 perf_pmu_enable(event->pmu);
1722 local_irq_restore(flags);
1726 * Start group events scheduling transaction
1727 * Set the flag to make pmu::enable() not perform the
1728 * schedulability test, it will be performed at commit time
1730 * We only support PERF_PMU_TXN_ADD transactions. Save the
1731 * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
1732 * transactions.
1734 static void power_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
1736 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1738 WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
1740 cpuhw->txn_flags = txn_flags;
1741 if (txn_flags & ~PERF_PMU_TXN_ADD)
1742 return;
1744 perf_pmu_disable(pmu);
1745 cpuhw->n_txn_start = cpuhw->n_events;
1749 * Stop group events scheduling transaction
1750 * Clear the flag and pmu::enable() will perform the
1751 * schedulability test.
1753 static void power_pmu_cancel_txn(struct pmu *pmu)
1755 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1756 unsigned int txn_flags;
1758 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1760 txn_flags = cpuhw->txn_flags;
1761 cpuhw->txn_flags = 0;
1762 if (txn_flags & ~PERF_PMU_TXN_ADD)
1763 return;
1765 perf_pmu_enable(pmu);
1769 * Commit group events scheduling transaction
1770 * Perform the group schedulability test as a whole
1771 * Return 0 if success
1773 static int power_pmu_commit_txn(struct pmu *pmu)
1775 struct cpu_hw_events *cpuhw;
1776 long i, n;
1778 if (!ppmu)
1779 return -EAGAIN;
1781 cpuhw = this_cpu_ptr(&cpu_hw_events);
1782 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1784 if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
1785 cpuhw->txn_flags = 0;
1786 return 0;
1789 n = cpuhw->n_events;
1790 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1791 return -EAGAIN;
1792 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1793 if (i < 0)
1794 return -EAGAIN;
1796 for (i = cpuhw->n_txn_start; i < n; ++i)
1797 cpuhw->event[i]->hw.config = cpuhw->events[i];
1799 cpuhw->txn_flags = 0;
1800 perf_pmu_enable(pmu);
1801 return 0;
1805 * Return 1 if we might be able to put event on a limited PMC,
1806 * or 0 if not.
1807 * An event can only go on a limited PMC if it counts something
1808 * that a limited PMC can count, doesn't require interrupts, and
1809 * doesn't exclude any processor mode.
1811 static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1812 unsigned int flags)
1814 int n;
1815 u64 alt[MAX_EVENT_ALTERNATIVES];
1817 if (event->attr.exclude_user
1818 || event->attr.exclude_kernel
1819 || event->attr.exclude_hv
1820 || event->attr.sample_period)
1821 return 0;
1823 if (ppmu->limited_pmc_event(ev))
1824 return 1;
1827 * The requested event_id isn't on a limited PMC already;
1828 * see if any alternative code goes on a limited PMC.
1830 if (!ppmu->get_alternatives)
1831 return 0;
1833 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1834 n = ppmu->get_alternatives(ev, flags, alt);
1836 return n > 0;
1840 * Find an alternative event_id that goes on a normal PMC, if possible,
1841 * and return the event_id code, or 0 if there is no such alternative.
1842 * (Note: event_id code 0 is "don't count" on all machines.)
1844 static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1846 u64 alt[MAX_EVENT_ALTERNATIVES];
1847 int n;
1849 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1850 n = ppmu->get_alternatives(ev, flags, alt);
1851 if (!n)
1852 return 0;
1853 return alt[0];
1856 /* Number of perf_events counting hardware events */
1857 static atomic_t num_events;
1858 /* Used to avoid races in calling reserve/release_pmc_hardware */
1859 static DEFINE_MUTEX(pmc_reserve_mutex);
1862 * Release the PMU if this is the last perf_event.
1864 static void hw_perf_event_destroy(struct perf_event *event)
1866 if (!atomic_add_unless(&num_events, -1, 1)) {
1867 mutex_lock(&pmc_reserve_mutex);
1868 if (atomic_dec_return(&num_events) == 0)
1869 release_pmc_hardware();
1870 mutex_unlock(&pmc_reserve_mutex);
1875 * Translate a generic cache event_id config to a raw event_id code.
1877 static int hw_perf_cache_event(u64 config, u64 *eventp)
1879 unsigned long type, op, result;
1880 u64 ev;
1882 if (!ppmu->cache_events)
1883 return -EINVAL;
1885 /* unpack config */
1886 type = config & 0xff;
1887 op = (config >> 8) & 0xff;
1888 result = (config >> 16) & 0xff;
1890 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1891 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1892 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1893 return -EINVAL;
1895 ev = (*ppmu->cache_events)[type][op][result];
1896 if (ev == 0)
1897 return -EOPNOTSUPP;
1898 if (ev == -1)
1899 return -EINVAL;
1900 *eventp = ev;
1901 return 0;
1904 static bool is_event_blacklisted(u64 ev)
1906 int i;
1908 for (i=0; i < ppmu->n_blacklist_ev; i++) {
1909 if (ppmu->blacklist_ev[i] == ev)
1910 return true;
1913 return false;
1916 static int power_pmu_event_init(struct perf_event *event)
1918 u64 ev;
1919 unsigned long flags, irq_flags;
1920 struct perf_event *ctrs[MAX_HWEVENTS];
1921 u64 events[MAX_HWEVENTS];
1922 unsigned int cflags[MAX_HWEVENTS];
1923 int n;
1924 int err;
1925 struct cpu_hw_events *cpuhw;
1927 if (!ppmu)
1928 return -ENOENT;
1930 if (has_branch_stack(event)) {
1931 /* PMU has BHRB enabled */
1932 if (!(ppmu->flags & PPMU_ARCH_207S))
1933 return -EOPNOTSUPP;
1936 switch (event->attr.type) {
1937 case PERF_TYPE_HARDWARE:
1938 ev = event->attr.config;
1939 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1940 return -EOPNOTSUPP;
1942 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1943 return -EINVAL;
1944 ev = ppmu->generic_events[ev];
1945 break;
1946 case PERF_TYPE_HW_CACHE:
1947 err = hw_perf_cache_event(event->attr.config, &ev);
1948 if (err)
1949 return err;
1951 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1952 return -EINVAL;
1953 break;
1954 case PERF_TYPE_RAW:
1955 ev = event->attr.config;
1957 if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1958 return -EINVAL;
1959 break;
1960 default:
1961 return -ENOENT;
1964 event->hw.config_base = ev;
1965 event->hw.idx = 0;
1968 * If we are not running on a hypervisor, force the
1969 * exclude_hv bit to 0 so that we don't care what
1970 * the user set it to.
1972 if (!firmware_has_feature(FW_FEATURE_LPAR))
1973 event->attr.exclude_hv = 0;
1976 * If this is a per-task event, then we can use
1977 * PM_RUN_* events interchangeably with their non RUN_*
1978 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1979 * XXX we should check if the task is an idle task.
1981 flags = 0;
1982 if (event->attach_state & PERF_ATTACH_TASK)
1983 flags |= PPMU_ONLY_COUNT_RUN;
1986 * If this machine has limited events, check whether this
1987 * event_id could go on a limited event.
1989 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1990 if (can_go_on_limited_pmc(event, ev, flags)) {
1991 flags |= PPMU_LIMITED_PMC_OK;
1992 } else if (ppmu->limited_pmc_event(ev)) {
1994 * The requested event_id is on a limited PMC,
1995 * but we can't use a limited PMC; see if any
1996 * alternative goes on a normal PMC.
1998 ev = normal_pmc_alternative(ev, flags);
1999 if (!ev)
2000 return -EINVAL;
2004 /* Extra checks for EBB */
2005 err = ebb_event_check(event);
2006 if (err)
2007 return err;
2010 * If this is in a group, check if it can go on with all the
2011 * other hardware events in the group. We assume the event
2012 * hasn't been linked into its leader's sibling list at this point.
2014 n = 0;
2015 if (event->group_leader != event) {
2016 n = collect_events(event->group_leader, ppmu->n_counter - 1,
2017 ctrs, events, cflags);
2018 if (n < 0)
2019 return -EINVAL;
2021 events[n] = ev;
2022 ctrs[n] = event;
2023 cflags[n] = flags;
2024 if (check_excludes(ctrs, cflags, n, 1))
2025 return -EINVAL;
2027 local_irq_save(irq_flags);
2028 cpuhw = this_cpu_ptr(&cpu_hw_events);
2030 err = power_check_constraints(cpuhw, events, cflags, n + 1);
2032 if (has_branch_stack(event)) {
2033 u64 bhrb_filter = -1;
2035 if (ppmu->bhrb_filter_map)
2036 bhrb_filter = ppmu->bhrb_filter_map(
2037 event->attr.branch_sample_type);
2039 if (bhrb_filter == -1) {
2040 local_irq_restore(irq_flags);
2041 return -EOPNOTSUPP;
2043 cpuhw->bhrb_filter = bhrb_filter;
2046 local_irq_restore(irq_flags);
2047 if (err)
2048 return -EINVAL;
2050 event->hw.config = events[n];
2051 event->hw.event_base = cflags[n];
2052 event->hw.last_period = event->hw.sample_period;
2053 local64_set(&event->hw.period_left, event->hw.last_period);
2056 * For EBB events we just context switch the PMC value, we don't do any
2057 * of the sample_period logic. We use hw.prev_count for this.
2059 if (is_ebb_event(event))
2060 local64_set(&event->hw.prev_count, 0);
2063 * See if we need to reserve the PMU.
2064 * If no events are currently in use, then we have to take a
2065 * mutex to ensure that we don't race with another task doing
2066 * reserve_pmc_hardware or release_pmc_hardware.
2068 err = 0;
2069 if (!atomic_inc_not_zero(&num_events)) {
2070 mutex_lock(&pmc_reserve_mutex);
2071 if (atomic_read(&num_events) == 0 &&
2072 reserve_pmc_hardware(perf_event_interrupt))
2073 err = -EBUSY;
2074 else
2075 atomic_inc(&num_events);
2076 mutex_unlock(&pmc_reserve_mutex);
2078 event->destroy = hw_perf_event_destroy;
2080 return err;
2083 static int power_pmu_event_idx(struct perf_event *event)
2085 return event->hw.idx;
2088 ssize_t power_events_sysfs_show(struct device *dev,
2089 struct device_attribute *attr, char *page)
2091 struct perf_pmu_events_attr *pmu_attr;
2093 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
2095 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
2098 static struct pmu power_pmu = {
2099 .pmu_enable = power_pmu_enable,
2100 .pmu_disable = power_pmu_disable,
2101 .event_init = power_pmu_event_init,
2102 .add = power_pmu_add,
2103 .del = power_pmu_del,
2104 .start = power_pmu_start,
2105 .stop = power_pmu_stop,
2106 .read = power_pmu_read,
2107 .start_txn = power_pmu_start_txn,
2108 .cancel_txn = power_pmu_cancel_txn,
2109 .commit_txn = power_pmu_commit_txn,
2110 .event_idx = power_pmu_event_idx,
2111 .sched_task = power_pmu_sched_task,
2114 #define PERF_SAMPLE_ADDR_TYPE (PERF_SAMPLE_ADDR | \
2115 PERF_SAMPLE_PHYS_ADDR | \
2116 PERF_SAMPLE_DATA_PAGE_SIZE)
2118 * A counter has overflowed; update its count and record
2119 * things if requested. Note that interrupts are hard-disabled
2120 * here so there is no possibility of being interrupted.
2122 static void record_and_restart(struct perf_event *event, unsigned long val,
2123 struct pt_regs *regs)
2125 u64 period = event->hw.sample_period;
2126 s64 prev, delta, left;
2127 int record = 0;
2129 if (event->hw.state & PERF_HES_STOPPED) {
2130 write_pmc(event->hw.idx, 0);
2131 return;
2134 /* we don't have to worry about interrupts here */
2135 prev = local64_read(&event->hw.prev_count);
2136 delta = check_and_compute_delta(prev, val);
2137 local64_add(delta, &event->count);
2140 * See if the total period for this event has expired,
2141 * and update for the next period.
2143 val = 0;
2144 left = local64_read(&event->hw.period_left) - delta;
2145 if (delta == 0)
2146 left++;
2147 if (period) {
2148 if (left <= 0) {
2149 left += period;
2150 if (left <= 0)
2151 left = period;
2152 record = siar_valid(regs);
2153 event->hw.last_period = event->hw.sample_period;
2155 if (left < 0x80000000LL)
2156 val = 0x80000000LL - left;
2159 write_pmc(event->hw.idx, val);
2160 local64_set(&event->hw.prev_count, val);
2161 local64_set(&event->hw.period_left, left);
2162 perf_event_update_userpage(event);
2165 * Due to hardware limitation, sometimes SIAR could sample a kernel
2166 * address even when freeze on supervisor state (kernel) is set in
2167 * MMCR2. Check attr.exclude_kernel and address to drop the sample in
2168 * these cases.
2170 if (event->attr.exclude_kernel && record)
2171 if (is_kernel_addr(mfspr(SPRN_SIAR)))
2172 record = 0;
2175 * Finally record data if requested.
2177 if (record) {
2178 struct perf_sample_data data;
2180 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
2182 if (event->attr.sample_type & PERF_SAMPLE_ADDR_TYPE)
2183 perf_get_data_addr(event, regs, &data.addr);
2185 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
2186 struct cpu_hw_events *cpuhw;
2187 cpuhw = this_cpu_ptr(&cpu_hw_events);
2188 power_pmu_bhrb_read(event, cpuhw);
2189 data.br_stack = &cpuhw->bhrb_stack;
2192 if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
2193 ppmu->get_mem_data_src)
2194 ppmu->get_mem_data_src(&data.data_src, ppmu->flags, regs);
2196 if (event->attr.sample_type & PERF_SAMPLE_WEIGHT &&
2197 ppmu->get_mem_weight)
2198 ppmu->get_mem_weight(&data.weight);
2200 if (perf_event_overflow(event, &data, regs))
2201 power_pmu_stop(event, 0);
2202 } else if (period) {
2203 /* Account for interrupt in case of invalid SIAR */
2204 if (perf_event_account_interrupt(event))
2205 power_pmu_stop(event, 0);
2210 * Called from generic code to get the misc flags (i.e. processor mode)
2211 * for an event_id.
2213 unsigned long perf_misc_flags(struct pt_regs *regs)
2215 u32 flags = perf_get_misc_flags(regs);
2217 if (flags)
2218 return flags;
2219 return user_mode(regs) ? PERF_RECORD_MISC_USER :
2220 PERF_RECORD_MISC_KERNEL;
2224 * Called from generic code to get the instruction pointer
2225 * for an event_id.
2227 unsigned long perf_instruction_pointer(struct pt_regs *regs)
2229 bool use_siar = regs_use_siar(regs);
2230 unsigned long siar = mfspr(SPRN_SIAR);
2232 if (ppmu->flags & PPMU_P10_DD1) {
2233 if (siar)
2234 return siar;
2235 else
2236 return regs->nip;
2237 } else if (use_siar && siar_valid(regs))
2238 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
2239 else if (use_siar)
2240 return 0; // no valid instruction pointer
2241 else
2242 return regs->nip;
2245 static bool pmc_overflow_power7(unsigned long val)
2248 * Events on POWER7 can roll back if a speculative event doesn't
2249 * eventually complete. Unfortunately in some rare cases they will
2250 * raise a performance monitor exception. We need to catch this to
2251 * ensure we reset the PMC. In all cases the PMC will be 256 or less
2252 * cycles from overflow.
2254 * We only do this if the first pass fails to find any overflowing
2255 * PMCs because a user might set a period of less than 256 and we
2256 * don't want to mistakenly reset them.
2258 if ((0x80000000 - val) <= 256)
2259 return true;
2261 return false;
2264 static bool pmc_overflow(unsigned long val)
2266 if ((int)val < 0)
2267 return true;
2269 return false;
2273 * Performance monitor interrupt stuff
2275 static void __perf_event_interrupt(struct pt_regs *regs)
2277 int i, j;
2278 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
2279 struct perf_event *event;
2280 unsigned long val[8];
2281 int found, active;
2282 int nmi;
2284 if (cpuhw->n_limited)
2285 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
2286 mfspr(SPRN_PMC6));
2288 perf_read_regs(regs);
2291 * If perf interrupts hit in a local_irq_disable (soft-masked) region,
2292 * we consider them as NMIs. This is required to prevent hash faults on
2293 * user addresses when reading callchains. See the NMI test in
2294 * do_hash_page.
2296 nmi = perf_intr_is_nmi(regs);
2297 if (nmi)
2298 nmi_enter();
2299 else
2300 irq_enter();
2302 /* Read all the PMCs since we'll need them a bunch of times */
2303 for (i = 0; i < ppmu->n_counter; ++i)
2304 val[i] = read_pmc(i + 1);
2306 /* Try to find what caused the IRQ */
2307 found = 0;
2308 for (i = 0; i < ppmu->n_counter; ++i) {
2309 if (!pmc_overflow(val[i]))
2310 continue;
2311 if (is_limited_pmc(i + 1))
2312 continue; /* these won't generate IRQs */
2314 * We've found one that's overflowed. For active
2315 * counters we need to log this. For inactive
2316 * counters, we need to reset it anyway
2318 found = 1;
2319 active = 0;
2320 for (j = 0; j < cpuhw->n_events; ++j) {
2321 event = cpuhw->event[j];
2322 if (event->hw.idx == (i + 1)) {
2323 active = 1;
2324 record_and_restart(event, val[i], regs);
2325 break;
2328 if (!active)
2329 /* reset non active counters that have overflowed */
2330 write_pmc(i + 1, 0);
2332 if (!found && pvr_version_is(PVR_POWER7)) {
2333 /* check active counters for special buggy p7 overflow */
2334 for (i = 0; i < cpuhw->n_events; ++i) {
2335 event = cpuhw->event[i];
2336 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
2337 continue;
2338 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2339 /* event has overflowed in a buggy way*/
2340 found = 1;
2341 record_and_restart(event,
2342 val[event->hw.idx - 1],
2343 regs);
2347 if (!found && !nmi && printk_ratelimit())
2348 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
2351 * Reset MMCR0 to its normal value. This will set PMXE and
2352 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
2353 * and thus allow interrupts to occur again.
2354 * XXX might want to use MSR.PM to keep the events frozen until
2355 * we get back out of this interrupt.
2357 write_mmcr0(cpuhw, cpuhw->mmcr.mmcr0);
2359 if (nmi)
2360 nmi_exit();
2361 else
2362 irq_exit();
2365 static void perf_event_interrupt(struct pt_regs *regs)
2367 u64 start_clock = sched_clock();
2369 __perf_event_interrupt(regs);
2370 perf_sample_event_took(sched_clock() - start_clock);
2373 static int power_pmu_prepare_cpu(unsigned int cpu)
2375 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2377 if (ppmu) {
2378 memset(cpuhw, 0, sizeof(*cpuhw));
2379 cpuhw->mmcr.mmcr0 = MMCR0_FC;
2381 return 0;
2384 int register_power_pmu(struct power_pmu *pmu)
2386 if (ppmu)
2387 return -EBUSY; /* something's already registered */
2389 ppmu = pmu;
2390 pr_info("%s performance monitor hardware support registered\n",
2391 pmu->name);
2393 power_pmu.attr_groups = ppmu->attr_groups;
2394 power_pmu.capabilities |= (ppmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS);
2396 #ifdef MSR_HV
2398 * Use FCHV to ignore kernel events if MSR.HV is set.
2400 if (mfmsr() & MSR_HV)
2401 freeze_events_kernel = MMCR0_FCHV;
2402 #endif /* CONFIG_PPC64 */
2404 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
2405 cpuhp_setup_state(CPUHP_PERF_POWER, "perf/powerpc:prepare",
2406 power_pmu_prepare_cpu, NULL);
2407 return 0;
2410 #ifdef CONFIG_PPC64
2411 static int __init init_ppc64_pmu(void)
2413 /* run through all the pmu drivers one at a time */
2414 if (!init_power5_pmu())
2415 return 0;
2416 else if (!init_power5p_pmu())
2417 return 0;
2418 else if (!init_power6_pmu())
2419 return 0;
2420 else if (!init_power7_pmu())
2421 return 0;
2422 else if (!init_power8_pmu())
2423 return 0;
2424 else if (!init_power9_pmu())
2425 return 0;
2426 else if (!init_power10_pmu())
2427 return 0;
2428 else if (!init_ppc970_pmu())
2429 return 0;
2430 else
2431 return init_generic_compat_pmu();
2433 early_initcall(init_ppc64_pmu);
2434 #endif