blob: b101c0b6daccbf4ba4354e1a53b462dbf5ab562d [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];
Michael Ellerman9de5cb02014-07-23 21:12:38 +100039 /*
40 * The order of the MMCR array is:
41 * - 64-bit, MMCR0, MMCR1, MMCRA, MMCR2
42 * - 32-bit, MMCR0, MMCR1, MMCR2
43 */
44 unsigned long mmcr[4];
Paul Mackerrasa8f90e92009-09-22 09:48:08 +100045 struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
46 u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047 u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
48 unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
49 unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
Lin Ming8e6d5572010-05-08 20:28:41 +100050
51 unsigned int group_flag;
52 int n_txn_start;
Anshuman Khandual3925f462013-04-22 19:42:44 +000053
54 /* BHRB bits */
55 u64 bhrb_filter; /* BHRB HW branch filter */
56 int bhrb_users;
57 void *bhrb_context;
58 struct perf_branch_stack bhrb_stack;
59 struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
Ingo Molnarcdd6c482009-09-21 12:02:48 +020060};
Anshuman Khandual3925f462013-04-22 19:42:44 +000061
Anton Blancharde51df2c2014-08-20 08:55:18 +100062static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020063
Anton Blancharde51df2c2014-08-20 08:55:18 +100064static struct power_pmu *ppmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020065
66/*
Ingo Molnar57c0c152009-09-21 12:20:38 +020067 * Normally, to ignore kernel events we set the FCS (freeze counters
Ingo Molnarcdd6c482009-09-21 12:02:48 +020068 * in supervisor mode) bit in MMCR0, but if the kernel runs with the
69 * hypervisor bit set in the MSR, or if we are running on a processor
70 * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
71 * then we need to use the FCHV bit to ignore kernel events.
72 */
73static unsigned int freeze_events_kernel = MMCR0_FCS;
74
75/*
76 * 32-bit doesn't have MMCRA but does have an MMCR2,
77 * and a few other names are different.
78 */
79#ifdef CONFIG_PPC32
80
81#define MMCR0_FCHV 0
82#define MMCR0_PMCjCE MMCR0_PMCnCE
Michael Ellerman7a7a41f2013-06-28 18:15:12 +100083#define MMCR0_FC56 0
Michael Ellerman378a6ee2013-06-28 18:15:11 +100084#define MMCR0_PMAO 0
Michael Ellerman330a1eb2013-06-28 18:15:16 +100085#define MMCR0_EBE 0
Michael Ellerman76cb8a72014-03-14 16:00:34 +110086#define MMCR0_BHRBA 0
Michael Ellerman330a1eb2013-06-28 18:15:16 +100087#define MMCR0_PMCC 0
88#define MMCR0_PMCC_U6 0
Ingo Molnarcdd6c482009-09-21 12:02:48 +020089
90#define SPRN_MMCRA SPRN_MMCR2
91#define MMCRA_SAMPLE_ENABLE 0
92
93static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
94{
95 return 0;
96}
97static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
98static inline u32 perf_get_misc_flags(struct pt_regs *regs)
99{
100 return 0;
101}
Anton Blanchard75382aa2012-06-26 01:01:36 +0000102static inline void perf_read_regs(struct pt_regs *regs)
103{
104 regs->result = 0;
105}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200106static inline int perf_intr_is_nmi(struct pt_regs *regs)
107{
108 return 0;
109}
110
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000111static inline int siar_valid(struct pt_regs *regs)
112{
113 return 1;
114}
115
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000116static bool is_ebb_event(struct perf_event *event) { return false; }
117static int ebb_event_check(struct perf_event *event) { return 0; }
118static void ebb_event_add(struct perf_event *event) { }
119static void ebb_switch_out(unsigned long mmcr0) { }
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000120static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000121{
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000122 return cpuhw->mmcr[0];
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000123}
124
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000125static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
126static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
Anton Blancharde51df2c2014-08-20 08:55:18 +1000127static void power_pmu_flush_branch_stack(void) {}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000128static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100129static void pmao_restore_workaround(bool ebb) { }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200130#endif /* CONFIG_PPC32 */
131
Michael Ellerman33904052013-04-25 19:28:25 +0000132static bool regs_use_siar(struct pt_regs *regs)
133{
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000134 return !!regs->result;
Michael Ellerman33904052013-04-25 19:28:25 +0000135}
136
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200137/*
138 * Things that are specific to 64-bit implementations.
139 */
140#ifdef CONFIG_PPC64
141
142static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
143{
144 unsigned long mmcra = regs->dsisr;
145
Michael Ellerman7a786832013-04-25 19:28:23 +0000146 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200147 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
148 if (slot > 1)
149 return 4 * (slot - 1);
150 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000151
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152 return 0;
153}
154
155/*
156 * The user wants a data address recorded.
157 * If we're not doing instruction sampling, give them the SDAR
158 * (sampled data address). If we are doing instruction sampling, then
159 * only give them the SDAR if it corresponds to the instruction
Michael Ellerman58a032c2013-05-15 20:19:31 +0000160 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
161 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162 */
163static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
164{
165 unsigned long mmcra = regs->dsisr;
Michael Ellerman58a032c2013-05-15 20:19:31 +0000166 bool sdar_valid;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000167
Michael Ellerman58a032c2013-05-15 20:19:31 +0000168 if (ppmu->flags & PPMU_HAS_SIER)
169 sdar_valid = regs->dar & SIER_SDAR_VALID;
170 else {
171 unsigned long sdsync;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200172
Michael Ellerman58a032c2013-05-15 20:19:31 +0000173 if (ppmu->flags & PPMU_SIAR_VALID)
174 sdsync = POWER7P_MMCRA_SDAR_VALID;
175 else if (ppmu->flags & PPMU_ALT_SIPR)
176 sdsync = POWER6_MMCRA_SDSYNC;
177 else
178 sdsync = MMCRA_SDSYNC;
179
180 sdar_valid = mmcra & sdsync;
181 }
182
183 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200184 *addrp = mfspr(SPRN_SDAR);
185}
186
Michael Ellerman5682c462013-04-25 19:28:24 +0000187static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000188{
189 unsigned long sihv = MMCRA_SIHV;
190
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000191 if (ppmu->flags & PPMU_HAS_SIER)
192 return !!(regs->dar & SIER_SIHV);
193
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000194 if (ppmu->flags & PPMU_ALT_SIPR)
195 sihv = POWER6_MMCRA_SIHV;
196
Michael Ellerman5682c462013-04-25 19:28:24 +0000197 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000198}
199
Michael Ellerman5682c462013-04-25 19:28:24 +0000200static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000201{
202 unsigned long sipr = MMCRA_SIPR;
203
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000204 if (ppmu->flags & PPMU_HAS_SIER)
205 return !!(regs->dar & SIER_SIPR);
206
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000207 if (ppmu->flags & PPMU_ALT_SIPR)
208 sipr = POWER6_MMCRA_SIPR;
209
Michael Ellerman5682c462013-04-25 19:28:24 +0000210 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000211}
212
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000213static inline u32 perf_flags_from_msr(struct pt_regs *regs)
214{
215 if (regs->msr & MSR_PR)
216 return PERF_RECORD_MISC_USER;
217 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
218 return PERF_RECORD_MISC_HYPERVISOR;
219 return PERF_RECORD_MISC_KERNEL;
220}
221
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200222static inline u32 perf_get_misc_flags(struct pt_regs *regs)
223{
Michael Ellerman33904052013-04-25 19:28:25 +0000224 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200225
Anton Blanchard75382aa2012-06-26 01:01:36 +0000226 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000227 return perf_flags_from_msr(regs);
228
229 /*
230 * If we don't have flags in MMCRA, rather than using
231 * the MSR, we intuit the flags from the address in
232 * SIAR which should give slightly more reliable
233 * results
234 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000235 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000236 unsigned long siar = mfspr(SPRN_SIAR);
237 if (siar >= PAGE_OFFSET)
238 return PERF_RECORD_MISC_KERNEL;
239 return PERF_RECORD_MISC_USER;
240 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200241
Michael Neuling7abb8402009-10-14 19:32:15 +0000242 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000243 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000244 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000245
246 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200247 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000248
Michael Neuling7abb8402009-10-14 19:32:15 +0000249 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200250}
251
252/*
253 * Overload regs->dsisr to store MMCRA so we only need to read it once
254 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000255 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000256 * Overload regs->result to specify whether we should use the MSR (result
257 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200258 */
259static inline void perf_read_regs(struct pt_regs *regs)
260{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000261 unsigned long mmcra = mfspr(SPRN_MMCRA);
262 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
263 int use_siar;
264
Michael Ellerman5682c462013-04-25 19:28:24 +0000265 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000266
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000267 if (ppmu->flags & PPMU_HAS_SIER)
268 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000269
270 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000271 * If this isn't a PMU exception (eg a software event) the SIAR is
272 * not valid. Use pt_regs.
273 *
274 * If it is a marked event use the SIAR.
275 *
276 * If the PMU doesn't update the SIAR for non marked events use
277 * pt_regs.
278 *
279 * If the PMU has HV/PR flags then check to see if they
280 * place the exception in userspace. If so, use pt_regs. In
281 * continuous sampling mode the SIAR and the PMU exception are
282 * not synchronised, so they may be many instructions apart.
283 * This can result in confusing backtraces. We still want
284 * hypervisor samples as well as samples in the kernel with
285 * interrupts off hence the userspace check.
286 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000287 if (TRAP(regs) != 0xf00)
288 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000289 else if (marked)
290 use_siar = 1;
291 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
292 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000293 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000294 use_siar = 0;
295 else
296 use_siar = 1;
297
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000298 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200299}
300
301/*
302 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
303 * it as an NMI.
304 */
305static inline int perf_intr_is_nmi(struct pt_regs *regs)
306{
307 return !regs->softe;
308}
309
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000310/*
311 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
312 * must be sampled only if the SIAR-valid bit is set.
313 *
314 * For unmarked instructions and for processors that don't have the SIAR-Valid
315 * bit, assume that SIAR is valid.
316 */
317static inline int siar_valid(struct pt_regs *regs)
318{
319 unsigned long mmcra = regs->dsisr;
320 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
321
Michael Ellerman58a032c2013-05-15 20:19:31 +0000322 if (marked) {
323 if (ppmu->flags & PPMU_HAS_SIER)
324 return regs->dar & SIER_SIAR_VALID;
325
326 if (ppmu->flags & PPMU_SIAR_VALID)
327 return mmcra & POWER7P_MMCRA_SIAR_VALID;
328 }
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000329
330 return 1;
331}
332
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000333
334/* Reset all possible BHRB entries */
335static void power_pmu_bhrb_reset(void)
336{
337 asm volatile(PPC_CLRBHRB);
338}
339
340static void power_pmu_bhrb_enable(struct perf_event *event)
341{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500342 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000343
344 if (!ppmu->bhrb_nr)
345 return;
346
347 /* Clear BHRB if we changed task context to avoid data leaks */
348 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
349 power_pmu_bhrb_reset();
350 cpuhw->bhrb_context = event->ctx;
351 }
352 cpuhw->bhrb_users++;
353}
354
355static void power_pmu_bhrb_disable(struct perf_event *event)
356{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500357 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000358
359 if (!ppmu->bhrb_nr)
360 return;
361
362 cpuhw->bhrb_users--;
363 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
364
365 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
366 /* BHRB cannot be turned off when other
367 * events are active on the PMU.
368 */
369
370 /* avoid stale pointer */
371 cpuhw->bhrb_context = NULL;
372 }
373}
374
375/* Called from ctxsw to prevent one process's branch entries to
376 * mingle with the other process's entries during context switch.
377 */
Anton Blancharde51df2c2014-08-20 08:55:18 +1000378static void power_pmu_flush_branch_stack(void)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000379{
380 if (ppmu->bhrb_nr)
381 power_pmu_bhrb_reset();
382}
Michael Neuling69123182013-05-13 18:44:58 +0000383/* Calculate the to address for a branch */
384static __u64 power_pmu_bhrb_to(u64 addr)
385{
386 unsigned int instr;
387 int ret;
388 __u64 target;
389
390 if (is_kernel_addr(addr))
391 return branch_target((unsigned int *)addr);
392
393 /* Userspace: need copy instruction here then translate it */
394 pagefault_disable();
395 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
396 if (ret) {
397 pagefault_enable();
398 return 0;
399 }
400 pagefault_enable();
401
402 target = branch_target(&instr);
403 if ((!target) || (instr & BRANCH_ABSOLUTE))
404 return target;
405
406 /* Translate relative branch target from kernel to user address */
407 return target - (unsigned long)&instr + addr;
408}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000409
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000410/* Processing BHRB entries */
Anton Blancharde51df2c2014-08-20 08:55:18 +1000411static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000412{
413 u64 val;
414 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000415 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000416
417 r_index = 0;
418 u_index = 0;
419 while (r_index < ppmu->bhrb_nr) {
420 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000421 val = read_bhrb(r_index++);
422 if (!val)
423 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000424 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000425 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000426 addr = val & BHRB_EA;
427 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000428
Michael Neuling506e70d2013-05-13 18:44:57 +0000429 if (!addr)
430 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000431 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000432
Michael Neuling506e70d2013-05-13 18:44:57 +0000433 /* Branches are read most recent first (ie. mfbhrb 0 is
434 * the most recent branch).
435 * There are two types of valid entries:
436 * 1) a target entry which is the to address of a
437 * computed goto like a blr,bctr,btar. The next
438 * entry read from the bhrb will be branch
439 * corresponding to this target (ie. the actual
440 * blr/bctr/btar instruction).
441 * 2) a from address which is an actual branch. If a
442 * target entry proceeds this, then this is the
443 * matching branch for that target. If this is not
444 * following a target entry, then this is a branch
445 * where the target is given as an immediate field
446 * in the instruction (ie. an i or b form branch).
447 * In this case we need to read the instruction from
448 * memory to determine the target/to address.
449 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000450
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000451 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000452 /* Target branches use two entries
453 * (ie. computed gotos/XL form)
454 */
455 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000456 cpuhw->bhrb_entries[u_index].mispred = pred;
457 cpuhw->bhrb_entries[u_index].predicted = ~pred;
458
Michael Neuling506e70d2013-05-13 18:44:57 +0000459 /* Get from address in next entry */
460 val = read_bhrb(r_index++);
461 addr = val & BHRB_EA;
462 if (val & BHRB_TARGET) {
463 /* Shouldn't have two targets in a
464 row.. Reset index and try again */
465 r_index--;
466 addr = 0;
467 }
468 cpuhw->bhrb_entries[u_index].from = addr;
469 } else {
470 /* Branches to immediate field
471 (ie I or B form) */
472 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000473 cpuhw->bhrb_entries[u_index].to =
474 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000475 cpuhw->bhrb_entries[u_index].mispred = pred;
476 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000477 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000478 u_index++;
479
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000480 }
481 }
482 cpuhw->bhrb_stack.nr = u_index;
483 return;
484}
485
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000486static bool is_ebb_event(struct perf_event *event)
487{
488 /*
489 * This could be a per-PMU callback, but we'd rather avoid the cost. We
490 * check that the PMU supports EBB, meaning those that don't can still
491 * use bit 63 of the event code for something else if they wish.
492 */
Joel Stanley4d9690d2014-07-08 16:08:21 +0930493 return (ppmu->flags & PPMU_ARCH_207S) &&
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000494 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000495}
496
497static int ebb_event_check(struct perf_event *event)
498{
499 struct perf_event *leader = event->group_leader;
500
501 /* Event and group leader must agree on EBB */
502 if (is_ebb_event(leader) != is_ebb_event(event))
503 return -EINVAL;
504
505 if (is_ebb_event(event)) {
506 if (!(event->attach_state & PERF_ATTACH_TASK))
507 return -EINVAL;
508
509 if (!leader->attr.pinned || !leader->attr.exclusive)
510 return -EINVAL;
511
Michael Ellerman58b5fb02014-03-14 16:00:30 +1100512 if (event->attr.freq ||
513 event->attr.inherit ||
514 event->attr.sample_type ||
515 event->attr.sample_period ||
516 event->attr.enable_on_exec)
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000517 return -EINVAL;
518 }
519
520 return 0;
521}
522
523static void ebb_event_add(struct perf_event *event)
524{
525 if (!is_ebb_event(event) || current->thread.used_ebb)
526 return;
527
528 /*
529 * IFF this is the first time we've added an EBB event, set
530 * PMXE in the user MMCR0 so we can detect when it's cleared by
531 * userspace. We need this so that we can context switch while
532 * userspace is in the EBB handler (where PMXE is 0).
533 */
534 current->thread.used_ebb = 1;
535 current->thread.mmcr0 |= MMCR0_PMXE;
536}
537
538static void ebb_switch_out(unsigned long mmcr0)
539{
540 if (!(mmcr0 & MMCR0_EBE))
541 return;
542
543 current->thread.siar = mfspr(SPRN_SIAR);
544 current->thread.sier = mfspr(SPRN_SIER);
545 current->thread.sdar = mfspr(SPRN_SDAR);
546 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
547 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
548}
549
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000550static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000551{
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000552 unsigned long mmcr0 = cpuhw->mmcr[0];
553
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000554 if (!ebb)
555 goto out;
556
Michael Ellerman76cb8a72014-03-14 16:00:34 +1100557 /* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
558 mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000559
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100560 /*
561 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
562 * with pmao_restore_workaround() because we may add PMAO but we never
563 * clear it here.
564 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000565 mmcr0 |= current->thread.mmcr0;
566
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100567 /*
568 * Be careful not to set PMXE if userspace had it cleared. This is also
569 * compatible with pmao_restore_workaround() because it has already
570 * cleared PMXE and we leave PMAO alone.
571 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000572 if (!(current->thread.mmcr0 & MMCR0_PMXE))
573 mmcr0 &= ~MMCR0_PMXE;
574
575 mtspr(SPRN_SIAR, current->thread.siar);
576 mtspr(SPRN_SIER, current->thread.sier);
577 mtspr(SPRN_SDAR, current->thread.sdar);
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000578
579 /*
580 * Merge the kernel & user values of MMCR2. The semantics we implement
581 * are that the user MMCR2 can set bits, ie. cause counters to freeze,
582 * but not clear bits. If a task wants to be able to clear bits, ie.
583 * unfreeze counters, it should not set exclude_xxx in its events and
584 * instead manage the MMCR2 entirely by itself.
585 */
586 mtspr(SPRN_MMCR2, cpuhw->mmcr[3] | current->thread.mmcr2);
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000587out:
588 return mmcr0;
589}
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100590
591static void pmao_restore_workaround(bool ebb)
592{
593 unsigned pmcs[6];
594
595 if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
596 return;
597
598 /*
599 * On POWER8E there is a hardware defect which affects the PMU context
600 * switch logic, ie. power_pmu_disable/enable().
601 *
602 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
603 * by the hardware. Sometime later the actual PMU exception is
604 * delivered.
605 *
606 * If we context switch, or simply disable/enable, the PMU prior to the
607 * exception arriving, the exception will be lost when we clear PMAO.
608 *
609 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
610 * set, and this _should_ generate an exception. However because of the
611 * defect no exception is generated when we write PMAO, and we get
612 * stuck with no counters counting but no exception delivered.
613 *
614 * The workaround is to detect this case and tweak the hardware to
615 * create another pending PMU exception.
616 *
617 * We do that by setting up PMC6 (cycles) for an imminent overflow and
618 * enabling the PMU. That causes a new exception to be generated in the
619 * chip, but we don't take it yet because we have interrupts hard
620 * disabled. We then write back the PMU state as we want it to be seen
621 * by the exception handler. When we reenable interrupts the exception
622 * handler will be called and see the correct state.
623 *
624 * The logic is the same for EBB, except that the exception is gated by
625 * us having interrupts hard disabled as well as the fact that we are
626 * not in userspace. The exception is finally delivered when we return
627 * to userspace.
628 */
629
630 /* Only if PMAO is set and PMAO_SYNC is clear */
631 if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
632 return;
633
634 /* If we're doing EBB, only if BESCR[GE] is set */
635 if (ebb && !(current->thread.bescr & BESCR_GE))
636 return;
637
638 /*
639 * We are already soft-disabled in power_pmu_enable(). We need to hard
640 * enable to actually prevent the PMU exception from firing.
641 */
642 hard_irq_disable();
643
644 /*
645 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
646 * Using read/write_pmc() in a for loop adds 12 function calls and
647 * almost doubles our code size.
648 */
649 pmcs[0] = mfspr(SPRN_PMC1);
650 pmcs[1] = mfspr(SPRN_PMC2);
651 pmcs[2] = mfspr(SPRN_PMC3);
652 pmcs[3] = mfspr(SPRN_PMC4);
653 pmcs[4] = mfspr(SPRN_PMC5);
654 pmcs[5] = mfspr(SPRN_PMC6);
655
656 /* Ensure all freeze bits are unset */
657 mtspr(SPRN_MMCR2, 0);
658
659 /* Set up PMC6 to overflow in one cycle */
660 mtspr(SPRN_PMC6, 0x7FFFFFFE);
661
662 /* Enable exceptions and unfreeze PMC6 */
663 mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
664
665 /* Now we need to refreeze and restore the PMCs */
666 mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
667
668 mtspr(SPRN_PMC1, pmcs[0]);
669 mtspr(SPRN_PMC2, pmcs[1]);
670 mtspr(SPRN_PMC3, pmcs[2]);
671 mtspr(SPRN_PMC4, pmcs[3]);
672 mtspr(SPRN_PMC5, pmcs[4]);
673 mtspr(SPRN_PMC6, pmcs[5]);
674}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200675#endif /* CONFIG_PPC64 */
676
677static void perf_event_interrupt(struct pt_regs *regs);
678
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200679/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200680 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200681 */
682static unsigned long read_pmc(int idx)
683{
684 unsigned long val;
685
686 switch (idx) {
687 case 1:
688 val = mfspr(SPRN_PMC1);
689 break;
690 case 2:
691 val = mfspr(SPRN_PMC2);
692 break;
693 case 3:
694 val = mfspr(SPRN_PMC3);
695 break;
696 case 4:
697 val = mfspr(SPRN_PMC4);
698 break;
699 case 5:
700 val = mfspr(SPRN_PMC5);
701 break;
702 case 6:
703 val = mfspr(SPRN_PMC6);
704 break;
705#ifdef CONFIG_PPC64
706 case 7:
707 val = mfspr(SPRN_PMC7);
708 break;
709 case 8:
710 val = mfspr(SPRN_PMC8);
711 break;
712#endif /* CONFIG_PPC64 */
713 default:
714 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
715 val = 0;
716 }
717 return val;
718}
719
720/*
721 * Write one PMC.
722 */
723static void write_pmc(int idx, unsigned long val)
724{
725 switch (idx) {
726 case 1:
727 mtspr(SPRN_PMC1, val);
728 break;
729 case 2:
730 mtspr(SPRN_PMC2, val);
731 break;
732 case 3:
733 mtspr(SPRN_PMC3, val);
734 break;
735 case 4:
736 mtspr(SPRN_PMC4, val);
737 break;
738 case 5:
739 mtspr(SPRN_PMC5, val);
740 break;
741 case 6:
742 mtspr(SPRN_PMC6, val);
743 break;
744#ifdef CONFIG_PPC64
745 case 7:
746 mtspr(SPRN_PMC7, val);
747 break;
748 case 8:
749 mtspr(SPRN_PMC8, val);
750 break;
751#endif /* CONFIG_PPC64 */
752 default:
753 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
754 }
755}
756
Anshuman Khandual5f6d0382014-03-14 16:00:27 +1100757/* Called from sysrq_handle_showregs() */
758void perf_event_print_debug(void)
759{
760 unsigned long sdar, sier, flags;
761 u32 pmcs[MAX_HWEVENTS];
762 int i;
763
764 if (!ppmu->n_counter)
765 return;
766
767 local_irq_save(flags);
768
769 pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
770 smp_processor_id(), ppmu->name, ppmu->n_counter);
771
772 for (i = 0; i < ppmu->n_counter; i++)
773 pmcs[i] = read_pmc(i + 1);
774
775 for (; i < MAX_HWEVENTS; i++)
776 pmcs[i] = 0xdeadbeef;
777
778 pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
779 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
780
781 if (ppmu->n_counter > 4)
782 pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
783 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
784
785 pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
786 mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
787
788 sdar = sier = 0;
789#ifdef CONFIG_PPC64
790 sdar = mfspr(SPRN_SDAR);
791
792 if (ppmu->flags & PPMU_HAS_SIER)
793 sier = mfspr(SPRN_SIER);
794
Joel Stanley4d9690d2014-07-08 16:08:21 +0930795 if (ppmu->flags & PPMU_ARCH_207S) {
Anshuman Khandual5f6d0382014-03-14 16:00:27 +1100796 pr_info("MMCR2: %016lx EBBHR: %016lx\n",
797 mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
798 pr_info("EBBRR: %016lx BESCR: %016lx\n",
799 mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
800 }
801#endif
802 pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
803 mfspr(SPRN_SIAR), sdar, sier);
804
805 local_irq_restore(flags);
806}
807
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200808/*
809 * Check if a set of events can all go on the PMU at once.
810 * If they can't, this will look at alternative codes for the events
811 * and see if any combination of alternative codes is feasible.
812 * The feasible set is returned in event_id[].
813 */
814static int power_check_constraints(struct cpu_hw_events *cpuhw,
815 u64 event_id[], unsigned int cflags[],
816 int n_ev)
817{
818 unsigned long mask, value, nv;
819 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
820 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
821 int i, j;
822 unsigned long addf = ppmu->add_fields;
823 unsigned long tadd = ppmu->test_adder;
824
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000825 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200826 return -1;
827
828 /* First see if the events will go on as-is */
829 for (i = 0; i < n_ev; ++i) {
830 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
831 && !ppmu->limited_pmc_event(event_id[i])) {
832 ppmu->get_alternatives(event_id[i], cflags[i],
833 cpuhw->alternatives[i]);
834 event_id[i] = cpuhw->alternatives[i][0];
835 }
836 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
837 &cpuhw->avalues[i][0]))
838 return -1;
839 }
840 value = mask = 0;
841 for (i = 0; i < n_ev; ++i) {
842 nv = (value | cpuhw->avalues[i][0]) +
843 (value & cpuhw->avalues[i][0] & addf);
844 if ((((nv + tadd) ^ value) & mask) != 0 ||
845 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
846 cpuhw->amasks[i][0]) != 0)
847 break;
848 value = nv;
849 mask |= cpuhw->amasks[i][0];
850 }
851 if (i == n_ev)
852 return 0; /* all OK */
853
854 /* doesn't work, gather alternatives... */
855 if (!ppmu->get_alternatives)
856 return -1;
857 for (i = 0; i < n_ev; ++i) {
858 choice[i] = 0;
859 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
860 cpuhw->alternatives[i]);
861 for (j = 1; j < n_alt[i]; ++j)
862 ppmu->get_constraint(cpuhw->alternatives[i][j],
863 &cpuhw->amasks[i][j],
864 &cpuhw->avalues[i][j]);
865 }
866
867 /* enumerate all possibilities and see if any will work */
868 i = 0;
869 j = -1;
870 value = mask = nv = 0;
871 while (i < n_ev) {
872 if (j >= 0) {
873 /* we're backtracking, restore context */
874 value = svalues[i];
875 mask = smasks[i];
876 j = choice[i];
877 }
878 /*
879 * See if any alternative k for event_id i,
880 * where k > j, will satisfy the constraints.
881 */
882 while (++j < n_alt[i]) {
883 nv = (value | cpuhw->avalues[i][j]) +
884 (value & cpuhw->avalues[i][j] & addf);
885 if ((((nv + tadd) ^ value) & mask) == 0 &&
886 (((nv + tadd) ^ cpuhw->avalues[i][j])
887 & cpuhw->amasks[i][j]) == 0)
888 break;
889 }
890 if (j >= n_alt[i]) {
891 /*
892 * No feasible alternative, backtrack
893 * to event_id i-1 and continue enumerating its
894 * alternatives from where we got up to.
895 */
896 if (--i < 0)
897 return -1;
898 } else {
899 /*
900 * Found a feasible alternative for event_id i,
901 * remember where we got up to with this event_id,
902 * go on to the next event_id, and start with
903 * the first alternative for it.
904 */
905 choice[i] = j;
906 svalues[i] = value;
907 smasks[i] = mask;
908 value = nv;
909 mask |= cpuhw->amasks[i][j];
910 ++i;
911 j = -1;
912 }
913 }
914
915 /* OK, we have a feasible combination, tell the caller the solution */
916 for (i = 0; i < n_ev; ++i)
917 event_id[i] = cpuhw->alternatives[i][choice[i]];
918 return 0;
919}
920
921/*
922 * Check if newly-added events have consistent settings for
923 * exclude_{user,kernel,hv} with each other and any previously
924 * added events.
925 */
926static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
927 int n_prev, int n_new)
928{
929 int eu = 0, ek = 0, eh = 0;
930 int i, n, first;
931 struct perf_event *event;
932
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000933 /*
934 * If the PMU we're on supports per event exclude settings then we
935 * don't need to do any of this logic. NB. This assumes no PMU has both
936 * per event exclude and limited PMCs.
937 */
938 if (ppmu->flags & PPMU_ARCH_207S)
939 return 0;
940
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200941 n = n_prev + n_new;
942 if (n <= 1)
943 return 0;
944
945 first = 1;
946 for (i = 0; i < n; ++i) {
947 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
948 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
949 continue;
950 }
951 event = ctrs[i];
952 if (first) {
953 eu = event->attr.exclude_user;
954 ek = event->attr.exclude_kernel;
955 eh = event->attr.exclude_hv;
956 first = 0;
957 } else if (event->attr.exclude_user != eu ||
958 event->attr.exclude_kernel != ek ||
959 event->attr.exclude_hv != eh) {
960 return -EAGAIN;
961 }
962 }
963
964 if (eu || ek || eh)
965 for (i = 0; i < n; ++i)
966 if (cflags[i] & PPMU_LIMITED_PMC_OK)
967 cflags[i] |= PPMU_LIMITED_PMC_REQD;
968
969 return 0;
970}
971
Eric B Munson86c74ab2011-04-15 08:12:30 +0000972static u64 check_and_compute_delta(u64 prev, u64 val)
973{
974 u64 delta = (val - prev) & 0xfffffffful;
975
976 /*
977 * POWER7 can roll back counter values, if the new value is smaller
978 * than the previous value it will cause the delta and the counter to
979 * have bogus values unless we rolled a counter over. If a coutner is
980 * rolled back, it will be smaller, but within 256, which is the maximum
981 * number of events to rollback at once. If we dectect a rollback
982 * return 0. This can lead to a small lack of precision in the
983 * counters.
984 */
985 if (prev > val && (prev - val) < 256)
986 delta = 0;
987
988 return delta;
989}
990
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200991static void power_pmu_read(struct perf_event *event)
992{
993 s64 val, delta, prev;
994
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200995 if (event->hw.state & PERF_HES_STOPPED)
996 return;
997
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200998 if (!event->hw.idx)
999 return;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001000
1001 if (is_ebb_event(event)) {
1002 val = read_pmc(event->hw.idx);
1003 local64_set(&event->hw.prev_count, val);
1004 return;
1005 }
1006
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001007 /*
1008 * Performance monitor interrupts come even when interrupts
1009 * are soft-disabled, as long as interrupts are hard-enabled.
1010 * Therefore we treat them like NMIs.
1011 */
1012 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +02001013 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014 barrier();
1015 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001016 delta = check_and_compute_delta(prev, val);
1017 if (!delta)
1018 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001019 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001020
Peter Zijlstrae7850592010-05-21 14:43:08 +02001021 local64_add(delta, &event->count);
Anton Blanchardf5602942014-05-29 08:15:38 +10001022
1023 /*
1024 * A number of places program the PMC with (0x80000000 - period_left).
1025 * We never want period_left to be less than 1 because we will program
1026 * the PMC with a value >= 0x800000000 and an edge detected PMC will
1027 * roll around to 0 before taking an exception. We have seen this
1028 * on POWER8.
1029 *
1030 * To fix this, clamp the minimum value of period_left to 1.
1031 */
1032 do {
1033 prev = local64_read(&event->hw.period_left);
1034 val = prev - delta;
1035 if (val < 1)
1036 val = 1;
1037 } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038}
1039
1040/*
1041 * On some machines, PMC5 and PMC6 can't be written, don't respect
1042 * the freeze conditions, and don't generate interrupts. This tells
1043 * us if `event' is using such a PMC.
1044 */
1045static int is_limited_pmc(int pmcnum)
1046{
1047 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1048 && (pmcnum == 5 || pmcnum == 6);
1049}
1050
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001051static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001052 unsigned long pmc5, unsigned long pmc6)
1053{
1054 struct perf_event *event;
1055 u64 val, prev, delta;
1056 int i;
1057
1058 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001059 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001060 if (!event->hw.idx)
1061 continue;
1062 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001063 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001064 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001065 delta = check_and_compute_delta(prev, val);
1066 if (delta)
1067 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001068 }
1069}
1070
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001071static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001072 unsigned long pmc5, unsigned long pmc6)
1073{
1074 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001075 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001076 int i;
1077
1078 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001079 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001080 event->hw.idx = cpuhw->limited_hwidx[i];
1081 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001082 prev = local64_read(&event->hw.prev_count);
1083 if (check_and_compute_delta(prev, val))
1084 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001085 perf_event_update_userpage(event);
1086 }
1087}
1088
1089/*
1090 * Since limited events don't respect the freeze conditions, we
1091 * have to read them immediately after freezing or unfreezing the
1092 * other events. We try to keep the values from the limited
1093 * events as consistent as possible by keeping the delay (in
1094 * cycles and instructions) between freezing/unfreezing and reading
1095 * the limited events as small and consistent as possible.
1096 * Therefore, if any limited events are in use, we read them
1097 * both, and always in the same order, to minimize variability,
1098 * and do it inside the same asm that writes MMCR0.
1099 */
1100static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1101{
1102 unsigned long pmc5, pmc6;
1103
1104 if (!cpuhw->n_limited) {
1105 mtspr(SPRN_MMCR0, mmcr0);
1106 return;
1107 }
1108
1109 /*
1110 * Write MMCR0, then read PMC5 and PMC6 immediately.
1111 * To ensure we don't get a performance monitor interrupt
1112 * between writing MMCR0 and freezing/thawing the limited
1113 * events, we first write MMCR0 with the event overflow
1114 * interrupt enable bits turned off.
1115 */
1116 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1117 : "=&r" (pmc5), "=&r" (pmc6)
1118 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1119 "i" (SPRN_MMCR0),
1120 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1121
1122 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001123 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001124 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001125 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126
1127 /*
1128 * Write the full MMCR0 including the event overflow interrupt
1129 * enable bits, if necessary.
1130 */
1131 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1132 mtspr(SPRN_MMCR0, mmcr0);
1133}
1134
1135/*
1136 * Disable all events to prevent PMU interrupts and to allow
1137 * events to be added or removed.
1138 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001139static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001140{
1141 struct cpu_hw_events *cpuhw;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001142 unsigned long flags, mmcr0, val;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143
1144 if (!ppmu)
1145 return;
1146 local_irq_save(flags);
Christoph Lameter69111ba2014-10-21 15:23:25 -05001147 cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001148
1149 if (!cpuhw->disabled) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001150 /*
1151 * Check if we ever enabled the PMU on this cpu.
1152 */
1153 if (!cpuhw->pmcs_enabled) {
1154 ppc_enable_pmcs();
1155 cpuhw->pmcs_enabled = 1;
1156 }
1157
1158 /*
Michael Ellerman76cb8a72014-03-14 16:00:34 +11001159 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001160 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001161 val = mmcr0 = mfspr(SPRN_MMCR0);
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001162 val |= MMCR0_FC;
Michael Ellerman76cb8a72014-03-14 16:00:34 +11001163 val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
1164 MMCR0_FC56);
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001165
1166 /*
1167 * The barrier is to make sure the mtspr has been
1168 * executed and the PMU has frozen the events etc.
1169 * before we return.
1170 */
1171 write_mmcr0(cpuhw, val);
1172 mb();
1173
1174 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175 * Disable instruction sampling if it was enabled
1176 */
1177 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1178 mtspr(SPRN_MMCRA,
1179 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1180 mb();
1181 }
1182
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001183 cpuhw->disabled = 1;
1184 cpuhw->n_added = 0;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001185
1186 ebb_switch_out(mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001189 local_irq_restore(flags);
1190}
1191
1192/*
1193 * Re-enable all events if disable == 0.
1194 * If we were previously disabled and events were added, then
1195 * put the new config on the PMU.
1196 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001197static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198{
1199 struct perf_event *event;
1200 struct cpu_hw_events *cpuhw;
1201 unsigned long flags;
1202 long i;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001203 unsigned long val, mmcr0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204 s64 left;
1205 unsigned int hwc_index[MAX_HWEVENTS];
1206 int n_lim;
1207 int idx;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001208 bool ebb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001209
1210 if (!ppmu)
1211 return;
1212 local_irq_save(flags);
Michael Ellerman0a488432013-06-28 18:15:13 +10001213
Christoph Lameter69111ba2014-10-21 15:23:25 -05001214 cpuhw = this_cpu_ptr(&cpu_hw_events);
Michael Ellerman0a488432013-06-28 18:15:13 +10001215 if (!cpuhw->disabled)
1216 goto out;
1217
Michael Ellerman4ea355b2013-06-28 18:15:14 +10001218 if (cpuhw->n_events == 0) {
1219 ppc_set_pmu_inuse(0);
1220 goto out;
1221 }
1222
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001223 cpuhw->disabled = 0;
1224
1225 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001226 * EBB requires an exclusive group and all events must have the EBB
1227 * flag set, or not set, so we can just check a single event. Also we
1228 * know we have at least one event.
1229 */
1230 ebb = is_ebb_event(cpuhw->event[0]);
1231
1232 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233 * If we didn't change anything, or only removed events,
1234 * no need to recalculate MMCR* settings and reset the PMCs.
1235 * Just reenable the PMU with the current MMCR* settings
1236 * (possibly updated for removal of events).
1237 */
1238 if (!cpuhw->n_added) {
1239 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1240 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 goto out_enable;
1242 }
1243
1244 /*
Michael Ellerman79a4cb22014-07-23 21:12:36 +10001245 * Clear all MMCR settings and recompute them for the new set of events.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246 */
Michael Ellerman79a4cb22014-07-23 21:12:36 +10001247 memset(cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
1248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001249 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
Michael Ellerman8abd8182014-07-23 21:12:37 +10001250 cpuhw->mmcr, cpuhw->event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 /* shouldn't ever get here */
1252 printk(KERN_ERR "oops compute_mmcr failed\n");
1253 goto out;
1254 }
1255
Michael Ellerman9de5cb02014-07-23 21:12:38 +10001256 if (!(ppmu->flags & PPMU_ARCH_207S)) {
1257 /*
1258 * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
1259 * bits for the first event. We have already checked that all
1260 * events have the same value for these bits as the first event.
1261 */
1262 event = cpuhw->event[0];
1263 if (event->attr.exclude_user)
1264 cpuhw->mmcr[0] |= MMCR0_FCP;
1265 if (event->attr.exclude_kernel)
1266 cpuhw->mmcr[0] |= freeze_events_kernel;
1267 if (event->attr.exclude_hv)
1268 cpuhw->mmcr[0] |= MMCR0_FCHV;
1269 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270
1271 /*
1272 * Write the new configuration to MMCR* with the freeze
1273 * bit set and set the hardware events to their initial values.
1274 * Then unfreeze the events.
1275 */
1276 ppc_set_pmu_inuse(1);
1277 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1278 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1279 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1280 | MMCR0_FC);
Michael Ellerman9de5cb02014-07-23 21:12:38 +10001281 if (ppmu->flags & PPMU_ARCH_207S)
1282 mtspr(SPRN_MMCR2, cpuhw->mmcr[3]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001283
1284 /*
1285 * Read off any pre-existing events that need to move
1286 * to another PMC.
1287 */
1288 for (i = 0; i < cpuhw->n_events; ++i) {
1289 event = cpuhw->event[i];
1290 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1291 power_pmu_read(event);
1292 write_pmc(event->hw.idx, 0);
1293 event->hw.idx = 0;
1294 }
1295 }
1296
1297 /*
1298 * Initialize the PMCs for all the new and moved events.
1299 */
1300 cpuhw->n_limited = n_lim = 0;
1301 for (i = 0; i < cpuhw->n_events; ++i) {
1302 event = cpuhw->event[i];
1303 if (event->hw.idx)
1304 continue;
1305 idx = hwc_index[i] + 1;
1306 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001307 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 cpuhw->limited_hwidx[n_lim] = idx;
1309 ++n_lim;
1310 continue;
1311 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001312
1313 if (ebb)
1314 val = local64_read(&event->hw.prev_count);
1315 else {
1316 val = 0;
1317 if (event->hw.sample_period) {
1318 left = local64_read(&event->hw.period_left);
1319 if (left < 0x80000000L)
1320 val = 0x80000000L - left;
1321 }
1322 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001323 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001325 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001326 if (event->hw.state & PERF_HES_STOPPED)
1327 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328 write_pmc(idx, val);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001329
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001330 perf_event_update_userpage(event);
1331 }
1332 cpuhw->n_limited = n_lim;
1333 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1334
1335 out_enable:
Michael Ellermanc2e37a22014-03-14 16:00:29 +11001336 pmao_restore_workaround(ebb);
1337
Michael Ellerman9de5cb02014-07-23 21:12:38 +10001338 mmcr0 = ebb_switch_in(ebb, cpuhw);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001340 mb();
Anshuman Khandualb4d6c062013-12-18 13:14:53 +11001341 if (cpuhw->bhrb_users)
1342 ppmu->config_bhrb(cpuhw->bhrb_filter);
1343
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001344 write_mmcr0(cpuhw, mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345
1346 /*
1347 * Enable instruction sampling if necessary
1348 */
1349 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1350 mb();
1351 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1352 }
1353
1354 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001355
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 local_irq_restore(flags);
1357}
1358
1359static int collect_events(struct perf_event *group, int max_count,
1360 struct perf_event *ctrs[], u64 *events,
1361 unsigned int *flags)
1362{
1363 int n = 0;
1364 struct perf_event *event;
1365
1366 if (!is_software_event(group)) {
1367 if (n >= max_count)
1368 return -1;
1369 ctrs[n] = group;
1370 flags[n] = group->hw.event_base;
1371 events[n++] = group->hw.config;
1372 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001373 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374 if (!is_software_event(event) &&
1375 event->state != PERF_EVENT_STATE_OFF) {
1376 if (n >= max_count)
1377 return -1;
1378 ctrs[n] = event;
1379 flags[n] = event->hw.event_base;
1380 events[n++] = event->hw.config;
1381 }
1382 }
1383 return n;
1384}
1385
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001386/*
1387 * Add a event to the PMU.
1388 * If all events are not already frozen, then we disable and
1389 * re-enable the PMU in order to get hw_perf_enable to do the
1390 * actual work of reconfiguring the PMU.
1391 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001392static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393{
1394 struct cpu_hw_events *cpuhw;
1395 unsigned long flags;
1396 int n0;
1397 int ret = -EAGAIN;
1398
1399 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001400 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401
1402 /*
1403 * Add the event to the list (if there is room)
1404 * and check whether the total set is still feasible.
1405 */
Christoph Lameter69111ba2014-10-21 15:23:25 -05001406 cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001408 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409 goto out;
1410 cpuhw->event[n0] = event;
1411 cpuhw->events[n0] = event->hw.config;
1412 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001413
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001414 /*
1415 * This event may have been disabled/stopped in record_and_restart()
1416 * because we exceeded the ->event_limit. If re-starting the event,
1417 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1418 * notification is re-enabled.
1419 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001420 if (!(ef_flags & PERF_EF_START))
1421 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001422 else
1423 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001424
Lin Ming8e6d5572010-05-08 20:28:41 +10001425 /*
1426 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001427 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001428 * at commit time(->commit_txn) as a whole
1429 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001430 if (cpuhw->group_flag & PERF_EVENT_TXN)
Lin Ming8e6d5572010-05-08 20:28:41 +10001431 goto nocheck;
1432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001433 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1434 goto out;
1435 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1436 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001437 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001438
1439nocheck:
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001440 ebb_event_add(event);
1441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442 ++cpuhw->n_events;
1443 ++cpuhw->n_added;
1444
1445 ret = 0;
1446 out:
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301447 if (has_branch_stack(event)) {
Anshuman Khandual3925f462013-04-22 19:42:44 +00001448 power_pmu_bhrb_enable(event);
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301449 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1450 event->attr.branch_sample_type);
1451 }
Anshuman Khandual3925f462013-04-22 19:42:44 +00001452
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001453 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001454 local_irq_restore(flags);
1455 return ret;
1456}
1457
1458/*
1459 * Remove a event from the PMU.
1460 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001461static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001462{
1463 struct cpu_hw_events *cpuhw;
1464 long i;
1465 unsigned long flags;
1466
1467 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001468 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469
1470 power_pmu_read(event);
1471
Christoph Lameter69111ba2014-10-21 15:23:25 -05001472 cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001473 for (i = 0; i < cpuhw->n_events; ++i) {
1474 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001475 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001476 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001477 cpuhw->events[i-1] = cpuhw->events[i];
1478 cpuhw->flags[i-1] = cpuhw->flags[i];
1479 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001480 --cpuhw->n_events;
1481 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1482 if (event->hw.idx) {
1483 write_pmc(event->hw.idx, 0);
1484 event->hw.idx = 0;
1485 }
1486 perf_event_update_userpage(event);
1487 break;
1488 }
1489 }
1490 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001491 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 break;
1493 if (i < cpuhw->n_limited) {
1494 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001495 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001496 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1497 }
1498 --cpuhw->n_limited;
1499 }
1500 if (cpuhw->n_events == 0) {
1501 /* disable exceptions if no events are running */
1502 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1503 }
1504
Anshuman Khandual3925f462013-04-22 19:42:44 +00001505 if (has_branch_stack(event))
1506 power_pmu_bhrb_disable(event);
1507
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001508 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509 local_irq_restore(flags);
1510}
1511
1512/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001513 * POWER-PMU does not support disabling individual counters, hence
1514 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001515 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001516
1517static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001519 unsigned long flags;
1520 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001521 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001522
1523 if (!event->hw.idx || !event->hw.sample_period)
1524 return;
1525
1526 if (!(event->hw.state & PERF_HES_STOPPED))
1527 return;
1528
1529 if (ef_flags & PERF_EF_RELOAD)
1530 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1531
1532 local_irq_save(flags);
1533 perf_pmu_disable(event->pmu);
1534
1535 event->hw.state = 0;
1536 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001537
1538 val = 0;
1539 if (left < 0x80000000L)
1540 val = 0x80000000L - left;
1541
1542 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001543
1544 perf_event_update_userpage(event);
1545 perf_pmu_enable(event->pmu);
1546 local_irq_restore(flags);
1547}
1548
1549static void power_pmu_stop(struct perf_event *event, int ef_flags)
1550{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001551 unsigned long flags;
1552
1553 if (!event->hw.idx || !event->hw.sample_period)
1554 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001555
1556 if (event->hw.state & PERF_HES_STOPPED)
1557 return;
1558
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001560 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001561
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001562 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001563 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1564 write_pmc(event->hw.idx, 0);
1565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001567 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001568 local_irq_restore(flags);
1569}
1570
Lin Ming8e6d5572010-05-08 20:28:41 +10001571/*
1572 * Start group events scheduling transaction
1573 * Set the flag to make pmu::enable() not perform the
1574 * schedulability test, it will be performed at commit time
1575 */
Anton Blancharde51df2c2014-08-20 08:55:18 +10001576static void power_pmu_start_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001577{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001578 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Lin Ming8e6d5572010-05-08 20:28:41 +10001579
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001580 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001581 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Ming8e6d5572010-05-08 20:28:41 +10001582 cpuhw->n_txn_start = cpuhw->n_events;
1583}
1584
1585/*
1586 * Stop group events scheduling transaction
1587 * Clear the flag and pmu::enable() will perform the
1588 * schedulability test.
1589 */
Anton Blancharde51df2c2014-08-20 08:55:18 +10001590static void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001591{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001592 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Lin Ming8e6d5572010-05-08 20:28:41 +10001593
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001594 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001595 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001596}
1597
1598/*
1599 * Commit group events scheduling transaction
1600 * Perform the group schedulability test as a whole
1601 * Return 0 if success
1602 */
Anton Blancharde51df2c2014-08-20 08:55:18 +10001603static int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001604{
1605 struct cpu_hw_events *cpuhw;
1606 long i, n;
1607
1608 if (!ppmu)
1609 return -EAGAIN;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001610 cpuhw = this_cpu_ptr(&cpu_hw_events);
Lin Ming8e6d5572010-05-08 20:28:41 +10001611 n = cpuhw->n_events;
1612 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1613 return -EAGAIN;
1614 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1615 if (i < 0)
1616 return -EAGAIN;
1617
1618 for (i = cpuhw->n_txn_start; i < n; ++i)
1619 cpuhw->event[i]->hw.config = cpuhw->events[i];
1620
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001621 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001622 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001623 return 0;
1624}
1625
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626/*
1627 * Return 1 if we might be able to put event on a limited PMC,
1628 * or 0 if not.
1629 * A event can only go on a limited PMC if it counts something
1630 * that a limited PMC can count, doesn't require interrupts, and
1631 * doesn't exclude any processor mode.
1632 */
1633static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1634 unsigned int flags)
1635{
1636 int n;
1637 u64 alt[MAX_EVENT_ALTERNATIVES];
1638
1639 if (event->attr.exclude_user
1640 || event->attr.exclude_kernel
1641 || event->attr.exclude_hv
1642 || event->attr.sample_period)
1643 return 0;
1644
1645 if (ppmu->limited_pmc_event(ev))
1646 return 1;
1647
1648 /*
1649 * The requested event_id isn't on a limited PMC already;
1650 * see if any alternative code goes on a limited PMC.
1651 */
1652 if (!ppmu->get_alternatives)
1653 return 0;
1654
1655 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1656 n = ppmu->get_alternatives(ev, flags, alt);
1657
1658 return n > 0;
1659}
1660
1661/*
1662 * Find an alternative event_id that goes on a normal PMC, if possible,
1663 * and return the event_id code, or 0 if there is no such alternative.
1664 * (Note: event_id code 0 is "don't count" on all machines.)
1665 */
1666static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1667{
1668 u64 alt[MAX_EVENT_ALTERNATIVES];
1669 int n;
1670
1671 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1672 n = ppmu->get_alternatives(ev, flags, alt);
1673 if (!n)
1674 return 0;
1675 return alt[0];
1676}
1677
1678/* Number of perf_events counting hardware events */
1679static atomic_t num_events;
1680/* Used to avoid races in calling reserve/release_pmc_hardware */
1681static DEFINE_MUTEX(pmc_reserve_mutex);
1682
1683/*
1684 * Release the PMU if this is the last perf_event.
1685 */
1686static void hw_perf_event_destroy(struct perf_event *event)
1687{
1688 if (!atomic_add_unless(&num_events, -1, 1)) {
1689 mutex_lock(&pmc_reserve_mutex);
1690 if (atomic_dec_return(&num_events) == 0)
1691 release_pmc_hardware();
1692 mutex_unlock(&pmc_reserve_mutex);
1693 }
1694}
1695
1696/*
1697 * Translate a generic cache event_id config to a raw event_id code.
1698 */
1699static int hw_perf_cache_event(u64 config, u64 *eventp)
1700{
1701 unsigned long type, op, result;
1702 int ev;
1703
1704 if (!ppmu->cache_events)
1705 return -EINVAL;
1706
1707 /* unpack config */
1708 type = config & 0xff;
1709 op = (config >> 8) & 0xff;
1710 result = (config >> 16) & 0xff;
1711
1712 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1713 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1714 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1715 return -EINVAL;
1716
1717 ev = (*ppmu->cache_events)[type][op][result];
1718 if (ev == 0)
1719 return -EOPNOTSUPP;
1720 if (ev == -1)
1721 return -EINVAL;
1722 *eventp = ev;
1723 return 0;
1724}
1725
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001726static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727{
1728 u64 ev;
1729 unsigned long flags;
1730 struct perf_event *ctrs[MAX_HWEVENTS];
1731 u64 events[MAX_HWEVENTS];
1732 unsigned int cflags[MAX_HWEVENTS];
1733 int n;
1734 int err;
1735 struct cpu_hw_events *cpuhw;
1736
1737 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001738 return -ENOENT;
1739
Anshuman Khandual3925f462013-04-22 19:42:44 +00001740 if (has_branch_stack(event)) {
1741 /* PMU has BHRB enabled */
Joel Stanley4d9690d2014-07-08 16:08:21 +09301742 if (!(ppmu->flags & PPMU_ARCH_207S))
Anshuman Khandual3925f462013-04-22 19:42:44 +00001743 return -EOPNOTSUPP;
1744 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001745
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746 switch (event->attr.type) {
1747 case PERF_TYPE_HARDWARE:
1748 ev = event->attr.config;
1749 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001750 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751 ev = ppmu->generic_events[ev];
1752 break;
1753 case PERF_TYPE_HW_CACHE:
1754 err = hw_perf_cache_event(event->attr.config, &ev);
1755 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001756 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001757 break;
1758 case PERF_TYPE_RAW:
1759 ev = event->attr.config;
1760 break;
1761 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001762 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001764
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765 event->hw.config_base = ev;
1766 event->hw.idx = 0;
1767
1768 /*
1769 * If we are not running on a hypervisor, force the
1770 * exclude_hv bit to 0 so that we don't care what
1771 * the user set it to.
1772 */
1773 if (!firmware_has_feature(FW_FEATURE_LPAR))
1774 event->attr.exclude_hv = 0;
1775
1776 /*
1777 * If this is a per-task event, then we can use
1778 * PM_RUN_* events interchangeably with their non RUN_*
1779 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1780 * XXX we should check if the task is an idle task.
1781 */
1782 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001783 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784 flags |= PPMU_ONLY_COUNT_RUN;
1785
1786 /*
1787 * If this machine has limited events, check whether this
1788 * event_id could go on a limited event.
1789 */
1790 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1791 if (can_go_on_limited_pmc(event, ev, flags)) {
1792 flags |= PPMU_LIMITED_PMC_OK;
1793 } else if (ppmu->limited_pmc_event(ev)) {
1794 /*
1795 * The requested event_id is on a limited PMC,
1796 * but we can't use a limited PMC; see if any
1797 * alternative goes on a normal PMC.
1798 */
1799 ev = normal_pmc_alternative(ev, flags);
1800 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001801 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802 }
1803 }
1804
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001805 /* Extra checks for EBB */
1806 err = ebb_event_check(event);
1807 if (err)
1808 return err;
1809
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001810 /*
1811 * If this is in a group, check if it can go on with all the
1812 * other hardware events in the group. We assume the event
1813 * hasn't been linked into its leader's sibling list at this point.
1814 */
1815 n = 0;
1816 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001817 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001818 ctrs, events, cflags);
1819 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001820 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821 }
1822 events[n] = ev;
1823 ctrs[n] = event;
1824 cflags[n] = flags;
1825 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001826 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827
1828 cpuhw = &get_cpu_var(cpu_hw_events);
1829 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001830
1831 if (has_branch_stack(event)) {
1832 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1833 event->attr.branch_sample_type);
1834
Jan Stancek68de8862015-03-24 08:33:22 -04001835 if (cpuhw->bhrb_filter == -1) {
1836 put_cpu_var(cpu_hw_events);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001837 return -EOPNOTSUPP;
Jan Stancek68de8862015-03-24 08:33:22 -04001838 }
Anshuman Khandual3925f462013-04-22 19:42:44 +00001839 }
1840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001841 put_cpu_var(cpu_hw_events);
1842 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001843 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844
1845 event->hw.config = events[n];
1846 event->hw.event_base = cflags[n];
1847 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001848 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849
1850 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001851 * For EBB events we just context switch the PMC value, we don't do any
1852 * of the sample_period logic. We use hw.prev_count for this.
1853 */
1854 if (is_ebb_event(event))
1855 local64_set(&event->hw.prev_count, 0);
1856
1857 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858 * See if we need to reserve the PMU.
1859 * If no events are currently in use, then we have to take a
1860 * mutex to ensure that we don't race with another task doing
1861 * reserve_pmc_hardware or release_pmc_hardware.
1862 */
1863 err = 0;
1864 if (!atomic_inc_not_zero(&num_events)) {
1865 mutex_lock(&pmc_reserve_mutex);
1866 if (atomic_read(&num_events) == 0 &&
1867 reserve_pmc_hardware(perf_event_interrupt))
1868 err = -EBUSY;
1869 else
1870 atomic_inc(&num_events);
1871 mutex_unlock(&pmc_reserve_mutex);
1872 }
1873 event->destroy = hw_perf_event_destroy;
1874
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001875 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876}
1877
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001878static int power_pmu_event_idx(struct perf_event *event)
1879{
1880 return event->hw.idx;
1881}
1882
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001883ssize_t power_events_sysfs_show(struct device *dev,
1884 struct device_attribute *attr, char *page)
1885{
1886 struct perf_pmu_events_attr *pmu_attr;
1887
1888 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1889
1890 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1891}
1892
Anton Blancharde51df2c2014-08-20 08:55:18 +10001893static struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001894 .pmu_enable = power_pmu_enable,
1895 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001896 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001897 .add = power_pmu_add,
1898 .del = power_pmu_del,
1899 .start = power_pmu_start,
1900 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001901 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001902 .start_txn = power_pmu_start_txn,
1903 .cancel_txn = power_pmu_cancel_txn,
1904 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001905 .event_idx = power_pmu_event_idx,
Anshuman Khandual3925f462013-04-22 19:42:44 +00001906 .flush_branch_stack = power_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001907};
1908
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001909/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001910 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911 * things if requested. Note that interrupts are hard-disabled
1912 * here so there is no possibility of being interrupted.
1913 */
1914static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001915 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916{
1917 u64 period = event->hw.sample_period;
1918 s64 prev, delta, left;
1919 int record = 0;
1920
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001921 if (event->hw.state & PERF_HES_STOPPED) {
1922 write_pmc(event->hw.idx, 0);
1923 return;
1924 }
1925
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001927 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001928 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001929 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930
1931 /*
1932 * See if the total period for this event has expired,
1933 * and update for the next period.
1934 */
1935 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001936 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001937 if (delta == 0)
1938 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001939 if (period) {
1940 if (left <= 0) {
1941 left += period;
1942 if (left <= 0)
1943 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001944 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001945 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001946 }
1947 if (left < 0x80000000LL)
1948 val = 0x80000000LL - left;
1949 }
1950
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001951 write_pmc(event->hw.idx, val);
1952 local64_set(&event->hw.prev_count, val);
1953 local64_set(&event->hw.period_left, left);
1954 perf_event_update_userpage(event);
1955
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001956 /*
1957 * Finally record data if requested.
1958 */
1959 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001960 struct perf_sample_data data;
1961
Robert Richterfd0d0002012-04-02 20:19:08 +02001962 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963
1964 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1965 perf_get_data_addr(regs, &data.addr);
1966
Anshuman Khandual3925f462013-04-22 19:42:44 +00001967 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
1968 struct cpu_hw_events *cpuhw;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001969 cpuhw = this_cpu_ptr(&cpu_hw_events);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001970 power_pmu_bhrb_read(cpuhw);
1971 data.br_stack = &cpuhw->bhrb_stack;
1972 }
1973
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001974 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001975 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977}
1978
1979/*
1980 * Called from generic code to get the misc flags (i.e. processor mode)
1981 * for an event_id.
1982 */
1983unsigned long perf_misc_flags(struct pt_regs *regs)
1984{
1985 u32 flags = perf_get_misc_flags(regs);
1986
1987 if (flags)
1988 return flags;
1989 return user_mode(regs) ? PERF_RECORD_MISC_USER :
1990 PERF_RECORD_MISC_KERNEL;
1991}
1992
1993/*
1994 * Called from generic code to get the instruction pointer
1995 * for an event_id.
1996 */
1997unsigned long perf_instruction_pointer(struct pt_regs *regs)
1998{
Michael Ellerman33904052013-04-25 19:28:25 +00001999 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00002001 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00002002 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00002003 else if (use_siar)
2004 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00002005 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00002006 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002007}
2008
Michael Neulingbc09c212012-11-05 15:53:54 +00002009static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11002010{
Anton Blanchard0837e322011-03-09 14:38:42 +11002011 /*
2012 * Events on POWER7 can roll back if a speculative event doesn't
2013 * eventually complete. Unfortunately in some rare cases they will
2014 * raise a performance monitor exception. We need to catch this to
2015 * ensure we reset the PMC. In all cases the PMC will be 256 or less
2016 * cycles from overflow.
2017 *
2018 * We only do this if the first pass fails to find any overflowing
2019 * PMCs because a user might set a period of less than 256 and we
2020 * don't want to mistakenly reset them.
2021 */
Michael Neulingbc09c212012-11-05 15:53:54 +00002022 if ((0x80000000 - val) <= 256)
2023 return true;
2024
2025 return false;
2026}
2027
2028static bool pmc_overflow(unsigned long val)
2029{
2030 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11002031 return true;
2032
2033 return false;
2034}
2035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002036/*
2037 * Performance monitor interrupt stuff
2038 */
2039static void perf_event_interrupt(struct pt_regs *regs)
2040{
Michael Neulingbc09c212012-11-05 15:53:54 +00002041 int i, j;
Christoph Lameter69111ba2014-10-21 15:23:25 -05002042 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002043 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00002044 unsigned long val[8];
2045 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002046 int nmi;
2047
2048 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10002049 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050 mfspr(SPRN_PMC6));
2051
2052 perf_read_regs(regs);
2053
2054 nmi = perf_intr_is_nmi(regs);
2055 if (nmi)
2056 nmi_enter();
2057 else
2058 irq_enter();
2059
Michael Neulingbc09c212012-11-05 15:53:54 +00002060 /* Read all the PMCs since we'll need them a bunch of times */
2061 for (i = 0; i < ppmu->n_counter; ++i)
2062 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063
Michael Neulingbc09c212012-11-05 15:53:54 +00002064 /* Try to find what caused the IRQ */
2065 found = 0;
2066 for (i = 0; i < ppmu->n_counter; ++i) {
2067 if (!pmc_overflow(val[i]))
2068 continue;
2069 if (is_limited_pmc(i + 1))
2070 continue; /* these won't generate IRQs */
2071 /*
2072 * We've found one that's overflowed. For active
2073 * counters we need to log this. For inactive
2074 * counters, we need to reset it anyway
2075 */
2076 found = 1;
2077 active = 0;
2078 for (j = 0; j < cpuhw->n_events; ++j) {
2079 event = cpuhw->event[j];
2080 if (event->hw.idx == (i + 1)) {
2081 active = 1;
2082 record_and_restart(event, val[i], regs);
2083 break;
2084 }
2085 }
2086 if (!active)
2087 /* reset non active counters that have overflowed */
2088 write_pmc(i + 1, 0);
2089 }
2090 if (!found && pvr_version_is(PVR_POWER7)) {
2091 /* check active counters for special buggy p7 overflow */
2092 for (i = 0; i < cpuhw->n_events; ++i) {
2093 event = cpuhw->event[i];
2094 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002095 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00002096 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2097 /* event has overflowed in a buggy way*/
2098 found = 1;
2099 record_and_restart(event,
2100 val[event->hw.idx - 1],
2101 regs);
2102 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103 }
2104 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00002105 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00002106 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107
2108 /*
2109 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02002110 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111 * and thus allow interrupts to occur again.
2112 * XXX might want to use MSR.PM to keep the events frozen until
2113 * we get back out of this interrupt.
2114 */
2115 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
2116
2117 if (nmi)
2118 nmi_exit();
2119 else
2120 irq_exit();
2121}
2122
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002123static void power_pmu_setup(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002124{
2125 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2126
2127 if (!ppmu)
2128 return;
2129 memset(cpuhw, 0, sizeof(*cpuhw));
2130 cpuhw->mmcr[0] = MMCR0_FC;
2131}
2132
Paul Gortmaker061d19f2013-06-24 15:30:09 -04002133static int
Peter Zijlstra85cfabb2010-03-11 13:06:56 +01002134power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002135{
2136 unsigned int cpu = (long)hcpu;
2137
2138 switch (action & ~CPU_TASKS_FROZEN) {
2139 case CPU_UP_PREPARE:
2140 power_pmu_setup(cpu);
2141 break;
2142
2143 default:
2144 break;
2145 }
2146
2147 return NOTIFY_OK;
2148}
2149
Paul Gortmaker061d19f2013-06-24 15:30:09 -04002150int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151{
2152 if (ppmu)
2153 return -EBUSY; /* something's already registered */
2154
2155 ppmu = pmu;
2156 pr_info("%s performance monitor hardware support registered\n",
2157 pmu->name);
2158
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08002159 power_pmu.attr_groups = ppmu->attr_groups;
2160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002161#ifdef MSR_HV
2162 /*
2163 * Use FCHV to ignore kernel events if MSR.HV is set.
2164 */
2165 if (mfmsr() & MSR_HV)
2166 freeze_events_kernel = MMCR0_FCHV;
2167#endif /* CONFIG_PPC64 */
2168
Peter Zijlstra2e80a822010-11-17 23:17:36 +01002169 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002170 perf_cpu_notifier(power_pmu_notifier);
2171
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172 return 0;
2173}