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