blob: d0bb02919c63cc80eb2a145f4fde18014d8a8a25 [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
Robert Richter39d81ea2009-04-29 12:47:05 +02007 * Copyright(C) 2009 Advanced Micro Devices, Inc., Robert Richter
Ingo Molnar241771e2008-12-03 10:39:53 +01008 *
9 * For licencing details see kernel-base/COPYING
10 */
11
12#include <linux/perf_counter.h>
13#include <linux/capability.h>
14#include <linux/notifier.h>
15#include <linux/hardirq.h>
16#include <linux/kprobes.h>
Thomas Gleixner4ac13292008-12-09 21:43:39 +010017#include <linux/module.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010018#include <linux/kdebug.h>
19#include <linux/sched.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020020#include <linux/uaccess.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010021
Ingo Molnar241771e2008-12-03 10:39:53 +010022#include <asm/apic.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020023#include <asm/stacktrace.h>
Peter Zijlstra4e935e42009-03-30 19:07:16 +020024#include <asm/nmi.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010025
26static bool perf_counters_initialized __read_mostly;
27
28/*
29 * Number of (generic) HW counters:
30 */
Ingo Molnar862a1a52008-12-17 13:09:20 +010031static int nr_counters_generic __read_mostly;
32static u64 perf_counter_mask __read_mostly;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +010033static u64 counter_value_mask __read_mostly;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010034static int counter_value_bits __read_mostly;
Ingo Molnar241771e2008-12-03 10:39:53 +010035
Ingo Molnar862a1a52008-12-17 13:09:20 +010036static int nr_counters_fixed __read_mostly;
Ingo Molnar703e9372008-12-17 10:51:15 +010037
Ingo Molnar241771e2008-12-03 10:39:53 +010038struct cpu_hw_counters {
Ingo Molnar862a1a52008-12-17 13:09:20 +010039 struct perf_counter *counters[X86_PMC_IDX_MAX];
40 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Mike Galbraith4b39fd92009-01-23 14:36:16 +010041 unsigned long interrupts;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010042 u64 throttle_ctrl;
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +010043 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010044 int enabled;
Ingo Molnar241771e2008-12-03 10:39:53 +010045};
46
47/*
Robert Richter5f4ec282009-04-29 12:47:04 +020048 * struct x86_pmu - generic x86 pmu
Ingo Molnar241771e2008-12-03 10:39:53 +010049 */
Robert Richter5f4ec282009-04-29 12:47:04 +020050struct x86_pmu {
Robert Richter39d81ea2009-04-29 12:47:05 +020051 int (*handle_irq)(struct pt_regs *, int);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +053052 u64 (*save_disable_all)(void);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010053 void (*restore_all)(u64);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010054 void (*ack_status)(u64);
55 void (*enable)(int, u64);
56 void (*disable)(int, u64);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +053057 unsigned eventsel;
58 unsigned perfctr;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010059 u64 (*event_map)(int);
60 u64 (*raw_event)(u64);
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +053061 int max_events;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053062};
63
Robert Richter5f4ec282009-04-29 12:47:04 +020064static struct x86_pmu *x86_pmu __read_mostly;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053065
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010066static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
67 .enabled = 1,
68};
Ingo Molnar241771e2008-12-03 10:39:53 +010069
Ingo Molnar7bb497b2009-03-18 08:59:21 +010070static __read_mostly int intel_perfmon_version;
71
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053072/*
73 * Intel PerfMon v3. Used on Core2 and later.
74 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010075static const u64 intel_perfmon_event_map[] =
Ingo Molnar241771e2008-12-03 10:39:53 +010076{
Ingo Molnarf650a672008-12-23 12:17:29 +010077 [PERF_COUNT_CPU_CYCLES] = 0x003c,
Ingo Molnar241771e2008-12-03 10:39:53 +010078 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
79 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
80 [PERF_COUNT_CACHE_MISSES] = 0x412e,
81 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
82 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
Ingo Molnarf650a672008-12-23 12:17:29 +010083 [PERF_COUNT_BUS_CYCLES] = 0x013c,
Ingo Molnar241771e2008-12-03 10:39:53 +010084};
85
Robert Richter5f4ec282009-04-29 12:47:04 +020086static u64 intel_pmu_event_map(int event)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +053087{
88 return intel_perfmon_event_map[event];
89}
Ingo Molnar241771e2008-12-03 10:39:53 +010090
Robert Richter5f4ec282009-04-29 12:47:04 +020091static u64 intel_pmu_raw_event(u64 event)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010092{
Peter Zijlstra82bae4f82009-03-13 12:21:31 +010093#define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
94#define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
95#define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010096
97#define CORE_EVNTSEL_MASK \
98 (CORE_EVNTSEL_EVENT_MASK | \
99 CORE_EVNTSEL_UNIT_MASK | \
100 CORE_EVNTSEL_COUNTER_MASK)
101
102 return event & CORE_EVNTSEL_MASK;
103}
104
Ingo Molnar241771e2008-12-03 10:39:53 +0100105/*
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530106 * AMD Performance Monitor K7 and later.
107 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100108static const u64 amd_perfmon_event_map[] =
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530109{
110 [PERF_COUNT_CPU_CYCLES] = 0x0076,
111 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
112 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
113 [PERF_COUNT_CACHE_MISSES] = 0x0081,
114 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
115 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
116};
117
Robert Richter5f4ec282009-04-29 12:47:04 +0200118static u64 amd_pmu_event_map(int event)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530119{
120 return amd_perfmon_event_map[event];
121}
122
Robert Richter5f4ec282009-04-29 12:47:04 +0200123static u64 amd_pmu_raw_event(u64 event)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100124{
Peter Zijlstra82bae4f82009-03-13 12:21:31 +0100125#define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
126#define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
127#define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100128
129#define K7_EVNTSEL_MASK \
130 (K7_EVNTSEL_EVENT_MASK | \
131 K7_EVNTSEL_UNIT_MASK | \
132 K7_EVNTSEL_COUNTER_MASK)
133
134 return event & K7_EVNTSEL_MASK;
135}
136
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530137/*
Ingo Molnaree060942008-12-13 09:00:03 +0100138 * Propagate counter elapsed time into the generic counter.
139 * Can only be executed on the CPU where the counter is active.
140 * Returns the delta events processed.
141 */
142static void
143x86_perf_counter_update(struct perf_counter *counter,
144 struct hw_perf_counter *hwc, int idx)
145{
146 u64 prev_raw_count, new_raw_count, delta;
147
Ingo Molnaree060942008-12-13 09:00:03 +0100148 /*
149 * Careful: an NMI might modify the previous counter value.
150 *
151 * Our tactic to handle this is to first atomically read and
152 * exchange a new raw count - then add that new-prev delta
153 * count to the generic counter atomically:
154 */
155again:
156 prev_raw_count = atomic64_read(&hwc->prev_count);
157 rdmsrl(hwc->counter_base + idx, new_raw_count);
158
159 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
160 new_raw_count) != prev_raw_count)
161 goto again;
162
163 /*
164 * Now we have the new raw value and have updated the prev
165 * timestamp already. We can now calculate the elapsed delta
166 * (counter-)time and add that to the generic counter.
167 *
168 * Careful, not all hw sign-extends above the physical width
169 * of the count, so we do that by clipping the delta to 32 bits:
170 */
171 delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
Ingo Molnaree060942008-12-13 09:00:03 +0100172
173 atomic64_add(delta, &counter->count);
174 atomic64_sub(delta, &hwc->period_left);
175}
176
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200177static atomic_t num_counters;
178static DEFINE_MUTEX(pmc_reserve_mutex);
179
180static bool reserve_pmc_hardware(void)
181{
182 int i;
183
184 if (nmi_watchdog == NMI_LOCAL_APIC)
185 disable_lapic_nmi_watchdog();
186
187 for (i = 0; i < nr_counters_generic; i++) {
Robert Richter5f4ec282009-04-29 12:47:04 +0200188 if (!reserve_perfctr_nmi(x86_pmu->perfctr + i))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200189 goto perfctr_fail;
190 }
191
192 for (i = 0; i < nr_counters_generic; i++) {
Robert Richter5f4ec282009-04-29 12:47:04 +0200193 if (!reserve_evntsel_nmi(x86_pmu->eventsel + i))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200194 goto eventsel_fail;
195 }
196
197 return true;
198
199eventsel_fail:
200 for (i--; i >= 0; i--)
Robert Richter5f4ec282009-04-29 12:47:04 +0200201 release_evntsel_nmi(x86_pmu->eventsel + i);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200202
203 i = nr_counters_generic;
204
205perfctr_fail:
206 for (i--; i >= 0; i--)
Robert Richter5f4ec282009-04-29 12:47:04 +0200207 release_perfctr_nmi(x86_pmu->perfctr + i);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200208
209 if (nmi_watchdog == NMI_LOCAL_APIC)
210 enable_lapic_nmi_watchdog();
211
212 return false;
213}
214
215static void release_pmc_hardware(void)
216{
217 int i;
218
219 for (i = 0; i < nr_counters_generic; i++) {
Robert Richter5f4ec282009-04-29 12:47:04 +0200220 release_perfctr_nmi(x86_pmu->perfctr + i);
221 release_evntsel_nmi(x86_pmu->eventsel + i);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200222 }
223
224 if (nmi_watchdog == NMI_LOCAL_APIC)
225 enable_lapic_nmi_watchdog();
226}
227
228static void hw_perf_counter_destroy(struct perf_counter *counter)
229{
230 if (atomic_dec_and_mutex_lock(&num_counters, &pmc_reserve_mutex)) {
231 release_pmc_hardware();
232 mutex_unlock(&pmc_reserve_mutex);
233 }
234}
235
Ingo Molnaree060942008-12-13 09:00:03 +0100236/*
Ingo Molnar241771e2008-12-03 10:39:53 +0100237 * Setup the hardware configuration for a given hw_event_type
238 */
Ingo Molnar621a01e2008-12-11 12:46:46 +0100239static int __hw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar241771e2008-12-03 10:39:53 +0100240{
Ingo Molnar9f66a382008-12-10 12:33:23 +0100241 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar241771e2008-12-03 10:39:53 +0100242 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200243 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100244
Robert Richter39d81ea2009-04-29 12:47:05 +0200245 /* disable temporarily */
246 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
247 return -ENOSYS;
248
Ingo Molnar241771e2008-12-03 10:39:53 +0100249 if (unlikely(!perf_counters_initialized))
250 return -EINVAL;
251
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200252 err = 0;
253 if (atomic_inc_not_zero(&num_counters)) {
254 mutex_lock(&pmc_reserve_mutex);
255 if (atomic_read(&num_counters) == 0 && !reserve_pmc_hardware())
256 err = -EBUSY;
257 else
258 atomic_inc(&num_counters);
259 mutex_unlock(&pmc_reserve_mutex);
260 }
261 if (err)
262 return err;
263
Ingo Molnar241771e2008-12-03 10:39:53 +0100264 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100265 * Generate PMC IRQs:
Ingo Molnar241771e2008-12-03 10:39:53 +0100266 * (keep 'enabled' bit clear for now)
267 */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100268 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
Ingo Molnar241771e2008-12-03 10:39:53 +0100269
270 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100271 * Count user and OS events unless requested not to.
272 */
273 if (!hw_event->exclude_user)
274 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
275 if (!hw_event->exclude_kernel)
276 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
277
278 /*
279 * If privileged enough, allow NMI events:
Ingo Molnar241771e2008-12-03 10:39:53 +0100280 */
281 hwc->nmi = 0;
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100282 if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
283 hwc->nmi = 1;
Ingo Molnar241771e2008-12-03 10:39:53 +0100284
Ingo Molnar9f66a382008-12-10 12:33:23 +0100285 hwc->irq_period = hw_event->irq_period;
Ingo Molnar241771e2008-12-03 10:39:53 +0100286 /*
287 * Intel PMCs cannot be accessed sanely above 32 bit width,
288 * so we install an artificial 1<<31 period regardless of
289 * the generic counter period:
290 */
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530291 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
292 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
293 hwc->irq_period = 0x7FFFFFFF;
Ingo Molnar241771e2008-12-03 10:39:53 +0100294
Ingo Molnaree060942008-12-13 09:00:03 +0100295 atomic64_set(&hwc->period_left, hwc->irq_period);
Ingo Molnar241771e2008-12-03 10:39:53 +0100296
297 /*
Thomas Gleixnerdfa7c892008-12-08 19:35:37 +0100298 * Raw event type provide the config in the event structure
Ingo Molnar241771e2008-12-03 10:39:53 +0100299 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +0100300 if (perf_event_raw(hw_event)) {
Robert Richter5f4ec282009-04-29 12:47:04 +0200301 hwc->config |= x86_pmu->raw_event(perf_event_config(hw_event));
Ingo Molnar241771e2008-12-03 10:39:53 +0100302 } else {
Robert Richter5f4ec282009-04-29 12:47:04 +0200303 if (perf_event_id(hw_event) >= x86_pmu->max_events)
Ingo Molnar241771e2008-12-03 10:39:53 +0100304 return -EINVAL;
305 /*
306 * The generic map:
307 */
Robert Richter5f4ec282009-04-29 12:47:04 +0200308 hwc->config |= x86_pmu->event_map(perf_event_id(hw_event));
Ingo Molnar241771e2008-12-03 10:39:53 +0100309 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100310
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200311 counter->destroy = hw_perf_counter_destroy;
312
Ingo Molnar241771e2008-12-03 10:39:53 +0100313 return 0;
314}
315
Robert Richter5f4ec282009-04-29 12:47:04 +0200316static u64 intel_pmu_save_disable_all(void)
Thomas Gleixner4ac13292008-12-09 21:43:39 +0100317{
318 u64 ctrl;
319
320 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
Ingo Molnar862a1a52008-12-17 13:09:20 +0100321 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +0100322
Thomas Gleixner4ac13292008-12-09 21:43:39 +0100323 return ctrl;
324}
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530325
Robert Richter5f4ec282009-04-29 12:47:04 +0200326static u64 amd_pmu_save_disable_all(void)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530327{
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100328 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
329 int enabled, idx;
330
331 enabled = cpuc->enabled;
332 cpuc->enabled = 0;
Peter Zijlstra60b3df92009-03-13 12:21:30 +0100333 /*
334 * ensure we write the disable before we start disabling the
Robert Richter5f4ec282009-04-29 12:47:04 +0200335 * counters proper, so that amd_pmu_enable_counter() does the
336 * right thing.
Peter Zijlstra60b3df92009-03-13 12:21:30 +0100337 */
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100338 barrier();
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530339
340 for (idx = 0; idx < nr_counters_generic; idx++) {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100341 u64 val;
342
Robert Richter4295ee62009-04-29 12:47:01 +0200343 if (!test_bit(idx, cpuc->active_mask))
344 continue;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530345 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
Robert Richter4295ee62009-04-29 12:47:01 +0200346 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
347 continue;
348 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
349 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530350 }
351
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100352 return enabled;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530353}
354
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530355u64 hw_perf_save_disable(void)
356{
357 if (unlikely(!perf_counters_initialized))
358 return 0;
359
Robert Richter5f4ec282009-04-29 12:47:04 +0200360 return x86_pmu->save_disable_all();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530361}
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100362/*
363 * Exported because of ACPI idle
364 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100365EXPORT_SYMBOL_GPL(hw_perf_save_disable);
Ingo Molnar241771e2008-12-03 10:39:53 +0100366
Robert Richter5f4ec282009-04-29 12:47:04 +0200367static void intel_pmu_restore_all(u64 ctrl)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530368{
369 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
370}
371
Robert Richter5f4ec282009-04-29 12:47:04 +0200372static void amd_pmu_restore_all(u64 ctrl)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530373{
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100374 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530375 int idx;
376
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100377 cpuc->enabled = ctrl;
378 barrier();
379 if (!ctrl)
380 return;
381
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530382 for (idx = 0; idx < nr_counters_generic; idx++) {
Robert Richter4295ee62009-04-29 12:47:01 +0200383 u64 val;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100384
Robert Richter4295ee62009-04-29 12:47:01 +0200385 if (!test_bit(idx, cpuc->active_mask))
386 continue;
387 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
388 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
389 continue;
390 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
391 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530392 }
393}
394
Ingo Molnaree060942008-12-13 09:00:03 +0100395void hw_perf_restore(u64 ctrl)
396{
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +0100397 if (unlikely(!perf_counters_initialized))
398 return;
399
Robert Richter5f4ec282009-04-29 12:47:04 +0200400 x86_pmu->restore_all(ctrl);
Ingo Molnaree060942008-12-13 09:00:03 +0100401}
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100402/*
403 * Exported because of ACPI idle
404 */
Ingo Molnaree060942008-12-13 09:00:03 +0100405EXPORT_SYMBOL_GPL(hw_perf_restore);
406
Robert Richterb7f88592009-04-29 12:47:06 +0200407static inline u64 intel_pmu_get_status(u64 mask)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100408{
409 u64 status;
410
Robert Richterb7f88592009-04-29 12:47:06 +0200411 if (unlikely(!perf_counters_initialized))
412 return 0;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100413 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
414
415 return status;
416}
417
Robert Richter5f4ec282009-04-29 12:47:04 +0200418static void intel_pmu_ack_status(u64 ack)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100419{
420 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
421}
422
Robert Richter5f4ec282009-04-29 12:47:04 +0200423static void amd_pmu_ack_status(u64 ack)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100424{
425}
426
427static void hw_perf_ack_status(u64 ack)
428{
429 if (unlikely(!perf_counters_initialized))
430 return;
431
Robert Richter5f4ec282009-04-29 12:47:04 +0200432 x86_pmu->ack_status(ack);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100433}
434
Robert Richter5f4ec282009-04-29 12:47:04 +0200435static void intel_pmu_enable_counter(int idx, u64 config)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100436{
437 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
438 config | ARCH_PERFMON_EVENTSEL0_ENABLE);
439}
440
Robert Richter5f4ec282009-04-29 12:47:04 +0200441static void amd_pmu_enable_counter(int idx, u64 config)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100442{
443 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
444
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +0100445 set_bit(idx, cpuc->active_mask);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100446 if (cpuc->enabled)
447 config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
448
449 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
450}
451
452static void hw_perf_enable(int idx, u64 config)
453{
454 if (unlikely(!perf_counters_initialized))
455 return;
456
Robert Richter5f4ec282009-04-29 12:47:04 +0200457 x86_pmu->enable(idx, config);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100458}
459
Robert Richter5f4ec282009-04-29 12:47:04 +0200460static void intel_pmu_disable_counter(int idx, u64 config)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100461{
462 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
463}
464
Robert Richter5f4ec282009-04-29 12:47:04 +0200465static void amd_pmu_disable_counter(int idx, u64 config)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100466{
467 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
468
Peter Zijlstra184fe4ab2009-03-08 11:34:19 +0100469 clear_bit(idx, cpuc->active_mask);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100470 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
471
472}
473
474static void hw_perf_disable(int idx, u64 config)
475{
476 if (unlikely(!perf_counters_initialized))
477 return;
478
Robert Richter5f4ec282009-04-29 12:47:04 +0200479 x86_pmu->disable(idx, config);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100480}
481
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100482static inline void
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100483__pmc_fixed_disable(struct perf_counter *counter,
484 struct hw_perf_counter *hwc, unsigned int __idx)
485{
486 int idx = __idx - X86_PMC_IDX_FIXED;
487 u64 ctrl_val, mask;
488 int err;
489
490 mask = 0xfULL << (idx * 4);
491
492 rdmsrl(hwc->config_base, ctrl_val);
493 ctrl_val &= ~mask;
494 err = checking_wrmsrl(hwc->config_base, ctrl_val);
495}
496
497static inline void
Robert Richter4aeb0b42009-04-29 12:47:03 +0200498__x86_pmu_disable(struct perf_counter *counter,
499 struct hw_perf_counter *hwc, unsigned int idx)
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100500{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100501 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
Jaswinder Singh Rajput2b583d82008-12-27 19:15:43 +0530502 __pmc_fixed_disable(counter, hwc, idx);
503 else
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100504 hw_perf_disable(idx, hwc->config);
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100505}
506
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100507static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
Ingo Molnar241771e2008-12-03 10:39:53 +0100508
Ingo Molnaree060942008-12-13 09:00:03 +0100509/*
510 * Set the next IRQ period, based on the hwc->period_left value.
511 * To be called with the counter disabled in hw:
512 */
513static void
514__hw_perf_counter_set_period(struct perf_counter *counter,
515 struct hw_perf_counter *hwc, int idx)
Ingo Molnar241771e2008-12-03 10:39:53 +0100516{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100517 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstra595258a2009-03-13 12:21:28 +0100518 s64 period = hwc->irq_period;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100519 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100520
Ingo Molnaree060942008-12-13 09:00:03 +0100521 /*
522 * If we are way outside a reasoable range then just skip forward:
523 */
524 if (unlikely(left <= -period)) {
525 left = period;
526 atomic64_set(&hwc->period_left, left);
527 }
528
529 if (unlikely(left <= 0)) {
530 left += period;
531 atomic64_set(&hwc->period_left, left);
532 }
533
Ingo Molnaree060942008-12-13 09:00:03 +0100534 per_cpu(prev_left[idx], smp_processor_id()) = left;
535
536 /*
537 * The hw counter starts counting from this counter offset,
538 * mark it to be able to extra future deltas:
539 */
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100540 atomic64_set(&hwc->prev_count, (u64)-left);
Ingo Molnaree060942008-12-13 09:00:03 +0100541
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100542 err = checking_wrmsrl(hwc->counter_base + idx,
543 (u64)(-left) & counter_value_mask);
544}
545
546static inline void
547__pmc_fixed_enable(struct perf_counter *counter,
548 struct hw_perf_counter *hwc, unsigned int __idx)
549{
550 int idx = __idx - X86_PMC_IDX_FIXED;
551 u64 ctrl_val, bits, mask;
552 int err;
553
554 /*
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100555 * Enable IRQ generation (0x8),
556 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
557 * if requested:
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100558 */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100559 bits = 0x8ULL;
560 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
561 bits |= 0x2;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100562 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
563 bits |= 0x1;
564 bits <<= (idx * 4);
565 mask = 0xfULL << (idx * 4);
566
567 rdmsrl(hwc->config_base, ctrl_val);
568 ctrl_val &= ~mask;
569 ctrl_val |= bits;
570 err = checking_wrmsrl(hwc->config_base, ctrl_val);
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100571}
572
Ingo Molnaree060942008-12-13 09:00:03 +0100573static void
Robert Richter4aeb0b42009-04-29 12:47:03 +0200574__x86_pmu_enable(struct perf_counter *counter,
575 struct hw_perf_counter *hwc, int idx)
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100576{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100577 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
Jaswinder Singh Rajput2b583d82008-12-27 19:15:43 +0530578 __pmc_fixed_enable(counter, hwc, idx);
579 else
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100580 hw_perf_enable(idx, hwc->config);
Ingo Molnar241771e2008-12-03 10:39:53 +0100581}
582
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100583static int
584fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
Ingo Molnar862a1a52008-12-17 13:09:20 +0100585{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100586 unsigned int event;
587
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530588 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
589 return -1;
590
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100591 if (unlikely(hwc->nmi))
592 return -1;
593
594 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
595
Robert Richter5f4ec282009-04-29 12:47:04 +0200596 if (unlikely(event == x86_pmu->event_map(PERF_COUNT_INSTRUCTIONS)))
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100597 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
Robert Richter5f4ec282009-04-29 12:47:04 +0200598 if (unlikely(event == x86_pmu->event_map(PERF_COUNT_CPU_CYCLES)))
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100599 return X86_PMC_IDX_FIXED_CPU_CYCLES;
Robert Richter5f4ec282009-04-29 12:47:04 +0200600 if (unlikely(event == x86_pmu->event_map(PERF_COUNT_BUS_CYCLES)))
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100601 return X86_PMC_IDX_FIXED_BUS_CYCLES;
602
Ingo Molnar862a1a52008-12-17 13:09:20 +0100603 return -1;
604}
605
Ingo Molnaree060942008-12-13 09:00:03 +0100606/*
607 * Find a PMC slot for the freshly enabled / scheduled in counter:
608 */
Robert Richter4aeb0b42009-04-29 12:47:03 +0200609static int x86_pmu_enable(struct perf_counter *counter)
Ingo Molnar241771e2008-12-03 10:39:53 +0100610{
611 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
612 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100613 int idx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100614
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100615 idx = fixed_mode_idx(counter, hwc);
616 if (idx >= 0) {
617 /*
618 * Try to get the fixed counter, if that is already taken
619 * then try to get a generic counter:
620 */
621 if (test_and_set_bit(idx, cpuc->used))
622 goto try_generic;
Ingo Molnar0dff86a2008-12-23 12:28:12 +0100623
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100624 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
625 /*
626 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
627 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
628 */
629 hwc->counter_base =
630 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
Ingo Molnar241771e2008-12-03 10:39:53 +0100631 hwc->idx = idx;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100632 } else {
633 idx = hwc->idx;
634 /* Try to get the previous generic counter again */
635 if (test_and_set_bit(idx, cpuc->used)) {
636try_generic:
637 idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
638 if (idx == nr_counters_generic)
639 return -EAGAIN;
640
641 set_bit(idx, cpuc->used);
642 hwc->idx = idx;
643 }
Robert Richter5f4ec282009-04-29 12:47:04 +0200644 hwc->config_base = x86_pmu->eventsel;
645 hwc->counter_base = x86_pmu->perfctr;
Ingo Molnar241771e2008-12-03 10:39:53 +0100646 }
647
648 perf_counters_lapic_init(hwc->nmi);
649
Robert Richter4aeb0b42009-04-29 12:47:03 +0200650 __x86_pmu_disable(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100651
Ingo Molnar862a1a52008-12-17 13:09:20 +0100652 cpuc->counters[idx] = counter;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100653 /*
654 * Make it visible before enabling the hw:
655 */
Robert Richter527e26a2009-04-29 12:47:02 +0200656 barrier();
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100657
Ingo Molnaree060942008-12-13 09:00:03 +0100658 __hw_perf_counter_set_period(counter, hwc, idx);
Robert Richter4aeb0b42009-04-29 12:47:03 +0200659 __x86_pmu_enable(counter, hwc, idx);
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100660
661 return 0;
Ingo Molnar241771e2008-12-03 10:39:53 +0100662}
663
664void perf_counter_print_debug(void)
665{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100666 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
Ingo Molnar0dff86a2008-12-23 12:28:12 +0100667 struct cpu_hw_counters *cpuc;
Ingo Molnar1e125672008-12-09 12:18:18 +0100668 int cpu, idx;
669
Ingo Molnar862a1a52008-12-17 13:09:20 +0100670 if (!nr_counters_generic)
Ingo Molnar1e125672008-12-09 12:18:18 +0100671 return;
Ingo Molnar241771e2008-12-03 10:39:53 +0100672
673 local_irq_disable();
674
675 cpu = smp_processor_id();
Ingo Molnar0dff86a2008-12-23 12:28:12 +0100676 cpuc = &per_cpu(cpu_hw_counters, cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +0100677
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100678 if (intel_perfmon_version >= 2) {
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530679 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
680 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
681 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
682 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
Ingo Molnar241771e2008-12-03 10:39:53 +0100683
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530684 pr_info("\n");
685 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
686 pr_info("CPU#%d: status: %016llx\n", cpu, status);
687 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
688 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530689 }
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530690 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
Ingo Molnar241771e2008-12-03 10:39:53 +0100691
Ingo Molnar862a1a52008-12-17 13:09:20 +0100692 for (idx = 0; idx < nr_counters_generic; idx++) {
Robert Richter5f4ec282009-04-29 12:47:04 +0200693 rdmsrl(x86_pmu->eventsel + idx, pmc_ctrl);
694 rdmsrl(x86_pmu->perfctr + idx, pmc_count);
Ingo Molnar241771e2008-12-03 10:39:53 +0100695
Ingo Molnaree060942008-12-13 09:00:03 +0100696 prev_left = per_cpu(prev_left[idx], cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +0100697
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530698 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +0100699 cpu, idx, pmc_ctrl);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530700 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +0100701 cpu, idx, pmc_count);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530702 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
Ingo Molnaree060942008-12-13 09:00:03 +0100703 cpu, idx, prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +0100704 }
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100705 for (idx = 0; idx < nr_counters_fixed; idx++) {
706 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
707
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530708 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100709 cpu, idx, pmc_count);
710 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100711 local_irq_enable();
712}
713
Robert Richter4aeb0b42009-04-29 12:47:03 +0200714static void x86_pmu_disable(struct perf_counter *counter)
Ingo Molnar241771e2008-12-03 10:39:53 +0100715{
716 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
717 struct hw_perf_counter *hwc = &counter->hw;
718 unsigned int idx = hwc->idx;
719
Robert Richter4aeb0b42009-04-29 12:47:03 +0200720 __x86_pmu_disable(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100721
722 clear_bit(idx, cpuc->used);
Ingo Molnar862a1a52008-12-17 13:09:20 +0100723 cpuc->counters[idx] = NULL;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100724 /*
725 * Make sure the cleared pointer becomes visible before we
726 * (potentially) free the counter:
727 */
Robert Richter527e26a2009-04-29 12:47:02 +0200728 barrier();
Ingo Molnar241771e2008-12-03 10:39:53 +0100729
Ingo Molnaree060942008-12-13 09:00:03 +0100730 /*
731 * Drain the remaining delta count out of a counter
732 * that we are disabling:
733 */
734 x86_perf_counter_update(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100735}
736
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100737/*
Ingo Molnaree060942008-12-13 09:00:03 +0100738 * Save and restart an expired counter. Called by NMI contexts,
739 * so it has to be careful about preempting normal counter ops:
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100740 */
Ingo Molnar241771e2008-12-03 10:39:53 +0100741static void perf_save_and_restart(struct perf_counter *counter)
742{
743 struct hw_perf_counter *hwc = &counter->hw;
744 int idx = hwc->idx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100745
Ingo Molnaree060942008-12-13 09:00:03 +0100746 x86_perf_counter_update(counter, hwc, idx);
747 __hw_perf_counter_set_period(counter, hwc, idx);
Ingo Molnar7e2ae342008-12-09 11:40:46 +0100748
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100749 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
Robert Richter4aeb0b42009-04-29 12:47:03 +0200750 __x86_pmu_enable(counter, hwc, idx);
Ingo Molnar241771e2008-12-03 10:39:53 +0100751}
752
Ingo Molnar241771e2008-12-03 10:39:53 +0100753/*
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100754 * Maximum interrupt frequency of 100KHz per CPU
755 */
Jaswinder Singh Rajput169e41e2009-02-28 18:37:49 +0530756#define PERFMON_MAX_INTERRUPTS (100000/HZ)
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100757
758/*
Ingo Molnar241771e2008-12-03 10:39:53 +0100759 * This handler is triggered by the local APIC, so the APIC IRQ handling
760 * rules apply:
761 */
Robert Richter39d81ea2009-04-29 12:47:05 +0200762static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
Ingo Molnar241771e2008-12-03 10:39:53 +0100763{
764 int bit, cpu = smp_processor_id();
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100765 u64 ack, status;
Mike Galbraith1b023a92009-01-23 10:13:01 +0100766 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100767 int ret = 0;
Ingo Molnar43874d22008-12-09 12:23:59 +0100768
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100769 cpuc->throttle_ctrl = hw_perf_save_disable();
Ingo Molnar241771e2008-12-03 10:39:53 +0100770
Robert Richterb7f88592009-04-29 12:47:06 +0200771 status = intel_pmu_get_status(cpuc->throttle_ctrl);
Ingo Molnar87b9cf42008-12-08 14:20:16 +0100772 if (!status)
773 goto out;
774
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100775 ret = 1;
Ingo Molnar241771e2008-12-03 10:39:53 +0100776again:
Mike Galbraithd278c482009-02-09 07:38:50 +0100777 inc_irq_stat(apic_perf_irqs);
Ingo Molnar241771e2008-12-03 10:39:53 +0100778 ack = status;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100779 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
Ingo Molnar862a1a52008-12-17 13:09:20 +0100780 struct perf_counter *counter = cpuc->counters[bit];
Ingo Molnar241771e2008-12-03 10:39:53 +0100781
782 clear_bit(bit, (unsigned long *) &status);
783 if (!counter)
784 continue;
785
786 perf_save_and_restart(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200787 if (perf_counter_overflow(counter, nmi, regs, 0))
Robert Richter4aeb0b42009-04-29 12:47:03 +0200788 __x86_pmu_disable(counter, &counter->hw, bit);
Ingo Molnar241771e2008-12-03 10:39:53 +0100789 }
790
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100791 hw_perf_ack_status(ack);
Ingo Molnar241771e2008-12-03 10:39:53 +0100792
793 /*
794 * Repeat if there is more work to be done:
795 */
Robert Richterb7f88592009-04-29 12:47:06 +0200796 status = intel_pmu_get_status(cpuc->throttle_ctrl);
Ingo Molnar241771e2008-12-03 10:39:53 +0100797 if (status)
798 goto again;
Ingo Molnar87b9cf42008-12-08 14:20:16 +0100799out:
Ingo Molnar241771e2008-12-03 10:39:53 +0100800 /*
Mike Galbraith1b023a92009-01-23 10:13:01 +0100801 * Restore - do not reenable when global enable is off or throttled:
Ingo Molnar241771e2008-12-03 10:39:53 +0100802 */
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100803 if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100804 hw_perf_restore(cpuc->throttle_ctrl);
805
806 return ret;
Mike Galbraith1b023a92009-01-23 10:13:01 +0100807}
808
Robert Richter39d81ea2009-04-29 12:47:05 +0200809static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi) { return 0; }
810
Mike Galbraith1b023a92009-01-23 10:13:01 +0100811void perf_counter_unthrottle(void)
812{
813 struct cpu_hw_counters *cpuc;
814
815 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
816 return;
817
818 if (unlikely(!perf_counters_initialized))
819 return;
820
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100821 cpuc = &__get_cpu_var(cpu_hw_counters);
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100822 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
Mike Galbraith1b023a92009-01-23 10:13:01 +0100823 if (printk_ratelimit())
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100824 printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100825 hw_perf_restore(cpuc->throttle_ctrl);
Mike Galbraith1b023a92009-01-23 10:13:01 +0100826 }
Mike Galbraith4b39fd92009-01-23 14:36:16 +0100827 cpuc->interrupts = 0;
Ingo Molnar241771e2008-12-03 10:39:53 +0100828}
829
830void smp_perf_counter_interrupt(struct pt_regs *regs)
831{
832 irq_enter();
Ingo Molnar241771e2008-12-03 10:39:53 +0100833 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100834 ack_APIC_irq();
Robert Richter39d81ea2009-04-29 12:47:05 +0200835 x86_pmu->handle_irq(regs, 0);
Ingo Molnar241771e2008-12-03 10:39:53 +0100836 irq_exit();
837}
838
Peter Zijlstrab6276f32009-04-06 11:45:03 +0200839void smp_perf_pending_interrupt(struct pt_regs *regs)
840{
841 irq_enter();
842 ack_APIC_irq();
843 inc_irq_stat(apic_pending_irqs);
844 perf_counter_do_pending();
845 irq_exit();
846}
847
848void set_perf_counter_pending(void)
849{
850 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
851}
852
Mike Galbraith3415dd92009-01-23 14:16:53 +0100853void perf_counters_lapic_init(int nmi)
Ingo Molnar241771e2008-12-03 10:39:53 +0100854{
855 u32 apic_val;
856
857 if (!perf_counters_initialized)
858 return;
859 /*
860 * Enable the performance counter vector in the APIC LVT:
861 */
862 apic_val = apic_read(APIC_LVTERR);
863
864 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
865 if (nmi)
866 apic_write(APIC_LVTPC, APIC_DM_NMI);
867 else
868 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
869 apic_write(APIC_LVTERR, apic_val);
870}
871
872static int __kprobes
873perf_counter_nmi_handler(struct notifier_block *self,
874 unsigned long cmd, void *__args)
875{
876 struct die_args *args = __args;
877 struct pt_regs *regs;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100878 int ret;
Ingo Molnar241771e2008-12-03 10:39:53 +0100879
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100880 switch (cmd) {
881 case DIE_NMI:
882 case DIE_NMI_IPI:
883 break;
884
885 default:
Ingo Molnar241771e2008-12-03 10:39:53 +0100886 return NOTIFY_DONE;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100887 }
Ingo Molnar241771e2008-12-03 10:39:53 +0100888
889 regs = args->regs;
890
891 apic_write(APIC_LVTPC, APIC_DM_NMI);
Robert Richter39d81ea2009-04-29 12:47:05 +0200892 ret = x86_pmu->handle_irq(regs, 1);
Ingo Molnar241771e2008-12-03 10:39:53 +0100893
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100894 return ret ? NOTIFY_STOP : NOTIFY_OK;
Ingo Molnar241771e2008-12-03 10:39:53 +0100895}
896
897static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
Mike Galbraith5b75af02009-02-04 17:11:34 +0100898 .notifier_call = perf_counter_nmi_handler,
899 .next = NULL,
900 .priority = 1
Ingo Molnar241771e2008-12-03 10:39:53 +0100901};
902
Robert Richter5f4ec282009-04-29 12:47:04 +0200903static struct x86_pmu intel_pmu = {
Robert Richter39d81ea2009-04-29 12:47:05 +0200904 .handle_irq = intel_pmu_handle_irq,
Robert Richter5f4ec282009-04-29 12:47:04 +0200905 .save_disable_all = intel_pmu_save_disable_all,
906 .restore_all = intel_pmu_restore_all,
Robert Richter5f4ec282009-04-29 12:47:04 +0200907 .ack_status = intel_pmu_ack_status,
908 .enable = intel_pmu_enable_counter,
909 .disable = intel_pmu_disable_counter,
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530910 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
911 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
Robert Richter5f4ec282009-04-29 12:47:04 +0200912 .event_map = intel_pmu_event_map,
913 .raw_event = intel_pmu_raw_event,
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530914 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
915};
916
Robert Richter5f4ec282009-04-29 12:47:04 +0200917static struct x86_pmu amd_pmu = {
Robert Richter39d81ea2009-04-29 12:47:05 +0200918 .handle_irq = amd_pmu_handle_irq,
Robert Richter5f4ec282009-04-29 12:47:04 +0200919 .save_disable_all = amd_pmu_save_disable_all,
920 .restore_all = amd_pmu_restore_all,
Robert Richter5f4ec282009-04-29 12:47:04 +0200921 .ack_status = amd_pmu_ack_status,
922 .enable = amd_pmu_enable_counter,
923 .disable = amd_pmu_disable_counter,
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530924 .eventsel = MSR_K7_EVNTSEL0,
925 .perfctr = MSR_K7_PERFCTR0,
Robert Richter5f4ec282009-04-29 12:47:04 +0200926 .event_map = amd_pmu_event_map,
927 .raw_event = amd_pmu_raw_event,
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530928 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
929};
930
Robert Richter5f4ec282009-04-29 12:47:04 +0200931static struct x86_pmu *intel_pmu_init(void)
Ingo Molnar241771e2008-12-03 10:39:53 +0100932{
Ingo Molnar703e9372008-12-17 10:51:15 +0100933 union cpuid10_edx edx;
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100934 union cpuid10_eax eax;
935 unsigned int unused;
936 unsigned int ebx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100937
Robert Richterda1a7762009-04-29 12:46:58 +0200938 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
939 return NULL;
940
Ingo Molnar241771e2008-12-03 10:39:53 +0100941 /*
942 * Check whether the Architectural PerfMon supports
943 * Branch Misses Retired Event or not.
944 */
Ingo Molnar703e9372008-12-17 10:51:15 +0100945 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
Ingo Molnar241771e2008-12-03 10:39:53 +0100946 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530947 return NULL;
Ingo Molnar241771e2008-12-03 10:39:53 +0100948
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100949 intel_perfmon_version = eax.split.version_id;
950 if (intel_perfmon_version < 2)
951 return NULL;
952
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530953 pr_info("Intel Performance Monitoring support detected.\n");
Ingo Molnar7bb497b2009-03-18 08:59:21 +0100954 pr_info("... version: %d\n", intel_perfmon_version);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530955 pr_info("... bit width: %d\n", eax.split.bit_width);
956 pr_info("... mask length: %d\n", eax.split.mask_length);
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530957
Ingo Molnar862a1a52008-12-17 13:09:20 +0100958 nr_counters_generic = eax.split.num_counters;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530959 nr_counters_fixed = edx.split.num_counters_fixed;
960 counter_value_mask = (1ULL << eax.split.bit_width) - 1;
961
Robert Richter5f4ec282009-04-29 12:47:04 +0200962 return &intel_pmu;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530963}
964
Robert Richter5f4ec282009-04-29 12:47:04 +0200965static struct x86_pmu *amd_pmu_init(void)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530966{
967 nr_counters_generic = 4;
968 nr_counters_fixed = 0;
Peter Zijlstrab5e8acf2009-03-05 20:34:21 +0100969 counter_value_mask = 0x0000FFFFFFFFFFFFULL;
970 counter_value_bits = 48;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530971
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530972 pr_info("AMD Performance Monitoring support detected.\n");
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530973
Robert Richter5f4ec282009-04-29 12:47:04 +0200974 return &amd_pmu;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530975}
976
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530977void __init init_hw_perf_counters(void)
978{
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530979 switch (boot_cpu_data.x86_vendor) {
980 case X86_VENDOR_INTEL:
Robert Richter5f4ec282009-04-29 12:47:04 +0200981 x86_pmu = intel_pmu_init();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530982 break;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530983 case X86_VENDOR_AMD:
Robert Richter5f4ec282009-04-29 12:47:04 +0200984 x86_pmu = amd_pmu_init();
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530985 break;
Robert Richter41389602009-04-29 12:47:00 +0200986 default:
987 return;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530988 }
Robert Richter5f4ec282009-04-29 12:47:04 +0200989 if (!x86_pmu)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530990 return;
991
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +0530992 pr_info("... num counters: %d\n", nr_counters_generic);
Ingo Molnar862a1a52008-12-17 13:09:20 +0100993 if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
994 nr_counters_generic = X86_PMC_MAX_GENERIC;
Ingo Molnar241771e2008-12-03 10:39:53 +0100995 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
Ingo Molnar862a1a52008-12-17 13:09:20 +0100996 nr_counters_generic, X86_PMC_MAX_GENERIC);
Ingo Molnar241771e2008-12-03 10:39:53 +0100997 }
Ingo Molnar862a1a52008-12-17 13:09:20 +0100998 perf_counter_mask = (1 << nr_counters_generic) - 1;
999 perf_max_counters = nr_counters_generic;
Ingo Molnar241771e2008-12-03 10:39:53 +01001000
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301001 pr_info("... value mask: %016Lx\n", counter_value_mask);
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001002
Ingo Molnar862a1a52008-12-17 13:09:20 +01001003 if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
1004 nr_counters_fixed = X86_PMC_MAX_FIXED;
Ingo Molnar703e9372008-12-17 10:51:15 +01001005 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
Ingo Molnar862a1a52008-12-17 13:09:20 +01001006 nr_counters_fixed, X86_PMC_MAX_FIXED);
Ingo Molnar703e9372008-12-17 10:51:15 +01001007 }
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301008 pr_info("... fixed counters: %d\n", nr_counters_fixed);
Ingo Molnar241771e2008-12-03 10:39:53 +01001009
Ingo Molnar862a1a52008-12-17 13:09:20 +01001010 perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1011
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301012 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
Ingo Molnar75f224c2008-12-14 21:58:46 +01001013 perf_counters_initialized = true;
1014
Ingo Molnar241771e2008-12-03 10:39:53 +01001015 perf_counters_lapic_init(0);
1016 register_die_notifier(&perf_counter_nmi_notifier);
Ingo Molnar241771e2008-12-03 10:39:53 +01001017}
Ingo Molnar621a01e2008-12-11 12:46:46 +01001018
Robert Richter4aeb0b42009-04-29 12:47:03 +02001019static void x86_pmu_read(struct perf_counter *counter)
Ingo Molnaree060942008-12-13 09:00:03 +01001020{
1021 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1022}
1023
Robert Richter4aeb0b42009-04-29 12:47:03 +02001024static const struct pmu pmu = {
1025 .enable = x86_pmu_enable,
1026 .disable = x86_pmu_disable,
1027 .read = x86_pmu_read,
Ingo Molnar621a01e2008-12-11 12:46:46 +01001028};
1029
Robert Richter4aeb0b42009-04-29 12:47:03 +02001030const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar621a01e2008-12-11 12:46:46 +01001031{
1032 int err;
1033
1034 err = __hw_perf_counter_init(counter);
1035 if (err)
Peter Zijlstra9ea98e12009-03-30 19:07:09 +02001036 return ERR_PTR(err);
Ingo Molnar621a01e2008-12-11 12:46:46 +01001037
Robert Richter4aeb0b42009-04-29 12:47:03 +02001038 return &pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001039}
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001040
1041/*
1042 * callchain support
1043 */
1044
1045static inline
1046void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1047{
1048 if (entry->nr < MAX_STACK_DEPTH)
1049 entry->ip[entry->nr++] = ip;
1050}
1051
1052static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1053static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1054
1055
1056static void
1057backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1058{
1059 /* Ignore warnings */
1060}
1061
1062static void backtrace_warning(void *data, char *msg)
1063{
1064 /* Ignore warnings */
1065}
1066
1067static int backtrace_stack(void *data, char *name)
1068{
1069 /* Don't bother with IRQ stacks for now */
1070 return -1;
1071}
1072
1073static void backtrace_address(void *data, unsigned long addr, int reliable)
1074{
1075 struct perf_callchain_entry *entry = data;
1076
1077 if (reliable)
1078 callchain_store(entry, addr);
1079}
1080
1081static const struct stacktrace_ops backtrace_ops = {
1082 .warning = backtrace_warning,
1083 .warning_symbol = backtrace_warning_symbol,
1084 .stack = backtrace_stack,
1085 .address = backtrace_address,
1086};
1087
1088static void
1089perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1090{
1091 unsigned long bp;
1092 char *stack;
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001093 int nr = entry->nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001094
1095 callchain_store(entry, instruction_pointer(regs));
1096
1097 stack = ((char *)regs + sizeof(struct pt_regs));
1098#ifdef CONFIG_FRAME_POINTER
1099 bp = frame_pointer(regs);
1100#else
1101 bp = 0;
1102#endif
1103
1104 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001105
1106 entry->kernel = entry->nr - nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001107}
1108
1109
1110struct stack_frame {
1111 const void __user *next_fp;
1112 unsigned long return_address;
1113};
1114
1115static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1116{
1117 int ret;
1118
1119 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1120 return 0;
1121
1122 ret = 1;
1123 pagefault_disable();
1124 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1125 ret = 0;
1126 pagefault_enable();
1127
1128 return ret;
1129}
1130
1131static void
1132perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1133{
1134 struct stack_frame frame;
1135 const void __user *fp;
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001136 int nr = entry->nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001137
1138 regs = (struct pt_regs *)current->thread.sp0 - 1;
1139 fp = (void __user *)regs->bp;
1140
1141 callchain_store(entry, regs->ip);
1142
1143 while (entry->nr < MAX_STACK_DEPTH) {
1144 frame.next_fp = NULL;
1145 frame.return_address = 0;
1146
1147 if (!copy_stack_frame(fp, &frame))
1148 break;
1149
1150 if ((unsigned long)fp < user_stack_pointer(regs))
1151 break;
1152
1153 callchain_store(entry, frame.return_address);
1154 fp = frame.next_fp;
1155 }
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001156
1157 entry->user = entry->nr - nr;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001158}
1159
1160static void
1161perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1162{
1163 int is_user;
1164
1165 if (!regs)
1166 return;
1167
1168 is_user = user_mode(regs);
1169
1170 if (!current || current->pid == 0)
1171 return;
1172
1173 if (is_user && current->state != TASK_RUNNING)
1174 return;
1175
1176 if (!is_user)
1177 perf_callchain_kernel(regs, entry);
1178
1179 if (current->mm)
1180 perf_callchain_user(regs, entry);
1181}
1182
1183struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1184{
1185 struct perf_callchain_entry *entry;
1186
1187 if (in_nmi())
1188 entry = &__get_cpu_var(nmi_entry);
1189 else
1190 entry = &__get_cpu_var(irq_entry);
1191
1192 entry->nr = 0;
Peter Zijlstra5872bdb82009-04-02 11:12:03 +02001193 entry->hv = 0;
1194 entry->kernel = 0;
1195 entry->user = 0;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001196
1197 perf_do_callchain(regs, entry);
1198
1199 return entry;
1200}