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