[PATCH] powerpc: merge the rest of arch/ppc*/oprofile
[linux-2.6.git] / arch / powerpc / oprofile / common.c
1 /*
2  * PPC 64 oprofile support:
3  * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
4  * PPC 32 oprofile support: (based on PPC 64 support)
5  * Copyright (C) Freescale Semiconductor, Inc 2004
6  *      Author: Andy Fleming
7  *
8  * Based on alpha version.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version
13  * 2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/oprofile.h>
17 #ifndef __powerpc64__
18 #include <linux/slab.h>
19 #endif /* ! __powerpc64__ */
20 #include <linux/init.h>
21 #include <linux/smp.h>
22 #include <linux/errno.h>
23 #include <asm/ptrace.h>
24 #include <asm/system.h>
25 #ifdef __powerpc64__
26 #include <asm/pmc.h>
27 #else /* __powerpc64__ */
28 #include <asm/perfmon.h>
29 #endif /* __powerpc64__ */
30 #include <asm/cputable.h>
31 #include <asm/oprofile_impl.h>
32
33 static struct op_powerpc_model *model;
34
35 static struct op_counter_config ctr[OP_MAX_COUNTER];
36 static struct op_system_config sys;
37
38 #ifndef __powerpc64__
39 static char *cpu_type;
40 #endif /* ! __powerpc64__ */
41
42 static void op_handle_interrupt(struct pt_regs *regs)
43 {
44         model->handle_interrupt(regs, ctr);
45 }
46
47 static int op_powerpc_setup(void)
48 {
49 #ifdef __powerpc64__
50         int err;
51
52         /* Grab the hardware */
53         err = reserve_pmc_hardware(op_handle_interrupt);
54         if (err)
55                 return err;
56 #else /* __powerpc64__ */
57         /* Install our interrupt handler into the existing hook.  */
58         if (request_perfmon_irq(&op_handle_interrupt))
59                 return -EBUSY;
60         mb();
61 #endif /* __powerpc64__ */
62
63         /* Pre-compute the values to stuff in the hardware registers.  */
64         model->reg_setup(ctr, &sys, model->num_counters);
65
66         /* Configure the registers on all cpus.  */
67 #ifdef __powerpc64__
68         on_each_cpu(model->cpu_setup, NULL, 0, 1);
69 #else /* __powerpc64__ */
70 #if 0
71         /* FIXME: Make multi-cpu work */
72         on_each_cpu(model->reg_setup, NULL, 0, 1);
73 #endif
74 #endif /* __powerpc64__ */
75
76         return 0;
77 }
78
79 static void op_powerpc_shutdown(void)
80 {
81 #ifdef __powerpc64__
82         release_pmc_hardware();
83 #else /* __powerpc64__ */
84         mb();
85         /* Remove our interrupt handler. We may be removing this module. */
86         free_perfmon_irq();
87 #endif /* __powerpc64__ */
88 }
89
90 static void op_powerpc_cpu_start(void *dummy)
91 {
92         model->start(ctr);
93 }
94
95 static int op_powerpc_start(void)
96 {
97         on_each_cpu(op_powerpc_cpu_start, NULL, 0, 1);
98         return 0;
99 }
100
101 static inline void op_powerpc_cpu_stop(void *dummy)
102 {
103         model->stop();
104 }
105
106 static void op_powerpc_stop(void)
107 {
108         on_each_cpu(op_powerpc_cpu_stop, NULL, 0, 1);
109 }
110
111 static int op_powerpc_create_files(struct super_block *sb, struct dentry *root)
112 {
113         int i;
114
115 #ifdef __powerpc64__
116         /*
117          * There is one mmcr0, mmcr1 and mmcra for setting the events for
118          * all of the counters.
119          */
120         oprofilefs_create_ulong(sb, root, "mmcr0", &sys.mmcr0);
121         oprofilefs_create_ulong(sb, root, "mmcr1", &sys.mmcr1);
122         oprofilefs_create_ulong(sb, root, "mmcra", &sys.mmcra);
123 #endif /* __powerpc64__ */
124
125         for (i = 0; i < model->num_counters; ++i) {
126                 struct dentry *dir;
127                 char buf[3];
128
129                 snprintf(buf, sizeof buf, "%d", i);
130                 dir = oprofilefs_mkdir(sb, root, buf);
131
132                 oprofilefs_create_ulong(sb, dir, "enabled", &ctr[i].enabled);
133                 oprofilefs_create_ulong(sb, dir, "event", &ctr[i].event);
134                 oprofilefs_create_ulong(sb, dir, "count", &ctr[i].count);
135 #ifdef __powerpc64__
136                 /*
137                  * We dont support per counter user/kernel selection, but
138                  * we leave the entries because userspace expects them
139                  */
140 #endif /* __powerpc64__ */
141                 oprofilefs_create_ulong(sb, dir, "kernel", &ctr[i].kernel);
142                 oprofilefs_create_ulong(sb, dir, "user", &ctr[i].user);
143
144 #ifndef __powerpc64__
145                 /* FIXME: Not sure if this is used */
146 #endif /* ! __powerpc64__ */
147                 oprofilefs_create_ulong(sb, dir, "unit_mask", &ctr[i].unit_mask);
148         }
149
150         oprofilefs_create_ulong(sb, root, "enable_kernel", &sys.enable_kernel);
151         oprofilefs_create_ulong(sb, root, "enable_user", &sys.enable_user);
152 #ifdef __powerpc64__
153         oprofilefs_create_ulong(sb, root, "backtrace_spinlocks",
154                                 &sys.backtrace_spinlocks);
155 #endif /* __powerpc64__ */
156
157         /* Default to tracing both kernel and user */
158         sys.enable_kernel = 1;
159         sys.enable_user = 1;
160 #ifdef __powerpc64__
161         /* Turn on backtracing through spinlocks by default */
162         sys.backtrace_spinlocks = 1;
163 #endif /* __powerpc64__ */
164
165         return 0;
166 }
167
168 int __init oprofile_arch_init(struct oprofile_operations *ops)
169 {
170 #ifndef __powerpc64__
171         int cpu_id = smp_processor_id();
172
173 #ifdef CONFIG_FSL_BOOKE
174         model = &op_model_fsl_booke;
175 #else
176         return -ENODEV;
177 #endif
178
179         cpu_type = kmalloc(32, GFP_KERNEL);
180         if (NULL == cpu_type)
181                 return -ENOMEM;
182
183         sprintf(cpu_type, "ppc/%s", cur_cpu_spec[cpu_id]->cpu_name);
184
185         model->num_counters = cur_cpu_spec[cpu_id]->num_pmcs;
186
187         ops->cpu_type = cpu_type;
188 #else /* __powerpc64__ */
189         if (!cur_cpu_spec->oprofile_model || !cur_cpu_spec->oprofile_cpu_type)
190                 return -ENODEV;
191         model = cur_cpu_spec->oprofile_model;
192         model->num_counters = cur_cpu_spec->num_pmcs;
193
194         ops->cpu_type = cur_cpu_spec->oprofile_cpu_type;
195 #endif /* __powerpc64__ */
196         ops->create_files = op_powerpc_create_files;
197         ops->setup = op_powerpc_setup;
198         ops->shutdown = op_powerpc_shutdown;
199         ops->start = op_powerpc_start;
200         ops->stop = op_powerpc_stop;
201
202         printk(KERN_INFO "oprofile: using %s performance monitoring.\n",
203                ops->cpu_type);
204
205         return 0;
206 }
207
208 void oprofile_arch_exit(void)
209 {
210 #ifndef __powerpc64__
211         kfree(cpu_type);
212         cpu_type = NULL;
213 #endif /* ! __powerpc64__ */
214 }