blob: 4c2beab1fdc199dd14e562cde95ea5d05a809e45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Michael Ellerman57cfb812006-03-21 20:45:59 +110013#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/ptrace.h>
15#include <asm/system.h>
16#include <asm/processor.h>
17#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/rtas.h>
Anton Blancharddca85932005-09-06 14:55:35 +100019#include <asm/oprofile_impl.h>
Anton Blanchardcb09cff2005-11-07 18:43:56 +110020#include <asm/reg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define dbg(args...)
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static unsigned long reset_value[OP_MAX_COUNTER];
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int oprofile_running;
27static int mmcra_has_sihv;
Anton Blanchard15e812a2006-03-27 12:00:45 +110028/* Unfortunately these bits vary between CPUs */
29static unsigned long mmcra_sihv = MMCRA_SIHV;
30static unsigned long mmcra_sipr = MMCRA_SIPR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
33static u32 mmcr0_val;
34static u64 mmcr1_val;
Anton Blanchard15e812a2006-03-27 12:00:45 +110035static u64 mmcra_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static void power4_reg_setup(struct op_counter_config *ctr,
38 struct op_system_config *sys,
39 int num_ctrs)
40{
41 int i;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 /*
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).
47 *
48 * It has been verified to work on POWER5 so we enable it there.
49 */
50 if (cpu_has_feature(CPU_FTR_MMCRA_SIHV))
51 mmcra_has_sihv = 1;
52
53 /*
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).
57 */
58 mmcr0_val = sys->mmcr0;
59 mmcr1_val = sys->mmcr1;
60 mmcra_val = sys->mmcra;
61
Anton Blancharda6908cd2005-09-06 14:52:12 +100062 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 reset_value[i] = 0x80000000UL - ctr[i].count;
64
65 /* setup user and kernel profiling */
66 if (sys->enable_kernel)
67 mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
68 else
69 mmcr0_val |= MMCR0_KERNEL_DISABLE;
70
71 if (sys->enable_user)
72 mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
73 else
74 mmcr0_val |= MMCR0_PROBLEM_DISABLE;
75}
76
77extern void ppc64_enable_pmcs(void);
78
Anton Blanchardcb09cff2005-11-07 18:43:56 +110079/*
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.
84 *
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.
88 */
89static inline int mmcra_must_set_sample(void)
90{
91 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
92 __is_processor(PV_970) || __is_processor(PV_970FX) ||
93 __is_processor(PV_970MP))
94 return 1;
95
96 return 0;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void power4_cpu_setup(void *unused)
100{
101 unsigned int mmcr0 = mmcr0_val;
102 unsigned long mmcra = mmcra_val;
103
104 ppc64_enable_pmcs();
105
106 /* set the freeze bit */
107 mmcr0 |= MMCR0_FC;
108 mtspr(SPRN_MMCR0, mmcr0);
109
110 mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
111 mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
112 mtspr(SPRN_MMCR0, mmcr0);
113
114 mtspr(SPRN_MMCR1, mmcr1_val);
115
Anton Blanchardcb09cff2005-11-07 18:43:56 +1100116 if (mmcra_must_set_sample())
117 mmcra |= MMCRA_SAMPLE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mtspr(SPRN_MMCRA, mmcra);
119
120 dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
121 mfspr(SPRN_MMCR0));
122 dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
123 mfspr(SPRN_MMCR1));
124 dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
125 mfspr(SPRN_MMCRA));
126}
127
128static void power4_start(struct op_counter_config *ctr)
129{
130 int i;
131 unsigned int mmcr0;
132
133 /* set the PMM bit (see comment below) */
134 mtmsrd(mfmsr() | MSR_PMM);
135
Anton Blancharda6908cd2005-09-06 14:52:12 +1000136 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (ctr[i].enabled) {
138 ctr_write(i, reset_value[i]);
139 } else {
140 ctr_write(i, 0);
141 }
142 }
143
144 mmcr0 = mfspr(SPRN_MMCR0);
145
146 /*
147 * We must clear the PMAO bit on some (GQ) chips. Just do it
148 * all the time
149 */
150 mmcr0 &= ~MMCR0_PMAO;
151
152 /*
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
156 */
157 mmcr0 &= ~MMCR0_FC;
158 mtspr(SPRN_MMCR0, mmcr0);
159
160 oprofile_running = 1;
161
162 dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
163}
164
165static 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 */
182static void __attribute_used__ hypervisor_bucket(void)
183{
184}
185
186static void __attribute_used__ rtas_bucket(void)
187{
188}
189
190static void __attribute_used__ kernel_unknown_bucket(void)
191{
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
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.
198 */
199static unsigned long get_pc(struct pt_regs *regs)
200{
201 unsigned long pc = mfspr(SPRN_SIAR);
202 unsigned long mmcra;
203
204 /* Cant do much about it */
205 if (!mmcra_has_sihv)
Anton Blanchard15e812a2006-03-27 12:00:45 +1100206 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 mmcra = mfspr(SPRN_MMCRA);
209
210 /* Were we in the hypervisor? */
Anton Blanchard15e812a2006-03-27 12:00:45 +1100211 if (firmware_has_feature(FW_FEATURE_LPAR) && (mmcra & mmcra_sihv))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* function descriptor madness */
213 return *((unsigned long *)hypervisor_bucket);
214
215 /* We were in userspace, nothing to do */
Anton Blanchard15e812a2006-03-27 12:00:45 +1100216 if (mmcra & mmcra_sipr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return pc;
218
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);
224#endif
225
226 /* Were we in our exception vectors or SLB real mode miss handler? */
227 if (pc < 0x1000000UL)
228 return (unsigned long)__va(pc);
229
230 /* Not sure where we were */
Michael Ellerman51fae6d2005-12-04 18:39:15 +1100231 if (!is_kernel_addr(pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* function descriptor madness */
233 return *((unsigned long *)kernel_unknown_bucket);
234
Anton Blanchard15e812a2006-03-27 12:00:45 +1100235 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static int get_kernel(unsigned long pc)
239{
240 int is_kernel;
241
242 if (!mmcra_has_sihv) {
Michael Ellerman51fae6d2005-12-04 18:39:15 +1100243 is_kernel = is_kernel_addr(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else {
245 unsigned long mmcra = mfspr(SPRN_MMCRA);
Anton Blanchard15e812a2006-03-27 12:00:45 +1100246 is_kernel = ((mmcra & mmcra_sipr) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 return is_kernel;
250}
251
252static void power4_handle_interrupt(struct pt_regs *regs,
253 struct op_counter_config *ctr)
254{
255 unsigned long pc;
256 int is_kernel;
257 int val;
258 int i;
259 unsigned int mmcr0;
260
261 pc = get_pc(regs);
262 is_kernel = get_kernel(pc);
263
264 /* set the PMM bit (see comment below) */
265 mtmsrd(mfmsr() | MSR_PMM);
266
Anton Blancharda6908cd2005-09-06 14:52:12 +1000267 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 val = ctr_read(i);
269 if (val < 0) {
270 if (oprofile_running && ctr[i].enabled) {
Brian Rogan6c6bd752006-03-27 11:57:01 +1100271 oprofile_add_ext_sample(pc, regs, i, is_kernel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 ctr_write(i, reset_value[i]);
273 } else {
274 ctr_write(i, 0);
275 }
276 }
277 }
278
279 mmcr0 = mfspr(SPRN_MMCR0);
280
281 /* reset the perfmon trigger */
282 mmcr0 |= MMCR0_PMXE;
283
284 /*
285 * We must clear the PMAO bit on some (GQ) chips. Just do it
286 * all the time
287 */
288 mmcr0 &= ~MMCR0_PMAO;
289
290 /*
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
294 */
295 mmcr0 &= ~MMCR0_FC;
296 mtspr(SPRN_MMCR0, mmcr0);
297}
298
Stephen Rothwella3e48c12005-09-19 23:18:31 +1000299struct op_powerpc_model op_model_power4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .reg_setup = power4_reg_setup,
301 .cpu_setup = power4_cpu_setup,
302 .start = power4_start,
303 .stop = power4_stop,
304 .handle_interrupt = power4_handle_interrupt,
305};