blob: f0fb44725d80b09d895401b415a019099e3b64ca [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* 0 == registered but off, 1 == registered and on */
35static int nmi_enabled = 0;
36
Jason Yeh4d4036e2009-07-08 13:49:38 +020037
38#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
39extern atomic_t multiplex_counter;
40#endif
41
42struct op_counter_config counter_config[OP_MAX_COUNTER];
43
Robert Richter3370d352009-05-25 15:10:32 +020044/* common functions */
45
46u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
47 struct op_counter_config *counter_config)
48{
49 u64 val = 0;
50 u16 event = (u16)counter_config->event;
51
52 val |= ARCH_PERFMON_EVENTSEL_INT;
53 val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
54 val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
55 val |= (counter_config->unit_mask & 0xFF) << 8;
56 event &= model->event_mask ? model->event_mask : 0xFF;
57 val |= event & 0xFF;
58 val |= (event & 0x0F00) << 24;
59
60 return val;
61}
62
63
Adrian Bunkc7c19f82006-09-26 10:52:27 +020064static int profile_exceptions_notify(struct notifier_block *self,
65 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Don Zickus2fbe7b22006-09-26 10:52:27 +020067 struct die_args *args = (struct die_args *)data;
68 int ret = NOTIFY_DONE;
69 int cpu = smp_processor_id();
70
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010071 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020072 case DIE_NMI:
Mike Galbraith5b75af02009-02-04 17:11:34 +010073 case DIE_NMI_IPI:
74 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
75 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);
103 model->start(msrs);
104}
105
106static int nmi_start(void)
107{
108 on_each_cpu(nmi_cpu_start, NULL, 1);
109 return 0;
110}
111
112static void nmi_cpu_stop(void *dummy)
113{
114 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
115 model->stop(msrs);
116}
117
118static void nmi_stop(void)
119{
120 on_each_cpu(nmi_cpu_stop, NULL, 1);
121}
122
Robert Richterd8471ad2009-07-16 13:04:43 +0200123#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
124
125static DEFINE_PER_CPU(int, switch_index);
126
Robert Richter39e97f42009-07-09 15:11:45 +0200127static inline int has_mux(void)
128{
129 return !!model->switch_ctrl;
130}
131
Robert Richterd8471ad2009-07-16 13:04:43 +0200132inline int op_x86_phys_to_virt(int phys)
133{
134 return __get_cpu_var(switch_index) + phys;
135}
136
Robert Richter6ab82f92009-07-09 14:40:04 +0200137static void nmi_shutdown_mux(void)
138{
139 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200140
141 if (!has_mux())
142 return;
143
Robert Richter6ab82f92009-07-09 14:40:04 +0200144 for_each_possible_cpu(i) {
145 kfree(per_cpu(cpu_msrs, i).multiplex);
146 per_cpu(cpu_msrs, i).multiplex = NULL;
147 per_cpu(switch_index, i) = 0;
148 }
149}
150
151static int nmi_setup_mux(void)
152{
153 size_t multiplex_size =
154 sizeof(struct op_msr) * model->num_virt_counters;
155 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200156
157 if (!has_mux())
158 return 1;
159
Robert Richter6ab82f92009-07-09 14:40:04 +0200160 for_each_possible_cpu(i) {
161 per_cpu(cpu_msrs, i).multiplex =
162 kmalloc(multiplex_size, GFP_KERNEL);
163 if (!per_cpu(cpu_msrs, i).multiplex)
164 return 0;
165 }
Robert Richter39e97f42009-07-09 15:11:45 +0200166
Robert Richter6ab82f92009-07-09 14:40:04 +0200167 return 1;
168}
169
Robert Richter48fb4b42009-07-09 14:38:49 +0200170static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
171{
172 int i;
173 struct op_msr *multiplex = msrs->multiplex;
174
Robert Richter39e97f42009-07-09 15:11:45 +0200175 if (!has_mux())
176 return;
177
Robert Richter48fb4b42009-07-09 14:38:49 +0200178 for (i = 0; i < model->num_virt_counters; ++i) {
179 if (counter_config[i].enabled) {
180 multiplex[i].saved = -(u64)counter_config[i].count;
181 } else {
182 multiplex[i].addr = 0;
183 multiplex[i].saved = 0;
184 }
185 }
186
187 per_cpu(switch_index, cpu) = 0;
188}
189
Robert Richterd0f585d2009-07-09 14:38:49 +0200190static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
191{
192 struct op_msr *multiplex = msrs->multiplex;
193 int i;
194
195 for (i = 0; i < model->num_counters; ++i) {
196 int virt = op_x86_phys_to_virt(i);
197 if (multiplex[virt].addr)
198 rdmsrl(multiplex[virt].addr, multiplex[virt].saved);
199 }
200}
201
202static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
203{
204 struct op_msr *multiplex = msrs->multiplex;
205 int i;
206
207 for (i = 0; i < model->num_counters; ++i) {
208 int virt = op_x86_phys_to_virt(i);
209 if (multiplex[virt].addr)
210 wrmsrl(multiplex[virt].addr, multiplex[virt].saved);
211 }
212}
213
Robert Richterb28d1b92009-07-09 14:38:49 +0200214static void nmi_cpu_switch(void *dummy)
215{
216 int cpu = smp_processor_id();
217 int si = per_cpu(switch_index, cpu);
218 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
219
220 nmi_cpu_stop(NULL);
221 nmi_cpu_save_mpx_registers(msrs);
222
223 /* move to next set */
224 si += model->num_counters;
225 if ((si > model->num_virt_counters) || (counter_config[si].count == 0))
226 per_cpu(switch_index, cpu) = 0;
227 else
228 per_cpu(switch_index, cpu) = si;
229
230 model->switch_ctrl(model, msrs);
231 nmi_cpu_restore_mpx_registers(msrs);
232
233 nmi_cpu_start(NULL);
234}
235
236
237/*
238 * Quick check to see if multiplexing is necessary.
239 * The check should be sufficient since counters are used
240 * in ordre.
241 */
242static int nmi_multiplex_on(void)
243{
244 return counter_config[model->num_counters].count ? 0 : -EINVAL;
245}
246
247static int nmi_switch_event(void)
248{
Robert Richter39e97f42009-07-09 15:11:45 +0200249 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200250 return -ENOSYS; /* not implemented */
251 if (nmi_multiplex_on() < 0)
252 return -EINVAL; /* not necessary */
253
254 on_each_cpu(nmi_cpu_switch, NULL, 1);
255
256 atomic_inc(&multiplex_counter);
257
258 return 0;
259}
260
Robert Richter52805142009-07-09 16:02:44 +0200261static inline void mux_init(struct oprofile_operations *ops)
262{
263 if (has_mux())
264 ops->switch_events = nmi_switch_event;
265}
266
Robert Richterd8471ad2009-07-16 13:04:43 +0200267#else
268
269inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200270static inline void nmi_shutdown_mux(void) { }
271static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200272static inline void
273nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200274static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200275
276#endif
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void free_msrs(void)
279{
280 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800281 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700282 kfree(per_cpu(cpu_msrs, i).counters);
283 per_cpu(cpu_msrs, i).counters = NULL;
284 kfree(per_cpu(cpu_msrs, i).controls);
285 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int allocate_msrs(void)
290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
292 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
293
Robert Richter4c168ea2008-09-24 11:08:52 +0200294 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700295 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700296 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200297 GFP_KERNEL);
298 if (!per_cpu(cpu_msrs, i).counters)
299 return 0;
Robert Richter4c168ea2008-09-24 11:08:52 +0200300 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200301 GFP_KERNEL);
302 if (!per_cpu(cpu_msrs, i).controls)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Robert Richter6ab82f92009-07-09 14:40:04 +0200306 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100309static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700312 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200313 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200315 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200316 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700318 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 apic_write(APIC_LVTPC, APIC_DM_NMI);
320}
321
Don Zickus2fbe7b22006-09-26 10:52:27 +0200322static struct notifier_block profile_exceptions_nb = {
323 .notifier_call = profile_exceptions_notify,
324 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100325 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200326};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328static int nmi_setup(void)
329{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100330 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200331 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!allocate_msrs())
Robert Richter6ab82f92009-07-09 14:40:04 +0200334 err = -ENOMEM;
335 else if (!nmi_setup_mux())
336 err = -ENOMEM;
337 else
338 err = register_die_notifier(&profile_exceptions_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100340 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 free_msrs();
Robert Richter6ab82f92009-07-09 14:40:04 +0200342 nmi_shutdown_mux();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Don Zickus2fbe7b22006-09-26 10:52:27 +0200345
Robert Richter4c168ea2008-09-24 11:08:52 +0200346 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * of msrs are distinct for save and setup operations
348 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200349
350 /* Assume saved/restored counters are the same on all CPUs */
Mike Travisd18d00f2008-03-25 15:06:59 -0700351 model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100352 for_each_possible_cpu(cpu) {
Chris Wright0939c172007-06-01 00:46:39 -0700353 if (cpu != 0) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700354 memcpy(per_cpu(cpu_msrs, cpu).counters,
355 per_cpu(cpu_msrs, 0).counters,
Chris Wright0939c172007-06-01 00:46:39 -0700356 sizeof(struct op_msr) * model->num_counters);
357
Mike Travisd18d00f2008-03-25 15:06:59 -0700358 memcpy(per_cpu(cpu_msrs, cpu).controls,
359 per_cpu(cpu_msrs, 0).controls,
Chris Wright0939c172007-06-01 00:46:39 -0700360 sizeof(struct op_msr) * model->num_controls);
Jason Yeh4d4036e2009-07-08 13:49:38 +0200361#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
362 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
363 per_cpu(cpu_msrs, 0).multiplex,
364 sizeof(struct op_msr) * model->num_virt_counters);
365#endif
Chris Wright0939c172007-06-01 00:46:39 -0700366 }
Andi Kleen6c977aa2007-05-21 14:31:45 +0200367 }
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200368 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 nmi_enabled = 1;
370 return 0;
371}
372
Robert Richter44ab9a62009-07-09 18:33:02 +0200373static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100375 struct op_msr *counters = msrs->counters;
376 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned int i;
378
Robert Richter1a245c42009-06-05 15:54:24 +0200379 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200380 if (controls[i].addr)
381 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100383
Robert Richter1a245c42009-06-05 15:54:24 +0200384 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200385 if (counters[i].addr)
386 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100390static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 unsigned int v;
393 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200394 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* restoring APIC_LVTPC can trigger an apic error because the delivery
397 * mode and vector nr combination can be illegal. That's by design: on
398 * power on apic lvt contain a zero vector nr which are legal only for
399 * NMI delivery mode. So inhibit apic err before restoring lvtpc
400 */
401 v = apic_read(APIC_LVTERR);
402 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700403 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200405 nmi_cpu_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static void nmi_shutdown(void)
409{
Andrea Righib61e06f2008-09-20 18:02:27 +0200410 struct op_msrs *msrs;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 nmi_enabled = 0;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200413 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200414 unregister_die_notifier(&profile_exceptions_nb);
Robert Richter6ab82f92009-07-09 14:40:04 +0200415 nmi_shutdown_mux();
Andrea Righib61e06f2008-09-20 18:02:27 +0200416 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700417 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200419 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100422static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 unsigned int i;
425
Jason Yeh4d4036e2009-07-08 13:49:38 +0200426 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100427 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700428 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100429
Jason Yeh4d4036e2009-07-08 13:49:38 +0200430#ifndef CONFIG_OPROFILE_EVENT_MULTIPLEX
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100431 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200432 * available for use. This should protect userspace app.
433 * NOTE: assumes 1:1 mapping here (that counters are organized
434 * sequentially in their struct assignment).
435 */
436 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
437 continue;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200438#endif /* CONFIG_OPROFILE_EVENT_MULTIPLEX */
Don Zickuscb9c4482006-09-26 10:52:26 +0200439
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700440 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100442 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
443 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
444 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
445 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
446 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
447 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 return 0;
451}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100452
Robert Richter69046d42008-09-05 12:17:40 +0200453#ifdef CONFIG_SMP
454static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
455 void *data)
456{
457 int cpu = (unsigned long)data;
458 switch (action) {
459 case CPU_DOWN_FAILED:
460 case CPU_ONLINE:
461 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
462 break;
463 case CPU_DOWN_PREPARE:
464 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
465 break;
466 }
467 return NOTIFY_DONE;
468}
469
470static struct notifier_block oprofile_cpu_nb = {
471 .notifier_call = oprofile_cpu_notifier
472};
473#endif
474
475#ifdef CONFIG_PM
476
477static int nmi_suspend(struct sys_device *dev, pm_message_t state)
478{
479 /* Only one CPU left, just stop that one */
480 if (nmi_enabled == 1)
481 nmi_cpu_stop(NULL);
482 return 0;
483}
484
485static int nmi_resume(struct sys_device *dev)
486{
487 if (nmi_enabled == 1)
488 nmi_cpu_start(NULL);
489 return 0;
490}
491
492static struct sysdev_class oprofile_sysclass = {
493 .name = "oprofile",
494 .resume = nmi_resume,
495 .suspend = nmi_suspend,
496};
497
498static struct sys_device device_oprofile = {
499 .id = 0,
500 .cls = &oprofile_sysclass,
501};
502
503static int __init init_sysfs(void)
504{
505 int error;
506
507 error = sysdev_class_register(&oprofile_sysclass);
508 if (!error)
509 error = sysdev_register(&device_oprofile);
510 return error;
511}
512
513static void exit_sysfs(void)
514{
515 sysdev_unregister(&device_oprofile);
516 sysdev_class_unregister(&oprofile_sysclass);
517}
518
519#else
520#define init_sysfs() do { } while (0)
521#define exit_sysfs() do { } while (0)
522#endif /* CONFIG_PM */
523
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100524static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 __u8 cpu_model = boot_cpu_data.x86_model;
527
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200528 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530
531#ifndef CONFIG_SMP
532 *cpu_type = "i386/p4";
533 model = &op_p4_spec;
534 return 1;
535#else
536 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100537 case 1:
538 *cpu_type = "i386/p4";
539 model = &op_p4_spec;
540 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100542 case 2:
543 *cpu_type = "i386/p4-ht";
544 model = &op_p4_ht2_spec;
545 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547#endif
548
549 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
550 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
551 return 0;
552}
553
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200554static int force_arch_perfmon;
555static int force_cpu_type(const char *str, struct kernel_param *kp)
556{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200557 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200558 force_arch_perfmon = 1;
559 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
560 }
561
562 return 0;
563}
564module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200565
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100566static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200569 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200571 if (force_arch_perfmon && cpu_has_arch_perfmon)
572 return 0;
573
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700574 switch (cpu_model) {
575 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700577 break;
578 case 3 ... 5:
579 *cpu_type = "i386/pii";
580 break;
581 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500582 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700583 *cpu_type = "i386/piii";
584 break;
585 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500586 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700587 *cpu_type = "i386/p6_mobile";
588 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700589 case 14:
590 *cpu_type = "i386/core";
591 break;
592 case 15: case 23:
593 *cpu_type = "i386/core_2";
594 break;
Andi Kleen6adf4062009-04-27 17:44:13 +0200595 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200596 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200597 *cpu_type = "i386/core_i7";
598 break;
599 case 28:
600 *cpu_type = "i386/atom";
601 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700602 default:
603 /* Unknown */
604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
Robert Richter802070f2009-06-12 18:32:07 +0200607 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 1;
609}
610
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100611/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static int using_nmi;
613
David Gibson96d08212005-09-06 15:17:26 -0700614int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 __u8 vendor = boot_cpu_data.x86_vendor;
617 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200618 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200619 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (!cpu_has_apic)
622 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100625 case X86_VENDOR_AMD:
626 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100628 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100629 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100630 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100632 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100633 /*
634 * Actually it could be i386/hammer too, but
635 * give user space an consistent name.
636 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100637 cpu_type = "x86-64/hammer";
638 break;
639 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100640 cpu_type = "x86-64/family10";
641 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200642 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200643 cpu_type = "x86-64/family11h";
644 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100645 default:
646 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100647 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100648 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100651 case X86_VENDOR_INTEL:
652 switch (family) {
653 /* Pentium IV */
654 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200655 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100658 /* A P6-class processor */
659 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200660 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
662
663 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200664 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100665 }
Andi Kleenb9917022008-08-18 14:50:31 +0200666
Robert Richtere4192942008-10-12 15:12:34 -0400667 if (cpu_type)
668 break;
669
670 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200671 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400672
673 /* use arch perfmon as fallback */
674 cpu_type = "i386/arch_perfmon";
675 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100676 break;
677
678 default:
679 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200682#ifdef CONFIG_SMP
683 register_cpu_notifier(&oprofile_cpu_nb);
684#endif
Robert Richter270d3e12008-07-22 21:09:01 +0200685 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200686 ops->create_files = nmi_create_files;
687 ops->setup = nmi_setup;
688 ops->shutdown = nmi_shutdown;
689 ops->start = nmi_start;
690 ops->stop = nmi_stop;
691 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200692
Robert Richteradf5ec02008-07-22 21:08:48 +0200693 if (model->init)
694 ret = model->init(ops);
695 if (ret)
696 return ret;
697
Robert Richter52471c62009-07-06 14:43:55 +0200698 if (!model->num_virt_counters)
699 model->num_virt_counters = model->num_counters;
700
Robert Richter52805142009-07-09 16:02:44 +0200701 mux_init(ops);
702
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100703 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
706 return 0;
707}
708
David Gibson96d08212005-09-06 15:17:26 -0700709void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200711 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100712 exit_sysfs();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200713#ifdef CONFIG_SMP
714 unregister_cpu_notifier(&oprofile_cpu_nb);
715#endif
716 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200717 if (model->exit)
718 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}