blob: 8dace181c88e51a19a8b3fb767d9d28f46b0a2f5 [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;
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:
Robert Richterde654642010-05-03 14:41:22 +020068 if (ctr_running)
69 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
70 else if (!nmi_enabled)
71 break;
72 else
73 model->stop(&__get_cpu_var(cpu_msrs));
Mike Galbraith5b75af02009-02-04 17:11:34 +010074 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020075 break;
76 default:
77 break;
78 }
79 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Don Zickus2fbe7b22006-09-26 10:52:27 +020081
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010082static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010084 struct op_msr *counters = msrs->counters;
85 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned int i;
87
Robert Richter1a245c42009-06-05 15:54:24 +020088 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020089 if (counters[i].addr)
90 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010092
Robert Richter1a245c42009-06-05 15:54:24 +020093 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020094 if (controls[i].addr)
95 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97}
98
Robert Richterb28d1b92009-07-09 14:38:49 +020099static void nmi_cpu_start(void *dummy)
100{
101 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200102 if (!msrs->controls)
103 WARN_ON_ONCE(1);
104 else
105 model->start(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200106}
107
108static int nmi_start(void)
109{
Robert Richter6ae56b52010-04-29 14:55:55 +0200110 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200111 on_each_cpu(nmi_cpu_start, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200112 ctr_running = 1;
113 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200114 return 0;
115}
116
117static void nmi_cpu_stop(void *dummy)
118{
119 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200120 if (!msrs->controls)
121 WARN_ON_ONCE(1);
122 else
123 model->stop(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200124}
125
126static void nmi_stop(void)
127{
Robert Richter6ae56b52010-04-29 14:55:55 +0200128 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200129 on_each_cpu(nmi_cpu_stop, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200130 ctr_running = 0;
131 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200132}
133
Robert Richterd8471ad2009-07-16 13:04:43 +0200134#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
135
136static DEFINE_PER_CPU(int, switch_index);
137
Robert Richter39e97f42009-07-09 15:11:45 +0200138static inline int has_mux(void)
139{
140 return !!model->switch_ctrl;
141}
142
Robert Richterd8471ad2009-07-16 13:04:43 +0200143inline int op_x86_phys_to_virt(int phys)
144{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100145 return __this_cpu_read(switch_index) + phys;
Robert Richterd8471ad2009-07-16 13:04:43 +0200146}
147
Robert Richter61d149d2009-07-10 15:47:17 +0200148inline int op_x86_virt_to_phys(int virt)
149{
150 return virt % model->num_counters;
151}
152
Robert Richter6ab82f92009-07-09 14:40:04 +0200153static void nmi_shutdown_mux(void)
154{
155 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200156
157 if (!has_mux())
158 return;
159
Robert Richter6ab82f92009-07-09 14:40:04 +0200160 for_each_possible_cpu(i) {
161 kfree(per_cpu(cpu_msrs, i).multiplex);
162 per_cpu(cpu_msrs, i).multiplex = NULL;
163 per_cpu(switch_index, i) = 0;
164 }
165}
166
167static int nmi_setup_mux(void)
168{
169 size_t multiplex_size =
170 sizeof(struct op_msr) * model->num_virt_counters;
171 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200172
173 if (!has_mux())
174 return 1;
175
Robert Richter6ab82f92009-07-09 14:40:04 +0200176 for_each_possible_cpu(i) {
177 per_cpu(cpu_msrs, i).multiplex =
Robert Richterc17c8fb2010-02-25 20:20:25 +0100178 kzalloc(multiplex_size, GFP_KERNEL);
Robert Richter6ab82f92009-07-09 14:40:04 +0200179 if (!per_cpu(cpu_msrs, i).multiplex)
180 return 0;
181 }
Robert Richter39e97f42009-07-09 15:11:45 +0200182
Robert Richter6ab82f92009-07-09 14:40:04 +0200183 return 1;
184}
185
Robert Richter48fb4b42009-07-09 14:38:49 +0200186static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
187{
188 int i;
189 struct op_msr *multiplex = msrs->multiplex;
190
Robert Richter39e97f42009-07-09 15:11:45 +0200191 if (!has_mux())
192 return;
193
Robert Richter48fb4b42009-07-09 14:38:49 +0200194 for (i = 0; i < model->num_virt_counters; ++i) {
195 if (counter_config[i].enabled) {
196 multiplex[i].saved = -(u64)counter_config[i].count;
197 } else {
Robert Richter48fb4b42009-07-09 14:38:49 +0200198 multiplex[i].saved = 0;
199 }
200 }
201
202 per_cpu(switch_index, cpu) = 0;
203}
204
Robert Richterd0f585d2009-07-09 14:38:49 +0200205static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
206{
Robert Richter68dc8192010-02-25 19:16:46 +0100207 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200208 struct op_msr *multiplex = msrs->multiplex;
209 int i;
210
211 for (i = 0; i < model->num_counters; ++i) {
212 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100213 if (counters[i].addr)
214 rdmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200215 }
216}
217
218static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
219{
Robert Richter68dc8192010-02-25 19:16:46 +0100220 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200221 struct op_msr *multiplex = msrs->multiplex;
222 int i;
223
224 for (i = 0; i < model->num_counters; ++i) {
225 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100226 if (counters[i].addr)
227 wrmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200228 }
229}
230
Robert Richterb28d1b92009-07-09 14:38:49 +0200231static void nmi_cpu_switch(void *dummy)
232{
233 int cpu = smp_processor_id();
234 int si = per_cpu(switch_index, cpu);
235 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
236
237 nmi_cpu_stop(NULL);
238 nmi_cpu_save_mpx_registers(msrs);
239
240 /* move to next set */
241 si += model->num_counters;
Suravee Suthikulpanitd8cc1082010-01-18 11:25:36 -0600242 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
Robert Richterb28d1b92009-07-09 14:38:49 +0200243 per_cpu(switch_index, cpu) = 0;
244 else
245 per_cpu(switch_index, cpu) = si;
246
247 model->switch_ctrl(model, msrs);
248 nmi_cpu_restore_mpx_registers(msrs);
249
250 nmi_cpu_start(NULL);
251}
252
253
254/*
255 * Quick check to see if multiplexing is necessary.
256 * The check should be sufficient since counters are used
257 * in ordre.
258 */
259static int nmi_multiplex_on(void)
260{
261 return counter_config[model->num_counters].count ? 0 : -EINVAL;
262}
263
264static int nmi_switch_event(void)
265{
Robert Richter39e97f42009-07-09 15:11:45 +0200266 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200267 return -ENOSYS; /* not implemented */
268 if (nmi_multiplex_on() < 0)
269 return -EINVAL; /* not necessary */
270
Robert Richter6ae56b52010-04-29 14:55:55 +0200271 get_online_cpus();
272 if (ctr_running)
273 on_each_cpu(nmi_cpu_switch, NULL, 1);
274 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200275
Robert Richterb28d1b92009-07-09 14:38:49 +0200276 return 0;
277}
278
Robert Richter52805142009-07-09 16:02:44 +0200279static inline void mux_init(struct oprofile_operations *ops)
280{
281 if (has_mux())
282 ops->switch_events = nmi_switch_event;
283}
284
Robert Richter4d015f72009-07-09 21:42:51 +0200285static void mux_clone(int cpu)
286{
287 if (!has_mux())
288 return;
289
290 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
291 per_cpu(cpu_msrs, 0).multiplex,
292 sizeof(struct op_msr) * model->num_virt_counters);
293}
294
Robert Richterd8471ad2009-07-16 13:04:43 +0200295#else
296
297inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200298inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200299static inline void nmi_shutdown_mux(void) { }
300static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200301static inline void
302nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200303static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200304static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200305
306#endif
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static void free_msrs(void)
309{
310 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800311 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700312 kfree(per_cpu(cpu_msrs, i).counters);
313 per_cpu(cpu_msrs, i).counters = NULL;
314 kfree(per_cpu(cpu_msrs, i).controls);
315 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100317 nmi_shutdown_mux();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static int allocate_msrs(void)
321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
323 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
324
Robert Richter4c168ea2008-09-24 11:08:52 +0200325 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700326 for_each_possible_cpu(i) {
Robert Richterc17c8fb2010-02-25 20:20:25 +0100327 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200328 GFP_KERNEL);
329 if (!per_cpu(cpu_msrs, i).counters)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100330 goto fail;
Robert Richterc17c8fb2010-02-25 20:20:25 +0100331 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200332 GFP_KERNEL);
333 if (!per_cpu(cpu_msrs, i).controls)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100334 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100337 if (!nmi_setup_mux())
338 goto fail;
339
Robert Richter6ab82f92009-07-09 14:40:04 +0200340 return 1;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100341
342fail:
343 free_msrs();
344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100347static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700350 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200351 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200353 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200354 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700356 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 apic_write(APIC_LVTPC, APIC_DM_NMI);
358}
359
Don Zickus2fbe7b22006-09-26 10:52:27 +0200360static struct notifier_block profile_exceptions_nb = {
361 .notifier_call = profile_exceptions_notify,
362 .next = NULL,
Don Zickus166d7512011-01-06 16:18:49 -0500363 .priority = NMI_LOCAL_LOW_PRIOR,
Don Zickus2fbe7b22006-09-26 10:52:27 +0200364};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Robert Richter44ab9a62009-07-09 18:33:02 +0200366static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100368 struct op_msr *counters = msrs->counters;
369 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int i;
371
Robert Richter1a245c42009-06-05 15:54:24 +0200372 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200373 if (controls[i].addr)
374 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100376
Robert Richter1a245c42009-06-05 15:54:24 +0200377 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200378 if (counters[i].addr)
379 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100383static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 unsigned int v;
386 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200387 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* restoring APIC_LVTPC can trigger an apic error because the delivery
390 * mode and vector nr combination can be illegal. That's by design: on
391 * power on apic lvt contain a zero vector nr which are legal only for
392 * NMI delivery mode. So inhibit apic err before restoring lvtpc
393 */
394 v = apic_read(APIC_LVTERR);
395 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700396 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200398 nmi_cpu_restore_registers(msrs);
Robert Richterbae663b2010-05-05 17:47:17 +0200399 if (model->cpu_down)
400 model->cpu_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Robert Richter6ae56b52010-04-29 14:55:55 +0200403static void nmi_cpu_up(void *dummy)
404{
405 if (nmi_enabled)
406 nmi_cpu_setup(dummy);
407 if (ctr_running)
408 nmi_cpu_start(dummy);
409}
410
411static void nmi_cpu_down(void *dummy)
412{
413 if (ctr_running)
414 nmi_cpu_stop(dummy);
415 if (nmi_enabled)
416 nmi_cpu_shutdown(dummy);
417}
418
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100419static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 unsigned int i;
422
Jason Yeh4d4036e2009-07-08 13:49:38 +0200423 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100424 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700425 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100426
427 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200428 * available for use. This should protect userspace app.
429 * NOTE: assumes 1:1 mapping here (that counters are organized
430 * sequentially in their struct assignment).
431 */
Robert Richter11be1a72009-07-10 18:15:21 +0200432 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200433 continue;
434
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700435 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100437 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
438 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
439 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
440 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
441 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
442 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 return 0;
446}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100447
Robert Richter69046d42008-09-05 12:17:40 +0200448static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
449 void *data)
450{
451 int cpu = (unsigned long)data;
452 switch (action) {
453 case CPU_DOWN_FAILED:
454 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200455 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200456 break;
457 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200458 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200459 break;
460 }
461 return NOTIFY_DONE;
462}
463
464static struct notifier_block oprofile_cpu_nb = {
465 .notifier_call = oprofile_cpu_notifier
466};
Robert Richter69046d42008-09-05 12:17:40 +0200467
Robert Richterd30d64c2010-05-03 15:52:26 +0200468static int nmi_setup(void)
469{
470 int err = 0;
471 int cpu;
472
473 if (!allocate_msrs())
474 return -ENOMEM;
475
476 /* We need to serialize save and setup for HT because the subset
477 * of msrs are distinct for save and setup operations
478 */
479
480 /* Assume saved/restored counters are the same on all CPUs */
481 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
482 if (err)
483 goto fail;
484
485 for_each_possible_cpu(cpu) {
486 if (!cpu)
487 continue;
488
489 memcpy(per_cpu(cpu_msrs, cpu).counters,
490 per_cpu(cpu_msrs, 0).counters,
491 sizeof(struct op_msr) * model->num_counters);
492
493 memcpy(per_cpu(cpu_msrs, cpu).controls,
494 per_cpu(cpu_msrs, 0).controls,
495 sizeof(struct op_msr) * model->num_controls);
496
497 mux_clone(cpu);
498 }
499
500 nmi_enabled = 0;
501 ctr_running = 0;
502 barrier();
503 err = register_die_notifier(&profile_exceptions_nb);
504 if (err)
505 goto fail;
506
507 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200508 register_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200509 on_each_cpu(nmi_cpu_setup, NULL, 1);
510 nmi_enabled = 1;
511 put_online_cpus();
512
513 return 0;
514fail:
515 free_msrs();
516 return err;
517}
518
519static void nmi_shutdown(void)
520{
521 struct op_msrs *msrs;
522
523 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200524 unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200525 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
526 nmi_enabled = 0;
527 ctr_running = 0;
528 put_online_cpus();
529 barrier();
530 unregister_die_notifier(&profile_exceptions_nb);
531 msrs = &get_cpu_var(cpu_msrs);
532 model->shutdown(msrs);
533 free_msrs();
534 put_cpu_var(cpu_msrs);
535}
536
Robert Richter69046d42008-09-05 12:17:40 +0200537#ifdef CONFIG_PM
538
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100539static int nmi_suspend(void)
Robert Richter69046d42008-09-05 12:17:40 +0200540{
541 /* Only one CPU left, just stop that one */
542 if (nmi_enabled == 1)
543 nmi_cpu_stop(NULL);
544 return 0;
545}
546
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100547static void nmi_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200548{
549 if (nmi_enabled == 1)
550 nmi_cpu_start(NULL);
Robert Richter69046d42008-09-05 12:17:40 +0200551}
552
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100553static struct syscore_ops oprofile_syscore_ops = {
Robert Richter69046d42008-09-05 12:17:40 +0200554 .resume = nmi_resume,
555 .suspend = nmi_suspend,
556};
557
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100558static void __init init_suspend_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200559{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100560 register_syscore_ops(&oprofile_syscore_ops);
Robert Richter69046d42008-09-05 12:17:40 +0200561}
562
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100563static void exit_suspend_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200564{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100565 unregister_syscore_ops(&oprofile_syscore_ops);
Robert Richter69046d42008-09-05 12:17:40 +0200566}
567
568#else
Robert Richter269f45c2010-09-01 14:50:50 +0200569
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100570static inline void init_suspend_resume(void) { }
571static inline void exit_suspend_resume(void) { }
Robert Richter269f45c2010-09-01 14:50:50 +0200572
Robert Richter69046d42008-09-05 12:17:40 +0200573#endif /* CONFIG_PM */
574
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100575static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 __u8 cpu_model = boot_cpu_data.x86_model;
578
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200579 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581
582#ifndef CONFIG_SMP
583 *cpu_type = "i386/p4";
584 model = &op_p4_spec;
585 return 1;
586#else
587 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100588 case 1:
589 *cpu_type = "i386/p4";
590 model = &op_p4_spec;
591 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100593 case 2:
594 *cpu_type = "i386/p4-ht";
595 model = &op_p4_ht2_spec;
596 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598#endif
599
600 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
601 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
602 return 0;
603}
604
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200605static int force_arch_perfmon;
606static int force_cpu_type(const char *str, struct kernel_param *kp)
607{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200608 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200609 force_arch_perfmon = 1;
610 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
611 }
612
613 return 0;
614}
615module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200616
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100617static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200620 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200622 if (force_arch_perfmon && cpu_has_arch_perfmon)
623 return 0;
624
John Villalovos45c34e02010-05-07 12:41:40 -0400625 /*
626 * Documentation on identifying Intel processors by CPU family
627 * and model can be found in the Intel Software Developer's
628 * Manuals (SDM):
629 *
630 * http://www.intel.com/products/processor/manuals/
631 *
632 * As of May 2010 the documentation for this was in the:
633 * "Intel 64 and IA-32 Architectures Software Developer's
634 * Manual Volume 3B: System Programming Guide", "Table B-1
635 * CPUID Signature Values of DisplayFamily_DisplayModel".
636 */
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700637 switch (cpu_model) {
638 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700640 break;
641 case 3 ... 5:
642 *cpu_type = "i386/pii";
643 break;
644 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500645 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700646 *cpu_type = "i386/piii";
647 break;
648 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500649 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700650 *cpu_type = "i386/p6_mobile";
651 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700652 case 14:
653 *cpu_type = "i386/core";
654 break;
Patrick Simmonsc33f5432010-09-08 10:34:28 -0400655 case 0x0f:
656 case 0x16:
657 case 0x17:
Jiri Olsabb7ab782010-09-21 03:26:35 -0400658 case 0x1d:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700659 *cpu_type = "i386/core_2";
660 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400661 case 0x1a:
Josh Hunta7c55cb2010-08-04 20:27:05 -0400662 case 0x1e:
Andi Kleene83e4522010-01-21 23:26:27 +0100663 case 0x2e:
Robert Richter802070f2009-06-12 18:32:07 +0200664 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200665 *cpu_type = "i386/core_i7";
666 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400667 case 0x1c:
Andi Kleen6adf4062009-04-27 17:44:13 +0200668 *cpu_type = "i386/atom";
669 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700670 default:
671 /* Unknown */
672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Robert Richter802070f2009-06-12 18:32:07 +0200675 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 1;
677}
678
David Gibson96d08212005-09-06 15:17:26 -0700679int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 __u8 vendor = boot_cpu_data.x86_vendor;
682 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200683 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200684 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (!cpu_has_apic)
687 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100690 case X86_VENDOR_AMD:
691 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100693 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100694 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100695 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100697 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100698 /*
699 * Actually it could be i386/hammer too, but
700 * give user space an consistent name.
701 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100702 cpu_type = "x86-64/hammer";
703 break;
704 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100705 cpu_type = "x86-64/family10";
706 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200707 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200708 cpu_type = "x86-64/family11h";
709 break;
Robert Richter3acbf0842010-08-31 10:44:17 +0200710 case 0x12:
711 cpu_type = "x86-64/family12h";
712 break;
Robert Richtere6341472010-08-26 12:30:17 +0200713 case 0x14:
714 cpu_type = "x86-64/family14h";
715 break;
Robert Richter30570bce2010-08-31 10:44:38 +0200716 case 0x15:
717 cpu_type = "x86-64/family15h";
718 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100719 default:
720 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100721 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100722 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100725 case X86_VENDOR_INTEL:
726 switch (family) {
727 /* Pentium IV */
728 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200729 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100730 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100732 /* A P6-class processor */
733 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200734 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736
737 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200738 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100739 }
Andi Kleenb9917022008-08-18 14:50:31 +0200740
Robert Richtere4192942008-10-12 15:12:34 -0400741 if (cpu_type)
742 break;
743
744 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200745 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400746
747 /* use arch perfmon as fallback */
748 cpu_type = "i386/arch_perfmon";
749 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100750 break;
751
752 default:
753 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Robert Richter270d3e12008-07-22 21:09:01 +0200756 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200757 ops->create_files = nmi_create_files;
758 ops->setup = nmi_setup;
759 ops->shutdown = nmi_shutdown;
760 ops->start = nmi_start;
761 ops->stop = nmi_stop;
762 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200763
Robert Richteradf5ec02008-07-22 21:08:48 +0200764 if (model->init)
765 ret = model->init(ops);
766 if (ret)
767 return ret;
768
Robert Richter52471c62009-07-06 14:43:55 +0200769 if (!model->num_virt_counters)
770 model->num_virt_counters = model->num_counters;
771
Robert Richter52805142009-07-09 16:02:44 +0200772 mux_init(ops);
773
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100774 init_suspend_resume();
Robert Richter10f04122010-08-30 10:56:18 +0200775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
777 return 0;
778}
779
David Gibson96d08212005-09-06 15:17:26 -0700780void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100782 exit_suspend_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}