xref: /openbmc/linux/arch/powerpc/perf/core-book3s.c (revision da97e184)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f2699491SMichael Ellerman /*
3f2699491SMichael Ellerman  * Performance event support - powerpc architecture code
4f2699491SMichael Ellerman  *
5f2699491SMichael Ellerman  * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
6f2699491SMichael Ellerman  */
7f2699491SMichael Ellerman #include <linux/kernel.h>
8f2699491SMichael Ellerman #include <linux/sched.h>
90c9108b0SRavi Bangoria #include <linux/sched/clock.h>
10f2699491SMichael Ellerman #include <linux/perf_event.h>
11f2699491SMichael Ellerman #include <linux/percpu.h>
12f2699491SMichael Ellerman #include <linux/hardirq.h>
1369123184SMichael Neuling #include <linux/uaccess.h>
14f2699491SMichael Ellerman #include <asm/reg.h>
15f2699491SMichael Ellerman #include <asm/pmc.h>
16f2699491SMichael Ellerman #include <asm/machdep.h>
17f2699491SMichael Ellerman #include <asm/firmware.h>
18f2699491SMichael Ellerman #include <asm/ptrace.h>
1969123184SMichael Neuling #include <asm/code-patching.h>
20f2699491SMichael Ellerman 
21708597daSMadhavan Srinivasan #ifdef CONFIG_PPC64
22708597daSMadhavan Srinivasan #include "internal.h"
23708597daSMadhavan Srinivasan #endif
24708597daSMadhavan Srinivasan 
253925f46bSAnshuman Khandual #define BHRB_MAX_ENTRIES	32
263925f46bSAnshuman Khandual #define BHRB_TARGET		0x0000000000000002
273925f46bSAnshuman Khandual #define BHRB_PREDICTION		0x0000000000000001
28b0d436c7SAnton Blanchard #define BHRB_EA			0xFFFFFFFFFFFFFFFCUL
293925f46bSAnshuman Khandual 
30f2699491SMichael Ellerman struct cpu_hw_events {
31f2699491SMichael Ellerman 	int n_events;
32f2699491SMichael Ellerman 	int n_percpu;
33f2699491SMichael Ellerman 	int disabled;
34f2699491SMichael Ellerman 	int n_added;
35f2699491SMichael Ellerman 	int n_limited;
36f2699491SMichael Ellerman 	u8  pmcs_enabled;
37f2699491SMichael Ellerman 	struct perf_event *event[MAX_HWEVENTS];
38f2699491SMichael Ellerman 	u64 events[MAX_HWEVENTS];
39f2699491SMichael Ellerman 	unsigned int flags[MAX_HWEVENTS];
409de5cb0fSMichael Ellerman 	/*
419de5cb0fSMichael Ellerman 	 * The order of the MMCR array is:
429de5cb0fSMichael Ellerman 	 *  - 64-bit, MMCR0, MMCR1, MMCRA, MMCR2
439de5cb0fSMichael Ellerman 	 *  - 32-bit, MMCR0, MMCR1, MMCR2
449de5cb0fSMichael Ellerman 	 */
459de5cb0fSMichael Ellerman 	unsigned long mmcr[4];
46f2699491SMichael Ellerman 	struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
47f2699491SMichael Ellerman 	u8  limited_hwidx[MAX_LIMITED_HWCOUNTERS];
48f2699491SMichael Ellerman 	u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
49f2699491SMichael Ellerman 	unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
50f2699491SMichael Ellerman 	unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
51f2699491SMichael Ellerman 
52fbbe0701SSukadev Bhattiprolu 	unsigned int txn_flags;
53f2699491SMichael Ellerman 	int n_txn_start;
543925f46bSAnshuman Khandual 
553925f46bSAnshuman Khandual 	/* BHRB bits */
563925f46bSAnshuman Khandual 	u64				bhrb_filter;	/* BHRB HW branch filter */
57f0322f7fSAnshuman Khandual 	unsigned int			bhrb_users;
583925f46bSAnshuman Khandual 	void				*bhrb_context;
593925f46bSAnshuman Khandual 	struct	perf_branch_stack	bhrb_stack;
603925f46bSAnshuman Khandual 	struct	perf_branch_entry	bhrb_entries[BHRB_MAX_ENTRIES];
61356d8ce3SMadhavan Srinivasan 	u64				ic_init;
62f2699491SMichael Ellerman };
633925f46bSAnshuman Khandual 
64e51df2c1SAnton Blanchard static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
65f2699491SMichael Ellerman 
66e51df2c1SAnton Blanchard static struct power_pmu *ppmu;
67f2699491SMichael Ellerman 
68f2699491SMichael Ellerman /*
69f2699491SMichael Ellerman  * Normally, to ignore kernel events we set the FCS (freeze counters
70f2699491SMichael Ellerman  * in supervisor mode) bit in MMCR0, but if the kernel runs with the
71f2699491SMichael Ellerman  * hypervisor bit set in the MSR, or if we are running on a processor
72f2699491SMichael Ellerman  * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
73f2699491SMichael Ellerman  * then we need to use the FCHV bit to ignore kernel events.
74f2699491SMichael Ellerman  */
75f2699491SMichael Ellerman static unsigned int freeze_events_kernel = MMCR0_FCS;
76f2699491SMichael Ellerman 
77f2699491SMichael Ellerman /*
78f2699491SMichael Ellerman  * 32-bit doesn't have MMCRA but does have an MMCR2,
79f2699491SMichael Ellerman  * and a few other names are different.
80f2699491SMichael Ellerman  */
81f2699491SMichael Ellerman #ifdef CONFIG_PPC32
82f2699491SMichael Ellerman 
83f2699491SMichael Ellerman #define MMCR0_FCHV		0
84f2699491SMichael Ellerman #define MMCR0_PMCjCE		MMCR0_PMCnCE
857a7a41f9SMichael Ellerman #define MMCR0_FC56		0
86378a6ee9SMichael Ellerman #define MMCR0_PMAO		0
87330a1eb7SMichael Ellerman #define MMCR0_EBE		0
8876cb8a78SMichael Ellerman #define MMCR0_BHRBA		0
89330a1eb7SMichael Ellerman #define MMCR0_PMCC		0
90330a1eb7SMichael Ellerman #define MMCR0_PMCC_U6		0
91f2699491SMichael Ellerman 
92f2699491SMichael Ellerman #define SPRN_MMCRA		SPRN_MMCR2
93f2699491SMichael Ellerman #define MMCRA_SAMPLE_ENABLE	0
94f2699491SMichael Ellerman 
95f2699491SMichael Ellerman static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
96f2699491SMichael Ellerman {
97f2699491SMichael Ellerman 	return 0;
98f2699491SMichael Ellerman }
99da97e184SJoel Fernandes (Google) static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp) { }
100f2699491SMichael Ellerman static inline u32 perf_get_misc_flags(struct pt_regs *regs)
101f2699491SMichael Ellerman {
102f2699491SMichael Ellerman 	return 0;
103f2699491SMichael Ellerman }
10475382aa7SAnton Blanchard static inline void perf_read_regs(struct pt_regs *regs)
10575382aa7SAnton Blanchard {
10675382aa7SAnton Blanchard 	regs->result = 0;
10775382aa7SAnton Blanchard }
108f2699491SMichael Ellerman static inline int perf_intr_is_nmi(struct pt_regs *regs)
109f2699491SMichael Ellerman {
110f2699491SMichael Ellerman 	return 0;
111f2699491SMichael Ellerman }
112f2699491SMichael Ellerman 
113e6878835Ssukadev@linux.vnet.ibm.com static inline int siar_valid(struct pt_regs *regs)
114e6878835Ssukadev@linux.vnet.ibm.com {
115e6878835Ssukadev@linux.vnet.ibm.com 	return 1;
116e6878835Ssukadev@linux.vnet.ibm.com }
117e6878835Ssukadev@linux.vnet.ibm.com 
118330a1eb7SMichael Ellerman static bool is_ebb_event(struct perf_event *event) { return false; }
119330a1eb7SMichael Ellerman static int ebb_event_check(struct perf_event *event) { return 0; }
120330a1eb7SMichael Ellerman static void ebb_event_add(struct perf_event *event) { }
121330a1eb7SMichael Ellerman static void ebb_switch_out(unsigned long mmcr0) { }
1229de5cb0fSMichael Ellerman static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
123330a1eb7SMichael Ellerman {
1249de5cb0fSMichael Ellerman 	return cpuhw->mmcr[0];
125330a1eb7SMichael Ellerman }
126330a1eb7SMichael Ellerman 
127d52f2dc4SMichael Neuling static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
128d52f2dc4SMichael Neuling static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
129acba3c7eSPeter Zijlstra static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
130da97e184SJoel Fernandes (Google) static inline void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw) {}
131c2e37a26SMichael Ellerman static void pmao_restore_workaround(bool ebb) { }
132f2699491SMichael Ellerman #endif /* CONFIG_PPC32 */
133f2699491SMichael Ellerman 
134333804dcSMadhavan Srinivasan bool is_sier_available(void)
135333804dcSMadhavan Srinivasan {
136333804dcSMadhavan Srinivasan 	if (ppmu->flags & PPMU_HAS_SIER)
137333804dcSMadhavan Srinivasan 		return true;
138333804dcSMadhavan Srinivasan 
139333804dcSMadhavan Srinivasan 	return false;
140333804dcSMadhavan Srinivasan }
141333804dcSMadhavan Srinivasan 
14233904054SMichael Ellerman static bool regs_use_siar(struct pt_regs *regs)
14333904054SMichael Ellerman {
14472e349f1SAnton Blanchard 	/*
14572e349f1SAnton Blanchard 	 * When we take a performance monitor exception the regs are setup
14672e349f1SAnton Blanchard 	 * using perf_read_regs() which overloads some fields, in particular
14772e349f1SAnton Blanchard 	 * regs->result to tell us whether to use SIAR.
14872e349f1SAnton Blanchard 	 *
14972e349f1SAnton Blanchard 	 * However if the regs are from another exception, eg. a syscall, then
15072e349f1SAnton Blanchard 	 * they have not been setup using perf_read_regs() and so regs->result
15172e349f1SAnton Blanchard 	 * is something random.
15272e349f1SAnton Blanchard 	 */
15372e349f1SAnton Blanchard 	return ((TRAP(regs) == 0xf00) && regs->result);
15433904054SMichael Ellerman }
15533904054SMichael Ellerman 
156f2699491SMichael Ellerman /*
157f2699491SMichael Ellerman  * Things that are specific to 64-bit implementations.
158f2699491SMichael Ellerman  */
159f2699491SMichael Ellerman #ifdef CONFIG_PPC64
160f2699491SMichael Ellerman 
161f2699491SMichael Ellerman static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
162f2699491SMichael Ellerman {
163f2699491SMichael Ellerman 	unsigned long mmcra = regs->dsisr;
164f2699491SMichael Ellerman 
1657a786832SMichael Ellerman 	if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
166f2699491SMichael Ellerman 		unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
167f2699491SMichael Ellerman 		if (slot > 1)
168f2699491SMichael Ellerman 			return 4 * (slot - 1);
169f2699491SMichael Ellerman 	}
1707a786832SMichael Ellerman 
171f2699491SMichael Ellerman 	return 0;
172f2699491SMichael Ellerman }
173f2699491SMichael Ellerman 
174f2699491SMichael Ellerman /*
175f2699491SMichael Ellerman  * The user wants a data address recorded.
176f2699491SMichael Ellerman  * If we're not doing instruction sampling, give them the SDAR
177f2699491SMichael Ellerman  * (sampled data address).  If we are doing instruction sampling, then
178f2699491SMichael Ellerman  * only give them the SDAR if it corresponds to the instruction
17958a032c3SMichael Ellerman  * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
18058a032c3SMichael Ellerman  * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
181f2699491SMichael Ellerman  */
182da97e184SJoel Fernandes (Google) static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp)
183f2699491SMichael Ellerman {
184f2699491SMichael Ellerman 	unsigned long mmcra = regs->dsisr;
18558a032c3SMichael Ellerman 	bool sdar_valid;
18658a032c3SMichael Ellerman 
18758a032c3SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
18858a032c3SMichael Ellerman 		sdar_valid = regs->dar & SIER_SDAR_VALID;
18958a032c3SMichael Ellerman 	else {
190e6878835Ssukadev@linux.vnet.ibm.com 		unsigned long sdsync;
191e6878835Ssukadev@linux.vnet.ibm.com 
192e6878835Ssukadev@linux.vnet.ibm.com 		if (ppmu->flags & PPMU_SIAR_VALID)
193e6878835Ssukadev@linux.vnet.ibm.com 			sdsync = POWER7P_MMCRA_SDAR_VALID;
194e6878835Ssukadev@linux.vnet.ibm.com 		else if (ppmu->flags & PPMU_ALT_SIPR)
195e6878835Ssukadev@linux.vnet.ibm.com 			sdsync = POWER6_MMCRA_SDSYNC;
196f04d1080SMadhavan Srinivasan 		else if (ppmu->flags & PPMU_NO_SIAR)
197f04d1080SMadhavan Srinivasan 			sdsync = MMCRA_SAMPLE_ENABLE;
198e6878835Ssukadev@linux.vnet.ibm.com 		else
199e6878835Ssukadev@linux.vnet.ibm.com 			sdsync = MMCRA_SDSYNC;
200f2699491SMichael Ellerman 
20158a032c3SMichael Ellerman 		sdar_valid = mmcra & sdsync;
20258a032c3SMichael Ellerman 	}
20358a032c3SMichael Ellerman 
20458a032c3SMichael Ellerman 	if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
205f2699491SMichael Ellerman 		*addrp = mfspr(SPRN_SDAR);
206cd1231d7SMadhavan Srinivasan 
207da97e184SJoel Fernandes (Google) 	if (is_kernel_addr(mfspr(SPRN_SDAR)) && perf_allow_kernel(&event->attr) != 0)
208cd1231d7SMadhavan Srinivasan 		*addrp = 0;
209f2699491SMichael Ellerman }
210f2699491SMichael Ellerman 
2115682c460SMichael Ellerman static bool regs_sihv(struct pt_regs *regs)
21268b30bb9SAnton Blanchard {
21368b30bb9SAnton Blanchard 	unsigned long sihv = MMCRA_SIHV;
21468b30bb9SAnton Blanchard 
2158f61aa32SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
2168f61aa32SMichael Ellerman 		return !!(regs->dar & SIER_SIHV);
2178f61aa32SMichael Ellerman 
21868b30bb9SAnton Blanchard 	if (ppmu->flags & PPMU_ALT_SIPR)
21968b30bb9SAnton Blanchard 		sihv = POWER6_MMCRA_SIHV;
22068b30bb9SAnton Blanchard 
2215682c460SMichael Ellerman 	return !!(regs->dsisr & sihv);
22268b30bb9SAnton Blanchard }
22368b30bb9SAnton Blanchard 
2245682c460SMichael Ellerman static bool regs_sipr(struct pt_regs *regs)
22568b30bb9SAnton Blanchard {
22668b30bb9SAnton Blanchard 	unsigned long sipr = MMCRA_SIPR;
22768b30bb9SAnton Blanchard 
2288f61aa32SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
2298f61aa32SMichael Ellerman 		return !!(regs->dar & SIER_SIPR);
2308f61aa32SMichael Ellerman 
23168b30bb9SAnton Blanchard 	if (ppmu->flags & PPMU_ALT_SIPR)
23268b30bb9SAnton Blanchard 		sipr = POWER6_MMCRA_SIPR;
23368b30bb9SAnton Blanchard 
2345682c460SMichael Ellerman 	return !!(regs->dsisr & sipr);
23568b30bb9SAnton Blanchard }
23668b30bb9SAnton Blanchard 
2371ce447b9SBenjamin Herrenschmidt static inline u32 perf_flags_from_msr(struct pt_regs *regs)
2381ce447b9SBenjamin Herrenschmidt {
2391ce447b9SBenjamin Herrenschmidt 	if (regs->msr & MSR_PR)
2401ce447b9SBenjamin Herrenschmidt 		return PERF_RECORD_MISC_USER;
2411ce447b9SBenjamin Herrenschmidt 	if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
2421ce447b9SBenjamin Herrenschmidt 		return PERF_RECORD_MISC_HYPERVISOR;
2431ce447b9SBenjamin Herrenschmidt 	return PERF_RECORD_MISC_KERNEL;
2441ce447b9SBenjamin Herrenschmidt }
2451ce447b9SBenjamin Herrenschmidt 
246f2699491SMichael Ellerman static inline u32 perf_get_misc_flags(struct pt_regs *regs)
247f2699491SMichael Ellerman {
24833904054SMichael Ellerman 	bool use_siar = regs_use_siar(regs);
249f2699491SMichael Ellerman 
25075382aa7SAnton Blanchard 	if (!use_siar)
2511ce447b9SBenjamin Herrenschmidt 		return perf_flags_from_msr(regs);
2521ce447b9SBenjamin Herrenschmidt 
2531ce447b9SBenjamin Herrenschmidt 	/*
2541ce447b9SBenjamin Herrenschmidt 	 * If we don't have flags in MMCRA, rather than using
2551ce447b9SBenjamin Herrenschmidt 	 * the MSR, we intuit the flags from the address in
2561ce447b9SBenjamin Herrenschmidt 	 * SIAR which should give slightly more reliable
2571ce447b9SBenjamin Herrenschmidt 	 * results
2581ce447b9SBenjamin Herrenschmidt 	 */
259cbda6aa1SMichael Ellerman 	if (ppmu->flags & PPMU_NO_SIPR) {
2601ce447b9SBenjamin Herrenschmidt 		unsigned long siar = mfspr(SPRN_SIAR);
261a2391b35SMadhavan Srinivasan 		if (is_kernel_addr(siar))
2621ce447b9SBenjamin Herrenschmidt 			return PERF_RECORD_MISC_KERNEL;
2631ce447b9SBenjamin Herrenschmidt 		return PERF_RECORD_MISC_USER;
2641ce447b9SBenjamin Herrenschmidt 	}
265f2699491SMichael Ellerman 
266f2699491SMichael Ellerman 	/* PR has priority over HV, so order below is important */
2675682c460SMichael Ellerman 	if (regs_sipr(regs))
268f2699491SMichael Ellerman 		return PERF_RECORD_MISC_USER;
2695682c460SMichael Ellerman 
2705682c460SMichael Ellerman 	if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
271f2699491SMichael Ellerman 		return PERF_RECORD_MISC_HYPERVISOR;
2725682c460SMichael Ellerman 
273f2699491SMichael Ellerman 	return PERF_RECORD_MISC_KERNEL;
274f2699491SMichael Ellerman }
275f2699491SMichael Ellerman 
276f2699491SMichael Ellerman /*
277f2699491SMichael Ellerman  * Overload regs->dsisr to store MMCRA so we only need to read it once
278f2699491SMichael Ellerman  * on each interrupt.
2798f61aa32SMichael Ellerman  * Overload regs->dar to store SIER if we have it.
28075382aa7SAnton Blanchard  * Overload regs->result to specify whether we should use the MSR (result
28175382aa7SAnton Blanchard  * is zero) or the SIAR (result is non zero).
282f2699491SMichael Ellerman  */
283f2699491SMichael Ellerman static inline void perf_read_regs(struct pt_regs *regs)
284f2699491SMichael Ellerman {
28575382aa7SAnton Blanchard 	unsigned long mmcra = mfspr(SPRN_MMCRA);
28675382aa7SAnton Blanchard 	int marked = mmcra & MMCRA_SAMPLE_ENABLE;
28775382aa7SAnton Blanchard 	int use_siar;
28875382aa7SAnton Blanchard 
2895682c460SMichael Ellerman 	regs->dsisr = mmcra;
290860aad71SMichael Ellerman 
291cbda6aa1SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
2928f61aa32SMichael Ellerman 		regs->dar = mfspr(SPRN_SIER);
2938f61aa32SMichael Ellerman 
2948f61aa32SMichael Ellerman 	/*
2955c093efaSAnton Blanchard 	 * If this isn't a PMU exception (eg a software event) the SIAR is
2965c093efaSAnton Blanchard 	 * not valid. Use pt_regs.
2975c093efaSAnton Blanchard 	 *
2985c093efaSAnton Blanchard 	 * If it is a marked event use the SIAR.
2995c093efaSAnton Blanchard 	 *
3005c093efaSAnton Blanchard 	 * If the PMU doesn't update the SIAR for non marked events use
3015c093efaSAnton Blanchard 	 * pt_regs.
3025c093efaSAnton Blanchard 	 *
3035c093efaSAnton Blanchard 	 * If the PMU has HV/PR flags then check to see if they
3045c093efaSAnton Blanchard 	 * place the exception in userspace. If so, use pt_regs. In
3055c093efaSAnton Blanchard 	 * continuous sampling mode the SIAR and the PMU exception are
3065c093efaSAnton Blanchard 	 * not synchronised, so they may be many instructions apart.
3075c093efaSAnton Blanchard 	 * This can result in confusing backtraces. We still want
3085c093efaSAnton Blanchard 	 * hypervisor samples as well as samples in the kernel with
3095c093efaSAnton Blanchard 	 * interrupts off hence the userspace check.
3105c093efaSAnton Blanchard 	 */
31175382aa7SAnton Blanchard 	if (TRAP(regs) != 0xf00)
31275382aa7SAnton Blanchard 		use_siar = 0;
31327593d72SMadhavan Srinivasan 	else if ((ppmu->flags & PPMU_NO_SIAR))
31427593d72SMadhavan Srinivasan 		use_siar = 0;
3155c093efaSAnton Blanchard 	else if (marked)
3165c093efaSAnton Blanchard 		use_siar = 1;
3175c093efaSAnton Blanchard 	else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
3185c093efaSAnton Blanchard 		use_siar = 0;
319cbda6aa1SMichael Ellerman 	else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
32075382aa7SAnton Blanchard 		use_siar = 0;
32175382aa7SAnton Blanchard 	else
32275382aa7SAnton Blanchard 		use_siar = 1;
32375382aa7SAnton Blanchard 
324cbda6aa1SMichael Ellerman 	regs->result = use_siar;
325f2699491SMichael Ellerman }
326f2699491SMichael Ellerman 
327f2699491SMichael Ellerman /*
328f2699491SMichael Ellerman  * If interrupts were soft-disabled when a PMU interrupt occurs, treat
329f2699491SMichael Ellerman  * it as an NMI.
330f2699491SMichael Ellerman  */
331f2699491SMichael Ellerman static inline int perf_intr_is_nmi(struct pt_regs *regs)
332f2699491SMichael Ellerman {
33301417c6cSMadhavan Srinivasan 	return (regs->softe & IRQS_DISABLED);
334f2699491SMichael Ellerman }
335f2699491SMichael Ellerman 
336e6878835Ssukadev@linux.vnet.ibm.com /*
337e6878835Ssukadev@linux.vnet.ibm.com  * On processors like P7+ that have the SIAR-Valid bit, marked instructions
338e6878835Ssukadev@linux.vnet.ibm.com  * must be sampled only if the SIAR-valid bit is set.
339e6878835Ssukadev@linux.vnet.ibm.com  *
340e6878835Ssukadev@linux.vnet.ibm.com  * For unmarked instructions and for processors that don't have the SIAR-Valid
341e6878835Ssukadev@linux.vnet.ibm.com  * bit, assume that SIAR is valid.
342e6878835Ssukadev@linux.vnet.ibm.com  */
343e6878835Ssukadev@linux.vnet.ibm.com static inline int siar_valid(struct pt_regs *regs)
344e6878835Ssukadev@linux.vnet.ibm.com {
345e6878835Ssukadev@linux.vnet.ibm.com 	unsigned long mmcra = regs->dsisr;
346e6878835Ssukadev@linux.vnet.ibm.com 	int marked = mmcra & MMCRA_SAMPLE_ENABLE;
347e6878835Ssukadev@linux.vnet.ibm.com 
34858a032c3SMichael Ellerman 	if (marked) {
34958a032c3SMichael Ellerman 		if (ppmu->flags & PPMU_HAS_SIER)
35058a032c3SMichael Ellerman 			return regs->dar & SIER_SIAR_VALID;
35158a032c3SMichael Ellerman 
35258a032c3SMichael Ellerman 		if (ppmu->flags & PPMU_SIAR_VALID)
353e6878835Ssukadev@linux.vnet.ibm.com 			return mmcra & POWER7P_MMCRA_SIAR_VALID;
35458a032c3SMichael Ellerman 	}
355e6878835Ssukadev@linux.vnet.ibm.com 
356e6878835Ssukadev@linux.vnet.ibm.com 	return 1;
357e6878835Ssukadev@linux.vnet.ibm.com }
358e6878835Ssukadev@linux.vnet.ibm.com 
359d52f2dc4SMichael Neuling 
360d52f2dc4SMichael Neuling /* Reset all possible BHRB entries */
361d52f2dc4SMichael Neuling static void power_pmu_bhrb_reset(void)
362d52f2dc4SMichael Neuling {
363d52f2dc4SMichael Neuling 	asm volatile(PPC_CLRBHRB);
364d52f2dc4SMichael Neuling }
365d52f2dc4SMichael Neuling 
366d52f2dc4SMichael Neuling static void power_pmu_bhrb_enable(struct perf_event *event)
367d52f2dc4SMichael Neuling {
36869111bacSChristoph Lameter 	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
369d52f2dc4SMichael Neuling 
370d52f2dc4SMichael Neuling 	if (!ppmu->bhrb_nr)
371d52f2dc4SMichael Neuling 		return;
372d52f2dc4SMichael Neuling 
373d52f2dc4SMichael Neuling 	/* Clear BHRB if we changed task context to avoid data leaks */
374d52f2dc4SMichael Neuling 	if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
375d52f2dc4SMichael Neuling 		power_pmu_bhrb_reset();
376d52f2dc4SMichael Neuling 		cpuhw->bhrb_context = event->ctx;
377d52f2dc4SMichael Neuling 	}
378d52f2dc4SMichael Neuling 	cpuhw->bhrb_users++;
379acba3c7eSPeter Zijlstra 	perf_sched_cb_inc(event->ctx->pmu);
380d52f2dc4SMichael Neuling }
381d52f2dc4SMichael Neuling 
382d52f2dc4SMichael Neuling static void power_pmu_bhrb_disable(struct perf_event *event)
383d52f2dc4SMichael Neuling {
38469111bacSChristoph Lameter 	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
385d52f2dc4SMichael Neuling 
386d52f2dc4SMichael Neuling 	if (!ppmu->bhrb_nr)
387d52f2dc4SMichael Neuling 		return;
388d52f2dc4SMichael Neuling 
389f0322f7fSAnshuman Khandual 	WARN_ON_ONCE(!cpuhw->bhrb_users);
390d52f2dc4SMichael Neuling 	cpuhw->bhrb_users--;
391acba3c7eSPeter Zijlstra 	perf_sched_cb_dec(event->ctx->pmu);
392d52f2dc4SMichael Neuling 
393d52f2dc4SMichael Neuling 	if (!cpuhw->disabled && !cpuhw->bhrb_users) {
394d52f2dc4SMichael Neuling 		/* BHRB cannot be turned off when other
395d52f2dc4SMichael Neuling 		 * events are active on the PMU.
396d52f2dc4SMichael Neuling 		 */
397d52f2dc4SMichael Neuling 
398d52f2dc4SMichael Neuling 		/* avoid stale pointer */
399d52f2dc4SMichael Neuling 		cpuhw->bhrb_context = NULL;
400d52f2dc4SMichael Neuling 	}
401d52f2dc4SMichael Neuling }
402d52f2dc4SMichael Neuling 
403d52f2dc4SMichael Neuling /* Called from ctxsw to prevent one process's branch entries to
404d52f2dc4SMichael Neuling  * mingle with the other process's entries during context switch.
405d52f2dc4SMichael Neuling  */
406acba3c7eSPeter Zijlstra static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
407d52f2dc4SMichael Neuling {
408acba3c7eSPeter Zijlstra 	if (!ppmu->bhrb_nr)
409acba3c7eSPeter Zijlstra 		return;
410acba3c7eSPeter Zijlstra 
411acba3c7eSPeter Zijlstra 	if (sched_in)
412d52f2dc4SMichael Neuling 		power_pmu_bhrb_reset();
413d52f2dc4SMichael Neuling }
41469123184SMichael Neuling /* Calculate the to address for a branch */
41569123184SMichael Neuling static __u64 power_pmu_bhrb_to(u64 addr)
41669123184SMichael Neuling {
41769123184SMichael Neuling 	unsigned int instr;
41869123184SMichael Neuling 	int ret;
41969123184SMichael Neuling 	__u64 target;
42069123184SMichael Neuling 
421f41d84ddSRavi Bangoria 	if (is_kernel_addr(addr)) {
422f41d84ddSRavi Bangoria 		if (probe_kernel_read(&instr, (void *)addr, sizeof(instr)))
423f41d84ddSRavi Bangoria 			return 0;
424f41d84ddSRavi Bangoria 
425f41d84ddSRavi Bangoria 		return branch_target(&instr);
426f41d84ddSRavi Bangoria 	}
42769123184SMichael Neuling 
42869123184SMichael Neuling 	/* Userspace: need copy instruction here then translate it */
42969123184SMichael Neuling 	pagefault_disable();
43069123184SMichael Neuling 	ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
43169123184SMichael Neuling 	if (ret) {
43269123184SMichael Neuling 		pagefault_enable();
43369123184SMichael Neuling 		return 0;
43469123184SMichael Neuling 	}
43569123184SMichael Neuling 	pagefault_enable();
43669123184SMichael Neuling 
43769123184SMichael Neuling 	target = branch_target(&instr);
43869123184SMichael Neuling 	if ((!target) || (instr & BRANCH_ABSOLUTE))
43969123184SMichael Neuling 		return target;
44069123184SMichael Neuling 
44169123184SMichael Neuling 	/* Translate relative branch target from kernel to user address */
44269123184SMichael Neuling 	return target - (unsigned long)&instr + addr;
44369123184SMichael Neuling }
444d52f2dc4SMichael Neuling 
445d52f2dc4SMichael Neuling /* Processing BHRB entries */
446da97e184SJoel Fernandes (Google) static void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw)
447d52f2dc4SMichael Neuling {
448d52f2dc4SMichael Neuling 	u64 val;
449d52f2dc4SMichael Neuling 	u64 addr;
450506e70d1SMichael Neuling 	int r_index, u_index, pred;
451d52f2dc4SMichael Neuling 
452d52f2dc4SMichael Neuling 	r_index = 0;
453d52f2dc4SMichael Neuling 	u_index = 0;
454d52f2dc4SMichael Neuling 	while (r_index < ppmu->bhrb_nr) {
455d52f2dc4SMichael Neuling 		/* Assembly read function */
456506e70d1SMichael Neuling 		val = read_bhrb(r_index++);
457506e70d1SMichael Neuling 		if (!val)
458d52f2dc4SMichael Neuling 			/* Terminal marker: End of valid BHRB entries */
459d52f2dc4SMichael Neuling 			break;
460506e70d1SMichael Neuling 		else {
461d52f2dc4SMichael Neuling 			addr = val & BHRB_EA;
462d52f2dc4SMichael Neuling 			pred = val & BHRB_PREDICTION;
463d52f2dc4SMichael Neuling 
464506e70d1SMichael Neuling 			if (!addr)
465506e70d1SMichael Neuling 				/* invalid entry */
466d52f2dc4SMichael Neuling 				continue;
467d52f2dc4SMichael Neuling 
468bb19af81SMadhavan Srinivasan 			/*
469bb19af81SMadhavan Srinivasan 			 * BHRB rolling buffer could very much contain the kernel
470bb19af81SMadhavan Srinivasan 			 * addresses at this point. Check the privileges before
471bb19af81SMadhavan Srinivasan 			 * exporting it to userspace (avoid exposure of regions
472bb19af81SMadhavan Srinivasan 			 * where we could have speculative execution)
473bb19af81SMadhavan Srinivasan 			 */
474da97e184SJoel Fernandes (Google) 			if (is_kernel_addr(addr) && perf_allow_kernel(&event->attr) != 0)
475bb19af81SMadhavan Srinivasan 				continue;
476bb19af81SMadhavan Srinivasan 
477506e70d1SMichael Neuling 			/* Branches are read most recent first (ie. mfbhrb 0 is
478506e70d1SMichael Neuling 			 * the most recent branch).
479506e70d1SMichael Neuling 			 * There are two types of valid entries:
480506e70d1SMichael Neuling 			 * 1) a target entry which is the to address of a
481506e70d1SMichael Neuling 			 *    computed goto like a blr,bctr,btar.  The next
482506e70d1SMichael Neuling 			 *    entry read from the bhrb will be branch
483506e70d1SMichael Neuling 			 *    corresponding to this target (ie. the actual
484506e70d1SMichael Neuling 			 *    blr/bctr/btar instruction).
485506e70d1SMichael Neuling 			 * 2) a from address which is an actual branch.  If a
486506e70d1SMichael Neuling 			 *    target entry proceeds this, then this is the
487506e70d1SMichael Neuling 			 *    matching branch for that target.  If this is not
488506e70d1SMichael Neuling 			 *    following a target entry, then this is a branch
489506e70d1SMichael Neuling 			 *    where the target is given as an immediate field
490506e70d1SMichael Neuling 			 *    in the instruction (ie. an i or b form branch).
491506e70d1SMichael Neuling 			 *    In this case we need to read the instruction from
492506e70d1SMichael Neuling 			 *    memory to determine the target/to address.
493506e70d1SMichael Neuling 			 */
494d52f2dc4SMichael Neuling 
495d52f2dc4SMichael Neuling 			if (val & BHRB_TARGET) {
496506e70d1SMichael Neuling 				/* Target branches use two entries
497506e70d1SMichael Neuling 				 * (ie. computed gotos/XL form)
498506e70d1SMichael Neuling 				 */
499506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].to = addr;
500d52f2dc4SMichael Neuling 				cpuhw->bhrb_entries[u_index].mispred = pred;
501d52f2dc4SMichael Neuling 				cpuhw->bhrb_entries[u_index].predicted = ~pred;
502d52f2dc4SMichael Neuling 
503506e70d1SMichael Neuling 				/* Get from address in next entry */
504506e70d1SMichael Neuling 				val = read_bhrb(r_index++);
505506e70d1SMichael Neuling 				addr = val & BHRB_EA;
506506e70d1SMichael Neuling 				if (val & BHRB_TARGET) {
507506e70d1SMichael Neuling 					/* Shouldn't have two targets in a
508506e70d1SMichael Neuling 					   row.. Reset index and try again */
509506e70d1SMichael Neuling 					r_index--;
510506e70d1SMichael Neuling 					addr = 0;
511d52f2dc4SMichael Neuling 				}
512506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].from = addr;
513506e70d1SMichael Neuling 			} else {
514506e70d1SMichael Neuling 				/* Branches to immediate field
515506e70d1SMichael Neuling 				   (ie I or B form) */
516506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].from = addr;
51769123184SMichael Neuling 				cpuhw->bhrb_entries[u_index].to =
51869123184SMichael Neuling 					power_pmu_bhrb_to(addr);
519506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].mispred = pred;
520506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].predicted = ~pred;
521506e70d1SMichael Neuling 			}
522506e70d1SMichael Neuling 			u_index++;
523506e70d1SMichael Neuling 
524d52f2dc4SMichael Neuling 		}
525d52f2dc4SMichael Neuling 	}
526d52f2dc4SMichael Neuling 	cpuhw->bhrb_stack.nr = u_index;
527d52f2dc4SMichael Neuling 	return;
528d52f2dc4SMichael Neuling }
529d52f2dc4SMichael Neuling 
530330a1eb7SMichael Ellerman static bool is_ebb_event(struct perf_event *event)
531330a1eb7SMichael Ellerman {
532330a1eb7SMichael Ellerman 	/*
533330a1eb7SMichael Ellerman 	 * This could be a per-PMU callback, but we'd rather avoid the cost. We
534330a1eb7SMichael Ellerman 	 * check that the PMU supports EBB, meaning those that don't can still
535330a1eb7SMichael Ellerman 	 * use bit 63 of the event code for something else if they wish.
536330a1eb7SMichael Ellerman 	 */
5374d9690ddSJoel Stanley 	return (ppmu->flags & PPMU_ARCH_207S) &&
5388d7c55d0SMichael Ellerman 	       ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
539330a1eb7SMichael Ellerman }
540330a1eb7SMichael Ellerman 
541330a1eb7SMichael Ellerman static int ebb_event_check(struct perf_event *event)
542330a1eb7SMichael Ellerman {
543330a1eb7SMichael Ellerman 	struct perf_event *leader = event->group_leader;
544330a1eb7SMichael Ellerman 
545330a1eb7SMichael Ellerman 	/* Event and group leader must agree on EBB */
546330a1eb7SMichael Ellerman 	if (is_ebb_event(leader) != is_ebb_event(event))
547330a1eb7SMichael Ellerman 		return -EINVAL;
548330a1eb7SMichael Ellerman 
549330a1eb7SMichael Ellerman 	if (is_ebb_event(event)) {
550330a1eb7SMichael Ellerman 		if (!(event->attach_state & PERF_ATTACH_TASK))
551330a1eb7SMichael Ellerman 			return -EINVAL;
552330a1eb7SMichael Ellerman 
553330a1eb7SMichael Ellerman 		if (!leader->attr.pinned || !leader->attr.exclusive)
554330a1eb7SMichael Ellerman 			return -EINVAL;
555330a1eb7SMichael Ellerman 
55658b5fb00SMichael Ellerman 		if (event->attr.freq ||
55758b5fb00SMichael Ellerman 		    event->attr.inherit ||
55858b5fb00SMichael Ellerman 		    event->attr.sample_type ||
55958b5fb00SMichael Ellerman 		    event->attr.sample_period ||
56058b5fb00SMichael Ellerman 		    event->attr.enable_on_exec)
561330a1eb7SMichael Ellerman 			return -EINVAL;
562330a1eb7SMichael Ellerman 	}
563330a1eb7SMichael Ellerman 
564330a1eb7SMichael Ellerman 	return 0;
565330a1eb7SMichael Ellerman }
566330a1eb7SMichael Ellerman 
567330a1eb7SMichael Ellerman static void ebb_event_add(struct perf_event *event)
568330a1eb7SMichael Ellerman {
569330a1eb7SMichael Ellerman 	if (!is_ebb_event(event) || current->thread.used_ebb)
570330a1eb7SMichael Ellerman 		return;
571330a1eb7SMichael Ellerman 
572330a1eb7SMichael Ellerman 	/*
573330a1eb7SMichael Ellerman 	 * IFF this is the first time we've added an EBB event, set
574330a1eb7SMichael Ellerman 	 * PMXE in the user MMCR0 so we can detect when it's cleared by
575330a1eb7SMichael Ellerman 	 * userspace. We need this so that we can context switch while
576330a1eb7SMichael Ellerman 	 * userspace is in the EBB handler (where PMXE is 0).
577330a1eb7SMichael Ellerman 	 */
578330a1eb7SMichael Ellerman 	current->thread.used_ebb = 1;
579330a1eb7SMichael Ellerman 	current->thread.mmcr0 |= MMCR0_PMXE;
580330a1eb7SMichael Ellerman }
581330a1eb7SMichael Ellerman 
582330a1eb7SMichael Ellerman static void ebb_switch_out(unsigned long mmcr0)
583330a1eb7SMichael Ellerman {
584330a1eb7SMichael Ellerman 	if (!(mmcr0 & MMCR0_EBE))
585330a1eb7SMichael Ellerman 		return;
586330a1eb7SMichael Ellerman 
587330a1eb7SMichael Ellerman 	current->thread.siar  = mfspr(SPRN_SIAR);
588330a1eb7SMichael Ellerman 	current->thread.sier  = mfspr(SPRN_SIER);
589330a1eb7SMichael Ellerman 	current->thread.sdar  = mfspr(SPRN_SDAR);
590330a1eb7SMichael Ellerman 	current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
591330a1eb7SMichael Ellerman 	current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
592330a1eb7SMichael Ellerman }
593330a1eb7SMichael Ellerman 
5949de5cb0fSMichael Ellerman static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
595330a1eb7SMichael Ellerman {
5969de5cb0fSMichael Ellerman 	unsigned long mmcr0 = cpuhw->mmcr[0];
5979de5cb0fSMichael Ellerman 
598330a1eb7SMichael Ellerman 	if (!ebb)
599330a1eb7SMichael Ellerman 		goto out;
600330a1eb7SMichael Ellerman 
60176cb8a78SMichael Ellerman 	/* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
60276cb8a78SMichael Ellerman 	mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
603330a1eb7SMichael Ellerman 
604c2e37a26SMichael Ellerman 	/*
605c2e37a26SMichael Ellerman 	 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
606c2e37a26SMichael Ellerman 	 * with pmao_restore_workaround() because we may add PMAO but we never
607c2e37a26SMichael Ellerman 	 * clear it here.
608c2e37a26SMichael Ellerman 	 */
609330a1eb7SMichael Ellerman 	mmcr0 |= current->thread.mmcr0;
610330a1eb7SMichael Ellerman 
611c2e37a26SMichael Ellerman 	/*
612c2e37a26SMichael Ellerman 	 * Be careful not to set PMXE if userspace had it cleared. This is also
613c2e37a26SMichael Ellerman 	 * compatible with pmao_restore_workaround() because it has already
614c2e37a26SMichael Ellerman 	 * cleared PMXE and we leave PMAO alone.
615c2e37a26SMichael Ellerman 	 */
616330a1eb7SMichael Ellerman 	if (!(current->thread.mmcr0 & MMCR0_PMXE))
617330a1eb7SMichael Ellerman 		mmcr0 &= ~MMCR0_PMXE;
618330a1eb7SMichael Ellerman 
619330a1eb7SMichael Ellerman 	mtspr(SPRN_SIAR, current->thread.siar);
620330a1eb7SMichael Ellerman 	mtspr(SPRN_SIER, current->thread.sier);
621330a1eb7SMichael Ellerman 	mtspr(SPRN_SDAR, current->thread.sdar);
6229de5cb0fSMichael Ellerman 
6239de5cb0fSMichael Ellerman 	/*
6249de5cb0fSMichael Ellerman 	 * Merge the kernel & user values of MMCR2. The semantics we implement
6259de5cb0fSMichael Ellerman 	 * are that the user MMCR2 can set bits, ie. cause counters to freeze,
6269de5cb0fSMichael Ellerman 	 * but not clear bits. If a task wants to be able to clear bits, ie.
6279de5cb0fSMichael Ellerman 	 * unfreeze counters, it should not set exclude_xxx in its events and
6289de5cb0fSMichael Ellerman 	 * instead manage the MMCR2 entirely by itself.
6299de5cb0fSMichael Ellerman 	 */
6309de5cb0fSMichael Ellerman 	mtspr(SPRN_MMCR2, cpuhw->mmcr[3] | current->thread.mmcr2);
631330a1eb7SMichael Ellerman out:
632330a1eb7SMichael Ellerman 	return mmcr0;
633330a1eb7SMichael Ellerman }
634c2e37a26SMichael Ellerman 
635c2e37a26SMichael Ellerman static void pmao_restore_workaround(bool ebb)
636c2e37a26SMichael Ellerman {
637c2e37a26SMichael Ellerman 	unsigned pmcs[6];
638c2e37a26SMichael Ellerman 
639c2e37a26SMichael Ellerman 	if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
640c2e37a26SMichael Ellerman 		return;
641c2e37a26SMichael Ellerman 
642c2e37a26SMichael Ellerman 	/*
643c2e37a26SMichael Ellerman 	 * On POWER8E there is a hardware defect which affects the PMU context
644c2e37a26SMichael Ellerman 	 * switch logic, ie. power_pmu_disable/enable().
645c2e37a26SMichael Ellerman 	 *
646c2e37a26SMichael Ellerman 	 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
647c2e37a26SMichael Ellerman 	 * by the hardware. Sometime later the actual PMU exception is
648c2e37a26SMichael Ellerman 	 * delivered.
649c2e37a26SMichael Ellerman 	 *
650c2e37a26SMichael Ellerman 	 * If we context switch, or simply disable/enable, the PMU prior to the
651c2e37a26SMichael Ellerman 	 * exception arriving, the exception will be lost when we clear PMAO.
652c2e37a26SMichael Ellerman 	 *
653c2e37a26SMichael Ellerman 	 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
654c2e37a26SMichael Ellerman 	 * set, and this _should_ generate an exception. However because of the
655c2e37a26SMichael Ellerman 	 * defect no exception is generated when we write PMAO, and we get
656c2e37a26SMichael Ellerman 	 * stuck with no counters counting but no exception delivered.
657c2e37a26SMichael Ellerman 	 *
658c2e37a26SMichael Ellerman 	 * The workaround is to detect this case and tweak the hardware to
659c2e37a26SMichael Ellerman 	 * create another pending PMU exception.
660c2e37a26SMichael Ellerman 	 *
661c2e37a26SMichael Ellerman 	 * We do that by setting up PMC6 (cycles) for an imminent overflow and
662c2e37a26SMichael Ellerman 	 * enabling the PMU. That causes a new exception to be generated in the
663c2e37a26SMichael Ellerman 	 * chip, but we don't take it yet because we have interrupts hard
664c2e37a26SMichael Ellerman 	 * disabled. We then write back the PMU state as we want it to be seen
665c2e37a26SMichael Ellerman 	 * by the exception handler. When we reenable interrupts the exception
666c2e37a26SMichael Ellerman 	 * handler will be called and see the correct state.
667c2e37a26SMichael Ellerman 	 *
668c2e37a26SMichael Ellerman 	 * The logic is the same for EBB, except that the exception is gated by
669c2e37a26SMichael Ellerman 	 * us having interrupts hard disabled as well as the fact that we are
670c2e37a26SMichael Ellerman 	 * not in userspace. The exception is finally delivered when we return
671c2e37a26SMichael Ellerman 	 * to userspace.
672c2e37a26SMichael Ellerman 	 */
673c2e37a26SMichael Ellerman 
674c2e37a26SMichael Ellerman 	/* Only if PMAO is set and PMAO_SYNC is clear */
675c2e37a26SMichael Ellerman 	if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
676c2e37a26SMichael Ellerman 		return;
677c2e37a26SMichael Ellerman 
678c2e37a26SMichael Ellerman 	/* If we're doing EBB, only if BESCR[GE] is set */
679c2e37a26SMichael Ellerman 	if (ebb && !(current->thread.bescr & BESCR_GE))
680c2e37a26SMichael Ellerman 		return;
681c2e37a26SMichael Ellerman 
682c2e37a26SMichael Ellerman 	/*
683c2e37a26SMichael Ellerman 	 * We are already soft-disabled in power_pmu_enable(). We need to hard
68458bffb5bSMadhavan Srinivasan 	 * disable to actually prevent the PMU exception from firing.
685c2e37a26SMichael Ellerman 	 */
686c2e37a26SMichael Ellerman 	hard_irq_disable();
687c2e37a26SMichael Ellerman 
688c2e37a26SMichael Ellerman 	/*
689c2e37a26SMichael Ellerman 	 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
690c2e37a26SMichael Ellerman 	 * Using read/write_pmc() in a for loop adds 12 function calls and
691c2e37a26SMichael Ellerman 	 * almost doubles our code size.
692c2e37a26SMichael Ellerman 	 */
693c2e37a26SMichael Ellerman 	pmcs[0] = mfspr(SPRN_PMC1);
694c2e37a26SMichael Ellerman 	pmcs[1] = mfspr(SPRN_PMC2);
695c2e37a26SMichael Ellerman 	pmcs[2] = mfspr(SPRN_PMC3);
696c2e37a26SMichael Ellerman 	pmcs[3] = mfspr(SPRN_PMC4);
697c2e37a26SMichael Ellerman 	pmcs[4] = mfspr(SPRN_PMC5);
698c2e37a26SMichael Ellerman 	pmcs[5] = mfspr(SPRN_PMC6);
699c2e37a26SMichael Ellerman 
700c2e37a26SMichael Ellerman 	/* Ensure all freeze bits are unset */
701c2e37a26SMichael Ellerman 	mtspr(SPRN_MMCR2, 0);
702c2e37a26SMichael Ellerman 
703c2e37a26SMichael Ellerman 	/* Set up PMC6 to overflow in one cycle */
704c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC6, 0x7FFFFFFE);
705c2e37a26SMichael Ellerman 
706c2e37a26SMichael Ellerman 	/* Enable exceptions and unfreeze PMC6 */
707c2e37a26SMichael Ellerman 	mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
708c2e37a26SMichael Ellerman 
709c2e37a26SMichael Ellerman 	/* Now we need to refreeze and restore the PMCs */
710c2e37a26SMichael Ellerman 	mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
711c2e37a26SMichael Ellerman 
712c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC1, pmcs[0]);
713c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC2, pmcs[1]);
714c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC3, pmcs[2]);
715c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC4, pmcs[3]);
716c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC5, pmcs[4]);
717c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC6, pmcs[5]);
718c2e37a26SMichael Ellerman }
719356d8ce3SMadhavan Srinivasan 
720f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
721f2699491SMichael Ellerman 
722f2699491SMichael Ellerman static void perf_event_interrupt(struct pt_regs *regs);
723f2699491SMichael Ellerman 
724f2699491SMichael Ellerman /*
725f2699491SMichael Ellerman  * Read one performance monitor counter (PMC).
726f2699491SMichael Ellerman  */
727f2699491SMichael Ellerman static unsigned long read_pmc(int idx)
728f2699491SMichael Ellerman {
729f2699491SMichael Ellerman 	unsigned long val;
730f2699491SMichael Ellerman 
731f2699491SMichael Ellerman 	switch (idx) {
732f2699491SMichael Ellerman 	case 1:
733f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC1);
734f2699491SMichael Ellerman 		break;
735f2699491SMichael Ellerman 	case 2:
736f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC2);
737f2699491SMichael Ellerman 		break;
738f2699491SMichael Ellerman 	case 3:
739f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC3);
740f2699491SMichael Ellerman 		break;
741f2699491SMichael Ellerman 	case 4:
742f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC4);
743f2699491SMichael Ellerman 		break;
744f2699491SMichael Ellerman 	case 5:
745f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC5);
746f2699491SMichael Ellerman 		break;
747f2699491SMichael Ellerman 	case 6:
748f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC6);
749f2699491SMichael Ellerman 		break;
750f2699491SMichael Ellerman #ifdef CONFIG_PPC64
751f2699491SMichael Ellerman 	case 7:
752f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC7);
753f2699491SMichael Ellerman 		break;
754f2699491SMichael Ellerman 	case 8:
755f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC8);
756f2699491SMichael Ellerman 		break;
757f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
758f2699491SMichael Ellerman 	default:
759f2699491SMichael Ellerman 		printk(KERN_ERR "oops trying to read PMC%d\n", idx);
760f2699491SMichael Ellerman 		val = 0;
761f2699491SMichael Ellerman 	}
762f2699491SMichael Ellerman 	return val;
763f2699491SMichael Ellerman }
764f2699491SMichael Ellerman 
765f2699491SMichael Ellerman /*
766f2699491SMichael Ellerman  * Write one PMC.
767f2699491SMichael Ellerman  */
768f2699491SMichael Ellerman static void write_pmc(int idx, unsigned long val)
769f2699491SMichael Ellerman {
770f2699491SMichael Ellerman 	switch (idx) {
771f2699491SMichael Ellerman 	case 1:
772f2699491SMichael Ellerman 		mtspr(SPRN_PMC1, val);
773f2699491SMichael Ellerman 		break;
774f2699491SMichael Ellerman 	case 2:
775f2699491SMichael Ellerman 		mtspr(SPRN_PMC2, val);
776f2699491SMichael Ellerman 		break;
777f2699491SMichael Ellerman 	case 3:
778f2699491SMichael Ellerman 		mtspr(SPRN_PMC3, val);
779f2699491SMichael Ellerman 		break;
780f2699491SMichael Ellerman 	case 4:
781f2699491SMichael Ellerman 		mtspr(SPRN_PMC4, val);
782f2699491SMichael Ellerman 		break;
783f2699491SMichael Ellerman 	case 5:
784f2699491SMichael Ellerman 		mtspr(SPRN_PMC5, val);
785f2699491SMichael Ellerman 		break;
786f2699491SMichael Ellerman 	case 6:
787f2699491SMichael Ellerman 		mtspr(SPRN_PMC6, val);
788f2699491SMichael Ellerman 		break;
789f2699491SMichael Ellerman #ifdef CONFIG_PPC64
790f2699491SMichael Ellerman 	case 7:
791f2699491SMichael Ellerman 		mtspr(SPRN_PMC7, val);
792f2699491SMichael Ellerman 		break;
793f2699491SMichael Ellerman 	case 8:
794f2699491SMichael Ellerman 		mtspr(SPRN_PMC8, val);
795f2699491SMichael Ellerman 		break;
796f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
797f2699491SMichael Ellerman 	default:
798f2699491SMichael Ellerman 		printk(KERN_ERR "oops trying to write PMC%d\n", idx);
799f2699491SMichael Ellerman 	}
800f2699491SMichael Ellerman }
801f2699491SMichael Ellerman 
8025f6d0380SAnshuman Khandual /* Called from sysrq_handle_showregs() */
8035f6d0380SAnshuman Khandual void perf_event_print_debug(void)
8045f6d0380SAnshuman Khandual {
8055f6d0380SAnshuman Khandual 	unsigned long sdar, sier, flags;
8065f6d0380SAnshuman Khandual 	u32 pmcs[MAX_HWEVENTS];
8075f6d0380SAnshuman Khandual 	int i;
8085f6d0380SAnshuman Khandual 
8094917fcb5SRavi Bangoria 	if (!ppmu) {
8104917fcb5SRavi Bangoria 		pr_info("Performance monitor hardware not registered.\n");
8114917fcb5SRavi Bangoria 		return;
8124917fcb5SRavi Bangoria 	}
8134917fcb5SRavi Bangoria 
8145f6d0380SAnshuman Khandual 	if (!ppmu->n_counter)
8155f6d0380SAnshuman Khandual 		return;
8165f6d0380SAnshuman Khandual 
8175f6d0380SAnshuman Khandual 	local_irq_save(flags);
8185f6d0380SAnshuman Khandual 
8195f6d0380SAnshuman Khandual 	pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
8205f6d0380SAnshuman Khandual 		 smp_processor_id(), ppmu->name, ppmu->n_counter);
8215f6d0380SAnshuman Khandual 
8225f6d0380SAnshuman Khandual 	for (i = 0; i < ppmu->n_counter; i++)
8235f6d0380SAnshuman Khandual 		pmcs[i] = read_pmc(i + 1);
8245f6d0380SAnshuman Khandual 
8255f6d0380SAnshuman Khandual 	for (; i < MAX_HWEVENTS; i++)
8265f6d0380SAnshuman Khandual 		pmcs[i] = 0xdeadbeef;
8275f6d0380SAnshuman Khandual 
8285f6d0380SAnshuman Khandual 	pr_info("PMC1:  %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
8295f6d0380SAnshuman Khandual 		 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
8305f6d0380SAnshuman Khandual 
8315f6d0380SAnshuman Khandual 	if (ppmu->n_counter > 4)
8325f6d0380SAnshuman Khandual 		pr_info("PMC5:  %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
8335f6d0380SAnshuman Khandual 			 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
8345f6d0380SAnshuman Khandual 
8355f6d0380SAnshuman Khandual 	pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
8365f6d0380SAnshuman Khandual 		mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
8375f6d0380SAnshuman Khandual 
8385f6d0380SAnshuman Khandual 	sdar = sier = 0;
8395f6d0380SAnshuman Khandual #ifdef CONFIG_PPC64
8405f6d0380SAnshuman Khandual 	sdar = mfspr(SPRN_SDAR);
8415f6d0380SAnshuman Khandual 
8425f6d0380SAnshuman Khandual 	if (ppmu->flags & PPMU_HAS_SIER)
8435f6d0380SAnshuman Khandual 		sier = mfspr(SPRN_SIER);
8445f6d0380SAnshuman Khandual 
8454d9690ddSJoel Stanley 	if (ppmu->flags & PPMU_ARCH_207S) {
8465f6d0380SAnshuman Khandual 		pr_info("MMCR2: %016lx EBBHR: %016lx\n",
8475f6d0380SAnshuman Khandual 			mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
8485f6d0380SAnshuman Khandual 		pr_info("EBBRR: %016lx BESCR: %016lx\n",
8495f6d0380SAnshuman Khandual 			mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
8505f6d0380SAnshuman Khandual 	}
8515f6d0380SAnshuman Khandual #endif
8525f6d0380SAnshuman Khandual 	pr_info("SIAR:  %016lx SDAR:  %016lx SIER:  %016lx\n",
8535f6d0380SAnshuman Khandual 		mfspr(SPRN_SIAR), sdar, sier);
8545f6d0380SAnshuman Khandual 
8555f6d0380SAnshuman Khandual 	local_irq_restore(flags);
8565f6d0380SAnshuman Khandual }
8575f6d0380SAnshuman Khandual 
858f2699491SMichael Ellerman /*
859f2699491SMichael Ellerman  * Check if a set of events can all go on the PMU at once.
860f2699491SMichael Ellerman  * If they can't, this will look at alternative codes for the events
861f2699491SMichael Ellerman  * and see if any combination of alternative codes is feasible.
862f2699491SMichael Ellerman  * The feasible set is returned in event_id[].
863f2699491SMichael Ellerman  */
864f2699491SMichael Ellerman static int power_check_constraints(struct cpu_hw_events *cpuhw,
865f2699491SMichael Ellerman 				   u64 event_id[], unsigned int cflags[],
866f2699491SMichael Ellerman 				   int n_ev)
867f2699491SMichael Ellerman {
868f2699491SMichael Ellerman 	unsigned long mask, value, nv;
869f2699491SMichael Ellerman 	unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
870f2699491SMichael Ellerman 	int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
871f2699491SMichael Ellerman 	int i, j;
872f2699491SMichael Ellerman 	unsigned long addf = ppmu->add_fields;
873f2699491SMichael Ellerman 	unsigned long tadd = ppmu->test_adder;
87459029136SMadhavan Srinivasan 	unsigned long grp_mask = ppmu->group_constraint_mask;
87559029136SMadhavan Srinivasan 	unsigned long grp_val = ppmu->group_constraint_val;
876f2699491SMichael Ellerman 
877f2699491SMichael Ellerman 	if (n_ev > ppmu->n_counter)
878f2699491SMichael Ellerman 		return -1;
879f2699491SMichael Ellerman 
880f2699491SMichael Ellerman 	/* First see if the events will go on as-is */
881f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
882f2699491SMichael Ellerman 		if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
883f2699491SMichael Ellerman 		    && !ppmu->limited_pmc_event(event_id[i])) {
884f2699491SMichael Ellerman 			ppmu->get_alternatives(event_id[i], cflags[i],
885f2699491SMichael Ellerman 					       cpuhw->alternatives[i]);
886f2699491SMichael Ellerman 			event_id[i] = cpuhw->alternatives[i][0];
887f2699491SMichael Ellerman 		}
888f2699491SMichael Ellerman 		if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
889f2699491SMichael Ellerman 					 &cpuhw->avalues[i][0]))
890f2699491SMichael Ellerman 			return -1;
891f2699491SMichael Ellerman 	}
892f2699491SMichael Ellerman 	value = mask = 0;
893f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
894f2699491SMichael Ellerman 		nv = (value | cpuhw->avalues[i][0]) +
895f2699491SMichael Ellerman 			(value & cpuhw->avalues[i][0] & addf);
89659029136SMadhavan Srinivasan 
89759029136SMadhavan Srinivasan 		if (((((nv + tadd) ^ value) & mask) & (~grp_mask)) != 0)
898f2699491SMichael Ellerman 			break;
89959029136SMadhavan Srinivasan 
90059029136SMadhavan Srinivasan 		if (((((nv + tadd) ^ cpuhw->avalues[i][0]) & cpuhw->amasks[i][0])
90159029136SMadhavan Srinivasan 			& (~grp_mask)) != 0)
90259029136SMadhavan Srinivasan 			break;
90359029136SMadhavan Srinivasan 
904f2699491SMichael Ellerman 		value = nv;
905f2699491SMichael Ellerman 		mask |= cpuhw->amasks[i][0];
906f2699491SMichael Ellerman 	}
90759029136SMadhavan Srinivasan 	if (i == n_ev) {
90859029136SMadhavan Srinivasan 		if ((value & mask & grp_mask) != (mask & grp_val))
90959029136SMadhavan Srinivasan 			return -1;
91059029136SMadhavan Srinivasan 		else
911f2699491SMichael Ellerman 			return 0;	/* all OK */
91259029136SMadhavan Srinivasan 	}
913f2699491SMichael Ellerman 
914f2699491SMichael Ellerman 	/* doesn't work, gather alternatives... */
915f2699491SMichael Ellerman 	if (!ppmu->get_alternatives)
916f2699491SMichael Ellerman 		return -1;
917f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
918f2699491SMichael Ellerman 		choice[i] = 0;
919f2699491SMichael Ellerman 		n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
920f2699491SMichael Ellerman 						  cpuhw->alternatives[i]);
921f2699491SMichael Ellerman 		for (j = 1; j < n_alt[i]; ++j)
922f2699491SMichael Ellerman 			ppmu->get_constraint(cpuhw->alternatives[i][j],
923f2699491SMichael Ellerman 					     &cpuhw->amasks[i][j],
924f2699491SMichael Ellerman 					     &cpuhw->avalues[i][j]);
925f2699491SMichael Ellerman 	}
926f2699491SMichael Ellerman 
927f2699491SMichael Ellerman 	/* enumerate all possibilities and see if any will work */
928f2699491SMichael Ellerman 	i = 0;
929f2699491SMichael Ellerman 	j = -1;
930f2699491SMichael Ellerman 	value = mask = nv = 0;
931f2699491SMichael Ellerman 	while (i < n_ev) {
932f2699491SMichael Ellerman 		if (j >= 0) {
933f2699491SMichael Ellerman 			/* we're backtracking, restore context */
934f2699491SMichael Ellerman 			value = svalues[i];
935f2699491SMichael Ellerman 			mask = smasks[i];
936f2699491SMichael Ellerman 			j = choice[i];
937f2699491SMichael Ellerman 		}
938f2699491SMichael Ellerman 		/*
939f2699491SMichael Ellerman 		 * See if any alternative k for event_id i,
940f2699491SMichael Ellerman 		 * where k > j, will satisfy the constraints.
941f2699491SMichael Ellerman 		 */
942f2699491SMichael Ellerman 		while (++j < n_alt[i]) {
943f2699491SMichael Ellerman 			nv = (value | cpuhw->avalues[i][j]) +
944f2699491SMichael Ellerman 				(value & cpuhw->avalues[i][j] & addf);
945f2699491SMichael Ellerman 			if ((((nv + tadd) ^ value) & mask) == 0 &&
946f2699491SMichael Ellerman 			    (((nv + tadd) ^ cpuhw->avalues[i][j])
947f2699491SMichael Ellerman 			     & cpuhw->amasks[i][j]) == 0)
948f2699491SMichael Ellerman 				break;
949f2699491SMichael Ellerman 		}
950f2699491SMichael Ellerman 		if (j >= n_alt[i]) {
951f2699491SMichael Ellerman 			/*
952f2699491SMichael Ellerman 			 * No feasible alternative, backtrack
953f2699491SMichael Ellerman 			 * to event_id i-1 and continue enumerating its
954f2699491SMichael Ellerman 			 * alternatives from where we got up to.
955f2699491SMichael Ellerman 			 */
956f2699491SMichael Ellerman 			if (--i < 0)
957f2699491SMichael Ellerman 				return -1;
958f2699491SMichael Ellerman 		} else {
959f2699491SMichael Ellerman 			/*
960f2699491SMichael Ellerman 			 * Found a feasible alternative for event_id i,
961f2699491SMichael Ellerman 			 * remember where we got up to with this event_id,
962f2699491SMichael Ellerman 			 * go on to the next event_id, and start with
963f2699491SMichael Ellerman 			 * the first alternative for it.
964f2699491SMichael Ellerman 			 */
965f2699491SMichael Ellerman 			choice[i] = j;
966f2699491SMichael Ellerman 			svalues[i] = value;
967f2699491SMichael Ellerman 			smasks[i] = mask;
968f2699491SMichael Ellerman 			value = nv;
969f2699491SMichael Ellerman 			mask |= cpuhw->amasks[i][j];
970f2699491SMichael Ellerman 			++i;
971f2699491SMichael Ellerman 			j = -1;
972f2699491SMichael Ellerman 		}
973f2699491SMichael Ellerman 	}
974f2699491SMichael Ellerman 
975f2699491SMichael Ellerman 	/* OK, we have a feasible combination, tell the caller the solution */
976f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i)
977f2699491SMichael Ellerman 		event_id[i] = cpuhw->alternatives[i][choice[i]];
978f2699491SMichael Ellerman 	return 0;
979f2699491SMichael Ellerman }
980f2699491SMichael Ellerman 
981f2699491SMichael Ellerman /*
982f2699491SMichael Ellerman  * Check if newly-added events have consistent settings for
983f2699491SMichael Ellerman  * exclude_{user,kernel,hv} with each other and any previously
984f2699491SMichael Ellerman  * added events.
985f2699491SMichael Ellerman  */
986f2699491SMichael Ellerman static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
987f2699491SMichael Ellerman 			  int n_prev, int n_new)
988f2699491SMichael Ellerman {
989f2699491SMichael Ellerman 	int eu = 0, ek = 0, eh = 0;
990f2699491SMichael Ellerman 	int i, n, first;
991f2699491SMichael Ellerman 	struct perf_event *event;
992f2699491SMichael Ellerman 
9939de5cb0fSMichael Ellerman 	/*
9949de5cb0fSMichael Ellerman 	 * If the PMU we're on supports per event exclude settings then we
9959de5cb0fSMichael Ellerman 	 * don't need to do any of this logic. NB. This assumes no PMU has both
9969de5cb0fSMichael Ellerman 	 * per event exclude and limited PMCs.
9979de5cb0fSMichael Ellerman 	 */
9989de5cb0fSMichael Ellerman 	if (ppmu->flags & PPMU_ARCH_207S)
9999de5cb0fSMichael Ellerman 		return 0;
10009de5cb0fSMichael Ellerman 
1001f2699491SMichael Ellerman 	n = n_prev + n_new;
1002f2699491SMichael Ellerman 	if (n <= 1)
1003f2699491SMichael Ellerman 		return 0;
1004f2699491SMichael Ellerman 
1005f2699491SMichael Ellerman 	first = 1;
1006f2699491SMichael Ellerman 	for (i = 0; i < n; ++i) {
1007f2699491SMichael Ellerman 		if (cflags[i] & PPMU_LIMITED_PMC_OK) {
1008f2699491SMichael Ellerman 			cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
1009f2699491SMichael Ellerman 			continue;
1010f2699491SMichael Ellerman 		}
1011f2699491SMichael Ellerman 		event = ctrs[i];
1012f2699491SMichael Ellerman 		if (first) {
1013f2699491SMichael Ellerman 			eu = event->attr.exclude_user;
1014f2699491SMichael Ellerman 			ek = event->attr.exclude_kernel;
1015f2699491SMichael Ellerman 			eh = event->attr.exclude_hv;
1016f2699491SMichael Ellerman 			first = 0;
1017f2699491SMichael Ellerman 		} else if (event->attr.exclude_user != eu ||
1018f2699491SMichael Ellerman 			   event->attr.exclude_kernel != ek ||
1019f2699491SMichael Ellerman 			   event->attr.exclude_hv != eh) {
1020f2699491SMichael Ellerman 			return -EAGAIN;
1021f2699491SMichael Ellerman 		}
1022f2699491SMichael Ellerman 	}
1023f2699491SMichael Ellerman 
1024f2699491SMichael Ellerman 	if (eu || ek || eh)
1025f2699491SMichael Ellerman 		for (i = 0; i < n; ++i)
1026f2699491SMichael Ellerman 			if (cflags[i] & PPMU_LIMITED_PMC_OK)
1027f2699491SMichael Ellerman 				cflags[i] |= PPMU_LIMITED_PMC_REQD;
1028f2699491SMichael Ellerman 
1029f2699491SMichael Ellerman 	return 0;
1030f2699491SMichael Ellerman }
1031f2699491SMichael Ellerman 
1032f2699491SMichael Ellerman static u64 check_and_compute_delta(u64 prev, u64 val)
1033f2699491SMichael Ellerman {
1034f2699491SMichael Ellerman 	u64 delta = (val - prev) & 0xfffffffful;
1035f2699491SMichael Ellerman 
1036f2699491SMichael Ellerman 	/*
1037f2699491SMichael Ellerman 	 * POWER7 can roll back counter values, if the new value is smaller
1038f2699491SMichael Ellerman 	 * than the previous value it will cause the delta and the counter to
1039f2699491SMichael Ellerman 	 * have bogus values unless we rolled a counter over.  If a coutner is
1040f2699491SMichael Ellerman 	 * rolled back, it will be smaller, but within 256, which is the maximum
1041027dfac6SMichael Ellerman 	 * number of events to rollback at once.  If we detect a rollback
1042f2699491SMichael Ellerman 	 * return 0.  This can lead to a small lack of precision in the
1043f2699491SMichael Ellerman 	 * counters.
1044f2699491SMichael Ellerman 	 */
1045f2699491SMichael Ellerman 	if (prev > val && (prev - val) < 256)
1046f2699491SMichael Ellerman 		delta = 0;
1047f2699491SMichael Ellerman 
1048f2699491SMichael Ellerman 	return delta;
1049f2699491SMichael Ellerman }
1050f2699491SMichael Ellerman 
1051f2699491SMichael Ellerman static void power_pmu_read(struct perf_event *event)
1052f2699491SMichael Ellerman {
1053f2699491SMichael Ellerman 	s64 val, delta, prev;
1054f2699491SMichael Ellerman 
1055f2699491SMichael Ellerman 	if (event->hw.state & PERF_HES_STOPPED)
1056f2699491SMichael Ellerman 		return;
1057f2699491SMichael Ellerman 
1058f2699491SMichael Ellerman 	if (!event->hw.idx)
1059f2699491SMichael Ellerman 		return;
1060330a1eb7SMichael Ellerman 
1061330a1eb7SMichael Ellerman 	if (is_ebb_event(event)) {
1062330a1eb7SMichael Ellerman 		val = read_pmc(event->hw.idx);
1063330a1eb7SMichael Ellerman 		local64_set(&event->hw.prev_count, val);
1064330a1eb7SMichael Ellerman 		return;
1065330a1eb7SMichael Ellerman 	}
1066330a1eb7SMichael Ellerman 
1067f2699491SMichael Ellerman 	/*
1068f2699491SMichael Ellerman 	 * Performance monitor interrupts come even when interrupts
1069f2699491SMichael Ellerman 	 * are soft-disabled, as long as interrupts are hard-enabled.
1070f2699491SMichael Ellerman 	 * Therefore we treat them like NMIs.
1071f2699491SMichael Ellerman 	 */
1072f2699491SMichael Ellerman 	do {
1073f2699491SMichael Ellerman 		prev = local64_read(&event->hw.prev_count);
1074f2699491SMichael Ellerman 		barrier();
1075f2699491SMichael Ellerman 		val = read_pmc(event->hw.idx);
1076f2699491SMichael Ellerman 		delta = check_and_compute_delta(prev, val);
1077f2699491SMichael Ellerman 		if (!delta)
1078f2699491SMichael Ellerman 			return;
1079f2699491SMichael Ellerman 	} while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
1080f2699491SMichael Ellerman 
1081f2699491SMichael Ellerman 	local64_add(delta, &event->count);
1082f5602941SAnton Blanchard 
1083f5602941SAnton Blanchard 	/*
1084f5602941SAnton Blanchard 	 * A number of places program the PMC with (0x80000000 - period_left).
1085f5602941SAnton Blanchard 	 * We never want period_left to be less than 1 because we will program
1086f5602941SAnton Blanchard 	 * the PMC with a value >= 0x800000000 and an edge detected PMC will
1087f5602941SAnton Blanchard 	 * roll around to 0 before taking an exception. We have seen this
1088f5602941SAnton Blanchard 	 * on POWER8.
1089f5602941SAnton Blanchard 	 *
1090f5602941SAnton Blanchard 	 * To fix this, clamp the minimum value of period_left to 1.
1091f5602941SAnton Blanchard 	 */
1092f5602941SAnton Blanchard 	do {
1093f5602941SAnton Blanchard 		prev = local64_read(&event->hw.period_left);
1094f5602941SAnton Blanchard 		val = prev - delta;
1095f5602941SAnton Blanchard 		if (val < 1)
1096f5602941SAnton Blanchard 			val = 1;
1097f5602941SAnton Blanchard 	} while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
1098f2699491SMichael Ellerman }
1099f2699491SMichael Ellerman 
1100f2699491SMichael Ellerman /*
1101f2699491SMichael Ellerman  * On some machines, PMC5 and PMC6 can't be written, don't respect
1102f2699491SMichael Ellerman  * the freeze conditions, and don't generate interrupts.  This tells
1103f2699491SMichael Ellerman  * us if `event' is using such a PMC.
1104f2699491SMichael Ellerman  */
1105f2699491SMichael Ellerman static int is_limited_pmc(int pmcnum)
1106f2699491SMichael Ellerman {
1107f2699491SMichael Ellerman 	return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1108f2699491SMichael Ellerman 		&& (pmcnum == 5 || pmcnum == 6);
1109f2699491SMichael Ellerman }
1110f2699491SMichael Ellerman 
1111f2699491SMichael Ellerman static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
1112f2699491SMichael Ellerman 				    unsigned long pmc5, unsigned long pmc6)
1113f2699491SMichael Ellerman {
1114f2699491SMichael Ellerman 	struct perf_event *event;
1115f2699491SMichael Ellerman 	u64 val, prev, delta;
1116f2699491SMichael Ellerman 	int i;
1117f2699491SMichael Ellerman 
1118f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_limited; ++i) {
1119f2699491SMichael Ellerman 		event = cpuhw->limited_counter[i];
1120f2699491SMichael Ellerman 		if (!event->hw.idx)
1121f2699491SMichael Ellerman 			continue;
1122f2699491SMichael Ellerman 		val = (event->hw.idx == 5) ? pmc5 : pmc6;
1123f2699491SMichael Ellerman 		prev = local64_read(&event->hw.prev_count);
1124f2699491SMichael Ellerman 		event->hw.idx = 0;
1125f2699491SMichael Ellerman 		delta = check_and_compute_delta(prev, val);
1126f2699491SMichael Ellerman 		if (delta)
1127f2699491SMichael Ellerman 			local64_add(delta, &event->count);
1128f2699491SMichael Ellerman 	}
1129f2699491SMichael Ellerman }
1130f2699491SMichael Ellerman 
1131f2699491SMichael Ellerman static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
1132f2699491SMichael Ellerman 				  unsigned long pmc5, unsigned long pmc6)
1133f2699491SMichael Ellerman {
1134f2699491SMichael Ellerman 	struct perf_event *event;
1135f2699491SMichael Ellerman 	u64 val, prev;
1136f2699491SMichael Ellerman 	int i;
1137f2699491SMichael Ellerman 
1138f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_limited; ++i) {
1139f2699491SMichael Ellerman 		event = cpuhw->limited_counter[i];
1140f2699491SMichael Ellerman 		event->hw.idx = cpuhw->limited_hwidx[i];
1141f2699491SMichael Ellerman 		val = (event->hw.idx == 5) ? pmc5 : pmc6;
1142f2699491SMichael Ellerman 		prev = local64_read(&event->hw.prev_count);
1143f2699491SMichael Ellerman 		if (check_and_compute_delta(prev, val))
1144f2699491SMichael Ellerman 			local64_set(&event->hw.prev_count, val);
1145f2699491SMichael Ellerman 		perf_event_update_userpage(event);
1146f2699491SMichael Ellerman 	}
1147f2699491SMichael Ellerman }
1148f2699491SMichael Ellerman 
1149f2699491SMichael Ellerman /*
1150f2699491SMichael Ellerman  * Since limited events don't respect the freeze conditions, we
1151f2699491SMichael Ellerman  * have to read them immediately after freezing or unfreezing the
1152f2699491SMichael Ellerman  * other events.  We try to keep the values from the limited
1153f2699491SMichael Ellerman  * events as consistent as possible by keeping the delay (in
1154f2699491SMichael Ellerman  * cycles and instructions) between freezing/unfreezing and reading
1155f2699491SMichael Ellerman  * the limited events as small and consistent as possible.
1156f2699491SMichael Ellerman  * Therefore, if any limited events are in use, we read them
1157f2699491SMichael Ellerman  * both, and always in the same order, to minimize variability,
1158f2699491SMichael Ellerman  * and do it inside the same asm that writes MMCR0.
1159f2699491SMichael Ellerman  */
1160f2699491SMichael Ellerman static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1161f2699491SMichael Ellerman {
1162f2699491SMichael Ellerman 	unsigned long pmc5, pmc6;
1163f2699491SMichael Ellerman 
1164f2699491SMichael Ellerman 	if (!cpuhw->n_limited) {
1165f2699491SMichael Ellerman 		mtspr(SPRN_MMCR0, mmcr0);
1166f2699491SMichael Ellerman 		return;
1167f2699491SMichael Ellerman 	}
1168f2699491SMichael Ellerman 
1169f2699491SMichael Ellerman 	/*
1170f2699491SMichael Ellerman 	 * Write MMCR0, then read PMC5 and PMC6 immediately.
1171f2699491SMichael Ellerman 	 * To ensure we don't get a performance monitor interrupt
1172f2699491SMichael Ellerman 	 * between writing MMCR0 and freezing/thawing the limited
1173f2699491SMichael Ellerman 	 * events, we first write MMCR0 with the event overflow
1174f2699491SMichael Ellerman 	 * interrupt enable bits turned off.
1175f2699491SMichael Ellerman 	 */
1176f2699491SMichael Ellerman 	asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1177f2699491SMichael Ellerman 		     : "=&r" (pmc5), "=&r" (pmc6)
1178f2699491SMichael Ellerman 		     : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1179f2699491SMichael Ellerman 		       "i" (SPRN_MMCR0),
1180f2699491SMichael Ellerman 		       "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1181f2699491SMichael Ellerman 
1182f2699491SMichael Ellerman 	if (mmcr0 & MMCR0_FC)
1183f2699491SMichael Ellerman 		freeze_limited_counters(cpuhw, pmc5, pmc6);
1184f2699491SMichael Ellerman 	else
1185f2699491SMichael Ellerman 		thaw_limited_counters(cpuhw, pmc5, pmc6);
1186f2699491SMichael Ellerman 
1187f2699491SMichael Ellerman 	/*
1188f2699491SMichael Ellerman 	 * Write the full MMCR0 including the event overflow interrupt
1189f2699491SMichael Ellerman 	 * enable bits, if necessary.
1190f2699491SMichael Ellerman 	 */
1191f2699491SMichael Ellerman 	if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1192f2699491SMichael Ellerman 		mtspr(SPRN_MMCR0, mmcr0);
1193f2699491SMichael Ellerman }
1194f2699491SMichael Ellerman 
1195f2699491SMichael Ellerman /*
1196f2699491SMichael Ellerman  * Disable all events to prevent PMU interrupts and to allow
1197f2699491SMichael Ellerman  * events to be added or removed.
1198f2699491SMichael Ellerman  */
1199f2699491SMichael Ellerman static void power_pmu_disable(struct pmu *pmu)
1200f2699491SMichael Ellerman {
1201f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1202330a1eb7SMichael Ellerman 	unsigned long flags, mmcr0, val;
1203f2699491SMichael Ellerman 
1204f2699491SMichael Ellerman 	if (!ppmu)
1205f2699491SMichael Ellerman 		return;
1206f2699491SMichael Ellerman 	local_irq_save(flags);
120769111bacSChristoph Lameter 	cpuhw = this_cpu_ptr(&cpu_hw_events);
1208f2699491SMichael Ellerman 
1209f2699491SMichael Ellerman 	if (!cpuhw->disabled) {
1210f2699491SMichael Ellerman 		/*
1211f2699491SMichael Ellerman 		 * Check if we ever enabled the PMU on this cpu.
1212f2699491SMichael Ellerman 		 */
1213f2699491SMichael Ellerman 		if (!cpuhw->pmcs_enabled) {
1214f2699491SMichael Ellerman 			ppc_enable_pmcs();
1215f2699491SMichael Ellerman 			cpuhw->pmcs_enabled = 1;
1216f2699491SMichael Ellerman 		}
1217f2699491SMichael Ellerman 
1218f2699491SMichael Ellerman 		/*
121976cb8a78SMichael Ellerman 		 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
1220378a6ee9SMichael Ellerman 		 */
1221330a1eb7SMichael Ellerman 		val  = mmcr0 = mfspr(SPRN_MMCR0);
1222378a6ee9SMichael Ellerman 		val |= MMCR0_FC;
122376cb8a78SMichael Ellerman 		val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
122476cb8a78SMichael Ellerman 			 MMCR0_FC56);
1225378a6ee9SMichael Ellerman 
1226378a6ee9SMichael Ellerman 		/*
1227378a6ee9SMichael Ellerman 		 * The barrier is to make sure the mtspr has been
1228378a6ee9SMichael Ellerman 		 * executed and the PMU has frozen the events etc.
1229378a6ee9SMichael Ellerman 		 * before we return.
1230378a6ee9SMichael Ellerman 		 */
1231378a6ee9SMichael Ellerman 		write_mmcr0(cpuhw, val);
1232378a6ee9SMichael Ellerman 		mb();
1233e1ebd0e5SMichael Ellerman 		isync();
1234378a6ee9SMichael Ellerman 
1235378a6ee9SMichael Ellerman 		/*
1236f2699491SMichael Ellerman 		 * Disable instruction sampling if it was enabled
1237f2699491SMichael Ellerman 		 */
1238f2699491SMichael Ellerman 		if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1239f2699491SMichael Ellerman 			mtspr(SPRN_MMCRA,
1240f2699491SMichael Ellerman 			      cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1241f2699491SMichael Ellerman 			mb();
1242e1ebd0e5SMichael Ellerman 			isync();
1243f2699491SMichael Ellerman 		}
1244f2699491SMichael Ellerman 
1245378a6ee9SMichael Ellerman 		cpuhw->disabled = 1;
1246378a6ee9SMichael Ellerman 		cpuhw->n_added = 0;
1247330a1eb7SMichael Ellerman 
1248330a1eb7SMichael Ellerman 		ebb_switch_out(mmcr0);
1249e1ebd0e5SMichael Ellerman 
1250e1ebd0e5SMichael Ellerman #ifdef CONFIG_PPC64
1251e1ebd0e5SMichael Ellerman 		/*
1252e1ebd0e5SMichael Ellerman 		 * These are readable by userspace, may contain kernel
1253e1ebd0e5SMichael Ellerman 		 * addresses and are not switched by context switch, so clear
1254e1ebd0e5SMichael Ellerman 		 * them now to avoid leaking anything to userspace in general
1255e1ebd0e5SMichael Ellerman 		 * including to another process.
1256e1ebd0e5SMichael Ellerman 		 */
1257e1ebd0e5SMichael Ellerman 		if (ppmu->flags & PPMU_ARCH_207S) {
1258e1ebd0e5SMichael Ellerman 			mtspr(SPRN_SDAR, 0);
1259e1ebd0e5SMichael Ellerman 			mtspr(SPRN_SIAR, 0);
1260e1ebd0e5SMichael Ellerman 		}
1261e1ebd0e5SMichael Ellerman #endif
1262f2699491SMichael Ellerman 	}
1263330a1eb7SMichael Ellerman 
1264f2699491SMichael Ellerman 	local_irq_restore(flags);
1265f2699491SMichael Ellerman }
1266f2699491SMichael Ellerman 
1267f2699491SMichael Ellerman /*
1268f2699491SMichael Ellerman  * Re-enable all events if disable == 0.
1269f2699491SMichael Ellerman  * If we were previously disabled and events were added, then
1270f2699491SMichael Ellerman  * put the new config on the PMU.
1271f2699491SMichael Ellerman  */
1272f2699491SMichael Ellerman static void power_pmu_enable(struct pmu *pmu)
1273f2699491SMichael Ellerman {
1274f2699491SMichael Ellerman 	struct perf_event *event;
1275f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1276f2699491SMichael Ellerman 	unsigned long flags;
1277f2699491SMichael Ellerman 	long i;
1278330a1eb7SMichael Ellerman 	unsigned long val, mmcr0;
1279f2699491SMichael Ellerman 	s64 left;
1280f2699491SMichael Ellerman 	unsigned int hwc_index[MAX_HWEVENTS];
1281f2699491SMichael Ellerman 	int n_lim;
1282f2699491SMichael Ellerman 	int idx;
1283330a1eb7SMichael Ellerman 	bool ebb;
1284f2699491SMichael Ellerman 
1285f2699491SMichael Ellerman 	if (!ppmu)
1286f2699491SMichael Ellerman 		return;
1287f2699491SMichael Ellerman 	local_irq_save(flags);
12880a48843dSMichael Ellerman 
128969111bacSChristoph Lameter 	cpuhw = this_cpu_ptr(&cpu_hw_events);
12900a48843dSMichael Ellerman 	if (!cpuhw->disabled)
12910a48843dSMichael Ellerman 		goto out;
12920a48843dSMichael Ellerman 
12934ea355b5SMichael Ellerman 	if (cpuhw->n_events == 0) {
12944ea355b5SMichael Ellerman 		ppc_set_pmu_inuse(0);
12954ea355b5SMichael Ellerman 		goto out;
12964ea355b5SMichael Ellerman 	}
12974ea355b5SMichael Ellerman 
1298f2699491SMichael Ellerman 	cpuhw->disabled = 0;
1299f2699491SMichael Ellerman 
1300f2699491SMichael Ellerman 	/*
1301330a1eb7SMichael Ellerman 	 * EBB requires an exclusive group and all events must have the EBB
1302330a1eb7SMichael Ellerman 	 * flag set, or not set, so we can just check a single event. Also we
1303330a1eb7SMichael Ellerman 	 * know we have at least one event.
1304330a1eb7SMichael Ellerman 	 */
1305330a1eb7SMichael Ellerman 	ebb = is_ebb_event(cpuhw->event[0]);
1306330a1eb7SMichael Ellerman 
1307330a1eb7SMichael Ellerman 	/*
1308f2699491SMichael Ellerman 	 * If we didn't change anything, or only removed events,
1309f2699491SMichael Ellerman 	 * no need to recalculate MMCR* settings and reset the PMCs.
1310f2699491SMichael Ellerman 	 * Just reenable the PMU with the current MMCR* settings
1311f2699491SMichael Ellerman 	 * (possibly updated for removal of events).
1312f2699491SMichael Ellerman 	 */
1313f2699491SMichael Ellerman 	if (!cpuhw->n_added) {
1314f2699491SMichael Ellerman 		mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1315f2699491SMichael Ellerman 		mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1316f2699491SMichael Ellerman 		goto out_enable;
1317f2699491SMichael Ellerman 	}
1318f2699491SMichael Ellerman 
1319f2699491SMichael Ellerman 	/*
132079a4cb28SMichael Ellerman 	 * Clear all MMCR settings and recompute them for the new set of events.
1321f2699491SMichael Ellerman 	 */
132279a4cb28SMichael Ellerman 	memset(cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
132379a4cb28SMichael Ellerman 
1324f2699491SMichael Ellerman 	if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
13258abd818fSMichael Ellerman 			       cpuhw->mmcr, cpuhw->event)) {
1326f2699491SMichael Ellerman 		/* shouldn't ever get here */
1327f2699491SMichael Ellerman 		printk(KERN_ERR "oops compute_mmcr failed\n");
1328f2699491SMichael Ellerman 		goto out;
1329f2699491SMichael Ellerman 	}
1330f2699491SMichael Ellerman 
13319de5cb0fSMichael Ellerman 	if (!(ppmu->flags & PPMU_ARCH_207S)) {
1332f2699491SMichael Ellerman 		/*
13339de5cb0fSMichael Ellerman 		 * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
13349de5cb0fSMichael Ellerman 		 * bits for the first event. We have already checked that all
13359de5cb0fSMichael Ellerman 		 * events have the same value for these bits as the first event.
1336f2699491SMichael Ellerman 		 */
1337f2699491SMichael Ellerman 		event = cpuhw->event[0];
1338f2699491SMichael Ellerman 		if (event->attr.exclude_user)
1339f2699491SMichael Ellerman 			cpuhw->mmcr[0] |= MMCR0_FCP;
1340f2699491SMichael Ellerman 		if (event->attr.exclude_kernel)
1341f2699491SMichael Ellerman 			cpuhw->mmcr[0] |= freeze_events_kernel;
1342f2699491SMichael Ellerman 		if (event->attr.exclude_hv)
1343f2699491SMichael Ellerman 			cpuhw->mmcr[0] |= MMCR0_FCHV;
13449de5cb0fSMichael Ellerman 	}
1345f2699491SMichael Ellerman 
1346f2699491SMichael Ellerman 	/*
1347f2699491SMichael Ellerman 	 * Write the new configuration to MMCR* with the freeze
1348f2699491SMichael Ellerman 	 * bit set and set the hardware events to their initial values.
1349f2699491SMichael Ellerman 	 * Then unfreeze the events.
1350f2699491SMichael Ellerman 	 */
1351f2699491SMichael Ellerman 	ppc_set_pmu_inuse(1);
1352f2699491SMichael Ellerman 	mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1353f2699491SMichael Ellerman 	mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1354f2699491SMichael Ellerman 	mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1355f2699491SMichael Ellerman 				| MMCR0_FC);
13569de5cb0fSMichael Ellerman 	if (ppmu->flags & PPMU_ARCH_207S)
13579de5cb0fSMichael Ellerman 		mtspr(SPRN_MMCR2, cpuhw->mmcr[3]);
1358f2699491SMichael Ellerman 
1359f2699491SMichael Ellerman 	/*
1360f2699491SMichael Ellerman 	 * Read off any pre-existing events that need to move
1361f2699491SMichael Ellerman 	 * to another PMC.
1362f2699491SMichael Ellerman 	 */
1363f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_events; ++i) {
1364f2699491SMichael Ellerman 		event = cpuhw->event[i];
1365f2699491SMichael Ellerman 		if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1366f2699491SMichael Ellerman 			power_pmu_read(event);
1367f2699491SMichael Ellerman 			write_pmc(event->hw.idx, 0);
1368f2699491SMichael Ellerman 			event->hw.idx = 0;
1369f2699491SMichael Ellerman 		}
1370f2699491SMichael Ellerman 	}
1371f2699491SMichael Ellerman 
1372f2699491SMichael Ellerman 	/*
1373f2699491SMichael Ellerman 	 * Initialize the PMCs for all the new and moved events.
1374f2699491SMichael Ellerman 	 */
1375f2699491SMichael Ellerman 	cpuhw->n_limited = n_lim = 0;
1376f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_events; ++i) {
1377f2699491SMichael Ellerman 		event = cpuhw->event[i];
1378f2699491SMichael Ellerman 		if (event->hw.idx)
1379f2699491SMichael Ellerman 			continue;
1380f2699491SMichael Ellerman 		idx = hwc_index[i] + 1;
1381f2699491SMichael Ellerman 		if (is_limited_pmc(idx)) {
1382f2699491SMichael Ellerman 			cpuhw->limited_counter[n_lim] = event;
1383f2699491SMichael Ellerman 			cpuhw->limited_hwidx[n_lim] = idx;
1384f2699491SMichael Ellerman 			++n_lim;
1385f2699491SMichael Ellerman 			continue;
1386f2699491SMichael Ellerman 		}
1387330a1eb7SMichael Ellerman 
1388330a1eb7SMichael Ellerman 		if (ebb)
1389330a1eb7SMichael Ellerman 			val = local64_read(&event->hw.prev_count);
1390330a1eb7SMichael Ellerman 		else {
1391f2699491SMichael Ellerman 			val = 0;
1392f2699491SMichael Ellerman 			if (event->hw.sample_period) {
1393f2699491SMichael Ellerman 				left = local64_read(&event->hw.period_left);
1394f2699491SMichael Ellerman 				if (left < 0x80000000L)
1395f2699491SMichael Ellerman 					val = 0x80000000L - left;
1396f2699491SMichael Ellerman 			}
1397f2699491SMichael Ellerman 			local64_set(&event->hw.prev_count, val);
1398330a1eb7SMichael Ellerman 		}
1399330a1eb7SMichael Ellerman 
1400f2699491SMichael Ellerman 		event->hw.idx = idx;
1401f2699491SMichael Ellerman 		if (event->hw.state & PERF_HES_STOPPED)
1402f2699491SMichael Ellerman 			val = 0;
1403f2699491SMichael Ellerman 		write_pmc(idx, val);
1404330a1eb7SMichael Ellerman 
1405f2699491SMichael Ellerman 		perf_event_update_userpage(event);
1406f2699491SMichael Ellerman 	}
1407f2699491SMichael Ellerman 	cpuhw->n_limited = n_lim;
1408f2699491SMichael Ellerman 	cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1409f2699491SMichael Ellerman 
1410f2699491SMichael Ellerman  out_enable:
1411c2e37a26SMichael Ellerman 	pmao_restore_workaround(ebb);
1412c2e37a26SMichael Ellerman 
14139de5cb0fSMichael Ellerman 	mmcr0 = ebb_switch_in(ebb, cpuhw);
1414330a1eb7SMichael Ellerman 
1415f2699491SMichael Ellerman 	mb();
1416b4d6c06cSAnshuman Khandual 	if (cpuhw->bhrb_users)
1417b4d6c06cSAnshuman Khandual 		ppmu->config_bhrb(cpuhw->bhrb_filter);
1418b4d6c06cSAnshuman Khandual 
1419330a1eb7SMichael Ellerman 	write_mmcr0(cpuhw, mmcr0);
1420f2699491SMichael Ellerman 
1421f2699491SMichael Ellerman 	/*
1422f2699491SMichael Ellerman 	 * Enable instruction sampling if necessary
1423f2699491SMichael Ellerman 	 */
1424f2699491SMichael Ellerman 	if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1425f2699491SMichael Ellerman 		mb();
1426f2699491SMichael Ellerman 		mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1427f2699491SMichael Ellerman 	}
1428f2699491SMichael Ellerman 
1429f2699491SMichael Ellerman  out:
14303925f46bSAnshuman Khandual 
1431f2699491SMichael Ellerman 	local_irq_restore(flags);
1432f2699491SMichael Ellerman }
1433f2699491SMichael Ellerman 
1434f2699491SMichael Ellerman static int collect_events(struct perf_event *group, int max_count,
1435f2699491SMichael Ellerman 			  struct perf_event *ctrs[], u64 *events,
1436f2699491SMichael Ellerman 			  unsigned int *flags)
1437f2699491SMichael Ellerman {
1438f2699491SMichael Ellerman 	int n = 0;
1439f2699491SMichael Ellerman 	struct perf_event *event;
1440f2699491SMichael Ellerman 
14415aa04b3eSRavi Bangoria 	if (group->pmu->task_ctx_nr == perf_hw_context) {
1442f2699491SMichael Ellerman 		if (n >= max_count)
1443f2699491SMichael Ellerman 			return -1;
1444f2699491SMichael Ellerman 		ctrs[n] = group;
1445f2699491SMichael Ellerman 		flags[n] = group->hw.event_base;
1446f2699491SMichael Ellerman 		events[n++] = group->hw.config;
1447f2699491SMichael Ellerman 	}
1448edb39592SPeter Zijlstra 	for_each_sibling_event(event, group) {
14495aa04b3eSRavi Bangoria 		if (event->pmu->task_ctx_nr == perf_hw_context &&
1450f2699491SMichael Ellerman 		    event->state != PERF_EVENT_STATE_OFF) {
1451f2699491SMichael Ellerman 			if (n >= max_count)
1452f2699491SMichael Ellerman 				return -1;
1453f2699491SMichael Ellerman 			ctrs[n] = event;
1454f2699491SMichael Ellerman 			flags[n] = event->hw.event_base;
1455f2699491SMichael Ellerman 			events[n++] = event->hw.config;
1456f2699491SMichael Ellerman 		}
1457f2699491SMichael Ellerman 	}
1458f2699491SMichael Ellerman 	return n;
1459f2699491SMichael Ellerman }
1460f2699491SMichael Ellerman 
1461f2699491SMichael Ellerman /*
1462788faab7STobias Tefke  * Add an event to the PMU.
1463f2699491SMichael Ellerman  * If all events are not already frozen, then we disable and
1464f2699491SMichael Ellerman  * re-enable the PMU in order to get hw_perf_enable to do the
1465f2699491SMichael Ellerman  * actual work of reconfiguring the PMU.
1466f2699491SMichael Ellerman  */
1467f2699491SMichael Ellerman static int power_pmu_add(struct perf_event *event, int ef_flags)
1468f2699491SMichael Ellerman {
1469f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1470f2699491SMichael Ellerman 	unsigned long flags;
1471f2699491SMichael Ellerman 	int n0;
1472f2699491SMichael Ellerman 	int ret = -EAGAIN;
1473f2699491SMichael Ellerman 
1474f2699491SMichael Ellerman 	local_irq_save(flags);
1475f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1476f2699491SMichael Ellerman 
1477f2699491SMichael Ellerman 	/*
1478f2699491SMichael Ellerman 	 * Add the event to the list (if there is room)
1479f2699491SMichael Ellerman 	 * and check whether the total set is still feasible.
1480f2699491SMichael Ellerman 	 */
148169111bacSChristoph Lameter 	cpuhw = this_cpu_ptr(&cpu_hw_events);
1482f2699491SMichael Ellerman 	n0 = cpuhw->n_events;
1483f2699491SMichael Ellerman 	if (n0 >= ppmu->n_counter)
1484f2699491SMichael Ellerman 		goto out;
1485f2699491SMichael Ellerman 	cpuhw->event[n0] = event;
1486f2699491SMichael Ellerman 	cpuhw->events[n0] = event->hw.config;
1487f2699491SMichael Ellerman 	cpuhw->flags[n0] = event->hw.event_base;
1488f2699491SMichael Ellerman 
1489f53d168cSsukadev@linux.vnet.ibm.com 	/*
1490f53d168cSsukadev@linux.vnet.ibm.com 	 * This event may have been disabled/stopped in record_and_restart()
1491f53d168cSsukadev@linux.vnet.ibm.com 	 * because we exceeded the ->event_limit. If re-starting the event,
1492f53d168cSsukadev@linux.vnet.ibm.com 	 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1493f53d168cSsukadev@linux.vnet.ibm.com 	 * notification is re-enabled.
1494f53d168cSsukadev@linux.vnet.ibm.com 	 */
1495f2699491SMichael Ellerman 	if (!(ef_flags & PERF_EF_START))
1496f2699491SMichael Ellerman 		event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1497f53d168cSsukadev@linux.vnet.ibm.com 	else
1498f53d168cSsukadev@linux.vnet.ibm.com 		event->hw.state = 0;
1499f2699491SMichael Ellerman 
1500f2699491SMichael Ellerman 	/*
1501f2699491SMichael Ellerman 	 * If group events scheduling transaction was started,
1502f2699491SMichael Ellerman 	 * skip the schedulability test here, it will be performed
1503f2699491SMichael Ellerman 	 * at commit time(->commit_txn) as a whole
1504f2699491SMichael Ellerman 	 */
15058f3e5684SSukadev Bhattiprolu 	if (cpuhw->txn_flags & PERF_PMU_TXN_ADD)
1506f2699491SMichael Ellerman 		goto nocheck;
1507f2699491SMichael Ellerman 
1508f2699491SMichael Ellerman 	if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1509f2699491SMichael Ellerman 		goto out;
1510f2699491SMichael Ellerman 	if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1511f2699491SMichael Ellerman 		goto out;
1512f2699491SMichael Ellerman 	event->hw.config = cpuhw->events[n0];
1513f2699491SMichael Ellerman 
1514f2699491SMichael Ellerman nocheck:
1515330a1eb7SMichael Ellerman 	ebb_event_add(event);
1516330a1eb7SMichael Ellerman 
1517f2699491SMichael Ellerman 	++cpuhw->n_events;
1518f2699491SMichael Ellerman 	++cpuhw->n_added;
1519f2699491SMichael Ellerman 
1520f2699491SMichael Ellerman 	ret = 0;
1521f2699491SMichael Ellerman  out:
1522ff3d79dcSAnshuman Khandual 	if (has_branch_stack(event)) {
15233925f46bSAnshuman Khandual 		power_pmu_bhrb_enable(event);
1524ff3d79dcSAnshuman Khandual 		cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1525ff3d79dcSAnshuman Khandual 					event->attr.branch_sample_type);
1526ff3d79dcSAnshuman Khandual 	}
15273925f46bSAnshuman Khandual 
1528f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1529f2699491SMichael Ellerman 	local_irq_restore(flags);
1530f2699491SMichael Ellerman 	return ret;
1531f2699491SMichael Ellerman }
1532f2699491SMichael Ellerman 
1533f2699491SMichael Ellerman /*
1534788faab7STobias Tefke  * Remove an event from the PMU.
1535f2699491SMichael Ellerman  */
1536f2699491SMichael Ellerman static void power_pmu_del(struct perf_event *event, int ef_flags)
1537f2699491SMichael Ellerman {
1538f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1539f2699491SMichael Ellerman 	long i;
1540f2699491SMichael Ellerman 	unsigned long flags;
1541f2699491SMichael Ellerman 
1542f2699491SMichael Ellerman 	local_irq_save(flags);
1543f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1544f2699491SMichael Ellerman 
1545f2699491SMichael Ellerman 	power_pmu_read(event);
1546f2699491SMichael Ellerman 
154769111bacSChristoph Lameter 	cpuhw = this_cpu_ptr(&cpu_hw_events);
1548f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_events; ++i) {
1549f2699491SMichael Ellerman 		if (event == cpuhw->event[i]) {
1550f2699491SMichael Ellerman 			while (++i < cpuhw->n_events) {
1551f2699491SMichael Ellerman 				cpuhw->event[i-1] = cpuhw->event[i];
1552f2699491SMichael Ellerman 				cpuhw->events[i-1] = cpuhw->events[i];
1553f2699491SMichael Ellerman 				cpuhw->flags[i-1] = cpuhw->flags[i];
1554f2699491SMichael Ellerman 			}
1555f2699491SMichael Ellerman 			--cpuhw->n_events;
1556f2699491SMichael Ellerman 			ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1557f2699491SMichael Ellerman 			if (event->hw.idx) {
1558f2699491SMichael Ellerman 				write_pmc(event->hw.idx, 0);
1559f2699491SMichael Ellerman 				event->hw.idx = 0;
1560f2699491SMichael Ellerman 			}
1561f2699491SMichael Ellerman 			perf_event_update_userpage(event);
1562f2699491SMichael Ellerman 			break;
1563f2699491SMichael Ellerman 		}
1564f2699491SMichael Ellerman 	}
1565f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_limited; ++i)
1566f2699491SMichael Ellerman 		if (event == cpuhw->limited_counter[i])
1567f2699491SMichael Ellerman 			break;
1568f2699491SMichael Ellerman 	if (i < cpuhw->n_limited) {
1569f2699491SMichael Ellerman 		while (++i < cpuhw->n_limited) {
1570f2699491SMichael Ellerman 			cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
1571f2699491SMichael Ellerman 			cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1572f2699491SMichael Ellerman 		}
1573f2699491SMichael Ellerman 		--cpuhw->n_limited;
1574f2699491SMichael Ellerman 	}
1575f2699491SMichael Ellerman 	if (cpuhw->n_events == 0) {
1576f2699491SMichael Ellerman 		/* disable exceptions if no events are running */
1577f2699491SMichael Ellerman 		cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1578f2699491SMichael Ellerman 	}
1579f2699491SMichael Ellerman 
15803925f46bSAnshuman Khandual 	if (has_branch_stack(event))
15813925f46bSAnshuman Khandual 		power_pmu_bhrb_disable(event);
15823925f46bSAnshuman Khandual 
1583f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1584f2699491SMichael Ellerman 	local_irq_restore(flags);
1585f2699491SMichael Ellerman }
1586f2699491SMichael Ellerman 
1587f2699491SMichael Ellerman /*
1588f2699491SMichael Ellerman  * POWER-PMU does not support disabling individual counters, hence
1589f2699491SMichael Ellerman  * program their cycle counter to their max value and ignore the interrupts.
1590f2699491SMichael Ellerman  */
1591f2699491SMichael Ellerman 
1592f2699491SMichael Ellerman static void power_pmu_start(struct perf_event *event, int ef_flags)
1593f2699491SMichael Ellerman {
1594f2699491SMichael Ellerman 	unsigned long flags;
1595f2699491SMichael Ellerman 	s64 left;
1596f2699491SMichael Ellerman 	unsigned long val;
1597f2699491SMichael Ellerman 
1598f2699491SMichael Ellerman 	if (!event->hw.idx || !event->hw.sample_period)
1599f2699491SMichael Ellerman 		return;
1600f2699491SMichael Ellerman 
1601f2699491SMichael Ellerman 	if (!(event->hw.state & PERF_HES_STOPPED))
1602f2699491SMichael Ellerman 		return;
1603f2699491SMichael Ellerman 
1604f2699491SMichael Ellerman 	if (ef_flags & PERF_EF_RELOAD)
1605f2699491SMichael Ellerman 		WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1606f2699491SMichael Ellerman 
1607f2699491SMichael Ellerman 	local_irq_save(flags);
1608f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1609f2699491SMichael Ellerman 
1610f2699491SMichael Ellerman 	event->hw.state = 0;
1611f2699491SMichael Ellerman 	left = local64_read(&event->hw.period_left);
1612f2699491SMichael Ellerman 
1613f2699491SMichael Ellerman 	val = 0;
1614f2699491SMichael Ellerman 	if (left < 0x80000000L)
1615f2699491SMichael Ellerman 		val = 0x80000000L - left;
1616f2699491SMichael Ellerman 
1617f2699491SMichael Ellerman 	write_pmc(event->hw.idx, val);
1618f2699491SMichael Ellerman 
1619f2699491SMichael Ellerman 	perf_event_update_userpage(event);
1620f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1621f2699491SMichael Ellerman 	local_irq_restore(flags);
1622f2699491SMichael Ellerman }
1623f2699491SMichael Ellerman 
1624f2699491SMichael Ellerman static void power_pmu_stop(struct perf_event *event, int ef_flags)
1625f2699491SMichael Ellerman {
1626f2699491SMichael Ellerman 	unsigned long flags;
1627f2699491SMichael Ellerman 
1628f2699491SMichael Ellerman 	if (!event->hw.idx || !event->hw.sample_period)
1629f2699491SMichael Ellerman 		return;
1630f2699491SMichael Ellerman 
1631f2699491SMichael Ellerman 	if (event->hw.state & PERF_HES_STOPPED)
1632f2699491SMichael Ellerman 		return;
1633f2699491SMichael Ellerman 
1634f2699491SMichael Ellerman 	local_irq_save(flags);
1635f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1636f2699491SMichael Ellerman 
1637f2699491SMichael Ellerman 	power_pmu_read(event);
1638f2699491SMichael Ellerman 	event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1639f2699491SMichael Ellerman 	write_pmc(event->hw.idx, 0);
1640f2699491SMichael Ellerman 
1641f2699491SMichael Ellerman 	perf_event_update_userpage(event);
1642f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1643f2699491SMichael Ellerman 	local_irq_restore(flags);
1644f2699491SMichael Ellerman }
1645f2699491SMichael Ellerman 
1646f2699491SMichael Ellerman /*
1647f2699491SMichael Ellerman  * Start group events scheduling transaction
1648f2699491SMichael Ellerman  * Set the flag to make pmu::enable() not perform the
1649f2699491SMichael Ellerman  * schedulability test, it will be performed at commit time
1650fbbe0701SSukadev Bhattiprolu  *
1651fbbe0701SSukadev Bhattiprolu  * We only support PERF_PMU_TXN_ADD transactions. Save the
1652fbbe0701SSukadev Bhattiprolu  * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
1653fbbe0701SSukadev Bhattiprolu  * transactions.
1654f2699491SMichael Ellerman  */
1655fbbe0701SSukadev Bhattiprolu static void power_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
1656f2699491SMichael Ellerman {
165769111bacSChristoph Lameter 	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1658f2699491SMichael Ellerman 
1659fbbe0701SSukadev Bhattiprolu 	WARN_ON_ONCE(cpuhw->txn_flags);		/* txn already in flight */
1660fbbe0701SSukadev Bhattiprolu 
1661fbbe0701SSukadev Bhattiprolu 	cpuhw->txn_flags = txn_flags;
1662fbbe0701SSukadev Bhattiprolu 	if (txn_flags & ~PERF_PMU_TXN_ADD)
1663fbbe0701SSukadev Bhattiprolu 		return;
1664fbbe0701SSukadev Bhattiprolu 
1665f2699491SMichael Ellerman 	perf_pmu_disable(pmu);
1666f2699491SMichael Ellerman 	cpuhw->n_txn_start = cpuhw->n_events;
1667f2699491SMichael Ellerman }
1668f2699491SMichael Ellerman 
1669f2699491SMichael Ellerman /*
1670f2699491SMichael Ellerman  * Stop group events scheduling transaction
1671f2699491SMichael Ellerman  * Clear the flag and pmu::enable() will perform the
1672f2699491SMichael Ellerman  * schedulability test.
1673f2699491SMichael Ellerman  */
1674e51df2c1SAnton Blanchard static void power_pmu_cancel_txn(struct pmu *pmu)
1675f2699491SMichael Ellerman {
167669111bacSChristoph Lameter 	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1677fbbe0701SSukadev Bhattiprolu 	unsigned int txn_flags;
1678fbbe0701SSukadev Bhattiprolu 
1679fbbe0701SSukadev Bhattiprolu 	WARN_ON_ONCE(!cpuhw->txn_flags);	/* no txn in flight */
1680fbbe0701SSukadev Bhattiprolu 
1681fbbe0701SSukadev Bhattiprolu 	txn_flags = cpuhw->txn_flags;
1682fbbe0701SSukadev Bhattiprolu 	cpuhw->txn_flags = 0;
1683fbbe0701SSukadev Bhattiprolu 	if (txn_flags & ~PERF_PMU_TXN_ADD)
1684fbbe0701SSukadev Bhattiprolu 		return;
1685f2699491SMichael Ellerman 
1686f2699491SMichael Ellerman 	perf_pmu_enable(pmu);
1687f2699491SMichael Ellerman }
1688f2699491SMichael Ellerman 
1689f2699491SMichael Ellerman /*
1690f2699491SMichael Ellerman  * Commit group events scheduling transaction
1691f2699491SMichael Ellerman  * Perform the group schedulability test as a whole
1692f2699491SMichael Ellerman  * Return 0 if success
1693f2699491SMichael Ellerman  */
1694e51df2c1SAnton Blanchard static int power_pmu_commit_txn(struct pmu *pmu)
1695f2699491SMichael Ellerman {
1696f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1697f2699491SMichael Ellerman 	long i, n;
1698f2699491SMichael Ellerman 
1699f2699491SMichael Ellerman 	if (!ppmu)
1700f2699491SMichael Ellerman 		return -EAGAIN;
1701fbbe0701SSukadev Bhattiprolu 
170269111bacSChristoph Lameter 	cpuhw = this_cpu_ptr(&cpu_hw_events);
1703fbbe0701SSukadev Bhattiprolu 	WARN_ON_ONCE(!cpuhw->txn_flags);	/* no txn in flight */
1704fbbe0701SSukadev Bhattiprolu 
1705fbbe0701SSukadev Bhattiprolu 	if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
1706fbbe0701SSukadev Bhattiprolu 		cpuhw->txn_flags = 0;
1707fbbe0701SSukadev Bhattiprolu 		return 0;
1708fbbe0701SSukadev Bhattiprolu 	}
1709fbbe0701SSukadev Bhattiprolu 
1710f2699491SMichael Ellerman 	n = cpuhw->n_events;
1711f2699491SMichael Ellerman 	if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1712f2699491SMichael Ellerman 		return -EAGAIN;
1713f2699491SMichael Ellerman 	i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1714f2699491SMichael Ellerman 	if (i < 0)
1715f2699491SMichael Ellerman 		return -EAGAIN;
1716f2699491SMichael Ellerman 
1717f2699491SMichael Ellerman 	for (i = cpuhw->n_txn_start; i < n; ++i)
1718f2699491SMichael Ellerman 		cpuhw->event[i]->hw.config = cpuhw->events[i];
1719f2699491SMichael Ellerman 
1720fbbe0701SSukadev Bhattiprolu 	cpuhw->txn_flags = 0;
1721f2699491SMichael Ellerman 	perf_pmu_enable(pmu);
1722f2699491SMichael Ellerman 	return 0;
1723f2699491SMichael Ellerman }
1724f2699491SMichael Ellerman 
1725f2699491SMichael Ellerman /*
1726f2699491SMichael Ellerman  * Return 1 if we might be able to put event on a limited PMC,
1727f2699491SMichael Ellerman  * or 0 if not.
1728788faab7STobias Tefke  * An event can only go on a limited PMC if it counts something
1729f2699491SMichael Ellerman  * that a limited PMC can count, doesn't require interrupts, and
1730f2699491SMichael Ellerman  * doesn't exclude any processor mode.
1731f2699491SMichael Ellerman  */
1732f2699491SMichael Ellerman static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1733f2699491SMichael Ellerman 				 unsigned int flags)
1734f2699491SMichael Ellerman {
1735f2699491SMichael Ellerman 	int n;
1736f2699491SMichael Ellerman 	u64 alt[MAX_EVENT_ALTERNATIVES];
1737f2699491SMichael Ellerman 
1738f2699491SMichael Ellerman 	if (event->attr.exclude_user
1739f2699491SMichael Ellerman 	    || event->attr.exclude_kernel
1740f2699491SMichael Ellerman 	    || event->attr.exclude_hv
1741f2699491SMichael Ellerman 	    || event->attr.sample_period)
1742f2699491SMichael Ellerman 		return 0;
1743f2699491SMichael Ellerman 
1744f2699491SMichael Ellerman 	if (ppmu->limited_pmc_event(ev))
1745f2699491SMichael Ellerman 		return 1;
1746f2699491SMichael Ellerman 
1747f2699491SMichael Ellerman 	/*
1748f2699491SMichael Ellerman 	 * The requested event_id isn't on a limited PMC already;
1749f2699491SMichael Ellerman 	 * see if any alternative code goes on a limited PMC.
1750f2699491SMichael Ellerman 	 */
1751f2699491SMichael Ellerman 	if (!ppmu->get_alternatives)
1752f2699491SMichael Ellerman 		return 0;
1753f2699491SMichael Ellerman 
1754f2699491SMichael Ellerman 	flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1755f2699491SMichael Ellerman 	n = ppmu->get_alternatives(ev, flags, alt);
1756f2699491SMichael Ellerman 
1757f2699491SMichael Ellerman 	return n > 0;
1758f2699491SMichael Ellerman }
1759f2699491SMichael Ellerman 
1760f2699491SMichael Ellerman /*
1761f2699491SMichael Ellerman  * Find an alternative event_id that goes on a normal PMC, if possible,
1762f2699491SMichael Ellerman  * and return the event_id code, or 0 if there is no such alternative.
1763f2699491SMichael Ellerman  * (Note: event_id code 0 is "don't count" on all machines.)
1764f2699491SMichael Ellerman  */
1765f2699491SMichael Ellerman static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1766f2699491SMichael Ellerman {
1767f2699491SMichael Ellerman 	u64 alt[MAX_EVENT_ALTERNATIVES];
1768f2699491SMichael Ellerman 	int n;
1769f2699491SMichael Ellerman 
1770f2699491SMichael Ellerman 	flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1771f2699491SMichael Ellerman 	n = ppmu->get_alternatives(ev, flags, alt);
1772f2699491SMichael Ellerman 	if (!n)
1773f2699491SMichael Ellerman 		return 0;
1774f2699491SMichael Ellerman 	return alt[0];
1775f2699491SMichael Ellerman }
1776f2699491SMichael Ellerman 
1777f2699491SMichael Ellerman /* Number of perf_events counting hardware events */
1778f2699491SMichael Ellerman static atomic_t num_events;
1779f2699491SMichael Ellerman /* Used to avoid races in calling reserve/release_pmc_hardware */
1780f2699491SMichael Ellerman static DEFINE_MUTEX(pmc_reserve_mutex);
1781f2699491SMichael Ellerman 
1782f2699491SMichael Ellerman /*
1783f2699491SMichael Ellerman  * Release the PMU if this is the last perf_event.
1784f2699491SMichael Ellerman  */
1785f2699491SMichael Ellerman static void hw_perf_event_destroy(struct perf_event *event)
1786f2699491SMichael Ellerman {
1787f2699491SMichael Ellerman 	if (!atomic_add_unless(&num_events, -1, 1)) {
1788f2699491SMichael Ellerman 		mutex_lock(&pmc_reserve_mutex);
1789f2699491SMichael Ellerman 		if (atomic_dec_return(&num_events) == 0)
1790f2699491SMichael Ellerman 			release_pmc_hardware();
1791f2699491SMichael Ellerman 		mutex_unlock(&pmc_reserve_mutex);
1792f2699491SMichael Ellerman 	}
1793f2699491SMichael Ellerman }
1794f2699491SMichael Ellerman 
1795f2699491SMichael Ellerman /*
1796f2699491SMichael Ellerman  * Translate a generic cache event_id config to a raw event_id code.
1797f2699491SMichael Ellerman  */
1798f2699491SMichael Ellerman static int hw_perf_cache_event(u64 config, u64 *eventp)
1799f2699491SMichael Ellerman {
1800f2699491SMichael Ellerman 	unsigned long type, op, result;
1801f2699491SMichael Ellerman 	int ev;
1802f2699491SMichael Ellerman 
1803f2699491SMichael Ellerman 	if (!ppmu->cache_events)
1804f2699491SMichael Ellerman 		return -EINVAL;
1805f2699491SMichael Ellerman 
1806f2699491SMichael Ellerman 	/* unpack config */
1807f2699491SMichael Ellerman 	type = config & 0xff;
1808f2699491SMichael Ellerman 	op = (config >> 8) & 0xff;
1809f2699491SMichael Ellerman 	result = (config >> 16) & 0xff;
1810f2699491SMichael Ellerman 
1811f2699491SMichael Ellerman 	if (type >= PERF_COUNT_HW_CACHE_MAX ||
1812f2699491SMichael Ellerman 	    op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1813f2699491SMichael Ellerman 	    result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1814f2699491SMichael Ellerman 		return -EINVAL;
1815f2699491SMichael Ellerman 
1816f2699491SMichael Ellerman 	ev = (*ppmu->cache_events)[type][op][result];
1817f2699491SMichael Ellerman 	if (ev == 0)
1818f2699491SMichael Ellerman 		return -EOPNOTSUPP;
1819f2699491SMichael Ellerman 	if (ev == -1)
1820f2699491SMichael Ellerman 		return -EINVAL;
1821f2699491SMichael Ellerman 	*eventp = ev;
1822f2699491SMichael Ellerman 	return 0;
1823f2699491SMichael Ellerman }
1824f2699491SMichael Ellerman 
1825b58064daSMadhavan Srinivasan static bool is_event_blacklisted(u64 ev)
1826b58064daSMadhavan Srinivasan {
1827b58064daSMadhavan Srinivasan 	int i;
1828b58064daSMadhavan Srinivasan 
1829b58064daSMadhavan Srinivasan 	for (i=0; i < ppmu->n_blacklist_ev; i++) {
1830b58064daSMadhavan Srinivasan 		if (ppmu->blacklist_ev[i] == ev)
1831b58064daSMadhavan Srinivasan 			return true;
1832b58064daSMadhavan Srinivasan 	}
1833b58064daSMadhavan Srinivasan 
1834b58064daSMadhavan Srinivasan 	return false;
1835b58064daSMadhavan Srinivasan }
1836b58064daSMadhavan Srinivasan 
1837f2699491SMichael Ellerman static int power_pmu_event_init(struct perf_event *event)
1838f2699491SMichael Ellerman {
1839f2699491SMichael Ellerman 	u64 ev;
1840f2699491SMichael Ellerman 	unsigned long flags;
1841f2699491SMichael Ellerman 	struct perf_event *ctrs[MAX_HWEVENTS];
1842f2699491SMichael Ellerman 	u64 events[MAX_HWEVENTS];
1843f2699491SMichael Ellerman 	unsigned int cflags[MAX_HWEVENTS];
1844f2699491SMichael Ellerman 	int n;
1845f2699491SMichael Ellerman 	int err;
1846f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
18473202e35eSRavi Bangoria 	u64 bhrb_filter;
1848f2699491SMichael Ellerman 
1849f2699491SMichael Ellerman 	if (!ppmu)
1850f2699491SMichael Ellerman 		return -ENOENT;
1851f2699491SMichael Ellerman 
18523925f46bSAnshuman Khandual 	if (has_branch_stack(event)) {
18533925f46bSAnshuman Khandual 	        /* PMU has BHRB enabled */
18544d9690ddSJoel Stanley 		if (!(ppmu->flags & PPMU_ARCH_207S))
18555375871dSLinus Torvalds 			return -EOPNOTSUPP;
18563925f46bSAnshuman Khandual 	}
18575375871dSLinus Torvalds 
1858f2699491SMichael Ellerman 	switch (event->attr.type) {
1859f2699491SMichael Ellerman 	case PERF_TYPE_HARDWARE:
1860f2699491SMichael Ellerman 		ev = event->attr.config;
1861f2699491SMichael Ellerman 		if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1862f2699491SMichael Ellerman 			return -EOPNOTSUPP;
1863b58064daSMadhavan Srinivasan 
1864b58064daSMadhavan Srinivasan 		if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1865b58064daSMadhavan Srinivasan 			return -EINVAL;
1866f2699491SMichael Ellerman 		ev = ppmu->generic_events[ev];
1867f2699491SMichael Ellerman 		break;
1868f2699491SMichael Ellerman 	case PERF_TYPE_HW_CACHE:
1869f2699491SMichael Ellerman 		err = hw_perf_cache_event(event->attr.config, &ev);
1870f2699491SMichael Ellerman 		if (err)
1871f2699491SMichael Ellerman 			return err;
1872b58064daSMadhavan Srinivasan 
1873b58064daSMadhavan Srinivasan 		if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1874b58064daSMadhavan Srinivasan 			return -EINVAL;
1875f2699491SMichael Ellerman 		break;
1876f2699491SMichael Ellerman 	case PERF_TYPE_RAW:
1877f2699491SMichael Ellerman 		ev = event->attr.config;
1878b58064daSMadhavan Srinivasan 
1879b58064daSMadhavan Srinivasan 		if (ppmu->blacklist_ev && is_event_blacklisted(ev))
1880b58064daSMadhavan Srinivasan 			return -EINVAL;
1881f2699491SMichael Ellerman 		break;
1882f2699491SMichael Ellerman 	default:
1883f2699491SMichael Ellerman 		return -ENOENT;
1884f2699491SMichael Ellerman 	}
1885f2699491SMichael Ellerman 
1886f2699491SMichael Ellerman 	event->hw.config_base = ev;
1887f2699491SMichael Ellerman 	event->hw.idx = 0;
1888f2699491SMichael Ellerman 
1889f2699491SMichael Ellerman 	/*
1890f2699491SMichael Ellerman 	 * If we are not running on a hypervisor, force the
1891f2699491SMichael Ellerman 	 * exclude_hv bit to 0 so that we don't care what
1892f2699491SMichael Ellerman 	 * the user set it to.
1893f2699491SMichael Ellerman 	 */
1894f2699491SMichael Ellerman 	if (!firmware_has_feature(FW_FEATURE_LPAR))
1895f2699491SMichael Ellerman 		event->attr.exclude_hv = 0;
1896f2699491SMichael Ellerman 
1897f2699491SMichael Ellerman 	/*
1898f2699491SMichael Ellerman 	 * If this is a per-task event, then we can use
1899f2699491SMichael Ellerman 	 * PM_RUN_* events interchangeably with their non RUN_*
1900f2699491SMichael Ellerman 	 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1901f2699491SMichael Ellerman 	 * XXX we should check if the task is an idle task.
1902f2699491SMichael Ellerman 	 */
1903f2699491SMichael Ellerman 	flags = 0;
1904f2699491SMichael Ellerman 	if (event->attach_state & PERF_ATTACH_TASK)
1905f2699491SMichael Ellerman 		flags |= PPMU_ONLY_COUNT_RUN;
1906f2699491SMichael Ellerman 
1907f2699491SMichael Ellerman 	/*
1908f2699491SMichael Ellerman 	 * If this machine has limited events, check whether this
1909f2699491SMichael Ellerman 	 * event_id could go on a limited event.
1910f2699491SMichael Ellerman 	 */
1911f2699491SMichael Ellerman 	if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1912f2699491SMichael Ellerman 		if (can_go_on_limited_pmc(event, ev, flags)) {
1913f2699491SMichael Ellerman 			flags |= PPMU_LIMITED_PMC_OK;
1914f2699491SMichael Ellerman 		} else if (ppmu->limited_pmc_event(ev)) {
1915f2699491SMichael Ellerman 			/*
1916f2699491SMichael Ellerman 			 * The requested event_id is on a limited PMC,
1917f2699491SMichael Ellerman 			 * but we can't use a limited PMC; see if any
1918f2699491SMichael Ellerman 			 * alternative goes on a normal PMC.
1919f2699491SMichael Ellerman 			 */
1920f2699491SMichael Ellerman 			ev = normal_pmc_alternative(ev, flags);
1921f2699491SMichael Ellerman 			if (!ev)
1922f2699491SMichael Ellerman 				return -EINVAL;
1923f2699491SMichael Ellerman 		}
1924f2699491SMichael Ellerman 	}
1925f2699491SMichael Ellerman 
1926330a1eb7SMichael Ellerman 	/* Extra checks for EBB */
1927330a1eb7SMichael Ellerman 	err = ebb_event_check(event);
1928330a1eb7SMichael Ellerman 	if (err)
1929330a1eb7SMichael Ellerman 		return err;
1930330a1eb7SMichael Ellerman 
1931f2699491SMichael Ellerman 	/*
1932f2699491SMichael Ellerman 	 * If this is in a group, check if it can go on with all the
1933f2699491SMichael Ellerman 	 * other hardware events in the group.  We assume the event
1934f2699491SMichael Ellerman 	 * hasn't been linked into its leader's sibling list at this point.
1935f2699491SMichael Ellerman 	 */
1936f2699491SMichael Ellerman 	n = 0;
1937f2699491SMichael Ellerman 	if (event->group_leader != event) {
1938f2699491SMichael Ellerman 		n = collect_events(event->group_leader, ppmu->n_counter - 1,
1939f2699491SMichael Ellerman 				   ctrs, events, cflags);
1940f2699491SMichael Ellerman 		if (n < 0)
1941f2699491SMichael Ellerman 			return -EINVAL;
1942f2699491SMichael Ellerman 	}
1943f2699491SMichael Ellerman 	events[n] = ev;
1944f2699491SMichael Ellerman 	ctrs[n] = event;
1945f2699491SMichael Ellerman 	cflags[n] = flags;
1946f2699491SMichael Ellerman 	if (check_excludes(ctrs, cflags, n, 1))
1947f2699491SMichael Ellerman 		return -EINVAL;
1948f2699491SMichael Ellerman 
1949f2699491SMichael Ellerman 	cpuhw = &get_cpu_var(cpu_hw_events);
1950f2699491SMichael Ellerman 	err = power_check_constraints(cpuhw, events, cflags, n + 1);
19513925f46bSAnshuman Khandual 
19523925f46bSAnshuman Khandual 	if (has_branch_stack(event)) {
19533202e35eSRavi Bangoria 		bhrb_filter = ppmu->bhrb_filter_map(
19543925f46bSAnshuman Khandual 					event->attr.branch_sample_type);
19553925f46bSAnshuman Khandual 
19563202e35eSRavi Bangoria 		if (bhrb_filter == -1) {
195768de8867SJan Stancek 			put_cpu_var(cpu_hw_events);
19583925f46bSAnshuman Khandual 			return -EOPNOTSUPP;
19593925f46bSAnshuman Khandual 		}
19603202e35eSRavi Bangoria 		cpuhw->bhrb_filter = bhrb_filter;
196168de8867SJan Stancek 	}
19623925f46bSAnshuman Khandual 
1963f2699491SMichael Ellerman 	put_cpu_var(cpu_hw_events);
1964f2699491SMichael Ellerman 	if (err)
1965f2699491SMichael Ellerman 		return -EINVAL;
1966f2699491SMichael Ellerman 
1967f2699491SMichael Ellerman 	event->hw.config = events[n];
1968f2699491SMichael Ellerman 	event->hw.event_base = cflags[n];
1969f2699491SMichael Ellerman 	event->hw.last_period = event->hw.sample_period;
1970f2699491SMichael Ellerman 	local64_set(&event->hw.period_left, event->hw.last_period);
1971f2699491SMichael Ellerman 
1972f2699491SMichael Ellerman 	/*
1973330a1eb7SMichael Ellerman 	 * For EBB events we just context switch the PMC value, we don't do any
1974330a1eb7SMichael Ellerman 	 * of the sample_period logic. We use hw.prev_count for this.
1975330a1eb7SMichael Ellerman 	 */
1976330a1eb7SMichael Ellerman 	if (is_ebb_event(event))
1977330a1eb7SMichael Ellerman 		local64_set(&event->hw.prev_count, 0);
1978330a1eb7SMichael Ellerman 
1979330a1eb7SMichael Ellerman 	/*
1980f2699491SMichael Ellerman 	 * See if we need to reserve the PMU.
1981f2699491SMichael Ellerman 	 * If no events are currently in use, then we have to take a
1982f2699491SMichael Ellerman 	 * mutex to ensure that we don't race with another task doing
1983f2699491SMichael Ellerman 	 * reserve_pmc_hardware or release_pmc_hardware.
1984f2699491SMichael Ellerman 	 */
1985f2699491SMichael Ellerman 	err = 0;
1986f2699491SMichael Ellerman 	if (!atomic_inc_not_zero(&num_events)) {
1987f2699491SMichael Ellerman 		mutex_lock(&pmc_reserve_mutex);
1988f2699491SMichael Ellerman 		if (atomic_read(&num_events) == 0 &&
1989f2699491SMichael Ellerman 		    reserve_pmc_hardware(perf_event_interrupt))
1990f2699491SMichael Ellerman 			err = -EBUSY;
1991f2699491SMichael Ellerman 		else
1992f2699491SMichael Ellerman 			atomic_inc(&num_events);
1993f2699491SMichael Ellerman 		mutex_unlock(&pmc_reserve_mutex);
1994f2699491SMichael Ellerman 	}
1995f2699491SMichael Ellerman 	event->destroy = hw_perf_event_destroy;
1996f2699491SMichael Ellerman 
1997f2699491SMichael Ellerman 	return err;
1998f2699491SMichael Ellerman }
1999f2699491SMichael Ellerman 
20005375871dSLinus Torvalds static int power_pmu_event_idx(struct perf_event *event)
20015375871dSLinus Torvalds {
20025375871dSLinus Torvalds 	return event->hw.idx;
20035375871dSLinus Torvalds }
20045375871dSLinus Torvalds 
20051c53a270SSukadev Bhattiprolu ssize_t power_events_sysfs_show(struct device *dev,
20061c53a270SSukadev Bhattiprolu 				struct device_attribute *attr, char *page)
20071c53a270SSukadev Bhattiprolu {
20081c53a270SSukadev Bhattiprolu 	struct perf_pmu_events_attr *pmu_attr;
20091c53a270SSukadev Bhattiprolu 
20101c53a270SSukadev Bhattiprolu 	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
20111c53a270SSukadev Bhattiprolu 
20121c53a270SSukadev Bhattiprolu 	return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
20131c53a270SSukadev Bhattiprolu }
20141c53a270SSukadev Bhattiprolu 
2015e51df2c1SAnton Blanchard static struct pmu power_pmu = {
2016f2699491SMichael Ellerman 	.pmu_enable	= power_pmu_enable,
2017f2699491SMichael Ellerman 	.pmu_disable	= power_pmu_disable,
2018f2699491SMichael Ellerman 	.event_init	= power_pmu_event_init,
2019f2699491SMichael Ellerman 	.add		= power_pmu_add,
2020f2699491SMichael Ellerman 	.del		= power_pmu_del,
2021f2699491SMichael Ellerman 	.start		= power_pmu_start,
2022f2699491SMichael Ellerman 	.stop		= power_pmu_stop,
2023f2699491SMichael Ellerman 	.read		= power_pmu_read,
2024f2699491SMichael Ellerman 	.start_txn	= power_pmu_start_txn,
2025f2699491SMichael Ellerman 	.cancel_txn	= power_pmu_cancel_txn,
2026f2699491SMichael Ellerman 	.commit_txn	= power_pmu_commit_txn,
20275375871dSLinus Torvalds 	.event_idx	= power_pmu_event_idx,
2028acba3c7eSPeter Zijlstra 	.sched_task	= power_pmu_sched_task,
2029f2699491SMichael Ellerman };
2030f2699491SMichael Ellerman 
2031f2699491SMichael Ellerman /*
2032f2699491SMichael Ellerman  * A counter has overflowed; update its count and record
2033f2699491SMichael Ellerman  * things if requested.  Note that interrupts are hard-disabled
2034f2699491SMichael Ellerman  * here so there is no possibility of being interrupted.
2035f2699491SMichael Ellerman  */
2036f2699491SMichael Ellerman static void record_and_restart(struct perf_event *event, unsigned long val,
2037f2699491SMichael Ellerman 			       struct pt_regs *regs)
2038f2699491SMichael Ellerman {
2039f2699491SMichael Ellerman 	u64 period = event->hw.sample_period;
2040f2699491SMichael Ellerman 	s64 prev, delta, left;
2041f2699491SMichael Ellerman 	int record = 0;
2042f2699491SMichael Ellerman 
2043f2699491SMichael Ellerman 	if (event->hw.state & PERF_HES_STOPPED) {
2044f2699491SMichael Ellerman 		write_pmc(event->hw.idx, 0);
2045f2699491SMichael Ellerman 		return;
2046f2699491SMichael Ellerman 	}
2047f2699491SMichael Ellerman 
2048f2699491SMichael Ellerman 	/* we don't have to worry about interrupts here */
2049f2699491SMichael Ellerman 	prev = local64_read(&event->hw.prev_count);
2050f2699491SMichael Ellerman 	delta = check_and_compute_delta(prev, val);
2051f2699491SMichael Ellerman 	local64_add(delta, &event->count);
2052f2699491SMichael Ellerman 
2053f2699491SMichael Ellerman 	/*
2054f2699491SMichael Ellerman 	 * See if the total period for this event has expired,
2055f2699491SMichael Ellerman 	 * and update for the next period.
2056f2699491SMichael Ellerman 	 */
2057f2699491SMichael Ellerman 	val = 0;
2058f2699491SMichael Ellerman 	left = local64_read(&event->hw.period_left) - delta;
2059e13e895fSMichael Neuling 	if (delta == 0)
2060e13e895fSMichael Neuling 		left++;
2061f2699491SMichael Ellerman 	if (period) {
2062f2699491SMichael Ellerman 		if (left <= 0) {
2063f2699491SMichael Ellerman 			left += period;
2064f2699491SMichael Ellerman 			if (left <= 0)
2065f2699491SMichael Ellerman 				left = period;
2066e6878835Ssukadev@linux.vnet.ibm.com 			record = siar_valid(regs);
2067f2699491SMichael Ellerman 			event->hw.last_period = event->hw.sample_period;
2068f2699491SMichael Ellerman 		}
2069f2699491SMichael Ellerman 		if (left < 0x80000000LL)
2070f2699491SMichael Ellerman 			val = 0x80000000LL - left;
2071f2699491SMichael Ellerman 	}
2072f2699491SMichael Ellerman 
2073f2699491SMichael Ellerman 	write_pmc(event->hw.idx, val);
2074f2699491SMichael Ellerman 	local64_set(&event->hw.prev_count, val);
2075f2699491SMichael Ellerman 	local64_set(&event->hw.period_left, left);
2076f2699491SMichael Ellerman 	perf_event_update_userpage(event);
2077f2699491SMichael Ellerman 
2078f2699491SMichael Ellerman 	/*
2079f2699491SMichael Ellerman 	 * Finally record data if requested.
2080f2699491SMichael Ellerman 	 */
2081f2699491SMichael Ellerman 	if (record) {
2082f2699491SMichael Ellerman 		struct perf_sample_data data;
2083f2699491SMichael Ellerman 
2084fd0d000bSRobert Richter 		perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
2085f2699491SMichael Ellerman 
2086fc7ce9c7SKan Liang 		if (event->attr.sample_type &
2087fc7ce9c7SKan Liang 		    (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
2088da97e184SJoel Fernandes (Google) 			perf_get_data_addr(event, regs, &data.addr);
2089f2699491SMichael Ellerman 
20903925f46bSAnshuman Khandual 		if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
20913925f46bSAnshuman Khandual 			struct cpu_hw_events *cpuhw;
209269111bacSChristoph Lameter 			cpuhw = this_cpu_ptr(&cpu_hw_events);
2093da97e184SJoel Fernandes (Google) 			power_pmu_bhrb_read(event, cpuhw);
20943925f46bSAnshuman Khandual 			data.br_stack = &cpuhw->bhrb_stack;
20953925f46bSAnshuman Khandual 		}
20963925f46bSAnshuman Khandual 
209779e96f8fSMadhavan Srinivasan 		if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
209879e96f8fSMadhavan Srinivasan 						ppmu->get_mem_data_src)
209979e96f8fSMadhavan Srinivasan 			ppmu->get_mem_data_src(&data.data_src, ppmu->flags, regs);
210079e96f8fSMadhavan Srinivasan 
2101170a315fSMadhavan Srinivasan 		if (event->attr.sample_type & PERF_SAMPLE_WEIGHT &&
2102170a315fSMadhavan Srinivasan 						ppmu->get_mem_weight)
2103170a315fSMadhavan Srinivasan 			ppmu->get_mem_weight(&data.weight);
2104170a315fSMadhavan Srinivasan 
2105f2699491SMichael Ellerman 		if (perf_event_overflow(event, &data, regs))
2106f2699491SMichael Ellerman 			power_pmu_stop(event, 0);
2107f2699491SMichael Ellerman 	}
2108f2699491SMichael Ellerman }
2109f2699491SMichael Ellerman 
2110f2699491SMichael Ellerman /*
2111f2699491SMichael Ellerman  * Called from generic code to get the misc flags (i.e. processor mode)
2112f2699491SMichael Ellerman  * for an event_id.
2113f2699491SMichael Ellerman  */
2114f2699491SMichael Ellerman unsigned long perf_misc_flags(struct pt_regs *regs)
2115f2699491SMichael Ellerman {
2116f2699491SMichael Ellerman 	u32 flags = perf_get_misc_flags(regs);
2117f2699491SMichael Ellerman 
2118f2699491SMichael Ellerman 	if (flags)
2119f2699491SMichael Ellerman 		return flags;
2120f2699491SMichael Ellerman 	return user_mode(regs) ? PERF_RECORD_MISC_USER :
2121f2699491SMichael Ellerman 		PERF_RECORD_MISC_KERNEL;
2122f2699491SMichael Ellerman }
2123f2699491SMichael Ellerman 
2124f2699491SMichael Ellerman /*
2125f2699491SMichael Ellerman  * Called from generic code to get the instruction pointer
2126f2699491SMichael Ellerman  * for an event_id.
2127f2699491SMichael Ellerman  */
2128f2699491SMichael Ellerman unsigned long perf_instruction_pointer(struct pt_regs *regs)
2129f2699491SMichael Ellerman {
213033904054SMichael Ellerman 	bool use_siar = regs_use_siar(regs);
2131f2699491SMichael Ellerman 
2132e6878835Ssukadev@linux.vnet.ibm.com 	if (use_siar && siar_valid(regs))
21331ce447b9SBenjamin Herrenschmidt 		return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
2134e6878835Ssukadev@linux.vnet.ibm.com 	else if (use_siar)
2135e6878835Ssukadev@linux.vnet.ibm.com 		return 0;		// no valid instruction pointer
213675382aa7SAnton Blanchard 	else
213775382aa7SAnton Blanchard 		return regs->nip;
2138f2699491SMichael Ellerman }
2139f2699491SMichael Ellerman 
2140bc09c219SMichael Neuling static bool pmc_overflow_power7(unsigned long val)
2141f2699491SMichael Ellerman {
2142f2699491SMichael Ellerman 	/*
2143f2699491SMichael Ellerman 	 * Events on POWER7 can roll back if a speculative event doesn't
2144f2699491SMichael Ellerman 	 * eventually complete. Unfortunately in some rare cases they will
2145f2699491SMichael Ellerman 	 * raise a performance monitor exception. We need to catch this to
2146f2699491SMichael Ellerman 	 * ensure we reset the PMC. In all cases the PMC will be 256 or less
2147f2699491SMichael Ellerman 	 * cycles from overflow.
2148f2699491SMichael Ellerman 	 *
2149f2699491SMichael Ellerman 	 * We only do this if the first pass fails to find any overflowing
2150f2699491SMichael Ellerman 	 * PMCs because a user might set a period of less than 256 and we
2151f2699491SMichael Ellerman 	 * don't want to mistakenly reset them.
2152f2699491SMichael Ellerman 	 */
2153bc09c219SMichael Neuling 	if ((0x80000000 - val) <= 256)
2154bc09c219SMichael Neuling 		return true;
2155bc09c219SMichael Neuling 
2156bc09c219SMichael Neuling 	return false;
2157bc09c219SMichael Neuling }
2158bc09c219SMichael Neuling 
2159bc09c219SMichael Neuling static bool pmc_overflow(unsigned long val)
2160bc09c219SMichael Neuling {
2161bc09c219SMichael Neuling 	if ((int)val < 0)
2162f2699491SMichael Ellerman 		return true;
2163f2699491SMichael Ellerman 
2164f2699491SMichael Ellerman 	return false;
2165f2699491SMichael Ellerman }
2166f2699491SMichael Ellerman 
2167f2699491SMichael Ellerman /*
2168f2699491SMichael Ellerman  * Performance monitor interrupt stuff
2169f2699491SMichael Ellerman  */
21700c9108b0SRavi Bangoria static void __perf_event_interrupt(struct pt_regs *regs)
2171f2699491SMichael Ellerman {
2172bc09c219SMichael Neuling 	int i, j;
217369111bacSChristoph Lameter 	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
2174f2699491SMichael Ellerman 	struct perf_event *event;
2175bc09c219SMichael Neuling 	unsigned long val[8];
2176bc09c219SMichael Neuling 	int found, active;
2177f2699491SMichael Ellerman 	int nmi;
2178f2699491SMichael Ellerman 
2179f2699491SMichael Ellerman 	if (cpuhw->n_limited)
2180f2699491SMichael Ellerman 		freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
2181f2699491SMichael Ellerman 					mfspr(SPRN_PMC6));
2182f2699491SMichael Ellerman 
2183f2699491SMichael Ellerman 	perf_read_regs(regs);
2184f2699491SMichael Ellerman 
2185f2699491SMichael Ellerman 	nmi = perf_intr_is_nmi(regs);
2186f2699491SMichael Ellerman 	if (nmi)
2187f2699491SMichael Ellerman 		nmi_enter();
2188f2699491SMichael Ellerman 	else
2189f2699491SMichael Ellerman 		irq_enter();
2190f2699491SMichael Ellerman 
2191bc09c219SMichael Neuling 	/* Read all the PMCs since we'll need them a bunch of times */
2192bc09c219SMichael Neuling 	for (i = 0; i < ppmu->n_counter; ++i)
2193bc09c219SMichael Neuling 		val[i] = read_pmc(i + 1);
2194bc09c219SMichael Neuling 
2195bc09c219SMichael Neuling 	/* Try to find what caused the IRQ */
2196bc09c219SMichael Neuling 	found = 0;
2197bc09c219SMichael Neuling 	for (i = 0; i < ppmu->n_counter; ++i) {
2198bc09c219SMichael Neuling 		if (!pmc_overflow(val[i]))
2199bc09c219SMichael Neuling 			continue;
2200bc09c219SMichael Neuling 		if (is_limited_pmc(i + 1))
2201bc09c219SMichael Neuling 			continue; /* these won't generate IRQs */
2202bc09c219SMichael Neuling 		/*
2203bc09c219SMichael Neuling 		 * We've found one that's overflowed.  For active
2204bc09c219SMichael Neuling 		 * counters we need to log this.  For inactive
2205bc09c219SMichael Neuling 		 * counters, we need to reset it anyway
2206bc09c219SMichael Neuling 		 */
2207bc09c219SMichael Neuling 		found = 1;
2208bc09c219SMichael Neuling 		active = 0;
2209bc09c219SMichael Neuling 		for (j = 0; j < cpuhw->n_events; ++j) {
2210bc09c219SMichael Neuling 			event = cpuhw->event[j];
2211bc09c219SMichael Neuling 			if (event->hw.idx == (i + 1)) {
2212bc09c219SMichael Neuling 				active = 1;
2213bc09c219SMichael Neuling 				record_and_restart(event, val[i], regs);
2214bc09c219SMichael Neuling 				break;
2215bc09c219SMichael Neuling 			}
2216bc09c219SMichael Neuling 		}
2217bc09c219SMichael Neuling 		if (!active)
2218bc09c219SMichael Neuling 			/* reset non active counters that have overflowed */
2219bc09c219SMichael Neuling 			write_pmc(i + 1, 0);
2220bc09c219SMichael Neuling 	}
2221bc09c219SMichael Neuling 	if (!found && pvr_version_is(PVR_POWER7)) {
2222bc09c219SMichael Neuling 		/* check active counters for special buggy p7 overflow */
2223f2699491SMichael Ellerman 		for (i = 0; i < cpuhw->n_events; ++i) {
2224f2699491SMichael Ellerman 			event = cpuhw->event[i];
2225f2699491SMichael Ellerman 			if (!event->hw.idx || is_limited_pmc(event->hw.idx))
2226f2699491SMichael Ellerman 				continue;
2227bc09c219SMichael Neuling 			if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2228bc09c219SMichael Neuling 				/* event has overflowed in a buggy way*/
2229f2699491SMichael Ellerman 				found = 1;
2230bc09c219SMichael Neuling 				record_and_restart(event,
2231bc09c219SMichael Neuling 						   val[event->hw.idx - 1],
2232bc09c219SMichael Neuling 						   regs);
2233f2699491SMichael Ellerman 			}
2234f2699491SMichael Ellerman 		}
2235f2699491SMichael Ellerman 	}
22366772faa1SMichael Ellerman 	if (!found && !nmi && printk_ratelimit())
2237bc09c219SMichael Neuling 		printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
2238f2699491SMichael Ellerman 
2239f2699491SMichael Ellerman 	/*
2240f2699491SMichael Ellerman 	 * Reset MMCR0 to its normal value.  This will set PMXE and
2241f2699491SMichael Ellerman 	 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
2242f2699491SMichael Ellerman 	 * and thus allow interrupts to occur again.
2243f2699491SMichael Ellerman 	 * XXX might want to use MSR.PM to keep the events frozen until
2244f2699491SMichael Ellerman 	 * we get back out of this interrupt.
2245f2699491SMichael Ellerman 	 */
2246f2699491SMichael Ellerman 	write_mmcr0(cpuhw, cpuhw->mmcr[0]);
2247f2699491SMichael Ellerman 
2248f2699491SMichael Ellerman 	if (nmi)
2249f2699491SMichael Ellerman 		nmi_exit();
2250f2699491SMichael Ellerman 	else
2251f2699491SMichael Ellerman 		irq_exit();
2252f2699491SMichael Ellerman }
2253f2699491SMichael Ellerman 
22540c9108b0SRavi Bangoria static void perf_event_interrupt(struct pt_regs *regs)
22550c9108b0SRavi Bangoria {
22560c9108b0SRavi Bangoria 	u64 start_clock = sched_clock();
22570c9108b0SRavi Bangoria 
22580c9108b0SRavi Bangoria 	__perf_event_interrupt(regs);
22590c9108b0SRavi Bangoria 	perf_sample_event_took(sched_clock() - start_clock);
22600c9108b0SRavi Bangoria }
22610c9108b0SRavi Bangoria 
22627c98bd72SDaniel Axtens static int power_pmu_prepare_cpu(unsigned int cpu)
2263f2699491SMichael Ellerman {
2264f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2265f2699491SMichael Ellerman 
226657ecde42SThomas Gleixner 	if (ppmu) {
2267f2699491SMichael Ellerman 		memset(cpuhw, 0, sizeof(*cpuhw));
2268f2699491SMichael Ellerman 		cpuhw->mmcr[0] = MMCR0_FC;
2269f2699491SMichael Ellerman 	}
227057ecde42SThomas Gleixner 	return 0;
2271f2699491SMichael Ellerman }
2272f2699491SMichael Ellerman 
2273061d19f2SPaul Gortmaker int register_power_pmu(struct power_pmu *pmu)
2274f2699491SMichael Ellerman {
2275f2699491SMichael Ellerman 	if (ppmu)
2276f2699491SMichael Ellerman 		return -EBUSY;		/* something's already registered */
2277f2699491SMichael Ellerman 
2278f2699491SMichael Ellerman 	ppmu = pmu;
2279f2699491SMichael Ellerman 	pr_info("%s performance monitor hardware support registered\n",
2280f2699491SMichael Ellerman 		pmu->name);
2281f2699491SMichael Ellerman 
22821c53a270SSukadev Bhattiprolu 	power_pmu.attr_groups = ppmu->attr_groups;
22831c53a270SSukadev Bhattiprolu 
2284f2699491SMichael Ellerman #ifdef MSR_HV
2285f2699491SMichael Ellerman 	/*
2286f2699491SMichael Ellerman 	 * Use FCHV to ignore kernel events if MSR.HV is set.
2287f2699491SMichael Ellerman 	 */
2288f2699491SMichael Ellerman 	if (mfmsr() & MSR_HV)
2289f2699491SMichael Ellerman 		freeze_events_kernel = MMCR0_FCHV;
2290f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
2291f2699491SMichael Ellerman 
2292f2699491SMichael Ellerman 	perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
229373c1b41eSThomas Gleixner 	cpuhp_setup_state(CPUHP_PERF_POWER, "perf/powerpc:prepare",
229457ecde42SThomas Gleixner 			  power_pmu_prepare_cpu, NULL);
2295f2699491SMichael Ellerman 	return 0;
2296f2699491SMichael Ellerman }
2297708597daSMadhavan Srinivasan 
2298708597daSMadhavan Srinivasan #ifdef CONFIG_PPC64
2299708597daSMadhavan Srinivasan static int __init init_ppc64_pmu(void)
2300708597daSMadhavan Srinivasan {
2301708597daSMadhavan Srinivasan 	/* run through all the pmu drivers one at a time */
2302708597daSMadhavan Srinivasan 	if (!init_power5_pmu())
2303708597daSMadhavan Srinivasan 		return 0;
2304708597daSMadhavan Srinivasan 	else if (!init_power5p_pmu())
2305708597daSMadhavan Srinivasan 		return 0;
2306708597daSMadhavan Srinivasan 	else if (!init_power6_pmu())
2307708597daSMadhavan Srinivasan 		return 0;
2308708597daSMadhavan Srinivasan 	else if (!init_power7_pmu())
2309708597daSMadhavan Srinivasan 		return 0;
2310708597daSMadhavan Srinivasan 	else if (!init_power8_pmu())
2311708597daSMadhavan Srinivasan 		return 0;
2312708597daSMadhavan Srinivasan 	else if (!init_power9_pmu())
2313708597daSMadhavan Srinivasan 		return 0;
2314708597daSMadhavan Srinivasan 	else if (!init_ppc970_pmu())
2315708597daSMadhavan Srinivasan 		return 0;
2316708597daSMadhavan Srinivasan 	else
2317be80e758SMadhavan Srinivasan 		return init_generic_compat_pmu();
2318708597daSMadhavan Srinivasan }
2319708597daSMadhavan Srinivasan early_initcall(init_ppc64_pmu);
2320708597daSMadhavan Srinivasan #endif
2321