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 static int nmi_start(void);
32 static void nmi_stop(void);
33 static void nmi_cpu_start(void *dummy
);
34 static void nmi_cpu_stop(void *dummy
);
36 /* 0 == registered but off, 1 == registered and on */
37 static int nmi_enabled
= 0;
40 static int oprofile_cpu_notifier(struct notifier_block
*b
, unsigned long action
,
43 int cpu
= (unsigned long)data
;
47 smp_call_function_single(cpu
, nmi_cpu_start
, NULL
, 0);
49 case CPU_DOWN_PREPARE
:
50 smp_call_function_single(cpu
, nmi_cpu_stop
, NULL
, 1);
56 static struct notifier_block oprofile_cpu_nb
= {
57 .notifier_call
= oprofile_cpu_notifier
63 static int nmi_suspend(struct sys_device
*dev
, pm_message_t state
)
65 /* Only one CPU left, just stop that one */
71 static int nmi_resume(struct sys_device
*dev
)
78 static struct sysdev_class oprofile_sysclass
= {
81 .suspend
= nmi_suspend
,
84 static struct sys_device device_oprofile
= {
86 .cls
= &oprofile_sysclass
,
89 static int __init
init_sysfs(void)
93 error
= sysdev_class_register(&oprofile_sysclass
);
95 error
= sysdev_register(&device_oprofile
);
99 static void exit_sysfs(void)
101 sysdev_unregister(&device_oprofile
);
102 sysdev_class_unregister(&oprofile_sysclass
);
106 #define init_sysfs() do { } while (0)
107 #define exit_sysfs() do { } while (0)
108 #endif /* CONFIG_PM */
110 static int profile_exceptions_notify(struct notifier_block
*self
,
111 unsigned long val
, void *data
)
113 struct die_args
*args
= (struct die_args
*)data
;
114 int ret
= NOTIFY_DONE
;
115 int cpu
= smp_processor_id();
119 if (model
->check_ctrs(args
->regs
, &per_cpu(cpu_msrs
, cpu
)))
128 static void nmi_cpu_save_registers(struct op_msrs
*msrs
)
130 unsigned int const nr_ctrs
= model
->num_counters
;
131 unsigned int const nr_ctrls
= model
->num_controls
;
132 struct op_msr
*counters
= msrs
->counters
;
133 struct op_msr
*controls
= msrs
->controls
;
136 for (i
= 0; i
< nr_ctrs
; ++i
) {
137 if (counters
[i
].addr
) {
138 rdmsr(counters
[i
].addr
,
139 counters
[i
].saved
.low
,
140 counters
[i
].saved
.high
);
144 for (i
= 0; i
< nr_ctrls
; ++i
) {
145 if (controls
[i
].addr
) {
146 rdmsr(controls
[i
].addr
,
147 controls
[i
].saved
.low
,
148 controls
[i
].saved
.high
);
153 static void nmi_save_registers(void *dummy
)
155 int cpu
= smp_processor_id();
156 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
157 nmi_cpu_save_registers(msrs
);
160 static void free_msrs(void)
163 for_each_possible_cpu(i
) {
164 kfree(per_cpu(cpu_msrs
, i
).counters
);
165 per_cpu(cpu_msrs
, i
).counters
= NULL
;
166 kfree(per_cpu(cpu_msrs
, i
).controls
);
167 per_cpu(cpu_msrs
, i
).controls
= NULL
;
171 static int allocate_msrs(void)
174 size_t controls_size
= sizeof(struct op_msr
) * model
->num_controls
;
175 size_t counters_size
= sizeof(struct op_msr
) * model
->num_counters
;
178 for_each_possible_cpu(i
) {
179 per_cpu(cpu_msrs
, i
).counters
= kmalloc(counters_size
,
181 if (!per_cpu(cpu_msrs
, i
).counters
) {
185 per_cpu(cpu_msrs
, i
).controls
= kmalloc(controls_size
,
187 if (!per_cpu(cpu_msrs
, i
).controls
) {
199 static void nmi_cpu_setup(void *dummy
)
201 int cpu
= smp_processor_id();
202 struct op_msrs
*msrs
= &per_cpu(cpu_msrs
, cpu
);
203 spin_lock(&oprofilefs_lock
);
204 model
->setup_ctrs(msrs
);
205 spin_unlock(&oprofilefs_lock
);
206 per_cpu(saved_lvtpc
, cpu
) = apic_read(APIC_LVTPC
);
207 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
210 static struct notifier_block profile_exceptions_nb
= {
211 .notifier_call
= profile_exceptions_notify
,
216 static int nmi_setup(void)
221 if (!allocate_msrs())
224 err
= register_die_notifier(&profile_exceptions_nb
);
230 /* We need to serialize save and setup for HT because the subset
231 * of msrs are distinct for save and setup operations
234 /* Assume saved/restored counters are the same on all CPUs */
235 model
->fill_in_addresses(&per_cpu(cpu_msrs
, 0));
236 for_each_possible_cpu(cpu
) {
238 memcpy(per_cpu(cpu_msrs
, cpu
).counters
,
239 per_cpu(cpu_msrs
, 0).counters
,
240 sizeof(struct op_msr
) * model
->num_counters
);
242 memcpy(per_cpu(cpu_msrs
, cpu
).controls
,
243 per_cpu(cpu_msrs
, 0).controls
,
244 sizeof(struct op_msr
) * model
->num_controls
);
248 on_each_cpu(nmi_save_registers
, NULL
, 1);
249 on_each_cpu(nmi_cpu_setup
, NULL
, 1);
254 static void nmi_restore_registers(struct op_msrs
*msrs
)
256 unsigned int const nr_ctrs
= model
->num_counters
;
257 unsigned int const nr_ctrls
= model
->num_controls
;
258 struct op_msr
*counters
= msrs
->counters
;
259 struct op_msr
*controls
= msrs
->controls
;
262 for (i
= 0; i
< nr_ctrls
; ++i
) {
263 if (controls
[i
].addr
) {
264 wrmsr(controls
[i
].addr
,
265 controls
[i
].saved
.low
,
266 controls
[i
].saved
.high
);
270 for (i
= 0; i
< nr_ctrs
; ++i
) {
271 if (counters
[i
].addr
) {
272 wrmsr(counters
[i
].addr
,
273 counters
[i
].saved
.low
,
274 counters
[i
].saved
.high
);
279 static void nmi_cpu_shutdown(void *dummy
)
282 int cpu
= smp_processor_id();
283 struct op_msrs
*msrs
= &__get_cpu_var(cpu_msrs
);
285 /* restoring APIC_LVTPC can trigger an apic error because the delivery
286 * mode and vector nr combination can be illegal. That's by design: on
287 * power on apic lvt contain a zero vector nr which are legal only for
288 * NMI delivery mode. So inhibit apic err before restoring lvtpc
290 v
= apic_read(APIC_LVTERR
);
291 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
292 apic_write(APIC_LVTPC
, per_cpu(saved_lvtpc
, cpu
));
293 apic_write(APIC_LVTERR
, v
);
294 nmi_restore_registers(msrs
);
297 static void nmi_shutdown(void)
299 struct op_msrs
*msrs
= &get_cpu_var(cpu_msrs
);
301 on_each_cpu(nmi_cpu_shutdown
, NULL
, 1);
302 unregister_die_notifier(&profile_exceptions_nb
);
303 model
->shutdown(msrs
);
305 put_cpu_var(cpu_msrs
);
308 static void nmi_cpu_start(void *dummy
)
310 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
314 static int nmi_start(void)
316 on_each_cpu(nmi_cpu_start
, NULL
, 1);
320 static void nmi_cpu_stop(void *dummy
)
322 struct op_msrs
const *msrs
= &__get_cpu_var(cpu_msrs
);
326 static void nmi_stop(void)
328 on_each_cpu(nmi_cpu_stop
, NULL
, 1);
331 struct op_counter_config counter_config
[OP_MAX_COUNTER
];
333 static int nmi_create_files(struct super_block
*sb
, struct dentry
*root
)
337 for (i
= 0; i
< model
->num_counters
; ++i
) {
341 /* quick little hack to _not_ expose a counter if it is not
342 * available for use. This should protect userspace app.
343 * NOTE: assumes 1:1 mapping here (that counters are organized
344 * sequentially in their struct assignment).
346 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i
)))
349 snprintf(buf
, sizeof(buf
), "%d", i
);
350 dir
= oprofilefs_mkdir(sb
, root
, buf
);
351 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
352 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
353 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
354 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
355 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
356 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
363 module_param(p4force
, int, 0);
365 static int __init
p4_init(char **cpu_type
)
367 __u8 cpu_model
= boot_cpu_data
.x86_model
;
369 if (!p4force
&& (cpu_model
> 6 || cpu_model
== 5))
373 *cpu_type
= "i386/p4";
377 switch (smp_num_siblings
) {
379 *cpu_type
= "i386/p4";
384 *cpu_type
= "i386/p4-ht";
385 model
= &op_p4_ht2_spec
;
390 printk(KERN_INFO
"oprofile: P4 HyperThreading detected with > 2 threads\n");
391 printk(KERN_INFO
"oprofile: Reverting to timer mode.\n");
395 static int __init
ppro_init(char **cpu_type
)
397 __u8 cpu_model
= boot_cpu_data
.x86_model
;
401 *cpu_type
= "i386/ppro";
404 *cpu_type
= "i386/pii";
407 *cpu_type
= "i386/piii";
410 *cpu_type
= "i386/p6_mobile";
413 *cpu_type
= "i386/p6";
416 *cpu_type
= "i386/core";
419 *cpu_type
= "i386/core_2";
422 *cpu_type
= "i386/core_2";
429 model
= &op_ppro_spec
;
433 /* in order to get sysfs right */
434 static int using_nmi
;
436 int __init
op_nmi_init(struct oprofile_operations
*ops
)
438 __u8 vendor
= boot_cpu_data
.x86_vendor
;
439 __u8 family
= boot_cpu_data
.x86
;
448 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
454 model
= &op_amd_spec
;
455 cpu_type
= "i386/athlon";
458 model
= &op_amd_spec
;
459 /* Actually it could be i386/hammer too, but give
460 user space an consistent name. */
461 cpu_type
= "x86-64/hammer";
464 model
= &op_amd_spec
;
465 cpu_type
= "x86-64/family10";
468 model
= &op_amd_spec
;
469 cpu_type
= "x86-64/family11h";
474 case X86_VENDOR_INTEL
:
478 if (!p4_init(&cpu_type
))
482 /* A P6-class processor */
484 if (!ppro_init(&cpu_type
))
498 register_cpu_notifier(&oprofile_cpu_nb
);
500 /* default values, can be overwritten by model */
501 ops
->create_files
= nmi_create_files
;
502 ops
->setup
= nmi_setup
;
503 ops
->shutdown
= nmi_shutdown
;
504 ops
->start
= nmi_start
;
505 ops
->stop
= nmi_stop
;
506 ops
->cpu_type
= cpu_type
;
509 ret
= model
->init(ops
);
515 printk(KERN_INFO
"oprofile: using NMI interrupt.\n");
519 void op_nmi_exit(void)
524 unregister_cpu_notifier(&oprofile_cpu_nb
);