xref: /openbmc/linux/arch/powerpc/perf/core-book3s.c (revision 76cb8a78)
1f2699491SMichael Ellerman /*
2f2699491SMichael Ellerman  * Performance event support - powerpc architecture code
3f2699491SMichael Ellerman  *
4f2699491SMichael Ellerman  * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
5f2699491SMichael Ellerman  *
6f2699491SMichael Ellerman  * This program is free software; you can redistribute it and/or
7f2699491SMichael Ellerman  * modify it under the terms of the GNU General Public License
8f2699491SMichael Ellerman  * as published by the Free Software Foundation; either version
9f2699491SMichael Ellerman  * 2 of the License, or (at your option) any later version.
10f2699491SMichael Ellerman  */
11f2699491SMichael Ellerman #include <linux/kernel.h>
12f2699491SMichael Ellerman #include <linux/sched.h>
13f2699491SMichael Ellerman #include <linux/perf_event.h>
14f2699491SMichael Ellerman #include <linux/percpu.h>
15f2699491SMichael Ellerman #include <linux/hardirq.h>
1669123184SMichael Neuling #include <linux/uaccess.h>
17f2699491SMichael Ellerman #include <asm/reg.h>
18f2699491SMichael Ellerman #include <asm/pmc.h>
19f2699491SMichael Ellerman #include <asm/machdep.h>
20f2699491SMichael Ellerman #include <asm/firmware.h>
21f2699491SMichael Ellerman #include <asm/ptrace.h>
2269123184SMichael Neuling #include <asm/code-patching.h>
23f2699491SMichael Ellerman 
243925f46bSAnshuman Khandual #define BHRB_MAX_ENTRIES	32
253925f46bSAnshuman Khandual #define BHRB_TARGET		0x0000000000000002
263925f46bSAnshuman Khandual #define BHRB_PREDICTION		0x0000000000000001
27b0d436c7SAnton Blanchard #define BHRB_EA			0xFFFFFFFFFFFFFFFCUL
283925f46bSAnshuman Khandual 
29f2699491SMichael Ellerman struct cpu_hw_events {
30f2699491SMichael Ellerman 	int n_events;
31f2699491SMichael Ellerman 	int n_percpu;
32f2699491SMichael Ellerman 	int disabled;
33f2699491SMichael Ellerman 	int n_added;
34f2699491SMichael Ellerman 	int n_limited;
35f2699491SMichael Ellerman 	u8  pmcs_enabled;
36f2699491SMichael Ellerman 	struct perf_event *event[MAX_HWEVENTS];
37f2699491SMichael Ellerman 	u64 events[MAX_HWEVENTS];
38f2699491SMichael Ellerman 	unsigned int flags[MAX_HWEVENTS];
39f2699491SMichael Ellerman 	unsigned long mmcr[3];
40f2699491SMichael Ellerman 	struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
41f2699491SMichael Ellerman 	u8  limited_hwidx[MAX_LIMITED_HWCOUNTERS];
42f2699491SMichael Ellerman 	u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
43f2699491SMichael Ellerman 	unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
44f2699491SMichael Ellerman 	unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
45f2699491SMichael Ellerman 
46f2699491SMichael Ellerman 	unsigned int group_flag;
47f2699491SMichael Ellerman 	int n_txn_start;
483925f46bSAnshuman Khandual 
493925f46bSAnshuman Khandual 	/* BHRB bits */
503925f46bSAnshuman Khandual 	u64				bhrb_filter;	/* BHRB HW branch filter */
513925f46bSAnshuman Khandual 	int				bhrb_users;
523925f46bSAnshuman Khandual 	void				*bhrb_context;
533925f46bSAnshuman Khandual 	struct	perf_branch_stack	bhrb_stack;
543925f46bSAnshuman Khandual 	struct	perf_branch_entry	bhrb_entries[BHRB_MAX_ENTRIES];
55f2699491SMichael Ellerman };
563925f46bSAnshuman Khandual 
57f2699491SMichael Ellerman DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
58f2699491SMichael Ellerman 
59f2699491SMichael Ellerman struct power_pmu *ppmu;
60f2699491SMichael Ellerman 
61f2699491SMichael Ellerman /*
62f2699491SMichael Ellerman  * Normally, to ignore kernel events we set the FCS (freeze counters
63f2699491SMichael Ellerman  * in supervisor mode) bit in MMCR0, but if the kernel runs with the
64f2699491SMichael Ellerman  * hypervisor bit set in the MSR, or if we are running on a processor
65f2699491SMichael Ellerman  * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
66f2699491SMichael Ellerman  * then we need to use the FCHV bit to ignore kernel events.
67f2699491SMichael Ellerman  */
68f2699491SMichael Ellerman static unsigned int freeze_events_kernel = MMCR0_FCS;
69f2699491SMichael Ellerman 
70f2699491SMichael Ellerman /*
71f2699491SMichael Ellerman  * 32-bit doesn't have MMCRA but does have an MMCR2,
72f2699491SMichael Ellerman  * and a few other names are different.
73f2699491SMichael Ellerman  */
74f2699491SMichael Ellerman #ifdef CONFIG_PPC32
75f2699491SMichael Ellerman 
76f2699491SMichael Ellerman #define MMCR0_FCHV		0
77f2699491SMichael Ellerman #define MMCR0_PMCjCE		MMCR0_PMCnCE
787a7a41f9SMichael Ellerman #define MMCR0_FC56		0
79378a6ee9SMichael Ellerman #define MMCR0_PMAO		0
80330a1eb7SMichael Ellerman #define MMCR0_EBE		0
8176cb8a78SMichael Ellerman #define MMCR0_BHRBA		0
82330a1eb7SMichael Ellerman #define MMCR0_PMCC		0
83330a1eb7SMichael Ellerman #define MMCR0_PMCC_U6		0
84f2699491SMichael Ellerman 
85f2699491SMichael Ellerman #define SPRN_MMCRA		SPRN_MMCR2
86f2699491SMichael Ellerman #define MMCRA_SAMPLE_ENABLE	0
87f2699491SMichael Ellerman 
88f2699491SMichael Ellerman static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
89f2699491SMichael Ellerman {
90f2699491SMichael Ellerman 	return 0;
91f2699491SMichael Ellerman }
92f2699491SMichael Ellerman static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
93f2699491SMichael Ellerman static inline u32 perf_get_misc_flags(struct pt_regs *regs)
94f2699491SMichael Ellerman {
95f2699491SMichael Ellerman 	return 0;
96f2699491SMichael Ellerman }
9775382aa7SAnton Blanchard static inline void perf_read_regs(struct pt_regs *regs)
9875382aa7SAnton Blanchard {
9975382aa7SAnton Blanchard 	regs->result = 0;
10075382aa7SAnton Blanchard }
101f2699491SMichael Ellerman static inline int perf_intr_is_nmi(struct pt_regs *regs)
102f2699491SMichael Ellerman {
103f2699491SMichael Ellerman 	return 0;
104f2699491SMichael Ellerman }
105f2699491SMichael Ellerman 
106e6878835Ssukadev@linux.vnet.ibm.com static inline int siar_valid(struct pt_regs *regs)
107e6878835Ssukadev@linux.vnet.ibm.com {
108e6878835Ssukadev@linux.vnet.ibm.com 	return 1;
109e6878835Ssukadev@linux.vnet.ibm.com }
110e6878835Ssukadev@linux.vnet.ibm.com 
111330a1eb7SMichael Ellerman static bool is_ebb_event(struct perf_event *event) { return false; }
112330a1eb7SMichael Ellerman static int ebb_event_check(struct perf_event *event) { return 0; }
113330a1eb7SMichael Ellerman static void ebb_event_add(struct perf_event *event) { }
114330a1eb7SMichael Ellerman static void ebb_switch_out(unsigned long mmcr0) { }
115330a1eb7SMichael Ellerman static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
116330a1eb7SMichael Ellerman {
117330a1eb7SMichael Ellerman 	return mmcr0;
118330a1eb7SMichael Ellerman }
119330a1eb7SMichael Ellerman 
120d52f2dc4SMichael Neuling static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
121d52f2dc4SMichael Neuling static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
122d52f2dc4SMichael Neuling void power_pmu_flush_branch_stack(void) {}
123d52f2dc4SMichael Neuling static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
124c2e37a26SMichael Ellerman static void pmao_restore_workaround(bool ebb) { }
125f2699491SMichael Ellerman #endif /* CONFIG_PPC32 */
126f2699491SMichael Ellerman 
12733904054SMichael Ellerman static bool regs_use_siar(struct pt_regs *regs)
12833904054SMichael Ellerman {
129cbda6aa1SMichael Ellerman 	return !!regs->result;
13033904054SMichael Ellerman }
13133904054SMichael Ellerman 
132f2699491SMichael Ellerman /*
133f2699491SMichael Ellerman  * Things that are specific to 64-bit implementations.
134f2699491SMichael Ellerman  */
135f2699491SMichael Ellerman #ifdef CONFIG_PPC64
136f2699491SMichael Ellerman 
137f2699491SMichael Ellerman static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
138f2699491SMichael Ellerman {
139f2699491SMichael Ellerman 	unsigned long mmcra = regs->dsisr;
140f2699491SMichael Ellerman 
1417a786832SMichael Ellerman 	if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
142f2699491SMichael Ellerman 		unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
143f2699491SMichael Ellerman 		if (slot > 1)
144f2699491SMichael Ellerman 			return 4 * (slot - 1);
145f2699491SMichael Ellerman 	}
1467a786832SMichael Ellerman 
147f2699491SMichael Ellerman 	return 0;
148f2699491SMichael Ellerman }
149f2699491SMichael Ellerman 
150f2699491SMichael Ellerman /*
151f2699491SMichael Ellerman  * The user wants a data address recorded.
152f2699491SMichael Ellerman  * If we're not doing instruction sampling, give them the SDAR
153f2699491SMichael Ellerman  * (sampled data address).  If we are doing instruction sampling, then
154f2699491SMichael Ellerman  * only give them the SDAR if it corresponds to the instruction
15558a032c3SMichael Ellerman  * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
15658a032c3SMichael Ellerman  * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
157f2699491SMichael Ellerman  */
158f2699491SMichael Ellerman static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
159f2699491SMichael Ellerman {
160f2699491SMichael Ellerman 	unsigned long mmcra = regs->dsisr;
16158a032c3SMichael Ellerman 	bool sdar_valid;
16258a032c3SMichael Ellerman 
16358a032c3SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
16458a032c3SMichael Ellerman 		sdar_valid = regs->dar & SIER_SDAR_VALID;
16558a032c3SMichael Ellerman 	else {
166e6878835Ssukadev@linux.vnet.ibm.com 		unsigned long sdsync;
167e6878835Ssukadev@linux.vnet.ibm.com 
168e6878835Ssukadev@linux.vnet.ibm.com 		if (ppmu->flags & PPMU_SIAR_VALID)
169e6878835Ssukadev@linux.vnet.ibm.com 			sdsync = POWER7P_MMCRA_SDAR_VALID;
170e6878835Ssukadev@linux.vnet.ibm.com 		else if (ppmu->flags & PPMU_ALT_SIPR)
171e6878835Ssukadev@linux.vnet.ibm.com 			sdsync = POWER6_MMCRA_SDSYNC;
172e6878835Ssukadev@linux.vnet.ibm.com 		else
173e6878835Ssukadev@linux.vnet.ibm.com 			sdsync = MMCRA_SDSYNC;
174f2699491SMichael Ellerman 
17558a032c3SMichael Ellerman 		sdar_valid = mmcra & sdsync;
17658a032c3SMichael Ellerman 	}
17758a032c3SMichael Ellerman 
17858a032c3SMichael Ellerman 	if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
179f2699491SMichael Ellerman 		*addrp = mfspr(SPRN_SDAR);
180f2699491SMichael Ellerman }
181f2699491SMichael Ellerman 
1825682c460SMichael Ellerman static bool regs_sihv(struct pt_regs *regs)
18368b30bb9SAnton Blanchard {
18468b30bb9SAnton Blanchard 	unsigned long sihv = MMCRA_SIHV;
18568b30bb9SAnton Blanchard 
1868f61aa32SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
1878f61aa32SMichael Ellerman 		return !!(regs->dar & SIER_SIHV);
1888f61aa32SMichael Ellerman 
18968b30bb9SAnton Blanchard 	if (ppmu->flags & PPMU_ALT_SIPR)
19068b30bb9SAnton Blanchard 		sihv = POWER6_MMCRA_SIHV;
19168b30bb9SAnton Blanchard 
1925682c460SMichael Ellerman 	return !!(regs->dsisr & sihv);
19368b30bb9SAnton Blanchard }
19468b30bb9SAnton Blanchard 
1955682c460SMichael Ellerman static bool regs_sipr(struct pt_regs *regs)
19668b30bb9SAnton Blanchard {
19768b30bb9SAnton Blanchard 	unsigned long sipr = MMCRA_SIPR;
19868b30bb9SAnton Blanchard 
1998f61aa32SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
2008f61aa32SMichael Ellerman 		return !!(regs->dar & SIER_SIPR);
2018f61aa32SMichael Ellerman 
20268b30bb9SAnton Blanchard 	if (ppmu->flags & PPMU_ALT_SIPR)
20368b30bb9SAnton Blanchard 		sipr = POWER6_MMCRA_SIPR;
20468b30bb9SAnton Blanchard 
2055682c460SMichael Ellerman 	return !!(regs->dsisr & sipr);
20668b30bb9SAnton Blanchard }
20768b30bb9SAnton Blanchard 
2081ce447b9SBenjamin Herrenschmidt static inline u32 perf_flags_from_msr(struct pt_regs *regs)
2091ce447b9SBenjamin Herrenschmidt {
2101ce447b9SBenjamin Herrenschmidt 	if (regs->msr & MSR_PR)
2111ce447b9SBenjamin Herrenschmidt 		return PERF_RECORD_MISC_USER;
2121ce447b9SBenjamin Herrenschmidt 	if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
2131ce447b9SBenjamin Herrenschmidt 		return PERF_RECORD_MISC_HYPERVISOR;
2141ce447b9SBenjamin Herrenschmidt 	return PERF_RECORD_MISC_KERNEL;
2151ce447b9SBenjamin Herrenschmidt }
2161ce447b9SBenjamin Herrenschmidt 
217f2699491SMichael Ellerman static inline u32 perf_get_misc_flags(struct pt_regs *regs)
218f2699491SMichael Ellerman {
21933904054SMichael Ellerman 	bool use_siar = regs_use_siar(regs);
220f2699491SMichael Ellerman 
22175382aa7SAnton Blanchard 	if (!use_siar)
2221ce447b9SBenjamin Herrenschmidt 		return perf_flags_from_msr(regs);
2231ce447b9SBenjamin Herrenschmidt 
2241ce447b9SBenjamin Herrenschmidt 	/*
2251ce447b9SBenjamin Herrenschmidt 	 * If we don't have flags in MMCRA, rather than using
2261ce447b9SBenjamin Herrenschmidt 	 * the MSR, we intuit the flags from the address in
2271ce447b9SBenjamin Herrenschmidt 	 * SIAR which should give slightly more reliable
2281ce447b9SBenjamin Herrenschmidt 	 * results
2291ce447b9SBenjamin Herrenschmidt 	 */
230cbda6aa1SMichael Ellerman 	if (ppmu->flags & PPMU_NO_SIPR) {
2311ce447b9SBenjamin Herrenschmidt 		unsigned long siar = mfspr(SPRN_SIAR);
2321ce447b9SBenjamin Herrenschmidt 		if (siar >= PAGE_OFFSET)
2331ce447b9SBenjamin Herrenschmidt 			return PERF_RECORD_MISC_KERNEL;
2341ce447b9SBenjamin Herrenschmidt 		return PERF_RECORD_MISC_USER;
2351ce447b9SBenjamin Herrenschmidt 	}
236f2699491SMichael Ellerman 
237f2699491SMichael Ellerman 	/* PR has priority over HV, so order below is important */
2385682c460SMichael Ellerman 	if (regs_sipr(regs))
239f2699491SMichael Ellerman 		return PERF_RECORD_MISC_USER;
2405682c460SMichael Ellerman 
2415682c460SMichael Ellerman 	if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
242f2699491SMichael Ellerman 		return PERF_RECORD_MISC_HYPERVISOR;
2435682c460SMichael Ellerman 
244f2699491SMichael Ellerman 	return PERF_RECORD_MISC_KERNEL;
245f2699491SMichael Ellerman }
246f2699491SMichael Ellerman 
247f2699491SMichael Ellerman /*
248f2699491SMichael Ellerman  * Overload regs->dsisr to store MMCRA so we only need to read it once
249f2699491SMichael Ellerman  * on each interrupt.
2508f61aa32SMichael Ellerman  * Overload regs->dar to store SIER if we have it.
25175382aa7SAnton Blanchard  * Overload regs->result to specify whether we should use the MSR (result
25275382aa7SAnton Blanchard  * is zero) or the SIAR (result is non zero).
253f2699491SMichael Ellerman  */
254f2699491SMichael Ellerman static inline void perf_read_regs(struct pt_regs *regs)
255f2699491SMichael Ellerman {
25675382aa7SAnton Blanchard 	unsigned long mmcra = mfspr(SPRN_MMCRA);
25775382aa7SAnton Blanchard 	int marked = mmcra & MMCRA_SAMPLE_ENABLE;
25875382aa7SAnton Blanchard 	int use_siar;
25975382aa7SAnton Blanchard 
2605682c460SMichael Ellerman 	regs->dsisr = mmcra;
261860aad71SMichael Ellerman 
262cbda6aa1SMichael Ellerman 	if (ppmu->flags & PPMU_HAS_SIER)
2638f61aa32SMichael Ellerman 		regs->dar = mfspr(SPRN_SIER);
2648f61aa32SMichael Ellerman 
2658f61aa32SMichael Ellerman 	/*
2665c093efaSAnton Blanchard 	 * If this isn't a PMU exception (eg a software event) the SIAR is
2675c093efaSAnton Blanchard 	 * not valid. Use pt_regs.
2685c093efaSAnton Blanchard 	 *
2695c093efaSAnton Blanchard 	 * If it is a marked event use the SIAR.
2705c093efaSAnton Blanchard 	 *
2715c093efaSAnton Blanchard 	 * If the PMU doesn't update the SIAR for non marked events use
2725c093efaSAnton Blanchard 	 * pt_regs.
2735c093efaSAnton Blanchard 	 *
2745c093efaSAnton Blanchard 	 * If the PMU has HV/PR flags then check to see if they
2755c093efaSAnton Blanchard 	 * place the exception in userspace. If so, use pt_regs. In
2765c093efaSAnton Blanchard 	 * continuous sampling mode the SIAR and the PMU exception are
2775c093efaSAnton Blanchard 	 * not synchronised, so they may be many instructions apart.
2785c093efaSAnton Blanchard 	 * This can result in confusing backtraces. We still want
2795c093efaSAnton Blanchard 	 * hypervisor samples as well as samples in the kernel with
2805c093efaSAnton Blanchard 	 * interrupts off hence the userspace check.
2815c093efaSAnton Blanchard 	 */
28275382aa7SAnton Blanchard 	if (TRAP(regs) != 0xf00)
28375382aa7SAnton Blanchard 		use_siar = 0;
2845c093efaSAnton Blanchard 	else if (marked)
2855c093efaSAnton Blanchard 		use_siar = 1;
2865c093efaSAnton Blanchard 	else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
2875c093efaSAnton Blanchard 		use_siar = 0;
288cbda6aa1SMichael Ellerman 	else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
28975382aa7SAnton Blanchard 		use_siar = 0;
29075382aa7SAnton Blanchard 	else
29175382aa7SAnton Blanchard 		use_siar = 1;
29275382aa7SAnton Blanchard 
293cbda6aa1SMichael Ellerman 	regs->result = use_siar;
294f2699491SMichael Ellerman }
295f2699491SMichael Ellerman 
296f2699491SMichael Ellerman /*
297f2699491SMichael Ellerman  * If interrupts were soft-disabled when a PMU interrupt occurs, treat
298f2699491SMichael Ellerman  * it as an NMI.
299f2699491SMichael Ellerman  */
300f2699491SMichael Ellerman static inline int perf_intr_is_nmi(struct pt_regs *regs)
301f2699491SMichael Ellerman {
302f2699491SMichael Ellerman 	return !regs->softe;
303f2699491SMichael Ellerman }
304f2699491SMichael Ellerman 
305e6878835Ssukadev@linux.vnet.ibm.com /*
306e6878835Ssukadev@linux.vnet.ibm.com  * On processors like P7+ that have the SIAR-Valid bit, marked instructions
307e6878835Ssukadev@linux.vnet.ibm.com  * must be sampled only if the SIAR-valid bit is set.
308e6878835Ssukadev@linux.vnet.ibm.com  *
309e6878835Ssukadev@linux.vnet.ibm.com  * For unmarked instructions and for processors that don't have the SIAR-Valid
310e6878835Ssukadev@linux.vnet.ibm.com  * bit, assume that SIAR is valid.
311e6878835Ssukadev@linux.vnet.ibm.com  */
312e6878835Ssukadev@linux.vnet.ibm.com static inline int siar_valid(struct pt_regs *regs)
313e6878835Ssukadev@linux.vnet.ibm.com {
314e6878835Ssukadev@linux.vnet.ibm.com 	unsigned long mmcra = regs->dsisr;
315e6878835Ssukadev@linux.vnet.ibm.com 	int marked = mmcra & MMCRA_SAMPLE_ENABLE;
316e6878835Ssukadev@linux.vnet.ibm.com 
31758a032c3SMichael Ellerman 	if (marked) {
31858a032c3SMichael Ellerman 		if (ppmu->flags & PPMU_HAS_SIER)
31958a032c3SMichael Ellerman 			return regs->dar & SIER_SIAR_VALID;
32058a032c3SMichael Ellerman 
32158a032c3SMichael Ellerman 		if (ppmu->flags & PPMU_SIAR_VALID)
322e6878835Ssukadev@linux.vnet.ibm.com 			return mmcra & POWER7P_MMCRA_SIAR_VALID;
32358a032c3SMichael Ellerman 	}
324e6878835Ssukadev@linux.vnet.ibm.com 
325e6878835Ssukadev@linux.vnet.ibm.com 	return 1;
326e6878835Ssukadev@linux.vnet.ibm.com }
327e6878835Ssukadev@linux.vnet.ibm.com 
328d52f2dc4SMichael Neuling 
329d52f2dc4SMichael Neuling /* Reset all possible BHRB entries */
330d52f2dc4SMichael Neuling static void power_pmu_bhrb_reset(void)
331d52f2dc4SMichael Neuling {
332d52f2dc4SMichael Neuling 	asm volatile(PPC_CLRBHRB);
333d52f2dc4SMichael Neuling }
334d52f2dc4SMichael Neuling 
335d52f2dc4SMichael Neuling static void power_pmu_bhrb_enable(struct perf_event *event)
336d52f2dc4SMichael Neuling {
337d52f2dc4SMichael Neuling 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
338d52f2dc4SMichael Neuling 
339d52f2dc4SMichael Neuling 	if (!ppmu->bhrb_nr)
340d52f2dc4SMichael Neuling 		return;
341d52f2dc4SMichael Neuling 
342d52f2dc4SMichael Neuling 	/* Clear BHRB if we changed task context to avoid data leaks */
343d52f2dc4SMichael Neuling 	if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
344d52f2dc4SMichael Neuling 		power_pmu_bhrb_reset();
345d52f2dc4SMichael Neuling 		cpuhw->bhrb_context = event->ctx;
346d52f2dc4SMichael Neuling 	}
347d52f2dc4SMichael Neuling 	cpuhw->bhrb_users++;
348d52f2dc4SMichael Neuling }
349d52f2dc4SMichael Neuling 
350d52f2dc4SMichael Neuling static void power_pmu_bhrb_disable(struct perf_event *event)
351d52f2dc4SMichael Neuling {
352d52f2dc4SMichael Neuling 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
353d52f2dc4SMichael Neuling 
354d52f2dc4SMichael Neuling 	if (!ppmu->bhrb_nr)
355d52f2dc4SMichael Neuling 		return;
356d52f2dc4SMichael Neuling 
357d52f2dc4SMichael Neuling 	cpuhw->bhrb_users--;
358d52f2dc4SMichael Neuling 	WARN_ON_ONCE(cpuhw->bhrb_users < 0);
359d52f2dc4SMichael Neuling 
360d52f2dc4SMichael Neuling 	if (!cpuhw->disabled && !cpuhw->bhrb_users) {
361d52f2dc4SMichael Neuling 		/* BHRB cannot be turned off when other
362d52f2dc4SMichael Neuling 		 * events are active on the PMU.
363d52f2dc4SMichael Neuling 		 */
364d52f2dc4SMichael Neuling 
365d52f2dc4SMichael Neuling 		/* avoid stale pointer */
366d52f2dc4SMichael Neuling 		cpuhw->bhrb_context = NULL;
367d52f2dc4SMichael Neuling 	}
368d52f2dc4SMichael Neuling }
369d52f2dc4SMichael Neuling 
370d52f2dc4SMichael Neuling /* Called from ctxsw to prevent one process's branch entries to
371d52f2dc4SMichael Neuling  * mingle with the other process's entries during context switch.
372d52f2dc4SMichael Neuling  */
373d52f2dc4SMichael Neuling void power_pmu_flush_branch_stack(void)
374d52f2dc4SMichael Neuling {
375d52f2dc4SMichael Neuling 	if (ppmu->bhrb_nr)
376d52f2dc4SMichael Neuling 		power_pmu_bhrb_reset();
377d52f2dc4SMichael Neuling }
37869123184SMichael Neuling /* Calculate the to address for a branch */
37969123184SMichael Neuling static __u64 power_pmu_bhrb_to(u64 addr)
38069123184SMichael Neuling {
38169123184SMichael Neuling 	unsigned int instr;
38269123184SMichael Neuling 	int ret;
38369123184SMichael Neuling 	__u64 target;
38469123184SMichael Neuling 
38569123184SMichael Neuling 	if (is_kernel_addr(addr))
38669123184SMichael Neuling 		return branch_target((unsigned int *)addr);
38769123184SMichael Neuling 
38869123184SMichael Neuling 	/* Userspace: need copy instruction here then translate it */
38969123184SMichael Neuling 	pagefault_disable();
39069123184SMichael Neuling 	ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
39169123184SMichael Neuling 	if (ret) {
39269123184SMichael Neuling 		pagefault_enable();
39369123184SMichael Neuling 		return 0;
39469123184SMichael Neuling 	}
39569123184SMichael Neuling 	pagefault_enable();
39669123184SMichael Neuling 
39769123184SMichael Neuling 	target = branch_target(&instr);
39869123184SMichael Neuling 	if ((!target) || (instr & BRANCH_ABSOLUTE))
39969123184SMichael Neuling 		return target;
40069123184SMichael Neuling 
40169123184SMichael Neuling 	/* Translate relative branch target from kernel to user address */
40269123184SMichael Neuling 	return target - (unsigned long)&instr + addr;
40369123184SMichael Neuling }
404d52f2dc4SMichael Neuling 
405d52f2dc4SMichael Neuling /* Processing BHRB entries */
406506e70d1SMichael Neuling void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
407d52f2dc4SMichael Neuling {
408d52f2dc4SMichael Neuling 	u64 val;
409d52f2dc4SMichael Neuling 	u64 addr;
410506e70d1SMichael Neuling 	int r_index, u_index, pred;
411d52f2dc4SMichael Neuling 
412d52f2dc4SMichael Neuling 	r_index = 0;
413d52f2dc4SMichael Neuling 	u_index = 0;
414d52f2dc4SMichael Neuling 	while (r_index < ppmu->bhrb_nr) {
415d52f2dc4SMichael Neuling 		/* Assembly read function */
416506e70d1SMichael Neuling 		val = read_bhrb(r_index++);
417506e70d1SMichael Neuling 		if (!val)
418d52f2dc4SMichael Neuling 			/* Terminal marker: End of valid BHRB entries */
419d52f2dc4SMichael Neuling 			break;
420506e70d1SMichael Neuling 		else {
421d52f2dc4SMichael Neuling 			addr = val & BHRB_EA;
422d52f2dc4SMichael Neuling 			pred = val & BHRB_PREDICTION;
423d52f2dc4SMichael Neuling 
424506e70d1SMichael Neuling 			if (!addr)
425506e70d1SMichael Neuling 				/* invalid entry */
426d52f2dc4SMichael Neuling 				continue;
427d52f2dc4SMichael Neuling 
428506e70d1SMichael Neuling 			/* Branches are read most recent first (ie. mfbhrb 0 is
429506e70d1SMichael Neuling 			 * the most recent branch).
430506e70d1SMichael Neuling 			 * There are two types of valid entries:
431506e70d1SMichael Neuling 			 * 1) a target entry which is the to address of a
432506e70d1SMichael Neuling 			 *    computed goto like a blr,bctr,btar.  The next
433506e70d1SMichael Neuling 			 *    entry read from the bhrb will be branch
434506e70d1SMichael Neuling 			 *    corresponding to this target (ie. the actual
435506e70d1SMichael Neuling 			 *    blr/bctr/btar instruction).
436506e70d1SMichael Neuling 			 * 2) a from address which is an actual branch.  If a
437506e70d1SMichael Neuling 			 *    target entry proceeds this, then this is the
438506e70d1SMichael Neuling 			 *    matching branch for that target.  If this is not
439506e70d1SMichael Neuling 			 *    following a target entry, then this is a branch
440506e70d1SMichael Neuling 			 *    where the target is given as an immediate field
441506e70d1SMichael Neuling 			 *    in the instruction (ie. an i or b form branch).
442506e70d1SMichael Neuling 			 *    In this case we need to read the instruction from
443506e70d1SMichael Neuling 			 *    memory to determine the target/to address.
444506e70d1SMichael Neuling 			 */
445d52f2dc4SMichael Neuling 
446d52f2dc4SMichael Neuling 			if (val & BHRB_TARGET) {
447506e70d1SMichael Neuling 				/* Target branches use two entries
448506e70d1SMichael Neuling 				 * (ie. computed gotos/XL form)
449506e70d1SMichael Neuling 				 */
450506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].to = addr;
451d52f2dc4SMichael Neuling 				cpuhw->bhrb_entries[u_index].mispred = pred;
452d52f2dc4SMichael Neuling 				cpuhw->bhrb_entries[u_index].predicted = ~pred;
453d52f2dc4SMichael Neuling 
454506e70d1SMichael Neuling 				/* Get from address in next entry */
455506e70d1SMichael Neuling 				val = read_bhrb(r_index++);
456506e70d1SMichael Neuling 				addr = val & BHRB_EA;
457506e70d1SMichael Neuling 				if (val & BHRB_TARGET) {
458506e70d1SMichael Neuling 					/* Shouldn't have two targets in a
459506e70d1SMichael Neuling 					   row.. Reset index and try again */
460506e70d1SMichael Neuling 					r_index--;
461506e70d1SMichael Neuling 					addr = 0;
462d52f2dc4SMichael Neuling 				}
463506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].from = addr;
464506e70d1SMichael Neuling 			} else {
465506e70d1SMichael Neuling 				/* Branches to immediate field
466506e70d1SMichael Neuling 				   (ie I or B form) */
467506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].from = addr;
46869123184SMichael Neuling 				cpuhw->bhrb_entries[u_index].to =
46969123184SMichael Neuling 					power_pmu_bhrb_to(addr);
470506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].mispred = pred;
471506e70d1SMichael Neuling 				cpuhw->bhrb_entries[u_index].predicted = ~pred;
472506e70d1SMichael Neuling 			}
473506e70d1SMichael Neuling 			u_index++;
474506e70d1SMichael Neuling 
475d52f2dc4SMichael Neuling 		}
476d52f2dc4SMichael Neuling 	}
477d52f2dc4SMichael Neuling 	cpuhw->bhrb_stack.nr = u_index;
478d52f2dc4SMichael Neuling 	return;
479d52f2dc4SMichael Neuling }
480d52f2dc4SMichael Neuling 
481330a1eb7SMichael Ellerman static bool is_ebb_event(struct perf_event *event)
482330a1eb7SMichael Ellerman {
483330a1eb7SMichael Ellerman 	/*
484330a1eb7SMichael Ellerman 	 * This could be a per-PMU callback, but we'd rather avoid the cost. We
485330a1eb7SMichael Ellerman 	 * check that the PMU supports EBB, meaning those that don't can still
486330a1eb7SMichael Ellerman 	 * use bit 63 of the event code for something else if they wish.
487330a1eb7SMichael Ellerman 	 */
488330a1eb7SMichael Ellerman 	return (ppmu->flags & PPMU_EBB) &&
4898d7c55d0SMichael Ellerman 	       ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
490330a1eb7SMichael Ellerman }
491330a1eb7SMichael Ellerman 
492330a1eb7SMichael Ellerman static int ebb_event_check(struct perf_event *event)
493330a1eb7SMichael Ellerman {
494330a1eb7SMichael Ellerman 	struct perf_event *leader = event->group_leader;
495330a1eb7SMichael Ellerman 
496330a1eb7SMichael Ellerman 	/* Event and group leader must agree on EBB */
497330a1eb7SMichael Ellerman 	if (is_ebb_event(leader) != is_ebb_event(event))
498330a1eb7SMichael Ellerman 		return -EINVAL;
499330a1eb7SMichael Ellerman 
500330a1eb7SMichael Ellerman 	if (is_ebb_event(event)) {
501330a1eb7SMichael Ellerman 		if (!(event->attach_state & PERF_ATTACH_TASK))
502330a1eb7SMichael Ellerman 			return -EINVAL;
503330a1eb7SMichael Ellerman 
504330a1eb7SMichael Ellerman 		if (!leader->attr.pinned || !leader->attr.exclusive)
505330a1eb7SMichael Ellerman 			return -EINVAL;
506330a1eb7SMichael Ellerman 
50758b5fb00SMichael Ellerman 		if (event->attr.freq ||
50858b5fb00SMichael Ellerman 		    event->attr.inherit ||
50958b5fb00SMichael Ellerman 		    event->attr.sample_type ||
51058b5fb00SMichael Ellerman 		    event->attr.sample_period ||
51158b5fb00SMichael Ellerman 		    event->attr.enable_on_exec)
512330a1eb7SMichael Ellerman 			return -EINVAL;
513330a1eb7SMichael Ellerman 	}
514330a1eb7SMichael Ellerman 
515330a1eb7SMichael Ellerman 	return 0;
516330a1eb7SMichael Ellerman }
517330a1eb7SMichael Ellerman 
518330a1eb7SMichael Ellerman static void ebb_event_add(struct perf_event *event)
519330a1eb7SMichael Ellerman {
520330a1eb7SMichael Ellerman 	if (!is_ebb_event(event) || current->thread.used_ebb)
521330a1eb7SMichael Ellerman 		return;
522330a1eb7SMichael Ellerman 
523330a1eb7SMichael Ellerman 	/*
524330a1eb7SMichael Ellerman 	 * IFF this is the first time we've added an EBB event, set
525330a1eb7SMichael Ellerman 	 * PMXE in the user MMCR0 so we can detect when it's cleared by
526330a1eb7SMichael Ellerman 	 * userspace. We need this so that we can context switch while
527330a1eb7SMichael Ellerman 	 * userspace is in the EBB handler (where PMXE is 0).
528330a1eb7SMichael Ellerman 	 */
529330a1eb7SMichael Ellerman 	current->thread.used_ebb = 1;
530330a1eb7SMichael Ellerman 	current->thread.mmcr0 |= MMCR0_PMXE;
531330a1eb7SMichael Ellerman }
532330a1eb7SMichael Ellerman 
533330a1eb7SMichael Ellerman static void ebb_switch_out(unsigned long mmcr0)
534330a1eb7SMichael Ellerman {
535330a1eb7SMichael Ellerman 	if (!(mmcr0 & MMCR0_EBE))
536330a1eb7SMichael Ellerman 		return;
537330a1eb7SMichael Ellerman 
538330a1eb7SMichael Ellerman 	current->thread.siar  = mfspr(SPRN_SIAR);
539330a1eb7SMichael Ellerman 	current->thread.sier  = mfspr(SPRN_SIER);
540330a1eb7SMichael Ellerman 	current->thread.sdar  = mfspr(SPRN_SDAR);
541330a1eb7SMichael Ellerman 	current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
542330a1eb7SMichael Ellerman 	current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
543330a1eb7SMichael Ellerman }
544330a1eb7SMichael Ellerman 
545330a1eb7SMichael Ellerman static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
546330a1eb7SMichael Ellerman {
547330a1eb7SMichael Ellerman 	if (!ebb)
548330a1eb7SMichael Ellerman 		goto out;
549330a1eb7SMichael Ellerman 
55076cb8a78SMichael Ellerman 	/* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
55176cb8a78SMichael Ellerman 	mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
552330a1eb7SMichael Ellerman 
553c2e37a26SMichael Ellerman 	/*
554c2e37a26SMichael Ellerman 	 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
555c2e37a26SMichael Ellerman 	 * with pmao_restore_workaround() because we may add PMAO but we never
556c2e37a26SMichael Ellerman 	 * clear it here.
557c2e37a26SMichael Ellerman 	 */
558330a1eb7SMichael Ellerman 	mmcr0 |= current->thread.mmcr0;
559330a1eb7SMichael Ellerman 
560c2e37a26SMichael Ellerman 	/*
561c2e37a26SMichael Ellerman 	 * Be careful not to set PMXE if userspace had it cleared. This is also
562c2e37a26SMichael Ellerman 	 * compatible with pmao_restore_workaround() because it has already
563c2e37a26SMichael Ellerman 	 * cleared PMXE and we leave PMAO alone.
564c2e37a26SMichael Ellerman 	 */
565330a1eb7SMichael Ellerman 	if (!(current->thread.mmcr0 & MMCR0_PMXE))
566330a1eb7SMichael Ellerman 		mmcr0 &= ~MMCR0_PMXE;
567330a1eb7SMichael Ellerman 
568330a1eb7SMichael Ellerman 	mtspr(SPRN_SIAR, current->thread.siar);
569330a1eb7SMichael Ellerman 	mtspr(SPRN_SIER, current->thread.sier);
570330a1eb7SMichael Ellerman 	mtspr(SPRN_SDAR, current->thread.sdar);
571330a1eb7SMichael Ellerman 	mtspr(SPRN_MMCR2, current->thread.mmcr2);
572330a1eb7SMichael Ellerman out:
573330a1eb7SMichael Ellerman 	return mmcr0;
574330a1eb7SMichael Ellerman }
575c2e37a26SMichael Ellerman 
576c2e37a26SMichael Ellerman static void pmao_restore_workaround(bool ebb)
577c2e37a26SMichael Ellerman {
578c2e37a26SMichael Ellerman 	unsigned pmcs[6];
579c2e37a26SMichael Ellerman 
580c2e37a26SMichael Ellerman 	if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
581c2e37a26SMichael Ellerman 		return;
582c2e37a26SMichael Ellerman 
583c2e37a26SMichael Ellerman 	/*
584c2e37a26SMichael Ellerman 	 * On POWER8E there is a hardware defect which affects the PMU context
585c2e37a26SMichael Ellerman 	 * switch logic, ie. power_pmu_disable/enable().
586c2e37a26SMichael Ellerman 	 *
587c2e37a26SMichael Ellerman 	 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
588c2e37a26SMichael Ellerman 	 * by the hardware. Sometime later the actual PMU exception is
589c2e37a26SMichael Ellerman 	 * delivered.
590c2e37a26SMichael Ellerman 	 *
591c2e37a26SMichael Ellerman 	 * If we context switch, or simply disable/enable, the PMU prior to the
592c2e37a26SMichael Ellerman 	 * exception arriving, the exception will be lost when we clear PMAO.
593c2e37a26SMichael Ellerman 	 *
594c2e37a26SMichael Ellerman 	 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
595c2e37a26SMichael Ellerman 	 * set, and this _should_ generate an exception. However because of the
596c2e37a26SMichael Ellerman 	 * defect no exception is generated when we write PMAO, and we get
597c2e37a26SMichael Ellerman 	 * stuck with no counters counting but no exception delivered.
598c2e37a26SMichael Ellerman 	 *
599c2e37a26SMichael Ellerman 	 * The workaround is to detect this case and tweak the hardware to
600c2e37a26SMichael Ellerman 	 * create another pending PMU exception.
601c2e37a26SMichael Ellerman 	 *
602c2e37a26SMichael Ellerman 	 * We do that by setting up PMC6 (cycles) for an imminent overflow and
603c2e37a26SMichael Ellerman 	 * enabling the PMU. That causes a new exception to be generated in the
604c2e37a26SMichael Ellerman 	 * chip, but we don't take it yet because we have interrupts hard
605c2e37a26SMichael Ellerman 	 * disabled. We then write back the PMU state as we want it to be seen
606c2e37a26SMichael Ellerman 	 * by the exception handler. When we reenable interrupts the exception
607c2e37a26SMichael Ellerman 	 * handler will be called and see the correct state.
608c2e37a26SMichael Ellerman 	 *
609c2e37a26SMichael Ellerman 	 * The logic is the same for EBB, except that the exception is gated by
610c2e37a26SMichael Ellerman 	 * us having interrupts hard disabled as well as the fact that we are
611c2e37a26SMichael Ellerman 	 * not in userspace. The exception is finally delivered when we return
612c2e37a26SMichael Ellerman 	 * to userspace.
613c2e37a26SMichael Ellerman 	 */
614c2e37a26SMichael Ellerman 
615c2e37a26SMichael Ellerman 	/* Only if PMAO is set and PMAO_SYNC is clear */
616c2e37a26SMichael Ellerman 	if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
617c2e37a26SMichael Ellerman 		return;
618c2e37a26SMichael Ellerman 
619c2e37a26SMichael Ellerman 	/* If we're doing EBB, only if BESCR[GE] is set */
620c2e37a26SMichael Ellerman 	if (ebb && !(current->thread.bescr & BESCR_GE))
621c2e37a26SMichael Ellerman 		return;
622c2e37a26SMichael Ellerman 
623c2e37a26SMichael Ellerman 	/*
624c2e37a26SMichael Ellerman 	 * We are already soft-disabled in power_pmu_enable(). We need to hard
625c2e37a26SMichael Ellerman 	 * enable to actually prevent the PMU exception from firing.
626c2e37a26SMichael Ellerman 	 */
627c2e37a26SMichael Ellerman 	hard_irq_disable();
628c2e37a26SMichael Ellerman 
629c2e37a26SMichael Ellerman 	/*
630c2e37a26SMichael Ellerman 	 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
631c2e37a26SMichael Ellerman 	 * Using read/write_pmc() in a for loop adds 12 function calls and
632c2e37a26SMichael Ellerman 	 * almost doubles our code size.
633c2e37a26SMichael Ellerman 	 */
634c2e37a26SMichael Ellerman 	pmcs[0] = mfspr(SPRN_PMC1);
635c2e37a26SMichael Ellerman 	pmcs[1] = mfspr(SPRN_PMC2);
636c2e37a26SMichael Ellerman 	pmcs[2] = mfspr(SPRN_PMC3);
637c2e37a26SMichael Ellerman 	pmcs[3] = mfspr(SPRN_PMC4);
638c2e37a26SMichael Ellerman 	pmcs[4] = mfspr(SPRN_PMC5);
639c2e37a26SMichael Ellerman 	pmcs[5] = mfspr(SPRN_PMC6);
640c2e37a26SMichael Ellerman 
641c2e37a26SMichael Ellerman 	/* Ensure all freeze bits are unset */
642c2e37a26SMichael Ellerman 	mtspr(SPRN_MMCR2, 0);
643c2e37a26SMichael Ellerman 
644c2e37a26SMichael Ellerman 	/* Set up PMC6 to overflow in one cycle */
645c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC6, 0x7FFFFFFE);
646c2e37a26SMichael Ellerman 
647c2e37a26SMichael Ellerman 	/* Enable exceptions and unfreeze PMC6 */
648c2e37a26SMichael Ellerman 	mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
649c2e37a26SMichael Ellerman 
650c2e37a26SMichael Ellerman 	/* Now we need to refreeze and restore the PMCs */
651c2e37a26SMichael Ellerman 	mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
652c2e37a26SMichael Ellerman 
653c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC1, pmcs[0]);
654c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC2, pmcs[1]);
655c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC3, pmcs[2]);
656c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC4, pmcs[3]);
657c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC5, pmcs[4]);
658c2e37a26SMichael Ellerman 	mtspr(SPRN_PMC6, pmcs[5]);
659c2e37a26SMichael Ellerman }
660f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
661f2699491SMichael Ellerman 
662f2699491SMichael Ellerman static void perf_event_interrupt(struct pt_regs *regs);
663f2699491SMichael Ellerman 
664f2699491SMichael Ellerman /*
665f2699491SMichael Ellerman  * Read one performance monitor counter (PMC).
666f2699491SMichael Ellerman  */
667f2699491SMichael Ellerman static unsigned long read_pmc(int idx)
668f2699491SMichael Ellerman {
669f2699491SMichael Ellerman 	unsigned long val;
670f2699491SMichael Ellerman 
671f2699491SMichael Ellerman 	switch (idx) {
672f2699491SMichael Ellerman 	case 1:
673f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC1);
674f2699491SMichael Ellerman 		break;
675f2699491SMichael Ellerman 	case 2:
676f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC2);
677f2699491SMichael Ellerman 		break;
678f2699491SMichael Ellerman 	case 3:
679f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC3);
680f2699491SMichael Ellerman 		break;
681f2699491SMichael Ellerman 	case 4:
682f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC4);
683f2699491SMichael Ellerman 		break;
684f2699491SMichael Ellerman 	case 5:
685f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC5);
686f2699491SMichael Ellerman 		break;
687f2699491SMichael Ellerman 	case 6:
688f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC6);
689f2699491SMichael Ellerman 		break;
690f2699491SMichael Ellerman #ifdef CONFIG_PPC64
691f2699491SMichael Ellerman 	case 7:
692f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC7);
693f2699491SMichael Ellerman 		break;
694f2699491SMichael Ellerman 	case 8:
695f2699491SMichael Ellerman 		val = mfspr(SPRN_PMC8);
696f2699491SMichael Ellerman 		break;
697f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
698f2699491SMichael Ellerman 	default:
699f2699491SMichael Ellerman 		printk(KERN_ERR "oops trying to read PMC%d\n", idx);
700f2699491SMichael Ellerman 		val = 0;
701f2699491SMichael Ellerman 	}
702f2699491SMichael Ellerman 	return val;
703f2699491SMichael Ellerman }
704f2699491SMichael Ellerman 
705f2699491SMichael Ellerman /*
706f2699491SMichael Ellerman  * Write one PMC.
707f2699491SMichael Ellerman  */
708f2699491SMichael Ellerman static void write_pmc(int idx, unsigned long val)
709f2699491SMichael Ellerman {
710f2699491SMichael Ellerman 	switch (idx) {
711f2699491SMichael Ellerman 	case 1:
712f2699491SMichael Ellerman 		mtspr(SPRN_PMC1, val);
713f2699491SMichael Ellerman 		break;
714f2699491SMichael Ellerman 	case 2:
715f2699491SMichael Ellerman 		mtspr(SPRN_PMC2, val);
716f2699491SMichael Ellerman 		break;
717f2699491SMichael Ellerman 	case 3:
718f2699491SMichael Ellerman 		mtspr(SPRN_PMC3, val);
719f2699491SMichael Ellerman 		break;
720f2699491SMichael Ellerman 	case 4:
721f2699491SMichael Ellerman 		mtspr(SPRN_PMC4, val);
722f2699491SMichael Ellerman 		break;
723f2699491SMichael Ellerman 	case 5:
724f2699491SMichael Ellerman 		mtspr(SPRN_PMC5, val);
725f2699491SMichael Ellerman 		break;
726f2699491SMichael Ellerman 	case 6:
727f2699491SMichael Ellerman 		mtspr(SPRN_PMC6, val);
728f2699491SMichael Ellerman 		break;
729f2699491SMichael Ellerman #ifdef CONFIG_PPC64
730f2699491SMichael Ellerman 	case 7:
731f2699491SMichael Ellerman 		mtspr(SPRN_PMC7, val);
732f2699491SMichael Ellerman 		break;
733f2699491SMichael Ellerman 	case 8:
734f2699491SMichael Ellerman 		mtspr(SPRN_PMC8, val);
735f2699491SMichael Ellerman 		break;
736f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
737f2699491SMichael Ellerman 	default:
738f2699491SMichael Ellerman 		printk(KERN_ERR "oops trying to write PMC%d\n", idx);
739f2699491SMichael Ellerman 	}
740f2699491SMichael Ellerman }
741f2699491SMichael Ellerman 
7425f6d0380SAnshuman Khandual /* Called from sysrq_handle_showregs() */
7435f6d0380SAnshuman Khandual void perf_event_print_debug(void)
7445f6d0380SAnshuman Khandual {
7455f6d0380SAnshuman Khandual 	unsigned long sdar, sier, flags;
7465f6d0380SAnshuman Khandual 	u32 pmcs[MAX_HWEVENTS];
7475f6d0380SAnshuman Khandual 	int i;
7485f6d0380SAnshuman Khandual 
7495f6d0380SAnshuman Khandual 	if (!ppmu->n_counter)
7505f6d0380SAnshuman Khandual 		return;
7515f6d0380SAnshuman Khandual 
7525f6d0380SAnshuman Khandual 	local_irq_save(flags);
7535f6d0380SAnshuman Khandual 
7545f6d0380SAnshuman Khandual 	pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
7555f6d0380SAnshuman Khandual 		 smp_processor_id(), ppmu->name, ppmu->n_counter);
7565f6d0380SAnshuman Khandual 
7575f6d0380SAnshuman Khandual 	for (i = 0; i < ppmu->n_counter; i++)
7585f6d0380SAnshuman Khandual 		pmcs[i] = read_pmc(i + 1);
7595f6d0380SAnshuman Khandual 
7605f6d0380SAnshuman Khandual 	for (; i < MAX_HWEVENTS; i++)
7615f6d0380SAnshuman Khandual 		pmcs[i] = 0xdeadbeef;
7625f6d0380SAnshuman Khandual 
7635f6d0380SAnshuman Khandual 	pr_info("PMC1:  %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
7645f6d0380SAnshuman Khandual 		 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
7655f6d0380SAnshuman Khandual 
7665f6d0380SAnshuman Khandual 	if (ppmu->n_counter > 4)
7675f6d0380SAnshuman Khandual 		pr_info("PMC5:  %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
7685f6d0380SAnshuman Khandual 			 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
7695f6d0380SAnshuman Khandual 
7705f6d0380SAnshuman Khandual 	pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
7715f6d0380SAnshuman Khandual 		mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
7725f6d0380SAnshuman Khandual 
7735f6d0380SAnshuman Khandual 	sdar = sier = 0;
7745f6d0380SAnshuman Khandual #ifdef CONFIG_PPC64
7755f6d0380SAnshuman Khandual 	sdar = mfspr(SPRN_SDAR);
7765f6d0380SAnshuman Khandual 
7775f6d0380SAnshuman Khandual 	if (ppmu->flags & PPMU_HAS_SIER)
7785f6d0380SAnshuman Khandual 		sier = mfspr(SPRN_SIER);
7795f6d0380SAnshuman Khandual 
7805f6d0380SAnshuman Khandual 	if (ppmu->flags & PPMU_EBB) {
7815f6d0380SAnshuman Khandual 		pr_info("MMCR2: %016lx EBBHR: %016lx\n",
7825f6d0380SAnshuman Khandual 			mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
7835f6d0380SAnshuman Khandual 		pr_info("EBBRR: %016lx BESCR: %016lx\n",
7845f6d0380SAnshuman Khandual 			mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
7855f6d0380SAnshuman Khandual 	}
7865f6d0380SAnshuman Khandual #endif
7875f6d0380SAnshuman Khandual 	pr_info("SIAR:  %016lx SDAR:  %016lx SIER:  %016lx\n",
7885f6d0380SAnshuman Khandual 		mfspr(SPRN_SIAR), sdar, sier);
7895f6d0380SAnshuman Khandual 
7905f6d0380SAnshuman Khandual 	local_irq_restore(flags);
7915f6d0380SAnshuman Khandual }
7925f6d0380SAnshuman Khandual 
793f2699491SMichael Ellerman /*
794f2699491SMichael Ellerman  * Check if a set of events can all go on the PMU at once.
795f2699491SMichael Ellerman  * If they can't, this will look at alternative codes for the events
796f2699491SMichael Ellerman  * and see if any combination of alternative codes is feasible.
797f2699491SMichael Ellerman  * The feasible set is returned in event_id[].
798f2699491SMichael Ellerman  */
799f2699491SMichael Ellerman static int power_check_constraints(struct cpu_hw_events *cpuhw,
800f2699491SMichael Ellerman 				   u64 event_id[], unsigned int cflags[],
801f2699491SMichael Ellerman 				   int n_ev)
802f2699491SMichael Ellerman {
803f2699491SMichael Ellerman 	unsigned long mask, value, nv;
804f2699491SMichael Ellerman 	unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
805f2699491SMichael Ellerman 	int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
806f2699491SMichael Ellerman 	int i, j;
807f2699491SMichael Ellerman 	unsigned long addf = ppmu->add_fields;
808f2699491SMichael Ellerman 	unsigned long tadd = ppmu->test_adder;
809f2699491SMichael Ellerman 
810f2699491SMichael Ellerman 	if (n_ev > ppmu->n_counter)
811f2699491SMichael Ellerman 		return -1;
812f2699491SMichael Ellerman 
813f2699491SMichael Ellerman 	/* First see if the events will go on as-is */
814f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
815f2699491SMichael Ellerman 		if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
816f2699491SMichael Ellerman 		    && !ppmu->limited_pmc_event(event_id[i])) {
817f2699491SMichael Ellerman 			ppmu->get_alternatives(event_id[i], cflags[i],
818f2699491SMichael Ellerman 					       cpuhw->alternatives[i]);
819f2699491SMichael Ellerman 			event_id[i] = cpuhw->alternatives[i][0];
820f2699491SMichael Ellerman 		}
821f2699491SMichael Ellerman 		if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
822f2699491SMichael Ellerman 					 &cpuhw->avalues[i][0]))
823f2699491SMichael Ellerman 			return -1;
824f2699491SMichael Ellerman 	}
825f2699491SMichael Ellerman 	value = mask = 0;
826f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
827f2699491SMichael Ellerman 		nv = (value | cpuhw->avalues[i][0]) +
828f2699491SMichael Ellerman 			(value & cpuhw->avalues[i][0] & addf);
829f2699491SMichael Ellerman 		if ((((nv + tadd) ^ value) & mask) != 0 ||
830f2699491SMichael Ellerman 		    (((nv + tadd) ^ cpuhw->avalues[i][0]) &
831f2699491SMichael Ellerman 		     cpuhw->amasks[i][0]) != 0)
832f2699491SMichael Ellerman 			break;
833f2699491SMichael Ellerman 		value = nv;
834f2699491SMichael Ellerman 		mask |= cpuhw->amasks[i][0];
835f2699491SMichael Ellerman 	}
836f2699491SMichael Ellerman 	if (i == n_ev)
837f2699491SMichael Ellerman 		return 0;	/* all OK */
838f2699491SMichael Ellerman 
839f2699491SMichael Ellerman 	/* doesn't work, gather alternatives... */
840f2699491SMichael Ellerman 	if (!ppmu->get_alternatives)
841f2699491SMichael Ellerman 		return -1;
842f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
843f2699491SMichael Ellerman 		choice[i] = 0;
844f2699491SMichael Ellerman 		n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
845f2699491SMichael Ellerman 						  cpuhw->alternatives[i]);
846f2699491SMichael Ellerman 		for (j = 1; j < n_alt[i]; ++j)
847f2699491SMichael Ellerman 			ppmu->get_constraint(cpuhw->alternatives[i][j],
848f2699491SMichael Ellerman 					     &cpuhw->amasks[i][j],
849f2699491SMichael Ellerman 					     &cpuhw->avalues[i][j]);
850f2699491SMichael Ellerman 	}
851f2699491SMichael Ellerman 
852f2699491SMichael Ellerman 	/* enumerate all possibilities and see if any will work */
853f2699491SMichael Ellerman 	i = 0;
854f2699491SMichael Ellerman 	j = -1;
855f2699491SMichael Ellerman 	value = mask = nv = 0;
856f2699491SMichael Ellerman 	while (i < n_ev) {
857f2699491SMichael Ellerman 		if (j >= 0) {
858f2699491SMichael Ellerman 			/* we're backtracking, restore context */
859f2699491SMichael Ellerman 			value = svalues[i];
860f2699491SMichael Ellerman 			mask = smasks[i];
861f2699491SMichael Ellerman 			j = choice[i];
862f2699491SMichael Ellerman 		}
863f2699491SMichael Ellerman 		/*
864f2699491SMichael Ellerman 		 * See if any alternative k for event_id i,
865f2699491SMichael Ellerman 		 * where k > j, will satisfy the constraints.
866f2699491SMichael Ellerman 		 */
867f2699491SMichael Ellerman 		while (++j < n_alt[i]) {
868f2699491SMichael Ellerman 			nv = (value | cpuhw->avalues[i][j]) +
869f2699491SMichael Ellerman 				(value & cpuhw->avalues[i][j] & addf);
870f2699491SMichael Ellerman 			if ((((nv + tadd) ^ value) & mask) == 0 &&
871f2699491SMichael Ellerman 			    (((nv + tadd) ^ cpuhw->avalues[i][j])
872f2699491SMichael Ellerman 			     & cpuhw->amasks[i][j]) == 0)
873f2699491SMichael Ellerman 				break;
874f2699491SMichael Ellerman 		}
875f2699491SMichael Ellerman 		if (j >= n_alt[i]) {
876f2699491SMichael Ellerman 			/*
877f2699491SMichael Ellerman 			 * No feasible alternative, backtrack
878f2699491SMichael Ellerman 			 * to event_id i-1 and continue enumerating its
879f2699491SMichael Ellerman 			 * alternatives from where we got up to.
880f2699491SMichael Ellerman 			 */
881f2699491SMichael Ellerman 			if (--i < 0)
882f2699491SMichael Ellerman 				return -1;
883f2699491SMichael Ellerman 		} else {
884f2699491SMichael Ellerman 			/*
885f2699491SMichael Ellerman 			 * Found a feasible alternative for event_id i,
886f2699491SMichael Ellerman 			 * remember where we got up to with this event_id,
887f2699491SMichael Ellerman 			 * go on to the next event_id, and start with
888f2699491SMichael Ellerman 			 * the first alternative for it.
889f2699491SMichael Ellerman 			 */
890f2699491SMichael Ellerman 			choice[i] = j;
891f2699491SMichael Ellerman 			svalues[i] = value;
892f2699491SMichael Ellerman 			smasks[i] = mask;
893f2699491SMichael Ellerman 			value = nv;
894f2699491SMichael Ellerman 			mask |= cpuhw->amasks[i][j];
895f2699491SMichael Ellerman 			++i;
896f2699491SMichael Ellerman 			j = -1;
897f2699491SMichael Ellerman 		}
898f2699491SMichael Ellerman 	}
899f2699491SMichael Ellerman 
900f2699491SMichael Ellerman 	/* OK, we have a feasible combination, tell the caller the solution */
901f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i)
902f2699491SMichael Ellerman 		event_id[i] = cpuhw->alternatives[i][choice[i]];
903f2699491SMichael Ellerman 	return 0;
904f2699491SMichael Ellerman }
905f2699491SMichael Ellerman 
906f2699491SMichael Ellerman /*
907f2699491SMichael Ellerman  * Check if newly-added events have consistent settings for
908f2699491SMichael Ellerman  * exclude_{user,kernel,hv} with each other and any previously
909f2699491SMichael Ellerman  * added events.
910f2699491SMichael Ellerman  */
911f2699491SMichael Ellerman static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
912f2699491SMichael Ellerman 			  int n_prev, int n_new)
913f2699491SMichael Ellerman {
914f2699491SMichael Ellerman 	int eu = 0, ek = 0, eh = 0;
915f2699491SMichael Ellerman 	int i, n, first;
916f2699491SMichael Ellerman 	struct perf_event *event;
917f2699491SMichael Ellerman 
918f2699491SMichael Ellerman 	n = n_prev + n_new;
919f2699491SMichael Ellerman 	if (n <= 1)
920f2699491SMichael Ellerman 		return 0;
921f2699491SMichael Ellerman 
922f2699491SMichael Ellerman 	first = 1;
923f2699491SMichael Ellerman 	for (i = 0; i < n; ++i) {
924f2699491SMichael Ellerman 		if (cflags[i] & PPMU_LIMITED_PMC_OK) {
925f2699491SMichael Ellerman 			cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
926f2699491SMichael Ellerman 			continue;
927f2699491SMichael Ellerman 		}
928f2699491SMichael Ellerman 		event = ctrs[i];
929f2699491SMichael Ellerman 		if (first) {
930f2699491SMichael Ellerman 			eu = event->attr.exclude_user;
931f2699491SMichael Ellerman 			ek = event->attr.exclude_kernel;
932f2699491SMichael Ellerman 			eh = event->attr.exclude_hv;
933f2699491SMichael Ellerman 			first = 0;
934f2699491SMichael Ellerman 		} else if (event->attr.exclude_user != eu ||
935f2699491SMichael Ellerman 			   event->attr.exclude_kernel != ek ||
936f2699491SMichael Ellerman 			   event->attr.exclude_hv != eh) {
937f2699491SMichael Ellerman 			return -EAGAIN;
938f2699491SMichael Ellerman 		}
939f2699491SMichael Ellerman 	}
940f2699491SMichael Ellerman 
941f2699491SMichael Ellerman 	if (eu || ek || eh)
942f2699491SMichael Ellerman 		for (i = 0; i < n; ++i)
943f2699491SMichael Ellerman 			if (cflags[i] & PPMU_LIMITED_PMC_OK)
944f2699491SMichael Ellerman 				cflags[i] |= PPMU_LIMITED_PMC_REQD;
945f2699491SMichael Ellerman 
946f2699491SMichael Ellerman 	return 0;
947f2699491SMichael Ellerman }
948f2699491SMichael Ellerman 
949f2699491SMichael Ellerman static u64 check_and_compute_delta(u64 prev, u64 val)
950f2699491SMichael Ellerman {
951f2699491SMichael Ellerman 	u64 delta = (val - prev) & 0xfffffffful;
952f2699491SMichael Ellerman 
953f2699491SMichael Ellerman 	/*
954f2699491SMichael Ellerman 	 * POWER7 can roll back counter values, if the new value is smaller
955f2699491SMichael Ellerman 	 * than the previous value it will cause the delta and the counter to
956f2699491SMichael Ellerman 	 * have bogus values unless we rolled a counter over.  If a coutner is
957f2699491SMichael Ellerman 	 * rolled back, it will be smaller, but within 256, which is the maximum
958f2699491SMichael Ellerman 	 * number of events to rollback at once.  If we dectect a rollback
959f2699491SMichael Ellerman 	 * return 0.  This can lead to a small lack of precision in the
960f2699491SMichael Ellerman 	 * counters.
961f2699491SMichael Ellerman 	 */
962f2699491SMichael Ellerman 	if (prev > val && (prev - val) < 256)
963f2699491SMichael Ellerman 		delta = 0;
964f2699491SMichael Ellerman 
965f2699491SMichael Ellerman 	return delta;
966f2699491SMichael Ellerman }
967f2699491SMichael Ellerman 
968f2699491SMichael Ellerman static void power_pmu_read(struct perf_event *event)
969f2699491SMichael Ellerman {
970f2699491SMichael Ellerman 	s64 val, delta, prev;
971f2699491SMichael Ellerman 
972f2699491SMichael Ellerman 	if (event->hw.state & PERF_HES_STOPPED)
973f2699491SMichael Ellerman 		return;
974f2699491SMichael Ellerman 
975f2699491SMichael Ellerman 	if (!event->hw.idx)
976f2699491SMichael Ellerman 		return;
977330a1eb7SMichael Ellerman 
978330a1eb7SMichael Ellerman 	if (is_ebb_event(event)) {
979330a1eb7SMichael Ellerman 		val = read_pmc(event->hw.idx);
980330a1eb7SMichael Ellerman 		local64_set(&event->hw.prev_count, val);
981330a1eb7SMichael Ellerman 		return;
982330a1eb7SMichael Ellerman 	}
983330a1eb7SMichael Ellerman 
984f2699491SMichael Ellerman 	/*
985f2699491SMichael Ellerman 	 * Performance monitor interrupts come even when interrupts
986f2699491SMichael Ellerman 	 * are soft-disabled, as long as interrupts are hard-enabled.
987f2699491SMichael Ellerman 	 * Therefore we treat them like NMIs.
988f2699491SMichael Ellerman 	 */
989f2699491SMichael Ellerman 	do {
990f2699491SMichael Ellerman 		prev = local64_read(&event->hw.prev_count);
991f2699491SMichael Ellerman 		barrier();
992f2699491SMichael Ellerman 		val = read_pmc(event->hw.idx);
993f2699491SMichael Ellerman 		delta = check_and_compute_delta(prev, val);
994f2699491SMichael Ellerman 		if (!delta)
995f2699491SMichael Ellerman 			return;
996f2699491SMichael Ellerman 	} while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
997f2699491SMichael Ellerman 
998f2699491SMichael Ellerman 	local64_add(delta, &event->count);
999f2699491SMichael Ellerman 	local64_sub(delta, &event->hw.period_left);
1000f2699491SMichael Ellerman }
1001f2699491SMichael Ellerman 
1002f2699491SMichael Ellerman /*
1003f2699491SMichael Ellerman  * On some machines, PMC5 and PMC6 can't be written, don't respect
1004f2699491SMichael Ellerman  * the freeze conditions, and don't generate interrupts.  This tells
1005f2699491SMichael Ellerman  * us if `event' is using such a PMC.
1006f2699491SMichael Ellerman  */
1007f2699491SMichael Ellerman static int is_limited_pmc(int pmcnum)
1008f2699491SMichael Ellerman {
1009f2699491SMichael Ellerman 	return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1010f2699491SMichael Ellerman 		&& (pmcnum == 5 || pmcnum == 6);
1011f2699491SMichael Ellerman }
1012f2699491SMichael Ellerman 
1013f2699491SMichael Ellerman static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
1014f2699491SMichael Ellerman 				    unsigned long pmc5, unsigned long pmc6)
1015f2699491SMichael Ellerman {
1016f2699491SMichael Ellerman 	struct perf_event *event;
1017f2699491SMichael Ellerman 	u64 val, prev, delta;
1018f2699491SMichael Ellerman 	int i;
1019f2699491SMichael Ellerman 
1020f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_limited; ++i) {
1021f2699491SMichael Ellerman 		event = cpuhw->limited_counter[i];
1022f2699491SMichael Ellerman 		if (!event->hw.idx)
1023f2699491SMichael Ellerman 			continue;
1024f2699491SMichael Ellerman 		val = (event->hw.idx == 5) ? pmc5 : pmc6;
1025f2699491SMichael Ellerman 		prev = local64_read(&event->hw.prev_count);
1026f2699491SMichael Ellerman 		event->hw.idx = 0;
1027f2699491SMichael Ellerman 		delta = check_and_compute_delta(prev, val);
1028f2699491SMichael Ellerman 		if (delta)
1029f2699491SMichael Ellerman 			local64_add(delta, &event->count);
1030f2699491SMichael Ellerman 	}
1031f2699491SMichael Ellerman }
1032f2699491SMichael Ellerman 
1033f2699491SMichael Ellerman static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
1034f2699491SMichael Ellerman 				  unsigned long pmc5, unsigned long pmc6)
1035f2699491SMichael Ellerman {
1036f2699491SMichael Ellerman 	struct perf_event *event;
1037f2699491SMichael Ellerman 	u64 val, prev;
1038f2699491SMichael Ellerman 	int i;
1039f2699491SMichael Ellerman 
1040f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_limited; ++i) {
1041f2699491SMichael Ellerman 		event = cpuhw->limited_counter[i];
1042f2699491SMichael Ellerman 		event->hw.idx = cpuhw->limited_hwidx[i];
1043f2699491SMichael Ellerman 		val = (event->hw.idx == 5) ? pmc5 : pmc6;
1044f2699491SMichael Ellerman 		prev = local64_read(&event->hw.prev_count);
1045f2699491SMichael Ellerman 		if (check_and_compute_delta(prev, val))
1046f2699491SMichael Ellerman 			local64_set(&event->hw.prev_count, val);
1047f2699491SMichael Ellerman 		perf_event_update_userpage(event);
1048f2699491SMichael Ellerman 	}
1049f2699491SMichael Ellerman }
1050f2699491SMichael Ellerman 
1051f2699491SMichael Ellerman /*
1052f2699491SMichael Ellerman  * Since limited events don't respect the freeze conditions, we
1053f2699491SMichael Ellerman  * have to read them immediately after freezing or unfreezing the
1054f2699491SMichael Ellerman  * other events.  We try to keep the values from the limited
1055f2699491SMichael Ellerman  * events as consistent as possible by keeping the delay (in
1056f2699491SMichael Ellerman  * cycles and instructions) between freezing/unfreezing and reading
1057f2699491SMichael Ellerman  * the limited events as small and consistent as possible.
1058f2699491SMichael Ellerman  * Therefore, if any limited events are in use, we read them
1059f2699491SMichael Ellerman  * both, and always in the same order, to minimize variability,
1060f2699491SMichael Ellerman  * and do it inside the same asm that writes MMCR0.
1061f2699491SMichael Ellerman  */
1062f2699491SMichael Ellerman static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1063f2699491SMichael Ellerman {
1064f2699491SMichael Ellerman 	unsigned long pmc5, pmc6;
1065f2699491SMichael Ellerman 
1066f2699491SMichael Ellerman 	if (!cpuhw->n_limited) {
1067f2699491SMichael Ellerman 		mtspr(SPRN_MMCR0, mmcr0);
1068f2699491SMichael Ellerman 		return;
1069f2699491SMichael Ellerman 	}
1070f2699491SMichael Ellerman 
1071f2699491SMichael Ellerman 	/*
1072f2699491SMichael Ellerman 	 * Write MMCR0, then read PMC5 and PMC6 immediately.
1073f2699491SMichael Ellerman 	 * To ensure we don't get a performance monitor interrupt
1074f2699491SMichael Ellerman 	 * between writing MMCR0 and freezing/thawing the limited
1075f2699491SMichael Ellerman 	 * events, we first write MMCR0 with the event overflow
1076f2699491SMichael Ellerman 	 * interrupt enable bits turned off.
1077f2699491SMichael Ellerman 	 */
1078f2699491SMichael Ellerman 	asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1079f2699491SMichael Ellerman 		     : "=&r" (pmc5), "=&r" (pmc6)
1080f2699491SMichael Ellerman 		     : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1081f2699491SMichael Ellerman 		       "i" (SPRN_MMCR0),
1082f2699491SMichael Ellerman 		       "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1083f2699491SMichael Ellerman 
1084f2699491SMichael Ellerman 	if (mmcr0 & MMCR0_FC)
1085f2699491SMichael Ellerman 		freeze_limited_counters(cpuhw, pmc5, pmc6);
1086f2699491SMichael Ellerman 	else
1087f2699491SMichael Ellerman 		thaw_limited_counters(cpuhw, pmc5, pmc6);
1088f2699491SMichael Ellerman 
1089f2699491SMichael Ellerman 	/*
1090f2699491SMichael Ellerman 	 * Write the full MMCR0 including the event overflow interrupt
1091f2699491SMichael Ellerman 	 * enable bits, if necessary.
1092f2699491SMichael Ellerman 	 */
1093f2699491SMichael Ellerman 	if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1094f2699491SMichael Ellerman 		mtspr(SPRN_MMCR0, mmcr0);
1095f2699491SMichael Ellerman }
1096f2699491SMichael Ellerman 
1097f2699491SMichael Ellerman /*
1098f2699491SMichael Ellerman  * Disable all events to prevent PMU interrupts and to allow
1099f2699491SMichael Ellerman  * events to be added or removed.
1100f2699491SMichael Ellerman  */
1101f2699491SMichael Ellerman static void power_pmu_disable(struct pmu *pmu)
1102f2699491SMichael Ellerman {
1103f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1104330a1eb7SMichael Ellerman 	unsigned long flags, mmcr0, val;
1105f2699491SMichael Ellerman 
1106f2699491SMichael Ellerman 	if (!ppmu)
1107f2699491SMichael Ellerman 		return;
1108f2699491SMichael Ellerman 	local_irq_save(flags);
1109f2699491SMichael Ellerman 	cpuhw = &__get_cpu_var(cpu_hw_events);
1110f2699491SMichael Ellerman 
1111f2699491SMichael Ellerman 	if (!cpuhw->disabled) {
1112f2699491SMichael Ellerman 		/*
1113f2699491SMichael Ellerman 		 * Check if we ever enabled the PMU on this cpu.
1114f2699491SMichael Ellerman 		 */
1115f2699491SMichael Ellerman 		if (!cpuhw->pmcs_enabled) {
1116f2699491SMichael Ellerman 			ppc_enable_pmcs();
1117f2699491SMichael Ellerman 			cpuhw->pmcs_enabled = 1;
1118f2699491SMichael Ellerman 		}
1119f2699491SMichael Ellerman 
1120f2699491SMichael Ellerman 		/*
112176cb8a78SMichael Ellerman 		 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
1122378a6ee9SMichael Ellerman 		 */
1123330a1eb7SMichael Ellerman 		val  = mmcr0 = mfspr(SPRN_MMCR0);
1124378a6ee9SMichael Ellerman 		val |= MMCR0_FC;
112576cb8a78SMichael Ellerman 		val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
112676cb8a78SMichael Ellerman 			 MMCR0_FC56);
1127378a6ee9SMichael Ellerman 
1128378a6ee9SMichael Ellerman 		/*
1129378a6ee9SMichael Ellerman 		 * The barrier is to make sure the mtspr has been
1130378a6ee9SMichael Ellerman 		 * executed and the PMU has frozen the events etc.
1131378a6ee9SMichael Ellerman 		 * before we return.
1132378a6ee9SMichael Ellerman 		 */
1133378a6ee9SMichael Ellerman 		write_mmcr0(cpuhw, val);
1134378a6ee9SMichael Ellerman 		mb();
1135378a6ee9SMichael Ellerman 
1136378a6ee9SMichael Ellerman 		/*
1137f2699491SMichael Ellerman 		 * Disable instruction sampling if it was enabled
1138f2699491SMichael Ellerman 		 */
1139f2699491SMichael Ellerman 		if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1140f2699491SMichael Ellerman 			mtspr(SPRN_MMCRA,
1141f2699491SMichael Ellerman 			      cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1142f2699491SMichael Ellerman 			mb();
1143f2699491SMichael Ellerman 		}
1144f2699491SMichael Ellerman 
1145378a6ee9SMichael Ellerman 		cpuhw->disabled = 1;
1146378a6ee9SMichael Ellerman 		cpuhw->n_added = 0;
1147330a1eb7SMichael Ellerman 
1148330a1eb7SMichael Ellerman 		ebb_switch_out(mmcr0);
1149f2699491SMichael Ellerman 	}
1150330a1eb7SMichael Ellerman 
1151f2699491SMichael Ellerman 	local_irq_restore(flags);
1152f2699491SMichael Ellerman }
1153f2699491SMichael Ellerman 
1154f2699491SMichael Ellerman /*
1155f2699491SMichael Ellerman  * Re-enable all events if disable == 0.
1156f2699491SMichael Ellerman  * If we were previously disabled and events were added, then
1157f2699491SMichael Ellerman  * put the new config on the PMU.
1158f2699491SMichael Ellerman  */
1159f2699491SMichael Ellerman static void power_pmu_enable(struct pmu *pmu)
1160f2699491SMichael Ellerman {
1161f2699491SMichael Ellerman 	struct perf_event *event;
1162f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1163f2699491SMichael Ellerman 	unsigned long flags;
1164f2699491SMichael Ellerman 	long i;
1165330a1eb7SMichael Ellerman 	unsigned long val, mmcr0;
1166f2699491SMichael Ellerman 	s64 left;
1167f2699491SMichael Ellerman 	unsigned int hwc_index[MAX_HWEVENTS];
1168f2699491SMichael Ellerman 	int n_lim;
1169f2699491SMichael Ellerman 	int idx;
1170330a1eb7SMichael Ellerman 	bool ebb;
1171f2699491SMichael Ellerman 
1172f2699491SMichael Ellerman 	if (!ppmu)
1173f2699491SMichael Ellerman 		return;
1174f2699491SMichael Ellerman 	local_irq_save(flags);
11750a48843dSMichael Ellerman 
1176f2699491SMichael Ellerman 	cpuhw = &__get_cpu_var(cpu_hw_events);
11770a48843dSMichael Ellerman 	if (!cpuhw->disabled)
11780a48843dSMichael Ellerman 		goto out;
11790a48843dSMichael Ellerman 
11804ea355b5SMichael Ellerman 	if (cpuhw->n_events == 0) {
11814ea355b5SMichael Ellerman 		ppc_set_pmu_inuse(0);
11824ea355b5SMichael Ellerman 		goto out;
11834ea355b5SMichael Ellerman 	}
11844ea355b5SMichael Ellerman 
1185f2699491SMichael Ellerman 	cpuhw->disabled = 0;
1186f2699491SMichael Ellerman 
1187f2699491SMichael Ellerman 	/*
1188330a1eb7SMichael Ellerman 	 * EBB requires an exclusive group and all events must have the EBB
1189330a1eb7SMichael Ellerman 	 * flag set, or not set, so we can just check a single event. Also we
1190330a1eb7SMichael Ellerman 	 * know we have at least one event.
1191330a1eb7SMichael Ellerman 	 */
1192330a1eb7SMichael Ellerman 	ebb = is_ebb_event(cpuhw->event[0]);
1193330a1eb7SMichael Ellerman 
1194330a1eb7SMichael Ellerman 	/*
1195f2699491SMichael Ellerman 	 * If we didn't change anything, or only removed events,
1196f2699491SMichael Ellerman 	 * no need to recalculate MMCR* settings and reset the PMCs.
1197f2699491SMichael Ellerman 	 * Just reenable the PMU with the current MMCR* settings
1198f2699491SMichael Ellerman 	 * (possibly updated for removal of events).
1199f2699491SMichael Ellerman 	 */
1200f2699491SMichael Ellerman 	if (!cpuhw->n_added) {
1201f2699491SMichael Ellerman 		mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1202f2699491SMichael Ellerman 		mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1203f2699491SMichael Ellerman 		goto out_enable;
1204f2699491SMichael Ellerman 	}
1205f2699491SMichael Ellerman 
1206f2699491SMichael Ellerman 	/*
1207f2699491SMichael Ellerman 	 * Compute MMCR* values for the new set of events
1208f2699491SMichael Ellerman 	 */
1209f2699491SMichael Ellerman 	if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
1210f2699491SMichael Ellerman 			       cpuhw->mmcr)) {
1211f2699491SMichael Ellerman 		/* shouldn't ever get here */
1212f2699491SMichael Ellerman 		printk(KERN_ERR "oops compute_mmcr failed\n");
1213f2699491SMichael Ellerman 		goto out;
1214f2699491SMichael Ellerman 	}
1215f2699491SMichael Ellerman 
1216f2699491SMichael Ellerman 	/*
1217f2699491SMichael Ellerman 	 * Add in MMCR0 freeze bits corresponding to the
1218f2699491SMichael Ellerman 	 * attr.exclude_* bits for the first event.
1219f2699491SMichael Ellerman 	 * We have already checked that all events have the
1220f2699491SMichael Ellerman 	 * same values for these bits as the first event.
1221f2699491SMichael Ellerman 	 */
1222f2699491SMichael Ellerman 	event = cpuhw->event[0];
1223f2699491SMichael Ellerman 	if (event->attr.exclude_user)
1224f2699491SMichael Ellerman 		cpuhw->mmcr[0] |= MMCR0_FCP;
1225f2699491SMichael Ellerman 	if (event->attr.exclude_kernel)
1226f2699491SMichael Ellerman 		cpuhw->mmcr[0] |= freeze_events_kernel;
1227f2699491SMichael Ellerman 	if (event->attr.exclude_hv)
1228f2699491SMichael Ellerman 		cpuhw->mmcr[0] |= MMCR0_FCHV;
1229f2699491SMichael Ellerman 
1230f2699491SMichael Ellerman 	/*
1231f2699491SMichael Ellerman 	 * Write the new configuration to MMCR* with the freeze
1232f2699491SMichael Ellerman 	 * bit set and set the hardware events to their initial values.
1233f2699491SMichael Ellerman 	 * Then unfreeze the events.
1234f2699491SMichael Ellerman 	 */
1235f2699491SMichael Ellerman 	ppc_set_pmu_inuse(1);
1236f2699491SMichael Ellerman 	mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1237f2699491SMichael Ellerman 	mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1238f2699491SMichael Ellerman 	mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1239f2699491SMichael Ellerman 				| MMCR0_FC);
1240f2699491SMichael Ellerman 
1241f2699491SMichael Ellerman 	/*
1242f2699491SMichael Ellerman 	 * Read off any pre-existing events that need to move
1243f2699491SMichael Ellerman 	 * to another PMC.
1244f2699491SMichael Ellerman 	 */
1245f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_events; ++i) {
1246f2699491SMichael Ellerman 		event = cpuhw->event[i];
1247f2699491SMichael Ellerman 		if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1248f2699491SMichael Ellerman 			power_pmu_read(event);
1249f2699491SMichael Ellerman 			write_pmc(event->hw.idx, 0);
1250f2699491SMichael Ellerman 			event->hw.idx = 0;
1251f2699491SMichael Ellerman 		}
1252f2699491SMichael Ellerman 	}
1253f2699491SMichael Ellerman 
1254f2699491SMichael Ellerman 	/*
1255f2699491SMichael Ellerman 	 * Initialize the PMCs for all the new and moved events.
1256f2699491SMichael Ellerman 	 */
1257f2699491SMichael Ellerman 	cpuhw->n_limited = n_lim = 0;
1258f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_events; ++i) {
1259f2699491SMichael Ellerman 		event = cpuhw->event[i];
1260f2699491SMichael Ellerman 		if (event->hw.idx)
1261f2699491SMichael Ellerman 			continue;
1262f2699491SMichael Ellerman 		idx = hwc_index[i] + 1;
1263f2699491SMichael Ellerman 		if (is_limited_pmc(idx)) {
1264f2699491SMichael Ellerman 			cpuhw->limited_counter[n_lim] = event;
1265f2699491SMichael Ellerman 			cpuhw->limited_hwidx[n_lim] = idx;
1266f2699491SMichael Ellerman 			++n_lim;
1267f2699491SMichael Ellerman 			continue;
1268f2699491SMichael Ellerman 		}
1269330a1eb7SMichael Ellerman 
1270330a1eb7SMichael Ellerman 		if (ebb)
1271330a1eb7SMichael Ellerman 			val = local64_read(&event->hw.prev_count);
1272330a1eb7SMichael Ellerman 		else {
1273f2699491SMichael Ellerman 			val = 0;
1274f2699491SMichael Ellerman 			if (event->hw.sample_period) {
1275f2699491SMichael Ellerman 				left = local64_read(&event->hw.period_left);
1276f2699491SMichael Ellerman 				if (left < 0x80000000L)
1277f2699491SMichael Ellerman 					val = 0x80000000L - left;
1278f2699491SMichael Ellerman 			}
1279f2699491SMichael Ellerman 			local64_set(&event->hw.prev_count, val);
1280330a1eb7SMichael Ellerman 		}
1281330a1eb7SMichael Ellerman 
1282f2699491SMichael Ellerman 		event->hw.idx = idx;
1283f2699491SMichael Ellerman 		if (event->hw.state & PERF_HES_STOPPED)
1284f2699491SMichael Ellerman 			val = 0;
1285f2699491SMichael Ellerman 		write_pmc(idx, val);
1286330a1eb7SMichael Ellerman 
1287f2699491SMichael Ellerman 		perf_event_update_userpage(event);
1288f2699491SMichael Ellerman 	}
1289f2699491SMichael Ellerman 	cpuhw->n_limited = n_lim;
1290f2699491SMichael Ellerman 	cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1291f2699491SMichael Ellerman 
1292f2699491SMichael Ellerman  out_enable:
1293c2e37a26SMichael Ellerman 	pmao_restore_workaround(ebb);
1294c2e37a26SMichael Ellerman 
1295330a1eb7SMichael Ellerman 	mmcr0 = ebb_switch_in(ebb, cpuhw->mmcr[0]);
1296330a1eb7SMichael Ellerman 
1297f2699491SMichael Ellerman 	mb();
1298b4d6c06cSAnshuman Khandual 	if (cpuhw->bhrb_users)
1299b4d6c06cSAnshuman Khandual 		ppmu->config_bhrb(cpuhw->bhrb_filter);
1300b4d6c06cSAnshuman Khandual 
1301330a1eb7SMichael Ellerman 	write_mmcr0(cpuhw, mmcr0);
1302f2699491SMichael Ellerman 
1303f2699491SMichael Ellerman 	/*
1304f2699491SMichael Ellerman 	 * Enable instruction sampling if necessary
1305f2699491SMichael Ellerman 	 */
1306f2699491SMichael Ellerman 	if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1307f2699491SMichael Ellerman 		mb();
1308f2699491SMichael Ellerman 		mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1309f2699491SMichael Ellerman 	}
1310f2699491SMichael Ellerman 
1311f2699491SMichael Ellerman  out:
13123925f46bSAnshuman Khandual 
1313f2699491SMichael Ellerman 	local_irq_restore(flags);
1314f2699491SMichael Ellerman }
1315f2699491SMichael Ellerman 
1316f2699491SMichael Ellerman static int collect_events(struct perf_event *group, int max_count,
1317f2699491SMichael Ellerman 			  struct perf_event *ctrs[], u64 *events,
1318f2699491SMichael Ellerman 			  unsigned int *flags)
1319f2699491SMichael Ellerman {
1320f2699491SMichael Ellerman 	int n = 0;
1321f2699491SMichael Ellerman 	struct perf_event *event;
1322f2699491SMichael Ellerman 
1323f2699491SMichael Ellerman 	if (!is_software_event(group)) {
1324f2699491SMichael Ellerman 		if (n >= max_count)
1325f2699491SMichael Ellerman 			return -1;
1326f2699491SMichael Ellerman 		ctrs[n] = group;
1327f2699491SMichael Ellerman 		flags[n] = group->hw.event_base;
1328f2699491SMichael Ellerman 		events[n++] = group->hw.config;
1329f2699491SMichael Ellerman 	}
1330f2699491SMichael Ellerman 	list_for_each_entry(event, &group->sibling_list, group_entry) {
1331f2699491SMichael Ellerman 		if (!is_software_event(event) &&
1332f2699491SMichael Ellerman 		    event->state != PERF_EVENT_STATE_OFF) {
1333f2699491SMichael Ellerman 			if (n >= max_count)
1334f2699491SMichael Ellerman 				return -1;
1335f2699491SMichael Ellerman 			ctrs[n] = event;
1336f2699491SMichael Ellerman 			flags[n] = event->hw.event_base;
1337f2699491SMichael Ellerman 			events[n++] = event->hw.config;
1338f2699491SMichael Ellerman 		}
1339f2699491SMichael Ellerman 	}
1340f2699491SMichael Ellerman 	return n;
1341f2699491SMichael Ellerman }
1342f2699491SMichael Ellerman 
1343f2699491SMichael Ellerman /*
1344f2699491SMichael Ellerman  * Add a event to the PMU.
1345f2699491SMichael Ellerman  * If all events are not already frozen, then we disable and
1346f2699491SMichael Ellerman  * re-enable the PMU in order to get hw_perf_enable to do the
1347f2699491SMichael Ellerman  * actual work of reconfiguring the PMU.
1348f2699491SMichael Ellerman  */
1349f2699491SMichael Ellerman static int power_pmu_add(struct perf_event *event, int ef_flags)
1350f2699491SMichael Ellerman {
1351f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1352f2699491SMichael Ellerman 	unsigned long flags;
1353f2699491SMichael Ellerman 	int n0;
1354f2699491SMichael Ellerman 	int ret = -EAGAIN;
1355f2699491SMichael Ellerman 
1356f2699491SMichael Ellerman 	local_irq_save(flags);
1357f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1358f2699491SMichael Ellerman 
1359f2699491SMichael Ellerman 	/*
1360f2699491SMichael Ellerman 	 * Add the event to the list (if there is room)
1361f2699491SMichael Ellerman 	 * and check whether the total set is still feasible.
1362f2699491SMichael Ellerman 	 */
1363f2699491SMichael Ellerman 	cpuhw = &__get_cpu_var(cpu_hw_events);
1364f2699491SMichael Ellerman 	n0 = cpuhw->n_events;
1365f2699491SMichael Ellerman 	if (n0 >= ppmu->n_counter)
1366f2699491SMichael Ellerman 		goto out;
1367f2699491SMichael Ellerman 	cpuhw->event[n0] = event;
1368f2699491SMichael Ellerman 	cpuhw->events[n0] = event->hw.config;
1369f2699491SMichael Ellerman 	cpuhw->flags[n0] = event->hw.event_base;
1370f2699491SMichael Ellerman 
1371f53d168cSsukadev@linux.vnet.ibm.com 	/*
1372f53d168cSsukadev@linux.vnet.ibm.com 	 * This event may have been disabled/stopped in record_and_restart()
1373f53d168cSsukadev@linux.vnet.ibm.com 	 * because we exceeded the ->event_limit. If re-starting the event,
1374f53d168cSsukadev@linux.vnet.ibm.com 	 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1375f53d168cSsukadev@linux.vnet.ibm.com 	 * notification is re-enabled.
1376f53d168cSsukadev@linux.vnet.ibm.com 	 */
1377f2699491SMichael Ellerman 	if (!(ef_flags & PERF_EF_START))
1378f2699491SMichael Ellerman 		event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1379f53d168cSsukadev@linux.vnet.ibm.com 	else
1380f53d168cSsukadev@linux.vnet.ibm.com 		event->hw.state = 0;
1381f2699491SMichael Ellerman 
1382f2699491SMichael Ellerman 	/*
1383f2699491SMichael Ellerman 	 * If group events scheduling transaction was started,
1384f2699491SMichael Ellerman 	 * skip the schedulability test here, it will be performed
1385f2699491SMichael Ellerman 	 * at commit time(->commit_txn) as a whole
1386f2699491SMichael Ellerman 	 */
1387f2699491SMichael Ellerman 	if (cpuhw->group_flag & PERF_EVENT_TXN)
1388f2699491SMichael Ellerman 		goto nocheck;
1389f2699491SMichael Ellerman 
1390f2699491SMichael Ellerman 	if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1391f2699491SMichael Ellerman 		goto out;
1392f2699491SMichael Ellerman 	if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1393f2699491SMichael Ellerman 		goto out;
1394f2699491SMichael Ellerman 	event->hw.config = cpuhw->events[n0];
1395f2699491SMichael Ellerman 
1396f2699491SMichael Ellerman nocheck:
1397330a1eb7SMichael Ellerman 	ebb_event_add(event);
1398330a1eb7SMichael Ellerman 
1399f2699491SMichael Ellerman 	++cpuhw->n_events;
1400f2699491SMichael Ellerman 	++cpuhw->n_added;
1401f2699491SMichael Ellerman 
1402f2699491SMichael Ellerman 	ret = 0;
1403f2699491SMichael Ellerman  out:
1404ff3d79dcSAnshuman Khandual 	if (has_branch_stack(event)) {
14053925f46bSAnshuman Khandual 		power_pmu_bhrb_enable(event);
1406ff3d79dcSAnshuman Khandual 		cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1407ff3d79dcSAnshuman Khandual 					event->attr.branch_sample_type);
1408ff3d79dcSAnshuman Khandual 	}
14093925f46bSAnshuman Khandual 
1410f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1411f2699491SMichael Ellerman 	local_irq_restore(flags);
1412f2699491SMichael Ellerman 	return ret;
1413f2699491SMichael Ellerman }
1414f2699491SMichael Ellerman 
1415f2699491SMichael Ellerman /*
1416f2699491SMichael Ellerman  * Remove a event from the PMU.
1417f2699491SMichael Ellerman  */
1418f2699491SMichael Ellerman static void power_pmu_del(struct perf_event *event, int ef_flags)
1419f2699491SMichael Ellerman {
1420f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1421f2699491SMichael Ellerman 	long i;
1422f2699491SMichael Ellerman 	unsigned long flags;
1423f2699491SMichael Ellerman 
1424f2699491SMichael Ellerman 	local_irq_save(flags);
1425f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1426f2699491SMichael Ellerman 
1427f2699491SMichael Ellerman 	power_pmu_read(event);
1428f2699491SMichael Ellerman 
1429f2699491SMichael Ellerman 	cpuhw = &__get_cpu_var(cpu_hw_events);
1430f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_events; ++i) {
1431f2699491SMichael Ellerman 		if (event == cpuhw->event[i]) {
1432f2699491SMichael Ellerman 			while (++i < cpuhw->n_events) {
1433f2699491SMichael Ellerman 				cpuhw->event[i-1] = cpuhw->event[i];
1434f2699491SMichael Ellerman 				cpuhw->events[i-1] = cpuhw->events[i];
1435f2699491SMichael Ellerman 				cpuhw->flags[i-1] = cpuhw->flags[i];
1436f2699491SMichael Ellerman 			}
1437f2699491SMichael Ellerman 			--cpuhw->n_events;
1438f2699491SMichael Ellerman 			ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1439f2699491SMichael Ellerman 			if (event->hw.idx) {
1440f2699491SMichael Ellerman 				write_pmc(event->hw.idx, 0);
1441f2699491SMichael Ellerman 				event->hw.idx = 0;
1442f2699491SMichael Ellerman 			}
1443f2699491SMichael Ellerman 			perf_event_update_userpage(event);
1444f2699491SMichael Ellerman 			break;
1445f2699491SMichael Ellerman 		}
1446f2699491SMichael Ellerman 	}
1447f2699491SMichael Ellerman 	for (i = 0; i < cpuhw->n_limited; ++i)
1448f2699491SMichael Ellerman 		if (event == cpuhw->limited_counter[i])
1449f2699491SMichael Ellerman 			break;
1450f2699491SMichael Ellerman 	if (i < cpuhw->n_limited) {
1451f2699491SMichael Ellerman 		while (++i < cpuhw->n_limited) {
1452f2699491SMichael Ellerman 			cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
1453f2699491SMichael Ellerman 			cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1454f2699491SMichael Ellerman 		}
1455f2699491SMichael Ellerman 		--cpuhw->n_limited;
1456f2699491SMichael Ellerman 	}
1457f2699491SMichael Ellerman 	if (cpuhw->n_events == 0) {
1458f2699491SMichael Ellerman 		/* disable exceptions if no events are running */
1459f2699491SMichael Ellerman 		cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1460f2699491SMichael Ellerman 	}
1461f2699491SMichael Ellerman 
14623925f46bSAnshuman Khandual 	if (has_branch_stack(event))
14633925f46bSAnshuman Khandual 		power_pmu_bhrb_disable(event);
14643925f46bSAnshuman Khandual 
1465f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1466f2699491SMichael Ellerman 	local_irq_restore(flags);
1467f2699491SMichael Ellerman }
1468f2699491SMichael Ellerman 
1469f2699491SMichael Ellerman /*
1470f2699491SMichael Ellerman  * POWER-PMU does not support disabling individual counters, hence
1471f2699491SMichael Ellerman  * program their cycle counter to their max value and ignore the interrupts.
1472f2699491SMichael Ellerman  */
1473f2699491SMichael Ellerman 
1474f2699491SMichael Ellerman static void power_pmu_start(struct perf_event *event, int ef_flags)
1475f2699491SMichael Ellerman {
1476f2699491SMichael Ellerman 	unsigned long flags;
1477f2699491SMichael Ellerman 	s64 left;
1478f2699491SMichael Ellerman 	unsigned long val;
1479f2699491SMichael Ellerman 
1480f2699491SMichael Ellerman 	if (!event->hw.idx || !event->hw.sample_period)
1481f2699491SMichael Ellerman 		return;
1482f2699491SMichael Ellerman 
1483f2699491SMichael Ellerman 	if (!(event->hw.state & PERF_HES_STOPPED))
1484f2699491SMichael Ellerman 		return;
1485f2699491SMichael Ellerman 
1486f2699491SMichael Ellerman 	if (ef_flags & PERF_EF_RELOAD)
1487f2699491SMichael Ellerman 		WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1488f2699491SMichael Ellerman 
1489f2699491SMichael Ellerman 	local_irq_save(flags);
1490f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1491f2699491SMichael Ellerman 
1492f2699491SMichael Ellerman 	event->hw.state = 0;
1493f2699491SMichael Ellerman 	left = local64_read(&event->hw.period_left);
1494f2699491SMichael Ellerman 
1495f2699491SMichael Ellerman 	val = 0;
1496f2699491SMichael Ellerman 	if (left < 0x80000000L)
1497f2699491SMichael Ellerman 		val = 0x80000000L - left;
1498f2699491SMichael Ellerman 
1499f2699491SMichael Ellerman 	write_pmc(event->hw.idx, val);
1500f2699491SMichael Ellerman 
1501f2699491SMichael Ellerman 	perf_event_update_userpage(event);
1502f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1503f2699491SMichael Ellerman 	local_irq_restore(flags);
1504f2699491SMichael Ellerman }
1505f2699491SMichael Ellerman 
1506f2699491SMichael Ellerman static void power_pmu_stop(struct perf_event *event, int ef_flags)
1507f2699491SMichael Ellerman {
1508f2699491SMichael Ellerman 	unsigned long flags;
1509f2699491SMichael Ellerman 
1510f2699491SMichael Ellerman 	if (!event->hw.idx || !event->hw.sample_period)
1511f2699491SMichael Ellerman 		return;
1512f2699491SMichael Ellerman 
1513f2699491SMichael Ellerman 	if (event->hw.state & PERF_HES_STOPPED)
1514f2699491SMichael Ellerman 		return;
1515f2699491SMichael Ellerman 
1516f2699491SMichael Ellerman 	local_irq_save(flags);
1517f2699491SMichael Ellerman 	perf_pmu_disable(event->pmu);
1518f2699491SMichael Ellerman 
1519f2699491SMichael Ellerman 	power_pmu_read(event);
1520f2699491SMichael Ellerman 	event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1521f2699491SMichael Ellerman 	write_pmc(event->hw.idx, 0);
1522f2699491SMichael Ellerman 
1523f2699491SMichael Ellerman 	perf_event_update_userpage(event);
1524f2699491SMichael Ellerman 	perf_pmu_enable(event->pmu);
1525f2699491SMichael Ellerman 	local_irq_restore(flags);
1526f2699491SMichael Ellerman }
1527f2699491SMichael Ellerman 
1528f2699491SMichael Ellerman /*
1529f2699491SMichael Ellerman  * Start group events scheduling transaction
1530f2699491SMichael Ellerman  * Set the flag to make pmu::enable() not perform the
1531f2699491SMichael Ellerman  * schedulability test, it will be performed at commit time
1532f2699491SMichael Ellerman  */
1533f2699491SMichael Ellerman void power_pmu_start_txn(struct pmu *pmu)
1534f2699491SMichael Ellerman {
1535f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1536f2699491SMichael Ellerman 
1537f2699491SMichael Ellerman 	perf_pmu_disable(pmu);
1538f2699491SMichael Ellerman 	cpuhw->group_flag |= PERF_EVENT_TXN;
1539f2699491SMichael Ellerman 	cpuhw->n_txn_start = cpuhw->n_events;
1540f2699491SMichael Ellerman }
1541f2699491SMichael Ellerman 
1542f2699491SMichael Ellerman /*
1543f2699491SMichael Ellerman  * Stop group events scheduling transaction
1544f2699491SMichael Ellerman  * Clear the flag and pmu::enable() will perform the
1545f2699491SMichael Ellerman  * schedulability test.
1546f2699491SMichael Ellerman  */
1547f2699491SMichael Ellerman void power_pmu_cancel_txn(struct pmu *pmu)
1548f2699491SMichael Ellerman {
1549f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1550f2699491SMichael Ellerman 
1551f2699491SMichael Ellerman 	cpuhw->group_flag &= ~PERF_EVENT_TXN;
1552f2699491SMichael Ellerman 	perf_pmu_enable(pmu);
1553f2699491SMichael Ellerman }
1554f2699491SMichael Ellerman 
1555f2699491SMichael Ellerman /*
1556f2699491SMichael Ellerman  * Commit group events scheduling transaction
1557f2699491SMichael Ellerman  * Perform the group schedulability test as a whole
1558f2699491SMichael Ellerman  * Return 0 if success
1559f2699491SMichael Ellerman  */
1560f2699491SMichael Ellerman int power_pmu_commit_txn(struct pmu *pmu)
1561f2699491SMichael Ellerman {
1562f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1563f2699491SMichael Ellerman 	long i, n;
1564f2699491SMichael Ellerman 
1565f2699491SMichael Ellerman 	if (!ppmu)
1566f2699491SMichael Ellerman 		return -EAGAIN;
1567f2699491SMichael Ellerman 	cpuhw = &__get_cpu_var(cpu_hw_events);
1568f2699491SMichael Ellerman 	n = cpuhw->n_events;
1569f2699491SMichael Ellerman 	if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1570f2699491SMichael Ellerman 		return -EAGAIN;
1571f2699491SMichael Ellerman 	i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1572f2699491SMichael Ellerman 	if (i < 0)
1573f2699491SMichael Ellerman 		return -EAGAIN;
1574f2699491SMichael Ellerman 
1575f2699491SMichael Ellerman 	for (i = cpuhw->n_txn_start; i < n; ++i)
1576f2699491SMichael Ellerman 		cpuhw->event[i]->hw.config = cpuhw->events[i];
1577f2699491SMichael Ellerman 
1578f2699491SMichael Ellerman 	cpuhw->group_flag &= ~PERF_EVENT_TXN;
1579f2699491SMichael Ellerman 	perf_pmu_enable(pmu);
1580f2699491SMichael Ellerman 	return 0;
1581f2699491SMichael Ellerman }
1582f2699491SMichael Ellerman 
1583f2699491SMichael Ellerman /*
1584f2699491SMichael Ellerman  * Return 1 if we might be able to put event on a limited PMC,
1585f2699491SMichael Ellerman  * or 0 if not.
1586f2699491SMichael Ellerman  * A event can only go on a limited PMC if it counts something
1587f2699491SMichael Ellerman  * that a limited PMC can count, doesn't require interrupts, and
1588f2699491SMichael Ellerman  * doesn't exclude any processor mode.
1589f2699491SMichael Ellerman  */
1590f2699491SMichael Ellerman static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1591f2699491SMichael Ellerman 				 unsigned int flags)
1592f2699491SMichael Ellerman {
1593f2699491SMichael Ellerman 	int n;
1594f2699491SMichael Ellerman 	u64 alt[MAX_EVENT_ALTERNATIVES];
1595f2699491SMichael Ellerman 
1596f2699491SMichael Ellerman 	if (event->attr.exclude_user
1597f2699491SMichael Ellerman 	    || event->attr.exclude_kernel
1598f2699491SMichael Ellerman 	    || event->attr.exclude_hv
1599f2699491SMichael Ellerman 	    || event->attr.sample_period)
1600f2699491SMichael Ellerman 		return 0;
1601f2699491SMichael Ellerman 
1602f2699491SMichael Ellerman 	if (ppmu->limited_pmc_event(ev))
1603f2699491SMichael Ellerman 		return 1;
1604f2699491SMichael Ellerman 
1605f2699491SMichael Ellerman 	/*
1606f2699491SMichael Ellerman 	 * The requested event_id isn't on a limited PMC already;
1607f2699491SMichael Ellerman 	 * see if any alternative code goes on a limited PMC.
1608f2699491SMichael Ellerman 	 */
1609f2699491SMichael Ellerman 	if (!ppmu->get_alternatives)
1610f2699491SMichael Ellerman 		return 0;
1611f2699491SMichael Ellerman 
1612f2699491SMichael Ellerman 	flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1613f2699491SMichael Ellerman 	n = ppmu->get_alternatives(ev, flags, alt);
1614f2699491SMichael Ellerman 
1615f2699491SMichael Ellerman 	return n > 0;
1616f2699491SMichael Ellerman }
1617f2699491SMichael Ellerman 
1618f2699491SMichael Ellerman /*
1619f2699491SMichael Ellerman  * Find an alternative event_id that goes on a normal PMC, if possible,
1620f2699491SMichael Ellerman  * and return the event_id code, or 0 if there is no such alternative.
1621f2699491SMichael Ellerman  * (Note: event_id code 0 is "don't count" on all machines.)
1622f2699491SMichael Ellerman  */
1623f2699491SMichael Ellerman static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1624f2699491SMichael Ellerman {
1625f2699491SMichael Ellerman 	u64 alt[MAX_EVENT_ALTERNATIVES];
1626f2699491SMichael Ellerman 	int n;
1627f2699491SMichael Ellerman 
1628f2699491SMichael Ellerman 	flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1629f2699491SMichael Ellerman 	n = ppmu->get_alternatives(ev, flags, alt);
1630f2699491SMichael Ellerman 	if (!n)
1631f2699491SMichael Ellerman 		return 0;
1632f2699491SMichael Ellerman 	return alt[0];
1633f2699491SMichael Ellerman }
1634f2699491SMichael Ellerman 
1635f2699491SMichael Ellerman /* Number of perf_events counting hardware events */
1636f2699491SMichael Ellerman static atomic_t num_events;
1637f2699491SMichael Ellerman /* Used to avoid races in calling reserve/release_pmc_hardware */
1638f2699491SMichael Ellerman static DEFINE_MUTEX(pmc_reserve_mutex);
1639f2699491SMichael Ellerman 
1640f2699491SMichael Ellerman /*
1641f2699491SMichael Ellerman  * Release the PMU if this is the last perf_event.
1642f2699491SMichael Ellerman  */
1643f2699491SMichael Ellerman static void hw_perf_event_destroy(struct perf_event *event)
1644f2699491SMichael Ellerman {
1645f2699491SMichael Ellerman 	if (!atomic_add_unless(&num_events, -1, 1)) {
1646f2699491SMichael Ellerman 		mutex_lock(&pmc_reserve_mutex);
1647f2699491SMichael Ellerman 		if (atomic_dec_return(&num_events) == 0)
1648f2699491SMichael Ellerman 			release_pmc_hardware();
1649f2699491SMichael Ellerman 		mutex_unlock(&pmc_reserve_mutex);
1650f2699491SMichael Ellerman 	}
1651f2699491SMichael Ellerman }
1652f2699491SMichael Ellerman 
1653f2699491SMichael Ellerman /*
1654f2699491SMichael Ellerman  * Translate a generic cache event_id config to a raw event_id code.
1655f2699491SMichael Ellerman  */
1656f2699491SMichael Ellerman static int hw_perf_cache_event(u64 config, u64 *eventp)
1657f2699491SMichael Ellerman {
1658f2699491SMichael Ellerman 	unsigned long type, op, result;
1659f2699491SMichael Ellerman 	int ev;
1660f2699491SMichael Ellerman 
1661f2699491SMichael Ellerman 	if (!ppmu->cache_events)
1662f2699491SMichael Ellerman 		return -EINVAL;
1663f2699491SMichael Ellerman 
1664f2699491SMichael Ellerman 	/* unpack config */
1665f2699491SMichael Ellerman 	type = config & 0xff;
1666f2699491SMichael Ellerman 	op = (config >> 8) & 0xff;
1667f2699491SMichael Ellerman 	result = (config >> 16) & 0xff;
1668f2699491SMichael Ellerman 
1669f2699491SMichael Ellerman 	if (type >= PERF_COUNT_HW_CACHE_MAX ||
1670f2699491SMichael Ellerman 	    op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1671f2699491SMichael Ellerman 	    result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1672f2699491SMichael Ellerman 		return -EINVAL;
1673f2699491SMichael Ellerman 
1674f2699491SMichael Ellerman 	ev = (*ppmu->cache_events)[type][op][result];
1675f2699491SMichael Ellerman 	if (ev == 0)
1676f2699491SMichael Ellerman 		return -EOPNOTSUPP;
1677f2699491SMichael Ellerman 	if (ev == -1)
1678f2699491SMichael Ellerman 		return -EINVAL;
1679f2699491SMichael Ellerman 	*eventp = ev;
1680f2699491SMichael Ellerman 	return 0;
1681f2699491SMichael Ellerman }
1682f2699491SMichael Ellerman 
1683f2699491SMichael Ellerman static int power_pmu_event_init(struct perf_event *event)
1684f2699491SMichael Ellerman {
1685f2699491SMichael Ellerman 	u64 ev;
1686f2699491SMichael Ellerman 	unsigned long flags;
1687f2699491SMichael Ellerman 	struct perf_event *ctrs[MAX_HWEVENTS];
1688f2699491SMichael Ellerman 	u64 events[MAX_HWEVENTS];
1689f2699491SMichael Ellerman 	unsigned int cflags[MAX_HWEVENTS];
1690f2699491SMichael Ellerman 	int n;
1691f2699491SMichael Ellerman 	int err;
1692f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw;
1693f2699491SMichael Ellerman 
1694f2699491SMichael Ellerman 	if (!ppmu)
1695f2699491SMichael Ellerman 		return -ENOENT;
1696f2699491SMichael Ellerman 
16973925f46bSAnshuman Khandual 	if (has_branch_stack(event)) {
16983925f46bSAnshuman Khandual 	        /* PMU has BHRB enabled */
16993925f46bSAnshuman Khandual 		if (!(ppmu->flags & PPMU_BHRB))
17005375871dSLinus Torvalds 			return -EOPNOTSUPP;
17013925f46bSAnshuman Khandual 	}
17025375871dSLinus Torvalds 
1703f2699491SMichael Ellerman 	switch (event->attr.type) {
1704f2699491SMichael Ellerman 	case PERF_TYPE_HARDWARE:
1705f2699491SMichael Ellerman 		ev = event->attr.config;
1706f2699491SMichael Ellerman 		if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1707f2699491SMichael Ellerman 			return -EOPNOTSUPP;
1708f2699491SMichael Ellerman 		ev = ppmu->generic_events[ev];
1709f2699491SMichael Ellerman 		break;
1710f2699491SMichael Ellerman 	case PERF_TYPE_HW_CACHE:
1711f2699491SMichael Ellerman 		err = hw_perf_cache_event(event->attr.config, &ev);
1712f2699491SMichael Ellerman 		if (err)
1713f2699491SMichael Ellerman 			return err;
1714f2699491SMichael Ellerman 		break;
1715f2699491SMichael Ellerman 	case PERF_TYPE_RAW:
1716f2699491SMichael Ellerman 		ev = event->attr.config;
1717f2699491SMichael Ellerman 		break;
1718f2699491SMichael Ellerman 	default:
1719f2699491SMichael Ellerman 		return -ENOENT;
1720f2699491SMichael Ellerman 	}
1721f2699491SMichael Ellerman 
1722f2699491SMichael Ellerman 	event->hw.config_base = ev;
1723f2699491SMichael Ellerman 	event->hw.idx = 0;
1724f2699491SMichael Ellerman 
1725f2699491SMichael Ellerman 	/*
1726f2699491SMichael Ellerman 	 * If we are not running on a hypervisor, force the
1727f2699491SMichael Ellerman 	 * exclude_hv bit to 0 so that we don't care what
1728f2699491SMichael Ellerman 	 * the user set it to.
1729f2699491SMichael Ellerman 	 */
1730f2699491SMichael Ellerman 	if (!firmware_has_feature(FW_FEATURE_LPAR))
1731f2699491SMichael Ellerman 		event->attr.exclude_hv = 0;
1732f2699491SMichael Ellerman 
1733f2699491SMichael Ellerman 	/*
1734f2699491SMichael Ellerman 	 * If this is a per-task event, then we can use
1735f2699491SMichael Ellerman 	 * PM_RUN_* events interchangeably with their non RUN_*
1736f2699491SMichael Ellerman 	 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1737f2699491SMichael Ellerman 	 * XXX we should check if the task is an idle task.
1738f2699491SMichael Ellerman 	 */
1739f2699491SMichael Ellerman 	flags = 0;
1740f2699491SMichael Ellerman 	if (event->attach_state & PERF_ATTACH_TASK)
1741f2699491SMichael Ellerman 		flags |= PPMU_ONLY_COUNT_RUN;
1742f2699491SMichael Ellerman 
1743f2699491SMichael Ellerman 	/*
1744f2699491SMichael Ellerman 	 * If this machine has limited events, check whether this
1745f2699491SMichael Ellerman 	 * event_id could go on a limited event.
1746f2699491SMichael Ellerman 	 */
1747f2699491SMichael Ellerman 	if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1748f2699491SMichael Ellerman 		if (can_go_on_limited_pmc(event, ev, flags)) {
1749f2699491SMichael Ellerman 			flags |= PPMU_LIMITED_PMC_OK;
1750f2699491SMichael Ellerman 		} else if (ppmu->limited_pmc_event(ev)) {
1751f2699491SMichael Ellerman 			/*
1752f2699491SMichael Ellerman 			 * The requested event_id is on a limited PMC,
1753f2699491SMichael Ellerman 			 * but we can't use a limited PMC; see if any
1754f2699491SMichael Ellerman 			 * alternative goes on a normal PMC.
1755f2699491SMichael Ellerman 			 */
1756f2699491SMichael Ellerman 			ev = normal_pmc_alternative(ev, flags);
1757f2699491SMichael Ellerman 			if (!ev)
1758f2699491SMichael Ellerman 				return -EINVAL;
1759f2699491SMichael Ellerman 		}
1760f2699491SMichael Ellerman 	}
1761f2699491SMichael Ellerman 
1762330a1eb7SMichael Ellerman 	/* Extra checks for EBB */
1763330a1eb7SMichael Ellerman 	err = ebb_event_check(event);
1764330a1eb7SMichael Ellerman 	if (err)
1765330a1eb7SMichael Ellerman 		return err;
1766330a1eb7SMichael Ellerman 
1767f2699491SMichael Ellerman 	/*
1768f2699491SMichael Ellerman 	 * If this is in a group, check if it can go on with all the
1769f2699491SMichael Ellerman 	 * other hardware events in the group.  We assume the event
1770f2699491SMichael Ellerman 	 * hasn't been linked into its leader's sibling list at this point.
1771f2699491SMichael Ellerman 	 */
1772f2699491SMichael Ellerman 	n = 0;
1773f2699491SMichael Ellerman 	if (event->group_leader != event) {
1774f2699491SMichael Ellerman 		n = collect_events(event->group_leader, ppmu->n_counter - 1,
1775f2699491SMichael Ellerman 				   ctrs, events, cflags);
1776f2699491SMichael Ellerman 		if (n < 0)
1777f2699491SMichael Ellerman 			return -EINVAL;
1778f2699491SMichael Ellerman 	}
1779f2699491SMichael Ellerman 	events[n] = ev;
1780f2699491SMichael Ellerman 	ctrs[n] = event;
1781f2699491SMichael Ellerman 	cflags[n] = flags;
1782f2699491SMichael Ellerman 	if (check_excludes(ctrs, cflags, n, 1))
1783f2699491SMichael Ellerman 		return -EINVAL;
1784f2699491SMichael Ellerman 
1785f2699491SMichael Ellerman 	cpuhw = &get_cpu_var(cpu_hw_events);
1786f2699491SMichael Ellerman 	err = power_check_constraints(cpuhw, events, cflags, n + 1);
17873925f46bSAnshuman Khandual 
17883925f46bSAnshuman Khandual 	if (has_branch_stack(event)) {
17893925f46bSAnshuman Khandual 		cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
17903925f46bSAnshuman Khandual 					event->attr.branch_sample_type);
17913925f46bSAnshuman Khandual 
17923925f46bSAnshuman Khandual 		if(cpuhw->bhrb_filter == -1)
17933925f46bSAnshuman Khandual 			return -EOPNOTSUPP;
17943925f46bSAnshuman Khandual 	}
17953925f46bSAnshuman Khandual 
1796f2699491SMichael Ellerman 	put_cpu_var(cpu_hw_events);
1797f2699491SMichael Ellerman 	if (err)
1798f2699491SMichael Ellerman 		return -EINVAL;
1799f2699491SMichael Ellerman 
1800f2699491SMichael Ellerman 	event->hw.config = events[n];
1801f2699491SMichael Ellerman 	event->hw.event_base = cflags[n];
1802f2699491SMichael Ellerman 	event->hw.last_period = event->hw.sample_period;
1803f2699491SMichael Ellerman 	local64_set(&event->hw.period_left, event->hw.last_period);
1804f2699491SMichael Ellerman 
1805f2699491SMichael Ellerman 	/*
1806330a1eb7SMichael Ellerman 	 * For EBB events we just context switch the PMC value, we don't do any
1807330a1eb7SMichael Ellerman 	 * of the sample_period logic. We use hw.prev_count for this.
1808330a1eb7SMichael Ellerman 	 */
1809330a1eb7SMichael Ellerman 	if (is_ebb_event(event))
1810330a1eb7SMichael Ellerman 		local64_set(&event->hw.prev_count, 0);
1811330a1eb7SMichael Ellerman 
1812330a1eb7SMichael Ellerman 	/*
1813f2699491SMichael Ellerman 	 * See if we need to reserve the PMU.
1814f2699491SMichael Ellerman 	 * If no events are currently in use, then we have to take a
1815f2699491SMichael Ellerman 	 * mutex to ensure that we don't race with another task doing
1816f2699491SMichael Ellerman 	 * reserve_pmc_hardware or release_pmc_hardware.
1817f2699491SMichael Ellerman 	 */
1818f2699491SMichael Ellerman 	err = 0;
1819f2699491SMichael Ellerman 	if (!atomic_inc_not_zero(&num_events)) {
1820f2699491SMichael Ellerman 		mutex_lock(&pmc_reserve_mutex);
1821f2699491SMichael Ellerman 		if (atomic_read(&num_events) == 0 &&
1822f2699491SMichael Ellerman 		    reserve_pmc_hardware(perf_event_interrupt))
1823f2699491SMichael Ellerman 			err = -EBUSY;
1824f2699491SMichael Ellerman 		else
1825f2699491SMichael Ellerman 			atomic_inc(&num_events);
1826f2699491SMichael Ellerman 		mutex_unlock(&pmc_reserve_mutex);
1827f2699491SMichael Ellerman 	}
1828f2699491SMichael Ellerman 	event->destroy = hw_perf_event_destroy;
1829f2699491SMichael Ellerman 
1830f2699491SMichael Ellerman 	return err;
1831f2699491SMichael Ellerman }
1832f2699491SMichael Ellerman 
18335375871dSLinus Torvalds static int power_pmu_event_idx(struct perf_event *event)
18345375871dSLinus Torvalds {
18355375871dSLinus Torvalds 	return event->hw.idx;
18365375871dSLinus Torvalds }
18375375871dSLinus Torvalds 
18381c53a270SSukadev Bhattiprolu ssize_t power_events_sysfs_show(struct device *dev,
18391c53a270SSukadev Bhattiprolu 				struct device_attribute *attr, char *page)
18401c53a270SSukadev Bhattiprolu {
18411c53a270SSukadev Bhattiprolu 	struct perf_pmu_events_attr *pmu_attr;
18421c53a270SSukadev Bhattiprolu 
18431c53a270SSukadev Bhattiprolu 	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
18441c53a270SSukadev Bhattiprolu 
18451c53a270SSukadev Bhattiprolu 	return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
18461c53a270SSukadev Bhattiprolu }
18471c53a270SSukadev Bhattiprolu 
1848f2699491SMichael Ellerman struct pmu power_pmu = {
1849f2699491SMichael Ellerman 	.pmu_enable	= power_pmu_enable,
1850f2699491SMichael Ellerman 	.pmu_disable	= power_pmu_disable,
1851f2699491SMichael Ellerman 	.event_init	= power_pmu_event_init,
1852f2699491SMichael Ellerman 	.add		= power_pmu_add,
1853f2699491SMichael Ellerman 	.del		= power_pmu_del,
1854f2699491SMichael Ellerman 	.start		= power_pmu_start,
1855f2699491SMichael Ellerman 	.stop		= power_pmu_stop,
1856f2699491SMichael Ellerman 	.read		= power_pmu_read,
1857f2699491SMichael Ellerman 	.start_txn	= power_pmu_start_txn,
1858f2699491SMichael Ellerman 	.cancel_txn	= power_pmu_cancel_txn,
1859f2699491SMichael Ellerman 	.commit_txn	= power_pmu_commit_txn,
18605375871dSLinus Torvalds 	.event_idx	= power_pmu_event_idx,
18613925f46bSAnshuman Khandual 	.flush_branch_stack = power_pmu_flush_branch_stack,
1862f2699491SMichael Ellerman };
1863f2699491SMichael Ellerman 
1864f2699491SMichael Ellerman /*
1865f2699491SMichael Ellerman  * A counter has overflowed; update its count and record
1866f2699491SMichael Ellerman  * things if requested.  Note that interrupts are hard-disabled
1867f2699491SMichael Ellerman  * here so there is no possibility of being interrupted.
1868f2699491SMichael Ellerman  */
1869f2699491SMichael Ellerman static void record_and_restart(struct perf_event *event, unsigned long val,
1870f2699491SMichael Ellerman 			       struct pt_regs *regs)
1871f2699491SMichael Ellerman {
1872f2699491SMichael Ellerman 	u64 period = event->hw.sample_period;
1873f2699491SMichael Ellerman 	s64 prev, delta, left;
1874f2699491SMichael Ellerman 	int record = 0;
1875f2699491SMichael Ellerman 
1876f2699491SMichael Ellerman 	if (event->hw.state & PERF_HES_STOPPED) {
1877f2699491SMichael Ellerman 		write_pmc(event->hw.idx, 0);
1878f2699491SMichael Ellerman 		return;
1879f2699491SMichael Ellerman 	}
1880f2699491SMichael Ellerman 
1881f2699491SMichael Ellerman 	/* we don't have to worry about interrupts here */
1882f2699491SMichael Ellerman 	prev = local64_read(&event->hw.prev_count);
1883f2699491SMichael Ellerman 	delta = check_and_compute_delta(prev, val);
1884f2699491SMichael Ellerman 	local64_add(delta, &event->count);
1885f2699491SMichael Ellerman 
1886f2699491SMichael Ellerman 	/*
1887f2699491SMichael Ellerman 	 * See if the total period for this event has expired,
1888f2699491SMichael Ellerman 	 * and update for the next period.
1889f2699491SMichael Ellerman 	 */
1890f2699491SMichael Ellerman 	val = 0;
1891f2699491SMichael Ellerman 	left = local64_read(&event->hw.period_left) - delta;
1892e13e895fSMichael Neuling 	if (delta == 0)
1893e13e895fSMichael Neuling 		left++;
1894f2699491SMichael Ellerman 	if (period) {
1895f2699491SMichael Ellerman 		if (left <= 0) {
1896f2699491SMichael Ellerman 			left += period;
1897f2699491SMichael Ellerman 			if (left <= 0)
1898f2699491SMichael Ellerman 				left = period;
1899e6878835Ssukadev@linux.vnet.ibm.com 			record = siar_valid(regs);
1900f2699491SMichael Ellerman 			event->hw.last_period = event->hw.sample_period;
1901f2699491SMichael Ellerman 		}
1902f2699491SMichael Ellerman 		if (left < 0x80000000LL)
1903f2699491SMichael Ellerman 			val = 0x80000000LL - left;
1904f2699491SMichael Ellerman 	}
1905f2699491SMichael Ellerman 
1906f2699491SMichael Ellerman 	write_pmc(event->hw.idx, val);
1907f2699491SMichael Ellerman 	local64_set(&event->hw.prev_count, val);
1908f2699491SMichael Ellerman 	local64_set(&event->hw.period_left, left);
1909f2699491SMichael Ellerman 	perf_event_update_userpage(event);
1910f2699491SMichael Ellerman 
1911f2699491SMichael Ellerman 	/*
1912f2699491SMichael Ellerman 	 * Finally record data if requested.
1913f2699491SMichael Ellerman 	 */
1914f2699491SMichael Ellerman 	if (record) {
1915f2699491SMichael Ellerman 		struct perf_sample_data data;
1916f2699491SMichael Ellerman 
1917fd0d000bSRobert Richter 		perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
1918f2699491SMichael Ellerman 
1919f2699491SMichael Ellerman 		if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1920f2699491SMichael Ellerman 			perf_get_data_addr(regs, &data.addr);
1921f2699491SMichael Ellerman 
19223925f46bSAnshuman Khandual 		if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
19233925f46bSAnshuman Khandual 			struct cpu_hw_events *cpuhw;
19243925f46bSAnshuman Khandual 			cpuhw = &__get_cpu_var(cpu_hw_events);
19253925f46bSAnshuman Khandual 			power_pmu_bhrb_read(cpuhw);
19263925f46bSAnshuman Khandual 			data.br_stack = &cpuhw->bhrb_stack;
19273925f46bSAnshuman Khandual 		}
19283925f46bSAnshuman Khandual 
1929f2699491SMichael Ellerman 		if (perf_event_overflow(event, &data, regs))
1930f2699491SMichael Ellerman 			power_pmu_stop(event, 0);
1931f2699491SMichael Ellerman 	}
1932f2699491SMichael Ellerman }
1933f2699491SMichael Ellerman 
1934f2699491SMichael Ellerman /*
1935f2699491SMichael Ellerman  * Called from generic code to get the misc flags (i.e. processor mode)
1936f2699491SMichael Ellerman  * for an event_id.
1937f2699491SMichael Ellerman  */
1938f2699491SMichael Ellerman unsigned long perf_misc_flags(struct pt_regs *regs)
1939f2699491SMichael Ellerman {
1940f2699491SMichael Ellerman 	u32 flags = perf_get_misc_flags(regs);
1941f2699491SMichael Ellerman 
1942f2699491SMichael Ellerman 	if (flags)
1943f2699491SMichael Ellerman 		return flags;
1944f2699491SMichael Ellerman 	return user_mode(regs) ? PERF_RECORD_MISC_USER :
1945f2699491SMichael Ellerman 		PERF_RECORD_MISC_KERNEL;
1946f2699491SMichael Ellerman }
1947f2699491SMichael Ellerman 
1948f2699491SMichael Ellerman /*
1949f2699491SMichael Ellerman  * Called from generic code to get the instruction pointer
1950f2699491SMichael Ellerman  * for an event_id.
1951f2699491SMichael Ellerman  */
1952f2699491SMichael Ellerman unsigned long perf_instruction_pointer(struct pt_regs *regs)
1953f2699491SMichael Ellerman {
195433904054SMichael Ellerman 	bool use_siar = regs_use_siar(regs);
1955f2699491SMichael Ellerman 
1956e6878835Ssukadev@linux.vnet.ibm.com 	if (use_siar && siar_valid(regs))
19571ce447b9SBenjamin Herrenschmidt 		return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
1958e6878835Ssukadev@linux.vnet.ibm.com 	else if (use_siar)
1959e6878835Ssukadev@linux.vnet.ibm.com 		return 0;		// no valid instruction pointer
196075382aa7SAnton Blanchard 	else
196175382aa7SAnton Blanchard 		return regs->nip;
1962f2699491SMichael Ellerman }
1963f2699491SMichael Ellerman 
1964bc09c219SMichael Neuling static bool pmc_overflow_power7(unsigned long val)
1965f2699491SMichael Ellerman {
1966f2699491SMichael Ellerman 	/*
1967f2699491SMichael Ellerman 	 * Events on POWER7 can roll back if a speculative event doesn't
1968f2699491SMichael Ellerman 	 * eventually complete. Unfortunately in some rare cases they will
1969f2699491SMichael Ellerman 	 * raise a performance monitor exception. We need to catch this to
1970f2699491SMichael Ellerman 	 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1971f2699491SMichael Ellerman 	 * cycles from overflow.
1972f2699491SMichael Ellerman 	 *
1973f2699491SMichael Ellerman 	 * We only do this if the first pass fails to find any overflowing
1974f2699491SMichael Ellerman 	 * PMCs because a user might set a period of less than 256 and we
1975f2699491SMichael Ellerman 	 * don't want to mistakenly reset them.
1976f2699491SMichael Ellerman 	 */
1977bc09c219SMichael Neuling 	if ((0x80000000 - val) <= 256)
1978bc09c219SMichael Neuling 		return true;
1979bc09c219SMichael Neuling 
1980bc09c219SMichael Neuling 	return false;
1981bc09c219SMichael Neuling }
1982bc09c219SMichael Neuling 
1983bc09c219SMichael Neuling static bool pmc_overflow(unsigned long val)
1984bc09c219SMichael Neuling {
1985bc09c219SMichael Neuling 	if ((int)val < 0)
1986f2699491SMichael Ellerman 		return true;
1987f2699491SMichael Ellerman 
1988f2699491SMichael Ellerman 	return false;
1989f2699491SMichael Ellerman }
1990f2699491SMichael Ellerman 
1991f2699491SMichael Ellerman /*
1992f2699491SMichael Ellerman  * Performance monitor interrupt stuff
1993f2699491SMichael Ellerman  */
1994f2699491SMichael Ellerman static void perf_event_interrupt(struct pt_regs *regs)
1995f2699491SMichael Ellerman {
1996bc09c219SMichael Neuling 	int i, j;
1997f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1998f2699491SMichael Ellerman 	struct perf_event *event;
1999bc09c219SMichael Neuling 	unsigned long val[8];
2000bc09c219SMichael Neuling 	int found, active;
2001f2699491SMichael Ellerman 	int nmi;
2002f2699491SMichael Ellerman 
2003f2699491SMichael Ellerman 	if (cpuhw->n_limited)
2004f2699491SMichael Ellerman 		freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
2005f2699491SMichael Ellerman 					mfspr(SPRN_PMC6));
2006f2699491SMichael Ellerman 
2007f2699491SMichael Ellerman 	perf_read_regs(regs);
2008f2699491SMichael Ellerman 
2009f2699491SMichael Ellerman 	nmi = perf_intr_is_nmi(regs);
2010f2699491SMichael Ellerman 	if (nmi)
2011f2699491SMichael Ellerman 		nmi_enter();
2012f2699491SMichael Ellerman 	else
2013f2699491SMichael Ellerman 		irq_enter();
2014f2699491SMichael Ellerman 
2015bc09c219SMichael Neuling 	/* Read all the PMCs since we'll need them a bunch of times */
2016bc09c219SMichael Neuling 	for (i = 0; i < ppmu->n_counter; ++i)
2017bc09c219SMichael Neuling 		val[i] = read_pmc(i + 1);
2018bc09c219SMichael Neuling 
2019bc09c219SMichael Neuling 	/* Try to find what caused the IRQ */
2020bc09c219SMichael Neuling 	found = 0;
2021bc09c219SMichael Neuling 	for (i = 0; i < ppmu->n_counter; ++i) {
2022bc09c219SMichael Neuling 		if (!pmc_overflow(val[i]))
2023bc09c219SMichael Neuling 			continue;
2024bc09c219SMichael Neuling 		if (is_limited_pmc(i + 1))
2025bc09c219SMichael Neuling 			continue; /* these won't generate IRQs */
2026bc09c219SMichael Neuling 		/*
2027bc09c219SMichael Neuling 		 * We've found one that's overflowed.  For active
2028bc09c219SMichael Neuling 		 * counters we need to log this.  For inactive
2029bc09c219SMichael Neuling 		 * counters, we need to reset it anyway
2030bc09c219SMichael Neuling 		 */
2031bc09c219SMichael Neuling 		found = 1;
2032bc09c219SMichael Neuling 		active = 0;
2033bc09c219SMichael Neuling 		for (j = 0; j < cpuhw->n_events; ++j) {
2034bc09c219SMichael Neuling 			event = cpuhw->event[j];
2035bc09c219SMichael Neuling 			if (event->hw.idx == (i + 1)) {
2036bc09c219SMichael Neuling 				active = 1;
2037bc09c219SMichael Neuling 				record_and_restart(event, val[i], regs);
2038bc09c219SMichael Neuling 				break;
2039bc09c219SMichael Neuling 			}
2040bc09c219SMichael Neuling 		}
2041bc09c219SMichael Neuling 		if (!active)
2042bc09c219SMichael Neuling 			/* reset non active counters that have overflowed */
2043bc09c219SMichael Neuling 			write_pmc(i + 1, 0);
2044bc09c219SMichael Neuling 	}
2045bc09c219SMichael Neuling 	if (!found && pvr_version_is(PVR_POWER7)) {
2046bc09c219SMichael Neuling 		/* check active counters for special buggy p7 overflow */
2047f2699491SMichael Ellerman 		for (i = 0; i < cpuhw->n_events; ++i) {
2048f2699491SMichael Ellerman 			event = cpuhw->event[i];
2049f2699491SMichael Ellerman 			if (!event->hw.idx || is_limited_pmc(event->hw.idx))
2050f2699491SMichael Ellerman 				continue;
2051bc09c219SMichael Neuling 			if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2052bc09c219SMichael Neuling 				/* event has overflowed in a buggy way*/
2053f2699491SMichael Ellerman 				found = 1;
2054bc09c219SMichael Neuling 				record_and_restart(event,
2055bc09c219SMichael Neuling 						   val[event->hw.idx - 1],
2056bc09c219SMichael Neuling 						   regs);
2057f2699491SMichael Ellerman 			}
2058f2699491SMichael Ellerman 		}
2059f2699491SMichael Ellerman 	}
20606772faa1SMichael Ellerman 	if (!found && !nmi && printk_ratelimit())
2061bc09c219SMichael Neuling 		printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
2062f2699491SMichael Ellerman 
2063f2699491SMichael Ellerman 	/*
2064f2699491SMichael Ellerman 	 * Reset MMCR0 to its normal value.  This will set PMXE and
2065f2699491SMichael Ellerman 	 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
2066f2699491SMichael Ellerman 	 * and thus allow interrupts to occur again.
2067f2699491SMichael Ellerman 	 * XXX might want to use MSR.PM to keep the events frozen until
2068f2699491SMichael Ellerman 	 * we get back out of this interrupt.
2069f2699491SMichael Ellerman 	 */
2070f2699491SMichael Ellerman 	write_mmcr0(cpuhw, cpuhw->mmcr[0]);
2071f2699491SMichael Ellerman 
2072f2699491SMichael Ellerman 	if (nmi)
2073f2699491SMichael Ellerman 		nmi_exit();
2074f2699491SMichael Ellerman 	else
2075f2699491SMichael Ellerman 		irq_exit();
2076f2699491SMichael Ellerman }
2077f2699491SMichael Ellerman 
2078f2699491SMichael Ellerman static void power_pmu_setup(int cpu)
2079f2699491SMichael Ellerman {
2080f2699491SMichael Ellerman 	struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2081f2699491SMichael Ellerman 
2082f2699491SMichael Ellerman 	if (!ppmu)
2083f2699491SMichael Ellerman 		return;
2084f2699491SMichael Ellerman 	memset(cpuhw, 0, sizeof(*cpuhw));
2085f2699491SMichael Ellerman 	cpuhw->mmcr[0] = MMCR0_FC;
2086f2699491SMichael Ellerman }
2087f2699491SMichael Ellerman 
2088061d19f2SPaul Gortmaker static int
2089f2699491SMichael Ellerman power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
2090f2699491SMichael Ellerman {
2091f2699491SMichael Ellerman 	unsigned int cpu = (long)hcpu;
2092f2699491SMichael Ellerman 
2093f2699491SMichael Ellerman 	switch (action & ~CPU_TASKS_FROZEN) {
2094f2699491SMichael Ellerman 	case CPU_UP_PREPARE:
2095f2699491SMichael Ellerman 		power_pmu_setup(cpu);
2096f2699491SMichael Ellerman 		break;
2097f2699491SMichael Ellerman 
2098f2699491SMichael Ellerman 	default:
2099f2699491SMichael Ellerman 		break;
2100f2699491SMichael Ellerman 	}
2101f2699491SMichael Ellerman 
2102f2699491SMichael Ellerman 	return NOTIFY_OK;
2103f2699491SMichael Ellerman }
2104f2699491SMichael Ellerman 
2105061d19f2SPaul Gortmaker int register_power_pmu(struct power_pmu *pmu)
2106f2699491SMichael Ellerman {
2107f2699491SMichael Ellerman 	if (ppmu)
2108f2699491SMichael Ellerman 		return -EBUSY;		/* something's already registered */
2109f2699491SMichael Ellerman 
2110f2699491SMichael Ellerman 	ppmu = pmu;
2111f2699491SMichael Ellerman 	pr_info("%s performance monitor hardware support registered\n",
2112f2699491SMichael Ellerman 		pmu->name);
2113f2699491SMichael Ellerman 
21141c53a270SSukadev Bhattiprolu 	power_pmu.attr_groups = ppmu->attr_groups;
21151c53a270SSukadev Bhattiprolu 
2116f2699491SMichael Ellerman #ifdef MSR_HV
2117f2699491SMichael Ellerman 	/*
2118f2699491SMichael Ellerman 	 * Use FCHV to ignore kernel events if MSR.HV is set.
2119f2699491SMichael Ellerman 	 */
2120f2699491SMichael Ellerman 	if (mfmsr() & MSR_HV)
2121f2699491SMichael Ellerman 		freeze_events_kernel = MMCR0_FCHV;
2122f2699491SMichael Ellerman #endif /* CONFIG_PPC64 */
2123f2699491SMichael Ellerman 
2124f2699491SMichael Ellerman 	perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
2125f2699491SMichael Ellerman 	perf_cpu_notifier(power_pmu_notifier);
2126f2699491SMichael Ellerman 
2127f2699491SMichael Ellerman 	return 0;
2128f2699491SMichael Ellerman }
2129