blob: f38c5cf0fdbb9d14da3610fddd10ee210d06093f [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
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010030static struct op_x86_model_spec const *model;
Mike Travisd18d00f2008-03-25 15:06:59 -070031static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
32static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
Don Zickus2fbe7b22006-09-26 10:52:27 +020033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* 0 == registered but off, 1 == registered and on */
35static int nmi_enabled = 0;
36
Jason Yeh4d4036e2009-07-08 13:49:38 +020037
38#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
39extern atomic_t multiplex_counter;
40#endif
41
42struct op_counter_config counter_config[OP_MAX_COUNTER];
43
Robert Richter3370d352009-05-25 15:10:32 +020044/* common functions */
45
46u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
47 struct op_counter_config *counter_config)
48{
49 u64 val = 0;
50 u16 event = (u16)counter_config->event;
51
52 val |= ARCH_PERFMON_EVENTSEL_INT;
53 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
54 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
55 val |= (counter_config->unit_mask & 0xFF) << 8;
56 event &= model->event_mask ? model->event_mask : 0xFF;
57 val |= event & 0xFF;
58 val |= (event & 0x0F00) << 24;
59
60 return val;
61}
62
63
Adrian Bunkc7c19f82006-09-26 10:52:27 +020064static int profile_exceptions_notify(struct notifier_block *self,
65 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Don Zickus2fbe7b22006-09-26 10:52:27 +020067 struct die_args *args = (struct die_args *)data;
68 int ret = NOTIFY_DONE;
69 int cpu = smp_processor_id();
70
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010071 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020072 case DIE_NMI:
Mike Galbraith5b75af02009-02-04 17:11:34 +010073 case DIE_NMI_IPI:
74 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
75 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020076 break;
77 default:
78 break;
79 }
80 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Don Zickus2fbe7b22006-09-26 10:52:27 +020082
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010083static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010085 struct op_msr *counters = msrs->counters;
86 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned int i;
88
Robert Richter1a245c42009-06-05 15:54:24 +020089 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020090 if (counters[i].addr)
91 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010093
Robert Richter1a245c42009-06-05 15:54:24 +020094 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020095 if (controls[i].addr)
96 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98}
99
Robert Richterd8471ad2009-07-16 13:04:43 +0200100#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
101
102static DEFINE_PER_CPU(int, switch_index);
103
104inline int op_x86_phys_to_virt(int phys)
105{
106 return __get_cpu_var(switch_index) + phys;
107}
108
Robert Richter6ab82f92009-07-09 14:40:04 +0200109static void nmi_shutdown_mux(void)
110{
111 int i;
112 for_each_possible_cpu(i) {
113 kfree(per_cpu(cpu_msrs, i).multiplex);
114 per_cpu(cpu_msrs, i).multiplex = NULL;
115 per_cpu(switch_index, i) = 0;
116 }
117}
118
119static int nmi_setup_mux(void)
120{
121 size_t multiplex_size =
122 sizeof(struct op_msr) * model->num_virt_counters;
123 int i;
124 for_each_possible_cpu(i) {
125 per_cpu(cpu_msrs, i).multiplex =
126 kmalloc(multiplex_size, GFP_KERNEL);
127 if (!per_cpu(cpu_msrs, i).multiplex)
128 return 0;
129 }
130 return 1;
131}
132
Robert Richter48fb4b42009-07-09 14:38:49 +0200133static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
134{
135 int i;
136 struct op_msr *multiplex = msrs->multiplex;
137
138 for (i = 0; i < model->num_virt_counters; ++i) {
139 if (counter_config[i].enabled) {
140 multiplex[i].saved = -(u64)counter_config[i].count;
141 } else {
142 multiplex[i].addr = 0;
143 multiplex[i].saved = 0;
144 }
145 }
146
147 per_cpu(switch_index, cpu) = 0;
148}
149
Robert Richterd0f585d2009-07-09 14:38:49 +0200150static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
151{
152 struct op_msr *multiplex = msrs->multiplex;
153 int i;
154
155 for (i = 0; i < model->num_counters; ++i) {
156 int virt = op_x86_phys_to_virt(i);
157 if (multiplex[virt].addr)
158 rdmsrl(multiplex[virt].addr, multiplex[virt].saved);
159 }
160}
161
162static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
163{
164 struct op_msr *multiplex = msrs->multiplex;
165 int i;
166
167 for (i = 0; i < model->num_counters; ++i) {
168 int virt = op_x86_phys_to_virt(i);
169 if (multiplex[virt].addr)
170 wrmsrl(multiplex[virt].addr, multiplex[virt].saved);
171 }
172}
173
Robert Richterd8471ad2009-07-16 13:04:43 +0200174#else
175
176inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200177static inline void nmi_shutdown_mux(void) { }
178static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200179static inline void
180nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200181
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static void free_msrs(void)
185{
186 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800187 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700188 kfree(per_cpu(cpu_msrs, i).counters);
189 per_cpu(cpu_msrs, i).counters = NULL;
190 kfree(per_cpu(cpu_msrs, i).controls);
191 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int allocate_msrs(void)
196{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
198 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
199
Robert Richter4c168ea2008-09-24 11:08:52 +0200200 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700201 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700202 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200203 GFP_KERNEL);
204 if (!per_cpu(cpu_msrs, i).counters)
205 return 0;
Robert Richter4c168ea2008-09-24 11:08:52 +0200206 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200207 GFP_KERNEL);
208 if (!per_cpu(cpu_msrs, i).controls)
209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
Robert Richter6ab82f92009-07-09 14:40:04 +0200212 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100215static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700218 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200219 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200221 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200222 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700224 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 apic_write(APIC_LVTPC, APIC_DM_NMI);
226}
227
Don Zickus2fbe7b22006-09-26 10:52:27 +0200228static struct notifier_block profile_exceptions_nb = {
229 .notifier_call = profile_exceptions_notify,
230 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100231 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200232};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234static int nmi_setup(void)
235{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100236 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200237 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!allocate_msrs())
Robert Richter6ab82f92009-07-09 14:40:04 +0200240 err = -ENOMEM;
241 else if (!nmi_setup_mux())
242 err = -ENOMEM;
243 else
244 err = register_die_notifier(&profile_exceptions_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100246 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 free_msrs();
Robert Richter6ab82f92009-07-09 14:40:04 +0200248 nmi_shutdown_mux();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200251
Robert Richter4c168ea2008-09-24 11:08:52 +0200252 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * of msrs are distinct for save and setup operations
254 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200255
256 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700257 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100258 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700259 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700260 memcpy(per_cpu(cpu_msrs, cpu).counters,
261 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700262 sizeof(struct op_msr) * model->num_counters);
263
Mike Travisd18d00f2008-03-25 15:06:59 -0700264 memcpy(per_cpu(cpu_msrs, cpu).controls,
265 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700266 sizeof(struct op_msr) * model->num_controls);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200267#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
268 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
269 per_cpu(cpu_msrs, 0).multiplex,
270 sizeof(struct op_msr) * model->num_virt_counters);
271#endif
Chris Wright0939c172007-06-01 00:46:39 -0700272 }
Andi Kleen6c977aa2007-05-21 14:31:45 +0200273 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200274 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 nmi_enabled = 1;
276 return 0;
277}
278
Robert Richter44ab9a62009-07-09 18:33:02 +0200279static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100281 struct op_msr *counters = msrs->counters;
282 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned int i;
284
Robert Richter1a245c42009-06-05 15:54:24 +0200285 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200286 if (controls[i].addr)
287 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100289
Robert Richter1a245c42009-06-05 15:54:24 +0200290 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200291 if (counters[i].addr)
292 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100296static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 unsigned int v;
299 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200300 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* restoring APIC_LVTPC can trigger an apic error because the delivery
303 * mode and vector nr combination can be illegal. That's by design: on
304 * power on apic lvt contain a zero vector nr which are legal only for
305 * NMI delivery mode. So inhibit apic err before restoring lvtpc
306 */
307 v = apic_read(APIC_LVTERR);
308 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700309 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200311 nmi_cpu_restore_registers(msrs);
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);
Robert Richter6ab82f92009-07-09 14:40:04 +0200321 nmi_shutdown_mux();
Andrea Righib61e06f2008-09-20 18:02:27 +0200322 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700323 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200325 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100328static void nmi_cpu_start(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Mike Travisd18d00f2008-03-25 15:06:59 -0700330 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 model->start(msrs);
332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334static int nmi_start(void)
335{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200336 on_each_cpu(nmi_cpu_start, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100339
340static void nmi_cpu_stop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Mike Travisd18d00f2008-03-25 15:06:59 -0700342 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 model->stop(msrs);
344}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static void nmi_stop(void)
347{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200348 on_each_cpu(nmi_cpu_stop, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100351static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 unsigned int i;
354
Jason Yeh4d4036e2009-07-08 13:49:38 +0200355 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100356 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700357 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100358
Jason Yeh4d4036e2009-07-08 13:49:38 +0200359#ifndef CONFIG_OPROFILE_EVENT_MULTIPLEX
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100360 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200361 * available for use. This should protect userspace app.
362 * NOTE: assumes 1:1 mapping here (that counters are organized
363 * sequentially in their struct assignment).
364 */
365 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
366 continue;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200367#endif /* CONFIG_OPROFILE_EVENT_MULTIPLEX */
Don Zickuscb9c4482006-09-26 10:52:26 +0200368
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700369 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100371 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
372 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
373 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
374 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
375 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
376 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 return 0;
380}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100381
Jason Yeh4d4036e2009-07-08 13:49:38 +0200382#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
383
384static void nmi_cpu_switch(void *dummy)
385{
386 int cpu = smp_processor_id();
387 int si = per_cpu(switch_index, cpu);
388 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
389
390 nmi_cpu_stop(NULL);
391 nmi_cpu_save_mpx_registers(msrs);
392
393 /* move to next set */
394 si += model->num_counters;
395 if ((si > model->num_virt_counters) || (counter_config[si].count == 0))
396 per_cpu(switch_index, cpu) = 0;
397 else
398 per_cpu(switch_index, cpu) = si;
399
400 model->switch_ctrl(model, msrs);
401 nmi_cpu_restore_mpx_registers(msrs);
402
403 nmi_cpu_start(NULL);
404}
405
406
407/*
408 * Quick check to see if multiplexing is necessary.
409 * The check should be sufficient since counters are used
410 * in ordre.
411 */
412static int nmi_multiplex_on(void)
413{
414 return counter_config[model->num_counters].count ? 0 : -EINVAL;
415}
416
417static int nmi_switch_event(void)
418{
419 if (!model->switch_ctrl)
420 return -ENOSYS; /* not implemented */
421 if (nmi_multiplex_on() < 0)
422 return -EINVAL; /* not necessary */
423
424 on_each_cpu(nmi_cpu_switch, NULL, 1);
425
426 atomic_inc(&multiplex_counter);
427
428 return 0;
429}
430
431#endif
432
Robert Richter69046d42008-09-05 12:17:40 +0200433#ifdef CONFIG_SMP
434static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
435 void *data)
436{
437 int cpu = (unsigned long)data;
438 switch (action) {
439 case CPU_DOWN_FAILED:
440 case CPU_ONLINE:
441 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
442 break;
443 case CPU_DOWN_PREPARE:
444 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
445 break;
446 }
447 return NOTIFY_DONE;
448}
449
450static struct notifier_block oprofile_cpu_nb = {
451 .notifier_call = oprofile_cpu_notifier
452};
453#endif
454
455#ifdef CONFIG_PM
456
457static int nmi_suspend(struct sys_device *dev, pm_message_t state)
458{
459 /* Only one CPU left, just stop that one */
460 if (nmi_enabled == 1)
461 nmi_cpu_stop(NULL);
462 return 0;
463}
464
465static int nmi_resume(struct sys_device *dev)
466{
467 if (nmi_enabled == 1)
468 nmi_cpu_start(NULL);
469 return 0;
470}
471
472static struct sysdev_class oprofile_sysclass = {
473 .name = "oprofile",
474 .resume = nmi_resume,
475 .suspend = nmi_suspend,
476};
477
478static struct sys_device device_oprofile = {
479 .id = 0,
480 .cls = &oprofile_sysclass,
481};
482
483static int __init init_sysfs(void)
484{
485 int error;
486
487 error = sysdev_class_register(&oprofile_sysclass);
488 if (!error)
489 error = sysdev_register(&device_oprofile);
490 return error;
491}
492
493static void exit_sysfs(void)
494{
495 sysdev_unregister(&device_oprofile);
496 sysdev_class_unregister(&oprofile_sysclass);
497}
498
499#else
500#define init_sysfs() do { } while (0)
501#define exit_sysfs() do { } while (0)
502#endif /* CONFIG_PM */
503
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100504static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 __u8 cpu_model = boot_cpu_data.x86_model;
507
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200508 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510
511#ifndef CONFIG_SMP
512 *cpu_type = "i386/p4";
513 model = &op_p4_spec;
514 return 1;
515#else
516 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100517 case 1:
518 *cpu_type = "i386/p4";
519 model = &op_p4_spec;
520 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100522 case 2:
523 *cpu_type = "i386/p4-ht";
524 model = &op_p4_ht2_spec;
525 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527#endif
528
529 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
530 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
531 return 0;
532}
533
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200534static int force_arch_perfmon;
535static int force_cpu_type(const char *str, struct kernel_param *kp)
536{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200537 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200538 force_arch_perfmon = 1;
539 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
540 }
541
542 return 0;
543}
544module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200545
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100546static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter802070f2009-06-12 18:32:07 +0200549 struct op_x86_model_spec const *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200551 if (force_arch_perfmon && cpu_has_arch_perfmon)
552 return 0;
553
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700554 switch (cpu_model) {
555 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700557 break;
558 case 3 ... 5:
559 *cpu_type = "i386/pii";
560 break;
561 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500562 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700563 *cpu_type = "i386/piii";
564 break;
565 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500566 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700567 *cpu_type = "i386/p6_mobile";
568 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700569 case 14:
570 *cpu_type = "i386/core";
571 break;
572 case 15: case 23:
573 *cpu_type = "i386/core_2";
574 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200575 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200576 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200577 *cpu_type = "i386/core_i7";
578 break;
579 case 28:
580 *cpu_type = "i386/atom";
581 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700582 default:
583 /* Unknown */
584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Robert Richter802070f2009-06-12 18:32:07 +0200587 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 1;
589}
590
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100591/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static int using_nmi;
593
David Gibson96d08212005-09-06 15:17:26 -0700594int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 __u8 vendor = boot_cpu_data.x86_vendor;
597 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200598 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200599 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (!cpu_has_apic)
602 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100605 case X86_VENDOR_AMD:
606 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100608 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100609 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100610 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100612 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100613 /*
614 * Actually it could be i386/hammer too, but
615 * give user space an consistent name.
616 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100617 cpu_type = "x86-64/hammer";
618 break;
619 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100620 cpu_type = "x86-64/family10";
621 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200622 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200623 cpu_type = "x86-64/family11h";
624 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100625 default:
626 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100627 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100628 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100631 case X86_VENDOR_INTEL:
632 switch (family) {
633 /* Pentium IV */
634 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200635 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100638 /* A P6-class processor */
639 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200640 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642
643 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200644 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100645 }
Andi Kleenb9917022008-08-18 14:50:31 +0200646
Robert Richtere4192942008-10-12 15:12:34 -0400647 if (cpu_type)
648 break;
649
650 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200651 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400652
653 /* use arch perfmon as fallback */
654 cpu_type = "i386/arch_perfmon";
655 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100656 break;
657
658 default:
659 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200662#ifdef CONFIG_SMP
663 register_cpu_notifier(&oprofile_cpu_nb);
664#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200665 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200666 ops->create_files = nmi_create_files;
667 ops->setup = nmi_setup;
668 ops->shutdown = nmi_shutdown;
669 ops->start = nmi_start;
670 ops->stop = nmi_stop;
671 ops->cpu_type = cpu_type;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200672#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
673 ops->switch_events = nmi_switch_event;
674#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200675
Robert Richteradf5ec02008-07-22 21:08:48 +0200676 if (model->init)
677 ret = model->init(ops);
678 if (ret)
679 return ret;
680
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100681 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
684 return 0;
685}
686
David Gibson96d08212005-09-06 15:17:26 -0700687void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200689 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100690 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200691#ifdef CONFIG_SMP
692 unregister_cpu_notifier(&oprofile_cpu_nb);
693#endif
694 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200695 if (model->exit)
696 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}