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>
19 #include <linux/cpu.h>
24 #include "op_counter.h"
25 #include "op_x86_model.h"
27 static struct op_x86_model_spec
const *model
;
28 static DEFINE_PER_CPU(struct op_msrs
, cpu_msrs
);
29 static DEFINE_PER_CPU(unsigned long, saved_lvtpc
);
31 /* 0 == registered but off, 1 == registered and on */
32 static int nmi_enabled
= 0;
34 /* common functions */
36 u64
op_x86_get_ctrl(struct op_x86_model_spec
const *model
,
37 struct op_counter_config
*counter_config
)
40 u16 event
= (u16
)counter_config
->event
;
42 val
|= ARCH_PERFMON_EVENTSEL_INT
;
43 val
|= counter_config
->user
? ARCH_PERFMON_EVENTSEL_USR
: 0;
44 val
|= counter_config
->kernel
? ARCH_PERFMON_EVENTSEL_OS
: 0;
45 val
|= (counter_config
->unit_mask
& 0xFF) << 8;
46 event
&= model
->event_mask
? model
->event_mask
: 0xFF;
48 val
|= (event
& 0x0F00) << 24;
54 static int profile_exceptions_notify(struct notifier_block
*self
,
55 unsigned long val
, void *data
)
57 struct die_args
*args
= (struct die_args
*)data
;
58 int ret
= NOTIFY_DONE
;
59 int cpu
= smp_processor_id();
63 if (model
->check_ctrs(args
->regs
, &per_cpu(cpu_msrs
, cpu
)))
72 static void nmi_cpu_save_registers(struct op_msrs
*msrs
)
74 struct op_msr
*counters
= msrs
->counters
;
75 struct op_msr
*controls
= msrs
->controls
;
78 for (i
= 0; i
< model
->num_counters
; ++i
) {
80 rdmsrl(counters
[i
].addr
, counters
[i
].saved
);
83 for (i
= 0; i
< model
->num_controls
; ++i
) {
85 rdmsrl(controls
[i
].addr
, controls
[i
].saved
);
89 static void nmi_save_registers(void *dummy
)
91 int cpu
= smp_processor_id();
92 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
93 nmi_cpu_save_registers(msrs
);
96 static void free_msrs(void)
99 for_each_possible_cpu(i
) {
100 kfree(per_cpu(cpu_msrs
, i
).counters
);
101 per_cpu(cpu_msrs
, i
).counters
= NULL
;
102 kfree(per_cpu(cpu_msrs
, i
).controls
);
103 per_cpu(cpu_msrs
, i
).controls
= NULL
;
107 static int allocate_msrs(void)
110 size_t controls_size
= sizeof(struct op_msr
) * model
->num_controls
;
111 size_t counters_size
= sizeof(struct op_msr
) * model
->num_counters
;
114 for_each_possible_cpu(i
) {
115 per_cpu(cpu_msrs
, i
).counters
= kmalloc(counters_size
,
117 if (!per_cpu(cpu_msrs
, i
).counters
) {
121 per_cpu(cpu_msrs
, i
).controls
= kmalloc(controls_size
,
123 if (!per_cpu(cpu_msrs
, i
).controls
) {
135 static void nmi_cpu_setup(void *dummy
)
137 int cpu
= smp_processor_id();
138 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
139 spin_lock(&oprofilefs_lock
);
140 model
->setup_ctrs(model
, msrs
);
141 spin_unlock(&oprofilefs_lock
);
142 per_cpu(saved_lvtpc
, cpu
) = apic_read(APIC_LVTPC
);
143 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
146 static struct notifier_block profile_exceptions_nb
= {
147 .notifier_call
= profile_exceptions_notify
,
152 static int nmi_setup(void)
157 if (!allocate_msrs())
160 err
= register_die_notifier(&profile_exceptions_nb
);
166 /* We need to serialize save and setup for HT because the subset
167 * of msrs are distinct for save and setup operations
170 /* Assume saved/restored counters are the same on all CPUs */
171 model
->fill_in_addresses(&per_cpu(cpu_msrs
, 0));
172 for_each_possible_cpu(cpu
) {
174 memcpy(per_cpu(cpu_msrs
, cpu
).counters
,
175 per_cpu(cpu_msrs
, 0).counters
,
176 sizeof(struct op_msr
) * model
->num_counters
);
178 memcpy(per_cpu(cpu_msrs
, cpu
).controls
,
179 per_cpu(cpu_msrs
, 0).controls
,
180 sizeof(struct op_msr
) * model
->num_controls
);
184 on_each_cpu(nmi_save_registers
, NULL
, 1);
185 on_each_cpu(nmi_cpu_setup
, NULL
, 1);
190 static void nmi_restore_registers(struct op_msrs
*msrs
)
192 struct op_msr
*counters
= msrs
->counters
;
193 struct op_msr
*controls
= msrs
->controls
;
196 for (i
= 0; i
< model
->num_controls
; ++i
) {
197 if (controls
[i
].addr
)
198 wrmsrl(controls
[i
].addr
, controls
[i
].saved
);
201 for (i
= 0; i
< model
->num_counters
; ++i
) {
202 if (counters
[i
].addr
)
203 wrmsrl(counters
[i
].addr
, counters
[i
].saved
);
207 static void nmi_cpu_shutdown(void *dummy
)
210 int cpu
= smp_processor_id();
211 struct op_msrs
*msrs
= &__get_cpu_var(cpu_msrs
);
213 /* restoring APIC_LVTPC can trigger an apic error because the delivery
214 * mode and vector nr combination can be illegal. That's by design: on
215 * power on apic lvt contain a zero vector nr which are legal only for
216 * NMI delivery mode. So inhibit apic err before restoring lvtpc
218 v
= apic_read(APIC_LVTERR
);
219 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
220 apic_write(APIC_LVTPC
, per_cpu(saved_lvtpc
, cpu
));
221 apic_write(APIC_LVTERR
, v
);
222 nmi_restore_registers(msrs
);
225 static void nmi_shutdown(void)
227 struct op_msrs
*msrs
;
230 on_each_cpu(nmi_cpu_shutdown
, NULL
, 1);
231 unregister_die_notifier(&profile_exceptions_nb
);
232 msrs
= &get_cpu_var(cpu_msrs
);
233 model
->shutdown(msrs
);
235 put_cpu_var(cpu_msrs
);
238 static void nmi_cpu_start(void *dummy
)
240 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
244 static int nmi_start(void)
246 on_each_cpu(nmi_cpu_start
, NULL
, 1);
250 static void nmi_cpu_stop(void *dummy
)
252 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
256 static void nmi_stop(void)
258 on_each_cpu(nmi_cpu_stop
, NULL
, 1);
261 struct op_counter_config counter_config
[OP_MAX_COUNTER
];
263 static int nmi_create_files(struct super_block
*sb
, struct dentry
*root
)
267 for (i
= 0; i
< model
->num_counters
; ++i
) {
271 /* quick little hack to _not_ expose a counter if it is not
272 * available for use. This should protect userspace app.
273 * NOTE: assumes 1:1 mapping here (that counters are organized
274 * sequentially in their struct assignment).
276 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i
)))
279 snprintf(buf
, sizeof(buf
), "%d", i
);
280 dir
= oprofilefs_mkdir(sb
, root
, buf
);
281 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
282 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
283 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
284 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
285 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
286 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
293 static int oprofile_cpu_notifier(struct notifier_block
*b
, unsigned long action
,
296 int cpu
= (unsigned long)data
;
298 case CPU_DOWN_FAILED
:
300 smp_call_function_single(cpu
, nmi_cpu_start
, NULL
, 0);
302 case CPU_DOWN_PREPARE
:
303 smp_call_function_single(cpu
, nmi_cpu_stop
, NULL
, 1);
309 static struct notifier_block oprofile_cpu_nb
= {
310 .notifier_call
= oprofile_cpu_notifier
316 static int nmi_suspend(struct sys_device
*dev
, pm_message_t state
)
318 /* Only one CPU left, just stop that one */
319 if (nmi_enabled
== 1)
324 static int nmi_resume(struct sys_device
*dev
)
326 if (nmi_enabled
== 1)
331 static struct sysdev_class oprofile_sysclass
= {
333 .resume
= nmi_resume
,
334 .suspend
= nmi_suspend
,
337 static struct sys_device device_oprofile
= {
339 .cls
= &oprofile_sysclass
,
342 static int __init
init_sysfs(void)
346 error
= sysdev_class_register(&oprofile_sysclass
);
348 error
= sysdev_register(&device_oprofile
);
352 static void exit_sysfs(void)
354 sysdev_unregister(&device_oprofile
);
355 sysdev_class_unregister(&oprofile_sysclass
);
359 #define init_sysfs() do { } while (0)
360 #define exit_sysfs() do { } while (0)
361 #endif /* CONFIG_PM */
363 static int __init
p4_init(char **cpu_type
)
365 __u8 cpu_model
= boot_cpu_data
.x86_model
;
367 if (cpu_model
> 6 || cpu_model
== 5)
371 *cpu_type
= "i386/p4";
375 switch (smp_num_siblings
) {
377 *cpu_type
= "i386/p4";
382 *cpu_type
= "i386/p4-ht";
383 model
= &op_p4_ht2_spec
;
388 printk(KERN_INFO
"oprofile: P4 HyperThreading detected with > 2 threads\n");
389 printk(KERN_INFO
"oprofile: Reverting to timer mode.\n");
393 static int force_arch_perfmon
;
394 static int force_cpu_type(const char *str
, struct kernel_param
*kp
)
396 if (!strcmp(str
, "archperfmon")) {
397 force_arch_perfmon
= 1;
398 printk(KERN_INFO
"oprofile: forcing architectural perfmon\n");
403 module_param_call(cpu_type
, force_cpu_type
, NULL
, NULL
, 0);
405 static int __init
ppro_init(char **cpu_type
)
407 __u8 cpu_model
= boot_cpu_data
.x86_model
;
409 if (force_arch_perfmon
&& cpu_has_arch_perfmon
)
414 *cpu_type
= "i386/ppro";
417 *cpu_type
= "i386/pii";
421 *cpu_type
= "i386/piii";
425 *cpu_type
= "i386/p6_mobile";
428 *cpu_type
= "i386/core";
431 *cpu_type
= "i386/core_2";
434 model
= &op_arch_perfmon_spec
;
435 *cpu_type
= "i386/core_i7";
438 *cpu_type
= "i386/atom";
445 model
= &op_ppro_spec
;
449 /* in order to get sysfs right */
450 static int using_nmi
;
452 int __init
op_nmi_init(struct oprofile_operations
*ops
)
454 __u8 vendor
= boot_cpu_data
.x86_vendor
;
455 __u8 family
= boot_cpu_data
.x86
;
456 char *cpu_type
= NULL
;
464 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
468 cpu_type
= "i386/athlon";
472 * Actually it could be i386/hammer too, but
473 * give user space an consistent name.
475 cpu_type
= "x86-64/hammer";
478 cpu_type
= "x86-64/family10";
481 cpu_type
= "x86-64/family11h";
486 model
= &op_amd_spec
;
489 case X86_VENDOR_INTEL
:
496 /* A P6-class processor */
498 ppro_init(&cpu_type
);
508 if (!cpu_has_arch_perfmon
)
511 /* use arch perfmon as fallback */
512 cpu_type
= "i386/arch_perfmon";
513 model
= &op_arch_perfmon_spec
;
521 register_cpu_notifier(&oprofile_cpu_nb
);
523 /* default values, can be overwritten by model */
524 ops
->create_files
= nmi_create_files
;
525 ops
->setup
= nmi_setup
;
526 ops
->shutdown
= nmi_shutdown
;
527 ops
->start
= nmi_start
;
528 ops
->stop
= nmi_stop
;
529 ops
->cpu_type
= cpu_type
;
532 ret
= model
->init(ops
);
538 printk(KERN_INFO
"oprofile: using NMI interrupt.\n");
542 void op_nmi_exit(void)
547 unregister_cpu_notifier(&oprofile_cpu_nb
);