4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
10 #include <linux/init.h>
11 #include <linux/notifier.h>
12 #include <linux/smp.h>
13 #include <linux/oprofile.h>
14 #include <linux/sysdev.h>
15 #include <linux/slab.h>
16 #include <linux/moduleparam.h>
17 #include <linux/kdebug.h>
22 #include "op_counter.h"
23 #include "op_x86_model.h"
25 static struct op_x86_model_spec
const *model
;
26 static struct op_msrs cpu_msrs
[NR_CPUS
];
27 static unsigned long saved_lvtpc
[NR_CPUS
];
29 static int nmi_start(void);
30 static void nmi_stop(void);
32 /* 0 == registered but off, 1 == registered and on */
33 static int nmi_enabled
= 0;
37 static int nmi_suspend(struct sys_device
*dev
, pm_message_t state
)
44 static int nmi_resume(struct sys_device
*dev
)
51 static struct sysdev_class oprofile_sysclass
= {
54 .suspend
= nmi_suspend
,
57 static struct sys_device device_oprofile
= {
59 .cls
= &oprofile_sysclass
,
62 static int __init
init_sysfs(void)
66 error
= sysdev_class_register(&oprofile_sysclass
);
68 error
= sysdev_register(&device_oprofile
);
72 static void exit_sysfs(void)
74 sysdev_unregister(&device_oprofile
);
75 sysdev_class_unregister(&oprofile_sysclass
);
79 #define init_sysfs() do { } while (0)
80 #define exit_sysfs() do { } while (0)
81 #endif /* CONFIG_PM */
83 static int profile_exceptions_notify(struct notifier_block
*self
,
84 unsigned long val
, void *data
)
86 struct die_args
*args
= (struct die_args
*)data
;
87 int ret
= NOTIFY_DONE
;
88 int cpu
= smp_processor_id();
92 if (model
->check_ctrs(args
->regs
, &cpu_msrs
[cpu
]))
101 static void nmi_cpu_save_registers(struct op_msrs
*msrs
)
103 unsigned int const nr_ctrs
= model
->num_counters
;
104 unsigned int const nr_ctrls
= model
->num_controls
;
105 struct op_msr
*counters
= msrs
->counters
;
106 struct op_msr
*controls
= msrs
->controls
;
109 for (i
= 0; i
< nr_ctrs
; ++i
) {
110 if (counters
[i
].addr
) {
111 rdmsr(counters
[i
].addr
,
112 counters
[i
].saved
.low
,
113 counters
[i
].saved
.high
);
117 for (i
= 0; i
< nr_ctrls
; ++i
) {
118 if (controls
[i
].addr
) {
119 rdmsr(controls
[i
].addr
,
120 controls
[i
].saved
.low
,
121 controls
[i
].saved
.high
);
126 static void nmi_save_registers(void *dummy
)
128 int cpu
= smp_processor_id();
129 struct op_msrs
*msrs
= &cpu_msrs
[cpu
];
130 nmi_cpu_save_registers(msrs
);
133 static void free_msrs(void)
136 for_each_possible_cpu(i
) {
137 kfree(cpu_msrs
[i
].counters
);
138 cpu_msrs
[i
].counters
= NULL
;
139 kfree(cpu_msrs
[i
].controls
);
140 cpu_msrs
[i
].controls
= NULL
;
144 static int allocate_msrs(void)
147 size_t controls_size
= sizeof(struct op_msr
) * model
->num_controls
;
148 size_t counters_size
= sizeof(struct op_msr
) * model
->num_counters
;
151 for_each_possible_cpu(i
) {
152 cpu_msrs
[i
].counters
= kmalloc(counters_size
, GFP_KERNEL
);
153 if (!cpu_msrs
[i
].counters
) {
157 cpu_msrs
[i
].controls
= kmalloc(controls_size
, GFP_KERNEL
);
158 if (!cpu_msrs
[i
].controls
) {
170 static void nmi_cpu_setup(void *dummy
)
172 int cpu
= smp_processor_id();
173 struct op_msrs
*msrs
= &cpu_msrs
[cpu
];
174 spin_lock(&oprofilefs_lock
);
175 model
->setup_ctrs(msrs
);
176 spin_unlock(&oprofilefs_lock
);
177 saved_lvtpc
[cpu
] = apic_read(APIC_LVTPC
);
178 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
181 static struct notifier_block profile_exceptions_nb
= {
182 .notifier_call
= profile_exceptions_notify
,
187 static int nmi_setup(void)
192 if (!allocate_msrs())
195 err
= register_die_notifier(&profile_exceptions_nb
);
201 /* We need to serialize save and setup for HT because the subset
202 * of msrs are distinct for save and setup operations
205 /* Assume saved/restored counters are the same on all CPUs */
206 model
->fill_in_addresses(&cpu_msrs
[0]);
207 for_each_possible_cpu(cpu
) {
209 memcpy(cpu_msrs
[cpu
].counters
, cpu_msrs
[0].counters
,
210 sizeof(struct op_msr
) * model
->num_counters
);
212 memcpy(cpu_msrs
[cpu
].controls
, cpu_msrs
[0].controls
,
213 sizeof(struct op_msr
) * model
->num_controls
);
217 on_each_cpu(nmi_save_registers
, NULL
, 0, 1);
218 on_each_cpu(nmi_cpu_setup
, NULL
, 0, 1);
223 static void nmi_restore_registers(struct op_msrs
*msrs
)
225 unsigned int const nr_ctrs
= model
->num_counters
;
226 unsigned int const nr_ctrls
= model
->num_controls
;
227 struct op_msr
*counters
= msrs
->counters
;
228 struct op_msr
*controls
= msrs
->controls
;
231 for (i
= 0; i
< nr_ctrls
; ++i
) {
232 if (controls
[i
].addr
) {
233 wrmsr(controls
[i
].addr
,
234 controls
[i
].saved
.low
,
235 controls
[i
].saved
.high
);
239 for (i
= 0; i
< nr_ctrs
; ++i
) {
240 if (counters
[i
].addr
) {
241 wrmsr(counters
[i
].addr
,
242 counters
[i
].saved
.low
,
243 counters
[i
].saved
.high
);
248 static void nmi_cpu_shutdown(void *dummy
)
251 int cpu
= smp_processor_id();
252 struct op_msrs
*msrs
= &cpu_msrs
[cpu
];
254 /* restoring APIC_LVTPC can trigger an apic error because the delivery
255 * mode and vector nr combination can be illegal. That's by design: on
256 * power on apic lvt contain a zero vector nr which are legal only for
257 * NMI delivery mode. So inhibit apic err before restoring lvtpc
259 v
= apic_read(APIC_LVTERR
);
260 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
261 apic_write(APIC_LVTPC
, saved_lvtpc
[cpu
]);
262 apic_write(APIC_LVTERR
, v
);
263 nmi_restore_registers(msrs
);
266 static void nmi_shutdown(void)
269 on_each_cpu(nmi_cpu_shutdown
, NULL
, 0, 1);
270 unregister_die_notifier(&profile_exceptions_nb
);
271 model
->shutdown(cpu_msrs
);
275 static void nmi_cpu_start(void *dummy
)
277 struct op_msrs
const *msrs
= &cpu_msrs
[smp_processor_id()];
281 static int nmi_start(void)
283 on_each_cpu(nmi_cpu_start
, NULL
, 0, 1);
287 static void nmi_cpu_stop(void *dummy
)
289 struct op_msrs
const *msrs
= &cpu_msrs
[smp_processor_id()];
293 static void nmi_stop(void)
295 on_each_cpu(nmi_cpu_stop
, NULL
, 0, 1);
298 struct op_counter_config counter_config
[OP_MAX_COUNTER
];
300 static int nmi_create_files(struct super_block
*sb
, struct dentry
*root
)
304 for (i
= 0; i
< model
->num_counters
; ++i
) {
308 /* quick little hack to _not_ expose a counter if it is not
309 * available for use. This should protect userspace app.
310 * NOTE: assumes 1:1 mapping here (that counters are organized
311 * sequentially in their struct assignment).
313 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i
)))
316 snprintf(buf
, sizeof(buf
), "%d", i
);
317 dir
= oprofilefs_mkdir(sb
, root
, buf
);
318 oprofilefs_create_ulong(sb
, dir
, "enabled", &counter_config
[i
].enabled
);
319 oprofilefs_create_ulong(sb
, dir
, "event", &counter_config
[i
].event
);
320 oprofilefs_create_ulong(sb
, dir
, "count", &counter_config
[i
].count
);
321 oprofilefs_create_ulong(sb
, dir
, "unit_mask", &counter_config
[i
].unit_mask
);
322 oprofilefs_create_ulong(sb
, dir
, "kernel", &counter_config
[i
].kernel
);
323 oprofilefs_create_ulong(sb
, dir
, "user", &counter_config
[i
].user
);
330 module_param(p4force
, int, 0);
332 static int __init
p4_init(char **cpu_type
)
334 __u8 cpu_model
= boot_cpu_data
.x86_model
;
336 if (!p4force
&& (cpu_model
> 6 || cpu_model
== 5))
340 *cpu_type
= "i386/p4";
344 switch (smp_num_siblings
) {
346 *cpu_type
= "i386/p4";
351 *cpu_type
= "i386/p4-ht";
352 model
= &op_p4_ht2_spec
;
357 printk(KERN_INFO
"oprofile: P4 HyperThreading detected with > 2 threads\n");
358 printk(KERN_INFO
"oprofile: Reverting to timer mode.\n");
362 static int __init
ppro_init(char **cpu_type
)
364 __u8 cpu_model
= boot_cpu_data
.x86_model
;
367 *cpu_type
= "i386/core";
368 else if (cpu_model
== 15 || cpu_model
== 23)
369 *cpu_type
= "i386/core_2";
370 else if (cpu_model
> 0xd)
372 else if (cpu_model
== 9) {
373 *cpu_type
= "i386/p6_mobile";
374 } else if (cpu_model
> 5) {
375 *cpu_type
= "i386/piii";
376 } else if (cpu_model
> 2) {
377 *cpu_type
= "i386/pii";
379 *cpu_type
= "i386/ppro";
382 model
= &op_ppro_spec
;
386 /* in order to get sysfs right */
387 static int using_nmi
;
389 int __init
op_nmi_init(struct oprofile_operations
*ops
)
391 __u8 vendor
= boot_cpu_data
.x86_vendor
;
392 __u8 family
= boot_cpu_data
.x86
;
400 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
406 model
= &op_athlon_spec
;
407 cpu_type
= "i386/athlon";
410 model
= &op_athlon_spec
;
411 /* Actually it could be i386/hammer too, but give
412 user space an consistent name. */
413 cpu_type
= "x86-64/hammer";
416 model
= &op_athlon_spec
;
417 cpu_type
= "x86-64/family10";
422 case X86_VENDOR_INTEL
:
426 if (!p4_init(&cpu_type
))
430 /* A P6-class processor */
432 if (!ppro_init(&cpu_type
))
447 ops
->create_files
= nmi_create_files
;
448 ops
->setup
= nmi_setup
;
449 ops
->shutdown
= nmi_shutdown
;
450 ops
->start
= nmi_start
;
451 ops
->stop
= nmi_stop
;
452 ops
->cpu_type
= cpu_type
;
453 printk(KERN_INFO
"oprofile: using NMI interrupt.\n");
457 void op_nmi_exit(void)