Merge branch 'fixes' of git://git.linaro.org/people/rmk/linux-arm
[linux-2.6-block.git] / arch / powerpc / perf / core-book3s.c
CommitLineData
4574910e 1/*
cdd6c482 2 * Performance event support - powerpc architecture code
4574910e
PM
3 *
4 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/kernel.h>
12#include <linux/sched.h>
cdd6c482 13#include <linux/perf_event.h>
4574910e
PM
14#include <linux/percpu.h>
15#include <linux/hardirq.h>
69123184 16#include <linux/uaccess.h>
4574910e
PM
17#include <asm/reg.h>
18#include <asm/pmc.h>
01d0287f 19#include <asm/machdep.h>
0475f9ea 20#include <asm/firmware.h>
0bbd0d4b 21#include <asm/ptrace.h>
69123184 22#include <asm/code-patching.h>
4574910e 23
3925f46b
AK
24#define BHRB_MAX_ENTRIES 32
25#define BHRB_TARGET 0x0000000000000002
26#define BHRB_PREDICTION 0x0000000000000001
27#define BHRB_EA 0xFFFFFFFFFFFFFFFC
28
cdd6c482
IM
29struct cpu_hw_events {
30 int n_events;
4574910e
PM
31 int n_percpu;
32 int disabled;
33 int n_added;
ab7ef2e5
PM
34 int n_limited;
35 u8 pmcs_enabled;
cdd6c482
IM
36 struct perf_event *event[MAX_HWEVENTS];
37 u64 events[MAX_HWEVENTS];
38 unsigned int flags[MAX_HWEVENTS];
448d64f8 39 unsigned long mmcr[3];
a8f90e90
PM
40 struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
41 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
cdd6c482
IM
42 u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
43 unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
44 unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
8e6d5573
LM
45
46 unsigned int group_flag;
47 int n_txn_start;
3925f46b
AK
48
49 /* BHRB bits */
50 u64 bhrb_filter; /* BHRB HW branch filter */
51 int bhrb_users;
52 void *bhrb_context;
53 struct perf_branch_stack bhrb_stack;
54 struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
4574910e 55};
3925f46b 56
cdd6c482 57DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
4574910e
PM
58
59struct power_pmu *ppmu;
60
d095cd46 61/*
57c0c15b 62 * Normally, to ignore kernel events we set the FCS (freeze counters
d095cd46
PM
63 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
64 * hypervisor bit set in the MSR, or if we are running on a processor
65 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
66 * then we need to use the FCHV bit to ignore kernel events.
67 */
cdd6c482 68static unsigned int freeze_events_kernel = MMCR0_FCS;
d095cd46 69
98fb1807
PM
70/*
71 * 32-bit doesn't have MMCRA but does have an MMCR2,
72 * and a few other names are different.
73 */
74#ifdef CONFIG_PPC32
75
76#define MMCR0_FCHV 0
77#define MMCR0_PMCjCE MMCR0_PMCnCE
7a7a41f9 78#define MMCR0_FC56 0
378a6ee9 79#define MMCR0_PMAO 0
330a1eb7
ME
80#define MMCR0_EBE 0
81#define MMCR0_PMCC 0
82#define MMCR0_PMCC_U6 0
98fb1807
PM
83
84#define SPRN_MMCRA SPRN_MMCR2
85#define MMCRA_SAMPLE_ENABLE 0
86
87static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
88{
89 return 0;
90}
98fb1807
PM
91static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
92static inline u32 perf_get_misc_flags(struct pt_regs *regs)
93{
94 return 0;
95}
75382aa7
AB
96static inline void perf_read_regs(struct pt_regs *regs)
97{
98 regs->result = 0;
99}
98fb1807
PM
100static inline int perf_intr_is_nmi(struct pt_regs *regs)
101{
102 return 0;
103}
104
e6878835 105static inline int siar_valid(struct pt_regs *regs)
106{
107 return 1;
108}
109
330a1eb7
ME
110static bool is_ebb_event(struct perf_event *event) { return false; }
111static int ebb_event_check(struct perf_event *event) { return 0; }
112static void ebb_event_add(struct perf_event *event) { }
113static void ebb_switch_out(unsigned long mmcr0) { }
114static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
115{
116 return mmcr0;
117}
118
d52f2dc4
MN
119static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
120static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
121void power_pmu_flush_branch_stack(void) {}
122static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
98fb1807
PM
123#endif /* CONFIG_PPC32 */
124
33904054
ME
125static bool regs_use_siar(struct pt_regs *regs)
126{
cbda6aa1 127 return !!regs->result;
33904054
ME
128}
129
98fb1807
PM
130/*
131 * Things that are specific to 64-bit implementations.
132 */
133#ifdef CONFIG_PPC64
134
135static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
136{
137 unsigned long mmcra = regs->dsisr;
138
7a786832 139 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
98fb1807
PM
140 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
141 if (slot > 1)
142 return 4 * (slot - 1);
143 }
7a786832 144
98fb1807
PM
145 return 0;
146}
147
98fb1807
PM
148/*
149 * The user wants a data address recorded.
150 * If we're not doing instruction sampling, give them the SDAR
151 * (sampled data address). If we are doing instruction sampling, then
152 * only give them the SDAR if it corresponds to the instruction
58a032c3
ME
153 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
154 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
98fb1807
PM
155 */
156static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
157{
158 unsigned long mmcra = regs->dsisr;
58a032c3 159 bool sdar_valid;
e6878835 160
58a032c3
ME
161 if (ppmu->flags & PPMU_HAS_SIER)
162 sdar_valid = regs->dar & SIER_SDAR_VALID;
163 else {
164 unsigned long sdsync;
165
166 if (ppmu->flags & PPMU_SIAR_VALID)
167 sdsync = POWER7P_MMCRA_SDAR_VALID;
168 else if (ppmu->flags & PPMU_ALT_SIPR)
169 sdsync = POWER6_MMCRA_SDSYNC;
170 else
171 sdsync = MMCRA_SDSYNC;
172
173 sdar_valid = mmcra & sdsync;
174 }
98fb1807 175
58a032c3 176 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
98fb1807
PM
177 *addrp = mfspr(SPRN_SDAR);
178}
179
5682c460 180static bool regs_sihv(struct pt_regs *regs)
68b30bb9
AB
181{
182 unsigned long sihv = MMCRA_SIHV;
183
8f61aa32
ME
184 if (ppmu->flags & PPMU_HAS_SIER)
185 return !!(regs->dar & SIER_SIHV);
186
68b30bb9
AB
187 if (ppmu->flags & PPMU_ALT_SIPR)
188 sihv = POWER6_MMCRA_SIHV;
189
5682c460 190 return !!(regs->dsisr & sihv);
68b30bb9
AB
191}
192
5682c460 193static bool regs_sipr(struct pt_regs *regs)
68b30bb9
AB
194{
195 unsigned long sipr = MMCRA_SIPR;
196
8f61aa32
ME
197 if (ppmu->flags & PPMU_HAS_SIER)
198 return !!(regs->dar & SIER_SIPR);
199
68b30bb9
AB
200 if (ppmu->flags & PPMU_ALT_SIPR)
201 sipr = POWER6_MMCRA_SIPR;
202
5682c460 203 return !!(regs->dsisr & sipr);
68b30bb9
AB
204}
205
1ce447b9
BH
206static inline u32 perf_flags_from_msr(struct pt_regs *regs)
207{
208 if (regs->msr & MSR_PR)
209 return PERF_RECORD_MISC_USER;
210 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
211 return PERF_RECORD_MISC_HYPERVISOR;
212 return PERF_RECORD_MISC_KERNEL;
213}
214
98fb1807
PM
215static inline u32 perf_get_misc_flags(struct pt_regs *regs)
216{
33904054 217 bool use_siar = regs_use_siar(regs);
98fb1807 218
75382aa7 219 if (!use_siar)
1ce447b9
BH
220 return perf_flags_from_msr(regs);
221
222 /*
223 * If we don't have flags in MMCRA, rather than using
224 * the MSR, we intuit the flags from the address in
225 * SIAR which should give slightly more reliable
226 * results
227 */
cbda6aa1 228 if (ppmu->flags & PPMU_NO_SIPR) {
1ce447b9
BH
229 unsigned long siar = mfspr(SPRN_SIAR);
230 if (siar >= PAGE_OFFSET)
231 return PERF_RECORD_MISC_KERNEL;
232 return PERF_RECORD_MISC_USER;
233 }
98fb1807 234
7abb840b 235 /* PR has priority over HV, so order below is important */
5682c460 236 if (regs_sipr(regs))
7abb840b 237 return PERF_RECORD_MISC_USER;
5682c460
ME
238
239 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
cdd6c482 240 return PERF_RECORD_MISC_HYPERVISOR;
5682c460 241
7abb840b 242 return PERF_RECORD_MISC_KERNEL;
98fb1807
PM
243}
244
245/*
246 * Overload regs->dsisr to store MMCRA so we only need to read it once
247 * on each interrupt.
8f61aa32 248 * Overload regs->dar to store SIER if we have it.
75382aa7
AB
249 * Overload regs->result to specify whether we should use the MSR (result
250 * is zero) or the SIAR (result is non zero).
98fb1807
PM
251 */
252static inline void perf_read_regs(struct pt_regs *regs)
253{
75382aa7
AB
254 unsigned long mmcra = mfspr(SPRN_MMCRA);
255 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
256 int use_siar;
257
5682c460 258 regs->dsisr = mmcra;
8f61aa32 259
cbda6aa1
ME
260 if (ppmu->flags & PPMU_HAS_SIER)
261 regs->dar = mfspr(SPRN_SIER);
8f61aa32 262
5c093efa
AB
263 /*
264 * If this isn't a PMU exception (eg a software event) the SIAR is
265 * not valid. Use pt_regs.
266 *
267 * If it is a marked event use the SIAR.
268 *
269 * If the PMU doesn't update the SIAR for non marked events use
270 * pt_regs.
271 *
272 * If the PMU has HV/PR flags then check to see if they
273 * place the exception in userspace. If so, use pt_regs. In
274 * continuous sampling mode the SIAR and the PMU exception are
275 * not synchronised, so they may be many instructions apart.
276 * This can result in confusing backtraces. We still want
277 * hypervisor samples as well as samples in the kernel with
278 * interrupts off hence the userspace check.
279 */
75382aa7
AB
280 if (TRAP(regs) != 0xf00)
281 use_siar = 0;
5c093efa
AB
282 else if (marked)
283 use_siar = 1;
284 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
285 use_siar = 0;
cbda6aa1 286 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
75382aa7
AB
287 use_siar = 0;
288 else
289 use_siar = 1;
290
cbda6aa1 291 regs->result = use_siar;
98fb1807
PM
292}
293
294/*
295 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
296 * it as an NMI.
297 */
298static inline int perf_intr_is_nmi(struct pt_regs *regs)
299{
300 return !regs->softe;
301}
302
e6878835 303/*
304 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
305 * must be sampled only if the SIAR-valid bit is set.
306 *
307 * For unmarked instructions and for processors that don't have the SIAR-Valid
308 * bit, assume that SIAR is valid.
309 */
310static inline int siar_valid(struct pt_regs *regs)
311{
312 unsigned long mmcra = regs->dsisr;
313 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
314
58a032c3
ME
315 if (marked) {
316 if (ppmu->flags & PPMU_HAS_SIER)
317 return regs->dar & SIER_SIAR_VALID;
318
319 if (ppmu->flags & PPMU_SIAR_VALID)
320 return mmcra & POWER7P_MMCRA_SIAR_VALID;
321 }
e6878835 322
323 return 1;
324}
325
d52f2dc4
MN
326
327/* Reset all possible BHRB entries */
328static void power_pmu_bhrb_reset(void)
329{
330 asm volatile(PPC_CLRBHRB);
331}
332
333static void power_pmu_bhrb_enable(struct perf_event *event)
334{
335 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
336
337 if (!ppmu->bhrb_nr)
338 return;
339
340 /* Clear BHRB if we changed task context to avoid data leaks */
341 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
342 power_pmu_bhrb_reset();
343 cpuhw->bhrb_context = event->ctx;
344 }
345 cpuhw->bhrb_users++;
346}
347
348static void power_pmu_bhrb_disable(struct perf_event *event)
349{
350 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
351
352 if (!ppmu->bhrb_nr)
353 return;
354
355 cpuhw->bhrb_users--;
356 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
357
358 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
359 /* BHRB cannot be turned off when other
360 * events are active on the PMU.
361 */
362
363 /* avoid stale pointer */
364 cpuhw->bhrb_context = NULL;
365 }
366}
367
368/* Called from ctxsw to prevent one process's branch entries to
369 * mingle with the other process's entries during context switch.
370 */
371void power_pmu_flush_branch_stack(void)
372{
373 if (ppmu->bhrb_nr)
374 power_pmu_bhrb_reset();
375}
69123184
MN
376/* Calculate the to address for a branch */
377static __u64 power_pmu_bhrb_to(u64 addr)
378{
379 unsigned int instr;
380 int ret;
381 __u64 target;
382
383 if (is_kernel_addr(addr))
384 return branch_target((unsigned int *)addr);
385
386 /* Userspace: need copy instruction here then translate it */
387 pagefault_disable();
388 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
389 if (ret) {
390 pagefault_enable();
391 return 0;
392 }
393 pagefault_enable();
394
395 target = branch_target(&instr);
396 if ((!target) || (instr & BRANCH_ABSOLUTE))
397 return target;
398
399 /* Translate relative branch target from kernel to user address */
400 return target - (unsigned long)&instr + addr;
401}
d52f2dc4 402
d52f2dc4 403/* Processing BHRB entries */
506e70d1 404void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
d52f2dc4
MN
405{
406 u64 val;
407 u64 addr;
506e70d1 408 int r_index, u_index, pred;
d52f2dc4
MN
409
410 r_index = 0;
411 u_index = 0;
412 while (r_index < ppmu->bhrb_nr) {
413 /* Assembly read function */
506e70d1
MN
414 val = read_bhrb(r_index++);
415 if (!val)
416 /* Terminal marker: End of valid BHRB entries */
d52f2dc4 417 break;
506e70d1 418 else {
d52f2dc4
MN
419 addr = val & BHRB_EA;
420 pred = val & BHRB_PREDICTION;
d52f2dc4 421
506e70d1
MN
422 if (!addr)
423 /* invalid entry */
d52f2dc4 424 continue;
d52f2dc4 425
506e70d1
MN
426 /* Branches are read most recent first (ie. mfbhrb 0 is
427 * the most recent branch).
428 * There are two types of valid entries:
429 * 1) a target entry which is the to address of a
430 * computed goto like a blr,bctr,btar. The next
431 * entry read from the bhrb will be branch
432 * corresponding to this target (ie. the actual
433 * blr/bctr/btar instruction).
434 * 2) a from address which is an actual branch. If a
435 * target entry proceeds this, then this is the
436 * matching branch for that target. If this is not
437 * following a target entry, then this is a branch
438 * where the target is given as an immediate field
439 * in the instruction (ie. an i or b form branch).
440 * In this case we need to read the instruction from
441 * memory to determine the target/to address.
442 */
d52f2dc4 443
d52f2dc4 444 if (val & BHRB_TARGET) {
506e70d1
MN
445 /* Target branches use two entries
446 * (ie. computed gotos/XL form)
447 */
448 cpuhw->bhrb_entries[u_index].to = addr;
449 cpuhw->bhrb_entries[u_index].mispred = pred;
450 cpuhw->bhrb_entries[u_index].predicted = ~pred;
d52f2dc4 451
506e70d1
MN
452 /* Get from address in next entry */
453 val = read_bhrb(r_index++);
454 addr = val & BHRB_EA;
455 if (val & BHRB_TARGET) {
456 /* Shouldn't have two targets in a
457 row.. Reset index and try again */
458 r_index--;
459 addr = 0;
460 }
461 cpuhw->bhrb_entries[u_index].from = addr;
d52f2dc4 462 } else {
506e70d1
MN
463 /* Branches to immediate field
464 (ie I or B form) */
d52f2dc4 465 cpuhw->bhrb_entries[u_index].from = addr;
69123184
MN
466 cpuhw->bhrb_entries[u_index].to =
467 power_pmu_bhrb_to(addr);
d52f2dc4
MN
468 cpuhw->bhrb_entries[u_index].mispred = pred;
469 cpuhw->bhrb_entries[u_index].predicted = ~pred;
d52f2dc4 470 }
506e70d1
MN
471 u_index++;
472
d52f2dc4
MN
473 }
474 }
475 cpuhw->bhrb_stack.nr = u_index;
476 return;
477}
478
330a1eb7
ME
479static bool is_ebb_event(struct perf_event *event)
480{
481 /*
482 * This could be a per-PMU callback, but we'd rather avoid the cost. We
483 * check that the PMU supports EBB, meaning those that don't can still
484 * use bit 63 of the event code for something else if they wish.
485 */
486 return (ppmu->flags & PPMU_EBB) &&
8d7c55d0 487 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
330a1eb7
ME
488}
489
490static int ebb_event_check(struct perf_event *event)
491{
492 struct perf_event *leader = event->group_leader;
493
494 /* Event and group leader must agree on EBB */
495 if (is_ebb_event(leader) != is_ebb_event(event))
496 return -EINVAL;
497
498 if (is_ebb_event(event)) {
499 if (!(event->attach_state & PERF_ATTACH_TASK))
500 return -EINVAL;
501
502 if (!leader->attr.pinned || !leader->attr.exclusive)
503 return -EINVAL;
504
505 if (event->attr.inherit || event->attr.sample_period ||
506 event->attr.enable_on_exec || event->attr.freq)
507 return -EINVAL;
508 }
509
510 return 0;
511}
512
513static void ebb_event_add(struct perf_event *event)
514{
515 if (!is_ebb_event(event) || current->thread.used_ebb)
516 return;
517
518 /*
519 * IFF this is the first time we've added an EBB event, set
520 * PMXE in the user MMCR0 so we can detect when it's cleared by
521 * userspace. We need this so that we can context switch while
522 * userspace is in the EBB handler (where PMXE is 0).
523 */
524 current->thread.used_ebb = 1;
525 current->thread.mmcr0 |= MMCR0_PMXE;
526}
527
528static void ebb_switch_out(unsigned long mmcr0)
529{
530 if (!(mmcr0 & MMCR0_EBE))
531 return;
532
533 current->thread.siar = mfspr(SPRN_SIAR);
534 current->thread.sier = mfspr(SPRN_SIER);
535 current->thread.sdar = mfspr(SPRN_SDAR);
536 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
537 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
538}
539
540static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
541{
542 if (!ebb)
543 goto out;
544
545 /* Enable EBB and read/write to all 6 PMCs for userspace */
546 mmcr0 |= MMCR0_EBE | MMCR0_PMCC_U6;
547
548 /* Add any bits from the user reg, FC or PMAO */
549 mmcr0 |= current->thread.mmcr0;
550
551 /* Be careful not to set PMXE if userspace had it cleared */
552 if (!(current->thread.mmcr0 & MMCR0_PMXE))
553 mmcr0 &= ~MMCR0_PMXE;
554
555 mtspr(SPRN_SIAR, current->thread.siar);
556 mtspr(SPRN_SIER, current->thread.sier);
557 mtspr(SPRN_SDAR, current->thread.sdar);
558 mtspr(SPRN_MMCR2, current->thread.mmcr2);
559out:
560 return mmcr0;
561}
98fb1807
PM
562#endif /* CONFIG_PPC64 */
563
cdd6c482 564static void perf_event_interrupt(struct pt_regs *regs);
7595d63b 565
cdd6c482 566void perf_event_print_debug(void)
4574910e
PM
567{
568}
569
4574910e 570/*
57c0c15b 571 * Read one performance monitor counter (PMC).
4574910e
PM
572 */
573static unsigned long read_pmc(int idx)
574{
575 unsigned long val;
576
577 switch (idx) {
578 case 1:
579 val = mfspr(SPRN_PMC1);
580 break;
581 case 2:
582 val = mfspr(SPRN_PMC2);
583 break;
584 case 3:
585 val = mfspr(SPRN_PMC3);
586 break;
587 case 4:
588 val = mfspr(SPRN_PMC4);
589 break;
590 case 5:
591 val = mfspr(SPRN_PMC5);
592 break;
593 case 6:
594 val = mfspr(SPRN_PMC6);
595 break;
98fb1807 596#ifdef CONFIG_PPC64
4574910e
PM
597 case 7:
598 val = mfspr(SPRN_PMC7);
599 break;
600 case 8:
601 val = mfspr(SPRN_PMC8);
602 break;
98fb1807 603#endif /* CONFIG_PPC64 */
4574910e
PM
604 default:
605 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
606 val = 0;
607 }
608 return val;
609}
610
611/*
612 * Write one PMC.
613 */
614static void write_pmc(int idx, unsigned long val)
615{
616 switch (idx) {
617 case 1:
618 mtspr(SPRN_PMC1, val);
619 break;
620 case 2:
621 mtspr(SPRN_PMC2, val);
622 break;
623 case 3:
624 mtspr(SPRN_PMC3, val);
625 break;
626 case 4:
627 mtspr(SPRN_PMC4, val);
628 break;
629 case 5:
630 mtspr(SPRN_PMC5, val);
631 break;
632 case 6:
633 mtspr(SPRN_PMC6, val);
634 break;
98fb1807 635#ifdef CONFIG_PPC64
4574910e
PM
636 case 7:
637 mtspr(SPRN_PMC7, val);
638 break;
639 case 8:
640 mtspr(SPRN_PMC8, val);
641 break;
98fb1807 642#endif /* CONFIG_PPC64 */
4574910e
PM
643 default:
644 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
645 }
646}
647
648/*
649 * Check if a set of events can all go on the PMU at once.
650 * If they can't, this will look at alternative codes for the events
651 * and see if any combination of alternative codes is feasible.
cdd6c482 652 * The feasible set is returned in event_id[].
4574910e 653 */
cdd6c482
IM
654static int power_check_constraints(struct cpu_hw_events *cpuhw,
655 u64 event_id[], unsigned int cflags[],
ab7ef2e5 656 int n_ev)
4574910e 657{
448d64f8 658 unsigned long mask, value, nv;
cdd6c482
IM
659 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
660 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
4574910e 661 int i, j;
448d64f8
PM
662 unsigned long addf = ppmu->add_fields;
663 unsigned long tadd = ppmu->test_adder;
4574910e 664
a8f90e90 665 if (n_ev > ppmu->n_counter)
4574910e
PM
666 return -1;
667
668 /* First see if the events will go on as-is */
669 for (i = 0; i < n_ev; ++i) {
ab7ef2e5 670 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
cdd6c482
IM
671 && !ppmu->limited_pmc_event(event_id[i])) {
672 ppmu->get_alternatives(event_id[i], cflags[i],
e51ee31e 673 cpuhw->alternatives[i]);
cdd6c482 674 event_id[i] = cpuhw->alternatives[i][0];
ab7ef2e5 675 }
cdd6c482 676 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
e51ee31e 677 &cpuhw->avalues[i][0]))
4574910e 678 return -1;
4574910e
PM
679 }
680 value = mask = 0;
681 for (i = 0; i < n_ev; ++i) {
e51ee31e
PM
682 nv = (value | cpuhw->avalues[i][0]) +
683 (value & cpuhw->avalues[i][0] & addf);
4574910e 684 if ((((nv + tadd) ^ value) & mask) != 0 ||
e51ee31e
PM
685 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
686 cpuhw->amasks[i][0]) != 0)
4574910e
PM
687 break;
688 value = nv;
e51ee31e 689 mask |= cpuhw->amasks[i][0];
4574910e
PM
690 }
691 if (i == n_ev)
692 return 0; /* all OK */
693
694 /* doesn't work, gather alternatives... */
695 if (!ppmu->get_alternatives)
696 return -1;
697 for (i = 0; i < n_ev; ++i) {
ab7ef2e5 698 choice[i] = 0;
cdd6c482 699 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
e51ee31e 700 cpuhw->alternatives[i]);
4574910e 701 for (j = 1; j < n_alt[i]; ++j)
e51ee31e
PM
702 ppmu->get_constraint(cpuhw->alternatives[i][j],
703 &cpuhw->amasks[i][j],
704 &cpuhw->avalues[i][j]);
4574910e
PM
705 }
706
707 /* enumerate all possibilities and see if any will work */
708 i = 0;
709 j = -1;
710 value = mask = nv = 0;
711 while (i < n_ev) {
712 if (j >= 0) {
713 /* we're backtracking, restore context */
714 value = svalues[i];
715 mask = smasks[i];
716 j = choice[i];
717 }
718 /*
cdd6c482 719 * See if any alternative k for event_id i,
4574910e
PM
720 * where k > j, will satisfy the constraints.
721 */
722 while (++j < n_alt[i]) {
e51ee31e
PM
723 nv = (value | cpuhw->avalues[i][j]) +
724 (value & cpuhw->avalues[i][j] & addf);
4574910e 725 if ((((nv + tadd) ^ value) & mask) == 0 &&
e51ee31e
PM
726 (((nv + tadd) ^ cpuhw->avalues[i][j])
727 & cpuhw->amasks[i][j]) == 0)
4574910e
PM
728 break;
729 }
730 if (j >= n_alt[i]) {
731 /*
732 * No feasible alternative, backtrack
cdd6c482 733 * to event_id i-1 and continue enumerating its
4574910e
PM
734 * alternatives from where we got up to.
735 */
736 if (--i < 0)
737 return -1;
738 } else {
739 /*
cdd6c482
IM
740 * Found a feasible alternative for event_id i,
741 * remember where we got up to with this event_id,
742 * go on to the next event_id, and start with
4574910e
PM
743 * the first alternative for it.
744 */
745 choice[i] = j;
746 svalues[i] = value;
747 smasks[i] = mask;
748 value = nv;
e51ee31e 749 mask |= cpuhw->amasks[i][j];
4574910e
PM
750 ++i;
751 j = -1;
752 }
753 }
754
755 /* OK, we have a feasible combination, tell the caller the solution */
756 for (i = 0; i < n_ev; ++i)
cdd6c482 757 event_id[i] = cpuhw->alternatives[i][choice[i]];
4574910e
PM
758 return 0;
759}
760
0475f9ea 761/*
cdd6c482 762 * Check if newly-added events have consistent settings for
0475f9ea 763 * exclude_{user,kernel,hv} with each other and any previously
cdd6c482 764 * added events.
0475f9ea 765 */
cdd6c482 766static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
ab7ef2e5 767 int n_prev, int n_new)
0475f9ea 768{
ab7ef2e5
PM
769 int eu = 0, ek = 0, eh = 0;
770 int i, n, first;
cdd6c482 771 struct perf_event *event;
0475f9ea
PM
772
773 n = n_prev + n_new;
774 if (n <= 1)
775 return 0;
776
ab7ef2e5
PM
777 first = 1;
778 for (i = 0; i < n; ++i) {
779 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
780 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
781 continue;
782 }
cdd6c482 783 event = ctrs[i];
ab7ef2e5 784 if (first) {
cdd6c482
IM
785 eu = event->attr.exclude_user;
786 ek = event->attr.exclude_kernel;
787 eh = event->attr.exclude_hv;
ab7ef2e5 788 first = 0;
cdd6c482
IM
789 } else if (event->attr.exclude_user != eu ||
790 event->attr.exclude_kernel != ek ||
791 event->attr.exclude_hv != eh) {
0475f9ea 792 return -EAGAIN;
ab7ef2e5 793 }
0475f9ea 794 }
ab7ef2e5
PM
795
796 if (eu || ek || eh)
797 for (i = 0; i < n; ++i)
798 if (cflags[i] & PPMU_LIMITED_PMC_OK)
799 cflags[i] |= PPMU_LIMITED_PMC_REQD;
800
0475f9ea
PM
801 return 0;
802}
803
86c74ab3
EM
804static u64 check_and_compute_delta(u64 prev, u64 val)
805{
806 u64 delta = (val - prev) & 0xfffffffful;
807
808 /*
809 * POWER7 can roll back counter values, if the new value is smaller
810 * than the previous value it will cause the delta and the counter to
811 * have bogus values unless we rolled a counter over. If a coutner is
812 * rolled back, it will be smaller, but within 256, which is the maximum
813 * number of events to rollback at once. If we dectect a rollback
814 * return 0. This can lead to a small lack of precision in the
815 * counters.
816 */
817 if (prev > val && (prev - val) < 256)
818 delta = 0;
819
820 return delta;
821}
822
cdd6c482 823static void power_pmu_read(struct perf_event *event)
4574910e 824{
98fb1807 825 s64 val, delta, prev;
4574910e 826
a4eaf7f1
PZ
827 if (event->hw.state & PERF_HES_STOPPED)
828 return;
829
cdd6c482 830 if (!event->hw.idx)
4574910e 831 return;
330a1eb7
ME
832
833 if (is_ebb_event(event)) {
834 val = read_pmc(event->hw.idx);
835 local64_set(&event->hw.prev_count, val);
836 return;
837 }
838
4574910e
PM
839 /*
840 * Performance monitor interrupts come even when interrupts
841 * are soft-disabled, as long as interrupts are hard-enabled.
842 * Therefore we treat them like NMIs.
843 */
844 do {
e7850595 845 prev = local64_read(&event->hw.prev_count);
4574910e 846 barrier();
cdd6c482 847 val = read_pmc(event->hw.idx);
86c74ab3
EM
848 delta = check_and_compute_delta(prev, val);
849 if (!delta)
850 return;
e7850595 851 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
4574910e 852
e7850595
PZ
853 local64_add(delta, &event->count);
854 local64_sub(delta, &event->hw.period_left);
4574910e
PM
855}
856
ab7ef2e5
PM
857/*
858 * On some machines, PMC5 and PMC6 can't be written, don't respect
859 * the freeze conditions, and don't generate interrupts. This tells
cdd6c482 860 * us if `event' is using such a PMC.
ab7ef2e5
PM
861 */
862static int is_limited_pmc(int pmcnum)
863{
0bbd0d4b
PM
864 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
865 && (pmcnum == 5 || pmcnum == 6);
ab7ef2e5
PM
866}
867
a8f90e90 868static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
ab7ef2e5
PM
869 unsigned long pmc5, unsigned long pmc6)
870{
cdd6c482 871 struct perf_event *event;
ab7ef2e5
PM
872 u64 val, prev, delta;
873 int i;
874
875 for (i = 0; i < cpuhw->n_limited; ++i) {
a8f90e90 876 event = cpuhw->limited_counter[i];
cdd6c482 877 if (!event->hw.idx)
ab7ef2e5 878 continue;
cdd6c482 879 val = (event->hw.idx == 5) ? pmc5 : pmc6;
e7850595 880 prev = local64_read(&event->hw.prev_count);
cdd6c482 881 event->hw.idx = 0;
86c74ab3
EM
882 delta = check_and_compute_delta(prev, val);
883 if (delta)
884 local64_add(delta, &event->count);
ab7ef2e5
PM
885 }
886}
887
a8f90e90 888static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
ab7ef2e5
PM
889 unsigned long pmc5, unsigned long pmc6)
890{
cdd6c482 891 struct perf_event *event;
86c74ab3 892 u64 val, prev;
ab7ef2e5
PM
893 int i;
894
895 for (i = 0; i < cpuhw->n_limited; ++i) {
a8f90e90 896 event = cpuhw->limited_counter[i];
cdd6c482
IM
897 event->hw.idx = cpuhw->limited_hwidx[i];
898 val = (event->hw.idx == 5) ? pmc5 : pmc6;
86c74ab3
EM
899 prev = local64_read(&event->hw.prev_count);
900 if (check_and_compute_delta(prev, val))
901 local64_set(&event->hw.prev_count, val);
cdd6c482 902 perf_event_update_userpage(event);
ab7ef2e5
PM
903 }
904}
905
906/*
cdd6c482 907 * Since limited events don't respect the freeze conditions, we
ab7ef2e5 908 * have to read them immediately after freezing or unfreezing the
cdd6c482
IM
909 * other events. We try to keep the values from the limited
910 * events as consistent as possible by keeping the delay (in
ab7ef2e5 911 * cycles and instructions) between freezing/unfreezing and reading
cdd6c482
IM
912 * the limited events as small and consistent as possible.
913 * Therefore, if any limited events are in use, we read them
ab7ef2e5
PM
914 * both, and always in the same order, to minimize variability,
915 * and do it inside the same asm that writes MMCR0.
916 */
cdd6c482 917static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
ab7ef2e5
PM
918{
919 unsigned long pmc5, pmc6;
920
921 if (!cpuhw->n_limited) {
922 mtspr(SPRN_MMCR0, mmcr0);
923 return;
924 }
925
926 /*
927 * Write MMCR0, then read PMC5 and PMC6 immediately.
dcd945e0
PM
928 * To ensure we don't get a performance monitor interrupt
929 * between writing MMCR0 and freezing/thawing the limited
cdd6c482 930 * events, we first write MMCR0 with the event overflow
dcd945e0 931 * interrupt enable bits turned off.
ab7ef2e5
PM
932 */
933 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
934 : "=&r" (pmc5), "=&r" (pmc6)
dcd945e0
PM
935 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
936 "i" (SPRN_MMCR0),
ab7ef2e5
PM
937 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
938
939 if (mmcr0 & MMCR0_FC)
a8f90e90 940 freeze_limited_counters(cpuhw, pmc5, pmc6);
ab7ef2e5 941 else
a8f90e90 942 thaw_limited_counters(cpuhw, pmc5, pmc6);
dcd945e0
PM
943
944 /*
cdd6c482 945 * Write the full MMCR0 including the event overflow interrupt
dcd945e0
PM
946 * enable bits, if necessary.
947 */
948 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
949 mtspr(SPRN_MMCR0, mmcr0);
ab7ef2e5
PM
950}
951
4574910e 952/*
cdd6c482
IM
953 * Disable all events to prevent PMU interrupts and to allow
954 * events to be added or removed.
4574910e 955 */
a4eaf7f1 956static void power_pmu_disable(struct pmu *pmu)
4574910e 957{
cdd6c482 958 struct cpu_hw_events *cpuhw;
330a1eb7 959 unsigned long flags, mmcr0, val;
4574910e 960
f36a1a13
PM
961 if (!ppmu)
962 return;
4574910e 963 local_irq_save(flags);
cdd6c482 964 cpuhw = &__get_cpu_var(cpu_hw_events);
4574910e 965
448d64f8 966 if (!cpuhw->disabled) {
01d0287f
PM
967 /*
968 * Check if we ever enabled the PMU on this cpu.
969 */
970 if (!cpuhw->pmcs_enabled) {
a6dbf93a 971 ppc_enable_pmcs();
01d0287f
PM
972 cpuhw->pmcs_enabled = 1;
973 }
974
378a6ee9 975 /*
330a1eb7 976 * Set the 'freeze counters' bit, clear EBE/PMCC/PMAO/FC56.
378a6ee9 977 */
330a1eb7 978 val = mmcr0 = mfspr(SPRN_MMCR0);
378a6ee9 979 val |= MMCR0_FC;
330a1eb7 980 val &= ~(MMCR0_EBE | MMCR0_PMCC | MMCR0_PMAO | MMCR0_FC56);
378a6ee9
ME
981
982 /*
983 * The barrier is to make sure the mtspr has been
984 * executed and the PMU has frozen the events etc.
985 * before we return.
986 */
987 write_mmcr0(cpuhw, val);
988 mb();
989
f708223d
PM
990 /*
991 * Disable instruction sampling if it was enabled
992 */
993 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
994 mtspr(SPRN_MMCRA,
995 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
996 mb();
997 }
998
378a6ee9
ME
999 cpuhw->disabled = 1;
1000 cpuhw->n_added = 0;
330a1eb7
ME
1001
1002 ebb_switch_out(mmcr0);
4574910e 1003 }
330a1eb7 1004
4574910e 1005 local_irq_restore(flags);
4574910e
PM
1006}
1007
1008/*
cdd6c482
IM
1009 * Re-enable all events if disable == 0.
1010 * If we were previously disabled and events were added, then
4574910e
PM
1011 * put the new config on the PMU.
1012 */
a4eaf7f1 1013static void power_pmu_enable(struct pmu *pmu)
4574910e 1014{
cdd6c482
IM
1015 struct perf_event *event;
1016 struct cpu_hw_events *cpuhw;
4574910e
PM
1017 unsigned long flags;
1018 long i;
330a1eb7 1019 unsigned long val, mmcr0;
4574910e 1020 s64 left;
cdd6c482 1021 unsigned int hwc_index[MAX_HWEVENTS];
ab7ef2e5
PM
1022 int n_lim;
1023 int idx;
330a1eb7 1024 bool ebb;
4574910e 1025
f36a1a13
PM
1026 if (!ppmu)
1027 return;
4574910e 1028 local_irq_save(flags);
0a48843d 1029
cdd6c482 1030 cpuhw = &__get_cpu_var(cpu_hw_events);
0a48843d
ME
1031 if (!cpuhw->disabled)
1032 goto out;
1033
4ea355b5
ME
1034 if (cpuhw->n_events == 0) {
1035 ppc_set_pmu_inuse(0);
1036 goto out;
1037 }
1038
4574910e
PM
1039 cpuhw->disabled = 0;
1040
330a1eb7
ME
1041 /*
1042 * EBB requires an exclusive group and all events must have the EBB
1043 * flag set, or not set, so we can just check a single event. Also we
1044 * know we have at least one event.
1045 */
1046 ebb = is_ebb_event(cpuhw->event[0]);
1047
4574910e 1048 /*
cdd6c482 1049 * If we didn't change anything, or only removed events,
4574910e
PM
1050 * no need to recalculate MMCR* settings and reset the PMCs.
1051 * Just reenable the PMU with the current MMCR* settings
cdd6c482 1052 * (possibly updated for removal of events).
4574910e
PM
1053 */
1054 if (!cpuhw->n_added) {
f708223d 1055 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
4574910e 1056 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
f708223d 1057 goto out_enable;
4574910e
PM
1058 }
1059
1060 /*
cdd6c482 1061 * Compute MMCR* values for the new set of events
4574910e 1062 */
cdd6c482 1063 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
4574910e
PM
1064 cpuhw->mmcr)) {
1065 /* shouldn't ever get here */
1066 printk(KERN_ERR "oops compute_mmcr failed\n");
1067 goto out;
1068 }
1069
0475f9ea
PM
1070 /*
1071 * Add in MMCR0 freeze bits corresponding to the
cdd6c482
IM
1072 * attr.exclude_* bits for the first event.
1073 * We have already checked that all events have the
1074 * same values for these bits as the first event.
0475f9ea 1075 */
cdd6c482
IM
1076 event = cpuhw->event[0];
1077 if (event->attr.exclude_user)
0475f9ea 1078 cpuhw->mmcr[0] |= MMCR0_FCP;
cdd6c482
IM
1079 if (event->attr.exclude_kernel)
1080 cpuhw->mmcr[0] |= freeze_events_kernel;
1081 if (event->attr.exclude_hv)
0475f9ea
PM
1082 cpuhw->mmcr[0] |= MMCR0_FCHV;
1083
4574910e
PM
1084 /*
1085 * Write the new configuration to MMCR* with the freeze
cdd6c482
IM
1086 * bit set and set the hardware events to their initial values.
1087 * Then unfreeze the events.
4574910e 1088 */
a6dbf93a 1089 ppc_set_pmu_inuse(1);
f708223d 1090 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
4574910e
PM
1091 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1092 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1093 | MMCR0_FC);
1094
1095 /*
cdd6c482 1096 * Read off any pre-existing events that need to move
4574910e
PM
1097 * to another PMC.
1098 */
cdd6c482
IM
1099 for (i = 0; i < cpuhw->n_events; ++i) {
1100 event = cpuhw->event[i];
1101 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1102 power_pmu_read(event);
1103 write_pmc(event->hw.idx, 0);
1104 event->hw.idx = 0;
4574910e
PM
1105 }
1106 }
1107
1108 /*
cdd6c482 1109 * Initialize the PMCs for all the new and moved events.
4574910e 1110 */
ab7ef2e5 1111 cpuhw->n_limited = n_lim = 0;
cdd6c482
IM
1112 for (i = 0; i < cpuhw->n_events; ++i) {
1113 event = cpuhw->event[i];
1114 if (event->hw.idx)
4574910e 1115 continue;
ab7ef2e5
PM
1116 idx = hwc_index[i] + 1;
1117 if (is_limited_pmc(idx)) {
a8f90e90 1118 cpuhw->limited_counter[n_lim] = event;
ab7ef2e5
PM
1119 cpuhw->limited_hwidx[n_lim] = idx;
1120 ++n_lim;
1121 continue;
1122 }
330a1eb7
ME
1123
1124 if (ebb)
1125 val = local64_read(&event->hw.prev_count);
1126 else {
1127 val = 0;
1128 if (event->hw.sample_period) {
1129 left = local64_read(&event->hw.period_left);
1130 if (left < 0x80000000L)
1131 val = 0x80000000L - left;
1132 }
1133 local64_set(&event->hw.prev_count, val);
4574910e 1134 }
330a1eb7 1135
cdd6c482 1136 event->hw.idx = idx;
a4eaf7f1
PZ
1137 if (event->hw.state & PERF_HES_STOPPED)
1138 val = 0;
ab7ef2e5 1139 write_pmc(idx, val);
330a1eb7 1140
cdd6c482 1141 perf_event_update_userpage(event);
4574910e 1142 }
ab7ef2e5 1143 cpuhw->n_limited = n_lim;
4574910e 1144 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
f708223d
PM
1145
1146 out_enable:
330a1eb7
ME
1147 mmcr0 = ebb_switch_in(ebb, cpuhw->mmcr[0]);
1148
f708223d 1149 mb();
330a1eb7 1150 write_mmcr0(cpuhw, mmcr0);
4574910e 1151
f708223d
PM
1152 /*
1153 * Enable instruction sampling if necessary
1154 */
1155 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1156 mb();
1157 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1158 }
1159
4574910e 1160 out:
3925f46b
AK
1161 if (cpuhw->bhrb_users)
1162 ppmu->config_bhrb(cpuhw->bhrb_filter);
1163
4574910e
PM
1164 local_irq_restore(flags);
1165}
1166
cdd6c482
IM
1167static int collect_events(struct perf_event *group, int max_count,
1168 struct perf_event *ctrs[], u64 *events,
ab7ef2e5 1169 unsigned int *flags)
4574910e
PM
1170{
1171 int n = 0;
cdd6c482 1172 struct perf_event *event;
4574910e 1173
cdd6c482 1174 if (!is_software_event(group)) {
4574910e
PM
1175 if (n >= max_count)
1176 return -1;
1177 ctrs[n] = group;
cdd6c482 1178 flags[n] = group->hw.event_base;
4574910e
PM
1179 events[n++] = group->hw.config;
1180 }
a8f90e90 1181 list_for_each_entry(event, &group->sibling_list, group_entry) {
cdd6c482
IM
1182 if (!is_software_event(event) &&
1183 event->state != PERF_EVENT_STATE_OFF) {
4574910e
PM
1184 if (n >= max_count)
1185 return -1;
cdd6c482
IM
1186 ctrs[n] = event;
1187 flags[n] = event->hw.event_base;
1188 events[n++] = event->hw.config;
4574910e
PM
1189 }
1190 }
1191 return n;
1192}
1193
4574910e 1194/*
cdd6c482
IM
1195 * Add a event to the PMU.
1196 * If all events are not already frozen, then we disable and
9e35ad38 1197 * re-enable the PMU in order to get hw_perf_enable to do the
4574910e
PM
1198 * actual work of reconfiguring the PMU.
1199 */
a4eaf7f1 1200static int power_pmu_add(struct perf_event *event, int ef_flags)
4574910e 1201{
cdd6c482 1202 struct cpu_hw_events *cpuhw;
4574910e 1203 unsigned long flags;
4574910e
PM
1204 int n0;
1205 int ret = -EAGAIN;
1206
1207 local_irq_save(flags);
33696fc0 1208 perf_pmu_disable(event->pmu);
4574910e
PM
1209
1210 /*
cdd6c482 1211 * Add the event to the list (if there is room)
4574910e
PM
1212 * and check whether the total set is still feasible.
1213 */
cdd6c482
IM
1214 cpuhw = &__get_cpu_var(cpu_hw_events);
1215 n0 = cpuhw->n_events;
a8f90e90 1216 if (n0 >= ppmu->n_counter)
4574910e 1217 goto out;
cdd6c482
IM
1218 cpuhw->event[n0] = event;
1219 cpuhw->events[n0] = event->hw.config;
1220 cpuhw->flags[n0] = event->hw.event_base;
8e6d5573 1221
f53d168c 1222 /*
1223 * This event may have been disabled/stopped in record_and_restart()
1224 * because we exceeded the ->event_limit. If re-starting the event,
1225 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1226 * notification is re-enabled.
1227 */
a4eaf7f1
PZ
1228 if (!(ef_flags & PERF_EF_START))
1229 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
f53d168c 1230 else
1231 event->hw.state = 0;
a4eaf7f1 1232
8e6d5573
LM
1233 /*
1234 * If group events scheduling transaction was started,
25985edc 1235 * skip the schedulability test here, it will be performed
8e6d5573
LM
1236 * at commit time(->commit_txn) as a whole
1237 */
8d2cacbb 1238 if (cpuhw->group_flag & PERF_EVENT_TXN)
8e6d5573
LM
1239 goto nocheck;
1240
cdd6c482 1241 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
0475f9ea 1242 goto out;
e51ee31e 1243 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
4574910e 1244 goto out;
cdd6c482 1245 event->hw.config = cpuhw->events[n0];
8e6d5573
LM
1246
1247nocheck:
330a1eb7
ME
1248 ebb_event_add(event);
1249
cdd6c482 1250 ++cpuhw->n_events;
4574910e
PM
1251 ++cpuhw->n_added;
1252
1253 ret = 0;
1254 out:
ff3d79dc 1255 if (has_branch_stack(event)) {
3925f46b 1256 power_pmu_bhrb_enable(event);
ff3d79dc
AK
1257 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1258 event->attr.branch_sample_type);
1259 }
3925f46b 1260
33696fc0 1261 perf_pmu_enable(event->pmu);
4574910e
PM
1262 local_irq_restore(flags);
1263 return ret;
1264}
1265
1266/*
cdd6c482 1267 * Remove a event from the PMU.
4574910e 1268 */
a4eaf7f1 1269static void power_pmu_del(struct perf_event *event, int ef_flags)
4574910e 1270{
cdd6c482 1271 struct cpu_hw_events *cpuhw;
4574910e 1272 long i;
4574910e
PM
1273 unsigned long flags;
1274
1275 local_irq_save(flags);
33696fc0 1276 perf_pmu_disable(event->pmu);
4574910e 1277
cdd6c482
IM
1278 power_pmu_read(event);
1279
1280 cpuhw = &__get_cpu_var(cpu_hw_events);
1281 for (i = 0; i < cpuhw->n_events; ++i) {
1282 if (event == cpuhw->event[i]) {
219a92a4 1283 while (++i < cpuhw->n_events) {
cdd6c482 1284 cpuhw->event[i-1] = cpuhw->event[i];
219a92a4
ME
1285 cpuhw->events[i-1] = cpuhw->events[i];
1286 cpuhw->flags[i-1] = cpuhw->flags[i];
1287 }
cdd6c482
IM
1288 --cpuhw->n_events;
1289 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1290 if (event->hw.idx) {
1291 write_pmc(event->hw.idx, 0);
1292 event->hw.idx = 0;
ab7ef2e5 1293 }
cdd6c482 1294 perf_event_update_userpage(event);
4574910e
PM
1295 break;
1296 }
1297 }
ab7ef2e5 1298 for (i = 0; i < cpuhw->n_limited; ++i)
a8f90e90 1299 if (event == cpuhw->limited_counter[i])
ab7ef2e5
PM
1300 break;
1301 if (i < cpuhw->n_limited) {
1302 while (++i < cpuhw->n_limited) {
a8f90e90 1303 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
ab7ef2e5
PM
1304 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1305 }
1306 --cpuhw->n_limited;
1307 }
cdd6c482
IM
1308 if (cpuhw->n_events == 0) {
1309 /* disable exceptions if no events are running */
4574910e
PM
1310 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1311 }
1312
3925f46b
AK
1313 if (has_branch_stack(event))
1314 power_pmu_bhrb_disable(event);
1315
33696fc0 1316 perf_pmu_enable(event->pmu);
4574910e
PM
1317 local_irq_restore(flags);
1318}
1319
8a7b8cb9 1320/*
a4eaf7f1
PZ
1321 * POWER-PMU does not support disabling individual counters, hence
1322 * program their cycle counter to their max value and ignore the interrupts.
8a7b8cb9 1323 */
a4eaf7f1
PZ
1324
1325static void power_pmu_start(struct perf_event *event, int ef_flags)
8a7b8cb9 1326{
8a7b8cb9 1327 unsigned long flags;
a4eaf7f1 1328 s64 left;
9a45a940 1329 unsigned long val;
8a7b8cb9 1330
cdd6c482 1331 if (!event->hw.idx || !event->hw.sample_period)
8a7b8cb9 1332 return;
a4eaf7f1
PZ
1333
1334 if (!(event->hw.state & PERF_HES_STOPPED))
1335 return;
1336
1337 if (ef_flags & PERF_EF_RELOAD)
1338 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1339
1340 local_irq_save(flags);
1341 perf_pmu_disable(event->pmu);
1342
1343 event->hw.state = 0;
1344 left = local64_read(&event->hw.period_left);
9a45a940
AB
1345
1346 val = 0;
1347 if (left < 0x80000000L)
1348 val = 0x80000000L - left;
1349
1350 write_pmc(event->hw.idx, val);
a4eaf7f1
PZ
1351
1352 perf_event_update_userpage(event);
1353 perf_pmu_enable(event->pmu);
1354 local_irq_restore(flags);
1355}
1356
1357static void power_pmu_stop(struct perf_event *event, int ef_flags)
1358{
1359 unsigned long flags;
1360
1361 if (!event->hw.idx || !event->hw.sample_period)
1362 return;
1363
1364 if (event->hw.state & PERF_HES_STOPPED)
1365 return;
1366
8a7b8cb9 1367 local_irq_save(flags);
33696fc0 1368 perf_pmu_disable(event->pmu);
a4eaf7f1 1369
cdd6c482 1370 power_pmu_read(event);
a4eaf7f1
PZ
1371 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1372 write_pmc(event->hw.idx, 0);
1373
cdd6c482 1374 perf_event_update_userpage(event);
33696fc0 1375 perf_pmu_enable(event->pmu);
8a7b8cb9
PM
1376 local_irq_restore(flags);
1377}
1378
8e6d5573
LM
1379/*
1380 * Start group events scheduling transaction
1381 * Set the flag to make pmu::enable() not perform the
1382 * schedulability test, it will be performed at commit time
1383 */
51b0fe39 1384void power_pmu_start_txn(struct pmu *pmu)
8e6d5573
LM
1385{
1386 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1387
33696fc0 1388 perf_pmu_disable(pmu);
8d2cacbb 1389 cpuhw->group_flag |= PERF_EVENT_TXN;
8e6d5573
LM
1390 cpuhw->n_txn_start = cpuhw->n_events;
1391}
1392
1393/*
1394 * Stop group events scheduling transaction
1395 * Clear the flag and pmu::enable() will perform the
1396 * schedulability test.
1397 */
51b0fe39 1398void power_pmu_cancel_txn(struct pmu *pmu)
8e6d5573
LM
1399{
1400 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1401
8d2cacbb 1402 cpuhw->group_flag &= ~PERF_EVENT_TXN;
33696fc0 1403 perf_pmu_enable(pmu);
8e6d5573
LM
1404}
1405
1406/*
1407 * Commit group events scheduling transaction
1408 * Perform the group schedulability test as a whole
1409 * Return 0 if success
1410 */
51b0fe39 1411int power_pmu_commit_txn(struct pmu *pmu)
8e6d5573
LM
1412{
1413 struct cpu_hw_events *cpuhw;
1414 long i, n;
1415
1416 if (!ppmu)
1417 return -EAGAIN;
1418 cpuhw = &__get_cpu_var(cpu_hw_events);
1419 n = cpuhw->n_events;
1420 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1421 return -EAGAIN;
1422 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1423 if (i < 0)
1424 return -EAGAIN;
1425
1426 for (i = cpuhw->n_txn_start; i < n; ++i)
1427 cpuhw->event[i]->hw.config = cpuhw->events[i];
1428
8d2cacbb 1429 cpuhw->group_flag &= ~PERF_EVENT_TXN;
33696fc0 1430 perf_pmu_enable(pmu);
8e6d5573
LM
1431 return 0;
1432}
1433
ab7ef2e5 1434/*
cdd6c482 1435 * Return 1 if we might be able to put event on a limited PMC,
ab7ef2e5 1436 * or 0 if not.
cdd6c482 1437 * A event can only go on a limited PMC if it counts something
ab7ef2e5
PM
1438 * that a limited PMC can count, doesn't require interrupts, and
1439 * doesn't exclude any processor mode.
1440 */
cdd6c482 1441static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
ab7ef2e5
PM
1442 unsigned int flags)
1443{
1444 int n;
ef923214 1445 u64 alt[MAX_EVENT_ALTERNATIVES];
ab7ef2e5 1446
cdd6c482
IM
1447 if (event->attr.exclude_user
1448 || event->attr.exclude_kernel
1449 || event->attr.exclude_hv
1450 || event->attr.sample_period)
ab7ef2e5
PM
1451 return 0;
1452
1453 if (ppmu->limited_pmc_event(ev))
1454 return 1;
1455
1456 /*
cdd6c482 1457 * The requested event_id isn't on a limited PMC already;
ab7ef2e5
PM
1458 * see if any alternative code goes on a limited PMC.
1459 */
1460 if (!ppmu->get_alternatives)
1461 return 0;
1462
1463 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1464 n = ppmu->get_alternatives(ev, flags, alt);
ab7ef2e5 1465
ef923214 1466 return n > 0;
ab7ef2e5
PM
1467}
1468
1469/*
cdd6c482
IM
1470 * Find an alternative event_id that goes on a normal PMC, if possible,
1471 * and return the event_id code, or 0 if there is no such alternative.
1472 * (Note: event_id code 0 is "don't count" on all machines.)
ab7ef2e5 1473 */
ef923214 1474static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
ab7ef2e5 1475{
ef923214 1476 u64 alt[MAX_EVENT_ALTERNATIVES];
ab7ef2e5
PM
1477 int n;
1478
1479 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1480 n = ppmu->get_alternatives(ev, flags, alt);
1481 if (!n)
1482 return 0;
1483 return alt[0];
1484}
1485
cdd6c482
IM
1486/* Number of perf_events counting hardware events */
1487static atomic_t num_events;
7595d63b
PM
1488/* Used to avoid races in calling reserve/release_pmc_hardware */
1489static DEFINE_MUTEX(pmc_reserve_mutex);
1490
1491/*
cdd6c482 1492 * Release the PMU if this is the last perf_event.
7595d63b 1493 */
cdd6c482 1494static void hw_perf_event_destroy(struct perf_event *event)
7595d63b 1495{
cdd6c482 1496 if (!atomic_add_unless(&num_events, -1, 1)) {
7595d63b 1497 mutex_lock(&pmc_reserve_mutex);
cdd6c482 1498 if (atomic_dec_return(&num_events) == 0)
7595d63b
PM
1499 release_pmc_hardware();
1500 mutex_unlock(&pmc_reserve_mutex);
1501 }
1502}
1503
106b506c 1504/*
cdd6c482 1505 * Translate a generic cache event_id config to a raw event_id code.
106b506c
PM
1506 */
1507static int hw_perf_cache_event(u64 config, u64 *eventp)
1508{
1509 unsigned long type, op, result;
1510 int ev;
1511
1512 if (!ppmu->cache_events)
1513 return -EINVAL;
1514
1515 /* unpack config */
1516 type = config & 0xff;
1517 op = (config >> 8) & 0xff;
1518 result = (config >> 16) & 0xff;
1519
1520 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1521 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1522 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1523 return -EINVAL;
1524
1525 ev = (*ppmu->cache_events)[type][op][result];
1526 if (ev == 0)
1527 return -EOPNOTSUPP;
1528 if (ev == -1)
1529 return -EINVAL;
1530 *eventp = ev;
1531 return 0;
1532}
1533
b0a873eb 1534static int power_pmu_event_init(struct perf_event *event)
4574910e 1535{
ef923214
PM
1536 u64 ev;
1537 unsigned long flags;
cdd6c482
IM
1538 struct perf_event *ctrs[MAX_HWEVENTS];
1539 u64 events[MAX_HWEVENTS];
1540 unsigned int cflags[MAX_HWEVENTS];
4574910e 1541 int n;
7595d63b 1542 int err;
cdd6c482 1543 struct cpu_hw_events *cpuhw;
4574910e
PM
1544
1545 if (!ppmu)
b0a873eb
PZ
1546 return -ENOENT;
1547
3925f46b
AK
1548 if (has_branch_stack(event)) {
1549 /* PMU has BHRB enabled */
1550 if (!(ppmu->flags & PPMU_BHRB))
1551 return -EOPNOTSUPP;
1552 }
2481c5fa 1553
cdd6c482 1554 switch (event->attr.type) {
106b506c 1555 case PERF_TYPE_HARDWARE:
cdd6c482 1556 ev = event->attr.config;
9aaa131a 1557 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
b0a873eb 1558 return -EOPNOTSUPP;
4574910e 1559 ev = ppmu->generic_events[ev];
106b506c
PM
1560 break;
1561 case PERF_TYPE_HW_CACHE:
cdd6c482 1562 err = hw_perf_cache_event(event->attr.config, &ev);
106b506c 1563 if (err)
b0a873eb 1564 return err;
106b506c
PM
1565 break;
1566 case PERF_TYPE_RAW:
cdd6c482 1567 ev = event->attr.config;
106b506c 1568 break;
90c8f954 1569 default:
b0a873eb 1570 return -ENOENT;
4574910e 1571 }
b0a873eb 1572
cdd6c482
IM
1573 event->hw.config_base = ev;
1574 event->hw.idx = 0;
4574910e 1575
0475f9ea
PM
1576 /*
1577 * If we are not running on a hypervisor, force the
1578 * exclude_hv bit to 0 so that we don't care what
d095cd46 1579 * the user set it to.
0475f9ea
PM
1580 */
1581 if (!firmware_has_feature(FW_FEATURE_LPAR))
cdd6c482 1582 event->attr.exclude_hv = 0;
ab7ef2e5
PM
1583
1584 /*
cdd6c482 1585 * If this is a per-task event, then we can use
ab7ef2e5
PM
1586 * PM_RUN_* events interchangeably with their non RUN_*
1587 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1588 * XXX we should check if the task is an idle task.
1589 */
1590 flags = 0;
57fa7214 1591 if (event->attach_state & PERF_ATTACH_TASK)
ab7ef2e5
PM
1592 flags |= PPMU_ONLY_COUNT_RUN;
1593
1594 /*
cdd6c482
IM
1595 * If this machine has limited events, check whether this
1596 * event_id could go on a limited event.
ab7ef2e5 1597 */
0bbd0d4b 1598 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
cdd6c482 1599 if (can_go_on_limited_pmc(event, ev, flags)) {
ab7ef2e5
PM
1600 flags |= PPMU_LIMITED_PMC_OK;
1601 } else if (ppmu->limited_pmc_event(ev)) {
1602 /*
cdd6c482 1603 * The requested event_id is on a limited PMC,
ab7ef2e5
PM
1604 * but we can't use a limited PMC; see if any
1605 * alternative goes on a normal PMC.
1606 */
1607 ev = normal_pmc_alternative(ev, flags);
1608 if (!ev)
b0a873eb 1609 return -EINVAL;
ab7ef2e5
PM
1610 }
1611 }
1612
330a1eb7
ME
1613 /* Extra checks for EBB */
1614 err = ebb_event_check(event);
1615 if (err)
1616 return err;
1617
4574910e
PM
1618 /*
1619 * If this is in a group, check if it can go on with all the
cdd6c482 1620 * other hardware events in the group. We assume the event
4574910e
PM
1621 * hasn't been linked into its leader's sibling list at this point.
1622 */
1623 n = 0;
cdd6c482 1624 if (event->group_leader != event) {
a8f90e90 1625 n = collect_events(event->group_leader, ppmu->n_counter - 1,
ab7ef2e5 1626 ctrs, events, cflags);
4574910e 1627 if (n < 0)
b0a873eb 1628 return -EINVAL;
4574910e 1629 }
0475f9ea 1630 events[n] = ev;
cdd6c482 1631 ctrs[n] = event;
ab7ef2e5
PM
1632 cflags[n] = flags;
1633 if (check_excludes(ctrs, cflags, n, 1))
b0a873eb 1634 return -EINVAL;
e51ee31e 1635
cdd6c482 1636 cpuhw = &get_cpu_var(cpu_hw_events);
e51ee31e 1637 err = power_check_constraints(cpuhw, events, cflags, n + 1);
3925f46b
AK
1638
1639 if (has_branch_stack(event)) {
1640 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1641 event->attr.branch_sample_type);
1642
1643 if(cpuhw->bhrb_filter == -1)
1644 return -EOPNOTSUPP;
1645 }
1646
cdd6c482 1647 put_cpu_var(cpu_hw_events);
e51ee31e 1648 if (err)
b0a873eb 1649 return -EINVAL;
4574910e 1650
cdd6c482
IM
1651 event->hw.config = events[n];
1652 event->hw.event_base = cflags[n];
1653 event->hw.last_period = event->hw.sample_period;
e7850595 1654 local64_set(&event->hw.period_left, event->hw.last_period);
7595d63b 1655
330a1eb7
ME
1656 /*
1657 * For EBB events we just context switch the PMC value, we don't do any
1658 * of the sample_period logic. We use hw.prev_count for this.
1659 */
1660 if (is_ebb_event(event))
1661 local64_set(&event->hw.prev_count, 0);
1662
7595d63b
PM
1663 /*
1664 * See if we need to reserve the PMU.
cdd6c482 1665 * If no events are currently in use, then we have to take a
7595d63b
PM
1666 * mutex to ensure that we don't race with another task doing
1667 * reserve_pmc_hardware or release_pmc_hardware.
1668 */
1669 err = 0;
cdd6c482 1670 if (!atomic_inc_not_zero(&num_events)) {
7595d63b 1671 mutex_lock(&pmc_reserve_mutex);
cdd6c482
IM
1672 if (atomic_read(&num_events) == 0 &&
1673 reserve_pmc_hardware(perf_event_interrupt))
7595d63b
PM
1674 err = -EBUSY;
1675 else
cdd6c482 1676 atomic_inc(&num_events);
7595d63b
PM
1677 mutex_unlock(&pmc_reserve_mutex);
1678 }
cdd6c482 1679 event->destroy = hw_perf_event_destroy;
7595d63b 1680
b0a873eb 1681 return err;
4574910e
PM
1682}
1683
35edc2a5
PZ
1684static int power_pmu_event_idx(struct perf_event *event)
1685{
1686 return event->hw.idx;
1687}
1688
1c53a270
SB
1689ssize_t power_events_sysfs_show(struct device *dev,
1690 struct device_attribute *attr, char *page)
1691{
1692 struct perf_pmu_events_attr *pmu_attr;
1693
1694 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1695
1696 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1697}
1698
b0a873eb 1699struct pmu power_pmu = {
a4eaf7f1
PZ
1700 .pmu_enable = power_pmu_enable,
1701 .pmu_disable = power_pmu_disable,
b0a873eb 1702 .event_init = power_pmu_event_init,
a4eaf7f1
PZ
1703 .add = power_pmu_add,
1704 .del = power_pmu_del,
1705 .start = power_pmu_start,
1706 .stop = power_pmu_stop,
b0a873eb 1707 .read = power_pmu_read,
b0a873eb
PZ
1708 .start_txn = power_pmu_start_txn,
1709 .cancel_txn = power_pmu_cancel_txn,
1710 .commit_txn = power_pmu_commit_txn,
35edc2a5 1711 .event_idx = power_pmu_event_idx,
3925f46b 1712 .flush_branch_stack = power_pmu_flush_branch_stack,
b0a873eb
PZ
1713};
1714
4574910e 1715/*
57c0c15b 1716 * A counter has overflowed; update its count and record
4574910e
PM
1717 * things if requested. Note that interrupts are hard-disabled
1718 * here so there is no possibility of being interrupted.
1719 */
cdd6c482 1720static void record_and_restart(struct perf_event *event, unsigned long val,
a8b0ca17 1721 struct pt_regs *regs)
4574910e 1722{
cdd6c482 1723 u64 period = event->hw.sample_period;
4574910e
PM
1724 s64 prev, delta, left;
1725 int record = 0;
1726
a4eaf7f1
PZ
1727 if (event->hw.state & PERF_HES_STOPPED) {
1728 write_pmc(event->hw.idx, 0);
1729 return;
1730 }
1731
4574910e 1732 /* we don't have to worry about interrupts here */
e7850595 1733 prev = local64_read(&event->hw.prev_count);
86c74ab3 1734 delta = check_and_compute_delta(prev, val);
e7850595 1735 local64_add(delta, &event->count);
4574910e
PM
1736
1737 /*
cdd6c482 1738 * See if the total period for this event has expired,
4574910e
PM
1739 * and update for the next period.
1740 */
1741 val = 0;
e7850595 1742 left = local64_read(&event->hw.period_left) - delta;
e13e895f
MN
1743 if (delta == 0)
1744 left++;
60db5e09 1745 if (period) {
4574910e 1746 if (left <= 0) {
60db5e09 1747 left += period;
4574910e 1748 if (left <= 0)
60db5e09 1749 left = period;
e6878835 1750 record = siar_valid(regs);
4bca770e 1751 event->hw.last_period = event->hw.sample_period;
4574910e 1752 }
98fb1807
PM
1753 if (left < 0x80000000LL)
1754 val = 0x80000000LL - left;
4574910e 1755 }
4574910e 1756
a4eaf7f1
PZ
1757 write_pmc(event->hw.idx, val);
1758 local64_set(&event->hw.prev_count, val);
1759 local64_set(&event->hw.period_left, left);
1760 perf_event_update_userpage(event);
1761
4574910e
PM
1762 /*
1763 * Finally record data if requested.
1764 */
0bbd0d4b 1765 if (record) {
dc1d628a
PZ
1766 struct perf_sample_data data;
1767
fd0d000b 1768 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
df1a132b 1769
cdd6c482 1770 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
98fb1807
PM
1771 perf_get_data_addr(regs, &data.addr);
1772
3925f46b
AK
1773 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1774 struct cpu_hw_events *cpuhw;
1775 cpuhw = &__get_cpu_var(cpu_hw_events);
1776 power_pmu_bhrb_read(cpuhw);
1777 data.br_stack = &cpuhw->bhrb_stack;
1778 }
1779
a8b0ca17 1780 if (perf_event_overflow(event, &data, regs))
a4eaf7f1 1781 power_pmu_stop(event, 0);
0bbd0d4b
PM
1782 }
1783}
1784
1785/*
1786 * Called from generic code to get the misc flags (i.e. processor mode)
cdd6c482 1787 * for an event_id.
0bbd0d4b
PM
1788 */
1789unsigned long perf_misc_flags(struct pt_regs *regs)
1790{
98fb1807 1791 u32 flags = perf_get_misc_flags(regs);
0bbd0d4b 1792
98fb1807
PM
1793 if (flags)
1794 return flags;
cdd6c482
IM
1795 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1796 PERF_RECORD_MISC_KERNEL;
0bbd0d4b
PM
1797}
1798
1799/*
1800 * Called from generic code to get the instruction pointer
cdd6c482 1801 * for an event_id.
0bbd0d4b
PM
1802 */
1803unsigned long perf_instruction_pointer(struct pt_regs *regs)
1804{
33904054 1805 bool use_siar = regs_use_siar(regs);
0bbd0d4b 1806
e6878835 1807 if (use_siar && siar_valid(regs))
75382aa7 1808 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
e6878835 1809 else if (use_siar)
1810 return 0; // no valid instruction pointer
75382aa7 1811 else
1ce447b9 1812 return regs->nip;
4574910e
PM
1813}
1814
bc09c219 1815static bool pmc_overflow_power7(unsigned long val)
0837e324 1816{
0837e324
AB
1817 /*
1818 * Events on POWER7 can roll back if a speculative event doesn't
1819 * eventually complete. Unfortunately in some rare cases they will
1820 * raise a performance monitor exception. We need to catch this to
1821 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1822 * cycles from overflow.
1823 *
1824 * We only do this if the first pass fails to find any overflowing
1825 * PMCs because a user might set a period of less than 256 and we
1826 * don't want to mistakenly reset them.
1827 */
bc09c219
MN
1828 if ((0x80000000 - val) <= 256)
1829 return true;
1830
1831 return false;
1832}
1833
1834static bool pmc_overflow(unsigned long val)
1835{
1836 if ((int)val < 0)
0837e324
AB
1837 return true;
1838
1839 return false;
1840}
1841
4574910e
PM
1842/*
1843 * Performance monitor interrupt stuff
1844 */
cdd6c482 1845static void perf_event_interrupt(struct pt_regs *regs)
4574910e 1846{
bc09c219 1847 int i, j;
cdd6c482
IM
1848 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1849 struct perf_event *event;
bc09c219
MN
1850 unsigned long val[8];
1851 int found, active;
ca8f2d7f
PM
1852 int nmi;
1853
ab7ef2e5 1854 if (cpuhw->n_limited)
a8f90e90 1855 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
ab7ef2e5
PM
1856 mfspr(SPRN_PMC6));
1857
98fb1807 1858 perf_read_regs(regs);
0bbd0d4b 1859
98fb1807 1860 nmi = perf_intr_is_nmi(regs);
ca8f2d7f
PM
1861 if (nmi)
1862 nmi_enter();
1863 else
1864 irq_enter();
4574910e 1865
bc09c219
MN
1866 /* Read all the PMCs since we'll need them a bunch of times */
1867 for (i = 0; i < ppmu->n_counter; ++i)
1868 val[i] = read_pmc(i + 1);
1869
1870 /* Try to find what caused the IRQ */
1871 found = 0;
1872 for (i = 0; i < ppmu->n_counter; ++i) {
1873 if (!pmc_overflow(val[i]))
ab7ef2e5 1874 continue;
bc09c219
MN
1875 if (is_limited_pmc(i + 1))
1876 continue; /* these won't generate IRQs */
1877 /*
1878 * We've found one that's overflowed. For active
1879 * counters we need to log this. For inactive
1880 * counters, we need to reset it anyway
1881 */
1882 found = 1;
1883 active = 0;
1884 for (j = 0; j < cpuhw->n_events; ++j) {
1885 event = cpuhw->event[j];
1886 if (event->hw.idx == (i + 1)) {
1887 active = 1;
1888 record_and_restart(event, val[i], regs);
1889 break;
1890 }
4574910e 1891 }
bc09c219
MN
1892 if (!active)
1893 /* reset non active counters that have overflowed */
1894 write_pmc(i + 1, 0);
4574910e 1895 }
bc09c219
MN
1896 if (!found && pvr_version_is(PVR_POWER7)) {
1897 /* check active counters for special buggy p7 overflow */
1898 for (i = 0; i < cpuhw->n_events; ++i) {
1899 event = cpuhw->event[i];
1900 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
ab7ef2e5 1901 continue;
bc09c219
MN
1902 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
1903 /* event has overflowed in a buggy way*/
1904 found = 1;
1905 record_and_restart(event,
1906 val[event->hw.idx - 1],
1907 regs);
1908 }
4574910e
PM
1909 }
1910 }
6772faa1 1911 if (!found && !nmi && printk_ratelimit())
bc09c219 1912 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
4574910e
PM
1913
1914 /*
1915 * Reset MMCR0 to its normal value. This will set PMXE and
57c0c15b 1916 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
4574910e 1917 * and thus allow interrupts to occur again.
cdd6c482 1918 * XXX might want to use MSR.PM to keep the events frozen until
4574910e
PM
1919 * we get back out of this interrupt.
1920 */
ab7ef2e5 1921 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
4574910e 1922
ca8f2d7f
PM
1923 if (nmi)
1924 nmi_exit();
1925 else
db4fb5ac 1926 irq_exit();
4574910e
PM
1927}
1928
3f6da390 1929static void power_pmu_setup(int cpu)
01d0287f 1930{
cdd6c482 1931 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
01d0287f 1932
f36a1a13
PM
1933 if (!ppmu)
1934 return;
01d0287f
PM
1935 memset(cpuhw, 0, sizeof(*cpuhw));
1936 cpuhw->mmcr[0] = MMCR0_FC;
1937}
1938
061d19f2 1939static int
85cfabbc 1940power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
3f6da390
PZ
1941{
1942 unsigned int cpu = (long)hcpu;
1943
1944 switch (action & ~CPU_TASKS_FROZEN) {
1945 case CPU_UP_PREPARE:
1946 power_pmu_setup(cpu);
1947 break;
1948
1949 default:
1950 break;
1951 }
1952
1953 return NOTIFY_OK;
1954}
1955
061d19f2 1956int register_power_pmu(struct power_pmu *pmu)
4574910e 1957{
079b3c56
PM
1958 if (ppmu)
1959 return -EBUSY; /* something's already registered */
1960
1961 ppmu = pmu;
1962 pr_info("%s performance monitor hardware support registered\n",
1963 pmu->name);
d095cd46 1964
1c53a270
SB
1965 power_pmu.attr_groups = ppmu->attr_groups;
1966
98fb1807 1967#ifdef MSR_HV
d095cd46
PM
1968 /*
1969 * Use FCHV to ignore kernel events if MSR.HV is set.
1970 */
1971 if (mfmsr() & MSR_HV)
cdd6c482 1972 freeze_events_kernel = MMCR0_FCHV;
98fb1807 1973#endif /* CONFIG_PPC64 */
d095cd46 1974
2e80a82a 1975 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
3f6da390
PZ
1976 perf_cpu_notifier(power_pmu_notifier);
1977
4574910e
PM
1978 return 0;
1979}