blob: c8c1df8ff2b479671e9733e6f382f1bfa4feae5c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/nmi.h>
18#include <asm/msr.h>
19#include <asm/apic.h>
20
21#include "op_counter.h"
22#include "op_x86_model.h"
23
24static struct op_x86_model_spec const * model;
25static struct op_msrs cpu_msrs[NR_CPUS];
26static unsigned long saved_lvtpc[NR_CPUS];
27
28static int nmi_start(void);
29static void nmi_stop(void);
30
31/* 0 == registered but off, 1 == registered and on */
32static int nmi_enabled = 0;
33
34#ifdef CONFIG_PM
35
Pavel Machek438510f2005-04-16 15:25:24 -070036static int nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 if (nmi_enabled == 1)
39 nmi_stop();
40 return 0;
41}
42
43
44static int nmi_resume(struct sys_device *dev)
45{
46 if (nmi_enabled == 1)
47 nmi_start();
48 return 0;
49}
50
51
52static struct sysdev_class oprofile_sysclass = {
53 set_kset_name("oprofile"),
54 .resume = nmi_resume,
55 .suspend = nmi_suspend,
56};
57
58
59static struct sys_device device_oprofile = {
60 .id = 0,
61 .cls = &oprofile_sysclass,
62};
63
64
65static int __init init_driverfs(void)
66{
67 int error;
68 if (!(error = sysdev_class_register(&oprofile_sysclass)))
69 error = sysdev_register(&device_oprofile);
70 return error;
71}
72
73
74static void exit_driverfs(void)
75{
76 sysdev_unregister(&device_oprofile);
77 sysdev_class_unregister(&oprofile_sysclass);
78}
79
80#else
81#define init_driverfs() do { } while (0)
82#define exit_driverfs() do { } while (0)
83#endif /* CONFIG_PM */
84
85
86static int nmi_callback(struct pt_regs * regs, int cpu)
87{
88 return model->check_ctrs(regs, &cpu_msrs[cpu]);
89}
90
91
92static void nmi_cpu_save_registers(struct op_msrs * msrs)
93{
94 unsigned int const nr_ctrs = model->num_counters;
95 unsigned int const nr_ctrls = model->num_controls;
96 struct op_msr * counters = msrs->counters;
97 struct op_msr * controls = msrs->controls;
98 unsigned int i;
99
100 for (i = 0; i < nr_ctrs; ++i) {
101 rdmsr(counters[i].addr,
102 counters[i].saved.low,
103 counters[i].saved.high);
104 }
105
106 for (i = 0; i < nr_ctrls; ++i) {
107 rdmsr(controls[i].addr,
108 controls[i].saved.low,
109 controls[i].saved.high);
110 }
111}
112
113
114static void nmi_save_registers(void * dummy)
115{
116 int cpu = smp_processor_id();
117 struct op_msrs * msrs = &cpu_msrs[cpu];
118 model->fill_in_addresses(msrs);
119 nmi_cpu_save_registers(msrs);
120}
121
122
123static void free_msrs(void)
124{
125 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800126 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 kfree(cpu_msrs[i].counters);
128 cpu_msrs[i].counters = NULL;
129 kfree(cpu_msrs[i].controls);
130 cpu_msrs[i].controls = NULL;
131 }
132}
133
134
135static int allocate_msrs(void)
136{
137 int success = 1;
138 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
139 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
140
141 int i;
Andrew Morton394e3902006-03-23 03:01:05 -0800142 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 cpu_msrs[i].counters = kmalloc(counters_size, GFP_KERNEL);
144 if (!cpu_msrs[i].counters) {
145 success = 0;
146 break;
147 }
148 cpu_msrs[i].controls = kmalloc(controls_size, GFP_KERNEL);
149 if (!cpu_msrs[i].controls) {
150 success = 0;
151 break;
152 }
153 }
154
155 if (!success)
156 free_msrs();
157
158 return success;
159}
160
161
162static void nmi_cpu_setup(void * dummy)
163{
164 int cpu = smp_processor_id();
165 struct op_msrs * msrs = &cpu_msrs[cpu];
166 spin_lock(&oprofilefs_lock);
167 model->setup_ctrs(msrs);
168 spin_unlock(&oprofilefs_lock);
169 saved_lvtpc[cpu] = apic_read(APIC_LVTPC);
170 apic_write(APIC_LVTPC, APIC_DM_NMI);
171}
172
173
174static int nmi_setup(void)
175{
176 if (!allocate_msrs())
177 return -ENOMEM;
178
179 /* We walk a thin line between law and rape here.
180 * We need to be careful to install our NMI handler
181 * without actually triggering any NMIs as this will
182 * break the core code horrifically.
183 */
184 if (reserve_lapic_nmi() < 0) {
185 free_msrs();
186 return -EBUSY;
187 }
188 /* We need to serialize save and setup for HT because the subset
189 * of msrs are distinct for save and setup operations
190 */
191 on_each_cpu(nmi_save_registers, NULL, 0, 1);
192 on_each_cpu(nmi_cpu_setup, NULL, 0, 1);
193 set_nmi_callback(nmi_callback);
194 nmi_enabled = 1;
195 return 0;
196}
197
198
199static void nmi_restore_registers(struct op_msrs * msrs)
200{
201 unsigned int const nr_ctrs = model->num_counters;
202 unsigned int const nr_ctrls = model->num_controls;
203 struct op_msr * counters = msrs->counters;
204 struct op_msr * controls = msrs->controls;
205 unsigned int i;
206
207 for (i = 0; i < nr_ctrls; ++i) {
208 wrmsr(controls[i].addr,
209 controls[i].saved.low,
210 controls[i].saved.high);
211 }
212
213 for (i = 0; i < nr_ctrs; ++i) {
214 wrmsr(counters[i].addr,
215 counters[i].saved.low,
216 counters[i].saved.high);
217 }
218}
219
220
221static void nmi_cpu_shutdown(void * dummy)
222{
223 unsigned int v;
224 int cpu = smp_processor_id();
225 struct op_msrs * msrs = &cpu_msrs[cpu];
226
227 /* restoring APIC_LVTPC can trigger an apic error because the delivery
228 * mode and vector nr combination can be illegal. That's by design: on
229 * power on apic lvt contain a zero vector nr which are legal only for
230 * NMI delivery mode. So inhibit apic err before restoring lvtpc
231 */
232 v = apic_read(APIC_LVTERR);
233 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
234 apic_write(APIC_LVTPC, saved_lvtpc[cpu]);
235 apic_write(APIC_LVTERR, v);
236 nmi_restore_registers(msrs);
237}
238
239
240static void nmi_shutdown(void)
241{
242 nmi_enabled = 0;
243 on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
244 unset_nmi_callback();
245 release_lapic_nmi();
246 free_msrs();
247}
248
249
250static void nmi_cpu_start(void * dummy)
251{
252 struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
253 model->start(msrs);
254}
255
256
257static int nmi_start(void)
258{
259 on_each_cpu(nmi_cpu_start, NULL, 0, 1);
260 return 0;
261}
262
263
264static void nmi_cpu_stop(void * dummy)
265{
266 struct op_msrs const * msrs = &cpu_msrs[smp_processor_id()];
267 model->stop(msrs);
268}
269
270
271static void nmi_stop(void)
272{
273 on_each_cpu(nmi_cpu_stop, NULL, 0, 1);
274}
275
276
277struct op_counter_config counter_config[OP_MAX_COUNTER];
278
279static int nmi_create_files(struct super_block * sb, struct dentry * root)
280{
281 unsigned int i;
282
283 for (i = 0; i < model->num_counters; ++i) {
284 struct dentry * dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700285 char buf[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700287 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dir = oprofilefs_mkdir(sb, root, buf);
289 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
290 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
291 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
292 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
293 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
294 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
295 }
296
297 return 0;
298}
299
Andi Kleen1cfcea12006-07-10 17:06:21 +0200300static int p4force;
301module_param(p4force, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303static int __init p4_init(char ** cpu_type)
304{
305 __u8 cpu_model = boot_cpu_data.x86_model;
306
Andi Kleen1cfcea12006-07-10 17:06:21 +0200307 if (!p4force && (cpu_model > 6 || cpu_model == 5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309
310#ifndef CONFIG_SMP
311 *cpu_type = "i386/p4";
312 model = &op_p4_spec;
313 return 1;
314#else
315 switch (smp_num_siblings) {
316 case 1:
317 *cpu_type = "i386/p4";
318 model = &op_p4_spec;
319 return 1;
320
321 case 2:
322 *cpu_type = "i386/p4-ht";
323 model = &op_p4_ht2_spec;
324 return 1;
325 }
326#endif
327
328 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
329 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
330 return 0;
331}
332
333
334static int __init ppro_init(char ** cpu_type)
335{
336 __u8 cpu_model = boot_cpu_data.x86_model;
337
Benjamin LaHaise64471eb2006-05-15 09:44:24 -0700338 if (cpu_model == 14)
339 *cpu_type = "i386/core";
340 else if (cpu_model > 0xd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
Benjamin LaHaise64471eb2006-05-15 09:44:24 -0700342 else if (cpu_model == 9) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *cpu_type = "i386/p6_mobile";
344 } else if (cpu_model > 5) {
345 *cpu_type = "i386/piii";
346 } else if (cpu_model > 2) {
347 *cpu_type = "i386/pii";
348 } else {
349 *cpu_type = "i386/ppro";
350 }
351
352 model = &op_ppro_spec;
353 return 1;
354}
355
356/* in order to get driverfs right */
357static int using_nmi;
358
David Gibson96d08212005-09-06 15:17:26 -0700359int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 __u8 vendor = boot_cpu_data.x86_vendor;
362 __u8 family = boot_cpu_data.x86;
363 char *cpu_type;
364
365 if (!cpu_has_apic)
366 return -ENODEV;
367
368 switch (vendor) {
369 case X86_VENDOR_AMD:
370 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
371
372 switch (family) {
373 default:
374 return -ENODEV;
375 case 6:
376 model = &op_athlon_spec;
377 cpu_type = "i386/athlon";
378 break;
379 case 0xf:
380 model = &op_athlon_spec;
381 /* Actually it could be i386/hammer too, but give
382 user space an consistent name. */
383 cpu_type = "x86-64/hammer";
384 break;
385 }
386 break;
387
388 case X86_VENDOR_INTEL:
389 switch (family) {
390 /* Pentium IV */
391 case 0xf:
392 if (!p4_init(&cpu_type))
393 return -ENODEV;
394 break;
395
396 /* A P6-class processor */
397 case 6:
398 if (!ppro_init(&cpu_type))
399 return -ENODEV;
400 break;
401
402 default:
403 return -ENODEV;
404 }
405 break;
406
407 default:
408 return -ENODEV;
409 }
410
411 init_driverfs();
412 using_nmi = 1;
413 ops->create_files = nmi_create_files;
414 ops->setup = nmi_setup;
415 ops->shutdown = nmi_shutdown;
416 ops->start = nmi_start;
417 ops->stop = nmi_stop;
418 ops->cpu_type = cpu_type;
419 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
420 return 0;
421}
422
423
David Gibson96d08212005-09-06 15:17:26 -0700424void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 if (using_nmi)
427 exit_driverfs();
428}