blob: 7de0572b0a5ef2875781d7f8d395784695d0348b [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
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;
52 event &= model->event_mask ? model->event_mask : 0xFF;
53 val |= event & 0xFF;
54 val |= (event & 0x0F00) << 24;
55
56 return val;
57}
58
59
Adrian Bunkc7c19f82006-09-26 10:52:27 +020060static int profile_exceptions_notify(struct notifier_block *self,
61 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Don Zickus2fbe7b22006-09-26 10:52:27 +020063 struct die_args *args = (struct die_args *)data;
64 int ret = NOTIFY_DONE;
Don Zickus2fbe7b22006-09-26 10:52:27 +020065
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:
Robert Richterde654642010-05-03 14:41:22 +020069 if (ctr_running)
70 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
71 else if (!nmi_enabled)
72 break;
73 else
74 model->stop(&__get_cpu_var(cpu_msrs));
Mike Galbraith5b75af02009-02-04 17:11:34 +010075 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 Richterb28d1b92009-07-09 14:38:49 +0200100static void nmi_cpu_start(void *dummy)
101{
102 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200103 if (!msrs->controls)
104 WARN_ON_ONCE(1);
105 else
106 model->start(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200107}
108
109static int nmi_start(void)
110{
Robert Richter6ae56b52010-04-29 14:55:55 +0200111 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200112 on_each_cpu(nmi_cpu_start, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200113 ctr_running = 1;
114 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200115 return 0;
116}
117
118static void nmi_cpu_stop(void *dummy)
119{
120 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200121 if (!msrs->controls)
122 WARN_ON_ONCE(1);
123 else
124 model->stop(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200125}
126
127static void nmi_stop(void)
128{
Robert Richter6ae56b52010-04-29 14:55:55 +0200129 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200130 on_each_cpu(nmi_cpu_stop, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200131 ctr_running = 0;
132 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200133}
134
Robert Richterd8471ad2009-07-16 13:04:43 +0200135#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
136
137static DEFINE_PER_CPU(int, switch_index);
138
Robert Richter39e97f42009-07-09 15:11:45 +0200139static inline int has_mux(void)
140{
141 return !!model->switch_ctrl;
142}
143
Robert Richterd8471ad2009-07-16 13:04:43 +0200144inline int op_x86_phys_to_virt(int phys)
145{
146 return __get_cpu_var(switch_index) + phys;
147}
148
Robert Richter61d149d2009-07-10 15:47:17 +0200149inline int op_x86_virt_to_phys(int virt)
150{
151 return virt % model->num_counters;
152}
153
Robert Richter6ab82f92009-07-09 14:40:04 +0200154static void nmi_shutdown_mux(void)
155{
156 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200157
158 if (!has_mux())
159 return;
160
Robert Richter6ab82f92009-07-09 14:40:04 +0200161 for_each_possible_cpu(i) {
162 kfree(per_cpu(cpu_msrs, i).multiplex);
163 per_cpu(cpu_msrs, i).multiplex = NULL;
164 per_cpu(switch_index, i) = 0;
165 }
166}
167
168static int nmi_setup_mux(void)
169{
170 size_t multiplex_size =
171 sizeof(struct op_msr) * model->num_virt_counters;
172 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200173
174 if (!has_mux())
175 return 1;
176
Robert Richter6ab82f92009-07-09 14:40:04 +0200177 for_each_possible_cpu(i) {
178 per_cpu(cpu_msrs, i).multiplex =
Robert Richterc17c8fb2010-02-25 20:20:25 +0100179 kzalloc(multiplex_size, GFP_KERNEL);
Robert Richter6ab82f92009-07-09 14:40:04 +0200180 if (!per_cpu(cpu_msrs, i).multiplex)
181 return 0;
182 }
Robert Richter39e97f42009-07-09 15:11:45 +0200183
Robert Richter6ab82f92009-07-09 14:40:04 +0200184 return 1;
185}
186
Robert Richter48fb4b42009-07-09 14:38:49 +0200187static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
188{
189 int i;
190 struct op_msr *multiplex = msrs->multiplex;
191
Robert Richter39e97f42009-07-09 15:11:45 +0200192 if (!has_mux())
193 return;
194
Robert Richter48fb4b42009-07-09 14:38:49 +0200195 for (i = 0; i < model->num_virt_counters; ++i) {
196 if (counter_config[i].enabled) {
197 multiplex[i].saved = -(u64)counter_config[i].count;
198 } else {
Robert Richter48fb4b42009-07-09 14:38:49 +0200199 multiplex[i].saved = 0;
200 }
201 }
202
203 per_cpu(switch_index, cpu) = 0;
204}
205
Robert Richterd0f585d2009-07-09 14:38:49 +0200206static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
207{
Robert Richter68dc8192010-02-25 19:16:46 +0100208 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200209 struct op_msr *multiplex = msrs->multiplex;
210 int i;
211
212 for (i = 0; i < model->num_counters; ++i) {
213 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100214 if (counters[i].addr)
215 rdmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200216 }
217}
218
219static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
220{
Robert Richter68dc8192010-02-25 19:16:46 +0100221 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200222 struct op_msr *multiplex = msrs->multiplex;
223 int i;
224
225 for (i = 0; i < model->num_counters; ++i) {
226 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100227 if (counters[i].addr)
228 wrmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200229 }
230}
231
Robert Richterb28d1b92009-07-09 14:38:49 +0200232static void nmi_cpu_switch(void *dummy)
233{
234 int cpu = smp_processor_id();
235 int si = per_cpu(switch_index, cpu);
236 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
237
238 nmi_cpu_stop(NULL);
239 nmi_cpu_save_mpx_registers(msrs);
240
241 /* move to next set */
242 si += model->num_counters;
Suravee Suthikulpanitd8cc1082010-01-18 11:25:36 -0600243 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
Robert Richterb28d1b92009-07-09 14:38:49 +0200244 per_cpu(switch_index, cpu) = 0;
245 else
246 per_cpu(switch_index, cpu) = si;
247
248 model->switch_ctrl(model, msrs);
249 nmi_cpu_restore_mpx_registers(msrs);
250
251 nmi_cpu_start(NULL);
252}
253
254
255/*
256 * Quick check to see if multiplexing is necessary.
257 * The check should be sufficient since counters are used
258 * in ordre.
259 */
260static int nmi_multiplex_on(void)
261{
262 return counter_config[model->num_counters].count ? 0 : -EINVAL;
263}
264
265static int nmi_switch_event(void)
266{
Robert Richter39e97f42009-07-09 15:11:45 +0200267 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200268 return -ENOSYS; /* not implemented */
269 if (nmi_multiplex_on() < 0)
270 return -EINVAL; /* not necessary */
271
Robert Richter6ae56b52010-04-29 14:55:55 +0200272 get_online_cpus();
273 if (ctr_running)
274 on_each_cpu(nmi_cpu_switch, NULL, 1);
275 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200276
Robert Richterb28d1b92009-07-09 14:38:49 +0200277 return 0;
278}
279
Robert Richter52805142009-07-09 16:02:44 +0200280static inline void mux_init(struct oprofile_operations *ops)
281{
282 if (has_mux())
283 ops->switch_events = nmi_switch_event;
284}
285
Robert Richter4d015f72009-07-09 21:42:51 +0200286static void mux_clone(int cpu)
287{
288 if (!has_mux())
289 return;
290
291 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
292 per_cpu(cpu_msrs, 0).multiplex,
293 sizeof(struct op_msr) * model->num_virt_counters);
294}
295
Robert Richterd8471ad2009-07-16 13:04:43 +0200296#else
297
298inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200299inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200300static inline void nmi_shutdown_mux(void) { }
301static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200302static inline void
303nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200304static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200305static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200306
307#endif
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static void free_msrs(void)
310{
311 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800312 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700313 kfree(per_cpu(cpu_msrs, i).counters);
314 per_cpu(cpu_msrs, i).counters = NULL;
315 kfree(per_cpu(cpu_msrs, i).controls);
316 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100318 nmi_shutdown_mux();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static int allocate_msrs(void)
322{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
324 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
325
Robert Richter4c168ea2008-09-24 11:08:52 +0200326 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700327 for_each_possible_cpu(i) {
Robert Richterc17c8fb2010-02-25 20:20:25 +0100328 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200329 GFP_KERNEL);
330 if (!per_cpu(cpu_msrs, i).counters)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100331 goto fail;
Robert Richterc17c8fb2010-02-25 20:20:25 +0100332 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200333 GFP_KERNEL);
334 if (!per_cpu(cpu_msrs, i).controls)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100335 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100338 if (!nmi_setup_mux())
339 goto fail;
340
Robert Richter6ab82f92009-07-09 14:40:04 +0200341 return 1;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100342
343fail:
344 free_msrs();
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100348static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700351 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200352 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200354 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200355 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700357 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 apic_write(APIC_LVTPC, APIC_DM_NMI);
359}
360
Don Zickus2fbe7b22006-09-26 10:52:27 +0200361static struct notifier_block profile_exceptions_nb = {
362 .notifier_call = profile_exceptions_notify,
363 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100364 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200365};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Robert Richter44ab9a62009-07-09 18:33:02 +0200367static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100369 struct op_msr *counters = msrs->counters;
370 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned int i;
372
Robert Richter1a245c42009-06-05 15:54:24 +0200373 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200374 if (controls[i].addr)
375 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100377
Robert Richter1a245c42009-06-05 15:54:24 +0200378 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200379 if (counters[i].addr)
380 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100384static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 unsigned int v;
387 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200388 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* restoring APIC_LVTPC can trigger an apic error because the delivery
391 * mode and vector nr combination can be illegal. That's by design: on
392 * power on apic lvt contain a zero vector nr which are legal only for
393 * NMI delivery mode. So inhibit apic err before restoring lvtpc
394 */
395 v = apic_read(APIC_LVTERR);
396 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700397 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200399 nmi_cpu_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Robert Richter6ae56b52010-04-29 14:55:55 +0200402static void nmi_cpu_up(void *dummy)
403{
404 if (nmi_enabled)
405 nmi_cpu_setup(dummy);
406 if (ctr_running)
407 nmi_cpu_start(dummy);
408}
409
410static void nmi_cpu_down(void *dummy)
411{
412 if (ctr_running)
413 nmi_cpu_stop(dummy);
414 if (nmi_enabled)
415 nmi_cpu_shutdown(dummy);
416}
417
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100418static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 unsigned int i;
421
Jason Yeh4d4036e2009-07-08 13:49:38 +0200422 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100423 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700424 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100425
426 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200427 * available for use. This should protect userspace app.
428 * NOTE: assumes 1:1 mapping here (that counters are organized
429 * sequentially in their struct assignment).
430 */
Robert Richter11be1a72009-07-10 18:15:21 +0200431 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200432 continue;
433
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700434 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100436 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
437 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
438 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
439 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
440 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
441 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
444 return 0;
445}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100446
Robert Richter69046d42008-09-05 12:17:40 +0200447static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
448 void *data)
449{
450 int cpu = (unsigned long)data;
451 switch (action) {
452 case CPU_DOWN_FAILED:
453 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200454 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200455 break;
456 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200457 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200458 break;
459 }
460 return NOTIFY_DONE;
461}
462
463static struct notifier_block oprofile_cpu_nb = {
464 .notifier_call = oprofile_cpu_notifier
465};
Robert Richter69046d42008-09-05 12:17:40 +0200466
Robert Richterd30d64c2010-05-03 15:52:26 +0200467static int nmi_setup(void)
468{
469 int err = 0;
470 int cpu;
471
472 if (!allocate_msrs())
473 return -ENOMEM;
474
475 /* We need to serialize save and setup for HT because the subset
476 * of msrs are distinct for save and setup operations
477 */
478
479 /* Assume saved/restored counters are the same on all CPUs */
480 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
481 if (err)
482 goto fail;
483
484 for_each_possible_cpu(cpu) {
485 if (!cpu)
486 continue;
487
488 memcpy(per_cpu(cpu_msrs, cpu).counters,
489 per_cpu(cpu_msrs, 0).counters,
490 sizeof(struct op_msr) * model->num_counters);
491
492 memcpy(per_cpu(cpu_msrs, cpu).controls,
493 per_cpu(cpu_msrs, 0).controls,
494 sizeof(struct op_msr) * model->num_controls);
495
496 mux_clone(cpu);
497 }
498
499 nmi_enabled = 0;
500 ctr_running = 0;
501 barrier();
502 err = register_die_notifier(&profile_exceptions_nb);
503 if (err)
504 goto fail;
505
506 get_online_cpus();
507 on_each_cpu(nmi_cpu_setup, NULL, 1);
508 nmi_enabled = 1;
509 put_online_cpus();
510
511 return 0;
512fail:
513 free_msrs();
514 return err;
515}
516
517static void nmi_shutdown(void)
518{
519 struct op_msrs *msrs;
520
521 get_online_cpus();
522 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
523 nmi_enabled = 0;
524 ctr_running = 0;
525 put_online_cpus();
526 barrier();
527 unregister_die_notifier(&profile_exceptions_nb);
528 msrs = &get_cpu_var(cpu_msrs);
529 model->shutdown(msrs);
530 free_msrs();
531 put_cpu_var(cpu_msrs);
532}
533
Robert Richter69046d42008-09-05 12:17:40 +0200534#ifdef CONFIG_PM
535
536static int nmi_suspend(struct sys_device *dev, pm_message_t state)
537{
538 /* Only one CPU left, just stop that one */
539 if (nmi_enabled == 1)
540 nmi_cpu_stop(NULL);
541 return 0;
542}
543
544static int nmi_resume(struct sys_device *dev)
545{
546 if (nmi_enabled == 1)
547 nmi_cpu_start(NULL);
548 return 0;
549}
550
551static struct sysdev_class oprofile_sysclass = {
552 .name = "oprofile",
553 .resume = nmi_resume,
554 .suspend = nmi_suspend,
555};
556
557static struct sys_device device_oprofile = {
558 .id = 0,
559 .cls = &oprofile_sysclass,
560};
561
562static int __init init_sysfs(void)
563{
564 int error;
565
566 error = sysdev_class_register(&oprofile_sysclass);
567 if (!error)
568 error = sysdev_register(&device_oprofile);
569 return error;
570}
571
572static void exit_sysfs(void)
573{
574 sysdev_unregister(&device_oprofile);
575 sysdev_class_unregister(&oprofile_sysclass);
576}
577
578#else
579#define init_sysfs() do { } while (0)
580#define exit_sysfs() do { } while (0)
581#endif /* CONFIG_PM */
582
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100583static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 __u8 cpu_model = boot_cpu_data.x86_model;
586
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200587 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 0;
589
590#ifndef CONFIG_SMP
591 *cpu_type = "i386/p4";
592 model = &op_p4_spec;
593 return 1;
594#else
595 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100596 case 1:
597 *cpu_type = "i386/p4";
598 model = &op_p4_spec;
599 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100601 case 2:
602 *cpu_type = "i386/p4-ht";
603 model = &op_p4_ht2_spec;
604 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606#endif
607
608 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
609 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
610 return 0;
611}
612
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200613static int force_arch_perfmon;
614static int force_cpu_type(const char *str, struct kernel_param *kp)
615{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200616 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200617 force_arch_perfmon = 1;
618 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
619 }
620
621 return 0;
622}
623module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200624
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100625static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200628 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200630 if (force_arch_perfmon && cpu_has_arch_perfmon)
631 return 0;
632
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700633 switch (cpu_model) {
634 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700636 break;
637 case 3 ... 5:
638 *cpu_type = "i386/pii";
639 break;
640 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500641 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700642 *cpu_type = "i386/piii";
643 break;
644 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500645 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700646 *cpu_type = "i386/p6_mobile";
647 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700648 case 14:
649 *cpu_type = "i386/core";
650 break;
651 case 15: case 23:
652 *cpu_type = "i386/core_2";
653 break;
Andi Kleene83e4522010-01-21 23:26:27 +0100654 case 0x2e:
Andi Kleen6adf4062009-04-27 17:44:13 +0200655 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200656 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200657 *cpu_type = "i386/core_i7";
658 break;
659 case 28:
660 *cpu_type = "i386/atom";
661 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700662 default:
663 /* Unknown */
664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Robert Richter802070f2009-06-12 18:32:07 +0200667 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 1;
669}
670
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100671/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static int using_nmi;
673
David Gibson96d08212005-09-06 15:17:26 -0700674int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 __u8 vendor = boot_cpu_data.x86_vendor;
677 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200678 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200679 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!cpu_has_apic)
682 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100685 case X86_VENDOR_AMD:
686 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100688 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100689 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100690 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100692 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100693 /*
694 * Actually it could be i386/hammer too, but
695 * give user space an consistent name.
696 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100697 cpu_type = "x86-64/hammer";
698 break;
699 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100700 cpu_type = "x86-64/family10";
701 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200702 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200703 cpu_type = "x86-64/family11h";
704 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100705 default:
706 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100707 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100708 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100711 case X86_VENDOR_INTEL:
712 switch (family) {
713 /* Pentium IV */
714 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200715 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100718 /* A P6-class processor */
719 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200720 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722
723 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200724 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100725 }
Andi Kleenb9917022008-08-18 14:50:31 +0200726
Robert Richtere4192942008-10-12 15:12:34 -0400727 if (cpu_type)
728 break;
729
730 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200731 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400732
733 /* use arch perfmon as fallback */
734 cpu_type = "i386/arch_perfmon";
735 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100736 break;
737
738 default:
739 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
Robert Richter6ae56b52010-04-29 14:55:55 +0200742 get_online_cpus();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200743 register_cpu_notifier(&oprofile_cpu_nb);
Robert Richter6ae56b52010-04-29 14:55:55 +0200744 nmi_enabled = 0;
745 ctr_running = 0;
746 put_online_cpus();
Robert Richter216f3d92010-05-03 11:58:46 +0200747
Robert Richter270d3e12008-07-22 21:09:01 +0200748 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200749 ops->create_files = nmi_create_files;
750 ops->setup = nmi_setup;
751 ops->shutdown = nmi_shutdown;
752 ops->start = nmi_start;
753 ops->stop = nmi_stop;
754 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200755
Robert Richteradf5ec02008-07-22 21:08:48 +0200756 if (model->init)
757 ret = model->init(ops);
758 if (ret)
759 return ret;
760
Robert Richter52471c62009-07-06 14:43:55 +0200761 if (!model->num_virt_counters)
762 model->num_virt_counters = model->num_counters;
763
Robert Richter52805142009-07-09 16:02:44 +0200764 mux_init(ops);
765
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100766 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
769 return 0;
770}
771
David Gibson96d08212005-09-06 15:17:26 -0700772void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200774 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100775 exit_sysfs();
Robert Richter6ae56b52010-04-29 14:55:55 +0200776 get_online_cpus();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200777 unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richter6ae56b52010-04-29 14:55:55 +0200778 nmi_enabled = 0;
779 ctr_running = 0;
780 put_online_cpus();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200781 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200782 if (model->exit)
783 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}