blob: a7c0783b269a5ecaf9d74feaa4d8b8350291a7df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file nmi_int.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon <levon@movementarian.org>
8 */
9
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>
Andi Kleen1cfcea12006-07-10 17:06:21 +020016#include <linux/moduleparam.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070017#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/nmi.h>
19#include <asm/msr.h>
20#include <asm/apic.h>
21
22#include "op_counter.h"
23#include "op_x86_model.h"
Don Zickus2fbe7b22006-09-26 10:52:27 +020024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static struct op_x86_model_spec const * model;
26static struct op_msrs cpu_msrs[NR_CPUS];
27static unsigned long saved_lvtpc[NR_CPUS];
Don Zickus2fbe7b22006-09-26 10:52:27 +020028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int nmi_start(void);
30static void nmi_stop(void);
31
32/* 0 == registered but off, 1 == registered and on */
33static int nmi_enabled = 0;
34
35#ifdef CONFIG_PM
36
Pavel Machek438510f2005-04-16 15:25:24 -070037static int nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 if (nmi_enabled == 1)
40 nmi_stop();
41 return 0;
42}
43
44
45static int nmi_resume(struct sys_device *dev)
46{
47 if (nmi_enabled == 1)
48 nmi_start();
49 return 0;
50}
51
52
53static struct sysdev_class oprofile_sysclass = {
54 set_kset_name("oprofile"),
55 .resume = nmi_resume,
56 .suspend = nmi_suspend,
57};
58
59
60static struct sys_device device_oprofile = {
61 .id = 0,
62 .cls = &oprofile_sysclass,
63};
64
65
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010066static int __init init_sysfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 int error;
69 if (!(error = sysdev_class_register(&oprofile_sysclass)))
70 error = sysdev_register(&device_oprofile);
71 return error;
72}
73
74
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010075static void exit_sysfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 sysdev_unregister(&device_oprofile);
78 sysdev_class_unregister(&oprofile_sysclass);
79}
80
81#else
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010082#define init_sysfs() do { } while (0)
83#define exit_sysfs() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif /* CONFIG_PM */
85
Adrian Bunkc7c19f82006-09-26 10:52:27 +020086static int profile_exceptions_notify(struct notifier_block *self,
87 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Don Zickus2fbe7b22006-09-26 10:52:27 +020089 struct die_args *args = (struct die_args *)data;
90 int ret = NOTIFY_DONE;
91 int cpu = smp_processor_id();
92
93 switch(val) {
94 case DIE_NMI:
95 if (model->check_ctrs(args->regs, &cpu_msrs[cpu]))
96 ret = NOTIFY_STOP;
97 break;
98 default:
99 break;
100 }
101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Don Zickus2fbe7b22006-09-26 10:52:27 +0200103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static void nmi_cpu_save_registers(struct op_msrs * msrs)
105{
106 unsigned int const nr_ctrs = model->num_counters;
107 unsigned int const nr_ctrls = model->num_controls;
108 struct op_msr * counters = msrs->counters;
109 struct op_msr * controls = msrs->controls;
110 unsigned int i;
111
112 for (i = 0; i < nr_ctrs; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200113 if (counters[i].addr){
114 rdmsr(counters[i].addr,
115 counters[i].saved.low,
116 counters[i].saved.high);
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
120 for (i = 0; i < nr_ctrls; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200121 if (controls[i].addr){
122 rdmsr(controls[i].addr,
123 controls[i].saved.low,
124 controls[i].saved.high);
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127}
128
129
130static void nmi_save_registers(void * dummy)
131{
132 int cpu = smp_processor_id();
133 struct op_msrs * msrs = &cpu_msrs[cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 nmi_cpu_save_registers(msrs);
135}
136
137
138static void free_msrs(void)
139{
140 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800141 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 kfree(cpu_msrs[i].counters);
143 cpu_msrs[i].counters = NULL;
144 kfree(cpu_msrs[i].controls);
145 cpu_msrs[i].controls = NULL;
146 }
147}
148
149
150static int allocate_msrs(void)
151{
152 int success = 1;
153 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
154 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
155
156 int i;
Andrew Morton394e3902006-03-23 03:01:05 -0800157 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 cpu_msrs[i].counters = kmalloc(counters_size, GFP_KERNEL);
159 if (!cpu_msrs[i].counters) {
160 success = 0;
161 break;
162 }
163 cpu_msrs[i].controls = kmalloc(controls_size, GFP_KERNEL);
164 if (!cpu_msrs[i].controls) {
165 success = 0;
166 break;
167 }
168 }
169
170 if (!success)
171 free_msrs();
172
173 return success;
174}
175
176
177static void nmi_cpu_setup(void * dummy)
178{
179 int cpu = smp_processor_id();
180 struct op_msrs * msrs = &cpu_msrs[cpu];
181 spin_lock(&oprofilefs_lock);
182 model->setup_ctrs(msrs);
183 spin_unlock(&oprofilefs_lock);
184 saved_lvtpc[cpu] = apic_read(APIC_LVTPC);
185 apic_write(APIC_LVTPC, APIC_DM_NMI);
186}
187
Don Zickus2fbe7b22006-09-26 10:52:27 +0200188static struct notifier_block profile_exceptions_nb = {
189 .notifier_call = profile_exceptions_notify,
190 .next = NULL,
191 .priority = 0
192};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194static int nmi_setup(void)
195{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200196 int err=0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200197 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!allocate_msrs())
200 return -ENOMEM;
201
Don Zickus2fbe7b22006-09-26 10:52:27 +0200202 if ((err = register_die_notifier(&profile_exceptions_nb))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* We need to serialize save and setup for HT because the subset
208 * of msrs are distinct for save and setup operations
209 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200210
211 /* Assume saved/restored counters are the same on all CPUs */
212 model->fill_in_addresses(&cpu_msrs[0]);
213 for_each_possible_cpu (cpu) {
214 if (cpu != 0)
215 cpu_msrs[cpu] = cpu_msrs[0];
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 on_each_cpu(nmi_save_registers, NULL, 0, 1);
218 on_each_cpu(nmi_cpu_setup, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 nmi_enabled = 1;
220 return 0;
221}
222
223
224static void nmi_restore_registers(struct op_msrs * msrs)
225{
226 unsigned int const nr_ctrs = model->num_counters;
227 unsigned int const nr_ctrls = model->num_controls;
228 struct op_msr * counters = msrs->counters;
229 struct op_msr * controls = msrs->controls;
230 unsigned int i;
231
232 for (i = 0; i < nr_ctrls; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200233 if (controls[i].addr){
234 wrmsr(controls[i].addr,
235 controls[i].saved.low,
236 controls[i].saved.high);
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
240 for (i = 0; i < nr_ctrs; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200241 if (counters[i].addr){
242 wrmsr(counters[i].addr,
243 counters[i].saved.low,
244 counters[i].saved.high);
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247}
248
249
250static void nmi_cpu_shutdown(void * dummy)
251{
252 unsigned int v;
253 int cpu = smp_processor_id();
254 struct op_msrs * msrs = &cpu_msrs[cpu];
255
256 /* restoring APIC_LVTPC can trigger an apic error because the delivery
257 * mode and vector nr combination can be illegal. That's by design: on
258 * power on apic lvt contain a zero vector nr which are legal only for
259 * NMI delivery mode. So inhibit apic err before restoring lvtpc
260 */
261 v = apic_read(APIC_LVTERR);
262 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
263 apic_write(APIC_LVTPC, saved_lvtpc[cpu]);
264 apic_write(APIC_LVTERR, v);
265 nmi_restore_registers(msrs);
Don Zickuscb9c4482006-09-26 10:52:26 +0200266 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270static void nmi_shutdown(void)
271{
272 nmi_enabled = 0;
273 on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200274 unregister_die_notifier(&profile_exceptions_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 free_msrs();
276}
277
278
279static void nmi_cpu_start(void * dummy)
280{
281 struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
282 model->start(msrs);
283}
284
285
286static int nmi_start(void)
287{
288 on_each_cpu(nmi_cpu_start, NULL, 0, 1);
289 return 0;
290}
291
292
293static void nmi_cpu_stop(void * dummy)
294{
295 struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
296 model->stop(msrs);
297}
298
299
300static void nmi_stop(void)
301{
302 on_each_cpu(nmi_cpu_stop, NULL, 0, 1);
303}
304
305
306struct op_counter_config counter_config[OP_MAX_COUNTER];
307
308static int nmi_create_files(struct super_block * sb, struct dentry * root)
309{
310 unsigned int i;
311
312 for (i = 0; i < model->num_counters; ++i) {
313 struct dentry * dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700314 char buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Don Zickuscb9c4482006-09-26 10:52:26 +0200316 /* quick little hack to _not_ expose a counter if it is not
317 * available for use. This should protect userspace app.
318 * NOTE: assumes 1:1 mapping here (that counters are organized
319 * sequentially in their struct assignment).
320 */
321 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
322 continue;
323
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700324 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 dir = oprofilefs_mkdir(sb, root, buf);
326 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
327 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
328 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
329 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
330 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
331 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
332 }
333
334 return 0;
335}
336
Andi Kleen1cfcea12006-07-10 17:06:21 +0200337static int p4force;
338module_param(p4force, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340static int __init p4_init(char ** cpu_type)
341{
342 __u8 cpu_model = boot_cpu_data.x86_model;
343
Andi Kleen1cfcea12006-07-10 17:06:21 +0200344 if (!p4force && (cpu_model > 6 || cpu_model == 5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346
347#ifndef CONFIG_SMP
348 *cpu_type = "i386/p4";
349 model = &op_p4_spec;
350 return 1;
351#else
352 switch (smp_num_siblings) {
353 case 1:
354 *cpu_type = "i386/p4";
355 model = &op_p4_spec;
356 return 1;
357
358 case 2:
359 *cpu_type = "i386/p4-ht";
360 model = &op_p4_ht2_spec;
361 return 1;
362 }
363#endif
364
365 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
366 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
367 return 0;
368}
369
370
371static int __init ppro_init(char ** cpu_type)
372{
373 __u8 cpu_model = boot_cpu_data.x86_model;
374
Benjamin LaHaise64471eb2006-05-15 09:44:24 -0700375 if (cpu_model == 14)
376 *cpu_type = "i386/core";
Benjamin LaHaisef04b92e2006-09-16 16:35:46 -0700377 else if (cpu_model == 15)
378 *cpu_type = "i386/core_2";
Benjamin LaHaise64471eb2006-05-15 09:44:24 -0700379 else if (cpu_model > 0xd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
Benjamin LaHaise64471eb2006-05-15 09:44:24 -0700381 else if (cpu_model == 9) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *cpu_type = "i386/p6_mobile";
383 } else if (cpu_model > 5) {
384 *cpu_type = "i386/piii";
385 } else if (cpu_model > 2) {
386 *cpu_type = "i386/pii";
387 } else {
388 *cpu_type = "i386/ppro";
389 }
390
391 model = &op_ppro_spec;
392 return 1;
393}
394
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100395/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static int using_nmi;
397
David Gibson96d08212005-09-06 15:17:26 -0700398int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 __u8 vendor = boot_cpu_data.x86_vendor;
401 __u8 family = boot_cpu_data.x86;
402 char *cpu_type;
403
404 if (!cpu_has_apic)
405 return -ENODEV;
406
407 switch (vendor) {
408 case X86_VENDOR_AMD:
409 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
410
411 switch (family) {
412 default:
413 return -ENODEV;
414 case 6:
415 model = &op_athlon_spec;
416 cpu_type = "i386/athlon";
417 break;
418 case 0xf:
419 model = &op_athlon_spec;
420 /* Actually it could be i386/hammer too, but give
421 user space an consistent name. */
422 cpu_type = "x86-64/hammer";
423 break;
Andi Kleen2a126522007-05-02 19:27:06 +0200424 case 0x10:
425 model = &op_athlon_spec;
426 cpu_type = "x86-64/family10";
427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 break;
430
431 case X86_VENDOR_INTEL:
432 switch (family) {
433 /* Pentium IV */
434 case 0xf:
435 if (!p4_init(&cpu_type))
436 return -ENODEV;
437 break;
438
439 /* A P6-class processor */
440 case 6:
441 if (!ppro_init(&cpu_type))
442 return -ENODEV;
443 break;
444
445 default:
446 return -ENODEV;
447 }
448 break;
449
450 default:
451 return -ENODEV;
452 }
453
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100454 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 using_nmi = 1;
456 ops->create_files = nmi_create_files;
457 ops->setup = nmi_setup;
458 ops->shutdown = nmi_shutdown;
459 ops->start = nmi_start;
460 ops->stop = nmi_stop;
461 ops->cpu_type = cpu_type;
462 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
463 return 0;
464}
465
466
David Gibson96d08212005-09-06 15:17:26 -0700467void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 if (using_nmi)
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100470 exit_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}