blob: 28ee490c1b808ebdb83d7ac0de87d97422674d5c [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 Galbraith5b75af02009-02-04 17:11:34 +010063 case DIE_NMI_IPI:
64 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
65 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020066 break;
67 default:
68 break;
69 }
70 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Don Zickus2fbe7b22006-09-26 10:52:27 +020072
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010073static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010075 struct op_msr *counters = msrs->counters;
76 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int i;
78
Robert Richter1a245c42009-06-05 15:54:24 +020079 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020080 if (counters[i].addr)
81 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010083
Robert Richter1a245c42009-06-05 15:54:24 +020084 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020085 if (controls[i].addr)
86 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88}
89
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010090static void nmi_save_registers(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -070093 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 nmi_cpu_save_registers(msrs);
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void free_msrs(void)
98{
99 int i;
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800100 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700101 kfree(per_cpu(cpu_msrs, i).counters);
102 per_cpu(cpu_msrs, i).counters = NULL;
103 kfree(per_cpu(cpu_msrs, i).controls);
104 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int allocate_msrs(void)
109{
Robert Richter4c168ea2008-09-24 11:08:52 +0200110 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
112 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
113
Robert Richter4c168ea2008-09-24 11:08:52 +0200114 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700115 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700116 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
117 GFP_KERNEL);
118 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 success = 0;
120 break;
121 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200122 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
123 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700124 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 success = 0;
126 break;
127 }
128 }
129
130 if (!success)
131 free_msrs();
132
133 return success;
134}
135
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100136static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700139 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200141 model->setup_ctrs(model, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700143 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 apic_write(APIC_LVTPC, APIC_DM_NMI);
145}
146
Don Zickus2fbe7b22006-09-26 10:52:27 +0200147static struct notifier_block profile_exceptions_nb = {
148 .notifier_call = profile_exceptions_notify,
149 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100150 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200151};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153static int nmi_setup(void)
154{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100155 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200156 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!allocate_msrs())
159 return -ENOMEM;
160
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100161 err = register_die_notifier(&profile_exceptions_nb);
162 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200166
Robert Richter4c168ea2008-09-24 11:08:52 +0200167 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * of msrs are distinct for save and setup operations
169 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200170
171 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700172 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100173 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700174 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700175 memcpy(per_cpu(cpu_msrs, cpu).counters,
176 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700177 sizeof(struct op_msr) * model->num_counters);
178
Mike Travisd18d00f2008-03-25 15:06:59 -0700179 memcpy(per_cpu(cpu_msrs, cpu).controls,
180 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700181 sizeof(struct op_msr) * model->num_controls);
182 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200183
Andi Kleen6c977aa2007-05-21 14:31:45 +0200184 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200185 on_each_cpu(nmi_save_registers, NULL, 1);
186 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 nmi_enabled = 1;
188 return 0;
189}
190
Robert Richter4c168ea2008-09-24 11:08:52 +0200191static void nmi_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100193 struct op_msr *counters = msrs->counters;
194 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int i;
196
Robert Richter1a245c42009-06-05 15:54:24 +0200197 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200198 if (controls[i].addr)
199 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100201
Robert Richter1a245c42009-06-05 15:54:24 +0200202 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200203 if (counters[i].addr)
204 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100208static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned int v;
211 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700212 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* restoring APIC_LVTPC can trigger an apic error because the delivery
215 * mode and vector nr combination can be illegal. That's by design: on
216 * power on apic lvt contain a zero vector nr which are legal only for
217 * NMI delivery mode. So inhibit apic err before restoring lvtpc
218 */
219 v = apic_read(APIC_LVTERR);
220 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700221 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 apic_write(APIC_LVTERR, v);
Robert Richter4c168ea2008-09-24 11:08:52 +0200223 nmi_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void nmi_shutdown(void)
227{
Andrea Righib61e06f2008-09-20 18:02:27 +0200228 struct op_msrs *msrs;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200231 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200232 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200233 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700234 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200236 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100239static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Mike Travisd18d00f2008-03-25 15:06:59 -0700241 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 model->start(msrs);
243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245static int nmi_start(void)
246{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200247 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100250
251static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Mike Travisd18d00f2008-03-25 15:06:59 -0700253 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 model->stop(msrs);
255}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static void nmi_stop(void)
258{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200259 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262struct op_counter_config counter_config[OP_MAX_COUNTER];
263
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100264static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 unsigned int i;
267
268 for (i = 0; i < model->num_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100269 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700270 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100271
272 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200273 * available for use. This should protect userspace app.
274 * NOTE: assumes 1:1 mapping here (that counters are organized
275 * sequentially in their struct assignment).
276 */
277 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
278 continue;
279
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700280 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100282 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
283 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
284 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
285 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
286 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
287 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 return 0;
291}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100292
Robert Richter69046d42008-09-05 12:17:40 +0200293#ifdef CONFIG_SMP
294static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
295 void *data)
296{
297 int cpu = (unsigned long)data;
298 switch (action) {
299 case CPU_DOWN_FAILED:
300 case CPU_ONLINE:
301 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
302 break;
303 case CPU_DOWN_PREPARE:
304 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
305 break;
306 }
307 return NOTIFY_DONE;
308}
309
310static struct notifier_block oprofile_cpu_nb = {
311 .notifier_call = oprofile_cpu_notifier
312};
313#endif
314
315#ifdef CONFIG_PM
316
317static int nmi_suspend(struct sys_device *dev, pm_message_t state)
318{
319 /* Only one CPU left, just stop that one */
320 if (nmi_enabled == 1)
321 nmi_cpu_stop(NULL);
322 return 0;
323}
324
325static int nmi_resume(struct sys_device *dev)
326{
327 if (nmi_enabled == 1)
328 nmi_cpu_start(NULL);
329 return 0;
330}
331
332static struct sysdev_class oprofile_sysclass = {
333 .name = "oprofile",
334 .resume = nmi_resume,
335 .suspend = nmi_suspend,
336};
337
338static struct sys_device device_oprofile = {
339 .id = 0,
340 .cls = &oprofile_sysclass,
341};
342
343static int __init init_sysfs(void)
344{
345 int error;
346
347 error = sysdev_class_register(&oprofile_sysclass);
348 if (!error)
349 error = sysdev_register(&device_oprofile);
350 return error;
351}
352
353static void exit_sysfs(void)
354{
355 sysdev_unregister(&device_oprofile);
356 sysdev_class_unregister(&oprofile_sysclass);
357}
358
359#else
360#define init_sysfs() do { } while (0)
361#define exit_sysfs() do { } while (0)
362#endif /* CONFIG_PM */
363
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100364static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 __u8 cpu_model = boot_cpu_data.x86_model;
367
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200368 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370
371#ifndef CONFIG_SMP
372 *cpu_type = "i386/p4";
373 model = &op_p4_spec;
374 return 1;
375#else
376 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100377 case 1:
378 *cpu_type = "i386/p4";
379 model = &op_p4_spec;
380 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100382 case 2:
383 *cpu_type = "i386/p4-ht";
384 model = &op_p4_ht2_spec;
385 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387#endif
388
389 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
390 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
391 return 0;
392}
393
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200394static int force_arch_perfmon;
395static int force_cpu_type(const char *str, struct kernel_param *kp)
396{
397 if (!strcmp(str, "archperfmon")) {
398 force_arch_perfmon = 1;
399 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
400 }
401
402 return 0;
403}
404module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200405
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100406static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter802070f2009-06-12 18:32:07 +0200409 struct op_x86_model_spec const *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200411 if (force_arch_perfmon && cpu_has_arch_perfmon)
412 return 0;
413
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700414 switch (cpu_model) {
415 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700417 break;
418 case 3 ... 5:
419 *cpu_type = "i386/pii";
420 break;
421 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500422 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700423 *cpu_type = "i386/piii";
424 break;
425 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500426 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700427 *cpu_type = "i386/p6_mobile";
428 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700429 case 14:
430 *cpu_type = "i386/core";
431 break;
432 case 15: case 23:
433 *cpu_type = "i386/core_2";
434 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200435 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200436 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200437 *cpu_type = "i386/core_i7";
438 break;
439 case 28:
440 *cpu_type = "i386/atom";
441 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700442 default:
443 /* Unknown */
444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Robert Richter802070f2009-06-12 18:32:07 +0200447 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 1;
449}
450
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100451/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static int using_nmi;
453
David Gibson96d08212005-09-06 15:17:26 -0700454int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 __u8 vendor = boot_cpu_data.x86_vendor;
457 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200458 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200459 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (!cpu_has_apic)
462 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100465 case X86_VENDOR_AMD:
466 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100468 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100469 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100470 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100472 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100473 /*
474 * Actually it could be i386/hammer too, but
475 * give user space an consistent name.
476 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100477 cpu_type = "x86-64/hammer";
478 break;
479 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100480 cpu_type = "x86-64/family10";
481 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200482 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200483 cpu_type = "x86-64/family11h";
484 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100485 default:
486 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100487 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100488 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100491 case X86_VENDOR_INTEL:
492 switch (family) {
493 /* Pentium IV */
494 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200495 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100498 /* A P6-class processor */
499 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200500 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502
503 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200504 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100505 }
Andi Kleenb9917022008-08-18 14:50:31 +0200506
Robert Richtere4192942008-10-12 15:12:34 -0400507 if (cpu_type)
508 break;
509
510 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200511 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400512
513 /* use arch perfmon as fallback */
514 cpu_type = "i386/arch_perfmon";
515 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100516 break;
517
518 default:
519 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200522#ifdef CONFIG_SMP
523 register_cpu_notifier(&oprofile_cpu_nb);
524#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200525 /* default values, can be overwritten by model */
526 ops->create_files = nmi_create_files;
527 ops->setup = nmi_setup;
528 ops->shutdown = nmi_shutdown;
529 ops->start = nmi_start;
530 ops->stop = nmi_stop;
531 ops->cpu_type = cpu_type;
532
Robert Richteradf5ec02008-07-22 21:08:48 +0200533 if (model->init)
534 ret = model->init(ops);
535 if (ret)
536 return ret;
537
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100538 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
541 return 0;
542}
543
David Gibson96d08212005-09-06 15:17:26 -0700544void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200546 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100547 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200548#ifdef CONFIG_SMP
549 unregister_cpu_notifier(&oprofile_cpu_nb);
550#endif
551 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200552 if (model->exit)
553 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}