blob: 7ca4d43e8988d87dad64994b15b9a42ee5d5443d [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>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010018#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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
Robert Richter6ae56b52010-04-29 14:55:55 +020034/* must be protected with get_online_cpus()/put_online_cpus(): */
35static int nmi_enabled;
36static int ctr_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Jason Yeh4d4036e2009-07-08 13:49:38 +020038struct op_counter_config counter_config[OP_MAX_COUNTER];
39
Robert Richter3370d352009-05-25 15:10:32 +020040/* common functions */
41
42u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
43 struct op_counter_config *counter_config)
44{
45 u64 val = 0;
46 u16 event = (u16)counter_config->event;
47
48 val |= ARCH_PERFMON_EVENTSEL_INT;
49 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
50 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
51 val |= (counter_config->unit_mask & 0xFF) << 8;
Andi Kleen914a76c2011-03-16 15:44:33 -040052 counter_config->extra &= (ARCH_PERFMON_EVENTSEL_INV |
53 ARCH_PERFMON_EVENTSEL_EDGE |
54 ARCH_PERFMON_EVENTSEL_CMASK);
55 val |= counter_config->extra;
Robert Richter3370d352009-05-25 15:10:32 +020056 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;
Don Zickus2fbe7b22006-09-26 10:52:27 +020069
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010070 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020071 case DIE_NMI:
Robert Richterde654642010-05-03 14:41:22 +020072 if (ctr_running)
73 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
74 else if (!nmi_enabled)
75 break;
76 else
77 model->stop(&__get_cpu_var(cpu_msrs));
Mike Galbraith5b75af02009-02-04 17:11:34 +010078 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020079 break;
80 default:
81 break;
82 }
83 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Don Zickus2fbe7b22006-09-26 10:52:27 +020085
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010086static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010088 struct op_msr *counters = msrs->counters;
89 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned int i;
91
Robert Richter1a245c42009-06-05 15:54:24 +020092 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020093 if (counters[i].addr)
94 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010096
Robert Richter1a245c42009-06-05 15:54:24 +020097 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020098 if (controls[i].addr)
99 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101}
102
Robert Richterb28d1b92009-07-09 14:38:49 +0200103static void nmi_cpu_start(void *dummy)
104{
105 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200106 if (!msrs->controls)
107 WARN_ON_ONCE(1);
108 else
109 model->start(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200110}
111
112static int nmi_start(void)
113{
Robert Richter6ae56b52010-04-29 14:55:55 +0200114 get_online_cpus();
Robert Richter6ae56b52010-04-29 14:55:55 +0200115 ctr_running = 1;
Robert Richter8fe7e942011-06-01 15:31:44 +0200116 /* make ctr_running visible to the nmi handler: */
117 smp_mb();
118 on_each_cpu(nmi_cpu_start, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200119 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200120 return 0;
121}
122
123static void nmi_cpu_stop(void *dummy)
124{
125 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200126 if (!msrs->controls)
127 WARN_ON_ONCE(1);
128 else
129 model->stop(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200130}
131
132static void nmi_stop(void)
133{
Robert Richter6ae56b52010-04-29 14:55:55 +0200134 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200135 on_each_cpu(nmi_cpu_stop, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200136 ctr_running = 0;
137 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200138}
139
Robert Richterd8471ad2009-07-16 13:04:43 +0200140#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
141
142static DEFINE_PER_CPU(int, switch_index);
143
Robert Richter39e97f42009-07-09 15:11:45 +0200144static inline int has_mux(void)
145{
146 return !!model->switch_ctrl;
147}
148
Robert Richterd8471ad2009-07-16 13:04:43 +0200149inline int op_x86_phys_to_virt(int phys)
150{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100151 return __this_cpu_read(switch_index) + phys;
Robert Richterd8471ad2009-07-16 13:04:43 +0200152}
153
Robert Richter61d149d2009-07-10 15:47:17 +0200154inline int op_x86_virt_to_phys(int virt)
155{
156 return virt % model->num_counters;
157}
158
Robert Richter6ab82f92009-07-09 14:40:04 +0200159static void nmi_shutdown_mux(void)
160{
161 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200162
163 if (!has_mux())
164 return;
165
Robert Richter6ab82f92009-07-09 14:40:04 +0200166 for_each_possible_cpu(i) {
167 kfree(per_cpu(cpu_msrs, i).multiplex);
168 per_cpu(cpu_msrs, i).multiplex = NULL;
169 per_cpu(switch_index, i) = 0;
170 }
171}
172
173static int nmi_setup_mux(void)
174{
175 size_t multiplex_size =
176 sizeof(struct op_msr) * model->num_virt_counters;
177 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200178
179 if (!has_mux())
180 return 1;
181
Robert Richter6ab82f92009-07-09 14:40:04 +0200182 for_each_possible_cpu(i) {
183 per_cpu(cpu_msrs, i).multiplex =
Robert Richterc17c8fb2010-02-25 20:20:25 +0100184 kzalloc(multiplex_size, GFP_KERNEL);
Robert Richter6ab82f92009-07-09 14:40:04 +0200185 if (!per_cpu(cpu_msrs, i).multiplex)
186 return 0;
187 }
Robert Richter39e97f42009-07-09 15:11:45 +0200188
Robert Richter6ab82f92009-07-09 14:40:04 +0200189 return 1;
190}
191
Robert Richter48fb4b42009-07-09 14:38:49 +0200192static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
193{
194 int i;
195 struct op_msr *multiplex = msrs->multiplex;
196
Robert Richter39e97f42009-07-09 15:11:45 +0200197 if (!has_mux())
198 return;
199
Robert Richter48fb4b42009-07-09 14:38:49 +0200200 for (i = 0; i < model->num_virt_counters; ++i) {
201 if (counter_config[i].enabled) {
202 multiplex[i].saved = -(u64)counter_config[i].count;
203 } else {
Robert Richter48fb4b42009-07-09 14:38:49 +0200204 multiplex[i].saved = 0;
205 }
206 }
207
208 per_cpu(switch_index, cpu) = 0;
209}
210
Robert Richterd0f585d2009-07-09 14:38:49 +0200211static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
212{
Robert Richter68dc8192010-02-25 19:16:46 +0100213 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200214 struct op_msr *multiplex = msrs->multiplex;
215 int i;
216
217 for (i = 0; i < model->num_counters; ++i) {
218 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100219 if (counters[i].addr)
220 rdmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200221 }
222}
223
224static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
225{
Robert Richter68dc8192010-02-25 19:16:46 +0100226 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200227 struct op_msr *multiplex = msrs->multiplex;
228 int i;
229
230 for (i = 0; i < model->num_counters; ++i) {
231 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100232 if (counters[i].addr)
233 wrmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200234 }
235}
236
Robert Richterb28d1b92009-07-09 14:38:49 +0200237static void nmi_cpu_switch(void *dummy)
238{
239 int cpu = smp_processor_id();
240 int si = per_cpu(switch_index, cpu);
241 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
242
243 nmi_cpu_stop(NULL);
244 nmi_cpu_save_mpx_registers(msrs);
245
246 /* move to next set */
247 si += model->num_counters;
Suravee Suthikulpanitd8cc1082010-01-18 11:25:36 -0600248 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
Robert Richterb28d1b92009-07-09 14:38:49 +0200249 per_cpu(switch_index, cpu) = 0;
250 else
251 per_cpu(switch_index, cpu) = si;
252
253 model->switch_ctrl(model, msrs);
254 nmi_cpu_restore_mpx_registers(msrs);
255
256 nmi_cpu_start(NULL);
257}
258
259
260/*
261 * Quick check to see if multiplexing is necessary.
262 * The check should be sufficient since counters are used
263 * in ordre.
264 */
265static int nmi_multiplex_on(void)
266{
267 return counter_config[model->num_counters].count ? 0 : -EINVAL;
268}
269
270static int nmi_switch_event(void)
271{
Robert Richter39e97f42009-07-09 15:11:45 +0200272 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200273 return -ENOSYS; /* not implemented */
274 if (nmi_multiplex_on() < 0)
275 return -EINVAL; /* not necessary */
276
Robert Richter6ae56b52010-04-29 14:55:55 +0200277 get_online_cpus();
278 if (ctr_running)
279 on_each_cpu(nmi_cpu_switch, NULL, 1);
280 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200281
Robert Richterb28d1b92009-07-09 14:38:49 +0200282 return 0;
283}
284
Robert Richter52805142009-07-09 16:02:44 +0200285static inline void mux_init(struct oprofile_operations *ops)
286{
287 if (has_mux())
288 ops->switch_events = nmi_switch_event;
289}
290
Robert Richter4d015f72009-07-09 21:42:51 +0200291static void mux_clone(int cpu)
292{
293 if (!has_mux())
294 return;
295
296 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
297 per_cpu(cpu_msrs, 0).multiplex,
298 sizeof(struct op_msr) * model->num_virt_counters);
299}
300
Robert Richterd8471ad2009-07-16 13:04:43 +0200301#else
302
303inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200304inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200305static inline void nmi_shutdown_mux(void) { }
306static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200307static inline void
308nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200309static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200310static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200311
312#endif
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static void free_msrs(void)
315{
316 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800317 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700318 kfree(per_cpu(cpu_msrs, i).counters);
319 per_cpu(cpu_msrs, i).counters = NULL;
320 kfree(per_cpu(cpu_msrs, i).controls);
321 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100323 nmi_shutdown_mux();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int allocate_msrs(void)
327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
329 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
330
Robert Richter4c168ea2008-09-24 11:08:52 +0200331 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700332 for_each_possible_cpu(i) {
Robert Richterc17c8fb2010-02-25 20:20:25 +0100333 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200334 GFP_KERNEL);
335 if (!per_cpu(cpu_msrs, i).counters)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100336 goto fail;
Robert Richterc17c8fb2010-02-25 20:20:25 +0100337 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200338 GFP_KERNEL);
339 if (!per_cpu(cpu_msrs, i).controls)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100340 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100343 if (!nmi_setup_mux())
344 goto fail;
345
Robert Richter6ab82f92009-07-09 14:40:04 +0200346 return 1;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100347
348fail:
349 free_msrs();
350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100353static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700356 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200357 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200359 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200360 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700362 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 apic_write(APIC_LVTPC, APIC_DM_NMI);
364}
365
Don Zickus2fbe7b22006-09-26 10:52:27 +0200366static struct notifier_block profile_exceptions_nb = {
367 .notifier_call = profile_exceptions_notify,
368 .next = NULL,
Don Zickus166d7512011-01-06 16:18:49 -0500369 .priority = NMI_LOCAL_LOW_PRIOR,
Don Zickus2fbe7b22006-09-26 10:52:27 +0200370};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Robert Richter44ab9a62009-07-09 18:33:02 +0200372static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100374 struct op_msr *counters = msrs->counters;
375 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned int i;
377
Robert Richter1a245c42009-06-05 15:54:24 +0200378 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200379 if (controls[i].addr)
380 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100382
Robert Richter1a245c42009-06-05 15:54:24 +0200383 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200384 if (counters[i].addr)
385 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100389static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned int v;
392 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200393 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* restoring APIC_LVTPC can trigger an apic error because the delivery
396 * mode and vector nr combination can be illegal. That's by design: on
397 * power on apic lvt contain a zero vector nr which are legal only for
398 * NMI delivery mode. So inhibit apic err before restoring lvtpc
399 */
400 v = apic_read(APIC_LVTERR);
401 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700402 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200404 nmi_cpu_restore_registers(msrs);
Robert Richterbae663b2010-05-05 17:47:17 +0200405 if (model->cpu_down)
406 model->cpu_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Robert Richter6ae56b52010-04-29 14:55:55 +0200409static void nmi_cpu_up(void *dummy)
410{
411 if (nmi_enabled)
412 nmi_cpu_setup(dummy);
413 if (ctr_running)
414 nmi_cpu_start(dummy);
415}
416
417static void nmi_cpu_down(void *dummy)
418{
419 if (ctr_running)
420 nmi_cpu_stop(dummy);
421 if (nmi_enabled)
422 nmi_cpu_shutdown(dummy);
423}
424
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100425static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 unsigned int i;
428
Jason Yeh4d4036e2009-07-08 13:49:38 +0200429 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100430 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700431 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100432
433 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200434 * available for use. This should protect userspace app.
435 * NOTE: assumes 1:1 mapping here (that counters are organized
436 * sequentially in their struct assignment).
437 */
Robert Richter11be1a72009-07-10 18:15:21 +0200438 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200439 continue;
440
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700441 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100443 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
444 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
445 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
446 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
447 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
448 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Andi Kleen914a76c2011-03-16 15:44:33 -0400449 oprofilefs_create_ulong(sb, dir, "extra", &counter_config[i].extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 return 0;
453}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100454
Robert Richter69046d42008-09-05 12:17:40 +0200455static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
456 void *data)
457{
458 int cpu = (unsigned long)data;
459 switch (action) {
460 case CPU_DOWN_FAILED:
461 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200462 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200463 break;
464 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200465 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200466 break;
467 }
468 return NOTIFY_DONE;
469}
470
471static struct notifier_block oprofile_cpu_nb = {
472 .notifier_call = oprofile_cpu_notifier
473};
Robert Richter69046d42008-09-05 12:17:40 +0200474
Robert Richterd30d64c2010-05-03 15:52:26 +0200475static int nmi_setup(void)
476{
477 int err = 0;
478 int cpu;
479
480 if (!allocate_msrs())
481 return -ENOMEM;
482
483 /* We need to serialize save and setup for HT because the subset
484 * of msrs are distinct for save and setup operations
485 */
486
487 /* Assume saved/restored counters are the same on all CPUs */
488 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
489 if (err)
490 goto fail;
491
492 for_each_possible_cpu(cpu) {
493 if (!cpu)
494 continue;
495
496 memcpy(per_cpu(cpu_msrs, cpu).counters,
497 per_cpu(cpu_msrs, 0).counters,
498 sizeof(struct op_msr) * model->num_counters);
499
500 memcpy(per_cpu(cpu_msrs, cpu).controls,
501 per_cpu(cpu_msrs, 0).controls,
502 sizeof(struct op_msr) * model->num_controls);
503
504 mux_clone(cpu);
505 }
506
507 nmi_enabled = 0;
508 ctr_running = 0;
Robert Richter8fe7e942011-06-01 15:31:44 +0200509 /* make variables visible to the nmi handler: */
510 smp_mb();
Robert Richterd30d64c2010-05-03 15:52:26 +0200511 err = register_die_notifier(&profile_exceptions_nb);
512 if (err)
513 goto fail;
514
515 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200516 register_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200517 nmi_enabled = 1;
Robert Richter8fe7e942011-06-01 15:31:44 +0200518 /* make nmi_enabled visible to the nmi handler: */
519 smp_mb();
520 on_each_cpu(nmi_cpu_setup, NULL, 1);
Robert Richterd30d64c2010-05-03 15:52:26 +0200521 put_online_cpus();
522
523 return 0;
524fail:
525 free_msrs();
526 return err;
527}
528
529static void nmi_shutdown(void)
530{
531 struct op_msrs *msrs;
532
533 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200534 unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200535 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
536 nmi_enabled = 0;
537 ctr_running = 0;
538 put_online_cpus();
Robert Richter8fe7e942011-06-01 15:31:44 +0200539 /* make variables visible to the nmi handler: */
540 smp_mb();
Robert Richterd30d64c2010-05-03 15:52:26 +0200541 unregister_die_notifier(&profile_exceptions_nb);
542 msrs = &get_cpu_var(cpu_msrs);
543 model->shutdown(msrs);
544 free_msrs();
545 put_cpu_var(cpu_msrs);
546}
547
Robert Richter69046d42008-09-05 12:17:40 +0200548#ifdef CONFIG_PM
549
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100550static int nmi_suspend(void)
Robert Richter69046d42008-09-05 12:17:40 +0200551{
552 /* Only one CPU left, just stop that one */
553 if (nmi_enabled == 1)
554 nmi_cpu_stop(NULL);
555 return 0;
556}
557
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100558static void nmi_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200559{
560 if (nmi_enabled == 1)
561 nmi_cpu_start(NULL);
Robert Richter69046d42008-09-05 12:17:40 +0200562}
563
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100564static struct syscore_ops oprofile_syscore_ops = {
Robert Richter69046d42008-09-05 12:17:40 +0200565 .resume = nmi_resume,
566 .suspend = nmi_suspend,
567};
568
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100569static void __init init_suspend_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200570{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100571 register_syscore_ops(&oprofile_syscore_ops);
Robert Richter69046d42008-09-05 12:17:40 +0200572}
573
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100574static void exit_suspend_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200575{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100576 unregister_syscore_ops(&oprofile_syscore_ops);
Robert Richter69046d42008-09-05 12:17:40 +0200577}
578
579#else
Robert Richter269f45c2010-09-01 14:50:50 +0200580
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100581static inline void init_suspend_resume(void) { }
582static inline void exit_suspend_resume(void) { }
Robert Richter269f45c2010-09-01 14:50:50 +0200583
Robert Richter69046d42008-09-05 12:17:40 +0200584#endif /* CONFIG_PM */
585
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100586static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 __u8 cpu_model = boot_cpu_data.x86_model;
589
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200590 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592
593#ifndef CONFIG_SMP
594 *cpu_type = "i386/p4";
595 model = &op_p4_spec;
596 return 1;
597#else
598 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100599 case 1:
600 *cpu_type = "i386/p4";
601 model = &op_p4_spec;
602 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100604 case 2:
605 *cpu_type = "i386/p4-ht";
606 model = &op_p4_ht2_spec;
607 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609#endif
610
611 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
612 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
613 return 0;
614}
615
Robert Richter159a80b2011-10-11 19:39:16 +0200616enum __force_cpu_type {
617 reserved = 0, /* do not force */
618 timer,
619 arch_perfmon,
620};
621
622static int force_cpu_type;
623
624static int set_cpu_type(const char *str, struct kernel_param *kp)
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200625{
Robert Richter159a80b2011-10-11 19:39:16 +0200626 if (!strcmp(str, "timer")) {
627 force_cpu_type = timer;
628 printk(KERN_INFO "oprofile: forcing NMI timer mode\n");
629 } else if (!strcmp(str, "arch_perfmon")) {
630 force_cpu_type = arch_perfmon;
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200631 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
Robert Richter159a80b2011-10-11 19:39:16 +0200632 } else {
633 force_cpu_type = 0;
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200634 }
635
636 return 0;
637}
Robert Richter159a80b2011-10-11 19:39:16 +0200638module_param_call(cpu_type, set_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200639
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100640static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200643 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Robert Richter159a80b2011-10-11 19:39:16 +0200645 if (force_cpu_type == arch_perfmon && cpu_has_arch_perfmon)
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200646 return 0;
647
John Villalovos45c34e02010-05-07 12:41:40 -0400648 /*
649 * Documentation on identifying Intel processors by CPU family
650 * and model can be found in the Intel Software Developer's
651 * Manuals (SDM):
652 *
653 * http://www.intel.com/products/processor/manuals/
654 *
655 * As of May 2010 the documentation for this was in the:
656 * "Intel 64 and IA-32 Architectures Software Developer's
657 * Manual Volume 3B: System Programming Guide", "Table B-1
658 * CPUID Signature Values of DisplayFamily_DisplayModel".
659 */
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700660 switch (cpu_model) {
661 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700663 break;
664 case 3 ... 5:
665 *cpu_type = "i386/pii";
666 break;
667 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500668 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700669 *cpu_type = "i386/piii";
670 break;
671 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500672 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700673 *cpu_type = "i386/p6_mobile";
674 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700675 case 14:
676 *cpu_type = "i386/core";
677 break;
Patrick Simmonsc33f5432010-09-08 10:34:28 -0400678 case 0x0f:
679 case 0x16:
680 case 0x17:
Jiri Olsabb7ab782010-09-21 03:26:35 -0400681 case 0x1d:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700682 *cpu_type = "i386/core_2";
683 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400684 case 0x1a:
Josh Hunta7c55cb2010-08-04 20:27:05 -0400685 case 0x1e:
Andi Kleene83e4522010-01-21 23:26:27 +0100686 case 0x2e:
Robert Richter802070f2009-06-12 18:32:07 +0200687 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200688 *cpu_type = "i386/core_i7";
689 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400690 case 0x1c:
Andi Kleen6adf4062009-04-27 17:44:13 +0200691 *cpu_type = "i386/atom";
692 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700693 default:
694 /* Unknown */
695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Robert Richter802070f2009-06-12 18:32:07 +0200698 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 1;
700}
701
David Gibson96d08212005-09-06 15:17:26 -0700702int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 __u8 vendor = boot_cpu_data.x86_vendor;
705 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200706 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200707 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (!cpu_has_apic)
710 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100711
Robert Richter159a80b2011-10-11 19:39:16 +0200712 if (force_cpu_type == timer)
713 return -ENODEV;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100716 case X86_VENDOR_AMD:
717 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100719 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100720 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100721 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100723 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100724 /*
725 * Actually it could be i386/hammer too, but
726 * give user space an consistent name.
727 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100728 cpu_type = "x86-64/hammer";
729 break;
730 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100731 cpu_type = "x86-64/family10";
732 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200733 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200734 cpu_type = "x86-64/family11h";
735 break;
Robert Richter3acbf0842010-08-31 10:44:17 +0200736 case 0x12:
737 cpu_type = "x86-64/family12h";
738 break;
Robert Richtere6341472010-08-26 12:30:17 +0200739 case 0x14:
740 cpu_type = "x86-64/family14h";
741 break;
Robert Richter30570bce2010-08-31 10:44:38 +0200742 case 0x15:
743 cpu_type = "x86-64/family15h";
744 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100745 default:
746 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100747 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100748 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100751 case X86_VENDOR_INTEL:
752 switch (family) {
753 /* Pentium IV */
754 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200755 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100756 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100758 /* A P6-class processor */
759 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200760 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762
763 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200764 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100765 }
Andi Kleenb9917022008-08-18 14:50:31 +0200766
Robert Richtere4192942008-10-12 15:12:34 -0400767 if (cpu_type)
768 break;
769
770 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200771 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400772
773 /* use arch perfmon as fallback */
774 cpu_type = "i386/arch_perfmon";
775 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100776 break;
777
778 default:
779 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
Robert Richter270d3e12008-07-22 21:09:01 +0200782 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200783 ops->create_files = nmi_create_files;
784 ops->setup = nmi_setup;
785 ops->shutdown = nmi_shutdown;
786 ops->start = nmi_start;
787 ops->stop = nmi_stop;
788 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200789
Robert Richteradf5ec02008-07-22 21:08:48 +0200790 if (model->init)
791 ret = model->init(ops);
792 if (ret)
793 return ret;
794
Robert Richter52471c62009-07-06 14:43:55 +0200795 if (!model->num_virt_counters)
796 model->num_virt_counters = model->num_counters;
797
Robert Richter52805142009-07-09 16:02:44 +0200798 mux_init(ops);
799
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100800 init_suspend_resume();
Robert Richter10f04122010-08-30 10:56:18 +0200801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
803 return 0;
804}
805
David Gibson96d08212005-09-06 15:17:26 -0700806void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100808 exit_suspend_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}