blob: 89b9a5cd63da1d52900a47dd0b269476dd09fb2c [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
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100360static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 __u8 cpu_model = boot_cpu_data.x86_model;
363
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200364 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366
367#ifndef CONFIG_SMP
368 *cpu_type = "i386/p4";
369 model = &op_p4_spec;
370 return 1;
371#else
372 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100373 case 1:
374 *cpu_type = "i386/p4";
375 model = &op_p4_spec;
376 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100378 case 2:
379 *cpu_type = "i386/p4-ht";
380 model = &op_p4_ht2_spec;
381 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383#endif
384
385 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
386 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
387 return 0;
388}
389
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200390static int force_arch_perfmon;
391static int force_cpu_type(const char *str, struct kernel_param *kp)
392{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200393 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200394 force_arch_perfmon = 1;
395 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
396 }
397
398 return 0;
399}
400module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200401
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100402static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 __u8 cpu_model = boot_cpu_data.x86_model;
405
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200406 if (force_arch_perfmon && cpu_has_arch_perfmon)
407 return 0;
408
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700409 switch (cpu_model) {
410 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700412 break;
413 case 3 ... 5:
414 *cpu_type = "i386/pii";
415 break;
416 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500417 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700418 *cpu_type = "i386/piii";
419 break;
420 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500421 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700422 *cpu_type = "i386/p6_mobile";
423 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700424 case 14:
425 *cpu_type = "i386/core";
426 break;
427 case 15: case 23:
428 *cpu_type = "i386/core_2";
429 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200430 case 26:
431 arch_perfmon_setup_counters();
432 *cpu_type = "i386/core_i7";
433 break;
434 case 28:
435 *cpu_type = "i386/atom";
436 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700437 default:
438 /* Unknown */
439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 model = &op_ppro_spec;
443 return 1;
444}
445
Andi Kleenb9917022008-08-18 14:50:31 +0200446static int __init arch_perfmon_init(char **cpu_type)
447{
448 if (!cpu_has_arch_perfmon)
449 return 0;
450 *cpu_type = "i386/arch_perfmon";
451 model = &op_arch_perfmon_spec;
452 arch_perfmon_setup_counters();
453 return 1;
454}
455
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100456/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static int using_nmi;
458
David Gibson96d08212005-09-06 15:17:26 -0700459int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 __u8 vendor = boot_cpu_data.x86_vendor;
462 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200463 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200464 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (!cpu_has_apic)
467 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100470 case X86_VENDOR_AMD:
471 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100473 switch (family) {
474 default:
475 return -ENODEV;
476 case 6:
Robert Richter6657fe42008-07-22 21:08:50 +0200477 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100478 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100480 case 0xf:
Robert Richter6657fe42008-07-22 21:08:50 +0200481 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100482 /* Actually it could be i386/hammer too, but give
483 user space an consistent name. */
484 cpu_type = "x86-64/hammer";
485 break;
486 case 0x10:
Robert Richter6657fe42008-07-22 21:08:50 +0200487 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100488 cpu_type = "x86-64/family10";
489 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200490 case 0x11:
Robert Richter6657fe42008-07-22 21:08:50 +0200491 model = &op_amd_spec;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200492 cpu_type = "x86-64/family11h";
493 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100494 }
495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100497 case X86_VENDOR_INTEL:
498 switch (family) {
499 /* Pentium IV */
500 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200501 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100504 /* A P6-class processor */
505 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200506 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508
509 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200510 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100511 }
Andi Kleenb9917022008-08-18 14:50:31 +0200512
513 if (!cpu_type && !arch_perfmon_init(&cpu_type))
514 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100515 break;
516
517 default:
518 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200521#ifdef CONFIG_SMP
522 register_cpu_notifier(&oprofile_cpu_nb);
523#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200524 /* default values, can be overwritten by model */
525 ops->create_files = nmi_create_files;
526 ops->setup = nmi_setup;
527 ops->shutdown = nmi_shutdown;
528 ops->start = nmi_start;
529 ops->stop = nmi_stop;
530 ops->cpu_type = cpu_type;
531
Robert Richteradf5ec02008-07-22 21:08:48 +0200532 if (model->init)
533 ret = model->init(ops);
534 if (ret)
535 return ret;
536
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100537 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
540 return 0;
541}
542
David Gibson96d08212005-09-06 15:17:26 -0700543void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200545 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100546 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200547#ifdef CONFIG_SMP
548 unregister_cpu_notifier(&oprofile_cpu_nb);
549#endif
550 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200551 if (model->exit)
552 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}