blob: 388ee15e0e42ac9113c861fb88100c1a76895893 [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
Robert Richter3370d352009-05-25 15:10:32 +020034/* common functions */
35
36u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
37 struct op_counter_config *counter_config)
38{
39 u64 val = 0;
40 u16 event = (u16)counter_config->event;
41
42 val |= ARCH_PERFMON_EVENTSEL_INT;
43 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
44 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
45 val |= (counter_config->unit_mask & 0xFF) << 8;
46 event &= model->event_mask ? model->event_mask : 0xFF;
47 val |= event & 0xFF;
48 val |= (event & 0x0F00) << 24;
49
50 return val;
51}
52
53
Adrian Bunkc7c19f82006-09-26 10:52:27 +020054static int profile_exceptions_notify(struct notifier_block *self,
55 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Don Zickus2fbe7b22006-09-26 10:52:27 +020057 struct die_args *args = (struct die_args *)data;
58 int ret = NOTIFY_DONE;
59 int cpu = smp_processor_id();
60
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010061 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020062 case DIE_NMI:
Mike Travisd18d00f2008-03-25 15:06:59 -070063 if (model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)))
Don Zickus2fbe7b22006-09-26 10:52:27 +020064 ret = NOTIFY_STOP;
65 break;
66 default:
67 break;
68 }
69 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Don Zickus2fbe7b22006-09-26 10:52:27 +020071
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010072static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010075 unsigned int const nr_ctrls = model->num_controls;
76 struct op_msr *counters = msrs->counters;
77 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned int i;
79
80 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010081 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +020082 rdmsr(counters[i].addr,
83 counters[i].saved.low,
84 counters[i].saved.high);
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010089 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +020090 rdmsr(controls[i].addr,
91 controls[i].saved.low,
92 controls[i].saved.high);
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95}
96
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010097static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700100 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 nmi_cpu_save_registers(msrs);
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static void free_msrs(void)
105{
106 int i;
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800107 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700108 kfree(per_cpu(cpu_msrs, i).counters);
109 per_cpu(cpu_msrs, i).counters = NULL;
110 kfree(per_cpu(cpu_msrs, i).controls);
111 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int allocate_msrs(void)
116{
Robert Richter4c168ea2008-09-24 11:08:52 +0200117 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
119 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
120
Robert Richter4c168ea2008-09-24 11:08:52 +0200121 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700122 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700123 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
124 GFP_KERNEL);
125 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 success = 0;
127 break;
128 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200129 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
130 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700131 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 success = 0;
133 break;
134 }
135 }
136
137 if (!success)
138 free_msrs();
139
140 return success;
141}
142
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100143static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700146 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200148 model->setup_ctrs(model, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700150 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 apic_write(APIC_LVTPC, APIC_DM_NMI);
152}
153
Don Zickus2fbe7b22006-09-26 10:52:27 +0200154static struct notifier_block profile_exceptions_nb = {
155 .notifier_call = profile_exceptions_notify,
156 .next = NULL,
157 .priority = 0
158};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160static int nmi_setup(void)
161{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100162 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200163 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!allocate_msrs())
166 return -ENOMEM;
167
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100168 err = register_die_notifier(&profile_exceptions_nb);
169 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200173
Robert Richter4c168ea2008-09-24 11:08:52 +0200174 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * of msrs are distinct for save and setup operations
176 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200177
178 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700179 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100180 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700181 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700182 memcpy(per_cpu(cpu_msrs, cpu).counters,
183 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700184 sizeof(struct op_msr) * model->num_counters);
185
Mike Travisd18d00f2008-03-25 15:06:59 -0700186 memcpy(per_cpu(cpu_msrs, cpu).controls,
187 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700188 sizeof(struct op_msr) * model->num_controls);
189 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200190
Andi Kleen6c977aa2007-05-21 14:31:45 +0200191 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200192 on_each_cpu(nmi_save_registers, NULL, 1);
193 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 nmi_enabled = 1;
195 return 0;
196}
197
Robert Richter4c168ea2008-09-24 11:08:52 +0200198static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100201 unsigned int const nr_ctrls = model->num_controls;
202 struct op_msr *counters = msrs->counters;
203 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned int i;
205
206 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100207 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200208 wrmsr(controls[i].addr,
209 controls[i].saved.low,
210 controls[i].saved.high);
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100215 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200216 wrmsr(counters[i].addr,
217 counters[i].saved.low,
218 counters[i].saved.high);
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100223static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 unsigned int v;
226 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700227 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* restoring APIC_LVTPC can trigger an apic error because the delivery
230 * mode and vector nr combination can be illegal. That's by design: on
231 * power on apic lvt contain a zero vector nr which are legal only for
232 * NMI delivery mode. So inhibit apic err before restoring lvtpc
233 */
234 v = apic_read(APIC_LVTERR);
235 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700236 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 apic_write(APIC_LVTERR, v);
Robert Richter4c168ea2008-09-24 11:08:52 +0200238 nmi_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static void nmi_shutdown(void)
242{
Andrea Righib61e06f2008-09-20 18:02:27 +0200243 struct op_msrs *msrs;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200246 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200247 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200248 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700249 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200251 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100254static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Mike Travisd18d00f2008-03-25 15:06:59 -0700256 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 model->start(msrs);
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260static int nmi_start(void)
261{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200262 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100265
266static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Mike Travisd18d00f2008-03-25 15:06:59 -0700268 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 model->stop(msrs);
270}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void nmi_stop(void)
273{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200274 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277struct op_counter_config counter_config[OP_MAX_COUNTER];
278
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100279static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned int i;
282
283 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100284 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700285 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100286
287 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200288 * available for use. This should protect userspace app.
289 * NOTE: assumes 1:1 mapping here (that counters are organized
290 * sequentially in their struct assignment).
291 */
292 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
293 continue;
294
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700295 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100297 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
298 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
299 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
300 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
301 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
302 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 return 0;
306}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100307
Robert Richter69046d42008-09-05 12:17:40 +0200308#ifdef CONFIG_SMP
309static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
310 void *data)
311{
312 int cpu = (unsigned long)data;
313 switch (action) {
314 case CPU_DOWN_FAILED:
315 case CPU_ONLINE:
316 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
317 break;
318 case CPU_DOWN_PREPARE:
319 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
320 break;
321 }
322 return NOTIFY_DONE;
323}
324
325static struct notifier_block oprofile_cpu_nb = {
326 .notifier_call = oprofile_cpu_notifier
327};
328#endif
329
330#ifdef CONFIG_PM
331
332static int nmi_suspend(struct sys_device *dev, pm_message_t state)
333{
334 /* Only one CPU left, just stop that one */
335 if (nmi_enabled == 1)
336 nmi_cpu_stop(NULL);
337 return 0;
338}
339
340static int nmi_resume(struct sys_device *dev)
341{
342 if (nmi_enabled == 1)
343 nmi_cpu_start(NULL);
344 return 0;
345}
346
347static struct sysdev_class oprofile_sysclass = {
348 .name = "oprofile",
349 .resume = nmi_resume,
350 .suspend = nmi_suspend,
351};
352
353static struct sys_device device_oprofile = {
354 .id = 0,
355 .cls = &oprofile_sysclass,
356};
357
358static int __init init_sysfs(void)
359{
360 int error;
361
362 error = sysdev_class_register(&oprofile_sysclass);
363 if (!error)
364 error = sysdev_register(&device_oprofile);
365 return error;
366}
367
368static void exit_sysfs(void)
369{
370 sysdev_unregister(&device_oprofile);
371 sysdev_class_unregister(&oprofile_sysclass);
372}
373
374#else
375#define init_sysfs() do { } while (0)
376#define exit_sysfs() do { } while (0)
377#endif /* CONFIG_PM */
378
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100379static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 __u8 cpu_model = boot_cpu_data.x86_model;
382
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200383 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385
386#ifndef CONFIG_SMP
387 *cpu_type = "i386/p4";
388 model = &op_p4_spec;
389 return 1;
390#else
391 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100392 case 1:
393 *cpu_type = "i386/p4";
394 model = &op_p4_spec;
395 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100397 case 2:
398 *cpu_type = "i386/p4-ht";
399 model = &op_p4_ht2_spec;
400 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402#endif
403
404 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
405 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
406 return 0;
407}
408
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200409static int force_arch_perfmon;
410static int force_cpu_type(const char *str, struct kernel_param *kp)
411{
412 if (!strcmp(str, "archperfmon")) {
413 force_arch_perfmon = 1;
414 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
415 }
416
417 return 0;
418}
419module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200420
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100421static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 __u8 cpu_model = boot_cpu_data.x86_model;
424
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200425 if (force_arch_perfmon && cpu_has_arch_perfmon)
426 return 0;
427
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700428 switch (cpu_model) {
429 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700431 break;
432 case 3 ... 5:
433 *cpu_type = "i386/pii";
434 break;
435 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500436 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700437 *cpu_type = "i386/piii";
438 break;
439 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500440 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700441 *cpu_type = "i386/p6_mobile";
442 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700443 case 14:
444 *cpu_type = "i386/core";
445 break;
446 case 15: case 23:
447 *cpu_type = "i386/core_2";
448 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200449 case 26:
Robert Richtere4192942008-10-12 15:12:34 -0400450 model = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200451 *cpu_type = "i386/core_i7";
452 break;
453 case 28:
454 *cpu_type = "i386/atom";
455 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700456 default:
457 /* Unknown */
458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 model = &op_ppro_spec;
462 return 1;
463}
464
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100465/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static int using_nmi;
467
David Gibson96d08212005-09-06 15:17:26 -0700468int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 __u8 vendor = boot_cpu_data.x86_vendor;
471 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200472 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200473 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (!cpu_has_apic)
476 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100479 case X86_VENDOR_AMD:
480 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100482 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100483 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100484 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100486 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100487 /*
488 * Actually it could be i386/hammer too, but
489 * give user space an consistent name.
490 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100491 cpu_type = "x86-64/hammer";
492 break;
493 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100494 cpu_type = "x86-64/family10";
495 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200496 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200497 cpu_type = "x86-64/family11h";
498 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100499 default:
500 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100501 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100502 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100505 case X86_VENDOR_INTEL:
506 switch (family) {
507 /* Pentium IV */
508 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200509 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100512 /* A P6-class processor */
513 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200514 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516
517 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200518 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100519 }
Andi Kleenb9917022008-08-18 14:50:31 +0200520
Robert Richtere4192942008-10-12 15:12:34 -0400521 if (cpu_type)
522 break;
523
524 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200525 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400526
527 /* use arch perfmon as fallback */
528 cpu_type = "i386/arch_perfmon";
529 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100530 break;
531
532 default:
533 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200536#ifdef CONFIG_SMP
537 register_cpu_notifier(&oprofile_cpu_nb);
538#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200539 /* default values, can be overwritten by model */
540 ops->create_files = nmi_create_files;
541 ops->setup = nmi_setup;
542 ops->shutdown = nmi_shutdown;
543 ops->start = nmi_start;
544 ops->stop = nmi_stop;
545 ops->cpu_type = cpu_type;
546
Robert Richteradf5ec02008-07-22 21:08:48 +0200547 if (model->init)
548 ret = model->init(ops);
549 if (ret)
550 return ret;
551
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100552 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
555 return 0;
556}
557
David Gibson96d08212005-09-06 15:17:26 -0700558void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200560 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100561 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200562#ifdef CONFIG_SMP
563 unregister_cpu_notifier(&oprofile_cpu_nb);
564#endif
565 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200566 if (model->exit)
567 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}