blob: f571f514de2a8d1603cdccb0ed72e4dda5b0b389 [file] [log] [blame]
Ingo Molnar241771e2008-12-03 10:39:53 +01001/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002 * Performance events x86 architecture code
Ingo Molnar241771e2008-12-03 10:39:53 +01003 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2009 Jaswinder Singh Rajput
7 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Markus Metzger30dd5682009-07-21 15:56:48 +02009 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
Stephane Eranian1da53e02010-01-18 10:58:01 +020010 * Copyright (C) 2009 Google, Inc., Stephane Eranian
Ingo Molnar241771e2008-12-03 10:39:53 +010011 *
12 * For licencing details see kernel-base/COPYING
13 */
14
Ingo Molnarcdd6c482009-09-21 12:02:48 +020015#include <linux/perf_event.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010016#include <linux/capability.h>
17#include <linux/notifier.h>
18#include <linux/hardirq.h>
19#include <linux/kprobes.h>
Thomas Gleixner4ac13292008-12-09 21:43:39 +010020#include <linux/module.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010021#include <linux/kdebug.h>
22#include <linux/sched.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020023#include <linux/uaccess.h>
Peter Zijlstra74193ef2009-06-15 13:07:24 +020024#include <linux/highmem.h>
Markus Metzger30dd5682009-07-21 15:56:48 +020025#include <linux/cpu.h>
Peter Zijlstra272d30b2010-01-22 16:32:17 +010026#include <linux/bitops.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010027
Ingo Molnar241771e2008-12-03 10:39:53 +010028#include <asm/apic.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020029#include <asm/stacktrace.h>
Peter Zijlstra4e935e42009-03-30 19:07:16 +020030#include <asm/nmi.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010031
Peter Zijlstra7645a242010-03-08 13:51:31 +010032#if 0
33#undef wrmsrl
34#define wrmsrl(msr, val) \
35do { \
36 trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
37 (unsigned long)(val)); \
38 native_write_msr((msr), (u32)((u64)(val)), \
39 (u32)((u64)(val) >> 32)); \
40} while (0)
41#endif
42
Peter Zijlstraef21f682010-03-03 13:12:23 +010043/*
44 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
45 */
46static unsigned long
47copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
48{
49 unsigned long offset, addr = (unsigned long)from;
50 int type = in_nmi() ? KM_NMI : KM_IRQ0;
51 unsigned long size, len = 0;
52 struct page *page;
53 void *map;
54 int ret;
55
56 do {
57 ret = __get_user_pages_fast(addr, 1, 0, &page);
58 if (!ret)
59 break;
60
61 offset = addr & (PAGE_SIZE - 1);
62 size = min(PAGE_SIZE - offset, n - len);
63
64 map = kmap_atomic(page, type);
65 memcpy(to, map+offset, size);
66 kunmap_atomic(map, type);
67 put_page(page);
68
69 len += size;
70 to += size;
71 addr += size;
72
73 } while (len < n);
74
75 return len;
76}
77
Stephane Eranian1da53e02010-01-18 10:58:01 +020078struct event_constraint {
Peter Zijlstrac91e0f52010-01-22 15:25:59 +010079 union {
80 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Peter Zijlstrab622d642010-02-01 15:36:30 +010081 u64 idxmsk64;
Peter Zijlstrac91e0f52010-01-22 15:25:59 +010082 };
Peter Zijlstrab622d642010-02-01 15:36:30 +010083 u64 code;
84 u64 cmask;
Peter Zijlstra272d30b2010-01-22 16:32:17 +010085 int weight;
Stephane Eranian1da53e02010-01-18 10:58:01 +020086};
87
Stephane Eranian38331f62010-02-08 17:17:01 +020088struct amd_nb {
89 int nb_id; /* NorthBridge id */
90 int refcnt; /* reference count */
91 struct perf_event *owners[X86_PMC_IDX_MAX];
92 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
93};
94
Peter Zijlstracaff2be2010-03-03 12:02:30 +010095#define MAX_LBR_ENTRIES 16
96
Ingo Molnarcdd6c482009-09-21 12:02:48 +020097struct cpu_hw_events {
Peter Zijlstraca037702010-03-02 19:52:12 +010098 /*
99 * Generic x86 PMC bits
100 */
Stephane Eranian1da53e02010-01-18 10:58:01 +0200101 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
Robert Richter43f62012009-04-29 16:55:56 +0200102 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100103 int enabled;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200104
105 int n_events;
106 int n_added;
107 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
Stephane Eranian447a1942010-02-01 14:50:01 +0200108 u64 tags[X86_PMC_IDX_MAX];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200109 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
Peter Zijlstraca037702010-03-02 19:52:12 +0100110
111 /*
112 * Intel DebugStore bits
113 */
114 struct debug_store *ds;
115 u64 pebs_enabled;
116
117 /*
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100118 * Intel LBR bits
119 */
120 int lbr_users;
121 void *lbr_context;
122 struct perf_branch_stack lbr_stack;
123 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
124
125 /*
Peter Zijlstraca037702010-03-02 19:52:12 +0100126 * AMD specific bits
127 */
Stephane Eranian38331f62010-02-08 17:17:01 +0200128 struct amd_nb *amd_nb;
Ingo Molnar241771e2008-12-03 10:39:53 +0100129};
130
Peter Zijlstrafce877e2010-01-29 13:25:12 +0100131#define __EVENT_CONSTRAINT(c, n, m, w) {\
Peter Zijlstrab622d642010-02-01 15:36:30 +0100132 { .idxmsk64 = (n) }, \
Peter Zijlstrac91e0f52010-01-22 15:25:59 +0100133 .code = (c), \
134 .cmask = (m), \
Peter Zijlstrafce877e2010-01-29 13:25:12 +0100135 .weight = (w), \
Peter Zijlstrac91e0f52010-01-22 15:25:59 +0100136}
Stephane Eranianb6900812009-10-06 16:42:09 +0200137
Peter Zijlstrafce877e2010-01-29 13:25:12 +0100138#define EVENT_CONSTRAINT(c, n, m) \
139 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
140
Peter Zijlstraca037702010-03-02 19:52:12 +0100141/*
142 * Constraint on the Event code.
143 */
Peter Zijlstraed8777f2010-01-27 23:07:46 +0100144#define INTEL_EVENT_CONSTRAINT(c, n) \
145 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK)
Peter Zijlstra8433be12010-01-22 15:38:26 +0100146
Peter Zijlstraca037702010-03-02 19:52:12 +0100147/*
148 * Constraint on the Event code + UMask + fixed-mask
149 */
Peter Zijlstraed8777f2010-01-27 23:07:46 +0100150#define FIXED_EVENT_CONSTRAINT(c, n) \
Peter Zijlstrab622d642010-02-01 15:36:30 +0100151 EVENT_CONSTRAINT(c, (1ULL << (32+n)), INTEL_ARCH_FIXED_MASK)
Peter Zijlstra8433be12010-01-22 15:38:26 +0100152
Peter Zijlstraca037702010-03-02 19:52:12 +0100153/*
154 * Constraint on the Event code + UMask
155 */
156#define PEBS_EVENT_CONSTRAINT(c, n) \
157 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
158
Peter Zijlstraed8777f2010-01-27 23:07:46 +0100159#define EVENT_CONSTRAINT_END \
160 EVENT_CONSTRAINT(0, 0, 0)
161
162#define for_each_event_constraint(e, c) \
163 for ((e) = (c); (e)->cmask; (e)++)
Stephane Eranianb6900812009-10-06 16:42:09 +0200164
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100165union perf_capabilities {
166 struct {
167 u64 lbr_format : 6;
168 u64 pebs_trap : 1;
169 u64 pebs_arch_reg : 1;
170 u64 pebs_format : 4;
171 u64 smm_freeze : 1;
172 };
173 u64 capabilities;
174};
175
Ingo Molnar241771e2008-12-03 10:39:53 +0100176/*
Robert Richter5f4ec282009-04-29 12:47:04 +0200177 * struct x86_pmu - generic x86 pmu
Ingo Molnar241771e2008-12-03 10:39:53 +0100178 */
Robert Richter5f4ec282009-04-29 12:47:04 +0200179struct x86_pmu {
Peter Zijlstraca037702010-03-02 19:52:12 +0100180 /*
181 * Generic x86 PMC bits
182 */
Robert Richterfaa28ae2009-04-29 12:47:13 +0200183 const char *name;
184 int version;
Yong Wanga3288102009-06-03 13:12:55 +0800185 int (*handle_irq)(struct pt_regs *);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200186 void (*disable_all)(void);
187 void (*enable_all)(void);
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100188 void (*enable)(struct perf_event *);
189 void (*disable)(struct perf_event *);
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300190 int (*hw_config)(struct perf_event_attr *attr, struct hw_perf_event *hwc);
191 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +0530192 unsigned eventsel;
193 unsigned perfctr;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100194 u64 (*event_map)(int);
195 u64 (*raw_event)(u64);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +0530196 int max_events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200197 int num_events;
198 int num_events_fixed;
199 int event_bits;
200 u64 event_mask;
Ingo Molnar04da8a42009-08-11 10:40:08 +0200201 int apic;
Robert Richterc619b8f2009-04-29 12:47:23 +0200202 u64 max_period;
Peter Zijlstra63b14642010-01-22 16:32:17 +0100203 struct event_constraint *
204 (*get_event_constraints)(struct cpu_hw_events *cpuc,
205 struct perf_event *event);
206
Peter Zijlstrac91e0f52010-01-22 15:25:59 +0100207 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
208 struct perf_event *event);
Peter Zijlstra63b14642010-01-22 16:32:17 +0100209 struct event_constraint *event_constraints;
Peter Zijlstra3c447802010-03-04 21:49:01 +0100210 void (*quirks)(void);
Peter Zijlstra3f6da392010-03-05 13:01:18 +0100211
212 void (*cpu_prepare)(int cpu);
213 void (*cpu_starting)(int cpu);
214 void (*cpu_dying)(int cpu);
215 void (*cpu_dead)(int cpu);
Peter Zijlstraca037702010-03-02 19:52:12 +0100216
217 /*
218 * Intel Arch Perfmon v2+
219 */
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100220 u64 intel_ctrl;
221 union perf_capabilities intel_cap;
Peter Zijlstraca037702010-03-02 19:52:12 +0100222
223 /*
224 * Intel DebugStore bits
225 */
226 int bts, pebs;
227 int pebs_record_size;
228 void (*drain_pebs)(struct pt_regs *regs);
229 struct event_constraint *pebs_constraints;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100230
231 /*
232 * Intel LBR
233 */
234 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
235 int lbr_nr; /* hardware stack size */
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530236};
237
Robert Richter4a06bd82009-04-29 12:47:11 +0200238static struct x86_pmu x86_pmu __read_mostly;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530239
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200240static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100241 .enabled = 1,
242};
Ingo Molnar241771e2008-12-03 10:39:53 +0100243
Peter Zijlstra07088ed2010-03-02 20:16:01 +0100244static int x86_perf_event_set_period(struct perf_event *event);
Stephane Eranianb6900812009-10-06 16:42:09 +0200245
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530246/*
Ingo Molnardfc65092009-09-21 11:31:35 +0200247 * Generalized hw caching related hw_event table, filled
Ingo Molnar8326f442009-06-05 20:22:46 +0200248 * in on a per model basis. A value of 0 means
Ingo Molnardfc65092009-09-21 11:31:35 +0200249 * 'not supported', -1 means 'hw_event makes no sense on
250 * this CPU', any other value means the raw hw_event
Ingo Molnar8326f442009-06-05 20:22:46 +0200251 * ID.
252 */
253
254#define C(x) PERF_COUNT_HW_CACHE_##x
255
256static u64 __read_mostly hw_cache_event_ids
257 [PERF_COUNT_HW_CACHE_MAX]
258 [PERF_COUNT_HW_CACHE_OP_MAX]
259 [PERF_COUNT_HW_CACHE_RESULT_MAX];
260
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530261/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200262 * Propagate event elapsed time into the generic event.
263 * Can only be executed on the CPU where the event is active.
Ingo Molnaree060942008-12-13 09:00:03 +0100264 * Returns the delta events processed.
265 */
Robert Richter4b7bfd02009-04-29 12:47:22 +0200266static u64
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +0100267x86_perf_event_update(struct perf_event *event)
Ingo Molnaree060942008-12-13 09:00:03 +0100268{
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +0100269 struct hw_perf_event *hwc = &event->hw;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200270 int shift = 64 - x86_pmu.event_bits;
Peter Zijlstraec3232b2009-05-13 09:45:19 +0200271 u64 prev_raw_count, new_raw_count;
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +0100272 int idx = hwc->idx;
Peter Zijlstraec3232b2009-05-13 09:45:19 +0200273 s64 delta;
Ingo Molnaree060942008-12-13 09:00:03 +0100274
Markus Metzger30dd5682009-07-21 15:56:48 +0200275 if (idx == X86_PMC_IDX_FIXED_BTS)
276 return 0;
277
Ingo Molnaree060942008-12-13 09:00:03 +0100278 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279 * Careful: an NMI might modify the previous event value.
Ingo Molnaree060942008-12-13 09:00:03 +0100280 *
281 * Our tactic to handle this is to first atomically read and
282 * exchange a new raw count - then add that new-prev delta
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200283 * count to the generic event atomically:
Ingo Molnaree060942008-12-13 09:00:03 +0100284 */
285again:
286 prev_raw_count = atomic64_read(&hwc->prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200287 rdmsrl(hwc->event_base + idx, new_raw_count);
Ingo Molnaree060942008-12-13 09:00:03 +0100288
289 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
290 new_raw_count) != prev_raw_count)
291 goto again;
292
293 /*
294 * Now we have the new raw value and have updated the prev
295 * timestamp already. We can now calculate the elapsed delta
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200296 * (event-)time and add that to the generic event.
Ingo Molnaree060942008-12-13 09:00:03 +0100297 *
298 * Careful, not all hw sign-extends above the physical width
Peter Zijlstraec3232b2009-05-13 09:45:19 +0200299 * of the count.
Ingo Molnaree060942008-12-13 09:00:03 +0100300 */
Peter Zijlstraec3232b2009-05-13 09:45:19 +0200301 delta = (new_raw_count << shift) - (prev_raw_count << shift);
302 delta >>= shift;
Ingo Molnaree060942008-12-13 09:00:03 +0100303
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200304 atomic64_add(delta, &event->count);
Ingo Molnaree060942008-12-13 09:00:03 +0100305 atomic64_sub(delta, &hwc->period_left);
Robert Richter4b7bfd02009-04-29 12:47:22 +0200306
307 return new_raw_count;
Ingo Molnaree060942008-12-13 09:00:03 +0100308}
309
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200310static atomic_t active_events;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200311static DEFINE_MUTEX(pmc_reserve_mutex);
312
Robert Richterb27ea292010-03-17 12:49:10 +0100313#ifdef CONFIG_X86_LOCAL_APIC
314
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200315static bool reserve_pmc_hardware(void)
316{
317 int i;
318
319 if (nmi_watchdog == NMI_LOCAL_APIC)
320 disable_lapic_nmi_watchdog();
321
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200322 for (i = 0; i < x86_pmu.num_events; i++) {
Robert Richter4a06bd82009-04-29 12:47:11 +0200323 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200324 goto perfctr_fail;
325 }
326
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200327 for (i = 0; i < x86_pmu.num_events; i++) {
Robert Richter4a06bd82009-04-29 12:47:11 +0200328 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200329 goto eventsel_fail;
330 }
331
332 return true;
333
334eventsel_fail:
335 for (i--; i >= 0; i--)
Robert Richter4a06bd82009-04-29 12:47:11 +0200336 release_evntsel_nmi(x86_pmu.eventsel + i);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200337
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338 i = x86_pmu.num_events;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200339
340perfctr_fail:
341 for (i--; i >= 0; i--)
Robert Richter4a06bd82009-04-29 12:47:11 +0200342 release_perfctr_nmi(x86_pmu.perfctr + i);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200343
344 if (nmi_watchdog == NMI_LOCAL_APIC)
345 enable_lapic_nmi_watchdog();
346
347 return false;
348}
349
350static void release_pmc_hardware(void)
351{
352 int i;
353
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200354 for (i = 0; i < x86_pmu.num_events; i++) {
Robert Richter4a06bd82009-04-29 12:47:11 +0200355 release_perfctr_nmi(x86_pmu.perfctr + i);
356 release_evntsel_nmi(x86_pmu.eventsel + i);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200357 }
358
359 if (nmi_watchdog == NMI_LOCAL_APIC)
360 enable_lapic_nmi_watchdog();
361}
362
Robert Richterb27ea292010-03-17 12:49:10 +0100363#else
364
365static bool reserve_pmc_hardware(void) { return true; }
366static void release_pmc_hardware(void) {}
367
368#endif
369
Peter Zijlstraca037702010-03-02 19:52:12 +0100370static int reserve_ds_buffers(void);
371static void release_ds_buffers(void);
Markus Metzger30dd5682009-07-21 15:56:48 +0200372
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200373static void hw_perf_event_destroy(struct perf_event *event)
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200374{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200375 if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200376 release_pmc_hardware();
Peter Zijlstraca037702010-03-02 19:52:12 +0100377 release_ds_buffers();
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200378 mutex_unlock(&pmc_reserve_mutex);
379 }
380}
381
Robert Richter85cf9db2009-04-29 12:47:20 +0200382static inline int x86_pmu_initialized(void)
383{
384 return x86_pmu.handle_irq != NULL;
385}
386
Ingo Molnar8326f442009-06-05 20:22:46 +0200387static inline int
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200388set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200389{
390 unsigned int cache_type, cache_op, cache_result;
391 u64 config, val;
392
393 config = attr->config;
394
395 cache_type = (config >> 0) & 0xff;
396 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
397 return -EINVAL;
398
399 cache_op = (config >> 8) & 0xff;
400 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
401 return -EINVAL;
402
403 cache_result = (config >> 16) & 0xff;
404 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
405 return -EINVAL;
406
407 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
408
409 if (val == 0)
410 return -ENOENT;
411
412 if (val == -1)
413 return -EINVAL;
414
415 hwc->config |= val;
416
417 return 0;
418}
419
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300420static int x86_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
421{
422 /*
423 * Generate PMC IRQs:
424 * (keep 'enabled' bit clear for now)
425 */
426 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
427
428 /*
429 * Count user and OS events unless requested not to
430 */
431 if (!attr->exclude_user)
432 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
433 if (!attr->exclude_kernel)
434 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
435
436 return 0;
437}
438
Ingo Molnaree060942008-12-13 09:00:03 +0100439/*
Peter Zijlstra0d486962009-06-02 19:22:16 +0200440 * Setup the hardware configuration for a given attr_type
Ingo Molnar241771e2008-12-03 10:39:53 +0100441 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200442static int __hw_perf_event_init(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +0100443{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200444 struct perf_event_attr *attr = &event->attr;
445 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstra9c74fb52009-07-08 10:21:41 +0200446 u64 config;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200447 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100448
Robert Richter85cf9db2009-04-29 12:47:20 +0200449 if (!x86_pmu_initialized())
450 return -ENODEV;
Ingo Molnar241771e2008-12-03 10:39:53 +0100451
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200452 err = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200453 if (!atomic_inc_not_zero(&active_events)) {
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200454 mutex_lock(&pmc_reserve_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200455 if (atomic_read(&active_events) == 0) {
Markus Metzger30dd5682009-07-21 15:56:48 +0200456 if (!reserve_pmc_hardware())
457 err = -EBUSY;
Stephane Eranian4b24a882010-03-17 23:21:01 +0200458 else {
Peter Zijlstraca037702010-03-02 19:52:12 +0100459 err = reserve_ds_buffers();
Stephane Eranian4b24a882010-03-17 23:21:01 +0200460 if (err)
461 release_pmc_hardware();
462 }
Markus Metzger30dd5682009-07-21 15:56:48 +0200463 }
464 if (!err)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200465 atomic_inc(&active_events);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200466 mutex_unlock(&pmc_reserve_mutex);
467 }
468 if (err)
469 return err;
470
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200471 event->destroy = hw_perf_event_destroy;
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +0200472
Stephane Eranianb6900812009-10-06 16:42:09 +0200473 hwc->idx = -1;
Stephane Eranian447a1942010-02-01 14:50:01 +0200474 hwc->last_cpu = -1;
475 hwc->last_tag = ~0ULL;
Stephane Eranianb6900812009-10-06 16:42:09 +0200476
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300477 /* Processor specifics */
Robert Richter984763c2010-03-16 17:07:33 +0100478 err = x86_pmu.hw_config(attr, hwc);
479 if (err)
480 return err;
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100481
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +0200482 if (!hwc->sample_period) {
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200483 hwc->sample_period = x86_pmu.max_period;
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200484 hwc->last_period = hwc->sample_period;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +0200485 atomic64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnar04da8a42009-08-11 10:40:08 +0200486 } else {
487 /*
488 * If we have a PMU initialized but no APIC
489 * interrupts, we cannot sample hardware
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200490 * events (user-space has to fall back and
491 * sample via a hrtimer based software event):
Ingo Molnar04da8a42009-08-11 10:40:08 +0200492 */
493 if (!x86_pmu.apic)
494 return -EOPNOTSUPP;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +0200495 }
Ingo Molnard2517a42009-05-17 10:04:45 +0200496
Ingo Molnar241771e2008-12-03 10:39:53 +0100497 /*
Ingo Molnardfc65092009-09-21 11:31:35 +0200498 * Raw hw_event type provide the config in the hw_event structure
Ingo Molnar241771e2008-12-03 10:39:53 +0100499 */
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200500 if (attr->type == PERF_TYPE_RAW) {
501 hwc->config |= x86_pmu.raw_event(attr->config);
Peter Zijlstra320ebf02010-03-02 12:35:37 +0100502 if ((hwc->config & ARCH_PERFMON_EVENTSEL_ANY) &&
503 perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
504 return -EACCES;
Ingo Molnar8326f442009-06-05 20:22:46 +0200505 return 0;
Ingo Molnar241771e2008-12-03 10:39:53 +0100506 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100507
Ingo Molnar8326f442009-06-05 20:22:46 +0200508 if (attr->type == PERF_TYPE_HW_CACHE)
509 return set_ext_hw_attr(hwc, attr);
510
511 if (attr->config >= x86_pmu.max_events)
512 return -EINVAL;
Peter Zijlstra9c74fb52009-07-08 10:21:41 +0200513
Ingo Molnar8326f442009-06-05 20:22:46 +0200514 /*
515 * The generic map:
516 */
Peter Zijlstra9c74fb52009-07-08 10:21:41 +0200517 config = x86_pmu.event_map(attr->config);
518
519 if (config == 0)
520 return -ENOENT;
521
522 if (config == -1LL)
523 return -EINVAL;
524
markus.t.metzger@intel.com747b50a2009-09-02 16:04:46 +0200525 /*
526 * Branch tracing:
527 */
528 if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
markus.t.metzger@intel.com16531922009-09-02 16:04:48 +0200529 (hwc->sample_period == 1)) {
530 /* BTS is not supported by this architecture. */
Peter Zijlstraca037702010-03-02 19:52:12 +0100531 if (!x86_pmu.bts)
markus.t.metzger@intel.com16531922009-09-02 16:04:48 +0200532 return -EOPNOTSUPP;
533
534 /* BTS is currently only allowed for user-mode. */
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300535 if (!attr->exclude_kernel)
markus.t.metzger@intel.com16531922009-09-02 16:04:48 +0200536 return -EOPNOTSUPP;
537 }
markus.t.metzger@intel.com747b50a2009-09-02 16:04:46 +0200538
Peter Zijlstra9c74fb52009-07-08 10:21:41 +0200539 hwc->config |= config;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200540
Ingo Molnar241771e2008-12-03 10:39:53 +0100541 return 0;
542}
543
Peter Zijlstra8c48e442010-01-29 13:25:31 +0100544static void x86_pmu_disable_all(void)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530545{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200546 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200547 int idx;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100548
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200549 for (idx = 0; idx < x86_pmu.num_events; idx++) {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100550 u64 val;
551
Robert Richter43f62012009-04-29 16:55:56 +0200552 if (!test_bit(idx, cpuc->active_mask))
Robert Richter4295ee62009-04-29 12:47:01 +0200553 continue;
Peter Zijlstra8c48e442010-01-29 13:25:31 +0100554 rdmsrl(x86_pmu.eventsel + idx, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100555 if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
Robert Richter4295ee62009-04-29 12:47:01 +0200556 continue;
Robert Richterbb1165d2010-03-01 14:21:23 +0100557 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Peter Zijlstra8c48e442010-01-29 13:25:31 +0100558 wrmsrl(x86_pmu.eventsel + idx, val);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530559 }
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530560}
561
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200562void hw_perf_disable(void)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530563{
Stephane Eranian1da53e02010-01-18 10:58:01 +0200564 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
565
Robert Richter85cf9db2009-04-29 12:47:20 +0200566 if (!x86_pmu_initialized())
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200567 return;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200568
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100569 if (!cpuc->enabled)
570 return;
571
572 cpuc->n_added = 0;
573 cpuc->enabled = 0;
574 barrier();
Stephane Eranian1da53e02010-01-18 10:58:01 +0200575
576 x86_pmu.disable_all();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530577}
Ingo Molnar241771e2008-12-03 10:39:53 +0100578
Peter Zijlstra8c48e442010-01-29 13:25:31 +0100579static void x86_pmu_enable_all(void)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530580{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200581 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530582 int idx;
583
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200584 for (idx = 0; idx < x86_pmu.num_events; idx++) {
585 struct perf_event *event = cpuc->events[idx];
Robert Richter4295ee62009-04-29 12:47:01 +0200586 u64 val;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100587
Robert Richter43f62012009-04-29 16:55:56 +0200588 if (!test_bit(idx, cpuc->active_mask))
Robert Richter4295ee62009-04-29 12:47:01 +0200589 continue;
Peter Zijlstra984b8382009-07-10 09:59:56 +0200590
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200591 val = event->hw.config;
Robert Richterbb1165d2010-03-01 14:21:23 +0100592 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
Peter Zijlstra8c48e442010-01-29 13:25:31 +0100593 wrmsrl(x86_pmu.eventsel + idx, val);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530594 }
595}
596
Stephane Eranian1da53e02010-01-18 10:58:01 +0200597static const struct pmu pmu;
598
599static inline int is_x86_event(struct perf_event *event)
600{
601 return event->pmu == &pmu;
602}
603
604static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
605{
Peter Zijlstra63b14642010-01-22 16:32:17 +0100606 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200607 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Peter Zijlstrac933c1a2010-01-22 16:40:12 +0100608 int i, j, w, wmax, num = 0;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200609 struct hw_perf_event *hwc;
610
611 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
612
613 for (i = 0; i < n; i++) {
Peter Zijlstrab622d642010-02-01 15:36:30 +0100614 c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
615 constraints[i] = c;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200616 }
617
618 /*
Stephane Eranian81130702010-01-21 17:39:01 +0200619 * fastpath, try to reuse previous register
620 */
Peter Zijlstrac933c1a2010-01-22 16:40:12 +0100621 for (i = 0; i < n; i++) {
Stephane Eranian81130702010-01-21 17:39:01 +0200622 hwc = &cpuc->event_list[i]->hw;
Peter Zijlstra81269a02010-01-22 14:55:22 +0100623 c = constraints[i];
Stephane Eranian81130702010-01-21 17:39:01 +0200624
625 /* never assigned */
626 if (hwc->idx == -1)
627 break;
628
629 /* constraint still honored */
Peter Zijlstra63b14642010-01-22 16:32:17 +0100630 if (!test_bit(hwc->idx, c->idxmsk))
Stephane Eranian81130702010-01-21 17:39:01 +0200631 break;
632
633 /* not already used */
634 if (test_bit(hwc->idx, used_mask))
635 break;
636
Peter Zijlstra34538ee2010-03-02 21:16:55 +0100637 __set_bit(hwc->idx, used_mask);
Stephane Eranian81130702010-01-21 17:39:01 +0200638 if (assign)
639 assign[i] = hwc->idx;
640 }
Peter Zijlstrac933c1a2010-01-22 16:40:12 +0100641 if (i == n)
Stephane Eranian81130702010-01-21 17:39:01 +0200642 goto done;
643
644 /*
645 * begin slow path
646 */
647
648 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
649
650 /*
Stephane Eranian1da53e02010-01-18 10:58:01 +0200651 * weight = number of possible counters
652 *
653 * 1 = most constrained, only works on one counter
654 * wmax = least constrained, works on any counter
655 *
656 * assign events to counters starting with most
657 * constrained events.
658 */
659 wmax = x86_pmu.num_events;
660
661 /*
662 * when fixed event counters are present,
663 * wmax is incremented by 1 to account
664 * for one more choice
665 */
666 if (x86_pmu.num_events_fixed)
667 wmax++;
668
Stephane Eranian81130702010-01-21 17:39:01 +0200669 for (w = 1, num = n; num && w <= wmax; w++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200670 /* for each event */
Stephane Eranian81130702010-01-21 17:39:01 +0200671 for (i = 0; num && i < n; i++) {
Peter Zijlstra81269a02010-01-22 14:55:22 +0100672 c = constraints[i];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200673 hwc = &cpuc->event_list[i]->hw;
674
Peter Zijlstra272d30b2010-01-22 16:32:17 +0100675 if (c->weight != w)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200676 continue;
677
Akinobu Mita984b3f52010-03-05 13:41:37 -0800678 for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200679 if (!test_bit(j, used_mask))
680 break;
681 }
682
683 if (j == X86_PMC_IDX_MAX)
684 break;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200685
Peter Zijlstra34538ee2010-03-02 21:16:55 +0100686 __set_bit(j, used_mask);
Stephane Eranian81130702010-01-21 17:39:01 +0200687
Stephane Eranian1da53e02010-01-18 10:58:01 +0200688 if (assign)
689 assign[i] = j;
690 num--;
691 }
692 }
Stephane Eranian81130702010-01-21 17:39:01 +0200693done:
Stephane Eranian1da53e02010-01-18 10:58:01 +0200694 /*
695 * scheduling failed or is just a simulation,
696 * free resources if necessary
697 */
698 if (!assign || num) {
699 for (i = 0; i < n; i++) {
700 if (x86_pmu.put_event_constraints)
701 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
702 }
703 }
704 return num ? -ENOSPC : 0;
705}
706
707/*
708 * dogrp: true if must collect siblings events (group)
709 * returns total number of events and error code
710 */
711static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
712{
713 struct perf_event *event;
714 int n, max_count;
715
716 max_count = x86_pmu.num_events + x86_pmu.num_events_fixed;
717
718 /* current number of events already accepted */
719 n = cpuc->n_events;
720
721 if (is_x86_event(leader)) {
722 if (n >= max_count)
723 return -ENOSPC;
724 cpuc->event_list[n] = leader;
725 n++;
726 }
727 if (!dogrp)
728 return n;
729
730 list_for_each_entry(event, &leader->sibling_list, group_entry) {
731 if (!is_x86_event(event) ||
Stephane Eranian81130702010-01-21 17:39:01 +0200732 event->state <= PERF_EVENT_STATE_OFF)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200733 continue;
734
735 if (n >= max_count)
736 return -ENOSPC;
737
738 cpuc->event_list[n] = event;
739 n++;
740 }
741 return n;
742}
743
Stephane Eranian1da53e02010-01-18 10:58:01 +0200744static inline void x86_assign_hw_event(struct perf_event *event,
Stephane Eranian447a1942010-02-01 14:50:01 +0200745 struct cpu_hw_events *cpuc, int i)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200746{
Stephane Eranian447a1942010-02-01 14:50:01 +0200747 struct hw_perf_event *hwc = &event->hw;
748
749 hwc->idx = cpuc->assign[i];
750 hwc->last_cpu = smp_processor_id();
751 hwc->last_tag = ++cpuc->tags[i];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200752
753 if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
754 hwc->config_base = 0;
755 hwc->event_base = 0;
756 } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
757 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
758 /*
759 * We set it so that event_base + idx in wrmsr/rdmsr maps to
760 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
761 */
762 hwc->event_base =
763 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
764 } else {
765 hwc->config_base = x86_pmu.eventsel;
766 hwc->event_base = x86_pmu.perfctr;
767 }
768}
769
Stephane Eranian447a1942010-02-01 14:50:01 +0200770static inline int match_prev_assignment(struct hw_perf_event *hwc,
771 struct cpu_hw_events *cpuc,
772 int i)
773{
774 return hwc->idx == cpuc->assign[i] &&
775 hwc->last_cpu == smp_processor_id() &&
776 hwc->last_tag == cpuc->tags[i];
777}
778
Peter Zijlstrac08053e2010-03-06 13:19:24 +0100779static int x86_pmu_start(struct perf_event *event);
Stephane Eraniand76a0812010-02-08 17:06:01 +0200780static void x86_pmu_stop(struct perf_event *event);
Peter Zijlstra2e841872010-01-25 15:58:43 +0100781
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200782void hw_perf_enable(void)
Ingo Molnaree060942008-12-13 09:00:03 +0100783{
Stephane Eranian1da53e02010-01-18 10:58:01 +0200784 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
785 struct perf_event *event;
786 struct hw_perf_event *hwc;
787 int i;
788
Robert Richter85cf9db2009-04-29 12:47:20 +0200789 if (!x86_pmu_initialized())
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +0100790 return;
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100791
792 if (cpuc->enabled)
793 return;
794
Stephane Eranian1da53e02010-01-18 10:58:01 +0200795 if (cpuc->n_added) {
Peter Zijlstra19925ce2010-03-06 13:20:40 +0100796 int n_running = cpuc->n_events - cpuc->n_added;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200797 /*
798 * apply assignment obtained either from
799 * hw_perf_group_sched_in() or x86_pmu_enable()
800 *
801 * step1: save events moving to new counters
802 * step2: reprogram moved events into new counters
803 */
Peter Zijlstra19925ce2010-03-06 13:20:40 +0100804 for (i = 0; i < n_running; i++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200805 event = cpuc->event_list[i];
806 hwc = &event->hw;
807
Stephane Eranian447a1942010-02-01 14:50:01 +0200808 /*
809 * we can avoid reprogramming counter if:
810 * - assigned same counter as last time
811 * - running on same CPU as last time
812 * - no other event has used the counter since
813 */
814 if (hwc->idx == -1 ||
815 match_prev_assignment(hwc, cpuc, i))
Stephane Eranian1da53e02010-01-18 10:58:01 +0200816 continue;
817
Stephane Eraniand76a0812010-02-08 17:06:01 +0200818 x86_pmu_stop(event);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200819 }
820
821 for (i = 0; i < cpuc->n_events; i++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200822 event = cpuc->event_list[i];
823 hwc = &event->hw;
824
Peter Zijlstra45e16a62010-03-11 13:40:30 +0100825 if (!match_prev_assignment(hwc, cpuc, i))
Stephane Eranian447a1942010-02-01 14:50:01 +0200826 x86_assign_hw_event(event, cpuc, i);
Peter Zijlstra45e16a62010-03-11 13:40:30 +0100827 else if (i < n_running)
828 continue;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200829
Peter Zijlstrac08053e2010-03-06 13:19:24 +0100830 x86_pmu_start(event);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200831 }
832 cpuc->n_added = 0;
833 perf_events_lapic_init();
834 }
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100835
836 cpuc->enabled = 1;
837 barrier();
838
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200839 x86_pmu.enable_all();
Ingo Molnaree060942008-12-13 09:00:03 +0100840}
Ingo Molnaree060942008-12-13 09:00:03 +0100841
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100842static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100843{
Peter Zijlstra7645a242010-03-08 13:51:31 +0100844 wrmsrl(hwc->config_base + hwc->idx,
Robert Richterbb1165d2010-03-01 14:21:23 +0100845 hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100846}
847
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100848static inline void x86_pmu_disable_event(struct perf_event *event)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100849{
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100850 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstra7645a242010-03-08 13:51:31 +0100851
852 wrmsrl(hwc->config_base + hwc->idx, hwc->config);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100853}
854
Tejun Heo245b2e72009-06-24 15:13:48 +0900855static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +0100856
Ingo Molnaree060942008-12-13 09:00:03 +0100857/*
858 * Set the next IRQ period, based on the hwc->period_left value.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200859 * To be called with the event disabled in hw:
Ingo Molnaree060942008-12-13 09:00:03 +0100860 */
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200861static int
Peter Zijlstra07088ed2010-03-02 20:16:01 +0100862x86_perf_event_set_period(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +0100863{
Peter Zijlstra07088ed2010-03-02 20:16:01 +0100864 struct hw_perf_event *hwc = &event->hw;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100865 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200866 s64 period = hwc->sample_period;
Peter Zijlstra7645a242010-03-08 13:51:31 +0100867 int ret = 0, idx = hwc->idx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100868
Markus Metzger30dd5682009-07-21 15:56:48 +0200869 if (idx == X86_PMC_IDX_FIXED_BTS)
870 return 0;
871
Ingo Molnaree060942008-12-13 09:00:03 +0100872 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200873 * If we are way outside a reasonable range then just skip forward:
Ingo Molnaree060942008-12-13 09:00:03 +0100874 */
875 if (unlikely(left <= -period)) {
876 left = period;
877 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200878 hwc->last_period = period;
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200879 ret = 1;
Ingo Molnaree060942008-12-13 09:00:03 +0100880 }
881
882 if (unlikely(left <= 0)) {
883 left += period;
884 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200885 hwc->last_period = period;
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200886 ret = 1;
Ingo Molnaree060942008-12-13 09:00:03 +0100887 }
Ingo Molnar1c80f4b2009-05-15 08:25:22 +0200888 /*
Ingo Molnardfc65092009-09-21 11:31:35 +0200889 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
Ingo Molnar1c80f4b2009-05-15 08:25:22 +0200890 */
891 if (unlikely(left < 2))
892 left = 2;
Ingo Molnaree060942008-12-13 09:00:03 +0100893
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200894 if (left > x86_pmu.max_period)
895 left = x86_pmu.max_period;
896
Tejun Heo245b2e72009-06-24 15:13:48 +0900897 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
Ingo Molnaree060942008-12-13 09:00:03 +0100898
899 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200900 * The hw event starts counting from this event offset,
Ingo Molnaree060942008-12-13 09:00:03 +0100901 * mark it to be able to extra future deltas:
902 */
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100903 atomic64_set(&hwc->prev_count, (u64)-left);
Ingo Molnaree060942008-12-13 09:00:03 +0100904
Peter Zijlstra7645a242010-03-08 13:51:31 +0100905 wrmsrl(hwc->event_base + idx,
906 (u64)(-left) & x86_pmu.event_mask);
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200907
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908 perf_event_update_userpage(event);
Peter Zijlstra194002b2009-06-22 16:35:24 +0200909
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200910 return ret;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100911}
912
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100913static void x86_pmu_enable_event(struct perf_event *event)
Robert Richter7c90cc42009-04-29 12:47:18 +0200914{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200915 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Robert Richter7c90cc42009-04-29 12:47:18 +0200916 if (cpuc->enabled)
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100917 __x86_pmu_enable_event(&event->hw);
Ingo Molnar241771e2008-12-03 10:39:53 +0100918}
919
Ingo Molnaree060942008-12-13 09:00:03 +0100920/*
Stephane Eranian1da53e02010-01-18 10:58:01 +0200921 * activate a single event
922 *
923 * The event is added to the group of enabled events
924 * but only if it can be scehduled with existing events.
925 *
926 * Called with PMU disabled. If successful and return value 1,
927 * then guaranteed to call perf_enable() and hw_perf_enable()
Peter Zijlstrafe9081c2009-10-08 11:56:07 +0200928 */
929static int x86_pmu_enable(struct perf_event *event)
930{
931 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200932 struct hw_perf_event *hwc;
933 int assign[X86_PMC_IDX_MAX];
934 int n, n0, ret;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +0200935
Stephane Eranian1da53e02010-01-18 10:58:01 +0200936 hwc = &event->hw;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +0200937
Stephane Eranian1da53e02010-01-18 10:58:01 +0200938 n0 = cpuc->n_events;
939 n = collect_events(cpuc, event, false);
940 if (n < 0)
941 return n;
Ingo Molnar53b441a2009-05-25 21:41:28 +0200942
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300943 ret = x86_pmu.schedule_events(cpuc, n, assign);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200944 if (ret)
945 return ret;
946 /*
947 * copy new assignment, now we know it is possible
948 * will be used by hw_perf_enable()
949 */
950 memcpy(cpuc->assign, assign, n*sizeof(int));
Ingo Molnar241771e2008-12-03 10:39:53 +0100951
Stephane Eranian1da53e02010-01-18 10:58:01 +0200952 cpuc->n_events = n;
Peter Zijlstra356e1f22010-03-06 13:49:56 +0100953 cpuc->n_added += n - n0;
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100954
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100955 return 0;
Ingo Molnar241771e2008-12-03 10:39:53 +0100956}
957
Stephane Eraniand76a0812010-02-08 17:06:01 +0200958static int x86_pmu_start(struct perf_event *event)
959{
Peter Zijlstrac08053e2010-03-06 13:19:24 +0100960 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
961 int idx = event->hw.idx;
962
963 if (idx == -1)
Stephane Eraniand76a0812010-02-08 17:06:01 +0200964 return -EAGAIN;
965
Peter Zijlstra07088ed2010-03-02 20:16:01 +0100966 x86_perf_event_set_period(event);
Peter Zijlstrac08053e2010-03-06 13:19:24 +0100967 cpuc->events[idx] = event;
968 __set_bit(idx, cpuc->active_mask);
Peter Zijlstraaff3d912010-03-02 20:32:08 +0100969 x86_pmu.enable(event);
Peter Zijlstrac08053e2010-03-06 13:19:24 +0100970 perf_event_update_userpage(event);
Stephane Eraniand76a0812010-02-08 17:06:01 +0200971
972 return 0;
973}
974
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200975static void x86_pmu_unthrottle(struct perf_event *event)
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200976{
Peter Zijlstra71e2d282010-03-08 17:51:33 +0100977 int ret = x86_pmu_start(event);
978 WARN_ON_ONCE(ret);
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200979}
980
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981void perf_event_print_debug(void)
Ingo Molnar241771e2008-12-03 10:39:53 +0100982{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100983 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
Peter Zijlstraca037702010-03-02 19:52:12 +0100984 u64 pebs;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200985 struct cpu_hw_events *cpuc;
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +0200986 unsigned long flags;
Ingo Molnar1e125672008-12-09 12:18:18 +0100987 int cpu, idx;
988
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200989 if (!x86_pmu.num_events)
Ingo Molnar1e125672008-12-09 12:18:18 +0100990 return;
Ingo Molnar241771e2008-12-03 10:39:53 +0100991
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +0200992 local_irq_save(flags);
Ingo Molnar241771e2008-12-03 10:39:53 +0100993
994 cpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200995 cpuc = &per_cpu(cpu_hw_events, cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +0100996
Robert Richterfaa28ae2009-04-29 12:47:13 +0200997 if (x86_pmu.version >= 2) {
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530998 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
999 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1000 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1001 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
Peter Zijlstraca037702010-03-02 19:52:12 +01001002 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
Ingo Molnar241771e2008-12-03 10:39:53 +01001003
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301004 pr_info("\n");
1005 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1006 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1007 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1008 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
Peter Zijlstraca037702010-03-02 19:52:12 +01001009 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301010 }
Peter Zijlstra7645a242010-03-08 13:51:31 +01001011 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
Ingo Molnar241771e2008-12-03 10:39:53 +01001012
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013 for (idx = 0; idx < x86_pmu.num_events; idx++) {
Robert Richter4a06bd82009-04-29 12:47:11 +02001014 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
1015 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
Ingo Molnar241771e2008-12-03 10:39:53 +01001016
Tejun Heo245b2e72009-06-24 15:13:48 +09001017 prev_left = per_cpu(pmc_prev_left[idx], cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +01001018
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301019 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +01001020 cpu, idx, pmc_ctrl);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301021 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +01001022 cpu, idx, pmc_count);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301023 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
Ingo Molnaree060942008-12-13 09:00:03 +01001024 cpu, idx, prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +01001025 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026 for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001027 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1028
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301029 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001030 cpu, idx, pmc_count);
1031 }
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001032 local_irq_restore(flags);
Ingo Molnar241771e2008-12-03 10:39:53 +01001033}
1034
Stephane Eraniand76a0812010-02-08 17:06:01 +02001035static void x86_pmu_stop(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +01001036{
Stephane Eraniand76a0812010-02-08 17:06:01 +02001037 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstra2e841872010-01-25 15:58:43 +01001039 int idx = hwc->idx;
Ingo Molnar241771e2008-12-03 10:39:53 +01001040
Peter Zijlstra71e2d282010-03-08 17:51:33 +01001041 if (!__test_and_clear_bit(idx, cpuc->active_mask))
1042 return;
1043
Peter Zijlstraaff3d912010-03-02 20:32:08 +01001044 x86_pmu.disable(event);
Ingo Molnar241771e2008-12-03 10:39:53 +01001045
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001046 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001047 * Drain the remaining delta count out of a event
Ingo Molnaree060942008-12-13 09:00:03 +01001048 * that we are disabling:
1049 */
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001050 x86_perf_event_update(event);
Markus Metzger30dd5682009-07-21 15:56:48 +02001051
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001052 cpuc->events[idx] = NULL;
Peter Zijlstra2e841872010-01-25 15:58:43 +01001053}
1054
1055static void x86_pmu_disable(struct perf_event *event)
1056{
1057 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1058 int i;
1059
Stephane Eraniand76a0812010-02-08 17:06:01 +02001060 x86_pmu_stop(event);
Peter Zijlstra194002b2009-06-22 16:35:24 +02001061
Stephane Eranian1da53e02010-01-18 10:58:01 +02001062 for (i = 0; i < cpuc->n_events; i++) {
1063 if (event == cpuc->event_list[i]) {
1064
1065 if (x86_pmu.put_event_constraints)
1066 x86_pmu.put_event_constraints(cpuc, event);
1067
1068 while (++i < cpuc->n_events)
1069 cpuc->event_list[i-1] = cpuc->event_list[i];
1070
1071 --cpuc->n_events;
Peter Zijlstra6c9687a2010-01-25 11:57:25 +01001072 break;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001073 }
1074 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001075 perf_event_update_userpage(event);
Ingo Molnar241771e2008-12-03 10:39:53 +01001076}
1077
Peter Zijlstra8c48e442010-01-29 13:25:31 +01001078static int x86_pmu_handle_irq(struct pt_regs *regs)
Robert Richtera29aa8a2009-04-29 12:47:21 +02001079{
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001080 struct perf_sample_data data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001081 struct cpu_hw_events *cpuc;
1082 struct perf_event *event;
1083 struct hw_perf_event *hwc;
Vince Weaver11d15782009-07-08 17:46:14 -04001084 int idx, handled = 0;
Ingo Molnar9029a5e2009-05-15 08:26:20 +02001085 u64 val;
1086
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001087 perf_sample_data_init(&data, 0);
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001088
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089 cpuc = &__get_cpu_var(cpu_hw_events);
Robert Richtera29aa8a2009-04-29 12:47:21 +02001090
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091 for (idx = 0; idx < x86_pmu.num_events; idx++) {
Robert Richter43f62012009-04-29 16:55:56 +02001092 if (!test_bit(idx, cpuc->active_mask))
Robert Richtera29aa8a2009-04-29 12:47:21 +02001093 continue;
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001094
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095 event = cpuc->events[idx];
1096 hwc = &event->hw;
Peter Zijlstraa4016a72009-05-14 14:52:17 +02001097
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001098 val = x86_perf_event_update(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099 if (val & (1ULL << (x86_pmu.event_bits - 1)))
Peter Zijlstra48e22d52009-05-25 17:39:04 +02001100 continue;
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001101
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001102 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103 * event overflow
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001104 */
1105 handled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001106 data.period = event->hw.last_period;
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001107
Peter Zijlstra07088ed2010-03-02 20:16:01 +01001108 if (!x86_perf_event_set_period(event))
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001109 continue;
1110
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001111 if (perf_event_overflow(event, 1, &data, regs))
Peter Zijlstra71e2d282010-03-08 17:51:33 +01001112 x86_pmu_stop(event);
Robert Richtera29aa8a2009-04-29 12:47:21 +02001113 }
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001114
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001115 if (handled)
1116 inc_irq_stat(apic_perf_irqs);
1117
Robert Richtera29aa8a2009-04-29 12:47:21 +02001118 return handled;
1119}
Robert Richter39d81ea2009-04-29 12:47:05 +02001120
Peter Zijlstrab6276f32009-04-06 11:45:03 +02001121void smp_perf_pending_interrupt(struct pt_regs *regs)
1122{
1123 irq_enter();
1124 ack_APIC_irq();
1125 inc_irq_stat(apic_pending_irqs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126 perf_event_do_pending();
Peter Zijlstrab6276f32009-04-06 11:45:03 +02001127 irq_exit();
1128}
1129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001130void set_perf_event_pending(void)
Peter Zijlstrab6276f32009-04-06 11:45:03 +02001131{
Ingo Molnar04da8a42009-08-11 10:40:08 +02001132#ifdef CONFIG_X86_LOCAL_APIC
Peter Zijlstra7d428962009-09-23 11:03:37 +02001133 if (!x86_pmu.apic || !x86_pmu_initialized())
1134 return;
1135
Peter Zijlstrab6276f32009-04-06 11:45:03 +02001136 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
Ingo Molnar04da8a42009-08-11 10:40:08 +02001137#endif
Peter Zijlstrab6276f32009-04-06 11:45:03 +02001138}
1139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001140void perf_events_lapic_init(void)
Ingo Molnar241771e2008-12-03 10:39:53 +01001141{
Ingo Molnar04da8a42009-08-11 10:40:08 +02001142 if (!x86_pmu.apic || !x86_pmu_initialized())
Ingo Molnar241771e2008-12-03 10:39:53 +01001143 return;
Robert Richter85cf9db2009-04-29 12:47:20 +02001144
Ingo Molnar241771e2008-12-03 10:39:53 +01001145 /*
Yong Wangc323d952009-05-29 13:28:35 +08001146 * Always use NMI for PMU
Ingo Molnar241771e2008-12-03 10:39:53 +01001147 */
Yong Wangc323d952009-05-29 13:28:35 +08001148 apic_write(APIC_LVTPC, APIC_DM_NMI);
Ingo Molnar241771e2008-12-03 10:39:53 +01001149}
1150
1151static int __kprobes
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001152perf_event_nmi_handler(struct notifier_block *self,
Ingo Molnar241771e2008-12-03 10:39:53 +01001153 unsigned long cmd, void *__args)
1154{
1155 struct die_args *args = __args;
1156 struct pt_regs *regs;
1157
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001158 if (!atomic_read(&active_events))
Peter Zijlstra63a809a2009-05-01 12:23:17 +02001159 return NOTIFY_DONE;
1160
Peter Zijlstrab0f3f282009-03-05 18:08:27 +01001161 switch (cmd) {
1162 case DIE_NMI:
1163 case DIE_NMI_IPI:
1164 break;
1165
1166 default:
Ingo Molnar241771e2008-12-03 10:39:53 +01001167 return NOTIFY_DONE;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +01001168 }
Ingo Molnar241771e2008-12-03 10:39:53 +01001169
1170 regs = args->regs;
1171
1172 apic_write(APIC_LVTPC, APIC_DM_NMI);
Peter Zijlstraa4016a72009-05-14 14:52:17 +02001173 /*
1174 * Can't rely on the handled return value to say it was our NMI, two
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175 * events could trigger 'simultaneously' raising two back-to-back NMIs.
Peter Zijlstraa4016a72009-05-14 14:52:17 +02001176 *
1177 * If the first NMI handles both, the latter will be empty and daze
1178 * the CPU.
1179 */
Yong Wanga3288102009-06-03 13:12:55 +08001180 x86_pmu.handle_irq(regs);
Ingo Molnar241771e2008-12-03 10:39:53 +01001181
Peter Zijlstraa4016a72009-05-14 14:52:17 +02001182 return NOTIFY_STOP;
Ingo Molnar241771e2008-12-03 10:39:53 +01001183}
1184
Peter Zijlstraf22f54f2010-02-26 12:05:05 +01001185static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1186 .notifier_call = perf_event_nmi_handler,
1187 .next = NULL,
1188 .priority = 1
1189};
1190
Peter Zijlstra63b14642010-01-22 16:32:17 +01001191static struct event_constraint unconstrained;
Stephane Eranian38331f62010-02-08 17:17:01 +02001192static struct event_constraint emptyconstraint;
Peter Zijlstra63b14642010-01-22 16:32:17 +01001193
Peter Zijlstra63b14642010-01-22 16:32:17 +01001194static struct event_constraint *
Peter Zijlstraf22f54f2010-02-26 12:05:05 +01001195x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
Stephane Eranian1da53e02010-01-18 10:58:01 +02001196{
Peter Zijlstra63b14642010-01-22 16:32:17 +01001197 struct event_constraint *c;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001198
Stephane Eranian1da53e02010-01-18 10:58:01 +02001199 if (x86_pmu.event_constraints) {
1200 for_each_event_constraint(c, x86_pmu.event_constraints) {
Peter Zijlstra63b14642010-01-22 16:32:17 +01001201 if ((event->hw.config & c->cmask) == c->code)
1202 return c;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001203 }
1204 }
Peter Zijlstra63b14642010-01-22 16:32:17 +01001205
1206 return &unconstrained;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001207}
1208
Stephane Eranian1da53e02010-01-18 10:58:01 +02001209static int x86_event_sched_in(struct perf_event *event,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001210 struct perf_cpu_context *cpuctx)
Stephane Eranian1da53e02010-01-18 10:58:01 +02001211{
1212 int ret = 0;
1213
1214 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001215 event->oncpu = smp_processor_id();
Stephane Eranian1da53e02010-01-18 10:58:01 +02001216 event->tstamp_running += event->ctx->time - event->tstamp_stopped;
1217
1218 if (!is_x86_event(event))
1219 ret = event->pmu->enable(event);
1220
1221 if (!ret && !is_software_event(event))
1222 cpuctx->active_oncpu++;
1223
1224 if (!ret && event->attr.exclusive)
1225 cpuctx->exclusive = 1;
1226
1227 return ret;
1228}
1229
1230static void x86_event_sched_out(struct perf_event *event,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001231 struct perf_cpu_context *cpuctx)
Stephane Eranian1da53e02010-01-18 10:58:01 +02001232{
1233 event->state = PERF_EVENT_STATE_INACTIVE;
1234 event->oncpu = -1;
1235
1236 if (!is_x86_event(event))
1237 event->pmu->disable(event);
1238
1239 event->tstamp_running -= event->ctx->time - event->tstamp_stopped;
1240
1241 if (!is_software_event(event))
1242 cpuctx->active_oncpu--;
1243
1244 if (event->attr.exclusive || !cpuctx->active_oncpu)
1245 cpuctx->exclusive = 0;
1246}
1247
1248/*
1249 * Called to enable a whole group of events.
1250 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
1251 * Assumes the caller has disabled interrupts and has
1252 * frozen the PMU with hw_perf_save_disable.
1253 *
1254 * called with PMU disabled. If successful and return value 1,
1255 * then guaranteed to call perf_enable() and hw_perf_enable()
1256 */
1257int hw_perf_group_sched_in(struct perf_event *leader,
1258 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001259 struct perf_event_context *ctx)
Stephane Eranian1da53e02010-01-18 10:58:01 +02001260{
Peter Zijlstra6e377382010-02-11 13:21:58 +01001261 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001262 struct perf_event *sub;
1263 int assign[X86_PMC_IDX_MAX];
1264 int n0, n1, ret;
1265
Cyrill Gorcunov0b861222010-03-12 00:50:16 +03001266 if (!x86_pmu_initialized())
1267 return 0;
1268
Stephane Eranian1da53e02010-01-18 10:58:01 +02001269 /* n0 = total number of events */
1270 n0 = collect_events(cpuc, leader, true);
1271 if (n0 < 0)
1272 return n0;
1273
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001274 ret = x86_pmu.schedule_events(cpuc, n0, assign);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001275 if (ret)
1276 return ret;
1277
Peter Zijlstra6e377382010-02-11 13:21:58 +01001278 ret = x86_event_sched_in(leader, cpuctx);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001279 if (ret)
1280 return ret;
1281
1282 n1 = 1;
1283 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Stephane Eranian81130702010-01-21 17:39:01 +02001284 if (sub->state > PERF_EVENT_STATE_OFF) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001285 ret = x86_event_sched_in(sub, cpuctx);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001286 if (ret)
1287 goto undo;
1288 ++n1;
1289 }
1290 }
1291 /*
1292 * copy new assignment, now we know it is possible
1293 * will be used by hw_perf_enable()
1294 */
1295 memcpy(cpuc->assign, assign, n0*sizeof(int));
1296
1297 cpuc->n_events = n0;
Peter Zijlstra356e1f22010-03-06 13:49:56 +01001298 cpuc->n_added += n1;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001299 ctx->nr_active += n1;
1300
1301 /*
1302 * 1 means successful and events are active
1303 * This is not quite true because we defer
1304 * actual activation until hw_perf_enable() but
1305 * this way we* ensure caller won't try to enable
1306 * individual events
1307 */
1308 return 1;
1309undo:
Peter Zijlstra6e377382010-02-11 13:21:58 +01001310 x86_event_sched_out(leader, cpuctx);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001311 n0 = 1;
1312 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1313 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001314 x86_event_sched_out(sub, cpuctx);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001315 if (++n0 == n1)
1316 break;
1317 }
1318 }
1319 return ret;
1320}
1321
Peter Zijlstraf22f54f2010-02-26 12:05:05 +01001322#include "perf_event_amd.c"
1323#include "perf_event_p6.c"
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001324#include "perf_event_p4.c"
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001325#include "perf_event_intel_lbr.c"
Peter Zijlstraca037702010-03-02 19:52:12 +01001326#include "perf_event_intel_ds.c"
Peter Zijlstraf22f54f2010-02-26 12:05:05 +01001327#include "perf_event_intel.c"
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301328
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001329static int __cpuinit
1330x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1331{
1332 unsigned int cpu = (long)hcpu;
1333
1334 switch (action & ~CPU_TASKS_FROZEN) {
1335 case CPU_UP_PREPARE:
1336 if (x86_pmu.cpu_prepare)
1337 x86_pmu.cpu_prepare(cpu);
1338 break;
1339
1340 case CPU_STARTING:
1341 if (x86_pmu.cpu_starting)
1342 x86_pmu.cpu_starting(cpu);
1343 break;
1344
1345 case CPU_DYING:
1346 if (x86_pmu.cpu_dying)
1347 x86_pmu.cpu_dying(cpu);
1348 break;
1349
1350 case CPU_DEAD:
1351 if (x86_pmu.cpu_dead)
1352 x86_pmu.cpu_dead(cpu);
1353 break;
1354
1355 default:
1356 break;
1357 }
1358
1359 return NOTIFY_OK;
1360}
1361
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001362static void __init pmu_check_apic(void)
1363{
1364 if (cpu_has_apic)
1365 return;
1366
1367 x86_pmu.apic = 0;
1368 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1369 pr_info("no hardware sampling interrupt available.\n");
1370}
1371
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001372void __init init_hw_perf_events(void)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301373{
Peter Zijlstrab622d642010-02-01 15:36:30 +01001374 struct event_constraint *c;
Robert Richter72eae042009-04-29 12:47:10 +02001375 int err;
1376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377 pr_info("Performance Events: ");
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001378
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301379 switch (boot_cpu_data.x86_vendor) {
1380 case X86_VENDOR_INTEL:
Robert Richter72eae042009-04-29 12:47:10 +02001381 err = intel_pmu_init();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301382 break;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301383 case X86_VENDOR_AMD:
Robert Richter72eae042009-04-29 12:47:10 +02001384 err = amd_pmu_init();
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301385 break;
Robert Richter41389602009-04-29 12:47:00 +02001386 default:
1387 return;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301388 }
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001389 if (err != 0) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001390 pr_cont("no PMU driver, software events only.\n");
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301391 return;
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001392 }
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301393
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001394 pmu_check_apic();
1395
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001396 pr_cont("%s PMU driver.\n", x86_pmu.name);
Robert Richterfaa28ae2009-04-29 12:47:13 +02001397
Peter Zijlstra3c447802010-03-04 21:49:01 +01001398 if (x86_pmu.quirks)
1399 x86_pmu.quirks();
1400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401 if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) {
1402 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
1403 x86_pmu.num_events, X86_PMC_MAX_GENERIC);
1404 x86_pmu.num_events = X86_PMC_MAX_GENERIC;
Ingo Molnar241771e2008-12-03 10:39:53 +01001405 }
Robert Richterd6dc0b42010-03-17 12:49:13 +01001406 x86_pmu.intel_ctrl = (1 << x86_pmu.num_events) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407 perf_max_events = x86_pmu.num_events;
Ingo Molnar241771e2008-12-03 10:39:53 +01001408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409 if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) {
1410 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
1411 x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED);
1412 x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED;
Ingo Molnar703e9372008-12-17 10:51:15 +01001413 }
Ingo Molnar241771e2008-12-03 10:39:53 +01001414
Robert Richterd6dc0b42010-03-17 12:49:13 +01001415 x86_pmu.intel_ctrl |=
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416 ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED;
Ingo Molnar862a1a52008-12-17 13:09:20 +01001417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001418 perf_events_lapic_init();
1419 register_die_notifier(&perf_event_nmi_notifier);
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001420
Peter Zijlstra63b14642010-01-22 16:32:17 +01001421 unconstrained = (struct event_constraint)
Peter Zijlstrafce877e2010-01-29 13:25:12 +01001422 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1,
1423 0, x86_pmu.num_events);
Peter Zijlstra63b14642010-01-22 16:32:17 +01001424
Peter Zijlstrab622d642010-02-01 15:36:30 +01001425 if (x86_pmu.event_constraints) {
1426 for_each_event_constraint(c, x86_pmu.event_constraints) {
1427 if (c->cmask != INTEL_ARCH_FIXED_MASK)
1428 continue;
1429
1430 c->idxmsk64 |= (1ULL << x86_pmu.num_events) - 1;
1431 c->weight += x86_pmu.num_events;
1432 }
1433 }
1434
Ingo Molnar57c0c152009-09-21 12:20:38 +02001435 pr_info("... version: %d\n", x86_pmu.version);
1436 pr_info("... bit width: %d\n", x86_pmu.event_bits);
1437 pr_info("... generic registers: %d\n", x86_pmu.num_events);
1438 pr_info("... value mask: %016Lx\n", x86_pmu.event_mask);
1439 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
1440 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed);
Robert Richterd6dc0b42010-03-17 12:49:13 +01001441 pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001442
1443 perf_cpu_notifier(x86_pmu_notifier);
Ingo Molnar241771e2008-12-03 10:39:53 +01001444}
Ingo Molnar621a01e2008-12-11 12:46:46 +01001445
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001446static inline void x86_pmu_read(struct perf_event *event)
Ingo Molnaree060942008-12-13 09:00:03 +01001447{
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001448 x86_perf_event_update(event);
Ingo Molnaree060942008-12-13 09:00:03 +01001449}
1450
Robert Richter4aeb0b42009-04-29 12:47:03 +02001451static const struct pmu pmu = {
1452 .enable = x86_pmu_enable,
1453 .disable = x86_pmu_disable,
Stephane Eraniand76a0812010-02-08 17:06:01 +02001454 .start = x86_pmu_start,
1455 .stop = x86_pmu_stop,
Robert Richter4aeb0b42009-04-29 12:47:03 +02001456 .read = x86_pmu_read,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001457 .unthrottle = x86_pmu_unthrottle,
Ingo Molnar621a01e2008-12-11 12:46:46 +01001458};
1459
Stephane Eranian1da53e02010-01-18 10:58:01 +02001460/*
Peter Zijlstraca037702010-03-02 19:52:12 +01001461 * validate that we can schedule this event
1462 */
1463static int validate_event(struct perf_event *event)
1464{
1465 struct cpu_hw_events *fake_cpuc;
1466 struct event_constraint *c;
1467 int ret = 0;
1468
1469 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1470 if (!fake_cpuc)
1471 return -ENOMEM;
1472
1473 c = x86_pmu.get_event_constraints(fake_cpuc, event);
1474
1475 if (!c || !c->weight)
1476 ret = -ENOSPC;
1477
1478 if (x86_pmu.put_event_constraints)
1479 x86_pmu.put_event_constraints(fake_cpuc, event);
1480
1481 kfree(fake_cpuc);
1482
1483 return ret;
1484}
1485
1486/*
Stephane Eranian1da53e02010-01-18 10:58:01 +02001487 * validate a single event group
1488 *
1489 * validation include:
Ingo Molnar184f4122010-01-27 08:39:39 +01001490 * - check events are compatible which each other
1491 * - events do not compete for the same counter
1492 * - number of events <= number of counters
Stephane Eranian1da53e02010-01-18 10:58:01 +02001493 *
1494 * validation ensures the group can be loaded onto the
1495 * PMU if it was the only group available.
1496 */
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001497static int validate_group(struct perf_event *event)
1498{
Stephane Eranian1da53e02010-01-18 10:58:01 +02001499 struct perf_event *leader = event->group_leader;
Peter Zijlstra502568d2010-01-22 14:35:46 +01001500 struct cpu_hw_events *fake_cpuc;
1501 int ret, n;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001502
Peter Zijlstra502568d2010-01-22 14:35:46 +01001503 ret = -ENOMEM;
1504 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1505 if (!fake_cpuc)
1506 goto out;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001507
Stephane Eranian1da53e02010-01-18 10:58:01 +02001508 /*
1509 * the event is not yet connected with its
1510 * siblings therefore we must first collect
1511 * existing siblings, then add the new event
1512 * before we can simulate the scheduling
1513 */
Peter Zijlstra502568d2010-01-22 14:35:46 +01001514 ret = -ENOSPC;
1515 n = collect_events(fake_cpuc, leader, true);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001516 if (n < 0)
Peter Zijlstra502568d2010-01-22 14:35:46 +01001517 goto out_free;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001518
Peter Zijlstra502568d2010-01-22 14:35:46 +01001519 fake_cpuc->n_events = n;
1520 n = collect_events(fake_cpuc, event, false);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001521 if (n < 0)
Peter Zijlstra502568d2010-01-22 14:35:46 +01001522 goto out_free;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001523
Peter Zijlstra502568d2010-01-22 14:35:46 +01001524 fake_cpuc->n_events = n;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001525
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001526 ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
Peter Zijlstra502568d2010-01-22 14:35:46 +01001527
1528out_free:
1529 kfree(fake_cpuc);
1530out:
1531 return ret;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001532}
1533
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001534const struct pmu *hw_perf_event_init(struct perf_event *event)
Ingo Molnar621a01e2008-12-11 12:46:46 +01001535{
Stephane Eranian81130702010-01-21 17:39:01 +02001536 const struct pmu *tmp;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001537 int err;
1538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539 err = __hw_perf_event_init(event);
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001540 if (!err) {
Stephane Eranian81130702010-01-21 17:39:01 +02001541 /*
1542 * we temporarily connect event to its pmu
1543 * such that validate_group() can classify
1544 * it as an x86 event using is_x86_event()
1545 */
1546 tmp = event->pmu;
1547 event->pmu = &pmu;
1548
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001549 if (event->group_leader != event)
1550 err = validate_group(event);
Peter Zijlstraca037702010-03-02 19:52:12 +01001551 else
1552 err = validate_event(event);
Stephane Eranian81130702010-01-21 17:39:01 +02001553
1554 event->pmu = tmp;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001555 }
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +02001556 if (err) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557 if (event->destroy)
1558 event->destroy(event);
Peter Zijlstra9ea98e12009-03-30 19:07:09 +02001559 return ERR_PTR(err);
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +02001560 }
Ingo Molnar621a01e2008-12-11 12:46:46 +01001561
Robert Richter4aeb0b42009-04-29 12:47:03 +02001562 return &pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001563}
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001564
1565/*
1566 * callchain support
1567 */
1568
1569static inline
Peter Zijlstraf9188e02009-06-18 22:20:52 +02001570void callchain_store(struct perf_callchain_entry *entry, u64 ip)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001571{
Peter Zijlstraf9188e02009-06-18 22:20:52 +02001572 if (entry->nr < PERF_MAX_STACK_DEPTH)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001573 entry->ip[entry->nr++] = ip;
1574}
1575
Tejun Heo245b2e72009-06-24 15:13:48 +09001576static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
1577static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001578
1579
1580static void
1581backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1582{
1583 /* Ignore warnings */
1584}
1585
1586static void backtrace_warning(void *data, char *msg)
1587{
1588 /* Ignore warnings */
1589}
1590
1591static int backtrace_stack(void *data, char *name)
1592{
Ingo Molnar038e8362009-06-15 09:57:59 +02001593 return 0;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001594}
1595
1596static void backtrace_address(void *data, unsigned long addr, int reliable)
1597{
1598 struct perf_callchain_entry *entry = data;
1599
1600 if (reliable)
1601 callchain_store(entry, addr);
1602}
1603
1604static const struct stacktrace_ops backtrace_ops = {
1605 .warning = backtrace_warning,
1606 .warning_symbol = backtrace_warning_symbol,
1607 .stack = backtrace_stack,
1608 .address = backtrace_address,
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +01001609 .walk_stack = print_context_stack_bp,
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001610};
1611
Ingo Molnar038e8362009-06-15 09:57:59 +02001612#include "../dumpstack.h"
1613
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001614static void
1615perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1616{
Peter Zijlstraf9188e02009-06-18 22:20:52 +02001617 callchain_store(entry, PERF_CONTEXT_KERNEL);
Ingo Molnar038e8362009-06-15 09:57:59 +02001618 callchain_store(entry, regs->ip);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001619
Frederic Weisbecker48b5ba92009-12-31 05:53:02 +01001620 dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001621}
1622
Peter Zijlstra74193ef2009-06-15 13:07:24 +02001623static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1624{
1625 unsigned long bytes;
1626
1627 bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
1628
1629 return bytes == sizeof(*frame);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001630}
1631
1632static void
1633perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1634{
1635 struct stack_frame frame;
1636 const void __user *fp;
1637
Ingo Molnar5a6cec32009-05-29 11:25:09 +02001638 if (!user_mode(regs))
1639 regs = task_pt_regs(current);
1640
Peter Zijlstra74193ef2009-06-15 13:07:24 +02001641 fp = (void __user *)regs->bp;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001642
Peter Zijlstraf9188e02009-06-18 22:20:52 +02001643 callchain_store(entry, PERF_CONTEXT_USER);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001644 callchain_store(entry, regs->ip);
1645
Peter Zijlstraf9188e02009-06-18 22:20:52 +02001646 while (entry->nr < PERF_MAX_STACK_DEPTH) {
Ingo Molnar038e8362009-06-15 09:57:59 +02001647 frame.next_frame = NULL;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001648 frame.return_address = 0;
1649
1650 if (!copy_stack_frame(fp, &frame))
1651 break;
1652
Ingo Molnar5a6cec32009-05-29 11:25:09 +02001653 if ((unsigned long)fp < regs->sp)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001654 break;
1655
1656 callchain_store(entry, frame.return_address);
Ingo Molnar038e8362009-06-15 09:57:59 +02001657 fp = frame.next_frame;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001658 }
1659}
1660
1661static void
1662perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1663{
1664 int is_user;
1665
1666 if (!regs)
1667 return;
1668
1669 is_user = user_mode(regs);
1670
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001671 if (is_user && current->state != TASK_RUNNING)
1672 return;
1673
1674 if (!is_user)
1675 perf_callchain_kernel(regs, entry);
1676
1677 if (current->mm)
1678 perf_callchain_user(regs, entry);
1679}
1680
1681struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1682{
1683 struct perf_callchain_entry *entry;
1684
1685 if (in_nmi())
Tejun Heo245b2e72009-06-24 15:13:48 +09001686 entry = &__get_cpu_var(pmc_nmi_entry);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001687 else
Tejun Heo245b2e72009-06-24 15:13:48 +09001688 entry = &__get_cpu_var(pmc_irq_entry);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001689
1690 entry->nr = 0;
1691
1692 perf_do_callchain(regs, entry);
1693
1694 return entry;
1695}
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001696
Frederic Weisbecker1d199b12010-03-16 01:05:02 +01001697#ifdef CONFIG_EVENT_TRACING
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001698void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
1699{
1700 regs->ip = ip;
1701 /*
1702 * perf_arch_fetch_caller_regs adds another call, we need to increment
1703 * the skip level
1704 */
1705 regs->bp = rewind_frame_pointer(skip + 1);
1706 regs->cs = __KERNEL_CS;
1707 local_save_flags(regs->flags);
1708}
Frederic Weisbecker1d199b12010-03-16 01:05:02 +01001709#endif