blob: c638685136e1cde308d33f7dde697d769ab3e8b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file nmi_int.c
3 *
Robert Richteradf5ec02008-07-22 21:08:48 +02004 * @remark Copyright 2002-2008 OProfile authors
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * @remark Read the file COPYING
6 *
7 * @author John Levon <levon@movementarian.org>
Robert Richteradf5ec02008-07-22 21:08:48 +02008 * @author Robert Richter <robert.richter@amd.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
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>
Andi Kleen1cfcea12006-07-10 17:06:21 +020017#include <linux/moduleparam.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070018#include <linux/kdebug.h>
Andi Kleen80a8c9f2008-08-19 03:13:38 +020019#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/nmi.h>
21#include <asm/msr.h>
22#include <asm/apic.h>
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "op_counter.h"
25#include "op_x86_model.h"
Don Zickus2fbe7b22006-09-26 10:52:27 +020026
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010027static struct op_x86_model_spec const *model;
Mike Travisd18d00f2008-03-25 15:06:59 -070028static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
29static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
Don Zickus2fbe7b22006-09-26 10:52:27 +020030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* 0 == registered but off, 1 == registered and on */
32static int nmi_enabled = 0;
33
Adrian Bunkc7c19f82006-09-26 10:52:27 +020034static int profile_exceptions_notify(struct notifier_block *self,
35 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Don Zickus2fbe7b22006-09-26 10:52:27 +020037 struct die_args *args = (struct die_args *)data;
38 int ret = NOTIFY_DONE;
39 int cpu = smp_processor_id();
40
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010041 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020042 case DIE_NMI:
Mike Galbraith5b75af02009-02-04 17:11:34 +010043 case DIE_NMI_IPI:
44 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
45 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020046 break;
47 default:
48 break;
49 }
50 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
Don Zickus2fbe7b22006-09-26 10:52:27 +020052
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010053static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010056 unsigned int const nr_ctrls = model->num_controls;
57 struct op_msr *counters = msrs->counters;
58 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int i;
60
61 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010062 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +020063 rdmsr(counters[i].addr,
64 counters[i].saved.low,
65 counters[i].saved.high);
66 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010070 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +020071 rdmsr(controls[i].addr,
72 controls[i].saved.low,
73 controls[i].saved.high);
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76}
77
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010078static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -070081 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 nmi_cpu_save_registers(msrs);
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void free_msrs(void)
86{
87 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -080088 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -070089 kfree(per_cpu(cpu_msrs, i).counters);
90 per_cpu(cpu_msrs, i).counters = NULL;
91 kfree(per_cpu(cpu_msrs, i).controls);
92 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int allocate_msrs(void)
97{
Robert Richter4c168ea2008-09-24 11:08:52 +020098 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
100 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
101
Robert Richter4c168ea2008-09-24 11:08:52 +0200102 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700103 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700104 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
105 GFP_KERNEL);
106 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 success = 0;
108 break;
109 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200110 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
111 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700112 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 success = 0;
114 break;
115 }
116 }
117
118 if (!success)
119 free_msrs();
120
121 return success;
122}
123
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100124static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700127 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_lock(&oprofilefs_lock);
129 model->setup_ctrs(msrs);
130 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700131 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 apic_write(APIC_LVTPC, APIC_DM_NMI);
133}
134
Don Zickus2fbe7b22006-09-26 10:52:27 +0200135static struct notifier_block profile_exceptions_nb = {
136 .notifier_call = profile_exceptions_notify,
137 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100138 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static int nmi_setup(void)
142{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100143 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200144 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!allocate_msrs())
147 return -ENOMEM;
148
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100149 err = register_die_notifier(&profile_exceptions_nb);
150 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200154
Robert Richter4c168ea2008-09-24 11:08:52 +0200155 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * of msrs are distinct for save and setup operations
157 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200158
159 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700160 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100161 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700162 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700163 memcpy(per_cpu(cpu_msrs, cpu).counters,
164 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700165 sizeof(struct op_msr) * model->num_counters);
166
Mike Travisd18d00f2008-03-25 15:06:59 -0700167 memcpy(per_cpu(cpu_msrs, cpu).controls,
168 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700169 sizeof(struct op_msr) * model->num_controls);
170 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200171
Andi Kleen6c977aa2007-05-21 14:31:45 +0200172 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200173 on_each_cpu(nmi_save_registers, NULL, 1);
174 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 nmi_enabled = 1;
176 return 0;
177}
178
Robert Richter4c168ea2008-09-24 11:08:52 +0200179static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100182 unsigned int const nr_ctrls = model->num_controls;
183 struct op_msr *counters = msrs->counters;
184 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned int i;
186
187 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100188 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200189 wrmsr(controls[i].addr,
190 controls[i].saved.low,
191 controls[i].saved.high);
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100196 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200197 wrmsr(counters[i].addr,
198 counters[i].saved.low,
199 counters[i].saved.high);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100204static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 unsigned int v;
207 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700208 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* restoring APIC_LVTPC can trigger an apic error because the delivery
211 * mode and vector nr combination can be illegal. That's by design: on
212 * power on apic lvt contain a zero vector nr which are legal only for
213 * NMI delivery mode. So inhibit apic err before restoring lvtpc
214 */
215 v = apic_read(APIC_LVTERR);
216 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700217 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 apic_write(APIC_LVTERR, v);
Robert Richter4c168ea2008-09-24 11:08:52 +0200219 nmi_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void nmi_shutdown(void)
223{
Andrea Righib61e06f2008-09-20 18:02:27 +0200224 struct op_msrs *msrs;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200227 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200228 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200229 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700230 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200232 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100235static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Mike Travisd18d00f2008-03-25 15:06:59 -0700237 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 model->start(msrs);
239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static int nmi_start(void)
242{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200243 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100246
247static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Mike Travisd18d00f2008-03-25 15:06:59 -0700249 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 model->stop(msrs);
251}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static void nmi_stop(void)
254{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200255 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258struct op_counter_config counter_config[OP_MAX_COUNTER];
259
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100260static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 unsigned int i;
263
264 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100265 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700266 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100267
268 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200269 * available for use. This should protect userspace app.
270 * NOTE: assumes 1:1 mapping here (that counters are organized
271 * sequentially in their struct assignment).
272 */
273 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
274 continue;
275
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700276 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100278 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
279 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
280 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
281 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
282 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
283 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 return 0;
287}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100288
Robert Richter69046d42008-09-05 12:17:40 +0200289#ifdef CONFIG_SMP
290static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
291 void *data)
292{
293 int cpu = (unsigned long)data;
294 switch (action) {
295 case CPU_DOWN_FAILED:
296 case CPU_ONLINE:
297 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
298 break;
299 case CPU_DOWN_PREPARE:
300 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
301 break;
302 }
303 return NOTIFY_DONE;
304}
305
306static struct notifier_block oprofile_cpu_nb = {
307 .notifier_call = oprofile_cpu_notifier
308};
309#endif
310
311#ifdef CONFIG_PM
312
313static int nmi_suspend(struct sys_device *dev, pm_message_t state)
314{
315 /* Only one CPU left, just stop that one */
316 if (nmi_enabled == 1)
317 nmi_cpu_stop(NULL);
318 return 0;
319}
320
321static int nmi_resume(struct sys_device *dev)
322{
323 if (nmi_enabled == 1)
324 nmi_cpu_start(NULL);
325 return 0;
326}
327
328static struct sysdev_class oprofile_sysclass = {
329 .name = "oprofile",
330 .resume = nmi_resume,
331 .suspend = nmi_suspend,
332};
333
334static struct sys_device device_oprofile = {
335 .id = 0,
336 .cls = &oprofile_sysclass,
337};
338
339static int __init init_sysfs(void)
340{
341 int error;
342
343 error = sysdev_class_register(&oprofile_sysclass);
344 if (!error)
345 error = sysdev_register(&device_oprofile);
346 return error;
347}
348
349static void exit_sysfs(void)
350{
351 sysdev_unregister(&device_oprofile);
352 sysdev_class_unregister(&oprofile_sysclass);
353}
354
355#else
356#define init_sysfs() do { } while (0)
357#define exit_sysfs() do { } while (0)
358#endif /* CONFIG_PM */
359
Andi Kleen1cfcea12006-07-10 17:06:21 +0200360static int p4force;
361module_param(p4force, int, 0);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100362
363static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 __u8 cpu_model = boot_cpu_data.x86_model;
366
Andi Kleen1cfcea12006-07-10 17:06:21 +0200367 if (!p4force && (cpu_model > 6 || cpu_model == 5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369
370#ifndef CONFIG_SMP
371 *cpu_type = "i386/p4";
372 model = &op_p4_spec;
373 return 1;
374#else
375 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100376 case 1:
377 *cpu_type = "i386/p4";
378 model = &op_p4_spec;
379 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100381 case 2:
382 *cpu_type = "i386/p4-ht";
383 model = &op_p4_ht2_spec;
384 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386#endif
387
388 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
389 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
390 return 0;
391}
392
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100393static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 __u8 cpu_model = boot_cpu_data.x86_model;
396
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700397 switch (cpu_model) {
398 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700400 break;
401 case 3 ... 5:
402 *cpu_type = "i386/pii";
403 break;
404 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500405 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700406 *cpu_type = "i386/piii";
407 break;
408 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500409 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700410 *cpu_type = "i386/p6_mobile";
411 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700412 case 14:
413 *cpu_type = "i386/core";
414 break;
415 case 15: case 23:
416 *cpu_type = "i386/core_2";
417 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700418 default:
419 /* Unknown */
420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 model = &op_ppro_spec;
424 return 1;
425}
426
Andi Kleenb9917022008-08-18 14:50:31 +0200427static int __init arch_perfmon_init(char **cpu_type)
428{
429 if (!cpu_has_arch_perfmon)
430 return 0;
431 *cpu_type = "i386/arch_perfmon";
432 model = &op_arch_perfmon_spec;
433 arch_perfmon_setup_counters();
434 return 1;
435}
436
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100437/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static int using_nmi;
439
David Gibson96d08212005-09-06 15:17:26 -0700440int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 __u8 vendor = boot_cpu_data.x86_vendor;
443 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200444 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200445 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (!cpu_has_apic)
448 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100451 case X86_VENDOR_AMD:
452 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100454 switch (family) {
455 default:
456 return -ENODEV;
457 case 6:
Robert Richter6657fe42008-07-22 21:08:50 +0200458 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100459 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100461 case 0xf:
Robert Richter6657fe42008-07-22 21:08:50 +0200462 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100463 /* Actually it could be i386/hammer too, but give
464 user space an consistent name. */
465 cpu_type = "x86-64/hammer";
466 break;
467 case 0x10:
Robert Richter6657fe42008-07-22 21:08:50 +0200468 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100469 cpu_type = "x86-64/family10";
470 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200471 case 0x11:
Robert Richter6657fe42008-07-22 21:08:50 +0200472 model = &op_amd_spec;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200473 cpu_type = "x86-64/family11h";
474 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100475 }
476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100478 case X86_VENDOR_INTEL:
479 switch (family) {
480 /* Pentium IV */
481 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200482 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100485 /* A P6-class processor */
486 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200487 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489
490 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200491 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100492 }
Andi Kleenb9917022008-08-18 14:50:31 +0200493
494 if (!cpu_type && !arch_perfmon_init(&cpu_type))
495 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100496 break;
497
498 default:
499 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200502#ifdef CONFIG_SMP
503 register_cpu_notifier(&oprofile_cpu_nb);
504#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200505 /* default values, can be overwritten by model */
506 ops->create_files = nmi_create_files;
507 ops->setup = nmi_setup;
508 ops->shutdown = nmi_shutdown;
509 ops->start = nmi_start;
510 ops->stop = nmi_stop;
511 ops->cpu_type = cpu_type;
512
Robert Richteradf5ec02008-07-22 21:08:48 +0200513 if (model->init)
514 ret = model->init(ops);
515 if (ret)
516 return ret;
517
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100518 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
521 return 0;
522}
523
David Gibson96d08212005-09-06 15:17:26 -0700524void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200526 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100527 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200528#ifdef CONFIG_SMP
529 unregister_cpu_notifier(&oprofile_cpu_nb);
530#endif
531 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200532 if (model->exit)
533 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}