blob: 0227694f7dab9b156350eaef0e90f79e8ec2ecc4 [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>
Andi Kleen80a8c9f2008-08-19 03:13:38 +020018#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/nmi.h>
20#include <asm/msr.h>
21#include <asm/apic.h>
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "op_counter.h"
24#include "op_x86_model.h"
Don Zickus2fbe7b22006-09-26 10:52:27 +020025
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010026static struct op_x86_model_spec const *model;
Mike Travisd18d00f2008-03-25 15:06:59 -070027static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
28static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
Don Zickus2fbe7b22006-09-26 10:52:27 +020029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int nmi_start(void);
31static void nmi_stop(void);
Andi Kleen80a8c9f2008-08-19 03:13:38 +020032static void nmi_cpu_start(void *dummy);
33static void nmi_cpu_stop(void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* 0 == registered but off, 1 == registered and on */
36static int nmi_enabled = 0;
37
Andi Kleen80a8c9f2008-08-19 03:13:38 +020038#ifdef CONFIG_SMP
39static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
40 void *data)
41{
42 int cpu = (unsigned long)data;
43 switch (action) {
44 case CPU_DOWN_FAILED:
45 case CPU_ONLINE:
46 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
47 break;
48 case CPU_DOWN_PREPARE:
49 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
50 break;
51 }
52 return NOTIFY_DONE;
53}
54
55static struct notifier_block oprofile_cpu_nb = {
56 .notifier_call = oprofile_cpu_notifier
57};
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_PM
61
Pavel Machek438510f2005-04-16 15:25:24 -070062static int nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Andi Kleen80a8c9f2008-08-19 03:13:38 +020064 /* Only one CPU left, just stop that one */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (nmi_enabled == 1)
Andi Kleen80a8c9f2008-08-19 03:13:38 +020066 nmi_cpu_stop(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return 0;
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int nmi_resume(struct sys_device *dev)
71{
72 if (nmi_enabled == 1)
Andi Kleen80a8c9f2008-08-19 03:13:38 +020073 nmi_cpu_start(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct sysdev_class oprofile_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010078 .name = "oprofile",
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 .resume = nmi_resume,
80 .suspend = nmi_suspend,
81};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct sys_device device_oprofile = {
84 .id = 0,
85 .cls = &oprofile_sysclass,
86};
87
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010088static int __init init_sysfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int error;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010091
92 error = sysdev_class_register(&oprofile_sysclass);
93 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 error = sysdev_register(&device_oprofile);
95 return error;
96}
97
Robert P. J. Day405ae7d2007-02-17 19:13:42 +010098static void exit_sysfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 sysdev_unregister(&device_oprofile);
101 sysdev_class_unregister(&oprofile_sysclass);
102}
103
104#else
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100105#define init_sysfs() do { } while (0)
106#define exit_sysfs() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif /* CONFIG_PM */
108
Adrian Bunkc7c19f82006-09-26 10:52:27 +0200109static int profile_exceptions_notify(struct notifier_block *self,
110 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200112 struct die_args *args = (struct die_args *)data;
113 int ret = NOTIFY_DONE;
114 int cpu = smp_processor_id();
115
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100116 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +0200117 case DIE_NMI:
Mike Travisd18d00f2008-03-25 15:06:59 -0700118 if (model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)))
Don Zickus2fbe7b22006-09-26 10:52:27 +0200119 ret = NOTIFY_STOP;
120 break;
121 default:
122 break;
123 }
124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
Don Zickus2fbe7b22006-09-26 10:52:27 +0200126
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100127static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100130 unsigned int const nr_ctrls = model->num_controls;
131 struct op_msr *counters = msrs->counters;
132 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned int i;
134
135 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100136 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200137 rdmsr(counters[i].addr,
138 counters[i].saved.low,
139 counters[i].saved.high);
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100144 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200145 rdmsr(controls[i].addr,
146 controls[i].saved.low,
147 controls[i].saved.high);
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150}
151
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100152static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700155 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 nmi_cpu_save_registers(msrs);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void free_msrs(void)
160{
161 int i;
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800162 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700163 kfree(per_cpu(cpu_msrs, i).counters);
164 per_cpu(cpu_msrs, i).counters = NULL;
165 kfree(per_cpu(cpu_msrs, i).controls);
166 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int allocate_msrs(void)
171{
172 int success = 1;
173 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
174 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
175
176 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700177 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700178 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
179 GFP_KERNEL);
180 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 success = 0;
182 break;
183 }
Mike Travisd18d00f2008-03-25 15:06:59 -0700184 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
185 GFP_KERNEL);
186 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 success = 0;
188 break;
189 }
190 }
191
192 if (!success)
193 free_msrs();
194
195 return success;
196}
197
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100198static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700201 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_lock(&oprofilefs_lock);
203 model->setup_ctrs(msrs);
204 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700205 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 apic_write(APIC_LVTPC, APIC_DM_NMI);
207}
208
Don Zickus2fbe7b22006-09-26 10:52:27 +0200209static struct notifier_block profile_exceptions_nb = {
210 .notifier_call = profile_exceptions_notify,
211 .next = NULL,
212 .priority = 0
213};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215static int nmi_setup(void)
216{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100217 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200218 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!allocate_msrs())
221 return -ENOMEM;
222
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100223 err = register_die_notifier(&profile_exceptions_nb);
224 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* We need to serialize save and setup for HT because the subset
230 * of msrs are distinct for save and setup operations
231 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200232
233 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700234 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100235 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700236 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700237 memcpy(per_cpu(cpu_msrs, cpu).counters,
238 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700239 sizeof(struct op_msr) * model->num_counters);
240
Mike Travisd18d00f2008-03-25 15:06:59 -0700241 memcpy(per_cpu(cpu_msrs, cpu).controls,
242 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700243 sizeof(struct op_msr) * model->num_controls);
244 }
245
Andi Kleen6c977aa2007-05-21 14:31:45 +0200246 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200247 on_each_cpu(nmi_save_registers, NULL, 1);
248 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 nmi_enabled = 1;
250 return 0;
251}
252
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100253static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100256 unsigned int const nr_ctrls = model->num_controls;
257 struct op_msr *counters = msrs->counters;
258 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned int i;
260
261 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100262 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200263 wrmsr(controls[i].addr,
264 controls[i].saved.low,
265 controls[i].saved.high);
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100270 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200271 wrmsr(counters[i].addr,
272 counters[i].saved.low,
273 counters[i].saved.high);
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100278static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned int v;
281 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700282 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* restoring APIC_LVTPC can trigger an apic error because the delivery
285 * mode and vector nr combination can be illegal. That's by design: on
286 * power on apic lvt contain a zero vector nr which are legal only for
287 * NMI delivery mode. So inhibit apic err before restoring lvtpc
288 */
289 v = apic_read(APIC_LVTERR);
290 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700291 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 apic_write(APIC_LVTERR, v);
293 nmi_restore_registers(msrs);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void nmi_shutdown(void)
297{
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200298 struct op_msrs *msrs = &get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200300 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200301 unregister_die_notifier(&profile_exceptions_nb);
Mike Travisd18d00f2008-03-25 15:06:59 -0700302 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200304 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100307static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Mike Travisd18d00f2008-03-25 15:06:59 -0700309 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 model->start(msrs);
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313static int nmi_start(void)
314{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200315 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100318
319static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Mike Travisd18d00f2008-03-25 15:06:59 -0700321 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 model->stop(msrs);
323}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static void nmi_stop(void)
326{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200327 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330struct op_counter_config counter_config[OP_MAX_COUNTER];
331
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100332static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 unsigned int i;
335
336 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100337 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700338 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100339
340 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200341 * available for use. This should protect userspace app.
342 * NOTE: assumes 1:1 mapping here (that counters are organized
343 * sequentially in their struct assignment).
344 */
345 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
346 continue;
347
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700348 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100350 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
351 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
352 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
353 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
354 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
355 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 return 0;
359}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100360
Andi Kleen1cfcea12006-07-10 17:06:21 +0200361static int p4force;
362module_param(p4force, int, 0);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100363
364static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 __u8 cpu_model = boot_cpu_data.x86_model;
367
Andi Kleen1cfcea12006-07-10 17:06:21 +0200368 if (!p4force && (cpu_model > 6 || cpu_model == 5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370
371#ifndef CONFIG_SMP
372 *cpu_type = "i386/p4";
373 model = &op_p4_spec;
374 return 1;
375#else
376 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100377 case 1:
378 *cpu_type = "i386/p4";
379 model = &op_p4_spec;
380 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100382 case 2:
383 *cpu_type = "i386/p4-ht";
384 model = &op_p4_ht2_spec;
385 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387#endif
388
389 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
390 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
391 return 0;
392}
393
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100394static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 __u8 cpu_model = boot_cpu_data.x86_model;
397
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700398 switch (cpu_model) {
399 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700401 break;
402 case 3 ... 5:
403 *cpu_type = "i386/pii";
404 break;
405 case 6 ... 8:
406 *cpu_type = "i386/piii";
407 break;
408 case 9:
409 *cpu_type = "i386/p6_mobile";
410 break;
411 case 10 ... 13:
412 *cpu_type = "i386/p6";
413 break;
414 case 14:
415 *cpu_type = "i386/core";
416 break;
417 case 15: case 23:
418 *cpu_type = "i386/core_2";
419 break;
420 case 26:
421 *cpu_type = "i386/core_2";
422 break;
423 default:
424 /* Unknown */
425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 model = &op_ppro_spec;
429 return 1;
430}
431
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100432/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int using_nmi;
434
David Gibson96d08212005-09-06 15:17:26 -0700435int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 __u8 vendor = boot_cpu_data.x86_vendor;
438 __u8 family = boot_cpu_data.x86;
439 char *cpu_type;
440
441 if (!cpu_has_apic)
442 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100445 case X86_VENDOR_AMD:
446 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100448 switch (family) {
449 default:
450 return -ENODEV;
451 case 6:
452 model = &op_athlon_spec;
453 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100455 case 0xf:
456 model = &op_athlon_spec;
457 /* Actually it could be i386/hammer too, but give
458 user space an consistent name. */
459 cpu_type = "x86-64/hammer";
460 break;
461 case 0x10:
462 model = &op_athlon_spec;
463 cpu_type = "x86-64/family10";
464 break;
465 }
466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100468 case X86_VENDOR_INTEL:
469 switch (family) {
470 /* Pentium IV */
471 case 0xf:
472 if (!p4_init(&cpu_type))
473 return -ENODEV;
474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100476 /* A P6-class processor */
477 case 6:
478 if (!ppro_init(&cpu_type))
479 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481
482 default:
483 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100484 }
485 break;
486
487 default:
488 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100491 init_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200492#ifdef CONFIG_SMP
493 register_cpu_notifier(&oprofile_cpu_nb);
494#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 using_nmi = 1;
496 ops->create_files = nmi_create_files;
497 ops->setup = nmi_setup;
498 ops->shutdown = nmi_shutdown;
499 ops->start = nmi_start;
500 ops->stop = nmi_stop;
501 ops->cpu_type = cpu_type;
502 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
503 return 0;
504}
505
David Gibson96d08212005-09-06 15:17:26 -0700506void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200508 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100509 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200510#ifdef CONFIG_SMP
511 unregister_cpu_notifier(&oprofile_cpu_nb);
512#endif
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}