blob: 18f9dad8dc793c07ba2f39c805b21fd9912d9322 [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;
Dan Carpenter4400910502012-10-10 10:18:35 +030058 val |= (u64)(event & 0x0F00) << 24;
Robert Richter3370d352009-05-25 15:10:32 +020059
60 return val;
61}
62
63
Don Zickus9c48f1c2011-09-30 15:06:21 -040064static int profile_exceptions_notify(unsigned int val, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Don Zickus9c48f1c2011-09-30 15:06:21 -040066 if (ctr_running)
Christoph Lameter89cbc762014-08-17 12:30:40 -050067 model->check_ctrs(regs, this_cpu_ptr(&cpu_msrs));
Don Zickus9c48f1c2011-09-30 15:06:21 -040068 else if (!nmi_enabled)
69 return NMI_DONE;
70 else
Christoph Lameter89cbc762014-08-17 12:30:40 -050071 model->stop(this_cpu_ptr(&cpu_msrs));
Don Zickus9c48f1c2011-09-30 15:06:21 -040072 return NMI_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Don Zickus2fbe7b22006-09-26 10:52:27 +020074
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010075static void nmi_cpu_save_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010077 struct op_msr *counters = msrs->counters;
78 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned int i;
80
Robert Richter1a245c42009-06-05 15:54:24 +020081 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020082 if (counters[i].addr)
83 rdmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +010085
Robert Richter1a245c42009-06-05 15:54:24 +020086 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +020087 if (controls[i].addr)
88 rdmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90}
91
Robert Richterb28d1b92009-07-09 14:38:49 +020092static void nmi_cpu_start(void *dummy)
93{
Christoph Lameter89cbc762014-08-17 12:30:40 -050094 struct op_msrs const *msrs = this_cpu_ptr(&cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +020095 if (!msrs->controls)
96 WARN_ON_ONCE(1);
97 else
98 model->start(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +020099}
100
101static int nmi_start(void)
102{
Robert Richter6ae56b52010-04-29 14:55:55 +0200103 get_online_cpus();
Robert Richter6ae56b52010-04-29 14:55:55 +0200104 ctr_running = 1;
Robert Richter8fe7e942011-06-01 15:31:44 +0200105 /* make ctr_running visible to the nmi handler: */
106 smp_mb();
107 on_each_cpu(nmi_cpu_start, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200108 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200109 return 0;
110}
111
112static void nmi_cpu_stop(void *dummy)
113{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500114 struct op_msrs const *msrs = this_cpu_ptr(&cpu_msrs);
Robert Richter2623a1d2010-05-03 19:44:32 +0200115 if (!msrs->controls)
116 WARN_ON_ONCE(1);
117 else
118 model->stop(msrs);
Robert Richterb28d1b92009-07-09 14:38:49 +0200119}
120
121static void nmi_stop(void)
122{
Robert Richter6ae56b52010-04-29 14:55:55 +0200123 get_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200124 on_each_cpu(nmi_cpu_stop, NULL, 1);
Robert Richter6ae56b52010-04-29 14:55:55 +0200125 ctr_running = 0;
126 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200127}
128
Robert Richterd8471ad2009-07-16 13:04:43 +0200129#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
130
131static DEFINE_PER_CPU(int, switch_index);
132
Robert Richter39e97f42009-07-09 15:11:45 +0200133static inline int has_mux(void)
134{
135 return !!model->switch_ctrl;
136}
137
Robert Richterd8471ad2009-07-16 13:04:43 +0200138inline int op_x86_phys_to_virt(int phys)
139{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100140 return __this_cpu_read(switch_index) + phys;
Robert Richterd8471ad2009-07-16 13:04:43 +0200141}
142
Robert Richter61d149d2009-07-10 15:47:17 +0200143inline int op_x86_virt_to_phys(int virt)
144{
145 return virt % model->num_counters;
146}
147
Robert Richter6ab82f92009-07-09 14:40:04 +0200148static void nmi_shutdown_mux(void)
149{
150 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200151
152 if (!has_mux())
153 return;
154
Robert Richter6ab82f92009-07-09 14:40:04 +0200155 for_each_possible_cpu(i) {
156 kfree(per_cpu(cpu_msrs, i).multiplex);
157 per_cpu(cpu_msrs, i).multiplex = NULL;
158 per_cpu(switch_index, i) = 0;
159 }
160}
161
162static int nmi_setup_mux(void)
163{
164 size_t multiplex_size =
165 sizeof(struct op_msr) * model->num_virt_counters;
166 int i;
Robert Richter39e97f42009-07-09 15:11:45 +0200167
168 if (!has_mux())
169 return 1;
170
Robert Richter6ab82f92009-07-09 14:40:04 +0200171 for_each_possible_cpu(i) {
172 per_cpu(cpu_msrs, i).multiplex =
Robert Richterc17c8fb2010-02-25 20:20:25 +0100173 kzalloc(multiplex_size, GFP_KERNEL);
Robert Richter6ab82f92009-07-09 14:40:04 +0200174 if (!per_cpu(cpu_msrs, i).multiplex)
175 return 0;
176 }
Robert Richter39e97f42009-07-09 15:11:45 +0200177
Robert Richter6ab82f92009-07-09 14:40:04 +0200178 return 1;
179}
180
Robert Richter48fb4b42009-07-09 14:38:49 +0200181static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs)
182{
183 int i;
184 struct op_msr *multiplex = msrs->multiplex;
185
Robert Richter39e97f42009-07-09 15:11:45 +0200186 if (!has_mux())
187 return;
188
Robert Richter48fb4b42009-07-09 14:38:49 +0200189 for (i = 0; i < model->num_virt_counters; ++i) {
190 if (counter_config[i].enabled) {
191 multiplex[i].saved = -(u64)counter_config[i].count;
192 } else {
Robert Richter48fb4b42009-07-09 14:38:49 +0200193 multiplex[i].saved = 0;
194 }
195 }
196
197 per_cpu(switch_index, cpu) = 0;
198}
199
Robert Richterd0f585d2009-07-09 14:38:49 +0200200static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs)
201{
Robert Richter68dc8192010-02-25 19:16:46 +0100202 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200203 struct op_msr *multiplex = msrs->multiplex;
204 int i;
205
206 for (i = 0; i < model->num_counters; ++i) {
207 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100208 if (counters[i].addr)
209 rdmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200210 }
211}
212
213static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs)
214{
Robert Richter68dc8192010-02-25 19:16:46 +0100215 struct op_msr *counters = msrs->counters;
Robert Richterd0f585d2009-07-09 14:38:49 +0200216 struct op_msr *multiplex = msrs->multiplex;
217 int i;
218
219 for (i = 0; i < model->num_counters; ++i) {
220 int virt = op_x86_phys_to_virt(i);
Robert Richter68dc8192010-02-25 19:16:46 +0100221 if (counters[i].addr)
222 wrmsrl(counters[i].addr, multiplex[virt].saved);
Robert Richterd0f585d2009-07-09 14:38:49 +0200223 }
224}
225
Robert Richterb28d1b92009-07-09 14:38:49 +0200226static void nmi_cpu_switch(void *dummy)
227{
228 int cpu = smp_processor_id();
229 int si = per_cpu(switch_index, cpu);
230 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
231
232 nmi_cpu_stop(NULL);
233 nmi_cpu_save_mpx_registers(msrs);
234
235 /* move to next set */
236 si += model->num_counters;
Suravee Suthikulpanitd8cc1082010-01-18 11:25:36 -0600237 if ((si >= model->num_virt_counters) || (counter_config[si].count == 0))
Robert Richterb28d1b92009-07-09 14:38:49 +0200238 per_cpu(switch_index, cpu) = 0;
239 else
240 per_cpu(switch_index, cpu) = si;
241
242 model->switch_ctrl(model, msrs);
243 nmi_cpu_restore_mpx_registers(msrs);
244
245 nmi_cpu_start(NULL);
246}
247
248
249/*
250 * Quick check to see if multiplexing is necessary.
251 * The check should be sufficient since counters are used
252 * in ordre.
253 */
254static int nmi_multiplex_on(void)
255{
256 return counter_config[model->num_counters].count ? 0 : -EINVAL;
257}
258
259static int nmi_switch_event(void)
260{
Robert Richter39e97f42009-07-09 15:11:45 +0200261 if (!has_mux())
Robert Richterb28d1b92009-07-09 14:38:49 +0200262 return -ENOSYS; /* not implemented */
263 if (nmi_multiplex_on() < 0)
264 return -EINVAL; /* not necessary */
265
Robert Richter6ae56b52010-04-29 14:55:55 +0200266 get_online_cpus();
267 if (ctr_running)
268 on_each_cpu(nmi_cpu_switch, NULL, 1);
269 put_online_cpus();
Robert Richterb28d1b92009-07-09 14:38:49 +0200270
Robert Richterb28d1b92009-07-09 14:38:49 +0200271 return 0;
272}
273
Robert Richter52805142009-07-09 16:02:44 +0200274static inline void mux_init(struct oprofile_operations *ops)
275{
276 if (has_mux())
277 ops->switch_events = nmi_switch_event;
278}
279
Robert Richter4d015f72009-07-09 21:42:51 +0200280static void mux_clone(int cpu)
281{
282 if (!has_mux())
283 return;
284
285 memcpy(per_cpu(cpu_msrs, cpu).multiplex,
286 per_cpu(cpu_msrs, 0).multiplex,
287 sizeof(struct op_msr) * model->num_virt_counters);
288}
289
Robert Richterd8471ad2009-07-16 13:04:43 +0200290#else
291
292inline int op_x86_phys_to_virt(int phys) { return phys; }
Robert Richter61d149d2009-07-10 15:47:17 +0200293inline int op_x86_virt_to_phys(int virt) { return virt; }
Robert Richter6ab82f92009-07-09 14:40:04 +0200294static inline void nmi_shutdown_mux(void) { }
295static inline int nmi_setup_mux(void) { return 1; }
Robert Richter48fb4b42009-07-09 14:38:49 +0200296static inline void
297nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { }
Robert Richter52805142009-07-09 16:02:44 +0200298static inline void mux_init(struct oprofile_operations *ops) { }
Robert Richter4d015f72009-07-09 21:42:51 +0200299static void mux_clone(int cpu) { }
Robert Richterd8471ad2009-07-16 13:04:43 +0200300
301#endif
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static void free_msrs(void)
304{
305 int i;
KAMEZAWA Hiroyukic89125992006-03-28 01:56:39 -0800306 for_each_possible_cpu(i) {
Mike Travisd18d00f2008-03-25 15:06:59 -0700307 kfree(per_cpu(cpu_msrs, i).counters);
308 per_cpu(cpu_msrs, i).counters = NULL;
309 kfree(per_cpu(cpu_msrs, i).controls);
310 per_cpu(cpu_msrs, i).controls = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100312 nmi_shutdown_mux();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static int allocate_msrs(void)
316{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 size_t controls_size = sizeof(struct op_msr) * model->num_controls;
318 size_t counters_size = sizeof(struct op_msr) * model->num_counters;
319
Robert Richter4c168ea2008-09-24 11:08:52 +0200320 int i;
Chris Wright0939c172007-06-01 00:46:39 -0700321 for_each_possible_cpu(i) {
Robert Richterc17c8fb2010-02-25 20:20:25 +0100322 per_cpu(cpu_msrs, i).counters = kzalloc(counters_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200323 GFP_KERNEL);
324 if (!per_cpu(cpu_msrs, i).counters)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100325 goto fail;
Robert Richterc17c8fb2010-02-25 20:20:25 +0100326 per_cpu(cpu_msrs, i).controls = kzalloc(controls_size,
Robert Richter6ab82f92009-07-09 14:40:04 +0200327 GFP_KERNEL);
328 if (!per_cpu(cpu_msrs, i).controls)
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100329 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100332 if (!nmi_setup_mux())
333 goto fail;
334
Robert Richter6ab82f92009-07-09 14:40:04 +0200335 return 1;
Robert Richter8f5a2dd2010-03-23 19:09:51 +0100336
337fail:
338 free_msrs();
339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100342static void nmi_cpu_setup(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 int cpu = smp_processor_id();
Mike Travisd18d00f2008-03-25 15:06:59 -0700345 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Robert Richter44ab9a62009-07-09 18:33:02 +0200346 nmi_cpu_save_registers(msrs);
Thomas Gleixner2d21a292009-07-25 16:18:34 +0200347 raw_spin_lock(&oprofilefs_lock);
Robert Richteref8828d2009-05-25 19:31:44 +0200348 model->setup_ctrs(model, msrs);
Robert Richter6bfccd02009-07-09 19:23:50 +0200349 nmi_cpu_setup_mux(cpu, msrs);
Thomas Gleixner2d21a292009-07-25 16:18:34 +0200350 raw_spin_unlock(&oprofilefs_lock);
Mike Travisd18d00f2008-03-25 15:06:59 -0700351 per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 apic_write(APIC_LVTPC, APIC_DM_NMI);
353}
354
Robert Richter44ab9a62009-07-09 18:33:02 +0200355static void nmi_cpu_restore_registers(struct op_msrs *msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100357 struct op_msr *counters = msrs->counters;
358 struct op_msr *controls = msrs->controls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned int i;
360
Robert Richter1a245c42009-06-05 15:54:24 +0200361 for (i = 0; i < model->num_controls; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200362 if (controls[i].addr)
363 wrmsrl(controls[i].addr, controls[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100365
Robert Richter1a245c42009-06-05 15:54:24 +0200366 for (i = 0; i < model->num_counters; ++i) {
Robert Richter95e74e62009-06-03 19:09:27 +0200367 if (counters[i].addr)
368 wrmsrl(counters[i].addr, counters[i].saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100372static void nmi_cpu_shutdown(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 unsigned int v;
375 int cpu = smp_processor_id();
Robert Richter82a22522009-07-09 16:29:34 +0200376 struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* restoring APIC_LVTPC can trigger an apic error because the delivery
379 * mode and vector nr combination can be illegal. That's by design: on
380 * power on apic lvt contain a zero vector nr which are legal only for
381 * NMI delivery mode. So inhibit apic err before restoring lvtpc
382 */
383 v = apic_read(APIC_LVTERR);
384 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Mike Travisd18d00f2008-03-25 15:06:59 -0700385 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 apic_write(APIC_LVTERR, v);
Robert Richter44ab9a62009-07-09 18:33:02 +0200387 nmi_cpu_restore_registers(msrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Robert Richter6ae56b52010-04-29 14:55:55 +0200390static void nmi_cpu_up(void *dummy)
391{
392 if (nmi_enabled)
393 nmi_cpu_setup(dummy);
394 if (ctr_running)
395 nmi_cpu_start(dummy);
396}
397
398static void nmi_cpu_down(void *dummy)
399{
400 if (ctr_running)
401 nmi_cpu_stop(dummy);
402 if (nmi_enabled)
403 nmi_cpu_shutdown(dummy);
404}
405
Al Viroef7bca12013-07-19 15:52:42 +0400406static int nmi_create_files(struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 unsigned int i;
409
Jason Yeh4d4036e2009-07-08 13:49:38 +0200410 for (i = 0; i < model->num_virt_counters; ++i) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100411 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700412 char buf[4];
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100413
414 /* quick little hack to _not_ expose a counter if it is not
Don Zickuscb9c4482006-09-26 10:52:26 +0200415 * available for use. This should protect userspace app.
416 * NOTE: assumes 1:1 mapping here (that counters are organized
417 * sequentially in their struct assignment).
418 */
Robert Richter11be1a72009-07-10 18:15:21 +0200419 if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200420 continue;
421
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700422 snprintf(buf, sizeof(buf), "%d", i);
Al Viroecde2822013-07-19 15:58:27 +0400423 dir = oprofilefs_mkdir(root, buf);
Al Viro6af4ea02013-07-19 16:10:36 +0400424 oprofilefs_create_ulong(dir, "enabled", &counter_config[i].enabled);
425 oprofilefs_create_ulong(dir, "event", &counter_config[i].event);
426 oprofilefs_create_ulong(dir, "count", &counter_config[i].count);
427 oprofilefs_create_ulong(dir, "unit_mask", &counter_config[i].unit_mask);
428 oprofilefs_create_ulong(dir, "kernel", &counter_config[i].kernel);
429 oprofilefs_create_ulong(dir, "user", &counter_config[i].user);
430 oprofilefs_create_ulong(dir, "extra", &counter_config[i].extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 return 0;
434}
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100435
Robert Richter69046d42008-09-05 12:17:40 +0200436static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
437 void *data)
438{
439 int cpu = (unsigned long)data;
Thomas Gleixner57d335c2016-03-19 12:28:38 +0100440
441 switch (action & ~CPU_TASKS_FROZEN) {
Robert Richter69046d42008-09-05 12:17:40 +0200442 case CPU_DOWN_FAILED:
443 case CPU_ONLINE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200444 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
Robert Richter69046d42008-09-05 12:17:40 +0200445 break;
446 case CPU_DOWN_PREPARE:
Robert Richter6ae56b52010-04-29 14:55:55 +0200447 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
Robert Richter69046d42008-09-05 12:17:40 +0200448 break;
449 }
450 return NOTIFY_DONE;
451}
452
453static struct notifier_block oprofile_cpu_nb = {
454 .notifier_call = oprofile_cpu_notifier
455};
Robert Richter69046d42008-09-05 12:17:40 +0200456
Robert Richterd30d64c2010-05-03 15:52:26 +0200457static int nmi_setup(void)
458{
459 int err = 0;
460 int cpu;
461
462 if (!allocate_msrs())
463 return -ENOMEM;
464
465 /* We need to serialize save and setup for HT because the subset
466 * of msrs are distinct for save and setup operations
467 */
468
469 /* Assume saved/restored counters are the same on all CPUs */
470 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
471 if (err)
472 goto fail;
473
474 for_each_possible_cpu(cpu) {
Arnd Bergmanndcc92a12018-02-20 21:58:21 +0100475 if (!IS_ENABLED(CONFIG_SMP) || !cpu)
Robert Richterd30d64c2010-05-03 15:52:26 +0200476 continue;
477
478 memcpy(per_cpu(cpu_msrs, cpu).counters,
479 per_cpu(cpu_msrs, 0).counters,
480 sizeof(struct op_msr) * model->num_counters);
481
482 memcpy(per_cpu(cpu_msrs, cpu).controls,
483 per_cpu(cpu_msrs, 0).controls,
484 sizeof(struct op_msr) * model->num_controls);
485
486 mux_clone(cpu);
487 }
488
489 nmi_enabled = 0;
490 ctr_running = 0;
Robert Richter8fe7e942011-06-01 15:31:44 +0200491 /* make variables visible to the nmi handler: */
492 smp_mb();
Don Zickus9c48f1c2011-09-30 15:06:21 -0400493 err = register_nmi_handler(NMI_LOCAL, profile_exceptions_notify,
494 0, "oprofile");
Robert Richterd30d64c2010-05-03 15:52:26 +0200495 if (err)
496 goto fail;
497
Srivatsa S. Bhat76902e32014-03-11 02:08:49 +0530498 cpu_notifier_register_begin();
499
500 /* Use get/put_online_cpus() to protect 'nmi_enabled' */
Robert Richterd30d64c2010-05-03 15:52:26 +0200501 get_online_cpus();
Robert Richterd30d64c2010-05-03 15:52:26 +0200502 nmi_enabled = 1;
Robert Richter8fe7e942011-06-01 15:31:44 +0200503 /* make nmi_enabled visible to the nmi handler: */
504 smp_mb();
505 on_each_cpu(nmi_cpu_setup, NULL, 1);
Srivatsa S. Bhat76902e32014-03-11 02:08:49 +0530506 __register_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200507 put_online_cpus();
508
Srivatsa S. Bhat76902e32014-03-11 02:08:49 +0530509 cpu_notifier_register_done();
510
Robert Richterd30d64c2010-05-03 15:52:26 +0200511 return 0;
512fail:
513 free_msrs();
514 return err;
515}
516
517static void nmi_shutdown(void)
518{
519 struct op_msrs *msrs;
520
Srivatsa S. Bhat76902e32014-03-11 02:08:49 +0530521 cpu_notifier_register_begin();
522
523 /* Use get/put_online_cpus() to protect 'nmi_enabled' & 'ctr_running' */
Robert Richterd30d64c2010-05-03 15:52:26 +0200524 get_online_cpus();
525 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
526 nmi_enabled = 0;
527 ctr_running = 0;
Srivatsa S. Bhat76902e32014-03-11 02:08:49 +0530528 __unregister_cpu_notifier(&oprofile_cpu_nb);
Robert Richterd30d64c2010-05-03 15:52:26 +0200529 put_online_cpus();
Srivatsa S. Bhat76902e32014-03-11 02:08:49 +0530530
531 cpu_notifier_register_done();
532
Robert Richter8fe7e942011-06-01 15:31:44 +0200533 /* make variables visible to the nmi handler: */
534 smp_mb();
Don Zickus9c48f1c2011-09-30 15:06:21 -0400535 unregister_nmi_handler(NMI_LOCAL, "oprofile");
Robert Richterd30d64c2010-05-03 15:52:26 +0200536 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 Richter159a80b2011-10-11 19:39:16 +0200610enum __force_cpu_type {
611 reserved = 0, /* do not force */
612 timer,
613 arch_perfmon,
614};
615
616static int force_cpu_type;
617
Kees Cook24da2c82017-10-17 19:04:42 -0700618static int set_cpu_type(const char *str, const struct kernel_param *kp)
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200619{
Robert Richter159a80b2011-10-11 19:39:16 +0200620 if (!strcmp(str, "timer")) {
621 force_cpu_type = timer;
622 printk(KERN_INFO "oprofile: forcing NMI timer mode\n");
623 } else if (!strcmp(str, "arch_perfmon")) {
624 force_cpu_type = arch_perfmon;
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200625 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
Robert Richter159a80b2011-10-11 19:39:16 +0200626 } else {
627 force_cpu_type = 0;
Robert Richter7e4e0bd2009-05-06 12:10:23 +0200628 }
629
630 return 0;
631}
Robert Richter159a80b2011-10-11 19:39:16 +0200632module_param_call(cpu_type, set_cpu_type, NULL, NULL, 0);
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200633
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100634static int __init ppro_init(char **cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 __u8 cpu_model = boot_cpu_data.x86_model;
Robert Richter259a83a2009-07-09 15:12:35 +0200637 struct op_x86_model_spec *spec = &op_ppro_spec; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Borislav Petkov7b5e74e2016-03-29 17:41:54 +0200639 if (force_cpu_type == arch_perfmon && boot_cpu_has(X86_FEATURE_ARCH_PERFMON))
Andi Kleen1dcdb5a2009-04-27 17:44:11 +0200640 return 0;
641
John Villalovos45c34e02010-05-07 12:41:40 -0400642 /*
643 * Documentation on identifying Intel processors by CPU family
644 * and model can be found in the Intel Software Developer's
645 * Manuals (SDM):
646 *
647 * http://www.intel.com/products/processor/manuals/
648 *
649 * As of May 2010 the documentation for this was in the:
650 * "Intel 64 and IA-32 Architectures Software Developer's
651 * Manual Volume 3B: System Programming Guide", "Table B-1
652 * CPUID Signature Values of DisplayFamily_DisplayModel".
653 */
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700654 switch (cpu_model) {
655 case 0 ... 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *cpu_type = "i386/ppro";
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700657 break;
658 case 3 ... 5:
659 *cpu_type = "i386/pii";
660 break;
661 case 6 ... 8:
William Cohen3d337c62008-11-30 15:39:10 -0500662 case 10 ... 11:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700663 *cpu_type = "i386/piii";
664 break;
665 case 9:
William Cohen3d337c62008-11-30 15:39:10 -0500666 case 13:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700667 *cpu_type = "i386/p6_mobile";
668 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700669 case 14:
670 *cpu_type = "i386/core";
671 break;
Patrick Simmonsc33f5432010-09-08 10:34:28 -0400672 case 0x0f:
673 case 0x16:
674 case 0x17:
Jiri Olsabb7ab782010-09-21 03:26:35 -0400675 case 0x1d:
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700676 *cpu_type = "i386/core_2";
677 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400678 case 0x1a:
Josh Hunta7c55cb2010-08-04 20:27:05 -0400679 case 0x1e:
Andi Kleene83e4522010-01-21 23:26:27 +0100680 case 0x2e:
Robert Richter802070f2009-06-12 18:32:07 +0200681 spec = &op_arch_perfmon_spec;
Andi Kleen6adf4062009-04-27 17:44:13 +0200682 *cpu_type = "i386/core_i7";
683 break;
John Villalovos45c34e02010-05-07 12:41:40 -0400684 case 0x1c:
Andi Kleen6adf4062009-04-27 17:44:13 +0200685 *cpu_type = "i386/atom";
686 break;
Linus Torvalds4b9f12a2008-07-24 17:29:00 -0700687 default:
688 /* Unknown */
689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Robert Richter802070f2009-06-12 18:32:07 +0200692 model = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 1;
694}
695
David Gibson96d08212005-09-06 15:17:26 -0700696int __init op_nmi_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 __u8 vendor = boot_cpu_data.x86_vendor;
699 __u8 family = boot_cpu_data.x86;
Andi Kleenb9917022008-08-18 14:50:31 +0200700 char *cpu_type = NULL;
Robert Richteradf5ec02008-07-22 21:08:48 +0200701 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Borislav Petkov93984fb2016-04-04 22:25:00 +0200703 if (!boot_cpu_has(X86_FEATURE_APIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100705
Robert Richter159a80b2011-10-11 19:39:16 +0200706 if (force_cpu_type == timer)
707 return -ENODEV;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 switch (vendor) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100710 case X86_VENDOR_AMD:
711 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100713 switch (family) {
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100714 case 6:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100715 cpu_type = "i386/athlon";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100717 case 0xf:
Robert Richterd20f24c2009-01-11 13:01:16 +0100718 /*
719 * Actually it could be i386/hammer too, but
720 * give user space an consistent name.
721 */
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100722 cpu_type = "x86-64/hammer";
723 break;
724 case 0x10:
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100725 cpu_type = "x86-64/family10";
726 break;
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200727 case 0x11:
Barry Kasindorf12f2b262008-07-22 21:08:47 +0200728 cpu_type = "x86-64/family11h";
729 break;
Robert Richter3acbf0842010-08-31 10:44:17 +0200730 case 0x12:
731 cpu_type = "x86-64/family12h";
732 break;
Robert Richtere6341472010-08-26 12:30:17 +0200733 case 0x14:
734 cpu_type = "x86-64/family14h";
735 break;
Robert Richter30570bc2010-08-31 10:44:38 +0200736 case 0x15:
737 cpu_type = "x86-64/family15h";
738 break;
Robert Richterd20f24c2009-01-11 13:01:16 +0100739 default:
740 return -ENODEV;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100741 }
Robert Richterd20f24c2009-01-11 13:01:16 +0100742 model = &op_amd_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100745 case X86_VENDOR_INTEL:
746 switch (family) {
747 /* Pentium IV */
748 case 0xf:
Andi Kleenb9917022008-08-18 14:50:31 +0200749 p4_init(&cpu_type);
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100750 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100752 /* A P6-class processor */
753 case 6:
Andi Kleenb9917022008-08-18 14:50:31 +0200754 ppro_init(&cpu_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756
757 default:
Andi Kleenb9917022008-08-18 14:50:31 +0200758 break;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100759 }
Andi Kleenb9917022008-08-18 14:50:31 +0200760
Robert Richtere4192942008-10-12 15:12:34 -0400761 if (cpu_type)
762 break;
763
Borislav Petkov7b5e74e2016-03-29 17:41:54 +0200764 if (!boot_cpu_has(X86_FEATURE_ARCH_PERFMON))
Andi Kleenb9917022008-08-18 14:50:31 +0200765 return -ENODEV;
Robert Richtere4192942008-10-12 15:12:34 -0400766
767 /* use arch perfmon as fallback */
768 cpu_type = "i386/arch_perfmon";
769 model = &op_arch_perfmon_spec;
Carlos R. Mafrab75f53d2008-01-30 13:32:33 +0100770 break;
771
772 default:
773 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Robert Richter270d3e12008-07-22 21:09:01 +0200776 /* default values, can be overwritten by model */
Robert Richter6e63ea42009-07-07 19:25:39 +0200777 ops->create_files = nmi_create_files;
778 ops->setup = nmi_setup;
779 ops->shutdown = nmi_shutdown;
780 ops->start = nmi_start;
781 ops->stop = nmi_stop;
782 ops->cpu_type = cpu_type;
Robert Richter270d3e12008-07-22 21:09:01 +0200783
Robert Richteradf5ec02008-07-22 21:08:48 +0200784 if (model->init)
785 ret = model->init(ops);
786 if (ret)
787 return ret;
788
Robert Richter52471c62009-07-06 14:43:55 +0200789 if (!model->num_virt_counters)
790 model->num_virt_counters = model->num_counters;
791
Robert Richter52805142009-07-09 16:02:44 +0200792 mux_init(ops);
793
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100794 init_suspend_resume();
Robert Richter10f04122010-08-30 10:56:18 +0200795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 printk(KERN_INFO "oprofile: using NMI interrupt.\n");
797 return 0;
798}
799
David Gibson96d08212005-09-06 15:17:26 -0700800void op_nmi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100802 exit_suspend_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}