blob: c74e20d593a77ede26b3c47de5d90b2811ee818b [file] [log] [blame]
Ingo Molnar241771e2008-12-03 10:39:53 +01001/*
2 * Performance counter x86 architecture code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05306 * Copyright(C) 2009 Jaswinder Singh Rajput
Ingo Molnar241771e2008-12-03 10:39:53 +01007 *
8 * For licencing details see kernel-base/COPYING
9 */
10
11#include <linux/perf_counter.h>
12#include <linux/capability.h>
13#include <linux/notifier.h>
14#include <linux/hardirq.h>
15#include <linux/kprobes.h>
Thomas Gleixner4ac13292008-12-09 21:43:39 +010016#include <linux/module.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010017#include <linux/kdebug.h>
18#include <linux/sched.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020019#include <linux/uaccess.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010020
Ingo Molnar241771e2008-12-03 10:39:53 +010021#include <asm/apic.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020022#include <asm/stacktrace.h>
Peter Zijlstra4e935e42009-03-30 19:07:16 +020023#include <asm/nmi.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010024
25static bool perf_counters_initialized __read_mostly;
26
27/*
28 * Number of (generic) HW counters:
29 */
Ingo Molnar862a1a52008-12-17 13:09:20 +010030static int nr_counters_generic __read_mostly;
31static u64 perf_counter_mask __read_mostly;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +010032static u64 counter_value_mask __read_mostly;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010033static int counter_value_bits __read_mostly;
Ingo Molnar241771e2008-12-03 10:39:53 +010034
Ingo Molnar862a1a52008-12-17 13:09:20 +010035static int nr_counters_fixed __read_mostly;
Ingo Molnar703e9372008-12-17 10:51:15 +010036
Ingo Molnar241771e2008-12-03 10:39:53 +010037struct cpu_hw_counters {
Ingo Molnar862a1a52008-12-17 13:09:20 +010038 struct perf_counter *counters[X86_PMC_IDX_MAX];
39 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Mike Galbraith4b39fd92009-01-23 14:36:16 +010040 unsigned long interrupts;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010041 u64 throttle_ctrl;
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +010042 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010043 int enabled;
Ingo Molnar241771e2008-12-03 10:39:53 +010044};
45
46/*
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053047 * struct pmc_x86_ops - performance counter x86 ops
Ingo Molnar241771e2008-12-03 10:39:53 +010048 */
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053049struct pmc_x86_ops {
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +053050 u64 (*save_disable_all)(void);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010051 void (*restore_all)(u64);
52 u64 (*get_status)(u64);
53 void (*ack_status)(u64);
54 void (*enable)(int, u64);
55 void (*disable)(int, u64);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +053056 unsigned eventsel;
57 unsigned perfctr;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010058 u64 (*event_map)(int);
59 u64 (*raw_event)(u64);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +053060 int max_events;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053061};
62
Ingo Molnar7bb497b2009-03-18 08:59:21 +010063static struct pmc_x86_ops *pmc_ops __read_mostly;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053064
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010065static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
66 .enabled = 1,
67};
Ingo Molnar241771e2008-12-03 10:39:53 +010068
Ingo Molnar7bb497b2009-03-18 08:59:21 +010069static __read_mostly int intel_perfmon_version;
70
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053071/*
72 * Intel PerfMon v3. Used on Core2 and later.
73 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010074static const u64 intel_perfmon_event_map[] =
Ingo Molnar241771e2008-12-03 10:39:53 +010075{
Ingo Molnarf650a672008-12-23 12:17:29 +010076 [PERF_COUNT_CPU_CYCLES] = 0x003c,
Ingo Molnar241771e2008-12-03 10:39:53 +010077 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
78 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
79 [PERF_COUNT_CACHE_MISSES] = 0x412e,
80 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
81 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
Ingo Molnarf650a672008-12-23 12:17:29 +010082 [PERF_COUNT_BUS_CYCLES] = 0x013c,
Ingo Molnar241771e2008-12-03 10:39:53 +010083};
84
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010085static u64 pmc_intel_event_map(int event)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053086{
87 return intel_perfmon_event_map[event];
88}
Ingo Molnar241771e2008-12-03 10:39:53 +010089
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010090static u64 pmc_intel_raw_event(u64 event)
91{
Peter Zijlstra82bae4f82009-03-13 12:21:31 +010092#define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
93#define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
94#define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010095
96#define CORE_EVNTSEL_MASK \
97 (CORE_EVNTSEL_EVENT_MASK | \
98 CORE_EVNTSEL_UNIT_MASK | \
99 CORE_EVNTSEL_COUNTER_MASK)
100
101 return event & CORE_EVNTSEL_MASK;
102}
103
Ingo Molnar241771e2008-12-03 10:39:53 +0100104/*
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530105 * AMD Performance Monitor K7 and later.
106 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100107static const u64 amd_perfmon_event_map[] =
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530108{
109 [PERF_COUNT_CPU_CYCLES] = 0x0076,
110 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
111 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
112 [PERF_COUNT_CACHE_MISSES] = 0x0081,
113 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
114 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
115};
116
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100117static u64 pmc_amd_event_map(int event)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530118{
119 return amd_perfmon_event_map[event];
120}
121
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100122static u64 pmc_amd_raw_event(u64 event)
123{
Peter Zijlstra82bae4f82009-03-13 12:21:31 +0100124#define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
125#define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
126#define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100127
128#define K7_EVNTSEL_MASK \
129 (K7_EVNTSEL_EVENT_MASK | \
130 K7_EVNTSEL_UNIT_MASK | \
131 K7_EVNTSEL_COUNTER_MASK)
132
133 return event & K7_EVNTSEL_MASK;
134}
135
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530136/*
Ingo Molnaree060942008-12-13 09:00:03 +0100137 * Propagate counter elapsed time into the generic counter.
138 * Can only be executed on the CPU where the counter is active.
139 * Returns the delta events processed.
140 */
141static void
142x86_perf_counter_update(struct perf_counter *counter,
143 struct hw_perf_counter *hwc, int idx)
144{
145 u64 prev_raw_count, new_raw_count, delta;
146
Ingo Molnaree060942008-12-13 09:00:03 +0100147 /*
148 * Careful: an NMI might modify the previous counter value.
149 *
150 * Our tactic to handle this is to first atomically read and
151 * exchange a new raw count - then add that new-prev delta
152 * count to the generic counter atomically:
153 */
154again:
155 prev_raw_count = atomic64_read(&hwc->prev_count);
156 rdmsrl(hwc->counter_base + idx, new_raw_count);
157
158 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
159 new_raw_count) != prev_raw_count)
160 goto again;
161
162 /*
163 * Now we have the new raw value and have updated the prev
164 * timestamp already. We can now calculate the elapsed delta
165 * (counter-)time and add that to the generic counter.
166 *
167 * Careful, not all hw sign-extends above the physical width
168 * of the count, so we do that by clipping the delta to 32 bits:
169 */
170 delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
Ingo Molnaree060942008-12-13 09:00:03 +0100171
172 atomic64_add(delta, &counter->count);
173 atomic64_sub(delta, &hwc->period_left);
174}
175
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200176static atomic_t num_counters;
177static DEFINE_MUTEX(pmc_reserve_mutex);
178
179static bool reserve_pmc_hardware(void)
180{
181 int i;
182
183 if (nmi_watchdog == NMI_LOCAL_APIC)
184 disable_lapic_nmi_watchdog();
185
186 for (i = 0; i < nr_counters_generic; i++) {
187 if (!reserve_perfctr_nmi(pmc_ops->perfctr + i))
188 goto perfctr_fail;
189 }
190
191 for (i = 0; i < nr_counters_generic; i++) {
192 if (!reserve_evntsel_nmi(pmc_ops->eventsel + i))
193 goto eventsel_fail;
194 }
195
196 return true;
197
198eventsel_fail:
199 for (i--; i >= 0; i--)
200 release_evntsel_nmi(pmc_ops->eventsel + i);
201
202 i = nr_counters_generic;
203
204perfctr_fail:
205 for (i--; i >= 0; i--)
206 release_perfctr_nmi(pmc_ops->perfctr + i);
207
208 if (nmi_watchdog == NMI_LOCAL_APIC)
209 enable_lapic_nmi_watchdog();
210
211 return false;
212}
213
214static void release_pmc_hardware(void)
215{
216 int i;
217
218 for (i = 0; i < nr_counters_generic; i++) {
219 release_perfctr_nmi(pmc_ops->perfctr + i);
220 release_evntsel_nmi(pmc_ops->eventsel + i);
221 }
222
223 if (nmi_watchdog == NMI_LOCAL_APIC)
224 enable_lapic_nmi_watchdog();
225}
226
227static void hw_perf_counter_destroy(struct perf_counter *counter)
228{
229 if (atomic_dec_and_mutex_lock(&num_counters, &pmc_reserve_mutex)) {
230 release_pmc_hardware();
231 mutex_unlock(&pmc_reserve_mutex);
232 }
233}
234
Ingo Molnaree060942008-12-13 09:00:03 +0100235/*
Ingo Molnar241771e2008-12-03 10:39:53 +0100236 * Setup the hardware configuration for a given hw_event_type
237 */
Ingo Molnar621a01e2008-12-11 12:46:46 +0100238static int __hw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar241771e2008-12-03 10:39:53 +0100239{
Ingo Molnar9f66a382008-12-10 12:33:23 +0100240 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar241771e2008-12-03 10:39:53 +0100241 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200242 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100243
244 if (unlikely(!perf_counters_initialized))
245 return -EINVAL;
246
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200247 err = 0;
248 if (atomic_inc_not_zero(&num_counters)) {
249 mutex_lock(&pmc_reserve_mutex);
250 if (atomic_read(&num_counters) == 0 && !reserve_pmc_hardware())
251 err = -EBUSY;
252 else
253 atomic_inc(&num_counters);
254 mutex_unlock(&pmc_reserve_mutex);
255 }
256 if (err)
257 return err;
258
Ingo Molnar241771e2008-12-03 10:39:53 +0100259 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100260 * Generate PMC IRQs:
Ingo Molnar241771e2008-12-03 10:39:53 +0100261 * (keep 'enabled' bit clear for now)
262 */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100263 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
Ingo Molnar241771e2008-12-03 10:39:53 +0100264
265 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100266 * Count user and OS events unless requested not to.
267 */
268 if (!hw_event->exclude_user)
269 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
270 if (!hw_event->exclude_kernel)
271 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
272
273 /*
274 * If privileged enough, allow NMI events:
Ingo Molnar241771e2008-12-03 10:39:53 +0100275 */
276 hwc->nmi = 0;
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100277 if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
278 hwc->nmi = 1;
Ingo Molnar241771e2008-12-03 10:39:53 +0100279
Ingo Molnar9f66a382008-12-10 12:33:23 +0100280 hwc->irq_period = hw_event->irq_period;
Ingo Molnar241771e2008-12-03 10:39:53 +0100281 /*
282 * Intel PMCs cannot be accessed sanely above 32 bit width,
283 * so we install an artificial 1<<31 period regardless of
284 * the generic counter period:
285 */
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530286 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
287 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
288 hwc->irq_period = 0x7FFFFFFF;
Ingo Molnar241771e2008-12-03 10:39:53 +0100289
Ingo Molnaree060942008-12-13 09:00:03 +0100290 atomic64_set(&hwc->period_left, hwc->irq_period);
Ingo Molnar241771e2008-12-03 10:39:53 +0100291
292 /*
Thomas Gleixnerdfa7c892008-12-08 19:35:37 +0100293 * Raw event type provide the config in the event structure
Ingo Molnar241771e2008-12-03 10:39:53 +0100294 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +0100295 if (perf_event_raw(hw_event)) {
296 hwc->config |= pmc_ops->raw_event(perf_event_config(hw_event));
Ingo Molnar241771e2008-12-03 10:39:53 +0100297 } else {
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +0100298 if (perf_event_id(hw_event) >= pmc_ops->max_events)
Ingo Molnar241771e2008-12-03 10:39:53 +0100299 return -EINVAL;
300 /*
301 * The generic map:
302 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +0100303 hwc->config |= pmc_ops->event_map(perf_event_id(hw_event));
Ingo Molnar241771e2008-12-03 10:39:53 +0100304 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100305
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200306 counter->destroy = hw_perf_counter_destroy;
307
Ingo Molnar241771e2008-12-03 10:39:53 +0100308 return 0;
309}
310
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530311static u64 pmc_intel_save_disable_all(void)
Thomas Gleixner4ac13292008-12-09 21:43:39 +0100312{
313 u64 ctrl;
314
315 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
Ingo Molnar862a1a52008-12-17 13:09:20 +0100316 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +0100317
Thomas Gleixner4ac13292008-12-09 21:43:39 +0100318 return ctrl;
319}
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530320
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530321static u64 pmc_amd_save_disable_all(void)
322{
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100323 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
324 int enabled, idx;
325
326 enabled = cpuc->enabled;
327 cpuc->enabled = 0;
Peter Zijlstra60b3df92009-03-13 12:21:30 +0100328 /*
329 * ensure we write the disable before we start disabling the
330 * counters proper, so that pcm_amd_enable() does the right thing.
331 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100332 barrier();
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530333
334 for (idx = 0; idx < nr_counters_generic; idx++) {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100335 u64 val;
336
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530337 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100338 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) {
339 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
340 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
341 }
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530342 }
343
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100344 return enabled;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530345}
346
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530347u64 hw_perf_save_disable(void)
348{
349 if (unlikely(!perf_counters_initialized))
350 return 0;
351
352 return pmc_ops->save_disable_all();
353}
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100354/*
355 * Exported because of ACPI idle
356 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100357EXPORT_SYMBOL_GPL(hw_perf_save_disable);
Ingo Molnar241771e2008-12-03 10:39:53 +0100358
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530359static void pmc_intel_restore_all(u64 ctrl)
360{
361 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
362}
363
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530364static void pmc_amd_restore_all(u64 ctrl)
365{
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100366 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530367 int idx;
368
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100369 cpuc->enabled = ctrl;
370 barrier();
371 if (!ctrl)
372 return;
373
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530374 for (idx = 0; idx < nr_counters_generic; idx++) {
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +0100375 if (test_bit(idx, cpuc->active_mask)) {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100376 u64 val;
377
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530378 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
379 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
380 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
381 }
382 }
383}
384
Ingo Molnaree060942008-12-13 09:00:03 +0100385void hw_perf_restore(u64 ctrl)
386{
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +0100387 if (unlikely(!perf_counters_initialized))
388 return;
389
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530390 pmc_ops->restore_all(ctrl);
Ingo Molnaree060942008-12-13 09:00:03 +0100391}
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100392/*
393 * Exported because of ACPI idle
394 */
Ingo Molnaree060942008-12-13 09:00:03 +0100395EXPORT_SYMBOL_GPL(hw_perf_restore);
396
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100397static u64 pmc_intel_get_status(u64 mask)
398{
399 u64 status;
400
401 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
402
403 return status;
404}
405
406static u64 pmc_amd_get_status(u64 mask)
407{
408 u64 status = 0;
409 int idx;
410
411 for (idx = 0; idx < nr_counters_generic; idx++) {
412 s64 val;
413
414 if (!(mask & (1 << idx)))
415 continue;
416
417 rdmsrl(MSR_K7_PERFCTR0 + idx, val);
418 val <<= (64 - counter_value_bits);
419 if (val >= 0)
420 status |= (1 << idx);
421 }
422
423 return status;
424}
425
426static u64 hw_perf_get_status(u64 mask)
427{
428 if (unlikely(!perf_counters_initialized))
429 return 0;
430
431 return pmc_ops->get_status(mask);
432}
433
434static void pmc_intel_ack_status(u64 ack)
435{
436 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
437}
438
439static void pmc_amd_ack_status(u64 ack)
440{
441}
442
443static void hw_perf_ack_status(u64 ack)
444{
445 if (unlikely(!perf_counters_initialized))
446 return;
447
448 pmc_ops->ack_status(ack);
449}
450
451static void pmc_intel_enable(int idx, u64 config)
452{
453 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
454 config | ARCH_PERFMON_EVENTSEL0_ENABLE);
455}
456
457static void pmc_amd_enable(int idx, u64 config)
458{
459 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
460
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +0100461 set_bit(idx, cpuc->active_mask);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100462 if (cpuc->enabled)
463 config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
464
465 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
466}
467
468static void hw_perf_enable(int idx, u64 config)
469{
470 if (unlikely(!perf_counters_initialized))
471 return;
472
473 pmc_ops->enable(idx, config);
474}
475
476static void pmc_intel_disable(int idx, u64 config)
477{
478 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
479}
480
481static void pmc_amd_disable(int idx, u64 config)
482{
483 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
484
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +0100485 clear_bit(idx, cpuc->active_mask);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100486 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
487
488}
489
490static void hw_perf_disable(int idx, u64 config)
491{
492 if (unlikely(!perf_counters_initialized))
493 return;
494
495 pmc_ops->disable(idx, config);
496}
497
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100498static inline void
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100499__pmc_fixed_disable(struct perf_counter *counter,
500 struct hw_perf_counter *hwc, unsigned int __idx)
501{
502 int idx = __idx - X86_PMC_IDX_FIXED;
503 u64 ctrl_val, mask;
504 int err;
505
506 mask = 0xfULL << (idx * 4);
507
508 rdmsrl(hwc->config_base, ctrl_val);
509 ctrl_val &= ~mask;
510 err = checking_wrmsrl(hwc->config_base, ctrl_val);
511}
512
513static inline void
Ingo Molnareb2b8612008-12-17 09:09:13 +0100514__pmc_generic_disable(struct perf_counter *counter,
Ingo Molnaree060942008-12-13 09:00:03 +0100515 struct hw_perf_counter *hwc, unsigned int idx)
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100516{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100517 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
Jaswinder Singh Rajput2b583d82008-12-27 19:15:43 +0530518 __pmc_fixed_disable(counter, hwc, idx);
519 else
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100520 hw_perf_disable(idx, hwc->config);
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100521}
522
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100523static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
Ingo Molnar241771e2008-12-03 10:39:53 +0100524
Ingo Molnaree060942008-12-13 09:00:03 +0100525/*
526 * Set the next IRQ period, based on the hwc->period_left value.
527 * To be called with the counter disabled in hw:
528 */
529static void
530__hw_perf_counter_set_period(struct perf_counter *counter,
531 struct hw_perf_counter *hwc, int idx)
Ingo Molnar241771e2008-12-03 10:39:53 +0100532{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100533 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstra595258a2009-03-13 12:21:28 +0100534 s64 period = hwc->irq_period;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100535 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100536
Ingo Molnaree060942008-12-13 09:00:03 +0100537 /*
538 * If we are way outside a reasoable range then just skip forward:
539 */
540 if (unlikely(left <= -period)) {
541 left = period;
542 atomic64_set(&hwc->period_left, left);
543 }
544
545 if (unlikely(left <= 0)) {
546 left += period;
547 atomic64_set(&hwc->period_left, left);
548 }
549
Ingo Molnaree060942008-12-13 09:00:03 +0100550 per_cpu(prev_left[idx], smp_processor_id()) = left;
551
552 /*
553 * The hw counter starts counting from this counter offset,
554 * mark it to be able to extra future deltas:
555 */
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100556 atomic64_set(&hwc->prev_count, (u64)-left);
Ingo Molnaree060942008-12-13 09:00:03 +0100557
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100558 err = checking_wrmsrl(hwc->counter_base + idx,
559 (u64)(-left) & counter_value_mask);
560}
561
562static inline void
563__pmc_fixed_enable(struct perf_counter *counter,
564 struct hw_perf_counter *hwc, unsigned int __idx)
565{
566 int idx = __idx - X86_PMC_IDX_FIXED;
567 u64 ctrl_val, bits, mask;
568 int err;
569
570 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100571 * Enable IRQ generation (0x8),
572 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
573 * if requested:
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100574 */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100575 bits = 0x8ULL;
576 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
577 bits |= 0x2;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100578 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
579 bits |= 0x1;
580 bits <<= (idx * 4);
581 mask = 0xfULL << (idx * 4);
582
583 rdmsrl(hwc->config_base, ctrl_val);
584 ctrl_val &= ~mask;
585 ctrl_val |= bits;
586 err = checking_wrmsrl(hwc->config_base, ctrl_val);
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100587}
588
Ingo Molnaree060942008-12-13 09:00:03 +0100589static void
Ingo Molnareb2b8612008-12-17 09:09:13 +0100590__pmc_generic_enable(struct perf_counter *counter,
Ingo Molnaree060942008-12-13 09:00:03 +0100591 struct hw_perf_counter *hwc, int idx)
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100592{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100593 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
Jaswinder Singh Rajput2b583d82008-12-27 19:15:43 +0530594 __pmc_fixed_enable(counter, hwc, idx);
595 else
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100596 hw_perf_enable(idx, hwc->config);
Ingo Molnar241771e2008-12-03 10:39:53 +0100597}
598
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100599static int
600fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
Ingo Molnar862a1a52008-12-17 13:09:20 +0100601{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100602 unsigned int event;
603
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530604 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
605 return -1;
606
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100607 if (unlikely(hwc->nmi))
608 return -1;
609
610 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
611
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530612 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS)))
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100613 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530614 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES)))
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100615 return X86_PMC_IDX_FIXED_CPU_CYCLES;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530616 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES)))
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100617 return X86_PMC_IDX_FIXED_BUS_CYCLES;
618
Ingo Molnar862a1a52008-12-17 13:09:20 +0100619 return -1;
620}
621
Ingo Molnaree060942008-12-13 09:00:03 +0100622/*
623 * Find a PMC slot for the freshly enabled / scheduled in counter:
624 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100625static int pmc_generic_enable(struct perf_counter *counter)
Ingo Molnar241771e2008-12-03 10:39:53 +0100626{
627 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
628 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100629 int idx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100630
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100631 idx = fixed_mode_idx(counter, hwc);
632 if (idx >= 0) {
633 /*
634 * Try to get the fixed counter, if that is already taken
635 * then try to get a generic counter:
636 */
637 if (test_and_set_bit(idx, cpuc->used))
638 goto try_generic;
Ingo Molnar0dff86a2008-12-23 12:28:12 +0100639
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100640 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
641 /*
642 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
643 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
644 */
645 hwc->counter_base =
646 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
Ingo Molnar241771e2008-12-03 10:39:53 +0100647 hwc->idx = idx;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100648 } else {
649 idx = hwc->idx;
650 /* Try to get the previous generic counter again */
651 if (test_and_set_bit(idx, cpuc->used)) {
652try_generic:
653 idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
654 if (idx == nr_counters_generic)
655 return -EAGAIN;
656
657 set_bit(idx, cpuc->used);
658 hwc->idx = idx;
659 }
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530660 hwc->config_base = pmc_ops->eventsel;
661 hwc->counter_base = pmc_ops->perfctr;
Ingo Molnar241771e2008-12-03 10:39:53 +0100662 }
663
664 perf_counters_lapic_init(hwc->nmi);
665
Ingo Molnareb2b8612008-12-17 09:09:13 +0100666 __pmc_generic_disable(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100667
Ingo Molnar862a1a52008-12-17 13:09:20 +0100668 cpuc->counters[idx] = counter;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100669 /*
670 * Make it visible before enabling the hw:
671 */
672 smp_wmb();
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100673
Ingo Molnaree060942008-12-13 09:00:03 +0100674 __hw_perf_counter_set_period(counter, hwc, idx);
Ingo Molnareb2b8612008-12-17 09:09:13 +0100675 __pmc_generic_enable(counter, hwc, idx);
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100676
677 return 0;
Ingo Molnar241771e2008-12-03 10:39:53 +0100678}
679
680void perf_counter_print_debug(void)
681{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100682 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
Ingo Molnar0dff86a2008-12-23 12:28:12 +0100683 struct cpu_hw_counters *cpuc;
Ingo Molnar1e125672008-12-09 12:18:18 +0100684 int cpu, idx;
685
Ingo Molnar862a1a52008-12-17 13:09:20 +0100686 if (!nr_counters_generic)
Ingo Molnar1e125672008-12-09 12:18:18 +0100687 return;
Ingo Molnar241771e2008-12-03 10:39:53 +0100688
689 local_irq_disable();
690
691 cpu = smp_processor_id();
Ingo Molnar0dff86a2008-12-23 12:28:12 +0100692 cpuc = &per_cpu(cpu_hw_counters, cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +0100693
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100694 if (intel_perfmon_version >= 2) {
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530695 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
696 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
697 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
698 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
Ingo Molnar241771e2008-12-03 10:39:53 +0100699
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530700 pr_info("\n");
701 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
702 pr_info("CPU#%d: status: %016llx\n", cpu, status);
703 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
704 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530705 }
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530706 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
Ingo Molnar241771e2008-12-03 10:39:53 +0100707
Ingo Molnar862a1a52008-12-17 13:09:20 +0100708 for (idx = 0; idx < nr_counters_generic; idx++) {
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530709 rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl);
710 rdmsrl(pmc_ops->perfctr + idx, pmc_count);
Ingo Molnar241771e2008-12-03 10:39:53 +0100711
Ingo Molnaree060942008-12-13 09:00:03 +0100712 prev_left = per_cpu(prev_left[idx], cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +0100713
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530714 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +0100715 cpu, idx, pmc_ctrl);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530716 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +0100717 cpu, idx, pmc_count);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530718 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
Ingo Molnaree060942008-12-13 09:00:03 +0100719 cpu, idx, prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +0100720 }
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100721 for (idx = 0; idx < nr_counters_fixed; idx++) {
722 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
723
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530724 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100725 cpu, idx, pmc_count);
726 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100727 local_irq_enable();
728}
729
Ingo Molnareb2b8612008-12-17 09:09:13 +0100730static void pmc_generic_disable(struct perf_counter *counter)
Ingo Molnar241771e2008-12-03 10:39:53 +0100731{
732 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
733 struct hw_perf_counter *hwc = &counter->hw;
734 unsigned int idx = hwc->idx;
735
Ingo Molnareb2b8612008-12-17 09:09:13 +0100736 __pmc_generic_disable(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100737
738 clear_bit(idx, cpuc->used);
Ingo Molnar862a1a52008-12-17 13:09:20 +0100739 cpuc->counters[idx] = NULL;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100740 /*
741 * Make sure the cleared pointer becomes visible before we
742 * (potentially) free the counter:
743 */
744 smp_wmb();
Ingo Molnar241771e2008-12-03 10:39:53 +0100745
Ingo Molnaree060942008-12-13 09:00:03 +0100746 /*
747 * Drain the remaining delta count out of a counter
748 * that we are disabling:
749 */
750 x86_perf_counter_update(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100751}
752
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100753/*
Ingo Molnaree060942008-12-13 09:00:03 +0100754 * Save and restart an expired counter. Called by NMI contexts,
755 * so it has to be careful about preempting normal counter ops:
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100756 */
Ingo Molnar241771e2008-12-03 10:39:53 +0100757static void perf_save_and_restart(struct perf_counter *counter)
758{
759 struct hw_perf_counter *hwc = &counter->hw;
760 int idx = hwc->idx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100761
Ingo Molnaree060942008-12-13 09:00:03 +0100762 x86_perf_counter_update(counter, hwc, idx);
763 __hw_perf_counter_set_period(counter, hwc, idx);
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100764
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100765 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
Ingo Molnareb2b8612008-12-17 09:09:13 +0100766 __pmc_generic_enable(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100767}
768
Ingo Molnar241771e2008-12-03 10:39:53 +0100769/*
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100770 * Maximum interrupt frequency of 100KHz per CPU
771 */
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +0530772#define PERFMON_MAX_INTERRUPTS (100000/HZ)
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100773
774/*
Ingo Molnar241771e2008-12-03 10:39:53 +0100775 * This handler is triggered by the local APIC, so the APIC IRQ handling
776 * rules apply:
777 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100778static int __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
Ingo Molnar241771e2008-12-03 10:39:53 +0100779{
780 int bit, cpu = smp_processor_id();
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100781 u64 ack, status;
Mike Galbraith1b023a92009-01-23 10:13:01 +0100782 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100783 int ret = 0;
Ingo Molnar43874d22008-12-09 12:23:59 +0100784
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100785 cpuc->throttle_ctrl = hw_perf_save_disable();
Ingo Molnar241771e2008-12-03 10:39:53 +0100786
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100787 status = hw_perf_get_status(cpuc->throttle_ctrl);
Ingo Molnar87b9cf42008-12-08 14:20:16 +0100788 if (!status)
789 goto out;
790
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100791 ret = 1;
Ingo Molnar241771e2008-12-03 10:39:53 +0100792again:
Mike Galbraithd278c482009-02-09 07:38:50 +0100793 inc_irq_stat(apic_perf_irqs);
Ingo Molnar241771e2008-12-03 10:39:53 +0100794 ack = status;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100795 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
Ingo Molnar862a1a52008-12-17 13:09:20 +0100796 struct perf_counter *counter = cpuc->counters[bit];
Ingo Molnar241771e2008-12-03 10:39:53 +0100797
798 clear_bit(bit, (unsigned long *) &status);
799 if (!counter)
800 continue;
801
802 perf_save_and_restart(counter);
Peter Zijlstra0322cd62009-03-19 20:26:19 +0100803 perf_counter_output(counter, nmi, regs);
Ingo Molnar241771e2008-12-03 10:39:53 +0100804 }
805
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100806 hw_perf_ack_status(ack);
Ingo Molnar241771e2008-12-03 10:39:53 +0100807
808 /*
809 * Repeat if there is more work to be done:
810 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100811 status = hw_perf_get_status(cpuc->throttle_ctrl);
Ingo Molnar241771e2008-12-03 10:39:53 +0100812 if (status)
813 goto again;
Ingo Molnar87b9cf42008-12-08 14:20:16 +0100814out:
Ingo Molnar241771e2008-12-03 10:39:53 +0100815 /*
Mike Galbraith1b023a92009-01-23 10:13:01 +0100816 * Restore - do not reenable when global enable is off or throttled:
Ingo Molnar241771e2008-12-03 10:39:53 +0100817 */
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100818 if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100819 hw_perf_restore(cpuc->throttle_ctrl);
820
821 return ret;
Mike Galbraith1b023a92009-01-23 10:13:01 +0100822}
823
824void perf_counter_unthrottle(void)
825{
826 struct cpu_hw_counters *cpuc;
827
828 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
829 return;
830
831 if (unlikely(!perf_counters_initialized))
832 return;
833
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100834 cpuc = &__get_cpu_var(cpu_hw_counters);
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100835 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
Mike Galbraith1b023a92009-01-23 10:13:01 +0100836 if (printk_ratelimit())
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100837 printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100838 hw_perf_restore(cpuc->throttle_ctrl);
Mike Galbraith1b023a92009-01-23 10:13:01 +0100839 }
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100840 cpuc->interrupts = 0;
Ingo Molnar241771e2008-12-03 10:39:53 +0100841}
842
843void smp_perf_counter_interrupt(struct pt_regs *regs)
844{
845 irq_enter();
Ingo Molnar241771e2008-12-03 10:39:53 +0100846 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100847 ack_APIC_irq();
Ingo Molnar241771e2008-12-03 10:39:53 +0100848 __smp_perf_counter_interrupt(regs, 0);
Ingo Molnar241771e2008-12-03 10:39:53 +0100849 irq_exit();
850}
851
Mike Galbraith3415dd92009-01-23 14:16:53 +0100852void perf_counters_lapic_init(int nmi)
Ingo Molnar241771e2008-12-03 10:39:53 +0100853{
854 u32 apic_val;
855
856 if (!perf_counters_initialized)
857 return;
858 /*
859 * Enable the performance counter vector in the APIC LVT:
860 */
861 apic_val = apic_read(APIC_LVTERR);
862
863 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
864 if (nmi)
865 apic_write(APIC_LVTPC, APIC_DM_NMI);
866 else
867 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
868 apic_write(APIC_LVTERR, apic_val);
869}
870
871static int __kprobes
872perf_counter_nmi_handler(struct notifier_block *self,
873 unsigned long cmd, void *__args)
874{
875 struct die_args *args = __args;
876 struct pt_regs *regs;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100877 int ret;
Ingo Molnar241771e2008-12-03 10:39:53 +0100878
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100879 switch (cmd) {
880 case DIE_NMI:
881 case DIE_NMI_IPI:
882 break;
883
884 default:
Ingo Molnar241771e2008-12-03 10:39:53 +0100885 return NOTIFY_DONE;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100886 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100887
888 regs = args->regs;
889
890 apic_write(APIC_LVTPC, APIC_DM_NMI);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100891 ret = __smp_perf_counter_interrupt(regs, 1);
Ingo Molnar241771e2008-12-03 10:39:53 +0100892
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100893 return ret ? NOTIFY_STOP : NOTIFY_OK;
Ingo Molnar241771e2008-12-03 10:39:53 +0100894}
895
896static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
Mike Galbraith5b75af02009-02-04 17:11:34 +0100897 .notifier_call = perf_counter_nmi_handler,
898 .next = NULL,
899 .priority = 1
Ingo Molnar241771e2008-12-03 10:39:53 +0100900};
901
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530902static struct pmc_x86_ops pmc_intel_ops = {
903 .save_disable_all = pmc_intel_save_disable_all,
904 .restore_all = pmc_intel_restore_all,
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100905 .get_status = pmc_intel_get_status,
906 .ack_status = pmc_intel_ack_status,
907 .enable = pmc_intel_enable,
908 .disable = pmc_intel_disable,
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530909 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
910 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
911 .event_map = pmc_intel_event_map,
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100912 .raw_event = pmc_intel_raw_event,
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530913 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
914};
915
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530916static struct pmc_x86_ops pmc_amd_ops = {
917 .save_disable_all = pmc_amd_save_disable_all,
918 .restore_all = pmc_amd_restore_all,
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100919 .get_status = pmc_amd_get_status,
920 .ack_status = pmc_amd_ack_status,
921 .enable = pmc_amd_enable,
922 .disable = pmc_amd_disable,
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530923 .eventsel = MSR_K7_EVNTSEL0,
924 .perfctr = MSR_K7_PERFCTR0,
925 .event_map = pmc_amd_event_map,
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100926 .raw_event = pmc_amd_raw_event,
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530927 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
928};
929
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530930static struct pmc_x86_ops *pmc_intel_init(void)
Ingo Molnar241771e2008-12-03 10:39:53 +0100931{
Ingo Molnar703e9372008-12-17 10:51:15 +0100932 union cpuid10_edx edx;
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100933 union cpuid10_eax eax;
934 unsigned int unused;
935 unsigned int ebx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100936
Ingo Molnar241771e2008-12-03 10:39:53 +0100937 /*
938 * Check whether the Architectural PerfMon supports
939 * Branch Misses Retired Event or not.
940 */
Ingo Molnar703e9372008-12-17 10:51:15 +0100941 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
Ingo Molnar241771e2008-12-03 10:39:53 +0100942 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530943 return NULL;
Ingo Molnar241771e2008-12-03 10:39:53 +0100944
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100945 intel_perfmon_version = eax.split.version_id;
946 if (intel_perfmon_version < 2)
947 return NULL;
948
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530949 pr_info("Intel Performance Monitoring support detected.\n");
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100950 pr_info("... version: %d\n", intel_perfmon_version);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530951 pr_info("... bit width: %d\n", eax.split.bit_width);
952 pr_info("... mask length: %d\n", eax.split.mask_length);
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530953
Ingo Molnar862a1a52008-12-17 13:09:20 +0100954 nr_counters_generic = eax.split.num_counters;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530955 nr_counters_fixed = edx.split.num_counters_fixed;
956 counter_value_mask = (1ULL << eax.split.bit_width) - 1;
957
958 return &pmc_intel_ops;
959}
960
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530961static struct pmc_x86_ops *pmc_amd_init(void)
962{
963 nr_counters_generic = 4;
964 nr_counters_fixed = 0;
Peter Zijlstrab5e8acf2009-03-05 20:34:21 +0100965 counter_value_mask = 0x0000FFFFFFFFFFFFULL;
966 counter_value_bits = 48;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530967
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530968 pr_info("AMD Performance Monitoring support detected.\n");
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530969
970 return &pmc_amd_ops;
971}
972
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530973void __init init_hw_perf_counters(void)
974{
975 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
976 return;
977
978 switch (boot_cpu_data.x86_vendor) {
979 case X86_VENDOR_INTEL:
980 pmc_ops = pmc_intel_init();
981 break;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530982 case X86_VENDOR_AMD:
983 pmc_ops = pmc_amd_init();
984 break;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530985 }
986 if (!pmc_ops)
987 return;
988
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530989 pr_info("... num counters: %d\n", nr_counters_generic);
Ingo Molnar862a1a52008-12-17 13:09:20 +0100990 if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
991 nr_counters_generic = X86_PMC_MAX_GENERIC;
Ingo Molnar241771e2008-12-03 10:39:53 +0100992 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
Ingo Molnar862a1a52008-12-17 13:09:20 +0100993 nr_counters_generic, X86_PMC_MAX_GENERIC);
Ingo Molnar241771e2008-12-03 10:39:53 +0100994 }
Ingo Molnar862a1a52008-12-17 13:09:20 +0100995 perf_counter_mask = (1 << nr_counters_generic) - 1;
996 perf_max_counters = nr_counters_generic;
Ingo Molnar241771e2008-12-03 10:39:53 +0100997
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530998 pr_info("... value mask: %016Lx\n", counter_value_mask);
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100999
Ingo Molnar862a1a52008-12-17 13:09:20 +01001000 if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
1001 nr_counters_fixed = X86_PMC_MAX_FIXED;
Ingo Molnar703e9372008-12-17 10:51:15 +01001002 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
Ingo Molnar862a1a52008-12-17 13:09:20 +01001003 nr_counters_fixed, X86_PMC_MAX_FIXED);
Ingo Molnar703e9372008-12-17 10:51:15 +01001004 }
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301005 pr_info("... fixed counters: %d\n", nr_counters_fixed);
Ingo Molnar241771e2008-12-03 10:39:53 +01001006
Ingo Molnar862a1a52008-12-17 13:09:20 +01001007 perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1008
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301009 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
Ingo Molnar75f224c2008-12-14 21:58:46 +01001010 perf_counters_initialized = true;
1011
Ingo Molnar241771e2008-12-03 10:39:53 +01001012 perf_counters_lapic_init(0);
1013 register_die_notifier(&perf_counter_nmi_notifier);
Ingo Molnar241771e2008-12-03 10:39:53 +01001014}
Ingo Molnar621a01e2008-12-11 12:46:46 +01001015
Ingo Molnareb2b8612008-12-17 09:09:13 +01001016static void pmc_generic_read(struct perf_counter *counter)
Ingo Molnaree060942008-12-13 09:00:03 +01001017{
1018 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1019}
1020
Ingo Molnar5c92d122008-12-11 13:21:10 +01001021static const struct hw_perf_counter_ops x86_perf_counter_ops = {
Ingo Molnar76715812008-12-17 14:20:28 +01001022 .enable = pmc_generic_enable,
1023 .disable = pmc_generic_disable,
1024 .read = pmc_generic_read,
Ingo Molnar621a01e2008-12-11 12:46:46 +01001025};
1026
Ingo Molnar5c92d122008-12-11 13:21:10 +01001027const struct hw_perf_counter_ops *
1028hw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar621a01e2008-12-11 12:46:46 +01001029{
1030 int err;
1031
1032 err = __hw_perf_counter_init(counter);
1033 if (err)
Peter Zijlstra9ea98e12009-03-30 19:07:09 +02001034 return ERR_PTR(err);
Ingo Molnar621a01e2008-12-11 12:46:46 +01001035
1036 return &x86_perf_counter_ops;
1037}
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001038
1039/*
1040 * callchain support
1041 */
1042
1043static inline
1044void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1045{
1046 if (entry->nr < MAX_STACK_DEPTH)
1047 entry->ip[entry->nr++] = ip;
1048}
1049
1050static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1051static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1052
1053
1054static void
1055backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1056{
1057 /* Ignore warnings */
1058}
1059
1060static void backtrace_warning(void *data, char *msg)
1061{
1062 /* Ignore warnings */
1063}
1064
1065static int backtrace_stack(void *data, char *name)
1066{
1067 /* Don't bother with IRQ stacks for now */
1068 return -1;
1069}
1070
1071static void backtrace_address(void *data, unsigned long addr, int reliable)
1072{
1073 struct perf_callchain_entry *entry = data;
1074
1075 if (reliable)
1076 callchain_store(entry, addr);
1077}
1078
1079static const struct stacktrace_ops backtrace_ops = {
1080 .warning = backtrace_warning,
1081 .warning_symbol = backtrace_warning_symbol,
1082 .stack = backtrace_stack,
1083 .address = backtrace_address,
1084};
1085
1086static void
1087perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1088{
1089 unsigned long bp;
1090 char *stack;
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001091 int nr = entry->nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001092
1093 callchain_store(entry, instruction_pointer(regs));
1094
1095 stack = ((char *)regs + sizeof(struct pt_regs));
1096#ifdef CONFIG_FRAME_POINTER
1097 bp = frame_pointer(regs);
1098#else
1099 bp = 0;
1100#endif
1101
1102 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001103
1104 entry->kernel = entry->nr - nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001105}
1106
1107
1108struct stack_frame {
1109 const void __user *next_fp;
1110 unsigned long return_address;
1111};
1112
1113static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1114{
1115 int ret;
1116
1117 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1118 return 0;
1119
1120 ret = 1;
1121 pagefault_disable();
1122 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1123 ret = 0;
1124 pagefault_enable();
1125
1126 return ret;
1127}
1128
1129static void
1130perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1131{
1132 struct stack_frame frame;
1133 const void __user *fp;
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001134 int nr = entry->nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001135
1136 regs = (struct pt_regs *)current->thread.sp0 - 1;
1137 fp = (void __user *)regs->bp;
1138
1139 callchain_store(entry, regs->ip);
1140
1141 while (entry->nr < MAX_STACK_DEPTH) {
1142 frame.next_fp = NULL;
1143 frame.return_address = 0;
1144
1145 if (!copy_stack_frame(fp, &frame))
1146 break;
1147
1148 if ((unsigned long)fp < user_stack_pointer(regs))
1149 break;
1150
1151 callchain_store(entry, frame.return_address);
1152 fp = frame.next_fp;
1153 }
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001154
1155 entry->user = entry->nr - nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001156}
1157
1158static void
1159perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1160{
1161 int is_user;
1162
1163 if (!regs)
1164 return;
1165
1166 is_user = user_mode(regs);
1167
1168 if (!current || current->pid == 0)
1169 return;
1170
1171 if (is_user && current->state != TASK_RUNNING)
1172 return;
1173
1174 if (!is_user)
1175 perf_callchain_kernel(regs, entry);
1176
1177 if (current->mm)
1178 perf_callchain_user(regs, entry);
1179}
1180
1181struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1182{
1183 struct perf_callchain_entry *entry;
1184
1185 if (in_nmi())
1186 entry = &__get_cpu_var(nmi_entry);
1187 else
1188 entry = &__get_cpu_var(irq_entry);
1189
1190 entry->nr = 0;
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001191 entry->hv = 0;
1192 entry->kernel = 0;
1193 entry->user = 0;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001194
1195 perf_do_callchain(regs, entry);
1196
1197 return entry;
1198}