blob: 3b84b789de0bb2e102b9a199c1ab7e917a387191 [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) {
Robert Richter95e74e62009-06-03 19:09:27 +020081 if (counters[i].addr)
82 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 for (i = 0; i < nr_ctrls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020086 if (controls[i].addr)
87 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89}
90
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010091static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -070094 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 nmi_cpu_save_registers(msrs);
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void free_msrs(void)
99{
100 int i;
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800101 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700102 kfree(per_cpu(cpu_msrs, i).counters);
103 per_cpu(cpu_msrs, i).counters = NULL;
104 kfree(per_cpu(cpu_msrs, i).controls);
105 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int allocate_msrs(void)
110{
Robert Richter4c168ea2008-09-24 11:08:52 +0200111 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
113 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
114
Robert Richter4c168ea2008-09-24 11:08:52 +0200115 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700116 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700117 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
118 GFP_KERNEL);
119 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 success = 0;
121 break;
122 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200123 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
124 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700125 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 success = 0;
127 break;
128 }
129 }
130
131 if (!success)
132 free_msrs();
133
134 return success;
135}
136
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100137static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700140 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200142 model->setup_ctrs(model, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700144 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 apic_write(APIC_LVTPC, APIC_DM_NMI);
146}
147
Don Zickus2fbe7b22006-09-26 10:52:27 +0200148static struct notifier_block profile_exceptions_nb = {
149 .notifier_call = profile_exceptions_notify,
150 .next = NULL,
151 .priority = 0
152};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154static int nmi_setup(void)
155{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100156 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200157 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!allocate_msrs())
160 return -ENOMEM;
161
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100162 err = register_die_notifier(&profile_exceptions_nb);
163 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200167
Robert Richter4c168ea2008-09-24 11:08:52 +0200168 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * of msrs are distinct for save and setup operations
170 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200171
172 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700173 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100174 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700175 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700176 memcpy(per_cpu(cpu_msrs, cpu).counters,
177 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700178 sizeof(struct op_msr) * model->num_counters);
179
Mike Travisd18d00f2008-03-25 15:06:59 -0700180 memcpy(per_cpu(cpu_msrs, cpu).controls,
181 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700182 sizeof(struct op_msr) * model->num_controls);
183 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200184
Andi Kleen6c977aa2007-05-21 14:31:45 +0200185 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200186 on_each_cpu(nmi_save_registers, NULL, 1);
187 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 nmi_enabled = 1;
189 return 0;
190}
191
Robert Richter4c168ea2008-09-24 11:08:52 +0200192static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 unsigned int const nr_ctrs = model->num_counters;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100195 unsigned int const nr_ctrls = model->num_controls;
196 struct op_msr *counters = msrs->counters;
197 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned int i;
199
200 for (i = 0; i < nr_ctrls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200201 if (controls[i].addr)
202 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for (i = 0; i < nr_ctrs; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200206 if (counters[i].addr)
207 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100211static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned int v;
214 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700215 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* restoring APIC_LVTPC can trigger an apic error because the delivery
218 * mode and vector nr combination can be illegal. That's by design: on
219 * power on apic lvt contain a zero vector nr which are legal only for
220 * NMI delivery mode. So inhibit apic err before restoring lvtpc
221 */
222 v = apic_read(APIC_LVTERR);
223 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700224 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 apic_write(APIC_LVTERR, v);
Robert Richter4c168ea2008-09-24 11:08:52 +0200226 nmi_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void nmi_shutdown(void)
230{
Andrea Righib61e06f2008-09-20 18:02:27 +0200231 struct op_msrs *msrs;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200234 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200235 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200236 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700237 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200239 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100242static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Mike Travisd18d00f2008-03-25 15:06:59 -0700244 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 model->start(msrs);
246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static int nmi_start(void)
249{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200250 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100253
254static void nmi_cpu_stop(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->stop(msrs);
258}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static void nmi_stop(void)
261{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200262 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265struct op_counter_config counter_config[OP_MAX_COUNTER];
266
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100267static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 unsigned int i;
270
271 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100272 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700273 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100274
275 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200276 * available for use. This should protect userspace app.
277 * NOTE: assumes 1:1 mapping here (that counters are organized
278 * sequentially in their struct assignment).
279 */
280 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
281 continue;
282
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700283 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100285 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
286 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
287 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
288 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
289 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
290 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 return 0;
294}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100295
Robert Richter69046d42008-09-05 12:17:40 +0200296#ifdef CONFIG_SMP
297static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
298 void *data)
299{
300 int cpu = (unsigned long)data;
301 switch (action) {
302 case CPU_DOWN_FAILED:
303 case CPU_ONLINE:
304 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
305 break;
306 case CPU_DOWN_PREPARE:
307 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
308 break;
309 }
310 return NOTIFY_DONE;
311}
312
313static struct notifier_block oprofile_cpu_nb = {
314 .notifier_call = oprofile_cpu_notifier
315};
316#endif
317
318#ifdef CONFIG_PM
319
320static int nmi_suspend(struct sys_device *dev, pm_message_t state)
321{
322 /* Only one CPU left, just stop that one */
323 if (nmi_enabled == 1)
324 nmi_cpu_stop(NULL);
325 return 0;
326}
327
328static int nmi_resume(struct sys_device *dev)
329{
330 if (nmi_enabled == 1)
331 nmi_cpu_start(NULL);
332 return 0;
333}
334
335static struct sysdev_class oprofile_sysclass = {
336 .name = "oprofile",
337 .resume = nmi_resume,
338 .suspend = nmi_suspend,
339};
340
341static struct sys_device device_oprofile = {
342 .id = 0,
343 .cls = &oprofile_sysclass,
344};
345
346static int __init init_sysfs(void)
347{
348 int error;
349
350 error = sysdev_class_register(&oprofile_sysclass);
351 if (!error)
352 error = sysdev_register(&device_oprofile);
353 return error;
354}
355
356static void exit_sysfs(void)
357{
358 sysdev_unregister(&device_oprofile);
359 sysdev_class_unregister(&oprofile_sysclass);
360}
361
362#else
363#define init_sysfs() do { } while (0)
364#define exit_sysfs() do { } while (0)
365#endif /* CONFIG_PM */
366
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100367static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 __u8 cpu_model = boot_cpu_data.x86_model;
370
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200371 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373
374#ifndef CONFIG_SMP
375 *cpu_type = "i386/p4";
376 model = &op_p4_spec;
377 return 1;
378#else
379 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100380 case 1:
381 *cpu_type = "i386/p4";
382 model = &op_p4_spec;
383 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100385 case 2:
386 *cpu_type = "i386/p4-ht";
387 model = &op_p4_ht2_spec;
388 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390#endif
391
392 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
393 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
394 return 0;
395}
396
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200397static int force_arch_perfmon;
398static int force_cpu_type(const char *str, struct kernel_param *kp)
399{
400 if (!strcmp(str, "archperfmon")) {
401 force_arch_perfmon = 1;
402 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
403 }
404
405 return 0;
406}
407module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200408
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100409static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 __u8 cpu_model = boot_cpu_data.x86_model;
412
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200413 if (force_arch_perfmon && cpu_has_arch_perfmon)
414 return 0;
415
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700416 switch (cpu_model) {
417 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700419 break;
420 case 3 ... 5:
421 *cpu_type = "i386/pii";
422 break;
423 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500424 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700425 *cpu_type = "i386/piii";
426 break;
427 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500428 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700429 *cpu_type = "i386/p6_mobile";
430 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700431 case 14:
432 *cpu_type = "i386/core";
433 break;
434 case 15: case 23:
435 *cpu_type = "i386/core_2";
436 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200437 case 26:
Robert Richtere4192942008-10-12 15:12:34 -0400438 model = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200439 *cpu_type = "i386/core_i7";
440 break;
441 case 28:
442 *cpu_type = "i386/atom";
443 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700444 default:
445 /* Unknown */
446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 model = &op_ppro_spec;
450 return 1;
451}
452
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100453/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int using_nmi;
455
David Gibson96d08212005-09-06 15:17:26 -0700456int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 __u8 vendor = boot_cpu_data.x86_vendor;
459 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200460 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200461 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (!cpu_has_apic)
464 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100467 case X86_VENDOR_AMD:
468 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100470 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100471 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100472 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100474 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100475 /*
476 * Actually it could be i386/hammer too, but
477 * give user space an consistent name.
478 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100479 cpu_type = "x86-64/hammer";
480 break;
481 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100482 cpu_type = "x86-64/family10";
483 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200484 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200485 cpu_type = "x86-64/family11h";
486 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100487 default:
488 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100489 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100490 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100493 case X86_VENDOR_INTEL:
494 switch (family) {
495 /* Pentium IV */
496 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200497 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100500 /* A P6-class processor */
501 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200502 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504
505 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200506 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100507 }
Andi Kleenb9917022008-08-18 14:50:31 +0200508
Robert Richtere4192942008-10-12 15:12:34 -0400509 if (cpu_type)
510 break;
511
512 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200513 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400514
515 /* use arch perfmon as fallback */
516 cpu_type = "i386/arch_perfmon";
517 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100518 break;
519
520 default:
521 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200524#ifdef CONFIG_SMP
525 register_cpu_notifier(&oprofile_cpu_nb);
526#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200527 /* default values, can be overwritten by model */
528 ops->create_files = nmi_create_files;
529 ops->setup = nmi_setup;
530 ops->shutdown = nmi_shutdown;
531 ops->start = nmi_start;
532 ops->stop = nmi_stop;
533 ops->cpu_type = cpu_type;
534
Robert Richteradf5ec02008-07-22 21:08:48 +0200535 if (model->init)
536 ret = model->init(ops);
537 if (ret)
538 return ret;
539
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100540 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
543 return 0;
544}
545
David Gibson96d08212005-09-06 15:17:26 -0700546void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200548 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100549 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200550#ifdef CONFIG_SMP
551 unregister_cpu_notifier(&oprofile_cpu_nb);
552#endif
553 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200554 if (model->exit)
555 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}