blob: 25e656c14945727acb42661d84a54d87ab5808df [file] [log] [blame]
Paul Mackerras45749102009-01-09 20:21:55 +11001/*
2 * Performance counter 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_counter.h>
14#include <linux/percpu.h>
15#include <linux/hardirq.h>
16#include <asm/reg.h>
17#include <asm/pmc.h>
Paul Mackerras01d02872009-01-14 13:44:19 +110018#include <asm/machdep.h>
Paul Mackerras0475f9e2009-02-11 14:35:35 +110019#include <asm/firmware.h>
Paul Mackerras0bbd0d42009-05-14 13:31:48 +100020#include <asm/ptrace.h>
Paul Mackerras45749102009-01-09 20:21:55 +110021
22struct cpu_hw_counters {
23 int n_counters;
24 int n_percpu;
25 int disabled;
26 int n_added;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +100027 int n_limited;
28 u8 pmcs_enabled;
Paul Mackerras45749102009-01-09 20:21:55 +110029 struct perf_counter *counter[MAX_HWCOUNTERS];
Paul Mackerrasef923212009-05-14 13:29:14 +100030 u64 events[MAX_HWCOUNTERS];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +100031 unsigned int flags[MAX_HWCOUNTERS];
Paul Mackerras448d64f2009-06-17 21:51:13 +100032 unsigned long mmcr[3];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +100033 struct perf_counter *limited_counter[MAX_LIMITED_HWCOUNTERS];
34 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
Paul Mackerras45749102009-01-09 20:21:55 +110035};
36DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
37
38struct power_pmu *ppmu;
39
Paul Mackerrasd095cd42009-02-23 23:01:28 +110040/*
41 * Normally, to ignore kernel events we set the FCS (freeze counters
42 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
43 * hypervisor bit set in the MSR, or if we are running on a processor
44 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
45 * then we need to use the FCHV bit to ignore kernel events.
46 */
47static unsigned int freeze_counters_kernel = MMCR0_FCS;
48
Paul Mackerras7595d632009-03-30 19:07:07 +020049static void perf_counter_interrupt(struct pt_regs *regs);
50
Paul Mackerras45749102009-01-09 20:21:55 +110051void perf_counter_print_debug(void)
52{
53}
54
55/*
Paul Mackerras45749102009-01-09 20:21:55 +110056 * Read one performance monitor counter (PMC).
57 */
58static unsigned long read_pmc(int idx)
59{
60 unsigned long val;
61
62 switch (idx) {
63 case 1:
64 val = mfspr(SPRN_PMC1);
65 break;
66 case 2:
67 val = mfspr(SPRN_PMC2);
68 break;
69 case 3:
70 val = mfspr(SPRN_PMC3);
71 break;
72 case 4:
73 val = mfspr(SPRN_PMC4);
74 break;
75 case 5:
76 val = mfspr(SPRN_PMC5);
77 break;
78 case 6:
79 val = mfspr(SPRN_PMC6);
80 break;
81 case 7:
82 val = mfspr(SPRN_PMC7);
83 break;
84 case 8:
85 val = mfspr(SPRN_PMC8);
86 break;
87 default:
88 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
89 val = 0;
90 }
91 return val;
92}
93
94/*
95 * Write one PMC.
96 */
97static void write_pmc(int idx, unsigned long val)
98{
99 switch (idx) {
100 case 1:
101 mtspr(SPRN_PMC1, val);
102 break;
103 case 2:
104 mtspr(SPRN_PMC2, val);
105 break;
106 case 3:
107 mtspr(SPRN_PMC3, val);
108 break;
109 case 4:
110 mtspr(SPRN_PMC4, val);
111 break;
112 case 5:
113 mtspr(SPRN_PMC5, val);
114 break;
115 case 6:
116 mtspr(SPRN_PMC6, val);
117 break;
118 case 7:
119 mtspr(SPRN_PMC7, val);
120 break;
121 case 8:
122 mtspr(SPRN_PMC8, val);
123 break;
124 default:
125 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
126 }
127}
128
129/*
130 * Check if a set of events can all go on the PMU at once.
131 * If they can't, this will look at alternative codes for the events
132 * and see if any combination of alternative codes is feasible.
133 * The feasible set is returned in event[].
134 */
Paul Mackerrasef923212009-05-14 13:29:14 +1000135static int power_check_constraints(u64 event[], unsigned int cflags[],
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000136 int n_ev)
Paul Mackerras45749102009-01-09 20:21:55 +1100137{
Paul Mackerras448d64f2009-06-17 21:51:13 +1000138 unsigned long mask, value, nv;
Paul Mackerrasef923212009-05-14 13:29:14 +1000139 u64 alternatives[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
Paul Mackerras448d64f2009-06-17 21:51:13 +1000140 unsigned long amasks[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
141 unsigned long avalues[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
142 unsigned long smasks[MAX_HWCOUNTERS], svalues[MAX_HWCOUNTERS];
Paul Mackerras45749102009-01-09 20:21:55 +1100143 int n_alt[MAX_HWCOUNTERS], choice[MAX_HWCOUNTERS];
144 int i, j;
Paul Mackerras448d64f2009-06-17 21:51:13 +1000145 unsigned long addf = ppmu->add_fields;
146 unsigned long tadd = ppmu->test_adder;
Paul Mackerras45749102009-01-09 20:21:55 +1100147
148 if (n_ev > ppmu->n_counter)
149 return -1;
150
151 /* First see if the events will go on as-is */
152 for (i = 0; i < n_ev; ++i) {
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000153 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
154 && !ppmu->limited_pmc_event(event[i])) {
155 ppmu->get_alternatives(event[i], cflags[i],
156 alternatives[i]);
157 event[i] = alternatives[i][0];
158 }
Paul Mackerras45749102009-01-09 20:21:55 +1100159 if (ppmu->get_constraint(event[i], &amasks[i][0],
160 &avalues[i][0]))
161 return -1;
Paul Mackerras45749102009-01-09 20:21:55 +1100162 }
163 value = mask = 0;
164 for (i = 0; i < n_ev; ++i) {
165 nv = (value | avalues[i][0]) + (value & avalues[i][0] & addf);
166 if ((((nv + tadd) ^ value) & mask) != 0 ||
167 (((nv + tadd) ^ avalues[i][0]) & amasks[i][0]) != 0)
168 break;
169 value = nv;
170 mask |= amasks[i][0];
171 }
172 if (i == n_ev)
173 return 0; /* all OK */
174
175 /* doesn't work, gather alternatives... */
176 if (!ppmu->get_alternatives)
177 return -1;
178 for (i = 0; i < n_ev; ++i) {
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000179 choice[i] = 0;
180 n_alt[i] = ppmu->get_alternatives(event[i], cflags[i],
181 alternatives[i]);
Paul Mackerras45749102009-01-09 20:21:55 +1100182 for (j = 1; j < n_alt[i]; ++j)
183 ppmu->get_constraint(alternatives[i][j],
184 &amasks[i][j], &avalues[i][j]);
185 }
186
187 /* enumerate all possibilities and see if any will work */
188 i = 0;
189 j = -1;
190 value = mask = nv = 0;
191 while (i < n_ev) {
192 if (j >= 0) {
193 /* we're backtracking, restore context */
194 value = svalues[i];
195 mask = smasks[i];
196 j = choice[i];
197 }
198 /*
199 * See if any alternative k for event i,
200 * where k > j, will satisfy the constraints.
201 */
202 while (++j < n_alt[i]) {
203 nv = (value | avalues[i][j]) +
204 (value & avalues[i][j] & addf);
205 if ((((nv + tadd) ^ value) & mask) == 0 &&
206 (((nv + tadd) ^ avalues[i][j])
207 & amasks[i][j]) == 0)
208 break;
209 }
210 if (j >= n_alt[i]) {
211 /*
212 * No feasible alternative, backtrack
213 * to event i-1 and continue enumerating its
214 * alternatives from where we got up to.
215 */
216 if (--i < 0)
217 return -1;
218 } else {
219 /*
220 * Found a feasible alternative for event i,
221 * remember where we got up to with this event,
222 * go on to the next event, and start with
223 * the first alternative for it.
224 */
225 choice[i] = j;
226 svalues[i] = value;
227 smasks[i] = mask;
228 value = nv;
229 mask |= amasks[i][j];
230 ++i;
231 j = -1;
232 }
233 }
234
235 /* OK, we have a feasible combination, tell the caller the solution */
236 for (i = 0; i < n_ev; ++i)
237 event[i] = alternatives[i][choice[i]];
238 return 0;
239}
240
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100241/*
242 * Check if newly-added counters have consistent settings for
243 * exclude_{user,kernel,hv} with each other and any previously
244 * added counters.
245 */
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000246static int check_excludes(struct perf_counter **ctrs, unsigned int cflags[],
247 int n_prev, int n_new)
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100248{
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000249 int eu = 0, ek = 0, eh = 0;
250 int i, n, first;
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100251 struct perf_counter *counter;
252
253 n = n_prev + n_new;
254 if (n <= 1)
255 return 0;
256
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000257 first = 1;
258 for (i = 0; i < n; ++i) {
259 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
260 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
261 continue;
262 }
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100263 counter = ctrs[i];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000264 if (first) {
Peter Zijlstra0d486962009-06-02 19:22:16 +0200265 eu = counter->attr.exclude_user;
266 ek = counter->attr.exclude_kernel;
267 eh = counter->attr.exclude_hv;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000268 first = 0;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200269 } else if (counter->attr.exclude_user != eu ||
270 counter->attr.exclude_kernel != ek ||
271 counter->attr.exclude_hv != eh) {
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100272 return -EAGAIN;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000273 }
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100274 }
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000275
276 if (eu || ek || eh)
277 for (i = 0; i < n; ++i)
278 if (cflags[i] & PPMU_LIMITED_PMC_OK)
279 cflags[i] |= PPMU_LIMITED_PMC_REQD;
280
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100281 return 0;
282}
283
Robert Richter4aeb0b42009-04-29 12:47:03 +0200284static void power_pmu_read(struct perf_counter *counter)
Paul Mackerras45749102009-01-09 20:21:55 +1100285{
286 long val, delta, prev;
287
288 if (!counter->hw.idx)
289 return;
290 /*
291 * Performance monitor interrupts come even when interrupts
292 * are soft-disabled, as long as interrupts are hard-enabled.
293 * Therefore we treat them like NMIs.
294 */
295 do {
296 prev = atomic64_read(&counter->hw.prev_count);
297 barrier();
298 val = read_pmc(counter->hw.idx);
299 } while (atomic64_cmpxchg(&counter->hw.prev_count, prev, val) != prev);
300
301 /* The counters are only 32 bits wide */
302 delta = (val - prev) & 0xfffffffful;
303 atomic64_add(delta, &counter->count);
304 atomic64_sub(delta, &counter->hw.period_left);
305}
306
307/*
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000308 * On some machines, PMC5 and PMC6 can't be written, don't respect
309 * the freeze conditions, and don't generate interrupts. This tells
310 * us if `counter' is using such a PMC.
311 */
312static int is_limited_pmc(int pmcnum)
313{
Paul Mackerras0bbd0d42009-05-14 13:31:48 +1000314 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
315 && (pmcnum == 5 || pmcnum == 6);
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000316}
317
318static void freeze_limited_counters(struct cpu_hw_counters *cpuhw,
319 unsigned long pmc5, unsigned long pmc6)
320{
321 struct perf_counter *counter;
322 u64 val, prev, delta;
323 int i;
324
325 for (i = 0; i < cpuhw->n_limited; ++i) {
326 counter = cpuhw->limited_counter[i];
327 if (!counter->hw.idx)
328 continue;
329 val = (counter->hw.idx == 5) ? pmc5 : pmc6;
330 prev = atomic64_read(&counter->hw.prev_count);
331 counter->hw.idx = 0;
332 delta = (val - prev) & 0xfffffffful;
333 atomic64_add(delta, &counter->count);
334 }
335}
336
337static void thaw_limited_counters(struct cpu_hw_counters *cpuhw,
338 unsigned long pmc5, unsigned long pmc6)
339{
340 struct perf_counter *counter;
341 u64 val;
342 int i;
343
344 for (i = 0; i < cpuhw->n_limited; ++i) {
345 counter = cpuhw->limited_counter[i];
346 counter->hw.idx = cpuhw->limited_hwidx[i];
347 val = (counter->hw.idx == 5) ? pmc5 : pmc6;
348 atomic64_set(&counter->hw.prev_count, val);
349 perf_counter_update_userpage(counter);
350 }
351}
352
353/*
354 * Since limited counters don't respect the freeze conditions, we
355 * have to read them immediately after freezing or unfreezing the
356 * other counters. We try to keep the values from the limited
357 * counters as consistent as possible by keeping the delay (in
358 * cycles and instructions) between freezing/unfreezing and reading
359 * the limited counters as small and consistent as possible.
360 * Therefore, if any limited counters are in use, we read them
361 * both, and always in the same order, to minimize variability,
362 * and do it inside the same asm that writes MMCR0.
363 */
364static void write_mmcr0(struct cpu_hw_counters *cpuhw, unsigned long mmcr0)
365{
366 unsigned long pmc5, pmc6;
367
368 if (!cpuhw->n_limited) {
369 mtspr(SPRN_MMCR0, mmcr0);
370 return;
371 }
372
373 /*
374 * Write MMCR0, then read PMC5 and PMC6 immediately.
Paul Mackerrasdcd945e2009-06-03 19:40:36 +1000375 * To ensure we don't get a performance monitor interrupt
376 * between writing MMCR0 and freezing/thawing the limited
377 * counters, we first write MMCR0 with the counter overflow
378 * interrupt enable bits turned off.
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000379 */
380 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
381 : "=&r" (pmc5), "=&r" (pmc6)
Paul Mackerrasdcd945e2009-06-03 19:40:36 +1000382 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
383 "i" (SPRN_MMCR0),
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000384 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
385
386 if (mmcr0 & MMCR0_FC)
387 freeze_limited_counters(cpuhw, pmc5, pmc6);
388 else
389 thaw_limited_counters(cpuhw, pmc5, pmc6);
Paul Mackerrasdcd945e2009-06-03 19:40:36 +1000390
391 /*
392 * Write the full MMCR0 including the counter overflow interrupt
393 * enable bits, if necessary.
394 */
395 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
396 mtspr(SPRN_MMCR0, mmcr0);
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000397}
398
399/*
Paul Mackerras45749102009-01-09 20:21:55 +1100400 * Disable all counters to prevent PMU interrupts and to allow
401 * counters to be added or removed.
402 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200403void hw_perf_disable(void)
Paul Mackerras45749102009-01-09 20:21:55 +1100404{
405 struct cpu_hw_counters *cpuhw;
Paul Mackerras45749102009-01-09 20:21:55 +1100406 unsigned long flags;
407
408 local_irq_save(flags);
409 cpuhw = &__get_cpu_var(cpu_hw_counters);
410
Paul Mackerras448d64f2009-06-17 21:51:13 +1000411 if (!cpuhw->disabled) {
Paul Mackerras45749102009-01-09 20:21:55 +1100412 cpuhw->disabled = 1;
413 cpuhw->n_added = 0;
414
415 /*
Paul Mackerras01d02872009-01-14 13:44:19 +1100416 * Check if we ever enabled the PMU on this cpu.
417 */
418 if (!cpuhw->pmcs_enabled) {
419 if (ppc_md.enable_pmcs)
420 ppc_md.enable_pmcs();
421 cpuhw->pmcs_enabled = 1;
422 }
423
424 /*
Paul Mackerrasf7082232009-04-08 20:30:18 +1000425 * Disable instruction sampling if it was enabled
426 */
427 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
428 mtspr(SPRN_MMCRA,
429 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
430 mb();
431 }
432
433 /*
Paul Mackerras45749102009-01-09 20:21:55 +1100434 * Set the 'freeze counters' bit.
435 * The barrier is to make sure the mtspr has been
436 * executed and the PMU has frozen the counters
437 * before we return.
438 */
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000439 write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
Paul Mackerras45749102009-01-09 20:21:55 +1100440 mb();
441 }
442 local_irq_restore(flags);
Paul Mackerras45749102009-01-09 20:21:55 +1100443}
444
445/*
446 * Re-enable all counters if disable == 0.
447 * If we were previously disabled and counters were added, then
448 * put the new config on the PMU.
449 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200450void hw_perf_enable(void)
Paul Mackerras45749102009-01-09 20:21:55 +1100451{
452 struct perf_counter *counter;
453 struct cpu_hw_counters *cpuhw;
454 unsigned long flags;
455 long i;
456 unsigned long val;
457 s64 left;
458 unsigned int hwc_index[MAX_HWCOUNTERS];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000459 int n_lim;
460 int idx;
Paul Mackerras45749102009-01-09 20:21:55 +1100461
Paul Mackerras45749102009-01-09 20:21:55 +1100462 local_irq_save(flags);
Paul Mackerrasc0daaf32009-05-18 14:02:12 +1000463 cpuhw = &__get_cpu_var(cpu_hw_counters);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200464 if (!cpuhw->disabled) {
465 local_irq_restore(flags);
466 return;
467 }
Paul Mackerras45749102009-01-09 20:21:55 +1100468 cpuhw->disabled = 0;
469
470 /*
471 * If we didn't change anything, or only removed counters,
472 * no need to recalculate MMCR* settings and reset the PMCs.
473 * Just reenable the PMU with the current MMCR* settings
474 * (possibly updated for removal of counters).
475 */
476 if (!cpuhw->n_added) {
Paul Mackerrasf7082232009-04-08 20:30:18 +1000477 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
Paul Mackerras45749102009-01-09 20:21:55 +1100478 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
Paul Mackerras01d02872009-01-14 13:44:19 +1100479 if (cpuhw->n_counters == 0)
480 get_lppaca()->pmcregs_in_use = 0;
Paul Mackerrasf7082232009-04-08 20:30:18 +1000481 goto out_enable;
Paul Mackerras45749102009-01-09 20:21:55 +1100482 }
483
484 /*
485 * Compute MMCR* values for the new set of counters
486 */
487 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index,
488 cpuhw->mmcr)) {
489 /* shouldn't ever get here */
490 printk(KERN_ERR "oops compute_mmcr failed\n");
491 goto out;
492 }
493
494 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100495 * Add in MMCR0 freeze bits corresponding to the
Peter Zijlstra0d486962009-06-02 19:22:16 +0200496 * attr.exclude_* bits for the first counter.
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100497 * We have already checked that all counters have the
498 * same values for these bits as the first counter.
499 */
500 counter = cpuhw->counter[0];
Peter Zijlstra0d486962009-06-02 19:22:16 +0200501 if (counter->attr.exclude_user)
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100502 cpuhw->mmcr[0] |= MMCR0_FCP;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200503 if (counter->attr.exclude_kernel)
Paul Mackerrasd095cd42009-02-23 23:01:28 +1100504 cpuhw->mmcr[0] |= freeze_counters_kernel;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200505 if (counter->attr.exclude_hv)
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100506 cpuhw->mmcr[0] |= MMCR0_FCHV;
507
508 /*
Paul Mackerras45749102009-01-09 20:21:55 +1100509 * Write the new configuration to MMCR* with the freeze
510 * bit set and set the hardware counters to their initial values.
511 * Then unfreeze the counters.
512 */
Paul Mackerras01d02872009-01-14 13:44:19 +1100513 get_lppaca()->pmcregs_in_use = 1;
Paul Mackerrasf7082232009-04-08 20:30:18 +1000514 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
Paul Mackerras45749102009-01-09 20:21:55 +1100515 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
516 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
517 | MMCR0_FC);
518
519 /*
520 * Read off any pre-existing counters that need to move
521 * to another PMC.
522 */
523 for (i = 0; i < cpuhw->n_counters; ++i) {
524 counter = cpuhw->counter[i];
525 if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) {
Robert Richter4aeb0b42009-04-29 12:47:03 +0200526 power_pmu_read(counter);
Paul Mackerras45749102009-01-09 20:21:55 +1100527 write_pmc(counter->hw.idx, 0);
528 counter->hw.idx = 0;
529 }
530 }
531
532 /*
533 * Initialize the PMCs for all the new and moved counters.
534 */
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000535 cpuhw->n_limited = n_lim = 0;
Paul Mackerras45749102009-01-09 20:21:55 +1100536 for (i = 0; i < cpuhw->n_counters; ++i) {
537 counter = cpuhw->counter[i];
538 if (counter->hw.idx)
539 continue;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000540 idx = hwc_index[i] + 1;
541 if (is_limited_pmc(idx)) {
542 cpuhw->limited_counter[n_lim] = counter;
543 cpuhw->limited_hwidx[n_lim] = idx;
544 ++n_lim;
545 continue;
546 }
Paul Mackerras45749102009-01-09 20:21:55 +1100547 val = 0;
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200548 if (counter->hw.sample_period) {
Paul Mackerras45749102009-01-09 20:21:55 +1100549 left = atomic64_read(&counter->hw.period_left);
550 if (left < 0x80000000L)
551 val = 0x80000000L - left;
552 }
553 atomic64_set(&counter->hw.prev_count, val);
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000554 counter->hw.idx = idx;
555 write_pmc(idx, val);
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100556 perf_counter_update_userpage(counter);
Paul Mackerras45749102009-01-09 20:21:55 +1100557 }
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000558 cpuhw->n_limited = n_lim;
Paul Mackerras45749102009-01-09 20:21:55 +1100559 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
Paul Mackerrasf7082232009-04-08 20:30:18 +1000560
561 out_enable:
562 mb();
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000563 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
Paul Mackerras45749102009-01-09 20:21:55 +1100564
Paul Mackerrasf7082232009-04-08 20:30:18 +1000565 /*
566 * Enable instruction sampling if necessary
567 */
568 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
569 mb();
570 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
571 }
572
Paul Mackerras45749102009-01-09 20:21:55 +1100573 out:
574 local_irq_restore(flags);
575}
576
577static int collect_events(struct perf_counter *group, int max_count,
Paul Mackerrasef923212009-05-14 13:29:14 +1000578 struct perf_counter *ctrs[], u64 *events,
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000579 unsigned int *flags)
Paul Mackerras45749102009-01-09 20:21:55 +1100580{
581 int n = 0;
582 struct perf_counter *counter;
583
584 if (!is_software_counter(group)) {
585 if (n >= max_count)
586 return -1;
587 ctrs[n] = group;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000588 flags[n] = group->hw.counter_base;
Paul Mackerras45749102009-01-09 20:21:55 +1100589 events[n++] = group->hw.config;
590 }
591 list_for_each_entry(counter, &group->sibling_list, list_entry) {
592 if (!is_software_counter(counter) &&
593 counter->state != PERF_COUNTER_STATE_OFF) {
594 if (n >= max_count)
595 return -1;
596 ctrs[n] = counter;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000597 flags[n] = counter->hw.counter_base;
Paul Mackerras45749102009-01-09 20:21:55 +1100598 events[n++] = counter->hw.config;
599 }
600 }
601 return n;
602}
603
604static void counter_sched_in(struct perf_counter *counter, int cpu)
605{
606 counter->state = PERF_COUNTER_STATE_ACTIVE;
607 counter->oncpu = cpu;
Paul Mackerrasdc662702009-04-08 20:30:10 +1000608 counter->tstamp_running += counter->ctx->time - counter->tstamp_stopped;
Paul Mackerras45749102009-01-09 20:21:55 +1100609 if (is_software_counter(counter))
Robert Richter4aeb0b42009-04-29 12:47:03 +0200610 counter->pmu->enable(counter);
Paul Mackerras45749102009-01-09 20:21:55 +1100611}
612
613/*
614 * Called to enable a whole group of counters.
615 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
616 * Assumes the caller has disabled interrupts and has
617 * frozen the PMU with hw_perf_save_disable.
618 */
619int hw_perf_group_sched_in(struct perf_counter *group_leader,
620 struct perf_cpu_context *cpuctx,
621 struct perf_counter_context *ctx, int cpu)
622{
623 struct cpu_hw_counters *cpuhw;
624 long i, n, n0;
625 struct perf_counter *sub;
626
627 cpuhw = &__get_cpu_var(cpu_hw_counters);
628 n0 = cpuhw->n_counters;
629 n = collect_events(group_leader, ppmu->n_counter - n0,
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000630 &cpuhw->counter[n0], &cpuhw->events[n0],
631 &cpuhw->flags[n0]);
Paul Mackerras45749102009-01-09 20:21:55 +1100632 if (n < 0)
633 return -EAGAIN;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000634 if (check_excludes(cpuhw->counter, cpuhw->flags, n0, n))
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100635 return -EAGAIN;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000636 i = power_check_constraints(cpuhw->events, cpuhw->flags, n + n0);
637 if (i < 0)
Paul Mackerras45749102009-01-09 20:21:55 +1100638 return -EAGAIN;
639 cpuhw->n_counters = n0 + n;
640 cpuhw->n_added += n;
641
642 /*
643 * OK, this group can go on; update counter states etc.,
644 * and enable any software counters
645 */
646 for (i = n0; i < n0 + n; ++i)
647 cpuhw->counter[i]->hw.config = cpuhw->events[i];
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100648 cpuctx->active_oncpu += n;
Paul Mackerras45749102009-01-09 20:21:55 +1100649 n = 1;
650 counter_sched_in(group_leader, cpu);
651 list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
652 if (sub->state != PERF_COUNTER_STATE_OFF) {
653 counter_sched_in(sub, cpu);
654 ++n;
655 }
656 }
Paul Mackerras45749102009-01-09 20:21:55 +1100657 ctx->nr_active += n;
658
659 return 1;
660}
661
662/*
663 * Add a counter to the PMU.
664 * If all counters are not already frozen, then we disable and
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200665 * re-enable the PMU in order to get hw_perf_enable to do the
Paul Mackerras45749102009-01-09 20:21:55 +1100666 * actual work of reconfiguring the PMU.
667 */
Robert Richter4aeb0b42009-04-29 12:47:03 +0200668static int power_pmu_enable(struct perf_counter *counter)
Paul Mackerras45749102009-01-09 20:21:55 +1100669{
670 struct cpu_hw_counters *cpuhw;
671 unsigned long flags;
Paul Mackerras45749102009-01-09 20:21:55 +1100672 int n0;
673 int ret = -EAGAIN;
674
675 local_irq_save(flags);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200676 perf_disable();
Paul Mackerras45749102009-01-09 20:21:55 +1100677
678 /*
679 * Add the counter to the list (if there is room)
680 * and check whether the total set is still feasible.
681 */
682 cpuhw = &__get_cpu_var(cpu_hw_counters);
683 n0 = cpuhw->n_counters;
684 if (n0 >= ppmu->n_counter)
685 goto out;
686 cpuhw->counter[n0] = counter;
687 cpuhw->events[n0] = counter->hw.config;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000688 cpuhw->flags[n0] = counter->hw.counter_base;
689 if (check_excludes(cpuhw->counter, cpuhw->flags, n0, 1))
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100690 goto out;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000691 if (power_check_constraints(cpuhw->events, cpuhw->flags, n0 + 1))
Paul Mackerras45749102009-01-09 20:21:55 +1100692 goto out;
693
694 counter->hw.config = cpuhw->events[n0];
695 ++cpuhw->n_counters;
696 ++cpuhw->n_added;
697
698 ret = 0;
699 out:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200700 perf_enable();
Paul Mackerras45749102009-01-09 20:21:55 +1100701 local_irq_restore(flags);
702 return ret;
703}
704
705/*
706 * Remove a counter from the PMU.
707 */
Robert Richter4aeb0b42009-04-29 12:47:03 +0200708static void power_pmu_disable(struct perf_counter *counter)
Paul Mackerras45749102009-01-09 20:21:55 +1100709{
710 struct cpu_hw_counters *cpuhw;
711 long i;
Paul Mackerras45749102009-01-09 20:21:55 +1100712 unsigned long flags;
713
714 local_irq_save(flags);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200715 perf_disable();
Paul Mackerras45749102009-01-09 20:21:55 +1100716
Robert Richter4aeb0b42009-04-29 12:47:03 +0200717 power_pmu_read(counter);
Paul Mackerras45749102009-01-09 20:21:55 +1100718
719 cpuhw = &__get_cpu_var(cpu_hw_counters);
720 for (i = 0; i < cpuhw->n_counters; ++i) {
721 if (counter == cpuhw->counter[i]) {
722 while (++i < cpuhw->n_counters)
723 cpuhw->counter[i-1] = cpuhw->counter[i];
724 --cpuhw->n_counters;
725 ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000726 if (counter->hw.idx) {
727 write_pmc(counter->hw.idx, 0);
728 counter->hw.idx = 0;
729 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100730 perf_counter_update_userpage(counter);
Paul Mackerras45749102009-01-09 20:21:55 +1100731 break;
732 }
733 }
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000734 for (i = 0; i < cpuhw->n_limited; ++i)
735 if (counter == cpuhw->limited_counter[i])
736 break;
737 if (i < cpuhw->n_limited) {
738 while (++i < cpuhw->n_limited) {
739 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
740 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
741 }
742 --cpuhw->n_limited;
743 }
Paul Mackerras45749102009-01-09 20:21:55 +1100744 if (cpuhw->n_counters == 0) {
745 /* disable exceptions if no counters are running */
746 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
747 }
748
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200749 perf_enable();
Paul Mackerras45749102009-01-09 20:21:55 +1100750 local_irq_restore(flags);
751}
752
Paul Mackerras8a7b8cb2009-05-26 16:27:59 +1000753/*
754 * Re-enable interrupts on a counter after they were throttled
755 * because they were coming too fast.
756 */
757static void power_pmu_unthrottle(struct perf_counter *counter)
758{
759 s64 val, left;
760 unsigned long flags;
761
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200762 if (!counter->hw.idx || !counter->hw.sample_period)
Paul Mackerras8a7b8cb2009-05-26 16:27:59 +1000763 return;
764 local_irq_save(flags);
765 perf_disable();
766 power_pmu_read(counter);
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200767 left = counter->hw.sample_period;
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200768 counter->hw.last_period = left;
Paul Mackerras8a7b8cb2009-05-26 16:27:59 +1000769 val = 0;
770 if (left < 0x80000000L)
771 val = 0x80000000L - left;
772 write_pmc(counter->hw.idx, val);
773 atomic64_set(&counter->hw.prev_count, val);
774 atomic64_set(&counter->hw.period_left, left);
775 perf_counter_update_userpage(counter);
776 perf_enable();
777 local_irq_restore(flags);
778}
779
Robert Richter4aeb0b42009-04-29 12:47:03 +0200780struct pmu power_pmu = {
781 .enable = power_pmu_enable,
782 .disable = power_pmu_disable,
783 .read = power_pmu_read,
Paul Mackerras8a7b8cb2009-05-26 16:27:59 +1000784 .unthrottle = power_pmu_unthrottle,
Paul Mackerras45749102009-01-09 20:21:55 +1100785};
786
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000787/*
788 * Return 1 if we might be able to put counter on a limited PMC,
789 * or 0 if not.
790 * A counter can only go on a limited PMC if it counts something
791 * that a limited PMC can count, doesn't require interrupts, and
792 * doesn't exclude any processor mode.
793 */
Paul Mackerrasef923212009-05-14 13:29:14 +1000794static int can_go_on_limited_pmc(struct perf_counter *counter, u64 ev,
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000795 unsigned int flags)
796{
797 int n;
Paul Mackerrasef923212009-05-14 13:29:14 +1000798 u64 alt[MAX_EVENT_ALTERNATIVES];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000799
Peter Zijlstra0d486962009-06-02 19:22:16 +0200800 if (counter->attr.exclude_user
801 || counter->attr.exclude_kernel
802 || counter->attr.exclude_hv
803 || counter->attr.sample_period)
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000804 return 0;
805
806 if (ppmu->limited_pmc_event(ev))
807 return 1;
808
809 /*
810 * The requested event isn't on a limited PMC already;
811 * see if any alternative code goes on a limited PMC.
812 */
813 if (!ppmu->get_alternatives)
814 return 0;
815
816 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
817 n = ppmu->get_alternatives(ev, flags, alt);
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000818
Paul Mackerrasef923212009-05-14 13:29:14 +1000819 return n > 0;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000820}
821
822/*
823 * Find an alternative event that goes on a normal PMC, if possible,
824 * and return the event code, or 0 if there is no such alternative.
825 * (Note: event code 0 is "don't count" on all machines.)
826 */
Paul Mackerrasef923212009-05-14 13:29:14 +1000827static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000828{
Paul Mackerrasef923212009-05-14 13:29:14 +1000829 u64 alt[MAX_EVENT_ALTERNATIVES];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000830 int n;
831
832 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
833 n = ppmu->get_alternatives(ev, flags, alt);
834 if (!n)
835 return 0;
836 return alt[0];
837}
838
Paul Mackerras7595d632009-03-30 19:07:07 +0200839/* Number of perf_counters counting hardware events */
840static atomic_t num_counters;
841/* Used to avoid races in calling reserve/release_pmc_hardware */
842static DEFINE_MUTEX(pmc_reserve_mutex);
843
844/*
845 * Release the PMU if this is the last perf_counter.
846 */
847static void hw_perf_counter_destroy(struct perf_counter *counter)
848{
849 if (!atomic_add_unless(&num_counters, -1, 1)) {
850 mutex_lock(&pmc_reserve_mutex);
851 if (atomic_dec_return(&num_counters) == 0)
852 release_pmc_hardware();
853 mutex_unlock(&pmc_reserve_mutex);
854 }
855}
856
Paul Mackerras106b5062009-06-11 14:55:42 +1000857/*
858 * Translate a generic cache event config to a raw event code.
859 */
860static int hw_perf_cache_event(u64 config, u64 *eventp)
861{
862 unsigned long type, op, result;
863 int ev;
864
865 if (!ppmu->cache_events)
866 return -EINVAL;
867
868 /* unpack config */
869 type = config & 0xff;
870 op = (config >> 8) & 0xff;
871 result = (config >> 16) & 0xff;
872
873 if (type >= PERF_COUNT_HW_CACHE_MAX ||
874 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
875 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
876 return -EINVAL;
877
878 ev = (*ppmu->cache_events)[type][op][result];
879 if (ev == 0)
880 return -EOPNOTSUPP;
881 if (ev == -1)
882 return -EINVAL;
883 *eventp = ev;
884 return 0;
885}
886
Robert Richter4aeb0b42009-04-29 12:47:03 +0200887const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Paul Mackerras45749102009-01-09 20:21:55 +1100888{
Paul Mackerrasef923212009-05-14 13:29:14 +1000889 u64 ev;
890 unsigned long flags;
Paul Mackerras45749102009-01-09 20:21:55 +1100891 struct perf_counter *ctrs[MAX_HWCOUNTERS];
Paul Mackerrasef923212009-05-14 13:29:14 +1000892 u64 events[MAX_HWCOUNTERS];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000893 unsigned int cflags[MAX_HWCOUNTERS];
Paul Mackerras45749102009-01-09 20:21:55 +1100894 int n;
Paul Mackerras7595d632009-03-30 19:07:07 +0200895 int err;
Paul Mackerras45749102009-01-09 20:21:55 +1100896
897 if (!ppmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +0200898 return ERR_PTR(-ENXIO);
Paul Mackerras106b5062009-06-11 14:55:42 +1000899 switch (counter->attr.type) {
900 case PERF_TYPE_HARDWARE:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200901 ev = counter->attr.config;
Paul Mackerras9aaa1312009-03-21 15:31:47 +1100902 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +0200903 return ERR_PTR(-EOPNOTSUPP);
Paul Mackerras45749102009-01-09 20:21:55 +1100904 ev = ppmu->generic_events[ev];
Paul Mackerras106b5062009-06-11 14:55:42 +1000905 break;
906 case PERF_TYPE_HW_CACHE:
907 err = hw_perf_cache_event(counter->attr.config, &ev);
908 if (err)
909 return ERR_PTR(err);
910 break;
911 case PERF_TYPE_RAW:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200912 ev = counter->attr.config;
Paul Mackerras106b5062009-06-11 14:55:42 +1000913 break;
Paul Mackerras90c8f952009-06-15 21:36:52 +1000914 default:
915 return ERR_PTR(-EINVAL);
Paul Mackerras45749102009-01-09 20:21:55 +1100916 }
917 counter->hw.config_base = ev;
918 counter->hw.idx = 0;
919
920 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100921 * If we are not running on a hypervisor, force the
922 * exclude_hv bit to 0 so that we don't care what
Paul Mackerrasd095cd42009-02-23 23:01:28 +1100923 * the user set it to.
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100924 */
925 if (!firmware_has_feature(FW_FEATURE_LPAR))
Peter Zijlstra0d486962009-06-02 19:22:16 +0200926 counter->attr.exclude_hv = 0;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000927
928 /*
929 * If this is a per-task counter, then we can use
930 * PM_RUN_* events interchangeably with their non RUN_*
931 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
932 * XXX we should check if the task is an idle task.
933 */
934 flags = 0;
935 if (counter->ctx->task)
936 flags |= PPMU_ONLY_COUNT_RUN;
937
938 /*
939 * If this machine has limited counters, check whether this
940 * event could go on a limited counter.
941 */
Paul Mackerras0bbd0d42009-05-14 13:31:48 +1000942 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000943 if (can_go_on_limited_pmc(counter, ev, flags)) {
944 flags |= PPMU_LIMITED_PMC_OK;
945 } else if (ppmu->limited_pmc_event(ev)) {
946 /*
947 * The requested event is on a limited PMC,
948 * but we can't use a limited PMC; see if any
949 * alternative goes on a normal PMC.
950 */
951 ev = normal_pmc_alternative(ev, flags);
952 if (!ev)
953 return ERR_PTR(-EINVAL);
954 }
955 }
956
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100957 /*
Paul Mackerras45749102009-01-09 20:21:55 +1100958 * If this is in a group, check if it can go on with all the
959 * other hardware counters in the group. We assume the counter
960 * hasn't been linked into its leader's sibling list at this point.
961 */
962 n = 0;
963 if (counter->group_leader != counter) {
964 n = collect_events(counter->group_leader, ppmu->n_counter - 1,
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000965 ctrs, events, cflags);
Paul Mackerras45749102009-01-09 20:21:55 +1100966 if (n < 0)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +0200967 return ERR_PTR(-EINVAL);
Paul Mackerras45749102009-01-09 20:21:55 +1100968 }
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100969 events[n] = ev;
Paul Mackerras86028592009-03-05 14:05:57 +1100970 ctrs[n] = counter;
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000971 cflags[n] = flags;
972 if (check_excludes(ctrs, cflags, n, 1))
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +0200973 return ERR_PTR(-EINVAL);
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000974 if (power_check_constraints(events, cflags, n + 1))
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +0200975 return ERR_PTR(-EINVAL);
Paul Mackerras45749102009-01-09 20:21:55 +1100976
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100977 counter->hw.config = events[n];
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +1000978 counter->hw.counter_base = cflags[n];
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200979 counter->hw.last_period = counter->hw.sample_period;
980 atomic64_set(&counter->hw.period_left, counter->hw.last_period);
Paul Mackerras7595d632009-03-30 19:07:07 +0200981
982 /*
983 * See if we need to reserve the PMU.
984 * If no counters are currently in use, then we have to take a
985 * mutex to ensure that we don't race with another task doing
986 * reserve_pmc_hardware or release_pmc_hardware.
987 */
988 err = 0;
989 if (!atomic_inc_not_zero(&num_counters)) {
990 mutex_lock(&pmc_reserve_mutex);
991 if (atomic_read(&num_counters) == 0 &&
992 reserve_pmc_hardware(perf_counter_interrupt))
993 err = -EBUSY;
994 else
995 atomic_inc(&num_counters);
996 mutex_unlock(&pmc_reserve_mutex);
997 }
998 counter->destroy = hw_perf_counter_destroy;
999
1000 if (err)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02001001 return ERR_PTR(err);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001002 return &power_pmu;
Paul Mackerras45749102009-01-09 20:21:55 +11001003}
1004
1005/*
Paul Mackerras45749102009-01-09 20:21:55 +11001006 * A counter has overflowed; update its count and record
1007 * things if requested. Note that interrupts are hard-disabled
1008 * here so there is no possibility of being interrupted.
1009 */
1010static void record_and_restart(struct perf_counter *counter, long val,
Paul Mackerrasca8f2d72009-04-09 14:42:56 +10001011 struct pt_regs *regs, int nmi)
Paul Mackerras45749102009-01-09 20:21:55 +11001012{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001013 u64 period = counter->hw.sample_period;
Paul Mackerras448d64f2009-06-17 21:51:13 +10001014 unsigned long mmcra, sdsync;
Paul Mackerras45749102009-01-09 20:21:55 +11001015 s64 prev, delta, left;
1016 int record = 0;
1017
1018 /* we don't have to worry about interrupts here */
1019 prev = atomic64_read(&counter->hw.prev_count);
1020 delta = (val - prev) & 0xfffffffful;
1021 atomic64_add(delta, &counter->count);
1022
1023 /*
1024 * See if the total period for this counter has expired,
1025 * and update for the next period.
1026 */
1027 val = 0;
1028 left = atomic64_read(&counter->hw.period_left) - delta;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001029 if (period) {
Paul Mackerras45749102009-01-09 20:21:55 +11001030 if (left <= 0) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001031 left += period;
Paul Mackerras45749102009-01-09 20:21:55 +11001032 if (left <= 0)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001033 left = period;
Paul Mackerras45749102009-01-09 20:21:55 +11001034 record = 1;
1035 }
1036 if (left < 0x80000000L)
1037 val = 0x80000000L - left;
1038 }
Paul Mackerras45749102009-01-09 20:21:55 +11001039
1040 /*
1041 * Finally record data if requested.
1042 */
Paul Mackerras0bbd0d42009-05-14 13:31:48 +10001043 if (record) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001044 struct perf_sample_data data = {
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001045 .regs = regs,
1046 .addr = 0,
1047 .period = counter->hw.last_period,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001048 };
1049
Paul Mackerras1b58c252009-06-04 09:49:59 +10001050 if (counter->attr.sample_type & PERF_SAMPLE_ADDR) {
Paul Mackerras0bbd0d42009-05-14 13:31:48 +10001051 /*
1052 * The user wants a data address recorded.
1053 * If we're not doing instruction sampling,
1054 * give them the SDAR (sampled data address).
1055 * If we are doing instruction sampling, then only
1056 * give them the SDAR if it corresponds to the
1057 * instruction pointed to by SIAR; this is indicated
1058 * by the [POWER6_]MMCRA_SDSYNC bit in MMCRA.
1059 */
1060 mmcra = regs->dsisr;
1061 sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
1062 POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
1063 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001064 data.addr = mfspr(SPRN_SDAR);
Paul Mackerras0bbd0d42009-05-14 13:31:48 +10001065 }
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001066 if (perf_counter_overflow(counter, nmi, &data)) {
Paul Mackerras8a7b8cb2009-05-26 16:27:59 +10001067 /*
1068 * Interrupts are coming too fast - throttle them
1069 * by setting the counter to 0, so it will be
1070 * at least 2^30 cycles until the next interrupt
1071 * (assuming each counter counts at most 2 counts
1072 * per cycle).
1073 */
1074 val = 0;
1075 left = ~0ULL >> 1;
1076 }
Paul Mackerras0bbd0d42009-05-14 13:31:48 +10001077 }
Paul Mackerras8a7b8cb2009-05-26 16:27:59 +10001078
1079 write_pmc(counter->hw.idx, val);
1080 atomic64_set(&counter->hw.prev_count, val);
1081 atomic64_set(&counter->hw.period_left, left);
1082 perf_counter_update_userpage(counter);
Paul Mackerras0bbd0d42009-05-14 13:31:48 +10001083}
1084
1085/*
1086 * Called from generic code to get the misc flags (i.e. processor mode)
1087 * for an event.
1088 */
1089unsigned long perf_misc_flags(struct pt_regs *regs)
1090{
1091 unsigned long mmcra;
1092
1093 if (TRAP(regs) != 0xf00) {
1094 /* not a PMU interrupt */
1095 return user_mode(regs) ? PERF_EVENT_MISC_USER :
1096 PERF_EVENT_MISC_KERNEL;
1097 }
1098
1099 mmcra = regs->dsisr;
1100 if (ppmu->flags & PPMU_ALT_SIPR) {
1101 if (mmcra & POWER6_MMCRA_SIHV)
1102 return PERF_EVENT_MISC_HYPERVISOR;
1103 return (mmcra & POWER6_MMCRA_SIPR) ? PERF_EVENT_MISC_USER :
1104 PERF_EVENT_MISC_KERNEL;
1105 }
1106 if (mmcra & MMCRA_SIHV)
1107 return PERF_EVENT_MISC_HYPERVISOR;
1108 return (mmcra & MMCRA_SIPR) ? PERF_EVENT_MISC_USER :
1109 PERF_EVENT_MISC_KERNEL;
1110}
1111
1112/*
1113 * Called from generic code to get the instruction pointer
1114 * for an event.
1115 */
1116unsigned long perf_instruction_pointer(struct pt_regs *regs)
1117{
1118 unsigned long mmcra;
1119 unsigned long ip;
1120 unsigned long slot;
1121
1122 if (TRAP(regs) != 0xf00)
1123 return regs->nip; /* not a PMU interrupt */
1124
1125 ip = mfspr(SPRN_SIAR);
1126 mmcra = regs->dsisr;
1127 if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
1128 slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
1129 if (slot > 1)
1130 ip += 4 * (slot - 1);
1131 }
1132 return ip;
Paul Mackerras45749102009-01-09 20:21:55 +11001133}
1134
1135/*
1136 * Performance monitor interrupt stuff
1137 */
1138static void perf_counter_interrupt(struct pt_regs *regs)
1139{
1140 int i;
1141 struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
1142 struct perf_counter *counter;
1143 long val;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001144 int found = 0;
Paul Mackerrasca8f2d72009-04-09 14:42:56 +10001145 int nmi;
1146
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +10001147 if (cpuhw->n_limited)
1148 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
1149 mfspr(SPRN_PMC6));
1150
Paul Mackerrasca8f2d72009-04-09 14:42:56 +10001151 /*
Paul Mackerras0bbd0d42009-05-14 13:31:48 +10001152 * Overload regs->dsisr to store MMCRA so we only need to read it once.
1153 */
1154 regs->dsisr = mfspr(SPRN_MMCRA);
1155
1156 /*
Paul Mackerrasca8f2d72009-04-09 14:42:56 +10001157 * If interrupts were soft-disabled when this PMU interrupt
1158 * occurred, treat it as an NMI.
1159 */
1160 nmi = !regs->softe;
1161 if (nmi)
1162 nmi_enter();
1163 else
1164 irq_enter();
Paul Mackerras45749102009-01-09 20:21:55 +11001165
1166 for (i = 0; i < cpuhw->n_counters; ++i) {
1167 counter = cpuhw->counter[i];
Paul Mackerrasdcd945e2009-06-03 19:40:36 +10001168 if (!counter->hw.idx || is_limited_pmc(counter->hw.idx))
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +10001169 continue;
Paul Mackerras45749102009-01-09 20:21:55 +11001170 val = read_pmc(counter->hw.idx);
1171 if ((int)val < 0) {
1172 /* counter has overflowed */
1173 found = 1;
Paul Mackerrasca8f2d72009-04-09 14:42:56 +10001174 record_and_restart(counter, val, regs, nmi);
Paul Mackerras45749102009-01-09 20:21:55 +11001175 }
1176 }
1177
1178 /*
1179 * In case we didn't find and reset the counter that caused
1180 * the interrupt, scan all counters and reset any that are
1181 * negative, to avoid getting continual interrupts.
1182 * Any that we processed in the previous loop will not be negative.
1183 */
1184 if (!found) {
1185 for (i = 0; i < ppmu->n_counter; ++i) {
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +10001186 if (is_limited_pmc(i + 1))
1187 continue;
Paul Mackerras45749102009-01-09 20:21:55 +11001188 val = read_pmc(i + 1);
1189 if ((int)val < 0)
1190 write_pmc(i + 1, 0);
1191 }
1192 }
1193
1194 /*
1195 * Reset MMCR0 to its normal value. This will set PMXE and
1196 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
1197 * and thus allow interrupts to occur again.
1198 * XXX might want to use MSR.PM to keep the counters frozen until
1199 * we get back out of this interrupt.
1200 */
Paul Mackerrasab7ef2e2009-04-29 22:38:51 +10001201 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
Paul Mackerras45749102009-01-09 20:21:55 +11001202
Paul Mackerrasca8f2d72009-04-09 14:42:56 +10001203 if (nmi)
1204 nmi_exit();
1205 else
Paul Mackerrasdb4fb5a2009-03-19 20:26:20 +01001206 irq_exit();
Paul Mackerras45749102009-01-09 20:21:55 +11001207}
1208
Paul Mackerras01d02872009-01-14 13:44:19 +11001209void hw_perf_counter_setup(int cpu)
1210{
1211 struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
1212
1213 memset(cpuhw, 0, sizeof(*cpuhw));
1214 cpuhw->mmcr[0] = MMCR0_FC;
1215}
1216
Paul Mackerras079b3c52009-06-17 21:52:09 +10001217int register_power_pmu(struct power_pmu *pmu)
Paul Mackerras45749102009-01-09 20:21:55 +11001218{
Paul Mackerras079b3c52009-06-17 21:52:09 +10001219 if (ppmu)
1220 return -EBUSY; /* something's already registered */
Paul Mackerras16b06792009-01-10 16:34:07 +11001221
Paul Mackerras079b3c52009-06-17 21:52:09 +10001222 ppmu = pmu;
1223 pr_info("%s performance monitor hardware support registered\n",
1224 pmu->name);
Paul Mackerrasd095cd42009-02-23 23:01:28 +11001225
1226 /*
1227 * Use FCHV to ignore kernel events if MSR.HV is set.
1228 */
1229 if (mfmsr() & MSR_HV)
1230 freeze_counters_kernel = MMCR0_FCHV;
1231
Paul Mackerras45749102009-01-09 20:21:55 +11001232 return 0;
1233}