blob: 67cf22083f4c2cfd6175c91ce416efb68ae1a6e6 [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
Anton Blanchardb0d436c2013-08-07 02:01:24 +100027#define BHRB_EA 0xFFFFFFFFFFFFFFFCUL
Anshuman Khandual3925f462013-04-22 19:42:44 +000028
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
Michael Ellerman7a7a41f2013-06-28 18:15:12 +100078#define MMCR0_FC56 0
Michael Ellerman378a6ee2013-06-28 18:15:11 +100079#define MMCR0_PMAO 0
Michael Ellerman330a1eb2013-06-28 18:15:16 +100080#define MMCR0_EBE 0
81#define MMCR0_PMCC 0
82#define MMCR0_PMCC_U6 0
Ingo Molnarcdd6c482009-09-21 12:02:48 +020083
84#define SPRN_MMCRA SPRN_MMCR2
85#define MMCRA_SAMPLE_ENABLE 0
86
87static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
88{
89 return 0;
90}
91static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
92static inline u32 perf_get_misc_flags(struct pt_regs *regs)
93{
94 return 0;
95}
Anton Blanchard75382aa2012-06-26 01:01:36 +000096static inline void perf_read_regs(struct pt_regs *regs)
97{
98 regs->result = 0;
99}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200100static inline int perf_intr_is_nmi(struct pt_regs *regs)
101{
102 return 0;
103}
104
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000105static inline int siar_valid(struct pt_regs *regs)
106{
107 return 1;
108}
109
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000110static bool is_ebb_event(struct perf_event *event) { return false; }
111static int ebb_event_check(struct perf_event *event) { return 0; }
112static void ebb_event_add(struct perf_event *event) { }
113static void ebb_switch_out(unsigned long mmcr0) { }
114static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
115{
116 return mmcr0;
117}
118
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000119static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
120static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
121void power_pmu_flush_branch_stack(void) {}
122static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200123#endif /* CONFIG_PPC32 */
124
Michael Ellerman33904052013-04-25 19:28:25 +0000125static bool regs_use_siar(struct pt_regs *regs)
126{
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000127 return !!regs->result;
Michael Ellerman33904052013-04-25 19:28:25 +0000128}
129
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200130/*
131 * Things that are specific to 64-bit implementations.
132 */
133#ifdef CONFIG_PPC64
134
135static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
136{
137 unsigned long mmcra = regs->dsisr;
138
Michael Ellerman7a786832013-04-25 19:28:23 +0000139 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200140 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
141 if (slot > 1)
142 return 4 * (slot - 1);
143 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000144
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145 return 0;
146}
147
148/*
149 * The user wants a data address recorded.
150 * If we're not doing instruction sampling, give them the SDAR
151 * (sampled data address). If we are doing instruction sampling, then
152 * only give them the SDAR if it corresponds to the instruction
Michael Ellerman58a032c2013-05-15 20:19:31 +0000153 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
154 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200155 */
156static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
157{
158 unsigned long mmcra = regs->dsisr;
Michael Ellerman58a032c2013-05-15 20:19:31 +0000159 bool sdar_valid;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000160
Michael Ellerman58a032c2013-05-15 20:19:31 +0000161 if (ppmu->flags & PPMU_HAS_SIER)
162 sdar_valid = regs->dar & SIER_SDAR_VALID;
163 else {
164 unsigned long sdsync;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200165
Michael Ellerman58a032c2013-05-15 20:19:31 +0000166 if (ppmu->flags & PPMU_SIAR_VALID)
167 sdsync = POWER7P_MMCRA_SDAR_VALID;
168 else if (ppmu->flags & PPMU_ALT_SIPR)
169 sdsync = POWER6_MMCRA_SDSYNC;
170 else
171 sdsync = MMCRA_SDSYNC;
172
173 sdar_valid = mmcra & sdsync;
174 }
175
176 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200177 *addrp = mfspr(SPRN_SDAR);
178}
179
Michael Ellerman5682c462013-04-25 19:28:24 +0000180static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000181{
182 unsigned long sihv = MMCRA_SIHV;
183
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000184 if (ppmu->flags & PPMU_HAS_SIER)
185 return !!(regs->dar & SIER_SIHV);
186
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000187 if (ppmu->flags & PPMU_ALT_SIPR)
188 sihv = POWER6_MMCRA_SIHV;
189
Michael Ellerman5682c462013-04-25 19:28:24 +0000190 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000191}
192
Michael Ellerman5682c462013-04-25 19:28:24 +0000193static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000194{
195 unsigned long sipr = MMCRA_SIPR;
196
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000197 if (ppmu->flags & PPMU_HAS_SIER)
198 return !!(regs->dar & SIER_SIPR);
199
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000200 if (ppmu->flags & PPMU_ALT_SIPR)
201 sipr = POWER6_MMCRA_SIPR;
202
Michael Ellerman5682c462013-04-25 19:28:24 +0000203 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000204}
205
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000206static inline u32 perf_flags_from_msr(struct pt_regs *regs)
207{
208 if (regs->msr & MSR_PR)
209 return PERF_RECORD_MISC_USER;
210 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
211 return PERF_RECORD_MISC_HYPERVISOR;
212 return PERF_RECORD_MISC_KERNEL;
213}
214
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200215static inline u32 perf_get_misc_flags(struct pt_regs *regs)
216{
Michael Ellerman33904052013-04-25 19:28:25 +0000217 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200218
Anton Blanchard75382aa2012-06-26 01:01:36 +0000219 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000220 return perf_flags_from_msr(regs);
221
222 /*
223 * If we don't have flags in MMCRA, rather than using
224 * the MSR, we intuit the flags from the address in
225 * SIAR which should give slightly more reliable
226 * results
227 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000228 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000229 unsigned long siar = mfspr(SPRN_SIAR);
230 if (siar >= PAGE_OFFSET)
231 return PERF_RECORD_MISC_KERNEL;
232 return PERF_RECORD_MISC_USER;
233 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200234
Michael Neuling7abb8402009-10-14 19:32:15 +0000235 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000236 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000237 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000238
239 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200240 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000241
Michael Neuling7abb8402009-10-14 19:32:15 +0000242 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200243}
244
245/*
246 * Overload regs->dsisr to store MMCRA so we only need to read it once
247 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000248 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000249 * Overload regs->result to specify whether we should use the MSR (result
250 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200251 */
252static inline void perf_read_regs(struct pt_regs *regs)
253{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000254 unsigned long mmcra = mfspr(SPRN_MMCRA);
255 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
256 int use_siar;
257
Michael Ellerman5682c462013-04-25 19:28:24 +0000258 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000259
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000260 if (ppmu->flags & PPMU_HAS_SIER)
261 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000262
263 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000264 * If this isn't a PMU exception (eg a software event) the SIAR is
265 * not valid. Use pt_regs.
266 *
267 * If it is a marked event use the SIAR.
268 *
269 * If the PMU doesn't update the SIAR for non marked events use
270 * pt_regs.
271 *
272 * If the PMU has HV/PR flags then check to see if they
273 * place the exception in userspace. If so, use pt_regs. In
274 * continuous sampling mode the SIAR and the PMU exception are
275 * not synchronised, so they may be many instructions apart.
276 * This can result in confusing backtraces. We still want
277 * hypervisor samples as well as samples in the kernel with
278 * interrupts off hence the userspace check.
279 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000280 if (TRAP(regs) != 0xf00)
281 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000282 else if (marked)
283 use_siar = 1;
284 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
285 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000286 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000287 use_siar = 0;
288 else
289 use_siar = 1;
290
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000291 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200292}
293
294/*
295 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
296 * it as an NMI.
297 */
298static inline int perf_intr_is_nmi(struct pt_regs *regs)
299{
300 return !regs->softe;
301}
302
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000303/*
304 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
305 * must be sampled only if the SIAR-valid bit is set.
306 *
307 * For unmarked instructions and for processors that don't have the SIAR-Valid
308 * bit, assume that SIAR is valid.
309 */
310static inline int siar_valid(struct pt_regs *regs)
311{
312 unsigned long mmcra = regs->dsisr;
313 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
314
Michael Ellerman58a032c2013-05-15 20:19:31 +0000315 if (marked) {
316 if (ppmu->flags & PPMU_HAS_SIER)
317 return regs->dar & SIER_SIAR_VALID;
318
319 if (ppmu->flags & PPMU_SIAR_VALID)
320 return mmcra & POWER7P_MMCRA_SIAR_VALID;
321 }
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000322
323 return 1;
324}
325
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000326
327/* Reset all possible BHRB entries */
328static void power_pmu_bhrb_reset(void)
329{
330 asm volatile(PPC_CLRBHRB);
331}
332
333static void power_pmu_bhrb_enable(struct perf_event *event)
334{
335 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
336
337 if (!ppmu->bhrb_nr)
338 return;
339
340 /* Clear BHRB if we changed task context to avoid data leaks */
341 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
342 power_pmu_bhrb_reset();
343 cpuhw->bhrb_context = event->ctx;
344 }
345 cpuhw->bhrb_users++;
346}
347
348static void power_pmu_bhrb_disable(struct perf_event *event)
349{
350 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
351
352 if (!ppmu->bhrb_nr)
353 return;
354
355 cpuhw->bhrb_users--;
356 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
357
358 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
359 /* BHRB cannot be turned off when other
360 * events are active on the PMU.
361 */
362
363 /* avoid stale pointer */
364 cpuhw->bhrb_context = NULL;
365 }
366}
367
368/* Called from ctxsw to prevent one process's branch entries to
369 * mingle with the other process's entries during context switch.
370 */
371void power_pmu_flush_branch_stack(void)
372{
373 if (ppmu->bhrb_nr)
374 power_pmu_bhrb_reset();
375}
Michael Neuling69123182013-05-13 18:44:58 +0000376/* Calculate the to address for a branch */
377static __u64 power_pmu_bhrb_to(u64 addr)
378{
379 unsigned int instr;
380 int ret;
381 __u64 target;
382
383 if (is_kernel_addr(addr))
384 return branch_target((unsigned int *)addr);
385
386 /* Userspace: need copy instruction here then translate it */
387 pagefault_disable();
388 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
389 if (ret) {
390 pagefault_enable();
391 return 0;
392 }
393 pagefault_enable();
394
395 target = branch_target(&instr);
396 if ((!target) || (instr & BRANCH_ABSOLUTE))
397 return target;
398
399 /* Translate relative branch target from kernel to user address */
400 return target - (unsigned long)&instr + addr;
401}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000402
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000403/* Processing BHRB entries */
Michael Neuling506e70d2013-05-13 18:44:57 +0000404void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000405{
406 u64 val;
407 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000408 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000409
410 r_index = 0;
411 u_index = 0;
412 while (r_index < ppmu->bhrb_nr) {
413 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000414 val = read_bhrb(r_index++);
415 if (!val)
416 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000417 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000418 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000419 addr = val & BHRB_EA;
420 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000421
Michael Neuling506e70d2013-05-13 18:44:57 +0000422 if (!addr)
423 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000424 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000425
Michael Neuling506e70d2013-05-13 18:44:57 +0000426 /* Branches are read most recent first (ie. mfbhrb 0 is
427 * the most recent branch).
428 * There are two types of valid entries:
429 * 1) a target entry which is the to address of a
430 * computed goto like a blr,bctr,btar. The next
431 * entry read from the bhrb will be branch
432 * corresponding to this target (ie. the actual
433 * blr/bctr/btar instruction).
434 * 2) a from address which is an actual branch. If a
435 * target entry proceeds this, then this is the
436 * matching branch for that target. If this is not
437 * following a target entry, then this is a branch
438 * where the target is given as an immediate field
439 * in the instruction (ie. an i or b form branch).
440 * In this case we need to read the instruction from
441 * memory to determine the target/to address.
442 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000443
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000444 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000445 /* Target branches use two entries
446 * (ie. computed gotos/XL form)
447 */
448 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000449 cpuhw->bhrb_entries[u_index].mispred = pred;
450 cpuhw->bhrb_entries[u_index].predicted = ~pred;
451
Michael Neuling506e70d2013-05-13 18:44:57 +0000452 /* Get from address in next entry */
453 val = read_bhrb(r_index++);
454 addr = val & BHRB_EA;
455 if (val & BHRB_TARGET) {
456 /* Shouldn't have two targets in a
457 row.. Reset index and try again */
458 r_index--;
459 addr = 0;
460 }
461 cpuhw->bhrb_entries[u_index].from = addr;
462 } else {
463 /* Branches to immediate field
464 (ie I or B form) */
465 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000466 cpuhw->bhrb_entries[u_index].to =
467 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000468 cpuhw->bhrb_entries[u_index].mispred = pred;
469 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000470 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000471 u_index++;
472
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000473 }
474 }
475 cpuhw->bhrb_stack.nr = u_index;
476 return;
477}
478
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000479static bool is_ebb_event(struct perf_event *event)
480{
481 /*
482 * This could be a per-PMU callback, but we'd rather avoid the cost. We
483 * check that the PMU supports EBB, meaning those that don't can still
484 * use bit 63 of the event code for something else if they wish.
485 */
486 return (ppmu->flags & PPMU_EBB) &&
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000487 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000488}
489
490static int ebb_event_check(struct perf_event *event)
491{
492 struct perf_event *leader = event->group_leader;
493
494 /* Event and group leader must agree on EBB */
495 if (is_ebb_event(leader) != is_ebb_event(event))
496 return -EINVAL;
497
498 if (is_ebb_event(event)) {
499 if (!(event->attach_state & PERF_ATTACH_TASK))
500 return -EINVAL;
501
502 if (!leader->attr.pinned || !leader->attr.exclusive)
503 return -EINVAL;
504
505 if (event->attr.inherit || event->attr.sample_period ||
506 event->attr.enable_on_exec || event->attr.freq)
507 return -EINVAL;
508 }
509
510 return 0;
511}
512
513static void ebb_event_add(struct perf_event *event)
514{
515 if (!is_ebb_event(event) || current->thread.used_ebb)
516 return;
517
518 /*
519 * IFF this is the first time we've added an EBB event, set
520 * PMXE in the user MMCR0 so we can detect when it's cleared by
521 * userspace. We need this so that we can context switch while
522 * userspace is in the EBB handler (where PMXE is 0).
523 */
524 current->thread.used_ebb = 1;
525 current->thread.mmcr0 |= MMCR0_PMXE;
526}
527
528static void ebb_switch_out(unsigned long mmcr0)
529{
530 if (!(mmcr0 & MMCR0_EBE))
531 return;
532
533 current->thread.siar = mfspr(SPRN_SIAR);
534 current->thread.sier = mfspr(SPRN_SIER);
535 current->thread.sdar = mfspr(SPRN_SDAR);
536 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
537 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
538}
539
540static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
541{
542 if (!ebb)
543 goto out;
544
545 /* Enable EBB and read/write to all 6 PMCs for userspace */
546 mmcr0 |= MMCR0_EBE | MMCR0_PMCC_U6;
547
548 /* Add any bits from the user reg, FC or PMAO */
549 mmcr0 |= current->thread.mmcr0;
550
551 /* Be careful not to set PMXE if userspace had it cleared */
552 if (!(current->thread.mmcr0 & MMCR0_PMXE))
553 mmcr0 &= ~MMCR0_PMXE;
554
555 mtspr(SPRN_SIAR, current->thread.siar);
556 mtspr(SPRN_SIER, current->thread.sier);
557 mtspr(SPRN_SDAR, current->thread.sdar);
558 mtspr(SPRN_MMCR2, current->thread.mmcr2);
559out:
560 return mmcr0;
561}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200562#endif /* CONFIG_PPC64 */
563
564static void perf_event_interrupt(struct pt_regs *regs);
565
566void perf_event_print_debug(void)
567{
568}
569
570/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200571 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200572 */
573static unsigned long read_pmc(int idx)
574{
575 unsigned long val;
576
577 switch (idx) {
578 case 1:
579 val = mfspr(SPRN_PMC1);
580 break;
581 case 2:
582 val = mfspr(SPRN_PMC2);
583 break;
584 case 3:
585 val = mfspr(SPRN_PMC3);
586 break;
587 case 4:
588 val = mfspr(SPRN_PMC4);
589 break;
590 case 5:
591 val = mfspr(SPRN_PMC5);
592 break;
593 case 6:
594 val = mfspr(SPRN_PMC6);
595 break;
596#ifdef CONFIG_PPC64
597 case 7:
598 val = mfspr(SPRN_PMC7);
599 break;
600 case 8:
601 val = mfspr(SPRN_PMC8);
602 break;
603#endif /* CONFIG_PPC64 */
604 default:
605 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
606 val = 0;
607 }
608 return val;
609}
610
611/*
612 * Write one PMC.
613 */
614static void write_pmc(int idx, unsigned long val)
615{
616 switch (idx) {
617 case 1:
618 mtspr(SPRN_PMC1, val);
619 break;
620 case 2:
621 mtspr(SPRN_PMC2, val);
622 break;
623 case 3:
624 mtspr(SPRN_PMC3, val);
625 break;
626 case 4:
627 mtspr(SPRN_PMC4, val);
628 break;
629 case 5:
630 mtspr(SPRN_PMC5, val);
631 break;
632 case 6:
633 mtspr(SPRN_PMC6, val);
634 break;
635#ifdef CONFIG_PPC64
636 case 7:
637 mtspr(SPRN_PMC7, val);
638 break;
639 case 8:
640 mtspr(SPRN_PMC8, val);
641 break;
642#endif /* CONFIG_PPC64 */
643 default:
644 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
645 }
646}
647
648/*
649 * Check if a set of events can all go on the PMU at once.
650 * If they can't, this will look at alternative codes for the events
651 * and see if any combination of alternative codes is feasible.
652 * The feasible set is returned in event_id[].
653 */
654static int power_check_constraints(struct cpu_hw_events *cpuhw,
655 u64 event_id[], unsigned int cflags[],
656 int n_ev)
657{
658 unsigned long mask, value, nv;
659 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
660 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
661 int i, j;
662 unsigned long addf = ppmu->add_fields;
663 unsigned long tadd = ppmu->test_adder;
664
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000665 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200666 return -1;
667
668 /* First see if the events will go on as-is */
669 for (i = 0; i < n_ev; ++i) {
670 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
671 && !ppmu->limited_pmc_event(event_id[i])) {
672 ppmu->get_alternatives(event_id[i], cflags[i],
673 cpuhw->alternatives[i]);
674 event_id[i] = cpuhw->alternatives[i][0];
675 }
676 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
677 &cpuhw->avalues[i][0]))
678 return -1;
679 }
680 value = mask = 0;
681 for (i = 0; i < n_ev; ++i) {
682 nv = (value | cpuhw->avalues[i][0]) +
683 (value & cpuhw->avalues[i][0] & addf);
684 if ((((nv + tadd) ^ value) & mask) != 0 ||
685 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
686 cpuhw->amasks[i][0]) != 0)
687 break;
688 value = nv;
689 mask |= cpuhw->amasks[i][0];
690 }
691 if (i == n_ev)
692 return 0; /* all OK */
693
694 /* doesn't work, gather alternatives... */
695 if (!ppmu->get_alternatives)
696 return -1;
697 for (i = 0; i < n_ev; ++i) {
698 choice[i] = 0;
699 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
700 cpuhw->alternatives[i]);
701 for (j = 1; j < n_alt[i]; ++j)
702 ppmu->get_constraint(cpuhw->alternatives[i][j],
703 &cpuhw->amasks[i][j],
704 &cpuhw->avalues[i][j]);
705 }
706
707 /* enumerate all possibilities and see if any will work */
708 i = 0;
709 j = -1;
710 value = mask = nv = 0;
711 while (i < n_ev) {
712 if (j >= 0) {
713 /* we're backtracking, restore context */
714 value = svalues[i];
715 mask = smasks[i];
716 j = choice[i];
717 }
718 /*
719 * See if any alternative k for event_id i,
720 * where k > j, will satisfy the constraints.
721 */
722 while (++j < n_alt[i]) {
723 nv = (value | cpuhw->avalues[i][j]) +
724 (value & cpuhw->avalues[i][j] & addf);
725 if ((((nv + tadd) ^ value) & mask) == 0 &&
726 (((nv + tadd) ^ cpuhw->avalues[i][j])
727 & cpuhw->amasks[i][j]) == 0)
728 break;
729 }
730 if (j >= n_alt[i]) {
731 /*
732 * No feasible alternative, backtrack
733 * to event_id i-1 and continue enumerating its
734 * alternatives from where we got up to.
735 */
736 if (--i < 0)
737 return -1;
738 } else {
739 /*
740 * Found a feasible alternative for event_id i,
741 * remember where we got up to with this event_id,
742 * go on to the next event_id, and start with
743 * the first alternative for it.
744 */
745 choice[i] = j;
746 svalues[i] = value;
747 smasks[i] = mask;
748 value = nv;
749 mask |= cpuhw->amasks[i][j];
750 ++i;
751 j = -1;
752 }
753 }
754
755 /* OK, we have a feasible combination, tell the caller the solution */
756 for (i = 0; i < n_ev; ++i)
757 event_id[i] = cpuhw->alternatives[i][choice[i]];
758 return 0;
759}
760
761/*
762 * Check if newly-added events have consistent settings for
763 * exclude_{user,kernel,hv} with each other and any previously
764 * added events.
765 */
766static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
767 int n_prev, int n_new)
768{
769 int eu = 0, ek = 0, eh = 0;
770 int i, n, first;
771 struct perf_event *event;
772
773 n = n_prev + n_new;
774 if (n <= 1)
775 return 0;
776
777 first = 1;
778 for (i = 0; i < n; ++i) {
779 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
780 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
781 continue;
782 }
783 event = ctrs[i];
784 if (first) {
785 eu = event->attr.exclude_user;
786 ek = event->attr.exclude_kernel;
787 eh = event->attr.exclude_hv;
788 first = 0;
789 } else if (event->attr.exclude_user != eu ||
790 event->attr.exclude_kernel != ek ||
791 event->attr.exclude_hv != eh) {
792 return -EAGAIN;
793 }
794 }
795
796 if (eu || ek || eh)
797 for (i = 0; i < n; ++i)
798 if (cflags[i] & PPMU_LIMITED_PMC_OK)
799 cflags[i] |= PPMU_LIMITED_PMC_REQD;
800
801 return 0;
802}
803
Eric B Munson86c74ab2011-04-15 08:12:30 +0000804static u64 check_and_compute_delta(u64 prev, u64 val)
805{
806 u64 delta = (val - prev) & 0xfffffffful;
807
808 /*
809 * POWER7 can roll back counter values, if the new value is smaller
810 * than the previous value it will cause the delta and the counter to
811 * have bogus values unless we rolled a counter over. If a coutner is
812 * rolled back, it will be smaller, but within 256, which is the maximum
813 * number of events to rollback at once. If we dectect a rollback
814 * return 0. This can lead to a small lack of precision in the
815 * counters.
816 */
817 if (prev > val && (prev - val) < 256)
818 delta = 0;
819
820 return delta;
821}
822
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200823static void power_pmu_read(struct perf_event *event)
824{
825 s64 val, delta, prev;
826
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200827 if (event->hw.state & PERF_HES_STOPPED)
828 return;
829
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200830 if (!event->hw.idx)
831 return;
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000832
833 if (is_ebb_event(event)) {
834 val = read_pmc(event->hw.idx);
835 local64_set(&event->hw.prev_count, val);
836 return;
837 }
838
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200839 /*
840 * Performance monitor interrupts come even when interrupts
841 * are soft-disabled, as long as interrupts are hard-enabled.
842 * Therefore we treat them like NMIs.
843 */
844 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +0200845 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200846 barrier();
847 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +0000848 delta = check_and_compute_delta(prev, val);
849 if (!delta)
850 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200851 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200852
Peter Zijlstrae7850592010-05-21 14:43:08 +0200853 local64_add(delta, &event->count);
854 local64_sub(delta, &event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200855}
856
857/*
858 * On some machines, PMC5 and PMC6 can't be written, don't respect
859 * the freeze conditions, and don't generate interrupts. This tells
860 * us if `event' is using such a PMC.
861 */
862static int is_limited_pmc(int pmcnum)
863{
864 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
865 && (pmcnum == 5 || pmcnum == 6);
866}
867
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000868static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200869 unsigned long pmc5, unsigned long pmc6)
870{
871 struct perf_event *event;
872 u64 val, prev, delta;
873 int i;
874
875 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000876 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200877 if (!event->hw.idx)
878 continue;
879 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200880 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200881 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000882 delta = check_and_compute_delta(prev, val);
883 if (delta)
884 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885 }
886}
887
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000888static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200889 unsigned long pmc5, unsigned long pmc6)
890{
891 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000892 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200893 int i;
894
895 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000896 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200897 event->hw.idx = cpuhw->limited_hwidx[i];
898 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000899 prev = local64_read(&event->hw.prev_count);
900 if (check_and_compute_delta(prev, val))
901 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200902 perf_event_update_userpage(event);
903 }
904}
905
906/*
907 * Since limited events don't respect the freeze conditions, we
908 * have to read them immediately after freezing or unfreezing the
909 * other events. We try to keep the values from the limited
910 * events as consistent as possible by keeping the delay (in
911 * cycles and instructions) between freezing/unfreezing and reading
912 * the limited events as small and consistent as possible.
913 * Therefore, if any limited events are in use, we read them
914 * both, and always in the same order, to minimize variability,
915 * and do it inside the same asm that writes MMCR0.
916 */
917static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
918{
919 unsigned long pmc5, pmc6;
920
921 if (!cpuhw->n_limited) {
922 mtspr(SPRN_MMCR0, mmcr0);
923 return;
924 }
925
926 /*
927 * Write MMCR0, then read PMC5 and PMC6 immediately.
928 * To ensure we don't get a performance monitor interrupt
929 * between writing MMCR0 and freezing/thawing the limited
930 * events, we first write MMCR0 with the event overflow
931 * interrupt enable bits turned off.
932 */
933 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
934 : "=&r" (pmc5), "=&r" (pmc6)
935 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
936 "i" (SPRN_MMCR0),
937 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
938
939 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000940 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200941 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000942 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200943
944 /*
945 * Write the full MMCR0 including the event overflow interrupt
946 * enable bits, if necessary.
947 */
948 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
949 mtspr(SPRN_MMCR0, mmcr0);
950}
951
952/*
953 * Disable all events to prevent PMU interrupts and to allow
954 * events to be added or removed.
955 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200956static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200957{
958 struct cpu_hw_events *cpuhw;
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000959 unsigned long flags, mmcr0, val;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200960
961 if (!ppmu)
962 return;
963 local_irq_save(flags);
964 cpuhw = &__get_cpu_var(cpu_hw_events);
965
966 if (!cpuhw->disabled) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200967 /*
968 * Check if we ever enabled the PMU on this cpu.
969 */
970 if (!cpuhw->pmcs_enabled) {
971 ppc_enable_pmcs();
972 cpuhw->pmcs_enabled = 1;
973 }
974
975 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000976 * Set the 'freeze counters' bit, clear EBE/PMCC/PMAO/FC56.
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000977 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000978 val = mmcr0 = mfspr(SPRN_MMCR0);
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000979 val |= MMCR0_FC;
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000980 val &= ~(MMCR0_EBE | MMCR0_PMCC | MMCR0_PMAO | MMCR0_FC56);
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000981
982 /*
983 * The barrier is to make sure the mtspr has been
984 * executed and the PMU has frozen the events etc.
985 * before we return.
986 */
987 write_mmcr0(cpuhw, val);
988 mb();
989
990 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200991 * Disable instruction sampling if it was enabled
992 */
993 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
994 mtspr(SPRN_MMCRA,
995 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
996 mb();
997 }
998
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000999 cpuhw->disabled = 1;
1000 cpuhw->n_added = 0;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001001
1002 ebb_switch_out(mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001003 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001004
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005 local_irq_restore(flags);
1006}
1007
1008/*
1009 * Re-enable all events if disable == 0.
1010 * If we were previously disabled and events were added, then
1011 * put the new config on the PMU.
1012 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001013static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014{
1015 struct perf_event *event;
1016 struct cpu_hw_events *cpuhw;
1017 unsigned long flags;
1018 long i;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001019 unsigned long val, mmcr0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001020 s64 left;
1021 unsigned int hwc_index[MAX_HWEVENTS];
1022 int n_lim;
1023 int idx;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001024 bool ebb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001025
1026 if (!ppmu)
1027 return;
1028 local_irq_save(flags);
Michael Ellerman0a488432013-06-28 18:15:13 +10001029
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001030 cpuhw = &__get_cpu_var(cpu_hw_events);
Michael Ellerman0a488432013-06-28 18:15:13 +10001031 if (!cpuhw->disabled)
1032 goto out;
1033
Michael Ellerman4ea355b2013-06-28 18:15:14 +10001034 if (cpuhw->n_events == 0) {
1035 ppc_set_pmu_inuse(0);
1036 goto out;
1037 }
1038
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001039 cpuhw->disabled = 0;
1040
1041 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001042 * EBB requires an exclusive group and all events must have the EBB
1043 * flag set, or not set, so we can just check a single event. Also we
1044 * know we have at least one event.
1045 */
1046 ebb = is_ebb_event(cpuhw->event[0]);
1047
1048 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001049 * If we didn't change anything, or only removed events,
1050 * no need to recalculate MMCR* settings and reset the PMCs.
1051 * Just reenable the PMU with the current MMCR* settings
1052 * (possibly updated for removal of events).
1053 */
1054 if (!cpuhw->n_added) {
1055 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1056 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001057 goto out_enable;
1058 }
1059
1060 /*
1061 * Compute MMCR* values for the new set of events
1062 */
1063 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
1064 cpuhw->mmcr)) {
1065 /* shouldn't ever get here */
1066 printk(KERN_ERR "oops compute_mmcr failed\n");
1067 goto out;
1068 }
1069
1070 /*
1071 * Add in MMCR0 freeze bits corresponding to the
1072 * attr.exclude_* bits for the first event.
1073 * We have already checked that all events have the
1074 * same values for these bits as the first event.
1075 */
1076 event = cpuhw->event[0];
1077 if (event->attr.exclude_user)
1078 cpuhw->mmcr[0] |= MMCR0_FCP;
1079 if (event->attr.exclude_kernel)
1080 cpuhw->mmcr[0] |= freeze_events_kernel;
1081 if (event->attr.exclude_hv)
1082 cpuhw->mmcr[0] |= MMCR0_FCHV;
1083
1084 /*
1085 * Write the new configuration to MMCR* with the freeze
1086 * bit set and set the hardware events to their initial values.
1087 * Then unfreeze the events.
1088 */
1089 ppc_set_pmu_inuse(1);
1090 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1091 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1092 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1093 | MMCR0_FC);
1094
1095 /*
1096 * Read off any pre-existing events that need to move
1097 * to another PMC.
1098 */
1099 for (i = 0; i < cpuhw->n_events; ++i) {
1100 event = cpuhw->event[i];
1101 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1102 power_pmu_read(event);
1103 write_pmc(event->hw.idx, 0);
1104 event->hw.idx = 0;
1105 }
1106 }
1107
1108 /*
1109 * Initialize the PMCs for all the new and moved events.
1110 */
1111 cpuhw->n_limited = n_lim = 0;
1112 for (i = 0; i < cpuhw->n_events; ++i) {
1113 event = cpuhw->event[i];
1114 if (event->hw.idx)
1115 continue;
1116 idx = hwc_index[i] + 1;
1117 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001118 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119 cpuhw->limited_hwidx[n_lim] = idx;
1120 ++n_lim;
1121 continue;
1122 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001123
1124 if (ebb)
1125 val = local64_read(&event->hw.prev_count);
1126 else {
1127 val = 0;
1128 if (event->hw.sample_period) {
1129 left = local64_read(&event->hw.period_left);
1130 if (left < 0x80000000L)
1131 val = 0x80000000L - left;
1132 }
1133 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001134 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001136 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001137 if (event->hw.state & PERF_HES_STOPPED)
1138 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139 write_pmc(idx, val);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001140
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141 perf_event_update_userpage(event);
1142 }
1143 cpuhw->n_limited = n_lim;
1144 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1145
1146 out_enable:
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001147 mmcr0 = ebb_switch_in(ebb, cpuhw->mmcr[0]);
1148
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 mb();
Anshuman Khandualb4d6c062013-12-18 13:14:53 +11001150 if (cpuhw->bhrb_users)
1151 ppmu->config_bhrb(cpuhw->bhrb_filter);
1152
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001153 write_mmcr0(cpuhw, mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154
1155 /*
1156 * Enable instruction sampling if necessary
1157 */
1158 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1159 mb();
1160 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1161 }
1162
1163 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001164
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001165 local_irq_restore(flags);
1166}
1167
1168static int collect_events(struct perf_event *group, int max_count,
1169 struct perf_event *ctrs[], u64 *events,
1170 unsigned int *flags)
1171{
1172 int n = 0;
1173 struct perf_event *event;
1174
1175 if (!is_software_event(group)) {
1176 if (n >= max_count)
1177 return -1;
1178 ctrs[n] = group;
1179 flags[n] = group->hw.event_base;
1180 events[n++] = group->hw.config;
1181 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001182 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001183 if (!is_software_event(event) &&
1184 event->state != PERF_EVENT_STATE_OFF) {
1185 if (n >= max_count)
1186 return -1;
1187 ctrs[n] = event;
1188 flags[n] = event->hw.event_base;
1189 events[n++] = event->hw.config;
1190 }
1191 }
1192 return n;
1193}
1194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195/*
1196 * Add a event to the PMU.
1197 * If all events are not already frozen, then we disable and
1198 * re-enable the PMU in order to get hw_perf_enable to do the
1199 * actual work of reconfiguring the PMU.
1200 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001201static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001202{
1203 struct cpu_hw_events *cpuhw;
1204 unsigned long flags;
1205 int n0;
1206 int ret = -EAGAIN;
1207
1208 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001209 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001210
1211 /*
1212 * Add the event to the list (if there is room)
1213 * and check whether the total set is still feasible.
1214 */
1215 cpuhw = &__get_cpu_var(cpu_hw_events);
1216 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001217 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001218 goto out;
1219 cpuhw->event[n0] = event;
1220 cpuhw->events[n0] = event->hw.config;
1221 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001222
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001223 /*
1224 * This event may have been disabled/stopped in record_and_restart()
1225 * because we exceeded the ->event_limit. If re-starting the event,
1226 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1227 * notification is re-enabled.
1228 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001229 if (!(ef_flags & PERF_EF_START))
1230 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001231 else
1232 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001233
Lin Ming8e6d5572010-05-08 20:28:41 +10001234 /*
1235 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001236 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001237 * at commit time(->commit_txn) as a whole
1238 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001239 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001240 goto nocheck;
1241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1243 goto out;
1244 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1245 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001247
1248nocheck:
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001249 ebb_event_add(event);
1250
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 ++cpuhw->n_events;
1252 ++cpuhw->n_added;
1253
1254 ret = 0;
1255 out:
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301256 if (has_branch_stack(event)) {
Anshuman Khandual3925f462013-04-22 19:42:44 +00001257 power_pmu_bhrb_enable(event);
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301258 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1259 event->attr.branch_sample_type);
1260 }
Anshuman Khandual3925f462013-04-22 19:42:44 +00001261
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001262 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001263 local_irq_restore(flags);
1264 return ret;
1265}
1266
1267/*
1268 * Remove a event from the PMU.
1269 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001270static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001271{
1272 struct cpu_hw_events *cpuhw;
1273 long i;
1274 unsigned long flags;
1275
1276 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001277 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001278
1279 power_pmu_read(event);
1280
1281 cpuhw = &__get_cpu_var(cpu_hw_events);
1282 for (i = 0; i < cpuhw->n_events; ++i) {
1283 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001284 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001285 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001286 cpuhw->events[i-1] = cpuhw->events[i];
1287 cpuhw->flags[i-1] = cpuhw->flags[i];
1288 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001289 --cpuhw->n_events;
1290 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1291 if (event->hw.idx) {
1292 write_pmc(event->hw.idx, 0);
1293 event->hw.idx = 0;
1294 }
1295 perf_event_update_userpage(event);
1296 break;
1297 }
1298 }
1299 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001300 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301 break;
1302 if (i < cpuhw->n_limited) {
1303 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001304 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001305 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1306 }
1307 --cpuhw->n_limited;
1308 }
1309 if (cpuhw->n_events == 0) {
1310 /* disable exceptions if no events are running */
1311 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1312 }
1313
Anshuman Khandual3925f462013-04-22 19:42:44 +00001314 if (has_branch_stack(event))
1315 power_pmu_bhrb_disable(event);
1316
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001317 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001318 local_irq_restore(flags);
1319}
1320
1321/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001322 * POWER-PMU does not support disabling individual counters, hence
1323 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001325
1326static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001327{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001328 unsigned long flags;
1329 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001330 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001331
1332 if (!event->hw.idx || !event->hw.sample_period)
1333 return;
1334
1335 if (!(event->hw.state & PERF_HES_STOPPED))
1336 return;
1337
1338 if (ef_flags & PERF_EF_RELOAD)
1339 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1340
1341 local_irq_save(flags);
1342 perf_pmu_disable(event->pmu);
1343
1344 event->hw.state = 0;
1345 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001346
1347 val = 0;
1348 if (left < 0x80000000L)
1349 val = 0x80000000L - left;
1350
1351 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001352
1353 perf_event_update_userpage(event);
1354 perf_pmu_enable(event->pmu);
1355 local_irq_restore(flags);
1356}
1357
1358static void power_pmu_stop(struct perf_event *event, int ef_flags)
1359{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 unsigned long flags;
1361
1362 if (!event->hw.idx || !event->hw.sample_period)
1363 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001364
1365 if (event->hw.state & PERF_HES_STOPPED)
1366 return;
1367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001368 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001369 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001370
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001372 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1373 write_pmc(event->hw.idx, 0);
1374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001375 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001376 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377 local_irq_restore(flags);
1378}
1379
Lin Ming8e6d5572010-05-08 20:28:41 +10001380/*
1381 * Start group events scheduling transaction
1382 * Set the flag to make pmu::enable() not perform the
1383 * schedulability test, it will be performed at commit time
1384 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001385void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001386{
1387 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1388
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001389 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001390 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001391 cpuhw->n_txn_start = cpuhw->n_events;
1392}
1393
1394/*
1395 * Stop group events scheduling transaction
1396 * Clear the flag and pmu::enable() will perform the
1397 * schedulability test.
1398 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001399void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001400{
1401 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1402
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001403 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001404 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001405}
1406
1407/*
1408 * Commit group events scheduling transaction
1409 * Perform the group schedulability test as a whole
1410 * Return 0 if success
1411 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001412int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001413{
1414 struct cpu_hw_events *cpuhw;
1415 long i, n;
1416
1417 if (!ppmu)
1418 return -EAGAIN;
1419 cpuhw = &__get_cpu_var(cpu_hw_events);
1420 n = cpuhw->n_events;
1421 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1422 return -EAGAIN;
1423 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1424 if (i < 0)
1425 return -EAGAIN;
1426
1427 for (i = cpuhw->n_txn_start; i < n; ++i)
1428 cpuhw->event[i]->hw.config = cpuhw->events[i];
1429
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001430 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001431 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001432 return 0;
1433}
1434
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435/*
1436 * Return 1 if we might be able to put event on a limited PMC,
1437 * or 0 if not.
1438 * A event can only go on a limited PMC if it counts something
1439 * that a limited PMC can count, doesn't require interrupts, and
1440 * doesn't exclude any processor mode.
1441 */
1442static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1443 unsigned int flags)
1444{
1445 int n;
1446 u64 alt[MAX_EVENT_ALTERNATIVES];
1447
1448 if (event->attr.exclude_user
1449 || event->attr.exclude_kernel
1450 || event->attr.exclude_hv
1451 || event->attr.sample_period)
1452 return 0;
1453
1454 if (ppmu->limited_pmc_event(ev))
1455 return 1;
1456
1457 /*
1458 * The requested event_id isn't on a limited PMC already;
1459 * see if any alternative code goes on a limited PMC.
1460 */
1461 if (!ppmu->get_alternatives)
1462 return 0;
1463
1464 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1465 n = ppmu->get_alternatives(ev, flags, alt);
1466
1467 return n > 0;
1468}
1469
1470/*
1471 * Find an alternative event_id that goes on a normal PMC, if possible,
1472 * and return the event_id code, or 0 if there is no such alternative.
1473 * (Note: event_id code 0 is "don't count" on all machines.)
1474 */
1475static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1476{
1477 u64 alt[MAX_EVENT_ALTERNATIVES];
1478 int n;
1479
1480 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1481 n = ppmu->get_alternatives(ev, flags, alt);
1482 if (!n)
1483 return 0;
1484 return alt[0];
1485}
1486
1487/* Number of perf_events counting hardware events */
1488static atomic_t num_events;
1489/* Used to avoid races in calling reserve/release_pmc_hardware */
1490static DEFINE_MUTEX(pmc_reserve_mutex);
1491
1492/*
1493 * Release the PMU if this is the last perf_event.
1494 */
1495static void hw_perf_event_destroy(struct perf_event *event)
1496{
1497 if (!atomic_add_unless(&num_events, -1, 1)) {
1498 mutex_lock(&pmc_reserve_mutex);
1499 if (atomic_dec_return(&num_events) == 0)
1500 release_pmc_hardware();
1501 mutex_unlock(&pmc_reserve_mutex);
1502 }
1503}
1504
1505/*
1506 * Translate a generic cache event_id config to a raw event_id code.
1507 */
1508static int hw_perf_cache_event(u64 config, u64 *eventp)
1509{
1510 unsigned long type, op, result;
1511 int ev;
1512
1513 if (!ppmu->cache_events)
1514 return -EINVAL;
1515
1516 /* unpack config */
1517 type = config & 0xff;
1518 op = (config >> 8) & 0xff;
1519 result = (config >> 16) & 0xff;
1520
1521 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1522 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1523 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1524 return -EINVAL;
1525
1526 ev = (*ppmu->cache_events)[type][op][result];
1527 if (ev == 0)
1528 return -EOPNOTSUPP;
1529 if (ev == -1)
1530 return -EINVAL;
1531 *eventp = ev;
1532 return 0;
1533}
1534
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001535static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536{
1537 u64 ev;
1538 unsigned long flags;
1539 struct perf_event *ctrs[MAX_HWEVENTS];
1540 u64 events[MAX_HWEVENTS];
1541 unsigned int cflags[MAX_HWEVENTS];
1542 int n;
1543 int err;
1544 struct cpu_hw_events *cpuhw;
1545
1546 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001547 return -ENOENT;
1548
Anshuman Khandual3925f462013-04-22 19:42:44 +00001549 if (has_branch_stack(event)) {
1550 /* PMU has BHRB enabled */
1551 if (!(ppmu->flags & PPMU_BHRB))
1552 return -EOPNOTSUPP;
1553 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001554
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555 switch (event->attr.type) {
1556 case PERF_TYPE_HARDWARE:
1557 ev = event->attr.config;
1558 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001559 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560 ev = ppmu->generic_events[ev];
1561 break;
1562 case PERF_TYPE_HW_CACHE:
1563 err = hw_perf_cache_event(event->attr.config, &ev);
1564 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001565 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566 break;
1567 case PERF_TYPE_RAW:
1568 ev = event->attr.config;
1569 break;
1570 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001571 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 event->hw.config_base = ev;
1575 event->hw.idx = 0;
1576
1577 /*
1578 * If we are not running on a hypervisor, force the
1579 * exclude_hv bit to 0 so that we don't care what
1580 * the user set it to.
1581 */
1582 if (!firmware_has_feature(FW_FEATURE_LPAR))
1583 event->attr.exclude_hv = 0;
1584
1585 /*
1586 * If this is a per-task event, then we can use
1587 * PM_RUN_* events interchangeably with their non RUN_*
1588 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1589 * XXX we should check if the task is an idle task.
1590 */
1591 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001592 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593 flags |= PPMU_ONLY_COUNT_RUN;
1594
1595 /*
1596 * If this machine has limited events, check whether this
1597 * event_id could go on a limited event.
1598 */
1599 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1600 if (can_go_on_limited_pmc(event, ev, flags)) {
1601 flags |= PPMU_LIMITED_PMC_OK;
1602 } else if (ppmu->limited_pmc_event(ev)) {
1603 /*
1604 * The requested event_id is on a limited PMC,
1605 * but we can't use a limited PMC; see if any
1606 * alternative goes on a normal PMC.
1607 */
1608 ev = normal_pmc_alternative(ev, flags);
1609 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001610 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611 }
1612 }
1613
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001614 /* Extra checks for EBB */
1615 err = ebb_event_check(event);
1616 if (err)
1617 return err;
1618
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619 /*
1620 * If this is in a group, check if it can go on with all the
1621 * other hardware events in the group. We assume the event
1622 * hasn't been linked into its leader's sibling list at this point.
1623 */
1624 n = 0;
1625 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001626 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 ctrs, events, cflags);
1628 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001629 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001630 }
1631 events[n] = ev;
1632 ctrs[n] = event;
1633 cflags[n] = flags;
1634 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001635 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001636
1637 cpuhw = &get_cpu_var(cpu_hw_events);
1638 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001639
1640 if (has_branch_stack(event)) {
1641 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1642 event->attr.branch_sample_type);
1643
1644 if(cpuhw->bhrb_filter == -1)
1645 return -EOPNOTSUPP;
1646 }
1647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001648 put_cpu_var(cpu_hw_events);
1649 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001650 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001651
1652 event->hw.config = events[n];
1653 event->hw.event_base = cflags[n];
1654 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001655 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656
1657 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001658 * For EBB events we just context switch the PMC value, we don't do any
1659 * of the sample_period logic. We use hw.prev_count for this.
1660 */
1661 if (is_ebb_event(event))
1662 local64_set(&event->hw.prev_count, 0);
1663
1664 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001665 * See if we need to reserve the PMU.
1666 * If no events are currently in use, then we have to take a
1667 * mutex to ensure that we don't race with another task doing
1668 * reserve_pmc_hardware or release_pmc_hardware.
1669 */
1670 err = 0;
1671 if (!atomic_inc_not_zero(&num_events)) {
1672 mutex_lock(&pmc_reserve_mutex);
1673 if (atomic_read(&num_events) == 0 &&
1674 reserve_pmc_hardware(perf_event_interrupt))
1675 err = -EBUSY;
1676 else
1677 atomic_inc(&num_events);
1678 mutex_unlock(&pmc_reserve_mutex);
1679 }
1680 event->destroy = hw_perf_event_destroy;
1681
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001682 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683}
1684
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001685static int power_pmu_event_idx(struct perf_event *event)
1686{
1687 return event->hw.idx;
1688}
1689
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001690ssize_t power_events_sysfs_show(struct device *dev,
1691 struct device_attribute *attr, char *page)
1692{
1693 struct perf_pmu_events_attr *pmu_attr;
1694
1695 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1696
1697 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1698}
1699
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001700struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001701 .pmu_enable = power_pmu_enable,
1702 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001703 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001704 .add = power_pmu_add,
1705 .del = power_pmu_del,
1706 .start = power_pmu_start,
1707 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001708 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001709 .start_txn = power_pmu_start_txn,
1710 .cancel_txn = power_pmu_cancel_txn,
1711 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001712 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001713 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001714};
1715
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001717 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718 * things if requested. Note that interrupts are hard-disabled
1719 * here so there is no possibility of being interrupted.
1720 */
1721static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001722 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723{
1724 u64 period = event->hw.sample_period;
1725 s64 prev, delta, left;
1726 int record = 0;
1727
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001728 if (event->hw.state & PERF_HES_STOPPED) {
1729 write_pmc(event->hw.idx, 0);
1730 return;
1731 }
1732
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001734 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001735 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001736 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737
1738 /*
1739 * See if the total period for this event has expired,
1740 * and update for the next period.
1741 */
1742 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001743 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001744 if (delta == 0)
1745 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746 if (period) {
1747 if (left <= 0) {
1748 left += period;
1749 if (left <= 0)
1750 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001751 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001752 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 }
1754 if (left < 0x80000000LL)
1755 val = 0x80000000LL - left;
1756 }
1757
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001758 write_pmc(event->hw.idx, val);
1759 local64_set(&event->hw.prev_count, val);
1760 local64_set(&event->hw.period_left, left);
1761 perf_event_update_userpage(event);
1762
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763 /*
1764 * Finally record data if requested.
1765 */
1766 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001767 struct perf_sample_data data;
1768
Robert Richterfd0d0002012-04-02 20:19:08 +02001769 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770
1771 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1772 perf_get_data_addr(regs, &data.addr);
1773
Anshuman Khandual3925f462013-04-22 19:42:44 +00001774 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1775 struct cpu_hw_events *cpuhw;
1776 cpuhw = &__get_cpu_var(cpu_hw_events);
1777 power_pmu_bhrb_read(cpuhw);
1778 data.br_stack = &cpuhw->bhrb_stack;
1779 }
1780
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001781 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001782 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001783 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784}
1785
1786/*
1787 * Called from generic code to get the misc flags (i.e. processor mode)
1788 * for an event_id.
1789 */
1790unsigned long perf_misc_flags(struct pt_regs *regs)
1791{
1792 u32 flags = perf_get_misc_flags(regs);
1793
1794 if (flags)
1795 return flags;
1796 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1797 PERF_RECORD_MISC_KERNEL;
1798}
1799
1800/*
1801 * Called from generic code to get the instruction pointer
1802 * for an event_id.
1803 */
1804unsigned long perf_instruction_pointer(struct pt_regs *regs)
1805{
Michael Ellerman33904052013-04-25 19:28:25 +00001806 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001808 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00001809 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001810 else if (use_siar)
1811 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00001812 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00001813 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001814}
1815
Michael Neulingbc09c212012-11-05 15:53:54 +00001816static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11001817{
Anton Blanchard0837e322011-03-09 14:38:42 +11001818 /*
1819 * Events on POWER7 can roll back if a speculative event doesn't
1820 * eventually complete. Unfortunately in some rare cases they will
1821 * raise a performance monitor exception. We need to catch this to
1822 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1823 * cycles from overflow.
1824 *
1825 * We only do this if the first pass fails to find any overflowing
1826 * PMCs because a user might set a period of less than 256 and we
1827 * don't want to mistakenly reset them.
1828 */
Michael Neulingbc09c212012-11-05 15:53:54 +00001829 if ((0x80000000 - val) <= 256)
1830 return true;
1831
1832 return false;
1833}
1834
1835static bool pmc_overflow(unsigned long val)
1836{
1837 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11001838 return true;
1839
1840 return false;
1841}
1842
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843/*
1844 * Performance monitor interrupt stuff
1845 */
1846static void perf_event_interrupt(struct pt_regs *regs)
1847{
Michael Neulingbc09c212012-11-05 15:53:54 +00001848 int i, j;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1850 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00001851 unsigned long val[8];
1852 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853 int nmi;
1854
1855 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001856 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001857 mfspr(SPRN_PMC6));
1858
1859 perf_read_regs(regs);
1860
1861 nmi = perf_intr_is_nmi(regs);
1862 if (nmi)
1863 nmi_enter();
1864 else
1865 irq_enter();
1866
Michael Neulingbc09c212012-11-05 15:53:54 +00001867 /* Read all the PMCs since we'll need them a bunch of times */
1868 for (i = 0; i < ppmu->n_counter; ++i)
1869 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001870
Michael Neulingbc09c212012-11-05 15:53:54 +00001871 /* Try to find what caused the IRQ */
1872 found = 0;
1873 for (i = 0; i < ppmu->n_counter; ++i) {
1874 if (!pmc_overflow(val[i]))
1875 continue;
1876 if (is_limited_pmc(i + 1))
1877 continue; /* these won't generate IRQs */
1878 /*
1879 * We've found one that's overflowed. For active
1880 * counters we need to log this. For inactive
1881 * counters, we need to reset it anyway
1882 */
1883 found = 1;
1884 active = 0;
1885 for (j = 0; j < cpuhw->n_events; ++j) {
1886 event = cpuhw->event[j];
1887 if (event->hw.idx == (i + 1)) {
1888 active = 1;
1889 record_and_restart(event, val[i], regs);
1890 break;
1891 }
1892 }
1893 if (!active)
1894 /* reset non active counters that have overflowed */
1895 write_pmc(i + 1, 0);
1896 }
1897 if (!found && pvr_version_is(PVR_POWER7)) {
1898 /* check active counters for special buggy p7 overflow */
1899 for (i = 0; i < cpuhw->n_events; ++i) {
1900 event = cpuhw->event[i];
1901 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00001903 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
1904 /* event has overflowed in a buggy way*/
1905 found = 1;
1906 record_and_restart(event,
1907 val[event->hw.idx - 1],
1908 regs);
1909 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001910 }
1911 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00001912 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00001913 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001914
1915 /*
1916 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02001917 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918 * and thus allow interrupts to occur again.
1919 * XXX might want to use MSR.PM to keep the events frozen until
1920 * we get back out of this interrupt.
1921 */
1922 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1923
1924 if (nmi)
1925 nmi_exit();
1926 else
1927 irq_exit();
1928}
1929
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001930static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001931{
1932 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
1933
1934 if (!ppmu)
1935 return;
1936 memset(cpuhw, 0, sizeof(*cpuhw));
1937 cpuhw->mmcr[0] = MMCR0_FC;
1938}
1939
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001940static int
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01001941power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001942{
1943 unsigned int cpu = (long)hcpu;
1944
1945 switch (action & ~CPU_TASKS_FROZEN) {
1946 case CPU_UP_PREPARE:
1947 power_pmu_setup(cpu);
1948 break;
1949
1950 default:
1951 break;
1952 }
1953
1954 return NOTIFY_OK;
1955}
1956
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001957int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958{
1959 if (ppmu)
1960 return -EBUSY; /* something's already registered */
1961
1962 ppmu = pmu;
1963 pr_info("%s performance monitor hardware support registered\n",
1964 pmu->name);
1965
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001966 power_pmu.attr_groups = ppmu->attr_groups;
1967
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968#ifdef MSR_HV
1969 /*
1970 * Use FCHV to ignore kernel events if MSR.HV is set.
1971 */
1972 if (mfmsr() & MSR_HV)
1973 freeze_events_kernel = MMCR0_FCHV;
1974#endif /* CONFIG_PPC64 */
1975
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001976 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001977 perf_cpu_notifier(power_pmu_notifier);
1978
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001979 return 0;
1980}