Merge Stephen Rothwell's patches
[powerpc.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         int err;
50
51         /* Grab the hardware */
52         err = reserve_pmc_hardware(op_handle_interrupt);
53         if (err)
54                 return err;
55
56         /* Pre-compute the values to stuff in the hardware registers.  */
57         model->reg_setup(ctr, &sys, model->num_counters);
58
59         /* Configure the registers on all cpus.  */
60 #ifdef __powerpc64__
61         on_each_cpu(model->cpu_setup, NULL, 0, 1);
62 #else /* __powerpc64__ */
63 #if 0
64         /* FIXME: Make multi-cpu work */
65         on_each_cpu(model->reg_setup, NULL, 0, 1);
66 #endif
67 #endif /* __powerpc64__ */
68
69         return 0;
70 }
71
72 static void op_powerpc_shutdown(void)
73 {
74         release_pmc_hardware();
75 }
76
77 static void op_powerpc_cpu_start(void *dummy)
78 {
79         model->start(ctr);
80 }
81
82 static int op_powerpc_start(void)
83 {
84         on_each_cpu(op_powerpc_cpu_start, NULL, 0, 1);
85         return 0;
86 }
87
88 static inline void op_powerpc_cpu_stop(void *dummy)
89 {
90         model->stop();
91 }
92
93 static void op_powerpc_stop(void)
94 {
95         on_each_cpu(op_powerpc_cpu_stop, NULL, 0, 1);
96 }
97
98 static int op_powerpc_create_files(struct super_block *sb, struct dentry *root)
99 {
100         int i;
101
102 #ifdef __powerpc64__
103         /*
104          * There is one mmcr0, mmcr1 and mmcra for setting the events for
105          * all of the counters.
106          */
107         oprofilefs_create_ulong(sb, root, "mmcr0", &sys.mmcr0);
108         oprofilefs_create_ulong(sb, root, "mmcr1", &sys.mmcr1);
109         oprofilefs_create_ulong(sb, root, "mmcra", &sys.mmcra);
110 #endif /* __powerpc64__ */
111
112         for (i = 0; i < model->num_counters; ++i) {
113                 struct dentry *dir;
114                 char buf[3];
115
116                 snprintf(buf, sizeof buf, "%d", i);
117                 dir = oprofilefs_mkdir(sb, root, buf);
118
119                 oprofilefs_create_ulong(sb, dir, "enabled", &ctr[i].enabled);
120                 oprofilefs_create_ulong(sb, dir, "event", &ctr[i].event);
121                 oprofilefs_create_ulong(sb, dir, "count", &ctr[i].count);
122 #ifdef __powerpc64__
123                 /*
124                  * We dont support per counter user/kernel selection, but
125                  * we leave the entries because userspace expects them
126                  */
127 #endif /* __powerpc64__ */
128                 oprofilefs_create_ulong(sb, dir, "kernel", &ctr[i].kernel);
129                 oprofilefs_create_ulong(sb, dir, "user", &ctr[i].user);
130
131 #ifndef __powerpc64__
132                 /* FIXME: Not sure if this is used */
133 #endif /* ! __powerpc64__ */
134                 oprofilefs_create_ulong(sb, dir, "unit_mask", &ctr[i].unit_mask);
135         }
136
137         oprofilefs_create_ulong(sb, root, "enable_kernel", &sys.enable_kernel);
138         oprofilefs_create_ulong(sb, root, "enable_user", &sys.enable_user);
139 #ifdef __powerpc64__
140         oprofilefs_create_ulong(sb, root, "backtrace_spinlocks",
141                                 &sys.backtrace_spinlocks);
142 #endif /* __powerpc64__ */
143
144         /* Default to tracing both kernel and user */
145         sys.enable_kernel = 1;
146         sys.enable_user = 1;
147 #ifdef __powerpc64__
148         /* Turn on backtracing through spinlocks by default */
149         sys.backtrace_spinlocks = 1;
150 #endif /* __powerpc64__ */
151
152         return 0;
153 }
154
155 int __init oprofile_arch_init(struct oprofile_operations *ops)
156 {
157 #ifndef __powerpc64__
158 #ifdef CONFIG_FSL_BOOKE
159         model = &op_model_fsl_booke;
160 #else
161         return -ENODEV;
162 #endif
163
164         cpu_type = kmalloc(32, GFP_KERNEL);
165         if (NULL == cpu_type)
166                 return -ENOMEM;
167
168         sprintf(cpu_type, "ppc/%s", cur_cpu_spec->cpu_name);
169
170         model->num_counters = cur_cpu_spec->num_pmcs;
171
172         ops->cpu_type = cpu_type;
173 #else /* __powerpc64__ */
174         if (!cur_cpu_spec->oprofile_model || !cur_cpu_spec->oprofile_cpu_type)
175                 return -ENODEV;
176         model = cur_cpu_spec->oprofile_model;
177         model->num_counters = cur_cpu_spec->num_pmcs;
178
179         ops->cpu_type = cur_cpu_spec->oprofile_cpu_type;
180 #endif /* __powerpc64__ */
181         ops->create_files = op_powerpc_create_files;
182         ops->setup = op_powerpc_setup;
183         ops->shutdown = op_powerpc_shutdown;
184         ops->start = op_powerpc_start;
185         ops->stop = op_powerpc_stop;
186
187         printk(KERN_INFO "oprofile: using %s performance monitoring.\n",
188                ops->cpu_type);
189
190         return 0;
191 }
192
193 void oprofile_arch_exit(void)
194 {
195 #ifndef __powerpc64__
196         kfree(cpu_type);
197         cpu_type = NULL;
198 #endif /* ! __powerpc64__ */
199 }