blob: 53ac1b20e14dbaf401c9159228d1cc2af2e10abd [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
2 * Performance event support - powerpc architecture code
3 *
4 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/kernel.h>
12#include <linux/sched.h>
13#include <linux/perf_event.h>
14#include <linux/percpu.h>
15#include <linux/hardirq.h>
Michael Neuling69123182013-05-13 18:44:58 +000016#include <linux/uaccess.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <asm/reg.h>
18#include <asm/pmc.h>
19#include <asm/machdep.h>
20#include <asm/firmware.h>
21#include <asm/ptrace.h>
Michael Neuling69123182013-05-13 18:44:58 +000022#include <asm/code-patching.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020023
Anshuman Khandual3925f462013-04-22 19:42:44 +000024#define BHRB_MAX_ENTRIES 32
25#define BHRB_TARGET 0x0000000000000002
26#define BHRB_PREDICTION 0x0000000000000001
Anton Blanchardb0d436c2013-08-07 02:01:24 +100027#define BHRB_EA 0xFFFFFFFFFFFFFFFCUL
Anshuman Khandual3925f462013-04-22 19:42:44 +000028
Ingo Molnarcdd6c482009-09-21 12:02:48 +020029struct cpu_hw_events {
30 int n_events;
31 int n_percpu;
32 int disabled;
33 int n_added;
34 int n_limited;
35 u8 pmcs_enabled;
36 struct perf_event *event[MAX_HWEVENTS];
37 u64 events[MAX_HWEVENTS];
38 unsigned int flags[MAX_HWEVENTS];
39 unsigned long mmcr[3];
Paul Mackerrasa8f90e92009-09-22 09:48:08 +100040 struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
41 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042 u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
43 unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
44 unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
Lin Ming8e6d5572010-05-08 20:28:41 +100045
46 unsigned int group_flag;
47 int n_txn_start;
Anshuman Khandual3925f462013-04-22 19:42:44 +000048
49 /* BHRB bits */
50 u64 bhrb_filter; /* BHRB HW branch filter */
51 int bhrb_users;
52 void *bhrb_context;
53 struct perf_branch_stack bhrb_stack;
54 struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
Ingo Molnarcdd6c482009-09-21 12:02:48 +020055};
Anshuman Khandual3925f462013-04-22 19:42:44 +000056
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
58
59struct power_pmu *ppmu;
60
61/*
Ingo Molnar57c0c152009-09-21 12:20:38 +020062 * Normally, to ignore kernel events we set the FCS (freeze counters
Ingo Molnarcdd6c482009-09-21 12:02:48 +020063 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
64 * hypervisor bit set in the MSR, or if we are running on a processor
65 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
66 * then we need to use the FCHV bit to ignore kernel events.
67 */
68static unsigned int freeze_events_kernel = MMCR0_FCS;
69
70/*
71 * 32-bit doesn't have MMCRA but does have an MMCR2,
72 * and a few other names are different.
73 */
74#ifdef CONFIG_PPC32
75
76#define MMCR0_FCHV 0
77#define MMCR0_PMCjCE MMCR0_PMCnCE
Michael Ellerman7a7a41f2013-06-28 18:15:12 +100078#define MMCR0_FC56 0
Michael Ellerman378a6ee2013-06-28 18:15:11 +100079#define MMCR0_PMAO 0
Michael Ellerman330a1eb2013-06-28 18:15:16 +100080#define MMCR0_EBE 0
81#define MMCR0_PMCC 0
82#define MMCR0_PMCC_U6 0
Ingo Molnarcdd6c482009-09-21 12:02:48 +020083
84#define SPRN_MMCRA SPRN_MMCR2
85#define MMCRA_SAMPLE_ENABLE 0
86
87static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
88{
89 return 0;
90}
91static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
92static inline u32 perf_get_misc_flags(struct pt_regs *regs)
93{
94 return 0;
95}
Anton Blanchard75382aa2012-06-26 01:01:36 +000096static inline void perf_read_regs(struct pt_regs *regs)
97{
98 regs->result = 0;
99}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200100static inline int perf_intr_is_nmi(struct pt_regs *regs)
101{
102 return 0;
103}
104
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000105static inline int siar_valid(struct pt_regs *regs)
106{
107 return 1;
108}
109
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000110static bool is_ebb_event(struct perf_event *event) { return false; }
111static int ebb_event_check(struct perf_event *event) { return 0; }
112static void ebb_event_add(struct perf_event *event) { }
113static void ebb_switch_out(unsigned long mmcr0) { }
114static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
115{
116 return mmcr0;
117}
118
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000119static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
120static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
121void power_pmu_flush_branch_stack(void) {}
122static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100123static void pmao_restore_workaround(bool ebb) { }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200124#endif /* CONFIG_PPC32 */
125
Michael Ellerman33904052013-04-25 19:28:25 +0000126static bool regs_use_siar(struct pt_regs *regs)
127{
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000128 return !!regs->result;
Michael Ellerman33904052013-04-25 19:28:25 +0000129}
130
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200131/*
132 * Things that are specific to 64-bit implementations.
133 */
134#ifdef CONFIG_PPC64
135
136static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
137{
138 unsigned long mmcra = regs->dsisr;
139
Michael Ellerman7a786832013-04-25 19:28:23 +0000140 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
142 if (slot > 1)
143 return 4 * (slot - 1);
144 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000145
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200146 return 0;
147}
148
149/*
150 * The user wants a data address recorded.
151 * If we're not doing instruction sampling, give them the SDAR
152 * (sampled data address). If we are doing instruction sampling, then
153 * only give them the SDAR if it corresponds to the instruction
Michael Ellerman58a032c2013-05-15 20:19:31 +0000154 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
155 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200156 */
157static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
158{
159 unsigned long mmcra = regs->dsisr;
Michael Ellerman58a032c2013-05-15 20:19:31 +0000160 bool sdar_valid;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000161
Michael Ellerman58a032c2013-05-15 20:19:31 +0000162 if (ppmu->flags & PPMU_HAS_SIER)
163 sdar_valid = regs->dar & SIER_SDAR_VALID;
164 else {
165 unsigned long sdsync;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200166
Michael Ellerman58a032c2013-05-15 20:19:31 +0000167 if (ppmu->flags & PPMU_SIAR_VALID)
168 sdsync = POWER7P_MMCRA_SDAR_VALID;
169 else if (ppmu->flags & PPMU_ALT_SIPR)
170 sdsync = POWER6_MMCRA_SDSYNC;
171 else
172 sdsync = MMCRA_SDSYNC;
173
174 sdar_valid = mmcra & sdsync;
175 }
176
177 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178 *addrp = mfspr(SPRN_SDAR);
179}
180
Michael Ellerman5682c462013-04-25 19:28:24 +0000181static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000182{
183 unsigned long sihv = MMCRA_SIHV;
184
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000185 if (ppmu->flags & PPMU_HAS_SIER)
186 return !!(regs->dar & SIER_SIHV);
187
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000188 if (ppmu->flags & PPMU_ALT_SIPR)
189 sihv = POWER6_MMCRA_SIHV;
190
Michael Ellerman5682c462013-04-25 19:28:24 +0000191 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000192}
193
Michael Ellerman5682c462013-04-25 19:28:24 +0000194static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000195{
196 unsigned long sipr = MMCRA_SIPR;
197
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000198 if (ppmu->flags & PPMU_HAS_SIER)
199 return !!(regs->dar & SIER_SIPR);
200
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000201 if (ppmu->flags & PPMU_ALT_SIPR)
202 sipr = POWER6_MMCRA_SIPR;
203
Michael Ellerman5682c462013-04-25 19:28:24 +0000204 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000205}
206
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000207static inline u32 perf_flags_from_msr(struct pt_regs *regs)
208{
209 if (regs->msr & MSR_PR)
210 return PERF_RECORD_MISC_USER;
211 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
212 return PERF_RECORD_MISC_HYPERVISOR;
213 return PERF_RECORD_MISC_KERNEL;
214}
215
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200216static inline u32 perf_get_misc_flags(struct pt_regs *regs)
217{
Michael Ellerman33904052013-04-25 19:28:25 +0000218 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200219
Anton Blanchard75382aa2012-06-26 01:01:36 +0000220 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000221 return perf_flags_from_msr(regs);
222
223 /*
224 * If we don't have flags in MMCRA, rather than using
225 * the MSR, we intuit the flags from the address in
226 * SIAR which should give slightly more reliable
227 * results
228 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000229 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000230 unsigned long siar = mfspr(SPRN_SIAR);
231 if (siar >= PAGE_OFFSET)
232 return PERF_RECORD_MISC_KERNEL;
233 return PERF_RECORD_MISC_USER;
234 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200235
Michael Neuling7abb8402009-10-14 19:32:15 +0000236 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000237 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000238 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000239
240 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200241 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000242
Michael Neuling7abb8402009-10-14 19:32:15 +0000243 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200244}
245
246/*
247 * Overload regs->dsisr to store MMCRA so we only need to read it once
248 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000249 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000250 * Overload regs->result to specify whether we should use the MSR (result
251 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200252 */
253static inline void perf_read_regs(struct pt_regs *regs)
254{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000255 unsigned long mmcra = mfspr(SPRN_MMCRA);
256 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
257 int use_siar;
258
Michael Ellerman5682c462013-04-25 19:28:24 +0000259 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000260
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000261 if (ppmu->flags & PPMU_HAS_SIER)
262 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000263
264 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000265 * If this isn't a PMU exception (eg a software event) the SIAR is
266 * not valid. Use pt_regs.
267 *
268 * If it is a marked event use the SIAR.
269 *
270 * If the PMU doesn't update the SIAR for non marked events use
271 * pt_regs.
272 *
273 * If the PMU has HV/PR flags then check to see if they
274 * place the exception in userspace. If so, use pt_regs. In
275 * continuous sampling mode the SIAR and the PMU exception are
276 * not synchronised, so they may be many instructions apart.
277 * This can result in confusing backtraces. We still want
278 * hypervisor samples as well as samples in the kernel with
279 * interrupts off hence the userspace check.
280 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000281 if (TRAP(regs) != 0xf00)
282 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000283 else if (marked)
284 use_siar = 1;
285 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
286 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000287 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000288 use_siar = 0;
289 else
290 use_siar = 1;
291
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000292 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293}
294
295/*
296 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
297 * it as an NMI.
298 */
299static inline int perf_intr_is_nmi(struct pt_regs *regs)
300{
301 return !regs->softe;
302}
303
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000304/*
305 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
306 * must be sampled only if the SIAR-valid bit is set.
307 *
308 * For unmarked instructions and for processors that don't have the SIAR-Valid
309 * bit, assume that SIAR is valid.
310 */
311static inline int siar_valid(struct pt_regs *regs)
312{
313 unsigned long mmcra = regs->dsisr;
314 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
315
Michael Ellerman58a032c2013-05-15 20:19:31 +0000316 if (marked) {
317 if (ppmu->flags & PPMU_HAS_SIER)
318 return regs->dar & SIER_SIAR_VALID;
319
320 if (ppmu->flags & PPMU_SIAR_VALID)
321 return mmcra & POWER7P_MMCRA_SIAR_VALID;
322 }
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000323
324 return 1;
325}
326
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000327
328/* Reset all possible BHRB entries */
329static void power_pmu_bhrb_reset(void)
330{
331 asm volatile(PPC_CLRBHRB);
332}
333
334static void power_pmu_bhrb_enable(struct perf_event *event)
335{
336 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
337
338 if (!ppmu->bhrb_nr)
339 return;
340
341 /* Clear BHRB if we changed task context to avoid data leaks */
342 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
343 power_pmu_bhrb_reset();
344 cpuhw->bhrb_context = event->ctx;
345 }
346 cpuhw->bhrb_users++;
347}
348
349static void power_pmu_bhrb_disable(struct perf_event *event)
350{
351 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
352
353 if (!ppmu->bhrb_nr)
354 return;
355
356 cpuhw->bhrb_users--;
357 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
358
359 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
360 /* BHRB cannot be turned off when other
361 * events are active on the PMU.
362 */
363
364 /* avoid stale pointer */
365 cpuhw->bhrb_context = NULL;
366 }
367}
368
369/* Called from ctxsw to prevent one process's branch entries to
370 * mingle with the other process's entries during context switch.
371 */
372void power_pmu_flush_branch_stack(void)
373{
374 if (ppmu->bhrb_nr)
375 power_pmu_bhrb_reset();
376}
Michael Neuling69123182013-05-13 18:44:58 +0000377/* Calculate the to address for a branch */
378static __u64 power_pmu_bhrb_to(u64 addr)
379{
380 unsigned int instr;
381 int ret;
382 __u64 target;
383
384 if (is_kernel_addr(addr))
385 return branch_target((unsigned int *)addr);
386
387 /* Userspace: need copy instruction here then translate it */
388 pagefault_disable();
389 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
390 if (ret) {
391 pagefault_enable();
392 return 0;
393 }
394 pagefault_enable();
395
396 target = branch_target(&instr);
397 if ((!target) || (instr & BRANCH_ABSOLUTE))
398 return target;
399
400 /* Translate relative branch target from kernel to user address */
401 return target - (unsigned long)&instr + addr;
402}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000403
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000404/* Processing BHRB entries */
Michael Neuling506e70d2013-05-13 18:44:57 +0000405void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000406{
407 u64 val;
408 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000409 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000410
411 r_index = 0;
412 u_index = 0;
413 while (r_index < ppmu->bhrb_nr) {
414 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000415 val = read_bhrb(r_index++);
416 if (!val)
417 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000418 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000419 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000420 addr = val & BHRB_EA;
421 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000422
Michael Neuling506e70d2013-05-13 18:44:57 +0000423 if (!addr)
424 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000425 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000426
Michael Neuling506e70d2013-05-13 18:44:57 +0000427 /* Branches are read most recent first (ie. mfbhrb 0 is
428 * the most recent branch).
429 * There are two types of valid entries:
430 * 1) a target entry which is the to address of a
431 * computed goto like a blr,bctr,btar. The next
432 * entry read from the bhrb will be branch
433 * corresponding to this target (ie. the actual
434 * blr/bctr/btar instruction).
435 * 2) a from address which is an actual branch. If a
436 * target entry proceeds this, then this is the
437 * matching branch for that target. If this is not
438 * following a target entry, then this is a branch
439 * where the target is given as an immediate field
440 * in the instruction (ie. an i or b form branch).
441 * In this case we need to read the instruction from
442 * memory to determine the target/to address.
443 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000444
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000445 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000446 /* Target branches use two entries
447 * (ie. computed gotos/XL form)
448 */
449 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000450 cpuhw->bhrb_entries[u_index].mispred = pred;
451 cpuhw->bhrb_entries[u_index].predicted = ~pred;
452
Michael Neuling506e70d2013-05-13 18:44:57 +0000453 /* Get from address in next entry */
454 val = read_bhrb(r_index++);
455 addr = val & BHRB_EA;
456 if (val & BHRB_TARGET) {
457 /* Shouldn't have two targets in a
458 row.. Reset index and try again */
459 r_index--;
460 addr = 0;
461 }
462 cpuhw->bhrb_entries[u_index].from = addr;
463 } else {
464 /* Branches to immediate field
465 (ie I or B form) */
466 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000467 cpuhw->bhrb_entries[u_index].to =
468 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000469 cpuhw->bhrb_entries[u_index].mispred = pred;
470 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000471 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000472 u_index++;
473
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000474 }
475 }
476 cpuhw->bhrb_stack.nr = u_index;
477 return;
478}
479
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000480static bool is_ebb_event(struct perf_event *event)
481{
482 /*
483 * This could be a per-PMU callback, but we'd rather avoid the cost. We
484 * check that the PMU supports EBB, meaning those that don't can still
485 * use bit 63 of the event code for something else if they wish.
486 */
487 return (ppmu->flags & PPMU_EBB) &&
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000488 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000489}
490
491static int ebb_event_check(struct perf_event *event)
492{
493 struct perf_event *leader = event->group_leader;
494
495 /* Event and group leader must agree on EBB */
496 if (is_ebb_event(leader) != is_ebb_event(event))
497 return -EINVAL;
498
499 if (is_ebb_event(event)) {
500 if (!(event->attach_state & PERF_ATTACH_TASK))
501 return -EINVAL;
502
503 if (!leader->attr.pinned || !leader->attr.exclusive)
504 return -EINVAL;
505
506 if (event->attr.inherit || event->attr.sample_period ||
507 event->attr.enable_on_exec || event->attr.freq)
508 return -EINVAL;
509 }
510
511 return 0;
512}
513
514static void ebb_event_add(struct perf_event *event)
515{
516 if (!is_ebb_event(event) || current->thread.used_ebb)
517 return;
518
519 /*
520 * IFF this is the first time we've added an EBB event, set
521 * PMXE in the user MMCR0 so we can detect when it's cleared by
522 * userspace. We need this so that we can context switch while
523 * userspace is in the EBB handler (where PMXE is 0).
524 */
525 current->thread.used_ebb = 1;
526 current->thread.mmcr0 |= MMCR0_PMXE;
527}
528
529static void ebb_switch_out(unsigned long mmcr0)
530{
531 if (!(mmcr0 & MMCR0_EBE))
532 return;
533
534 current->thread.siar = mfspr(SPRN_SIAR);
535 current->thread.sier = mfspr(SPRN_SIER);
536 current->thread.sdar = mfspr(SPRN_SDAR);
537 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
538 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
539}
540
541static unsigned long ebb_switch_in(bool ebb, unsigned long mmcr0)
542{
543 if (!ebb)
544 goto out;
545
546 /* Enable EBB and read/write to all 6 PMCs for userspace */
547 mmcr0 |= MMCR0_EBE | MMCR0_PMCC_U6;
548
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100549 /*
550 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
551 * with pmao_restore_workaround() because we may add PMAO but we never
552 * clear it here.
553 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000554 mmcr0 |= current->thread.mmcr0;
555
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100556 /*
557 * Be careful not to set PMXE if userspace had it cleared. This is also
558 * compatible with pmao_restore_workaround() because it has already
559 * cleared PMXE and we leave PMAO alone.
560 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000561 if (!(current->thread.mmcr0 & MMCR0_PMXE))
562 mmcr0 &= ~MMCR0_PMXE;
563
564 mtspr(SPRN_SIAR, current->thread.siar);
565 mtspr(SPRN_SIER, current->thread.sier);
566 mtspr(SPRN_SDAR, current->thread.sdar);
567 mtspr(SPRN_MMCR2, current->thread.mmcr2);
568out:
569 return mmcr0;
570}
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100571
572static void pmao_restore_workaround(bool ebb)
573{
574 unsigned pmcs[6];
575
576 if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
577 return;
578
579 /*
580 * On POWER8E there is a hardware defect which affects the PMU context
581 * switch logic, ie. power_pmu_disable/enable().
582 *
583 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
584 * by the hardware. Sometime later the actual PMU exception is
585 * delivered.
586 *
587 * If we context switch, or simply disable/enable, the PMU prior to the
588 * exception arriving, the exception will be lost when we clear PMAO.
589 *
590 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
591 * set, and this _should_ generate an exception. However because of the
592 * defect no exception is generated when we write PMAO, and we get
593 * stuck with no counters counting but no exception delivered.
594 *
595 * The workaround is to detect this case and tweak the hardware to
596 * create another pending PMU exception.
597 *
598 * We do that by setting up PMC6 (cycles) for an imminent overflow and
599 * enabling the PMU. That causes a new exception to be generated in the
600 * chip, but we don't take it yet because we have interrupts hard
601 * disabled. We then write back the PMU state as we want it to be seen
602 * by the exception handler. When we reenable interrupts the exception
603 * handler will be called and see the correct state.
604 *
605 * The logic is the same for EBB, except that the exception is gated by
606 * us having interrupts hard disabled as well as the fact that we are
607 * not in userspace. The exception is finally delivered when we return
608 * to userspace.
609 */
610
611 /* Only if PMAO is set and PMAO_SYNC is clear */
612 if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
613 return;
614
615 /* If we're doing EBB, only if BESCR[GE] is set */
616 if (ebb && !(current->thread.bescr & BESCR_GE))
617 return;
618
619 /*
620 * We are already soft-disabled in power_pmu_enable(). We need to hard
621 * enable to actually prevent the PMU exception from firing.
622 */
623 hard_irq_disable();
624
625 /*
626 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
627 * Using read/write_pmc() in a for loop adds 12 function calls and
628 * almost doubles our code size.
629 */
630 pmcs[0] = mfspr(SPRN_PMC1);
631 pmcs[1] = mfspr(SPRN_PMC2);
632 pmcs[2] = mfspr(SPRN_PMC3);
633 pmcs[3] = mfspr(SPRN_PMC4);
634 pmcs[4] = mfspr(SPRN_PMC5);
635 pmcs[5] = mfspr(SPRN_PMC6);
636
637 /* Ensure all freeze bits are unset */
638 mtspr(SPRN_MMCR2, 0);
639
640 /* Set up PMC6 to overflow in one cycle */
641 mtspr(SPRN_PMC6, 0x7FFFFFFE);
642
643 /* Enable exceptions and unfreeze PMC6 */
644 mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
645
646 /* Now we need to refreeze and restore the PMCs */
647 mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
648
649 mtspr(SPRN_PMC1, pmcs[0]);
650 mtspr(SPRN_PMC2, pmcs[1]);
651 mtspr(SPRN_PMC3, pmcs[2]);
652 mtspr(SPRN_PMC4, pmcs[3]);
653 mtspr(SPRN_PMC5, pmcs[4]);
654 mtspr(SPRN_PMC6, pmcs[5]);
655}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656#endif /* CONFIG_PPC64 */
657
658static void perf_event_interrupt(struct pt_regs *regs);
659
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200661 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200662 */
663static unsigned long read_pmc(int idx)
664{
665 unsigned long val;
666
667 switch (idx) {
668 case 1:
669 val = mfspr(SPRN_PMC1);
670 break;
671 case 2:
672 val = mfspr(SPRN_PMC2);
673 break;
674 case 3:
675 val = mfspr(SPRN_PMC3);
676 break;
677 case 4:
678 val = mfspr(SPRN_PMC4);
679 break;
680 case 5:
681 val = mfspr(SPRN_PMC5);
682 break;
683 case 6:
684 val = mfspr(SPRN_PMC6);
685 break;
686#ifdef CONFIG_PPC64
687 case 7:
688 val = mfspr(SPRN_PMC7);
689 break;
690 case 8:
691 val = mfspr(SPRN_PMC8);
692 break;
693#endif /* CONFIG_PPC64 */
694 default:
695 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
696 val = 0;
697 }
698 return val;
699}
700
701/*
702 * Write one PMC.
703 */
704static void write_pmc(int idx, unsigned long val)
705{
706 switch (idx) {
707 case 1:
708 mtspr(SPRN_PMC1, val);
709 break;
710 case 2:
711 mtspr(SPRN_PMC2, val);
712 break;
713 case 3:
714 mtspr(SPRN_PMC3, val);
715 break;
716 case 4:
717 mtspr(SPRN_PMC4, val);
718 break;
719 case 5:
720 mtspr(SPRN_PMC5, val);
721 break;
722 case 6:
723 mtspr(SPRN_PMC6, val);
724 break;
725#ifdef CONFIG_PPC64
726 case 7:
727 mtspr(SPRN_PMC7, val);
728 break;
729 case 8:
730 mtspr(SPRN_PMC8, val);
731 break;
732#endif /* CONFIG_PPC64 */
733 default:
734 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
735 }
736}
737
Anshuman Khandual5f6d0382014-03-14 16:00:27 +1100738/* Called from sysrq_handle_showregs() */
739void perf_event_print_debug(void)
740{
741 unsigned long sdar, sier, flags;
742 u32 pmcs[MAX_HWEVENTS];
743 int i;
744
745 if (!ppmu->n_counter)
746 return;
747
748 local_irq_save(flags);
749
750 pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
751 smp_processor_id(), ppmu->name, ppmu->n_counter);
752
753 for (i = 0; i < ppmu->n_counter; i++)
754 pmcs[i] = read_pmc(i + 1);
755
756 for (; i < MAX_HWEVENTS; i++)
757 pmcs[i] = 0xdeadbeef;
758
759 pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
760 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
761
762 if (ppmu->n_counter > 4)
763 pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
764 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
765
766 pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
767 mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
768
769 sdar = sier = 0;
770#ifdef CONFIG_PPC64
771 sdar = mfspr(SPRN_SDAR);
772
773 if (ppmu->flags & PPMU_HAS_SIER)
774 sier = mfspr(SPRN_SIER);
775
776 if (ppmu->flags & PPMU_EBB) {
777 pr_info("MMCR2: %016lx EBBHR: %016lx\n",
778 mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
779 pr_info("EBBRR: %016lx BESCR: %016lx\n",
780 mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
781 }
782#endif
783 pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
784 mfspr(SPRN_SIAR), sdar, sier);
785
786 local_irq_restore(flags);
787}
788
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200789/*
790 * Check if a set of events can all go on the PMU at once.
791 * If they can't, this will look at alternative codes for the events
792 * and see if any combination of alternative codes is feasible.
793 * The feasible set is returned in event_id[].
794 */
795static int power_check_constraints(struct cpu_hw_events *cpuhw,
796 u64 event_id[], unsigned int cflags[],
797 int n_ev)
798{
799 unsigned long mask, value, nv;
800 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
801 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
802 int i, j;
803 unsigned long addf = ppmu->add_fields;
804 unsigned long tadd = ppmu->test_adder;
805
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000806 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200807 return -1;
808
809 /* First see if the events will go on as-is */
810 for (i = 0; i < n_ev; ++i) {
811 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
812 && !ppmu->limited_pmc_event(event_id[i])) {
813 ppmu->get_alternatives(event_id[i], cflags[i],
814 cpuhw->alternatives[i]);
815 event_id[i] = cpuhw->alternatives[i][0];
816 }
817 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
818 &cpuhw->avalues[i][0]))
819 return -1;
820 }
821 value = mask = 0;
822 for (i = 0; i < n_ev; ++i) {
823 nv = (value | cpuhw->avalues[i][0]) +
824 (value & cpuhw->avalues[i][0] & addf);
825 if ((((nv + tadd) ^ value) & mask) != 0 ||
826 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
827 cpuhw->amasks[i][0]) != 0)
828 break;
829 value = nv;
830 mask |= cpuhw->amasks[i][0];
831 }
832 if (i == n_ev)
833 return 0; /* all OK */
834
835 /* doesn't work, gather alternatives... */
836 if (!ppmu->get_alternatives)
837 return -1;
838 for (i = 0; i < n_ev; ++i) {
839 choice[i] = 0;
840 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
841 cpuhw->alternatives[i]);
842 for (j = 1; j < n_alt[i]; ++j)
843 ppmu->get_constraint(cpuhw->alternatives[i][j],
844 &cpuhw->amasks[i][j],
845 &cpuhw->avalues[i][j]);
846 }
847
848 /* enumerate all possibilities and see if any will work */
849 i = 0;
850 j = -1;
851 value = mask = nv = 0;
852 while (i < n_ev) {
853 if (j >= 0) {
854 /* we're backtracking, restore context */
855 value = svalues[i];
856 mask = smasks[i];
857 j = choice[i];
858 }
859 /*
860 * See if any alternative k for event_id i,
861 * where k > j, will satisfy the constraints.
862 */
863 while (++j < n_alt[i]) {
864 nv = (value | cpuhw->avalues[i][j]) +
865 (value & cpuhw->avalues[i][j] & addf);
866 if ((((nv + tadd) ^ value) & mask) == 0 &&
867 (((nv + tadd) ^ cpuhw->avalues[i][j])
868 & cpuhw->amasks[i][j]) == 0)
869 break;
870 }
871 if (j >= n_alt[i]) {
872 /*
873 * No feasible alternative, backtrack
874 * to event_id i-1 and continue enumerating its
875 * alternatives from where we got up to.
876 */
877 if (--i < 0)
878 return -1;
879 } else {
880 /*
881 * Found a feasible alternative for event_id i,
882 * remember where we got up to with this event_id,
883 * go on to the next event_id, and start with
884 * the first alternative for it.
885 */
886 choice[i] = j;
887 svalues[i] = value;
888 smasks[i] = mask;
889 value = nv;
890 mask |= cpuhw->amasks[i][j];
891 ++i;
892 j = -1;
893 }
894 }
895
896 /* OK, we have a feasible combination, tell the caller the solution */
897 for (i = 0; i < n_ev; ++i)
898 event_id[i] = cpuhw->alternatives[i][choice[i]];
899 return 0;
900}
901
902/*
903 * Check if newly-added events have consistent settings for
904 * exclude_{user,kernel,hv} with each other and any previously
905 * added events.
906 */
907static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
908 int n_prev, int n_new)
909{
910 int eu = 0, ek = 0, eh = 0;
911 int i, n, first;
912 struct perf_event *event;
913
914 n = n_prev + n_new;
915 if (n <= 1)
916 return 0;
917
918 first = 1;
919 for (i = 0; i < n; ++i) {
920 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
921 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
922 continue;
923 }
924 event = ctrs[i];
925 if (first) {
926 eu = event->attr.exclude_user;
927 ek = event->attr.exclude_kernel;
928 eh = event->attr.exclude_hv;
929 first = 0;
930 } else if (event->attr.exclude_user != eu ||
931 event->attr.exclude_kernel != ek ||
932 event->attr.exclude_hv != eh) {
933 return -EAGAIN;
934 }
935 }
936
937 if (eu || ek || eh)
938 for (i = 0; i < n; ++i)
939 if (cflags[i] & PPMU_LIMITED_PMC_OK)
940 cflags[i] |= PPMU_LIMITED_PMC_REQD;
941
942 return 0;
943}
944
Eric B Munson86c74ab2011-04-15 08:12:30 +0000945static u64 check_and_compute_delta(u64 prev, u64 val)
946{
947 u64 delta = (val - prev) & 0xfffffffful;
948
949 /*
950 * POWER7 can roll back counter values, if the new value is smaller
951 * than the previous value it will cause the delta and the counter to
952 * have bogus values unless we rolled a counter over. If a coutner is
953 * rolled back, it will be smaller, but within 256, which is the maximum
954 * number of events to rollback at once. If we dectect a rollback
955 * return 0. This can lead to a small lack of precision in the
956 * counters.
957 */
958 if (prev > val && (prev - val) < 256)
959 delta = 0;
960
961 return delta;
962}
963
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200964static void power_pmu_read(struct perf_event *event)
965{
966 s64 val, delta, prev;
967
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200968 if (event->hw.state & PERF_HES_STOPPED)
969 return;
970
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200971 if (!event->hw.idx)
972 return;
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000973
974 if (is_ebb_event(event)) {
975 val = read_pmc(event->hw.idx);
976 local64_set(&event->hw.prev_count, val);
977 return;
978 }
979
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200980 /*
981 * Performance monitor interrupts come even when interrupts
982 * are soft-disabled, as long as interrupts are hard-enabled.
983 * Therefore we treat them like NMIs.
984 */
985 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +0200986 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987 barrier();
988 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +0000989 delta = check_and_compute_delta(prev, val);
990 if (!delta)
991 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200992 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200993
Peter Zijlstrae7850592010-05-21 14:43:08 +0200994 local64_add(delta, &event->count);
995 local64_sub(delta, &event->hw.period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200996}
997
998/*
999 * On some machines, PMC5 and PMC6 can't be written, don't respect
1000 * the freeze conditions, and don't generate interrupts. This tells
1001 * us if `event' is using such a PMC.
1002 */
1003static int is_limited_pmc(int pmcnum)
1004{
1005 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1006 && (pmcnum == 5 || pmcnum == 6);
1007}
1008
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001009static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001010 unsigned long pmc5, unsigned long pmc6)
1011{
1012 struct perf_event *event;
1013 u64 val, prev, delta;
1014 int i;
1015
1016 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001017 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 if (!event->hw.idx)
1019 continue;
1020 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001021 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001023 delta = check_and_compute_delta(prev, val);
1024 if (delta)
1025 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026 }
1027}
1028
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001029static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001030 unsigned long pmc5, unsigned long pmc6)
1031{
1032 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001033 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001034 int i;
1035
1036 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001037 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038 event->hw.idx = cpuhw->limited_hwidx[i];
1039 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001040 prev = local64_read(&event->hw.prev_count);
1041 if (check_and_compute_delta(prev, val))
1042 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001043 perf_event_update_userpage(event);
1044 }
1045}
1046
1047/*
1048 * Since limited events don't respect the freeze conditions, we
1049 * have to read them immediately after freezing or unfreezing the
1050 * other events. We try to keep the values from the limited
1051 * events as consistent as possible by keeping the delay (in
1052 * cycles and instructions) between freezing/unfreezing and reading
1053 * the limited events as small and consistent as possible.
1054 * Therefore, if any limited events are in use, we read them
1055 * both, and always in the same order, to minimize variability,
1056 * and do it inside the same asm that writes MMCR0.
1057 */
1058static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1059{
1060 unsigned long pmc5, pmc6;
1061
1062 if (!cpuhw->n_limited) {
1063 mtspr(SPRN_MMCR0, mmcr0);
1064 return;
1065 }
1066
1067 /*
1068 * Write MMCR0, then read PMC5 and PMC6 immediately.
1069 * To ensure we don't get a performance monitor interrupt
1070 * between writing MMCR0 and freezing/thawing the limited
1071 * events, we first write MMCR0 with the event overflow
1072 * interrupt enable bits turned off.
1073 */
1074 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1075 : "=&r" (pmc5), "=&r" (pmc6)
1076 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1077 "i" (SPRN_MMCR0),
1078 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1079
1080 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001081 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001083 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001084
1085 /*
1086 * Write the full MMCR0 including the event overflow interrupt
1087 * enable bits, if necessary.
1088 */
1089 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1090 mtspr(SPRN_MMCR0, mmcr0);
1091}
1092
1093/*
1094 * Disable all events to prevent PMU interrupts and to allow
1095 * events to be added or removed.
1096 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001097static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098{
1099 struct cpu_hw_events *cpuhw;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001100 unsigned long flags, mmcr0, val;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001101
1102 if (!ppmu)
1103 return;
1104 local_irq_save(flags);
1105 cpuhw = &__get_cpu_var(cpu_hw_events);
1106
1107 if (!cpuhw->disabled) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001108 /*
1109 * Check if we ever enabled the PMU on this cpu.
1110 */
1111 if (!cpuhw->pmcs_enabled) {
1112 ppc_enable_pmcs();
1113 cpuhw->pmcs_enabled = 1;
1114 }
1115
1116 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001117 * Set the 'freeze counters' bit, clear EBE/PMCC/PMAO/FC56.
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001118 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001119 val = mmcr0 = mfspr(SPRN_MMCR0);
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001120 val |= MMCR0_FC;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001121 val &= ~(MMCR0_EBE | MMCR0_PMCC | MMCR0_PMAO | MMCR0_FC56);
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001122
1123 /*
1124 * The barrier is to make sure the mtspr has been
1125 * executed and the PMU has frozen the events etc.
1126 * before we return.
1127 */
1128 write_mmcr0(cpuhw, val);
1129 mb();
1130
1131 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132 * Disable instruction sampling if it was enabled
1133 */
1134 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1135 mtspr(SPRN_MMCRA,
1136 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1137 mb();
1138 }
1139
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001140 cpuhw->disabled = 1;
1141 cpuhw->n_added = 0;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001142
1143 ebb_switch_out(mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001145
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001146 local_irq_restore(flags);
1147}
1148
1149/*
1150 * Re-enable all events if disable == 0.
1151 * If we were previously disabled and events were added, then
1152 * put the new config on the PMU.
1153 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001154static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001155{
1156 struct perf_event *event;
1157 struct cpu_hw_events *cpuhw;
1158 unsigned long flags;
1159 long i;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001160 unsigned long val, mmcr0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001161 s64 left;
1162 unsigned int hwc_index[MAX_HWEVENTS];
1163 int n_lim;
1164 int idx;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001165 bool ebb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166
1167 if (!ppmu)
1168 return;
1169 local_irq_save(flags);
Michael Ellerman0a488432013-06-28 18:15:13 +10001170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171 cpuhw = &__get_cpu_var(cpu_hw_events);
Michael Ellerman0a488432013-06-28 18:15:13 +10001172 if (!cpuhw->disabled)
1173 goto out;
1174
Michael Ellerman4ea355b2013-06-28 18:15:14 +10001175 if (cpuhw->n_events == 0) {
1176 ppc_set_pmu_inuse(0);
1177 goto out;
1178 }
1179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001180 cpuhw->disabled = 0;
1181
1182 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001183 * EBB requires an exclusive group and all events must have the EBB
1184 * flag set, or not set, so we can just check a single event. Also we
1185 * know we have at least one event.
1186 */
1187 ebb = is_ebb_event(cpuhw->event[0]);
1188
1189 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001190 * If we didn't change anything, or only removed events,
1191 * no need to recalculate MMCR* settings and reset the PMCs.
1192 * Just reenable the PMU with the current MMCR* settings
1193 * (possibly updated for removal of events).
1194 */
1195 if (!cpuhw->n_added) {
1196 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1197 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198 goto out_enable;
1199 }
1200
1201 /*
1202 * Compute MMCR* values for the new set of events
1203 */
1204 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
1205 cpuhw->mmcr)) {
1206 /* shouldn't ever get here */
1207 printk(KERN_ERR "oops compute_mmcr failed\n");
1208 goto out;
1209 }
1210
1211 /*
1212 * Add in MMCR0 freeze bits corresponding to the
1213 * attr.exclude_* bits for the first event.
1214 * We have already checked that all events have the
1215 * same values for these bits as the first event.
1216 */
1217 event = cpuhw->event[0];
1218 if (event->attr.exclude_user)
1219 cpuhw->mmcr[0] |= MMCR0_FCP;
1220 if (event->attr.exclude_kernel)
1221 cpuhw->mmcr[0] |= freeze_events_kernel;
1222 if (event->attr.exclude_hv)
1223 cpuhw->mmcr[0] |= MMCR0_FCHV;
1224
1225 /*
1226 * Write the new configuration to MMCR* with the freeze
1227 * bit set and set the hardware events to their initial values.
1228 * Then unfreeze the events.
1229 */
1230 ppc_set_pmu_inuse(1);
1231 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1232 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1233 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1234 | MMCR0_FC);
1235
1236 /*
1237 * Read off any pre-existing events that need to move
1238 * to another PMC.
1239 */
1240 for (i = 0; i < cpuhw->n_events; ++i) {
1241 event = cpuhw->event[i];
1242 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1243 power_pmu_read(event);
1244 write_pmc(event->hw.idx, 0);
1245 event->hw.idx = 0;
1246 }
1247 }
1248
1249 /*
1250 * Initialize the PMCs for all the new and moved events.
1251 */
1252 cpuhw->n_limited = n_lim = 0;
1253 for (i = 0; i < cpuhw->n_events; ++i) {
1254 event = cpuhw->event[i];
1255 if (event->hw.idx)
1256 continue;
1257 idx = hwc_index[i] + 1;
1258 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001259 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260 cpuhw->limited_hwidx[n_lim] = idx;
1261 ++n_lim;
1262 continue;
1263 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001264
1265 if (ebb)
1266 val = local64_read(&event->hw.prev_count);
1267 else {
1268 val = 0;
1269 if (event->hw.sample_period) {
1270 left = local64_read(&event->hw.period_left);
1271 if (left < 0x80000000L)
1272 val = 0x80000000L - left;
1273 }
1274 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001275 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001277 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001278 if (event->hw.state & PERF_HES_STOPPED)
1279 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001280 write_pmc(idx, val);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001282 perf_event_update_userpage(event);
1283 }
1284 cpuhw->n_limited = n_lim;
1285 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1286
1287 out_enable:
Michael Ellermanc2e37a22014-03-14 16:00:29 +11001288 pmao_restore_workaround(ebb);
1289
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001290 mmcr0 = ebb_switch_in(ebb, cpuhw->mmcr[0]);
1291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001292 mb();
Anshuman Khandualb4d6c062013-12-18 13:14:53 +11001293 if (cpuhw->bhrb_users)
1294 ppmu->config_bhrb(cpuhw->bhrb_filter);
1295
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001296 write_mmcr0(cpuhw, mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297
1298 /*
1299 * Enable instruction sampling if necessary
1300 */
1301 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1302 mb();
1303 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1304 }
1305
1306 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 local_irq_restore(flags);
1309}
1310
1311static int collect_events(struct perf_event *group, int max_count,
1312 struct perf_event *ctrs[], u64 *events,
1313 unsigned int *flags)
1314{
1315 int n = 0;
1316 struct perf_event *event;
1317
1318 if (!is_software_event(group)) {
1319 if (n >= max_count)
1320 return -1;
1321 ctrs[n] = group;
1322 flags[n] = group->hw.event_base;
1323 events[n++] = group->hw.config;
1324 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001325 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326 if (!is_software_event(event) &&
1327 event->state != PERF_EVENT_STATE_OFF) {
1328 if (n >= max_count)
1329 return -1;
1330 ctrs[n] = event;
1331 flags[n] = event->hw.event_base;
1332 events[n++] = event->hw.config;
1333 }
1334 }
1335 return n;
1336}
1337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338/*
1339 * Add a event to the PMU.
1340 * If all events are not already frozen, then we disable and
1341 * re-enable the PMU in order to get hw_perf_enable to do the
1342 * actual work of reconfiguring the PMU.
1343 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001344static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345{
1346 struct cpu_hw_events *cpuhw;
1347 unsigned long flags;
1348 int n0;
1349 int ret = -EAGAIN;
1350
1351 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001352 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001353
1354 /*
1355 * Add the event to the list (if there is room)
1356 * and check whether the total set is still feasible.
1357 */
1358 cpuhw = &__get_cpu_var(cpu_hw_events);
1359 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001360 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 goto out;
1362 cpuhw->event[n0] = event;
1363 cpuhw->events[n0] = event->hw.config;
1364 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001365
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001366 /*
1367 * This event may have been disabled/stopped in record_and_restart()
1368 * because we exceeded the ->event_limit. If re-starting the event,
1369 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1370 * notification is re-enabled.
1371 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001372 if (!(ef_flags & PERF_EF_START))
1373 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001374 else
1375 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001376
Lin Ming8e6d5572010-05-08 20:28:41 +10001377 /*
1378 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001379 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001380 * at commit time(->commit_txn) as a whole
1381 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001382 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001383 goto nocheck;
1384
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1386 goto out;
1387 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1388 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001390
1391nocheck:
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001392 ebb_event_add(event);
1393
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394 ++cpuhw->n_events;
1395 ++cpuhw->n_added;
1396
1397 ret = 0;
1398 out:
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301399 if (has_branch_stack(event)) {
Anshuman Khandual3925f462013-04-22 19:42:44 +00001400 power_pmu_bhrb_enable(event);
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301401 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1402 event->attr.branch_sample_type);
1403 }
Anshuman Khandual3925f462013-04-22 19:42:44 +00001404
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001405 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406 local_irq_restore(flags);
1407 return ret;
1408}
1409
1410/*
1411 * Remove a event from the PMU.
1412 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001413static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414{
1415 struct cpu_hw_events *cpuhw;
1416 long i;
1417 unsigned long flags;
1418
1419 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001420 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421
1422 power_pmu_read(event);
1423
1424 cpuhw = &__get_cpu_var(cpu_hw_events);
1425 for (i = 0; i < cpuhw->n_events; ++i) {
1426 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001427 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001429 cpuhw->events[i-1] = cpuhw->events[i];
1430 cpuhw->flags[i-1] = cpuhw->flags[i];
1431 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001432 --cpuhw->n_events;
1433 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1434 if (event->hw.idx) {
1435 write_pmc(event->hw.idx, 0);
1436 event->hw.idx = 0;
1437 }
1438 perf_event_update_userpage(event);
1439 break;
1440 }
1441 }
1442 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001443 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444 break;
1445 if (i < cpuhw->n_limited) {
1446 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001447 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001448 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1449 }
1450 --cpuhw->n_limited;
1451 }
1452 if (cpuhw->n_events == 0) {
1453 /* disable exceptions if no events are running */
1454 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1455 }
1456
Anshuman Khandual3925f462013-04-22 19:42:44 +00001457 if (has_branch_stack(event))
1458 power_pmu_bhrb_disable(event);
1459
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001460 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001461 local_irq_restore(flags);
1462}
1463
1464/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001465 * POWER-PMU does not support disabling individual counters, hence
1466 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001467 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001468
1469static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001471 unsigned long flags;
1472 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001473 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001474
1475 if (!event->hw.idx || !event->hw.sample_period)
1476 return;
1477
1478 if (!(event->hw.state & PERF_HES_STOPPED))
1479 return;
1480
1481 if (ef_flags & PERF_EF_RELOAD)
1482 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1483
1484 local_irq_save(flags);
1485 perf_pmu_disable(event->pmu);
1486
1487 event->hw.state = 0;
1488 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001489
1490 val = 0;
1491 if (left < 0x80000000L)
1492 val = 0x80000000L - left;
1493
1494 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001495
1496 perf_event_update_userpage(event);
1497 perf_pmu_enable(event->pmu);
1498 local_irq_restore(flags);
1499}
1500
1501static void power_pmu_stop(struct perf_event *event, int ef_flags)
1502{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001503 unsigned long flags;
1504
1505 if (!event->hw.idx || !event->hw.sample_period)
1506 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001507
1508 if (event->hw.state & PERF_HES_STOPPED)
1509 return;
1510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001512 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001513
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001514 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001515 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1516 write_pmc(event->hw.idx, 0);
1517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001519 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001520 local_irq_restore(flags);
1521}
1522
Lin Ming8e6d5572010-05-08 20:28:41 +10001523/*
1524 * Start group events scheduling transaction
1525 * Set the flag to make pmu::enable() not perform the
1526 * schedulability test, it will be performed at commit time
1527 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001528void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001529{
1530 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1531
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001532 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001533 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001534 cpuhw->n_txn_start = cpuhw->n_events;
1535}
1536
1537/*
1538 * Stop group events scheduling transaction
1539 * Clear the flag and pmu::enable() will perform the
1540 * schedulability test.
1541 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001542void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001543{
1544 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1545
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001546 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001547 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001548}
1549
1550/*
1551 * Commit group events scheduling transaction
1552 * Perform the group schedulability test as a whole
1553 * Return 0 if success
1554 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001555int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001556{
1557 struct cpu_hw_events *cpuhw;
1558 long i, n;
1559
1560 if (!ppmu)
1561 return -EAGAIN;
1562 cpuhw = &__get_cpu_var(cpu_hw_events);
1563 n = cpuhw->n_events;
1564 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1565 return -EAGAIN;
1566 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1567 if (i < 0)
1568 return -EAGAIN;
1569
1570 for (i = cpuhw->n_txn_start; i < n; ++i)
1571 cpuhw->event[i]->hw.config = cpuhw->events[i];
1572
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001573 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001574 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001575 return 0;
1576}
1577
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578/*
1579 * Return 1 if we might be able to put event on a limited PMC,
1580 * or 0 if not.
1581 * A event can only go on a limited PMC if it counts something
1582 * that a limited PMC can count, doesn't require interrupts, and
1583 * doesn't exclude any processor mode.
1584 */
1585static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1586 unsigned int flags)
1587{
1588 int n;
1589 u64 alt[MAX_EVENT_ALTERNATIVES];
1590
1591 if (event->attr.exclude_user
1592 || event->attr.exclude_kernel
1593 || event->attr.exclude_hv
1594 || event->attr.sample_period)
1595 return 0;
1596
1597 if (ppmu->limited_pmc_event(ev))
1598 return 1;
1599
1600 /*
1601 * The requested event_id isn't on a limited PMC already;
1602 * see if any alternative code goes on a limited PMC.
1603 */
1604 if (!ppmu->get_alternatives)
1605 return 0;
1606
1607 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1608 n = ppmu->get_alternatives(ev, flags, alt);
1609
1610 return n > 0;
1611}
1612
1613/*
1614 * Find an alternative event_id that goes on a normal PMC, if possible,
1615 * and return the event_id code, or 0 if there is no such alternative.
1616 * (Note: event_id code 0 is "don't count" on all machines.)
1617 */
1618static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1619{
1620 u64 alt[MAX_EVENT_ALTERNATIVES];
1621 int n;
1622
1623 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1624 n = ppmu->get_alternatives(ev, flags, alt);
1625 if (!n)
1626 return 0;
1627 return alt[0];
1628}
1629
1630/* Number of perf_events counting hardware events */
1631static atomic_t num_events;
1632/* Used to avoid races in calling reserve/release_pmc_hardware */
1633static DEFINE_MUTEX(pmc_reserve_mutex);
1634
1635/*
1636 * Release the PMU if this is the last perf_event.
1637 */
1638static void hw_perf_event_destroy(struct perf_event *event)
1639{
1640 if (!atomic_add_unless(&num_events, -1, 1)) {
1641 mutex_lock(&pmc_reserve_mutex);
1642 if (atomic_dec_return(&num_events) == 0)
1643 release_pmc_hardware();
1644 mutex_unlock(&pmc_reserve_mutex);
1645 }
1646}
1647
1648/*
1649 * Translate a generic cache event_id config to a raw event_id code.
1650 */
1651static int hw_perf_cache_event(u64 config, u64 *eventp)
1652{
1653 unsigned long type, op, result;
1654 int ev;
1655
1656 if (!ppmu->cache_events)
1657 return -EINVAL;
1658
1659 /* unpack config */
1660 type = config & 0xff;
1661 op = (config >> 8) & 0xff;
1662 result = (config >> 16) & 0xff;
1663
1664 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1665 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1666 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1667 return -EINVAL;
1668
1669 ev = (*ppmu->cache_events)[type][op][result];
1670 if (ev == 0)
1671 return -EOPNOTSUPP;
1672 if (ev == -1)
1673 return -EINVAL;
1674 *eventp = ev;
1675 return 0;
1676}
1677
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001678static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679{
1680 u64 ev;
1681 unsigned long flags;
1682 struct perf_event *ctrs[MAX_HWEVENTS];
1683 u64 events[MAX_HWEVENTS];
1684 unsigned int cflags[MAX_HWEVENTS];
1685 int n;
1686 int err;
1687 struct cpu_hw_events *cpuhw;
1688
1689 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001690 return -ENOENT;
1691
Anshuman Khandual3925f462013-04-22 19:42:44 +00001692 if (has_branch_stack(event)) {
1693 /* PMU has BHRB enabled */
1694 if (!(ppmu->flags & PPMU_BHRB))
1695 return -EOPNOTSUPP;
1696 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001698 switch (event->attr.type) {
1699 case PERF_TYPE_HARDWARE:
1700 ev = event->attr.config;
1701 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001702 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703 ev = ppmu->generic_events[ev];
1704 break;
1705 case PERF_TYPE_HW_CACHE:
1706 err = hw_perf_cache_event(event->attr.config, &ev);
1707 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001708 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709 break;
1710 case PERF_TYPE_RAW:
1711 ev = event->attr.config;
1712 break;
1713 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001714 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001716
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717 event->hw.config_base = ev;
1718 event->hw.idx = 0;
1719
1720 /*
1721 * If we are not running on a hypervisor, force the
1722 * exclude_hv bit to 0 so that we don't care what
1723 * the user set it to.
1724 */
1725 if (!firmware_has_feature(FW_FEATURE_LPAR))
1726 event->attr.exclude_hv = 0;
1727
1728 /*
1729 * If this is a per-task event, then we can use
1730 * PM_RUN_* events interchangeably with their non RUN_*
1731 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1732 * XXX we should check if the task is an idle task.
1733 */
1734 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001735 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001736 flags |= PPMU_ONLY_COUNT_RUN;
1737
1738 /*
1739 * If this machine has limited events, check whether this
1740 * event_id could go on a limited event.
1741 */
1742 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1743 if (can_go_on_limited_pmc(event, ev, flags)) {
1744 flags |= PPMU_LIMITED_PMC_OK;
1745 } else if (ppmu->limited_pmc_event(ev)) {
1746 /*
1747 * The requested event_id is on a limited PMC,
1748 * but we can't use a limited PMC; see if any
1749 * alternative goes on a normal PMC.
1750 */
1751 ev = normal_pmc_alternative(ev, flags);
1752 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001753 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001754 }
1755 }
1756
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001757 /* Extra checks for EBB */
1758 err = ebb_event_check(event);
1759 if (err)
1760 return err;
1761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001762 /*
1763 * If this is in a group, check if it can go on with all the
1764 * other hardware events in the group. We assume the event
1765 * hasn't been linked into its leader's sibling list at this point.
1766 */
1767 n = 0;
1768 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001769 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 ctrs, events, cflags);
1771 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001772 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773 }
1774 events[n] = ev;
1775 ctrs[n] = event;
1776 cflags[n] = flags;
1777 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001778 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779
1780 cpuhw = &get_cpu_var(cpu_hw_events);
1781 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001782
1783 if (has_branch_stack(event)) {
1784 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1785 event->attr.branch_sample_type);
1786
1787 if(cpuhw->bhrb_filter == -1)
1788 return -EOPNOTSUPP;
1789 }
1790
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001791 put_cpu_var(cpu_hw_events);
1792 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001793 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794
1795 event->hw.config = events[n];
1796 event->hw.event_base = cflags[n];
1797 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001798 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799
1800 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001801 * For EBB events we just context switch the PMC value, we don't do any
1802 * of the sample_period logic. We use hw.prev_count for this.
1803 */
1804 if (is_ebb_event(event))
1805 local64_set(&event->hw.prev_count, 0);
1806
1807 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001808 * See if we need to reserve the PMU.
1809 * If no events are currently in use, then we have to take a
1810 * mutex to ensure that we don't race with another task doing
1811 * reserve_pmc_hardware or release_pmc_hardware.
1812 */
1813 err = 0;
1814 if (!atomic_inc_not_zero(&num_events)) {
1815 mutex_lock(&pmc_reserve_mutex);
1816 if (atomic_read(&num_events) == 0 &&
1817 reserve_pmc_hardware(perf_event_interrupt))
1818 err = -EBUSY;
1819 else
1820 atomic_inc(&num_events);
1821 mutex_unlock(&pmc_reserve_mutex);
1822 }
1823 event->destroy = hw_perf_event_destroy;
1824
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001825 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826}
1827
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001828static int power_pmu_event_idx(struct perf_event *event)
1829{
1830 return event->hw.idx;
1831}
1832
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001833ssize_t power_events_sysfs_show(struct device *dev,
1834 struct device_attribute *attr, char *page)
1835{
1836 struct perf_pmu_events_attr *pmu_attr;
1837
1838 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1839
1840 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1841}
1842
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001843struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001844 .pmu_enable = power_pmu_enable,
1845 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001846 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001847 .add = power_pmu_add,
1848 .del = power_pmu_del,
1849 .start = power_pmu_start,
1850 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001851 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001852 .start_txn = power_pmu_start_txn,
1853 .cancel_txn = power_pmu_cancel_txn,
1854 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001855 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001856 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001857};
1858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001860 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001861 * things if requested. Note that interrupts are hard-disabled
1862 * here so there is no possibility of being interrupted.
1863 */
1864static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001865 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001866{
1867 u64 period = event->hw.sample_period;
1868 s64 prev, delta, left;
1869 int record = 0;
1870
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001871 if (event->hw.state & PERF_HES_STOPPED) {
1872 write_pmc(event->hw.idx, 0);
1873 return;
1874 }
1875
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001877 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001878 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001879 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001880
1881 /*
1882 * See if the total period for this event has expired,
1883 * and update for the next period.
1884 */
1885 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001886 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001887 if (delta == 0)
1888 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889 if (period) {
1890 if (left <= 0) {
1891 left += period;
1892 if (left <= 0)
1893 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001894 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001895 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896 }
1897 if (left < 0x80000000LL)
1898 val = 0x80000000LL - left;
1899 }
1900
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001901 write_pmc(event->hw.idx, val);
1902 local64_set(&event->hw.prev_count, val);
1903 local64_set(&event->hw.period_left, left);
1904 perf_event_update_userpage(event);
1905
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001906 /*
1907 * Finally record data if requested.
1908 */
1909 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001910 struct perf_sample_data data;
1911
Robert Richterfd0d0002012-04-02 20:19:08 +02001912 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001913
1914 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1915 perf_get_data_addr(regs, &data.addr);
1916
Anshuman Khandual3925f462013-04-22 19:42:44 +00001917 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1918 struct cpu_hw_events *cpuhw;
1919 cpuhw = &__get_cpu_var(cpu_hw_events);
1920 power_pmu_bhrb_read(cpuhw);
1921 data.br_stack = &cpuhw->bhrb_stack;
1922 }
1923
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001924 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001925 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001927}
1928
1929/*
1930 * Called from generic code to get the misc flags (i.e. processor mode)
1931 * for an event_id.
1932 */
1933unsigned long perf_misc_flags(struct pt_regs *regs)
1934{
1935 u32 flags = perf_get_misc_flags(regs);
1936
1937 if (flags)
1938 return flags;
1939 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1940 PERF_RECORD_MISC_KERNEL;
1941}
1942
1943/*
1944 * Called from generic code to get the instruction pointer
1945 * for an event_id.
1946 */
1947unsigned long perf_instruction_pointer(struct pt_regs *regs)
1948{
Michael Ellerman33904052013-04-25 19:28:25 +00001949 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001950
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001951 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00001952 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001953 else if (use_siar)
1954 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00001955 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00001956 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957}
1958
Michael Neulingbc09c212012-11-05 15:53:54 +00001959static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11001960{
Anton Blanchard0837e322011-03-09 14:38:42 +11001961 /*
1962 * Events on POWER7 can roll back if a speculative event doesn't
1963 * eventually complete. Unfortunately in some rare cases they will
1964 * raise a performance monitor exception. We need to catch this to
1965 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1966 * cycles from overflow.
1967 *
1968 * We only do this if the first pass fails to find any overflowing
1969 * PMCs because a user might set a period of less than 256 and we
1970 * don't want to mistakenly reset them.
1971 */
Michael Neulingbc09c212012-11-05 15:53:54 +00001972 if ((0x80000000 - val) <= 256)
1973 return true;
1974
1975 return false;
1976}
1977
1978static bool pmc_overflow(unsigned long val)
1979{
1980 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11001981 return true;
1982
1983 return false;
1984}
1985
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986/*
1987 * Performance monitor interrupt stuff
1988 */
1989static void perf_event_interrupt(struct pt_regs *regs)
1990{
Michael Neulingbc09c212012-11-05 15:53:54 +00001991 int i, j;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001992 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1993 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00001994 unsigned long val[8];
1995 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996 int nmi;
1997
1998 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001999 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000 mfspr(SPRN_PMC6));
2001
2002 perf_read_regs(regs);
2003
2004 nmi = perf_intr_is_nmi(regs);
2005 if (nmi)
2006 nmi_enter();
2007 else
2008 irq_enter();
2009
Michael Neulingbc09c212012-11-05 15:53:54 +00002010 /* Read all the PMCs since we'll need them a bunch of times */
2011 for (i = 0; i < ppmu->n_counter; ++i)
2012 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013
Michael Neulingbc09c212012-11-05 15:53:54 +00002014 /* Try to find what caused the IRQ */
2015 found = 0;
2016 for (i = 0; i < ppmu->n_counter; ++i) {
2017 if (!pmc_overflow(val[i]))
2018 continue;
2019 if (is_limited_pmc(i + 1))
2020 continue; /* these won't generate IRQs */
2021 /*
2022 * We've found one that's overflowed. For active
2023 * counters we need to log this. For inactive
2024 * counters, we need to reset it anyway
2025 */
2026 found = 1;
2027 active = 0;
2028 for (j = 0; j < cpuhw->n_events; ++j) {
2029 event = cpuhw->event[j];
2030 if (event->hw.idx == (i + 1)) {
2031 active = 1;
2032 record_and_restart(event, val[i], regs);
2033 break;
2034 }
2035 }
2036 if (!active)
2037 /* reset non active counters that have overflowed */
2038 write_pmc(i + 1, 0);
2039 }
2040 if (!found && pvr_version_is(PVR_POWER7)) {
2041 /* check active counters for special buggy p7 overflow */
2042 for (i = 0; i < cpuhw->n_events; ++i) {
2043 event = cpuhw->event[i];
2044 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00002046 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2047 /* event has overflowed in a buggy way*/
2048 found = 1;
2049 record_and_restart(event,
2050 val[event->hw.idx - 1],
2051 regs);
2052 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053 }
2054 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00002055 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00002056 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002057
2058 /*
2059 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02002060 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061 * and thus allow interrupts to occur again.
2062 * XXX might want to use MSR.PM to keep the events frozen until
2063 * we get back out of this interrupt.
2064 */
2065 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
2066
2067 if (nmi)
2068 nmi_exit();
2069 else
2070 irq_exit();
2071}
2072
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002073static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074{
2075 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2076
2077 if (!ppmu)
2078 return;
2079 memset(cpuhw, 0, sizeof(*cpuhw));
2080 cpuhw->mmcr[0] = MMCR0_FC;
2081}
2082
Paul Gortmaker061d19f2013-06-24 15:30:09 -04002083static int
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01002084power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002085{
2086 unsigned int cpu = (long)hcpu;
2087
2088 switch (action & ~CPU_TASKS_FROZEN) {
2089 case CPU_UP_PREPARE:
2090 power_pmu_setup(cpu);
2091 break;
2092
2093 default:
2094 break;
2095 }
2096
2097 return NOTIFY_OK;
2098}
2099
Paul Gortmaker061d19f2013-06-24 15:30:09 -04002100int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101{
2102 if (ppmu)
2103 return -EBUSY; /* something's already registered */
2104
2105 ppmu = pmu;
2106 pr_info("%s performance monitor hardware support registered\n",
2107 pmu->name);
2108
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08002109 power_pmu.attr_groups = ppmu->attr_groups;
2110
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111#ifdef MSR_HV
2112 /*
2113 * Use FCHV to ignore kernel events if MSR.HV is set.
2114 */
2115 if (mfmsr() & MSR_HV)
2116 freeze_events_kernel = MMCR0_FCHV;
2117#endif /* CONFIG_PPC64 */
2118
Peter Zijlstra2e80a822010-11-17 23:17:36 +01002119 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002120 perf_cpu_notifier(power_pmu_notifier);
2121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002122 return 0;
2123}