blob: 17807acb05d998da3fdf6e9af0f459b00bb0d8f0 [file] [log] [blame]
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +10001/*
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>
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100017#include <linux/init.h>
18#include <linux/smp.h>
19#include <linux/errno.h>
20#include <asm/ptrace.h>
21#include <asm/system.h>
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100022#include <asm/pmc.h>
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100023#include <asm/cputable.h>
24#include <asm/oprofile_impl.h>
Kelly Daly2191fe32006-06-21 13:52:55 +100025#include <asm/firmware.h>
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100026
27static struct op_powerpc_model *model;
28
29static struct op_counter_config ctr[OP_MAX_COUNTER];
30static struct op_system_config sys;
31
Bob Nelson14748552007-07-20 21:39:53 +020032static int op_per_cpu_rc;
33
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100034static void op_handle_interrupt(struct pt_regs *regs)
35{
36 model->handle_interrupt(regs, ctr);
37}
38
Andy Flemingdd6c89f2006-10-27 15:06:32 -050039static void op_powerpc_cpu_setup(void *dummy)
40{
Bob Nelson14748552007-07-20 21:39:53 +020041 int ret;
42
43 ret = model->cpu_setup(ctr);
44
45 if (ret != 0)
46 op_per_cpu_rc = ret;
Andy Flemingdd6c89f2006-10-27 15:06:32 -050047}
48
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100049static int op_powerpc_setup(void)
50{
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100051 int err;
52
Bob Nelson14748552007-07-20 21:39:53 +020053 op_per_cpu_rc = 0;
54
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100055 /* Grab the hardware */
56 err = reserve_pmc_hardware(op_handle_interrupt);
57 if (err)
58 return err;
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100059
60 /* Pre-compute the values to stuff in the hardware registers. */
Bob Nelson14748552007-07-20 21:39:53 +020061 op_per_cpu_rc = model->reg_setup(ctr, &sys, model->num_counters);
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100062
Bob Nelson14748552007-07-20 21:39:53 +020063 if (op_per_cpu_rc)
64 goto out;
65
66 /* Configure the registers on all cpus. If an error occurs on one
67 * of the cpus, op_per_cpu_rc will be set to the error */
Jens Axboe15c8b6c2008-05-09 09:39:44 +020068 on_each_cpu(op_powerpc_cpu_setup, NULL, 1);
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100069
Bob Nelson14748552007-07-20 21:39:53 +020070out: if (op_per_cpu_rc) {
71 /* error on setup release the performance counter hardware */
72 release_pmc_hardware();
73 }
74
75 return op_per_cpu_rc;
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100076}
77
78static void op_powerpc_shutdown(void)
79{
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100080 release_pmc_hardware();
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100081}
82
83static void op_powerpc_cpu_start(void *dummy)
84{
Bob Nelson14748552007-07-20 21:39:53 +020085 /* If any of the cpus have return an error, set the
86 * global flag to the error so it can be returned
87 * to the generic OProfile caller.
88 */
89 int ret;
90
91 ret = model->start(ctr);
92 if (ret != 0)
93 op_per_cpu_rc = ret;
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +100094}
95
96static int op_powerpc_start(void)
97{
Bob Nelson14748552007-07-20 21:39:53 +020098 op_per_cpu_rc = 0;
99
Maynard Johnson18f21902006-11-20 18:45:16 +0100100 if (model->global_start)
Bob Nelson14748552007-07-20 21:39:53 +0200101 return model->global_start(ctr);
102 if (model->start) {
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200103 on_each_cpu(op_powerpc_cpu_start, NULL, 1);
Bob Nelson14748552007-07-20 21:39:53 +0200104 return op_per_cpu_rc;
105 }
106 return -EIO; /* No start function is defined for this
107 power architecture */
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000108}
109
110static inline void op_powerpc_cpu_stop(void *dummy)
111{
112 model->stop();
113}
114
115static void op_powerpc_stop(void)
116{
Maynard Johnson18f21902006-11-20 18:45:16 +0100117 if (model->stop)
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200118 on_each_cpu(op_powerpc_cpu_stop, NULL, 1);
Maynard Johnson18f21902006-11-20 18:45:16 +0100119 if (model->global_stop)
120 model->global_stop();
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000121}
122
123static int op_powerpc_create_files(struct super_block *sb, struct dentry *root)
124{
125 int i;
126
Andy Fleming555d97a2005-12-15 20:02:04 -0600127#ifdef CONFIG_PPC64
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000128 /*
129 * There is one mmcr0, mmcr1 and mmcra for setting the events for
130 * all of the counters.
131 */
132 oprofilefs_create_ulong(sb, root, "mmcr0", &sys.mmcr0);
133 oprofilefs_create_ulong(sb, root, "mmcr1", &sys.mmcr1);
134 oprofilefs_create_ulong(sb, root, "mmcra", &sys.mmcra);
Andy Fleming555d97a2005-12-15 20:02:04 -0600135#endif
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000136
137 for (i = 0; i < model->num_counters; ++i) {
138 struct dentry *dir;
Markus Armbruster0c6856f2006-06-26 00:24:34 -0700139 char buf[4];
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000140
141 snprintf(buf, sizeof buf, "%d", i);
142 dir = oprofilefs_mkdir(sb, root, buf);
143
144 oprofilefs_create_ulong(sb, dir, "enabled", &ctr[i].enabled);
145 oprofilefs_create_ulong(sb, dir, "event", &ctr[i].event);
146 oprofilefs_create_ulong(sb, dir, "count", &ctr[i].count);
Andy Fleming555d97a2005-12-15 20:02:04 -0600147
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000148 /*
Andy Fleming555d97a2005-12-15 20:02:04 -0600149 * Classic PowerPC doesn't support per-counter
150 * control like this, but the options are
151 * expected, so they remain. For Freescale
152 * Book-E style performance monitors, we do
153 * support them.
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000154 */
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000155 oprofilefs_create_ulong(sb, dir, "kernel", &ctr[i].kernel);
156 oprofilefs_create_ulong(sb, dir, "user", &ctr[i].user);
157
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000158 oprofilefs_create_ulong(sb, dir, "unit_mask", &ctr[i].unit_mask);
159 }
160
161 oprofilefs_create_ulong(sb, root, "enable_kernel", &sys.enable_kernel);
162 oprofilefs_create_ulong(sb, root, "enable_user", &sys.enable_user);
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000163
164 /* Default to tracing both kernel and user */
165 sys.enable_kernel = 1;
166 sys.enable_user = 1;
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000167
168 return 0;
169}
170
171int __init oprofile_arch_init(struct oprofile_operations *ops)
172{
Anton Blanchard32a33992006-01-09 15:41:31 +1100173 if (!cur_cpu_spec->oprofile_cpu_type)
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000174 return -ENODEV;
Anton Blanchard32a33992006-01-09 15:41:31 +1100175
Kelly Daly2191fe32006-06-21 13:52:55 +1000176 if (firmware_has_feature(FW_FEATURE_ISERIES))
177 return -ENODEV;
178
Anton Blanchard32a33992006-01-09 15:41:31 +1100179 switch (cur_cpu_spec->oprofile_type) {
180#ifdef CONFIG_PPC64
Bob Nelson14748552007-07-20 21:39:53 +0200181#ifdef CONFIG_OPROFILE_CELL
Maynard Johnson18f21902006-11-20 18:45:16 +0100182 case PPC_OPROFILE_CELL:
Ishizaki Kouef66f792007-01-12 09:56:44 +0900183 if (firmware_has_feature(FW_FEATURE_LPAR))
184 return -ENODEV;
Maynard Johnson18f21902006-11-20 18:45:16 +0100185 model = &op_model_cell;
Bob Nelson14748552007-07-20 21:39:53 +0200186 ops->sync_start = model->sync_start;
187 ops->sync_stop = model->sync_stop;
Maynard Johnson18f21902006-11-20 18:45:16 +0100188 break;
189#endif
Andy Whitcroft7a45fb12006-01-13 12:35:49 +0000190 case PPC_OPROFILE_RS64:
Anton Blanchard32a33992006-01-09 15:41:31 +1100191 model = &op_model_rs64;
192 break;
Andy Whitcroft7a45fb12006-01-13 12:35:49 +0000193 case PPC_OPROFILE_POWER4:
Anton Blanchard32a33992006-01-09 15:41:31 +1100194 model = &op_model_power4;
195 break;
Olof Johansson25fc5302007-04-18 16:38:21 +1000196 case PPC_OPROFILE_PA6T:
197 model = &op_model_pa6t;
198 break;
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500199#endif
200#ifdef CONFIG_6xx
Andy Whitcroft7a45fb12006-01-13 12:35:49 +0000201 case PPC_OPROFILE_G4:
Anton Blanchard32a33992006-01-09 15:41:31 +1100202 model = &op_model_7450;
203 break;
204#endif
Andy Fleming39aef682008-02-04 18:27:55 -0600205#if defined(CONFIG_FSL_EMB_PERFMON)
206 case PPC_OPROFILE_FSL_EMB:
207 model = &op_model_fsl_emb;
Anton Blanchard32a33992006-01-09 15:41:31 +1100208 break;
209#endif
210 default:
211 return -ENODEV;
212 }
213
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000214 model->num_counters = cur_cpu_spec->num_pmcs;
215
216 ops->cpu_type = cur_cpu_spec->oprofile_cpu_type;
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000217 ops->create_files = op_powerpc_create_files;
218 ops->setup = op_powerpc_setup;
219 ops->shutdown = op_powerpc_shutdown;
220 ops->start = op_powerpc_start;
221 ops->stop = op_powerpc_stop;
Brian Rogan6c6bd752006-03-27 11:57:01 +1100222 ops->backtrace = op_powerpc_backtrace;
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000223
Olof Johansson5e1415c2006-04-12 15:29:00 -0500224 printk(KERN_DEBUG "oprofile: using %s performance monitoring.\n",
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000225 ops->cpu_type);
226
227 return 0;
228}
229
230void oprofile_arch_exit(void)
231{
Stephen Rothwell86a5cdd2005-09-19 23:24:08 +1000232}