blob: 358c8b9c96a79c725766e1627544486eb312a0bc [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);
Robert Richterbae663b2010-05-05 17:47:17 +0200400 if (model->cpu_down)
401 model->cpu_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Robert Richter6ae56b52010-04-29 14:55:55 +0200404static void nmi_cpu_up(void *dummy)
405{
406 if (nmi_enabled)
407 nmi_cpu_setup(dummy);
408 if (ctr_running)
409 nmi_cpu_start(dummy);
410}
411
412static void nmi_cpu_down(void *dummy)
413{
414 if (ctr_running)
415 nmi_cpu_stop(dummy);
416 if (nmi_enabled)
417 nmi_cpu_shutdown(dummy);
418}
419
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100420static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned int i;
423
Jason Yeh4d4036e2009-07-08 13:49:38 +0200424 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100425 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700426 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100427
428 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200429 * available for use. This should protect userspace app.
430 * NOTE: assumes 1:1 mapping here (that counters are organized
431 * sequentially in their struct assignment).
432 */
Robert Richter11be1a72009-07-10 18:15:21 +0200433 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200434 continue;
435
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700436 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100438 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
439 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
440 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
441 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
442 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
443 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 return 0;
447}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100448
Robert Richter69046d42008-09-05 12:17:40 +0200449static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
450 void *data)
451{
452 int cpu = (unsigned long)data;
453 switch (action) {
454 case CPU_DOWN_FAILED:
455 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200456 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200457 break;
458 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200459 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200460 break;
461 }
462 return NOTIFY_DONE;
463}
464
465static struct notifier_block oprofile_cpu_nb = {
466 .notifier_call = oprofile_cpu_notifier
467};
Robert Richter69046d42008-09-05 12:17:40 +0200468
Robert Richterd30d64c2010-05-03 15:52:26 +0200469static int nmi_setup(void)
470{
471 int err = 0;
472 int cpu;
473
474 if (!allocate_msrs())
475 return -ENOMEM;
476
477 /* We need to serialize save and setup for HT because the subset
478 * of msrs are distinct for save and setup operations
479 */
480
481 /* Assume saved/restored counters are the same on all CPUs */
482 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
483 if (err)
484 goto fail;
485
486 for_each_possible_cpu(cpu) {
487 if (!cpu)
488 continue;
489
490 memcpy(per_cpu(cpu_msrs, cpu).counters,
491 per_cpu(cpu_msrs, 0).counters,
492 sizeof(struct op_msr) * model->num_counters);
493
494 memcpy(per_cpu(cpu_msrs, cpu).controls,
495 per_cpu(cpu_msrs, 0).controls,
496 sizeof(struct op_msr) * model->num_controls);
497
498 mux_clone(cpu);
499 }
500
501 nmi_enabled = 0;
502 ctr_running = 0;
503 barrier();
504 err = register_die_notifier(&profile_exceptions_nb);
505 if (err)
506 goto fail;
507
508 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200509 register_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200510 on_each_cpu(nmi_cpu_setup, NULL, 1);
511 nmi_enabled = 1;
512 put_online_cpus();
513
514 return 0;
515fail:
516 free_msrs();
517 return err;
518}
519
520static void nmi_shutdown(void)
521{
522 struct op_msrs *msrs;
523
524 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200525 unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200526 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
527 nmi_enabled = 0;
528 ctr_running = 0;
529 put_online_cpus();
530 barrier();
531 unregister_die_notifier(&profile_exceptions_nb);
532 msrs = &get_cpu_var(cpu_msrs);
533 model->shutdown(msrs);
534 free_msrs();
535 put_cpu_var(cpu_msrs);
536}
537
Robert Richter69046d42008-09-05 12:17:40 +0200538#ifdef CONFIG_PM
539
540static int nmi_suspend(struct sys_device *dev, pm_message_t state)
541{
542 /* Only one CPU left, just stop that one */
543 if (nmi_enabled == 1)
544 nmi_cpu_stop(NULL);
545 return 0;
546}
547
548static int nmi_resume(struct sys_device *dev)
549{
550 if (nmi_enabled == 1)
551 nmi_cpu_start(NULL);
552 return 0;
553}
554
555static struct sysdev_class oprofile_sysclass = {
556 .name = "oprofile",
557 .resume = nmi_resume,
558 .suspend = nmi_suspend,
559};
560
561static struct sys_device device_oprofile = {
562 .id = 0,
563 .cls = &oprofile_sysclass,
564};
565
566static int __init init_sysfs(void)
567{
568 int error;
569
570 error = sysdev_class_register(&oprofile_sysclass);
Robert Richter10f04122010-08-30 10:56:18 +0200571 if (error)
572 return error;
573
574 error = sysdev_register(&device_oprofile);
575 if (error)
576 sysdev_class_unregister(&oprofile_sysclass);
577
Robert Richter69046d42008-09-05 12:17:40 +0200578 return error;
579}
580
581static void exit_sysfs(void)
582{
583 sysdev_unregister(&device_oprofile);
584 sysdev_class_unregister(&oprofile_sysclass);
585}
586
587#else
Robert Richter269f45c2010-09-01 14:50:50 +0200588
589static inline int init_sysfs(void) { return 0; }
590static inline void exit_sysfs(void) { }
591
Robert Richter69046d42008-09-05 12:17:40 +0200592#endif /* CONFIG_PM */
593
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100594static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 __u8 cpu_model = boot_cpu_data.x86_model;
597
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200598 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600
601#ifndef CONFIG_SMP
602 *cpu_type = "i386/p4";
603 model = &op_p4_spec;
604 return 1;
605#else
606 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100607 case 1:
608 *cpu_type = "i386/p4";
609 model = &op_p4_spec;
610 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100612 case 2:
613 *cpu_type = "i386/p4-ht";
614 model = &op_p4_ht2_spec;
615 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617#endif
618
619 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
620 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
621 return 0;
622}
623
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200624static int force_arch_perfmon;
625static int force_cpu_type(const char *str, struct kernel_param *kp)
626{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200627 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200628 force_arch_perfmon = 1;
629 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
630 }
631
632 return 0;
633}
634module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200635
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100636static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200639 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200641 if (force_arch_perfmon && cpu_has_arch_perfmon)
642 return 0;
643
John Villalovos45c34e02010-05-07 12:41:40 -0400644 /*
645 * Documentation on identifying Intel processors by CPU family
646 * and model can be found in the Intel Software Developer's
647 * Manuals (SDM):
648 *
649 * http://www.intel.com/products/processor/manuals/
650 *
651 * As of May 2010 the documentation for this was in the:
652 * "Intel 64 and IA-32 Architectures Software Developer's
653 * Manual Volume 3B: System Programming Guide", "Table B-1
654 * CPUID Signature Values of DisplayFamily_DisplayModel".
655 */
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700656 switch (cpu_model) {
657 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700659 break;
660 case 3 ... 5:
661 *cpu_type = "i386/pii";
662 break;
663 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500664 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700665 *cpu_type = "i386/piii";
666 break;
667 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500668 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700669 *cpu_type = "i386/p6_mobile";
670 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700671 case 14:
672 *cpu_type = "i386/core";
673 break;
Patrick Simmonsc33f5432010-09-08 10:34:28 -0400674 case 0x0f:
675 case 0x16:
676 case 0x17:
Jiri Olsabb7ab782010-09-21 03:26:35 -0400677 case 0x1d:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700678 *cpu_type = "i386/core_2";
679 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400680 case 0x1a:
Josh Hunta7c55cb2010-08-04 20:27:05 -0400681 case 0x1e:
Andi Kleene83e4522010-01-21 23:26:27 +0100682 case 0x2e:
Robert Richter802070f2009-06-12 18:32:07 +0200683 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200684 *cpu_type = "i386/core_i7";
685 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400686 case 0x1c:
Andi Kleen6adf4062009-04-27 17:44:13 +0200687 *cpu_type = "i386/atom";
688 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700689 default:
690 /* Unknown */
691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Robert Richter802070f2009-06-12 18:32:07 +0200694 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 1;
696}
697
David Gibson96d08212005-09-06 15:17:26 -0700698int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 __u8 vendor = boot_cpu_data.x86_vendor;
701 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200702 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200703 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (!cpu_has_apic)
706 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100709 case X86_VENDOR_AMD:
710 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100712 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100713 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100714 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100716 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100717 /*
718 * Actually it could be i386/hammer too, but
719 * give user space an consistent name.
720 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100721 cpu_type = "x86-64/hammer";
722 break;
723 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100724 cpu_type = "x86-64/family10";
725 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200726 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200727 cpu_type = "x86-64/family11h";
728 break;
Robert Richter3acbf0842010-08-31 10:44:17 +0200729 case 0x12:
730 cpu_type = "x86-64/family12h";
731 break;
Robert Richtere6341472010-08-26 12:30:17 +0200732 case 0x14:
733 cpu_type = "x86-64/family14h";
734 break;
Robert Richter30570bc2010-08-31 10:44:38 +0200735 case 0x15:
736 cpu_type = "x86-64/family15h";
737 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100738 default:
739 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100740 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100741 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100744 case X86_VENDOR_INTEL:
745 switch (family) {
746 /* Pentium IV */
747 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200748 p4_init(&cpu_type);
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 /* A P6-class processor */
752 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200753 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755
756 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200757 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100758 }
Andi Kleenb9917022008-08-18 14:50:31 +0200759
Robert Richtere4192942008-10-12 15:12:34 -0400760 if (cpu_type)
761 break;
762
763 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200764 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400765
766 /* use arch perfmon as fallback */
767 cpu_type = "i386/arch_perfmon";
768 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100769 break;
770
771 default:
772 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Robert Richter270d3e12008-07-22 21:09:01 +0200775 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200776 ops->create_files = nmi_create_files;
777 ops->setup = nmi_setup;
778 ops->shutdown = nmi_shutdown;
779 ops->start = nmi_start;
780 ops->stop = nmi_stop;
781 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200782
Robert Richteradf5ec02008-07-22 21:08:48 +0200783 if (model->init)
784 ret = model->init(ops);
785 if (ret)
786 return ret;
787
Robert Richter52471c62009-07-06 14:43:55 +0200788 if (!model->num_virt_counters)
789 model->num_virt_counters = model->num_counters;
790
Robert Richter52805142009-07-09 16:02:44 +0200791 mux_init(ops);
792
Robert Richter10f04122010-08-30 10:56:18 +0200793 ret = init_sysfs();
794 if (ret)
795 return ret;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
798 return 0;
799}
800
David Gibson96d08212005-09-06 15:17:26 -0700801void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Robert Richter51404342010-09-30 18:55:47 +0200803 exit_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}