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