blob: ca65469d7e30b141c3a072e41a8226bc505bb3da [file] [log] [blame]
Ralf Baechle54176732005-02-07 02:54:29 +00001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Ralf Baechle937a8012006-10-07 19:44:33 +01006 * Copyright (C) 2004, 05, 06 by Ralf Baechle
Ralf Baechle54176732005-02-07 02:54:29 +00007 * Copyright (C) 2005 by MIPS Technologies, Inc.
8 */
Ralf Baechle5e2862e2007-12-06 09:12:28 +00009#include <linux/cpumask.h>
Ralf Baechle54176732005-02-07 02:54:29 +000010#include <linux/oprofile.h>
11#include <linux/interrupt.h>
12#include <linux/smp.h>
Ralf Baechle937a8012006-10-07 19:44:33 +010013#include <asm/irq_regs.h>
Ralf Baechle54176732005-02-07 02:54:29 +000014
15#include "op_impl.h"
16
Ralf Baechle92c7b622006-06-23 18:39:00 +010017#define M_PERFCTL_EXL (1UL << 0)
18#define M_PERFCTL_KERNEL (1UL << 1)
19#define M_PERFCTL_SUPERVISOR (1UL << 2)
20#define M_PERFCTL_USER (1UL << 3)
21#define M_PERFCTL_INTERRUPT_ENABLE (1UL << 4)
Ralf Baechle39a51102008-01-29 10:14:59 +000022#define M_PERFCTL_EVENT(event) (((event) & 0x3ff) << 5)
Ralf Baechle92c7b622006-06-23 18:39:00 +010023#define M_PERFCTL_VPEID(vpe) ((vpe) << 16)
24#define M_PERFCTL_MT_EN(filter) ((filter) << 20)
25#define M_TC_EN_ALL M_PERFCTL_MT_EN(0)
26#define M_TC_EN_VPE M_PERFCTL_MT_EN(1)
27#define M_TC_EN_TC M_PERFCTL_MT_EN(2)
28#define M_PERFCTL_TCID(tcid) ((tcid) << 22)
29#define M_PERFCTL_WIDE (1UL << 30)
30#define M_PERFCTL_MORE (1UL << 31)
Ralf Baechle54176732005-02-07 02:54:29 +000031
Ralf Baechle92c7b622006-06-23 18:39:00 +010032#define M_COUNTER_OVERFLOW (1UL << 31)
33
34#ifdef CONFIG_MIPS_MT_SMP
Ralf Baechle39b8d522008-04-28 17:14:26 +010035static int cpu_has_mipsmt_pertccounters;
36#define WHAT (M_TC_EN_VPE | \
37 M_PERFCTL_VPEID(cpu_data[smp_processor_id()].vpe_id))
38#define vpe_id() (cpu_has_mipsmt_pertccounters ? \
39 0 : cpu_data[smp_processor_id()].vpe_id)
Ralf Baechle5e2862e2007-12-06 09:12:28 +000040
41/*
42 * The number of bits to shift to convert between counters per core and
43 * counters per VPE. There is no reasonable interface atm to obtain the
44 * number of VPEs used by Linux and in the 34K this number is fixed to two
45 * anyways so we hardcore a few things here for the moment. The way it's
46 * done here will ensure that oprofile VSMP kernel will run right on a lesser
47 * core like a 24K also or with maxcpus=1.
48 */
49static inline unsigned int vpe_shift(void)
50{
51 if (num_possible_cpus() > 1)
52 return 1;
53
54 return 0;
55}
56
Ralf Baechle92c7b622006-06-23 18:39:00 +010057#else
Ralf Baechle5e2862e2007-12-06 09:12:28 +000058
Ralf Baechlebe609f32006-10-23 13:22:06 +010059#define WHAT 0
Ralf Baechle6f4c5bd2007-04-24 21:42:20 +010060#define vpe_id() 0
Ralf Baechle5e2862e2007-12-06 09:12:28 +000061
62static inline unsigned int vpe_shift(void)
63{
64 return 0;
65}
66
Ralf Baechle92c7b622006-06-23 18:39:00 +010067#endif
68
Ralf Baechle5e2862e2007-12-06 09:12:28 +000069static inline unsigned int counters_total_to_per_cpu(unsigned int counters)
70{
71 return counters >> vpe_shift();
72}
73
74static inline unsigned int counters_per_cpu_to_total(unsigned int counters)
75{
76 return counters << vpe_shift();
77}
78
Ralf Baechle92c7b622006-06-23 18:39:00 +010079#define __define_perf_accessors(r, n, np) \
80 \
81static inline unsigned int r_c0_ ## r ## n(void) \
82{ \
Ralf Baechlebe609f32006-10-23 13:22:06 +010083 unsigned int cpu = vpe_id(); \
Ralf Baechle92c7b622006-06-23 18:39:00 +010084 \
85 switch (cpu) { \
86 case 0: \
87 return read_c0_ ## r ## n(); \
88 case 1: \
89 return read_c0_ ## r ## np(); \
90 default: \
91 BUG(); \
92 } \
Thiemo Seufer30f244a2006-07-07 10:38:51 +010093 return 0; \
Ralf Baechle92c7b622006-06-23 18:39:00 +010094} \
95 \
96static inline void w_c0_ ## r ## n(unsigned int value) \
97{ \
Ralf Baechlebe609f32006-10-23 13:22:06 +010098 unsigned int cpu = vpe_id(); \
Ralf Baechle92c7b622006-06-23 18:39:00 +010099 \
100 switch (cpu) { \
101 case 0: \
102 write_c0_ ## r ## n(value); \
103 return; \
104 case 1: \
105 write_c0_ ## r ## np(value); \
106 return; \
107 default: \
108 BUG(); \
109 } \
Thiemo Seufer30f244a2006-07-07 10:38:51 +0100110 return; \
Ralf Baechle92c7b622006-06-23 18:39:00 +0100111} \
112
113__define_perf_accessors(perfcntr, 0, 2)
114__define_perf_accessors(perfcntr, 1, 3)
Chris Dearman795a2252007-03-01 17:58:24 +0000115__define_perf_accessors(perfcntr, 2, 0)
116__define_perf_accessors(perfcntr, 3, 1)
Ralf Baechle92c7b622006-06-23 18:39:00 +0100117
118__define_perf_accessors(perfctrl, 0, 2)
119__define_perf_accessors(perfctrl, 1, 3)
Chris Dearman795a2252007-03-01 17:58:24 +0000120__define_perf_accessors(perfctrl, 2, 0)
121__define_perf_accessors(perfctrl, 3, 1)
Ralf Baechle54176732005-02-07 02:54:29 +0000122
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900123struct op_mips_model op_model_mipsxx_ops;
Ralf Baechle54176732005-02-07 02:54:29 +0000124
125static struct mipsxx_register_config {
126 unsigned int control[4];
127 unsigned int counter[4];
128} reg;
129
130/* Compute all of the registers in preparation for enabling profiling. */
131
132static void mipsxx_reg_setup(struct op_counter_config *ctr)
133{
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900134 unsigned int counters = op_model_mipsxx_ops.num_counters;
Ralf Baechle54176732005-02-07 02:54:29 +0000135 int i;
136
137 /* Compute the performance counter control word. */
Ralf Baechle54176732005-02-07 02:54:29 +0000138 for (i = 0; i < counters; i++) {
139 reg.control[i] = 0;
140 reg.counter[i] = 0;
141
142 if (!ctr[i].enabled)
143 continue;
144
145 reg.control[i] = M_PERFCTL_EVENT(ctr[i].event) |
146 M_PERFCTL_INTERRUPT_ENABLE;
147 if (ctr[i].kernel)
148 reg.control[i] |= M_PERFCTL_KERNEL;
149 if (ctr[i].user)
150 reg.control[i] |= M_PERFCTL_USER;
151 if (ctr[i].exl)
152 reg.control[i] |= M_PERFCTL_EXL;
153 reg.counter[i] = 0x80000000 - ctr[i].count;
154 }
155}
156
157/* Program all of the registers in preparation for enabling profiling. */
158
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100159static void mipsxx_cpu_setup(void *args)
Ralf Baechle54176732005-02-07 02:54:29 +0000160{
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900161 unsigned int counters = op_model_mipsxx_ops.num_counters;
Ralf Baechle54176732005-02-07 02:54:29 +0000162
163 switch (counters) {
164 case 4:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100165 w_c0_perfctrl3(0);
166 w_c0_perfcntr3(reg.counter[3]);
Ralf Baechle54176732005-02-07 02:54:29 +0000167 case 3:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100168 w_c0_perfctrl2(0);
169 w_c0_perfcntr2(reg.counter[2]);
Ralf Baechle54176732005-02-07 02:54:29 +0000170 case 2:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100171 w_c0_perfctrl1(0);
172 w_c0_perfcntr1(reg.counter[1]);
Ralf Baechle54176732005-02-07 02:54:29 +0000173 case 1:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100174 w_c0_perfctrl0(0);
175 w_c0_perfcntr0(reg.counter[0]);
Ralf Baechle54176732005-02-07 02:54:29 +0000176 }
177}
178
179/* Start all counters on current CPU */
180static void mipsxx_cpu_start(void *args)
181{
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900182 unsigned int counters = op_model_mipsxx_ops.num_counters;
Ralf Baechle54176732005-02-07 02:54:29 +0000183
184 switch (counters) {
185 case 4:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100186 w_c0_perfctrl3(WHAT | reg.control[3]);
Ralf Baechle54176732005-02-07 02:54:29 +0000187 case 3:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100188 w_c0_perfctrl2(WHAT | reg.control[2]);
Ralf Baechle54176732005-02-07 02:54:29 +0000189 case 2:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100190 w_c0_perfctrl1(WHAT | reg.control[1]);
Ralf Baechle54176732005-02-07 02:54:29 +0000191 case 1:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100192 w_c0_perfctrl0(WHAT | reg.control[0]);
Ralf Baechle54176732005-02-07 02:54:29 +0000193 }
194}
195
196/* Stop all counters on current CPU */
197static void mipsxx_cpu_stop(void *args)
198{
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900199 unsigned int counters = op_model_mipsxx_ops.num_counters;
Ralf Baechle54176732005-02-07 02:54:29 +0000200
201 switch (counters) {
202 case 4:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100203 w_c0_perfctrl3(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000204 case 3:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100205 w_c0_perfctrl2(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000206 case 2:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100207 w_c0_perfctrl1(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000208 case 1:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100209 w_c0_perfctrl0(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000210 }
211}
212
Ralf Baechle937a8012006-10-07 19:44:33 +0100213static int mipsxx_perfcount_handler(void)
Ralf Baechle54176732005-02-07 02:54:29 +0000214{
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900215 unsigned int counters = op_model_mipsxx_ops.num_counters;
Ralf Baechle54176732005-02-07 02:54:29 +0000216 unsigned int control;
217 unsigned int counter;
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100218 int handled = IRQ_NONE;
219
220 if (cpu_has_mips_r2 && !(read_c0_cause() & (1 << 26)))
221 return handled;
Ralf Baechle54176732005-02-07 02:54:29 +0000222
223 switch (counters) {
224#define HANDLE_COUNTER(n) \
225 case n + 1: \
Ralf Baechle92c7b622006-06-23 18:39:00 +0100226 control = r_c0_perfctrl ## n(); \
227 counter = r_c0_perfcntr ## n(); \
Ralf Baechle54176732005-02-07 02:54:29 +0000228 if ((control & M_PERFCTL_INTERRUPT_ENABLE) && \
229 (counter & M_COUNTER_OVERFLOW)) { \
Ralf Baechle937a8012006-10-07 19:44:33 +0100230 oprofile_add_sample(get_irq_regs(), n); \
Ralf Baechle92c7b622006-06-23 18:39:00 +0100231 w_c0_perfcntr ## n(reg.counter[n]); \
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100232 handled = IRQ_HANDLED; \
Ralf Baechle54176732005-02-07 02:54:29 +0000233 }
234 HANDLE_COUNTER(3)
235 HANDLE_COUNTER(2)
236 HANDLE_COUNTER(1)
237 HANDLE_COUNTER(0)
238 }
Ralf Baechleba339c02005-12-09 12:29:38 +0000239
240 return handled;
Ralf Baechle54176732005-02-07 02:54:29 +0000241}
242
243#define M_CONFIG1_PC (1 << 4)
244
Ralf Baechle92c7b622006-06-23 18:39:00 +0100245static inline int __n_counters(void)
Ralf Baechle54176732005-02-07 02:54:29 +0000246{
247 if (!(read_c0_config1() & M_CONFIG1_PC))
248 return 0;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100249 if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
Ralf Baechle54176732005-02-07 02:54:29 +0000250 return 1;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100251 if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
Ralf Baechle54176732005-02-07 02:54:29 +0000252 return 2;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100253 if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
Ralf Baechle54176732005-02-07 02:54:29 +0000254 return 3;
255
256 return 4;
257}
258
Ralf Baechle92c7b622006-06-23 18:39:00 +0100259static inline int n_counters(void)
260{
Ralf Baechle714cfe72006-10-23 00:44:02 +0100261 int counters;
262
Ralf Baechle10cc3522007-10-11 23:46:15 +0100263 switch (current_cpu_type()) {
Ralf Baechle714cfe72006-10-23 00:44:02 +0100264 case CPU_R10000:
265 counters = 2;
Ralf Baechle148171b2007-02-28 15:34:22 +0000266 break;
Ralf Baechle714cfe72006-10-23 00:44:02 +0100267
268 case CPU_R12000:
269 case CPU_R14000:
270 counters = 4;
Ralf Baechle148171b2007-02-28 15:34:22 +0000271 break;
Ralf Baechle714cfe72006-10-23 00:44:02 +0100272
273 default:
274 counters = __n_counters();
275 }
Ralf Baechle92c7b622006-06-23 18:39:00 +0100276
Ralf Baechle92c7b622006-06-23 18:39:00 +0100277 return counters;
278}
279
Ralf Baechle39b8d522008-04-28 17:14:26 +0100280static void reset_counters(void *arg)
Ralf Baechle54176732005-02-07 02:54:29 +0000281{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100282 int counters = (int)arg;
Ralf Baechle54176732005-02-07 02:54:29 +0000283 switch (counters) {
284 case 4:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100285 w_c0_perfctrl3(0);
286 w_c0_perfcntr3(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000287 case 3:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100288 w_c0_perfctrl2(0);
289 w_c0_perfcntr2(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000290 case 2:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100291 w_c0_perfctrl1(0);
292 w_c0_perfcntr1(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000293 case 1:
Ralf Baechle92c7b622006-06-23 18:39:00 +0100294 w_c0_perfctrl0(0);
295 w_c0_perfcntr0(0);
Ralf Baechle54176732005-02-07 02:54:29 +0000296 }
297}
298
299static int __init mipsxx_init(void)
300{
301 int counters;
302
303 counters = n_counters();
Ralf Baechle9efeae92005-12-09 12:34:45 +0000304 if (counters == 0) {
305 printk(KERN_ERR "Oprofile: CPU has no performance counters\n");
Ralf Baechle54176732005-02-07 02:54:29 +0000306 return -ENODEV;
Ralf Baechle9efeae92005-12-09 12:34:45 +0000307 }
Ralf Baechle54176732005-02-07 02:54:29 +0000308
Ralf Baechle39b8d522008-04-28 17:14:26 +0100309#ifdef CONFIG_MIPS_MT_SMP
310 cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19);
311 if (!cpu_has_mipsmt_pertccounters)
312 counters = counters_total_to_per_cpu(counters);
313#endif
314 on_each_cpu(reset_counters, (void *)counters, 0, 1);
Chris Dearman795a2252007-03-01 17:58:24 +0000315
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900316 op_model_mipsxx_ops.num_counters = counters;
Ralf Baechle10cc3522007-10-11 23:46:15 +0100317 switch (current_cpu_type()) {
Ralf Baechle20659882005-12-09 12:42:13 +0000318 case CPU_20KC:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900319 op_model_mipsxx_ops.cpu_type = "mips/20K";
Ralf Baechle20659882005-12-09 12:42:13 +0000320 break;
321
Ralf Baechle54176732005-02-07 02:54:29 +0000322 case CPU_24K:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900323 op_model_mipsxx_ops.cpu_type = "mips/24K";
Ralf Baechle54176732005-02-07 02:54:29 +0000324 break;
325
Ralf Baechle20659882005-12-09 12:42:13 +0000326 case CPU_25KF:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900327 op_model_mipsxx_ops.cpu_type = "mips/25K";
Ralf Baechle20659882005-12-09 12:42:13 +0000328 break;
329
Ralf Baechle39b8d522008-04-28 17:14:26 +0100330 case CPU_1004K:
331#if 0
332 /* FIXME: report as 34K for now */
333 op_model_mipsxx_ops.cpu_type = "mips/1004K";
334 break;
335#endif
336
Ralf Baechlefcfd9802006-02-01 17:54:30 +0000337 case CPU_34K:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900338 op_model_mipsxx_ops.cpu_type = "mips/34K";
Ralf Baechlefcfd9802006-02-01 17:54:30 +0000339 break;
Chris Dearmanc6209532006-05-02 14:08:46 +0100340
341 case CPU_74K:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900342 op_model_mipsxx_ops.cpu_type = "mips/74K";
Chris Dearmanc6209532006-05-02 14:08:46 +0100343 break;
Ralf Baechlefcfd9802006-02-01 17:54:30 +0000344
Ralf Baechle20659882005-12-09 12:42:13 +0000345 case CPU_5KC:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900346 op_model_mipsxx_ops.cpu_type = "mips/5K";
Ralf Baechle20659882005-12-09 12:42:13 +0000347 break;
348
Ralf Baechle714cfe72006-10-23 00:44:02 +0100349 case CPU_R10000:
350 if ((current_cpu_data.processor_id & 0xff) == 0x20)
351 op_model_mipsxx_ops.cpu_type = "mips/r10000-v2.x";
352 else
353 op_model_mipsxx_ops.cpu_type = "mips/r10000";
354 break;
355
356 case CPU_R12000:
357 case CPU_R14000:
358 op_model_mipsxx_ops.cpu_type = "mips/r12000";
359 break;
360
Mark Masonc03bc122006-01-17 12:06:32 -0800361 case CPU_SB1:
362 case CPU_SB1A:
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900363 op_model_mipsxx_ops.cpu_type = "mips/sb1";
Mark Masonc03bc122006-01-17 12:06:32 -0800364 break;
365
Ralf Baechle54176732005-02-07 02:54:29 +0000366 default:
367 printk(KERN_ERR "Profiling unsupported for this CPU\n");
368
369 return -ENODEV;
370 }
371
372 perf_irq = mipsxx_perfcount_handler;
373
374 return 0;
375}
376
377static void mipsxx_exit(void)
378{
Chris Dearman795a2252007-03-01 17:58:24 +0000379 int counters = op_model_mipsxx_ops.num_counters;
Ralf Baechle5e2862e2007-12-06 09:12:28 +0000380
381 counters = counters_per_cpu_to_total(counters);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100382 on_each_cpu(reset_counters, (void *)counters, 0, 1);
Ralf Baechle54176732005-02-07 02:54:29 +0000383
384 perf_irq = null_perf_irq;
385}
386
Atsushi Nemoto1acf1ca2006-05-23 16:42:38 +0900387struct op_mips_model op_model_mipsxx_ops = {
Ralf Baechle54176732005-02-07 02:54:29 +0000388 .reg_setup = mipsxx_reg_setup,
389 .cpu_setup = mipsxx_cpu_setup,
390 .init = mipsxx_init,
391 .exit = mipsxx_exit,
392 .cpu_start = mipsxx_cpu_start,
393 .cpu_stop = mipsxx_cpu_stop,
394};