Linux 2.6.35-rc1
[orion.git] / arch / x86 / oprofile / nmi_int.c
blobb28d2f1253bbc927731afa08022fdef21a598a24
1 /**
2 * @file nmi_int.c
4 * @remark Copyright 2002-2009 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 * @author Robert Richter <robert.richter@amd.com>
9 * @author Barry Kasindorf <barry.kasindorf@amd.com>
10 * @author Jason Yeh <jason.yeh@amd.com>
11 * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
14 #include <linux/init.h>
15 #include <linux/notifier.h>
16 #include <linux/smp.h>
17 #include <linux/oprofile.h>
18 #include <linux/sysdev.h>
19 #include <linux/slab.h>
20 #include <linux/moduleparam.h>
21 #include <linux/kdebug.h>
22 #include <linux/cpu.h>
23 #include <asm/nmi.h>
24 #include <asm/msr.h>
25 #include <asm/apic.h>
27 #include "op_counter.h"
28 #include "op_x86_model.h"
30 static struct op_x86_model_spec *model;
31 static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
32 static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
34 /* must be protected with get_online_cpus()/put_online_cpus(): */
35 static int nmi_enabled;
36 static int ctr_running;
38 struct op_counter_config counter_config[OP_MAX_COUNTER];
40 /* common functions */
42 u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
43 struct op_counter_config *counter_config)
45 u64 val = 0;
46 u16 event = (u16)counter_config->event;
48 val |= ARCH_PERFMON_EVENTSEL_INT;
49 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
50 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
51 val |= (counter_config->unit_mask & 0xFF) << 8;
52 event &= model->event_mask ? model->event_mask : 0xFF;
53 val |= event & 0xFF;
54 val |= (event & 0x0F00) << 24;
56 return val;
60 static int profile_exceptions_notify(struct notifier_block *self,
61 unsigned long val, void *data)
63 struct die_args *args = (struct die_args *)data;
64 int ret = NOTIFY_DONE;
66 switch (val) {
67 case DIE_NMI:
68 case DIE_NMI_IPI:
69 if (ctr_running)
70 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
71 else if (!nmi_enabled)
72 break;
73 else
74 model->stop(&__get_cpu_var(cpu_msrs));
75 ret = NOTIFY_STOP;
76 break;
77 default:
78 break;
80 return ret;
83 static void nmi_cpu_save_registers(struct op_msrs *msrs)
85 struct op_msr *counters = msrs->counters;
86 struct op_msr *controls = msrs->controls;
87 unsigned int i;
89 for (i = 0; i < model->num_counters; ++i) {
90 if (counters[i].addr)
91 rdmsrl(counters[i].addr, counters[i].saved);
94 for (i = 0; i < model->num_controls; ++i) {
95 if (controls[i].addr)
96 rdmsrl(controls[i].addr, controls[i].saved);
100 static void nmi_cpu_start(void *dummy)
102 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
103 if (!msrs->controls)
104 WARN_ON_ONCE(1);
105 else
106 model->start(msrs);
109 static int nmi_start(void)
111 get_online_cpus();
112 on_each_cpu(nmi_cpu_start, NULL, 1);
113 ctr_running = 1;
114 put_online_cpus();
115 return 0;
118 static void nmi_cpu_stop(void *dummy)
120 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
121 if (!msrs->controls)
122 WARN_ON_ONCE(1);
123 else
124 model->stop(msrs);
127 static void nmi_stop(void)
129 get_online_cpus();
130 on_each_cpu(nmi_cpu_stop, NULL, 1);
131 ctr_running = 0;
132 put_online_cpus();
135 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
137 static DEFINE_PER_CPU(int, switch_index);
139 static inline int has_mux(void)
141 return !!model->switch_ctrl;
144 inline int op_x86_phys_to_virt(int phys)
146 return __get_cpu_var(switch_index) + phys;
149 inline int op_x86_virt_to_phys(int virt)
151 return virt % model->num_counters;
154 static void nmi_shutdown_mux(void)
156 int i;
158 if (!has_mux())
159 return;
161 for_each_possible_cpu(i) {
162 kfree(per_cpu(cpu_msrs, i).multiplex);
163 per_cpu(cpu_msrs, i).multiplex = NULL;
164 per_cpu(switch_index, i) = 0;
168 static int nmi_setup_mux(void)
170 size_t multiplex_size =
171 sizeof(struct op_msr) * model->num_virt_counters;
172 int i;
174 if (!has_mux())
175 return 1;
177 for_each_possible_cpu(i) {
178 per_cpu(cpu_msrs, i).multiplex =
179 kzalloc(multiplex_size, GFP_KERNEL);
180 if (!per_cpu(cpu_msrs, i).multiplex)
181 return 0;
184 return 1;
187 static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
189 int i;
190 struct op_msr *multiplex = msrs->multiplex;
192 if (!has_mux())
193 return;
195 for (i = 0; i < model->num_virt_counters; ++i) {
196 if (counter_config[i].enabled) {
197 multiplex[i].saved = -(u64)counter_config[i].count;
198 } else {
199 multiplex[i].saved = 0;
203 per_cpu(switch_index, cpu) = 0;
206 static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
208 struct op_msr *counters = msrs->counters;
209 struct op_msr *multiplex = msrs->multiplex;
210 int i;
212 for (i = 0; i < model->num_counters; ++i) {
213 int virt = op_x86_phys_to_virt(i);
214 if (counters[i].addr)
215 rdmsrl(counters[i].addr, multiplex[virt].saved);
219 static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
221 struct op_msr *counters = msrs->counters;
222 struct op_msr *multiplex = msrs->multiplex;
223 int i;
225 for (i = 0; i < model->num_counters; ++i) {
226 int virt = op_x86_phys_to_virt(i);
227 if (counters[i].addr)
228 wrmsrl(counters[i].addr, multiplex[virt].saved);
232 static void nmi_cpu_switch(void *dummy)
234 int cpu = smp_processor_id();
235 int si = per_cpu(switch_index, cpu);
236 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
238 nmi_cpu_stop(NULL);
239 nmi_cpu_save_mpx_registers(msrs);
241 /* move to next set */
242 si += model->num_counters;
243 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
244 per_cpu(switch_index, cpu) = 0;
245 else
246 per_cpu(switch_index, cpu) = si;
248 model->switch_ctrl(model, msrs);
249 nmi_cpu_restore_mpx_registers(msrs);
251 nmi_cpu_start(NULL);
256 * Quick check to see if multiplexing is necessary.
257 * The check should be sufficient since counters are used
258 * in ordre.
260 static int nmi_multiplex_on(void)
262 return counter_config[model->num_counters].count ? 0 : -EINVAL;
265 static int nmi_switch_event(void)
267 if (!has_mux())
268 return -ENOSYS; /* not implemented */
269 if (nmi_multiplex_on() < 0)
270 return -EINVAL; /* not necessary */
272 get_online_cpus();
273 if (ctr_running)
274 on_each_cpu(nmi_cpu_switch, NULL, 1);
275 put_online_cpus();
277 return 0;
280 static inline void mux_init(struct oprofile_operations *ops)
282 if (has_mux())
283 ops->switch_events = nmi_switch_event;
286 static void mux_clone(int cpu)
288 if (!has_mux())
289 return;
291 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
292 per_cpu(cpu_msrs, 0).multiplex,
293 sizeof(struct op_msr) * model->num_virt_counters);
296 #else
298 inline int op_x86_phys_to_virt(int phys) { return phys; }
299 inline int op_x86_virt_to_phys(int virt) { return virt; }
300 static inline void nmi_shutdown_mux(void) { }
301 static inline int nmi_setup_mux(void) { return 1; }
302 static inline void
303 nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
304 static inline void mux_init(struct oprofile_operations *ops) { }
305 static void mux_clone(int cpu) { }
307 #endif
309 static void free_msrs(void)
311 int i;
312 for_each_possible_cpu(i) {
313 kfree(per_cpu(cpu_msrs, i).counters);
314 per_cpu(cpu_msrs, i).counters = NULL;
315 kfree(per_cpu(cpu_msrs, i).controls);
316 per_cpu(cpu_msrs, i).controls = NULL;
318 nmi_shutdown_mux();
321 static int allocate_msrs(void)
323 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
324 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
326 int i;
327 for_each_possible_cpu(i) {
328 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
329 GFP_KERNEL);
330 if (!per_cpu(cpu_msrs, i).counters)
331 goto fail;
332 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
333 GFP_KERNEL);
334 if (!per_cpu(cpu_msrs, i).controls)
335 goto fail;
338 if (!nmi_setup_mux())
339 goto fail;
341 return 1;
343 fail:
344 free_msrs();
345 return 0;
348 static void nmi_cpu_setup(void *dummy)
350 int cpu = smp_processor_id();
351 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
352 nmi_cpu_save_registers(msrs);
353 spin_lock(&oprofilefs_lock);
354 model->setup_ctrs(model, msrs);
355 nmi_cpu_setup_mux(cpu, msrs);
356 spin_unlock(&oprofilefs_lock);
357 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
358 apic_write(APIC_LVTPC, APIC_DM_NMI);
361 static struct notifier_block profile_exceptions_nb = {
362 .notifier_call = profile_exceptions_notify,
363 .next = NULL,
364 .priority = 2
367 static void nmi_cpu_restore_registers(struct op_msrs *msrs)
369 struct op_msr *counters = msrs->counters;
370 struct op_msr *controls = msrs->controls;
371 unsigned int i;
373 for (i = 0; i < model->num_controls; ++i) {
374 if (controls[i].addr)
375 wrmsrl(controls[i].addr, controls[i].saved);
378 for (i = 0; i < model->num_counters; ++i) {
379 if (counters[i].addr)
380 wrmsrl(counters[i].addr, counters[i].saved);
384 static void nmi_cpu_shutdown(void *dummy)
386 unsigned int v;
387 int cpu = smp_processor_id();
388 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
390 /* restoring APIC_LVTPC can trigger an apic error because the delivery
391 * mode and vector nr combination can be illegal. That's by design: on
392 * power on apic lvt contain a zero vector nr which are legal only for
393 * NMI delivery mode. So inhibit apic err before restoring lvtpc
395 v = apic_read(APIC_LVTERR);
396 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
397 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
398 apic_write(APIC_LVTERR, v);
399 nmi_cpu_restore_registers(msrs);
400 if (model->cpu_down)
401 model->cpu_down();
404 static void nmi_cpu_up(void *dummy)
406 if (nmi_enabled)
407 nmi_cpu_setup(dummy);
408 if (ctr_running)
409 nmi_cpu_start(dummy);
412 static void nmi_cpu_down(void *dummy)
414 if (ctr_running)
415 nmi_cpu_stop(dummy);
416 if (nmi_enabled)
417 nmi_cpu_shutdown(dummy);
420 static int nmi_create_files(struct super_block *sb, struct dentry *root)
422 unsigned int i;
424 for (i = 0; i < model->num_virt_counters; ++i) {
425 struct dentry *dir;
426 char buf[4];
428 /* quick little hack to _not_ expose a counter if it is not
429 * available for use. This should protect userspace app.
430 * NOTE: assumes 1:1 mapping here (that counters are organized
431 * sequentially in their struct assignment).
433 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
434 continue;
436 snprintf(buf, sizeof(buf), "%d", i);
437 dir = oprofilefs_mkdir(sb, root, buf);
438 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
439 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
440 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
441 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
442 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
443 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
446 return 0;
449 static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
450 void *data)
452 int cpu = (unsigned long)data;
453 switch (action) {
454 case CPU_DOWN_FAILED:
455 case CPU_ONLINE:
456 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
457 break;
458 case CPU_DOWN_PREPARE:
459 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
460 break;
462 return NOTIFY_DONE;
465 static struct notifier_block oprofile_cpu_nb = {
466 .notifier_call = oprofile_cpu_notifier
469 static int nmi_setup(void)
471 int err = 0;
472 int cpu;
474 if (!allocate_msrs())
475 return -ENOMEM;
477 /* We need to serialize save and setup for HT because the subset
478 * of msrs are distinct for save and setup operations
481 /* Assume saved/restored counters are the same on all CPUs */
482 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
483 if (err)
484 goto fail;
486 for_each_possible_cpu(cpu) {
487 if (!cpu)
488 continue;
490 memcpy(per_cpu(cpu_msrs, cpu).counters,
491 per_cpu(cpu_msrs, 0).counters,
492 sizeof(struct op_msr) * model->num_counters);
494 memcpy(per_cpu(cpu_msrs, cpu).controls,
495 per_cpu(cpu_msrs, 0).controls,
496 sizeof(struct op_msr) * model->num_controls);
498 mux_clone(cpu);
501 nmi_enabled = 0;
502 ctr_running = 0;
503 barrier();
504 err = register_die_notifier(&profile_exceptions_nb);
505 if (err)
506 goto fail;
508 get_online_cpus();
509 register_cpu_notifier(&oprofile_cpu_nb);
510 on_each_cpu(nmi_cpu_setup, NULL, 1);
511 nmi_enabled = 1;
512 put_online_cpus();
514 return 0;
515 fail:
516 free_msrs();
517 return err;
520 static void nmi_shutdown(void)
522 struct op_msrs *msrs;
524 get_online_cpus();
525 unregister_cpu_notifier(&oprofile_cpu_nb);
526 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
527 nmi_enabled = 0;
528 ctr_running = 0;
529 put_online_cpus();
530 barrier();
531 unregister_die_notifier(&profile_exceptions_nb);
532 msrs = &get_cpu_var(cpu_msrs);
533 model->shutdown(msrs);
534 free_msrs();
535 put_cpu_var(cpu_msrs);
538 #ifdef CONFIG_PM
540 static int nmi_suspend(struct sys_device *dev, pm_message_t state)
542 /* Only one CPU left, just stop that one */
543 if (nmi_enabled == 1)
544 nmi_cpu_stop(NULL);
545 return 0;
548 static int nmi_resume(struct sys_device *dev)
550 if (nmi_enabled == 1)
551 nmi_cpu_start(NULL);
552 return 0;
555 static struct sysdev_class oprofile_sysclass = {
556 .name = "oprofile",
557 .resume = nmi_resume,
558 .suspend = nmi_suspend,
561 static struct sys_device device_oprofile = {
562 .id = 0,
563 .cls = &oprofile_sysclass,
566 static int __init init_sysfs(void)
568 int error;
570 error = sysdev_class_register(&oprofile_sysclass);
571 if (!error)
572 error = sysdev_register(&device_oprofile);
573 return error;
576 static void exit_sysfs(void)
578 sysdev_unregister(&device_oprofile);
579 sysdev_class_unregister(&oprofile_sysclass);
582 #else
583 #define init_sysfs() do { } while (0)
584 #define exit_sysfs() do { } while (0)
585 #endif /* CONFIG_PM */
587 static int __init p4_init(char **cpu_type)
589 __u8 cpu_model = boot_cpu_data.x86_model;
591 if (cpu_model > 6 || cpu_model == 5)
592 return 0;
594 #ifndef CONFIG_SMP
595 *cpu_type = "i386/p4";
596 model = &op_p4_spec;
597 return 1;
598 #else
599 switch (smp_num_siblings) {
600 case 1:
601 *cpu_type = "i386/p4";
602 model = &op_p4_spec;
603 return 1;
605 case 2:
606 *cpu_type = "i386/p4-ht";
607 model = &op_p4_ht2_spec;
608 return 1;
610 #endif
612 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
613 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
614 return 0;
617 static int force_arch_perfmon;
618 static int force_cpu_type(const char *str, struct kernel_param *kp)
620 if (!strcmp(str, "arch_perfmon")) {
621 force_arch_perfmon = 1;
622 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
625 return 0;
627 module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
629 static int __init ppro_init(char **cpu_type)
631 __u8 cpu_model = boot_cpu_data.x86_model;
632 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
634 if (force_arch_perfmon && cpu_has_arch_perfmon)
635 return 0;
637 switch (cpu_model) {
638 case 0 ... 2:
639 *cpu_type = "i386/ppro";
640 break;
641 case 3 ... 5:
642 *cpu_type = "i386/pii";
643 break;
644 case 6 ... 8:
645 case 10 ... 11:
646 *cpu_type = "i386/piii";
647 break;
648 case 9:
649 case 13:
650 *cpu_type = "i386/p6_mobile";
651 break;
652 case 14:
653 *cpu_type = "i386/core";
654 break;
655 case 15: case 23:
656 *cpu_type = "i386/core_2";
657 break;
658 case 0x2e:
659 case 26:
660 spec = &op_arch_perfmon_spec;
661 *cpu_type = "i386/core_i7";
662 break;
663 case 28:
664 *cpu_type = "i386/atom";
665 break;
666 default:
667 /* Unknown */
668 return 0;
671 model = spec;
672 return 1;
675 /* in order to get sysfs right */
676 static int using_nmi;
678 int __init op_nmi_init(struct oprofile_operations *ops)
680 __u8 vendor = boot_cpu_data.x86_vendor;
681 __u8 family = boot_cpu_data.x86;
682 char *cpu_type = NULL;
683 int ret = 0;
685 if (!cpu_has_apic)
686 return -ENODEV;
688 switch (vendor) {
689 case X86_VENDOR_AMD:
690 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
692 switch (family) {
693 case 6:
694 cpu_type = "i386/athlon";
695 break;
696 case 0xf:
698 * Actually it could be i386/hammer too, but
699 * give user space an consistent name.
701 cpu_type = "x86-64/hammer";
702 break;
703 case 0x10:
704 cpu_type = "x86-64/family10";
705 break;
706 case 0x11:
707 cpu_type = "x86-64/family11h";
708 break;
709 default:
710 return -ENODEV;
712 model = &op_amd_spec;
713 break;
715 case X86_VENDOR_INTEL:
716 switch (family) {
717 /* Pentium IV */
718 case 0xf:
719 p4_init(&cpu_type);
720 break;
722 /* A P6-class processor */
723 case 6:
724 ppro_init(&cpu_type);
725 break;
727 default:
728 break;
731 if (cpu_type)
732 break;
734 if (!cpu_has_arch_perfmon)
735 return -ENODEV;
737 /* use arch perfmon as fallback */
738 cpu_type = "i386/arch_perfmon";
739 model = &op_arch_perfmon_spec;
740 break;
742 default:
743 return -ENODEV;
746 /* default values, can be overwritten by model */
747 ops->create_files = nmi_create_files;
748 ops->setup = nmi_setup;
749 ops->shutdown = nmi_shutdown;
750 ops->start = nmi_start;
751 ops->stop = nmi_stop;
752 ops->cpu_type = cpu_type;
754 if (model->init)
755 ret = model->init(ops);
756 if (ret)
757 return ret;
759 if (!model->num_virt_counters)
760 model->num_virt_counters = model->num_counters;
762 mux_init(ops);
764 init_sysfs();
765 using_nmi = 1;
766 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
767 return 0;
770 void op_nmi_exit(void)
772 if (using_nmi)
773 exit_sysfs();