blob: c91dc43e04de2d9ae11d0c23b3e5e177a11fc0c0 [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 Ellerman7a7a41f2013-06-28 18:15:12 +100078#define MMCR0_FC56 0
Michael Ellerman378a6ee2013-06-28 18:15:11 +100079#define MMCR0_PMAO 0
Ingo Molnarcdd6c482009-09-21 12:02:48 +020080
81#define SPRN_MMCRA SPRN_MMCR2
82#define MMCRA_SAMPLE_ENABLE 0
83
84static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
85{
86 return 0;
87}
88static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
89static inline u32 perf_get_misc_flags(struct pt_regs *regs)
90{
91 return 0;
92}
Anton Blanchard75382aa2012-06-26 01:01:36 +000093static inline void perf_read_regs(struct pt_regs *regs)
94{
95 regs->result = 0;
96}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020097static inline int perf_intr_is_nmi(struct pt_regs *regs)
98{
99 return 0;
100}
101
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000102static inline int siar_valid(struct pt_regs *regs)
103{
104 return 1;
105}
106
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000107static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
108static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
109void power_pmu_flush_branch_stack(void) {}
110static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200111#endif /* CONFIG_PPC32 */
112
Michael Ellerman33904052013-04-25 19:28:25 +0000113static bool regs_use_siar(struct pt_regs *regs)
114{
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000115 return !!regs->result;
Michael Ellerman33904052013-04-25 19:28:25 +0000116}
117
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200118/*
119 * Things that are specific to 64-bit implementations.
120 */
121#ifdef CONFIG_PPC64
122
123static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
124{
125 unsigned long mmcra = regs->dsisr;
126
Michael Ellerman7a786832013-04-25 19:28:23 +0000127 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200128 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
129 if (slot > 1)
130 return 4 * (slot - 1);
131 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000132
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200133 return 0;
134}
135
136/*
137 * The user wants a data address recorded.
138 * If we're not doing instruction sampling, give them the SDAR
139 * (sampled data address). If we are doing instruction sampling, then
140 * only give them the SDAR if it corresponds to the instruction
Michael Ellerman58a032c2013-05-15 20:19:31 +0000141 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
142 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200143 */
144static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
145{
146 unsigned long mmcra = regs->dsisr;
Michael Ellerman58a032c2013-05-15 20:19:31 +0000147 bool sdar_valid;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000148
Michael Ellerman58a032c2013-05-15 20:19:31 +0000149 if (ppmu->flags & PPMU_HAS_SIER)
150 sdar_valid = regs->dar & SIER_SDAR_VALID;
151 else {
152 unsigned long sdsync;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200153
Michael Ellerman58a032c2013-05-15 20:19:31 +0000154 if (ppmu->flags & PPMU_SIAR_VALID)
155 sdsync = POWER7P_MMCRA_SDAR_VALID;
156 else if (ppmu->flags & PPMU_ALT_SIPR)
157 sdsync = POWER6_MMCRA_SDSYNC;
158 else
159 sdsync = MMCRA_SDSYNC;
160
161 sdar_valid = mmcra & sdsync;
162 }
163
164 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200165 *addrp = mfspr(SPRN_SDAR);
166}
167
Michael Ellerman5682c462013-04-25 19:28:24 +0000168static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000169{
170 unsigned long sihv = MMCRA_SIHV;
171
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000172 if (ppmu->flags & PPMU_HAS_SIER)
173 return !!(regs->dar & SIER_SIHV);
174
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000175 if (ppmu->flags & PPMU_ALT_SIPR)
176 sihv = POWER6_MMCRA_SIHV;
177
Michael Ellerman5682c462013-04-25 19:28:24 +0000178 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000179}
180
Michael Ellerman5682c462013-04-25 19:28:24 +0000181static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000182{
183 unsigned long sipr = MMCRA_SIPR;
184
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000185 if (ppmu->flags & PPMU_HAS_SIER)
186 return !!(regs->dar & SIER_SIPR);
187
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000188 if (ppmu->flags & PPMU_ALT_SIPR)
189 sipr = POWER6_MMCRA_SIPR;
190
Michael Ellerman5682c462013-04-25 19:28:24 +0000191 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000192}
193
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000194static inline u32 perf_flags_from_msr(struct pt_regs *regs)
195{
196 if (regs->msr & MSR_PR)
197 return PERF_RECORD_MISC_USER;
198 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
199 return PERF_RECORD_MISC_HYPERVISOR;
200 return PERF_RECORD_MISC_KERNEL;
201}
202
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203static inline u32 perf_get_misc_flags(struct pt_regs *regs)
204{
Michael Ellerman33904052013-04-25 19:28:25 +0000205 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200206
Anton Blanchard75382aa2012-06-26 01:01:36 +0000207 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000208 return perf_flags_from_msr(regs);
209
210 /*
211 * If we don't have flags in MMCRA, rather than using
212 * the MSR, we intuit the flags from the address in
213 * SIAR which should give slightly more reliable
214 * results
215 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000216 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000217 unsigned long siar = mfspr(SPRN_SIAR);
218 if (siar >= PAGE_OFFSET)
219 return PERF_RECORD_MISC_KERNEL;
220 return PERF_RECORD_MISC_USER;
221 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200222
Michael Neuling7abb8402009-10-14 19:32:15 +0000223 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000224 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000225 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000226
227 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200228 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000229
Michael Neuling7abb8402009-10-14 19:32:15 +0000230 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200231}
232
233/*
234 * Overload regs->dsisr to store MMCRA so we only need to read it once
235 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000236 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000237 * Overload regs->result to specify whether we should use the MSR (result
238 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200239 */
240static inline void perf_read_regs(struct pt_regs *regs)
241{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000242 unsigned long mmcra = mfspr(SPRN_MMCRA);
243 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
244 int use_siar;
245
Michael Ellerman5682c462013-04-25 19:28:24 +0000246 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000247
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000248 if (ppmu->flags & PPMU_HAS_SIER)
249 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000250
251 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000252 * If this isn't a PMU exception (eg a software event) the SIAR is
253 * not valid. Use pt_regs.
254 *
255 * If it is a marked event use the SIAR.
256 *
257 * If the PMU doesn't update the SIAR for non marked events use
258 * pt_regs.
259 *
260 * If the PMU has HV/PR flags then check to see if they
261 * place the exception in userspace. If so, use pt_regs. In
262 * continuous sampling mode the SIAR and the PMU exception are
263 * not synchronised, so they may be many instructions apart.
264 * This can result in confusing backtraces. We still want
265 * hypervisor samples as well as samples in the kernel with
266 * interrupts off hence the userspace check.
267 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000268 if (TRAP(regs) != 0xf00)
269 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000270 else if (marked)
271 use_siar = 1;
272 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
273 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000274 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000275 use_siar = 0;
276 else
277 use_siar = 1;
278
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000279 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200280}
281
282/*
283 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
284 * it as an NMI.
285 */
286static inline int perf_intr_is_nmi(struct pt_regs *regs)
287{
288 return !regs->softe;
289}
290
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000291/*
292 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
293 * must be sampled only if the SIAR-valid bit is set.
294 *
295 * For unmarked instructions and for processors that don't have the SIAR-Valid
296 * bit, assume that SIAR is valid.
297 */
298static inline int siar_valid(struct pt_regs *regs)
299{
300 unsigned long mmcra = regs->dsisr;
301 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
302
Michael Ellerman58a032c2013-05-15 20:19:31 +0000303 if (marked) {
304 if (ppmu->flags & PPMU_HAS_SIER)
305 return regs->dar & SIER_SIAR_VALID;
306
307 if (ppmu->flags & PPMU_SIAR_VALID)
308 return mmcra & POWER7P_MMCRA_SIAR_VALID;
309 }
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000310
311 return 1;
312}
313
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000314
315/* Reset all possible BHRB entries */
316static void power_pmu_bhrb_reset(void)
317{
318 asm volatile(PPC_CLRBHRB);
319}
320
321static void power_pmu_bhrb_enable(struct perf_event *event)
322{
323 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
324
325 if (!ppmu->bhrb_nr)
326 return;
327
328 /* Clear BHRB if we changed task context to avoid data leaks */
329 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
330 power_pmu_bhrb_reset();
331 cpuhw->bhrb_context = event->ctx;
332 }
333 cpuhw->bhrb_users++;
334}
335
336static void power_pmu_bhrb_disable(struct perf_event *event)
337{
338 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
339
340 if (!ppmu->bhrb_nr)
341 return;
342
343 cpuhw->bhrb_users--;
344 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
345
346 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
347 /* BHRB cannot be turned off when other
348 * events are active on the PMU.
349 */
350
351 /* avoid stale pointer */
352 cpuhw->bhrb_context = NULL;
353 }
354}
355
356/* Called from ctxsw to prevent one process's branch entries to
357 * mingle with the other process's entries during context switch.
358 */
359void power_pmu_flush_branch_stack(void)
360{
361 if (ppmu->bhrb_nr)
362 power_pmu_bhrb_reset();
363}
Michael Neuling69123182013-05-13 18:44:58 +0000364/* Calculate the to address for a branch */
365static __u64 power_pmu_bhrb_to(u64 addr)
366{
367 unsigned int instr;
368 int ret;
369 __u64 target;
370
371 if (is_kernel_addr(addr))
372 return branch_target((unsigned int *)addr);
373
374 /* Userspace: need copy instruction here then translate it */
375 pagefault_disable();
376 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
377 if (ret) {
378 pagefault_enable();
379 return 0;
380 }
381 pagefault_enable();
382
383 target = branch_target(&instr);
384 if ((!target) || (instr & BRANCH_ABSOLUTE))
385 return target;
386
387 /* Translate relative branch target from kernel to user address */
388 return target - (unsigned long)&instr + addr;
389}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000390
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000391/* Processing BHRB entries */
Michael Neuling506e70d2013-05-13 18:44:57 +0000392void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000393{
394 u64 val;
395 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000396 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000397
398 r_index = 0;
399 u_index = 0;
400 while (r_index < ppmu->bhrb_nr) {
401 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000402 val = read_bhrb(r_index++);
403 if (!val)
404 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000405 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000406 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000407 addr = val & BHRB_EA;
408 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000409
Michael Neuling506e70d2013-05-13 18:44:57 +0000410 if (!addr)
411 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000412 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000413
Michael Neuling506e70d2013-05-13 18:44:57 +0000414 /* Branches are read most recent first (ie. mfbhrb 0 is
415 * the most recent branch).
416 * There are two types of valid entries:
417 * 1) a target entry which is the to address of a
418 * computed goto like a blr,bctr,btar. The next
419 * entry read from the bhrb will be branch
420 * corresponding to this target (ie. the actual
421 * blr/bctr/btar instruction).
422 * 2) a from address which is an actual branch. If a
423 * target entry proceeds this, then this is the
424 * matching branch for that target. If this is not
425 * following a target entry, then this is a branch
426 * where the target is given as an immediate field
427 * in the instruction (ie. an i or b form branch).
428 * In this case we need to read the instruction from
429 * memory to determine the target/to address.
430 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000431
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000432 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000433 /* Target branches use two entries
434 * (ie. computed gotos/XL form)
435 */
436 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000437 cpuhw->bhrb_entries[u_index].mispred = pred;
438 cpuhw->bhrb_entries[u_index].predicted = ~pred;
439
Michael Neuling506e70d2013-05-13 18:44:57 +0000440 /* Get from address in next entry */
441 val = read_bhrb(r_index++);
442 addr = val & BHRB_EA;
443 if (val & BHRB_TARGET) {
444 /* Shouldn't have two targets in a
445 row.. Reset index and try again */
446 r_index--;
447 addr = 0;
448 }
449 cpuhw->bhrb_entries[u_index].from = addr;
450 } else {
451 /* Branches to immediate field
452 (ie I or B form) */
453 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000454 cpuhw->bhrb_entries[u_index].to =
455 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000456 cpuhw->bhrb_entries[u_index].mispred = pred;
457 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000458 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000459 u_index++;
460
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000461 }
462 }
463 cpuhw->bhrb_stack.nr = u_index;
464 return;
465}
466
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200467#endif /* CONFIG_PPC64 */
468
469static void perf_event_interrupt(struct pt_regs *regs);
470
471void perf_event_print_debug(void)
472{
473}
474
475/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200476 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200477 */
478static unsigned long read_pmc(int idx)
479{
480 unsigned long val;
481
482 switch (idx) {
483 case 1:
484 val = mfspr(SPRN_PMC1);
485 break;
486 case 2:
487 val = mfspr(SPRN_PMC2);
488 break;
489 case 3:
490 val = mfspr(SPRN_PMC3);
491 break;
492 case 4:
493 val = mfspr(SPRN_PMC4);
494 break;
495 case 5:
496 val = mfspr(SPRN_PMC5);
497 break;
498 case 6:
499 val = mfspr(SPRN_PMC6);
500 break;
501#ifdef CONFIG_PPC64
502 case 7:
503 val = mfspr(SPRN_PMC7);
504 break;
505 case 8:
506 val = mfspr(SPRN_PMC8);
507 break;
508#endif /* CONFIG_PPC64 */
509 default:
510 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
511 val = 0;
512 }
513 return val;
514}
515
516/*
517 * Write one PMC.
518 */
519static void write_pmc(int idx, unsigned long val)
520{
521 switch (idx) {
522 case 1:
523 mtspr(SPRN_PMC1, val);
524 break;
525 case 2:
526 mtspr(SPRN_PMC2, val);
527 break;
528 case 3:
529 mtspr(SPRN_PMC3, val);
530 break;
531 case 4:
532 mtspr(SPRN_PMC4, val);
533 break;
534 case 5:
535 mtspr(SPRN_PMC5, val);
536 break;
537 case 6:
538 mtspr(SPRN_PMC6, val);
539 break;
540#ifdef CONFIG_PPC64
541 case 7:
542 mtspr(SPRN_PMC7, val);
543 break;
544 case 8:
545 mtspr(SPRN_PMC8, val);
546 break;
547#endif /* CONFIG_PPC64 */
548 default:
549 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
550 }
551}
552
553/*
554 * Check if a set of events can all go on the PMU at once.
555 * If they can't, this will look at alternative codes for the events
556 * and see if any combination of alternative codes is feasible.
557 * The feasible set is returned in event_id[].
558 */
559static int power_check_constraints(struct cpu_hw_events *cpuhw,
560 u64 event_id[], unsigned int cflags[],
561 int n_ev)
562{
563 unsigned long mask, value, nv;
564 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
565 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
566 int i, j;
567 unsigned long addf = ppmu->add_fields;
568 unsigned long tadd = ppmu->test_adder;
569
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000570 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200571 return -1;
572
573 /* First see if the events will go on as-is */
574 for (i = 0; i < n_ev; ++i) {
575 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
576 && !ppmu->limited_pmc_event(event_id[i])) {
577 ppmu->get_alternatives(event_id[i], cflags[i],
578 cpuhw->alternatives[i]);
579 event_id[i] = cpuhw->alternatives[i][0];
580 }
581 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
582 &cpuhw->avalues[i][0]))
583 return -1;
584 }
585 value = mask = 0;
586 for (i = 0; i < n_ev; ++i) {
587 nv = (value | cpuhw->avalues[i][0]) +
588 (value & cpuhw->avalues[i][0] & addf);
589 if ((((nv + tadd) ^ value) & mask) != 0 ||
590 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
591 cpuhw->amasks[i][0]) != 0)
592 break;
593 value = nv;
594 mask |= cpuhw->amasks[i][0];
595 }
596 if (i == n_ev)
597 return 0; /* all OK */
598
599 /* doesn't work, gather alternatives... */
600 if (!ppmu->get_alternatives)
601 return -1;
602 for (i = 0; i < n_ev; ++i) {
603 choice[i] = 0;
604 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
605 cpuhw->alternatives[i]);
606 for (j = 1; j < n_alt[i]; ++j)
607 ppmu->get_constraint(cpuhw->alternatives[i][j],
608 &cpuhw->amasks[i][j],
609 &cpuhw->avalues[i][j]);
610 }
611
612 /* enumerate all possibilities and see if any will work */
613 i = 0;
614 j = -1;
615 value = mask = nv = 0;
616 while (i < n_ev) {
617 if (j >= 0) {
618 /* we're backtracking, restore context */
619 value = svalues[i];
620 mask = smasks[i];
621 j = choice[i];
622 }
623 /*
624 * See if any alternative k for event_id i,
625 * where k > j, will satisfy the constraints.
626 */
627 while (++j < n_alt[i]) {
628 nv = (value | cpuhw->avalues[i][j]) +
629 (value & cpuhw->avalues[i][j] & addf);
630 if ((((nv + tadd) ^ value) & mask) == 0 &&
631 (((nv + tadd) ^ cpuhw->avalues[i][j])
632 & cpuhw->amasks[i][j]) == 0)
633 break;
634 }
635 if (j >= n_alt[i]) {
636 /*
637 * No feasible alternative, backtrack
638 * to event_id i-1 and continue enumerating its
639 * alternatives from where we got up to.
640 */
641 if (--i < 0)
642 return -1;
643 } else {
644 /*
645 * Found a feasible alternative for event_id i,
646 * remember where we got up to with this event_id,
647 * go on to the next event_id, and start with
648 * the first alternative for it.
649 */
650 choice[i] = j;
651 svalues[i] = value;
652 smasks[i] = mask;
653 value = nv;
654 mask |= cpuhw->amasks[i][j];
655 ++i;
656 j = -1;
657 }
658 }
659
660 /* OK, we have a feasible combination, tell the caller the solution */
661 for (i = 0; i < n_ev; ++i)
662 event_id[i] = cpuhw->alternatives[i][choice[i]];
663 return 0;
664}
665
666/*
667 * Check if newly-added events have consistent settings for
668 * exclude_{user,kernel,hv} with each other and any previously
669 * added events.
670 */
671static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
672 int n_prev, int n_new)
673{
674 int eu = 0, ek = 0, eh = 0;
675 int i, n, first;
676 struct perf_event *event;
677
678 n = n_prev + n_new;
679 if (n <= 1)
680 return 0;
681
682 first = 1;
683 for (i = 0; i < n; ++i) {
684 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
685 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
686 continue;
687 }
688 event = ctrs[i];
689 if (first) {
690 eu = event->attr.exclude_user;
691 ek = event->attr.exclude_kernel;
692 eh = event->attr.exclude_hv;
693 first = 0;
694 } else if (event->attr.exclude_user != eu ||
695 event->attr.exclude_kernel != ek ||
696 event->attr.exclude_hv != eh) {
697 return -EAGAIN;
698 }
699 }
700
701 if (eu || ek || eh)
702 for (i = 0; i < n; ++i)
703 if (cflags[i] & PPMU_LIMITED_PMC_OK)
704 cflags[i] |= PPMU_LIMITED_PMC_REQD;
705
706 return 0;
707}
708
Eric B Munson86c74ab2011-04-15 08:12:30 +0000709static u64 check_and_compute_delta(u64 prev, u64 val)
710{
711 u64 delta = (val - prev) & 0xfffffffful;
712
713 /*
714 * POWER7 can roll back counter values, if the new value is smaller
715 * than the previous value it will cause the delta and the counter to
716 * have bogus values unless we rolled a counter over. If a coutner is
717 * rolled back, it will be smaller, but within 256, which is the maximum
718 * number of events to rollback at once. If we dectect a rollback
719 * return 0. This can lead to a small lack of precision in the
720 * counters.
721 */
722 if (prev > val && (prev - val) < 256)
723 delta = 0;
724
725 return delta;
726}
727
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200728static void power_pmu_read(struct perf_event *event)
729{
730 s64 val, delta, prev;
731
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200732 if (event->hw.state & PERF_HES_STOPPED)
733 return;
734
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200735 if (!event->hw.idx)
736 return;
737 /*
738 * Performance monitor interrupts come even when interrupts
739 * are soft-disabled, as long as interrupts are hard-enabled.
740 * Therefore we treat them like NMIs.
741 */
742 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +0200743 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200744 barrier();
745 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +0000746 delta = check_and_compute_delta(prev, val);
747 if (!delta)
748 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200749 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200750
Peter Zijlstrae7850592010-05-21 14:43:08 +0200751 local64_add(delta, &event->count);
752 local64_sub(delta, &event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200753}
754
755/*
756 * On some machines, PMC5 and PMC6 can't be written, don't respect
757 * the freeze conditions, and don't generate interrupts. This tells
758 * us if `event' is using such a PMC.
759 */
760static int is_limited_pmc(int pmcnum)
761{
762 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
763 && (pmcnum == 5 || pmcnum == 6);
764}
765
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000766static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200767 unsigned long pmc5, unsigned long pmc6)
768{
769 struct perf_event *event;
770 u64 val, prev, delta;
771 int i;
772
773 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000774 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200775 if (!event->hw.idx)
776 continue;
777 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200778 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200779 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000780 delta = check_and_compute_delta(prev, val);
781 if (delta)
782 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200783 }
784}
785
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000786static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200787 unsigned long pmc5, unsigned long pmc6)
788{
789 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000790 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200791 int i;
792
793 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000794 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200795 event->hw.idx = cpuhw->limited_hwidx[i];
796 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +0000797 prev = local64_read(&event->hw.prev_count);
798 if (check_and_compute_delta(prev, val))
799 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200800 perf_event_update_userpage(event);
801 }
802}
803
804/*
805 * Since limited events don't respect the freeze conditions, we
806 * have to read them immediately after freezing or unfreezing the
807 * other events. We try to keep the values from the limited
808 * events as consistent as possible by keeping the delay (in
809 * cycles and instructions) between freezing/unfreezing and reading
810 * the limited events as small and consistent as possible.
811 * Therefore, if any limited events are in use, we read them
812 * both, and always in the same order, to minimize variability,
813 * and do it inside the same asm that writes MMCR0.
814 */
815static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
816{
817 unsigned long pmc5, pmc6;
818
819 if (!cpuhw->n_limited) {
820 mtspr(SPRN_MMCR0, mmcr0);
821 return;
822 }
823
824 /*
825 * Write MMCR0, then read PMC5 and PMC6 immediately.
826 * To ensure we don't get a performance monitor interrupt
827 * between writing MMCR0 and freezing/thawing the limited
828 * events, we first write MMCR0 with the event overflow
829 * interrupt enable bits turned off.
830 */
831 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
832 : "=&r" (pmc5), "=&r" (pmc6)
833 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
834 "i" (SPRN_MMCR0),
835 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
836
837 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000838 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200839 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000840 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200841
842 /*
843 * Write the full MMCR0 including the event overflow interrupt
844 * enable bits, if necessary.
845 */
846 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
847 mtspr(SPRN_MMCR0, mmcr0);
848}
849
850/*
851 * Disable all events to prevent PMU interrupts and to allow
852 * events to be added or removed.
853 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200854static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200855{
856 struct cpu_hw_events *cpuhw;
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000857 unsigned long flags, val;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200858
859 if (!ppmu)
860 return;
861 local_irq_save(flags);
862 cpuhw = &__get_cpu_var(cpu_hw_events);
863
864 if (!cpuhw->disabled) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200865 /*
866 * Check if we ever enabled the PMU on this cpu.
867 */
868 if (!cpuhw->pmcs_enabled) {
869 ppc_enable_pmcs();
870 cpuhw->pmcs_enabled = 1;
871 }
872
873 /*
Michael Ellerman7a7a41f2013-06-28 18:15:12 +1000874 * Set the 'freeze counters' bit, clear PMAO/FC56.
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000875 */
876 val = mfspr(SPRN_MMCR0);
877 val |= MMCR0_FC;
Michael Ellerman7a7a41f2013-06-28 18:15:12 +1000878 val &= ~(MMCR0_PMAO | MMCR0_FC56);
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000879
880 /*
881 * The barrier is to make sure the mtspr has been
882 * executed and the PMU has frozen the events etc.
883 * before we return.
884 */
885 write_mmcr0(cpuhw, val);
886 mb();
887
888 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200889 * Disable instruction sampling if it was enabled
890 */
891 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
892 mtspr(SPRN_MMCRA,
893 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
894 mb();
895 }
896
Michael Ellerman378a6ee2013-06-28 18:15:11 +1000897 cpuhw->disabled = 1;
898 cpuhw->n_added = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200899 }
900 local_irq_restore(flags);
901}
902
903/*
904 * Re-enable all events if disable == 0.
905 * If we were previously disabled and events were added, then
906 * put the new config on the PMU.
907 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200908static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200909{
910 struct perf_event *event;
911 struct cpu_hw_events *cpuhw;
912 unsigned long flags;
913 long i;
914 unsigned long val;
915 s64 left;
916 unsigned int hwc_index[MAX_HWEVENTS];
917 int n_lim;
918 int idx;
919
920 if (!ppmu)
921 return;
Michael Ellerman0a488432013-06-28 18:15:13 +1000922
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200923 local_irq_save(flags);
Michael Ellerman0a488432013-06-28 18:15:13 +1000924
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200925 cpuhw = &__get_cpu_var(cpu_hw_events);
Michael Ellerman0a488432013-06-28 18:15:13 +1000926 if (!cpuhw->disabled)
927 goto out;
928
Michael Ellerman4ea355b2013-06-28 18:15:14 +1000929 if (cpuhw->n_events == 0) {
930 ppc_set_pmu_inuse(0);
931 goto out;
932 }
933
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200934 cpuhw->disabled = 0;
935
936 /*
937 * If we didn't change anything, or only removed events,
938 * no need to recalculate MMCR* settings and reset the PMCs.
939 * Just reenable the PMU with the current MMCR* settings
940 * (possibly updated for removal of events).
941 */
942 if (!cpuhw->n_added) {
943 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
944 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200945 goto out_enable;
946 }
947
948 /*
949 * Compute MMCR* values for the new set of events
950 */
951 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
952 cpuhw->mmcr)) {
953 /* shouldn't ever get here */
954 printk(KERN_ERR "oops compute_mmcr failed\n");
955 goto out;
956 }
957
958 /*
959 * Add in MMCR0 freeze bits corresponding to the
960 * attr.exclude_* bits for the first event.
961 * We have already checked that all events have the
962 * same values for these bits as the first event.
963 */
964 event = cpuhw->event[0];
965 if (event->attr.exclude_user)
966 cpuhw->mmcr[0] |= MMCR0_FCP;
967 if (event->attr.exclude_kernel)
968 cpuhw->mmcr[0] |= freeze_events_kernel;
969 if (event->attr.exclude_hv)
970 cpuhw->mmcr[0] |= MMCR0_FCHV;
971
972 /*
973 * Write the new configuration to MMCR* with the freeze
974 * bit set and set the hardware events to their initial values.
975 * Then unfreeze the events.
976 */
977 ppc_set_pmu_inuse(1);
978 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
979 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
980 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
981 | MMCR0_FC);
982
983 /*
984 * Read off any pre-existing events that need to move
985 * to another PMC.
986 */
987 for (i = 0; i < cpuhw->n_events; ++i) {
988 event = cpuhw->event[i];
989 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
990 power_pmu_read(event);
991 write_pmc(event->hw.idx, 0);
992 event->hw.idx = 0;
993 }
994 }
995
996 /*
997 * Initialize the PMCs for all the new and moved events.
998 */
999 cpuhw->n_limited = n_lim = 0;
1000 for (i = 0; i < cpuhw->n_events; ++i) {
1001 event = cpuhw->event[i];
1002 if (event->hw.idx)
1003 continue;
1004 idx = hwc_index[i] + 1;
1005 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001006 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001007 cpuhw->limited_hwidx[n_lim] = idx;
1008 ++n_lim;
1009 continue;
1010 }
1011 val = 0;
1012 if (event->hw.sample_period) {
Peter Zijlstrae7850592010-05-21 14:43:08 +02001013 left = local64_read(&event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014 if (left < 0x80000000L)
1015 val = 0x80000000L - left;
1016 }
Peter Zijlstrae7850592010-05-21 14:43:08 +02001017 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001019 if (event->hw.state & PERF_HES_STOPPED)
1020 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001021 write_pmc(idx, val);
1022 perf_event_update_userpage(event);
1023 }
1024 cpuhw->n_limited = n_lim;
1025 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1026
1027 out_enable:
1028 mb();
1029 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1030
1031 /*
1032 * Enable instruction sampling if necessary
1033 */
1034 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1035 mb();
1036 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1037 }
1038
1039 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001040 if (cpuhw->bhrb_users)
1041 ppmu->config_bhrb(cpuhw->bhrb_filter);
1042
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001043 local_irq_restore(flags);
1044}
1045
1046static int collect_events(struct perf_event *group, int max_count,
1047 struct perf_event *ctrs[], u64 *events,
1048 unsigned int *flags)
1049{
1050 int n = 0;
1051 struct perf_event *event;
1052
1053 if (!is_software_event(group)) {
1054 if (n >= max_count)
1055 return -1;
1056 ctrs[n] = group;
1057 flags[n] = group->hw.event_base;
1058 events[n++] = group->hw.config;
1059 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001060 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001061 if (!is_software_event(event) &&
1062 event->state != PERF_EVENT_STATE_OFF) {
1063 if (n >= max_count)
1064 return -1;
1065 ctrs[n] = event;
1066 flags[n] = event->hw.event_base;
1067 events[n++] = event->hw.config;
1068 }
1069 }
1070 return n;
1071}
1072
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001073/*
1074 * Add a event to the PMU.
1075 * If all events are not already frozen, then we disable and
1076 * re-enable the PMU in order to get hw_perf_enable to do the
1077 * actual work of reconfiguring the PMU.
1078 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001079static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001080{
1081 struct cpu_hw_events *cpuhw;
1082 unsigned long flags;
1083 int n0;
1084 int ret = -EAGAIN;
1085
1086 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001087 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001088
1089 /*
1090 * Add the event to the list (if there is room)
1091 * and check whether the total set is still feasible.
1092 */
1093 cpuhw = &__get_cpu_var(cpu_hw_events);
1094 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001095 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 goto out;
1097 cpuhw->event[n0] = event;
1098 cpuhw->events[n0] = event->hw.config;
1099 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001100
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001101 /*
1102 * This event may have been disabled/stopped in record_and_restart()
1103 * because we exceeded the ->event_limit. If re-starting the event,
1104 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1105 * notification is re-enabled.
1106 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001107 if (!(ef_flags & PERF_EF_START))
1108 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001109 else
1110 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001111
Lin Ming8e6d5572010-05-08 20:28:41 +10001112 /*
1113 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001114 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001115 * at commit time(->commit_txn) as a whole
1116 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001117 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001118 goto nocheck;
1119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001120 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1121 goto out;
1122 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1123 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001124 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001125
1126nocheck:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001127 ++cpuhw->n_events;
1128 ++cpuhw->n_added;
1129
1130 ret = 0;
1131 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001132 if (has_branch_stack(event))
1133 power_pmu_bhrb_enable(event);
1134
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001135 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001136 local_irq_restore(flags);
1137 return ret;
1138}
1139
1140/*
1141 * Remove a event from the PMU.
1142 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001143static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144{
1145 struct cpu_hw_events *cpuhw;
1146 long i;
1147 unsigned long flags;
1148
1149 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001150 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151
1152 power_pmu_read(event);
1153
1154 cpuhw = &__get_cpu_var(cpu_hw_events);
1155 for (i = 0; i < cpuhw->n_events; ++i) {
1156 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001157 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001158 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001159 cpuhw->events[i-1] = cpuhw->events[i];
1160 cpuhw->flags[i-1] = cpuhw->flags[i];
1161 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001162 --cpuhw->n_events;
1163 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1164 if (event->hw.idx) {
1165 write_pmc(event->hw.idx, 0);
1166 event->hw.idx = 0;
1167 }
1168 perf_event_update_userpage(event);
1169 break;
1170 }
1171 }
1172 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001173 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001174 break;
1175 if (i < cpuhw->n_limited) {
1176 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001177 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001178 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1179 }
1180 --cpuhw->n_limited;
1181 }
1182 if (cpuhw->n_events == 0) {
1183 /* disable exceptions if no events are running */
1184 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1185 }
1186
Anshuman Khandual3925f462013-04-22 19:42:44 +00001187 if (has_branch_stack(event))
1188 power_pmu_bhrb_disable(event);
1189
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001190 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191 local_irq_restore(flags);
1192}
1193
1194/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001195 * POWER-PMU does not support disabling individual counters, hence
1196 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001198
1199static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001201 unsigned long flags;
1202 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001203 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001204
1205 if (!event->hw.idx || !event->hw.sample_period)
1206 return;
1207
1208 if (!(event->hw.state & PERF_HES_STOPPED))
1209 return;
1210
1211 if (ef_flags & PERF_EF_RELOAD)
1212 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1213
1214 local_irq_save(flags);
1215 perf_pmu_disable(event->pmu);
1216
1217 event->hw.state = 0;
1218 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001219
1220 val = 0;
1221 if (left < 0x80000000L)
1222 val = 0x80000000L - left;
1223
1224 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001225
1226 perf_event_update_userpage(event);
1227 perf_pmu_enable(event->pmu);
1228 local_irq_restore(flags);
1229}
1230
1231static void power_pmu_stop(struct perf_event *event, int ef_flags)
1232{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233 unsigned long flags;
1234
1235 if (!event->hw.idx || !event->hw.sample_period)
1236 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001237
1238 if (event->hw.state & PERF_HES_STOPPED)
1239 return;
1240
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001242 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001244 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001245 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1246 write_pmc(event->hw.idx, 0);
1247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001248 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001249 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001250 local_irq_restore(flags);
1251}
1252
Lin Ming8e6d5572010-05-08 20:28:41 +10001253/*
1254 * Start group events scheduling transaction
1255 * Set the flag to make pmu::enable() not perform the
1256 * schedulability test, it will be performed at commit time
1257 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001258void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001259{
1260 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1261
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001262 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001263 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001264 cpuhw->n_txn_start = cpuhw->n_events;
1265}
1266
1267/*
1268 * Stop group events scheduling transaction
1269 * Clear the flag and pmu::enable() will perform the
1270 * schedulability test.
1271 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001272void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001273{
1274 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1275
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001276 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001277 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001278}
1279
1280/*
1281 * Commit group events scheduling transaction
1282 * Perform the group schedulability test as a whole
1283 * Return 0 if success
1284 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001285int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001286{
1287 struct cpu_hw_events *cpuhw;
1288 long i, n;
1289
1290 if (!ppmu)
1291 return -EAGAIN;
1292 cpuhw = &__get_cpu_var(cpu_hw_events);
1293 n = cpuhw->n_events;
1294 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1295 return -EAGAIN;
1296 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1297 if (i < 0)
1298 return -EAGAIN;
1299
1300 for (i = cpuhw->n_txn_start; i < n; ++i)
1301 cpuhw->event[i]->hw.config = cpuhw->events[i];
1302
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001303 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001304 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001305 return 0;
1306}
1307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308/*
1309 * Return 1 if we might be able to put event on a limited PMC,
1310 * or 0 if not.
1311 * A event can only go on a limited PMC if it counts something
1312 * that a limited PMC can count, doesn't require interrupts, and
1313 * doesn't exclude any processor mode.
1314 */
1315static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1316 unsigned int flags)
1317{
1318 int n;
1319 u64 alt[MAX_EVENT_ALTERNATIVES];
1320
1321 if (event->attr.exclude_user
1322 || event->attr.exclude_kernel
1323 || event->attr.exclude_hv
1324 || event->attr.sample_period)
1325 return 0;
1326
1327 if (ppmu->limited_pmc_event(ev))
1328 return 1;
1329
1330 /*
1331 * The requested event_id isn't on a limited PMC already;
1332 * see if any alternative code goes on a limited PMC.
1333 */
1334 if (!ppmu->get_alternatives)
1335 return 0;
1336
1337 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1338 n = ppmu->get_alternatives(ev, flags, alt);
1339
1340 return n > 0;
1341}
1342
1343/*
1344 * Find an alternative event_id that goes on a normal PMC, if possible,
1345 * and return the event_id code, or 0 if there is no such alternative.
1346 * (Note: event_id code 0 is "don't count" on all machines.)
1347 */
1348static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1349{
1350 u64 alt[MAX_EVENT_ALTERNATIVES];
1351 int n;
1352
1353 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1354 n = ppmu->get_alternatives(ev, flags, alt);
1355 if (!n)
1356 return 0;
1357 return alt[0];
1358}
1359
1360/* Number of perf_events counting hardware events */
1361static atomic_t num_events;
1362/* Used to avoid races in calling reserve/release_pmc_hardware */
1363static DEFINE_MUTEX(pmc_reserve_mutex);
1364
1365/*
1366 * Release the PMU if this is the last perf_event.
1367 */
1368static void hw_perf_event_destroy(struct perf_event *event)
1369{
1370 if (!atomic_add_unless(&num_events, -1, 1)) {
1371 mutex_lock(&pmc_reserve_mutex);
1372 if (atomic_dec_return(&num_events) == 0)
1373 release_pmc_hardware();
1374 mutex_unlock(&pmc_reserve_mutex);
1375 }
1376}
1377
1378/*
1379 * Translate a generic cache event_id config to a raw event_id code.
1380 */
1381static int hw_perf_cache_event(u64 config, u64 *eventp)
1382{
1383 unsigned long type, op, result;
1384 int ev;
1385
1386 if (!ppmu->cache_events)
1387 return -EINVAL;
1388
1389 /* unpack config */
1390 type = config & 0xff;
1391 op = (config >> 8) & 0xff;
1392 result = (config >> 16) & 0xff;
1393
1394 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1395 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1396 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1397 return -EINVAL;
1398
1399 ev = (*ppmu->cache_events)[type][op][result];
1400 if (ev == 0)
1401 return -EOPNOTSUPP;
1402 if (ev == -1)
1403 return -EINVAL;
1404 *eventp = ev;
1405 return 0;
1406}
1407
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001408static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409{
1410 u64 ev;
1411 unsigned long flags;
1412 struct perf_event *ctrs[MAX_HWEVENTS];
1413 u64 events[MAX_HWEVENTS];
1414 unsigned int cflags[MAX_HWEVENTS];
1415 int n;
1416 int err;
1417 struct cpu_hw_events *cpuhw;
1418
1419 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001420 return -ENOENT;
1421
Anshuman Khandual3925f462013-04-22 19:42:44 +00001422 if (has_branch_stack(event)) {
1423 /* PMU has BHRB enabled */
1424 if (!(ppmu->flags & PPMU_BHRB))
1425 return -EOPNOTSUPP;
1426 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428 switch (event->attr.type) {
1429 case PERF_TYPE_HARDWARE:
1430 ev = event->attr.config;
1431 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001432 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001433 ev = ppmu->generic_events[ev];
1434 break;
1435 case PERF_TYPE_HW_CACHE:
1436 err = hw_perf_cache_event(event->attr.config, &ev);
1437 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001438 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001439 break;
1440 case PERF_TYPE_RAW:
1441 ev = event->attr.config;
1442 break;
1443 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001444 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447 event->hw.config_base = ev;
1448 event->hw.idx = 0;
1449
1450 /*
1451 * If we are not running on a hypervisor, force the
1452 * exclude_hv bit to 0 so that we don't care what
1453 * the user set it to.
1454 */
1455 if (!firmware_has_feature(FW_FEATURE_LPAR))
1456 event->attr.exclude_hv = 0;
1457
1458 /*
1459 * If this is a per-task event, then we can use
1460 * PM_RUN_* events interchangeably with their non RUN_*
1461 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1462 * XXX we should check if the task is an idle task.
1463 */
1464 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001465 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001466 flags |= PPMU_ONLY_COUNT_RUN;
1467
1468 /*
1469 * If this machine has limited events, check whether this
1470 * event_id could go on a limited event.
1471 */
1472 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1473 if (can_go_on_limited_pmc(event, ev, flags)) {
1474 flags |= PPMU_LIMITED_PMC_OK;
1475 } else if (ppmu->limited_pmc_event(ev)) {
1476 /*
1477 * The requested event_id is on a limited PMC,
1478 * but we can't use a limited PMC; see if any
1479 * alternative goes on a normal PMC.
1480 */
1481 ev = normal_pmc_alternative(ev, flags);
1482 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001483 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484 }
1485 }
1486
1487 /*
1488 * If this is in a group, check if it can go on with all the
1489 * other hardware events in the group. We assume the event
1490 * hasn't been linked into its leader's sibling list at this point.
1491 */
1492 n = 0;
1493 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001494 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 ctrs, events, cflags);
1496 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001497 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001498 }
1499 events[n] = ev;
1500 ctrs[n] = event;
1501 cflags[n] = flags;
1502 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001503 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504
1505 cpuhw = &get_cpu_var(cpu_hw_events);
1506 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001507
1508 if (has_branch_stack(event)) {
1509 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1510 event->attr.branch_sample_type);
1511
1512 if(cpuhw->bhrb_filter == -1)
1513 return -EOPNOTSUPP;
1514 }
1515
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516 put_cpu_var(cpu_hw_events);
1517 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001518 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519
1520 event->hw.config = events[n];
1521 event->hw.event_base = cflags[n];
1522 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001523 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001524
1525 /*
1526 * See if we need to reserve the PMU.
1527 * If no events are currently in use, then we have to take a
1528 * mutex to ensure that we don't race with another task doing
1529 * reserve_pmc_hardware or release_pmc_hardware.
1530 */
1531 err = 0;
1532 if (!atomic_inc_not_zero(&num_events)) {
1533 mutex_lock(&pmc_reserve_mutex);
1534 if (atomic_read(&num_events) == 0 &&
1535 reserve_pmc_hardware(perf_event_interrupt))
1536 err = -EBUSY;
1537 else
1538 atomic_inc(&num_events);
1539 mutex_unlock(&pmc_reserve_mutex);
1540 }
1541 event->destroy = hw_perf_event_destroy;
1542
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001543 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544}
1545
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001546static int power_pmu_event_idx(struct perf_event *event)
1547{
1548 return event->hw.idx;
1549}
1550
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001551ssize_t power_events_sysfs_show(struct device *dev,
1552 struct device_attribute *attr, char *page)
1553{
1554 struct perf_pmu_events_attr *pmu_attr;
1555
1556 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1557
1558 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1559}
1560
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001561struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001562 .pmu_enable = power_pmu_enable,
1563 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001564 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001565 .add = power_pmu_add,
1566 .del = power_pmu_del,
1567 .start = power_pmu_start,
1568 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001569 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001570 .start_txn = power_pmu_start_txn,
1571 .cancel_txn = power_pmu_cancel_txn,
1572 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001573 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001574 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001575};
1576
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001578 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001579 * things if requested. Note that interrupts are hard-disabled
1580 * here so there is no possibility of being interrupted.
1581 */
1582static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001583 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584{
1585 u64 period = event->hw.sample_period;
1586 s64 prev, delta, left;
1587 int record = 0;
1588
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001589 if (event->hw.state & PERF_HES_STOPPED) {
1590 write_pmc(event->hw.idx, 0);
1591 return;
1592 }
1593
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001594 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001595 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001596 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001597 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001598
1599 /*
1600 * See if the total period for this event has expired,
1601 * and update for the next period.
1602 */
1603 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001604 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001605 if (delta == 0)
1606 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001607 if (period) {
1608 if (left <= 0) {
1609 left += period;
1610 if (left <= 0)
1611 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001612 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001613 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614 }
1615 if (left < 0x80000000LL)
1616 val = 0x80000000LL - left;
1617 }
1618
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001619 write_pmc(event->hw.idx, val);
1620 local64_set(&event->hw.prev_count, val);
1621 local64_set(&event->hw.period_left, left);
1622 perf_event_update_userpage(event);
1623
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001624 /*
1625 * Finally record data if requested.
1626 */
1627 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001628 struct perf_sample_data data;
1629
Robert Richterfd0d0002012-04-02 20:19:08 +02001630 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001631
1632 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1633 perf_get_data_addr(regs, &data.addr);
1634
Anshuman Khandual3925f462013-04-22 19:42:44 +00001635 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1636 struct cpu_hw_events *cpuhw;
1637 cpuhw = &__get_cpu_var(cpu_hw_events);
1638 power_pmu_bhrb_read(cpuhw);
1639 data.br_stack = &cpuhw->bhrb_stack;
1640 }
1641
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001642 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001643 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001644 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645}
1646
1647/*
1648 * Called from generic code to get the misc flags (i.e. processor mode)
1649 * for an event_id.
1650 */
1651unsigned long perf_misc_flags(struct pt_regs *regs)
1652{
1653 u32 flags = perf_get_misc_flags(regs);
1654
1655 if (flags)
1656 return flags;
1657 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1658 PERF_RECORD_MISC_KERNEL;
1659}
1660
1661/*
1662 * Called from generic code to get the instruction pointer
1663 * for an event_id.
1664 */
1665unsigned long perf_instruction_pointer(struct pt_regs *regs)
1666{
Michael Ellerman33904052013-04-25 19:28:25 +00001667 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001669 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00001670 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001671 else if (use_siar)
1672 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00001673 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00001674 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001675}
1676
Michael Neulingbc09c212012-11-05 15:53:54 +00001677static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11001678{
Anton Blanchard0837e322011-03-09 14:38:42 +11001679 /*
1680 * Events on POWER7 can roll back if a speculative event doesn't
1681 * eventually complete. Unfortunately in some rare cases they will
1682 * raise a performance monitor exception. We need to catch this to
1683 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1684 * cycles from overflow.
1685 *
1686 * We only do this if the first pass fails to find any overflowing
1687 * PMCs because a user might set a period of less than 256 and we
1688 * don't want to mistakenly reset them.
1689 */
Michael Neulingbc09c212012-11-05 15:53:54 +00001690 if ((0x80000000 - val) <= 256)
1691 return true;
1692
1693 return false;
1694}
1695
1696static bool pmc_overflow(unsigned long val)
1697{
1698 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11001699 return true;
1700
1701 return false;
1702}
1703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704/*
1705 * Performance monitor interrupt stuff
1706 */
1707static void perf_event_interrupt(struct pt_regs *regs)
1708{
Michael Neulingbc09c212012-11-05 15:53:54 +00001709 int i, j;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001710 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1711 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00001712 unsigned long val[8];
1713 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001714 int nmi;
1715
1716 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001717 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718 mfspr(SPRN_PMC6));
1719
1720 perf_read_regs(regs);
1721
1722 nmi = perf_intr_is_nmi(regs);
1723 if (nmi)
1724 nmi_enter();
1725 else
1726 irq_enter();
1727
Michael Neulingbc09c212012-11-05 15:53:54 +00001728 /* Read all the PMCs since we'll need them a bunch of times */
1729 for (i = 0; i < ppmu->n_counter; ++i)
1730 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731
Michael Neulingbc09c212012-11-05 15:53:54 +00001732 /* Try to find what caused the IRQ */
1733 found = 0;
1734 for (i = 0; i < ppmu->n_counter; ++i) {
1735 if (!pmc_overflow(val[i]))
1736 continue;
1737 if (is_limited_pmc(i + 1))
1738 continue; /* these won't generate IRQs */
1739 /*
1740 * We've found one that's overflowed. For active
1741 * counters we need to log this. For inactive
1742 * counters, we need to reset it anyway
1743 */
1744 found = 1;
1745 active = 0;
1746 for (j = 0; j < cpuhw->n_events; ++j) {
1747 event = cpuhw->event[j];
1748 if (event->hw.idx == (i + 1)) {
1749 active = 1;
1750 record_and_restart(event, val[i], regs);
1751 break;
1752 }
1753 }
1754 if (!active)
1755 /* reset non active counters that have overflowed */
1756 write_pmc(i + 1, 0);
1757 }
1758 if (!found && pvr_version_is(PVR_POWER7)) {
1759 /* check active counters for special buggy p7 overflow */
1760 for (i = 0; i < cpuhw->n_events; ++i) {
1761 event = cpuhw->event[i];
1762 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00001764 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
1765 /* event has overflowed in a buggy way*/
1766 found = 1;
1767 record_and_restart(event,
1768 val[event->hw.idx - 1],
1769 regs);
1770 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001771 }
1772 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00001773 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00001774 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775
1776 /*
1777 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02001778 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779 * and thus allow interrupts to occur again.
1780 * XXX might want to use MSR.PM to keep the events frozen until
1781 * we get back out of this interrupt.
1782 */
1783 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1784
1785 if (nmi)
1786 nmi_exit();
1787 else
1788 irq_exit();
1789}
1790
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001791static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792{
1793 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
1794
1795 if (!ppmu)
1796 return;
1797 memset(cpuhw, 0, sizeof(*cpuhw));
1798 cpuhw->mmcr[0] = MMCR0_FC;
1799}
1800
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001801static int
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01001802power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001803{
1804 unsigned int cpu = (long)hcpu;
1805
1806 switch (action & ~CPU_TASKS_FROZEN) {
1807 case CPU_UP_PREPARE:
1808 power_pmu_setup(cpu);
1809 break;
1810
1811 default:
1812 break;
1813 }
1814
1815 return NOTIFY_OK;
1816}
1817
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001818int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001819{
1820 if (ppmu)
1821 return -EBUSY; /* something's already registered */
1822
1823 ppmu = pmu;
1824 pr_info("%s performance monitor hardware support registered\n",
1825 pmu->name);
1826
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001827 power_pmu.attr_groups = ppmu->attr_groups;
1828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829#ifdef MSR_HV
1830 /*
1831 * Use FCHV to ignore kernel events if MSR.HV is set.
1832 */
1833 if (mfmsr() & MSR_HV)
1834 freeze_events_kernel = MMCR0_FCHV;
1835#endif /* CONFIG_PPC64 */
1836
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001837 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001838 perf_cpu_notifier(power_pmu_notifier);
1839
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001840 return 0;
1841}