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();
64 model
->check_ctrs(args
->regs
, &per_cpu(cpu_msrs
, cpu
));
73 static void nmi_cpu_save_registers(struct op_msrs
*msrs
)
75 struct op_msr
*counters
= msrs
->counters
;
76 struct op_msr
*controls
= msrs
->controls
;
79 for (i
= 0; i
< model
->num_counters
; ++i
) {
81 rdmsrl(counters
[i
].addr
, counters
[i
].saved
);
84 for (i
= 0; i
< model
->num_controls
; ++i
) {
86 rdmsrl(controls
[i
].addr
, controls
[i
].saved
);
90 static void nmi_save_registers(void *dummy
)
92 int cpu
= smp_processor_id();
93 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
94 nmi_cpu_save_registers(msrs
);
97 static void free_msrs(void)
100 for_each_possible_cpu(i
) {
101 kfree(per_cpu(cpu_msrs
, i
).counters
);
102 per_cpu(cpu_msrs
, i
).counters
= NULL
;
103 kfree(per_cpu(cpu_msrs
, i
).controls
);
104 per_cpu(cpu_msrs
, i
).controls
= NULL
;
108 static int allocate_msrs(void)
111 size_t controls_size
= sizeof(struct op_msr
) * model
->num_controls
;
112 size_t counters_size
= sizeof(struct op_msr
) * model
->num_counters
;
115 for_each_possible_cpu(i
) {
116 per_cpu(cpu_msrs
, i
).counters
= kmalloc(counters_size
,
118 if (!per_cpu(cpu_msrs
, i
).counters
) {
122 per_cpu(cpu_msrs
, i
).controls
= kmalloc(controls_size
,
124 if (!per_cpu(cpu_msrs
, i
).controls
) {
136 static void nmi_cpu_setup(void *dummy
)
138 int cpu
= smp_processor_id();
139 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
140 spin_lock(&oprofilefs_lock
);
141 model
->setup_ctrs(model
, msrs
);
142 spin_unlock(&oprofilefs_lock
);
143 per_cpu(saved_lvtpc
, cpu
) = apic_read(APIC_LVTPC
);
144 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
147 static struct notifier_block profile_exceptions_nb
= {
148 .notifier_call
= profile_exceptions_notify
,
153 static int nmi_setup(void)
158 if (!allocate_msrs())
161 err
= register_die_notifier(&profile_exceptions_nb
);
167 /* We need to serialize save and setup for HT because the subset
168 * of msrs are distinct for save and setup operations
171 /* Assume saved/restored counters are the same on all CPUs */
172 model
->fill_in_addresses(&per_cpu(cpu_msrs
, 0));
173 for_each_possible_cpu(cpu
) {
175 memcpy(per_cpu(cpu_msrs
, cpu
).counters
,
176 per_cpu(cpu_msrs
, 0).counters
,
177 sizeof(struct op_msr
) * model
->num_counters
);
179 memcpy(per_cpu(cpu_msrs
, cpu
).controls
,
180 per_cpu(cpu_msrs
, 0).controls
,
181 sizeof(struct op_msr
) * model
->num_controls
);
185 on_each_cpu(nmi_save_registers
, NULL
, 1);
186 on_each_cpu(nmi_cpu_setup
, NULL
, 1);
191 static void nmi_restore_registers(struct op_msrs
*msrs
)
193 struct op_msr
*counters
= msrs
->counters
;
194 struct op_msr
*controls
= msrs
->controls
;
197 for (i
= 0; i
< model
->num_controls
; ++i
) {
198 if (controls
[i
].addr
)
199 wrmsrl(controls
[i
].addr
, controls
[i
].saved
);
202 for (i
= 0; i
< model
->num_counters
; ++i
) {
203 if (counters
[i
].addr
)
204 wrmsrl(counters
[i
].addr
, counters
[i
].saved
);
208 static void nmi_cpu_shutdown(void *dummy
)
211 int cpu
= smp_processor_id();
212 struct op_msrs
*msrs
= &__get_cpu_var(cpu_msrs
);
214 /* restoring APIC_LVTPC can trigger an apic error because the delivery
215 * mode and vector nr combination can be illegal. That's by design: on
216 * power on apic lvt contain a zero vector nr which are legal only for
217 * NMI delivery mode. So inhibit apic err before restoring lvtpc
219 v
= apic_read(APIC_LVTERR
);
220 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
221 apic_write(APIC_LVTPC
, per_cpu(saved_lvtpc
, cpu
));
222 apic_write(APIC_LVTERR
, v
);
223 nmi_restore_registers(msrs
);
226 static void nmi_shutdown(void)
228 struct op_msrs
*msrs
;
231 on_each_cpu(nmi_cpu_shutdown
, NULL
, 1);
232 unregister_die_notifier(&profile_exceptions_nb
);
233 msrs
= &get_cpu_var(cpu_msrs
);
234 model
->shutdown(msrs
);
236 put_cpu_var(cpu_msrs
);
239 static void nmi_cpu_start(void *dummy
)
241 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
245 static int nmi_start(void)
247 on_each_cpu(nmi_cpu_start
, NULL
, 1);
251 static void nmi_cpu_stop(void *dummy
)
253 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
257 static void nmi_stop(void)
259 on_each_cpu(nmi_cpu_stop
, NULL
, 1);
262 struct op_counter_config counter_config
[OP_MAX_COUNTER
];
264 static int nmi_create_files(struct super_block
*sb
, struct dentry
*root
)
268 for (i
= 0; i
< model
->num_counters
; ++i
) {
272 /* quick little hack to _not_ expose a counter if it is not
273 * available for use. This should protect userspace app.
274 * NOTE: assumes 1:1 mapping here (that counters are organized
275 * sequentially in their struct assignment).
277 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i
)))
280 snprintf(buf
, sizeof(buf
), "%d", i
);
281 dir
= oprofilefs_mkdir(sb
, root
, buf
);
282 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
283 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
284 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
285 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
286 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
287 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
294 static int oprofile_cpu_notifier(struct notifier_block
*b
, unsigned long action
,
297 int cpu
= (unsigned long)data
;
299 case CPU_DOWN_FAILED
:
301 smp_call_function_single(cpu
, nmi_cpu_start
, NULL
, 0);
303 case CPU_DOWN_PREPARE
:
304 smp_call_function_single(cpu
, nmi_cpu_stop
, NULL
, 1);
310 static struct notifier_block oprofile_cpu_nb
= {
311 .notifier_call
= oprofile_cpu_notifier
317 static int nmi_suspend(struct sys_device
*dev
, pm_message_t state
)
319 /* Only one CPU left, just stop that one */
320 if (nmi_enabled
== 1)
325 static int nmi_resume(struct sys_device
*dev
)
327 if (nmi_enabled
== 1)
332 static struct sysdev_class oprofile_sysclass
= {
334 .resume
= nmi_resume
,
335 .suspend
= nmi_suspend
,
338 static struct sys_device device_oprofile
= {
340 .cls
= &oprofile_sysclass
,
343 static int __init
init_sysfs(void)
347 error
= sysdev_class_register(&oprofile_sysclass
);
349 error
= sysdev_register(&device_oprofile
);
353 static void exit_sysfs(void)
355 sysdev_unregister(&device_oprofile
);
356 sysdev_class_unregister(&oprofile_sysclass
);
360 #define init_sysfs() do { } while (0)
361 #define exit_sysfs() do { } while (0)
362 #endif /* CONFIG_PM */
364 static int __init
p4_init(char **cpu_type
)
366 __u8 cpu_model
= boot_cpu_data
.x86_model
;
368 if (cpu_model
> 6 || cpu_model
== 5)
372 *cpu_type
= "i386/p4";
376 switch (smp_num_siblings
) {
378 *cpu_type
= "i386/p4";
383 *cpu_type
= "i386/p4-ht";
384 model
= &op_p4_ht2_spec
;
389 printk(KERN_INFO
"oprofile: P4 HyperThreading detected with > 2 threads\n");
390 printk(KERN_INFO
"oprofile: Reverting to timer mode.\n");
394 static int force_arch_perfmon
;
395 static int force_cpu_type(const char *str
, struct kernel_param
*kp
)
397 if (!strcmp(str
, "archperfmon")) {
398 force_arch_perfmon
= 1;
399 printk(KERN_INFO
"oprofile: forcing architectural perfmon\n");
404 module_param_call(cpu_type
, force_cpu_type
, NULL
, NULL
, 0);
406 static int __init
ppro_init(char **cpu_type
)
408 __u8 cpu_model
= boot_cpu_data
.x86_model
;
409 struct op_x86_model_spec
const *spec
= &op_ppro_spec
; /* default */
411 if (force_arch_perfmon
&& cpu_has_arch_perfmon
)
416 *cpu_type
= "i386/ppro";
419 *cpu_type
= "i386/pii";
423 *cpu_type
= "i386/piii";
427 *cpu_type
= "i386/p6_mobile";
430 *cpu_type
= "i386/core";
433 *cpu_type
= "i386/core_2";
436 spec
= &op_arch_perfmon_spec
;
437 *cpu_type
= "i386/core_i7";
440 *cpu_type
= "i386/atom";
451 /* in order to get sysfs right */
452 static int using_nmi
;
454 int __init
op_nmi_init(struct oprofile_operations
*ops
)
456 __u8 vendor
= boot_cpu_data
.x86_vendor
;
457 __u8 family
= boot_cpu_data
.x86
;
458 char *cpu_type
= NULL
;
466 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
470 cpu_type
= "i386/athlon";
474 * Actually it could be i386/hammer too, but
475 * give user space an consistent name.
477 cpu_type
= "x86-64/hammer";
480 cpu_type
= "x86-64/family10";
483 cpu_type
= "x86-64/family11h";
488 model
= &op_amd_spec
;
491 case X86_VENDOR_INTEL
:
498 /* A P6-class processor */
500 ppro_init(&cpu_type
);
510 if (!cpu_has_arch_perfmon
)
513 /* use arch perfmon as fallback */
514 cpu_type
= "i386/arch_perfmon";
515 model
= &op_arch_perfmon_spec
;
523 register_cpu_notifier(&oprofile_cpu_nb
);
525 /* default values, can be overwritten by model */
526 ops
->create_files
= nmi_create_files
;
527 ops
->setup
= nmi_setup
;
528 ops
->shutdown
= nmi_shutdown
;
529 ops
->start
= nmi_start
;
530 ops
->stop
= nmi_stop
;
531 ops
->cpu_type
= cpu_type
;
534 ret
= model
->init(ops
);
540 printk(KERN_INFO
"oprofile: using NMI interrupt.\n");
544 void op_nmi_exit(void)
549 unregister_cpu_notifier(&oprofile_cpu_nb
);