Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * local apic based NMI watchdog for various CPUs. |
| 3 | * |
| 4 | * This file also handles reservation of performance counters for coordination |
| 5 | * with other users (like oprofile). |
| 6 | * |
| 7 | * Note that these events normally don't tick when the CPU idles. This means |
| 8 | * the frequency varies with CPU load. |
| 9 | * |
| 10 | * Original code for K7/P6 written by Keith Owens |
| 11 | * |
| 12 | */ |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 13 | |
| 14 | #include <linux/percpu.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/smp.h> |
Don Zickus | 4a7863c | 2010-12-22 14:00:03 -0500 | [diff] [blame^] | 19 | #include <asm/nmi.h> |
Ingo Molnar | 8b1fa1d | 2008-07-29 12:36:02 +0200 | [diff] [blame] | 20 | #include <linux/kprobes.h> |
| 21 | |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 22 | #include <asm/apic.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 23 | #include <asm/perf_event.h> |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 24 | |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 25 | /* |
| 26 | * this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's |
| 27 | * offset from MSR_P4_BSU_ESCR0. |
| 28 | * |
| 29 | * It will be the max for all platforms (for now) |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 30 | */ |
| 31 | #define NMI_MAX_COUNTER_BITS 66 |
| 32 | |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 33 | /* |
| 34 | * perfctr_nmi_owner tracks the ownership of the perfctr registers: |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 35 | * evtsel_nmi_owner tracks the ownership of the event selection |
| 36 | * - different performance counters/ event selection may be reserved for |
| 37 | * different subsystems this reservation system just tries to coordinate |
| 38 | * things a little |
| 39 | */ |
| 40 | static DECLARE_BITMAP(perfctr_nmi_owner, NMI_MAX_COUNTER_BITS); |
| 41 | static DECLARE_BITMAP(evntsel_nmi_owner, NMI_MAX_COUNTER_BITS); |
| 42 | |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 43 | /* converts an msr to an appropriate reservation bit */ |
| 44 | static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr) |
| 45 | { |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 46 | /* returns the bit offset of the performance counter register */ |
| 47 | switch (boot_cpu_data.x86_vendor) { |
| 48 | case X86_VENDOR_AMD: |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 49 | return msr - MSR_K7_PERFCTR0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 50 | case X86_VENDOR_INTEL: |
| 51 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 52 | return msr - MSR_ARCH_PERFMON_PERFCTR0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 53 | |
| 54 | switch (boot_cpu_data.x86) { |
| 55 | case 6: |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 56 | return msr - MSR_P6_PERFCTR0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 57 | case 15: |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 58 | return msr - MSR_P4_BPU_PERFCTR0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | return 0; |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 64 | /* |
| 65 | * converts an msr to an appropriate reservation bit |
| 66 | * returns the bit offset of the event selection register |
| 67 | */ |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 68 | static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr) |
| 69 | { |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 70 | /* returns the bit offset of the event selection register */ |
| 71 | switch (boot_cpu_data.x86_vendor) { |
| 72 | case X86_VENDOR_AMD: |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 73 | return msr - MSR_K7_EVNTSEL0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 74 | case X86_VENDOR_INTEL: |
| 75 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 76 | return msr - MSR_ARCH_PERFMON_EVENTSEL0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 77 | |
| 78 | switch (boot_cpu_data.x86) { |
| 79 | case 6: |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 80 | return msr - MSR_P6_EVNTSEL0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 81 | case 15: |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 82 | return msr - MSR_P4_BSU_ESCR0; |
Andi Kleen | 5dcccd8 | 2007-07-04 01:38:13 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return 0; |
| 86 | |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* checks for a bit availability (hack for oprofile) */ |
| 90 | int avail_to_resrv_perfctr_nmi_bit(unsigned int counter) |
| 91 | { |
| 92 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 93 | |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 94 | return !test_bit(counter, perfctr_nmi_owner); |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 95 | } |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 96 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit); |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 97 | |
| 98 | int reserve_perfctr_nmi(unsigned int msr) |
| 99 | { |
| 100 | unsigned int counter; |
| 101 | |
| 102 | counter = nmi_perfctr_msr_to_bit(msr); |
Stephane Eranian | 124d395 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 103 | /* register not managed by the allocator? */ |
| 104 | if (counter > NMI_MAX_COUNTER_BITS) |
| 105 | return 1; |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 106 | |
| 107 | if (!test_and_set_bit(counter, perfctr_nmi_owner)) |
| 108 | return 1; |
| 109 | return 0; |
| 110 | } |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 111 | EXPORT_SYMBOL(reserve_perfctr_nmi); |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 112 | |
| 113 | void release_perfctr_nmi(unsigned int msr) |
| 114 | { |
| 115 | unsigned int counter; |
| 116 | |
| 117 | counter = nmi_perfctr_msr_to_bit(msr); |
Stephane Eranian | 124d395 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 118 | /* register not managed by the allocator? */ |
| 119 | if (counter > NMI_MAX_COUNTER_BITS) |
| 120 | return; |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 121 | |
| 122 | clear_bit(counter, perfctr_nmi_owner); |
| 123 | } |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 124 | EXPORT_SYMBOL(release_perfctr_nmi); |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 125 | |
| 126 | int reserve_evntsel_nmi(unsigned int msr) |
| 127 | { |
| 128 | unsigned int counter; |
| 129 | |
| 130 | counter = nmi_evntsel_msr_to_bit(msr); |
Stephane Eranian | 124d395 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 131 | /* register not managed by the allocator? */ |
| 132 | if (counter > NMI_MAX_COUNTER_BITS) |
| 133 | return 1; |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 134 | |
| 135 | if (!test_and_set_bit(counter, evntsel_nmi_owner)) |
| 136 | return 1; |
| 137 | return 0; |
| 138 | } |
Cyrill Gorcunov | 47a486c | 2008-06-24 22:52:03 +0200 | [diff] [blame] | 139 | EXPORT_SYMBOL(reserve_evntsel_nmi); |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 140 | |
| 141 | void release_evntsel_nmi(unsigned int msr) |
| 142 | { |
| 143 | unsigned int counter; |
| 144 | |
| 145 | counter = nmi_evntsel_msr_to_bit(msr); |
Stephane Eranian | 124d395 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 146 | /* register not managed by the allocator? */ |
| 147 | if (counter > NMI_MAX_COUNTER_BITS) |
| 148 | return; |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 149 | |
| 150 | clear_bit(counter, evntsel_nmi_owner); |
| 151 | } |
Andi Kleen | 09198e6 | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 152 | EXPORT_SYMBOL(release_evntsel_nmi); |