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
const *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;
38 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
39 extern atomic_t multiplex_counter
;
42 struct op_counter_config counter_config
[OP_MAX_COUNTER
];
44 /* common functions */
46 u64
op_x86_get_ctrl(struct op_x86_model_spec
const *model
,
47 struct op_counter_config
*counter_config
)
50 u16 event
= (u16
)counter_config
->event
;
52 val
|= ARCH_PERFMON_EVENTSEL_INT
;
53 val
|= counter_config
->user
? ARCH_PERFMON_EVENTSEL_USR
: 0;
54 val
|= counter_config
->kernel
? ARCH_PERFMON_EVENTSEL_OS
: 0;
55 val
|= (counter_config
->unit_mask
& 0xFF) << 8;
56 event
&= model
->event_mask
? model
->event_mask
: 0xFF;
58 val
|= (event
& 0x0F00) << 24;
64 static int profile_exceptions_notify(struct notifier_block
*self
,
65 unsigned long val
, void *data
)
67 struct die_args
*args
= (struct die_args
*)data
;
68 int ret
= NOTIFY_DONE
;
69 int cpu
= smp_processor_id();
74 model
->check_ctrs(args
->regs
, &per_cpu(cpu_msrs
, cpu
));
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 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
102 static DEFINE_PER_CPU(int, switch_index
);
104 inline int op_x86_phys_to_virt(int phys
)
106 return __get_cpu_var(switch_index
) + phys
;
109 static void nmi_shutdown_mux(void)
112 for_each_possible_cpu(i
) {
113 kfree(per_cpu(cpu_msrs
, i
).multiplex
);
114 per_cpu(cpu_msrs
, i
).multiplex
= NULL
;
115 per_cpu(switch_index
, i
) = 0;
119 static int nmi_setup_mux(void)
121 size_t multiplex_size
=
122 sizeof(struct op_msr
) * model
->num_virt_counters
;
124 for_each_possible_cpu(i
) {
125 per_cpu(cpu_msrs
, i
).multiplex
=
126 kmalloc(multiplex_size
, GFP_KERNEL
);
127 if (!per_cpu(cpu_msrs
, i
).multiplex
)
133 static void nmi_cpu_setup_mux(int cpu
, struct op_msrs
const * const msrs
)
136 struct op_msr
*multiplex
= msrs
->multiplex
;
138 for (i
= 0; i
< model
->num_virt_counters
; ++i
) {
139 if (counter_config
[i
].enabled
) {
140 multiplex
[i
].saved
= -(u64
)counter_config
[i
].count
;
142 multiplex
[i
].addr
= 0;
143 multiplex
[i
].saved
= 0;
147 per_cpu(switch_index
, cpu
) = 0;
150 static void nmi_cpu_save_mpx_registers(struct op_msrs
*msrs
)
152 struct op_msr
*multiplex
= msrs
->multiplex
;
155 for (i
= 0; i
< model
->num_counters
; ++i
) {
156 int virt
= op_x86_phys_to_virt(i
);
157 if (multiplex
[virt
].addr
)
158 rdmsrl(multiplex
[virt
].addr
, multiplex
[virt
].saved
);
162 static void nmi_cpu_restore_mpx_registers(struct op_msrs
*msrs
)
164 struct op_msr
*multiplex
= msrs
->multiplex
;
167 for (i
= 0; i
< model
->num_counters
; ++i
) {
168 int virt
= op_x86_phys_to_virt(i
);
169 if (multiplex
[virt
].addr
)
170 wrmsrl(multiplex
[virt
].addr
, multiplex
[virt
].saved
);
176 inline int op_x86_phys_to_virt(int phys
) { return phys
; }
177 static inline void nmi_shutdown_mux(void) { }
178 static inline int nmi_setup_mux(void) { return 1; }
180 nmi_cpu_setup_mux(int cpu
, struct op_msrs
const * const msrs
) { }
184 static void free_msrs(void)
187 for_each_possible_cpu(i
) {
188 kfree(per_cpu(cpu_msrs
, i
).counters
);
189 per_cpu(cpu_msrs
, i
).counters
= NULL
;
190 kfree(per_cpu(cpu_msrs
, i
).controls
);
191 per_cpu(cpu_msrs
, i
).controls
= NULL
;
195 static int allocate_msrs(void)
197 size_t controls_size
= sizeof(struct op_msr
) * model
->num_controls
;
198 size_t counters_size
= sizeof(struct op_msr
) * model
->num_counters
;
201 for_each_possible_cpu(i
) {
202 per_cpu(cpu_msrs
, i
).counters
= kmalloc(counters_size
,
204 if (!per_cpu(cpu_msrs
, i
).counters
)
206 per_cpu(cpu_msrs
, i
).controls
= kmalloc(controls_size
,
208 if (!per_cpu(cpu_msrs
, i
).controls
)
215 static void nmi_cpu_setup(void *dummy
)
217 int cpu
= smp_processor_id();
218 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
219 nmi_cpu_save_registers(msrs
);
220 spin_lock(&oprofilefs_lock
);
221 model
->setup_ctrs(model
, msrs
);
222 nmi_cpu_setup_mux(cpu
, msrs
);
223 spin_unlock(&oprofilefs_lock
);
224 per_cpu(saved_lvtpc
, cpu
) = apic_read(APIC_LVTPC
);
225 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
228 static struct notifier_block profile_exceptions_nb
= {
229 .notifier_call
= profile_exceptions_notify
,
234 static int nmi_setup(void)
239 if (!allocate_msrs())
241 else if (!nmi_setup_mux())
244 err
= register_die_notifier(&profile_exceptions_nb
);
252 /* We need to serialize save and setup for HT because the subset
253 * of msrs are distinct for save and setup operations
256 /* Assume saved/restored counters are the same on all CPUs */
257 model
->fill_in_addresses(&per_cpu(cpu_msrs
, 0));
258 for_each_possible_cpu(cpu
) {
260 memcpy(per_cpu(cpu_msrs
, cpu
).counters
,
261 per_cpu(cpu_msrs
, 0).counters
,
262 sizeof(struct op_msr
) * model
->num_counters
);
264 memcpy(per_cpu(cpu_msrs
, cpu
).controls
,
265 per_cpu(cpu_msrs
, 0).controls
,
266 sizeof(struct op_msr
) * model
->num_controls
);
267 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
268 memcpy(per_cpu(cpu_msrs
, cpu
).multiplex
,
269 per_cpu(cpu_msrs
, 0).multiplex
,
270 sizeof(struct op_msr
) * model
->num_virt_counters
);
274 on_each_cpu(nmi_cpu_setup
, NULL
, 1);
279 static void nmi_cpu_restore_registers(struct op_msrs
*msrs
)
281 struct op_msr
*counters
= msrs
->counters
;
282 struct op_msr
*controls
= msrs
->controls
;
285 for (i
= 0; i
< model
->num_controls
; ++i
) {
286 if (controls
[i
].addr
)
287 wrmsrl(controls
[i
].addr
, controls
[i
].saved
);
290 for (i
= 0; i
< model
->num_counters
; ++i
) {
291 if (counters
[i
].addr
)
292 wrmsrl(counters
[i
].addr
, counters
[i
].saved
);
296 static void nmi_cpu_shutdown(void *dummy
)
299 int cpu
= smp_processor_id();
300 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
302 /* restoring APIC_LVTPC can trigger an apic error because the delivery
303 * mode and vector nr combination can be illegal. That's by design: on
304 * power on apic lvt contain a zero vector nr which are legal only for
305 * NMI delivery mode. So inhibit apic err before restoring lvtpc
307 v
= apic_read(APIC_LVTERR
);
308 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
309 apic_write(APIC_LVTPC
, per_cpu(saved_lvtpc
, cpu
));
310 apic_write(APIC_LVTERR
, v
);
311 nmi_cpu_restore_registers(msrs
);
314 static void nmi_shutdown(void)
316 struct op_msrs
*msrs
;
319 on_each_cpu(nmi_cpu_shutdown
, NULL
, 1);
320 unregister_die_notifier(&profile_exceptions_nb
);
322 msrs
= &get_cpu_var(cpu_msrs
);
323 model
->shutdown(msrs
);
325 put_cpu_var(cpu_msrs
);
328 static void nmi_cpu_start(void *dummy
)
330 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
334 static int nmi_start(void)
336 on_each_cpu(nmi_cpu_start
, NULL
, 1);
340 static void nmi_cpu_stop(void *dummy
)
342 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
346 static void nmi_stop(void)
348 on_each_cpu(nmi_cpu_stop
, NULL
, 1);
351 static int nmi_create_files(struct super_block
*sb
, struct dentry
*root
)
355 for (i
= 0; i
< model
->num_virt_counters
; ++i
) {
359 #ifndef CONFIG_OPROFILE_EVENT_MULTIPLEX
360 /* quick little hack to _not_ expose a counter if it is not
361 * available for use. This should protect userspace app.
362 * NOTE: assumes 1:1 mapping here (that counters are organized
363 * sequentially in their struct assignment).
365 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i
)))
367 #endif /* CONFIG_OPROFILE_EVENT_MULTIPLEX */
369 snprintf(buf
, sizeof(buf
), "%d", i
);
370 dir
= oprofilefs_mkdir(sb
, root
, buf
);
371 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
372 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
373 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
374 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
375 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
376 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
382 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
384 static void nmi_cpu_switch(void *dummy
)
386 int cpu
= smp_processor_id();
387 int si
= per_cpu(switch_index
, cpu
);
388 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
391 nmi_cpu_save_mpx_registers(msrs
);
393 /* move to next set */
394 si
+= model
->num_counters
;
395 if ((si
> model
->num_virt_counters
) || (counter_config
[si
].count
== 0))
396 per_cpu(switch_index
, cpu
) = 0;
398 per_cpu(switch_index
, cpu
) = si
;
400 model
->switch_ctrl(model
, msrs
);
401 nmi_cpu_restore_mpx_registers(msrs
);
408 * Quick check to see if multiplexing is necessary.
409 * The check should be sufficient since counters are used
412 static int nmi_multiplex_on(void)
414 return counter_config
[model
->num_counters
].count
? 0 : -EINVAL
;
417 static int nmi_switch_event(void)
419 if (!model
->switch_ctrl
)
420 return -ENOSYS
; /* not implemented */
421 if (nmi_multiplex_on() < 0)
422 return -EINVAL
; /* not necessary */
424 on_each_cpu(nmi_cpu_switch
, NULL
, 1);
426 atomic_inc(&multiplex_counter
);
434 static int oprofile_cpu_notifier(struct notifier_block
*b
, unsigned long action
,
437 int cpu
= (unsigned long)data
;
439 case CPU_DOWN_FAILED
:
441 smp_call_function_single(cpu
, nmi_cpu_start
, NULL
, 0);
443 case CPU_DOWN_PREPARE
:
444 smp_call_function_single(cpu
, nmi_cpu_stop
, NULL
, 1);
450 static struct notifier_block oprofile_cpu_nb
= {
451 .notifier_call
= oprofile_cpu_notifier
457 static int nmi_suspend(struct sys_device
*dev
, pm_message_t state
)
459 /* Only one CPU left, just stop that one */
460 if (nmi_enabled
== 1)
465 static int nmi_resume(struct sys_device
*dev
)
467 if (nmi_enabled
== 1)
472 static struct sysdev_class oprofile_sysclass
= {
474 .resume
= nmi_resume
,
475 .suspend
= nmi_suspend
,
478 static struct sys_device device_oprofile
= {
480 .cls
= &oprofile_sysclass
,
483 static int __init
init_sysfs(void)
487 error
= sysdev_class_register(&oprofile_sysclass
);
489 error
= sysdev_register(&device_oprofile
);
493 static void exit_sysfs(void)
495 sysdev_unregister(&device_oprofile
);
496 sysdev_class_unregister(&oprofile_sysclass
);
500 #define init_sysfs() do { } while (0)
501 #define exit_sysfs() do { } while (0)
502 #endif /* CONFIG_PM */
504 static int __init
p4_init(char **cpu_type
)
506 __u8 cpu_model
= boot_cpu_data
.x86_model
;
508 if (cpu_model
> 6 || cpu_model
== 5)
512 *cpu_type
= "i386/p4";
516 switch (smp_num_siblings
) {
518 *cpu_type
= "i386/p4";
523 *cpu_type
= "i386/p4-ht";
524 model
= &op_p4_ht2_spec
;
529 printk(KERN_INFO
"oprofile: P4 HyperThreading detected with > 2 threads\n");
530 printk(KERN_INFO
"oprofile: Reverting to timer mode.\n");
534 static int force_arch_perfmon
;
535 static int force_cpu_type(const char *str
, struct kernel_param
*kp
)
537 if (!strcmp(str
, "arch_perfmon")) {
538 force_arch_perfmon
= 1;
539 printk(KERN_INFO
"oprofile: forcing architectural perfmon\n");
544 module_param_call(cpu_type
, force_cpu_type
, NULL
, NULL
, 0);
546 static int __init
ppro_init(char **cpu_type
)
548 __u8 cpu_model
= boot_cpu_data
.x86_model
;
549 struct op_x86_model_spec
const *spec
= &op_ppro_spec
; /* default */
551 if (force_arch_perfmon
&& cpu_has_arch_perfmon
)
556 *cpu_type
= "i386/ppro";
559 *cpu_type
= "i386/pii";
563 *cpu_type
= "i386/piii";
567 *cpu_type
= "i386/p6_mobile";
570 *cpu_type
= "i386/core";
573 *cpu_type
= "i386/core_2";
576 spec
= &op_arch_perfmon_spec
;
577 *cpu_type
= "i386/core_i7";
580 *cpu_type
= "i386/atom";
591 /* in order to get sysfs right */
592 static int using_nmi
;
594 int __init
op_nmi_init(struct oprofile_operations
*ops
)
596 __u8 vendor
= boot_cpu_data
.x86_vendor
;
597 __u8 family
= boot_cpu_data
.x86
;
598 char *cpu_type
= NULL
;
606 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
610 cpu_type
= "i386/athlon";
614 * Actually it could be i386/hammer too, but
615 * give user space an consistent name.
617 cpu_type
= "x86-64/hammer";
620 cpu_type
= "x86-64/family10";
623 cpu_type
= "x86-64/family11h";
628 model
= &op_amd_spec
;
631 case X86_VENDOR_INTEL
:
638 /* A P6-class processor */
640 ppro_init(&cpu_type
);
650 if (!cpu_has_arch_perfmon
)
653 /* use arch perfmon as fallback */
654 cpu_type
= "i386/arch_perfmon";
655 model
= &op_arch_perfmon_spec
;
663 register_cpu_notifier(&oprofile_cpu_nb
);
665 /* default values, can be overwritten by model */
666 ops
->create_files
= nmi_create_files
;
667 ops
->setup
= nmi_setup
;
668 ops
->shutdown
= nmi_shutdown
;
669 ops
->start
= nmi_start
;
670 ops
->stop
= nmi_stop
;
671 ops
->cpu_type
= cpu_type
;
672 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
673 ops
->switch_events
= nmi_switch_event
;
677 ret
= model
->init(ops
);
683 printk(KERN_INFO
"oprofile: using NMI interrupt.\n");
687 void op_nmi_exit(void)
692 unregister_cpu_notifier(&oprofile_cpu_nb
);