blob: 95ae77dec3f6ada8e2fc6fc58a223ed235d0032a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/processor.h>
18#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/rtas.h>
Anton Blancharddca85932005-09-06 14:55:35 +100020#include <asm/oprofile_impl.h>
Anton Blanchardcb09cff2005-11-07 18:43:56 +110021#include <asm/reg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define dbg(args...)
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static unsigned long reset_value[OP_MAX_COUNTER];
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int oprofile_running;
Maynard Johnsone5fc9482009-05-07 05:48:32 +000028static int use_slot_nums;
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
Maynard Johnsone5fc9482009-05-07 05:48:32 +000064 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
65 __is_processor(PV_970) || __is_processor(PV_970FX) ||
66 __is_processor(PV_970MP) || __is_processor(PV_970GX) ||
67 __is_processor(PV_POWER5) || __is_processor(PV_POWER5p))
68 use_slot_nums = 1;
69
Bob Nelson14748552007-07-20 21:39:53 +020070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100073extern void ppc_enable_pmcs(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Anton Blanchardcb09cff2005-11-07 18:43:56 +110075/*
76 * Older CPUs require the MMCRA sample bit to be always set, but newer
77 * CPUs only want it set for some groups. Eventually we will remove all
78 * knowledge of this bit in the kernel, oprofile userspace should be
79 * setting it when required.
80 *
81 * In order to keep current installations working we force the bit for
82 * those older CPUs. Once everyone has updated their oprofile userspace we
83 * can remove this hack.
84 */
85static inline int mmcra_must_set_sample(void)
86{
87 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p) ||
88 __is_processor(PV_970) || __is_processor(PV_970FX) ||
Jake Moilanen362ff7b2006-10-18 10:47:22 -050089 __is_processor(PV_970MP) || __is_processor(PV_970GX))
Anton Blanchardcb09cff2005-11-07 18:43:56 +110090 return 1;
91
92 return 0;
93}
94
Bob Nelson14748552007-07-20 21:39:53 +020095static int power4_cpu_setup(struct op_counter_config *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 unsigned int mmcr0 = mmcr0_val;
98 unsigned long mmcra = mmcra_val;
99
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000100 ppc_enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* set the freeze bit */
103 mmcr0 |= MMCR0_FC;
104 mtspr(SPRN_MMCR0, mmcr0);
105
106 mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
107 mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
108 mtspr(SPRN_MMCR0, mmcr0);
109
110 mtspr(SPRN_MMCR1, mmcr1_val);
111
Anton Blanchardcb09cff2005-11-07 18:43:56 +1100112 if (mmcra_must_set_sample())
113 mmcra |= MMCRA_SAMPLE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 mtspr(SPRN_MMCRA, mmcra);
115
116 dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
117 mfspr(SPRN_MMCR0));
118 dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
119 mfspr(SPRN_MMCR1));
120 dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
121 mfspr(SPRN_MMCRA));
Bob Nelson14748552007-07-20 21:39:53 +0200122
123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Bob Nelson14748552007-07-20 21:39:53 +0200126static int power4_start(struct op_counter_config *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int i;
129 unsigned int mmcr0;
130
131 /* set the PMM bit (see comment below) */
132 mtmsrd(mfmsr() | MSR_PMM);
133
Anton Blancharda6908cd2005-09-06 14:52:12 +1000134 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (ctr[i].enabled) {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600136 classic_ctr_write(i, reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } else {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600138 classic_ctr_write(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 }
141
142 mmcr0 = mfspr(SPRN_MMCR0);
143
144 /*
145 * We must clear the PMAO bit on some (GQ) chips. Just do it
146 * all the time
147 */
148 mmcr0 &= ~MMCR0_PMAO;
149
150 /*
151 * now clear the freeze bit, counting will not start until we
152 * rfid from this excetion, because only at that point will
153 * the PMM bit be cleared
154 */
155 mmcr0 &= ~MMCR0_FC;
156 mtspr(SPRN_MMCR0, mmcr0);
157
158 oprofile_running = 1;
159
160 dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
Bob Nelson14748552007-07-20 21:39:53 +0200161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static void power4_stop(void)
165{
166 unsigned int mmcr0;
167
168 /* freeze counters */
169 mmcr0 = mfspr(SPRN_MMCR0);
170 mmcr0 |= MMCR0_FC;
171 mtspr(SPRN_MMCR0, mmcr0);
172
173 oprofile_running = 0;
174
175 dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
176
177 mb();
178}
179
180/* Fake functions used by canonicalize_pc */
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100181static void __used hypervisor_bucket(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183}
184
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100185static void __used rtas_bucket(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187}
188
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100189static void __used kernel_unknown_bucket(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * On GQ and newer the MMCRA stores the HV and PR bits at the time
195 * the SIAR was sampled. We use that to work out if the SIAR was sampled in
196 * the hypervisor, our exception vectors or RTAS.
will schmidt078f1942007-06-27 02:12:33 +1000197 * If the MMCRA_SAMPLE_ENABLE bit is set, we can use the MMCRA[slot] bits
198 * to more accurately identify the address of the sampled instruction. The
199 * mmcra[slot] bits represent the slot number of a sampled instruction
200 * within an instruction group. The slot will contain a value between 1
201 * and 5 if MMCRA_SAMPLE_ENABLE is set, otherwise 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
203static unsigned long get_pc(struct pt_regs *regs)
204{
205 unsigned long pc = mfspr(SPRN_SIAR);
206 unsigned long mmcra;
will schmidt078f1942007-06-27 02:12:33 +1000207 unsigned long slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300209 /* Can't do much about it */
Michael Neulinge78dbc82006-06-08 14:42:34 +1000210 if (!cur_cpu_spec->oprofile_mmcra_sihv)
Anton Blanchard15e812a2006-03-27 12:00:45 +1100211 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 mmcra = mfspr(SPRN_MMCRA);
214
Maynard Johnsone5fc9482009-05-07 05:48:32 +0000215 if (use_slot_nums && (mmcra & MMCRA_SAMPLE_ENABLE)) {
will schmidt078f1942007-06-27 02:12:33 +1000216 slot = ((mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT);
217 if (slot > 1)
218 pc += 4 * (slot - 1);
219 }
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Were we in the hypervisor? */
Michael Neulinge78dbc82006-06-08 14:42:34 +1000222 if (firmware_has_feature(FW_FEATURE_LPAR) &&
223 (mmcra & cur_cpu_spec->oprofile_mmcra_sihv))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* function descriptor madness */
225 return *((unsigned long *)hypervisor_bucket);
226
227 /* We were in userspace, nothing to do */
Michael Neulinge78dbc82006-06-08 14:42:34 +1000228 if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return pc;
230
231#ifdef CONFIG_PPC_RTAS
232 /* Were we in RTAS? */
233 if (pc >= rtas.base && pc < (rtas.base + rtas.size))
234 /* function descriptor madness */
235 return *((unsigned long *)rtas_bucket);
236#endif
237
238 /* Were we in our exception vectors or SLB real mode miss handler? */
239 if (pc < 0x1000000UL)
240 return (unsigned long)__va(pc);
241
242 /* Not sure where we were */
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100243 if (!is_kernel_addr(pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* function descriptor madness */
245 return *((unsigned long *)kernel_unknown_bucket);
246
Anton Blanchard15e812a2006-03-27 12:00:45 +1100247 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Michael Neulinge78dbc82006-06-08 14:42:34 +1000250static int get_kernel(unsigned long pc, unsigned long mmcra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 int is_kernel;
253
Michael Neulinge78dbc82006-06-08 14:42:34 +1000254 if (!cur_cpu_spec->oprofile_mmcra_sihv) {
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100255 is_kernel = is_kernel_addr(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } else {
Michael Neulinge78dbc82006-06-08 14:42:34 +1000257 is_kernel = ((mmcra & cur_cpu_spec->oprofile_mmcra_sipr) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 return is_kernel;
261}
262
Eric B Munsonad5d5292011-05-23 04:22:40 +0000263static bool pmc_overflow(unsigned long val)
264{
265 if ((int)val < 0)
266 return true;
267
268 /*
269 * Events on POWER7 can roll back if a speculative event doesn't
270 * eventually complete. Unfortunately in some rare cases they will
271 * raise a performance monitor exception. We need to catch this to
272 * ensure we reset the PMC. In all cases the PMC will be 256 or less
273 * cycles from overflow.
274 *
275 * We only do this if the first pass fails to find any overflowing
276 * PMCs because a user might set a period of less than 256 and we
277 * don't want to mistakenly reset them.
278 */
279 if (__is_processor(PV_POWER7) && ((0x80000000 - val) <= 256))
280 return true;
281
282 return false;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static void power4_handle_interrupt(struct pt_regs *regs,
286 struct op_counter_config *ctr)
287{
288 unsigned long pc;
289 int is_kernel;
290 int val;
291 int i;
292 unsigned int mmcr0;
Michael Neulinge78dbc82006-06-08 14:42:34 +1000293 unsigned long mmcra;
294
295 mmcra = mfspr(SPRN_MMCRA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 pc = get_pc(regs);
Michael Neulinge78dbc82006-06-08 14:42:34 +1000298 is_kernel = get_kernel(pc, mmcra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* set the PMM bit (see comment below) */
301 mtmsrd(mfmsr() | MSR_PMM);
302
Anton Blancharda6908cd2005-09-06 14:52:12 +1000303 for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600304 val = classic_ctr_read(i);
Eric B Munsonad5d5292011-05-23 04:22:40 +0000305 if (pmc_overflow(val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (oprofile_running && ctr[i].enabled) {
Brian Rogan6c6bd752006-03-27 11:57:01 +1100307 oprofile_add_ext_sample(pc, regs, i, is_kernel);
Olof Johanssonc69b7672007-01-28 21:23:14 -0600308 classic_ctr_write(i, reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } else {
Olof Johanssonc69b7672007-01-28 21:23:14 -0600310 classic_ctr_write(i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 }
313 }
314
315 mmcr0 = mfspr(SPRN_MMCR0);
316
317 /* reset the perfmon trigger */
318 mmcr0 |= MMCR0_PMXE;
319
320 /*
321 * We must clear the PMAO bit on some (GQ) chips. Just do it
322 * all the time
323 */
324 mmcr0 &= ~MMCR0_PMAO;
325
Michael Neulinge78dbc82006-06-08 14:42:34 +1000326 /* Clear the appropriate bits in the MMCRA */
327 mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear;
328 mtspr(SPRN_MMCRA, mmcra);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * now clear the freeze bit, counting will not start until we
332 * rfid from this exception, because only at that point will
333 * the PMM bit be cleared
334 */
335 mmcr0 &= ~MMCR0_FC;
336 mtspr(SPRN_MMCR0, mmcr0);
337}
338
Stephen Rothwella3e48c12005-09-19 23:18:31 +1000339struct op_powerpc_model op_model_power4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .reg_setup = power4_reg_setup,
341 .cpu_setup = power4_cpu_setup,
342 .start = power4_start,
343 .stop = power4_stop,
344 .handle_interrupt = power4_handle_interrupt,
345};