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