]>
Commit | Line | Data |
---|---|---|
241771ef IM |
1 | /* |
2 | * Performance counter x86 architecture code | |
3 | * | |
98144511 IM |
4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar | |
6 | * Copyright (C) 2009 Jaswinder Singh Rajput | |
7 | * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter | |
8 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> | |
241771ef IM |
9 | * |
10 | * For licencing details see kernel-base/COPYING | |
11 | */ | |
12 | ||
13 | #include <linux/perf_counter.h> | |
14 | #include <linux/capability.h> | |
15 | #include <linux/notifier.h> | |
16 | #include <linux/hardirq.h> | |
17 | #include <linux/kprobes.h> | |
4ac13294 | 18 | #include <linux/module.h> |
241771ef IM |
19 | #include <linux/kdebug.h> |
20 | #include <linux/sched.h> | |
d7d59fb3 | 21 | #include <linux/uaccess.h> |
241771ef | 22 | |
241771ef | 23 | #include <asm/apic.h> |
d7d59fb3 | 24 | #include <asm/stacktrace.h> |
4e935e47 | 25 | #include <asm/nmi.h> |
241771ef | 26 | |
862a1a5f | 27 | static u64 perf_counter_mask __read_mostly; |
703e937c | 28 | |
241771ef | 29 | struct cpu_hw_counters { |
862a1a5f | 30 | struct perf_counter *counters[X86_PMC_IDX_MAX]; |
43f6201a RR |
31 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
32 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
4b39fd96 | 33 | unsigned long interrupts; |
b0f3f28e | 34 | u64 throttle_ctrl; |
b0f3f28e | 35 | int enabled; |
241771ef IM |
36 | }; |
37 | ||
38 | /* | |
5f4ec28f | 39 | * struct x86_pmu - generic x86 pmu |
241771ef | 40 | */ |
5f4ec28f | 41 | struct x86_pmu { |
faa28ae0 RR |
42 | const char *name; |
43 | int version; | |
39d81eab | 44 | int (*handle_irq)(struct pt_regs *, int); |
169e41eb | 45 | u64 (*save_disable_all)(void); |
b0f3f28e | 46 | void (*restore_all)(u64); |
7c90cc45 | 47 | void (*enable)(struct hw_perf_counter *, int); |
d4369891 | 48 | void (*disable)(struct hw_perf_counter *, int); |
169e41eb JSR |
49 | unsigned eventsel; |
50 | unsigned perfctr; | |
b0f3f28e PZ |
51 | u64 (*event_map)(int); |
52 | u64 (*raw_event)(u64); | |
169e41eb | 53 | int max_events; |
0933e5c6 RR |
54 | int num_counters; |
55 | int num_counters_fixed; | |
56 | int counter_bits; | |
57 | u64 counter_mask; | |
c619b8ff | 58 | u64 max_period; |
b56a3802 JSR |
59 | }; |
60 | ||
4a06bd85 | 61 | static struct x86_pmu x86_pmu __read_mostly; |
b56a3802 | 62 | |
b0f3f28e PZ |
63 | static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = { |
64 | .enabled = 1, | |
65 | }; | |
241771ef | 66 | |
b56a3802 JSR |
67 | /* |
68 | * Intel PerfMon v3. Used on Core2 and later. | |
69 | */ | |
b0f3f28e | 70 | static const u64 intel_perfmon_event_map[] = |
241771ef | 71 | { |
f650a672 | 72 | [PERF_COUNT_CPU_CYCLES] = 0x003c, |
241771ef IM |
73 | [PERF_COUNT_INSTRUCTIONS] = 0x00c0, |
74 | [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e, | |
75 | [PERF_COUNT_CACHE_MISSES] = 0x412e, | |
76 | [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4, | |
77 | [PERF_COUNT_BRANCH_MISSES] = 0x00c5, | |
f650a672 | 78 | [PERF_COUNT_BUS_CYCLES] = 0x013c, |
241771ef IM |
79 | }; |
80 | ||
5f4ec28f | 81 | static u64 intel_pmu_event_map(int event) |
b56a3802 JSR |
82 | { |
83 | return intel_perfmon_event_map[event]; | |
84 | } | |
241771ef | 85 | |
5f4ec28f | 86 | static u64 intel_pmu_raw_event(u64 event) |
b0f3f28e | 87 | { |
82bae4f8 PZ |
88 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL |
89 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL | |
90 | #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL | |
b0f3f28e PZ |
91 | |
92 | #define CORE_EVNTSEL_MASK \ | |
93 | (CORE_EVNTSEL_EVENT_MASK | \ | |
94 | CORE_EVNTSEL_UNIT_MASK | \ | |
95 | CORE_EVNTSEL_COUNTER_MASK) | |
96 | ||
97 | return event & CORE_EVNTSEL_MASK; | |
98 | } | |
99 | ||
f87ad35d JSR |
100 | /* |
101 | * AMD Performance Monitor K7 and later. | |
102 | */ | |
b0f3f28e | 103 | static const u64 amd_perfmon_event_map[] = |
f87ad35d JSR |
104 | { |
105 | [PERF_COUNT_CPU_CYCLES] = 0x0076, | |
106 | [PERF_COUNT_INSTRUCTIONS] = 0x00c0, | |
107 | [PERF_COUNT_CACHE_REFERENCES] = 0x0080, | |
108 | [PERF_COUNT_CACHE_MISSES] = 0x0081, | |
109 | [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4, | |
110 | [PERF_COUNT_BRANCH_MISSES] = 0x00c5, | |
111 | }; | |
112 | ||
5f4ec28f | 113 | static u64 amd_pmu_event_map(int event) |
f87ad35d JSR |
114 | { |
115 | return amd_perfmon_event_map[event]; | |
116 | } | |
117 | ||
5f4ec28f | 118 | static u64 amd_pmu_raw_event(u64 event) |
b0f3f28e | 119 | { |
82bae4f8 PZ |
120 | #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL |
121 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL | |
122 | #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL | |
b0f3f28e PZ |
123 | |
124 | #define K7_EVNTSEL_MASK \ | |
125 | (K7_EVNTSEL_EVENT_MASK | \ | |
126 | K7_EVNTSEL_UNIT_MASK | \ | |
127 | K7_EVNTSEL_COUNTER_MASK) | |
128 | ||
129 | return event & K7_EVNTSEL_MASK; | |
130 | } | |
131 | ||
ee06094f IM |
132 | /* |
133 | * Propagate counter elapsed time into the generic counter. | |
134 | * Can only be executed on the CPU where the counter is active. | |
135 | * Returns the delta events processed. | |
136 | */ | |
4b7bfd0d | 137 | static u64 |
ee06094f IM |
138 | x86_perf_counter_update(struct perf_counter *counter, |
139 | struct hw_perf_counter *hwc, int idx) | |
140 | { | |
ec3232bd PZ |
141 | int shift = 64 - x86_pmu.counter_bits; |
142 | u64 prev_raw_count, new_raw_count; | |
143 | s64 delta; | |
ee06094f | 144 | |
ee06094f IM |
145 | /* |
146 | * Careful: an NMI might modify the previous counter value. | |
147 | * | |
148 | * Our tactic to handle this is to first atomically read and | |
149 | * exchange a new raw count - then add that new-prev delta | |
150 | * count to the generic counter atomically: | |
151 | */ | |
152 | again: | |
153 | prev_raw_count = atomic64_read(&hwc->prev_count); | |
154 | rdmsrl(hwc->counter_base + idx, new_raw_count); | |
155 | ||
156 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, | |
157 | new_raw_count) != prev_raw_count) | |
158 | goto again; | |
159 | ||
160 | /* | |
161 | * Now we have the new raw value and have updated the prev | |
162 | * timestamp already. We can now calculate the elapsed delta | |
163 | * (counter-)time and add that to the generic counter. | |
164 | * | |
165 | * Careful, not all hw sign-extends above the physical width | |
ec3232bd | 166 | * of the count. |
ee06094f | 167 | */ |
ec3232bd PZ |
168 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
169 | delta >>= shift; | |
ee06094f IM |
170 | |
171 | atomic64_add(delta, &counter->count); | |
172 | atomic64_sub(delta, &hwc->period_left); | |
4b7bfd0d RR |
173 | |
174 | return new_raw_count; | |
ee06094f IM |
175 | } |
176 | ||
ba77813a | 177 | static atomic_t active_counters; |
4e935e47 PZ |
178 | static DEFINE_MUTEX(pmc_reserve_mutex); |
179 | ||
180 | static bool reserve_pmc_hardware(void) | |
181 | { | |
182 | int i; | |
183 | ||
184 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
185 | disable_lapic_nmi_watchdog(); | |
186 | ||
0933e5c6 | 187 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 | 188 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
4e935e47 PZ |
189 | goto perfctr_fail; |
190 | } | |
191 | ||
0933e5c6 | 192 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 | 193 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
4e935e47 PZ |
194 | goto eventsel_fail; |
195 | } | |
196 | ||
197 | return true; | |
198 | ||
199 | eventsel_fail: | |
200 | for (i--; i >= 0; i--) | |
4a06bd85 | 201 | release_evntsel_nmi(x86_pmu.eventsel + i); |
4e935e47 | 202 | |
0933e5c6 | 203 | i = x86_pmu.num_counters; |
4e935e47 PZ |
204 | |
205 | perfctr_fail: | |
206 | for (i--; i >= 0; i--) | |
4a06bd85 | 207 | release_perfctr_nmi(x86_pmu.perfctr + i); |
4e935e47 PZ |
208 | |
209 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
210 | enable_lapic_nmi_watchdog(); | |
211 | ||
212 | return false; | |
213 | } | |
214 | ||
215 | static void release_pmc_hardware(void) | |
216 | { | |
217 | int i; | |
218 | ||
0933e5c6 | 219 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 RR |
220 | release_perfctr_nmi(x86_pmu.perfctr + i); |
221 | release_evntsel_nmi(x86_pmu.eventsel + i); | |
4e935e47 PZ |
222 | } |
223 | ||
224 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
225 | enable_lapic_nmi_watchdog(); | |
226 | } | |
227 | ||
228 | static void hw_perf_counter_destroy(struct perf_counter *counter) | |
229 | { | |
ba77813a | 230 | if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) { |
4e935e47 PZ |
231 | release_pmc_hardware(); |
232 | mutex_unlock(&pmc_reserve_mutex); | |
233 | } | |
234 | } | |
235 | ||
85cf9dba RR |
236 | static inline int x86_pmu_initialized(void) |
237 | { | |
238 | return x86_pmu.handle_irq != NULL; | |
239 | } | |
240 | ||
241771ef IM |
241 | /* |
242 | * Setup the hardware configuration for a given hw_event_type | |
243 | */ | |
621a01ea | 244 | static int __hw_perf_counter_init(struct perf_counter *counter) |
241771ef | 245 | { |
9f66a381 | 246 | struct perf_counter_hw_event *hw_event = &counter->hw_event; |
241771ef | 247 | struct hw_perf_counter *hwc = &counter->hw; |
4e935e47 | 248 | int err; |
241771ef | 249 | |
85cf9dba RR |
250 | if (!x86_pmu_initialized()) |
251 | return -ENODEV; | |
241771ef | 252 | |
4e935e47 | 253 | err = 0; |
ba77813a | 254 | if (!atomic_inc_not_zero(&active_counters)) { |
4e935e47 | 255 | mutex_lock(&pmc_reserve_mutex); |
ba77813a | 256 | if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware()) |
4e935e47 PZ |
257 | err = -EBUSY; |
258 | else | |
ba77813a | 259 | atomic_inc(&active_counters); |
4e935e47 PZ |
260 | mutex_unlock(&pmc_reserve_mutex); |
261 | } | |
262 | if (err) | |
263 | return err; | |
264 | ||
241771ef | 265 | /* |
0475f9ea | 266 | * Generate PMC IRQs: |
241771ef IM |
267 | * (keep 'enabled' bit clear for now) |
268 | */ | |
0475f9ea | 269 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
241771ef IM |
270 | |
271 | /* | |
0475f9ea | 272 | * Count user and OS events unless requested not to. |
241771ef | 273 | */ |
0475f9ea PM |
274 | if (!hw_event->exclude_user) |
275 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; | |
276 | if (!hw_event->exclude_kernel) | |
241771ef | 277 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
0475f9ea PM |
278 | |
279 | /* | |
280 | * If privileged enough, allow NMI events: | |
281 | */ | |
282 | hwc->nmi = 0; | |
a026dfec PZ |
283 | if (hw_event->nmi) { |
284 | if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN)) | |
285 | return -EACCES; | |
0475f9ea | 286 | hwc->nmi = 1; |
a026dfec | 287 | } |
241771ef | 288 | |
ba77813a | 289 | hwc->irq_period = hw_event->irq_period; |
c619b8ff RR |
290 | if ((s64)hwc->irq_period <= 0 || hwc->irq_period > x86_pmu.max_period) |
291 | hwc->irq_period = x86_pmu.max_period; | |
241771ef | 292 | |
ee06094f | 293 | atomic64_set(&hwc->period_left, hwc->irq_period); |
241771ef IM |
294 | |
295 | /* | |
dfa7c899 | 296 | * Raw event type provide the config in the event structure |
241771ef | 297 | */ |
f4a2deb4 | 298 | if (perf_event_raw(hw_event)) { |
4a06bd85 | 299 | hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event)); |
241771ef | 300 | } else { |
4a06bd85 | 301 | if (perf_event_id(hw_event) >= x86_pmu.max_events) |
241771ef IM |
302 | return -EINVAL; |
303 | /* | |
304 | * The generic map: | |
305 | */ | |
4a06bd85 | 306 | hwc->config |= x86_pmu.event_map(perf_event_id(hw_event)); |
241771ef | 307 | } |
241771ef | 308 | |
4e935e47 PZ |
309 | counter->destroy = hw_perf_counter_destroy; |
310 | ||
241771ef IM |
311 | return 0; |
312 | } | |
313 | ||
5f4ec28f | 314 | static u64 intel_pmu_save_disable_all(void) |
4ac13294 TG |
315 | { |
316 | u64 ctrl; | |
317 | ||
318 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); | |
862a1a5f | 319 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0); |
2b9ff0db | 320 | |
4ac13294 | 321 | return ctrl; |
241771ef | 322 | } |
b56a3802 | 323 | |
5f4ec28f | 324 | static u64 amd_pmu_save_disable_all(void) |
f87ad35d | 325 | { |
b0f3f28e PZ |
326 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
327 | int enabled, idx; | |
328 | ||
329 | enabled = cpuc->enabled; | |
330 | cpuc->enabled = 0; | |
60b3df9c PZ |
331 | /* |
332 | * ensure we write the disable before we start disabling the | |
5f4ec28f RR |
333 | * counters proper, so that amd_pmu_enable_counter() does the |
334 | * right thing. | |
60b3df9c | 335 | */ |
b0f3f28e | 336 | barrier(); |
962bf7a6 PZ |
337 | if (!enabled) |
338 | goto out; | |
f87ad35d | 339 | |
0933e5c6 | 340 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
b0f3f28e PZ |
341 | u64 val; |
342 | ||
43f6201a | 343 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 344 | continue; |
f87ad35d | 345 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
4295ee62 RR |
346 | if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE)) |
347 | continue; | |
348 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; | |
349 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); | |
f87ad35d JSR |
350 | } |
351 | ||
962bf7a6 | 352 | out: |
b0f3f28e | 353 | return enabled; |
f87ad35d JSR |
354 | } |
355 | ||
b56a3802 JSR |
356 | u64 hw_perf_save_disable(void) |
357 | { | |
85cf9dba | 358 | if (!x86_pmu_initialized()) |
b56a3802 | 359 | return 0; |
4a06bd85 | 360 | return x86_pmu.save_disable_all(); |
b56a3802 | 361 | } |
b0f3f28e PZ |
362 | /* |
363 | * Exported because of ACPI idle | |
364 | */ | |
01b2838c | 365 | EXPORT_SYMBOL_GPL(hw_perf_save_disable); |
241771ef | 366 | |
5f4ec28f | 367 | static void intel_pmu_restore_all(u64 ctrl) |
b56a3802 JSR |
368 | { |
369 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); | |
370 | } | |
371 | ||
5f4ec28f | 372 | static void amd_pmu_restore_all(u64 ctrl) |
f87ad35d | 373 | { |
b0f3f28e | 374 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
f87ad35d JSR |
375 | int idx; |
376 | ||
b0f3f28e PZ |
377 | cpuc->enabled = ctrl; |
378 | barrier(); | |
379 | if (!ctrl) | |
380 | return; | |
381 | ||
0933e5c6 | 382 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
4295ee62 | 383 | u64 val; |
b0f3f28e | 384 | |
43f6201a | 385 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 RR |
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); | |
f87ad35d JSR |
392 | } |
393 | } | |
394 | ||
ee06094f IM |
395 | void hw_perf_restore(u64 ctrl) |
396 | { | |
85cf9dba | 397 | if (!x86_pmu_initialized()) |
2b9ff0db | 398 | return; |
4a06bd85 | 399 | x86_pmu.restore_all(ctrl); |
ee06094f | 400 | } |
b0f3f28e PZ |
401 | /* |
402 | * Exported because of ACPI idle | |
403 | */ | |
ee06094f IM |
404 | EXPORT_SYMBOL_GPL(hw_perf_restore); |
405 | ||
19d84dab | 406 | static inline u64 intel_pmu_get_status(void) |
b0f3f28e PZ |
407 | { |
408 | u64 status; | |
409 | ||
b7f8859a | 410 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
b0f3f28e | 411 | |
b7f8859a | 412 | return status; |
b0f3f28e PZ |
413 | } |
414 | ||
dee5d906 | 415 | static inline void intel_pmu_ack_status(u64 ack) |
b0f3f28e PZ |
416 | { |
417 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); | |
418 | } | |
419 | ||
7c90cc45 | 420 | static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
b0f3f28e | 421 | { |
7c90cc45 | 422 | int err; |
7c90cc45 RR |
423 | err = checking_wrmsrl(hwc->config_base + idx, |
424 | hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE); | |
b0f3f28e PZ |
425 | } |
426 | ||
d4369891 | 427 | static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
b0f3f28e | 428 | { |
d4369891 | 429 | int err; |
d4369891 RR |
430 | err = checking_wrmsrl(hwc->config_base + idx, |
431 | hwc->config); | |
b0f3f28e PZ |
432 | } |
433 | ||
2f18d1e8 | 434 | static inline void |
d4369891 | 435 | intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx) |
2f18d1e8 IM |
436 | { |
437 | int idx = __idx - X86_PMC_IDX_FIXED; | |
438 | u64 ctrl_val, mask; | |
439 | int err; | |
440 | ||
441 | mask = 0xfULL << (idx * 4); | |
442 | ||
443 | rdmsrl(hwc->config_base, ctrl_val); | |
444 | ctrl_val &= ~mask; | |
445 | err = checking_wrmsrl(hwc->config_base, ctrl_val); | |
446 | } | |
447 | ||
7e2ae347 | 448 | static inline void |
d4369891 | 449 | intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
7e2ae347 | 450 | { |
d4369891 RR |
451 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
452 | intel_pmu_disable_fixed(hwc, idx); | |
453 | return; | |
454 | } | |
455 | ||
456 | x86_pmu_disable_counter(hwc, idx); | |
457 | } | |
458 | ||
459 | static inline void | |
460 | amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) | |
461 | { | |
462 | x86_pmu_disable_counter(hwc, idx); | |
7e2ae347 IM |
463 | } |
464 | ||
2f18d1e8 | 465 | static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]); |
241771ef | 466 | |
ee06094f IM |
467 | /* |
468 | * Set the next IRQ period, based on the hwc->period_left value. | |
469 | * To be called with the counter disabled in hw: | |
470 | */ | |
471 | static void | |
26816c28 | 472 | x86_perf_counter_set_period(struct perf_counter *counter, |
ee06094f | 473 | struct hw_perf_counter *hwc, int idx) |
241771ef | 474 | { |
2f18d1e8 | 475 | s64 left = atomic64_read(&hwc->period_left); |
595258aa | 476 | s64 period = hwc->irq_period; |
2f18d1e8 | 477 | int err; |
ee06094f | 478 | |
ee06094f IM |
479 | /* |
480 | * If we are way outside a reasoable range then just skip forward: | |
481 | */ | |
482 | if (unlikely(left <= -period)) { | |
483 | left = period; | |
484 | atomic64_set(&hwc->period_left, left); | |
485 | } | |
486 | ||
487 | if (unlikely(left <= 0)) { | |
488 | left += period; | |
489 | atomic64_set(&hwc->period_left, left); | |
490 | } | |
241771ef | 491 | |
ee06094f IM |
492 | per_cpu(prev_left[idx], smp_processor_id()) = left; |
493 | ||
494 | /* | |
495 | * The hw counter starts counting from this counter offset, | |
496 | * mark it to be able to extra future deltas: | |
497 | */ | |
2f18d1e8 | 498 | atomic64_set(&hwc->prev_count, (u64)-left); |
ee06094f | 499 | |
2f18d1e8 | 500 | err = checking_wrmsrl(hwc->counter_base + idx, |
0933e5c6 | 501 | (u64)(-left) & x86_pmu.counter_mask); |
2f18d1e8 IM |
502 | } |
503 | ||
504 | static inline void | |
7c90cc45 | 505 | intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx) |
2f18d1e8 IM |
506 | { |
507 | int idx = __idx - X86_PMC_IDX_FIXED; | |
508 | u64 ctrl_val, bits, mask; | |
509 | int err; | |
510 | ||
511 | /* | |
0475f9ea PM |
512 | * Enable IRQ generation (0x8), |
513 | * and enable ring-3 counting (0x2) and ring-0 counting (0x1) | |
514 | * if requested: | |
2f18d1e8 | 515 | */ |
0475f9ea PM |
516 | bits = 0x8ULL; |
517 | if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) | |
518 | bits |= 0x2; | |
2f18d1e8 IM |
519 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
520 | bits |= 0x1; | |
521 | bits <<= (idx * 4); | |
522 | mask = 0xfULL << (idx * 4); | |
523 | ||
524 | rdmsrl(hwc->config_base, ctrl_val); | |
525 | ctrl_val &= ~mask; | |
526 | ctrl_val |= bits; | |
527 | err = checking_wrmsrl(hwc->config_base, ctrl_val); | |
7e2ae347 IM |
528 | } |
529 | ||
7c90cc45 | 530 | static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
7e2ae347 | 531 | { |
7c90cc45 RR |
532 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
533 | intel_pmu_enable_fixed(hwc, idx); | |
534 | return; | |
535 | } | |
536 | ||
537 | x86_pmu_enable_counter(hwc, idx); | |
538 | } | |
539 | ||
540 | static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) | |
541 | { | |
542 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
543 | ||
544 | if (cpuc->enabled) | |
545 | x86_pmu_enable_counter(hwc, idx); | |
2b583d8b | 546 | else |
d4369891 | 547 | x86_pmu_disable_counter(hwc, idx); |
241771ef IM |
548 | } |
549 | ||
2f18d1e8 IM |
550 | static int |
551 | fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc) | |
862a1a5f | 552 | { |
2f18d1e8 IM |
553 | unsigned int event; |
554 | ||
ef7b3e09 | 555 | if (!x86_pmu.num_counters_fixed) |
f87ad35d JSR |
556 | return -1; |
557 | ||
2f18d1e8 IM |
558 | if (unlikely(hwc->nmi)) |
559 | return -1; | |
560 | ||
561 | event = hwc->config & ARCH_PERFMON_EVENT_MASK; | |
562 | ||
4a06bd85 | 563 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS))) |
2f18d1e8 | 564 | return X86_PMC_IDX_FIXED_INSTRUCTIONS; |
4a06bd85 | 565 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES))) |
2f18d1e8 | 566 | return X86_PMC_IDX_FIXED_CPU_CYCLES; |
4a06bd85 | 567 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES))) |
2f18d1e8 IM |
568 | return X86_PMC_IDX_FIXED_BUS_CYCLES; |
569 | ||
862a1a5f IM |
570 | return -1; |
571 | } | |
572 | ||
ee06094f IM |
573 | /* |
574 | * Find a PMC slot for the freshly enabled / scheduled in counter: | |
575 | */ | |
4aeb0b42 | 576 | static int x86_pmu_enable(struct perf_counter *counter) |
241771ef IM |
577 | { |
578 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
579 | struct hw_perf_counter *hwc = &counter->hw; | |
2f18d1e8 | 580 | int idx; |
241771ef | 581 | |
2f18d1e8 IM |
582 | idx = fixed_mode_idx(counter, hwc); |
583 | if (idx >= 0) { | |
584 | /* | |
585 | * Try to get the fixed counter, if that is already taken | |
586 | * then try to get a generic counter: | |
587 | */ | |
43f6201a | 588 | if (test_and_set_bit(idx, cpuc->used_mask)) |
2f18d1e8 | 589 | goto try_generic; |
0dff86aa | 590 | |
2f18d1e8 IM |
591 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
592 | /* | |
593 | * We set it so that counter_base + idx in wrmsr/rdmsr maps to | |
594 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: | |
595 | */ | |
596 | hwc->counter_base = | |
597 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; | |
241771ef | 598 | hwc->idx = idx; |
2f18d1e8 IM |
599 | } else { |
600 | idx = hwc->idx; | |
601 | /* Try to get the previous generic counter again */ | |
43f6201a | 602 | if (test_and_set_bit(idx, cpuc->used_mask)) { |
2f18d1e8 | 603 | try_generic: |
43f6201a | 604 | idx = find_first_zero_bit(cpuc->used_mask, |
0933e5c6 RR |
605 | x86_pmu.num_counters); |
606 | if (idx == x86_pmu.num_counters) | |
2f18d1e8 IM |
607 | return -EAGAIN; |
608 | ||
43f6201a | 609 | set_bit(idx, cpuc->used_mask); |
2f18d1e8 IM |
610 | hwc->idx = idx; |
611 | } | |
4a06bd85 RR |
612 | hwc->config_base = x86_pmu.eventsel; |
613 | hwc->counter_base = x86_pmu.perfctr; | |
241771ef IM |
614 | } |
615 | ||
616 | perf_counters_lapic_init(hwc->nmi); | |
617 | ||
d4369891 | 618 | x86_pmu.disable(hwc, idx); |
241771ef | 619 | |
862a1a5f | 620 | cpuc->counters[idx] = counter; |
43f6201a | 621 | set_bit(idx, cpuc->active_mask); |
7e2ae347 | 622 | |
26816c28 | 623 | x86_perf_counter_set_period(counter, hwc, idx); |
7c90cc45 | 624 | x86_pmu.enable(hwc, idx); |
95cdd2e7 IM |
625 | |
626 | return 0; | |
241771ef IM |
627 | } |
628 | ||
629 | void perf_counter_print_debug(void) | |
630 | { | |
2f18d1e8 | 631 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
0dff86aa | 632 | struct cpu_hw_counters *cpuc; |
5bb9efe3 | 633 | unsigned long flags; |
1e125676 IM |
634 | int cpu, idx; |
635 | ||
0933e5c6 | 636 | if (!x86_pmu.num_counters) |
1e125676 | 637 | return; |
241771ef | 638 | |
5bb9efe3 | 639 | local_irq_save(flags); |
241771ef IM |
640 | |
641 | cpu = smp_processor_id(); | |
0dff86aa | 642 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
241771ef | 643 | |
faa28ae0 | 644 | if (x86_pmu.version >= 2) { |
a1ef58f4 JSR |
645 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
646 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); | |
647 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); | |
648 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); | |
649 | ||
650 | pr_info("\n"); | |
651 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); | |
652 | pr_info("CPU#%d: status: %016llx\n", cpu, status); | |
653 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); | |
654 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); | |
f87ad35d | 655 | } |
43f6201a | 656 | pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask); |
241771ef | 657 | |
0933e5c6 | 658 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
4a06bd85 RR |
659 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
660 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); | |
241771ef | 661 | |
ee06094f | 662 | prev_left = per_cpu(prev_left[idx], cpu); |
241771ef | 663 | |
a1ef58f4 | 664 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
241771ef | 665 | cpu, idx, pmc_ctrl); |
a1ef58f4 | 666 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
241771ef | 667 | cpu, idx, pmc_count); |
a1ef58f4 | 668 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
ee06094f | 669 | cpu, idx, prev_left); |
241771ef | 670 | } |
0933e5c6 | 671 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
2f18d1e8 IM |
672 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
673 | ||
a1ef58f4 | 674 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
2f18d1e8 IM |
675 | cpu, idx, pmc_count); |
676 | } | |
5bb9efe3 | 677 | local_irq_restore(flags); |
241771ef IM |
678 | } |
679 | ||
4aeb0b42 | 680 | static void x86_pmu_disable(struct perf_counter *counter) |
241771ef IM |
681 | { |
682 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
683 | struct hw_perf_counter *hwc = &counter->hw; | |
6f00cada | 684 | int idx = hwc->idx; |
241771ef | 685 | |
09534238 RR |
686 | /* |
687 | * Must be done before we disable, otherwise the nmi handler | |
688 | * could reenable again: | |
689 | */ | |
43f6201a | 690 | clear_bit(idx, cpuc->active_mask); |
d4369891 | 691 | x86_pmu.disable(hwc, idx); |
241771ef | 692 | |
2f18d1e8 IM |
693 | /* |
694 | * Make sure the cleared pointer becomes visible before we | |
695 | * (potentially) free the counter: | |
696 | */ | |
527e26af | 697 | barrier(); |
241771ef | 698 | |
ee06094f IM |
699 | /* |
700 | * Drain the remaining delta count out of a counter | |
701 | * that we are disabling: | |
702 | */ | |
703 | x86_perf_counter_update(counter, hwc, idx); | |
09534238 | 704 | cpuc->counters[idx] = NULL; |
43f6201a | 705 | clear_bit(idx, cpuc->used_mask); |
241771ef IM |
706 | } |
707 | ||
7e2ae347 | 708 | /* |
ee06094f IM |
709 | * Save and restart an expired counter. Called by NMI contexts, |
710 | * so it has to be careful about preempting normal counter ops: | |
7e2ae347 | 711 | */ |
55de0f2e | 712 | static void intel_pmu_save_and_restart(struct perf_counter *counter) |
241771ef IM |
713 | { |
714 | struct hw_perf_counter *hwc = &counter->hw; | |
715 | int idx = hwc->idx; | |
241771ef | 716 | |
ee06094f | 717 | x86_perf_counter_update(counter, hwc, idx); |
26816c28 | 718 | x86_perf_counter_set_period(counter, hwc, idx); |
7e2ae347 | 719 | |
2f18d1e8 | 720 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
7c90cc45 | 721 | intel_pmu_enable_counter(hwc, idx); |
241771ef IM |
722 | } |
723 | ||
4b39fd96 MG |
724 | /* |
725 | * Maximum interrupt frequency of 100KHz per CPU | |
726 | */ | |
169e41eb | 727 | #define PERFMON_MAX_INTERRUPTS (100000/HZ) |
4b39fd96 | 728 | |
241771ef IM |
729 | /* |
730 | * This handler is triggered by the local APIC, so the APIC IRQ handling | |
731 | * rules apply: | |
732 | */ | |
39d81eab | 733 | static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi) |
241771ef IM |
734 | { |
735 | int bit, cpu = smp_processor_id(); | |
4b39fd96 | 736 | u64 ack, status; |
1b023a96 | 737 | struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu); |
b0f3f28e | 738 | int ret = 0; |
43874d23 | 739 | |
55de0f2e | 740 | cpuc->throttle_ctrl = intel_pmu_save_disable_all(); |
241771ef | 741 | |
19d84dab | 742 | status = intel_pmu_get_status(); |
87b9cf46 IM |
743 | if (!status) |
744 | goto out; | |
745 | ||
b0f3f28e | 746 | ret = 1; |
241771ef | 747 | again: |
d278c484 | 748 | inc_irq_stat(apic_perf_irqs); |
241771ef | 749 | ack = status; |
2f18d1e8 | 750 | for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
862a1a5f | 751 | struct perf_counter *counter = cpuc->counters[bit]; |
241771ef IM |
752 | |
753 | clear_bit(bit, (unsigned long *) &status); | |
43f6201a | 754 | if (!test_bit(bit, cpuc->active_mask)) |
241771ef IM |
755 | continue; |
756 | ||
55de0f2e | 757 | intel_pmu_save_and_restart(counter); |
78f13e95 | 758 | if (perf_counter_overflow(counter, nmi, regs, 0)) |
d4369891 | 759 | intel_pmu_disable_counter(&counter->hw, bit); |
241771ef IM |
760 | } |
761 | ||
dee5d906 | 762 | intel_pmu_ack_status(ack); |
241771ef IM |
763 | |
764 | /* | |
765 | * Repeat if there is more work to be done: | |
766 | */ | |
19d84dab | 767 | status = intel_pmu_get_status(); |
241771ef IM |
768 | if (status) |
769 | goto again; | |
87b9cf46 | 770 | out: |
241771ef | 771 | /* |
1b023a96 | 772 | * Restore - do not reenable when global enable is off or throttled: |
241771ef | 773 | */ |
f5a5a2f6 IM |
774 | if (cpuc->throttle_ctrl) { |
775 | if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS) { | |
776 | intel_pmu_restore_all(cpuc->throttle_ctrl); | |
777 | } else { | |
778 | pr_info("CPU#%d: perfcounters: max interrupt rate exceeded! Throttle on.\n", smp_processor_id()); | |
779 | } | |
780 | } | |
b0f3f28e PZ |
781 | |
782 | return ret; | |
1b023a96 MG |
783 | } |
784 | ||
a29aa8a7 RR |
785 | static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi) |
786 | { | |
787 | int cpu = smp_processor_id(); | |
788 | struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu); | |
789 | u64 val; | |
790 | int handled = 0; | |
791 | struct perf_counter *counter; | |
792 | struct hw_perf_counter *hwc; | |
962bf7a6 PZ |
793 | int idx, throttle = 0; |
794 | ||
795 | cpuc->throttle_ctrl = cpuc->enabled; | |
796 | cpuc->enabled = 0; | |
797 | barrier(); | |
798 | ||
799 | if (cpuc->throttle_ctrl) { | |
800 | if (++cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) | |
801 | throttle = 1; | |
802 | } | |
a29aa8a7 | 803 | |
a29aa8a7 | 804 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
962bf7a6 PZ |
805 | int disable = 0; |
806 | ||
43f6201a | 807 | if (!test_bit(idx, cpuc->active_mask)) |
a29aa8a7 | 808 | continue; |
962bf7a6 | 809 | |
a29aa8a7 RR |
810 | counter = cpuc->counters[idx]; |
811 | hwc = &counter->hw; | |
4b7bfd0d | 812 | val = x86_perf_counter_update(counter, hwc, idx); |
a29aa8a7 | 813 | if (val & (1ULL << (x86_pmu.counter_bits - 1))) |
962bf7a6 PZ |
814 | goto next; |
815 | ||
a29aa8a7 RR |
816 | /* counter overflow */ |
817 | x86_perf_counter_set_period(counter, hwc, idx); | |
818 | handled = 1; | |
819 | inc_irq_stat(apic_perf_irqs); | |
962bf7a6 PZ |
820 | disable = perf_counter_overflow(counter, nmi, regs, 0); |
821 | ||
822 | next: | |
823 | if (disable || throttle) | |
a29aa8a7 | 824 | amd_pmu_disable_counter(hwc, idx); |
a29aa8a7 | 825 | } |
962bf7a6 PZ |
826 | |
827 | if (cpuc->throttle_ctrl && !throttle) | |
828 | cpuc->enabled = 1; | |
829 | ||
a29aa8a7 RR |
830 | return handled; |
831 | } | |
39d81eab | 832 | |
1b023a96 MG |
833 | void perf_counter_unthrottle(void) |
834 | { | |
835 | struct cpu_hw_counters *cpuc; | |
836 | ||
85cf9dba | 837 | if (!x86_pmu_initialized()) |
1b023a96 MG |
838 | return; |
839 | ||
b0f3f28e | 840 | cpuc = &__get_cpu_var(cpu_hw_counters); |
4b39fd96 | 841 | if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) { |
f5a5a2f6 IM |
842 | pr_info("CPU#%d: perfcounters: throttle off.\n", smp_processor_id()); |
843 | ||
844 | /* | |
845 | * Clear them before re-enabling irqs/NMIs again: | |
846 | */ | |
847 | cpuc->interrupts = 0; | |
b0f3f28e | 848 | hw_perf_restore(cpuc->throttle_ctrl); |
f5a5a2f6 IM |
849 | } else { |
850 | cpuc->interrupts = 0; | |
1b023a96 | 851 | } |
241771ef IM |
852 | } |
853 | ||
854 | void smp_perf_counter_interrupt(struct pt_regs *regs) | |
855 | { | |
856 | irq_enter(); | |
241771ef | 857 | apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR); |
b0f3f28e | 858 | ack_APIC_irq(); |
4a06bd85 | 859 | x86_pmu.handle_irq(regs, 0); |
241771ef IM |
860 | irq_exit(); |
861 | } | |
862 | ||
b6276f35 PZ |
863 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
864 | { | |
865 | irq_enter(); | |
866 | ack_APIC_irq(); | |
867 | inc_irq_stat(apic_pending_irqs); | |
868 | perf_counter_do_pending(); | |
869 | irq_exit(); | |
870 | } | |
871 | ||
872 | void set_perf_counter_pending(void) | |
873 | { | |
874 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); | |
875 | } | |
876 | ||
3415dd91 | 877 | void perf_counters_lapic_init(int nmi) |
241771ef IM |
878 | { |
879 | u32 apic_val; | |
880 | ||
85cf9dba | 881 | if (!x86_pmu_initialized()) |
241771ef | 882 | return; |
85cf9dba | 883 | |
241771ef IM |
884 | /* |
885 | * Enable the performance counter vector in the APIC LVT: | |
886 | */ | |
887 | apic_val = apic_read(APIC_LVTERR); | |
888 | ||
889 | apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED); | |
890 | if (nmi) | |
891 | apic_write(APIC_LVTPC, APIC_DM_NMI); | |
892 | else | |
893 | apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR); | |
894 | apic_write(APIC_LVTERR, apic_val); | |
895 | } | |
896 | ||
897 | static int __kprobes | |
898 | perf_counter_nmi_handler(struct notifier_block *self, | |
899 | unsigned long cmd, void *__args) | |
900 | { | |
901 | struct die_args *args = __args; | |
902 | struct pt_regs *regs; | |
b0f3f28e PZ |
903 | int ret; |
904 | ||
ba77813a | 905 | if (!atomic_read(&active_counters)) |
63a809a2 PZ |
906 | return NOTIFY_DONE; |
907 | ||
b0f3f28e PZ |
908 | switch (cmd) { |
909 | case DIE_NMI: | |
910 | case DIE_NMI_IPI: | |
911 | break; | |
241771ef | 912 | |
b0f3f28e | 913 | default: |
241771ef | 914 | return NOTIFY_DONE; |
b0f3f28e | 915 | } |
241771ef IM |
916 | |
917 | regs = args->regs; | |
918 | ||
919 | apic_write(APIC_LVTPC, APIC_DM_NMI); | |
4a06bd85 | 920 | ret = x86_pmu.handle_irq(regs, 1); |
241771ef | 921 | |
b0f3f28e | 922 | return ret ? NOTIFY_STOP : NOTIFY_OK; |
241771ef IM |
923 | } |
924 | ||
925 | static __read_mostly struct notifier_block perf_counter_nmi_notifier = { | |
5b75af0a MG |
926 | .notifier_call = perf_counter_nmi_handler, |
927 | .next = NULL, | |
928 | .priority = 1 | |
241771ef IM |
929 | }; |
930 | ||
5f4ec28f | 931 | static struct x86_pmu intel_pmu = { |
faa28ae0 | 932 | .name = "Intel", |
39d81eab | 933 | .handle_irq = intel_pmu_handle_irq, |
5f4ec28f RR |
934 | .save_disable_all = intel_pmu_save_disable_all, |
935 | .restore_all = intel_pmu_restore_all, | |
5f4ec28f RR |
936 | .enable = intel_pmu_enable_counter, |
937 | .disable = intel_pmu_disable_counter, | |
b56a3802 JSR |
938 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
939 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, | |
5f4ec28f RR |
940 | .event_map = intel_pmu_event_map, |
941 | .raw_event = intel_pmu_raw_event, | |
b56a3802 | 942 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
c619b8ff RR |
943 | /* |
944 | * Intel PMCs cannot be accessed sanely above 32 bit width, | |
945 | * so we install an artificial 1<<31 period regardless of | |
946 | * the generic counter period: | |
947 | */ | |
948 | .max_period = (1ULL << 31) - 1, | |
b56a3802 JSR |
949 | }; |
950 | ||
5f4ec28f | 951 | static struct x86_pmu amd_pmu = { |
faa28ae0 | 952 | .name = "AMD", |
39d81eab | 953 | .handle_irq = amd_pmu_handle_irq, |
5f4ec28f RR |
954 | .save_disable_all = amd_pmu_save_disable_all, |
955 | .restore_all = amd_pmu_restore_all, | |
5f4ec28f RR |
956 | .enable = amd_pmu_enable_counter, |
957 | .disable = amd_pmu_disable_counter, | |
f87ad35d JSR |
958 | .eventsel = MSR_K7_EVNTSEL0, |
959 | .perfctr = MSR_K7_PERFCTR0, | |
5f4ec28f RR |
960 | .event_map = amd_pmu_event_map, |
961 | .raw_event = amd_pmu_raw_event, | |
f87ad35d | 962 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
0933e5c6 RR |
963 | .num_counters = 4, |
964 | .counter_bits = 48, | |
965 | .counter_mask = (1ULL << 48) - 1, | |
c619b8ff RR |
966 | /* use highest bit to detect overflow */ |
967 | .max_period = (1ULL << 47) - 1, | |
f87ad35d JSR |
968 | }; |
969 | ||
72eae04d | 970 | static int intel_pmu_init(void) |
241771ef | 971 | { |
7bb497bd | 972 | union cpuid10_edx edx; |
241771ef | 973 | union cpuid10_eax eax; |
703e937c | 974 | unsigned int unused; |
7bb497bd | 975 | unsigned int ebx; |
faa28ae0 | 976 | int version; |
241771ef | 977 | |
da1a776b | 978 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
72eae04d | 979 | return -ENODEV; |
da1a776b | 980 | |
241771ef IM |
981 | /* |
982 | * Check whether the Architectural PerfMon supports | |
983 | * Branch Misses Retired Event or not. | |
984 | */ | |
703e937c | 985 | cpuid(10, &eax.full, &ebx, &unused, &edx.full); |
241771ef | 986 | if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED) |
72eae04d | 987 | return -ENODEV; |
241771ef | 988 | |
faa28ae0 RR |
989 | version = eax.split.version_id; |
990 | if (version < 2) | |
72eae04d | 991 | return -ENODEV; |
7bb497bd | 992 | |
4a06bd85 | 993 | x86_pmu = intel_pmu; |
faa28ae0 | 994 | x86_pmu.version = version; |
0933e5c6 | 995 | x86_pmu.num_counters = eax.split.num_counters; |
066d7dea IM |
996 | |
997 | /* | |
998 | * Quirk: v2 perfmon does not report fixed-purpose counters, so | |
999 | * assume at least 3 counters: | |
1000 | */ | |
1001 | x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3); | |
1002 | ||
0933e5c6 RR |
1003 | x86_pmu.counter_bits = eax.split.bit_width; |
1004 | x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1; | |
b56a3802 | 1005 | |
72eae04d | 1006 | return 0; |
b56a3802 JSR |
1007 | } |
1008 | ||
72eae04d | 1009 | static int amd_pmu_init(void) |
f87ad35d | 1010 | { |
4a06bd85 | 1011 | x86_pmu = amd_pmu; |
72eae04d | 1012 | return 0; |
f87ad35d JSR |
1013 | } |
1014 | ||
b56a3802 JSR |
1015 | void __init init_hw_perf_counters(void) |
1016 | { | |
72eae04d RR |
1017 | int err; |
1018 | ||
b56a3802 JSR |
1019 | switch (boot_cpu_data.x86_vendor) { |
1020 | case X86_VENDOR_INTEL: | |
72eae04d | 1021 | err = intel_pmu_init(); |
b56a3802 | 1022 | break; |
f87ad35d | 1023 | case X86_VENDOR_AMD: |
72eae04d | 1024 | err = amd_pmu_init(); |
f87ad35d | 1025 | break; |
4138960a RR |
1026 | default: |
1027 | return; | |
b56a3802 | 1028 | } |
72eae04d | 1029 | if (err != 0) |
b56a3802 JSR |
1030 | return; |
1031 | ||
faa28ae0 RR |
1032 | pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name); |
1033 | pr_info("... version: %d\n", x86_pmu.version); | |
1034 | pr_info("... bit width: %d\n", x86_pmu.counter_bits); | |
1035 | ||
0933e5c6 RR |
1036 | pr_info("... num counters: %d\n", x86_pmu.num_counters); |
1037 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { | |
1038 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; | |
241771ef | 1039 | WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!", |
0933e5c6 | 1040 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
241771ef | 1041 | } |
0933e5c6 RR |
1042 | perf_counter_mask = (1 << x86_pmu.num_counters) - 1; |
1043 | perf_max_counters = x86_pmu.num_counters; | |
241771ef | 1044 | |
0933e5c6 | 1045 | pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask); |
c619b8ff | 1046 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
2f18d1e8 | 1047 | |
0933e5c6 RR |
1048 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
1049 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; | |
703e937c | 1050 | WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!", |
0933e5c6 | 1051 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
703e937c | 1052 | } |
0933e5c6 | 1053 | pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed); |
862a1a5f | 1054 | |
0933e5c6 RR |
1055 | perf_counter_mask |= |
1056 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; | |
241771ef | 1057 | |
a1ef58f4 | 1058 | pr_info("... counter mask: %016Lx\n", perf_counter_mask); |
75f224cf | 1059 | |
241771ef IM |
1060 | perf_counters_lapic_init(0); |
1061 | register_die_notifier(&perf_counter_nmi_notifier); | |
241771ef | 1062 | } |
621a01ea | 1063 | |
bb775fc2 | 1064 | static inline void x86_pmu_read(struct perf_counter *counter) |
ee06094f IM |
1065 | { |
1066 | x86_perf_counter_update(counter, &counter->hw, counter->hw.idx); | |
1067 | } | |
1068 | ||
4aeb0b42 RR |
1069 | static const struct pmu pmu = { |
1070 | .enable = x86_pmu_enable, | |
1071 | .disable = x86_pmu_disable, | |
1072 | .read = x86_pmu_read, | |
621a01ea IM |
1073 | }; |
1074 | ||
4aeb0b42 | 1075 | const struct pmu *hw_perf_counter_init(struct perf_counter *counter) |
621a01ea IM |
1076 | { |
1077 | int err; | |
1078 | ||
1079 | err = __hw_perf_counter_init(counter); | |
1080 | if (err) | |
9ea98e19 | 1081 | return ERR_PTR(err); |
621a01ea | 1082 | |
4aeb0b42 | 1083 | return &pmu; |
621a01ea | 1084 | } |
d7d59fb3 PZ |
1085 | |
1086 | /* | |
1087 | * callchain support | |
1088 | */ | |
1089 | ||
1090 | static inline | |
1091 | void callchain_store(struct perf_callchain_entry *entry, unsigned long ip) | |
1092 | { | |
1093 | if (entry->nr < MAX_STACK_DEPTH) | |
1094 | entry->ip[entry->nr++] = ip; | |
1095 | } | |
1096 | ||
1097 | static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry); | |
1098 | static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry); | |
1099 | ||
1100 | ||
1101 | static void | |
1102 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) | |
1103 | { | |
1104 | /* Ignore warnings */ | |
1105 | } | |
1106 | ||
1107 | static void backtrace_warning(void *data, char *msg) | |
1108 | { | |
1109 | /* Ignore warnings */ | |
1110 | } | |
1111 | ||
1112 | static int backtrace_stack(void *data, char *name) | |
1113 | { | |
1114 | /* Don't bother with IRQ stacks for now */ | |
1115 | return -1; | |
1116 | } | |
1117 | ||
1118 | static void backtrace_address(void *data, unsigned long addr, int reliable) | |
1119 | { | |
1120 | struct perf_callchain_entry *entry = data; | |
1121 | ||
1122 | if (reliable) | |
1123 | callchain_store(entry, addr); | |
1124 | } | |
1125 | ||
1126 | static const struct stacktrace_ops backtrace_ops = { | |
1127 | .warning = backtrace_warning, | |
1128 | .warning_symbol = backtrace_warning_symbol, | |
1129 | .stack = backtrace_stack, | |
1130 | .address = backtrace_address, | |
1131 | }; | |
1132 | ||
1133 | static void | |
1134 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1135 | { | |
1136 | unsigned long bp; | |
1137 | char *stack; | |
5872bdb8 | 1138 | int nr = entry->nr; |
d7d59fb3 PZ |
1139 | |
1140 | callchain_store(entry, instruction_pointer(regs)); | |
1141 | ||
1142 | stack = ((char *)regs + sizeof(struct pt_regs)); | |
1143 | #ifdef CONFIG_FRAME_POINTER | |
1144 | bp = frame_pointer(regs); | |
1145 | #else | |
1146 | bp = 0; | |
1147 | #endif | |
1148 | ||
1149 | dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry); | |
5872bdb8 PZ |
1150 | |
1151 | entry->kernel = entry->nr - nr; | |
d7d59fb3 PZ |
1152 | } |
1153 | ||
1154 | ||
1155 | struct stack_frame { | |
1156 | const void __user *next_fp; | |
1157 | unsigned long return_address; | |
1158 | }; | |
1159 | ||
1160 | static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) | |
1161 | { | |
1162 | int ret; | |
1163 | ||
1164 | if (!access_ok(VERIFY_READ, fp, sizeof(*frame))) | |
1165 | return 0; | |
1166 | ||
1167 | ret = 1; | |
1168 | pagefault_disable(); | |
1169 | if (__copy_from_user_inatomic(frame, fp, sizeof(*frame))) | |
1170 | ret = 0; | |
1171 | pagefault_enable(); | |
1172 | ||
1173 | return ret; | |
1174 | } | |
1175 | ||
1176 | static void | |
1177 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1178 | { | |
1179 | struct stack_frame frame; | |
1180 | const void __user *fp; | |
5872bdb8 | 1181 | int nr = entry->nr; |
d7d59fb3 PZ |
1182 | |
1183 | regs = (struct pt_regs *)current->thread.sp0 - 1; | |
1184 | fp = (void __user *)regs->bp; | |
1185 | ||
1186 | callchain_store(entry, regs->ip); | |
1187 | ||
1188 | while (entry->nr < MAX_STACK_DEPTH) { | |
1189 | frame.next_fp = NULL; | |
1190 | frame.return_address = 0; | |
1191 | ||
1192 | if (!copy_stack_frame(fp, &frame)) | |
1193 | break; | |
1194 | ||
1195 | if ((unsigned long)fp < user_stack_pointer(regs)) | |
1196 | break; | |
1197 | ||
1198 | callchain_store(entry, frame.return_address); | |
1199 | fp = frame.next_fp; | |
1200 | } | |
5872bdb8 PZ |
1201 | |
1202 | entry->user = entry->nr - nr; | |
d7d59fb3 PZ |
1203 | } |
1204 | ||
1205 | static void | |
1206 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1207 | { | |
1208 | int is_user; | |
1209 | ||
1210 | if (!regs) | |
1211 | return; | |
1212 | ||
1213 | is_user = user_mode(regs); | |
1214 | ||
1215 | if (!current || current->pid == 0) | |
1216 | return; | |
1217 | ||
1218 | if (is_user && current->state != TASK_RUNNING) | |
1219 | return; | |
1220 | ||
1221 | if (!is_user) | |
1222 | perf_callchain_kernel(regs, entry); | |
1223 | ||
1224 | if (current->mm) | |
1225 | perf_callchain_user(regs, entry); | |
1226 | } | |
1227 | ||
1228 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) | |
1229 | { | |
1230 | struct perf_callchain_entry *entry; | |
1231 | ||
1232 | if (in_nmi()) | |
1233 | entry = &__get_cpu_var(nmi_entry); | |
1234 | else | |
1235 | entry = &__get_cpu_var(irq_entry); | |
1236 | ||
1237 | entry->nr = 0; | |
5872bdb8 PZ |
1238 | entry->hv = 0; |
1239 | entry->kernel = 0; | |
1240 | entry->user = 0; | |
d7d59fb3 PZ |
1241 | |
1242 | perf_do_callchain(regs, entry); | |
1243 | ||
1244 | return entry; | |
1245 | } |