Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/oprofile/op_model_sh7750.c |
| 3 | * |
| 4 | * OProfile support for SH7750/SH7750S Performance Counters |
| 5 | * |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 6 | * Copyright (C) 2003 - 2008 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This file is subject to the terms and conditions of the GNU General Public |
| 9 | * License. See the file "COPYING" in the main directory of this archive |
| 10 | * for more details. |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/oprofile.h> |
| 14 | #include <linux/profile.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/interrupt.h> |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 18 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/fs.h> |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 20 | #include "op_impl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define PM_CR_BASE 0xff000084 /* 16-bit */ |
| 23 | #define PM_CTR_BASE 0xff100004 /* 32-bit */ |
| 24 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 25 | #define PMCR(n) (PM_CR_BASE + ((n) * 0x04)) |
| 26 | #define PMCTRH(n) (PM_CTR_BASE + 0x00 + ((n) * 0x08)) |
| 27 | #define PMCTRL(n) (PM_CTR_BASE + 0x04 + ((n) * 0x08)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define PMCR_PMM_MASK 0x0000003f |
| 30 | |
| 31 | #define PMCR_CLKF 0x00000100 |
| 32 | #define PMCR_PMCLR 0x00002000 |
| 33 | #define PMCR_PMST 0x00004000 |
| 34 | #define PMCR_PMEN 0x00008000 |
| 35 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 36 | struct op_sh_model op_model_sh7750_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define NR_CNTRS 2 |
| 39 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 40 | static struct sh7750_ppc_register_config { |
| 41 | unsigned int ctrl; |
| 42 | unsigned long cnt_hi; |
| 43 | unsigned long cnt_lo; |
| 44 | } regcache[NR_CNTRS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * There are a number of events supported by each counter (33 in total). |
| 48 | * Since we have 2 counters, each counter will take the event code as it |
| 49 | * corresponds to the PMCR PMM setting. Each counter can be configured |
| 50 | * independently. |
| 51 | * |
| 52 | * Event Code Description |
| 53 | * ---------- ----------- |
| 54 | * |
| 55 | * 0x01 Operand read access |
| 56 | * 0x02 Operand write access |
| 57 | * 0x03 UTLB miss |
| 58 | * 0x04 Operand cache read miss |
| 59 | * 0x05 Operand cache write miss |
| 60 | * 0x06 Instruction fetch (w/ cache) |
| 61 | * 0x07 Instruction TLB miss |
| 62 | * 0x08 Instruction cache miss |
| 63 | * 0x09 All operand accesses |
| 64 | * 0x0a All instruction accesses |
| 65 | * 0x0b OC RAM operand access |
| 66 | * 0x0d On-chip I/O space access |
| 67 | * 0x0e Operand access (r/w) |
| 68 | * 0x0f Operand cache miss (r/w) |
| 69 | * 0x10 Branch instruction |
| 70 | * 0x11 Branch taken |
| 71 | * 0x12 BSR/BSRF/JSR |
| 72 | * 0x13 Instruction execution |
| 73 | * 0x14 Instruction execution in parallel |
| 74 | * 0x15 FPU Instruction execution |
| 75 | * 0x16 Interrupt |
| 76 | * 0x17 NMI |
| 77 | * 0x18 trapa instruction execution |
| 78 | * 0x19 UBCA match |
| 79 | * 0x1a UBCB match |
| 80 | * 0x21 Instruction cache fill |
| 81 | * 0x22 Operand cache fill |
| 82 | * 0x23 Elapsed time |
| 83 | * 0x24 Pipeline freeze by I-cache miss |
| 84 | * 0x25 Pipeline freeze by D-cache miss |
| 85 | * 0x27 Pipeline freeze by branch instruction |
| 86 | * 0x28 Pipeline freeze by CPU register |
| 87 | * 0x29 Pipeline freeze by FPU |
| 88 | * |
| 89 | * Unfortunately we don't have a native exception or interrupt for counter |
| 90 | * overflow (although since these counters can run for 16.3 days without |
| 91 | * overflowing, it's not really necessary). |
| 92 | * |
| 93 | * OProfile on the other hand likes to have samples taken periodically, so |
| 94 | * for now we just piggyback the timer interrupt to get the expected |
| 95 | * behavior. |
| 96 | */ |
| 97 | |
Paul Mundt | b46378b | 2007-10-30 18:24:17 +0900 | [diff] [blame] | 98 | static int sh7750_timer_notify(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Paul Mundt | b46378b | 2007-10-30 18:24:17 +0900 | [diff] [blame] | 100 | oprofile_add_sample(regs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static u64 sh7750_read_counter(int counter) |
| 105 | { |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 106 | return (u64)((u64)(__raw_readl(PMCTRH(counter)) & 0xffff) << 32) | |
| 107 | __raw_readl(PMCTRL(counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Files will be in a path like: |
| 112 | * |
| 113 | * /<oprofilefs mount point>/<counter number>/<file> |
| 114 | * |
| 115 | * So when dealing with <file>, we look to the parent dentry for the counter |
| 116 | * number. |
| 117 | */ |
| 118 | static inline int to_counter(struct file *file) |
| 119 | { |
Josef Sipek | c943c4b | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 120 | const unsigned char *name = file->f_path.dentry->d_parent->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | return (int)simple_strtol(name, NULL, 10); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * XXX: We have 48-bit counters, so we're probably going to want something |
| 127 | * more along the lines of oprofilefs_ullong_to_user().. Truncating to |
| 128 | * unsigned long works fine for now though, as long as we don't attempt to |
| 129 | * profile for too horribly long. |
| 130 | */ |
| 131 | static ssize_t sh7750_read_count(struct file *file, char __user *buf, |
| 132 | size_t count, loff_t *ppos) |
| 133 | { |
| 134 | int counter = to_counter(file); |
| 135 | u64 val = sh7750_read_counter(counter); |
| 136 | |
| 137 | return oprofilefs_ulong_to_user((unsigned long)val, buf, count, ppos); |
| 138 | } |
| 139 | |
| 140 | static ssize_t sh7750_write_count(struct file *file, const char __user *buf, |
| 141 | size_t count, loff_t *ppos) |
| 142 | { |
| 143 | int counter = to_counter(file); |
| 144 | unsigned long val; |
| 145 | |
| 146 | if (oprofilefs_ulong_from_user(&val, buf, count)) |
| 147 | return -EFAULT; |
| 148 | |
| 149 | /* |
| 150 | * Any write will clear the counter, although only 0 should be |
| 151 | * written for this purpose, as we do not support setting the |
| 152 | * counter to an arbitrary value. |
| 153 | */ |
| 154 | WARN_ON(val != 0); |
| 155 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 156 | __raw_writew(__raw_readw(PMCR(counter)) | PMCR_PMCLR, PMCR(counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | return count; |
| 159 | } |
| 160 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 161 | static const struct file_operations count_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | .read = sh7750_read_count, |
| 163 | .write = sh7750_write_count, |
| 164 | }; |
| 165 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 166 | static int sh7750_ppc_create_files(struct super_block *sb, struct dentry *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 168 | return oprofilefs_create_file(sb, dir, "count", &count_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 171 | static void sh7750_ppc_reg_setup(struct op_counter_config *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 173 | unsigned int counters = op_model_sh7750_ops.num_counters; |
| 174 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 176 | for (i = 0; i < counters; i++) { |
| 177 | regcache[i].ctrl = 0; |
| 178 | regcache[i].cnt_hi = 0; |
| 179 | regcache[i].cnt_lo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 181 | if (!ctr[i].enabled) |
| 182 | continue; |
| 183 | |
| 184 | regcache[i].ctrl |= ctr[i].event | PMCR_PMEN | PMCR_PMST; |
| 185 | regcache[i].cnt_hi = (unsigned long)((ctr->count >> 32) & 0xffff); |
| 186 | regcache[i].cnt_lo = (unsigned long)(ctr->count & 0xffffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 190 | static void sh7750_ppc_cpu_setup(void *args) |
| 191 | { |
| 192 | unsigned int counters = op_model_sh7750_ops.num_counters; |
| 193 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 195 | for (i = 0; i < counters; i++) { |
| 196 | __raw_writew(0, PMCR(i)); |
| 197 | __raw_writel(regcache[i].cnt_hi, PMCTRH(i)); |
| 198 | __raw_writel(regcache[i].cnt_lo, PMCTRL(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void sh7750_ppc_cpu_start(void *args) |
| 203 | { |
| 204 | unsigned int counters = op_model_sh7750_ops.num_counters; |
| 205 | int i; |
| 206 | |
| 207 | for (i = 0; i < counters; i++) |
| 208 | __raw_writew(regcache[i].ctrl, PMCR(i)); |
| 209 | } |
| 210 | |
| 211 | static void sh7750_ppc_cpu_stop(void *args) |
| 212 | { |
| 213 | unsigned int counters = op_model_sh7750_ops.num_counters; |
| 214 | int i; |
| 215 | |
| 216 | /* Disable the counters */ |
| 217 | for (i = 0; i < counters; i++) |
| 218 | __raw_writew(__raw_readw(PMCR(i)) & ~PMCR_PMEN, PMCR(i)); |
| 219 | } |
| 220 | |
| 221 | static inline void sh7750_ppc_reset(void) |
| 222 | { |
| 223 | unsigned int counters = op_model_sh7750_ops.num_counters; |
| 224 | int i; |
| 225 | |
| 226 | /* Clear the counters */ |
| 227 | for (i = 0; i < counters; i++) |
| 228 | __raw_writew(__raw_readw(PMCR(i)) | PMCR_PMCLR, PMCR(i)); |
| 229 | } |
| 230 | |
| 231 | static int sh7750_ppc_init(void) |
| 232 | { |
| 233 | sh7750_ppc_reset(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Paul Mundt | b46378b | 2007-10-30 18:24:17 +0900 | [diff] [blame] | 235 | return register_timer_hook(sh7750_timer_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 238 | static void sh7750_ppc_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Paul Mundt | b46378b | 2007-10-30 18:24:17 +0900 | [diff] [blame] | 240 | unregister_timer_hook(sh7750_timer_notify); |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 241 | |
| 242 | sh7750_ppc_reset(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Paul Mundt | d9341b5 | 2008-12-16 09:36:25 +0900 | [diff] [blame] | 245 | struct op_sh_model op_model_sh7750_ops = { |
| 246 | .cpu_type = "sh/sh7750", |
| 247 | .num_counters = NR_CNTRS, |
| 248 | .reg_setup = sh7750_ppc_reg_setup, |
| 249 | .cpu_setup = sh7750_ppc_cpu_setup, |
| 250 | .cpu_start = sh7750_ppc_cpu_start, |
| 251 | .cpu_stop = sh7750_ppc_cpu_stop, |
| 252 | .init = sh7750_ppc_init, |
| 253 | .exit = sh7750_ppc_exit, |
| 254 | .create_files = sh7750_ppc_create_files, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | }; |