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