blob: e77ea0b566e0fea6671e25c333ecd679438197a7 [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:
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{
145 return __get_cpu_var(switch_index) + phys;
146}
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 Hiroyukic8912592006-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
539static int nmi_suspend(struct sys_device *dev, pm_message_t state)
540{
541 /* Only one CPU left, just stop that one */
542 if (nmi_enabled == 1)
543 nmi_cpu_stop(NULL);
544 return 0;
545}
546
547static int nmi_resume(struct sys_device *dev)
548{
549 if (nmi_enabled == 1)
550 nmi_cpu_start(NULL);
551 return 0;
552}
553
554static struct sysdev_class oprofile_sysclass = {
555 .name = "oprofile",
556 .resume = nmi_resume,
557 .suspend = nmi_suspend,
558};
559
560static struct sys_device device_oprofile = {
561 .id = 0,
562 .cls = &oprofile_sysclass,
563};
564
565static int __init init_sysfs(void)
566{
567 int error;
568
569 error = sysdev_class_register(&oprofile_sysclass);
Robert Richter10f04122010-08-30 10:56:18 +0200570 if (error)
571 return error;
572
573 error = sysdev_register(&device_oprofile);
574 if (error)
575 sysdev_class_unregister(&oprofile_sysclass);
576
Robert Richter69046d42008-09-05 12:17:40 +0200577 return error;
578}
579
580static void exit_sysfs(void)
581{
582 sysdev_unregister(&device_oprofile);
583 sysdev_class_unregister(&oprofile_sysclass);
584}
585
586#else
Robert Richter269f45c2010-09-01 14:50:50 +0200587
588static inline int init_sysfs(void) { return 0; }
589static inline void exit_sysfs(void) { }
590
Robert Richter69046d42008-09-05 12:17:40 +0200591#endif /* CONFIG_PM */
592
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100593static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 __u8 cpu_model = boot_cpu_data.x86_model;
596
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200597 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599
600#ifndef CONFIG_SMP
601 *cpu_type = "i386/p4";
602 model = &op_p4_spec;
603 return 1;
604#else
605 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100606 case 1:
607 *cpu_type = "i386/p4";
608 model = &op_p4_spec;
609 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100611 case 2:
612 *cpu_type = "i386/p4-ht";
613 model = &op_p4_ht2_spec;
614 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616#endif
617
618 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
619 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
620 return 0;
621}
622
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200623static int force_arch_perfmon;
624static int force_cpu_type(const char *str, struct kernel_param *kp)
625{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200626 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200627 force_arch_perfmon = 1;
628 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
629 }
630
631 return 0;
632}
633module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200634
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100635static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200638 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200640 if (force_arch_perfmon && cpu_has_arch_perfmon)
641 return 0;
642
John Villalovos45c34e02010-05-07 12:41:40 -0400643 /*
644 * Documentation on identifying Intel processors by CPU family
645 * and model can be found in the Intel Software Developer's
646 * Manuals (SDM):
647 *
648 * http://www.intel.com/products/processor/manuals/
649 *
650 * As of May 2010 the documentation for this was in the:
651 * "Intel 64 and IA-32 Architectures Software Developer's
652 * Manual Volume 3B: System Programming Guide", "Table B-1
653 * CPUID Signature Values of DisplayFamily_DisplayModel".
654 */
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700655 switch (cpu_model) {
656 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700658 break;
659 case 3 ... 5:
660 *cpu_type = "i386/pii";
661 break;
662 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500663 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700664 *cpu_type = "i386/piii";
665 break;
666 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500667 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700668 *cpu_type = "i386/p6_mobile";
669 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700670 case 14:
671 *cpu_type = "i386/core";
672 break;
Patrick Simmonsc33f5432010-09-08 10:34:28 -0400673 case 0x0f:
674 case 0x16:
675 case 0x17:
Jiri Olsabb7ab782010-09-21 03:26:35 -0400676 case 0x1d:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700677 *cpu_type = "i386/core_2";
678 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400679 case 0x1a:
Josh Hunta7c55cb2010-08-04 20:27:05 -0400680 case 0x1e:
Andi Kleene83e4522010-01-21 23:26:27 +0100681 case 0x2e:
Robert Richter802070f2009-06-12 18:32:07 +0200682 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200683 *cpu_type = "i386/core_i7";
684 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400685 case 0x1c:
Andi Kleen6adf4062009-04-27 17:44:13 +0200686 *cpu_type = "i386/atom";
687 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700688 default:
689 /* Unknown */
690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Robert Richter802070f2009-06-12 18:32:07 +0200693 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 1;
695}
696
David Gibson96d08212005-09-06 15:17:26 -0700697int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 __u8 vendor = boot_cpu_data.x86_vendor;
700 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200701 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200702 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!cpu_has_apic)
705 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100708 case X86_VENDOR_AMD:
709 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100711 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100712 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100713 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100715 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100716 /*
717 * Actually it could be i386/hammer too, but
718 * give user space an consistent name.
719 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100720 cpu_type = "x86-64/hammer";
721 break;
722 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100723 cpu_type = "x86-64/family10";
724 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200725 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200726 cpu_type = "x86-64/family11h";
727 break;
Robert Richter3acbf082010-08-31 10:44:17 +0200728 case 0x12:
729 cpu_type = "x86-64/family12h";
730 break;
Robert Richtere6341472010-08-26 12:30:17 +0200731 case 0x14:
732 cpu_type = "x86-64/family14h";
733 break;
Robert Richter30570bc2010-08-31 10:44:38 +0200734 case 0x15:
735 cpu_type = "x86-64/family15h";
736 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100737 default:
738 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100739 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100740 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100743 case X86_VENDOR_INTEL:
744 switch (family) {
745 /* Pentium IV */
746 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200747 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100750 /* A P6-class processor */
751 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200752 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754
755 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200756 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100757 }
Andi Kleenb9917022008-08-18 14:50:31 +0200758
Robert Richtere4192942008-10-12 15:12:34 -0400759 if (cpu_type)
760 break;
761
762 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200763 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400764
765 /* use arch perfmon as fallback */
766 cpu_type = "i386/arch_perfmon";
767 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100768 break;
769
770 default:
771 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Robert Richter270d3e12008-07-22 21:09:01 +0200774 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200775 ops->create_files = nmi_create_files;
776 ops->setup = nmi_setup;
777 ops->shutdown = nmi_shutdown;
778 ops->start = nmi_start;
779 ops->stop = nmi_stop;
780 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200781
Robert Richteradf5ec02008-07-22 21:08:48 +0200782 if (model->init)
783 ret = model->init(ops);
784 if (ret)
785 return ret;
786
Robert Richter52471c62009-07-06 14:43:55 +0200787 if (!model->num_virt_counters)
788 model->num_virt_counters = model->num_counters;
789
Robert Richter52805142009-07-09 16:02:44 +0200790 mux_init(ops);
791
Robert Richter10f04122010-08-30 10:56:18 +0200792 ret = init_sysfs();
793 if (ret)
794 return ret;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
797 return 0;
798}
799
David Gibson96d08212005-09-06 15:17:26 -0700800void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Robert Richter51404342010-09-30 18:55:47 +0200802 exit_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}