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