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