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>
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)
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;
54 val |= (event & 0x0F00) << 24;
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;
70 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
71 else if (!nmi_enabled)
74 model->stop(&__get_cpu_var(cpu_msrs));
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;
89 for (i = 0; i < model->num_counters; ++i) {
91 rdmsrl(counters[i].addr, counters[i].saved);
94 for (i = 0; i < model->num_controls; ++i) {
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);
109 static int nmi_start(void)
112 on_each_cpu(nmi_cpu_start, NULL, 1);
118 static void nmi_cpu_stop(void *dummy)
120 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
127 static void nmi_stop(void)
130 on_each_cpu(nmi_cpu_stop, NULL, 1);
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)
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;
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)
187 static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
190 struct op_msr *multiplex = msrs->multiplex;
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;
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;
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;
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);
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;
246 per_cpu(switch_index, cpu) = si;
248 model->switch_ctrl(model, msrs);
249 nmi_cpu_restore_mpx_registers(msrs);
256 * Quick check to see if multiplexing is necessary.
257 * The check should be sufficient since counters are used
260 static int nmi_multiplex_on(void)
262 return counter_config[model->num_counters].count ? 0 : -EINVAL;
265 static int nmi_switch_event(void)
268 return -ENOSYS; /* not implemented */
269 if (nmi_multiplex_on() < 0)
270 return -EINVAL; /* not necessary */
274 on_each_cpu(nmi_cpu_switch, NULL, 1);
280 static inline void mux_init(struct oprofile_operations *ops)
283 ops->switch_events = nmi_switch_event;
286 static void mux_clone(int cpu)
291 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
292 per_cpu(cpu_msrs, 0).multiplex,
293 sizeof(struct op_msr) * model->num_virt_counters);
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; }
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) { }
309 static void free_msrs(void)
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;
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;
327 for_each_possible_cpu(i) {
328 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
330 if (!per_cpu(cpu_msrs, i).counters)
332 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
334 if (!per_cpu(cpu_msrs, i).controls)
338 if (!nmi_setup_mux())
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,
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;
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)
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);
404 static void nmi_cpu_up(void *dummy)
407 nmi_cpu_setup(dummy);
409 nmi_cpu_start(dummy);
412 static void nmi_cpu_down(void *dummy)
417 nmi_cpu_shutdown(dummy);
420 static int nmi_create_files(struct super_block *sb, struct dentry *root)
424 for (i = 0; i < model->num_virt_counters; ++i) {
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)))
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);
449 static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
452 int cpu = (unsigned long)data;
454 case CPU_DOWN_FAILED:
456 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
458 case CPU_DOWN_PREPARE:
459 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
465 static struct notifier_block oprofile_cpu_nb = {
466 .notifier_call = oprofile_cpu_notifier
469 static int nmi_setup(void)
474 if (!allocate_msrs())
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));
486 for_each_possible_cpu(cpu) {
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);
504 err = register_die_notifier(&profile_exceptions_nb);
509 register_cpu_notifier(&oprofile_cpu_nb);
510 on_each_cpu(nmi_cpu_setup, NULL, 1);
520 static void nmi_shutdown(void)
522 struct op_msrs *msrs;
525 unregister_cpu_notifier(&oprofile_cpu_nb);
526 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
531 unregister_die_notifier(&profile_exceptions_nb);
532 msrs = &get_cpu_var(cpu_msrs);
533 model->shutdown(msrs);
535 put_cpu_var(cpu_msrs);
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)
548 static int nmi_resume(struct sys_device *dev)
550 if (nmi_enabled == 1)
555 static struct sysdev_class oprofile_sysclass = {
557 .resume = nmi_resume,
558 .suspend = nmi_suspend,
561 static struct sys_device device_oprofile = {
563 .cls = &oprofile_sysclass,
566 static int __init init_sysfs(void)
570 error = sysdev_class_register(&oprofile_sysclass);
574 error = sysdev_register(&device_oprofile);
576 sysdev_class_unregister(&oprofile_sysclass);
581 static void exit_sysfs(void)
583 sysdev_unregister(&device_oprofile);
584 sysdev_class_unregister(&oprofile_sysclass);
589 static inline int init_sysfs(void) { return 0; }
590 static inline void exit_sysfs(void) { }
592 #endif /* CONFIG_PM */
594 static int __init p4_init(char **cpu_type)
596 __u8 cpu_model = boot_cpu_data.x86_model;
598 if (cpu_model > 6 || cpu_model == 5)
602 *cpu_type = "i386/p4";
606 switch (smp_num_siblings) {
608 *cpu_type = "i386/p4";
613 *cpu_type = "i386/p4-ht";
614 model = &op_p4_ht2_spec;
619 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
620 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
624 static int force_arch_perfmon;
625 static int force_cpu_type(const char *str, struct kernel_param *kp)
627 if (!strcmp(str, "arch_perfmon")) {
628 force_arch_perfmon = 1;
629 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
634 module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
636 static int __init ppro_init(char **cpu_type)
638 __u8 cpu_model = boot_cpu_data.x86_model;
639 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
641 if (force_arch_perfmon && cpu_has_arch_perfmon)
645 * Documentation on identifying Intel processors by CPU family
646 * and model can be found in the Intel Software Developer's
649 * http://www.intel.com/products/processor/manuals/
651 * As of May 2010 the documentation for this was in the:
652 * "Intel 64 and IA-32 Architectures Software Developer's
653 * Manual Volume 3B: System Programming Guide", "Table B-1
654 * CPUID Signature Values of DisplayFamily_DisplayModel".
658 *cpu_type = "i386/ppro";
661 *cpu_type = "i386/pii";
665 *cpu_type = "i386/piii";
669 *cpu_type = "i386/p6_mobile";
672 *cpu_type = "i386/core";
678 *cpu_type = "i386/core_2";
683 spec = &op_arch_perfmon_spec;
684 *cpu_type = "i386/core_i7";
687 *cpu_type = "i386/atom";
698 int __init op_nmi_init(struct oprofile_operations *ops)
700 __u8 vendor = boot_cpu_data.x86_vendor;
701 __u8 family = boot_cpu_data.x86;
702 char *cpu_type = NULL;
710 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
714 cpu_type = "i386/athlon";
718 * Actually it could be i386/hammer too, but
719 * give user space an consistent name.
721 cpu_type = "x86-64/hammer";
724 cpu_type = "x86-64/family10";
727 cpu_type = "x86-64/family11h";
732 model = &op_amd_spec;
735 case X86_VENDOR_INTEL:
742 /* A P6-class processor */
744 ppro_init(&cpu_type);
754 if (!cpu_has_arch_perfmon)
757 /* use arch perfmon as fallback */
758 cpu_type = "i386/arch_perfmon";
759 model = &op_arch_perfmon_spec;
766 /* default values, can be overwritten by model */
767 ops->create_files = nmi_create_files;
768 ops->setup = nmi_setup;
769 ops->shutdown = nmi_shutdown;
770 ops->start = nmi_start;
771 ops->stop = nmi_stop;
772 ops->cpu_type = cpu_type;
775 ret = model->init(ops);
779 if (!model->num_virt_counters)
780 model->num_virt_counters = model->num_counters;
788 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
792 void op_nmi_exit(void)