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