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