blob: cb88b1a0bd5f1c2c1a9b34a6e93c33400114f4ef [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
Robert Richter259a83a2009-07-09 15:12:35 +020030static struct op_x86_model_spec *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 +020037struct op_counter_config counter_config[OP_MAX_COUNTER];
38
Robert Richter3370d352009-05-25 15:10:32 +020039/* common functions */
40
41u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
42 struct op_counter_config *counter_config)
43{
44 u64 val = 0;
45 u16 event = (u16)counter_config->event;
46
47 val |= ARCH_PERFMON_EVENTSEL_INT;
48 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
49 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
50 val |= (counter_config->unit_mask & 0xFF) << 8;
51 event &= model->event_mask ? model->event_mask : 0xFF;
52 val |= event & 0xFF;
53 val |= (event & 0x0F00) << 24;
54
55 return val;
56}
57
58
Adrian Bunkc7c19f82006-09-26 10:52:27 +020059static int profile_exceptions_notify(struct notifier_block *self,
60 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Don Zickus2fbe7b22006-09-26 10:52:27 +020062 struct die_args *args = (struct die_args *)data;
63 int ret = NOTIFY_DONE;
64 int cpu = smp_processor_id();
65
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010066 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020067 case DIE_NMI:
Mike Galbraith5b75af02009-02-04 17:11:34 +010068 case DIE_NMI_IPI:
69 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
70 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020071 break;
72 default:
73 break;
74 }
75 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
Don Zickus2fbe7b22006-09-26 10:52:27 +020077
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010078static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010080 struct op_msr *counters = msrs->counters;
81 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned int i;
83
Robert Richter1a245c42009-06-05 15:54:24 +020084 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020085 if (counters[i].addr)
86 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010088
Robert Richter1a245c42009-06-05 15:54:24 +020089 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020090 if (controls[i].addr)
91 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93}
94
Robert Richterb28d1b92009-07-09 14:38:49 +020095static void nmi_cpu_start(void *dummy)
96{
97 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
98 model->start(msrs);
99}
100
101static int nmi_start(void)
102{
103 on_each_cpu(nmi_cpu_start, NULL, 1);
104 return 0;
105}
106
107static void nmi_cpu_stop(void *dummy)
108{
109 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
110 model->stop(msrs);
111}
112
113static void nmi_stop(void)
114{
115 on_each_cpu(nmi_cpu_stop, NULL, 1);
116}
117
Robert Richterd8471ad2009-07-16 13:04:43 +0200118#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
119
120static DEFINE_PER_CPU(int, switch_index);
121
Robert Richter39e97f42009-07-09 15:11:45 +0200122static inline int has_mux(void)
123{
124 return !!model->switch_ctrl;
125}
126
Robert Richterd8471ad2009-07-16 13:04:43 +0200127inline int op_x86_phys_to_virt(int phys)
128{
129 return __get_cpu_var(switch_index) + phys;
130}
131
Robert Richter61d149d2009-07-10 15:47:17 +0200132inline int op_x86_virt_to_phys(int virt)
133{
134 return virt % model->num_counters;
135}
136
Robert Richter6ab82f92009-07-09 14:40:04 +0200137static void nmi_shutdown_mux(void)
138{
139 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200140
141 if (!has_mux())
142 return;
143
Robert Richter6ab82f92009-07-09 14:40:04 +0200144 for_each_possible_cpu(i) {
145 kfree(per_cpu(cpu_msrs, i).multiplex);
146 per_cpu(cpu_msrs, i).multiplex = NULL;
147 per_cpu(switch_index, i) = 0;
148 }
149}
150
151static int nmi_setup_mux(void)
152{
153 size_t multiplex_size =
154 sizeof(struct op_msr) * model->num_virt_counters;
155 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200156
157 if (!has_mux())
158 return 1;
159
Robert Richter6ab82f92009-07-09 14:40:04 +0200160 for_each_possible_cpu(i) {
161 per_cpu(cpu_msrs, i).multiplex =
162 kmalloc(multiplex_size, GFP_KERNEL);
163 if (!per_cpu(cpu_msrs, i).multiplex)
164 return 0;
165 }
Robert Richter39e97f42009-07-09 15:11:45 +0200166
Robert Richter6ab82f92009-07-09 14:40:04 +0200167 return 1;
168}
169
Robert Richter48fb4b42009-07-09 14:38:49 +0200170static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
171{
172 int i;
173 struct op_msr *multiplex = msrs->multiplex;
174
Robert Richter39e97f42009-07-09 15:11:45 +0200175 if (!has_mux())
176 return;
177
Robert Richter48fb4b42009-07-09 14:38:49 +0200178 for (i = 0; i < model->num_virt_counters; ++i) {
179 if (counter_config[i].enabled) {
180 multiplex[i].saved = -(u64)counter_config[i].count;
181 } else {
182 multiplex[i].addr = 0;
183 multiplex[i].saved = 0;
184 }
185 }
186
187 per_cpu(switch_index, cpu) = 0;
188}
189
Robert Richterd0f585d2009-07-09 14:38:49 +0200190static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
191{
192 struct op_msr *multiplex = msrs->multiplex;
193 int i;
194
195 for (i = 0; i < model->num_counters; ++i) {
196 int virt = op_x86_phys_to_virt(i);
197 if (multiplex[virt].addr)
198 rdmsrl(multiplex[virt].addr, multiplex[virt].saved);
199 }
200}
201
202static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
203{
204 struct op_msr *multiplex = msrs->multiplex;
205 int i;
206
207 for (i = 0; i < model->num_counters; ++i) {
208 int virt = op_x86_phys_to_virt(i);
209 if (multiplex[virt].addr)
210 wrmsrl(multiplex[virt].addr, multiplex[virt].saved);
211 }
212}
213
Robert Richterb28d1b92009-07-09 14:38:49 +0200214static void nmi_cpu_switch(void *dummy)
215{
216 int cpu = smp_processor_id();
217 int si = per_cpu(switch_index, cpu);
218 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
219
220 nmi_cpu_stop(NULL);
221 nmi_cpu_save_mpx_registers(msrs);
222
223 /* move to next set */
224 si += model->num_counters;
225 if ((si > model->num_virt_counters) || (counter_config[si].count == 0))
226 per_cpu(switch_index, cpu) = 0;
227 else
228 per_cpu(switch_index, cpu) = si;
229
230 model->switch_ctrl(model, msrs);
231 nmi_cpu_restore_mpx_registers(msrs);
232
233 nmi_cpu_start(NULL);
234}
235
236
237/*
238 * Quick check to see if multiplexing is necessary.
239 * The check should be sufficient since counters are used
240 * in ordre.
241 */
242static int nmi_multiplex_on(void)
243{
244 return counter_config[model->num_counters].count ? 0 : -EINVAL;
245}
246
247static int nmi_switch_event(void)
248{
Robert Richter39e97f42009-07-09 15:11:45 +0200249 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200250 return -ENOSYS; /* not implemented */
251 if (nmi_multiplex_on() < 0)
252 return -EINVAL; /* not necessary */
253
254 on_each_cpu(nmi_cpu_switch, NULL, 1);
255
Robert Richterb28d1b92009-07-09 14:38:49 +0200256 return 0;
257}
258
Robert Richter52805142009-07-09 16:02:44 +0200259static inline void mux_init(struct oprofile_operations *ops)
260{
261 if (has_mux())
262 ops->switch_events = nmi_switch_event;
263}
264
Robert Richter4d015f72009-07-09 21:42:51 +0200265static void mux_clone(int cpu)
266{
267 if (!has_mux())
268 return;
269
270 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
271 per_cpu(cpu_msrs, 0).multiplex,
272 sizeof(struct op_msr) * model->num_virt_counters);
273}
274
Robert Richterd8471ad2009-07-16 13:04:43 +0200275#else
276
277inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200278inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200279static inline void nmi_shutdown_mux(void) { }
280static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200281static inline void
282nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200283static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200284static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200285
286#endif
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static void free_msrs(void)
289{
290 int i;
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800291 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700292 kfree(per_cpu(cpu_msrs, i).counters);
293 per_cpu(cpu_msrs, i).counters = NULL;
294 kfree(per_cpu(cpu_msrs, i).controls);
295 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static int allocate_msrs(void)
300{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
302 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
303
Robert Richter4c168ea2008-09-24 11:08:52 +0200304 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700305 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700306 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200307 GFP_KERNEL);
308 if (!per_cpu(cpu_msrs, i).counters)
309 return 0;
Robert Richter4c168ea2008-09-24 11:08:52 +0200310 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200311 GFP_KERNEL);
312 if (!per_cpu(cpu_msrs, i).controls)
313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
Robert Richter6ab82f92009-07-09 14:40:04 +0200316 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100319static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700322 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200323 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200325 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200326 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700328 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 apic_write(APIC_LVTPC, APIC_DM_NMI);
330}
331
Don Zickus2fbe7b22006-09-26 10:52:27 +0200332static struct notifier_block profile_exceptions_nb = {
333 .notifier_call = profile_exceptions_notify,
334 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100335 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200336};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338static int nmi_setup(void)
339{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100340 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200341 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!allocate_msrs())
Robert Richter6ab82f92009-07-09 14:40:04 +0200344 err = -ENOMEM;
345 else if (!nmi_setup_mux())
346 err = -ENOMEM;
347 else
348 err = register_die_notifier(&profile_exceptions_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100350 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 free_msrs();
Robert Richter6ab82f92009-07-09 14:40:04 +0200352 nmi_shutdown_mux();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200355
Robert Richter4c168ea2008-09-24 11:08:52 +0200356 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * of msrs are distinct for save and setup operations
358 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200359
360 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700361 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100362 for_each_possible_cpu(cpu) {
Robert Richter4d015f72009-07-09 21:42:51 +0200363 if (!cpu)
364 continue;
Chris Wright0939c172007-06-01 00:46:39 -0700365
Robert Richter4d015f72009-07-09 21:42:51 +0200366 memcpy(per_cpu(cpu_msrs, cpu).counters,
367 per_cpu(cpu_msrs, 0).counters,
368 sizeof(struct op_msr) * model->num_counters);
369
370 memcpy(per_cpu(cpu_msrs, cpu).controls,
371 per_cpu(cpu_msrs, 0).controls,
372 sizeof(struct op_msr) * model->num_controls);
373
374 mux_clone(cpu);
Andi Kleen6c977aa2007-05-21 14:31:45 +0200375 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200376 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 nmi_enabled = 1;
378 return 0;
379}
380
Robert Richter44ab9a62009-07-09 18:33:02 +0200381static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100383 struct op_msr *counters = msrs->counters;
384 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned int i;
386
Robert Richter1a245c42009-06-05 15:54:24 +0200387 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200388 if (controls[i].addr)
389 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100391
Robert Richter1a245c42009-06-05 15:54:24 +0200392 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200393 if (counters[i].addr)
394 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100398static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 unsigned int v;
401 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200402 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* restoring APIC_LVTPC can trigger an apic error because the delivery
405 * mode and vector nr combination can be illegal. That's by design: on
406 * power on apic lvt contain a zero vector nr which are legal only for
407 * NMI delivery mode. So inhibit apic err before restoring lvtpc
408 */
409 v = apic_read(APIC_LVTERR);
410 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700411 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200413 nmi_cpu_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static void nmi_shutdown(void)
417{
Andrea Righib61e06f2008-09-20 18:02:27 +0200418 struct op_msrs *msrs;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200421 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200422 unregister_die_notifier(&profile_exceptions_nb);
Robert Richter6ab82f92009-07-09 14:40:04 +0200423 nmi_shutdown_mux();
Andrea Righib61e06f2008-09-20 18:02:27 +0200424 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700425 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200427 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100430static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 unsigned int i;
433
Jason Yeh4d4036e2009-07-08 13:49:38 +0200434 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100435 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700436 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100437
438 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200439 * available for use. This should protect userspace app.
440 * NOTE: assumes 1:1 mapping here (that counters are organized
441 * sequentially in their struct assignment).
442 */
Robert Richter11be1a72009-07-10 18:15:21 +0200443 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200444 continue;
445
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700446 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100448 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
449 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
450 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
451 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
452 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
453 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 return 0;
457}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100458
Robert Richter69046d42008-09-05 12:17:40 +0200459#ifdef CONFIG_SMP
460static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
461 void *data)
462{
463 int cpu = (unsigned long)data;
464 switch (action) {
465 case CPU_DOWN_FAILED:
466 case CPU_ONLINE:
467 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
468 break;
469 case CPU_DOWN_PREPARE:
470 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
471 break;
472 }
473 return NOTIFY_DONE;
474}
475
476static struct notifier_block oprofile_cpu_nb = {
477 .notifier_call = oprofile_cpu_notifier
478};
479#endif
480
481#ifdef CONFIG_PM
482
483static int nmi_suspend(struct sys_device *dev, pm_message_t state)
484{
485 /* Only one CPU left, just stop that one */
486 if (nmi_enabled == 1)
487 nmi_cpu_stop(NULL);
488 return 0;
489}
490
491static int nmi_resume(struct sys_device *dev)
492{
493 if (nmi_enabled == 1)
494 nmi_cpu_start(NULL);
495 return 0;
496}
497
498static struct sysdev_class oprofile_sysclass = {
499 .name = "oprofile",
500 .resume = nmi_resume,
501 .suspend = nmi_suspend,
502};
503
504static struct sys_device device_oprofile = {
505 .id = 0,
506 .cls = &oprofile_sysclass,
507};
508
509static int __init init_sysfs(void)
510{
511 int error;
512
513 error = sysdev_class_register(&oprofile_sysclass);
514 if (!error)
515 error = sysdev_register(&device_oprofile);
516 return error;
517}
518
519static void exit_sysfs(void)
520{
521 sysdev_unregister(&device_oprofile);
522 sysdev_class_unregister(&oprofile_sysclass);
523}
524
525#else
526#define init_sysfs() do { } while (0)
527#define exit_sysfs() do { } while (0)
528#endif /* CONFIG_PM */
529
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100530static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 __u8 cpu_model = boot_cpu_data.x86_model;
533
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200534 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536
537#ifndef CONFIG_SMP
538 *cpu_type = "i386/p4";
539 model = &op_p4_spec;
540 return 1;
541#else
542 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100543 case 1:
544 *cpu_type = "i386/p4";
545 model = &op_p4_spec;
546 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100548 case 2:
549 *cpu_type = "i386/p4-ht";
550 model = &op_p4_ht2_spec;
551 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553#endif
554
555 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
556 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
557 return 0;
558}
559
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200560static int force_arch_perfmon;
561static int force_cpu_type(const char *str, struct kernel_param *kp)
562{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200563 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200564 force_arch_perfmon = 1;
565 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
566 }
567
568 return 0;
569}
570module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200571
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100572static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200575 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200577 if (force_arch_perfmon && cpu_has_arch_perfmon)
578 return 0;
579
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700580 switch (cpu_model) {
581 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700583 break;
584 case 3 ... 5:
585 *cpu_type = "i386/pii";
586 break;
587 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500588 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700589 *cpu_type = "i386/piii";
590 break;
591 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500592 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700593 *cpu_type = "i386/p6_mobile";
594 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700595 case 14:
596 *cpu_type = "i386/core";
597 break;
598 case 15: case 23:
599 *cpu_type = "i386/core_2";
600 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200601 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200602 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200603 *cpu_type = "i386/core_i7";
604 break;
605 case 28:
606 *cpu_type = "i386/atom";
607 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700608 default:
609 /* Unknown */
610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Robert Richter802070f2009-06-12 18:32:07 +0200613 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 1;
615}
616
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100617/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static int using_nmi;
619
David Gibson96d08212005-09-06 15:17:26 -0700620int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 __u8 vendor = boot_cpu_data.x86_vendor;
623 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200624 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200625 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (!cpu_has_apic)
628 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100631 case X86_VENDOR_AMD:
632 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100634 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100635 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100636 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100638 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100639 /*
640 * Actually it could be i386/hammer too, but
641 * give user space an consistent name.
642 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100643 cpu_type = "x86-64/hammer";
644 break;
645 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100646 cpu_type = "x86-64/family10";
647 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200648 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200649 cpu_type = "x86-64/family11h";
650 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100651 default:
652 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100653 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100654 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100657 case X86_VENDOR_INTEL:
658 switch (family) {
659 /* Pentium IV */
660 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200661 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100664 /* A P6-class processor */
665 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200666 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
668
669 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200670 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100671 }
Andi Kleenb9917022008-08-18 14:50:31 +0200672
Robert Richtere4192942008-10-12 15:12:34 -0400673 if (cpu_type)
674 break;
675
676 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200677 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400678
679 /* use arch perfmon as fallback */
680 cpu_type = "i386/arch_perfmon";
681 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100682 break;
683
684 default:
685 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200688#ifdef CONFIG_SMP
689 register_cpu_notifier(&oprofile_cpu_nb);
690#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200691 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200692 ops->create_files = nmi_create_files;
693 ops->setup = nmi_setup;
694 ops->shutdown = nmi_shutdown;
695 ops->start = nmi_start;
696 ops->stop = nmi_stop;
697 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200698
Robert Richteradf5ec02008-07-22 21:08:48 +0200699 if (model->init)
700 ret = model->init(ops);
701 if (ret)
702 return ret;
703
Robert Richter52471c62009-07-06 14:43:55 +0200704 if (!model->num_virt_counters)
705 model->num_virt_counters = model->num_counters;
706
Robert Richter52805142009-07-09 16:02:44 +0200707 mux_init(ops);
708
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100709 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
712 return 0;
713}
714
David Gibson96d08212005-09-06 15:17:26 -0700715void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200717 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100718 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200719#ifdef CONFIG_SMP
720 unregister_cpu_notifier(&oprofile_cpu_nb);
721#endif
722 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200723 if (model->exit)
724 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}