blob: f472c0c48a3ef91835ac14ec9fe483e4660b6ad0 [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 Travisd18d00f2008-03-25 15:06:59 -070043 if (model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)))
Don Zickus2fbe7b22006-09-26 10:52:27 +020044 ret = NOTIFY_STOP;
45 break;
46 default:
47 break;
48 }
49 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
Don Zickus2fbe7b22006-09-26 10:52:27 +020051
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010052static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010055 unsigned int const nr_ctrls = model->num_controls;
56 struct op_msr *counters = msrs->counters;
57 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned int i;
59
60 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010061 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +020062 rdmsr(counters[i].addr,
63 counters[i].saved.low,
64 counters[i].saved.high);
65 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010069 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +020070 rdmsr(controls[i].addr,
71 controls[i].saved.low,
72 controls[i].saved.high);
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75}
76
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010077static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -070080 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 nmi_cpu_save_registers(msrs);
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static void free_msrs(void)
85{
86 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -080087 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -070088 kfree(per_cpu(cpu_msrs, i).counters);
89 per_cpu(cpu_msrs, i).counters = NULL;
90 kfree(per_cpu(cpu_msrs, i).controls);
91 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int allocate_msrs(void)
96{
Robert Richter4c168ea2008-09-24 11:08:52 +020097 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
99 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
100
Robert Richter4c168ea2008-09-24 11:08:52 +0200101 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700102 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700103 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
104 GFP_KERNEL);
105 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 success = 0;
107 break;
108 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200109 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
110 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700111 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 success = 0;
113 break;
114 }
115 }
116
117 if (!success)
118 free_msrs();
119
120 return success;
121}
122
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100123static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700126 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 spin_lock(&oprofilefs_lock);
128 model->setup_ctrs(msrs);
129 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700130 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 apic_write(APIC_LVTPC, APIC_DM_NMI);
132}
133
Don Zickus2fbe7b22006-09-26 10:52:27 +0200134static struct notifier_block profile_exceptions_nb = {
135 .notifier_call = profile_exceptions_notify,
136 .next = NULL,
137 .priority = 0
138};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static int nmi_setup(void)
141{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100142 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200143 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!allocate_msrs())
146 return -ENOMEM;
147
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100148 err = register_die_notifier(&profile_exceptions_nb);
149 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200151 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200153
Robert Richter4c168ea2008-09-24 11:08:52 +0200154 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * of msrs are distinct for save and setup operations
156 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200157
158 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700159 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100160 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700161 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700162 memcpy(per_cpu(cpu_msrs, cpu).counters,
163 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700164 sizeof(struct op_msr) * model->num_counters);
165
Mike Travisd18d00f2008-03-25 15:06:59 -0700166 memcpy(per_cpu(cpu_msrs, cpu).controls,
167 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700168 sizeof(struct op_msr) * model->num_controls);
169 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200170
Andi Kleen6c977aa2007-05-21 14:31:45 +0200171 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200172 on_each_cpu(nmi_save_registers, NULL, 1);
173 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 nmi_enabled = 1;
175 return 0;
176}
177
Robert Richter4c168ea2008-09-24 11:08:52 +0200178static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100181 unsigned int const nr_ctrls = model->num_controls;
182 struct op_msr *counters = msrs->counters;
183 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned int i;
185
186 for (i = 0; i < nr_ctrls; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100187 if (controls[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200188 wrmsr(controls[i].addr,
189 controls[i].saved.low,
190 controls[i].saved.high);
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 for (i = 0; i < nr_ctrs; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100195 if (counters[i].addr) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200196 wrmsr(counters[i].addr,
197 counters[i].saved.low,
198 counters[i].saved.high);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100203static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned int v;
206 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700207 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* restoring APIC_LVTPC can trigger an apic error because the delivery
210 * mode and vector nr combination can be illegal. That's by design: on
211 * power on apic lvt contain a zero vector nr which are legal only for
212 * NMI delivery mode. So inhibit apic err before restoring lvtpc
213 */
214 v = apic_read(APIC_LVTERR);
215 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700216 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 apic_write(APIC_LVTERR, v);
Robert Richter4c168ea2008-09-24 11:08:52 +0200218 nmi_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static void nmi_shutdown(void)
222{
Andrea Righib61e06f2008-09-20 18:02:27 +0200223 struct op_msrs *msrs;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200226 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200227 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200228 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700229 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200231 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100234static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Mike Travisd18d00f2008-03-25 15:06:59 -0700236 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 model->start(msrs);
238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240static int nmi_start(void)
241{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200242 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100245
246static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Mike Travisd18d00f2008-03-25 15:06:59 -0700248 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 model->stop(msrs);
250}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static void nmi_stop(void)
253{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200254 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257struct op_counter_config counter_config[OP_MAX_COUNTER];
258
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100259static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 unsigned int i;
262
263 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100264 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700265 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100266
267 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200268 * available for use. This should protect userspace app.
269 * NOTE: assumes 1:1 mapping here (that counters are organized
270 * sequentially in their struct assignment).
271 */
272 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
273 continue;
274
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700275 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100277 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
278 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
279 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
280 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
281 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
282 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 return 0;
286}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100287
Robert Richter69046d42008-09-05 12:17:40 +0200288#ifdef CONFIG_SMP
289static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
290 void *data)
291{
292 int cpu = (unsigned long)data;
293 switch (action) {
294 case CPU_DOWN_FAILED:
295 case CPU_ONLINE:
296 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
297 break;
298 case CPU_DOWN_PREPARE:
299 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
300 break;
301 }
302 return NOTIFY_DONE;
303}
304
305static struct notifier_block oprofile_cpu_nb = {
306 .notifier_call = oprofile_cpu_notifier
307};
308#endif
309
310#ifdef CONFIG_PM
311
312static int nmi_suspend(struct sys_device *dev, pm_message_t state)
313{
314 /* Only one CPU left, just stop that one */
315 if (nmi_enabled == 1)
316 nmi_cpu_stop(NULL);
317 return 0;
318}
319
320static int nmi_resume(struct sys_device *dev)
321{
322 if (nmi_enabled == 1)
323 nmi_cpu_start(NULL);
324 return 0;
325}
326
327static struct sysdev_class oprofile_sysclass = {
328 .name = "oprofile",
329 .resume = nmi_resume,
330 .suspend = nmi_suspend,
331};
332
333static struct sys_device device_oprofile = {
334 .id = 0,
335 .cls = &oprofile_sysclass,
336};
337
338static int __init init_sysfs(void)
339{
340 int error;
341
342 error = sysdev_class_register(&oprofile_sysclass);
343 if (!error)
344 error = sysdev_register(&device_oprofile);
345 return error;
346}
347
348static void exit_sysfs(void)
349{
350 sysdev_unregister(&device_oprofile);
351 sysdev_class_unregister(&oprofile_sysclass);
352}
353
354#else
355#define init_sysfs() do { } while (0)
356#define exit_sysfs() do { } while (0)
357#endif /* CONFIG_PM */
358
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100359static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 __u8 cpu_model = boot_cpu_data.x86_model;
362
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200363 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365
366#ifndef CONFIG_SMP
367 *cpu_type = "i386/p4";
368 model = &op_p4_spec;
369 return 1;
370#else
371 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100372 case 1:
373 *cpu_type = "i386/p4";
374 model = &op_p4_spec;
375 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100377 case 2:
378 *cpu_type = "i386/p4-ht";
379 model = &op_p4_ht2_spec;
380 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382#endif
383
384 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
385 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
386 return 0;
387}
388
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200389int force_arch_perfmon;
390module_param(force_arch_perfmon, int, 0);
391
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100392static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 __u8 cpu_model = boot_cpu_data.x86_model;
395
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200396 if (force_arch_perfmon && cpu_has_arch_perfmon)
397 return 0;
398
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700399 switch (cpu_model) {
400 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700402 break;
403 case 3 ... 5:
404 *cpu_type = "i386/pii";
405 break;
406 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500407 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700408 *cpu_type = "i386/piii";
409 break;
410 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500411 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700412 *cpu_type = "i386/p6_mobile";
413 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700414 case 14:
415 *cpu_type = "i386/core";
416 break;
417 case 15: case 23:
418 *cpu_type = "i386/core_2";
419 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700420 default:
421 /* Unknown */
422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 model = &op_ppro_spec;
426 return 1;
427}
428
Andi Kleenb9917022008-08-18 14:50:31 +0200429static int __init arch_perfmon_init(char **cpu_type)
430{
431 if (!cpu_has_arch_perfmon)
432 return 0;
433 *cpu_type = "i386/arch_perfmon";
434 model = &op_arch_perfmon_spec;
435 arch_perfmon_setup_counters();
436 return 1;
437}
438
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100439/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static int using_nmi;
441
David Gibson96d08212005-09-06 15:17:26 -0700442int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 __u8 vendor = boot_cpu_data.x86_vendor;
445 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200446 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200447 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (!cpu_has_apic)
450 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100453 case X86_VENDOR_AMD:
454 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100456 switch (family) {
457 default:
458 return -ENODEV;
459 case 6:
Robert Richter6657fe42008-07-22 21:08:50 +0200460 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100461 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100463 case 0xf:
Robert Richter6657fe42008-07-22 21:08:50 +0200464 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100465 /* Actually it could be i386/hammer too, but give
466 user space an consistent name. */
467 cpu_type = "x86-64/hammer";
468 break;
469 case 0x10:
Robert Richter6657fe42008-07-22 21:08:50 +0200470 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100471 cpu_type = "x86-64/family10";
472 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200473 case 0x11:
Robert Richter6657fe42008-07-22 21:08:50 +0200474 model = &op_amd_spec;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200475 cpu_type = "x86-64/family11h";
476 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100477 }
478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100480 case X86_VENDOR_INTEL:
481 switch (family) {
482 /* Pentium IV */
483 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200484 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100487 /* A P6-class processor */
488 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200489 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491
492 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200493 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100494 }
Andi Kleenb9917022008-08-18 14:50:31 +0200495
496 if (!cpu_type && !arch_perfmon_init(&cpu_type))
497 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100498 break;
499
500 default:
501 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200504#ifdef CONFIG_SMP
505 register_cpu_notifier(&oprofile_cpu_nb);
506#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200507 /* default values, can be overwritten by model */
508 ops->create_files = nmi_create_files;
509 ops->setup = nmi_setup;
510 ops->shutdown = nmi_shutdown;
511 ops->start = nmi_start;
512 ops->stop = nmi_stop;
513 ops->cpu_type = cpu_type;
514
Robert Richteradf5ec02008-07-22 21:08:48 +0200515 if (model->init)
516 ret = model->init(ops);
517 if (ret)
518 return ret;
519
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100520 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
523 return 0;
524}
525
David Gibson96d08212005-09-06 15:17:26 -0700526void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200528 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100529 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200530#ifdef CONFIG_SMP
531 unregister_cpu_notifier(&oprofile_cpu_nb);
532#endif
533 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200534 if (model->exit)
535 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}