vserver 1.9.3
[linux-2.6.git] / arch / ppc64 / 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/ptrace.h>
14 #include <asm/system.h>
15 #include <asm/processor.h>
16 #include <asm/cputable.h>
17 #include <asm/systemcfg.h>
18 #include <asm/rtas.h>
19
20 #define dbg(args...)
21
22 #include "op_impl.h"
23
24 static unsigned long reset_value[OP_MAX_COUNTER];
25
26 static int num_counters;
27 static int oprofile_running;
28 static int mmcra_has_sihv;
29
30 /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
31 static u32 mmcr0_val;
32 static u64 mmcr1_val;
33 static u32 mmcra_val;
34
35 static void power4_reg_setup(struct op_counter_config *ctr,
36                              struct op_system_config *sys,
37                              int num_ctrs)
38 {
39         int i;
40
41         num_counters = num_ctrs;
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 (cur_cpu_spec->cpu_features & 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 < num_counters; ++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 static void power4_cpu_setup(void *unused)
80 {
81         unsigned int mmcr0 = mmcr0_val;
82         unsigned long mmcra = mmcra_val;
83
84         ppc64_enable_pmcs();
85
86         /* set the freeze bit */
87         mmcr0 |= MMCR0_FC;
88         mtspr(SPRN_MMCR0, mmcr0);
89
90         mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
91         mmcr0 |= MMCR0_PMC1INTCONTROL|MMCR0_PMCNINTCONTROL;
92         mtspr(SPRN_MMCR0, mmcr0);
93
94         mtspr(SPRN_MMCR1, mmcr1_val);
95
96         mmcra |= MMCRA_SAMPLE_ENABLE;
97         mtspr(SPRN_MMCRA, mmcra);
98
99         dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
100             mfspr(SPRN_MMCR0));
101         dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
102             mfspr(SPRN_MMCR1));
103         dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
104             mfspr(SPRN_MMCRA));
105 }
106
107 static void power4_start(struct op_counter_config *ctr)
108 {
109         int i;
110         unsigned int mmcr0;
111
112         /* set the PMM bit (see comment below) */
113         mtmsrd(mfmsr() | MSR_PMM);
114
115         for (i = 0; i < num_counters; ++i) {
116                 if (ctr[i].enabled) {
117                         ctr_write(i, reset_value[i]);
118                 } else {
119                         ctr_write(i, 0);
120                 }
121         }
122
123         mmcr0 = mfspr(SPRN_MMCR0);
124
125         /*
126          * We must clear the PMAO bit on some (GQ) chips. Just do it
127          * all the time
128          */
129         mmcr0 &= ~MMCR0_PMAO;
130
131         /*
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
135          */
136         mmcr0 &= ~MMCR0_FC;
137         mtspr(SPRN_MMCR0, mmcr0);
138
139         oprofile_running = 1;
140
141         dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
142 }
143
144 static void power4_stop(void)
145 {
146         unsigned int mmcr0;
147
148         /* freeze counters */
149         mmcr0 = mfspr(SPRN_MMCR0);
150         mmcr0 |= MMCR0_FC;
151         mtspr(SPRN_MMCR0, mmcr0);
152
153         oprofile_running = 0;
154
155         dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
156
157         mb();
158 }
159
160 /* Fake functions used by canonicalize_pc */
161 static void __attribute_used__ hypervisor_bucket(void)
162 {
163 }
164
165 static void __attribute_used__ rtas_bucket(void)
166 {
167 }
168
169 static void __attribute_used__ kernel_unknown_bucket(void)
170 {
171 }
172
173 /*
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.
177  */
178 static unsigned long get_pc(void)
179 {
180         unsigned long pc = mfspr(SPRN_SIAR);
181         unsigned long mmcra;
182
183         /* Cant do much about it */
184         if (!mmcra_has_sihv)
185                 return pc;
186
187         mmcra = mfspr(SPRN_MMCRA);
188
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);
194
195         /* We were in userspace, nothing to do */
196         if (mmcra & MMCRA_SIPR)
197                 return pc;
198
199         /* Were we in our exception vectors? */
200         if (pc < 0x4000UL)
201                 return (unsigned long)__va(pc);
202
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);
208 #endif
209
210         /* Not sure where we were */
211         if (pc < KERNELBASE)
212                 /* function descriptor madness */
213                 return *((unsigned long *)kernel_unknown_bucket);
214
215         return pc;
216 }
217
218 static int get_kernel(unsigned long pc)
219 {
220         int is_kernel;
221
222         if (!mmcra_has_sihv) {
223                 is_kernel = (pc >= KERNELBASE);
224         } else {
225                 unsigned long mmcra = mfspr(SPRN_MMCRA);
226                 is_kernel = ((mmcra & MMCRA_SIPR) == 0);
227         }
228
229         return is_kernel;
230 }
231
232 static void power4_handle_interrupt(struct pt_regs *regs,
233                                     struct op_counter_config *ctr)
234 {
235         unsigned long pc;
236         int is_kernel;
237         int val;
238         int i;
239         unsigned int cpu = smp_processor_id();
240         unsigned int mmcr0;
241
242         pc = get_pc();
243         is_kernel = get_kernel(pc);
244
245         /* set the PMM bit (see comment below) */
246         mtmsrd(mfmsr() | MSR_PMM);
247
248         for (i = 0; i < num_counters; ++i) {
249                 val = ctr_read(i);
250                 if (val < 0) {
251                         if (oprofile_running && ctr[i].enabled) {
252                                 oprofile_add_sample(pc, is_kernel, i, cpu);
253                                 ctr_write(i, reset_value[i]);
254                         } else {
255                                 ctr_write(i, 0);
256                         }
257                 }
258         }
259
260         mmcr0 = mfspr(SPRN_MMCR0);
261
262         /* reset the perfmon trigger */
263         mmcr0 |= MMCR0_PMXE;
264
265         /*
266          * We must clear the PMAO bit on some (GQ) chips. Just do it
267          * all the time
268          */
269         mmcr0 &= ~MMCR0_PMAO;
270
271         /*
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
275          */
276         mmcr0 &= ~MMCR0_FC;
277         mtspr(SPRN_MMCR0, mmcr0);
278 }
279
280 struct op_ppc64_model op_model_power4 = {
281         .reg_setup              = power4_reg_setup,
282         .cpu_setup              = power4_cpu_setup,
283         .start                  = power4_start,
284         .stop                   = power4_stop,
285         .handle_interrupt       = power4_handle_interrupt,
286 };