blob: 15ea778b5e6664c0f305681b3c9a0c3c2fad3f2e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Ralf Baechlebb9b8132007-03-09 15:59:56 +000015 *
16 * Copyright (C) 2001, 2002, 2003 Broadcom Corporation
17 * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
18 * Copyright (C) 2007 MIPS Technologies, Inc.
19 * written by Ralf Baechle <ralf@linux-mips.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Ralf Baechlebb9b8132007-03-09 15:59:56 +000022#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ralf Baechlebb9b8132007-03-09 15:59:56 +000024#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/types.h>
28#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/slab.h>
Jonathan Corbet7558da92008-05-15 09:10:50 -060031#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/vmalloc.h>
33#include <linux/fs.h>
34#include <linux/errno.h>
Ralf Baechledb89a482005-02-03 13:37:41 +000035#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/io.h>
37#include <asm/sibyte/sb1250.h>
Mark Masond619f382007-03-29 11:39:56 -070038
39#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
40#include <asm/sibyte/bcm1480_regs.h>
41#include <asm/sibyte/bcm1480_scd.h>
42#include <asm/sibyte/bcm1480_int.h>
43#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/sibyte/sb1250_regs.h>
45#include <asm/sibyte/sb1250_scd.h>
46#include <asm/sibyte/sb1250_int.h>
Mark Masond619f382007-03-29 11:39:56 -070047#else
48#error invalid SiByte UART configuation
49#endif
50
51#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
52#undef K_INT_TRACE_FREEZE
53#define K_INT_TRACE_FREEZE K_BCM1480_INT_TRACE_FREEZE
54#undef K_INT_PERF_CNT
55#define K_INT_PERF_CNT K_BCM1480_INT_PERF_CNT
56#endif
57
Ralf Baechlebb9b8132007-03-09 15:59:56 +000058#include <asm/system.h>
59#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ralf Baechlebb9b8132007-03-09 15:59:56 +000061#define SBPROF_TB_MAJOR 240
62
63typedef u64 tb_sample_t[6*256];
64
65enum open_status {
66 SB_CLOSED,
67 SB_OPENING,
68 SB_OPEN
69};
70
71struct sbprof_tb {
72 wait_queue_head_t tb_sync;
73 wait_queue_head_t tb_read;
74 struct mutex lock;
75 enum open_status open;
76 tb_sample_t *sbprof_tbbuf;
77 int next_tb_sample;
78
79 volatile int tb_enable;
80 volatile int tb_armed;
81
82};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static struct sbprof_tb sbp;
85
Ralf Baechlebb9b8132007-03-09 15:59:56 +000086#define MAX_SAMPLE_BYTES (24*1024*1024)
87#define MAX_TBSAMPLE_BYTES (12*1024*1024)
88
89#define MAX_SAMPLES (MAX_SAMPLE_BYTES/sizeof(u_int32_t))
90#define TB_SAMPLE_SIZE (sizeof(tb_sample_t))
91#define MAX_TB_SAMPLES (MAX_TBSAMPLE_BYTES/TB_SAMPLE_SIZE)
92
93/* ioctls */
94#define SBPROF_ZBSTART _IOW('s', 0, int)
95#define SBPROF_ZBSTOP _IOW('s', 1, int)
96#define SBPROF_ZBWAITFULL _IOW('s', 2, int)
97
98/*
99 * Routines for using 40-bit SCD cycle counter
100 *
101 * Client responsible for either handling interrupts or making sure
102 * the cycles counter never saturates, e.g., by doing
103 * zclk_timer_init(0) at least every 2^40 - 1 ZCLKs.
104 */
105
106/*
107 * Configures SCD counter 0 to count ZCLKs starting from val;
108 * Configures SCD counters1,2,3 to count nothing.
109 * Must not be called while gathering ZBbus profiles.
110 */
111
112#define zclk_timer_init(val) \
113 __asm__ __volatile__ (".set push;" \
114 ".set mips64;" \
115 "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
116 "sd %0, 0x10($8);" /* write val to counter0 */ \
117 "sd %1, 0($8);" /* config counter0 for zclks*/ \
118 ".set pop" \
119 : /* no outputs */ \
120 /* enable, counter0 */ \
121 : /* inputs */ "r"(val), "r" ((1ULL << 33) | 1ULL) \
122 : /* modifies */ "$8" )
123
124
125/* Reads SCD counter 0 and puts result in value
126 unsigned long long val; */
127#define zclk_get(val) \
128 __asm__ __volatile__ (".set push;" \
129 ".set mips64;" \
130 "la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \
131 "ld %0, 0x10($8);" /* write val to counter0 */ \
132 ".set pop" \
133 : /* outputs */ "=r"(val) \
134 : /* inputs */ \
135 : /* modifies */ "$8" )
136
Mark Masond619f382007-03-29 11:39:56 -0700137#define DEVNAME "sb_tbprof"
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define TB_FULL (sbp.next_tb_sample == MAX_TB_SAMPLES)
140
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * Support for ZBbus sampling using the trace buffer
143 *
144 * We use the SCD performance counter interrupt, caused by a Zclk counter
145 * overflow, to trigger the start of tracing.
146 *
147 * We set the trace buffer to sample everything and freeze on
148 * overflow.
149 *
150 * We map the interrupt for trace_buffer_freeze to handle it on CPU 0.
Mark Masond619f382007-03-29 11:39:56 -0700151 *
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000154static u64 tb_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156static void arm_tb(void)
157{
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000158 u64 scdperfcnt;
159 u64 next = (1ULL << 40) - tb_period;
160 u64 tb_options = M_SCD_TRACE_CFG_FREEZE_FULL;
161
162 /*
Mark Masond619f382007-03-29 11:39:56 -0700163 * Generate an SCD_PERFCNT interrupt in TB_PERIOD Zclks to
164 * trigger start of trace. XXX vary sampling period
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000165 */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000166 __raw_writeq(0, IOADDR(A_SCD_PERF_CNT_1));
167 scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000168
169 /*
Mark Masond619f382007-03-29 11:39:56 -0700170 * Unfortunately, in Pass 2 we must clear all counters to knock down
171 * a previous interrupt request. This means that bus profiling
172 * requires ALL of the SCD perf counters.
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000173 */
Mark Masond619f382007-03-29 11:39:56 -0700174#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
175 __raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |
176 /* keep counters 0,2,3,4,5,6,7 as is */
177 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
178 IOADDR(A_BCM1480_SCD_PERF_CNT_CFG0));
179 __raw_writeq(
180 M_SPC_CFG_ENABLE | /* enable counting */
181 M_SPC_CFG_CLEAR | /* clear all counters */
182 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
183 IOADDR(A_BCM1480_SCD_PERF_CNT_CFG1));
184#else
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000185 __raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000186 /* keep counters 0,2,3 as is */
187 M_SPC_CFG_ENABLE | /* enable counting */
188 M_SPC_CFG_CLEAR | /* clear all counters */
189 V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000190 IOADDR(A_SCD_PERF_CNT_CFG));
Mark Masond619f382007-03-29 11:39:56 -0700191#endif
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000192 __raw_writeq(next, IOADDR(A_SCD_PERF_CNT_1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Reset the trace buffer */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000194 __raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#if 0 && defined(M_SCD_TRACE_CFG_FORCECNT)
196 /* XXXKW may want to expose control to the data-collector */
197 tb_options |= M_SCD_TRACE_CFG_FORCECNT;
198#endif
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000199 __raw_writeq(tb_options, IOADDR(A_SCD_TRACE_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sbp.tb_armed = 1;
201}
202
Ralf Baechle36d98e72006-10-15 09:19:58 +0100203static irqreturn_t sbprof_tb_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int i;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000206
207 pr_debug(DEVNAME ": tb_intr\n");
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (sbp.next_tb_sample < MAX_TB_SAMPLES) {
210 /* XXX should use XKPHYS to make writes bypass L2 */
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000211 u64 *p = sbp.sbprof_tbbuf[sbp.next_tb_sample++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Read out trace */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000213 __raw_writeq(M_SCD_TRACE_CFG_START_READ,
214 IOADDR(A_SCD_TRACE_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 __asm__ __volatile__ ("sync" : : : "memory");
216 /* Loop runs backwards because bundles are read out in reverse order */
217 for (i = 256 * 6; i > 0; i -= 6) {
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000218 /* Subscripts decrease to put bundle in the order */
219 /* t0 lo, t0 hi, t1 lo, t1 hi, t2 lo, t2 hi */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000220 p[i - 1] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
Mark Masond619f382007-03-29 11:39:56 -0700221 /* read t2 hi */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000222 p[i - 2] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
Mark Masond619f382007-03-29 11:39:56 -0700223 /* read t2 lo */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000224 p[i - 3] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
Mark Masond619f382007-03-29 11:39:56 -0700225 /* read t1 hi */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000226 p[i - 4] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
Mark Masond619f382007-03-29 11:39:56 -0700227 /* read t1 lo */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000228 p[i - 5] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
Mark Masond619f382007-03-29 11:39:56 -0700229 /* read t0 hi */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000230 p[i - 6] = __raw_readq(IOADDR(A_SCD_TRACE_READ));
Mark Masond619f382007-03-29 11:39:56 -0700231 /* read t0 lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 if (!sbp.tb_enable) {
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000234 pr_debug(DEVNAME ": tb_intr shutdown\n");
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000235 __raw_writeq(M_SCD_TRACE_CFG_RESET,
236 IOADDR(A_SCD_TRACE_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sbp.tb_armed = 0;
Mark Masond619f382007-03-29 11:39:56 -0700238 wake_up_interruptible(&sbp.tb_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 } else {
Mark Masond619f382007-03-29 11:39:56 -0700240 /* knock down current interrupt and get another one later */
241 arm_tb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 } else {
244 /* No more trace buffer samples */
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000245 pr_debug(DEVNAME ": tb_intr full\n");
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000246 __raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 sbp.tb_armed = 0;
Mark Masond619f382007-03-29 11:39:56 -0700248 if (!sbp.tb_enable)
249 wake_up_interruptible(&sbp.tb_sync);
250 wake_up_interruptible(&sbp.tb_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return IRQ_HANDLED;
253}
254
Ralf Baechle36d98e72006-10-15 09:19:58 +0100255static irqreturn_t sbprof_pc_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 printk(DEVNAME ": unexpected pc_intr");
258 return IRQ_NONE;
259}
260
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000261/*
262 * Requires: Already called zclk_timer_init with a value that won't
263 * saturate 40 bits. No subsequent use of SCD performance counters
264 * or trace buffer.
265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000267static int sbprof_zbprof_start(struct file *filp)
268{
269 u64 scdperfcnt;
270 int err;
271
272 if (xchg(&sbp.tb_enable, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EBUSY;
274
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000275 pr_debug(DEVNAME ": starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 sbp.next_tb_sample = 0;
278 filp->f_pos = 0;
279
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100280 err = request_irq(K_INT_TRACE_FREEZE, sbprof_tb_intr, 0,
281 DEVNAME " trace freeze", &sbp);
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000282 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return -EBUSY;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Make sure there isn't a perf-cnt interrupt waiting */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000286 scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* Disable and clear counters, override SRC_1 */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000288 __raw_writeq((scdperfcnt & ~(M_SPC_CFG_SRC1 | M_SPC_CFG_ENABLE)) |
289 M_SPC_CFG_ENABLE | M_SPC_CFG_CLEAR | V_SPC_CFG_SRC1(1),
290 IOADDR(A_SCD_PERF_CNT_CFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000292 /*
Mark Masond619f382007-03-29 11:39:56 -0700293 * We grab this interrupt to prevent others from trying to use
294 * it, even though we don't want to service the interrupts
295 * (they only feed into the trace-on-interrupt mechanism)
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000296 */
Mark Masond619f382007-03-29 11:39:56 -0700297 if (request_irq(K_INT_PERF_CNT, sbprof_pc_intr, 0, DEVNAME " scd perfcnt", &sbp)) {
298 free_irq(K_INT_TRACE_FREEZE, &sbp);
299 return -EBUSY;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000302 /*
Mark Masond619f382007-03-29 11:39:56 -0700303 * I need the core to mask these, but the interrupt mapper to
304 * pass them through. I am exploiting my knowledge that
305 * cp0_status masks out IP[5]. krw
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000306 */
Mark Masond619f382007-03-29 11:39:56 -0700307#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
308 __raw_writeq(K_BCM1480_INT_MAP_I3,
309 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_MAP_BASE_L) +
310 ((K_BCM1480_INT_PERF_CNT & 0x3f) << 3)));
311#else
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000312 __raw_writeq(K_INT_MAP_I3,
313 IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +
314 (K_INT_PERF_CNT << 3)));
Mark Masond619f382007-03-29 11:39:56 -0700315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Initialize address traps */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000318 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_0));
319 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_1));
320 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_2));
321 __raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000323 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_0));
324 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_1));
325 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_2));
326 __raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000328 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_0));
329 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_1));
330 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_2));
331 __raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Initialize Trace Event 0-7 */
Mark Masond619f382007-03-29 11:39:56 -0700334 /* when interrupt */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000335 __raw_writeq(M_SCD_TREVT_INTERRUPT, IOADDR(A_SCD_TRACE_EVENT_0));
336 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_1));
337 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_2));
338 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_3));
339 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_4));
340 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_5));
341 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_6));
342 __raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Initialize Trace Sequence 0-7 */
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000345 /* Start on event 0 (interrupt) */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000346 __raw_writeq(V_SCD_TRSEQ_FUNC_START | 0x0fff,
347 IOADDR(A_SCD_TRACE_SEQUENCE_0));
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000348 /* dsamp when d used | asamp when a used */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000349 __raw_writeq(M_SCD_TRSEQ_ASAMPLE | M_SCD_TRSEQ_DSAMPLE |
350 K_SCD_TRSEQ_TRIGGER_ALL,
351 IOADDR(A_SCD_TRACE_SEQUENCE_1));
352 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_2));
353 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_3));
354 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_4));
355 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_5));
356 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_6));
357 __raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* Now indicate the PERF_CNT interrupt as a trace-relevant interrupt */
Mark Masond619f382007-03-29 11:39:56 -0700360#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
361 __raw_writeq(1ULL << (K_BCM1480_INT_PERF_CNT & 0x3f),
362 IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_TRACE_L)));
363#else
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000364 __raw_writeq(1ULL << K_INT_PERF_CNT,
365 IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_TRACE)));
Mark Masond619f382007-03-29 11:39:56 -0700366#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 arm_tb();
368
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000369 pr_debug(DEVNAME ": done starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return 0;
372}
373
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000374static int sbprof_zbprof_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Mark Masond619f382007-03-29 11:39:56 -0700376 int err = 0;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000377
378 pr_debug(DEVNAME ": stopping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (sbp.tb_enable) {
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000381 /*
382 * XXXKW there is a window here where the intr handler may run,
383 * see the disable, and do the wake_up before this sleep
384 * happens.
385 */
386 pr_debug(DEVNAME ": wait for disarm\n");
387 err = wait_event_interruptible(sbp.tb_sync, !sbp.tb_armed);
388 pr_debug(DEVNAME ": disarm complete, stat %d\n", err);
389
390 if (err)
391 return err;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sbp.tb_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 free_irq(K_INT_TRACE_FREEZE, &sbp);
395 free_irq(K_INT_PERF_CNT, &sbp);
396 }
397
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000398 pr_debug(DEVNAME ": done stopping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Mark Masond619f382007-03-29 11:39:56 -0700400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403static int sbprof_tb_open(struct inode *inode, struct file *filp)
404{
405 int minor;
406
407 minor = iminor(inode);
Ralf Baechle36ac8292009-09-28 16:57:54 +0100408 if (minor != 0)
409 return -ENODEV;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000410
Ralf Baechle36ac8292009-09-28 16:57:54 +0100411 if (xchg(&sbp.open, SB_OPENING) != SB_CLOSED)
412 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 memset(&sbp, 0, sizeof(struct sbprof_tb));
415 sbp.sbprof_tbbuf = vmalloc(MAX_TBSAMPLE_BYTES);
Jonathan Corbet7558da92008-05-15 09:10:50 -0600416 if (!sbp.sbprof_tbbuf) {
Ralf Baechle36ac8292009-09-28 16:57:54 +0100417 sbp.open = SB_CLOSED;
418 wmb();
419 return -ENOMEM;
Jonathan Corbet7558da92008-05-15 09:10:50 -0600420 }
Ralf Baechle36ac8292009-09-28 16:57:54 +0100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 memset(sbp.sbprof_tbbuf, 0, MAX_TBSAMPLE_BYTES);
423 init_waitqueue_head(&sbp.tb_sync);
424 init_waitqueue_head(&sbp.tb_read);
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000425 mutex_init(&sbp.lock);
426
427 sbp.open = SB_OPEN;
Ralf Baechle36ac8292009-09-28 16:57:54 +0100428 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Ralf Baechle36ac8292009-09-28 16:57:54 +0100430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static int sbprof_tb_release(struct inode *inode, struct file *filp)
434{
Mark Masond619f382007-03-29 11:39:56 -0700435 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Mark Masond619f382007-03-29 11:39:56 -0700437 minor = iminor(inode);
Ralf Baechle36ac8292009-09-28 16:57:54 +0100438 if (minor != 0 || sbp.open != SB_CLOSED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000441 mutex_lock(&sbp.lock);
442
443 if (sbp.tb_armed || sbp.tb_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 sbprof_zbprof_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 vfree(sbp.sbprof_tbbuf);
Ralf Baechle36ac8292009-09-28 16:57:54 +0100447 sbp.open = SB_CLOSED;
448 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000450 mutex_unlock(&sbp.lock);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453}
454
455static ssize_t sbprof_tb_read(struct file *filp, char *buf,
456 size_t size, loff_t *offp)
457{
458 int cur_sample, sample_off, cur_count, sample_left;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000459 char *src;
Mark Masond619f382007-03-29 11:39:56 -0700460 int count = 0;
461 char *dest = buf;
462 long cur_off = *offp;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000463
464 if (!access_ok(VERIFY_WRITE, buf, size))
465 return -EFAULT;
466
467 mutex_lock(&sbp.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 count = 0;
470 cur_sample = cur_off / TB_SAMPLE_SIZE;
471 sample_off = cur_off % TB_SAMPLE_SIZE;
472 sample_left = TB_SAMPLE_SIZE - sample_off;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 while (size && (cur_sample < sbp.next_tb_sample)) {
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000475 int err;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 cur_count = size < sample_left ? size : sample_left;
478 src = (char *)(((long)sbp.sbprof_tbbuf[cur_sample])+sample_off);
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000479 err = __copy_to_user(dest, src, cur_count);
480 if (err) {
481 *offp = cur_off + cur_count - err;
482 mutex_unlock(&sbp.lock);
483 return err;
484 }
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000485 pr_debug(DEVNAME ": read from sample %d, %d bytes\n",
486 cur_sample, cur_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 size -= cur_count;
488 sample_left -= cur_count;
489 if (!sample_left) {
490 cur_sample++;
491 sample_off = 0;
492 sample_left = TB_SAMPLE_SIZE;
493 } else {
494 sample_off += cur_count;
495 }
496 cur_off += cur_count;
497 dest += cur_count;
498 count += cur_count;
499 }
500 *offp = cur_off;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000501 mutex_unlock(&sbp.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 return count;
504}
505
Mark Masond619f382007-03-29 11:39:56 -0700506static long sbprof_tb_ioctl(struct file *filp,
507 unsigned int command,
508 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Mark Masond619f382007-03-29 11:39:56 -0700510 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 switch (command) {
513 case SBPROF_ZBSTART:
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000514 mutex_lock(&sbp.lock);
Mark Masond619f382007-03-29 11:39:56 -0700515 err = sbprof_zbprof_start(filp);
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000516 mutex_unlock(&sbp.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 case SBPROF_ZBSTOP:
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000520 mutex_lock(&sbp.lock);
Mark Masond619f382007-03-29 11:39:56 -0700521 err = sbprof_zbprof_stop();
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000522 mutex_unlock(&sbp.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000524
Mark Masond619f382007-03-29 11:39:56 -0700525 case SBPROF_ZBWAITFULL: {
526 err = wait_event_interruptible(sbp.tb_read, TB_FULL);
527 if (err)
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000528 break;
529
Mark Masond619f382007-03-29 11:39:56 -0700530 err = put_user(TB_FULL, (int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532 }
533
Mark Masond619f382007-03-29 11:39:56 -0700534 default:
535 err = -EINVAL;
536 break;
537 }
538
539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800542static const struct file_operations sbprof_tb_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .owner = THIS_MODULE,
544 .open = sbprof_tb_open,
545 .release = sbprof_tb_release,
546 .read = sbprof_tb_read,
Ralf Baechleb288f132005-09-30 01:51:21 +0100547 .unlocked_ioctl = sbprof_tb_ioctl,
548 .compat_ioctl = sbprof_tb_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 .mmap = NULL,
550};
551
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000552static struct class *tb_class;
553static struct device *tb_dev;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int __init sbprof_tb_init(void)
556{
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000557 struct device *dev;
558 struct class *tbc;
559 int err;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (register_chrdev(SBPROF_TB_MAJOR, DEVNAME, &sbprof_tb_fops)) {
562 printk(KERN_WARNING DEVNAME ": initialization failed (dev %d)\n",
563 SBPROF_TB_MAJOR);
564 return -EIO;
565 }
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000566
567 tbc = class_create(THIS_MODULE, "sb_tracebuffer");
568 if (IS_ERR(tbc)) {
569 err = PTR_ERR(tbc);
570 goto out_chrdev;
571 }
572
573 tb_class = tbc;
574
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700575 dev = device_create(tbc, NULL, MKDEV(SBPROF_TB_MAJOR, 0), NULL, "tb");
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000576 if (IS_ERR(dev)) {
577 err = PTR_ERR(dev);
578 goto out_class;
579 }
580 tb_dev = dev;
581
Ralf Baechle36ac8292009-09-28 16:57:54 +0100582 sbp.open = SB_CLOSED;
583 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 tb_period = zbbus_mhz * 10000LL;
Mark Masond619f382007-03-29 11:39:56 -0700585 pr_info(DEVNAME ": initialized - tb_period = %lld\n",
586 (long long) tb_period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000588
589out_class:
590 class_destroy(tb_class);
591out_chrdev:
592 unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);
593
594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597static void __exit sbprof_tb_cleanup(void)
598{
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000599 device_destroy(tb_class, MKDEV(SBPROF_TB_MAJOR, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000601 class_destroy(tb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604module_init(sbprof_tb_init);
605module_exit(sbprof_tb_cleanup);
Ralf Baechlebb9b8132007-03-09 15:59:56 +0000606
607MODULE_ALIAS_CHARDEV_MAJOR(SBPROF_TB_MAJOR);
608MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
609MODULE_LICENSE("GPL");