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