blob: 80b63d5db50918b840677f96e952eeed63f7b8c2 [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{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010074 struct op_msr *counters = msrs->counters;
75 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 unsigned int i;
77
Robert Richter1a245c42009-06-05 15:54:24 +020078 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020079 if (counters[i].addr)
80 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010082
Robert Richter1a245c42009-06-05 15:54:24 +020083 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020084 if (controls[i].addr)
85 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87}
88
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010089static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -070092 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 nmi_cpu_save_registers(msrs);
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static void free_msrs(void)
97{
98 int i;
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -080099 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700100 kfree(per_cpu(cpu_msrs, i).counters);
101 per_cpu(cpu_msrs, i).counters = NULL;
102 kfree(per_cpu(cpu_msrs, i).controls);
103 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int allocate_msrs(void)
108{
Robert Richter4c168ea2008-09-24 11:08:52 +0200109 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
111 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
112
Robert Richter4c168ea2008-09-24 11:08:52 +0200113 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700114 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700115 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
116 GFP_KERNEL);
117 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 success = 0;
119 break;
120 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200121 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
122 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700123 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 success = 0;
125 break;
126 }
127 }
128
129 if (!success)
130 free_msrs();
131
132 return success;
133}
134
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100135static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700138 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200140 model->setup_ctrs(model, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700142 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 apic_write(APIC_LVTPC, APIC_DM_NMI);
144}
145
Don Zickus2fbe7b22006-09-26 10:52:27 +0200146static struct notifier_block profile_exceptions_nb = {
147 .notifier_call = profile_exceptions_notify,
148 .next = NULL,
149 .priority = 0
150};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static int nmi_setup(void)
153{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100154 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200155 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!allocate_msrs())
158 return -ENOMEM;
159
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100160 err = register_die_notifier(&profile_exceptions_nb);
161 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200165
Robert Richter4c168ea2008-09-24 11:08:52 +0200166 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * of msrs are distinct for save and setup operations
168 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200169
170 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700171 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100172 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700173 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700174 memcpy(per_cpu(cpu_msrs, cpu).counters,
175 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700176 sizeof(struct op_msr) * model->num_counters);
177
Mike Travisd18d00f2008-03-25 15:06:59 -0700178 memcpy(per_cpu(cpu_msrs, cpu).controls,
179 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700180 sizeof(struct op_msr) * model->num_controls);
181 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200182
Andi Kleen6c977aa2007-05-21 14:31:45 +0200183 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200184 on_each_cpu(nmi_save_registers, NULL, 1);
185 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 nmi_enabled = 1;
187 return 0;
188}
189
Robert Richter4c168ea2008-09-24 11:08:52 +0200190static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100192 struct op_msr *counters = msrs->counters;
193 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned int i;
195
Robert Richter1a245c42009-06-05 15:54:24 +0200196 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200197 if (controls[i].addr)
198 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100200
Robert Richter1a245c42009-06-05 15:54:24 +0200201 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200202 if (counters[i].addr)
203 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100207static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned int v;
210 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700211 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* restoring APIC_LVTPC can trigger an apic error because the delivery
214 * mode and vector nr combination can be illegal. That's by design: on
215 * power on apic lvt contain a zero vector nr which are legal only for
216 * NMI delivery mode. So inhibit apic err before restoring lvtpc
217 */
218 v = apic_read(APIC_LVTERR);
219 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700220 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 apic_write(APIC_LVTERR, v);
Robert Richter4c168ea2008-09-24 11:08:52 +0200222 nmi_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void nmi_shutdown(void)
226{
Andrea Righib61e06f2008-09-20 18:02:27 +0200227 struct op_msrs *msrs;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200230 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200231 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200232 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700233 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200235 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100238static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Mike Travisd18d00f2008-03-25 15:06:59 -0700240 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 model->start(msrs);
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static int nmi_start(void)
245{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200246 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100249
250static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Mike Travisd18d00f2008-03-25 15:06:59 -0700252 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 model->stop(msrs);
254}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static void nmi_stop(void)
257{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200258 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261struct op_counter_config counter_config[OP_MAX_COUNTER];
262
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100263static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 unsigned int i;
266
267 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100268 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700269 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100270
271 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200272 * available for use. This should protect userspace app.
273 * NOTE: assumes 1:1 mapping here (that counters are organized
274 * sequentially in their struct assignment).
275 */
276 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
277 continue;
278
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700279 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100281 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
282 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
283 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
284 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
285 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
286 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289 return 0;
290}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100291
Robert Richter69046d42008-09-05 12:17:40 +0200292#ifdef CONFIG_SMP
293static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
294 void *data)
295{
296 int cpu = (unsigned long)data;
297 switch (action) {
298 case CPU_DOWN_FAILED:
299 case CPU_ONLINE:
300 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
301 break;
302 case CPU_DOWN_PREPARE:
303 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
304 break;
305 }
306 return NOTIFY_DONE;
307}
308
309static struct notifier_block oprofile_cpu_nb = {
310 .notifier_call = oprofile_cpu_notifier
311};
312#endif
313
314#ifdef CONFIG_PM
315
316static int nmi_suspend(struct sys_device *dev, pm_message_t state)
317{
318 /* Only one CPU left, just stop that one */
319 if (nmi_enabled == 1)
320 nmi_cpu_stop(NULL);
321 return 0;
322}
323
324static int nmi_resume(struct sys_device *dev)
325{
326 if (nmi_enabled == 1)
327 nmi_cpu_start(NULL);
328 return 0;
329}
330
331static struct sysdev_class oprofile_sysclass = {
332 .name = "oprofile",
333 .resume = nmi_resume,
334 .suspend = nmi_suspend,
335};
336
337static struct sys_device device_oprofile = {
338 .id = 0,
339 .cls = &oprofile_sysclass,
340};
341
342static int __init init_sysfs(void)
343{
344 int error;
345
346 error = sysdev_class_register(&oprofile_sysclass);
347 if (!error)
348 error = sysdev_register(&device_oprofile);
349 return error;
350}
351
352static void exit_sysfs(void)
353{
354 sysdev_unregister(&device_oprofile);
355 sysdev_class_unregister(&oprofile_sysclass);
356}
357
358#else
359#define init_sysfs() do { } while (0)
360#define exit_sysfs() do { } while (0)
361#endif /* CONFIG_PM */
362
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100363static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 __u8 cpu_model = boot_cpu_data.x86_model;
366
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200367 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369
370#ifndef CONFIG_SMP
371 *cpu_type = "i386/p4";
372 model = &op_p4_spec;
373 return 1;
374#else
375 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100376 case 1:
377 *cpu_type = "i386/p4";
378 model = &op_p4_spec;
379 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100381 case 2:
382 *cpu_type = "i386/p4-ht";
383 model = &op_p4_ht2_spec;
384 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386#endif
387
388 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
389 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
390 return 0;
391}
392
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200393static int force_arch_perfmon;
394static int force_cpu_type(const char *str, struct kernel_param *kp)
395{
396 if (!strcmp(str, "archperfmon")) {
397 force_arch_perfmon = 1;
398 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
399 }
400
401 return 0;
402}
403module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200404
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100405static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 __u8 cpu_model = boot_cpu_data.x86_model;
408
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200409 if (force_arch_perfmon && cpu_has_arch_perfmon)
410 return 0;
411
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700412 switch (cpu_model) {
413 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700415 break;
416 case 3 ... 5:
417 *cpu_type = "i386/pii";
418 break;
419 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500420 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700421 *cpu_type = "i386/piii";
422 break;
423 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500424 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700425 *cpu_type = "i386/p6_mobile";
426 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700427 case 14:
428 *cpu_type = "i386/core";
429 break;
430 case 15: case 23:
431 *cpu_type = "i386/core_2";
432 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200433 case 26:
Robert Richtere4192942008-10-12 15:12:34 -0400434 model = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200435 *cpu_type = "i386/core_i7";
436 break;
437 case 28:
438 *cpu_type = "i386/atom";
439 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700440 default:
441 /* Unknown */
442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 model = &op_ppro_spec;
446 return 1;
447}
448
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100449/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static int using_nmi;
451
David Gibson96d08212005-09-06 15:17:26 -0700452int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 __u8 vendor = boot_cpu_data.x86_vendor;
455 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200456 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200457 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (!cpu_has_apic)
460 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100463 case X86_VENDOR_AMD:
464 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100466 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100467 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100468 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100470 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100471 /*
472 * Actually it could be i386/hammer too, but
473 * give user space an consistent name.
474 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100475 cpu_type = "x86-64/hammer";
476 break;
477 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100478 cpu_type = "x86-64/family10";
479 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200480 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200481 cpu_type = "x86-64/family11h";
482 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100483 default:
484 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100485 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100486 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100489 case X86_VENDOR_INTEL:
490 switch (family) {
491 /* Pentium IV */
492 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200493 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100496 /* A P6-class processor */
497 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200498 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
500
501 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200502 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100503 }
Andi Kleenb9917022008-08-18 14:50:31 +0200504
Robert Richtere4192942008-10-12 15:12:34 -0400505 if (cpu_type)
506 break;
507
508 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200509 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400510
511 /* use arch perfmon as fallback */
512 cpu_type = "i386/arch_perfmon";
513 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100514 break;
515
516 default:
517 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200520#ifdef CONFIG_SMP
521 register_cpu_notifier(&oprofile_cpu_nb);
522#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200523 /* default values, can be overwritten by model */
524 ops->create_files = nmi_create_files;
525 ops->setup = nmi_setup;
526 ops->shutdown = nmi_shutdown;
527 ops->start = nmi_start;
528 ops->stop = nmi_stop;
529 ops->cpu_type = cpu_type;
530
Robert Richteradf5ec02008-07-22 21:08:48 +0200531 if (model->init)
532 ret = model->init(ops);
533 if (ret)
534 return ret;
535
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100536 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
539 return 0;
540}
541
David Gibson96d08212005-09-06 15:17:26 -0700542void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200544 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100545 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200546#ifdef CONFIG_SMP
547 unregister_cpu_notifier(&oprofile_cpu_nb);
548#endif
549 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200550 if (model->exit)
551 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}