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 /* 0 == registered but off, 1 == registered and on */
35 static int nmi_enabled = 0;
37 struct op_counter_config counter_config[OP_MAX_COUNTER];
39 /* common functions */
41 u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
42 struct op_counter_config *counter_config)
45 u16 event = (u16)counter_config->event;
47 val |= ARCH_PERFMON_EVENTSEL_INT;
48 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
49 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
50 val |= (counter_config->unit_mask & 0xFF) << 8;
51 event &= model->event_mask ? model->event_mask : 0xFF;
53 val |= (event & 0x0F00) << 24;
59 static int profile_exceptions_notify(struct notifier_block *self,
60 unsigned long val, void *data)
62 struct die_args *args = (struct die_args *)data;
63 int ret = NOTIFY_DONE;
64 int cpu = smp_processor_id();
69 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
78 static void nmi_cpu_save_registers(struct op_msrs *msrs)
80 struct op_msr *counters = msrs->counters;
81 struct op_msr *controls = msrs->controls;
84 for (i = 0; i < model->num_counters; ++i) {
86 rdmsrl(counters[i].addr, counters[i].saved);
89 for (i = 0; i < model->num_controls; ++i) {
91 rdmsrl(controls[i].addr, controls[i].saved);
95 static void nmi_cpu_start(void *dummy)
97 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
101 static int nmi_start(void)
103 on_each_cpu(nmi_cpu_start, NULL, 1);
107 static void nmi_cpu_stop(void *dummy)
109 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
113 static void nmi_stop(void)
115 on_each_cpu(nmi_cpu_stop, NULL, 1);
118 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
120 static DEFINE_PER_CPU(int, switch_index);
122 static inline int has_mux(void)
124 return !!model->switch_ctrl;
127 inline int op_x86_phys_to_virt(int phys)
129 return __get_cpu_var(switch_index) + phys;
132 inline int op_x86_virt_to_phys(int virt)
134 return virt % model->num_counters;
137 static void nmi_shutdown_mux(void)
144 for_each_possible_cpu(i) {
145 kfree(per_cpu(cpu_msrs, i).multiplex);
146 per_cpu(cpu_msrs, i).multiplex = NULL;
147 per_cpu(switch_index, i) = 0;
151 static int nmi_setup_mux(void)
153 size_t multiplex_size =
154 sizeof(struct op_msr) * model->num_virt_counters;
160 for_each_possible_cpu(i) {
161 per_cpu(cpu_msrs, i).multiplex =
162 kmalloc(multiplex_size, GFP_KERNEL);
163 if (!per_cpu(cpu_msrs, i).multiplex)
170 static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
173 struct op_msr *multiplex = msrs->multiplex;
178 for (i = 0; i < model->num_virt_counters; ++i) {
179 if (counter_config[i].enabled) {
180 multiplex[i].saved = -(u64)counter_config[i].count;
182 multiplex[i].addr = 0;
183 multiplex[i].saved = 0;
187 per_cpu(switch_index, cpu) = 0;
190 static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
192 struct op_msr *multiplex = msrs->multiplex;
195 for (i = 0; i < model->num_counters; ++i) {
196 int virt = op_x86_phys_to_virt(i);
197 if (multiplex[virt].addr)
198 rdmsrl(multiplex[virt].addr, multiplex[virt].saved);
202 static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
204 struct op_msr *multiplex = msrs->multiplex;
207 for (i = 0; i < model->num_counters; ++i) {
208 int virt = op_x86_phys_to_virt(i);
209 if (multiplex[virt].addr)
210 wrmsrl(multiplex[virt].addr, multiplex[virt].saved);
214 static void nmi_cpu_switch(void *dummy)
216 int cpu = smp_processor_id();
217 int si = per_cpu(switch_index, cpu);
218 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
221 nmi_cpu_save_mpx_registers(msrs);
223 /* move to next set */
224 si += model->num_counters;
225 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
226 per_cpu(switch_index, cpu) = 0;
228 per_cpu(switch_index, cpu) = si;
230 model->switch_ctrl(model, msrs);
231 nmi_cpu_restore_mpx_registers(msrs);
238 * Quick check to see if multiplexing is necessary.
239 * The check should be sufficient since counters are used
242 static int nmi_multiplex_on(void)
244 return counter_config[model->num_counters].count ? 0 : -EINVAL;
247 static int nmi_switch_event(void)
250 return -ENOSYS; /* not implemented */
251 if (nmi_multiplex_on() < 0)
252 return -EINVAL; /* not necessary */
254 on_each_cpu(nmi_cpu_switch, NULL, 1);
259 static inline void mux_init(struct oprofile_operations *ops)
262 ops->switch_events = nmi_switch_event;
265 static void mux_clone(int cpu)
270 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
271 per_cpu(cpu_msrs, 0).multiplex,
272 sizeof(struct op_msr) * model->num_virt_counters);
277 inline int op_x86_phys_to_virt(int phys) { return phys; }
278 inline int op_x86_virt_to_phys(int virt) { return virt; }
279 static inline void nmi_shutdown_mux(void) { }
280 static inline int nmi_setup_mux(void) { return 1; }
282 nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
283 static inline void mux_init(struct oprofile_operations *ops) { }
284 static void mux_clone(int cpu) { }
288 static void free_msrs(void)
291 for_each_possible_cpu(i) {
292 kfree(per_cpu(cpu_msrs, i).counters);
293 per_cpu(cpu_msrs, i).counters = NULL;
294 kfree(per_cpu(cpu_msrs, i).controls);
295 per_cpu(cpu_msrs, i).controls = NULL;
299 static int allocate_msrs(void)
301 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
302 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
305 for_each_possible_cpu(i) {
306 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
308 if (!per_cpu(cpu_msrs, i).counters)
310 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
312 if (!per_cpu(cpu_msrs, i).controls)
319 static void nmi_cpu_setup(void *dummy)
321 int cpu = smp_processor_id();
322 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
323 nmi_cpu_save_registers(msrs);
324 spin_lock(&oprofilefs_lock);
325 model->setup_ctrs(model, msrs);
326 nmi_cpu_setup_mux(cpu, msrs);
327 spin_unlock(&oprofilefs_lock);
328 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
329 apic_write(APIC_LVTPC, APIC_DM_NMI);
332 static struct notifier_block profile_exceptions_nb = {
333 .notifier_call = profile_exceptions_notify,
338 static int nmi_setup(void)
343 if (!allocate_msrs())
345 else if (!nmi_setup_mux())
348 err = register_die_notifier(&profile_exceptions_nb);
356 /* We need to serialize save and setup for HT because the subset
357 * of msrs are distinct for save and setup operations
360 /* Assume saved/restored counters are the same on all CPUs */
361 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
362 for_each_possible_cpu(cpu) {
366 memcpy(per_cpu(cpu_msrs, cpu).counters,
367 per_cpu(cpu_msrs, 0).counters,
368 sizeof(struct op_msr) * model->num_counters);
370 memcpy(per_cpu(cpu_msrs, cpu).controls,
371 per_cpu(cpu_msrs, 0).controls,
372 sizeof(struct op_msr) * model->num_controls);
376 on_each_cpu(nmi_cpu_setup, NULL, 1);
381 static void nmi_cpu_restore_registers(struct op_msrs *msrs)
383 struct op_msr *counters = msrs->counters;
384 struct op_msr *controls = msrs->controls;
387 for (i = 0; i < model->num_controls; ++i) {
388 if (controls[i].addr)
389 wrmsrl(controls[i].addr, controls[i].saved);
392 for (i = 0; i < model->num_counters; ++i) {
393 if (counters[i].addr)
394 wrmsrl(counters[i].addr, counters[i].saved);
398 static void nmi_cpu_shutdown(void *dummy)
401 int cpu = smp_processor_id();
402 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
404 /* restoring APIC_LVTPC can trigger an apic error because the delivery
405 * mode and vector nr combination can be illegal. That's by design: on
406 * power on apic lvt contain a zero vector nr which are legal only for
407 * NMI delivery mode. So inhibit apic err before restoring lvtpc
409 v = apic_read(APIC_LVTERR);
410 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
411 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
412 apic_write(APIC_LVTERR, v);
413 nmi_cpu_restore_registers(msrs);
416 static void nmi_shutdown(void)
418 struct op_msrs *msrs;
421 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
422 unregister_die_notifier(&profile_exceptions_nb);
424 msrs = &get_cpu_var(cpu_msrs);
425 model->shutdown(msrs);
427 put_cpu_var(cpu_msrs);
430 static int nmi_create_files(struct super_block *sb, struct dentry *root)
434 for (i = 0; i < model->num_virt_counters; ++i) {
438 /* quick little hack to _not_ expose a counter if it is not
439 * available for use. This should protect userspace app.
440 * NOTE: assumes 1:1 mapping here (that counters are organized
441 * sequentially in their struct assignment).
443 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
446 snprintf(buf, sizeof(buf), "%d", i);
447 dir = oprofilefs_mkdir(sb, root, buf);
448 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
449 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
450 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
451 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
452 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
453 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
460 static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
463 int cpu = (unsigned long)data;
465 case CPU_DOWN_FAILED:
467 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
469 case CPU_DOWN_PREPARE:
470 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
476 static struct notifier_block oprofile_cpu_nb = {
477 .notifier_call = oprofile_cpu_notifier
483 static int nmi_suspend(struct sys_device *dev, pm_message_t state)
485 /* Only one CPU left, just stop that one */
486 if (nmi_enabled == 1)
491 static int nmi_resume(struct sys_device *dev)
493 if (nmi_enabled == 1)
498 static struct sysdev_class oprofile_sysclass = {
500 .resume = nmi_resume,
501 .suspend = nmi_suspend,
504 static struct sys_device device_oprofile = {
506 .cls = &oprofile_sysclass,
509 static int __init init_sysfs(void)
513 error = sysdev_class_register(&oprofile_sysclass);
515 error = sysdev_register(&device_oprofile);
519 static void exit_sysfs(void)
521 sysdev_unregister(&device_oprofile);
522 sysdev_class_unregister(&oprofile_sysclass);
526 #define init_sysfs() do { } while (0)
527 #define exit_sysfs() do { } while (0)
528 #endif /* CONFIG_PM */
530 static int __init p4_init(char **cpu_type)
532 __u8 cpu_model = boot_cpu_data.x86_model;
534 if (cpu_model > 6 || cpu_model == 5)
538 *cpu_type = "i386/p4";
542 switch (smp_num_siblings) {
544 *cpu_type = "i386/p4";
549 *cpu_type = "i386/p4-ht";
550 model = &op_p4_ht2_spec;
555 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
556 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
560 static int force_arch_perfmon;
561 static int force_cpu_type(const char *str, struct kernel_param *kp)
563 if (!strcmp(str, "arch_perfmon")) {
564 force_arch_perfmon = 1;
565 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
570 module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
572 static int __init ppro_init(char **cpu_type)
574 __u8 cpu_model = boot_cpu_data.x86_model;
575 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
577 if (force_arch_perfmon && cpu_has_arch_perfmon)
582 *cpu_type = "i386/ppro";
585 *cpu_type = "i386/pii";
589 *cpu_type = "i386/piii";
593 *cpu_type = "i386/p6_mobile";
596 *cpu_type = "i386/core";
599 *cpu_type = "i386/core_2";
603 spec = &op_arch_perfmon_spec;
604 *cpu_type = "i386/core_i7";
607 *cpu_type = "i386/atom";
618 /* in order to get sysfs right */
619 static int using_nmi;
621 int __init op_nmi_init(struct oprofile_operations *ops)
623 __u8 vendor = boot_cpu_data.x86_vendor;
624 __u8 family = boot_cpu_data.x86;
625 char *cpu_type = NULL;
633 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
637 cpu_type = "i386/athlon";
641 * Actually it could be i386/hammer too, but
642 * give user space an consistent name.
644 cpu_type = "x86-64/hammer";
647 cpu_type = "x86-64/family10";
650 cpu_type = "x86-64/family11h";
655 model = &op_amd_spec;
658 case X86_VENDOR_INTEL:
665 /* A P6-class processor */
667 ppro_init(&cpu_type);
677 if (!cpu_has_arch_perfmon)
680 /* use arch perfmon as fallback */
681 cpu_type = "i386/arch_perfmon";
682 model = &op_arch_perfmon_spec;
690 register_cpu_notifier(&oprofile_cpu_nb);
692 /* default values, can be overwritten by model */
693 ops->create_files = nmi_create_files;
694 ops->setup = nmi_setup;
695 ops->shutdown = nmi_shutdown;
696 ops->start = nmi_start;
697 ops->stop = nmi_stop;
698 ops->cpu_type = cpu_type;
701 ret = model->init(ops);
705 if (!model->num_virt_counters)
706 model->num_virt_counters = model->num_counters;
712 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
716 void op_nmi_exit(void)
721 unregister_cpu_notifier(&oprofile_cpu_nb);