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