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