Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file nmi_int.c |
| 3 | * |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 4 | * @remark Copyright 2002-2009 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> |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 9 | * @author Barry Kasindorf <barry.kasindorf@amd.com> |
| 10 | * @author Jason Yeh <jason.yeh@amd.com> |
| 11 | * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/notifier.h> |
| 16 | #include <linux/smp.h> |
| 17 | #include <linux/oprofile.h> |
| 18 | #include <linux/sysdev.h> |
| 19 | #include <linux/slab.h> |
Andi Kleen | 1cfcea1 | 2006-07-10 17:06:21 +0200 | [diff] [blame] | 20 | #include <linux/moduleparam.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 21 | #include <linux/kdebug.h> |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 22 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/nmi.h> |
| 24 | #include <asm/msr.h> |
| 25 | #include <asm/apic.h> |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "op_counter.h" |
| 28 | #include "op_x86_model.h" |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 29 | |
Robert Richter | 259a83a | 2009-07-09 15:12:35 +0200 | [diff] [blame] | 30 | static struct op_x86_model_spec *model; |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 31 | static DEFINE_PER_CPU(struct op_msrs, cpu_msrs); |
| 32 | static DEFINE_PER_CPU(unsigned long, saved_lvtpc); |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 33 | |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 34 | /* must be protected with get_online_cpus()/put_online_cpus(): */ |
| 35 | static int nmi_enabled; |
| 36 | static int ctr_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 38 | struct op_counter_config counter_config[OP_MAX_COUNTER]; |
| 39 | |
Robert Richter | 3370d35 | 2009-05-25 15:10:32 +0200 | [diff] [blame] | 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; |
| 52 | event &= model->event_mask ? model->event_mask : 0xFF; |
| 53 | val |= event & 0xFF; |
| 54 | val |= (event & 0x0F00) << 24; |
| 55 | |
| 56 | return val; |
| 57 | } |
| 58 | |
| 59 | |
Adrian Bunk | c7c19f8 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 60 | static int profile_exceptions_notify(struct notifier_block *self, |
| 61 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 63 | struct die_args *args = (struct die_args *)data; |
| 64 | int ret = NOTIFY_DONE; |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 65 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 66 | switch (val) { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 67 | case DIE_NMI: |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 68 | case DIE_NMI_IPI: |
Robert Richter | de65464 | 2010-05-03 14:41:22 +0200 | [diff] [blame] | 69 | if (ctr_running) |
| 70 | model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs)); |
| 71 | else if (!nmi_enabled) |
| 72 | break; |
| 73 | else |
| 74 | model->stop(&__get_cpu_var(cpu_msrs)); |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 75 | ret = NOTIFY_STOP; |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 76 | break; |
| 77 | default: |
| 78 | break; |
| 79 | } |
| 80 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 82 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 83 | static void nmi_cpu_save_registers(struct op_msrs *msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 85 | struct op_msr *counters = msrs->counters; |
| 86 | struct op_msr *controls = msrs->controls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | unsigned int i; |
| 88 | |
Robert Richter | 1a245c4 | 2009-06-05 15:54:24 +0200 | [diff] [blame] | 89 | for (i = 0; i < model->num_counters; ++i) { |
Robert Richter | 95e74e6 | 2009-06-03 19:09:27 +0200 | [diff] [blame] | 90 | if (counters[i].addr) |
| 91 | rdmsrl(counters[i].addr, counters[i].saved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 93 | |
Robert Richter | 1a245c4 | 2009-06-05 15:54:24 +0200 | [diff] [blame] | 94 | for (i = 0; i < model->num_controls; ++i) { |
Robert Richter | 95e74e6 | 2009-06-03 19:09:27 +0200 | [diff] [blame] | 95 | if (controls[i].addr) |
| 96 | rdmsrl(controls[i].addr, controls[i].saved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 100 | static void nmi_cpu_start(void *dummy) |
| 101 | { |
| 102 | struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs); |
Robert Richter | 2623a1d | 2010-05-03 19:44:32 +0200 | [diff] [blame] | 103 | if (!msrs->controls) |
| 104 | WARN_ON_ONCE(1); |
| 105 | else |
| 106 | model->start(msrs); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int nmi_start(void) |
| 110 | { |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 111 | get_online_cpus(); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 112 | on_each_cpu(nmi_cpu_start, NULL, 1); |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 113 | ctr_running = 1; |
| 114 | put_online_cpus(); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static void nmi_cpu_stop(void *dummy) |
| 119 | { |
| 120 | struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs); |
Robert Richter | 2623a1d | 2010-05-03 19:44:32 +0200 | [diff] [blame] | 121 | if (!msrs->controls) |
| 122 | WARN_ON_ONCE(1); |
| 123 | else |
| 124 | model->stop(msrs); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void nmi_stop(void) |
| 128 | { |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 129 | get_online_cpus(); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 130 | on_each_cpu(nmi_cpu_stop, NULL, 1); |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 131 | ctr_running = 0; |
| 132 | put_online_cpus(); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 136 | |
| 137 | static DEFINE_PER_CPU(int, switch_index); |
| 138 | |
Robert Richter | 39e97f4 | 2009-07-09 15:11:45 +0200 | [diff] [blame] | 139 | static inline int has_mux(void) |
| 140 | { |
| 141 | return !!model->switch_ctrl; |
| 142 | } |
| 143 | |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 144 | inline int op_x86_phys_to_virt(int phys) |
| 145 | { |
| 146 | return __get_cpu_var(switch_index) + phys; |
| 147 | } |
| 148 | |
Robert Richter | 61d149d | 2009-07-10 15:47:17 +0200 | [diff] [blame] | 149 | inline int op_x86_virt_to_phys(int virt) |
| 150 | { |
| 151 | return virt % model->num_counters; |
| 152 | } |
| 153 | |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 154 | static void nmi_shutdown_mux(void) |
| 155 | { |
| 156 | int i; |
Robert Richter | 39e97f4 | 2009-07-09 15:11:45 +0200 | [diff] [blame] | 157 | |
| 158 | if (!has_mux()) |
| 159 | return; |
| 160 | |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 161 | for_each_possible_cpu(i) { |
| 162 | kfree(per_cpu(cpu_msrs, i).multiplex); |
| 163 | per_cpu(cpu_msrs, i).multiplex = NULL; |
| 164 | per_cpu(switch_index, i) = 0; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static int nmi_setup_mux(void) |
| 169 | { |
| 170 | size_t multiplex_size = |
| 171 | sizeof(struct op_msr) * model->num_virt_counters; |
| 172 | int i; |
Robert Richter | 39e97f4 | 2009-07-09 15:11:45 +0200 | [diff] [blame] | 173 | |
| 174 | if (!has_mux()) |
| 175 | return 1; |
| 176 | |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 177 | for_each_possible_cpu(i) { |
| 178 | per_cpu(cpu_msrs, i).multiplex = |
Robert Richter | c17c8fb | 2010-02-25 20:20:25 +0100 | [diff] [blame] | 179 | kzalloc(multiplex_size, GFP_KERNEL); |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 180 | if (!per_cpu(cpu_msrs, i).multiplex) |
| 181 | return 0; |
| 182 | } |
Robert Richter | 39e97f4 | 2009-07-09 15:11:45 +0200 | [diff] [blame] | 183 | |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 184 | return 1; |
| 185 | } |
| 186 | |
Robert Richter | 48fb4b4 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 187 | static void nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) |
| 188 | { |
| 189 | int i; |
| 190 | struct op_msr *multiplex = msrs->multiplex; |
| 191 | |
Robert Richter | 39e97f4 | 2009-07-09 15:11:45 +0200 | [diff] [blame] | 192 | if (!has_mux()) |
| 193 | return; |
| 194 | |
Robert Richter | 48fb4b4 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 195 | for (i = 0; i < model->num_virt_counters; ++i) { |
| 196 | if (counter_config[i].enabled) { |
| 197 | multiplex[i].saved = -(u64)counter_config[i].count; |
| 198 | } else { |
Robert Richter | 48fb4b4 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 199 | multiplex[i].saved = 0; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | per_cpu(switch_index, cpu) = 0; |
| 204 | } |
| 205 | |
Robert Richter | d0f585d | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 206 | static void nmi_cpu_save_mpx_registers(struct op_msrs *msrs) |
| 207 | { |
Robert Richter | 68dc819 | 2010-02-25 19:16:46 +0100 | [diff] [blame] | 208 | struct op_msr *counters = msrs->counters; |
Robert Richter | d0f585d | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 209 | struct op_msr *multiplex = msrs->multiplex; |
| 210 | int i; |
| 211 | |
| 212 | for (i = 0; i < model->num_counters; ++i) { |
| 213 | int virt = op_x86_phys_to_virt(i); |
Robert Richter | 68dc819 | 2010-02-25 19:16:46 +0100 | [diff] [blame] | 214 | if (counters[i].addr) |
| 215 | rdmsrl(counters[i].addr, multiplex[virt].saved); |
Robert Richter | d0f585d | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | static void nmi_cpu_restore_mpx_registers(struct op_msrs *msrs) |
| 220 | { |
Robert Richter | 68dc819 | 2010-02-25 19:16:46 +0100 | [diff] [blame] | 221 | struct op_msr *counters = msrs->counters; |
Robert Richter | d0f585d | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 222 | struct op_msr *multiplex = msrs->multiplex; |
| 223 | int i; |
| 224 | |
| 225 | for (i = 0; i < model->num_counters; ++i) { |
| 226 | int virt = op_x86_phys_to_virt(i); |
Robert Richter | 68dc819 | 2010-02-25 19:16:46 +0100 | [diff] [blame] | 227 | if (counters[i].addr) |
| 228 | wrmsrl(counters[i].addr, multiplex[virt].saved); |
Robert Richter | d0f585d | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 232 | static void nmi_cpu_switch(void *dummy) |
| 233 | { |
| 234 | int cpu = smp_processor_id(); |
| 235 | int si = per_cpu(switch_index, cpu); |
| 236 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
| 237 | |
| 238 | nmi_cpu_stop(NULL); |
| 239 | nmi_cpu_save_mpx_registers(msrs); |
| 240 | |
| 241 | /* move to next set */ |
| 242 | si += model->num_counters; |
Suravee Suthikulpanit | d8cc108 | 2010-01-18 11:25:36 -0600 | [diff] [blame] | 243 | if ((si >= model->num_virt_counters) || (counter_config[si].count == 0)) |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 244 | per_cpu(switch_index, cpu) = 0; |
| 245 | else |
| 246 | per_cpu(switch_index, cpu) = si; |
| 247 | |
| 248 | model->switch_ctrl(model, msrs); |
| 249 | nmi_cpu_restore_mpx_registers(msrs); |
| 250 | |
| 251 | nmi_cpu_start(NULL); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * Quick check to see if multiplexing is necessary. |
| 257 | * The check should be sufficient since counters are used |
| 258 | * in ordre. |
| 259 | */ |
| 260 | static int nmi_multiplex_on(void) |
| 261 | { |
| 262 | return counter_config[model->num_counters].count ? 0 : -EINVAL; |
| 263 | } |
| 264 | |
| 265 | static int nmi_switch_event(void) |
| 266 | { |
Robert Richter | 39e97f4 | 2009-07-09 15:11:45 +0200 | [diff] [blame] | 267 | if (!has_mux()) |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 268 | return -ENOSYS; /* not implemented */ |
| 269 | if (nmi_multiplex_on() < 0) |
| 270 | return -EINVAL; /* not necessary */ |
| 271 | |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 272 | get_online_cpus(); |
| 273 | if (ctr_running) |
| 274 | on_each_cpu(nmi_cpu_switch, NULL, 1); |
| 275 | put_online_cpus(); |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 276 | |
Robert Richter | b28d1b9 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Robert Richter | 5280514 | 2009-07-09 16:02:44 +0200 | [diff] [blame] | 280 | static inline void mux_init(struct oprofile_operations *ops) |
| 281 | { |
| 282 | if (has_mux()) |
| 283 | ops->switch_events = nmi_switch_event; |
| 284 | } |
| 285 | |
Robert Richter | 4d015f7 | 2009-07-09 21:42:51 +0200 | [diff] [blame] | 286 | static void mux_clone(int cpu) |
| 287 | { |
| 288 | if (!has_mux()) |
| 289 | return; |
| 290 | |
| 291 | memcpy(per_cpu(cpu_msrs, cpu).multiplex, |
| 292 | per_cpu(cpu_msrs, 0).multiplex, |
| 293 | sizeof(struct op_msr) * model->num_virt_counters); |
| 294 | } |
| 295 | |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 296 | #else |
| 297 | |
| 298 | inline int op_x86_phys_to_virt(int phys) { return phys; } |
Robert Richter | 61d149d | 2009-07-10 15:47:17 +0200 | [diff] [blame] | 299 | inline int op_x86_virt_to_phys(int virt) { return virt; } |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 300 | static inline void nmi_shutdown_mux(void) { } |
| 301 | static inline int nmi_setup_mux(void) { return 1; } |
Robert Richter | 48fb4b4 | 2009-07-09 14:38:49 +0200 | [diff] [blame] | 302 | static inline void |
| 303 | nmi_cpu_setup_mux(int cpu, struct op_msrs const * const msrs) { } |
Robert Richter | 5280514 | 2009-07-09 16:02:44 +0200 | [diff] [blame] | 304 | static inline void mux_init(struct oprofile_operations *ops) { } |
Robert Richter | 4d015f7 | 2009-07-09 21:42:51 +0200 | [diff] [blame] | 305 | static void mux_clone(int cpu) { } |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 306 | |
| 307 | #endif |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | static void free_msrs(void) |
| 310 | { |
| 311 | int i; |
KAMEZAWA Hiroyuki | c8912599 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 312 | for_each_possible_cpu(i) { |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 313 | kfree(per_cpu(cpu_msrs, i).counters); |
| 314 | per_cpu(cpu_msrs, i).counters = NULL; |
| 315 | kfree(per_cpu(cpu_msrs, i).controls); |
| 316 | per_cpu(cpu_msrs, i).controls = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
Robert Richter | 8f5a2dd | 2010-03-23 19:09:51 +0100 | [diff] [blame] | 318 | nmi_shutdown_mux(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | static int allocate_msrs(void) |
| 322 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | size_t controls_size = sizeof(struct op_msr) * model->num_controls; |
| 324 | size_t counters_size = sizeof(struct op_msr) * model->num_counters; |
| 325 | |
Robert Richter | 4c168ea | 2008-09-24 11:08:52 +0200 | [diff] [blame] | 326 | int i; |
Chris Wright | 0939c17 | 2007-06-01 00:46:39 -0700 | [diff] [blame] | 327 | for_each_possible_cpu(i) { |
Robert Richter | c17c8fb | 2010-02-25 20:20:25 +0100 | [diff] [blame] | 328 | per_cpu(cpu_msrs, i).counters = kzalloc(counters_size, |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 329 | GFP_KERNEL); |
| 330 | if (!per_cpu(cpu_msrs, i).counters) |
Robert Richter | 8f5a2dd | 2010-03-23 19:09:51 +0100 | [diff] [blame] | 331 | goto fail; |
Robert Richter | c17c8fb | 2010-02-25 20:20:25 +0100 | [diff] [blame] | 332 | per_cpu(cpu_msrs, i).controls = kzalloc(controls_size, |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 333 | GFP_KERNEL); |
| 334 | if (!per_cpu(cpu_msrs, i).controls) |
Robert Richter | 8f5a2dd | 2010-03-23 19:09:51 +0100 | [diff] [blame] | 335 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Robert Richter | 8f5a2dd | 2010-03-23 19:09:51 +0100 | [diff] [blame] | 338 | if (!nmi_setup_mux()) |
| 339 | goto fail; |
| 340 | |
Robert Richter | 6ab82f9 | 2009-07-09 14:40:04 +0200 | [diff] [blame] | 341 | return 1; |
Robert Richter | 8f5a2dd | 2010-03-23 19:09:51 +0100 | [diff] [blame] | 342 | |
| 343 | fail: |
| 344 | free_msrs(); |
| 345 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 348 | static void nmi_cpu_setup(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
| 350 | int cpu = smp_processor_id(); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 351 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
Robert Richter | 44ab9a6 | 2009-07-09 18:33:02 +0200 | [diff] [blame] | 352 | nmi_cpu_save_registers(msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | spin_lock(&oprofilefs_lock); |
Robert Richter | ef8828d | 2009-05-25 19:31:44 +0200 | [diff] [blame] | 354 | model->setup_ctrs(model, msrs); |
Robert Richter | 6bfccd0 | 2009-07-09 19:23:50 +0200 | [diff] [blame] | 355 | nmi_cpu_setup_mux(cpu, msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | spin_unlock(&oprofilefs_lock); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 357 | per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 359 | } |
| 360 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 361 | static struct notifier_block profile_exceptions_nb = { |
| 362 | .notifier_call = profile_exceptions_notify, |
| 363 | .next = NULL, |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 364 | .priority = 2 |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 365 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Robert Richter | 44ab9a6 | 2009-07-09 18:33:02 +0200 | [diff] [blame] | 367 | static void nmi_cpu_restore_registers(struct op_msrs *msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 369 | struct op_msr *counters = msrs->counters; |
| 370 | struct op_msr *controls = msrs->controls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | unsigned int i; |
| 372 | |
Robert Richter | 1a245c4 | 2009-06-05 15:54:24 +0200 | [diff] [blame] | 373 | for (i = 0; i < model->num_controls; ++i) { |
Robert Richter | 95e74e6 | 2009-06-03 19:09:27 +0200 | [diff] [blame] | 374 | if (controls[i].addr) |
| 375 | wrmsrl(controls[i].addr, controls[i].saved); |
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 | |
Robert Richter | 1a245c4 | 2009-06-05 15:54:24 +0200 | [diff] [blame] | 378 | for (i = 0; i < model->num_counters; ++i) { |
Robert Richter | 95e74e6 | 2009-06-03 19:09:27 +0200 | [diff] [blame] | 379 | if (counters[i].addr) |
| 380 | wrmsrl(counters[i].addr, counters[i].saved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 384 | static void nmi_cpu_shutdown(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
| 386 | unsigned int v; |
| 387 | int cpu = smp_processor_id(); |
Robert Richter | 82a2252 | 2009-07-09 16:29:34 +0200 | [diff] [blame] | 388 | struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* restoring APIC_LVTPC can trigger an apic error because the delivery |
| 391 | * mode and vector nr combination can be illegal. That's by design: on |
| 392 | * power on apic lvt contain a zero vector nr which are legal only for |
| 393 | * NMI delivery mode. So inhibit apic err before restoring lvtpc |
| 394 | */ |
| 395 | v = apic_read(APIC_LVTERR); |
| 396 | apic_write(APIC_LVTERR, v | APIC_LVT_MASKED); |
Mike Travis | d18d00f | 2008-03-25 15:06:59 -0700 | [diff] [blame] | 397 | apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | apic_write(APIC_LVTERR, v); |
Robert Richter | 44ab9a6 | 2009-07-09 18:33:02 +0200 | [diff] [blame] | 399 | nmi_cpu_restore_registers(msrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 402 | static void nmi_cpu_up(void *dummy) |
| 403 | { |
| 404 | if (nmi_enabled) |
| 405 | nmi_cpu_setup(dummy); |
| 406 | if (ctr_running) |
| 407 | nmi_cpu_start(dummy); |
| 408 | } |
| 409 | |
| 410 | static void nmi_cpu_down(void *dummy) |
| 411 | { |
| 412 | if (ctr_running) |
| 413 | nmi_cpu_stop(dummy); |
| 414 | if (nmi_enabled) |
| 415 | nmi_cpu_shutdown(dummy); |
| 416 | } |
| 417 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 418 | static int nmi_create_files(struct super_block *sb, struct dentry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
| 420 | unsigned int i; |
| 421 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 422 | for (i = 0; i < model->num_virt_counters; ++i) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 423 | struct dentry *dir; |
Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 424 | char buf[4]; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 425 | |
| 426 | /* quick little hack to _not_ expose a counter if it is not |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 427 | * available for use. This should protect userspace app. |
| 428 | * NOTE: assumes 1:1 mapping here (that counters are organized |
| 429 | * sequentially in their struct assignment). |
| 430 | */ |
Robert Richter | 11be1a7 | 2009-07-10 18:15:21 +0200 | [diff] [blame] | 431 | if (!avail_to_resrv_perfctr_nmi_bit(op_x86_virt_to_phys(i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 432 | continue; |
| 433 | |
Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 434 | snprintf(buf, sizeof(buf), "%d", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | dir = oprofilefs_mkdir(sb, root, buf); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 436 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); |
| 437 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); |
| 438 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); |
| 439 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); |
| 440 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); |
| 441 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 446 | |
Robert Richter | 69046d4 | 2008-09-05 12:17:40 +0200 | [diff] [blame] | 447 | static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action, |
| 448 | void *data) |
| 449 | { |
| 450 | int cpu = (unsigned long)data; |
| 451 | switch (action) { |
| 452 | case CPU_DOWN_FAILED: |
| 453 | case CPU_ONLINE: |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 454 | smp_call_function_single(cpu, nmi_cpu_up, NULL, 0); |
Robert Richter | 69046d4 | 2008-09-05 12:17:40 +0200 | [diff] [blame] | 455 | break; |
| 456 | case CPU_DOWN_PREPARE: |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 457 | smp_call_function_single(cpu, nmi_cpu_down, NULL, 1); |
Robert Richter | 69046d4 | 2008-09-05 12:17:40 +0200 | [diff] [blame] | 458 | break; |
| 459 | } |
| 460 | return NOTIFY_DONE; |
| 461 | } |
| 462 | |
| 463 | static struct notifier_block oprofile_cpu_nb = { |
| 464 | .notifier_call = oprofile_cpu_notifier |
| 465 | }; |
Robert Richter | 69046d4 | 2008-09-05 12:17:40 +0200 | [diff] [blame] | 466 | |
Robert Richter | d30d64c | 2010-05-03 15:52:26 +0200 | [diff] [blame^] | 467 | static int nmi_setup(void) |
| 468 | { |
| 469 | int err = 0; |
| 470 | int cpu; |
| 471 | |
| 472 | if (!allocate_msrs()) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | /* We need to serialize save and setup for HT because the subset |
| 476 | * of msrs are distinct for save and setup operations |
| 477 | */ |
| 478 | |
| 479 | /* Assume saved/restored counters are the same on all CPUs */ |
| 480 | err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0)); |
| 481 | if (err) |
| 482 | goto fail; |
| 483 | |
| 484 | for_each_possible_cpu(cpu) { |
| 485 | if (!cpu) |
| 486 | continue; |
| 487 | |
| 488 | memcpy(per_cpu(cpu_msrs, cpu).counters, |
| 489 | per_cpu(cpu_msrs, 0).counters, |
| 490 | sizeof(struct op_msr) * model->num_counters); |
| 491 | |
| 492 | memcpy(per_cpu(cpu_msrs, cpu).controls, |
| 493 | per_cpu(cpu_msrs, 0).controls, |
| 494 | sizeof(struct op_msr) * model->num_controls); |
| 495 | |
| 496 | mux_clone(cpu); |
| 497 | } |
| 498 | |
| 499 | nmi_enabled = 0; |
| 500 | ctr_running = 0; |
| 501 | barrier(); |
| 502 | err = register_die_notifier(&profile_exceptions_nb); |
| 503 | if (err) |
| 504 | goto fail; |
| 505 | |
| 506 | get_online_cpus(); |
| 507 | on_each_cpu(nmi_cpu_setup, NULL, 1); |
| 508 | nmi_enabled = 1; |
| 509 | put_online_cpus(); |
| 510 | |
| 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 | |
| 521 | get_online_cpus(); |
| 522 | on_each_cpu(nmi_cpu_shutdown, NULL, 1); |
| 523 | nmi_enabled = 0; |
| 524 | ctr_running = 0; |
| 525 | put_online_cpus(); |
| 526 | barrier(); |
| 527 | unregister_die_notifier(&profile_exceptions_nb); |
| 528 | msrs = &get_cpu_var(cpu_msrs); |
| 529 | model->shutdown(msrs); |
| 530 | free_msrs(); |
| 531 | put_cpu_var(cpu_msrs); |
| 532 | } |
| 533 | |
Robert Richter | 69046d4 | 2008-09-05 12:17:40 +0200 | [diff] [blame] | 534 | #ifdef CONFIG_PM |
| 535 | |
| 536 | static int nmi_suspend(struct sys_device *dev, pm_message_t state) |
| 537 | { |
| 538 | /* Only one CPU left, just stop that one */ |
| 539 | if (nmi_enabled == 1) |
| 540 | nmi_cpu_stop(NULL); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int nmi_resume(struct sys_device *dev) |
| 545 | { |
| 546 | if (nmi_enabled == 1) |
| 547 | nmi_cpu_start(NULL); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static struct sysdev_class oprofile_sysclass = { |
| 552 | .name = "oprofile", |
| 553 | .resume = nmi_resume, |
| 554 | .suspend = nmi_suspend, |
| 555 | }; |
| 556 | |
| 557 | static struct sys_device device_oprofile = { |
| 558 | .id = 0, |
| 559 | .cls = &oprofile_sysclass, |
| 560 | }; |
| 561 | |
| 562 | static int __init init_sysfs(void) |
| 563 | { |
| 564 | int error; |
| 565 | |
| 566 | error = sysdev_class_register(&oprofile_sysclass); |
| 567 | if (!error) |
| 568 | error = sysdev_register(&device_oprofile); |
| 569 | return error; |
| 570 | } |
| 571 | |
| 572 | static void exit_sysfs(void) |
| 573 | { |
| 574 | sysdev_unregister(&device_oprofile); |
| 575 | sysdev_class_unregister(&oprofile_sysclass); |
| 576 | } |
| 577 | |
| 578 | #else |
| 579 | #define init_sysfs() do { } while (0) |
| 580 | #define exit_sysfs() do { } while (0) |
| 581 | #endif /* CONFIG_PM */ |
| 582 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 583 | static int __init p4_init(char **cpu_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
| 585 | __u8 cpu_model = boot_cpu_data.x86_model; |
| 586 | |
Andi Kleen | 1f3d7b6 | 2009-04-27 17:44:12 +0200 | [diff] [blame] | 587 | if (cpu_model > 6 || cpu_model == 5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | return 0; |
| 589 | |
| 590 | #ifndef CONFIG_SMP |
| 591 | *cpu_type = "i386/p4"; |
| 592 | model = &op_p4_spec; |
| 593 | return 1; |
| 594 | #else |
| 595 | switch (smp_num_siblings) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 596 | case 1: |
| 597 | *cpu_type = "i386/p4"; |
| 598 | model = &op_p4_spec; |
| 599 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 601 | case 2: |
| 602 | *cpu_type = "i386/p4-ht"; |
| 603 | model = &op_p4_ht2_spec; |
| 604 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | #endif |
| 607 | |
| 608 | printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n"); |
| 609 | printk(KERN_INFO "oprofile: Reverting to timer mode.\n"); |
| 610 | return 0; |
| 611 | } |
| 612 | |
Robert Richter | 7e4e0bd | 2009-05-06 12:10:23 +0200 | [diff] [blame] | 613 | static int force_arch_perfmon; |
| 614 | static int force_cpu_type(const char *str, struct kernel_param *kp) |
| 615 | { |
Robert Richter | 8d7ff4f | 2009-06-23 11:48:14 +0200 | [diff] [blame] | 616 | if (!strcmp(str, "arch_perfmon")) { |
Robert Richter | 7e4e0bd | 2009-05-06 12:10:23 +0200 | [diff] [blame] | 617 | force_arch_perfmon = 1; |
| 618 | printk(KERN_INFO "oprofile: forcing architectural perfmon\n"); |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | module_param_call(cpu_type, force_cpu_type, NULL, NULL, 0); |
Andi Kleen | 1dcdb5a | 2009-04-27 17:44:11 +0200 | [diff] [blame] | 624 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 625 | static int __init ppro_init(char **cpu_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
| 627 | __u8 cpu_model = boot_cpu_data.x86_model; |
Robert Richter | 259a83a | 2009-07-09 15:12:35 +0200 | [diff] [blame] | 628 | struct op_x86_model_spec *spec = &op_ppro_spec; /* default */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Andi Kleen | 1dcdb5a | 2009-04-27 17:44:11 +0200 | [diff] [blame] | 630 | if (force_arch_perfmon && cpu_has_arch_perfmon) |
| 631 | return 0; |
| 632 | |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 633 | switch (cpu_model) { |
| 634 | case 0 ... 2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | *cpu_type = "i386/ppro"; |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 636 | break; |
| 637 | case 3 ... 5: |
| 638 | *cpu_type = "i386/pii"; |
| 639 | break; |
| 640 | case 6 ... 8: |
William Cohen | 3d337c6 | 2008-11-30 15:39:10 -0500 | [diff] [blame] | 641 | case 10 ... 11: |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 642 | *cpu_type = "i386/piii"; |
| 643 | break; |
| 644 | case 9: |
William Cohen | 3d337c6 | 2008-11-30 15:39:10 -0500 | [diff] [blame] | 645 | case 13: |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 646 | *cpu_type = "i386/p6_mobile"; |
| 647 | break; |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 648 | case 14: |
| 649 | *cpu_type = "i386/core"; |
| 650 | break; |
| 651 | case 15: case 23: |
| 652 | *cpu_type = "i386/core_2"; |
| 653 | break; |
Andi Kleen | e83e452 | 2010-01-21 23:26:27 +0100 | [diff] [blame] | 654 | case 0x2e: |
Andi Kleen | 6adf406 | 2009-04-27 17:44:13 +0200 | [diff] [blame] | 655 | case 26: |
Robert Richter | 802070f | 2009-06-12 18:32:07 +0200 | [diff] [blame] | 656 | spec = &op_arch_perfmon_spec; |
Andi Kleen | 6adf406 | 2009-04-27 17:44:13 +0200 | [diff] [blame] | 657 | *cpu_type = "i386/core_i7"; |
| 658 | break; |
| 659 | case 28: |
| 660 | *cpu_type = "i386/atom"; |
| 661 | break; |
Linus Torvalds | 4b9f12a | 2008-07-24 17:29:00 -0700 | [diff] [blame] | 662 | default: |
| 663 | /* Unknown */ |
| 664 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Robert Richter | 802070f | 2009-06-12 18:32:07 +0200 | [diff] [blame] | 667 | model = spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return 1; |
| 669 | } |
| 670 | |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 671 | /* in order to get sysfs right */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | static int using_nmi; |
| 673 | |
David Gibson | 96d0821 | 2005-09-06 15:17:26 -0700 | [diff] [blame] | 674 | int __init op_nmi_init(struct oprofile_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
| 676 | __u8 vendor = boot_cpu_data.x86_vendor; |
| 677 | __u8 family = boot_cpu_data.x86; |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 678 | char *cpu_type = NULL; |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 679 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
| 681 | if (!cpu_has_apic) |
| 682 | return -ENODEV; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | switch (vendor) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 685 | case X86_VENDOR_AMD: |
| 686 | /* Needs to be at least an Athlon (or hammer in 32bit mode) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 688 | switch (family) { |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 689 | case 6: |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 690 | cpu_type = "i386/athlon"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | break; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 692 | case 0xf: |
Robert Richter | d20f24c | 2009-01-11 13:01:16 +0100 | [diff] [blame] | 693 | /* |
| 694 | * Actually it could be i386/hammer too, but |
| 695 | * give user space an consistent name. |
| 696 | */ |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 697 | cpu_type = "x86-64/hammer"; |
| 698 | break; |
| 699 | case 0x10: |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 700 | cpu_type = "x86-64/family10"; |
| 701 | break; |
Barry Kasindorf | 12f2b26 | 2008-07-22 21:08:47 +0200 | [diff] [blame] | 702 | case 0x11: |
Barry Kasindorf | 12f2b26 | 2008-07-22 21:08:47 +0200 | [diff] [blame] | 703 | cpu_type = "x86-64/family11h"; |
| 704 | break; |
Robert Richter | d20f24c | 2009-01-11 13:01:16 +0100 | [diff] [blame] | 705 | default: |
| 706 | return -ENODEV; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 707 | } |
Robert Richter | d20f24c | 2009-01-11 13:01:16 +0100 | [diff] [blame] | 708 | model = &op_amd_spec; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 709 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 711 | case X86_VENDOR_INTEL: |
| 712 | switch (family) { |
| 713 | /* Pentium IV */ |
| 714 | case 0xf: |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 715 | p4_init(&cpu_type); |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 716 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 718 | /* A P6-class processor */ |
| 719 | case 6: |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 720 | ppro_init(&cpu_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | break; |
| 722 | |
| 723 | default: |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 724 | break; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 725 | } |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 726 | |
Robert Richter | e419294 | 2008-10-12 15:12:34 -0400 | [diff] [blame] | 727 | if (cpu_type) |
| 728 | break; |
| 729 | |
| 730 | if (!cpu_has_arch_perfmon) |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 731 | return -ENODEV; |
Robert Richter | e419294 | 2008-10-12 15:12:34 -0400 | [diff] [blame] | 732 | |
| 733 | /* use arch perfmon as fallback */ |
| 734 | cpu_type = "i386/arch_perfmon"; |
| 735 | model = &op_arch_perfmon_spec; |
Carlos R. Mafra | b75f53d | 2008-01-30 13:32:33 +0100 | [diff] [blame] | 736 | break; |
| 737 | |
| 738 | default: |
| 739 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 742 | get_online_cpus(); |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 743 | register_cpu_notifier(&oprofile_cpu_nb); |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 744 | nmi_enabled = 0; |
| 745 | ctr_running = 0; |
| 746 | put_online_cpus(); |
Robert Richter | 216f3d9 | 2010-05-03 11:58:46 +0200 | [diff] [blame] | 747 | |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 748 | /* default values, can be overwritten by model */ |
Robert Richter | 6e63ea4 | 2009-07-07 19:25:39 +0200 | [diff] [blame] | 749 | ops->create_files = nmi_create_files; |
| 750 | ops->setup = nmi_setup; |
| 751 | ops->shutdown = nmi_shutdown; |
| 752 | ops->start = nmi_start; |
| 753 | ops->stop = nmi_stop; |
| 754 | ops->cpu_type = cpu_type; |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 755 | |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 756 | if (model->init) |
| 757 | ret = model->init(ops); |
| 758 | if (ret) |
| 759 | return ret; |
| 760 | |
Robert Richter | 52471c6 | 2009-07-06 14:43:55 +0200 | [diff] [blame] | 761 | if (!model->num_virt_counters) |
| 762 | model->num_virt_counters = model->num_counters; |
| 763 | |
Robert Richter | 5280514 | 2009-07-09 16:02:44 +0200 | [diff] [blame] | 764 | mux_init(ops); |
| 765 | |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 766 | init_sysfs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | using_nmi = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | printk(KERN_INFO "oprofile: using NMI interrupt.\n"); |
| 769 | return 0; |
| 770 | } |
| 771 | |
David Gibson | 96d0821 | 2005-09-06 15:17:26 -0700 | [diff] [blame] | 772 | void op_nmi_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 774 | if (using_nmi) { |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 775 | exit_sysfs(); |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 776 | get_online_cpus(); |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 777 | unregister_cpu_notifier(&oprofile_cpu_nb); |
Robert Richter | 6ae56b5 | 2010-04-29 14:55:55 +0200 | [diff] [blame] | 778 | nmi_enabled = 0; |
| 779 | ctr_running = 0; |
| 780 | put_online_cpus(); |
Andi Kleen | 80a8c9f | 2008-08-19 03:13:38 +0200 | [diff] [blame] | 781 | } |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 782 | if (model->exit) |
| 783 | model->exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |