Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /** |
2 | * @file nmi_int.c | |
3 | * | |
4d4036e0 | 4 | * @remark Copyright 2002-2009 OProfile authors |
1da177e4 LT |
5 | * @remark Read the file COPYING |
6 | * | |
7 | * @author John Levon <levon@movementarian.org> | |
adf5ec0b | 8 | * @author Robert Richter <robert.richter@amd.com> |
4d4036e0 JY |
9 | * @author Barry Kasindorf <barry.kasindorf@amd.com> |
10 | * @author Jason Yeh <jason.yeh@amd.com> | |
11 | * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> | |
1da177e4 LT |
12 | */ |
13 | ||
14 | #include <linux/init.h> | |
15 | #include <linux/notifier.h> | |
16 | #include <linux/smp.h> | |
17 | #include <linux/oprofile.h> | |
f3c6ea1b | 18 | #include <linux/syscore_ops.h> |
1da177e4 | 19 | #include <linux/slab.h> |
1cfcea1b | 20 | #include <linux/moduleparam.h> |
1eeb66a1 | 21 | #include <linux/kdebug.h> |
80a8c9ff | 22 | #include <linux/cpu.h> |
1da177e4 LT |
23 | #include <asm/nmi.h> |
24 | #include <asm/msr.h> | |
25 | #include <asm/apic.h> | |
b75f53db | 26 | |
1da177e4 LT |
27 | #include "op_counter.h" |
28 | #include "op_x86_model.h" | |
2fbe7b25 | 29 | |
259a83a8 | 30 | static struct op_x86_model_spec *model; |
d18d00f5 MT |
31 | static DEFINE_PER_CPU(struct op_msrs, cpu_msrs); |
32 | static DEFINE_PER_CPU(unsigned long, saved_lvtpc); | |
2fbe7b25 | 33 | |
6ae56b55 RR |
34 | /* must be protected with get_online_cpus()/put_online_cpus(): */ |
35 | static int nmi_enabled; | |
36 | static int ctr_running; | |
1da177e4 | 37 | |
4d4036e0 JY |
38 | struct op_counter_config counter_config[OP_MAX_COUNTER]; |
39 | ||
3370d358 RR |
40 | /* common functions */ |
41 | ||
42 | u64 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; | |
914a76ca AK |
52 | counter_config->extra &= (ARCH_PERFMON_EVENTSEL_INV | |
53 | ARCH_PERFMON_EVENTSEL_EDGE | | |
54 | ARCH_PERFMON_EVENTSEL_CMASK); | |
55 | val |= counter_config->extra; | |
3370d358 RR |
56 | event &= model->event_mask ? model->event_mask : 0xFF; |
57 | val |= event & 0xFF; | |
44009105 | 58 | val |= (u64)(event & 0x0F00) << 24; |
3370d358 RR |
59 | |
60 | return val; | |
61 | } | |
62 | ||
63 | ||
9c48f1c6 DZ |
64 | static int profile_exceptions_notify(unsigned int val, struct pt_regs *regs) |
65 | { | |
66 | if (ctr_running) | |
89cbc767 | 67 | model->check_ctrs(regs, this_cpu_ptr(&cpu_msrs)); |
9c48f1c6 DZ |
68 | else if (!nmi_enabled) |
69 | return NMI_DONE; | |
70 | else | |
89cbc767 | 71 | model->stop(this_cpu_ptr(&cpu_msrs)); |
9c48f1c6 | 72 | return NMI_HANDLED; |
1da177e4 | 73 | } |
2fbe7b25 | 74 | |
b75f53db | 75 | static void nmi_cpu_save_registers(struct op_msrs *msrs) |
1da177e4 | 76 | { |
b75f53db CM |
77 | struct op_msr *counters = msrs->counters; |
78 | struct op_msr *controls = msrs->controls; | |
1da177e4 LT |
79 | unsigned int i; |
80 | ||
1a245c45 | 81 | for (i = 0; i < model->num_counters; ++i) { |
95e74e62 RR |
82 | if (counters[i].addr) |
83 | rdmsrl(counters[i].addr, counters[i].saved); | |
1da177e4 | 84 | } |
b75f53db | 85 | |
1a245c45 | 86 | for (i = 0; i < model->num_controls; ++i) { |
95e74e62 RR |
87 | if (controls[i].addr) |
88 | rdmsrl(controls[i].addr, controls[i].saved); | |
1da177e4 LT |
89 | } |
90 | } | |
91 | ||
b28d1b92 RR |
92 | static void nmi_cpu_start(void *dummy) |
93 | { | |
89cbc767 | 94 | struct op_msrs const *msrs = this_cpu_ptr(&cpu_msrs); |
2623a1d5 RR |
95 | if (!msrs->controls) |
96 | WARN_ON_ONCE(1); | |
97 | else | |
98 | model->start(msrs); | |
b28d1b92 RR |
99 | } |
100 | ||
101 | static int nmi_start(void) | |
102 | { | |
6ae56b55 | 103 | get_online_cpus(); |
6ae56b55 | 104 | ctr_running = 1; |
8fe7e94e RR |
105 | /* make ctr_running visible to the nmi handler: */ |
106 | smp_mb(); | |
107 | on_each_cpu(nmi_cpu_start, NULL, 1); | |
6ae56b55 | 108 | put_online_cpus(); |
b28d1b92 RR |
109 | return 0; |
110 | } | |
111 | ||
112 | static void nmi_cpu_stop(void *dummy) | |
113 | { | |
89cbc767 | 114 | struct op_msrs const *msrs = this_cpu_ptr(&cpu_msrs); |
2623a1d5 RR |
115 | if (!msrs->controls) |
116 | WARN_ON_ONCE(1); | |
117 | else | |
118 | model->stop(msrs); | |
b28d1b92 RR |
119 | } |
120 | ||
121 | static void nmi_stop(void) | |
122 | { | |
6ae56b55 | 123 | get_online_cpus(); |
b28d1b92 | 124 | on_each_cpu(nmi_cpu_stop, NULL, 1); |
6ae56b55 RR |
125 | ctr_running = 0; |
126 | put_online_cpus(); | |
b28d1b92 RR |
127 | } |
128 | ||
d8471ad3 RR |
129 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
130 | ||
131 | static DEFINE_PER_CPU(int, switch_index); | |
132 | ||
39e97f40 RR |
133 | static inline int has_mux(void) |
134 | { | |
135 | return !!model->switch_ctrl; | |
136 | } | |
137 | ||
d8471ad3 RR |
138 | inline int op_x86_phys_to_virt(int phys) |
139 | { | |
0a3aee0d | 140 | return __this_cpu_read(switch_index) + phys; |
d8471ad3 RR |
141 | } |
142 | ||
61d149d5 RR |
143 | inline int op_x86_virt_to_phys(int virt) |
144 | { | |
145 | return virt % model->num_counters; | |
146 | } | |
147 | ||
6ab82f95 RR |
148 | static void nmi_shutdown_mux(void) |
149 | { | |
150 | int i; | |
39e97f40 RR |
151 | |
152 | if (!has_mux()) | |
153 | return; | |
154 | ||
6ab82f95 RR |
155 | 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 | ||
162 | static int nmi_setup_mux(void) | |
163 | { | |
164 | size_t multiplex_size = | |
165 | sizeof(struct op_msr) * model->num_virt_counters; | |
166 | int i; | |
39e97f40 RR |
167 | |
168 | if (!has_mux()) | |
169 | return 1; | |
170 | ||
6ab82f95 RR |
171 | for_each_possible_cpu(i) { |
172 | per_cpu(cpu_msrs, i).multiplex = | |
c17c8fbf | 173 | kzalloc(multiplex_size, GFP_KERNEL); |
6ab82f95 RR |
174 | if (!per_cpu(cpu_msrs, i).multiplex) |
175 | return 0; | |
176 | } | |
39e97f40 | 177 | |
6ab82f95 RR |
178 | return 1; |
179 | } | |
180 | ||
48fb4b46 RR |
181 | static 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 | ||
39e97f40 RR |
186 | if (!has_mux()) |
187 | return; | |
188 | ||
48fb4b46 RR |
189 | 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 { | |
48fb4b46 RR |
193 | multiplex[i].saved = 0; |
194 | } | |
195 | } | |
196 | ||
197 | per_cpu(switch_index, cpu) = 0; | |
198 | } | |
199 | ||
d0f585dd RR |
200 | static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs) |
201 | { | |
68dc819c | 202 | struct op_msr *counters = msrs->counters; |
d0f585dd RR |
203 | 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); | |
68dc819c RR |
208 | if (counters[i].addr) |
209 | rdmsrl(counters[i].addr, multiplex[virt].saved); | |
d0f585dd RR |
210 | } |
211 | } | |
212 | ||
213 | static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs) | |
214 | { | |
68dc819c | 215 | struct op_msr *counters = msrs->counters; |
d0f585dd RR |
216 | 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); | |
68dc819c RR |
221 | if (counters[i].addr) |
222 | wrmsrl(counters[i].addr, multiplex[virt].saved); | |
d0f585dd RR |
223 | } |
224 | } | |
225 | ||
b28d1b92 RR |
226 | static 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; | |
d8cc108f | 237 | if ((si >= model->num_virt_counters) || (counter_config[si].count == 0)) |
b28d1b92 RR |
238 | 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 | */ | |
254 | static int nmi_multiplex_on(void) | |
255 | { | |
256 | return counter_config[model->num_counters].count ? 0 : -EINVAL; | |
257 | } | |
258 | ||
259 | static int nmi_switch_event(void) | |
260 | { | |
39e97f40 | 261 | if (!has_mux()) |
b28d1b92 RR |
262 | return -ENOSYS; /* not implemented */ |
263 | if (nmi_multiplex_on() < 0) | |
264 | return -EINVAL; /* not necessary */ | |
265 | ||
6ae56b55 RR |
266 | get_online_cpus(); |
267 | if (ctr_running) | |
268 | on_each_cpu(nmi_cpu_switch, NULL, 1); | |
269 | put_online_cpus(); | |
b28d1b92 | 270 | |
b28d1b92 RR |
271 | return 0; |
272 | } | |
273 | ||
52805144 RR |
274 | static inline void mux_init(struct oprofile_operations *ops) |
275 | { | |
276 | if (has_mux()) | |
277 | ops->switch_events = nmi_switch_event; | |
278 | } | |
279 | ||
4d015f79 RR |
280 | static 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 | ||
d8471ad3 RR |
290 | #else |
291 | ||
292 | inline int op_x86_phys_to_virt(int phys) { return phys; } | |
61d149d5 | 293 | inline int op_x86_virt_to_phys(int virt) { return virt; } |
6ab82f95 RR |
294 | static inline void nmi_shutdown_mux(void) { } |
295 | static inline int nmi_setup_mux(void) { return 1; } | |
48fb4b46 RR |
296 | static inline void |
297 | nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { } | |
52805144 | 298 | static inline void mux_init(struct oprofile_operations *ops) { } |
4d015f79 | 299 | static void mux_clone(int cpu) { } |
d8471ad3 RR |
300 | |
301 | #endif | |
302 | ||
1da177e4 LT |
303 | static void free_msrs(void) |
304 | { | |
305 | int i; | |
c8912599 | 306 | for_each_possible_cpu(i) { |
d18d00f5 MT |
307 | 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; | |
1da177e4 | 311 | } |
8f5a2dd8 | 312 | nmi_shutdown_mux(); |
1da177e4 LT |
313 | } |
314 | ||
1da177e4 LT |
315 | static int allocate_msrs(void) |
316 | { | |
1da177e4 LT |
317 | size_t controls_size = sizeof(struct op_msr) * model->num_controls; |
318 | size_t counters_size = sizeof(struct op_msr) * model->num_counters; | |
319 | ||
4c168eaf | 320 | int i; |
0939c17c | 321 | for_each_possible_cpu(i) { |
c17c8fbf | 322 | per_cpu(cpu_msrs, i).counters = kzalloc(counters_size, |
6ab82f95 RR |
323 | GFP_KERNEL); |
324 | if (!per_cpu(cpu_msrs, i).counters) | |
8f5a2dd8 | 325 | goto fail; |
c17c8fbf | 326 | per_cpu(cpu_msrs, i).controls = kzalloc(controls_size, |
6ab82f95 RR |
327 | GFP_KERNEL); |
328 | if (!per_cpu(cpu_msrs, i).controls) | |
8f5a2dd8 | 329 | goto fail; |
1da177e4 LT |
330 | } |
331 | ||
8f5a2dd8 RR |
332 | if (!nmi_setup_mux()) |
333 | goto fail; | |
334 | ||
6ab82f95 | 335 | return 1; |
8f5a2dd8 RR |
336 | |
337 | fail: | |
338 | free_msrs(); | |
339 | return 0; | |
1da177e4 LT |
340 | } |
341 | ||
b75f53db | 342 | static void nmi_cpu_setup(void *dummy) |
1da177e4 LT |
343 | { |
344 | int cpu = smp_processor_id(); | |
d18d00f5 | 345 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
44ab9a6b | 346 | nmi_cpu_save_registers(msrs); |
2d21a29f | 347 | raw_spin_lock(&oprofilefs_lock); |
ef8828dd | 348 | model->setup_ctrs(model, msrs); |
6bfccd09 | 349 | nmi_cpu_setup_mux(cpu, msrs); |
2d21a29f | 350 | raw_spin_unlock(&oprofilefs_lock); |
d18d00f5 | 351 | per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC); |
1da177e4 LT |
352 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
353 | } | |
354 | ||
44ab9a6b | 355 | static void nmi_cpu_restore_registers(struct op_msrs *msrs) |
1da177e4 | 356 | { |
b75f53db CM |
357 | struct op_msr *counters = msrs->counters; |
358 | struct op_msr *controls = msrs->controls; | |
1da177e4 LT |
359 | unsigned int i; |
360 | ||
1a245c45 | 361 | for (i = 0; i < model->num_controls; ++i) { |
95e74e62 RR |
362 | if (controls[i].addr) |
363 | wrmsrl(controls[i].addr, controls[i].saved); | |
1da177e4 | 364 | } |
b75f53db | 365 | |
1a245c45 | 366 | for (i = 0; i < model->num_counters; ++i) { |
95e74e62 RR |
367 | if (counters[i].addr) |
368 | wrmsrl(counters[i].addr, counters[i].saved); | |
1da177e4 LT |
369 | } |
370 | } | |
1da177e4 | 371 | |
b75f53db | 372 | static void nmi_cpu_shutdown(void *dummy) |
1da177e4 LT |
373 | { |
374 | unsigned int v; | |
375 | int cpu = smp_processor_id(); | |
82a22528 | 376 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
b75f53db | 377 | |
1da177e4 LT |
378 | /* 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); | |
d18d00f5 | 385 | apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu)); |
1da177e4 | 386 | apic_write(APIC_LVTERR, v); |
44ab9a6b | 387 | nmi_cpu_restore_registers(msrs); |
1da177e4 LT |
388 | } |
389 | ||
6ae56b55 RR |
390 | static 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 | ||
398 | static 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 | ||
ef7bca14 | 406 | static int nmi_create_files(struct dentry *root) |
1da177e4 LT |
407 | { |
408 | unsigned int i; | |
409 | ||
4d4036e0 | 410 | for (i = 0; i < model->num_virt_counters; ++i) { |
b75f53db | 411 | struct dentry *dir; |
0c6856f7 | 412 | char buf[4]; |
b75f53db CM |
413 | |
414 | /* quick little hack to _not_ expose a counter if it is not | |
cb9c448c DZ |
415 | * 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 | */ | |
11be1a7b | 419 | if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i))) |
cb9c448c DZ |
420 | continue; |
421 | ||
0c6856f7 | 422 | snprintf(buf, sizeof(buf), "%d", i); |
ecde2823 | 423 | dir = oprofilefs_mkdir(root, buf); |
6af4ea0b AV |
424 | 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); | |
1da177e4 LT |
431 | } |
432 | ||
433 | return 0; | |
434 | } | |
b75f53db | 435 | |
69046d43 RR |
436 | static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action, |
437 | void *data) | |
438 | { | |
439 | int cpu = (unsigned long)data; | |
57d335ce TG |
440 | |
441 | switch (action & ~CPU_TASKS_FROZEN) { | |
69046d43 RR |
442 | case CPU_DOWN_FAILED: |
443 | case CPU_ONLINE: | |
6ae56b55 | 444 | smp_call_function_single(cpu, nmi_cpu_up, NULL, 0); |
69046d43 RR |
445 | break; |
446 | case CPU_DOWN_PREPARE: | |
6ae56b55 | 447 | smp_call_function_single(cpu, nmi_cpu_down, NULL, 1); |
69046d43 RR |
448 | break; |
449 | } | |
450 | return NOTIFY_DONE; | |
451 | } | |
452 | ||
453 | static struct notifier_block oprofile_cpu_nb = { | |
454 | .notifier_call = oprofile_cpu_notifier | |
455 | }; | |
69046d43 | 456 | |
d30d64c6 RR |
457 | static 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) { | |
475 | if (!cpu) | |
476 | 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; | |
8fe7e94e RR |
491 | /* make variables visible to the nmi handler: */ |
492 | smp_mb(); | |
9c48f1c6 DZ |
493 | err = register_nmi_handler(NMI_LOCAL, profile_exceptions_notify, |
494 | 0, "oprofile"); | |
d30d64c6 RR |
495 | if (err) |
496 | goto fail; | |
497 | ||
76902e3d SB |
498 | cpu_notifier_register_begin(); |
499 | ||
500 | /* Use get/put_online_cpus() to protect 'nmi_enabled' */ | |
d30d64c6 | 501 | get_online_cpus(); |
d30d64c6 | 502 | nmi_enabled = 1; |
8fe7e94e RR |
503 | /* make nmi_enabled visible to the nmi handler: */ |
504 | smp_mb(); | |
505 | on_each_cpu(nmi_cpu_setup, NULL, 1); | |
76902e3d | 506 | __register_cpu_notifier(&oprofile_cpu_nb); |
d30d64c6 RR |
507 | put_online_cpus(); |
508 | ||
76902e3d SB |
509 | cpu_notifier_register_done(); |
510 | ||
d30d64c6 RR |
511 | return 0; |
512 | fail: | |
513 | free_msrs(); | |
514 | return err; | |
515 | } | |
516 | ||
517 | static void nmi_shutdown(void) | |
518 | { | |
519 | struct op_msrs *msrs; | |
520 | ||
76902e3d SB |
521 | cpu_notifier_register_begin(); |
522 | ||
523 | /* Use get/put_online_cpus() to protect 'nmi_enabled' & 'ctr_running' */ | |
d30d64c6 RR |
524 | get_online_cpus(); |
525 | on_each_cpu(nmi_cpu_shutdown, NULL, 1); | |
526 | nmi_enabled = 0; | |
527 | ctr_running = 0; | |
76902e3d | 528 | __unregister_cpu_notifier(&oprofile_cpu_nb); |
d30d64c6 | 529 | put_online_cpus(); |
76902e3d SB |
530 | |
531 | cpu_notifier_register_done(); | |
532 | ||
8fe7e94e RR |
533 | /* make variables visible to the nmi handler: */ |
534 | smp_mb(); | |
9c48f1c6 | 535 | unregister_nmi_handler(NMI_LOCAL, "oprofile"); |
d30d64c6 RR |
536 | msrs = &get_cpu_var(cpu_msrs); |
537 | model->shutdown(msrs); | |
538 | free_msrs(); | |
539 | put_cpu_var(cpu_msrs); | |
540 | } | |
541 | ||
69046d43 RR |
542 | #ifdef CONFIG_PM |
543 | ||
f3c6ea1b | 544 | static int nmi_suspend(void) |
69046d43 RR |
545 | { |
546 | /* Only one CPU left, just stop that one */ | |
547 | if (nmi_enabled == 1) | |
548 | nmi_cpu_stop(NULL); | |
549 | return 0; | |
550 | } | |
551 | ||
f3c6ea1b | 552 | static void nmi_resume(void) |
69046d43 RR |
553 | { |
554 | if (nmi_enabled == 1) | |
555 | nmi_cpu_start(NULL); | |
69046d43 RR |
556 | } |
557 | ||
f3c6ea1b | 558 | static struct syscore_ops oprofile_syscore_ops = { |
69046d43 RR |
559 | .resume = nmi_resume, |
560 | .suspend = nmi_suspend, | |
561 | }; | |
562 | ||
f3c6ea1b | 563 | static void __init init_suspend_resume(void) |
69046d43 | 564 | { |
f3c6ea1b | 565 | register_syscore_ops(&oprofile_syscore_ops); |
69046d43 RR |
566 | } |
567 | ||
f3c6ea1b | 568 | static void exit_suspend_resume(void) |
69046d43 | 569 | { |
f3c6ea1b | 570 | unregister_syscore_ops(&oprofile_syscore_ops); |
69046d43 RR |
571 | } |
572 | ||
573 | #else | |
269f45c2 | 574 | |
f3c6ea1b RW |
575 | static inline void init_suspend_resume(void) { } |
576 | static inline void exit_suspend_resume(void) { } | |
269f45c2 | 577 | |
69046d43 RR |
578 | #endif /* CONFIG_PM */ |
579 | ||
b75f53db | 580 | static int __init p4_init(char **cpu_type) |
1da177e4 LT |
581 | { |
582 | __u8 cpu_model = boot_cpu_data.x86_model; | |
583 | ||
1f3d7b60 | 584 | if (cpu_model > 6 || cpu_model == 5) |
1da177e4 LT |
585 | 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) { | |
b75f53db CM |
593 | case 1: |
594 | *cpu_type = "i386/p4"; | |
595 | model = &op_p4_spec; | |
596 | return 1; | |
597 | ||
598 | case 2: | |
599 | *cpu_type = "i386/p4-ht"; | |
600 | model = &op_p4_ht2_spec; | |
601 | return 1; | |
1da177e4 LT |
602 | } |
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 | ||
159a80b2 RR |
610 | enum __force_cpu_type { |
611 | reserved = 0, /* do not force */ | |
612 | timer, | |
613 | arch_perfmon, | |
614 | }; | |
615 | ||
616 | static int force_cpu_type; | |
617 | ||
618 | static int set_cpu_type(const char *str, struct kernel_param *kp) | |
7e4e0bd5 | 619 | { |
159a80b2 RR |
620 | 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; | |
7e4e0bd5 | 625 | printk(KERN_INFO "oprofile: forcing architectural perfmon\n"); |
159a80b2 RR |
626 | } else { |
627 | force_cpu_type = 0; | |
7e4e0bd5 RR |
628 | } |
629 | ||
630 | return 0; | |
631 | } | |
159a80b2 | 632 | module_param_call(cpu_type, set_cpu_type, NULL, NULL, 0); |
1dcdb5a9 | 633 | |
b75f53db | 634 | static int __init ppro_init(char **cpu_type) |
1da177e4 LT |
635 | { |
636 | __u8 cpu_model = boot_cpu_data.x86_model; | |
259a83a8 | 637 | struct op_x86_model_spec *spec = &op_ppro_spec; /* default */ |
1da177e4 | 638 | |
7b5e74e6 | 639 | if (force_cpu_type == arch_perfmon && boot_cpu_has(X86_FEATURE_ARCH_PERFMON)) |
1dcdb5a9 AK |
640 | return 0; |
641 | ||
45c34e05 JV |
642 | /* |
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 | */ | |
4b9f12a3 LT |
654 | switch (cpu_model) { |
655 | case 0 ... 2: | |
656 | *cpu_type = "i386/ppro"; | |
657 | break; | |
658 | case 3 ... 5: | |
659 | *cpu_type = "i386/pii"; | |
660 | break; | |
661 | case 6 ... 8: | |
3d337c65 | 662 | case 10 ... 11: |
4b9f12a3 LT |
663 | *cpu_type = "i386/piii"; |
664 | break; | |
665 | case 9: | |
3d337c65 | 666 | case 13: |
4b9f12a3 LT |
667 | *cpu_type = "i386/p6_mobile"; |
668 | break; | |
4b9f12a3 | 669 | case 14: |
64471ebe | 670 | *cpu_type = "i386/core"; |
4b9f12a3 | 671 | break; |
c33f543d PS |
672 | case 0x0f: |
673 | case 0x16: | |
674 | case 0x17: | |
bb7ab785 | 675 | case 0x1d: |
4b9f12a3 LT |
676 | *cpu_type = "i386/core_2"; |
677 | break; | |
45c34e05 | 678 | case 0x1a: |
a7c55cbe | 679 | case 0x1e: |
e83e452b | 680 | case 0x2e: |
802070f5 | 681 | spec = &op_arch_perfmon_spec; |
6adf406f AK |
682 | *cpu_type = "i386/core_i7"; |
683 | break; | |
45c34e05 | 684 | case 0x1c: |
6adf406f AK |
685 | *cpu_type = "i386/atom"; |
686 | break; | |
4b9f12a3 LT |
687 | default: |
688 | /* Unknown */ | |
1da177e4 | 689 | return 0; |
1da177e4 LT |
690 | } |
691 | ||
802070f5 | 692 | model = spec; |
1da177e4 LT |
693 | return 1; |
694 | } | |
695 | ||
96d0821c | 696 | int __init op_nmi_init(struct oprofile_operations *ops) |
1da177e4 LT |
697 | { |
698 | __u8 vendor = boot_cpu_data.x86_vendor; | |
699 | __u8 family = boot_cpu_data.x86; | |
b9917028 | 700 | char *cpu_type = NULL; |
adf5ec0b | 701 | int ret = 0; |
1da177e4 LT |
702 | |
703 | if (!cpu_has_apic) | |
704 | return -ENODEV; | |
b75f53db | 705 | |
159a80b2 RR |
706 | if (force_cpu_type == timer) |
707 | return -ENODEV; | |
708 | ||
1da177e4 | 709 | switch (vendor) { |
b75f53db CM |
710 | case X86_VENDOR_AMD: |
711 | /* Needs to be at least an Athlon (or hammer in 32bit mode) */ | |
1da177e4 | 712 | |
b75f53db | 713 | switch (family) { |
b75f53db | 714 | case 6: |
b75f53db CM |
715 | cpu_type = "i386/athlon"; |
716 | break; | |
717 | case 0xf: | |
d20f24c6 RR |
718 | /* |
719 | * Actually it could be i386/hammer too, but | |
720 | * give user space an consistent name. | |
721 | */ | |
b75f53db CM |
722 | cpu_type = "x86-64/hammer"; |
723 | break; | |
724 | case 0x10: | |
b75f53db CM |
725 | cpu_type = "x86-64/family10"; |
726 | break; | |
12f2b261 | 727 | case 0x11: |
12f2b261 BK |
728 | cpu_type = "x86-64/family11h"; |
729 | break; | |
3acbf084 RR |
730 | case 0x12: |
731 | cpu_type = "x86-64/family12h"; | |
732 | break; | |
e6341474 RR |
733 | case 0x14: |
734 | cpu_type = "x86-64/family14h"; | |
735 | break; | |
30570bce RR |
736 | case 0x15: |
737 | cpu_type = "x86-64/family15h"; | |
738 | break; | |
d20f24c6 RR |
739 | default: |
740 | return -ENODEV; | |
b75f53db | 741 | } |
d20f24c6 | 742 | model = &op_amd_spec; |
b75f53db CM |
743 | break; |
744 | ||
745 | case X86_VENDOR_INTEL: | |
746 | switch (family) { | |
747 | /* Pentium IV */ | |
748 | case 0xf: | |
b9917028 | 749 | p4_init(&cpu_type); |
1da177e4 | 750 | break; |
b75f53db CM |
751 | |
752 | /* A P6-class processor */ | |
753 | case 6: | |
b9917028 | 754 | ppro_init(&cpu_type); |
1da177e4 LT |
755 | break; |
756 | ||
757 | default: | |
b9917028 | 758 | break; |
b75f53db | 759 | } |
b9917028 | 760 | |
e419294e RR |
761 | if (cpu_type) |
762 | break; | |
763 | ||
7b5e74e6 | 764 | if (!boot_cpu_has(X86_FEATURE_ARCH_PERFMON)) |
b9917028 | 765 | return -ENODEV; |
e419294e RR |
766 | |
767 | /* use arch perfmon as fallback */ | |
768 | cpu_type = "i386/arch_perfmon"; | |
769 | model = &op_arch_perfmon_spec; | |
b75f53db CM |
770 | break; |
771 | ||
772 | default: | |
773 | return -ENODEV; | |
1da177e4 LT |
774 | } |
775 | ||
270d3e1a | 776 | /* default values, can be overwritten by model */ |
6e63ea4b RR |
777 | 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; | |
270d3e1a | 783 | |
adf5ec0b RR |
784 | if (model->init) |
785 | ret = model->init(ops); | |
786 | if (ret) | |
787 | return ret; | |
788 | ||
52471c67 RR |
789 | if (!model->num_virt_counters) |
790 | model->num_virt_counters = model->num_counters; | |
791 | ||
52805144 RR |
792 | mux_init(ops); |
793 | ||
f3c6ea1b | 794 | init_suspend_resume(); |
10f0412f | 795 | |
1da177e4 LT |
796 | printk(KERN_INFO "oprofile: using NMI interrupt.\n"); |
797 | return 0; | |
798 | } | |
799 | ||
96d0821c | 800 | void op_nmi_exit(void) |
1da177e4 | 801 | { |
f3c6ea1b | 802 | exit_suspend_resume(); |
1da177e4 | 803 | } |