blob: cddc250a6a5cf13325556175cbbed3473c61ec02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
will schmidt078f1942007-06-27 02:12:33 +10003 * Added mmcra[slot] support:
4 * Copyright (C) 2006-2007 Will Schmidt <willschm@us.ibm.com>, IBM
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 Ellerman57cfb812006-03-21 20:45:59 +110015#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/ptrace.h>
17#include <asm/system.h>
18#include <asm/processor.h>
19#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/rtas.h>
Anton Blancharddca85932005-09-06 14:55:35 +100021#include <asm/oprofile_impl.h>
Anton Blanchardcb09cff2005-11-07 18:43:56 +110022#include <asm/reg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define dbg(args...)
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static unsigned long reset_value[OP_MAX_COUNTER];
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int oprofile_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
31static u32 mmcr0_val;
32static u64 mmcr1_val;
Anton Blanchard15e812a2006-03-27 12:00:45 +110033static u64 mmcra_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Bob Nelson14748552007-07-20 21:39:53 +020035static int power4_reg_setup(struct op_counter_config *ctr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct op_system_config *sys,
37 int num_ctrs)
38{
39 int i;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * The performance counter event settings are given in the mmcr0,
43 * mmcr1 and mmcra values passed from the user in the
44 * op_system_config structure (sys variable).
45 */
46 mmcr0_val = sys->mmcr0;
47 mmcr1_val = sys->mmcr1;
48 mmcra_val = sys->mmcra;
49
Anton Blancharda6908cd2005-09-06 14:52:12 +100050 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 reset_value[i] = 0x80000000UL - ctr[i].count;
52
53 /* setup user and kernel profiling */
54 if (sys->enable_kernel)
55 mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
56 else
57 mmcr0_val |= MMCR0_KERNEL_DISABLE;
58
59 if (sys->enable_user)
60 mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
61 else
62 mmcr0_val |= MMCR0_PROBLEM_DISABLE;
Bob Nelson14748552007-07-20 21:39:53 +020063
64 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67extern void ppc64_enable_pmcs(void);
68
Anton Blanchardcb09cff2005-11-07 18:43:56 +110069/*
70 * Older CPUs require the MMCRA sample bit to be always set, but newer
71 * CPUs only want it set for some groups. Eventually we will remove all
72 * knowledge of this bit in the kernel, oprofile userspace should be
73 * setting it when required.
74 *
75 * In order to keep current installations working we force the bit for
76 * those older CPUs. Once everyone has updated their oprofile userspace we
77 * can remove this hack.
78 */
79static inline int mmcra_must_set_sample(void)
80{
81 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
82 __is_processor(PV_970) || __is_processor(PV_970FX) ||
Jake Moilanen362ff7b2006-10-18 10:47:22 -050083 __is_processor(PV_970MP) || __is_processor(PV_970GX))
Anton Blanchardcb09cff2005-11-07 18:43:56 +110084 return 1;
85
86 return 0;
87}
88
Bob Nelson14748552007-07-20 21:39:53 +020089static int power4_cpu_setup(struct op_counter_config *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 unsigned int mmcr0 = mmcr0_val;
92 unsigned long mmcra = mmcra_val;
93
94 ppc64_enable_pmcs();
95
96 /* set the freeze bit */
97 mmcr0 |= MMCR0_FC;
98 mtspr(SPRN_MMCR0, mmcr0);
99
100 mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
101 mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
102 mtspr(SPRN_MMCR0, mmcr0);
103
104 mtspr(SPRN_MMCR1, mmcr1_val);
105
Anton Blanchardcb09cff2005-11-07 18:43:56 +1100106 if (mmcra_must_set_sample())
107 mmcra |= MMCRA_SAMPLE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 mtspr(SPRN_MMCRA, mmcra);
109
110 dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
111 mfspr(SPRN_MMCR0));
112 dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
113 mfspr(SPRN_MMCR1));
114 dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
115 mfspr(SPRN_MMCRA));
Bob Nelson14748552007-07-20 21:39:53 +0200116
117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Bob Nelson14748552007-07-20 21:39:53 +0200120static int power4_start(struct op_counter_config *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 int i;
123 unsigned int mmcr0;
124
125 /* set the PMM bit (see comment below) */
126 mtmsrd(mfmsr() | MSR_PMM);
127
Anton Blancharda6908cd2005-09-06 14:52:12 +1000128 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (ctr[i].enabled) {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600130 classic_ctr_write(i, reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } else {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600132 classic_ctr_write(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 }
135
136 mmcr0 = mfspr(SPRN_MMCR0);
137
138 /*
139 * We must clear the PMAO bit on some (GQ) chips. Just do it
140 * all the time
141 */
142 mmcr0 &= ~MMCR0_PMAO;
143
144 /*
145 * now clear the freeze bit, counting will not start until we
146 * rfid from this excetion, because only at that point will
147 * the PMM bit be cleared
148 */
149 mmcr0 &= ~MMCR0_FC;
150 mtspr(SPRN_MMCR0, mmcr0);
151
152 oprofile_running = 1;
153
154 dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
Bob Nelson14748552007-07-20 21:39:53 +0200155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static void power4_stop(void)
159{
160 unsigned int mmcr0;
161
162 /* freeze counters */
163 mmcr0 = mfspr(SPRN_MMCR0);
164 mmcr0 |= MMCR0_FC;
165 mtspr(SPRN_MMCR0, mmcr0);
166
167 oprofile_running = 0;
168
169 dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
170
171 mb();
172}
173
174/* Fake functions used by canonicalize_pc */
175static void __attribute_used__ hypervisor_bucket(void)
176{
177}
178
179static void __attribute_used__ rtas_bucket(void)
180{
181}
182
183static void __attribute_used__ kernel_unknown_bucket(void)
184{
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * On GQ and newer the MMCRA stores the HV and PR bits at the time
189 * the SIAR was sampled. We use that to work out if the SIAR was sampled in
190 * the hypervisor, our exception vectors or RTAS.
will schmidt078f1942007-06-27 02:12:33 +1000191 * If the MMCRA_SAMPLE_ENABLE bit is set, we can use the MMCRA[slot] bits
192 * to more accurately identify the address of the sampled instruction. The
193 * mmcra[slot] bits represent the slot number of a sampled instruction
194 * within an instruction group. The slot will contain a value between 1
195 * and 5 if MMCRA_SAMPLE_ENABLE is set, otherwise 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197static unsigned long get_pc(struct pt_regs *regs)
198{
199 unsigned long pc = mfspr(SPRN_SIAR);
200 unsigned long mmcra;
will schmidt078f1942007-06-27 02:12:33 +1000201 unsigned long slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Cant do much about it */
Michael Neulinge78dbc82006-06-08 14:42:34 +1000204 if (!cur_cpu_spec->oprofile_mmcra_sihv)
Anton Blanchard15e812a2006-03-27 12:00:45 +1100205 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 mmcra = mfspr(SPRN_MMCRA);
208
will schmidt078f1942007-06-27 02:12:33 +1000209 if (mmcra & MMCRA_SAMPLE_ENABLE) {
210 slot = ((mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT);
211 if (slot > 1)
212 pc += 4 * (slot - 1);
213 }
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Were we in the hypervisor? */
Michael Neulinge78dbc82006-06-08 14:42:34 +1000216 if (firmware_has_feature(FW_FEATURE_LPAR) &&
217 (mmcra & cur_cpu_spec->oprofile_mmcra_sihv))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* function descriptor madness */
219 return *((unsigned long *)hypervisor_bucket);
220
221 /* We were in userspace, nothing to do */
Michael Neulinge78dbc82006-06-08 14:42:34 +1000222 if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return pc;
224
225#ifdef CONFIG_PPC_RTAS
226 /* Were we in RTAS? */
227 if (pc >= rtas.base && pc < (rtas.base + rtas.size))
228 /* function descriptor madness */
229 return *((unsigned long *)rtas_bucket);
230#endif
231
232 /* Were we in our exception vectors or SLB real mode miss handler? */
233 if (pc < 0x1000000UL)
234 return (unsigned long)__va(pc);
235
236 /* Not sure where we were */
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100237 if (!is_kernel_addr(pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* function descriptor madness */
239 return *((unsigned long *)kernel_unknown_bucket);
240
Anton Blanchard15e812a2006-03-27 12:00:45 +1100241 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Michael Neulinge78dbc82006-06-08 14:42:34 +1000244static int get_kernel(unsigned long pc, unsigned long mmcra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int is_kernel;
247
Michael Neulinge78dbc82006-06-08 14:42:34 +1000248 if (!cur_cpu_spec->oprofile_mmcra_sihv) {
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100249 is_kernel = is_kernel_addr(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 } else {
Michael Neulinge78dbc82006-06-08 14:42:34 +1000251 is_kernel = ((mmcra & cur_cpu_spec->oprofile_mmcra_sipr) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 return is_kernel;
255}
256
257static void power4_handle_interrupt(struct pt_regs *regs,
258 struct op_counter_config *ctr)
259{
260 unsigned long pc;
261 int is_kernel;
262 int val;
263 int i;
264 unsigned int mmcr0;
Michael Neulinge78dbc82006-06-08 14:42:34 +1000265 unsigned long mmcra;
266
267 mmcra = mfspr(SPRN_MMCRA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 pc = get_pc(regs);
Michael Neulinge78dbc82006-06-08 14:42:34 +1000270 is_kernel = get_kernel(pc, mmcra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* set the PMM bit (see comment below) */
273 mtmsrd(mfmsr() | MSR_PMM);
274
Anton Blancharda6908cd2005-09-06 14:52:12 +1000275 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600276 val = classic_ctr_read(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (val < 0) {
278 if (oprofile_running && ctr[i].enabled) {
Brian Rogan6c6bd752006-03-27 11:57:01 +1100279 oprofile_add_ext_sample(pc, regs, i, is_kernel);
Olof Johanssonc69b7672007-01-28 21:23:14 -0600280 classic_ctr_write(i, reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600282 classic_ctr_write(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 }
285 }
286
287 mmcr0 = mfspr(SPRN_MMCR0);
288
289 /* reset the perfmon trigger */
290 mmcr0 |= MMCR0_PMXE;
291
292 /*
293 * We must clear the PMAO bit on some (GQ) chips. Just do it
294 * all the time
295 */
296 mmcr0 &= ~MMCR0_PMAO;
297
Michael Neulinge78dbc82006-06-08 14:42:34 +1000298 /* Clear the appropriate bits in the MMCRA */
299 mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear;
300 mtspr(SPRN_MMCRA, mmcra);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /*
303 * now clear the freeze bit, counting will not start until we
304 * rfid from this exception, because only at that point will
305 * the PMM bit be cleared
306 */
307 mmcr0 &= ~MMCR0_FC;
308 mtspr(SPRN_MMCR0, mmcr0);
309}
310
Stephen Rothwella3e48c12005-09-19 23:18:31 +1000311struct op_powerpc_model op_model_power4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .reg_setup = power4_reg_setup,
313 .cpu_setup = power4_cpu_setup,
314 .start = power4_start,
315 .stop = power4_stop,
316 .handle_interrupt = power4_handle_interrupt,
317};