blob: cf9750004a08fe673eb312414090c7b0c7759b09 [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;
Andi Kleen914a76c2011-03-16 15:44:33 -040052 counter_config->extra &= (ARCH_PERFMON_EVENTSEL_INV |
53 ARCH_PERFMON_EVENTSEL_EDGE |
54 ARCH_PERFMON_EVENTSEL_CMASK);
55 val |= counter_config->extra;
Robert Richter3370d352009-05-25 15:10:32 +020056 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;
Don Zickus2fbe7b22006-09-26 10:52:27 +020069
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010070 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020071 case DIE_NMI:
Robert Richterde654642010-05-03 14:41:22 +020072 if (ctr_running)
73 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
74 else if (!nmi_enabled)
75 break;
76 else
77 model->stop(&__get_cpu_var(cpu_msrs));
Mike Galbraith5b75af02009-02-04 17:11:34 +010078 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020079 break;
80 default:
81 break;
82 }
83 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Don Zickus2fbe7b22006-09-26 10:52:27 +020085
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010086static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010088 struct op_msr *counters = msrs->counters;
89 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned int i;
91
Robert Richter1a245c42009-06-05 15:54:24 +020092 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020093 if (counters[i].addr)
94 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010096
Robert Richter1a245c42009-06-05 15:54:24 +020097 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020098 if (controls[i].addr)
99 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101}
102
Robert Richterb28d1b92009-07-09 14:38:49 +0200103static void nmi_cpu_start(void *dummy)
104{
105 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200106 if (!msrs->controls)
107 WARN_ON_ONCE(1);
108 else
109 model->start(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200110}
111
112static int nmi_start(void)
113{
Robert Richter6ae56b52010-04-29 14:55:55 +0200114 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200115 on_each_cpu(nmi_cpu_start, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200116 ctr_running = 1;
117 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200118 return 0;
119}
120
121static void nmi_cpu_stop(void *dummy)
122{
123 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200124 if (!msrs->controls)
125 WARN_ON_ONCE(1);
126 else
127 model->stop(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200128}
129
130static void nmi_stop(void)
131{
Robert Richter6ae56b52010-04-29 14:55:55 +0200132 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200133 on_each_cpu(nmi_cpu_stop, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200134 ctr_running = 0;
135 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200136}
137
Robert Richterd8471ad2009-07-16 13:04:43 +0200138#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
139
140static DEFINE_PER_CPU(int, switch_index);
141
Robert Richter39e97f42009-07-09 15:11:45 +0200142static inline int has_mux(void)
143{
144 return !!model->switch_ctrl;
145}
146
Robert Richterd8471ad2009-07-16 13:04:43 +0200147inline int op_x86_phys_to_virt(int phys)
148{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100149 return __this_cpu_read(switch_index) + phys;
Robert Richterd8471ad2009-07-16 13:04:43 +0200150}
151
Robert Richter61d149d2009-07-10 15:47:17 +0200152inline int op_x86_virt_to_phys(int virt)
153{
154 return virt % model->num_counters;
155}
156
Robert Richter6ab82f92009-07-09 14:40:04 +0200157static void nmi_shutdown_mux(void)
158{
159 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200160
161 if (!has_mux())
162 return;
163
Robert Richter6ab82f92009-07-09 14:40:04 +0200164 for_each_possible_cpu(i) {
165 kfree(per_cpu(cpu_msrs, i).multiplex);
166 per_cpu(cpu_msrs, i).multiplex = NULL;
167 per_cpu(switch_index, i) = 0;
168 }
169}
170
171static int nmi_setup_mux(void)
172{
173 size_t multiplex_size =
174 sizeof(struct op_msr) * model->num_virt_counters;
175 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200176
177 if (!has_mux())
178 return 1;
179
Robert Richter6ab82f92009-07-09 14:40:04 +0200180 for_each_possible_cpu(i) {
181 per_cpu(cpu_msrs, i).multiplex =
Robert Richterc17c8fb2010-02-25 20:20:25 +0100182 kzalloc(multiplex_size, GFP_KERNEL);
Robert Richter6ab82f92009-07-09 14:40:04 +0200183 if (!per_cpu(cpu_msrs, i).multiplex)
184 return 0;
185 }
Robert Richter39e97f42009-07-09 15:11:45 +0200186
Robert Richter6ab82f92009-07-09 14:40:04 +0200187 return 1;
188}
189
Robert Richter48fb4b42009-07-09 14:38:49 +0200190static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
191{
192 int i;
193 struct op_msr *multiplex = msrs->multiplex;
194
Robert Richter39e97f42009-07-09 15:11:45 +0200195 if (!has_mux())
196 return;
197
Robert Richter48fb4b42009-07-09 14:38:49 +0200198 for (i = 0; i < model->num_virt_counters; ++i) {
199 if (counter_config[i].enabled) {
200 multiplex[i].saved = -(u64)counter_config[i].count;
201 } else {
Robert Richter48fb4b42009-07-09 14:38:49 +0200202 multiplex[i].saved = 0;
203 }
204 }
205
206 per_cpu(switch_index, cpu) = 0;
207}
208
Robert Richterd0f585d2009-07-09 14:38:49 +0200209static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
210{
Robert Richter68dc8192010-02-25 19:16:46 +0100211 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200212 struct op_msr *multiplex = msrs->multiplex;
213 int i;
214
215 for (i = 0; i < model->num_counters; ++i) {
216 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100217 if (counters[i].addr)
218 rdmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200219 }
220}
221
222static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
223{
Robert Richter68dc8192010-02-25 19:16:46 +0100224 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200225 struct op_msr *multiplex = msrs->multiplex;
226 int i;
227
228 for (i = 0; i < model->num_counters; ++i) {
229 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100230 if (counters[i].addr)
231 wrmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200232 }
233}
234
Robert Richterb28d1b92009-07-09 14:38:49 +0200235static void nmi_cpu_switch(void *dummy)
236{
237 int cpu = smp_processor_id();
238 int si = per_cpu(switch_index, cpu);
239 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
240
241 nmi_cpu_stop(NULL);
242 nmi_cpu_save_mpx_registers(msrs);
243
244 /* move to next set */
245 si += model->num_counters;
Suravee Suthikulpanitd8cc1082010-01-18 11:25:36 -0600246 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
Robert Richterb28d1b92009-07-09 14:38:49 +0200247 per_cpu(switch_index, cpu) = 0;
248 else
249 per_cpu(switch_index, cpu) = si;
250
251 model->switch_ctrl(model, msrs);
252 nmi_cpu_restore_mpx_registers(msrs);
253
254 nmi_cpu_start(NULL);
255}
256
257
258/*
259 * Quick check to see if multiplexing is necessary.
260 * The check should be sufficient since counters are used
261 * in ordre.
262 */
263static int nmi_multiplex_on(void)
264{
265 return counter_config[model->num_counters].count ? 0 : -EINVAL;
266}
267
268static int nmi_switch_event(void)
269{
Robert Richter39e97f42009-07-09 15:11:45 +0200270 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200271 return -ENOSYS; /* not implemented */
272 if (nmi_multiplex_on() < 0)
273 return -EINVAL; /* not necessary */
274
Robert Richter6ae56b52010-04-29 14:55:55 +0200275 get_online_cpus();
276 if (ctr_running)
277 on_each_cpu(nmi_cpu_switch, NULL, 1);
278 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200279
Robert Richterb28d1b92009-07-09 14:38:49 +0200280 return 0;
281}
282
Robert Richter52805142009-07-09 16:02:44 +0200283static inline void mux_init(struct oprofile_operations *ops)
284{
285 if (has_mux())
286 ops->switch_events = nmi_switch_event;
287}
288
Robert Richter4d015f72009-07-09 21:42:51 +0200289static void mux_clone(int cpu)
290{
291 if (!has_mux())
292 return;
293
294 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
295 per_cpu(cpu_msrs, 0).multiplex,
296 sizeof(struct op_msr) * model->num_virt_counters);
297}
298
Robert Richterd8471ad2009-07-16 13:04:43 +0200299#else
300
301inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200302inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200303static inline void nmi_shutdown_mux(void) { }
304static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200305static inline void
306nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200307static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200308static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200309
310#endif
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static void free_msrs(void)
313{
314 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800315 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700316 kfree(per_cpu(cpu_msrs, i).counters);
317 per_cpu(cpu_msrs, i).counters = NULL;
318 kfree(per_cpu(cpu_msrs, i).controls);
319 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100321 nmi_shutdown_mux();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static int allocate_msrs(void)
325{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
327 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
328
Robert Richter4c168ea2008-09-24 11:08:52 +0200329 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700330 for_each_possible_cpu(i) {
Robert Richterc17c8fb2010-02-25 20:20:25 +0100331 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200332 GFP_KERNEL);
333 if (!per_cpu(cpu_msrs, i).counters)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100334 goto fail;
Robert Richterc17c8fb2010-02-25 20:20:25 +0100335 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200336 GFP_KERNEL);
337 if (!per_cpu(cpu_msrs, i).controls)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100338 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100341 if (!nmi_setup_mux())
342 goto fail;
343
Robert Richter6ab82f92009-07-09 14:40:04 +0200344 return 1;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100345
346fail:
347 free_msrs();
348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100351static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700354 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200355 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200357 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200358 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700360 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 apic_write(APIC_LVTPC, APIC_DM_NMI);
362}
363
Don Zickus2fbe7b22006-09-26 10:52:27 +0200364static struct notifier_block profile_exceptions_nb = {
365 .notifier_call = profile_exceptions_notify,
366 .next = NULL,
Don Zickus166d7512011-01-06 16:18:49 -0500367 .priority = NMI_LOCAL_LOW_PRIOR,
Don Zickus2fbe7b22006-09-26 10:52:27 +0200368};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Robert Richter44ab9a62009-07-09 18:33:02 +0200370static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100372 struct op_msr *counters = msrs->counters;
373 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned int i;
375
Robert Richter1a245c42009-06-05 15:54:24 +0200376 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200377 if (controls[i].addr)
378 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100380
Robert Richter1a245c42009-06-05 15:54:24 +0200381 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200382 if (counters[i].addr)
383 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100387static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 unsigned int v;
390 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200391 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* restoring APIC_LVTPC can trigger an apic error because the delivery
394 * mode and vector nr combination can be illegal. That's by design: on
395 * power on apic lvt contain a zero vector nr which are legal only for
396 * NMI delivery mode. So inhibit apic err before restoring lvtpc
397 */
398 v = apic_read(APIC_LVTERR);
399 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700400 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200402 nmi_cpu_restore_registers(msrs);
Robert Richterbae663b2010-05-05 17:47:17 +0200403 if (model->cpu_down)
404 model->cpu_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Robert Richter6ae56b52010-04-29 14:55:55 +0200407static void nmi_cpu_up(void *dummy)
408{
409 if (nmi_enabled)
410 nmi_cpu_setup(dummy);
411 if (ctr_running)
412 nmi_cpu_start(dummy);
413}
414
415static void nmi_cpu_down(void *dummy)
416{
417 if (ctr_running)
418 nmi_cpu_stop(dummy);
419 if (nmi_enabled)
420 nmi_cpu_shutdown(dummy);
421}
422
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100423static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int i;
426
Jason Yeh4d4036e2009-07-08 13:49:38 +0200427 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100428 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700429 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100430
431 /* 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 */
Robert Richter11be1a72009-07-10 18:15:21 +0200436 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200437 continue;
438
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700439 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100441 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
442 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
443 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
444 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
445 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
446 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Andi Kleen914a76c2011-03-16 15:44:33 -0400447 oprofilefs_create_ulong(sb, dir, "extra", &counter_config[i].extra);
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 +0200453static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
454 void *data)
455{
456 int cpu = (unsigned long)data;
457 switch (action) {
458 case CPU_DOWN_FAILED:
459 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200460 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200461 break;
462 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200463 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200464 break;
465 }
466 return NOTIFY_DONE;
467}
468
469static struct notifier_block oprofile_cpu_nb = {
470 .notifier_call = oprofile_cpu_notifier
471};
Robert Richter69046d42008-09-05 12:17:40 +0200472
Robert Richterd30d64c2010-05-03 15:52:26 +0200473static int nmi_setup(void)
474{
475 int err = 0;
476 int cpu;
477
478 if (!allocate_msrs())
479 return -ENOMEM;
480
481 /* We need to serialize save and setup for HT because the subset
482 * of msrs are distinct for save and setup operations
483 */
484
485 /* Assume saved/restored counters are the same on all CPUs */
486 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
487 if (err)
488 goto fail;
489
490 for_each_possible_cpu(cpu) {
491 if (!cpu)
492 continue;
493
494 memcpy(per_cpu(cpu_msrs, cpu).counters,
495 per_cpu(cpu_msrs, 0).counters,
496 sizeof(struct op_msr) * model->num_counters);
497
498 memcpy(per_cpu(cpu_msrs, cpu).controls,
499 per_cpu(cpu_msrs, 0).controls,
500 sizeof(struct op_msr) * model->num_controls);
501
502 mux_clone(cpu);
503 }
504
505 nmi_enabled = 0;
506 ctr_running = 0;
507 barrier();
508 err = register_die_notifier(&profile_exceptions_nb);
509 if (err)
510 goto fail;
511
512 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200513 register_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200514 on_each_cpu(nmi_cpu_setup, NULL, 1);
515 nmi_enabled = 1;
516 put_online_cpus();
517
518 return 0;
519fail:
520 free_msrs();
521 return err;
522}
523
524static void nmi_shutdown(void)
525{
526 struct op_msrs *msrs;
527
528 get_online_cpus();
Robert Richter3de668e2010-05-03 15:00:25 +0200529 unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200530 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
531 nmi_enabled = 0;
532 ctr_running = 0;
533 put_online_cpus();
534 barrier();
535 unregister_die_notifier(&profile_exceptions_nb);
536 msrs = &get_cpu_var(cpu_msrs);
537 model->shutdown(msrs);
538 free_msrs();
539 put_cpu_var(cpu_msrs);
540}
541
Robert Richter69046d42008-09-05 12:17:40 +0200542#ifdef CONFIG_PM
543
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100544static int nmi_suspend(void)
Robert Richter69046d42008-09-05 12:17:40 +0200545{
546 /* Only one CPU left, just stop that one */
547 if (nmi_enabled == 1)
548 nmi_cpu_stop(NULL);
549 return 0;
550}
551
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100552static void nmi_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200553{
554 if (nmi_enabled == 1)
555 nmi_cpu_start(NULL);
Robert Richter69046d42008-09-05 12:17:40 +0200556}
557
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100558static struct syscore_ops oprofile_syscore_ops = {
Robert Richter69046d42008-09-05 12:17:40 +0200559 .resume = nmi_resume,
560 .suspend = nmi_suspend,
561};
562
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100563static void __init init_suspend_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200564{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100565 register_syscore_ops(&oprofile_syscore_ops);
Robert Richter69046d42008-09-05 12:17:40 +0200566}
567
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100568static void exit_suspend_resume(void)
Robert Richter69046d42008-09-05 12:17:40 +0200569{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100570 unregister_syscore_ops(&oprofile_syscore_ops);
Robert Richter69046d42008-09-05 12:17:40 +0200571}
572
573#else
Robert Richter269f45c2010-09-01 14:50:50 +0200574
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100575static inline void init_suspend_resume(void) { }
576static inline void exit_suspend_resume(void) { }
Robert Richter269f45c2010-09-01 14:50:50 +0200577
Robert Richter69046d42008-09-05 12:17:40 +0200578#endif /* CONFIG_PM */
579
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100580static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 __u8 cpu_model = boot_cpu_data.x86_model;
583
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200584 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586
587#ifndef CONFIG_SMP
588 *cpu_type = "i386/p4";
589 model = &op_p4_spec;
590 return 1;
591#else
592 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100593 case 1:
594 *cpu_type = "i386/p4";
595 model = &op_p4_spec;
596 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100598 case 2:
599 *cpu_type = "i386/p4-ht";
600 model = &op_p4_ht2_spec;
601 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603#endif
604
605 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
606 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
607 return 0;
608}
609
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200610static int force_arch_perfmon;
611static int force_cpu_type(const char *str, struct kernel_param *kp)
612{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200613 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200614 force_arch_perfmon = 1;
615 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
616 }
617
618 return 0;
619}
620module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200621
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100622static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200625 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200627 if (force_arch_perfmon && cpu_has_arch_perfmon)
628 return 0;
629
John Villalovos45c34e02010-05-07 12:41:40 -0400630 /*
631 * Documentation on identifying Intel processors by CPU family
632 * and model can be found in the Intel Software Developer's
633 * Manuals (SDM):
634 *
635 * http://www.intel.com/products/processor/manuals/
636 *
637 * As of May 2010 the documentation for this was in the:
638 * "Intel 64 and IA-32 Architectures Software Developer's
639 * Manual Volume 3B: System Programming Guide", "Table B-1
640 * CPUID Signature Values of DisplayFamily_DisplayModel".
641 */
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700642 switch (cpu_model) {
643 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700645 break;
646 case 3 ... 5:
647 *cpu_type = "i386/pii";
648 break;
649 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500650 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700651 *cpu_type = "i386/piii";
652 break;
653 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500654 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700655 *cpu_type = "i386/p6_mobile";
656 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700657 case 14:
658 *cpu_type = "i386/core";
659 break;
Patrick Simmonsc33f5432010-09-08 10:34:28 -0400660 case 0x0f:
661 case 0x16:
662 case 0x17:
Jiri Olsabb7ab782010-09-21 03:26:35 -0400663 case 0x1d:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700664 *cpu_type = "i386/core_2";
665 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400666 case 0x1a:
Josh Hunta7c55cb2010-08-04 20:27:05 -0400667 case 0x1e:
Andi Kleene83e4522010-01-21 23:26:27 +0100668 case 0x2e:
Robert Richter802070f2009-06-12 18:32:07 +0200669 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200670 *cpu_type = "i386/core_i7";
671 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400672 case 0x1c:
Andi Kleen6adf4062009-04-27 17:44:13 +0200673 *cpu_type = "i386/atom";
674 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700675 default:
676 /* Unknown */
677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Robert Richter802070f2009-06-12 18:32:07 +0200680 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 1;
682}
683
David Gibson96d08212005-09-06 15:17:26 -0700684int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 __u8 vendor = boot_cpu_data.x86_vendor;
687 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200688 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200689 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (!cpu_has_apic)
692 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100695 case X86_VENDOR_AMD:
696 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100698 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100699 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100700 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100702 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100703 /*
704 * Actually it could be i386/hammer too, but
705 * give user space an consistent name.
706 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100707 cpu_type = "x86-64/hammer";
708 break;
709 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100710 cpu_type = "x86-64/family10";
711 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200712 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200713 cpu_type = "x86-64/family11h";
714 break;
Robert Richter3acbf0842010-08-31 10:44:17 +0200715 case 0x12:
716 cpu_type = "x86-64/family12h";
717 break;
Robert Richtere6341472010-08-26 12:30:17 +0200718 case 0x14:
719 cpu_type = "x86-64/family14h";
720 break;
Robert Richter30570bc2010-08-31 10:44:38 +0200721 case 0x15:
722 cpu_type = "x86-64/family15h";
723 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100724 default:
725 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100726 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100727 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100730 case X86_VENDOR_INTEL:
731 switch (family) {
732 /* Pentium IV */
733 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200734 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100737 /* A P6-class processor */
738 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200739 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741
742 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200743 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100744 }
Andi Kleenb9917022008-08-18 14:50:31 +0200745
Robert Richtere4192942008-10-12 15:12:34 -0400746 if (cpu_type)
747 break;
748
749 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200750 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400751
752 /* use arch perfmon as fallback */
753 cpu_type = "i386/arch_perfmon";
754 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100755 break;
756
757 default:
758 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Robert Richter270d3e12008-07-22 21:09:01 +0200761 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200762 ops->create_files = nmi_create_files;
763 ops->setup = nmi_setup;
764 ops->shutdown = nmi_shutdown;
765 ops->start = nmi_start;
766 ops->stop = nmi_stop;
767 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200768
Robert Richteradf5ec02008-07-22 21:08:48 +0200769 if (model->init)
770 ret = model->init(ops);
771 if (ret)
772 return ret;
773
Robert Richter52471c62009-07-06 14:43:55 +0200774 if (!model->num_virt_counters)
775 model->num_virt_counters = model->num_counters;
776
Robert Richter52805142009-07-09 16:02:44 +0200777 mux_init(ops);
778
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100779 init_suspend_resume();
Robert Richter10f04122010-08-30 10:56:18 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
782 return 0;
783}
784
David Gibson96d08212005-09-06 15:17:26 -0700785void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100787 exit_suspend_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}