blob: 1bb26d586e3c7a3e55d9dcd07cfa6d051e9b7984 [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
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200929 cpuhw->disabled = 0;
930
931 /*
932 * If we didn't change anything, or only removed events,
933 * no need to recalculate MMCR* settings and reset the PMCs.
934 * Just reenable the PMU with the current MMCR* settings
935 * (possibly updated for removal of events).
936 */
937 if (!cpuhw->n_added) {
938 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
939 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
940 if (cpuhw->n_events == 0)
941 ppc_set_pmu_inuse(0);
942 goto out_enable;
943 }
944
945 /*
946 * Compute MMCR* values for the new set of events
947 */
948 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
949 cpuhw->mmcr)) {
950 /* shouldn't ever get here */
951 printk(KERN_ERR "oops compute_mmcr failed\n");
952 goto out;
953 }
954
955 /*
956 * Add in MMCR0 freeze bits corresponding to the
957 * attr.exclude_* bits for the first event.
958 * We have already checked that all events have the
959 * same values for these bits as the first event.
960 */
961 event = cpuhw->event[0];
962 if (event->attr.exclude_user)
963 cpuhw->mmcr[0] |= MMCR0_FCP;
964 if (event->attr.exclude_kernel)
965 cpuhw->mmcr[0] |= freeze_events_kernel;
966 if (event->attr.exclude_hv)
967 cpuhw->mmcr[0] |= MMCR0_FCHV;
968
969 /*
970 * Write the new configuration to MMCR* with the freeze
971 * bit set and set the hardware events to their initial values.
972 * Then unfreeze the events.
973 */
974 ppc_set_pmu_inuse(1);
975 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
976 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
977 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
978 | MMCR0_FC);
979
980 /*
981 * Read off any pre-existing events that need to move
982 * to another PMC.
983 */
984 for (i = 0; i < cpuhw->n_events; ++i) {
985 event = cpuhw->event[i];
986 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
987 power_pmu_read(event);
988 write_pmc(event->hw.idx, 0);
989 event->hw.idx = 0;
990 }
991 }
992
993 /*
994 * Initialize the PMCs for all the new and moved events.
995 */
996 cpuhw->n_limited = n_lim = 0;
997 for (i = 0; i < cpuhw->n_events; ++i) {
998 event = cpuhw->event[i];
999 if (event->hw.idx)
1000 continue;
1001 idx = hwc_index[i] + 1;
1002 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001003 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001004 cpuhw->limited_hwidx[n_lim] = idx;
1005 ++n_lim;
1006 continue;
1007 }
1008 val = 0;
1009 if (event->hw.sample_period) {
Peter Zijlstrae7850592010-05-21 14:43:08 +02001010 left = local64_read(&event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001011 if (left < 0x80000000L)
1012 val = 0x80000000L - left;
1013 }
Peter Zijlstrae7850592010-05-21 14:43:08 +02001014 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001015 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001016 if (event->hw.state & PERF_HES_STOPPED)
1017 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 write_pmc(idx, val);
1019 perf_event_update_userpage(event);
1020 }
1021 cpuhw->n_limited = n_lim;
1022 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1023
1024 out_enable:
1025 mb();
1026 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1027
1028 /*
1029 * Enable instruction sampling if necessary
1030 */
1031 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1032 mb();
1033 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1034 }
1035
1036 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001037 if (cpuhw->bhrb_users)
1038 ppmu->config_bhrb(cpuhw->bhrb_filter);
1039
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040 local_irq_restore(flags);
1041}
1042
1043static int collect_events(struct perf_event *group, int max_count,
1044 struct perf_event *ctrs[], u64 *events,
1045 unsigned int *flags)
1046{
1047 int n = 0;
1048 struct perf_event *event;
1049
1050 if (!is_software_event(group)) {
1051 if (n >= max_count)
1052 return -1;
1053 ctrs[n] = group;
1054 flags[n] = group->hw.event_base;
1055 events[n++] = group->hw.config;
1056 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001057 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001058 if (!is_software_event(event) &&
1059 event->state != PERF_EVENT_STATE_OFF) {
1060 if (n >= max_count)
1061 return -1;
1062 ctrs[n] = event;
1063 flags[n] = event->hw.event_base;
1064 events[n++] = event->hw.config;
1065 }
1066 }
1067 return n;
1068}
1069
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001070/*
1071 * Add a event to the PMU.
1072 * If all events are not already frozen, then we disable and
1073 * re-enable the PMU in order to get hw_perf_enable to do the
1074 * actual work of reconfiguring the PMU.
1075 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001076static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001077{
1078 struct cpu_hw_events *cpuhw;
1079 unsigned long flags;
1080 int n0;
1081 int ret = -EAGAIN;
1082
1083 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001084 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001085
1086 /*
1087 * Add the event to the list (if there is room)
1088 * and check whether the total set is still feasible.
1089 */
1090 cpuhw = &__get_cpu_var(cpu_hw_events);
1091 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001092 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001093 goto out;
1094 cpuhw->event[n0] = event;
1095 cpuhw->events[n0] = event->hw.config;
1096 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001097
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001098 /*
1099 * This event may have been disabled/stopped in record_and_restart()
1100 * because we exceeded the ->event_limit. If re-starting the event,
1101 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1102 * notification is re-enabled.
1103 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001104 if (!(ef_flags & PERF_EF_START))
1105 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001106 else
1107 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001108
Lin Ming8e6d5572010-05-08 20:28:41 +10001109 /*
1110 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001111 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001112 * at commit time(->commit_txn) as a whole
1113 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001114 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001115 goto nocheck;
1116
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1118 goto out;
1119 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1120 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001122
1123nocheck:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001124 ++cpuhw->n_events;
1125 ++cpuhw->n_added;
1126
1127 ret = 0;
1128 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001129 if (has_branch_stack(event))
1130 power_pmu_bhrb_enable(event);
1131
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001132 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001133 local_irq_restore(flags);
1134 return ret;
1135}
1136
1137/*
1138 * Remove a event from the PMU.
1139 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001140static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141{
1142 struct cpu_hw_events *cpuhw;
1143 long i;
1144 unsigned long flags;
1145
1146 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001147 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001148
1149 power_pmu_read(event);
1150
1151 cpuhw = &__get_cpu_var(cpu_hw_events);
1152 for (i = 0; i < cpuhw->n_events; ++i) {
1153 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001154 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001155 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001156 cpuhw->events[i-1] = cpuhw->events[i];
1157 cpuhw->flags[i-1] = cpuhw->flags[i];
1158 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001159 --cpuhw->n_events;
1160 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1161 if (event->hw.idx) {
1162 write_pmc(event->hw.idx, 0);
1163 event->hw.idx = 0;
1164 }
1165 perf_event_update_userpage(event);
1166 break;
1167 }
1168 }
1169 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001170 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171 break;
1172 if (i < cpuhw->n_limited) {
1173 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001174 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1176 }
1177 --cpuhw->n_limited;
1178 }
1179 if (cpuhw->n_events == 0) {
1180 /* disable exceptions if no events are running */
1181 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1182 }
1183
Anshuman Khandual3925f462013-04-22 19:42:44 +00001184 if (has_branch_stack(event))
1185 power_pmu_bhrb_disable(event);
1186
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001187 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188 local_irq_restore(flags);
1189}
1190
1191/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001192 * POWER-PMU does not support disabling individual counters, hence
1193 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001194 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001195
1196static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001198 unsigned long flags;
1199 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001200 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001201
1202 if (!event->hw.idx || !event->hw.sample_period)
1203 return;
1204
1205 if (!(event->hw.state & PERF_HES_STOPPED))
1206 return;
1207
1208 if (ef_flags & PERF_EF_RELOAD)
1209 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1210
1211 local_irq_save(flags);
1212 perf_pmu_disable(event->pmu);
1213
1214 event->hw.state = 0;
1215 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001216
1217 val = 0;
1218 if (left < 0x80000000L)
1219 val = 0x80000000L - left;
1220
1221 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001222
1223 perf_event_update_userpage(event);
1224 perf_pmu_enable(event->pmu);
1225 local_irq_restore(flags);
1226}
1227
1228static void power_pmu_stop(struct perf_event *event, int ef_flags)
1229{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001230 unsigned long flags;
1231
1232 if (!event->hw.idx || !event->hw.sample_period)
1233 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001234
1235 if (event->hw.state & PERF_HES_STOPPED)
1236 return;
1237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001239 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001240
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001242 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1243 write_pmc(event->hw.idx, 0);
1244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001245 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001246 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247 local_irq_restore(flags);
1248}
1249
Lin Ming8e6d5572010-05-08 20:28:41 +10001250/*
1251 * Start group events scheduling transaction
1252 * Set the flag to make pmu::enable() not perform the
1253 * schedulability test, it will be performed at commit time
1254 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001255void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001256{
1257 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1258
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001259 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001260 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001261 cpuhw->n_txn_start = cpuhw->n_events;
1262}
1263
1264/*
1265 * Stop group events scheduling transaction
1266 * Clear the flag and pmu::enable() will perform the
1267 * schedulability test.
1268 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001269void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001270{
1271 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1272
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001273 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001274 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001275}
1276
1277/*
1278 * Commit group events scheduling transaction
1279 * Perform the group schedulability test as a whole
1280 * Return 0 if success
1281 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001282int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001283{
1284 struct cpu_hw_events *cpuhw;
1285 long i, n;
1286
1287 if (!ppmu)
1288 return -EAGAIN;
1289 cpuhw = &__get_cpu_var(cpu_hw_events);
1290 n = cpuhw->n_events;
1291 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1292 return -EAGAIN;
1293 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1294 if (i < 0)
1295 return -EAGAIN;
1296
1297 for (i = cpuhw->n_txn_start; i < n; ++i)
1298 cpuhw->event[i]->hw.config = cpuhw->events[i];
1299
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001300 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001301 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001302 return 0;
1303}
1304
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001305/*
1306 * Return 1 if we might be able to put event on a limited PMC,
1307 * or 0 if not.
1308 * A event can only go on a limited PMC if it counts something
1309 * that a limited PMC can count, doesn't require interrupts, and
1310 * doesn't exclude any processor mode.
1311 */
1312static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1313 unsigned int flags)
1314{
1315 int n;
1316 u64 alt[MAX_EVENT_ALTERNATIVES];
1317
1318 if (event->attr.exclude_user
1319 || event->attr.exclude_kernel
1320 || event->attr.exclude_hv
1321 || event->attr.sample_period)
1322 return 0;
1323
1324 if (ppmu->limited_pmc_event(ev))
1325 return 1;
1326
1327 /*
1328 * The requested event_id isn't on a limited PMC already;
1329 * see if any alternative code goes on a limited PMC.
1330 */
1331 if (!ppmu->get_alternatives)
1332 return 0;
1333
1334 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1335 n = ppmu->get_alternatives(ev, flags, alt);
1336
1337 return n > 0;
1338}
1339
1340/*
1341 * Find an alternative event_id that goes on a normal PMC, if possible,
1342 * and return the event_id code, or 0 if there is no such alternative.
1343 * (Note: event_id code 0 is "don't count" on all machines.)
1344 */
1345static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1346{
1347 u64 alt[MAX_EVENT_ALTERNATIVES];
1348 int n;
1349
1350 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1351 n = ppmu->get_alternatives(ev, flags, alt);
1352 if (!n)
1353 return 0;
1354 return alt[0];
1355}
1356
1357/* Number of perf_events counting hardware events */
1358static atomic_t num_events;
1359/* Used to avoid races in calling reserve/release_pmc_hardware */
1360static DEFINE_MUTEX(pmc_reserve_mutex);
1361
1362/*
1363 * Release the PMU if this is the last perf_event.
1364 */
1365static void hw_perf_event_destroy(struct perf_event *event)
1366{
1367 if (!atomic_add_unless(&num_events, -1, 1)) {
1368 mutex_lock(&pmc_reserve_mutex);
1369 if (atomic_dec_return(&num_events) == 0)
1370 release_pmc_hardware();
1371 mutex_unlock(&pmc_reserve_mutex);
1372 }
1373}
1374
1375/*
1376 * Translate a generic cache event_id config to a raw event_id code.
1377 */
1378static int hw_perf_cache_event(u64 config, u64 *eventp)
1379{
1380 unsigned long type, op, result;
1381 int ev;
1382
1383 if (!ppmu->cache_events)
1384 return -EINVAL;
1385
1386 /* unpack config */
1387 type = config & 0xff;
1388 op = (config >> 8) & 0xff;
1389 result = (config >> 16) & 0xff;
1390
1391 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1392 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1393 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1394 return -EINVAL;
1395
1396 ev = (*ppmu->cache_events)[type][op][result];
1397 if (ev == 0)
1398 return -EOPNOTSUPP;
1399 if (ev == -1)
1400 return -EINVAL;
1401 *eventp = ev;
1402 return 0;
1403}
1404
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001405static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406{
1407 u64 ev;
1408 unsigned long flags;
1409 struct perf_event *ctrs[MAX_HWEVENTS];
1410 u64 events[MAX_HWEVENTS];
1411 unsigned int cflags[MAX_HWEVENTS];
1412 int n;
1413 int err;
1414 struct cpu_hw_events *cpuhw;
1415
1416 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001417 return -ENOENT;
1418
Anshuman Khandual3925f462013-04-22 19:42:44 +00001419 if (has_branch_stack(event)) {
1420 /* PMU has BHRB enabled */
1421 if (!(ppmu->flags & PPMU_BHRB))
1422 return -EOPNOTSUPP;
1423 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001424
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425 switch (event->attr.type) {
1426 case PERF_TYPE_HARDWARE:
1427 ev = event->attr.config;
1428 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001429 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430 ev = ppmu->generic_events[ev];
1431 break;
1432 case PERF_TYPE_HW_CACHE:
1433 err = hw_perf_cache_event(event->attr.config, &ev);
1434 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001435 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001436 break;
1437 case PERF_TYPE_RAW:
1438 ev = event->attr.config;
1439 break;
1440 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001441 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001443
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444 event->hw.config_base = ev;
1445 event->hw.idx = 0;
1446
1447 /*
1448 * If we are not running on a hypervisor, force the
1449 * exclude_hv bit to 0 so that we don't care what
1450 * the user set it to.
1451 */
1452 if (!firmware_has_feature(FW_FEATURE_LPAR))
1453 event->attr.exclude_hv = 0;
1454
1455 /*
1456 * If this is a per-task event, then we can use
1457 * PM_RUN_* events interchangeably with their non RUN_*
1458 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1459 * XXX we should check if the task is an idle task.
1460 */
1461 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001462 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 flags |= PPMU_ONLY_COUNT_RUN;
1464
1465 /*
1466 * If this machine has limited events, check whether this
1467 * event_id could go on a limited event.
1468 */
1469 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1470 if (can_go_on_limited_pmc(event, ev, flags)) {
1471 flags |= PPMU_LIMITED_PMC_OK;
1472 } else if (ppmu->limited_pmc_event(ev)) {
1473 /*
1474 * The requested event_id is on a limited PMC,
1475 * but we can't use a limited PMC; see if any
1476 * alternative goes on a normal PMC.
1477 */
1478 ev = normal_pmc_alternative(ev, flags);
1479 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001480 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001481 }
1482 }
1483
1484 /*
1485 * If this is in a group, check if it can go on with all the
1486 * other hardware events in the group. We assume the event
1487 * hasn't been linked into its leader's sibling list at this point.
1488 */
1489 n = 0;
1490 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001491 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 ctrs, events, cflags);
1493 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001494 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 }
1496 events[n] = ev;
1497 ctrs[n] = event;
1498 cflags[n] = flags;
1499 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001500 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001501
1502 cpuhw = &get_cpu_var(cpu_hw_events);
1503 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001504
1505 if (has_branch_stack(event)) {
1506 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1507 event->attr.branch_sample_type);
1508
1509 if(cpuhw->bhrb_filter == -1)
1510 return -EOPNOTSUPP;
1511 }
1512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513 put_cpu_var(cpu_hw_events);
1514 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001515 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516
1517 event->hw.config = events[n];
1518 event->hw.event_base = cflags[n];
1519 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001520 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001521
1522 /*
1523 * See if we need to reserve the PMU.
1524 * If no events are currently in use, then we have to take a
1525 * mutex to ensure that we don't race with another task doing
1526 * reserve_pmc_hardware or release_pmc_hardware.
1527 */
1528 err = 0;
1529 if (!atomic_inc_not_zero(&num_events)) {
1530 mutex_lock(&pmc_reserve_mutex);
1531 if (atomic_read(&num_events) == 0 &&
1532 reserve_pmc_hardware(perf_event_interrupt))
1533 err = -EBUSY;
1534 else
1535 atomic_inc(&num_events);
1536 mutex_unlock(&pmc_reserve_mutex);
1537 }
1538 event->destroy = hw_perf_event_destroy;
1539
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001540 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001541}
1542
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001543static int power_pmu_event_idx(struct perf_event *event)
1544{
1545 return event->hw.idx;
1546}
1547
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001548ssize_t power_events_sysfs_show(struct device *dev,
1549 struct device_attribute *attr, char *page)
1550{
1551 struct perf_pmu_events_attr *pmu_attr;
1552
1553 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1554
1555 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1556}
1557
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001558struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001559 .pmu_enable = power_pmu_enable,
1560 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001561 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001562 .add = power_pmu_add,
1563 .del = power_pmu_del,
1564 .start = power_pmu_start,
1565 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001566 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001567 .start_txn = power_pmu_start_txn,
1568 .cancel_txn = power_pmu_cancel_txn,
1569 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001570 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001571 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001572};
1573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001575 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 * things if requested. Note that interrupts are hard-disabled
1577 * here so there is no possibility of being interrupted.
1578 */
1579static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001580 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581{
1582 u64 period = event->hw.sample_period;
1583 s64 prev, delta, left;
1584 int record = 0;
1585
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001586 if (event->hw.state & PERF_HES_STOPPED) {
1587 write_pmc(event->hw.idx, 0);
1588 return;
1589 }
1590
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001591 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001592 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001593 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001594 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595
1596 /*
1597 * See if the total period for this event has expired,
1598 * and update for the next period.
1599 */
1600 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001601 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001602 if (delta == 0)
1603 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604 if (period) {
1605 if (left <= 0) {
1606 left += period;
1607 if (left <= 0)
1608 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001609 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001610 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611 }
1612 if (left < 0x80000000LL)
1613 val = 0x80000000LL - left;
1614 }
1615
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001616 write_pmc(event->hw.idx, val);
1617 local64_set(&event->hw.prev_count, val);
1618 local64_set(&event->hw.period_left, left);
1619 perf_event_update_userpage(event);
1620
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001621 /*
1622 * Finally record data if requested.
1623 */
1624 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001625 struct perf_sample_data data;
1626
Robert Richterfd0d0002012-04-02 20:19:08 +02001627 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628
1629 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1630 perf_get_data_addr(regs, &data.addr);
1631
Anshuman Khandual3925f462013-04-22 19:42:44 +00001632 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1633 struct cpu_hw_events *cpuhw;
1634 cpuhw = &__get_cpu_var(cpu_hw_events);
1635 power_pmu_bhrb_read(cpuhw);
1636 data.br_stack = &cpuhw->bhrb_stack;
1637 }
1638
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001639 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001640 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001641 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642}
1643
1644/*
1645 * Called from generic code to get the misc flags (i.e. processor mode)
1646 * for an event_id.
1647 */
1648unsigned long perf_misc_flags(struct pt_regs *regs)
1649{
1650 u32 flags = perf_get_misc_flags(regs);
1651
1652 if (flags)
1653 return flags;
1654 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1655 PERF_RECORD_MISC_KERNEL;
1656}
1657
1658/*
1659 * Called from generic code to get the instruction pointer
1660 * for an event_id.
1661 */
1662unsigned long perf_instruction_pointer(struct pt_regs *regs)
1663{
Michael Ellerman33904052013-04-25 19:28:25 +00001664 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001665
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001666 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00001667 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001668 else if (use_siar)
1669 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00001670 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00001671 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001672}
1673
Michael Neulingbc09c212012-11-05 15:53:54 +00001674static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11001675{
Anton Blanchard0837e322011-03-09 14:38:42 +11001676 /*
1677 * Events on POWER7 can roll back if a speculative event doesn't
1678 * eventually complete. Unfortunately in some rare cases they will
1679 * raise a performance monitor exception. We need to catch this to
1680 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1681 * cycles from overflow.
1682 *
1683 * We only do this if the first pass fails to find any overflowing
1684 * PMCs because a user might set a period of less than 256 and we
1685 * don't want to mistakenly reset them.
1686 */
Michael Neulingbc09c212012-11-05 15:53:54 +00001687 if ((0x80000000 - val) <= 256)
1688 return true;
1689
1690 return false;
1691}
1692
1693static bool pmc_overflow(unsigned long val)
1694{
1695 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11001696 return true;
1697
1698 return false;
1699}
1700
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701/*
1702 * Performance monitor interrupt stuff
1703 */
1704static void perf_event_interrupt(struct pt_regs *regs)
1705{
Michael Neulingbc09c212012-11-05 15:53:54 +00001706 int i, j;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001707 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1708 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00001709 unsigned long val[8];
1710 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711 int nmi;
1712
1713 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001714 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 mfspr(SPRN_PMC6));
1716
1717 perf_read_regs(regs);
1718
1719 nmi = perf_intr_is_nmi(regs);
1720 if (nmi)
1721 nmi_enter();
1722 else
1723 irq_enter();
1724
Michael Neulingbc09c212012-11-05 15:53:54 +00001725 /* Read all the PMCs since we'll need them a bunch of times */
1726 for (i = 0; i < ppmu->n_counter; ++i)
1727 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728
Michael Neulingbc09c212012-11-05 15:53:54 +00001729 /* Try to find what caused the IRQ */
1730 found = 0;
1731 for (i = 0; i < ppmu->n_counter; ++i) {
1732 if (!pmc_overflow(val[i]))
1733 continue;
1734 if (is_limited_pmc(i + 1))
1735 continue; /* these won't generate IRQs */
1736 /*
1737 * We've found one that's overflowed. For active
1738 * counters we need to log this. For inactive
1739 * counters, we need to reset it anyway
1740 */
1741 found = 1;
1742 active = 0;
1743 for (j = 0; j < cpuhw->n_events; ++j) {
1744 event = cpuhw->event[j];
1745 if (event->hw.idx == (i + 1)) {
1746 active = 1;
1747 record_and_restart(event, val[i], regs);
1748 break;
1749 }
1750 }
1751 if (!active)
1752 /* reset non active counters that have overflowed */
1753 write_pmc(i + 1, 0);
1754 }
1755 if (!found && pvr_version_is(PVR_POWER7)) {
1756 /* check active counters for special buggy p7 overflow */
1757 for (i = 0; i < cpuhw->n_events; ++i) {
1758 event = cpuhw->event[i];
1759 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00001761 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
1762 /* event has overflowed in a buggy way*/
1763 found = 1;
1764 record_and_restart(event,
1765 val[event->hw.idx - 1],
1766 regs);
1767 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001768 }
1769 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00001770 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00001771 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772
1773 /*
1774 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02001775 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001776 * and thus allow interrupts to occur again.
1777 * XXX might want to use MSR.PM to keep the events frozen until
1778 * we get back out of this interrupt.
1779 */
1780 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1781
1782 if (nmi)
1783 nmi_exit();
1784 else
1785 irq_exit();
1786}
1787
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001788static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789{
1790 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
1791
1792 if (!ppmu)
1793 return;
1794 memset(cpuhw, 0, sizeof(*cpuhw));
1795 cpuhw->mmcr[0] = MMCR0_FC;
1796}
1797
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001798static int
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01001799power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001800{
1801 unsigned int cpu = (long)hcpu;
1802
1803 switch (action & ~CPU_TASKS_FROZEN) {
1804 case CPU_UP_PREPARE:
1805 power_pmu_setup(cpu);
1806 break;
1807
1808 default:
1809 break;
1810 }
1811
1812 return NOTIFY_OK;
1813}
1814
Paul Gortmaker061d19f2013-06-24 15:30:09 -04001815int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001816{
1817 if (ppmu)
1818 return -EBUSY; /* something's already registered */
1819
1820 ppmu = pmu;
1821 pr_info("%s performance monitor hardware support registered\n",
1822 pmu->name);
1823
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001824 power_pmu.attr_groups = ppmu->attr_groups;
1825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826#ifdef MSR_HV
1827 /*
1828 * Use FCHV to ignore kernel events if MSR.HV is set.
1829 */
1830 if (mfmsr() & MSR_HV)
1831 freeze_events_kernel = MMCR0_FCHV;
1832#endif /* CONFIG_PPC64 */
1833
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001834 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001835 perf_cpu_notifier(power_pmu_notifier);
1836
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837 return 0;
1838}