Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM |
will schmidt | 078f194 | 2007-06-27 02:12:33 +1000 | [diff] [blame^] | 3 | * Added mmcra[slot] support: |
| 4 | * Copyright (C) 2006-2007 Will Schmidt <willschm@us.ibm.com>, IBM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/oprofile.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/smp.h> |
Michael Ellerman | 57cfb81 | 2006-03-21 20:45:59 +1100 | [diff] [blame] | 15 | #include <asm/firmware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/ptrace.h> |
| 17 | #include <asm/system.h> |
| 18 | #include <asm/processor.h> |
| 19 | #include <asm/cputable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/rtas.h> |
Anton Blanchard | dca8593 | 2005-09-06 14:55:35 +1000 | [diff] [blame] | 21 | #include <asm/oprofile_impl.h> |
Anton Blanchard | cb09cff | 2005-11-07 18:43:56 +1100 | [diff] [blame] | 22 | #include <asm/reg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define dbg(args...) |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static unsigned long reset_value[OP_MAX_COUNTER]; |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static int oprofile_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */ |
| 31 | static u32 mmcr0_val; |
| 32 | static u64 mmcr1_val; |
Anton Blanchard | 15e812a | 2006-03-27 12:00:45 +1100 | [diff] [blame] | 33 | static u64 mmcra_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | static void power4_reg_setup(struct op_counter_config *ctr, |
| 36 | struct op_system_config *sys, |
| 37 | int num_ctrs) |
| 38 | { |
| 39 | int i; |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * The performance counter event settings are given in the mmcr0, |
| 43 | * mmcr1 and mmcra values passed from the user in the |
| 44 | * op_system_config structure (sys variable). |
| 45 | */ |
| 46 | mmcr0_val = sys->mmcr0; |
| 47 | mmcr1_val = sys->mmcr1; |
| 48 | mmcra_val = sys->mmcra; |
| 49 | |
Anton Blanchard | a6908cd | 2005-09-06 14:52:12 +1000 | [diff] [blame] | 50 | for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | reset_value[i] = 0x80000000UL - ctr[i].count; |
| 52 | |
| 53 | /* setup user and kernel profiling */ |
| 54 | if (sys->enable_kernel) |
| 55 | mmcr0_val &= ~MMCR0_KERNEL_DISABLE; |
| 56 | else |
| 57 | mmcr0_val |= MMCR0_KERNEL_DISABLE; |
| 58 | |
| 59 | if (sys->enable_user) |
| 60 | mmcr0_val &= ~MMCR0_PROBLEM_DISABLE; |
| 61 | else |
| 62 | mmcr0_val |= MMCR0_PROBLEM_DISABLE; |
| 63 | } |
| 64 | |
| 65 | extern void ppc64_enable_pmcs(void); |
| 66 | |
Anton Blanchard | cb09cff | 2005-11-07 18:43:56 +1100 | [diff] [blame] | 67 | /* |
| 68 | * Older CPUs require the MMCRA sample bit to be always set, but newer |
| 69 | * CPUs only want it set for some groups. Eventually we will remove all |
| 70 | * knowledge of this bit in the kernel, oprofile userspace should be |
| 71 | * setting it when required. |
| 72 | * |
| 73 | * In order to keep current installations working we force the bit for |
| 74 | * those older CPUs. Once everyone has updated their oprofile userspace we |
| 75 | * can remove this hack. |
| 76 | */ |
| 77 | static inline int mmcra_must_set_sample(void) |
| 78 | { |
| 79 | if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) || |
| 80 | __is_processor(PV_970) || __is_processor(PV_970FX) || |
Jake Moilanen | 362ff7b | 2006-10-18 10:47:22 -0500 | [diff] [blame] | 81 | __is_processor(PV_970MP) || __is_processor(PV_970GX)) |
Anton Blanchard | cb09cff | 2005-11-07 18:43:56 +1100 | [diff] [blame] | 82 | return 1; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 87 | static void power4_cpu_setup(struct op_counter_config *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | unsigned int mmcr0 = mmcr0_val; |
| 90 | unsigned long mmcra = mmcra_val; |
| 91 | |
| 92 | ppc64_enable_pmcs(); |
| 93 | |
| 94 | /* set the freeze bit */ |
| 95 | mmcr0 |= MMCR0_FC; |
| 96 | mtspr(SPRN_MMCR0, mmcr0); |
| 97 | |
| 98 | mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE; |
| 99 | mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE; |
| 100 | mtspr(SPRN_MMCR0, mmcr0); |
| 101 | |
| 102 | mtspr(SPRN_MMCR1, mmcr1_val); |
| 103 | |
Anton Blanchard | cb09cff | 2005-11-07 18:43:56 +1100 | [diff] [blame] | 104 | if (mmcra_must_set_sample()) |
| 105 | mmcra |= MMCRA_SAMPLE_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | mtspr(SPRN_MMCRA, mmcra); |
| 107 | |
| 108 | dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(), |
| 109 | mfspr(SPRN_MMCR0)); |
| 110 | dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(), |
| 111 | mfspr(SPRN_MMCR1)); |
| 112 | dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(), |
| 113 | mfspr(SPRN_MMCRA)); |
| 114 | } |
| 115 | |
| 116 | static void power4_start(struct op_counter_config *ctr) |
| 117 | { |
| 118 | int i; |
| 119 | unsigned int mmcr0; |
| 120 | |
| 121 | /* set the PMM bit (see comment below) */ |
| 122 | mtmsrd(mfmsr() | MSR_PMM); |
| 123 | |
Anton Blanchard | a6908cd | 2005-09-06 14:52:12 +1000 | [diff] [blame] | 124 | for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (ctr[i].enabled) { |
Olof Johansson | c69b767 | 2007-01-28 21:23:14 -0600 | [diff] [blame] | 126 | classic_ctr_write(i, reset_value[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } else { |
Olof Johansson | c69b767 | 2007-01-28 21:23:14 -0600 | [diff] [blame] | 128 | classic_ctr_write(i, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | mmcr0 = mfspr(SPRN_MMCR0); |
| 133 | |
| 134 | /* |
| 135 | * We must clear the PMAO bit on some (GQ) chips. Just do it |
| 136 | * all the time |
| 137 | */ |
| 138 | mmcr0 &= ~MMCR0_PMAO; |
| 139 | |
| 140 | /* |
| 141 | * now clear the freeze bit, counting will not start until we |
| 142 | * rfid from this excetion, because only at that point will |
| 143 | * the PMM bit be cleared |
| 144 | */ |
| 145 | mmcr0 &= ~MMCR0_FC; |
| 146 | mtspr(SPRN_MMCR0, mmcr0); |
| 147 | |
| 148 | oprofile_running = 1; |
| 149 | |
| 150 | dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0); |
| 151 | } |
| 152 | |
| 153 | static void power4_stop(void) |
| 154 | { |
| 155 | unsigned int mmcr0; |
| 156 | |
| 157 | /* freeze counters */ |
| 158 | mmcr0 = mfspr(SPRN_MMCR0); |
| 159 | mmcr0 |= MMCR0_FC; |
| 160 | mtspr(SPRN_MMCR0, mmcr0); |
| 161 | |
| 162 | oprofile_running = 0; |
| 163 | |
| 164 | dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0); |
| 165 | |
| 166 | mb(); |
| 167 | } |
| 168 | |
| 169 | /* Fake functions used by canonicalize_pc */ |
| 170 | static void __attribute_used__ hypervisor_bucket(void) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | static void __attribute_used__ rtas_bucket(void) |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | static void __attribute_used__ kernel_unknown_bucket(void) |
| 179 | { |
| 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
| 183 | * On GQ and newer the MMCRA stores the HV and PR bits at the time |
| 184 | * the SIAR was sampled. We use that to work out if the SIAR was sampled in |
| 185 | * the hypervisor, our exception vectors or RTAS. |
will schmidt | 078f194 | 2007-06-27 02:12:33 +1000 | [diff] [blame^] | 186 | * If the MMCRA_SAMPLE_ENABLE bit is set, we can use the MMCRA[slot] bits |
| 187 | * to more accurately identify the address of the sampled instruction. The |
| 188 | * mmcra[slot] bits represent the slot number of a sampled instruction |
| 189 | * within an instruction group. The slot will contain a value between 1 |
| 190 | * and 5 if MMCRA_SAMPLE_ENABLE is set, otherwise 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | */ |
| 192 | static unsigned long get_pc(struct pt_regs *regs) |
| 193 | { |
| 194 | unsigned long pc = mfspr(SPRN_SIAR); |
| 195 | unsigned long mmcra; |
will schmidt | 078f194 | 2007-06-27 02:12:33 +1000 | [diff] [blame^] | 196 | unsigned long slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | /* Cant do much about it */ |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 199 | if (!cur_cpu_spec->oprofile_mmcra_sihv) |
Anton Blanchard | 15e812a | 2006-03-27 12:00:45 +1100 | [diff] [blame] | 200 | return pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | mmcra = mfspr(SPRN_MMCRA); |
| 203 | |
will schmidt | 078f194 | 2007-06-27 02:12:33 +1000 | [diff] [blame^] | 204 | if (mmcra & MMCRA_SAMPLE_ENABLE) { |
| 205 | slot = ((mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT); |
| 206 | if (slot > 1) |
| 207 | pc += 4 * (slot - 1); |
| 208 | } |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* Were we in the hypervisor? */ |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 211 | if (firmware_has_feature(FW_FEATURE_LPAR) && |
| 212 | (mmcra & cur_cpu_spec->oprofile_mmcra_sihv)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* function descriptor madness */ |
| 214 | return *((unsigned long *)hypervisor_bucket); |
| 215 | |
| 216 | /* We were in userspace, nothing to do */ |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 217 | if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return pc; |
| 219 | |
| 220 | #ifdef CONFIG_PPC_RTAS |
| 221 | /* Were we in RTAS? */ |
| 222 | if (pc >= rtas.base && pc < (rtas.base + rtas.size)) |
| 223 | /* function descriptor madness */ |
| 224 | return *((unsigned long *)rtas_bucket); |
| 225 | #endif |
| 226 | |
| 227 | /* Were we in our exception vectors or SLB real mode miss handler? */ |
| 228 | if (pc < 0x1000000UL) |
| 229 | return (unsigned long)__va(pc); |
| 230 | |
| 231 | /* Not sure where we were */ |
Michael Ellerman | 51fae6d | 2005-12-04 18:39:15 +1100 | [diff] [blame] | 232 | if (!is_kernel_addr(pc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | /* function descriptor madness */ |
| 234 | return *((unsigned long *)kernel_unknown_bucket); |
| 235 | |
Anton Blanchard | 15e812a | 2006-03-27 12:00:45 +1100 | [diff] [blame] | 236 | return pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 239 | static int get_kernel(unsigned long pc, unsigned long mmcra) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | int is_kernel; |
| 242 | |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 243 | if (!cur_cpu_spec->oprofile_mmcra_sihv) { |
Michael Ellerman | 51fae6d | 2005-12-04 18:39:15 +1100 | [diff] [blame] | 244 | is_kernel = is_kernel_addr(pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } else { |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 246 | is_kernel = ((mmcra & cur_cpu_spec->oprofile_mmcra_sipr) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return is_kernel; |
| 250 | } |
| 251 | |
| 252 | static void power4_handle_interrupt(struct pt_regs *regs, |
| 253 | struct op_counter_config *ctr) |
| 254 | { |
| 255 | unsigned long pc; |
| 256 | int is_kernel; |
| 257 | int val; |
| 258 | int i; |
| 259 | unsigned int mmcr0; |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 260 | unsigned long mmcra; |
| 261 | |
| 262 | mmcra = mfspr(SPRN_MMCRA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | pc = get_pc(regs); |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 265 | is_kernel = get_kernel(pc, mmcra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | /* set the PMM bit (see comment below) */ |
| 268 | mtmsrd(mfmsr() | MSR_PMM); |
| 269 | |
Anton Blanchard | a6908cd | 2005-09-06 14:52:12 +1000 | [diff] [blame] | 270 | for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) { |
Olof Johansson | c69b767 | 2007-01-28 21:23:14 -0600 | [diff] [blame] | 271 | val = classic_ctr_read(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (val < 0) { |
| 273 | if (oprofile_running && ctr[i].enabled) { |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 274 | oprofile_add_ext_sample(pc, regs, i, is_kernel); |
Olof Johansson | c69b767 | 2007-01-28 21:23:14 -0600 | [diff] [blame] | 275 | classic_ctr_write(i, reset_value[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } else { |
Olof Johansson | c69b767 | 2007-01-28 21:23:14 -0600 | [diff] [blame] | 277 | classic_ctr_write(i, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | mmcr0 = mfspr(SPRN_MMCR0); |
| 283 | |
| 284 | /* reset the perfmon trigger */ |
| 285 | mmcr0 |= MMCR0_PMXE; |
| 286 | |
| 287 | /* |
| 288 | * We must clear the PMAO bit on some (GQ) chips. Just do it |
| 289 | * all the time |
| 290 | */ |
| 291 | mmcr0 &= ~MMCR0_PMAO; |
| 292 | |
Michael Neuling | e78dbc8 | 2006-06-08 14:42:34 +1000 | [diff] [blame] | 293 | /* Clear the appropriate bits in the MMCRA */ |
| 294 | mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear; |
| 295 | mtspr(SPRN_MMCRA, mmcra); |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* |
| 298 | * now clear the freeze bit, counting will not start until we |
| 299 | * rfid from this exception, because only at that point will |
| 300 | * the PMM bit be cleared |
| 301 | */ |
| 302 | mmcr0 &= ~MMCR0_FC; |
| 303 | mtspr(SPRN_MMCR0, mmcr0); |
| 304 | } |
| 305 | |
Stephen Rothwell | a3e48c1 | 2005-09-19 23:18:31 +1000 | [diff] [blame] | 306 | struct op_powerpc_model op_model_power4 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | .reg_setup = power4_reg_setup, |
| 308 | .cpu_setup = power4_cpu_setup, |
| 309 | .start = power4_start, |
| 310 | .stop = power4_stop, |
| 311 | .handle_interrupt = power4_handle_interrupt, |
| 312 | }; |