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/firmware.h>
14 #include <asm/ptrace.h>
15 #include <asm/system.h>
16 #include <asm/processor.h>
17 #include <asm/cputable.h>
19 #include <asm/oprofile_impl.h>
24 static unsigned long reset_value[OP_MAX_COUNTER];
26 static int oprofile_running;
27 static int mmcra_has_sihv;
28 /* Unfortunately these bits vary between CPUs */
29 static unsigned long mmcra_sihv = MMCRA_SIHV;
30 static unsigned long mmcra_sipr = MMCRA_SIPR;
32 /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
37 static void power4_reg_setup(struct op_counter_config *ctr,
38 struct op_system_config *sys,
44 * SIHV / SIPR bits are only implemented on POWER4+ (GQ) and above.
45 * However we disable it on all POWER4 until we verify it works
46 * (I was seeing some strange behaviour last time I tried).
48 * It has been verified to work on POWER5 so we enable it there.
50 if (cpu_has_feature(CPU_FTR_MMCRA_SIHV))
54 * The performance counter event settings are given in the mmcr0,
55 * mmcr1 and mmcra values passed from the user in the
56 * op_system_config structure (sys variable).
58 mmcr0_val = sys->mmcr0;
59 mmcr1_val = sys->mmcr1;
60 mmcra_val = sys->mmcra;
62 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
63 reset_value[i] = 0x80000000UL - ctr[i].count;
65 /* setup user and kernel profiling */
66 if (sys->enable_kernel)
67 mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
69 mmcr0_val |= MMCR0_KERNEL_DISABLE;
72 mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
74 mmcr0_val |= MMCR0_PROBLEM_DISABLE;
77 extern void ppc64_enable_pmcs(void);
80 * Older CPUs require the MMCRA sample bit to be always set, but newer
81 * CPUs only want it set for some groups. Eventually we will remove all
82 * knowledge of this bit in the kernel, oprofile userspace should be
83 * setting it when required.
85 * In order to keep current installations working we force the bit for
86 * those older CPUs. Once everyone has updated their oprofile userspace we
87 * can remove this hack.
89 static inline int mmcra_must_set_sample(void)
91 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
92 __is_processor(PV_970) || __is_processor(PV_970FX) ||
93 __is_processor(PV_970MP))
99 static void power4_cpu_setup(void *unused)
101 unsigned int mmcr0 = mmcr0_val;
102 unsigned long mmcra = mmcra_val;
106 /* set the freeze bit */
108 mtspr(SPRN_MMCR0, mmcr0);
110 mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
111 mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
112 mtspr(SPRN_MMCR0, mmcr0);
114 mtspr(SPRN_MMCR1, mmcr1_val);
116 if (mmcra_must_set_sample())
117 mmcra |= MMCRA_SAMPLE_ENABLE;
118 mtspr(SPRN_MMCRA, mmcra);
120 dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
122 dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
124 dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
128 static void power4_start(struct op_counter_config *ctr)
133 /* set the PMM bit (see comment below) */
134 mtmsrd(mfmsr() | MSR_PMM);
136 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
137 if (ctr[i].enabled) {
138 ctr_write(i, reset_value[i]);
144 mmcr0 = mfspr(SPRN_MMCR0);
147 * We must clear the PMAO bit on some (GQ) chips. Just do it
150 mmcr0 &= ~MMCR0_PMAO;
153 * now clear the freeze bit, counting will not start until we
154 * rfid from this excetion, because only at that point will
155 * the PMM bit be cleared
158 mtspr(SPRN_MMCR0, mmcr0);
160 oprofile_running = 1;
162 dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
165 static void power4_stop(void)
169 /* freeze counters */
170 mmcr0 = mfspr(SPRN_MMCR0);
172 mtspr(SPRN_MMCR0, mmcr0);
174 oprofile_running = 0;
176 dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
181 /* Fake functions used by canonicalize_pc */
182 static void __attribute_used__ hypervisor_bucket(void)
186 static void __attribute_used__ rtas_bucket(void)
190 static void __attribute_used__ kernel_unknown_bucket(void)
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.
199 static unsigned long get_pc(struct pt_regs *regs)
201 unsigned long pc = mfspr(SPRN_SIAR);
204 /* Cant do much about it */
208 mmcra = mfspr(SPRN_MMCRA);
210 /* Were we in the hypervisor? */
211 if (firmware_has_feature(FW_FEATURE_LPAR) && (mmcra & mmcra_sihv))
212 /* function descriptor madness */
213 return *((unsigned long *)hypervisor_bucket);
215 /* We were in userspace, nothing to do */
216 if (mmcra & mmcra_sipr)
219 #ifdef CONFIG_PPC_RTAS
220 /* Were we in RTAS? */
221 if (pc >= rtas.base && pc < (rtas.base + rtas.size))
222 /* function descriptor madness */
223 return *((unsigned long *)rtas_bucket);
226 /* Were we in our exception vectors or SLB real mode miss handler? */
227 if (pc < 0x1000000UL)
228 return (unsigned long)__va(pc);
230 /* Not sure where we were */
231 if (!is_kernel_addr(pc))
232 /* function descriptor madness */
233 return *((unsigned long *)kernel_unknown_bucket);
238 static int get_kernel(unsigned long pc)
242 if (!mmcra_has_sihv) {
243 is_kernel = is_kernel_addr(pc);
245 unsigned long mmcra = mfspr(SPRN_MMCRA);
246 is_kernel = ((mmcra & mmcra_sipr) == 0);
252 static void power4_handle_interrupt(struct pt_regs *regs,
253 struct op_counter_config *ctr)
262 is_kernel = get_kernel(pc);
264 /* set the PMM bit (see comment below) */
265 mtmsrd(mfmsr() | MSR_PMM);
267 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
270 if (oprofile_running && ctr[i].enabled) {
271 oprofile_add_ext_sample(pc, regs, i, is_kernel);
272 ctr_write(i, reset_value[i]);
279 mmcr0 = mfspr(SPRN_MMCR0);
281 /* reset the perfmon trigger */
285 * We must clear the PMAO bit on some (GQ) chips. Just do it
288 mmcr0 &= ~MMCR0_PMAO;
291 * now clear the freeze bit, counting will not start until we
292 * rfid from this exception, because only at that point will
293 * the PMM bit be cleared
296 mtspr(SPRN_MMCR0, mmcr0);
299 struct op_powerpc_model op_model_power4 = {
300 .reg_setup = power4_reg_setup,
301 .cpu_setup = power4_cpu_setup,
302 .start = power4_start,
304 .handle_interrupt = power4_handle_interrupt,