blob: b56601eaf29d5719a7a6706a4854c3405f6da63a [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;
65 int cpu = smp_processor_id();
66
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010067 switch (val) {
Don Zickus2fbe7b22006-09-26 10:52:27 +020068 case DIE_NMI:
Mike Galbraith5b75af02009-02-04 17:11:34 +010069 case DIE_NMI_IPI:
70 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
71 ret = NOTIFY_STOP;
Don Zickus2fbe7b22006-09-26 10:52:27 +020072 break;
73 default:
74 break;
75 }
76 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Don Zickus2fbe7b22006-09-26 10:52:27 +020078
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010079static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010081 struct op_msr *counters = msrs->counters;
82 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned int i;
84
Robert Richter1a245c42009-06-05 15:54:24 +020085 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020086 if (counters[i].addr)
87 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010089
Robert Richter1a245c42009-06-05 15:54:24 +020090 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020091 if (controls[i].addr)
92 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94}
95
Robert Richterb28d1b92009-07-09 14:38:49 +020096static void nmi_cpu_start(void *dummy)
97{
98 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +020099 if (!msrs->controls)
100 WARN_ON_ONCE(1);
101 else
102 model->start(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200103}
104
105static int nmi_start(void)
106{
Robert Richter6ae56b52010-04-29 14:55:55 +0200107 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200108 on_each_cpu(nmi_cpu_start, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200109 ctr_running = 1;
110 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200111 return 0;
112}
113
114static void nmi_cpu_stop(void *dummy)
115{
116 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200117 if (!msrs->controls)
118 WARN_ON_ONCE(1);
119 else
120 model->stop(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200121}
122
123static void nmi_stop(void)
124{
Robert Richter6ae56b52010-04-29 14:55:55 +0200125 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200126 on_each_cpu(nmi_cpu_stop, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200127 ctr_running = 0;
128 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200129}
130
Robert Richterd8471ad2009-07-16 13:04:43 +0200131#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
132
133static DEFINE_PER_CPU(int, switch_index);
134
Robert Richter39e97f42009-07-09 15:11:45 +0200135static inline int has_mux(void)
136{
137 return !!model->switch_ctrl;
138}
139
Robert Richterd8471ad2009-07-16 13:04:43 +0200140inline int op_x86_phys_to_virt(int phys)
141{
142 return __get_cpu_var(switch_index) + phys;
143}
144
Robert Richter61d149d2009-07-10 15:47:17 +0200145inline int op_x86_virt_to_phys(int virt)
146{
147 return virt % model->num_counters;
148}
149
Robert Richter6ab82f92009-07-09 14:40:04 +0200150static void nmi_shutdown_mux(void)
151{
152 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200153
154 if (!has_mux())
155 return;
156
Robert Richter6ab82f92009-07-09 14:40:04 +0200157 for_each_possible_cpu(i) {
158 kfree(per_cpu(cpu_msrs, i).multiplex);
159 per_cpu(cpu_msrs, i).multiplex = NULL;
160 per_cpu(switch_index, i) = 0;
161 }
162}
163
164static int nmi_setup_mux(void)
165{
166 size_t multiplex_size =
167 sizeof(struct op_msr) * model->num_virt_counters;
168 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200169
170 if (!has_mux())
171 return 1;
172
Robert Richter6ab82f92009-07-09 14:40:04 +0200173 for_each_possible_cpu(i) {
174 per_cpu(cpu_msrs, i).multiplex =
Robert Richterc17c8fb2010-02-25 20:20:25 +0100175 kzalloc(multiplex_size, GFP_KERNEL);
Robert Richter6ab82f92009-07-09 14:40:04 +0200176 if (!per_cpu(cpu_msrs, i).multiplex)
177 return 0;
178 }
Robert Richter39e97f42009-07-09 15:11:45 +0200179
Robert Richter6ab82f92009-07-09 14:40:04 +0200180 return 1;
181}
182
Robert Richter48fb4b42009-07-09 14:38:49 +0200183static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
184{
185 int i;
186 struct op_msr *multiplex = msrs->multiplex;
187
Robert Richter39e97f42009-07-09 15:11:45 +0200188 if (!has_mux())
189 return;
190
Robert Richter48fb4b42009-07-09 14:38:49 +0200191 for (i = 0; i < model->num_virt_counters; ++i) {
192 if (counter_config[i].enabled) {
193 multiplex[i].saved = -(u64)counter_config[i].count;
194 } else {
Robert Richter48fb4b42009-07-09 14:38:49 +0200195 multiplex[i].saved = 0;
196 }
197 }
198
199 per_cpu(switch_index, cpu) = 0;
200}
201
Robert Richterd0f585d2009-07-09 14:38:49 +0200202static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
203{
Robert Richter68dc8192010-02-25 19:16:46 +0100204 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200205 struct op_msr *multiplex = msrs->multiplex;
206 int i;
207
208 for (i = 0; i < model->num_counters; ++i) {
209 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100210 if (counters[i].addr)
211 rdmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200212 }
213}
214
215static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
216{
Robert Richter68dc8192010-02-25 19:16:46 +0100217 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200218 struct op_msr *multiplex = msrs->multiplex;
219 int i;
220
221 for (i = 0; i < model->num_counters; ++i) {
222 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100223 if (counters[i].addr)
224 wrmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200225 }
226}
227
Robert Richterb28d1b92009-07-09 14:38:49 +0200228static void nmi_cpu_switch(void *dummy)
229{
230 int cpu = smp_processor_id();
231 int si = per_cpu(switch_index, cpu);
232 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
233
234 nmi_cpu_stop(NULL);
235 nmi_cpu_save_mpx_registers(msrs);
236
237 /* move to next set */
238 si += model->num_counters;
Suravee Suthikulpanitd8cc1082010-01-18 11:25:36 -0600239 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
Robert Richterb28d1b92009-07-09 14:38:49 +0200240 per_cpu(switch_index, cpu) = 0;
241 else
242 per_cpu(switch_index, cpu) = si;
243
244 model->switch_ctrl(model, msrs);
245 nmi_cpu_restore_mpx_registers(msrs);
246
247 nmi_cpu_start(NULL);
248}
249
250
251/*
252 * Quick check to see if multiplexing is necessary.
253 * The check should be sufficient since counters are used
254 * in ordre.
255 */
256static int nmi_multiplex_on(void)
257{
258 return counter_config[model->num_counters].count ? 0 : -EINVAL;
259}
260
261static int nmi_switch_event(void)
262{
Robert Richter39e97f42009-07-09 15:11:45 +0200263 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200264 return -ENOSYS; /* not implemented */
265 if (nmi_multiplex_on() < 0)
266 return -EINVAL; /* not necessary */
267
Robert Richter6ae56b52010-04-29 14:55:55 +0200268 get_online_cpus();
269 if (ctr_running)
270 on_each_cpu(nmi_cpu_switch, NULL, 1);
271 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200272
Robert Richterb28d1b92009-07-09 14:38:49 +0200273 return 0;
274}
275
Robert Richter52805142009-07-09 16:02:44 +0200276static inline void mux_init(struct oprofile_operations *ops)
277{
278 if (has_mux())
279 ops->switch_events = nmi_switch_event;
280}
281
Robert Richter4d015f72009-07-09 21:42:51 +0200282static void mux_clone(int cpu)
283{
284 if (!has_mux())
285 return;
286
287 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
288 per_cpu(cpu_msrs, 0).multiplex,
289 sizeof(struct op_msr) * model->num_virt_counters);
290}
291
Robert Richterd8471ad2009-07-16 13:04:43 +0200292#else
293
294inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200295inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200296static inline void nmi_shutdown_mux(void) { }
297static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200298static inline void
299nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200300static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200301static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200302
303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static void free_msrs(void)
306{
307 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800308 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700309 kfree(per_cpu(cpu_msrs, i).counters);
310 per_cpu(cpu_msrs, i).counters = NULL;
311 kfree(per_cpu(cpu_msrs, i).controls);
312 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100314 nmi_shutdown_mux();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static int allocate_msrs(void)
318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
320 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
321
Robert Richter4c168ea2008-09-24 11:08:52 +0200322 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700323 for_each_possible_cpu(i) {
Robert Richterc17c8fb2010-02-25 20:20:25 +0100324 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200325 GFP_KERNEL);
326 if (!per_cpu(cpu_msrs, i).counters)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100327 goto fail;
Robert Richterc17c8fb2010-02-25 20:20:25 +0100328 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200329 GFP_KERNEL);
330 if (!per_cpu(cpu_msrs, i).controls)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100331 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100334 if (!nmi_setup_mux())
335 goto fail;
336
Robert Richter6ab82f92009-07-09 14:40:04 +0200337 return 1;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100338
339fail:
340 free_msrs();
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100344static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700347 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200348 nmi_cpu_save_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200350 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200351 nmi_cpu_setup_mux(cpu, msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700353 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 apic_write(APIC_LVTPC, APIC_DM_NMI);
355}
356
Don Zickus2fbe7b22006-09-26 10:52:27 +0200357static struct notifier_block profile_exceptions_nb = {
358 .notifier_call = profile_exceptions_notify,
359 .next = NULL,
Mike Galbraith5b75af02009-02-04 17:11:34 +0100360 .priority = 2
Don Zickus2fbe7b22006-09-26 10:52:27 +0200361};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363static int nmi_setup(void)
364{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100365 int err = 0;
Andi Kleen6c977aa2007-05-21 14:31:45 +0200366 int cpu;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!allocate_msrs())
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100369 return -ENOMEM;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200370
Robert Richter4c168ea2008-09-24 11:08:52 +0200371 /* We need to serialize save and setup for HT because the subset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * of msrs are distinct for save and setup operations
373 */
Andi Kleen6c977aa2007-05-21 14:31:45 +0200374
375 /* Assume saved/restored counters are the same on all CPUs */
Robert Richter8617f982010-02-26 17:20:55 +0100376 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
377 if (err)
378 goto fail;
379
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100380 for_each_possible_cpu(cpu) {
Robert Richter4d015f72009-07-09 21:42:51 +0200381 if (!cpu)
382 continue;
Chris Wright0939c172007-06-01 00:46:39 -0700383
Robert Richter4d015f72009-07-09 21:42:51 +0200384 memcpy(per_cpu(cpu_msrs, cpu).counters,
385 per_cpu(cpu_msrs, 0).counters,
386 sizeof(struct op_msr) * model->num_counters);
387
388 memcpy(per_cpu(cpu_msrs, cpu).controls,
389 per_cpu(cpu_msrs, 0).controls,
390 sizeof(struct op_msr) * model->num_controls);
391
392 mux_clone(cpu);
Andi Kleen6c977aa2007-05-21 14:31:45 +0200393 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100394
395 err = register_die_notifier(&profile_exceptions_nb);
396 if (err)
397 goto fail;
398
Robert Richter6ae56b52010-04-29 14:55:55 +0200399 get_online_cpus();
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200400 on_each_cpu(nmi_cpu_setup, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 nmi_enabled = 1;
Robert Richter6ae56b52010-04-29 14:55:55 +0200402 put_online_cpus();
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100405fail:
406 free_msrs();
407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Robert Richter44ab9a62009-07-09 18:33:02 +0200410static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100412 struct op_msr *counters = msrs->counters;
413 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned int i;
415
Robert Richter1a245c42009-06-05 15:54:24 +0200416 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200417 if (controls[i].addr)
418 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100420
Robert Richter1a245c42009-06-05 15:54:24 +0200421 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200422 if (counters[i].addr)
423 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100427static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 unsigned int v;
430 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200431 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* restoring APIC_LVTPC can trigger an apic error because the delivery
434 * mode and vector nr combination can be illegal. That's by design: on
435 * power on apic lvt contain a zero vector nr which are legal only for
436 * NMI delivery mode. So inhibit apic err before restoring lvtpc
437 */
438 v = apic_read(APIC_LVTERR);
439 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700440 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200442 nmi_cpu_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static void nmi_shutdown(void)
446{
Andrea Righib61e06f2008-09-20 18:02:27 +0200447 struct op_msrs *msrs;
448
Robert Richter6ae56b52010-04-29 14:55:55 +0200449 get_online_cpus();
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200450 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200451 nmi_enabled = 0;
452 ctr_running = 0;
453 put_online_cpus();
Don Zickus2fbe7b22006-09-26 10:52:27 +0200454 unregister_die_notifier(&profile_exceptions_nb);
Andrea Righib61e06f2008-09-20 18:02:27 +0200455 msrs = &get_cpu_var(cpu_msrs);
Mike Travisd18d00f2008-03-25 15:06:59 -0700456 model->shutdown(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 free_msrs();
Vegard Nossum93e1ade2008-06-22 09:40:18 +0200458 put_cpu_var(cpu_msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Robert Richter6ae56b52010-04-29 14:55:55 +0200461static void nmi_cpu_up(void *dummy)
462{
463 if (nmi_enabled)
464 nmi_cpu_setup(dummy);
465 if (ctr_running)
466 nmi_cpu_start(dummy);
467}
468
469static void nmi_cpu_down(void *dummy)
470{
471 if (ctr_running)
472 nmi_cpu_stop(dummy);
473 if (nmi_enabled)
474 nmi_cpu_shutdown(dummy);
475}
476
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100477static int nmi_create_files(struct super_block *sb, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 unsigned int i;
480
Jason Yeh4d4036e2009-07-08 13:49:38 +0200481 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100482 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700483 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100484
485 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200486 * available for use. This should protect userspace app.
487 * NOTE: assumes 1:1 mapping here (that counters are organized
488 * sequentially in their struct assignment).
489 */
Robert Richter11be1a72009-07-10 18:15:21 +0200490 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200491 continue;
492
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700493 snprintf(buf, sizeof(buf), "%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 dir = oprofilefs_mkdir(sb, root, buf);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100495 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
496 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
497 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
498 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
499 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
500 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 return 0;
504}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100505
Robert Richter69046d42008-09-05 12:17:40 +0200506static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
507 void *data)
508{
509 int cpu = (unsigned long)data;
510 switch (action) {
511 case CPU_DOWN_FAILED:
512 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200513 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200514 break;
515 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200516 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200517 break;
518 }
519 return NOTIFY_DONE;
520}
521
522static struct notifier_block oprofile_cpu_nb = {
523 .notifier_call = oprofile_cpu_notifier
524};
Robert Richter69046d42008-09-05 12:17:40 +0200525
526#ifdef CONFIG_PM
527
528static int nmi_suspend(struct sys_device *dev, pm_message_t state)
529{
530 /* Only one CPU left, just stop that one */
531 if (nmi_enabled == 1)
532 nmi_cpu_stop(NULL);
533 return 0;
534}
535
536static int nmi_resume(struct sys_device *dev)
537{
538 if (nmi_enabled == 1)
539 nmi_cpu_start(NULL);
540 return 0;
541}
542
543static struct sysdev_class oprofile_sysclass = {
544 .name = "oprofile",
545 .resume = nmi_resume,
546 .suspend = nmi_suspend,
547};
548
549static struct sys_device device_oprofile = {
550 .id = 0,
551 .cls = &oprofile_sysclass,
552};
553
554static int __init init_sysfs(void)
555{
556 int error;
557
558 error = sysdev_class_register(&oprofile_sysclass);
559 if (!error)
560 error = sysdev_register(&device_oprofile);
561 return error;
562}
563
564static void exit_sysfs(void)
565{
566 sysdev_unregister(&device_oprofile);
567 sysdev_class_unregister(&oprofile_sysclass);
568}
569
570#else
571#define init_sysfs() do { } while (0)
572#define exit_sysfs() do { } while (0)
573#endif /* CONFIG_PM */
574
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100575static int __init p4_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 __u8 cpu_model = boot_cpu_data.x86_model;
578
Andi Kleen1f3d7b62009-04-27 17:44:12 +0200579 if (cpu_model > 6 || cpu_model == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581
582#ifndef CONFIG_SMP
583 *cpu_type = "i386/p4";
584 model = &op_p4_spec;
585 return 1;
586#else
587 switch (smp_num_siblings) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100588 case 1:
589 *cpu_type = "i386/p4";
590 model = &op_p4_spec;
591 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100593 case 2:
594 *cpu_type = "i386/p4-ht";
595 model = &op_p4_ht2_spec;
596 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598#endif
599
600 printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
601 printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
602 return 0;
603}
604
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200605static int force_arch_perfmon;
606static int force_cpu_type(const char *str, struct kernel_param *kp)
607{
Robert Richter8d7ff4f2009-06-23 11:48:14 +0200608 if (!strcmp(str, "arch_perfmon")) {
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200609 force_arch_perfmon = 1;
610 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
611 }
612
613 return 0;
614}
615module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200616
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100617static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200620 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200622 if (force_arch_perfmon && cpu_has_arch_perfmon)
623 return 0;
624
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700625 switch (cpu_model) {
626 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700628 break;
629 case 3 ... 5:
630 *cpu_type = "i386/pii";
631 break;
632 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500633 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700634 *cpu_type = "i386/piii";
635 break;
636 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500637 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700638 *cpu_type = "i386/p6_mobile";
639 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700640 case 14:
641 *cpu_type = "i386/core";
642 break;
643 case 15: case 23:
644 *cpu_type = "i386/core_2";
645 break;
Andi Kleene83e4522010-01-21 23:26:27 +0100646 case 0x2e:
Andi Kleen6adf4062009-04-27 17:44:13 +0200647 case 26:
Robert Richter802070f2009-06-12 18:32:07 +0200648 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200649 *cpu_type = "i386/core_i7";
650 break;
651 case 28:
652 *cpu_type = "i386/atom";
653 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700654 default:
655 /* Unknown */
656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Robert Richter802070f2009-06-12 18:32:07 +0200659 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 1;
661}
662
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100663/* in order to get sysfs right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static int using_nmi;
665
David Gibson96d08212005-09-06 15:17:26 -0700666int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 __u8 vendor = boot_cpu_data.x86_vendor;
669 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200670 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200671 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!cpu_has_apic)
674 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100677 case X86_VENDOR_AMD:
678 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100680 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100681 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100682 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100684 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100685 /*
686 * Actually it could be i386/hammer too, but
687 * give user space an consistent name.
688 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100689 cpu_type = "x86-64/hammer";
690 break;
691 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100692 cpu_type = "x86-64/family10";
693 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200694 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200695 cpu_type = "x86-64/family11h";
696 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100697 default:
698 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100699 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100700 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100703 case X86_VENDOR_INTEL:
704 switch (family) {
705 /* Pentium IV */
706 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200707 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100710 /* A P6-class processor */
711 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200712 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 break;
714
715 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200716 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100717 }
Andi Kleenb9917022008-08-18 14:50:31 +0200718
Robert Richtere4192942008-10-12 15:12:34 -0400719 if (cpu_type)
720 break;
721
722 if (!cpu_has_arch_perfmon)
Andi Kleenb9917022008-08-18 14:50:31 +0200723 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400724
725 /* use arch perfmon as fallback */
726 cpu_type = "i386/arch_perfmon";
727 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100728 break;
729
730 default:
731 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
Robert Richter6ae56b52010-04-29 14:55:55 +0200734 get_online_cpus();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200735 register_cpu_notifier(&oprofile_cpu_nb);
Robert Richter6ae56b52010-04-29 14:55:55 +0200736 nmi_enabled = 0;
737 ctr_running = 0;
738 put_online_cpus();
Robert Richter216f3d92010-05-03 11:58:46 +0200739
Robert Richter270d3e12008-07-22 21:09:01 +0200740 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200741 ops->create_files = nmi_create_files;
742 ops->setup = nmi_setup;
743 ops->shutdown = nmi_shutdown;
744 ops->start = nmi_start;
745 ops->stop = nmi_stop;
746 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200747
Robert Richteradf5ec02008-07-22 21:08:48 +0200748 if (model->init)
749 ret = model->init(ops);
750 if (ret)
751 return ret;
752
Robert Richter52471c62009-07-06 14:43:55 +0200753 if (!model->num_virt_counters)
754 model->num_virt_counters = model->num_counters;
755
Robert Richter52805142009-07-09 16:02:44 +0200756 mux_init(ops);
757
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100758 init_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 using_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
761 return 0;
762}
763
David Gibson96d08212005-09-06 15:17:26 -0700764void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200766 if (using_nmi) {
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100767 exit_sysfs();
Robert Richter6ae56b52010-04-29 14:55:55 +0200768 get_online_cpus();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200769 unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richter6ae56b52010-04-29 14:55:55 +0200770 nmi_enabled = 0;
771 ctr_running = 0;
772 put_online_cpus();
Andi Kleen80a8c9f2008-08-19 03:13:38 +0200773 }
Robert Richteradf5ec02008-07-22 21:08:48 +0200774 if (model->exit)
775 model->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}