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