Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 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> |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/smp.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <asm/ptrace.h> |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 21 | #include <asm/pmc.h> |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 22 | #include <asm/cputable.h> |
| 23 | #include <asm/oprofile_impl.h> |
Kelly Daly | 2191fe3 | 2006-06-21 13:52:55 +1000 | [diff] [blame] | 24 | #include <asm/firmware.h> |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 25 | |
| 26 | static struct op_powerpc_model *model; |
| 27 | |
| 28 | static struct op_counter_config ctr[OP_MAX_COUNTER]; |
| 29 | static struct op_system_config sys; |
| 30 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 31 | static int op_per_cpu_rc; |
| 32 | |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 33 | static void op_handle_interrupt(struct pt_regs *regs) |
| 34 | { |
| 35 | model->handle_interrupt(regs, ctr); |
| 36 | } |
| 37 | |
Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 38 | static void op_powerpc_cpu_setup(void *dummy) |
| 39 | { |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 40 | int ret; |
| 41 | |
| 42 | ret = model->cpu_setup(ctr); |
| 43 | |
| 44 | if (ret != 0) |
| 45 | op_per_cpu_rc = ret; |
Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 48 | static int op_powerpc_setup(void) |
| 49 | { |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 50 | int err; |
| 51 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 52 | op_per_cpu_rc = 0; |
| 53 | |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 54 | /* Grab the hardware */ |
| 55 | err = reserve_pmc_hardware(op_handle_interrupt); |
| 56 | if (err) |
| 57 | return err; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 58 | |
| 59 | /* Pre-compute the values to stuff in the hardware registers. */ |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 60 | op_per_cpu_rc = model->reg_setup(ctr, &sys, model->num_counters); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 61 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 62 | if (op_per_cpu_rc) |
| 63 | goto out; |
| 64 | |
| 65 | /* Configure the registers on all cpus. If an error occurs on one |
| 66 | * of the cpus, op_per_cpu_rc will be set to the error */ |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 67 | on_each_cpu(op_powerpc_cpu_setup, NULL, 1); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 68 | |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 69 | out: if (op_per_cpu_rc) { |
| 70 | /* error on setup release the performance counter hardware */ |
| 71 | release_pmc_hardware(); |
| 72 | } |
| 73 | |
| 74 | return op_per_cpu_rc; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void op_powerpc_shutdown(void) |
| 78 | { |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 79 | release_pmc_hardware(); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void op_powerpc_cpu_start(void *dummy) |
| 83 | { |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 84 | /* If any of the cpus have return an error, set the |
| 85 | * global flag to the error so it can be returned |
| 86 | * to the generic OProfile caller. |
| 87 | */ |
| 88 | int ret; |
| 89 | |
| 90 | ret = model->start(ctr); |
| 91 | if (ret != 0) |
| 92 | op_per_cpu_rc = ret; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int op_powerpc_start(void) |
| 96 | { |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 97 | op_per_cpu_rc = 0; |
| 98 | |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 99 | if (model->global_start) |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 100 | return model->global_start(ctr); |
| 101 | if (model->start) { |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 102 | on_each_cpu(op_powerpc_cpu_start, NULL, 1); |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 103 | return op_per_cpu_rc; |
| 104 | } |
| 105 | return -EIO; /* No start function is defined for this |
| 106 | power architecture */ |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline void op_powerpc_cpu_stop(void *dummy) |
| 110 | { |
| 111 | model->stop(); |
| 112 | } |
| 113 | |
| 114 | static void op_powerpc_stop(void) |
| 115 | { |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 116 | if (model->stop) |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 117 | on_each_cpu(op_powerpc_cpu_stop, NULL, 1); |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 118 | if (model->global_stop) |
| 119 | model->global_stop(); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 120 | } |
| 121 | |
Al Viro | ef7bca1 | 2013-07-19 15:52:42 +0400 | [diff] [blame] | 122 | static int op_powerpc_create_files(struct dentry *root) |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 123 | { |
| 124 | int i; |
| 125 | |
Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 126 | #ifdef CONFIG_PPC64 |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 127 | /* |
| 128 | * There is one mmcr0, mmcr1 and mmcra for setting the events for |
| 129 | * all of the counters. |
| 130 | */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 131 | oprofilefs_create_ulong(root, "mmcr0", &sys.mmcr0); |
| 132 | oprofilefs_create_ulong(root, "mmcr1", &sys.mmcr1); |
| 133 | oprofilefs_create_ulong(root, "mmcra", &sys.mmcra); |
Carl Love | 8838232 | 2008-12-01 16:18:36 -0800 | [diff] [blame] | 134 | #ifdef CONFIG_OPROFILE_CELL |
| 135 | /* create a file the user tool can check to see what level of profiling |
| 136 | * support exits with this kernel. Initialize bit mask to indicate |
| 137 | * what support the kernel has: |
| 138 | * bit 0 - Supports SPU event profiling in addition to PPU |
| 139 | * event and cycles; and SPU cycle profiling |
| 140 | * bits 1-31 - Currently unused. |
| 141 | * |
| 142 | * If the file does not exist, then the kernel only supports SPU |
| 143 | * cycle profiling, PPU event and cycle profiling. |
| 144 | */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 145 | oprofilefs_create_ulong(root, "cell_support", &sys.cell_support); |
Carl Love | 8838232 | 2008-12-01 16:18:36 -0800 | [diff] [blame] | 146 | sys.cell_support = 0x1; /* Note, the user OProfile tool must check |
| 147 | * that this bit is set before attempting to |
| 148 | * user SPU event profiling. Older kernels |
| 149 | * will not have this file, hence the user |
| 150 | * tool is not allowed to do SPU event |
| 151 | * profiling on older kernels. Older kernels |
| 152 | * will accept SPU events but collected data |
| 153 | * is garbage. |
| 154 | */ |
| 155 | #endif |
Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 156 | #endif |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 157 | |
| 158 | for (i = 0; i < model->num_counters; ++i) { |
| 159 | struct dentry *dir; |
Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 160 | char buf[4]; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 161 | |
| 162 | snprintf(buf, sizeof buf, "%d", i); |
Al Viro | ecde282 | 2013-07-19 15:58:27 +0400 | [diff] [blame] | 163 | dir = oprofilefs_mkdir(root, buf); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 164 | |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 165 | oprofilefs_create_ulong(dir, "enabled", &ctr[i].enabled); |
| 166 | oprofilefs_create_ulong(dir, "event", &ctr[i].event); |
| 167 | oprofilefs_create_ulong(dir, "count", &ctr[i].count); |
Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 168 | |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 169 | /* |
Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 170 | * Classic PowerPC doesn't support per-counter |
| 171 | * control like this, but the options are |
| 172 | * expected, so they remain. For Freescale |
| 173 | * Book-E style performance monitors, we do |
| 174 | * support them. |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 175 | */ |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 176 | oprofilefs_create_ulong(dir, "kernel", &ctr[i].kernel); |
| 177 | oprofilefs_create_ulong(dir, "user", &ctr[i].user); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 178 | |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 179 | oprofilefs_create_ulong(dir, "unit_mask", &ctr[i].unit_mask); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 182 | oprofilefs_create_ulong(root, "enable_kernel", &sys.enable_kernel); |
| 183 | oprofilefs_create_ulong(root, "enable_user", &sys.enable_user); |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 184 | |
| 185 | /* Default to tracing both kernel and user */ |
| 186 | sys.enable_kernel = 1; |
| 187 | sys.enable_user = 1; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
| 193 | { |
Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 194 | if (!cur_cpu_spec->oprofile_cpu_type) |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 195 | return -ENODEV; |
Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 196 | |
| 197 | switch (cur_cpu_spec->oprofile_type) { |
Benjamin Herrenschmidt | 1caca37 | 2010-07-09 15:18:44 +1000 | [diff] [blame] | 198 | #ifdef CONFIG_PPC_BOOK3S_64 |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_OPROFILE_CELL |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 200 | case PPC_OPROFILE_CELL: |
Ishizaki Kou | ef66f79 | 2007-01-12 09:56:44 +0900 | [diff] [blame] | 201 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
| 202 | return -ENODEV; |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 203 | model = &op_model_cell; |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 204 | ops->sync_start = model->sync_start; |
| 205 | ops->sync_stop = model->sync_stop; |
Maynard Johnson | 18f2190 | 2006-11-20 18:45:16 +0100 | [diff] [blame] | 206 | break; |
| 207 | #endif |
Andy Whitcroft | 7a45fb1 | 2006-01-13 12:35:49 +0000 | [diff] [blame] | 208 | case PPC_OPROFILE_POWER4: |
Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 209 | model = &op_model_power4; |
| 210 | break; |
Olof Johansson | 25fc530 | 2007-04-18 16:38:21 +1000 | [diff] [blame] | 211 | case PPC_OPROFILE_PA6T: |
| 212 | model = &op_model_pa6t; |
| 213 | break; |
Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 214 | #endif |
| 215 | #ifdef CONFIG_6xx |
Andy Whitcroft | 7a45fb1 | 2006-01-13 12:35:49 +0000 | [diff] [blame] | 216 | case PPC_OPROFILE_G4: |
Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 217 | model = &op_model_7450; |
| 218 | break; |
| 219 | #endif |
Andy Fleming | 39aef68 | 2008-02-04 18:27:55 -0600 | [diff] [blame] | 220 | #if defined(CONFIG_FSL_EMB_PERFMON) |
| 221 | case PPC_OPROFILE_FSL_EMB: |
| 222 | model = &op_model_fsl_emb; |
Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 223 | break; |
| 224 | #endif |
| 225 | default: |
| 226 | return -ENODEV; |
| 227 | } |
| 228 | |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 229 | model->num_counters = cur_cpu_spec->num_pmcs; |
| 230 | |
| 231 | ops->cpu_type = cur_cpu_spec->oprofile_cpu_type; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 232 | ops->create_files = op_powerpc_create_files; |
| 233 | ops->setup = op_powerpc_setup; |
| 234 | ops->shutdown = op_powerpc_shutdown; |
| 235 | ops->start = op_powerpc_start; |
| 236 | ops->stop = op_powerpc_stop; |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 237 | ops->backtrace = op_powerpc_backtrace; |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 238 | |
Olof Johansson | 5e1415c | 2006-04-12 15:29:00 -0500 | [diff] [blame] | 239 | printk(KERN_DEBUG "oprofile: using %s performance monitoring.\n", |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 240 | ops->cpu_type); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | void oprofile_arch_exit(void) |
| 246 | { |
Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 247 | } |