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
6 * Copyright (C) 2004, 2005 by Ralf Baechle
7 * Copyright (C) 2005 by MIPS Technologies, Inc.
9 #include <linux/oprofile.h>
10 #include <linux/interrupt.h>
11 #include <linux/smp.h>
15 #define M_PERFCTL_EXL (1UL << 0)
16 #define M_PERFCTL_KERNEL (1UL << 1)
17 #define M_PERFCTL_SUPERVISOR (1UL << 2)
18 #define M_PERFCTL_USER (1UL << 3)
19 #define M_PERFCTL_INTERRUPT_ENABLE (1UL << 4)
20 #define M_PERFCTL_EVENT(event) ((event) << 5)
21 #define M_PERFCTL_WIDE (1UL << 30)
22 #define M_PERFCTL_MORE (1UL << 31)
24 #define M_COUNTER_OVERFLOW (1UL << 31)
26 struct op_mips_model op_model_mipsxx;
28 static struct mipsxx_register_config {
29 unsigned int control[4];
30 unsigned int counter[4];
33 /* Compute all of the registers in preparation for enabling profiling. */
35 static void mipsxx_reg_setup(struct op_counter_config *ctr)
37 unsigned int counters = op_model_mipsxx.num_counters;
40 /* Compute the performance counter control word. */
41 /* For now count kernel and user mode */
42 for (i = 0; i < counters; i++) {
49 reg.control[i] = M_PERFCTL_EVENT(ctr[i].event) |
50 M_PERFCTL_INTERRUPT_ENABLE;
52 reg.control[i] |= M_PERFCTL_KERNEL;
54 reg.control[i] |= M_PERFCTL_USER;
56 reg.control[i] |= M_PERFCTL_EXL;
57 reg.counter[i] = 0x80000000 - ctr[i].count;
61 /* Program all of the registers in preparation for enabling profiling. */
63 static void mipsxx_cpu_setup (void *args)
65 unsigned int counters = op_model_mipsxx.num_counters;
69 write_c0_perfctrl3(0);
70 write_c0_perfcntr3(reg.counter[3]);
72 write_c0_perfctrl2(0);
73 write_c0_perfcntr2(reg.counter[2]);
75 write_c0_perfctrl1(0);
76 write_c0_perfcntr1(reg.counter[1]);
78 write_c0_perfctrl0(0);
79 write_c0_perfcntr0(reg.counter[0]);
83 /* Start all counters on current CPU */
84 static void mipsxx_cpu_start(void *args)
86 unsigned int counters = op_model_mipsxx.num_counters;
90 write_c0_perfctrl3(reg.control[3]);
92 write_c0_perfctrl2(reg.control[2]);
94 write_c0_perfctrl1(reg.control[1]);
96 write_c0_perfctrl0(reg.control[0]);
100 /* Stop all counters on current CPU */
101 static void mipsxx_cpu_stop(void *args)
103 unsigned int counters = op_model_mipsxx.num_counters;
107 write_c0_perfctrl3(0);
109 write_c0_perfctrl2(0);
111 write_c0_perfctrl1(0);
113 write_c0_perfctrl0(0);
117 static int mipsxx_perfcount_handler(struct pt_regs *regs)
119 unsigned int counters = op_model_mipsxx.num_counters;
120 unsigned int control;
121 unsigned int counter;
125 #define HANDLE_COUNTER(n) \
127 control = read_c0_perfctrl ## n(); \
128 counter = read_c0_perfcntr ## n(); \
129 if ((control & M_PERFCTL_INTERRUPT_ENABLE) && \
130 (counter & M_COUNTER_OVERFLOW)) { \
131 oprofile_add_sample(regs, n); \
132 write_c0_perfcntr ## n(reg.counter[n]); \
144 #define M_CONFIG1_PC (1 << 4)
146 static inline int n_counters(void)
148 if (!(read_c0_config1() & M_CONFIG1_PC))
150 if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
152 if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
154 if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
160 static inline void reset_counters(int counters)
164 write_c0_perfctrl3(0);
165 write_c0_perfcntr3(0);
167 write_c0_perfctrl2(0);
168 write_c0_perfcntr2(0);
170 write_c0_perfctrl1(0);
171 write_c0_perfcntr1(0);
173 write_c0_perfctrl0(0);
174 write_c0_perfcntr0(0);
178 static int __init mipsxx_init(void)
182 counters = n_counters();
184 printk(KERN_ERR "Oprofile: CPU has no performance counters\n");
188 reset_counters(counters);
190 op_model_mipsxx.num_counters = counters;
191 switch (current_cpu_data.cputype) {
193 op_model_mipsxx.cpu_type = "mips/20K";
197 op_model_mipsxx.cpu_type = "mips/24K";
201 op_model_mipsxx.cpu_type = "mips/25K";
205 op_model_mipsxx.cpu_type = "mips/5K";
209 printk(KERN_ERR "Profiling unsupported for this CPU\n");
214 perf_irq = mipsxx_perfcount_handler;
219 static void mipsxx_exit(void)
221 reset_counters(op_model_mipsxx.num_counters);
223 perf_irq = null_perf_irq;
226 struct op_mips_model op_model_mipsxx = {
227 .reg_setup = mipsxx_reg_setup,
228 .cpu_setup = mipsxx_cpu_setup,
231 .cpu_start = mipsxx_cpu_start,
232 .cpu_stop = mipsxx_cpu_stop,