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>
17 #include <asm/systemcfg.h>
24 static unsigned long reset_value[OP_MAX_COUNTER];
26 static int num_counters;
27 static int oprofile_running;
28 static int mmcra_has_sihv;
30 /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
35 static void power4_reg_setup(struct op_counter_config *ctr,
36 struct op_system_config *sys,
41 num_counters = num_ctrs;
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 (cur_cpu_spec->cpu_features & 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 < num_counters; ++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);
79 static void power4_cpu_setup(void *unused)
81 unsigned int mmcr0 = mmcr0_val;
82 unsigned long mmcra = mmcra_val;
86 /* set the freeze bit */
88 mtspr(SPRN_MMCR0, mmcr0);
90 mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
91 mmcr0 |= MMCR0_PMC1INTCONTROL|MMCR0_PMCNINTCONTROL;
92 mtspr(SPRN_MMCR0, mmcr0);
94 mtspr(SPRN_MMCR1, mmcr1_val);
96 mmcra |= MMCRA_SAMPLE_ENABLE;
97 mtspr(SPRN_MMCRA, mmcra);
99 dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
101 dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
103 dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
107 static void power4_start(struct op_counter_config *ctr)
112 /* set the PMM bit (see comment below) */
113 mtmsrd(mfmsr() | MSR_PMM);
115 for (i = 0; i < num_counters; ++i) {
116 if (ctr[i].enabled) {
117 ctr_write(i, reset_value[i]);
123 mmcr0 = mfspr(SPRN_MMCR0);
126 * We must clear the PMAO bit on some (GQ) chips. Just do it
129 mmcr0 &= ~MMCR0_PMAO;
132 * now clear the freeze bit, counting will not start until we
133 * rfid from this excetion, because only at that point will
134 * the PMM bit be cleared
137 mtspr(SPRN_MMCR0, mmcr0);
139 oprofile_running = 1;
141 dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
144 static void power4_stop(void)
148 /* freeze counters */
149 mmcr0 = mfspr(SPRN_MMCR0);
151 mtspr(SPRN_MMCR0, mmcr0);
153 oprofile_running = 0;
155 dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
160 /* Fake functions used by canonicalize_pc */
161 static void __attribute_used__ hypervisor_bucket(void)
165 static void __attribute_used__ rtas_bucket(void)
169 static void __attribute_used__ kernel_unknown_bucket(void)
174 * On GQ and newer the MMCRA stores the HV and PR bits at the time
175 * the SIAR was sampled. We use that to work out if the SIAR was sampled in
176 * the hypervisor, our exception vectors or RTAS.
178 static unsigned long get_pc(void)
180 unsigned long pc = mfspr(SPRN_SIAR);
183 /* Cant do much about it */
187 mmcra = mfspr(SPRN_MMCRA);
189 /* Were we in the hypervisor? */
190 if ((systemcfg->platform == PLATFORM_PSERIES_LPAR) &&
191 (mmcra & MMCRA_SIHV))
192 /* function descriptor madness */
193 return *((unsigned long *)hypervisor_bucket);
195 /* We were in userspace, nothing to do */
196 if (mmcra & MMCRA_SIPR)
199 /* Were we in our exception vectors? */
201 return (unsigned long)__va(pc);
203 #ifdef CONFIG_PPC_PSERIES
204 /* Were we in RTAS? */
205 if (pc >= rtas.base && pc < (rtas.base + rtas.size))
206 /* function descriptor madness */
207 return *((unsigned long *)rtas_bucket);
210 /* Not sure where we were */
212 /* function descriptor madness */
213 return *((unsigned long *)kernel_unknown_bucket);
218 static int get_kernel(unsigned long pc)
222 if (!mmcra_has_sihv) {
223 is_kernel = (pc >= KERNELBASE);
225 unsigned long mmcra = mfspr(SPRN_MMCRA);
226 is_kernel = ((mmcra & MMCRA_SIPR) == 0);
232 static void power4_handle_interrupt(struct pt_regs *regs,
233 struct op_counter_config *ctr)
239 unsigned int cpu = smp_processor_id();
243 is_kernel = get_kernel(pc);
245 /* set the PMM bit (see comment below) */
246 mtmsrd(mfmsr() | MSR_PMM);
248 for (i = 0; i < num_counters; ++i) {
251 if (oprofile_running && ctr[i].enabled) {
252 oprofile_add_sample(pc, is_kernel, i, cpu);
253 ctr_write(i, reset_value[i]);
260 mmcr0 = mfspr(SPRN_MMCR0);
262 /* reset the perfmon trigger */
266 * We must clear the PMAO bit on some (GQ) chips. Just do it
269 mmcr0 &= ~MMCR0_PMAO;
272 * now clear the freeze bit, counting will not start until we
273 * rfid from this exception, because only at that point will
274 * the PMM bit be cleared
277 mtspr(SPRN_MMCR0, mmcr0);
280 struct op_ppc64_model op_model_power4 = {
281 .reg_setup = power4_reg_setup,
282 .cpu_setup = power4_cpu_setup,
283 .start = power4_start,
285 .handle_interrupt = power4_handle_interrupt,