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>
23 #include "op_counter.h"
24 #include "op_x86_model.h"
26 DEFINE_PER_CPU(int, switch_index);
28 static struct op_x86_model_spec const *model;
29 static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
30 static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
32 static int nmi_start(void);
33 static void nmi_stop(void);
34 static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs);
35 static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs);
36 static void nmi_cpu_stop(void *dummy);
37 static void nmi_cpu_start(void *dummy);
39 /* 0 == registered but off, 1 == registered and on */
40 static int nmi_enabled = 0;
44 static int nmi_suspend(struct sys_device *dev, pm_message_t state)
51 static int nmi_resume(struct sys_device *dev)
58 static struct sysdev_class oprofile_sysclass = {
61 .suspend = nmi_suspend,
64 static struct sys_device device_oprofile = {
66 .cls = &oprofile_sysclass,
69 static int __init init_sysfs(void)
73 error = sysdev_class_register(&oprofile_sysclass);
75 error = sysdev_register(&device_oprofile);
79 static void exit_sysfs(void)
81 sysdev_unregister(&device_oprofile);
82 sysdev_class_unregister(&oprofile_sysclass);
86 #define init_sysfs() do { } while (0)
87 #define exit_sysfs() do { } while (0)
88 #endif /* CONFIG_PM */
90 static void nmi_cpu_switch(void *dummy)
92 int cpu = smp_processor_id();
93 int si = per_cpu(switch_index, cpu);
94 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
97 nmi_cpu_save_mpx_registers(msrs);
99 /* move to next set */
100 si += model->num_hardware_counters;
101 if ((si > model->num_counters) || (counter_config[si].count == 0))
102 per_cpu(switch_index, smp_processor_id()) = 0;
104 per_cpu(switch_index, smp_processor_id()) = si;
106 nmi_cpu_restore_mpx_registers(msrs);
107 model->setup_ctrs(msrs);
112 * Quick check to see if multiplexing is necessary.
113 * The check should be sufficient since counters are used
116 static int nmi_multiplex_on(void)
118 return counter_config[model->num_hardware_counters].count ? 0 : -EINVAL;
121 static int nmi_switch_event(void)
123 if (nmi_multiplex_on() < 0)
126 on_each_cpu(nmi_cpu_switch, NULL, 1);
131 static int profile_exceptions_notify(struct notifier_block *self,
132 unsigned long val, void *data)
134 struct die_args *args = (struct die_args *)data;
135 int ret = NOTIFY_DONE;
136 int cpu = smp_processor_id();
140 if (model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)))
149 static void nmi_cpu_save_registers(struct op_msrs *msrs)
151 unsigned int const nr_ctrs = model->num_counters;
152 unsigned int const nr_ctrls = model->num_controls;
153 struct op_msr *counters = msrs->counters;
154 struct op_msr *controls = msrs->controls;
157 for (i = 0; i < nr_ctrs; ++i) {
158 if (counters[i].addr) {
159 rdmsr(counters[i].addr,
160 counters[i].saved.low,
161 counters[i].saved.high);
165 for (i = 0; i < nr_ctrls; ++i) {
166 if (controls[i].addr) {
167 rdmsr(controls[i].addr,
168 controls[i].saved.low,
169 controls[i].saved.high);
174 static void nmi_save_registers(void *dummy)
176 int cpu = smp_processor_id();
177 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
178 nmi_cpu_save_registers(msrs);
181 static void free_msrs(void)
184 for_each_possible_cpu(i) {
185 kfree(per_cpu(cpu_msrs, i).counters);
186 per_cpu(cpu_msrs, i).counters = NULL;
187 kfree(per_cpu(cpu_msrs, i).controls);
188 per_cpu(cpu_msrs, i).controls = NULL;
192 static int allocate_msrs(void)
195 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
196 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
198 for_each_possible_cpu(i) {
199 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
201 if (!per_cpu(cpu_msrs, i).counters) {
205 per_cpu(cpu_msrs, i).controls =
206 kmalloc(controls_size, GFP_KERNEL);
207 if (!per_cpu(cpu_msrs, i).controls) {
219 static void nmi_cpu_setup(void *dummy)
221 int cpu = smp_processor_id();
222 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
223 spin_lock(&oprofilefs_lock);
224 model->setup_ctrs(msrs);
225 spin_unlock(&oprofilefs_lock);
226 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
227 apic_write(APIC_LVTPC, APIC_DM_NMI);
230 static struct notifier_block profile_exceptions_nb = {
231 .notifier_call = profile_exceptions_notify,
236 static int nmi_setup(void)
241 if (!allocate_msrs())
244 err = register_die_notifier(&profile_exceptions_nb);
251 * We need to serialize save and setup for HT because the subset
252 * of msrs are distinct for save and setup operations
255 /* Assume saved/restored counters are the same on all CPUs */
256 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
257 for_each_possible_cpu(cpu) {
259 memcpy(per_cpu(cpu_msrs, cpu).counters,
260 per_cpu(cpu_msrs, 0).counters,
261 sizeof(struct op_msr) * model->num_counters);
263 memcpy(per_cpu(cpu_msrs, cpu).controls,
264 per_cpu(cpu_msrs, 0).controls,
265 sizeof(struct op_msr) * model->num_controls);
268 on_each_cpu(nmi_save_registers, NULL, 1);
269 on_each_cpu(nmi_cpu_setup, NULL, 1);
274 static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
276 unsigned int si = __get_cpu_var(switch_index);
277 unsigned int const nr_ctrs = model->num_hardware_counters;
278 struct op_msr *counters = &msrs->counters[si];
281 for (i = 0; i < nr_ctrs; ++i) {
283 if (counters[offset].addr) {
284 rdmsr(counters[offset].addr,
285 counters[offset].multiplex.low,
286 counters[offset].multiplex.high);
291 static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
293 unsigned int si = __get_cpu_var(switch_index);
294 unsigned int const nr_ctrs = model->num_hardware_counters;
295 struct op_msr *counters = &msrs->counters[si];
298 for (i = 0; i < nr_ctrs; ++i) {
300 if (counters[offset].addr) {
301 wrmsr(counters[offset].addr,
302 counters[offset].multiplex.low,
303 counters[offset].multiplex.high);
308 static void nmi_cpu_restore_registers(struct op_msrs *msrs)
310 unsigned int const nr_ctrs = model->num_counters;
311 unsigned int const nr_ctrls = model->num_controls;
312 struct op_msr *counters = msrs->counters;
313 struct op_msr *controls = msrs->controls;
316 for (i = 0; i < nr_ctrls; ++i) {
317 if (controls[i].addr) {
318 wrmsr(controls[i].addr,
319 controls[i].saved.low,
320 controls[i].saved.high);
324 for (i = 0; i < nr_ctrs; ++i) {
325 if (counters[i].addr) {
326 wrmsr(counters[i].addr,
327 counters[i].saved.low,
328 counters[i].saved.high);
333 static void nmi_cpu_shutdown(void *dummy)
336 int cpu = smp_processor_id();
337 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
339 /* restoring APIC_LVTPC can trigger an apic error because the delivery
340 * mode and vector nr combination can be illegal. That's by design: on
341 * power on apic lvt contain a zero vector nr which are legal only for
342 * NMI delivery mode. So inhibit apic err before restoring lvtpc
344 v = apic_read(APIC_LVTERR);
345 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
346 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
347 apic_write(APIC_LVTERR, v);
348 nmi_cpu_restore_registers(msrs);
349 __get_cpu_var(switch_index) = 0;
352 static void nmi_shutdown(void)
354 struct op_msrs *msrs = &get_cpu_var(cpu_msrs);
356 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
357 unregister_die_notifier(&profile_exceptions_nb);
358 model->shutdown(msrs);
360 put_cpu_var(cpu_msrs);
363 static void nmi_cpu_start(void *dummy)
365 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
369 static int nmi_start(void)
371 on_each_cpu(nmi_cpu_start, NULL, 1);
375 static void nmi_cpu_stop(void *dummy)
377 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
381 static void nmi_stop(void)
383 on_each_cpu(nmi_cpu_stop, NULL, 1);
386 struct op_counter_config counter_config[OP_MAX_COUNTER];
388 static int nmi_create_files(struct super_block *sb, struct dentry *root)
392 for (i = 0; i < model->num_counters; ++i) {
396 /* quick little hack to _not_ expose a counter if it is not
397 * available for use. This should protect userspace app.
398 * NOTE: assumes 1:1 mapping here (that counters are organized
399 * sequentially in their struct assignment).
401 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
404 snprintf(buf, sizeof(buf), "%d", i);
405 dir = oprofilefs_mkdir(sb, root, buf);
406 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
407 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
408 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
409 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
410 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
411 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
412 counter_config[i].save_count_low = 0;
419 module_param(p4force, int, 0);
421 static int __init p4_init(char **cpu_type)
423 __u8 cpu_model = boot_cpu_data.x86_model;
425 if (!p4force && (cpu_model > 6 || cpu_model == 5))
429 *cpu_type = "i386/p4";
433 switch (smp_num_siblings) {
435 *cpu_type = "i386/p4";
440 *cpu_type = "i386/p4-ht";
441 model = &op_p4_ht2_spec;
446 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
447 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
451 static int __init ppro_init(char **cpu_type)
453 __u8 cpu_model = boot_cpu_data.x86_model;
457 *cpu_type = "i386/ppro";
460 *cpu_type = "i386/pii";
463 *cpu_type = "i386/piii";
466 *cpu_type = "i386/p6_mobile";
469 *cpu_type = "i386/p6";
472 *cpu_type = "i386/core";
475 *cpu_type = "i386/core_2";
478 *cpu_type = "i386/core_2";
485 model = &op_ppro_spec;
489 /* in order to get sysfs right */
490 static int using_nmi;
492 int __init op_nmi_init(struct oprofile_operations *ops)
494 __u8 vendor = boot_cpu_data.x86_vendor;
495 __u8 family = boot_cpu_data.x86;
504 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
510 model = &op_amd_spec;
511 cpu_type = "i386/athlon";
514 model = &op_amd_spec;
515 /* Actually it could be i386/hammer too, but give
516 user space an consistent name. */
517 cpu_type = "x86-64/hammer";
520 model = &op_amd_spec;
521 cpu_type = "x86-64/family10";
524 model = &op_amd_spec;
525 cpu_type = "x86-64/family11h";
530 case X86_VENDOR_INTEL:
534 if (!p4_init(&cpu_type))
538 /* A P6-class processor */
540 if (!ppro_init(&cpu_type))
553 /* default values, can be overwritten by model */
554 __raw_get_cpu_var(switch_index) = 0;
555 ops->create_files = nmi_create_files;
556 ops->setup = nmi_setup;
557 ops->shutdown = nmi_shutdown;
558 ops->start = nmi_start;
559 ops->stop = nmi_stop;
560 ops->cpu_type = cpu_type;
561 ops->switch_events = nmi_switch_event;
564 ret = model->init(ops);
570 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
574 void op_nmi_exit(void)