2 * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
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.
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>
18 #include <asm/oprofile_impl.h>
23 static unsigned long reset_value[OP_MAX_COUNTER];
25 static int oprofile_running;
26 static int mmcra_has_sihv;
28 /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
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
38 static int backtrace_spinlocks;
40 static void power4_reg_setup(struct op_counter_config *ctr,
41 struct op_system_config *sys,
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).
51 * It has been verified to work on POWER5 so we enable it there.
53 if (cpu_has_feature(CPU_FTR_MMCRA_SIHV))
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).
61 mmcr0_val = sys->mmcr0;
62 mmcr1_val = sys->mmcr1;
63 mmcra_val = sys->mmcra;
65 backtrace_spinlocks = sys->backtrace_spinlocks;
67 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
68 reset_value[i] = 0x80000000UL - ctr[i].count;
70 /* setup user and kernel profiling */
71 if (sys->enable_kernel)
72 mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
74 mmcr0_val |= MMCR0_KERNEL_DISABLE;
77 mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
79 mmcr0_val |= MMCR0_PROBLEM_DISABLE;
82 extern void ppc64_enable_pmcs(void);
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.
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.
94 static inline int mmcra_must_set_sample(void)
96 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
97 __is_processor(PV_970) || __is_processor(PV_970FX) ||
98 __is_processor(PV_970MP))
104 static void power4_cpu_setup(void *unused)
106 unsigned int mmcr0 = mmcr0_val;
107 unsigned long mmcra = mmcra_val;
111 /* set the freeze bit */
113 mtspr(SPRN_MMCR0, mmcr0);
115 mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
116 mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
117 mtspr(SPRN_MMCR0, mmcr0);
119 mtspr(SPRN_MMCR1, mmcr1_val);
121 if (mmcra_must_set_sample())
122 mmcra |= MMCRA_SAMPLE_ENABLE;
123 mtspr(SPRN_MMCRA, mmcra);
125 dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
127 dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
129 dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
133 static void power4_start(struct op_counter_config *ctr)
138 /* set the PMM bit (see comment below) */
139 mtmsrd(mfmsr() | MSR_PMM);
141 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
142 if (ctr[i].enabled) {
143 ctr_write(i, reset_value[i]);
149 mmcr0 = mfspr(SPRN_MMCR0);
152 * We must clear the PMAO bit on some (GQ) chips. Just do it
155 mmcr0 &= ~MMCR0_PMAO;
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
163 mtspr(SPRN_MMCR0, mmcr0);
165 oprofile_running = 1;
167 dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
170 static void power4_stop(void)
174 /* freeze counters */
175 mmcr0 = mfspr(SPRN_MMCR0);
177 mtspr(SPRN_MMCR0, mmcr0);
179 oprofile_running = 0;
181 dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
186 /* Fake functions used by canonicalize_pc */
187 static void __attribute_used__ hypervisor_bucket(void)
191 static void __attribute_used__ rtas_bucket(void)
195 static void __attribute_used__ kernel_unknown_bucket(void)
199 static unsigned long check_spinlock_pc(struct pt_regs *regs,
200 unsigned long profile_pc)
202 unsigned long pc = instruction_pointer(regs);
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.
211 if (backtrace_spinlocks && in_lock_functions(pc) &&
212 in_lock_functions(profile_pc))
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.
223 static unsigned long get_pc(struct pt_regs *regs)
225 unsigned long pc = mfspr(SPRN_SIAR);
228 /* Cant do much about it */
230 return check_spinlock_pc(regs, pc);
232 mmcra = mfspr(SPRN_MMCRA);
234 /* Were we in the hypervisor? */
235 if (platform_is_lpar() && (mmcra & MMCRA_SIHV))
236 /* function descriptor madness */
237 return *((unsigned long *)hypervisor_bucket);
239 /* We were in userspace, nothing to do */
240 if (mmcra & MMCRA_SIPR)
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);
250 /* Were we in our exception vectors or SLB real mode miss handler? */
251 if (pc < 0x1000000UL)
252 return (unsigned long)__va(pc);
254 /* Not sure where we were */
255 if (!is_kernel_addr(pc))
256 /* function descriptor madness */
257 return *((unsigned long *)kernel_unknown_bucket);
259 return check_spinlock_pc(regs, pc);
262 static int get_kernel(unsigned long pc)
266 if (!mmcra_has_sihv) {
267 is_kernel = is_kernel_addr(pc);
269 unsigned long mmcra = mfspr(SPRN_MMCRA);
270 is_kernel = ((mmcra & MMCRA_SIPR) == 0);
276 static void power4_handle_interrupt(struct pt_regs *regs,
277 struct op_counter_config *ctr)
286 is_kernel = get_kernel(pc);
288 /* set the PMM bit (see comment below) */
289 mtmsrd(mfmsr() | MSR_PMM);
291 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
294 if (oprofile_running && ctr[i].enabled) {
295 oprofile_add_pc(pc, is_kernel, i);
296 ctr_write(i, reset_value[i]);
303 mmcr0 = mfspr(SPRN_MMCR0);
305 /* reset the perfmon trigger */
309 * We must clear the PMAO bit on some (GQ) chips. Just do it
312 mmcr0 &= ~MMCR0_PMAO;
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
320 mtspr(SPRN_MMCR0, mmcr0);
323 struct op_powerpc_model op_model_power4 = {
324 .reg_setup = power4_reg_setup,
325 .cpu_setup = power4_cpu_setup,
326 .start = power4_start,
328 .handle_interrupt = power4_handle_interrupt,