blob: b2f873bc891bfe7ab6aac3be3e81a403756f244d [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
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>
Michael Neuling69123182013-05-13 18:44:58 +000016#include <linux/uaccess.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <asm/reg.h>
18#include <asm/pmc.h>
19#include <asm/machdep.h>
20#include <asm/firmware.h>
21#include <asm/ptrace.h>
Michael Neuling69123182013-05-13 18:44:58 +000022#include <asm/code-patching.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020023
Anshuman Khandual3925f462013-04-22 19:42:44 +000024#define BHRB_MAX_ENTRIES 32
25#define BHRB_TARGET 0x0000000000000002
26#define BHRB_PREDICTION 0x0000000000000001
27#define BHRB_EA 0xFFFFFFFFFFFFFFFC
28
Ingo Molnarcdd6c482009-09-21 12:02:48 +020029struct cpu_hw_events {
30 int n_events;
31 int n_percpu;
32 int disabled;
33 int n_added;
34 int n_limited;
35 u8 pmcs_enabled;
36 struct perf_event *event[MAX_HWEVENTS];
37 u64 events[MAX_HWEVENTS];
38 unsigned int flags[MAX_HWEVENTS];
39 unsigned long mmcr[3];
Paul Mackerrasa8f90e92009-09-22 09:48:08 +100040 struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
41 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042 u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
43 unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
44 unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
Lin Ming8e6d5572010-05-08 20:28:41 +100045
46 unsigned int group_flag;
47 int n_txn_start;
Anshuman Khandual3925f462013-04-22 19:42:44 +000048
49 /* BHRB bits */
50 u64 bhrb_filter; /* BHRB HW branch filter */
51 int bhrb_users;
52 void *bhrb_context;
53 struct perf_branch_stack bhrb_stack;
54 struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
Ingo Molnarcdd6c482009-09-21 12:02:48 +020055};
Anshuman Khandual3925f462013-04-22 19:42:44 +000056
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
58
59struct power_pmu *ppmu;
60
61/*
Ingo Molnar57c0c152009-09-21 12:20:38 +020062 * Normally, to ignore kernel events we set the FCS (freeze counters
Ingo Molnarcdd6c482009-09-21 12:02:48 +020063 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
64 * hypervisor bit set in the MSR, or if we are running on a processor
65 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
66 * then we need to use the FCHV bit to ignore kernel events.
67 */
68static unsigned int freeze_events_kernel = MMCR0_FCS;
69
70/*
71 * 32-bit doesn't have MMCRA but does have an MMCR2,
72 * and a few other names are different.
73 */
74#ifdef CONFIG_PPC32
75
76#define MMCR0_FCHV 0
77#define MMCR0_PMCjCE MMCR0_PMCnCE
78
79#define SPRN_MMCRA SPRN_MMCR2
80#define MMCRA_SAMPLE_ENABLE 0
81
82static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
83{
84 return 0;
85}
86static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
87static inline u32 perf_get_misc_flags(struct pt_regs *regs)
88{
89 return 0;
90}
Anton Blanchard75382aa2012-06-26 01:01:36 +000091static inline void perf_read_regs(struct pt_regs *regs)
92{
93 regs->result = 0;
94}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020095static inline int perf_intr_is_nmi(struct pt_regs *regs)
96{
97 return 0;
98}
99
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000100static inline int siar_valid(struct pt_regs *regs)
101{
102 return 1;
103}
104
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000105static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
106static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
107void power_pmu_flush_branch_stack(void) {}
108static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200109#endif /* CONFIG_PPC32 */
110
Michael Ellerman33904052013-04-25 19:28:25 +0000111static bool regs_use_siar(struct pt_regs *regs)
112{
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000113 return !!regs->result;
Michael Ellerman33904052013-04-25 19:28:25 +0000114}
115
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200116/*
117 * Things that are specific to 64-bit implementations.
118 */
119#ifdef CONFIG_PPC64
120
121static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
122{
123 unsigned long mmcra = regs->dsisr;
124
Michael Ellerman7a786832013-04-25 19:28:23 +0000125 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200126 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
127 if (slot > 1)
128 return 4 * (slot - 1);
129 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000130
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200131 return 0;
132}
133
134/*
135 * The user wants a data address recorded.
136 * If we're not doing instruction sampling, give them the SDAR
137 * (sampled data address). If we are doing instruction sampling, then
138 * only give them the SDAR if it corresponds to the instruction
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000139 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC or
140 * the [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141 */
142static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
143{
144 unsigned long mmcra = regs->dsisr;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000145 unsigned long sdsync;
146
147 if (ppmu->flags & PPMU_SIAR_VALID)
148 sdsync = POWER7P_MMCRA_SDAR_VALID;
149 else if (ppmu->flags & PPMU_ALT_SIPR)
150 sdsync = POWER6_MMCRA_SDSYNC;
151 else
152 sdsync = MMCRA_SDSYNC;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200153
154 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
155 *addrp = mfspr(SPRN_SDAR);
156}
157
Michael Ellerman5682c462013-04-25 19:28:24 +0000158static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000159{
160 unsigned long sihv = MMCRA_SIHV;
161
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000162 if (ppmu->flags & PPMU_HAS_SIER)
163 return !!(regs->dar & SIER_SIHV);
164
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000165 if (ppmu->flags & PPMU_ALT_SIPR)
166 sihv = POWER6_MMCRA_SIHV;
167
Michael Ellerman5682c462013-04-25 19:28:24 +0000168 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000169}
170
Michael Ellerman5682c462013-04-25 19:28:24 +0000171static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000172{
173 unsigned long sipr = MMCRA_SIPR;
174
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000175 if (ppmu->flags & PPMU_HAS_SIER)
176 return !!(regs->dar & SIER_SIPR);
177
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000178 if (ppmu->flags & PPMU_ALT_SIPR)
179 sipr = POWER6_MMCRA_SIPR;
180
Michael Ellerman5682c462013-04-25 19:28:24 +0000181 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000182}
183
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000184static inline u32 perf_flags_from_msr(struct pt_regs *regs)
185{
186 if (regs->msr & MSR_PR)
187 return PERF_RECORD_MISC_USER;
188 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
189 return PERF_RECORD_MISC_HYPERVISOR;
190 return PERF_RECORD_MISC_KERNEL;
191}
192
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200193static inline u32 perf_get_misc_flags(struct pt_regs *regs)
194{
Michael Ellerman33904052013-04-25 19:28:25 +0000195 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200196
Anton Blanchard75382aa2012-06-26 01:01:36 +0000197 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000198 return perf_flags_from_msr(regs);
199
200 /*
201 * If we don't have flags in MMCRA, rather than using
202 * the MSR, we intuit the flags from the address in
203 * SIAR which should give slightly more reliable
204 * results
205 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000206 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000207 unsigned long siar = mfspr(SPRN_SIAR);
208 if (siar >= PAGE_OFFSET)
209 return PERF_RECORD_MISC_KERNEL;
210 return PERF_RECORD_MISC_USER;
211 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200212
Michael Neuling7abb8402009-10-14 19:32:15 +0000213 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000214 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000215 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000216
217 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200218 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000219
Michael Neuling7abb8402009-10-14 19:32:15 +0000220 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200221}
222
223/*
224 * Overload regs->dsisr to store MMCRA so we only need to read it once
225 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000226 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000227 * Overload regs->result to specify whether we should use the MSR (result
228 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200229 */
230static inline void perf_read_regs(struct pt_regs *regs)
231{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000232 unsigned long mmcra = mfspr(SPRN_MMCRA);
233 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
234 int use_siar;
235
Michael Ellerman5682c462013-04-25 19:28:24 +0000236 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000237
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000238 if (ppmu->flags & PPMU_HAS_SIER)
239 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000240
241 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000242 * If this isn't a PMU exception (eg a software event) the SIAR is
243 * not valid. Use pt_regs.
244 *
245 * If it is a marked event use the SIAR.
246 *
247 * If the PMU doesn't update the SIAR for non marked events use
248 * pt_regs.
249 *
250 * If the PMU has HV/PR flags then check to see if they
251 * place the exception in userspace. If so, use pt_regs. In
252 * continuous sampling mode the SIAR and the PMU exception are
253 * not synchronised, so they may be many instructions apart.
254 * This can result in confusing backtraces. We still want
255 * hypervisor samples as well as samples in the kernel with
256 * interrupts off hence the userspace check.
257 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000258 if (TRAP(regs) != 0xf00)
259 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000260 else if (marked)
261 use_siar = 1;
262 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
263 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000264 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000265 use_siar = 0;
266 else
267 use_siar = 1;
268
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000269 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200270}
271
272/*
273 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
274 * it as an NMI.
275 */
276static inline int perf_intr_is_nmi(struct pt_regs *regs)
277{
278 return !regs->softe;
279}
280
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000281/*
282 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
283 * must be sampled only if the SIAR-valid bit is set.
284 *
285 * For unmarked instructions and for processors that don't have the SIAR-Valid
286 * bit, assume that SIAR is valid.
287 */
288static inline int siar_valid(struct pt_regs *regs)
289{
290 unsigned long mmcra = regs->dsisr;
291 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
292
293 if ((ppmu->flags & PPMU_SIAR_VALID) && marked)
294 return mmcra & POWER7P_MMCRA_SIAR_VALID;
295
296 return 1;
297}
298
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000299
300/* Reset all possible BHRB entries */
301static void power_pmu_bhrb_reset(void)
302{
303 asm volatile(PPC_CLRBHRB);
304}
305
306static void power_pmu_bhrb_enable(struct perf_event *event)
307{
308 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
309
310 if (!ppmu->bhrb_nr)
311 return;
312
313 /* Clear BHRB if we changed task context to avoid data leaks */
314 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
315 power_pmu_bhrb_reset();
316 cpuhw->bhrb_context = event->ctx;
317 }
318 cpuhw->bhrb_users++;
319}
320
321static void power_pmu_bhrb_disable(struct perf_event *event)
322{
323 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
324
325 if (!ppmu->bhrb_nr)
326 return;
327
328 cpuhw->bhrb_users--;
329 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
330
331 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
332 /* BHRB cannot be turned off when other
333 * events are active on the PMU.
334 */
335
336 /* avoid stale pointer */
337 cpuhw->bhrb_context = NULL;
338 }
339}
340
341/* Called from ctxsw to prevent one process's branch entries to
342 * mingle with the other process's entries during context switch.
343 */
344void power_pmu_flush_branch_stack(void)
345{
346 if (ppmu->bhrb_nr)
347 power_pmu_bhrb_reset();
348}
Michael Neuling69123182013-05-13 18:44:58 +0000349/* Calculate the to address for a branch */
350static __u64 power_pmu_bhrb_to(u64 addr)
351{
352 unsigned int instr;
353 int ret;
354 __u64 target;
355
356 if (is_kernel_addr(addr))
357 return branch_target((unsigned int *)addr);
358
359 /* Userspace: need copy instruction here then translate it */
360 pagefault_disable();
361 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
362 if (ret) {
363 pagefault_enable();
364 return 0;
365 }
366 pagefault_enable();
367
368 target = branch_target(&instr);
369 if ((!target) || (instr & BRANCH_ABSOLUTE))
370 return target;
371
372 /* Translate relative branch target from kernel to user address */
373 return target - (unsigned long)&instr + addr;
374}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000375
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000376/* Processing BHRB entries */
Michael Neuling506e70d2013-05-13 18:44:57 +0000377void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000378{
379 u64 val;
380 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000381 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000382
383 r_index = 0;
384 u_index = 0;
385 while (r_index < ppmu->bhrb_nr) {
386 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000387 val = read_bhrb(r_index++);
388 if (!val)
389 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000390 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000391 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000392 addr = val & BHRB_EA;
393 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000394
Michael Neuling506e70d2013-05-13 18:44:57 +0000395 if (!addr)
396 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000397 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000398
Michael Neuling506e70d2013-05-13 18:44:57 +0000399 /* Branches are read most recent first (ie. mfbhrb 0 is
400 * the most recent branch).
401 * There are two types of valid entries:
402 * 1) a target entry which is the to address of a
403 * computed goto like a blr,bctr,btar. The next
404 * entry read from the bhrb will be branch
405 * corresponding to this target (ie. the actual
406 * blr/bctr/btar instruction).
407 * 2) a from address which is an actual branch. If a
408 * target entry proceeds this, then this is the
409 * matching branch for that target. If this is not
410 * following a target entry, then this is a branch
411 * where the target is given as an immediate field
412 * in the instruction (ie. an i or b form branch).
413 * In this case we need to read the instruction from
414 * memory to determine the target/to address.
415 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000416
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000417 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000418 /* Target branches use two entries
419 * (ie. computed gotos/XL form)
420 */
421 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000422 cpuhw->bhrb_entries[u_index].mispred = pred;
423 cpuhw->bhrb_entries[u_index].predicted = ~pred;
424
Michael Neuling506e70d2013-05-13 18:44:57 +0000425 /* Get from address in next entry */
426 val = read_bhrb(r_index++);
427 addr = val & BHRB_EA;
428 if (val & BHRB_TARGET) {
429 /* Shouldn't have two targets in a
430 row.. Reset index and try again */
431 r_index--;
432 addr = 0;
433 }
434 cpuhw->bhrb_entries[u_index].from = addr;
435 } else {
436 /* Branches to immediate field
437 (ie I or B form) */
438 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000439 cpuhw->bhrb_entries[u_index].to =
440 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000441 cpuhw->bhrb_entries[u_index].mispred = pred;
442 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000443 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000444 u_index++;
445
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000446 }
447 }
448 cpuhw->bhrb_stack.nr = u_index;
449 return;
450}
451
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200452#endif /* CONFIG_PPC64 */
453
454static void perf_event_interrupt(struct pt_regs *regs);
455
456void perf_event_print_debug(void)
457{
458}
459
460/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200461 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200462 */
463static unsigned long read_pmc(int idx)
464{
465 unsigned long val;
466
467 switch (idx) {
468 case 1:
469 val = mfspr(SPRN_PMC1);
470 break;
471 case 2:
472 val = mfspr(SPRN_PMC2);
473 break;
474 case 3:
475 val = mfspr(SPRN_PMC3);
476 break;
477 case 4:
478 val = mfspr(SPRN_PMC4);
479 break;
480 case 5:
481 val = mfspr(SPRN_PMC5);
482 break;
483 case 6:
484 val = mfspr(SPRN_PMC6);
485 break;
486#ifdef CONFIG_PPC64
487 case 7:
488 val = mfspr(SPRN_PMC7);
489 break;
490 case 8:
491 val = mfspr(SPRN_PMC8);
492 break;
493#endif /* CONFIG_PPC64 */
494 default:
495 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
496 val = 0;
497 }
498 return val;
499}
500
501/*
502 * Write one PMC.
503 */
504static void write_pmc(int idx, unsigned long val)
505{
506 switch (idx) {
507 case 1:
508 mtspr(SPRN_PMC1, val);
509 break;
510 case 2:
511 mtspr(SPRN_PMC2, val);
512 break;
513 case 3:
514 mtspr(SPRN_PMC3, val);
515 break;
516 case 4:
517 mtspr(SPRN_PMC4, val);
518 break;
519 case 5:
520 mtspr(SPRN_PMC5, val);
521 break;
522 case 6:
523 mtspr(SPRN_PMC6, val);
524 break;
525#ifdef CONFIG_PPC64
526 case 7:
527 mtspr(SPRN_PMC7, val);
528 break;
529 case 8:
530 mtspr(SPRN_PMC8, val);
531 break;
532#endif /* CONFIG_PPC64 */
533 default:
534 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
535 }
536}
537
538/*
539 * Check if a set of events can all go on the PMU at once.
540 * If they can't, this will look at alternative codes for the events
541 * and see if any combination of alternative codes is feasible.
542 * The feasible set is returned in event_id[].
543 */
544static int power_check_constraints(struct cpu_hw_events *cpuhw,
545 u64 event_id[], unsigned int cflags[],
546 int n_ev)
547{
548 unsigned long mask, value, nv;
549 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
550 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
551 int i, j;
552 unsigned long addf = ppmu->add_fields;
553 unsigned long tadd = ppmu->test_adder;
554
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000555 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556 return -1;
557
558 /* First see if the events will go on as-is */
559 for (i = 0; i < n_ev; ++i) {
560 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
561 && !ppmu->limited_pmc_event(event_id[i])) {
562 ppmu->get_alternatives(event_id[i], cflags[i],
563 cpuhw->alternatives[i]);
564 event_id[i] = cpuhw->alternatives[i][0];
565 }
566 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
567 &cpuhw->avalues[i][0]))
568 return -1;
569 }
570 value = mask = 0;
571 for (i = 0; i < n_ev; ++i) {
572 nv = (value | cpuhw->avalues[i][0]) +
573 (value & cpuhw->avalues[i][0] & addf);
574 if ((((nv + tadd) ^ value) & mask) != 0 ||
575 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
576 cpuhw->amasks[i][0]) != 0)
577 break;
578 value = nv;
579 mask |= cpuhw->amasks[i][0];
580 }
581 if (i == n_ev)
582 return 0; /* all OK */
583
584 /* doesn't work, gather alternatives... */
585 if (!ppmu->get_alternatives)
586 return -1;
587 for (i = 0; i < n_ev; ++i) {
588 choice[i] = 0;
589 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
590 cpuhw->alternatives[i]);
591 for (j = 1; j < n_alt[i]; ++j)
592 ppmu->get_constraint(cpuhw->alternatives[i][j],
593 &cpuhw->amasks[i][j],
594 &cpuhw->avalues[i][j]);
595 }
596
597 /* enumerate all possibilities and see if any will work */
598 i = 0;
599 j = -1;
600 value = mask = nv = 0;
601 while (i < n_ev) {
602 if (j >= 0) {
603 /* we're backtracking, restore context */
604 value = svalues[i];
605 mask = smasks[i];
606 j = choice[i];
607 }
608 /*
609 * See if any alternative k for event_id i,
610 * where k > j, will satisfy the constraints.
611 */
612 while (++j < n_alt[i]) {
613 nv = (value | cpuhw->avalues[i][j]) +
614 (value & cpuhw->avalues[i][j] & addf);
615 if ((((nv + tadd) ^ value) & mask) == 0 &&
616 (((nv + tadd) ^ cpuhw->avalues[i][j])
617 & cpuhw->amasks[i][j]) == 0)
618 break;
619 }
620 if (j >= n_alt[i]) {
621 /*
622 * No feasible alternative, backtrack
623 * to event_id i-1 and continue enumerating its
624 * alternatives from where we got up to.
625 */
626 if (--i < 0)
627 return -1;
628 } else {
629 /*
630 * Found a feasible alternative for event_id i,
631 * remember where we got up to with this event_id,
632 * go on to the next event_id, and start with
633 * the first alternative for it.
634 */
635 choice[i] = j;
636 svalues[i] = value;
637 smasks[i] = mask;
638 value = nv;
639 mask |= cpuhw->amasks[i][j];
640 ++i;
641 j = -1;
642 }
643 }
644
645 /* OK, we have a feasible combination, tell the caller the solution */
646 for (i = 0; i < n_ev; ++i)
647 event_id[i] = cpuhw->alternatives[i][choice[i]];
648 return 0;
649}
650
651/*
652 * Check if newly-added events have consistent settings for
653 * exclude_{user,kernel,hv} with each other and any previously
654 * added events.
655 */
656static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
657 int n_prev, int n_new)
658{
659 int eu = 0, ek = 0, eh = 0;
660 int i, n, first;
661 struct perf_event *event;
662
663 n = n_prev + n_new;
664 if (n <= 1)
665 return 0;
666
667 first = 1;
668 for (i = 0; i < n; ++i) {
669 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
670 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
671 continue;
672 }
673 event = ctrs[i];
674 if (first) {
675 eu = event->attr.exclude_user;
676 ek = event->attr.exclude_kernel;
677 eh = event->attr.exclude_hv;
678 first = 0;
679 } else if (event->attr.exclude_user != eu ||
680 event->attr.exclude_kernel != ek ||
681 event->attr.exclude_hv != eh) {
682 return -EAGAIN;
683 }
684 }
685
686 if (eu || ek || eh)
687 for (i = 0; i < n; ++i)
688 if (cflags[i] & PPMU_LIMITED_PMC_OK)
689 cflags[i] |= PPMU_LIMITED_PMC_REQD;
690
691 return 0;
692}
693
Eric B Munson86c74ab2011-04-15 08:12:30 +0000694static u64 check_and_compute_delta(u64 prev, u64 val)
695{
696 u64 delta = (val - prev) & 0xfffffffful;
697
698 /*
699 * POWER7 can roll back counter values, if the new value is smaller
700 * than the previous value it will cause the delta and the counter to
701 * have bogus values unless we rolled a counter over. If a coutner is
702 * rolled back, it will be smaller, but within 256, which is the maximum
703 * number of events to rollback at once. If we dectect a rollback
704 * return 0. This can lead to a small lack of precision in the
705 * counters.
706 */
707 if (prev > val && (prev - val) < 256)
708 delta = 0;
709
710 return delta;
711}
712
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200713static void power_pmu_read(struct perf_event *event)
714{
715 s64 val, delta, prev;
716
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200717 if (event->hw.state & PERF_HES_STOPPED)
718 return;
719
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200720 if (!event->hw.idx)
721 return;
722 /*
723 * Performance monitor interrupts come even when interrupts
724 * are soft-disabled, as long as interrupts are hard-enabled.
725 * Therefore we treat them like NMIs.
726 */
727 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +0200728 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200729 barrier();
730 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +0000731 delta = check_and_compute_delta(prev, val);
732 if (!delta)
733 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200734 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200735
Peter Zijlstrae7850592010-05-21 14:43:08 +0200736 local64_add(delta, &event->count);
737 local64_sub(delta, &event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200738}
739
740/*
741 * On some machines, PMC5 and PMC6 can't be written, don't respect
742 * the freeze conditions, and don't generate interrupts. This tells
743 * us if `event' is using such a PMC.
744 */
745static int is_limited_pmc(int pmcnum)
746{
747 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
748 && (pmcnum == 5 || pmcnum == 6);
749}
750
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000751static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200752 unsigned long pmc5, unsigned long pmc6)
753{
754 struct perf_event *event;
755 u64 val, prev, delta;
756 int i;
757
758 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000759 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200760 if (!event->hw.idx)
761 continue;
762 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200763 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200764 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000765 delta = check_and_compute_delta(prev, val);
766 if (delta)
767 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200768 }
769}
770
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000771static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200772 unsigned long pmc5, unsigned long pmc6)
773{
774 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000775 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200776 int i;
777
778 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000779 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200780 event->hw.idx = cpuhw->limited_hwidx[i];
781 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000782 prev = local64_read(&event->hw.prev_count);
783 if (check_and_compute_delta(prev, val))
784 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200785 perf_event_update_userpage(event);
786 }
787}
788
789/*
790 * Since limited events don't respect the freeze conditions, we
791 * have to read them immediately after freezing or unfreezing the
792 * other events. We try to keep the values from the limited
793 * events as consistent as possible by keeping the delay (in
794 * cycles and instructions) between freezing/unfreezing and reading
795 * the limited events as small and consistent as possible.
796 * Therefore, if any limited events are in use, we read them
797 * both, and always in the same order, to minimize variability,
798 * and do it inside the same asm that writes MMCR0.
799 */
800static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
801{
802 unsigned long pmc5, pmc6;
803
804 if (!cpuhw->n_limited) {
805 mtspr(SPRN_MMCR0, mmcr0);
806 return;
807 }
808
809 /*
810 * Write MMCR0, then read PMC5 and PMC6 immediately.
811 * To ensure we don't get a performance monitor interrupt
812 * between writing MMCR0 and freezing/thawing the limited
813 * events, we first write MMCR0 with the event overflow
814 * interrupt enable bits turned off.
815 */
816 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
817 : "=&r" (pmc5), "=&r" (pmc6)
818 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
819 "i" (SPRN_MMCR0),
820 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
821
822 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000823 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200824 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000825 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200826
827 /*
828 * Write the full MMCR0 including the event overflow interrupt
829 * enable bits, if necessary.
830 */
831 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
832 mtspr(SPRN_MMCR0, mmcr0);
833}
834
835/*
836 * Disable all events to prevent PMU interrupts and to allow
837 * events to be added or removed.
838 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200839static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200840{
841 struct cpu_hw_events *cpuhw;
842 unsigned long flags;
843
844 if (!ppmu)
845 return;
846 local_irq_save(flags);
847 cpuhw = &__get_cpu_var(cpu_hw_events);
848
849 if (!cpuhw->disabled) {
850 cpuhw->disabled = 1;
851 cpuhw->n_added = 0;
852
853 /*
854 * Check if we ever enabled the PMU on this cpu.
855 */
856 if (!cpuhw->pmcs_enabled) {
857 ppc_enable_pmcs();
858 cpuhw->pmcs_enabled = 1;
859 }
860
861 /*
862 * Disable instruction sampling if it was enabled
863 */
864 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
865 mtspr(SPRN_MMCRA,
866 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
867 mb();
868 }
869
870 /*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200871 * Set the 'freeze counters' bit.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200872 * The barrier is to make sure the mtspr has been
873 * executed and the PMU has frozen the events
874 * before we return.
875 */
876 write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
877 mb();
878 }
879 local_irq_restore(flags);
880}
881
882/*
883 * Re-enable all events if disable == 0.
884 * If we were previously disabled and events were added, then
885 * put the new config on the PMU.
886 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200887static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200888{
889 struct perf_event *event;
890 struct cpu_hw_events *cpuhw;
891 unsigned long flags;
892 long i;
893 unsigned long val;
894 s64 left;
895 unsigned int hwc_index[MAX_HWEVENTS];
896 int n_lim;
897 int idx;
898
899 if (!ppmu)
900 return;
901 local_irq_save(flags);
902 cpuhw = &__get_cpu_var(cpu_hw_events);
903 if (!cpuhw->disabled) {
904 local_irq_restore(flags);
905 return;
906 }
907 cpuhw->disabled = 0;
908
909 /*
910 * If we didn't change anything, or only removed events,
911 * no need to recalculate MMCR* settings and reset the PMCs.
912 * Just reenable the PMU with the current MMCR* settings
913 * (possibly updated for removal of events).
914 */
915 if (!cpuhw->n_added) {
916 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
917 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
918 if (cpuhw->n_events == 0)
919 ppc_set_pmu_inuse(0);
920 goto out_enable;
921 }
922
923 /*
924 * Compute MMCR* values for the new set of events
925 */
926 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
927 cpuhw->mmcr)) {
928 /* shouldn't ever get here */
929 printk(KERN_ERR "oops compute_mmcr failed\n");
930 goto out;
931 }
932
933 /*
934 * Add in MMCR0 freeze bits corresponding to the
935 * attr.exclude_* bits for the first event.
936 * We have already checked that all events have the
937 * same values for these bits as the first event.
938 */
939 event = cpuhw->event[0];
940 if (event->attr.exclude_user)
941 cpuhw->mmcr[0] |= MMCR0_FCP;
942 if (event->attr.exclude_kernel)
943 cpuhw->mmcr[0] |= freeze_events_kernel;
944 if (event->attr.exclude_hv)
945 cpuhw->mmcr[0] |= MMCR0_FCHV;
946
947 /*
948 * Write the new configuration to MMCR* with the freeze
949 * bit set and set the hardware events to their initial values.
950 * Then unfreeze the events.
951 */
952 ppc_set_pmu_inuse(1);
953 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
954 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
955 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
956 | MMCR0_FC);
957
958 /*
959 * Read off any pre-existing events that need to move
960 * to another PMC.
961 */
962 for (i = 0; i < cpuhw->n_events; ++i) {
963 event = cpuhw->event[i];
964 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
965 power_pmu_read(event);
966 write_pmc(event->hw.idx, 0);
967 event->hw.idx = 0;
968 }
969 }
970
971 /*
972 * Initialize the PMCs for all the new and moved events.
973 */
974 cpuhw->n_limited = n_lim = 0;
975 for (i = 0; i < cpuhw->n_events; ++i) {
976 event = cpuhw->event[i];
977 if (event->hw.idx)
978 continue;
979 idx = hwc_index[i] + 1;
980 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000981 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200982 cpuhw->limited_hwidx[n_lim] = idx;
983 ++n_lim;
984 continue;
985 }
986 val = 0;
987 if (event->hw.sample_period) {
Peter Zijlstrae7850592010-05-21 14:43:08 +0200988 left = local64_read(&event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200989 if (left < 0x80000000L)
990 val = 0x80000000L - left;
991 }
Peter Zijlstrae7850592010-05-21 14:43:08 +0200992 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200993 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200994 if (event->hw.state & PERF_HES_STOPPED)
995 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200996 write_pmc(idx, val);
997 perf_event_update_userpage(event);
998 }
999 cpuhw->n_limited = n_lim;
1000 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1001
1002 out_enable:
1003 mb();
1004 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1005
1006 /*
1007 * Enable instruction sampling if necessary
1008 */
1009 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1010 mb();
1011 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1012 }
1013
1014 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001015 if (cpuhw->bhrb_users)
1016 ppmu->config_bhrb(cpuhw->bhrb_filter);
1017
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 local_irq_restore(flags);
1019}
1020
1021static int collect_events(struct perf_event *group, int max_count,
1022 struct perf_event *ctrs[], u64 *events,
1023 unsigned int *flags)
1024{
1025 int n = 0;
1026 struct perf_event *event;
1027
1028 if (!is_software_event(group)) {
1029 if (n >= max_count)
1030 return -1;
1031 ctrs[n] = group;
1032 flags[n] = group->hw.event_base;
1033 events[n++] = group->hw.config;
1034 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001035 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001036 if (!is_software_event(event) &&
1037 event->state != PERF_EVENT_STATE_OFF) {
1038 if (n >= max_count)
1039 return -1;
1040 ctrs[n] = event;
1041 flags[n] = event->hw.event_base;
1042 events[n++] = event->hw.config;
1043 }
1044 }
1045 return n;
1046}
1047
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048/*
1049 * Add a event to the PMU.
1050 * If all events are not already frozen, then we disable and
1051 * re-enable the PMU in order to get hw_perf_enable to do the
1052 * actual work of reconfiguring the PMU.
1053 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001054static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001055{
1056 struct cpu_hw_events *cpuhw;
1057 unsigned long flags;
1058 int n0;
1059 int ret = -EAGAIN;
1060
1061 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001062 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001063
1064 /*
1065 * Add the event to the list (if there is room)
1066 * and check whether the total set is still feasible.
1067 */
1068 cpuhw = &__get_cpu_var(cpu_hw_events);
1069 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001070 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001071 goto out;
1072 cpuhw->event[n0] = event;
1073 cpuhw->events[n0] = event->hw.config;
1074 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001075
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001076 /*
1077 * This event may have been disabled/stopped in record_and_restart()
1078 * because we exceeded the ->event_limit. If re-starting the event,
1079 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1080 * notification is re-enabled.
1081 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001082 if (!(ef_flags & PERF_EF_START))
1083 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001084 else
1085 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001086
Lin Ming8e6d5572010-05-08 20:28:41 +10001087 /*
1088 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001089 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001090 * at commit time(->commit_txn) as a whole
1091 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001092 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001093 goto nocheck;
1094
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1096 goto out;
1097 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1098 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001100
1101nocheck:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001102 ++cpuhw->n_events;
1103 ++cpuhw->n_added;
1104
1105 ret = 0;
1106 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001107 if (has_branch_stack(event))
1108 power_pmu_bhrb_enable(event);
1109
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001110 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001111 local_irq_restore(flags);
1112 return ret;
1113}
1114
1115/*
1116 * Remove a event from the PMU.
1117 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001118static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119{
1120 struct cpu_hw_events *cpuhw;
1121 long i;
1122 unsigned long flags;
1123
1124 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001125 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126
1127 power_pmu_read(event);
1128
1129 cpuhw = &__get_cpu_var(cpu_hw_events);
1130 for (i = 0; i < cpuhw->n_events; ++i) {
1131 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001132 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001133 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001134 cpuhw->events[i-1] = cpuhw->events[i];
1135 cpuhw->flags[i-1] = cpuhw->flags[i];
1136 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137 --cpuhw->n_events;
1138 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1139 if (event->hw.idx) {
1140 write_pmc(event->hw.idx, 0);
1141 event->hw.idx = 0;
1142 }
1143 perf_event_update_userpage(event);
1144 break;
1145 }
1146 }
1147 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001148 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 break;
1150 if (i < cpuhw->n_limited) {
1151 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001152 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001153 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1154 }
1155 --cpuhw->n_limited;
1156 }
1157 if (cpuhw->n_events == 0) {
1158 /* disable exceptions if no events are running */
1159 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1160 }
1161
Anshuman Khandual3925f462013-04-22 19:42:44 +00001162 if (has_branch_stack(event))
1163 power_pmu_bhrb_disable(event);
1164
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001165 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166 local_irq_restore(flags);
1167}
1168
1169/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001170 * POWER-PMU does not support disabling individual counters, hence
1171 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001172 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001173
1174static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001176 unsigned long flags;
1177 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001178 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001179
1180 if (!event->hw.idx || !event->hw.sample_period)
1181 return;
1182
1183 if (!(event->hw.state & PERF_HES_STOPPED))
1184 return;
1185
1186 if (ef_flags & PERF_EF_RELOAD)
1187 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1188
1189 local_irq_save(flags);
1190 perf_pmu_disable(event->pmu);
1191
1192 event->hw.state = 0;
1193 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001194
1195 val = 0;
1196 if (left < 0x80000000L)
1197 val = 0x80000000L - left;
1198
1199 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001200
1201 perf_event_update_userpage(event);
1202 perf_pmu_enable(event->pmu);
1203 local_irq_restore(flags);
1204}
1205
1206static void power_pmu_stop(struct perf_event *event, int ef_flags)
1207{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208 unsigned long flags;
1209
1210 if (!event->hw.idx || !event->hw.sample_period)
1211 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001212
1213 if (event->hw.state & PERF_HES_STOPPED)
1214 return;
1215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001217 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001220 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1221 write_pmc(event->hw.idx, 0);
1222
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001223 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001224 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 local_irq_restore(flags);
1226}
1227
Lin Ming8e6d5572010-05-08 20:28:41 +10001228/*
1229 * Start group events scheduling transaction
1230 * Set the flag to make pmu::enable() not perform the
1231 * schedulability test, it will be performed at commit time
1232 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001233void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001234{
1235 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1236
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001237 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001238 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001239 cpuhw->n_txn_start = cpuhw->n_events;
1240}
1241
1242/*
1243 * Stop group events scheduling transaction
1244 * Clear the flag and pmu::enable() will perform the
1245 * schedulability test.
1246 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001247void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001248{
1249 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1250
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001251 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001252 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001253}
1254
1255/*
1256 * Commit group events scheduling transaction
1257 * Perform the group schedulability test as a whole
1258 * Return 0 if success
1259 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001260int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001261{
1262 struct cpu_hw_events *cpuhw;
1263 long i, n;
1264
1265 if (!ppmu)
1266 return -EAGAIN;
1267 cpuhw = &__get_cpu_var(cpu_hw_events);
1268 n = cpuhw->n_events;
1269 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1270 return -EAGAIN;
1271 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1272 if (i < 0)
1273 return -EAGAIN;
1274
1275 for (i = cpuhw->n_txn_start; i < n; ++i)
1276 cpuhw->event[i]->hw.config = cpuhw->events[i];
1277
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001278 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001279 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001280 return 0;
1281}
1282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001283/*
1284 * Return 1 if we might be able to put event on a limited PMC,
1285 * or 0 if not.
1286 * A event can only go on a limited PMC if it counts something
1287 * that a limited PMC can count, doesn't require interrupts, and
1288 * doesn't exclude any processor mode.
1289 */
1290static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1291 unsigned int flags)
1292{
1293 int n;
1294 u64 alt[MAX_EVENT_ALTERNATIVES];
1295
1296 if (event->attr.exclude_user
1297 || event->attr.exclude_kernel
1298 || event->attr.exclude_hv
1299 || event->attr.sample_period)
1300 return 0;
1301
1302 if (ppmu->limited_pmc_event(ev))
1303 return 1;
1304
1305 /*
1306 * The requested event_id isn't on a limited PMC already;
1307 * see if any alternative code goes on a limited PMC.
1308 */
1309 if (!ppmu->get_alternatives)
1310 return 0;
1311
1312 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1313 n = ppmu->get_alternatives(ev, flags, alt);
1314
1315 return n > 0;
1316}
1317
1318/*
1319 * Find an alternative event_id that goes on a normal PMC, if possible,
1320 * and return the event_id code, or 0 if there is no such alternative.
1321 * (Note: event_id code 0 is "don't count" on all machines.)
1322 */
1323static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1324{
1325 u64 alt[MAX_EVENT_ALTERNATIVES];
1326 int n;
1327
1328 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1329 n = ppmu->get_alternatives(ev, flags, alt);
1330 if (!n)
1331 return 0;
1332 return alt[0];
1333}
1334
1335/* Number of perf_events counting hardware events */
1336static atomic_t num_events;
1337/* Used to avoid races in calling reserve/release_pmc_hardware */
1338static DEFINE_MUTEX(pmc_reserve_mutex);
1339
1340/*
1341 * Release the PMU if this is the last perf_event.
1342 */
1343static void hw_perf_event_destroy(struct perf_event *event)
1344{
1345 if (!atomic_add_unless(&num_events, -1, 1)) {
1346 mutex_lock(&pmc_reserve_mutex);
1347 if (atomic_dec_return(&num_events) == 0)
1348 release_pmc_hardware();
1349 mutex_unlock(&pmc_reserve_mutex);
1350 }
1351}
1352
1353/*
1354 * Translate a generic cache event_id config to a raw event_id code.
1355 */
1356static int hw_perf_cache_event(u64 config, u64 *eventp)
1357{
1358 unsigned long type, op, result;
1359 int ev;
1360
1361 if (!ppmu->cache_events)
1362 return -EINVAL;
1363
1364 /* unpack config */
1365 type = config & 0xff;
1366 op = (config >> 8) & 0xff;
1367 result = (config >> 16) & 0xff;
1368
1369 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1370 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1371 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1372 return -EINVAL;
1373
1374 ev = (*ppmu->cache_events)[type][op][result];
1375 if (ev == 0)
1376 return -EOPNOTSUPP;
1377 if (ev == -1)
1378 return -EINVAL;
1379 *eventp = ev;
1380 return 0;
1381}
1382
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001383static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001384{
1385 u64 ev;
1386 unsigned long flags;
1387 struct perf_event *ctrs[MAX_HWEVENTS];
1388 u64 events[MAX_HWEVENTS];
1389 unsigned int cflags[MAX_HWEVENTS];
1390 int n;
1391 int err;
1392 struct cpu_hw_events *cpuhw;
1393
1394 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001395 return -ENOENT;
1396
Anshuman Khandual3925f462013-04-22 19:42:44 +00001397 if (has_branch_stack(event)) {
1398 /* PMU has BHRB enabled */
1399 if (!(ppmu->flags & PPMU_BHRB))
1400 return -EOPNOTSUPP;
1401 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001403 switch (event->attr.type) {
1404 case PERF_TYPE_HARDWARE:
1405 ev = event->attr.config;
1406 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001407 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001408 ev = ppmu->generic_events[ev];
1409 break;
1410 case PERF_TYPE_HW_CACHE:
1411 err = hw_perf_cache_event(event->attr.config, &ev);
1412 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001413 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414 break;
1415 case PERF_TYPE_RAW:
1416 ev = event->attr.config;
1417 break;
1418 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001419 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001420 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001422 event->hw.config_base = ev;
1423 event->hw.idx = 0;
1424
1425 /*
1426 * If we are not running on a hypervisor, force the
1427 * exclude_hv bit to 0 so that we don't care what
1428 * the user set it to.
1429 */
1430 if (!firmware_has_feature(FW_FEATURE_LPAR))
1431 event->attr.exclude_hv = 0;
1432
1433 /*
1434 * If this is a per-task event, then we can use
1435 * PM_RUN_* events interchangeably with their non RUN_*
1436 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1437 * XXX we should check if the task is an idle task.
1438 */
1439 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001440 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001441 flags |= PPMU_ONLY_COUNT_RUN;
1442
1443 /*
1444 * If this machine has limited events, check whether this
1445 * event_id could go on a limited event.
1446 */
1447 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1448 if (can_go_on_limited_pmc(event, ev, flags)) {
1449 flags |= PPMU_LIMITED_PMC_OK;
1450 } else if (ppmu->limited_pmc_event(ev)) {
1451 /*
1452 * The requested event_id is on a limited PMC,
1453 * but we can't use a limited PMC; see if any
1454 * alternative goes on a normal PMC.
1455 */
1456 ev = normal_pmc_alternative(ev, flags);
1457 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001458 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459 }
1460 }
1461
1462 /*
1463 * If this is in a group, check if it can go on with all the
1464 * other hardware events in the group. We assume the event
1465 * hasn't been linked into its leader's sibling list at this point.
1466 */
1467 n = 0;
1468 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001469 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470 ctrs, events, cflags);
1471 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001472 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001473 }
1474 events[n] = ev;
1475 ctrs[n] = event;
1476 cflags[n] = flags;
1477 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001478 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001479
1480 cpuhw = &get_cpu_var(cpu_hw_events);
1481 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001482
1483 if (has_branch_stack(event)) {
1484 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1485 event->attr.branch_sample_type);
1486
1487 if(cpuhw->bhrb_filter == -1)
1488 return -EOPNOTSUPP;
1489 }
1490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001491 put_cpu_var(cpu_hw_events);
1492 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001493 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001494
1495 event->hw.config = events[n];
1496 event->hw.event_base = cflags[n];
1497 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001498 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001499
1500 /*
1501 * See if we need to reserve the PMU.
1502 * If no events are currently in use, then we have to take a
1503 * mutex to ensure that we don't race with another task doing
1504 * reserve_pmc_hardware or release_pmc_hardware.
1505 */
1506 err = 0;
1507 if (!atomic_inc_not_zero(&num_events)) {
1508 mutex_lock(&pmc_reserve_mutex);
1509 if (atomic_read(&num_events) == 0 &&
1510 reserve_pmc_hardware(perf_event_interrupt))
1511 err = -EBUSY;
1512 else
1513 atomic_inc(&num_events);
1514 mutex_unlock(&pmc_reserve_mutex);
1515 }
1516 event->destroy = hw_perf_event_destroy;
1517
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001518 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519}
1520
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001521static int power_pmu_event_idx(struct perf_event *event)
1522{
1523 return event->hw.idx;
1524}
1525
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001526ssize_t power_events_sysfs_show(struct device *dev,
1527 struct device_attribute *attr, char *page)
1528{
1529 struct perf_pmu_events_attr *pmu_attr;
1530
1531 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1532
1533 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1534}
1535
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001536struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001537 .pmu_enable = power_pmu_enable,
1538 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001539 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001540 .add = power_pmu_add,
1541 .del = power_pmu_del,
1542 .start = power_pmu_start,
1543 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001544 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001545 .start_txn = power_pmu_start_txn,
1546 .cancel_txn = power_pmu_cancel_txn,
1547 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001548 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001549 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001550};
1551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001553 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001554 * things if requested. Note that interrupts are hard-disabled
1555 * here so there is no possibility of being interrupted.
1556 */
1557static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001558 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559{
1560 u64 period = event->hw.sample_period;
1561 s64 prev, delta, left;
1562 int record = 0;
1563
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001564 if (event->hw.state & PERF_HES_STOPPED) {
1565 write_pmc(event->hw.idx, 0);
1566 return;
1567 }
1568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001570 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001571 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001572 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573
1574 /*
1575 * See if the total period for this event has expired,
1576 * and update for the next period.
1577 */
1578 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001579 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001580 if (delta == 0)
1581 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582 if (period) {
1583 if (left <= 0) {
1584 left += period;
1585 if (left <= 0)
1586 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001587 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001588 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589 }
1590 if (left < 0x80000000LL)
1591 val = 0x80000000LL - left;
1592 }
1593
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001594 write_pmc(event->hw.idx, val);
1595 local64_set(&event->hw.prev_count, val);
1596 local64_set(&event->hw.period_left, left);
1597 perf_event_update_userpage(event);
1598
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599 /*
1600 * Finally record data if requested.
1601 */
1602 if (record) {
Peter Zijlstradc1d628a2010-03-03 15:55:04 +01001603 struct perf_sample_data data;
1604
Robert Richterfd0d0002012-04-02 20:19:08 +02001605 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001606
1607 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1608 perf_get_data_addr(regs, &data.addr);
1609
Anshuman Khandual3925f462013-04-22 19:42:44 +00001610 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1611 struct cpu_hw_events *cpuhw;
1612 cpuhw = &__get_cpu_var(cpu_hw_events);
1613 power_pmu_bhrb_read(cpuhw);
1614 data.br_stack = &cpuhw->bhrb_stack;
1615 }
1616
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001617 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001618 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620}
1621
1622/*
1623 * Called from generic code to get the misc flags (i.e. processor mode)
1624 * for an event_id.
1625 */
1626unsigned long perf_misc_flags(struct pt_regs *regs)
1627{
1628 u32 flags = perf_get_misc_flags(regs);
1629
1630 if (flags)
1631 return flags;
1632 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1633 PERF_RECORD_MISC_KERNEL;
1634}
1635
1636/*
1637 * Called from generic code to get the instruction pointer
1638 * for an event_id.
1639 */
1640unsigned long perf_instruction_pointer(struct pt_regs *regs)
1641{
Michael Ellerman33904052013-04-25 19:28:25 +00001642 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001643
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001644 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00001645 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001646 else if (use_siar)
1647 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00001648 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00001649 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650}
1651
Michael Neulingbc09c212012-11-05 15:53:54 +00001652static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11001653{
Anton Blanchard0837e322011-03-09 14:38:42 +11001654 /*
1655 * Events on POWER7 can roll back if a speculative event doesn't
1656 * eventually complete. Unfortunately in some rare cases they will
1657 * raise a performance monitor exception. We need to catch this to
1658 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1659 * cycles from overflow.
1660 *
1661 * We only do this if the first pass fails to find any overflowing
1662 * PMCs because a user might set a period of less than 256 and we
1663 * don't want to mistakenly reset them.
1664 */
Michael Neulingbc09c212012-11-05 15:53:54 +00001665 if ((0x80000000 - val) <= 256)
1666 return true;
1667
1668 return false;
1669}
1670
1671static bool pmc_overflow(unsigned long val)
1672{
1673 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11001674 return true;
1675
1676 return false;
1677}
1678
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679/*
1680 * Performance monitor interrupt stuff
1681 */
1682static void perf_event_interrupt(struct pt_regs *regs)
1683{
Michael Neulingbc09c212012-11-05 15:53:54 +00001684 int i, j;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001685 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1686 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00001687 unsigned long val[8];
1688 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001689 int nmi;
1690
1691 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001692 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693 mfspr(SPRN_PMC6));
1694
1695 perf_read_regs(regs);
1696
1697 nmi = perf_intr_is_nmi(regs);
1698 if (nmi)
1699 nmi_enter();
1700 else
1701 irq_enter();
1702
Michael Neulingbc09c212012-11-05 15:53:54 +00001703 /* Read all the PMCs since we'll need them a bunch of times */
1704 for (i = 0; i < ppmu->n_counter; ++i)
1705 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001706
Michael Neulingbc09c212012-11-05 15:53:54 +00001707 /* Try to find what caused the IRQ */
1708 found = 0;
1709 for (i = 0; i < ppmu->n_counter; ++i) {
1710 if (!pmc_overflow(val[i]))
1711 continue;
1712 if (is_limited_pmc(i + 1))
1713 continue; /* these won't generate IRQs */
1714 /*
1715 * We've found one that's overflowed. For active
1716 * counters we need to log this. For inactive
1717 * counters, we need to reset it anyway
1718 */
1719 found = 1;
1720 active = 0;
1721 for (j = 0; j < cpuhw->n_events; ++j) {
1722 event = cpuhw->event[j];
1723 if (event->hw.idx == (i + 1)) {
1724 active = 1;
1725 record_and_restart(event, val[i], regs);
1726 break;
1727 }
1728 }
1729 if (!active)
1730 /* reset non active counters that have overflowed */
1731 write_pmc(i + 1, 0);
1732 }
1733 if (!found && pvr_version_is(PVR_POWER7)) {
1734 /* check active counters for special buggy p7 overflow */
1735 for (i = 0; i < cpuhw->n_events; ++i) {
1736 event = cpuhw->event[i];
1737 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00001739 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
1740 /* event has overflowed in a buggy way*/
1741 found = 1;
1742 record_and_restart(event,
1743 val[event->hw.idx - 1],
1744 regs);
1745 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746 }
1747 }
Michael Neulingbc09c212012-11-05 15:53:54 +00001748 if ((!found) && printk_ratelimit())
1749 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750
1751 /*
1752 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02001753 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001754 * and thus allow interrupts to occur again.
1755 * XXX might want to use MSR.PM to keep the events frozen until
1756 * we get back out of this interrupt.
1757 */
1758 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1759
1760 if (nmi)
1761 nmi_exit();
1762 else
1763 irq_exit();
1764}
1765
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001766static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767{
1768 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
1769
1770 if (!ppmu)
1771 return;
1772 memset(cpuhw, 0, sizeof(*cpuhw));
1773 cpuhw->mmcr[0] = MMCR0_FC;
1774}
1775
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001776static int __cpuinit
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01001777power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001778{
1779 unsigned int cpu = (long)hcpu;
1780
1781 switch (action & ~CPU_TASKS_FROZEN) {
1782 case CPU_UP_PREPARE:
1783 power_pmu_setup(cpu);
1784 break;
1785
1786 default:
1787 break;
1788 }
1789
1790 return NOTIFY_OK;
1791}
1792
Dmitry Eremin-Solenikov77c23422011-06-29 04:54:00 +00001793int __cpuinit register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794{
1795 if (ppmu)
1796 return -EBUSY; /* something's already registered */
1797
1798 ppmu = pmu;
1799 pr_info("%s performance monitor hardware support registered\n",
1800 pmu->name);
1801
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001802 power_pmu.attr_groups = ppmu->attr_groups;
1803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001804#ifdef MSR_HV
1805 /*
1806 * Use FCHV to ignore kernel events if MSR.HV is set.
1807 */
1808 if (mfmsr() & MSR_HV)
1809 freeze_events_kernel = MMCR0_FCHV;
1810#endif /* CONFIG_PPC64 */
1811
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001812 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001813 perf_cpu_notifier(power_pmu_notifier);
1814
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001815 return 0;
1816}