blob: bf949623de9079862183dfe34caaf9c6eb3fbab9 [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
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070051 unsigned int txn_flags;
Lin Ming8e6d5572010-05-08 20:28:41 +100052 int n_txn_start;
Anshuman Khandual3925f462013-04-22 19:42:44 +000053
54 /* BHRB bits */
55 u64 bhrb_filter; /* BHRB HW branch filter */
Anshuman Khandualf0322f72015-06-30 13:50:28 +053056 unsigned int bhrb_users;
Anshuman Khandual3925f462013-04-22 19:42:44 +000057 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) {}
Peter Zijlstraacba3c72015-01-14 14:15:39 +0100127static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
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{
Anton Blanchard72e349f2015-05-26 15:10:24 +1000134 /*
135 * When we take a performance monitor exception the regs are setup
136 * using perf_read_regs() which overloads some fields, in particular
137 * regs->result to tell us whether to use SIAR.
138 *
139 * However if the regs are from another exception, eg. a syscall, then
140 * they have not been setup using perf_read_regs() and so regs->result
141 * is something random.
142 */
143 return ((TRAP(regs) == 0xf00) && regs->result);
Michael Ellerman33904052013-04-25 19:28:25 +0000144}
145
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200146/*
147 * Things that are specific to 64-bit implementations.
148 */
149#ifdef CONFIG_PPC64
150
151static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
152{
153 unsigned long mmcra = regs->dsisr;
154
Michael Ellerman7a786832013-04-25 19:28:23 +0000155 if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200156 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
157 if (slot > 1)
158 return 4 * (slot - 1);
159 }
Michael Ellerman7a786832013-04-25 19:28:23 +0000160
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200161 return 0;
162}
163
164/*
165 * The user wants a data address recorded.
166 * If we're not doing instruction sampling, give them the SDAR
167 * (sampled data address). If we are doing instruction sampling, then
168 * only give them the SDAR if it corresponds to the instruction
Michael Ellerman58a032c2013-05-15 20:19:31 +0000169 * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
170 * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200171 */
172static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
173{
174 unsigned long mmcra = regs->dsisr;
Michael Ellerman58a032c2013-05-15 20:19:31 +0000175 bool sdar_valid;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000176
Michael Ellerman58a032c2013-05-15 20:19:31 +0000177 if (ppmu->flags & PPMU_HAS_SIER)
178 sdar_valid = regs->dar & SIER_SDAR_VALID;
179 else {
180 unsigned long sdsync;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200181
Michael Ellerman58a032c2013-05-15 20:19:31 +0000182 if (ppmu->flags & PPMU_SIAR_VALID)
183 sdsync = POWER7P_MMCRA_SDAR_VALID;
184 else if (ppmu->flags & PPMU_ALT_SIPR)
185 sdsync = POWER6_MMCRA_SDSYNC;
186 else
187 sdsync = MMCRA_SDSYNC;
188
189 sdar_valid = mmcra & sdsync;
190 }
191
192 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200193 *addrp = mfspr(SPRN_SDAR);
194}
195
Michael Ellerman5682c462013-04-25 19:28:24 +0000196static bool regs_sihv(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000197{
198 unsigned long sihv = MMCRA_SIHV;
199
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000200 if (ppmu->flags & PPMU_HAS_SIER)
201 return !!(regs->dar & SIER_SIHV);
202
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000203 if (ppmu->flags & PPMU_ALT_SIPR)
204 sihv = POWER6_MMCRA_SIHV;
205
Michael Ellerman5682c462013-04-25 19:28:24 +0000206 return !!(regs->dsisr & sihv);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000207}
208
Michael Ellerman5682c462013-04-25 19:28:24 +0000209static bool regs_sipr(struct pt_regs *regs)
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000210{
211 unsigned long sipr = MMCRA_SIPR;
212
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000213 if (ppmu->flags & PPMU_HAS_SIER)
214 return !!(regs->dar & SIER_SIPR);
215
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000216 if (ppmu->flags & PPMU_ALT_SIPR)
217 sipr = POWER6_MMCRA_SIPR;
218
Michael Ellerman5682c462013-04-25 19:28:24 +0000219 return !!(regs->dsisr & sipr);
Anton Blanchard68b30bb2012-06-26 01:00:13 +0000220}
221
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000222static inline u32 perf_flags_from_msr(struct pt_regs *regs)
223{
224 if (regs->msr & MSR_PR)
225 return PERF_RECORD_MISC_USER;
226 if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
227 return PERF_RECORD_MISC_HYPERVISOR;
228 return PERF_RECORD_MISC_KERNEL;
229}
230
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200231static inline u32 perf_get_misc_flags(struct pt_regs *regs)
232{
Michael Ellerman33904052013-04-25 19:28:25 +0000233 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200234
Anton Blanchard75382aa2012-06-26 01:01:36 +0000235 if (!use_siar)
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000236 return perf_flags_from_msr(regs);
237
238 /*
239 * If we don't have flags in MMCRA, rather than using
240 * the MSR, we intuit the flags from the address in
241 * SIAR which should give slightly more reliable
242 * results
243 */
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000244 if (ppmu->flags & PPMU_NO_SIPR) {
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +0000245 unsigned long siar = mfspr(SPRN_SIAR);
246 if (siar >= PAGE_OFFSET)
247 return PERF_RECORD_MISC_KERNEL;
248 return PERF_RECORD_MISC_USER;
249 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200250
Michael Neuling7abb8402009-10-14 19:32:15 +0000251 /* PR has priority over HV, so order below is important */
Michael Ellerman5682c462013-04-25 19:28:24 +0000252 if (regs_sipr(regs))
Michael Neuling7abb8402009-10-14 19:32:15 +0000253 return PERF_RECORD_MISC_USER;
Michael Ellerman5682c462013-04-25 19:28:24 +0000254
255 if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200256 return PERF_RECORD_MISC_HYPERVISOR;
Michael Ellerman5682c462013-04-25 19:28:24 +0000257
Michael Neuling7abb8402009-10-14 19:32:15 +0000258 return PERF_RECORD_MISC_KERNEL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200259}
260
261/*
262 * Overload regs->dsisr to store MMCRA so we only need to read it once
263 * on each interrupt.
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000264 * Overload regs->dar to store SIER if we have it.
Anton Blanchard75382aa2012-06-26 01:01:36 +0000265 * Overload regs->result to specify whether we should use the MSR (result
266 * is zero) or the SIAR (result is non zero).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200267 */
268static inline void perf_read_regs(struct pt_regs *regs)
269{
Anton Blanchard75382aa2012-06-26 01:01:36 +0000270 unsigned long mmcra = mfspr(SPRN_MMCRA);
271 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
272 int use_siar;
273
Michael Ellerman5682c462013-04-25 19:28:24 +0000274 regs->dsisr = mmcra;
Michael Ellerman860aad72013-04-25 19:28:26 +0000275
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000276 if (ppmu->flags & PPMU_HAS_SIER)
277 regs->dar = mfspr(SPRN_SIER);
Michael Ellerman8f61aa32013-04-25 19:28:27 +0000278
279 /*
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000280 * If this isn't a PMU exception (eg a software event) the SIAR is
281 * not valid. Use pt_regs.
282 *
283 * If it is a marked event use the SIAR.
284 *
285 * If the PMU doesn't update the SIAR for non marked events use
286 * pt_regs.
287 *
288 * If the PMU has HV/PR flags then check to see if they
289 * place the exception in userspace. If so, use pt_regs. In
290 * continuous sampling mode the SIAR and the PMU exception are
291 * not synchronised, so they may be many instructions apart.
292 * This can result in confusing backtraces. We still want
293 * hypervisor samples as well as samples in the kernel with
294 * interrupts off hence the userspace check.
295 */
Anton Blanchard75382aa2012-06-26 01:01:36 +0000296 if (TRAP(regs) != 0xf00)
297 use_siar = 0;
Anton Blanchard5c093ef2012-06-26 01:02:15 +0000298 else if (marked)
299 use_siar = 1;
300 else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
301 use_siar = 0;
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000302 else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +0000303 use_siar = 0;
304 else
305 use_siar = 1;
306
Michael Ellermancbda6aa2013-05-15 20:19:30 +0000307 regs->result = use_siar;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200308}
309
310/*
311 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
312 * it as an NMI.
313 */
314static inline int perf_intr_is_nmi(struct pt_regs *regs)
315{
316 return !regs->softe;
317}
318
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000319/*
320 * On processors like P7+ that have the SIAR-Valid bit, marked instructions
321 * must be sampled only if the SIAR-valid bit is set.
322 *
323 * For unmarked instructions and for processors that don't have the SIAR-Valid
324 * bit, assume that SIAR is valid.
325 */
326static inline int siar_valid(struct pt_regs *regs)
327{
328 unsigned long mmcra = regs->dsisr;
329 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
330
Michael Ellerman58a032c2013-05-15 20:19:31 +0000331 if (marked) {
332 if (ppmu->flags & PPMU_HAS_SIER)
333 return regs->dar & SIER_SIAR_VALID;
334
335 if (ppmu->flags & PPMU_SIAR_VALID)
336 return mmcra & POWER7P_MMCRA_SIAR_VALID;
337 }
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +0000338
339 return 1;
340}
341
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000342
343/* Reset all possible BHRB entries */
344static void power_pmu_bhrb_reset(void)
345{
346 asm volatile(PPC_CLRBHRB);
347}
348
349static void power_pmu_bhrb_enable(struct perf_event *event)
350{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500351 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000352
353 if (!ppmu->bhrb_nr)
354 return;
355
356 /* Clear BHRB if we changed task context to avoid data leaks */
357 if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
358 power_pmu_bhrb_reset();
359 cpuhw->bhrb_context = event->ctx;
360 }
361 cpuhw->bhrb_users++;
Peter Zijlstraacba3c72015-01-14 14:15:39 +0100362 perf_sched_cb_inc(event->ctx->pmu);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000363}
364
365static void power_pmu_bhrb_disable(struct perf_event *event)
366{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500367 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000368
369 if (!ppmu->bhrb_nr)
370 return;
371
Anshuman Khandualf0322f72015-06-30 13:50:28 +0530372 WARN_ON_ONCE(!cpuhw->bhrb_users);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000373 cpuhw->bhrb_users--;
Peter Zijlstraacba3c72015-01-14 14:15:39 +0100374 perf_sched_cb_dec(event->ctx->pmu);
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000375
376 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
377 /* BHRB cannot be turned off when other
378 * events are active on the PMU.
379 */
380
381 /* avoid stale pointer */
382 cpuhw->bhrb_context = NULL;
383 }
384}
385
386/* Called from ctxsw to prevent one process's branch entries to
387 * mingle with the other process's entries during context switch.
388 */
Peter Zijlstraacba3c72015-01-14 14:15:39 +0100389static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000390{
Peter Zijlstraacba3c72015-01-14 14:15:39 +0100391 if (!ppmu->bhrb_nr)
392 return;
393
394 if (sched_in)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000395 power_pmu_bhrb_reset();
396}
Michael Neuling69123182013-05-13 18:44:58 +0000397/* Calculate the to address for a branch */
398static __u64 power_pmu_bhrb_to(u64 addr)
399{
400 unsigned int instr;
401 int ret;
402 __u64 target;
403
Ravi Bangoria77b318a2017-12-12 17:59:15 +0530404 if (is_kernel_addr(addr)) {
405 if (probe_kernel_read(&instr, (void *)addr, sizeof(instr)))
406 return 0;
407
408 return branch_target(&instr);
409 }
Michael Neuling69123182013-05-13 18:44:58 +0000410
411 /* Userspace: need copy instruction here then translate it */
412 pagefault_disable();
413 ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
414 if (ret) {
415 pagefault_enable();
416 return 0;
417 }
418 pagefault_enable();
419
420 target = branch_target(&instr);
421 if ((!target) || (instr & BRANCH_ABSOLUTE))
422 return target;
423
424 /* Translate relative branch target from kernel to user address */
425 return target - (unsigned long)&instr + addr;
426}
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000427
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000428/* Processing BHRB entries */
Anton Blancharde51df2c2014-08-20 08:55:18 +1000429static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000430{
431 u64 val;
432 u64 addr;
Michael Neuling506e70d2013-05-13 18:44:57 +0000433 int r_index, u_index, pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000434
435 r_index = 0;
436 u_index = 0;
437 while (r_index < ppmu->bhrb_nr) {
438 /* Assembly read function */
Michael Neuling506e70d2013-05-13 18:44:57 +0000439 val = read_bhrb(r_index++);
440 if (!val)
441 /* Terminal marker: End of valid BHRB entries */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000442 break;
Michael Neuling506e70d2013-05-13 18:44:57 +0000443 else {
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000444 addr = val & BHRB_EA;
445 pred = val & BHRB_PREDICTION;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000446
Michael Neuling506e70d2013-05-13 18:44:57 +0000447 if (!addr)
448 /* invalid entry */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000449 continue;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000450
Michael Neuling506e70d2013-05-13 18:44:57 +0000451 /* Branches are read most recent first (ie. mfbhrb 0 is
452 * the most recent branch).
453 * There are two types of valid entries:
454 * 1) a target entry which is the to address of a
455 * computed goto like a blr,bctr,btar. The next
456 * entry read from the bhrb will be branch
457 * corresponding to this target (ie. the actual
458 * blr/bctr/btar instruction).
459 * 2) a from address which is an actual branch. If a
460 * target entry proceeds this, then this is the
461 * matching branch for that target. If this is not
462 * following a target entry, then this is a branch
463 * where the target is given as an immediate field
464 * in the instruction (ie. an i or b form branch).
465 * In this case we need to read the instruction from
466 * memory to determine the target/to address.
467 */
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000468
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000469 if (val & BHRB_TARGET) {
Michael Neuling506e70d2013-05-13 18:44:57 +0000470 /* Target branches use two entries
471 * (ie. computed gotos/XL form)
472 */
473 cpuhw->bhrb_entries[u_index].to = addr;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000474 cpuhw->bhrb_entries[u_index].mispred = pred;
475 cpuhw->bhrb_entries[u_index].predicted = ~pred;
476
Michael Neuling506e70d2013-05-13 18:44:57 +0000477 /* Get from address in next entry */
478 val = read_bhrb(r_index++);
479 addr = val & BHRB_EA;
480 if (val & BHRB_TARGET) {
481 /* Shouldn't have two targets in a
482 row.. Reset index and try again */
483 r_index--;
484 addr = 0;
485 }
486 cpuhw->bhrb_entries[u_index].from = addr;
487 } else {
488 /* Branches to immediate field
489 (ie I or B form) */
490 cpuhw->bhrb_entries[u_index].from = addr;
Michael Neuling69123182013-05-13 18:44:58 +0000491 cpuhw->bhrb_entries[u_index].to =
492 power_pmu_bhrb_to(addr);
Michael Neuling506e70d2013-05-13 18:44:57 +0000493 cpuhw->bhrb_entries[u_index].mispred = pred;
494 cpuhw->bhrb_entries[u_index].predicted = ~pred;
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000495 }
Michael Neuling506e70d2013-05-13 18:44:57 +0000496 u_index++;
497
Michael Neulingd52f2dc2013-05-13 18:44:56 +0000498 }
499 }
500 cpuhw->bhrb_stack.nr = u_index;
501 return;
502}
503
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000504static bool is_ebb_event(struct perf_event *event)
505{
506 /*
507 * This could be a per-PMU callback, but we'd rather avoid the cost. We
508 * check that the PMU supports EBB, meaning those that don't can still
509 * use bit 63 of the event code for something else if they wish.
510 */
Joel Stanley4d9690d2014-07-08 16:08:21 +0930511 return (ppmu->flags & PPMU_ARCH_207S) &&
Michael Ellerman8d7c55d2013-07-23 18:07:45 +1000512 ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000513}
514
515static int ebb_event_check(struct perf_event *event)
516{
517 struct perf_event *leader = event->group_leader;
518
519 /* Event and group leader must agree on EBB */
520 if (is_ebb_event(leader) != is_ebb_event(event))
521 return -EINVAL;
522
523 if (is_ebb_event(event)) {
524 if (!(event->attach_state & PERF_ATTACH_TASK))
525 return -EINVAL;
526
527 if (!leader->attr.pinned || !leader->attr.exclusive)
528 return -EINVAL;
529
Michael Ellerman58b5fb02014-03-14 16:00:30 +1100530 if (event->attr.freq ||
531 event->attr.inherit ||
532 event->attr.sample_type ||
533 event->attr.sample_period ||
534 event->attr.enable_on_exec)
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000535 return -EINVAL;
536 }
537
538 return 0;
539}
540
541static void ebb_event_add(struct perf_event *event)
542{
543 if (!is_ebb_event(event) || current->thread.used_ebb)
544 return;
545
546 /*
547 * IFF this is the first time we've added an EBB event, set
548 * PMXE in the user MMCR0 so we can detect when it's cleared by
549 * userspace. We need this so that we can context switch while
550 * userspace is in the EBB handler (where PMXE is 0).
551 */
552 current->thread.used_ebb = 1;
553 current->thread.mmcr0 |= MMCR0_PMXE;
554}
555
556static void ebb_switch_out(unsigned long mmcr0)
557{
558 if (!(mmcr0 & MMCR0_EBE))
559 return;
560
561 current->thread.siar = mfspr(SPRN_SIAR);
562 current->thread.sier = mfspr(SPRN_SIER);
563 current->thread.sdar = mfspr(SPRN_SDAR);
564 current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
565 current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
566}
567
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000568static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000569{
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000570 unsigned long mmcr0 = cpuhw->mmcr[0];
571
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000572 if (!ebb)
573 goto out;
574
Michael Ellerman76cb8a72014-03-14 16:00:34 +1100575 /* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
576 mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000577
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100578 /*
579 * Add any bits from the user MMCR0, FC or PMAO. This is compatible
580 * with pmao_restore_workaround() because we may add PMAO but we never
581 * clear it here.
582 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000583 mmcr0 |= current->thread.mmcr0;
584
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100585 /*
586 * Be careful not to set PMXE if userspace had it cleared. This is also
587 * compatible with pmao_restore_workaround() because it has already
588 * cleared PMXE and we leave PMAO alone.
589 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000590 if (!(current->thread.mmcr0 & MMCR0_PMXE))
591 mmcr0 &= ~MMCR0_PMXE;
592
593 mtspr(SPRN_SIAR, current->thread.siar);
594 mtspr(SPRN_SIER, current->thread.sier);
595 mtspr(SPRN_SDAR, current->thread.sdar);
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000596
597 /*
598 * Merge the kernel & user values of MMCR2. The semantics we implement
599 * are that the user MMCR2 can set bits, ie. cause counters to freeze,
600 * but not clear bits. If a task wants to be able to clear bits, ie.
601 * unfreeze counters, it should not set exclude_xxx in its events and
602 * instead manage the MMCR2 entirely by itself.
603 */
604 mtspr(SPRN_MMCR2, cpuhw->mmcr[3] | current->thread.mmcr2);
Michael Ellerman330a1eb2013-06-28 18:15:16 +1000605out:
606 return mmcr0;
607}
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100608
609static void pmao_restore_workaround(bool ebb)
610{
611 unsigned pmcs[6];
612
613 if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
614 return;
615
616 /*
617 * On POWER8E there is a hardware defect which affects the PMU context
618 * switch logic, ie. power_pmu_disable/enable().
619 *
620 * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
621 * by the hardware. Sometime later the actual PMU exception is
622 * delivered.
623 *
624 * If we context switch, or simply disable/enable, the PMU prior to the
625 * exception arriving, the exception will be lost when we clear PMAO.
626 *
627 * When we reenable the PMU, we will write the saved MMCR0 with PMAO
628 * set, and this _should_ generate an exception. However because of the
629 * defect no exception is generated when we write PMAO, and we get
630 * stuck with no counters counting but no exception delivered.
631 *
632 * The workaround is to detect this case and tweak the hardware to
633 * create another pending PMU exception.
634 *
635 * We do that by setting up PMC6 (cycles) for an imminent overflow and
636 * enabling the PMU. That causes a new exception to be generated in the
637 * chip, but we don't take it yet because we have interrupts hard
638 * disabled. We then write back the PMU state as we want it to be seen
639 * by the exception handler. When we reenable interrupts the exception
640 * handler will be called and see the correct state.
641 *
642 * The logic is the same for EBB, except that the exception is gated by
643 * us having interrupts hard disabled as well as the fact that we are
644 * not in userspace. The exception is finally delivered when we return
645 * to userspace.
646 */
647
648 /* Only if PMAO is set and PMAO_SYNC is clear */
649 if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
650 return;
651
652 /* If we're doing EBB, only if BESCR[GE] is set */
653 if (ebb && !(current->thread.bescr & BESCR_GE))
654 return;
655
656 /*
657 * We are already soft-disabled in power_pmu_enable(). We need to hard
Madhavan Srinivasan58bffb52016-03-04 10:31:49 +0530658 * disable to actually prevent the PMU exception from firing.
Michael Ellermanc2e37a22014-03-14 16:00:29 +1100659 */
660 hard_irq_disable();
661
662 /*
663 * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
664 * Using read/write_pmc() in a for loop adds 12 function calls and
665 * almost doubles our code size.
666 */
667 pmcs[0] = mfspr(SPRN_PMC1);
668 pmcs[1] = mfspr(SPRN_PMC2);
669 pmcs[2] = mfspr(SPRN_PMC3);
670 pmcs[3] = mfspr(SPRN_PMC4);
671 pmcs[4] = mfspr(SPRN_PMC5);
672 pmcs[5] = mfspr(SPRN_PMC6);
673
674 /* Ensure all freeze bits are unset */
675 mtspr(SPRN_MMCR2, 0);
676
677 /* Set up PMC6 to overflow in one cycle */
678 mtspr(SPRN_PMC6, 0x7FFFFFFE);
679
680 /* Enable exceptions and unfreeze PMC6 */
681 mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
682
683 /* Now we need to refreeze and restore the PMCs */
684 mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
685
686 mtspr(SPRN_PMC1, pmcs[0]);
687 mtspr(SPRN_PMC2, pmcs[1]);
688 mtspr(SPRN_PMC3, pmcs[2]);
689 mtspr(SPRN_PMC4, pmcs[3]);
690 mtspr(SPRN_PMC5, pmcs[4]);
691 mtspr(SPRN_PMC6, pmcs[5]);
692}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200693#endif /* CONFIG_PPC64 */
694
695static void perf_event_interrupt(struct pt_regs *regs);
696
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697/*
Ingo Molnar57c0c152009-09-21 12:20:38 +0200698 * Read one performance monitor counter (PMC).
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200699 */
700static unsigned long read_pmc(int idx)
701{
702 unsigned long val;
703
704 switch (idx) {
705 case 1:
706 val = mfspr(SPRN_PMC1);
707 break;
708 case 2:
709 val = mfspr(SPRN_PMC2);
710 break;
711 case 3:
712 val = mfspr(SPRN_PMC3);
713 break;
714 case 4:
715 val = mfspr(SPRN_PMC4);
716 break;
717 case 5:
718 val = mfspr(SPRN_PMC5);
719 break;
720 case 6:
721 val = mfspr(SPRN_PMC6);
722 break;
723#ifdef CONFIG_PPC64
724 case 7:
725 val = mfspr(SPRN_PMC7);
726 break;
727 case 8:
728 val = mfspr(SPRN_PMC8);
729 break;
730#endif /* CONFIG_PPC64 */
731 default:
732 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
733 val = 0;
734 }
735 return val;
736}
737
738/*
739 * Write one PMC.
740 */
741static void write_pmc(int idx, unsigned long val)
742{
743 switch (idx) {
744 case 1:
745 mtspr(SPRN_PMC1, val);
746 break;
747 case 2:
748 mtspr(SPRN_PMC2, val);
749 break;
750 case 3:
751 mtspr(SPRN_PMC3, val);
752 break;
753 case 4:
754 mtspr(SPRN_PMC4, val);
755 break;
756 case 5:
757 mtspr(SPRN_PMC5, val);
758 break;
759 case 6:
760 mtspr(SPRN_PMC6, val);
761 break;
762#ifdef CONFIG_PPC64
763 case 7:
764 mtspr(SPRN_PMC7, val);
765 break;
766 case 8:
767 mtspr(SPRN_PMC8, val);
768 break;
769#endif /* CONFIG_PPC64 */
770 default:
771 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
772 }
773}
774
Anshuman Khandual5f6d0382014-03-14 16:00:27 +1100775/* Called from sysrq_handle_showregs() */
776void perf_event_print_debug(void)
777{
778 unsigned long sdar, sier, flags;
779 u32 pmcs[MAX_HWEVENTS];
780 int i;
781
782 if (!ppmu->n_counter)
783 return;
784
785 local_irq_save(flags);
786
787 pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
788 smp_processor_id(), ppmu->name, ppmu->n_counter);
789
790 for (i = 0; i < ppmu->n_counter; i++)
791 pmcs[i] = read_pmc(i + 1);
792
793 for (; i < MAX_HWEVENTS; i++)
794 pmcs[i] = 0xdeadbeef;
795
796 pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
797 pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
798
799 if (ppmu->n_counter > 4)
800 pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
801 pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
802
803 pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
804 mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
805
806 sdar = sier = 0;
807#ifdef CONFIG_PPC64
808 sdar = mfspr(SPRN_SDAR);
809
810 if (ppmu->flags & PPMU_HAS_SIER)
811 sier = mfspr(SPRN_SIER);
812
Joel Stanley4d9690d2014-07-08 16:08:21 +0930813 if (ppmu->flags & PPMU_ARCH_207S) {
Anshuman Khandual5f6d0382014-03-14 16:00:27 +1100814 pr_info("MMCR2: %016lx EBBHR: %016lx\n",
815 mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
816 pr_info("EBBRR: %016lx BESCR: %016lx\n",
817 mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
818 }
819#endif
820 pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
821 mfspr(SPRN_SIAR), sdar, sier);
822
823 local_irq_restore(flags);
824}
825
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200826/*
827 * Check if a set of events can all go on the PMU at once.
828 * If they can't, this will look at alternative codes for the events
829 * and see if any combination of alternative codes is feasible.
830 * The feasible set is returned in event_id[].
831 */
832static int power_check_constraints(struct cpu_hw_events *cpuhw,
833 u64 event_id[], unsigned int cflags[],
834 int n_ev)
835{
836 unsigned long mask, value, nv;
837 unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
838 int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
839 int i, j;
840 unsigned long addf = ppmu->add_fields;
841 unsigned long tadd = ppmu->test_adder;
842
Paul Mackerrasa8f90e92009-09-22 09:48:08 +1000843 if (n_ev > ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200844 return -1;
845
846 /* First see if the events will go on as-is */
847 for (i = 0; i < n_ev; ++i) {
848 if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
849 && !ppmu->limited_pmc_event(event_id[i])) {
850 ppmu->get_alternatives(event_id[i], cflags[i],
851 cpuhw->alternatives[i]);
852 event_id[i] = cpuhw->alternatives[i][0];
853 }
854 if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
855 &cpuhw->avalues[i][0]))
856 return -1;
857 }
858 value = mask = 0;
859 for (i = 0; i < n_ev; ++i) {
860 nv = (value | cpuhw->avalues[i][0]) +
861 (value & cpuhw->avalues[i][0] & addf);
862 if ((((nv + tadd) ^ value) & mask) != 0 ||
863 (((nv + tadd) ^ cpuhw->avalues[i][0]) &
864 cpuhw->amasks[i][0]) != 0)
865 break;
866 value = nv;
867 mask |= cpuhw->amasks[i][0];
868 }
869 if (i == n_ev)
870 return 0; /* all OK */
871
872 /* doesn't work, gather alternatives... */
873 if (!ppmu->get_alternatives)
874 return -1;
875 for (i = 0; i < n_ev; ++i) {
876 choice[i] = 0;
877 n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
878 cpuhw->alternatives[i]);
879 for (j = 1; j < n_alt[i]; ++j)
880 ppmu->get_constraint(cpuhw->alternatives[i][j],
881 &cpuhw->amasks[i][j],
882 &cpuhw->avalues[i][j]);
883 }
884
885 /* enumerate all possibilities and see if any will work */
886 i = 0;
887 j = -1;
888 value = mask = nv = 0;
889 while (i < n_ev) {
890 if (j >= 0) {
891 /* we're backtracking, restore context */
892 value = svalues[i];
893 mask = smasks[i];
894 j = choice[i];
895 }
896 /*
897 * See if any alternative k for event_id i,
898 * where k > j, will satisfy the constraints.
899 */
900 while (++j < n_alt[i]) {
901 nv = (value | cpuhw->avalues[i][j]) +
902 (value & cpuhw->avalues[i][j] & addf);
903 if ((((nv + tadd) ^ value) & mask) == 0 &&
904 (((nv + tadd) ^ cpuhw->avalues[i][j])
905 & cpuhw->amasks[i][j]) == 0)
906 break;
907 }
908 if (j >= n_alt[i]) {
909 /*
910 * No feasible alternative, backtrack
911 * to event_id i-1 and continue enumerating its
912 * alternatives from where we got up to.
913 */
914 if (--i < 0)
915 return -1;
916 } else {
917 /*
918 * Found a feasible alternative for event_id i,
919 * remember where we got up to with this event_id,
920 * go on to the next event_id, and start with
921 * the first alternative for it.
922 */
923 choice[i] = j;
924 svalues[i] = value;
925 smasks[i] = mask;
926 value = nv;
927 mask |= cpuhw->amasks[i][j];
928 ++i;
929 j = -1;
930 }
931 }
932
933 /* OK, we have a feasible combination, tell the caller the solution */
934 for (i = 0; i < n_ev; ++i)
935 event_id[i] = cpuhw->alternatives[i][choice[i]];
936 return 0;
937}
938
939/*
940 * Check if newly-added events have consistent settings for
941 * exclude_{user,kernel,hv} with each other and any previously
942 * added events.
943 */
944static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
945 int n_prev, int n_new)
946{
947 int eu = 0, ek = 0, eh = 0;
948 int i, n, first;
949 struct perf_event *event;
950
Michael Ellerman9de5cb02014-07-23 21:12:38 +1000951 /*
952 * If the PMU we're on supports per event exclude settings then we
953 * don't need to do any of this logic. NB. This assumes no PMU has both
954 * per event exclude and limited PMCs.
955 */
956 if (ppmu->flags & PPMU_ARCH_207S)
957 return 0;
958
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200959 n = n_prev + n_new;
960 if (n <= 1)
961 return 0;
962
963 first = 1;
964 for (i = 0; i < n; ++i) {
965 if (cflags[i] & PPMU_LIMITED_PMC_OK) {
966 cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
967 continue;
968 }
969 event = ctrs[i];
970 if (first) {
971 eu = event->attr.exclude_user;
972 ek = event->attr.exclude_kernel;
973 eh = event->attr.exclude_hv;
974 first = 0;
975 } else if (event->attr.exclude_user != eu ||
976 event->attr.exclude_kernel != ek ||
977 event->attr.exclude_hv != eh) {
978 return -EAGAIN;
979 }
980 }
981
982 if (eu || ek || eh)
983 for (i = 0; i < n; ++i)
984 if (cflags[i] & PPMU_LIMITED_PMC_OK)
985 cflags[i] |= PPMU_LIMITED_PMC_REQD;
986
987 return 0;
988}
989
Eric B Munson86c74ab2011-04-15 08:12:30 +0000990static u64 check_and_compute_delta(u64 prev, u64 val)
991{
992 u64 delta = (val - prev) & 0xfffffffful;
993
994 /*
995 * POWER7 can roll back counter values, if the new value is smaller
996 * than the previous value it will cause the delta and the counter to
997 * have bogus values unless we rolled a counter over. If a coutner is
998 * rolled back, it will be smaller, but within 256, which is the maximum
Michael Ellerman027dfac2016-06-01 16:34:37 +1000999 * number of events to rollback at once. If we detect a rollback
Eric B Munson86c74ab2011-04-15 08:12:30 +00001000 * return 0. This can lead to a small lack of precision in the
1001 * counters.
1002 */
1003 if (prev > val && (prev - val) < 256)
1004 delta = 0;
1005
1006 return delta;
1007}
1008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009static void power_pmu_read(struct perf_event *event)
1010{
1011 s64 val, delta, prev;
1012
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001013 if (event->hw.state & PERF_HES_STOPPED)
1014 return;
1015
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001016 if (!event->hw.idx)
1017 return;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001018
1019 if (is_ebb_event(event)) {
1020 val = read_pmc(event->hw.idx);
1021 local64_set(&event->hw.prev_count, val);
1022 return;
1023 }
1024
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001025 /*
1026 * Performance monitor interrupts come even when interrupts
1027 * are soft-disabled, as long as interrupts are hard-enabled.
1028 * Therefore we treat them like NMIs.
1029 */
1030 do {
Peter Zijlstrae7850592010-05-21 14:43:08 +02001031 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001032 barrier();
1033 val = read_pmc(event->hw.idx);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001034 delta = check_and_compute_delta(prev, val);
1035 if (!delta)
1036 return;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001037 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038
Peter Zijlstrae7850592010-05-21 14:43:08 +02001039 local64_add(delta, &event->count);
Anton Blanchardf5602942014-05-29 08:15:38 +10001040
1041 /*
1042 * A number of places program the PMC with (0x80000000 - period_left).
1043 * We never want period_left to be less than 1 because we will program
1044 * the PMC with a value >= 0x800000000 and an edge detected PMC will
1045 * roll around to 0 before taking an exception. We have seen this
1046 * on POWER8.
1047 *
1048 * To fix this, clamp the minimum value of period_left to 1.
1049 */
1050 do {
1051 prev = local64_read(&event->hw.period_left);
1052 val = prev - delta;
1053 if (val < 1)
1054 val = 1;
1055 } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001056}
1057
1058/*
1059 * On some machines, PMC5 and PMC6 can't be written, don't respect
1060 * the freeze conditions, and don't generate interrupts. This tells
1061 * us if `event' is using such a PMC.
1062 */
1063static int is_limited_pmc(int pmcnum)
1064{
1065 return (ppmu->flags & PPMU_LIMITED_PMC5_6)
1066 && (pmcnum == 5 || pmcnum == 6);
1067}
1068
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001069static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001070 unsigned long pmc5, unsigned long pmc6)
1071{
1072 struct perf_event *event;
1073 u64 val, prev, delta;
1074 int i;
1075
1076 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001077 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001078 if (!event->hw.idx)
1079 continue;
1080 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001081 prev = local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082 event->hw.idx = 0;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001083 delta = check_and_compute_delta(prev, val);
1084 if (delta)
1085 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086 }
1087}
1088
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001089static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001090 unsigned long pmc5, unsigned long pmc6)
1091{
1092 struct perf_event *event;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001093 u64 val, prev;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001094 int i;
1095
1096 for (i = 0; i < cpuhw->n_limited; ++i) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001097 event = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098 event->hw.idx = cpuhw->limited_hwidx[i];
1099 val = (event->hw.idx == 5) ? pmc5 : pmc6;
Eric B Munson86c74ab2011-04-15 08:12:30 +00001100 prev = local64_read(&event->hw.prev_count);
1101 if (check_and_compute_delta(prev, val))
1102 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103 perf_event_update_userpage(event);
1104 }
1105}
1106
1107/*
1108 * Since limited events don't respect the freeze conditions, we
1109 * have to read them immediately after freezing or unfreezing the
1110 * other events. We try to keep the values from the limited
1111 * events as consistent as possible by keeping the delay (in
1112 * cycles and instructions) between freezing/unfreezing and reading
1113 * the limited events as small and consistent as possible.
1114 * Therefore, if any limited events are in use, we read them
1115 * both, and always in the same order, to minimize variability,
1116 * and do it inside the same asm that writes MMCR0.
1117 */
1118static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
1119{
1120 unsigned long pmc5, pmc6;
1121
1122 if (!cpuhw->n_limited) {
1123 mtspr(SPRN_MMCR0, mmcr0);
1124 return;
1125 }
1126
1127 /*
1128 * Write MMCR0, then read PMC5 and PMC6 immediately.
1129 * To ensure we don't get a performance monitor interrupt
1130 * between writing MMCR0 and freezing/thawing the limited
1131 * events, we first write MMCR0 with the event overflow
1132 * interrupt enable bits turned off.
1133 */
1134 asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
1135 : "=&r" (pmc5), "=&r" (pmc6)
1136 : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
1137 "i" (SPRN_MMCR0),
1138 "i" (SPRN_PMC5), "i" (SPRN_PMC6));
1139
1140 if (mmcr0 & MMCR0_FC)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001141 freeze_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142 else
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001143 thaw_limited_counters(cpuhw, pmc5, pmc6);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144
1145 /*
1146 * Write the full MMCR0 including the event overflow interrupt
1147 * enable bits, if necessary.
1148 */
1149 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
1150 mtspr(SPRN_MMCR0, mmcr0);
1151}
1152
1153/*
1154 * Disable all events to prevent PMU interrupts and to allow
1155 * events to be added or removed.
1156 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001157static void power_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001158{
1159 struct cpu_hw_events *cpuhw;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001160 unsigned long flags, mmcr0, val;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001161
1162 if (!ppmu)
1163 return;
1164 local_irq_save(flags);
Christoph Lameter69111ba2014-10-21 15:23:25 -05001165 cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166
1167 if (!cpuhw->disabled) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001168 /*
1169 * Check if we ever enabled the PMU on this cpu.
1170 */
1171 if (!cpuhw->pmcs_enabled) {
1172 ppc_enable_pmcs();
1173 cpuhw->pmcs_enabled = 1;
1174 }
1175
1176 /*
Michael Ellerman76cb8a72014-03-14 16:00:34 +11001177 * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001178 */
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001179 val = mmcr0 = mfspr(SPRN_MMCR0);
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001180 val |= MMCR0_FC;
Michael Ellerman76cb8a72014-03-14 16:00:34 +11001181 val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
1182 MMCR0_FC56);
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001183
1184 /*
1185 * The barrier is to make sure the mtspr has been
1186 * executed and the PMU has frozen the events etc.
1187 * before we return.
1188 */
1189 write_mmcr0(cpuhw, val);
1190 mb();
1191
1192 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193 * Disable instruction sampling if it was enabled
1194 */
1195 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1196 mtspr(SPRN_MMCRA,
1197 cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1198 mb();
1199 }
1200
Michael Ellerman378a6ee2013-06-28 18:15:11 +10001201 cpuhw->disabled = 1;
1202 cpuhw->n_added = 0;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001203
1204 ebb_switch_out(mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001207 local_irq_restore(flags);
1208}
1209
1210/*
1211 * Re-enable all events if disable == 0.
1212 * If we were previously disabled and events were added, then
1213 * put the new config on the PMU.
1214 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001215static void power_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216{
1217 struct perf_event *event;
1218 struct cpu_hw_events *cpuhw;
1219 unsigned long flags;
1220 long i;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001221 unsigned long val, mmcr0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001222 s64 left;
1223 unsigned int hwc_index[MAX_HWEVENTS];
1224 int n_lim;
1225 int idx;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001226 bool ebb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227
1228 if (!ppmu)
1229 return;
1230 local_irq_save(flags);
Michael Ellerman0a488432013-06-28 18:15:13 +10001231
Christoph Lameter69111ba2014-10-21 15:23:25 -05001232 cpuhw = this_cpu_ptr(&cpu_hw_events);
Michael Ellerman0a488432013-06-28 18:15:13 +10001233 if (!cpuhw->disabled)
1234 goto out;
1235
Michael Ellerman4ea355b2013-06-28 18:15:14 +10001236 if (cpuhw->n_events == 0) {
1237 ppc_set_pmu_inuse(0);
1238 goto out;
1239 }
1240
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 cpuhw->disabled = 0;
1242
1243 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001244 * EBB requires an exclusive group and all events must have the EBB
1245 * flag set, or not set, so we can just check a single event. Also we
1246 * know we have at least one event.
1247 */
1248 ebb = is_ebb_event(cpuhw->event[0]);
1249
1250 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 * If we didn't change anything, or only removed events,
1252 * no need to recalculate MMCR* settings and reset the PMCs.
1253 * Just reenable the PMU with the current MMCR* settings
1254 * (possibly updated for removal of events).
1255 */
1256 if (!cpuhw->n_added) {
1257 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1258 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259 goto out_enable;
1260 }
1261
1262 /*
Michael Ellerman79a4cb22014-07-23 21:12:36 +10001263 * Clear all MMCR settings and recompute them for the new set of events.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 */
Michael Ellerman79a4cb22014-07-23 21:12:36 +10001265 memset(cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
1266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267 if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
Michael Ellerman8abd8182014-07-23 21:12:37 +10001268 cpuhw->mmcr, cpuhw->event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269 /* shouldn't ever get here */
1270 printk(KERN_ERR "oops compute_mmcr failed\n");
1271 goto out;
1272 }
1273
Michael Ellerman9de5cb02014-07-23 21:12:38 +10001274 if (!(ppmu->flags & PPMU_ARCH_207S)) {
1275 /*
1276 * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
1277 * bits for the first event. We have already checked that all
1278 * events have the same value for these bits as the first event.
1279 */
1280 event = cpuhw->event[0];
1281 if (event->attr.exclude_user)
1282 cpuhw->mmcr[0] |= MMCR0_FCP;
1283 if (event->attr.exclude_kernel)
1284 cpuhw->mmcr[0] |= freeze_events_kernel;
1285 if (event->attr.exclude_hv)
1286 cpuhw->mmcr[0] |= MMCR0_FCHV;
1287 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001288
1289 /*
1290 * Write the new configuration to MMCR* with the freeze
1291 * bit set and set the hardware events to their initial values.
1292 * Then unfreeze the events.
1293 */
1294 ppc_set_pmu_inuse(1);
1295 mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
1296 mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
1297 mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
1298 | MMCR0_FC);
Michael Ellerman9de5cb02014-07-23 21:12:38 +10001299 if (ppmu->flags & PPMU_ARCH_207S)
1300 mtspr(SPRN_MMCR2, cpuhw->mmcr[3]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301
1302 /*
1303 * Read off any pre-existing events that need to move
1304 * to another PMC.
1305 */
1306 for (i = 0; i < cpuhw->n_events; ++i) {
1307 event = cpuhw->event[i];
1308 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
1309 power_pmu_read(event);
1310 write_pmc(event->hw.idx, 0);
1311 event->hw.idx = 0;
1312 }
1313 }
1314
1315 /*
1316 * Initialize the PMCs for all the new and moved events.
1317 */
1318 cpuhw->n_limited = n_lim = 0;
1319 for (i = 0; i < cpuhw->n_events; ++i) {
1320 event = cpuhw->event[i];
1321 if (event->hw.idx)
1322 continue;
1323 idx = hwc_index[i] + 1;
1324 if (is_limited_pmc(idx)) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001325 cpuhw->limited_counter[n_lim] = event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326 cpuhw->limited_hwidx[n_lim] = idx;
1327 ++n_lim;
1328 continue;
1329 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001330
1331 if (ebb)
1332 val = local64_read(&event->hw.prev_count);
1333 else {
1334 val = 0;
1335 if (event->hw.sample_period) {
1336 left = local64_read(&event->hw.period_left);
1337 if (left < 0x80000000L)
1338 val = 0x80000000L - left;
1339 }
1340 local64_set(&event->hw.prev_count, val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341 }
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001342
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001343 event->hw.idx = idx;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001344 if (event->hw.state & PERF_HES_STOPPED)
1345 val = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346 write_pmc(idx, val);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001348 perf_event_update_userpage(event);
1349 }
1350 cpuhw->n_limited = n_lim;
1351 cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
1352
1353 out_enable:
Michael Ellermanc2e37a22014-03-14 16:00:29 +11001354 pmao_restore_workaround(ebb);
1355
Michael Ellerman9de5cb02014-07-23 21:12:38 +10001356 mmcr0 = ebb_switch_in(ebb, cpuhw);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001357
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001358 mb();
Anshuman Khandualb4d6c062013-12-18 13:14:53 +11001359 if (cpuhw->bhrb_users)
1360 ppmu->config_bhrb(cpuhw->bhrb_filter);
1361
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001362 write_mmcr0(cpuhw, mmcr0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363
1364 /*
1365 * Enable instruction sampling if necessary
1366 */
1367 if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
1368 mb();
1369 mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
1370 }
1371
1372 out:
Anshuman Khandual3925f462013-04-22 19:42:44 +00001373
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374 local_irq_restore(flags);
1375}
1376
1377static int collect_events(struct perf_event *group, int max_count,
1378 struct perf_event *ctrs[], u64 *events,
1379 unsigned int *flags)
1380{
1381 int n = 0;
1382 struct perf_event *event;
1383
Ravi Bangoriad532f622017-11-30 14:03:22 +05301384 if (group->pmu->task_ctx_nr == perf_hw_context) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385 if (n >= max_count)
1386 return -1;
1387 ctrs[n] = group;
1388 flags[n] = group->hw.event_base;
1389 events[n++] = group->hw.config;
1390 }
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001391 list_for_each_entry(event, &group->sibling_list, group_entry) {
Ravi Bangoriad532f622017-11-30 14:03:22 +05301392 if (event->pmu->task_ctx_nr == perf_hw_context &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 event->state != PERF_EVENT_STATE_OFF) {
1394 if (n >= max_count)
1395 return -1;
1396 ctrs[n] = event;
1397 flags[n] = event->hw.event_base;
1398 events[n++] = event->hw.config;
1399 }
1400 }
1401 return n;
1402}
1403
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001404/*
1405 * Add a event to the PMU.
1406 * If all events are not already frozen, then we disable and
1407 * re-enable the PMU in order to get hw_perf_enable to do the
1408 * actual work of reconfiguring the PMU.
1409 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001410static int power_pmu_add(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001411{
1412 struct cpu_hw_events *cpuhw;
1413 unsigned long flags;
1414 int n0;
1415 int ret = -EAGAIN;
1416
1417 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001418 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001419
1420 /*
1421 * Add the event to the list (if there is room)
1422 * and check whether the total set is still feasible.
1423 */
Christoph Lameter69111ba2014-10-21 15:23:25 -05001424 cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425 n0 = cpuhw->n_events;
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001426 if (n0 >= ppmu->n_counter)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001427 goto out;
1428 cpuhw->event[n0] = event;
1429 cpuhw->events[n0] = event->hw.config;
1430 cpuhw->flags[n0] = event->hw.event_base;
Lin Ming8e6d5572010-05-08 20:28:41 +10001431
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001432 /*
1433 * This event may have been disabled/stopped in record_and_restart()
1434 * because we exceeded the ->event_limit. If re-starting the event,
1435 * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
1436 * notification is re-enabled.
1437 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001438 if (!(ef_flags & PERF_EF_START))
1439 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
sukadev@linux.vnet.ibm.comf53d1682013-01-24 13:25:23 +00001440 else
1441 event->hw.state = 0;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001442
Lin Ming8e6d5572010-05-08 20:28:41 +10001443 /*
1444 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001445 * skip the schedulability test here, it will be performed
Lin Ming8e6d5572010-05-08 20:28:41 +10001446 * at commit time(->commit_txn) as a whole
1447 */
Sukadev Bhattiprolu8f3e5682015-09-03 20:07:53 -07001448 if (cpuhw->txn_flags & PERF_PMU_TXN_ADD)
Lin Ming8e6d5572010-05-08 20:28:41 +10001449 goto nocheck;
1450
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001451 if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
1452 goto out;
1453 if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
1454 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001455 event->hw.config = cpuhw->events[n0];
Lin Ming8e6d5572010-05-08 20:28:41 +10001456
1457nocheck:
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001458 ebb_event_add(event);
1459
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001460 ++cpuhw->n_events;
1461 ++cpuhw->n_added;
1462
1463 ret = 0;
1464 out:
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301465 if (has_branch_stack(event)) {
Anshuman Khandual3925f462013-04-22 19:42:44 +00001466 power_pmu_bhrb_enable(event);
Anshuman Khandualff3d79d2013-06-10 11:23:29 +05301467 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1468 event->attr.branch_sample_type);
1469 }
Anshuman Khandual3925f462013-04-22 19:42:44 +00001470
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001471 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 local_irq_restore(flags);
1473 return ret;
1474}
1475
1476/*
1477 * Remove a event from the PMU.
1478 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001479static void power_pmu_del(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001480{
1481 struct cpu_hw_events *cpuhw;
1482 long i;
1483 unsigned long flags;
1484
1485 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001486 perf_pmu_disable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487
1488 power_pmu_read(event);
1489
Christoph Lameter69111ba2014-10-21 15:23:25 -05001490 cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001491 for (i = 0; i < cpuhw->n_events; ++i) {
1492 if (event == cpuhw->event[i]) {
Matt Evans219a92a2010-07-05 17:36:32 +00001493 while (++i < cpuhw->n_events) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001494 cpuhw->event[i-1] = cpuhw->event[i];
Matt Evans219a92a2010-07-05 17:36:32 +00001495 cpuhw->events[i-1] = cpuhw->events[i];
1496 cpuhw->flags[i-1] = cpuhw->flags[i];
1497 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001498 --cpuhw->n_events;
1499 ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
1500 if (event->hw.idx) {
1501 write_pmc(event->hw.idx, 0);
1502 event->hw.idx = 0;
1503 }
1504 perf_event_update_userpage(event);
1505 break;
1506 }
1507 }
1508 for (i = 0; i < cpuhw->n_limited; ++i)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001509 if (event == cpuhw->limited_counter[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001510 break;
1511 if (i < cpuhw->n_limited) {
1512 while (++i < cpuhw->n_limited) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001513 cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001514 cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
1515 }
1516 --cpuhw->n_limited;
1517 }
1518 if (cpuhw->n_events == 0) {
1519 /* disable exceptions if no events are running */
1520 cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
1521 }
1522
Anshuman Khandual3925f462013-04-22 19:42:44 +00001523 if (has_branch_stack(event))
1524 power_pmu_bhrb_disable(event);
1525
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001526 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001527 local_irq_restore(flags);
1528}
1529
1530/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001531 * POWER-PMU does not support disabling individual counters, hence
1532 * program their cycle counter to their max value and ignore the interrupts.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001534
1535static void power_pmu_start(struct perf_event *event, int ef_flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001537 unsigned long flags;
1538 s64 left;
Anton Blanchard9a45a942012-02-15 18:48:22 +00001539 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001540
1541 if (!event->hw.idx || !event->hw.sample_period)
1542 return;
1543
1544 if (!(event->hw.state & PERF_HES_STOPPED))
1545 return;
1546
1547 if (ef_flags & PERF_EF_RELOAD)
1548 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1549
1550 local_irq_save(flags);
1551 perf_pmu_disable(event->pmu);
1552
1553 event->hw.state = 0;
1554 left = local64_read(&event->hw.period_left);
Anton Blanchard9a45a942012-02-15 18:48:22 +00001555
1556 val = 0;
1557 if (left < 0x80000000L)
1558 val = 0x80000000L - left;
1559
1560 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001561
1562 perf_event_update_userpage(event);
1563 perf_pmu_enable(event->pmu);
1564 local_irq_restore(flags);
1565}
1566
1567static void power_pmu_stop(struct perf_event *event, int ef_flags)
1568{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569 unsigned long flags;
1570
1571 if (!event->hw.idx || !event->hw.sample_period)
1572 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001573
1574 if (event->hw.state & PERF_HES_STOPPED)
1575 return;
1576
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001578 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001579
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 power_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001581 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
1582 write_pmc(event->hw.idx, 0);
1583
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001585 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001586 local_irq_restore(flags);
1587}
1588
Lin Ming8e6d5572010-05-08 20:28:41 +10001589/*
1590 * Start group events scheduling transaction
1591 * Set the flag to make pmu::enable() not perform the
1592 * schedulability test, it will be performed at commit time
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001593 *
1594 * We only support PERF_PMU_TXN_ADD transactions. Save the
1595 * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
1596 * transactions.
Lin Ming8e6d5572010-05-08 20:28:41 +10001597 */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001598static void power_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
Lin Ming8e6d5572010-05-08 20:28:41 +10001599{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001600 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Lin Ming8e6d5572010-05-08 20:28:41 +10001601
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001602 WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
1603
1604 cpuhw->txn_flags = txn_flags;
1605 if (txn_flags & ~PERF_PMU_TXN_ADD)
1606 return;
1607
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001608 perf_pmu_disable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001609 cpuhw->n_txn_start = cpuhw->n_events;
1610}
1611
1612/*
1613 * Stop group events scheduling transaction
1614 * Clear the flag and pmu::enable() will perform the
1615 * schedulability test.
1616 */
Anton Blancharde51df2c2014-08-20 08:55:18 +10001617static void power_pmu_cancel_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001618{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001619 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001620 unsigned int txn_flags;
1621
1622 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1623
1624 txn_flags = cpuhw->txn_flags;
1625 cpuhw->txn_flags = 0;
1626 if (txn_flags & ~PERF_PMU_TXN_ADD)
1627 return;
Lin Ming8e6d5572010-05-08 20:28:41 +10001628
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001629 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001630}
1631
1632/*
1633 * Commit group events scheduling transaction
1634 * Perform the group schedulability test as a whole
1635 * Return 0 if success
1636 */
Anton Blancharde51df2c2014-08-20 08:55:18 +10001637static int power_pmu_commit_txn(struct pmu *pmu)
Lin Ming8e6d5572010-05-08 20:28:41 +10001638{
1639 struct cpu_hw_events *cpuhw;
1640 long i, n;
1641
1642 if (!ppmu)
1643 return -EAGAIN;
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001644
Christoph Lameter69111ba2014-10-21 15:23:25 -05001645 cpuhw = this_cpu_ptr(&cpu_hw_events);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001646 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1647
1648 if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
1649 cpuhw->txn_flags = 0;
1650 return 0;
1651 }
1652
Lin Ming8e6d5572010-05-08 20:28:41 +10001653 n = cpuhw->n_events;
1654 if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1655 return -EAGAIN;
1656 i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1657 if (i < 0)
1658 return -EAGAIN;
1659
1660 for (i = cpuhw->n_txn_start; i < n; ++i)
1661 cpuhw->event[i]->hw.config = cpuhw->events[i];
1662
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001663 cpuhw->txn_flags = 0;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001664 perf_pmu_enable(pmu);
Lin Ming8e6d5572010-05-08 20:28:41 +10001665 return 0;
1666}
1667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668/*
1669 * Return 1 if we might be able to put event on a limited PMC,
1670 * or 0 if not.
1671 * A event can only go on a limited PMC if it counts something
1672 * that a limited PMC can count, doesn't require interrupts, and
1673 * doesn't exclude any processor mode.
1674 */
1675static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1676 unsigned int flags)
1677{
1678 int n;
1679 u64 alt[MAX_EVENT_ALTERNATIVES];
1680
1681 if (event->attr.exclude_user
1682 || event->attr.exclude_kernel
1683 || event->attr.exclude_hv
1684 || event->attr.sample_period)
1685 return 0;
1686
1687 if (ppmu->limited_pmc_event(ev))
1688 return 1;
1689
1690 /*
1691 * The requested event_id isn't on a limited PMC already;
1692 * see if any alternative code goes on a limited PMC.
1693 */
1694 if (!ppmu->get_alternatives)
1695 return 0;
1696
1697 flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1698 n = ppmu->get_alternatives(ev, flags, alt);
1699
1700 return n > 0;
1701}
1702
1703/*
1704 * Find an alternative event_id that goes on a normal PMC, if possible,
1705 * and return the event_id code, or 0 if there is no such alternative.
1706 * (Note: event_id code 0 is "don't count" on all machines.)
1707 */
1708static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1709{
1710 u64 alt[MAX_EVENT_ALTERNATIVES];
1711 int n;
1712
1713 flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1714 n = ppmu->get_alternatives(ev, flags, alt);
1715 if (!n)
1716 return 0;
1717 return alt[0];
1718}
1719
1720/* Number of perf_events counting hardware events */
1721static atomic_t num_events;
1722/* Used to avoid races in calling reserve/release_pmc_hardware */
1723static DEFINE_MUTEX(pmc_reserve_mutex);
1724
1725/*
1726 * Release the PMU if this is the last perf_event.
1727 */
1728static void hw_perf_event_destroy(struct perf_event *event)
1729{
1730 if (!atomic_add_unless(&num_events, -1, 1)) {
1731 mutex_lock(&pmc_reserve_mutex);
1732 if (atomic_dec_return(&num_events) == 0)
1733 release_pmc_hardware();
1734 mutex_unlock(&pmc_reserve_mutex);
1735 }
1736}
1737
1738/*
1739 * Translate a generic cache event_id config to a raw event_id code.
1740 */
1741static int hw_perf_cache_event(u64 config, u64 *eventp)
1742{
1743 unsigned long type, op, result;
1744 int ev;
1745
1746 if (!ppmu->cache_events)
1747 return -EINVAL;
1748
1749 /* unpack config */
1750 type = config & 0xff;
1751 op = (config >> 8) & 0xff;
1752 result = (config >> 16) & 0xff;
1753
1754 if (type >= PERF_COUNT_HW_CACHE_MAX ||
1755 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1756 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1757 return -EINVAL;
1758
1759 ev = (*ppmu->cache_events)[type][op][result];
1760 if (ev == 0)
1761 return -EOPNOTSUPP;
1762 if (ev == -1)
1763 return -EINVAL;
1764 *eventp = ev;
1765 return 0;
1766}
1767
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001768static int power_pmu_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769{
1770 u64 ev;
1771 unsigned long flags;
1772 struct perf_event *ctrs[MAX_HWEVENTS];
1773 u64 events[MAX_HWEVENTS];
1774 unsigned int cflags[MAX_HWEVENTS];
1775 int n;
1776 int err;
1777 struct cpu_hw_events *cpuhw;
1778
1779 if (!ppmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001780 return -ENOENT;
1781
Anshuman Khandual3925f462013-04-22 19:42:44 +00001782 if (has_branch_stack(event)) {
1783 /* PMU has BHRB enabled */
Joel Stanley4d9690d2014-07-08 16:08:21 +09301784 if (!(ppmu->flags & PPMU_ARCH_207S))
Anshuman Khandual3925f462013-04-22 19:42:44 +00001785 return -EOPNOTSUPP;
1786 }
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001787
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788 switch (event->attr.type) {
1789 case PERF_TYPE_HARDWARE:
1790 ev = event->attr.config;
1791 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001792 return -EOPNOTSUPP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793 ev = ppmu->generic_events[ev];
1794 break;
1795 case PERF_TYPE_HW_CACHE:
1796 err = hw_perf_cache_event(event->attr.config, &ev);
1797 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001798 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799 break;
1800 case PERF_TYPE_RAW:
1801 ev = event->attr.config;
1802 break;
1803 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001804 return -ENOENT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001806
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807 event->hw.config_base = ev;
1808 event->hw.idx = 0;
1809
1810 /*
1811 * If we are not running on a hypervisor, force the
1812 * exclude_hv bit to 0 so that we don't care what
1813 * the user set it to.
1814 */
1815 if (!firmware_has_feature(FW_FEATURE_LPAR))
1816 event->attr.exclude_hv = 0;
1817
1818 /*
1819 * If this is a per-task event, then we can use
1820 * PM_RUN_* events interchangeably with their non RUN_*
1821 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1822 * XXX we should check if the task is an idle task.
1823 */
1824 flags = 0;
Paul Mackerras57fa7212010-10-19 16:55:35 +11001825 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826 flags |= PPMU_ONLY_COUNT_RUN;
1827
1828 /*
1829 * If this machine has limited events, check whether this
1830 * event_id could go on a limited event.
1831 */
1832 if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1833 if (can_go_on_limited_pmc(event, ev, flags)) {
1834 flags |= PPMU_LIMITED_PMC_OK;
1835 } else if (ppmu->limited_pmc_event(ev)) {
1836 /*
1837 * The requested event_id is on a limited PMC,
1838 * but we can't use a limited PMC; see if any
1839 * alternative goes on a normal PMC.
1840 */
1841 ev = normal_pmc_alternative(ev, flags);
1842 if (!ev)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001843 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844 }
1845 }
1846
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001847 /* Extra checks for EBB */
1848 err = ebb_event_check(event);
1849 if (err)
1850 return err;
1851
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001852 /*
1853 * If this is in a group, check if it can go on with all the
1854 * other hardware events in the group. We assume the event
1855 * hasn't been linked into its leader's sibling list at this point.
1856 */
1857 n = 0;
1858 if (event->group_leader != event) {
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10001859 n = collect_events(event->group_leader, ppmu->n_counter - 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860 ctrs, events, cflags);
1861 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001862 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 }
1864 events[n] = ev;
1865 ctrs[n] = event;
1866 cflags[n] = flags;
1867 if (check_excludes(ctrs, cflags, n, 1))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001868 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869
1870 cpuhw = &get_cpu_var(cpu_hw_events);
1871 err = power_check_constraints(cpuhw, events, cflags, n + 1);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001872
1873 if (has_branch_stack(event)) {
1874 cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1875 event->attr.branch_sample_type);
1876
Jan Stancek68de8862015-03-24 08:33:22 -04001877 if (cpuhw->bhrb_filter == -1) {
1878 put_cpu_var(cpu_hw_events);
Anshuman Khandual3925f462013-04-22 19:42:44 +00001879 return -EOPNOTSUPP;
Jan Stancek68de8862015-03-24 08:33:22 -04001880 }
Anshuman Khandual3925f462013-04-22 19:42:44 +00001881 }
1882
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883 put_cpu_var(cpu_hw_events);
1884 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001885 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001886
1887 event->hw.config = events[n];
1888 event->hw.event_base = cflags[n];
1889 event->hw.last_period = event->hw.sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001890 local64_set(&event->hw.period_left, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891
1892 /*
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001893 * For EBB events we just context switch the PMC value, we don't do any
1894 * of the sample_period logic. We use hw.prev_count for this.
1895 */
1896 if (is_ebb_event(event))
1897 local64_set(&event->hw.prev_count, 0);
1898
1899 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900 * See if we need to reserve the PMU.
1901 * If no events are currently in use, then we have to take a
1902 * mutex to ensure that we don't race with another task doing
1903 * reserve_pmc_hardware or release_pmc_hardware.
1904 */
1905 err = 0;
1906 if (!atomic_inc_not_zero(&num_events)) {
1907 mutex_lock(&pmc_reserve_mutex);
1908 if (atomic_read(&num_events) == 0 &&
1909 reserve_pmc_hardware(perf_event_interrupt))
1910 err = -EBUSY;
1911 else
1912 atomic_inc(&num_events);
1913 mutex_unlock(&pmc_reserve_mutex);
1914 }
1915 event->destroy = hw_perf_event_destroy;
1916
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001917 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918}
1919
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001920static int power_pmu_event_idx(struct perf_event *event)
1921{
1922 return event->hw.idx;
1923}
1924
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08001925ssize_t power_events_sysfs_show(struct device *dev,
1926 struct device_attribute *attr, char *page)
1927{
1928 struct perf_pmu_events_attr *pmu_attr;
1929
1930 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
1931
1932 return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
1933}
1934
Anton Blancharde51df2c2014-08-20 08:55:18 +10001935static struct pmu power_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001936 .pmu_enable = power_pmu_enable,
1937 .pmu_disable = power_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001938 .event_init = power_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001939 .add = power_pmu_add,
1940 .del = power_pmu_del,
1941 .start = power_pmu_start,
1942 .stop = power_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001943 .read = power_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001944 .start_txn = power_pmu_start_txn,
1945 .cancel_txn = power_pmu_cancel_txn,
1946 .commit_txn = power_pmu_commit_txn,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01001947 .event_idx = power_pmu_event_idx,
Peter Zijlstraacba3c72015-01-14 14:15:39 +01001948 .sched_task = power_pmu_sched_task,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001949};
1950
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001951/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02001952 * A counter has overflowed; update its count and record
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001953 * things if requested. Note that interrupts are hard-disabled
1954 * here so there is no possibility of being interrupted.
1955 */
1956static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001957 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958{
1959 u64 period = event->hw.sample_period;
1960 s64 prev, delta, left;
1961 int record = 0;
1962
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001963 if (event->hw.state & PERF_HES_STOPPED) {
1964 write_pmc(event->hw.idx, 0);
1965 return;
1966 }
1967
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 /* we don't have to worry about interrupts here */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001969 prev = local64_read(&event->hw.prev_count);
Eric B Munson86c74ab2011-04-15 08:12:30 +00001970 delta = check_and_compute_delta(prev, val);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001971 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972
1973 /*
1974 * See if the total period for this event has expired,
1975 * and update for the next period.
1976 */
1977 val = 0;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001978 left = local64_read(&event->hw.period_left) - delta;
Michael Neulinge13e8952012-11-05 15:08:38 +00001979 if (delta == 0)
1980 left++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001981 if (period) {
1982 if (left <= 0) {
1983 left += period;
1984 if (left <= 0)
1985 left = period;
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00001986 record = siar_valid(regs);
Anton Blanchard4bca7702011-01-17 16:17:42 +11001987 event->hw.last_period = event->hw.sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001988 }
1989 if (left < 0x80000000LL)
1990 val = 0x80000000LL - left;
1991 }
1992
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001993 write_pmc(event->hw.idx, val);
1994 local64_set(&event->hw.prev_count, val);
1995 local64_set(&event->hw.period_left, left);
1996 perf_event_update_userpage(event);
1997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998 /*
1999 * Finally record data if requested.
2000 */
2001 if (record) {
Peter Zijlstradc1d6282010-03-03 15:55:04 +01002002 struct perf_sample_data data;
2003
Robert Richterfd0d0002012-04-02 20:19:08 +02002004 perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002005
2006 if (event->attr.sample_type & PERF_SAMPLE_ADDR)
2007 perf_get_data_addr(regs, &data.addr);
2008
Anshuman Khandual3925f462013-04-22 19:42:44 +00002009 if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
2010 struct cpu_hw_events *cpuhw;
Christoph Lameter69111ba2014-10-21 15:23:25 -05002011 cpuhw = this_cpu_ptr(&cpu_hw_events);
Anshuman Khandual3925f462013-04-22 19:42:44 +00002012 power_pmu_bhrb_read(cpuhw);
2013 data.br_stack = &cpuhw->bhrb_stack;
2014 }
2015
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02002016 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002017 power_pmu_stop(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019}
2020
2021/*
2022 * Called from generic code to get the misc flags (i.e. processor mode)
2023 * for an event_id.
2024 */
2025unsigned long perf_misc_flags(struct pt_regs *regs)
2026{
2027 u32 flags = perf_get_misc_flags(regs);
2028
2029 if (flags)
2030 return flags;
2031 return user_mode(regs) ? PERF_RECORD_MISC_USER :
2032 PERF_RECORD_MISC_KERNEL;
2033}
2034
2035/*
2036 * Called from generic code to get the instruction pointer
2037 * for an event_id.
2038 */
2039unsigned long perf_instruction_pointer(struct pt_regs *regs)
2040{
Michael Ellerman33904052013-04-25 19:28:25 +00002041 bool use_siar = regs_use_siar(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00002043 if (use_siar && siar_valid(regs))
Anton Blanchard75382aa2012-06-26 01:01:36 +00002044 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
sukadev@linux.vnet.ibm.come6878832012-09-18 20:56:11 +00002045 else if (use_siar)
2046 return 0; // no valid instruction pointer
Anton Blanchard75382aa2012-06-26 01:01:36 +00002047 else
Benjamin Herrenschmidt1ce447b2012-03-26 20:47:34 +00002048 return regs->nip;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002049}
2050
Michael Neulingbc09c212012-11-05 15:53:54 +00002051static bool pmc_overflow_power7(unsigned long val)
Anton Blanchard0837e322011-03-09 14:38:42 +11002052{
Anton Blanchard0837e322011-03-09 14:38:42 +11002053 /*
2054 * Events on POWER7 can roll back if a speculative event doesn't
2055 * eventually complete. Unfortunately in some rare cases they will
2056 * raise a performance monitor exception. We need to catch this to
2057 * ensure we reset the PMC. In all cases the PMC will be 256 or less
2058 * cycles from overflow.
2059 *
2060 * We only do this if the first pass fails to find any overflowing
2061 * PMCs because a user might set a period of less than 256 and we
2062 * don't want to mistakenly reset them.
2063 */
Michael Neulingbc09c212012-11-05 15:53:54 +00002064 if ((0x80000000 - val) <= 256)
2065 return true;
2066
2067 return false;
2068}
2069
2070static bool pmc_overflow(unsigned long val)
2071{
2072 if ((int)val < 0)
Anton Blanchard0837e322011-03-09 14:38:42 +11002073 return true;
2074
2075 return false;
2076}
2077
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078/*
2079 * Performance monitor interrupt stuff
2080 */
2081static void perf_event_interrupt(struct pt_regs *regs)
2082{
Michael Neulingbc09c212012-11-05 15:53:54 +00002083 int i, j;
Christoph Lameter69111ba2014-10-21 15:23:25 -05002084 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085 struct perf_event *event;
Michael Neulingbc09c212012-11-05 15:53:54 +00002086 unsigned long val[8];
2087 int found, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002088 int nmi;
2089
2090 if (cpuhw->n_limited)
Paul Mackerrasa8f90e92009-09-22 09:48:08 +10002091 freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002092 mfspr(SPRN_PMC6));
2093
2094 perf_read_regs(regs);
2095
2096 nmi = perf_intr_is_nmi(regs);
2097 if (nmi)
2098 nmi_enter();
2099 else
2100 irq_enter();
2101
Michael Neulingbc09c212012-11-05 15:53:54 +00002102 /* Read all the PMCs since we'll need them a bunch of times */
2103 for (i = 0; i < ppmu->n_counter; ++i)
2104 val[i] = read_pmc(i + 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105
Michael Neulingbc09c212012-11-05 15:53:54 +00002106 /* Try to find what caused the IRQ */
2107 found = 0;
2108 for (i = 0; i < ppmu->n_counter; ++i) {
2109 if (!pmc_overflow(val[i]))
2110 continue;
2111 if (is_limited_pmc(i + 1))
2112 continue; /* these won't generate IRQs */
2113 /*
2114 * We've found one that's overflowed. For active
2115 * counters we need to log this. For inactive
2116 * counters, we need to reset it anyway
2117 */
2118 found = 1;
2119 active = 0;
2120 for (j = 0; j < cpuhw->n_events; ++j) {
2121 event = cpuhw->event[j];
2122 if (event->hw.idx == (i + 1)) {
2123 active = 1;
2124 record_and_restart(event, val[i], regs);
2125 break;
2126 }
2127 }
2128 if (!active)
2129 /* reset non active counters that have overflowed */
2130 write_pmc(i + 1, 0);
2131 }
2132 if (!found && pvr_version_is(PVR_POWER7)) {
2133 /* check active counters for special buggy p7 overflow */
2134 for (i = 0; i < cpuhw->n_events; ++i) {
2135 event = cpuhw->event[i];
2136 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137 continue;
Michael Neulingbc09c212012-11-05 15:53:54 +00002138 if (pmc_overflow_power7(val[event->hw.idx - 1])) {
2139 /* event has overflowed in a buggy way*/
2140 found = 1;
2141 record_and_restart(event,
2142 val[event->hw.idx - 1],
2143 regs);
2144 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145 }
2146 }
Michael Ellerman6772faa2013-06-05 17:58:20 +00002147 if (!found && !nmi && printk_ratelimit())
Michael Neulingbc09c212012-11-05 15:53:54 +00002148 printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002149
2150 /*
2151 * Reset MMCR0 to its normal value. This will set PMXE and
Ingo Molnar57c0c152009-09-21 12:20:38 +02002152 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002153 * and thus allow interrupts to occur again.
2154 * XXX might want to use MSR.PM to keep the events frozen until
2155 * we get back out of this interrupt.
2156 */
2157 write_mmcr0(cpuhw, cpuhw->mmcr[0]);
2158
2159 if (nmi)
2160 nmi_exit();
2161 else
2162 irq_exit();
2163}
2164
Daniel Axtens7c98bd72016-09-06 15:32:40 +10002165static int power_pmu_prepare_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166{
2167 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
2168
Thomas Gleixner57ecde42016-07-13 17:16:20 +00002169 if (ppmu) {
2170 memset(cpuhw, 0, sizeof(*cpuhw));
2171 cpuhw->mmcr[0] = MMCR0_FC;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002172 }
Thomas Gleixner57ecde42016-07-13 17:16:20 +00002173 return 0;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01002174}
2175
Paul Gortmaker061d19f2013-06-24 15:30:09 -04002176int register_power_pmu(struct power_pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177{
2178 if (ppmu)
2179 return -EBUSY; /* something's already registered */
2180
2181 ppmu = pmu;
2182 pr_info("%s performance monitor hardware support registered\n",
2183 pmu->name);
2184
Sukadev Bhattiprolu1c53a272013-01-22 22:24:54 -08002185 power_pmu.attr_groups = ppmu->attr_groups;
2186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002187#ifdef MSR_HV
2188 /*
2189 * Use FCHV to ignore kernel events if MSR.HV is set.
2190 */
2191 if (mfmsr() & MSR_HV)
2192 freeze_events_kernel = MMCR0_FCHV;
2193#endif /* CONFIG_PPC64 */
2194
Peter Zijlstra2e80a822010-11-17 23:17:36 +01002195 perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
Thomas Gleixner57ecde42016-07-13 17:16:20 +00002196 cpuhp_setup_state(CPUHP_PERF_POWER, "PERF_POWER",
2197 power_pmu_prepare_cpu, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002198 return 0;
2199}