blob: e54f6a0b35ace7c0ff44747bbd3f7a58bdebe669 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file nmi_int.c
3 *
Jason Yeh4d4036e2009-07-08 13:49:38 +02004 * @remark Copyright 2002-2009 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>
Jason Yeh4d4036e2009-07-08 13:49:38 +02009 * @author Barry Kasindorf <barry.kasindorf@amd.com>
10 * @author Jason Yeh <jason.yeh@amd.com>
11 * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/init.h>
15#include <linux/notifier.h>
16#include <linux/smp.h>
17#include <linux/oprofile.h>
18#include <linux/sysdev.h>
19#include <linux/slab.h>
Andi Kleen1cfcea12006-07-10 17:06:21 +020020#include <linux/moduleparam.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070021#include <linux/kdebug.h>
Andi Kleen80a8c9f2008-08-19 03:13:38 +020022#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/nmi.h>
24#include <asm/msr.h>
25#include <asm/apic.h>
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "op_counter.h"
28#include "op_x86_model.h"
Don Zickus2fbe7b22006-09-26 10:52:27 +020029
Jason Yeh4d4036e2009-07-08 13:49:38 +020030
31#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
32DEFINE_PER_CPU(int, switch_index);
33#endif
34
35
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010036static struct op_x86_model_spec const *model;
Mike Travisd18d00f2008-03-25 15:06:59 -070037static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
38static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
Don Zickus2fbe7b22006-09-26 10:52:27 +020039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* 0 == registered but off, 1 == registered and on */
41static int nmi_enabled = 0;
42
Jason Yeh4d4036e2009-07-08 13:49:38 +020043
44#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
45extern atomic_t multiplex_counter;
46#endif
47
48struct op_counter_config counter_config[OP_MAX_COUNTER];
49
Robert Richter3370d352009-05-25 15:10:32 +020050/* common functions */
51
52u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
53 struct op_counter_config *counter_config)
54{
55 u64 val = 0;
56 u16 event = (u16)counter_config->event;
57
58 val |= ARCH_PERFMON_EVENTSEL_INT;
59 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
60 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
61 val |= (counter_config->unit_mask & 0xFF) << 8;
62 event &= model->event_mask ? model->event_mask : 0xFF;
63 val |= event & 0xFF;
64 val |= (event & 0x0F00) << 24;
65
66 return val;
67}
68
69
Adrian Bunkc7c19f82006-09-26 10:52:27 +020070static int profile_exceptions_notify(struct notifier_block *self,
71 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Don Zickus2fbe7b22006-09-26 10:52:27 +020073 struct die_args *args = (struct die_args *)data;
74 int ret = NOTIFY_DONE;
75 int cpu = smp_processor_id();
76
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010077 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020078 case DIE_NMI:
Mike Galbraith5b75af02009-02-04 17:11:34 +010079 case DIE_NMI_IPI:
80 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
81 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020082 break;
83 default:
84 break;
85 }
86 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Don Zickus2fbe7b22006-09-26 10:52:27 +020088
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010089static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010091 struct op_msr *counters = msrs->counters;
92 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int i;
94
Robert Richter1a245c42009-06-05 15:54:24 +020095 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020096 if (counters[i].addr)
97 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010099
Robert Richter1a245c42009-06-05 15:54:24 +0200100 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200101 if (controls[i].addr)
102 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static void free_msrs(void)
107{
108 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800109 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700110 kfree(per_cpu(cpu_msrs, i).counters);
111 per_cpu(cpu_msrs, i).counters = NULL;
112 kfree(per_cpu(cpu_msrs, i).controls);
113 per_cpu(cpu_msrs, i).controls = NULL;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200114
115#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
116 kfree(per_cpu(cpu_msrs, i).multiplex);
117 per_cpu(cpu_msrs, i).multiplex = NULL;
118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int allocate_msrs(void)
123{
Robert Richter4c168ea2008-09-24 11:08:52 +0200124 int success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
126 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200127#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
128 size_t multiplex_size = sizeof(struct op_msr) * model->num_virt_counters;
129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Robert Richter4c168ea2008-09-24 11:08:52 +0200131 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700132 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700133 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
134 GFP_KERNEL);
135 if (!per_cpu(cpu_msrs, i).counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 success = 0;
137 break;
138 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200139 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
140 GFP_KERNEL);
Mike Travisd18d00f2008-03-25 15:06:59 -0700141 if (!per_cpu(cpu_msrs, i).controls) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 success = 0;
143 break;
144 }
Jason Yeh4d4036e2009-07-08 13:49:38 +0200145#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
146 per_cpu(cpu_msrs, i).multiplex =
147 kmalloc(multiplex_size, GFP_KERNEL);
148 if (!per_cpu(cpu_msrs, i).multiplex) {
149 success = 0;
150 break;
151 }
152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 if (!success)
156 free_msrs();
157
158 return success;
159}
160
Jason Yeh4d4036e2009-07-08 13:49:38 +0200161#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
162
163static void nmi_setup_cpu_mux(struct op_msrs const * const msrs)
164{
165 int i;
166 struct op_msr *multiplex = msrs->multiplex;
167
168 for (i = 0; i < model->num_virt_counters; ++i) {
169 if (counter_config[i].enabled) {
170 multiplex[i].saved = -(u64)counter_config[i].count;
171 } else {
172 multiplex[i].addr = 0;
173 multiplex[i].saved = 0;
174 }
175 }
176}
177
178#endif
179
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100180static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700183 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200184 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200186 model->setup_ctrs(model, msrs);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200187#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
188 nmi_setup_cpu_mux(msrs);
189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700191 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 apic_write(APIC_LVTPC, APIC_DM_NMI);
193}
194
Don Zickus2fbe7b22006-09-26 10:52:27 +0200195static struct notifier_block profile_exceptions_nb = {
196 .notifier_call = profile_exceptions_notify,
197 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100198 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200199};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201static int nmi_setup(void)
202{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100203 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200204 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!allocate_msrs())
207 return -ENOMEM;
208
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100209 err = register_die_notifier(&profile_exceptions_nb);
210 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 free_msrs();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200214
Robert Richter4c168ea2008-09-24 11:08:52 +0200215 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * of msrs are distinct for save and setup operations
217 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200218
219 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700220 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100221 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700222 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700223 memcpy(per_cpu(cpu_msrs, cpu).counters,
224 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700225 sizeof(struct op_msr) * model->num_counters);
226
Mike Travisd18d00f2008-03-25 15:06:59 -0700227 memcpy(per_cpu(cpu_msrs, cpu).controls,
228 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700229 sizeof(struct op_msr) * model->num_controls);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200230#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
231 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
232 per_cpu(cpu_msrs, 0).multiplex,
233 sizeof(struct op_msr) * model->num_virt_counters);
234#endif
Chris Wright0939c172007-06-01 00:46:39 -0700235 }
Andi Kleen6c977aa2007-05-21 14:31:45 +0200236 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200237 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 nmi_enabled = 1;
239 return 0;
240}
241
Jason Yeh4d4036e2009-07-08 13:49:38 +0200242#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
243
244static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
245{
246 unsigned int si = __get_cpu_var(switch_index);
247 struct op_msr *multiplex = msrs->multiplex;
248 unsigned int i;
249
250 for (i = 0; i < model->num_counters; ++i) {
251 int offset = i + si;
252 if (multiplex[offset].addr) {
253 rdmsrl(multiplex[offset].addr,
254 multiplex[offset].saved);
255 }
256 }
257}
258
259static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
260{
261 unsigned int si = __get_cpu_var(switch_index);
262 struct op_msr *multiplex = msrs->multiplex;
263 unsigned int i;
264
265 for (i = 0; i < model->num_counters; ++i) {
266 int offset = i + si;
267 if (multiplex[offset].addr) {
268 wrmsrl(multiplex[offset].addr,
269 multiplex[offset].saved);
270 }
271 }
272}
273
274#endif
275
Robert Richter44ab9a62009-07-09 18:33:02 +0200276static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100278 struct op_msr *counters = msrs->counters;
279 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned int i;
281
Robert Richter1a245c42009-06-05 15:54:24 +0200282 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200283 if (controls[i].addr)
284 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100286
Robert Richter1a245c42009-06-05 15:54:24 +0200287 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200288 if (counters[i].addr)
289 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100293static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 unsigned int v;
296 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700297 struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* restoring APIC_LVTPC can trigger an apic error because the delivery
300 * mode and vector nr combination can be illegal. That's by design: on
301 * power on apic lvt contain a zero vector nr which are legal only for
302 * NMI delivery mode. So inhibit apic err before restoring lvtpc
303 */
304 v = apic_read(APIC_LVTERR);
305 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700306 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200308 nmi_cpu_restore_registers(msrs);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200309#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
310 __get_cpu_var(switch_index) = 0;
311#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static void nmi_shutdown(void)
315{
Andrea Righib61e06f2008-09-20 18:02:27 +0200316 struct op_msrs *msrs;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200319 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200320 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200321 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700322 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200324 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100327static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Mike Travisd18d00f2008-03-25 15:06:59 -0700329 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 model->start(msrs);
331}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333static int nmi_start(void)
334{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200335 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100338
339static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Mike Travisd18d00f2008-03-25 15:06:59 -0700341 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 model->stop(msrs);
343}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static void nmi_stop(void)
346{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200347 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100350static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned int i;
353
Jason Yeh4d4036e2009-07-08 13:49:38 +0200354 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100355 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700356 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100357
Jason Yeh4d4036e2009-07-08 13:49:38 +0200358#ifndef CONFIG_OPROFILE_EVENT_MULTIPLEX
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100359 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200360 * available for use. This should protect userspace app.
361 * NOTE: assumes 1:1 mapping here (that counters are organized
362 * sequentially in their struct assignment).
363 */
364 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
365 continue;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200366#endif /* CONFIG_OPROFILE_EVENT_MULTIPLEX */
Don Zickuscb9c4482006-09-26 10:52:26 +0200367
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700368 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100370 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
371 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
372 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
373 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
374 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
375 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 return 0;
379}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100380
Jason Yeh4d4036e2009-07-08 13:49:38 +0200381#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
382
383static void nmi_cpu_switch(void *dummy)
384{
385 int cpu = smp_processor_id();
386 int si = per_cpu(switch_index, cpu);
387 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
388
389 nmi_cpu_stop(NULL);
390 nmi_cpu_save_mpx_registers(msrs);
391
392 /* move to next set */
393 si += model->num_counters;
394 if ((si > model->num_virt_counters) || (counter_config[si].count == 0))
395 per_cpu(switch_index, cpu) = 0;
396 else
397 per_cpu(switch_index, cpu) = si;
398
399 model->switch_ctrl(model, msrs);
400 nmi_cpu_restore_mpx_registers(msrs);
401
402 nmi_cpu_start(NULL);
403}
404
405
406/*
407 * Quick check to see if multiplexing is necessary.
408 * The check should be sufficient since counters are used
409 * in ordre.
410 */
411static int nmi_multiplex_on(void)
412{
413 return counter_config[model->num_counters].count ? 0 : -EINVAL;
414}
415
416static int nmi_switch_event(void)
417{
418 if (!model->switch_ctrl)
419 return -ENOSYS; /* not implemented */
420 if (nmi_multiplex_on() < 0)
421 return -EINVAL; /* not necessary */
422
423 on_each_cpu(nmi_cpu_switch, NULL, 1);
424
425 atomic_inc(&multiplex_counter);
426
427 return 0;
428}
429
430#endif
431
Robert Richter69046d42008-09-05 12:17:40 +0200432#ifdef CONFIG_SMP
433static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
434 void *data)
435{
436 int cpu = (unsigned long)data;
437 switch (action) {
438 case CPU_DOWN_FAILED:
439 case CPU_ONLINE:
440 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
441 break;
442 case CPU_DOWN_PREPARE:
443 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
444 break;
445 }
446 return NOTIFY_DONE;
447}
448
449static struct notifier_block oprofile_cpu_nb = {
450 .notifier_call = oprofile_cpu_notifier
451};
452#endif
453
454#ifdef CONFIG_PM
455
456static int nmi_suspend(struct sys_device *dev, pm_message_t state)
457{
458 /* Only one CPU left, just stop that one */
459 if (nmi_enabled == 1)
460 nmi_cpu_stop(NULL);
461 return 0;
462}
463
464static int nmi_resume(struct sys_device *dev)
465{
466 if (nmi_enabled == 1)
467 nmi_cpu_start(NULL);
468 return 0;
469}
470
471static struct sysdev_class oprofile_sysclass = {
472 .name = "oprofile",
473 .resume = nmi_resume,
474 .suspend = nmi_suspend,
475};
476
477static struct sys_device device_oprofile = {
478 .id = 0,
479 .cls = &oprofile_sysclass,
480};
481
482static int __init init_sysfs(void)
483{
484 int error;
485
486 error = sysdev_class_register(&oprofile_sysclass);
487 if (!error)
488 error = sysdev_register(&device_oprofile);
489 return error;
490}
491
492static void exit_sysfs(void)
493{
494 sysdev_unregister(&device_oprofile);
495 sysdev_class_unregister(&oprofile_sysclass);
496}
497
498#else
499#define init_sysfs() do { } while (0)
500#define exit_sysfs() do { } while (0)
501#endif /* CONFIG_PM */
502
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100503static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 __u8 cpu_model = boot_cpu_data.x86_model;
506
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200507 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return 0;
509
510#ifndef CONFIG_SMP
511 *cpu_type = "i386/p4";
512 model = &op_p4_spec;
513 return 1;
514#else
515 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100516 case 1:
517 *cpu_type = "i386/p4";
518 model = &op_p4_spec;
519 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100521 case 2:
522 *cpu_type = "i386/p4-ht";
523 model = &op_p4_ht2_spec;
524 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526#endif
527
528 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
529 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
530 return 0;
531}
532
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200533static int force_arch_perfmon;
534static int force_cpu_type(const char *str, struct kernel_param *kp)
535{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200536 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200537 force_arch_perfmon = 1;
538 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
539 }
540
541 return 0;
542}
543module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200544
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100545static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter802070f2009-06-12 18:32:07 +0200548 struct op_x86_model_spec const *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200550 if (force_arch_perfmon && cpu_has_arch_perfmon)
551 return 0;
552
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700553 switch (cpu_model) {
554 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700556 break;
557 case 3 ... 5:
558 *cpu_type = "i386/pii";
559 break;
560 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500561 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700562 *cpu_type = "i386/piii";
563 break;
564 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500565 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700566 *cpu_type = "i386/p6_mobile";
567 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700568 case 14:
569 *cpu_type = "i386/core";
570 break;
571 case 15: case 23:
572 *cpu_type = "i386/core_2";
573 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200574 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200575 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200576 *cpu_type = "i386/core_i7";
577 break;
578 case 28:
579 *cpu_type = "i386/atom";
580 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700581 default:
582 /* Unknown */
583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Robert Richter802070f2009-06-12 18:32:07 +0200586 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 1;
588}
589
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100590/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static int using_nmi;
592
David Gibson96d08212005-09-06 15:17:26 -0700593int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 __u8 vendor = boot_cpu_data.x86_vendor;
596 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200597 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200598 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (!cpu_has_apic)
601 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100604 case X86_VENDOR_AMD:
605 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100607 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100608 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100609 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100611 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100612 /*
613 * Actually it could be i386/hammer too, but
614 * give user space an consistent name.
615 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100616 cpu_type = "x86-64/hammer";
617 break;
618 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100619 cpu_type = "x86-64/family10";
620 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200621 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200622 cpu_type = "x86-64/family11h";
623 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100624 default:
625 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100626 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100627 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100628 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100630 case X86_VENDOR_INTEL:
631 switch (family) {
632 /* Pentium IV */
633 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200634 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100635 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100637 /* A P6-class processor */
638 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200639 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641
642 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200643 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100644 }
Andi Kleenb9917022008-08-18 14:50:31 +0200645
Robert Richtere4192942008-10-12 15:12:34 -0400646 if (cpu_type)
647 break;
648
649 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200650 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400651
652 /* use arch perfmon as fallback */
653 cpu_type = "i386/arch_perfmon";
654 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100655 break;
656
657 default:
658 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200661#ifdef CONFIG_SMP
662 register_cpu_notifier(&oprofile_cpu_nb);
663#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200664 /* default values, can be overwritten by model */
Jason Yeh4d4036e2009-07-08 13:49:38 +0200665#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
666 __raw_get_cpu_var(switch_index) = 0;
667#endif
Robert Richter6e63ea42009-07-07 19:25:39 +0200668 ops->create_files = nmi_create_files;
669 ops->setup = nmi_setup;
670 ops->shutdown = nmi_shutdown;
671 ops->start = nmi_start;
672 ops->stop = nmi_stop;
673 ops->cpu_type = cpu_type;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200674#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
675 ops->switch_events = nmi_switch_event;
676#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200677
Robert Richteradf5ec02008-07-22 21:08:48 +0200678 if (model->init)
679 ret = model->init(ops);
680 if (ret)
681 return ret;
682
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100683 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
686 return 0;
687}
688
David Gibson96d08212005-09-06 15:17:26 -0700689void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200691 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100692 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200693#ifdef CONFIG_SMP
694 unregister_cpu_notifier(&oprofile_cpu_nb);
695#endif
696 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200697 if (model->exit)
698 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}