4 * @remark Copyright 2002-2008 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 * @author Robert Richter <robert.richter@amd.com>
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>
17 #include <linux/moduleparam.h>
18 #include <linux/kdebug.h>
19 #include <linux/cpu.h>
24 #include "op_counter.h"
25 #include "op_x86_model.h"
27 static struct op_x86_model_spec const *model;
28 static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
29 static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
31 /* 0 == registered but off, 1 == registered and on */
32 static int nmi_enabled = 0;
34 static int profile_exceptions_notify(struct notifier_block *self,
35 unsigned long val, void *data)
37 struct die_args *args = (struct die_args *)data;
38 int ret = NOTIFY_DONE;
39 int cpu = smp_processor_id();
44 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
53 static void nmi_cpu_save_registers(struct op_msrs *msrs)
55 unsigned int const nr_ctrs = model->num_counters;
56 unsigned int const nr_ctrls = model->num_controls;
57 struct op_msr *counters = msrs->counters;
58 struct op_msr *controls = msrs->controls;
61 for (i = 0; i < nr_ctrs; ++i) {
62 if (counters[i].addr) {
63 rdmsr(counters[i].addr,
64 counters[i].saved.low,
65 counters[i].saved.high);
69 for (i = 0; i < nr_ctrls; ++i) {
70 if (controls[i].addr) {
71 rdmsr(controls[i].addr,
72 controls[i].saved.low,
73 controls[i].saved.high);
78 static void nmi_save_registers(void *dummy)
80 int cpu = smp_processor_id();
81 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
82 nmi_cpu_save_registers(msrs);
85 static void free_msrs(void)
88 for_each_possible_cpu(i) {
89 kfree(per_cpu(cpu_msrs, i).counters);
90 per_cpu(cpu_msrs, i).counters = NULL;
91 kfree(per_cpu(cpu_msrs, i).controls);
92 per_cpu(cpu_msrs, i).controls = NULL;
96 static int allocate_msrs(void)
99 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
100 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
103 for_each_possible_cpu(i) {
104 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
106 if (!per_cpu(cpu_msrs, i).counters) {
110 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
112 if (!per_cpu(cpu_msrs, i).controls) {
124 static void nmi_cpu_setup(void *dummy)
126 int cpu = smp_processor_id();
127 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
128 spin_lock(&oprofilefs_lock);
129 model->setup_ctrs(msrs);
130 spin_unlock(&oprofilefs_lock);
131 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
132 apic_write(APIC_LVTPC, APIC_DM_NMI);
135 static struct notifier_block profile_exceptions_nb = {
136 .notifier_call = profile_exceptions_notify,
141 static int nmi_setup(void)
146 if (!allocate_msrs())
149 err = register_die_notifier(&profile_exceptions_nb);
155 /* We need to serialize save and setup for HT because the subset
156 * of msrs are distinct for save and setup operations
159 /* Assume saved/restored counters are the same on all CPUs */
160 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
161 for_each_possible_cpu(cpu) {
163 memcpy(per_cpu(cpu_msrs, cpu).counters,
164 per_cpu(cpu_msrs, 0).counters,
165 sizeof(struct op_msr) * model->num_counters);
167 memcpy(per_cpu(cpu_msrs, cpu).controls,
168 per_cpu(cpu_msrs, 0).controls,
169 sizeof(struct op_msr) * model->num_controls);
173 on_each_cpu(nmi_save_registers, NULL, 1);
174 on_each_cpu(nmi_cpu_setup, NULL, 1);
179 static void nmi_restore_registers(struct op_msrs *msrs)
181 unsigned int const nr_ctrs = model->num_counters;
182 unsigned int const nr_ctrls = model->num_controls;
183 struct op_msr *counters = msrs->counters;
184 struct op_msr *controls = msrs->controls;
187 for (i = 0; i < nr_ctrls; ++i) {
188 if (controls[i].addr) {
189 wrmsr(controls[i].addr,
190 controls[i].saved.low,
191 controls[i].saved.high);
195 for (i = 0; i < nr_ctrs; ++i) {
196 if (counters[i].addr) {
197 wrmsr(counters[i].addr,
198 counters[i].saved.low,
199 counters[i].saved.high);
204 static void nmi_cpu_shutdown(void *dummy)
207 int cpu = smp_processor_id();
208 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
210 /* restoring APIC_LVTPC can trigger an apic error because the delivery
211 * mode and vector nr combination can be illegal. That's by design: on
212 * power on apic lvt contain a zero vector nr which are legal only for
213 * NMI delivery mode. So inhibit apic err before restoring lvtpc
215 v = apic_read(APIC_LVTERR);
216 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
217 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
218 apic_write(APIC_LVTERR, v);
219 nmi_restore_registers(msrs);
222 static void nmi_shutdown(void)
224 struct op_msrs *msrs;
227 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
228 unregister_die_notifier(&profile_exceptions_nb);
229 msrs = &get_cpu_var(cpu_msrs);
230 model->shutdown(msrs);
232 put_cpu_var(cpu_msrs);
235 static void nmi_cpu_start(void *dummy)
237 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
241 static int nmi_start(void)
243 on_each_cpu(nmi_cpu_start, NULL, 1);
247 static void nmi_cpu_stop(void *dummy)
249 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
253 static void nmi_stop(void)
255 on_each_cpu(nmi_cpu_stop, NULL, 1);
258 struct op_counter_config counter_config[OP_MAX_COUNTER];
260 static int nmi_create_files(struct super_block *sb, struct dentry *root)
264 for (i = 0; i < model->num_counters; ++i) {
268 /* quick little hack to _not_ expose a counter if it is not
269 * available for use. This should protect userspace app.
270 * NOTE: assumes 1:1 mapping here (that counters are organized
271 * sequentially in their struct assignment).
273 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
276 snprintf(buf, sizeof(buf), "%d", i);
277 dir = oprofilefs_mkdir(sb, root, buf);
278 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
279 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
280 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
281 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
282 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
283 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
290 static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
293 int cpu = (unsigned long)data;
295 case CPU_DOWN_FAILED:
297 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
299 case CPU_DOWN_PREPARE:
300 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
306 static struct notifier_block oprofile_cpu_nb = {
307 .notifier_call = oprofile_cpu_notifier
313 static int nmi_suspend(struct sys_device *dev, pm_message_t state)
315 /* Only one CPU left, just stop that one */
316 if (nmi_enabled == 1)
321 static int nmi_resume(struct sys_device *dev)
323 if (nmi_enabled == 1)
328 static struct sysdev_class oprofile_sysclass = {
330 .resume = nmi_resume,
331 .suspend = nmi_suspend,
334 static struct sys_device device_oprofile = {
336 .cls = &oprofile_sysclass,
339 static int __init init_sysfs(void)
343 error = sysdev_class_register(&oprofile_sysclass);
345 error = sysdev_register(&device_oprofile);
349 static void exit_sysfs(void)
351 sysdev_unregister(&device_oprofile);
352 sysdev_class_unregister(&oprofile_sysclass);
356 #define init_sysfs() do { } while (0)
357 #define exit_sysfs() do { } while (0)
358 #endif /* CONFIG_PM */
361 module_param(p4force, int, 0);
363 static int __init p4_init(char **cpu_type)
365 __u8 cpu_model = boot_cpu_data.x86_model;
367 if (!p4force && (cpu_model > 6 || cpu_model == 5))
371 *cpu_type = "i386/p4";
375 switch (smp_num_siblings) {
377 *cpu_type = "i386/p4";
382 *cpu_type = "i386/p4-ht";
383 model = &op_p4_ht2_spec;
388 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
389 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
393 static int __init ppro_init(char **cpu_type)
395 __u8 cpu_model = boot_cpu_data.x86_model;
399 *cpu_type = "i386/ppro";
402 *cpu_type = "i386/pii";
406 *cpu_type = "i386/piii";
410 *cpu_type = "i386/p6_mobile";
413 *cpu_type = "i386/core";
416 *cpu_type = "i386/core_2";
423 model = &op_ppro_spec;
427 static int __init arch_perfmon_init(char **cpu_type)
429 if (!cpu_has_arch_perfmon)
431 *cpu_type = "i386/arch_perfmon";
432 model = &op_arch_perfmon_spec;
433 arch_perfmon_setup_counters();
437 /* in order to get sysfs right */
438 static int using_nmi;
440 int __init op_nmi_init(struct oprofile_operations *ops)
442 __u8 vendor = boot_cpu_data.x86_vendor;
443 __u8 family = boot_cpu_data.x86;
444 char *cpu_type = NULL;
452 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
458 model = &op_amd_spec;
459 cpu_type = "i386/athlon";
462 model = &op_amd_spec;
463 /* Actually it could be i386/hammer too, but give
464 user space an consistent name. */
465 cpu_type = "x86-64/hammer";
468 model = &op_amd_spec;
469 cpu_type = "x86-64/family10";
472 model = &op_amd_spec;
473 cpu_type = "x86-64/family11h";
478 case X86_VENDOR_INTEL:
485 /* A P6-class processor */
487 ppro_init(&cpu_type);
494 if (!cpu_type && !arch_perfmon_init(&cpu_type))
503 register_cpu_notifier(&oprofile_cpu_nb);
505 /* default values, can be overwritten by model */
506 ops->create_files = nmi_create_files;
507 ops->setup = nmi_setup;
508 ops->shutdown = nmi_shutdown;
509 ops->start = nmi_start;
510 ops->stop = nmi_stop;
511 ops->cpu_type = cpu_type;
514 ret = model->init(ops);
520 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
524 void op_nmi_exit(void)
529 unregister_cpu_notifier(&oprofile_cpu_nb);