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