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