4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
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>
20 #include "op_counter.h"
21 #include "op_x86_model.h"
23 static struct op_x86_model_spec const * model;
24 static struct op_msrs cpu_msrs[NR_CPUS];
25 static unsigned long saved_lvtpc[NR_CPUS];
27 static int nmi_start(void);
28 static void nmi_stop(void);
30 /* 0 == registered but off, 1 == registered and on */
31 static int nmi_enabled = 0;
35 static int nmi_suspend(struct sys_device *dev, pm_message_t state)
43 static int nmi_resume(struct sys_device *dev)
51 static struct sysdev_class oprofile_sysclass = {
52 set_kset_name("oprofile"),
54 .suspend = nmi_suspend,
58 static struct sys_device device_oprofile = {
60 .cls = &oprofile_sysclass,
64 static int __init init_driverfs(void)
67 if (!(error = sysdev_class_register(&oprofile_sysclass)))
68 error = sysdev_register(&device_oprofile);
73 static void exit_driverfs(void)
75 sysdev_unregister(&device_oprofile);
76 sysdev_class_unregister(&oprofile_sysclass);
80 #define init_driverfs() do { } while (0)
81 #define exit_driverfs() do { } while (0)
82 #endif /* CONFIG_PM */
85 static int nmi_callback(struct pt_regs * regs, int cpu)
87 return model->check_ctrs(regs, &cpu_msrs[cpu]);
91 static void nmi_cpu_save_registers(struct op_msrs * msrs)
93 unsigned int const nr_ctrs = model->num_counters;
94 unsigned int const nr_ctrls = model->num_controls;
95 struct op_msr * counters = msrs->counters;
96 struct op_msr * controls = msrs->controls;
99 for (i = 0; i < nr_ctrs; ++i) {
100 rdmsr(counters[i].addr,
101 counters[i].saved.low,
102 counters[i].saved.high);
105 for (i = 0; i < nr_ctrls; ++i) {
106 rdmsr(controls[i].addr,
107 controls[i].saved.low,
108 controls[i].saved.high);
113 static void nmi_save_registers(void * dummy)
115 int cpu = smp_processor_id();
116 struct op_msrs * msrs = &cpu_msrs[cpu];
117 model->fill_in_addresses(msrs);
118 nmi_cpu_save_registers(msrs);
122 static void free_msrs(void)
125 for_each_possible_cpu(i) {
126 kfree(cpu_msrs[i].counters);
127 cpu_msrs[i].counters = NULL;
128 kfree(cpu_msrs[i].controls);
129 cpu_msrs[i].controls = NULL;
134 static int allocate_msrs(void)
137 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
138 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
141 for_each_online_cpu(i) {
142 cpu_msrs[i].counters = kmalloc(counters_size, GFP_KERNEL);
143 if (!cpu_msrs[i].counters) {
147 cpu_msrs[i].controls = kmalloc(controls_size, GFP_KERNEL);
148 if (!cpu_msrs[i].controls) {
161 static void nmi_cpu_setup(void * dummy)
163 int cpu = smp_processor_id();
164 struct op_msrs * msrs = &cpu_msrs[cpu];
165 spin_lock(&oprofilefs_lock);
166 model->setup_ctrs(msrs);
167 spin_unlock(&oprofilefs_lock);
168 saved_lvtpc[cpu] = apic_read(APIC_LVTPC);
169 apic_write(APIC_LVTPC, APIC_DM_NMI);
173 static int nmi_setup(void)
175 if (!allocate_msrs())
178 /* We walk a thin line between law and rape here.
179 * We need to be careful to install our NMI handler
180 * without actually triggering any NMIs as this will
181 * break the core code horrifically.
183 if (reserve_lapic_nmi() < 0) {
187 /* We need to serialize save and setup for HT because the subset
188 * of msrs are distinct for save and setup operations
190 on_each_cpu(nmi_save_registers, NULL, 0, 1);
191 on_each_cpu(nmi_cpu_setup, NULL, 0, 1);
192 set_nmi_callback(nmi_callback);
198 static void nmi_restore_registers(struct op_msrs * msrs)
200 unsigned int const nr_ctrs = model->num_counters;
201 unsigned int const nr_ctrls = model->num_controls;
202 struct op_msr * counters = msrs->counters;
203 struct op_msr * controls = msrs->controls;
206 for (i = 0; i < nr_ctrls; ++i) {
207 wrmsr(controls[i].addr,
208 controls[i].saved.low,
209 controls[i].saved.high);
212 for (i = 0; i < nr_ctrs; ++i) {
213 wrmsr(counters[i].addr,
214 counters[i].saved.low,
215 counters[i].saved.high);
220 static void nmi_cpu_shutdown(void * dummy)
223 int cpu = smp_processor_id();
224 struct op_msrs * msrs = &cpu_msrs[cpu];
226 /* restoring APIC_LVTPC can trigger an apic error because the delivery
227 * mode and vector nr combination can be illegal. That's by design: on
228 * power on apic lvt contain a zero vector nr which are legal only for
229 * NMI delivery mode. So inhibit apic err before restoring lvtpc
231 v = apic_read(APIC_LVTERR);
232 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
233 apic_write(APIC_LVTPC, saved_lvtpc[cpu]);
234 apic_write(APIC_LVTERR, v);
235 nmi_restore_registers(msrs);
239 static void nmi_shutdown(void)
242 on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
243 unset_nmi_callback();
249 static void nmi_cpu_start(void * dummy)
251 struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
256 static int nmi_start(void)
258 on_each_cpu(nmi_cpu_start, NULL, 0, 1);
263 static void nmi_cpu_stop(void * dummy)
265 struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
270 static void nmi_stop(void)
272 on_each_cpu(nmi_cpu_stop, NULL, 0, 1);
276 struct op_counter_config counter_config[OP_MAX_COUNTER];
278 static int nmi_create_files(struct super_block * sb, struct dentry * root)
282 for (i = 0; i < model->num_counters; ++i) {
286 snprintf(buf, sizeof(buf), "%d", i);
287 dir = oprofilefs_mkdir(sb, root, buf);
288 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
289 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
290 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
291 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
292 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
293 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
300 static int __init p4_init(char ** cpu_type)
302 __u8 cpu_model = boot_cpu_data.x86_model;
308 *cpu_type = "i386/p4";
312 switch (smp_num_siblings) {
314 *cpu_type = "i386/p4";
319 *cpu_type = "i386/p4-ht";
320 model = &op_p4_ht2_spec;
325 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
326 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
331 static int __init ppro_init(char ** cpu_type)
333 __u8 cpu_model = boot_cpu_data.x86_model;
336 *cpu_type = "i386/core";
337 else if (cpu_model > 0xd)
339 else if (cpu_model == 9) {
340 *cpu_type = "i386/p6_mobile";
341 } else if (cpu_model > 5) {
342 *cpu_type = "i386/piii";
343 } else if (cpu_model > 2) {
344 *cpu_type = "i386/pii";
346 *cpu_type = "i386/ppro";
349 model = &op_ppro_spec;
353 /* in order to get driverfs right */
354 static int using_nmi;
356 int __init op_nmi_init(struct oprofile_operations *ops)
358 __u8 vendor = boot_cpu_data.x86_vendor;
359 __u8 family = boot_cpu_data.x86;
367 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
373 model = &op_athlon_spec;
374 cpu_type = "i386/athlon";
377 model = &op_athlon_spec;
378 /* Actually it could be i386/hammer too, but give
379 user space an consistent name. */
380 cpu_type = "x86-64/hammer";
385 case X86_VENDOR_INTEL:
389 if (!p4_init(&cpu_type))
393 /* A P6-class processor */
395 if (!ppro_init(&cpu_type))
410 ops->create_files = nmi_create_files;
411 ops->setup = nmi_setup;
412 ops->shutdown = nmi_shutdown;
413 ops->start = nmi_start;
414 ops->stop = nmi_stop;
415 ops->cpu_type = cpu_type;
416 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
421 void op_nmi_exit(void)