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