| 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; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static struct op_msrs cpu_msrs[NR_CPUS]; | 
|  | 27 | static unsigned long saved_lvtpc[NR_CPUS]; | 
| 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: | 
|  | 92 | if (model->check_ctrs(args->regs, &cpu_msrs[cpu])) | 
|  | 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(); | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 129 | struct op_msrs *msrs = &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 | c891259 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 136 | for_each_possible_cpu(i) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | kfree(cpu_msrs[i].counters); | 
|  | 138 | cpu_msrs[i].counters = NULL; | 
|  | 139 | kfree(cpu_msrs[i].controls); | 
|  | 140 | cpu_msrs[i].controls = NULL; | 
|  | 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) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | cpu_msrs[i].counters = kmalloc(counters_size, GFP_KERNEL); | 
|  | 153 | if (!cpu_msrs[i].counters) { | 
|  | 154 | success = 0; | 
|  | 155 | break; | 
|  | 156 | } | 
|  | 157 | cpu_msrs[i].controls = kmalloc(controls_size, GFP_KERNEL); | 
|  | 158 | if (!cpu_msrs[i].controls) { | 
|  | 159 | success = 0; | 
|  | 160 | break; | 
|  | 161 | } | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | if (!success) | 
|  | 165 | free_msrs(); | 
|  | 166 |  | 
|  | 167 | return success; | 
|  | 168 | } | 
|  | 169 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 170 | static void nmi_cpu_setup(void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { | 
|  | 172 | int cpu = smp_processor_id(); | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 173 | struct op_msrs *msrs = &cpu_msrs[cpu]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | spin_lock(&oprofilefs_lock); | 
|  | 175 | model->setup_ctrs(msrs); | 
|  | 176 | spin_unlock(&oprofilefs_lock); | 
|  | 177 | saved_lvtpc[cpu] = apic_read(APIC_LVTPC); | 
|  | 178 | apic_write(APIC_LVTPC, APIC_DM_NMI); | 
|  | 179 | } | 
|  | 180 |  | 
| Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 181 | static struct notifier_block profile_exceptions_nb = { | 
|  | 182 | .notifier_call = profile_exceptions_notify, | 
|  | 183 | .next = NULL, | 
|  | 184 | .priority = 0 | 
|  | 185 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | static int nmi_setup(void) | 
|  | 188 | { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 189 | int err = 0; | 
| Andi Kleen | 6c977aa | 2007-05-21 14:31:45 +0200 | [diff] [blame] | 190 | int cpu; | 
| Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 191 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (!allocate_msrs()) | 
|  | 193 | return -ENOMEM; | 
|  | 194 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 195 | err = register_die_notifier(&profile_exceptions_nb); | 
|  | 196 | if (err) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | free_msrs(); | 
| Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 198 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } | 
| Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 200 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* We need to serialize save and setup for HT because the subset | 
|  | 202 | * of msrs are distinct for save and setup operations | 
|  | 203 | */ | 
| Andi Kleen | 6c977aa | 2007-05-21 14:31:45 +0200 | [diff] [blame] | 204 |  | 
|  | 205 | /* Assume saved/restored counters are the same on all CPUs */ | 
|  | 206 | model->fill_in_addresses(&cpu_msrs[0]); | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 207 | for_each_possible_cpu(cpu) { | 
| Chris Wright | 0939c17 | 2007-06-01 00:46:39 -0700 | [diff] [blame] | 208 | if (cpu != 0) { | 
|  | 209 | memcpy(cpu_msrs[cpu].counters, cpu_msrs[0].counters, | 
|  | 210 | sizeof(struct op_msr) * model->num_counters); | 
|  | 211 |  | 
|  | 212 | memcpy(cpu_msrs[cpu].controls, cpu_msrs[0].controls, | 
|  | 213 | sizeof(struct op_msr) * model->num_controls); | 
|  | 214 | } | 
|  | 215 |  | 
| Andi Kleen | 6c977aa | 2007-05-21 14:31:45 +0200 | [diff] [blame] | 216 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | on_each_cpu(nmi_save_registers, NULL, 0, 1); | 
|  | 218 | on_each_cpu(nmi_cpu_setup, NULL, 0, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | nmi_enabled = 1; | 
|  | 220 | return 0; | 
|  | 221 | } | 
|  | 222 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 223 | static void nmi_restore_registers(struct op_msrs *msrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { | 
|  | 225 | unsigned int const nr_ctrs = model->num_counters; | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 226 | unsigned int const nr_ctrls = model->num_controls; | 
|  | 227 | struct op_msr *counters = msrs->counters; | 
|  | 228 | struct op_msr *controls = msrs->controls; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | unsigned int i; | 
|  | 230 |  | 
|  | 231 | for (i = 0; i < nr_ctrls; ++i) { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 232 | if (controls[i].addr) { | 
| Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 233 | wrmsr(controls[i].addr, | 
|  | 234 | controls[i].saved.low, | 
|  | 235 | controls[i].saved.high); | 
|  | 236 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 238 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | for (i = 0; i < nr_ctrs; ++i) { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 240 | if (counters[i].addr) { | 
| Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 241 | wrmsr(counters[i].addr, | 
|  | 242 | counters[i].saved.low, | 
|  | 243 | counters[i].saved.high); | 
|  | 244 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } | 
|  | 246 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 248 | static void nmi_cpu_shutdown(void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { | 
|  | 250 | unsigned int v; | 
|  | 251 | int cpu = smp_processor_id(); | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 252 | struct op_msrs *msrs = &cpu_msrs[cpu]; | 
|  | 253 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /* restoring APIC_LVTPC can trigger an apic error because the delivery | 
|  | 255 | * mode and vector nr combination can be illegal. That's by design: on | 
|  | 256 | * power on apic lvt contain a zero vector nr which are legal only for | 
|  | 257 | * NMI delivery mode. So inhibit apic err before restoring lvtpc | 
|  | 258 | */ | 
|  | 259 | v = apic_read(APIC_LVTERR); | 
|  | 260 | apic_write(APIC_LVTERR, v | APIC_LVT_MASKED); | 
|  | 261 | apic_write(APIC_LVTPC, saved_lvtpc[cpu]); | 
|  | 262 | apic_write(APIC_LVTERR, v); | 
|  | 263 | nmi_restore_registers(msrs); | 
|  | 264 | } | 
|  | 265 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | static void nmi_shutdown(void) | 
|  | 267 | { | 
|  | 268 | nmi_enabled = 0; | 
|  | 269 | on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1); | 
| Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 270 | unregister_die_notifier(&profile_exceptions_nb); | 
| Stephane Eranian | 0f8e45a | 2007-10-17 18:04:32 +0200 | [diff] [blame] | 271 | model->shutdown(cpu_msrs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | free_msrs(); | 
|  | 273 | } | 
|  | 274 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 275 | static void nmi_cpu_start(void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 277 | struct op_msrs const *msrs = &cpu_msrs[smp_processor_id()]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | model->start(msrs); | 
|  | 279 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 |  | 
|  | 281 | static int nmi_start(void) | 
|  | 282 | { | 
|  | 283 | on_each_cpu(nmi_cpu_start, NULL, 0, 1); | 
|  | 284 | return 0; | 
|  | 285 | } | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 286 |  | 
|  | 287 | static void nmi_cpu_stop(void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 289 | struct op_msrs const *msrs = &cpu_msrs[smp_processor_id()]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | model->stop(msrs); | 
|  | 291 | } | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 292 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | static void nmi_stop(void) | 
|  | 294 | { | 
|  | 295 | on_each_cpu(nmi_cpu_stop, NULL, 0, 1); | 
|  | 296 | } | 
|  | 297 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | struct op_counter_config counter_config[OP_MAX_COUNTER]; | 
|  | 299 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 300 | static int nmi_create_files(struct super_block *sb, struct dentry *root) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { | 
|  | 302 | unsigned int i; | 
|  | 303 |  | 
|  | 304 | for (i = 0; i < model->num_counters; ++i) { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 305 | struct dentry *dir; | 
| Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 306 | char buf[4]; | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 307 |  | 
|  | 308 | /* quick little hack to _not_ expose a counter if it is not | 
| Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 309 | * available for use.  This should protect userspace app. | 
|  | 310 | * NOTE:  assumes 1:1 mapping here (that counters are organized | 
|  | 311 | *        sequentially in their struct assignment). | 
|  | 312 | */ | 
|  | 313 | if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i))) | 
|  | 314 | continue; | 
|  | 315 |  | 
| Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 316 | snprintf(buf,  sizeof(buf), "%d", i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | dir = oprofilefs_mkdir(sb, root, buf); | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 318 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); | 
|  | 319 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); | 
|  | 320 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); | 
|  | 321 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); | 
|  | 322 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); | 
|  | 323 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | return 0; | 
|  | 327 | } | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 328 |  | 
| Andi Kleen | 1cfcea1 | 2006-07-10 17:06:21 +0200 | [diff] [blame] | 329 | static int p4force; | 
|  | 330 | module_param(p4force, int, 0); | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 331 |  | 
|  | 332 | static int __init p4_init(char **cpu_type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { | 
|  | 334 | __u8 cpu_model = boot_cpu_data.x86_model; | 
|  | 335 |  | 
| Andi Kleen | 1cfcea1 | 2006-07-10 17:06:21 +0200 | [diff] [blame] | 336 | if (!p4force && (cpu_model > 6 || cpu_model == 5)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return 0; | 
|  | 338 |  | 
|  | 339 | #ifndef CONFIG_SMP | 
|  | 340 | *cpu_type = "i386/p4"; | 
|  | 341 | model = &op_p4_spec; | 
|  | 342 | return 1; | 
|  | 343 | #else | 
|  | 344 | switch (smp_num_siblings) { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 345 | case 1: | 
|  | 346 | *cpu_type = "i386/p4"; | 
|  | 347 | model = &op_p4_spec; | 
|  | 348 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 350 | case 2: | 
|  | 351 | *cpu_type = "i386/p4-ht"; | 
|  | 352 | model = &op_p4_ht2_spec; | 
|  | 353 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } | 
|  | 355 | #endif | 
|  | 356 |  | 
|  | 357 | printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n"); | 
|  | 358 | printk(KERN_INFO "oprofile: Reverting to timer mode.\n"); | 
|  | 359 | return 0; | 
|  | 360 | } | 
|  | 361 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 362 | static int __init ppro_init(char **cpu_type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { | 
|  | 364 | __u8 cpu_model = boot_cpu_data.x86_model; | 
|  | 365 |  | 
| Benjamin LaHaise | 64471eb | 2006-05-15 09:44:24 -0700 | [diff] [blame] | 366 | if (cpu_model == 14) | 
|  | 367 | *cpu_type = "i386/core"; | 
| Arjan van de Ven | e107ebe | 2008-01-18 22:49:33 +0100 | [diff] [blame] | 368 | else if (cpu_model == 15 || cpu_model == 23) | 
| Benjamin LaHaise | f04b92e | 2006-09-16 16:35:46 -0700 | [diff] [blame] | 369 | *cpu_type = "i386/core_2"; | 
| Benjamin LaHaise | 64471eb | 2006-05-15 09:44:24 -0700 | [diff] [blame] | 370 | else if (cpu_model > 0xd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return 0; | 
| Benjamin LaHaise | 64471eb | 2006-05-15 09:44:24 -0700 | [diff] [blame] | 372 | else if (cpu_model == 9) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | *cpu_type = "i386/p6_mobile"; | 
|  | 374 | } else if (cpu_model > 5) { | 
|  | 375 | *cpu_type = "i386/piii"; | 
|  | 376 | } else if (cpu_model > 2) { | 
|  | 377 | *cpu_type = "i386/pii"; | 
|  | 378 | } else { | 
|  | 379 | *cpu_type = "i386/ppro"; | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | model = &op_ppro_spec; | 
|  | 383 | return 1; | 
|  | 384 | } | 
|  | 385 |  | 
| Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 386 | /* in order to get sysfs right */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | static int using_nmi; | 
|  | 388 |  | 
| David Gibson | 96d0821 | 2005-09-06 15:17:26 -0700 | [diff] [blame] | 389 | int __init op_nmi_init(struct oprofile_operations *ops) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { | 
|  | 391 | __u8 vendor = boot_cpu_data.x86_vendor; | 
|  | 392 | __u8 family = boot_cpu_data.x86; | 
|  | 393 | char *cpu_type; | 
|  | 394 |  | 
|  | 395 | if (!cpu_has_apic) | 
|  | 396 | return -ENODEV; | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 397 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | switch (vendor) { | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 399 | case X86_VENDOR_AMD: | 
|  | 400 | /* Needs to be at least an Athlon (or hammer in 32bit mode) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 402 | switch (family) { | 
|  | 403 | default: | 
|  | 404 | return -ENODEV; | 
|  | 405 | case 6: | 
|  | 406 | model = &op_athlon_spec; | 
|  | 407 | cpu_type = "i386/athlon"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | break; | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 409 | case 0xf: | 
|  | 410 | model = &op_athlon_spec; | 
|  | 411 | /* Actually it could be i386/hammer too, but give | 
|  | 412 | user space an consistent name. */ | 
|  | 413 | cpu_type = "x86-64/hammer"; | 
|  | 414 | break; | 
|  | 415 | case 0x10: | 
|  | 416 | model = &op_athlon_spec; | 
|  | 417 | cpu_type = "x86-64/family10"; | 
|  | 418 | break; | 
|  | 419 | } | 
|  | 420 | break; | 
| 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 | case X86_VENDOR_INTEL: | 
|  | 423 | switch (family) { | 
|  | 424 | /* Pentium IV */ | 
|  | 425 | case 0xf: | 
|  | 426 | if (!p4_init(&cpu_type)) | 
|  | 427 | return -ENODEV; | 
|  | 428 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 |  | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 430 | /* A P6-class processor */ | 
|  | 431 | case 6: | 
|  | 432 | if (!ppro_init(&cpu_type)) | 
|  | 433 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | break; | 
|  | 435 |  | 
|  | 436 | default: | 
|  | 437 | return -ENODEV; | 
| Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 438 | } | 
|  | 439 | break; | 
|  | 440 |  | 
|  | 441 | default: | 
|  | 442 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } | 
|  | 444 |  | 
| Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 445 | init_sysfs(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | using_nmi = 1; | 
|  | 447 | ops->create_files = nmi_create_files; | 
|  | 448 | ops->setup = nmi_setup; | 
|  | 449 | ops->shutdown = nmi_shutdown; | 
|  | 450 | ops->start = nmi_start; | 
|  | 451 | ops->stop = nmi_stop; | 
|  | 452 | ops->cpu_type = cpu_type; | 
|  | 453 | printk(KERN_INFO "oprofile: using NMI interrupt.\n"); | 
|  | 454 | return 0; | 
|  | 455 | } | 
|  | 456 |  | 
| David Gibson | 96d0821 | 2005-09-06 15:17:26 -0700 | [diff] [blame] | 457 | void op_nmi_exit(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { | 
|  | 459 | if (using_nmi) | 
| Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 460 | exit_sysfs(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |