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