4c2beab1fdc199dd14e562cde95ea5d05a809e45
[powerpc.git] / arch / powerpc / oprofile / op_model_power4.c
1 /*
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>
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>
18 #include <asm/rtas.h>
19 #include <asm/oprofile_impl.h>
20 #include <asm/reg.h>
21
22 #define dbg(args...)
23
24 static unsigned long reset_value[OP_MAX_COUNTER];
25
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;
31
32 /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
33 static u32 mmcr0_val;
34 static u64 mmcr1_val;
35 static u64 mmcra_val;
36
37 static void power4_reg_setup(struct op_counter_config *ctr,
38                              struct op_system_config *sys,
39                              int num_ctrs)
40 {
41         int i;
42
43         /*
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
62         for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
63                 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
77 extern void ppc64_enable_pmcs(void);
78
79 /*
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  */
89 static 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
99 static 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
116         if (mmcra_must_set_sample())
117                 mmcra |= MMCRA_SAMPLE_ENABLE;
118         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
128 static 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
136         for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
137                 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
165 static 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 */
182 static void __attribute_used__ hypervisor_bucket(void)
183 {
184 }
185
186 static void __attribute_used__ rtas_bucket(void)
187 {
188 }
189
190 static void __attribute_used__ kernel_unknown_bucket(void)
191 {
192 }
193
194 /*
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  */
199 static 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)
206                 return pc;
207
208         mmcra = mfspr(SPRN_MMCRA);
209
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);
214
215         /* We were in userspace, nothing to do */
216         if (mmcra & mmcra_sipr)
217                 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 */
231         if (!is_kernel_addr(pc))
232                 /* function descriptor madness */
233                 return *((unsigned long *)kernel_unknown_bucket);
234
235         return pc;
236 }
237
238 static int get_kernel(unsigned long pc)
239 {
240         int is_kernel;
241
242         if (!mmcra_has_sihv) {
243                 is_kernel = is_kernel_addr(pc);
244         } else {
245                 unsigned long mmcra = mfspr(SPRN_MMCRA);
246                 is_kernel = ((mmcra & mmcra_sipr) == 0);
247         }
248
249         return is_kernel;
250 }
251
252 static 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
267         for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
268                 val = ctr_read(i);
269                 if (val < 0) {
270                         if (oprofile_running && ctr[i].enabled) {
271                                 oprofile_add_ext_sample(pc, regs, i, is_kernel);
272                                 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
299 struct op_powerpc_model op_model_power4 = {
300         .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 };