blob: 8a5f1614a3d57cde8d7495c1b71f8e8542e99c64 [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 Hiroyukic89125992006-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{
Andrea Righib61e06f2008-09-20 18:02:27 +0200298 struct op_msrs *msrs;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200301 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200302 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200303 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700304 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200306 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100309static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Mike Travisd18d00f2008-03-25 15:06:59 -0700311 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 model->start(msrs);
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315static int nmi_start(void)
316{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200317 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100320
321static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Mike Travisd18d00f2008-03-25 15:06:59 -0700323 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 model->stop(msrs);
325}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static void nmi_stop(void)
328{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200329 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332struct op_counter_config counter_config[OP_MAX_COUNTER];
333
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100334static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 unsigned int i;
337
338 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100339 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700340 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100341
342 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200343 * available for use. This should protect userspace app.
344 * NOTE: assumes 1:1 mapping here (that counters are organized
345 * sequentially in their struct assignment).
346 */
347 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
348 continue;
349
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700350 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100352 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
353 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
354 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
355 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
356 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
357 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 return 0;
361}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100362
Andi Kleen1cfcea12006-07-10 17:06:21 +0200363static int p4force;
364module_param(p4force, int, 0);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100365
366static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 __u8 cpu_model = boot_cpu_data.x86_model;
369
Andi Kleen1cfcea12006-07-10 17:06:21 +0200370 if (!p4force && (cpu_model > 6 || cpu_model == 5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372
373#ifndef CONFIG_SMP
374 *cpu_type = "i386/p4";
375 model = &op_p4_spec;
376 return 1;
377#else
378 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100379 case 1:
380 *cpu_type = "i386/p4";
381 model = &op_p4_spec;
382 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100384 case 2:
385 *cpu_type = "i386/p4-ht";
386 model = &op_p4_ht2_spec;
387 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389#endif
390
391 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
392 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
393 return 0;
394}
395
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100396static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 __u8 cpu_model = boot_cpu_data.x86_model;
399
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700400 switch (cpu_model) {
401 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700403 break;
404 case 3 ... 5:
405 *cpu_type = "i386/pii";
406 break;
407 case 6 ... 8:
408 *cpu_type = "i386/piii";
409 break;
410 case 9:
411 *cpu_type = "i386/p6_mobile";
412 break;
413 case 10 ... 13:
414 *cpu_type = "i386/p6";
415 break;
416 case 14:
417 *cpu_type = "i386/core";
418 break;
419 case 15: case 23:
420 *cpu_type = "i386/core_2";
421 break;
422 case 26:
423 *cpu_type = "i386/core_2";
424 break;
425 default:
426 /* Unknown */
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 model = &op_ppro_spec;
431 return 1;
432}
433
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100434/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static int using_nmi;
436
David Gibson96d08212005-09-06 15:17:26 -0700437int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 __u8 vendor = boot_cpu_data.x86_vendor;
440 __u8 family = boot_cpu_data.x86;
441 char *cpu_type;
442
443 if (!cpu_has_apic)
444 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100447 case X86_VENDOR_AMD:
448 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100450 switch (family) {
451 default:
452 return -ENODEV;
453 case 6:
454 model = &op_athlon_spec;
455 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100457 case 0xf:
458 model = &op_athlon_spec;
459 /* Actually it could be i386/hammer too, but give
460 user space an consistent name. */
461 cpu_type = "x86-64/hammer";
462 break;
463 case 0x10:
464 model = &op_athlon_spec;
465 cpu_type = "x86-64/family10";
466 break;
467 }
468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100470 case X86_VENDOR_INTEL:
471 switch (family) {
472 /* Pentium IV */
473 case 0xf:
474 if (!p4_init(&cpu_type))
475 return -ENODEV;
476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100478 /* A P6-class processor */
479 case 6:
480 if (!ppro_init(&cpu_type))
481 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483
484 default:
485 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100486 }
487 break;
488
489 default:
490 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100493 init_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200494#ifdef CONFIG_SMP
495 register_cpu_notifier(&oprofile_cpu_nb);
496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 using_nmi = 1;
498 ops->create_files = nmi_create_files;
499 ops->setup = nmi_setup;
500 ops->shutdown = nmi_shutdown;
501 ops->start = nmi_start;
502 ops->stop = nmi_stop;
503 ops->cpu_type = cpu_type;
504 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
505 return 0;
506}
507
David Gibson96d08212005-09-06 15:17:26 -0700508void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200510 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100511 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200512#ifdef CONFIG_SMP
513 unregister_cpu_notifier(&oprofile_cpu_nb);
514#endif
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}