Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file nmi_int.c |
| 3 | * |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 4 | * @remark Copyright 2002-2008 OProfile authors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author John Levon <levon@movementarian.org> |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 8 | * @author Robert Richter <robert.richter@amd.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/notifier.h> |
| 13 | #include <linux/smp.h> |
| 14 | #include <linux/oprofile.h> |
| 15 | #include <linux/sysdev.h> |
| 16 | #include <linux/slab.h> |
Andi Kleen | 1cfcea1 | 2006-07-10 17:06:21 +0200 | [diff] [blame] | 17 | #include <linux/moduleparam.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 18 | #include <linux/kdebug.h> |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/nmi.h> |
| 21 | #include <asm/msr.h> |
| 22 | #include <asm/apic.h> |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "op_counter.h" |
| 25 | #include "op_x86_model.h" |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 26 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 27 | static struct op_x86_model_spec const *model; |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 28 | static DEFINE_PER_CPU(struct op_msrs, cpu_msrs); |
| 29 | static DEFINE_PER_CPU(unsigned long, saved_lvtpc); |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* 0 == registered but off, 1 == registered and on */ |
| 32 | static int nmi_enabled = 0; |
| 33 | |
Adrian Bunk | c7c19f8 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 34 | static int profile_exceptions_notify(struct notifier_block *self, |
| 35 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 37 | struct die_args *args = (struct die_args *)data; |
| 38 | int ret = NOTIFY_DONE; |
| 39 | int cpu = smp_processor_id(); |
| 40 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 41 | switch (val) { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 42 | case DIE_NMI: |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame^] | 43 | case DIE_NMI_IPI: |
| 44 | model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)); |
| 45 | ret = NOTIFY_STOP; |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 46 | break; |
| 47 | default: |
| 48 | break; |
| 49 | } |
| 50 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 52 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 53 | static void nmi_cpu_save_registers(struct op_msrs *msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | unsigned int const nr_ctrs = model->num_counters; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 56 | unsigned int const nr_ctrls = model->num_controls; |
| 57 | struct op_msr *counters = msrs->counters; |
| 58 | struct op_msr *controls = msrs->controls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | unsigned int i; |
| 60 | |
| 61 | for (i = 0; i < nr_ctrs; ++i) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 62 | if (counters[i].addr) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 63 | rdmsr(counters[i].addr, |
| 64 | counters[i].saved.low, |
| 65 | counters[i].saved.high); |
| 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | for (i = 0; i < nr_ctrls; ++i) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 70 | if (controls[i].addr) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 71 | rdmsr(controls[i].addr, |
| 72 | controls[i].saved.low, |
| 73 | controls[i].saved.high); |
| 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 78 | static void nmi_save_registers(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | int cpu = smp_processor_id(); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 81 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | nmi_cpu_save_registers(msrs); |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | static void free_msrs(void) |
| 86 | { |
| 87 | int i; |
KAMEZAWA Hiroyuki | c8912599 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 88 | for_each_possible_cpu(i) { |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 89 | kfree(per_cpu(cpu_msrs, i).counters); |
| 90 | per_cpu(cpu_msrs, i).counters = NULL; |
| 91 | kfree(per_cpu(cpu_msrs, i).controls); |
| 92 | per_cpu(cpu_msrs, i).controls = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static int allocate_msrs(void) |
| 97 | { |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 98 | int success = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | size_t controls_size = sizeof(struct op_msr) * model->num_controls; |
| 100 | size_t counters_size = sizeof(struct op_msr) * model->num_counters; |
| 101 | |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 102 | int i; |
Chris Wright | 0939c17 | 2007-06-01 00:46:39 -0700 | [diff] [blame] | 103 | for_each_possible_cpu(i) { |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 104 | per_cpu(cpu_msrs, i).counters = kmalloc(counters_size, |
| 105 | GFP_KERNEL); |
| 106 | if (!per_cpu(cpu_msrs, i).counters) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | success = 0; |
| 108 | break; |
| 109 | } |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 110 | per_cpu(cpu_msrs, i).controls = kmalloc(controls_size, |
| 111 | GFP_KERNEL); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 112 | if (!per_cpu(cpu_msrs, i).controls) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | success = 0; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!success) |
| 119 | free_msrs(); |
| 120 | |
| 121 | return success; |
| 122 | } |
| 123 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 124 | static void nmi_cpu_setup(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | int cpu = smp_processor_id(); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 127 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | spin_lock(&oprofilefs_lock); |
| 129 | model->setup_ctrs(msrs); |
| 130 | spin_unlock(&oprofilefs_lock); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 131 | per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 133 | } |
| 134 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 135 | static struct notifier_block profile_exceptions_nb = { |
| 136 | .notifier_call = profile_exceptions_notify, |
| 137 | .next = NULL, |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame^] | 138 | .priority = 2 |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 139 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | static int nmi_setup(void) |
| 142 | { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 143 | int err = 0; |
Andi Kleen | 6c977aa | 2007-05-21 14:31:45 +0200 | [diff] [blame] | 144 | int cpu; |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!allocate_msrs()) |
| 147 | return -ENOMEM; |
| 148 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 149 | err = register_die_notifier(&profile_exceptions_nb); |
| 150 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | free_msrs(); |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 152 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 154 | |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 155 | /* We need to serialize save and setup for HT because the subset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * of msrs are distinct for save and setup operations |
| 157 | */ |
Andi Kleen | 6c977aa | 2007-05-21 14:31:45 +0200 | [diff] [blame] | 158 | |
| 159 | /* Assume saved/restored counters are the same on all CPUs */ |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 160 | model->fill_in_addresses(&per_cpu(cpu_msrs, 0)); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 161 | for_each_possible_cpu(cpu) { |
Chris Wright | 0939c17 | 2007-06-01 00:46:39 -0700 | [diff] [blame] | 162 | if (cpu != 0) { |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 163 | memcpy(per_cpu(cpu_msrs, cpu).counters, |
| 164 | per_cpu(cpu_msrs, 0).counters, |
Chris Wright | 0939c17 | 2007-06-01 00:46:39 -0700 | [diff] [blame] | 165 | sizeof(struct op_msr) * model->num_counters); |
| 166 | |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 167 | memcpy(per_cpu(cpu_msrs, cpu).controls, |
| 168 | per_cpu(cpu_msrs, 0).controls, |
Chris Wright | 0939c17 | 2007-06-01 00:46:39 -0700 | [diff] [blame] | 169 | sizeof(struct op_msr) * model->num_controls); |
| 170 | } |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 171 | |
Andi Kleen | 6c977aa | 2007-05-21 14:31:45 +0200 | [diff] [blame] | 172 | } |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 173 | on_each_cpu(nmi_save_registers, NULL, 1); |
| 174 | on_each_cpu(nmi_cpu_setup, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | nmi_enabled = 1; |
| 176 | return 0; |
| 177 | } |
| 178 | |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 179 | static void nmi_restore_registers(struct op_msrs *msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | unsigned int const nr_ctrs = model->num_counters; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 182 | unsigned int const nr_ctrls = model->num_controls; |
| 183 | struct op_msr *counters = msrs->counters; |
| 184 | struct op_msr *controls = msrs->controls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | unsigned int i; |
| 186 | |
| 187 | for (i = 0; i < nr_ctrls; ++i) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 188 | if (controls[i].addr) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 189 | wrmsr(controls[i].addr, |
| 190 | controls[i].saved.low, |
| 191 | controls[i].saved.high); |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | for (i = 0; i < nr_ctrs; ++i) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 196 | if (counters[i].addr) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 197 | wrmsr(counters[i].addr, |
| 198 | counters[i].saved.low, |
| 199 | counters[i].saved.high); |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 204 | static void nmi_cpu_shutdown(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | unsigned int v; |
| 207 | int cpu = smp_processor_id(); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 208 | struct op_msrs *msrs = &__get_cpu_var(cpu_msrs); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* restoring APIC_LVTPC can trigger an apic error because the delivery |
| 211 | * mode and vector nr combination can be illegal. That's by design: on |
| 212 | * power on apic lvt contain a zero vector nr which are legal only for |
| 213 | * NMI delivery mode. So inhibit apic err before restoring lvtpc |
| 214 | */ |
| 215 | v = apic_read(APIC_LVTERR); |
| 216 | apic_write(APIC_LVTERR, v | APIC_LVT_MASKED); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 217 | apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | apic_write(APIC_LVTERR, v); |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 219 | nmi_restore_registers(msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | static void nmi_shutdown(void) |
| 223 | { |
Andrea Righi | b61e06f | 2008-09-20 18:02:27 +0200 | [diff] [blame] | 224 | struct op_msrs *msrs; |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | nmi_enabled = 0; |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 227 | on_each_cpu(nmi_cpu_shutdown, NULL, 1); |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 228 | unregister_die_notifier(&profile_exceptions_nb); |
Andrea Righi | b61e06f | 2008-09-20 18:02:27 +0200 | [diff] [blame] | 229 | msrs = &get_cpu_var(cpu_msrs); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 230 | model->shutdown(msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | free_msrs(); |
Vegard Nossum | 93e1ade | 2008-06-22 09:40:18 +0200 | [diff] [blame] | 232 | put_cpu_var(cpu_msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 235 | static void nmi_cpu_start(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 237 | struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | model->start(msrs); |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | static int nmi_start(void) |
| 242 | { |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 243 | on_each_cpu(nmi_cpu_start, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 246 | |
| 247 | static void nmi_cpu_stop(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 249 | struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | model->stop(msrs); |
| 251 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | static void nmi_stop(void) |
| 254 | { |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 255 | on_each_cpu(nmi_cpu_stop, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | struct op_counter_config counter_config[OP_MAX_COUNTER]; |
| 259 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 260 | static int nmi_create_files(struct super_block *sb, struct dentry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | unsigned int i; |
| 263 | |
| 264 | for (i = 0; i < model->num_counters; ++i) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 265 | struct dentry *dir; |
Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 266 | char buf[4]; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 267 | |
| 268 | /* quick little hack to _not_ expose a counter if it is not |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 269 | * available for use. This should protect userspace app. |
| 270 | * NOTE: assumes 1:1 mapping here (that counters are organized |
| 271 | * sequentially in their struct assignment). |
| 272 | */ |
| 273 | if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i))) |
| 274 | continue; |
| 275 | |
Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 276 | snprintf(buf, sizeof(buf), "%d", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | dir = oprofilefs_mkdir(sb, root, buf); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 278 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); |
| 279 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); |
| 280 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); |
| 281 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); |
| 282 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); |
| 283 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 288 | |
Robert Richter | 69046d4 | 2008-09-05 12:17:40 +0200 | [diff] [blame] | 289 | #ifdef CONFIG_SMP |
| 290 | static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action, |
| 291 | void *data) |
| 292 | { |
| 293 | int cpu = (unsigned long)data; |
| 294 | switch (action) { |
| 295 | case CPU_DOWN_FAILED: |
| 296 | case CPU_ONLINE: |
| 297 | smp_call_function_single(cpu, nmi_cpu_start, NULL, 0); |
| 298 | break; |
| 299 | case CPU_DOWN_PREPARE: |
| 300 | smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1); |
| 301 | break; |
| 302 | } |
| 303 | return NOTIFY_DONE; |
| 304 | } |
| 305 | |
| 306 | static struct notifier_block oprofile_cpu_nb = { |
| 307 | .notifier_call = oprofile_cpu_notifier |
| 308 | }; |
| 309 | #endif |
| 310 | |
| 311 | #ifdef CONFIG_PM |
| 312 | |
| 313 | static int nmi_suspend(struct sys_device *dev, pm_message_t state) |
| 314 | { |
| 315 | /* Only one CPU left, just stop that one */ |
| 316 | if (nmi_enabled == 1) |
| 317 | nmi_cpu_stop(NULL); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int nmi_resume(struct sys_device *dev) |
| 322 | { |
| 323 | if (nmi_enabled == 1) |
| 324 | nmi_cpu_start(NULL); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static struct sysdev_class oprofile_sysclass = { |
| 329 | .name = "oprofile", |
| 330 | .resume = nmi_resume, |
| 331 | .suspend = nmi_suspend, |
| 332 | }; |
| 333 | |
| 334 | static struct sys_device device_oprofile = { |
| 335 | .id = 0, |
| 336 | .cls = &oprofile_sysclass, |
| 337 | }; |
| 338 | |
| 339 | static int __init init_sysfs(void) |
| 340 | { |
| 341 | int error; |
| 342 | |
| 343 | error = sysdev_class_register(&oprofile_sysclass); |
| 344 | if (!error) |
| 345 | error = sysdev_register(&device_oprofile); |
| 346 | return error; |
| 347 | } |
| 348 | |
| 349 | static void exit_sysfs(void) |
| 350 | { |
| 351 | sysdev_unregister(&device_oprofile); |
| 352 | sysdev_class_unregister(&oprofile_sysclass); |
| 353 | } |
| 354 | |
| 355 | #else |
| 356 | #define init_sysfs() do { } while (0) |
| 357 | #define exit_sysfs() do { } while (0) |
| 358 | #endif /* CONFIG_PM */ |
| 359 | |
Andi Kleen | 1cfcea1 | 2006-07-10 17:06:21 +0200 | [diff] [blame] | 360 | static int p4force; |
| 361 | module_param(p4force, int, 0); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 362 | |
| 363 | static int __init p4_init(char **cpu_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | __u8 cpu_model = boot_cpu_data.x86_model; |
| 366 | |
Andi Kleen | 1cfcea1 | 2006-07-10 17:06:21 +0200 | [diff] [blame] | 367 | if (!p4force && (cpu_model > 6 || cpu_model == 5)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | |
| 370 | #ifndef CONFIG_SMP |
| 371 | *cpu_type = "i386/p4"; |
| 372 | model = &op_p4_spec; |
| 373 | return 1; |
| 374 | #else |
| 375 | switch (smp_num_siblings) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 376 | case 1: |
| 377 | *cpu_type = "i386/p4"; |
| 378 | model = &op_p4_spec; |
| 379 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 381 | case 2: |
| 382 | *cpu_type = "i386/p4-ht"; |
| 383 | model = &op_p4_ht2_spec; |
| 384 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | #endif |
| 387 | |
| 388 | printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n"); |
| 389 | printk(KERN_INFO "oprofile: Reverting to timer mode.\n"); |
| 390 | return 0; |
| 391 | } |
| 392 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 393 | static int __init ppro_init(char **cpu_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | __u8 cpu_model = boot_cpu_data.x86_model; |
| 396 | |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 397 | switch (cpu_model) { |
| 398 | case 0 ... 2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | *cpu_type = "i386/ppro"; |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 400 | break; |
| 401 | case 3 ... 5: |
| 402 | *cpu_type = "i386/pii"; |
| 403 | break; |
| 404 | case 6 ... 8: |
William Cohen | 3d337c6 | 2008-11-30 15:39:10 -0500 | [diff] [blame] | 405 | case 10 ... 11: |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 406 | *cpu_type = "i386/piii"; |
| 407 | break; |
| 408 | case 9: |
William Cohen | 3d337c6 | 2008-11-30 15:39:10 -0500 | [diff] [blame] | 409 | case 13: |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 410 | *cpu_type = "i386/p6_mobile"; |
| 411 | break; |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 412 | case 14: |
| 413 | *cpu_type = "i386/core"; |
| 414 | break; |
| 415 | case 15: case 23: |
| 416 | *cpu_type = "i386/core_2"; |
| 417 | break; |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 418 | default: |
| 419 | /* Unknown */ |
| 420 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | model = &op_ppro_spec; |
| 424 | return 1; |
| 425 | } |
| 426 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 427 | static int __init arch_perfmon_init(char **cpu_type) |
| 428 | { |
| 429 | if (!cpu_has_arch_perfmon) |
| 430 | return 0; |
| 431 | *cpu_type = "i386/arch_perfmon"; |
| 432 | model = &op_arch_perfmon_spec; |
| 433 | arch_perfmon_setup_counters(); |
| 434 | return 1; |
| 435 | } |
| 436 | |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 437 | /* in order to get sysfs right */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | static int using_nmi; |
| 439 | |
David Gibson | 96d0821 | 2005-09-06 15:17:26 -0700 | [diff] [blame] | 440 | int __init op_nmi_init(struct oprofile_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | __u8 vendor = boot_cpu_data.x86_vendor; |
| 443 | __u8 family = boot_cpu_data.x86; |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 444 | char *cpu_type = NULL; |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 445 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | if (!cpu_has_apic) |
| 448 | return -ENODEV; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | switch (vendor) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 451 | case X86_VENDOR_AMD: |
| 452 | /* Needs to be at least an Athlon (or hammer in 32bit mode) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 454 | switch (family) { |
| 455 | default: |
| 456 | return -ENODEV; |
| 457 | case 6: |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 458 | model = &op_amd_spec; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 459 | cpu_type = "i386/athlon"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | break; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 461 | case 0xf: |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 462 | model = &op_amd_spec; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 463 | /* Actually it could be i386/hammer too, but give |
| 464 | user space an consistent name. */ |
| 465 | cpu_type = "x86-64/hammer"; |
| 466 | break; |
| 467 | case 0x10: |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 468 | model = &op_amd_spec; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 469 | cpu_type = "x86-64/family10"; |
| 470 | break; |
Barry Kasindorf | 12f2b26 | 2008-07-22 21:08:47 +0200 | [diff] [blame] | 471 | case 0x11: |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 472 | model = &op_amd_spec; |
Barry Kasindorf | 12f2b26 | 2008-07-22 21:08:47 +0200 | [diff] [blame] | 473 | cpu_type = "x86-64/family11h"; |
| 474 | break; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 475 | } |
| 476 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 478 | case X86_VENDOR_INTEL: |
| 479 | switch (family) { |
| 480 | /* Pentium IV */ |
| 481 | case 0xf: |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 482 | p4_init(&cpu_type); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 483 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 485 | /* A P6-class processor */ |
| 486 | case 6: |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 487 | ppro_init(&cpu_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | break; |
| 489 | |
| 490 | default: |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 491 | break; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 492 | } |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 493 | |
| 494 | if (!cpu_type && !arch_perfmon_init(&cpu_type)) |
| 495 | return -ENODEV; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | default: |
| 499 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 502 | #ifdef CONFIG_SMP |
| 503 | register_cpu_notifier(&oprofile_cpu_nb); |
| 504 | #endif |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 505 | /* default values, can be overwritten by model */ |
| 506 | ops->create_files = nmi_create_files; |
| 507 | ops->setup = nmi_setup; |
| 508 | ops->shutdown = nmi_shutdown; |
| 509 | ops->start = nmi_start; |
| 510 | ops->stop = nmi_stop; |
| 511 | ops->cpu_type = cpu_type; |
| 512 | |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 513 | if (model->init) |
| 514 | ret = model->init(ops); |
| 515 | if (ret) |
| 516 | return ret; |
| 517 | |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 518 | init_sysfs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | using_nmi = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | printk(KERN_INFO "oprofile: using NMI interrupt.\n"); |
| 521 | return 0; |
| 522 | } |
| 523 | |
David Gibson | 96d0821 | 2005-09-06 15:17:26 -0700 | [diff] [blame] | 524 | void op_nmi_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 526 | if (using_nmi) { |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 527 | exit_sysfs(); |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 528 | #ifdef CONFIG_SMP |
| 529 | unregister_cpu_notifier(&oprofile_cpu_nb); |
| 530 | #endif |
| 531 | } |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 532 | if (model->exit) |
| 533 | model->exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |