7826dfcc842823a7a893b4e2831c34864597b1c7
[safe/jmp/linux-2.6] / arch / x86 / oprofile / nmi_int.c
1 /**
2  * @file nmi_int.c
3  *
4  * @remark Copyright 2002-2008 OProfile authors
5  * @remark Read the file COPYING
6  *
7  * @author John Levon <levon@movementarian.org>
8  * @author Robert Richter <robert.richter@amd.com>
9  */
10
11 #include <linux/init.h>
12 #include <linux/notifier.h>
13 #include <linux/smp.h>
14 #include <linux/oprofile.h>
15 #include <linux/sysdev.h>
16 #include <linux/slab.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kdebug.h>
19 #include <linux/cpu.h>
20 #include <asm/nmi.h>
21 #include <asm/msr.h>
22 #include <asm/apic.h>
23
24 #include "op_counter.h"
25 #include "op_x86_model.h"
26
27 static struct op_x86_model_spec const *model;
28 static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
29 static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
30
31 /* 0 == registered but off, 1 == registered and on */
32 static int nmi_enabled = 0;
33
34 /* common functions */
35
36 u64 op_x86_get_ctrl(struct op_x86_model_spec const *model,
37                     struct op_counter_config *counter_config)
38 {
39         u64 val = 0;
40         u16 event = (u16)counter_config->event;
41
42         val |= ARCH_PERFMON_EVENTSEL_INT;
43         val |= counter_config->user ? ARCH_PERFMON_EVENTSEL_USR : 0;
44         val |= counter_config->kernel ? ARCH_PERFMON_EVENTSEL_OS : 0;
45         val |= (counter_config->unit_mask & 0xFF) << 8;
46         event &= model->event_mask ? model->event_mask : 0xFF;
47         val |= event & 0xFF;
48         val |= (event & 0x0F00) << 24;
49
50         return val;
51 }
52
53
54 static int profile_exceptions_notify(struct notifier_block *self,
55                                      unsigned long val, void *data)
56 {
57         struct die_args *args = (struct die_args *)data;
58         int ret = NOTIFY_DONE;
59         int cpu = smp_processor_id();
60
61         switch (val) {
62         case DIE_NMI:
63         case DIE_NMI_IPI:
64                 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu));
65                 ret = NOTIFY_STOP;
66                 break;
67         default:
68                 break;
69         }
70         return ret;
71 }
72
73 static void nmi_cpu_save_registers(struct op_msrs *msrs)
74 {
75         struct op_msr *counters = msrs->counters;
76         struct op_msr *controls = msrs->controls;
77         unsigned int i;
78
79         for (i = 0; i < model->num_counters; ++i) {
80                 if (counters[i].addr)
81                         rdmsrl(counters[i].addr, counters[i].saved);
82         }
83
84         for (i = 0; i < model->num_controls; ++i) {
85                 if (controls[i].addr)
86                         rdmsrl(controls[i].addr, controls[i].saved);
87         }
88 }
89
90 static void nmi_save_registers(void *dummy)
91 {
92         int cpu = smp_processor_id();
93         struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
94         nmi_cpu_save_registers(msrs);
95 }
96
97 static void free_msrs(void)
98 {
99         int i;
100         for_each_possible_cpu(i) {
101                 kfree(per_cpu(cpu_msrs, i).counters);
102                 per_cpu(cpu_msrs, i).counters = NULL;
103                 kfree(per_cpu(cpu_msrs, i).controls);
104                 per_cpu(cpu_msrs, i).controls = NULL;
105         }
106 }
107
108 static int allocate_msrs(void)
109 {
110         int success = 1;
111         size_t controls_size = sizeof(struct op_msr) * model->num_controls;
112         size_t counters_size = sizeof(struct op_msr) * model->num_counters;
113
114         int i;
115         for_each_possible_cpu(i) {
116                 per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
117                                                                 GFP_KERNEL);
118                 if (!per_cpu(cpu_msrs, i).counters) {
119                         success = 0;
120                         break;
121                 }
122                 per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
123                                                                 GFP_KERNEL);
124                 if (!per_cpu(cpu_msrs, i).controls) {
125                         success = 0;
126                         break;
127                 }
128         }
129
130         if (!success)
131                 free_msrs();
132
133         return success;
134 }
135
136 static void nmi_cpu_setup(void *dummy)
137 {
138         int cpu = smp_processor_id();
139         struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
140         spin_lock(&oprofilefs_lock);
141         model->setup_ctrs(model, msrs);
142         spin_unlock(&oprofilefs_lock);
143         per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
144         apic_write(APIC_LVTPC, APIC_DM_NMI);
145 }
146
147 static struct notifier_block profile_exceptions_nb = {
148         .notifier_call = profile_exceptions_notify,
149         .next = NULL,
150         .priority = 2
151 };
152
153 static int nmi_setup(void)
154 {
155         int err = 0;
156         int cpu;
157
158         if (!allocate_msrs())
159                 return -ENOMEM;
160
161         err = register_die_notifier(&profile_exceptions_nb);
162         if (err) {
163                 free_msrs();
164                 return err;
165         }
166
167         /* We need to serialize save and setup for HT because the subset
168          * of msrs are distinct for save and setup operations
169          */
170
171         /* Assume saved/restored counters are the same on all CPUs */
172         model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
173         for_each_possible_cpu(cpu) {
174                 if (cpu != 0) {
175                         memcpy(per_cpu(cpu_msrs, cpu).counters,
176                                 per_cpu(cpu_msrs, 0).counters,
177                                 sizeof(struct op_msr) * model->num_counters);
178
179                         memcpy(per_cpu(cpu_msrs, cpu).controls,
180                                 per_cpu(cpu_msrs, 0).controls,
181                                 sizeof(struct op_msr) * model->num_controls);
182                 }
183
184         }
185         on_each_cpu(nmi_save_registers, NULL, 1);
186         on_each_cpu(nmi_cpu_setup, NULL, 1);
187         nmi_enabled = 1;
188         return 0;
189 }
190
191 static void nmi_restore_registers(struct op_msrs *msrs)
192 {
193         struct op_msr *counters = msrs->counters;
194         struct op_msr *controls = msrs->controls;
195         unsigned int i;
196
197         for (i = 0; i < model->num_controls; ++i) {
198                 if (controls[i].addr)
199                         wrmsrl(controls[i].addr, controls[i].saved);
200         }
201
202         for (i = 0; i < model->num_counters; ++i) {
203                 if (counters[i].addr)
204                         wrmsrl(counters[i].addr, counters[i].saved);
205         }
206 }
207
208 static void nmi_cpu_shutdown(void *dummy)
209 {
210         unsigned int v;
211         int cpu = smp_processor_id();
212         struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
213
214         /* restoring APIC_LVTPC can trigger an apic error because the delivery
215          * mode and vector nr combination can be illegal. That's by design: on
216          * power on apic lvt contain a zero vector nr which are legal only for
217          * NMI delivery mode. So inhibit apic err before restoring lvtpc
218          */
219         v = apic_read(APIC_LVTERR);
220         apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
221         apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
222         apic_write(APIC_LVTERR, v);
223         nmi_restore_registers(msrs);
224 }
225
226 static void nmi_shutdown(void)
227 {
228         struct op_msrs *msrs;
229
230         nmi_enabled = 0;
231         on_each_cpu(nmi_cpu_shutdown, NULL, 1);
232         unregister_die_notifier(&profile_exceptions_nb);
233         msrs = &get_cpu_var(cpu_msrs);
234         model->shutdown(msrs);
235         free_msrs();
236         put_cpu_var(cpu_msrs);
237 }
238
239 static void nmi_cpu_start(void *dummy)
240 {
241         struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
242         model->start(msrs);
243 }
244
245 static int nmi_start(void)
246 {
247         on_each_cpu(nmi_cpu_start, NULL, 1);
248         return 0;
249 }
250
251 static void nmi_cpu_stop(void *dummy)
252 {
253         struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
254         model->stop(msrs);
255 }
256
257 static void nmi_stop(void)
258 {
259         on_each_cpu(nmi_cpu_stop, NULL, 1);
260 }
261
262 struct op_counter_config counter_config[OP_MAX_COUNTER];
263
264 static int nmi_create_files(struct super_block *sb, struct dentry *root)
265 {
266         unsigned int i;
267
268         for (i = 0; i < model->num_counters; ++i) {
269                 struct dentry *dir;
270                 char buf[4];
271
272                 /* quick little hack to _not_ expose a counter if it is not
273                  * available for use.  This should protect userspace app.
274                  * NOTE:  assumes 1:1 mapping here (that counters are organized
275                  *        sequentially in their struct assignment).
276                  */
277                 if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
278                         continue;
279
280                 snprintf(buf,  sizeof(buf), "%d", i);
281                 dir = oprofilefs_mkdir(sb, root, buf);
282                 oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
283                 oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
284                 oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
285                 oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
286                 oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
287                 oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
288         }
289
290         return 0;
291 }
292
293 #ifdef CONFIG_SMP
294 static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
295                                  void *data)
296 {
297         int cpu = (unsigned long)data;
298         switch (action) {
299         case CPU_DOWN_FAILED:
300         case CPU_ONLINE:
301                 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0);
302                 break;
303         case CPU_DOWN_PREPARE:
304                 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1);
305                 break;
306         }
307         return NOTIFY_DONE;
308 }
309
310 static struct notifier_block oprofile_cpu_nb = {
311         .notifier_call = oprofile_cpu_notifier
312 };
313 #endif
314
315 #ifdef CONFIG_PM
316
317 static int nmi_suspend(struct sys_device *dev, pm_message_t state)
318 {
319         /* Only one CPU left, just stop that one */
320         if (nmi_enabled == 1)
321                 nmi_cpu_stop(NULL);
322         return 0;
323 }
324
325 static int nmi_resume(struct sys_device *dev)
326 {
327         if (nmi_enabled == 1)
328                 nmi_cpu_start(NULL);
329         return 0;
330 }
331
332 static struct sysdev_class oprofile_sysclass = {
333         .name           = "oprofile",
334         .resume         = nmi_resume,
335         .suspend        = nmi_suspend,
336 };
337
338 static struct sys_device device_oprofile = {
339         .id     = 0,
340         .cls    = &oprofile_sysclass,
341 };
342
343 static int __init init_sysfs(void)
344 {
345         int error;
346
347         error = sysdev_class_register(&oprofile_sysclass);
348         if (!error)
349                 error = sysdev_register(&device_oprofile);
350         return error;
351 }
352
353 static void exit_sysfs(void)
354 {
355         sysdev_unregister(&device_oprofile);
356         sysdev_class_unregister(&oprofile_sysclass);
357 }
358
359 #else
360 #define init_sysfs() do { } while (0)
361 #define exit_sysfs() do { } while (0)
362 #endif /* CONFIG_PM */
363
364 static int __init p4_init(char **cpu_type)
365 {
366         __u8 cpu_model = boot_cpu_data.x86_model;
367
368         if (cpu_model > 6 || cpu_model == 5)
369                 return 0;
370
371 #ifndef CONFIG_SMP
372         *cpu_type = "i386/p4";
373         model = &op_p4_spec;
374         return 1;
375 #else
376         switch (smp_num_siblings) {
377         case 1:
378                 *cpu_type = "i386/p4";
379                 model = &op_p4_spec;
380                 return 1;
381
382         case 2:
383                 *cpu_type = "i386/p4-ht";
384                 model = &op_p4_ht2_spec;
385                 return 1;
386         }
387 #endif
388
389         printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
390         printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
391         return 0;
392 }
393
394 static int force_arch_perfmon;
395 static int force_cpu_type(const char *str, struct kernel_param *kp)
396 {
397         if (!strcmp(str, "archperfmon")) {
398                 force_arch_perfmon = 1;
399                 printk(KERN_INFO "oprofile: forcing architectural perfmon\n");
400         }
401
402         return 0;
403 }
404 module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0);
405
406 static int __init ppro_init(char **cpu_type)
407 {
408         __u8 cpu_model = boot_cpu_data.x86_model;
409
410         if (force_arch_perfmon && cpu_has_arch_perfmon)
411                 return 0;
412
413         switch (cpu_model) {
414         case 0 ... 2:
415                 *cpu_type = "i386/ppro";
416                 break;
417         case 3 ... 5:
418                 *cpu_type = "i386/pii";
419                 break;
420         case 6 ... 8:
421         case 10 ... 11:
422                 *cpu_type = "i386/piii";
423                 break;
424         case 9:
425         case 13:
426                 *cpu_type = "i386/p6_mobile";
427                 break;
428         case 14:
429                 *cpu_type = "i386/core";
430                 break;
431         case 15: case 23:
432                 *cpu_type = "i386/core_2";
433                 break;
434         case 26:
435                 model = &op_arch_perfmon_spec;
436                 *cpu_type = "i386/core_i7";
437                 break;
438         case 28:
439                 *cpu_type = "i386/atom";
440                 break;
441         default:
442                 /* Unknown */
443                 return 0;
444         }
445
446         model = &op_ppro_spec;
447         return 1;
448 }
449
450 /* in order to get sysfs right */
451 static int using_nmi;
452
453 int __init op_nmi_init(struct oprofile_operations *ops)
454 {
455         __u8 vendor = boot_cpu_data.x86_vendor;
456         __u8 family = boot_cpu_data.x86;
457         char *cpu_type = NULL;
458         int ret = 0;
459
460         if (!cpu_has_apic)
461                 return -ENODEV;
462
463         switch (vendor) {
464         case X86_VENDOR_AMD:
465                 /* Needs to be at least an Athlon (or hammer in 32bit mode) */
466
467                 switch (family) {
468                 case 6:
469                         cpu_type = "i386/athlon";
470                         break;
471                 case 0xf:
472                         /*
473                          * Actually it could be i386/hammer too, but
474                          * give user space an consistent name.
475                          */
476                         cpu_type = "x86-64/hammer";
477                         break;
478                 case 0x10:
479                         cpu_type = "x86-64/family10";
480                         break;
481                 case 0x11:
482                         cpu_type = "x86-64/family11h";
483                         break;
484                 default:
485                         return -ENODEV;
486                 }
487                 model = &op_amd_spec;
488                 break;
489
490         case X86_VENDOR_INTEL:
491                 switch (family) {
492                         /* Pentium IV */
493                 case 0xf:
494                         p4_init(&cpu_type);
495                         break;
496
497                         /* A P6-class processor */
498                 case 6:
499                         ppro_init(&cpu_type);
500                         break;
501
502                 default:
503                         break;
504                 }
505
506                 if (cpu_type)
507                         break;
508
509                 if (!cpu_has_arch_perfmon)
510                         return -ENODEV;
511
512                 /* use arch perfmon as fallback */
513                 cpu_type = "i386/arch_perfmon";
514                 model = &op_arch_perfmon_spec;
515                 break;
516
517         default:
518                 return -ENODEV;
519         }
520
521 #ifdef CONFIG_SMP
522         register_cpu_notifier(&oprofile_cpu_nb);
523 #endif
524         /* default values, can be overwritten by model */
525         ops->create_files = nmi_create_files;
526         ops->setup = nmi_setup;
527         ops->shutdown = nmi_shutdown;
528         ops->start = nmi_start;
529         ops->stop = nmi_stop;
530         ops->cpu_type = cpu_type;
531
532         if (model->init)
533                 ret = model->init(ops);
534         if (ret)
535                 return ret;
536
537         init_sysfs();
538         using_nmi = 1;
539         printk(KERN_INFO "oprofile: using NMI interrupt.\n");
540         return 0;
541 }
542
543 void op_nmi_exit(void)
544 {
545         if (using_nmi) {
546                 exit_sysfs();
547 #ifdef CONFIG_SMP
548                 unregister_cpu_notifier(&oprofile_cpu_nb);
549 #endif
550         }
551         if (model->exit)
552                 model->exit();
553 }