blob: 5d502bf374eaa57ced3fa3505a3fd7dc1e6a59bf [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
Michael Ellerman378a6ee2013-06-28 18:15:11 +100078#define MMCR0_PMAO 0
Ingo Molnarcdd6c482009-09-21 12:02:48 +020079
80#define SPRN_MMCRA SPRN_MMCR2
81#define MMCRA_SAMPLE_ENABLE 0
82
83static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
84{
85 return 0;
86}
87static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
88static inline u32 perf_get_misc_flags(struct pt_regs *regs)
89{
90 return 0;
91}
Anton Blanchard75382aa2012-06-26 01:01:36 +000092static inline void perf_read_regs(struct pt_regs *regs)
93{
94 regs->result = 0;
95}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020096static inline int perf_intr_is_nmi(struct pt_regs *regs)
97{
98 return 0;
99}
100
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000101static inline int siar_valid(struct pt_regs *regs)
102{
103 return 1;
104}
105
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000106static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
107static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
108void power_pmu_flush_branch_stack(void) {}
109static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200110#endif /* CONFIG_PPC32 */
111
Michael Ellerman33904052013-04-25 19:28:25 +0000112static bool regs_use_siar(struct pt_regs *regs)
113{
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000114 return !!regs->result;
Michael Ellerman33904052013-04-25 19:28:25 +0000115}
116
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200117/*
118 * Things that are specific to 64-bit implementations.
119 */
120#ifdef CONFIG_PPC64
121
122static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
123{
124 unsigned long mmcra = regs->dsisr;
125
Michael Ellerman7a786832013-04-25 19:28:23 +0000126 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200127 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
128 if (slot > 1)
129 return 4 * (slot - 1);
130 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000131
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200132 return 0;
133}
134
135/*
136 * The user wants a data address recorded.
137 * If we're not doing instruction sampling, give them the SDAR
138 * (sampled data address). If we are doing instruction sampling, then
139 * only give them the SDAR if it corresponds to the instruction
Michael Ellerman58a032c2013-05-15 20:19:31 +0000140 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
141 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200142 */
143static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
144{
145 unsigned long mmcra = regs->dsisr;
Michael Ellerman58a032c2013-05-15 20:19:31 +0000146 bool sdar_valid;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000147
Michael Ellerman58a032c2013-05-15 20:19:31 +0000148 if (ppmu->flags & PPMU_HAS_SIER)
149 sdar_valid = regs->dar & SIER_SDAR_VALID;
150 else {
151 unsigned long sdsync;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152
Michael Ellerman58a032c2013-05-15 20:19:31 +0000153 if (ppmu->flags & PPMU_SIAR_VALID)
154 sdsync = POWER7P_MMCRA_SDAR_VALID;
155 else if (ppmu->flags & PPMU_ALT_SIPR)
156 sdsync = POWER6_MMCRA_SDSYNC;
157 else
158 sdsync = MMCRA_SDSYNC;
159
160 sdar_valid = mmcra & sdsync;
161 }
162
163 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164 *addrp = mfspr(SPRN_SDAR);
165}
166
Michael Ellerman5682c462013-04-25 19:28:24 +0000167static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000168{
169 unsigned long sihv = MMCRA_SIHV;
170
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000171 if (ppmu->flags & PPMU_HAS_SIER)
172 return !!(regs->dar & SIER_SIHV);
173
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000174 if (ppmu->flags & PPMU_ALT_SIPR)
175 sihv = POWER6_MMCRA_SIHV;
176
Michael Ellerman5682c462013-04-25 19:28:24 +0000177 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000178}
179
Michael Ellerman5682c462013-04-25 19:28:24 +0000180static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000181{
182 unsigned long sipr = MMCRA_SIPR;
183
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000184 if (ppmu->flags & PPMU_HAS_SIER)
185 return !!(regs->dar & SIER_SIPR);
186
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000187 if (ppmu->flags & PPMU_ALT_SIPR)
188 sipr = POWER6_MMCRA_SIPR;
189
Michael Ellerman5682c462013-04-25 19:28:24 +0000190 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000191}
192
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000193static inline u32 perf_flags_from_msr(struct pt_regs *regs)
194{
195 if (regs->msr & MSR_PR)
196 return PERF_RECORD_MISC_USER;
197 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
198 return PERF_RECORD_MISC_HYPERVISOR;
199 return PERF_RECORD_MISC_KERNEL;
200}
201
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200202static inline u32 perf_get_misc_flags(struct pt_regs *regs)
203{
Michael Ellerman33904052013-04-25 19:28:25 +0000204 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200205
Anton Blanchard75382aa2012-06-26 01:01:36 +0000206 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000207 return perf_flags_from_msr(regs);
208
209 /*
210 * If we don't have flags in MMCRA, rather than using
211 * the MSR, we intuit the flags from the address in
212 * SIAR which should give slightly more reliable
213 * results
214 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000215 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000216 unsigned long siar = mfspr(SPRN_SIAR);
217 if (siar >= PAGE_OFFSET)
218 return PERF_RECORD_MISC_KERNEL;
219 return PERF_RECORD_MISC_USER;
220 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200221
Michael Neuling7abb8402009-10-14 19:32:15 +0000222 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000223 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000224 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000225
226 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200227 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000228
Michael Neuling7abb8402009-10-14 19:32:15 +0000229 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200230}
231
232/*
233 * Overload regs->dsisr to store MMCRA so we only need to read it once
234 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000235 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000236 * Overload regs->result to specify whether we should use the MSR (result
237 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200238 */
239static inline void perf_read_regs(struct pt_regs *regs)
240{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000241 unsigned long mmcra = mfspr(SPRN_MMCRA);
242 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
243 int use_siar;
244
Michael Ellerman5682c462013-04-25 19:28:24 +0000245 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000246
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000247 if (ppmu->flags & PPMU_HAS_SIER)
248 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000249
250 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000251 * If this isn't a PMU exception (eg a software event) the SIAR is
252 * not valid. Use pt_regs.
253 *
254 * If it is a marked event use the SIAR.
255 *
256 * If the PMU doesn't update the SIAR for non marked events use
257 * pt_regs.
258 *
259 * If the PMU has HV/PR flags then check to see if they
260 * place the exception in userspace. If so, use pt_regs. In
261 * continuous sampling mode the SIAR and the PMU exception are
262 * not synchronised, so they may be many instructions apart.
263 * This can result in confusing backtraces. We still want
264 * hypervisor samples as well as samples in the kernel with
265 * interrupts off hence the userspace check.
266 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000267 if (TRAP(regs) != 0xf00)
268 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000269 else if (marked)
270 use_siar = 1;
271 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
272 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000273 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000274 use_siar = 0;
275 else
276 use_siar = 1;
277
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000278 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279}
280
281/*
282 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
283 * it as an NMI.
284 */
285static inline int perf_intr_is_nmi(struct pt_regs *regs)
286{
287 return !regs->softe;
288}
289
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000290/*
291 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
292 * must be sampled only if the SIAR-valid bit is set.
293 *
294 * For unmarked instructions and for processors that don't have the SIAR-Valid
295 * bit, assume that SIAR is valid.
296 */
297static inline int siar_valid(struct pt_regs *regs)
298{
299 unsigned long mmcra = regs->dsisr;
300 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
301
Michael Ellerman58a032c2013-05-15 20:19:31 +0000302 if (marked) {
303 if (ppmu->flags & PPMU_HAS_SIER)
304 return regs->dar & SIER_SIAR_VALID;
305
306 if (ppmu->flags & PPMU_SIAR_VALID)
307 return mmcra & POWER7P_MMCRA_SIAR_VALID;
308 }
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000309
310 return 1;
311}
312
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000313
314/* Reset all possible BHRB entries */
315static void power_pmu_bhrb_reset(void)
316{
317 asm volatile(PPC_CLRBHRB);
318}
319
320static void power_pmu_bhrb_enable(struct perf_event *event)
321{
322 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
323
324 if (!ppmu->bhrb_nr)
325 return;
326
327 /* Clear BHRB if we changed task context to avoid data leaks */
328 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
329 power_pmu_bhrb_reset();
330 cpuhw->bhrb_context = event->ctx;
331 }
332 cpuhw->bhrb_users++;
333}
334
335static void power_pmu_bhrb_disable(struct perf_event *event)
336{
337 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
338
339 if (!ppmu->bhrb_nr)
340 return;
341
342 cpuhw->bhrb_users--;
343 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
344
345 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
346 /* BHRB cannot be turned off when other
347 * events are active on the PMU.
348 */
349
350 /* avoid stale pointer */
351 cpuhw->bhrb_context = NULL;
352 }
353}
354
355/* Called from ctxsw to prevent one process's branch entries to
356 * mingle with the other process's entries during context switch.
357 */
358void power_pmu_flush_branch_stack(void)
359{
360 if (ppmu->bhrb_nr)
361 power_pmu_bhrb_reset();
362}
Michael Neuling69123182013-05-13 18:44:58 +0000363/* Calculate the to address for a branch */
364static __u64 power_pmu_bhrb_to(u64 addr)
365{
366 unsigned int instr;
367 int ret;
368 __u64 target;
369
370 if (is_kernel_addr(addr))
371 return branch_target((unsigned int *)addr);
372
373 /* Userspace: need copy instruction here then translate it */
374 pagefault_disable();
375 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
376 if (ret) {
377 pagefault_enable();
378 return 0;
379 }
380 pagefault_enable();
381
382 target = branch_target(&instr);
383 if ((!target) || (instr & BRANCH_ABSOLUTE))
384 return target;
385
386 /* Translate relative branch target from kernel to user address */
387 return target - (unsigned long)&instr + addr;
388}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000389
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000390/* Processing BHRB entries */
Michael Neuling506e70d2013-05-13 18:44:57 +0000391void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000392{
393 u64 val;
394 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000395 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000396
397 r_index = 0;
398 u_index = 0;
399 while (r_index < ppmu->bhrb_nr) {
400 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000401 val = read_bhrb(r_index++);
402 if (!val)
403 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000404 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000405 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000406 addr = val & BHRB_EA;
407 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000408
Michael Neuling506e70d2013-05-13 18:44:57 +0000409 if (!addr)
410 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000411 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000412
Michael Neuling506e70d2013-05-13 18:44:57 +0000413 /* Branches are read most recent first (ie. mfbhrb 0 is
414 * the most recent branch).
415 * There are two types of valid entries:
416 * 1) a target entry which is the to address of a
417 * computed goto like a blr,bctr,btar. The next
418 * entry read from the bhrb will be branch
419 * corresponding to this target (ie. the actual
420 * blr/bctr/btar instruction).
421 * 2) a from address which is an actual branch. If a
422 * target entry proceeds this, then this is the
423 * matching branch for that target. If this is not
424 * following a target entry, then this is a branch
425 * where the target is given as an immediate field
426 * in the instruction (ie. an i or b form branch).
427 * In this case we need to read the instruction from
428 * memory to determine the target/to address.
429 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000430
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000431 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000432 /* Target branches use two entries
433 * (ie. computed gotos/XL form)
434 */
435 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000436 cpuhw->bhrb_entries[u_index].mispred = pred;
437 cpuhw->bhrb_entries[u_index].predicted = ~pred;
438
Michael Neuling506e70d2013-05-13 18:44:57 +0000439 /* Get from address in next entry */
440 val = read_bhrb(r_index++);
441 addr = val & BHRB_EA;
442 if (val & BHRB_TARGET) {
443 /* Shouldn't have two targets in a
444 row.. Reset index and try again */
445 r_index--;
446 addr = 0;
447 }
448 cpuhw->bhrb_entries[u_index].from = addr;
449 } else {
450 /* Branches to immediate field
451 (ie I or B form) */
452 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000453 cpuhw->bhrb_entries[u_index].to =
454 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000455 cpuhw->bhrb_entries[u_index].mispred = pred;
456 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000457 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000458 u_index++;
459
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000460 }
461 }
462 cpuhw->bhrb_stack.nr = u_index;
463 return;
464}
465
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200466#endif /* CONFIG_PPC64 */
467
468static void perf_event_interrupt(struct pt_regs *regs);
469
470void perf_event_print_debug(void)
471{
472}
473
474/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200475 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200476 */
477static unsigned long read_pmc(int idx)
478{
479 unsigned long val;
480
481 switch (idx) {
482 case 1:
483 val = mfspr(SPRN_PMC1);
484 break;
485 case 2:
486 val = mfspr(SPRN_PMC2);
487 break;
488 case 3:
489 val = mfspr(SPRN_PMC3);
490 break;
491 case 4:
492 val = mfspr(SPRN_PMC4);
493 break;
494 case 5:
495 val = mfspr(SPRN_PMC5);
496 break;
497 case 6:
498 val = mfspr(SPRN_PMC6);
499 break;
500#ifdef CONFIG_PPC64
501 case 7:
502 val = mfspr(SPRN_PMC7);
503 break;
504 case 8:
505 val = mfspr(SPRN_PMC8);
506 break;
507#endif /* CONFIG_PPC64 */
508 default:
509 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
510 val = 0;
511 }
512 return val;
513}
514
515/*
516 * Write one PMC.
517 */
518static void write_pmc(int idx, unsigned long val)
519{
520 switch (idx) {
521 case 1:
522 mtspr(SPRN_PMC1, val);
523 break;
524 case 2:
525 mtspr(SPRN_PMC2, val);
526 break;
527 case 3:
528 mtspr(SPRN_PMC3, val);
529 break;
530 case 4:
531 mtspr(SPRN_PMC4, val);
532 break;
533 case 5:
534 mtspr(SPRN_PMC5, val);
535 break;
536 case 6:
537 mtspr(SPRN_PMC6, val);
538 break;
539#ifdef CONFIG_PPC64
540 case 7:
541 mtspr(SPRN_PMC7, val);
542 break;
543 case 8:
544 mtspr(SPRN_PMC8, val);
545 break;
546#endif /* CONFIG_PPC64 */
547 default:
548 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
549 }
550}
551
552/*
553 * Check if a set of events can all go on the PMU at once.
554 * If they can't, this will look at alternative codes for the events
555 * and see if any combination of alternative codes is feasible.
556 * The feasible set is returned in event_id[].
557 */
558static int power_check_constraints(struct cpu_hw_events *cpuhw,
559 u64 event_id[], unsigned int cflags[],
560 int n_ev)
561{
562 unsigned long mask, value, nv;
563 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
564 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
565 int i, j;
566 unsigned long addf = ppmu->add_fields;
567 unsigned long tadd = ppmu->test_adder;
568
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000569 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570 return -1;
571
572 /* First see if the events will go on as-is */
573 for (i = 0; i < n_ev; ++i) {
574 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
575 && !ppmu->limited_pmc_event(event_id[i])) {
576 ppmu->get_alternatives(event_id[i], cflags[i],
577 cpuhw->alternatives[i]);
578 event_id[i] = cpuhw->alternatives[i][0];
579 }
580 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
581 &cpuhw->avalues[i][0]))
582 return -1;
583 }
584 value = mask = 0;
585 for (i = 0; i < n_ev; ++i) {
586 nv = (value | cpuhw->avalues[i][0]) +
587 (value & cpuhw->avalues[i][0] & addf);
588 if ((((nv + tadd) ^ value) & mask) != 0 ||
589 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
590 cpuhw->amasks[i][0]) != 0)
591 break;
592 value = nv;
593 mask |= cpuhw->amasks[i][0];
594 }
595 if (i == n_ev)
596 return 0; /* all OK */
597
598 /* doesn't work, gather alternatives... */
599 if (!ppmu->get_alternatives)
600 return -1;
601 for (i = 0; i < n_ev; ++i) {
602 choice[i] = 0;
603 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
604 cpuhw->alternatives[i]);
605 for (j = 1; j < n_alt[i]; ++j)
606 ppmu->get_constraint(cpuhw->alternatives[i][j],
607 &cpuhw->amasks[i][j],
608 &cpuhw->avalues[i][j]);
609 }
610
611 /* enumerate all possibilities and see if any will work */
612 i = 0;
613 j = -1;
614 value = mask = nv = 0;
615 while (i < n_ev) {
616 if (j >= 0) {
617 /* we're backtracking, restore context */
618 value = svalues[i];
619 mask = smasks[i];
620 j = choice[i];
621 }
622 /*
623 * See if any alternative k for event_id i,
624 * where k > j, will satisfy the constraints.
625 */
626 while (++j < n_alt[i]) {
627 nv = (value | cpuhw->avalues[i][j]) +
628 (value & cpuhw->avalues[i][j] & addf);
629 if ((((nv + tadd) ^ value) & mask) == 0 &&
630 (((nv + tadd) ^ cpuhw->avalues[i][j])
631 & cpuhw->amasks[i][j]) == 0)
632 break;
633 }
634 if (j >= n_alt[i]) {
635 /*
636 * No feasible alternative, backtrack
637 * to event_id i-1 and continue enumerating its
638 * alternatives from where we got up to.
639 */
640 if (--i < 0)
641 return -1;
642 } else {
643 /*
644 * Found a feasible alternative for event_id i,
645 * remember where we got up to with this event_id,
646 * go on to the next event_id, and start with
647 * the first alternative for it.
648 */
649 choice[i] = j;
650 svalues[i] = value;
651 smasks[i] = mask;
652 value = nv;
653 mask |= cpuhw->amasks[i][j];
654 ++i;
655 j = -1;
656 }
657 }
658
659 /* OK, we have a feasible combination, tell the caller the solution */
660 for (i = 0; i < n_ev; ++i)
661 event_id[i] = cpuhw->alternatives[i][choice[i]];
662 return 0;
663}
664
665/*
666 * Check if newly-added events have consistent settings for
667 * exclude_{user,kernel,hv} with each other and any previously
668 * added events.
669 */
670static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
671 int n_prev, int n_new)
672{
673 int eu = 0, ek = 0, eh = 0;
674 int i, n, first;
675 struct perf_event *event;
676
677 n = n_prev + n_new;
678 if (n <= 1)
679 return 0;
680
681 first = 1;
682 for (i = 0; i < n; ++i) {
683 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
684 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
685 continue;
686 }
687 event = ctrs[i];
688 if (first) {
689 eu = event->attr.exclude_user;
690 ek = event->attr.exclude_kernel;
691 eh = event->attr.exclude_hv;
692 first = 0;
693 } else if (event->attr.exclude_user != eu ||
694 event->attr.exclude_kernel != ek ||
695 event->attr.exclude_hv != eh) {
696 return -EAGAIN;
697 }
698 }
699
700 if (eu || ek || eh)
701 for (i = 0; i < n; ++i)
702 if (cflags[i] & PPMU_LIMITED_PMC_OK)
703 cflags[i] |= PPMU_LIMITED_PMC_REQD;
704
705 return 0;
706}
707
Eric B Munson86c74ab2011-04-15 08:12:30 +0000708static u64 check_and_compute_delta(u64 prev, u64 val)
709{
710 u64 delta = (val - prev) & 0xfffffffful;
711
712 /*
713 * POWER7 can roll back counter values, if the new value is smaller
714 * than the previous value it will cause the delta and the counter to
715 * have bogus values unless we rolled a counter over. If a coutner is
716 * rolled back, it will be smaller, but within 256, which is the maximum
717 * number of events to rollback at once. If we dectect a rollback
718 * return 0. This can lead to a small lack of precision in the
719 * counters.
720 */
721 if (prev > val && (prev - val) < 256)
722 delta = 0;
723
724 return delta;
725}
726
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200727static void power_pmu_read(struct perf_event *event)
728{
729 s64 val, delta, prev;
730
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200731 if (event->hw.state & PERF_HES_STOPPED)
732 return;
733
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200734 if (!event->hw.idx)
735 return;
736 /*
737 * Performance monitor interrupts come even when interrupts
738 * are soft-disabled, as long as interrupts are hard-enabled.
739 * Therefore we treat them like NMIs.
740 */
741 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +0200742 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200743 barrier();
744 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +0000745 delta = check_and_compute_delta(prev, val);
746 if (!delta)
747 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200748 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200749
Peter Zijlstrae7850592010-05-21 14:43:08 +0200750 local64_add(delta, &event->count);
751 local64_sub(delta, &event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200752}
753
754/*
755 * On some machines, PMC5 and PMC6 can't be written, don't respect
756 * the freeze conditions, and don't generate interrupts. This tells
757 * us if `event' is using such a PMC.
758 */
759static int is_limited_pmc(int pmcnum)
760{
761 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
762 && (pmcnum == 5 || pmcnum == 6);
763}
764
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000765static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200766 unsigned long pmc5, unsigned long pmc6)
767{
768 struct perf_event *event;
769 u64 val, prev, delta;
770 int i;
771
772 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000773 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200774 if (!event->hw.idx)
775 continue;
776 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200777 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200778 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000779 delta = check_and_compute_delta(prev, val);
780 if (delta)
781 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200782 }
783}
784
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000785static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200786 unsigned long pmc5, unsigned long pmc6)
787{
788 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000789 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200790 int i;
791
792 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000793 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200794 event->hw.idx = cpuhw->limited_hwidx[i];
795 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000796 prev = local64_read(&event->hw.prev_count);
797 if (check_and_compute_delta(prev, val))
798 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200799 perf_event_update_userpage(event);
800 }
801}
802
803/*
804 * Since limited events don't respect the freeze conditions, we
805 * have to read them immediately after freezing or unfreezing the
806 * other events. We try to keep the values from the limited
807 * events as consistent as possible by keeping the delay (in
808 * cycles and instructions) between freezing/unfreezing and reading
809 * the limited events as small and consistent as possible.
810 * Therefore, if any limited events are in use, we read them
811 * both, and always in the same order, to minimize variability,
812 * and do it inside the same asm that writes MMCR0.
813 */
814static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
815{
816 unsigned long pmc5, pmc6;
817
818 if (!cpuhw->n_limited) {
819 mtspr(SPRN_MMCR0, mmcr0);
820 return;
821 }
822
823 /*
824 * Write MMCR0, then read PMC5 and PMC6 immediately.
825 * To ensure we don't get a performance monitor interrupt
826 * between writing MMCR0 and freezing/thawing the limited
827 * events, we first write MMCR0 with the event overflow
828 * interrupt enable bits turned off.
829 */
830 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
831 : "=&r" (pmc5), "=&r" (pmc6)
832 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
833 "i" (SPRN_MMCR0),
834 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
835
836 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000837 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200838 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000839 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200840
841 /*
842 * Write the full MMCR0 including the event overflow interrupt
843 * enable bits, if necessary.
844 */
845 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
846 mtspr(SPRN_MMCR0, mmcr0);
847}
848
849/*
850 * Disable all events to prevent PMU interrupts and to allow
851 * events to be added or removed.
852 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200853static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200854{
855 struct cpu_hw_events *cpuhw;
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000856 unsigned long flags, val;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200857
858 if (!ppmu)
859 return;
860 local_irq_save(flags);
861 cpuhw = &__get_cpu_var(cpu_hw_events);
862
863 if (!cpuhw->disabled) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200864 /*
865 * Check if we ever enabled the PMU on this cpu.
866 */
867 if (!cpuhw->pmcs_enabled) {
868 ppc_enable_pmcs();
869 cpuhw->pmcs_enabled = 1;
870 }
871
872 /*
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000873 * Set the 'freeze counters' bit, clear PMAO.
874 */
875 val = mfspr(SPRN_MMCR0);
876 val |= MMCR0_FC;
877 val &= ~MMCR0_PMAO;
878
879 /*
880 * The barrier is to make sure the mtspr has been
881 * executed and the PMU has frozen the events etc.
882 * before we return.
883 */
884 write_mmcr0(cpuhw, val);
885 mb();
886
887 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200888 * Disable instruction sampling if it was enabled
889 */
890 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
891 mtspr(SPRN_MMCRA,
892 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
893 mb();
894 }
895
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000896 cpuhw->disabled = 1;
897 cpuhw->n_added = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200898 }
899 local_irq_restore(flags);
900}
901
902/*
903 * Re-enable all events if disable == 0.
904 * If we were previously disabled and events were added, then
905 * put the new config on the PMU.
906 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200907static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908{
909 struct perf_event *event;
910 struct cpu_hw_events *cpuhw;
911 unsigned long flags;
912 long i;
913 unsigned long val;
914 s64 left;
915 unsigned int hwc_index[MAX_HWEVENTS];
916 int n_lim;
917 int idx;
918
919 if (!ppmu)
920 return;
921 local_irq_save(flags);
922 cpuhw = &__get_cpu_var(cpu_hw_events);
923 if (!cpuhw->disabled) {
924 local_irq_restore(flags);
925 return;
926 }
927 cpuhw->disabled = 0;
928
929 /*
930 * If we didn't change anything, or only removed events,
931 * no need to recalculate MMCR* settings and reset the PMCs.
932 * Just reenable the PMU with the current MMCR* settings
933 * (possibly updated for removal of events).
934 */
935 if (!cpuhw->n_added) {
936 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
937 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
938 if (cpuhw->n_events == 0)
939 ppc_set_pmu_inuse(0);
940 goto out_enable;
941 }
942
943 /*
944 * Compute MMCR* values for the new set of events
945 */
946 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
947 cpuhw->mmcr)) {
948 /* shouldn't ever get here */
949 printk(KERN_ERR "oops compute_mmcr failed\n");
950 goto out;
951 }
952
953 /*
954 * Add in MMCR0 freeze bits corresponding to the
955 * attr.exclude_* bits for the first event.
956 * We have already checked that all events have the
957 * same values for these bits as the first event.
958 */
959 event = cpuhw->event[0];
960 if (event->attr.exclude_user)
961 cpuhw->mmcr[0] |= MMCR0_FCP;
962 if (event->attr.exclude_kernel)
963 cpuhw->mmcr[0] |= freeze_events_kernel;
964 if (event->attr.exclude_hv)
965 cpuhw->mmcr[0] |= MMCR0_FCHV;
966
967 /*
968 * Write the new configuration to MMCR* with the freeze
969 * bit set and set the hardware events to their initial values.
970 * Then unfreeze the events.
971 */
972 ppc_set_pmu_inuse(1);
973 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
974 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
975 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
976 | MMCR0_FC);
977
978 /*
979 * Read off any pre-existing events that need to move
980 * to another PMC.
981 */
982 for (i = 0; i < cpuhw->n_events; ++i) {
983 event = cpuhw->event[i];
984 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
985 power_pmu_read(event);
986 write_pmc(event->hw.idx, 0);
987 event->hw.idx = 0;
988 }
989 }
990
991 /*
992 * Initialize the PMCs for all the new and moved events.
993 */
994 cpuhw->n_limited = n_lim = 0;
995 for (i = 0; i < cpuhw->n_events; ++i) {
996 event = cpuhw->event[i];
997 if (event->hw.idx)
998 continue;
999 idx = hwc_index[i] + 1;
1000 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001001 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001002 cpuhw->limited_hwidx[n_lim] = idx;
1003 ++n_lim;
1004 continue;
1005 }
1006 val = 0;
1007 if (event->hw.sample_period) {
Peter Zijlstrae7850592010-05-21 14:43:08 +02001008 left = local64_read(&event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009 if (left < 0x80000000L)
1010 val = 0x80000000L - left;
1011 }
Peter Zijlstrae7850592010-05-21 14:43:08 +02001012 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001014 if (event->hw.state & PERF_HES_STOPPED)
1015 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001016 write_pmc(idx, val);
1017 perf_event_update_userpage(event);
1018 }
1019 cpuhw->n_limited = n_lim;
1020 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1021
1022 out_enable:
1023 mb();
1024 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1025
1026 /*
1027 * Enable instruction sampling if necessary
1028 */
1029 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1030 mb();
1031 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1032 }
1033
1034 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001035 if (cpuhw->bhrb_users)
1036 ppmu->config_bhrb(cpuhw->bhrb_filter);
1037
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038 local_irq_restore(flags);
1039}
1040
1041static int collect_events(struct perf_event *group, int max_count,
1042 struct perf_event *ctrs[], u64 *events,
1043 unsigned int *flags)
1044{
1045 int n = 0;
1046 struct perf_event *event;
1047
1048 if (!is_software_event(group)) {
1049 if (n >= max_count)
1050 return -1;
1051 ctrs[n] = group;
1052 flags[n] = group->hw.event_base;
1053 events[n++] = group->hw.config;
1054 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001055 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001056 if (!is_software_event(event) &&
1057 event->state != PERF_EVENT_STATE_OFF) {
1058 if (n >= max_count)
1059 return -1;
1060 ctrs[n] = event;
1061 flags[n] = event->hw.event_base;
1062 events[n++] = event->hw.config;
1063 }
1064 }
1065 return n;
1066}
1067
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001068/*
1069 * Add a event to the PMU.
1070 * If all events are not already frozen, then we disable and
1071 * re-enable the PMU in order to get hw_perf_enable to do the
1072 * actual work of reconfiguring the PMU.
1073 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001074static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001075{
1076 struct cpu_hw_events *cpuhw;
1077 unsigned long flags;
1078 int n0;
1079 int ret = -EAGAIN;
1080
1081 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001082 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001083
1084 /*
1085 * Add the event to the list (if there is room)
1086 * and check whether the total set is still feasible.
1087 */
1088 cpuhw = &__get_cpu_var(cpu_hw_events);
1089 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001090 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091 goto out;
1092 cpuhw->event[n0] = event;
1093 cpuhw->events[n0] = event->hw.config;
1094 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001095
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001096 /*
1097 * This event may have been disabled/stopped in record_and_restart()
1098 * because we exceeded the ->event_limit. If re-starting the event,
1099 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1100 * notification is re-enabled.
1101 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001102 if (!(ef_flags & PERF_EF_START))
1103 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001104 else
1105 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001106
Lin Ming8e6d5572010-05-08 20:28:41 +10001107 /*
1108 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001109 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001110 * at commit time(->commit_txn) as a whole
1111 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001112 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001113 goto nocheck;
1114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001115 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1116 goto out;
1117 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1118 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001120
1121nocheck:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001122 ++cpuhw->n_events;
1123 ++cpuhw->n_added;
1124
1125 ret = 0;
1126 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001127 if (has_branch_stack(event))
1128 power_pmu_bhrb_enable(event);
1129
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001130 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001131 local_irq_restore(flags);
1132 return ret;
1133}
1134
1135/*
1136 * Remove a event from the PMU.
1137 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001138static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139{
1140 struct cpu_hw_events *cpuhw;
1141 long i;
1142 unsigned long flags;
1143
1144 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001145 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001146
1147 power_pmu_read(event);
1148
1149 cpuhw = &__get_cpu_var(cpu_hw_events);
1150 for (i = 0; i < cpuhw->n_events; ++i) {
1151 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001152 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001153 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001154 cpuhw->events[i-1] = cpuhw->events[i];
1155 cpuhw->flags[i-1] = cpuhw->flags[i];
1156 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001157 --cpuhw->n_events;
1158 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1159 if (event->hw.idx) {
1160 write_pmc(event->hw.idx, 0);
1161 event->hw.idx = 0;
1162 }
1163 perf_event_update_userpage(event);
1164 break;
1165 }
1166 }
1167 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001168 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169 break;
1170 if (i < cpuhw->n_limited) {
1171 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001172 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1174 }
1175 --cpuhw->n_limited;
1176 }
1177 if (cpuhw->n_events == 0) {
1178 /* disable exceptions if no events are running */
1179 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1180 }
1181
Anshuman Khandual3925f462013-04-22 19:42:44 +00001182 if (has_branch_stack(event))
1183 power_pmu_bhrb_disable(event);
1184
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001185 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001186 local_irq_restore(flags);
1187}
1188
1189/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001190 * POWER-PMU does not support disabling individual counters, hence
1191 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001192 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001193
1194static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001196 unsigned long flags;
1197 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001198 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001199
1200 if (!event->hw.idx || !event->hw.sample_period)
1201 return;
1202
1203 if (!(event->hw.state & PERF_HES_STOPPED))
1204 return;
1205
1206 if (ef_flags & PERF_EF_RELOAD)
1207 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1208
1209 local_irq_save(flags);
1210 perf_pmu_disable(event->pmu);
1211
1212 event->hw.state = 0;
1213 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001214
1215 val = 0;
1216 if (left < 0x80000000L)
1217 val = 0x80000000L - left;
1218
1219 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001220
1221 perf_event_update_userpage(event);
1222 perf_pmu_enable(event->pmu);
1223 local_irq_restore(flags);
1224}
1225
1226static void power_pmu_stop(struct perf_event *event, int ef_flags)
1227{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 unsigned long flags;
1229
1230 if (!event->hw.idx || !event->hw.sample_period)
1231 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001232
1233 if (event->hw.state & PERF_HES_STOPPED)
1234 return;
1235
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001236 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001237 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001238
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001239 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001240 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1241 write_pmc(event->hw.idx, 0);
1242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001243 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001244 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001245 local_irq_restore(flags);
1246}
1247
Lin Ming8e6d5572010-05-08 20:28:41 +10001248/*
1249 * Start group events scheduling transaction
1250 * Set the flag to make pmu::enable() not perform the
1251 * schedulability test, it will be performed at commit time
1252 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001253void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001254{
1255 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1256
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001257 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001258 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001259 cpuhw->n_txn_start = cpuhw->n_events;
1260}
1261
1262/*
1263 * Stop group events scheduling transaction
1264 * Clear the flag and pmu::enable() will perform the
1265 * schedulability test.
1266 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001267void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001268{
1269 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1270
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001271 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001272 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001273}
1274
1275/*
1276 * Commit group events scheduling transaction
1277 * Perform the group schedulability test as a whole
1278 * Return 0 if success
1279 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001280int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001281{
1282 struct cpu_hw_events *cpuhw;
1283 long i, n;
1284
1285 if (!ppmu)
1286 return -EAGAIN;
1287 cpuhw = &__get_cpu_var(cpu_hw_events);
1288 n = cpuhw->n_events;
1289 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1290 return -EAGAIN;
1291 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1292 if (i < 0)
1293 return -EAGAIN;
1294
1295 for (i = cpuhw->n_txn_start; i < n; ++i)
1296 cpuhw->event[i]->hw.config = cpuhw->events[i];
1297
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001298 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001299 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001300 return 0;
1301}
1302
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001303/*
1304 * Return 1 if we might be able to put event on a limited PMC,
1305 * or 0 if not.
1306 * A event can only go on a limited PMC if it counts something
1307 * that a limited PMC can count, doesn't require interrupts, and
1308 * doesn't exclude any processor mode.
1309 */
1310static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1311 unsigned int flags)
1312{
1313 int n;
1314 u64 alt[MAX_EVENT_ALTERNATIVES];
1315
1316 if (event->attr.exclude_user
1317 || event->attr.exclude_kernel
1318 || event->attr.exclude_hv
1319 || event->attr.sample_period)
1320 return 0;
1321
1322 if (ppmu->limited_pmc_event(ev))
1323 return 1;
1324
1325 /*
1326 * The requested event_id isn't on a limited PMC already;
1327 * see if any alternative code goes on a limited PMC.
1328 */
1329 if (!ppmu->get_alternatives)
1330 return 0;
1331
1332 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1333 n = ppmu->get_alternatives(ev, flags, alt);
1334
1335 return n > 0;
1336}
1337
1338/*
1339 * Find an alternative event_id that goes on a normal PMC, if possible,
1340 * and return the event_id code, or 0 if there is no such alternative.
1341 * (Note: event_id code 0 is "don't count" on all machines.)
1342 */
1343static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1344{
1345 u64 alt[MAX_EVENT_ALTERNATIVES];
1346 int n;
1347
1348 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1349 n = ppmu->get_alternatives(ev, flags, alt);
1350 if (!n)
1351 return 0;
1352 return alt[0];
1353}
1354
1355/* Number of perf_events counting hardware events */
1356static atomic_t num_events;
1357/* Used to avoid races in calling reserve/release_pmc_hardware */
1358static DEFINE_MUTEX(pmc_reserve_mutex);
1359
1360/*
1361 * Release the PMU if this is the last perf_event.
1362 */
1363static void hw_perf_event_destroy(struct perf_event *event)
1364{
1365 if (!atomic_add_unless(&num_events, -1, 1)) {
1366 mutex_lock(&pmc_reserve_mutex);
1367 if (atomic_dec_return(&num_events) == 0)
1368 release_pmc_hardware();
1369 mutex_unlock(&pmc_reserve_mutex);
1370 }
1371}
1372
1373/*
1374 * Translate a generic cache event_id config to a raw event_id code.
1375 */
1376static int hw_perf_cache_event(u64 config, u64 *eventp)
1377{
1378 unsigned long type, op, result;
1379 int ev;
1380
1381 if (!ppmu->cache_events)
1382 return -EINVAL;
1383
1384 /* unpack config */
1385 type = config & 0xff;
1386 op = (config >> 8) & 0xff;
1387 result = (config >> 16) & 0xff;
1388
1389 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1390 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1391 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1392 return -EINVAL;
1393
1394 ev = (*ppmu->cache_events)[type][op][result];
1395 if (ev == 0)
1396 return -EOPNOTSUPP;
1397 if (ev == -1)
1398 return -EINVAL;
1399 *eventp = ev;
1400 return 0;
1401}
1402
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001403static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001404{
1405 u64 ev;
1406 unsigned long flags;
1407 struct perf_event *ctrs[MAX_HWEVENTS];
1408 u64 events[MAX_HWEVENTS];
1409 unsigned int cflags[MAX_HWEVENTS];
1410 int n;
1411 int err;
1412 struct cpu_hw_events *cpuhw;
1413
1414 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001415 return -ENOENT;
1416
Anshuman Khandual3925f462013-04-22 19:42:44 +00001417 if (has_branch_stack(event)) {
1418 /* PMU has BHRB enabled */
1419 if (!(ppmu->flags & PPMU_BHRB))
1420 return -EOPNOTSUPP;
1421 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001422
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423 switch (event->attr.type) {
1424 case PERF_TYPE_HARDWARE:
1425 ev = event->attr.config;
1426 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001427 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428 ev = ppmu->generic_events[ev];
1429 break;
1430 case PERF_TYPE_HW_CACHE:
1431 err = hw_perf_cache_event(event->attr.config, &ev);
1432 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001433 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001434 break;
1435 case PERF_TYPE_RAW:
1436 ev = event->attr.config;
1437 break;
1438 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001439 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001440 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442 event->hw.config_base = ev;
1443 event->hw.idx = 0;
1444
1445 /*
1446 * If we are not running on a hypervisor, force the
1447 * exclude_hv bit to 0 so that we don't care what
1448 * the user set it to.
1449 */
1450 if (!firmware_has_feature(FW_FEATURE_LPAR))
1451 event->attr.exclude_hv = 0;
1452
1453 /*
1454 * If this is a per-task event, then we can use
1455 * PM_RUN_* events interchangeably with their non RUN_*
1456 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1457 * XXX we should check if the task is an idle task.
1458 */
1459 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001460 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001461 flags |= PPMU_ONLY_COUNT_RUN;
1462
1463 /*
1464 * If this machine has limited events, check whether this
1465 * event_id could go on a limited event.
1466 */
1467 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1468 if (can_go_on_limited_pmc(event, ev, flags)) {
1469 flags |= PPMU_LIMITED_PMC_OK;
1470 } else if (ppmu->limited_pmc_event(ev)) {
1471 /*
1472 * The requested event_id is on a limited PMC,
1473 * but we can't use a limited PMC; see if any
1474 * alternative goes on a normal PMC.
1475 */
1476 ev = normal_pmc_alternative(ev, flags);
1477 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001478 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001479 }
1480 }
1481
1482 /*
1483 * If this is in a group, check if it can go on with all the
1484 * other hardware events in the group. We assume the event
1485 * hasn't been linked into its leader's sibling list at this point.
1486 */
1487 n = 0;
1488 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001489 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001490 ctrs, events, cflags);
1491 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001492 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001493 }
1494 events[n] = ev;
1495 ctrs[n] = event;
1496 cflags[n] = flags;
1497 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001498 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001499
1500 cpuhw = &get_cpu_var(cpu_hw_events);
1501 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001502
1503 if (has_branch_stack(event)) {
1504 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1505 event->attr.branch_sample_type);
1506
1507 if(cpuhw->bhrb_filter == -1)
1508 return -EOPNOTSUPP;
1509 }
1510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511 put_cpu_var(cpu_hw_events);
1512 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001513 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001514
1515 event->hw.config = events[n];
1516 event->hw.event_base = cflags[n];
1517 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001518 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519
1520 /*
1521 * See if we need to reserve the PMU.
1522 * If no events are currently in use, then we have to take a
1523 * mutex to ensure that we don't race with another task doing
1524 * reserve_pmc_hardware or release_pmc_hardware.
1525 */
1526 err = 0;
1527 if (!atomic_inc_not_zero(&num_events)) {
1528 mutex_lock(&pmc_reserve_mutex);
1529 if (atomic_read(&num_events) == 0 &&
1530 reserve_pmc_hardware(perf_event_interrupt))
1531 err = -EBUSY;
1532 else
1533 atomic_inc(&num_events);
1534 mutex_unlock(&pmc_reserve_mutex);
1535 }
1536 event->destroy = hw_perf_event_destroy;
1537
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001538 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539}
1540
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001541static int power_pmu_event_idx(struct perf_event *event)
1542{
1543 return event->hw.idx;
1544}
1545
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001546ssize_t power_events_sysfs_show(struct device *dev,
1547 struct device_attribute *attr, char *page)
1548{
1549 struct perf_pmu_events_attr *pmu_attr;
1550
1551 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1552
1553 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1554}
1555
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001556struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001557 .pmu_enable = power_pmu_enable,
1558 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001559 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001560 .add = power_pmu_add,
1561 .del = power_pmu_del,
1562 .start = power_pmu_start,
1563 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001564 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001565 .start_txn = power_pmu_start_txn,
1566 .cancel_txn = power_pmu_cancel_txn,
1567 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001568 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001569 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001570};
1571
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001573 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 * things if requested. Note that interrupts are hard-disabled
1575 * here so there is no possibility of being interrupted.
1576 */
1577static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001578 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001579{
1580 u64 period = event->hw.sample_period;
1581 s64 prev, delta, left;
1582 int record = 0;
1583
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001584 if (event->hw.state & PERF_HES_STOPPED) {
1585 write_pmc(event->hw.idx, 0);
1586 return;
1587 }
1588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001590 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001591 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001592 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593
1594 /*
1595 * See if the total period for this event has expired,
1596 * and update for the next period.
1597 */
1598 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001599 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001600 if (delta == 0)
1601 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001602 if (period) {
1603 if (left <= 0) {
1604 left += period;
1605 if (left <= 0)
1606 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001607 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001608 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609 }
1610 if (left < 0x80000000LL)
1611 val = 0x80000000LL - left;
1612 }
1613
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001614 write_pmc(event->hw.idx, val);
1615 local64_set(&event->hw.prev_count, val);
1616 local64_set(&event->hw.period_left, left);
1617 perf_event_update_userpage(event);
1618
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619 /*
1620 * Finally record data if requested.
1621 */
1622 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001623 struct perf_sample_data data;
1624
Robert Richterfd0d0002012-04-02 20:19:08 +02001625 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626
1627 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1628 perf_get_data_addr(regs, &data.addr);
1629
Anshuman Khandual3925f462013-04-22 19:42:44 +00001630 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1631 struct cpu_hw_events *cpuhw;
1632 cpuhw = &__get_cpu_var(cpu_hw_events);
1633 power_pmu_bhrb_read(cpuhw);
1634 data.br_stack = &cpuhw->bhrb_stack;
1635 }
1636
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001637 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001638 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001640}
1641
1642/*
1643 * Called from generic code to get the misc flags (i.e. processor mode)
1644 * for an event_id.
1645 */
1646unsigned long perf_misc_flags(struct pt_regs *regs)
1647{
1648 u32 flags = perf_get_misc_flags(regs);
1649
1650 if (flags)
1651 return flags;
1652 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1653 PERF_RECORD_MISC_KERNEL;
1654}
1655
1656/*
1657 * Called from generic code to get the instruction pointer
1658 * for an event_id.
1659 */
1660unsigned long perf_instruction_pointer(struct pt_regs *regs)
1661{
Michael Ellerman33904052013-04-25 19:28:25 +00001662 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001664 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00001665 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001666 else if (use_siar)
1667 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00001668 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00001669 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001670}
1671
Michael Neulingbc09c212012-11-05 15:53:54 +00001672static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11001673{
Anton Blanchard0837e322011-03-09 14:38:42 +11001674 /*
1675 * Events on POWER7 can roll back if a speculative event doesn't
1676 * eventually complete. Unfortunately in some rare cases they will
1677 * raise a performance monitor exception. We need to catch this to
1678 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1679 * cycles from overflow.
1680 *
1681 * We only do this if the first pass fails to find any overflowing
1682 * PMCs because a user might set a period of less than 256 and we
1683 * don't want to mistakenly reset them.
1684 */
Michael Neulingbc09c212012-11-05 15:53:54 +00001685 if ((0x80000000 - val) <= 256)
1686 return true;
1687
1688 return false;
1689}
1690
1691static bool pmc_overflow(unsigned long val)
1692{
1693 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11001694 return true;
1695
1696 return false;
1697}
1698
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699/*
1700 * Performance monitor interrupt stuff
1701 */
1702static void perf_event_interrupt(struct pt_regs *regs)
1703{
Michael Neulingbc09c212012-11-05 15:53:54 +00001704 int i, j;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001705 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1706 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00001707 unsigned long val[8];
1708 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709 int nmi;
1710
1711 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001712 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001713 mfspr(SPRN_PMC6));
1714
1715 perf_read_regs(regs);
1716
1717 nmi = perf_intr_is_nmi(regs);
1718 if (nmi)
1719 nmi_enter();
1720 else
1721 irq_enter();
1722
Michael Neulingbc09c212012-11-05 15:53:54 +00001723 /* Read all the PMCs since we'll need them a bunch of times */
1724 for (i = 0; i < ppmu->n_counter; ++i)
1725 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726
Michael Neulingbc09c212012-11-05 15:53:54 +00001727 /* Try to find what caused the IRQ */
1728 found = 0;
1729 for (i = 0; i < ppmu->n_counter; ++i) {
1730 if (!pmc_overflow(val[i]))
1731 continue;
1732 if (is_limited_pmc(i + 1))
1733 continue; /* these won't generate IRQs */
1734 /*
1735 * We've found one that's overflowed. For active
1736 * counters we need to log this. For inactive
1737 * counters, we need to reset it anyway
1738 */
1739 found = 1;
1740 active = 0;
1741 for (j = 0; j < cpuhw->n_events; ++j) {
1742 event = cpuhw->event[j];
1743 if (event->hw.idx == (i + 1)) {
1744 active = 1;
1745 record_and_restart(event, val[i], regs);
1746 break;
1747 }
1748 }
1749 if (!active)
1750 /* reset non active counters that have overflowed */
1751 write_pmc(i + 1, 0);
1752 }
1753 if (!found && pvr_version_is(PVR_POWER7)) {
1754 /* check active counters for special buggy p7 overflow */
1755 for (i = 0; i < cpuhw->n_events; ++i) {
1756 event = cpuhw->event[i];
1757 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00001759 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
1760 /* event has overflowed in a buggy way*/
1761 found = 1;
1762 record_and_restart(event,
1763 val[event->hw.idx - 1],
1764 regs);
1765 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766 }
1767 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00001768 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00001769 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770
1771 /*
1772 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02001773 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774 * and thus allow interrupts to occur again.
1775 * XXX might want to use MSR.PM to keep the events frozen until
1776 * we get back out of this interrupt.
1777 */
1778 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1779
1780 if (nmi)
1781 nmi_exit();
1782 else
1783 irq_exit();
1784}
1785
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001786static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787{
1788 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
1789
1790 if (!ppmu)
1791 return;
1792 memset(cpuhw, 0, sizeof(*cpuhw));
1793 cpuhw->mmcr[0] = MMCR0_FC;
1794}
1795
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001796static int
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01001797power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001798{
1799 unsigned int cpu = (long)hcpu;
1800
1801 switch (action & ~CPU_TASKS_FROZEN) {
1802 case CPU_UP_PREPARE:
1803 power_pmu_setup(cpu);
1804 break;
1805
1806 default:
1807 break;
1808 }
1809
1810 return NOTIFY_OK;
1811}
1812
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001813int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001814{
1815 if (ppmu)
1816 return -EBUSY; /* something's already registered */
1817
1818 ppmu = pmu;
1819 pr_info("%s performance monitor hardware support registered\n",
1820 pmu->name);
1821
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001822 power_pmu.attr_groups = ppmu->attr_groups;
1823
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001824#ifdef MSR_HV
1825 /*
1826 * Use FCHV to ignore kernel events if MSR.HV is set.
1827 */
1828 if (mfmsr() & MSR_HV)
1829 freeze_events_kernel = MMCR0_FCHV;
1830#endif /* CONFIG_PPC64 */
1831
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001832 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001833 perf_cpu_notifier(power_pmu_notifier);
1834
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001835 return 0;
1836}